Skip to content

Commit 0f37405

Browse files
fix(clownfish): address review for ghcrawl-156678-autonomous-smoke (1)
Co-authored-by: HollyChou <[email protected]> Co-authored-by: de1ty <[email protected]> Co-authored-by: 吴杨帆 <[email protected]> Co-authored-by: Zhao Shiqi <[email protected]>
1 parent f72f271 commit 0f37405

3 files changed

Lines changed: 51 additions & 6 deletions

File tree

src/agents/compaction-usage.ts

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,10 @@ function parseCompactionUsageTimestamp(value: unknown): number | null {
2020

2121
export function stripStaleAssistantUsageBeforeLatestCompaction<TMessage extends AgentMessage>(
2222
messages: TMessage[],
23-
options: { mutate?: boolean } = {},
23+
options: {
24+
mutate?: boolean;
25+
whenMissingCompactionSummary?: "preserve" | "zeroAssistantUsage";
26+
} = {},
2427
): TMessage[] {
2528
let latestCompactionSummaryIndex = -1;
2629
let latestCompactionTimestamp: number | null = null;
@@ -34,7 +37,8 @@ export function stripStaleAssistantUsageBeforeLatestCompaction<TMessage extends
3437
(entry as { timestamp?: unknown }).timestamp ?? null,
3538
);
3639
}
37-
if (latestCompactionSummaryIndex === -1) {
40+
const hasCompactionSummary = latestCompactionSummaryIndex !== -1;
41+
if (!hasCompactionSummary && options.whenMissingCompactionSummary !== "zeroAssistantUsage") {
3842
return messages;
3943
}
4044

@@ -53,10 +57,13 @@ export function stripStaleAssistantUsageBeforeLatestCompaction<TMessage extends
5357

5458
const messageTimestamp = parseCompactionUsageTimestamp(candidate.timestamp);
5559
const compactionTimestamp = latestCompactionTimestamp;
56-
const hasTimestampBoundary = compactionTimestamp !== null && messageTimestamp !== null;
60+
const hasTimestampBoundary =
61+
hasCompactionSummary && compactionTimestamp !== null && messageTimestamp !== null;
62+
const staleByMissingSummary = !hasCompactionSummary;
5763
const staleByTimestamp = hasTimestampBoundary && messageTimestamp <= compactionTimestamp;
58-
const staleByLegacyOrdering = !hasTimestampBoundary && i < latestCompactionSummaryIndex;
59-
if (!staleByTimestamp && !staleByLegacyOrdering) {
64+
const staleByLegacyOrdering =
65+
hasCompactionSummary && !hasTimestampBoundary && i < latestCompactionSummaryIndex;
66+
if (!staleByMissingSummary && !staleByTimestamp && !staleByLegacyOrdering) {
6067
continue;
6168
}
6269

src/agents/embedded-agent-subscribe.handlers.compaction.test.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -469,6 +469,38 @@ describe("handleCompactionEnd", () => {
469469
expect(freshAssistant.usage).toEqual(freshUsage);
470470
});
471471

472+
it("clears assistant usage when final compaction has no summary marker", async () => {
473+
const firstUsage = makeUsageSnapshot(120_000);
474+
const secondUsage = makeUsageSnapshot(1_250);
475+
const messages = [
476+
makeAssistantUsageMessage({
477+
text: "first answer before marker-free compaction",
478+
usage: firstUsage,
479+
}),
480+
{ role: "user", content: "new question" },
481+
makeAssistantUsageMessage({
482+
text: "second answer before marker-free compaction",
483+
usage: secondUsage,
484+
}),
485+
] as AgentMessage[];
486+
const tmp = await fs.mkdtemp(path.join(os.tmpdir(), "openclaw-compaction-no-summary-"));
487+
const storePath = path.join(tmp, "sessions.json");
488+
const sessionKey = "main";
489+
const ctx = createCompactionContext({
490+
storePath,
491+
sessionKey,
492+
initialCount: 0,
493+
messages,
494+
});
495+
496+
finishCompaction(ctx);
497+
498+
const firstAssistant = messages[0] as Extract<AgentMessage, { role: "assistant" }>;
499+
const secondAssistant = messages[2] as Extract<AgentMessage, { role: "assistant" }>;
500+
expect(firstAssistant.usage).toEqual(makeZeroUsageSnapshot());
501+
expect(secondAssistant.usage).toEqual(makeZeroUsageSnapshot());
502+
});
503+
472504
it("does not let legacy index fallback erase timestamp-fresh usage", async () => {
473505
const freshUsage = makeUsageSnapshot(1_250);
474506
const messages = [

src/agents/embedded-agent-subscribe.handlers.compaction.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,5 +197,11 @@ function clearStaleAssistantUsageOnSessionMessages(ctx: EmbeddedAgentSubscribeCo
197197
if (!Array.isArray(messages)) {
198198
return;
199199
}
200-
stripStaleAssistantUsageBeforeLatestCompaction(messages, { mutate: true });
200+
// Marker-free final compaction has no fresh boundary to compare against.
201+
// Clear all assistant usage or stale pre-compaction totals keep driving the
202+
// context counter after cleanup.
203+
stripStaleAssistantUsageBeforeLatestCompaction(messages, {
204+
mutate: true,
205+
whenMissingCompactionSummary: "zeroAssistantUsage",
206+
});
201207
}

0 commit comments

Comments
 (0)