Skip to content

CanvasAnvil is an open source AI diagram maker and flowchart generator - an infinite-canvas diagram editor and self-hosted drawio alternative that writes editable .drawio files. Text to diagram, then fix nodes, layout and labels in place. Also AI presentations, interior design CAD/BOM, posters and infographics.

fix(chat): keep the newest turns when the history budget runs out

codingFeng101ca6bf5729717
diff --git a/client/shared/chat/chat-storage.ts b/client/shared/chat/chat-storage.ts
index db82602..5547c4b 100644
--- a/client/shared/chat/chat-storage.ts
+++ b/client/shared/chat/chat-storage.ts
@@ -127,15 +127,19 @@ export function buildRecentHistoryContext(
.slice(-limits.maxMessages)
if (recent.length === 0) return ""
+ // Walked newest-first so the budget is spent on the turns that matter:
+ // this exists for intent continuity, and the latest exchange is the one
+ // the model needs. Filling from the oldest end would drop it.
const lines: string[] = []
let totalChars = 0
- for (const message of recent) {
+ for (let i = recent.length - 1; i >= 0; i -= 1) {
+ const message = recent[i]
const text = truncateForStorage(String(message.content || ""), limits.maxMessageChars)
if (!text) continue
const remaining = limits.maxTotalChars - totalChars
if (remaining <= 0) break
const clipped = text.length > remaining ? truncateForStorage(text, remaining) : text
- lines.push(`[${message.role === "assistant" ? "Assistant" : "User"}] ${clipped}`)
+ lines.unshift(`[${message.role === "assistant" ? "Assistant" : "User"}] ${clipped}`)
totalChars += clipped.length
}
diff --git a/tests/chat-storage.test.ts b/tests/chat-storage.test.ts
index ea96759..3cdd56f 100644
--- a/tests/chat-storage.test.ts
+++ b/tests/chat-storage.test.ts
@@ -169,22 +169,38 @@ test("buildRecentHistoryContext respects its budget", () => {
assert.ok(context.length < 600, `expected a small context, got ${context.length}`);
});
-test("KNOWN ODDITY: over budget, it drops the newest turns, not the oldest", () => {
- // It takes the last maxMessages turns, then fills forward from the oldest of
- // those until maxTotalChars runs out — so turns 48 and 49 are the ones lost.
- // For something labelled "recent chat history (for intent continuity)" that
- // is backwards. Pinned as-is so the fix is a deliberate, visible change.
- const messages = Array.from({ length: 50 }, (_, i) => message("user", `turn ${i} ${"y".repeat(500)}`));
+test("an over-budget history keeps the newest turns, not the oldest", () => {
+ // This is for intent continuity, so the latest exchange is the one the
+ // model needs; spending the budget from the oldest end would drop it.
+ const messages = [
+ message("user", `old ${"y".repeat(200)}`),
+ message("user", `middle ${"y".repeat(200)}`),
+ message("user", `newest ${"y".repeat(200)}`),
+ ];
const context = buildRecentHistoryContext(messages, {
- maxMessages: 5,
- maxMessageChars: 100,
- maxTotalChars: 300,
+ maxMessages: 10,
+ maxMessageChars: 500,
+ maxTotalChars: 250,
});
- assert.ok(context.includes("turn 45"), "keeps the oldest of the window");
- assert.ok(!context.includes("turn 49"), "and drops the newest turn");
+ assert.ok(context.includes("newest"), context.slice(0, 120));
+ assert.ok(!context.includes("old y"), "the oldest turn is the one dropped");
});
+test("turns stay in the order they were said", () => {
+ const context = buildRecentHistoryContext([
+ message("user", "first"),
+ message("assistant", "second"),
+ message("user", "third"),
+ ]);
+ assert.ok(
+ context.indexOf("first") < context.indexOf("second") &&
+ context.indexOf("second") < context.indexOf("third"),
+ context,
+ );
+});
+
+
/**
* The guard that stops ChatInput's two file-sync effects from swapping arrays
* forever. It lives in chat-input.tsx alongside the effects it protects; this