@fitlab-ai/agent-infra 0.12.1 → 0.12.2

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 (303) hide show
  1. package/README.md +0 -2
  2. package/README.zh-CN.md +0 -2
  3. package/dist/bin/internal-cli.js +2 -17
  4. package/dist/lib/agent-clients/adapter.js +0 -13
  5. package/dist/lib/agent-clients/adapters/antigravity-cli.js +0 -5
  6. package/dist/lib/agent-clients/adapters/claude-code-credentials.js +0 -17
  7. package/dist/lib/agent-clients/adapters/claude-code.js +0 -9
  8. package/dist/lib/agent-clients/adapters/codex.js +0 -52
  9. package/dist/lib/agent-clients/adapters/opencode.js +0 -5
  10. package/dist/lib/agent-clients/adapters/traecli.js +0 -5
  11. package/dist/lib/agent-clients/config.js +19 -54
  12. package/dist/lib/agent-clients/registry.js +1 -12
  13. package/dist/lib/agent-clients/schema.js +0 -18
  14. package/dist/lib/agent-clients/tokens.js +1 -2
  15. package/dist/lib/agent-clients/types.js +0 -1
  16. package/dist/lib/decide.js +2 -0
  17. package/dist/lib/git/worktree-identity.js +1 -9
  18. package/dist/lib/internal/agent-client.js +2 -62
  19. package/dist/lib/internal/cli-route-inventory.js +1 -5
  20. package/dist/lib/internal/manual-validation.js +18 -10
  21. package/dist/lib/internal/task-create.js +1 -1
  22. package/dist/lib/internal/task-event.js +1 -8
  23. package/dist/lib/internal/task-finalization.js +0 -1
  24. package/dist/lib/internal/task-lifecycle.js +2 -2
  25. package/dist/lib/internal/task-operation-registry.js +4 -20
  26. package/dist/lib/internal/task-review.js +4 -4
  27. package/dist/lib/platform/comment-safety.js +1 -7
  28. package/dist/lib/platform/in-label-sync.js +1 -14
  29. package/dist/lib/platform/issue-comments.js +1 -16
  30. package/dist/lib/platform/issues.js +1 -1
  31. package/dist/lib/platform/operation-coordinator.js +4 -2
  32. package/dist/lib/platform/pr-change-report.js +1 -4
  33. package/dist/lib/platform/pr-checks.js +2 -8
  34. package/dist/lib/platform/provider-validation.js +0 -20
  35. package/dist/lib/platform/pull-requests.js +73 -84
  36. package/dist/lib/platform/releases.js +1 -43
  37. package/dist/lib/platform/verification-required.js +0 -2
  38. package/dist/lib/platform/verification-sync.js +1 -79
  39. package/dist/lib/process-data/normalize.js +1 -185
  40. package/dist/lib/process-data/privacy.js +1 -16
  41. package/dist/lib/process-data/sources.js +2 -279
  42. package/dist/lib/render.js +0 -12
  43. package/dist/lib/run/index.js +15 -10
  44. package/dist/lib/run/skills.js +0 -3
  45. package/dist/lib/sandbox/commands/create.js +40 -53
  46. package/dist/lib/sandbox/commands/list-running.js +0 -18
  47. package/dist/lib/sandbox/commands/start.js +7 -8
  48. package/dist/lib/sandbox/constants.js +0 -6
  49. package/dist/lib/sandbox/control/client.js +0 -1
  50. package/dist/lib/sandbox/control/container-identity.js +1 -16
  51. package/dist/lib/sandbox/control/executor.js +0 -4
  52. package/dist/lib/sandbox/control/identity-sentinel.js +0 -7
  53. package/dist/lib/sandbox/control/lifecycle.js +21 -37
  54. package/dist/lib/sandbox/control/native-file-lock.js +0 -3
  55. package/dist/lib/sandbox/control/server.js +18 -38
  56. package/dist/lib/sandbox/control/state.js +1 -9
  57. package/dist/lib/sandbox/dockerfile.js +0 -6
  58. package/dist/lib/sandbox/engine.js +0 -14
  59. package/dist/lib/sandbox/engines/docker-exact-discovery.js +1 -4
  60. package/dist/lib/sandbox/engines/wsl2-paths.js +0 -3
  61. package/dist/lib/sandbox/recovery.js +24 -26
  62. package/dist/lib/sandbox/removal.js +11 -519
  63. package/dist/lib/sandbox/task-resolver.js +0 -41
  64. package/dist/lib/sandbox/tools.js +1 -4
  65. package/dist/lib/sandbox/workspace-view.js +1 -2
  66. package/dist/lib/server/display.js +0 -6
  67. package/dist/lib/task/activity-intent.js +45 -40
  68. package/dist/lib/task/activity-log.js +9 -16
  69. package/dist/lib/task/artifact-command.js +1 -1
  70. package/dist/lib/task/artifact-lifecycle.js +61 -63
  71. package/dist/lib/task/artifact-name.js +0 -6
  72. package/dist/lib/task/artifact-operations.js +3 -3
  73. package/dist/lib/task/capabilities.js +4 -5
  74. package/dist/lib/task/commands/log.js +0 -6
  75. package/dist/lib/task/commands/status.js +17 -60
  76. package/dist/lib/task/commit-operation.js +28 -39
  77. package/dist/lib/task/completion-facts.js +56 -43
  78. package/dist/lib/task/control-authority.js +23 -256
  79. package/dist/lib/task/control-recovery.js +3 -5
  80. package/dist/lib/task/create.js +2 -4
  81. package/dist/lib/task/decision-intents.js +8 -9
  82. package/dist/lib/task/delivery-target.js +1 -40
  83. package/dist/lib/task/events.js +84 -107
  84. package/dist/lib/task/finalization.js +44 -8
  85. package/dist/lib/task/ledger-intents.js +1 -1
  86. package/dist/lib/task/lifecycle-execution.js +38 -33
  87. package/dist/lib/task/lifecycle.js +49 -41
  88. package/dist/lib/task/local-artifact-finalization.js +1 -4
  89. package/dist/lib/task/manual-validation-evidence.js +1 -37
  90. package/dist/lib/task/platform-operation-recovery.js +67 -24
  91. package/dist/lib/task/pr-delivery-fact.js +2 -5
  92. package/dist/lib/task/qualification-confirmation.js +5 -7
  93. package/dist/lib/task/resolve-ref.js +1 -25
  94. package/dist/lib/task/review-command.js +2 -2
  95. package/dist/lib/task/review-finalization.js +1 -21
  96. package/dist/lib/task/review-fingerprint.js +0 -4
  97. package/dist/lib/task/secure-fs.js +0 -56
  98. package/dist/lib/task/short-id.js +1 -39
  99. package/dist/lib/task/skill-scope.js +0 -1
  100. package/dist/lib/task/verification-engine.js +58 -282
  101. package/dist/lib/task/verification.js +6 -3
  102. package/dist/lib/task/write.js +11 -1
  103. package/lib/agent-clients/adapter.ts +0 -76
  104. package/lib/agent-clients/adapters/antigravity-cli.ts +0 -5
  105. package/lib/agent-clients/adapters/claude-code-credentials.ts +0 -19
  106. package/lib/agent-clients/adapters/claude-code.ts +0 -9
  107. package/lib/agent-clients/adapters/codex.ts +0 -56
  108. package/lib/agent-clients/adapters/opencode.ts +0 -5
  109. package/lib/agent-clients/adapters/traecli.ts +0 -5
  110. package/lib/agent-clients/config.ts +20 -61
  111. package/lib/agent-clients/registry.ts +1 -34
  112. package/lib/agent-clients/schema.ts +0 -18
  113. package/lib/agent-clients/tokens.ts +1 -2
  114. package/lib/agent-clients/types.ts +0 -15
  115. package/lib/decide.ts +1 -0
  116. package/lib/git/worktree-identity.ts +0 -10
  117. package/lib/internal/agent-client.ts +2 -70
  118. package/lib/internal/cli-route-inventory.ts +1 -4
  119. package/lib/internal/manual-validation.ts +16 -9
  120. package/lib/internal/task-create.ts +0 -1
  121. package/lib/internal/task-event.ts +1 -5
  122. package/lib/internal/task-finalization.ts +0 -1
  123. package/lib/internal/task-lifecycle.ts +3 -5
  124. package/lib/internal/task-operation-registry.ts +3 -19
  125. package/lib/internal/task-review.ts +4 -4
  126. package/lib/platform/comment-safety.ts +0 -10
  127. package/lib/platform/github-provider.ts +0 -2
  128. package/lib/platform/in-label-sync.ts +0 -16
  129. package/lib/platform/issue-comments.ts +0 -21
  130. package/lib/platform/issues.ts +0 -2
  131. package/lib/platform/operation-coordinator.ts +4 -2
  132. package/lib/platform/pr-change-report.ts +0 -5
  133. package/lib/platform/pr-checks.ts +1 -10
  134. package/lib/platform/pr-review.ts +1 -1
  135. package/lib/platform/provider-bridge.ts +0 -1
  136. package/lib/platform/provider-loader.ts +1 -5
  137. package/lib/platform/provider-validation.ts +1 -49
  138. package/lib/platform/pull-requests.ts +64 -99
  139. package/lib/platform/releases.ts +1 -40
  140. package/lib/platform/verification-required.ts +0 -2
  141. package/lib/platform/verification-sync.ts +0 -94
  142. package/lib/process-data/normalize.ts +1 -186
  143. package/lib/process-data/privacy.ts +2 -19
  144. package/lib/process-data/sources.ts +3 -283
  145. package/lib/render.ts +0 -15
  146. package/lib/run/index.ts +13 -7
  147. package/lib/run/skills.ts +0 -4
  148. package/lib/sandbox/commands/create.ts +37 -74
  149. package/lib/sandbox/commands/list-running.ts +0 -19
  150. package/lib/sandbox/commands/start.ts +7 -10
  151. package/lib/sandbox/constants.ts +0 -8
  152. package/lib/sandbox/control/client.ts +0 -1
  153. package/lib/sandbox/control/container-identity.ts +1 -16
  154. package/lib/sandbox/control/executor.ts +1 -5
  155. package/lib/sandbox/control/identity-sentinel.ts +0 -8
  156. package/lib/sandbox/control/lifecycle.ts +18 -51
  157. package/lib/sandbox/control/native-file-lock.ts +0 -8
  158. package/lib/sandbox/control/protocol.ts +0 -2
  159. package/lib/sandbox/control/server.ts +13 -39
  160. package/lib/sandbox/control/state.ts +1 -10
  161. package/lib/sandbox/dockerfile.ts +0 -11
  162. package/lib/sandbox/engine.ts +0 -17
  163. package/lib/sandbox/engines/docker-exact-discovery.ts +1 -5
  164. package/lib/sandbox/engines/wsl2-paths.ts +0 -4
  165. package/lib/sandbox/recovery.ts +24 -26
  166. package/lib/sandbox/removal.ts +11 -696
  167. package/lib/sandbox/shell.ts +1 -1
  168. package/lib/sandbox/task-resolver.ts +0 -48
  169. package/lib/sandbox/tools.ts +1 -5
  170. package/lib/sandbox/workspace-view.ts +2 -4
  171. package/lib/server/display.ts +0 -14
  172. package/lib/server/process-control.ts +1 -1
  173. package/lib/task/activity-intent.ts +36 -44
  174. package/lib/task/activity-log.ts +9 -22
  175. package/lib/task/artifact-command.ts +1 -1
  176. package/lib/task/artifact-lifecycle.ts +58 -67
  177. package/lib/task/artifact-name.ts +0 -7
  178. package/lib/task/artifact-operations.ts +3 -3
  179. package/lib/task/capabilities.ts +5 -6
  180. package/lib/task/commands/log.ts +0 -7
  181. package/lib/task/commands/status.ts +14 -79
  182. package/lib/task/commit-operation.ts +29 -51
  183. package/lib/task/completion-facts.ts +45 -42
  184. package/lib/task/control-authority.ts +27 -317
  185. package/lib/task/control-recovery.ts +2 -5
  186. package/lib/task/create.ts +2 -5
  187. package/lib/task/decision-intents.ts +5 -5
  188. package/lib/task/delivery-target.ts +0 -40
  189. package/lib/task/events.ts +89 -123
  190. package/lib/task/finalization.ts +41 -9
  191. package/lib/task/ledger-intents.ts +1 -1
  192. package/lib/task/lifecycle-execution.ts +37 -89
  193. package/lib/task/lifecycle.ts +48 -44
  194. package/lib/task/local-artifact-finalization.ts +0 -7
  195. package/lib/task/manual-validation-evidence.ts +1 -47
  196. package/lib/task/platform-operation-recovery.ts +50 -26
  197. package/lib/task/pr-delivery-fact.ts +2 -11
  198. package/lib/task/qualification-confirmation.ts +6 -6
  199. package/lib/task/resolve-ref.ts +0 -23
  200. package/lib/task/review-command.ts +2 -2
  201. package/lib/task/review-finalization.ts +1 -27
  202. package/lib/task/review-fingerprint.ts +0 -8
  203. package/lib/task/secure-fs.ts +0 -42
  204. package/lib/task/short-id.ts +0 -43
  205. package/lib/task/skill-scope.ts +0 -1
  206. package/lib/task/verification-engine.ts +56 -287
  207. package/lib/task/verification.ts +5 -5
  208. package/lib/task/write.ts +12 -1
  209. package/package.json +2 -2
  210. package/templates/.agents/README.en.md +1 -3
  211. package/templates/.agents/README.zh-CN.md +0 -2
  212. package/templates/.agents/rules/README.en.md +0 -1
  213. package/templates/.agents/rules/README.zh-CN.md +0 -1
  214. package/templates/.agents/rules/commit-and-pr.en.md +1 -1
  215. package/templates/.agents/rules/commit-and-pr.zh-CN.md +1 -1
  216. package/templates/.agents/rules/no-mid-flow-questions.en.md +0 -10
  217. package/templates/.agents/rules/no-mid-flow-questions.zh-CN.md +0 -10
  218. package/templates/.agents/rules/pr-sync.en.md +1 -1
  219. package/templates/.agents/rules/pr-sync.zh-CN.md +1 -1
  220. package/templates/.agents/rules/task-management.en.md +3 -3
  221. package/templates/.agents/rules/task-management.zh-CN.md +3 -3
  222. package/templates/.agents/skills/analyze-task/SKILL.en.md +3 -3
  223. package/templates/.agents/skills/analyze-task/SKILL.zh-CN.md +3 -3
  224. package/templates/.agents/skills/cancel-task/SKILL.en.md +2 -2
  225. package/templates/.agents/skills/cancel-task/SKILL.zh-CN.md +2 -2
  226. package/templates/.agents/skills/code-task/SKILL.en.md +2 -2
  227. package/templates/.agents/skills/code-task/SKILL.zh-CN.md +4 -4
  228. package/templates/.agents/skills/commit/SKILL.en.md +2 -2
  229. package/templates/.agents/skills/commit/SKILL.zh-CN.md +2 -2
  230. package/templates/.agents/skills/commit/reference/commit-orchestration.en.md +6 -6
  231. package/templates/.agents/skills/commit/reference/commit-orchestration.zh-CN.md +6 -6
  232. package/templates/.agents/skills/plan-task/SKILL.en.md +2 -2
  233. package/templates/.agents/skills/plan-task/SKILL.zh-CN.md +2 -2
  234. package/templates/.agents/skills/review-analysis/SKILL.en.md +3 -3
  235. package/templates/.agents/skills/review-analysis/SKILL.zh-CN.md +3 -3
  236. package/templates/.agents/skills/review-code/SKILL.en.md +5 -5
  237. package/templates/.agents/skills/review-code/SKILL.zh-CN.md +5 -5
  238. package/templates/.agents/skills/review-plan/SKILL.en.md +3 -3
  239. package/templates/.agents/skills/review-plan/SKILL.zh-CN.md +3 -3
  240. package/templates/.agents/skills/review-pr/SKILL.en.md +4 -4
  241. package/templates/.agents/skills/review-pr/SKILL.zh-CN.md +4 -4
  242. package/templates/.agents/skills/run-manual-validation/SKILL.en.md +1 -1
  243. package/templates/.agents/skills/run-manual-validation/SKILL.zh-CN.md +1 -1
  244. package/templates/.agents/skills/update-agent-infra/scripts/sync-templates.js +3 -75
  245. package/templates/.agents/templates/task.en.md +1 -0
  246. package/templates/.agents/templates/task.zh-CN.md +1 -0
  247. package/templates/.claude/settings.json +0 -24
  248. package/templates/.codex/hooks.json +0 -50
  249. package/dist/lib/agent-clients/adapters/codex-lifecycle/app-server.js +0 -582
  250. package/dist/lib/agent-clients/adapters/codex-lifecycle/binding.js +0 -114
  251. package/dist/lib/agent-clients/adapters/codex-lifecycle/build-identity.js +0 -237
  252. package/dist/lib/agent-clients/adapters/codex-lifecycle/cli.js +0 -443
  253. package/dist/lib/agent-clients/adapters/codex-lifecycle/evidence.js +0 -236
  254. package/dist/lib/agent-clients/adapters/codex-lifecycle/recovery.js +0 -292
  255. package/dist/lib/agent-clients/adapters/codex-lifecycle/store.js +0 -267
  256. package/dist/lib/agent-clients/adapters/codex-orchestration.js +0 -466
  257. package/dist/lib/internal/task-orchestration.js +0 -71
  258. package/dist/lib/platform/verification.js +0 -6
  259. package/dist/lib/run/prompt.js +0 -2
  260. package/dist/lib/sandbox/engines/authority.js +0 -289
  261. package/dist/lib/task/delegation-receipts.js +0 -410
  262. package/dist/lib/task/orchestration.js +0 -1233
  263. package/dist/lib/task/verification-checks.js +0 -9
  264. package/dist/lib/task/workspace-snapshot.js +0 -121
  265. package/dist/lifecycle-build-manifest.json +0 -6
  266. package/lib/agent-clients/adapters/codex-lifecycle/app-server.ts +0 -688
  267. package/lib/agent-clients/adapters/codex-lifecycle/binding.ts +0 -147
  268. package/lib/agent-clients/adapters/codex-lifecycle/build-identity.ts +0 -349
  269. package/lib/agent-clients/adapters/codex-lifecycle/cli.ts +0 -506
  270. package/lib/agent-clients/adapters/codex-lifecycle/evidence.ts +0 -385
  271. package/lib/agent-clients/adapters/codex-lifecycle/manifest-files.json +0 -41
  272. package/lib/agent-clients/adapters/codex-lifecycle/recovery.ts +0 -418
  273. package/lib/agent-clients/adapters/codex-lifecycle/store.ts +0 -354
  274. package/lib/agent-clients/adapters/codex-orchestration.ts +0 -630
  275. package/lib/internal/task-orchestration.ts +0 -74
  276. package/lib/platform/verification.ts +0 -11
  277. package/lib/run/prompt.ts +0 -1
  278. package/lib/sandbox/engines/authority.ts +0 -365
  279. package/lib/task/delegation-receipts.ts +0 -547
  280. package/lib/task/orchestration.ts +0 -1651
  281. package/lib/task/verification-checks.ts +0 -10
  282. package/lib/task/workspace-snapshot.ts +0 -144
  283. package/templates/.agents/hooks/lifecycle-delegation.js +0 -140
  284. package/templates/.agents/rules/lifecycle-orchestration.en.md +0 -44
  285. package/templates/.agents/rules/lifecycle-orchestration.zh-CN.md +0 -44
  286. package/templates/.agents/skills/run-task/SKILL.en.md +0 -29
  287. package/templates/.agents/skills/run-task/SKILL.zh-CN.md +0 -29
  288. package/templates/.agents/skills/run-task/config/verify.en.json +0 -10
  289. package/templates/.agents/skills/run-task/config/verify.zh-CN.json +0 -10
  290. package/templates/.agents/skills/run-task/reference/host-validation.en.md +0 -24
  291. package/templates/.agents/skills/run-task/reference/host-validation.zh-CN.md +0 -24
  292. package/templates/.claude/agents/agent-infra-lifecycle-executor.en.md +0 -6
  293. package/templates/.claude/agents/agent-infra-lifecycle-executor.zh-CN.md +0 -6
  294. package/templates/.claude/agents/agent-infra-lifecycle-reviewer.en.md +0 -6
  295. package/templates/.claude/agents/agent-infra-lifecycle-reviewer.zh-CN.md +0 -6
  296. package/templates/.claude/commands/run-task.en.md +0 -8
  297. package/templates/.claude/commands/run-task.zh-CN.md +0 -8
  298. package/templates/.codex/agents/agent-infra-lifecycle-executor.en.toml +0 -3
  299. package/templates/.codex/agents/agent-infra-lifecycle-executor.zh-CN.toml +0 -3
  300. package/templates/.codex/agents/agent-infra-lifecycle-reviewer.en.toml +0 -3
  301. package/templates/.codex/agents/agent-infra-lifecycle-reviewer.zh-CN.toml +0 -3
  302. package/templates/.opencode/commands/run-task.en.md +0 -11
  303. package/templates/.opencode/commands/run-task.zh-CN.md +0 -11
@@ -1,443 +0,0 @@
1
- import { spawnSync } from 'node:child_process';
2
- import crypto from 'node:crypto';
3
- import fs from 'node:fs';
4
- import path from 'node:path';
5
- import { preflightCodexLifecycleEvidence, resolveCodexTerminal, resolveCodexThread } from './app-server.js';
6
- import { createCodexLifecycleStore } from './store.js';
7
- import { resolveTaskRef } from '../../../task/resolve-ref.js';
8
- import { resolveTaskContext } from '../../../task/resolve-ref.js';
9
- import { hasSealableOrchestrationDelegation, readRun } from '../../../task/orchestration.js';
10
- import { parseCodexLifecycleBinding, verifyCodexLifecycleTaskBinding } from './binding.js';
11
- import { activateCodexOrchestrationDelegation, activateCodexSpawnDelegation, reconcileCodexOrchestrationDelegation, sealCodexOrchestrationDelegation, sealCodexParentDelegation } from '../codex-orchestration.js';
12
- import { ensureInternalHandlerRoute, internalHandlerRoute } from '../../../internal/cli-route-inventory.js';
13
- const USAGE = 'Usage: agent-infra-internal codex-lifecycle <hook-event|resolve-start|resolve-stop|preflight|consume> [options]\n';
14
- const MANAGED_AGENT = /^agent-infra-lifecycle-(executor|reviewer)$/;
15
- function output(value) {
16
- process.stdout.write(`${JSON.stringify(value)}\n`);
17
- }
18
- function failure(code, message) {
19
- output({ status: 'failed', changed: false, evidence: null, diagnostics: [], error: { code, message } });
20
- process.exitCode = 1;
21
- }
22
- function outputBridgeResult(result) {
23
- if (result.error?.code === 'ORCHESTRATION_DELEGATION_MISSING') {
24
- output({ status: 'ignored', changed: false, evidence: null, diagnostics: [], error: null });
25
- return;
26
- }
27
- output(result);
28
- if (result.status !== 'running')
29
- process.exitCode = 1;
30
- }
31
- function parse(args) {
32
- if (args[0] === '--help' || args[0] === '-h') {
33
- process.stdout.write(USAGE);
34
- return null;
35
- }
36
- const operation = args[0];
37
- if (!operation) {
38
- failure('CODEX_LIFECYCLE_PAYLOAD_INVALID', 'operation is required');
39
- return null;
40
- }
41
- const allowed = {
42
- 'hook-event': ['--event', '--bridge'],
43
- 'resolve-start': ['--child-id'],
44
- 'resolve-stop': ['--child-id'],
45
- preflight: ['--format', '--session-id', '--turn-id', '--tool-use-id'],
46
- consume: ['--child-id', '--consumer']
47
- }[operation];
48
- if (!allowed) {
49
- failure('CODEX_LIFECYCLE_PAYLOAD_INVALID', 'unknown operation');
50
- return null;
51
- }
52
- const values = {};
53
- for (let index = 1; index < args.length; index += 1) {
54
- const flag = args[index];
55
- if (!allowed.includes(flag) || Object.hasOwn(values, flag)) {
56
- failure('CODEX_LIFECYCLE_PAYLOAD_INVALID', `invalid or duplicate option '${flag}'`);
57
- return null;
58
- }
59
- const value = args[++index];
60
- if (!value || value.startsWith('--')) {
61
- failure('CODEX_LIFECYCLE_PAYLOAD_INVALID', `option '${flag}' requires a value`);
62
- return null;
63
- }
64
- values[flag] = value;
65
- }
66
- return Object.freeze({ operation, values: Object.freeze(values) });
67
- }
68
- function cliVersion() {
69
- const result = spawnSync('codex', ['--version'], { encoding: 'utf8' });
70
- const version = /codex-cli\s+(\d+\.\d+\.\d+)/.exec(result.stdout ?? '')?.[1];
71
- if (result.status !== 0 || !version)
72
- throw new Error('Codex CLI version is unavailable');
73
- return version;
74
- }
75
- function hookDefinitionHash() {
76
- const file = path.join(process.cwd(), '.codex', 'hooks.json');
77
- return crypto.createHash('sha256').update(fs.readFileSync(file)).digest('hex');
78
- }
79
- function resolveLifecycleTaskContext() {
80
- const taskId = process.env.AGENT_INFRA_TASK_ID;
81
- const context = taskId
82
- ? resolveTaskRef(taskId, { repoRoot: process.cwd() })
83
- : resolveTaskContext(undefined, { repoRoot: process.cwd() });
84
- if (!context.ok)
85
- throw new Error(`${context.code}: ${context.message}`);
86
- return context;
87
- }
88
- function verifyPendingBinding(binding, nativeAgent, expected = {}) {
89
- const resolved = resolveLifecycleTaskContext();
90
- const run = readRun(resolved.taskDir);
91
- if (!run)
92
- throw new Error('Codex lifecycle task binding has no current orchestration run');
93
- verifyCodexLifecycleTaskBinding(binding, run, nativeAgent, expected);
94
- return { resolved, run };
95
- }
96
- function verifyStoredBinding(taskId, binding, nativeAgent) {
97
- const resolved = resolveTaskRef(taskId, { repoRoot: process.cwd() });
98
- if (!resolved.ok)
99
- throw new Error(`${resolved.code}: ${resolved.message}`);
100
- const run = readRun(resolved.taskDir);
101
- const receipt = run?.pendingDelegation;
102
- if (!receipt || run?.status !== 'running'
103
- || receipt.id !== binding.receiptId
104
- || receipt.runId !== binding.runId
105
- || receipt.client !== 'codex'
106
- || receipt.role !== nativeRole(nativeAgent)) {
107
- throw new Error('CODEX_LIFECYCLE_TASK_BINDING_MISMATCH: stored task receipt is not the current pending lifecycle event');
108
- }
109
- }
110
- function sameTaskBinding(left, right) {
111
- return left?.runId === right.runId && left.receiptId === right.receiptId;
112
- }
113
- function nativeRole(nativeAgent) {
114
- const match = /^agent-infra-lifecycle-(executor|reviewer)$/u.exec(nativeAgent);
115
- return match?.[1] ?? null;
116
- }
117
- function taskScopedStore(taskId) {
118
- return createCodexLifecycleStore({
119
- repoRoot: process.cwd(),
120
- taskId,
121
- cliVersion: cliVersion()
122
- });
123
- }
124
- async function readStdin() {
125
- let input = '';
126
- process.stdin.setEncoding('utf8');
127
- for await (const chunk of process.stdin) {
128
- input += chunk;
129
- if (Buffer.byteLength(input) > 64 * 1024)
130
- throw new Error('Codex lifecycle input exceeds 64 KiB');
131
- }
132
- return JSON.parse(input || '{}');
133
- }
134
- function recordFromPayload(phase, payload, taskBinding) {
135
- if (!payload || typeof payload !== 'object' || Array.isArray(payload)) {
136
- throw new Error('Codex lifecycle hook payload must be an object');
137
- }
138
- const value = payload;
139
- const text = (key) => typeof value[key] === 'string' ? value[key] : '';
140
- if (!MANAGED_AGENT.test(text('nativeAgent')))
141
- return null;
142
- if (phase === 'pre-tool')
143
- return {
144
- type: 'hook-spawn',
145
- sessionId: text('sessionId'),
146
- turnId: text('turnId'),
147
- toolUseId: text('toolUseId'),
148
- nativeAgent: text('nativeAgent'),
149
- ...(text('requestedModel') ? { requestedModel: text('requestedModel') } : {}),
150
- ...(text('requestedReasoningEffort') ? { requestedReasoningEffort: text('requestedReasoningEffort') } : {}),
151
- hookDefinitionHash: text('hookDefinitionHash'),
152
- ...(taskBinding ? { taskBinding } : {})
153
- };
154
- if (phase === 'subagent-start')
155
- return {
156
- type: 'hook-child',
157
- sessionId: text('sessionId'),
158
- turnId: text('turnId'),
159
- childThreadId: text('childThreadId'),
160
- nativeAgent: text('nativeAgent'),
161
- source: 'hook'
162
- };
163
- if (phase === 'subagent-stop')
164
- return {
165
- type: 'hook-stop',
166
- sessionId: text('sessionId'),
167
- turnId: text('turnId'),
168
- childThreadId: text('childThreadId'),
169
- nativeAgent: text('nativeAgent')
170
- };
171
- if (phase === 'post-tool')
172
- return null;
173
- throw new Error(`unknown Codex lifecycle hook phase '${phase}'`);
174
- }
175
- function payloadText(payload, key) {
176
- return payload && typeof payload === 'object' && !Array.isArray(payload)
177
- && typeof payload[key] === 'string'
178
- ? payload[key]
179
- : '';
180
- }
181
- function resolveHookTaskContext(phase, toolName, payload) {
182
- const spawnName = payloadText(payload, 'taskName');
183
- const needsSpawnBinding = phase === 'pre-tool' || (phase === 'post-tool' && toolName === 'collaborationspawn_agent');
184
- const spawnBinding = needsSpawnBinding ? parseCodexLifecycleBinding(spawnName) : null;
185
- if (needsSpawnBinding && !spawnBinding) {
186
- throw new Error('CODEX_LIFECYCLE_TASK_BINDING_MISSING: task_name must carry the prepared task binding');
187
- }
188
- const nativeAgent = payloadText(payload, 'nativeAgent');
189
- const taskContext = spawnBinding
190
- ? verifyPendingBinding(spawnBinding.binding, nativeAgent, {
191
- requestedModel: payloadText(payload, 'requestedModel') || undefined,
192
- requestedReasoningEffort: payloadText(payload, 'requestedReasoningEffort') || undefined
193
- }).resolved
194
- : resolveLifecycleTaskContext();
195
- const store = taskScopedStore(taskContext.taskId);
196
- if (spawnBinding)
197
- verifyStoredBinding(taskContext.taskId, spawnBinding.binding, nativeAgent);
198
- return { spawnBinding, nativeAgent, taskContext, store };
199
- }
200
- function currentHookTaskBinding(taskDir, taskId, spawnBinding, nativeAgent) {
201
- if (!spawnBinding) {
202
- const run = readRun(taskDir);
203
- const receipt = run?.pendingDelegation;
204
- if (!receipt || receipt.client !== 'codex' || receipt.role !== nativeRole(nativeAgent)) {
205
- throw new Error('CODEX_LIFECYCLE_TASK_BINDING_MISMATCH: no matching pending task receipt');
206
- }
207
- verifyStoredBinding(taskId, { taskId: receipt.taskId, runId: receipt.runId, receiptId: receipt.id }, nativeAgent);
208
- }
209
- const receipt = readRun(taskDir)?.pendingDelegation;
210
- return receipt && { taskId: receipt.taskId, runId: receipt.runId, receiptId: receipt.id };
211
- }
212
- function validateHookStopIdentity(store, event, currentBinding) {
213
- const record = store.read(event.childThreadId);
214
- const child = record.state.child;
215
- if (!currentBinding || !sameTaskBinding(record.taskBinding, currentBinding)
216
- || !child
217
- || child.sessionId !== event.sessionId
218
- || child.turnId !== event.turnId
219
- || child.childThreadId !== event.childThreadId
220
- || child.nativeAgent !== event.nativeAgent) {
221
- throw new Error('CODEX_LIFECYCLE_TASK_BINDING_MISMATCH: stop event does not match the current task receipt and child');
222
- }
223
- }
224
- function matchingHookChildSpawns(store, binding, event) {
225
- return binding ? store.findByTaskBinding(binding).filter((record) => record.state.spawn?.sessionId === event.sessionId
226
- && record.state.spawn?.nativeAgent === event.nativeAgent) : [];
227
- }
228
- async function applyHookChildEvent(event, currentBinding, taskContext, store, bridge, resolved) {
229
- const candidates = matchingHookChildSpawns(store, currentBinding, event);
230
- if (candidates.length !== 1) {
231
- throw new Error('CODEX_LIFECYCLE_TASK_BINDING_MISMATCH: child event has no unique spawn for the current task receipt');
232
- }
233
- const childResolution = resolved ?? await resolveCodexThread(event.childThreadId);
234
- verifyStoredBinding(taskContext.taskId, currentBinding, event.nativeAgent);
235
- const latestCandidates = matchingHookChildSpawns(store, currentBinding, event);
236
- if (latestCandidates.length !== 1 || latestCandidates[0].revision !== candidates[0].revision) {
237
- throw new Error('CODEX_LIFECYCLE_TASK_BINDING_MISMATCH: task receipt spawn became ambiguous during child resolution');
238
- }
239
- if (childResolution.resolution.thread.childThreadId !== event.childThreadId
240
- || childResolution.resolution.thread.parentThreadId !== event.sessionId
241
- || childResolution.resolution.thread.nativeAgent !== event.nativeAgent) {
242
- throw new Error('CODEX_LIFECYCLE_TASK_BINDING_MISMATCH: resolved child does not match the current spawn');
243
- }
244
- verifyStoredBinding(taskContext.taskId, currentBinding, event.nativeAgent);
245
- const spawn = latestCandidates[0].state.spawn;
246
- if (!spawn)
247
- throw new Error('CODEX_LIFECYCLE_TASK_BINDING_MISMATCH: current spawn record is missing');
248
- const result = store.applyToSpawn({
249
- sessionId: spawn.sessionId, turnId: spawn.turnId, toolUseId: spawn.toolUseId, taskBinding: currentBinding
250
- }, { ...event, parentThreadId: childResolution.resolution.thread.parentThreadId }, latestCandidates[0].revision);
251
- if (bridge) {
252
- outputBridgeResult(await activateCodexOrchestrationDelegation(event.childThreadId, {
253
- store, orchestrationOptions: { repoRoot: process.cwd(), taskId: taskContext.taskId },
254
- resolveThread: async () => childResolution
255
- }));
256
- return;
257
- }
258
- output({ status: result.state.status, changed: true, evidence: result.state, diagnostics: childResolution.diagnostics, error: result.state.error });
259
- if (result.state.status === 'invalid')
260
- process.exitCode = 1;
261
- }
262
- async function handleBridgePostTool(toolName, payload, nativeAgent, taskId, store) {
263
- const orchestrationOptions = { repoRoot: process.cwd(), taskId };
264
- if (toolName === 'collaborationspawn_agent') {
265
- if (!MANAGED_AGENT.test(nativeAgent)) {
266
- output({ status: 'ignored', changed: false, evidence: null, diagnostics: [], error: null });
267
- return;
268
- }
269
- const requestedModel = payloadText(payload, 'requestedModel');
270
- const requestedReasoningEffort = payloadText(payload, 'requestedReasoningEffort');
271
- const bridged = await activateCodexSpawnDelegation({
272
- sessionId: payloadText(payload, 'sessionId'), turnId: payloadText(payload, 'turnId'),
273
- toolUseId: payloadText(payload, 'toolUseId'), transcriptPath: payloadText(payload, 'transcriptPath'),
274
- nativeAgent, taskName: payloadText(payload, 'taskName'),
275
- ...(requestedModel ? { requestedModel } : {}),
276
- ...(requestedReasoningEffort ? { requestedReasoningEffort } : {})
277
- }, { store, orchestrationOptions });
278
- outputBridgeResult(bridged);
279
- return;
280
- }
281
- if (toolName === 'collaborationwait_agent') {
282
- outputBridgeResult(await sealCodexParentDelegation(payloadText(payload, 'sessionId'), { store, orchestrationOptions }));
283
- return;
284
- }
285
- output({ status: 'ignored', changed: false, evidence: null, diagnostics: [], error: null });
286
- }
287
- async function sealConsumedHookStop(event, store, taskId) {
288
- const result = await sealCodexOrchestrationDelegation(event.childThreadId, {
289
- store, orchestrationOptions: { repoRoot: process.cwd(), taskId }
290
- });
291
- outputBridgeResult(result);
292
- }
293
- function handleMissingHookEvent(phase, bridge, payload, taskId) {
294
- if (phase === 'post-tool' && bridge) {
295
- const value = payload;
296
- outputBridgeResult(reconcileCodexOrchestrationDelegation(String(value.childThreadId ?? ''), {
297
- orchestrationOptions: { repoRoot: process.cwd(), taskId }
298
- }));
299
- return;
300
- }
301
- output({ status: 'ignored', changed: false, evidence: null, diagnostics: [], error: null });
302
- }
303
- async function handlePreflight(parsed) {
304
- const format = parsed.values['--format'] ?? 'json';
305
- if (!['text', 'json'].includes(format))
306
- throw new Error("format must be 'text' or 'json'");
307
- const runtimeFields = [parsed.values['--session-id'], parsed.values['--turn-id'], parsed.values['--tool-use-id']];
308
- if (runtimeFields.some(Boolean) && !runtimeFields.every(Boolean)) {
309
- throw new Error('preflight runtime identity requires --session-id, --turn-id, and --tool-use-id together');
310
- }
311
- const runtimeIdentity = runtimeFields.every(Boolean)
312
- ? { sessionId: runtimeFields[0], turnId: runtimeFields[1], toolUseId: runtimeFields[2] }
313
- : undefined;
314
- const result = await preflightCodexLifecycleEvidence(process.cwd(), runtimeIdentity);
315
- if (format === 'text') {
316
- process.stdout.write(`Codex lifecycle static preflight: ready\nCLI: ${result.cliVersion}\nRuntime hook liveness: ${result.runtimeLiveness ? 'observed' : 'not-yet-observed'}\n`);
317
- }
318
- else
319
- output({ status: 'ready', changed: false, evidence: result, diagnostics: result.diagnostics, error: null });
320
- }
321
- async function codexLifecycle(args = []) {
322
- if (!ensureInternalHandlerRoute('codex-lifecycle', args))
323
- return;
324
- const parsed = parse(args);
325
- if (!parsed || process.exitCode)
326
- return;
327
- try {
328
- if (internalHandlerRoute('codex-lifecycle', 'preflight', parsed.operation)) {
329
- await handlePreflight(parsed);
330
- return;
331
- }
332
- if (internalHandlerRoute('codex-lifecycle', 'hook-event', parsed.operation)) {
333
- const phase = parsed.values['--event'];
334
- if (!phase || !['pre-tool', 'subagent-start', 'post-tool', 'subagent-stop'].includes(phase)) {
335
- throw new Error('hook-event requires a known --event');
336
- }
337
- if (parsed.values['--bridge'] !== undefined && parsed.values['--bridge'] !== 'true') {
338
- throw new Error("hook-event --bridge must be 'true'");
339
- }
340
- const payload = await readStdin();
341
- const toolName = payloadText(payload, 'toolName');
342
- const hookContext = resolveHookTaskContext(phase, toolName, payload);
343
- const { spawnBinding, nativeAgent, taskContext, store } = hookContext;
344
- if (phase === 'post-tool' && parsed.values['--bridge'] === 'true') {
345
- await handleBridgePostTool(toolName, payload, nativeAgent, taskContext.taskId, store);
346
- return;
347
- }
348
- const event = recordFromPayload(phase, payload, spawnBinding?.binding);
349
- if (!event) {
350
- handleMissingHookEvent(phase, parsed.values['--bridge'] === 'true', payload, taskContext.taskId);
351
- return;
352
- }
353
- const currentBinding = currentHookTaskBinding(taskContext.taskDir, taskContext.taskId, spawnBinding, nativeAgent);
354
- if (event.type === 'hook-stop') {
355
- validateHookStopIdentity(store, event, currentBinding);
356
- }
357
- if (parsed.values['--bridge'] === 'true' && event.type === 'hook-stop'
358
- && store.read(event.childThreadId).consumer) {
359
- await sealConsumedHookStop(event, store, taskContext.taskId);
360
- return;
361
- }
362
- if (event.type === 'hook-child') {
363
- await applyHookChildEvent(event, currentBinding, taskContext, store, parsed.values['--bridge'] === 'true');
364
- return;
365
- }
366
- if (!currentBinding)
367
- throw new Error('CODEX_LIFECYCLE_TASK_BINDING_MISMATCH: current task receipt is missing');
368
- verifyStoredBinding(taskContext.taskId, currentBinding, nativeAgent);
369
- const result = store.apply(event);
370
- if (parsed.values['--bridge'] === 'true' && event.type === 'hook-stop') {
371
- if (!hasSealableOrchestrationDelegation('codex', event.childThreadId, { repoRoot: process.cwd(), taskId: taskContext.taskId })) {
372
- output({ status: 'ignored', changed: false, evidence: null, diagnostics: [], error: null });
373
- return;
374
- }
375
- output({
376
- status: result.state.status,
377
- changed: true,
378
- evidence: result.state,
379
- diagnostics: [],
380
- error: result.state.error
381
- });
382
- return;
383
- }
384
- output({ status: result.state.status, changed: true, evidence: result.state, diagnostics: [], error: result.state.error });
385
- if (result.state.status === 'invalid')
386
- process.exitCode = 1;
387
- return;
388
- }
389
- const childThreadId = parsed.values['--child-id'];
390
- if (!childThreadId)
391
- throw new Error(`operation '${parsed.operation}' requires --child-id`);
392
- const taskContext = resolveLifecycleTaskContext();
393
- const store = taskScopedStore(taskContext.taskId);
394
- const existing = store.read(childThreadId);
395
- if (!existing.taskBinding)
396
- throw new Error('CODEX_LIFECYCLE_TASK_BINDING_MISSING: stored lifecycle identity has no task binding');
397
- verifyStoredBinding(taskContext.taskId, existing.taskBinding, existing.state.startEvidence?.nativeAgent ?? existing.state.spawn?.nativeAgent ?? '');
398
- if (internalHandlerRoute('codex-lifecycle', 'resolve-start', parsed.operation)) {
399
- if (store.read(childThreadId).state.spawn?.hookDefinitionHash !== hookDefinitionHash()) {
400
- throw new Error('Codex lifecycle hook definition hash is stale');
401
- }
402
- const resolved = await resolveCodexThread(childThreadId);
403
- verifyStoredBinding(taskContext.taskId, existing.taskBinding, existing.state.startEvidence?.nativeAgent ?? existing.state.spawn?.nativeAgent ?? '');
404
- let latest = store.apply(resolved.resolution.thread);
405
- for (const reroute of resolved.reroutes)
406
- latest = store.apply(reroute);
407
- latest = store.apply(resolved.resolution.settings);
408
- output({ status: latest.state.status, changed: true, evidence: latest.state.startEvidence, diagnostics: resolved.diagnostics, error: latest.state.error });
409
- if (latest.state.status !== 'start-ready')
410
- process.exitCode = 1;
411
- return;
412
- }
413
- if (internalHandlerRoute('codex-lifecycle', 'resolve-stop', parsed.operation)) {
414
- const stopTurnId = store.read(childThreadId).state.stop?.turnId;
415
- if (!stopTurnId)
416
- throw new Error('Codex lifecycle stop hook is not available');
417
- const terminal = await resolveCodexTerminal(childThreadId, stopTurnId);
418
- verifyStoredBinding(taskContext.taskId, existing.taskBinding, existing.state.startEvidence?.nativeAgent ?? existing.state.spawn?.nativeAgent ?? '');
419
- const latest = store.apply(terminal);
420
- if (latest.state.status !== 'stop-ready') {
421
- throw new Error(`Codex lifecycle stop evidence is not ready (status=${latest.state.status})`);
422
- }
423
- output({ status: latest.state.status, changed: true, evidence: latest.state.stopEvidence, diagnostics: [], error: latest.state.error });
424
- return;
425
- }
426
- if (internalHandlerRoute('codex-lifecycle', 'consume', parsed.operation)) {
427
- const consumer = parsed.values['--consumer'];
428
- if (!consumer)
429
- throw new Error('consume requires --consumer');
430
- verifyStoredBinding(taskContext.taskId, existing.taskBinding, existing.state.startEvidence?.nativeAgent ?? existing.state.spawn?.nativeAgent ?? '');
431
- const consumed = store.consume(childThreadId, consumer, hookDefinitionHash());
432
- output({ status: 'consumed', changed: true, evidence: {
433
- start: consumed.state.startEvidence,
434
- stop: consumed.state.stopEvidence
435
- }, diagnostics: [], error: null });
436
- }
437
- }
438
- catch (error) {
439
- failure('CODEX_LIFECYCLE_FAILED', error instanceof Error ? error.message : String(error));
440
- }
441
- }
442
- export { codexLifecycle };
443
- //# sourceMappingURL=cli.js.map
@@ -1,236 +0,0 @@
1
- function isCodexLifecycleActivationEvidence(value, parentId) {
2
- if (!value || typeof value !== 'object' || Array.isArray(value))
3
- return false;
4
- const state = value;
5
- const evidence = state.activationEvidence;
6
- if (!evidence || typeof evidence !== 'object' || Array.isArray(evidence))
7
- return false;
8
- const activation = evidence;
9
- return activation.kind === 'codex-lifecycle-v2'
10
- && typeof activation.hookDefinitionHash === 'string'
11
- && activation.hookDefinitionHash.trim() !== ''
12
- && Number.isSafeInteger(activation.startRevision)
13
- && activation.startRevision >= 1
14
- && activation.capabilitySessionId === parentId;
15
- }
16
- function isCodexDelegationReceiptEvidence(value) {
17
- if (!value || typeof value !== 'object' || Array.isArray(value))
18
- return false;
19
- const receipt = value;
20
- if (receipt.client !== 'codex' || typeof receipt.id !== 'string')
21
- return false;
22
- const status = receipt.status;
23
- if (!['activated', 'stage-completed', 'sealed', 'consumed', 'aborted'].includes(status))
24
- return true;
25
- const adapterEvidence = receipt.adapterEvidence;
26
- if (!adapterEvidence || typeof adapterEvidence !== 'object' || Array.isArray(adapterEvidence))
27
- return false;
28
- const codex = adapterEvidence.codex;
29
- if (!codex || typeof codex !== 'object' || Array.isArray(codex))
30
- return false;
31
- const state = codex;
32
- if (!isCodexLifecycleActivationEvidence(state, String(receipt.parentId ?? '')))
33
- return false;
34
- if (!['sealed', 'consumed', 'aborted'].includes(status))
35
- return true;
36
- const records = state.records;
37
- if (!records || typeof records !== 'object' || Array.isArray(records))
38
- return false;
39
- return Object.values(records).some((record) => {
40
- if (!record || typeof record !== 'object' || Array.isArray(record))
41
- return false;
42
- const evidence = record;
43
- return evidence.consumer === receipt.id
44
- && typeof evidence.consumedAt === 'string'
45
- && evidence.consumedAt.trim() !== '';
46
- });
47
- }
48
- function createCodexLifecycleState(cliVersion) {
49
- if (!cliVersion.trim())
50
- throw new Error('Codex CLI version is required');
51
- return Object.freeze({
52
- schemaVersion: 1,
53
- cliVersion,
54
- status: 'empty',
55
- spawn: null,
56
- child: null,
57
- thread: null,
58
- settings: null,
59
- reroute: null,
60
- terminal: null,
61
- stop: null,
62
- startEvidence: null,
63
- stopEvidence: null,
64
- error: null
65
- });
66
- }
67
- function invalid(state, code, message) {
68
- return Object.freeze({
69
- ...state,
70
- status: 'invalid',
71
- startEvidence: null,
72
- stopEvidence: null,
73
- error: Object.freeze({ code, message })
74
- });
75
- }
76
- function same(left, right) {
77
- return JSON.stringify(left) === JSON.stringify(right);
78
- }
79
- function sameChildCorrelation(left, right) {
80
- return left.sessionId === right.sessionId
81
- && left.childThreadId === right.childThreadId
82
- && left.parentThreadId === right.parentThreadId
83
- && left.nativeAgent === right.nativeAgent;
84
- }
85
- function requiredStrings(event) {
86
- return Object.entries(event).every(([key, value]) => key === 'forkedFromId'
87
- || (typeof value !== 'string' || value.trim() !== ''));
88
- }
89
- function derive(state) {
90
- const { spawn, child, thread, settings, reroute, terminal, stop } = state;
91
- if (spawn && child && (spawn.sessionId !== child.parentThreadId
92
- || spawn.nativeAgent !== child.nativeAgent)) {
93
- return invalid(state, 'CODEX_EVIDENCE_IDENTITY_MISMATCH', 'hook spawn and child identity do not match');
94
- }
95
- if (child && child.sessionId !== child.parentThreadId) {
96
- return invalid(state, 'CODEX_EVIDENCE_IDENTITY_MISMATCH', 'child event session does not match the parent thread');
97
- }
98
- if (child && thread && child.childThreadId !== thread.childThreadId) {
99
- return invalid(state, 'CODEX_EVIDENCE_IDENTITY_MISMATCH', 'hook and App Server child identity do not match');
100
- }
101
- if (child && thread && child.nativeAgent !== thread.nativeAgent) {
102
- return invalid(state, 'CODEX_EVIDENCE_IDENTITY_MISMATCH', 'hook and App Server agent roles do not match');
103
- }
104
- if (child && thread && child.parentThreadId !== thread.parentThreadId) {
105
- return invalid(state, 'CODEX_EVIDENCE_PARENT_MISMATCH', 'hook and App Server parent identity do not match');
106
- }
107
- if (spawn && thread && (thread.parentThreadId !== spawn.sessionId
108
- || thread.sourceParentThreadId !== spawn.sessionId)) {
109
- return invalid(state, 'CODEX_EVIDENCE_PARENT_MISMATCH', 'App Server parent identity does not match the spawning session');
110
- }
111
- if (spawn && thread && spawn.nativeAgent !== thread.nativeAgent) {
112
- return invalid(state, 'CODEX_EVIDENCE_IDENTITY_MISMATCH', 'spawn and App Server agent roles do not match');
113
- }
114
- if (thread?.forkedFromId !== null && thread?.forkedFromId !== undefined) {
115
- return invalid(state, 'CODEX_EVIDENCE_FORK_FORBIDDEN', 'forked child threads are not fresh lifecycle evidence');
116
- }
117
- if (settings && child && settings.childThreadId !== child.childThreadId) {
118
- return invalid(state, 'CODEX_EVIDENCE_IDENTITY_MISMATCH', 'resolved settings belong to a different child');
119
- }
120
- if (reroute && child && reroute.childThreadId !== child.childThreadId) {
121
- return invalid(state, 'CODEX_EVIDENCE_IDENTITY_MISMATCH', 'model reroute belongs to a different child');
122
- }
123
- if (spawn?.requestedModel && settings && spawn.requestedModel !== settings.model) {
124
- if (!reroute
125
- || reroute.fromModel !== spawn.requestedModel
126
- || reroute.toModel !== settings.model
127
- || !reroute.reason.trim()) {
128
- return invalid(state, 'CODEX_EVIDENCE_MODEL_UNEXPLAINED', 'resolved model differs without a matching host reroute');
129
- }
130
- }
131
- if (spawn?.requestedReasoningEffort
132
- && settings
133
- && spawn.requestedReasoningEffort !== settings.reasoningEffort) {
134
- return invalid(state, 'CODEX_EVIDENCE_EFFORT_UNEXPLAINED', 'resolved reasoning effort differs without structured host evidence');
135
- }
136
- if (terminal && terminal.status !== 'completed') {
137
- return invalid(state, 'CODEX_EVIDENCE_TERMINAL_INVALID', `child turn ended as '${terminal.status}'`);
138
- }
139
- if (terminal && child && terminal.childThreadId !== child.childThreadId) {
140
- return invalid(state, 'CODEX_EVIDENCE_IDENTITY_MISMATCH', 'terminal event belongs to a different child');
141
- }
142
- if (stop && child && (stop.sessionId !== child.sessionId
143
- || stop.turnId !== child.turnId
144
- || stop.childThreadId !== child.childThreadId
145
- || stop.nativeAgent !== child.nativeAgent)) {
146
- return invalid(state, 'CODEX_EVIDENCE_IDENTITY_MISMATCH', 'hook stop identity does not match the child');
147
- }
148
- let startEvidence = null;
149
- if (spawn && child && thread && settings) {
150
- startEvidence = Object.freeze({
151
- schemaVersion: 1,
152
- cliVersion: state.cliVersion,
153
- parentThreadId: spawn.sessionId,
154
- parentTurnId: spawn.turnId,
155
- spawnToolUseId: spawn.toolUseId,
156
- childThreadId: child.childThreadId,
157
- nativeAgent: child.nativeAgent,
158
- spawnMode: 'fresh',
159
- actualModel: Object.freeze({ value: settings.model, source: 'codex-app-server-settings' }),
160
- actualReasoningEffort: Object.freeze({ value: settings.reasoningEffort, source: 'codex-app-server-settings' }),
161
- modelFallbackReason: reroute?.reason ?? null,
162
- reasoningEffortFallbackReason: null,
163
- hookDefinitionHash: spawn.hookDefinitionHash
164
- });
165
- }
166
- let stopEvidence = null;
167
- if (startEvidence && terminal?.status === 'completed' && stop) {
168
- stopEvidence = Object.freeze({
169
- schemaVersion: 1,
170
- childThreadId: startEvidence.childThreadId,
171
- turnId: terminal.turnId,
172
- terminalStatus: 'completed',
173
- hookStopObserved: stop.source !== 'parent-rollout'
174
- });
175
- }
176
- const status = stopEvidence
177
- ? 'stop-ready'
178
- : terminal
179
- ? 'observed-terminal'
180
- : startEvidence
181
- ? 'start-ready'
182
- : child
183
- ? 'observed-child'
184
- : spawn
185
- ? 'observed-spawn'
186
- : 'empty';
187
- return Object.freeze({ ...state, status, startEvidence, stopEvidence, error: null });
188
- }
189
- function reduceCodexLifecycleEvent(state, event) {
190
- if (state.status === 'invalid' || state.status === 'expired')
191
- return state;
192
- if (!requiredStrings(event)) {
193
- return invalid(state, 'CODEX_EVIDENCE_FIELD_MISSING', 'lifecycle event contains an empty required field');
194
- }
195
- const key = {
196
- 'hook-spawn': 'spawn',
197
- 'hook-child': 'child',
198
- 'app-thread': 'thread',
199
- 'app-settings': 'settings',
200
- 'app-reroute': 'reroute',
201
- 'app-terminal': 'terminal',
202
- 'hook-stop': 'stop'
203
- }[event.type];
204
- const existing = state[key];
205
- if (existing) {
206
- if (same(existing, event))
207
- return state;
208
- if (existing.type === 'hook-child'
209
- && event.type === 'hook-child'
210
- && sameChildCorrelation(existing, event)
211
- && (existing.source === 'parent-rollout') !== (event.source === 'parent-rollout')) {
212
- if (existing.source !== 'parent-rollout')
213
- return state;
214
- return derive(Object.freeze({ ...state, child: Object.freeze({ ...event }) }));
215
- }
216
- return invalid(state, 'CODEX_EVIDENCE_REPLAY_CONFLICT', `conflicting '${event.type}' event`);
217
- }
218
- return derive(Object.freeze({ ...state, [key]: Object.freeze({ ...event }) }));
219
- }
220
- function expireCodexLifecycleState(state) {
221
- if (!['observed-spawn', 'observed-child', 'start-ready', 'observed-terminal'].includes(state.status)) {
222
- return state;
223
- }
224
- return Object.freeze({
225
- ...state,
226
- status: 'expired',
227
- startEvidence: null,
228
- stopEvidence: null,
229
- error: Object.freeze({
230
- code: 'CODEX_EVIDENCE_EXPIRED',
231
- message: 'Codex lifecycle evidence expired before completion'
232
- })
233
- });
234
- }
235
- export { createCodexLifecycleState, expireCodexLifecycleState, isCodexLifecycleActivationEvidence, isCodexDelegationReceiptEvidence, reduceCodexLifecycleEvent };
236
- //# sourceMappingURL=evidence.js.map