Skip to content

Commit 1ba588e

Browse files
committed
fix(codex): report whether a sync actually wrote the catalog or cache
syncCatalogModels() and invalidateCodexModelsCache() both succeeded silently whether or not they wrote anything, so a caller could not tell a real catalog update from a no-op on a missing or unreadable catalog. Return that fact: syncCatalogModels() gains catalogWritten, and invalidateCodexModelsCache() returns whether it rewrote models_cache. refreshCodexModelCatalog() carries both outward. This is the signal half of lidge-jun#518, split out so it can be reviewed on its own. Nothing acts on it yet — the consumer (warning about, and optionally restarting, stale app-server processes) is the other half, and it touches process termination, which deserves its own review.
1 parent 4618c93 commit 1ba588e

6 files changed

Lines changed: 86 additions & 14 deletions

File tree

‎src/codex/catalog/sync.ts‎

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -457,11 +457,12 @@ export function mergeCatalogEntriesForSync(
457457
export async function syncCatalogModels(config: OcxConfig): Promise<{
458458
added: number;
459459
path: string;
460+
catalogWritten: boolean;
460461
comboOmissions: ComboCatalogOmission[];
461462
}> {
462463
const catalogPath = readCodexCatalogPath();
463464
const catalog = loadCatalogForSync(catalogPath);
464-
if (!catalog) return { added: 0, path: catalogPath, comboOmissions: [] };
465+
if (!catalog) return { added: 0, path: catalogPath, catalogWritten: false, comboOmissions: [] };
465466

466467
const template = findNativeTemplate(catalog);
467468

@@ -500,7 +501,7 @@ export async function syncCatalogModels(config: OcxConfig): Promise<{
500501
clampCatalogModelsToCodexSupport(catalog.models);
501502

502503
atomicWriteFile(catalogPath, JSON.stringify(catalog, null, 2) + "\n");
503-
return { added: goEntries.length, path: catalogPath, comboOmissions };
504+
return { added: goEntries.length, path: catalogPath, catalogWritten: true, comboOmissions };
504505
}
505506

506507
export function restoreCodexCatalog(): { removed: number; kept: number; path: string } {
@@ -531,10 +532,11 @@ export function restoreCodexCatalog(): { removed: number; kept: number; path: st
531532
return { removed, kept: native.length, path: catalogPath };
532533
}
533534

534-
export function invalidateCodexModelsCache(): void {
535+
/** Force Codex's models_cache stale from the on-disk catalog. Returns whether a cache write occurred. */
536+
export function invalidateCodexModelsCache(): boolean {
535537
try {
536538
const catalogPath = readCodexCatalogPath();
537-
if (!existsSync(catalogPath)) return;
539+
if (!existsSync(catalogPath)) return false;
538540
const catalog = JSON.parse(readFileSync(catalogPath, "utf8"));
539541
const models = catalog.models ?? catalog;
540542
const wrapper = {
@@ -543,5 +545,8 @@ export function invalidateCodexModelsCache(): void {
543545
models,
544546
};
545547
atomicWriteFile(activeCodexModelsCachePath(), JSON.stringify(wrapper, null, 2) + "\n");
546-
} catch { /* best-effort */ }
548+
return true;
549+
} catch {
550+
return false;
551+
}
547552
}

‎src/codex/refresh.ts‎

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ export interface CodexCatalogRefreshResult {
99
added: number;
1010
path: string;
1111
catalogExists: boolean;
12+
catalogWritten: boolean;
1213
cacheSynced: boolean;
1314
comboOmissions: ComboCatalogOmission[];
1415
}
@@ -42,8 +43,11 @@ export async function refreshCodexModelCatalog(
4243
): Promise<CodexCatalogRefreshResult> {
4344
const result = await deps.syncCatalogModels(config);
4445
const catalogExists = deps.existsSync(result.path);
46+
const catalogWritten = result.catalogWritten === true;
4547
const comboOmissions = result.comboOmissions ?? [];
46-
if (!catalogExists) return { ...result, catalogExists, cacheSynced: false, comboOmissions };
47-
deps.invalidateCodexModelsCache();
48-
return { ...result, catalogExists, cacheSynced: true, comboOmissions };
48+
if (!catalogExists) {
49+
return { ...result, catalogExists, catalogWritten: false, cacheSynced: false, comboOmissions };
50+
}
51+
const cacheSynced = deps.invalidateCodexModelsCache();
52+
return { ...result, catalogExists, catalogWritten, cacheSynced, comboOmissions };
4953
}

‎src/codex/sync.ts‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ export interface CodexSyncResult {
1111
added: number;
1212
catalogPath: string | null;
1313
catalogExists: boolean;
14+
catalogWritten: boolean;
1415
cacheSynced: boolean;
1516
message: string;
1617
warning?: string;
@@ -61,6 +62,7 @@ export async function syncModelsToCodex(
6162
added: 0,
6263
catalogPath: null,
6364
catalogExists: false,
65+
catalogWritten: false,
6466
cacheSynced: false,
6567
message: result.message,
6668
};
@@ -71,6 +73,7 @@ export async function syncModelsToCodex(
7173
let catalogPath: string | null = null;
7274
let catalogPathForInjection: string | null | undefined;
7375
let catalogExists = false;
76+
let catalogWritten = false;
7477
let cacheSynced = false;
7578
let warning: string | undefined;
7679
let comboOmissions: ComboCatalogOmission[] = [];
@@ -79,6 +82,7 @@ export async function syncModelsToCodex(
7982
const cat = await deps.refreshCodexModelCatalog(config);
8083
added = cat.added;
8184
catalogExists = cat.catalogExists;
85+
catalogWritten = cat.catalogWritten;
8286
cacheSynced = cat.cacheSynced;
8387
catalogPathForInjection = cat.catalogExists ? cat.path : null;
8488
catalogPath = catalogPathForInjection;
@@ -110,6 +114,7 @@ export async function syncModelsToCodex(
110114
added,
111115
catalogPath,
112116
catalogExists,
117+
catalogWritten,
113118
cacheSynced,
114119
message: result.message,
115120
...(warning ? { warning } : {}),

‎tests/codex-refresh.test.ts‎

Lines changed: 57 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,24 @@ describe("Codex catalog refresh", () => {
1212
test("writes an expired Codex models cache whenever the materialized catalog exists", async () => {
1313
let invalidated = 0;
1414
const result = await refreshCodexModelCatalog(config, {
15-
syncCatalogModels: async () => ({ added: 0, path: "/tmp/opencodex-catalog.json", comboOmissions: [] }),
16-
invalidateCodexModelsCache: () => { invalidated += 1; },
15+
syncCatalogModels: async () => ({
16+
added: 0,
17+
path: "/tmp/opencodex-catalog.json",
18+
catalogWritten: true,
19+
comboOmissions: [],
20+
}),
21+
invalidateCodexModelsCache: () => {
22+
invalidated += 1;
23+
return true;
24+
},
1725
existsSync: () => true,
1826
});
1927

2028
expect(result).toEqual({
2129
added: 0,
2230
path: "/tmp/opencodex-catalog.json",
2331
catalogExists: true,
32+
catalogWritten: true,
2433
cacheSynced: true,
2534
comboOmissions: [],
2635
});
@@ -30,14 +39,58 @@ describe("Codex catalog refresh", () => {
3039
test("does not touch the cache when no Codex catalog can be materialized", async () => {
3140
let invalidated = 0;
3241
const result = await refreshCodexModelCatalog(config, {
33-
syncCatalogModels: async () => ({ added: 0, path: "/tmp/missing-catalog.json", comboOmissions: [] }),
34-
invalidateCodexModelsCache: () => { invalidated += 1; },
42+
syncCatalogModels: async () => ({
43+
added: 0,
44+
path: "/tmp/missing-catalog.json",
45+
catalogWritten: false,
46+
comboOmissions: [],
47+
}),
48+
invalidateCodexModelsCache: () => {
49+
invalidated += 1;
50+
return true;
51+
},
3552
existsSync: () => false,
3653
});
3754

3855
expect(result.catalogExists).toBe(false);
56+
expect(result.catalogWritten).toBe(false);
3957
expect(result.cacheSynced).toBe(false);
4058
expect(result.comboOmissions).toEqual([]);
4159
expect(invalidated).toBe(0);
4260
});
61+
62+
test("reports cacheSynced false when invalidate cannot write", async () => {
63+
const result = await refreshCodexModelCatalog(config, {
64+
syncCatalogModels: async () => ({
65+
added: 0,
66+
path: "/tmp/opencodex-catalog.json",
67+
catalogWritten: true,
68+
comboOmissions: [],
69+
}),
70+
invalidateCodexModelsCache: () => false,
71+
existsSync: () => true,
72+
});
73+
74+
expect(result.catalogExists).toBe(true);
75+
expect(result.catalogWritten).toBe(true);
76+
expect(result.cacheSynced).toBe(false);
77+
expect(result.comboOmissions).toEqual([]);
78+
});
79+
80+
test("preserves catalogWritten false when the catalog path exists but sync did not write", async () => {
81+
const result = await refreshCodexModelCatalog(config, {
82+
syncCatalogModels: async () => ({
83+
added: 0,
84+
path: "/tmp/broken-catalog.json",
85+
catalogWritten: false,
86+
comboOmissions: [],
87+
}),
88+
invalidateCodexModelsCache: () => false,
89+
existsSync: () => true,
90+
});
91+
92+
expect(result.catalogExists).toBe(true);
93+
expect(result.catalogWritten).toBe(false);
94+
expect(result.cacheSynced).toBe(false);
95+
});
4396
});

‎tests/codex-sync-api.test.ts‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ describe("GUI/CLI Codex sync backend", () => {
5353
added: 3,
5454
path: "/tmp/opencodex-catalog.json",
5555
catalogExists: true,
56+
catalogWritten: true,
5657
cacheSynced: true,
5758
comboOmissions: [],
5859
}),
@@ -72,6 +73,7 @@ describe("GUI/CLI Codex sync backend", () => {
7273
added: 3,
7374
catalogPath: "/tmp/opencodex-catalog.json",
7475
catalogExists: true,
76+
catalogWritten: true,
7577
cacheSynced: true,
7678
message: "injected",
7779
});
@@ -93,6 +95,7 @@ describe("GUI/CLI Codex sync backend", () => {
9395
added: 1,
9496
path: "/tmp/opencodex-catalog.json",
9597
catalogExists: true,
98+
catalogWritten: true,
9699
cacheSynced: true,
97100
comboOmissions: [omission],
98101
}),
@@ -121,6 +124,7 @@ describe("GUI/CLI Codex sync backend", () => {
121124
added: 0,
122125
path: "/tmp/opencodex-catalog.json",
123126
catalogExists: true,
127+
catalogWritten: true,
124128
cacheSynced: true,
125129
comboOmissions: [omission],
126130
}),
@@ -192,6 +196,7 @@ describe("GUI/CLI Codex sync backend", () => {
192196
added: 0,
193197
catalogPath: null,
194198
catalogExists: false,
199+
catalogWritten: false,
195200
cacheSynced: false,
196201
message: "external provider preserved",
197202
});

‎tests/injection-model-api.test.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -249,9 +249,9 @@ describe("/api/injection-model guidance kill switch + partial update", () => {
249249
await refreshCodexModelCatalog(config, {
250250
syncCatalogModels: async syncedConfig => {
251251
flagSeenBySync = syncedConfig.multiAgentGuidanceEnabled;
252-
return { added: 0, path: join(tempHome!, "missing-catalog.json") };
252+
return { added: 0, path: join(tempHome!, "missing-catalog.json"), catalogWritten: false };
253253
},
254-
invalidateCodexModelsCache: () => {},
254+
invalidateCodexModelsCache: () => false,
255255
existsSync: () => false,
256256
});
257257
expect(flagSeenBySync).toBe(false);

0 commit comments

Comments
 (0)