fix(compaction): resolve model via runtime when ctx.model is undefined

When using the embedded pi-agent runner (createAgentSession path),
extensionRunner.initialize() is never called, leaving ctx.model undefined.

This fix passes the model through the existing WeakMap-based runtime
registry and uses it as a fallback when ctx.model is unavailable.

Fixes #2851

🤖 Generated with Claude Code
This commit is contained in:
Jarvis 2026-01-28 04:12:48 +00:00 committed by bonald
parent d93f8ffc13
commit cb932cfe21
3 changed files with 9 additions and 2 deletions

View File

@ -79,6 +79,7 @@ export function buildEmbeddedExtensionPaths(params: {
const compactionCfg = params.cfg?.agents?.defaults?.compaction;
setCompactionSafeguardRuntime(params.sessionManager, {
maxHistoryShare: compactionCfg?.maxHistoryShare,
model: params.model,
});
paths.push(resolvePiExtensionPath("compaction-safeguard"));
}

View File

@ -1,5 +1,8 @@
import type { Api, Model } from "@mariozechner/pi-ai";
export type CompactionSafeguardRuntimeValue = {
maxHistoryShare?: number;
model?: Model<Api>;
};
// Session-scoped runtime registry keyed by object identity.

View File

@ -146,8 +146,12 @@ export default function compactionSafeguardExtension(api: ExtensionAPI): void {
const toolFailureSection = formatToolFailuresSection(toolFailures);
const fallbackSummary = `${FALLBACK_SUMMARY}${toolFailureSection}${fileOpsSummary}`;
const model = ctx.model;
const runtime = getCompactionSafeguardRuntime(ctx.sessionManager);
const model = ctx.model ?? runtime?.model;
if (!model) {
console.warn(
"Compaction safeguard: no model available (ctx.model and runtime.model both undefined)",
);
return {
compaction: {
summary: fallbackSummary,
@ -175,7 +179,6 @@ export default function compactionSafeguardExtension(api: ExtensionAPI): void {
const turnPrefixMessages = preparation.turnPrefixMessages ?? [];
let messagesToSummarize = preparation.messagesToSummarize;
const runtime = getCompactionSafeguardRuntime(ctx.sessionManager);
const maxHistoryShare = runtime?.maxHistoryShare ?? 0.5;
const tokensBefore =