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
28 changes: 22 additions & 6 deletions electron/ai-edition/chat-compaction.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
applyCompaction,
budgetSnapshot,
buildCompactionPrompt,
compactionReducesHistory,
estimateHistoryTokens,
shouldCompact,
} from "./chat-compaction";
Expand Down Expand Up @@ -78,18 +79,33 @@ describe("shouldCompact", () => {
});

describe("applyCompaction", () => {
it("inserts the summary message at the split point", () => {
it("replaces the summarized prefix with the summary message", () => {
const msgs = [
msg("user", "1", "u1"),
msg("assistant", "2", "a1"),
msg("user", "x".repeat(4_000), "u1"),
msg("assistant", "y".repeat(4_000), "a1"),
msg("user", "3", "u2"),
msg("assistant", "4", "a2"),
];
const out = applyCompaction(msgs, 2, "summary text", "2026-02-01T00:00:00.000Z");
expect(out.length).toBe(5);
expect(out[2]?.content).toBe("summary text");
expect(out[0]?.id).toBe("u1");
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);
});
});

Expand Down
11 changes: 9 additions & 2 deletions electron/ai-edition/chat-compaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,15 +83,22 @@ export function applyCompaction(
summary: string,
summaryAt: string,
): AiEditionChatMessage[] {
const head = messages.slice(0, splitIndex);
const tail = messages.slice(splitIndex);
const summaryMessage: AiEditionChatMessage = {
id: `summary_${Date.now()}`,
role: "assistant",
content: summary,
createdAt: summaryAt,
};
return [...head, summaryMessage, ...tail];
return [summaryMessage, ...tail];
Comment thread
EtienneLescot marked this conversation as resolved.
}

/** True only when a proposed compaction strictly reduces estimated context use. */
export function compactionReducesHistory(
original: AiEditionChatMessage[],
compacted: AiEditionChatMessage[],
): boolean {
return estimateHistoryTokens(compacted) < estimateHistoryTokens(original);
Comment thread
EtienneLescot marked this conversation as resolved.
}

/** System-prompt addendum to ask the LLM to compact its own history. */
Expand Down
169 changes: 169 additions & 0 deletions electron/ai-edition/chat-service.compaction.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,169 @@
// Compaction as seen from chat-service: what the user keeps versus what the
// model is handed. Both seams are mocked — `invokeOpenScreenAgent` for the
// turn itself (so we can read the history it was given) and the chat model
// behind the summarizer, so no test here needs a provider or a key.

import { beforeEach, describe, expect, it, vi } from "vitest";

vi.mock("./deep-agent/service", () => ({
invokeOpenScreenAgent: vi.fn(),
}));

vi.mock("./deep-agent/chat-model", () => ({
createOpenScreenChatModel: vi.fn(),
messageContentToText: (content: unknown) => String(content),
}));

import {
compactSessionNow,
createSession,
getSessionContextUsage,
runChat,
selectSession,
} from "./chat-service";
import { createOpenScreenChatModel } from "./deep-agent/chat-model";
import { invokeOpenScreenAgent } from "./deep-agent/service";
import type { LlmConfigStore } from "./llm-config-store";

const invokeMock = vi.mocked(invokeOpenScreenAgent);
const chatModelMock = vi.mocked(createOpenScreenChatModel);

type ModelHistory = Array<{ role: "user" | "assistant" | "system"; content: string }>;

let histories: ModelHistory[] = [];

function stubConfig(): LlmConfigStore {
return {
getConfig: () => ({ provider: "openai", model: "gpt-4o" }),
getApiKey: () => "sk-test",
getCredential: () => ({ value: "sk-test", entry: { kind: "api-key", apiKey: "sk-test" } }),
} as unknown as LlmConfigStore;
}

/** Point the summarizer at a fixed reply and return its call spy. */
function stubSummarizer(reply: string) {
const invoke = vi.fn(async () => ({ content: reply }));
chatModelMock.mockImplementation(
async () => ({ invoke }) as unknown as Awaited<ReturnType<typeof createOpenScreenChatModel>>,
);
return invoke;
}

// Long enough that four of them clear the 70%-of-80k-tokens trip point, short
// enough that three of them do not.
const LONG = "x".repeat(60_000);

beforeEach(() => {
histories = [];
invokeMock.mockReset();
chatModelMock.mockReset();
invokeMock.mockImplementation(async (args) => {
histories.push([...args.history]);
return { text: "ok", document: args.document, mutated: false };
});
});

describe("auto-compaction", () => {
it("leaves the transcript whole and compacts only what the model is given", async () => {
stubSummarizer("EARLIER CONTEXT");
const session = createSession("proj_compact_transcript");
for (let i = 0; i < 4; i += 1) {
await runChat("proj_compact_transcript", session.id, `${LONG}#${i}`, stubConfig());
}

// Four user turns, four replies, nothing deleted: this array is what the
// renderer shows, and the user never asked for half of it to go away.
const transcript = selectSession("proj_compact_transcript", session.id)?.messages ?? [];
expect(transcript).toHaveLength(8);
expect(transcript[0]?.content).toBe(`${LONG}#0`);
expect(transcript.filter((m) => m.role === "user")).toHaveLength(4);

// The fourth turn is the one that tripped the budget: the model got the
// summary in place of the older half, not the whole conversation.
const history = histories.at(-1) ?? [];
expect(history[0]?.content).toBe("EARLIER CONTEXT");
expect(history).toHaveLength(4);
expect(history.some((m) => m.content === `${LONG}#0`)).toBe(false);
expect(history.at(-1)?.content).toBe(`${LONG}#3`);

// The context pill measures the payload, so compaction actually shows up:
// the whole transcript estimates at ~60k tokens, the payload at half.
const usage = getSessionContextUsage("proj_compact_transcript", session.id);
expect(usage?.usedTokens).toBeLessThan(40_000);
});

it("keeps the summary in the payload when the tail is longer than the window", async () => {
stubSummarizer("EARLIER CONTEXT");
const session = createSession("proj_compact_window");
for (let i = 0; i < 30; i += 1) {
await runChat("proj_compact_window", session.id, `turn ${i}`, stubConfig());
}
const huge = LONG.repeat(4);
await runChat("proj_compact_window", session.id, huge, stubConfig());

// 31 messages survive the boundary — a plain slice(-20) would drop the
// summary we just paid a model call to produce.
const history = histories.at(-1) ?? [];
expect(history).toHaveLength(20);
expect(history[0]?.content).toBe("EARLIER CONTEXT");
expect(history.at(-1)?.content).toBe(huge);
});

it("stops retrying after a summary that does not shrink the payload", async () => {
const oversized = stubSummarizer("z".repeat(400_000));
const session = createSession("proj_compact_blocked");
for (let i = 0; i < 5; i += 1) {
await runChat("proj_compact_blocked", session.id, `${LONG}#${i}`, stubConfig());
}

// Two more turns tripped the heuristic after the failure; neither paid
// for another summarizer call.
expect(oversized).toHaveBeenCalledTimes(1);
const history = histories.at(-1) ?? [];
expect(history.some((m) => m.content === "EARLIER CONTEXT")).toBe(false);
expect(selectSession("proj_compact_blocked", session.id)?.messages).toHaveLength(10);

// The Compact button is an explicit request, so it tries again — and a
// success unblocks the automatic path.
const usable = stubSummarizer("EARLIER CONTEXT");
const manual = await compactSessionNow("proj_compact_blocked", session.id, stubConfig());
expect(usable).toHaveBeenCalledTimes(1);
expect(manual?.summary).toBe("EARLIER CONTEXT");
expect(manual?.session.messages).toHaveLength(10);

await runChat("proj_compact_blocked", session.id, "and then?", stubConfig());
expect(histories.at(-1)?.[0]?.content).toBe("EARLIER CONTEXT");
});

// The regression this guards is `planCompaction` measuring the wrong list.
// `splitIndex` comes back from `shouldCompact` as an index INTO WHAT IT WAS
// GIVEN, and it is then applied to the payload. Measure the transcript
// instead — which never shrinks, so it keeps tripping — and the index runs
// off the end of the much shorter payload, so `payload.slice(0, splitIndex)`
// swallows the whole thing, current user turn included. The model is then
// asked to answer a question it was never shown.
//
// Three turns is not enough to see it: the collapse needs a payload that has
// already been compacted at least once, so the two lists have diverged.
it("never summarizes away the turn the user just sent", async () => {
stubSummarizer("EARLIER CONTEXT");
const session = createSession("proj_compact_current_turn");
for (let i = 0; i < 10; i += 1) {
await runChat("proj_compact_current_turn", session.id, `${LONG}#${i}`, stubConfig());
}

// Every turn, not just the last: the collapse is intermittent, so a
// spot-check on `histories.at(-1)` walks straight past it. When it bites,
// the payload is `[summary]` alone, so the last entry is the summary
// rather than the message the user just typed — which is exactly what
// this asserts. (Turn 0 is legitimately a one-message payload, so length
// is the wrong thing to check.)
expect(histories).toHaveLength(10);
histories.forEach((history, turn) => {
expect(
history.at(-1)?.content,
`turn ${turn} was handed a payload that did not end with the user's message`,
).toBe(`${LONG}#${turn}`);
});
});
});
Loading
Loading