Skip to content

Commit d53c056

Browse files
committed
Migrate token count cache from global state to file-based storage and remove legacy state key
1 parent 25ae8ed commit d53c056

5 files changed

Lines changed: 85 additions & 19 deletions

File tree

‎packages/vscode/src/constants/state-keys.ts‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,6 @@ export const LAST_REFACTOR_INSTRUCTION_SOURCE_STATE_KEY =
2929
'last-refactor-instruction-source'
3030
export const LAST_REFACTOR_INSTRUCTION_STATE_KEY = 'last-refactor-instruction'
3131

32-
export const TOKEN_COUNTS_CACHE = 'token-count-cache'
33-
3432
export const CHECKPOINTS_STATE_KEY = 'checkpoints'
3533
export const TEMPORARY_CHECKPOINT_STATE_KEY = 'temporary-checkpoint'
3634
export const CHECKPOINT_OPERATION_IN_PROGRESS_STATE_KEY =

‎packages/vscode/src/context/providers/workspace/modules/token-calculator.ts‎

Lines changed: 41 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import * as vscode from 'vscode'
22
import * as fs from 'fs'
33
import * as path from 'path'
4-
import { TOKEN_COUNTS_CACHE } from '@/constants/state-keys'
54
import { dictionary } from '@shared/constants/dictionary'
65
import { Logger } from '@shared/utils/logger'
76
import type { IWorkspaceProvider } from '../workspace-provider'
@@ -20,6 +19,8 @@ type TokenCountsCache = {
2019

2120
const SHOW_COUNTING_NOTIFICATION_DELAY_MS = 3000
2221

22+
const TOKEN_CACHE_FILE_NAME = 'token-count-cache.json'
23+
2324
export class TokenCalculator implements vscode.Disposable {
2425
private _file_token_counts: Map<string, number> = new Map()
2526
private _directory_token_counts: Map<string, number> = new Map()
@@ -35,9 +36,15 @@ export class TokenCalculator implements vscode.Disposable {
3536
this._load_token_cache()
3637
}
3738

38-
private _load_token_cache(): void {
39-
this._token_cache =
40-
this._context.globalState.get<TokenCountsCache>(TOKEN_COUNTS_CACHE) ?? {}
39+
private async _load_token_cache(): Promise<void> {
40+
try {
41+
const storage_path = this._context.globalStorageUri.fsPath
42+
const cache_path = path.join(storage_path, TOKEN_CACHE_FILE_NAME)
43+
const content = await fs.promises.readFile(cache_path, 'utf8')
44+
this._token_cache = JSON.parse(content)
45+
} catch (error) {
46+
this._token_cache = {}
47+
}
4148
}
4249

4350
private _update_token_counts_cache(): void {
@@ -48,9 +55,19 @@ export class TokenCalculator implements vscode.Disposable {
4855
}
4956

5057
this._token_cache_update_timeout = setTimeout(async () => {
51-
const current_global_cache =
52-
this._context.globalState.get<TokenCountsCache>(TOKEN_COUNTS_CACHE) ??
53-
{}
58+
const storage_path = this._context.globalStorageUri.fsPath
59+
const cache_path = path.join(storage_path, TOKEN_CACHE_FILE_NAME)
60+
let current_global_cache: TokenCountsCache = {}
61+
62+
try {
63+
if (!fs.existsSync(storage_path)) {
64+
await fs.promises.mkdir(storage_path, { recursive: true })
65+
}
66+
const content = await fs.promises.readFile(cache_path, 'utf8')
67+
current_global_cache = JSON.parse(content)
68+
} catch {
69+
// Cache file might not exist yet or be corrupt
70+
}
5471

5572
for (const root of this._provider.getWorkspaceRoots()) {
5673
if (this._token_cache[root]) {
@@ -66,16 +83,24 @@ export class TokenCalculator implements vscode.Disposable {
6683
}
6784
}
6885

69-
await this._context.globalState.update(
70-
TOKEN_COUNTS_CACHE,
71-
current_global_cache
72-
)
86+
try {
87+
await fs.promises.writeFile(
88+
cache_path,
89+
JSON.stringify(current_global_cache)
90+
)
7391

74-
Logger.info({
75-
function_name: '_update_token_counts_cache',
76-
message: 'Token counts cache updated'
77-
})
78-
this._has_token_counts_cache_updated_once = true
92+
Logger.info({
93+
function_name: '_update_token_counts_cache',
94+
message: 'Token counts cache updated'
95+
})
96+
this._has_token_counts_cache_updated_once = true
97+
} catch (error) {
98+
Logger.error({
99+
function_name: '_update_token_counts_cache',
100+
message: 'Error writing token cache',
101+
data: error
102+
})
103+
}
79104
}, 10000)
80105
}
81106

‎packages/vscode/src/extension.ts‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ import { WebSocketManager } from './services/websocket-manager'
55
import { ApiManager } from './services/api-manager'
66
import {
77
migrate_preset_is_default_to_is_selected,
8-
migrate_api_providers_to_model_providers
8+
migrate_api_providers_to_model_providers,
9+
migrate_token_count_cache
910
} from './migrations'
1011
import {
1112
apply_chat_response_command,
@@ -50,6 +51,8 @@ export async function activate(context: vscode.ExtensionContext) {
5051
await migrate_preset_is_default_to_is_selected(context)
5152
// 16 September 2025
5253
await migrate_api_providers_to_model_providers(context)
54+
// 4 January 2026
55+
await migrate_token_count_cache(context)
5356
}
5457

5558
await migrations()
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
export * from './migrate-preset-is-default-to-is-selected'
22
export * from './migrate-api-providers-to-model-providers'
3+
export * from './migrate-token-count-cache'
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import * as vscode from 'vscode'
2+
import { Logger } from '@shared/utils/logger'
3+
4+
const MIGRATION_ID = 'token-count-cache-cleanup-20260104'
5+
const OLD_KEY = 'token-count-cache'
6+
7+
/**
8+
* Migration to clear the old token count cache from global state
9+
* as it has been moved to a file-based cache.
10+
*/
11+
export async function migrate_token_count_cache(
12+
context: vscode.ExtensionContext
13+
): Promise<void> {
14+
try {
15+
if (context.globalState.get(MIGRATION_ID)) {
16+
return
17+
}
18+
19+
const old_cache = context.globalState.get(OLD_KEY)
20+
21+
if (old_cache) {
22+
// Passing undefined removes the key from storage
23+
await context.globalState.update(OLD_KEY, undefined)
24+
25+
Logger.info({
26+
function_name: 'migrate_token_count_cache',
27+
message: 'Successfully cleared old token count cache from global state'
28+
})
29+
}
30+
31+
await context.globalState.update(MIGRATION_ID, true)
32+
} catch (error) {
33+
Logger.error({
34+
function_name: 'migrate_token_count_cache',
35+
message: 'Error cleaning up old token count cache',
36+
data: error instanceof Error ? error.message : String(error)
37+
})
38+
}
39+
}

0 commit comments

Comments
 (0)