Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions packages/tui/src/util/part.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,13 @@ function isTextLike(part: Part): part is TextPart | ReasoningPart {
// (`packages/opencode/src/session/processor.ts`):
// - `message.part.delta` text evolves only by append (`text-delta`,
// `reasoning-delta`), so a snapshot that is a strict prefix of what is
// already rendered is necessarily older — keep the longer streamed text;
// already rendered is older — keep the longer streamed text, except for
// the first terminal text snapshot described below;
// - a snapshot that extends the rendered text accumulated more deltas — adopt;
// - the only legitimate non-cumulative text write is the `text-end` snapshot
// (the `experimental.text.complete` plugin rewrites the final text) and it
// is published together with `time.end`; that snapshot is adopted;
// is published together with `time.end`; adopt it even if it is a prefix or
// empty. Reasoning snapshots retain the existing prefix behavior;
// - a part reaches its terminal snapshot exactly once, so once `time.end` is
// set the part is a sink: a replayed row without `time.end` — even one that
// "extends" the final text — is older and must not reopen the part, and a
Expand All @@ -65,6 +67,9 @@ export function mergePartText(current: Part | undefined, incoming: Part): Part {
return { ...incoming, text: currentText }
}

// text-end may rewrite text to a strict prefix or even an empty string.
// Reasoning has no corresponding plugin rewrite and keeps prefix protection.
if (current.type === "text" && incoming.type === "text" && incomingTerminal) return incoming
if (currentText.startsWith(incomingText)) return { ...incoming, text: currentText }
if (incomingText.startsWith(currentText)) return incoming
if (incomingTerminal) return incoming
Expand Down
31 changes: 30 additions & 1 deletion packages/tui/test/util/part.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -374,6 +374,35 @@ test("the terminal text-end snapshot is the only non-cumulative write allowed",
expect(partText(parts[0])).toBe("Hello there")
})

test.each(["Hello", ""])("terminal text rewrite accepts prefix or empty text: %j", (rewritten) => {
let parts = applyPartUpdated(undefined, text({ text: "", time: { start: 1 } }))
const original = parts[0]
parts = applyPartDelta(parts, "part-1", "text", "Hello secret")!
parts = applyPartUpdated(parts, text({ text: rewritten, time: { start: 99, end: 2 } }))
expect(parts[0]).toBe(original)
expect(partText(parts[0])).toBe(rewritten)
expect((parts[0] as TextPart).time).toEqual({ start: 1, end: 2 })
})

test.each(["Hello", ""])("non-terminal text prefix or empty snapshot remains stale: %j", (stale) => {
let parts = applyPartUpdated(undefined, text({ text: "", time: { start: 1 } }))
const original = parts[0]
parts = applyPartDelta(parts, "part-1", "text", "Hello secret")!
parts = applyPartUpdated(parts, text({ text: stale, time: { start: 1 } }))
expect(parts[0]).toBe(original)
expect(partText(parts[0])).toBe("Hello secret")
expect((parts[0] as TextPart).time).toEqual({ start: 1 })
})

test.each(["Hello", ""])("already-terminal text rejects another terminal prefix or empty snapshot: %j", (stale) => {
let parts = applyPartUpdated(undefined, text({ text: "Hello secret", time: { start: 1, end: 10 } }))
const original = parts[0]
parts = applyPartUpdated(parts, text({ text: stale, time: { start: 99, end: 5 } }))
expect(parts[0]).toBe(original)
expect(partText(parts[0])).toBe("Hello secret")
expect((parts[0] as TextPart).time).toEqual({ start: 1, end: 10 })
})

test("a finished part is not rewound by an unfinished conflicting snapshot", () => {
let parts = applyPartUpdated(undefined, text({ text: "Hello there", time: { start: 1, end: 2 } }))
const original = parts[0]
Expand All @@ -382,7 +411,7 @@ test("a finished part is not rewound by an unfinished conflicting snapshot", ()
expect(partText(parts[0])).toBe("Hello there")
})

test("a prefix snapshot is always older than the streamed text", () => {
test("a non-terminal prefix snapshot is older than the streamed text", () => {
let parts = applyPartUpdated(undefined, text({ text: "Hello world" }))
const original = parts[0]
// Durable row persisted partway through the same stream.
Expand Down
Loading