mirror of
https://github.com/wu736139669/hapi.git
synced 2026-12-02 06:37:03 +00:00
feat(hub,cli): reconcile OpenCode usage from its own token ledger
The live pipeline records only the final model step of an OpenCode turn, so the usage dashboard showed roughly 42% of OpenCode's own totals. Add an offline reconciliation ledger instead of touching the streaming path (a previous attempt that fed cumulative counters into the live pipeline made the chat/status UI numbers jump): - shared: read and aggregate `~/.local/share/opencode/opencode.db` per (day, model), exposed as `@hapi/protocol/opencodeUsage` - hub: additive `usage_reconciliation` table (no schema bump, safe to roll back), local scan at startup and every 30 minutes, and the usage summary prefers snapshots over live events for reconciled sessions - cli: each runner scans its machine's store every 30 minutes and reports snapshots over the new `opencode-usage-report` socket event, covering remote machines and sessions whose process already exited - the hub-local scan skips sessions whose store lives on another machine, so it can never wipe remotely reported rows
This commit is contained in:
@@ -9,6 +9,7 @@ import { basename, dirname, isAbsolute, join, relative, resolve as resolvePath }
|
||||
import { logger } from '@/ui/logger'
|
||||
import { configuration } from '@/configuration'
|
||||
import type { ClientToServerEvents, ServerToClientEvents, Update, UpdateMachineBody } from '@hapi/protocol'
|
||||
import type { OpencodeUsageSessionReport } from '@hapi/protocol/usage'
|
||||
import {
|
||||
ArchiveCodexSessionRpcRequestSchema,
|
||||
ListClaudeSessionsRpcRequestSchema,
|
||||
@@ -649,6 +650,14 @@ export class ApiMachineClient {
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* Report this machine's OpenCode usage snapshots to the hub. Best-effort:
|
||||
* a dropped report is re-sent by the next scan interval.
|
||||
*/
|
||||
reportOpencodeUsage(sessions: OpencodeUsageSessionReport[]): void {
|
||||
this.socket.emit('opencode-usage-report', { machineId: this.machine.id, sessions })
|
||||
}
|
||||
|
||||
connect(): void {
|
||||
this.socket = io(`${configuration.apiUrl}/cli`, {
|
||||
transports: ['websocket'],
|
||||
|
||||
@@ -0,0 +1,87 @@
|
||||
import { afterEach, describe, expect, it, vi } from 'vitest'
|
||||
import type { OpencodeUsageSessionReport } from '@hapi/protocol/usage'
|
||||
import { collectOpencodeUsageReports, startOpencodeUsageScanner } from './opencodeUsageScanner'
|
||||
|
||||
// The real reader needs Bun's sqlite built-in, which this vitest (node)
|
||||
// environment lacks; the reader itself is covered by the hub's bun tests.
|
||||
// This suite only exercises the scanner's collection/filter/timer plumbing.
|
||||
const mocks = vi.hoisted(() => ({
|
||||
list: vi.fn(),
|
||||
read: vi.fn()
|
||||
}))
|
||||
|
||||
vi.mock('@hapi/protocol/opencodeUsage', () => ({
|
||||
resolveOpencodeDbPath: () => '/tmp/hapi-opencode-scanner-test/opencode.db',
|
||||
listOpencodeSessionIds: (...args: unknown[]) => mocks.list(...args),
|
||||
readOpencodeUsageForSessions: (...args: unknown[]) => mocks.read(...args)
|
||||
}))
|
||||
|
||||
const row = {
|
||||
day: '2026-09-17',
|
||||
model: 'opencode-go/deepseek-v4.1-flash',
|
||||
inputTokens: 1_000,
|
||||
outputTokens: 15,
|
||||
cacheReadTokens: 900,
|
||||
cacheCreationTokens: 0,
|
||||
requests: 1
|
||||
}
|
||||
|
||||
afterEach(() => {
|
||||
vi.useRealTimers()
|
||||
mocks.list.mockReset()
|
||||
mocks.read.mockReset()
|
||||
})
|
||||
|
||||
describe('opencode usage scanner', () => {
|
||||
it('collects reports only for sessions with usage rows', async () => {
|
||||
mocks.list.mockResolvedValue(['ses_a', 'ses_b'])
|
||||
mocks.read.mockResolvedValue(new Map([
|
||||
['ses_a', { rows: [row], messages: 3 }],
|
||||
['ses_b', { rows: [], messages: 0 }]
|
||||
]))
|
||||
|
||||
await expect(collectOpencodeUsageReports('/tmp/opencode.db')).resolves.toEqual([
|
||||
{ opencodeSessionId: 'ses_a', rows: [row] }
|
||||
])
|
||||
})
|
||||
|
||||
it('reports on its interval and stops cleanly', async () => {
|
||||
vi.useFakeTimers()
|
||||
mocks.list.mockResolvedValue(['ses_a'])
|
||||
mocks.read.mockResolvedValue(new Map([['ses_a', { rows: [row], messages: 3 }]]))
|
||||
const reports: OpencodeUsageSessionReport[][] = []
|
||||
const scanner = startOpencodeUsageScanner({
|
||||
dbPath: '/tmp/opencode.db',
|
||||
initialDelayMs: 0,
|
||||
intervalMs: 1_000,
|
||||
report: (sessions) => reports.push(sessions)
|
||||
})
|
||||
|
||||
await vi.advanceTimersByTimeAsync(1)
|
||||
expect(reports).toEqual([[{ opencodeSessionId: 'ses_a', rows: [row] }]])
|
||||
|
||||
await vi.advanceTimersByTimeAsync(1_000)
|
||||
expect(reports).toHaveLength(2)
|
||||
|
||||
scanner.stop()
|
||||
await vi.advanceTimersByTimeAsync(5_000)
|
||||
expect(reports).toHaveLength(2)
|
||||
})
|
||||
|
||||
it('stays silent when the store has no sessions', async () => {
|
||||
vi.useFakeTimers()
|
||||
mocks.list.mockResolvedValue([])
|
||||
const reports: OpencodeUsageSessionReport[][] = []
|
||||
const scanner = startOpencodeUsageScanner({
|
||||
dbPath: '/tmp/opencode.db',
|
||||
initialDelayMs: 0,
|
||||
intervalMs: 1_000,
|
||||
report: (sessions) => reports.push(sessions)
|
||||
})
|
||||
|
||||
await vi.advanceTimersByTimeAsync(2_000)
|
||||
expect(reports).toHaveLength(0)
|
||||
expect(mocks.read).not.toHaveBeenCalled()
|
||||
scanner.stop()
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,81 @@
|
||||
import { listOpencodeSessionIds, readOpencodeUsageForSessions, resolveOpencodeDbPath } from '@hapi/protocol/opencodeUsage'
|
||||
import type { OpencodeUsageSessionReport } from '@hapi/protocol/usage'
|
||||
import { logger } from '@/ui/logger'
|
||||
|
||||
/**
|
||||
* Machine-level OpenCode usage reconciliation.
|
||||
*
|
||||
* The hub can only read its own OpenCode store, so each runner scans its
|
||||
* machine's store and reports absolute per-(day, model) snapshots for every
|
||||
* session it finds there — including sessions whose process already exited.
|
||||
* The hub maps the OpenCode session ids back to HAPI sessions and replaces
|
||||
* their reconciliation rows, which makes repeated or concurrent reports
|
||||
* idempotent (see hub/src/sync/usageReconciliation.ts).
|
||||
*
|
||||
* The live chat pipeline is not involved at all — a report never becomes a
|
||||
* timeline message.
|
||||
*/
|
||||
|
||||
const SCAN_INTERVAL_MS = 30 * 60 * 1000
|
||||
const INITIAL_DELAY_MS = 20_000
|
||||
|
||||
export type OpencodeUsageScannerOptions = {
|
||||
/** Best-effort delivery; the next scan re-sends on failure. */
|
||||
report: (sessions: OpencodeUsageSessionReport[]) => void
|
||||
/** Override for tests; defaults to the XDG OpenCode store path. */
|
||||
dbPath?: string | null
|
||||
intervalMs?: number
|
||||
initialDelayMs?: number
|
||||
}
|
||||
|
||||
export type OpencodeUsageScanner = {
|
||||
/** Idempotent. */
|
||||
stop: () => void
|
||||
/** Run one scan immediately (used by tests). */
|
||||
runNow: () => void
|
||||
}
|
||||
|
||||
/** Collect snapshot rows for every session present in the OpenCode store. */
|
||||
export async function collectOpencodeUsageReports(dbPath: string): Promise<OpencodeUsageSessionReport[]> {
|
||||
const opencodeSessionIds = await listOpencodeSessionIds(dbPath)
|
||||
if (opencodeSessionIds.length === 0) return []
|
||||
const usageBySession = await readOpencodeUsageForSessions(dbPath, opencodeSessionIds)
|
||||
const reports: OpencodeUsageSessionReport[] = []
|
||||
for (const opencodeSessionId of opencodeSessionIds) {
|
||||
const usage = usageBySession.get(opencodeSessionId)
|
||||
if (!usage || usage.rows.length === 0) continue
|
||||
reports.push({ opencodeSessionId, rows: usage.rows })
|
||||
}
|
||||
return reports
|
||||
}
|
||||
|
||||
export function startOpencodeUsageScanner(opts: OpencodeUsageScannerOptions): OpencodeUsageScanner {
|
||||
let stopped = false
|
||||
const run = (): void => {
|
||||
if (stopped) return
|
||||
const dbPath = opts.dbPath === undefined ? resolveOpencodeDbPath() : opts.dbPath
|
||||
if (!dbPath) return
|
||||
void collectOpencodeUsageReports(dbPath)
|
||||
.then((reports) => {
|
||||
if (stopped || reports.length === 0) return
|
||||
opts.report(reports)
|
||||
})
|
||||
.catch((error) => {
|
||||
logger.debug('[opencode-usage] machine reconciliation report failed', error)
|
||||
})
|
||||
}
|
||||
|
||||
const initial = setTimeout(run, opts.initialDelayMs ?? INITIAL_DELAY_MS)
|
||||
const interval = setInterval(run, opts.intervalMs ?? SCAN_INTERVAL_MS)
|
||||
initial.unref?.()
|
||||
interval.unref?.()
|
||||
|
||||
return {
|
||||
stop: () => {
|
||||
stopped = true
|
||||
clearTimeout(initial)
|
||||
clearInterval(interval)
|
||||
},
|
||||
runNow: run
|
||||
}
|
||||
}
|
||||
@@ -19,6 +19,7 @@ import { PERMISSION_MODES } from '@hapi/protocol/modes';
|
||||
import { RUNNER_CAPABILITIES } from '@hapi/protocol';
|
||||
import { withRetry } from '@/utils/time';
|
||||
import { isRetryableConnectionError } from '@/utils/errorUtils';
|
||||
import { startOpencodeUsageScanner } from './opencodeUsageScanner';
|
||||
|
||||
import { cleanupRunnerState, getInstalledCliMtimeMs, isRunnerRunningCurrentlyInstalledHappyVersion, stopRunner, waitForRunnerHandoff } from './controlClient';
|
||||
import { startRunnerControlServer } from './controlServer';
|
||||
@@ -1164,6 +1165,12 @@ export async function startRunner(options: { workspaceRoots?: string[] } = {}):
|
||||
|
||||
// Connect to server
|
||||
apiMachine.connect();
|
||||
// Report this machine's OpenCode usage snapshots so the hub's token
|
||||
// dashboard does not under-count tool-loop turns on non-hub machines.
|
||||
// The scan reads the local OpenCode store; best-effort, never blocks.
|
||||
const opencodeUsageScanner = startOpencodeUsageScanner({
|
||||
report: (sessions) => apiMachine.reportOpencodeUsage(sessions)
|
||||
});
|
||||
scheduleCursorModelsPrewarm();
|
||||
|
||||
// Visible startup banner. Use console.log so it always appears on stdout,
|
||||
@@ -1429,6 +1436,7 @@ export async function startRunner(options: { workspaceRoots?: string[] } = {}):
|
||||
clearInterval(restartOnStaleVersionAndHeartbeat);
|
||||
logger.debug('[RUNNER RUN] Health check interval cleared');
|
||||
}
|
||||
opencodeUsageScanner.stop();
|
||||
|
||||
// Update runner state before shutting down
|
||||
await apiMachine.updateRunnerState((state: RunnerState | null) => ({
|
||||
|
||||
@@ -0,0 +1,107 @@
|
||||
import { describe, expect, it } from 'bun:test'
|
||||
import { EventEmitter } from 'node:events'
|
||||
import { Store, type StoredMachine } from '../../../store'
|
||||
import type { CliSocketWithData } from '../../socketTypes'
|
||||
import { registerMachineHandlers } from './machineHandlers'
|
||||
|
||||
function harness(options: { access: 'ok' | 'denied' }) {
|
||||
const socket = new EventEmitter() as unknown as CliSocketWithData
|
||||
const store = new Store(':memory:')
|
||||
const accessErrors: Array<{ scope: string; id: string; reason: string }> = []
|
||||
|
||||
registerMachineHandlers(socket, {
|
||||
store,
|
||||
resolveMachineAccess: () => (
|
||||
options.access === 'ok'
|
||||
? { ok: true, value: { namespace: 'default' } as unknown as StoredMachine }
|
||||
: { ok: false, reason: 'access-denied' }
|
||||
),
|
||||
emitAccessError: (scope, id, reason) => { accessErrors.push({ scope, id, reason }) }
|
||||
})
|
||||
|
||||
return { socket: socket as unknown as EventEmitter, store, accessErrors }
|
||||
}
|
||||
|
||||
const row = {
|
||||
day: '2026-09-17',
|
||||
model: 'opencode-go/deepseek-v4.1-flash',
|
||||
inputTokens: 100,
|
||||
outputTokens: 10,
|
||||
cacheReadTokens: 80,
|
||||
cacheCreationTokens: 0,
|
||||
requests: 2
|
||||
}
|
||||
|
||||
describe('opencode-usage-report', () => {
|
||||
it('replaces the matching session snapshot by OpenCode session id', () => {
|
||||
const { socket, store } = harness({ access: 'ok' })
|
||||
const session = store.sessions.getOrCreateSession(
|
||||
'opencode-machine-report',
|
||||
{ path: '/tmp', host: 'k2lab', flavor: 'opencode', opencodeSessionId: 'ses_a' },
|
||||
null,
|
||||
'default'
|
||||
)
|
||||
|
||||
socket.emit('opencode-usage-report', {
|
||||
machineId: 'machine-1',
|
||||
sessions: [{ opencodeSessionId: 'ses_a', rows: [row] }]
|
||||
})
|
||||
|
||||
expect(store.usage.getReconciledByNamespace('default')).toEqual([
|
||||
expect.objectContaining({ sessionId: session.id, day: '2026-09-17', requests: 2 })
|
||||
])
|
||||
})
|
||||
|
||||
it('ignores reports for machines without access', () => {
|
||||
const { socket, store, accessErrors } = harness({ access: 'denied' })
|
||||
|
||||
socket.emit('opencode-usage-report', {
|
||||
machineId: 'someone-elses-machine',
|
||||
sessions: [{ opencodeSessionId: 'ses_a', rows: [row] }]
|
||||
})
|
||||
|
||||
expect(store.usage.getReconciledByNamespace('default')).toHaveLength(0)
|
||||
expect(accessErrors).toEqual([
|
||||
{ scope: 'machine', id: 'someone-elses-machine', reason: 'access-denied' }
|
||||
])
|
||||
})
|
||||
|
||||
it('skips unknown session ids and empty row sets without touching snapshots', () => {
|
||||
const { socket, store } = harness({ access: 'ok' })
|
||||
const session = store.sessions.getOrCreateSession(
|
||||
'opencode-machine-report-known',
|
||||
{ path: '/tmp', host: 'k2lab', flavor: 'opencode', opencodeSessionId: 'ses_known' },
|
||||
null,
|
||||
'default'
|
||||
)
|
||||
store.usage.replaceReconciled(session.id, 'default', [
|
||||
{ ...row, sessionId: session.id, agent: 'opencode' }
|
||||
], Date.now())
|
||||
|
||||
socket.emit('opencode-usage-report', {
|
||||
machineId: 'machine-1',
|
||||
sessions: [
|
||||
{ opencodeSessionId: 'ses_unknown', rows: [row] },
|
||||
{ opencodeSessionId: 'ses_known', rows: [] }
|
||||
]
|
||||
})
|
||||
|
||||
expect(store.usage.getReconciledByNamespace('default')).toEqual([
|
||||
expect.objectContaining({ sessionId: session.id, requests: 2 })
|
||||
])
|
||||
})
|
||||
|
||||
it('drops malformed payloads without erroring', () => {
|
||||
const { socket, store, accessErrors } = harness({ access: 'ok' })
|
||||
|
||||
socket.emit('opencode-usage-report', {})
|
||||
socket.emit('opencode-usage-report', null)
|
||||
socket.emit('opencode-usage-report', {
|
||||
machineId: 'machine-1',
|
||||
sessions: [{ opencodeSessionId: 'ses_a', rows: [{ day: 'nope' }] }]
|
||||
})
|
||||
|
||||
expect(store.usage.getReconciledByNamespace('default')).toHaveLength(0)
|
||||
expect(accessErrors).toEqual([])
|
||||
})
|
||||
})
|
||||
@@ -35,6 +35,29 @@ const machineUpdateStateSchema = z.object({
|
||||
runnerState: z.unknown().nullable()
|
||||
})
|
||||
|
||||
/**
|
||||
* Absolute OpenCode usage snapshots read from the reporting machine's own
|
||||
* store (the hub cannot read another machine's OpenCode store). Each entry
|
||||
* replaces the matching HAPI session's reconciliation snapshot; entries are
|
||||
* matched by the OpenCode session id recorded in the session metadata.
|
||||
* Reports never flow through the chat pipeline.
|
||||
*/
|
||||
const opencodeUsageReportSchema = z.object({
|
||||
machineId: z.string().min(1),
|
||||
sessions: z.array(z.object({
|
||||
opencodeSessionId: z.string().min(1),
|
||||
rows: z.array(z.object({
|
||||
day: z.string().regex(/^\d{4}-\d{2}-\d{2}$/),
|
||||
model: z.string().min(1).max(200),
|
||||
inputTokens: z.number().int().nonnegative(),
|
||||
outputTokens: z.number().int().nonnegative(),
|
||||
cacheReadTokens: z.number().int().nonnegative(),
|
||||
cacheCreationTokens: z.number().int().nonnegative(),
|
||||
requests: z.number().int().nonnegative()
|
||||
})).max(1000)
|
||||
})).max(500)
|
||||
})
|
||||
|
||||
export type MachineHandlersDeps = {
|
||||
store: Store
|
||||
resolveMachineAccess: ResolveMachineAccess
|
||||
@@ -159,4 +182,43 @@ export function registerMachineHandlers(socket: CliSocketWithData, deps: Machine
|
||||
|
||||
socket.on('machine-update-metadata', handleMachineMetadataUpdate)
|
||||
socket.on('machine-update-state', handleMachineStateUpdate)
|
||||
|
||||
socket.on('opencode-usage-report', (data) => {
|
||||
const parsed = opencodeUsageReportSchema.safeParse(data)
|
||||
if (!parsed.success) return
|
||||
const { machineId, sessions } = parsed.data
|
||||
const machineAccess = resolveMachineAccess(machineId)
|
||||
if (!machineAccess.ok) {
|
||||
emitAccessError('machine', machineId, machineAccess.reason)
|
||||
return
|
||||
}
|
||||
const namespace = machineAccess.value.namespace
|
||||
|
||||
// Map the machine's OpenCode session ids onto HAPI sessions once per
|
||||
// report; the runner reports every session in its store, including
|
||||
// ones whose process already exited.
|
||||
const sessionIdByOpencodeId = new Map<string, string>()
|
||||
for (const session of store.sessions.getSessionsByNamespace(namespace)) {
|
||||
const metadata = session.metadata !== null && typeof session.metadata === 'object' && !Array.isArray(session.metadata)
|
||||
? session.metadata as Record<string, unknown>
|
||||
: null
|
||||
const opencodeSessionId = metadata && typeof metadata.opencodeSessionId === 'string'
|
||||
? metadata.opencodeSessionId
|
||||
: null
|
||||
if (opencodeSessionId) sessionIdByOpencodeId.set(opencodeSessionId, session.id)
|
||||
}
|
||||
|
||||
const now = Date.now()
|
||||
for (const entry of sessions) {
|
||||
if (entry.rows.length === 0) continue
|
||||
const sessionId = sessionIdByOpencodeId.get(entry.opencodeSessionId)
|
||||
if (!sessionId) continue
|
||||
store.usage.replaceReconciled(
|
||||
sessionId,
|
||||
namespace,
|
||||
entry.rows.map((row) => ({ ...row, sessionId, agent: 'opencode' as const })),
|
||||
now
|
||||
)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
@@ -22,6 +22,7 @@ import { IosPushService } from './push-ios/iosPushService'
|
||||
import { IosPushNotificationChannel } from './push-ios/iosPushChannel'
|
||||
import { resolveIosPushConfig } from './push-ios/iosPushConfig'
|
||||
import { VisibilityTracker } from './visibility/visibilityTracker'
|
||||
import { startOpencodeUsageReconciliation, type UsageReconciliationJob } from './sync/usageReconciliation'
|
||||
import { TunnelManager } from './tunnel'
|
||||
import { refreshRejectedRelayAuthKey, resolveRelayAuthKey } from './tunnel/relayAuth'
|
||||
import { waitForTunnelTlsReady } from './tunnel/tlsGate'
|
||||
@@ -122,6 +123,7 @@ export async function startHub(options: StartHubOptions = {}): Promise<HubInstan
|
||||
let visibilityTracker: VisibilityTracker | null = null
|
||||
let notificationHub: NotificationHub | null = null
|
||||
let tunnelManager: TunnelManager | null = null
|
||||
let usageReconciliation: UsageReconciliationJob | null = null
|
||||
|
||||
// Load configuration (async - loads from env/file with persistence)
|
||||
const relayApiDomain = process.env.HAPI_RELAY_API || 'relay.hapi.run'
|
||||
@@ -183,6 +185,9 @@ export async function startHub(options: StartHubOptions = {}): Promise<HubInstan
|
||||
}
|
||||
|
||||
const store = new Store(config.dbPath)
|
||||
// Periodic offline usage reconciliation (OpenCode reports only the final
|
||||
// step of a turn through the live pipeline; see usageReconciliation.ts).
|
||||
usageReconciliation = startOpencodeUsageReconciliation(store)
|
||||
const jwtSecret = await getOrCreateJwtSecret()
|
||||
const vapidKeys = await getOrCreateVapidKeys(config.dataDir)
|
||||
const vapidSubject = process.env.VAPID_SUBJECT ?? 'mailto:admin@hapi.run'
|
||||
@@ -511,6 +516,7 @@ export async function startHub(options: StartHubOptions = {}): Promise<HubInstan
|
||||
|
||||
return {
|
||||
stop: async () => {
|
||||
usageReconciliation?.stop()
|
||||
await tunnelManager?.stop()
|
||||
await happyBot?.stop()
|
||||
notificationHub?.stop()
|
||||
|
||||
@@ -124,6 +124,7 @@ export class Store {
|
||||
this.db.exec('PRAGMA busy_timeout = 5000')
|
||||
this.initSchema()
|
||||
this.ensureSessionShareSchema()
|
||||
this.ensureUsageReconciliationSchema()
|
||||
|
||||
if (dbPath !== ':memory:' && !dbPath.startsWith('file::memory:')) {
|
||||
for (const path of [dbPath, `${dbPath}-wal`, `${dbPath}-shm`]) {
|
||||
@@ -1248,6 +1249,36 @@ export class Store {
|
||||
`)
|
||||
}
|
||||
|
||||
/**
|
||||
* Additive table for the offline usage reconciliation snapshots. Created
|
||||
* outside the migration ladder on purpose: it is derived data (like
|
||||
* session_shares) and an older hub build must keep opening this database
|
||||
* untouched, so the schema version is not bumped. The live pipeline keeps
|
||||
* its own `usage_events` ledger; the summary prefers reconciliation rows
|
||||
* for reconciled sessions and never mixes both sources. See
|
||||
* sync/usageReconciliation.ts.
|
||||
*/
|
||||
private ensureUsageReconciliationSchema(): void {
|
||||
this.db.exec(`
|
||||
CREATE TABLE IF NOT EXISTS usage_reconciliation (
|
||||
namespace TEXT NOT NULL DEFAULT 'default',
|
||||
session_id TEXT NOT NULL,
|
||||
day TEXT NOT NULL,
|
||||
model TEXT NOT NULL,
|
||||
agent TEXT NOT NULL,
|
||||
input_tokens INTEGER NOT NULL DEFAULT 0,
|
||||
output_tokens INTEGER NOT NULL DEFAULT 0,
|
||||
cache_read_tokens INTEGER NOT NULL DEFAULT 0,
|
||||
cache_creation_tokens INTEGER NOT NULL DEFAULT 0,
|
||||
requests INTEGER NOT NULL DEFAULT 0,
|
||||
updated_at INTEGER NOT NULL,
|
||||
PRIMARY KEY (session_id, day, model)
|
||||
);
|
||||
CREATE INDEX IF NOT EXISTS idx_usage_reconciliation_namespace_day
|
||||
ON usage_reconciliation(namespace, day);
|
||||
`)
|
||||
}
|
||||
|
||||
private getSessionColumnNames(): Set<string> {
|
||||
const rows = this.db.prepare('PRAGMA table_info(sessions)').all() as Array<{ name: string }>
|
||||
return new Set(rows.map((row) => row.name))
|
||||
|
||||
@@ -25,6 +25,24 @@ export type UsageScanState = {
|
||||
lastSeq: number
|
||||
}
|
||||
|
||||
/**
|
||||
* Absolute per-(day, model) usage snapshot for a session, produced by the
|
||||
* offline reconciliation job (see sync/usageReconciliation.ts) rather than by
|
||||
* the live message pipeline. Values are totals, never deltas: replacing a
|
||||
* session's rows is idempotent, so the job can re-run at any cadence.
|
||||
*/
|
||||
export type ReconciledUsageRow = {
|
||||
sessionId: string
|
||||
day: string
|
||||
model: string
|
||||
agent: string
|
||||
inputTokens: number
|
||||
outputTokens: number
|
||||
cacheReadTokens: number
|
||||
cacheCreationTokens: number
|
||||
requests: number
|
||||
}
|
||||
|
||||
type UsageEventRow = {
|
||||
namespace: string
|
||||
session_id: string
|
||||
@@ -301,5 +319,135 @@ export function transferUsageSession(db: Database, fromSessionId: string, toSess
|
||||
`).run(toSessionId, toSessionId, fromSessionId)
|
||||
db.prepare('DELETE FROM usage_events WHERE session_id = ?').run(fromSessionId)
|
||||
db.prepare('DELETE FROM usage_scan_state WHERE session_id IN (?, ?)').run(fromSessionId, toSessionId)
|
||||
db.prepare(`
|
||||
INSERT OR REPLACE INTO usage_reconciliation (
|
||||
namespace,
|
||||
session_id,
|
||||
day,
|
||||
model,
|
||||
agent,
|
||||
input_tokens,
|
||||
output_tokens,
|
||||
cache_read_tokens,
|
||||
cache_creation_tokens,
|
||||
requests,
|
||||
updated_at
|
||||
)
|
||||
SELECT
|
||||
?,
|
||||
?,
|
||||
day,
|
||||
model,
|
||||
agent,
|
||||
input_tokens,
|
||||
output_tokens,
|
||||
cache_read_tokens,
|
||||
cache_creation_tokens,
|
||||
requests,
|
||||
updated_at
|
||||
FROM usage_reconciliation
|
||||
WHERE session_id = ?
|
||||
`).run(
|
||||
(db.prepare('SELECT namespace FROM sessions WHERE id = ?').get(toSessionId) as { namespace: string } | undefined)?.namespace ?? 'default',
|
||||
toSessionId,
|
||||
fromSessionId
|
||||
)
|
||||
db.prepare('DELETE FROM usage_reconciliation WHERE session_id = ?').run(fromSessionId)
|
||||
})()
|
||||
}
|
||||
|
||||
/** Replace a session's reconciliation snapshot, deleting any previous rows. */
|
||||
export function replaceReconciledUsage(
|
||||
db: Database,
|
||||
namespace: string,
|
||||
sessionId: string,
|
||||
rows: ReconciledUsageRow[],
|
||||
updatedAt: number
|
||||
): void {
|
||||
db.transaction(() => {
|
||||
db.prepare('DELETE FROM usage_reconciliation WHERE session_id = ?').run(sessionId)
|
||||
if (rows.length === 0) return
|
||||
const statement = db.prepare(`
|
||||
INSERT INTO usage_reconciliation (
|
||||
namespace,
|
||||
session_id,
|
||||
day,
|
||||
model,
|
||||
agent,
|
||||
input_tokens,
|
||||
output_tokens,
|
||||
cache_read_tokens,
|
||||
cache_creation_tokens,
|
||||
requests,
|
||||
updated_at
|
||||
) VALUES (
|
||||
@namespace,
|
||||
@session_id,
|
||||
@day,
|
||||
@model,
|
||||
@agent,
|
||||
@input_tokens,
|
||||
@output_tokens,
|
||||
@cache_read_tokens,
|
||||
@cache_creation_tokens,
|
||||
@requests,
|
||||
@updated_at
|
||||
)
|
||||
`)
|
||||
for (const row of rows) {
|
||||
statement.run({
|
||||
namespace,
|
||||
session_id: row.sessionId,
|
||||
day: row.day,
|
||||
model: row.model,
|
||||
agent: row.agent,
|
||||
input_tokens: row.inputTokens,
|
||||
output_tokens: row.outputTokens,
|
||||
cache_read_tokens: row.cacheReadTokens,
|
||||
cache_creation_tokens: row.cacheCreationTokens,
|
||||
requests: row.requests,
|
||||
updated_at: updatedAt
|
||||
})
|
||||
}
|
||||
})()
|
||||
}
|
||||
|
||||
export function getReconciledUsageByNamespace(db: Database, namespace: string): ReconciledUsageRow[] {
|
||||
const rows = db.prepare(`
|
||||
SELECT
|
||||
session_id,
|
||||
day,
|
||||
model,
|
||||
agent,
|
||||
input_tokens,
|
||||
output_tokens,
|
||||
cache_read_tokens,
|
||||
cache_creation_tokens,
|
||||
requests
|
||||
FROM usage_reconciliation
|
||||
WHERE namespace = ?
|
||||
ORDER BY day ASC, session_id ASC, model ASC
|
||||
`).all(namespace) as Array<{
|
||||
session_id: string
|
||||
day: string
|
||||
model: string
|
||||
agent: string
|
||||
input_tokens: number
|
||||
output_tokens: number
|
||||
cache_read_tokens: number
|
||||
cache_creation_tokens: number
|
||||
requests: number
|
||||
}>
|
||||
|
||||
return rows.map((row) => ({
|
||||
sessionId: row.session_id,
|
||||
day: row.day,
|
||||
model: row.model,
|
||||
agent: row.agent,
|
||||
inputTokens: row.input_tokens,
|
||||
outputTokens: row.output_tokens,
|
||||
cacheReadTokens: row.cache_read_tokens,
|
||||
cacheCreationTokens: row.cache_creation_tokens,
|
||||
requests: row.requests
|
||||
}))
|
||||
}
|
||||
|
||||
@@ -1,11 +1,14 @@
|
||||
import type { Database } from 'bun:sqlite'
|
||||
|
||||
import {
|
||||
getReconciledUsageByNamespace,
|
||||
getUsageEvents,
|
||||
getUsageEventsByNamespace,
|
||||
getUsageScanStates,
|
||||
recordUsageScan,
|
||||
replaceReconciledUsage,
|
||||
transferUsageSession,
|
||||
type ReconciledUsageRow,
|
||||
type UsageEvent,
|
||||
type UsageScanState
|
||||
} from './usage'
|
||||
@@ -39,4 +42,12 @@ export class UsageStore {
|
||||
transferSession(fromSessionId: string, toSessionId: string): void {
|
||||
transferUsageSession(this.db, fromSessionId, toSessionId)
|
||||
}
|
||||
|
||||
replaceReconciled(sessionId: string, namespace: string, rows: ReconciledUsageRow[], updatedAt: number): void {
|
||||
replaceReconciledUsage(this.db, namespace, sessionId, rows, updatedAt)
|
||||
}
|
||||
|
||||
getReconciledByNamespace(namespace: string): ReconciledUsageRow[] {
|
||||
return getReconciledUsageByNamespace(this.db, namespace)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,190 @@
|
||||
import { Database } from 'bun:sqlite'
|
||||
import { afterEach, describe, expect, it } from 'bun:test'
|
||||
import { mkdtempSync, rmSync } from 'node:fs'
|
||||
import { tmpdir } from 'node:os'
|
||||
import { join } from 'node:path'
|
||||
import { parseOpencodeAssistantUsage, resolveOpencodeDbPath } from '@hapi/protocol/opencodeUsage'
|
||||
import { Store } from '../store'
|
||||
import { reconcileOpencodeUsage } from './usageReconciliation'
|
||||
import { getUsageSummary } from './usageService'
|
||||
|
||||
const tempDirs: string[] = []
|
||||
|
||||
afterEach(() => {
|
||||
for (const dir of tempDirs.splice(0)) {
|
||||
rmSync(dir, { recursive: true, force: true })
|
||||
}
|
||||
})
|
||||
|
||||
function createOpencodeStore(messages: Array<{ id: string; sessionId: string; data: unknown }>): string {
|
||||
const dir = mkdtempSync(join(tmpdir(), 'hapi-opencode-usage-'))
|
||||
tempDirs.push(dir)
|
||||
const dbPath = join(dir, 'opencode.db')
|
||||
const db = new Database(dbPath, { create: true })
|
||||
db.exec(`
|
||||
CREATE TABLE message (
|
||||
id TEXT PRIMARY KEY,
|
||||
session_id TEXT NOT NULL,
|
||||
time_created INTEGER NOT NULL,
|
||||
time_updated INTEGER NOT NULL,
|
||||
data TEXT NOT NULL
|
||||
)
|
||||
`)
|
||||
const insert = db.prepare(
|
||||
'INSERT INTO message (id, session_id, time_created, time_updated, data) VALUES (?, ?, ?, ?, ?)'
|
||||
)
|
||||
for (const message of messages) {
|
||||
const created = (message.data as { time?: { created?: number } }).time?.created ?? 0
|
||||
insert.run(message.id, message.sessionId, created, created, JSON.stringify(message.data))
|
||||
}
|
||||
db.close()
|
||||
return dbPath
|
||||
}
|
||||
|
||||
describe('usage reconciliation', () => {
|
||||
it('snapshots OpenCode per-day totals and stays idempotent', async () => {
|
||||
const store = new Store(':memory:')
|
||||
const session = store.sessions.getOrCreateSession(
|
||||
'opencode-reconcile-test',
|
||||
{ path: '/tmp', host: 'test', flavor: 'opencode', opencodeSessionId: 'ses_reconcile' },
|
||||
null,
|
||||
'default',
|
||||
'opencode-go/deepseek-v4.1-flash'
|
||||
)
|
||||
|
||||
const dbPath = createOpencodeStore([
|
||||
{
|
||||
id: 'msg-1',
|
||||
sessionId: 'ses_reconcile',
|
||||
data: {
|
||||
role: 'assistant',
|
||||
providerID: 'opencode-go',
|
||||
modelID: 'deepseek-v4.1-flash',
|
||||
time: { created: Date.parse('2026-09-17T10:00:00') },
|
||||
tokens: { input: 100, output: 10, reasoning: 5, cache: { read: 900, write: 50 } }
|
||||
}
|
||||
},
|
||||
{
|
||||
id: 'msg-2',
|
||||
sessionId: 'ses_reconcile',
|
||||
data: {
|
||||
role: 'assistant',
|
||||
providerID: 'opencode-go',
|
||||
modelID: 'deepseek-v4.1-flash',
|
||||
time: { created: Date.parse('2026-09-17T10:05:00') },
|
||||
tokens: { input: 200, output: 20, reasoning: 0, cache: { read: 800, write: 0 } }
|
||||
}
|
||||
},
|
||||
{
|
||||
// Zero-token synthetic rows must not count as requests.
|
||||
id: 'msg-3',
|
||||
sessionId: 'ses_reconcile',
|
||||
data: {
|
||||
role: 'assistant',
|
||||
time: { created: Date.parse('2026-09-17T10:06:00') },
|
||||
tokens: { input: 0, output: 0, reasoning: 0, cache: { read: 0, write: 0 } }
|
||||
}
|
||||
},
|
||||
{
|
||||
// Sessions without a HAPI row are ignored entirely.
|
||||
id: 'msg-4',
|
||||
sessionId: 'ses_other',
|
||||
data: {
|
||||
role: 'assistant',
|
||||
time: { created: Date.parse('2026-09-17T10:07:00') },
|
||||
tokens: { input: 999, output: 99 }
|
||||
}
|
||||
}
|
||||
])
|
||||
|
||||
const first = await reconcileOpencodeUsage(store, { dbPath })
|
||||
expect(first.messages).toBe(2)
|
||||
expect(first.sessions).toBe(1)
|
||||
expect(first.rows).toBe(1)
|
||||
|
||||
const summary = getUsageSummary(store, 'default', 'all')
|
||||
expect(summary.totals.inputTokens).toBe(100 + 900 + 50 + 200 + 800)
|
||||
expect(summary.totals.outputTokens).toBe(10 + 5 + 20)
|
||||
expect(summary.totals.cacheReadTokens).toBe(900 + 800)
|
||||
expect(summary.totals.cacheCreationTokens).toBe(50)
|
||||
expect(summary.totals.totalTokens).toBe(2_050 + 35)
|
||||
expect(summary.totals.uncachedTokens).toBe(350 + 35)
|
||||
expect(summary.totals.requests).toBe(2)
|
||||
expect(summary.daily).toHaveLength(1)
|
||||
expect(summary.byModel.find((row) => row.key === 'opencode-go/deepseek-v4.1-flash')).toMatchObject({
|
||||
inputTokens: 2_050,
|
||||
outputTokens: 35,
|
||||
requests: 2
|
||||
})
|
||||
|
||||
// Re-running replaces the snapshot instead of accumulating.
|
||||
await reconcileOpencodeUsage(store, { dbPath })
|
||||
expect(getUsageSummary(store, 'default', 'all').totals).toEqual(summary.totals)
|
||||
store.close()
|
||||
})
|
||||
|
||||
it('keeps remote-reported rows when the local store has no such session', async () => {
|
||||
const store = new Store(':memory:')
|
||||
const remote = store.sessions.getOrCreateSession(
|
||||
'opencode-remote-usage',
|
||||
{ path: '/tmp', host: 'k2lab', flavor: 'opencode', opencodeSessionId: 'ses_remote' },
|
||||
null,
|
||||
'default',
|
||||
'opencode-go/deepseek-v4.1-flash'
|
||||
)
|
||||
// The remote session's machine reported a snapshot over `opencode-usage-report`.
|
||||
store.usage.replaceReconciled(remote.id, 'default', [{
|
||||
sessionId: remote.id,
|
||||
day: '2026-09-17',
|
||||
model: 'opencode-go/deepseek-v4.1-flash',
|
||||
agent: 'opencode',
|
||||
inputTokens: 5_000,
|
||||
outputTokens: 200,
|
||||
cacheReadTokens: 4_700,
|
||||
cacheCreationTokens: 0,
|
||||
requests: 42
|
||||
}], Date.now())
|
||||
|
||||
// The local job scans a store that has no entry for `ses_remote`.
|
||||
const dbPath = createOpencodeStore([{
|
||||
id: 'msg-local',
|
||||
sessionId: 'ses_local_only',
|
||||
data: {
|
||||
role: 'assistant',
|
||||
time: { created: Date.parse('2026-09-17T10:00:00') },
|
||||
tokens: { input: 10, output: 1 }
|
||||
}
|
||||
}])
|
||||
const result = await reconcileOpencodeUsage(store, { dbPath })
|
||||
expect(result.sessions).toBe(0)
|
||||
expect(store.usage.getReconciledByNamespace('default')).toEqual([
|
||||
expect.objectContaining({ sessionId: remote.id, requests: 42, inputTokens: 5_000 })
|
||||
])
|
||||
})
|
||||
|
||||
it('parses only assistant messages with positive token usage', () => { expect(parseOpencodeAssistantUsage('not json')).toBeNull()
|
||||
expect(parseOpencodeAssistantUsage(JSON.stringify({ role: 'user' }))).toBeNull()
|
||||
expect(parseOpencodeAssistantUsage(JSON.stringify({
|
||||
role: 'assistant',
|
||||
time: { created: Date.parse('2026-09-17T10:00:00') },
|
||||
tokens: { input: 0, output: 0, reasoning: 0, cache: { read: 0, write: 0 } }
|
||||
}))).toBeNull()
|
||||
expect(parseOpencodeAssistantUsage(JSON.stringify({
|
||||
role: 'assistant',
|
||||
modelID: 'deepseek-v4.1-flash',
|
||||
time: { created: Date.parse('2026-09-17T10:00:00') },
|
||||
tokens: { input: 3, output: 1 }
|
||||
}))).toMatchObject({
|
||||
model: 'deepseek-v4.1-flash',
|
||||
inputTokens: 3,
|
||||
outputTokens: 1
|
||||
})
|
||||
})
|
||||
|
||||
it('resolves the OpenCode store path from the environment', () => {
|
||||
expect(resolveOpencodeDbPath({ HAPI_OPENCODE_DB: '/custom/opencode.db' } as NodeJS.ProcessEnv))
|
||||
.toBe('/custom/opencode.db')
|
||||
expect(resolveOpencodeDbPath({ XDG_DATA_HOME: '/xdg' } as NodeJS.ProcessEnv))
|
||||
.toBe('/xdg/opencode/opencode.db')
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,128 @@
|
||||
import { existsSync } from 'node:fs'
|
||||
import { readOpencodeUsageForSessions, resolveOpencodeDbPath } from '@hapi/protocol/opencodeUsage'
|
||||
import type { Store } from '../store'
|
||||
import type { ReconciledUsageRow } from '../store/usage'
|
||||
|
||||
/**
|
||||
* Offline usage reconciliation for OpenCode sessions.
|
||||
*
|
||||
* The live pipeline records usage from the agent's prompt responses. OpenCode
|
||||
* only reports the *final* model step of a turn there, so every tool-loop step
|
||||
* (usually the bulk of the tokens) is missing from the dashboard. Instead of
|
||||
* changing the live stream — which previously leaked cumulative counters into
|
||||
* the chat/status UI — this job periodically reads OpenCode's own SQLite store
|
||||
* and writes absolute per-(day, model) snapshots into `usage_reconciliation`.
|
||||
* `getUsageSummary` prefers those snapshots for reconciled sessions and never
|
||||
* mixes them with the live events of the same session, so the job is
|
||||
* idempotent and can re-run at any cadence without double counting.
|
||||
*
|
||||
* This hub-side pass only covers sessions whose OpenCode store lives on the
|
||||
* hub machine. Every other machine's runner reports the same row shape over
|
||||
* the `opencode-usage-report` socket event (see `@hapi/protocol/opencodeUsage`
|
||||
* and cli/src/runner/opencodeUsageScanner.ts).
|
||||
*/
|
||||
|
||||
const RECONCILE_INTERVAL_MS = 30 * 60 * 1000
|
||||
const INITIAL_DELAY_MS = 15_000
|
||||
|
||||
export type UsageReconciliationResult = {
|
||||
dbPath: string | null
|
||||
sessions: number
|
||||
rows: number
|
||||
messages: number
|
||||
}
|
||||
|
||||
function asRecord(value: unknown): Record<string, unknown> | null {
|
||||
return value !== null && typeof value === 'object' && !Array.isArray(value)
|
||||
? value as Record<string, unknown>
|
||||
: null
|
||||
}
|
||||
|
||||
function asString(value: unknown): string | null {
|
||||
return typeof value === 'string' && value.trim().length > 0 ? value.trim() : null
|
||||
}
|
||||
|
||||
/** Re-read every known OpenCode session from the local store and refresh snapshots. */
|
||||
export async function reconcileOpencodeUsage(
|
||||
store: Store,
|
||||
options?: { dbPath?: string | null; now?: number }
|
||||
): Promise<UsageReconciliationResult> {
|
||||
const dbPath = options?.dbPath === undefined ? resolveOpencodeDbPath() : options.dbPath
|
||||
const now = options?.now ?? Date.now()
|
||||
if (!dbPath || !existsSync(dbPath)) {
|
||||
return { dbPath: dbPath ?? null, sessions: 0, rows: 0, messages: 0 }
|
||||
}
|
||||
|
||||
const targets: Array<{ sessionId: string; namespace: string; opencodeSessionId: string }> = []
|
||||
for (const session of store.sessions.getSessions()) {
|
||||
const opencodeSessionId = asString(asRecord(session.metadata)?.opencodeSessionId)
|
||||
if (!opencodeSessionId) continue
|
||||
targets.push({ sessionId: session.id, namespace: session.namespace, opencodeSessionId })
|
||||
}
|
||||
|
||||
const usageBySession = await readOpencodeUsageForSessions(dbPath, targets.map((target) => target.opencodeSessionId))
|
||||
let sessionsTouched = 0
|
||||
let rowsTotal = 0
|
||||
let messagesTotal = 0
|
||||
for (const target of targets) {
|
||||
const usage = usageBySession.get(target.opencodeSessionId) ?? { rows: [], messages: 0 }
|
||||
// No local messages means this session's OpenCode store lives on
|
||||
// another machine (its own process reports the rows) — leave the
|
||||
// existing snapshot alone instead of wiping it with an empty one.
|
||||
if (usage.messages === 0) continue
|
||||
const rows: ReconciledUsageRow[] = usage.rows.map((row) => ({
|
||||
sessionId: target.sessionId,
|
||||
agent: 'opencode',
|
||||
...row
|
||||
}))
|
||||
store.usage.replaceReconciled(target.sessionId, target.namespace, rows, now)
|
||||
sessionsTouched += 1
|
||||
rowsTotal += rows.length
|
||||
messagesTotal += usage.messages
|
||||
}
|
||||
|
||||
return { dbPath, sessions: sessionsTouched, rows: rowsTotal, messages: messagesTotal }
|
||||
}
|
||||
|
||||
export type UsageReconciliationJob = {
|
||||
/** Idempotent. */
|
||||
stop: () => void
|
||||
/** Run one reconciliation pass immediately (used by tests). */
|
||||
runNow: () => void
|
||||
}
|
||||
|
||||
/** Schedule periodic reconciliation while the hub is running. */
|
||||
export function startOpencodeUsageReconciliation(
|
||||
store: Store,
|
||||
options?: { intervalMs?: number; initialDelayMs?: number }
|
||||
): UsageReconciliationJob {
|
||||
let stopped = false
|
||||
const run = (): void => {
|
||||
if (stopped) return
|
||||
void reconcileOpencodeUsage(store)
|
||||
.then((result) => {
|
||||
if (result.messages > 0) {
|
||||
console.log(
|
||||
`[UsageReconciliation] opencode: ${result.messages} messages → ${result.rows} day/model rows across ${result.sessions} sessions`
|
||||
)
|
||||
}
|
||||
})
|
||||
.catch((error) => {
|
||||
console.warn('[UsageReconciliation] failed:', error instanceof Error ? error.message : error)
|
||||
})
|
||||
}
|
||||
|
||||
const initial = setTimeout(run, options?.initialDelayMs ?? INITIAL_DELAY_MS)
|
||||
const interval = setInterval(run, options?.intervalMs ?? RECONCILE_INTERVAL_MS)
|
||||
initial.unref?.()
|
||||
interval.unref?.()
|
||||
|
||||
return {
|
||||
stop: () => {
|
||||
stopped = true
|
||||
clearTimeout(initial)
|
||||
clearInterval(interval)
|
||||
},
|
||||
runNow: run
|
||||
}
|
||||
}
|
||||
@@ -866,4 +866,62 @@ describe('usage service', () => {
|
||||
expect(store.usage.getEvents([target.id])).toHaveLength(1)
|
||||
store.close()
|
||||
})
|
||||
|
||||
it('prefers reconciliation snapshots over live events for the same session', () => {
|
||||
const store = new Store(':memory:')
|
||||
const reconciled = store.sessions.getOrCreateSession(
|
||||
'opencode-reconciled-session',
|
||||
{ path: '/tmp', host: 'test', flavor: 'opencode', opencodeSessionId: 'ses_reconciled' },
|
||||
null,
|
||||
'default',
|
||||
'opencode-go/deepseek-v4.1-flash'
|
||||
)
|
||||
const liveOnly = store.sessions.getOrCreateSession(
|
||||
'opencode-live-session',
|
||||
{ path: '/tmp', host: 'test', flavor: 'opencode' },
|
||||
null,
|
||||
'default',
|
||||
'opencode-go/deepseek-v4.1-flash'
|
||||
)
|
||||
|
||||
const liveUsage = {
|
||||
type: 'codex',
|
||||
data: {
|
||||
type: 'token_count',
|
||||
usageSchema: 'hapi.usage.v1',
|
||||
inputTokenSemantics: 'includes-cache',
|
||||
info: { total: { inputTokens: 180, outputTokens: 5, cachedInputTokens: 80 } }
|
||||
}
|
||||
}
|
||||
addAgentMessage(store, reconciled.id, liveUsage)
|
||||
addAgentMessage(store, liveOnly.id, liveUsage)
|
||||
|
||||
store.usage.replaceReconciled(reconciled.id, 'default', [{
|
||||
sessionId: reconciled.id,
|
||||
day: '2026-09-17',
|
||||
model: 'opencode-go/deepseek-v4.1-flash',
|
||||
agent: 'opencode',
|
||||
inputTokens: 5_000,
|
||||
outputTokens: 200,
|
||||
cacheReadTokens: 4_700,
|
||||
cacheCreationTokens: 0,
|
||||
requests: 42
|
||||
}], Date.now())
|
||||
|
||||
const result = getUsageSummary(store, 'default', 'all')
|
||||
// Reconciled session contributes its snapshot only; the live-only
|
||||
// session keeps the message-derived totals.
|
||||
expect(result.totals.inputTokens).toBe(5_000 + 180)
|
||||
expect(result.totals.outputTokens).toBe(200 + 5)
|
||||
expect(result.totals.cacheReadTokens).toBe(4_700 + 80)
|
||||
expect(result.totals.requests).toBe(42 + 1)
|
||||
expect(result.totals.totalTokens).toBe(5_200 + 185)
|
||||
expect(result.totals.uncachedTokens).toBe(300 + 200 + 100 + 5)
|
||||
expect(result.byModel.find((row) => row.key === 'opencode-go/deepseek-v4.1-flash')).toMatchObject({
|
||||
inputTokens: 5_180,
|
||||
requests: 43
|
||||
})
|
||||
expect(result.totals.sessions).toBe(2)
|
||||
store.close()
|
||||
})
|
||||
})
|
||||
|
||||
@@ -279,7 +279,14 @@ function emptyTotals(): Totals {
|
||||
}
|
||||
}
|
||||
|
||||
function addTotals(target: Totals, inputTokens: number, outputTokens: number, cacheReadTokens: number, cacheCreationTokens: number): void {
|
||||
function addTotals(
|
||||
target: Totals,
|
||||
inputTokens: number,
|
||||
outputTokens: number,
|
||||
cacheReadTokens: number,
|
||||
cacheCreationTokens: number,
|
||||
requests: number = 1
|
||||
): void {
|
||||
target.inputTokens += inputTokens
|
||||
target.outputTokens += outputTokens
|
||||
target.cacheReadTokens += cacheReadTokens
|
||||
@@ -288,7 +295,7 @@ function addTotals(target: Totals, inputTokens: number, outputTokens: number, ca
|
||||
// input_tokens excludes cache fields and is normalized before this call.
|
||||
target.totalTokens += inputTokens + outputTokens
|
||||
target.uncachedTokens += Math.max(0, inputTokens - cacheReadTokens) + outputTokens
|
||||
target.requests += 1
|
||||
target.requests += requests
|
||||
}
|
||||
|
||||
type UsageSnapshot = [number, number, number, number]
|
||||
@@ -348,6 +355,12 @@ export function getUsageSummary(
|
||||
// Read the durable namespace ledger rather than only live session ids;
|
||||
// usage rows intentionally survive session deletion.
|
||||
const events = store.usage.getEventsByNamespace(namespace)
|
||||
// OpenCode snapshot rows below replace the live OpenCode events of the
|
||||
// same session: the live pipeline under-counts turns that report usage
|
||||
// only for their final step, and mixing both sources would double count
|
||||
// the steps it did see. See usageReconciliation.ts.
|
||||
const reconciledRows = store.usage.getReconciledByNamespace(namespace)
|
||||
const reconciledSessionIds = new Set(reconciledRows.map((row) => row.sessionId))
|
||||
const isInRange = (event: UsageEvent) => (from === null || event.createdAt >= from) && event.createdAt <= now
|
||||
|
||||
const totals = emptyTotals()
|
||||
@@ -413,6 +426,10 @@ export function getUsageSummary(
|
||||
}
|
||||
}
|
||||
if (duplicateCumulativeEvent || !isInRange(event) || inputTokens + outputTokens + cacheReadTokens + cacheCreationTokens <= 0) continue
|
||||
// Skip only the live OpenCode rows of a reconciled session: the
|
||||
// snapshot replaces them. Other agents' events on the same session
|
||||
// (e.g. after a flavor switch) still count.
|
||||
if (event.agent === 'opencode' && reconciledSessionIds.has(event.sessionId)) continue
|
||||
// Cache reads and writes partition processed input. Preserve the
|
||||
// request and its primary token counts when a provider emits an
|
||||
// impossible partition, but conservatively decline to credit either
|
||||
@@ -436,6 +453,28 @@ export function getUsageSummary(
|
||||
sessionsWithUsage.add(event.sessionId)
|
||||
}
|
||||
|
||||
// Reconciliation rows are absolute per-day totals, so range filtering
|
||||
// compares day keys rather than event timestamps. Snapshot days are keyed
|
||||
// in the hub's local timezone; when the request asks for another timezone
|
||||
// the daily bucket boundary can drift by at most one day.
|
||||
const fromDayKey = from === null ? null : dayKey(from, dayFormatter)
|
||||
const nowDayKey = dayKey(now, dayFormatter)
|
||||
for (const row of reconciledRows) {
|
||||
if (fromDayKey !== null && row.day < fromDayKey) continue
|
||||
if (row.day > nowDayKey) continue
|
||||
addTotals(totals, row.inputTokens, row.outputTokens, row.cacheReadTokens, row.cacheCreationTokens, row.requests)
|
||||
const dailyTotals = daily.get(row.day) ?? emptyTotals()
|
||||
addTotals(dailyTotals, row.inputTokens, row.outputTokens, row.cacheReadTokens, row.cacheCreationTokens, row.requests)
|
||||
daily.set(row.day, dailyTotals)
|
||||
const agentTotals = byAgent.get(row.agent) ?? emptyTotals()
|
||||
addTotals(agentTotals, row.inputTokens, row.outputTokens, row.cacheReadTokens, row.cacheCreationTokens, row.requests)
|
||||
byAgent.set(row.agent, agentTotals)
|
||||
const modelTotals = byModel.get(row.model) ?? emptyTotals()
|
||||
addTotals(modelTotals, row.inputTokens, row.outputTokens, row.cacheReadTokens, row.cacheCreationTokens, row.requests)
|
||||
byModel.set(row.model, modelTotals)
|
||||
sessionsWithUsage.add(row.sessionId)
|
||||
}
|
||||
|
||||
const sortBuckets = (values: Map<string, Totals>): UsageSummaryBucket[] => Array.from(values.entries())
|
||||
.map(([key, value]) => toBucket(key, value))
|
||||
.sort((a, b) => b.totalTokens - a.totalTokens)
|
||||
|
||||
@@ -23,6 +23,7 @@
|
||||
"./voicePickerCatalog": "./src/voicePickerCatalog.ts",
|
||||
"./voice-personality": "./src/voicePersonality.ts",
|
||||
"./usage": "./src/usage.ts",
|
||||
"./opencodeUsage": "./src/opencodeUsage.ts",
|
||||
"./settingsFileLock": "./src/settingsFileLock.ts",
|
||||
"./agentCliSpawnLease": "./src/agentCliSpawnLease.ts"
|
||||
},
|
||||
|
||||
@@ -0,0 +1,193 @@
|
||||
import { existsSync } from 'node:fs'
|
||||
import { homedir } from 'node:os'
|
||||
import { join } from 'node:path'
|
||||
import type { OpencodeUsageReportRow } from './usage'
|
||||
|
||||
/**
|
||||
* Reading and aggregating OpenCode's own token accounting.
|
||||
*
|
||||
* OpenCode stores every message in `~/.local/share/opencode/opencode.db` with
|
||||
* per-message provider counters (`tokens.input/output/reasoning/cache.*`).
|
||||
* HAPI's live pipeline only sees the final model step of a turn, so both the
|
||||
* hub's local reconciliation job and each session process (reporting its own
|
||||
* machine's store) derive the dashboard numbers from this file instead.
|
||||
*/
|
||||
|
||||
export type OpencodeAssistantUsage = {
|
||||
day: string
|
||||
model: string
|
||||
inputTokens: number
|
||||
outputTokens: number
|
||||
cacheReadTokens: number
|
||||
cacheCreationTokens: number
|
||||
}
|
||||
|
||||
export type OpencodeSessionUsage = {
|
||||
rows: OpencodeUsageReportRow[]
|
||||
messages: number
|
||||
}
|
||||
|
||||
/**
|
||||
* Resolve the OpenCode SQLite store. `HAPI_OPENCODE_DB` overrides the path
|
||||
* (tests, non-XDG layouts); the default follows the XDG data directory.
|
||||
*/
|
||||
export function resolveOpencodeDbPath(env: NodeJS.ProcessEnv = process.env): string | null {
|
||||
const override = env.HAPI_OPENCODE_DB?.trim()
|
||||
if (override) return override
|
||||
const dataHome = env.XDG_DATA_HOME?.trim()
|
||||
return join(
|
||||
dataHome && dataHome.length > 0 ? dataHome : join(homedir(), '.local', 'share'),
|
||||
'opencode',
|
||||
'opencode.db'
|
||||
)
|
||||
}
|
||||
|
||||
function asRecord(value: unknown): Record<string, unknown> | null {
|
||||
return value !== null && typeof value === 'object' && !Array.isArray(value)
|
||||
? value as Record<string, unknown>
|
||||
: null
|
||||
}
|
||||
|
||||
function asString(value: unknown): string | null {
|
||||
return typeof value === 'string' && value.trim().length > 0 ? value.trim() : null
|
||||
}
|
||||
|
||||
function asCount(value: unknown): number {
|
||||
return typeof value === 'number' && Number.isFinite(value) && value > 0
|
||||
? Math.floor(value)
|
||||
: 0
|
||||
}
|
||||
|
||||
/**
|
||||
* Day bucket for a message timestamp, in the machine's local timezone. The
|
||||
* dashboard's daily buckets use the request timezone; for the default
|
||||
* (same-timezone) view these line up exactly.
|
||||
*/
|
||||
function localDayKey(createdAtMs: number): string {
|
||||
const date = new Date(createdAtMs)
|
||||
const year = String(date.getFullYear()).padStart(4, '0')
|
||||
const month = String(date.getMonth() + 1).padStart(2, '0')
|
||||
const day = String(date.getDate()).padStart(2, '0')
|
||||
return `${year}-${month}-${day}`
|
||||
}
|
||||
|
||||
/**
|
||||
* Parse one `message.data` JSON row from the OpenCode store. Only assistant
|
||||
* messages carry provider accounting; zero-token rows (aborted or synthetic
|
||||
* messages) are skipped so they do not inflate the request count.
|
||||
*/
|
||||
export function parseOpencodeAssistantUsage(raw: string): OpencodeAssistantUsage | null {
|
||||
let parsed: unknown
|
||||
try {
|
||||
parsed = JSON.parse(raw)
|
||||
} catch {
|
||||
return null
|
||||
}
|
||||
const message = asRecord(parsed)
|
||||
if (!message || message.role !== 'assistant') return null
|
||||
const tokens = asRecord(message.tokens)
|
||||
if (!tokens) return null
|
||||
const cache = asRecord(tokens.cache)
|
||||
const input = asCount(tokens.input)
|
||||
const output = asCount(tokens.output)
|
||||
const reasoning = asCount(tokens.reasoning)
|
||||
const cacheRead = asCount(cache?.read)
|
||||
const cacheCreation = asCount(cache?.write)
|
||||
if (input + output + reasoning + cacheRead + cacheCreation <= 0) return null
|
||||
const time = asRecord(message.time)
|
||||
const createdAt = asCount(time?.created)
|
||||
if (createdAt <= 0) return null
|
||||
|
||||
const providerId = asString(message.providerID)
|
||||
const modelId = asString(message.modelID) ?? asString(asRecord(message.model)?.modelID)
|
||||
const model = providerId && modelId
|
||||
? `${providerId}/${modelId}`
|
||||
: modelId ?? 'unknown'
|
||||
|
||||
return {
|
||||
day: localDayKey(createdAt),
|
||||
model,
|
||||
// The HAPI ledger stores input inclusive of cache reads/writes
|
||||
// (hapi.usage.v1) and folds reasoning into output, matching how the
|
||||
// dashboard tallies input/output downstream.
|
||||
inputTokens: input + cacheRead + cacheCreation,
|
||||
outputTokens: output + reasoning,
|
||||
cacheReadTokens: cacheRead,
|
||||
cacheCreationTokens: cacheCreation
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Every OpenCode session id present in the store's message table. Used by a
|
||||
* machine's runner to report all of its sessions (including ones whose
|
||||
* process already exited) in a single pass.
|
||||
*/
|
||||
export async function listOpencodeSessionIds(dbPath: string): Promise<string[]> {
|
||||
if (!existsSync(dbPath)) return []
|
||||
const { Database } = await import('bun:sqlite')
|
||||
const db = new Database(dbPath, { readonly: true })
|
||||
try {
|
||||
const rows = db.prepare('SELECT DISTINCT session_id FROM message').all() as Array<{ session_id: string }>
|
||||
return rows
|
||||
.map((row) => row.session_id)
|
||||
.filter((id): id is string => typeof id === 'string' && id.length > 0)
|
||||
} finally {
|
||||
db.close()
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Aggregate absolute per-(day, model) totals for the given OpenCode session
|
||||
* ids. Missing database or unknown session ids yield empty entries, so
|
||||
* callers can always distinguish "no usage" from "not reconciled yet".
|
||||
*
|
||||
* `bun:sqlite` is imported lazily because the CLI test runner (vitest under
|
||||
* node) imports this module transitively without Bun's built-ins.
|
||||
*/
|
||||
export async function readOpencodeUsageForSessions(
|
||||
dbPath: string,
|
||||
opencodeSessionIds: string[]
|
||||
): Promise<Map<string, OpencodeSessionUsage>> {
|
||||
const result = new Map<string, OpencodeSessionUsage>()
|
||||
for (const id of opencodeSessionIds) {
|
||||
result.set(id, { rows: [], messages: 0 })
|
||||
}
|
||||
if (opencodeSessionIds.length === 0 || !existsSync(dbPath)) return result
|
||||
|
||||
const { Database } = await import('bun:sqlite')
|
||||
const db = new Database(dbPath, { readonly: true })
|
||||
try {
|
||||
const selectMessages = db.prepare('SELECT data FROM message WHERE session_id = ?')
|
||||
for (const sessionId of opencodeSessionIds) {
|
||||
const messageRows = selectMessages.all(sessionId) as Array<{ data: string }>
|
||||
const buckets = new Map<string, OpencodeUsageReportRow>()
|
||||
let messages = 0
|
||||
for (const row of messageRows) {
|
||||
const usage = parseOpencodeAssistantUsage(row.data)
|
||||
if (!usage) continue
|
||||
messages += 1
|
||||
const key = `${usage.day}|${usage.model}`
|
||||
const bucket = buckets.get(key) ?? {
|
||||
day: usage.day,
|
||||
model: usage.model,
|
||||
inputTokens: 0,
|
||||
outputTokens: 0,
|
||||
cacheReadTokens: 0,
|
||||
cacheCreationTokens: 0,
|
||||
requests: 0
|
||||
}
|
||||
bucket.inputTokens += usage.inputTokens
|
||||
bucket.outputTokens += usage.outputTokens
|
||||
bucket.cacheReadTokens += usage.cacheReadTokens
|
||||
bucket.cacheCreationTokens += usage.cacheCreationTokens
|
||||
bucket.requests += 1
|
||||
buckets.set(key, bucket)
|
||||
}
|
||||
result.set(sessionId, { rows: Array.from(buckets.values()), messages })
|
||||
}
|
||||
} finally {
|
||||
db.close()
|
||||
}
|
||||
|
||||
return result
|
||||
}
|
||||
@@ -2,6 +2,7 @@ import { z } from 'zod'
|
||||
import type { CodexCollaborationMode, PermissionMode } from './modes'
|
||||
import type { CopilotAgentMode } from './copilotModes'
|
||||
import type { SessionEndReason } from './schemas'
|
||||
import type { OpencodeUsageMachineReportPayload } from './usage'
|
||||
export { SessionEndReasonSchema, type SessionEndReason } from './schemas'
|
||||
|
||||
export type SocketErrorReason = 'namespace-missing' | 'access-denied' | 'not-found'
|
||||
@@ -296,5 +297,10 @@ export interface ClientToServerEvents {
|
||||
// spawned, e.g. after archive→restart, so old output must not replay).
|
||||
'agent-terminal:reset': (data: { sessionId: string }) => void
|
||||
ping: (callback: () => void) => void
|
||||
'usage-report': (data: unknown) => void
|
||||
/**
|
||||
* Absolute OpenCode usage snapshots read from this machine's own OpenCode
|
||||
* store. The hub stores them as reconciliation rows; they never flow
|
||||
* through the chat pipeline.
|
||||
*/
|
||||
'opencode-usage-report': (data: OpencodeUsageMachineReportPayload) => void
|
||||
}
|
||||
|
||||
@@ -8,3 +8,31 @@ export const INCLUSIVE_INPUT_TOKEN_USAGE_MARKER = {
|
||||
} as const
|
||||
|
||||
export type InclusiveInputTokenUsageMarker = typeof INCLUSIVE_INPUT_TOKEN_USAGE_MARKER
|
||||
|
||||
/**
|
||||
* One absolute per-(day, model) OpenCode token bucket. Produced by the
|
||||
* offline usage reconciler (`@hapi/protocol/opencodeUsage`), either by the
|
||||
* hub's local scan or by a machine's runner reporting its own OpenCode store
|
||||
* over `opencode-usage-report`. Values are totals, never deltas.
|
||||
*/
|
||||
export type OpencodeUsageReportRow = {
|
||||
day: string
|
||||
model: string
|
||||
inputTokens: number
|
||||
outputTokens: number
|
||||
cacheReadTokens: number
|
||||
cacheCreationTokens: number
|
||||
requests: number
|
||||
}
|
||||
|
||||
/** One OpenCode session's snapshot inside a machine-level report. */
|
||||
export type OpencodeUsageSessionReport = {
|
||||
opencodeSessionId: string
|
||||
rows: OpencodeUsageReportRow[]
|
||||
}
|
||||
|
||||
/** CLI machine → hub `opencode-usage-report` payload. */
|
||||
export type OpencodeUsageMachineReportPayload = {
|
||||
machineId: string
|
||||
sessions: OpencodeUsageSessionReport[]
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user