@genesislcap/ai-assistant 15.12.0 → 15.13.1

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 (50) hide show
  1. package/dist/ai-assistant.api.json +245 -3
  2. package/dist/ai-assistant.d.ts +127 -7
  3. package/dist/chat-driver.cjs +79 -13
  4. package/dist/chat-driver.cjs.map +2 -2
  5. package/dist/chat-driver.mjs +76 -12
  6. package/dist/chat-driver.mjs.map +2 -2
  7. package/dist/custom-elements.json +99 -6
  8. package/dist/dts/chat-driver-node.d.ts +2 -2
  9. package/dist/dts/chat-driver-node.d.ts.map +1 -1
  10. package/dist/dts/components/chat-driver/chat-driver.d.ts +45 -1
  11. package/dist/dts/components/chat-driver/chat-driver.d.ts.map +1 -1
  12. package/dist/dts/components/chat-driver/chat-driver.turn-usage.test.d.ts +2 -0
  13. package/dist/dts/components/chat-driver/chat-driver.turn-usage.test.d.ts.map +1 -0
  14. package/dist/dts/main/main.d.ts +20 -1
  15. package/dist/dts/main/main.d.ts.map +1 -1
  16. package/dist/dts/state/debug-event-log.d.ts.map +1 -1
  17. package/dist/dts/state/persistence/diagnostics.d.ts +65 -8
  18. package/dist/dts/state/persistence/diagnostics.d.ts.map +1 -1
  19. package/dist/dts/state/persistence/index.d.ts +1 -1
  20. package/dist/dts/state/persistence/index.d.ts.map +1 -1
  21. package/dist/dts/state/persistence/session-persister.d.ts +4 -3
  22. package/dist/dts/state/persistence/session-persister.d.ts.map +1 -1
  23. package/dist/dts/utils/sum-usage.d.ts +20 -0
  24. package/dist/dts/utils/sum-usage.d.ts.map +1 -1
  25. package/dist/esm/chat-driver-node.js +12 -2
  26. package/dist/esm/components/chat-driver/chat-driver.js +60 -5
  27. package/dist/esm/components/chat-driver/chat-driver.turn-usage.test.js +268 -0
  28. package/dist/esm/main/main.js +53 -28
  29. package/dist/esm/state/debug-event-log.js +7 -2
  30. package/dist/esm/state/persistence/diagnostics.js +79 -16
  31. package/dist/esm/state/persistence/diagnostics.test.js +174 -1
  32. package/dist/esm/state/persistence/index.js +1 -1
  33. package/dist/esm/state/persistence/session-persister.js +13 -5
  34. package/dist/esm/state/persistence/session-persister.test.js +31 -0
  35. package/dist/esm/utils/sum-usage.js +43 -0
  36. package/dist/esm/utils/sum-usage.test.js +45 -1
  37. package/dist/tsconfig.tsbuildinfo +1 -1
  38. package/package.json +17 -17
  39. package/src/chat-driver-node.ts +12 -2
  40. package/src/components/chat-driver/chat-driver.ts +107 -6
  41. package/src/components/chat-driver/chat-driver.turn-usage.test.ts +362 -0
  42. package/src/main/main.ts +52 -23
  43. package/src/state/debug-event-log.ts +7 -2
  44. package/src/state/persistence/diagnostics.test.ts +208 -1
  45. package/src/state/persistence/diagnostics.ts +117 -15
  46. package/src/state/persistence/index.ts +1 -1
  47. package/src/state/persistence/session-persister.test.ts +37 -0
  48. package/src/state/persistence/session-persister.ts +13 -5
  49. package/src/utils/sum-usage.test.ts +52 -1
  50. package/src/utils/sum-usage.ts +45 -0
@@ -0,0 +1,362 @@
1
+ import type {
2
+ AIProvider,
3
+ AIProviderRegistry,
4
+ ChatMessage,
5
+ ChatToolDefinition,
6
+ } from '@genesislcap/foundation-ai';
7
+ import { assert, createLogicSuite } from '@genesislcap/foundation-testing';
8
+ import type { AgentConfig } from '../../config/config';
9
+ import { clearMetaEventRegistry, getMetaEvents } from '../../state/debug-event-log';
10
+ import { messageUsage, sumUsage } from '../../utils/sum-usage';
11
+ // Side-effect import — MUST come before `./chat-driver` so the driver subclasses
12
+ // jsdom's EventTarget rather than Node's native one. Mirrors chat-driver.test.ts.
13
+ import './align-event-globals';
14
+ import { ChatDriver } from './chat-driver';
15
+
16
+ // ---------------------------------------------------------------------------
17
+ // Per-call usage on the turn snapshots — the four token buckets plus USD priced
18
+ // onto each `kind: 'turn'` entry of the exported debug log.
19
+ //
20
+ // The snapshot is captured BEFORE the model call (that is where the prompt, tool
21
+ // surface and agent state line up with what the model saw), so usage has to be
22
+ // back-filled when the response lands. Two things follow, and both are asserted
23
+ // here: a completed call's turn agrees with the message it produced, and a call
24
+ // that produced NO message (a blank response, retried) is still priced — that
25
+ // spend appears nowhere in the transcript.
26
+ // ---------------------------------------------------------------------------
27
+
28
+ const makeRegistry = (provider: AIProvider): AIProviderRegistry => ({
29
+ get: () => provider,
30
+ default: () => provider,
31
+ defaultName: () => 'test',
32
+ names: () => ['test'],
33
+ getStatus: async () => null,
34
+ listStatuses: async () => [],
35
+ });
36
+
37
+ /** Answers from a queue, then ends the turn with a plain unpriced reply. */
38
+ const scriptedProvider = (responses: ChatMessage[]): AIProvider => {
39
+ const queue = [...responses];
40
+ return {
41
+ chat: async (): Promise<ChatMessage> => queue.shift() ?? { role: 'assistant', content: 'done' },
42
+ };
43
+ };
44
+
45
+ const agent = (overrides: Partial<AgentConfig> & { name: string }): AgentConfig =>
46
+ ({ description: 'test agent', ...overrides }) as AgentConfig;
47
+
48
+ const makeDriver = (config: AgentConfig, provider: AIProvider): ChatDriver => {
49
+ const driver = new ChatDriver(makeRegistry(provider), {
50
+ maxToolIterations: 50,
51
+ maxFoldOperations: 5,
52
+ sessionKey: '',
53
+ });
54
+ driver.applyAgent(config);
55
+ return driver;
56
+ };
57
+
58
+ const def = (name: string): ChatToolDefinition => ({
59
+ name,
60
+ description: `${name} tool`,
61
+ parameters: { type: 'object', properties: {} },
62
+ });
63
+
64
+ /** One priced response: a 1000-token prompt that was mostly a cache hit. */
65
+ const priced = (over: Partial<ChatMessage> = {}): ChatMessage => ({
66
+ role: 'assistant',
67
+ content: 'answer',
68
+ cost: 0.1,
69
+ inputTokens: 1000,
70
+ cacheReadTokens: 900,
71
+ cacheWriteTokens: 50,
72
+ outputTokens: 20,
73
+ ...over,
74
+ });
75
+
76
+ const suite = createLogicSuite('ChatDriver per-turn usage');
77
+
78
+ suite('prices each turn snapshot with the four buckets and USD of its own call', async () => {
79
+ const driver = makeDriver(agent({ name: 'a' }), scriptedProvider([priced()]));
80
+ await driver.sendMessage('go');
81
+
82
+ const [turn] = driver.getTurnSnapshots();
83
+ assert.equal(
84
+ turn.usage,
85
+ {
86
+ costUsd: 0.1,
87
+ // The prompt split into disjoint buckets: 1000 total, 900 read + 50 written,
88
+ // so 50 uncached. A reader of the log gets this without re-deriving it.
89
+ uncachedInputTokens: 50,
90
+ cacheReadTokens: 900,
91
+ cacheWriteTokens: 50,
92
+ outputTokens: 20,
93
+ },
94
+ 'the turn carries its own call’s usage',
95
+ );
96
+ // The turn and the message it produced are the SAME charge, so they must agree
97
+ // exactly — a log whose two views of one call disagree is worse than one view.
98
+ assert.equal(turn.usage, sumUsage(driver.getHistory()), 'turn agrees with the transcript total');
99
+ });
100
+
101
+ suite('prices every model call in a tool loop separately', async () => {
102
+ const driver = makeDriver(
103
+ agent({
104
+ name: 'a',
105
+ toolDefinitions: [def('work')],
106
+ toolHandlers: { work: async () => 'worked' },
107
+ }),
108
+ scriptedProvider([
109
+ priced({ content: '', cost: 0.02, toolCalls: [{ id: 'w1', name: 'work', args: {} }] }),
110
+ priced({ cost: 0.03 }),
111
+ ]),
112
+ );
113
+ await driver.sendMessage('go');
114
+
115
+ const snapshots = driver.getTurnSnapshots();
116
+ assert.is(snapshots.length, 2, 'one snapshot per model call, not per user turn');
117
+ assert.equal(
118
+ snapshots.map((s) => s.usage?.costUsd),
119
+ [0.02, 0.03],
120
+ );
121
+ });
122
+
123
+ suite('prices a blank response that produced no message', async () => {
124
+ // A blank turn is billed and then discarded before the retry, so the transcript can
125
+ // never account for it. The snapshot is the only record — which is the whole reason
126
+ // usage is stamped before the empty-response branch runs.
127
+ const driver = makeDriver(
128
+ agent({ name: 'a' }),
129
+ scriptedProvider([
130
+ priced({ content: '', cost: 0.07, outputTokens: 0 }),
131
+ priced({ cost: 0.03 }),
132
+ ]),
133
+ );
134
+ await driver.sendMessage('go');
135
+
136
+ const snapshots = driver.getTurnSnapshots();
137
+ assert.is(snapshots.length, 2, 'the retried attempt has its own snapshot');
138
+ assert.is(snapshots[0].usage?.costUsd, 0.07, 'the discarded attempt is still priced');
139
+ assert.is(
140
+ sumUsage(driver.getHistory()).costUsd,
141
+ 0.03,
142
+ 'and is genuinely absent from the transcript — that is the gap the snapshot fills',
143
+ );
144
+ });
145
+
146
+ suite('leaves usage undefined when the provider reports none', async () => {
147
+ // Absent, not zeroed: "this provider reports no usage" (e.g. Chrome's built-in model)
148
+ // must not read as a free call.
149
+ const driver = makeDriver(agent({ name: 'a' }), scriptedProvider([]));
150
+ await driver.sendMessage('go');
151
+ assert.is(driver.getTurnSnapshots()[0].usage, undefined);
152
+ });
153
+
154
+ suite.run();
155
+
156
+ // ---------------------------------------------------------------------------
157
+ // The reasoning/narration/answer split must not multiply the cache buckets.
158
+ //
159
+ // One model response becomes up to three messages; only the last carries usage.
160
+ // The clearing list was written when usage meant cost + input + output, so when the
161
+ // cache buckets arrived they kept riding along on the display-only copies — and every
162
+ // bucket aggregate (session totals, usage rows, the exported log) counted a split
163
+ // turn's cache volume two or three times. The cost total hid it, since that comes
164
+ // from `cost`, which WAS cleared.
165
+ // ---------------------------------------------------------------------------
166
+
167
+ const split = createLogicSuite('ChatDriver response split usage');
168
+
169
+ split('counts a reasoning + answer split once, not twice', async () => {
170
+ const response = priced({ reasoning: 'thinking…' });
171
+ const driver = makeDriver(agent({ name: 'a' }), scriptedProvider([response]));
172
+ await driver.sendMessage('go');
173
+
174
+ const history = driver.getHistory();
175
+ assert.is(
176
+ history.filter((m) => m.role === 'assistant').length,
177
+ 2,
178
+ 'the response did split into a reasoning message and an answer',
179
+ );
180
+ assert.equal(
181
+ sumUsage(history),
182
+ messageUsage(response),
183
+ 'the split totals exactly one call’s usage',
184
+ );
185
+ const reasoning = history.find((m) => m.category === 'reasoning')!;
186
+ assert.is(reasoning.cacheReadTokens, undefined, 'the display-only copy carries no cache volume');
187
+ assert.is(reasoning.cacheWriteTokens, undefined);
188
+ });
189
+
190
+ split('counts a reasoning + narration + answer split once', async () => {
191
+ // The three-way case: content alongside a tool call is interstitial narration, so this
192
+ // response produces reasoning + narration + the tool-call message. Worst case for the
193
+ // old behaviour — cache volume counted three times.
194
+ const first = priced({
195
+ content: 'let me look that up',
196
+ reasoning: 'thinking…',
197
+ cost: 0.02,
198
+ toolCalls: [{ id: 'w1', name: 'work', args: {} }],
199
+ });
200
+ const driver = makeDriver(
201
+ agent({
202
+ name: 'a',
203
+ toolDefinitions: [def('work')],
204
+ toolHandlers: { work: async () => 'worked' },
205
+ }),
206
+ scriptedProvider([first, priced({ cost: 0.03 })]),
207
+ );
208
+ await driver.sendMessage('go');
209
+
210
+ const total = sumUsage(driver.getHistory());
211
+ assert.is(total.costUsd, 0.05, 'two calls, each counted once');
212
+ assert.is(total.cacheReadTokens, 1800, '900 per call — not 2700 with the narration copies');
213
+ assert.is(total.cacheWriteTokens, 100);
214
+ assert.is(total.uncachedInputTokens, 100);
215
+ });
216
+
217
+ split.run();
218
+
219
+ // ---------------------------------------------------------------------------
220
+ // Per-turn model attribution across a tier switch.
221
+ //
222
+ // The shape this exists for is an agent whose `provider` selector varies by state —
223
+ // e.g. the showcase trade-operations agent: `({state}) => isPlanning(state) ? High : Low`,
224
+ // planning on sonnet and executing on haiku. Each call has to name the model that ran
225
+ // it, on the turn itself: joining a turn to the message after it works for a normal
226
+ // step but not for a call that produced no message, and `provider.selected` fires only
227
+ // when the slot CHANGES, so the nearest event can be many turns back.
228
+ //
229
+ // The ordering trap: the snapshot is recorded BEFORE the provider is resolved for that
230
+ // call, so stamping `lastResolvedModel` at creation time yields the previous call's
231
+ // model — wrong on exactly the turn the tier changes.
232
+ // ---------------------------------------------------------------------------
233
+
234
+ const tiers = createLogicSuite('ChatDriver per-turn model attribution');
235
+
236
+ /** A tiered registry: named slots, each reporting its own model via `getStatus`. */
237
+ const tieredRegistry = (
238
+ slots: Record<string, AIProvider>,
239
+ defaultName: string,
240
+ ): AIProviderRegistry =>
241
+ ({
242
+ get: (name: string) => slots[name],
243
+ default: () => slots[defaultName],
244
+ defaultName: () => defaultName,
245
+ names: () => Object.keys(slots),
246
+ getStatus: async () => null,
247
+ listStatuses: async () => [],
248
+ }) as AIProviderRegistry;
249
+
250
+ const tierProvider = (model: string, responses: ChatMessage[]): AIProvider => {
251
+ const queue = [...responses];
252
+ return {
253
+ getStatus: async () => ({ model, provider: 'anthropic' }) as never,
254
+ chat: async () => queue.shift() ?? { role: 'assistant', content: 'done' },
255
+ };
256
+ };
257
+
258
+ tiers('attributes each step to the tier that ran it when the agent switches mid-flow', async () => {
259
+ // Responses deliberately carry NO `model` of their own — the common case for a
260
+ // transport that leaves attribution to the driver, and the one where a stale
261
+ // `lastResolved*` read would go unnoticed.
262
+ const slots = {
263
+ high: tierProvider('claude-sonnet-4-6', [
264
+ priced({
265
+ content: '',
266
+ cost: 0.05,
267
+ toolCalls: [{ id: 'p1', name: 'finish_planning', args: {} }],
268
+ }),
269
+ ]),
270
+ low: tierProvider('claude-haiku-4-5-20251001', [priced({ content: 'Booked.', cost: 0.001 })]),
271
+ };
272
+ // The "state" the selector reads, advanced by the tool — as a flow agent's machine does.
273
+ let planning = true;
274
+ const driver = new ChatDriver(tieredRegistry(slots, 'low'), {
275
+ maxToolIterations: 20,
276
+ maxFoldOperations: 5,
277
+ sessionKey: 'tiers',
278
+ });
279
+ driver.applyAgent(
280
+ agent({
281
+ name: 'Trade Operations',
282
+ provider: () => (planning ? 'high' : 'low'),
283
+ toolDefinitions: [def('finish_planning')],
284
+ toolHandlers: {
285
+ finish_planning: async () => {
286
+ planning = false;
287
+ return 'planned';
288
+ },
289
+ },
290
+ }),
291
+ );
292
+ await driver.sendMessage('book me a trade');
293
+
294
+ const snapshots = driver.getTurnSnapshots();
295
+ assert.equal(
296
+ snapshots.map((s) => [s.providerName, s.model, s.usage?.costUsd]),
297
+ [
298
+ ['high', 'claude-sonnet-4-6', 0.05],
299
+ ['low', 'claude-haiku-4-5-20251001', 0.001],
300
+ ],
301
+ 'the planning call is attributed to the high tier and the execution call to the low one',
302
+ );
303
+ assert.equal(
304
+ snapshots.map((s) => s.provider),
305
+ ['anthropic', 'anthropic'],
306
+ 'the vendor behind each slot is recorded too',
307
+ );
308
+
309
+ // A turn and the message it produced must never disagree about which model ran.
310
+ const assistantModels = driver
311
+ .getHistory()
312
+ .filter((m) => m.role === 'assistant' && m.cost != null)
313
+ .map((m) => m.model);
314
+ assert.equal(
315
+ assistantModels,
316
+ snapshots.map((s) => s.model),
317
+ );
318
+ });
319
+
320
+ tiers('records the SERVING model when a fallback answers on another model', async () => {
321
+ // A server-side fallback chain answers on a different model than the one requested.
322
+ // The transport stamps the real one; the turn must follow it rather than relabel the
323
+ // call as the tier's configured model — that would misattribute the spend.
324
+ const slots = {
325
+ high: tierProvider('claude-fable-5', [
326
+ priced({ content: 'answered by the fallback', model: 'claude-opus-4-8' }),
327
+ ]),
328
+ };
329
+ const driver = new ChatDriver(tieredRegistry(slots, 'high'), {
330
+ maxToolIterations: 20,
331
+ maxFoldOperations: 5,
332
+ sessionKey: 'fallback',
333
+ });
334
+ driver.applyAgent(agent({ name: 'a', provider: 'high' }));
335
+ await driver.sendMessage('go');
336
+
337
+ const [turn] = driver.getTurnSnapshots();
338
+ assert.is(turn.model, 'claude-opus-4-8', 'the model that answered, not the one asked for');
339
+ assert.is(turn.providerName, 'high', 'the slot asked for is still recorded');
340
+ });
341
+
342
+ tiers('names the model and vendor on the provider.selected event, not just the slot', async () => {
343
+ clearMetaEventRegistry();
344
+ const slots = {
345
+ high: tierProvider('claude-sonnet-4-6', [priced({ content: 'hi' })]),
346
+ };
347
+ const driver = new ChatDriver(tieredRegistry(slots, 'high'), {
348
+ maxToolIterations: 20,
349
+ maxFoldOperations: 5,
350
+ sessionKey: 'selected',
351
+ });
352
+ driver.applyAgent(agent({ name: 'a', provider: 'high' }));
353
+ await driver.sendMessage('go');
354
+
355
+ const selected = getMetaEvents('selected').find((e) => e.type === 'provider.selected');
356
+ assert.ok(selected, 'a provider.selected event is recorded');
357
+ assert.is(selected!.detail?.provider, 'high', 'the registry slot');
358
+ assert.is(selected!.detail?.model, 'claude-sonnet-4-6', 'and the model behind it');
359
+ assert.is(selected!.detail?.vendor, 'anthropic');
360
+ });
361
+
362
+ tiers.run();
package/src/main/main.ts CHANGED
@@ -88,7 +88,7 @@ import {
88
88
  deleteDriver,
89
89
  } from '../state/driver-registry';
90
90
  import { buildTimelineEntries } from '../state/persistence/build-timeline-entries';
91
- import { assembleDebugLog } from '../state/persistence/diagnostics';
91
+ import { assembleDebugLog, withFreshMetaSnapshot } from '../state/persistence/diagnostics';
92
92
  import type { DebugLog, DiagnosticEntry } from '../state/persistence/diagnostics';
93
93
  import {
94
94
  deleteDiagnosticsCursorsFor,
@@ -4044,7 +4044,7 @@ export class FoundationAiAssistant extends GenesisElement {
4044
4044
  );
4045
4045
  }
4046
4046
 
4047
- /** The live current-page debug log (`{ readme, timeline, meta }`). @public */
4047
+ /** The live current-page debug log (`{ readme, sessionUsage, timeline, meta }`). @public */
4048
4048
  getDebugLog(): DebugLog {
4049
4049
  return assembleDebugLog(this.buildDiagnosticEntries(), DEBUG_LOG_README);
4050
4050
  }
@@ -4058,6 +4058,39 @@ export class FoundationAiAssistant extends GenesisElement {
4058
4058
  * been appended to the persisted stream.
4059
4059
  */
4060
4060
  private buildDiagnosticEntries(): DiagnosticEntry[] {
4061
+ const stateKey = this.getStateKey();
4062
+
4063
+ // The message/turn/event timeline entries — built by the shared, pure `buildTimelineEntries`
4064
+ // (the same helper a headless consumer uses to harvest its own log), from the driver's pull
4065
+ // surfaces. Prefer the driver's raw history (carries sub-agent traces) over the redux projection.
4066
+ const timelineEntries = buildTimelineEntries({
4067
+ turnSnapshots: this.driver?.getTurnSnapshots?.() ?? [],
4068
+ messages: this.driver?.getRawHistory?.() ?? this.messages,
4069
+ metaEvents: stateKey ? getMetaEvents(stateKey) : [],
4070
+ });
4071
+
4072
+ // Fold in any external diagnostics harvested from an out-of-band driver (e.g. a server-side
4073
+ // ChatDriver whose collated log an interaction widget returned on its result). They ride the
4074
+ // same download + persisted-diagnostics path; `assembleDebugLog` sorts the whole timeline by
4075
+ // timestamp so they interleave chronologically. (GENC-1461 unified diagnostics.)
4076
+ return [
4077
+ ...timelineEntries,
4078
+ ...(this.driver?.getExternalDiagnostics?.() ?? []),
4079
+ this.buildMetaSnapshot(),
4080
+ ] as DiagnosticEntry[];
4081
+ }
4082
+
4083
+ /**
4084
+ * The single `meta-snapshot` entry for right now — the export-time `meta` block
4085
+ * (agent summary, active prompt/state, context + cost) plus the `dedupSignature`
4086
+ * the forward-capture delta keys on.
4087
+ *
4088
+ * Separate from `buildDiagnosticEntries` because the download path needs a
4089
+ * FRESH one on its own: the persisted stream only re-appends this block when the
4090
+ * near-static config changes, so the newest stored snapshot's volatile half — the
4091
+ * `context` figures especially — is typically frozen at the session's first flush.
4092
+ */
4093
+ private buildMetaSnapshot(): DiagnosticEntry {
4061
4094
  const timestamp = new Date().toISOString().replace(/:/g, '-');
4062
4095
  // Snapshot the live active agent from the DRIVER — the instance whose
4063
4096
  // `onActivate` ran, so its `getDebugSnapshot` closure holds state.
@@ -4074,16 +4107,6 @@ export class FoundationAiAssistant extends GenesisElement {
4074
4107
  this.contextTokens != null && this.contextLimit != null && this.contextLimit > 0
4075
4108
  ? Math.round((this.contextTokens / this.contextLimit) * 100)
4076
4109
  : undefined;
4077
- const stateKey = this.getStateKey();
4078
-
4079
- // The message/turn/event timeline entries — built by the shared, pure `buildTimelineEntries`
4080
- // (the same helper a headless consumer uses to harvest its own log), from the driver's pull
4081
- // surfaces. Prefer the driver's raw history (carries sub-agent traces) over the redux projection.
4082
- const timelineEntries = buildTimelineEntries({
4083
- turnSnapshots: this.driver?.getTurnSnapshots?.() ?? [],
4084
- messages: this.driver?.getRawHistory?.() ?? this.messages,
4085
- metaEvents: stateKey ? getMetaEvents(stateKey) : [],
4086
- });
4087
4110
 
4088
4111
  // The export-time `meta` block, carried on a `meta-snapshot` entry so it lives
4089
4112
  // in the same forward stream (the latest one wins on reassembly, and the
@@ -4149,6 +4172,8 @@ export class FoundationAiAssistant extends GenesisElement {
4149
4172
  // instead of "only when the block actually changed". The volatile evolution is
4150
4173
  // already in the timeline (turn snapshots + `context.updated` events), so the
4151
4174
  // persister only needs a fresh meta-snapshot when the config/prompt changes.
4175
+ // Consequence for the download path: the newest STORED snapshot's context/cost
4176
+ // figures are stale, which is why `buildDownloadLog` appends a fresh one.
4152
4177
  const m = metaSnapshot.meta as Record<string, unknown>;
4153
4178
  metaSnapshot.dedupSignature = JSON.stringify({
4154
4179
  host: m.host,
@@ -4157,16 +4182,7 @@ export class FoundationAiAssistant extends GenesisElement {
4157
4182
  activePrimerHistory: m.activePrimerHistory,
4158
4183
  activeFoldStack: m.activeFoldStack,
4159
4184
  });
4160
-
4161
- // Fold in any external diagnostics harvested from an out-of-band driver (e.g. a server-side
4162
- // ChatDriver whose collated log an interaction widget returned on its result). They ride the
4163
- // same download + persisted-diagnostics path; `assembleDebugLog` sorts the whole timeline by
4164
- // timestamp so they interleave chronologically. (GENC-1461 unified diagnostics.)
4165
- return [
4166
- ...timelineEntries,
4167
- ...(this.driver?.getExternalDiagnostics?.() ?? []),
4168
- metaSnapshot,
4169
- ] as DiagnosticEntry[];
4185
+ return metaSnapshot;
4170
4186
  }
4171
4187
 
4172
4188
  async downloadDebugLog(): Promise<void> {
@@ -4200,6 +4216,14 @@ export class FoundationAiAssistant extends GenesisElement {
4200
4216
  * chat" — diagnostics are a forensic stream keyed on provider capability, not on
4201
4217
  * whether the *chat* is remembered (like preferences). Falls back to the live
4202
4218
  * current-page log when diagnostics isn't available or the fetch fails.
4219
+ *
4220
+ * The stored `meta-snapshot`s are replaced by a fresh one (`withFreshMetaSnapshot`)
4221
+ * before reassembly. The persisted stream only re-appends that block when the
4222
+ * near-static config signature changes (see `collectDiagnosticsDelta`), so on a session
4223
+ * whose config never changes the newest STORED snapshot is the first one — its
4224
+ * `context` half (session cost/usage, context tokens, live agent state) frozen seconds
4225
+ * into the session, which is how a lifetime log came out reporting near-zero spend
4226
+ * against a transcript full of priced messages.
4203
4227
  */
4204
4228
  private async buildDownloadLog(): Promise<DebugLog> {
4205
4229
  const provider = this.persistence.provider;
@@ -4209,7 +4233,12 @@ export class FoundationAiAssistant extends GenesisElement {
4209
4233
  // Land the current page's unflushed delta first so the download includes it.
4210
4234
  await this.persister()?.flushDiagnostics();
4211
4235
  const stored = await provider.loadDiagnostics(key);
4212
- if (stored.length) return assembleDebugLog(stored, DEBUG_LOG_README);
4236
+ if (stored.length) {
4237
+ return assembleDebugLog(
4238
+ withFreshMetaSnapshot(stored, this.buildMetaSnapshot()),
4239
+ DEBUG_LOG_README,
4240
+ );
4241
+ }
4213
4242
  } catch (e) {
4214
4243
  logger.error('Diagnostics load failed — using current-page log:', e);
4215
4244
  }
@@ -316,17 +316,22 @@ export function clearSession(key: string): void {
316
316
  */
317
317
  export const DEBUG_LOG_README: readonly string[] = [
318
318
  'This is an exported debug log for the Genesis AI assistant. Read it top-to-bottom.',
319
+ "`sessionUsage` is what this session spent: `costUsd` (USD, provider-reported per request and summed, cache discounts already applied) plus the four TOKEN BUCKETS — `uncachedInputTokens`, `cacheReadTokens`, `cacheWriteTokens`, `outputTokens`. Those four are disjoint and safe to add up for a total-tokens figure; the per-message fields further down are NOT (see kind:'message'). Each bucket bills at a different rate — cache reads a fraction of uncached input, cache writes a premium, output highest — so a big token count against a small cost means the prompt was mostly cache hits, not a missing charge. It covers sub-agent turns and spend a compaction banked, so for the CONVERSATION it is the authoritative total: prefer it over re-summing the timeline, which can be short by whatever the ring buffers evicted. The same figures appear under `meta.context`. It is as current as the snapshot it came from: the assistant's own download stamps a fresh one, but a log stitched by another tool can carry an older one — compare `meta.timestamp` with the last timeline entry, and if it is well behind, read these totals as historical and fall back to summing the timeline. One scope caveat: it is derived from the transcript, so it excludes billed calls that produced no message (a blank or refused attempt that was retried — see kind:'turn'.`usage`). Summing the turns can therefore come out HIGHER than this, and the difference is exactly that discarded spend, not a double-count.",
320
+ "What 'session' means for those figures: the CURRENT conversation under this session key (a host-supplied per-project id, or the element id + header title when the host supplies none) — which is not necessarily everything in `timeline`. It is re-derived from the live transcript, so it spans page loads only when the chat is being remembered, and a Clear resets it to zero. `timeline` is append-only regardless: it keeps every page load and the pre-Clear conversation, with the session.cleared event as the boundary. So on a long-lived log, read these totals as the latest conversation's and the earlier sections as history — not as a total of the whole file.",
319
321
  '`timeline` is the entire session as one array, already sorted chronologically by `timestamp` (ISO 8601). Every entry has a `kind`.',
320
322
  'Timestamps are millisecond-resolution; entries that share the same millisecond are ordered by a fixed kind rank (event, then turn, then message), which is a heuristic and may not reflect exact causal order within that millisecond — e.g. a user message and the turn it triggered, or a final assistant message and its turn.end event, can appear in either order depending on whether they landed in the same millisecond. Read the logical structure of a turn rather than over-interpreting the micro-ordering of co-timestamped entries of different kinds.',
321
323
  "kind:'message' — the conversation. `role` is user/assistant/tool/system-event/synthetic-user; `agentName` says which agent produced it; `toolCalls`/`toolResult`/`interaction` carry tool and widget activity; `inputTokens`/`outputTokens`/`cost` are per-message LLM usage, where `inputTokens` is the WHOLE prompt for that request and `cacheReadTokens`/`cacheWriteTokens` BREAK IT DOWN rather than add to it — uncached input is `inputTokens` minus those two, and adding the cache fields to `inputTokens` counts the prompt twice. The cache fields are absent on providers that report no cache split (Gemini reports reads only, since implicit caching bills no write) and on messages persisted before they existed, so read them as 0 when missing. Each bucket bills at a different rate — cache reads a fraction of uncached input, cache writes a premium, output highest — so a large token count at a small cost means the prompt was mostly cache hits. `externalCostUsd` is any non-LLM cost a widget reported for its own external service calls (folded into the session cost total alongside `cost`). On model-produced assistant messages, `model` is the concrete model id that generated it (e.g. 'gemini-2.5-flash-lite') and `providerName` is the registry slot it resolved under (e.g. a tier name like 'high'/'low', or the default); together they attribute the message — and any tool calls it carries — to an exact model even across a mid-session vendor/tier switch, where one slot name can map to different models before and after the switch. Both are undefined on any entry that is NOT an LLM response: non-assistant roles (user/tool/system-event) and 'synthetic-user' echoes; assistant interaction/widget entries (empty content carrying an `interaction` — a rendered widget, not a model turn); driver-authored assistant fallbacks (the timeout, repeated-malformed-call, and empty-response apology messages); and messages restored from a session persisted before these fields existed. One partial case: on a genuine model turn whose provider exposes no `getStatus` (or reports no model), `providerName` is still set but `model` alone is undefined. A 'synthetic-user' message is a display-only echo of an interaction outcome (e.g. the answer a widget reported): it renders on the user's side of the chat and `agentName` is the agent that created it, but it is never sent to the LLM — so it has no matching 'turn' and the model learns the outcome only from the corresponding tool result.",
322
324
  "Sub-agent messages appear inline. When a tool delegates to a sub-agent (via `requestSubAgent`), the sub-agent's whole conversation — its own assistant/tool messages, each with their own `content`/`thinking`/`toolCalls`/`toolResult` and per-message `model`/`providerName`/`inputTokens`/`outputTokens`/`cacheReadTokens`/`cacheWriteTokens`/`cost` — is hoisted into the timeline as ordinary kind:'message' entries, interleaved by timestamp right after the tool call that spawned them (so you read the delegation top-to-bottom). A hoisted entry is marked: `subAgentDepth` is its delegation depth (1 for a sub-agent, 2 for a sub-agent's sub-agent, …), `subAgentOf` is the id of the parent tool call that spawned it (correlates it back even when two sub-agents run in one parent turn), `subAgentName` is the sub-agent's own name, and `agentName` is rewritten to a `\"<parent> › <sub-agent>\"` breadcrumb (composing when nested, e.g. `\"UI Builder › Planner › Grounding\"`). The sub-agent's per-LLM-call snapshots also surface as kind:'turn' entries with an N-M `turnIndex`, and subagent.started/completed (or subagent.failed) events bracket the run. Per-message `cost` on hoisted entries is already part of the session total (it is summed from the un-flattened history), so summing the top-level timeline does NOT double-count.",
323
325
  "kind:'turn' — one LLM call. `turnIndex` is a string: a top-level turn is the bare counter ('0', '1', …); a sub-agent's turns are numbered under the parent turn that activated them ('3-1', '3-2', …, and a nested sub-agent contributes '3-2-1', …), and `agentName` names the agent that ran the turn. `systemPrompt` and `toolNames` are what the model saw. A systemPrompt of '<repeated — identical to turn N>' was byte-identical to turn N and de-duplicated; the full prompt is shown whenever it changes (often because a stateful agent advanced), so prompt evolution is visible.",
326
+ "kind:'turn'.`model`/`providerName`/`provider` — which model ran that call: the concrete model id, the registry slot it resolved under (a tier name like 'high'/'low', or the default), and the vendor. Recorded per CALL, so an agent whose `provider` selector varies by state — a flow that plans on a high tier and executes on a low one — has every step attributed, including calls that produced no message. `model` is the SERVING model where the provider reports one, so a turn answered by a server-side fallback names the model that answered rather than the one requested; it always matches the `model` on the message that call produced. Absent when the provider exposes no `getStatus` and the transport stamped nothing.",
327
+ "kind:'turn'.`usage` — what that ONE call cost, in the same four-bucket + `costUsd` shape as `sessionUsage`. Absent while a call is in flight and on providers that report no usage. This is the SAME money as the message the call produced, not extra money: never add turn usage to message usage, and read a turn plus its message as one charge. Its distinct value is the calls that produced NO message — a blank or refused response is billed and then discarded before the retry, so the turn entry is the only record of that spend, and a turn with `usage` but no message after it is exactly that. `costUsd: 0` alongside a nonzero token count means the provider reported no price, not that the call was free.",
324
328
  "kind:'turn'.`agentSnapshot` — the active agent's own view of its internal state, captured at that turn. An agent opts into this by exposing a `getDebugSnapshot()` that returns JSON-serializable per-state info; stateful/flow agents wire it automatically, so you can watch a flow advance turn-by-turn (e.g. current step, cursor, collected fields, pending changes). Absent for agents that don't expose one.",
325
329
  "kind:'event' — a meta/lifecycle event. `type` names it (see below); `detail` carries structured data. `detail.placement` is the emitting UI instance: 'bubble' (collapsed), 'panel' (popped-out), or 'standalone'.",
326
330
  "Each 'event' also has an `importance`: 'high' (failures/limits — turn.error, tool.failed, subagent.failed, file.read-failed, suggestions.failed, context.threshold-crossed), 'normal' (session flow — connects, turns, retries, handoffs, agent/provider changes, interactions, sub-agent start/complete), or 'low' (skippable UI/bookkeeping noise — panel.toggled, attachment.added, driver.wired/unwired, context.updated, context.condensed). To skim, ignore importance:'low'; to triage a failure, filter to importance:'high' then read the nearby messages and turns. A 'high' turn.error is often preceded by one or more 'normal' turn.retry events for the same reason — read them together to see how many attempts were made before bailing. 'message' and 'turn' entries carry no importance — they are the substance, always read them.",
327
- 'Event types: assistant.connected/disconnected (mount + placement + whether the session was created or restored), assistant.popout/popin (window placement), driver.created/wired/unwired (which driver is live and why it stops/starts responding across a popout), state.changed (idle↔loading), turn.start/turn.end (turn boundary; turn.end carries durationMs), turn.retry (a recoverable in-turn retry — detail.reason plus attempt/maxAttempts; for malformed calls also finishMessage; for empty responses also the provider finishReason + thoughtsTokens + parts breakdown), turn.error (a turn failed or hit a guardrail — detail.reason is one of exception/malformed-function-call/empty-response/unknown-tool-limit/max-iterations/response-truncated/refusal/budget-exhausted, plus reason-specific diagnostics: attempts (for empty-response also finishReason + thoughtsTokens + a parts breakdown, distinguishing a thinking-only STOP from a truly empty turn), finishMessage, for response-truncated the model + maxTokens + outputTokens + tools, unknownTools (split into staleTools — real earlier this activation but retired by the current state or hidden behind an open exclusive fold — and hallucinatedTools — never advertised) + availableTools, iterations + limit, for budget-exhausted the budgetUsd + spentUsd figures reported by the proxy plus the resolved vendor, or name + message for exceptions), tool.failed (a tool threw), tool.unresolved (the model called a tool that could not be dispatched — detail.kind is folded/fold-hidden/stale/unknown, plus tool + agent and, for the counted kinds, the consecutive streak; the recurring lead-up to an unknown-tool-limit turn.error), subagent.started/completed/failed (the lifecycle of a `requestSubAgent` delegation — detail.agent names the sub-agent; these bracket the sub-agent turns that appear as kind:turn entries with an N-M `turnIndex`; subagent.failed also carries detail.reason, one of max_iterations/malformed_tool_call/empty_response/unknown_tool_limit/timeout/response_truncated/refusal/budget_exhausted; budget_exhausted is terminal for the PARENT turn too — the parent stops rather than calling the model again into the same wall), agent.handoff (routing; from=null is the initial activation), agent.pinned/unpinned (forced routing), provider.selected (model/provider for the upcoming turns), interaction.requested/resolved (blocking user widgets — explain quiet gaps; note that when a sub-agent opens a widget, detail.agent — and the agentName on the interaction message — is the HOST agent that owns the widget, NOT the sub-agent that asked, because widgets render and resolve on the host driver), context.updated/threshold-crossed (token + cost), context.condensed (a stale tool payload was collapsed out of the model-bound history by a `condenseWhen` declaration on the tool — detail.tool + toolCallId, target args|response, trigger (superseded:<key> or age:<n>), stubLen, and an estimated tokensSaved; stored history and this log keep the FULL payload, so the model-visible slice at any point is the full history minus the condensations recorded up to then), panel.toggled, attachment.added, file.read-failed, suggestions.failed.',
331
+ 'Event types: assistant.connected/disconnected (mount + placement + whether the session was created or restored), assistant.popout/popin (window placement), driver.created/wired/unwired (which driver is live and why it stops/starts responding across a popout), state.changed (idle↔loading), turn.start/turn.end (turn boundary; turn.end carries durationMs), turn.retry (a recoverable in-turn retry — detail.reason plus attempt/maxAttempts; for malformed calls also finishMessage; for empty responses also the provider finishReason + thoughtsTokens + parts breakdown), turn.error (a turn failed or hit a guardrail — detail.reason is one of exception/malformed-function-call/empty-response/unknown-tool-limit/max-iterations/response-truncated/refusal/budget-exhausted, plus reason-specific diagnostics: attempts (for empty-response also finishReason + thoughtsTokens + a parts breakdown, distinguishing a thinking-only STOP from a truly empty turn), finishMessage, for response-truncated the model + maxTokens + outputTokens + tools, unknownTools (split into staleTools — real earlier this activation but retired by the current state or hidden behind an open exclusive fold — and hallucinatedTools — never advertised) + availableTools, iterations + limit, for budget-exhausted the budgetUsd + spentUsd figures reported by the proxy plus the resolved vendor, or name + message for exceptions), tool.failed (a tool threw), tool.unresolved (the model called a tool that could not be dispatched — detail.kind is folded/fold-hidden/stale/unknown, plus tool + agent and, for the counted kinds, the consecutive streak; the recurring lead-up to an unknown-tool-limit turn.error), subagent.started/completed/failed (the lifecycle of a `requestSubAgent` delegation — detail.agent names the sub-agent; these bracket the sub-agent turns that appear as kind:turn entries with an N-M `turnIndex`; subagent.failed also carries detail.reason, one of max_iterations/malformed_tool_call/empty_response/unknown_tool_limit/timeout/response_truncated/refusal/budget_exhausted; budget_exhausted is terminal for the PARENT turn too — the parent stops rather than calling the model again into the same wall), agent.handoff (routing; from=null is the initial activation), agent.pinned/unpinned (forced routing), provider.selected (the resolved provider for the upcoming turns — detail.provider is the registry SLOT/tier name, detail.model the concrete model behind it and detail.vendor its vendor; emitted only when the slot CHANGES, so read the per-turn `model` for the model of any given call rather than assuming the nearest event still applies), interaction.requested/resolved (blocking user widgets — explain quiet gaps; note that when a sub-agent opens a widget, detail.agent — and the agentName on the interaction message — is the HOST agent that owns the widget, NOT the sub-agent that asked, because widgets render and resolve on the host driver), context.updated/threshold-crossed (token + cost), context.condensed (a stale tool payload was collapsed out of the model-bound history by a `condenseWhen` declaration on the tool — detail.tool + toolCallId, target args|response, trigger (superseded:<key> or age:<n>), stubLen, and an estimated tokensSaved; stored history and this log keep the FULL payload, so the model-visible slice at any point is the full history minus the condensations recorded up to then), panel.toggled, attachment.added, file.read-failed, suggestions.failed.',
328
332
  'Sub-agent meta events: a sub-agent\'s own turn.retry/turn.error/tool.failed/tool.unresolved events are merged into this same timeline, tagged with `detail.subAgent` — a `"<parent> › <sub-agent>"` breadcrumb that composes when nested (e.g. `"UI Builder › Planner › Grounding"`) — and interleaved by their original timestamps within the subagent.started→completed/failed bracket. These are the per-attempt/per-failure signals that do NOT appear among the sub-agent\'s (hoisted) messages: a malformed/empty attempt that gets retried produces no message, and the stale-vs-hallucinated split and streak counts live only on the event. A sub-agent\'s high-volume, message-derivable events (turn.start/turn.end, provider.selected, context.updated) are intentionally NOT merged — read its hoisted messages for model/tokens/cost and turn-by-turn activity, and the bracketing subagent.* events for the run\'s span.',
329
- "`meta` holds context captured at export time: agentSummary (full agent configs), context (active model, token usage, session cost), activeDebugSnapshot (the active agent's `getDebugSnapshot()` taken fresh at export — reflects state NOW, which may have advanced beyond the last turn's agentSnapshot), debug (optional host-supplied debug state), host, and the export timestamp.",
333
+ "`meta` holds context captured at export time: agentSummary (full agent configs), context (active model, contextTokens/contextLimit/contextUsagePercent for the last call, and the session totals — sessionCostUsd, sessionTokensConsumed, and the four-bucket sessionUsage lifted to the top of this log), activeDebugSnapshot (the active agent's `getDebugSnapshot()` taken fresh at export — reflects state NOW, which may have advanced beyond the last turn's agentSnapshot), debug (optional host-supplied debug state), host, and the export timestamp.",
334
+ 'Note the two different scopes in `meta.context`: `contextTokens` is the prompt size of the LAST call (against `contextLimit`, the model context window), while `sessionUsage`/`sessionTokensConsumed` are cumulative BILLED throughput. Every turn resends the conversation, so the cumulative figure counts each turn’s prompt again in the next turn’s and is expected to dwarf the context size — that is not double-counting.',
330
335
  'To debug a failure: find the last turn.error or tool.failed, then read upward for the user message, the turn(s), and the agent/provider/state events that led into it.',
331
336
  ];
332
337