mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-28 11:30:42 +00:00
fix(agents): increment compaction counter on overflow-triggered compaction (#39123)
Co-authored-by: MumuTW <clothl47364@gmail.com>
This commit is contained in:
@@ -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) {
|
||||
|
||||
@@ -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 () => {
|
||||
|
||||
Reference in New Issue
Block a user