@bookedsolid/rea 0.10.1 → 0.10.3
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.
- package/dist/hooks/review-gate/args.d.ts +126 -0
- package/dist/hooks/review-gate/args.js +315 -0
- package/dist/hooks/review-gate/audit.d.ts +131 -0
- package/dist/hooks/review-gate/audit.js +181 -0
- package/dist/hooks/review-gate/banner.d.ts +97 -0
- package/dist/hooks/review-gate/banner.js +172 -0
- package/dist/hooks/review-gate/base-resolve.d.ts +155 -0
- package/dist/hooks/review-gate/base-resolve.js +247 -0
- package/dist/hooks/review-gate/cache-key.d.ts +55 -0
- package/dist/hooks/review-gate/cache-key.js +41 -0
- package/dist/hooks/review-gate/cache.d.ts +108 -0
- package/dist/hooks/review-gate/cache.js +120 -0
- package/dist/hooks/review-gate/constants.d.ts +26 -0
- package/dist/hooks/review-gate/constants.js +34 -0
- package/dist/hooks/review-gate/diff.d.ts +181 -0
- package/dist/hooks/review-gate/diff.js +232 -0
- package/dist/hooks/review-gate/errors.d.ts +72 -0
- package/dist/hooks/review-gate/errors.js +100 -0
- package/dist/hooks/review-gate/hash.d.ts +43 -0
- package/dist/hooks/review-gate/hash.js +46 -0
- package/dist/hooks/review-gate/index.d.ts +31 -0
- package/dist/hooks/review-gate/index.js +35 -0
- package/dist/hooks/review-gate/metadata.d.ts +98 -0
- package/dist/hooks/review-gate/metadata.js +158 -0
- package/dist/hooks/review-gate/policy.d.ts +55 -0
- package/dist/hooks/review-gate/policy.js +71 -0
- package/dist/hooks/review-gate/protected-paths.d.ts +46 -0
- package/dist/hooks/review-gate/protected-paths.js +76 -0
- package/package.json +1 -1
|
@@ -0,0 +1,181 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Audit-record emission + consumption for the review gate.
|
|
3
|
+
*
|
|
4
|
+
* ## Responsibilities
|
|
5
|
+
*
|
|
6
|
+
* 1. Emit `push.review.skipped` and `codex.review.skipped` records via the
|
|
7
|
+
* existing `appendAuditRecord()` helper. These are NEVER forgeable-
|
|
8
|
+
* verdict records (the push-review gate never consults them as Codex
|
|
9
|
+
* receipts), so they intentionally go through the `"other"`-stamped
|
|
10
|
+
* public path rather than the `"rea-cli"` dedicated writer.
|
|
11
|
+
*
|
|
12
|
+
* 2. Scan `.rea/audit.jsonl` for a qualifying `codex.review` receipt
|
|
13
|
+
* certifying a given `head_sha`. This is the TS equivalent of the
|
|
14
|
+
* bash core's `jq -R 'fromjson? | select(...)'` predicate
|
|
15
|
+
* (push-review-core.sh §959-966).
|
|
16
|
+
*
|
|
17
|
+
* ## Defect carry-forwards
|
|
18
|
+
*
|
|
19
|
+
* - **Defect P** (forgery rejection). The scan filter requires
|
|
20
|
+
* `emission_source ∈ {"rea-cli", "codex-cli"}`. The public
|
|
21
|
+
* `appendAuditRecord()` helper stamps `"other"`; only the dedicated
|
|
22
|
+
* `appendCodexReviewAuditRecord()` helper and the Codex CLI write
|
|
23
|
+
* `"rea-cli"` / `"codex-cli"`. Records with `emission_source: "other"`
|
|
24
|
+
* or missing the field entirely are rejected here.
|
|
25
|
+
*
|
|
26
|
+
* - **Defect U** (streaming-parse tolerance). Every line in `.rea/
|
|
27
|
+
* audit.jsonl` is parsed independently in a try/catch. A single
|
|
28
|
+
* corrupt line mid-file does NOT abort the scan — later lines still
|
|
29
|
+
* get a chance. Before 0.10.2 the bash `jq -e` scan would bail on the
|
|
30
|
+
* first unparseable line and miss every subsequent legitimate record.
|
|
31
|
+
*
|
|
32
|
+
* - **Verdict whitelist**. Only `verdict ∈ {"pass", "concerns"}` records
|
|
33
|
+
* satisfy the protected-path gate. `blocking` and `error` verdicts are
|
|
34
|
+
* receipts that a review HAPPENED but with a negative outcome, which
|
|
35
|
+
* does NOT unblock the push. Mirrors push-review-core.sh §964.
|
|
36
|
+
*/
|
|
37
|
+
import fs from 'node:fs/promises';
|
|
38
|
+
import path from 'node:path';
|
|
39
|
+
import { appendAuditRecord, InvocationStatus, Tier, } from '../../audit/append.js';
|
|
40
|
+
import { collectOsIdentity } from './metadata.js';
|
|
41
|
+
/** Tool-names the gate emits. Kept as constants so string-literal drift is caught at compile time. */
|
|
42
|
+
export const PUSH_REVIEW_SKIPPED_TOOL = 'push.review.skipped';
|
|
43
|
+
export const CODEX_REVIEW_SKIPPED_TOOL = 'codex.review.skipped';
|
|
44
|
+
export const PUSH_REVIEW_CACHE_HIT_TOOL = 'push.review.cache.hit';
|
|
45
|
+
export const PUSH_REVIEW_CACHE_ERROR_TOOL = 'push.review.cache.error';
|
|
46
|
+
/** Server-names for the emit paths — carry forward from bash §473/§639. */
|
|
47
|
+
export const ESCAPE_HATCH_SERVER = 'rea.escape_hatch';
|
|
48
|
+
export const PUSH_REVIEW_SERVER = 'rea.push_review';
|
|
49
|
+
/**
|
|
50
|
+
* Emit the `push.review.skipped` audit record. Wraps the public
|
|
51
|
+
* `appendAuditRecord()` helper — emission_source lands as `"other"`.
|
|
52
|
+
*
|
|
53
|
+
* The skipped record is intentionally NOT a `codex.review` receipt: the
|
|
54
|
+
* push-review cache-gate scan rejects any record whose `tool_name` is not
|
|
55
|
+
* `codex.review` AND any record whose `emission_source` is not
|
|
56
|
+
* `rea-cli` / `codex-cli`. So this record is on the hash chain as
|
|
57
|
+
* forensic evidence but cannot be confused with a real Codex review.
|
|
58
|
+
*/
|
|
59
|
+
export async function emitPushReviewSkipped(input) {
|
|
60
|
+
const osIdentity = input.os_identity ?? collectOsIdentity();
|
|
61
|
+
const metadata = {
|
|
62
|
+
head_sha: input.head_sha,
|
|
63
|
+
branch: input.branch,
|
|
64
|
+
reason: input.reason,
|
|
65
|
+
actor: input.actor,
|
|
66
|
+
verdict: 'skipped',
|
|
67
|
+
os_identity: osIdentity,
|
|
68
|
+
};
|
|
69
|
+
const record = {
|
|
70
|
+
tool_name: PUSH_REVIEW_SKIPPED_TOOL,
|
|
71
|
+
server_name: ESCAPE_HATCH_SERVER,
|
|
72
|
+
status: InvocationStatus.Allowed,
|
|
73
|
+
tier: Tier.Read,
|
|
74
|
+
metadata,
|
|
75
|
+
};
|
|
76
|
+
return appendAuditRecord(input.baseDir, record);
|
|
77
|
+
}
|
|
78
|
+
export async function emitCodexReviewSkipped(input) {
|
|
79
|
+
const metadata = {
|
|
80
|
+
head_sha: input.head_sha,
|
|
81
|
+
target: input.target,
|
|
82
|
+
reason: input.reason,
|
|
83
|
+
actor: input.actor,
|
|
84
|
+
verdict: 'skipped',
|
|
85
|
+
files_changed: null,
|
|
86
|
+
metadata_source: input.metadata_source,
|
|
87
|
+
};
|
|
88
|
+
const record = {
|
|
89
|
+
tool_name: CODEX_REVIEW_SKIPPED_TOOL,
|
|
90
|
+
server_name: ESCAPE_HATCH_SERVER,
|
|
91
|
+
status: InvocationStatus.Allowed,
|
|
92
|
+
tier: Tier.Read,
|
|
93
|
+
metadata,
|
|
94
|
+
};
|
|
95
|
+
return appendAuditRecord(input.baseDir, record);
|
|
96
|
+
}
|
|
97
|
+
/** Verdicts that satisfy the protected-path Codex-receipt gate. */
|
|
98
|
+
const ACCEPTABLE_VERDICTS = new Set(['pass', 'concerns']);
|
|
99
|
+
/** Emission sources that satisfy the protected-path Codex-receipt gate. */
|
|
100
|
+
const ACCEPTABLE_SOURCES = new Set(['rea-cli', 'codex-cli']);
|
|
101
|
+
/**
|
|
102
|
+
* Predicate: does this parsed JSON object qualify as a valid
|
|
103
|
+
* `codex.review` receipt for the given `head_sha`?
|
|
104
|
+
*
|
|
105
|
+
* Exported for unit tests; callers should usually use
|
|
106
|
+
* `hasValidCodexReview()` below.
|
|
107
|
+
*/
|
|
108
|
+
export function isQualifyingCodexReview(record, head_sha) {
|
|
109
|
+
if (record === null || typeof record !== 'object')
|
|
110
|
+
return false;
|
|
111
|
+
const r = record;
|
|
112
|
+
if (r.tool_name !== 'codex.review')
|
|
113
|
+
return false;
|
|
114
|
+
if (typeof r.emission_source !== 'string' || !ACCEPTABLE_SOURCES.has(r.emission_source)) {
|
|
115
|
+
return false;
|
|
116
|
+
}
|
|
117
|
+
const md = r.metadata;
|
|
118
|
+
if (md === null || md === undefined || typeof md !== 'object')
|
|
119
|
+
return false;
|
|
120
|
+
if (md.head_sha !== head_sha)
|
|
121
|
+
return false;
|
|
122
|
+
if (typeof md.verdict !== 'string' || !ACCEPTABLE_VERDICTS.has(md.verdict)) {
|
|
123
|
+
return false;
|
|
124
|
+
}
|
|
125
|
+
return true;
|
|
126
|
+
}
|
|
127
|
+
/**
|
|
128
|
+
* Scan `.rea/audit.jsonl` for a qualifying `codex.review` record matching
|
|
129
|
+
* the given `head_sha`. Returns true as soon as one is found.
|
|
130
|
+
*
|
|
131
|
+
* ## Defect U tolerance
|
|
132
|
+
*
|
|
133
|
+
* Each line is parsed independently via `JSON.parse` inside try/catch. A
|
|
134
|
+
* malformed line logs nothing and the scan continues. The bash fix in
|
|
135
|
+
* 0.10.2 was `jq -R 'fromjson?'`; we mirror the per-line behavior in
|
|
136
|
+
* native JS.
|
|
137
|
+
*
|
|
138
|
+
* ## Path safety
|
|
139
|
+
*
|
|
140
|
+
* The audit file is always `<baseDir>/.rea/audit.jsonl` — baseDir flows
|
|
141
|
+
* in from the caller and is the same resolved path used everywhere else.
|
|
142
|
+
* No caller-supplied path segments.
|
|
143
|
+
*
|
|
144
|
+
* ## Missing file
|
|
145
|
+
*
|
|
146
|
+
* ENOENT resolves to `false` (no receipt exists yet). Any other error
|
|
147
|
+
* propagates — the caller's policy is to fail-closed, and a permission
|
|
148
|
+
* error on the audit file is a distinct operational concern the caller
|
|
149
|
+
* should surface rather than silently mask as "no receipt".
|
|
150
|
+
*/
|
|
151
|
+
export async function hasValidCodexReview(baseDir, head_sha) {
|
|
152
|
+
const auditFile = path.join(baseDir, '.rea', 'audit.jsonl');
|
|
153
|
+
let raw;
|
|
154
|
+
try {
|
|
155
|
+
raw = await fs.readFile(auditFile, 'utf8');
|
|
156
|
+
}
|
|
157
|
+
catch (err) {
|
|
158
|
+
if (err.code === 'ENOENT')
|
|
159
|
+
return false;
|
|
160
|
+
throw err;
|
|
161
|
+
}
|
|
162
|
+
if (raw.length === 0)
|
|
163
|
+
return false;
|
|
164
|
+
// Walk lines. Each line is independently parsed; a corrupt line is
|
|
165
|
+
// silently skipped. A matching record short-circuits the scan.
|
|
166
|
+
for (const line of raw.split('\n')) {
|
|
167
|
+
if (line.length === 0)
|
|
168
|
+
continue;
|
|
169
|
+
let parsed;
|
|
170
|
+
try {
|
|
171
|
+
parsed = JSON.parse(line);
|
|
172
|
+
}
|
|
173
|
+
catch {
|
|
174
|
+
// Defect U tolerance — move on.
|
|
175
|
+
continue;
|
|
176
|
+
}
|
|
177
|
+
if (isQualifyingCodexReview(parsed, head_sha))
|
|
178
|
+
return true;
|
|
179
|
+
}
|
|
180
|
+
return false;
|
|
181
|
+
}
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Operator-facing banner composition.
|
|
3
|
+
*
|
|
4
|
+
* The bash core builds its banners via `printf` inside a `{ ... } >&2`
|
|
5
|
+
* block, counting diff lines with `grep -cE ...` and file changes with
|
|
6
|
+
* `grep -c '^+++ '`. Defect K (rea#62) surfaced because `grep -c`
|
|
7
|
+
* emits `0` to stdout AND exits non-zero on no-match, and the bash author
|
|
8
|
+
* wrote `$(grep -c ... || echo 0)` — which emitted `0\n0` when the pipe
|
|
9
|
+
* produced no matches. The fix in the bash core was `|| true` + default
|
|
10
|
+
* via `${LINE_COUNT:-0}`.
|
|
11
|
+
*
|
|
12
|
+
* The TS port closes this entire class of bug: counting happens over an
|
|
13
|
+
* actual string in Node, not via a pipe-on-a-side-effect. The only way
|
|
14
|
+
* LINE_COUNT / FILE_COUNT can ever be wrong now is a test-missed edge in
|
|
15
|
+
* `countChangedLines` or `countChangedFiles` — unit tests in `banner.test.ts`
|
|
16
|
+
* cover the zero case, the empty-diff case, the unicode-filename case, and
|
|
17
|
+
* the `+++ b/-file` edge explicitly.
|
|
18
|
+
*
|
|
19
|
+
* ## Format parity
|
|
20
|
+
*
|
|
21
|
+
* `renderPushReviewRequiredBanner` reproduces the byte-exact output of the
|
|
22
|
+
* bash core's "PUSH REVIEW GATE: Review required..." block, including the
|
|
23
|
+
* cache-disabled fallback branch. A fixture test in `banner.test.ts` asserts
|
|
24
|
+
* the output against a snapshot captured from the 0.10.1 bash core.
|
|
25
|
+
*/
|
|
26
|
+
export interface DiffStats {
|
|
27
|
+
/** Number of `^\+[^+]|^-[^-]` lines in the unified diff. */
|
|
28
|
+
line_count: number;
|
|
29
|
+
/** Number of `^\+\+\+ ` lines (one per changed file). */
|
|
30
|
+
file_count: number;
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* Count lines that begin with `+` followed by a non-`+` character, OR `-`
|
|
34
|
+
* followed by a non-`-` character. Bash-core parity (push-review-core.sh
|
|
35
|
+
* §1082): `grep -cE '^\+[^+]|^-[^-]'`. This rejects every line whose
|
|
36
|
+
* SECOND character is the same as the first — not just `+++`/`---`
|
|
37
|
+
* headers, but also pathological `++foo` and `--bar` strings (which bash
|
|
38
|
+
* did not count). Codex pass-1 on phase 1 flagged the earlier too-lax
|
|
39
|
+
* char-1-only TS implementation that would have silently changed the
|
|
40
|
+
* Scope: banner line count vs. bash and broken phase-4 byte compatibility.
|
|
41
|
+
*
|
|
42
|
+
* Empty input → 0. Bare `+` or `-` (single char line) → 0, same as bash
|
|
43
|
+
* (the regex requires a second character).
|
|
44
|
+
*/
|
|
45
|
+
export declare function countChangedLines(diff: string): number;
|
|
46
|
+
/**
|
|
47
|
+
* Count `^\+\+\+ ` header lines (one per file in the diff). Parity with
|
|
48
|
+
* the bash core's `grep -c '^\+\+\+ '`.
|
|
49
|
+
*/
|
|
50
|
+
export declare function countChangedFiles(diff: string): number;
|
|
51
|
+
/**
|
|
52
|
+
* Compute `{line_count, file_count}` over a diff string. Exposed separately
|
|
53
|
+
* so callers can use just the stats without generating the full banner.
|
|
54
|
+
*/
|
|
55
|
+
export declare function computeDiffStats(diff: string): DiffStats;
|
|
56
|
+
export interface PushReviewRequiredBannerInput {
|
|
57
|
+
/** The ref being pushed (e.g. `refs/heads/feature/foo` or `HEAD`). */
|
|
58
|
+
source_ref: string;
|
|
59
|
+
/** The source commit SHA (12 chars + rest; full sha expected). */
|
|
60
|
+
source_sha: string;
|
|
61
|
+
/** Target branch / base label (defect N completion surfaces here). */
|
|
62
|
+
target_branch: string;
|
|
63
|
+
/** Resolved merge-base SHA. */
|
|
64
|
+
merge_base: string;
|
|
65
|
+
/** Diff stats — pre-computed by `computeDiffStats`. */
|
|
66
|
+
stats: DiffStats;
|
|
67
|
+
/**
|
|
68
|
+
* The sha256-of-diff cache key. When empty, the banner emits the
|
|
69
|
+
* cache-disabled fallback branch (`Cache is DISABLED on this host`).
|
|
70
|
+
*/
|
|
71
|
+
push_sha: string;
|
|
72
|
+
/** Source branch name for the `rea cache set` hint. */
|
|
73
|
+
source_branch: string;
|
|
74
|
+
}
|
|
75
|
+
/**
|
|
76
|
+
* Compose the "PUSH REVIEW GATE: Review required before pushing" banner.
|
|
77
|
+
* Output goes to stderr via the caller; this function is pure. Returns the
|
|
78
|
+
* exact text the bash core would have printed (including trailing blank
|
|
79
|
+
* line and spacing), so the fixture snapshot can be compared byte-exactly.
|
|
80
|
+
*/
|
|
81
|
+
export declare function renderPushReviewRequiredBanner(input: PushReviewRequiredBannerInput): string;
|
|
82
|
+
export interface ProtectedPathsBlockedBannerInput {
|
|
83
|
+
source_ref: string;
|
|
84
|
+
source_sha: string;
|
|
85
|
+
}
|
|
86
|
+
/**
|
|
87
|
+
* Compose the "PUSH BLOCKED: protected paths changed — /codex-review
|
|
88
|
+
* required" banner. Pure; exit-2 translation happens in the CLI shim.
|
|
89
|
+
*/
|
|
90
|
+
export declare function renderProtectedPathsBlockedBanner(input: ProtectedPathsBlockedBannerInput): string;
|
|
91
|
+
/**
|
|
92
|
+
* Strip C0 control characters (0x00-0x1F, 0x7F) and C1 (0x80-0x9F) from a
|
|
93
|
+
* string. Used when a banner embeds text from a subprocess's stderr (e.g.
|
|
94
|
+
* the cache-check failure case). Mirrors the `LC_ALL=C tr -d` invocation
|
|
95
|
+
* in the bash core's cache-error path. Codex LOW 5 on the 0.9.4 pass.
|
|
96
|
+
*/
|
|
97
|
+
export declare function stripControlChars(input: string): string;
|
|
@@ -0,0 +1,172 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Operator-facing banner composition.
|
|
3
|
+
*
|
|
4
|
+
* The bash core builds its banners via `printf` inside a `{ ... } >&2`
|
|
5
|
+
* block, counting diff lines with `grep -cE ...` and file changes with
|
|
6
|
+
* `grep -c '^+++ '`. Defect K (rea#62) surfaced because `grep -c`
|
|
7
|
+
* emits `0` to stdout AND exits non-zero on no-match, and the bash author
|
|
8
|
+
* wrote `$(grep -c ... || echo 0)` — which emitted `0\n0` when the pipe
|
|
9
|
+
* produced no matches. The fix in the bash core was `|| true` + default
|
|
10
|
+
* via `${LINE_COUNT:-0}`.
|
|
11
|
+
*
|
|
12
|
+
* The TS port closes this entire class of bug: counting happens over an
|
|
13
|
+
* actual string in Node, not via a pipe-on-a-side-effect. The only way
|
|
14
|
+
* LINE_COUNT / FILE_COUNT can ever be wrong now is a test-missed edge in
|
|
15
|
+
* `countChangedLines` or `countChangedFiles` — unit tests in `banner.test.ts`
|
|
16
|
+
* cover the zero case, the empty-diff case, the unicode-filename case, and
|
|
17
|
+
* the `+++ b/-file` edge explicitly.
|
|
18
|
+
*
|
|
19
|
+
* ## Format parity
|
|
20
|
+
*
|
|
21
|
+
* `renderPushReviewRequiredBanner` reproduces the byte-exact output of the
|
|
22
|
+
* bash core's "PUSH REVIEW GATE: Review required..." block, including the
|
|
23
|
+
* cache-disabled fallback branch. A fixture test in `banner.test.ts` asserts
|
|
24
|
+
* the output against a snapshot captured from the 0.10.1 bash core.
|
|
25
|
+
*/
|
|
26
|
+
/**
|
|
27
|
+
* Count lines that begin with `+` followed by a non-`+` character, OR `-`
|
|
28
|
+
* followed by a non-`-` character. Bash-core parity (push-review-core.sh
|
|
29
|
+
* §1082): `grep -cE '^\+[^+]|^-[^-]'`. This rejects every line whose
|
|
30
|
+
* SECOND character is the same as the first — not just `+++`/`---`
|
|
31
|
+
* headers, but also pathological `++foo` and `--bar` strings (which bash
|
|
32
|
+
* did not count). Codex pass-1 on phase 1 flagged the earlier too-lax
|
|
33
|
+
* char-1-only TS implementation that would have silently changed the
|
|
34
|
+
* Scope: banner line count vs. bash and broken phase-4 byte compatibility.
|
|
35
|
+
*
|
|
36
|
+
* Empty input → 0. Bare `+` or `-` (single char line) → 0, same as bash
|
|
37
|
+
* (the regex requires a second character).
|
|
38
|
+
*/
|
|
39
|
+
export function countChangedLines(diff) {
|
|
40
|
+
if (diff.length === 0)
|
|
41
|
+
return 0;
|
|
42
|
+
let n = 0;
|
|
43
|
+
const lines = diff.split('\n');
|
|
44
|
+
for (const line of lines) {
|
|
45
|
+
if (line.length < 2)
|
|
46
|
+
continue;
|
|
47
|
+
const c0 = line.charCodeAt(0);
|
|
48
|
+
const c1 = line.charCodeAt(1);
|
|
49
|
+
// `+` = 43: match only when char-2 is NOT `+`.
|
|
50
|
+
if (c0 === 43 && c1 !== 43) {
|
|
51
|
+
n++;
|
|
52
|
+
continue;
|
|
53
|
+
}
|
|
54
|
+
// `-` = 45: match only when char-2 is NOT `-`.
|
|
55
|
+
if (c0 === 45 && c1 !== 45) {
|
|
56
|
+
n++;
|
|
57
|
+
continue;
|
|
58
|
+
}
|
|
59
|
+
// Any other leading char, including `++...` and `--...`, is skipped.
|
|
60
|
+
}
|
|
61
|
+
return n;
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* Count `^\+\+\+ ` header lines (one per file in the diff). Parity with
|
|
65
|
+
* the bash core's `grep -c '^\+\+\+ '`.
|
|
66
|
+
*/
|
|
67
|
+
export function countChangedFiles(diff) {
|
|
68
|
+
if (diff.length === 0)
|
|
69
|
+
return 0;
|
|
70
|
+
let n = 0;
|
|
71
|
+
const lines = diff.split('\n');
|
|
72
|
+
for (const line of lines) {
|
|
73
|
+
if (line.startsWith('+++ '))
|
|
74
|
+
n++;
|
|
75
|
+
}
|
|
76
|
+
return n;
|
|
77
|
+
}
|
|
78
|
+
/**
|
|
79
|
+
* Compute `{line_count, file_count}` over a diff string. Exposed separately
|
|
80
|
+
* so callers can use just the stats without generating the full banner.
|
|
81
|
+
*/
|
|
82
|
+
export function computeDiffStats(diff) {
|
|
83
|
+
return {
|
|
84
|
+
line_count: countChangedLines(diff),
|
|
85
|
+
file_count: countChangedFiles(diff),
|
|
86
|
+
};
|
|
87
|
+
}
|
|
88
|
+
/**
|
|
89
|
+
* Compose the "PUSH REVIEW GATE: Review required before pushing" banner.
|
|
90
|
+
* Output goes to stderr via the caller; this function is pure. Returns the
|
|
91
|
+
* exact text the bash core would have printed (including trailing blank
|
|
92
|
+
* line and spacing), so the fixture snapshot can be compared byte-exactly.
|
|
93
|
+
*/
|
|
94
|
+
export function renderPushReviewRequiredBanner(input) {
|
|
95
|
+
const lines = [];
|
|
96
|
+
lines.push('PUSH REVIEW GATE: Review required before pushing');
|
|
97
|
+
lines.push('');
|
|
98
|
+
lines.push(` Source ref: ${input.source_ref} (${input.source_sha.slice(0, 12)})`);
|
|
99
|
+
lines.push(` Target: ${input.target_branch}`);
|
|
100
|
+
lines.push(` Scope: ${input.stats.file_count} files changed, ${input.stats.line_count} lines`);
|
|
101
|
+
lines.push('');
|
|
102
|
+
lines.push(' Action required:');
|
|
103
|
+
lines.push(` 1. Spawn a code-reviewer agent to review: git diff ${input.merge_base}..${input.source_sha}`);
|
|
104
|
+
lines.push(' 2. Spawn a security-engineer agent for security review');
|
|
105
|
+
if (input.push_sha.length > 0) {
|
|
106
|
+
lines.push(' 3. After both pass, cache the result:');
|
|
107
|
+
lines.push(` rea cache set ${input.push_sha} pass --branch ${input.source_branch} --base ${input.target_branch}`);
|
|
108
|
+
}
|
|
109
|
+
else {
|
|
110
|
+
lines.push(' 3. Cache is DISABLED on this host (no sha256 hasher found).');
|
|
111
|
+
lines.push(' After both reviews pass, bypass the push-review gate with:');
|
|
112
|
+
lines.push(' REA_SKIP_PUSH_REVIEW="<reason>" git push ...');
|
|
113
|
+
lines.push(' The bypass is audited as push.review.skipped — this is the');
|
|
114
|
+
lines.push(' documented escape hatch when cache is unavailable.');
|
|
115
|
+
lines.push(' To restore the cache path, install one of: sha256sum,');
|
|
116
|
+
lines.push(' shasum (Perl Digest::SHA), or openssl.');
|
|
117
|
+
}
|
|
118
|
+
lines.push('');
|
|
119
|
+
// bash `printf '%s\n'` with no trailing args adds a final newline; the
|
|
120
|
+
// block renders terminal-ready. `lines.join('\n') + '\n'` reproduces
|
|
121
|
+
// exactly that shape.
|
|
122
|
+
return lines.join('\n') + '\n';
|
|
123
|
+
}
|
|
124
|
+
/**
|
|
125
|
+
* Compose the "PUSH BLOCKED: protected paths changed — /codex-review
|
|
126
|
+
* required" banner. Pure; exit-2 translation happens in the CLI shim.
|
|
127
|
+
*/
|
|
128
|
+
export function renderProtectedPathsBlockedBanner(input) {
|
|
129
|
+
const lines = [];
|
|
130
|
+
lines.push(`PUSH BLOCKED: protected paths changed — /codex-review required for ${input.source_sha}`);
|
|
131
|
+
lines.push('');
|
|
132
|
+
lines.push(` Source ref: ${input.source_ref}`);
|
|
133
|
+
lines.push(' Diff touches one of:');
|
|
134
|
+
lines.push(' - src/gateway/middleware/');
|
|
135
|
+
lines.push(' - hooks/');
|
|
136
|
+
lines.push(' - .claude/hooks/');
|
|
137
|
+
lines.push(' - src/policy/');
|
|
138
|
+
lines.push(' - .github/workflows/');
|
|
139
|
+
lines.push(' - .rea/');
|
|
140
|
+
lines.push(' - .husky/');
|
|
141
|
+
lines.push('');
|
|
142
|
+
lines.push(` Run /codex-review against ${input.source_sha}, then retry the push.`);
|
|
143
|
+
lines.push(' The codex-adversarial agent emits the required audit entry.');
|
|
144
|
+
lines.push(' Only `pass` or `concerns` verdicts satisfy this gate.');
|
|
145
|
+
lines.push('');
|
|
146
|
+
return lines.join('\n') + '\n';
|
|
147
|
+
}
|
|
148
|
+
/**
|
|
149
|
+
* Strip C0 control characters (0x00-0x1F, 0x7F) and C1 (0x80-0x9F) from a
|
|
150
|
+
* string. Used when a banner embeds text from a subprocess's stderr (e.g.
|
|
151
|
+
* the cache-check failure case). Mirrors the `LC_ALL=C tr -d` invocation
|
|
152
|
+
* in the bash core's cache-error path. Codex LOW 5 on the 0.9.4 pass.
|
|
153
|
+
*/
|
|
154
|
+
export function stripControlChars(input) {
|
|
155
|
+
let out = '';
|
|
156
|
+
for (let i = 0; i < input.length; i++) {
|
|
157
|
+
const c = input.charCodeAt(i);
|
|
158
|
+
// Allow tab (9), LF (10), CR (13) — but not any other C0/C1 byte.
|
|
159
|
+
if (c === 9 || c === 10 || c === 13) {
|
|
160
|
+
out += input[i];
|
|
161
|
+
continue;
|
|
162
|
+
}
|
|
163
|
+
if (c <= 0x1f)
|
|
164
|
+
continue;
|
|
165
|
+
if (c === 0x7f)
|
|
166
|
+
continue;
|
|
167
|
+
if (c >= 0x80 && c <= 0x9f)
|
|
168
|
+
continue;
|
|
169
|
+
out += input[i];
|
|
170
|
+
}
|
|
171
|
+
return out;
|
|
172
|
+
}
|
|
@@ -0,0 +1,155 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Base-ref resolution for the push-review gate.
|
|
3
|
+
*
|
|
4
|
+
* ## What "base resolution" means
|
|
5
|
+
*
|
|
6
|
+
* Given a pushed refspec (a `local_sha` + `remote_ref` pair, plus the
|
|
7
|
+
* remote name), determine:
|
|
8
|
+
*
|
|
9
|
+
* 1. the commit SHA the local changes should be diffed against
|
|
10
|
+
* (the "merge base"), and
|
|
11
|
+
* 2. the human-facing label for the `Target:` banner line
|
|
12
|
+
* (defect N semantic: the SEMANTIC base, not the refspec destination).
|
|
13
|
+
*
|
|
14
|
+
* The four code paths the bash core walked (push-review-core.sh §720-889):
|
|
15
|
+
*
|
|
16
|
+
* A. Tracked-branch push (`remote_sha != ZERO`). Use
|
|
17
|
+
* `git merge-base <remote_sha> <local_sha>`. Label = refspec target.
|
|
18
|
+
* B. New-branch push with `branch.<source>.base` config (defect N). The
|
|
19
|
+
* operator opted into a specific base. Prefer `refs/remotes/<remote>/
|
|
20
|
+
* <configured>` if it exists, else fall back to `refs/heads/<configured>`
|
|
21
|
+
* with a WARN on stderr. Label = configured base name.
|
|
22
|
+
* C. New-branch push without config, with `refs/remotes/<remote>/HEAD`
|
|
23
|
+
* resolvable. Use that symbolic-ref as the anchor. Label = refspec
|
|
24
|
+
* target (preserves the cache-key contract for bare pushes).
|
|
25
|
+
* D. Bootstrap: no config, no symbolic-ref, probe `main` then `master`.
|
|
26
|
+
* If both fail, anchor on the empty-tree SHA so the full push content
|
|
27
|
+
* is reviewable. Label = refspec target.
|
|
28
|
+
*
|
|
29
|
+
* ## Phase 2a scope (this file)
|
|
30
|
+
*
|
|
31
|
+
* `resolveBaseForRefspec()` composes the four paths via the `GitRunner`
|
|
32
|
+
* port from `diff.ts`. This module is pure in the same sense `diff.ts`
|
|
33
|
+
* is — every git hit goes through the injected runner, so unit tests
|
|
34
|
+
* enumerate the four paths without touching a real repo.
|
|
35
|
+
*
|
|
36
|
+
* Defect-N fail-loud (design §7) is Phase 4's final cutover and is NOT
|
|
37
|
+
* turned on here. `NoBaseResolvableError` is reserved in `errors.ts` but
|
|
38
|
+
* the empty-tree bootstrap remains the current production fallback.
|
|
39
|
+
* Phase 2b composes the final policy into `runPushReviewGate()`.
|
|
40
|
+
*/
|
|
41
|
+
import { type GitRunner } from './diff.js';
|
|
42
|
+
import type { RefspecRecord } from './args.js';
|
|
43
|
+
/**
|
|
44
|
+
* Resolved base outcome for a single refspec. Never thrown — callers
|
|
45
|
+
* translate blocked conditions (remote object missing, no merge-base)
|
|
46
|
+
* into `BlockedError` subclasses up the stack.
|
|
47
|
+
*/
|
|
48
|
+
export interface ResolvedBase {
|
|
49
|
+
/**
|
|
50
|
+
* The commit / tree SHA to diff against. Always set when
|
|
51
|
+
* `status === 'ok'`; otherwise null.
|
|
52
|
+
*/
|
|
53
|
+
merge_base: string | null;
|
|
54
|
+
/**
|
|
55
|
+
* The human-facing `Target:` label (defect N). The bash core defaults
|
|
56
|
+
* this to the refspec target and promotes it to the configured base's
|
|
57
|
+
* short name only when `branch.<source>.base` resolved. We mirror that.
|
|
58
|
+
*/
|
|
59
|
+
target_label: string;
|
|
60
|
+
/** Discriminator for the caller. */
|
|
61
|
+
status: 'ok' | 'remote_object_missing' | 'no_merge_base' | 'no_base_resolvable';
|
|
62
|
+
/**
|
|
63
|
+
* For the "tracked branch but the remote commit isn't locally present"
|
|
64
|
+
* path, return the remote SHA so the caller's banner can echo it. Empty
|
|
65
|
+
* otherwise.
|
|
66
|
+
*/
|
|
67
|
+
remote_sha?: string;
|
|
68
|
+
/**
|
|
69
|
+
* True when the configured-base branch was resolved via the LOCAL ref
|
|
70
|
+
* (`refs/heads/<configured>`) instead of the remote-tracking ref. The
|
|
71
|
+
* bash core prints a WARN in this case (push-review-core.sh §819-820).
|
|
72
|
+
* Phase 2a carries the signal; Phase 2b's composition emits the banner.
|
|
73
|
+
*/
|
|
74
|
+
local_ref_fallback_warning?: string;
|
|
75
|
+
/**
|
|
76
|
+
* The resolution path taken. Audit + debugging aid; never part of the
|
|
77
|
+
* cache key. Phase 2b's audit records include this for forensic trace.
|
|
78
|
+
*/
|
|
79
|
+
path: 'tracked' | 'new_branch_config' | 'new_branch_origin_head' | 'bootstrap_empty_tree';
|
|
80
|
+
}
|
|
81
|
+
/**
|
|
82
|
+
* Deps for base resolution. Same `GitRunner` port `diff.ts` uses; plus the
|
|
83
|
+
* remote name (from the adapter's argv, defaults to `origin`). `cwd` is
|
|
84
|
+
* the resolved repo root.
|
|
85
|
+
*/
|
|
86
|
+
export interface ResolveBaseDeps {
|
|
87
|
+
runner: GitRunner;
|
|
88
|
+
cwd: string;
|
|
89
|
+
/** Remote name (`origin` by convention, but respect what git passed to the hook). */
|
|
90
|
+
remote: string;
|
|
91
|
+
}
|
|
92
|
+
/**
|
|
93
|
+
* Strip `refs/heads/` / `refs/for/` prefixes from a ref and return the
|
|
94
|
+
* trailing branch name. Used for the target-label normalization path
|
|
95
|
+
* where both ref families should collapse to a bare branch name for
|
|
96
|
+
* display. Exported for tests.
|
|
97
|
+
*/
|
|
98
|
+
export declare function stripRefsPrefix(ref: string): string;
|
|
99
|
+
/**
|
|
100
|
+
* Strip ONLY the `refs/heads/` prefix — leaves `refs/for/`, `refs/tags/`,
|
|
101
|
+
* and every other ref-namespace untouched. Mirrors the bash core's
|
|
102
|
+
* `${local_ref#refs/heads/}` on the source-branch lookup path (push-review-
|
|
103
|
+
* core.sh §797), so Gerrit-style pushes (`refs/for/main`) keep their
|
|
104
|
+
* namespace and do NOT accidentally match a `branch.main.base` config
|
|
105
|
+
* entry intended for a regular branch push.
|
|
106
|
+
*
|
|
107
|
+
* Codex pass-1 on Phase 2a flagged the earlier implementation that used
|
|
108
|
+
* `stripRefsPrefix` here — it would have promoted the Target: label for
|
|
109
|
+
* a Gerrit push against the reviewer's intent. Exported for tests.
|
|
110
|
+
*/
|
|
111
|
+
export declare function stripRefsHeadsOnly(ref: string): string;
|
|
112
|
+
/**
|
|
113
|
+
* Resolve the base anchor for a single push refspec. See the file-top
|
|
114
|
+
* docstring for the four code paths.
|
|
115
|
+
*
|
|
116
|
+
* Deletion refspecs (local_sha === ZERO_SHA) return `{merge_base: null,
|
|
117
|
+
* status: 'ok'}` with `path: 'tracked'` — the caller is expected to have
|
|
118
|
+
* already trapped deletions via `hasDeletion()` before calling here. We
|
|
119
|
+
* don't throw in that case because the caller owns the deletion policy,
|
|
120
|
+
* not this resolver.
|
|
121
|
+
*/
|
|
122
|
+
export declare function resolveBaseForRefspec(record: RefspecRecord, deps: ResolveBaseDeps): ResolvedBase;
|
|
123
|
+
/**
|
|
124
|
+
* Compute the initial `Target:` label for a refspec: the short name of
|
|
125
|
+
* the remote ref, falling back to `main` when it's empty (defensive;
|
|
126
|
+
* `args.ts` should never emit an empty remote_ref for a non-deletion).
|
|
127
|
+
*
|
|
128
|
+
* Exported for unit tests. Mirrors push-review-core.sh §725-727.
|
|
129
|
+
*/
|
|
130
|
+
export declare function computeInitialTargetLabel(record: RefspecRecord): string;
|
|
131
|
+
/**
|
|
132
|
+
* Inner helper: resolve the new-branch anchor via the B→C→D walk. Stays
|
|
133
|
+
* module-private so callers only see `resolveBaseForRefspec` as the
|
|
134
|
+
* public surface. Returns a discriminated union so the caller can branch
|
|
135
|
+
* on path + extract the warning / label cleanly.
|
|
136
|
+
*/
|
|
137
|
+
type NewBranchOutcome = {
|
|
138
|
+
kind: 'config_hit';
|
|
139
|
+
ref: string;
|
|
140
|
+
label: string;
|
|
141
|
+
/** Non-null when we fell back to `refs/heads/<base>` (§819-820 WARN). */
|
|
142
|
+
warning: string | null;
|
|
143
|
+
} | {
|
|
144
|
+
kind: 'origin_head';
|
|
145
|
+
ref: string;
|
|
146
|
+
} | {
|
|
147
|
+
kind: 'bootstrap';
|
|
148
|
+
};
|
|
149
|
+
/**
|
|
150
|
+
* Path B: consult `branch.<source>.base`. Returns `config_hit` iff a base
|
|
151
|
+
* was configured AND resolvable to a ref that exists. Falls through
|
|
152
|
+
* otherwise. Exported so tests can exercise the config-path independently.
|
|
153
|
+
*/
|
|
154
|
+
export declare function resolveNewBranchBase(sourceBranch: string, deps: ResolveBaseDeps): NewBranchOutcome;
|
|
155
|
+
export {};
|