@bookedsolid/rea 0.4.0 → 0.5.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.
- package/dist/cache/review-cache.d.ts +115 -0
- package/dist/cache/review-cache.js +200 -0
- package/dist/cli/cache.d.ts +52 -0
- package/dist/cli/cache.js +112 -0
- package/dist/cli/index.js +41 -0
- package/dist/cli/init.js +16 -0
- package/dist/cli/install/gitignore.d.ts +114 -0
- package/dist/cli/install/gitignore.js +356 -0
- package/dist/cli/upgrade.js +20 -0
- package/dist/policy/loader.d.ts +10 -0
- package/dist/policy/loader.js +2 -0
- package/dist/policy/types.d.ts +20 -0
- package/hooks/push-review-gate.sh +185 -1
- package/package.json +1 -1
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* BUG-010 — `.gitignore` scaffolding for rea-managed runtime artifacts.
|
|
3
|
+
*
|
|
4
|
+
* Background. `rea serve` (G7 catalog fingerprint) writes
|
|
5
|
+
* `.rea/fingerprints.json` at startup. `rea init` in 0.4.0 and earlier never
|
|
6
|
+
* scaffolded ANY `.gitignore` entries for the consumer repo, so an operator
|
|
7
|
+
* who ran `rea init` then started the gateway would see a "new file" in
|
|
8
|
+
* `git status` that nobody told them about. Helix reported this as BUG-010.
|
|
9
|
+
*
|
|
10
|
+
* The fix is broader than fingerprints.json — every runtime artifact rea
|
|
11
|
+
* writes (under `.rea/` AND its sibling `proper-lockfile` directory at
|
|
12
|
+
* `.rea.lock`) must be in the consumer's `.gitignore`:
|
|
13
|
+
*
|
|
14
|
+
* - `.rea/audit.jsonl` — G1 hash-chained audit log (append-only)
|
|
15
|
+
* - `.rea/audit-*.jsonl` — G1 rotated audit archives
|
|
16
|
+
* - `.rea/HALT` — /freeze marker (ephemeral)
|
|
17
|
+
* - `.rea/metrics.jsonl` — G5 metrics stream
|
|
18
|
+
* - `.rea/serve.pid` — G5 `rea serve` pidfile
|
|
19
|
+
* - `.rea/serve.state.json` — G5 `rea serve` state snapshot
|
|
20
|
+
* - `.rea/fingerprints.json` — G7 downstream catalog fingerprints (BUG-010)
|
|
21
|
+
* - `.rea/review-cache.jsonl` — BUG-009 review cache (rea cache set/check)
|
|
22
|
+
* - `.rea/*.tmp` — serve temp-file-then-rename pattern
|
|
23
|
+
* - `.rea/*.tmp.*` — review-cache pid-salted temp pattern
|
|
24
|
+
* - `.rea/install-manifest.json.bak` / `.tmp` — fs-safe atomic-replace sidecars
|
|
25
|
+
* - `.gitignore.rea-tmp-*` — this module's own temp files on crash
|
|
26
|
+
* (root-level — writeAtomic stages next
|
|
27
|
+
* to .gitignore, not under .rea/)
|
|
28
|
+
* - `.rea.lock` — proper-lockfile sibling dir (NOT under .rea/)
|
|
29
|
+
* (Codex F1 on the BUG-010 review caught all three of these last groups.)
|
|
30
|
+
*
|
|
31
|
+
* Idempotency contract.
|
|
32
|
+
*
|
|
33
|
+
* - `rea init` on a fresh repo with no `.gitignore` → create one with the
|
|
34
|
+
* managed block only.
|
|
35
|
+
* - `rea init` on a repo with a `.gitignore` that has NO rea block → append
|
|
36
|
+
* a managed block separated by a blank line.
|
|
37
|
+
* - `rea upgrade` on an older install whose `.gitignore` lacks the block →
|
|
38
|
+
* same as init; backfill the block so `fingerprints.json` stops showing
|
|
39
|
+
* up as an untracked file.
|
|
40
|
+
* - `rea upgrade` where the managed block exists but is missing some new
|
|
41
|
+
* entries (e.g. `fingerprints.json`, `review-cache.jsonl` added in 0.5.0)
|
|
42
|
+
* → insert the missing lines inside the existing block, preserving any
|
|
43
|
+
* operator-authored lines within the block.
|
|
44
|
+
* - All entries already present, in any order → no-op.
|
|
45
|
+
*
|
|
46
|
+
* Operator DELETIONS of canonical entries are NOT preserved — re-running
|
|
47
|
+
* ensureReaGitignore will re-insert any canonical entry missing from the
|
|
48
|
+
* block body. To opt out of ignoring a specific artifact, operators must
|
|
49
|
+
* configure rea itself, not edit the managed block. This is intentional —
|
|
50
|
+
* the managed block is rea's territory.
|
|
51
|
+
*
|
|
52
|
+
* Security/containment.
|
|
53
|
+
*
|
|
54
|
+
* - Refuse to follow a `.gitignore` symlink (`lstat` gate before any read).
|
|
55
|
+
* The subsequent read uses `O_NOFOLLOW | O_RDONLY` so a TOCTOU swap after
|
|
56
|
+
* the lstat cannot trick us into reading through a symlink to secrets
|
|
57
|
+
* (e.g. `~/.ssh/id_rsa`) and splicing them into the written `.gitignore`.
|
|
58
|
+
* - Temp file name uses `crypto.randomBytes(16)` — not PID + Date.now, which
|
|
59
|
+
* are predictable and leak process info. (Codex F2.)
|
|
60
|
+
* - Cleanup best-effort on write failure so a stale temp file from a
|
|
61
|
+
* prior crash does not accrete.
|
|
62
|
+
*
|
|
63
|
+
* CRLF compatibility (Codex F3).
|
|
64
|
+
*
|
|
65
|
+
* Windows consumers with `core.autocrlf=true` get CRLF line endings on
|
|
66
|
+
* `.gitignore`. Without explicit handling, `"# === rea managed ==="` !==
|
|
67
|
+
* `"# === rea managed ===\r"` and every upgrade would append a duplicate
|
|
68
|
+
* block. We detect the input EOL on read, split on `\r?\n`, trim trailing
|
|
69
|
+
* whitespace from each line before marker-anchored matching, and re-emit
|
|
70
|
+
* with the detected EOL on write.
|
|
71
|
+
*
|
|
72
|
+
* Duplicate blocks (Codex F4).
|
|
73
|
+
*
|
|
74
|
+
* If the file already contains two managed blocks (from a prior bug,
|
|
75
|
+
* manual copy-paste, or two different rea versions), refuse to modify and
|
|
76
|
+
* surface a warning. Merging is more ambitious than this module needs to
|
|
77
|
+
* be — the operator resolves manually, then a subsequent run proceeds.
|
|
78
|
+
*/
|
|
79
|
+
export declare const GITIGNORE_BLOCK_START = "# === rea managed \u2014 do not edit between markers ===";
|
|
80
|
+
export declare const GITIGNORE_BLOCK_END = "# === end rea managed ===";
|
|
81
|
+
/**
|
|
82
|
+
* Ordered list of entries every rea install must gitignore. Order is stable
|
|
83
|
+
* so the scaffolded block is deterministic across runs, which in turn makes
|
|
84
|
+
* drift detection tractable: a diff in the managed block means a consumer
|
|
85
|
+
* (or another installer) edited it, not that rea reshuffled.
|
|
86
|
+
*
|
|
87
|
+
* The grouping below is by origin, not alphabetical:
|
|
88
|
+
* 1. audit + HALT + metrics (G1, G4, G5)
|
|
89
|
+
* 2. serve state (G5)
|
|
90
|
+
* 3. fingerprints (G7 / BUG-010)
|
|
91
|
+
* 4. review cache (BUG-009)
|
|
92
|
+
* 5. temp/sidecar patterns (Codex F1)
|
|
93
|
+
* 6. sibling lockfile (Codex F1 — OUTSIDE .rea/)
|
|
94
|
+
*/
|
|
95
|
+
export declare const REA_GITIGNORE_ENTRIES: readonly string[];
|
|
96
|
+
export interface EnsureGitignoreResult {
|
|
97
|
+
/** Absolute path to the `.gitignore` file that was (maybe) written. */
|
|
98
|
+
path: string;
|
|
99
|
+
/** `created` = no file before. `updated` = block added or amended. `unchanged` = no-op. */
|
|
100
|
+
action: 'created' | 'updated' | 'unchanged';
|
|
101
|
+
/** Entries the caller added this run (subset of `REA_GITIGNORE_ENTRIES`). */
|
|
102
|
+
addedEntries: string[];
|
|
103
|
+
/** Non-fatal operator-facing messages (e.g. symlink refused, duplicate blocks). */
|
|
104
|
+
warnings: string[];
|
|
105
|
+
}
|
|
106
|
+
/**
|
|
107
|
+
* Main entry point. Idempotent: calling twice in a row produces `unchanged`
|
|
108
|
+
* on the second call.
|
|
109
|
+
*
|
|
110
|
+
* The `entries` parameter defaults to `REA_GITIGNORE_ENTRIES` — both `rea
|
|
111
|
+
* init` and `rea upgrade` pass the default. Tests override to verify
|
|
112
|
+
* reconciliation.
|
|
113
|
+
*/
|
|
114
|
+
export declare function ensureReaGitignore(targetDir: string, entries?: readonly string[]): Promise<EnsureGitignoreResult>;
|
|
@@ -0,0 +1,356 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* BUG-010 — `.gitignore` scaffolding for rea-managed runtime artifacts.
|
|
3
|
+
*
|
|
4
|
+
* Background. `rea serve` (G7 catalog fingerprint) writes
|
|
5
|
+
* `.rea/fingerprints.json` at startup. `rea init` in 0.4.0 and earlier never
|
|
6
|
+
* scaffolded ANY `.gitignore` entries for the consumer repo, so an operator
|
|
7
|
+
* who ran `rea init` then started the gateway would see a "new file" in
|
|
8
|
+
* `git status` that nobody told them about. Helix reported this as BUG-010.
|
|
9
|
+
*
|
|
10
|
+
* The fix is broader than fingerprints.json — every runtime artifact rea
|
|
11
|
+
* writes (under `.rea/` AND its sibling `proper-lockfile` directory at
|
|
12
|
+
* `.rea.lock`) must be in the consumer's `.gitignore`:
|
|
13
|
+
*
|
|
14
|
+
* - `.rea/audit.jsonl` — G1 hash-chained audit log (append-only)
|
|
15
|
+
* - `.rea/audit-*.jsonl` — G1 rotated audit archives
|
|
16
|
+
* - `.rea/HALT` — /freeze marker (ephemeral)
|
|
17
|
+
* - `.rea/metrics.jsonl` — G5 metrics stream
|
|
18
|
+
* - `.rea/serve.pid` — G5 `rea serve` pidfile
|
|
19
|
+
* - `.rea/serve.state.json` — G5 `rea serve` state snapshot
|
|
20
|
+
* - `.rea/fingerprints.json` — G7 downstream catalog fingerprints (BUG-010)
|
|
21
|
+
* - `.rea/review-cache.jsonl` — BUG-009 review cache (rea cache set/check)
|
|
22
|
+
* - `.rea/*.tmp` — serve temp-file-then-rename pattern
|
|
23
|
+
* - `.rea/*.tmp.*` — review-cache pid-salted temp pattern
|
|
24
|
+
* - `.rea/install-manifest.json.bak` / `.tmp` — fs-safe atomic-replace sidecars
|
|
25
|
+
* - `.gitignore.rea-tmp-*` — this module's own temp files on crash
|
|
26
|
+
* (root-level — writeAtomic stages next
|
|
27
|
+
* to .gitignore, not under .rea/)
|
|
28
|
+
* - `.rea.lock` — proper-lockfile sibling dir (NOT under .rea/)
|
|
29
|
+
* (Codex F1 on the BUG-010 review caught all three of these last groups.)
|
|
30
|
+
*
|
|
31
|
+
* Idempotency contract.
|
|
32
|
+
*
|
|
33
|
+
* - `rea init` on a fresh repo with no `.gitignore` → create one with the
|
|
34
|
+
* managed block only.
|
|
35
|
+
* - `rea init` on a repo with a `.gitignore` that has NO rea block → append
|
|
36
|
+
* a managed block separated by a blank line.
|
|
37
|
+
* - `rea upgrade` on an older install whose `.gitignore` lacks the block →
|
|
38
|
+
* same as init; backfill the block so `fingerprints.json` stops showing
|
|
39
|
+
* up as an untracked file.
|
|
40
|
+
* - `rea upgrade` where the managed block exists but is missing some new
|
|
41
|
+
* entries (e.g. `fingerprints.json`, `review-cache.jsonl` added in 0.5.0)
|
|
42
|
+
* → insert the missing lines inside the existing block, preserving any
|
|
43
|
+
* operator-authored lines within the block.
|
|
44
|
+
* - All entries already present, in any order → no-op.
|
|
45
|
+
*
|
|
46
|
+
* Operator DELETIONS of canonical entries are NOT preserved — re-running
|
|
47
|
+
* ensureReaGitignore will re-insert any canonical entry missing from the
|
|
48
|
+
* block body. To opt out of ignoring a specific artifact, operators must
|
|
49
|
+
* configure rea itself, not edit the managed block. This is intentional —
|
|
50
|
+
* the managed block is rea's territory.
|
|
51
|
+
*
|
|
52
|
+
* Security/containment.
|
|
53
|
+
*
|
|
54
|
+
* - Refuse to follow a `.gitignore` symlink (`lstat` gate before any read).
|
|
55
|
+
* The subsequent read uses `O_NOFOLLOW | O_RDONLY` so a TOCTOU swap after
|
|
56
|
+
* the lstat cannot trick us into reading through a symlink to secrets
|
|
57
|
+
* (e.g. `~/.ssh/id_rsa`) and splicing them into the written `.gitignore`.
|
|
58
|
+
* - Temp file name uses `crypto.randomBytes(16)` — not PID + Date.now, which
|
|
59
|
+
* are predictable and leak process info. (Codex F2.)
|
|
60
|
+
* - Cleanup best-effort on write failure so a stale temp file from a
|
|
61
|
+
* prior crash does not accrete.
|
|
62
|
+
*
|
|
63
|
+
* CRLF compatibility (Codex F3).
|
|
64
|
+
*
|
|
65
|
+
* Windows consumers with `core.autocrlf=true` get CRLF line endings on
|
|
66
|
+
* `.gitignore`. Without explicit handling, `"# === rea managed ==="` !==
|
|
67
|
+
* `"# === rea managed ===\r"` and every upgrade would append a duplicate
|
|
68
|
+
* block. We detect the input EOL on read, split on `\r?\n`, trim trailing
|
|
69
|
+
* whitespace from each line before marker-anchored matching, and re-emit
|
|
70
|
+
* with the detected EOL on write.
|
|
71
|
+
*
|
|
72
|
+
* Duplicate blocks (Codex F4).
|
|
73
|
+
*
|
|
74
|
+
* If the file already contains two managed blocks (from a prior bug,
|
|
75
|
+
* manual copy-paste, or two different rea versions), refuse to modify and
|
|
76
|
+
* surface a warning. Merging is more ambitious than this module needs to
|
|
77
|
+
* be — the operator resolves manually, then a subsequent run proceeds.
|
|
78
|
+
*/
|
|
79
|
+
import crypto from 'node:crypto';
|
|
80
|
+
import fsPromises from 'node:fs/promises';
|
|
81
|
+
import path from 'node:path';
|
|
82
|
+
const GITIGNORE = '.gitignore';
|
|
83
|
+
export const GITIGNORE_BLOCK_START = '# === rea managed — do not edit between markers ===';
|
|
84
|
+
export const GITIGNORE_BLOCK_END = '# === end rea managed ===';
|
|
85
|
+
/**
|
|
86
|
+
* Ordered list of entries every rea install must gitignore. Order is stable
|
|
87
|
+
* so the scaffolded block is deterministic across runs, which in turn makes
|
|
88
|
+
* drift detection tractable: a diff in the managed block means a consumer
|
|
89
|
+
* (or another installer) edited it, not that rea reshuffled.
|
|
90
|
+
*
|
|
91
|
+
* The grouping below is by origin, not alphabetical:
|
|
92
|
+
* 1. audit + HALT + metrics (G1, G4, G5)
|
|
93
|
+
* 2. serve state (G5)
|
|
94
|
+
* 3. fingerprints (G7 / BUG-010)
|
|
95
|
+
* 4. review cache (BUG-009)
|
|
96
|
+
* 5. temp/sidecar patterns (Codex F1)
|
|
97
|
+
* 6. sibling lockfile (Codex F1 — OUTSIDE .rea/)
|
|
98
|
+
*/
|
|
99
|
+
export const REA_GITIGNORE_ENTRIES = [
|
|
100
|
+
'.rea/audit.jsonl',
|
|
101
|
+
'.rea/audit-*.jsonl',
|
|
102
|
+
'.rea/HALT',
|
|
103
|
+
'.rea/metrics.jsonl',
|
|
104
|
+
'.rea/serve.pid',
|
|
105
|
+
'.rea/serve.state.json',
|
|
106
|
+
'.rea/fingerprints.json',
|
|
107
|
+
'.rea/review-cache.jsonl',
|
|
108
|
+
'.rea/*.tmp',
|
|
109
|
+
'.rea/*.tmp.*',
|
|
110
|
+
'.rea/install-manifest.json.bak',
|
|
111
|
+
'.rea/install-manifest.json.tmp',
|
|
112
|
+
// This module's own crash-time temp files. `writeAtomic` stages the temp
|
|
113
|
+
// next to `.gitignore` (i.e. at the repo root), NOT under `.rea/` — so
|
|
114
|
+
// the glob must live at the repo root too. Codex F2 on the re-review
|
|
115
|
+
// caught the earlier `.rea/.gitignore.rea-tmp-*` mismatch.
|
|
116
|
+
'.gitignore.rea-tmp-*',
|
|
117
|
+
// proper-lockfile (audit chain, cache) locks `.rea/` via a SIBLING dir at
|
|
118
|
+
// `.rea.lock` — NOT inside `.rea/`. If this looks wrong to a future
|
|
119
|
+
// maintainer: it is correct, see src/audit/fs.ts.
|
|
120
|
+
'.rea.lock',
|
|
121
|
+
];
|
|
122
|
+
function buildManagedBlock(entries, eol) {
|
|
123
|
+
return [GITIGNORE_BLOCK_START, ...entries, GITIGNORE_BLOCK_END].join(eol);
|
|
124
|
+
}
|
|
125
|
+
/**
|
|
126
|
+
* Trim trailing whitespace ONLY (not leading) and strip a leading UTF-8 BOM.
|
|
127
|
+
* Leading whitespace would defeat the substring-spoof-rejection guarantee
|
|
128
|
+
* the tests exercise (`## === rea managed ===` must NOT match).
|
|
129
|
+
*/
|
|
130
|
+
function normalizeLineForMatch(line, isFirst) {
|
|
131
|
+
const noBom = isFirst ? line.replace(/^\uFEFF/, '') : line;
|
|
132
|
+
return noBom.replace(/\s+$/, '');
|
|
133
|
+
}
|
|
134
|
+
/**
|
|
135
|
+
* Find the managed block by ANCHORED marker lines — substring matches are
|
|
136
|
+
* rejected. A consumer comment containing the sentinel string must not
|
|
137
|
+
* reclassify an arbitrary block as rea-managed.
|
|
138
|
+
*
|
|
139
|
+
* Returns `null` if the start or end marker is not present, or if the start
|
|
140
|
+
* appears after the end (mangled block — caller falls back to append).
|
|
141
|
+
*
|
|
142
|
+
* Returns `'duplicate'` if more than one start marker or more than one end
|
|
143
|
+
* marker is found — caller refuses to modify in that case.
|
|
144
|
+
*/
|
|
145
|
+
function findManagedBlock(lines) {
|
|
146
|
+
const startIndices = [];
|
|
147
|
+
const endIndices = [];
|
|
148
|
+
for (let i = 0; i < lines.length; i += 1) {
|
|
149
|
+
const norm = normalizeLineForMatch(lines[i], i === 0);
|
|
150
|
+
if (norm === GITIGNORE_BLOCK_START)
|
|
151
|
+
startIndices.push(i);
|
|
152
|
+
else if (norm === GITIGNORE_BLOCK_END)
|
|
153
|
+
endIndices.push(i);
|
|
154
|
+
}
|
|
155
|
+
if (startIndices.length === 0 || endIndices.length === 0)
|
|
156
|
+
return null;
|
|
157
|
+
if (startIndices.length > 1 || endIndices.length > 1)
|
|
158
|
+
return 'duplicate';
|
|
159
|
+
const [startIdx] = startIndices;
|
|
160
|
+
const [endIdx] = endIndices;
|
|
161
|
+
if (endIdx <= startIdx)
|
|
162
|
+
return null;
|
|
163
|
+
return { startIdx, endIdx };
|
|
164
|
+
}
|
|
165
|
+
/**
|
|
166
|
+
* Ensure every required entry is present in the managed block. Preserves any
|
|
167
|
+
* operator-authored lines between the markers (e.g. a consumer adds
|
|
168
|
+
* `.rea/my-local-cache` to the block directly — we leave it alone). Missing
|
|
169
|
+
* required entries are appended in the canonical order, after the existing
|
|
170
|
+
* body lines.
|
|
171
|
+
*
|
|
172
|
+
* NOTE: operator deletions of canonical entries are NOT preserved — see the
|
|
173
|
+
* module docstring.
|
|
174
|
+
*/
|
|
175
|
+
function reconcileBlock(bodyLines, required) {
|
|
176
|
+
const present = new Set(bodyLines.map((l) => l.replace(/\s+$/, '')).filter((l) => l.length > 0));
|
|
177
|
+
const added = [];
|
|
178
|
+
const appended = [];
|
|
179
|
+
for (const entry of required) {
|
|
180
|
+
if (!present.has(entry)) {
|
|
181
|
+
appended.push(entry);
|
|
182
|
+
added.push(entry);
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
return { lines: [...bodyLines, ...appended], added };
|
|
186
|
+
}
|
|
187
|
+
/**
|
|
188
|
+
* Open `.gitignore` via `O_NOFOLLOW | O_RDONLY` so a symlink that appeared
|
|
189
|
+
* after our `lstat` (TOCTOU window) cannot be followed. Darwin/Linux map
|
|
190
|
+
* `O_NOFOLLOW` to `ELOOP`; we translate that to the same symlink-refusal
|
|
191
|
+
* message the lstat path would produce.
|
|
192
|
+
*
|
|
193
|
+
* Returns `null` when the file does not exist.
|
|
194
|
+
*/
|
|
195
|
+
async function readGitignoreIfFile(absPath) {
|
|
196
|
+
let lst;
|
|
197
|
+
try {
|
|
198
|
+
lst = await fsPromises.lstat(absPath);
|
|
199
|
+
}
|
|
200
|
+
catch (err) {
|
|
201
|
+
if (err.code === 'ENOENT')
|
|
202
|
+
return null;
|
|
203
|
+
throw err;
|
|
204
|
+
}
|
|
205
|
+
if (lst.isSymbolicLink()) {
|
|
206
|
+
throw new Error(`${absPath} is a symlink — refusing to edit .gitignore through a link. ` +
|
|
207
|
+
`Replace the link with a regular file and rerun.`);
|
|
208
|
+
}
|
|
209
|
+
if (!lst.isFile()) {
|
|
210
|
+
throw new Error(`${absPath} is not a regular file (type=${String(lst.mode & 0o170000)}) — refusing to edit.`);
|
|
211
|
+
}
|
|
212
|
+
// O_NOFOLLOW closes the TOCTOU window between lstat and open on POSIX.
|
|
213
|
+
// On Windows O_NOFOLLOW is not defined — refuse to edit an existing
|
|
214
|
+
// `.gitignore` there rather than silently accept the TOCTOU hole.
|
|
215
|
+
// (Codex F1 on the bc2b77b re-review.) Consumers who still have a
|
|
216
|
+
// regular file get the lstat-only protection below; operators who end
|
|
217
|
+
// up with a symlinked .gitignore get a refusal rather than a splice.
|
|
218
|
+
const O_NOFOLLOW = fsPromises.constants?.O_NOFOLLOW;
|
|
219
|
+
const O_RDONLY = fsPromises.constants?.O_RDONLY;
|
|
220
|
+
if (O_NOFOLLOW === undefined || O_RDONLY === undefined) {
|
|
221
|
+
throw new Error(`${absPath} exists and this platform lacks O_NOFOLLOW — refusing to edit ` +
|
|
222
|
+
`an existing .gitignore without symlink-race protection. Delete the ` +
|
|
223
|
+
`file first if rea should scaffold a fresh one.`);
|
|
224
|
+
}
|
|
225
|
+
const fd = await fsPromises
|
|
226
|
+
.open(absPath, O_RDONLY | O_NOFOLLOW)
|
|
227
|
+
.catch((err) => {
|
|
228
|
+
if (err.code === 'ELOOP') {
|
|
229
|
+
throw new Error(`${absPath} became a symlink between lstat and open — refusing to read.`);
|
|
230
|
+
}
|
|
231
|
+
throw err;
|
|
232
|
+
});
|
|
233
|
+
try {
|
|
234
|
+
return await fd.readFile('utf8');
|
|
235
|
+
}
|
|
236
|
+
finally {
|
|
237
|
+
await fd.close();
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
/**
|
|
241
|
+
* Write `.gitignore` with a temp-file + rename, same pattern as the cache
|
|
242
|
+
* atomic-clear (F4). Avoids torn reads for any tool (IDE, `rea doctor`)
|
|
243
|
+
* racing this write.
|
|
244
|
+
*
|
|
245
|
+
* Temp-name uses `crypto.randomBytes(16)` (not PID/timestamp) — Codex F2
|
|
246
|
+
* flagged the old name as predictable, which gave a local attacker a way
|
|
247
|
+
* to pre-create the path and block the write (or place a FIFO on it).
|
|
248
|
+
*/
|
|
249
|
+
async function writeAtomic(absPath, content) {
|
|
250
|
+
const dir = path.dirname(absPath);
|
|
251
|
+
const rand = crypto.randomBytes(16).toString('hex');
|
|
252
|
+
const tmp = path.join(dir, `.gitignore.rea-tmp-${rand}`);
|
|
253
|
+
try {
|
|
254
|
+
await fsPromises.writeFile(tmp, content, { encoding: 'utf8', mode: 0o644 });
|
|
255
|
+
await fsPromises.rename(tmp, absPath);
|
|
256
|
+
}
|
|
257
|
+
catch (err) {
|
|
258
|
+
await fsPromises.unlink(tmp).catch(() => {
|
|
259
|
+
// Best-effort cleanup. If rename failed the tmp exists; if writeFile
|
|
260
|
+
// failed before anything landed, unlink fails with ENOENT — either way
|
|
261
|
+
// we don't want the original error masked.
|
|
262
|
+
});
|
|
263
|
+
throw err;
|
|
264
|
+
}
|
|
265
|
+
}
|
|
266
|
+
/**
|
|
267
|
+
* Main entry point. Idempotent: calling twice in a row produces `unchanged`
|
|
268
|
+
* on the second call.
|
|
269
|
+
*
|
|
270
|
+
* The `entries` parameter defaults to `REA_GITIGNORE_ENTRIES` — both `rea
|
|
271
|
+
* init` and `rea upgrade` pass the default. Tests override to verify
|
|
272
|
+
* reconciliation.
|
|
273
|
+
*/
|
|
274
|
+
export async function ensureReaGitignore(targetDir, entries = REA_GITIGNORE_ENTRIES) {
|
|
275
|
+
const absPath = path.resolve(targetDir, GITIGNORE);
|
|
276
|
+
const warnings = [];
|
|
277
|
+
let existing;
|
|
278
|
+
try {
|
|
279
|
+
existing = await readGitignoreIfFile(absPath);
|
|
280
|
+
}
|
|
281
|
+
catch (err) {
|
|
282
|
+
warnings.push(err.message);
|
|
283
|
+
return { path: absPath, action: 'unchanged', addedEntries: [], warnings };
|
|
284
|
+
}
|
|
285
|
+
// Detect EOL so a CRLF repo stays CRLF and doesn't get torn. Codex F3.
|
|
286
|
+
const eol = existing !== null && existing.includes('\r\n') ? '\r\n' : '\n';
|
|
287
|
+
if (existing === null) {
|
|
288
|
+
const content = buildManagedBlock(entries, '\n') + '\n';
|
|
289
|
+
await writeAtomic(absPath, content);
|
|
290
|
+
return {
|
|
291
|
+
path: absPath,
|
|
292
|
+
action: 'created',
|
|
293
|
+
addedEntries: [...entries],
|
|
294
|
+
warnings,
|
|
295
|
+
};
|
|
296
|
+
}
|
|
297
|
+
const lines = existing.split(/\r?\n/);
|
|
298
|
+
const hadTrailingNewline = existing.endsWith('\n');
|
|
299
|
+
const block = findManagedBlock(lines);
|
|
300
|
+
if (block === 'duplicate') {
|
|
301
|
+
warnings.push(`${absPath} contains multiple '# === rea managed' blocks — refusing to modify. ` +
|
|
302
|
+
`Consolidate the managed blocks manually and rerun.`);
|
|
303
|
+
return { path: absPath, action: 'unchanged', addedEntries: [], warnings };
|
|
304
|
+
}
|
|
305
|
+
if (block === null) {
|
|
306
|
+
// No managed block. Append one after a blank-line separator (unless the
|
|
307
|
+
// file is empty or already ends with a blank line).
|
|
308
|
+
const trimmedTailIdx = (() => {
|
|
309
|
+
let i = lines.length - 1;
|
|
310
|
+
while (i >= 0 && lines[i] === '')
|
|
311
|
+
i -= 1;
|
|
312
|
+
return i;
|
|
313
|
+
})();
|
|
314
|
+
const bodyLines = lines.slice(0, trimmedTailIdx + 1);
|
|
315
|
+
const separator = bodyLines.length === 0 ? [] : [''];
|
|
316
|
+
const newLines = [
|
|
317
|
+
...bodyLines,
|
|
318
|
+
...separator,
|
|
319
|
+
buildManagedBlock(entries, eol),
|
|
320
|
+
];
|
|
321
|
+
const content = newLines.join(eol) + eol;
|
|
322
|
+
await writeAtomic(absPath, content);
|
|
323
|
+
return {
|
|
324
|
+
path: absPath,
|
|
325
|
+
action: 'updated',
|
|
326
|
+
addedEntries: [...entries],
|
|
327
|
+
warnings,
|
|
328
|
+
};
|
|
329
|
+
}
|
|
330
|
+
// Managed block exists — reconcile body lines.
|
|
331
|
+
const bodyLines = lines.slice(block.startIdx + 1, block.endIdx);
|
|
332
|
+
const { lines: reconciledBody, added } = reconcileBlock(bodyLines, entries);
|
|
333
|
+
if (added.length === 0) {
|
|
334
|
+
return {
|
|
335
|
+
path: absPath,
|
|
336
|
+
action: 'unchanged',
|
|
337
|
+
addedEntries: [],
|
|
338
|
+
warnings,
|
|
339
|
+
};
|
|
340
|
+
}
|
|
341
|
+
const newLines = [
|
|
342
|
+
...lines.slice(0, block.startIdx + 1),
|
|
343
|
+
...reconciledBody,
|
|
344
|
+
...lines.slice(block.endIdx),
|
|
345
|
+
];
|
|
346
|
+
let content = newLines.join(eol);
|
|
347
|
+
if (hadTrailingNewline && !content.endsWith(eol))
|
|
348
|
+
content += eol;
|
|
349
|
+
await writeAtomic(absPath, content);
|
|
350
|
+
return {
|
|
351
|
+
path: absPath,
|
|
352
|
+
action: 'updated',
|
|
353
|
+
addedEntries: added,
|
|
354
|
+
warnings,
|
|
355
|
+
};
|
|
356
|
+
}
|
package/dist/cli/upgrade.js
CHANGED
|
@@ -45,6 +45,7 @@ import { CLAUDE_MD_MANIFEST_PATH, SETTINGS_MANIFEST_PATH, enumerateCanonicalFile
|
|
|
45
45
|
import { buildFragment, extractFragment, } from './install/claude-md.js';
|
|
46
46
|
import { atomicReplaceFile, safeDeleteFile, safeInstallFile, safeReadFile, } from './install/fs-safe.js';
|
|
47
47
|
import { canonicalSettingsSubsetHash, defaultDesiredHooks, mergeSettings, readSettings, writeSettingsAtomic, } from './install/settings-merge.js';
|
|
48
|
+
import { ensureReaGitignore } from './install/gitignore.js';
|
|
48
49
|
import { manifestExists, readManifest, writeManifestAtomic, } from './install/manifest-io.js';
|
|
49
50
|
import { sha256OfBuffer, sha256OfFile } from './install/sha.js';
|
|
50
51
|
import { err, getPkgVersion, log, warn } from './utils.js';
|
|
@@ -475,6 +476,25 @@ export async function runUpgrade(options = {}) {
|
|
|
475
476
|
source: 'claude-md',
|
|
476
477
|
});
|
|
477
478
|
}
|
|
479
|
+
// BUG-010 — ensure `.gitignore` carries every runtime artifact entry. This
|
|
480
|
+
// backfills older installs that predate the scaffolding in `rea init`. A
|
|
481
|
+
// consumer who upgraded from 0.3.x/0.4.0 was previously seeing
|
|
482
|
+
// `.rea/fingerprints.json` show up as an untracked file; this closes the
|
|
483
|
+
// loop without touching operator-authored gitignore lines.
|
|
484
|
+
if (!dryRun) {
|
|
485
|
+
const gi = await ensureReaGitignore(resolvedRoot);
|
|
486
|
+
if (gi.action === 'created') {
|
|
487
|
+
console.log(` + ${path.relative(resolvedRoot, gi.path)} (managed block written)`);
|
|
488
|
+
}
|
|
489
|
+
else if (gi.action === 'updated') {
|
|
490
|
+
console.log(` ~ ${path.relative(resolvedRoot, gi.path)} (managed block ${gi.addedEntries.length} entr${gi.addedEntries.length === 1 ? 'y' : 'ies'} added)`);
|
|
491
|
+
}
|
|
492
|
+
else {
|
|
493
|
+
console.log(` · ${path.relative(resolvedRoot, gi.path)} (managed block up to date)`);
|
|
494
|
+
}
|
|
495
|
+
for (const w of gi.warnings)
|
|
496
|
+
warn(w);
|
|
497
|
+
}
|
|
478
498
|
if (dryRun) {
|
|
479
499
|
console.log('');
|
|
480
500
|
log('dry run — no changes written.');
|
package/dist/policy/loader.d.ts
CHANGED
|
@@ -32,10 +32,16 @@ declare const PolicySchema: z.ZodObject<{
|
|
|
32
32
|
}>>;
|
|
33
33
|
review: z.ZodOptional<z.ZodObject<{
|
|
34
34
|
codex_required: z.ZodOptional<z.ZodBoolean>;
|
|
35
|
+
cache_max_age_seconds: z.ZodOptional<z.ZodNumber>;
|
|
36
|
+
allow_skip_in_ci: z.ZodOptional<z.ZodBoolean>;
|
|
35
37
|
}, "strict", z.ZodTypeAny, {
|
|
36
38
|
codex_required?: boolean | undefined;
|
|
39
|
+
cache_max_age_seconds?: number | undefined;
|
|
40
|
+
allow_skip_in_ci?: boolean | undefined;
|
|
37
41
|
}, {
|
|
38
42
|
codex_required?: boolean | undefined;
|
|
43
|
+
cache_max_age_seconds?: number | undefined;
|
|
44
|
+
allow_skip_in_ci?: boolean | undefined;
|
|
39
45
|
}>>;
|
|
40
46
|
redact: z.ZodOptional<z.ZodObject<{
|
|
41
47
|
match_timeout_ms: z.ZodOptional<z.ZodNumber>;
|
|
@@ -110,6 +116,8 @@ declare const PolicySchema: z.ZodObject<{
|
|
|
110
116
|
} | undefined;
|
|
111
117
|
review?: {
|
|
112
118
|
codex_required?: boolean | undefined;
|
|
119
|
+
cache_max_age_seconds?: number | undefined;
|
|
120
|
+
allow_skip_in_ci?: boolean | undefined;
|
|
113
121
|
} | undefined;
|
|
114
122
|
redact?: {
|
|
115
123
|
match_timeout_ms?: number | undefined;
|
|
@@ -146,6 +154,8 @@ declare const PolicySchema: z.ZodObject<{
|
|
|
146
154
|
} | undefined;
|
|
147
155
|
review?: {
|
|
148
156
|
codex_required?: boolean | undefined;
|
|
157
|
+
cache_max_age_seconds?: number | undefined;
|
|
158
|
+
allow_skip_in_ci?: boolean | undefined;
|
|
149
159
|
} | undefined;
|
|
150
160
|
redact?: {
|
|
151
161
|
match_timeout_ms?: number | undefined;
|
package/dist/policy/loader.js
CHANGED
|
@@ -24,6 +24,8 @@ const ContextProtectionSchema = z.object({
|
|
|
24
24
|
const ReviewPolicySchema = z
|
|
25
25
|
.object({
|
|
26
26
|
codex_required: z.boolean().optional(),
|
|
27
|
+
cache_max_age_seconds: z.number().int().positive().optional(),
|
|
28
|
+
allow_skip_in_ci: z.boolean().optional(),
|
|
27
29
|
})
|
|
28
30
|
.strict();
|
|
29
31
|
/**
|
package/dist/policy/types.d.ts
CHANGED
|
@@ -31,6 +31,26 @@ export interface ReviewPolicy {
|
|
|
31
31
|
* log. Default when unset is `true` (Codex required).
|
|
32
32
|
*/
|
|
33
33
|
codex_required?: boolean;
|
|
34
|
+
/**
|
|
35
|
+
* Review-cache TTL used by `rea cache check` (BUG-009). Entries older
|
|
36
|
+
* than this window are treated as a miss, forcing re-review. Default
|
|
37
|
+
* when unset is 3600 seconds (1 hour) — matches the windows the
|
|
38
|
+
* push-review-gate hook already assumes. Express in seconds, positive
|
|
39
|
+
* integer.
|
|
40
|
+
*/
|
|
41
|
+
cache_max_age_seconds?: number;
|
|
42
|
+
/**
|
|
43
|
+
* Authorization for `REA_SKIP_PUSH_REVIEW` / `REA_SKIP_CODEX_REVIEW` when
|
|
44
|
+
* the `CI` environment variable is set. The skip hatches are ambient and
|
|
45
|
+
* unauthenticated — a leaked env file or a malicious parent process can
|
|
46
|
+
* bypass the gate and record a forged actor (git config is mutable repo
|
|
47
|
+
* config). Refusing these hatches in CI contexts by default removes that
|
|
48
|
+
* bypass surface. Set `true` ONLY on build agents where the operator has
|
|
49
|
+
* an independent reason to trust the environment. Default `false`.
|
|
50
|
+
*
|
|
51
|
+
* Added in 0.5.0 as Codex F2 on the PR1 adversarial review.
|
|
52
|
+
*/
|
|
53
|
+
allow_skip_in_ci?: boolean;
|
|
34
54
|
}
|
|
35
55
|
/**
|
|
36
56
|
* User-supplied redaction pattern entry. Each pattern has a stable `name` used
|