Skip to content

[Bug]: Opening an old search result in a long agent turn can place tool calls above the user's question (follow-up to #29700 / #29711) #41224

Closed as not planned

Description

@gpu004

What I observed

Follow-up to #29700 / PR #29711. PR #29711 fixed history ordering when the full page overlaps already-loaded rows, and explicitly noted: "When the page shares no rows with the projection, the result is the same as before (page first)."

That leftover no-overlap case happens in real threads whenever a user jumps to an old search result, citation, or mobile "load through" target (loadEarlier(throughEntryId) at packages/client-runtime/src/state/threads.ts) and the newest rows before the current cursor that fit within the server's 1 MiB / 150-row full-page cap are all work rows (e.g., command_execution, reasoning, file_change, dynamic_tool).

Because loadEarlier(throughEntryId) fetches view=conversation first and then the full page from the same cursor, the full page shares no row with the conversation rows added by pass 1. interleavePage (packages/client-runtime/src/state/threadHistoryMerge.ts:56-84) has no anchor and places the entire full page before everything else — displaying that turn's tool calls above the user's question and answer. Subsequent "Load earlier" clicks do not repair the order; only reloading the thread from a fresh snapshot does.

Reproduction

  • Commit: main @ 9d3054a7bb
  • Environment: macOS 27.2.1, Node v26.13.1

Minimal plain repro (packages/client-runtime):

import { mergeOlderHistoryIntoProjection } from "./src/state/threadHistoryMerge.ts";

// Minimal shrunk timeline: [conv0, work1, conv2], loaded window: [conv2]
// Real server pages produced by ProjectionStore.getThreadHistoryPage (verified against SQLite):
//   conversationPage = [user:0, answer:0]
//   fullPage         = [tool:77 .. tool:199] (123 command_execution rows totaling ~1 MiB)
const row = (id: string, ordinal: number, type: string) => ({
  position: ordinal,
  visibility: "local" as const,
  sourceThreadId: threadId,
  sourceItemId: id,
  item: { id, type, threadId, runId: null, ordinal, status: "completed", updatedAt: now },
});

const windowRows = [row("user:1", 202, "user_message"), row("answer:1", 203, "assistant_message")];
let p = { ...baseProjection, turnItems: windowRows.map((r) => r.item), visibleTurnItems: windowRows };
const conversationPage = [row("user:0", 0, "user_message"), row("answer:0", 1, "assistant_message")];
const fullPage = Array.from({ length: 123 }, (_, i) => row(`tool:${77 + i}`, 79 + i, "command_execution"));

p = mergeOlderHistoryIntoProjection(p, conversationPage);
p = mergeOlderHistoryIntoProjection(p, fullPage);
console.log(p.visibleTurnItems.map((r) => r.sourceItemId));

Real server preconditions (reproduced against SQLite ProjectionStore.getThreadHistoryPage):
A thread with user:0, answer:0, 200 completed command_execution rows (~8 KB input each, e.g., heredoc writes), then user:1, answer:1. Requesting history with cursor at user:1 and throughEntryId = "answer:0" returns:

  • view=conversation: [user:0, answer:0] (2 rows)
  • full page (view=undefined): [tool:77 .. tool:199] (123 command_execution rows, nextCursor set — trimmed by real byte budget to 1 MiB / maxItems: 150 with userTurns === 0).

Expected behavior

Rows preserve canonical timeline order: user:0, answer:0, tool:77..tool:199, user:1, answer:1.

Actual behavior

interleavePage hits merged.push(...pending) before emitCurrentThrough(current.length), producing tool:77..tool:199, user:0, answer:0, user:1, answer:1. Web (session-logic.ts), desktop, and mobile all render visibleTurnItems in array order, so the tool calls appear above the question and answer the user jumped to.

A subsequent plain "Load earlier" (fetching [user:0, answer:0, tool:0..76]) leaves the timeline as tool:77..199, user:0, answer:0, tool:0..76, user:1, answer:1 (tool:77 at index 0, user:0 at 123, tool:0 at 125).

Evidence

Plain repro test output:

[ 'tool:77', 'tool:78' ] ... [ 'user:0', 'answer:0', 'user:1', 'answer:1' ]
AssertionError: expected [ 'tool:77', 'tool:78' ] to deeply equal [ 'user:0', 'answer:0' ]

Real SQLite ProjectionStore.getThreadHistoryPage output for the thread described above:

conversation {"count":2,"first":"user:0","last":"answer:0","types":["user_message","assistant_message"]}
full         {"count":123,"first":"tool:77","last":"tool:199","types":["command_execution"],"nextCursor":true}

Additional investigation

  • How found: Property-based testing of mergeOlderHistoryIntoProjection (shrunk to a 3-row timeline [conv0, work1, conv2]), then verified end-to-end against real SQLite ProjectionStore.getThreadHistoryPage.
  • Verified: Confirmed interleavePage at packages/client-runtime/src/state/threadHistoryMerge.ts:56-84 is unchanged on origin/main (9d3054a7bb).
  • Duplicate check: Follow-up to closed [Bug]: Loading an old search result can reorder conversation messages #29700 / merged PR fix(client-runtime): opening an old search result keeps messages in order #29711. Searched open issues/PRs for interleavePage, throughEntryId, mergeOlderHistoryIntoProjection, and 29711 — no open follow-up issue exists.
  • Impact: Minor/moderate — wrong visible message/tool order across web, desktop, and mobile when jumping to old search results in long agent turns; no data loss (reloading the thread fixes it).

Activity

  1. juliusmarminge commented on Nov 23, 2026

    @juliusmarminge
    Member

    I tried to reproduce this on the current build and could not.

    Commit origin/main d775f66. Not driven through the UI, because that needs a thread with about 200 tool calls in one turn. Both halves were run on the real code instead. (1) Server: called apps/server selectHistoryPageFromCursor on a synthetic timeline user:0, answer:0, tool:0..199 (command_execution), user:1, answer:1, with the cursor at user:1 and throughEntryId=answer:0. Conversation-only items gave {count:2, first:user:0, last:answer:0}. The full page gave {count:150, first:tool:50, last:tool:199, nextCursor:true}: only work rows, capped by the jump policy's maxItems (75*2), before any byte budget applies. (2) Client: added a test to packages/client-runtime/src/state/threadHistoryMerge.test.ts that merges conversation page [item-0,item-1] and then a full page [item-2,item-3] sharing no rows with it into window [item-4,item-5]. vp test run src/state/threadHistoryMerge.test.ts fails on main with [item-2, item-3, item-0, item-1, item-4, item-5] instead of item-0..item-5 in order. This matches the reporter's analysis and the order the clients render, since they show visibleTurnItems in array order.

    Closing as not planned. If it still happens for you, reply with the exact steps and the version you are on and it will be looked at again.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions