Skip to content

Commit 58d90ee

Browse files
committed
Agents: align compaction summary runtime context
1 parent 76f86e4 commit 58d90ee

7 files changed

Lines changed: 86 additions & 18 deletions

File tree

src/agents/compaction.ts

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
1-
import type { AgentMessage } from "@mariozechner/pi-agent-core";
1+
import type { AgentMessage, ThinkingLevel } from "@mariozechner/pi-agent-core";
22
import type { SimpleStreamOptions } from "@mariozechner/pi-ai";
33
import type { ExtensionContext } from "@mariozechner/pi-coding-agent";
44
import {
55
estimateTokens,
66
generateSummary as piGenerateSummary,
77
} from "@mariozechner/pi-coding-agent";
8-
import type { ThinkLevel } from "../auto-reply/thinking.js";
98
import type { AgentCompactionIdentifierPolicy } from "../config/types.agent-defaults.js";
109
import { formatErrorMessage } from "../infra/errors.js";
1110
import { retryAsync } from "../infra/retry.js";
@@ -58,7 +57,7 @@ type GenerateSummaryCompat = {
5857
signal?: AbortSignal,
5958
customInstructions?: string,
6059
previousSummary?: string,
61-
thinkingLevel?: ThinkLevel,
60+
thinkingLevel?: ThinkingLevel,
6261
completionOptions?: CompactionSummaryCompletionOptions,
6362
): Promise<string>;
6463
(
@@ -70,7 +69,7 @@ type GenerateSummaryCompat = {
7069
signal?: AbortSignal,
7170
customInstructions?: string,
7271
previousSummary?: string,
73-
thinkingLevel?: ThinkLevel,
72+
thinkingLevel?: ThinkingLevel,
7473
completionOptions?: CompactionSummaryCompletionOptions,
7574
): Promise<string>;
7675
};
@@ -309,7 +308,7 @@ async function summarizeChunks(params: {
309308
customInstructions?: string;
310309
summarizationInstructions?: CompactionSummarizationInstructions;
311310
previousSummary?: string;
312-
thinkingLevel?: ThinkLevel;
311+
thinkingLevel?: ThinkingLevel;
313312
completionOptions?: CompactionSummaryCompletionOptions;
314313
}): Promise<string> {
315314
if (params.messages.length === 0) {
@@ -362,7 +361,7 @@ function generateSummary(
362361
signal: AbortSignal,
363362
customInstructions?: string,
364363
previousSummary?: string,
365-
thinkingLevel?: ThinkLevel,
364+
thinkingLevel?: ThinkingLevel,
366365
completionOptions?: CompactionSummaryCompletionOptions,
367366
): Promise<string> {
368367
if (piGenerateSummary.length >= 8 || thinkingLevel || completionOptions) {
@@ -406,7 +405,7 @@ export async function summarizeWithFallback(params: {
406405
customInstructions?: string;
407406
summarizationInstructions?: CompactionSummarizationInstructions;
408407
previousSummary?: string;
409-
thinkingLevel?: ThinkLevel;
408+
thinkingLevel?: ThinkingLevel;
410409
completionOptions?: CompactionSummaryCompletionOptions;
411410
}): Promise<string> {
412411
const { messages, contextWindow } = params;
@@ -472,7 +471,7 @@ export async function summarizeInStages(params: {
472471
customInstructions?: string;
473472
summarizationInstructions?: CompactionSummarizationInstructions;
474473
previousSummary?: string;
475-
thinkingLevel?: ThinkLevel;
474+
thinkingLevel?: ThinkingLevel;
476475
parts?: number;
477476
minMessagesForSplit?: number;
478477
completionOptions?: CompactionSummaryCompletionOptions;

src/agents/pi-embedded-runner/compact.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -843,6 +843,7 @@ export async function compactEmbeddedPiSessionDirect(
843843
modelId,
844844
model,
845845
thinkingLevel: thinkLevel,
846+
agentId: sessionAgentId,
846847
workspaceDir: effectiveWorkspace,
847848
agentDir,
848849
});

src/agents/pi-embedded-runner/extensions.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import { ensurePiCompactionReserveTokens } from "../pi-settings.js";
1717
import { resolveTranscriptPolicy } from "../transcript-policy.js";
1818
import { isCacheTtlEligibleProvider, readLastCacheTtlTimestamp } from "./cache-ttl.js";
1919
import { resolvePreparedExtraParams } from "./extra-params.js";
20+
import { mapThinkingLevel } from "./utils.js";
2021

2122
type PiToolResultEvent = {
2223
threadId?: string;
@@ -141,6 +142,7 @@ export function buildEmbeddedExtensionFactories(params: {
141142
modelId: string;
142143
model: ProviderRuntimeModel | undefined;
143144
thinkingLevel?: ThinkLevel;
145+
agentId?: string;
144146
workspaceDir?: string;
145147
agentDir?: string;
146148
}): ExtensionFactory[] {
@@ -165,11 +167,12 @@ export function buildEmbeddedExtensionFactories(params: {
165167
qualityGuardEnabled: qualityGuardCfg?.enabled ?? true,
166168
qualityGuardMaxRetries: qualityGuardCfg?.maxRetries,
167169
model: params.model,
168-
thinkingLevel: params.thinkingLevel,
170+
thinkingLevel: mapThinkingLevel(params.thinkingLevel),
169171
extraParams: resolvePreparedExtraParams({
170172
cfg: params.cfg,
171173
provider: params.provider,
172174
modelId: params.modelId,
175+
agentId: params.agentId,
173176
thinkingLevel: params.thinkingLevel,
174177
workspaceDir: params.workspaceDir,
175178
agentDir: params.agentDir,

src/agents/pi-embedded-runner/run/attempt.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1392,6 +1392,7 @@ export async function runEmbeddedAttempt(
13921392
modelId: params.modelId,
13931393
model: params.model,
13941394
thinkingLevel: params.thinkLevel,
1395+
agentId: sessionAgentId,
13951396
workspaceDir: effectiveWorkspace,
13961397
agentDir,
13971398
});

src/agents/pi-hooks/compaction-safeguard-runtime.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1+
import type { ThinkingLevel } from "@mariozechner/pi-agent-core";
12
import type { Api, Model } from "@mariozechner/pi-ai";
2-
import type { ThinkLevel } from "../../auto-reply/thinking.js";
33
import type { AgentCompactionIdentifierPolicy } from "../../config/types.agent-defaults.js";
44
import { createSessionManagerRuntimeRegistry } from "./session-manager-runtime-registry.js";
55

@@ -18,7 +18,7 @@ export type CompactionSafeguardRuntimeValue = {
1818
/** Resolved agent/provider params to apply to built-in LLM summary requests. */
1919
extraParams?: Record<string, unknown>;
2020
/** Resolved thinking level to use for built-in LLM summary requests. */
21-
thinkingLevel?: ThinkLevel;
21+
thinkingLevel?: ThinkingLevel;
2222
recentTurnsPreserve?: number;
2323
qualityGuardEnabled?: boolean;
2424
qualityGuardMaxRetries?: number;

src/agents/pi-hooks/compaction-safeguard.test.ts

Lines changed: 69 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -622,17 +622,45 @@ describe("compaction-safeguard runtime registry", () => {
622622
defaults: {
623623
params: {
624624
chat_template_kwargs: {
625-
enable_thinking: false,
626-
preserve_thinking: true,
625+
enable_thinking: true,
626+
source: "default",
627627
},
628628
extra_body: {
629-
top_k: 20,
629+
top_k: 10,
630+
},
631+
},
632+
models: {
633+
"vllm/Qwen/Qwen3-8B": {
634+
params: {
635+
chat_template_kwargs: {
636+
enable_thinking: true,
637+
source: "model",
638+
},
639+
extra_body: {
640+
top_k: 20,
641+
},
642+
},
630643
},
631644
},
632645
compaction: {
633646
mode: "safeguard",
634647
},
635648
},
649+
list: [
650+
{
651+
id: "research",
652+
params: {
653+
chat_template_kwargs: {
654+
enable_thinking: false,
655+
preserve_thinking: true,
656+
source: "agent",
657+
},
658+
extra_body: {
659+
top_k: 30,
660+
},
661+
},
662+
},
663+
],
636664
},
637665
} as OpenClawConfig;
638666

@@ -641,6 +669,7 @@ describe("compaction-safeguard runtime registry", () => {
641669
sessionManager,
642670
provider: "vllm",
643671
modelId: "Qwen/Qwen3-8B",
672+
agentId: "research",
644673
model: {
645674
api: "openai-completions",
646675
provider: "vllm",
@@ -658,12 +687,48 @@ describe("compaction-safeguard runtime registry", () => {
658687
chat_template_kwargs: {
659688
enable_thinking: false,
660689
preserve_thinking: true,
690+
source: "agent",
661691
},
662692
extra_body: {
663-
top_k: 20,
693+
top_k: 30,
664694
},
665695
});
666696
});
697+
698+
it("maps OpenClaw thinking levels before storing compaction summary runtime", () => {
699+
for (const [thinkingLevel, expected] of [
700+
["max", "xhigh"],
701+
["adaptive", "medium"],
702+
] as const) {
703+
const sessionManager = {} as unknown as Parameters<
704+
typeof buildEmbeddedExtensionFactories
705+
>[0]["sessionManager"];
706+
707+
buildEmbeddedExtensionFactories({
708+
cfg: {
709+
agents: {
710+
defaults: {
711+
compaction: {
712+
mode: "safeguard",
713+
},
714+
},
715+
},
716+
} as OpenClawConfig,
717+
sessionManager,
718+
provider: "vllm",
719+
modelId: "Qwen/Qwen3-8B",
720+
thinkingLevel,
721+
model: {
722+
api: "openai-completions",
723+
provider: "vllm",
724+
id: "Qwen/Qwen3-8B",
725+
contextWindow: 200_000,
726+
} as Parameters<typeof buildEmbeddedExtensionFactories>[0]["model"],
727+
});
728+
729+
expect(getCompactionSafeguardRuntime(sessionManager)?.thinkingLevel).toBe(expected);
730+
}
731+
});
667732
});
668733

669734
describe("compaction-safeguard recent-turn preservation", () => {

src/agents/pi-hooks/compaction-safeguard.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
import fs from "node:fs";
22
import path from "node:path";
3-
import type { AgentMessage } from "@mariozechner/pi-agent-core";
3+
import type { AgentMessage, ThinkingLevel } from "@mariozechner/pi-agent-core";
44
import type { ExtensionAPI, ExtensionContext, FileOperations } from "@mariozechner/pi-coding-agent";
55
import { extractSections } from "../../auto-reply/reply/post-compaction-context.js";
6-
import type { ThinkLevel } from "../../auto-reply/thinking.js";
76
import { openBoundaryFile } from "../../infra/boundary-file-read.js";
87
import { formatErrorMessage } from "../../infra/errors.js";
98
import { isAbortError } from "../../infra/unhandled-rejections.js";
@@ -178,7 +177,7 @@ async function summarizeViaLLM(params: {
178177
customInstructions?: string;
179178
summarizationInstructions?: Parameters<typeof summarizeInStages>[0]["summarizationInstructions"];
180179
previousSummary?: string;
181-
thinkingLevel?: ThinkLevel;
180+
thinkingLevel?: ThinkingLevel;
182181
completionOptions?: CompactionSummaryCompletionOptions;
183182
}): Promise<string> {
184183
const messages = prependPreviousSummaryForRedistill({

0 commit comments

Comments
 (0)