feat(coding-agent): expose post-compaction token estimates

closes #5877
This commit is contained in:
Armin Ronacher
2026-06-18 23:01:10 +02:00
parent b09fbde00e
commit c60f6a8ab3
5 changed files with 28 additions and 1 deletions

View File

@@ -5,6 +5,7 @@ import {
type Model,
} from "@earendil-works/pi-ai";
import { afterEach, describe, expect, it, vi } from "vitest";
import { estimateTokens } from "../../src/core/compaction/index.ts";
import { createHarness, type Harness } from "./harness.ts";
type SessionWithCompactionInternals = {
@@ -118,8 +119,10 @@ describe("AgentSession compaction characterization", () => {
const result = await harness.session.compact();
const compactionEntries = harness.sessionManager.getEntries().filter((entry) => entry.type === "compaction");
const estimatedTokensAfter = harness.session.messages.reduce((sum, message) => sum + estimateTokens(message), 0);
expect(result.summary).toBe("summary from extension");
expect(result.estimatedTokensAfter).toBe(estimatedTokensAfter);
expect(compactionEntries).toHaveLength(1);
expect(harness.session.messages[0]?.role).toBe("compactionSummary");
});
@@ -161,7 +164,9 @@ describe("AgentSession compaction characterization", () => {
await sessionInternals._runAutoCompaction("threshold", false);
const compactionEntries = harness.sessionManager.getEntries().filter((entry) => entry.type === "compaction");
const compactionEnd = harness.eventsOfType("compaction_end").at(-1);
expect(compactionEntries).toHaveLength(1);
expect(compactionEnd?.result?.estimatedTokensAfter).toBeGreaterThan(0);
expect(getStreamCallCount()).toBe(1);
});