fix(coding-agent): reuse session thinking for compaction closes #3438

This commit is contained in:
Mario Zechner
2026-04-20 15:09:50 +02:00
parent d66ef6dce8
commit da6a81d398
4 changed files with 69 additions and 10 deletions

View File

@@ -5,7 +5,7 @@
* and after compaction the session is reloaded.
*/
import type { AgentMessage } from "@mariozechner/pi-agent-core";
import type { AgentMessage, ThinkingLevel } from "@mariozechner/pi-agent-core";
import type { AssistantMessage, Model, Usage } from "@mariozechner/pi-ai";
import { completeSimple } from "@mariozechner/pi-ai";
import {
@@ -536,6 +536,7 @@ export async function generateSummary(
signal?: AbortSignal,
customInstructions?: string,
previousSummary?: string,
thinkingLevel?: ThinkingLevel,
): Promise<string> {
const maxTokens = Math.floor(0.8 * reserveTokens);
@@ -565,9 +566,10 @@ export async function generateSummary(
},
];
const completionOptions = model.reasoning
? { maxTokens, signal, apiKey, headers, reasoning: "high" as const }
: { maxTokens, signal, apiKey, headers };
const completionOptions =
model.reasoning && thinkingLevel && thinkingLevel !== "off"
? { maxTokens, signal, apiKey, headers, reasoning: thinkingLevel }
: { maxTokens, signal, apiKey, headers };
const response = await completeSimple(
model,
@@ -719,6 +721,7 @@ export async function compact(
headers?: Record<string, string>,
customInstructions?: string,
signal?: AbortSignal,
thinkingLevel?: ThinkingLevel,
): Promise<CompactionResult> {
const {
firstKeptEntryId,
@@ -747,9 +750,18 @@ export async function compact(
signal,
customInstructions,
previousSummary,
thinkingLevel,
)
: Promise.resolve("No prior history."),
generateTurnPrefixSummary(turnPrefixMessages, model, settings.reserveTokens, apiKey, headers, signal),
generateTurnPrefixSummary(
turnPrefixMessages,
model,
settings.reserveTokens,
apiKey,
headers,
signal,
thinkingLevel,
),
]);
// Merge into single summary
summary = `${historyResult}\n\n---\n\n**Turn Context (split turn):**\n\n${turnPrefixResult}`;
@@ -764,6 +776,7 @@ export async function compact(
signal,
customInstructions,
previousSummary,
thinkingLevel,
);
}
@@ -793,6 +806,7 @@ async function generateTurnPrefixSummary(
apiKey: string,
headers?: Record<string, string>,
signal?: AbortSignal,
thinkingLevel?: ThinkingLevel,
): Promise<string> {
const maxTokens = Math.floor(0.5 * reserveTokens); // Smaller budget for turn prefix
const llmMessages = convertToLlm(messages);
@@ -809,7 +823,9 @@ async function generateTurnPrefixSummary(
const response = await completeSimple(
model,
{ systemPrompt: SUMMARIZATION_SYSTEM_PROMPT, messages: summarizationMessages },
{ maxTokens, signal, apiKey, headers },
model.reasoning && thinkingLevel && thinkingLevel !== "off"
? { maxTokens, signal, apiKey, headers, reasoning: thinkingLevel }
: { maxTokens, signal, apiKey, headers },
);
if (response.stopReason === "error") {