feat(sessions): show token usage and estimated cost in logs (#557)

* feat(sessions): show token usage and estimated cost in logs

* fix(sessions): compact the usage footer

* fix(ci): bypass Bun cache for Windows ARM64
This commit is contained in:
Yiling-J
2026-12-01 20:49:07 +08:00
committed by GitHub
parent e3fe5792b9
commit bfeabb8244
16 changed files with 292 additions and 60 deletions
@@ -60,6 +60,16 @@ describe('Native session storage and PostgreSQL synchronization', () => {
expect(await prisma.agentSessionEntry.count({ where: { sessionId: session.id } })).toBe(0)
})
it('returns logs with unavailable usage before a session has an accounting checkpoint', async () => {
const { session } = await fixture()
expect(await agentSessionService.getLogs(session.id)).toEqual({ entries: [], usage: null })
const entries = await appendSessionEntries(session.id, [
{ kind: 'pi.user', model: [{ role: 'user', content: 'No usage yet', timestamp: 1 }] },
])
expect(await agentSessionService.getLogs(session.id)).toEqual({ entries, usage: null })
})
it.each(['missing', 'corrupt', 'dirty'] as const)(
'rebuilds a %s local cache from mirrored commits',
async (failure) => {
@@ -438,6 +448,10 @@ describe('Native session storage and PostgreSQL synchronization', () => {
expect(Number(usage.totalTokens)).toBeGreaterThan(0)
expect(Number(usage.totalTokens)).toBe(checkpoint.totalTokens)
expect(Number(usage.cost)).toBe(checkpoint.cost)
const logs = await agentSessionService.getLogs(session.id)
expect(logs.entries).not.toHaveLength(0)
expect(logs.entries).toEqual(await agentSessionService.getEntries(session.id))
expect(logs.usage).toEqual(checkpoint)
})
it('rolls back usage if PostgreSQL cannot persist the native commit', async () => {
@@ -476,5 +490,6 @@ describe('Native session storage and PostgreSQL synchronization', () => {
await prisma.agentSession.delete({ where: { id: session.id } })
expect(await prisma.agentSessionCommit.count({ where: { sessionId: session.id } })).toBe(0)
await expect(agentSessionService.getEntries(session.id)).rejects.toThrow('Session not found')
await expect(agentSessionService.getLogs(session.id)).rejects.toThrow('Session not found')
})
})