[Bug]: A turn that continues an earlier sub-agent shows its "Worked for …" row above the previous answer #15891
Before submitting#
- I searched existing issues and did not find a duplicate.
- I included enough detail to reproduce or investigate the problem.
Area#
apps/web
Steps to reproduce#
- With the Codex provider, send a prompt that makes the agent start a sub-agent. Let the turn finish.
- Send a second prompt that makes the agent continue that same sub-agent (a follow-up to the existing agent) instead of starting a new one. Asking for more agents than it starts fresh is one way to get this: it reuses the idle one.
- Let the second turn finish and look at the settled thread. Reload if needed.
Expected behavior#
Each "Worked for …" row sits between its own prompt and answer:
prompt 1 → "Worked for …" → answer 1 → prompt 2 → "Worked for …" → answer 2
Actual behavior#
The second turn's "Worked for …" row sits under the first prompt, above the first answer, and the second prompt has no fold of its own:
prompt 1 → "Worked for …" (turn 1) → "Worked for …" (turn 2) → answer 1 → prompt 2 → answer 2
Cause, from reading the code:
- When a sub-agent is continued, its turn item is updated in place. The Codex adapter emits it with the parent's current run (
runId: input.subagent.parentContext.projectionRunIdinCodexAdapterV2.ts), so itsrunIdbecomes the second run. Thesubagent_continuefixture asserts this for the sub-agent record (subagent.runId === projection.runs[1].id). - Its position does not move.
TurnItemPositionStore.allocateinserts withON CONFLICT(thread_id, turn_item_id) DO NOTHING, so the item keeps the ordinal it got in the first run. deriveTurnFoldsinapps/web/src/components/chat/MessagesTimeline.logic.tsgroups entries byrunIdand creates a run's group at the first entry it meets with that id. That first entry is now the continued sub-agent, sitting inside turn 1. The first run's group already consumedpendingBoundary, so the second run's fold anchors at that entry (pendingBoundary?.anchorEntryId ?? entry.id).
Stored shape from the thread where I saw it (two runs, ordinals are runOrdinal * 1_000_000 + n):
ordinal run type
1000001 run 1 user_message
1000002 run 1 assistant_message
1000003 run 1 command_execution
1000004 run 2 subagent <- started in run 1, continued in run 2
1000005 run 1 web_search
...
1000015 run 1 assistant_message (answer 1)
2000001 run 2 user_message
2000003 run 2 subagent
2000004 run 2 subagent
...
2000017 run 2 assistant_message (answer 2)
How this was established: I hit it in a downstream fork that shares this code path, traced it to the data above, and then compared against main, where TurnItemPositionStore.ts is identical and deriveTurnFolds groups and anchors the same way. I have not reproduced it on a stock T3 Code build, so there is no T3 screenshot attached.
A unit-level repro for deriveMessagesTimelineRows: timeline entries user(run-1), work(run-1), subagent event (run-2), assistant(run-1), user(run-2), work(run-2), assistant(run-2), with latestRun = run-2 completed. Expected row ids: prompt-1, turn-fold:run-1, answer-1, prompt-2, turn-fold:run-2, answer-2. In the fork, before the fix, turn-fold:run-2 came out directly after turn-fold:run-1.
Impact#
Minor bug or occasional failure
Version or commit#
main @ a1d9d72aef (code read at this commit)
Environment#
macOS, Codex provider
Logs or stack traces#
No response
Workaround#
None for the user. In the fork I fixed it in deriveTurnFolds: an entry positioned before its own run's prompt folds with the turn it sits in (keyed by the run of the prompt that precedes it). That fixes placement only. The continued sub-agent still shows under the turn that started it, so the later turn lists one sub-agent fewer than it used.
Related: #15279 is a different problem in the same function (steers and fold placement).
Investigated and drafted with Claude Code (Claude Opus 5.5).
Note
Grok responding on behalf of Julius.
Triage#
Thanks for the careful write-up and the code trace, @rithwikshetty!
What I found#
- This is separate from #15279. That issue is about steers staying inside one run's fold, while this one is a later run's fold anchoring on an item that still sits inside the earlier turn. I didn't find another open issue for it, and I didn't replay a Codex session. The stored shape you describe matches what this code writes.
- Continuing a Codex sub-agent updates the same turn item in place and reassigns it to the parent run that resumed it. The
subagent_continuefixture expectssubagent.runIdto be the second run. https://github.com/pingdotgg/t3code/blob/a1d9d72aefc2a388ea3915956e9d84d279c87da7/apps/server/src/orchestration-v2/Adapters/CodexAdapterV2.ts#L2210-L2241 - The ordinal doesn't move. The update keeps the original
turnItemOrdinal, andTurnItemPositionStore.allocateinserts withON CONFLICT(thread_id, turn_item_id) DO NOTHING, so the card stays among turn 1's items while itsrunIdis run 2. https://github.com/pingdotgg/t3code/blob/a1d9d72aefc2a388ea3915956e9d84d279c87da7/apps/server/src/orchestration-v2/TurnItemPositionStore.ts#L61-L71 deriveTurnFoldsgroups byrunIdand anchors on the first entry it sees for that id. Run 1 already consumed the prompt boundary, so run 2's fold anchors on the continued sub-agent inside turn 1. The work after the second prompt is hidden in that fold, while the second answer stays visible because the terminal assistant message isn't folded. https://github.com/pingdotgg/t3code/blob/a1d9d72aefc2a388ea3915956e9d84d279c87da7/apps/web/src/components/chat/MessagesTimeline.logic.ts#L761-L774- Claude does the same reassignment on resume and also keeps the original ordinal, so a resumed Claude sub-agent can likely misplace the fold the same way. Cursor and ACP leave the sub-agent on the run that started it.
- Mobile groups by
runIdthe same way inderiveThreadFeedRunFolds.
Likely fix area#
deriveTurnFoldson web andderiveThreadFeedRunFoldson mobile. The adapters move therunIdon purpose, and the ordinal is kept so the card stays where it was created.- One option is your fork's approach of folding an entry with the run of the prompt before it. That puts the "Worked for …" row back between the prompt and its answer, but leaves the continued sub-agent card in turn 1.
- Another option is to also move the continued card into the later turn. That's a separate choice from fixing the fold.
A maintainer will decide on the fix direction.
juliusmarminge added labelsbug
juliusmarminge added labelsvia-triage
A suggested fix, in case it saves time. This is what I applied in the fork, adapted by hand to
deriveTurnFoldsonmain @ a1d9d72aef. Not opening a PR as you are not looking for other contributors, but will be happy to help when needed.Why it is not Codex-specific. The rule below looks only at the shape of the timeline, never at the provider. It also looks like the re-attribution is intended engine behavior rather than a Codex quirk:
claude_background_subagent_lifecycle/output.tsasserts "The resume re-attributes Agent A to the run that sent the SendMessage", andsubagent_continue/codex_output.tsasserts the same for Codex. So any provider that continues an earlier sub-agent can produce an entry whoserunIdis newer than its position.Rule. An entry that sits before its own run's prompt folds with the turn it sits in, keyed by the run of the prompt that precedes it.
+function responseBoundaryRunId(entry: TimelineEntry): RunId | null { + if (entry.kind === "message") return entry.message.runId ?? null; + if (entry.kind === "work") return entry.entry.runId ?? null; + return null; +} // inside deriveTurnFolds let runlessKey: RunId | null = null; let pendingBoundary: { createdAt: string; anchorEntryId: string } | null = null; + // A sub-agent continued by a later turn keeps its place in the turn that + // started it but takes the later run's id. It folds with the turn it sits + // in, or the later turn's fold would open above its own prompt. + const promptIndexByRunId = new Map<RunId, number>(); + for (const [index, entry] of input.timelineEntries.entries()) { + if (!timelineEntryStartsResponse(entry)) continue; + const runId = responseBoundaryRunId(entry); + if (runId !== null && !promptIndexByRunId.has(runId)) promptIndexByRunId.set(runId, index); + } + let promptRunId: RunId | null = null; for (const [index, entry] of input.timelineEntries.entries()) { if (timelineEntryStartsResponse(entry)) { const nextEntry = input.timelineEntries[index + 1]; pendingBoundary = nextEntry ? { createdAt: entry.createdAt, anchorEntryId: nextEntry.id } : null; - const boundaryRunId = - entry.kind === "message" - ? entry.message.runId - : entry.kind === "work" - ? entry.entry.runId - : null; + const boundaryRunId = responseBoundaryRunId(entry); runlessKey = boundaryRunId == null ? RunId.make(`runless:${entry.id}`) : null; + promptRunId = boundaryRunId; continue; } - const runId = timelineEntryFoldRunId(entry, runlessKey); - if (!runId) { + const ownRunId = timelineEntryFoldRunId(entry, runlessKey); + if (!ownRunId) { continue; } + const sitsBeforeOwnPrompt = (promptIndexByRunId.get(ownRunId) ?? -1) > index; + const runId = sitsBeforeOwnPrompt && promptRunId !== null ? promptRunId : ownRunId;Two things this also covers on
main:- If the continued sub-agent is the first entry after prompt 1, the second run's group currently takes prompt 1's
pendingBoundary, so the first run loses its start boundary and anchor as well. - While the second run is still working it is unfolded, so the continued sub-agent's row drops out of the first turn's fold and sits loose above answer 1. With the rule it stays in the first turn's fold.
How it was checked. In the fork: a unit test with the entries listed in the report (fails before, passes after), the rest of the
MessagesTimelinelogic and render tests, and a run over the stored turn items of the affected thread. The diff above is the same change re-fitted tomain; I have not run it againstmain.Limits.
- It fixes placement only. The continued sub-agent still shows under the turn that started it, so the later turn lists one sub-agent fewer than it used. Fixing that needs the server to give a continued sub-agent a place in the new run (move the item, or add one), which would also make this client rule unnecessary.
- It assumes a run's prompt or wake notification comes before that run's work. The one shape I can think of that breaks this: a notification carrying the running run's id arrives mid-run, in a run whose initiating prompt belongs to a different run. Earlier work of that run would then fold with the prompt's run. I have not seen that shape, and prompts without a run id are left alone, but it is worth a look given the wake and steer cases.
- If the continued sub-agent is the first entry after prompt 1, the second run's group currently takes prompt 1's
This also happens on stock T3 Code nightly 0.0.46-nightly.20261009 (3b6af0bd) on Windows, with the Claude provider (Opus 5.5).
Steps: a turn starts a background subagent with the Agent tool. Several turns later, a new turn resumes that subagent with SendMessage.
Stored shape from the affected thread:
ordinal run type 57000008 run 72 subagent <- started in run 57, resumed in run 72 ... 72000001 run 72 user_message 72000002 run 72 reasoning 72000003 run 72 assistant_message (the actual answer, 852 chars) 72000004 run 72 dynamic_tool (SendMessage to the subagent) 72000005 run 72 assistant_message (short closing line, 213 chars) 72000006 run 72 checkpointThe settled turn showed only the closing line, with no "Worked for" row under its prompt. Because of #11353, the actual answer was folded into the misplaced row, so it could not be seen at all.
t3dotgg(GitHub profile, opens in a new tab) closed this
Sign in to join this conversation.
Update: reproduced on a stock T3 Code build, so the "not reproduced on T3" note in the report no longer applies.
Build: current nightly desktop app on macOS, 2026-10-05.