File size: 12,095 Bytes
f0634fb | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 | import { afterEach, beforeEach, describe, expect, it } from 'vitest';
import { IAgentContextMemoryService, IAgentProfileService } from '#/index';
import { TurnEnded } from '#/agent/loop/turnOps';
import { TokenCountingMeasured } from '#/agent/tokenCounting/tokenCountingOps';
import { TokenCountingAgentModelDefinition } from '#/session/tokenCounting/tokenCountingAgentModel';
import { estimateTokensForMessages } from '#/llm-adapter/contract/tokens';
import type { TokenUsage } from '#human/llm/usage';
import { IWireService } from '#/wire/wire';
import { createTestAgent, InMemoryWireRecordPersistence, type TestAgentContext } from '../../harness';
function totalOf(usage: TokenUsage | undefined): number {
if (usage === undefined) return 0;
return usage.inputOther + usage.output + usage.inputCacheRead + usage.inputCacheCreation;
}
function tokenCountingState(ctx: TestAgentContext) {
return ctx.readModel(TokenCountingAgentModelDefinition, (model) => model._state());
}
describe('Agent token counting', () => {
let ctx: TestAgentContext;
let context: IAgentContextMemoryService;
let tokenCounting: TestAgentContext['tokenCounting'];
let profile: IAgentProfileService;
let usage: TestAgentContext['usage'];
beforeEach(async () => {
ctx = createTestAgent();
context = ctx.get(IAgentContextMemoryService);
tokenCounting = ctx.tokenCounting;
profile = ctx.get(IAgentProfileService);
usage = ctx.usage;
await ctx.restorePersisted();
});
afterEach(async () => {
try {
await ctx.expectResumeMatches();
} finally {
await ctx.dispose();
}
});
it('adopts the exchange totals as the measured context size after a turn', async () => {
profile.update({ activeToolNames: [] });
ctx.mockNextResponse({ type: 'text', text: 'Hi there!' });
await ctx.rpc.prompt({ input: [{ type: 'text', text: 'hi' }] });
await ctx.untilTurnEnd();
const exchangeTotal = totalOf(usage.status().total);
expect(exchangeTotal).toBeGreaterThan(0);
expect(context.get()).toHaveLength(2);
expect(tokenCountingState(ctx)).toEqual({
anchors: [{ length: context.get().length, tokens: exchangeTotal, measured: true }],
tokens: exchangeTotal,
});
const size = tokenCounting.get();
expect(size.measured).toBe(exchangeTotal);
expect(size.estimated).toBe(0);
expect(size.size).toBe(exchangeTotal);
expect((await ctx.rpc.getContext({})).tokenCount).toBe(exchangeTotal);
});
it('repoints the measured size at the last exchange across turns', async () => {
profile.update({ activeToolNames: [] });
ctx.mockNextResponse({ type: 'text', text: 'first' });
await ctx.rpc.prompt({ input: [{ type: 'text', text: 'hi' }] });
await ctx.untilTurnEnd();
ctx.mockNextResponse({ type: 'text', text: 'second reply, a longer one' });
await ctx.rpc.prompt({ input: [{ type: 'text', text: 'again' }] });
await ctx.untilTurnEnd();
const lastExchangeTotal = totalOf(usage.status().currentTurn);
expect(lastExchangeTotal).toBeGreaterThan(0);
expect(context.get()).toHaveLength(4);
expect(tokenCountingState(ctx).anchors).toHaveLength(2);
expect(tokenCountingState(ctx).anchors[1]).toEqual({
length: context.get().length,
tokens: lastExchangeTotal,
measured: true,
});
expect(tokenCounting.get().measured).toBe(lastExchangeTotal);
expect((await ctx.rpc.getContext({})).tokenCount).toBe(lastExchangeTotal);
});
it('estimates the not-yet-measured tail instead of dropping it', () => {
ctx.appendUserMessage([{ type: 'text', text: 'hello world, not measured yet' }]);
const size = tokenCounting.get();
expect(size.measured).toBe(0);
expect(size.estimated).toBeGreaterThan(0);
expect(size.size).toBe(size.estimated);
});
it('ignores a stored anchor that overshoots the live context', async () => {
ctx.appendUserMessage([{ type: 'text', text: 'only one message' }]);
await ctx.dispatcher.dispatch(new TokenCountingMeasured({ agentId: 'main', length: 5, tokens: 1234 }));
const size = tokenCounting.get();
expect(size.measured).toBe(0);
expect(size.size).toBe(estimateTokensForMessages(context.get()));
});
it('restores the REAL size of the surviving prefix when undo truncates the ledger', async () => {
ctx.appendTurnExchange('u1', 'a1', 1_000);
ctx.appendTurnExchange('u2', 'a2', 2_000);
expect(tokenCounting.get()).toEqual({ size: 2_000, measured: 2_000, estimated: 0 });
await ctx.undoHistory(1);
expect(context.get().map((m) => m.role)).toEqual(['user', 'assistant']);
expect(tokenCounting.get()).toEqual({ size: 1_000, measured: 1_000, estimated: 0 });
expect(tokenCounting.latestMeasured()).toBe(1_000);
});
it('rebases the ledger on compaction and blends in the measured summary tokens', () => {
ctx.appendTurnExchange('u1', 'a1', 1_000);
context.applyCompaction({
summary: 'summary of u1',
compactedCount: 2,
tokensBefore: 1_000,
summaryOutputTokens: 500,
});
const history = context.get();
const kept = estimateTokensForMessages(history.filter((m) => m.origin?.kind !== 'compaction_summary'));
const expected = 500 + kept;
expect(tokenCountingState(ctx).anchors).toEqual([
{ length: history.length, tokens: expected, measured: false },
]);
expect(tokenCounting.get()).toEqual({ size: expected, measured: expected, estimated: 0 });
});
it('resets the ledger when the context is cleared', () => {
ctx.appendAssistantTextWithUsage(1, 'answer', 1_000);
expect(tokenCounting.get().measured).toBe(1_000);
context.clear();
expect(tokenCounting.get()).toEqual({ size: 0, measured: 0, estimated: 0 });
expect(tokenCountingState(ctx).anchors).toEqual([
{ length: 0, tokens: 0, measured: true },
]);
});
it('keeps estimates and anchors live for internal reads under the measured strategy', () => {
const measured = createTestAgent({ initialConfig: { tokenCounting: { strategy: 'measured' } } });
try {
const counting = measured.tokenCounting;
expect(counting.strategy).toBe('measured');
expect(counting.estimateText('abcd')).toBeGreaterThan(0);
measured.appendUserMessage([{ type: 'text', text: 'hello world, not measured yet' }]);
const tailEstimate = estimateTokensForMessages(
measured.get(IAgentContextMemoryService).get(),
);
expect(tailEstimate).toBeGreaterThan(0);
expect(counting.get()).toEqual({ size: tailEstimate, measured: 0, estimated: tailEstimate });
measured.appendTurnExchange('u1', 'a1', 1_000);
expect(counting.get().measured).toBe(1_000);
} finally {
void measured.dispose();
}
});
it('keeps anchors in internal reads under the estimated strategy', () => {
const estimated = createTestAgent({
initialConfig: { tokenCounting: { strategy: 'estimated' } },
});
try {
const counting = estimated.tokenCounting;
expect(counting.strategy).toBe('estimated');
estimated.appendTurnExchange('u1', 'a1', 1_000);
expect(counting.get()).toEqual({ size: 1_000, measured: 1_000, estimated: 0 });
} finally {
void estimated.dispose();
}
});
it('keeps the measured size across a close → resume round trip', async () => {
const persistence = new InMemoryWireRecordPersistence();
const live = createTestAgent({ persistence });
try {
live.appendTurnExchange('u1', 'a1', 1_000);
live.appendTurnExchange('u2', 'a2', 2_000);
const liveCounting = live.tokenCounting;
expect(liveCounting.statusSize()).toBe(2_000);
await live.get(IWireService).flush();
expect(persistence.records.map((record) => record.type)).toContain('token_counting.measured');
const resumed = createTestAgent({ persistence, autoConfigure: false });
try {
await resumed.restorePersisted();
const resumedCounting = resumed.tokenCounting;
expect(tokenCountingState(resumed)).toEqual(tokenCountingState(live));
expect(resumedCounting.latestMeasured()).toBe(2_000);
expect(resumedCounting.statusSize()).toBe(liveCounting.statusSize());
} finally {
await resumed.dispose();
}
} finally {
await live.dispose();
}
});
it('statusSize reports the strategy-selected reading', () => {
const measured = createTestAgent({ initialConfig: { tokenCounting: { strategy: 'measured' } } });
try {
const counting = measured.tokenCounting;
expect(counting.statusSize()).toBe(0);
measured.appendTurnExchange('u1', 'a1', 1_000);
measured.appendUserMessage([{ type: 'text', text: 'not measured yet' }]);
expect(counting.statusSize()).toBe(1_000);
} finally {
void measured.dispose();
}
const estimated = createTestAgent({
initialConfig: { tokenCounting: { strategy: 'estimated' } },
});
try {
const counting = estimated.tokenCounting;
estimated.appendTurnExchange('u1', 'a1', 1_000_000);
const estimate = estimateTokensForMessages(estimated.get(IAgentContextMemoryService).get());
expect(counting.latestMeasured()).toBe(1_000_000);
expect(counting.statusSize()).toBe(estimate);
} finally {
void estimated.dispose();
}
ctx.appendTurnExchange('u1', 'a1', 1_000);
expect(tokenCounting.statusSize()).toBe(
Math.max(tokenCounting.get().size, tokenCounting.latestMeasured()),
);
});
it('journals the reported size as a durable record at every turn end', async () => {
const persistence = new InMemoryWireRecordPersistence();
const live = createTestAgent({ persistence });
try {
live.get(IAgentProfileService).update({ activeToolNames: [] });
live.mockNextResponse({ type: 'text', text: 'Hi there!' });
await live.rpc.prompt({ input: [{ type: 'text', text: 'hi' }] });
await live.untilTurnEnd();
const counting = live.tokenCounting;
const reported = counting.statusSize();
expect(reported).toBeGreaterThan(0);
await live.get(IWireService).flush();
const records = persistence.records.filter(
(record) => record.type === 'token_counting.turn_recorded',
);
expect(records).toHaveLength(1);
expect(records[0]).toMatchObject({
agentId: 'main',
length: live.get(IAgentContextMemoryService).get().length,
tokens: reported,
});
expect(tokenCountingState(live).anchors).toEqual([
{ length: 2, tokens: reported, measured: true },
]);
} finally {
await live.dispose();
}
});
it('pins the reported size at turn end when no measured anchor covers it', async () => {
ctx.appendUserMessage([{ type: 'text', text: 'unmeasured tail' }]);
const expected = tokenCounting.statusSize();
expect(expected).toBeGreaterThan(0);
expect(tokenCountingState(ctx).anchors).toEqual([]);
await ctx.dispatcher.dispatch(
new TurnEnded({ agentId: 'main', turnId: 1, reason: 'completed' }),
);
expect(tokenCountingState(ctx).anchors).toEqual([
{ length: 1, tokens: expected, measured: false },
]);
expect(tokenCounting.statusSize()).toBe(expected);
});
it('drops the pinned turn reading on compaction', async () => {
ctx.appendUserMessage([{ type: 'text', text: 'unmeasured tail' }]);
await ctx.dispatcher.dispatch(
new TurnEnded({ agentId: 'main', turnId: 1, reason: 'completed' }),
);
expect(tokenCountingState(ctx).anchors).toHaveLength(1);
context.applyCompaction({
summary: 'summary of the tail',
compactedCount: 1,
tokensBefore: 100,
summaryOutputTokens: 50,
});
const history = context.get();
const anchors = tokenCountingState(ctx).anchors;
expect(anchors).toHaveLength(1);
expect(anchors[0]).toEqual({
length: history.length,
tokens: tokenCounting.get().size,
measured: false,
});
expect(tokenCounting.statusSize()).toBe(tokenCounting.get().size);
});
});
|