fix(tui): keep transcript rows stable when history is prepended (#52824)

Session rows have no id, so reconcile matched them by position. Each page of older history shifted every row into a different store object and remounted the visible transcript. Newly mounted markdown is blank until highlighting finishes, so the transcript blinked once per page. This was most visible with Low verbosity, where opening a session loads several pages to complete the oldest group.
This commit is contained in:
Simon Klee
2026-10-03 08:09:37 +02:00
committed by GitHub
Unverified
parent 12810e84d3
commit 40679546d4
2 changed files with 115 additions and 38 deletions
+47 -17
View File
@@ -21,6 +21,7 @@ import {
type Verbosity,
defaultVerbosity,
} from "./grouping/session"
import { groupID } from "./anchors"
export type { CacheUsage, PartRef, SessionRow } from "./grouping/session"
/**
@@ -47,7 +48,7 @@ export function createSessionRows(sessionID: Accessor<string>, onSynced?: (sessi
const data = useData()
const client = useClient()
const config = useConfig()
const [rows, setRows] = createStore<SessionRow[]>([])
const [rows, setRows] = createStore<(SessionRow & { key?: string })[]>([])
const revertBoundary = () => data.session.get(sessionID())?.revert?.messageID
const turnTokens = () => Boolean(config.data.debug?.turn_tokens)
const verbosity = () => config.data.session?.verbosity ?? defaultVerbosity
@@ -79,6 +80,25 @@ export function createSessionRows(sessionID: Accessor<string>, onSynced?: (sessi
return rows
}
// Rows have no `id`, Solid's default reconcile key. Matching by position instead would move every
// row into another store object whenever older history is prepended, remounting the whole transcript.
// Rows inserted live carry no key, so key the current rows too before matching.
function rebuild() {
setRows(
produce((draft) => {
draft.forEach((row) => {
row.key = rowKey(row)
})
}),
)
setRows(
reconcile(
reduce().map((row) => ({ ...row, key: rowKey(row) })),
{ key: "key" },
),
)
}
function pendingPermissions() {
return new Set(
(data.session.permission.list(sessionID()) ?? []).flatMap((request) =>
@@ -99,13 +119,13 @@ export function createSessionRows(sessionID: Accessor<string>, onSynced?: (sessi
createEffect(
on([sessionID, () => client.connection.status()], ([id, status]) => {
if (status !== "connected") return
setRows(reconcile(reduce()))
rebuild()
void data.session.pending.sync(id).catch(() => undefined)
void data.session.message
.sync(id)
.then(async () => {
if (sessionID() !== id) return
setRows(reconcile(reduce()))
rebuild()
// Restoration waits for complete boundary groups so saved group IDs resolve.
await completeGroupBoundary({
rows,
@@ -122,15 +142,7 @@ export function createSessionRows(sessionID: Accessor<string>, onSynced?: (sessi
// Re-reduce when the revert boundary changes (stage/clear/commit). These reactions defer
// their first run: the mount effect above has already reduced the same state.
createEffect(
on(
revertBoundary,
() => {
setRows(reconcile(reduce()))
},
{ defer: true },
),
)
createEffect(on(revertBoundary, rebuild, { defer: true }))
createEffect(
on(
@@ -140,7 +152,7 @@ export function createSessionRows(sessionID: Accessor<string>, onSynced?: (sessi
if (item.type === "user" && item.delivery === "queue") return [`${item.id}:queue`]
return []
}),
() => setRows(reconcile(reduce())),
rebuild,
{ defer: true },
),
)
@@ -166,12 +178,12 @@ export function createSessionRows(sessionID: Accessor<string>, onSynced?: (sessi
]
: [],
),
() => setRows(reconcile(reduce())),
rebuild,
{ defer: true },
),
)
createEffect(on([turnTokens, verbosity], () => setRows(reconcile(reduce())), { defer: true }))
createEffect(on([turnTokens, verbosity], rebuild, { defer: true }))
const appendMessage = (messageID: string) =>
setRows(
@@ -297,12 +309,12 @@ export function createSessionRows(sessionID: Accessor<string>, onSynced?: (sessi
data.on("session.step.ended", (event) => {
if (event.data.sessionID !== sessionID() || ["tool-calls", "unknown"].includes(event.data.finish)) return
appendFooter(event.data.assistantMessageID)
if (turnTokens()) setRows(reconcile(reduce()))
if (turnTokens()) rebuild()
}),
data.on("session.step.failed", (event) => {
if (event.data.sessionID !== sessionID()) return
appendFooter(event.data.assistantMessageID)
if (turnTokens()) setRows(reconcile(reduce()))
if (turnTokens()) rebuild()
}),
]
onCleanup(() => subscriptions.forEach((unsubscribe) => unsubscribe()))
@@ -310,6 +322,24 @@ export function createSessionRows(sessionID: Accessor<string>, onSynced?: (sessi
return rows
}
function rowKey(row: SessionRow) {
switch (row.type) {
case "group":
return groupID(row, 0)
case "part":
return JSON.stringify([row.type, row.ref.messageID, row.ref.partID])
case "message":
case "assistant-footer":
return JSON.stringify([row.type, row.messageID])
case "compaction-queued":
return JSON.stringify([row.type, row.inboxID])
case "turn-usage":
return JSON.stringify([row.type, row.messageIDs[0]])
default:
return row satisfies never
}
}
export function reduceSessionRows(
messages: SessionMessageInfo[],
inputs = new Set<string>(),
+68 -21
View File
@@ -850,7 +850,7 @@ test("reconnects the event stream and resyncs active data", async () => {
}
})
test("completes exploration when a queued prompt is promoted", async () => {
test("completes exploration and keeps live rows when a queued prompt is promoted", async () => {
const events = createEventStream()
const sessionID = "session-promotion"
const calls = createFetch((url) => {
@@ -858,10 +858,16 @@ test("completes exploration when a queued prompt is promoted", async () => {
}, events)
let rows!: ReturnType<typeof createSessionRows>
let client!: ReturnType<typeof useClient>
let data!: ReturnType<typeof useData>
let synced = false
function Probe() {
client = useClient()
rows = createSessionRows(() => sessionID)
data = useData()
rows = createSessionRows(
() => sessionID,
() => (synced = true),
)
return <box />
}
@@ -879,6 +885,9 @@ test("completes exploration when a queued prompt is promoted", async () => {
try {
await wait(() => client.connection.status() === "connected")
// Rebuilds from the history sync and the new assistant message must land before the parts stream in,
// as they do live; otherwise those rebuilds, not the live append, create the part rows.
await wait(() => synced)
emitEvent(events, {
id: "evt_step_started",
created: 1,
@@ -892,11 +901,25 @@ test("completes exploration when a queued prompt is promoted", async () => {
model: { id: "model", providerID: "provider" },
},
})
await wait(() => data.session.message.get(sessionID, "message-assistant") !== undefined)
emitEvent(events, {
id: "evt_text_started",
created: 1,
type: "session.text.started",
durable: durable(sessionID, 1),
data: { sessionID, assistantMessageID: "message-assistant", ordinal: 0 },
})
emitEvent(events, {
id: "evt_text_delta",
created: 1,
type: "session.text.delta",
data: { sessionID, assistantMessageID: "message-assistant", ordinal: 0, delta: "Looking" },
})
emitEvent(events, {
id: "evt_tool_started",
created: 2,
type: "session.tool.input.started",
durable: durable(sessionID, 1),
durable: durable(sessionID, 2),
data: {
sessionID,
assistantMessageID: "message-assistant",
@@ -905,12 +928,13 @@ test("completes exploration when a queued prompt is promoted", async () => {
},
})
await wait(() => rows.some((row) => row.type === "group" && !row.completed))
const text = rows.find((row) => row.type === "part")
emitEvent(events, {
id: "evt_prompt_admitted",
created: 3,
type: "session.inbox.enqueued",
durable: durable(sessionID, 2),
durable: durable(sessionID, 3),
data: {
sessionID,
inboxID: "message-user",
@@ -924,14 +948,17 @@ test("completes exploration when a queued prompt is promoted", async () => {
id: "evt_prompt_promoted",
created: 4,
type: "session.inbox.delivered",
durable: durable(sessionID, 3),
durable: durable(sessionID, 4),
data: {
sessionID,
inboxID: "message-user",
},
})
await wait(() => rows.find((row) => row.type === "group")?.completed === true)
expect(rows.at(-1)).toEqual({ type: "message", messageID: "message-user" })
expect(rows.at(-1)).toMatchObject({ type: "message", messageID: "message-user" })
// Promotion rebuilds every row; the live text must keep its store object or it remounts.
expect(text).toMatchObject({ type: "part", ref: { messageID: "message-assistant", partID: "text:0" } })
expect(rows.find((row) => row.type === "part")).toBe(text)
} finally {
app.renderer.destroy()
}
@@ -980,7 +1007,7 @@ test("updates and removes queued inputs from durable lifecycle events", async ()
},
})
await wait(() => data.session.pending.list(sessionID).length === 1)
expect(rows).not.toContainEqual({ type: "message", messageID: "message-queued" })
expect(rows).not.toContainEqual(expect.objectContaining({ type: "message", messageID: "message-queued" }))
emitEvent(events, {
id: "evt_queue_steered",
@@ -994,7 +1021,7 @@ test("updates and removes queued inputs from durable lifecycle events", async ()
.list(sessionID)
.some((item) => item.id === "message-queued" && item.type !== "compaction" && item.delivery === "steer"),
)
expect(rows).toContainEqual({ type: "message", messageID: "message-queued" })
expect(rows).toContainEqual(expect.objectContaining({ type: "message", messageID: "message-queued" }))
emitEvent(events, {
id: "evt_queue_restored",
@@ -1008,7 +1035,7 @@ test("updates and removes queued inputs from durable lifecycle events", async ()
.list(sessionID)
.some((item) => item.id === "message-queued" && item.type !== "compaction" && item.delivery === "queue"),
)
expect(rows).not.toContainEqual({ type: "message", messageID: "message-queued" })
expect(rows).not.toContainEqual(expect.objectContaining({ type: "message", messageID: "message-queued" }))
emitEvent(events, {
id: "evt_cancel_admitted",
@@ -1080,17 +1107,19 @@ test("classifies live tool rows independently of their call ID", async () => {
})
await wait(() => rows.length > 0)
expect(rows).toEqual([{ type: "part", ref: { messageID: "message-assistant", partID: "reasoning:0" } }])
expect(unwrap(rows)).toMatchObject([
{ type: "part", ref: { messageID: "message-assistant", partID: "reasoning:0" } },
])
} finally {
app.renderer.destroy()
}
})
test("loads older pages until the oldest exploration group is complete before reporting sync", async () => {
test("loads older pages until the oldest exploration group is complete before reporting sync and keeps existing rows", async () => {
const events = createEventStream()
const sessionID = "session-boundary"
const model = { id: "model", providerID: "provider" }
// One prompt, then 50 single-read steps: the 20-message first page cuts the group.
// One prompt, 50 single-read steps, then an answer: the 20-message first page cuts the group.
const history = [
{ type: "user", id: "msg_000", text: "Explore", time: { created: 0 } },
...Array.from({ length: 50 }, (_, index) => ({
@@ -1110,11 +1139,23 @@ test("loads older pages until the oldest exploration group is complete before re
},
],
})),
{
type: "assistant",
id: "msg_051",
agent: "build",
model,
time: { created: 51, completed: 51 },
finish: "stop",
content: [{ type: "text", text: "Done" }],
},
]
const pages: string[] = []
const calls = createFetch((url) => {
const older = Promise.withResolvers<void>()
const calls = createFetch(async (url) => {
if (url.pathname !== `/api/session/${sessionID}/message`) return
const end = Number(url.searchParams.get("cursor") ?? history.length)
const cursor = url.searchParams.get("cursor")
if (cursor) await older.promise
const end = Number(cursor ?? history.length)
const start = Math.max(0, end - Number(url.searchParams.get("limit") ?? 20))
pages.push(`${start}-${end}`)
return json({ data: history.slice(start, end).toReversed(), cursor: start > 0 ? { next: String(start) } : {} })
@@ -1146,14 +1187,20 @@ test("loads older pages until the oldest exploration group is complete before re
try {
await wait(() => client.connection.status() === "connected")
const answer = () => rows.find((row) => row.type === "part" && row.ref.messageID === "msg_051")
await wait(() => answer() !== undefined)
const mounted = answer()
older.resolve()
await wait(() => synced.length > 0, 4000)
expect(pages).toEqual(["31-51", "11-31", "0-11"])
expect(pages).toEqual(["32-52", "12-32", "0-12"])
// Sync is reported only once the group's true first read is loaded.
expect(synced[0]).toEqual({ type: "message", messageID: "msg_000" })
expect(synced[0]).toMatchObject({ type: "message", messageID: "msg_000" })
const group = rows[1]
if (group?.type !== "group") throw new Error("Expected exploration group")
expect(group.size).toBe(50)
expect(groupRefs(group)[0]).toEqual({ messageID: "msg_001", partID: "read-0" })
// The transcript keys rows by store object, so a new object would remount the answer.
expect(answer()).toBe(mounted)
} finally {
app.renderer.destroy()
}
@@ -1603,7 +1650,7 @@ test("tracks session status from active sessions and execution events", async ()
const message = data.session.message.get("session-manual", "message-compaction")
return message?.type === "compaction" && message.status === "completed"
})
expect(manualRows.filter((row) => row.type === "message")).toEqual([
expect(manualRows.filter((row) => row.type === "message")).toMatchObject([
{ type: "message", messageID: "message-compaction" },
])
expect(manualRows.find((row) => row.type === "message" && row.messageID === "message-compaction")).toBe(
@@ -1706,7 +1753,7 @@ test.each(["before", "between", "after"])("shows compaction admitted %s steers i
}),
)
await wait(() => rows.length === 3)
expect(rows).toEqual([
expect(unwrap(rows)).toMatchObject([
{ type: "compaction-queued", inboxID: "compact" },
{ type: "message", messageID: "a" },
{ type: "message", messageID: "b" },
@@ -1719,7 +1766,7 @@ test.each(["before", "between", "after"])("shows compaction admitted %s steers i
data: { sessionID, reason: "manual", recent: "", inputID: "compact" },
})
await wait(() => rows[0]?.type === "message")
expect(rows).toEqual(["compact", "a", "b"].map((messageID) => ({ type: "message", messageID })))
expect(unwrap(rows)).toMatchObject(["compact", "a", "b"].map((messageID) => ({ type: "message", messageID })))
emitEvent(events, {
id: "evt_compaction_ended",
created: 5,
@@ -1737,7 +1784,7 @@ test.each(["before", "between", "after"])("shows compaction admitted %s steers i
})
}
await app.renderOnce()
expect(rows).toEqual(["compact", "a", "b"].map((messageID) => ({ type: "message", messageID })))
expect(unwrap(rows)).toMatchObject(["compact", "a", "b"].map((messageID) => ({ type: "message", messageID })))
} finally {
app.renderer.destroy()
}
@@ -1799,7 +1846,7 @@ test("restores queued compaction from durable pending input", async () => {
"message-compaction-later",
])
await wait(() => rows.filter((row) => row.type === "compaction-queued").length === 2)
expect(rows.filter((row) => row.type === "compaction-queued")).toEqual([
expect(rows.filter((row) => row.type === "compaction-queued")).toMatchObject([
{ type: "compaction-queued", inboxID: "message-compaction-queued" },
{ type: "compaction-queued", inboxID: "message-compaction-later" },
])