Skip to content

The agent that grows with you

fix(desktop): a turn stopped during its tool run draws once in a long chat

Teknium29e8f90b0403
diff --git a/apps/desktop/src/app/chat/transcript-backfill.ts b/apps/desktop/src/app/chat/transcript-backfill.ts
index bca00c3dfbc..15b20326e73 100644
--- a/apps/desktop/src/app/chat/transcript-backfill.ts
+++ b/apps/desktop/src/app/chat/transcript-backfill.ts
@@ -15,7 +15,11 @@
* drift on the next page.
*/
-import { dropPageFoldsCutByTheBoundary, dropReceiptTurnsThePageHolds } from '@/app/chat/transcript-receipt-turns'
+import {
+ dropPageFoldsCutByTheBoundary,
+ dropPageRowsOfStoppedTurns,
+ dropReceiptTurnsThePageHolds
+} from '@/app/chat/transcript-receipt-turns'
import { transcriptRowIds } from '@/app/session/hooks/use-session-actions/pending-turn-identity'
import { getOlderSessionMessages, getSessionMessages, type ProfileScope } from '@/hermes'
import { type ChatMessage, chatMessageText, toChatMessages } from '@/lib/chat-messages'
@@ -372,7 +376,8 @@ function mergeOverlappingTail(previous: ChatMessage[], refreshedTail: ChatMessag
// A turn the page holds whole is the page's to draw, whatever stored ids
// its live rows managed to bind (see transcript-receipt-turns).
const previousTrailing = place(dropReceiptTurnsThePageHolds(previous, refreshedTail), false)
- const refreshedTrailing = place(dropPageFoldsCutByTheBoundary(previous, refreshedTail), true)
+ const freshRows = dropPageRowsOfStoppedTurns(previous, dropPageFoldsCutByTheBoundary(previous, refreshedTail))
+ const refreshedTrailing = place(freshRows, true)
const stored = [...byRowId.entries()]
.sort((left, right) => left[0] - right[0])
diff --git a/apps/desktop/src/app/chat/transcript-receipt-turns.test.ts b/apps/desktop/src/app/chat/transcript-receipt-turns.test.ts
index 5bb607b6809..58a6d6a6d4d 100644
--- a/apps/desktop/src/app/chat/transcript-receipt-turns.test.ts
+++ b/apps/desktop/src/app/chat/transcript-receipt-turns.test.ts
@@ -171,6 +171,54 @@ describe('a refreshed page that holds the live turn whole', () => {
expect(count(stopped, 'old-b answer')).toBe(1)
})
+ it('draws a turn stopped during its tool run once, keeping its stopped mark', () => {
+ // No reply text: the turn ends on its tool result and an empty assistant
+ // row, neither of which a page paints.
+ const receipt: PersistedTurn = {
+ row_ids: [17, 18, 19, 20],
+ complete: false,
+ user_row_id: 17,
+ user_row_ids: [17],
+ submission_id: 'sub-tool-stop'
+ }
+
+ const stopped = bindStoppedTurnReceipt(
+ [
+ ...toChatMessages(windowRows),
+ live('user-sub-tool-stop', 'user', 'steered prompt'),
+ {
+ ...live('assistant-stream-1', 'assistant', 'live look'),
+ parts: [
+ { type: 'text', text: 'live look' },
+ {
+ type: 'tool-call',
+ toolCallId: 'live-1',
+ toolName: 'terminal',
+ args: { command: 'echo' },
+ argsText: '{"command":"echo"}',
+ interrupted: true
+ }
+ ],
+ interrupted: true,
+ pending: false
+ }
+ ],
+ receipt
+ )
+
+ const page = toChatMessages([
+ ...olderTurn(1, 'old-a').slice(3),
+ ...olderTurn(9, 'old-b'),
+ ...liveRows,
+ answer(20, '')
+ ])
+ const merged = graftRefreshedTailOntoBackfill(page, stopped)
+
+ expect(count(texts(merged), 'steered prompt')).toBe(1)
+ expect(count(texts(merged), 'live look')).toBe(1)
+ expect(merged.find(message => chatMessageText(message).includes('live look'))?.interrupted).toBe(true)
+ })
+
it('keeps a live turn the page does not reach yet', () => {
const receipt: PersistedTurn = { row_ids: [17, 18, 19, 20, 21], complete: false, submission_id: 'sub-late' }
diff --git a/apps/desktop/src/app/chat/transcript-receipt-turns.ts b/apps/desktop/src/app/chat/transcript-receipt-turns.ts
index ad73dbcc72a..4fbeefcb2ca 100644
--- a/apps/desktop/src/app/chat/transcript-receipt-turns.ts
+++ b/apps/desktop/src/app/chat/transcript-receipt-turns.ts
@@ -56,23 +56,16 @@ function receiptPromptIndex(window: ChatMessage[], receipt: PersistedTurn, befor
return -1
}
-/** Window indices of the receipt turn ending at `end`, or none when its identity is incomplete. */
-function receiptTurnIndices(window: ChatMessage[], end: number, pageRows: Set<number>): number[] {
- const receipt = window[end].persistedTurn
+/** First and last stored row of the receipt on `message`, when it names any. */
+function receiptBounds(message: ChatMessage): null | { first: number; last: number; receipt: PersistedTurn } {
+ const receipt = message.persistedTurn
const rows = (receipt?.row_ids ?? []).filter(isRowId)
- if (!receipt || !rows.length) {
- return []
- }
-
- const first = Math.min(...rows)
- const last = Math.max(...rows)
- const pageLast = Math.max(...pageRows)
-
- if (!pageRows.has(first) || pageLast < last) {
- return []
- }
+ return receipt && rows.length ? { first: Math.min(...rows), last: Math.max(...rows), receipt } : null
+}
+/** Window indices of the turn ending at `end`, or none when its identity is incomplete. */
+function windowTurnIndices(window: ChatMessage[], end: number, first: number, last: number, receipt: PersistedTurn) {
const start = receiptPromptIndex(window, receipt, end)
if (start < 0) {
@@ -102,6 +95,17 @@ function receiptTurnIndices(window: ChatMessage[], end: number, pageRows: Set<nu
return indices
}
+/** Window indices of the receipt turn ending at `end` that the page holds whole. */
+function receiptTurnIndices(window: ChatMessage[], end: number, pageRows: Set<number>): number[] {
+ const bounds = receiptBounds(window[end])
+
+ if (!bounds || !pageRows.has(bounds.first) || Math.max(...pageRows) < bounds.last) {
+ return []
+ }
+
+ return windowTurnIndices(window, end, bounds.first, bounds.last, bounds.receipt)
+}
+
/** Drop the window rows whose turn the page holds whole, by the turn's receipt. */
export function dropReceiptTurnsThePageHolds(window: ChatMessage[], page: ChatMessage[]): ChatMessage[] {
const pageRows = new Set(page.flatMap(transcriptRowIds))
@@ -121,6 +125,48 @@ export function dropReceiptTurnsThePageHolds(window: ChatMessage[], page: ChatMe
return dropped.size ? window.filter((_, index) => !dropped.has(index)) : window
}
+/**
+ * A turn stopped during its tool run ends on stored rows no page paints: the
+ * tool result folds into its call and the empty closing assistant row is
+ * skipped. A page that holds the turn therefore never reaches its last row,
+ * the window's live copy was never dropped, and the page's copy of the
+ * narration and tool drew beside it. When the page holds the stopped turn's
+ * first row but not its last, the window's live copy (its receipt bound by the
+ * interrupted completion, carrying the "Response stopped" mark) is the whole
+ * turn, and the page's rows inside the receipt are the partial copy that goes.
+ */
+export function dropPageRowsOfStoppedTurns(window: ChatMessage[], page: ChatMessage[]): ChatMessage[] {
+ const pageRows = new Set(page.flatMap(transcriptRowIds))
+
+ if (!pageRows.size) {
+ return page
+ }
+
+ const pageLast = Math.max(...pageRows)
+ const liveRows = new Set<number>()
+
+ window.forEach((message, index) => {
+ const bounds = message.interrupted && !message.pending ? receiptBounds(message) : null
+
+ if (
+ bounds &&
+ pageRows.has(bounds.first) &&
+ pageLast < bounds.last &&
+ windowTurnIndices(window, index, bounds.first, bounds.last, bounds.receipt).length
+ ) {
+ bounds.receipt.row_ids.filter(isRowId).forEach(id => liveRows.add(id))
+ }
+ })
+
+ const partial = (message: ChatMessage) => {
+ const rows = transcriptRowIds(message)
+
+ return rows.length > 0 && rows.every(id => liveRows.has(id))
+ }
+
+ return liveRows.size && page.some(partial) ? page.filter(message => !partial(message)) : page
+}
+
/**
* The newest page can start inside an older tool turn. History folds that turn
* under its first stored row, so the page's fold carries only the rows from