Skip to content
Source
Sign inSign up
pingdotgg/t3code

[Bug]: A turn that continues an earlier sub-agent shows its "Worked for …" row above the previous answer #15891

Closedrithwikshetty(GitHub profile, opens in a new tab) opened this issue 4 comments

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

  1. With the Codex provider, send a prompt that makes the agent start a sub-agent. Let the turn finish.
  2. 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.
  3. 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.projectionRunId in CodexAdapterV2.ts), so its runId becomes the second run. The subagent_continue fixture asserts this for the sub-agent record (subagent.runId === projection.runs[1].id).
  • Its position does not move. TurnItemPositionStore.allocate inserts with ON CONFLICT(thread_id, turn_item_id) DO NOTHING, so the item keeps the ordinal it got in the first run.
  • deriveTurnFolds in apps/web/src/components/chat/MessagesTimeline.logic.ts groups entries by runId and 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 consumed pendingBoundary, 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).

  1. rithwikshetty(GitHub profile, opens in a new tab)commented

    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.

    Image
  2. Note

    Grok responding on behalf of Julius.

    Triage

    Thanks for the careful write-up and the code trace, @rithwikshetty!

    What I found

    Likely fix area

    • deriveTurnFolds on web and deriveThreadFeedRunFolds on mobile. The adapters move the runId on 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.

  3. juliusmarminge added labelsbug

  4. juliusmarminge added labelsvia-triage

  5. rithwikshetty(GitHub profile, opens in a new tab)commented

    A suggested fix, in case it saves time. This is what I applied in the fork, adapted by hand to deriveTurnFolds on main @ 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.ts asserts "The resume re-attributes Agent A to the run that sent the SendMessage", and subagent_continue/codex_output.ts asserts the same for Codex. So any provider that continues an earlier sub-agent can produce an entry whose runId is 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 MessagesTimeline logic and render tests, and a run over the stored turn items of the affected thread. The diff above is the same change re-fitted to main; I have not run it against main.

    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.
  6. 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  checkpoint

    The 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.

Sign in to join this conversation.