fix(token-meter): keep pressure anchors conservative

This commit is contained in:
Hypatia May
2026-07-16 15:26:30 +08:00
parent 6d96b3e4a8
commit 06e0450b8d
4 changed files with 44 additions and 10 deletions

View File

@@ -252,6 +252,36 @@ describe('replay anchors and surface folds', () => {
}).toThrow(TypeError)
})
it('selects a heuristic anchor when provider usage would undercut its scale', () => {
const service = meter()
const session = new Session(SessionId('low-usage-anchor'))
const system = 'system context'
const requestHeader = header('deepseek-v4-flash', { system })
appendSuccessfulCall(session, requestHeader, {
providerText: 'abcd'.repeat(512),
usage: { inputTokens: 20, outputTokens: 7 },
})
const anchored = service.measure(session)
expect(anchored.baseline.kind).toBe('estimated')
const assistant = anchored.nodes[0]!.seq
session.append('user/message', {
content: [{ type: 'text', text: 'short' }],
source: { kind: 'plugin', plugin: 'test' },
}, {
surfaceOp: { op: 'replace', start: assistant, end: assistant },
sourceEventSeqs: [assistant],
})
const shrunken = service.measure(session)
expect(27 + shrunken.surfaceDeltaTokens).toBeLessThan(0)
expect(shrunken.totalTokens).toBeGreaterThan(0)
expect(shrunken.totalTokens).toBe(service.measure(
session,
header('different-model', { system }),
).totalTokens)
})
it('uses an estimated anchor when provider usage is absent', () => {
const service = meter()
const session = new Session(SessionId('missing-usage'))