Skip to content

Commit f7ebe4c

Browse files
committed
fix(tui): preserve shared prompts when scrollback is full
1 parent 6499143 commit f7ebe4c

2 files changed

Lines changed: 40 additions & 5 deletions

File tree

‎src/tui/components/chat-log.test.ts‎

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -387,6 +387,38 @@ describe("ChatLog", () => {
387387
expect(normalizeTestText(chatLog.render(120).join("\n"))).toContain("Still streaming.");
388388
});
389389

390+
it("preserves a delayed shared prompt and its active reply when scrollback is full", () => {
391+
const chatLog = new ChatLog(20);
392+
chatLog.updateAssistant("Already streaming.", "shared-run");
393+
for (let index = 0; index < 19; index += 1) {
394+
chatLog.addSystem(`Older notice ${index}.`);
395+
}
396+
397+
chatLog.addLiveUser("Sent from the other client.", {
398+
messageId: "shared-overflow-user",
399+
runId: "shared-run",
400+
});
401+
chatLog.addLiveUser("Sent from the other client.", {
402+
messageId: "shared-overflow-user",
403+
runId: "shared-run",
404+
});
405+
406+
const rendered = normalizeTestText(chatLog.render(120).join("\n"));
407+
expect(chatLog.children).toHaveLength(20);
408+
expect(rendered).toContain("Sent from the other client.");
409+
expect(rendered).toContain("Already streaming.");
410+
expect(rendered).not.toContain("Older notice 0.");
411+
expect(rendered.match(/Sent from the other client\./g)).toHaveLength(1);
412+
expect(rendered.indexOf("Sent from the other client.")).toBeLessThan(
413+
rendered.indexOf("Already streaming."),
414+
);
415+
416+
chatLog.updateAssistant("Still streaming after overflow.", "shared-run");
417+
expect(normalizeTestText(chatLog.render(120).join("\n"))).toContain(
418+
"Still streaming after overflow.",
419+
);
420+
});
421+
390422
it("deduplicates authoritative user events and adopts the matching pending prompt", () => {
391423
const chatLog = new ChatLog(40);
392424
chatLog.addPendingUser("shared-run", "Persisted prompt.");

‎src/tui/components/chat-log.ts‎

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -87,9 +87,11 @@ export class ChatLog extends Container {
8787
}
8888
}
8989

90-
private pruneOverflow() {
90+
private pruneOverflow(protectedComponents?: ReadonlySet<Component>) {
9191
while (this.children.length > this.maxComponents) {
92-
const oldest = this.children[0];
92+
const oldest = protectedComponents
93+
? this.children.find((component) => !protectedComponents.has(component))
94+
: this.children[0];
9395
if (!oldest) {
9496
return;
9597
}
@@ -231,12 +233,13 @@ export class ChatLog extends Container {
231233
frozen?.values().next().value ??
232234
(options.runId ? this.streamingRuns.get(options.runId) : undefined);
233235
const assistantIndex = assistant ? this.children.indexOf(assistant) : -1;
234-
if (assistantIndex >= 0) {
236+
if (assistant && assistantIndex >= 0) {
235237
// Transcript broadcasts can trail the first delta; insert their prompt
236-
// before the existing reply without resetting live stream or tool state.
238+
// before the existing reply. Preserve both when full scrollback evicts
239+
// older components so the newly recovered prompt cannot disappear.
237240
this.repeatableSystemMessage = null;
238241
this.children.splice(assistantIndex, 0, component);
239-
this.pruneOverflow();
242+
this.pruneOverflow(new Set([component, assistant]));
240243
return component;
241244
}
242245
this.appendNonSystem(component);

0 commit comments

Comments
 (0)