maka-agent 0.2.0-dev.23.20260906 → 0.2.0-dev.25.20260908

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 (215) hide show
  1. package/README.md +38 -19
  2. package/README.zh-CN.md +34 -19
  3. package/dist/acp/maka-acp-agent.js +2 -1
  4. package/dist/acp/session-configuration.js +143 -0
  5. package/dist/acp/session-registry.js +90 -4
  6. package/dist/cli-core.js +2 -0
  7. package/dist/pi-transcript-tools.js +6 -0
  8. package/dist/pi-transcript.js +5 -12
  9. package/dist/pi-tui-layout.js +56 -10
  10. package/dist/pi-tui-pickers.js +102 -1
  11. package/dist/pi-tui-runner.js +230 -46
  12. package/dist/pi-tui-todo.js +221 -0
  13. package/dist/pi-tui-transcript-viewer.js +134 -7
  14. package/dist/runtime-host-cli-context.js +224 -78
  15. package/dist/runtime-host-cli-installation.js +8 -2
  16. package/dist/runtime-host-cli.js +31 -1
  17. package/dist/runtime-host-handoff-surface.js +106 -0
  18. package/dist/runtime-host-installed-update-bootstrap.js +13 -0
  19. package/dist/runtime-host-installed-update-coordinator.js +33 -0
  20. package/dist/runtime-host-lifecycle-transaction.js +9 -1
  21. package/dist/runtime-host-local-handoff.js +48 -5
  22. package/dist/runtime-host-local-managed-activation.js +84 -0
  23. package/dist/runtime-host-local-source-retirement.js +18 -2
  24. package/dist/runtime-host-managed-lifecycle-manager.js +1 -0
  25. package/dist/runtime-host-operator-retirement.js +50 -0
  26. package/dist/runtime-host-service-manager.js +3 -3
  27. package/dist/runtime-host-session-channel.js +13 -0
  28. package/dist/runtime-host-session-driver.js +46 -26
  29. package/dist/runtime-host-session-update.js +62 -0
  30. package/dist/runtime-host-tui-command.js +38 -73
  31. package/dist/runtime-host-tui-context.js +1 -0
  32. package/dist/runtime-host-tui-owner.js +226 -0
  33. package/dist/tui-copy-catalog.js +219 -6
  34. package/dist/tui-host-owner-copy.js +24 -0
  35. package/native/runtime-host-windows-task-launcher/prebuilds/win32-x64/maka-runtime-host-task-launcher.exe +0 -0
  36. package/node_modules/@maka/core/dist/agent-run.js +1 -0
  37. package/node_modules/@maka/core/dist/attachments.js +3 -0
  38. package/node_modules/@maka/core/dist/canonical-json.js +27 -0
  39. package/node_modules/@maka/core/dist/canonical-runtime-event.js +3 -1
  40. package/node_modules/@maka/core/dist/capabilities.js +35 -0
  41. package/node_modules/@maka/core/dist/computer-use.js +0 -1
  42. package/node_modules/@maka/core/dist/display-redaction.js +18 -0
  43. package/node_modules/@maka/core/dist/llm-connections.js +13 -0
  44. package/node_modules/@maka/core/dist/model-call-attempt.js +2 -0
  45. package/node_modules/@maka/core/dist/model-failure.js +40 -0
  46. package/node_modules/@maka/core/dist/model-metadata.generated.js +4 -4
  47. package/node_modules/@maka/core/dist/model-metadata.js +7 -3
  48. package/node_modules/@maka/core/dist/model-web-search.js +5 -4
  49. package/node_modules/@maka/core/dist/provider-registry.js +45 -4
  50. package/node_modules/@maka/core/dist/record-schema.js +9 -2
  51. package/node_modules/@maka/core/dist/redaction.js +35 -0
  52. package/node_modules/@maka/core/dist/run-composition.js +77 -2
  53. package/node_modules/@maka/core/dist/runtime-boundary.js +31 -11
  54. package/node_modules/@maka/core/dist/runtime-event.js +37 -6
  55. package/node_modules/@maka/core/dist/runtime-handoff.js +98 -0
  56. package/node_modules/@maka/core/dist/runtime-invocation.js +1 -1
  57. package/node_modules/@maka/core/dist/runtime-logical-execution.js +112 -0
  58. package/node_modules/@maka/core/dist/session.js +84 -56
  59. package/node_modules/@maka/core/dist/slash-command-catalog.js +3 -0
  60. package/node_modules/@maka/core/dist/tool-result-archive-evidence.js +20 -0
  61. package/node_modules/@maka/core/dist/tool-result-record-schema.js +2 -1
  62. package/node_modules/@maka/core/dist/usage-record-schema.js +26 -35
  63. package/node_modules/@maka/core/package.json +5 -0
  64. package/node_modules/@maka/eval/dist/experiment-directory.js +4 -13
  65. package/node_modules/@maka/mcp/dist/index.js +131 -1
  66. package/node_modules/@maka/runtime/dist/agent-run-inspect.js +0 -13
  67. package/node_modules/@maka/runtime/dist/agent-run.js +351 -105
  68. package/node_modules/@maka/runtime/dist/ai-sdk-backend.js +29 -13
  69. package/node_modules/@maka/runtime/dist/ai-sdk-compaction.js +109 -34
  70. package/node_modules/@maka/runtime/dist/ai-sdk-turn.js +184 -183
  71. package/node_modules/@maka/runtime/dist/archive-read-tool.js +2 -2
  72. package/node_modules/@maka/runtime/dist/context-budget-helpers.js +0 -16
  73. package/node_modules/@maka/runtime/dist/context-budget.js +14 -9
  74. package/node_modules/@maka/runtime/dist/conversation-copy.js +158 -15
  75. package/node_modules/@maka/runtime/dist/github-copilot-oauth-enrollment.js +3 -3
  76. package/node_modules/@maka/runtime/dist/goal-continuation.js +36 -4
  77. package/node_modules/@maka/runtime/dist/history-compact-checkpoint.js +18 -9
  78. package/node_modules/@maka/runtime/dist/history-compact-summarizer.js +30 -22
  79. package/node_modules/@maka/runtime/dist/history-compaction.js +29 -4
  80. package/node_modules/@maka/runtime/dist/ledger-tool-result-archive-reader.js +174 -0
  81. package/node_modules/@maka/runtime/dist/mcp-tools.js +7 -2
  82. package/node_modules/@maka/runtime/dist/message-authority.js +9 -0
  83. package/node_modules/@maka/runtime/dist/model-adapter.js +19 -98
  84. package/node_modules/@maka/runtime/dist/model-factory.js +19 -81
  85. package/node_modules/@maka/runtime/dist/model-fetcher.js +49 -2
  86. package/node_modules/@maka/runtime/dist/model-history.js +0 -112
  87. package/node_modules/@maka/runtime/dist/model-runtime.js +85 -111
  88. package/node_modules/@maka/runtime/dist/native-web-search-tool.js +2 -1
  89. package/node_modules/@maka/runtime/dist/openai-codex-history-compactor.js +2 -2
  90. package/node_modules/@maka/runtime/dist/opencode-session-header.js +4 -2
  91. package/node_modules/@maka/runtime/dist/plugin-runtime.js +20 -10
  92. package/node_modules/@maka/runtime/dist/plugin-tool-service.js +205 -0
  93. package/node_modules/@maka/runtime/dist/provider-error-classification.js +84 -123
  94. package/node_modules/@maka/runtime/dist/provider-request-telemetry.js +76 -109
  95. package/node_modules/@maka/runtime/dist/request-shape.js +23 -0
  96. package/node_modules/@maka/runtime/dist/run-handoff-gate.js +97 -0
  97. package/node_modules/@maka/runtime/dist/runtime-event-backfill.js +83 -55
  98. package/node_modules/@maka/runtime/dist/runtime-event-read-model.js +121 -159
  99. package/node_modules/@maka/runtime/dist/runtime-kernel.js +157 -112
  100. package/node_modules/@maka/runtime/dist/runtime-ledger-repair.js +160 -79
  101. package/node_modules/@maka/runtime/dist/runtime-read-model.js +30 -89
  102. package/node_modules/@maka/runtime/dist/runtime-resume.js +80 -4
  103. package/node_modules/@maka/runtime/dist/session-event-runtime-mapper.js +5 -0
  104. package/node_modules/@maka/runtime/dist/session-manager.js +171 -288
  105. package/node_modules/@maka/runtime/dist/session-projection-helpers.js +0 -27
  106. package/node_modules/@maka/runtime/dist/stream-graph-coordinator.js +21 -0
  107. package/node_modules/@maka/runtime/dist/stream-graph-supervisor-tools.js +0 -5
  108. package/node_modules/@maka/runtime/dist/subscription-model-fetch.js +3 -4
  109. package/node_modules/@maka/runtime/dist/test-connection.js +16 -17
  110. package/node_modules/@maka/runtime/dist/tool-activation-identity.js +41 -0
  111. package/node_modules/@maka/runtime/dist/tool-availability.js +11 -1
  112. package/node_modules/@maka/runtime/dist/tool-result-archive-encoding.js +38 -0
  113. package/node_modules/@maka/runtime/dist/tool-result-archive-resource.js +63 -1
  114. package/node_modules/@maka/runtime/dist/tool-result-archive-transition.js +42 -12
  115. package/node_modules/@maka/runtime/dist/tool-result-archive.js +27 -4
  116. package/node_modules/@maka/runtime/dist/tool-runtime.js +3 -61
  117. package/node_modules/@maka/runtime/package.json +2 -0
  118. package/node_modules/@maka/runtime-host/dist/adapter/session-projector.js +35 -0
  119. package/node_modules/@maka/runtime-host/dist/client/connect-or-spawn.js +9 -1
  120. package/node_modules/@maka/runtime-host/dist/client/connection.js +3 -0
  121. package/node_modules/@maka/runtime-host/dist/client/host-handoff-copy.js +179 -0
  122. package/node_modules/@maka/runtime-host/dist/client/host-handoff.js +275 -0
  123. package/node_modules/@maka/runtime-host/dist/client/host-retirement.js +26 -5
  124. package/node_modules/@maka/runtime-host/dist/client/index.js +2 -0
  125. package/node_modules/@maka/runtime-host/dist/client/launcher.js +3 -0
  126. package/node_modules/@maka/runtime-host/dist/client/remote-compatibility-error.js +2 -0
  127. package/node_modules/@maka/runtime-host/dist/client/session-subscription.js +46 -5
  128. package/node_modules/@maka/runtime-host/dist/control/endpoint.js +1 -3
  129. package/node_modules/@maka/runtime-host/dist/node-error.js +22 -0
  130. package/node_modules/@maka/runtime-host/dist/operator/index.js +2 -1
  131. package/node_modules/@maka/runtime-host/dist/operator/managed-deployment.js +1 -3
  132. package/node_modules/@maka/runtime-host/dist/operator/retirement-cancellation.js +20 -0
  133. package/node_modules/@maka/runtime-host/dist/peer-mesh/limits.js +0 -1
  134. package/node_modules/@maka/runtime-host/dist/peer-mesh/model.js +1 -1
  135. package/node_modules/@maka/runtime-host/dist/peer-reachability/publisher.js +1 -3
  136. package/node_modules/@maka/runtime-host/dist/protocol/host-status.js +21 -12
  137. package/node_modules/@maka/runtime-host/dist/protocol/index.js +29 -1
  138. package/node_modules/@maka/runtime-host/dist/protocol/operations.js +1 -0
  139. package/node_modules/@maka/runtime-host/dist/protocol/plan.js +1 -0
  140. package/node_modules/@maka/runtime-host/dist/protocol/plugin-platform.js +27 -5
  141. package/node_modules/@maka/runtime-host/dist/protocol/project-catalog.js +2 -2
  142. package/node_modules/@maka/runtime-host/dist/protocol/session-continuity.js +40 -20
  143. package/node_modules/@maka/runtime-host/dist/protocol/session-transcript.js +0 -5
  144. package/node_modules/@maka/runtime-host/dist/protocol/session-turns.js +29 -58
  145. package/node_modules/@maka/runtime-host/dist/protocol/turn.js +11 -1
  146. package/node_modules/@maka/runtime-host/dist/protocol/workhub-coordination.js +31 -2
  147. package/node_modules/@maka/runtime-host/dist/server/access-credential-store.js +1 -0
  148. package/node_modules/@maka/runtime-host/dist/server/agent-graph-coordinator.js +0 -3
  149. package/node_modules/@maka/runtime-host/dist/server/canonical-turn-snapshot.js +19 -4
  150. package/node_modules/@maka/runtime-host/dist/server/connection-session.js +7 -6
  151. package/node_modules/@maka/runtime-host/dist/server/execution-artifacts.js +32 -32
  152. package/node_modules/@maka/runtime-host/dist/server/execution-composition.js +144 -33
  153. package/node_modules/@maka/runtime-host/dist/server/execution-model-authority.js +5 -5
  154. package/node_modules/@maka/runtime-host/dist/server/execution-model-composition.js +45 -16
  155. package/node_modules/@maka/runtime-host/dist/server/goal-coordinator.js +40 -1
  156. package/node_modules/@maka/runtime-host/dist/server/host-kernel.js +96 -17
  157. package/node_modules/@maka/runtime-host/dist/server/host-residency-registry.js +13 -7
  158. package/node_modules/@maka/runtime-host/dist/server/hosted-execution-admission-registry.js +3 -1
  159. package/node_modules/@maka/runtime-host/dist/server/hosted-execution-recovery.js +124 -62
  160. package/node_modules/@maka/runtime-host/dist/server/interactive-run-composer.js +47 -22
  161. package/node_modules/@maka/runtime-host/dist/server/message-coordinator.js +76 -6
  162. package/node_modules/@maka/runtime-host/dist/server/plugin-platform-coordinator.js +6 -0
  163. package/node_modules/@maka/runtime-host/dist/server/plugin-platform.js +6 -0
  164. package/node_modules/@maka/runtime-host/dist/server/root-turn-coordinator.js +325 -73
  165. package/node_modules/@maka/runtime-host/dist/server/scheduled-task-coordinator.js +37 -2
  166. package/node_modules/@maka/runtime-host/dist/server/serial-outbound-writer.js +45 -3
  167. package/node_modules/@maka/runtime-host/dist/server/session-catalog-coordinator.js +50 -7
  168. package/node_modules/@maka/runtime-host/dist/server/session-continuity-coordinator.js +87 -25
  169. package/node_modules/@maka/runtime-host/dist/server/session-retirement-coordinator.js +5 -2
  170. package/node_modules/@maka/runtime-host/dist/server/session-revision-coordinator.js +12 -55
  171. package/node_modules/@maka/runtime-host/dist/server/session-sidecar-purge.js +1 -16
  172. package/node_modules/@maka/runtime-host/dist/server/session-transcript-pager.js +0 -1
  173. package/node_modules/@maka/runtime-host/dist/server/session-transcript-reader.js +460 -70
  174. package/node_modules/@maka/runtime-host/dist/server/shared-session-transcript.js +1 -1
  175. package/node_modules/@maka/runtime-host/dist/server/storage-maintenance.js +109 -0
  176. package/node_modules/@maka/runtime-host/dist/server/workhub-coordination-action-gate.js +20 -2
  177. package/node_modules/@maka/runtime-host/dist/server/workhub-coordination-coordinator.js +13 -4
  178. package/node_modules/@maka/runtime-host/dist/server/workhub-message-attachments.js +46 -0
  179. package/node_modules/@maka/storage/dist/artifact-store.js +21 -10
  180. package/node_modules/@maka/storage/dist/artifact-stores.js +1 -1
  181. package/node_modules/@maka/storage/dist/claude-code-session-adapter.js +0 -5
  182. package/node_modules/@maka/storage/dist/codex-session-adapter.js +284 -198
  183. package/node_modules/@maka/storage/dist/context-offload-snapshot.js +325 -0
  184. package/node_modules/@maka/storage/dist/execution-stores.js +9 -7
  185. package/node_modules/@maka/storage/dist/file-session-repository.js +1012 -0
  186. package/node_modules/@maka/storage/dist/git-exec.js +61 -0
  187. package/node_modules/@maka/storage/dist/git-worktree-child-executor.js +8 -35
  188. package/node_modules/@maka/storage/dist/goal-authority.js +2 -2
  189. package/node_modules/@maka/storage/dist/interaction-store.js +2 -2
  190. package/node_modules/@maka/storage/dist/opencode-session-adapter.js +0 -4
  191. package/node_modules/@maka/storage/dist/operational-state-backup.js +9 -5
  192. package/node_modules/@maka/storage/dist/project-catalog.js +2 -23
  193. package/node_modules/@maka/storage/dist/runtime-event-invariants.js +2 -2
  194. package/node_modules/@maka/storage/dist/runtime-event-persistence.js +1 -0
  195. package/node_modules/@maka/storage/dist/runtime-policy/connection-catalog-document.js +8 -3
  196. package/node_modules/@maka/storage/dist/runtime-transcript-query.js +308 -0
  197. package/node_modules/@maka/storage/dist/scheduled-task-store.js +249 -283
  198. package/node_modules/@maka/storage/dist/session-bundle-manifest.js +5 -17
  199. package/node_modules/@maka/storage/dist/session-bundle-policy.js +26 -5
  200. package/node_modules/@maka/storage/dist/session-copy-cleanup.js +2 -1
  201. package/node_modules/@maka/storage/dist/session-message-projection.js +15 -0
  202. package/node_modules/@maka/storage/dist/session-repository.js +767 -0
  203. package/node_modules/@maka/storage/dist/session-store.js +25 -99
  204. package/node_modules/@maka/storage/dist/sqlite-artifact-metadata.js +10 -3
  205. package/node_modules/@maka/storage/dist/sqlite-context-offload-store.js +24 -8
  206. package/node_modules/@maka/storage/dist/sqlite-core-execution-schema.js +7 -1
  207. package/node_modules/@maka/storage/dist/sqlite-runtime-schema.js +44 -1
  208. package/node_modules/@maka/storage/dist/sqlite-runtime-store.js +130 -27
  209. package/node_modules/@maka/storage/dist/sqlite-session-metadata-store.js +109 -892
  210. package/node_modules/@maka/storage/dist/storage-id.js +28 -0
  211. package/node_modules/@maka/storage/dist/tool-result-archive-evidence.js +138 -0
  212. package/node_modules/@maka/storage/dist/workspace-identity.js +2 -15
  213. package/node_modules/@maka/storage/dist/workspace-version-authority-internal.js +1 -8
  214. package/node_modules/@maka/storage/package.json +2 -0
  215. package/package.json +1 -1
@@ -22,8 +22,12 @@ import { basename, join, resolve, sep } from 'node:path';
22
22
  import { isSupportedCodexThreadSource, sanitizeForeignTitle } from '@maka/core/foreign-session';
23
23
  import { externalSessionMatchesQuery } from '@maka/core/external-session';
24
24
  export const CODEX_SESSION_ADAPTER_ID = 'codex';
25
- export const CODEX_ROLLOUT_MAX_BYTES = 64 * 1024 * 1024;
25
+ export const CODEX_ROLLOUT_MAX_BYTES = 2 * 1024 * 1024 * 1024;
26
26
  const CODEX_ROLLOUT_HEAD_BYTES = 512 * 1024;
27
+ const CODEX_ROLLOUT_READ_BYTES = 64 * 1024;
28
+ const CODEX_ROLLOUT_MAX_RECORD_BYTES = 64 * 1024 * 1024;
29
+ const CODEX_ROLLOUT_MAX_CONVERTED_BYTES = 256 * 1024 * 1024;
30
+ const CODEX_ROLLOUT_MAX_MESSAGES = 250_000;
27
31
  const CODEX_SESSION_ID_PATTERN = /^[A-Za-z0-9_-]{1,128}$/;
28
32
  const CODEX_UNSAFE_PATH_CHARS = /[\u0000-\u001F\u007F\u0080-\u009F\u061C\u200B-\u200F\u202A-\u202E\u2060-\u2064\u2066-\u2069\uFEFF]/;
29
33
  /**
@@ -40,12 +44,19 @@ export class CodexSessionAdapter {
40
44
  id = CODEX_SESSION_ADAPTER_ID;
41
45
  codexHome;
42
46
  maxRolloutBytes;
47
+ maxRecordBytes;
48
+ maxConvertedBytes;
49
+ maxMessages;
43
50
  constructor(options = {}) {
44
51
  this.codexHome = resolve(options.codexHome ?? process.env.CODEX_HOME ?? join(homedir(), '.codex'));
45
52
  this.maxRolloutBytes = options.maxRolloutBytes ?? CODEX_ROLLOUT_MAX_BYTES;
46
- if (!Number.isSafeInteger(this.maxRolloutBytes) || this.maxRolloutBytes <= 0) {
47
- throw new Error('Codex rollout byte limit must be a positive safe integer');
48
- }
53
+ this.maxRecordBytes = options.maxRecordBytes ?? CODEX_ROLLOUT_MAX_RECORD_BYTES;
54
+ this.maxConvertedBytes = options.maxConvertedBytes ?? CODEX_ROLLOUT_MAX_CONVERTED_BYTES;
55
+ this.maxMessages = options.maxMessages ?? CODEX_ROLLOUT_MAX_MESSAGES;
56
+ assertPositiveSafeInteger(this.maxRolloutBytes, 'Codex rollout byte limit');
57
+ assertPositiveSafeInteger(this.maxRecordBytes, 'Codex rollout record byte limit');
58
+ assertPositiveSafeInteger(this.maxConvertedBytes, 'Codex converted message byte limit');
59
+ assertPositiveSafeInteger(this.maxMessages, 'Codex converted message count limit');
49
60
  }
50
61
  async detect() {
51
62
  return ((await isDirectory(join(this.codexHome, 'sessions'))) ||
@@ -64,13 +75,12 @@ export class CodexSessionAdapter {
64
75
  const rolloutPath = await this.resolveRolloutPath(catalogEntry.rolloutPath, sessionId);
65
76
  if (!rolloutPath)
66
77
  throw new Error(`Codex rollout is unavailable: ${sessionId}`);
67
- const text = await readBoundedUtf8File(rolloutPath, this.maxRolloutBytes);
68
- const converted = convertCodexRollout(text, sessionId, catalogEntry.name, catalogEntry.cwd);
69
- return {
70
- sourceSessionId: sessionId,
71
- metadata: converted.metadata,
72
- messages: converted.messages,
73
- };
78
+ return convertCodexRollout(rolloutPath, sessionId, catalogEntry.name, catalogEntry.cwd, {
79
+ maxRolloutBytes: this.maxRolloutBytes,
80
+ maxRecordBytes: this.maxRecordBytes,
81
+ maxConvertedBytes: this.maxConvertedBytes,
82
+ maxMessages: this.maxMessages,
83
+ });
74
84
  }
75
85
  async listCatalog(query) {
76
86
  for (const dbPath of await codexStateDbsNewestFirst(this.codexHome)) {
@@ -181,263 +191,275 @@ export class CodexSessionAdapter {
181
191
  }
182
192
  }
183
193
  }
184
- function convertCodexRollout(text, expectedSessionId, fallbackName, fallbackCwd) {
185
- const records = parseRolloutRecords(text, expectedSessionId);
186
- const sessionMeta = records.find((record) => record.value.type === 'session_meta')?.value;
187
- const metaPayload = asRecord(sessionMeta?.payload);
188
- const actualSessionId = stringField(metaPayload, 'session_id') ?? stringField(metaPayload, 'id');
189
- if (actualSessionId !== expectedSessionId) {
190
- throw new Error(`Codex rollout Session id mismatch: expected ${expectedSessionId}`);
191
- }
192
- const metaCwd = safeCodexCwd(metaPayload?.cwd);
193
- const messages = [];
194
- let activeTurnId;
195
- let activeTurnIsExplicit = false;
196
- let activeModel = stringField(metaPayload, 'model_provider') ?? 'codex';
197
- let lastTimestamp = normalizeEpochMs(sessionMeta?.timestamp) ?? 0;
198
- let firstUserText;
199
- const failedTurnIds = new Set();
200
- const timestampFor = (record) => {
201
- const parsed = normalizeEpochMs(record.value.timestamp);
202
- if (parsed !== undefined)
203
- lastTimestamp = Math.max(lastTimestamp, parsed);
204
- else
205
- lastTimestamp += 1;
206
- return parsed ?? lastTimestamp;
207
- };
208
- const ensureTurnId = (line) => {
209
- activeTurnId ??= generatedCodexId(expectedSessionId, 'turn', line);
210
- return activeTurnId;
211
- };
212
- for (const record of records) {
194
+ async function convertCodexRollout(path, expectedSessionId, fallbackName, fallbackCwd, limits) {
195
+ const converter = new CodexRolloutConverter(expectedSessionId, fallbackName, fallbackCwd, limits);
196
+ for await (const record of readCodexRolloutRecords(path, expectedSessionId, limits)) {
197
+ converter.accept(record);
198
+ }
199
+ return converter.finish();
200
+ }
201
+ class CodexRolloutConverter {
202
+ expectedSessionId;
203
+ fallbackName;
204
+ fallbackCwd;
205
+ limits;
206
+ messages = [];
207
+ failedTurnIds = new Set();
208
+ activeTurnId;
209
+ activeTurnIsExplicit = false;
210
+ activeModel = 'codex';
211
+ lastTimestamp = 0;
212
+ firstUserText;
213
+ metaCwd = '';
214
+ hasSessionMeta = false;
215
+ convertedBytes = 0;
216
+ constructor(expectedSessionId, fallbackName, fallbackCwd, limits) {
217
+ this.expectedSessionId = expectedSessionId;
218
+ this.fallbackName = fallbackName;
219
+ this.fallbackCwd = fallbackCwd;
220
+ this.limits = limits;
221
+ }
222
+ accept(record) {
213
223
  const envelope = record.value;
224
+ if (envelope.type === 'session_meta' && !this.hasSessionMeta) {
225
+ this.hasSessionMeta = true;
226
+ const metaPayload = asRecord(envelope.payload);
227
+ const actualSessionId = stringField(metaPayload, 'session_id') ?? stringField(metaPayload, 'id');
228
+ if (actualSessionId !== this.expectedSessionId) {
229
+ throw new Error(`Codex rollout Session id mismatch: expected ${this.expectedSessionId}`);
230
+ }
231
+ this.metaCwd = safeCodexCwd(metaPayload?.cwd);
232
+ this.activeModel = stringField(metaPayload, 'model_provider') ?? this.activeModel;
233
+ const timestamp = normalizeEpochMs(envelope.timestamp);
234
+ if (timestamp !== undefined)
235
+ this.lastTimestamp = Math.max(this.lastTimestamp, timestamp);
236
+ return;
237
+ }
214
238
  const payload = asRecord(envelope.payload);
215
239
  if (!payload)
216
- continue;
240
+ return;
217
241
  if (envelope.type === 'turn_context') {
218
- activeTurnId = stringField(payload, 'turn_id') ?? activeTurnId;
219
- activeModel = stringField(payload, 'model') ?? activeModel;
220
- continue;
242
+ this.activeTurnId = stringField(payload, 'turn_id') ?? this.activeTurnId;
243
+ this.activeModel = stringField(payload, 'model') ?? this.activeModel;
244
+ return;
221
245
  }
222
246
  if (envelope.type === 'event_msg') {
223
247
  const eventType = stringField(payload, 'type');
224
248
  if (eventType === 'task_started' || eventType === 'turn_started') {
225
249
  const turnId = stringField(payload, 'turn_id');
226
250
  if (turnId) {
227
- activeTurnId = turnId;
228
- activeTurnIsExplicit = true;
251
+ this.activeTurnId = turnId;
252
+ this.activeTurnIsExplicit = true;
229
253
  }
230
- continue;
254
+ return;
231
255
  }
232
256
  if (eventType === 'item_completed') {
233
257
  const item = asRecord(payload.item);
234
258
  const itemType = stringField(item, 'type')?.toLowerCase();
235
259
  const eventTurnId = stringField(payload, 'turn_id');
236
260
  if (eventTurnId) {
237
- activeTurnId = eventTurnId;
238
- activeTurnIsExplicit = true;
261
+ this.activeTurnId = eventTurnId;
262
+ this.activeTurnIsExplicit = true;
239
263
  }
240
264
  if (itemType === 'usermessage') {
241
- if (!activeTurnIsExplicit) {
242
- activeTurnId = generatedCodexId(expectedSessionId, 'turn', record.line);
265
+ if (!this.activeTurnIsExplicit) {
266
+ this.activeTurnId = generatedCodexId(this.expectedSessionId, 'turn', record.line);
243
267
  }
244
268
  const text = codexCompletedItemText(item) || codexCompletedItemMediaText(item);
245
269
  if (text.length === 0)
246
- continue;
247
- firstUserText ??= text;
248
- messages.push({
270
+ return;
271
+ this.firstUserText ??= text;
272
+ this.append({
249
273
  type: 'user',
250
274
  id: stringField(item, 'client_id') ??
251
275
  stringField(item, 'id') ??
252
- generatedCodexId(expectedSessionId, 'user', record.line),
253
- turnId: ensureTurnId(record.line),
254
- ts: timestampFor(record),
276
+ generatedCodexId(this.expectedSessionId, 'user', record.line),
277
+ turnId: this.ensureTurnId(record.line),
278
+ ts: this.timestampFor(record),
255
279
  text,
256
280
  });
257
- continue;
281
+ return;
258
282
  }
259
283
  if (itemType === 'agentmessage') {
260
284
  const text = codexCompletedItemText(item);
261
285
  if (text.length === 0)
262
- continue;
286
+ return;
263
287
  const providerOptions = codexAssistantProviderOptions(item);
264
- messages.push({
288
+ this.append({
265
289
  type: 'assistant',
266
290
  id: stringField(item, 'id') ??
267
- generatedCodexId(expectedSessionId, 'assistant', record.line),
268
- turnId: ensureTurnId(record.line),
269
- ts: timestampFor(record),
291
+ generatedCodexId(this.expectedSessionId, 'assistant', record.line),
292
+ turnId: this.ensureTurnId(record.line),
293
+ ts: this.timestampFor(record),
270
294
  text,
271
295
  ...(providerOptions !== undefined ? { providerOptions } : {}),
272
- modelId: activeModel,
296
+ modelId: this.activeModel,
273
297
  contentOrder: ['text'],
274
298
  });
275
- continue;
299
+ return;
276
300
  }
277
301
  if (itemType === 'reasoning') {
278
302
  const reasoning = codexCompletedReasoningText(item);
279
303
  if (reasoning.length === 0)
280
- continue;
281
- messages.push({
304
+ return;
305
+ this.append({
282
306
  type: 'assistant',
283
307
  id: stringField(item, 'id') ??
284
- generatedCodexId(expectedSessionId, 'reasoning', record.line),
285
- turnId: ensureTurnId(record.line),
286
- ts: timestampFor(record),
308
+ generatedCodexId(this.expectedSessionId, 'reasoning', record.line),
309
+ turnId: this.ensureTurnId(record.line),
310
+ ts: this.timestampFor(record),
287
311
  text: '',
288
312
  thinking: { text: reasoning },
289
313
  contentOrder: ['thinking'],
290
- modelId: activeModel,
314
+ modelId: this.activeModel,
291
315
  });
292
- continue;
316
+ return;
293
317
  }
294
318
  }
295
319
  if (eventType === 'user_message') {
296
- if (!activeTurnIsExplicit) {
297
- activeTurnId = generatedCodexId(expectedSessionId, 'turn', record.line);
320
+ if (!this.activeTurnIsExplicit) {
321
+ this.activeTurnId = generatedCodexId(this.expectedSessionId, 'turn', record.line);
298
322
  }
299
323
  const text = stringField(payload, 'message') ?? mediaOnlyUserText(payload);
300
324
  if (text.length === 0)
301
- continue;
302
- firstUserText ??= text;
303
- const turnId = ensureTurnId(record.line);
304
- messages.push({
325
+ return;
326
+ this.firstUserText ??= text;
327
+ const turnId = this.ensureTurnId(record.line);
328
+ this.append({
305
329
  type: 'user',
306
330
  id: stringField(payload, 'client_id') ??
307
- generatedCodexId(expectedSessionId, 'user', record.line),
331
+ generatedCodexId(this.expectedSessionId, 'user', record.line),
308
332
  turnId,
309
- ts: timestampFor(record),
333
+ ts: this.timestampFor(record),
310
334
  text,
311
335
  });
312
- continue;
336
+ return;
313
337
  }
314
338
  if (eventType === 'agent_message') {
315
339
  const text = stringField(payload, 'message');
316
340
  if (!text)
317
- continue;
341
+ return;
318
342
  const providerOptions = codexAssistantProviderOptions(payload);
319
- messages.push({
343
+ this.append({
320
344
  type: 'assistant',
321
- id: generatedCodexId(expectedSessionId, 'assistant', record.line),
322
- turnId: ensureTurnId(record.line),
323
- ts: timestampFor(record),
345
+ id: generatedCodexId(this.expectedSessionId, 'assistant', record.line),
346
+ turnId: this.ensureTurnId(record.line),
347
+ ts: this.timestampFor(record),
324
348
  text,
325
349
  ...(providerOptions !== undefined ? { providerOptions } : {}),
326
- modelId: activeModel,
350
+ modelId: this.activeModel,
327
351
  contentOrder: ['text'],
328
352
  });
329
- continue;
353
+ return;
330
354
  }
331
355
  if (eventType === 'agent_reasoning') {
332
356
  const reasoning = stringField(payload, 'text');
333
357
  if (!reasoning)
334
- continue;
335
- messages.push({
358
+ return;
359
+ this.append({
336
360
  type: 'assistant',
337
- id: generatedCodexId(expectedSessionId, 'reasoning', record.line),
338
- turnId: ensureTurnId(record.line),
339
- ts: timestampFor(record),
361
+ id: generatedCodexId(this.expectedSessionId, 'reasoning', record.line),
362
+ turnId: this.ensureTurnId(record.line),
363
+ ts: this.timestampFor(record),
340
364
  text: '',
341
365
  thinking: { text: reasoning },
342
366
  contentOrder: ['thinking'],
343
- modelId: activeModel,
367
+ modelId: this.activeModel,
344
368
  });
345
- continue;
369
+ return;
346
370
  }
347
371
  if (eventType === 'context_compacted') {
348
- messages.push({
372
+ this.append({
349
373
  type: 'system_note',
350
- id: generatedCodexId(expectedSessionId, 'compact', record.line),
351
- turnId: activeTurnId,
352
- ts: timestampFor(record),
374
+ id: generatedCodexId(this.expectedSessionId, 'compact', record.line),
375
+ turnId: this.activeTurnId,
376
+ ts: this.timestampFor(record),
353
377
  kind: 'context_compacted',
354
378
  });
355
- continue;
379
+ return;
356
380
  }
357
381
  if (eventType === 'error') {
358
- if (activeTurnId && codexErrorAffectsTurnStatus(payload)) {
359
- failedTurnIds.add(activeTurnId);
382
+ if (this.activeTurnId && codexErrorAffectsTurnStatus(payload)) {
383
+ this.failedTurnIds.add(this.activeTurnId);
360
384
  }
361
- messages.push({
385
+ this.append({
362
386
  type: 'system_note',
363
- id: generatedCodexId(expectedSessionId, 'error', record.line),
364
- turnId: activeTurnId,
365
- ts: timestampFor(record),
387
+ id: generatedCodexId(this.expectedSessionId, 'error', record.line),
388
+ turnId: this.activeTurnId,
389
+ ts: this.timestampFor(record),
366
390
  kind: 'error',
367
391
  data: JSON.parse(JSON.stringify(payload)),
368
392
  });
369
- continue;
393
+ return;
370
394
  }
371
395
  if (eventType === 'task_complete' || eventType === 'turn_complete') {
372
- const turnId = stringField(payload, 'turn_id') ?? ensureTurnId(record.line);
373
- const failed = failedTurnIds.has(turnId) || payload.error != null;
374
- messages.push({
396
+ const turnId = stringField(payload, 'turn_id') ?? this.ensureTurnId(record.line);
397
+ const failed = this.failedTurnIds.has(turnId) || payload.error != null;
398
+ this.append({
375
399
  type: 'turn_state',
376
- id: generatedCodexId(expectedSessionId, 'turn-state', record.line),
400
+ id: generatedCodexId(this.expectedSessionId, 'turn-state', record.line),
377
401
  turnId,
378
- ts: timestampFor(record),
402
+ ts: this.timestampFor(record),
379
403
  status: failed ? 'failed' : 'completed',
380
404
  ...(failed ? { errorClass: 'codex_error' } : {}),
381
- partialOutputRetained: true,
382
405
  });
383
- failedTurnIds.delete(turnId);
384
- if (activeTurnId === turnId) {
385
- activeTurnId = undefined;
386
- activeTurnIsExplicit = false;
406
+ this.failedTurnIds.delete(turnId);
407
+ if (this.activeTurnId === turnId) {
408
+ this.activeTurnId = undefined;
409
+ this.activeTurnIsExplicit = false;
387
410
  }
388
- continue;
411
+ return;
389
412
  }
390
413
  if (eventType === 'turn_aborted') {
391
- const turnId = stringField(payload, 'turn_id') ?? ensureTurnId(record.line);
392
- const ts = timestampFor(record);
393
- messages.push({
414
+ const turnId = stringField(payload, 'turn_id') ?? this.ensureTurnId(record.line);
415
+ const ts = this.timestampFor(record);
416
+ this.append({
394
417
  type: 'turn_state',
395
- id: generatedCodexId(expectedSessionId, 'turn-state', record.line),
418
+ id: generatedCodexId(this.expectedSessionId, 'turn-state', record.line),
396
419
  turnId,
397
420
  ts,
398
421
  status: 'aborted',
399
422
  abortedAt: normalizeEpochMs(payload.completed_at) ?? ts,
400
423
  abortSource: stringField(payload, 'reason') ?? 'codex',
401
- partialOutputRetained: true,
402
424
  });
403
- if (activeTurnId === turnId) {
404
- activeTurnId = undefined;
405
- activeTurnIsExplicit = false;
425
+ if (this.activeTurnId === turnId) {
426
+ this.activeTurnId = undefined;
427
+ this.activeTurnIsExplicit = false;
406
428
  }
407
- continue;
429
+ return;
408
430
  }
409
431
  }
410
432
  if (envelope.type !== 'response_item')
411
- continue;
433
+ return;
412
434
  const itemType = stringField(payload, 'type');
413
435
  if (itemType === 'function_call' || itemType === 'custom_tool_call') {
414
436
  const callId = stringField(payload, 'call_id');
415
437
  const toolName = namespacedToolName(payload);
416
438
  if (!callId || !toolName)
417
- continue;
439
+ return;
418
440
  const rawArgs = itemType === 'function_call'
419
441
  ? stringField(payload, 'arguments')
420
442
  : stringField(payload, 'input');
421
- messages.push({
443
+ this.append({
422
444
  type: 'tool_call',
423
445
  id: callId,
424
- turnId: ensureTurnId(record.line),
425
- ts: timestampFor(record),
446
+ turnId: this.ensureTurnId(record.line),
447
+ ts: this.timestampFor(record),
426
448
  toolName,
427
449
  args: parseJsonString(rawArgs),
428
450
  });
429
- continue;
451
+ return;
430
452
  }
431
453
  if (itemType === 'function_call_output' || itemType === 'custom_tool_call_output') {
432
454
  const callId = stringField(payload, 'call_id');
433
455
  if (!callId)
434
- continue;
435
- messages.push({
456
+ return;
457
+ this.append({
436
458
  type: 'tool_result',
437
459
  id: stringField(payload, 'id') ??
438
- generatedCodexId(expectedSessionId, 'tool-result', record.line),
439
- turnId: ensureTurnId(record.line),
440
- ts: timestampFor(record),
460
+ generatedCodexId(this.expectedSessionId, 'tool-result', record.line),
461
+ turnId: this.ensureTurnId(record.line),
462
+ ts: this.timestampFor(record),
441
463
  toolUseId: callId,
442
464
  // Codex persists the output body but not FunctionCallOutputPayload.success.
443
465
  // Preserve the raw body and avoid guessing failure from its text.
@@ -446,37 +468,134 @@ function convertCodexRollout(text, expectedSessionId, fallbackName, fallbackCwd)
446
468
  });
447
469
  }
448
470
  }
449
- const name = sanitizeForeignTitle(fallbackName) || sanitizeForeignTitle(firstUserText) || expectedSessionId;
450
- return {
451
- sourceSessionId: expectedSessionId,
452
- metadata: { name, cwd: metaCwd || fallbackCwd },
453
- messages,
454
- };
471
+ finish() {
472
+ if (!this.hasSessionMeta) {
473
+ throw new Error(`Codex rollout Session id mismatch: expected ${this.expectedSessionId}`);
474
+ }
475
+ const name = sanitizeForeignTitle(this.fallbackName) ||
476
+ sanitizeForeignTitle(this.firstUserText) ||
477
+ this.expectedSessionId;
478
+ return {
479
+ sourceSessionId: this.expectedSessionId,
480
+ metadata: { name, cwd: this.metaCwd || this.fallbackCwd },
481
+ messages: this.messages,
482
+ };
483
+ }
484
+ timestampFor(record) {
485
+ const parsed = normalizeEpochMs(record.value.timestamp);
486
+ if (parsed !== undefined)
487
+ this.lastTimestamp = Math.max(this.lastTimestamp, parsed);
488
+ else
489
+ this.lastTimestamp += 1;
490
+ return parsed ?? this.lastTimestamp;
491
+ }
492
+ ensureTurnId(line) {
493
+ this.activeTurnId ??= generatedCodexId(this.expectedSessionId, 'turn', line);
494
+ return this.activeTurnId;
495
+ }
496
+ append(message) {
497
+ if (this.messages.length >= this.limits.maxMessages) {
498
+ throw new Error(`Codex rollout converts to more than ${this.limits.maxMessages} messages`);
499
+ }
500
+ const encodedBytes = Buffer.byteLength(JSON.stringify(message), 'utf8');
501
+ if (encodedBytes > this.limits.maxConvertedBytes - this.convertedBytes) {
502
+ throw new Error(`Codex rollout converts to more than ${this.limits.maxConvertedBytes} bytes`);
503
+ }
504
+ this.convertedBytes += encodedBytes;
505
+ this.messages.push(message);
506
+ }
455
507
  }
456
- function parseRolloutRecords(text, sessionId) {
457
- const endsWithNewline = text.endsWith('\n');
458
- const lines = text.split('\n');
459
- if (endsWithNewline)
460
- lines.pop();
461
- const records = [];
462
- for (let index = 0; index < lines.length; index += 1) {
463
- const line = lines[index];
464
- if (line.trim().length === 0)
465
- continue;
466
- try {
467
- const value = JSON.parse(line);
468
- if (!isRecord(value))
469
- throw new Error('record is not an object');
470
- records.push({ line: index + 1, value });
508
+ async function* readCodexRolloutRecords(path, sessionId, limits) {
509
+ const handle = await open(path, 'r');
510
+ try {
511
+ const metadata = await handle.stat();
512
+ if (!metadata.isFile())
513
+ throw new Error('Codex rollout is not a regular file');
514
+ if (metadata.size > limits.maxRolloutBytes) {
515
+ throw new Error(`Codex rollout exceeds ${limits.maxRolloutBytes} bytes`);
471
516
  }
472
- catch (error) {
473
- if (!endsWithNewline && index === lines.length - 1)
474
- break;
475
- const detail = error instanceof Error ? error.message : String(error);
476
- throw new Error(`Invalid Codex rollout ${sessionId} at line ${index + 1}: ${detail}`);
517
+ const snapshotBytes = metadata.size;
518
+ const pending = [];
519
+ let pendingBytes = 0;
520
+ let observedBytes = 0;
521
+ let line = 0;
522
+ if (snapshotBytes > 0) {
523
+ for await (const value of handle.createReadStream({
524
+ autoClose: false,
525
+ emitClose: false,
526
+ start: 0,
527
+ end: snapshotBytes - 1,
528
+ highWaterMark: CODEX_ROLLOUT_READ_BYTES,
529
+ })) {
530
+ const chunk = Buffer.from(value);
531
+ observedBytes += chunk.byteLength;
532
+ if (observedBytes > snapshotBytes) {
533
+ throw new Error('Codex rollout changed while being read');
534
+ }
535
+ let start = 0;
536
+ for (;;) {
537
+ const newline = chunk.indexOf(0x0a, start);
538
+ if (newline === -1)
539
+ break;
540
+ const segment = chunk.subarray(start, newline);
541
+ assertCodexRecordSize(pendingBytes + segment.byteLength, limits.maxRecordBytes, line + 1);
542
+ line += 1;
543
+ const record = parseCodexRolloutLine(pending.length === 0
544
+ ? segment
545
+ : Buffer.concat([...pending, segment], pendingBytes + segment.byteLength), sessionId, line, false);
546
+ if (record)
547
+ yield record;
548
+ pending.length = 0;
549
+ pendingBytes = 0;
550
+ start = newline + 1;
551
+ }
552
+ if (start < chunk.byteLength) {
553
+ const segment = chunk.subarray(start);
554
+ assertCodexRecordSize(pendingBytes + segment.byteLength, limits.maxRecordBytes, line + 1);
555
+ pending.push(segment);
556
+ pendingBytes += segment.byteLength;
557
+ }
558
+ }
559
+ }
560
+ if (observedBytes !== snapshotBytes)
561
+ throw new Error('Codex rollout changed while being read');
562
+ if (pendingBytes > 0) {
563
+ line += 1;
564
+ const record = parseCodexRolloutLine(pending.length === 1 ? pending[0] : Buffer.concat(pending, pendingBytes), sessionId, line, true);
565
+ if (record)
566
+ yield record;
477
567
  }
478
568
  }
479
- return records;
569
+ finally {
570
+ await handle.close();
571
+ }
572
+ }
573
+ function parseCodexRolloutLine(bytes, sessionId, line, tolerateTornTail) {
574
+ const text = bytes.toString('utf8');
575
+ if (text.trim().length === 0)
576
+ return undefined;
577
+ try {
578
+ const value = JSON.parse(text);
579
+ if (!isRecord(value))
580
+ throw new Error('record is not an object');
581
+ return { line, value };
582
+ }
583
+ catch (error) {
584
+ if (tolerateTornTail)
585
+ return undefined;
586
+ const detail = error instanceof Error ? error.message : String(error);
587
+ throw new Error(`Invalid Codex rollout ${sessionId} at line ${line}: ${detail}`);
588
+ }
589
+ }
590
+ function assertCodexRecordSize(actualBytes, maxBytes, line) {
591
+ if (actualBytes > maxBytes) {
592
+ throw new Error(`Codex rollout record at line ${line} exceeds ${maxBytes} bytes`);
593
+ }
594
+ }
595
+ function assertPositiveSafeInteger(value, label) {
596
+ if (!Number.isSafeInteger(value) || value <= 0) {
597
+ throw new Error(`${label} must be a positive safe integer`);
598
+ }
480
599
  }
481
600
  function catalogEntryFromRolloutHead(text, candidate) {
482
601
  const lines = text.split('\n');
@@ -646,39 +765,6 @@ async function walkRolloutFiles(root, archived) {
646
765
  await visit(root);
647
766
  return files;
648
767
  }
649
- async function readBoundedUtf8File(path, maxBytes) {
650
- const handle = await open(path, 'r');
651
- try {
652
- const metadata = await handle.stat();
653
- if (!metadata.isFile())
654
- throw new Error('Codex rollout is not a regular file');
655
- if (metadata.size > maxBytes)
656
- throw new Error(`Codex rollout exceeds ${maxBytes} bytes`);
657
- const chunks = [];
658
- let total = 0;
659
- for (;;) {
660
- const buffer = Buffer.allocUnsafe(Math.min(64 * 1024, maxBytes + 1 - total));
661
- const { bytesRead } = await handle.read(buffer, 0, buffer.length, total);
662
- if (bytesRead === 0)
663
- break;
664
- total += bytesRead;
665
- if (total > maxBytes)
666
- throw new Error(`Codex rollout exceeds ${maxBytes} bytes`);
667
- chunks.push(buffer.subarray(0, bytesRead));
668
- if (total === maxBytes) {
669
- const probe = Buffer.allocUnsafe(1);
670
- if ((await handle.read(probe, 0, 1, total)).bytesRead > 0) {
671
- throw new Error(`Codex rollout exceeds ${maxBytes} bytes`);
672
- }
673
- break;
674
- }
675
- }
676
- return Buffer.concat(chunks, total).toString('utf8');
677
- }
678
- finally {
679
- await handle.close();
680
- }
681
- }
682
768
  async function readUtf8Prefix(path, maxBytes) {
683
769
  const handle = await open(path, 'r');
684
770
  try {