Skip to content

A desktop coding agent with free access to frontier models.

TypeScript

fix: let Esc end automatic compaction

Samrat Talukdar3eecfee774d8
diff --git a/apps/server/src/auto-compact-engine.ts b/apps/server/src/auto-compact-engine.ts
index dd27ae64..f5cb3770 100644
--- a/apps/server/src/auto-compact-engine.ts
+++ b/apps/server/src/auto-compact-engine.ts
@@ -41,6 +41,23 @@ export function desktopAutoCompactEngine(target: EngineTarget, sessionId: string
const text = await response.text().catch(() => "");
if (!response.ok) throw new Error(`summarize answered ${response.status}${text ? `: ${text.slice(0, 200)}` : ""}`);
},
+ async removeMessage(messageID: string) {
+ const response = await fetchImpl(buildOpencodeProxyUrl(target.baseUrl, `/session/${encodeURIComponent(sessionId)}/message/${encodeURIComponent(messageID)}`, target.search), {
+ method: "DELETE",
+ headers: new Headers(target.headers),
+ signal: AbortSignal.timeout(10_000),
+ });
+ await response.body?.cancel().catch(() => undefined);
+ if (!response.ok && response.status !== 404) throw new Error(`message delete answered ${response.status}`);
+ },
+ async abort() {
+ const response = await fetchImpl(buildOpencodeProxyUrl(target.baseUrl, `/session/${encodeURIComponent(sessionId)}/abort`, target.search), {
+ method: "POST",
+ headers: new Headers(target.headers),
+ signal: AbortSignal.timeout(10_000),
+ });
+ await response.body?.cancel().catch(() => undefined);
+ },
};
}
diff --git a/apps/server/src/server.ts b/apps/server/src/server.ts
index 7f90ea66..de1ce92c 100644
--- a/apps/server/src/server.ts
+++ b/apps/server/src/server.ts
@@ -2251,6 +2251,11 @@ export async function proxyOpencodeRequest(input: {
// and end snapshot behind.
const captured = Boolean(capture?.uploadEnabled && uploadedSessionId && workspace && workspace.workspaceType !== "remote"
&& (capture.hasSession(uploadedSessionId) || await engineHasSession(baseUrl, auth, directory, uploadedSessionId)));
+ // Stop while the chat compacts: the compaction's leftovers are removed and the held prompts go out (auto-compact.ts).
+ const stoppedSessionId = method === "POST" ? normalizeOpencodeProxyPath(proxyPath).match(/^\/session\/([^/]+)\/abort$/)?.[1] ?? null : null;
+ if (stoppedSessionId && autoCompactorsByServer.get(input.config)?.stopped(stoppedSessionId)) {
+ createServerLogger(input.config).log("info", "Automatic compaction stopped by the person.", {});
+ }
// Automatic compaction (session-archive/auto-compact.ts): a chat with the server's number of prompts
// since its last compaction is compacted before this prompt (never mid-turn, never a sub-agent's chat).
const autoCompactor = workspace && workspace.workspaceType !== "remote" && uploadedSessionId && isUploadPromptDispatch(method, proxyPath)
diff --git a/apps/server/src/session-archive/PARITY.sha256 b/apps/server/src/session-archive/PARITY.sha256
index 5472f171..7ac92929 100644
--- a/apps/server/src/session-archive/PARITY.sha256
+++ b/apps/server/src/session-archive/PARITY.sha256
@@ -1,6 +1,6 @@
# Session-archive modules that are the same files in omnirush-cli (assets/extensions/omnirush/capture/session-archive/)
# and omnirush-gui (apps/server/src/session-archive/). A change to one must be ported to the other and this list updated in both.
-4291c19235d4027c441b2787dcec31ed01fa3067ea6d33fbdc8177eab279684a auto-compact.ts
+5e4f36aa539098e179edcadae2284a7698ed4efb9cd3a91e69f9000c0d839353 auto-compact.ts
8fd84683b5542236f0c88750e3d9bd756e15a304d263141b57998b17c4a1ab9c late-writes.ts
4d9a13a5df136cd6e13d0aeb67983249ca4a6913b66c5a8f768daa63da511dda tool-start.ts
38dc65a461de6905134c22837f5230d03383ec0bdd6df35090dd269160446edf turn-overlap.ts
diff --git a/apps/server/src/session-archive/auto-compact.test.ts b/apps/server/src/session-archive/auto-compact.test.ts
index e0b7f47e..3a8e9b98 100644
--- a/apps/server/src/session-archive/auto-compact.test.ts
+++ b/apps/server/src/session-archive/auto-compact.test.ts
@@ -5,7 +5,7 @@ import { describe, expect, test } from "bun:test";
import { brokerAutoCompactPolicy, desktopAutoCompactEngine } from "../auto-compact-engine.js";
import { compactionSummary, workingOn } from "../turn-steer.js";
-import { AutoCompactor, autoCompactPolicy, RETRY_AFTER_TURNS, turnsSinceCompaction, type AutoCompactEngine, type ModelRef } from "./auto-compact.js";
+import { AutoCompactor, autoCompactPolicy, compactionResult, RETRY_AFTER_TURNS, turnsSinceCompaction, type AutoCompactEngine, type ModelRef } from "./auto-compact.js";
import { completedTurnCount, steerPrompts } from "./turn-prompts.js";
const MODEL: ModelRef = { providerID: "omnirush", modelID: "astra" };
@@ -47,7 +47,8 @@ describe("auto compaction", () => {
const outcome = await compactor.beforePrompt("s", engine, new TextEncoder().encode(JSON.stringify({ parts: [], model: { providerID: "omnirush", modelID: "sol" } })).buffer);
expect(outcome.action).toBe("compacted");
expect(calls.summarize).toEqual([{ providerID: "omnirush", modelID: "sol" }]);
- expect(calls.messages).toBe(1);
+ // The seed (also the check before compacting), then how it ended.
+ expect(calls.messages).toBe(2);
expect(compactor.count("s")).toBe(1);
});
@@ -66,6 +67,31 @@ describe("auto compaction", () => {
expect(retry.count("s")).toBe(Math.max(0, 3 - RETRY_AFTER_TURNS) + 1);
expect(busy.calls.summarize.length + child.calls.summarize.length).toBe(0);
});
+
+ test("a stopped compaction: its request and broken summary are removed, the held prompt goes, no quick retry", async () => {
+ // Left in the chat, the engine would run the stopped compaction again with the next prompt.
+ const aborted = { info: { id: "s1", parentID: "c1", role: "assistant", summary: true, mode: "compaction", agent: "compaction", time: { created: 401, completed: 402 }, error: { name: "MessageAbortedError" } }, parts: [] };
+ expect(compactionResult([...chat(3), compactRequest("c1", 400), aborted], new Set())).toEqual({ state: "broken", error: "MessageAbortedError", leftovers: ["s1", "c1"] });
+ let list: unknown[] = chat(3);
+ let release = () => {};
+ const removed: string[] = [];
+ const engine: AutoCompactEngine = {
+ messages: async () => list,
+ session: async () => ({ id: "ses" }),
+ busy: async () => false,
+ summarize: () => new Promise<void>((resolve) => { release = resolve; }),
+ removeMessage: async (id) => { removed.push(id); },
+ };
+ const compactor = new AutoCompactor({ policy: async () => ({ enabled: true, afterTurns: 3, minContextTokens: 0 }) });
+ const held = compactor.beforePrompt("s", engine, { model: MODEL });
+ await new Promise((resolve) => setTimeout(resolve, 5));
+ expect(compactor.stopped("s")).toBe(true);
+ list = [...chat(3), compactRequest("c1", 400), aborted];
+ release();
+ expect((await held).action).toBe("failed");
+ expect(removed).toEqual(["s1", "c1"]);
+ expect(compactor.count("s")).toBe(1);
+ });
});
describe("the desktop's glue", () => {
@@ -96,5 +122,11 @@ describe("the desktop's glue", () => {
await engine.summarize(MODEL, new AbortController().signal);
expect(seen[0]).toEqual({ url: "http://127.0.0.1:4096/session/status?directory=%2Fw", method: "GET", auth: "Basic x" });
expect(seen[1]).toEqual({ url: "http://127.0.0.1:4096/session/ses_1/summarize?directory=%2Fw", method: "POST", auth: "Basic x", body: JSON.stringify(MODEL) });
+ await engine.removeMessage!("msg_1");
+ await engine.abort!();
+ expect(seen.slice(2).map(({ url, method }) => `${method} ${url}`)).toEqual([
+ "DELETE http://127.0.0.1:4096/session/ses_1/message/msg_1?directory=%2Fw",
+ "POST http://127.0.0.1:4096/session/ses_1/abort?directory=%2Fw",
+ ]);
});
});
diff --git a/apps/server/src/session-archive/auto-compact.ts b/apps/server/src/session-archive/auto-compact.ts
index c56e18b9..9db24e80 100644
--- a/apps/server/src/session-archive/auto-compact.ts
+++ b/apps/server/src/session-archive/auto-compact.ts
@@ -19,6 +19,13 @@
* caller asks only for a prompt that opens a turn (not a steer). Sub-agent
* chats are never compacted on their own. A failed compaction is tried
* again RETRY_AFTER_TURNS prompts later; the prompt goes out regardless.
+ * One the person stopped (Esc) is tried again only after a full
+ * `after_turns`.
+ *
+ * A compaction that ended without a summary (stopped, failed, timed out)
+ * leaves its request in the chat, and the engine would run that request
+ * again with the next prompt, answering the prompt with a summary instead.
+ * Its request and broken summary are removed before the prompt goes out.
*
* The prompt waits for the compaction at most `holdMs`; past that it goes
* out and the engine runs it right after the compaction.
@@ -31,8 +38,10 @@ export const AUTO_COMPACT_FAILED_NOTICE = "Could not compact the conversation. C
export const RETRY_AFTER_TURNS = 5;
/** How long the first prompt of a chat waits for the read that starts its count. */
export const SEED_WAIT_MS = 10_000;
-/** The longest a compaction may run before it counts as failed. */
+/** The longest a compaction may run before it counts as failed (and the engine is told to stop it). */
export const COMPACTION_TIMEOUT_MS = 10 * 60_000;
+/** After the person stops a compaction, how long the engine has to end it before the wait for it is given up. */
+export const STOP_GRACE_MS = 5_000;
export type AutoCompactPolicy = { enabled: boolean; afterTurns: number; minContextTokens: number };
export type ModelRef = { providerID: string; modelID: string };
@@ -47,6 +56,10 @@ export type AutoCompactEngine = {
busy(): Promise<boolean | null>;
/** Runs the engine's compaction on `model`; resolves once it finished, rejects when it failed. */
summarize(model: ModelRef, signal: AbortSignal): Promise<void>;
+ /** Removes one message from the chat (a compaction's leftover request or broken summary). */
+ removeMessage?(messageID: string): Promise<void>;
+ /** Stops what the engine runs for the chat (a compaction past its time). */
+ abort?(): Promise<void>;
};
export type AutoCompactStage = "started" | "finished" | "failed";
@@ -60,6 +73,7 @@ export type AutoCompactorOptions = {
holdMs?: number;
seedWaitMs?: number;
timeoutMs?: number;
+ stopGraceMs?: number;
};
export type AutoCompactOutcome = {
@@ -194,12 +208,64 @@ function errorText(error: unknown): string {
return error instanceof Error ? error.message : String(error);
}
+function idOf(message: unknown): string | null {
+ if (!isRecord(message)) return null;
+ const id = infoOf(message).id;
+ return typeof id === "string" && id ? id : null;
+}
+
+/** The ids of the chat's compaction summaries. */
+function summaryIds(messages: unknown): Set<string> {
+ const ids = new Set<string>();
+ if (Array.isArray(messages)) for (const message of messages) if (isCompactionSummary(message)) { const id = idOf(message); if (id) ids.add(id); }
+ return ids;
+}
+
+export type CompactionResult = {
+ /** "summarized": a summary without an error; "broken": one with an error (stopped, failed); "none": no new summary. */
+ state: "summarized" | "broken" | "none";
+ /** The engine's error name on a broken summary (MessageAbortedError: the person stopped it). */
+ error?: string;
+ /** What to remove for a broken one: the summary and its compaction request. */
+ leftovers: string[];
+};
+
+/**
+ * How the compaction that just ran ended, from the chat's messages read
+ * after it: its summary is the newest one not in `before` (the ids seen
+ * before it started).
+ */
+export function compactionResult(messages: unknown, before: Set<string>): CompactionResult {
+ if (!Array.isArray(messages)) return { state: "none", leftovers: [] };
+ for (let index = messages.length - 1; index >= 0; index -= 1) {
+ const message = messages[index];
+ if (!isCompactionSummary(message)) continue;
+ const id = idOf(message);
+ if (!id || before.has(id)) return { state: "none", leftovers: [] };
+ const info = infoOf(message as Json);
+ if (info.error == null) return { state: "summarized", leftovers: [] };
+ const error = isRecord(info.error) && typeof info.error.name === "string" ? info.error.name : "error";
+ const leftovers = [id];
+ const parentID = typeof info.parentID === "string" ? info.parentID : null;
+ const request = parentID ? messages.find((entry) => idOf(entry) === parentID) : undefined;
+ // Only a request that is nothing but the compaction (never a prompt someone sent).
+ if (parentID && isRecord(request) && infoOf(request).role === "user") {
+ const parts = Array.isArray(request.parts) ? request.parts.filter(isRecord) : [];
+ if (parts.length > 0 && parts.every((part) => part.type === "compaction")) leftovers.push(parentID);
+ }
+ return { state: "broken", error, leftovers };
+ }
+ return { state: "none", leftovers: [] };
+}
+
export class AutoCompactor {
/** Prompts sent per chat since its last compaction (an upper bound, module docstring). */
private readonly counts = new Map<string, number>();
private readonly seeding = new Map<string, Promise<unknown>>();
private readonly children = new Set<string>();
private readonly running = new Map<string, Promise<boolean>>();
+ /** The running compactions' stop handles (the person stopped the chat: give the engine STOP_GRACE_MS, then let go). */
+ private readonly stops = new Map<string, () => void>();
constructor(private readonly options: AutoCompactorOptions) {}
@@ -213,6 +279,19 @@ export class AutoCompactor {
if (!this.running.has(sessionId)) this.counts.set(sessionId, 0);
}
+ /**
+ * The person stopped the chat (Esc) while it compacts: the engine ends the
+ * compaction, its leftovers are removed, and the held prompts go out. If
+ * the engine has not answered within STOP_GRACE_MS, the wait ends anyway.
+ * Returns whether a compaction was running.
+ */
+ stopped(sessionId: string): boolean {
+ const stop = this.stops.get(sessionId);
+ if (!stop) return false;
+ stop();
+ return true;
+ }
+
forget(sessionId: string): void {
this.counts.delete(sessionId);
this.children.delete(sessionId);
@@ -252,6 +331,16 @@ export class AutoCompactor {
return run;
}
+ /** Removes a broken compaction's summary and request; resolves how many went. */
+ private async removeLeftovers(engine: AutoCompactEngine, ids: string[]): Promise<number> {
+ if (!engine.removeMessage || ids.length === 0) return 0;
+ let removed = 0;
+ for (const id of ids) {
+ try { await engine.removeMessage(id); removed += 1; } catch (error) { this.log("warn", "auto compaction leftover not removed", { messageID: id, error: errorText(error) }); }
+ }
+ return removed;
+ }
+
private add(sessionId: string, base?: number): void {
this.counts.set(sessionId, (base ?? this.counts.get(sessionId) ?? 0) + 1);
}
@@ -330,24 +419,51 @@ export class AutoCompactor {
this.notify(sessionId, "started");
this.log("info", "auto compaction started", { sessionId, turns, context_tokens: context, after_turns: current.afterTurns });
const started = Date.now();
+ const before = summaryIds(messages);
const controller = new AbortController();
+ let stoppedByPerson = false;
+ let timedOut = false;
const timeoutMs = this.options.timeoutMs ?? COMPACTION_TIMEOUT_MS;
- const timer = setTimeout(() => controller.abort(new Error("compaction timed out")), timeoutMs);
+ const timer = setTimeout(() => {
+ timedOut = true;
+ // The engine goes on compacting after our wait ends: tell it to stop, so its leftovers can be removed.
+ void (engine.abort?.() ?? Promise.resolve()).catch(() => undefined).finally(() => controller.abort(new Error("compaction timed out")));
+ }, timeoutMs);
(timer as { unref?: () => void }).unref?.();
+ let grace: ReturnType<typeof setTimeout> | undefined;
+ this.stops.set(sessionId, () => {
+ stoppedByPerson = true;
+ grace ??= setTimeout(() => controller.abort(new Error("compaction stopped")), this.options.stopGraceMs ?? STOP_GRACE_MS);
+ (grace as { unref?: () => void }).unref?.();
+ });
const run: Promise<boolean> = engine.summarize(model, controller.signal).then(
- () => {
+ () => null,
+ (error: unknown) => error ?? new Error("summarize failed"),
+ ).then(async (failure) => {
+ // Summarize answers once the engine is done, also when it was stopped: the chat says how it ended.
+ const result = compactionResult(await engine.messages().catch(() => null), before);
+ if (failure === null && result.state !== "broken") {
this.log("info", "auto compaction finished", { sessionId, elapsed_ms: Date.now() - started });
this.notify(sessionId, "finished");
return true;
- },
- (error: unknown) => {
- this.log("warn", "auto compaction failed", { sessionId, elapsed_ms: Date.now() - started, error: errorText(error) });
- this.later(sessionId, current);
- this.notify(sessionId, "failed");
- return false;
- },
- ).finally(() => {
+ }
+ const removed = await this.removeLeftovers(engine, result.leftovers);
+ const byPerson = stoppedByPerson || result.error === "MessageAbortedError";
+ this.log("warn", byPerson ? "auto compaction stopped" : "auto compaction failed", {
+ sessionId,
+ elapsed_ms: Date.now() - started,
+ error: failure === null ? result.error ?? null : errorText(failure),
+ timed_out: timedOut,
+ removed,
+ });
+ // Stopped by the person: the count started again at this prompt (a full after_turns); otherwise RETRY_AFTER_TURNS.
+ if (!byPerson) this.later(sessionId, current);
+ this.notify(sessionId, "failed");
+ return false;
+ }).finally(() => {
clearTimeout(timer);
+ clearTimeout(grace);
+ this.stops.delete(sessionId);
this.running.delete(sessionId);
});
this.running.set(sessionId, run);