Skip to content

Self-hosted, Self-Improving, Federated Everything App — dev management, agent orchestration, persistent mind, digital twin, second brain, and creative pipeline from a single dashboard over Tailscale to port the user into the OS

fix: keep a corrupt Codex metadata file from taking down the agent index

[._.]/ Adam Eivy611ef69d9b60
diff --git a/server/services/codexSummaryRepair.js b/server/services/codexSummaryRepair.js
index 20d8b25ba1..c86699d85a 100644
--- a/server/services/codexSummaryRepair.js
+++ b/server/services/codexSummaryRepair.js
@@ -7,7 +7,7 @@
import { existsSync } from 'fs';
import { join } from 'path';
-import { atomicWrite, tryReadFile } from '../lib/fileUtils.js';
+import { atomicWrite, safeJSONParse, tryReadFile } from '../lib/fileUtils.js';
import { extractCodexAssistantTail } from '../lib/codexAssistantExtract.js';
// Any taskSummary above this size is almost certainly a transcript dump —
@@ -43,8 +43,11 @@ export async function repairCodexTaskSummary(agentDir, agent) {
const metaPath = join(agentDir, 'metadata.json');
const rawContent = await tryReadFile(metaPath);
if (!rawContent) return null;
- const raw = JSON.parse(rawContent);
- raw.metadata = { ...(raw.metadata || {}), taskSummary: repaired, simplifySummary: null };
+ // A crashed writer can leave truncated JSON. Throwing here rejects the
+ // whole agent index promise (cosAgentIndex.loadAgentIndex).
+ const raw = safeJSONParse(rawContent, null, { logError: false });
+ if (!raw || typeof raw !== 'object' || Array.isArray(raw)) return null;
+ raw.metadata = { ...(raw.metadata && typeof raw.metadata === 'object' && !Array.isArray(raw.metadata) ? raw.metadata : {}), taskSummary: repaired, simplifySummary: null };
await atomicWrite(metaPath, raw);
const beforeSize = (storedTask?.length || 0) + (storedSimplify?.length || 0);
diff --git a/server/services/codexSummaryRepair.test.js b/server/services/codexSummaryRepair.test.js
index 4e6afce62a..79a46c0331 100644
--- a/server/services/codexSummaryRepair.test.js
+++ b/server/services/codexSummaryRepair.test.js
@@ -111,6 +111,19 @@ describe('repairCodexTaskSummary', () => {
expect(persisted.metadata.simplifySummary).toBeNull();
});
+ it('returns null without throwing when metadata.json is malformed or empty', async () => {
+ const wonky = 'x'.repeat(25_000);
+ const output = 'apply patch\ntokens used\n100\nThe real summary.';
+ const agent = { id: 'agent-test', metadata: { taskSummary: wonky } };
+
+ for (const body of ['{not json', '', ' ', 'null', '[]']) {
+ await writeFile(join(agentDir, 'output.txt'), output);
+ await writeFile(join(agentDir, 'metadata.json'), body);
+ await expect(repairCodexTaskSummary(agentDir, agent)).resolves.toBeNull();
+ expect(await readFile(join(agentDir, 'metadata.json'), 'utf-8')).toBe(body);
+ }
+ });
+
it('repairs when only simplifySummary is wonky (taskSummary already small)', async () => {
const wonkySimplify = 'y'.repeat(25_000);
const output = 'apply patch\ntokens used\n100\nFinal summary.';