Skip to content

fix(native-chat): keep transcript rows that share a timestamp in file order - #31794

Closed
QZTnb wants to merge 3 commits into
stablyai:mainfrom
QZTnb:fix/native-chat-transcript-tie-order
Closed

QZTnb wants to merge 3 commits into
stablyai:mainfrom
QZTnb:fix/native-chat-transcript-tie-order

Conversation

@QZTnb

@QZTnb QZTnb commented Oct 22, 2026 •

Copy link
Copy Markdown
Contributor

ELI5

When two lines of a chat transcript carry the exact same time, Orca had no reliable way to tell which came first, and sometimes showed an answer before the thinking that produced it. Now, whenever times tie, the order the lines were written in wins.

What Changed

Before: tied rows fell back to random id order. For a CLI that writes a whole response with one timestamp, the answer could render above its own thinking, and get folded into the "Worked for Xs" group while the thinking showed as the reply.

After: tied rows keep the order they have in the transcript file.

Mechanism: inTranscriptFileOrder already moved a row whose time is earlier than the newest row above it to one millisecond past that time. It now does the same when the time is equal. This covers full reads, tail reads and live appends, which all use the same helper.

The one exception is a pasted-image pair. Claude writes a pasted-image prompt and its image companion row at one time, and sortForImageNormalization pairs them by that shared time. So the ordering state also remembers an image row right above, recognized by the same prompt and image-source markers the chat folds by. A pasted-image prompt or image row with the same file time takes that row's ordered time, so the pair moves together, even below rows that already moved. Any other tied row, user rows included, moves one past the newest time above it. Full, tail and append reads carry this state, including from the first snapshot into later appends.

Why

  • Why reuse the existing clamp: its stated rule is already "a row never sorts above the rows before it". Ties were the case it missed, so extending it keeps one rule in one place.
  • Why not an explicit order key: giving every file-backed row a transcriptOrder would change how those rows sort against rows that have none, such as optimistic sends.
  • Why the image-pair exception: the image pairing relies on equal timestamps. I checked real Claude Code transcripts on my machine: the prompt row comes first, the image row after it, and both share one timestamp. Pairing by the image row above, rather than keeping every tied user row's time, keeps the pair together when rows above it moved and leaves other tied user rows in file order (both found in review).

Linked Issue

Fixes #31785

Visual Proof

Seen with Qoder CLI in Chat UI, which writes every row of a response with one timestamp (see #31796). These shots use Orca's Chinese UI: 工作了 9s is "Worked for 9s", and the prompt asks for a one-sentence Chinese summary of README.md followed by DONE-1 on its own line.

  • Before: the answer is hidden inside "Worked for 9s" and the thinking shows as the reply.

    Before: the thinking shows as the reply and the answer is folded into Worked for 9s
  • After: the thinking is folded and the answer shows below it.

    After: the thinking is folded and the answer shows below it

Testing

Manual: macOS dev build, Qoder CLI 1.1.75 chat: the same turn before and after the change.

Automated (transcript-file-order.test.ts):

  • Full and tail reads: a thinking row and an answer row that share a timestamp render in file order. These cases fail without the change.
  • Image pair: a pasted-image prompt and its companion row keep their shared timestamp.
  • Image pair below moved rows: two tied rows above the pair move first, and the pair still shares one time. Covered for full and tail reads, and for an image row appended after the first snapshot. These cases fail without the pairing rule.
  • Other tied user rows: two tied prompts without image markers keep file order instead of sharing one time.

Local runs:

AI Disclosure

Implemented with Claude Code (Claude Opus 5.5) under my direction. I reviewed the diff and the test runs.

Review

AI agent review of this change:

  • Agents: applies to every file-backed terminal transcript (Claude, OpenClaude, Codex, Grok, OMP). OpenCode-family SQLite reads are untouched.
    • Before: tied rows were ordered by random id.
    • After: tied rows follow file order, and a pasted-image prompt and its image rows keep one shared time.
    • The shift is at most a few milliseconds per tied row, and visible times are minutes.
  • Remote and SSH: same helper on every host. Nothing on the wire changes.
  • Performance: a few comparisons and one small object per decoded row.
  • Security: none.

Agent skill upstream boundary

  • Not applicable, or this change copies or mechanically translates no upstream skill-installer source, tests, fixtures, registry entries, path tables, comments, or documentation.

Notes

  • Page boundary edge case: when a tie straddles a "load older" boundary, the older page's last row can sit one millisecond above the newer page's first row. The same was already true of the existing out-of-order clamp.

Checklist

  • This PR is small and focused
  • I explained what changed and why (ELI5, the user-facing before/after, the mechanism, and why over the alternatives)
  • Before/after screenshots or videos attached for UI changes, or N/A with reason
  • Self-reviewed for correctness, security, and performance
  • Cross-platform, SSH/remote, and path/shortcut impact considered (or N/A)
  • pnpm lint, pnpm typecheck, pnpm test, and pnpm build pass (or CI will cover; local preferred)
    • Typecheck, oxlint, the changed-code gate and the native-chat suites pass locally. CI covers the rest.

🤖 Generated with Claude Code

… order

Terminal transcript rows are sorted by time, and a row that ties the
newest time above it fell back to id order, so random row ids could put
a response's answer above its own thinking. inTranscriptFileOrder already
moved an older row one past the newest time above it; it now does the
same for a tie.

User rows keep a tied time: Claude writes a pasted-image prompt and its
image companion row at one time, and the chat pairs them by that shared
time.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@coderabbitai

coderabbitai Bot commented Oct 22, 2026 •

Copy link
Copy Markdown
Contributor

Review in Change Stack →

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration
  • Configuration used: Repository UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: df2681d8-d5b6-429f-a8ae-4ef0571fa0d5
📥 Commits

Reviewing files that changed from the base of the PR and between 2f30cb1 and ad85380.

📒 Files selected for processing: 2
  • src/main/native-chat/transcript-file-order.test.ts
  • src/main/native-chat/transcript-file-order.ts

Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 7 remain after this review.

📝 Walkthrough

The ordering logic assigns later timestamps to rows whose timestamps are equal to or earlier than the latest assigned timestamp. Matching image-source and image-prompt rows with the same original timestamp retain a shared assigned timestamp. Readers and watchers carry ordering state across messages and snapshots. New tests cover full reads, tail reads, and watcher appends.

Priority: ➖ Normal

Severity of issue fixed: Medium

Merge Risk: ⚪ Minimal · up to ad853

The transcript-ordering change has no established remaining merge-blocking issue; it is ready for normal checks.

Pre-merge checks: Passed 4 · Failed 1

Failed Failed checks: 1 warning

Check name Status Explanation Resolution
Docstring Coverage Warning Docstring coverage is 25.00% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 16 functions across 6 files. Write docstrings for the functions missing them to satisfy the coverage threshold.
Passed Passed checks: 4 passed
Check name Status Explanation
Title check Passed The title clearly and concisely describes the primary change: preserving file order for transcript rows with equal timestamps.
Description check Passed The description is complete and focused. It includes the user impact, implementation details, rationale, linked issue, visual proof, manual and automated testing, AI disclosure, review notes, compatib…
Linked Issues check Passed The change meets #31785. inTranscriptFileOrder changes tied non-image rows to latest + 1, so file order wins over random ID order. It preserves one ordered timestamp for matching pasted-image prom…
Out of Scope Changes check Passed The changes stay within #31785. Production changes update transcript ordering and carry its state through existing read and append paths. Tests verify the timestamp tie rule and the required image-pai…
Full details: Description check

Explanation

The description is complete and focused. It includes the user impact, implementation details, rationale, linked issue, visual proof, manual and automated testing, AI disclosure, review notes, compatibility considerations, and checklist status. It also explains that the remaining full repository checks are covered by CI.

Full details: Linked Issues check

Explanation

The change meets #31785. inTranscriptFileOrder changes tied non-image rows to latest + 1, so file order wins over random ID order. It preserves one ordered timestamp for matching pasted-image prompt and companion rows. The tests cover full reads, tail reads, live appends, moved rows, and tied non-image user rows. The supplied summary also shows that the ordering state is propagated across these read paths.

Full details: Out of Scope Changes check

Explanation

The changes stay within #31785. Production changes update transcript ordering and carry its state through existing read and append paths. Tests verify the timestamp tie rule and the required image-pair exception. No unrelated product behavior is shown.

  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai 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.

Actionable comments posted: 1


ℹ️ Review info
⚙️ Run configuration
  • Configuration used: Repository UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 3cf5487c-f23a-4445-a384-c104b579cb41
📥 Commits

Reviewing files that changed from the base of the PR and between 5c5f9a2 and f9f8070.

📒 Files selected for processing: 2
  • src/main/native-chat/transcript-file-order.test.ts
  • src/main/native-chat/transcript-file-order.ts

Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 9 remain after this review.

…ve move

A tie now moves a row one past the newest time above it. When a
pasted-image prompt and its image row tie rows that already moved, both
moved, but to consecutive times, and the chat, which pairs them by their
shared time, split the image from its prompt.

File order now remembers the user row right above it. A user row with
the same file time takes that row's ordered time, so the pair moves
together. This replaces the rule that kept a user row's tied time. Full,
tail and appended reads carry the same state, so an image row appended
after the first snapshot still pairs.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

@coderabbitai coderabbitai 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.

Caution

Some comments are outside the diff and can’t be posted inline due to GitHub limitations.

⚠️ Outside diff range comments (1)

🟡 Minor · Restrict timestamp reuse to recognized image pairs. · transcript-file-order.ts:37-48

src/main/native-chat/transcript-file-order.ts:37-48
🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win

Restrict timestamp reuse to recognized image pairs.

The full, tail, and incremental readers call inTranscriptFileOrder, but they do not assign transcriptOrder. The Chat UI therefore compares ordinary tied user rows by timestamp and then semantic row ID. This can place the rows in ID order instead of transcript file order.

Reuse the preceding ordered timestamp only for adjacent, recognized image-pair rows.

Suggested fix
 import type { NativeChatMessage } from '../../shared/native-chat-types'
+import {
+  hasImagePromptMarker,
+  isImageSourceUserTurn
+} from '../../shared/native-chat-image-transcript-markers'

 export type TranscriptFileOrder = {
   latest: number
-  userAbove?: { timestamp: number; ordered: number }
+  userAbove?: { timestamp: number; ordered: number; message: NativeChatMessage }
 }

-  const ordered = orderedTimestamp(timestamp, order.latest, isUser ? order.userAbove : undefined)
+  const ordered = orderedTimestamp(
+    timestamp,
+    order.latest,
+    isUser ? order.userAbove : undefined,
+    message
+  )
   return {
     message: ordered === timestamp ? message : { ...message, timestamp: ordered },
     order: {
       latest: Math.max(order.latest, ordered),
-      ...(isUser ? { userAbove: { timestamp, ordered } } : {})
+      ...(isUser ? { userAbove: { timestamp, ordered, message } } : {})
     }
   }
 }

 function orderedTimestamp(
   timestamp: number,
   latest: number,
-  userAbove: TranscriptFileOrder['userAbove']
+  userAbove: TranscriptFileOrder['userAbove'],
+  message: NativeChatMessage
 ): number {
+  const aboveIsImageSource = userAbove ? isImageSourceUserTurn(userAbove.message) : false
+  const messageIsImageSource = isImageSourceUserTurn(message)
+  const isImagePairRow =
+    (aboveIsImageSource && (messageIsImageSource || hasImagePromptMarker(message))) ||
+    (userAbove ? hasImagePromptMarker(userAbove.message) && messageIsImageSource : false)
   // Why: Claude stamps a pasted-image prompt and its image rows alike, and the chat pairs them by
   // that time, so a pair moved below earlier rows moves together.
-  if (userAbove?.timestamp === timestamp) {
+  if (
+    userAbove?.timestamp === timestamp &&
+    userAbove.message.source === message.source &&
+    isImagePairRow
+  ) {
     return userAbove.ordered
   }

ℹ️ Review info
⚙️ Run configuration
  • Configuration used: Repository UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: ee3bb287-0110-4c1b-9b8a-1652d7fbd0a6
📥 Commits

Reviewing files that changed from the base of the PR and between f9f8070 and 2f30cb1.

📒 Files selected for processing: 6
  • src/main/native-chat/transcript-file-order.test.ts
  • src/main/native-chat/transcript-file-order.ts
  • src/main/native-chat/transcript-incremental-reader.ts
  • src/main/native-chat/transcript-stream-lines.ts
  • src/main/native-chat/transcript-tail-reader.ts
  • src/main/native-chat/transcript-watch-engine.ts
🚧 Files skipped from review as they are similar to previous changes: 2
  • src/main/native-chat/transcript-file-order.test.ts
  • src/main/native-chat/transcript-file-order.ts

Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 8 remain after this review.

The previous commit let any user row take the ordered time of a tied
user row right above it. Other tied user rows then shared one time
again, and the chat ordered them by id instead of by the file.

Only a pasted-image prompt and its image rows, recognized by the same
markers the chat folds them by, now share the time. Every other tied
row moves one past the newest time above it.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@nwparker

nwparker commented Nov 5, 2026

Copy link
Copy Markdown
Contributor

Thanks for tracking this down! This was fixed on main by #33628 (028f609): transcript readers now order rows by their position in the file (transcriptOrder from the source offset) rather than by timestamp/id, so rows sharing a timestamp keep file order. Closing as superseded. Feel free to reopen if you still see an answer above its thinking.

@nwparker nwparker closed this Nov 5, 2026
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.

[Bug]: Chat UI can show an answer above its own thinking when transcript rows share a timestamp

2 participants