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: lazy-repair pre-fix Codex agent summaries on read

[._.]/ Adam Eivyb535bc71a8fc
diff --git a/.changelog/NEXT.md b/.changelog/NEXT.md
index 1eaa465d6e..b661ffc315 100644
--- a/.changelog/NEXT.md
+++ b/.changelog/NEXT.md
@@ -391,6 +391,18 @@
show the full prompt on hover (the visible cell is still truncated to
80 chars).
+- **CoS agent summary — historical Codex agents now auto-repair on read.**
+ The previous fix (above) corrected the extractor for *new* Codex agents, but
+ agents that completed before the patch landed already had their
+ multi-megabyte transcript dumps persisted into `metadata.json` (e.g.
+ agent-5f6951e3: 5.2MB metadata, 737KB `taskSummary`, 4.4MB `simplifySummary`).
+ Adds `server/services/codexSummaryRepair.js`: when `getAgent` or
+ `getAgentsByDate` loads a completed agent whose `taskSummary` or
+ `simplifySummary` is ≥20KB, it re-reads `output.txt`, re-extracts the
+ assistant tail via `extractCodexAssistantTail`, clears the false
+ `simplifySummary` (Codex CLI cannot execute `/simplify`), and rewrites
+ metadata.json in place. One-time per agent, idempotent thereafter.
+
- **CoS agent summary — Codex output now extracts the actual final message.**
When a CoS task ran on the Codex provider, the persisted `taskSummary`
ballooned to multi-megabyte dumps containing every diff, grep result, and
diff --git a/server/services/codexSummaryRepair.js b/server/services/codexSummaryRepair.js
new file mode 100644
index 0000000000..91a79b842c
--- /dev/null
+++ b/server/services/codexSummaryRepair.js
@@ -0,0 +1,53 @@
+// Lazy repair for agent task summaries that were saved before the
+// `extractCodexAssistantTail` fix (commit 73c59162). Pre-fix Codex agents
+// had their entire transcript persisted into metadata.json.taskSummary
+// (often hundreds of KB to megabytes). When such an agent is loaded for
+// display, this module re-extracts the real assistant tail from the
+// matching output.txt and rewrites metadata.json in place.
+
+import { readFile, writeFile } from 'fs/promises';
+import { existsSync } from 'fs';
+import { join } from 'path';
+import { extractCodexAssistantTail } from '../lib/codexAssistantExtract.js';
+
+// Any taskSummary above this size is almost certainly a transcript dump —
+// real assistant summaries are typically a few KB at most.
+const WONKY_TASKSUMMARY_THRESHOLD = 20_000;
+
+export function isWonkyTaskSummary(summary) {
+ return typeof summary === 'string' && summary.length >= WONKY_TASKSUMMARY_THRESHOLD;
+}
+
+// Reads output.txt, re-extracts the Codex tail, and rewrites metadata.json.
+// Returns the repaired summary string when a repair was applied, or null
+// when no repair was needed/possible (also a no-op on non-Codex output).
+// Also clears any wonky `simplifySummary` — Codex CLI cannot execute the
+// `/simplify` slash command, so any pre-fix split was a false positive.
+export async function repairCodexTaskSummary(agentDir, agent) {
+ const storedTask = agent?.metadata?.taskSummary;
+ const storedSimplify = agent?.metadata?.simplifySummary;
+ const taskWonky = isWonkyTaskSummary(storedTask);
+ const simplifyWonky = isWonkyTaskSummary(storedSimplify);
+ if (!taskWonky && !simplifyWonky) return null;
+
+ const outputFile = join(agentDir, 'output.txt');
+ if (!existsSync(outputFile)) return null;
+
+ const fullOutput = await readFile(outputFile, 'utf-8').catch(() => null);
+ if (!fullOutput) return null;
+
+ const repaired = extractCodexAssistantTail(fullOutput);
+ if (!repaired) return null;
+ if (repaired === storedTask && !simplifyWonky) return null;
+
+ const metaPath = join(agentDir, 'metadata.json');
+ const rawContent = await readFile(metaPath, 'utf-8').catch(() => null);
+ if (!rawContent) return null;
+ const raw = JSON.parse(rawContent);
+ raw.metadata = { ...(raw.metadata || {}), taskSummary: repaired, simplifySummary: null };
+ await writeFile(metaPath, JSON.stringify(raw, null, 2));
+
+ const beforeSize = (storedTask?.length || 0) + (storedSimplify?.length || 0);
+ console.log(`🔧 Repaired Codex task summary for ${agent.id} (${beforeSize} → ${repaired.length} chars)`);
+ return repaired;
+}
diff --git a/server/services/codexSummaryRepair.test.js b/server/services/codexSummaryRepair.test.js
new file mode 100644
index 0000000000..4e6afce62a
--- /dev/null
+++ b/server/services/codexSummaryRepair.test.js
@@ -0,0 +1,125 @@
+import { describe, it, expect, beforeEach, afterEach } from 'vitest';
+import { mkdtemp, writeFile, readFile, rm } from 'fs/promises';
+import { tmpdir } from 'os';
+import { join } from 'path';
+import { repairCodexTaskSummary, isWonkyTaskSummary } from './codexSummaryRepair.js';
+
+describe('isWonkyTaskSummary', () => {
+ it('returns false for short summaries', () => {
+ expect(isWonkyTaskSummary('A short summary.\n- bullet')).toBe(false);
+ });
+
+ it('returns false for empty/non-string input', () => {
+ expect(isWonkyTaskSummary('')).toBe(false);
+ expect(isWonkyTaskSummary(null)).toBe(false);
+ expect(isWonkyTaskSummary(undefined)).toBe(false);
+ });
+
+ it('returns true for summaries above the 20KB threshold', () => {
+ expect(isWonkyTaskSummary('x'.repeat(20_001))).toBe(true);
+ });
+});
+
+describe('repairCodexTaskSummary', () => {
+ let agentDir;
+
+ beforeEach(async () => {
+ agentDir = await mkdtemp(join(tmpdir(), 'codex-repair-'));
+ });
+
+ afterEach(async () => {
+ await rm(agentDir, { recursive: true, force: true });
+ });
+
+ async function seed(metadata, outputBuffer) {
+ await writeFile(join(agentDir, 'metadata.json'), JSON.stringify({ id: 'agent-test', metadata }, null, 2));
+ if (outputBuffer != null) await writeFile(join(agentDir, 'output.txt'), outputBuffer);
+ }
+
+ it('rewrites metadata.json when stored summary is wonky and tail is recoverable', async () => {
+ const fakeTranscript = 'x'.repeat(25_000);
+ const output = [
+ fakeTranscript,
+ 'apply patch',
+ 'diff --git a/foo b/foo',
+ '+something',
+ 'tokens used',
+ '12,345',
+ 'Final assistant summary.',
+ '- bullet one',
+ ].join('\n');
+
+ await seed({ taskSummary: fakeTranscript }, output);
+
+ const repaired = await repairCodexTaskSummary(agentDir, { id: 'agent-test', metadata: { taskSummary: fakeTranscript } });
+ expect(repaired).toBe('Final assistant summary.\n- bullet one');
+
+ const persisted = JSON.parse(await readFile(join(agentDir, 'metadata.json'), 'utf-8'));
+ expect(persisted.metadata.taskSummary).toBe('Final assistant summary.\n- bullet one');
+ });
+
+ it('returns null and leaves metadata untouched when summary is below threshold', async () => {
+ await seed({ taskSummary: 'short' }, 'tokens used\n123\nA tail');
+ const before = await readFile(join(agentDir, 'metadata.json'), 'utf-8');
+
+ const result = await repairCodexTaskSummary(agentDir, { id: 'agent-test', metadata: { taskSummary: 'short' } });
+ expect(result).toBeNull();
+
+ const after = await readFile(join(agentDir, 'metadata.json'), 'utf-8');
+ expect(after).toBe(before);
+ });
+
+ it('returns null when output.txt is missing', async () => {
+ await seed({ taskSummary: 'x'.repeat(25_000) }, null);
+
+ const result = await repairCodexTaskSummary(agentDir, { id: 'agent-test', metadata: { taskSummary: 'x'.repeat(25_000) } });
+ expect(result).toBeNull();
+ });
+
+ it('returns null when output.txt has no Codex markers (cannot extract tail)', async () => {
+ const wonky = 'x'.repeat(25_000);
+ await seed({ taskSummary: wonky }, 'Just a Claude transcript with 🔧 Using Read\nNo codex markers.');
+
+ const result = await repairCodexTaskSummary(agentDir, { id: 'agent-test', metadata: { taskSummary: wonky } });
+ expect(result).toBeNull();
+ });
+
+ it('preserves other metadata fields when persisting the repair', async () => {
+ const wonky = 'x'.repeat(25_000);
+ const output = 'apply patch\ntokens used\n100\nThe real summary.';
+ await seed({ taskSummary: wonky, providerId: 'codex', model: 'gpt-5' }, output);
+
+ await repairCodexTaskSummary(agentDir, { id: 'agent-test', metadata: { taskSummary: wonky } });
+
+ const persisted = JSON.parse(await readFile(join(agentDir, 'metadata.json'), 'utf-8'));
+ expect(persisted.metadata.providerId).toBe('codex');
+ expect(persisted.metadata.model).toBe('gpt-5');
+ expect(persisted.metadata.taskSummary).toBe('The real summary.');
+ });
+
+ it('also clears wonky simplifySummary (Codex cannot run /simplify)', async () => {
+ const wonkyTask = 'x'.repeat(25_000);
+ const wonkySimplify = 'y'.repeat(25_000);
+ const output = 'apply patch\ntokens used\n100\nThe real summary.';
+ await seed({ taskSummary: wonkyTask, simplifySummary: wonkySimplify }, output);
+
+ const repaired = await repairCodexTaskSummary(agentDir, { id: 'agent-test', metadata: { taskSummary: wonkyTask, simplifySummary: wonkySimplify } });
+ expect(repaired).toBe('The real summary.');
+
+ const persisted = JSON.parse(await readFile(join(agentDir, 'metadata.json'), 'utf-8'));
+ expect(persisted.metadata.taskSummary).toBe('The real summary.');
+ expect(persisted.metadata.simplifySummary).toBeNull();
+ });
+
+ 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.';
+ await seed({ taskSummary: 'already small', simplifySummary: wonkySimplify }, output);
+
+ const repaired = await repairCodexTaskSummary(agentDir, { id: 'agent-test', metadata: { taskSummary: 'already small', simplifySummary: wonkySimplify } });
+ expect(repaired).toBe('Final summary.');
+
+ const persisted = JSON.parse(await readFile(join(agentDir, 'metadata.json'), 'utf-8'));
+ expect(persisted.metadata.simplifySummary).toBeNull();
+ });
+});
diff --git a/server/services/cosAgents.js b/server/services/cosAgents.js
index c4c7ed124d..d10c57dba1 100644
--- a/server/services/cosAgents.js
+++ b/server/services/cosAgents.js
@@ -12,6 +12,7 @@ import { join } from 'path';
import { cosEvents, emitLog } from './cosEvents.js';
import { loadState, saveState, withStateLock, AGENTS_DIR } from './cosState.js';
import { ensureDir, safeJSONParse } from '../lib/fileUtils.js';
+import { repairCodexTaskSummary } from './codexSummaryRepair.js';
const INDEX_FILE = join(AGENTS_DIR, 'index.json');
@@ -402,7 +403,10 @@ export async function getAgentsByDate(date) {
if (!raw) return;
const id = raw.id || raw.agentId || entry.name;
const { output, ...rest } = raw;
- agents.push({ ...rest, id, status: raw.status || 'completed' });
+ const agent = { ...rest, id, status: raw.status || 'completed' };
+ const repaired = await repairCodexTaskSummary(join(dateDir, entry.name), agent);
+ if (repaired) agent.metadata = { ...agent.metadata, taskSummary: repaired };
+ agents.push(agent);
});
await Promise.allSettled(reads);
}
@@ -437,6 +441,8 @@ export async function getAgent(agentId) {
if (agent.status === 'completed') {
const dateStr = agent.completedAt?.slice(0, 10);
const agentDir = dateStr ? getAgentDir(agentId, dateStr) : getAgentDir(agentId);
+ const repaired = await repairCodexTaskSummary(agentDir, agent);
+ if (repaired) agent = { ...agent, metadata: { ...agent.metadata, taskSummary: repaired } };
const outputFile = join(agentDir, 'output.txt');
if (existsSync(outputFile)) {
const fullOutput = await readFile(outputFile, 'utf-8');