Skip to content

feat: auto-compact context when token usage exceeds ENV threshold#17

Merged
william0wang merged 1 commit into
mainfrom
feat/auto-compact
Jul 13, 2026
Merged

feat: auto-compact context when token usage exceeds ENV threshold#17
william0wang merged 1 commit into
mainfrom
feat/auto-compact

Conversation

@william0wang

Copy link
Copy Markdown
Owner

No description provided.

@william0wang william0wang merged commit 2408a9a into main Jul 13, 2026
1 check passed
@william0wang william0wang deleted the feat/auto-compact branch July 13, 2026 02:49

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces an auto-compaction feature configured via the ZCODE_ACP_AUTO_COMPACT_THRESHOLD environment variable. When context usage exceeds this threshold, the server automatically triggers session/compact after a successful turn. The feedback highlights two main improvement opportunities: first, running maybeAutoCompact asynchronously without await to prevent blocking the prompt response and introducing user-visible latency; second, passing the already-fetched context usage to maybeAutoCompact to avoid a redundant session/read roundtrip to the backend.

Important

The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.

Comment thread src/handlers/session.ts
Comment on lines +382 to +385
if (result.stopReason === "end_turn") {
const { maybeAutoCompact } = await import("../config/auto-compact.js");
await maybeAutoCompact(server, cx, params.sessionId, zcodeSid);
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

Awaiting maybeAutoCompact here blocks the prompt response from returning to the client. Since compaction can take a significant amount of time (up to 5 minutes as configured in compact's waitForTurnIdle), this introduces severe user-visible latency after the turn has already successfully ended.

Since maybeAutoCompact is fully wrapped in try/catch blocks and is guaranteed not to throw or reject, you can safely run it in the background by calling it without await (using void). This allows the prompt response to return immediately, vastly improving the user experience.

    if (result.stopReason === "end_turn") {
      const { maybeAutoCompact } = await import("../config/auto-compact.js");
      // Run auto-compaction in the background so we don't block the prompt response.
      // Compaction can take up to several minutes, and awaiting it here would introduce
      // severe user-visible latency after the turn has already successfully ended.
      void maybeAutoCompact(server, cx, params.sessionId, zcodeSid);
    }

Comment on lines +40 to +56
// Read current context usage via session/read.
let used = 0;
try {
const backend = server.ensureBackend();
const resp = await backend.request(
server.nextId(),
"session/read",
{ sessionId: zcodeSid },
5000,
);
if (resp.error) return;
const result = (resp.result ?? {}) as { projection?: { contextUsed?: number } };
used = result.projection?.contextUsed ?? 0;
} catch (e) {
warn(`auto-compact: session/read failed (${e instanceof Error ? e.message : String(e)})`);
return;
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

This session/read call is redundant. Right before maybeAutoCompact is triggered in prompt(), runEventTurn has already fetched the latest session state via buildSnapshot (which internally calls session/read to get the projection and context usage).

To avoid an extra roundtrip to the backend subprocess, consider passing the known context usage (or the snapshot) down from prompt() to maybeAutoCompact. For example, you could retrieve the last usage from the differ or return it from runEventTurn and pass it as an optional parameter to maybeAutoCompact.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant