@relayflows/sdk 2.0.26 → 2.0.28

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (138) hide show
  1. package/dist/authored-flow-executor.d.ts +8 -1
  2. package/dist/authored-flow-executor.d.ts.map +1 -1
  3. package/dist/authored-flow-executor.js +16 -7
  4. package/dist/authored-flow-executor.js.map +1 -1
  5. package/dist/authored-flow-lifecycle.d.ts +68 -1
  6. package/dist/authored-flow-lifecycle.d.ts.map +1 -1
  7. package/dist/authored-flow-lifecycle.js +108 -10
  8. package/dist/authored-flow-lifecycle.js.map +1 -1
  9. package/dist/authored-flow-operation.d.ts +9 -1
  10. package/dist/authored-flow-operation.d.ts.map +1 -1
  11. package/dist/authored-flow-operation.js +7 -1
  12. package/dist/authored-flow-operation.js.map +1 -1
  13. package/dist/authored-node-entry.js +4 -0
  14. package/dist/authored-node-entry.js.map +1 -1
  15. package/dist/authored-node-runner.d.ts.map +1 -1
  16. package/dist/authored-node-runner.js +2 -1
  17. package/dist/authored-node-runner.js.map +1 -1
  18. package/dist/authored-promise-graph.d.ts +19 -4
  19. package/dist/authored-promise-graph.d.ts.map +1 -1
  20. package/dist/authored-promise-graph.js +44 -19
  21. package/dist/authored-promise-graph.js.map +1 -1
  22. package/dist/authored-root.d.ts +2 -0
  23. package/dist/authored-root.d.ts.map +1 -1
  24. package/dist/authored-root.js +2 -0
  25. package/dist/authored-root.js.map +1 -1
  26. package/dist/authored-step-graph.d.ts +83 -0
  27. package/dist/authored-step-graph.d.ts.map +1 -0
  28. package/dist/authored-step-graph.js +217 -0
  29. package/dist/authored-step-graph.js.map +1 -0
  30. package/dist/authored-step-index.d.ts +30 -0
  31. package/dist/authored-step-index.d.ts.map +1 -1
  32. package/dist/authored-step-index.js +26 -1
  33. package/dist/authored-step-index.js.map +1 -1
  34. package/dist/authored-step-output.d.ts +3 -0
  35. package/dist/authored-step-output.d.ts.map +1 -1
  36. package/dist/authored-step-output.js +4 -2
  37. package/dist/authored-step-output.js.map +1 -1
  38. package/dist/authored-worker-step.d.ts +3 -1
  39. package/dist/authored-worker-step.d.ts.map +1 -1
  40. package/dist/authored-worker-step.js +25 -6
  41. package/dist/authored-worker-step.js.map +1 -1
  42. package/dist/cli/direct-run.d.ts.map +1 -1
  43. package/dist/cli/direct-run.js +5 -2
  44. package/dist/cli/direct-run.js.map +1 -1
  45. package/dist/cli/run.d.ts +2 -0
  46. package/dist/cli/run.d.ts.map +1 -1
  47. package/dist/cli/run.js +9 -5
  48. package/dist/cli/run.js.map +1 -1
  49. package/dist/cli/step-excerpt.d.ts +48 -0
  50. package/dist/cli/step-excerpt.d.ts.map +1 -0
  51. package/dist/cli/step-excerpt.js +302 -0
  52. package/dist/cli/step-excerpt.js.map +1 -0
  53. package/dist/cli/step-failure.d.ts.map +1 -1
  54. package/dist/cli/step-failure.js +9 -20
  55. package/dist/cli/step-failure.js.map +1 -1
  56. package/dist/cli-commands.d.ts +8 -0
  57. package/dist/cli-commands.d.ts.map +1 -1
  58. package/dist/cli-commands.js +6 -0
  59. package/dist/cli-commands.js.map +1 -1
  60. package/dist/cli.d.ts +2 -0
  61. package/dist/cli.d.ts.map +1 -1
  62. package/dist/cli.js +38 -7
  63. package/dist/cli.js.map +1 -1
  64. package/dist/cloud-transcript-codex.d.ts +42 -0
  65. package/dist/cloud-transcript-codex.d.ts.map +1 -0
  66. package/dist/cloud-transcript-codex.js +411 -0
  67. package/dist/cloud-transcript-codex.js.map +1 -0
  68. package/dist/cloud-transcript-types.d.ts +161 -0
  69. package/dist/cloud-transcript-types.d.ts.map +1 -0
  70. package/dist/cloud-transcript-types.js +47 -0
  71. package/dist/cloud-transcript-types.js.map +1 -0
  72. package/dist/cloud-transcript.d.ts +2 -78
  73. package/dist/cloud-transcript.d.ts.map +1 -1
  74. package/dist/cloud-transcript.js +36 -38
  75. package/dist/cloud-transcript.js.map +1 -1
  76. package/dist/failure-kinds.d.ts +10 -2
  77. package/dist/failure-kinds.d.ts.map +1 -1
  78. package/dist/failure-kinds.js.map +1 -1
  79. package/dist/journal-client.d.ts +8 -2
  80. package/dist/journal-client.d.ts.map +1 -1
  81. package/dist/journal-client.js +22 -6
  82. package/dist/journal-client.js.map +1 -1
  83. package/dist/llm-worker.d.ts +2 -1
  84. package/dist/llm-worker.d.ts.map +1 -1
  85. package/dist/llm-worker.js +5 -2
  86. package/dist/llm-worker.js.map +1 -1
  87. package/dist/local-agent.d.ts +1 -1
  88. package/dist/local-agent.d.ts.map +1 -1
  89. package/dist/local-agent.js +8 -3
  90. package/dist/local-agent.js.map +1 -1
  91. package/dist/preflight.d.ts.map +1 -1
  92. package/dist/preflight.js +29 -3
  93. package/dist/preflight.js.map +1 -1
  94. package/dist/promise-ancestry.d.ts +20 -0
  95. package/dist/promise-ancestry.d.ts.map +1 -0
  96. package/dist/promise-ancestry.js +104 -0
  97. package/dist/promise-ancestry.js.map +1 -0
  98. package/dist/validate.d.ts.map +1 -1
  99. package/dist/validate.js +7 -0
  100. package/dist/validate.js.map +1 -1
  101. package/dist/worker-cli.d.ts +7 -0
  102. package/dist/worker-cli.d.ts.map +1 -1
  103. package/dist/worker-cli.js +42 -10
  104. package/dist/worker-cli.js.map +1 -1
  105. package/dist/worker-slots.d.ts +42 -0
  106. package/dist/worker-slots.d.ts.map +1 -0
  107. package/dist/worker-slots.js +94 -0
  108. package/dist/worker-slots.js.map +1 -0
  109. package/package.json +2 -2
  110. package/src/authored-flow-executor.ts +22 -3
  111. package/src/authored-flow-lifecycle.ts +111 -16
  112. package/src/authored-flow-operation.ts +10 -0
  113. package/src/authored-node-entry.ts +4 -1
  114. package/src/authored-node-runner.ts +2 -1
  115. package/src/authored-promise-graph.ts +46 -19
  116. package/src/authored-root.ts +4 -0
  117. package/src/authored-step-graph.ts +246 -0
  118. package/src/authored-step-index.ts +44 -1
  119. package/src/authored-step-output.ts +7 -3
  120. package/src/authored-worker-step.ts +25 -6
  121. package/src/cli/direct-run.ts +5 -2
  122. package/src/cli/run.ts +11 -4
  123. package/src/cli/step-excerpt.ts +332 -0
  124. package/src/cli/step-failure.ts +9 -20
  125. package/src/cli-commands.ts +6 -0
  126. package/src/cli.ts +40 -9
  127. package/src/cloud-transcript-codex.ts +429 -0
  128. package/src/cloud-transcript-types.ts +214 -0
  129. package/src/cloud-transcript.ts +44 -136
  130. package/src/failure-kinds.ts +10 -2
  131. package/src/journal-client.ts +25 -6
  132. package/src/llm-worker.ts +7 -2
  133. package/src/local-agent.ts +8 -2
  134. package/src/preflight.ts +37 -2
  135. package/src/promise-ancestry.ts +98 -0
  136. package/src/validate.ts +7 -0
  137. package/src/worker-cli.ts +41 -9
  138. package/src/worker-slots.ts +92 -0
package/src/cli.ts CHANGED
@@ -4,6 +4,7 @@ import { parsePluginArgs, runPluginCommand, type PluginArgs } from './cli/plugin
4
4
  import { watchCheck } from './cli-watch.js';
5
5
  import { checkHelperBody } from './cli/check-helper-body.js';
6
6
  import { describeFlowRequirements } from './flow-requirements.js';
7
+ import type { CliModelSource } from './cli-adapter.js';
7
8
 
8
9
  import { renderProgress, type ProgressEvent } from './progress.js';
9
10
  import { realpathSync } from 'node:fs';
@@ -44,6 +45,7 @@ import { runHnMonitor } from './cli/hn-monitor.js';
44
45
  import { runTickRunner } from './cli/tick-runner.js';
45
46
  import { DEFAULT_DATA_DIR } from './daemon-connection.js';
46
47
  import { CLI_VERB_NAMES } from './cli-commands.js';
48
+ import { isAgentCapacity } from './worker-slots.js';
47
49
  import {
48
50
  mintObserverUrl,
49
51
  resolveObserverLinkEnv,
@@ -80,8 +82,8 @@ export type ParsedArgs =
80
82
  | { command: 'schedules'; json: boolean }
81
83
  | { command: 'unschedule'; scheduleId: string; json: boolean }
82
84
  | { command: 'check'; json: boolean; watch: boolean; value: string }
83
- | { command: 'run'; bucket: string | undefined; reuseFromRunId: string | undefined; localAgent: boolean; dataDir: string; input: string | undefined; json: boolean; spawn: boolean; noObserverLink: boolean; allowHumanInfluenced: boolean; value: string }
84
- | { command: 'resume'; localAgent: boolean; dataDir: string; json: boolean; spawn: boolean; noObserverLink: boolean; allowHumanInfluenced: boolean; value: string }
85
+ | { command: 'run'; bucket: string | undefined; reuseFromRunId: string | undefined; localAgent: boolean; agentCapacity: number | undefined; dataDir: string; input: string | undefined; json: boolean; spawn: boolean; noObserverLink: boolean; allowHumanInfluenced: boolean; value: string }
86
+ | { command: 'resume'; localAgent: boolean; agentCapacity: number | undefined; dataDir: string; json: boolean; spawn: boolean; noObserverLink: boolean; allowHumanInfluenced: boolean; value: string }
85
87
  | { command: 'answer'; dataDir: string; json: boolean; spawn: boolean; note: string | undefined; by: string | undefined; runId: string; waitId: string; answer: boolean }
86
88
  | RunsArgs
87
89
  | LogsArgs
@@ -111,13 +113,13 @@ const USAGE = [
111
113
  'flows run <flow>@sha256:<digest> [--bucket <file-bucket-uri>] [--data-dir <dir>] [--json]',
112
114
  'flows check [--watch] [--json] <flow.ts|flow.yaml|spec.json>',
113
115
  'flows serve-webhook --data-dir <dir> --port <p> [--allow <name>[,<name>]]',
114
- 'flows run [--json] [--no-spawn] [--no-observer-link] [--data-dir <dir>] [--local-agent] [--reuse-from <run-id>] <flow.yaml|spec.json>',
116
+ 'flows run [--json] [--no-spawn] [--no-observer-link] [--data-dir <dir>] [--local-agent [--agent-capacity <n>]] [--reuse-from <run-id>] <flow.yaml|spec.json>',
115
117
  'flows run --cloud [--json] [--wait] [--sync-code] [--no-connect] <flow.yaml|spec.json>',
116
118
  'flows run --cloud [--json] [--wait] [--sync-code] [--no-connect] <flow.ts> --input <inline-json-or-file>',
117
119
  'flows sync [--json] [--dry-run] [--dir <path>] <run-id>',
118
- 'flows run [--json] [--no-spawn] [--no-observer-link] [--data-dir <dir>] [--local-agent] <flow.ts> --input <inline-json-or-file>',
120
+ 'flows run [--json] [--no-spawn] [--no-observer-link] [--data-dir <dir>] [--local-agent [--agent-capacity <n>]] <flow.ts> --input <inline-json-or-file>',
119
121
  'flows tick start --schedule-id <id> --interval-ms <ms> [--epoch-ms <ms>] [--max-catch-up <n>] [--poll-interval-ms <ms>] [--data-dir <dir>] <spec.json>',
120
- 'flows resume [--allow-human-influenced] [--json] [--no-spawn] [--no-observer-link] [--data-dir <dir>] [--local-agent] <run-id>',
122
+ 'flows resume [--allow-human-influenced] [--json] [--no-spawn] [--no-observer-link] [--data-dir <dir>] [--local-agent [--agent-capacity <n>]] <run-id>',
121
123
  'flows answer [--json] [--no-spawn] [--data-dir <dir>] [--note <text>] [--by <identity>] <run-id> <wait-id> <yes|no>',
122
124
  'flows replay [--allow-human-influenced] [--json] [--data-dir <dir>] <run-id> [--at <step-id>]',
123
125
  'flows status [--json] [--data-dir <dir>] [--tail <n>] [<run-id>]',
@@ -295,6 +297,7 @@ export async function runCli(
295
297
  onPtyReady: (path: string) => io.stderr(`PTY ${path}`),
296
298
  ...(parsed.command === 'run' && parsed.reuseFromRunId !== undefined ? { reuseFromRunId: parsed.reuseFromRunId } : {}),
297
299
  localAgent: parsed.localAgent,
300
+ ...(parsed.agentCapacity === undefined ? {} : { agentCapacity: parsed.agentCapacity }),
298
301
  onProgress: showProgress,
299
302
  onWait: (progress: RunProgress) => {
300
303
  emitWait(progress, io);
@@ -581,6 +584,7 @@ function parseArgs(args: readonly string[]): ParsedArgs | undefined {
581
584
  let syncCode = false;
582
585
  let noConnect = false;
583
586
  let localAgent = false;
587
+ let agentCapacity: number | undefined;
584
588
  let allowHumanInfluenced = false;
585
589
  let dataDir = DEFAULT_DATA_DIR;
586
590
  let sawDataDir = false;
@@ -612,6 +616,14 @@ function parseArgs(args: readonly string[]): ParsedArgs | undefined {
612
616
  localAgent = true;
613
617
  continue;
614
618
  }
619
+ if (argument === '--agent-capacity') {
620
+ // Decimal digits only: `Number` would also read "0x4", "4e0" and " 4".
621
+ const value = args[++index];
622
+ if (command === 'check' || agentCapacity !== undefined || value === undefined || !/^[0-9]+$/.test(value)) return undefined;
623
+ agentCapacity = Number(value);
624
+ if (!isAgentCapacity(agentCapacity)) return undefined;
625
+ continue;
626
+ }
615
627
  if (argument === '--watch') {
616
628
  if (command !== 'check' || watch) return undefined;
617
629
  watch = true;
@@ -669,6 +681,8 @@ function parseArgs(args: readonly string[]): ParsedArgs | undefined {
669
681
  positionals.push(argument);
670
682
  }
671
683
  if (positionals.length !== 1) return undefined;
684
+ // It sizes the in-process worker, so without `--local-agent` it describes nothing.
685
+ if (agentCapacity !== undefined && !localAgent) return undefined;
672
686
 
673
687
  if (bucket !== undefined && (cloud || !parseDigestReference(positionals[0]!))) return undefined;
674
688
  if (cloud) {
@@ -688,8 +702,8 @@ function parseArgs(args: readonly string[]): ParsedArgs | undefined {
688
702
  return command === 'check'
689
703
  ? { command, json, watch, value: positionals[0]! }
690
704
  : command === 'run'
691
- ? { command, bucket, reuseFromRunId, localAgent, dataDir, input, json, spawn, noObserverLink, allowHumanInfluenced, value: positionals[0]! }
692
- : { command, localAgent, dataDir, json, spawn, noObserverLink, allowHumanInfluenced, value: positionals[0]! };
705
+ ? { command, bucket, reuseFromRunId, localAgent, agentCapacity, dataDir, input, json, spawn, noObserverLink, allowHumanInfluenced, value: positionals[0]! }
706
+ : { command, localAgent, agentCapacity, dataDir, json, spawn, noObserverLink, allowHumanInfluenced, value: positionals[0]! };
693
707
  }
694
708
 
695
709
  /**
@@ -927,6 +941,17 @@ function parseTickArgs(rest: readonly string[]): ParsedArgs | undefined {
927
941
  };
928
942
  }
929
943
 
944
+ const MODEL_PROVENANCE: Readonly<Record<CliModelSource, string>> = {
945
+ step: 'step',
946
+ named: 'named agent',
947
+ adapter: 'adapter default',
948
+ };
949
+
950
+ /** A resolution carrying no model source prints no provenance rather than a guess. */
951
+ function modelProvenance(source: CliModelSource | undefined): string {
952
+ return source === undefined ? '' : ` from ${MODEL_PROVENANCE[source]}`;
953
+ }
954
+
930
955
  function emitCheckReport(report: CheckReport, json: boolean, io: CliIo): void {
931
956
  emitDiagnostics(report.diagnostics, io);
932
957
  if (json) {
@@ -963,8 +988,14 @@ function emitCheckReport(report: CheckReport, json: boolean, io: CliIo): void {
963
988
  const config = resolution.source === 'project' && report.projectConfigPath !== undefined
964
989
  ? ` (${report.projectConfigPath})`
965
990
  : '';
966
- const model = resolution.model === undefined ? '' : ` model "${resolution.model}"`;
967
- io.stdout(`RESOLVED step "${resolution.stepId}" cli "${resolution.cli}"${model} from ${resolution.source}${config}`);
991
+ // `from <source>` describes whatever noun precedes it, and the source here
992
+ // is the CLI's. Printing it after the model made every step whose CLI and
993
+ // model come from different places claim the model was declared where the
994
+ // CLI was — a step `cli:` plus an adapter default read as `from step`.
995
+ const model = resolution.model === undefined
996
+ ? ''
997
+ : ` model "${resolution.model}"${modelProvenance(resolution.modelSource)}`;
998
+ io.stdout(`RESOLVED step "${resolution.stepId}" cli "${resolution.cli}" from ${resolution.source}${config}${model}`);
968
999
  }
969
1000
  // What the workspace must have connected before this flow can run there;
970
1001
  // the hosted verbs check the same list against Cloud before submitting.
@@ -0,0 +1,429 @@
1
+ // The Codex half of `flows logs --step`: `codex exec --json` JSONL.
2
+ //
3
+ // The vocabulary was captured, not guessed. Every frame shape read here came
4
+ // out of a real `codex exec --json` run of codex-cli 0.155.1 on 2026-09-21;
5
+ // the captures are the two fixtures under `tests/fixtures/`, and the event and
6
+ // item names are the ones the binary's own enums carry (`thread.started`,
7
+ // `turn.started`, `turn.completed`, `turn.failed`, `item.started`,
8
+ // `item.updated`, `item.completed`, `error`; items `agent_message`,
9
+ // `reasoning`, `command_execution`, `file_change`, `mcp_tool_call`,
10
+ // `web_search`, `todo_list`, `error`). `web_search` and `todo_list` keep the
11
+ // unknown-frame placeholder: naming an item type is not the same as knowing
12
+ // which of its fields carry what, and a placeholder is honest.
13
+ //
14
+ // The two rules `cloud-transcript.ts` states hold here too. Nothing is
15
+ // dropped: an item this module cannot read becomes the placeholder line rather
16
+ // than vanishing or being reported as a success. And every provider string is
17
+ // redacted whole before it is bounded, because a truncated credential still
18
+ // matches nothing and is still most of a credential.
19
+
20
+ import {
21
+ ERROR_MAX_CHARS, OUTPUT_MAX_CHARS, OUTPUT_MAX_LINES, PATH_MAX_CHARS, TARGET_MAX_CHARS,
22
+ isRecord, num, oneLine, safe, separator, str, thousands,
23
+ type TranscriptEntry, type TranscriptToolCodex,
24
+ } from './cloud-transcript-types.js';
25
+
26
+ /** Frame types that make a log a transcript this module can render. */
27
+ export const CODEX_FRAME_TYPES: ReadonlySet<string> = new Set([
28
+ 'thread.started', 'turn.started', 'turn.completed', 'turn.failed',
29
+ 'item.started', 'item.updated', 'item.completed', 'error',
30
+ ]);
31
+
32
+ /** Item types whose lifecycles are matched and whose fields are read. */
33
+ const SUPPORTED_ITEMS: ReadonlySet<string> = new Set([
34
+ 'agent_message', 'reasoning', 'command_execution', 'file_change', 'mcp_tool_call', 'error',
35
+ ]);
36
+
37
+ const ITEM_FRAMES: ReadonlySet<string> = new Set(['item.started', 'item.updated', 'item.completed']);
38
+
39
+ /** Calls are numbered; an `apply_patch` is file activity, not a call. */
40
+ const NUMBERED_ITEMS: ReadonlySet<string> = new Set(['command_execution', 'mcp_tool_call']);
41
+
42
+ interface Lifecycle {
43
+ snapshots: Array<Record<string, unknown>>;
44
+ complete: boolean;
45
+ /** The index of the last frame of this lifecycle: where the entry is emitted. */
46
+ at: number;
47
+ }
48
+
49
+ export interface CodexIndex {
50
+ /** Every frame index that belongs to a matched lifecycle. */
51
+ matched: Set<number>;
52
+ /** The lifecycle to emit at this frame index. */
53
+ emit: Map<number, Lifecycle>;
54
+ }
55
+
56
+ /**
57
+ * Match item lifecycles locally and in order, rather than by a global set of
58
+ * completed ids.
59
+ *
60
+ * `partition` changes at every attempt marker, thread and turn boundary, so a
61
+ * reused id never reaches across one: an attempt that was killed mid-command
62
+ * keeps its unfinished call even when the next attempt completes the same id.
63
+ * Within a partition a lifecycle is keyed by id *and* item type -- two items
64
+ * that merely share an id are two items -- and a start that arrives after that
65
+ * key already completed opens a new one.
66
+ */
67
+ export function indexCodexItems(frames: ReadonlyArray<{ frame: unknown }>): CodexIndex {
68
+ const matched = new Set<number>();
69
+ const emit = new Map<number, Lifecycle>();
70
+ const open = new Map<string, Lifecycle>();
71
+ let partition = 0;
72
+ for (let index = 0; index < frames.length; index += 1) {
73
+ const frame = frames[index]!.frame;
74
+ if (!isRecord(frame)) continue;
75
+ const type = str(frame['type']);
76
+ if (type === null) continue;
77
+ if (type.startsWith('relayflow.attempt') || type === 'thread.started' || type.startsWith('turn.')) {
78
+ partition += 1;
79
+ open.clear();
80
+ continue;
81
+ }
82
+ if (!ITEM_FRAMES.has(type)) continue;
83
+ const item = isRecord(frame['item']) ? frame['item'] : null;
84
+ if (item === null) continue;
85
+ const id = str(item['id']);
86
+ const itemType = str(item['type']);
87
+ // No id is not a correlation key, and an unsupported item is not one this
88
+ // module can merge. Either way the frame stands where it is.
89
+ if (id === null || itemType === null || !SUPPORTED_ITEMS.has(itemType)) continue;
90
+ const key = `${partition}\u0000${id}\u0000${itemType}`;
91
+ let lifecycle = open.get(key);
92
+ if (lifecycle === undefined || lifecycle.complete) {
93
+ lifecycle = { snapshots: [], complete: false, at: index };
94
+ open.set(key, lifecycle);
95
+ }
96
+ lifecycle.snapshots.push(item);
97
+ lifecycle.complete ||= type === 'item.completed';
98
+ emit.delete(lifecycle.at);
99
+ lifecycle.at = index;
100
+ emit.set(index, lifecycle);
101
+ matched.add(index);
102
+ }
103
+ return { matched, emit };
104
+ }
105
+
106
+ /** Where the numbering lives; the parse loop resets it at an attempt boundary. */
107
+ export interface CodexState { seq: number }
108
+
109
+ interface Context {
110
+ clean: (text: string) => string;
111
+ state: CodexState;
112
+ /** The whole source line, for a placeholder's size. */
113
+ line: string;
114
+ type: string;
115
+ }
116
+
117
+ function bounded(text: string, clean: (t: string) => string, limit = TARGET_MAX_CHARS): string {
118
+ return oneLine(clean(text), limit);
119
+ }
120
+
121
+ /**
122
+ * The textual content of an MCP result, as one string. `content` is a string
123
+ * or the MCP content-block array; a block that carries no text (an image, say)
124
+ * contributes nothing, exactly as `textChars` counts nothing for it.
125
+ */
126
+ function mcpResultText(result: Record<string, unknown>): string | null {
127
+ const content = result['content'];
128
+ if (typeof content === 'string') return content.length === 0 ? null : content;
129
+ const parts: string[] = [];
130
+ if (Array.isArray(content)) {
131
+ for (const block of content) {
132
+ if (!isRecord(block)) continue;
133
+ const text = str(block['text']);
134
+ if (text !== null && text.length > 0) parts.push(text);
135
+ }
136
+ }
137
+ if (parts.length > 0) return parts.join('\n');
138
+ // A result can be structured only; showing it beats reporting a size alone.
139
+ const structured = result['structured_content'];
140
+ return structured === undefined || structured === null ? null : JSON.stringify(structured);
141
+ }
142
+
143
+ /** A result's size, never its content. */
144
+ function textChars(content: unknown): number {
145
+ if (typeof content === 'string') return content.length;
146
+ if (!Array.isArray(content)) return 0;
147
+ let total = 0;
148
+ for (const block of content) if (isRecord(block)) total += str(block['text'])?.length ?? 0;
149
+ return total;
150
+ }
151
+
152
+ /**
153
+ * Redact the whole output, then cut it. Both bounds are applied and either one
154
+ * sets the flag, so the reader is told the excerpt is short of the size the
155
+ * call reported.
156
+ */
157
+ function excerpt(output: string, clean: (text: string) => string): { text: string; truncated: boolean } {
158
+ const cleaned = clean(output).replace(/\n+$/u, '');
159
+ const lines = cleaned.split('\n');
160
+ let truncated = lines.length > OUTPUT_MAX_LINES;
161
+ let text = lines.slice(0, OUTPUT_MAX_LINES).join('\n');
162
+ if (text.length > OUTPUT_MAX_CHARS) { text = text.slice(0, OUTPUT_MAX_CHARS); truncated = true; }
163
+ return { text, truncated };
164
+ }
165
+
166
+ function placeholder(context: Context, itemType: string | null): TranscriptEntry {
167
+ const label = itemType === null ? context.type : `${context.type}/${itemType}`;
168
+ return { kind: 'unknown', type: context.clean(label), chars: context.line.length };
169
+ }
170
+
171
+ function tool(name: string, target: string, resultChars: number | null, isError: boolean,
172
+ codex: TranscriptToolCodex): TranscriptEntry {
173
+ return { kind: 'tool', name, target, result_chars: resultChars, is_error: isError, nested: false, codex };
174
+ }
175
+
176
+ function commandEntry(item: Record<string, unknown>, complete: boolean, context: Context,
177
+ seq: number): TranscriptEntry | null {
178
+ const command = item['command'];
179
+ if (typeof command !== 'string') return null;
180
+ const output = typeof item['aggregated_output'] === 'string' ? item['aggregated_output'] : null;
181
+ const cut = output === null ? null : excerpt(output, context.clean);
182
+ const exitCode = num(item['exit_code']);
183
+ const status = str(item['status']);
184
+ return tool('command_execution', bounded(command, context.clean), output === null ? null : output.length,
185
+ status === 'failed' || (exitCode !== null && exitCode !== 0), {
186
+ seq, status: status === null ? null : bounded(status, context.clean, 40),
187
+ exit_code: exitCode, output_excerpt: cut === null ? null : cut.text,
188
+ output_truncated: cut?.truncated === true, complete, error: null,
189
+ });
190
+ }
191
+
192
+ /** `{"message": "..."}` is the shape both `turn.failed` and a failed call use. */
193
+ function errorText(value: unknown): string | null {
194
+ if (typeof value === 'string') return value;
195
+ return isRecord(value) ? str(value['message']) : null;
196
+ }
197
+
198
+ /**
199
+ * Redact every string an argument value carries, while each one is still the
200
+ * string the provider decoded.
201
+ *
202
+ * `redact` matches an environment value literally, and `JSON.stringify` escapes
203
+ * a quote, backslash, newline or tab inside one -- so a secret containing any
204
+ * of them no longer matches its own value once serialized, and reached the page
205
+ * escaped but complete. Keys are provider strings too: an argument object built
206
+ * out of an environment dump carries the value in the name.
207
+ */
208
+ function redactLeaves(value: unknown, clean: (text: string) => string): unknown {
209
+ if (typeof value === 'string') return clean(value);
210
+ if (Array.isArray(value)) return value.map((element) => redactLeaves(element, clean));
211
+ if (!isRecord(value)) return value;
212
+ const out: Record<string, unknown> = {};
213
+ for (const [key, element] of Object.entries(value)) out[clean(key)] = redactLeaves(element, clean);
214
+ return out;
215
+ }
216
+
217
+ function mcpEntry(item: Record<string, unknown>, complete: boolean, context: Context,
218
+ seq: number): TranscriptEntry | null {
219
+ const server = str(item['server']);
220
+ const name = str(item['tool']);
221
+ if (server === null && name === null) return null;
222
+ const args = item['arguments'];
223
+ // Redacted twice, bounded once, and in that order. The leaves go first,
224
+ // before serialization can escape a secret out of its own match; the
225
+ // serialized form is redacted again by the `bounded` call below, because
226
+ // `redact` recognises a credential field by its name and a leaf standing on
227
+ // its own has no name left to recognise. Only then is the line cut.
228
+ const rendered = args === undefined || args === null
229
+ ? '' : ` ${JSON.stringify(redactLeaves(args, context.clean))}`;
230
+ const result = isRecord(item['result']) ? item['result'] : null;
231
+ const structured = result === null ? null : result['structured_content'];
232
+ const failure = errorText(item['error']);
233
+ const status = str(item['status']);
234
+ // The result is evidence: bound and redact it exactly as command output is,
235
+ // instead of reporting a size the reader then has to fetch with `--raw`.
236
+ const text = result === null ? null : mcpResultText(result);
237
+ const cut = text === null ? null : excerpt(text, context.clean);
238
+ return tool('mcp_tool_call', bounded(`${server ?? '?'}/${name ?? '?'}${rendered}`, context.clean),
239
+ result === null ? null : textChars(result['content']) + (structured === undefined || structured === null
240
+ ? 0 : JSON.stringify(structured).length),
241
+ status === 'failed' || failure !== null, {
242
+ seq, status: status === null ? null : bounded(status, context.clean, 40),
243
+ exit_code: null, output_excerpt: cut === null ? null : cut.text,
244
+ output_truncated: cut?.truncated === true, complete,
245
+ error: failure === null ? null : bounded(failure, context.clean, ERROR_MAX_CHARS),
246
+ });
247
+ }
248
+
249
+ function fileChangeEntry(item: Record<string, unknown>, complete: boolean, context: Context): TranscriptEntry | null {
250
+ const raw = item['changes'];
251
+ if (!Array.isArray(raw)) return null;
252
+ const changes: Array<{ path: string | null; change: string | null }> = [];
253
+ let malformed = 0;
254
+ for (const change of raw) {
255
+ if (!isRecord(change)) { malformed += 1; continue; }
256
+ const path = str(change['path']);
257
+ const kind = str(change['kind']);
258
+ if (path === null && kind === null) { malformed += 1; continue; }
259
+ changes.push({
260
+ path: path === null ? null : bounded(path, context.clean, PATH_MAX_CHARS),
261
+ change: kind === null ? null : bounded(kind, context.clean, 40),
262
+ });
263
+ }
264
+ const status = str(item['status']);
265
+ return {
266
+ kind: 'file_change', changes, malformed,
267
+ status: status === null ? null : bounded(status, context.clean, 40), complete,
268
+ };
269
+ }
270
+
271
+ function itemEntries(item: Record<string, unknown>, complete: boolean, context: Context): TranscriptEntry[] {
272
+ const itemType = str(item['type']);
273
+ if (itemType === 'agent_message') {
274
+ const text = item['text'];
275
+ if (typeof text !== 'string') return [placeholder(context, itemType)];
276
+ return [{ kind: 'message', role: 'assistant', text: context.clean(text), nested: false,
277
+ ...(complete ? {} : { complete: false }) }];
278
+ }
279
+ if (itemType === 'reasoning') {
280
+ const text = item['text'];
281
+ // The count, never the text -- the one thing Claude's renderer refuses to
282
+ // print, and Codex is not the place that starts printing it.
283
+ if (typeof text !== 'string') return [placeholder(context, itemType)];
284
+ return [{ kind: 'thinking', chars: text.length, nested: false }];
285
+ }
286
+ if (itemType === 'error') {
287
+ const message = errorText(item['message']);
288
+ if (message === null) return [placeholder(context, itemType)];
289
+ return [{ kind: 'error', source: 'item', message: bounded(message, context.clean, ERROR_MAX_CHARS) }];
290
+ }
291
+ if (itemType === 'command_execution' || itemType === 'mcp_tool_call') {
292
+ // The number is only spent on a call this module could actually read, so a
293
+ // malformed item leaves no gap in the sequence.
294
+ const seq = context.state.seq + 1;
295
+ const entry = itemType === 'command_execution'
296
+ ? commandEntry(item, complete, context, seq) : mcpEntry(item, complete, context, seq);
297
+ if (entry === null) return [placeholder(context, itemType)];
298
+ context.state.seq = seq;
299
+ return [entry];
300
+ }
301
+ if (itemType === 'file_change') {
302
+ const entry = fileChangeEntry(item, complete, context);
303
+ return [entry ?? placeholder(context, itemType)];
304
+ }
305
+ return [placeholder(context, itemType)];
306
+ }
307
+
308
+ const NO_USAGE = {
309
+ tokens_in: null, tokens_out: null, cache_read: null, cache_creation: null, reasoning_out: null,
310
+ } as const;
311
+
312
+ /**
313
+ * One Codex frame's entries, or undefined when the frame belongs to a
314
+ * lifecycle that is reported at a later frame.
315
+ */
316
+ export function codexFrame(
317
+ frame: Record<string, unknown>, type: string, index: number, index_: CodexIndex,
318
+ state: CodexState, clean: (text: string) => string, line: string,
319
+ ): TranscriptEntry[] | undefined {
320
+ const context: Context = { clean, state, line, type };
321
+ if (type === 'thread.started') {
322
+ const id = str(frame['thread_id']);
323
+ return [{ kind: 'thread', thread_id: id === null ? null : bounded(id, clean) }];
324
+ }
325
+ if (type === 'turn.started') return [{ kind: 'turn', phase: 'started', ...NO_USAGE }];
326
+ if (type === 'turn.completed') {
327
+ const usage = isRecord(frame['usage']) ? frame['usage'] : null;
328
+ return [{
329
+ kind: 'turn', phase: 'completed',
330
+ tokens_in: usage === null ? null : num(usage['input_tokens']),
331
+ tokens_out: usage === null ? null : num(usage['output_tokens']),
332
+ cache_read: usage === null ? null : num(usage['cached_input_tokens']),
333
+ cache_creation: usage === null ? null : num(usage['cache_write_input_tokens']),
334
+ reasoning_out: usage === null ? null : num(usage['reasoning_output_tokens']),
335
+ }];
336
+ }
337
+ if (type === 'turn.failed' || type === 'error') {
338
+ const message = errorText(type === 'error' ? frame['message'] : frame['error']);
339
+ // A failure with no readable message is still a failure; it must not fall
340
+ // through to anything that reads like success.
341
+ const entry: TranscriptEntry = {
342
+ kind: 'error', source: type,
343
+ message: message === null ? '(no message)' : bounded(message, clean, ERROR_MAX_CHARS),
344
+ };
345
+ return type === 'turn.failed' ? [{ kind: 'turn', phase: 'failed', ...NO_USAGE }, entry] : [entry];
346
+ }
347
+ // Everything left in `CODEX_FRAME_TYPES` is an `item.*` frame.
348
+ const lifecycle = index_.emit.get(index);
349
+ if (lifecycle === undefined) {
350
+ // Part of a lifecycle reported later, or an item this module does not
351
+ // correlate -- no id, an unsupported type, a malformed frame.
352
+ if (index_.matched.has(index)) return [];
353
+ const item = isRecord(frame['item']) ? frame['item'] : null;
354
+ if (item === null) return [placeholder(context, null)];
355
+ return itemEntries(item, type === 'item.completed', context);
356
+ }
357
+ // The latest snapshot wins field by field, so a completion's null exit code
358
+ // is not overwritten by the zero-length output its start reported.
359
+ const merged = Object.assign({}, ...lifecycle.snapshots) as Record<string, unknown>;
360
+ return itemEntries(merged, lifecycle.complete, context);
361
+ }
362
+
363
+ function indented(text: string): string[] {
364
+ return text.split('\n').map((line) => ` ${safe(line)}`);
365
+ }
366
+
367
+ /** The Codex entry kinds. Undefined for everything the Claude renderer owns. */
368
+ export function renderCodexEntry(entry: TranscriptEntry): string[] | undefined {
369
+ switch (entry.kind) {
370
+ case 'thread':
371
+ return [`session codex${entry.thread_id === null ? '' : ` · thread ${safe(entry.thread_id)}`}`];
372
+ case 'turn': {
373
+ if (entry.phase === 'started') return [separator('turn')];
374
+ if (entry.phase === 'failed') return [separator('turn failed')];
375
+ const facts = [
376
+ entry.tokens_in === null && entry.tokens_out === null ? null
377
+ : `${entry.tokens_in === null ? '?' : thousands(entry.tokens_in)} in`
378
+ + ` / ${entry.tokens_out === null ? '?' : thousands(entry.tokens_out)} out`,
379
+ entry.cache_read === null ? null : `${thousands(entry.cache_read)} cache read`,
380
+ entry.cache_creation === null ? null : `${thousands(entry.cache_creation)} cache write`,
381
+ entry.reasoning_out === null ? null : `${thousands(entry.reasoning_out)} reasoning out`,
382
+ ].filter((fact): fact is string => fact !== null);
383
+ return [separator(`turn complete${facts.length === 0 ? '' : ` · ${facts.join(' · ')}`}`)];
384
+ }
385
+ case 'error':
386
+ return [entry.source === 'error' ? 'error:' : `error (${safe(entry.source)}):`, ...entry.message.split('\n').map((line) => ` ${safe(line)}`)];
387
+ case 'file_change': {
388
+ const total = entry.changes.length + entry.malformed;
389
+ const state = [
390
+ entry.status === null ? null : safe(entry.status),
391
+ entry.complete ? null : 'no completion frame',
392
+ ].filter((fact): fact is string => fact !== null);
393
+ const lines = [` files ${total} change${total === 1 ? '' : 's'}`
394
+ + `${state.length === 0 ? '' : ` · ${state.join(' · ')}`}`];
395
+ for (const change of entry.changes) {
396
+ lines.push(` ${safe(change.change ?? '?')} ${safe(change.path ?? '(no path)')}`);
397
+ }
398
+ if (entry.malformed > 0) {
399
+ lines.push(` ${entry.malformed} change${entry.malformed === 1 ? '' : 's'}`
400
+ + ' not rendered here — see --raw');
401
+ }
402
+ return lines;
403
+ }
404
+ case 'tool': {
405
+ if (entry.codex === undefined) return undefined;
406
+ const codex = entry.codex;
407
+ // The size stays on the line whatever happened: a reviewer reading a
408
+ // failed step needs the exit code, the status and the size together.
409
+ const facts = [
410
+ entry.result_chars === null ? 'no result' : `${thousands(entry.result_chars)} chars`,
411
+ codex.exit_code === null ? null : `exit ${codex.exit_code}`,
412
+ codex.status === null ? null : safe(codex.status),
413
+ codex.complete ? null : 'no completion frame',
414
+ ].filter((fact): fact is string => fact !== null);
415
+ const lines = [` tool ${codex.seq} ${safe(entry.name)} ${safe(entry.target ?? '')} → ${facts.join(' · ')}`];
416
+ if (codex.error !== null) lines.push(...indented(`error ${codex.error}`));
417
+ if (codex.output_excerpt !== null && codex.output_excerpt.length > 0) {
418
+ lines.push(...indented(codex.output_excerpt));
419
+ }
420
+ if (codex.output_truncated) {
421
+ lines.push(` … excerpt cut at ${thousands(OUTPUT_MAX_LINES)} lines`
422
+ + ` / ${thousands(OUTPUT_MAX_CHARS)} chars — see --raw`);
423
+ }
424
+ return lines;
425
+ }
426
+ default:
427
+ return undefined;
428
+ }
429
+ }