The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(desktop): a refreshed page that holds a live turn whole draws it once
Tekniumee4539b5b137
diff --git a/apps/desktop/e2e/core/tail-resync-merge.spec.ts b/apps/desktop/e2e/core/tail-resync-merge.spec.tsnew file mode 100644index 00000000000..a76866907ac--- /dev/null+++ b/apps/desktop/e2e/core/tail-resync-merge.spec.ts@@ -0,0 +1,278 @@+/**+ * C2 core: a refreshed newest page merged onto a long chat's live turn.+ *+ * Every settled turn re-reads the newest stored page (120 rows) and merges it+ * onto what the live stream drew. Once a chat holds more rows than one page,+ * that page starts inside an older tool turn, so its first stored row is not+ * the first row the window holds and the merge pairs rows by stored id. The+ * streamed copy of the turn that just finished has stored ids on its settled+ * reply only: the prompt and the narration sealed before each tool were drawn+ * from frames. Those rows must be recognised as the stored rows the page+ * brings (#139741: the whole turn, prompt included, rendered twice; #141771:+ * the turn's narration re-rendered under its final answer).+ *+ * The chat is seeded with wide tool turns (one call fanning out to many tool+ * rows) until it spans more than a page, then each checked turn narrates+ * before every one of three tools. Each checked turn is sampled for a while+ * after it settles, so a duplicate that a later read heals still fails.+ */++import { type ElectronApplication, expect, type Page, test } from '@playwright/test'++import {+ coreAppEnv,+ type CoreSandbox,+ createCoreSandbox,+ launchCoreApp,+ recordWebSockets,+ send,+ waitForInteractive,+ writeProviderHome,+ type WsRecorder+} from './harness'+import { gate, type ScriptedProvider, startScriptedProvider } from './provider'++const nonce = Math.random()+ .toString(36)+ .slice(2, 8)+ .replace(/[^a-z0-9]/g, 'x')+ .padEnd(4, 'q')++const U = (n: number) => `U${n}-${nonce}`+const A = (n: number) => `A${n}-${nonce}`+const N = (n: number, k: number) => `A${n}i${k}-${nonce}`+const MARKER_RE = new RegExp(`\\b[UA]\\d+(?:i\\d+)?-${nonce}\\b`, 'g')++const words = (marker: string, ...rest: string[]) => [+ `${marker} `,+ ...rest.map((w, i) => (i === rest.length - 1 ? w : `${w} `))+]++const echo = (tag: string) => ({ name: 'terminal', args: { command: `echo core-${tag}` } })++// One seed turn = prompt + SEED_ROUNDS x (narration row with a tool call + tool row) + reply.+const SEED_ROUNDS = 10+const SEED_TURNS = 6+const CHECKED_TURNS = 3+const OBSERVE_MS = 13_000++const SHOTS = process.env.HERMES_E2E_SHOT_DIR++function viewport(page: Page) {+ return page.locator('[data-slot="aui_thread-viewport"]').filter({ visible: true }).first()+}++function stopButton(page: Page) {+ return page.locator('[data-slot="composer-root"] button[aria-label="Stop"]')+}++async function settled(page: Page, ws: WsRecorder, marker: string) {+ await expect+ .poll(() => ws.events.some(e => e.type === 'message.complete' && String(e.payload?.text ?? '').includes(marker)), {+ timeout: 120_000,+ message: `message.complete for ${marker}`+ })+ .toBe(true)+ await expect(stopButton(page)).toHaveCount(0, { timeout: 30_000 })+}++async function withApp(+ label: string,+ body: (ctx: {+ app: ElectronApplication+ page: Page+ provider: ScriptedProvider+ sandbox: CoreSandbox+ ws: WsRecorder+ }) => Promise<void>+) {+ const provider = await startScriptedProvider()+ const sandbox = createCoreSandbox(label)+ writeProviderHome(sandbox.hermesHome, provider.url)+ const { app, page } = await launchCoreApp(coreAppEnv(sandbox))+ const ws = recordWebSockets(page)++ try {+ await waitForInteractive(app, page)+ await body({ app, page, provider, sandbox, ws })+ } finally {+ await app.close().catch(() => undefined)+ await provider.close()+ sandbox.cleanup()+ }+}++/** How often each marker shows in the visible thread, and whether they read in order. */+async function turnShape(+ page: Page,+ markers: string[]+): Promise<{ counts: number[]; doubled: string[]; ordered: boolean }> {+ const text = await page.evaluate(() => {+ const el = ([...document.querySelectorAll('[data-slot="aui_thread-viewport"]')] as HTMLElement[]).find(+ node => node.getClientRects().length > 0+ )++ return el ? el.innerText.replace(/\s+/g, ' ') : ''+ })++ const counts = markers.map(marker => text.split(marker).length - 1)+ const positions = markers.map(marker => text.indexOf(marker))+ const seen = new Map<string, number>()++ for (const marker of text.match(MARKER_RE) ?? []) {+ seen.set(marker, (seen.get(marker) ?? 0) + 1)+ }++ return {+ counts,+ doubled: [...seen].filter(([, count]) => count > 1).map(([marker, count]) => `${marker}x${count}`),+ ordered: positions.every((at, i) => at >= 0 && (i === 0 || at > positions[i - 1]))+ }+}++async function seedLongChat(page: Page, provider: ScriptedProvider, ws: WsRecorder) {+ for (let n = 1; n <= SEED_TURNS; n += 1) {+ provider.script(U(n), [+ ...Array.from({ length: SEED_ROUNDS }, (_, k) => ({+ text: words(N(n, k + 1), 'seeding'),+ toolCalls: [echo(`seed-${n}-${k}`)]+ })),+ { text: words(A(n), 'seeded') }+ ])+ await send(page, `${U(n)} seed`, 'Enter', ws)+ await settled(page, ws, A(n))+ }+}++/**+ * Sample the newest turn for OBSERVE_MS after it settled while the window-return+ * backstop re-reads the newest page (the app gates it to one read per 5 s; the+ * sessions.changed tick does the same on its own cadence). Returns every sample+ * where a marker was not rendered exactly once, in order.+ */+async function observeTurn(page: Page, markers: string[], label: string): Promise<string[]> {+ const bad: string[] = []+ const deadline = Date.now() + OBSERVE_MS+ let shot = false+ let nextReturn = 0++ while (Date.now() < deadline) {+ if (Date.now() >= nextReturn) {+ nextReturn = Date.now() + 5_500+ await page.evaluate(() => document.dispatchEvent(new Event('visibilitychange')))+ }++ const shape = await turnShape(page, markers)++ if (shape.counts.some(count => count !== 1) || !shape.ordered || shape.doubled.length) {+ bad.push(+ `${markers.map((m, i) => `${m}x${shape.counts[i]}`).join(' ')} ordered=${shape.ordered} doubled=${shape.doubled.join(',')}`+ )++ if (SHOTS && !shot) {+ shot = true+ await page.screenshot({ path: `${SHOTS}/${label}-symptom.png` })+ }+ }++ await page.waitForTimeout(200)+ }++ if (SHOTS) {+ await page.screenshot({ path: `${SHOTS}/${label}-settled.png` })++ const probe = await page.evaluate(async () => {+ const sid = decodeURIComponent(location.hash.replace(/^#\/?/, '').split('?')[0] ?? '')++ const latest = await (window as any).hermesDesktop.api({+ path: `/api/sessions/${sid}/messages?order=latest&limit=120&include_compacted=true`+ })++ const rows = latest?.messages ?? []++ return {+ head: rows.slice(0, 2).map((m: any) => `${m.id}:${m.role}`),+ bubbles: [+ ...document.querySelectorAll('[data-slot="aui_user-message-root"], [data-slot="aui_assistant-message-root"]')+ ]+ .slice(-14)+ .map(el => (el as HTMLElement).innerText.replace(/\s+/g, ' ').slice(0, 80))+ }+ })++ console.log(`[probe ${label}] ${JSON.stringify(probe)}`)+ }++ return bad+}++test('a long chat keeps each finished tool turn once, in order, after the newest page merges', async () => {+ test.setTimeout(900_000)++ await withApp('tail-resync', async ({ page, provider, ws }) => {+ await test.step(`seed ${SEED_TURNS} wide tool turns (more than one page of rows)`, () =>+ seedLongChat(page, provider, ws))++ // Every checked turn so far must keep rendering once, in order, under later merges.+ const shown: string[] = []++ for (let c = 0; c < CHECKED_TURNS; c += 1) {+ const n = SEED_TURNS + 1 + c++ await test.step(`turn ${n}: narration before each of three tools, then the answer`, async () => {+ provider.script(U(n), [+ { text: words(N(n, 1), 'first', 'look'), toolCalls: [echo(`n${n}-a`)] },+ { text: words(N(n, 2), 'second', 'look'), toolCalls: [echo(`n${n}-b`)] },+ { text: words(N(n, 3), 'third', 'look'), toolCalls: [echo(`n${n}-c`)] },+ { text: words(A(n), 'final', 'answer') }+ ])+ await send(page, `${U(n)} narrate tools`, 'Enter', ws)+ await settled(page, ws, A(n))++ shown.push(U(n), N(n, 1), N(n, 2), N(n, 3), A(n))+ const bad = await observeTurn(page, shown, `narrated-${n}`)++ expect(bad.slice(0, 5), `turn ${n} rendered once and in order after the page merge`).toEqual([])+ })+ }+ })+})++test('a long chat keeps a steered tool turn once, in order, after the newest page merges', async () => {+ test.setTimeout(900_000)++ await withApp('tail-resync-steer', async ({ page, provider, ws }) => {+ await test.step(`seed ${SEED_TURNS} wide tool turns (more than one page of rows)`, () =>+ seedLongChat(page, provider, ws))++ const shown: string[] = []++ for (let c = 0; c < CHECKED_TURNS; c += 1) {+ const n = SEED_TURNS + 1 + 2 * c++ await test.step(`turn ${n}: narration and a tool, steered by turn ${n + 1} during the answer`, async () => {+ const hold = gate()+ provider.script(U(n), [+ { text: words(N(n, 1), 'first', 'look'), toolCalls: [echo(`t${n}-a`)] },+ { text: words(N(n, 2), 'second', 'look'), toolCalls: [echo(`t${n}-b`)] },+ { text: words(A(n), 'answer', 'cut', 'by', 'steer'), holdAfterFirstChunk: hold }+ ])+ provider.script(U(n + 1), [+ { text: words(N(n + 1, 1), 'steered', 'look'), toolCalls: [echo(`t${n}-c`)] },+ { text: words(A(n + 1), 'steered', 'answer') }+ ])+ await send(page, `${U(n)} narrate then steer`, 'Enter', ws)+ await provider.streamStarted(U(n), 2)+ await expect(viewport(page)).toContainText(A(n))+ await send(page, `${U(n + 1)} change course`, 'Enter', ws)+ hold.open()+ await settled(page, ws, A(n + 1))++ shown.push(U(n), N(n, 1), N(n, 2), U(n + 1), N(n + 1, 1), A(n + 1))+ const bad = await observeTurn(page, shown, `steered-${n}`)++ expect(bad.slice(0, 5), `steered turn ${n} rendered once and in order after the page merge`).toEqual([])+ })+ }+ })+})diff --git a/apps/desktop/src/app/chat/transcript-backfill.ts b/apps/desktop/src/app/chat/transcript-backfill.tsindex 5bac291df53..194b7684089 100644--- a/apps/desktop/src/app/chat/transcript-backfill.ts+++ b/apps/desktop/src/app/chat/transcript-backfill.ts@@ -15,6 +15,7 @@* drift on the next page.*/+import { 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'@@ -368,7 +369,9 @@ function mergeOverlappingTail(previous: ChatMessage[], refreshedTail: ChatMessagreturn pending}- const previousTrailing = place(previous, false)+ // 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(refreshedTail, true)const stored = [...byRowId.entries()]diff --git a/apps/desktop/src/app/chat/transcript-receipt-turns.test.ts b/apps/desktop/src/app/chat/transcript-receipt-turns.test.tsnew file mode 100644index 00000000000..71afb3f7560--- /dev/null+++ b/apps/desktop/src/app/chat/transcript-receipt-turns.test.ts@@ -0,0 +1,113 @@+import type { PersistedTurn } from '@hermes/shared'+import { describe, expect, it } from 'vitest'++import { graftRefreshedTailOntoBackfill } from '@/app/chat/transcript-backfill'+import { type ChatMessage, chatMessageText, toChatMessages } from '@/lib/chat-messages'+import type { SessionMessage } from '@/types/hermes'++/**+ * A long chat's newest page begins inside an older tool turn, so the refresh+ * graft pairs rows by stored id. The turn that just ran was drawn live and+ * carries stored ids on some rows at most. The page holds every stored row of+ * that turn, and the turn's receipt says so: each of its messages must render+ * once (#139741 whole turn twice, #141771 narration again under the answer).+ */++const at = (id: number) => 1_000 + id++const user = (id: number, text: string): SessionMessage =>+ ({ id, role: 'user', content: text, timestamp: at(id) }) as SessionMessage++const narration = (id: number, text: string, call: string): SessionMessage =>+ ({+ id,+ role: 'assistant',+ content: text,+ timestamp: at(id),+ tool_calls: [{ id: call, type: 'function', function: { name: 'terminal', arguments: '{"command":"echo"}' } }]+ }) as SessionMessage++const tool = (id: number, call: string): SessionMessage =>+ ({ id, role: 'tool', content: 'ok', tool_call_id: call, tool_name: 'terminal', timestamp: at(id) }) as SessionMessage++const answer = (id: number, text: string): SessionMessage =>+ ({ id, role: 'assistant', content: text, timestamp: at(id) }) as SessionMessage++/** An older turn of three narrated tool rounds: rows first..first+7. */+const olderTurn = (first: number, tag: string): SessionMessage[] => [+ user(first, `${tag} prompt`),+ narration(first + 1, `${tag} look one`, `${tag}-1`),+ tool(first + 2, `${tag}-1`),+ narration(first + 3, `${tag} look two`, `${tag}-2`),+ tool(first + 4, `${tag}-2`),+ narration(first + 5, `${tag} look three`, `${tag}-3`),+ tool(first + 6, `${tag}-3`),+ answer(first + 7, `${tag} answer`)+]++const texts = (messages: ChatMessage[]) => messages.map(message => chatMessageText(message)).join('\n')++const count = (haystack: string, needle: string) => haystack.split(needle).length - 1++const live = (id: string, role: ChatMessage['role'], text: string, extra: Partial<ChatMessage> = {}): ChatMessage => ({+ id,+ role,+ parts: [{ type: 'text', text }],+ ...extra+})++describe('a refreshed page that holds the live turn whole', () => {+ // Window: two older turns hydrated from an earlier page. Page: starts at the+ // second narration of the first older turn (its fold is addressed by a row+ // the window does not use as a message id), then the live turn's stored rows.+ const windowRows = [...olderTurn(1, 'old-a'), ...olderTurn(9, 'old-b')]+ const liveRows = [user(17, 'steered prompt'), narration(18, 'live look', 'live-1'), tool(19, 'live-1')]++ it('draws a steered turn once when its receipt is not complete', () => {+ const receipt: PersistedTurn = {+ row_ids: [17, 18, 19, 20, 21],+ complete: false,+ user_row_ids: [17, 20],+ submission_id: 'sub-steer'+ }++ const window = [+ ...toChatMessages(windowRows),+ live('user-sub-steer', 'user', 'steered prompt', { rowId: 17 }),+ live('assistant-stream-1', 'assistant', 'live look', { interim: false }),+ live('user-other', 'user', 'change course', { rowId: 20 }),+ live('assistant-stream-2', 'assistant', 'steered answer', { rowId: 21, persistedTurn: receipt })+ ]++ const page = toChatMessages([+ ...olderTurn(1, 'old-a').slice(3),+ ...olderTurn(9, 'old-b'),+ ...liveRows,+ user(20, 'change course'),+ answer(21, 'steered answer')+ ])++ const merged = texts(graftRefreshedTailOntoBackfill(page, window))++ for (const text of ['steered prompt', 'live look', 'change course', 'steered answer', 'old-b look two']) {+ expect(count(merged, text), text).toBe(1)+ }+ })++ 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' }++ const window = [+ ...toChatMessages(windowRows),+ live('user-sub-late', 'user', 'late prompt'),+ live('assistant-stream-1', 'assistant', 'late answer', { persistedTurn: receipt })+ ]++ // A stale page that ends before the turn's last row.+ const page = toChatMessages([...olderTurn(1, 'old-a').slice(3), ...olderTurn(9, 'old-b')])+ const merged = texts(graftRefreshedTailOntoBackfill(page, window))++ expect(count(merged, 'late prompt')).toBe(1)+ expect(count(merged, 'late answer')).toBe(1)+ })+})diff --git a/apps/desktop/src/app/chat/transcript-receipt-turns.ts b/apps/desktop/src/app/chat/transcript-receipt-turns.tsnew file mode 100644index 00000000000..e4d88f59aaa--- /dev/null+++ b/apps/desktop/src/app/chat/transcript-receipt-turns.ts@@ -0,0 +1,114 @@+/**+ * A refreshed page that holds a whole turn the window drew live supersedes the+ * window's copy of that turn, decided by the turn's completion receipt.+ *+ * The stored-id merge (transcript-backfill `mergeOverlappingTail`) pairs rows+ * by stored id. A live turn reaches it with stored ids on only some of its rows:+ * a stopped turn binds none, a steered turn's receipt is not `complete`, and the+ * narration the stream sealed before each tool never has one. Those rows used+ * to travel as "unstored" rows beside the stored copies the page brings, so the+ * whole turn, prompt included, rendered twice (#139741), or the narration+ * re-rendered under the final answer once the duplicate final collapsed+ * (#141771).+ *+ * `message.complete.persisted_turn` names every stored row of the turn+ * (`row_ids`) and the turn's own prompt (`submission_id`, `user_row_id(s)`).+ * The page is one contiguous run of the newest stored rows, so a page that+ * holds the turn's first row and reaches its last row holds the whole turn.+ * The window's rows from that prompt to the receipt bubble are then the live+ * copy of rows the page carries, and only the page's copy is kept.+ */++import type { PersistedTurn } from '@hermes/shared'++import { transcriptRowIds } from '@/app/session/hooks/use-session-actions/pending-turn-identity'+import type { ChatMessage } from '@/lib/chat-messages'++const isRowId = (value: unknown): value is number =>+ typeof value === 'number' && Number.isSafeInteger(value) && value > 0++/** Index of the turn's prompt in the window, by the receipt's own identity. */+function receiptPromptIndex(window: ChatMessage[], receipt: PersistedTurn, before: number): number {+ const submission = typeof receipt.submission_id === 'string' && receipt.submission_id ? receipt.submission_id : ''+ const userRows = [receipt.user_row_id, ...(receipt.user_row_ids ?? [])].filter(isRowId)++ for (let index = before; index >= 0; index -= 1) {+ const message = window[index]++ if (message.role !== 'user') {+ continue+ }++ if (+ (submission && message.id === `user-${submission}`) ||+ (message.rowId !== undefined && userRows.includes(message.rowId))+ ) {+ // A steer row of the same turn sits after the prompt: keep walking while+ // an earlier user row still belongs to this receipt.+ const earlier = receiptPromptIndex(window, receipt, index - 1)++ return earlier >= 0 && earlier < index ? earlier : index+ }+ }++ 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+ 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 []+ }++ const start = receiptPromptIndex(window, receipt, end)++ if (start < 0) {+ return []+ }++ const indices: number[] = []++ for (let index = start; index <= end; index += 1) {+ const ids = transcriptRowIds(window[index])++ // A row that carries a stored id outside the turn is not this turn's copy:+ // the pairing is wrong, so nothing is dropped.+ if (ids.some(id => id < first || id > last)) {+ return []+ }++ indices.push(index)+ }++ return indices+}++/** 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))++ if (!pageRows.size) {+ return window+ }++ const dropped = new Set<number>()++ window.forEach((message, index) => {+ if (message.persistedTurn && !message.pending) {+ receiptTurnIndices(window, index, pageRows).forEach(at => dropped.add(at))+ }+ })++ return dropped.size ? window.filter((_, index) => !dropped.has(index)) : window+}