@memnexus-ai/mx-agent-cli 0.1.294-next.0 → 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 (450) 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 +35 -0
  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__/digest-team-omission.test.d.ts +15 -0
  82. package/dist/__tests__/digest-team-omission.test.d.ts.map +1 -0
  83. package/dist/__tests__/digest-team-omission.test.js +247 -0
  84. package/dist/__tests__/digest-team-omission.test.js.map +1 -0
  85. package/dist/__tests__/doctor-worktree-path.test.d.ts +17 -0
  86. package/dist/__tests__/doctor-worktree-path.test.d.ts.map +1 -0
  87. package/dist/__tests__/doctor-worktree-path.test.js +158 -0
  88. package/dist/__tests__/doctor-worktree-path.test.js.map +1 -0
  89. package/dist/__tests__/fleet-data.test.js +40 -7
  90. package/dist/__tests__/fleet-data.test.js.map +1 -1
  91. package/dist/__tests__/fleet-tmux.test.js +2 -2
  92. package/dist/__tests__/fleet-tmux.test.js.map +1 -1
  93. package/dist/__tests__/generate-claude.test.js +137 -3
  94. package/dist/__tests__/generate-claude.test.js.map +1 -1
  95. package/dist/__tests__/git-env.test.d.ts +14 -0
  96. package/dist/__tests__/git-env.test.d.ts.map +1 -0
  97. package/dist/__tests__/git-env.test.js +131 -0
  98. package/dist/__tests__/git-env.test.js.map +1 -0
  99. package/dist/__tests__/git-identity-isolation.test.d.ts +33 -0
  100. package/dist/__tests__/git-identity-isolation.test.d.ts.map +1 -0
  101. package/dist/__tests__/git-identity-isolation.test.js +626 -0
  102. package/dist/__tests__/git-identity-isolation.test.js.map +1 -0
  103. package/dist/__tests__/hook-heal.test.js +17 -0
  104. package/dist/__tests__/hook-heal.test.js.map +1 -1
  105. package/dist/__tests__/init-coordination-url.test.d.ts +11 -0
  106. package/dist/__tests__/init-coordination-url.test.d.ts.map +1 -0
  107. package/dist/__tests__/init-coordination-url.test.js +114 -0
  108. package/dist/__tests__/init-coordination-url.test.js.map +1 -0
  109. package/dist/__tests__/memory-autocreate.test.js +574 -8
  110. package/dist/__tests__/memory-autocreate.test.js.map +1 -1
  111. package/dist/__tests__/memory-log-result.test.d.ts +13 -0
  112. package/dist/__tests__/memory-log-result.test.d.ts.map +1 -0
  113. package/dist/__tests__/memory-log-result.test.js +113 -0
  114. package/dist/__tests__/memory-log-result.test.js.map +1 -0
  115. package/dist/__tests__/memory-named-exists.test.d.ts +16 -0
  116. package/dist/__tests__/memory-named-exists.test.d.ts.map +1 -0
  117. package/dist/__tests__/memory-named-exists.test.js +288 -0
  118. package/dist/__tests__/memory-named-exists.test.js.map +1 -0
  119. package/dist/__tests__/mx-agent-cli-pin-validation.test.js +23 -47
  120. package/dist/__tests__/mx-agent-cli-pin-validation.test.js.map +1 -1
  121. package/dist/__tests__/mx-agent-cli-version-pin.test.js +34 -88
  122. package/dist/__tests__/mx-agent-cli-version-pin.test.js.map +1 -1
  123. package/dist/__tests__/new-project-first-team.test.d.ts +15 -0
  124. package/dist/__tests__/new-project-first-team.test.d.ts.map +1 -0
  125. package/dist/__tests__/new-project-first-team.test.js +263 -0
  126. package/dist/__tests__/new-project-first-team.test.js.map +1 -0
  127. package/dist/__tests__/otel-env.test.d.ts +2 -0
  128. package/dist/__tests__/otel-env.test.d.ts.map +1 -0
  129. package/dist/__tests__/otel-env.test.js +127 -0
  130. package/dist/__tests__/otel-env.test.js.map +1 -0
  131. package/dist/__tests__/project-config.test.js +283 -1
  132. package/dist/__tests__/project-config.test.js.map +1 -1
  133. package/dist/__tests__/provision-behavioral-contract.test.js +96 -4
  134. package/dist/__tests__/provision-behavioral-contract.test.js.map +1 -1
  135. package/dist/__tests__/provision-catalog-precondition.test.d.ts +26 -0
  136. package/dist/__tests__/provision-catalog-precondition.test.d.ts.map +1 -0
  137. package/dist/__tests__/provision-catalog-precondition.test.js +620 -0
  138. package/dist/__tests__/provision-catalog-precondition.test.js.map +1 -0
  139. package/dist/__tests__/provision-completion-rows.test.d.ts +17 -0
  140. package/dist/__tests__/provision-completion-rows.test.d.ts.map +1 -0
  141. package/dist/__tests__/provision-completion-rows.test.js +3095 -0
  142. package/dist/__tests__/provision-completion-rows.test.js.map +1 -0
  143. package/dist/__tests__/provision-identity-gate.test.d.ts +20 -0
  144. package/dist/__tests__/provision-identity-gate.test.d.ts.map +1 -0
  145. package/dist/__tests__/provision-identity-gate.test.js +600 -0
  146. package/dist/__tests__/provision-identity-gate.test.js.map +1 -0
  147. package/dist/__tests__/provision-resume.test.d.ts +26 -0
  148. package/dist/__tests__/provision-resume.test.d.ts.map +1 -0
  149. package/dist/__tests__/provision-resume.test.js +1164 -0
  150. package/dist/__tests__/provision-resume.test.js.map +1 -0
  151. package/dist/__tests__/provision-roleguide-anchors.test.d.ts +27 -0
  152. package/dist/__tests__/provision-roleguide-anchors.test.d.ts.map +1 -0
  153. package/dist/__tests__/provision-roleguide-anchors.test.js +747 -0
  154. package/dist/__tests__/provision-roleguide-anchors.test.js.map +1 -0
  155. package/dist/__tests__/provision-skeleton-fill.test.d.ts +16 -0
  156. package/dist/__tests__/provision-skeleton-fill.test.d.ts.map +1 -0
  157. package/dist/__tests__/provision-skeleton-fill.test.js +488 -0
  158. package/dist/__tests__/provision-skeleton-fill.test.js.map +1 -0
  159. package/dist/__tests__/provision-teambuilder-flow.test.js +93 -7
  160. package/dist/__tests__/provision-teambuilder-flow.test.js.map +1 -1
  161. package/dist/__tests__/repo-guard-wiring.test.d.ts +21 -0
  162. package/dist/__tests__/repo-guard-wiring.test.d.ts.map +1 -0
  163. package/dist/__tests__/repo-guard-wiring.test.js +638 -0
  164. package/dist/__tests__/repo-guard-wiring.test.js.map +1 -0
  165. package/dist/__tests__/runtime-codex-gateway-pilot.test.d.ts +2 -0
  166. package/dist/__tests__/runtime-codex-gateway-pilot.test.d.ts.map +1 -0
  167. package/dist/__tests__/runtime-codex-gateway-pilot.test.js +309 -0
  168. package/dist/__tests__/runtime-codex-gateway-pilot.test.js.map +1 -0
  169. package/dist/__tests__/runtime-codex-gateway-provider.test.d.ts +2 -0
  170. package/dist/__tests__/runtime-codex-gateway-provider.test.d.ts.map +1 -0
  171. package/dist/__tests__/runtime-codex-gateway-provider.test.js +163 -0
  172. package/dist/__tests__/runtime-codex-gateway-provider.test.js.map +1 -0
  173. package/dist/__tests__/runtime-identity.test.d.ts +2 -0
  174. package/dist/__tests__/runtime-identity.test.d.ts.map +1 -0
  175. package/dist/__tests__/runtime-identity.test.js +117 -0
  176. package/dist/__tests__/runtime-identity.test.js.map +1 -0
  177. package/dist/__tests__/session-pause-provisioning.test.js +6 -1
  178. package/dist/__tests__/session-pause-provisioning.test.js.map +1 -1
  179. package/dist/__tests__/session-record-disclosure.test.d.ts +24 -0
  180. package/dist/__tests__/session-record-disclosure.test.d.ts.map +1 -0
  181. package/dist/__tests__/session-record-disclosure.test.js +494 -0
  182. package/dist/__tests__/session-record-disclosure.test.js.map +1 -0
  183. package/dist/__tests__/session.test.js +35 -0
  184. package/dist/__tests__/session.test.js.map +1 -1
  185. package/dist/__tests__/slug-validation.test.js +27 -21
  186. package/dist/__tests__/slug-validation.test.js.map +1 -1
  187. package/dist/__tests__/standards-injection.test.js +23 -1
  188. package/dist/__tests__/standards-injection.test.js.map +1 -1
  189. package/dist/__tests__/start-interactive-role.test.d.ts +22 -0
  190. package/dist/__tests__/start-interactive-role.test.d.ts.map +1 -0
  191. package/dist/__tests__/start-interactive-role.test.js +204 -0
  192. package/dist/__tests__/start-interactive-role.test.js.map +1 -0
  193. package/dist/__tests__/start-team-identity.test.d.ts +25 -0
  194. package/dist/__tests__/start-team-identity.test.d.ts.map +1 -0
  195. package/dist/__tests__/start-team-identity.test.js +307 -0
  196. package/dist/__tests__/start-team-identity.test.js.map +1 -0
  197. package/dist/__tests__/team-memory-commands.test.js +142 -19
  198. package/dist/__tests__/team-memory-commands.test.js.map +1 -1
  199. package/dist/__tests__/template-sync.test.d.ts +19 -0
  200. package/dist/__tests__/template-sync.test.d.ts.map +1 -0
  201. package/dist/__tests__/template-sync.test.js +270 -0
  202. package/dist/__tests__/template-sync.test.js.map +1 -0
  203. package/dist/__tests__/test-project.test.d.ts +18 -0
  204. package/dist/__tests__/test-project.test.d.ts.map +1 -0
  205. package/dist/__tests__/test-project.test.js +465 -0
  206. package/dist/__tests__/test-project.test.js.map +1 -0
  207. package/dist/__tests__/usage-per-call-grouping.test.d.ts +25 -0
  208. package/dist/__tests__/usage-per-call-grouping.test.d.ts.map +1 -0
  209. package/dist/__tests__/usage-per-call-grouping.test.js +952 -0
  210. package/dist/__tests__/usage-per-call-grouping.test.js.map +1 -0
  211. package/dist/__tests__/verify-hooks-check-all.test.d.ts +32 -0
  212. package/dist/__tests__/verify-hooks-check-all.test.d.ts.map +1 -0
  213. package/dist/__tests__/verify-hooks-check-all.test.js +553 -0
  214. package/dist/__tests__/verify-hooks-check-all.test.js.map +1 -0
  215. package/dist/__tests__/worktree-sync-fixture.test.d.ts +17 -0
  216. package/dist/__tests__/worktree-sync-fixture.test.d.ts.map +1 -0
  217. package/dist/__tests__/worktree-sync-fixture.test.js +519 -0
  218. package/dist/__tests__/worktree-sync-fixture.test.js.map +1 -0
  219. package/dist/__tests__/worktree-sync-hardening.test.d.ts +13 -0
  220. package/dist/__tests__/worktree-sync-hardening.test.d.ts.map +1 -0
  221. package/dist/__tests__/worktree-sync-hardening.test.js +919 -0
  222. package/dist/__tests__/worktree-sync-hardening.test.js.map +1 -0
  223. package/dist/__tests__/worktree-sync-shell-free.test.d.ts +25 -0
  224. package/dist/__tests__/worktree-sync-shell-free.test.d.ts.map +1 -0
  225. package/dist/__tests__/worktree-sync-shell-free.test.js +185 -0
  226. package/dist/__tests__/worktree-sync-shell-free.test.js.map +1 -0
  227. package/dist/__tests__/worktree.test.js +75 -1
  228. package/dist/__tests__/worktree.test.js.map +1 -1
  229. package/dist/agent-config/.mx-config-generation.json +2 -2
  230. package/dist/agent-config/AGENTS.md.template +2 -2
  231. package/dist/agent-config/agents/bar-raiser.md +34 -1
  232. package/dist/agent-config/agents/red-team.md +34 -1
  233. package/dist/agent-config/agents/security-reviewer.md +22 -1
  234. package/dist/agent-config/claude-md/CLAUDE.core.md +6 -1
  235. package/dist/agent-config/claude-md/CLAUDE.overlay.memnexus.md +1 -3
  236. package/dist/agent-config/coordination-rosters.generated.json +10 -0
  237. package/dist/agent-config/hooks/HOOK-TIMEOUTS.md +27 -1
  238. package/dist/agent-config/hooks/OR-TRUE-AUDIT.md +8 -8
  239. package/dist/agent-config/hooks/auto-capture-precompact.sh +279 -0
  240. package/dist/agent-config/hooks/auto-capture-session-end.sh +277 -0
  241. package/dist/agent-config/hooks/coordination-brief.sh +290 -245
  242. package/dist/agent-config/hooks/deploy-verification-reminder.sh +359 -22
  243. package/dist/agent-config/hooks/git-mutation-guard.sh +85 -13
  244. package/dist/agent-config/hooks/session-idle-gate.sh +177 -12
  245. package/dist/agent-config/hooks/subagent-write-guard.sh +670 -0
  246. package/dist/agent-config/hooks/worktree-guard.sh +36 -7
  247. package/dist/agent-config/settings.json +15 -2
  248. package/dist/agent-config/shared/creation-pr-review-checklist.md +317 -0
  249. package/dist/commands/bootstrap.d.ts.map +1 -1
  250. package/dist/commands/bootstrap.js +22 -10
  251. package/dist/commands/bootstrap.js.map +1 -1
  252. package/dist/commands/config.d.ts.map +1 -1
  253. package/dist/commands/config.js +28 -1
  254. package/dist/commands/config.js.map +1 -1
  255. package/dist/commands/coordination.d.ts +150 -8
  256. package/dist/commands/coordination.d.ts.map +1 -1
  257. package/dist/commands/coordination.js +519 -25
  258. package/dist/commands/coordination.js.map +1 -1
  259. package/dist/commands/create.d.ts +913 -1
  260. package/dist/commands/create.d.ts.map +1 -1
  261. package/dist/commands/create.js +4879 -345
  262. package/dist/commands/create.js.map +1 -1
  263. package/dist/commands/digest.d.ts.map +1 -1
  264. package/dist/commands/digest.js +91 -7
  265. package/dist/commands/digest.js.map +1 -1
  266. package/dist/commands/fleet-tmux.js +1 -1
  267. package/dist/commands/fleet-tmux.js.map +1 -1
  268. package/dist/commands/init.d.ts +8 -0
  269. package/dist/commands/init.d.ts.map +1 -1
  270. package/dist/commands/init.js +26 -1
  271. package/dist/commands/init.js.map +1 -1
  272. package/dist/commands/list.d.ts +32 -1
  273. package/dist/commands/list.d.ts.map +1 -1
  274. package/dist/commands/list.js +98 -5
  275. package/dist/commands/list.js.map +1 -1
  276. package/dist/commands/new.d.ts +2 -0
  277. package/dist/commands/new.d.ts.map +1 -1
  278. package/dist/commands/new.js +114 -21
  279. package/dist/commands/new.js.map +1 -1
  280. package/dist/commands/session.js +1 -1
  281. package/dist/commands/session.js.map +1 -1
  282. package/dist/commands/start.d.ts +38 -0
  283. package/dist/commands/start.d.ts.map +1 -1
  284. package/dist/commands/start.js +171 -8
  285. package/dist/commands/start.js.map +1 -1
  286. package/dist/commands/sync.d.ts +57 -0
  287. package/dist/commands/sync.d.ts.map +1 -1
  288. package/dist/commands/sync.js +117 -3
  289. package/dist/commands/sync.js.map +1 -1
  290. package/dist/commands/usage.d.ts +64 -0
  291. package/dist/commands/usage.d.ts.map +1 -1
  292. package/dist/commands/usage.js +96 -3
  293. package/dist/commands/usage.js.map +1 -1
  294. package/dist/commands/verify-hooks.d.ts +37 -1
  295. package/dist/commands/verify-hooks.d.ts.map +1 -1
  296. package/dist/commands/verify-hooks.js +114 -2
  297. package/dist/commands/verify-hooks.js.map +1 -1
  298. package/dist/commands/window.d.ts +10 -0
  299. package/dist/commands/window.d.ts.map +1 -0
  300. package/dist/commands/window.js +265 -0
  301. package/dist/commands/window.js.map +1 -0
  302. package/dist/commands/worktree-sync.d.ts +54 -0
  303. package/dist/commands/worktree-sync.d.ts.map +1 -0
  304. package/dist/commands/worktree-sync.js +135 -0
  305. package/dist/commands/worktree-sync.js.map +1 -0
  306. package/dist/index.d.ts.map +1 -1
  307. package/dist/index.js +122 -11
  308. package/dist/index.js.map +1 -1
  309. package/dist/lib/agent-session.d.ts.map +1 -1
  310. package/dist/lib/agent-session.js +6 -1
  311. package/dist/lib/agent-session.js.map +1 -1
  312. package/dist/lib/catalog.d.ts +321 -2
  313. package/dist/lib/catalog.d.ts.map +1 -1
  314. package/dist/lib/catalog.js +490 -11
  315. package/dist/lib/catalog.js.map +1 -1
  316. package/dist/lib/claude.d.ts +53 -23
  317. package/dist/lib/claude.d.ts.map +1 -1
  318. package/dist/lib/claude.js +306 -17
  319. package/dist/lib/claude.js.map +1 -1
  320. package/dist/lib/cli-build.d.ts +172 -0
  321. package/dist/lib/cli-build.d.ts.map +1 -0
  322. package/dist/lib/cli-build.js +654 -0
  323. package/dist/lib/cli-build.js.map +1 -0
  324. package/dist/lib/code-context-file.d.ts +109 -0
  325. package/dist/lib/code-context-file.d.ts.map +1 -0
  326. package/dist/lib/code-context-file.js +219 -0
  327. package/dist/lib/code-context-file.js.map +1 -0
  328. package/dist/lib/code-context-file.test.d.ts +19 -0
  329. package/dist/lib/code-context-file.test.d.ts.map +1 -0
  330. package/dist/lib/code-context-file.test.js +202 -0
  331. package/dist/lib/code-context-file.test.js.map +1 -0
  332. package/dist/lib/config-fidelity.d.ts +40 -20
  333. package/dist/lib/config-fidelity.d.ts.map +1 -1
  334. package/dist/lib/config-fidelity.js +144 -56
  335. package/dist/lib/config-fidelity.js.map +1 -1
  336. package/dist/lib/coordination-client.d.ts +73 -0
  337. package/dist/lib/coordination-client.d.ts.map +1 -1
  338. package/dist/lib/coordination-client.js +180 -8
  339. package/dist/lib/coordination-client.js.map +1 -1
  340. package/dist/lib/coordination-token.d.ts +304 -6
  341. package/dist/lib/coordination-token.d.ts.map +1 -1
  342. package/dist/lib/coordination-token.js +851 -52
  343. package/dist/lib/coordination-token.js.map +1 -1
  344. package/dist/lib/fleet-data.d.ts +34 -0
  345. package/dist/lib/fleet-data.d.ts.map +1 -1
  346. package/dist/lib/fleet-data.js +37 -9
  347. package/dist/lib/fleet-data.js.map +1 -1
  348. package/dist/lib/fleet-render.js +1 -1
  349. package/dist/lib/fleet-render.js.map +1 -1
  350. package/dist/lib/git-identity.d.ts +302 -0
  351. package/dist/lib/git-identity.d.ts.map +1 -0
  352. package/dist/lib/git-identity.js +546 -0
  353. package/dist/lib/git-identity.js.map +1 -0
  354. package/dist/lib/hook-staleness.d.ts +95 -0
  355. package/dist/lib/hook-staleness.d.ts.map +1 -0
  356. package/dist/lib/hook-staleness.js +304 -0
  357. package/dist/lib/hook-staleness.js.map +1 -0
  358. package/dist/lib/jsonl.d.ts +270 -2
  359. package/dist/lib/jsonl.d.ts.map +1 -1
  360. package/dist/lib/jsonl.js +472 -76
  361. package/dist/lib/jsonl.js.map +1 -1
  362. package/dist/lib/memory.d.ts +350 -23
  363. package/dist/lib/memory.d.ts.map +1 -1
  364. package/dist/lib/memory.js +751 -38
  365. package/dist/lib/memory.js.map +1 -1
  366. package/dist/lib/otel-env.d.ts +90 -0
  367. package/dist/lib/otel-env.d.ts.map +1 -0
  368. package/dist/lib/otel-env.js +98 -0
  369. package/dist/lib/otel-env.js.map +1 -0
  370. package/dist/lib/project-config.d.ts +167 -0
  371. package/dist/lib/project-config.d.ts.map +1 -1
  372. package/dist/lib/project-config.js +321 -5
  373. package/dist/lib/project-config.js.map +1 -1
  374. package/dist/lib/projects-registry.d.ts +82 -0
  375. package/dist/lib/projects-registry.d.ts.map +1 -0
  376. package/dist/lib/projects-registry.js +145 -0
  377. package/dist/lib/projects-registry.js.map +1 -0
  378. package/dist/lib/projects-registry.test.d.ts +2 -0
  379. package/dist/lib/projects-registry.test.d.ts.map +1 -0
  380. package/dist/lib/projects-registry.test.js +176 -0
  381. package/dist/lib/projects-registry.test.js.map +1 -0
  382. package/dist/lib/repo-guard-scope.d.ts +126 -0
  383. package/dist/lib/repo-guard-scope.d.ts.map +1 -0
  384. package/dist/lib/repo-guard-scope.js +249 -0
  385. package/dist/lib/repo-guard-scope.js.map +1 -0
  386. package/dist/lib/roster.d.ts +87 -0
  387. package/dist/lib/roster.d.ts.map +1 -0
  388. package/dist/lib/roster.js +332 -0
  389. package/dist/lib/roster.js.map +1 -0
  390. package/dist/lib/roster.test.d.ts +15 -0
  391. package/dist/lib/roster.test.d.ts.map +1 -0
  392. package/dist/lib/roster.test.js +299 -0
  393. package/dist/lib/roster.test.js.map +1 -0
  394. package/dist/lib/runtime/codex-adapter.d.ts.map +1 -1
  395. package/dist/lib/runtime/codex-adapter.js +5 -0
  396. package/dist/lib/runtime/codex-adapter.js.map +1 -1
  397. package/dist/lib/runtime/codex-config.d.ts +129 -0
  398. package/dist/lib/runtime/codex-config.d.ts.map +1 -1
  399. package/dist/lib/runtime/codex-config.js +225 -0
  400. package/dist/lib/runtime/codex-config.js.map +1 -1
  401. package/dist/lib/runtime/identity.d.ts +72 -0
  402. package/dist/lib/runtime/identity.d.ts.map +1 -0
  403. package/dist/lib/runtime/identity.js +128 -0
  404. package/dist/lib/runtime/identity.js.map +1 -0
  405. package/dist/lib/standards.d.ts +10 -1
  406. package/dist/lib/standards.d.ts.map +1 -1
  407. package/dist/lib/standards.js +8 -1
  408. package/dist/lib/standards.js.map +1 -1
  409. package/dist/lib/team-identity.d.ts +134 -0
  410. package/dist/lib/team-identity.d.ts.map +1 -0
  411. package/dist/lib/team-identity.js +193 -0
  412. package/dist/lib/team-identity.js.map +1 -0
  413. package/dist/lib/templates.d.ts +29 -2
  414. package/dist/lib/templates.d.ts.map +1 -1
  415. package/dist/lib/templates.js +1042 -82
  416. package/dist/lib/templates.js.map +1 -1
  417. package/dist/lib/test-project.d.ts +95 -0
  418. package/dist/lib/test-project.d.ts.map +1 -0
  419. package/dist/lib/test-project.js +103 -0
  420. package/dist/lib/test-project.js.map +1 -0
  421. package/dist/lib/vault.d.ts +60 -0
  422. package/dist/lib/vault.d.ts.map +1 -0
  423. package/dist/lib/vault.js +102 -0
  424. package/dist/lib/vault.js.map +1 -0
  425. package/dist/lib/vault.test.d.ts +17 -0
  426. package/dist/lib/vault.test.d.ts.map +1 -0
  427. package/dist/lib/vault.test.js +169 -0
  428. package/dist/lib/vault.test.js.map +1 -0
  429. package/dist/lib/window-view.d.ts +63 -0
  430. package/dist/lib/window-view.d.ts.map +1 -0
  431. package/dist/lib/window-view.js +217 -0
  432. package/dist/lib/window-view.js.map +1 -0
  433. package/dist/lib/window-view.test.d.ts +2 -0
  434. package/dist/lib/window-view.test.d.ts.map +1 -0
  435. package/dist/lib/window-view.test.js +168 -0
  436. package/dist/lib/window-view.test.js.map +1 -0
  437. package/dist/lib/worktree-sync.d.ts +439 -0
  438. package/dist/lib/worktree-sync.d.ts.map +1 -0
  439. package/dist/lib/worktree-sync.js +1167 -0
  440. package/dist/lib/worktree-sync.js.map +1 -0
  441. package/dist/lib/worktree.d.ts +58 -1
  442. package/dist/lib/worktree.d.ts.map +1 -1
  443. package/dist/lib/worktree.js +91 -4
  444. package/dist/lib/worktree.js.map +1 -1
  445. package/dist/standards/decision-request.md +8 -0
  446. package/dist/standards/po-communication-standard.md +126 -0
  447. package/dist/standards/report-format.md +8 -0
  448. package/dist/standards/writing-standard.md +146 -4
  449. package/package.json +1 -1
  450. package/scripts/generate-claude.mjs +119 -46
@@ -0,0 +1,670 @@
1
+ #!/bin/bash
2
+ # subagent-write-guard — PreToolUse(Bash) guard that refuses four `gh` write
3
+ # commands when the caller is a DISPATCHED SUBAGENT, and allows them on the
4
+ # MAIN THREAD.
5
+ #
6
+ # Contract: reads the tool-call JSON on stdin.
7
+ # exit 0 = allow, exit 2 = block (a non-zero exit blocks the Bash command).
8
+ #
9
+ # WHY. Product-owner ruling, given 2026-08-21 (#12): "The sub-agent shouldn't be
10
+ # able to merge the PR, but the team lead should." Commits 4ac2951 and 7c23b68
11
+ # gave the bar-raiser and red-team agent types Bash so their gates could run real
12
+ # commands. The security review on that change found settings.json allows bare
13
+ # `Bash` and denies none of `gh pr merge`, `gh pr review`, `gh issue close` or
14
+ # `gh api -X`, so a grader that followed an instruction planted in the pull
15
+ # request it was grading could approve or merge that pull request itself. The
16
+ # party who wrote a change never produces the verdict on it, and the party who
17
+ # produces the verdict does not get to act on it either.
18
+ #
19
+ # WHY NOT A permissions.deny ENTRY. `permissions.deny` in settings.json binds
20
+ # every session in the worktree, the team lead's included. A deny on
21
+ # `Bash(gh pr merge *)` would satisfy the first half of the ruling and break the
22
+ # second. Permission rules have no caller dimension; a hook does.
23
+ #
24
+ # THE SIGNAL IS .agent_id. The harness sets it only inside a dispatched subagent
25
+ # and never on the main thread. Two sources, checked 2026-08-21:
26
+ #
27
+ # 1. The published hook reference, https://code.claude.com/docs/en/hooks, hook
28
+ # input fields table:
29
+ # agent_id — "Unique identifier for the subagent. Present only when the
30
+ # hook fires inside a subagent call. Use this to distinguish
31
+ # subagent hook calls from main-thread calls."
32
+ # 2. The installed bundle (version 2.1.238,
33
+ # ~/.npm-global/lib/node_modules/@anthropic-ai/claude-code/bin/claude.exe),
34
+ # zod .describe() on agent_id: "Subagent identifier. Present only when the
35
+ # hook fires from within a subagent (e.g., a tool called by an AgentTool
36
+ # worker). Absent for the main thread, even in --agent sessions. Use this
37
+ # field (not agent_type) to distinguish subagent calls from main-thread
38
+ # calls."
39
+ #
40
+ # memory-checkpoint-reminder.sh already branches on .agent_id for the same
41
+ # reason (#5136), so this file adds no new dependency on harness input.
42
+ #
43
+ # .agent_id, not .agent_type: agent_type is ALSO set on the main thread of an
44
+ # --agent session, so branching on it would refuse a legitimate lead. Pinned by
45
+ # a test.
46
+ #
47
+ # Measured directly, 2026-08-21: a PreToolUse(Bash) call from a dispatched
48
+ # subagent carried agent_id (17 characters), agent_type
49
+ # ("implementation-engineer"), and the DISPATCHER'S session_id and
50
+ # transcript_path. session_id and transcript_path therefore cannot separate a
51
+ # subagent from its lead. agent_id is the only field that does.
52
+ #
53
+ # WHAT IS REFUSED, for a subagent:
54
+ # gh pr merge the ruling's subject
55
+ # gh pr review approving or requesting changes is the reviewer's verdict,
56
+ # and settings.json denies it to nobody
57
+ # gh issue close closing the issue a change is graded against
58
+ # gh api when the effective HTTP method is not GET
59
+ #
60
+ # `gh api` METHOD INFERENCE. `-X`/`--method` names the method outright. With no
61
+ # such flag, `gh` sends GET unless a field flag is present (-f, -F, --field,
62
+ # --raw-field, --input), in which case it sends POST. Both forms are refused,
63
+ # because the second is a complete route around the first: `gh api
64
+ # repos/{owner}/{repo}/pulls/1/merge -f merge_method=squash` merges a pull
65
+ # request and contains no `-X`. The ruling names `gh api -X`; refusing the
66
+ # fieldful no-`-X` form is this file's judgement, recorded here because it widens
67
+ # the named scope by one form.
68
+ #
69
+ # `--help` / `-h` immediately after the subcommand is allowed: it prints usage
70
+ # and makes no API call.
71
+ #
72
+ # FAILURE DIRECTION — CLOSED, and this is the opposite of gh-pr-edit-guard.sh.
73
+ # That guard is a signpost about a broken binary, and the cost of a miss is one
74
+ # confusing error, so it fails open. This one answers "may this caller take an
75
+ # irreversible, externally-visible action", and a guard that cannot answer must
76
+ # not answer yes. Three states, kept apart:
77
+ # * agent_id present → subagent → refuse a matched command
78
+ # * agent_id absent, envelope well-formed → main thread → allow
79
+ # * envelope not well-formed, or jq missing → INDETERMINATE → refuse a matched
80
+ # command, saying that identity could not be established
81
+ # "Well-formed" means the payload parses as a JSON object carrying session_id or
82
+ # hook_event_name, both of which the harness sets on every call. The absence of
83
+ # agent_id is only evidence of a main thread when there is an envelope for it to
84
+ # be absent from.
85
+ #
86
+ # The indeterminate branch refuses ONLY the four matched command families, never
87
+ # an unrelated Bash call. With jq missing there is no parse, so that branch scans
88
+ # the raw stdin text for the same patterns without segment splitting. It
89
+ # over-matches prose that quotes those commands. That is the intended direction
90
+ # for a degraded environment, and it costs a blocked message, not lost work.
91
+ #
92
+ # The degraded scan still needs `grep`. With grep also gone the hook allows, and
93
+ # that is stated rather than fixed: refusing every Bash call in a worktree whose
94
+ # PATH has lost grep leaves no route to repair it from inside the session, which
95
+ # is a worse outcome than the one it prevents. Named here so the limit is a
96
+ # recorded decision and not an unnoticed hole.
97
+ #
98
+ # RULING HISTORY (recorded by the bootstrap lead, who witnessed both rulings
99
+ # in the lead's own session; the implementer did not witness them and did not
100
+ # write this block):
101
+ # 2026-08-21 "The sub-agent shouldn't be able to merge the PR, but the team
102
+ # lead should." This is the ruling the four families below enforce.
103
+ # 2026-08-22 A read-only variant was chosen ("block gh pr comment too") and
104
+ # reversed by the product owner minutes later: "I think they should
105
+ # be able to comment on a PR, but not merge them." gh pr comment
106
+ # stays allowed for subagents. No code changed for either message;
107
+ # the implementer halted on the first and was countermanded before
108
+ # writing anything.
109
+ #
110
+ # MATCHING IS ASYMMETRIC, and deliberately so (security review of 4dd3212, one
111
+ # Must Fix). The main thread runs all four families legitimately, so a false
112
+ # refusal there is a real cost. It is not matched precisely; it is not matched AT
113
+ # ALL. The main-thread test is a bare `exit 0` before any pattern is evaluated. An
114
+ # earlier version of this paragraph claimed the main thread "keeps the precise
115
+ # segment-anchored GHPFX", which was false in the reassuring direction and is
116
+ # corrected here. A subagent has NO legitimate use of any of the four, so
117
+ # precision buys nothing on that path and costs a bypass. The subagent path
118
+ # matches with the unanchored GHSUB patterns, which accept any non-word character
119
+ # before `gh`.
120
+ #
121
+ # THE WHOLE ASYMMETRY RESTS ON ONE FIELD. Any dispatch path that delivers a
122
+ # subagent WITHOUT agent_id receives the main-thread path, which is now an
123
+ # unconditional allow with no matching at all. That is the single point of
124
+ # failure, it is not defended anywhere else in this file, and it is the first
125
+ # thing to re-measure if the harness changes how subagents are launched.
126
+ #
127
+ # What the first version got wrong: GHPFX admits a launcher word followed by an
128
+ # optional NUMERIC argument only. `timeout 30 gh pr merge 1` was refused and
129
+ # `timeout -s KILL 30 gh pr merge 1` was not, which is the exact wrapper AGENTS.md
130
+ # tells every agent to use. Measured on 4dd3212, subagent payload, jq present:
131
+ # `timeout -s KILL 30`, `env -i` and `nice -n 5` before the command each exited 0,
132
+ # meaning allowed. Enumerating launcher flags is a losing game; not needing the
133
+ # enumeration is the fix.
134
+ #
135
+ # ACCEPTED COST, stated as two lists rather than as general coverage. A subagent
136
+ # that names one of these commands is refused unless the naming happens in a
137
+ # carrier this file recognises.
138
+ #
139
+ # COVERED, quoted text blanked before matching:
140
+ # echo, printf, cat, tee, write
141
+ # grep, rg, jq pattern arguments, never executed
142
+ # mx / mx-agent with --content, --body or --query the mandated memory path
143
+ # gh with --body, --title or --comment
144
+ # git commit / tag / notes with -m or --message
145
+ #
146
+ # NOT COVERED, and a subagent naming a refused command through one of these is
147
+ # refused. Measured, not assumed:
148
+ # sed 's/gh pr merge/x/' a carrier, but not on the list
149
+ # git log --grep='gh pr merge' only commit/tag/notes are carriers
150
+ # awk, perl -e, python3 -c each can execute what it is handed
151
+ # ANY carrier holding $( ) or a backtick, including the covered ones, because
152
+ # a substitution executes (see check_sink). This invariant was FALSE for
153
+ # heredocs until f90382b's successor: an unquoted delimiter expands the body,
154
+ # and the body was being dropped before check_sink ever saw it. See
155
+ # strip_heredocs. A heredoc with a QUOTED delimiter is inert and stays
156
+ # allowed, which is the one place a subagent can write a substitution
157
+ # verbatim.
158
+ #
159
+ # The refusal says what to do instead, so the cost is one blocked call and a
160
+ # re-word. A subagent that needs a report attached to a pull request hands it to
161
+ # the lead, who posts it with attribution.
162
+ #
163
+ # SCOPE — WHAT THIS IS NOT. This is a guardrail, not a security boundary, the
164
+ # same posture as git-mutation-guard.sh. It is bypassable by design: indirection
165
+ # through a script file, shell-variable or `eval` assembly, an alias, a git
166
+ # push that triggers a merge workflow, or a payload handed to a remote shell all
167
+ # escape it, and no attempt is made to close them. It stops a subagent that has
168
+ # been told to merge from merging, which is the recorded failure. It does not
169
+ # contain a subagent that intends to.
170
+ #
171
+ # NOT REFUSED, deliberately: `gh pr close`, `gh pr reopen`, `gh issue reopen`,
172
+ # `gh pr comment`, `gh pr create`, `gh workflow run` (workflow-dispatch-guard.sh
173
+ # owns that one) and every read form. The ruling names four commands. Widening
174
+ # past a named ruling is the product owner's call, not this file's.
175
+ #
176
+ # OUTPUT ROUTING (KI-HOOKS-STDERR-SYSTEMIC): the harness surfaces a blocking
177
+ # hook's STDERR, not its stdout. All refuse() output goes to stderr (>&2). The
178
+ # allow paths stay silent.
179
+ MXHT_HOOK=subagent-write-guard MXHT_EVENT=PreToolUse MXHT_TOOL=Bash
180
+ # >>> SHARED hook-timing probe v1
181
+ # Diagnostic instrumentation: records how long THIS hook ran, so hook time can be
182
+ # separated from time spent waiting on the API when a session stalls mid-stream.
183
+ # Appends one line to a local, gitignored log. Read it with
184
+ # mx-agent-system/scripts/hook-timing-report.sh. Nothing is transmitted anywhere.
185
+ #
186
+ # FAIL SILENT IS DELIBERATE HERE, and it is the opposite of this repo's usual
187
+ # fail-loud direction. Do not "correct" it to fail-loud by applying the fleet
188
+ # default. A diagnostic that can break the gate it observes is worse than no
189
+ # diagnostic: an unwritable path or a full disk would otherwise take out every
190
+ # guard on the PreToolUse path at once. Every step below is guarded and every
191
+ # failure path is a silent no-op that leaves the hook's behaviour untouched.
192
+ #
193
+ # NEAR-ZERO OVERHEAD: bash builtins only on the hot path ($EPOCHREALTIME, printf
194
+ # -T, one read, one append). No subprocess, no network, no JSON parsing, and the
195
+ # hook's stdin is never touched (the hook owns it). mkdir runs at most once per
196
+ # worktree. Kill switch: MX_HOOK_TIMING=0.
197
+ MXHT_T0="${EPOCHREALTIME:-}"
198
+ _mxht_emit() {
199
+ [ -z "${MXHT_DONE:-}" ] || return 0 # an ERR trap that exits fires EXIT too
200
+ MXHT_DONE=1
201
+ [ "${MX_HOOK_TIMING:-1}" != "0" ] || return 0
202
+ [ -n "${MXHT_T0:-}" ] && [ -n "${EPOCHREALTIME:-}" ] || return 0
203
+ local root="${CLAUDE_PROJECT_DIR:-${CLAUDE_WORKTREE_PATH:-}}"
204
+ [ -n "$root" ] && [ -d "$root" ] || return 0
205
+ local dir="$root/.mx-hook-timing"
206
+ [ -d "$dir" ] || mkdir -p "$dir" 2>/dev/null || return 0
207
+ # EPOCHREALTIME is seconds.microseconds; the separator is locale-dependent, so
208
+ # strip both forms and subtract as plain integer microseconds.
209
+ local dur=-1
210
+ { dur=$(( ${EPOCHREALTIME//[.,]/} - ${MXHT_T0//[.,]/} )); } 2>/dev/null
211
+ [ "$dur" -ge 0 ] 2>/dev/null || return 0
212
+ local stamp hour
213
+ printf -v stamp '%(%Y-%m-%d %H:%M:%S)T' -1
214
+ printf -v hour '%(%H)T' -1
215
+ # BOUNDED BY CONSTRUCTION: a 24-slot ring, one file per hour-of-day, so the log
216
+ # is at most 24 files each holding one hour of this machine's hook calls. The
217
+ # first writer into a slot on a new day truncates it instead of appending —
218
+ # detected by reading the slot's first line, which is a builtin read (no stat,
219
+ # no rm, no find, no cron). Two writers can race that truncation and lose a
220
+ # line or two at an hour boundary; that is acceptable for a sampling diagnostic.
221
+ local f="$dir/h$hour.log" first="" line
222
+ # THE SLOT MUST BE AN ORDINARY FILE, OR NOT EXIST YET (security review, #4848).
223
+ # Anything else is a silent no-op. Two concrete failures if this check is
224
+ # dropped: a FIFO at the slot makes the `read` below block until the hook's
225
+ # settings.json timeout kills it, and a PreToolUse guard killed that way loses
226
+ # its exit 2 — the command it meant to block then runs; a symlink at the slot
227
+ # sends the day-rollover truncation onto the symlink's target. The `-e || -L`
228
+ # wrapper is what lets a not-yet-created slot through (-e is false for a
229
+ # dangling symlink, so -L is needed to catch that case too).
230
+ if [ -e "$f" ] || [ -L "$f" ]; then
231
+ [ -f "$f" ] && [ ! -L "$f" ] || return 0
232
+ fi
233
+ [ -r "$f" ] && { read -r first < "$f"; } 2>/dev/null
234
+ printf -v line '%s dur_us=%s hook=%s event=%s ppid=%s tool=%s\n' \
235
+ "$stamp" "$dur" "${MXHT_HOOK:--}" "${MXHT_EVENT:--}" "$PPID" "${MXHT_TOOL:--}"
236
+ # One printf of ~90 bytes to an O_APPEND fd is atomic under PIPE_BUF, so
237
+ # concurrent hooks cannot interleave partial lines.
238
+ # The 2>/dev/null wraps the GROUP, not the printf: a failing redirection is
239
+ # reported by the shell before an inner 2>/dev/null would be in effect, and a
240
+ # "Permission denied" on stderr from a PreToolUse hook is surfaced to the model.
241
+ if [ "${first%% *}" = "${stamp%% *}" ]; then
242
+ { printf '%s' "$line" >>"$f"; } 2>/dev/null
243
+ else
244
+ { printf '%s' "$line" >"$f"; } 2>/dev/null
245
+ fi
246
+ }
247
+ # `|| :` IS LOAD-BEARING — do not simplify to `trap _mxht_emit EXIT`. The hooks
248
+ # this block is inlined into run under `set -e`, and the EXIT trap runs with
249
+ # errexit still in force. Without `|| :`, a non-zero return from the probe — the
250
+ # final printf failing on an unwritable slot is the reachable case — replaces the
251
+ # hook's own exit status: a PreToolUse guard's exit 2 becomes exit 1, which the
252
+ # harness reads as "error", not "block", so the command it meant to block runs.
253
+ # Pinned verbatim by hook-timing-parity.test.sh.
254
+ trap '_mxht_emit || :' EXIT
255
+ # <<< SHARED hook-timing probe v1
256
+
257
+
258
+ set -uo pipefail
259
+ # NO `set -e` HERE, and the shared probe block above says "the hooks this block is
260
+ # inlined into run under `set -e`". That sentence is inaccurate for TWELVE of the
261
+ # sixteen instrumented hooks, this one included; only four set -e. Counted with
262
+ # `grep -LE '^set -[a-z]*e'` over the list in hook-timing-parity.test.sh. It is NOT corrected
263
+ # in place: hook-timing-parity.test.sh pins the block byte-for-byte across all 16
264
+ # instrumented hooks, so editing it here alone fails that gate, and editing all 16
265
+ # is a fleet-wide change that needs its own review. The `|| :` on the EXIT trap
266
+ # stays verbatim regardless — under `-uo pipefail` it is harmless, and it is what
267
+ # keeps a failing probe from replacing this guard's exit 2 with exit 1 in the hooks
268
+ # that DO set -e. Recorded here so the inaccuracy is known rather than unnoticed.
269
+
270
+ # Read stdin with the bash redirection builtin rather than `cat`. A hook whose
271
+ # very first statement needs an external binary cannot fail in a controlled way
272
+ # when the thing that is broken is the environment itself: with PATH unset or
273
+ # wrong, `cat` exits 127 and the hook exits 127 with it, which the harness reads
274
+ # as "error" and not as "block".
275
+ INPUT=$(</dev/stdin)
276
+
277
+ # ── caller identity ───────────────────────────────────────────────────────────
278
+ # Three states, see FAILURE DIRECTION in the header. AGENT_ID is read only as a
279
+ # 0/1 branch selector and is NEVER printed: harness-supplied text must not reach
280
+ # another agent's context through a refusal message.
281
+ IS_SUBAGENT=0
282
+ ENVELOPE_OK=0
283
+ COMMAND=""
284
+ JQ_OK=0
285
+ if command -v jq >/dev/null 2>&1; then
286
+ JQ_OK=1
287
+ fi
288
+
289
+ if [[ $JQ_OK -eq 1 ]]; then
290
+ if printf '%s' "$INPUT" | jq -e 'type == "object" and (has("session_id") or has("hook_event_name"))' >/dev/null 2>&1; then
291
+ ENVELOPE_OK=1
292
+ fi
293
+ AGENT_ID=$(printf '%s' "$INPUT" | jq -r '.agent_id // empty' 2>/dev/null) || AGENT_ID=""
294
+ # `if`, not `[[ ... ]] && VAR=1` — never leave a failing test as the last
295
+ # statement in reach of a non-zero exit.
296
+ if [[ -n "$AGENT_ID" ]]; then
297
+ IS_SUBAGENT=1
298
+ fi
299
+ COMMAND=$(printf '%s' "$INPUT" | jq -r '.tool_input.command // .command // empty' 2>/dev/null) || COMMAND=""
300
+ fi
301
+
302
+ # MAIN THREAD → ALLOW, silently, before any scanning. This is the half of the
303
+ # ruling that a permissions.deny entry cannot express.
304
+ if [[ $ENVELOPE_OK -eq 1 && $IS_SUBAGENT -eq 0 ]]; then
305
+ exit 0
306
+ fi
307
+
308
+ # --- backslash line-continuation folding -------------------------------------
309
+ # `gh pr \<newline> merge 1` is ONE command to the shell and two records to awk,
310
+ # so every downstream matcher sees `gh pr` and `merge 1` separately and neither
311
+ # matches. Fold the continuations first. Found by the security review of 4dd3212.
312
+ fold_continuations() {
313
+ sed -e :a -e '/\\$/N; s/\\\n[[:space:]]*/ /; ta' 2>/dev/null || true
314
+ }
315
+
316
+ # --- heredoc-body stripping (consumer-aware AND expansion-aware) -------------
317
+ # Three outcomes for a body, not two:
318
+ # keepbody 1 the CONSUMER executes it (bash/sh/zsh/dash/ksh/eval/source/`.`,
319
+ # or a pipe into one). Every line is kept.
320
+ # keepbody 2 the consumer does not execute it, but the delimiter is UNQUOTED,
321
+ # so the SHELL expands it first. Only the lines carrying `$(` or a
322
+ # backtick are kept.
323
+ # keepbody 0 the delimiter is quoted (<<'EOF' or <<"EOF"), so the body is
324
+ # inert text. Dropped whole.
325
+ # The operator line itself is always preserved.
326
+ #
327
+ # This no longer mirrors git-mutation-guard.sh and gh-pr-edit-guard.sh, which
328
+ # still carry the two-outcome shape. That divergence is deliberate and is tracked
329
+ # in #49; nothing pins these functions across the three files.
330
+ strip_heredocs() {
331
+ awk '
332
+ !inbody {
333
+ line = $0
334
+ if (match(line, /<<[-]?[[:space:]]*["'"'"']?[A-Za-z_][A-Za-z0-9_]*["'"'"']?/)) {
335
+ d = substr(line, RSTART, RLENGTH)
336
+ # ORDER IS LOAD-BEARING. Read the quoting off the RAW match, BEFORE the
337
+ # two gsubs below strip the quotes out of d. Computed after them this test
338
+ # is always true, and the <<QUOTED bodies the shell leaves inert would be
339
+ # scanned as if they expanded.
340
+ unquoted = (d !~ /["'"'"']/)
341
+ gsub(/<<[-]?[[:space:]]*["'"'"']?/, "", d)
342
+ gsub(/["'"'"'].*/, "", d)
343
+ delim = d
344
+ inbody = 1
345
+ keepbody = 0
346
+ if (line ~ /(^|[^[:alnum:]_])(bash|sh|zsh|dash|ksh|eval|source)([^[:alnum:]_]|$)/) keepbody = 1
347
+ if (line ~ /(^|[[:space:]])\.[[:space:]]/) keepbody = 1
348
+ if (line ~ /\|[^|]*(bash|sh|zsh|dash|ksh|eval)([^[:alnum:]_]|$)/) keepbody = 1
349
+ # WHO EXECUTES THE BODY IS TWO QUESTIONS, NOT ONE. The three tests above
350
+ # ask whether the CONSUMER runs the body. They never asked whether the
351
+ # SHELL expands it first. With an unquoted delimiter bash performs command
352
+ # substitution on every body line before the consumer sees anything, so
353
+ # `cat <<EOF` holding `$(gh pr merge 1)` runs the merge and hands cat the
354
+ # output. Measured on f90382b: that form, the <<-EOF form, the backtick
355
+ # form, `cat > f <<EOF` and `tee f <<EOF` were all allowed, while the
356
+ # shell expanded every one of them. keepbody 2 keeps ONLY the lines that
357
+ # carry a substitution, so a heredoc of plain prose naming a refused
358
+ # command stays allowed and the invariant in ACCEPTED COST holds again.
359
+ if (keepbody == 0 && unquoted) keepbody = 2
360
+ }
361
+ print line
362
+ next
363
+ }
364
+ {
365
+ t = $0
366
+ gsub(/^[[:space:]]+/, "", t)
367
+ if (t == delim) { inbody = 0; keepbody = 0; next }
368
+ if (keepbody == 1) print $0
369
+ else if (keepbody == 2 && ($0 ~ /\$\(/ || $0 ~ /`/)) print $0
370
+ }
371
+ ' 2>/dev/null || true
372
+ }
373
+
374
+ # --- segment split + sink-scoped quote blanking -------------------------------
375
+ # Emits one command segment per line, splitting on unquoted `;` `&&` `||` `|`.
376
+ # Segments whose leading command is a text SINK have their quoted content
377
+ # blanked, so prose that merely mentions `gh pr edit` is not mistaken for an
378
+ # invocation. `gh` itself counts as a sink only for its own --body/--title/
379
+ # --comment ARGUMENT text; the segment's own `gh pr edit ...` words are outside
380
+ # the quotes and still scanned, so `gh pr edit --body "x"` is still blocked
381
+ # while `gh pr comment 1 --body "use gh pr edit"` is not.
382
+ split_segments() {
383
+ awk '
384
+ function get_lead_cmd(seg, s) {
385
+ s = seg; gsub(/^[[:space:]]+/, "", s)
386
+ while (match(s, /^[A-Za-z_][A-Za-z0-9_]*=[^[:space:]]*[[:space:]]+/))
387
+ s = substr(s, RSTART + RLENGTH)
388
+ while (match(s, /^[0-9]*[<>][>&]?[^[:space:]]*[[:space:]]+/))
389
+ s = substr(s, RSTART + RLENGTH)
390
+ if (match(s, /^[^[:space:]]+/)) return substr(s, RSTART, RLENGTH)
391
+ return ""
392
+ }
393
+ function check_sink(seg, cmd, s, sc) {
394
+ # A COMMAND SUBSTITUTION IS NEVER TEXT. This clause runs FIRST, before any
395
+ # carrier is recognised, and it is the whole reason blanking was unsafe: the
396
+ # blanker erases a quoted region, and `$( )` or a backtick inside that region
397
+ # is text to the blanker and an executed command to the shell. So
398
+ # `echo "$(gh pr merge 1)"` merged the pull request while this function
399
+ # classified it as prose. Measured on 6d6249c for echo, printf, cat, tee,
400
+ # git commit -m, gh --body and the backtick form: all seven allowed. The
401
+ # degraded no-jq branch does no blanking and caught every one of them, so the
402
+ # guard was WEAKER with jq than without. Returning 0 here means "not a sink",
403
+ # which leaves the quotes intact and lets the matcher see the invocation.
404
+ if (seg ~ /\$\(/ || seg ~ /`/) return 0
405
+ cmd = get_lead_cmd(seg)
406
+ if (cmd == "echo" || cmd == "printf" || cmd == "cat" || cmd == "tee" || cmd == "write")
407
+ return 1
408
+ # PATTERN CARRIERS. These take their argument as a pattern or a literal and
409
+ # never execute it. Without them a subagent cannot search for, or record, the
410
+ # name of a command this guard refuses.
411
+ if (cmd == "grep" || cmd == "rg" || cmd == "jq")
412
+ return 1
413
+ # THE MEMORY PATH. AGENTS.md makes MemNexus the sole memory store, so a
414
+ # memory-creator recording the lesson about this guard has to be able to name
415
+ # the command in --content. Measured on 6d6249c: it could not.
416
+ if ((cmd == "mx" || cmd == "mx-agent") && (seg ~ /--content/ || seg ~ /--body/ || seg ~ /--query/))
417
+ return 1
418
+ if (cmd == "gh" && (seg ~ /--body/ || seg ~ /--title/ || seg ~ /--comment/))
419
+ return 1
420
+ if (cmd == "git") {
421
+ s = seg; gsub(/^[[:space:]]+/, "", s); sub(/^git[[:space:]]+/, "", s)
422
+ while (match(s, /^(-C[[:space:]]+[^[:space:]]+|-c[[:space:]]+[^[:space:]]+|--[a-z][-a-z0-9]*(=[^[:space:]]*)?)[[:space:]]+/))
423
+ s = substr(s, RSTART + RLENGTH)
424
+ if (match(s, /^[^[:space:]]+/)) {
425
+ sc = substr(s, RSTART, RLENGTH)
426
+ if ((sc == "commit" || sc == "tag" || sc == "notes") && \
427
+ (seg ~ /-[a-zA-Z]*m[[:space:]]/ || seg ~ /--message/))
428
+ return 1
429
+ }
430
+ }
431
+ return 0
432
+ }
433
+ function blank_quotes(seg, n, i, c, out, isq, idq) {
434
+ n = length(seg); out = ""; isq = 0; idq = 0
435
+ for (i = 1; i <= n; i++) {
436
+ c = substr(seg, i, 1)
437
+ if (isq) {
438
+ if (c == "'"'"'") { isq = 0; out = out c } else out = out " "
439
+ continue
440
+ }
441
+ if (idq) {
442
+ if (c == "\\") { out = out " "; i++; continue }
443
+ if (c == "\"") { idq = 0; out = out c } else out = out " "
444
+ continue
445
+ }
446
+ if (c == "'"'"'") { isq = 1; out = out c; continue }
447
+ if (c == "\"") { idq = 1; out = out c; continue }
448
+ out = out c
449
+ }
450
+ return out
451
+ }
452
+ {
453
+ line = $0; n = length(line)
454
+ isq = 0; idq = 0; num = 0; seg_s = 1
455
+ for (i = 1; i <= n; i++) {
456
+ c = substr(line, i, 1)
457
+ if (isq) { if (c == "'"'"'") isq = 0; continue }
458
+ if (idq) { if (c == "\\") { i++; continue }; if (c == "\"") idq = 0; continue }
459
+ if (c == "'"'"'") { isq = 1; continue }
460
+ if (c == "\"") { idq = 1; continue }
461
+ sp = 0; sl = 1
462
+ if (c == ";") sp = 1
463
+ else if (c == "&" && i < n && substr(line, i+1, 1) == "&") { sp = 1; sl = 2 }
464
+ else if (c == "|" && i < n && substr(line, i+1, 1) == "|") { sp = 1; sl = 2 }
465
+ else if (c == "|") sp = 1
466
+ if (sp) {
467
+ num++; segs[num] = substr(line, seg_s, i - seg_s)
468
+ seg_s = i + sl; if (sl == 2) i++
469
+ }
470
+ }
471
+ num++; segs[num] = substr(line, seg_s)
472
+ for (s = 1; s <= num; s++) {
473
+ if (check_sink(segs[s])) print blank_quotes(segs[s])
474
+ else print segs[s]
475
+ }
476
+ }
477
+ ' 2>/dev/null || true
478
+ }
479
+
480
+ # Reduce untrusted command text to something safe to print back: first line
481
+ # only, control characters and non-ASCII removed, truncated. The command arrives
482
+ # from the model and is echoed into a message another agent reads.
483
+ sanitize() {
484
+ printf '%s' "$1" \
485
+ | head -n 1 \
486
+ | LC_ALL=C tr -d '\000-\037\177' \
487
+ | LC_ALL=C tr -c 'A-Za-z0-9 _.,:/=@+~^-' '?' \
488
+ | cut -c1-200
489
+ }
490
+
491
+ refuse() {
492
+ local family="$1" reason="$2" safe_cmd
493
+ safe_cmd="$(sanitize "$COMMAND")"
494
+ [[ -n "$safe_cmd" ]] || safe_cmd="(unavailable: the hook payload could not be parsed)"
495
+ {
496
+ echo ""
497
+ echo "BLOCKED: $family"
498
+ echo " Command: $safe_cmd"
499
+ echo ""
500
+ if [[ "$reason" == "subagent" ]]; then
501
+ echo "WHY: this tool call comes from a DISPATCHED SUBAGENT. Product-owner ruling,"
502
+ echo "2026-08-21 (#12): a subagent does not merge the pull request, and the team lead"
503
+ echo "does. Merging, approving, requesting changes, closing an issue, and writing"
504
+ echo "through the API are the lead's to take from the main session. A subagent that"
505
+ echo "graded the change must not also be able to act on its own verdict, and a"
506
+ echo "subagent that read an instruction planted in a pull request must not be able to"
507
+ echo "merge that pull request."
508
+ else
509
+ echo "WHY: this hook could not establish whether the call comes from the main session"
510
+ echo "or from a dispatched subagent. The harness sets agent_id only inside a subagent,"
511
+ echo "and reading it needs a parseable payload and jq. Neither was available here, so"
512
+ echo "the absence of agent_id proves nothing. A guard that cannot tell who is asking"
513
+ echo "does not answer yes to an irreversible action."
514
+ echo ""
515
+ echo "If jq is missing from this environment, that is the thing to fix:"
516
+ echo " command -v jq"
517
+ fi
518
+ echo ""
519
+ echo "WHAT TO DO INSTEAD:"
520
+ echo " Report the state to your team lead and stop: which pull request, what the"
521
+ echo " checks say, and what you would do. The lead runs the merge from the main"
522
+ echo " session."
523
+ echo " Read forms are untouched: gh pr view, gh pr checks, gh pr diff, gh api (GET),"
524
+ echo " gh issue view. gh pr comment and gh pr create are untouched too."
525
+ echo ""
526
+ } >&2
527
+ exit 2
528
+ }
529
+
530
+ # ── matching ──────────────────────────────────────────────────────────────────
531
+ # `gh` must START a command segment: at start-of-line (segments are emitted one
532
+ # per line), after a separator, or after a shell keyword — optionally preceded by
533
+ # leading redirections, env-var assignments, and launcher words that exec their
534
+ # argv, and optionally by a path whose final component is `gh`. Mirrors
535
+ # gh-pr-edit-guard.sh's GHSEG, deliberately: the guards should agree about what
536
+ # counts as an invocation rather than a mention (the loose-match failure is
537
+ # #4804).
538
+ # The segment-anchored GHPFX and its four RE_* patterns lived here until the
539
+ # fourth security pass found them unreferenced. After the main thread was moved to
540
+ # an early exit, nothing evaluated them: the subagent path uses SUB_*, the
541
+ # degraded path uses RAW_*, and both are unanchored. Deleted rather than kept,
542
+ # because a reader finding a precise pattern in the file reasonably concludes
543
+ # something precise is being done with it.
544
+
545
+ # DEGRADED-MODE regexes. The anchored prefix above cannot be used on raw stdin:
546
+ # the text there is JSON, so an invocation reads `"command":"gh pr merge 1"` and
547
+ # the character before `gh` is a double quote, which is not a segment boundary in
548
+ # any shell. Matching the raw payload with GHPFX therefore matches nothing, which
549
+ # is a fail-OPEN in the one branch that exists to fail closed. Found by the
550
+ # absent-identity case in subagent-write-guard.test.sh, which the first draft of
551
+ # this file passed by allowing the merge. These accept any non-word character
552
+ # before `gh`, which also matches prose that quotes the command. That
553
+ # over-matching is the intended direction for a degraded environment.
554
+ GHRAW='(^|[^A-Za-z0-9_-])([^[:space:]";&|]*/)?gh[[:space:]]+'
555
+ RAW_MERGE="${GHRAW}pr[[:space:]]+merge([^A-Za-z0-9_-]|\$)"
556
+ RAW_REVIEW="${GHRAW}pr[[:space:]]+review([^A-Za-z0-9_-]|\$)"
557
+ RAW_ICLOSE="${GHRAW}issue[[:space:]]+close([^A-Za-z0-9_-]|\$)"
558
+ RAW_API="${GHRAW}api([^A-Za-z0-9_-]|\$)"
559
+
560
+ # SUBAGENT-BRANCH regexes. Same unanchored idea as the degraded ones, plus a run
561
+ # of closing punctuation after `gh`, so a command-substitution launcher is caught:
562
+ # in `$(which gh) pr merge 1` the character after `gh` is `)`, and without this
563
+ # class the `gh[[:space:]]+pr` requirement fails and the merge is allowed. Covers
564
+ # the backtick and quoted forms for the same reason. See MATCHING IS ASYMMETRIC.
565
+ GHSUB="(^|[^A-Za-z0-9_-])([^[:space:]\";&|]*/)?gh[)}\"'\`]*[[:space:]]+"
566
+ SUB_MERGE="${GHSUB}pr[[:space:]]+merge([^A-Za-z0-9_-]|\$)"
567
+ SUB_REVIEW="${GHSUB}pr[[:space:]]+review([^A-Za-z0-9_-]|\$)"
568
+ SUB_ICLOSE="${GHSUB}issue[[:space:]]+close([^A-Za-z0-9_-]|\$)"
569
+ SUB_API="${GHSUB}api([^A-Za-z0-9_-]|\$)"
570
+
571
+ # Everything after the matched subcommand, as argv-ish tokens. Quoted text inside
572
+ # sink commands is blanked upstream, so a `-h` inside a message body cannot reach
573
+ # here.
574
+ TAIL_RE='([^[:space:];&|]*/)?gh[[:space:]]+(pr[[:space:]]+(merge|review)|issue[[:space:]]+close|api)([[:space:]]+(.*))?$'
575
+
576
+ # `--help`/`-h` as the FIRST token after the subcommand prints usage and makes no
577
+ # API call. Only that position is allowed: a `-h` anywhere in the tail used to
578
+ # wave through `gh pr edit 4785 --body-file b.md -h` in the neighbouring guard
579
+ # (SF-4), and the same trick must not work here.
580
+ is_help_first() {
581
+ local tail="$1"
582
+ # shellcheck disable=SC2206 # deliberate word-splitting: this is argv, not a path
583
+ local toks=($tail)
584
+ [[ ${#toks[@]} -gt 0 ]] || return 1
585
+ [[ "${toks[0]}" == "--help" || "${toks[0]}" == "-h" ]]
586
+ }
587
+
588
+ # Effective HTTP method for `gh api`. See `gh api` METHOD INFERENCE in the header.
589
+ # Returns 0 when the call writes.
590
+ api_writes() {
591
+ local tail="$1" tok method="" want_method=0 saw_field=0
592
+ # shellcheck disable=SC2206 # deliberate word-splitting: this is argv, not a path
593
+ local toks=($tail)
594
+ for tok in ${toks[@]+"${toks[@]}"}; do
595
+ if [[ $want_method -eq 1 ]]; then
596
+ method="$tok"; want_method=0; continue
597
+ fi
598
+ case "$tok" in
599
+ -X|--method) want_method=1 ;;
600
+ --method=*) method="${tok#--method=}" ;;
601
+ -X?*) method="${tok#-X}" ;;
602
+ -f|-F|--field|--raw-field|--input) saw_field=1 ;;
603
+ -f*|-F*|--field=*|--raw-field=*|--input=*) saw_field=1 ;;
604
+ esac
605
+ done
606
+ if [[ -n "$method" ]]; then
607
+ # A named method decides, fields or not.
608
+ [[ "$(printf '%s' "$method" | tr '[:lower:]' '[:upper:]')" != "GET" ]]
609
+ return $?
610
+ fi
611
+ # No named method: gh sends POST when a field flag is present, GET otherwise.
612
+ [[ $saw_field -eq 1 ]]
613
+ }
614
+
615
+ if [[ $JQ_OK -eq 0 || $ENVELOPE_OK -eq 0 ]]; then
616
+ # DEGRADED SCAN. No parse, so no segment splitting and no tail: any raw text
617
+ # matching a refused family is refused. Over-matching is the intended direction
618
+ # here; see FAILURE DIRECTION in the header.
619
+ SCAN_RAW=" $INPUT"
620
+ if printf '%s' "$SCAN_RAW" | grep -qE "$RAW_MERGE"; then
621
+ refuse "gh pr merge" "indeterminate"
622
+ elif printf '%s' "$SCAN_RAW" | grep -qE "$RAW_REVIEW"; then
623
+ refuse "gh pr review" "indeterminate"
624
+ elif printf '%s' "$SCAN_RAW" | grep -qE "$RAW_ICLOSE"; then
625
+ refuse "gh issue close" "indeterminate"
626
+ elif printf '%s' "$SCAN_RAW" | grep -qE "$RAW_API"; then
627
+ # No tail parse is possible here, so every `gh api` form is refused,
628
+ # including a GET. A read the lead can repeat is the cheap side of this.
629
+ refuse "gh api" "indeterminate"
630
+ fi
631
+ exit 0
632
+ fi
633
+
634
+ # Subagent, parseable payload. Nothing to scan without a command — a Bash call
635
+ # with no command string cannot be one of the four.
636
+ if [[ -z "$COMMAND" ]]; then
637
+ exit 0
638
+ fi
639
+
640
+ SCAN=$(printf '%s' "$COMMAND" | fold_continuations | strip_heredocs)
641
+
642
+ while IFS= read -r seg; do
643
+ [[ -n "$seg" ]] || continue
644
+
645
+ GH_TAIL=""
646
+ TAIL_READ=0
647
+ if [[ "$seg" =~ $TAIL_RE ]]; then
648
+ GH_TAIL="${BASH_REMATCH[5]}"
649
+ TAIL_READ=1
650
+ fi
651
+
652
+ if printf '%s' "$seg" | grep -qE "$SUB_MERGE"; then
653
+ is_help_first "$GH_TAIL" || refuse "gh pr merge" "subagent"
654
+ elif printf '%s' "$seg" | grep -qE "$SUB_REVIEW"; then
655
+ is_help_first "$GH_TAIL" || refuse "gh pr review" "subagent"
656
+ elif printf '%s' "$seg" | grep -qE "$SUB_ICLOSE"; then
657
+ is_help_first "$GH_TAIL" || refuse "gh issue close" "subagent"
658
+ elif printf '%s' "$seg" | grep -qE "$SUB_API"; then
659
+ is_help_first "$GH_TAIL" && continue
660
+ # The method comes from the tail. GHSUB matches invocation forms that TAIL_RE
661
+ # cannot split, `$(which gh) api ...` among them, and an unread tail means the
662
+ # method is unknown. Unknown is not GET.
663
+ if [[ $TAIL_READ -eq 0 ]]; then
664
+ refuse "gh api, with a method that could not be read" "subagent"
665
+ fi
666
+ api_writes "$GH_TAIL" && refuse "gh api, with a method other than GET" "subagent"
667
+ fi
668
+ done < <(printf '%s\n' "$SCAN" | split_segments)
669
+
670
+ exit 0