fix(agents): increment compaction counter on overflow-triggered compaction (#39123)

Co-authored-by: MumuTW <clothl47364@gmail.com>
This commit is contained in:
Peter Steinberger
2026-03-07 19:44:06 +00:00
parent 4c2cb73055
commit e4497234c7
4 changed files with 104 additions and 7 deletions

View File

@@ -40,11 +40,17 @@ export function handleAutoCompactionStart(ctx: EmbeddedPiSubscribeContext) {
export function handleAutoCompactionEnd(
ctx: EmbeddedPiSubscribeContext,
evt: AgentEvent & { willRetry?: unknown },
evt: AgentEvent & { willRetry?: unknown; result?: unknown; aborted?: unknown },
) {
ctx.state.compactionInFlight = false;
const willRetry = Boolean(evt.willRetry);
if (!willRetry) {
// Increment counter whenever compaction actually produced a result,
// regardless of willRetry. Overflow-triggered compaction sets willRetry=true
// (the framework retries the LLM request), but the compaction itself succeeded
// and context was trimmed — the counter must reflect that. (#38905)
const hasResult = evt.result != null;
const wasAborted = Boolean(evt.aborted);
if (hasResult && !wasAborted) {
ctx.incrementCompactionCount?.();
}
if (willRetry) {

View File

@@ -38,11 +38,26 @@ describe("subscribeEmbeddedPiSession", () => {
emit({ type: "auto_compaction_start" });
expect(subscription.getCompactionCount()).toBe(0);
emit({ type: "auto_compaction_end", willRetry: true });
// willRetry with result — counter IS incremented (overflow compaction succeeded)
emit({ type: "auto_compaction_end", willRetry: true, result: { summary: "s" } });
expect(subscription.getCompactionCount()).toBe(1);
// willRetry=false with result — counter incremented again
emit({ type: "auto_compaction_end", willRetry: false, result: { summary: "s2" } });
expect(subscription.getCompactionCount()).toBe(2);
});
it("does not count compaction when result is absent", async () => {
const { emit, subscription } = createSubscribedSessionHarness({
runId: "run-compaction-no-result",
});
// No result (e.g. aborted or cancelled) — counter stays at 0
emit({ type: "auto_compaction_end", willRetry: false, result: undefined });
expect(subscription.getCompactionCount()).toBe(0);
emit({ type: "auto_compaction_end", willRetry: false });
expect(subscription.getCompactionCount()).toBe(1);
emit({ type: "auto_compaction_end", willRetry: false, aborted: true });
expect(subscription.getCompactionCount()).toBe(0);
});
it("emits compaction events on the agent event bus", async () => {