@webpieces/ai-hook-rules 0.4.838 → 0.4.840

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 (390) hide show
  1. package/README.md +12 -140
  2. package/package.json +7 -15
  3. package/src/adapters/hook-app-fixtures.d.ts +1 -1
  4. package/src/adapters/hook-app-fixtures.js +0 -8
  5. package/src/adapters/hook-app-fixtures.js.map +1 -1
  6. package/src/adapters/hook-core.d.ts +1 -21
  7. package/src/adapters/hook-core.js +22 -341
  8. package/src/adapters/hook-core.js.map +1 -1
  9. package/src/core/load-rules.d.ts +4 -39
  10. package/src/core/load-rules.js +15 -127
  11. package/src/core/load-rules.js.map +1 -1
  12. package/src/core/rules/catch-error-pattern.d.ts +3 -3
  13. package/src/core/rules/catch-error-pattern.js +13 -13
  14. package/src/core/rules/catch-error-pattern.js.map +1 -1
  15. package/src/core/rules/index.d.ts +1 -14
  16. package/src/core/rules/index.js +1 -17
  17. package/src/core/rules/index.js.map +1 -1
  18. package/src/core/rules/match-rule.d.ts +3 -3
  19. package/src/core/rules/match-rule.js +6 -6
  20. package/src/core/rules/match-rule.js.map +1 -1
  21. package/src/core/rules/max-file-lines.d.ts +3 -3
  22. package/src/core/rules/max-file-lines.js +7 -7
  23. package/src/core/rules/max-file-lines.js.map +1 -1
  24. package/src/core/rules/no-any-unknown.d.ts +3 -3
  25. package/src/core/rules/no-any-unknown.js +6 -6
  26. package/src/core/rules/no-any-unknown.js.map +1 -1
  27. package/src/core/rules/no-custom-css.d.ts +3 -3
  28. package/src/core/rules/no-custom-css.js +7 -7
  29. package/src/core/rules/no-custom-css.js.map +1 -1
  30. package/src/core/rules/no-destructure.d.ts +3 -3
  31. package/src/core/rules/no-destructure.js +7 -7
  32. package/src/core/rules/no-destructure.js.map +1 -1
  33. package/src/core/rules/no-implicit-any.d.ts +3 -3
  34. package/src/core/rules/no-implicit-any.js +6 -6
  35. package/src/core/rules/no-implicit-any.js.map +1 -1
  36. package/src/core/rules/no-js-files.d.ts +3 -3
  37. package/src/core/rules/no-js-files.js +6 -6
  38. package/src/core/rules/no-js-files.js.map +1 -1
  39. package/src/core/rules/no-process-exit-outside-main.d.ts +3 -3
  40. package/src/core/rules/no-process-exit-outside-main.js +7 -7
  41. package/src/core/rules/no-process-exit-outside-main.js.map +1 -1
  42. package/src/core/rules/no-symbol-di-tokens.d.ts +3 -3
  43. package/src/core/rules/no-symbol-di-tokens.js +7 -7
  44. package/src/core/rules/no-symbol-di-tokens.js.map +1 -1
  45. package/src/core/rules/no-unmanaged-exceptions.d.ts +3 -3
  46. package/src/core/rules/no-unmanaged-exceptions.js +9 -9
  47. package/src/core/rules/no-unmanaged-exceptions.js.map +1 -1
  48. package/src/core/rules/require-return-type.d.ts +3 -3
  49. package/src/core/rules/require-return-type.js +6 -6
  50. package/src/core/rules/require-return-type.js.map +1 -1
  51. package/src/core/rules/throw-cause-required.d.ts +3 -3
  52. package/src/core/rules/throw-cause-required.js +9 -9
  53. package/src/core/rules/throw-cause-required.js.map +1 -1
  54. package/src/core/rules/validate-ts-in-src.d.ts +3 -3
  55. package/src/core/rules/validate-ts-in-src.js +7 -7
  56. package/src/core/rules/validate-ts-in-src.js.map +1 -1
  57. package/src/core/runner.d.ts +10 -19
  58. package/src/core/runner.js +24 -622
  59. package/src/core/runner.js.map +1 -1
  60. package/src/index.d.ts +2 -15
  61. package/src/index.js +4 -54
  62. package/src/index.js.map +1 -1
  63. package/openclaw.plugin.json +0 -15
  64. package/src/adapters/agent-adapters.d.ts +0 -13
  65. package/src/adapters/agent-adapters.js +0 -23
  66. package/src/adapters/agent-adapters.js.map +0 -1
  67. package/src/adapters/agent-payload.d.ts +0 -48
  68. package/src/adapters/agent-payload.js +0 -30
  69. package/src/adapters/agent-payload.js.map +0 -1
  70. package/src/adapters/agent-response.d.ts +0 -5
  71. package/src/adapters/agent-response.js +0 -86
  72. package/src/adapters/agent-response.js.map +0 -1
  73. package/src/adapters/claude-code-adapter.d.ts +0 -26
  74. package/src/adapters/claude-code-adapter.js +0 -69
  75. package/src/adapters/claude-code-adapter.js.map +0 -1
  76. package/src/adapters/codex-adapter.d.ts +0 -19
  77. package/src/adapters/codex-adapter.js +0 -48
  78. package/src/adapters/codex-adapter.js.map +0 -1
  79. package/src/adapters/codex-subagent-guard.d.ts +0 -45
  80. package/src/adapters/codex-subagent-guard.js +0 -88
  81. package/src/adapters/codex-subagent-guard.js.map +0 -1
  82. package/src/adapters/detect-ai.d.ts +0 -70
  83. package/src/adapters/detect-ai.js +0 -82
  84. package/src/adapters/detect-ai.js.map +0 -1
  85. package/src/adapters/guards-hook.d.ts +0 -3
  86. package/src/adapters/guards-hook.js +0 -30
  87. package/src/adapters/guards-hook.js.map +0 -1
  88. package/src/adapters/openclaw-plugin.d.ts +0 -14
  89. package/src/adapters/openclaw-plugin.js +0 -87
  90. package/src/adapters/openclaw-plugin.js.map +0 -1
  91. package/src/adapters/review-identity-stamper.d.ts +0 -20
  92. package/src/adapters/review-identity-stamper.js +0 -34
  93. package/src/adapters/review-identity-stamper.js.map +0 -1
  94. package/src/bin/codex-trust.d.ts +0 -105
  95. package/src/bin/codex-trust.js +0 -205
  96. package/src/bin/codex-trust.js.map +0 -1
  97. package/src/bin/dev-hook-install.d.ts +0 -1
  98. package/src/bin/dev-hook-install.js +0 -82
  99. package/src/bin/dev-hook-install.js.map +0 -1
  100. package/src/bin/dev-hook-uninstall.d.ts +0 -1
  101. package/src/bin/dev-hook-uninstall.js +0 -45
  102. package/src/bin/dev-hook-uninstall.js.map +0 -1
  103. package/src/bin/hook-registration.d.ts +0 -429
  104. package/src/bin/hook-registration.js +0 -661
  105. package/src/bin/hook-registration.js.map +0 -1
  106. package/src/bin/install-entry.d.ts +0 -19
  107. package/src/bin/install-entry.js +0 -128
  108. package/src/bin/install-entry.js.map +0 -1
  109. package/src/bin/l0-allowlist.d.ts +0 -49
  110. package/src/bin/l0-allowlist.js +0 -528
  111. package/src/bin/l0-allowlist.js.map +0 -1
  112. package/src/bin/l0-codex-read.d.ts +0 -21
  113. package/src/bin/l0-codex-read.js +0 -109
  114. package/src/bin/l0-codex-read.js.map +0 -1
  115. package/src/bin/l0-decide.d.ts +0 -50
  116. package/src/bin/l0-decide.js +0 -103
  117. package/src/bin/l0-decide.js.map +0 -1
  118. package/src/bin/l0-entry.d.ts +0 -103
  119. package/src/bin/l0-entry.js +0 -98
  120. package/src/bin/l0-entry.js.map +0 -1
  121. package/src/bin/l0-ignored-tools.d.ts +0 -38
  122. package/src/bin/l0-ignored-tools.js +0 -44
  123. package/src/bin/l0-ignored-tools.js.map +0 -1
  124. package/src/bin/managed-env.d.ts +0 -46
  125. package/src/bin/managed-env.js +0 -50
  126. package/src/bin/managed-env.js.map +0 -1
  127. package/src/bin/neighbour-hooks.d.ts +0 -42
  128. package/src/bin/neighbour-hooks.js +0 -177
  129. package/src/bin/neighbour-hooks.js.map +0 -1
  130. package/src/bin/reviewer-agent.d.ts +0 -19
  131. package/src/bin/reviewer-agent.js +0 -135
  132. package/src/bin/reviewer-agent.js.map +0 -1
  133. package/src/bin/settings-shape.d.ts +0 -37
  134. package/src/bin/settings-shape.js +0 -17
  135. package/src/bin/settings-shape.js.map +0 -1
  136. package/src/bin/setup-config.d.ts +0 -19
  137. package/src/bin/setup-config.js +0 -370
  138. package/src/bin/setup-config.js.map +0 -1
  139. package/src/bin/setup.d.ts +0 -78
  140. package/src/bin/setup.js +0 -400
  141. package/src/bin/setup.js.map +0 -1
  142. package/src/bin/shim-audit-log.d.ts +0 -119
  143. package/src/bin/shim-audit-log.js +0 -286
  144. package/src/bin/shim-audit-log.js.map +0 -1
  145. package/src/bin/shim-deny-reason.d.ts +0 -1
  146. package/src/bin/shim-deny-reason.js +0 -244
  147. package/src/bin/shim-deny-reason.js.map +0 -1
  148. package/src/bin/shim-drift-fix.d.ts +0 -1
  149. package/src/bin/shim-drift-fix.js +0 -72
  150. package/src/bin/shim-drift-fix.js.map +0 -1
  151. package/src/bin/shim-testkit.d.ts +0 -107
  152. package/src/bin/shim-testkit.js +0 -197
  153. package/src/bin/shim-testkit.js.map +0 -1
  154. package/src/bin/shim-version-drift.d.ts +0 -1
  155. package/src/bin/shim-version-drift.js +0 -164
  156. package/src/bin/shim-version-drift.js.map +0 -1
  157. package/src/bin/shim.d.ts +0 -16
  158. package/src/bin/shim.js +0 -636
  159. package/src/bin/shim.js.map +0 -1
  160. package/src/bin/upgrade-shim.d.ts +0 -18
  161. package/src/bin/upgrade-shim.js +0 -283
  162. package/src/bin/upgrade-shim.js.map +0 -1
  163. package/src/core/ai-type-context.d.ts +0 -36
  164. package/src/core/ai-type-context.js +0 -45
  165. package/src/core/ai-type-context.js.map +0 -1
  166. package/src/core/apply-patch-parse.d.ts +0 -36
  167. package/src/core/apply-patch-parse.js +0 -154
  168. package/src/core/apply-patch-parse.js.map +0 -1
  169. package/src/core/build-context.d.ts +0 -10
  170. package/src/core/build-context.js +0 -66
  171. package/src/core/build-context.js.map +0 -1
  172. package/src/core/command-scan.d.ts +0 -122
  173. package/src/core/command-scan.js +0 -282
  174. package/src/core/command-scan.js.map +0 -1
  175. package/src/core/custom-rule-adapter.d.ts +0 -22
  176. package/src/core/custom-rule-adapter.js +0 -50
  177. package/src/core/custom-rule-adapter.js.map +0 -1
  178. package/src/core/decision-log.d.ts +0 -190
  179. package/src/core/decision-log.js +0 -406
  180. package/src/core/decision-log.js.map +0 -1
  181. package/src/core/delete-scoped-rules.d.ts +0 -8
  182. package/src/core/delete-scoped-rules.js +0 -31
  183. package/src/core/delete-scoped-rules.js.map +0 -1
  184. package/src/core/disable-directives.d.ts +0 -8
  185. package/src/core/disable-directives.js +0 -80
  186. package/src/core/disable-directives.js.map +0 -1
  187. package/src/core/effective-tree.d.ts +0 -204
  188. package/src/core/effective-tree.js +0 -277
  189. package/src/core/effective-tree.js.map +0 -1
  190. package/src/core/excluded-path-escape.d.ts +0 -101
  191. package/src/core/excluded-path-escape.js +0 -236
  192. package/src/core/excluded-path-escape.js.map +0 -1
  193. package/src/core/excluded-paths.d.ts +0 -23
  194. package/src/core/excluded-paths.js +0 -54
  195. package/src/core/excluded-paths.js.map +0 -1
  196. package/src/core/fix-hint.d.ts +0 -40
  197. package/src/core/fix-hint.js +0 -53
  198. package/src/core/fix-hint.js.map +0 -1
  199. package/src/core/force-to-root.d.ts +0 -32
  200. package/src/core/force-to-root.js +0 -60
  201. package/src/core/force-to-root.js.map +0 -1
  202. package/src/core/guard-index-doc.d.ts +0 -63
  203. package/src/core/guard-index-doc.js +0 -172
  204. package/src/core/guard-index-doc.js.map +0 -1
  205. package/src/core/instruct-ai-writer.d.ts +0 -1
  206. package/src/core/instruct-ai-writer.js +0 -6
  207. package/src/core/instruct-ai-writer.js.map +0 -1
  208. package/src/core/l0-fault-codes.d.ts +0 -85
  209. package/src/core/l0-fault-codes.js +0 -121
  210. package/src/core/l0-fault-codes.js.map +0 -1
  211. package/src/core/l0-matrix.d.ts +0 -114
  212. package/src/core/l0-matrix.js +0 -416
  213. package/src/core/l0-matrix.js.map +0 -1
  214. package/src/core/l0-tooling-doc.d.ts +0 -45
  215. package/src/core/l0-tooling-doc.js +0 -258
  216. package/src/core/l0-tooling-doc.js.map +0 -1
  217. package/src/core/l1-doc.d.ts +0 -7
  218. package/src/core/l1-doc.js +0 -340
  219. package/src/core/l1-doc.js.map +0 -1
  220. package/src/core/l1-matrix-doc.d.ts +0 -31
  221. package/src/core/l1-matrix-doc.js +0 -111
  222. package/src/core/l1-matrix-doc.js.map +0 -1
  223. package/src/core/l1-rows.d.ts +0 -167
  224. package/src/core/l1-rows.js +0 -262
  225. package/src/core/l1-rows.js.map +0 -1
  226. package/src/core/l2-doc.d.ts +0 -7
  227. package/src/core/l2-doc.js +0 -385
  228. package/src/core/l2-doc.js.map +0 -1
  229. package/src/core/l2-matrix-doc.d.ts +0 -23
  230. package/src/core/l2-matrix-doc.js +0 -72
  231. package/src/core/l2-matrix-doc.js.map +0 -1
  232. package/src/core/l2-row.d.ts +0 -54
  233. package/src/core/l2-row.js +0 -75
  234. package/src/core/l2-row.js.map +0 -1
  235. package/src/core/l2-rows.d.ts +0 -56
  236. package/src/core/l2-rows.js +0 -272
  237. package/src/core/l2-rows.js.map +0 -1
  238. package/src/core/load-config.d.ts +0 -1
  239. package/src/core/load-config.js +0 -9
  240. package/src/core/load-config.js.map +0 -1
  241. package/src/core/log-stream.d.ts +0 -117
  242. package/src/core/log-stream.js +0 -156
  243. package/src/core/log-stream.js.map +0 -1
  244. package/src/core/log-streams.d.ts +0 -48
  245. package/src/core/log-streams.js +0 -55
  246. package/src/core/log-streams.js.map +0 -1
  247. package/src/core/log-target.d.ts +0 -21
  248. package/src/core/log-target.js +0 -29
  249. package/src/core/log-target.js.map +0 -1
  250. package/src/core/main-sync-log.d.ts +0 -16
  251. package/src/core/main-sync-log.js +0 -105
  252. package/src/core/main-sync-log.js.map +0 -1
  253. package/src/core/main-sync-refresh.d.ts +0 -39
  254. package/src/core/main-sync-refresh.js +0 -96
  255. package/src/core/main-sync-refresh.js.map +0 -1
  256. package/src/core/main-sync-timeout.d.ts +0 -32
  257. package/src/core/main-sync-timeout.js +0 -94
  258. package/src/core/main-sync-timeout.js.map +0 -1
  259. package/src/core/matrix-cures.d.ts +0 -12
  260. package/src/core/matrix-cures.js +0 -56
  261. package/src/core/matrix-cures.js.map +0 -1
  262. package/src/core/missing-directory.d.ts +0 -38
  263. package/src/core/missing-directory.js +0 -66
  264. package/src/core/missing-directory.js.map +0 -1
  265. package/src/core/read-only-inspection.d.ts +0 -34
  266. package/src/core/read-only-inspection.js +0 -88
  267. package/src/core/read-only-inspection.js.map +0 -1
  268. package/src/core/rejection-log.d.ts +0 -9
  269. package/src/core/rejection-log.js +0 -258
  270. package/src/core/rejection-log.js.map +0 -1
  271. package/src/core/report.d.ts +0 -21
  272. package/src/core/report.js +0 -73
  273. package/src/core/report.js.map +0 -1
  274. package/src/core/rule-base.d.ts +0 -39
  275. package/src/core/rule-base.js +0 -36
  276. package/src/core/rule-base.js.map +0 -1
  277. package/src/core/rules/branch-creation-guard.d.ts +0 -113
  278. package/src/core/rules/branch-creation-guard.js +0 -422
  279. package/src/core/rules/branch-creation-guard.js.map +0 -1
  280. package/src/core/rules/branch-switch-scan.d.ts +0 -43
  281. package/src/core/rules/branch-switch-scan.js +0 -132
  282. package/src/core/rules/branch-switch-scan.js.map +0 -1
  283. package/src/core/rules/build-output-pipe-guard.d.ts +0 -56
  284. package/src/core/rules/build-output-pipe-guard.js +0 -120
  285. package/src/core/rules/build-output-pipe-guard.js.map +0 -1
  286. package/src/core/rules/build-output-pipe-scan.d.ts +0 -47
  287. package/src/core/rules/build-output-pipe-scan.js +0 -84
  288. package/src/core/rules/build-output-pipe-scan.js.map +0 -1
  289. package/src/core/rules/cap-remedies.d.ts +0 -39
  290. package/src/core/rules/cap-remedies.js +0 -59
  291. package/src/core/rules/cap-remedies.js.map +0 -1
  292. package/src/core/rules/commit-message-substitution-guard.d.ts +0 -72
  293. package/src/core/rules/commit-message-substitution-guard.js +0 -134
  294. package/src/core/rules/commit-message-substitution-guard.js.map +0 -1
  295. package/src/core/rules/commit-message-substitution-scan.d.ts +0 -42
  296. package/src/core/rules/commit-message-substitution-scan.js +0 -145
  297. package/src/core/rules/commit-message-substitution-scan.js.map +0 -1
  298. package/src/core/rules/content-read-scan.d.ts +0 -76
  299. package/src/core/rules/content-read-scan.js +0 -176
  300. package/src/core/rules/content-read-scan.js.map +0 -1
  301. package/src/core/rules/cure-prefix-scan.d.ts +0 -64
  302. package/src/core/rules/cure-prefix-scan.js +0 -133
  303. package/src/core/rules/cure-prefix-scan.js.map +0 -1
  304. package/src/core/rules/feature-branch-guard.d.ts +0 -121
  305. package/src/core/rules/feature-branch-guard.js +0 -326
  306. package/src/core/rules/feature-branch-guard.js.map +0 -1
  307. package/src/core/rules/judged-tree.d.ts +0 -66
  308. package/src/core/rules/judged-tree.js +0 -97
  309. package/src/core/rules/judged-tree.js.map +0 -1
  310. package/src/core/rules/main-freshness.d.ts +0 -37
  311. package/src/core/rules/main-freshness.js +0 -61
  312. package/src/core/rules/main-freshness.js.map +0 -1
  313. package/src/core/rules/merge-in-progress-guard.d.ts +0 -17
  314. package/src/core/rules/merge-in-progress-guard.js +0 -122
  315. package/src/core/rules/merge-in-progress-guard.js.map +0 -1
  316. package/src/core/rules/merged-branch-bash-guard.d.ts +0 -62
  317. package/src/core/rules/merged-branch-bash-guard.js +0 -170
  318. package/src/core/rules/merged-branch-bash-guard.js.map +0 -1
  319. package/src/core/rules/merged-branch-message.d.ts +0 -59
  320. package/src/core/rules/merged-branch-message.js +0 -137
  321. package/src/core/rules/merged-branch-message.js.map +0 -1
  322. package/src/core/rules/pr-creation-or-push-guard.d.ts +0 -26
  323. package/src/core/rules/pr-creation-or-push-guard.js +0 -146
  324. package/src/core/rules/pr-creation-or-push-guard.js.map +0 -1
  325. package/src/core/rules/pr-merge-guard.d.ts +0 -36
  326. package/src/core/rules/pr-merge-guard.js +0 -86
  327. package/src/core/rules/pr-merge-guard.js.map +0 -1
  328. package/src/core/rules/read-stale-guard.d.ts +0 -136
  329. package/src/core/rules/read-stale-guard.js +0 -326
  330. package/src/core/rules/read-stale-guard.js.map +0 -1
  331. package/src/core/rules/recovery-allowlist.d.ts +0 -49
  332. package/src/core/rules/recovery-allowlist.js +0 -187
  333. package/src/core/rules/recovery-allowlist.js.map +0 -1
  334. package/src/core/rules/redirect-how-to-merge-main.d.ts +0 -16
  335. package/src/core/rules/redirect-how-to-merge-main.js +0 -157
  336. package/src/core/rules/redirect-how-to-merge-main.js.map +0 -1
  337. package/src/core/rules/shell-segment-scan.d.ts +0 -64
  338. package/src/core/rules/shell-segment-scan.js +0 -88
  339. package/src/core/rules/shell-segment-scan.js.map +0 -1
  340. package/src/core/rules/stale-main-bash-guard.d.ts +0 -198
  341. package/src/core/rules/stale-main-bash-guard.js +0 -361
  342. package/src/core/rules/stale-main-bash-guard.js.map +0 -1
  343. package/src/core/rules/stale-main-message.d.ts +0 -62
  344. package/src/core/rules/stale-main-message.js +0 -102
  345. package/src/core/rules/stale-main-message.js.map +0 -1
  346. package/src/core/rules/tree-recovery.d.ts +0 -92
  347. package/src/core/rules/tree-recovery.js +0 -165
  348. package/src/core/rules/tree-recovery.js.map +0 -1
  349. package/src/core/rules/wait-spin-guard.d.ts +0 -128
  350. package/src/core/rules/wait-spin-guard.js +0 -211
  351. package/src/core/rules/wait-spin-guard.js.map +0 -1
  352. package/src/core/rules/wait-spin-scan.d.ts +0 -118
  353. package/src/core/rules/wait-spin-scan.js +0 -203
  354. package/src/core/rules/wait-spin-scan.js.map +0 -1
  355. package/src/core/rules/whole-repo-build-guard.d.ts +0 -125
  356. package/src/core/rules/whole-repo-build-guard.js +0 -257
  357. package/src/core/rules/whole-repo-build-guard.js.map +0 -1
  358. package/src/core/rules/whole-repo-build-scan.d.ts +0 -28
  359. package/src/core/rules/whole-repo-build-scan.js +0 -198
  360. package/src/core/rules/whole-repo-build-scan.js.map +0 -1
  361. package/src/core/session-call-history.d.ts +0 -44
  362. package/src/core/session-call-history.js +0 -79
  363. package/src/core/session-call-history.js.map +0 -1
  364. package/src/core/shell-read-parity.d.ts +0 -56
  365. package/src/core/shell-read-parity.js +0 -155
  366. package/src/core/shell-read-parity.js.map +0 -1
  367. package/src/core/strip-ts-noise.d.ts +0 -1
  368. package/src/core/strip-ts-noise.js +0 -185
  369. package/src/core/strip-ts-noise.js.map +0 -1
  370. package/src/core/sync-main.d.ts +0 -46
  371. package/src/core/sync-main.js +0 -175
  372. package/src/core/sync-main.js.map +0 -1
  373. package/src/core/target-tree.d.ts +0 -84
  374. package/src/core/target-tree.js +0 -148
  375. package/src/core/target-tree.js.map +0 -1
  376. package/src/core/to-error.d.ts +0 -5
  377. package/src/core/to-error.js +0 -37
  378. package/src/core/to-error.js.map +0 -1
  379. package/src/core/types.d.ts +0 -152
  380. package/src/core/types.js +0 -200
  381. package/src/core/types.js.map +0 -1
  382. package/src/core/version-sync.d.ts +0 -219
  383. package/src/core/version-sync.js +0 -558
  384. package/src/core/version-sync.js.map +0 -1
  385. package/src/core/webpieces-versions.d.ts +0 -98
  386. package/src/core/webpieces-versions.js +0 -206
  387. package/src/core/webpieces-versions.js.map +0 -1
  388. package/templates/ai-hook.sh +0 -497
  389. package/templates/claude-settings-hook.json +0 -28
  390. package/templates/webpieces-reviewer.md +0 -60
@@ -1,136 +0,0 @@
1
- import { BranchStateGuardConfig } from '@webpieces/rules-config';
2
- import type { FileContext, Violation } from '../types';
3
- import { FileRuleBase } from '../rule-base';
4
- import { FixHint } from '../fix-hint';
5
- /**
6
- * Blocks READS while the checked-out branch is a stale place to read from. TWO states:
7
- *
8
- * A. on `main`, and local main is farther BEHIND origin/main than the configured tolerance
9
- * B. on a feature branch whose PR is ALREADY MERGED (a pre-merge snapshot; origin/main has moved
10
- * past it and a squash merge means its HEAD is not even an ancestor of main)
11
- *
12
- * WHY READ, of all tools: either state means the AI reads stale FILE CONTENT and then reasons,
13
- * plans and writes against code that no longer exists upstream. Blocking the write is too late —
14
- * the bad premise is already in context. So the block lands on the read. (feature-branch-guard
15
- * blocks the WRITE in state B; this guard is the read-side half of that same protection, and the
16
- * two share one recovery message via MergedBranchMessage.)
17
- *
18
- * THERE IS NO DIRTY-TREE ASYMMETRY, and there is no dirty-tree valve in either state. Both used to
19
- * fail open on uncommitted work; both now block. The argument for the state-A valve was that its cure,
20
- * an in-place pull, is not a fast-forward on a dirty tree — true, but that is a fact about the
21
- * MESSAGE, which printed only the pull. Row 6 has always carried a second cure, `git checkout -b <new>
22
- * origin/main`, and that one CARRIES uncommitted changes onto the new branch, so the work comes with
23
- * you and nothing is trapped. StaleMainMessage now prints both, labelled with which survives a dirty
24
- * tree, so the block no longer has to be suppressed to keep the printed cure runnable. State B's valve
25
- * never had an argument at all — its cure was always the branch form.
26
- *
27
- * Residual, in both states: if `origin/main` changed the same files you edited, git refuses the switch.
28
- * `git stash` is on the L2 skip list and is never blocked, so the path out is stash → branch → pop.
29
- *
30
- * WHY THIS CANNOT WEDGE: the block is scoped to Read ONLY. Every cure — `pnpm wp-sync-main`,
31
- * `pnpm install`, any webpieces upgrade — is a Bash command, and this guard never looks at Bash.
32
- * So there is no command allowlist to maintain and no way to lock the agent out of its own fix.
33
- * (Every `wp-*` bin is on the L2 skip list, and the pull it wraps is explicitly permitted on main by
34
- * redirect-how-to-merge-main, which returns null when the branch IS main — the guards are
35
- * complementary, not stacked.)
36
- *
37
- * That scoping is also this guard's HOLE, and it is closed elsewhere rather than here: leaving Bash
38
- * entirely alone let a session `cat`/`grep`/`ls` the same stale tree the Read block was rejecting,
39
- * for a whole session, while the logs read "read-stale-guard handled". stale-main-bash-guard is the
40
- * State-A Bash counterpart (as merged-branch-bash-guard is State B's): in the SAME state this guard
41
- * blocks — `main`, KNOWN BEYOND the cached commit-distance tolerance below — it default-denies Bash and
42
- * allowlists only the commands that get you out, so the cure is never blocked and this guard can stay
43
- * simple and Read-only.
44
- *
45
- * Everything here is FAIL-OPEN on data we could not ESTABLISH. A guard that blocks reads on bad data
46
- * is far worse than one that misses; every unknown resolves to "allow". Note the dual, which is what
47
- * the deleted dirty valve violated: never fail open on data you DID establish. A dirty tree is not an
48
- * unknown — it is a known state with a known cure. The three deliberate escape valves:
49
- *
50
- * 1. CACHE LAG — we do NOT compare hashes for equality. The cached `originMain` is written by
51
- * the detached refresher and is arbitrarily old, so `local !== origin` stays
52
- * true for a while AFTER a successful pull, which would spin the agent forever.
53
- * Instead: is the cached origin/main an ANCESTOR of local main? If local main
54
- * already contains it, we are not behind. That flips the instant the pull lands,
55
- * with no refresher round-trip. This is the single most important line here.
56
- * 2. CONFIG READ — webpieces.config.json stays readable so the agent can always read-then-edit
57
- * it to set `mode: OFF`. Its EDIT is already bypassed in runner.ts + hook-core;
58
- * this closes the read half of that same escape hatch.
59
- * 3. NO DATA — no cache, cache for another branch, empty originMain (offline), or no local
60
- * main at all (fresh clone / worktree) → allow.
61
- *
62
- * Runs from the Read fast path in hook-core (Read is neither a file-edit nor a bash payload, so it
63
- * never reaches the runner's rule loop). Fires the detached refresher on every call, which is also
64
- * what makes reads keep the shared main-sync cache warm for feature-branch-guard.
65
- */
66
- export declare class ReadStaleGuardRule extends FileRuleBase<BranchStateGuardConfig> {
67
- constructor(config: BranchStateGuardConfig);
68
- private readonly freshness;
69
- readonly description = "Block reads on a branch that is stale to read from \u2014 a `main` beyond its configured commit-distance tolerance, or a feature branch whose PR is already merged.";
70
- readonly files: string[];
71
- readonly defaultOptions: {
72
- hangTimeoutMinutes: number;
73
- };
74
- readonly fixHint: FixHint;
75
- /**
76
- * WHICH TREE is judged — the one that owns the FILE BEING READ, resolved by git through
77
- * TargetTreeResolver, never by `ctx.workspaceRoot`. Same fix and same reasoning as
78
- * feature-branch-guard's (issue #851): `ctx.workspaceRoot` is the walk-up from the SESSION's cwd, so
79
- * a main-session Read of a file inside an agent worktree was judged on the PRIMARY clone's branch —
80
- * and a `main` that the primary happened to be sitting on would close reads of another tree's
81
- * feature branch.
82
- */
83
- check(ctx: FileContext): readonly Violation[];
84
- private checkStaleMain;
85
- /**
86
- * State B — a feature branch whose PR is already merged. Reads a PRE-MERGE snapshot, so every
87
- * plan built from it is built on code origin/main has moved past.
88
- *
89
- * `branchAlreadyMerged` comes straight from the shared cache (the refresher's `gh pr list --state
90
- * merged`), so this path spawns nothing. No `gh` / offline → `mergedPr` is '' → not merged → allow,
91
- * which is the fail-open direction for free.
92
- *
93
- * NO DIRTY-TREE VALVE. `git checkout -b <new> origin/main` carries uncommitted changes onto the
94
- * fresh branch, so the work comes with you and there is nothing to rescue by reading. When it does
95
- * NOT (an overlapping change landed in main, so git refuses the switch), `git stash` is on the L2
96
- * skip list and is never blocked: stash → branch → pop. The valve that used to sit here was drift
97
- * from the documented design, not a decision — this docblock described the strict behaviour for
98
- * releases while the code failed open.
99
- */
100
- private checkMergedBranch;
101
- private mergedMessage;
102
- private isConfigFile;
103
- private staleMainMessage;
104
- private cacheSummary;
105
- /**
106
- * The guard could not ESTABLISH the state it judges on, so it judged nothing.
107
- *
108
- * A sibling of allow() rather than a reason string passed to it, because the difference has to
109
- * reach the LOG as a value: `ALLOW_FAIL_OPEN` vs `ALLOW`. It was previously a `' (fail-open)'`
110
- * suffix on the free-text reason, which meant an abstention and a real approval were the same
111
- * verdict and the abstentions could not be counted — so nobody could tell whether these guards
112
- * were protecting anything or quietly standing down. Never block on data you could not
113
- * establish; but say out loud, in a field, that you did not establish it.
114
- */
115
- private failOpen;
116
- private allow;
117
- private block;
118
- private logDecision;
119
- /**
120
- * The current branch, WITHOUT spawning git on the common path.
121
- *
122
- * This runs on EVERY read, so it is the one call whose cost actually matters. Spawning
123
- * `git rev-parse --abbrev-ref HEAD` measures ~12ms — essentially all process-spawn overhead —
124
- * whereas `.git/HEAD` is a single tiny file whose read is microseconds. On a feature branch
125
- * (the overwhelmingly common case) that file read is the ONLY work this guard does before
126
- * short-circuiting, so reads stay effectively free.
127
- *
128
- * Falls back to spawning git whenever `.git/HEAD` cannot answer authoritatively:
129
- * - `.git` is a FILE, not a dir → we are in a worktree and HEAD lives elsewhere
130
- * - detached HEAD → the file holds a raw sha, not a `ref:` line
131
- * - anything unreadable/unexpected
132
- * The fallback is correct in all those cases; it is just slower, and they are rare.
133
- */
134
- private currentBranch;
135
- private branchFromGitHead;
136
- }
@@ -1,326 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.ReadStaleGuardRule = void 0;
4
- const tslib_1 = require("tslib");
5
- const child_process_1 = require("child_process");
6
- const fs = tslib_1.__importStar(require("fs"));
7
- const path = tslib_1.__importStar(require("path"));
8
- const rules_config_1 = require("@webpieces/rules-config");
9
- const repo_workflow_core_1 = require("@webpieces/repo-workflow-core");
10
- const types_1 = require("../types");
11
- const rule_base_1 = require("../rule-base");
12
- const fix_hint_1 = require("../fix-hint");
13
- const to_error_1 = require("../to-error");
14
- const main_sync_refresh_1 = require("../main-sync-refresh");
15
- const main_sync_timeout_1 = require("../main-sync-timeout");
16
- const decision_log_1 = require("../decision-log");
17
- const l2_matrix_doc_1 = require("../l2-matrix-doc");
18
- const l0_fault_codes_1 = require("../l0-fault-codes");
19
- const target_tree_1 = require("../target-tree");
20
- const merged_branch_message_1 = require("./merged-branch-message");
21
- const judged_tree_1 = require("./judged-tree");
22
- const stale_main_message_1 = require("./stale-main-message");
23
- const main_freshness_1 = require("./main-freshness");
24
- const tree_recovery_1 = require("./tree-recovery");
25
- /**
26
- * Blocks READS while the checked-out branch is a stale place to read from. TWO states:
27
- *
28
- * A. on `main`, and local main is farther BEHIND origin/main than the configured tolerance
29
- * B. on a feature branch whose PR is ALREADY MERGED (a pre-merge snapshot; origin/main has moved
30
- * past it and a squash merge means its HEAD is not even an ancestor of main)
31
- *
32
- * WHY READ, of all tools: either state means the AI reads stale FILE CONTENT and then reasons,
33
- * plans and writes against code that no longer exists upstream. Blocking the write is too late —
34
- * the bad premise is already in context. So the block lands on the read. (feature-branch-guard
35
- * blocks the WRITE in state B; this guard is the read-side half of that same protection, and the
36
- * two share one recovery message via MergedBranchMessage.)
37
- *
38
- * THERE IS NO DIRTY-TREE ASYMMETRY, and there is no dirty-tree valve in either state. Both used to
39
- * fail open on uncommitted work; both now block. The argument for the state-A valve was that its cure,
40
- * an in-place pull, is not a fast-forward on a dirty tree — true, but that is a fact about the
41
- * MESSAGE, which printed only the pull. Row 6 has always carried a second cure, `git checkout -b <new>
42
- * origin/main`, and that one CARRIES uncommitted changes onto the new branch, so the work comes with
43
- * you and nothing is trapped. StaleMainMessage now prints both, labelled with which survives a dirty
44
- * tree, so the block no longer has to be suppressed to keep the printed cure runnable. State B's valve
45
- * never had an argument at all — its cure was always the branch form.
46
- *
47
- * Residual, in both states: if `origin/main` changed the same files you edited, git refuses the switch.
48
- * `git stash` is on the L2 skip list and is never blocked, so the path out is stash → branch → pop.
49
- *
50
- * WHY THIS CANNOT WEDGE: the block is scoped to Read ONLY. Every cure — `pnpm wp-sync-main`,
51
- * `pnpm install`, any webpieces upgrade — is a Bash command, and this guard never looks at Bash.
52
- * So there is no command allowlist to maintain and no way to lock the agent out of its own fix.
53
- * (Every `wp-*` bin is on the L2 skip list, and the pull it wraps is explicitly permitted on main by
54
- * redirect-how-to-merge-main, which returns null when the branch IS main — the guards are
55
- * complementary, not stacked.)
56
- *
57
- * That scoping is also this guard's HOLE, and it is closed elsewhere rather than here: leaving Bash
58
- * entirely alone let a session `cat`/`grep`/`ls` the same stale tree the Read block was rejecting,
59
- * for a whole session, while the logs read "read-stale-guard handled". stale-main-bash-guard is the
60
- * State-A Bash counterpart (as merged-branch-bash-guard is State B's): in the SAME state this guard
61
- * blocks — `main`, KNOWN BEYOND the cached commit-distance tolerance below — it default-denies Bash and
62
- * allowlists only the commands that get you out, so the cure is never blocked and this guard can stay
63
- * simple and Read-only.
64
- *
65
- * Everything here is FAIL-OPEN on data we could not ESTABLISH. A guard that blocks reads on bad data
66
- * is far worse than one that misses; every unknown resolves to "allow". Note the dual, which is what
67
- * the deleted dirty valve violated: never fail open on data you DID establish. A dirty tree is not an
68
- * unknown — it is a known state with a known cure. The three deliberate escape valves:
69
- *
70
- * 1. CACHE LAG — we do NOT compare hashes for equality. The cached `originMain` is written by
71
- * the detached refresher and is arbitrarily old, so `local !== origin` stays
72
- * true for a while AFTER a successful pull, which would spin the agent forever.
73
- * Instead: is the cached origin/main an ANCESTOR of local main? If local main
74
- * already contains it, we are not behind. That flips the instant the pull lands,
75
- * with no refresher round-trip. This is the single most important line here.
76
- * 2. CONFIG READ — webpieces.config.json stays readable so the agent can always read-then-edit
77
- * it to set `mode: OFF`. Its EDIT is already bypassed in runner.ts + hook-core;
78
- * this closes the read half of that same escape hatch.
79
- * 3. NO DATA — no cache, cache for another branch, empty originMain (offline), or no local
80
- * main at all (fresh clone / worktree) → allow.
81
- *
82
- * Runs from the Read fast path in hook-core (Read is neither a file-edit nor a bash payload, so it
83
- * never reaches the runner's rule loop). Fires the detached refresher on every call, which is also
84
- * what makes reads keep the shared main-sync cache warm for feature-branch-guard.
85
- */
86
- class ReadStaleGuardRule extends rule_base_1.FileRuleBase {
87
- constructor(config) { super(config, 'read-stale-guard', rules_config_1.BRANCH_STATE_GUARD_KEY); }
88
- // The ancestry test and the cache summary, shared with stale-main-bash-guard — see main-freshness.ts.
89
- freshness = new main_freshness_1.MainFreshness();
90
- description = 'Block reads on a branch that is stale to read from — a `main` beyond its configured commit-distance tolerance, or a feature branch whose PR is already merged.';
91
- files = ['**/*'];
92
- defaultOptions = {
93
- hangTimeoutMinutes: repo_workflow_core_1.DEFAULT_HANG_TIMEOUT_MINUTES,
94
- };
95
- fixHint = new fix_hint_1.FixHint('This branch is stale to read from — reading it would give you pre-merge/out-of-date content.', 'Get onto current code before reading anything else:', [
96
- new rules_config_1.Option('On main, beyond branch-state-guard.maxCommitsBehind → pnpm wp-sync-main (CLEAN TREE ONLY), or git checkout -b <new-branch> origin/main which works with UNCOMMITTED CHANGES and brings them along. On an already-merged branch → git fetch origin main && git checkout -b <new-branch> origin/main, which likewise carries your edits. Then retry the read.', true),
97
- new rules_config_1.Option('If a checkout -b refuses because origin/main changed the same files you edited: git stash (never blocked), redo the checkout, then git stash pop.'),
98
- new rules_config_1.Option("If pnpm wp-sync-main dies with 'fatal: Cannot fast-forward to multiple branches', .git/FETCH_HEAD holds a duplicate line — run 'git fetch --prune origin main' to rewrite it cleanly, then run it again."),
99
- new rules_config_1.Option('Still allowed right now: reading webpieces.config.json, and the Bash commands that get you OUT or tell you where you are — git checkout -b <new> origin/main, git switch, git pull/fetch, git status|log|diff|show|branch, git stash, gh, curl/wget, every wp-* bin, installs. Everything ELSE through Bash is blocked in this same state (a main beyond maxCommitsBehind → stale-main-bash-guard; a merged branch → merged-branch-bash-guard), and Write/Edit on main is blocked by feature-branch-guard however current main is. There is no side door: get onto a branch off origin/main.'),
100
- new rules_config_1.Option('Disable in webpieces.config.json under hookGuards → branch-state-guard (mode OFF) if intentional — that one key governs the Write, Read and Bash halves of this policy together.'),
101
- ]);
102
- /**
103
- * WHICH TREE is judged — the one that owns the FILE BEING READ, resolved by git through
104
- * TargetTreeResolver, never by `ctx.workspaceRoot`. Same fix and same reasoning as
105
- * feature-branch-guard's (issue #851): `ctx.workspaceRoot` is the walk-up from the SESSION's cwd, so
106
- * a main-session Read of a file inside an agent worktree was judged on the PRIMARY clone's branch —
107
- * and a `main` that the primary happened to be sitting on would close reads of another tree's
108
- * feature branch.
109
- */
110
- check(ctx) {
111
- // Outside the workspace root — no jurisdiction.
112
- if (ctx.relativePath.startsWith('..'))
113
- return [];
114
- const target = new target_tree_1.TargetTreeResolver().resolve(ctx.filePath, ctx.workspaceRoot);
115
- // A nested clone under `repositories/**` is out of scope, exactly as it is on the bash path.
116
- if (target.kind === 'foreign')
117
- return this.failOpen(ctx, null, 'target-tree-foreign');
118
- const judgedRoot = target.root;
119
- const branch = this.currentBranch(judgedRoot);
120
- if (branch === null)
121
- return this.failOpen(ctx, branch, 'branch-undeterminable');
122
- // Mid-rebase in the target tree: no branch name, so no key into the branch-keyed cache and
123
- // nothing to judge. Matrix row 14 — abstain, and say WHY rather than logging a cache miss.
124
- if (branch === 'HEAD')
125
- return this.failOpen(ctx, branch, 'detached-head');
126
- // Keep the shared cache warm for the next call. Detached; never blocks this read. Fired for
127
- // BOTH states — the merged-branch signal comes out of that same cache. Rooted at the JUDGED
128
- // tree so the entry refreshed is the one this guard reads.
129
- (0, main_sync_refresh_1.triggerMainSyncRefresh)(judgedRoot, (0, main_sync_timeout_1.hangTimeoutOf)(this.config));
130
- // Escape valve 3 — the read half of the config escape hatch. Ahead of BOTH states' blocks so
131
- // the agent can always read-then-edit the file that turns this guard off.
132
- if (this.isConfigFile(ctx.relativePath))
133
- return this.allow(ctx, branch, 'webpieces-config-read (escape hatch)');
134
- const judged = new judged_tree_1.JudgedTree(target, branch, target.governedRoot);
135
- return branch === 'main'
136
- ? this.checkStaleMain(ctx, judged)
137
- : this.checkMergedBranch(ctx, judged);
138
- }
139
- // State A — on main, possibly behind origin/main.
140
- checkStaleMain(ctx, judged) {
141
- const branch = judged.branch;
142
- const status = (0, repo_workflow_core_1.readMainSyncStatus)(judged.root, 'main');
143
- if (status === null)
144
- return this.failOpen(ctx, branch, 'no-sync-cache', 'cache=none');
145
- const cache = this.cacheSummary(status);
146
- // BELT-AND-BRACES since the cache became branch-keyed: we asked for the 'main' entry by key, so
147
- // a mismatch means the map's key and the entry's own `branch` disagree — a shape bug. Kept so
148
- // that degrades to an allow. Unreachable in normal operation.
149
- if (status.branch !== 'main')
150
- return this.failOpen(ctx, branch, 'stale-cross-branch-cache', cache);
151
- // Offline / origin unresolvable, or no local main to compare against.
152
- if (status.originMain === '')
153
- return this.failOpen(ctx, branch, 'origin-main-unknown', cache);
154
- // Escape valve 2 — ancestry, NOT equality. See the class comment.
155
- if (this.freshness.containsOriginMain(judged.root, status.originMain)) {
156
- return this.allow(ctx, branch, 'local-main-contains-origin (up to date)', cache);
157
- }
158
- const maxCommitsBehind = this.config.maxCommitsBehind ?? rules_config_1.DEFAULT_MAX_COMMITS_BEHIND;
159
- const withinThreshold = this.freshness.isWithinAllowedDrift(status, maxCommitsBehind);
160
- if (withinThreshold === null)
161
- return this.failOpen(ctx, branch, 'commit-distance-unknown', cache);
162
- if (withinThreshold)
163
- return this.allow(ctx, branch, 'within-max-commits-behind', cache);
164
- // NO DIRTY VALVE. It used to fail open here, on the argument that the prescribed in-place pull
165
- // is not a clean fast-forward on a dirty tree. That argument was about the MESSAGE, not the row:
166
- // row 6's cure cell has always offered `git checkout -b <new> origin/main` as an alternative,
167
- // and THAT works dirty — it carries uncommitted changes onto the new branch and lands you on
168
- // current code, which is the whole point. The message now leads with it when the tree is dirty
169
- // (StaleMainMessage.forReads), so the cure an agent reads is one it can actually run.
170
- // Residual, same as row 8: if origin/main touched the files you edited, git refuses the switch
171
- // — `git stash` is on the skip list and clears it. Two steps worst case, never a dead end.
172
- return this.block(ctx, branch, 'on-stale-main', this.staleMainMessage(judged, status.commitsBehind ?? 0), cache);
173
- }
174
- /**
175
- * State B — a feature branch whose PR is already merged. Reads a PRE-MERGE snapshot, so every
176
- * plan built from it is built on code origin/main has moved past.
177
- *
178
- * `branchAlreadyMerged` comes straight from the shared cache (the refresher's `gh pr list --state
179
- * merged`), so this path spawns nothing. No `gh` / offline → `mergedPr` is '' → not merged → allow,
180
- * which is the fail-open direction for free.
181
- *
182
- * NO DIRTY-TREE VALVE. `git checkout -b <new> origin/main` carries uncommitted changes onto the
183
- * fresh branch, so the work comes with you and there is nothing to rescue by reading. When it does
184
- * NOT (an overlapping change landed in main, so git refuses the switch), `git stash` is on the L2
185
- * skip list and is never blocked: stash → branch → pop. The valve that used to sit here was drift
186
- * from the documented design, not a decision — this docblock described the strict behaviour for
187
- * releases while the code failed open.
188
- */
189
- checkMergedBranch(ctx, judged) {
190
- const branch = judged.branch;
191
- const status = (0, repo_workflow_core_1.readMainSyncStatus)(judged.root, branch);
192
- if (status === null)
193
- return this.failOpen(ctx, branch, 'no-sync-cache', 'cache=none');
194
- const cache = this.cacheSummary(status);
195
- // BELT-AND-BRACES since the cache became branch-keyed: the entry was looked up BY `branch`, so
196
- // a mismatch is a shape bug rather than the old "cache is for another branch" state. Kept so
197
- // such a bug degrades to an allow. Unreachable in normal operation. (A branch the refresh has
198
- // not seen yet is the `status === null` case above — still fail-open.)
199
- if (status.branch !== branch)
200
- return this.failOpen(ctx, branch, 'stale-cross-branch-cache', cache);
201
- // NOT-MERGED, or NOT-ASKED? `branchAlreadyMerged: false` is produced both by "this branch has
202
- // no merged PR" and by "the forge could not be reached" (`gh` missing, unauthenticated,
203
- // rate-limited, offline). Same allow either way — never block on data you could not establish
204
- // — but the LOG must not call the second one an approval, or the trail cannot tell a policy
205
- // that is protecting something from one that is quietly standing down.
206
- if (!status.branchAlreadyMerged) {
207
- return status.forgeReachable
208
- ? this.allow(ctx, branch, 'clean-feature-branch', cache)
209
- : this.failOpen(ctx, branch, 'no-forge', cache);
210
- }
211
- // NO DIRTY VALVE — and this one never had an argument behind it at all. Row 8's cure is
212
- // `git fetch origin main && git checkout -b <new> origin/main`, which carries uncommitted work
213
- // with you, so a dirty tree traps nobody. The valve was code drift from the documented design;
214
- // read-stale-guard's own class comment said so while the code did the opposite.
215
- const pr = status.mergedPr !== '' ? status.mergedPr : '?';
216
- return this.block(ctx, branch, `already-merged PR#${pr}`, this.mergedMessage(judged, status.mergedPr), cache);
217
- }
218
- // The merged-branch text, told in the flavour of the tree we are standing in: a linked worktree
219
- // is told to open a NEW worktree off origin/main and reap this dead one; the primary clone is
220
- // told to branch off origin/main. Neither is ever told to `git checkout main` (fatal in a
221
- // worktree). Detection is one statSync — see WorktreeService.isLinkedWorktree.
222
- mergedMessage(judged, mergedPr) {
223
- const recovery = new tree_recovery_1.TreeRecovery();
224
- const body = new merged_branch_message_1.MergedBranchMessage(judged.root).forReads(judged.branch, mergedPr, recovery.kindOf(judged.root), judged.root);
225
- // NO redirect note here, deliberately — see feature-branch-guard.alreadyMergedMessage for the
226
- // argument. MergedBranchMessage already aims its commands at the judged root through `atRoot`
227
- // and prints the WORKTREE flavour of the cure when the judged tree is one.
228
- return [judged.header(), body].join('\n');
229
- }
230
- isConfigFile(relativePath) {
231
- return relativePath === 'webpieces.config.json';
232
- }
233
- // StaleMainMessage's remaining consumer. It used to be shared with stale-main-bash-guard so the two
234
- // halves of the State-A block could never prescribe different cures; that guard now blocks on the
235
- // BRANCH (row 5) rather than on staleness and carries its own message, so this is the only caller.
236
- staleMainMessage(judged, commitsBehind) {
237
- return [
238
- judged.header(),
239
- new stale_main_message_1.StaleMainMessage(judged.root).forReads(String(commitsBehind)),
240
- judged.redirectNote([judged.pnpmCure('wp-sync-main')]),
241
- ].join('\n');
242
- }
243
- cacheSummary(status) {
244
- return this.freshness.summarize(status);
245
- }
246
- /**
247
- * The guard could not ESTABLISH the state it judges on, so it judged nothing.
248
- *
249
- * A sibling of allow() rather than a reason string passed to it, because the difference has to
250
- * reach the LOG as a value: `ALLOW_FAIL_OPEN` vs `ALLOW`. It was previously a `' (fail-open)'`
251
- * suffix on the free-text reason, which meant an abstention and a real approval were the same
252
- * verdict and the abstentions could not be counted — so nobody could tell whether these guards
253
- * were protecting anything or quietly standing down. Never block on data you could not
254
- * establish; but say out loud, in a field, that you did not establish it.
255
- */
256
- failOpen(ctx, branch, reason, cache = '-') {
257
- this.logDecision(ctx, branch, 'ALLOW_FAIL_OPEN', reason, cache);
258
- return [];
259
- }
260
- allow(ctx, branch, reason, cache = '-') {
261
- this.logDecision(ctx, branch, 'ALLOW', reason, cache);
262
- return [];
263
- }
264
- block(ctx, branch, reason, message, cache = '-') {
265
- this.logDecision(ctx, branch, 'BLOCK_AI_CURE', reason, cache);
266
- // Deliver the matrix and name the row — see stale-main-bash-guard.block for why it is lazy.
267
- const pointer = (0, l2_matrix_doc_1.branchStateMatrixPointer)((0, l2_matrix_doc_1.writeBranchStateMatrixDoc)(ctx.workspaceRoot), (0, decision_log_1.matrixL2Row)(reason).row);
268
- return [new types_1.Violation(1, ctx.relativePath, message + pointer)];
269
- }
270
- logDecision(ctx, branch, verdict, reason, cache) {
271
- (0, decision_log_1.logGuardDecision)(ctx.workspaceRoot, new decision_log_1.GuardDecision('read-stale-guard', ctx.tool, ctx.relativePath, branch ?? 'unknown', verdict, reason, cache, l0_fault_codes_1.L0_FAULT_NONE, (0, decision_log_1.matrixL2Row)(reason)));
272
- }
273
- /**
274
- * The current branch, WITHOUT spawning git on the common path.
275
- *
276
- * This runs on EVERY read, so it is the one call whose cost actually matters. Spawning
277
- * `git rev-parse --abbrev-ref HEAD` measures ~12ms — essentially all process-spawn overhead —
278
- * whereas `.git/HEAD` is a single tiny file whose read is microseconds. On a feature branch
279
- * (the overwhelmingly common case) that file read is the ONLY work this guard does before
280
- * short-circuiting, so reads stay effectively free.
281
- *
282
- * Falls back to spawning git whenever `.git/HEAD` cannot answer authoritatively:
283
- * - `.git` is a FILE, not a dir → we are in a worktree and HEAD lives elsewhere
284
- * - detached HEAD → the file holds a raw sha, not a `ref:` line
285
- * - anything unreadable/unexpected
286
- * The fallback is correct in all those cases; it is just slower, and they are rare.
287
- */
288
- currentBranch(workspaceRoot) {
289
- const fromHead = this.branchFromGitHead(workspaceRoot);
290
- if (fromHead !== null)
291
- return fromHead;
292
- // eslint-disable-next-line @webpieces/no-unmanaged-exceptions
293
- try {
294
- return (0, child_process_1.execSync)('git rev-parse --abbrev-ref HEAD', {
295
- cwd: workspaceRoot,
296
- encoding: 'utf8',
297
- stdio: ['pipe', 'pipe', 'pipe'],
298
- }).trim();
299
- }
300
- catch (err) {
301
- const error = (0, to_error_1.toError)(err);
302
- void error;
303
- return null;
304
- }
305
- }
306
- // Parse `.git/HEAD` ("ref: refs/heads/<branch>"). null = cannot answer, caller must fall back.
307
- branchFromGitHead(workspaceRoot) {
308
- // eslint-disable-next-line @webpieces/no-unmanaged-exceptions
309
- try {
310
- const gitPath = path.join(workspaceRoot, '.git');
311
- // A worktree/submodule has `.git` as a file pointing at the real gitdir — HEAD is not here.
312
- if (!fs.statSync(gitPath).isDirectory())
313
- return null;
314
- const head = fs.readFileSync(path.join(gitPath, 'HEAD'), 'utf8').trim();
315
- const match = /^ref:\s*refs\/heads\/(.+)$/.exec(head);
316
- return match ? match[1] : null; // no match = detached HEAD → fall back
317
- }
318
- catch (err) {
319
- const error = (0, to_error_1.toError)(err);
320
- void error;
321
- return null;
322
- }
323
- }
324
- }
325
- exports.ReadStaleGuardRule = ReadStaleGuardRule;
326
- //# sourceMappingURL=read-stale-guard.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"read-stale-guard.js","sourceRoot":"","sources":["../../../../../../../packages/tooling/ai-hook-rules/src/core/rules/read-stale-guard.ts"],"names":[],"mappings":";;;;AAAA,iDAAyC;AACzC,+CAAyB;AACzB,mDAA6B;AAE7B,0DAA6H;AAC7H,sEAAiH;AAGjH,oCAA0C;AAC1C,4CAA4C;AAC5C,0CAAsC;AACtC,0CAAsC;AACtC,4DAA8D;AAC9D,4DAAqD;AACrD,kDAAwF;AACxF,oDAAuF;AACvF,sDAAkD;AACl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CAAC;YACpB,MAAM,KAAK,GAAG,IAAA,kBAAO,EAAC,GAAG,CAAC,CAAC;YAC3B,KAAK,KAAK,CAAC;YACX,OAAO,IAAI,CAAC;QAChB,CAAC;IACL,CAAC;CACJ;AAzQD,gDAyQC","sourcesContent":["import { execSync } from 'child_process';\nimport * as fs from 'fs';\nimport * as path from 'path';\n\nimport { BranchStateGuardConfig, BRANCH_STATE_GUARD_KEY, DEFAULT_MAX_COMMITS_BEHIND, Option } from '@webpieces/rules-config';\nimport { DEFAULT_HANG_TIMEOUT_MINUTES, readMainSyncStatus, MainSyncStatus } from '@webpieces/repo-workflow-core';\n\nimport type { FileContext, Violation } from '../types';\nimport { Violation as V } from '../types';\nimport { FileRuleBase } from '../rule-base';\nimport { FixHint } from '../fix-hint';\nimport { toError } from '../to-error';\nimport { triggerMainSyncRefresh } from '../main-sync-refresh';\nimport { hangTimeoutOf } from '../main-sync-timeout';\nimport { logGuardDecision, GuardDecision, Verdict, matrixL2Row } from '../decision-log';\nimport { writeBranchStateMatrixDoc, branchStateMatrixPointer } from '../l2-matrix-doc';\nimport { L0_FAULT_NONE } from '../l0-fault-codes';\nimport { TargetTreeResolver } from '../target-tree';\nimport { MergedBranchMessage } from './merged-branch-message';\nimport { JudgedTree } from './judged-tree';\nimport { StaleMainMessage } from './stale-main-message';\nimport { MainFreshness } from './main-freshness';\nimport { TreeRecovery } from './tree-recovery';\n\n/**\n * Blocks READS while the checked-out branch is a stale place to read from. TWO states:\n *\n * A. on `main`, and local main is farther BEHIND origin/main than the configured tolerance\n * B. on a feature branch whose PR is ALREADY MERGED (a pre-merge snapshot; origin/main has moved\n * past it and a squash merge means its HEAD is not even an ancestor of main)\n *\n * WHY READ, of all tools: either state means the AI reads stale FILE CONTENT and then reasons,\n * plans and writes against code that no longer exists upstream. Blocking the write is too late —\n * the bad premise is already in context. So the block lands on the read. (feature-branch-guard\n * blocks the WRITE in state B; this guard is the read-side half of that same protection, and the\n * two share one recovery message via MergedBranchMessage.)\n *\n * THERE IS NO DIRTY-TREE ASYMMETRY, and there is no dirty-tree valve in either state. Both used to\n * fail open on uncommitted work; both now block. The argument for the state-A valve was that its cure,\n * an in-place pull, is not a fast-forward on a dirty tree — true, but that is a fact about the\n * MESSAGE, which printed only the pull. Row 6 has always carried a second cure, `git checkout -b <new>\n * origin/main`, and that one CARRIES uncommitted changes onto the new branch, so the work comes with\n * you and nothing is trapped. StaleMainMessage now prints both, labelled with which survives a dirty\n * tree, so the block no longer has to be suppressed to keep the printed cure runnable. State B's valve\n * never had an argument at all — its cure was always the branch form.\n *\n * Residual, in both states: if `origin/main` changed the same files you edited, git refuses the switch.\n * `git stash` is on the L2 skip list and is never blocked, so the path out is stash → branch → pop.\n *\n * WHY THIS CANNOT WEDGE: the block is scoped to Read ONLY. Every cure — `pnpm wp-sync-main`,\n * `pnpm install`, any webpieces upgrade — is a Bash command, and this guard never looks at Bash.\n * So there is no command allowlist to maintain and no way to lock the agent out of its own fix.\n * (Every `wp-*` bin is on the L2 skip list, and the pull it wraps is explicitly permitted on main by\n * redirect-how-to-merge-main, which returns null when the branch IS main — the guards are\n * complementary, not stacked.)\n *\n * That scoping is also this guard's HOLE, and it is closed elsewhere rather than here: leaving Bash\n * entirely alone let a session `cat`/`grep`/`ls` the same stale tree the Read block was rejecting,\n * for a whole session, while the logs read \"read-stale-guard handled\". stale-main-bash-guard is the\n * State-A Bash counterpart (as merged-branch-bash-guard is State B's): in the SAME state this guard\n * blocks — `main`, KNOWN BEYOND the cached commit-distance tolerance below — it default-denies Bash and\n * allowlists only the commands that get you out, so the cure is never blocked and this guard can stay\n * simple and Read-only.\n *\n * Everything here is FAIL-OPEN on data we could not ESTABLISH. A guard that blocks reads on bad data\n * is far worse than one that misses; every unknown resolves to \"allow\". Note the dual, which is what\n * the deleted dirty valve violated: never fail open on data you DID establish. A dirty tree is not an\n * unknown — it is a known state with a known cure. The three deliberate escape valves:\n *\n * 1. CACHE LAG — we do NOT compare hashes for equality. The cached `originMain` is written by\n * the detached refresher and is arbitrarily old, so `local !== origin` stays\n * true for a while AFTER a successful pull, which would spin the agent forever.\n * Instead: is the cached origin/main an ANCESTOR of local main? If local main\n * already contains it, we are not behind. That flips the instant the pull lands,\n * with no refresher round-trip. This is the single most important line here.\n * 2. CONFIG READ — webpieces.config.json stays readable so the agent can always read-then-edit\n * it to set `mode: OFF`. Its EDIT is already bypassed in runner.ts + hook-core;\n * this closes the read half of that same escape hatch.\n * 3. NO DATA — no cache, cache for another branch, empty originMain (offline), or no local\n * main at all (fresh clone / worktree) → allow.\n *\n * Runs from the Read fast path in hook-core (Read is neither a file-edit nor a bash payload, so it\n * never reaches the runner's rule loop). Fires the detached refresher on every call, which is also\n * what makes reads keep the shared main-sync cache warm for feature-branch-guard.\n */\nexport class ReadStaleGuardRule extends FileRuleBase<BranchStateGuardConfig> {\n constructor(config: BranchStateGuardConfig) { super(config, 'read-stale-guard', BRANCH_STATE_GUARD_KEY); }\n\n // The ancestry test and the cache summary, shared with stale-main-bash-guard — see main-freshness.ts.\n private readonly freshness = new MainFreshness();\n\n readonly description = 'Block reads on a branch that is stale to read from — a `main` beyond its configured commit-distance tolerance, or a feature branch whose PR is already merged.';\n override readonly files = ['**/*'];\n override readonly defaultOptions = {\n hangTimeoutMinutes: DEFAULT_HANG_TIMEOUT_MINUTES,\n };\n readonly fixHint = new FixHint(\n 'This branch is stale to read from — reading it would give you pre-merge/out-of-date content.',\n 'Get onto current code before reading anything else:',\n [\n new Option('On main, beyond branch-state-guard.maxCommitsBehind → pnpm wp-sync-main (CLEAN TREE ONLY), or git checkout -b <new-branch> origin/main which works with UNCOMMITTED CHANGES and brings them along. On an already-merged branch → git fetch origin main && git checkout -b <new-branch> origin/main, which likewise carries your edits. Then retry the read.', true),\n new Option('If a checkout -b refuses because origin/main changed the same files you edited: git stash (never blocked), redo the checkout, then git stash pop.'),\n new Option(\"If pnpm wp-sync-main dies with 'fatal: Cannot fast-forward to multiple branches', .git/FETCH_HEAD holds a duplicate line — run 'git fetch --prune origin main' to rewrite it cleanly, then run it again.\"),\n new Option('Still allowed right now: reading webpieces.config.json, and the Bash commands that get you OUT or tell you where you are — git checkout -b <new> origin/main, git switch, git pull/fetch, git status|log|diff|show|branch, git stash, gh, curl/wget, every wp-* bin, installs. Everything ELSE through Bash is blocked in this same state (a main beyond maxCommitsBehind → stale-main-bash-guard; a merged branch → merged-branch-bash-guard), and Write/Edit on main is blocked by feature-branch-guard however current main is. There is no side door: get onto a branch off origin/main.'),\n new Option('Disable in webpieces.config.json under hookGuards → branch-state-guard (mode OFF) if intentional — that one key governs the Write, Read and Bash halves of this policy together.'),\n ],\n );\n\n /**\n * WHICH TREE is judged — the one that owns the FILE BEING READ, resolved by git through\n * TargetTreeResolver, never by `ctx.workspaceRoot`. Same fix and same reasoning as\n * feature-branch-guard's (issue #851): `ctx.workspaceRoot` is the walk-up from the SESSION's cwd, so\n * a main-session Read of a file inside an agent worktree was judged on the PRIMARY clone's branch —\n * and a `main` that the primary happened to be sitting on would close reads of another tree's\n * feature branch.\n */\n check(ctx: FileContext): readonly Violation[] {\n // Outside the workspace root — no jurisdiction.\n if (ctx.relativePath.startsWith('..')) return [];\n\n const target = new TargetTreeResolver().resolve(ctx.filePath, ctx.workspaceRoot);\n // A nested clone under `repositories/**` is out of scope, exactly as it is on the bash path.\n if (target.kind === 'foreign') return this.failOpen(ctx, null, 'target-tree-foreign');\n\n const judgedRoot = target.root;\n const branch = this.currentBranch(judgedRoot);\n if (branch === null) return this.failOpen(ctx, branch, 'branch-undeterminable');\n // Mid-rebase in the target tree: no branch name, so no key into the branch-keyed cache and\n // nothing to judge. Matrix row 14 — abstain, and say WHY rather than logging a cache miss.\n if (branch === 'HEAD') return this.failOpen(ctx, branch, 'detached-head');\n\n // Keep the shared cache warm for the next call. Detached; never blocks this read. Fired for\n // BOTH states — the merged-branch signal comes out of that same cache. Rooted at the JUDGED\n // tree so the entry refreshed is the one this guard reads.\n triggerMainSyncRefresh(judgedRoot, hangTimeoutOf(this.config));\n\n // Escape valve 3 — the read half of the config escape hatch. Ahead of BOTH states' blocks so\n // the agent can always read-then-edit the file that turns this guard off.\n if (this.isConfigFile(ctx.relativePath)) return this.allow(ctx, branch, 'webpieces-config-read (escape hatch)');\n\n const judged = new JudgedTree(target, branch, target.governedRoot);\n return branch === 'main'\n ? this.checkStaleMain(ctx, judged)\n : this.checkMergedBranch(ctx, judged);\n }\n\n // State A — on main, possibly behind origin/main.\n private checkStaleMain(ctx: FileContext, judged: JudgedTree): readonly Violation[] {\n const branch = judged.branch;\n const status = readMainSyncStatus(judged.root, 'main');\n if (status === null) return this.failOpen(ctx, branch, 'no-sync-cache', 'cache=none');\n\n const cache = this.cacheSummary(status);\n // BELT-AND-BRACES since the cache became branch-keyed: we asked for the 'main' entry by key, so\n // a mismatch means the map's key and the entry's own `branch` disagree — a shape bug. Kept so\n // that degrades to an allow. Unreachable in normal operation.\n if (status.branch !== 'main') return this.failOpen(ctx, branch, 'stale-cross-branch-cache', cache);\n // Offline / origin unresolvable, or no local main to compare against.\n if (status.originMain === '') return this.failOpen(ctx, branch, 'origin-main-unknown', cache);\n\n // Escape valve 2 — ancestry, NOT equality. See the class comment.\n if (this.freshness.containsOriginMain(judged.root, status.originMain)) {\n return this.allow(ctx, branch, 'local-main-contains-origin (up to date)', cache);\n }\n\n const maxCommitsBehind = this.config.maxCommitsBehind ?? DEFAULT_MAX_COMMITS_BEHIND;\n const withinThreshold = this.freshness.isWithinAllowedDrift(status, maxCommitsBehind);\n if (withinThreshold === null) return this.failOpen(ctx, branch, 'commit-distance-unknown', cache);\n if (withinThreshold) return this.allow(ctx, branch, 'within-max-commits-behind', cache);\n\n // NO DIRTY VALVE. It used to fail open here, on the argument that the prescribed in-place pull\n // is not a clean fast-forward on a dirty tree. That argument was about the MESSAGE, not the row:\n // row 6's cure cell has always offered `git checkout -b <new> origin/main` as an alternative,\n // and THAT works dirty — it carries uncommitted changes onto the new branch and lands you on\n // current code, which is the whole point. The message now leads with it when the tree is dirty\n // (StaleMainMessage.forReads), so the cure an agent reads is one it can actually run.\n // Residual, same as row 8: if origin/main touched the files you edited, git refuses the switch\n // — `git stash` is on the skip list and clears it. Two steps worst case, never a dead end.\n return this.block(\n ctx, branch, 'on-stale-main',\n this.staleMainMessage(judged, status.commitsBehind ?? 0), cache);\n }\n\n /**\n * State B — a feature branch whose PR is already merged. Reads a PRE-MERGE snapshot, so every\n * plan built from it is built on code origin/main has moved past.\n *\n * `branchAlreadyMerged` comes straight from the shared cache (the refresher's `gh pr list --state\n * merged`), so this path spawns nothing. No `gh` / offline → `mergedPr` is '' → not merged → allow,\n * which is the fail-open direction for free.\n *\n * NO DIRTY-TREE VALVE. `git checkout -b <new> origin/main` carries uncommitted changes onto the\n * fresh branch, so the work comes with you and there is nothing to rescue by reading. When it does\n * NOT (an overlapping change landed in main, so git refuses the switch), `git stash` is on the L2\n * skip list and is never blocked: stash → branch → pop. The valve that used to sit here was drift\n * from the documented design, not a decision — this docblock described the strict behaviour for\n * releases while the code failed open.\n */\n private checkMergedBranch(ctx: FileContext, judged: JudgedTree): readonly Violation[] {\n const branch = judged.branch;\n const status = readMainSyncStatus(judged.root, branch);\n if (status === null) return this.failOpen(ctx, branch, 'no-sync-cache', 'cache=none');\n\n const cache = this.cacheSummary(status);\n // BELT-AND-BRACES since the cache became branch-keyed: the entry was looked up BY `branch`, so\n // a mismatch is a shape bug rather than the old \"cache is for another branch\" state. Kept so\n // such a bug degrades to an allow. Unreachable in normal operation. (A branch the refresh has\n // not seen yet is the `status === null` case above — still fail-open.)\n if (status.branch !== branch) return this.failOpen(ctx, branch, 'stale-cross-branch-cache', cache);\n // NOT-MERGED, or NOT-ASKED? `branchAlreadyMerged: false` is produced both by \"this branch has\n // no merged PR\" and by \"the forge could not be reached\" (`gh` missing, unauthenticated,\n // rate-limited, offline). Same allow either way — never block on data you could not establish\n // — but the LOG must not call the second one an approval, or the trail cannot tell a policy\n // that is protecting something from one that is quietly standing down.\n if (!status.branchAlreadyMerged) {\n return status.forgeReachable\n ? this.allow(ctx, branch, 'clean-feature-branch', cache)\n : this.failOpen(ctx, branch, 'no-forge', cache);\n }\n // NO DIRTY VALVE — and this one never had an argument behind it at all. Row 8's cure is\n // `git fetch origin main && git checkout -b <new> origin/main`, which carries uncommitted work\n // with you, so a dirty tree traps nobody. The valve was code drift from the documented design;\n // read-stale-guard's own class comment said so while the code did the opposite.\n const pr = status.mergedPr !== '' ? status.mergedPr : '?';\n return this.block(\n ctx,\n branch,\n `already-merged PR#${pr}`,\n this.mergedMessage(judged, status.mergedPr),\n cache,\n );\n }\n\n // The merged-branch text, told in the flavour of the tree we are standing in: a linked worktree\n // is told to open a NEW worktree off origin/main and reap this dead one; the primary clone is\n // told to branch off origin/main. Neither is ever told to `git checkout main` (fatal in a\n // worktree). Detection is one statSync — see WorktreeService.isLinkedWorktree.\n private mergedMessage(judged: JudgedTree, mergedPr: string): string {\n const recovery = new TreeRecovery();\n const body = new MergedBranchMessage(judged.root).forReads(\n judged.branch, mergedPr, recovery.kindOf(judged.root), judged.root,\n );\n // NO redirect note here, deliberately — see feature-branch-guard.alreadyMergedMessage for the\n // argument. MergedBranchMessage already aims its commands at the judged root through `atRoot`\n // and prints the WORKTREE flavour of the cure when the judged tree is one.\n return [judged.header(), body].join('\\n');\n }\n\n\n private isConfigFile(relativePath: string): boolean {\n return relativePath === 'webpieces.config.json';\n }\n\n // StaleMainMessage's remaining consumer. It used to be shared with stale-main-bash-guard so the two\n // halves of the State-A block could never prescribe different cures; that guard now blocks on the\n // BRANCH (row 5) rather than on staleness and carries its own message, so this is the only caller.\n private staleMainMessage(judged: JudgedTree, commitsBehind: number): string {\n return [\n judged.header(),\n new StaleMainMessage(judged.root).forReads(String(commitsBehind)),\n judged.redirectNote([judged.pnpmCure('wp-sync-main')]),\n ].join('\\n');\n }\n\n private cacheSummary(status: MainSyncStatus): string {\n return this.freshness.summarize(status);\n }\n\n /**\n * The guard could not ESTABLISH the state it judges on, so it judged nothing.\n *\n * A sibling of allow() rather than a reason string passed to it, because the difference has to\n * reach the LOG as a value: `ALLOW_FAIL_OPEN` vs `ALLOW`. It was previously a `' (fail-open)'`\n * suffix on the free-text reason, which meant an abstention and a real approval were the same\n * verdict and the abstentions could not be counted — so nobody could tell whether these guards\n * were protecting anything or quietly standing down. Never block on data you could not\n * establish; but say out loud, in a field, that you did not establish it.\n */\n private failOpen(ctx: FileContext, branch: string | null, reason: string, cache: string = '-'): readonly Violation[] {\n this.logDecision(ctx, branch, 'ALLOW_FAIL_OPEN', reason, cache);\n return [];\n }\n\n private allow(ctx: FileContext, branch: string | null, reason: string, cache: string = '-'): readonly Violation[] {\n this.logDecision(ctx, branch, 'ALLOW', reason, cache);\n return [];\n }\n\n private block(ctx: FileContext, branch: string, reason: string, message: string, cache: string = '-'): readonly Violation[] {\n this.logDecision(ctx, branch, 'BLOCK_AI_CURE', reason, cache);\n // Deliver the matrix and name the row — see stale-main-bash-guard.block for why it is lazy.\n const pointer = branchStateMatrixPointer(writeBranchStateMatrixDoc(ctx.workspaceRoot), matrixL2Row(reason).row);\n return [new V(1, ctx.relativePath, message + pointer)];\n }\n\n private logDecision(ctx: FileContext, branch: string | null, verdict: Verdict, reason: string, cache: string): void {\n logGuardDecision(\n ctx.workspaceRoot,\n new GuardDecision('read-stale-guard', ctx.tool, ctx.relativePath, branch ?? 'unknown', verdict, reason, cache, L0_FAULT_NONE, matrixL2Row(reason)),\n );\n }\n\n /**\n * The current branch, WITHOUT spawning git on the common path.\n *\n * This runs on EVERY read, so it is the one call whose cost actually matters. Spawning\n * `git rev-parse --abbrev-ref HEAD` measures ~12ms — essentially all process-spawn overhead —\n * whereas `.git/HEAD` is a single tiny file whose read is microseconds. On a feature branch\n * (the overwhelmingly common case) that file read is the ONLY work this guard does before\n * short-circuiting, so reads stay effectively free.\n *\n * Falls back to spawning git whenever `.git/HEAD` cannot answer authoritatively:\n * - `.git` is a FILE, not a dir → we are in a worktree and HEAD lives elsewhere\n * - detached HEAD → the file holds a raw sha, not a `ref:` line\n * - anything unreadable/unexpected\n * The fallback is correct in all those cases; it is just slower, and they are rare.\n */\n private currentBranch(workspaceRoot: string): string | null {\n const fromHead = this.branchFromGitHead(workspaceRoot);\n if (fromHead !== null) return fromHead;\n // eslint-disable-next-line @webpieces/no-unmanaged-exceptions\n try {\n return execSync('git rev-parse --abbrev-ref HEAD', {\n cwd: workspaceRoot,\n encoding: 'utf8',\n stdio: ['pipe', 'pipe', 'pipe'],\n }).trim();\n } catch (err: unknown) {\n const error = toError(err);\n void error;\n return null;\n }\n }\n\n // Parse `.git/HEAD` (\"ref: refs/heads/<branch>\"). null = cannot answer, caller must fall back.\n private branchFromGitHead(workspaceRoot: string): string | null {\n // eslint-disable-next-line @webpieces/no-unmanaged-exceptions\n try {\n const gitPath = path.join(workspaceRoot, '.git');\n // A worktree/submodule has `.git` as a file pointing at the real gitdir — HEAD is not here.\n if (!fs.statSync(gitPath).isDirectory()) return null;\n const head = fs.readFileSync(path.join(gitPath, 'HEAD'), 'utf8').trim();\n const match = /^ref:\\s*refs\\/heads\\/(.+)$/.exec(head);\n return match ? match[1] : null; // no match = detached HEAD → fall back\n } catch (err: unknown) {\n const error = toError(err);\n void error;\n return null;\n }\n }\n}\n"]}
@@ -1,49 +0,0 @@
1
- import type { BashContext } from '../types';
2
- import { CommandScanner } from '../command-scan';
3
- export declare class RecoveryAllowlist {
4
- private readonly scanner;
5
- private readonly shell;
6
- constructor(scanner: CommandScanner);
7
- /**
8
- * Is EVERY segment of this command one that gets you out, or tells you where you are?
9
- *
10
- * An empty command is `false` — nothing to be sure about, and the default here is deny. (Note this
11
- * is one of the three places the two Bash guards genuinely differ; the stale-main blocklist allows
12
- * the empty command because its default is the other way round.)
13
- */
14
- isFullyRecovery(ctx: BashContext): boolean;
15
- private isRecoverySegment;
16
- /**
17
- * `gh` GENERALLY — it talks to GitHub, not to the working tree.
18
- *
19
- * This used to be an allowlist of read-only actions (`gh pr view|list|status|checks`, `gh run
20
- * view`), which fell over the moment an agent needed `gh pr close`, `gh pr comment` or `gh api`
21
- * from a parked session: those change something on GitHub and NOTHING in this tree, so the branch
22
- * state cannot be an argument against them. The exclusions are therefore the `gh` subcommands that
23
- * write LOCAL files — a clone, a checkout, a download — plus any segment carrying a `> file`
24
- * redirect.
25
- *
26
- * gh commands that are wrong for OTHER reasons stay wrong: `gh pr create`/`push` is governed by
27
- * pr-creation-or-push-guard and `gh pr merge` by pr-merge-guard. Those are separate policies and
28
- * this list was never what enforced them.
29
- *
30
- * YES, THIS ONE SUBLIST IS A BLOCKLIST, and that is the opposite polarity to the guard around it.
31
- * The argument against a blocklist elsewhere is that the hazardous set is UNBOUNDED — any program
32
- * can write a file as a side effect of doing something else, so no enumeration could ever be
33
- * complete. `gh`'s surface is not: it is one vendor's CLI, its verbs are documented, and writing
34
- * into the working tree is the rare exception rather than the ambient default. A new `gh`
35
- * subcommand that clones or downloads is a known, greppable maintenance point — `GH_LOCAL_FILE_WRITES`
36
- * — where "every future program that might write" is not.
37
- */
38
- private isGh;
39
- /**
40
- * `curl` / `wget` — a network fetch reads a URL, not this repo.
41
- *
42
- * Excluded: the forms that name a local FILE to write (`curl -o`, `curl -O`, `wget -O`, an
43
- * `--output-dir`/`-P`, or a `> file` redirect), because those are how a fetch becomes a write into
44
- * the tree. A bare `wget <url>` still drops its download into the cwd; that CREATES an untracked
45
- * file rather than modifying tracked content, which is the line this list draws everywhere else.
46
- */
47
- private isNetworkClient;
48
- private isPackageRecovery;
49
- }