Skip to content
Merged
2 changes: 1 addition & 1 deletion dev-docs/TRANSCRIPT.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ Message content visibility and presets, token lines, tool renderers, native sess

## Agent conventions

- Partial live projections preserve omitted resident message runs beside their preceding shared row, with leading history before the snapshot and pending optimistic sends at the tail. Shared rows follow the incoming native order. HTTP pages can contain later steps whose events are still in transit; message IDs and wall-clock timestamps remain identity/display data rather than transcript sort keys.
- Partial live projections preserve omitted resident message runs beside their preceding shared row, with leading history before the snapshot and pending optimistic sends at the tail. Shared rows follow the incoming native order. A prompt delivered from the session inbox takes the position the native message page owns: the SDK's `session.inbox.delivered` moves the prompt's row to the reducer's tail, so a delivery that relocates a row which is not the reducer's last owes an authoritative page read — invalidated and finished like a settlement read, shared with any in-flight read and superseded only by a newer admitted execution, and published through the instance layer's frame gates (active session, latest window) exactly as live events' projections are. The outstanding read is a retirement barrier, and a verified reconnect carries the demand: after the seeded page lands, the repair resumes immediately instead of trailing behind the quiet reconciliation. No client-side ordering state survives the delivery: the reducer itself is healed, so reconnect seeding and authoritative replacement carry the corrected order. HTTP pages can contain later steps whose events are still in transit; message IDs and wall-clock timestamps remain identity/display data rather than transcript sort keys.

- The message-content popup and Chat settings share `components/transcript-visibility.ts` and the semantic icons from `components/message-content-icons.ts`; tool presentation metadata lives independently of renderers in `components/tool-call/tool-presentation.ts`. Popup styles live in `styles/components/transcript-filters.css`. Popup rows show a requested state at once and keep it until the save carrying it settles. Each save re-renders the transcript, so a drag sweep (`dev-docs/UI_CONVENTIONS.md`) saves its rows once, on release (`afterSweep`). Saves run in order, each built from the preferences the previous one returned; a batch of rows is sealed as it joins the queue, so a later click never jumps ahead of a preset change nor rides along with a sweep still held.
- Transcript presets are Nothing (hides every row, including all tools), Minimal, Balanced and Everything (`TRANSCRIPT_PRESETS`); saved Detailed choices load as Custom with their effective tool column. Chat settings shows the four as buttons; the popup shows the same choice as a native select under its title, where a disabled Custom entry appears only after a manual row change. Escape closes an open list before the popup. Preset names and descriptions are localized in every locale; covered by `tests/browser/transcript-presets.test.ts`.
Expand Down
83 changes: 83 additions & 0 deletions packages/ui/src/stores/opencode-data-reconnect.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,89 @@ test("a mid-stream reconnect keeps live deltas displaying and reconciles once qu
} finally { f.cleanup() }
})

test("a verified reconnect resumes an outstanding delivery repair after seeding", async ctx => {
ctx.mock.timers.enable({ apis: ["setTimeout"] })
const f = fixture("reconnect-delivery-resume", true)
try {
// The daemon's page: the delivered prompt above its reply.
const stored = [
{ id: "msg_q", type: "user", text: "steer", time: { created: 501 } },
{ id: "msg_live", type: "assistant", agent: "build", model, content: [{ type: "reasoning", text: "" }], time: { created: 500 } },
]
const control = f.controlledList(() => [...stored].reverse())
const promptAboveReply = () => {
const ids = f.store.getSessionMessageIds(sessionId)
return ids.includes("msg_q") && ids.includes("msg_live") && ids.indexOf("msg_q") < ids.indexOf("msg_live")
}
f.dispatch("session.inbox.enqueued", { inboxID: "msg_q", item: { type: "user", payload: { text: "steer" }, delivery: "steer" } })
f.start()
f.dispatch("session.reasoning.started", live)
// The delivery relocates the prompt below the reply; its repair read holds.
f.dispatch("session.inbox.delivered", { inboxID: "msg_q" })
await converge(ctx, () => control.calls >= 1 && control.pending())

// A verified reconnect lands mid-repair while the stream keeps arriving,
// so the quiet reconciliation keeps trailing behind live activity.
f.dispatch("server.connected", { daemon: "daemon-a" })
await microtasks()
f.dispatch("session.reasoning.delta", { ...live, delta: "streaming " })
ctx.mock.timers.tick(0)
await microtasks()

// Seeding landed; the carried demand resumes a fresh repair read at once
// instead of waiting for the quiet resync.
await converge(ctx, () => control.calls >= 2)
control.settle()
await converge(ctx, promptAboveReply)
assert.ok(promptAboveReply(), "the resumed repair reconciles the prompt above its reply while streaming continues")
} finally { f.cleanup() }
})

test("a verified reconnect keeps a delivered prompt's reconciled position", async ctx => {
ctx.mock.timers.enable({ apis: ["setTimeout"] })
const f = fixture("reconnect-delivery-order", true)
try {
// Native storage, oldest first: the page owns the delivered prompt's
// position, here above the reply its admission started.
const stored: any[] = []
let pageReads = 0
f.client.message.list = async () => { pageReads += 1; return { data: [...stored].reverse(), cursor: {} } }

f.dispatch("session.inbox.enqueued", { inboxID: "msg_q", item: { type: "user", payload: { text: "steer" }, delivery: "steer" } })
f.start()
stored.push({ id: "msg_q", type: "user", text: "steer", time: { created: 501 } })
stored.push({ id: "msg_live", type: "assistant", agent: "build", model, content: [], time: { created: 500 } })
// Admission lands after the reply's row exists: the SDK's tail move sinks
// the prompt below it, and the owed authoritative read restores the page's
// order into the reducer itself.
f.dispatch("session.inbox.delivered", { inboxID: "msg_q" })
const promptAboveReply = () => {
const ids = f.store.getSessionMessageIds(sessionId)
return ids.includes("msg_q") && ids.includes("msg_live") && ids.indexOf("msg_q") < ids.indexOf("msg_live")
}
await converge(ctx, promptAboveReply)

// A same-daemon reconnect seeds a fresh reducer from the live rows; the
// healed order must ride along instead of the corrupted one.
f.dispatch("server.connected", { daemon: "daemon-a" })
await microtasks()
ctx.mock.timers.tick(0)
await microtasks()
assert.ok(promptAboveReply(), "reconnect seeding keeps the delivered prompt above its reply")

// Once quiet, the authoritative snapshot replaces the seed and must keep
// the same order (the delivery read is the first page read; the resync's
// is the second).
for (let index = 0; index < 200 && pageReads < 2; index++) {
ctx.mock.timers.tick(1000)
await microtasks()
}
assert.ok(pageReads >= 2, "the quiet reconnect resync read the page")
await converge(ctx, promptAboveReply)
assert.ok(promptAboveReply(), "the quiet reconnect resync keeps the reconciled order")
} finally { f.cleanup() }
})

test("arrivals during a pending reconnect read are ordered and drained when it settles", async ctx => {
ctx.mock.timers.enable({ apis: ["setTimeout"] })
const f = fixture("reconnect-pending-read", true)
Expand Down
169 changes: 168 additions & 1 deletion packages/ui/src/stores/opencode-data-settlement.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { sdkManager } from "../lib/sdk-manager.ts"
import { sseManager } from "../lib/sse-manager.ts"
import { addInstance, handleInstanceInvalidation, removeInstance } from "./instances.ts"
import { messageStoreBus } from "./message-v2/bus.ts"
import { destroyOpenCodeData, invalidateOpenCodeSessionContent } from "./opencode-data.ts"
import { destroyOpenCodeData, applyOpenCodeDataEvent, invalidateOpenCodeSessionContent } from "./opencode-data.ts"
import { handleNativeSessionEvent } from "./session-events.ts"
import { setActiveSession, setSessions } from "./session-state.ts"

Expand Down Expand Up @@ -230,3 +230,170 @@ test("a disposed native projection does not retry a delayed terminal history rea
assert.equal(f.text(), "hello", "disposal must preserve the separately owned visible snapshot")
} finally { f.cleanup() }
})

test("a delivery crossing an HTTP-ahead settlement read reconciles through its own read and outlives the round's settlement", async () => {
const f = fixture("native-delivery-http-ahead")
const sessionId = "s"
const order = () => messageStoreBus.getOrCreate("native-delivery-http-ahead")
.getSessionMessageIds(sessionId)
try {
// Round one is interrupted with a tool still running: its settlement read
// is owed and holds.
const base = { sessionID: sessionId, assistantMessageID: "m1" }
f.emit("session.step.started", { ...base, agent: "build", model, started: 1 })
f.emit("session.tool.input.started", { ...base, id: "tool", name: "read" })
f.emit("session.tool.called", { ...base, id: "tool", input: {} })
f.emit("session.execution.interrupted", { sessionID: sessionId })
assert.equal(f.reads.length, 1)

// The prompt is parked over SSE before that read resolves.
f.emit("session.inbox.enqueued", { sessionID: sessionId, inboxID: "msg_user", item: { type: "user", payload: { text: "crossing prompt" }, delivery: "steer" } })

// The held read returns the daemon's page, which already carries the
// delivered prompt above its reply: the delivery's own SSE still trails.
// Round one's rows appear in their terminal native shapes, as the daemon
// stored them (its idle record's id derives from the terminal event).
const page = [
{ id: "m1", type: "assistant", agent: "build", model, time: { created: 1, completed: 2 }, content: [{
type: "tool", id: "tool", name: "read", time: { created: 2, ran: 3, completed: 4 },
state: { status: "completed", input: {}, content: [{ type: "text", text: "settled" }], metadata: {} },
}] },
{ id: "msg_4", type: "idle", outcome: "interrupted", time: { created: 4 } },
{ id: "msg_user", type: "user", text: "crossing prompt", time: { created: 5 } },
{ id: "reply", type: "assistant", agent: "build", model, time: { created: 6 }, content: [{ type: "text", text: "answer" }] },
]
f.reads[0].resolve({ data: [...page].reverse(), cursor: {} })
await flush()
assert.deepEqual(order(), ["m1", "msg_4", "msg_user", "reply"])

// The delivery event finally arrives over SSE: the SDK relocates the
// prompt below the reply, and the delivery owes its own authoritative
// read, which holds.
f.emit("session.inbox.delivered", { sessionID: sessionId, inboxID: "msg_user" })
assert.equal(f.reads.length, 2, "the relocating delivery owed a repair read")

// The reply's round settles while the repair is outstanding: the reducer
// must survive as the repair's publication owner.
f.emit("session.step.ended", { sessionID: sessionId, assistantMessageID: "reply", finish: "stop", cost: 0, tokens: { input: 0, output: 0, reasoning: 0, cache: { read: 0, write: 0 } } })
f.emit("session.execution.succeeded", { sessionID: sessionId })
assert.equal(f.reads.length, 2, "the settlement must not start a second read beneath the repair")

// Resolving the repair publishes the page's order through the surviving
// reducer instead of leaving the sunk prompt behind. (msg_8 is the round's
// own idle record, inserted by its terminal event.)
f.reads[1].resolve({ data: [...page].reverse(), cursor: {} })
await flush()
assert.deepEqual(order(), ["m1", "msg_4", "msg_user", "reply", "msg_8"], "the crossing prompt reconciles above its reply")

// The settled reducer retired once the repair settled: the next event
// lands on a fresh one, holding only its own row.
const after = applyOpenCodeDataEvent("native-delivery-http-ahead", "/fixture", {
id: "evt_probe", type: "session.step.started", created: 99, location: { directory: "/fixture" },
data: { sessionID: sessionId, assistantMessageID: "m3", agent: "build", model, started: 9 },
} as any)
await flush()
assert.deepEqual(
after.session.message.list(sessionId).map((row: { id: string }) => row.id),
["m3"],
"the settled reducer retired once the repair settled")
} finally { f.cleanup() }
})

test("a settlement read superseding a delivery repair keeps the reducer until it publishes", async () => {
const instanceId = "native-delivery-superseded-repair"
const f = fixture(instanceId)
const sessionId = "s"
const zero = { input: 0, output: 0, reasoning: 0, cache: { read: 0, write: 0 } }
const tool = () => Object.values(messageStoreBus.getOrCreate(instanceId).getMessage("prior")?.parts ?? {})
.map((part: any) => part.data).find((part: any) => part.type === "tool") as any
try {
f.emit("session.step.started", { sessionID: sessionId, assistantMessageID: "initial", agent: "build", model, started: 1 })
f.emit("session.inbox.enqueued", { sessionID: sessionId, inboxID: "q", item: { type: "user", payload: { text: "queued follow-up" }, delivery: "queue" } })
f.emit("session.step.ended", { sessionID: sessionId, assistantMessageID: "initial", finish: "stop", cost: 0, tokens: zero })
f.emit("session.step.started", { sessionID: sessionId, assistantMessageID: "prior", agent: "build", model, started: 2 })
f.emit("session.tool.input.started", { sessionID: sessionId, assistantMessageID: "prior", id: "lost", name: "read" })
f.emit("session.tool.called", { sessionID: sessionId, assistantMessageID: "prior", id: "lost", input: {} })
// The tool finished natively, but its terminal event never arrived.
f.emit("session.step.ended", { sessionID: sessionId, assistantMessageID: "prior", finish: "stop", cost: 0, tokens: zero })
f.emit("session.inbox.delivered", { sessionID: sessionId, inboxID: "q" })
assert.equal(f.reads.length, 1, "the relocating delivery started repair read A")
f.emit("session.step.started", { sessionID: sessionId, assistantMessageID: "reply", agent: "build", model, started: 3 })
f.emit("session.step.ended", { sessionID: sessionId, assistantMessageID: "reply", finish: "stop", cost: 0, tokens: zero })
// The settlement invalidates beneath A: its trailing read B owns publication.
f.emit("session.execution.succeeded", { sessionID: sessionId })
assert.equal(tool()?.state?.status, "running")
const page = [
{ id: "initial", type: "assistant", agent: "build", model, time: { created: 1, completed: 2 }, content: [] },
{ id: "prior", type: "assistant", agent: "build", model, time: { created: 2, completed: 3 }, content: [{
type: "tool", id: "lost", name: "read", time: { created: 2, ran: 3, completed: 4 },
state: { status: "completed", input: {}, content: [{ type: "text", text: "native result" }], metadata: {} },
}] },
{ id: "q", type: "user", text: "queued follow-up", time: { created: 3 } },
{ id: "reply", type: "assistant", agent: "build", model, time: { created: 4, completed: 5 }, content: [] },
]
f.reads[0].resolve({ data: [...page].reverse(), cursor: {} })
await flush(); await flush()
assert.equal(f.reads.length, 2, "the superseding read B started")
f.reads[1].resolve({ data: [...page].reverse(), cursor: {} })
await flush(); await flush()
assert.equal(tool()?.state?.status, "completed", "B publishes before the settled reducer retires")
} finally { f.cleanup() }
})

test("a delivery repair completing over a history window leaves that window's membership alone", async () => {
const instanceId = "native-delivery-history-window"
const f = fixture(instanceId)
const store = messageStoreBus.getOrCreate(instanceId)
const sessionId = "s"
try {
f.emit("session.inbox.enqueued", { sessionID: sessionId, inboxID: "msg_h", item: { type: "user", payload: { text: "parked" }, delivery: "steer" } })
f.emit("session.step.started", { sessionID: sessionId, assistantMessageID: "h_reply", agent: "build", model, started: 2 })
f.emit("session.inbox.delivered", { sessionID: sessionId, inboxID: "msg_h" })
assert.equal(f.reads.length, 1, "the relocating delivery owed a repair read")
await flush()
const sunk = store.getSessionMessageIds(sessionId)
assert.deepEqual(sunk, ["h_reply", "msg_h"], "the late delivery sinks the prompt until the repair lands")

// The reader moves to a history window before the repair completes: the
// repair must not rewrite that window's membership or cursors.
store.setMessageWindow(sessionId, { kind: "history", resumeCursor: "c1", newerCursors: [null] })
f.reads[0].resolve({
data: [
{ id: "msg_h", type: "user", text: "parked", time: { created: 3 } },
{ id: "h_reply", type: "assistant", agent: "build", model, time: { created: 2 }, content: [] },
].reverse(),
cursor: {},
})
await flush()
assert.deepEqual(store.getSessionMessageIds(sessionId), sunk, "the history window keeps its membership")
assert.equal(store.getMessageWindow(sessionId)?.kind, "history", "the window keeps its cursors")
} finally { f.cleanup() }
})

test("a delivery repair completing for an inactive session does not project into its display", async () => {
const instanceId = "native-delivery-inactive"
const f = fixture(instanceId)
const store = messageStoreBus.getOrCreate(instanceId)
const sessionId = "s"
try {
f.emit("session.inbox.enqueued", { sessionID: sessionId, inboxID: "msg_i", item: { type: "user", payload: { text: "parked" }, delivery: "steer" } })
f.emit("session.step.started", { sessionID: sessionId, assistantMessageID: "i_reply", agent: "build", model, started: 2 })
f.emit("session.inbox.delivered", { sessionID: sessionId, inboxID: "msg_i" })
assert.equal(f.reads.length, 1, "the relocating delivery owed a repair read")
await flush()
const before = store.getSessionMessageIds(sessionId)

// The reader switches sessions before the repair completes: the inactive
// session's display is left to its next activation read.
setActiveSession(instanceId, "other")
f.reads[0].resolve({
data: [
{ id: "msg_i", type: "user", text: "parked", time: { created: 3 } },
{ id: "i_reply", type: "assistant", agent: "build", model, time: { created: 2 }, content: [] },
].reverse(),
cursor: {},
})
await flush()
assert.deepEqual(store.getSessionMessageIds(sessionId), before, "an inactive session's display keeps its projected page")
} finally { f.cleanup() }
})
Loading
Loading