fix(agents): harden compaction and reset safety

Co-authored-by: jaden-clovervnd <91520439+jaden-clovervnd@users.noreply.github.com>
Co-authored-by: Sid <201593046+Sid-Qin@users.noreply.github.com>
Co-authored-by: Marcus Widing <245375637+widingmarcus-cyber@users.noreply.github.com>
This commit is contained in:
Peter Steinberger
2026-02-26 17:35:55 +01:00
parent 273973d374
commit 0ec7711bc2
20 changed files with 472 additions and 18 deletions

View File

@@ -52,6 +52,7 @@ export function handleAutoCompactionEnd(
ctx.log.debug(`embedded run compaction retry: runId=${ctx.params.runId}`);
} else {
ctx.maybeResolveCompactionWait();
clearStaleAssistantUsageOnSessionMessages(ctx);
}
emitAgentEvent({
runId: ctx.params.runId,
@@ -81,3 +82,23 @@ export function handleAutoCompactionEnd(
}
}
}
function clearStaleAssistantUsageOnSessionMessages(ctx: EmbeddedPiSubscribeContext): void {
const messages = ctx.params.session.messages;
if (!Array.isArray(messages)) {
return;
}
for (const message of messages) {
if (!message || typeof message !== "object") {
continue;
}
const candidate = message as { role?: unknown; usage?: unknown };
if (candidate.role !== "assistant") {
continue;
}
if (!("usage" in candidate)) {
continue;
}
delete (candidate as { usage?: unknown }).usage;
}
}