From 14c63ca42a1133dde51c050e301bd6ecb6e2c86f Mon Sep 17 00:00:00 2001 From: Shakker Date: Fri, 27 Mar 2026 10:29:32 +0000 Subject: [PATCH] fix(tui): prune chat log system messages atomically --- src/tui/components/chat-log.test.ts | 16 ++++++++++++++++ src/tui/components/chat-log.ts | 10 ++++++++-- 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/src/tui/components/chat-log.test.ts b/src/tui/components/chat-log.test.ts index 700a2abb9d2..a57de225f52 100644 --- a/src/tui/components/chat-log.test.ts +++ b/src/tui/components/chat-log.test.ts @@ -53,6 +53,22 @@ describe("ChatLog", () => { expect(chatLog.children.length).toBe(20); }); + it("prunes system messages atomically when a non-system entry overflows the log", () => { + const chatLog = new ChatLog(20); + for (let i = 1; i <= 20; i++) { + chatLog.addSystem(`system-${i}`); + } + + chatLog.addUser("hello"); + + const rendered = chatLog.render(120).join("\n"); + expect(rendered).not.toMatch(/\bsystem-1\b/); + expect(rendered).toMatch(/\bsystem-2\b/); + expect(rendered).toMatch(/\bsystem-20\b/); + expect(rendered).toContain("hello"); + expect(chatLog.children.length).toBe(20); + }); + it("renders BTW inline and removes it when dismissed", () => { const chatLog = new ChatLog(40); diff --git a/src/tui/components/chat-log.ts b/src/tui/components/chat-log.ts index c46e6065b9b..07dd91d21c0 100644 --- a/src/tui/components/chat-log.ts +++ b/src/tui/components/chat-log.ts @@ -57,9 +57,15 @@ export class ChatLog extends Container { this.btwMessage = null; } + private createSystemMessage(text: string): Container { + const entry = new Container(); + entry.addChild(new Spacer(1)); + entry.addChild(new Text(theme.system(text), 1, 0)); + return entry; + } + addSystem(text: string) { - this.append(new Spacer(1)); - this.append(new Text(theme.system(text), 1, 0)); + this.append(this.createSystemMessage(text)); } addUser(text: string) {