@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
@@ -132,6 +132,48 @@ SESSION_ID=$(echo "$INPUT" | jq -r '.session_id // empty' 2>/dev/null) || SESSIO
132
132
  # second field. It is only ever COMPARED, never printed into the agent-visible
133
133
  # message, so no harness-supplied text reaches agent context through this hook.
134
134
  SESSION_ID="${SESSION_ID//[^a-zA-Z0-9_-]/}"
135
+ # Bound the length as well as the alphabet, mirroring
136
+ # memory-checkpoint-reminder.sh:304-319. This value is a filename component
137
+ # further down, NAME_MAX is 255 bytes, and the composite key added for #50 makes
138
+ # that name longer. A real session id is a 36-character UUID, so nothing the
139
+ # harness sends is truncated. The same bound is applied to the value read back
140
+ # from the state file, so a long stored id still compares equal to its own
141
+ # session rather than silently becoming a different one.
142
+ SESSION_ID="${SESSION_ID:0:64}"
143
+
144
+ # -- Agent attribution (#50) --------------------------------------------------
145
+ # A dispatched subagent runs under its DISPATCHER'S session id. That is verified
146
+ # in the fleet's own transcripts and recorded at memory-checkpoint-reminder.sh:79-92:
147
+ # a subagent's records live at
148
+ # <projectDir>/<sessionId>/subagents/agent-<agentId>.jsonl, and every line in the
149
+ # child file carries the parent's session id.
150
+ #
151
+ # So .session_id alone cannot tell a lead apart from the subagents it dispatched.
152
+ # Once the lead merged, every teammate in the worktree matched the stored id, was
153
+ # classified `self`, and took the demanding branch on every Bash call. The
154
+ # one-shot suppression below covers `other` only, so that demand repeated until
155
+ # the state cleared or 30 minutes passed. Four witnesses saw it on 2026-08-21
156
+ # (#50); a larger count on 2026-08-22 is unsourced.
157
+ #
158
+ # .agent_id, not .agent_type: the harness sets agent_id only inside a dispatched
159
+ # subagent, while agent_type is ALSO set on the main thread of an --agent
160
+ # session. Branching on agent_type would reclassify a real lead as a subagent.
161
+ # The two sources for that are quoted in full at memory-checkpoint-reminder.sh:38-68.
162
+ #
163
+ # A MISSING agent_id FIELD KEEPS TODAY'S BEHAVIOUR. An older harness that never
164
+ # sends the field, and a main thread on a harness that does, both parse to the
165
+ # empty string. Empty on both sides compares equal, so attribution falls back to
166
+ # the session comparison exactly as before this change. An empty agent id never
167
+ # routes to `unknown`: absence is the normal main-thread case, not a failure to
168
+ # attribute.
169
+ #
170
+ # Same load-bearing `|| AGENT_ID=""` as the two captures above: a broken jq must
171
+ # degrade, not return non-zero into this script's ERR trap. The value is only
172
+ # compared and used as a filename component, and it is never printed into the
173
+ # agent-visible message, so no harness-supplied text reaches agent context.
174
+ AGENT_ID=$(echo "$INPUT" | jq -r '.agent_id // empty' 2>/dev/null) || AGENT_ID=""
175
+ AGENT_ID="${AGENT_ID//[^a-zA-Z0-9_-]/}"
176
+ AGENT_ID="${AGENT_ID:0:64}"
135
177
 
136
178
  WORKTREE_NAME="${CLAUDE_WORKTREE_NAME:-default}"
137
179
  # Sanitize: alphanumeric, hyphens, underscores only — prevent path traversal
@@ -173,10 +215,213 @@ emit_telemetry() {
173
215
  >/dev/null 2>&1 || true
174
216
  }
175
217
 
218
+ # ── Command text scan (#50, Must Fix 2) ──────────────────────────────────────
219
+ # Reduce the raw command to the text the shell actually RUNS, then match against
220
+ # that. Both matchers below use it, so the two halves of one merge-verify cycle
221
+ # are judged by one rule.
222
+ #
223
+ # WHY A PARSER AND NOT AN ANCHOR. .tool_input.command holds the whole multi-line
224
+ # text of one Bash call, heredoc body included; the Bash tool_input has only
225
+ # command, description and timeout, so there is no structured field to read
226
+ # instead. Piping that string into grep re-anchors ^ at every line, so a heredoc
227
+ # body naming a command set the latch with no command run. An earlier fix
228
+ # narrowed the anchor instead, and the security review of 70fca59 measured what
229
+ # that missed: `bash <<EOF` with a merge in the body, `echo "$(gh pr merge 5)"`,
230
+ # and a merge on any line after a line containing a quote. All three run a merge
231
+ # and none latched. Deciding on the PRESENCE of a quote cannot separate those
232
+ # from prose; only deciding on what the quote ENCLOSES can.
233
+ #
234
+ # THE THREE FUNCTIONS BELOW ARE COPIED VERBATIM from subagent-write-guard.sh
235
+ # (fold_continuations :312-315, strip_heredocs :330-372, split_segments
236
+ # :382-482), where they are the tested construction for this exact question.
237
+ # Copied rather than shared: a shared hook library would put a single defect in
238
+ # reach of every gate at once, which is the standing argument recorded in
239
+ # hook-timing-parity.test.sh. Divergence across the copies is tracked in #49.
240
+ # Do not edit them here without checking that file.
241
+ #
242
+ # fold_continuations joins a backslash line continuation, so `gh pr \<nl> merge`
243
+ # is one line to the matchers.
244
+ # strip_heredocs drops an inert body (quoted delimiter), keeps a body the
245
+ # consumer executes (bash/sh/eval/source or a pipe into
246
+ # one), and keeps only the substituting lines of a body the
247
+ # shell expands (unquoted delimiter).
248
+ # split_segments emits one command per line, splitting on unquoted ; && ||
249
+ # and |, and blanks quoted text in segments whose leading
250
+ # command is a text sink (echo, printf, cat, tee, grep, jq,
251
+ # mx --content, gh --body, git commit -m). A segment holding
252
+ # $( or a backtick is never a sink, because the blanker
253
+ # cannot tell text from an executed substitution.
254
+ fold_continuations() {
255
+ sed -e :a -e '/\\$/N; s/\\\n[[:space:]]*/ /; ta' 2>/dev/null || true
256
+ }
257
+
258
+ strip_heredocs() {
259
+ awk '
260
+ !inbody {
261
+ line = $0
262
+ if (match(line, /<<[-]?[[:space:]]*["'"'"']?[A-Za-z_][A-Za-z0-9_]*["'"'"']?/)) {
263
+ d = substr(line, RSTART, RLENGTH)
264
+ # ORDER IS LOAD-BEARING. Read the quoting off the RAW match, BEFORE the
265
+ # two gsubs below strip the quotes out of d. Computed after them this test
266
+ # is always true, and the <<QUOTED bodies the shell leaves inert would be
267
+ # scanned as if they expanded.
268
+ unquoted = (d !~ /["'"'"']/)
269
+ gsub(/<<[-]?[[:space:]]*["'"'"']?/, "", d)
270
+ gsub(/["'"'"'].*/, "", d)
271
+ delim = d
272
+ inbody = 1
273
+ keepbody = 0
274
+ if (line ~ /(^|[^[:alnum:]_])(bash|sh|zsh|dash|ksh|eval|source)([^[:alnum:]_]|$)/) keepbody = 1
275
+ if (line ~ /(^|[[:space:]])\.[[:space:]]/) keepbody = 1
276
+ if (line ~ /\|[^|]*(bash|sh|zsh|dash|ksh|eval)([^[:alnum:]_]|$)/) keepbody = 1
277
+ # WHO EXECUTES THE BODY IS TWO QUESTIONS, NOT ONE. The three tests above
278
+ # ask whether the CONSUMER runs the body. They never asked whether the
279
+ # SHELL expands it first. With an unquoted delimiter bash performs command
280
+ # substitution on every body line before the consumer sees anything, so
281
+ # `cat <<EOF` holding `$(gh pr merge 1)` runs the merge and hands cat the
282
+ # output. Measured on f90382b: that form, the <<-EOF form, the backtick
283
+ # form, `cat > f <<EOF` and `tee f <<EOF` were all allowed, while the
284
+ # shell expanded every one of them. keepbody 2 keeps ONLY the lines that
285
+ # carry a substitution, so a heredoc of plain prose naming a refused
286
+ # command stays allowed and the invariant in ACCEPTED COST holds again.
287
+ if (keepbody == 0 && unquoted) keepbody = 2
288
+ }
289
+ print line
290
+ next
291
+ }
292
+ {
293
+ t = $0
294
+ gsub(/^[[:space:]]+/, "", t)
295
+ if (t == delim) { inbody = 0; keepbody = 0; next }
296
+ if (keepbody == 1) print $0
297
+ else if (keepbody == 2 && ($0 ~ /\$\(/ || $0 ~ /`/)) print $0
298
+ }
299
+ ' 2>/dev/null || true
300
+ }
301
+
302
+ split_segments() {
303
+ awk '
304
+ function get_lead_cmd(seg, s) {
305
+ s = seg; gsub(/^[[:space:]]+/, "", s)
306
+ while (match(s, /^[A-Za-z_][A-Za-z0-9_]*=[^[:space:]]*[[:space:]]+/))
307
+ s = substr(s, RSTART + RLENGTH)
308
+ while (match(s, /^[0-9]*[<>][>&]?[^[:space:]]*[[:space:]]+/))
309
+ s = substr(s, RSTART + RLENGTH)
310
+ if (match(s, /^[^[:space:]]+/)) return substr(s, RSTART, RLENGTH)
311
+ return ""
312
+ }
313
+ function check_sink(seg, cmd, s, sc) {
314
+ # A COMMAND SUBSTITUTION IS NEVER TEXT. This clause runs FIRST, before any
315
+ # carrier is recognised, and it is the whole reason blanking was unsafe: the
316
+ # blanker erases a quoted region, and `$( )` or a backtick inside that region
317
+ # is text to the blanker and an executed command to the shell. So
318
+ # `echo "$(gh pr merge 1)"` merged the pull request while this function
319
+ # classified it as prose. Measured on 6d6249c for echo, printf, cat, tee,
320
+ # git commit -m, gh --body and the backtick form: all seven allowed. The
321
+ # degraded no-jq branch does no blanking and caught every one of them, so the
322
+ # guard was WEAKER with jq than without. Returning 0 here means "not a sink",
323
+ # which leaves the quotes intact and lets the matcher see the invocation.
324
+ if (seg ~ /\$\(/ || seg ~ /`/) return 0
325
+ cmd = get_lead_cmd(seg)
326
+ if (cmd == "echo" || cmd == "printf" || cmd == "cat" || cmd == "tee" || cmd == "write")
327
+ return 1
328
+ # PATTERN CARRIERS. These take their argument as a pattern or a literal and
329
+ # never execute it. Without them a subagent cannot search for, or record, the
330
+ # name of a command this guard refuses.
331
+ if (cmd == "grep" || cmd == "rg" || cmd == "jq")
332
+ return 1
333
+ # THE MEMORY PATH. AGENTS.md makes MemNexus the sole memory store, so a
334
+ # memory-creator recording the lesson about this guard has to be able to name
335
+ # the command in --content. Measured on 6d6249c: it could not.
336
+ if ((cmd == "mx" || cmd == "mx-agent") && (seg ~ /--content/ || seg ~ /--body/ || seg ~ /--query/))
337
+ return 1
338
+ if (cmd == "gh" && (seg ~ /--body/ || seg ~ /--title/ || seg ~ /--comment/))
339
+ return 1
340
+ if (cmd == "git") {
341
+ s = seg; gsub(/^[[:space:]]+/, "", s); sub(/^git[[:space:]]+/, "", s)
342
+ while (match(s, /^(-C[[:space:]]+[^[:space:]]+|-c[[:space:]]+[^[:space:]]+|--[a-z][-a-z0-9]*(=[^[:space:]]*)?)[[:space:]]+/))
343
+ s = substr(s, RSTART + RLENGTH)
344
+ if (match(s, /^[^[:space:]]+/)) {
345
+ sc = substr(s, RSTART, RLENGTH)
346
+ if ((sc == "commit" || sc == "tag" || sc == "notes") && \
347
+ (seg ~ /-[a-zA-Z]*m[[:space:]]/ || seg ~ /--message/))
348
+ return 1
349
+ }
350
+ }
351
+ return 0
352
+ }
353
+ function blank_quotes(seg, n, i, c, out, isq, idq) {
354
+ n = length(seg); out = ""; isq = 0; idq = 0
355
+ for (i = 1; i <= n; i++) {
356
+ c = substr(seg, i, 1)
357
+ if (isq) {
358
+ if (c == "'"'"'") { isq = 0; out = out c } else out = out " "
359
+ continue
360
+ }
361
+ if (idq) {
362
+ if (c == "\\") { out = out " "; i++; continue }
363
+ if (c == "\"") { idq = 0; out = out c } else out = out " "
364
+ continue
365
+ }
366
+ if (c == "'"'"'") { isq = 1; out = out c; continue }
367
+ if (c == "\"") { idq = 1; out = out c; continue }
368
+ out = out c
369
+ }
370
+ return out
371
+ }
372
+ {
373
+ line = $0; n = length(line)
374
+ isq = 0; idq = 0; num = 0; seg_s = 1
375
+ for (i = 1; i <= n; i++) {
376
+ c = substr(line, i, 1)
377
+ if (isq) { if (c == "'"'"'") isq = 0; continue }
378
+ if (idq) { if (c == "\\") { i++; continue }; if (c == "\"") idq = 0; continue }
379
+ if (c == "'"'"'") { isq = 1; continue }
380
+ if (c == "\"") { idq = 1; continue }
381
+ sp = 0; sl = 1
382
+ if (c == ";") sp = 1
383
+ else if (c == "&" && i < n && substr(line, i+1, 1) == "&") { sp = 1; sl = 2 }
384
+ else if (c == "|" && i < n && substr(line, i+1, 1) == "|") { sp = 1; sl = 2 }
385
+ else if (c == "|") sp = 1
386
+ if (sp) {
387
+ num++; segs[num] = substr(line, seg_s, i - seg_s)
388
+ seg_s = i + sl; if (sl == 2) i++
389
+ }
390
+ }
391
+ num++; segs[num] = substr(line, seg_s)
392
+ for (s = 1; s <= num; s++) {
393
+ if (check_sink(segs[s])) print blank_quotes(segs[s])
394
+ else print segs[s]
395
+ }
396
+ }
397
+ ' 2>/dev/null || true
398
+ }
399
+
400
+ COMMAND_SCAN=$(printf '%s' "$COMMAND" | fold_continuations | strip_heredocs | split_segments)
401
+ # FAIL TOWARDS OVER-DETECTING, never towards silence. A missing or broken awk or
402
+ # sed empties the scan, and an empty scan matches nothing, which would leave the
403
+ # gate off for the session with no error. Degrade to the raw command instead:
404
+ # that is the pre-#50 behaviour, which over-detects. `:112` states the direction.
405
+ if [[ -z "$COMMAND_SCAN" && -n "$COMMAND" ]]; then
406
+ COMMAND_SCAN="$COMMAND"
407
+ fi
408
+
409
+ # One anchor for all three matchers. A match must open the segment, or follow a
410
+ # top-level separator, or open a command substitution. The substitution openers
411
+ # are load-bearing: `echo "$(gh pr merge 5)"` runs the merge, and split_segments
412
+ # deliberately leaves that segment unblanked so this anchor can see it.
413
+ DVR_SEP='(^|[;&|]|\$\(|`)[[:space:]]*'
414
+ DVR_VERIFY_RE="${DVR_SEP}gh[[:space:]]+run[[:space:]]+(list|view|watch)([[:space:]]|\$)"
415
+ DVR_MERGE_RE="${DVR_SEP}gh[[:space:]]+pr[[:space:]]+merge([[:space:]]|\$)"
416
+ DVR_CREATE_RE="${DVR_SEP}gh[[:space:]]+pr[[:space:]]+create([[:space:]]|\$)"
417
+
176
418
  # ── 1. Detect verification signal (clears pending state) ─────────────────────
177
- # Anchored match: only clear on actual gh run list/view/watch commands, not substrings
178
- # in echo/comments/unrelated commands. Must be at command start or after && ; |.
179
- if printf '%s\n' "$COMMAND" | grep -qE '(^|[;&|]\s*)gh\s+run\s+(list|view|watch)\b'; then
419
+ # Only clear on a gh run list/view/watch the shell RAN, never on the text of one.
420
+ # Converted to the shared scan by Must Fix 2: this matcher kept the old per-line
421
+ # shape while the merge matcher lost it, so naming the command in a heredoc or a
422
+ # multi-line string cleared a real latch. The security reviewer cleared a live
423
+ # latch that way during the review of 70fca59.
424
+ if printf '%s\n' "$COMMAND_SCAN" | grep -qE "$DVR_VERIFY_RE"; then
180
425
  # Gate to one-shot per cycle: emit only if verify sentinel doesn't exist yet.
181
426
  if [ ! -f "$VERIFY_SENT" ]; then
182
427
  emit_telemetry "false" "true" "false"
@@ -192,18 +437,25 @@ if printf '%s\n' "$COMMAND" | grep -qE '(^|[;&|]\s*)gh\s+run\s+(list|view|watch)
192
437
  fi
193
438
 
194
439
  # ── 2. Detect merge signal (sets pending state) ───────────────────────────────
440
+ #
441
+ # Both matchers read $COMMAND_SCAN, so a merge inside an inert heredoc or a text
442
+ # sink's quoted argument is not seen, and a merge the shell runs is.
195
443
  IS_MERGE=0
196
- if printf '%s\n' "$COMMAND" | grep -qE '(^|[;&|]\s*)gh\s+pr\s+merge\b'; then
444
+ if printf '%s\n' "$COMMAND_SCAN" | grep -qE "$DVR_MERGE_RE"; then
197
445
  IS_MERGE=1
198
446
  fi
199
- if printf '%s\n' "$COMMAND" | grep -qE '(^|[;&|]\s*)gh\s+pr\s+create\b' && printf '%s\n' "$COMMAND" | grep -qE '\s--merge\b'; then
447
+ # --merge is an argument, not a command, so it has no segment to open. The
448
+ # command word it belongs to is what the anchored matcher constrains.
449
+ if printf '%s\n' "$COMMAND_SCAN" | grep -qE "$DVR_CREATE_RE" && printf '%s\n' "$COMMAND_SCAN" | grep -qE '\s--merge\b'; then
200
450
  IS_MERGE=1
201
451
  fi
202
452
 
203
453
  if [[ "$IS_MERGE" -eq 1 ]]; then
204
454
  # Extract PR number if possible
205
455
  PR_NUMBER=""
206
- if [[ "$COMMAND" =~ gh[[:space:]]+pr[[:space:]]+merge[[:space:]]+([0-9]+) ]]; then
456
+ # Read from the scan, not the raw command: a number inside quoted prose must
457
+ # not become the PR the reminder names.
458
+ if [[ "$COMMAND_SCAN" =~ gh[[:space:]]+pr[[:space:]]+merge[[:space:]]+([0-9]+) ]]; then
207
459
  PR_NUMBER="${BASH_REMATCH[1]}"
208
460
  fi
209
461
  TIMESTAMP=$(date +%s)
@@ -217,7 +469,12 @@ if [[ "$IS_MERGE" -eq 1 ]]; then
217
469
  # session apart from the others sharing this worktree-keyed state file. An empty
218
470
  # value (no session_id on stdin, or jq unavailable) is written as an empty field
219
471
  # and read back as "unknown" — the same branch legacy state files take.
220
- printf 'PR_NUMBER=%s\nTIMESTAMP=%s\nSESSION_ID=%s\n' "$PR_NUMBER" "$TIMESTAMP" "$SESSION_ID" > "$STATE_FILE" 2>/dev/null || true
472
+ #
473
+ # AGENT_ID records WHICH AGENT INSIDE THAT SESSION merged (#50). It is empty for
474
+ # a main thread and set inside a dispatched subagent. A state file written before
475
+ # this change carries no AGENT_ID line, so it reads back empty, which is the
476
+ # value a main-thread merge writes today.
477
+ printf 'PR_NUMBER=%s\nTIMESTAMP=%s\nSESSION_ID=%s\nAGENT_ID=%s\n' "$PR_NUMBER" "$TIMESTAMP" "$SESSION_ID" "$AGENT_ID" > "$STATE_FILE" 2>/dev/null || true
221
478
  # A new merge starts a new cycle — clear verify sentinel so the next verify re-arms.
222
479
  # Per-session `other` notices clear too (#4734), so sessions already notified
223
480
  # about the PREVIOUS merge are notified once about this new one.
@@ -277,24 +534,96 @@ PR_REF="${STORED_PR:-<PR>}"
277
534
  # misattribute the merge, which is the bug this block exists to prevent.
278
535
  # Re-sanitized on read: the file lives in world-writable /tmp, so its contents are
279
536
  # not trusted to be well-formed even though this hook wrote them.
280
- STORED_SESSION=""
537
+ # A FIELD THE FILTER CHANGES MAKES THE WHOLE FILE UNKNOWN (Must Fix 5, security
538
+ # review of 70fca59). Deleting the disallowed characters leaves a DIFFERENT
539
+ # string that is then compared as though it were genuine, so a planted value
540
+ # collides with a real id. Measured by the reviewer: `AGENT_ID=a!b` read by an
541
+ # agent whose real id is `ab` produced `self` and the repeating demand, for an
542
+ # agent that merged nothing. That is the #50 failure reached through the state
543
+ # file instead of the command text, and /tmp is mode 1777, so any process on this
544
+ # machine can write it.
545
+ #
546
+ # Three states, not two. Absent or empty is a real identity, the main thread or a
547
+ # pre-change file, and keeps the existing comparison. Present and unchanged by
548
+ # the filter is genuine. Present and changed means this hook did not write it, so
549
+ # the record is malformed and nothing may be asserted about it.
550
+ #
551
+ # `unknown` rather than "ignore the field": it prints and exits 2, so nothing is
552
+ # silenced, and its wording claims nothing about who merged. Emptying the field
553
+ # instead would convert a corrupt value into the positive claim "the main thread
554
+ # merged", which is the class of error #4734 exists to prevent.
555
+ #
556
+ # `grep -m1` because two `AGENT_ID=` lines otherwise concatenate: the strip
557
+ # removes the newline between them and the result is a third string that is
558
+ # neither. The length test is separate from the 64-character write bound, so a
559
+ # genuinely over-long id is rejected rather than truncated into a possible match.
560
+ STATE_MALFORMED=0
561
+
562
+ STORED_SESSION_RAW=""
281
563
  if [[ -f "$STATE_FILE" ]]; then
282
- STORED_SESSION=$(grep '^SESSION_ID=' "$STATE_FILE" 2>/dev/null | cut -d= -f2- || true)
564
+ STORED_SESSION_RAW=$(grep -m1 '^SESSION_ID=' "$STATE_FILE" 2>/dev/null | cut -d= -f2- || true)
565
+ fi
566
+ STORED_SESSION="${STORED_SESSION_RAW//[^a-zA-Z0-9_-]/}"
567
+ if [[ "$STORED_SESSION" != "$STORED_SESSION_RAW" || "${#STORED_SESSION_RAW}" -gt 64 ]]; then
568
+ STATE_MALFORMED=1
569
+ fi
570
+
571
+ # Read back the agent that recorded the merge (#50). Same parse, same
572
+ # sanitization and the same bound as the session field above, for the same
573
+ # reason: the file lives in world-writable /tmp, so its contents are not trusted
574
+ # to be well-formed. Every state file written before this change lacks the line,
575
+ # which reads back as the empty string and compares equal to a main thread's own
576
+ # empty agent id.
577
+ STORED_AGENT_RAW=""
578
+ if [[ -f "$STATE_FILE" ]]; then
579
+ STORED_AGENT_RAW=$(grep -m1 '^AGENT_ID=' "$STATE_FILE" 2>/dev/null | cut -d= -f2- || true)
580
+ fi
581
+ STORED_AGENT="${STORED_AGENT_RAW//[^a-zA-Z0-9_-]/}"
582
+ if [[ "$STORED_AGENT" != "$STORED_AGENT_RAW" || "${#STORED_AGENT_RAW}" -gt 64 ]]; then
583
+ STATE_MALFORMED=1
283
584
  fi
284
- STORED_SESSION="${STORED_SESSION//[^a-zA-Z0-9_-]/}"
285
585
 
286
586
  # Three-way attribution. Every branch still emits a message and still exits 2 —
287
587
  # failing to attribute must never SWALLOW the reminder, only soften its claim.
288
- # self → this session merged it; the original directive wording applies.
289
- # other → a different concurrent session merged it; non-actionable notice.
588
+ # self → this agent merged it, OR this is the main thread of the session
589
+ # that merged it; the original directive wording applies.
590
+ # other → a different session, or a different SUBAGENT inside this session,
591
+ # merged it; non-actionable notice.
290
592
  # unknown → no stored id (state file written by the pre-#4734 hook, or the
291
593
  # harness supplied no session_id, or jq was unavailable on either
292
- # side). Assert neither; say so.
293
- if [[ -z "$STORED_SESSION" || -z "$SESSION_ID" ]]; then
594
+ # side), or a stored id this hook did not write. Assert neither.
595
+ if [[ "$STATE_MALFORMED" -eq 1 ]]; then
596
+ # A field this hook did not write. Assert nothing about who merged.
597
+ ATTRIBUTION="unknown"
598
+ elif [[ -z "$STORED_SESSION" || -z "$SESSION_ID" ]]; then
294
599
  ATTRIBUTION="unknown"
295
- elif [[ "$STORED_SESSION" == "$SESSION_ID" ]]; then
600
+ elif [[ "$STORED_SESSION" != "$SESSION_ID" ]]; then
601
+ # A different session. This is the #4734 case and it is decided FIRST, before
602
+ # any agent-id test, so no agent-id branch below can reach across sessions.
603
+ ATTRIBUTION="other"
604
+ elif [[ "$STORED_AGENT" == "$AGENT_ID" ]]; then
605
+ # Same session, same agent. A subagent shares its dispatcher's session id and
606
+ # carries a different agent id, so it does not reach this branch (#50).
607
+ ATTRIBUTION="self"
608
+ elif [[ -z "$AGENT_ID" ]]; then
609
+ # THE MAIN THREAD OF THE MERGING SESSION IS NEVER SILENCED (Must Fix 1,
610
+ # security review of 70fca59). A dispatched subagent can run the merge, and
611
+ # AGENTS.md makes the session that dispatched it responsible for verifying the
612
+ # deploy. Keying on the pair alone told that session once that the merge was
613
+ # somebody else's job and then went quiet, which turns the gate off for the
614
+ # only party that can clear it. An empty agent id is a positive reading, not a
615
+ # failure: the harness omits the field only on a main thread.
616
+ #
617
+ # SCOPED TO THE MERGING SESSION ON PURPOSE. The review's remedy reads
618
+ # `elif [[ -z "$AGENT_ID" ]]` placed after the both-match test. Applied to the
619
+ # chain as it stood, that also catches a main thread in a DIFFERENT session,
620
+ # which is #4734 exactly. Measured before this edit: with SESSION_ID=sess-A
621
+ # stored and sess-B reading, the literal form printed "Deploy verification
622
+ # required" where the committed code printed the notice. The `!=` branch above
623
+ # is what confines this one to the session that merged.
296
624
  ATTRIBUTION="self"
297
625
  else
626
+ # Same session, and both agents are named subagents that differ.
298
627
  ATTRIBUTION="other"
299
628
  fi
300
629
 
@@ -308,16 +637,20 @@ fi
308
637
  # non-actionable notice repeated dozens of times would itself get escalated as an
309
638
  # anomaly — a softer version of the failure this hook is being fixed for.
310
639
  #
311
- # Keyed by session so each concurrent session is told once. SESSION_ID is already
312
- # constrained to [a-zA-Z0-9_-] at parse time, so it is safe in a filename; on this
313
- # branch it is always non-empty (empty would have routed to `unknown`), and the
640
+ # Keyed by session AND agent (#50), so two subagents dispatched by one session are
641
+ # each told once instead of sharing one sentinel and silencing the second. Both
642
+ # components are constrained to [a-zA-Z0-9_-] and bounded to 64 characters at parse
643
+ # time, so the name is safe in a filename and stays well inside NAME_MAX. The agent
644
+ # component is appended only when it is non-empty, so a main thread's sentinel name
645
+ # is byte-identical to the one this hook wrote before the change. SESSION_ID on this
646
+ # branch is always non-empty (empty would have routed to `unknown`), and the
314
647
  # `:-unknown` default is belt-and-braces.
315
648
  #
316
649
  # Suppressed repeats exit 0 with NO output, not exit 2. A non-zero exit with an
317
650
  # empty stderr is what produced the "No stderr output" reports in
318
651
  # KI-HOOKS-STDERR-SYSTEMIC; emitting nothing while claiming a warning is the bug,
319
652
  # not the fix. Every branch that actually PRINTS still exits 2.
320
- NOTICE_SENT="${STATE_FILE}.notice-${SESSION_ID:-unknown}"
653
+ NOTICE_SENT="${STATE_FILE}.notice-${SESSION_ID:-unknown}${AGENT_ID:+-$AGENT_ID}"
321
654
  if [[ "$ATTRIBUTION" == "other" ]]; then
322
655
  if [[ -f "$NOTICE_SENT" ]]; then
323
656
  exit 0
@@ -360,9 +693,13 @@ CLI_PACKAGE="${CLI_PACKAGE:-@memnexus-ai/mx-agent-cli}"
360
693
  # to "verify the deploy and retry your command" would be a false directive —
361
694
  # exactly the misdirection that got read as prompt injection. No numbered
362
695
  # steps here; steps are what made the previous message look actionable.
363
- echo "ℹ️ Notice: PR ${PR_REF} was merged by a different agent session running concurrently in this worktree."
364
- echo "No action is required from you. Verifying that deploy is the responsibility of the session that ran the merge."
365
- echo "You are seeing this because deploy-pending state is shared per worktree, not per session."
696
+ # "a different agent", not "a different agent session" (#50). A dispatched
697
+ # subagent shares its dispatcher's session id, so this branch is now reached
698
+ # by two agents inside ONE session as well as by two sessions. Naming a
699
+ # session here would state something the hook has not established.
700
+ echo "ℹ️ Notice: PR ${PR_REF} was merged by a different agent running concurrently in this worktree."
701
+ echo "No action is required from you. Verifying that deploy is the responsibility of the agent that ran the merge."
702
+ echo "You are seeing this because deploy-pending state is shared per worktree, not per agent."
366
703
  elif [[ "$ATTRIBUTION" == "unknown" ]]; then
367
704
  # Provenance genuinely undetermined — state the uncertainty instead of
368
705
  # asserting either ownership. Steps are offered CONDITIONALLY.
@@ -41,16 +41,48 @@
41
41
  # the log_override() header for the failure modes this had to be hardened
42
42
  # against (a FIFO that blocked the append forever, a symlink that redirected it)
43
43
  # and for the residual TOCTOU that is knowingly accepted.
44
- # LAYERING CAVEAT: GIT_EXPERT=1 only clears THIS hook's blocks (bare-SHA/branch
45
- # reset forms, stash, checkout-file, restore-worktree). It does NOT clear the two
46
- # patterns `git reset --hard HEAD~*` and `git reset --hard HEAD^*` — those are
44
+ # LAYERING CAVEAT (corrected 2026-08-25, issue #144): GIT_EXPERT=1 clears THIS
45
+ # hook's blocks (bare-SHA/branch reset forms, stash, checkout-file,
46
+ # restore-worktree). The ONLY patterns it cannot clear are the two
47
+ # `git reset --hard HEAD~*` and `git reset --hard HEAD^*` forms, which stay
47
48
  # hard-denied at the Claude Code permission layer (.claude/settings.json deny
48
- # list), which fires BEFORE this hook runs. If an override appears to "fail" on a
49
- # HEAD~/HEAD^ reset, debug the settings.json deny list, not this script.
49
+ # list). That layer decides BEFORE this hook runs, so the override never reaches
50
+ # it. If an override appears to "fail" on a HEAD~/HEAD^ reset, debug the
51
+ # settings.json deny list, not this script.
52
+ # Checkout-file and restore-worktree are HOOK-ONLY as of 2026-08-25. Their two
53
+ # settings entries, `Bash(git checkout -- *)` and `Bash(git restore --worktree*)`,
54
+ # were removed because they denied `GIT_EXPERT=1 git checkout -- <path>` before
55
+ # this hook saw it, so the override this script prints in its own block message
56
+ # could not work. A git-expert spent a full task discovering that dead end (#144,
57
+ # reported by the surface team 2026-08-24), and the same denial closed every exit
58
+ # from the post-squash-merge sync cycle (#156). This guard is now the sole
59
+ # enforcer for those two verbs, which is what its own header already described:
60
+ # instrumented friction with an audit line per override, not a lock.
50
61
  #
51
62
  # This guard ONLY inspects git mutation patterns; all non-git and read-only git
52
63
  # commands (status, log, diff, stash list, stash show, restore --staged) pass.
53
64
  #
65
+ # SANCTIONED IN-PROCESS EXCEPTION (issue #172, product owner approval given in
66
+ # the bootstrap lead's session 2026-08-25): `mx-agent worktree sync` runs git
67
+ # from Node via execFile/spawn with argv arrays, so no Bash tool call carries
68
+ # the mutation and this hook never sees it. That is a reviewed, deliberate
69
+ # carve-out with its own controls (leader-lock gate, checkpoint to
70
+ # refs/mx/checkpoint/<worktree>/<epoch-ms>-<rand6> before any destructive
71
+ # step, quiescence check, and an audit record in
72
+ # ~/.mx-agent/logs/worktree-sync.jsonl on every path). WORDING CORRECTED
73
+ # 2026-08-25 (Should Fix 1, security review of PR #182, comment 5415821643):
74
+ # this said "one audit line per run", which no longer describes the command. A
75
+ # run that reaches the destructive step writes TWO records, an `in-progress`
76
+ # record before that step and a terminal record after it, correlated by
77
+ # `runId`. The invariant is one TERMINAL record per run, and an `in-progress`
78
+ # record with no matching terminal record is the signal that a run was killed
79
+ # mid-step. It is NOT evidence that any hook can
80
+ # be routed around by moving an operation into a Node child process: the same
81
+ # technique applied to workflow-dispatch-guard.sh or gh-pr-edit-guard.sh
82
+ # without an equivalent review is a bypass, and AGENTS.md's rule stands that a
83
+ # blocked operation routes through the git-expert teammate, not around the
84
+ # guard. Plan and review record: issue #172, revisions 1 to 4.
85
+ #
54
86
  # SCOPE (SF-2, QA probe audit 2026-07-06 — this is a guardrail, not a security
55
87
  # boundary). INERT-CONSUMER HEREDOC BODIES ARE EXCLUDED FROM MATCHING: a git verb
56
88
  # inside a heredoc whose consuming command does NOT execute the body (e.g. a
@@ -417,6 +449,33 @@ function get_lead_cmd(seg, s) {
417
449
  if (match(s, /^[^[:space:]]+/)) return substr(s, RSTART, RLENGTH)
418
450
  return ""
419
451
  }
452
+ function peel_keywords(seg, s, i) {
453
+ # #225 MF-2 route (a): strip leading shell keywords so the segment starts at
454
+ # the command the keyword introduces. The segmenter splits only on `;` `&`
455
+ # `&&` `|` `||`, so `if git reset --hard; then` arrived as ONE segment
456
+ # beginning `if `. GITSEG matched nothing there (its boundary class named
457
+ # then/do/else and not if/elif/while/until) and OVSEG matched nothing either,
458
+ # because `if` is neither an assignment nor a launcher word. The verb was
459
+ # invisible AND the override was unusable in the same position. Peeling puts
460
+ # the verb at ^ where both patterns already work, which is the repair lines
461
+ # 241-243 name: widen the SEGMENTER, never widen OVSEG toward GITSEG.
462
+ # BOUNDED AT 8 ITERATIONS ON PURPOSE. Each pass is O(segment length), so the
463
+ # bound keeps this linear; an unbounded `while` with a per-pass substr is
464
+ # quadratic on an adversarial `if if if ...` prefix, and a hook killed by its
465
+ # 60s timeout loses its exit 2 (header, lines 155-161). Stacking more than 8
466
+ # keywords does not evade: the GITSEG alternative (^|\s)(if|elif|while|until|
467
+ # then|do|else)\s still matches the 9th, which is why both changes ship.
468
+ # NO APOSTROPHE MAY APPEAR IN THIS COMMENT. It sits inside the single-quoted
469
+ # awk program, so one apostrophe closes the quote and breaks the whole hook.
470
+ # `!` is deliberately NOT peeled — `! V`, `if ! V` and `while ! V` stay open
471
+ # and are recorded as such (#228).
472
+ s = seg
473
+ for (i = 0; i < 8; i++) {
474
+ if (!match(s, /^[[:space:]]*(if|elif|while|until|then|do|else)[[:space:]]+/)) break
475
+ s = substr(s, RSTART + RLENGTH)
476
+ }
477
+ return s
478
+ }
420
479
  function is_exec(cmd) {
421
480
  return (cmd == "bash" || cmd == "sh" || cmd == "zsh" || cmd == "dash" || \
422
481
  cmd == "ksh" || cmd == "eval" || cmd == "source" || cmd == "ssh" || \
@@ -517,27 +576,32 @@ function extract_quotes(seg, n, i, c, ct, isq, idq, res) {
517
576
  }
518
577
  num++; segs[num] = substr(line, seg_s); stypes[num] = ""
519
578
  # classify each segment
520
- for (s = 1; s <= num; s++) { snk[s] = check_sink(segs[s]); pxe[s] = 0 }
579
+ # psegs[] is segs[] with leading shell keywords peeled (#225 MF-2). Every
580
+ # consumer below reads psegs, so classification, executor detection, the
581
+ # override stamp and the emitted text all see the same string. Peeling for
582
+ # the emit alone would leave check_sink deciding on `if` as the lead command,
583
+ # which is the kind of half-applied transform that becomes its own defect.
584
+ for (s = 1; s <= num; s++) { psegs[s] = peel_keywords(segs[s]); snk[s] = check_sink(psegs[s]); pxe[s] = 0 }
521
585
  # backward-propagate executor status through pipes
522
586
  for (s = num; s >= 2; s--) {
523
587
  if (stypes[s-1] == "pipe") {
524
- cmd = get_lead_cmd(segs[s])
588
+ cmd = get_lead_cmd(psegs[s])
525
589
  if (is_exec(cmd) || pxe[s]) { snk[s-1] = 0; pxe[s-1] = 1 }
526
590
  }
527
591
  }
528
592
  # emit processed segments
529
593
  for (s = 1; s <= num; s++) {
530
- if (snk[s]) { print blank_quotes(segs[s]) }
594
+ if (snk[s]) { print blank_quotes(psegs[s]) }
531
595
  else {
532
- print segs[s]
533
- cmd = get_lead_cmd(segs[s])
596
+ print psegs[s]
597
+ cmd = get_lead_cmd(psegs[s])
534
598
  if (is_exec(cmd) || pxe[s]) {
535
- ex = extract_quotes(segs[s])
599
+ ex = extract_quotes(psegs[s])
536
600
  if (ex != "") {
537
601
  # An executor payload is executed BY this segment, so an
538
602
  # anchored override on the segment applies to it (#4804).
539
603
  # Stamp each extracted line, which is its own scan segment.
540
- if (segs[s] ~ ovre) {
604
+ if (psegs[s] ~ ovre) {
541
605
  nx = split(ex, xa, "\n")
542
606
  for (k = 1; k <= nx; k++) print "GIT_EXPERT=1 " xa[k]
543
607
  } else print ex
@@ -600,6 +664,14 @@ refuse() {
600
664
  echo "If you truly must run this, prefix it with GIT_EXPERT=1 to override,"
601
665
  echo "e.g. GIT_EXPERT=1 $what ..."
602
666
  echo ""
667
+ echo "In a compound command, put the token on the command itself, NOT on the"
668
+ echo "keyword (#225). Bash rejects a token before if/while/until with a"
669
+ echo "syntax error, so these are the forms that work:"
670
+ echo " if GIT_EXPERT=1 $what ...; then ...; fi"
671
+ echo " while GIT_EXPERT=1 $what ...; do ...; done"
672
+ echo " if true; then GIT_EXPERT=1 $what ...; fi"
673
+ echo " GIT_EXPERT=1 bash -c '<the whole compound command>'"
674
+ echo ""
603
675
  echo "IF YOU OVERRIDE, YOU MUST REPORT IT. Your report back to whoever asked"
604
676
  echo "for this work has to state: the command that was blocked, that"
605
677
  echo "git-mutation-guard blocked it, that you used GIT_EXPERT=1 to override,"
@@ -643,7 +715,7 @@ refuse() {
643
715
  # mx-guard-hook.sh. Applied per-segment to the heredoc- and quote-stripped SCAN.
644
716
  # GIT_EXPERT=1 clears any accepted false positive. `echo` is deliberately NOT a
645
717
  # wrapper word, so `echo "use timeout 5 git reset --hard"` stays allowed.
646
- GITSEG='(^|[;&|({`]|\s(then|do|else)\s)\s*([0-9]*[<>][^[:space:]]*\s+|[A-Za-z_][A-Za-z0-9_]*=[^[:space:];&|]*\s+|(sudo|env|nice|nohup|time|timeout|ionice|stdbuf|command|xargs|setsid|doas|nsenter)\s+([0-9]+\s+)?)*git(\s+(-C\s+[^[:space:];&|]+|-c\s+[^[:space:];&|]+|--(git-dir|work-tree|namespace|super-prefix)(=[^[:space:];&|]*|\s+[^[:space:];&|]+)|--[A-Za-z][A-Za-z0-9-]*=[^[:space:];&|]*|--[A-Za-z][A-Za-z0-9-]*|-[A-Za-z]))*\s+'
718
+ GITSEG='(^|[;&|({`]|(^|\s)(then|do|else|if|elif|while|until)\s)\s*([0-9]*[<>][^[:space:]]*\s+|[A-Za-z_][A-Za-z0-9_]*=[^[:space:];&|]*\s+|(sudo|env|nice|nohup|time|timeout|ionice|stdbuf|command|xargs|setsid|doas|nsenter)\s+([0-9]+\s+)?)*git(\s+(-C\s+[^[:space:];&|]+|-c\s+[^[:space:];&|]+|--(git-dir|work-tree|namespace|super-prefix)(=[^[:space:];&|]*|\s+[^[:space:];&|]+)|--[A-Za-z][A-Za-z0-9-]*=[^[:space:];&|]*|--[A-Za-z][A-Za-z0-9-]*|-[A-Za-z]))*\s+'
647
719
 
648
720
  # --- per-segment evaluation (item 3, #3934; sink scoping, #3938) --------------
649
721
  # Each allow-check (stash list|show; restore --staged-without-worktree) must be