A desktop coding agent with free access to frontier models.
TypeScript
fix: auto compaction finds the chat model on the new engine
ogsamrat3b65edb166e8
diff --git a/apps/server/src/auto-compact-engine.test.ts b/apps/server/src/auto-compact-engine.test.tsnew file mode 100644index 00000000..495d5ffd--- /dev/null+++ b/apps/server/src/auto-compact-engine.test.ts@@ -0,0 +1,84 @@+// Automatic compaction on the 2.x engine (auto-compact-engine.ts opencodeV2AutoCompactEngine + the shared+// auto-compact.ts): a 2.x prompt body names no model, so the chat's model comes from its stored messages.+import { expect, test } from "bun:test";++import { desktopAutoCompactEngine } from "./auto-compact-engine.js";+import { AutoCompactor } from "./session-archive/auto-compact.js";++const MODEL = { id: "gpt-6-astra", providerID: "omnirush" };++/** A 2.x engine for one chat: its stored messages (GET /api/session/:id/context) and the compact call. */+function v2Engine() {+ const stored: Record<string, unknown>[] = [{ id: "msg_switch", type: "model-switched", time: { created: 1 }, model: MODEL }];+ const compacts: string[] = [];+ let at = 10;+ const turn = (text: string) => {+ stored.push({ id: `msg_u${at}`, type: "user", time: { created: at }, text });+ stored.push({ id: `msg_a${at}`, type: "assistant", time: { created: at + 1, completed: at + 2 }, agent: "build", model: MODEL, finish: "stop", content: [{ type: "text", text: "done" }] });+ at += 10;+ };+ const engineCompacted = () => {+ stored.push({ id: `cmp_engine${at}`, type: "compaction", reason: "auto", status: "completed", time: { created: at, completed: at + 1 } });+ at += 10;+ };+ const fetchImpl = async (input: string | URL | Request, init: RequestInit = {}) => {+ const url = new URL(String(input instanceof Request ? input.url : input));+ const path = url.pathname;+ if (path.endsWith("/api/session/active")) return Response.json({ data: {} });+ if (path.endsWith("/context")) return Response.json({ data: stored });+ if (path.endsWith("/compact") && init.method === "POST") {+ const id = `cmp_${compacts.length + 1}`;+ compacts.push(id);+ stored.push({ id, type: "compaction", reason: "manual", status: "completed", time: { created: at, completed: at + 1 } });+ at += 10;+ return Response.json({ data: { id } });+ }+ if (path.endsWith("/message") && url.searchParams.get("type") === "compaction") return Response.json({ data: stored.filter((item) => item.type === "compaction").reverse() });+ if (/\/api\/session\/ses_v2$/.test(path)) return Response.json({ data: { id: "ses_v2", model: MODEL } });+ throw new Error(`unexpected ${init.method ?? "GET"} ${path}`);+ };+ const engine = desktopAutoCompactEngine({ baseUrl: "http://127.0.0.1:4096", headers: [["authorization", "Basic x"]], search: "?location%5Bdirectory%5D=%2Fw", engine: "v2" }, "ses_v2", fetchImpl as never);+ return { engine, turn, engineCompacted, compacts, fetchImpl };+}++/** The chat's reads go through the server's loopback fetch (session-upload-observer.ts): the same stub serves them. */+async function withFetch<T>(fetchImpl: (input: string | URL | Request, init?: RequestInit) => Promise<Response>, run: () => Promise<T>): Promise<T> {+ const original = globalThis.fetch;+ globalThis.fetch = fetchImpl as typeof fetch;+ try { return await run(); } finally { globalThis.fetch = original; }+}++const policy = async () => ({ enabled: true, afterTurns: 2, minContextTokens: 0 });+/** A 2.x prompt body: text only, its model was set on the session before (POST /api/session/:id/model). */+const body = (text: string) => JSON.stringify({ text });++test("v2: no compaction under the threshold, exactly one past it, on the chat's model", async () => {+ const chat = v2Engine();+ const compactor = new AutoCompactor({ policy, holdMs: 5_000 });+ const actions: string[] = [];+ await withFetch(chat.fetchImpl, async () => {+ for (const [index, text] of ["first", "second", "third", "fourth"].entries()) {+ const outcome = await compactor.beforePrompt("ses_v2", chat.engine, body(text));+ actions.push(outcome.action);+ chat.turn(text);+ if (index === 1) expect(chat.compacts).toEqual([]);+ }+ });+ expect(actions).toEqual(["none", "none", "compacted", "none"]);+ expect(chat.compacts).toEqual(["cmp_1"]);+});++test("v2: a chat the engine compacted itself is not compacted again by us", async () => {+ const chat = v2Engine();+ const compactor = new AutoCompactor({ policy, holdMs: 5_000 });+ const actions: string[] = [];+ await withFetch(chat.fetchImpl, async () => {+ for (const text of ["first", "second", "third"]) {+ if (text === "third") chat.engineCompacted();+ actions.push((await compactor.beforePrompt("ses_v2", chat.engine, body(text))).action);+ chat.turn(text);+ }+ });+ expect(actions).toEqual(["none", "none", "none"]);+ expect(chat.compacts).toEqual([]);+});diff --git a/apps/server/src/engine-v2-view.ts b/apps/server/src/engine-v2-view.tsindex 26989b18..9d9ee46b 100644--- a/apps/server/src/engine-v2-view.ts+++ b/apps/server/src/engine-v2-view.ts@@ -114,7 +114,18 @@ function compactionSummary(message: Rec, sessionID: string): Rec | null {/** A message list for the shared modules: 1.x lists unchanged, 2.x lists as their 1.x view. */export function v1View(messages: readonly unknown[], sessionID = ""): unknown[] {if (!isV2Messages(messages)) return [...messages];- return messages.flatMap((message) => [v1ViewOf(message, sessionID), isRecord(message) ? compactionSummary(message, sessionID) : null]).filter((message): message is Rec => message !== null);+ // A 2.x prompt names no model: the chat's model as of that prompt (its last model switch, else the model+ // of the answer before it) goes on the 1.x user message, as 1.x prompts carried it (auto-compact.ts lastPromptModel).+ let model: { providerID: string; modelID: string } | null = null;+ return messages.flatMap((message) => {+ if (isRecord(message) && isRecord(message.model) && typeof message.model.id === "string" && typeof message.model.providerID === "string"+ && (message.type === "model-switched" || message.type === "assistant")) {+ model = { providerID: message.model.providerID, modelID: message.model.id };+ }+ const view = v1ViewOf(message, sessionID);+ if (view && model && isRecord(view.info) && view.info.role === "user" && isRecord(message) && message.type === "user") view.info = { ...view.info, model };+ return [view, isRecord(message) ? compactionSummary(message, sessionID) : null];+ }).filter((message): message is Rec => message !== null);}/** The sub-agent sessions a 2.x turn started (its `subagent` calls' child sessions). */