Skip to content

Commit 61a1eda

Browse files
author
jun
committed
fix(windows): degrade an invalidated in-flight catalog observation to unknown
The generation check suppressed the cache write but still returned the pre-write status to the request already awaiting it. That request then saw `fresh`, which is the one state authorizing positive model guidance (collaboration.ts returns null for stale/unknown), so it would advertise the newly written disk catalog to an app-server whose in-memory copy the write had just made stale. That trades the hang this change exists to fix for a wrong answer, which is the worse failure. Return `unknown` when the generation moved: it is what an invalidated observation actually knows, and the guidance path already treats it as say-nothing. The existing regression asserted the old `fresh` result, so it pinned the defect; it now asserts `unknown`, and a companion test proves the next post-write observation still reports `fresh` rather than being poisoned. Both fail when the fix is reverted.
1 parent 53ffc9e commit 61a1eda

2 files changed

Lines changed: 58 additions & 4 deletions

File tree

‎src/codex/app-server-processes.ts‎

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -900,9 +900,25 @@ export async function collectCodexAppServerCatalogStateForRequest(
900900
}));
901901
let flight: RequestCatalogStateFlight;
902902
const promise = pending.then(status => {
903-
// A catalog write can invalidate while slow CIM is still running. Never
904-
// let that pre-write result repopulate the post-write cache.
905-
if (requestCatalogStateGeneration === generation && requestCatalogStateFlight === flight) {
903+
// A catalog write can invalidate while slow CIM is still running. The result
904+
// describes the PRE-write world, so it must neither repopulate the post-write
905+
// cache nor reach the caller.
906+
//
907+
// Suppressing only the cache write is not enough. The awaiting request still
908+
// received `fresh`, and `fresh` is the one state that authorizes positive
909+
// guidance (`src/server/responses/collaboration.ts:279-280` returns null for
910+
// `stale`/`unknown` but describes the catalog for `fresh`). So the request
911+
// would advertise the newly written disk catalog to an app-server whose
912+
// in-memory copy the write just made stale — a wrong answer, which is worse
913+
// than the slow answer this whole change exists to fix.
914+
//
915+
// Degrade to `unknown` instead: it is the honest description of what an
916+
// invalidated observation knows, and the guidance path already treats it as
917+
// "say nothing positive".
918+
if (requestCatalogStateGeneration !== generation) {
919+
return { state: "unknown" as const, processes: [], catalogMtimeMs: null };
920+
}
921+
if (requestCatalogStateFlight === flight) {
906922
requestCatalogStateCache = {
907923
generation,
908924
identity,

‎tests/codex-app-server-processes.test.ts‎

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,12 @@ describe("collectCodexAppServerCatalogState (#857)", () => {
152152
const staleFlight = collectCodexAppServerCatalogStateForRequest(io);
153153
resetCodexAppServerCatalogStateCache();
154154
releaseFirst?.([{ pid: 42, commandLine: APP_SERVER_CMD }]);
155-
await expect(staleFlight).resolves.toMatchObject({ state: "fresh" });
155+
// The awaiting request must NOT receive the pre-write `fresh`. It was true
156+
// before the invalidating write and is false after it, and `fresh` is the one
157+
// state that authorizes positive model guidance — so handing it back trades the
158+
// original hang for a wrong answer. `unknown` is what an invalidated
159+
// observation actually knows, and the guidance path already stays silent on it.
160+
await expect(staleFlight).resolves.toMatchObject({ state: "unknown" });
156161

157162
await expect(collectCodexAppServerCatalogStateForRequest(io)).resolves.toMatchObject({
158163
state: "not_running",
@@ -161,6 +166,39 @@ describe("collectCodexAppServerCatalogState (#857)", () => {
161166
resetCodexAppServerCatalogStateCache();
162167
});
163168

169+
test("an invalidated in-flight refresh does not poison the next request (#1852)", async () => {
170+
// Companion to the case above: degrading the obsolete result to `unknown` must
171+
// not also suppress the NEXT observation, which is made after the write and is
172+
// therefore the one the caller should trust.
173+
resetCodexAppServerCatalogStateCache();
174+
let calls = 0;
175+
let releaseFirst: ((snapshots: Array<{ pid: number; commandLine: string }>) => void) | undefined;
176+
const firstSnapshots = new Promise<Array<{ pid: number; commandLine: string }>>(resolve => {
177+
releaseFirst = resolve;
178+
});
179+
const io = {
180+
platform: "win32" as const,
181+
listSnapshotsAsync: async () => {
182+
calls += 1;
183+
if (calls === 1) return firstSnapshots;
184+
return [{ pid: 43, commandLine: APP_SERVER_CMD }];
185+
},
186+
readStartMsBatchAsync: async (pids: readonly number[]) => new Map(pids.map(pid => [pid, 2_000])),
187+
catalogMtimeMs: () => 1_000,
188+
};
189+
190+
const obsolete = collectCodexAppServerCatalogStateForRequest(io);
191+
resetCodexAppServerCatalogStateCache();
192+
releaseFirst?.([{ pid: 42, commandLine: APP_SERVER_CMD }]);
193+
await expect(obsolete).resolves.toMatchObject({ state: "unknown" });
194+
195+
await expect(collectCodexAppServerCatalogStateForRequest(io)).resolves.toMatchObject({
196+
state: "fresh",
197+
});
198+
expect(calls).toBe(2);
199+
resetCodexAppServerCatalogStateCache();
200+
});
201+
164202
test("Windows request collection briefly caches failed CIM enumeration (#1852)", async () => {
165203
resetCodexAppServerCatalogStateCache();
166204
let calls = 0;

0 commit comments

Comments
 (0)