Skip to content

armgit / cindy

Public Imported from GitHub
0

Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。

fix(context): drop the cached prefs store after a staged compaction clear (#7537)

Zhao Dengfeng6e5065489dbe
diff --git a/apps/desktop/src/main/maker-host/__tests__/model-context-limit-store.test.ts b/apps/desktop/src/main/maker-host/__tests__/model-context-limit-store.test.ts
index fb61dfddf4..cee623ed7f 100644
--- a/apps/desktop/src/main/maker-host/__tests__/model-context-limit-store.test.ts
+++ b/apps/desktop/src/main/maker-host/__tests__/model-context-limit-store.test.ts
@@ -99,14 +99,28 @@ describe('model context limit store', () => {
await save([target, retired], 65);
await save([other], 80);
writeModelContextLimit('codex', providerId, 'gpt', 272_000);
- const restore = stageProviderModelCompactionClear(providerId);
+ // Windows CI: the staged write can land within the cached store's mtime
+ // granularity. Pin the mtime back so the stale-cache case is deterministic.
+ let pinned = 1_700_000_000;
+ const withSameMtime = <T>(write: () => T): T => {
+ // Cache the file under an exact whole-second mtime, write, then put that
+ // same mtime back: the cached store sees no change, as on Windows.
+ pinned += 10;
+ fs.utimesSync(PREFS, pinned, pinned);
+ readModelCompactionPct(providerId, 'gpt');
+ const result = write();
+ fs.utimesSync(PREFS, pinned, pinned);
+ return result;
+ };
+ const restore = withSameMtime(() => stageProviderModelCompactionClear(providerId));
expect(readModelCompactionPct(providerId, 'gpt')).toBeNull();
expect(readModelCompactionPct(providerId, 'retired')).toBeNull();
expect(readModelCompactionPct(other.providerId, 'gpt')).toBe(80);
expect(readModelContextLimit('codex', providerId, 'gpt')).toBe(272_000);
expect(stageProviderModelCompactionClear(providerId)()).toBe(true);
await save([other], 70);
- expect(restore()).toBe(true);
+ expect(readModelCompactionPct(other.providerId, 'gpt')).toBe(70);
+ expect(withSameMtime(restore)).toBe(true);
expect(readModelCompactionPct(providerId, 'gpt')).toBe(65);
expect(readModelCompactionPct(providerId, 'retired')).toBe(65);
expect(readModelCompactionPct(other.providerId, 'gpt')).toBe(70);
diff --git a/apps/desktop/src/main/maker-host/model-context-limit-store.ts b/apps/desktop/src/main/maker-host/model-context-limit-store.ts
index 48efd9cb7d..3c82d8be9c 100644
--- a/apps/desktop/src/main/maker-host/model-context-limit-store.ts
+++ b/apps/desktop/src/main/maker-host/model-context-limit-store.ts
@@ -148,6 +148,15 @@ export function readModelCompactionPct(providerId: string, modelId: string): num
}
/** Participate in the existing provider deletion transaction, including rollback. */
+/**
+ * A separate owner-pinned store wrote the shared prefs file. The module store's
+ * mtime guard can miss that write when both land within the filesystem's mtime
+ * granularity (seen on Windows CI), so drop its cache, as the rollback paths do.
+ */
+function dropCachedStore(): void {
+ store = createStore(() => ownerScopedUserDataPath('model-context-limit-prefs.json'));
+}
+
export function stageProviderModelCompactionClear(providerId: string): () => boolean {
const filePath = ownerScopedUserDataPath('model-context-limit-prefs.json');
const ownerStore = createStore(() => filePath);
@@ -159,6 +168,7 @@ export function stageProviderModelCompactionClear(providerId: string): () => boo
ownerStore.writePatch({
compactionPcts: Object.fromEntries(Object.entries(previous).filter(([key]) => !key.startsWith(prefix))),
});
+ dropCachedStore();
return () => {
try {
ownerStore.invalidateIfChanged();
@@ -166,6 +176,7 @@ export function stageProviderModelCompactionClear(providerId: string): () => boo
// Restore only removed entries; do not overwrite a newer external edit.
for (const [key, value] of Object.entries(removed)) current[key] ??= value;
ownerStore.writePatch({ compactionPcts: current });
+ dropCachedStore();
return true;
} catch (error) {
log.warn('restore provider model compaction failed', {