@schlessera/brain-ui-server 0.18.0 → 0.20.0

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 (130) hide show
  1. package/README.md +5 -1
  2. package/data/model-prices.json +63 -0
  3. package/dist/activity/digest.d.ts +29 -0
  4. package/dist/activity/digest.d.ts.map +1 -0
  5. package/dist/activity/digest.js +89 -0
  6. package/dist/activity/digest.js.map +1 -0
  7. package/dist/activity/notify.d.ts +62 -0
  8. package/dist/activity/notify.d.ts.map +1 -0
  9. package/dist/activity/notify.js +193 -0
  10. package/dist/activity/notify.js.map +1 -0
  11. package/dist/activity/push-sender.d.ts +63 -0
  12. package/dist/activity/push-sender.d.ts.map +1 -0
  13. package/dist/activity/push-sender.js +131 -0
  14. package/dist/activity/push-sender.js.map +1 -0
  15. package/dist/activity/query.d.ts +16 -0
  16. package/dist/activity/query.d.ts.map +1 -0
  17. package/dist/activity/query.js +155 -0
  18. package/dist/activity/query.js.map +1 -0
  19. package/dist/activity/recorder.d.ts +53 -0
  20. package/dist/activity/recorder.d.ts.map +1 -0
  21. package/dist/activity/recorder.js +238 -0
  22. package/dist/activity/recorder.js.map +1 -0
  23. package/dist/activity/runtime.d.ts +37 -0
  24. package/dist/activity/runtime.d.ts.map +1 -0
  25. package/dist/activity/runtime.js +127 -0
  26. package/dist/activity/runtime.js.map +1 -0
  27. package/dist/activity/span-sink.d.ts +22 -0
  28. package/dist/activity/span-sink.d.ts.map +1 -0
  29. package/dist/activity/span-sink.js +138 -0
  30. package/dist/activity/span-sink.js.map +1 -0
  31. package/dist/activity/store.d.ts +256 -0
  32. package/dist/activity/store.d.ts.map +1 -0
  33. package/dist/activity/store.js +657 -0
  34. package/dist/activity/store.js.map +1 -0
  35. package/dist/activity/stream.d.ts +49 -0
  36. package/dist/activity/stream.d.ts.map +1 -0
  37. package/dist/activity/stream.js +353 -0
  38. package/dist/activity/stream.js.map +1 -0
  39. package/dist/agent/backend.d.ts +13 -4
  40. package/dist/agent/backend.d.ts.map +1 -1
  41. package/dist/agent/backend.js +68 -5
  42. package/dist/agent/backend.js.map +1 -1
  43. package/dist/app.d.ts.map +1 -1
  44. package/dist/app.js +33 -2
  45. package/dist/app.js.map +1 -1
  46. package/dist/config/env.d.ts +36 -0
  47. package/dist/config/env.d.ts.map +1 -1
  48. package/dist/config/env.js +66 -2
  49. package/dist/config/env.js.map +1 -1
  50. package/dist/cron/scheduler.d.ts.map +1 -1
  51. package/dist/cron/scheduler.js +20 -5
  52. package/dist/cron/scheduler.js.map +1 -1
  53. package/dist/db/client.d.ts.map +1 -1
  54. package/dist/db/client.js +32 -6
  55. package/dist/db/client.js.map +1 -1
  56. package/dist/db/settings.d.ts +15 -0
  57. package/dist/db/settings.d.ts.map +1 -1
  58. package/dist/db/settings.js +43 -0
  59. package/dist/db/settings.js.map +1 -1
  60. package/dist/index.d.ts +4 -0
  61. package/dist/index.d.ts.map +1 -1
  62. package/dist/index.js +7 -0
  63. package/dist/index.js.map +1 -1
  64. package/dist/pricing/model-pricing.d.ts +61 -0
  65. package/dist/pricing/model-pricing.d.ts.map +1 -0
  66. package/dist/pricing/model-pricing.js +379 -0
  67. package/dist/pricing/model-pricing.js.map +1 -0
  68. package/dist/routes/activity.d.ts +20 -0
  69. package/dist/routes/activity.d.ts.map +1 -0
  70. package/dist/routes/activity.js +308 -0
  71. package/dist/routes/activity.js.map +1 -0
  72. package/dist/routes/models.d.ts +6 -0
  73. package/dist/routes/models.d.ts.map +1 -1
  74. package/dist/routes/models.js +38 -3
  75. package/dist/routes/models.js.map +1 -1
  76. package/dist/routes/push.d.ts +6 -0
  77. package/dist/routes/push.d.ts.map +1 -0
  78. package/dist/routes/push.js +73 -0
  79. package/dist/routes/push.js.map +1 -0
  80. package/dist/routes/sessions.d.ts.map +1 -1
  81. package/dist/routes/sessions.js +20 -4
  82. package/dist/routes/sessions.js.map +1 -1
  83. package/dist/ws/bridge.d.ts +2 -1
  84. package/dist/ws/bridge.d.ts.map +1 -1
  85. package/dist/ws/bridge.js +27 -2
  86. package/dist/ws/bridge.js.map +1 -1
  87. package/dist/ws/connection.d.ts.map +1 -1
  88. package/dist/ws/connection.js +9 -1
  89. package/dist/ws/connection.js.map +1 -1
  90. package/dist/ws/dispatch.d.ts.map +1 -1
  91. package/dist/ws/dispatch.js +11 -0
  92. package/dist/ws/dispatch.js.map +1 -1
  93. package/dist/ws/host.d.ts +16 -0
  94. package/dist/ws/host.d.ts.map +1 -1
  95. package/dist/ws/host.js +2 -0
  96. package/dist/ws/host.js.map +1 -1
  97. package/dist/ws/run-session.d.ts.map +1 -1
  98. package/dist/ws/run-session.js +55 -1
  99. package/dist/ws/run-session.js.map +1 -1
  100. package/migrations/007_activity.sql +114 -0
  101. package/migrations/008_push_subscriptions.sql +24 -0
  102. package/migrations/009_activity_followups.sql +22 -0
  103. package/migrations/010_effective_cost.sql +21 -0
  104. package/package.json +6 -3
  105. package/src/activity/digest.ts +120 -0
  106. package/src/activity/notify.ts +307 -0
  107. package/src/activity/push-sender.ts +215 -0
  108. package/src/activity/query.ts +187 -0
  109. package/src/activity/recorder.ts +313 -0
  110. package/src/activity/runtime.ts +169 -0
  111. package/src/activity/span-sink.ts +152 -0
  112. package/src/activity/store.ts +1069 -0
  113. package/src/activity/stream.ts +396 -0
  114. package/src/agent/backend.ts +91 -10
  115. package/src/app.ts +35 -2
  116. package/src/config/env.ts +90 -2
  117. package/src/cron/scheduler.ts +28 -8
  118. package/src/db/client.ts +31 -6
  119. package/src/db/settings.ts +56 -0
  120. package/src/index.ts +23 -0
  121. package/src/pricing/model-pricing.ts +497 -0
  122. package/src/routes/activity.ts +371 -0
  123. package/src/routes/models.ts +57 -6
  124. package/src/routes/push.ts +90 -0
  125. package/src/routes/sessions.ts +24 -4
  126. package/src/ws/bridge.ts +29 -2
  127. package/src/ws/connection.ts +9 -1
  128. package/src/ws/dispatch.ts +13 -0
  129. package/src/ws/host.ts +18 -0
  130. package/src/ws/run-session.ts +71 -2
@@ -0,0 +1,657 @@
1
+ import { isBillingMode } from "@schlessera/brain-ui-sdk/protocol";
2
+ import { resolveAmbientBillingMode, resolveStandalonePricingConfig, } from "../config/env.js";
3
+ import { createModelPricing } from "../pricing/model-pricing.js";
4
+ export const SPAN_OUTCOMES = [
5
+ "success",
6
+ "error",
7
+ "timeout",
8
+ "cancelled",
9
+ "denied",
10
+ "interrupted",
11
+ ];
12
+ /** The one snake→camel mapper for rollup rows — every reader shares it. */
13
+ export function rowToRunRollup(r) {
14
+ return {
15
+ runId: r.run_id,
16
+ origin: r.origin,
17
+ name: r.name,
18
+ sessionId: r.session_id,
19
+ jobName: r.job_name,
20
+ startedAt: r.started_at,
21
+ endedAt: r.ended_at,
22
+ outcome: r.outcome,
23
+ durationMs: r.duration_ms,
24
+ spanCount: r.span_count,
25
+ inputTokens: r.input_tokens,
26
+ outputTokens: r.output_tokens,
27
+ cacheReadTokens: r.cache_read_tokens,
28
+ cacheCreationTokens: r.cache_creation_tokens,
29
+ costUsd: r.cost_usd,
30
+ effectiveCostUsd: r.effective_cost_usd ?? null,
31
+ // Migration 010 CHECKs this column, but a row written around them (older
32
+ // binary, manual edit) must degrade to unknown, never to a wrong mode.
33
+ billingMode: isBillingMode(r.billing_mode) ? r.billing_mode : null,
34
+ pricingEstimate: r.pricing_estimate == null ? null : r.pricing_estimate === 1,
35
+ failureReason: r.failure_reason,
36
+ detailPruned: r.detail_pruned === 1,
37
+ };
38
+ }
39
+ /**
40
+ * Sum-of-KNOWNS over effective costs: unknown (NULL) rows contribute nothing
41
+ * to the sum and are counted instead, so no aggregate can pass an unknown off
42
+ * as $0 (AE3). One definition for every reader that folds rollup rows.
43
+ */
44
+ export function sumEffectiveCost(rows) {
45
+ let effectiveCostUsd = 0;
46
+ let unpricedRuns = 0;
47
+ for (const row of rows) {
48
+ if (row.effectiveCostUsd === null)
49
+ unpricedRuns += 1;
50
+ else
51
+ effectiveCostUsd += row.effectiveCostUsd;
52
+ }
53
+ return { effectiveCostUsd, unpricedRuns };
54
+ }
55
+ /** A single event payload is capped so no delta can approach the WS frame cap. */
56
+ export const MAX_EVENT_PAYLOAD_BYTES = 16_384;
57
+ export function createActivityStore(db, options = {}) {
58
+ const writer = options.writer ?? `pid:${process.pid}:${Date.now()}`;
59
+ // Lazy so a store that never rolls up (span-only paths, most tests)
60
+ // touches neither the env nor the disk. The env reads live in
61
+ // config/env.ts — the one chokepoint the env-access gate allows. Every
62
+ // wrapper whose transaction can reach rollupRunInTx calls this BEFORE
63
+ // entering `inWrite`, so the SQLite write lock never covers the two
64
+ // synchronous JSON reads construction costs.
65
+ let pricing = options.pricing;
66
+ const getPricing = () => (pricing ??= createModelPricing(resolveStandalonePricingConfig()));
67
+ // bun:sqlite transactions: `.immediate` takes the write lock up front, so a
68
+ // concurrent writer waits (busy_timeout) instead of failing mid-upgrade.
69
+ const inWrite = (fn) => db.transaction(fn).immediate();
70
+ function nextSeq(runId) {
71
+ const row = db
72
+ .query("SELECT COALESCE(MAX(seq), 0) AS hi FROM activity_changes WHERE run_id = ?")
73
+ .get(runId);
74
+ return row.hi + 1;
75
+ }
76
+ function logChange(runId, seq, spanId, eventIndex) {
77
+ db.query("INSERT INTO activity_changes (run_id, seq, span_id, event_index) VALUES (?, ?, ?, ?)").run(runId, seq, spanId, eventIndex ?? null);
78
+ }
79
+ function rowToSpan(r) {
80
+ return {
81
+ spanId: r.span_id,
82
+ runId: r.run_id,
83
+ parentSpanId: r.parent_span_id,
84
+ name: r.name,
85
+ kind: r.kind,
86
+ origin: r.origin,
87
+ sessionId: r.session_id,
88
+ jobName: r.job_name,
89
+ attrs: safeParse(r.attrs) ?? {},
90
+ startedAt: r.started_at,
91
+ waitUntil: r.wait_until,
92
+ endedAt: r.ended_at,
93
+ outcome: r.outcome,
94
+ outcomeReason: r.outcome_reason,
95
+ usage: {
96
+ inputTokens: r.input_tokens ?? undefined,
97
+ outputTokens: r.output_tokens ?? undefined,
98
+ cacheReadTokens: r.cache_read_tokens ?? undefined,
99
+ cacheCreationTokens: r.cache_creation_tokens ?? undefined,
100
+ costUsd: r.cost_usd ?? undefined,
101
+ model: r.model ?? undefined,
102
+ },
103
+ writer: r.writer,
104
+ lastHeartbeatAt: r.last_heartbeat_at,
105
+ };
106
+ }
107
+ function rowToEvent(r) {
108
+ const parsed = safeParse(r.payload);
109
+ return {
110
+ spanId: r.span_id,
111
+ eventIndex: r.event_index,
112
+ ts: r.ts,
113
+ eventType: r.event_type,
114
+ payload: parsed?.v,
115
+ truncated: parsed?.truncated === true,
116
+ };
117
+ }
118
+ function getSpanRaw(spanId) {
119
+ const r = db.query("SELECT * FROM activity_spans WHERE span_id = ?").get(spanId);
120
+ return r ? rowToSpan(r) : null;
121
+ }
122
+ function runHighWaterSeqRaw(runId) {
123
+ const row = db
124
+ .query("SELECT COALESCE(MAX(seq), 0) AS hi FROM activity_changes WHERE run_id = ?")
125
+ .get(runId);
126
+ return row.hi;
127
+ }
128
+ function endSpanInTx(spanId, input) {
129
+ const existing = db
130
+ .query("SELECT run_id, outcome, attrs FROM activity_spans WHERE span_id = ?")
131
+ .get(spanId);
132
+ if (!existing || existing.outcome !== null)
133
+ return false;
134
+ const endedAt = input.endedAt ?? Date.now();
135
+ const attrs = input.attrs
136
+ ? JSON.stringify({ ...(safeParse(existing.attrs) ?? {}), ...input.attrs })
137
+ : existing.attrs;
138
+ const u = input.usage ?? {};
139
+ db.query(`UPDATE activity_spans SET outcome = ?, outcome_reason = ?, ended_at = ?, attrs = ?,
140
+ input_tokens = COALESCE(?, input_tokens),
141
+ output_tokens = COALESCE(?, output_tokens),
142
+ cache_read_tokens = COALESCE(?, cache_read_tokens),
143
+ cache_creation_tokens = COALESCE(?, cache_creation_tokens),
144
+ cost_usd = COALESCE(?, cost_usd),
145
+ model = COALESCE(?, model)
146
+ WHERE span_id = ?`).run(input.outcome, input.reason ?? null, endedAt, attrs, u.inputTokens ?? null, u.outputTokens ?? null, u.cacheReadTokens ?? null, u.cacheCreationTokens ?? null, u.costUsd ?? null, u.model ?? null, spanId);
147
+ logChange(existing.run_id, nextSeq(existing.run_id), spanId);
148
+ return true;
149
+ }
150
+ /** Close every open span of the run (children first) and roll it up. */
151
+ function closeRunInTx(runId, outcome, reason) {
152
+ const open = db
153
+ .query("SELECT span_id FROM activity_spans WHERE run_id = ? AND outcome IS NULL ORDER BY started_at DESC")
154
+ .all(runId);
155
+ let closed = 0;
156
+ for (const { span_id } of open) {
157
+ if (endSpanInTx(span_id, { outcome, reason }))
158
+ closed++;
159
+ }
160
+ if (closed > 0)
161
+ rollupRunInTx(runId);
162
+ return closed;
163
+ }
164
+ function rollupRunInTx(runId) {
165
+ const spans = db
166
+ .query("SELECT * FROM activity_spans WHERE run_id = ? ORDER BY started_at")
167
+ .all(runId)
168
+ .map(rowToSpan);
169
+ if (spans.length === 0)
170
+ return;
171
+ const root = spans.find((s) => s.parentSpanId === null) ?? spans[0];
172
+ // Aggregation scope: ROOT spans only. Result-level accounting (the
173
+ // SDK's modelUsage) already includes subagent consumption — summing the
174
+ // tree would double-count every fan-out. Verified empirically (plan U3
175
+ // smoke test).
176
+ const failure = spans.find((s) => (s.outcome === "error" || s.outcome === "timeout") && s.outcomeReason != null)?.outcomeReason ?? (root.outcome === "interrupted" ? "interrupted" : null);
177
+ // Billing rides the root span when the recorder knew it at run start
178
+ // (session runs, U3). A root without the attr classifies from THIS
179
+ // process's env ONLY for non-session origins: for cron rollups the
180
+ // executing process is the wrapper itself — exactly the credential set
181
+ // the job's agent authenticated under — so classification and reality
182
+ // move together. A SESSION root without the attr (a custom backend the
183
+ // registry could not classify, or a failed profile resolution) stays
184
+ // unknown instead: this process's ambient credentials say nothing about
185
+ // whichever backend ran the turn, and a wrong subscription-$0 would
186
+ // freeze forever where unknown stays honestly unpriced.
187
+ const attrBilling = root.attrs["brain.billing_mode"];
188
+ const billingMode = isBillingMode(attrBilling)
189
+ ? attrBilling
190
+ : root.origin === "session"
191
+ ? null
192
+ : resolveAmbientBillingMode();
193
+ // List-price math runs regardless of billing mode: it gap-fills a
194
+ // missing backend cost_usd (pi without snapshots, cron) AND provides the
195
+ // api-billed effective number. resolve() is synchronous by contract —
196
+ // nothing here may await inside the write transaction.
197
+ const usage = usageForPricing(root, spans);
198
+ const priced = usage.kind === "usage" ? priceUsage(usage.byModel, getPricing()) : null;
199
+ // Effective-cost semantics (NULL = unknown, 0 = genuinely free):
200
+ // subscription → 0 regardless of tokens (AE1); api → the priced sum —
201
+ // 0 when the run verifiably consumed nothing, NULL when usage was never
202
+ // recorded (denied before inference) or any contributing model/rate is
203
+ // missing. The estimate flag qualifies the effective number, so it is
204
+ // NULL exactly when that is, and 0 for a subscription $0 (exact, not
205
+ // estimated).
206
+ // An unclassified run (billingMode null — session root without the
207
+ // attr) prices as unknown: no subscription-zero, no api pricing.
208
+ const effectiveCostUsd = billingMode === null ? null : billingMode === "subscription" ? 0 : (priced?.costUsd ?? null);
209
+ const pricingEstimate = billingMode === null
210
+ ? null
211
+ : billingMode === "subscription"
212
+ ? 0
213
+ : priced
214
+ ? (priced.estimate ? 1 : 0)
215
+ : null;
216
+ const costUsd = root.usage.costUsd ?? priced?.costUsd ?? null;
217
+ db.query(`INSERT INTO activity_run_rollups
218
+ (run_id, origin, name, session_id, job_name, started_at, ended_at, outcome,
219
+ duration_ms, span_count, input_tokens, output_tokens, cache_read_tokens,
220
+ cache_creation_tokens, cost_usd, effective_cost_usd, billing_mode,
221
+ pricing_estimate, failure_reason, detail_pruned)
222
+ VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, 0)
223
+ ON CONFLICT(run_id) DO UPDATE SET
224
+ ended_at = excluded.ended_at, outcome = excluded.outcome,
225
+ duration_ms = excluded.duration_ms, span_count = excluded.span_count,
226
+ input_tokens = excluded.input_tokens, output_tokens = excluded.output_tokens,
227
+ cache_read_tokens = excluded.cache_read_tokens,
228
+ cache_creation_tokens = excluded.cache_creation_tokens,
229
+ cost_usd = COALESCE(activity_run_rollups.cost_usd, excluded.cost_usd),
230
+ effective_cost_usd = CASE
231
+ WHEN activity_run_rollups.effective_cost_usd IS NOT NULL
232
+ THEN activity_run_rollups.effective_cost_usd
233
+ WHEN activity_run_rollups.billing_mode IS NULL
234
+ OR activity_run_rollups.billing_mode = excluded.billing_mode
235
+ THEN excluded.effective_cost_usd
236
+ ELSE NULL
237
+ END,
238
+ billing_mode = COALESCE(activity_run_rollups.billing_mode, excluded.billing_mode),
239
+ pricing_estimate = CASE
240
+ WHEN activity_run_rollups.pricing_estimate IS NOT NULL
241
+ THEN activity_run_rollups.pricing_estimate
242
+ WHEN activity_run_rollups.billing_mode IS NULL
243
+ OR activity_run_rollups.billing_mode = excluded.billing_mode
244
+ THEN excluded.pricing_estimate
245
+ ELSE NULL
246
+ END,
247
+ failure_reason = excluded.failure_reason`
248
+ // Every cost column is FROZEN at first non-NULL write (AE5): a re-rollup
249
+ // after a pricing refresh must not silently reprice history — only a
250
+ // still-NULL slot may be filled by a later computation. cost_usd gets
251
+ // the plain first-write-wins COALESCE (the terminal endSpan already
252
+ // merged the backend's authoritative number before the first rollup).
253
+ // The effective/estimate pair additionally requires the LATER fill to
254
+ // agree with the frozen classification: billing_mode is first-write-wins,
255
+ // and a slot left NULL under one classification must never be filled by
256
+ // a number computed under a different one (a subscription $0 landing on
257
+ // an api-classified row would fabricate a cross-classified price).
258
+ ).run(runId, root.origin, root.name, root.sessionId, root.jobName, root.startedAt, root.endedAt, root.outcome, root.endedAt !== null ? root.endedAt - root.startedAt : null, spans.length, root.usage.inputTokens ?? null, root.usage.outputTokens ?? null, root.usage.cacheReadTokens ?? null, root.usage.cacheCreationTokens ?? null, costUsd, effectiveCostUsd, billingMode, pricingEstimate, failure);
259
+ }
260
+ return {
261
+ writer,
262
+ startSpan(input) {
263
+ return inWrite(() => {
264
+ const startedAt = input.startedAt ?? Date.now();
265
+ db.query(`INSERT INTO activity_spans
266
+ (span_id, run_id, parent_span_id, name, kind, origin, session_id, job_name,
267
+ attrs, started_at, writer, last_heartbeat_at)
268
+ VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`).run(input.spanId, input.runId, input.parentSpanId ?? null, input.name, input.kind, input.origin, input.sessionId ?? null, input.jobName ?? null, input.attrs ? JSON.stringify(input.attrs) : null, startedAt, writer, input.parentSpanId ? null : startedAt);
269
+ logChange(input.runId, nextSeq(input.runId), input.spanId);
270
+ return getSpanRaw(input.spanId);
271
+ });
272
+ },
273
+ endSpan(spanId, input) {
274
+ return inWrite(() => endSpanInTx(spanId, input));
275
+ },
276
+ patchSpan(spanId, patch) {
277
+ return inWrite(() => {
278
+ const existing = db
279
+ .query("SELECT run_id, outcome, attrs FROM activity_spans WHERE span_id = ?")
280
+ .get(spanId);
281
+ if (!existing || existing.outcome !== null)
282
+ return false;
283
+ const attrs = patch.attrs
284
+ ? JSON.stringify({ ...(safeParse(existing.attrs) ?? {}), ...patch.attrs })
285
+ : existing.attrs;
286
+ const u = patch.usage ?? {};
287
+ db.query(`UPDATE activity_spans SET attrs = ?, wait_until = COALESCE(?, wait_until),
288
+ input_tokens = COALESCE(?, input_tokens),
289
+ output_tokens = COALESCE(?, output_tokens),
290
+ cache_read_tokens = COALESCE(?, cache_read_tokens),
291
+ cache_creation_tokens = COALESCE(?, cache_creation_tokens),
292
+ cost_usd = COALESCE(?, cost_usd),
293
+ model = COALESCE(?, model)
294
+ WHERE span_id = ?`).run(attrs, patch.waitUntil ?? null, u.inputTokens ?? null, u.outputTokens ?? null, u.cacheReadTokens ?? null, u.cacheCreationTokens ?? null, u.costUsd ?? null, u.model ?? null, spanId);
295
+ logChange(existing.run_id, nextSeq(existing.run_id), spanId);
296
+ return true;
297
+ });
298
+ },
299
+ appendEvent(spanId, eventType, payload, ts, cap) {
300
+ return inWrite(() => {
301
+ const span = db
302
+ .query("SELECT run_id FROM activity_spans WHERE span_id = ?")
303
+ .get(spanId);
304
+ if (!span)
305
+ return null;
306
+ const next = db
307
+ .query("SELECT COALESCE(MAX(event_index), -1) + 1 AS idx FROM activity_events WHERE span_id = ?")
308
+ .get(spanId);
309
+ const stored = capPayload(payload, cap);
310
+ const at = ts ?? Date.now();
311
+ db.query("INSERT INTO activity_events (span_id, event_index, ts, event_type, payload) VALUES (?, ?, ?, ?, ?)").run(spanId, next.idx, at, eventType, JSON.stringify(stored));
312
+ logChange(span.run_id, nextSeq(span.run_id), spanId, next.idx);
313
+ // Built from the values just written — no read-back needed.
314
+ return {
315
+ spanId,
316
+ eventIndex: next.idx,
317
+ ts: at,
318
+ eventType,
319
+ payload: stored.v,
320
+ truncated: stored.truncated === true,
321
+ };
322
+ });
323
+ },
324
+ heartbeat(spanId, at) {
325
+ inWrite(() => {
326
+ // Deliberately NOT change-logged: a heartbeat is liveness metadata,
327
+ // not activity the client needs a delta for.
328
+ db.query("UPDATE activity_spans SET last_heartbeat_at = ? WHERE span_id = ? AND outcome IS NULL").run(at ?? Date.now(), spanId);
329
+ });
330
+ },
331
+ cascadeClose(runId, outcome, reason) {
332
+ getPricing(); // construct outside the write lock; rollup reuses it
333
+ return inWrite(() => closeRunInTx(runId, outcome, reason));
334
+ },
335
+ sweepOwnOrphans() {
336
+ // "Own" means this PROCESS IDENTITY's rows from a previous life. The
337
+ // writer stamp includes the start time, so rows written by the
338
+ // current instance never match a fresh store's sweep... but a boot
339
+ // sweep runs before any spans are written, so sweeping by pid-prefix
340
+ // alone would be wrong across pid reuse. Sweep every session-origin
341
+ // open span instead: only THIS server writes session spans, and at
342
+ // boot none of ours can legitimately be open.
343
+ //
344
+ // Candidates are read OUTSIDE the write transaction — the common case
345
+ // finds nothing and must not take the exclusive lock for it. The close
346
+ // loop re-checks open-ness inside the transaction (endSpanInTx is
347
+ // write-once), so a race just skips.
348
+ const open = db
349
+ .query("SELECT DISTINCT run_id FROM activity_spans WHERE outcome IS NULL AND origin = 'session'")
350
+ .all();
351
+ if (open.length === 0)
352
+ return 0;
353
+ getPricing(); // construct outside the write lock; rollup reuses it
354
+ return inWrite(() => {
355
+ let closed = 0;
356
+ for (const { run_id } of open) {
357
+ closed += closeRunInTx(run_id, "interrupted", "server restarted");
358
+ }
359
+ return closed;
360
+ });
361
+ },
362
+ sweepStale(staleAfterMs, now) {
363
+ const cutoff = (now ?? Date.now()) - staleAfterMs;
364
+ // Staleness is judged on ROOT heartbeat age — never span age — so a
365
+ // legitimately long, quiet run with a live writer is never killed.
366
+ // Candidate read outside the write transaction, same as sweepOwnOrphans.
367
+ const staleRoots = db
368
+ .query(`SELECT span_id, run_id FROM activity_spans
369
+ WHERE outcome IS NULL AND parent_span_id IS NULL
370
+ AND writer != ? AND COALESCE(last_heartbeat_at, started_at) < ?`)
371
+ .all(writer, cutoff);
372
+ if (staleRoots.length === 0)
373
+ return 0;
374
+ getPricing(); // construct outside the write lock; rollup reuses it
375
+ return inWrite(() => {
376
+ let closed = 0;
377
+ for (const { run_id } of staleRoots) {
378
+ closed += closeRunInTx(run_id, "interrupted", "writer went silent");
379
+ }
380
+ return closed;
381
+ });
382
+ },
383
+ findStuck(thresholdMs, now) {
384
+ const cutoff = (now ?? Date.now()) - thresholdMs;
385
+ return db
386
+ .query("SELECT * FROM activity_spans WHERE outcome IS NULL AND parent_span_id IS NULL AND started_at < ?")
387
+ .all(cutoff).map(rowToSpan);
388
+ },
389
+ changesSince(changeCursor, limit = 500) {
390
+ // Set-based: one join per change kind instead of a lookup per row. A
391
+ // change whose span/event row was pruned drops out via the join, same
392
+ // as the old per-row miss.
393
+ const spanRows = db
394
+ .query(`SELECT c.change_id, c.run_id, c.seq, s.*
395
+ FROM activity_changes c
396
+ JOIN activity_spans s ON s.span_id = c.span_id
397
+ WHERE c.change_id > ? AND c.event_index IS NULL
398
+ ORDER BY c.change_id LIMIT ?`)
399
+ .all(changeCursor, limit);
400
+ const eventRows = db
401
+ .query(`SELECT c.change_id, c.run_id, c.seq, e.*
402
+ FROM activity_changes c
403
+ JOIN activity_events e
404
+ ON e.span_id = c.span_id AND e.event_index = c.event_index
405
+ WHERE c.change_id > ? AND c.event_index IS NOT NULL
406
+ ORDER BY c.change_id LIMIT ?`)
407
+ .all(changeCursor, limit);
408
+ const changes = [
409
+ ...spanRows.map((r) => ({
410
+ changeId: r.change_id,
411
+ runId: r.run_id,
412
+ seq: r.seq,
413
+ kind: "span",
414
+ span: rowToSpan(r),
415
+ })),
416
+ ...eventRows.map((r) => ({
417
+ changeId: r.change_id,
418
+ runId: r.run_id,
419
+ seq: r.seq,
420
+ kind: "event",
421
+ event: rowToEvent(r),
422
+ })),
423
+ ];
424
+ changes.sort((a, b) => a.changeId - b.changeId);
425
+ return changes.length > limit ? changes.slice(0, limit) : changes;
426
+ },
427
+ terminalRootChangesSince(changeCursor, limit = 500) {
428
+ const rows = db
429
+ .query(`SELECT MAX(c.change_id) AS change_id, s.*
430
+ FROM activity_changes c
431
+ JOIN activity_spans s ON s.span_id = c.span_id
432
+ WHERE c.change_id > ? AND c.event_index IS NULL
433
+ AND s.parent_span_id IS NULL AND s.outcome IS NOT NULL
434
+ GROUP BY s.span_id
435
+ ORDER BY change_id LIMIT ?`)
436
+ .all(changeCursor, limit);
437
+ return rows.map((r) => ({ changeId: r.change_id, span: rowToSpan(r) }));
438
+ },
439
+ latestChangeCursor() {
440
+ const row = db
441
+ .query("SELECT COALESCE(MAX(change_id), 0) AS hi FROM activity_changes")
442
+ .get();
443
+ return row.hi;
444
+ },
445
+ snapshotRun(runId) {
446
+ // A read transaction so spans, events, high-water seq, and the global
447
+ // cursor are one consistent picture — the poller baseline is pinned to
448
+ // exactly this moment (no gap between snapshot and first delta).
449
+ return db.transaction(() => {
450
+ const spans = db
451
+ .query("SELECT * FROM activity_spans WHERE run_id = ? ORDER BY started_at")
452
+ .all(runId).map(rowToSpan);
453
+ if (spans.length === 0)
454
+ return null;
455
+ const events = db
456
+ .query(`SELECT e.* FROM activity_events e
457
+ JOIN activity_spans s ON s.span_id = e.span_id
458
+ WHERE s.run_id = ? ORDER BY e.ts, e.event_index`)
459
+ .all(runId).map(rowToEvent);
460
+ const cursor = db
461
+ .query("SELECT COALESCE(MAX(change_id), 0) AS hi FROM activity_changes")
462
+ .get();
463
+ return {
464
+ runId,
465
+ spans,
466
+ events,
467
+ highWaterSeq: runHighWaterSeqRaw(runId),
468
+ changeCursor: cursor.hi,
469
+ };
470
+ })();
471
+ },
472
+ runHighWaterSeq: runHighWaterSeqRaw,
473
+ getSpan: getSpanRaw,
474
+ openRootSpans() {
475
+ return db
476
+ .query("SELECT * FROM activity_spans WHERE outcome IS NULL AND parent_span_id IS NULL ORDER BY started_at DESC")
477
+ .all().map(rowToSpan);
478
+ },
479
+ rollupRun(runId) {
480
+ getPricing(); // construct outside the write lock; rollup reuses it
481
+ inWrite(() => rollupRunInTx(runId));
482
+ },
483
+ prune(options) {
484
+ const now = options.now ?? Date.now();
485
+ const batch = options.batch ?? 50;
486
+ // The digest floor gates pruning, but a dead digest job must not freeze
487
+ // it forever: the hard ceiling prunes regardless (marking the rollup so
488
+ // the coverage gap is visible). The floor alone is NOT enough: it meant
489
+ // "safe to prune once summarized", but drill-in debugging is a second
490
+ // reader of detail with a different clock — nightly cron runs end
491
+ // before the morning digest, day sessions after, so floor-only pruning
492
+ // took cron span trees within the hour while sessions kept theirs (it
493
+ // looked cron-specific; it was clock skew). The retention cutoff ANDs
494
+ // with the floor so covered runs still keep detail for a minimum window.
495
+ const floor = Math.max(options.digestFloorAt, 0);
496
+ const retentionCutoff = now - options.detailRetentionMs;
497
+ const ceiling = now - options.hardCeilingMs;
498
+ return inWrite(() => {
499
+ // Candidates come from the rollups (they exist for every finished
500
+ // run — that terminal write is what made the run prunable), guarded
501
+ // against any run that still has an open span.
502
+ const candidates = db
503
+ .query(`SELECT run_id, ended_at AS ended FROM activity_run_rollups r
504
+ WHERE detail_pruned = 0 AND ended_at IS NOT NULL
505
+ AND ((ended_at < ? AND ended_at < ?) OR ended_at < ?)
506
+ AND NOT EXISTS (
507
+ SELECT 1 FROM activity_spans s
508
+ WHERE s.run_id = r.run_id AND s.outcome IS NULL
509
+ )
510
+ LIMIT ?`)
511
+ .all(floor, retentionCutoff, ceiling, batch);
512
+ let spansDeleted = 0;
513
+ for (const { run_id, ended } of candidates) {
514
+ db.query("UPDATE activity_run_rollups SET detail_pruned = 1 WHERE run_id = ?").run(run_id);
515
+ if (ended >= floor) {
516
+ // Pruned by the hard ceiling alone — the digest never covered
517
+ // this run; make the coverage gap visible on the rollup.
518
+ db.query("UPDATE activity_run_rollups SET failure_reason = COALESCE(failure_reason, 'digest coverage gap') WHERE run_id = ?").run(run_id);
519
+ }
520
+ db.query(`DELETE FROM activity_events WHERE span_id IN
521
+ (SELECT span_id FROM activity_spans WHERE run_id = ?)`).run(run_id);
522
+ const res = db.query("DELETE FROM activity_spans WHERE run_id = ?").run(run_id);
523
+ spansDeleted += res.changes;
524
+ db.query("DELETE FROM activity_changes WHERE run_id = ?").run(run_id);
525
+ }
526
+ // The change log only serves live polling; rows this deep in the past
527
+ // are unreachable by any live cursor. Keep runs with open spans.
528
+ db.query(`DELETE FROM activity_changes WHERE change_id < (
529
+ SELECT COALESCE(MAX(change_id), 0) FROM activity_changes
530
+ ) - 100000 AND run_id NOT IN (
531
+ SELECT DISTINCT run_id FROM activity_spans WHERE outcome IS NULL
532
+ )`).run();
533
+ return { runsPruned: candidates.length, spansDeleted };
534
+ });
535
+ },
536
+ };
537
+ }
538
+ function tokenCount(value) {
539
+ return typeof value === "number" && Number.isFinite(value) && value >= 0 ? value : undefined;
540
+ }
541
+ /**
542
+ * The usage a run is priced from. Session runs carry the SDK's per-model
543
+ * breakdown as a root attr (result-level accounting, subagents included —
544
+ * the same root-only scope the token rollup uses). Cron runs have no root
545
+ * aggregate: the wrapper's children carry the usage, summed per model here
546
+ * — the sanctioned origin-scoped exception to root-only aggregation, safe
547
+ * because sink children are not double-counted in their root.
548
+ */
549
+ function usageForPricing(root, spans) {
550
+ if (root.origin === "cron") {
551
+ // Null-prototype: model ids are foreign strings — an id like "__proto__"
552
+ // must be an ordinary key, never a prototype write.
553
+ const byModel = Object.create(null);
554
+ let sawUsage = false;
555
+ for (const span of spans) {
556
+ if (span.spanId === root.spanId)
557
+ continue;
558
+ const u = span.usage;
559
+ if (u.inputTokens == null &&
560
+ u.outputTokens == null &&
561
+ u.cacheReadTokens == null &&
562
+ u.cacheCreationTokens == null) {
563
+ continue; // no inference on this span (tool spans etc.) — not "unknown"
564
+ }
565
+ sawUsage = true;
566
+ // Tokens without a model cannot be priced at any rate — guessing one
567
+ // would silently misprice, so the whole run goes unknown.
568
+ if (!u.model)
569
+ return { kind: "unpriceable" };
570
+ const agg = (byModel[u.model] ??= {});
571
+ agg.inputTokens = (agg.inputTokens ?? 0) + (u.inputTokens ?? 0);
572
+ agg.outputTokens = (agg.outputTokens ?? 0) + (u.outputTokens ?? 0);
573
+ agg.cacheReadTokens = (agg.cacheReadTokens ?? 0) + (u.cacheReadTokens ?? 0);
574
+ agg.cacheCreationTokens = (agg.cacheCreationTokens ?? 0) + (u.cacheCreationTokens ?? 0);
575
+ }
576
+ return sawUsage ? { kind: "usage", byModel } : { kind: "none" };
577
+ }
578
+ const perModel = root.attrs["gen_ai.usage.per_model"];
579
+ if (typeof perModel !== "object" || perModel === null || Array.isArray(perModel)) {
580
+ return { kind: "none" };
581
+ }
582
+ const byModel = Object.create(null);
583
+ for (const [model, value] of Object.entries(perModel)) {
584
+ if (typeof value !== "object" || value === null)
585
+ continue;
586
+ const v = value;
587
+ byModel[model] = {
588
+ inputTokens: tokenCount(v.inputTokens) ?? 0,
589
+ outputTokens: tokenCount(v.outputTokens) ?? 0,
590
+ cacheReadTokens: tokenCount(v.cacheReadTokens) ?? 0,
591
+ cacheCreationTokens: tokenCount(v.cacheCreationTokens) ?? 0,
592
+ };
593
+ }
594
+ return Object.keys(byModel).length > 0 ? { kind: "usage", byModel } : { kind: "none" };
595
+ }
596
+ /**
597
+ * Price one run's per-model usage at list rates. Null = unknown: an
598
+ * unresolvable model with consumed tokens, or a consumed token class with no
599
+ * rate, poisons the WHOLE run — cache reads dominate Claude usage, so
600
+ * partial pricing would systematically understate (the binding
601
+ * missing-cache-rate decision). A model with zero consumption contributes
602
+ * nothing and needs no rate. Variant/snapshot fallbacks are the pricing
603
+ * service's job (`resolve()`); the store only propagates the estimate flag.
604
+ */
605
+ function priceUsage(byModel, pricing) {
606
+ let costUsd = 0;
607
+ let estimate = false;
608
+ for (const [model, tokens] of Object.entries(byModel)) {
609
+ const classes = [
610
+ [tokens.inputTokens ?? 0, (r) => r.input],
611
+ [tokens.outputTokens ?? 0, (r) => r.output],
612
+ [tokens.cacheReadTokens ?? 0, (r) => r.cacheRead],
613
+ [tokens.cacheCreationTokens ?? 0, (r) => r.cacheWrite],
614
+ ];
615
+ if (!classes.some(([count]) => count > 0))
616
+ continue;
617
+ const rates = pricing.resolve(model);
618
+ if (!rates)
619
+ return null;
620
+ for (const [count, rateOf] of classes) {
621
+ if (count <= 0)
622
+ continue;
623
+ const perToken = rateOf(rates);
624
+ if (perToken === null)
625
+ return null;
626
+ costUsd += count * perToken;
627
+ }
628
+ if (rates.estimate)
629
+ estimate = true;
630
+ }
631
+ // A non-finite sum (overflowed or poisoned rates) must surface as unknown —
632
+ // once frozen into the rollup it would render as an exact number forever.
633
+ return Number.isFinite(costUsd) ? { costUsd, estimate } : null;
634
+ }
635
+ function safeParse(text) {
636
+ if (!text)
637
+ return undefined;
638
+ try {
639
+ return JSON.parse(text);
640
+ }
641
+ catch {
642
+ return undefined;
643
+ }
644
+ }
645
+ /** Cap a payload's serialized size; a clipped payload carries the `truncated`
646
+ * flag (which rides the wire), so no in-text marker is stored. */
647
+ function capPayload(payload, cap) {
648
+ const limit = Math.min(cap ?? MAX_EVENT_PAYLOAD_BYTES, MAX_EVENT_PAYLOAD_BYTES);
649
+ const json = JSON.stringify(payload ?? null);
650
+ if (json.length <= limit)
651
+ return { v: payload ?? null };
652
+ if (typeof payload === "string") {
653
+ return { v: payload.slice(0, limit), truncated: true };
654
+ }
655
+ return { v: json.slice(0, limit), truncated: true };
656
+ }
657
+ //# sourceMappingURL=store.js.map