fix(hermes): keep a priced observation's known cost beside a $0 one in the rebuild

Pricing every observation by token share moved known cost onto observations
stored at $0 (a $10 and a $0 observation became $5 and $5). Priced
observations are now scaled by recomputed over their stored sum, never up when
$0 observations sit beside them, and only the remainder goes to the $0 ones
by token share.
This commit is contained in:
Resham Joshi 2026-11-06 19:33:03 -08:00
parent 32d676bc72
commit 6412e2ec8a
2 changed files with 33 additions and 6 deletions

View file

@ -498,12 +498,15 @@ function repriceRebuild(cursor: HermesSessionCursor, snapshot: HermesSnapshot):
const weight = (t: HermesTokenTotals) => t.inputTokens + t.outputTokens + t.cacheReadTokens + t.cacheWriteTokens
const share = weight(snapshot.tokens) > 0 ? weight(held) / weight(snapshot.tokens) : 1
const costUSD = snapshot.costUSD * share
// A stored $0 beside tokens cannot be scaled, so the observations then take
// the recomputed cost by token share instead.
const byTokens = cursor.observations.some(o => o.costUSD <= 0 && weight(o) > 0)
const repriced = (o: HermesObservation): number => byTokens
? (weight(held) > 0 ? costUSD * weight(o) / weight(held) : 0)
: heldCost > 0 ? o.costUSD * (costUSD / heldCost) : 0
// Beside observations stored at $0 (no price to scale), the priced ones are
// scaled but never up, since the v4 rule only removes reasoning priced twice,
// and what the recomputed cost leaves over goes to the $0 ones by token share.
const unpricedWeight = cursor.observations.filter(o => o.costUSD <= 0).reduce((sum, o) => sum + weight(o), 0)
const factor = heldCost <= 0 ? 0 : unpricedWeight > 0 ? Math.min(1, costUSD / heldCost) : costUSD / heldCost
const remainder = Math.max(0, costUSD - heldCost * factor)
const repriced = (o: HermesObservation): number => o.costUSD > 0
? o.costUSD * factor
: unpricedWeight > 0 ? remainder * weight(o) / unpricedWeight : 0
return {
...rest,
lastSeen: { ...held, costUSD, costBasis: snapshot.costBasis },

View file

@ -373,6 +373,30 @@ describe('hermes session ledger unit', () => {
expect(cursor.rebuild).toBeUndefined()
})
it('keeps a priced observation\'s known cost when a $0 observation sits beside it', async () => {
const delta = { inputTokens: 1000, outputTokens: 200, cacheReadTokens: 0, cacheWriteTokens: 0, reasoningTokens: 50 }
const obs = (index: number, timestamp: string, costUSD: number) => ({ index, timestamp, ...delta, costUSD, costBasis: 'calculated', supplementaryAccounting: index > 0 })
const lifetime = { inputTokens: 2000, outputTokens: 400, cacheReadTokens: 0, cacheWriteTokens: 0, reasoningTokens: 100 }
const write = (obs1Cost: number) => writeFile(join(cacheDir, 'hermes-session-ledger.v3.json'), JSON.stringify({
version: 3,
cursors: { default: { s1: {
profile: 'default', sessionId: 's1',
lastSeen: { ...lifetime, costUSD: 10, costBasis: 'calculated' },
observations: [obs(0, '2026-10-01T10:00:00.000Z', 10), obs(1, '2026-11-05T10:00:00.000Z', obs1Cost)],
} } },
}))
const costs = async (snapshotCost: number) => {
await rm(hermesSessionLedgerPath(), { force: true })
resetHermesSessionLedgerForTests()
const cursor = await recordHermesSnapshot(snap({ costUSD: snapshotCost, costBasis: 'calculated', tokens: lifetime }))
return cursor.observations.map(o => o.costUSD)
}
await write(0)
expect(await costs(10)).toEqual([10, 0])
expect(await costs(6)).toEqual([6, 0])
expect(await costs(14)).toEqual([10, 4])
})
it('ignores a superseded v1 ledger and removes it from the cache dir', async () => {
const v1Path = join(cacheDir, 'hermes-session-ledger.v1.json')
await writeFile(v1Path, JSON.stringify({