dsh-cost-meter 1.9.26 → 1.9.28

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (39) hide show
  1. package/README.md +9 -9
  2. package/README.zh-CN.md +9 -9
  3. package/docs/billion-context-billing.md +10 -2
  4. package/docs/openrouter-prices.md +12 -0
  5. package/docs/plugin-compatibility-watch.md +2 -0
  6. package/docs/provider-pricing-2026-10.md +2 -2
  7. package/docs/update-notes-1.9.27.md +38 -0
  8. package/docs/update-notes-1.9.28.md +91 -0
  9. package/lib/backfill.js +108 -59
  10. package/lib/billion-context-billing.js +85 -10
  11. package/lib/billion-context-history.js +86 -11
  12. package/lib/billion-context-proxy.js +16 -5
  13. package/lib/bootstrap.js +7 -7
  14. package/lib/client.display.js +2 -2
  15. package/lib/client.js +6 -6
  16. package/lib/client.sidebar-order.js +3 -2
  17. package/lib/client.statistics.js +6 -6
  18. package/lib/coding-plans.js +7 -5
  19. package/lib/exchange-rate-refresh.js +2 -3
  20. package/lib/gateway-quota-adapters.js +42 -3
  21. package/lib/gateway-quotas.js +36 -2
  22. package/lib/index.js +62 -20
  23. package/lib/journal-storage.js +88 -0
  24. package/lib/ledger-persistence.js +4 -1
  25. package/lib/ledger-writer-worker.js +30 -0
  26. package/lib/ledger-writer.js +33 -0
  27. package/lib/native-search-history.js +30 -10
  28. package/lib/net.js +49 -21
  29. package/lib/openrouter-pricing.js +8 -2
  30. package/lib/openrouter-refresh-state.js +57 -0
  31. package/lib/pi-ai-codex-quota.js +66 -0
  32. package/lib/plan-billing.js +39 -16
  33. package/lib/session-cost-source.js +2 -2
  34. package/lib/statistics-warmup.js +3 -2
  35. package/lib/store.js +98 -16
  36. package/lib/typert.host.js +21 -2
  37. package/lib/usage-dedup.js +24 -11
  38. package/lib/usage-totals.js +7 -0
  39. package/package.json +5 -2
package/lib/backfill.js CHANGED
@@ -12,6 +12,8 @@
12
12
 
13
13
  import { FX_FIELDS, frozenAmounts, frozenFields, addFrozen, fxRatesOf } from './exchange-rates.js'
14
14
  import { open } from 'node:fs/promises'
15
+ import { StringDecoder } from 'node:string_decoder'
16
+ import { totalUsageTokens } from './usage-totals.js'
15
17
  import * as zlib from 'node:zlib'
16
18
  import { costOf, providerPriceEntryFor, usdFromCost, isWrapperProviderId, wrapperUpstreamProvider } from './pricing.js'
17
19
  import { localDayKey, zeroDay, splitLedgerApiCost } from './store.js'
@@ -51,6 +53,9 @@ const PACKED_ROW_PROBE_MIN_LINE = 4096
51
53
  const MAX_DECOMPRESSED_PER_FILE = 4 * 1024 * 1024 * 1024
52
54
  /** 流式读文件的分块大小。 */
53
55
  const READ_CHUNK_BYTES = 8 * 1024 * 1024
56
+ const MAX_FRAME_BYTES = 64 * 1024 * 1024
57
+ const MAX_LINE_BYTES = 32 * 1024 * 1024
58
+ const MAX_EVENTS = 1_000_000
54
59
  /** 每 N 帧解压/每 N 条事件让出一次事件循环(保持宿主 UI 响应)。 */
55
60
  const YIELD_EVERY_FRAMES = 64
56
61
  const YIELD_EVERY_EVENTS = 4096
@@ -89,45 +94,53 @@ export function parseRecordLine(line) {
89
94
  * @param buffer - 会话日志原始字节。
90
95
  * @returns 完整 frame 的字节区间数组。
91
96
  */
97
+ function scanZstdFrame(buffer, start = 0) {
98
+ let offset = start
99
+ const incomplete = { status: 'incomplete' }, invalid = { status: 'invalid' }
100
+ if (buffer.length - offset < 4) return incomplete
101
+ if (buffer.readUInt32LE(offset) !== ZSTD_MAGIC) return invalid
102
+ offset += 4
103
+ if (offset === buffer.length) return incomplete
104
+ const descriptor = buffer.readUInt8(offset)
105
+ offset += 1
106
+ if ((descriptor & 24) !== 0) return invalid
107
+ const contentSizeFlag = descriptor >>> 6
108
+ const singleSegment = (descriptor & 32) !== 0
109
+ const checksum = (descriptor & 4) !== 0
110
+ const dictionaryFlag = descriptor & 3
111
+ const dictionaryBytes = dictionaryFlag === 3 ? 4 : dictionaryFlag
112
+ const contentSizeBytes = contentSizeFlag === 0 ? (singleSegment ? 1 : 0) : 1 << contentSizeFlag
113
+ const remainingHeaderBytes = (singleSegment ? 0 : 1) + dictionaryBytes + contentSizeBytes
114
+ if (buffer.length - offset < remainingHeaderBytes) return incomplete
115
+ offset += remainingHeaderBytes
116
+ for (;;) {
117
+ if (buffer.length - offset < 3) return incomplete
118
+ const blockHeader = buffer.readUIntLE(offset, 3)
119
+ offset += 3
120
+ const lastBlock = (blockHeader & 1) !== 0
121
+ const blockType = (blockHeader >>> 1) & 3
122
+ const blockSize = blockHeader >>> 3
123
+ if (blockType === 3) return invalid
124
+ const payloadBytes = blockType === 1 ? 1 : blockSize
125
+ if (buffer.length - offset < payloadBytes) return incomplete
126
+ offset += payloadBytes
127
+ if (lastBlock) break
128
+ }
129
+ if (checksum) {
130
+ if (buffer.length - offset < 4) return incomplete
131
+ offset += 4
132
+ }
133
+ return { status: 'complete', end: offset }
134
+ }
135
+
92
136
  export function scanZstdFrames(buffer) {
93
137
  const frames = []
94
138
  let offset = 0
95
139
  while (offset < buffer.length) {
96
- const start = offset
97
- if (buffer.length - offset < 4) return frames
98
- if (buffer.readUInt32LE(offset) !== ZSTD_MAGIC) return frames
99
- offset += 4
100
- if (offset === buffer.length) return frames
101
- const descriptor = buffer.readUInt8(offset)
102
- offset += 1
103
- if ((descriptor & 24) !== 0) return frames // 保留位:结构非法,停止扫描
104
- const contentSizeFlag = descriptor >>> 6
105
- const singleSegment = (descriptor & 32) !== 0
106
- const checksum = (descriptor & 4) !== 0
107
- const dictionaryFlag = descriptor & 3
108
- const dictionaryBytes = dictionaryFlag === 3 ? 4 : dictionaryFlag
109
- const contentSizeBytes = contentSizeFlag === 0 ? (singleSegment ? 1 : 0) : 1 << contentSizeFlag
110
- const remainingHeaderBytes = (singleSegment ? 0 : 1) + dictionaryBytes + contentSizeBytes
111
- if (buffer.length - offset < remainingHeaderBytes) return frames
112
- offset += remainingHeaderBytes
113
- for (;;) {
114
- if (buffer.length - offset < 3) return frames
115
- const blockHeader = buffer.readUIntLE(offset, 3)
116
- offset += 3
117
- const lastBlock = (blockHeader & 1) !== 0
118
- const blockType = (blockHeader >>> 1) & 3
119
- const blockSize = blockHeader >>> 3
120
- if (blockType === 3) return frames // 保留块类型:结构非法
121
- const payloadBytes = blockType === 1 ? 1 : blockSize
122
- if (buffer.length - offset < payloadBytes) return frames
123
- offset += payloadBytes
124
- if (lastBlock) break
125
- }
126
- if (checksum) {
127
- if (buffer.length - offset < 4) return frames
128
- offset += 4
129
- }
130
- frames.push({ start, end: offset })
140
+ const frame = scanZstdFrame(buffer, offset)
141
+ if (frame.status !== 'complete') break
142
+ frames.push({ start: offset, end: frame.end })
143
+ offset = frame.end
131
144
  }
132
145
  return frames
133
146
  }
@@ -149,6 +162,9 @@ export function scanZstdFrames(buffer) {
149
162
  export async function* iterateSessionRecords(path, options = {}) {
150
163
  const maxDecompressed = Number(options.maxDecompressed) > 0 ? Number(options.maxDecompressed) : MAX_DECOMPRESSED_PER_FILE
151
164
  const yieldEvery = Number(options.yieldEvery) > 0 ? Number(options.yieldEvery) : YIELD_EVERY_FRAMES
165
+ const maxFrameBytes = Number(options.maxFrameBytes) > 0 ? Number(options.maxFrameBytes) : MAX_FRAME_BYTES
166
+ const maxLineBytes = Number(options.maxLineBytes) > 0 ? Number(options.maxLineBytes) : MAX_LINE_BYTES
167
+ const maxEvents = Number(options.maxEvents) > 0 ? Number(options.maxEvents) : MAX_EVENTS
152
168
  const isZstd = path.endsWith('.zstd')
153
169
  if (isZstd && typeof zlib.zstdDecompressSync !== 'function') return
154
170
  const handle = await open(path, 'r')
@@ -157,20 +173,40 @@ export async function* iterateSessionRecords(path, options = {}) {
157
173
  let tail = Buffer.alloc(0) // 压缩字节尾部缓冲:块边界落在帧中间时拼接后继续扫。
158
174
  let decompressedTotal = 0
159
175
  let framesSinceYield = 0
176
+ let events = 0
177
+ let droppingPacked = false
178
+ const decoder = new StringDecoder('utf8')
179
+ const budgetExceeded = () => new RangeError(`session log decompress budget exceeded (${maxDecompressed} bytes): ${path}`)
180
+ const consumeBytes = (bytes, out) => {
181
+ decompressedTotal += bytes.length
182
+ if (decompressedTotal > maxDecompressed) throw budgetExceeded()
183
+ consumeText(decoder.write(bytes), out)
184
+ }
160
185
  const consumeText = (text, out) => {
161
186
  if (text.length === 0) return
162
187
  const lines = text.split('\n')
163
188
  lines[0] = pending + lines[0]
164
189
  pending = lines[lines.length - 1]
165
190
  for (let i = 0; i < lines.length - 1; i++) {
191
+ if (droppingPacked) { droppingPacked = false; continue }
192
+ if (Buffer.byteLength(lines[i]) > maxLineBytes) {
193
+ if (PACKED_ROW_PROBE.test(lines[i].slice(0, PACKED_ROW_PROBE_HEAD))) continue
194
+ throw new RangeError('Session log line exceeds byte limit')
195
+ }
166
196
  const rec = parseRecordLine(lines[i])
167
- if (rec !== null) out.push(rec)
197
+ if (rec !== null) {
198
+ if (++events > maxEvents) throw new RangeError('Session log exceeds event limit')
199
+ out.push(rec)
200
+ }
201
+ }
202
+ if (droppingPacked) pending = ''
203
+ else if (Buffer.byteLength(pending) > maxLineBytes) {
204
+ if (!PACKED_ROW_PROBE.test(pending.slice(0, PACKED_ROW_PROBE_HEAD))) throw new RangeError('Session log line exceeds byte limit')
205
+ droppingPacked = true; pending = ''
168
206
  }
169
207
  }
170
208
  const records = []
171
- // 结构损坏标志:一旦出现「一个完整分块里连一个可解帧都没有」,按旧版
172
- // 全量扫描的语义(在首个非法结构处停止)忽略文件后续内容——否则坏字节
173
- // 会在 tail 里无限拼接,且行为与旧版不一致。
209
+ // Only invalid frame structure stops the scan; an incomplete frame keeps reading.
174
210
  let stopped = false
175
211
  for (;;) {
176
212
  const chunk = await handle.read(Buffer.allocUnsafe(READ_CHUNK_BYTES), 0, READ_CHUNK_BYTES, null)
@@ -179,7 +215,7 @@ export async function* iterateSessionRecords(path, options = {}) {
179
215
  tail = Buffer.alloc(0)
180
216
  if (stopped) continue
181
217
  if (!isZstd) {
182
- consumeText(data.toString('utf8'), records)
218
+ consumeBytes(data, records)
183
219
  if (records.length >= YIELD_EVERY_EVENTS) {
184
220
  yield* drain(records)
185
221
  await new Promise(resolve => setImmediate(resolve))
@@ -190,25 +226,34 @@ export async function* iterateSessionRecords(path, options = {}) {
190
226
  // 解压→逐行→释放引用;剩余不完整帧(块边界截断)留在 tail 等下一块。
191
227
  let offset = 0
192
228
  while (offset < data.length) {
193
- const frames = scanZstdFrames(data.subarray(offset))
194
- if (frames.length === 0) {
195
- // 剩余字节连不出完整帧:未满一个分块 = 尾部截断,留 tail 等下一块;
196
- // 已满一个分块仍无帧 = 结构损坏(与旧版全量扫描停在非法结构同语义),
197
- // 标记停止,忽略文件后续。
198
- if (data.length - offset >= READ_CHUNK_BYTES) stopped = true
199
- else tail = data.subarray(offset)
229
+ const frame = scanZstdFrame(data, offset)
230
+ if (frame.status !== 'complete') {
231
+ if (frame.status === 'invalid') stopped = true
232
+ else {
233
+ if (data.length - offset > maxFrameBytes) throw new RangeError('Session log compressed frame exceeds byte limit')
234
+ tail = Buffer.from(data.subarray(offset))
235
+ }
200
236
  break
201
237
  }
202
- for (const f of frames) {
203
- decompressedTotal += f.end - f.start
204
- if (decompressedTotal > maxDecompressed) {
205
- throw new RangeError(`session log decompress budget exceeded (${decompressedTotal} > ${maxDecompressed} bytes): ${path}`)
238
+ if (frame.end - offset > maxFrameBytes) throw new RangeError('Session log compressed frame exceeds byte limit')
239
+ const remaining = maxDecompressed - decompressedTotal
240
+ if (remaining <= 0) throw budgetExceeded()
241
+ let plain
242
+ try { plain = zlib.zstdDecompressSync(data.subarray(offset, frame.end), { maxOutputLength: Math.min(remaining, maxFrameBytes) }) }
243
+ catch (error) {
244
+ if (error.code === 'ERR_BUFFER_TOO_LARGE') {
245
+ if (remaining <= maxFrameBytes) throw budgetExceeded()
246
+ throw new RangeError(`Session log decompressed frame exceeds byte limit (${maxFrameBytes})`)
206
247
  }
207
- const plain = zlib.zstdDecompressSync(data.subarray(offset + f.start, offset + f.end))
208
- consumeText(plain.toString('utf8'), records)
209
- framesSinceYield += 1
248
+ throw error
249
+ }
250
+ consumeBytes(plain, records)
251
+ framesSinceYield += 1
252
+ offset = frame.end
253
+ if (framesSinceYield >= yieldEvery || records.length >= YIELD_EVERY_EVENTS) {
254
+ yield* drain(records); framesSinceYield = 0
255
+ await new Promise(resolve => setImmediate(resolve))
210
256
  }
211
- offset += frames[frames.length - 1].end
212
257
  }
213
258
  if (framesSinceYield >= yieldEvery || records.length >= YIELD_EVERY_EVENTS) {
214
259
  yield* drain(records)
@@ -217,9 +262,13 @@ export async function* iterateSessionRecords(path, options = {}) {
217
262
  }
218
263
  }
219
264
  // 文件读完:tail 里剩的残帧(崩溃截断)按既有语义忽略;行缓冲兜底末行。
265
+ consumeText(decoder.end(), records)
220
266
  if (pending.length > 0) {
221
267
  const rec = parseRecordLine(pending)
222
- if (rec !== null) records.push(rec)
268
+ if (rec !== null) {
269
+ if (++events > maxEvents) throw new RangeError('Session log exceeds event limit')
270
+ records.push(rec)
271
+ }
223
272
  }
224
273
  yield* drain(records)
225
274
  } finally {
@@ -344,7 +393,7 @@ export async function repairPlanHourBuckets(ledger, sessionsRoot, { nowMs = Date
344
393
  ?? (isRoutedThirdPartyCall(call.provider, call.model, config.prices) ? 'go' : null)
345
394
  const entry = byDay.get(call.date)
346
395
  if (id && entry) entry.buckets = appendHourBucket(entry.buckets, id, call.atMs,
347
- ['input', 'output', 'cacheRead', 'cacheWrite', 'reasoning'].reduce((sum, key) => sum + (Number(call.buckets[key]) || 0), 0), call.cost)
396
+ totalUsageTokens(call.buckets), call.cost)
348
397
  }
349
398
  }
350
399
  if (!isActive() || ledger.closed) return { days: 0 }
@@ -568,11 +617,11 @@ export function replaySessionRecords(records, config, wantDates = null, collectS
568
617
  effectiveProvider = wrapperUpstreamProvider(sampleProvider) ?? sampleProvider
569
618
  if (!isCompaction && !metered) {
570
619
  const fp = usageFingerprint(sampleModel, buckets, effectiveProvider)
571
- remappedAdmitted = remappedAdmitted.filter(entry => atMs - entry.at <= USAGE_DEDUP_WINDOW_MS)
572
620
  const plainTimes = plainFingerprints.get(fp)
573
621
  const plainHit = plainTimes !== undefined && plainTimes.some(t => Math.abs(atMs - t) <= USAGE_DEDUP_WINDOW_MS)
574
- if (plainHit || remappedAdmitted.some(entry => entry.fp === fp)) continue
622
+ if (plainHit || remappedAdmitted.some(entry => entry.fp === fp && Math.abs(atMs - entry.at) <= USAGE_DEDUP_WINDOW_MS)) continue
575
623
  remappedAdmitted.push({ fp, at: atMs })
624
+ if (remappedAdmitted.length > 4096) remappedAdmitted.shift()
576
625
  }
577
626
  }
578
627
  const key = metered ? `${isBillion ? 'billion' : 'search'}:${event.data.requestId}` : isCompaction
@@ -5,8 +5,9 @@ import { statSync } from 'node:fs'
5
5
  import { dirname, join } from 'node:path'
6
6
  import { billionVersionSupported } from './billion-context-compatibility.js'
7
7
  import { installBillionAuto } from './billion-context-auto.js'
8
+ import { journalInfo, archiveUsageJournals, journalChangeRevision, notifyJournalChange } from './journal-storage.js'
8
9
  import { billionRoot, billionSessionKey, billionStreamPath, writeBillionJson, readBillionJson, listBillionFiles, iterateBillionCalls, readBillionStream,
9
- billionProvider, usageOnly, usageBuckets, usageSignature, validUsage, validSessionId } from './billion-context-history.js'
10
+ listBillionStreamFiles, readBillionStreamFile, billionProvider, usageOnly, usageBuckets, usageSignature, validUsage, validSessionId, billionCoverage, billionCoverageIndex, billionHostMeter } from './billion-context-history.js'
10
11
 
11
12
  const bridgeKey = Symbol.for('dsh-cost-meter.billion-context.v1'), bridges = new WeakMap()
12
13
  const plainId = value => typeof value === 'string' ? value.slice(0, 200) : ''
@@ -25,9 +26,25 @@ function stepIdentity(ctx, id) {
25
26
  export function installBillionContextBilling(ctx, ledger) {
26
27
  if (!ledger.path) return null
27
28
  const root = billionRoot(ledger.path), scope = new AsyncLocalStorage(), streams = new Map(), groups = new Map(), files = new Map()
29
+ const dirtyStreams = new Map()
30
+ let inventoryAt = 0, inventoryRevision = '', callFiles = []
28
31
  let stopped = false, pending = null, rerun = false, peerAt = 0, peerPending, releaseAuto
29
32
  const status = { protocol: 1, requests: 0, unreported: 0, partial: 0, uncovered: 0, connected: false, failed: false,
30
- detected: false, version: '', compatibility: 'unknown', noticeId: '' }
33
+ detected: false, version: '', compatibility: 'unknown', noticeId: '', unresolved: 0 }
34
+ const warned = new Set()
35
+ const applyCoverage = (stream, coverage) => {
36
+ stream.coverage = coverage
37
+ if (coverage.mode !== 'proxy') { stream.covered = false; stream.hostSuppressed = true }
38
+ if (coverage.mode === 'unresolved' && !warned.has(stream.streamId)) {
39
+ warned.add(stream.streamId); status.unresolved++; status.uncovered++
40
+ } else if (coverage.mode !== 'unresolved' && warned.delete(stream.streamId)) status.unresolved--
41
+ }
42
+ const importHost = stream => {
43
+ if (stream.coverage?.mode !== 'host' || !validUsage(stream.reported)) return
44
+ const meter = billionHostMeter(stream)
45
+ if (stream.startedAtMs <= (ledger.historyReset?.at ?? 0)) ledger.advanceUsageCursor(meter)
46
+ else ledger.account(usageBuckets(stream.reported), stream.model, stream.sessionId, stream.startedAtMs, stream.provider, meter)
47
+ }
31
48
  const notice = reason => {
32
49
  status.compatibility = billionVersionSupported(status.version) === false ? 'needs_update' : reason
33
50
  status.noticeId = status.compatibility + ':' + (status.version || 'unknown')
@@ -68,8 +85,16 @@ export function installBillionContextBilling(ctx, ledger) {
68
85
  protocol: 1, streamId: stream.streamId, sessionId: stream.sessionId, model: stream.model, provider: stream.provider,
69
86
  turn: stream.turn, step: stream.step, startedAtMs: stream.startedAtMs, covered: stream.covered ?? stream.lastAck === true,
70
87
  finished: !!stream.finishedAt, policies: stream.policies,
88
+ hostEligible: stream.hostEligible === true,
71
89
  ...(stream.reported ? { reported: stream.reported } : {}), sourceSeqs: stream.sourceSeqs,
90
+ ...(stream.coverage ? { coverage: stream.coverage, hostSuppressed: stream.hostSuppressed === true } : {}),
72
91
  })
92
+ dirtyStreams.set(stream.streamId, { path: billionStreamPath(root, stream.sessionId, stream.streamId), key: billionSessionKey(stream.sessionId), sessionId: stream.sessionId })
93
+ if (stream.finishedAt) {
94
+ const previous = journalChangeRevision(root)
95
+ notifyJournalChange(root)
96
+ if (previous === inventoryRevision) inventoryRevision = journalChangeRevision(root)
97
+ }
73
98
  }
74
99
  // The proxy reads this fixed private file. No filesystem path is sent upstream.
75
100
  try { writeBillionJson(join(dirname(ledger.path), 'billion-context-bridge.json'), { protocol: 1, root }) }
@@ -82,13 +107,54 @@ export function installBillionContextBilling(ctx, ledger) {
82
107
  rerun = false
83
108
  ledger.refresh()
84
109
  let work = 0
85
- for (const file of listBillionFiles(ledger.path)) {
110
+ const legacyCoverage = new Map()
111
+ const dirty = [...dirtyStreams.values()]; dirtyStreams.clear()
112
+ const revision = journalChangeRevision(root)
113
+ const fullScan = Date.now() - inventoryAt >= 30000 || revision !== inventoryRevision
114
+ const streamFiles = fullScan ? listBillionStreamFiles(ledger.path) : dirty
115
+ if (fullScan) {
116
+ callFiles = listBillionFiles(ledger.path); inventoryAt = Date.now(); inventoryRevision = revision
117
+ const retainedPaths = new Set([...streamFiles, ...callFiles].map(file => file.path))
118
+ for (const path of files.keys()) if (!retainedPaths.has(path)) files.delete(path)
119
+ }
120
+ else if (dirty.length) {
121
+ const updated = dirty.flatMap(file => listBillionFiles(ledger.path, file.sessionId))
122
+ callFiles = [...new Map([...callFiles, ...updated].map(file => [file.path, file])).values()]
123
+ }
124
+ // A completed stream can legitimately contain zero proxy call rows.
125
+ // Discover its once-per-stream fallback independently of the call journal.
126
+ for (const file of streamFiles) {
86
127
  const info = statSync(file.path), cached = files.get(file.path)
87
128
  if (cached?.size === info.size && cached?.mtime === info.mtimeMs) continue
129
+ const stream = readBillionStreamFile(file)
130
+ if (!stream?.finished) continue
131
+ if (!stream.coverage) {
132
+ if (!legacyCoverage.has(stream.sessionId)) legacyCoverage.set(stream.sessionId, await billionCoverageIndex(ledger.path, stream.sessionId))
133
+ applyCoverage(stream, legacyCoverage.get(stream.sessionId).get(stream.streamId) ?? await billionCoverage(ledger.path, stream))
134
+ writeBillionJson(file.path, stream)
135
+ } else applyCoverage(stream, stream.coverage)
136
+ importHost(stream)
137
+ files.set(file.path, { size: info.size, mtime: info.mtimeMs })
138
+ }
139
+ for (const file of callFiles) {
140
+ let info
141
+ try { info = journalInfo(file.path) } catch (error) { if (error.code === 'ENOENT') { inventoryAt = 0; rerun = true; continue }; throw error }
142
+ const cached = files.get(file.path)
143
+ if (cached?.size === info.size && cached?.mtime === info.mtimeMs) continue
88
144
  let offset = cached?.offset <= info.size ? cached.offset : 0, blocked = false
145
+ const prepared = new Map()
89
146
  for await (const item of iterateBillionCalls(file, offset)) {
90
- const row = item.row, stream = readBillionStream(ledger.path, row)
147
+ const row = item.row
148
+ let stream = prepared.get(row.streamId) ?? readBillionStream(ledger.path, row)
91
149
  if (!stream || !stream.finished) { blocked = true; break }
150
+ if (!prepared.has(row.streamId)) {
151
+ const originalCoverage = stream.coverage
152
+ if (!originalCoverage && !legacyCoverage.has(stream.sessionId)) legacyCoverage.set(stream.sessionId, await billionCoverageIndex(ledger.path, stream.sessionId))
153
+ applyCoverage(stream, originalCoverage ?? legacyCoverage.get(stream.sessionId).get(stream.streamId))
154
+ if (!originalCoverage) writeBillionJson(billionStreamPath(root, stream.sessionId, stream.streamId), stream)
155
+ importHost(stream)
156
+ prepared.set(row.streamId, stream)
157
+ }
92
158
  offset = item.offset
93
159
  status.requests++
94
160
  if (!row.usage) status.unreported++
@@ -170,45 +236,54 @@ export function installBillionContextBilling(ctx, ledger) {
170
236
  if (current.policies[groupId] !== 'full') status.uncovered++
171
237
  }
172
238
  current.covered = current.lastAck === true && current.policies[current.lastGroup] !== 'fallback'
239
+ current.hostEligible = current.covered
173
240
  current.finishedAt = Date.now()
174
241
  let saved = true
175
- try { save(current) } catch {
242
+ try {
243
+ if (current.covered) applyCoverage(current, await billionCoverage(ledger.path, current, files))
244
+ save(current)
245
+ } catch {
176
246
  saved = false; current.covered = false; current.lastAck = false
247
+ delete current.coverage; current.hostSuppressed = false; current.hostEligible = false
177
248
  // A later host event must not resurrect full proxy import after host fallback billing.
178
249
  for (const groupId of current.groups) current.policies[groupId] = 'fallback'
179
250
  status.uncovered++
180
251
  }
181
252
  await ingest()
182
253
  if (!saved) status.failed = true
183
- return current.covered
254
+ return current.covered || saved && current.hostSuppressed === true
184
255
  },
185
256
  }
186
257
  },
187
258
  ingest,
188
259
  refreshPeer,
189
- status: () => ({ ...status }),
260
+ status: () => ({ ...status, ...(status.unresolved ? { noticeId: 'usage_missing:' + status.unresolved } : {}) }),
190
261
  dispose() { if (stopped) return; stopped = true; releaseAuto?.(); if (globalThis[bridgeKey] === api) delete globalThis[bridgeKey]; scope.disable() },
191
262
  }
192
263
  globalThis[bridgeKey] = api; bridges.set(ledger, api)
193
264
  releaseAuto = installBillionAuto()
194
- const timer = setInterval(() => { void ingest(); void refreshPeer() }, 3000); timer.unref?.()
265
+ const timer = setInterval(() => { void ingest(); void refreshPeer(); void archiveUsageJournals(ledger.path) }, 3000); timer.unref?.()
195
266
  ctx.effect(() => () => { clearInterval(timer); api.dispose(); bridges.delete(ledger) }, 'cost-meter: billion-context billing')
196
267
  ctx.on('session/event', (session, event) => {
197
268
  if (!Number.isSafeInteger(event.seq) || event.seq < 0) return
198
269
  const usage = event.type === 'assistant/message' || event.type === 'compaction/summary' ? event.data?.usage
199
270
  : event.type === 'assistant/chunk' && event.data?.chunk?.type === 'usage' ? event.data.chunk.usage : null
200
271
  if (!usage) return
201
- const candidates = [...streams.values()].filter(stream => stream.sessionId === session.id && stream.lastAck && stream.reported
202
- && usageSignature(stream.reported) === usageSignature(usage)
272
+ const candidates = [...streams.values()].filter(stream => stream.sessionId === session.id && stream.lastAck
273
+ && (stream.reported ? usageSignature(stream.reported) === usageSignature(usage)
274
+ : stream.finishedAt && stream.turn != null && stream.step != null && stream.turn === event.data?.turn && stream.step === event.data?.step && event.data?.message?.source?.model === stream.model)
203
275
  && (stream.turn == null || event.data?.turn == null || stream.turn === event.data.turn)
204
276
  && (stream.step == null || event.data?.step == null || stream.step === event.data.step)
205
277
  && (!event.data?.message?.source?.model || event.data.message.source.model === stream.model))
206
278
  if (candidates.length !== 1) return
207
279
  const stream = candidates[0]
280
+ const late = !stream.reported && validUsage(usageOnly(usage))
281
+ if (late) { stream.reported = usageOnly(usage); delete stream.coverage; stream.hostSuppressed = false }
208
282
  if (!stream.sourceSeqs.includes(event.seq) && stream.sourceSeqs.length < 32) {
209
283
  stream.sourceSeqs.push(event.seq)
210
284
  try { save(stream) } catch { status.failed = true }
211
285
  }
286
+ if (late) { for (const file of listBillionFiles(ledger.path, session.id)) files.delete(file.path); void ingest() }
212
287
  }, { global: true })
213
288
  void ingest()
214
289
  void refreshPeer()
@@ -1,7 +1,8 @@
1
1
  /** Sanitized proxy call journals and explicit links to the host's original usage. */
2
2
  import { createHash } from 'node:crypto'
3
- import { mkdirSync, lstatSync, readFileSync, writeFileSync, renameSync, readdirSync, createReadStream } from 'node:fs'
3
+ import { mkdirSync, lstatSync, readFileSync, writeFileSync, renameSync, readdirSync } from 'node:fs'
4
4
  import { join } from 'node:path'
5
+ import { journalChunks } from './journal-storage.js'
5
6
 
6
7
  export const BILLION_USAGE_EVENT = 'cost-meter/billion-context-usage'
7
8
  export const BILLION_REPLACEMENT_EVENT = 'cost-meter/billion-context-replacement'
@@ -41,14 +42,34 @@ export function validBillionCall(row) {
41
42
  function directories(path) {
42
43
  try { if (lstatSync(path).isSymbolicLink()) return []; return readdirSync(path, { withFileTypes: true }) } catch { return [] }
43
44
  }
45
+ export function listBillionStreamFiles(ledgerPath, sessionId) {
46
+ if (!ledgerPath) return []
47
+ const root = join(billionRoot(ledgerPath), 'streams')
48
+ const keys = sessionId ? [billionSessionKey(sessionId)] : directories(root).filter(row => row.isDirectory() && /^[a-f\d]{64}$/.test(row.name)).map(row => row.name)
49
+ return keys.flatMap(key => directories(join(root, key))
50
+ .filter(row => row.isFile() && row.name.endsWith('.json') && uuid(row.name.slice(0, -5)))
51
+ .map(row => ({ path: join(root, key, row.name), key })))
52
+ }
53
+ export function readBillionStreamFile(file) {
54
+ try {
55
+ const stream = readBillionJson(file.path)
56
+ return stream.protocol === 1 && uuid(stream.streamId) && validSessionId(stream.sessionId)
57
+ && billionSessionKey(stream.sessionId) === file.key && file.path.endsWith(stream.streamId + '.json') ? stream : null
58
+ } catch { return null }
59
+ }
44
60
  export function listBillionFiles(ledgerPath, sessionId) {
45
61
  if (!ledgerPath) return []
46
62
  const root = join(billionRoot(ledgerPath), 'calls')
47
63
  const sessions = sessionId ? [billionSessionKey(sessionId)] : directories(root).filter(row => row.isDirectory() && /^[a-f\d]{64}$/.test(row.name)).map(row => row.name)
48
64
  const files = []
65
+ let activeFiles = 0
49
66
  for (const key of sessions) for (const entry of directories(join(root, key))) {
50
- if (entry.isFile() && uuid(entry.name.replace(/\.jsonl$/, '')) && entry.name.endsWith('.jsonl')) files.push({ path: join(root, key, entry.name), key, source: entry.name.slice(0, -6) })
51
- if (files.length > 20000) throw Error('Proxy billing source limit exceeded')
67
+ const source = entry.name.replace(/(?:\.sealed)?\.jsonl(?:\.gz)?$/, '')
68
+ if (entry.isFile() && uuid(source) && /(?:\.sealed)?\.jsonl(?:\.gz)?$/.test(entry.name)) {
69
+ files.push({ path: join(root, key, entry.name), key, source })
70
+ if (!entry.name.endsWith('.sealed.jsonl.gz')) activeFiles++
71
+ }
72
+ if (activeFiles > 20000) throw Error('Active proxy billing source limit exceeded')
52
73
  }
53
74
  return files
54
75
  }
@@ -60,10 +81,8 @@ export function billionSourcePaths(ledgerPath, sessionId) {
60
81
  }
61
82
  /** Bounded UTF-8 rows; offsets only advance through complete newline-terminated writes. */
62
83
  export async function* iterateBillionCalls(file, start = 0) {
63
- const stat = lstatSync(file.path)
64
- if (!stat.isFile() || stat.isSymbolicLink() || stat.size > 256 * 1024 * 1024) throw Error('Invalid proxy billing journal')
65
84
  let pending = Buffer.alloc(0), dropping = false, offset = start
66
- for await (const chunk of createReadStream(file.path, { start, highWaterMark: 32 * 1024 })) {
85
+ for await (const chunk of journalChunks(file.path, start)) {
67
86
  const bytes = Buffer.concat([pending, chunk]); let from = 0, end
68
87
  while ((end = bytes.indexOf(10, from)) >= 0) {
69
88
  const line = bytes.subarray(from, end); offset += end - from + 1; from = end + 1
@@ -90,21 +109,77 @@ export function readBillionStream(ledgerPath, row) {
90
109
  return stream.protocol === 1 && stream.streamId === row.streamId && stream.sessionId === row.sessionId ? stream : null
91
110
  } catch { return null }
92
111
  }
112
+ const coverageCounts = () => ({ known: 0, unknown: 0, matched: false })
113
+ function observeCoverage(counts, stream, row) {
114
+ if (!['full', 'partial'].includes(stream.policies?.[row.groupId]) || row.kind !== 'model') return
115
+ if (row.usage === null) counts.unknown++
116
+ else { counts.known++; if (row.model === stream.model && usageSignature(row.usage) === usageSignature(stream.reported)) counts.matched = true }
117
+ }
118
+ function coverageResult(stream, { known, unknown, matched }) {
119
+ // One host sample belongs to one stream, never to each unknown attempt.
120
+ // Mixed aggregate/attempt usage cannot be separated without more evidence.
121
+ // Old covered:false records may already have ordinary host charges.
122
+ const eligible = stream.hostEligible === true || stream.covered === true
123
+ const mode = !eligible ? 'proxy' : !known && validUsage(stream.reported) ? 'host'
124
+ : unknown && !matched ? 'unresolved' : 'proxy'
125
+ return { version: 1, mode, known, unknown }
126
+ }
127
+ /** A persistence receipt says nothing about whether model usage was observed. */
128
+ export async function billionCoverage(ledgerPath, stream, offsets) {
129
+ if (stream.coverage?.version === 1) return stream.coverage
130
+ const counts = coverageCounts()
131
+ for (const file of listBillionFiles(ledgerPath, stream.sessionId)) for await (const { row } of iterateBillionCalls(file, offsets?.get(file.path)?.offset ?? 0)) {
132
+ if (row.streamId === stream.streamId) observeCoverage(counts, stream, row)
133
+ }
134
+ return coverageResult(stream, counts)
135
+ }
136
+ /** Legacy streams share one session scan instead of rescanning for every call. */
137
+ export async function billionCoverageIndex(ledgerPath, sessionId) {
138
+ const entries = new Map()
139
+ for (const file of listBillionFiles(ledgerPath, sessionId)) for await (const { row } of iterateBillionCalls(file)) {
140
+ if (!entries.has(row.streamId)) {
141
+ if (entries.size >= 64000) throw Error('Proxy coverage stream limit exceeded')
142
+ entries.set(row.streamId, { stream: readBillionStream(ledgerPath, row), counts: coverageCounts() })
143
+ }
144
+ const { stream, counts } = entries.get(row.streamId)
145
+ if (stream?.finished && !stream.coverage) observeCoverage(counts, stream, row)
146
+ }
147
+ return new Map([...entries].filter(([, value]) => value.stream?.finished)
148
+ .map(([id, { stream, counts }]) => [id, stream.coverage ?? coverageResult(stream, counts)]))
149
+ }
150
+ export const billionHostMeter = stream => ({ source: stream.streamId + ':' + billionSessionKey('host:' + stream.sessionId), seq: 1 })
151
+ export function billionHostRecord(stream) {
152
+ return { type: BILLION_USAGE_EVENT, time: stream.startedAtMs, data: { provider: stream.provider, model: stream.model, usage: stream.reported,
153
+ startedAtMs: stream.startedAtMs, requestId: stream.streamId, kind: 'model', turn: stream.turn ?? null, step: stream.step ?? null,
154
+ metering: { source: 'billion-context', requestId: stream.streamId, attempt: 1, complete: false, status: 'complete', upstream: 'host-usage' } } }
155
+ }
93
156
  export async function billionRecordsFor(ledgerPath, sessionId) {
94
- const records = [], streams = new Map(), seen = new Set()
157
+ const records = [], streams = new Map(), counts = new Map(), seen = new Set()
158
+ for (const file of listBillionStreamFiles(ledgerPath, sessionId)) {
159
+ const stream = readBillionStreamFile(file)
160
+ if (stream?.finished) streams.set(stream.streamId, stream)
161
+ }
95
162
  for (const file of listBillionFiles(ledgerPath, sessionId)) for await (const { row } of iterateBillionCalls(file)) {
96
- if (!row.usage || seen.has(row.callId)) continue
163
+ if (seen.has(row.callId)) continue
97
164
  const stream = readBillionStream(ledgerPath, row)
98
165
  if (!stream?.finished || !['full', 'partial'].includes(stream.policies?.[row.groupId])) continue
99
166
  seen.add(row.callId); streams.set(stream.streamId, stream)
167
+ if (seen.size > 200000) throw Error('Proxy billing detail limit exceeded')
168
+ if (!counts.has(stream.streamId)) counts.set(stream.streamId, coverageCounts())
169
+ observeCoverage(counts.get(stream.streamId), stream, row)
170
+ if (!row.usage) continue
100
171
  records.push({ type: BILLION_USAGE_EVENT, time: row.startedAtMs, data: { provider: billionProvider(row, stream), model: row.model, usage: row.usage,
101
172
  startedAtMs: row.startedAtMs, requestId: row.callId, kind: row.kind, turn: stream.turn ?? null, step: stream.step ?? null,
102
173
  metering: { source: 'billion-context', requestId: row.callId, attempt: row.attempt, complete: row.complete, status: row.status, upstream: row.upstream } } })
103
174
  if (records.length > 200000) throw Error('Proxy billing detail limit exceeded')
104
175
  }
105
- for (const stream of streams.values()) if (stream.covered && validUsage(stream.reported)) records.push({ type: BILLION_REPLACEMENT_EVENT, data: {
106
- streamId: stream.streamId, sourceSeqs: stream.sourceSeqs ?? [], turn: stream.turn ?? null, step: stream.step ?? null, usage: stream.reported,
107
- provider: stream.provider, model: stream.model } })
176
+ for (const stream of streams.values()) {
177
+ const coverage = stream.coverage ?? coverageResult(stream, counts.get(stream.streamId) ?? coverageCounts())
178
+ if (coverage.mode === 'host') records.push(billionHostRecord(stream))
179
+ if ((stream.covered || stream.hostSuppressed || coverage.mode === 'host' || coverage.mode === 'unresolved') && validUsage(stream.reported)) records.push({ type: BILLION_REPLACEMENT_EVENT, data: {
180
+ streamId: stream.streamId, sourceSeqs: stream.sourceSeqs ?? [], turn: stream.turn ?? null, step: stream.step ?? null, usage: stream.reported,
181
+ provider: stream.provider, model: stream.model } })
182
+ }
108
183
  return records
109
184
  }
110
185
  export function excludeBillionReportedUsage(records) {
@@ -4,6 +4,7 @@ import { randomUUID, createHash } from 'node:crypto'
4
4
  import { readFileSync, lstatSync, mkdirSync, openSync, writeSync, closeSync, fsyncSync, writeFileSync, renameSync } from 'node:fs'
5
5
  import { homedir } from 'node:os'
6
6
  import { join, resolve } from 'node:path'
7
+ import { JOURNAL_SEGMENT_BYTES, notifyJournalChange } from './journal-storage.js'
7
8
 
8
9
  const scope = new AsyncLocalStorage(), producer = randomUUID(), sequences = new Map()
9
10
  export const BILLION_GROUP_HEADER = 'x-dsh-cost-meter-group'
@@ -148,10 +149,19 @@ function finish(call, status) {
148
149
  try {
149
150
  const sessionKey = createHash('sha256').update(call.sessionId).digest('hex')
150
151
  const root = join(call.root, 'calls', sessionKey); safeDirectory(root)
151
- const file = join(root, producer + '.jsonl')
152
- try { const info = lstatSync(file); if (!info.isFile() || info.isSymbolicLink()) return } catch (error) { if (error.code !== 'ENOENT') return }
153
- const seq = (sequences.get(root) ?? 0) + 1
154
- const row = { protocol: 1, source: producer, seq, callId: call.callId, groupId: call.groupId, streamId: call.streamId, sessionId: call.sessionId,
152
+ let state = sequences.get(root) ?? { source: producer, seq: 0 }
153
+ let file = join(root, state.source + '.jsonl')
154
+ try {
155
+ const info = lstatSync(file)
156
+ if (!info.isFile() || info.isSymbolicLink()) return
157
+ if (info.size >= JOURNAL_SEGMENT_BYTES) {
158
+ renameSync(file, join(root, state.source + '.sealed.jsonl'))
159
+ state = { source: randomUUID(), seq: 0 }; sequences.set(root, state)
160
+ file = join(root, state.source + '.jsonl')
161
+ }
162
+ } catch (error) { if (error.code !== 'ENOENT') return }
163
+ const seq = state.seq + 1
164
+ const row = { protocol: 1, source: state.source, seq, callId: call.callId, groupId: call.groupId, streamId: call.streamId, sessionId: call.sessionId,
155
165
  kind: call.kind, attempt: call.attempt, upstream: call.upstream, model: call.model, startedAtMs: call.startedAtMs, status, complete: call.terminal,
156
166
  usage: call.usage }
157
167
  const fd = openSync(file, 'a', 0o600)
@@ -160,8 +170,9 @@ function finish(call, status) {
160
170
  let offset = 0
161
171
  while (offset < bytes.length) offset += writeSync(fd, bytes, offset, bytes.length - offset)
162
172
  fsyncSync(fd)
163
- sequences.set(root, seq)
173
+ sequences.set(root, { source: state.source, seq })
164
174
  written = true
175
+ if (seq === 1) { try { notifyJournalChange(call.root) } catch {} }
165
176
  } finally { closeSync(fd) }
166
177
  } catch { /* Metering failure never interrupts a model response. */ }
167
178
  finally {