Skip to content

A desktop coding agent with free access to frontier models.

TypeScript

Add explicit context compaction control

Task Builder78301576351e
diff --git a/apps/app/src/i18n/locales/en.ts b/apps/app/src/i18n/locales/en.ts
index 55d411bd..7491d675 100644
--- a/apps/app/src/i18n/locales/en.ts
+++ b/apps/app/src/i18n/locales/en.ts
@@ -1369,6 +1369,8 @@ export default {
"session.assistant_empty_response": "The agent responded with nothing. Try again.",
"session.assistant_compacting": "Compacting",
"session.assistant_error": "Error",
+ "session.compact_context": "Compact context",
+ "session.compact_context_running": "Compacting context…",
"session.assistant_idle": "Idle",
"session.assistant_responding": "Responding",
"session.assistant_thinking": "Thinking",
diff --git a/apps/app/src/react-app/domains/session/surface/composer/composer.tsx b/apps/app/src/react-app/domains/session/surface/composer/composer.tsx
index e85fa44d..d4e2213d 100644
--- a/apps/app/src/react-app/domains/session/surface/composer/composer.tsx
+++ b/apps/app/src/react-app/domains/session/surface/composer/composer.tsx
@@ -57,6 +57,10 @@ type ComposerProps = {
onSteer: () => void | Promise<void>;
onQueue: () => void | Promise<void>;
onStop: () => void | Promise<void>;
+ /** Explicitly summarize the current session's context. */
+ onCompact?: () => void | Promise<void>;
+ canCompact?: boolean;
+ compacting?: boolean;
busy: boolean;
steering: boolean;
submissionPreparing: boolean;
@@ -1707,6 +1711,20 @@ export const ReactSessionComposer = memo(function ReactSessionComposer(props: Co
<Paperclip size={16} />
</button>
<VoiceButton voice={voice} disabled={props.disabled} />
+ {props.onCompact ? (
+ <button
+ type="button"
+ data-context-compact
+ className="inline-flex h-9 max-h-9 shrink-0 items-center gap-1.5 rounded-md px-2 text-[11px] font-medium text-gray-10 transition-colors hover:bg-gray-3 hover:text-gray-12 disabled:cursor-not-allowed disabled:opacity-50"
+ onClick={() => void props.onCompact?.()}
+ disabled={props.disabled || props.busy || props.compacting || props.canCompact === false}
+ aria-label={props.compacting ? t("session.compact_context_running") : t("session.compact_context")}
+ title={props.compacting ? t("session.compact_context_running") : t("app.compact_command_desc")}
+ >
+ {props.compacting ? <LoaderCircle size={13} className="animate-spin" /> : <RefreshCw size={13} />}
+ <span>{props.compacting ? t("session.compact_context_running") : t("session.compact_context")}</span>
+ </button>
+ ) : null}
</div>
diff --git a/apps/app/src/react-app/domains/session/surface/session-surface.tsx b/apps/app/src/react-app/domains/session/surface/session-surface.tsx
index 47a94767..6b41d454 100644
--- a/apps/app/src/react-app/domains/session/surface/session-surface.tsx
+++ b/apps/app/src/react-app/domains/session/surface/session-surface.tsx
@@ -3,12 +3,13 @@ import { useCallback, useEffect, useEffectEvent, useLayoutEffect, useMemo, useRe
import type { UIMessage } from "ai";
import { useQuery } from "@tanstack/react-query";
import type { SessionStatus } from "@opencode-ai/sdk/v2/client";
-import { Check, CirclePause, Minimize2 } from "lucide-react";
+import { Check, CirclePause, LoaderCircle, Minimize2 } from "lucide-react";
import { toast } from "@/components/ui/sonner";
import { captureAnalyticsEvent } from "@/app/lib/analytics";
import { interruptSessionTurn, sessionNeedsStop, submitAfterInterruption, subscribeSessionInterruption } from "@/app/lib/opencode-interruption";
import { createClient, createPromptMessageID, hasAcceptedPromptMessage, isPromptAdmissionUnknown, unwrap } from "@/app/lib/opencode";
+import { compactSession } from "@/app/lib/opencode-session";
import { createClientV2, isOpencodeV2BaseUrl } from "@/app/lib/opencode-v2-adapter";
import * as opencodeSessionNative from "@/app/lib/opencode-session-native";
import type { NativeSessionSnapshotTarget } from "@/app/lib/opencode-session-native";
@@ -702,6 +703,26 @@ function resolveFindOwnerSessionId() {
return firstMountedSessionSurfaceId();
}
+function ContextCompactionProgress() {
+ const label = t("session.compact_context_running");
+ return (
+ <div
+ className="mx-3 mb-2 rounded-xl border border-dls-border bg-gray-2/40 px-3 py-2"
+ data-testid="context-compaction-progress"
+ role="status"
+ aria-live="polite"
+ >
+ <div className="flex items-center gap-2 text-xs font-medium text-gray-11">
+ <LoaderCircle size={13} className="animate-spin text-gray-10" />
+ <span>{label}</span>
+ </div>
+ <div className="mt-2 h-1 overflow-hidden rounded-full bg-gray-4" aria-hidden="true">
+ <div className="h-full w-1/3 rounded-full bg-[var(--dls-accent)] animate-pulse" />
+ </div>
+ </div>
+ );
+}
+
function statusLabel(snapshot: OmniRushSessionSnapshot | undefined, busy: boolean) {
if (busy) return "Running...";
if (snapshot?.status.type === "busy") return "Running...";
@@ -1763,6 +1784,36 @@ export function SessionSurface(props: SessionSurfaceProps) {
: showAssistantRespondingState
? "responding"
: "idle";
+ const canCompactSession = baseRenderedMessages.length > 0
+ && !chatStreaming
+ && !sending
+ && !sessionModelUnavailable
+ && effectiveActivityStatus !== "compacting";
+ const handleCompactSession = useCallback(async () => {
+ if (!canCompactSession) return;
+ const activity = useSessionActivityStore.getState();
+ activity.setCompacting(props.workspaceId, props.sessionId, true);
+ try {
+ await compactSession(
+ opencodeClient,
+ props.sessionId,
+ sessionModel.selectedModel,
+ { directory: props.workspaceRoot.trim() || undefined },
+ );
+ } catch (error) {
+ toast.error(error instanceof Error ? error.message : t("app.error_request_failed"));
+ } finally {
+ activity.setCompacting(props.workspaceId, props.sessionId, false);
+ }
+ }, [
+ canCompactSession,
+ opencodeClient,
+ props.sessionId,
+ props.workspaceId,
+ props.workspaceRoot,
+ sessionModel.selectedModel,
+ ]);
+ const showCompactionProgress = effectiveActivityStatus === "compacting";
useReactRenderWatchdog("SessionSurface", {
sessionId: props.sessionId,
workspaceId: props.workspaceId,
@@ -3252,6 +3303,9 @@ export function SessionSurface(props: SessionSurfaceProps) {
onSteer={handleSteer}
onQueue={handleQueue}
onStop={async () => { await handleAbort(); }}
+ onCompact={handleCompactSession}
+ canCompact={canCompactSession}
+ compacting={showCompactionProgress}
busy={chatStreaming}
steering={steering}
submissionPreparing={preparingCloudTools || sending || autoSending}
@@ -3305,10 +3359,11 @@ export function SessionSurface(props: SessionSurfaceProps) {
isRemoteWorkspace={props.isRemoteWorkspace}
isSandboxWorkspace={props.isSandboxWorkspace}
onUploadInboxFiles={props.onUploadInboxFiles ?? handleUploadInboxFiles}
- compactTopSpacing={Boolean(props.activeQuestion || (props.todos ?? []).some((todo) => todo.content.trim()) || props.activePermission || queuedItems.length > 0)}
+ compactTopSpacing={Boolean(showCompactionProgress || props.activeQuestion || (props.todos ?? []).some((todo) => todo.content.trim()) || props.activePermission || queuedItems.length > 0)}
topAccessory={
- props.activeQuestion || (props.todos ?? []).some((todo) => todo.content.trim()) || props.activePermission || queuedItems.length > 0 ? (
+ showCompactionProgress || props.activeQuestion || (props.todos ?? []).some((todo) => todo.content.trim()) || props.activePermission || queuedItems.length > 0 ? (
<div>
+ {showCompactionProgress ? <ContextCompactionProgress /> : null}
{queuedItems.length > 0 ? (
<QueuedMessagesPanel
items={queuedItems}
diff --git a/evals/specs/responsive-session-layout.e2e.test.ts b/evals/specs/responsive-session-layout.e2e.test.ts
index 96722a31..69a4b77d 100644
--- a/evals/specs/responsive-session-layout.e2e.test.ts
+++ b/evals/specs/responsive-session-layout.e2e.test.ts
@@ -171,7 +171,11 @@ test.skipIf(!runnable)(
const buttons = [...toolbar.querySelectorAll('button')]
.map((button) => button.getBoundingClientRect())
.filter((rect) => rect.width > 0 && rect.height > 0);
- return buttons.length >= 4 && buttons.every((rect, index) =>
+ const compactButtons = [...toolbar.querySelectorAll<HTMLButtonElement>('button[data-context-compact]')]
+ .filter((button) => button.getBoundingClientRect().width > 0 && button.getBoundingClientRect().height > 0);
+ return buttons.length >= 4
+ && compactButtons.length === 1
+ && buttons.every((rect, index) =>
rect.left >= bounds.left - 1 && rect.right <= bounds.right + 1
&& rect.top >= bounds.top - 1 && rect.bottom <= bounds.bottom + 1
&& buttons.slice(index + 1).every((other) =>
@@ -182,7 +186,7 @@ test.skipIf(!runnable)(
await screenshot(app);
evidence.recordAssertionEvidence(
`Composer controls remain contained and do not overlap at ${width}px`,
- "Measured every visible toolbar button against the toolbar bounds and every other button.",
+ "Measured every visible toolbar button against the toolbar bounds and every other button; the explicit Compact context action remained visible in the toolbar.",
true,
);
}