@relayflows/sdk 2.0.31 → 2.0.33

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 (95) hide show
  1. package/dist/authored-preflight.d.ts +4 -0
  2. package/dist/authored-preflight.d.ts.map +1 -0
  3. package/dist/authored-preflight.js +50 -0
  4. package/dist/authored-preflight.js.map +1 -0
  5. package/dist/authored-worker-step.d.ts.map +1 -1
  6. package/dist/authored-worker-step.js +3 -2
  7. package/dist/authored-worker-step.js.map +1 -1
  8. package/dist/cli/check.d.ts +10 -2
  9. package/dist/cli/check.d.ts.map +1 -1
  10. package/dist/cli/check.js +16 -137
  11. package/dist/cli/check.js.map +1 -1
  12. package/dist/cli/cli-probe.d.ts +20 -0
  13. package/dist/cli/cli-probe.d.ts.map +1 -0
  14. package/dist/cli/cli-probe.js +166 -0
  15. package/dist/cli/cli-probe.js.map +1 -0
  16. package/dist/cli/cloud-mirror-session.d.ts +97 -0
  17. package/dist/cli/cloud-mirror-session.d.ts.map +1 -0
  18. package/dist/cli/cloud-mirror-session.js +264 -0
  19. package/dist/cli/cloud-mirror-session.js.map +1 -0
  20. package/dist/cli-commands.d.ts +6 -0
  21. package/dist/cli-commands.d.ts.map +1 -1
  22. package/dist/cli-commands.js +1 -0
  23. package/dist/cli-commands.js.map +1 -1
  24. package/dist/cli.d.ts +7 -0
  25. package/dist/cli.d.ts.map +1 -1
  26. package/dist/cli.js +84 -18
  27. package/dist/cli.js.map +1 -1
  28. package/dist/cloud-deploy.d.ts +1 -1
  29. package/dist/cloud-deploy.d.ts.map +1 -1
  30. package/dist/cloud-deploy.js +24 -8
  31. package/dist/cloud-deploy.js.map +1 -1
  32. package/dist/cloud-mirror-ledger.d.ts +40 -0
  33. package/dist/cloud-mirror-ledger.d.ts.map +1 -0
  34. package/dist/cloud-mirror-ledger.js +142 -0
  35. package/dist/cloud-mirror-ledger.js.map +1 -0
  36. package/dist/cloud-mirror-step.d.ts +180 -0
  37. package/dist/cloud-mirror-step.d.ts.map +1 -0
  38. package/dist/cloud-mirror-step.js +523 -0
  39. package/dist/cloud-mirror-step.js.map +1 -0
  40. package/dist/cloud-mirror-transport.d.ts +94 -0
  41. package/dist/cloud-mirror-transport.d.ts.map +1 -0
  42. package/dist/cloud-mirror-transport.js +163 -0
  43. package/dist/cloud-mirror-transport.js.map +1 -0
  44. package/dist/cloud-mirror.d.ts +132 -0
  45. package/dist/cloud-mirror.d.ts.map +1 -0
  46. package/dist/cloud-mirror.js +556 -0
  47. package/dist/cloud-mirror.js.map +1 -0
  48. package/dist/cloud-transcript-codex.d.ts.map +1 -1
  49. package/dist/cloud-transcript-codex.js +90 -4
  50. package/dist/cloud-transcript-codex.js.map +1 -1
  51. package/dist/cloud-transcript-types.d.ts +19 -1
  52. package/dist/cloud-transcript-types.d.ts.map +1 -1
  53. package/dist/cloud-transcript-types.js.map +1 -1
  54. package/dist/cloud-transcript.d.ts +1 -1
  55. package/dist/cloud-transcript.d.ts.map +1 -1
  56. package/dist/cloud-transcript.js +10 -0
  57. package/dist/cloud-transcript.js.map +1 -1
  58. package/dist/helper-reference.d.ts.map +1 -1
  59. package/dist/helper-reference.js +134 -6
  60. package/dist/helper-reference.js.map +1 -1
  61. package/dist/hosted-extension-runtime.js +1 -1
  62. package/dist/hosted-extension-sandbox.js +1 -1
  63. package/dist/package-version.d.ts +10 -0
  64. package/dist/package-version.d.ts.map +1 -0
  65. package/dist/package-version.js +22 -0
  66. package/dist/package-version.js.map +1 -0
  67. package/dist/preflight.d.ts +14 -0
  68. package/dist/preflight.d.ts.map +1 -1
  69. package/dist/preflight.js +18 -3
  70. package/dist/preflight.js.map +1 -1
  71. package/dist/relay-cli.d.ts.map +1 -1
  72. package/dist/relay-cli.js +1 -21
  73. package/dist/relay-cli.js.map +1 -1
  74. package/package.json +2 -2
  75. package/src/authored-preflight.ts +46 -0
  76. package/src/authored-worker-step.ts +4 -3
  77. package/src/cli/check.ts +25 -163
  78. package/src/cli/cli-probe.ts +194 -0
  79. package/src/cli/cloud-mirror-session.ts +329 -0
  80. package/src/cli-commands.ts +1 -0
  81. package/src/cli.ts +94 -19
  82. package/src/cloud-deploy.ts +26 -8
  83. package/src/cloud-mirror-ledger.ts +161 -0
  84. package/src/cloud-mirror-step.ts +650 -0
  85. package/src/cloud-mirror-transport.ts +261 -0
  86. package/src/cloud-mirror.ts +633 -0
  87. package/src/cloud-transcript-codex.ts +96 -4
  88. package/src/cloud-transcript-types.ts +18 -1
  89. package/src/cloud-transcript.ts +9 -1
  90. package/src/helper-reference.ts +131 -6
  91. package/src/hosted-extension-runtime.ts +1 -1
  92. package/src/hosted-extension-sandbox.ts +1 -1
  93. package/src/package-version.ts +23 -0
  94. package/src/preflight.ts +31 -7
  95. package/src/relay-cli.ts +1 -24
@@ -0,0 +1,650 @@
1
+ // One journal, folded into the two step shapes Cloud stores for a run.
2
+ //
3
+ // A hosted run's steps reach Cloud twice: as a *live snapshot* while it runs
4
+ // (`POST /runs/<id>/steps/snapshot`) and as the *final report* once it is over
5
+ // (`POST /runs/<id>/steps`). The sandbox derives both from `flows status`;
6
+ // this module derives them from the same journal fold `flows status` uses
7
+ // (`run-state.ts`), so a local run and a hosted one put the same facts in the
8
+ // same fields and the dashboard cannot tell which produced a row.
9
+ //
10
+ // Nothing here does I/O or reads a clock: the caller passes the events and
11
+ // `now_ms`, exactly as `foldRunState` does. That is what lets a test fold a
12
+ // hand-built journal and assert the exact bytes a push would carry.
13
+ //
14
+ // ## The bounds are the server's, restated
15
+ //
16
+ // Every cap below is the one Cloud's own parser enforces
17
+ // (`@cloud/core` `storage/step-snapshot.ts` and `storage/step-detail.ts`).
18
+ // They are restated rather than imported because this package cannot depend on
19
+ // the Cloud app — the same reason `cloud-transcript.ts` restates the
20
+ // dashboard's transcript vocabulary. A push that violates one is refused
21
+ // whole, so producing a bounded value here is not politeness: it is the
22
+ // difference between a run that appears on the dashboard and one that does
23
+ // not.
24
+ //
25
+ // ## Redaction
26
+ //
27
+ // Every dynamic string goes through `redact` before it is bounded, never
28
+ // after: clipping first can leave the head of a secret standing where the
29
+ // redactor would have replaced the whole value. Identifier-shaped fields are
30
+ // then normalized into the shape Cloud's parser accepts, because the redactor
31
+ // emits `[redacted:NAME]`, which no identifier pattern admits.
32
+
33
+ import { redact } from './redact.js';
34
+ import { foldRunState, type RunView, type StepView } from './run-state.js';
35
+ import type { JournalEvent } from './journal-reader.js';
36
+ import { AUTHORED_STEP_STREAM, foldAuthoredStepRecords } from './authored-step-index.js';
37
+
38
+ /** Steps one snapshot may carry; a longer run publishes its live tail. */
39
+ export const SNAPSHOT_MAX_STEPS = 64;
40
+ /** Serialized snapshot envelope cap, against the real UTF-8 byte count. */
41
+ export const SNAPSHOT_MAX_BYTES = 64 * 1024;
42
+ /** Artifact paths named per step; the rest are counted. */
43
+ export const SNAPSHOT_ARTIFACTS_MAX = 5;
44
+ export const SNAPSHOT_ARTIFACT_MAX_CHARS = 200;
45
+ /** `stepName`, `journalRunId`, `stepType`, `completionReason`, gate, model. */
46
+ export const IDENTIFIER_MAX_CHARS = 128;
47
+ /** Predecessors one live step lists. */
48
+ export const SNAPSHOT_DEPENDS_ON_MAX = 32;
49
+ /** Predecessors a final row lists. */
50
+ export const DEPENDS_ON_MAX_ENTRIES = 256;
51
+ /** The backing columns are PostgreSQL 32-bit integers. */
52
+ export const MAX_INT32 = 2_147_483_647;
53
+ /** `workflow_steps.display_name`, in code points. */
54
+ export const LABEL_MAX_CHARS = 120;
55
+ /** `workflow_steps.output_summary`. */
56
+ export const OUTPUT_SUMMARY_MAX_CHARS = 1000;
57
+ /** `workflow_steps.error`. */
58
+ export const ERROR_MAX_CHARS = 1024;
59
+ /** Serialized `workflow_steps.detail`. */
60
+ export const DETAIL_MAX_BYTES = 16 * 1024;
61
+ /** Attempt rows kept on `detail.attempts`; older ones are counted, not listed. */
62
+ export const DETAIL_MAX_ATTEMPTS = 20;
63
+ /**
64
+ * The one completion reason the kernel uses for success. Cloud keys a step's
65
+ * status off exactly this value, so the live derivation and the final row
66
+ * agree about what "completed" means.
67
+ */
68
+ export const SUCCESS_COMPLETION_REASON = 'success';
69
+ /** The authored root step spans the whole flow; its children carry the steps. */
70
+ export const AUTHORED_ROOT_STEP = 'authored-root';
71
+
72
+ const IDENTIFIER = /^[A-Za-z0-9_.:/-]+$/u;
73
+ const IDENTIFIER_UNSAFE = /[^A-Za-z0-9_.:/-]+/gu;
74
+
75
+ /** Live kernel states Cloud publishes. A step that has not begun has no honest rendering. */
76
+ export type SnapshotState = 'running' | 'backoff' | 'waiting' | 'needs_human' | 'done';
77
+
78
+ export interface SnapshotStep {
79
+ stepName: string;
80
+ /** Child runs may repeat a step id, so identity is the pair. */
81
+ journalRunId: string;
82
+ stepType: string;
83
+ state: SnapshotState;
84
+ attempt: number;
85
+ elapsedMs: number;
86
+ completionReason?: string;
87
+ gate?: { name: string; verdict: string };
88
+ model?: string;
89
+ turns?: number;
90
+ toolCalls?: number;
91
+ costUsd?: number;
92
+ artifactCount?: number;
93
+ artifacts?: string[];
94
+ label?: string;
95
+ dependsOn?: string[];
96
+ }
97
+
98
+ /** One row of the final report; the field names are `workflow_steps`'s own. */
99
+ export interface FinalStep {
100
+ stepName: string;
101
+ stepType: string;
102
+ agent: string;
103
+ preset: string;
104
+ cli: string;
105
+ sandboxId: string;
106
+ startTime: string;
107
+ endTime: string;
108
+ durationMs: number;
109
+ exitCode: number;
110
+ outputSummary: string;
111
+ status: 'completed' | 'failed';
112
+ completionReason: string;
113
+ retryCount: number;
114
+ model?: string;
115
+ tokensInput?: number;
116
+ tokensOutput?: number;
117
+ /**
118
+ * True when tokens were counted but nobody priced them.
119
+ *
120
+ * The kernel already records this (`run-state.ts`'s `Spend`), and
121
+ * `flows status` already prints `(unmetered)` from it. Without it on the
122
+ * wire, Cloud cannot tell a step that genuinely cost nothing — a
123
+ * deterministic `echo` — from one that spent real money against an unpriced
124
+ * model: both arrive as an absent `costUsd` and render blank. Codex is the
125
+ * everyday case, because it selects its own model and never reports it, so
126
+ * `MODEL_PRICING` has nothing to match (`model-pricing.ts` says so outright).
127
+ */
128
+ costUnmetered?: true;
129
+ costUsd?: number;
130
+ error?: string;
131
+ detail?: Record<string, unknown>;
132
+ detailTruncated?: true;
133
+ label?: string;
134
+ dependsOn?: string[];
135
+ }
136
+
137
+ /** One step's per-attempt transcript files, for the `<stepName>/agent.log` upload. */
138
+ export interface StepTranscriptRef {
139
+ stepName: string;
140
+ attempts: Array<{ attempt: number; path: string }>;
141
+ }
142
+
143
+ /** What one journal contributes, keyed for the caller's cross-journal cache. */
144
+ export interface MirroredJournal {
145
+ runId: string;
146
+ status: RunView['status'];
147
+ terminal: boolean;
148
+ steps: SnapshotStep[];
149
+ finals: FinalStep[];
150
+ transcripts: StepTranscriptRef[];
151
+ /** Child journals this run admitted, from an authored root's step index. */
152
+ children: string[];
153
+ /** Authored graph hints by step id, from an authored root's step index. */
154
+ hints: Map<string, { label?: string; after?: string[] }>;
155
+ }
156
+
157
+ type Payload = Record<string, unknown>;
158
+
159
+ function record(value: unknown): Payload | undefined {
160
+ return value !== null && typeof value === 'object' && !Array.isArray(value) ? value as Payload : undefined;
161
+ }
162
+
163
+ function int32(value: unknown, floor = 0): number | undefined {
164
+ if (typeof value !== 'number' || !Number.isFinite(value)) return undefined;
165
+ const rounded = Math.round(value);
166
+ if (rounded < floor) return undefined;
167
+ return Math.min(rounded, MAX_INT32);
168
+ }
169
+
170
+ /**
171
+ * Redact, then normalize, then clip — in that order. Clipping first could
172
+ * leave the head of a secret standing; normalizing first would let a
173
+ * multi-character replacement push the result back over the cap.
174
+ */
175
+ export function identifier(
176
+ value: unknown,
177
+ env: NodeJS.ProcessEnv,
178
+ maxChars: number = IDENTIFIER_MAX_CHARS,
179
+ ): string | undefined {
180
+ if (typeof value !== 'string' || value.length === 0) return undefined;
181
+ const clipped = redact(value, env).replace(IDENTIFIER_UNSAFE, '_').slice(0, Math.max(1, maxChars));
182
+ return IDENTIFIER.test(clipped) ? clipped : undefined;
183
+ }
184
+
185
+ /** Free text, redacted then bounded by code points. Empty becomes undefined. */
186
+ export function text(value: unknown, env: NodeJS.ProcessEnv, maxChars: number): string | undefined {
187
+ if (typeof value !== 'string' || value.length === 0) return undefined;
188
+ const cleaned = redact(value, env);
189
+ const bounded = [...cleaned].slice(0, maxChars).join('');
190
+ return bounded.length === 0 ? undefined : bounded;
191
+ }
192
+
193
+ /**
194
+ * A step label as `workflow_steps.display_name` accepts it: control characters
195
+ * become spaces, whitespace runs collapse, the result is trimmed and clipped.
196
+ */
197
+ export function label(value: unknown, env: NodeJS.ProcessEnv): string | undefined {
198
+ if (typeof value !== 'string') return undefined;
199
+ const normalized = redact(value, env)
200
+ .replace(/[\p{Cc}\p{Cf}\p{Zl}\p{Zp}]+/gu, ' ')
201
+ .replace(/\s+/gu, ' ')
202
+ .trim();
203
+ if (normalized.length === 0) return undefined;
204
+ return [...normalized].slice(0, LABEL_MAX_CHARS).join('').trimEnd();
205
+ }
206
+
207
+ function snapshotState(state: StepView['state']): SnapshotState | undefined {
208
+ // `pending` and `runnable` are states, but not publishable ones: a step that
209
+ // has not begun is neither completed nor running.
210
+ return state === 'pending' || state === 'runnable' ? undefined : state;
211
+ }
212
+
213
+ /** Cloud's own vocabulary for a live step, derived rather than carried. */
214
+ export function snapshotStepStatus(step: Pick<SnapshotStep, 'state' | 'completionReason'>):
215
+ 'running' | 'completed' | 'failed' {
216
+ if (step.state !== 'done') return 'running';
217
+ return step.completionReason === SUCCESS_COMPLETION_REASON ? 'completed' : 'failed';
218
+ }
219
+
220
+ function snapshotStep(journalRunId: string, step: StepView, env: NodeJS.ProcessEnv): SnapshotStep | undefined {
221
+ const state = snapshotState(step.state);
222
+ if (state === undefined) return undefined;
223
+ const stepName = identifier(step.id, env);
224
+ const journal = identifier(journalRunId, env);
225
+ const stepType = identifier(step.type, env);
226
+ if (stepName === undefined || journal === undefined || stepType === undefined) return undefined;
227
+ const transcript = step.last_attempt?.transcript ?? null;
228
+ const verification = step.last_attempt?.verification ?? null;
229
+ const gateName = verification === null ? undefined : identifier(verification.gate, env);
230
+ const gateVerdict = verification === null ? undefined : identifier(verification.verdict, env);
231
+ const paths = step.artifacts.paths
232
+ .slice(0, SNAPSHOT_ARTIFACTS_MAX)
233
+ .map(path => identifier(path, env, SNAPSHOT_ARTIFACT_MAX_CHARS))
234
+ .filter((path): path is string => path !== undefined);
235
+ const completionReason = identifier(step.completion_reason ?? step.last_attempt?.completion_reason, env);
236
+ const cost = transcript?.total_cost_usd;
237
+ return {
238
+ stepName,
239
+ journalRunId: journal,
240
+ stepType,
241
+ state,
242
+ attempt: int32(step.attempt) ?? 0,
243
+ elapsedMs: int32(step.elapsed_ms) ?? 0,
244
+ ...(completionReason === undefined ? {} : { completionReason }),
245
+ ...(gateName === undefined || gateVerdict === undefined ? {} : { gate: { name: gateName, verdict: gateVerdict } }),
246
+ ...(identifier(transcript?.model, env) === undefined ? {} : { model: identifier(transcript?.model, env)! }),
247
+ ...(int32(transcript?.num_turns) === undefined ? {} : { turns: int32(transcript?.num_turns)! }),
248
+ ...(int32(transcript?.tool_calls) === undefined ? {} : { toolCalls: int32(transcript?.tool_calls)! }),
249
+ ...(typeof cost === 'number' && Number.isFinite(cost) && cost >= 0 ? { costUsd: cost } : {}),
250
+ ...(step.artifacts.journaled ? { artifactCount: int32(step.artifacts.paths.length) ?? 0 } : {}),
251
+ ...(paths.length === 0 ? {} : { artifacts: paths }),
252
+ };
253
+ }
254
+
255
+ function isoAt(ms: number | null | undefined): string {
256
+ if (typeof ms !== 'number' || !Number.isFinite(ms)) return '';
257
+ const at = new Date(ms);
258
+ return Number.isNaN(at.getTime()) ? '' : at.toISOString();
259
+ }
260
+
261
+ /**
262
+ * The bounded `workflow_steps.detail` for one step: the attempt's journaled
263
+ * transcript digest, plus the attempt roster. The digest is already redacted
264
+ * and bounded where the worker wrote it (`agent-transcript.ts`); it is
265
+ * re-bounded here because this process, not that one, is what Cloud's parser
266
+ * will judge.
267
+ */
268
+ function stepDetail(
269
+ attempts: AttemptRecord[],
270
+ env: NodeJS.ProcessEnv,
271
+ ): { detail?: Record<string, unknown>; truncated?: true } {
272
+ const last = attempts.at(-1);
273
+ const digest = record(record(last?.trajectoryTail)?.['transcript']);
274
+ const roster = attempts.slice(-DETAIL_MAX_ATTEMPTS).map(entry => ({
275
+ attempt: entry.attempt,
276
+ atMs: entry.atMs,
277
+ completionReason: identifier(entry.completionReason, env) ?? 'unknown',
278
+ ...(identifier(entry.disposition, env) === undefined ? {} : { disposition: identifier(entry.disposition, env)! }),
279
+ ...(entry.transcriptBytes === undefined ? {} : { transcriptBytes: entry.transcriptBytes }),
280
+ }));
281
+ if (digest === undefined && roster.length <= 1) return {};
282
+ // The digest carries a sandbox-free copy of the transcript's facts. `file`
283
+ // names a path on *this* machine, which is neither useful to a reader of the
284
+ // dashboard nor something to publish; the byte counts beside it are.
285
+ const file = record(digest?.['file']);
286
+ const detail: Record<string, unknown> = {
287
+ ...(digest === undefined ? {} : {
288
+ transcript: {
289
+ ...digest,
290
+ ...(file === undefined ? {} : {
291
+ file: Object.fromEntries(Object.entries(file).filter(([key]) => key !== 'path')),
292
+ }),
293
+ },
294
+ }),
295
+ ...(roster.length === 0 ? {} : { attempts: roster }),
296
+ ...(attempts.length > roster.length ? { attemptsOmitted: attempts.length - roster.length } : {}),
297
+ };
298
+ if (Buffer.byteLength(JSON.stringify(detail), 'utf8') <= DETAIL_MAX_BYTES) return { detail };
299
+ // Over the cap: drop the transcript's free text and its tool roster — the
300
+ // largest fields by far — and say the detail was cut rather than dropping it
301
+ // whole, which would take the attempt history with it.
302
+ const transcript = record(detail['transcript']);
303
+ const reduced: Record<string, unknown> = {
304
+ ...(transcript === undefined ? {} : {
305
+ transcript: Object.fromEntries(
306
+ Object.entries(transcript).filter(([key]) => key === 'attempt' || key === 'exit_code'
307
+ || key === 'file' || key === 'result'),
308
+ ),
309
+ }),
310
+ ...(roster.length === 0 ? {} : { attempts: roster }),
311
+ };
312
+ if (Buffer.byteLength(JSON.stringify(reduced), 'utf8') <= DETAIL_MAX_BYTES) {
313
+ return { detail: reduced, truncated: true };
314
+ }
315
+ return { detail: { attempts: roster.slice(-1) }, truncated: true };
316
+ }
317
+
318
+ interface AttemptRecord {
319
+ attempt: number;
320
+ atMs: number;
321
+ completionReason: string;
322
+ disposition: string;
323
+ trajectoryTail: unknown;
324
+ transcriptBytes?: number;
325
+ transcriptPath?: string;
326
+ tokensIn?: number;
327
+ tokensOut?: number;
328
+ costUsd?: number;
329
+ costUnmetered?: boolean;
330
+ }
331
+
332
+ /**
333
+ * Every `step.completed` the journal holds, in order, by step id.
334
+ *
335
+ * `foldRunState` keeps only the *last* attempt, because that is what a status
336
+ * view shows. The final report's `detail.attempts` is the whole roster, and
337
+ * its token totals are the sum across attempts, so both need the raw entries.
338
+ */
339
+ function attemptsByStep(events: readonly JournalEvent[]): Map<string, AttemptRecord[]> {
340
+ const byStep = new Map<string, AttemptRecord[]>();
341
+ for (const event of events) {
342
+ if (event.entry_type !== 'step.completed' || event.step_id === null) continue;
343
+ const payload = record(event.payload) ?? {};
344
+ const budget = record(payload['budget']) ?? {};
345
+ const digest = record(record(payload['trajectory_tail'])?.['transcript']);
346
+ const file = record(digest?.['file']);
347
+ const result = record(digest?.['result']);
348
+ const entries = byStep.get(event.step_id) ?? [];
349
+ entries.push({
350
+ attempt: event.attempt ?? entries.length + 1,
351
+ atMs: event.at_ms,
352
+ completionReason: typeof payload['completionReason'] === 'string' ? payload['completionReason'] : 'unknown',
353
+ disposition: typeof payload['disposition'] === 'string' ? payload['disposition'] : 'unknown',
354
+ trajectoryTail: payload['trajectory_tail'],
355
+ ...(int32(file?.['bytes_kept']) === undefined ? {} : { transcriptBytes: int32(file?.['bytes_kept'])! }),
356
+ ...(typeof file?.['path'] === 'string' ? { transcriptPath: file['path'] } : {}),
357
+ ...(int32(budget['tokens_in']) === undefined ? {} : { tokensIn: int32(budget['tokens_in'])! }),
358
+ ...(int32(budget['tokens_out']) === undefined ? {} : { tokensOut: int32(budget['tokens_out'])! }),
359
+ ...(typeof result?.['total_cost_usd'] === 'number' && Number.isFinite(result['total_cost_usd'])
360
+ && result['total_cost_usd'] >= 0
361
+ ? { costUsd: result['total_cost_usd'] as number } : {}),
362
+ ...(budget['dollars_unmetered'] === true ? { costUnmetered: true } : {}),
363
+ });
364
+ byStep.set(event.step_id, entries);
365
+ }
366
+ return byStep;
367
+ }
368
+
369
+ function finalStep(
370
+ step: StepView,
371
+ attempts: AttemptRecord[],
372
+ env: NodeJS.ProcessEnv,
373
+ ): FinalStep | undefined {
374
+ const stepName = identifier(step.id, env);
375
+ const stepType = identifier(step.type, env);
376
+ if (stepName === undefined || stepType === undefined) return undefined;
377
+ const completionReason = step.completion_reason ?? step.last_attempt?.completion_reason ?? 'unknown';
378
+ const succeeded = completionReason === SUCCESS_COMPLETION_REASON;
379
+ const transcript = step.last_attempt?.transcript ?? null;
380
+ const startMs = step.started_at_ms;
381
+ const endMs = step.last_attempt?.ended_at_ms ?? null;
382
+ const duration = startMs !== null && endMs !== null ? Math.max(0, endMs - startMs) : (step.elapsed_ms ?? 0);
383
+ const tokensIn = attempts.reduce((total, entry) => total + (entry.tokensIn ?? 0), 0);
384
+ const tokensOut = attempts.reduce((total, entry) => total + (entry.tokensOut ?? 0), 0);
385
+ const failure = transcript?.failure ?? null;
386
+ // Summed across attempts, exactly as the tokens above are. Taking the last
387
+ // attempt's figure while summing its tokens was inconsistent in itself, and
388
+ // Cloud totals these rows for the run's spend — so a retried agent's earlier
389
+ // charges simply vanished from the run.
390
+ const spentUsd = attempts.reduce((total, entry) => total + (entry.costUsd ?? 0), 0);
391
+ const priced = attempts.some(entry => entry.costUsd !== undefined);
392
+ /**
393
+ * Any unpriced attempt makes the step's cost unknown, even when another
394
+ * attempt reported one.
395
+ *
396
+ * The first cut suppressed the flag whenever `spentUsd > 0`, which published
397
+ * a partial subtotal as though it were the whole cost: a step that spent
398
+ * $0.02 on attempt 1 and an unpriced amount on attempt 2 reported exactly
399
+ * $0.02. The two facts are not exclusive — `costUsd` is then a *lower bound*
400
+ * and the flag says so. `run-state.ts`'s `addSpend` keeps them together for
401
+ * the same reason.
402
+ *
403
+ * Presence, not a positive sum: an attempt reporting `total_cost_usd: 0` did
404
+ * produce a price, and testing `spentUsd === 0` would have called that step
405
+ * unpriced.
406
+ */
407
+ const unmetered = attempts.some(entry => entry.costUnmetered === true);
408
+ const detail = stepDetail(attempts, env);
409
+ const verification = step.last_attempt?.verification ?? null;
410
+ // What the step said about itself. A gate's verdict detail is the nearest
411
+ // thing a deterministic step has to an agent's final message; an agent step
412
+ // has neither in the status view, so the completion reason stands alone
413
+ // rather than being invented.
414
+ const summary = text(verification?.detail, env, OUTPUT_SUMMARY_MAX_CHARS)
415
+ ?? `step ${succeeded ? 'completed' : 'ended'}: ${identifier(completionReason, env) ?? 'unknown'}`;
416
+ const model = identifier(transcript?.model, env);
417
+ const error = succeeded ? undefined : text(failure?.excerpt, env, ERROR_MAX_CHARS);
418
+ return {
419
+ stepName,
420
+ stepType,
421
+ // Cloud's legacy v1 columns. The kernel has no agent/preset/CLI identity
422
+ // per step, and an empty string is the honest answer the hosted v2
423
+ // executor gives too — never a guess that would render as a real name.
424
+ agent: '',
425
+ preset: '',
426
+ cli: '',
427
+ // Named after the uploaded transcript object once one is written, exactly
428
+ // as the sandbox does; empty until then, never a fabricated sandbox id.
429
+ sandboxId: '',
430
+ startTime: isoAt(startMs),
431
+ endTime: isoAt(endMs),
432
+ durationMs: int32(duration) ?? 0,
433
+ exitCode: succeeded ? 0 : 1,
434
+ outputSummary: summary,
435
+ status: succeeded ? 'completed' : 'failed',
436
+ completionReason: identifier(completionReason, env) ?? 'unknown',
437
+ retryCount: Math.max(0, attempts.length - 1),
438
+ ...(model === undefined ? {} : { model }),
439
+ ...(tokensIn > 0 ? { tokensInput: Math.min(tokensIn, MAX_INT32) } : {}),
440
+ ...(tokensOut > 0 ? { tokensOutput: Math.min(tokensOut, MAX_INT32) } : {}),
441
+ ...(priced ? { costUsd: spentUsd } : {}),
442
+ ...(unmetered ? { costUnmetered: true as const } : {}),
443
+ ...(error === undefined ? {} : { error }),
444
+ ...(detail.detail === undefined ? {} : { detail: detail.detail }),
445
+ ...(detail.truncated === undefined ? {} : { detailTruncated: detail.truncated }),
446
+ };
447
+ }
448
+
449
+ /** Per-attempt transcript files this step left on disk, in attempt order. */
450
+ function transcriptRef(stepName: string, attempts: AttemptRecord[]): StepTranscriptRef | undefined {
451
+ const files = attempts
452
+ .filter((entry): entry is AttemptRecord & { transcriptPath: string } => entry.transcriptPath !== undefined)
453
+ .map(entry => ({ attempt: entry.attempt, path: entry.transcriptPath }));
454
+ return files.length === 0 ? undefined : { stepName, attempts: files };
455
+ }
456
+
457
+ /**
458
+ * Fold one journal into everything the mirror can say about it.
459
+ *
460
+ * The authored root is deliberately not published as a step: it spans the
461
+ * whole flow, so a snapshot carrying it would show one node called
462
+ * `authored-root` sitting at 100% for the run's entire duration beside the
463
+ * steps that actually did the work. Its journal is still read, for the step
464
+ * index that names the child journals and their graph edges.
465
+ */
466
+ export function mirrorJournal(
467
+ runId: string,
468
+ events: readonly JournalEvent[],
469
+ nowMs: number,
470
+ env: NodeJS.ProcessEnv = process.env,
471
+ ): MirroredJournal {
472
+ const view = foldRunState(events, nowMs);
473
+ const attempts = attemptsByStep(events);
474
+ const steps: SnapshotStep[] = [];
475
+ const finals: FinalStep[] = [];
476
+ const transcripts: StepTranscriptRef[] = [];
477
+ for (const step of view.steps) {
478
+ if (step.id === AUTHORED_ROOT_STEP) continue;
479
+ const live = snapshotStep(runId, step, env);
480
+ if (live !== undefined) steps.push(live);
481
+ const stepAttempts = attempts.get(step.id) ?? [];
482
+ if (step.state === 'done') {
483
+ const row = finalStep(step, stepAttempts, env);
484
+ if (row !== undefined) {
485
+ finals.push(row);
486
+ const ref = transcriptRef(row.stepName, stepAttempts);
487
+ if (ref !== undefined) transcripts.push(ref);
488
+ }
489
+ }
490
+ }
491
+ const index = authoredIndex(events, env);
492
+ // A declarative flow declares its edges in the spec, not in an authored-step
493
+ // stream — so a YAML run had no hints at all and drew every node
494
+ // unconnected. Read them from `run.spawned`, keyed the same way, and let the
495
+ // authored index win where both exist (an authored root's index is the
496
+ // richer record, and carries labels too).
497
+ const declared = declaredEdges(runId, events, env);
498
+ for (const [key, hint] of declared) {
499
+ if (!index.hints.has(key)) index.hints.set(key, hint);
500
+ }
501
+ return {
502
+ runId,
503
+ status: view.status,
504
+ terminal: view.status === 'completed' || view.status === 'failed' || view.status === 'cancelled',
505
+ steps,
506
+ finals,
507
+ transcripts,
508
+ children: index.children,
509
+ hints: index.hints,
510
+ };
511
+ }
512
+
513
+ /**
514
+ * An authored root's step index: which child journal runs each authored step,
515
+ * what the author called it, and what it came after.
516
+ *
517
+ * This is the only place those facts exist. A child journal's own view knows
518
+ * its kernel step ids and nothing about the authored graph above it, so
519
+ * without the root's index a mirrored authored run would show its steps with
520
+ * no names and no edges — which is exactly what the run graph draws from.
521
+ */
522
+ function authoredIndex(
523
+ events: readonly JournalEvent[],
524
+ env: NodeJS.ProcessEnv,
525
+ ): { children: string[]; hints: Map<string, { label?: string; after?: string[] }> } {
526
+ const messages: unknown[] = [];
527
+ for (const event of events) {
528
+ if (event.entry_type !== 'stream.appended') continue;
529
+ const payload = record(event.payload);
530
+ if (payload?.['stream'] === AUTHORED_STEP_STREAM) messages.push(payload['message']);
531
+ }
532
+ const children: string[] = [];
533
+ const hints = new Map<string, { label?: string; after?: string[] }>();
534
+ for (const entry of foldAuthoredStepRecords(messages).values()) {
535
+ if (/^[A-Za-z0-9][A-Za-z0-9_-]*$/u.test(entry.runId)) children.push(entry.runId);
536
+ const name = label(entry.label, env);
537
+ const after = entry.after === undefined
538
+ ? undefined
539
+ : [...entry.after]
540
+ .slice(0, DEPENDS_ON_MAX_ENTRIES)
541
+ .map(value => identifier(value, env))
542
+ .filter((value): value is string => value !== undefined);
543
+ if (name === undefined && after === undefined) continue;
544
+ hints.set(`${entry.runId}/${entry.step}`, {
545
+ ...(name === undefined ? {} : { label: name }),
546
+ ...(after === undefined ? {} : { after }),
547
+ });
548
+ }
549
+ return { children, hints };
550
+ }
551
+
552
+ /**
553
+ * The `depends_on` a declarative spec declares, as graph hints.
554
+ *
555
+ * `foldRunState` reads these to decide readiness but does not surface them on
556
+ * `StepView`, and deliberately: `flows status --json` is a pinned shape. So
557
+ * they are read here, straight off the `run.spawned` payload this fold already
558
+ * has in hand, rather than by widening a view other readers depend on.
559
+ */
560
+ function declaredEdges(
561
+ runId: string,
562
+ events: readonly JournalEvent[],
563
+ env: NodeJS.ProcessEnv,
564
+ ): Map<string, { after?: string[] }> {
565
+ const hints = new Map<string, { after?: string[] }>();
566
+ const spawned = events.find(event => event.entry_type === 'run.spawned');
567
+ const steps = record(spawned?.payload)?.['spec'];
568
+ const declared = record(steps)?.['steps'];
569
+ if (!Array.isArray(declared)) return hints;
570
+ for (const entry of declared) {
571
+ const step = record(entry);
572
+ const id = identifier(step?.['id'], env);
573
+ if (id === undefined) continue;
574
+ const raw = step?.['depends_on'] ?? step?.['dependsOn'];
575
+ if (!Array.isArray(raw)) continue;
576
+ const after = raw
577
+ .slice(0, DEPENDS_ON_MAX_ENTRIES)
578
+ .map(value => identifier(value, env))
579
+ .filter((value): value is string => value !== undefined);
580
+ hints.set(`${runId}/${id}`, { after });
581
+ }
582
+ return hints;
583
+ }
584
+
585
+ /**
586
+ * Build a snapshot envelope that actually fits both bounds.
587
+ *
588
+ * The count cap applies unconditionally — 65 tiny steps are 65 steps. Only
589
+ * then does the byte cap bite: first the artifact *names* go (their count
590
+ * survives), then the `dependsOn` edges, then whole entries from the head of
591
+ * the display order, because the tail is the newest work and that is what a
592
+ * live view exists to show. A capped view announces itself as incomplete;
593
+ * silently showing fewer steps than ran is the one thing it must not do.
594
+ */
595
+ export function fitSnapshot(
596
+ steps: readonly SnapshotStep[],
597
+ envelope: { sequence: number; capturedAt: string; alreadyOmitted?: number },
598
+ ): { sequence: number; capturedAt: string; truncated?: true; omittedStepCount?: number; steps: SnapshotStep[] } {
599
+ let kept = [...steps];
600
+ let omitted = envelope.alreadyOmitted ?? 0;
601
+ let truncated = omitted > 0;
602
+ if (kept.length > SNAPSHOT_MAX_STEPS) {
603
+ omitted += kept.length - SNAPSHOT_MAX_STEPS;
604
+ kept = kept.slice(kept.length - SNAPSHOT_MAX_STEPS);
605
+ truncated = true;
606
+ }
607
+ const build = (): { sequence: number; capturedAt: string; truncated?: true; omittedStepCount?: number; steps: SnapshotStep[] } => ({
608
+ sequence: envelope.sequence,
609
+ capturedAt: envelope.capturedAt,
610
+ ...(truncated ? { truncated: true as const } : {}),
611
+ ...(omitted > 0 ? { omittedStepCount: Math.min(omitted, MAX_INT32) } : {}),
612
+ steps: kept,
613
+ });
614
+ const bytes = (): number => Buffer.byteLength(JSON.stringify(build()), 'utf8');
615
+ if (bytes() <= SNAPSHOT_MAX_BYTES) return build();
616
+ kept = kept.map(({ artifacts: _artifacts, ...step }) => step);
617
+ truncated = true;
618
+ if (bytes() <= SNAPSHOT_MAX_BYTES) return build();
619
+ kept = kept.map(({ dependsOn: _dependsOn, ...step }) => step);
620
+ if (bytes() <= SNAPSHOT_MAX_BYTES) return build();
621
+ while (kept.length > 1 && bytes() > SNAPSHOT_MAX_BYTES) {
622
+ kept = kept.slice(1);
623
+ omitted += 1;
624
+ }
625
+ return build();
626
+ }
627
+
628
+ /** Apply an authored root's graph hints to a step, bounded for the live view. */
629
+ export function withGraphHints<T extends { stepName: string; journalRunId?: string }>(
630
+ step: T,
631
+ hints: Map<string, { label?: string; after?: string[] }>,
632
+ journalRunId: string,
633
+ maxDependsOn: number,
634
+ knownSteps: ReadonlySet<string>,
635
+ ): T & { label?: string; dependsOn?: string[] } {
636
+ const hint = hints.get(`${journalRunId}/${step.stepName}`);
637
+ if (hint === undefined) return step;
638
+ // Only predecessors that are themselves steps of this report: an edge to a
639
+ // node the graph does not contain draws nothing and reads as data loss.
640
+ const after = hint.after
641
+ ?.filter(name => name !== step.stepName && knownSteps.has(name))
642
+ .slice(0, maxDependsOn);
643
+ return {
644
+ ...step,
645
+ ...(hint.label === undefined ? {} : { label: hint.label }),
646
+ ...(after === undefined ? {} : { dependsOn: after }),
647
+ };
648
+ }
649
+
650
+ export type { RunView };