@bookedsolid/rea 0.3.0 → 0.4.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 (56) hide show
  1. package/.husky/pre-push +15 -18
  2. package/README.md +41 -1
  3. package/dist/cli/doctor.d.ts +19 -4
  4. package/dist/cli/doctor.js +172 -5
  5. package/dist/cli/index.js +9 -1
  6. package/dist/cli/init.js +93 -7
  7. package/dist/cli/install/pre-push.d.ts +335 -0
  8. package/dist/cli/install/pre-push.js +2818 -0
  9. package/dist/cli/serve.d.ts +64 -0
  10. package/dist/cli/serve.js +270 -2
  11. package/dist/cli/status.d.ts +90 -0
  12. package/dist/cli/status.js +399 -0
  13. package/dist/cli/utils.d.ts +4 -0
  14. package/dist/cli/utils.js +4 -0
  15. package/dist/gateway/circuit-breaker.d.ts +17 -0
  16. package/dist/gateway/circuit-breaker.js +32 -3
  17. package/dist/gateway/downstream-pool.d.ts +2 -1
  18. package/dist/gateway/downstream-pool.js +2 -2
  19. package/dist/gateway/downstream.d.ts +39 -3
  20. package/dist/gateway/downstream.js +73 -14
  21. package/dist/gateway/log.d.ts +122 -0
  22. package/dist/gateway/log.js +334 -0
  23. package/dist/gateway/middleware/audit.d.ts +10 -1
  24. package/dist/gateway/middleware/audit.js +26 -1
  25. package/dist/gateway/middleware/blocked-paths.d.ts +0 -9
  26. package/dist/gateway/middleware/blocked-paths.js +439 -67
  27. package/dist/gateway/middleware/injection.d.ts +218 -13
  28. package/dist/gateway/middleware/injection.js +433 -51
  29. package/dist/gateway/middleware/kill-switch.d.ts +10 -1
  30. package/dist/gateway/middleware/kill-switch.js +20 -1
  31. package/dist/gateway/observability/metrics.d.ts +125 -0
  32. package/dist/gateway/observability/metrics.js +321 -0
  33. package/dist/gateway/server.d.ts +19 -0
  34. package/dist/gateway/server.js +99 -15
  35. package/dist/policy/loader.d.ts +13 -0
  36. package/dist/policy/loader.js +28 -0
  37. package/dist/policy/profiles.d.ts +13 -0
  38. package/dist/policy/profiles.js +12 -0
  39. package/dist/policy/types.d.ts +28 -0
  40. package/dist/registry/fingerprint.d.ts +73 -0
  41. package/dist/registry/fingerprint.js +81 -0
  42. package/dist/registry/fingerprints-store.d.ts +62 -0
  43. package/dist/registry/fingerprints-store.js +111 -0
  44. package/dist/registry/interpolate.d.ts +58 -0
  45. package/dist/registry/interpolate.js +121 -0
  46. package/dist/registry/loader.d.ts +2 -2
  47. package/dist/registry/loader.js +22 -1
  48. package/dist/registry/tofu-gate.d.ts +41 -0
  49. package/dist/registry/tofu-gate.js +189 -0
  50. package/dist/registry/tofu.d.ts +111 -0
  51. package/dist/registry/tofu.js +173 -0
  52. package/dist/registry/types.d.ts +9 -1
  53. package/package.json +1 -1
  54. package/profiles/bst-internal-no-codex.yaml +5 -0
  55. package/profiles/bst-internal.yaml +7 -0
  56. package/scripts/tarball-smoke.sh +197 -0
@@ -0,0 +1,335 @@
1
+ /**
2
+ * G6 — Pre-push hook fallback installer.
3
+ *
4
+ * Ships alongside `commit-msg.ts` as a second-line defender for the
5
+ * protected-path Codex audit gate. The primary path is `.husky/pre-push`,
6
+ * which rea copies into the consumer's `.husky/` via the canonical copy
7
+ * module. That file only runs when the consumer has husky active
8
+ * (`core.hooksPath` points at `.husky/`). Consumers who have never run
9
+ * `husky install`, or who have disabled husky entirely, would otherwise get
10
+ * ZERO pre-push enforcement — and the protected-path gate is exactly the
11
+ * thing we cannot let silently lapse.
12
+ *
13
+ * The fallback writes a small shell script that `exec`s the same
14
+ * `push-review-gate.sh` logic the Claude Code hook already runs. The gate
15
+ * itself is shared — we do NOT duplicate its 700 lines.
16
+ *
17
+ * ## Install policy (decision tree, documented)
18
+ *
19
+ * Given a consumer repo, we must decide where (if anywhere) to install a
20
+ * fallback `pre-push`:
21
+ *
22
+ * 1. `core.hooksPath` unset (vanilla git):
23
+ * → Install `.git/hooks/pre-push`. This is the only path git will fire.
24
+ * `.husky/pre-push` sits on disk as a source-of-truth copy but is not
25
+ * consulted by git directly.
26
+ *
27
+ * 2. `core.hooksPath` set to a directory containing an EXECUTABLE,
28
+ * governance-carrying `pre-push`:
29
+ * → Do NOT install. A hook is "governance-carrying" when it either
30
+ * carries our `FALLBACK_MARKER` (rea-managed) or execs / invokes
31
+ * `.claude/hooks/push-review-gate.sh` (consumer-wired delegation).
32
+ * This is the happy path for any project running husky 9+ that has
33
+ * wired the gate.
34
+ *
35
+ * 3. `core.hooksPath` set to a directory with a pre-push that is NOT
36
+ * governance-carrying (wrong bits, unrelated script, lint-only husky
37
+ * hook, directory, etc.):
38
+ * → Classify as foreign. Leave it alone, warn the user, and let
39
+ * `rea doctor` downgrade the check to `warn` so the gap is visible.
40
+ *
41
+ * 4. `core.hooksPath` set to a directory WITHOUT a pre-push:
42
+ * → Install into the configured hooksPath (as `pre-push`). This is the
43
+ * "hooksPath is set but nothing lives there yet" case. The active
44
+ * hook directory has changed; we install where git will actually look.
45
+ *
46
+ * Idempotency: every install writes a stable managed header
47
+ * (`# rea:pre-push-fallback v1`). Re-running `rea init` detects the header
48
+ * by ANCHORED match (exact second line after the shebang) and refreshes in
49
+ * place; it NEVER overwrites a hook without our marker — if the consumer
50
+ * has their own pre-push already, we warn and skip. Substring matches are
51
+ * deliberately rejected: a consumer comment, a grep log, or copy-pasted
52
+ * snippet containing the sentinel must not reclassify a foreign file as
53
+ * rea-managed.
54
+ *
55
+ * ## Why not just rely on `.husky/pre-push`?
56
+ *
57
+ * Three concrete failure modes we saw during 0.2.x dogfooding:
58
+ * - Consumer hasn't run `husky install` (fresh clone, pnpm hasn't run
59
+ * postinstall yet, etc.). `.husky/pre-push` exists but git's hooksPath
60
+ * still points at `.git/hooks/`. No enforcement.
61
+ * - Consumer deliberately uses `core.hooksPath=./custom-hooks` with a
62
+ * different tool. `.husky/pre-push` is dead weight.
63
+ * - CI or release automation disables husky via `HUSKY=0`. Again, no
64
+ * enforcement at push time.
65
+ *
66
+ * The protected-path Codex audit requirement is too important to let any
67
+ * of those slip through silently. See THREAT_MODEL.md §Governance for the
68
+ * full rationale.
69
+ */
70
+ /**
71
+ * Marker baked into every rea-installed fallback pre-push hook. Used for
72
+ * idempotency: on re-run we refresh files carrying the marker and refuse
73
+ * to touch anything that doesn't.
74
+ *
75
+ * Bump the version suffix whenever the embedded script semantics change so
76
+ * upgrades can migrate old installs. Comparison is NOT a substring match —
77
+ * see `isReaManagedFallback` for the anchored form required to classify
78
+ * a file as rea-managed.
79
+ */
80
+ export declare const FALLBACK_MARKER = "# rea:pre-push-fallback v1";
81
+ /**
82
+ * Marker present in the shipped `.husky/pre-push` governance gate. Detection
83
+ * requires the marker to appear on the SECOND LINE of the file (immediately
84
+ * after the shebang) to prevent a consumer comment or copy-pasted snippet
85
+ * that mentions the string from causing a foreign hook to be misclassified
86
+ * as rea-managed and then silently overwritten. See `isReaManagedHuskyGate`
87
+ * for the anchored check.
88
+ */
89
+ export declare const HUSKY_GATE_MARKER = "# rea:husky-pre-push-gate v1";
90
+ /**
91
+ * Second versioned marker embedded in the body of the shipped `.husky/pre-push`.
92
+ * Required alongside `HUSKY_GATE_MARKER` so that a hook containing only the
93
+ * header marker + `exit 0` (or any stub body) is not classified as rea-managed.
94
+ * A genuine rea Husky gate always carries both. The marker is versioned so it
95
+ * can be bumped if the gate implementation changes significantly.
96
+ */
97
+ export declare const HUSKY_GATE_BODY_MARKER = "# rea:gate-body-v1";
98
+ /**
99
+ * True when `content` starts with the exact rea fallback prelude. The
100
+ * marker must appear as the second line, immediately after the shebang,
101
+ * with no leading whitespace, no alternate shebang (`#!/usr/bin/env sh`),
102
+ * and no interposed blank lines. Anything else is foreign.
103
+ *
104
+ * Rejecting a substring match is what stops a consumer comment like
105
+ * `# Hint: the old rea:pre-push-fallback v1 marker moved into .husky/` from
106
+ * accidentally classifying a user's own hook as rea-managed and then
107
+ * getting overwritten on the next `rea init`.
108
+ */
109
+ export declare function isReaManagedFallback(content: string): boolean;
110
+ /**
111
+ * True when `content` has the shipped Husky gate marker on the SECOND LINE
112
+ * (immediately after the shebang). This is the canonical structure of the
113
+ * rea-authored `.husky/pre-push` — the shebang occupies line 1 and the marker
114
+ * occupies line 2 with no intervening blank lines.
115
+ *
116
+ * Requiring line-2 placement prevents a consumer comment, copy-pasted snippet,
117
+ * or any other text that merely *mentions* the marker string from reclassifying
118
+ * a consumer-owned hook as rea-managed and triggering an overwrite on the next
119
+ * `rea init`. A marker buried anywhere else in the file is not the canonical
120
+ * structure and must not be trusted.
121
+ *
122
+ * This classification is checked BEFORE `isReaManagedFallback` in
123
+ * `classifyExistingHook` so that the shipped `.husky/pre-push` is recognized
124
+ * as a governance-carrying hook rather than `foreign/no-marker`.
125
+ */
126
+ export declare function isReaManagedHuskyGate(content: string): boolean;
127
+ /**
128
+ * Pre-0.4 rea-authored `.husky/pre-push` shape — same governance behavior
129
+ * as the current gate but lacks the line-2/3 versioned markers
130
+ * (`# rea:husky-pre-push-gate v1` / `# rea:gate-body-v1`) introduced in
131
+ * 0.4.
132
+ *
133
+ * Codex R21 F1: without this detector, any consumer upgrading from a rea
134
+ * release that shipped the pre-marker hook fell into `foreign/no-marker`.
135
+ * `classifyPrePushInstall` mapped that to `skip/foreign-pre-push` and
136
+ * `rea init` refused to touch the file. `rea doctor` reported
137
+ * `activeForeign=true`. Users had no self-heal path short of manually
138
+ * deleting the hook — which is a bad migration story for a governance
139
+ * primitive that they are supposed to trust.
140
+ *
141
+ * Shape-level detection:
142
+ * 1. Line 2 is the canonical pre-0.4 filename header
143
+ * `# .husky/pre-push — rea governance gate for terminal-initiated pushes.`
144
+ * This header shipped verbatim across the 0.2.x/0.3.x rea releases.
145
+ * 2. Real governance still present — `hasHaltEnforcement(content)` AND
146
+ * `hasAuditCheck(content)` both pass. A stub that only matches the
147
+ * header comment (no enforcement) fails the shape check and stays
148
+ * classified as foreign.
149
+ *
150
+ * Classification consequence: `classifyExistingHook` returns
151
+ * `rea-managed-husky` for legacy matches. `classifyPrePushInstall` maps
152
+ * that to `skip/active-pre-push-present` — `rea init` does not touch the
153
+ * hook (correctness: the file IS still functional governance), but
154
+ * `inspectPrePushState` reports `ok=true, activeForeign=false` so doctor
155
+ * stops flagging it. The canonical-manifest-driven upgrade path
156
+ * (`rea upgrade`) detects the hash mismatch against the packaged
157
+ * `.husky/pre-push` and surfaces the legacy shape as drift, letting the
158
+ * operator opt into the refresh explicitly.
159
+ */
160
+ export declare function isLegacyReaManagedHuskyGate(content: string): boolean;
161
+ /**
162
+ * True when `content` contains a REAL shell invocation of
163
+ * `push-review-gate.sh`. Used as a softer signal that a consumer-owned
164
+ * pre-push still wires the shared gate (e.g. a husky 9 file that runs
165
+ * lint AND execs the gate). Combined with "exists AND executable", a
166
+ * gate-referencing foreign hook is a legitimate integration point —
167
+ * doctor reports `pass`, install skips.
168
+ *
169
+ * Accepts (positive-match allowlist):
170
+ * - Bare invocation: `.claude/hooks/push-review-gate.sh "$@"`
171
+ * - POSIX exec keyword: `exec`, `.`, `sh`, `bash`, `zsh` followed by the
172
+ * gate path. The bash-only `source` keyword is NOT accepted — the POSIX
173
+ * equivalent `.` (dot) is.
174
+ * - Quoted/expanded path prefix: `exec "$REA_ROOT"/.claude/hooks/push-review-gate.sh "$@"`
175
+ * — double- or single-quoted variable expansions before the literal path
176
+ * are treated as part of the path, not as a mention context.
177
+ * - Trailing `;` after `exec <gate>`: `exec gate.sh "$@";` — exec replaces
178
+ * the shell, so the `;` and anything after it never runs; gate exit IS
179
+ * the hook's exit status.
180
+ * - Variable indirection: `GATE=<path-containing-gate>` on one line plus
181
+ * `exec "$GATE"` / `. "$GATE"` / etc. on a later line.
182
+ *
183
+ * Rejects:
184
+ * - Comment lines starting with `#`
185
+ * - Shell tests: `[ -x .claude/hooks/push-review-gate.sh ]`
186
+ * - File tests: `test -f .claude/hooks/push-review-gate.sh`
187
+ * - Chmod / cp / mv / cat / printf / echo mentioning the path
188
+ * - String literals inside quoted arguments to non-invocation commands
189
+ * - Invocations inside `if`/`for`/`while`/`case` blocks (conditional —
190
+ * not guaranteed to run)
191
+ * - Invocations after an unconditional top-level `exit`
192
+ * - Non-`exec` invocations followed by `||`, `&&`, `;`, or trailing `&`
193
+ * (status-swallowing operators)
194
+ *
195
+ * This is a pragmatic heuristic, not a full shell parser. R12 F2 broadened
196
+ * the allowlist to match the forms Codex flagged as valid but previously
197
+ * rejected; narrower patterns silently hard-failed `rea doctor` on
198
+ * correctly-governed consumer repos.
199
+ */
200
+ export declare function referencesReviewGate(content: string): boolean;
201
+ /**
202
+ * Resolve a configured `core.hooksPath` (possibly relative) to an absolute
203
+ * path relative to `targetDir`, or `null` if the key is unset.
204
+ */
205
+ export declare function resolveHooksDir(targetDir: string): Promise<{
206
+ dir: string | null;
207
+ configured: boolean;
208
+ }>;
209
+ export type InstallDecision =
210
+ /** Active pre-push already present and governance-carrying. */
211
+ {
212
+ action: 'skip';
213
+ reason: 'active-pre-push-present';
214
+ hookPath: string;
215
+ }
216
+ /** Consumer owns a non-rea pre-push; refusing to stomp it. */
217
+ | {
218
+ action: 'skip';
219
+ reason: 'foreign-pre-push';
220
+ hookPath: string;
221
+ }
222
+ /** Write a fresh hook. */
223
+ | {
224
+ action: 'install';
225
+ hookPath: string;
226
+ }
227
+ /** Refresh an existing rea-managed hook (marker match). */
228
+ | {
229
+ action: 'refresh';
230
+ hookPath: string;
231
+ };
232
+ /**
233
+ * Classify what we should do at `targetDir` based on current state. Pure —
234
+ * reads the filesystem and git config but performs no writes. Split out so
235
+ * tests can drive every branch without going through the write path.
236
+ *
237
+ * NOTE: The result is a snapshot. `installPrePushFallback` re-resolves and
238
+ * re-classifies immediately before writing to defend against a husky
239
+ * install or concurrent `rea init` running between classify and write.
240
+ */
241
+ export declare function classifyPrePushInstall(targetDir: string): Promise<InstallDecision>;
242
+ export interface PrePushInstallResult {
243
+ decision: InstallDecision;
244
+ /** Absolute path of the file written, if any. */
245
+ written?: string;
246
+ /** User-facing warnings accumulated during install. */
247
+ warnings: string[];
248
+ }
249
+ export interface WriteExecutableResult {
250
+ /**
251
+ * R25 F2 — set to true when the install path had to use a non-atomic
252
+ * fallback (copyFile after link() refused). Callers surface this as a
253
+ * warning to the operator so they know publication was best-effort on
254
+ * this filesystem rather than atomic.
255
+ */
256
+ degradedFromAtomic: boolean;
257
+ }
258
+ /**
259
+ * Options controlling `installPrePushFallback`. Exposed primarily for
260
+ * tests — production callers get sensible defaults.
261
+ */
262
+ export interface InstallPrePushOptions {
263
+ /**
264
+ * Serialize concurrent installs via an advisory lockfile under `.git/`.
265
+ * Defaults to `true`. Tests that simulate concurrent races must keep
266
+ * this on; the only reason to turn it off is unit-testing a specific
267
+ * write branch in isolation.
268
+ */
269
+ useLock?: boolean;
270
+ /**
271
+ * Called exactly once inside the advisory lock, after classification
272
+ * and before re-resolution + write. Test-only seam that lets a race
273
+ * partner drop a file in between those two steps so we can assert on
274
+ * the re-check behavior. Invoked with the classified target path.
275
+ * Production callers never set this.
276
+ */
277
+ onBeforeReresolve?: (hookPath: string) => Promise<void> | void;
278
+ /**
279
+ * Called inside the lock, after the safety re-check passes but
280
+ * immediately before `writeExecutable`. Test-only seam: creates a
281
+ * file at the hook path to exercise the EEXIST-from-link path that
282
+ * guards the remaining TOCTOU window. Production callers never set this.
283
+ */
284
+ onBeforeWrite?: (hookPath: string) => Promise<void> | void;
285
+ }
286
+ /**
287
+ * Install (or refresh, or skip) the fallback pre-push hook at `targetDir`.
288
+ * Idempotent: safe to call on every `rea init`, including re-runs over an
289
+ * existing install. Never overwrites a foreign hook.
290
+ *
291
+ * Requires `targetDir/.git` to exist. Non-git directories are skipped with
292
+ * a warning — same shape as `installCommitMsgHook`.
293
+ */
294
+ export declare function installPrePushFallback(targetDir: string, options?: InstallPrePushOptions): Promise<PrePushInstallResult>;
295
+ /**
296
+ * Doctor check: at least one pre-push hook (Husky OR git fallback OR the
297
+ * configured hooksPath location) must exist AND be executable AND carry
298
+ * governance (rea marker or gate delegation). Returns a small record the
299
+ * doctor module can turn into a CheckResult.
300
+ *
301
+ * "Executable" is defined as having any of the user/group/other exec bits
302
+ * set, matching the existing `checkHooksInstalled` convention. A file that
303
+ * is executable but does not wire the Codex review gate is intentionally
304
+ * classified as non-governing: `ok=false` + `activeForeign=true`, which
305
+ * doctor turns into a `warn`, not a `pass`.
306
+ */
307
+ export interface PrePushDoctorState {
308
+ /** Every candidate path we consulted, with its live status on disk. */
309
+ candidates: Array<{
310
+ path: string;
311
+ exists: boolean;
312
+ executable: boolean;
313
+ /** `true` when the file content carries our anchored rea prelude. */
314
+ reaManaged: boolean;
315
+ /** `true` when the body references the shared review gate. */
316
+ delegatesToGate: boolean;
317
+ }>;
318
+ /**
319
+ * The candidate path git would actually fire right now, given current
320
+ * `core.hooksPath`. May or may not exist.
321
+ */
322
+ activePath: string;
323
+ /**
324
+ * True when the active candidate exists, is executable, AND carries
325
+ * governance (rea marker OR references the review gate).
326
+ */
327
+ ok: boolean;
328
+ /**
329
+ * True when the active candidate exists + is executable but does NOT
330
+ * carry governance. This is the "silent bypass" case doctor surfaces as
331
+ * a warn. Distinct from `ok=false + absent` (which is a hard fail).
332
+ */
333
+ activeForeign: boolean;
334
+ }
335
+ export declare function inspectPrePushState(targetDir: string): Promise<PrePushDoctorState>;