Skip to content

Open-source Grok Bot alternative with a virtual machine that bots can use

feat(chat): mark when the engine condenses the chat into its own summary

AdityaUmalea5cf727f6676
diff --git a/server/delta-context.test.ts b/server/delta-context.test.ts
index 78ef640479..1b3c553d51 100644
--- a/server/delta-context.test.ts
+++ b/server/delta-context.test.ts
@@ -477,6 +477,8 @@ describe("isContextMessage", () => {
expect(isContextMessage({ kind: "text", text: "", attachments: [{ kind: "audio", path: "/attachments/n.mp3", mime: "audio/mpeg" }] })).toBe(false);
expect(isContextMessage({ kind: "text", text: "", webhookPost: true, attachments: [{ kind: "audio", path: "/attachments/n.mp3", mime: "audio/mpeg", text: "Spoken words" }] })).toBe(false);
expect(isContextMessage({ kind: "compaction" })).toBe(true);
+ // The engine condensed its own session: a marker for people, with nothing for a provider to read.
+ expect(isContextMessage({ kind: "compaction", engineCompaction: true })).toBe(false);
expect(isContextMessage({ kind: "text", roomRequest: { id: "r1", phase: "result" } })).toBe(true);
expect(isContextMessage({ kind: "activity", text: "Bash" })).toBe(false);
expect(isContextMessage({ kind: "call", text: "Call with Ada · 0:42" })).toBe(false);
diff --git a/server/delta-context.ts b/server/delta-context.ts
index 5be29fa418..248d88a1f6 100644
--- a/server/delta-context.ts
+++ b/server/delta-context.ts
@@ -28,9 +28,10 @@ export function contextText(m: Pick<Message, "text" | "attachments" | "peerPost"
return spoken.length ? [m.text?.trim(), ...spoken].filter(Boolean).join("\n\n") : m.text ?? "";
}
-/** A Live call's row (kind "call") is a record for people, never context. */
-export function isContextMessage(m: Pick<Message, "kind" | "text" | "digest" | "roomRequest" | "attachments" | "peerPost" | "webhookPost">): boolean {
- return Boolean(((m.kind === "text" || m.kind === "widget" || m.kind === "app") && contextText(m)) || (m.kind === "digest" && m.digest) || m.kind === "compaction" || m.roomRequest?.phase === "result");
+/** A Live call's row (kind "call") and an engine's own compaction marker (kind "compaction", engineCompaction) are
+ * records for people, never context. */
+export function isContextMessage(m: Pick<Message, "kind" | "text" | "digest" | "engineCompaction" | "roomRequest" | "attachments" | "peerPost" | "webhookPost">): boolean {
+ return Boolean(((m.kind === "text" || m.kind === "widget" || m.kind === "app") && contextText(m)) || (m.kind === "digest" && m.digest) || (m.kind === "compaction" && !m.engineCompaction) || m.roomRequest?.phase === "result");
}
export interface HandedState {
diff --git a/server/drivers/claude.ts b/server/drivers/claude.ts
index b808591eb5..15b1cde835 100644
--- a/server/drivers/claude.ts
+++ b/server/drivers/claude.ts
@@ -1485,6 +1485,7 @@ export const ClaudeDriver: ProviderDriver<ClaudeConfig> = {
retry.rebuilt = false;
} else if (o.subtype === "compact_boundary") {
session.volatile = null; // only the system prompt survives a compaction: the next turn re-sends the volatile half
+ if (!o.parent_tool_use_id) emit({ ...base(threadId, currentTurnId()), type: "context.compacted" }); // a subagent's own compaction is not the chat's
} else if (o.subtype === "thinking_tokens") {
emit({ ...base(threadId, currentTurnId()), type: "item.updated", itemType: "reasoning", tokens: o.estimated_tokens });
}
diff --git a/server/drivers/pi.test.ts b/server/drivers/pi.test.ts
index 8ed4113353..13ddc42eb8 100644
--- a/server/drivers/pi.test.ts
+++ b/server/drivers/pi.test.ts
@@ -538,6 +538,8 @@ describe("PiDriver turns (fake CLI)", () => {
expect(first.message).toBe("Standing rules.\n\nMemory: likes quiet hours.\n\nfirst");
const second = await send("second", first.cursor);
expect(second.message).toBe("Standing rules.\n\nMemory: likes quiet hours.\n\nsecond");
+ // Each turn condensed the session once; the chat marks each one.
+ expect(recorder.events.filter((e) => e.type === "context.compacted" && e.threadId === threadId)).toHaveLength(2);
});
it("keeps pi alive through post-run compaction recovery and re-establishes the full prompt", async () => {
diff --git a/server/drivers/pi.ts b/server/drivers/pi.ts
index 94417bfda9..e955843d89 100644
--- a/server/drivers/pi.ts
+++ b/server/drivers/pi.ts
@@ -730,11 +730,11 @@ export const PiDriver: ProviderDriver<PiConfig> = {
case "auto_compaction_end": {
if (agentEndTimer) clearTimeout(agentEndTimer);
agentEndTimer = null;
- // pi compaction (manual or automatic) can absorb the turn that carried the standing prompt.
- if (sessionReady && sessionFile) {
+ if (sessionReady && sessionFile) { // pi compaction (manual or automatic) can absorb the turn that carried the standing prompt
compactionObserved = true;
deletePromptSplitReceipt("pi", JSON.stringify([threadId, sessionFile]));
}
+ if (evt.type.endsWith("compaction_end") && (evt as { aborted?: boolean }).aborted !== true) emit({ ...base(threadId, turnId), type: "context.compacted" });
return;
}
case "message_end":
diff --git a/server/hooks.e2e.test.ts b/server/hooks.e2e.test.ts
index f0b7e5ae5e..ebdb8c266a 100644
--- a/server/hooks.e2e.test.ts
+++ b/server/hooks.e2e.test.ts
@@ -20,6 +20,8 @@ interface Msg {
text?: string;
tool?: { name: string; itemId?: string; fullResult?: boolean; output?: string };
digest?: { hookCoverage: string; tools: Array<{ name: string; count: number }> };
+ compaction?: unknown;
+ engineCompaction?: true;
}
const BIG = `line ${"x".repeat(80)}\n`.repeat(200); // ~17 KB: past the 6 KB preview
@@ -157,6 +159,11 @@ posixOnly("engine hooks e2e: compaction", () => {
"context compaction started (auto)",
"context compacted — re-sent the last 1 digest",
]);
+ // Those rows hide with tool calls; the chat also gets one marker people always see, with nothing for the engine.
+ const markers: Msg[] = after.messages.filter((m: Msg) => m.kind === "compaction");
+ expect(markers).toHaveLength(1);
+ expect(markers[0]).toMatchObject({ role: "bot", engineCompaction: true });
+ expect(markers[0]!.compaction).toBeUndefined();
// The fake echoes what SessionStart's stdout gave it: the first turn's digest line.
const replies: Msg[] = after.messages.filter((m: Msg) => m.role === "bot" && m.kind === "text" && m.text);
expect(replies.at(-1)!.text).toContain("[What ");
diff --git a/server/index.ts b/server/index.ts
index d31c4871cc..da731e32eb 100644
--- a/server/index.ts
+++ b/server/index.ts
@@ -6684,6 +6684,7 @@ bus.subscribe((event: RuntimeEvent) => {
tool: { name: `retrying — attempt ${event.attempt + 1}/${RETRY_MAX_ATTEMPTS} in ${Math.round(event.delayMs / 1000)}s — ${event.reason}`, ok: true },
});
break;
+ case "context.compacted": pushMessage({ role: "bot", kind: "compaction", engineCompaction: true }); break;
case "runtime.notice":
pushMessage({ role: "bot", kind: "activity", tool: { ...runtimeNoticeTool(event), name: `notice: ${event.message.slice(0, 400)}` } });
break;
diff --git a/server/thread-events.ts b/server/thread-events.ts
index 5e82b4855c..9b4343653a 100644
--- a/server/thread-events.ts
+++ b/server/thread-events.ts
@@ -217,6 +217,8 @@ export function isRuntimeEvent(value: unknown): value is RuntimeEvent {
return typeof value.input === "number" && typeof value.output === "number";
case "runtime.notice":
return typeof value.message === "string";
+ case "context.compacted":
+ return true;
case "runtime.error":
return typeof value.message === "string" &&
(value.setup === undefined || typeof value.setup === "boolean") &&
diff --git a/shared/runtime-events.ts b/shared/runtime-events.ts
index 02cf380f23..3e860dcbe4 100644
--- a/shared/runtime-events.ts
+++ b/shared/runtime-events.ts
@@ -204,6 +204,8 @@ export type RuntimeEvent = RuntimeEventBase &
/** Something the person should know that did not fail the turn — for
* example, a saved model the engine no longer offers was replaced. */
| { type: "runtime.notice"; message: string; text?: ServerText }
+ /** The engine condensed the conversation into its own summary (Claude Code's compact_boundary); the chat marks it. */
+ | { type: "context.compacted" }
);
export type RuntimeEventListener = (event: RuntimeEvent) => void;
diff --git a/shared/wire.ts b/shared/wire.ts
index bffaf851be..116808bd77 100644
--- a/shared/wire.ts
+++ b/shared/wire.ts
@@ -458,13 +458,9 @@ export interface WireMessage extends ServerTextLine, HelperIntroField, SetupCard
kind: "text" | "options" | "activity" | "screen" | "connector" | "secret" | "signin" | "routine.run" | "goal.run" | "digest" | "compaction" | "call" | "widget" | "app";
text?: string;
digest?: TurnDigest;
- compaction?: {
- summary: string;
- firstKeptId: string;
- foldedThroughId: string;
- tokensBefore: number;
- by: "person" | "harness";
- };
+ /** kind "compaction" with no `compaction` record: the engine condensed its own session. A marker for people, never context. */
+ engineCompaction?: true;
+ compaction?: { summary: string; firstKeptId: string; foldedThroughId: string; tokensBefore: number; by: "person" | "harness" };
/** Renderers receive only the allowlisted /api/attachments URL. `file` entries are opened through the message-scoped
* file route, never by path. `source`: a bot attach-file copy's original path on the server, for Show in folder.
* `fromTool`: an image a tool's result returned while the bot worked, which it did not make or send. */
diff --git a/src/components/DigestChip.tsx b/src/components/DigestChip.tsx
index 368030fe92..92a1a2d163 100644
--- a/src/components/DigestChip.tsx
+++ b/src/components/DigestChip.tsx
@@ -45,11 +45,18 @@ export function DigestChip({ message }: { message: Message }) {
);
}
+const COMPACTION_CHIP = "max-w-[600px] rounded-xl border border-hairline/40 bg-panel px-3 py-2 text-[12px] text-ink-secondary";
+
export function CompactionChip({ message }: { message: Message }) {
- if (!message.compaction) return null;
+ if (!message.compaction) return message.engineCompaction ? (
+ <div className={COMPACTION_CHIP} data-testid="engine-compaction-chip">
+ <p className="font-medium">{t("chat.engineCompactionTitle")}</p>
+ <p className="mt-1">{t("chat.engineCompactionHint")}</p>
+ </div>
+ ) : null;
const excerpts = message.compaction.summary.startsWith(EXCERPT_SUMMARY_HEADER);
return (
- <details className="max-w-[600px] rounded-xl border border-hairline/40 bg-panel px-3 py-2 text-[12px] text-ink-secondary" data-testid="compaction-chip">
+ <details className={COMPACTION_CHIP} data-testid="compaction-chip">
<summary className="cursor-pointer font-medium">{t(excerpts ? "chat.compactionExcerptTitle" : "chat.compactionTitle")}</summary>
<p className="mt-2">{t(excerpts ? "chat.compactionExcerptHint" : "chat.compactionHint")}</p>
<p className="mt-2 whitespace-pre-wrap break-words">{message.compaction.summary}</p>
diff --git a/src/locales/en.json b/src/locales/en.json
index 7b837a6bae..723e2fcea9 100644
--- a/src/locales/en.json
+++ b/src/locales/en.json
@@ -2580,6 +2580,8 @@
"chat.digestChipNoFiles": "Did {tools} tool calls",
"chat.digestTitle": "What this turn did",
"chat.compactionTitle": "Earlier context summarized",
+ "chat.engineCompactionTitle": "Chat condensed into a summary",
+ "chat.engineCompactionHint": "The AI summarized earlier messages to make room. Your full conversation is still here, but the agent may not recall older details exactly.",
"chat.compactionHint": "Your full conversation is still here. The agent uses this summary and recent messages when continuing.",
"chat.compactionExcerptTitle": "Earlier context shortened to recent excerpts",
"chat.compactionExcerptHint": "A written summary wasn't available, so the agent continues from these excerpts and recent messages. Older details may be missing from its context. Your full conversation is still here.",
diff --git a/src/state/store.tsx b/src/state/store.tsx
index 869288741b..ad9d2dc874 100644
--- a/src/state/store.tsx
+++ b/src/state/store.tsx
@@ -117,7 +117,7 @@ export type SecretRequestCardData = Omit<import("../../shared/wire").SecretReque
export type ComputerControlState = { held: boolean; helpReason: string | null; help?: import("../../shared/wire").ComputerHelpRef | null };
-export interface Message extends Pick<import("../../shared/wire").WireMessage, "widget" | "app" | "fromApp" | "pointed" | "routedBy" | "quickModel" | "automation" | "publishedPage" | "sender" | "setupSuggestion" | "helperIntro" | "helperFollowup" | "setupOffer" | "savedSteps" | "setupChoices" | "setupChoicesPending" | "setupChoicesUpdating" | "setupRequest" | "relay" | "inbox" | "sentDirectly" | "doneWhen" | "bugReport"> {
+export interface Message extends Pick<import("../../shared/wire").WireMessage, "widget" | "app" | "fromApp" | "pointed" | "routedBy" | "quickModel" | "automation" | "publishedPage" | "sender" | "setupSuggestion" | "helperIntro" | "helperFollowup" | "setupOffer" | "savedSteps" | "setupChoices" | "setupChoicesPending" | "setupChoicesUpdating" | "setupRequest" | "relay" | "inbox" | "sentDirectly" | "doneWhen" | "bugReport" | "engineCompaction"> {
id: string;
role: "bot" | "user";
kind: "text" | "options" | "activity" | "screen" | "connector" | "secret" | "signin" | "routine.run" | "goal.run" | "digest" | "compaction" | "call" | "widget" | "app";