From e0b82c3ff814d06fb01a528998c1312ccc15c188 Mon Sep 17 00:00:00 2001 From: Bikkies Date: Fri, 10 Apr 2026 14:57:01 +1000 Subject: [PATCH] fix: revert skipAfterTurn, let finalize ingest the final message - Remove loopHookActive flag, skipAfterTurn param, and the conditional skip in finalizeAttemptContextEngineTurn - The finalize afterTurn must run to ingest the final assistant message which is appended after the last transformContext call - lossless-claw deduplicates internally so the overlap with the loop hook's earlier ingestion is benign --- .../run/attempt.context-engine-helpers.ts | 8 ++------ src/agents/pi-embedded-runner/run/attempt.ts | 3 --- 2 files changed, 2 insertions(+), 9 deletions(-) diff --git a/src/agents/pi-embedded-runner/run/attempt.context-engine-helpers.ts b/src/agents/pi-embedded-runner/run/attempt.context-engine-helpers.ts index b3d941ba9be..6b89298d904 100644 --- a/src/agents/pi-embedded-runner/run/attempt.context-engine-helpers.ts +++ b/src/agents/pi-embedded-runner/run/attempt.context-engine-helpers.ts @@ -199,9 +199,6 @@ export async function finalizeAttemptContextEngineTurn(params: { }) => Promise; sessionManager: unknown; warn: (message: string) => void; - /** When true, skip the afterTurn/ingest calls because the loop hook already - * handled per-iteration ingestion during the tool loop. Maintenance still runs. */ - skipAfterTurn?: boolean; }) { if (!params.contextEngine) { return { postTurnFinalizationSucceeded: true }; @@ -209,8 +206,7 @@ export async function finalizeAttemptContextEngineTurn(params: { let postTurnFinalizationSucceeded = true; - if (!params.skipAfterTurn) { - if (typeof params.contextEngine.afterTurn === "function") { + if (typeof params.contextEngine.afterTurn === "function") { try { await params.contextEngine.afterTurn({ sessionId: params.sessionIdUsed, @@ -255,7 +251,7 @@ export async function finalizeAttemptContextEngineTurn(params: { } } } - } + if ( !params.promptError && diff --git a/src/agents/pi-embedded-runner/run/attempt.ts b/src/agents/pi-embedded-runner/run/attempt.ts index 59252a3ca5e..cc70688ed7f 100644 --- a/src/agents/pi-embedded-runner/run/attempt.ts +++ b/src/agents/pi-embedded-runner/run/attempt.ts @@ -997,7 +997,6 @@ export async function runEmbeddedAttempt( queueYieldInterruptForSession = () => { queueSessionsYieldInterruptMessage(activeSession); }; - let loopHookActive = false; if (params.contextEngine?.info?.ownsCompaction !== true) { removeToolResultContextGuard = installToolResultContextGuard({ agent: activeSession.agent, @@ -1018,7 +1017,6 @@ export async function runEmbeddedAttempt( tokenBudget: params.contextTokenBudget, modelId: params.modelId, }); - loopHookActive = true; } const cacheTrace = createCacheTrace({ cfg: params.config, @@ -2218,7 +2216,6 @@ export async function runEmbeddedAttempt( prePromptMessageCount, tokenBudget: params.contextTokenBudget, runtimeContext: afterTurnRuntimeContext, - skipAfterTurn: loopHookActive, runMaintenance: async (contextParams) => await runContextEngineMaintenance({ contextEngine: contextParams.contextEngine as never,