@codedology/code-forge 0.7.6 → 0.8.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (63) hide show
  1. package/CHANGELOG.md +51 -0
  2. package/README.md +57 -2
  3. package/docs/README.md +2 -1
  4. package/docs/reference/blocks.json +171 -1
  5. package/docs/reference/config.md +26 -8
  6. package/package.json +1 -1
  7. package/schema/code-forge.schema.json +81 -4
  8. package/skill/SKILL.md +6 -6
  9. package/skill/references/code.md +33 -4
  10. package/skill/references/ledger.md +8 -4
  11. package/skill/references/plan.md +12 -0
  12. package/skill/references/proof.md +44 -3
  13. package/skill/references/review.md +4 -2
  14. package/skill/references/security.md +1 -1
  15. package/skill/scripts/forge +1 -1
  16. package/skill/templates/coder-brief.md +18 -1
  17. package/src/autopilot/binnacle.mjs +21 -3
  18. package/src/cli/block.mjs +337 -37
  19. package/src/cli/context.mjs +175 -0
  20. package/src/cli/gates.mjs +125 -15
  21. package/src/cli/ledger.mjs +46 -2
  22. package/src/cli/proof.mjs +62 -16
  23. package/src/cli/report.mjs +21 -4
  24. package/src/cli/review-file.mjs +61 -6
  25. package/src/cli/run.mjs +16 -6
  26. package/src/cli/status.mjs +212 -0
  27. package/src/config/validate.mjs +45 -11
  28. package/src/context/pack.mjs +319 -0
  29. package/src/decide/lane-facts.mjs +41 -0
  30. package/src/doctor/local.mjs +2 -1
  31. package/src/engines/sentinel.mjs +27 -6
  32. package/src/gates/detect.mjs +21 -8
  33. package/src/gates/expand.mjs +202 -0
  34. package/src/gates/owned-count.mjs +66 -0
  35. package/src/gates/run.mjs +49 -12
  36. package/src/install/wizard/answers.mjs +34 -5
  37. package/src/install/wizard/flags.mjs +8 -1
  38. package/src/install/wizard/project-file.mjs +2 -0
  39. package/src/install/wizard/run.mjs +6 -0
  40. package/src/install/wizard/steps.mjs +24 -4
  41. package/src/install/wizard/summary.mjs +13 -5
  42. package/src/ledger/export.mjs +1 -1
  43. package/src/ledger/outside-review.mjs +164 -0
  44. package/src/ledger/report.mjs +32 -6
  45. package/src/proof/export.mjs +93 -10
  46. package/src/proof/lock.mjs +28 -5
  47. package/src/proof/worktree.mjs +482 -0
  48. package/src/review/engine.mjs +3 -2
  49. package/src/review/gate-check.mjs +9 -5
  50. package/src/review/open-findings.mjs +59 -0
  51. package/src/state/block-end.mjs +290 -0
  52. package/src/state/block.mjs +111 -37
  53. package/src/state/brief-status.mjs +131 -0
  54. package/src/state/config-snapshot.mjs +68 -8
  55. package/src/state/registry.mjs +35 -4
  56. package/src/state/run.mjs +21 -2
  57. package/src/util/breadcrumbs.mjs +1 -0
  58. package/src/util/forbidden.mjs +36 -1
  59. package/src/util/gate-token.mjs +8 -0
  60. package/src/worker/engine.mjs +157 -17
  61. package/src/worker/loop.mjs +26 -7
  62. package/src/worker/queue.mjs +36 -10
  63. package/src/worker/ticket.mjs +46 -6
@@ -0,0 +1,319 @@
1
+ /**
2
+ * The coder context pack (block B63a; issue #13 item 5, issue #10 item 2).
3
+ *
4
+ * The top-level `context` key of `.code-forge.yml` names what every coder task should carry:
5
+ *
6
+ * context:
7
+ * rules: [CLAUDE.md, docs/conventions.md] repository-relative files, in order
8
+ * lessons: docs/lessons.md one repository-relative file
9
+ * commands: {build: [...], test: [...], lint: [...], format: [...]} argv arrays, each optional
10
+ * max_lines: 300 the pack's line cap (integer >= 20)
11
+ * require_coder_tests: false B63b: close needs the coder's `TESTS … exit=0` line
12
+ *
13
+ * `assemblePack` turns it into ONE pack for a block, in this order: the header (run, block, level,
14
+ * owned files, test files when the block has them, worktree path and branch when it has one), the
15
+ * commands (never run; each argv shown POSIX shell-quoted, so a pasted line expands nothing), each
16
+ * rules file, then the lessons file — every file section headed by its source path (an empty
17
+ * file: the heading alone). Over `max_lines` the lessons are cut first, then the
18
+ * rules from the last file backwards, each cut section ending in `[cut: N lines dropped]`; the
19
+ * header and the commands are never cut.
20
+ *
21
+ * `reviewDigest` is the ONE builder of the review packets' rules digest (`## project rules`, ≤ 60
22
+ * lines, `packet.mjs`): the rules files' text, then a `### lessons` subheading and the lessons.
23
+ * With no `context` key it is '' — the packet is then byte-identical to before (`(none)`). A file
24
+ * it cannot read throws: the worker answers that ticket `review.unavailable`, `context-unreadable`.
25
+ *
26
+ * Files are always read from the MAIN tree's root (`repoRoot`), never from a block worktree a coder
27
+ * can write to. A path that is absolute, has a `..` segment, or leads out of the repository through
28
+ * a symlink (a broken one too: its target is checked) is refused, and so is a missing file
29
+ * (`ContextError`). A control character (a newline included) in a command token, a rules or
30
+ * lessons path, or a header value (owned or test file, worktree path or branch) is refused, so
31
+ * nothing can forge a line of the pack. Command lines go through `redact`, as the rules and
32
+ * lessons do. The `context` key is not
33
+ * authority: it is not fixed for the run, and `run reload` takes a change to it — except B63b's
34
+ * `context.require_coder_tests`, which tightens `block close`: it is read from the run-start
35
+ * snapshot only (`requireCoderTests`, `src/state/config-snapshot.mjs`) and `run reload` refuses a
36
+ * change to it. It plays no part in the pack.
37
+ */
38
+
39
+ import { lstatSync, readFileSync, readlinkSync, realpathSync } from 'node:fs';
40
+ import path from 'node:path';
41
+ import { estimateTokens } from '../review/packet.mjs';
42
+ import { splitLines } from '../review/context.mjs';
43
+ import { redact } from '../util/redact.mjs';
44
+
45
+ /** The pack's line cap when `context.max_lines` is not set. */
46
+ export const DEFAULT_MAX_LINES = 300;
47
+
48
+ /** The smallest `context.max_lines` accepted. */
49
+ export const MIN_MAX_LINES = 20;
50
+
51
+ /** The `context.commands` keys, in the order the pack lists them. */
52
+ export const COMMAND_KEYS = Object.freeze(['build', 'test', 'lint', 'format']);
53
+
54
+ /** The subheading the lessons get in the review digest. */
55
+ export const DIGEST_LESSONS_HEADING = '### lessons';
56
+
57
+ /**
58
+ * A refusal with a stable `code`. Thrown here: `bad-context` (a bad `context` value),
59
+ * `control-character` (in a path or a header value), `outside`, `missing` (ENOENT), `unreadable`
60
+ * (any other lstat or read error, with its code), `not-file`. Thrown by `src/cli/context.mjs`:
61
+ * `no-block`, `bad-config`, `no-context`, `no-run-root`, `write-failed`.
62
+ */
63
+ export class ContextError extends Error {
64
+ /** @param {string} code @param {string} message */
65
+ constructor(code, message) {
66
+ super(message);
67
+ this.name = 'ContextError';
68
+ this.code = code;
69
+ }
70
+ }
71
+
72
+ /** C0 and C1 control characters and DEL: a newline among them. */
73
+ const CONTROL = /[\u0000-\u001f\u007f-\u009f]/;
74
+
75
+ /** @param {unknown} v @returns {v is Record<string, unknown>} */
76
+ const isMapping = (v) => v !== null && typeof v === 'object' && !Array.isArray(v);
77
+
78
+ /**
79
+ * @typedef {object} ContextSettings
80
+ * @property {string[]} rules
81
+ * @property {string | null} lessons
82
+ * @property {Record<string, string[]>} commands - only the keys that are set.
83
+ * @property {number} maxLines
84
+ */
85
+
86
+ /**
87
+ * The `context` key of a config, checked; null when the key is absent.
88
+ * @param {Record<string, any> | null | undefined} cfg
89
+ * @returns {ContextSettings | null}
90
+ * @throws {ContextError} `bad-context`, naming the key.
91
+ */
92
+ export function contextSettings(cfg) {
93
+ const raw = cfg?.context;
94
+ if (raw === undefined || raw === null) return null;
95
+ if (!isMapping(raw)) throw new ContextError('bad-context', 'context must be a mapping');
96
+ const rules = raw.rules ?? [];
97
+ if (!Array.isArray(rules) || !rules.every((r) => typeof r === 'string' && r.length > 0)) throw new ContextError('bad-context', 'context.rules must be a list of file paths');
98
+ // `CLAUDE.md` and `./CLAUDE.md` are the same file
99
+ if (new Set(rules.map((r) => path.posix.normalize(r))).size !== rules.length) throw new ContextError('bad-context', 'context.rules names a file twice');
100
+ const lessons = raw.lessons ?? null;
101
+ if (lessons !== null && (typeof lessons !== 'string' || lessons.length === 0)) throw new ContextError('bad-context', 'context.lessons must be one file path');
102
+ const commandsRaw = raw.commands ?? {};
103
+ if (!isMapping(commandsRaw)) throw new ContextError('bad-context', 'context.commands must be a mapping');
104
+ /** @type {Record<string, string[]>} */
105
+ const commands = {};
106
+ for (const key of Object.keys(commandsRaw)) {
107
+ if (!COMMAND_KEYS.includes(key)) throw new ContextError('bad-context', `context.commands.${key} is not one of ${COMMAND_KEYS.join(', ')}`);
108
+ }
109
+ for (const key of COMMAND_KEYS) {
110
+ const argv = commandsRaw[key];
111
+ if (argv === undefined || argv === null) continue;
112
+ if (!Array.isArray(argv) || argv.length === 0 || !argv.every((a) => typeof a === 'string' && a.length > 0)) throw new ContextError('bad-context', `context.commands.${key} must be an argv list of non-empty strings`);
113
+ if (argv.some((a) => CONTROL.test(a))) throw new ContextError('bad-context', `context.commands.${key} contains a control character`);
114
+ commands[key] = [...argv];
115
+ }
116
+ // B63b: authority, read by `block close` from the run-start snapshot only; checked here so a
117
+ // pack is never built from a config that carries a malformed value
118
+ const requireTests = raw.require_coder_tests ?? false;
119
+ if (typeof requireTests !== 'boolean') throw new ContextError('bad-context', 'context.require_coder_tests must be true or false');
120
+ const maxLines = raw.max_lines ?? DEFAULT_MAX_LINES;
121
+ if (!Number.isInteger(maxLines) || /** @type {number} */ (maxLines) < MIN_MAX_LINES) throw new ContextError('bad-context', `context.max_lines must be an integer >= ${MIN_MAX_LINES}`);
122
+ return { rules: [...rules], lessons: /** @type {string | null} */ (lessons), commands, maxLines: /** @type {number} */ (maxLines) };
123
+ }
124
+
125
+ /** How many symlinks one path may pass through before it counts as a loop (as the kernel's ELOOP). */
126
+ const MAX_LINK_HOPS = 40;
127
+
128
+ /**
129
+ * `abs` with every symlink on it resolved, as far as its components exist — a symlink whose target
130
+ * is missing (a broken link) is still followed through its `readlink` text, so where it POINTS is
131
+ * what is checked. A component that does not exist ends the walk (the rest is joined as given); a
132
+ * link loop ends it at the link.
133
+ * @param {string} abs - absolute, without `..` segments. @returns {string}
134
+ */
135
+ function walkLinks(abs) {
136
+ const parts = abs.split(path.sep).filter(Boolean);
137
+ let cur = path.parse(abs).root;
138
+ let hops = 0;
139
+ while (parts.length > 0) {
140
+ const next = path.join(cur, /** @type {string} */ (parts.shift()));
141
+ let st;
142
+ try {
143
+ st = lstatSync(next);
144
+ } catch {
145
+ return path.join(next, ...parts);
146
+ }
147
+ if (!st.isSymbolicLink()) {
148
+ cur = next;
149
+ continue;
150
+ }
151
+ hops += 1;
152
+ if (hops > MAX_LINK_HOPS) return path.join(next, ...parts);
153
+ // `cur` holds no symlink, so resolving the target's `..` against it is exact
154
+ const target = path.resolve(cur, readlinkSync(next));
155
+ parts.unshift(...target.split(path.sep).filter(Boolean));
156
+ cur = path.parse(target).root;
157
+ }
158
+ return cur;
159
+ }
160
+
161
+ /** @param {string} root @param {string} p @returns {boolean} `p` is `root` or under it. */
162
+ const inside = (root, p) => {
163
+ const rel = path.relative(root, p);
164
+ // only a real `..` segment leaves the root: a file named `..notes.md` is inside
165
+ return !(rel === '..' || rel.startsWith(`..${path.sep}`) || path.isAbsolute(rel));
166
+ };
167
+
168
+ /**
169
+ * The real path of the context file `rel` (named by `key`) under `repoRoot`.
170
+ * @param {string} repoRoot - the main tree's root. @param {string} rel @param {string} key
171
+ * @returns {string}
172
+ * @throws {ContextError} `control-character`, `outside`, `missing`, `unreadable`, `not-file`
173
+ */
174
+ export function resolveContextFile(repoRoot, rel, key) {
175
+ const shown = JSON.stringify(rel);
176
+ if (CONTROL.test(rel)) throw new ContextError('control-character', `${key}: ${shown} contains a control character`);
177
+ if (path.isAbsolute(rel) || rel.split(/[\\/]/).includes('..')) {
178
+ throw new ContextError('outside', `${key}: ${shown} is outside the repository (give a path relative to the repository root)`);
179
+ }
180
+ const root = realpathSync(repoRoot);
181
+ // every symlink on the way is followed — a broken one too, by its target — and the end checked
182
+ const real = walkLinks(path.resolve(root, rel));
183
+ if (!inside(root, real)) throw new ContextError('outside', `${key}: ${shown} leads outside the repository`);
184
+ let st;
185
+ try {
186
+ st = lstatSync(real);
187
+ } catch (err) {
188
+ const code = /** @type {NodeJS.ErrnoException} */ (err).code ?? 'error';
189
+ if (code === 'ENOENT') throw new ContextError('missing', `${key}: ${shown} does not exist`);
190
+ throw new ContextError('unreadable', `${key}: ${shown} cannot be read (${code})`);
191
+ }
192
+ // still a link here: a loop (or more than MAX_LINK_HOPS links) — nothing to read
193
+ if (st.isSymbolicLink()) throw new ContextError('missing', `${key}: ${shown} does not exist`);
194
+ if (!st.isFile()) throw new ContextError('not-file', `${key}: ${shown} is not a file`);
195
+ return real;
196
+ }
197
+
198
+ /**
199
+ * @param {string} repoRoot @param {string} rel @param {string} key
200
+ * @returns {string[]} the file's lines (redacted, trailing whitespace of the file dropped); none
201
+ * for an empty or whitespace-only file.
202
+ * @throws {ContextError} as {@link resolveContextFile}, or `unreadable` when the read fails.
203
+ */
204
+ function readLines(repoRoot, rel, key) {
205
+ const file = resolveContextFile(repoRoot, rel, key);
206
+ let raw;
207
+ try {
208
+ raw = readFileSync(file, 'utf8');
209
+ } catch (err) {
210
+ throw new ContextError('unreadable', `${key}: ${JSON.stringify(rel)} cannot be read (${/** @type {NodeJS.ErrnoException} */ (err).code ?? 'error'})`);
211
+ }
212
+ const text = /** @type {string} */ (redact(raw)).trimEnd();
213
+ return text === '' ? [] : splitLines(text);
214
+ }
215
+
216
+ /**
217
+ * Every rules file and the lessons file, read. A missing or outside file is a refusal.
218
+ * @param {string} repoRoot @param {ContextSettings} settings
219
+ * @returns {{rules: Array<{path: string, lines: string[]}>, lessons: {path: string, lines: string[]} | null}}
220
+ * @throws {ContextError}
221
+ */
222
+ export function readSources(repoRoot, settings) {
223
+ const rules = settings.rules.map((rel) => ({ path: rel, lines: readLines(repoRoot, rel, 'context.rules') }));
224
+ const lessons = settings.lessons === null ? null : { path: settings.lessons, lines: readLines(repoRoot, settings.lessons, 'context.lessons') };
225
+ return { rules, lessons };
226
+ }
227
+
228
+ /**
229
+ * The token as a POSIX shell would take it back: bare when it holds only characters no shell
230
+ * expands, else in single quotes (an embedded `'` as `'\\''`), so a pasted line never expands
231
+ * `$x`, a backtick, a glob or a brace.
232
+ * @param {string} token @returns {string}
233
+ */
234
+ const argvToken = (token) => (/^[A-Za-z0-9_@%+=:,./-]+$/.test(token) ? token : `'${token.replaceAll("'", "'\\''")}'`);
235
+
236
+ /** @param {string[]} argv @returns {string} */
237
+ export const argvText = (argv) => argv.map(argvToken).join(' ');
238
+
239
+ /**
240
+ * @typedef {object} PackBlock
241
+ * @property {string} run
242
+ * @property {string} block
243
+ * @property {string | null} [level]
244
+ * @property {string[]} [owned]
245
+ * @property {string[] | null} [tests]
246
+ * @property {{path: string, branch: string} | null} [worktree]
247
+ */
248
+
249
+ /**
250
+ * Build the pack text for a block (pure over what it is given).
251
+ * @param {{settings: ContextSettings, sources: ReturnType<typeof readSources>, block: PackBlock}} opts
252
+ * @returns {{text: string, lines: number, cut: number}} `cut`: the lines dropped.
253
+ * @throws {ContextError} `control-character` when a header value holds one.
254
+ */
255
+ export function assemblePack({ settings, sources, block }) {
256
+ /** @type {Array<[string, unknown]>} */
257
+ const values = [['level', block.level], ...(block.owned ?? []).map((f) => /** @type {[string, unknown]} */ (['owned file', f])), ...(block.tests ?? []).map((f) => /** @type {[string, unknown]} */ (['test file', f]))];
258
+ if (block.worktree) values.push(['worktree path', block.worktree.path], ['worktree branch', block.worktree.branch]);
259
+ for (const [label, v] of values) {
260
+ if (typeof v === 'string' && CONTROL.test(v)) throw new ContextError('control-character', `block ${block.block}: ${label} ${JSON.stringify(v)} contains a control character`);
261
+ }
262
+ const header = ['# code-forge context pack', '## block', `run: ${block.run}`, `block: ${block.block}`, `level: ${block.level ?? '(unknown)'}`, 'owned files:'];
263
+ for (const f of block.owned ?? []) header.push(`- ${f}`);
264
+ if (Array.isArray(block.tests) && block.tests.length > 0) {
265
+ header.push('test files:');
266
+ for (const f of block.tests) header.push(`- ${f}`);
267
+ }
268
+ if (block.worktree) header.push(`worktree: ${block.worktree.path}`, `branch: ${block.worktree.branch}`);
269
+ header.push('## commands (never run by code-forge)');
270
+ const keys = COMMAND_KEYS.filter((k) => Object.hasOwn(settings.commands, k));
271
+ if (keys.length === 0) header.push('(none)');
272
+ // a command line can name a secret (a token in an argv): it is redacted as rules and lessons are
273
+ for (const k of keys) header.push(`${k}: ${redact(argvText(settings.commands[k]))}`);
274
+
275
+ /** @type {Array<{heading: string, body: string[], dropped: number}>} */
276
+ const sections = sources.rules.map((r) => ({ heading: `## rules: ${r.path}`, body: [...r.lines], dropped: 0 }));
277
+ if (sources.lessons) sections.push({ heading: `## lessons: ${sources.lessons.path}`, body: [...sources.lessons.lines], dropped: 0 });
278
+
279
+ const total = () => header.length + sections.reduce((n, s) => n + 1 + s.body.length + (s.dropped > 0 ? 1 : 0), 0);
280
+ // cut order: the lessons first, then the rules from the last file backwards
281
+ for (const s of [...sections].reverse()) {
282
+ let excess = total() - settings.maxLines;
283
+ if (excess <= 0) break;
284
+ // the marker takes one line: a section is only cut when that leaves it shorter
285
+ if (s.body.length < 2) continue;
286
+ const drop = Math.min(s.body.length, excess + 1);
287
+ s.body = s.body.slice(0, s.body.length - drop);
288
+ s.dropped = drop;
289
+ }
290
+ const out = [...header];
291
+ for (const s of sections) {
292
+ out.push(s.heading, ...s.body);
293
+ if (s.dropped > 0) out.push(`[cut: ${s.dropped} lines dropped]`);
294
+ }
295
+ return { text: `${out.join('\n')}\n`, lines: out.length, cut: sections.reduce((n, s) => n + s.dropped, 0) };
296
+ }
297
+
298
+ /**
299
+ * The review packets' rules digest (`## project rules`), built from the MAIN tree each time a
300
+ * packet is built: the rules files' lines in order, then `### lessons` and the lessons' lines.
301
+ * '' when the config has no `context` key (the packet stays as it was), or names no rules and no
302
+ * lessons. `packet.mjs` keeps the first 60 lines and trims from the end over budget, rules and
303
+ * lessons together.
304
+ * @param {{repoRoot: string, cfg: Record<string, any> | null | undefined}} opts
305
+ * @returns {string}
306
+ * @throws {ContextError} a bad `context` value, or a rules or lessons file that is missing,
307
+ * outside the repository or unreadable — the worker makes that ticket `review.unavailable`
308
+ * with `reason: context-unreadable` (no review runs without the rules it was promised).
309
+ */
310
+ export function reviewDigest({ repoRoot, cfg }) {
311
+ const settings = contextSettings(cfg);
312
+ if (settings === null) return '';
313
+ const out = settings.rules.flatMap((rel) => readLines(repoRoot, rel, 'context.rules'));
314
+ if (settings.lessons !== null) out.push(DIGEST_LESSONS_HEADING, ...readLines(repoRoot, settings.lessons, 'context.lessons'));
315
+ return out.join('\n');
316
+ }
317
+
318
+ /** @param {string} text @returns {number} the pack's estimated tokens (the review packets' bytes / 4). */
319
+ export const packTokens = (text) => estimateTokens(text);
@@ -0,0 +1,41 @@
1
+ /**
2
+ * B59: the facts `fallbackLane` (./fallback-rules.mjs) needs, built from what a brief-mode
3
+ * `block open` knows before any code exists: the block's owned entries, the estimated changed
4
+ * lines (`--lines`) and `proof.tiers.high.paths` from the run's config snapshot. Pure — no disk,
5
+ * no git; the caller reads the snapshot.
6
+ *
7
+ * filesChanged the number of distinct owned entries (a directory entry counts once)
8
+ * linesAdded `--lines`, 0 when not given
9
+ * touchesMigration an owned entry names a migration (`isMigrationPath`)
10
+ * touchesPolicyOrMiddleware an owned entry names a policy or middleware (`isPolicyPath`)
11
+ * pathFloorHit a high path owns an owned entry, or an owned entry owns a high path
12
+ * (a directory entry above a high path counts: its files are in scope)
13
+ *
14
+ * The migration and policy checks are case-insensitive SUBSTRING matches anywhere in the path
15
+ * (`src/migrationHelper.ts` is a migration path), the same patterns the §3.5 review rules use
16
+ * (`rulesRisk` in src/review/engine.mjs calls these two functions).
17
+ */
18
+
19
+ import { ownsFile } from '../state/registry.mjs';
20
+
21
+ /** A path the §3.5 rules treat as a migration. @param {string} file @returns {boolean} */
22
+ export const isMigrationPath = (file) => /migration/i.test(file);
23
+
24
+ /** A path the §3.5 rules treat as a policy or middleware. @param {string} file @returns {boolean} */
25
+ export const isPolicyPath = (file) => /polic(y|ies)|middleware/i.test(file);
26
+
27
+ /**
28
+ * @param {{owned: ReadonlyArray<string>, lines?: number | null, highPaths?: unknown}} input
29
+ * `highPaths` that is not an array (absent, null) means no high paths.
30
+ * @returns {Required<Omit<import('./fallback-rules.mjs').FallbackFacts, 'keywordsFound'>>}
31
+ */
32
+ export function laneFacts({ owned, lines = null, highPaths = [] }) {
33
+ const high = (Array.isArray(highPaths) ? highPaths : []).filter((p) => typeof p === 'string' && p.length > 0);
34
+ return {
35
+ filesChanged: new Set(owned).size,
36
+ linesAdded: lines ?? 0,
37
+ touchesMigration: owned.some(isMigrationPath),
38
+ touchesPolicyOrMiddleware: owned.some(isPolicyPath),
39
+ pathFloorHit: owned.some((entry) => ownsFile(high, entry) || high.some((h) => ownsFile([entry], h))),
40
+ };
41
+ }
@@ -104,7 +104,8 @@ export async function checkCommands(cfg, pathEnv) {
104
104
  rows.push(row('solo', 'INFO', 'solo', 'UNVERIFIED (no Solo probe in this version)'));
105
105
  const gates = cfg?.gates && typeof cfg.gates === 'object' ? cfg.gates : {};
106
106
  let any = false;
107
- for (const [name, argv] of Object.entries(gates)) {
107
+ for (const [name, command] of Object.entries(gates)) {
108
+ const argv = command && !Array.isArray(command) && typeof command === 'object' ? command.argv : command; // B60: {argv, env}
108
109
  if (!Array.isArray(argv) || argv.length === 0) continue;
109
110
  any = true;
110
111
  const found = await commandOnPath(String(argv[0]), { pathEnv });
@@ -11,6 +11,16 @@
11
11
  * trimmed line (`^...$`), so a coder quoting the sentinel text inside ordinary prose ("I will not
12
12
  * print ===BLOCK B4 COMPLETE=== yet") never matches — only a line that IS exactly the sentinel does.
13
13
  *
14
+ * **TESTS.** B63b: a coder proves a green test run with a line that IS exactly `TESTS <command>
15
+ * exit=<n>` (matched after trimming; `<command>` non-empty free text, `<n>` a decimal 0–255 with no
16
+ * leading zeros — a non-numeric, signed or out-of-range value does not count) printed before its
17
+ * sentinel. On a returned sentinel, `tests: {command, exit}` is the LAST such line in that
18
+ * sentinel's own segment: after the previous sentinel line of ANY block (COMPLETE or FAILED) and
19
+ * before this one. A TESTS line before an earlier attempt's sentinel, or before another block's
20
+ * sentinel in a shared log, never carries over, and one after the sentinel is ignored; with none
21
+ * the result has no `tests` key at all. Like the sentinel, the pattern is anchored to the whole
22
+ * trimmed line, so a TESTS line quoted in prose never matches.
23
+ *
14
24
  * **ACK.** Before any tool call, a coder replies `ACK <sha8> lines=<n>` to the wire receipt it was
15
25
  * sent — the sha of the brief file, truncated to 8 hex characters (matched case-insensitively;
16
26
  * compared lowercased), and the brief's line count. `parseAck` returns the LAST matching line (fix
@@ -23,17 +33,21 @@
23
33
  * look like an ACK (an echoed instruction, a quoted example) being mistaken for the real one.
24
34
  */
25
35
 
26
- /** @typedef {{status: "complete", blockId: string} | {status: "failed", blockId: string, reason: string}} SentinelResult */
36
+ /** @typedef {{command: string, exit: number}} TestsLine */
37
+ /** @typedef {({status: "complete", blockId: string} | {status: "failed", blockId: string, reason: string}) & {tests?: TestsLine}} SentinelResult */
27
38
 
28
39
  const COMPLETE_RE = /^===BLOCK\s+(\S+)\s+COMPLETE===$/;
29
40
  const FAILED_RE = /^===BLOCK\s+(\S+)\s+FAILED:\s*(.*?)\s*===$/;
41
+ /** B63b: `TESTS <command> exit=<n>` as a whole trimmed line (no leading zeros); 0–255 is checked after the match. */
42
+ const TESTS_RE = /^TESTS\s+(\S.*?)\s+exit=(0|[1-9]\d{0,2})$/;
30
43
 
31
44
  /**
32
45
  * @param {string} output - the full captured transcript (subprocess stdout, or a Solo
33
46
  * `get_process_output` join).
34
47
  * @param {string} [blockId] - when given, only sentinel lines for THIS block id count — a shared
35
48
  * pane/log can interleave another block's lines.
36
- * @returns {SentinelResult | null} the LAST matching sentinel line, or `null` if none is present.
49
+ * @returns {SentinelResult | null} the LAST matching sentinel line, or `null` if none is present;
50
+ * with `tests` when a `TESTS` line comes before it in its segment (B63b).
37
51
  */
38
52
  export function parseSentinel(output, blockId) {
39
53
  if (typeof output !== 'string') {
@@ -41,17 +55,24 @@ export function parseSentinel(output, blockId) {
41
55
  }
42
56
  /** @type {SentinelResult | null} */
43
57
  let last = null;
58
+ /** @type {TestsLine | null} */
59
+ let tests = null;
44
60
  for (const rawLine of output.split('\n')) {
45
61
  const line = rawLine.trim();
46
62
  const complete = COMPLETE_RE.exec(line);
47
- if (complete && (!blockId || complete[1] === blockId)) {
48
- last = { status: 'complete', blockId: complete[1] };
63
+ if (complete) {
64
+ if (!blockId || complete[1] === blockId) last = { status: 'complete', blockId: complete[1], ...(tests ? { tests } : {}) };
65
+ tests = null; // B63b: any sentinel line, of any block, ends the segment
49
66
  continue;
50
67
  }
51
68
  const failed = FAILED_RE.exec(line);
52
- if (failed && (!blockId || failed[1] === blockId)) {
53
- last = { status: 'failed', blockId: failed[1], reason: failed[2] };
69
+ if (failed) {
70
+ if (!blockId || failed[1] === blockId) last = { status: 'failed', blockId: failed[1], reason: failed[2], ...(tests ? { tests } : {}) };
71
+ tests = null;
72
+ continue;
54
73
  }
74
+ const run = TESTS_RE.exec(line);
75
+ if (run && Number(run[2]) <= 255) tests = { command: run[1], exit: Number(run[2]) };
55
76
  }
56
77
  return last;
57
78
  }
@@ -4,7 +4,7 @@
4
4
  * arrays, or `null` when the stack has no tool for that gate. Detection is pure filesystem
5
5
  * evidence (`fs.existsSync`/`readFileSync`) — no `git`, no child process, no network.
6
6
  *
7
- * Evidence is checked in the table's own order (PHP/Pest, then `package.json` scripts, Cargo,
7
+ * Evidence is checked in the table's own order (PHP/Pest or PHPUnit, then `package.json` scripts, Cargo,
8
8
  * pyproject, go.mod); the first stack whose marker file exists wins. A project with no marker
9
9
  * file at all is `unknown`: every gate is `null`, and `code` (a later block) refuses to run a
10
10
  * block whose `gates.test` is null — a block without a test gate cannot produce evidence.
@@ -18,10 +18,15 @@ import path from 'node:path';
18
18
  * (`gofmt -l`) exit 0 regardless of whether anything is unformatted and instead print the
19
19
  * offending file names to stdout — for those, the descriptor carries `failOnStdout: true` so
20
20
  * `run.mjs` treats non-empty stdout as red too, since a bare exit-code check can never fail.
21
- * @typedef {string[] | {argv: string[], failOnStdout: true}} GateCommand
21
+ * B60: a configured gate may be `{argv, env}` — `env` adds variables to the gate's environment
22
+ * (the values are never printed, `run.mjs`).
23
+ * @typedef {string[] | {argv: string[], failOnStdout?: true, env?: Record<string, string>}} GateCommand
22
24
  * @typedef {GateCommand | null} Gate
23
25
  */
24
- /** @typedef {{stack: string, test: Gate, lint: Gate, types: Gate, format: Gate}} DetectedGates */
26
+ /**
27
+ * `stack` is one of `php-pest`, `php-phpunit`, `node`, `rust`, `python`, `go` or `unknown`.
28
+ * @typedef {{stack: string, test: Gate, lint: Gate, types: Gate, format: Gate}} DetectedGates
29
+ */
25
30
 
26
31
  /** @param {string} root @param {string} rel @returns {boolean} */
27
32
  function has(root, rel) {
@@ -49,15 +54,23 @@ function readText(root, rel) {
49
54
  }
50
55
  }
51
56
 
52
- // ── PHP / Pest ───────────────────────────────────────────────────────────────
57
+ // ── PHP (Pest, PHPUnit) ─────────────────────────────────────────────────────
53
58
 
54
- /** @param {string} root @returns {DetectedGates | null} */
59
+ /**
60
+ * `composer.json` plus an installed runner: `vendor/bin/pest` is `php-pest` (checked first, so a
61
+ * project with both binaries is Pest), else `vendor/bin/phpunit` is `php-phpunit` (B61). A Laravel
62
+ * app (`artisan` present) runs either through `php artisan test --compact`; without `artisan` the
63
+ * runner's own binary runs. `types` and `format` do not depend on the runner.
64
+ * @param {string} root @returns {DetectedGates | null}
65
+ */
55
66
  function detectPhp(root) {
56
- if (!has(root, 'composer.json') || !has(root, 'vendor/bin/pest')) return null;
57
- const test = has(root, 'artisan') ? ['php', 'artisan', 'test', '--compact'] : ['vendor/bin/pest'];
67
+ if (!has(root, 'composer.json')) return null;
68
+ const runner = has(root, 'vendor/bin/pest') ? 'pest' : has(root, 'vendor/bin/phpunit') ? 'phpunit' : null;
69
+ if (runner === null) return null;
70
+ const test = has(root, 'artisan') ? ['php', 'artisan', 'test', '--compact'] : [`vendor/bin/${runner}`];
58
71
  const types = has(root, 'phpstan.neon') || has(root, 'phpstan.neon.dist') ? ['vendor/bin/phpstan'] : null;
59
72
  const format = has(root, 'pint.json') || has(root, 'vendor/bin/pint') ? ['vendor/bin/pint', '--test'] : null;
60
- return { stack: 'php-pest', test, lint: null, types, format };
73
+ return { stack: `php-${runner}`, test, lint: null, types, format };
61
74
  }
62
75
 
63
76
  // ── Node (package.json scripts) ────────────────────────────────────────────