Record your screen, ship a demo. Free and open-source, GPU-accelerated, no watermarks, no subscriptions. Windows, macOS, Linux. Actively maintained.
TypeScript cross-platformelectronffmpegopen-sourceproduct-demorustscreen-capturescreen-recorderscreen-recordingscreen-studioscreencastspeech-to-texttypescriptvideo-editingvideo-editorwaylandwgpuwhisper
fix(ai): reject non-reducing compaction
Arham Wani5672a99de337
diff --git a/electron/ai-edition/chat-compaction.test.ts b/electron/ai-edition/chat-compaction.test.tsindex e10ee3454..5c9c17bd3 100644--- a/electron/ai-edition/chat-compaction.test.ts+++ b/electron/ai-edition/chat-compaction.test.ts@@ -4,6 +4,7 @@ import {applyCompaction,budgetSnapshot,buildCompactionPrompt,+ compactionReducesHistory,estimateHistoryTokens,shouldCompact,} from "./chat-compaction";@@ -88,9 +89,23 @@ describe("applyCompaction", () => {const out = applyCompaction(msgs, 2, "summary text", "2026-02-01T00:00:00.000Z");expect(out).toHaveLength(3);expect(out[0]?.content).toBe("summary text");+ expect(out[0]?.id).toMatch(/^summary_\d+$/);expect(out[1]?.id).toBe("u2");expect(out.at(-1)?.id).toBe("a2");expect(estimateHistoryTokens(out)).toBeLessThan(estimateHistoryTokens(msgs));+ expect(compactionReducesHistory(msgs, out)).toBe(true);+ });++ it("rejects a summary that does not reduce estimated context use", () => {+ const msgs = [+ msg("user", "small", "u1"),+ msg("assistant", "reply", "a1"),+ msg("user", "recent", "u2"),+ msg("assistant", "tail", "a2"),+ ];+ const oversized = applyCompaction(msgs, 2, "x".repeat(1_000), "2026-02-01T00:00:00.000Z");++ expect(compactionReducesHistory(msgs, oversized)).toBe(false);});});diff --git a/electron/ai-edition/chat-compaction.ts b/electron/ai-edition/chat-compaction.tsindex 49ccb803a..1b11b5e32 100644--- a/electron/ai-edition/chat-compaction.ts+++ b/electron/ai-edition/chat-compaction.ts@@ -93,6 +93,14 @@ export function applyCompaction(return [summaryMessage, ...tail];}+/** True only when a proposed compaction strictly reduces estimated context use. */+export function compactionReducesHistory(+ original: AiEditionChatMessage[],+ compacted: AiEditionChatMessage[],+): boolean {+ return estimateHistoryTokens(compacted) < estimateHistoryTokens(original);+}+/** System-prompt addendum to ask the LLM to compact its own history. */export const COMPACTION_SYSTEM_PROMPT = ["Summarize the conversation so far in 8 short bullet points and 2 short paragraphs.",diff --git a/electron/ai-edition/chat-service.ts b/electron/ai-edition/chat-service.tsindex 668f5fd04..55086acfc 100644--- a/electron/ai-edition/chat-service.ts+++ b/electron/ai-edition/chat-service.ts@@ -26,6 +26,7 @@ import {budgetSnapshot,buildCompactionPrompt,COMPACTION_SYSTEM_PROMPT,+ compactionReducesHistory,DEFAULT_BUDGET_TOKENS,shouldCompact,} from "./chat-compaction";@@ -654,6 +655,7 @@ export async function compactSession(return ok;}+/** Summarize and replace a session prefix when doing so strictly reduces context use. */async function tryCompactSession(opts: {session: ChatSession;splitIndex: number;@@ -700,6 +702,7 @@ async function tryCompactSession(opts: {summary,new Date().toISOString(),);+ if (!compactionReducesHistory(session.messages, compacted)) return null;const inserted = compacted[0];session.messages = compacted;return {