Skip to content

Commit 677e5e3

Browse files
test(config): pin that a reset settles every delete before it rejects
The Zoo-Code-Org#2235 row asked for a regression test for the straggler delete: a secrets.delete that lands after resetAllState() has already rejected, once the guard is back down, so a save that slipped into that window is erased from storage while the cache still serves it. The first version of this case could not be written against the settled shape at all. Held open by the test and awaited before the delete was released, resetAllState() simply never settled - vitest timed it out at 20 s. That is itself the answer to the row: on this shape the ordering the row describes is unreachable, because the reset does not reject until every delete has landed. So the case observes the ordering instead of the hole. openRouterApiKey's delete rejects at once, apiKey's lands 20 ms later on its own, and the case records whether it had landed by the moment the reset's rejection was observed. It also asserts the guard window the fix buys: a save issued while the pass is still settling is refused with SettingsWriteError rather than accepted, and the first save after the reset resolves and leaves storage and cache holding the same value. Measured with four runs of vitest run core/config in this tree, and the numstat below is from the same runs: at 41bd742, no case Test Files 10 passed (10) Tests 320 passed (320) (exit 0) case added Test Files 10 passed (10) Tests 321 passed (321) (exit 0) CONTROL, CX from Test Files 1 failed | 9 passed (10) Tests 1 failed | 320 passed (321), AssertionError: expected false to be true // Object.is equality | - Expect 492cfb3, case kept (the pre-settle shape: the reset rejects while apiKey's delete is still outstanding, so stragglerLanded is false at that assertion) final shape, formatted Test Files 10 passed (10) Tests 321 passed (321) (exit 0) Test-only: 41 added / 0 deleted in ContextProxy.spec.ts and nothing else, from the git diff --numstat that fed this message. ContextProxy.ts is untouched. tsc --noEmit -p . exit 0, eslint --max-warnings=0 exit 0, prettier --check 0 on the committed copy and 0 on the HEAD copy fed through the same invocation. --prune-suppressions leaves spec {"@typescript-eslint/no-explicit-any":{"count":5}}, CX {"@typescript-eslint/no-explicit-any":{"count":1}} exactly as recorded and the suppressions file is not in the commit. No production behaviour changes, so no new observable is introduced here.
1 parent 41bd742 commit 677e5e3

1 file changed

Lines changed: 41 additions & 0 deletions

File tree

‎src/core/config/__tests__/ContextProxy.spec.ts‎

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1310,6 +1310,47 @@ Output only the summary of the conversation so far, without any additional comme
13101310
expect(stateCache["polluted"]).toBeUndefined()
13111311
expect(mockGlobalState.update).not.toHaveBeenCalledWith("__proto__", expect.anything())
13121312
})
1313+
it("settles every delete before the reset rejects, so no save can land between the guard coming down and a straggler erasing it", async () => {
1314+
const durable = new Map<string, string | undefined>()
1315+
mockSecrets.get.mockImplementation(async (key: string) => durable.get(key))
1316+
mockSecrets.store.mockImplementation(async (key: string, value: string) => {
1317+
durable.set(key, value)
1318+
})
1319+
// openRouterApiKey rejects first; apiKey lands 20 ms later on its own, the way a slow
1320+
// keychain call does. stragglerLanded is what the two shapes disagree about.
1321+
let stragglerLanded = false
1322+
let landStraggler: (() => void) | undefined
1323+
mockSecrets.delete.mockImplementation((key: string) => {
1324+
if (key === "openRouterApiKey") {
1325+
return Promise.reject(new Error("keychain locked"))
1326+
}
1327+
if (key === "apiKey") {
1328+
return new Promise<void>((resolve) => {
1329+
landStraggler = () => {
1330+
durable.delete(key)
1331+
stragglerLanded = true
1332+
resolve()
1333+
}
1334+
})
1335+
}
1336+
durable.delete(key)
1337+
return Promise.resolve()
1338+
})
1339+
const reset = proxy.resetAllState()
1340+
setTimeout(() => landStraggler?.(), 20)
1341+
// While the pass is still settling the guard is up, so a save here is refused rather than
1342+
// accepted and erased later. That refusal is the observable the fix bought.
1343+
await expect(proxy.storeSecret("apiKey", "mid-settling-save")).rejects.toThrow(
1344+
"secret writes are refused while state is being reset",
1345+
)
1346+
await expect(reset).rejects.toThrow("keychain locked")
1347+
// A reset that rejects before every delete has landed leaves an outstanding delete that can
1348+
// still reach storage after the guard is down. This is the line the two shapes split on.
1349+
expect(stragglerLanded).toBe(true)
1350+
await expect(proxy.storeSecret("apiKey", "saved-after-settled-reset")).resolves.toBeUndefined()
1351+
expect(await mockSecrets.get("apiKey")).toBe("saved-after-settled-reset")
1352+
expect(proxy.getSecret("apiKey")).toBe("saved-after-settled-reset")
1353+
})
13131354
})
13141355

13151356
describe("state write generations", () => {

0 commit comments

Comments
 (0)