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: CoS agent taskSummary on Codex output extracts the assistant tail

[._.]/ Adam Eivy73c59162fa0e
diff --git a/.changelog/NEXT.md b/.changelog/NEXT.md
index 05e62325da..aa9803a9ee 100644
--- a/.changelog/NEXT.md
+++ b/.changelog/NEXT.md
@@ -356,6 +356,21 @@
show the full prompt on hover (the visible cell is still truncated to
80 chars).
+- **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
+ `apply_patch` payload Codex streamed during the run. `extractFinalSummary`'s
+ tool-marker heuristic (`🔧`, `→`, `↳`) does not match any of Codex's section
+ markers (`exec` / `apply patch` / `codex` / `tokens used`), so the
+ backwards walk swept through the whole transcript. The simplify-summary
+ splitter also false-positived on diff lines that quoted source code
+ containing `/simplify` and a `run` verb. Adds an `extractCodexAssistantTail`
+ helper next to `extractCodexAssistant` that carves out just the message
+ following the last `tokens used\n<count>` (or inline `tokens used: <n>`)
+ footer; both `extractFinalSummary` and `extractSimplifySummaries` short-
+ circuit on Codex output so the agent card shows the real summary instead of
+ a transcript dump.
+
- **Codex image gen — "Codex returned no session id" false negative.** With
long pipeline prompts (multi-KB comic-script payloads), codex emits the
banner + echoed prompt in a single stderr chunk that exceeds the
diff --git a/server/lib/codexAssistantExtract.js b/server/lib/codexAssistantExtract.js
index d034e400d3..8a66c350df 100644
--- a/server/lib/codexAssistantExtract.js
+++ b/server/lib/codexAssistantExtract.js
@@ -15,3 +15,28 @@ export function extractCodexAssistant(text) {
return response.trim();
}
+
+const RE_CODEX_MARKERS = /(^|\n)(tokens used|apply patch|patch: completed)\b/;
+const RE_TOKENS_USED_LINE = /^tokens used\b/;
+const RE_NUMERIC_LINE = /^[\d,.\s]+$/;
+
+// Newer Codex CLI streams the FINAL assistant message AFTER a `tokens used\n<count>`
+// footer (rather than before, as `extractCodexAssistant` above assumes). The
+// transcript also intersperses `exec` / `apply patch` / `codex` sections with raw
+// diff and grep output that has no tool-marker prefix, so the generic backwards
+// walk in `extractFinalSummary` cannot find the boundary on its own. Returns null
+// for non-Codex input so callers fall through to the generic logic.
+export function extractCodexAssistantTail(outputBuffer) {
+ if (typeof outputBuffer !== 'string' || !outputBuffer) return null;
+ if (!RE_CODEX_MARKERS.test(outputBuffer)) return null;
+
+ const lines = outputBuffer.split('\n');
+ for (let i = lines.length - 1; i >= 0; i--) {
+ if (!RE_TOKENS_USED_LINE.test(lines[i].trim())) continue;
+ let j = i + 1;
+ while (j < lines.length && RE_NUMERIC_LINE.test(lines[j].trim())) j++;
+ const tail = lines.slice(j).join('\n').trim();
+ if (tail) return tail;
+ }
+ return null;
+}
diff --git a/server/lib/codexAssistantExtract.test.js b/server/lib/codexAssistantExtract.test.js
index d601caab48..f823ff8c81 100644
--- a/server/lib/codexAssistantExtract.test.js
+++ b/server/lib/codexAssistantExtract.test.js
@@ -1,5 +1,5 @@
import { describe, it, expect } from 'vitest';
-import { extractCodexAssistant } from './codexAssistantExtract.js';
+import { extractCodexAssistant, extractCodexAssistantTail } from './codexAssistantExtract.js';
describe('extractCodexAssistant', () => {
it('returns text unchanged when there is no Codex banner', () => {
@@ -68,3 +68,45 @@ prompt body`;
expect(extractCodexAssistant(undefined)).toBe(undefined);
});
});
+
+describe('extractCodexAssistantTail', () => {
+ it('returns the message that follows the last "tokens used" footer', () => {
+ const output = [
+ 'exec',
+ 'apply patch',
+ 'diff --git a/foo b/foo',
+ '+something',
+ 'tokens used',
+ '285,345',
+ 'Final assistant summary.',
+ '- bullet one',
+ ].join('\n');
+ expect(extractCodexAssistantTail(output)).toBe('Final assistant summary.\n- bullet one');
+ });
+
+ it('handles inline "tokens used: <n>" with the summary on the next line', () => {
+ const output = [
+ 'apply patch',
+ '+added',
+ 'tokens used: 12345',
+ 'Inline-format reply.',
+ ].join('\n');
+ expect(extractCodexAssistantTail(output)).toBe('Inline-format reply.');
+ });
+
+ it('returns null when the output is not Codex (no markers)', () => {
+ const output = 'Some Claude-style narration.\n🔧 Using Read...\nFinal note.';
+ expect(extractCodexAssistantTail(output)).toBeNull();
+ });
+
+ it('returns null for empty or non-string input', () => {
+ expect(extractCodexAssistantTail('')).toBeNull();
+ expect(extractCodexAssistantTail(null)).toBeNull();
+ expect(extractCodexAssistantTail(undefined)).toBeNull();
+ });
+
+ it('returns null when Codex markers exist but no tail follows the last "tokens used"', () => {
+ const output = 'apply patch\npatch: completed\ndiff --git a/x b/x\ntokens used\n123\n';
+ expect(extractCodexAssistantTail(output)).toBeNull();
+ });
+});
diff --git a/server/services/agentLifecycle.js b/server/services/agentLifecycle.js
index 075f55d915..2b00baf694 100644
--- a/server/services/agentLifecycle.js
+++ b/server/services/agentLifecycle.js
@@ -30,6 +30,7 @@ import { analyzeAgentFailure, resolveFailedTaskUpdate } from './agentErrorAnalys
import { createAgentRun, completeAgentRun, checkForTaskCommit } from './agentRunTracking.js';
import { buildAgentPrompt, getAppWorkspace, getAppDataForTask, createJiraTicketForTask } from './agentPromptBuilder.js';
import { buildCliSpawnConfig, isClaudeCliProvider, getClaudeSettingsEnv, spawnDirectly } from './agentCliSpawning.js';
+import { extractCodexAssistantTail } from '../lib/codexAssistantExtract.js';
import { selectModelForTask } from './agentModelSelection.js';
import { processAgentCompletion } from './agentCompletion.js';
import { activeAgents, runnerAgents, spawningTasks, useRunner, isTruthyMeta, isFalsyMeta } from './agentState.js';
@@ -641,6 +642,9 @@ export async function spawnViaRunner(agentId, task, opts) {
export function extractFinalSummary(outputBuffer) {
if (!outputBuffer) return null;
+ const codexTail = extractCodexAssistantTail(outputBuffer);
+ if (codexTail) return codexTail;
+
const lines = outputBuffer.split('\n');
const contentLines = [];
let foundContent = false;
@@ -667,6 +671,12 @@ const RE_SIMPLIFY_ACTION = /\b(run|running|launch|now)\b/i;
export function extractSimplifySummaries(outputBuffer) {
if (!outputBuffer) return null;
+ // Codex CLI cannot execute slash commands like /simplify, so any match
+ // inside its output is from a diff/grep dump that quotes source code.
+ // Treat the assistant tail as the task summary and skip the simplify split.
+ const codexTail = extractCodexAssistantTail(outputBuffer);
+ if (codexTail) return { taskSummary: codexTail, simplifySummary: null };
+
const lines = outputBuffer.split('\n');
let simplifyIdx = -1;
for (let i = 0; i < lines.length; i++) {
diff --git a/server/services/agentSummaryExtraction.test.js b/server/services/agentSummaryExtraction.test.js
index 184c795d47..bb2f977798 100644
--- a/server/services/agentSummaryExtraction.test.js
+++ b/server/services/agentSummaryExtraction.test.js
@@ -20,6 +20,47 @@ describe('extractFinalSummary', () => {
expect(extractFinalSummary('')).toBeNull();
expect(extractFinalSummary(null)).toBeNull();
});
+
+ it('extracts only the assistant tail from Codex output, ignoring earlier diff/exec dumps', () => {
+ const output = [
+ 'server/services/imageGen/codex.test.js:300: // diff',
+ 'server/services/imageGen/codex.test.js:307: // more diff',
+ 'exec',
+ 'codex',
+ 'I am about to make changes.',
+ 'apply patch',
+ 'patch: completed',
+ 'diff --git a/foo.js b/foo.js',
+ '@@ -1 +1 @@',
+ '-old',
+ '+new',
+ 'tokens used',
+ '285,345',
+ 'Implemented the TUI provider path.',
+ '- New `type: "tui"` provider support.',
+ '- New PTY-backed runner.',
+ ].join('\n');
+
+ expect(extractFinalSummary(output)).toBe(
+ 'Implemented the TUI provider path.\n- New `type: "tui"` provider support.\n- New PTY-backed runner.'
+ );
+ });
+
+ it('handles legacy Codex inline "tokens used: <n>" format', () => {
+ const output = [
+ 'codex',
+ 'doing stuff',
+ 'apply patch',
+ '+added',
+ 'tokens used: 12345',
+ 'Final assistant reply line 1.',
+ 'Final assistant reply line 2.',
+ ].join('\n');
+
+ expect(extractFinalSummary(output)).toBe(
+ 'Final assistant reply line 1.\nFinal assistant reply line 2.'
+ );
+ });
});
describe('extractSimplifySummaries', () => {
@@ -72,6 +113,32 @@ describe('extractSimplifySummaries', () => {
expect(result.simplifySummary).toBe('Code is clean.');
});
+ it('treats Codex output as a single task summary (no /simplify split, even if the diff quotes /simplify)', () => {
+ // Mirrors the agent-5f6951e3 regression: Codex dumps source code that
+ // contains both "/simplify" and a "run" verb on the same line. The old
+ // logic split there and serialized 9000+ lines of grep output as the
+ // task summary.
+ const output = [
+ 'server/services/cos.js:1596:9. Run `/simplify` to review changed code.',
+ 'client/src/components/cos/tabs/AgentCard.jsx:697: title="Will run /simplify before committing">',
+ 'exec',
+ 'apply patch',
+ 'diff --git a/foo b/foo',
+ '+something',
+ 'tokens used',
+ '12000',
+ 'Implemented the requested change.',
+ '- Did X.',
+ '- Did Y.',
+ ].join('\n');
+
+ const result = extractSimplifySummaries(output);
+ expect(result).toEqual({
+ taskSummary: 'Implemented the requested change.\n- Did X.\n- Did Y.',
+ simplifySummary: null
+ });
+ });
+
it('matches various /simplify narration patterns', () => {
const patterns = [
'Now let me run `/simplify` as instructed.',