Skip to content

Commit bb2f3a5

Browse files
authored
fix(google): preserve Gemini native compaction ownership (#132527)
1 parent 8e673fc commit bb2f3a5

3 files changed

Lines changed: 34 additions & 23 deletions

File tree

‎docs/gateway/cli-backends.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,8 @@ Some CLI backends run an agent that compacts its own transcript, so OpenClaw mus
302302

303303
`claude-cli` has no harness endpoint (Claude Code compacts internally), so it declares `ownsNativeCompaction: true`. Automatic OpenClaw compaction defers to Claude Code, while an explicit `/compact` resumes the bound Claude Code session and sends its native `/compact` command. OpenClaw passes the run's effective context budget through Claude Code's documented [`CLAUDE_CODE_AUTO_COMPACT_WINDOW`](https://code.claude.com/docs/en/env-vars), keeping native auto-compaction aligned with configured Anthropic `contextTokens` limits. Native-harness sessions such as Codex keep routing to their harness compaction endpoint instead.
304304

305+
`google-gemini-cli` also owns automatic compaction and persists its compressed session for resume. OpenClaw defers to Gemini CLI rather than running a second summarizer. Explicit `/compact` is unsupported for this backend because it does not declare a manual compaction capability.
306+
305307
```typescript
306308
api.registerCliBackend({
307309
id: "my-cli",

‎extensions/google/cli-backend.ts‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,9 @@ export function buildGoogleGeminiCliBackend(): CliBackendPlugin {
118118
},
119119
bundleMcp: true,
120120
bundleMcpMode: "gemini-system-settings",
121+
// Gemini compresses and persists its native history before requests; a second
122+
// compactor would require API auth outside the CLI and discard its resume binding.
123+
ownsNativeCompaction: true,
121124
nativeToolMode: "selectable",
122125
toolAvailabilityEnforcement: "prepare-execution",
123126
authEpochMode: "profile-only",

‎src/agents/command/cli-compaction.test.ts‎

Lines changed: 29 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { replaceSessionEntry } from "../../config/sessions/session-accessor.js";
88
import { SESSION_TOTAL_TOKENS_VERSION, type SessionEntry } from "../../config/sessions/types.js";
99
import type { OpenClawConfig } from "../../config/types.openclaw.js";
1010
import type { ContextEngine } from "../../context-engine/types.js";
11+
import { resolveCliBackendConfig } from "../cli-backends.js";
1112
import { createModelGenerationFixture } from "../embedded-agent-runner/model.generation-scope.test-support.js";
1213
import { SessionManager } from "../sessions/session-manager.js";
1314
import {
@@ -1183,29 +1184,34 @@ describe("runCliTurnCompactionLifecycle", () => {
11831184
);
11841185
});
11851186

1186-
it("skips compaction when backend declares ownsNativeCompaction and has no harness endpoint", async () => {
1187-
const compactAgentHarnessSession = vi.fn();
1188-
const scenario = await prepareCompactionScenario({
1189-
suffix: "claude-owns-compaction",
1190-
tmpDir,
1191-
deps: {
1192-
maybeCompactAgentHarnessSession: compactAgentHarnessSession as never,
1193-
resolveCliBackendConfig: () => ({
1194-
id: "claude-cli",
1195-
config: { command: "claude" },
1196-
bundleMcp: true,
1197-
ownsNativeCompaction: true,
1198-
}),
1199-
},
1200-
});
1201-
const { compactCalls, recordCliCompactionInStore, sessionEntry } = scenario;
1202-
const updatedEntry = await scenario.run();
1203-
1204-
expect(compactAgentHarnessSession).not.toHaveBeenCalled();
1205-
expect(compactCalls).toHaveLength(0);
1206-
expect(recordCliCompactionInStore).not.toHaveBeenCalled();
1207-
expect(updatedEntry).toBe(sessionEntry);
1208-
});
1187+
it.each(["claude-cli", "google-gemini-cli"])(
1188+
"preserves %s native history and resume binding under compaction pressure",
1189+
async (provider) => {
1190+
const backend = resolveCliBackendConfig(provider);
1191+
expect(backend).not.toBeNull();
1192+
const compactAgentHarnessSession = vi.fn();
1193+
const scenario = await prepareCompactionScenario({
1194+
suffix: `${provider}-owns-compaction`,
1195+
tmpDir,
1196+
provider,
1197+
sessionEntry: {
1198+
cliSessionBindings: { [provider]: { sessionId: "native-session" } },
1199+
},
1200+
deps: {
1201+
maybeCompactAgentHarnessSession: compactAgentHarnessSession as never,
1202+
resolveCliBackendConfig: () => backend,
1203+
},
1204+
});
1205+
const { compactCalls, recordCliCompactionInStore, sessionEntry } = scenario;
1206+
const updatedEntry = await scenario.run();
1207+
1208+
expect(compactAgentHarnessSession).not.toHaveBeenCalled();
1209+
expect(compactCalls).toHaveLength(0);
1210+
expect(recordCliCompactionInStore).not.toHaveBeenCalled();
1211+
expect(updatedEntry).toBe(sessionEntry);
1212+
expect(updatedEntry?.cliSessionBindings?.[provider]?.sessionId).toBe("native-session");
1213+
},
1214+
);
12091215

12101216
it("does not skip compaction when backend does not declare ownsNativeCompaction", async () => {
12111217
const scenario = await prepareCompactionScenario({

0 commit comments

Comments
 (0)