gipity 1.1.12 → 1.1.13

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.
package/dist/adopt-cwd.js CHANGED
@@ -17,7 +17,6 @@ import { get, post, ApiError } from './api.js';
17
17
  import { isSyncIgnored } from './setup.js';
18
18
  import { finalizeLocalProject } from './project-setup.js';
19
19
  import { getProjectsRoot } from './relay/paths.js';
20
- import * as relayState from './relay/state.js';
21
20
  const EASY_FILES = 200;
22
21
  const EASY_BYTES = 100 * 1024 * 1024; // 100 MB
23
22
  const REFUSE_FILES = 2000;
@@ -210,15 +209,15 @@ export async function adoptCurrentDir(opts) {
210
209
  }
211
210
  let isNew = false;
212
211
  if (!project) {
213
- const device = relayState.getDevice();
212
+ // No autoChat: which agent will run here isn't known (init wires them
213
+ // all up; build's picker runs later), so a pre-created chat would guess
214
+ // `claude_code` and sit empty forever when another agent is used. The
215
+ // first captured session creates/reuses a conversation with the right
216
+ // source via the launch path or the capture runner's self-arm resolve.
214
217
  const body = {
215
218
  name: opts.projectName,
216
219
  slug: opts.projectSlug,
217
220
  };
218
- if (device) {
219
- body.autoChat = 'claude_code';
220
- body.deviceGuid = device.guid;
221
- }
222
221
  try {
223
222
  const res = await post('/projects', body);
224
223
  project = res.data;
package/dist/api.js CHANGED
@@ -25,6 +25,15 @@ export class ApiError extends Error {
25
25
  // the time-to-first-byte here and an idle watchdog in sync.ts guards the body.
26
26
  const REQUEST_TIMEOUT_MS = 60_000;
27
27
  const DOWNLOAD_HEADER_TIMEOUT_MS = 30_000;
28
+ // Media generation (music/video/image/speech, `service call` POSTs) can
29
+ // legitimately run minutes in ONE request/response: a GPU-backed music model
30
+ // waking from scale-to-zero spends 1-3 min booting + loading weights before it
31
+ // renders a note, and Veo video runs 30-120s. The server bounds all of these at
32
+ // 300s (its provider fetch budget and the ALB idle timeout are both 300s), so
33
+ // the long-call cap sits just above that ceiling - the server's clearer error
34
+ // always arrives before the CLI gives up. Callers opt in per request; plain
35
+ // JSON calls keep the tight default so a wedged connection still fails fast.
36
+ export const LONG_REQUEST_TIMEOUT_MS = 310_000;
28
37
  /** fetch() that rejects with a clean 408 ApiError if the whole exchange (headers
29
38
  * + body) doesn't complete within timeoutMs, instead of hanging forever. For
30
39
  * request/response JSON calls only - never wrap a long streaming body in this. */
@@ -111,16 +120,16 @@ export async function getAuthHeader() {
111
120
  const headers = await getHeaders();
112
121
  return headers.Authorization;
113
122
  }
114
- async function request(method, path, body, retrying = false) {
123
+ async function request(method, path, body, retrying = false, timeoutMs = REQUEST_TIMEOUT_MS) {
115
124
  const headers = await getHeaders();
116
125
  const url = `${baseUrl()}${path}`;
117
126
  const res = await fetchWithTimeout(url, {
118
127
  method,
119
128
  headers,
120
129
  body: body ? JSON.stringify(body) : undefined,
121
- }, REQUEST_TIMEOUT_MS, `${method} ${path}`);
130
+ }, timeoutMs, `${method} ${path}`);
122
131
  if (await shouldRetryAfter401(res.status, retrying)) {
123
- return request(method, path, body, true);
132
+ return request(method, path, body, true, timeoutMs);
124
133
  }
125
134
  if (!res.ok) {
126
135
  const json = await res.json().catch(() => ({ error: { code: 'UNKNOWN', message: res.statusText } }));
@@ -132,8 +141,8 @@ async function request(method, path, body, retrying = false) {
132
141
  export function get(path) {
133
142
  return request('GET', path);
134
143
  }
135
- export function post(path, body) {
136
- return request('POST', path, body);
144
+ export function post(path, body, opts) {
145
+ return request('POST', path, body, false, opts?.timeoutMs);
137
146
  }
138
147
  /** POST JSON and consume the response as a tar stream, returning each entry as
139
148
  * { name, buffer } in arrival order. Non-2xx responses are parsed as JSON errors. */
@@ -176,6 +176,16 @@ export function parseTranscript(content, afterUuid) {
176
176
  // whole file (including pre-watermark lines) so a result whose tool_use
177
177
  // was forwarded in an earlier sweep still resolves its name.
178
178
  const toolNames = new Map();
179
+ // Token totals for the newly parsed range. Claude Code splits one API
180
+ // response across several transcript lines (one per content block), each
181
+ // repeating the same `message.usage`, so count each `message.id` once.
182
+ // tokens_in is the full input side (fresh + cache-read + cache-creation)
183
+ // to match the relay result footer's semantics; the per-entry input_tokens
184
+ // stays fresh-input-only, which is why summing entries can't replace this.
185
+ const countedUsage = new Set();
186
+ let tokensIn = 0;
187
+ let tokensOut = 0;
188
+ const num = (v) => (typeof v === 'number' && v > 0 ? v : 0);
179
189
  for (const raw of content.split('\n')) {
180
190
  const line = raw.trim();
181
191
  if (!line)
@@ -201,6 +211,18 @@ export function parseTranscript(content, afterUuid) {
201
211
  seenWatermark = true;
202
212
  continue;
203
213
  }
214
+ // Usage is counted independently of entry emission: an assistant line
215
+ // whose content produced no entries still carries real token usage.
216
+ if (parsed?.type === 'assistant' && parsed?.isSidechain !== true) {
217
+ const m = parsed.message;
218
+ const u = m?.usage;
219
+ const key = typeof m?.id === 'string' && m.id ? m.id : parsed.uuid;
220
+ if (u && typeof key === 'string' && !countedUsage.has(key)) {
221
+ countedUsage.add(key);
222
+ tokensIn += num(u.input_tokens) + num(u.cache_read_input_tokens) + num(u.cache_creation_input_tokens);
223
+ tokensOut += num(u.output_tokens);
224
+ }
225
+ }
204
226
  const entries = transcriptLineToEntries(parsed, toolNames);
205
227
  if (entries.length) {
206
228
  for (const e of entries)
@@ -218,6 +240,7 @@ export function parseTranscript(content, afterUuid) {
218
240
  entries: out,
219
241
  lastUuid,
220
242
  foundWatermark: seenWatermark,
243
+ usage: { tokensIn, tokensOut },
221
244
  };
222
245
  }
223
246
  //# sourceMappingURL=claude-code.js.map
@@ -149,6 +149,19 @@ function lineToEntries(parsed, sessionId, idx, toolNames) {
149
149
  * *after* the positional watermark `afterUuid`. Same contract as the
150
150
  * claude-code parser: returns entries in order, the new watermark, and
151
151
  * whether the old watermark was still valid for this file. */
152
+ /** Cumulative token totals from a `token_count` event_msg, or null. The
153
+ * rollout's `info.total_token_usage` is the session running total and its
154
+ * `input_tokens` already includes cached input (verified against real
155
+ * rollouts: total_tokens = input_tokens + output_tokens), so range usage is
156
+ * a plain delta of totals - never a sum over events, which would re-count
157
+ * the whole session on every event. */
158
+ function tokenTotals(payload) {
159
+ const t = payload?.info?.total_token_usage;
160
+ if (!t || typeof t !== 'object')
161
+ return null;
162
+ const num = (v) => (typeof v === 'number' && v > 0 ? v : 0);
163
+ return { input: num(t.input_tokens), output: num(t.output_tokens) };
164
+ }
152
165
  export function parseTranscript(content, afterUuid) {
153
166
  const lines = content.split('\n');
154
167
  // The session id lives in the first session_meta line. Without it we can
@@ -180,6 +193,10 @@ export function parseTranscript(content, afterUuid) {
180
193
  const out = [];
181
194
  const toolNames = new Map();
182
195
  let lastIdx = startAfter;
196
+ // Cumulative token totals at the watermark (baseline) and at the end of
197
+ // the parsed range - their delta is this range's usage.
198
+ let baseUsage = null;
199
+ let lastUsage = null;
183
200
  for (let i = 0; i < lines.length; i++) {
184
201
  const line = lines[i].trim();
185
202
  if (!line)
@@ -191,17 +208,23 @@ export function parseTranscript(content, afterUuid) {
191
208
  catch {
192
209
  continue;
193
210
  }
211
+ const isTokenCount = parsed?.type === 'event_msg' && parsed?.payload?.type === 'token_count';
194
212
  if (startAfter !== null && i <= startAfter) {
195
213
  // Pre-watermark: record tool names only, so a post-watermark
196
- // tool_result still resolves the name of an already-forwarded call.
214
+ // tool_result still resolves the name of an already-forwarded call
215
+ // (and the running token total, so the range delta starts right).
197
216
  const p = parsed?.payload;
198
217
  if (parsed?.type === 'response_item' && p &&
199
218
  (p.type === 'custom_tool_call' || p.type === 'function_call') &&
200
219
  typeof p.call_id === 'string') {
201
220
  toolNames.set(p.call_id, typeof p.name === 'string' && p.name ? p.name : 'tool');
202
221
  }
222
+ if (isTokenCount)
223
+ baseUsage = tokenTotals(parsed.payload) ?? baseUsage;
203
224
  continue;
204
225
  }
226
+ if (isTokenCount)
227
+ lastUsage = tokenTotals(parsed.payload) ?? lastUsage;
205
228
  const entries = lineToEntries(parsed, sessionId, i, toolNames);
206
229
  for (const e of entries)
207
230
  out.push(e);
@@ -211,6 +234,12 @@ export function parseTranscript(content, afterUuid) {
211
234
  entries: out,
212
235
  lastUuid: lastIdx === null ? afterUuid : positional(sessionId, lastIdx),
213
236
  foundWatermark,
237
+ usage: lastUsage === null
238
+ ? { tokensIn: 0, tokensOut: 0 }
239
+ : {
240
+ tokensIn: Math.max(0, lastUsage.input - (baseUsage?.input ?? 0)),
241
+ tokensOut: Math.max(0, lastUsage.output - (baseUsage?.output ?? 0)),
242
+ },
214
243
  };
215
244
  }
216
245
  //# sourceMappingURL=codex.js.map
@@ -116,6 +116,12 @@ export function parseTranscript(content, afterUuid) {
116
116
  const foundWatermark = afterUuid === null || watermarkLine !== -1;
117
117
  const out = [];
118
118
  let lastUuid = foundWatermark ? afterUuid : null;
119
+ // Token totals for the newly parsed range. tokens_in is the full input side
120
+ // (fresh input + cache read/write) to match the relay result footer's
121
+ // semantics; the per-entry input_tokens stays `tokens.input` only.
122
+ let tokensIn = 0;
123
+ let tokensOut = 0;
124
+ const num = (v) => (typeof v === 'number' && v > 0 ? v : 0);
119
125
  for (let i = 0; i < lines.length; i++) {
120
126
  if (watermarkLine !== -1 && i <= watermarkLine)
121
127
  continue;
@@ -129,14 +135,22 @@ export function parseTranscript(content, afterUuid) {
129
135
  catch {
130
136
  continue;
131
137
  }
132
- const entries = messageToEntries(parsed?.info, Array.isArray(parsed?.parts) ? parsed.parts : []);
138
+ const info = parsed?.info;
139
+ const entries = messageToEntries(info, Array.isArray(parsed?.parts) ? parsed.parts : []);
133
140
  if (!entries.length)
134
141
  continue;
142
+ // Count usage only for lines that emit entries: entry-less lines don't
143
+ // advance the watermark, so counting them would re-add their tokens on
144
+ // every subsequent flush.
145
+ if (info?.role === 'assistant' && info?.tokens) {
146
+ tokensIn += num(info.tokens.input) + num(info.tokens.cache?.read) + num(info.tokens.cache?.write);
147
+ tokensOut += num(info.tokens.output);
148
+ }
135
149
  for (const e of entries)
136
150
  out.push(e);
137
- if (typeof parsed?.info?.id === 'string')
138
- lastUuid = parsed.info.id;
151
+ if (typeof info?.id === 'string')
152
+ lastUuid = info.id;
139
153
  }
140
- return { entries: out, lastUuid, foundWatermark };
154
+ return { entries: out, lastUuid, foundWatermark, usage: { tokensIn, tokensOut } };
141
155
  }
142
156
  //# sourceMappingURL=opencode.js.map
@@ -1395,12 +1395,12 @@ async function createNewProject(existingSlugs, preset) {
1395
1395
  continue;
1396
1396
  }
1397
1397
  try {
1398
- const device = relayState.getDevice();
1398
+ // No autoChat here: the agent isn't chosen yet (the picker runs after
1399
+ // project setup), so a pre-created chat would guess `claude_code` and
1400
+ // sit empty forever whenever the user picks another agent. The launch
1401
+ // path creates (or the placeholder-reuse logic finds) the conversation
1402
+ // with the RIGHT source moments later.
1399
1403
  const body = { name: projectName, slug: projectSlug };
1400
- if (device) {
1401
- body.autoChat = 'claude_code';
1402
- body.deviceGuid = device.guid;
1403
- }
1404
1404
  const res = await post('/projects', body);
1405
1405
  // Single console.log keeps the line on the headless stderr stream -
1406
1406
  // process.stdout.write would leak the message onto the child's stdout.
@@ -1,5 +1,5 @@
1
1
  import { Command, Option } from 'commander';
2
- import { post } from '../api.js';
2
+ import { post, LONG_REQUEST_TIMEOUT_MS } from '../api.js';
3
3
  import { resolveProjectContext, getConfigPath } from '../config.js';
4
4
  import { pushFile } from '../sync.js';
5
5
  import { mkdirSync, writeFileSync, readFileSync } from 'fs';
@@ -254,7 +254,7 @@ Examples:
254
254
  image_size: opts.imageSize,
255
255
  seed: Number.isFinite(opts.seed) ? opts.seed : undefined,
256
256
  input_images: inputImages,
257
- });
257
+ }, { timeoutMs: LONG_REQUEST_TIMEOUT_MS });
258
258
  const verb = inputImages ? 'Editing image...' : 'Generating image...';
259
259
  const started = Date.now();
260
260
  const result = opts.json
@@ -323,7 +323,7 @@ Examples:
323
323
  aspect_ratio: opts.aspect,
324
324
  resolution: opts.resolution,
325
325
  duration_seconds: opts.duration ? Number(opts.duration) : undefined,
326
- });
326
+ }, { timeoutMs: LONG_REQUEST_TIMEOUT_MS });
327
327
  // Veo runs 30-120s; the bouncing bar + timer keeps the wait honest.
328
328
  const started = Date.now();
329
329
  const result = opts.json
@@ -396,7 +396,7 @@ Examples:
396
396
  voice: opts.voice,
397
397
  language: opts.language,
398
398
  speakers,
399
- });
399
+ }, { timeoutMs: LONG_REQUEST_TIMEOUT_MS });
400
400
  const started = Date.now();
401
401
  const result = opts.json
402
402
  ? await doGenerate()
@@ -467,7 +467,7 @@ Examples:
467
467
  // Lyrics imply vocals; otherwise instrumental unless --vocals.
468
468
  instrumental: lyrics ? false : !opts.vocals,
469
469
  lyrics: lyrics || undefined,
470
- });
470
+ }, { timeoutMs: LONG_REQUEST_TIMEOUT_MS });
471
471
  const started = Date.now();
472
472
  const result = opts.json
473
473
  ? await doGenerate()
@@ -521,7 +521,7 @@ Examples:
521
521
  text,
522
522
  duration_seconds: opts.duration,
523
523
  prompt_influence: opts.influence,
524
- });
524
+ }, { timeoutMs: LONG_REQUEST_TIMEOUT_MS });
525
525
  const started = Date.now();
526
526
  const result = opts.json
527
527
  ? await doGenerate()
@@ -1,5 +1,5 @@
1
1
  import { Command } from 'commander';
2
- import { get, post, publicRequest, mintAppToken, ApiError } from '../api.js';
2
+ import { get, post, publicRequest, mintAppToken, ApiError, LONG_REQUEST_TIMEOUT_MS } from '../api.js';
3
3
  import { getAuth } from '../auth.js';
4
4
  import { requireConfig } from '../config.js';
5
5
  import { bold, muted } from '../colors.js';
@@ -110,7 +110,12 @@ serviceCommand
110
110
  res = await publicRequest(opts.get ? 'GET' : 'POST', url, opts.get ? undefined : body, headers);
111
111
  }
112
112
  else {
113
- res = opts.get ? await get(url) : await post(url, body);
113
+ // Generation services (music, video, image, ...) can hold the request
114
+ // open for minutes - e.g. a GPU music model cold-starting from
115
+ // scale-to-zero renders nothing for 1-3 min first. The server bounds
116
+ // the work at 300s, so give POSTs that full budget instead of the 60s
117
+ // default (which killed every cold-start render at exactly 60s).
118
+ res = opts.get ? await get(url) : await post(url, body, { timeoutMs: LONG_REQUEST_TIMEOUT_MS });
114
119
  }
115
120
  }
116
121
  catch (err) {
@@ -79,10 +79,14 @@ function readState(convGuid) {
79
79
  try {
80
80
  const raw = readFileSync(p, 'utf-8');
81
81
  const parsed = JSON.parse(raw);
82
+ const posNum = (v) => (typeof v === 'number' && v >= 0 ? v : undefined);
82
83
  return {
83
84
  last_uuid: typeof parsed.last_uuid === 'string' ? parsed.last_uuid : null,
84
85
  last_flush_ms: typeof parsed.last_flush_ms === 'number' ? parsed.last_flush_ms : undefined,
85
86
  attached: typeof parsed.attached === 'boolean' ? parsed.attached : undefined,
87
+ pending_tokens_in: posNum(parsed.pending_tokens_in),
88
+ pending_tokens_out: posNum(parsed.pending_tokens_out),
89
+ pending_active_ms: posNum(parsed.pending_active_ms),
86
90
  };
87
91
  }
88
92
  catch {
@@ -365,7 +369,51 @@ async function ensureAttached(convGuid, hook) {
365
369
  if (ok)
366
370
  writeState(convGuid, { ...(state ?? { last_uuid: null }), attached: true });
367
371
  }
368
- async function handleStopFamily(convGuid, src, hook, isSubagent, minIntervalMs = 0) {
372
+ // A gap between consecutive transcript timestamps longer than this is idle
373
+ // time (e.g. a replayed range spanning the user's think-time between
374
+ // prompts), not agent work - skip it when estimating active time.
375
+ export const ACTIVE_GAP_MAX_MS = 5 * 60_000;
376
+ /** Estimate the agent's working time over a batch of entries: the sum of
377
+ * gaps between consecutive entry timestamps, skipping idle stretches.
378
+ * Within one prompt run the transcript grows continuously (tool calls,
379
+ * assistant chunks), so this tracks real work closely; the hook path has no
380
+ * agent-reported duration the way the relay's stream `result` footer does. */
381
+ export function activeMsFromEntries(entries) {
382
+ let total = 0;
383
+ let prev = null;
384
+ for (const e of entries) {
385
+ if (!e.ts)
386
+ continue;
387
+ const t = Date.parse(e.ts);
388
+ if (isNaN(t))
389
+ continue;
390
+ if (prev !== null) {
391
+ const gap = t - prev;
392
+ if (gap > 0 && gap <= ACTIVE_GAP_MAX_MS)
393
+ total += gap;
394
+ }
395
+ prev = t;
396
+ }
397
+ return total;
398
+ }
399
+ /** Fallback token totals for parsers that don't report `usage` (Codex, Grok):
400
+ * sum the assistant entries' token fields. Claude/opencode override this via
401
+ * ParseResult.usage - their per-entry fields undercount (no cache tokens)
402
+ * or repeat across lines, which a plain entry sum can't correct. */
403
+ export function entryTokenTotals(entries) {
404
+ let tokensIn = 0;
405
+ let tokensOut = 0;
406
+ for (const e of entries) {
407
+ if (e.kind !== 'assistant')
408
+ continue;
409
+ if (typeof e.input_tokens === 'number' && e.input_tokens > 0)
410
+ tokensIn += e.input_tokens;
411
+ if (typeof e.output_tokens === 'number' && e.output_tokens > 0)
412
+ tokensOut += e.output_tokens;
413
+ }
414
+ return { tokensIn, tokensOut };
415
+ }
416
+ async function handleStopFamily(convGuid, src, hook, isSubagent, minIntervalMs = 0, emitResult = false) {
369
417
  void isSubagent;
370
418
  if (!hook.transcript_path || !existsSync(hook.transcript_path))
371
419
  return;
@@ -382,11 +430,13 @@ async function handleStopFamily(convGuid, src, hook, isSubagent, minIntervalMs =
382
430
  try {
383
431
  const state = readState(convGuid) ?? { last_uuid: null };
384
432
  const content = await readWholeFile(hook.transcript_path);
433
+ let replayed = false;
385
434
  let result = src.capture.parse(content, state.last_uuid, hook);
386
435
  if (!result.foundWatermark && state.last_uuid !== null) {
387
436
  // Transcript rotated (/clear or compact) - watermark isn't present.
388
437
  // Replay from top; server dedupes via the source_uuid unique index.
389
438
  result = src.capture.parse(content, null, hook);
439
+ replayed = true;
390
440
  }
391
441
  // Base64 image blocks (Read-of-image tool results) upload to VFS and
392
442
  // become image_ref blocks — same no-inline-base64 chokepoint as the
@@ -395,11 +445,49 @@ async function handleStopFamily(convGuid, src, hook, isSubagent, minIntervalMs =
395
445
  if (hook.cwd)
396
446
  rewriter.setCwd(hook.cwd);
397
447
  const entries = await rewriter.rewrite(result.entries);
448
+ // Roll this flush's token usage + working time into the pending
449
+ // counters. A replayed-from-top range re-parses lines whose rows the
450
+ // server already has (and whose tokens an earlier result already
451
+ // counted), so its usage is unknowable without server state - skip it
452
+ // rather than double-count; only the rotation boundary is undercounted.
453
+ const flushUsage = replayed
454
+ ? { tokensIn: 0, tokensOut: 0 }
455
+ : result.usage ?? entryTokenTotals(result.entries);
456
+ const flushActive = replayed ? 0 : activeMsFromEntries(result.entries);
457
+ const pendIn = (state.pending_tokens_in ?? 0) + flushUsage.tokensIn;
458
+ const pendOut = (state.pending_tokens_out ?? 0) + flushUsage.tokensOut;
459
+ const pendActive = (state.pending_active_ms ?? 0) + flushActive;
460
+ // On a clean end-of-run event, emit the accumulated metrics as a
461
+ // `result` entry - the same footer the relay's stream path posts, which
462
+ // the server rolls into the conversation counters the projects list
463
+ // reads (tokens_in/tokens_out/active_ms). The source_uuid is
464
+ // deterministic in the watermark so a retried POST dedupes instead of
465
+ // double-counting. No cost here: only the stream footer reports cost.
466
+ let resultEmitted = false;
467
+ if (emitResult && result.lastUuid && (pendIn > 0 || pendOut > 0 || pendActive > 0)) {
468
+ const lastTs = [...entries].reverse().find((e) => e.ts)?.ts;
469
+ entries.push({
470
+ kind: 'result',
471
+ tokens_in: pendIn,
472
+ tokens_out: pendOut,
473
+ duration_ms: pendActive,
474
+ source_uuid: `${hook.session_id ?? convGuid}-result-${result.lastUuid}`,
475
+ ...(lastTs ? { ts: lastTs } : {}),
476
+ });
477
+ resultEmitted = true;
478
+ }
398
479
  const ok = await postEntries(convGuid, entries);
399
480
  if (ok) {
400
481
  // Stamp the flush time even when no new lines landed, so the throttle
401
482
  // above measures from the last attempt. Keep the watermark if unchanged.
402
- writeState(convGuid, { last_uuid: result.lastUuid ?? state.last_uuid, last_flush_ms: Date.now() });
483
+ writeState(convGuid, {
484
+ ...state,
485
+ last_uuid: result.lastUuid ?? state.last_uuid,
486
+ last_flush_ms: Date.now(),
487
+ pending_tokens_in: resultEmitted ? 0 : pendIn,
488
+ pending_tokens_out: resultEmitted ? 0 : pendOut,
489
+ pending_active_ms: resultEmitted ? 0 : pendActive,
490
+ });
403
491
  }
404
492
  }
405
493
  finally {
@@ -411,7 +499,7 @@ async function handleSessionEnd(convGuid, src, hook) {
411
499
  // Stop, so there's usually nothing new, but a race between Stop and
412
500
  // SessionEnd could leave lines behind.
413
501
  if (hook.transcript_path && existsSync(hook.transcript_path)) {
414
- await handleStopFamily(convGuid, src, hook, false);
502
+ await handleStopFamily(convGuid, src, hook, false, 0, true);
415
503
  }
416
504
  const sessionId = hook.session_id ?? 'unknown';
417
505
  const entries = [{
@@ -518,7 +606,9 @@ async function main() {
518
606
  case 'session-start':
519
607
  break; // ensureAttached above already covers what this event used to do
520
608
  case 'stop':
521
- await handleStopFamily(convGuid, src, hook, false);
609
+ // End of a prompt run - flush AND roll the run's accumulated
610
+ // tokens/working time into the conversation via a result entry.
611
+ await handleStopFamily(convGuid, src, hook, false, 0, true);
522
612
  break;
523
613
  case 'subagent-stop':
524
614
  await handleStopFamily(convGuid, src, hook, true);