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: drop Codex commentary messages from text-turn answers so review verdicts parse (#9969)

Adam Eivy177d0fc45e3e
diff --git a/server/lib/codexTurn.js b/server/lib/codexTurn.js
index 136bdf561e..b48902a630 100644
--- a/server/lib/codexTurn.js
+++ b/server/lib/codexTurn.js
@@ -323,6 +323,9 @@ export const classifyCodexTransportError = (err) => {
}
};
+// `MessagePhase` in the app-server protocol: interim narration vs the answer.
+const CODEX_COMMENTARY_PHASE = 'commentary';
+
/** A fresh accumulator for one turn. */
export const createTurnAccumulator = ({ threadId = null, turnId = null } = {}) => ({
threadId,
@@ -380,8 +383,17 @@ export const applyCodexTurnEvent = (acc, method, params) => {
// that previewed it are dropped — appending both would double the prose —
// while deltas that arrive AFTER it belong to the next message and are
// still carried by `finalizeCodexTurn`.
+ //
+ // Newer models narrate before answering: an interim `commentary` message
+ // ("I'll review the diff…") precedes the `final_answer` one. Folding that
+ // narration into the answer turns a JSON envelope into prose-then-JSON
+ // that no parser accepts (#9969), so a commentary item is dropped along
+ // with the deltas that previewed it. `phase: null` is "unknown" — legacy
+ // models never set it — and keeps the concatenating behavior.
if (params.item?.type === 'agentMessage' && typeof params.item.text === 'string') {
- acc.finalText = acc.finalText === null ? params.item.text : `${acc.finalText}${params.item.text}`;
+ if (params.item.phase !== CODEX_COMMENTARY_PHASE) {
+ acc.finalText = acc.finalText === null ? params.item.text : `${acc.finalText}${params.item.text}`;
+ }
acc.deltas.length = 0;
}
return false;
diff --git a/server/lib/codexTurn.test.js b/server/lib/codexTurn.test.js
index e4f56f2c44..583db4b51e 100644
--- a/server/lib/codexTurn.test.js
+++ b/server/lib/codexTurn.test.js
@@ -279,6 +279,32 @@ describe('turn projection', () => {
expect(finalizeCodexTurn(acc).text).toBe('message one. message two.');
});
+ it('drops commentary messages and keeps final answers and phase-less messages (#9969)', () => {
+ const acc = createTurnAccumulator({ threadId: 't', turnId: 'turn-1' });
+ const message = (text, phase) => applyCodexTurnEvent(acc, CODEX_TURN_NOTIFICATIONS.itemCompleted, {
+ threadId: 't', turnId: 'turn-1', item: { type: 'agentMessage', text, ...(phase === undefined ? {} : { phase }) },
+ });
+ delta(acc, 'narration preview');
+ message('narration. ', 'commentary');
+ message('legacy one. ', null);
+ message('legacy two. ');
+ message('answer.', 'final_answer');
+ complete(acc, { id: 'turn-1', status: 'completed' });
+
+ expect(finalizeCodexTurn(acc).text).toBe('legacy one. legacy two. answer.');
+ });
+
+ it('reports an empty completion when a turn emitted only commentary', () => {
+ const acc = createTurnAccumulator({ threadId: 't', turnId: 'turn-1' });
+ delta(acc, 'thinking out loud');
+ applyCodexTurnEvent(acc, CODEX_TURN_NOTIFICATIONS.itemCompleted, {
+ threadId: 't', turnId: 'turn-1', item: { type: 'agentMessage', text: 'thinking out loud', phase: 'commentary' },
+ });
+ complete(acc, { id: 'turn-1', status: 'completed' });
+
+ expect(finalizeCodexTurn(acc)).toMatchObject({ error: 'Codex returned an empty completion' });
+ });
+
it('accepts an anonymous completion on its own thread', () => {
// The frame already reached this accumulator BY THREAD, and PortOS's threads
// are ephemeral with exactly one turn — so rejecting it over an id the
diff --git a/server/services/codexAppServer.turn.test.js b/server/services/codexAppServer.turn.test.js
index cdbe7a017d..3855049a92 100644
--- a/server/services/codexAppServer.turn.test.js
+++ b/server/services/codexAppServer.turn.test.js
@@ -69,13 +69,13 @@ const awaitRequest = async (child, method, answer) => {
};
/** Drive a text turn to a `turn/completed` with `status`, streaming `deltas`. */
-const driveTurn = async (child, { deltas = [], status = 'completed', item = null, error = null, usage = null } = {}) => {
+const driveTurn = async (child, { deltas = [], status = 'completed', item = null, items = [], error = null, usage = null } = {}) => {
await awaitRequest(child, 'thread/start', { thread: { id: 'thread-1' }, model: 'model-alpha' });
await awaitRequest(child, 'turn/start', { turn: { id: 'turn-1', items: [], status: 'inProgress' } });
for (const delta of deltas) {
child.notify('item/agentMessage/delta', { threadId: 'thread-1', turnId: 'turn-1', itemId: 'i1', delta });
}
- if (item) child.notify('item/completed', { threadId: 'thread-1', turnId: 'turn-1', completedAtMs: 1, item });
+ for (const done of item ? [item] : items) child.notify('item/completed', { threadId: 'thread-1', turnId: 'turn-1', completedAtMs: 1, item: done });
if (usage) child.notify('thread/tokenUsage/updated', { threadId: 'thread-1', turnId: 'turn-1', tokenUsage: usage });
child.notify('turn/completed', { threadId: 'thread-1', turn: { id: 'turn-1', items: [], status, error } });
};
@@ -148,6 +148,24 @@ describe('running a text turn', () => {
expect(child.lastRequest('turn/start').params.outputSchema).toEqual(schema);
});
+ it('returns only the final answer when the model narrates first (#9969)', async () => {
+ // gpt-6-class models emit an interim `commentary` message before the
+ // `final_answer`. Concatenating both made the code-review envelope
+ // prose-then-JSON, which normalizeCodeReviewVerdict rejects as invalid_json.
+ const promise = runCodexTextTurn({ prompt: 'review' });
+ await handshake();
+ await driveTurn(child, {
+ deltas: ["I'll check the diff first."],
+ items: [
+ { type: 'agentMessage', id: 'i1', phase: 'commentary', text: "I'll check the diff first." },
+ { type: 'agentMessage', id: 'i2', phase: 'final_answer', text: '{"verdict":"clean","findings":[]}' },
+ ],
+ });
+
+ const { text } = await promise;
+ expect(JSON.parse(text)).toEqual({ verdict: 'clean', findings: [] });
+ });
+
it('rejects rather than returning the partial text of an interrupted turn', async () => {
const promise = runCodexTextTurn({ prompt: 'long one' });
await handshake();