Skip to content

fix(app): order messages by their place in the transcript, not by their IDs - #66168

Open
Hona wants to merge 8 commits into
devfrom
order-by-sequence
Open

Hona wants to merge 8 commits into
devfrom
order-by-sequence

Conversation

@Hona

@Hona Hona commented Nov 24, 2026 •

Copy link
Copy Markdown
Member

Problem

  • A revert could leave the timeline blank although the server kept the messages before it. Staging the revert hid earlier messages; committing it removed every loaded message, until a reload.
  • The clients compared message IDs as text to decide what a revert leaves. The server orders a transcript by sequence and deletes by sequence (core/src/session/projector.ts).
  • IDs do not sort in transcript order:
    • A prompt gets its ID when it is sent and its place when it is delivered. A prompt sent while the agent works sorts before the replies that precede it.
    • A caller may choose any ID. Scheduled prompts (msg_sch_…), automations (msg_aut_…) and watch notices (msg_watch_…) sort after every other message.
    • A fork copies messages as msg_<event>_<seq>, so _10 sorts before _9.
After session.revert.committed at "Rename the helper"
dev: the earlier scheduled prompt and its reply are gone too This PR: the messages before the boundary stay

How common

42 sessions (four long ones whole, and the newest 1,000 messages of the 38 most recent): 30,067 messages, 1,376 prompts.

Prompts Sessions
A revert by ID text differs from a revert by place 1,181 of 1,376 24 of 42
It hides or removes an earlier message that the server keeps 753 23
It keeps a later message that the server removes 1,064 17
The same, in the 15 sessions whose IDs are all time-ordered 175 of 315 7
  • msg_watch_… is in 26 of the 42 sessions, msg_aut_… in 4, msg_sch_… in 1. None is a fork.
  • To hit it: Undo or "Revert to here" in such a session hides the wrong rows. Sending the next prompt commits the revert and removes them from the client.

Fix

Order comes from a message's place in the transcript, never from the text of its ID.

// @opencode/util/session-revert
export function beforeRevert(messages, boundary, older) {
  if (!boundary) return messages
  const index = messages.findIndex((message) => message.id === boundary)
  if (index !== -1) return messages.slice(0, index)
  // Not loaded. In an older page, it is older than every loaded message.
  // Otherwise the session no longer has it.
  return older ? [] : messages
}
flowchart TD
  A[session.revert.committed] --> B{Is the boundary loaded?}
  B -- yes --> C[Remove it and every message after it, by place]
  B -- no --> J{Did a read already apply the revert?}
  J -- "yes: the whole transcript is loaded, or a read held the message before the boundary and not the boundary" --> K[Change nothing]
  J -- "not known: only the server knows" --> D[Keep the rows and the staged pointer. Read the transcript again]
  D --> L[The read lands: it replaces the rows and clears the pointer]
  C --> E{Is the loaded transcript empty, with older messages left?}
  E -- yes --> G[Clear the cursor, read the newest page again]
  C --> H{Was an input waiting?}
  H -- yes --> I[Drop the inputs whose rows went, read the inbox again]
Loading
  • A staged revert whose boundary is in an older page: the loaded transcript is the end of the server's, so the boundary is older than all of it. None of the loaded messages shows, and the timeline loads older pages by itself until it has the boundary.
  • That search stops at 1,000 loaded messages (REVERT_SEARCH_LIMIT). The loaded messages then show, at the end of the session, and the composer still offers Redo, which clears the pointer. The largest turn in 42 sessions (1,418 turns) is 565 messages, so Undo of one prompt stays inside the limit.
  • A committed revert whose boundary is not loaded: only the server knows whether the loaded messages came after it. The client reads the transcript again. Until that read lands, the rows and the staged pointer stay, so the timeline is never empty in between.
  • A read can apply a revert before its event comes. A read that holds the message before the boundary and not the boundary has done so: it clears the pointer, and the late event changes nothing. A read that still holds the boundary started before the commit, and settles nothing.
  • Waiting inputs: the server deletes the inputs enqueued at or after the boundary. Only it knows when each was enqueued, so the client reads the inbox again when an input was waiting.
  • A link to a message that is not loaded: the timeline asks the server whether the session has it (session.message.get) before history pages toward it. Before, the ID decided, so a link to an older scheduled prompt was dropped.
Site It compared What went wrong
client/src/solid/data.ts, committed revert id >= boundary Removed from the first ID that sorts at or after the boundary, which can be the first loaded message. Kept later messages with a smaller ID
client/src/solid/data.ts, waiting inputs id < boundary Dropped an input the server keeps, kept one it deleted
app/src/session/timeline/controller-projection.ts id < boundary A staged revert hid earlier rows and showed later ones
app/src/session/session-domain.ts id < boundary Prompt navigation and the composer saw the wrong prompts
app/src/session/revert-machine.ts, 3 places id < boundary Undo rewound the wrong prompt, or was off though a prompt was left
app/src/session/commands/dialog-jump-to-message.tsx id < boundary The picker hid kept prompts and listed reverted ones
app/src/session/timeline/cursor-machine.ts target >= boundary, target >= oldest loaded A link to an older scheduled or automation prompt was taken as missing. A link to a message that does not exist paged the whole history when the oldest loaded ID sorted high
app/src/session/timeline/cursor.ts last > previous A prompt after a scheduled one did not count as newer
tui/src/routes/session/rows.ts id < boundary As the app's timeline
tui/src/routes/session/index.tsx Place, but a boundary in an older page counted as no boundary Counted no message as reverted
  • Not changed: cli-rs/crates/store (messages.rs, sessions.rs) has the same two comparisons as data.ts. parity/deviations/O89-revert-order.md records the difference.
  • Not changed: the todo lists (app/src/session/composer/todos.ts, tui/src/util/todo.ts) go by place, and take a boundary that is not loaded as no boundary.
  • Not order: the ID tie-breaks in session and project lists only make a sort stable.

A revert pointer that outlived its message

The server can keep the pointer of a revert that it committed (#63282). The client cannot tell that pointer from a boundary in an older page.

  • How reachable: 0 of 2,757 sessions on the owner's server hold a revert pointer, stale or staged. So this PR adds no request to look the boundary up.
  • Whole transcript loaded: nothing is reverted.
  • Older pages left: the search limit bounds it. On a 6,579-message session:
A stale pointer when the session opens Requests Read Until a row shows
Without the limit (1 run) 321 30.4 MB 141 s
With the limit of 1,000 (6 runs) 49 2.5 MB 17 to 28 s
  • The TUI has no such limit. It loads older pages only when the reader pages up, so it shows no message until then.

Related work

  • fix(app): order messages by position, not by ID string #57581 applies the same rule to the committed revert in data.ts, to session-domain.ts, controller-projection.ts and the TUI's rows. It conflicts with this PR in those files.
  • This PR also covers the revert machine, the jump picker, the cursor machine, a revert whose event comes late, and the e2e keepers.

Before and after

A real page of 40 messages whose earliest message is a scheduled prompt; the revert is at message 29, so the server keeps 28. The events were delivered to the page only; no revert ran on the session. 3 runs on each timeline:

dev This PR
Staged: the earlier prompt Gone, 6 of 6 At y = 109, 6 of 6
Committed: rows mounted 0, 6 of 6 14, 6 of 6
Reload 14 rows, prompt at y = 109 The same 14 rows as before the reload
Control: the same page with a time-ordered ID for that prompt Rows stay, 6 of 6 Rows stay, 6 of 6

The event of a committed revert comes after a read applied it, on a 6,579-message session. The read that the event starts is answered after 300 ms. 3 runs on each timeline:

dev This PR
Reader at the end The 4 rows in view go, and nothing is read again, 6 of 6 The same rows in every frame, 6 of 6
Reader 3 wheel bursts up Not run The same rows at the same place in every frame and after the read, 6 of 6

Tests

e2e/regression/timeline-stream.spec.ts, "revert", on both timelines. Each transcript has IDs that do not sort in its order:

Case What belongs On dev
A staged revert Rows msg_sch_0001, msg_0002 msg_0002, msg_0004
A staged revert at a message in an older page Rows msg_sch_0001, msg_0002 msg_0002, msg_0008, msg_0007, msg_0006
A staged revert whose boundary the session no longer has The newest reply shows after 1,000 of 1,060 messages are read No row
A committed revert Rows msg_sch_0001, msg_0002 None
A committed revert whose boundary is not loaded The loaded rows in each frame until the read lands No read, no row
  • The last case records the rows at each animation frame. A handler that empties the transcript before it reads again fails it with 6 and 8 frames without a row.
  • packages/client/test/solid-revert.test.ts: removal by place, a boundary at the oldest loaded message, a boundary that may be in an older page, a read that applied the revert first, a read that started before the commit, and waiting inputs.
  • packages/util/src/session-revert.test.ts, and cases in the tests of each app module in the table.

No flag: a fix.


Added by the opencode bot (AI-generated).

Related: #61189, #63282, #57581

@opencodejr opencodejr Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The fix works for the case it targets. I reproduced it in the TUI with the simulated model: a prompt with ID msg_sch_0001 came before the revert boundary. After the commit, dev dropped that prompt and its reply even though the server kept them. With this PR the TUI matches the server (screenshots below). Unit tests for util, client, app (src/session) and the TUI pass. App and client type-check clean, and the TUI has the same errors as base.

Public API is unchanged. The client now also calls the existing session.message.get endpoint.

One case should change before merge. A boundary that is not loaded is always treated as older than everything loaded. When the client already holds the whole history, a missing boundary means it is already gone: a commit that a sync already applied, or a stale revert pointer like #63282. A staged revert in that state now blanks the timeline for good (base showed most of it). A late session.revert.committed wipes the transcript and reloads it. Details are in the inline comments.

Fixes #61189
Related: #63282, #57581

Before

After

Comment thread packages/util/src/session-revert.ts Outdated
) {
if (!boundary) return messages
const index = messages.findIndex((message) => message.id === boundary)
return index === -1 ? [] : messages.slice(0, index)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A boundary that is not loaded is only older than everything when older pages exist. If the whole history is loaded, the boundary has already been deleted. Two ways that happens: a commit that a sync already applied, or a leftover revert.messageID like #63282. Returning [] then hides the whole transcript in both the app and the TUI with no way out. The base hid only the messages with a higher ID. Could callers pass whether older pages exist (the client already tracks this) and treat a missing boundary as no boundary when there are none? #57581 handles it that way.

Comment thread packages/client/src/solid/data.ts Outdated
// do not sort in that order. A boundary that is not loaded is older than every loaded message.
if (loaded)
message.update(sessionID, (draft, index) => {
for (const item of draft.splice(index.get(event.data.to) ?? 0)) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same case here. With the whole history loaded and no event.data.to in the index, ?? 0 removes every loaded message and reloads the transcript. That happens when the event arrives after a sync that already applied the commit, e.g. on reconnect. The reload brings the messages back, but the timeline flashes blank. When no older page exists, a missing boundary should cut nothing.

@opencodejr opencodejr Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The new commit leaves composer/todos.ts as it is on dev and only adds blank lines to a test. That is fine, and the TUI fix I verified earlier is unchanged. App tests pass and tsc -b is clean. Public API is unchanged.

Both earlier threads are still open. When the revert boundary is not loaded, beforeRevert and session.revert.committed still treat it as older than every loaded message, even when the whole history is loaded.

@opencodejr opencodejr Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Both earlier points are fixed. beforeRevert and session.revert.committed now treat a boundary that is not loaded as older than everything only while older pages remain. With the whole transcript loaded, nothing is hidden or cut. I re-ran the TUI repro on the new head and the scheduled prompt and its reply still show after the commit. Util, client, app and TUI tests pass, and app and client type-check clean. Public API is unchanged. Nothing new to flag.

@opencodejr opencodejr Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The new commit only gives the timeline cache test's fake session the more field added last round. timeline-cache.test.ts passes. No production code changed, so my earlier TUI check and test runs still apply. Public API is unchanged. Nothing new to flag.

@opencodejr opencodejr Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The new commits work, and the earlier fixes for a boundary that isn't loaded still hold: a committed revert no longer empties the timeline while the transcript is read again. I re-ran the TUI repro on the new head and the scheduled prompt and its reply stay after the commit. Util, client and app tests (including timeline-cache.test.ts) pass, and app and client type-check clean. Public API is unchanged.

One narrow race in settleRevert, noted inline.

function settleRevert(sessionID: string, read: ReadonlySet<string>) {
const pointer = store.session.info[sessionID]?.revert?.messageID
const awaited = revertReads.get(sessionID)
if (awaited?.status === "awaited") {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A newest-page read can already be running when session.revert.committed arrives. sync.invalidate only marks it invalidated: it still finishes and publishes before the new read runs (createSync). That stale read predates the commit, so it still holds the boundary and the messages after it. This branch takes it as the awaited read and clears the pointer anyway, so the reverted rows show until the next read lands. One option: only settle when read doesn't hold awaited.boundary, or count the read epoch the event saw and skip reads that started before it.

@opencodejr opencodejr Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The race in settleRevert is fixed. A read that holds the boundary no longer settles the awaited revert, so the pointer keeps hiding the reverted rows until the read the event started lands. The new test covers it, client tests pass and tsc --noEmit is clean. Public API is unchanged. Nothing new to flag.

@opencodejr opencodejr Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The new head only merges dev. I checked how the revert handling fits with #66035, which now keeps loaded history when the transcript is read again. Revert settling still runs on every read. A read after a commit whose boundary isn't loaded starts outside the loaded history, so it keeps none of the removed messages. A read from before the commit stays hidden behind the pointer. I re-ran the TUI repro on the merged head and the scheduled prompt and its reply stay after the commit. Client, util, app and TUI tests pass, and client and app type-check clean. This PR's public API is unchanged. Nothing new to flag.

This branch has not been deployed

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant