@pugi/cli 0.1.0-alpha.9 → 0.1.0-beta.2

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 (68) hide show
  1. package/README.md +33 -0
  2. package/assets/pugi-mascot.ansi +41 -0
  3. package/dist/commands/deploy.js +439 -0
  4. package/dist/core/agents/loader.js +104 -0
  5. package/dist/core/agents/registry.js +1 -1
  6. package/dist/core/consensus/anvil-fanout.js +276 -0
  7. package/dist/core/consensus/diff-capture.js +382 -0
  8. package/dist/core/consensus/rubric.js +233 -0
  9. package/dist/core/context/index.js +21 -0
  10. package/dist/core/context/pugiignore.js +316 -0
  11. package/dist/core/context/repo-skeleton.js +533 -0
  12. package/dist/core/context/watcher.js +342 -0
  13. package/dist/core/context/working-set.js +165 -0
  14. package/dist/core/edits/dispatch.js +185 -0
  15. package/dist/core/edits/index.js +15 -0
  16. package/dist/core/edits/layer-a-apply.js +217 -0
  17. package/dist/core/edits/layer-b-apply.js +211 -0
  18. package/dist/core/edits/layer-c-apply.js +160 -0
  19. package/dist/core/edits/layer-d-ast.js +29 -0
  20. package/dist/core/edits/marker-parser.js +401 -0
  21. package/dist/core/edits/security-gate.js +223 -0
  22. package/dist/core/edits/worktree.js +229 -0
  23. package/dist/core/engine/native-pugi.js +6 -1
  24. package/dist/core/engine/prompts.js +4 -1
  25. package/dist/core/engine/tool-bridge.js +33 -1
  26. package/dist/core/lsp/client.js +631 -0
  27. package/dist/core/repl/ask.js +512 -0
  28. package/dist/core/repl/cancellation.js +98 -0
  29. package/dist/core/repl/dispatch-fsm.js +220 -0
  30. package/dist/core/repl/privacy-banner.js +71 -0
  31. package/dist/core/repl/session.js +1896 -13
  32. package/dist/core/repl/slash-commands.js +59 -32
  33. package/dist/core/repl/store/index.js +12 -0
  34. package/dist/core/repl/store/jsonl-log.js +321 -0
  35. package/dist/core/repl/store/lockfile.js +155 -0
  36. package/dist/core/repl/store/session-store.js +792 -0
  37. package/dist/core/repl/store/types.js +44 -0
  38. package/dist/core/repl/store/uuid-v7.js +68 -0
  39. package/dist/core/repl/workspace-context.js +72 -1
  40. package/dist/core/skills/loader.js +454 -0
  41. package/dist/core/skills/sources.js +480 -0
  42. package/dist/core/skills/trust.js +172 -0
  43. package/dist/runtime/cli.js +767 -10
  44. package/dist/runtime/commands/agents.js +385 -0
  45. package/dist/runtime/commands/config.js +338 -8
  46. package/dist/runtime/commands/lsp.js +184 -0
  47. package/dist/runtime/commands/patch.js +111 -0
  48. package/dist/runtime/commands/review-consensus.js +399 -0
  49. package/dist/runtime/commands/skills.js +401 -0
  50. package/dist/runtime/commands/worktree.js +133 -0
  51. package/dist/tools/apply-patch.js +314 -0
  52. package/dist/tools/file-tools.js +90 -0
  53. package/dist/tools/lsp-tools.js +189 -0
  54. package/dist/tools/registry.js +18 -0
  55. package/dist/tools/web-fetch.js +1 -1
  56. package/dist/tui/agent-tree-pane.js +9 -0
  57. package/dist/tui/ask-cli.js +52 -0
  58. package/dist/tui/ask-modal.js +211 -0
  59. package/dist/tui/conversation-pane.js +48 -3
  60. package/dist/tui/input-box.js +48 -5
  61. package/dist/tui/markdown-render.js +266 -0
  62. package/dist/tui/repl-render.js +185 -0
  63. package/dist/tui/repl-splash-mascot.js +130 -0
  64. package/dist/tui/repl-splash.js +7 -1
  65. package/dist/tui/repl.js +82 -11
  66. package/dist/tui/status-bar.js +63 -3
  67. package/dist/tui/tool-stream-pane.js +91 -0
  68. package/package.json +11 -5
@@ -0,0 +1,382 @@
1
+ /**
2
+ * Diff capture — `pugi review --consensus` (α6.7).
3
+ *
4
+ * Captures the diff that the consensus fan-out will send to Anvil. Four
5
+ * supported source kinds (in order of precedence):
6
+ *
7
+ * 1. `--pr <number>` — uses `gh pr diff <num>` (gh CLI required).
8
+ * 2. `--commit <sha>` — diff of that commit vs its first parent.
9
+ * 3. `--branch <name>` — diff of HEAD vs `origin/<name>` merge-base.
10
+ * 4. (default) — diff of HEAD vs `origin/main` merge-base
11
+ * covering BOTH committed-since-base AND
12
+ * uncommitted (staged + working tree) edits.
13
+ *
14
+ * The shape mirrors the existing `performRemoteTripleReview` flow:
15
+ * uncommitted edits are deliberately included by computing the diff
16
+ * against the merge-base SHA rather than `base...HEAD`, otherwise the
17
+ * common case ("review what I'm about to commit") would lose signal.
18
+ *
19
+ * Protected paths (`.env*`, `*.key`, `*.pem`, `*.sql` etc) are excluded
20
+ * at the git layer so a secret cannot leak into the egress payload even
21
+ * if the operator has it staged.
22
+ */
23
+ import { execFileSync } from 'node:child_process';
24
+ /**
25
+ * Hard cap on the diff payload sent egress. Anvil enforces its own cap
26
+ * server-side; this is a defense-in-depth so a runaway monorepo merge
27
+ * doesn't OOM the SSE encoder. 1 MiB ≈ 30k LOC, which is well above the
28
+ * largest review the rubric can reason about.
29
+ */
30
+ export const DIFF_MAX_BYTES = 1 * 1024 * 1024;
31
+ /**
32
+ * Git pathspec exclusions for sensitive blobs. This is the source of truth
33
+ * for both the consensus surface AND the legacy `PROTECTED_DIFF_EXCLUDES`
34
+ * in cli.ts; keep both lists in sync when adding new patterns.
35
+ *
36
+ * Coverage policy: a credential committed under ANY plausible filename
37
+ * pattern must be excluded. Adversarial PRs can stage secrets under
38
+ * unconventional names (deploy.crt, credentials, .netrc) to bypass a
39
+ * narrow exclude list and exfiltrate to the reviewer payload.
40
+ *
41
+ * Pathspec form `:(exclude,glob)<starstar>/<pattern>` (where `<starstar>`
42
+ * is the `*` `*` doubled glob) matches at the repo root AND in any
43
+ * subdirectory; without the doubled-star prefix git's literal pathspec
44
+ * syntax silently misses subdir matches in pnpm/turbo monorepos.
45
+ */
46
+ export const PROTECTED_PATHSPEC_EXCLUDES = Object.freeze([
47
+ // Dotfiles + RC files that frequently hold tokens.
48
+ ':(exclude,glob)**/.env',
49
+ ':(exclude,glob)**/.env.*',
50
+ ':(exclude,glob)**/.npmrc',
51
+ ':(exclude,glob)**/.yarnrc',
52
+ ':(exclude,glob)**/.pypirc',
53
+ ':(exclude,glob)**/.gitconfig',
54
+ ':(exclude,glob)**/.netrc',
55
+ // SSH private keys (every algorithm we have seen committed in the wild).
56
+ ':(exclude,glob)**/id_rsa',
57
+ ':(exclude,glob)**/id_ed25519',
58
+ ':(exclude,glob)**/id_ecdsa',
59
+ ':(exclude,glob)**/id_dsa',
60
+ // PEM-encoded + DER-encoded private keys / certs / containers.
61
+ ':(exclude,glob)**/*.pem',
62
+ ':(exclude,glob)**/*.key',
63
+ ':(exclude,glob)**/*.crt',
64
+ ':(exclude,glob)**/*.cer',
65
+ ':(exclude,glob)**/*.der',
66
+ ':(exclude,glob)**/*.pfx',
67
+ ':(exclude,glob)**/*.p12',
68
+ // SQL dumps / DB exports often contain real PII + credentials.
69
+ ':(exclude,glob)**/*.dump',
70
+ ':(exclude,glob)**/*.sql',
71
+ // Generic credential blobs under any directory.
72
+ ':(exclude,glob)**/*.secret',
73
+ ':(exclude,glob)**/credentials',
74
+ ':(exclude,glob)**/credentials.json',
75
+ // `secrets/**` (not `secrets/*`) so nested credential paths recurse:
76
+ // `secrets/prod/token.txt`, `apps/foo/secrets/nested/key`, and any
77
+ // arbitrarily deep `**/secrets/<...>/<file>` get excluded. With glob
78
+ // pathspec magic enabled, a single `*` does NOT cross path separators,
79
+ // so the non-recursive form would leak nested-directory secrets.
80
+ ':(exclude,glob)**/secrets/**',
81
+ ]);
82
+ /**
83
+ * Captures the diff per the source spec and returns the augmented payload
84
+ * plus narrative context (branch, commit, title) that gets attached to
85
+ * the egress request.
86
+ *
87
+ * Errors are returned as thrown `Error` instances; the caller (the
88
+ * command handler) translates them to JSON error payloads + exit codes
89
+ * so the CLI never crashes on a malformed ref.
90
+ */
91
+ export function captureDiff(spec) {
92
+ const cwd = spec.cwd ?? process.cwd();
93
+ // Source precedence: pr > commit > branch > default.
94
+ if (typeof spec.pr === 'number' && Number.isFinite(spec.pr) && spec.pr > 0) {
95
+ return captureFromPr(cwd, spec.pr);
96
+ }
97
+ if (typeof spec.commit === 'string' && spec.commit.length > 0) {
98
+ return captureFromCommit(cwd, spec.commit);
99
+ }
100
+ if (typeof spec.branch === 'string' && spec.branch.length > 0) {
101
+ return captureFromBranch(cwd, spec.branch, spec.baseRef ?? 'origin/main');
102
+ }
103
+ return captureFromBase(cwd, spec.baseRef ?? 'origin/main');
104
+ }
105
+ function captureFromPr(cwd, pr) {
106
+ // CRITICAL: `gh pr diff <num>` bypasses PROTECTED_PATHSPEC_EXCLUDES,
107
+ // exfiltrating `.env`, `*.key`, `*.pem`, `*.sql`, secrets/* to the
108
+ // reviewer payload. We instead fetch the PR head ref locally and run
109
+ // `git diff` with the same pathspec excludes as every other capture
110
+ // path. PR metadata still comes from `gh pr view` (read-only).
111
+ const metaRaw = safeExec(cwd, 'gh', ['pr', 'view', String(pr), '--json', 'title,headRefName,headRefOid,baseRefName']);
112
+ const meta = safeParseJson(metaRaw);
113
+ const tempRef = `refs/pugi/consensus-pr-${pr}`;
114
+ // Fetch the PR head into a private ref so we have local objects to
115
+ // diff against. `pull/<num>/head` is GitHub's special refspec exposed
116
+ // to anyone with read access on the repo.
117
+ safeExec(cwd, 'git', ['fetch', 'origin', `pull/${pr}/head:${tempRef}`]);
118
+ try {
119
+ // Resolve the base ref to diff against. Prefer the PR's declared
120
+ // base; fall back to `origin/main`. We compute the merge-base so a
121
+ // PR that's behind main still shows only the author's hunks.
122
+ const baseRef = meta?.baseRefName ? `origin/${meta.baseRefName}` : 'origin/main';
123
+ const mergeBase = safeExecOptional(cwd, 'git', ['merge-base', baseRef, tempRef]).trim();
124
+ const range = mergeBase ? `${mergeBase}..${tempRef}` : `${baseRef}..${tempRef}`;
125
+ const diff = safeExec(cwd, 'git', [
126
+ 'diff',
127
+ range,
128
+ '--',
129
+ '.',
130
+ ...PROTECTED_PATHSPEC_EXCLUDES,
131
+ ]);
132
+ const cappedDiff = capDiff(diff);
133
+ const stats = computeStats(cappedDiff);
134
+ return {
135
+ diff: cappedDiff,
136
+ context: {
137
+ branch: meta?.headRefName ?? `pr-${pr}`,
138
+ commit: shortSha(meta?.headRefOid ?? ''),
139
+ title: meta?.title ?? `PR #${pr}`,
140
+ ref: `pr:${pr}`,
141
+ stats,
142
+ },
143
+ };
144
+ }
145
+ finally {
146
+ // Best-effort cleanup of the private ref. Never throw from the
147
+ // cleanup path so the operator's primary error (if any) reaches them.
148
+ try {
149
+ safeExec(cwd, 'git', ['update-ref', '-d', tempRef]);
150
+ }
151
+ catch {
152
+ // Swallow: a leftover ref under refs/pugi/ is harmless. The next
153
+ // run will overwrite it via `fetch ... :ref` anyway.
154
+ }
155
+ }
156
+ }
157
+ function captureFromCommit(cwd, commit) {
158
+ // `<sha>~1..<sha>` covers exactly that commit's changes. For a ROOT
159
+ // commit (no parent) the `~1` lookup explodes and produces an empty
160
+ // diff masquerading as success. Detect this up front and fall back
161
+ // to the git empty-tree sha so the first commit's introduction shows
162
+ // up in the diff.
163
+ const fullSha = safeExec(cwd, 'git', ['rev-parse', commit]).trim();
164
+ if (!fullSha)
165
+ throw new Error(`Unknown commit ref: ${commit}`);
166
+ // Probe for a parent. `rev-parse --verify <sha>~1` exits non-zero on
167
+ // a root commit; we treat that as "diff against the empty tree".
168
+ const hasParent = safeExecOptional(cwd, 'git', ['rev-parse', '--verify', `${fullSha}~1`]).trim().length > 0;
169
+ // The well-known git "empty tree" SHA. Stable across all git versions
170
+ // since 2005; documented in `git hash-object -t tree /dev/null`.
171
+ const EMPTY_TREE_SHA = '4b825dc642cb6eb9a060e54bf8d69288fbee4904';
172
+ const range = hasParent ? `${fullSha}~1..${fullSha}` : `${EMPTY_TREE_SHA}..${fullSha}`;
173
+ const diff = safeExec(cwd, 'git', [
174
+ 'diff',
175
+ range,
176
+ '--',
177
+ '.',
178
+ ...PROTECTED_PATHSPEC_EXCLUDES,
179
+ ]);
180
+ const cappedDiff = capDiff(diff);
181
+ const subject = safeExec(cwd, 'git', ['log', '-1', '--pretty=%s', fullSha]).trim();
182
+ const branch = safeExec(cwd, 'git', ['name-rev', '--name-only', fullSha]).trim() || 'detached';
183
+ const stats = computeStats(cappedDiff);
184
+ return {
185
+ diff: cappedDiff,
186
+ context: {
187
+ branch,
188
+ commit: shortSha(fullSha),
189
+ title: subject || `commit ${shortSha(fullSha)}`,
190
+ ref: `commit:${shortSha(fullSha)}`,
191
+ stats,
192
+ },
193
+ };
194
+ }
195
+ function captureFromBranch(cwd, branch, baseRef) {
196
+ const remoteRef = branch.includes('/') ? branch : `origin/${branch}`;
197
+ const mergeBase = safeExec(cwd, 'git', ['merge-base', baseRef, remoteRef]).trim();
198
+ if (!mergeBase)
199
+ throw new Error(`Cannot compute merge-base of ${baseRef} and ${remoteRef}`);
200
+ const diff = safeExec(cwd, 'git', [
201
+ 'diff',
202
+ `${mergeBase}..${remoteRef}`,
203
+ '--',
204
+ '.',
205
+ ...PROTECTED_PATHSPEC_EXCLUDES,
206
+ ]);
207
+ const cappedDiff = capDiff(diff);
208
+ const head = safeExec(cwd, 'git', ['rev-parse', remoteRef]).trim();
209
+ const subject = safeExec(cwd, 'git', ['log', '-1', '--pretty=%s', remoteRef]).trim();
210
+ const stats = computeStats(cappedDiff);
211
+ return {
212
+ diff: cappedDiff,
213
+ context: {
214
+ branch,
215
+ commit: shortSha(head),
216
+ title: subject || `branch ${branch}`,
217
+ ref: `branch:${branch}`,
218
+ stats,
219
+ },
220
+ };
221
+ }
222
+ function captureFromBase(cwd, baseRef) {
223
+ // The default surface — diff HEAD against the merge-base of the
224
+ // protected base. When the merge-base lookup fails (shallow clone,
225
+ // no upstream, baseRef not configured), fall back to the working-tree
226
+ // diff so the consensus gate still has signal rather than crashing.
227
+ const mergeBase = safeExecOptional(cwd, 'git', ['merge-base', baseRef, 'HEAD']).trim();
228
+ if (mergeBase) {
229
+ // Two parts (non-overlapping):
230
+ // 1. Committed since base: `<base>..HEAD`
231
+ // 2. Uncommitted (staged + working tree as a single union): `git diff HEAD`
232
+ // `git diff HEAD` already reports BOTH staged AND working-tree
233
+ // changes relative to HEAD, so we MUST NOT add a separate
234
+ // `--cached` invocation: doing so emits the same staged hunks
235
+ // twice, inflating reviewer cost and confusing the rubric on
236
+ // duplicate-finding correlation.
237
+ const committedDiff = safeExec(cwd, 'git', [
238
+ 'diff',
239
+ `${mergeBase}..HEAD`,
240
+ '--',
241
+ '.',
242
+ ...PROTECTED_PATHSPEC_EXCLUDES,
243
+ ]);
244
+ const uncommittedDiff = safeExec(cwd, 'git', [
245
+ 'diff',
246
+ 'HEAD',
247
+ '--',
248
+ '.',
249
+ ...PROTECTED_PATHSPEC_EXCLUDES,
250
+ ]);
251
+ const combined = [committedDiff, uncommittedDiff]
252
+ .map((s) => s.trim())
253
+ .filter((s) => s.length > 0)
254
+ .join('\n');
255
+ const cappedDiff = capDiff(combined);
256
+ const branch = safeExec(cwd, 'git', ['branch', '--show-current']).trim() || 'detached';
257
+ const head = safeExec(cwd, 'git', ['rev-parse', 'HEAD']).trim();
258
+ const subject = safeExec(cwd, 'git', ['log', '-1', '--pretty=%s', 'HEAD']).trim();
259
+ const stats = computeStats(cappedDiff);
260
+ return {
261
+ diff: cappedDiff,
262
+ context: {
263
+ branch,
264
+ commit: shortSha(head),
265
+ title: subject || branch,
266
+ ref: `merge-base:${baseRef}`,
267
+ stats,
268
+ },
269
+ };
270
+ }
271
+ // Fallback path: no merge-base available. `git diff HEAD` reports
272
+ // BOTH staged AND working-tree changes relative to HEAD in a single
273
+ // unified diff, so it's the right one-shot capture for "what would I
274
+ // commit if I ran `git add -A && git commit` right now". A separate
275
+ // `--cached` call would double-report the staged hunks.
276
+ const cappedDiff = capDiff(safeExec(cwd, 'git', ['diff', 'HEAD', '--', '.', ...PROTECTED_PATHSPEC_EXCLUDES]));
277
+ const branch = safeExec(cwd, 'git', ['branch', '--show-current']).trim() || 'detached';
278
+ const head = safeExec(cwd, 'git', ['rev-parse', 'HEAD']).trim();
279
+ const subject = safeExec(cwd, 'git', ['log', '-1', '--pretty=%s', 'HEAD']).trim();
280
+ const stats = computeStats(cappedDiff);
281
+ return {
282
+ diff: cappedDiff,
283
+ context: {
284
+ branch,
285
+ commit: shortSha(head),
286
+ title: subject || branch,
287
+ ref: 'head-only',
288
+ stats,
289
+ },
290
+ };
291
+ }
292
+ /**
293
+ * Non-throwing variant of `safeExec`. Returns an empty string on
294
+ * non-zero exit instead of throwing. Used for the optional `merge-base`
295
+ * lookup which fails legitimately in shallow clones / no-upstream setups.
296
+ */
297
+ function safeExecOptional(cwd, file, args) {
298
+ try {
299
+ return safeExec(cwd, file, args);
300
+ }
301
+ catch {
302
+ return '';
303
+ }
304
+ }
305
+ /** Compute file / insertion / deletion counts from a unified diff. */
306
+ function computeStats(diff) {
307
+ let filesChanged = 0;
308
+ let insertions = 0;
309
+ let deletions = 0;
310
+ for (const line of diff.split(/\r?\n/)) {
311
+ if (line.startsWith('diff --git '))
312
+ filesChanged += 1;
313
+ else if (line.startsWith('+') && !line.startsWith('+++'))
314
+ insertions += 1;
315
+ else if (line.startsWith('-') && !line.startsWith('---'))
316
+ deletions += 1;
317
+ }
318
+ return { filesChanged, insertions, deletions };
319
+ }
320
+ /**
321
+ * Truncate the diff if it grows past `DIFF_MAX_BYTES`. Truncation is
322
+ * marked with a sentinel comment so reviewers see the cap explicitly
323
+ * instead of silently reasoning over a partial patch.
324
+ */
325
+ function capDiff(diff) {
326
+ // `Buffer.byteLength` is required — `.length` counts UTF-16 code units
327
+ // and underestimates multi-byte sequences common in diffs that touch
328
+ // i18n / cyrillic content.
329
+ if (Buffer.byteLength(diff, 'utf8') <= DIFF_MAX_BYTES)
330
+ return diff;
331
+ // Slice by code units, then re-check byte length. UTF-8 is variable-
332
+ // width, so 1 MiB of code units can exceed the cap; iterate until it
333
+ // fits. Two passes is the worst case for any reasonable input.
334
+ let slice = diff.slice(0, DIFF_MAX_BYTES);
335
+ while (Buffer.byteLength(slice, 'utf8') > DIFF_MAX_BYTES && slice.length > 0) {
336
+ slice = slice.slice(0, slice.length - 1024);
337
+ }
338
+ return `${slice}\n\n# [pugi-cli] diff truncated at ${DIFF_MAX_BYTES} bytes; reviewers see a partial patch.\n`;
339
+ }
340
+ function shortSha(sha) {
341
+ if (!sha)
342
+ return '';
343
+ return sha.length > 7 ? sha.slice(0, 7) : sha;
344
+ }
345
+ /**
346
+ * Wrapper around `execFileSync` that returns stdout as a UTF-8 string,
347
+ * swallows stderr, and throws with a stable shape on non-zero exit.
348
+ *
349
+ * The `execFileSync` form avoids shell injection (no shell process is
350
+ * spawned), which matters because we pass user-supplied refs / branch
351
+ * names into the command line.
352
+ */
353
+ function safeExec(cwd, file, args) {
354
+ try {
355
+ const out = execFileSync(file, args, {
356
+ cwd,
357
+ stdio: ['ignore', 'pipe', 'pipe'],
358
+ // 32 MiB buffer — covers the worst-case PR diff before our 1 MiB
359
+ // cap kicks in. The cap is applied after capture so we can report
360
+ // truncation honestly.
361
+ maxBuffer: 32 * 1024 * 1024,
362
+ encoding: 'utf8',
363
+ });
364
+ // Specifying `encoding: 'utf8'` narrows the return type to string,
365
+ // but TS still types `out` as `string` always here — defensively
366
+ // coerce via `String()` to satisfy lint without an `as` cast.
367
+ return String(out);
368
+ }
369
+ catch (error) {
370
+ const message = error instanceof Error ? error.message : String(error);
371
+ throw new Error(`${file} ${args.slice(0, 2).join(' ')} failed: ${message.split('\n')[0]}`);
372
+ }
373
+ }
374
+ function safeParseJson(raw) {
375
+ try {
376
+ return JSON.parse(raw);
377
+ }
378
+ catch {
379
+ return null;
380
+ }
381
+ }
382
+ //# sourceMappingURL=diff-capture.js.map
@@ -0,0 +1,233 @@
1
+ /**
2
+ * Consensus rubric — `pugi review --consensus` (α6.7).
3
+ *
4
+ * Three independent reviewers (Codex / Claude / DeepSeek) produce findings
5
+ * tagged `[P0]` / `[P1]` / `[P2]` / `[P3]`. The rubric translates the per-
6
+ * reviewer severity vector into one of `PASS` / `WARN` / `BLOCK`.
7
+ *
8
+ * Rubric (verbatim from /triple-review skill + admin-api OES MCP triple_review):
9
+ *
10
+ * any reviewer reports [P0] -> BLOCK
11
+ * two or more reviewers report [P1] -> BLOCK (consensus)
12
+ * exactly one reviewer reports [P1] -> WARN (asymmetric)
13
+ * no reviewer reports [P0] or [P1] -> PASS (P2/P3 only)
14
+ * every reviewer errored -> BLOCK (no signal)
15
+ *
16
+ * The rubric never reads model text beyond the severity markers; the
17
+ * reviewer-side narrative is shown to the operator unchanged. Keeping the
18
+ * verdict deterministic + LLM-free is the entire point of the gate (CEO
19
+ * directive 2026-05-19): a model that disagrees with the rubric can be
20
+ * audited, a model that produces the verdict cannot.
21
+ */
22
+ /**
23
+ * Regex matches the `[P0]` / `[P1]` / `[P2]` / `[P3]` token at the start
24
+ * of a line OR inline. Accepts surrounding whitespace and lowercase form.
25
+ * The line-anchored `m` flag scans every line; `g` allows `matchAll`.
26
+ *
27
+ * The capture group on the digit lets `parseFindings` reconstruct the
28
+ * severity without a second regex. Square brackets are escaped because
29
+ * the JS regex engine treats `[` as a character class otherwise.
30
+ */
31
+ const SEVERITY_TOKEN = /\[\s*[Pp]([0-3])\s*\]/g;
32
+ /**
33
+ * Parse a raw reviewer text blob into structured findings.
34
+ *
35
+ * Heuristics (intentionally permissive — different models format very
36
+ * differently and a strict parser would drop signal):
37
+ *
38
+ * 1. Split the text on `[Px]` tokens, preserving the marker.
39
+ * 2. Each marker starts a new finding. The summary is the rest of the
40
+ * same line (and the next line if the first is `:` or empty after
41
+ * stripping whitespace).
42
+ * 3. Empty / whitespace-only summaries are dropped — a bare `[P1]`
43
+ * with no context cannot be acted on, and treating it as a finding
44
+ * would falsely trigger consensus.
45
+ */
46
+ export function parseFindings(raw) {
47
+ if (typeof raw !== 'string' || raw.length === 0)
48
+ return [];
49
+ const findings = [];
50
+ // Track marker positions so we can slice the summary up to the next
51
+ // marker without quadratic re-scans.
52
+ const markers = [];
53
+ // Reset lastIndex defensively — `matchAll` allocates its own iterator,
54
+ // but belt-and-braces against a future caller passing a stateful regex.
55
+ SEVERITY_TOKEN.lastIndex = 0;
56
+ for (const match of raw.matchAll(SEVERITY_TOKEN)) {
57
+ const digit = match[1] ?? '';
58
+ const severity = `P${digit}`;
59
+ markers.push({ severity, index: match.index ?? 0, matchLength: match[0].length });
60
+ }
61
+ for (let i = 0; i < markers.length; i += 1) {
62
+ const marker = markers[i];
63
+ const start = marker.index + marker.matchLength;
64
+ const end = i + 1 < markers.length ? markers[i + 1].index : raw.length;
65
+ const slice = raw.slice(start, end);
66
+ const summary = extractSummary(slice);
67
+ if (summary.length === 0)
68
+ continue;
69
+ findings.push({ severity: marker.severity, summary });
70
+ }
71
+ return findings;
72
+ }
73
+ /**
74
+ * Extract a single-line summary from the text following a severity
75
+ * marker. Trims leading colon / dash / whitespace; truncates at the
76
+ * first newline so multi-paragraph findings render as one line in the
77
+ * REPL transcript (the full reviewer text stays available in `rawContent`).
78
+ */
79
+ function extractSummary(slice) {
80
+ let cursor = 0;
81
+ while (cursor < slice.length && /[\s:\-—–]/.test(slice[cursor]))
82
+ cursor += 1;
83
+ const tail = slice.slice(cursor);
84
+ const newlineIdx = tail.search(/\r?\n/);
85
+ const oneLine = newlineIdx === -1 ? tail : tail.slice(0, newlineIdx);
86
+ return oneLine.trim();
87
+ }
88
+ /**
89
+ * Compute the highest BLOCKING severity from a finding list. Returns
90
+ * `null` when the reviewer is clean for gating purposes, i.e. either:
91
+ * - no findings at all, OR
92
+ * - only P2 / P3 findings (informational, non-blocking by rubric).
93
+ *
94
+ * `null` is the right contract for downstream tooling that gates on
95
+ * "did this reviewer flag anything that should block ship?" - the
96
+ * rubric in `aggregate` already treats P2/P3 as non-blocking, so an
97
+ * external `topSeverity === null` check matches the gate's semantics
98
+ * exactly without re-parsing the finding list.
99
+ *
100
+ * Per-finding severity is still preserved in `findings[].severity`
101
+ * for callers that want to surface P2/P3 counts in their UX.
102
+ *
103
+ * Returns the priority floor for blocking findings only: P0 > P1.
104
+ */
105
+ export function topSeverityOf(findings) {
106
+ let best = null;
107
+ for (const finding of findings) {
108
+ if (finding.severity === 'P0')
109
+ return 'P0';
110
+ if (finding.severity === 'P1')
111
+ best = 'P1';
112
+ // P2 / P3 are non-blocking by rubric -> they do NOT contribute to
113
+ // topSeverity. They remain visible via `findings[].severity` for
114
+ // operators who want to see the full breakdown.
115
+ }
116
+ return best;
117
+ }
118
+ /**
119
+ * Convenience: parse a raw reviewer text blob into a `ReviewerVerdict`.
120
+ * `errored` defaults to false; callers that detected a transport-level
121
+ * failure should set it true and pass an empty raw string.
122
+ */
123
+ export function reviewerVerdictFromRaw(reviewer, raw, errored = false) {
124
+ if (errored) {
125
+ return { reviewer, topSeverity: null, findings: [], errored: true };
126
+ }
127
+ const findings = parseFindings(raw);
128
+ return {
129
+ reviewer,
130
+ topSeverity: topSeverityOf(findings),
131
+ findings,
132
+ errored: false,
133
+ };
134
+ }
135
+ /**
136
+ * Apply the rubric to 1..N reviewer verdicts. The shape is N-aware so the
137
+ * same function handles the dev `/triple-review` 3-reviewer case AND a
138
+ * customer running with 2 reviewers (e.g. a tier that does not yet include
139
+ * DeepSeek).
140
+ */
141
+ export function aggregate(verdicts) {
142
+ const totalReviewers = verdicts.length;
143
+ const erroredReviewers = verdicts.filter((v) => v.errored).length;
144
+ // Zero reviewers = zero signal. Falling through to the no-P0/no-P1
145
+ // branch would emit a false PASS — exactly the regression flagged by
146
+ // Codex during PR #370 review. Treat empty input as BLOCK so the gate
147
+ // fails closed when the backend returns no events at all (5xx that
148
+ // somehow drained the SSE, server-side bug, dispatcher misconfig).
149
+ if (totalReviewers === 0) {
150
+ return {
151
+ verdict: 'BLOCK',
152
+ p0Count: 0,
153
+ p1Count: 0,
154
+ p1Reviewers: 0,
155
+ reasoning: 'No reviewer signal: backend returned 0 events. Fail-closed BLOCK.',
156
+ };
157
+ }
158
+ if (totalReviewers > 0 && erroredReviewers === totalReviewers) {
159
+ return {
160
+ verdict: 'BLOCK',
161
+ p0Count: 0,
162
+ p1Count: 0,
163
+ p1Reviewers: 0,
164
+ reasoning: 'Every reviewer errored: no signal. Treating as BLOCK until at least one reviewer returns.',
165
+ };
166
+ }
167
+ let p0Count = 0;
168
+ let p1Count = 0;
169
+ let p1Reviewers = 0;
170
+ for (const verdict of verdicts) {
171
+ if (verdict.errored)
172
+ continue;
173
+ const p0 = verdict.findings.filter((f) => f.severity === 'P0').length;
174
+ const p1 = verdict.findings.filter((f) => f.severity === 'P1').length;
175
+ p0Count += p0;
176
+ p1Count += p1;
177
+ if (p1 > 0)
178
+ p1Reviewers += 1;
179
+ }
180
+ if (p0Count > 0) {
181
+ return {
182
+ verdict: 'BLOCK',
183
+ p0Count,
184
+ p1Count,
185
+ p1Reviewers,
186
+ reasoning: `${p0Count}x P0 finding${p0Count === 1 ? '' : 's'}: BLOCK (any P0 fails the gate).`,
187
+ };
188
+ }
189
+ if (p1Reviewers >= 2) {
190
+ return {
191
+ verdict: 'BLOCK',
192
+ p0Count,
193
+ p1Count,
194
+ p1Reviewers,
195
+ reasoning: `${p1Reviewers} reviewers each reported P1: consensus = likely real bug, BLOCK.`,
196
+ };
197
+ }
198
+ if (p1Reviewers === 1) {
199
+ return {
200
+ verdict: 'WARN',
201
+ p0Count,
202
+ p1Count,
203
+ p1Reviewers,
204
+ reasoning: 'One reviewer reported P1: asymmetric signal, examine the disagreement before merging.',
205
+ };
206
+ }
207
+ return {
208
+ verdict: 'PASS',
209
+ p0Count,
210
+ p1Count,
211
+ p1Reviewers,
212
+ reasoning: 'No P0 or P1 findings: PASS. P2/P3 findings are non-blocking.',
213
+ };
214
+ }
215
+ /**
216
+ * Map a rubric verdict to the conventional exit code Pugi CLI uses for
217
+ * gates (spec α6.7):
218
+ *
219
+ * PASS -> 0
220
+ * WARN -> 1
221
+ * BLOCK -> 2
222
+ *
223
+ * The non-zero codes are distinct so a shell script can branch on the
224
+ * exact outcome without re-parsing stdout.
225
+ */
226
+ export function exitCodeFor(verdict) {
227
+ if (verdict === 'PASS')
228
+ return 0;
229
+ if (verdict === 'WARN')
230
+ return 1;
231
+ return 2;
232
+ }
233
+ //# sourceMappingURL=rubric.js.map
@@ -0,0 +1,21 @@
1
+ /**
2
+ * Three-tier context model - α6.5 Phase 1 barrel.
3
+ *
4
+ * Bundles the four primitives so the REPL bootstrap can import from a
5
+ * single path:
6
+ *
7
+ * import {
8
+ * loadPugiIgnore,
9
+ * buildRepoSkeleton,
10
+ * renderSkeleton,
11
+ * WorkingSet,
12
+ * PugiWatcher,
13
+ * } from '../context/index.js';
14
+ *
15
+ * No new logic lives here - just re-exports.
16
+ */
17
+ export { BASELINE_IGNORE_PATTERNS, SECRET_IGNORE_PATTERNS, globalPugiIgnorePath, loadPugiIgnore, parsePatternText, readPatternFile, workspaceGitIgnorePath, workspacePugiIgnorePath, } from './pugiignore.js';
18
+ export { COLLAPSE_DIR_ENTRIES, MAX_README_LINES, MAX_SKELETON_BYTES, MAX_TREE_DEPTH, MAX_WALK_NODES, TOP_LANGUAGES, buildRepoSkeleton, detectPackageManager, languageForExtension, readGitBranch, readPackageJson, readReadme, renderSkeleton, topLanguages, } from './repo-skeleton.js';
19
+ export { DEFAULT_WORKING_SET_CAPACITY, WorkingSet, } from './working-set.js';
20
+ export { MAX_WATCHED_PATHS, PugiWatcher, THROTTLE_WINDOW_MS, } from './watcher.js';
21
+ //# sourceMappingURL=index.js.map