Skip to content

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

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

Hona wants to merge 3 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.
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.
  • 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) {
  if (!boundary) return messages
  const index = messages.findIndex((message) => message.id === boundary)
  // A boundary that is not loaded is older than every loaded message.
  return index === -1 ? [] : messages.slice(0, index)
}
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 --> D[Every loaded message came after it: remove them all]
  C --> E{Is a message left?}
  D --> E
  E -- yes --> F[Done: the cursor names a kept message]
  E -- no --> G[Clear the cursor, read the newest page again]
  C --> H{Was an input waiting?}
  D --> H
  H -- yes --> I[Drop the inputs whose rows went, read the inbox again]
Loading
  • A boundary that is not loaded: the loaded transcript is the end of the server's, so the boundary is older than all of it. A staged revert shows none of the loaded messages. A committed revert removes them and reads the transcript again, because the client then holds nothing and its cursor names a message that is gone.
  • 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/util/todo.ts, tui/src/routes/session/index.tsx Place, but an unloaded boundary counted as no boundary Showed reverted todos and messages
  • 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: app/src/session/composer/todos.ts goes by place, but still takes an unloaded boundary as no boundary. A change to that file must first rewrite its parser for the lint.
  • Not order: the ID tie-breaks in session and project lists only make a sort stable.

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

Tests

  • e2e/regression/timeline-stream.spec.ts, "revert": a staged and a committed revert on a transcript whose IDs do not sort in its order, on both timelines. On dev the staged case shows ["msg_0002", "msg_0004"] where ["msg_sch_0001", "msg_0002"] belong, and the committed case shows [].
  • packages/client/test/solid-revert.test.ts: removal by place, a boundary that is not loaded or is the oldest loaded message, 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).

Fixes #61189

Related: #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.

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.

Session history replay truncates after edit-and-resend of a message on legacy-schema long sessions (data intact, UI stops rendering earlier checkpoint)

1 participant