@memnexus-ai/mx-agent-cli 0.1.294 → 0.1.295-next.0

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 (637) hide show
  1. package/dist/__tests__/agents-md-canonicalization.test.js +31 -1
  2. package/dist/__tests__/agents-md-canonicalization.test.js.map +1 -1
  3. package/dist/__tests__/behavioral-extraction.test.js +50 -0
  4. package/dist/__tests__/behavioral-extraction.test.js.map +1 -1
  5. package/dist/__tests__/bootstrap-behavioral-contract.test.js +6 -3
  6. package/dist/__tests__/bootstrap-behavioral-contract.test.js.map +1 -1
  7. package/dist/__tests__/bundle-config.test.js +28 -2
  8. package/dist/__tests__/bundle-config.test.js.map +1 -1
  9. package/dist/__tests__/catalog-interaction.test.d.ts +25 -0
  10. package/dist/__tests__/catalog-interaction.test.d.ts.map +1 -0
  11. package/dist/__tests__/catalog-interaction.test.js +269 -0
  12. package/dist/__tests__/catalog-interaction.test.js.map +1 -0
  13. package/dist/__tests__/catalog-named-memories.test.d.ts +2 -0
  14. package/dist/__tests__/catalog-named-memories.test.d.ts.map +1 -0
  15. package/dist/__tests__/catalog-named-memories.test.js +204 -0
  16. package/dist/__tests__/catalog-named-memories.test.js.map +1 -0
  17. package/dist/__tests__/catalog-origin-main.test.d.ts +15 -0
  18. package/dist/__tests__/catalog-origin-main.test.d.ts.map +1 -0
  19. package/dist/__tests__/catalog-origin-main.test.js +296 -0
  20. package/dist/__tests__/catalog-origin-main.test.js.map +1 -0
  21. package/dist/__tests__/catalog.test.d.ts +2 -0
  22. package/dist/__tests__/catalog.test.d.ts.map +1 -0
  23. package/dist/__tests__/catalog.test.js +131 -0
  24. package/dist/__tests__/catalog.test.js.map +1 -0
  25. package/dist/__tests__/cli-build-integration.test.d.ts +2 -0
  26. package/dist/__tests__/cli-build-integration.test.d.ts.map +1 -0
  27. package/dist/__tests__/cli-build-integration.test.js +64 -0
  28. package/dist/__tests__/cli-build-integration.test.js.map +1 -0
  29. package/dist/__tests__/cli-build.test.d.ts +2 -0
  30. package/dist/__tests__/cli-build.test.d.ts.map +1 -0
  31. package/dist/__tests__/cli-build.test.js +640 -0
  32. package/dist/__tests__/cli-build.test.js.map +1 -0
  33. package/dist/__tests__/codex-deploy-config.test.js +60 -0
  34. package/dist/__tests__/codex-deploy-config.test.js.map +1 -1
  35. package/dist/__tests__/completion-rows-discriminate.test.d.ts +20 -0
  36. package/dist/__tests__/completion-rows-discriminate.test.d.ts.map +1 -0
  37. package/dist/__tests__/completion-rows-discriminate.test.js +481 -0
  38. package/dist/__tests__/completion-rows-discriminate.test.js.map +1 -0
  39. package/dist/__tests__/config-fidelity-archived-symlinks.test.d.ts +2 -0
  40. package/dist/__tests__/config-fidelity-archived-symlinks.test.d.ts.map +1 -0
  41. package/dist/__tests__/config-fidelity-archived-symlinks.test.js +127 -0
  42. package/dist/__tests__/config-fidelity-archived-symlinks.test.js.map +1 -0
  43. package/dist/__tests__/config-fidelity-display.test.d.ts +2 -0
  44. package/dist/__tests__/config-fidelity-display.test.d.ts.map +1 -0
  45. package/dist/__tests__/config-fidelity-display.test.js +77 -0
  46. package/dist/__tests__/config-fidelity-display.test.js.map +1 -0
  47. package/dist/__tests__/config-fidelity.test.js +166 -33
  48. package/dist/__tests__/config-fidelity.test.js.map +1 -1
  49. package/dist/__tests__/config-generation-gate.test.js +78 -0
  50. package/dist/__tests__/config-generation-gate.test.js.map +1 -1
  51. package/dist/__tests__/config-sync.test.js +89 -1
  52. package/dist/__tests__/config-sync.test.js.map +1 -1
  53. package/dist/__tests__/coordination-401-production-wiring.test.js +12 -7
  54. package/dist/__tests__/coordination-401-production-wiring.test.js.map +1 -1
  55. package/dist/__tests__/coordination-401-recovery.test.js +39 -32
  56. package/dist/__tests__/coordination-401-recovery.test.js.map +1 -1
  57. package/dist/__tests__/coordination-brief-pin.test.js +21 -8
  58. package/dist/__tests__/coordination-brief-pin.test.js.map +1 -1
  59. package/dist/__tests__/coordination-client.test.js +227 -3
  60. package/dist/__tests__/coordination-client.test.js.map +1 -1
  61. package/dist/__tests__/coordination-credential-endpoint.test.d.ts +24 -0
  62. package/dist/__tests__/coordination-credential-endpoint.test.d.ts.map +1 -0
  63. package/dist/__tests__/coordination-credential-endpoint.test.js +1230 -0
  64. package/dist/__tests__/coordination-credential-endpoint.test.js.map +1 -0
  65. package/dist/__tests__/coordination-memory.test.js +20 -4
  66. package/dist/__tests__/coordination-memory.test.js.map +1 -1
  67. package/dist/__tests__/coordination-provision.test.js +22 -14
  68. package/dist/__tests__/coordination-provision.test.js.map +1 -1
  69. package/dist/__tests__/coordination-status-registration.test.d.ts +16 -0
  70. package/dist/__tests__/coordination-status-registration.test.d.ts.map +1 -0
  71. package/dist/__tests__/coordination-status-registration.test.js +480 -0
  72. package/dist/__tests__/coordination-status-registration.test.js.map +1 -0
  73. package/dist/__tests__/coordination-token-multitenant.test.js +11 -11
  74. package/dist/__tests__/coordination-token-multitenant.test.js.map +1 -1
  75. package/dist/__tests__/coordination-token.test.js +35 -28
  76. package/dist/__tests__/coordination-token.test.js.map +1 -1
  77. package/dist/__tests__/creation-pr-class-check.test.d.ts +17 -0
  78. package/dist/__tests__/creation-pr-class-check.test.d.ts.map +1 -0
  79. package/dist/__tests__/creation-pr-class-check.test.js +346 -0
  80. package/dist/__tests__/creation-pr-class-check.test.js.map +1 -0
  81. package/dist/__tests__/decision-check.test.d.ts +2 -0
  82. package/dist/__tests__/decision-check.test.d.ts.map +1 -0
  83. package/dist/__tests__/decision-check.test.js +105 -0
  84. package/dist/__tests__/decision-check.test.js.map +1 -0
  85. package/dist/__tests__/digest-team-omission.test.d.ts +15 -0
  86. package/dist/__tests__/digest-team-omission.test.d.ts.map +1 -0
  87. package/dist/__tests__/digest-team-omission.test.js +247 -0
  88. package/dist/__tests__/digest-team-omission.test.js.map +1 -0
  89. package/dist/__tests__/doctor-worktree-path.test.d.ts +17 -0
  90. package/dist/__tests__/doctor-worktree-path.test.d.ts.map +1 -0
  91. package/dist/__tests__/doctor-worktree-path.test.js +158 -0
  92. package/dist/__tests__/doctor-worktree-path.test.js.map +1 -0
  93. package/dist/__tests__/fleet-data.test.js +40 -7
  94. package/dist/__tests__/fleet-data.test.js.map +1 -1
  95. package/dist/__tests__/fleet-tmux.test.js +2 -2
  96. package/dist/__tests__/fleet-tmux.test.js.map +1 -1
  97. package/dist/__tests__/generate-claude.test.js +137 -3
  98. package/dist/__tests__/generate-claude.test.js.map +1 -1
  99. package/dist/__tests__/git-env.test.d.ts +14 -0
  100. package/dist/__tests__/git-env.test.d.ts.map +1 -0
  101. package/dist/__tests__/git-env.test.js +131 -0
  102. package/dist/__tests__/git-env.test.js.map +1 -0
  103. package/dist/__tests__/git-identity-isolation.test.d.ts +33 -0
  104. package/dist/__tests__/git-identity-isolation.test.d.ts.map +1 -0
  105. package/dist/__tests__/git-identity-isolation.test.js +626 -0
  106. package/dist/__tests__/git-identity-isolation.test.js.map +1 -0
  107. package/dist/__tests__/harness-extract.test.d.ts +19 -0
  108. package/dist/__tests__/harness-extract.test.d.ts.map +1 -0
  109. package/dist/__tests__/harness-extract.test.js +566 -0
  110. package/dist/__tests__/harness-extract.test.js.map +1 -0
  111. package/dist/__tests__/harness-wiring.test.d.ts +16 -0
  112. package/dist/__tests__/harness-wiring.test.d.ts.map +1 -0
  113. package/dist/__tests__/harness-wiring.test.js +469 -0
  114. package/dist/__tests__/harness-wiring.test.js.map +1 -0
  115. package/dist/__tests__/hook-heal.test.js +17 -0
  116. package/dist/__tests__/hook-heal.test.js.map +1 -1
  117. package/dist/__tests__/init-coordination-url.test.d.ts +11 -0
  118. package/dist/__tests__/init-coordination-url.test.d.ts.map +1 -0
  119. package/dist/__tests__/init-coordination-url.test.js +114 -0
  120. package/dist/__tests__/init-coordination-url.test.js.map +1 -0
  121. package/dist/__tests__/memory-autocreate.test.js +574 -8
  122. package/dist/__tests__/memory-autocreate.test.js.map +1 -1
  123. package/dist/__tests__/memory-log-result.test.d.ts +13 -0
  124. package/dist/__tests__/memory-log-result.test.d.ts.map +1 -0
  125. package/dist/__tests__/memory-log-result.test.js +113 -0
  126. package/dist/__tests__/memory-log-result.test.js.map +1 -0
  127. package/dist/__tests__/memory-named-exists.test.d.ts +16 -0
  128. package/dist/__tests__/memory-named-exists.test.d.ts.map +1 -0
  129. package/dist/__tests__/memory-named-exists.test.js +288 -0
  130. package/dist/__tests__/memory-named-exists.test.js.map +1 -0
  131. package/dist/__tests__/mx-agent-cli-pin-validation.test.js +23 -47
  132. package/dist/__tests__/mx-agent-cli-pin-validation.test.js.map +1 -1
  133. package/dist/__tests__/mx-agent-cli-version-pin.test.js +34 -88
  134. package/dist/__tests__/mx-agent-cli-version-pin.test.js.map +1 -1
  135. package/dist/__tests__/new-project-first-team.test.d.ts +15 -0
  136. package/dist/__tests__/new-project-first-team.test.d.ts.map +1 -0
  137. package/dist/__tests__/new-project-first-team.test.js +263 -0
  138. package/dist/__tests__/new-project-first-team.test.js.map +1 -0
  139. package/dist/__tests__/otel-env.test.d.ts +2 -0
  140. package/dist/__tests__/otel-env.test.d.ts.map +1 -0
  141. package/dist/__tests__/otel-env.test.js +127 -0
  142. package/dist/__tests__/otel-env.test.js.map +1 -0
  143. package/dist/__tests__/project-config.test.js +283 -1
  144. package/dist/__tests__/project-config.test.js.map +1 -1
  145. package/dist/__tests__/provision-behavioral-contract.test.js +96 -4
  146. package/dist/__tests__/provision-behavioral-contract.test.js.map +1 -1
  147. package/dist/__tests__/provision-catalog-precondition.test.d.ts +26 -0
  148. package/dist/__tests__/provision-catalog-precondition.test.d.ts.map +1 -0
  149. package/dist/__tests__/provision-catalog-precondition.test.js +620 -0
  150. package/dist/__tests__/provision-catalog-precondition.test.js.map +1 -0
  151. package/dist/__tests__/provision-completion-rows.test.d.ts +17 -0
  152. package/dist/__tests__/provision-completion-rows.test.d.ts.map +1 -0
  153. package/dist/__tests__/provision-completion-rows.test.js +3095 -0
  154. package/dist/__tests__/provision-completion-rows.test.js.map +1 -0
  155. package/dist/__tests__/provision-identity-gate.test.d.ts +20 -0
  156. package/dist/__tests__/provision-identity-gate.test.d.ts.map +1 -0
  157. package/dist/__tests__/provision-identity-gate.test.js +600 -0
  158. package/dist/__tests__/provision-identity-gate.test.js.map +1 -0
  159. package/dist/__tests__/provision-resume.test.d.ts +26 -0
  160. package/dist/__tests__/provision-resume.test.d.ts.map +1 -0
  161. package/dist/__tests__/provision-resume.test.js +1164 -0
  162. package/dist/__tests__/provision-resume.test.js.map +1 -0
  163. package/dist/__tests__/provision-roleguide-anchors.test.d.ts +27 -0
  164. package/dist/__tests__/provision-roleguide-anchors.test.d.ts.map +1 -0
  165. package/dist/__tests__/provision-roleguide-anchors.test.js +747 -0
  166. package/dist/__tests__/provision-roleguide-anchors.test.js.map +1 -0
  167. package/dist/__tests__/provision-skeleton-fill.test.d.ts +16 -0
  168. package/dist/__tests__/provision-skeleton-fill.test.d.ts.map +1 -0
  169. package/dist/__tests__/provision-skeleton-fill.test.js +488 -0
  170. package/dist/__tests__/provision-skeleton-fill.test.js.map +1 -0
  171. package/dist/__tests__/provision-teambuilder-flow.test.js +93 -7
  172. package/dist/__tests__/provision-teambuilder-flow.test.js.map +1 -1
  173. package/dist/__tests__/repo-guard-wiring.test.d.ts +21 -0
  174. package/dist/__tests__/repo-guard-wiring.test.d.ts.map +1 -0
  175. package/dist/__tests__/repo-guard-wiring.test.js +638 -0
  176. package/dist/__tests__/repo-guard-wiring.test.js.map +1 -0
  177. package/dist/__tests__/report-format.test.d.ts +2 -0
  178. package/dist/__tests__/report-format.test.d.ts.map +1 -0
  179. package/dist/__tests__/report-format.test.js +219 -0
  180. package/dist/__tests__/report-format.test.js.map +1 -0
  181. package/dist/__tests__/runtime-codex-gateway-pilot.test.d.ts +2 -0
  182. package/dist/__tests__/runtime-codex-gateway-pilot.test.d.ts.map +1 -0
  183. package/dist/__tests__/runtime-codex-gateway-pilot.test.js +309 -0
  184. package/dist/__tests__/runtime-codex-gateway-pilot.test.js.map +1 -0
  185. package/dist/__tests__/runtime-codex-gateway-provider.test.d.ts +2 -0
  186. package/dist/__tests__/runtime-codex-gateway-provider.test.d.ts.map +1 -0
  187. package/dist/__tests__/runtime-codex-gateway-provider.test.js +163 -0
  188. package/dist/__tests__/runtime-codex-gateway-provider.test.js.map +1 -0
  189. package/dist/__tests__/runtime-identity.test.d.ts +2 -0
  190. package/dist/__tests__/runtime-identity.test.d.ts.map +1 -0
  191. package/dist/__tests__/runtime-identity.test.js +117 -0
  192. package/dist/__tests__/runtime-identity.test.js.map +1 -0
  193. package/dist/__tests__/session-pause-provisioning.test.js +6 -1
  194. package/dist/__tests__/session-pause-provisioning.test.js.map +1 -1
  195. package/dist/__tests__/session-record-disclosure.test.d.ts +24 -0
  196. package/dist/__tests__/session-record-disclosure.test.d.ts.map +1 -0
  197. package/dist/__tests__/session-record-disclosure.test.js +494 -0
  198. package/dist/__tests__/session-record-disclosure.test.js.map +1 -0
  199. package/dist/__tests__/session.test.js +35 -0
  200. package/dist/__tests__/session.test.js.map +1 -1
  201. package/dist/__tests__/slug-validation.test.js +27 -21
  202. package/dist/__tests__/slug-validation.test.js.map +1 -1
  203. package/dist/__tests__/standards-injection.test.d.ts +2 -0
  204. package/dist/__tests__/standards-injection.test.d.ts.map +1 -0
  205. package/dist/__tests__/standards-injection.test.js +179 -0
  206. package/dist/__tests__/standards-injection.test.js.map +1 -0
  207. package/dist/__tests__/start-interactive-role.test.d.ts +22 -0
  208. package/dist/__tests__/start-interactive-role.test.d.ts.map +1 -0
  209. package/dist/__tests__/start-interactive-role.test.js +204 -0
  210. package/dist/__tests__/start-interactive-role.test.js.map +1 -0
  211. package/dist/__tests__/start-team-identity.test.d.ts +25 -0
  212. package/dist/__tests__/start-team-identity.test.d.ts.map +1 -0
  213. package/dist/__tests__/start-team-identity.test.js +307 -0
  214. package/dist/__tests__/start-team-identity.test.js.map +1 -0
  215. package/dist/__tests__/team-memory-commands.test.js +142 -19
  216. package/dist/__tests__/team-memory-commands.test.js.map +1 -1
  217. package/dist/__tests__/template-sync.test.d.ts +19 -0
  218. package/dist/__tests__/template-sync.test.d.ts.map +1 -0
  219. package/dist/__tests__/template-sync.test.js +270 -0
  220. package/dist/__tests__/template-sync.test.js.map +1 -0
  221. package/dist/__tests__/test-project.test.d.ts +18 -0
  222. package/dist/__tests__/test-project.test.d.ts.map +1 -0
  223. package/dist/__tests__/test-project.test.js +465 -0
  224. package/dist/__tests__/test-project.test.js.map +1 -0
  225. package/dist/__tests__/usage-per-call-grouping.test.d.ts +25 -0
  226. package/dist/__tests__/usage-per-call-grouping.test.d.ts.map +1 -0
  227. package/dist/__tests__/usage-per-call-grouping.test.js +952 -0
  228. package/dist/__tests__/usage-per-call-grouping.test.js.map +1 -0
  229. package/dist/__tests__/verify-hooks-check-all.test.d.ts +32 -0
  230. package/dist/__tests__/verify-hooks-check-all.test.d.ts.map +1 -0
  231. package/dist/__tests__/verify-hooks-check-all.test.js +553 -0
  232. package/dist/__tests__/verify-hooks-check-all.test.js.map +1 -0
  233. package/dist/__tests__/worktree-sync-fixture.test.d.ts +17 -0
  234. package/dist/__tests__/worktree-sync-fixture.test.d.ts.map +1 -0
  235. package/dist/__tests__/worktree-sync-fixture.test.js +519 -0
  236. package/dist/__tests__/worktree-sync-fixture.test.js.map +1 -0
  237. package/dist/__tests__/worktree-sync-hardening.test.d.ts +13 -0
  238. package/dist/__tests__/worktree-sync-hardening.test.d.ts.map +1 -0
  239. package/dist/__tests__/worktree-sync-hardening.test.js +919 -0
  240. package/dist/__tests__/worktree-sync-hardening.test.js.map +1 -0
  241. package/dist/__tests__/worktree-sync-shell-free.test.d.ts +25 -0
  242. package/dist/__tests__/worktree-sync-shell-free.test.d.ts.map +1 -0
  243. package/dist/__tests__/worktree-sync-shell-free.test.js +185 -0
  244. package/dist/__tests__/worktree-sync-shell-free.test.js.map +1 -0
  245. package/dist/__tests__/worktree.test.js +75 -1
  246. package/dist/__tests__/worktree.test.js.map +1 -1
  247. package/dist/agent-config/.mx-config-generation.json +2 -2
  248. package/dist/agent-config/AGENTS.md.template +2 -2
  249. package/dist/agent-config/agents/bar-raiser.md +34 -1
  250. package/dist/agent-config/agents/red-team.md +34 -1
  251. package/dist/agent-config/agents/security-reviewer.md +22 -1
  252. package/dist/agent-config/claude-md/CLAUDE.core.md +6 -1
  253. package/dist/agent-config/claude-md/CLAUDE.overlay.memnexus.md +1 -3
  254. package/dist/agent-config/coordination-rosters.generated.json +10 -0
  255. package/dist/agent-config/hooks/HOOK-TIMEOUTS.md +27 -1
  256. package/dist/agent-config/hooks/OR-TRUE-AUDIT.md +8 -8
  257. package/dist/agent-config/hooks/auto-capture-precompact.sh +180 -83
  258. package/dist/agent-config/hooks/auto-capture-session-end.sh +216 -87
  259. package/dist/agent-config/hooks/auto-checkpoint.sh +75 -5
  260. package/dist/agent-config/hooks/coordination-brief.sh +290 -245
  261. package/dist/agent-config/hooks/deploy-verification-reminder.sh +359 -22
  262. package/dist/agent-config/hooks/git-mutation-guard.sh +85 -13
  263. package/dist/agent-config/hooks/observability-sync.sh +46 -9
  264. package/dist/agent-config/hooks/session-idle-gate.sh +177 -12
  265. package/dist/agent-config/hooks/subagent-write-guard.sh +670 -0
  266. package/dist/agent-config/hooks/worktree-guard.sh +36 -7
  267. package/dist/agent-config/settings.json +15 -2
  268. package/dist/agent-config/shared/creation-pr-review-checklist.md +317 -0
  269. package/dist/commands/bootstrap.d.ts.map +1 -1
  270. package/dist/commands/bootstrap.js +22 -10
  271. package/dist/commands/bootstrap.js.map +1 -1
  272. package/dist/commands/config-sync.d.ts +37 -0
  273. package/dist/commands/config-sync.d.ts.map +1 -1
  274. package/dist/commands/config-sync.js +36 -18
  275. package/dist/commands/config-sync.js.map +1 -1
  276. package/dist/commands/config.d.ts.map +1 -1
  277. package/dist/commands/config.js +28 -1
  278. package/dist/commands/config.js.map +1 -1
  279. package/dist/commands/coordination.d.ts +150 -8
  280. package/dist/commands/coordination.d.ts.map +1 -1
  281. package/dist/commands/coordination.js +519 -25
  282. package/dist/commands/coordination.js.map +1 -1
  283. package/dist/commands/create.d.ts +913 -1
  284. package/dist/commands/create.d.ts.map +1 -1
  285. package/dist/commands/create.js +4879 -345
  286. package/dist/commands/create.js.map +1 -1
  287. package/dist/commands/dashboard.d.ts.map +1 -1
  288. package/dist/commands/dashboard.js +10 -2
  289. package/dist/commands/dashboard.js.map +1 -1
  290. package/dist/commands/decision.d.ts +20 -0
  291. package/dist/commands/decision.d.ts.map +1 -0
  292. package/dist/commands/decision.js +54 -0
  293. package/dist/commands/decision.js.map +1 -0
  294. package/dist/commands/digest.d.ts.map +1 -1
  295. package/dist/commands/digest.js +91 -7
  296. package/dist/commands/digest.js.map +1 -1
  297. package/dist/commands/fleet-tmux.js +1 -1
  298. package/dist/commands/fleet-tmux.js.map +1 -1
  299. package/dist/commands/init.d.ts +8 -0
  300. package/dist/commands/init.d.ts.map +1 -1
  301. package/dist/commands/init.js +26 -1
  302. package/dist/commands/init.js.map +1 -1
  303. package/dist/commands/list.d.ts +32 -1
  304. package/dist/commands/list.d.ts.map +1 -1
  305. package/dist/commands/list.js +98 -5
  306. package/dist/commands/list.js.map +1 -1
  307. package/dist/commands/monitor.d.ts.map +1 -1
  308. package/dist/commands/monitor.js +8 -2
  309. package/dist/commands/monitor.js.map +1 -1
  310. package/dist/commands/new.d.ts +2 -0
  311. package/dist/commands/new.d.ts.map +1 -1
  312. package/dist/commands/new.js +114 -21
  313. package/dist/commands/new.js.map +1 -1
  314. package/dist/commands/report.d.ts +33 -0
  315. package/dist/commands/report.d.ts.map +1 -0
  316. package/dist/commands/report.js +73 -0
  317. package/dist/commands/report.js.map +1 -0
  318. package/dist/commands/session.js +1 -1
  319. package/dist/commands/session.js.map +1 -1
  320. package/dist/commands/start.d.ts +42 -0
  321. package/dist/commands/start.d.ts.map +1 -1
  322. package/dist/commands/start.js +177 -8
  323. package/dist/commands/start.js.map +1 -1
  324. package/dist/commands/sync.d.ts +57 -0
  325. package/dist/commands/sync.d.ts.map +1 -1
  326. package/dist/commands/sync.js +117 -3
  327. package/dist/commands/sync.js.map +1 -1
  328. package/dist/commands/update.d.ts +13 -1
  329. package/dist/commands/update.d.ts.map +1 -1
  330. package/dist/commands/update.js +50 -1
  331. package/dist/commands/update.js.map +1 -1
  332. package/dist/commands/usage.d.ts +64 -0
  333. package/dist/commands/usage.d.ts.map +1 -1
  334. package/dist/commands/usage.js +96 -3
  335. package/dist/commands/usage.js.map +1 -1
  336. package/dist/commands/verify-hooks.d.ts +37 -1
  337. package/dist/commands/verify-hooks.d.ts.map +1 -1
  338. package/dist/commands/verify-hooks.js +114 -2
  339. package/dist/commands/verify-hooks.js.map +1 -1
  340. package/dist/commands/window.d.ts +10 -0
  341. package/dist/commands/window.d.ts.map +1 -0
  342. package/dist/commands/window.js +265 -0
  343. package/dist/commands/window.js.map +1 -0
  344. package/dist/commands/worktree-sync.d.ts +54 -0
  345. package/dist/commands/worktree-sync.d.ts.map +1 -0
  346. package/dist/commands/worktree-sync.js +135 -0
  347. package/dist/commands/worktree-sync.js.map +1 -0
  348. package/dist/index.d.ts.map +1 -1
  349. package/dist/index.js +189 -14
  350. package/dist/index.js.map +1 -1
  351. package/dist/lib/agent-session.d.ts +12 -0
  352. package/dist/lib/agent-session.d.ts.map +1 -1
  353. package/dist/lib/agent-session.js +189 -42
  354. package/dist/lib/agent-session.js.map +1 -1
  355. package/dist/lib/catalog.d.ts +321 -2
  356. package/dist/lib/catalog.d.ts.map +1 -1
  357. package/dist/lib/catalog.js +490 -11
  358. package/dist/lib/catalog.js.map +1 -1
  359. package/dist/lib/claude.d.ts +65 -23
  360. package/dist/lib/claude.d.ts.map +1 -1
  361. package/dist/lib/claude.js +335 -19
  362. package/dist/lib/claude.js.map +1 -1
  363. package/dist/lib/cli-build.d.ts +172 -0
  364. package/dist/lib/cli-build.d.ts.map +1 -0
  365. package/dist/lib/cli-build.js +654 -0
  366. package/dist/lib/cli-build.js.map +1 -0
  367. package/dist/lib/code-context-file.d.ts +109 -0
  368. package/dist/lib/code-context-file.d.ts.map +1 -0
  369. package/dist/lib/code-context-file.js +219 -0
  370. package/dist/lib/code-context-file.js.map +1 -0
  371. package/dist/lib/code-context-file.test.d.ts +19 -0
  372. package/dist/lib/code-context-file.test.d.ts.map +1 -0
  373. package/dist/lib/code-context-file.test.js +202 -0
  374. package/dist/lib/code-context-file.test.js.map +1 -0
  375. package/dist/lib/config-fidelity.d.ts +40 -20
  376. package/dist/lib/config-fidelity.d.ts.map +1 -1
  377. package/dist/lib/config-fidelity.js +144 -56
  378. package/dist/lib/config-fidelity.js.map +1 -1
  379. package/dist/lib/coordination-client.d.ts +73 -0
  380. package/dist/lib/coordination-client.d.ts.map +1 -1
  381. package/dist/lib/coordination-client.js +180 -8
  382. package/dist/lib/coordination-client.js.map +1 -1
  383. package/dist/lib/coordination-token.d.ts +304 -6
  384. package/dist/lib/coordination-token.d.ts.map +1 -1
  385. package/dist/lib/coordination-token.js +851 -52
  386. package/dist/lib/coordination-token.js.map +1 -1
  387. package/dist/lib/decision-check.d.ts +48 -0
  388. package/dist/lib/decision-check.d.ts.map +1 -0
  389. package/dist/lib/decision-check.js +149 -0
  390. package/dist/lib/decision-check.js.map +1 -0
  391. package/dist/lib/fleet-data.d.ts +34 -0
  392. package/dist/lib/fleet-data.d.ts.map +1 -1
  393. package/dist/lib/fleet-data.js +37 -9
  394. package/dist/lib/fleet-data.js.map +1 -1
  395. package/dist/lib/fleet-render.js +1 -1
  396. package/dist/lib/fleet-render.js.map +1 -1
  397. package/dist/lib/git-identity.d.ts +302 -0
  398. package/dist/lib/git-identity.d.ts.map +1 -0
  399. package/dist/lib/git-identity.js +546 -0
  400. package/dist/lib/git-identity.js.map +1 -0
  401. package/dist/lib/harness/console/announce.d.ts +45 -0
  402. package/dist/lib/harness/console/announce.d.ts.map +1 -0
  403. package/dist/lib/harness/console/announce.js +48 -0
  404. package/dist/lib/harness/console/announce.js.map +1 -0
  405. package/dist/lib/harness/console/announce.test.d.ts +2 -0
  406. package/dist/lib/harness/console/announce.test.d.ts.map +1 -0
  407. package/dist/lib/harness/console/announce.test.js +104 -0
  408. package/dist/lib/harness/console/announce.test.js.map +1 -0
  409. package/dist/lib/harness/console/by-session.d.ts +50 -0
  410. package/dist/lib/harness/console/by-session.d.ts.map +1 -0
  411. package/dist/lib/harness/console/by-session.js +83 -0
  412. package/dist/lib/harness/console/by-session.js.map +1 -0
  413. package/dist/lib/harness/console/by-session.test.d.ts +2 -0
  414. package/dist/lib/harness/console/by-session.test.d.ts.map +1 -0
  415. package/dist/lib/harness/console/by-session.test.js +86 -0
  416. package/dist/lib/harness/console/by-session.test.js.map +1 -0
  417. package/dist/lib/harness/console/console.d.ts +67 -0
  418. package/dist/lib/harness/console/console.d.ts.map +1 -0
  419. package/dist/lib/harness/console/console.js +373 -0
  420. package/dist/lib/harness/console/console.js.map +1 -0
  421. package/dist/lib/harness/console/console.test.d.ts +2 -0
  422. package/dist/lib/harness/console/console.test.d.ts.map +1 -0
  423. package/dist/lib/harness/console/console.test.js +269 -0
  424. package/dist/lib/harness/console/console.test.js.map +1 -0
  425. package/dist/lib/harness/console/decision-request.d.ts +32 -0
  426. package/dist/lib/harness/console/decision-request.d.ts.map +1 -0
  427. package/dist/lib/harness/console/decision-request.js +112 -0
  428. package/dist/lib/harness/console/decision-request.js.map +1 -0
  429. package/dist/lib/harness/console/decision-request.test.d.ts +2 -0
  430. package/dist/lib/harness/console/decision-request.test.d.ts.map +1 -0
  431. package/dist/lib/harness/console/decision-request.test.js +75 -0
  432. package/dist/lib/harness/console/decision-request.test.js.map +1 -0
  433. package/dist/lib/harness/console/fixtures.d.ts +16 -0
  434. package/dist/lib/harness/console/fixtures.d.ts.map +1 -0
  435. package/dist/lib/harness/console/fixtures.js +49 -0
  436. package/dist/lib/harness/console/fixtures.js.map +1 -0
  437. package/dist/lib/harness/console/index.d.ts +15 -0
  438. package/dist/lib/harness/console/index.d.ts.map +1 -0
  439. package/dist/lib/harness/console/index.js +14 -0
  440. package/dist/lib/harness/console/index.js.map +1 -0
  441. package/dist/lib/harness/console/rank.d.ts +73 -0
  442. package/dist/lib/harness/console/rank.d.ts.map +1 -0
  443. package/dist/lib/harness/console/rank.js +113 -0
  444. package/dist/lib/harness/console/rank.js.map +1 -0
  445. package/dist/lib/harness/console/rank.test.d.ts +2 -0
  446. package/dist/lib/harness/console/rank.test.d.ts.map +1 -0
  447. package/dist/lib/harness/console/rank.test.js +124 -0
  448. package/dist/lib/harness/console/rank.test.js.map +1 -0
  449. package/dist/lib/harness/console/render.d.ts +22 -0
  450. package/dist/lib/harness/console/render.d.ts.map +1 -0
  451. package/dist/lib/harness/console/render.js +65 -0
  452. package/dist/lib/harness/console/render.js.map +1 -0
  453. package/dist/lib/harness/console/render.test.d.ts +2 -0
  454. package/dist/lib/harness/console/render.test.d.ts.map +1 -0
  455. package/dist/lib/harness/console/render.test.js +96 -0
  456. package/dist/lib/harness/console/render.test.js.map +1 -0
  457. package/dist/lib/harness/console/replay-cli.test.d.ts +2 -0
  458. package/dist/lib/harness/console/replay-cli.test.d.ts.map +1 -0
  459. package/dist/lib/harness/console/replay-cli.test.js +96 -0
  460. package/dist/lib/harness/console/replay-cli.test.js.map +1 -0
  461. package/dist/lib/harness/console/replay.d.ts +36 -0
  462. package/dist/lib/harness/console/replay.d.ts.map +1 -0
  463. package/dist/lib/harness/console/replay.js +59 -0
  464. package/dist/lib/harness/console/replay.js.map +1 -0
  465. package/dist/lib/harness/console/replay.test.d.ts +2 -0
  466. package/dist/lib/harness/console/replay.test.d.ts.map +1 -0
  467. package/dist/lib/harness/console/replay.test.js +81 -0
  468. package/dist/lib/harness/console/replay.test.js.map +1 -0
  469. package/dist/lib/harness/console/turn-reader.d.ts +50 -0
  470. package/dist/lib/harness/console/turn-reader.d.ts.map +1 -0
  471. package/dist/lib/harness/console/turn-reader.js +184 -0
  472. package/dist/lib/harness/console/turn-reader.js.map +1 -0
  473. package/dist/lib/harness/console/turn-reader.test.d.ts +2 -0
  474. package/dist/lib/harness/console/turn-reader.test.d.ts.map +1 -0
  475. package/dist/lib/harness/console/turn-reader.test.js +108 -0
  476. package/dist/lib/harness/console/turn-reader.test.js.map +1 -0
  477. package/dist/lib/harness/console/types.d.ts +109 -0
  478. package/dist/lib/harness/console/types.d.ts.map +1 -0
  479. package/dist/lib/harness/console/types.js +2 -0
  480. package/dist/lib/harness/console/types.js.map +1 -0
  481. package/dist/lib/harness/contract.d.ts +229 -0
  482. package/dist/lib/harness/contract.d.ts.map +1 -0
  483. package/dist/lib/harness/contract.js +22 -0
  484. package/dist/lib/harness/contract.js.map +1 -0
  485. package/dist/lib/harness/credential.d.ts +23 -0
  486. package/dist/lib/harness/credential.d.ts.map +1 -0
  487. package/dist/lib/harness/credential.js +81 -0
  488. package/dist/lib/harness/credential.js.map +1 -0
  489. package/dist/lib/harness/credential.test.d.ts +2 -0
  490. package/dist/lib/harness/credential.test.d.ts.map +1 -0
  491. package/dist/lib/harness/credential.test.js +74 -0
  492. package/dist/lib/harness/credential.test.js.map +1 -0
  493. package/dist/lib/harness/evaluate.d.ts +390 -0
  494. package/dist/lib/harness/evaluate.d.ts.map +1 -0
  495. package/dist/lib/harness/evaluate.js +988 -0
  496. package/dist/lib/harness/evaluate.js.map +1 -0
  497. package/dist/lib/harness/evaluate.test.d.ts +2 -0
  498. package/dist/lib/harness/evaluate.test.d.ts.map +1 -0
  499. package/dist/lib/harness/evaluate.test.js +621 -0
  500. package/dist/lib/harness/evaluate.test.js.map +1 -0
  501. package/dist/lib/harness/extract.d.ts +152 -0
  502. package/dist/lib/harness/extract.d.ts.map +1 -0
  503. package/dist/lib/harness/extract.js +1601 -0
  504. package/dist/lib/harness/extract.js.map +1 -0
  505. package/dist/lib/harness/gate.d.ts +48 -0
  506. package/dist/lib/harness/gate.d.ts.map +1 -0
  507. package/dist/lib/harness/gate.js +60 -0
  508. package/dist/lib/harness/gate.js.map +1 -0
  509. package/dist/lib/harness/gate.test.d.ts +2 -0
  510. package/dist/lib/harness/gate.test.d.ts.map +1 -0
  511. package/dist/lib/harness/gate.test.js +111 -0
  512. package/dist/lib/harness/gate.test.js.map +1 -0
  513. package/dist/lib/harness/self-check.d.ts +59 -0
  514. package/dist/lib/harness/self-check.d.ts.map +1 -0
  515. package/dist/lib/harness/self-check.js +56 -0
  516. package/dist/lib/harness/self-check.js.map +1 -0
  517. package/dist/lib/harness/self-check.test.d.ts +2 -0
  518. package/dist/lib/harness/self-check.test.d.ts.map +1 -0
  519. package/dist/lib/harness/self-check.test.js +43 -0
  520. package/dist/lib/harness/self-check.test.js.map +1 -0
  521. package/dist/lib/harness/transcript.d.ts +65 -0
  522. package/dist/lib/harness/transcript.d.ts.map +1 -0
  523. package/dist/lib/harness/transcript.js +144 -0
  524. package/dist/lib/harness/transcript.js.map +1 -0
  525. package/dist/lib/harness/transcript.test.d.ts +2 -0
  526. package/dist/lib/harness/transcript.test.d.ts.map +1 -0
  527. package/dist/lib/harness/transcript.test.js +118 -0
  528. package/dist/lib/harness/transcript.test.js.map +1 -0
  529. package/dist/lib/harness-wiring.d.ts +145 -0
  530. package/dist/lib/harness-wiring.d.ts.map +1 -0
  531. package/dist/lib/harness-wiring.js +280 -0
  532. package/dist/lib/harness-wiring.js.map +1 -0
  533. package/dist/lib/hook-staleness.d.ts +95 -0
  534. package/dist/lib/hook-staleness.d.ts.map +1 -0
  535. package/dist/lib/hook-staleness.js +304 -0
  536. package/dist/lib/hook-staleness.js.map +1 -0
  537. package/dist/lib/jsonl.d.ts +270 -2
  538. package/dist/lib/jsonl.d.ts.map +1 -1
  539. package/dist/lib/jsonl.js +472 -76
  540. package/dist/lib/jsonl.js.map +1 -1
  541. package/dist/lib/memory.d.ts +350 -23
  542. package/dist/lib/memory.d.ts.map +1 -1
  543. package/dist/lib/memory.js +751 -38
  544. package/dist/lib/memory.js.map +1 -1
  545. package/dist/lib/otel-env.d.ts +90 -0
  546. package/dist/lib/otel-env.d.ts.map +1 -0
  547. package/dist/lib/otel-env.js +98 -0
  548. package/dist/lib/otel-env.js.map +1 -0
  549. package/dist/lib/project-config.d.ts +167 -0
  550. package/dist/lib/project-config.d.ts.map +1 -1
  551. package/dist/lib/project-config.js +321 -5
  552. package/dist/lib/project-config.js.map +1 -1
  553. package/dist/lib/projects-registry.d.ts +82 -0
  554. package/dist/lib/projects-registry.d.ts.map +1 -0
  555. package/dist/lib/projects-registry.js +145 -0
  556. package/dist/lib/projects-registry.js.map +1 -0
  557. package/dist/lib/projects-registry.test.d.ts +2 -0
  558. package/dist/lib/projects-registry.test.d.ts.map +1 -0
  559. package/dist/lib/projects-registry.test.js +176 -0
  560. package/dist/lib/projects-registry.test.js.map +1 -0
  561. package/dist/lib/repo-guard-scope.d.ts +126 -0
  562. package/dist/lib/repo-guard-scope.d.ts.map +1 -0
  563. package/dist/lib/repo-guard-scope.js +249 -0
  564. package/dist/lib/repo-guard-scope.js.map +1 -0
  565. package/dist/lib/report-format.d.ts +132 -0
  566. package/dist/lib/report-format.d.ts.map +1 -0
  567. package/dist/lib/report-format.js +317 -0
  568. package/dist/lib/report-format.js.map +1 -0
  569. package/dist/lib/roster.d.ts +87 -0
  570. package/dist/lib/roster.d.ts.map +1 -0
  571. package/dist/lib/roster.js +332 -0
  572. package/dist/lib/roster.js.map +1 -0
  573. package/dist/lib/roster.test.d.ts +15 -0
  574. package/dist/lib/roster.test.d.ts.map +1 -0
  575. package/dist/lib/roster.test.js +299 -0
  576. package/dist/lib/roster.test.js.map +1 -0
  577. package/dist/lib/runtime/codex-adapter.d.ts.map +1 -1
  578. package/dist/lib/runtime/codex-adapter.js +5 -0
  579. package/dist/lib/runtime/codex-adapter.js.map +1 -1
  580. package/dist/lib/runtime/codex-config.d.ts +129 -0
  581. package/dist/lib/runtime/codex-config.d.ts.map +1 -1
  582. package/dist/lib/runtime/codex-config.js +225 -0
  583. package/dist/lib/runtime/codex-config.js.map +1 -1
  584. package/dist/lib/runtime/identity.d.ts +72 -0
  585. package/dist/lib/runtime/identity.d.ts.map +1 -0
  586. package/dist/lib/runtime/identity.js +128 -0
  587. package/dist/lib/runtime/identity.js.map +1 -0
  588. package/dist/lib/standards.d.ts +99 -0
  589. package/dist/lib/standards.d.ts.map +1 -0
  590. package/dist/lib/standards.js +201 -0
  591. package/dist/lib/standards.js.map +1 -0
  592. package/dist/lib/team-identity.d.ts +134 -0
  593. package/dist/lib/team-identity.d.ts.map +1 -0
  594. package/dist/lib/team-identity.js +193 -0
  595. package/dist/lib/team-identity.js.map +1 -0
  596. package/dist/lib/templates.d.ts +29 -2
  597. package/dist/lib/templates.d.ts.map +1 -1
  598. package/dist/lib/templates.js +1042 -82
  599. package/dist/lib/templates.js.map +1 -1
  600. package/dist/lib/terminal-ui.d.ts.map +1 -1
  601. package/dist/lib/terminal-ui.js +12 -1
  602. package/dist/lib/terminal-ui.js.map +1 -1
  603. package/dist/lib/test-project.d.ts +95 -0
  604. package/dist/lib/test-project.d.ts.map +1 -0
  605. package/dist/lib/test-project.js +103 -0
  606. package/dist/lib/test-project.js.map +1 -0
  607. package/dist/lib/vault.d.ts +60 -0
  608. package/dist/lib/vault.d.ts.map +1 -0
  609. package/dist/lib/vault.js +102 -0
  610. package/dist/lib/vault.js.map +1 -0
  611. package/dist/lib/vault.test.d.ts +17 -0
  612. package/dist/lib/vault.test.d.ts.map +1 -0
  613. package/dist/lib/vault.test.js +169 -0
  614. package/dist/lib/vault.test.js.map +1 -0
  615. package/dist/lib/window-view.d.ts +63 -0
  616. package/dist/lib/window-view.d.ts.map +1 -0
  617. package/dist/lib/window-view.js +217 -0
  618. package/dist/lib/window-view.js.map +1 -0
  619. package/dist/lib/window-view.test.d.ts +2 -0
  620. package/dist/lib/window-view.test.d.ts.map +1 -0
  621. package/dist/lib/window-view.test.js +168 -0
  622. package/dist/lib/window-view.test.js.map +1 -0
  623. package/dist/lib/worktree-sync.d.ts +439 -0
  624. package/dist/lib/worktree-sync.d.ts.map +1 -0
  625. package/dist/lib/worktree-sync.js +1167 -0
  626. package/dist/lib/worktree-sync.js.map +1 -0
  627. package/dist/lib/worktree.d.ts +58 -1
  628. package/dist/lib/worktree.d.ts.map +1 -1
  629. package/dist/lib/worktree.js +91 -4
  630. package/dist/lib/worktree.js.map +1 -1
  631. package/dist/standards/decision-request.md +98 -0
  632. package/dist/standards/fail-loud-standard.md +495 -0
  633. package/dist/standards/po-communication-standard.md +126 -0
  634. package/dist/standards/report-format.md +128 -0
  635. package/dist/standards/writing-standard.md +382 -0
  636. package/package.json +3 -2
  637. package/scripts/generate-claude.mjs +119 -46
@@ -0,0 +1,1601 @@
1
+ /**
2
+ * Attribute extraction: given a tool call, say what it would touch.
3
+ *
4
+ * This is the extractor half of `contract.ts`. It owns shell and filesystem
5
+ * semantics and knows nothing about policy. It never decides. It resolves.
6
+ *
7
+ * tool call ──▶ extract() ──▶ Attributes ──▶ evaluate() ──▶ Decision
8
+ *
9
+ * Why it exists in this shape: `docs/guard-coverage.md` measured the shipped
10
+ * guards at 28.6% (502 refused of 1,756) against 2,043 real commands. The
11
+ * single largest hole is that `worktree-guard.sh` inspects `cd` targets and git
12
+ * verbs and never inspects the target of a write, so `rm`, `cp`, `mkdir`,
13
+ * redirects and `sed -i` outside the worktree all pass: 0 of 146. A rule can
14
+ * only bind a write target if something resolves it first. That is this file.
15
+ *
16
+ * Three properties it holds to.
17
+ *
18
+ * 1. `uncertain` is a result, not a fallback. When a target cannot be
19
+ * determined the extractor says so and says why in `uncertainReason`. It
20
+ * never returns empty `writes` for a command it did not understand, which is
21
+ * the #5434 shape where unparseable input looked like safety.
22
+ *
23
+ * 2. Relative paths resolve against the working directory, and the working
24
+ * directory follows `cd` through `&&`, `;`, `|` and subshells. A guard that
25
+ * reads only absolute paths misses `cd /workspace/shared && rm note.md`.
26
+ *
27
+ * 3. `reads[0]` is the effective working directory of the call. A boundary rule
28
+ * needs to tell "the shell moved to /workspace" from "the command read
29
+ * /home/vscode/.claude/settings.json", and `Attributes` has one array for
30
+ * both. The convention is: `reads[0]` is where the call worked, and
31
+ * `reads[1..]` are files and directories it read. When the call moved, the
32
+ * last directory it moved to is `reads[0]`; otherwise the entry cwd is.
33
+ *
34
+ * Known limit, stated rather than hidden: `Attributes` carries one `operation`
35
+ * and one call can perform several. `operation` names the most consequential
36
+ * one by the severity order in `severityOf()`. `writes` and `reads` are the
37
+ * union across all of them, so no path is lost, but a rule keyed on a verb sees
38
+ * only the dominant verb. `extractOperations()` returns the full list for
39
+ * callers that need it. Section 6 of the coverage report measures what the fold
40
+ * costs.
41
+ */
42
+ // ── 1. Paths ─────────────────────────────────────────────────────────────────
43
+ /** Normalise a POSIX path textually. No filesystem access: the extractor runs
44
+ * inside a permission callback and must not stat anything. */
45
+ export function normalisePath(p) {
46
+ const absolute = p.startsWith('/');
47
+ const out = [];
48
+ for (const part of p.split('/')) {
49
+ if (part === '' || part === '.')
50
+ continue;
51
+ if (part === '..') {
52
+ if (out.length && out[out.length - 1] !== '..')
53
+ out.pop();
54
+ else if (!absolute)
55
+ out.push('..');
56
+ continue;
57
+ }
58
+ out.push(part);
59
+ }
60
+ const joined = out.join('/');
61
+ if (absolute)
62
+ return '/' + joined;
63
+ return joined || '.';
64
+ }
65
+ /**
66
+ * Absolute on the host in a form the POSIX rules above do not model: a Windows
67
+ * drive letter, or a UNC share.
68
+ *
69
+ * This exists because of what happened without it. A live session on a Windows
70
+ * host wrote to `C:/…/other-team/should-not-exist.md` from a worktree at
71
+ * `C:/…/worktree`. `C:/…` does not start with `/`, so it was read as relative
72
+ * and joined onto the working directory, producing
73
+ * `C:/…/worktree/C:/…/other-team/should-not-exist.md`. That path starts with
74
+ * the worktree root, so the boundary rule read an out-of-scope write as an
75
+ * in-scope one and allowed it, with `uncertain` false. The write happened.
76
+ *
77
+ * A path the extractor cannot parse must never resolve to a path inside the
78
+ * scope it is being checked against. That is #5434 in a second place: an input
79
+ * the tool does not model rendering as permission.
80
+ */
81
+ export function hostAbsolute(p) {
82
+ return /^[A-Za-z]:[\\/]/.test(p) || p.startsWith('\\\\');
83
+ }
84
+ /**
85
+ * Normalise a host-absolute path, keeping its root prefix.
86
+ *
87
+ * Separators fold to `/` so the evaluator's `isInside` can compare them, which
88
+ * it already does: `evaluate.ts` normalizePath folds backslashes and leaves a
89
+ * `C:` prefix intact, so a drive path arrives there comparable.
90
+ */
91
+ function normaliseHostPath(p) {
92
+ const slashed = p.replace(/\\/g, '/');
93
+ const unc = slashed.startsWith('//');
94
+ const drive = unc ? null : /^([A-Za-z]:)(.*)$/.exec(slashed);
95
+ const prefix = unc ? '//' : drive ? drive[1] : '';
96
+ const rest = unc ? slashed.slice(2) : drive ? drive[2] : slashed;
97
+ const out = [];
98
+ for (const part of rest.split('/')) {
99
+ if (part === '' || part === '.')
100
+ continue;
101
+ if (part === '..') {
102
+ out.pop();
103
+ continue;
104
+ }
105
+ out.push(part);
106
+ }
107
+ // A UNC root is the two leading slashes themselves, so it takes no third.
108
+ return unc ? `${prefix}${out.join('/')}` : `${prefix}/${out.join('/')}`;
109
+ }
110
+ /** Resolve `p` against `cwd`. Absolute paths pass through normalised. */
111
+ export function resolvePath(cwd, p) {
112
+ if (hostAbsolute(p))
113
+ return normaliseHostPath(p);
114
+ if (p.startsWith('/'))
115
+ return normalisePath(p);
116
+ return normalisePath(cwd + '/' + p);
117
+ }
118
+ function baseName(p) {
119
+ const parts = normalisePath(p).split('/');
120
+ return parts[parts.length - 1] || '/';
121
+ }
122
+ /** The literal directory prefix of a glob, which is the most a textual
123
+ * extractor can know about `rm -rf /workspace/shared/*`. */
124
+ function globPrefix(p) {
125
+ const cut = p.search(/[*?[]/);
126
+ if (cut < 0)
127
+ return p;
128
+ const slash = p.lastIndexOf('/', cut);
129
+ return slash <= 0 ? '/' : p.slice(0, slash);
130
+ }
131
+ export function wordRaw(w) {
132
+ return w.parts.map((p) => (p.kind === 'lit' ? p.text : p.raw)).join('');
133
+ }
134
+ function wordIsPlain(w) {
135
+ return w.parts.every((p) => p.kind === 'lit');
136
+ }
137
+ function wordHasGlob(w) {
138
+ return w.parts.some((p) => p.kind === 'lit' && !p.quoted && /[*?[]/.test(p.text));
139
+ }
140
+ /** Command substitution bodies inside a word, so the extractor can look at what
141
+ * `$(rm -rf /x)` would do rather than only note that it cannot expand it. */
142
+ function wordSubstitutions(w) {
143
+ return w.parts.filter((p) => p.kind === 'cmd').map((p) => p.body);
144
+ }
145
+ const REDIR = /^(\d*)(&>>|&>|>>|>\||>&|>|<<<|<<-|<<|<&|<)$/;
146
+ /** Find the index just past a balanced `(`…`)` or `${`…`}` run, respecting
147
+ * quotes so that `$(echo ")")` does not end early. */
148
+ function matchClose(src, start, open, close) {
149
+ let depth = 0;
150
+ let i = start;
151
+ while (i < src.length) {
152
+ const c = src[i];
153
+ if (c === '\\') {
154
+ i += 2;
155
+ continue;
156
+ }
157
+ if (c === "'") {
158
+ const j = src.indexOf("'", i + 1);
159
+ i = j < 0 ? src.length : j + 1;
160
+ continue;
161
+ }
162
+ if (c === '"') {
163
+ i += 1;
164
+ while (i < src.length && src[i] !== '"') {
165
+ if (src[i] === '\\')
166
+ i += 1;
167
+ i += 1;
168
+ }
169
+ i += 1;
170
+ continue;
171
+ }
172
+ if (c === open)
173
+ depth += 1;
174
+ else if (c === close) {
175
+ depth -= 1;
176
+ if (depth === 0)
177
+ return i + 1;
178
+ }
179
+ i += 1;
180
+ }
181
+ return src.length;
182
+ }
183
+ /** Read one expansion starting at `$` or a backtick. */
184
+ function readExpansion(src, i) {
185
+ if (src[i] === '`') {
186
+ let j = i + 1;
187
+ while (j < src.length && src[j] !== '`') {
188
+ if (src[j] === '\\')
189
+ j += 1;
190
+ j += 1;
191
+ }
192
+ const raw = src.slice(i, Math.min(j + 1, src.length));
193
+ return { part: { kind: 'cmd', body: src.slice(i + 1, j), raw }, next: j + 1 };
194
+ }
195
+ // src[i] === '$'
196
+ const next = src[i + 1];
197
+ if (next === '(' && src[i + 2] === '(') {
198
+ const end = matchClose(src, i + 2, '(', ')');
199
+ const end2 = end < src.length && src[end] === ')' ? end + 1 : end;
200
+ const raw = src.slice(i, end2);
201
+ return { part: { kind: 'var', name: '', raw }, next: end2 };
202
+ }
203
+ if (next === '(') {
204
+ const end = matchClose(src, i + 1, '(', ')');
205
+ const raw = src.slice(i, end);
206
+ return { part: { kind: 'cmd', body: src.slice(i + 2, end - 1), raw }, next: end };
207
+ }
208
+ if (next === '{') {
209
+ const end = matchClose(src, i + 1, '{', '}');
210
+ const raw = src.slice(i, end);
211
+ const inner = src.slice(i + 2, end - 1);
212
+ const name = /^[A-Za-z_][A-Za-z0-9_]*$/.test(inner) ? inner : '';
213
+ return { part: { kind: 'var', name, raw }, next: end };
214
+ }
215
+ const m = /^[A-Za-z_][A-Za-z0-9_]*/.exec(src.slice(i + 1));
216
+ if (m)
217
+ return { part: { kind: 'var', name: m[0], raw: '$' + m[0] }, next: i + 1 + m[0].length };
218
+ // $?, $$, $1, $@ and friends. Named so the reason can say which.
219
+ return { part: { kind: 'var', name: '', raw: src.slice(i, i + 2) }, next: i + 2 };
220
+ }
221
+ export function tokenize(src) {
222
+ const toks = [];
223
+ let parts = [];
224
+ let started = false;
225
+ const flush = () => {
226
+ if (!started)
227
+ return;
228
+ toks.push({ kind: 'word', word: { parts } });
229
+ parts = [];
230
+ started = false;
231
+ };
232
+ const lit = (text, quoted) => {
233
+ started = true;
234
+ const last = parts[parts.length - 1];
235
+ if (last && last.kind === 'lit' && last.quoted === quoted)
236
+ last.text += text;
237
+ else
238
+ parts.push({ kind: 'lit', text, quoted });
239
+ };
240
+ let i = 0;
241
+ while (i < src.length) {
242
+ const c = src[i];
243
+ if (c === '\\' && src[i + 1] === '\n') {
244
+ i += 2;
245
+ continue;
246
+ }
247
+ if (c === '\\') {
248
+ lit(src[i + 1] ?? '', true);
249
+ i += 2;
250
+ continue;
251
+ }
252
+ if (c === "'") {
253
+ const j = src.indexOf("'", i + 1);
254
+ const end = j < 0 ? src.length : j;
255
+ lit(src.slice(i + 1, end), true);
256
+ i = end + 1;
257
+ continue;
258
+ }
259
+ if (c === '"') {
260
+ i += 1;
261
+ started = true;
262
+ while (i < src.length && src[i] !== '"') {
263
+ if (src[i] === '\\') {
264
+ const n = src[i + 1];
265
+ if (n === '"' || n === '\\' || n === '$' || n === '`') {
266
+ lit(n, true);
267
+ i += 2;
268
+ continue;
269
+ }
270
+ lit('\\', true);
271
+ i += 1;
272
+ continue;
273
+ }
274
+ if (src[i] === '$' || src[i] === '`') {
275
+ const r = readExpansion(src, i);
276
+ parts.push(r.part);
277
+ i = r.next;
278
+ continue;
279
+ }
280
+ lit(src[i], true);
281
+ i += 1;
282
+ }
283
+ i += 1;
284
+ continue;
285
+ }
286
+ if (c === '$' || c === '`') {
287
+ const r = readExpansion(src, i);
288
+ started = true;
289
+ parts.push(r.part);
290
+ i = r.next;
291
+ continue;
292
+ }
293
+ // Process substitution. The word becomes a /dev/fd path at run time, so the
294
+ // target cannot be named, but the inner command is still worth reading.
295
+ if ((c === '<' || c === '>') && src[i + 1] === '(') {
296
+ const end = matchClose(src, i + 1, '(', ')');
297
+ started = true;
298
+ parts.push({ kind: 'cmd', body: src.slice(i + 2, end - 1), raw: src.slice(i, end) });
299
+ i = end;
300
+ continue;
301
+ }
302
+ if (c === '#' && !started && (i === 0 || /\s/.test(src[i - 1]))) {
303
+ while (i < src.length && src[i] !== '\n')
304
+ i += 1;
305
+ continue;
306
+ }
307
+ if (c === '\n') {
308
+ flush();
309
+ toks.push({ kind: 'op', text: '\n' });
310
+ i += 1;
311
+ continue;
312
+ }
313
+ if (c === ' ' || c === '\t' || c === '\r') {
314
+ flush();
315
+ i += 1;
316
+ continue;
317
+ }
318
+ // Redirections, including a leading file descriptor: `2>`, `1>&2`.
319
+ if (c === '<' || c === '>' || (c === '&' && (src[i + 1] === '>'))) {
320
+ let fd = '';
321
+ if (started && parts.length === 1 && parts[0].kind === 'lit' && !parts[0].quoted && /^\d+$/.test(parts[0].text)) {
322
+ fd = parts[0].text;
323
+ parts = [];
324
+ started = false;
325
+ }
326
+ flush();
327
+ let op;
328
+ if (src.startsWith('&>>', i))
329
+ op = '&>>';
330
+ else if (src.startsWith('&>', i))
331
+ op = '&>';
332
+ else if (src.startsWith('<<<', i))
333
+ op = '<<<';
334
+ else if (src.startsWith('<<-', i))
335
+ op = '<<-';
336
+ else if (src.startsWith('<<', i))
337
+ op = '<<';
338
+ else if (src.startsWith('>>', i))
339
+ op = '>>';
340
+ else if (src.startsWith('>|', i))
341
+ op = '>|';
342
+ else if (src.startsWith('>&', i))
343
+ op = '>&';
344
+ else if (src.startsWith('<&', i))
345
+ op = '<&';
346
+ else
347
+ op = c;
348
+ toks.push({ kind: 'op', text: fd + op });
349
+ i += op.length;
350
+ continue;
351
+ }
352
+ if (src.startsWith('&&', i)) {
353
+ flush();
354
+ toks.push({ kind: 'op', text: '&&' });
355
+ i += 2;
356
+ continue;
357
+ }
358
+ if (src.startsWith('||', i)) {
359
+ flush();
360
+ toks.push({ kind: 'op', text: '||' });
361
+ i += 2;
362
+ continue;
363
+ }
364
+ if (src.startsWith(';;', i)) {
365
+ flush();
366
+ toks.push({ kind: 'op', text: ';;' });
367
+ i += 2;
368
+ continue;
369
+ }
370
+ if (src.startsWith('|&', i)) {
371
+ flush();
372
+ toks.push({ kind: 'op', text: '|' });
373
+ i += 2;
374
+ continue;
375
+ }
376
+ if (c === '|' || c === ';' || c === '&' || c === '(' || c === ')') {
377
+ flush();
378
+ toks.push({ kind: 'op', text: c });
379
+ i += 1;
380
+ continue;
381
+ }
382
+ lit(c, false);
383
+ i += 1;
384
+ }
385
+ flush();
386
+ return toks;
387
+ }
388
+ // ── 4. Heredocs ──────────────────────────────────────────────────────────────
389
+ //
390
+ // A heredoc body is data, not commands. Left in place it is read as a command
391
+ // list, which is version 2 of the defect recorded in
392
+ // `evidence-tools/transcripts/read.mjs`: an `mx memories create --content "…"`
393
+ // body counted as attempts. Bodies come out before tokenising and go back in at
394
+ // the redirect that consumes them.
395
+ export function extractHeredocs(src) {
396
+ const lines = src.split('\n');
397
+ const out = [];
398
+ const bodies = [];
399
+ let queue = [];
400
+ let collecting = null;
401
+ let buf = [];
402
+ for (const line of lines) {
403
+ if (collecting) {
404
+ const probe = collecting.strip ? line.replace(/^\t+/, '') : line;
405
+ if (probe.trim() === collecting.delim) {
406
+ bodies[collecting.index] = buf.join('\n');
407
+ buf = [];
408
+ collecting = queue.shift() ?? null;
409
+ continue;
410
+ }
411
+ buf.push(line);
412
+ continue;
413
+ }
414
+ out.push(line);
415
+ const starts = [...line.matchAll(/<<(-?)\s*(["']?)([A-Za-z_][A-Za-z0-9_]*)\2/g)]
416
+ .filter((m) => line[m.index + 2] !== '<');
417
+ for (const m of starts) {
418
+ const entry = { delim: m[3], strip: m[1] === '-', index: bodies.length };
419
+ bodies.push('');
420
+ queue.push(entry);
421
+ }
422
+ if (queue.length)
423
+ collecting = queue.shift() ?? null;
424
+ }
425
+ if (collecting)
426
+ bodies[collecting.index] = buf.join('\n');
427
+ return { text: out.join('\n'), bodies };
428
+ }
429
+ /** Reserved words that introduce or close a compound and carry no target. */
430
+ const RESERVED = new Set(['if', 'then', 'elif', 'else', 'fi', 'do', 'done', 'while', 'until', '!', 'time', 'function', 'esac', 'in']);
431
+ const SEPARATORS = new Set([';', '&&', '||', '&', '\n', ';;']);
432
+ export function parse(toks) {
433
+ let i = 0;
434
+ function parseList(closers) {
435
+ const items = [];
436
+ for (;;) {
437
+ while (i < toks.length) {
438
+ const t = toks[i];
439
+ if (t.kind === 'op' && SEPARATORS.has(t.text)) {
440
+ i += 1;
441
+ continue;
442
+ }
443
+ break;
444
+ }
445
+ if (i >= toks.length)
446
+ break;
447
+ const t = toks[i];
448
+ if (t.kind === 'op' && closers.has(t.text))
449
+ break;
450
+ if (t.kind === 'word' && closers.has(wordRaw(t.word)))
451
+ break;
452
+ const cmds = [];
453
+ for (;;) {
454
+ const cmd = parseCommand(closers);
455
+ if (cmd)
456
+ cmds.push(cmd);
457
+ const nxt = toks[i];
458
+ if (nxt && nxt.kind === 'op' && nxt.text === '|') {
459
+ i += 1;
460
+ continue;
461
+ }
462
+ break;
463
+ }
464
+ if (cmds.length)
465
+ items.push({ cmds });
466
+ const nxt = toks[i];
467
+ if (!nxt)
468
+ break;
469
+ if (nxt.kind === 'op' && SEPARATORS.has(nxt.text)) {
470
+ i += 1;
471
+ continue;
472
+ }
473
+ if (nxt.kind === 'op' && closers.has(nxt.text))
474
+ break;
475
+ if (nxt.kind === 'word' && closers.has(wordRaw(nxt.word)))
476
+ break;
477
+ // Anything else at this position is a token the grammar above does not
478
+ // model. Skipping one token keeps the walk finite.
479
+ i += 1;
480
+ }
481
+ return items;
482
+ }
483
+ function parseRedirects(redirs) {
484
+ const t = toks[i];
485
+ if (!t || t.kind !== 'op' || !REDIR.test(t.text))
486
+ return false;
487
+ i += 1;
488
+ const target = toks[i];
489
+ if (target && target.kind === 'word') {
490
+ redirs.push({ op: t.text, target: target.word });
491
+ i += 1;
492
+ }
493
+ else
494
+ redirs.push({ op: t.text, target: null });
495
+ return true;
496
+ }
497
+ function parseCommand(closers) {
498
+ const t = toks[i];
499
+ if (!t)
500
+ return null;
501
+ if (t.kind === 'op' && t.text === '(') {
502
+ i += 1;
503
+ const body = parseList(new Set([')']));
504
+ if (toks[i] && toks[i].kind === 'op' && toks[i].text === ')')
505
+ i += 1;
506
+ const redirs = [];
507
+ while (parseRedirects(redirs)) { /* consume */ }
508
+ return { kind: 'sub', body, redirs };
509
+ }
510
+ if (t.kind === 'word') {
511
+ const head = wordRaw(t.word);
512
+ if (head === '{') {
513
+ i += 1;
514
+ const body = parseList(new Set(['}']));
515
+ const close = toks[i];
516
+ if (close && close.kind === 'word' && wordRaw(close.word) === '}')
517
+ i += 1;
518
+ const redirs = [];
519
+ while (parseRedirects(redirs)) { /* consume */ }
520
+ return { kind: 'group', body, redirs };
521
+ }
522
+ // Function definition: `name() { … }`. The body does not run here, and a
523
+ // `cd` inside it must not leak, so it parses as a subshell.
524
+ const p1 = toks[i + 1];
525
+ const p2 = toks[i + 2];
526
+ if (p1 && p1.kind === 'op' && p1.text === '(' && p2 && p2.kind === 'op' && p2.text === ')') {
527
+ i += 3;
528
+ const inner = parseCommand(closers);
529
+ return { kind: 'sub', body: inner ? [{ cmds: [inner] }] : [], redirs: [] };
530
+ }
531
+ if (head === 'for' || head === 'select' || head === 'case') {
532
+ const words = [];
533
+ i += 1;
534
+ while (i < toks.length) {
535
+ const w = toks[i];
536
+ if (w.kind === 'op' && (SEPARATORS.has(w.text) || w.text === ')'))
537
+ break;
538
+ if (w.kind === 'word' && (wordRaw(w.word) === 'do' || wordRaw(w.word) === 'esac'))
539
+ break;
540
+ if (w.kind === 'word')
541
+ words.push(w.word);
542
+ i += 1;
543
+ }
544
+ return { kind: 'expr', words };
545
+ }
546
+ }
547
+ const words = [];
548
+ const redirs = [];
549
+ for (;;) {
550
+ const w = toks[i];
551
+ if (!w)
552
+ break;
553
+ if (w.kind === 'op') {
554
+ if (REDIR.test(w.text)) {
555
+ parseRedirects(redirs);
556
+ continue;
557
+ }
558
+ break;
559
+ }
560
+ const raw = wordRaw(w.word);
561
+ if (closers.has(raw))
562
+ break;
563
+ if (RESERVED.has(raw) && words.length === 0) {
564
+ i += 1;
565
+ continue;
566
+ }
567
+ if (raw === ';;')
568
+ break;
569
+ words.push(w.word);
570
+ i += 1;
571
+ }
572
+ if (!words.length && !redirs.length)
573
+ return null;
574
+ return { kind: 'simple', words, redirs };
575
+ }
576
+ return parseList(new Set());
577
+ }
578
+ function cloneState(s) {
579
+ return { cwd: s.cwd, prevCwd: s.prevCwd, vars: new Map(s.vars) };
580
+ }
581
+ // Commands that read and do not write. Everything outside this list and the
582
+ // writer table below is reported as `unknown` with a reason, because "I do not
583
+ // recognise this" and "this writes nothing" are different answers and #5434 is
584
+ // what happens when they are merged.
585
+ const READERS = new Set([
586
+ 'ls', 'cat', 'head', 'tail', 'less', 'more', 'wc', 'grep', 'egrep', 'fgrep', 'rg', 'ag', 'ack',
587
+ 'file', 'stat', 'du', 'df', 'pwd', 'echo', 'printf', 'true', 'false', 'test', '[', '[[',
588
+ 'which', 'type', 'whereis', 'basename', 'dirname', 'realpath', 'readlink', 'date', 'sleep',
589
+ 'sort', 'uniq', 'cut', 'tr', 'nl', 'fold', 'column', 'rev', 'paste', 'join', 'comm',
590
+ 'diff', 'cmp', 'md5sum', 'sha1sum', 'sha256sum', 'cksum', 'xxd', 'od', 'strings', 'hexdump',
591
+ 'seq', 'yes', 'id', 'whoami', 'hostname', 'uname', 'ps', 'pgrep', 'top', 'free', 'uptime',
592
+ 'jq', 'yq', 'awk', 'gawk', 'mawk', 'nproc', 'tty', 'locale', 'printenv', 'export', 'unset',
593
+ 'set', 'shopt', 'alias', 'read', 'wait', 'jobs', 'trap', 'return', 'break', 'continue', 'exit',
594
+ 'grep-ast', 'tree', 'lsof', 'getent', 'base64', 'tac', 'expr', 'let', 'declare', 'local', 'typeset',
595
+ ]);
596
+ /** Readers whose arguments are values rather than paths. */
597
+ const NO_PATH_READERS = new Set([
598
+ 'echo', 'printf', 'true', 'false', 'seq', 'yes', 'date', 'sleep', 'id', 'whoami', 'hostname',
599
+ 'uname', 'pwd', 'expr', 'let', 'export', 'unset', 'set', 'shopt', 'alias', 'declare', 'local',
600
+ 'typeset', 'read', 'wait', 'jobs', 'trap', 'return', 'break', 'continue', 'exit', 'nproc',
601
+ 'tty', 'locale', 'printenv', 'which', 'type', 'whereis', 'basename', 'dirname', 'test', '[', '[[',
602
+ 'ps', 'pgrep', 'top', 'free', 'uptime', 'df', 'base64',
603
+ ]);
604
+ /** Readers whose first positional is a pattern or a script, not a path. */
605
+ const PATTERN_FIRST = new Set(['grep', 'egrep', 'fgrep', 'rg', 'ag', 'ack', 'awk', 'gawk', 'mawk', 'jq', 'yq', 'tr']);
606
+ /** Wrappers that run another command. The prefix comes off and the rest is the
607
+ * real command. The number is how many positional arguments belong to the
608
+ * wrapper itself, which is 1 for `timeout 900 npx …`. */
609
+ const WRAPPERS = new Map([
610
+ ['sudo', 0], ['command', 0], ['builtin', 0], ['exec', 0], ['nohup', 0], ['time', 0],
611
+ ['nice', 0], ['ionice', 0], ['stdbuf', 0], ['setsid', 0], ['timeout', 1], ['doas', 0],
612
+ ['env', 0],
613
+ ]);
614
+ /** Wrapper options that take a separate value. `env -u CLAUDE_WORKTREE_PATH git
615
+ * -C /workspace fetch` is a real command from the corpus, and reading `-u` as
616
+ * the command loses the `git -C` that follows it. */
617
+ const WRAPPER_OPT_WITH_VALUE = {
618
+ env: new Set(['-u', '--unset', '-C', '--chdir', '-S', '--split-string']),
619
+ sudo: new Set(['-u', '--user', '-g', '--group', '-p', '--prompt', '-C', '--close-from', '-D', '--chdir']),
620
+ nice: new Set(['-n', '--adjustment']),
621
+ ionice: new Set(['-c', '-n', '-p']),
622
+ timeout: new Set(['-s', '--signal', '-k', '--kill-after']),
623
+ stdbuf: new Set(['-i', '-o', '-e', '--input', '--output', '--error']),
624
+ // /usr/bin/time -f 'suite wall: %e s' bash x.sh, from the corpus. Reading the
625
+ // format string as the command loses the bash that follows it.
626
+ time: new Set(['-f', '--format', '-o', '--output']),
627
+ };
628
+ /** Options that take a separate value, so positional arguments can be found
629
+ * without mistaking a value for a path. */
630
+ const OPT_WITH_VALUE = {
631
+ cp: new Set(['-t', '--target-directory', '-S', '--suffix', '--sparse', '--reflink', '--preserve', '--no-preserve']),
632
+ mv: new Set(['-t', '--target-directory', '-S', '--suffix']),
633
+ rm: new Set([]),
634
+ mkdir: new Set(['-m', '--mode', '-Z', '--context']),
635
+ touch: new Set(['-d', '--date', '-r', '--reference', '-t']),
636
+ truncate: new Set(['-s', '--size', '-r', '--reference', '-o']),
637
+ install: new Set(['-m', '--mode', '-o', '--owner', '-g', '--group', '-t', '--target-directory', '-S', '--suffix', '-Z', '--context', '--strip-program', '--backup']),
638
+ ln: new Set(['-S', '--suffix', '-t', '--target-directory', '--backup']),
639
+ tee: new Set([]),
640
+ patch: new Set(['-i', '--input', '-o', '--output', '-d', '--directory', '-B', '--prefix', '-D', '-r', '--reject-file', '-F', '-V', '-z', '-Y', '-b']),
641
+ chmod: new Set(['--reference']),
642
+ chown: new Set(['--reference']),
643
+ chgrp: new Set(['--reference']),
644
+ };
645
+ function positionals(argv, spec) {
646
+ const out = [];
647
+ for (let i = 1; i < argv.length; i += 1) {
648
+ const a = argv[i];
649
+ if (a === '--') {
650
+ out.push(...argv.slice(i + 1));
651
+ break;
652
+ }
653
+ if (a.length > 1 && a.startsWith('-')) {
654
+ if (spec.has(a))
655
+ i += 1;
656
+ continue;
657
+ }
658
+ out.push(a);
659
+ }
660
+ return out;
661
+ }
662
+ function expandWord(w, state, ctx) {
663
+ let value = '';
664
+ let resolved = true;
665
+ let reason = null;
666
+ for (const p of w.parts) {
667
+ if (p.kind === 'lit') {
668
+ value += p.text;
669
+ continue;
670
+ }
671
+ if (p.kind === 'cmd') {
672
+ value += p.raw;
673
+ resolved = false;
674
+ reason = reason ?? `command substitution ${short(p.raw)} is not evaluated`;
675
+ continue;
676
+ }
677
+ const known = lookupVar(p.name, state, ctx);
678
+ if (known !== null) {
679
+ value += known;
680
+ continue;
681
+ }
682
+ value += p.raw;
683
+ resolved = false;
684
+ reason = reason ?? `${p.raw} is not set in this command`;
685
+ }
686
+ if (resolved && value.startsWith('~')) {
687
+ resolved = false;
688
+ reason = 'the home directory is not known to the extractor';
689
+ }
690
+ return { value, resolved, reason, glob: wordHasGlob(w), subs: wordSubstitutions(w) };
691
+ }
692
+ function lookupVar(name, state, ctx) {
693
+ if (!name)
694
+ return null;
695
+ const local = state.vars.get(name);
696
+ if (local !== undefined)
697
+ return local;
698
+ if (name === 'CLAUDE_WORKTREE_PATH' || name === 'CLAUDE_PROJECT_DIR')
699
+ return ctx.worktreeRoot;
700
+ if ((name === 'PWD' || name === 'CWD') && state.cwd)
701
+ return state.cwd;
702
+ if (name === 'OLDPWD' && state.prevCwd)
703
+ return state.prevCwd;
704
+ return null;
705
+ }
706
+ function short(s) {
707
+ const one = s.replace(/\s+/g, ' ').trim();
708
+ return one.length > 60 ? one.slice(0, 57) + '...' : one;
709
+ }
710
+ const INERT_TARGET = /^(\/dev\/(null|stdout|stderr|fd\/\d+|tty)|-)$/;
711
+ function resolveTarget(e, state, label) {
712
+ if (!e.resolved)
713
+ return { path: null, uncertain: true, reason: `${label} ${short(e.value)}: ${e.reason}` };
714
+ if (!e.value)
715
+ return { path: null, uncertain: true, reason: `${label} is empty` };
716
+ // The null device and the standard streams are determined targets that touch
717
+ // no file, so they are certain and empty rather than uncertain.
718
+ if (INERT_TARGET.test(e.value))
719
+ return { path: null, uncertain: false, reason: null };
720
+ if (!e.value.startsWith('/') && !hostAbsolute(e.value) && state.cwd === null) {
721
+ return { path: null, uncertain: true, reason: `${label} ${short(e.value)} is relative and the working directory is not known` };
722
+ }
723
+ const base = state.cwd ?? '/';
724
+ if (e.glob) {
725
+ const prefix = globPrefix(e.value);
726
+ const resolvedPrefix = prefix ? resolvePath(base, prefix) : base;
727
+ return { path: resolvedPrefix, uncertain: true, reason: `${label} ${short(e.value)} is a glob; only the directory ${resolvedPrefix} is determined` };
728
+ }
729
+ return { path: resolvePath(base, e.value), uncertain: false, reason: null };
730
+ }
731
+ const INERT_REDIRECT = /^(\/dev\/(null|stdout|stderr|fd\/\d+|tty)|&\d+|&-|-)$/;
732
+ // ── 9. Git ───────────────────────────────────────────────────────────────────
733
+ const GIT_GLOBAL_WITH_VALUE = new Set(['-C', '-c', '--git-dir', '--work-tree', '--namespace', '--exec-path', '--super-prefix']);
734
+ function parseGit(argv, state, ctx, words) {
735
+ let dir = state.cwd ?? '/';
736
+ let dirUncertain = state.cwd === null;
737
+ let reason = state.cwd === null ? 'the working directory is not known' : null;
738
+ let i = 1;
739
+ while (i < argv.length && argv[i].startsWith('-')) {
740
+ const a = argv[i];
741
+ if (a === '-C') {
742
+ const raw = argv[i + 1] ?? '';
743
+ const w = words[i + 1];
744
+ const e = w ? expandWord(w, state, ctx) : { value: raw, resolved: true, reason: null, glob: false, subs: [] };
745
+ if (!e.resolved) {
746
+ dirUncertain = true;
747
+ reason = reason ?? `git -C ${short(raw)}: ${e.reason}`;
748
+ }
749
+ else
750
+ dir = resolvePath(dir, e.value);
751
+ i += 2;
752
+ continue;
753
+ }
754
+ if (GIT_GLOBAL_WITH_VALUE.has(a)) {
755
+ i += 2;
756
+ continue;
757
+ }
758
+ if (a.startsWith('--git-dir=') || a.startsWith('--work-tree=')) {
759
+ const v = a.slice(a.indexOf('=') + 1);
760
+ if (a.startsWith('--work-tree='))
761
+ dir = resolvePath(dir, v);
762
+ i += 1;
763
+ continue;
764
+ }
765
+ i += 1;
766
+ }
767
+ if (i >= argv.length)
768
+ return { sub: null, args: [], dir, dirUncertain, reason };
769
+ return { sub: argv[i], args: argv.slice(i + 1), dir, dirUncertain, reason };
770
+ }
771
+ const hasFlag = (args, ...flags) => args.some((a) => flags.includes(a));
772
+ /** Name the git operation. The names match the pattern identifiers in
773
+ * `evidence-tools/transcripts/read.mjs` so that coverage can be compared
774
+ * pattern by pattern against `docs/guard-coverage.md`. */
775
+ function gitOperation(g, ownBranch) {
776
+ const { sub, args } = g;
777
+ if (!sub)
778
+ return 'git';
779
+ const touchesMain = args.some((a) => a === 'main' || a === 'master' || /:(main|master)$/.test(a));
780
+ const touchesOwn = ownBranch ? args.some((a) => a.includes(ownBranch)) : false;
781
+ switch (sub) {
782
+ case 'reset':
783
+ if (hasFlag(args, '--hard'))
784
+ return 'git.reset-hard';
785
+ if (hasFlag(args, '--soft'))
786
+ return 'git.reset-soft';
787
+ return args.length ? 'git.reset' : 'git.reset-bare';
788
+ case 'checkout':
789
+ if (hasFlag(args, '--') || args.includes('.'))
790
+ return 'git.checkout-path';
791
+ if (hasFlag(args, '-b', '-B'))
792
+ return 'git.checkout-new-branch';
793
+ return args.length ? 'git.checkout-branch' : 'git.checkout-bare';
794
+ case 'switch':
795
+ return hasFlag(args, '-c', '-C') ? 'git.switch-create' : 'git.switch';
796
+ case 'restore':
797
+ return 'git.restore';
798
+ case 'clean':
799
+ return args.some((a) => /^-[a-zA-Z]*[fd]/.test(a)) ? 'git.clean' : 'git.clean-dry-run';
800
+ case 'merge':
801
+ return 'git.merge';
802
+ case 'rebase':
803
+ if (hasFlag(args, '--abort', '--continue', '--skip', '--quit'))
804
+ return 'git.rebase-control';
805
+ if (args.some((a) => /^(origin\/)?(main|master)$/.test(a)))
806
+ return 'git.rebase-onto-main';
807
+ return 'git.rebase';
808
+ case 'branch':
809
+ if (hasFlag(args, '-d', '-D', '--delete'))
810
+ return 'git.branch-delete';
811
+ if (hasFlag(args, '-m', '-M', '--move'))
812
+ return 'git.branch-move';
813
+ return 'git.branch';
814
+ case 'stash':
815
+ if (hasFlag(args, 'drop', 'clear', 'pop'))
816
+ return 'git.stash-drop';
817
+ if (hasFlag(args, 'push', 'save'))
818
+ return 'git.stash-push';
819
+ return 'git.stash';
820
+ case 'push': {
821
+ const lease = args.some((a) => a.startsWith('--force-with-lease'));
822
+ const force = hasFlag(args, '--force', '-f');
823
+ if ((lease || force) && touchesMain)
824
+ return 'git.push-force-main';
825
+ if (force)
826
+ return 'git.push-force';
827
+ if (lease)
828
+ return touchesOwn ? 'git.push-force-with-lease-own' : 'git.push-force-with-lease';
829
+ if (touchesMain)
830
+ return 'git.push-main';
831
+ return 'git.push';
832
+ }
833
+ case 'filter-branch':
834
+ return 'git.filter-branch';
835
+ default:
836
+ return 'git.' + sub;
837
+ }
838
+ }
839
+ /** Git subcommands that change the repository the call points at. */
840
+ const GIT_MUTATES_REPO = new Set([
841
+ 'reset', 'checkout', 'switch', 'restore', 'clean', 'merge', 'rebase', 'branch', 'stash',
842
+ 'commit', 'add', 'rm', 'mv', 'apply', 'am', 'cherry-pick', 'revert', 'pull', 'filter-branch',
843
+ 'gc', 'prune', 'repack', 'worktree', 'init', 'clone', 'submodule', 'tag', 'notes', 'update-ref',
844
+ 'update-index', 'sparse-checkout', 'bisect', 'fetch', 'config', 'remote', 'lfs', 'maintenance',
845
+ ]);
846
+ // ── 10. The walk ─────────────────────────────────────────────────────────────
847
+ function pushOp(w, state, op) {
848
+ w.ops.push({
849
+ op: op.op,
850
+ dir: op.dir !== undefined ? op.dir : state.cwd,
851
+ writes: op.writes ?? [],
852
+ reads: op.reads ?? [],
853
+ uncertain: op.uncertain ?? false,
854
+ // `targets` is the default because it is the stricter of the two: a rule
855
+ // that escalates on an unresolved target keeps doing so unless a call site
856
+ // says the uncertainty is about an unmodelled command instead.
857
+ uncertainKind: op.uncertainKind ?? 'targets',
858
+ reasons: op.reasons ?? [],
859
+ moved: (op.dir !== undefined ? op.dir : state.cwd) !== w.ctx.cwd,
860
+ });
861
+ }
862
+ function walkItems(items, state, w) {
863
+ for (const item of items) {
864
+ if (item.cmds.length > 1) {
865
+ // Every stage of a pipeline runs in its own subshell, so a `cd` or an
866
+ // assignment inside one does not survive it.
867
+ for (const c of item.cmds)
868
+ walkCommand(c, cloneState(state), w);
869
+ continue;
870
+ }
871
+ if (item.cmds.length === 1)
872
+ walkCommand(item.cmds[0], state, w);
873
+ }
874
+ }
875
+ function walkCommand(cmd, state, w) {
876
+ if (w.depth > 12)
877
+ return;
878
+ if (cmd.kind === 'expr') {
879
+ for (const word of cmd.words)
880
+ for (const body of wordSubstitutions(word))
881
+ walkSource(body, cloneState(state), w);
882
+ return;
883
+ }
884
+ if (cmd.kind === 'sub' || cmd.kind === 'group') {
885
+ applyRedirects(cmd.redirs, state, w);
886
+ const inner = cmd.kind === 'sub' ? cloneState(state) : state;
887
+ w.depth += 1;
888
+ walkItems(cmd.body, inner, w);
889
+ w.depth -= 1;
890
+ return;
891
+ }
892
+ walkSimple(cmd, state, w);
893
+ }
894
+ function walkSource(src, state, w) {
895
+ const { text, bodies } = extractHeredocs(src);
896
+ const saved = w.bodies;
897
+ const savedAt = w.heredocAt;
898
+ w.bodies = bodies;
899
+ w.heredocAt = 0;
900
+ w.depth += 1;
901
+ walkItems(parse(tokenize(text)), state, w);
902
+ w.depth -= 1;
903
+ w.bodies = saved;
904
+ w.heredocAt = savedAt;
905
+ }
906
+ function applyRedirects(redirs, state, w) {
907
+ for (const r of redirs) {
908
+ const m = REDIR.exec(r.op);
909
+ const bare = m ? m[2] : r.op;
910
+ if (bare === '<<' || bare === '<<-') {
911
+ w.heredocAt += 1;
912
+ continue;
913
+ }
914
+ if (bare === '<&' || bare === '>&')
915
+ continue;
916
+ if (!r.target)
917
+ continue;
918
+ const e = expandWord(r.target, state, w.ctx);
919
+ if (e.resolved && INERT_REDIRECT.test(e.value))
920
+ continue;
921
+ if (bare === '<' || bare === '<<<') {
922
+ const t = resolveTarget(e, state, 'redirect source');
923
+ if (t.path)
924
+ pushOp(w, state, { op: 'read', reads: [t.path] });
925
+ else
926
+ pushOp(w, state, { op: 'read', uncertain: true, reasons: t.reason ? [t.reason] : [] });
927
+ continue;
928
+ }
929
+ const t = resolveTarget(e, state, 'redirect target');
930
+ if (t.path)
931
+ pushOp(w, state, { op: 'write', writes: [t.path], uncertain: t.uncertain, reasons: t.reason ? [t.reason] : [] });
932
+ else
933
+ pushOp(w, state, { op: 'write', uncertain: true, reasons: t.reason ? [t.reason] : ['redirect target could not be resolved'] });
934
+ }
935
+ }
936
+ function walkSimple(cmd, state, w) {
937
+ // Redirects resolve against the working directory the command starts in, so
938
+ // they are handled before a `cd` in the same command can move it.
939
+ applyRedirects(cmd.redirs, state, w);
940
+ let words = cmd.words;
941
+ // Leading assignments. With no command after them they persist; with a
942
+ // command after them they are that command's environment only.
943
+ let assignEnd = 0;
944
+ while (assignEnd < words.length) {
945
+ const first = words[assignEnd].parts[0];
946
+ if (!(first && first.kind === 'lit' && !first.quoted && /^[A-Za-z_][A-Za-z0-9_]*=/.test(first.text)))
947
+ break;
948
+ assignEnd += 1;
949
+ }
950
+ const assignments = words.slice(0, assignEnd);
951
+ words = words.slice(assignEnd);
952
+ if (assignments.length && words.length === 0) {
953
+ for (const a of assignments) {
954
+ const raw = wordRaw(a);
955
+ const eq = raw.indexOf('=');
956
+ const name = raw.slice(0, eq);
957
+ const valueWord = { parts: splitAfterEquals(a) };
958
+ const e = expandWord(valueWord, state, w.ctx);
959
+ if (e.resolved)
960
+ state.vars.set(name, e.value);
961
+ else
962
+ state.vars.delete(name);
963
+ for (const body of e.subs)
964
+ walkSource(body, cloneState(state), w);
965
+ }
966
+ if (!cmd.redirs.length)
967
+ pushOp(w, state, { op: 'noop' });
968
+ return;
969
+ }
970
+ for (const a of assignments)
971
+ for (const body of wordSubstitutions(a))
972
+ walkSource(body, cloneState(state), w);
973
+ if (!words.length)
974
+ return;
975
+ // Wrappers come off before the real command is read.
976
+ for (let guard = 0; guard < 8; guard += 1) {
977
+ const head = baseName(wordRaw(words[0]));
978
+ const own = WRAPPERS.get(head);
979
+ if (own === undefined)
980
+ break;
981
+ const optValues = WRAPPER_OPT_WITH_VALUE[head] ?? new Set();
982
+ let next = 1;
983
+ while (next < words.length) {
984
+ const raw = wordRaw(words[next]);
985
+ if (raw.startsWith('-') && raw.length > 1) {
986
+ next += optValues.has(raw) ? 2 : 1;
987
+ continue;
988
+ }
989
+ // `env FOO=bar cmd` sets the environment and then runs cmd.
990
+ if (head === 'env' && /^[A-Za-z_][A-Za-z0-9_]*=/.test(raw)) {
991
+ next += 1;
992
+ continue;
993
+ }
994
+ break;
995
+ }
996
+ next += own;
997
+ if (next >= words.length)
998
+ return;
999
+ words = words.slice(next);
1000
+ }
1001
+ const expanded = words.map((word) => expandWord(word, state, w.ctx));
1002
+ const argv = expanded.map((e) => e.value);
1003
+ // Every command substitution in the arguments is walked, so `$(rm -rf /x)`
1004
+ // reports the removal rather than only that the word could not be expanded.
1005
+ for (const e of expanded)
1006
+ for (const body of e.subs)
1007
+ walkSource(body, cloneState(state), w);
1008
+ dispatch(baseName(argv[0]), argv, expanded, words, state, w);
1009
+ }
1010
+ function splitAfterEquals(word) {
1011
+ const parts = [...word.parts];
1012
+ const first = parts[0];
1013
+ if (first && first.kind === 'lit') {
1014
+ const eq = first.text.indexOf('=');
1015
+ if (eq >= 0)
1016
+ parts[0] = { kind: 'lit', text: first.text.slice(eq + 1), quoted: first.quoted };
1017
+ }
1018
+ return parts;
1019
+ }
1020
+ function dispatch(name, argv, expanded, words, state, w) {
1021
+ const ctx = w.ctx;
1022
+ const targets = (values, label) => {
1023
+ const paths = [];
1024
+ const reasons = [];
1025
+ let uncertain = false;
1026
+ for (const v of values) {
1027
+ const idx = argv.indexOf(v);
1028
+ const e = idx >= 0 ? expanded[idx] : { value: v, resolved: true, reason: null, glob: /[*?[]/.test(v), subs: [] };
1029
+ const t = resolveTarget(e, state, label);
1030
+ if (t.path)
1031
+ paths.push(t.path);
1032
+ if (t.uncertain) {
1033
+ uncertain = true;
1034
+ if (t.reason)
1035
+ reasons.push(t.reason);
1036
+ }
1037
+ }
1038
+ return { paths, uncertain, reasons };
1039
+ };
1040
+ switch (name) {
1041
+ case 'cd':
1042
+ case 'pushd': {
1043
+ const arg = argv[1];
1044
+ if (arg === undefined || arg === '~') {
1045
+ state.prevCwd = state.cwd;
1046
+ state.cwd = null;
1047
+ pushOp(w, state, { op: 'chdir', dir: null, uncertain: true, reasons: ['cd with no argument moves to the home directory, which the extractor does not know'] });
1048
+ return;
1049
+ }
1050
+ if (arg === '-') {
1051
+ const back = state.prevCwd;
1052
+ state.prevCwd = state.cwd;
1053
+ state.cwd = back;
1054
+ pushOp(w, state, { op: 'chdir', dir: back, uncertain: back === null, reasons: back === null ? ['cd - has no recorded previous directory'] : [] });
1055
+ return;
1056
+ }
1057
+ const e = expanded[1];
1058
+ const t = resolveTarget(e, state, 'cd target');
1059
+ state.prevCwd = state.cwd;
1060
+ state.cwd = t.path;
1061
+ pushOp(w, state, {
1062
+ op: 'chdir',
1063
+ dir: t.path,
1064
+ reads: t.path ? [t.path] : [],
1065
+ uncertain: t.uncertain || t.path === null,
1066
+ reasons: t.reason ? [t.reason] : [],
1067
+ });
1068
+ return;
1069
+ }
1070
+ case 'popd':
1071
+ state.cwd = null;
1072
+ pushOp(w, state, { op: 'chdir', dir: null, uncertain: true, reasons: ['popd returns to a directory the extractor did not record'] });
1073
+ return;
1074
+ case 'rm': {
1075
+ const pos = positionals(argv, OPT_WITH_VALUE.rm);
1076
+ const t = targets(pos, 'rm target');
1077
+ pushOp(w, state, { op: 'write', writes: t.paths, uncertain: t.uncertain || pos.length === 0, reasons: t.reasons });
1078
+ return;
1079
+ }
1080
+ case 'mkdir':
1081
+ case 'rmdir':
1082
+ case 'touch':
1083
+ case 'truncate': {
1084
+ const pos = positionals(argv, OPT_WITH_VALUE[name] ?? new Set());
1085
+ const t = targets(pos, `${name} target`);
1086
+ pushOp(w, state, { op: 'write', writes: t.paths, uncertain: t.uncertain || pos.length === 0, reasons: t.reasons });
1087
+ return;
1088
+ }
1089
+ case 'chmod':
1090
+ case 'chown':
1091
+ case 'chgrp': {
1092
+ const pos = positionals(argv, OPT_WITH_VALUE[name]).slice(1); // first positional is the mode or owner
1093
+ const t = targets(pos, `${name} target`);
1094
+ pushOp(w, state, { op: 'chmod', writes: t.paths, uncertain: t.uncertain || pos.length === 0, reasons: t.reasons });
1095
+ return;
1096
+ }
1097
+ case 'cp':
1098
+ case 'mv':
1099
+ case 'install': {
1100
+ const spec = OPT_WITH_VALUE[name];
1101
+ const pos = positionals(argv, spec);
1102
+ const tIdx = argv.findIndex((a) => a === '-t' || a === '--target-directory');
1103
+ let sources = pos;
1104
+ let dest = null;
1105
+ if (tIdx >= 0)
1106
+ dest = argv[tIdx + 1] ?? null;
1107
+ else if (pos.length >= 2) {
1108
+ dest = pos[pos.length - 1];
1109
+ sources = pos.slice(0, -1);
1110
+ }
1111
+ else {
1112
+ sources = pos;
1113
+ dest = null;
1114
+ }
1115
+ const src = targets(sources, `${name} source`);
1116
+ const reasons = [...src.reasons];
1117
+ let uncertain = src.uncertain;
1118
+ const writes = [];
1119
+ if (dest === null) {
1120
+ uncertain = true;
1121
+ reasons.push(`${name} has no destination the extractor can name`);
1122
+ }
1123
+ else {
1124
+ const d = targets([dest], `${name} destination`);
1125
+ uncertain = uncertain || d.uncertain;
1126
+ reasons.push(...d.reasons);
1127
+ for (const p of d.paths) {
1128
+ writes.push(p);
1129
+ // With more than one source, or with -t, the destination is a
1130
+ // directory and each source lands inside it.
1131
+ if (sources.length > 1 || tIdx >= 0)
1132
+ for (const s of src.paths)
1133
+ writes.push(p + '/' + baseName(s));
1134
+ }
1135
+ }
1136
+ // mv removes its sources, so a source is a write as well as a read.
1137
+ if (name === 'mv')
1138
+ writes.push(...src.paths);
1139
+ pushOp(w, state, { op: 'write', writes, reads: src.paths, uncertain, reasons });
1140
+ return;
1141
+ }
1142
+ case 'ln': {
1143
+ const pos = positionals(argv, OPT_WITH_VALUE.ln);
1144
+ if (pos.length >= 2) {
1145
+ const link = targets([pos[pos.length - 1]], 'ln link');
1146
+ const src = targets(pos.slice(0, -1), 'ln target');
1147
+ pushOp(w, state, { op: 'write', writes: link.paths, reads: src.paths, uncertain: link.uncertain || src.uncertain, reasons: [...link.reasons, ...src.reasons] });
1148
+ return;
1149
+ }
1150
+ const src = targets(pos, 'ln target');
1151
+ const writes = src.paths.map((p) => (state.cwd ? state.cwd + '/' + baseName(p) : p));
1152
+ pushOp(w, state, { op: 'write', writes, reads: src.paths, uncertain: src.uncertain || state.cwd === null, reasons: src.reasons });
1153
+ return;
1154
+ }
1155
+ case 'tee': {
1156
+ const pos = positionals(argv, OPT_WITH_VALUE.tee);
1157
+ const t = targets(pos, 'tee target');
1158
+ pushOp(w, state, { op: 'write', writes: t.paths, uncertain: t.uncertain || pos.length === 0, reasons: pos.length === 0 ? [...t.reasons, 'tee with no file argument writes to standard output only'] : t.reasons });
1159
+ return;
1160
+ }
1161
+ case 'dd': {
1162
+ const of = argv.find((a) => a.startsWith('of='));
1163
+ const inf = argv.find((a) => a.startsWith('if='));
1164
+ const outT = of ? targets([of.slice(3)], 'dd of=') : null;
1165
+ const inT = inf ? targets([inf.slice(3)], 'dd if=') : null;
1166
+ pushOp(w, state, {
1167
+ op: 'write',
1168
+ writes: outT?.paths ?? [],
1169
+ reads: inT?.paths ?? [],
1170
+ uncertain: (outT?.uncertain ?? false) || (inT?.uncertain ?? false) || !of,
1171
+ reasons: [...(outT?.reasons ?? []), ...(inT?.reasons ?? []), ...(of ? [] : ['dd without of= writes to standard output'])],
1172
+ });
1173
+ return;
1174
+ }
1175
+ case 'sed': {
1176
+ const inPlace = argv.some((a, k) => k > 0 && (a === '--in-place' || a.startsWith('--in-place=') || (/^-[a-zA-Z]*i/.test(a) && !a.startsWith('--'))));
1177
+ const files = sedFiles(argv);
1178
+ const t = targets(files, 'sed file');
1179
+ if (!inPlace) {
1180
+ pushOp(w, state, { op: 'read', reads: t.paths, uncertain: t.uncertain, reasons: t.reasons });
1181
+ return;
1182
+ }
1183
+ pushOp(w, state, { op: 'write', writes: t.paths, reads: t.paths, uncertain: t.uncertain || files.length === 0, reasons: files.length === 0 ? [...t.reasons, 'sed -i with no file argument'] : t.reasons });
1184
+ return;
1185
+ }
1186
+ case 'perl': {
1187
+ const inPlace = argv.some((a, k) => k > 0 && /^-[a-zA-Z]*i/.test(a) && !a.startsWith('--'));
1188
+ const files = perlFiles(argv);
1189
+ const t = targets(files, 'perl file');
1190
+ if (!inPlace) {
1191
+ pushOp(w, state, { op: 'unknown', reads: t.paths, uncertain: true, uncertainKind: 'effects', reasons: ['perl runs a script whose writes are not determined from the command'] });
1192
+ return;
1193
+ }
1194
+ pushOp(w, state, { op: 'write', writes: t.paths, reads: t.paths, uncertain: t.uncertain || files.length === 0, reasons: files.length === 0 ? [...t.reasons, 'perl -i with no file argument'] : t.reasons });
1195
+ return;
1196
+ }
1197
+ case 'patch': {
1198
+ const pos = positionals(argv, OPT_WITH_VALUE.patch);
1199
+ const outIdx = argv.findIndex((a) => a === '-o' || a === '--output');
1200
+ const out = outIdx >= 0 ? argv[outIdx + 1] : undefined;
1201
+ if (out) {
1202
+ const t = targets([out], 'patch output');
1203
+ pushOp(w, state, { op: 'write', writes: t.paths, uncertain: t.uncertain, reasons: t.reasons });
1204
+ return;
1205
+ }
1206
+ if (pos.length) {
1207
+ const t = targets(pos, 'patch target');
1208
+ pushOp(w, state, { op: 'write', writes: t.paths, uncertain: t.uncertain, reasons: t.reasons });
1209
+ return;
1210
+ }
1211
+ pushOp(w, state, { op: 'write', writes: state.cwd ? [state.cwd] : [], uncertain: true, reasons: ['patch reads its target file names from the diff, which the extractor does not have'] });
1212
+ return;
1213
+ }
1214
+ case 'git': {
1215
+ const g = parseGit(argv, state, ctx, words);
1216
+ const ownBranch = ctx.worktreeRoot ? baseName(ctx.worktreeRoot) : null;
1217
+ const op = gitOperation(g, ownBranch);
1218
+ const writes = [];
1219
+ const reasons = g.reason ? [g.reason] : [];
1220
+ if (g.sub && GIT_MUTATES_REPO.has(g.sub) && !g.dirUncertain)
1221
+ writes.push(g.dir);
1222
+ if (op === 'git.checkout-path' || op === 'git.restore' || g.sub === 'add' || g.sub === 'rm' || g.sub === 'apply') {
1223
+ const after = g.args.indexOf('--');
1224
+ const pathArgs = (after >= 0 ? g.args.slice(after + 1) : g.args).filter((a) => !a.startsWith('-'));
1225
+ const scoped = { cwd: g.dirUncertain ? null : g.dir, prevCwd: state.prevCwd, vars: state.vars };
1226
+ for (const p of pathArgs) {
1227
+ const t = resolveTarget({ value: p, resolved: true, reason: null, glob: /[*?[]/.test(p), subs: [] }, scoped, 'git path');
1228
+ if (t.path)
1229
+ writes.push(t.path);
1230
+ if (t.reason)
1231
+ reasons.push(t.reason);
1232
+ }
1233
+ }
1234
+ if (g.sub === 'push' || g.sub === 'fetch' || g.sub === 'ls-remote') {
1235
+ pushOp(w, state, { op, dir: g.dir, reads: [g.dir], writes: g.sub === 'fetch' ? [g.dir] : [], uncertain: g.dirUncertain, reasons });
1236
+ return;
1237
+ }
1238
+ pushOp(w, state, { op, dir: g.dirUncertain ? null : g.dir, writes, reads: [g.dir], uncertain: g.dirUncertain, reasons });
1239
+ return;
1240
+ }
1241
+ case 'bash':
1242
+ case 'sh':
1243
+ case 'zsh':
1244
+ case 'dash':
1245
+ case 'ksh': {
1246
+ const cIdx = argv.findIndex((a, k) => k > 0 && a === '-c');
1247
+ if (cIdx >= 0 && expanded[cIdx + 1]) {
1248
+ const inner = expanded[cIdx + 1];
1249
+ if (inner.resolved) {
1250
+ walkSource(inner.value, cloneState(state), w);
1251
+ return;
1252
+ }
1253
+ pushOp(w, state, { op: 'unknown', uncertain: true, uncertainKind: 'effects', reasons: [`${name} -c runs a string the extractor cannot expand: ${short(inner.value)}`] });
1254
+ return;
1255
+ }
1256
+ const heredoc = w.bodies[w.heredocAt - 1];
1257
+ if (heredoc !== undefined && argv.length === 1) {
1258
+ walkSource(heredoc, cloneState(state), w);
1259
+ return;
1260
+ }
1261
+ const script = positionals(argv, new Set())[0];
1262
+ const t = script ? targets([script], `${name} script`) : null;
1263
+ pushOp(w, state, {
1264
+ op: 'unknown',
1265
+ reads: t?.paths ?? [],
1266
+ uncertain: true,
1267
+ uncertainKind: 'effects',
1268
+ reasons: [script ? `${name} runs ${short(script)}, whose writes are not determined from the command` : `${name} runs a script the extractor cannot name`],
1269
+ });
1270
+ return;
1271
+ }
1272
+ case 'eval': {
1273
+ const rest = expanded.slice(1);
1274
+ if (rest.length && rest.every((e) => e.resolved)) {
1275
+ walkSource(rest.map((e) => e.value).join(' '), cloneState(state), w);
1276
+ return;
1277
+ }
1278
+ pushOp(w, state, { op: 'unknown', uncertain: true, uncertainKind: 'effects', reasons: ['eval runs a string the extractor cannot expand'] });
1279
+ return;
1280
+ }
1281
+ case 'source':
1282
+ case '.': {
1283
+ const t = targets(positionals(argv, new Set()), 'source file');
1284
+ pushOp(w, state, { op: 'unknown', reads: t.paths, uncertain: true, uncertainKind: 'effects', reasons: ['source runs a file whose writes are not determined from the command'] });
1285
+ return;
1286
+ }
1287
+ case 'xargs': {
1288
+ const inner = argv.slice(1).filter((a) => !a.startsWith('-'));
1289
+ pushOp(w, state, {
1290
+ op: 'unknown',
1291
+ uncertain: true,
1292
+ uncertainKind: 'effects',
1293
+ reasons: [`xargs takes its arguments from standard input, so the targets of ${short(inner.join(' ') || 'the inner command')} are not determined`],
1294
+ });
1295
+ return;
1296
+ }
1297
+ case 'find': {
1298
+ const roots = positionals(argv, new Set(['-name', '-iname', '-path', '-type', '-maxdepth', '-mindepth', '-newer', '-size', '-perm', '-user', '-group', '-regex', '-not', '-printf'])).filter((a) => !a.startsWith('-'));
1299
+ const t = targets(roots.length ? roots : ['.'], 'find root');
1300
+ const mutates = argv.some((a) => a === '-delete' || a === '-exec' || a === '-execdir' || a === '-ok' || a === '-fprint' || a === '-fls');
1301
+ if (!mutates) {
1302
+ pushOp(w, state, { op: 'read', reads: t.paths, uncertain: t.uncertain, reasons: t.reasons });
1303
+ return;
1304
+ }
1305
+ pushOp(w, state, {
1306
+ op: 'write',
1307
+ writes: t.paths,
1308
+ reads: t.paths,
1309
+ uncertain: true,
1310
+ reasons: [...t.reasons, 'find -delete or -exec acts on files matched at run time, so only the search roots are determined'],
1311
+ });
1312
+ return;
1313
+ }
1314
+ case 'curl': {
1315
+ const oIdx = argv.findIndex((a) => a === '-o' || a === '--output');
1316
+ if (oIdx >= 0 && argv[oIdx + 1]) {
1317
+ const t = targets([argv[oIdx + 1]], 'curl output');
1318
+ pushOp(w, state, { op: 'write', writes: t.paths, uncertain: t.uncertain, reasons: t.reasons });
1319
+ return;
1320
+ }
1321
+ if (argv.includes('-O') || argv.includes('--remote-name')) {
1322
+ pushOp(w, state, { op: 'write', writes: state.cwd ? [state.cwd] : [], uncertain: true, reasons: ['curl -O names the file from the URL, so only the directory is determined'] });
1323
+ return;
1324
+ }
1325
+ pushOp(w, state, { op: 'read' });
1326
+ return;
1327
+ }
1328
+ case 'wget': {
1329
+ const oIdx = argv.findIndex((a) => a === '-O' || a === '--output-document');
1330
+ if (oIdx >= 0 && argv[oIdx + 1]) {
1331
+ const t = targets([argv[oIdx + 1]], 'wget output');
1332
+ pushOp(w, state, { op: 'write', writes: t.paths, uncertain: t.uncertain, reasons: t.reasons });
1333
+ return;
1334
+ }
1335
+ pushOp(w, state, { op: 'write', writes: state.cwd ? [state.cwd] : [], uncertain: true, reasons: ['wget names the file from the URL, so only the directory is determined'] });
1336
+ return;
1337
+ }
1338
+ case 'mktemp':
1339
+ pushOp(w, state, { op: 'write', uncertain: true, reasons: ['mktemp creates a file whose name is chosen at run time'] });
1340
+ return;
1341
+ case 'tar':
1342
+ case 'unzip':
1343
+ case 'zip': {
1344
+ const cIdx = argv.findIndex((a) => a === '-C' || a === '-d');
1345
+ const dir = cIdx >= 0 ? argv[cIdx + 1] : undefined;
1346
+ const t = dir ? targets([dir], `${name} -C`) : null;
1347
+ pushOp(w, state, {
1348
+ op: 'write',
1349
+ writes: t?.paths ?? (state.cwd ? [state.cwd] : []),
1350
+ uncertain: true,
1351
+ reasons: [`${name} writes members named inside the archive, so only the destination directory is determined`],
1352
+ });
1353
+ return;
1354
+ }
1355
+ default:
1356
+ break;
1357
+ }
1358
+ if (READERS.has(name)) {
1359
+ if (NO_PATH_READERS.has(name)) {
1360
+ pushOp(w, state, { op: 'read' });
1361
+ return;
1362
+ }
1363
+ let pos = positionals(argv, new Set()).filter((a) => !a.startsWith('-'));
1364
+ // The first positional of these is a pattern or an expression, not a path.
1365
+ if (PATTERN_FIRST.has(name) && pos.length)
1366
+ pos = pos.slice(1);
1367
+ const t = targets(pos, `${name} argument`);
1368
+ pushOp(w, state, { op: 'read', reads: t.paths, uncertain: false, reasons: [] });
1369
+ return;
1370
+ }
1371
+ pushOp(w, state, {
1372
+ op: 'unknown',
1373
+ uncertain: true,
1374
+ uncertainKind: 'effects',
1375
+ reasons: [`${name} is not a command the extractor models, so its write targets are not determined`],
1376
+ });
1377
+ }
1378
+ /** sed positional files. The first positional is the script unless -e or -f
1379
+ * supplied one. */
1380
+ function sedFiles(argv) {
1381
+ let scriptGiven = false;
1382
+ const pos = [];
1383
+ for (let i = 1; i < argv.length; i += 1) {
1384
+ const a = argv[i];
1385
+ if (a === '--') {
1386
+ pos.push(...argv.slice(i + 1));
1387
+ break;
1388
+ }
1389
+ if (a.startsWith('--')) {
1390
+ if (a.startsWith('--expression') || a.startsWith('--file'))
1391
+ scriptGiven = true;
1392
+ if ((a === '--expression' || a === '--file') && argv[i + 1]) {
1393
+ i += 1;
1394
+ }
1395
+ continue;
1396
+ }
1397
+ if (a.startsWith('-') && a.length > 1) {
1398
+ if (a === '-e' || a === '-f') {
1399
+ scriptGiven = true;
1400
+ i += 1;
1401
+ continue;
1402
+ }
1403
+ if (/^-[a-zA-Z]*[ef]/.test(a) && !/^-[a-zA-Z]*i/.test(a))
1404
+ scriptGiven = true;
1405
+ continue;
1406
+ }
1407
+ pos.push(a);
1408
+ }
1409
+ if (!scriptGiven && pos.length)
1410
+ pos.shift();
1411
+ return pos;
1412
+ }
1413
+ /** perl positional files. Without -e the first positional is the script. */
1414
+ function perlFiles(argv) {
1415
+ let scriptGiven = false;
1416
+ const pos = [];
1417
+ for (let i = 1; i < argv.length; i += 1) {
1418
+ const a = argv[i];
1419
+ if (a === '--') {
1420
+ pos.push(...argv.slice(i + 1));
1421
+ break;
1422
+ }
1423
+ if (a === '-e' || a === '-E') {
1424
+ scriptGiven = true;
1425
+ i += 1;
1426
+ continue;
1427
+ }
1428
+ if (a.startsWith('-') && a.length > 1) {
1429
+ if (/^-[a-zA-Z]*[eE]$/.test(a)) {
1430
+ scriptGiven = true;
1431
+ i += 1;
1432
+ continue;
1433
+ }
1434
+ if (a === '-M' || a === '-m' || a === '-I' || a === '-F') {
1435
+ i += 1;
1436
+ continue;
1437
+ }
1438
+ continue;
1439
+ }
1440
+ pos.push(a);
1441
+ }
1442
+ if (!scriptGiven && pos.length)
1443
+ pos.shift();
1444
+ return pos;
1445
+ }
1446
+ // ── 11. Folding operations into Attributes ───────────────────────────────────
1447
+ /** Severity order for choosing which operation names the call. Destructive git
1448
+ * outranks a write because it is the narrower claim; `writes` and `reads` are
1449
+ * the union either way, so nothing is dropped by this choice. */
1450
+ const DESTRUCTIVE_GIT = new Set([
1451
+ 'git.reset', 'git.reset-hard', 'git.checkout-path', 'git.checkout-branch', 'git.checkout-new-branch',
1452
+ 'git.switch', 'git.switch-create', 'git.restore', 'git.clean', 'git.merge', 'git.rebase',
1453
+ 'git.branch-delete', 'git.stash-drop', 'git.push-force', 'git.push-force-main',
1454
+ 'git.push-force-with-lease', 'git.push-main', 'git.filter-branch',
1455
+ ]);
1456
+ export function isDestructiveGit(op) {
1457
+ return DESTRUCTIVE_GIT.has(op);
1458
+ }
1459
+ export function severityOf(op) {
1460
+ if (DESTRUCTIVE_GIT.has(op))
1461
+ return 60;
1462
+ if (op === 'write')
1463
+ return 50;
1464
+ if (op.startsWith('git.') || op === 'git')
1465
+ return 40;
1466
+ if (op === 'chmod')
1467
+ return 35;
1468
+ if (op === 'chdir')
1469
+ return 30;
1470
+ if (op === 'unknown')
1471
+ return 20;
1472
+ if (op === 'read')
1473
+ return 10;
1474
+ return 0;
1475
+ }
1476
+ function uniq(values) {
1477
+ return [...new Set(values)];
1478
+ }
1479
+ export function foldOperations(ops, ctx, command) {
1480
+ if (!ops.length) {
1481
+ return { operation: 'noop', writes: [], reads: [ctx.cwd], workingDir: ctx.cwd, uncertain: false, ...(command !== undefined ? { command } : {}) };
1482
+ }
1483
+ let dominant = ops[0];
1484
+ for (const op of ops) {
1485
+ const better = severityOf(op.op) - severityOf(dominant.op);
1486
+ if (better > 0 || (better === 0 && op.moved && !dominant.moved))
1487
+ dominant = op;
1488
+ }
1489
+ // reads[0] is the working directory the call used. When the call moved, the
1490
+ // last directory it moved to is the one a boundary rule has to see.
1491
+ const movedDirs = ops.filter((o) => o.moved && o.dir !== null).map((o) => o.dir);
1492
+ const workingDir = movedDirs.length ? movedDirs[movedDirs.length - 1] : (dominant.dir ?? ctx.cwd);
1493
+ const lostDir = ops.some((o) => o.dir === null);
1494
+ const reasons = uniq(ops.flatMap((o) => o.reasons)).slice(0, 6);
1495
+ const uncertain = ops.some((o) => o.uncertain);
1496
+ // Which kinds of uncertainty this call holds. A rule answers an unresolved
1497
+ // target and an unmodelled command differently, so the fold keeps them apart
1498
+ // rather than reporting one flag for both.
1499
+ const kinds = new Set(ops.filter((o) => o.uncertain).map((o) => o.uncertainKind));
1500
+ const about = kinds.size > 1
1501
+ ? 'both'
1502
+ : kinds.has('effects') ? 'effects' : 'targets';
1503
+ const attrs = {
1504
+ operation: dominant.op,
1505
+ writes: uniq(ops.flatMap((o) => o.writes)),
1506
+ reads: uniq([workingDir, ...ops.flatMap((o) => o.reads)]),
1507
+ workingDir,
1508
+ uncertain,
1509
+ ...(command !== undefined ? { command } : {}),
1510
+ };
1511
+ if (uncertain) {
1512
+ attrs.uncertainAbout = about;
1513
+ attrs.uncertainReason = reasons.length
1514
+ ? reasons.join('; ')
1515
+ : (lostDir ? 'the working directory could not be resolved' : 'a target could not be resolved');
1516
+ }
1517
+ return attrs;
1518
+ }
1519
+ // ── 12. The public surface ───────────────────────────────────────────────────
1520
+ /** Every operation a shell command performs, in order. Exported for tests and
1521
+ * for tooling that needs more than the single verb `Attributes` carries. */
1522
+ export function extractOperations(command, ctx) {
1523
+ const { text, bodies } = extractHeredocs(command);
1524
+ const walk = { ctx, ops: [], bodies, heredocAt: 0, depth: 0 };
1525
+ const state = { cwd: ctx.cwd, prevCwd: null, vars: new Map() };
1526
+ try {
1527
+ walkItems(parse(tokenize(text)), state, walk);
1528
+ }
1529
+ catch (err) {
1530
+ walk.ops.push({
1531
+ op: 'unknown',
1532
+ dir: ctx.cwd,
1533
+ writes: [],
1534
+ reads: [],
1535
+ uncertain: true,
1536
+ // A command that did not parse tells us nothing about either its targets
1537
+ // or its effects, so it takes the stricter of the two.
1538
+ uncertainKind: 'targets',
1539
+ reasons: [`the command could not be parsed: ${err.message}`],
1540
+ moved: false,
1541
+ });
1542
+ }
1543
+ return walk.ops;
1544
+ }
1545
+ const WRITE_TOOLS = new Set(['Write', 'Edit', 'MultiEdit', 'NotebookEdit']);
1546
+ const READ_TOOLS = new Set(['Read', 'NotebookRead', 'Glob', 'Grep']);
1547
+ /** Tools that touch no path in the principal's filesystem. */
1548
+ const INERT_TOOLS = new Set(['WebFetch', 'WebSearch', 'TodoWrite', 'AskUserQuestion', 'ExitPlanMode', 'SlashCommand']);
1549
+ export function extract(call, context) {
1550
+ const ctx = { cwd: normalisePath(context.cwd), worktreeRoot: normalisePath(context.worktreeRoot) };
1551
+ if (call.name === 'Bash') {
1552
+ const command = typeof call.input.command === 'string' ? call.input.command : '';
1553
+ if (!command.trim()) {
1554
+ return { operation: 'noop', writes: [], reads: [ctx.cwd], workingDir: ctx.cwd, uncertain: false, command };
1555
+ }
1556
+ return foldOperations(extractOperations(command, ctx), ctx, command);
1557
+ }
1558
+ if (WRITE_TOOLS.has(call.name)) {
1559
+ const raw = call.input.file_path ?? call.input.notebook_path;
1560
+ if (typeof raw !== 'string' || !raw) {
1561
+ return { operation: 'write', writes: [], reads: [ctx.cwd], workingDir: ctx.cwd, uncertain: true, uncertainAbout: 'targets', uncertainReason: `${call.name} was called without a file path` };
1562
+ }
1563
+ return { operation: 'write', writes: [resolvePath(ctx.cwd, raw)], reads: [ctx.cwd], workingDir: ctx.cwd, uncertain: false };
1564
+ }
1565
+ if (READ_TOOLS.has(call.name)) {
1566
+ const raw = call.input.file_path ?? call.input.notebook_path ?? call.input.path;
1567
+ const reads = [ctx.cwd];
1568
+ if (typeof raw === 'string' && raw)
1569
+ reads.push(resolvePath(ctx.cwd, raw));
1570
+ return { operation: 'read', writes: [], reads: uniq(reads), workingDir: ctx.cwd, uncertain: false };
1571
+ }
1572
+ if (INERT_TOOLS.has(call.name)) {
1573
+ return { operation: 'tool.' + call.name, writes: [], reads: [ctx.cwd], workingDir: ctx.cwd, uncertain: false };
1574
+ }
1575
+ if (call.name === 'Task' || call.name === 'Agent') {
1576
+ return {
1577
+ operation: 'tool.' + call.name,
1578
+ writes: [],
1579
+ reads: [ctx.cwd],
1580
+ workingDir: ctx.cwd,
1581
+ uncertain: true,
1582
+ uncertainAbout: 'effects',
1583
+ uncertainReason: `${call.name} delegates to a subagent whose calls are not visible here; each of them reaches this extractor separately`,
1584
+ };
1585
+ }
1586
+ return {
1587
+ operation: 'tool.' + call.name,
1588
+ writes: [],
1589
+ reads: [ctx.cwd],
1590
+ workingDir: ctx.cwd,
1591
+ uncertain: true,
1592
+ uncertainAbout: 'effects',
1593
+ uncertainReason: `${call.name} is not a tool the extractor models, so its write targets are not determined`,
1594
+ };
1595
+ }
1596
+ export const extractor = {
1597
+ extract(call, context) {
1598
+ return extract(call, context);
1599
+ },
1600
+ };
1601
+ //# sourceMappingURL=extract.js.map