Skip to content

Commit 640d73e

Browse files
VACIncroboclaw-bot
andauthored
fix(anthropic): reload compacted Claude sessions (#131288)
* fix(anthropic): reload compacted SDK sessions * test(cache): require 90% Anthropic embedded hit rate Worked on by: - @VACInc Co-authored-by: VACInc <[email protected]> * test(cache): align Anthropic API cache floor * test(cache): run embedded Anthropic parity probe * test(cache): enforce Anthropic API hit-rate parity --------- Co-authored-by: VACInc <[email protected]> Co-authored-by: roboclaw-bot <[email protected]>
1 parent c07b753 commit 640d73e

5 files changed

Lines changed: 113 additions & 0 deletions

File tree

‎extensions/anthropic/agent-sdk.runtime.test.ts‎

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -510,6 +510,35 @@ describe("Anthropic Agent SDK runtime ownership", () => {
510510
expect(sdkOptions()).not.toHaveProperty("sessionId");
511511
});
512512

513+
it("preserves cache, effort, and checkpoint-fork controls through SDK options", async () => {
514+
useSdkMessages();
515+
516+
await collect(
517+
createContext({
518+
args: [
519+
"-p",
520+
"--cache-system-prompt",
521+
"--effort",
522+
"max",
523+
"--fork-session",
524+
"--resume-session-at",
525+
"assistant-before-stall",
526+
],
527+
useResume: true,
528+
}),
529+
);
530+
531+
expect(sdkOptions()).toEqual(
532+
expect.objectContaining({
533+
resume: SESSION_ID,
534+
effort: "max",
535+
forkSession: true,
536+
resumeSessionAt: "assistant-before-stall",
537+
extraArgs: { "cache-system-prompt": null },
538+
}),
539+
);
540+
});
541+
513542
it("reuses one official SDK query and Claude process across compatible agent turns", async () => {
514543
const live = useLiveSdkStreams();
515544
const capability = createLiveCapability();
@@ -544,6 +573,39 @@ describe("Anthropic Agent SDK runtime ownership", () => {
544573
]);
545574
});
546575

576+
it("keeps a terminal error turn's warm query reusable for the next turn", async () => {
577+
const live = useLiveSdkStreams();
578+
const capability = createLiveCapability();
579+
const first = collect(createContext({ prompt: "Attempt the task.", liveSession: capability }));
580+
await vi.waitFor(() => expect(queryMock).toHaveBeenCalledOnce());
581+
live.streams[0]?.write({
582+
...SUCCESS_RESULT,
583+
subtype: "error_during_execution",
584+
is_error: true,
585+
result: "The native tool failed.",
586+
});
587+
588+
await expect(first).resolves.toContainEqual(
589+
expect.objectContaining({ is_error: true, result: "The native tool failed." }),
590+
);
591+
const firstHandle = capability.current();
592+
expect(firstHandle?.isIdle()).toBe(true);
593+
594+
const second = collect(
595+
createContext({
596+
prompt: "Continue without repeating the failed action.",
597+
useResume: true,
598+
liveSession: capability,
599+
}),
600+
);
601+
await vi.waitFor(() => expect(live.prompts[0]).toHaveLength(2));
602+
live.streams[0]?.write({ ...SUCCESS_RESULT, result: "continued" });
603+
604+
await expect(second).resolves.toContainEqual(expect.objectContaining({ result: "continued" }));
605+
expect(queryMock).toHaveBeenCalledOnce();
606+
expect(capability.current()).toBe(firstHandle);
607+
});
608+
547609
it("restarts the warm SDK query when its system prompt or execution fingerprint changes", async () => {
548610
const live = useLiveSdkStreams();
549611
const shared: { current?: CliBackendLiveSessionHandle } = {};
@@ -621,6 +683,21 @@ describe("Anthropic Agent SDK runtime ownership", () => {
621683
},
622684
),
623685
).resolves.toEqual({ behavior: "deny", message: "The OpenClaw run is no longer active." });
686+
687+
const resumed = collect(
688+
createContext({
689+
prompt: "Resume after the interrupted turn.",
690+
useResume: true,
691+
liveSession: capability,
692+
}),
693+
);
694+
await vi.waitFor(() => expect(queryMock).toHaveBeenCalledTimes(2));
695+
live.streams[1]?.write({ ...SUCCESS_RESULT, result: "resumed" });
696+
697+
await expect(resumed).resolves.toContainEqual(expect.objectContaining({ result: "resumed" }));
698+
expect(queryMock.mock.calls[1]?.[0]?.options).toEqual(
699+
expect.objectContaining({ resume: SESSION_ID }),
700+
);
624701
});
625702

626703
it("rebinds a persistent SDK approval callback to only the active admitted turn", async () => {

‎src/agents/cli-runner/cli-live-session-registry.test.ts‎

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,10 @@ describe("generic plugin-owned live session registry", () => {
141141
const fresh = buildPreparedCliRunContext({ systemPrompt: "Original system policy." });
142142
const resumed = buildPreparedCliRunContext({ systemPrompt: "Original system policy." });
143143
const changed = buildPreparedCliRunContext({ systemPrompt: "Changed system policy." });
144+
const mcpFresh = buildPreparedCliRunContext({ systemPrompt: "Original system policy." });
145+
const mcpResumed = buildPreparedCliRunContext({ systemPrompt: "Original system policy." });
146+
mcpFresh.preparedBackend.mcpConfigHash = "stable-mcp-config";
147+
mcpResumed.preparedBackend.mcpConfigHash = "stable-mcp-config";
144148
const env = { PATH: "/usr/bin:/bin" };
145149
const freshFingerprint = buildCliLiveSessionFingerprint({
146150
context: fresh,
@@ -162,6 +166,33 @@ describe("generic plugin-owned live session registry", () => {
162166
env,
163167
}),
164168
).not.toBe(freshFingerprint);
169+
expect(
170+
buildCliLiveSessionFingerprint({
171+
context: resumed,
172+
argv: ["claude", "-p", "--resume", "native-session", "--effort", "max"],
173+
env,
174+
}),
175+
).not.toBe(freshFingerprint);
176+
expect(
177+
buildCliLiveSessionFingerprint({
178+
context: resumed,
179+
argv: ["claude", "-p", "--resume", "native-session"],
180+
env: { ...env, CLAUDE_CODE_EFFORT_LEVEL: "max" },
181+
}),
182+
).not.toBe(freshFingerprint);
183+
184+
const mcpFreshFingerprint = buildCliLiveSessionFingerprint({
185+
context: mcpFresh,
186+
argv: ["claude", "-p", "--session-id", "native-session", "--mcp-config", "/tmp/turn-a.json"],
187+
env,
188+
});
189+
expect(
190+
buildCliLiveSessionFingerprint({
191+
context: mcpResumed,
192+
argv: ["claude", "-p", "--resume", "native-session", "--mcp-config", "/tmp/turn-b.json"],
193+
env,
194+
}),
195+
).toBe(mcpFreshFingerprint);
165196
});
166197

167198
it("exposes only an active registered generation and never revives a removed owner", async () => {

‎src/agents/embedded-agent-runner/compact.native-cli.test.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@ describe("native CLI manual compaction", () => {
117117
sessionEntry: { execHost: "node", execNode: "paired-node" },
118118
controlOperation: "compact",
119119
disableCliLiveSession: true,
120+
cleanupCliLiveSessionOnRunEnd: true,
120121
allowEmptyAssistantReplyAsSilent: true,
121122
}),
122123
);

‎src/agents/embedded-agent-runner/compact.ts‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,9 @@ export async function compactNativeCliSession(params: {
148148
trigger: "manual",
149149
controlOperation: "compact",
150150
disableCliLiveSession: true,
151+
// Compaction rewrites the persisted session behind any idle SDK query. Retire that query
152+
// after the control turn so the next user turn reloads the compacted conversation.
153+
cleanupCliLiveSessionOnRunEnd: true,
151154
allowEmptyAssistantReplyAsSilent: true,
152155
abortSignal: params.compactParams.abortSignal,
153156
});

‎src/agents/live-cache-regression-baseline.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ export const LIVE_CACHE_REGRESSION_BASELINE = {
4949
observedHitRate: 0.996,
5050
minCacheReadOrWrite: 5_400,
5151
minCacheWrite: 1,
52+
minHitRate: 0.9,
5253
},
5354
tool: {
5455
observedCacheRead: 6_223,

0 commit comments

Comments
 (0)