rungate 0.3.0 → 0.4.1

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 (49) hide show
  1. package/lib/ac-quality.ts +119 -0
  2. package/lib/brief-precedence.ts +114 -0
  3. package/lib/ci-checkout.ts +27 -1
  4. package/lib/compliance-backfill.ts +16 -4
  5. package/lib/compliance-report.ts +109 -8
  6. package/lib/config-loader.ts +129 -5
  7. package/lib/config-readers.ts +86 -0
  8. package/lib/conformity.ts +520 -27
  9. package/lib/context-docs.ts +182 -0
  10. package/lib/create-brief.ts +22 -17
  11. package/lib/create-sc.ts +55 -33
  12. package/lib/decomposition-waves.ts +197 -0
  13. package/lib/doc-scan-targets.ts +435 -0
  14. package/lib/gate-check-audit.ts +190 -0
  15. package/lib/generators/agents-md.ts +10 -5
  16. package/lib/issue-extraction.ts +368 -0
  17. package/lib/organize.ts +129 -40
  18. package/lib/package-closure.ts +111 -0
  19. package/lib/phase2-decomposition.ts +270 -0
  20. package/lib/prior-branch.ts +24 -1
  21. package/lib/project-aliases.ts +100 -0
  22. package/lib/prove-label-gate.ts +93 -0
  23. package/lib/release-preflight.ts +225 -15
  24. package/lib/rungate-schema.ts +36 -2
  25. package/lib/sc-cover.ts +92 -1
  26. package/lib/sc-ids.ts +151 -0
  27. package/lib/scaffold/pre-push-hook.ts +165 -0
  28. package/lib/scaffold/rehearsal.ts +330 -0
  29. package/lib/scaffold/steps.ts +175 -122
  30. package/lib/scaffold/write-steps.ts +167 -0
  31. package/lib/scope-denylist.ts +258 -0
  32. package/lib/scratch-cleanup.ts +198 -0
  33. package/lib/scratch-roots.ts +94 -0
  34. package/lib/task-completion-checks.ts +50 -3
  35. package/lib/typescript-project.ts +233 -0
  36. package/lib/validators/spec-validators.ts +23 -34
  37. package/lib/verified-push.ts +179 -2
  38. package/lib/workflow-security.ts +51 -7
  39. package/lib/worktree-cleanup.ts +318 -20
  40. package/lib/worktree-repo.ts +465 -0
  41. package/package.json +8 -2
  42. package/scripts/audit-specs.ts +134 -37
  43. package/scripts/audit-transcript.ts +259 -0
  44. package/scripts/create-sc.ts +15 -1
  45. package/scripts/detect-sc-drift.ts +200 -0
  46. package/scripts/extract-constraints.ts +16 -36
  47. package/scripts/scaffold-project.ts +26 -136
  48. package/scripts/split-spec.ts +315 -0
  49. package/scripts/sync-sc-status.ts +751 -0
@@ -0,0 +1,368 @@
1
+ /**
2
+ * Reading a goal and its acceptance criteria out of an issue body (#210, #202).
3
+ *
4
+ * `scripts/precompute-goal.ts` replaces two ceremony agents with two regexes
5
+ * run over the raw markdown of a GitHub issue. Both were wrong in the same
6
+ * way, and the shared cause is worth stating before either case:
7
+ *
8
+ * **The extractor had no model of the document it was reading.** It matched
9
+ * lines. A line inside a fenced code block, a line under a "Scope" heading,
10
+ * and a line the harness itself wrote into the issue are all just lines.
11
+ *
12
+ * Two measured instances:
13
+ *
14
+ * - **#210.** Shipping #209, the criteria list came back as exactly one AC:
15
+ * ``SC-N: `workflows/ship.js` executes [MARKER-NAME]``. That is an
16
+ * illustration inside a fenced block showing the SYNTAX of a proposed SC.
17
+ * Marcus was about to be briefed with one acceptance criterion made
18
+ * entirely of placeholders, and nothing downstream would have asked.
19
+ * - **#202.** When a run rescopes an issue for the 6-AC limit it PREPENDS a
20
+ * blockquote banner, which then becomes the first paragraph and therefore
21
+ * the goal. Measured on #200: 414 characters describing the split, none
22
+ * describing the defect. The harness degraded its own input, and only on
23
+ * the LARGE issues where the goal statement matters most.
24
+ *
25
+ * ## Why this is a module and not twenty lines in the script
26
+ *
27
+ * It was twenty lines in the script, at module top level, so none of it was
28
+ * reachable from a test — which is why both defects were found by reading
29
+ * pipeline output by hand rather than by the suite. The functions here take a
30
+ * string and return a value; every case below is a fixture.
31
+ *
32
+ * ## The direction these are allowed to be wrong in
33
+ *
34
+ * Toward extracting LESS and saying so. A criterion missed costs Discovery
35
+ * writing one, which is the designed path. A criterion invented out of
36
+ * documentation is briefed to an agent as a thing to build.
37
+ */
38
+
39
+ /**
40
+ * Markers that identify a blockquote as harness-authored metadata rather than
41
+ * the author's prose.
42
+ *
43
+ * Exported and asserted against `workflows/ship.js`'s decompose prompt by
44
+ * `test/issue-extraction.test.ts`, which is the "fails loudly" half: the
45
+ * banner is written by an agent told to use this wording, so a change to the
46
+ * instruction and no change here turns the suite red instead of silently
47
+ * handing the banner back as a goal (#202).
48
+ *
49
+ * A leading blockquote carrying NONE of these is kept. That is the positive
50
+ * control, and it is the whole difficulty of the case: a parser that
51
+ * discarded every leading blockquote would pass the banner cases for free and
52
+ * lose the goal of any issue whose author opens with a quote.
53
+ */
54
+ export const HARNESS_BANNER_MARKERS = [
55
+ "Rescoped to Phase",
56
+ "Sub-issues:",
57
+ ] as const;
58
+
59
+ /**
60
+ * Line prefixes that make a leading paragraph harness-written front matter.
61
+ *
62
+ * Found by this change rather than reported: `test/fixtures/github/issue-48.json`
63
+ * is a sub-issue, sub-issue bodies open with `Parent: #47`, and the old regex
64
+ * took the first paragraph. So **every decomposed run has briefed its agent
65
+ * with the goal "Parent: #N"** — eleven characters of cross-reference. Same
66
+ * defect as #202's banner, one producer earlier and far more frequent: the
67
+ * banner fires only on a rescope, this fires on every sub-issue.
68
+ *
69
+ * `workflows/ship.js`'s decompose step is told to write both lines, and
70
+ * `test/issue-extraction.test.ts` asserts they are still in that prompt.
71
+ *
72
+ * A paragraph is skipped only when EVERY line in it is one of these and short.
73
+ * An author writing a sentence that begins "Spec: the contract says…" keeps
74
+ * their goal.
75
+ */
76
+ export const HARNESS_PREAMBLE_MARKERS = [
77
+ "Parent: #",
78
+ "Spec: ",
79
+ ] as const;
80
+
81
+ /** Above this, a line is prose that happens to start with a marker. */
82
+ const MAX_PREAMBLE_LINE_CHARS = 120;
83
+
84
+ /** A goal shorter than this is a failed extraction, not a terse author. */
85
+ export const MIN_GOAL_CHARS = 40;
86
+
87
+ /**
88
+ * The one exit code every refusal in `scripts/precompute-goal.ts` routes
89
+ * through, per `.claude/rules/checks-must-be-able-to-fail.md`. Declared once
90
+ * so the test can build a mutant copy of the script with it set to `0` and
91
+ * watch the refusals stop refusing.
92
+ *
93
+ * Not `1`: `1` is the usage error the script already exits with for missing
94
+ * arguments, and a caller that cannot tell "you called me wrong" from "this
95
+ * issue cannot be read" learns nothing from either.
96
+ */
97
+ export const GOAL_REFUSE_EXIT = 3;
98
+
99
+ export interface GoalReading {
100
+ /** The goal paragraph, or null when nothing usable was left. */
101
+ goal: string | null;
102
+ /** Non-null when the body yielded no goal a brief could be written from. */
103
+ refusal: string | null;
104
+ /** True when a harness banner was recognised and stepped over. */
105
+ skippedBanner: boolean;
106
+ }
107
+
108
+ export interface CriteriaReading {
109
+ criteria: string[];
110
+ /** Where they came from, so "none found" can say which question it answered. */
111
+ source: "success-criteria-section" | "none";
112
+ /** Non-null when what was found cannot be briefed — see `isPlaceholder`. */
113
+ refusal: string | null;
114
+ }
115
+
116
+ /**
117
+ * Blank out code so a line inside it cannot be read as content.
118
+ *
119
+ * Lines are REPLACED with empty lines rather than removed: every caller below
120
+ * reasons about headings and ordering, and silently renumbering the document
121
+ * would make a section boundary land somewhere else than the author put it.
122
+ *
123
+ * Indented code is only recognised outside a list. Four-space indentation is
124
+ * also how a nested checkbox is written, and `- [ ] SC-2` nested under a
125
+ * bullet is an acceptance criterion in every issue in this repo — treating it
126
+ * as code would delete real criteria to catch a rare fake one, which is the
127
+ * wrong direction for this module to be wrong in.
128
+ */
129
+ export function stripCodeBlocks(body: string): string {
130
+ const lines = String(body ?? "").split("\n");
131
+ const out: string[] = [];
132
+ let fence: string | null = null;
133
+ let inList = false;
134
+
135
+ for (const line of lines) {
136
+ if (fence !== null) {
137
+ // A closing fence is the same character, at least as long, alone on its
138
+ // line. Anything else is content.
139
+ const close = line.match(/^\s{0,3}(`{3,}|~{3,})\s*$/);
140
+ out.push("");
141
+ if (close && close[1]![0] === fence[0] && close[1]!.length >= fence.length) fence = null;
142
+ continue;
143
+ }
144
+
145
+ const open = line.match(/^\s{0,3}(`{3,}|~{3,})/);
146
+ if (open) {
147
+ fence = open[1]!;
148
+ out.push("");
149
+ continue;
150
+ }
151
+
152
+ if (line.trim() === "") {
153
+ inList = false;
154
+ out.push(line);
155
+ continue;
156
+ }
157
+
158
+ // Indentation is checked BEFORE the list marker, and only outside a list.
159
+ // An indented bullet is a nested list item when a list is open and an
160
+ // indented code block when one is not, and the two are the same bytes —
161
+ // the only thing that tells them apart is what came before.
162
+ if (!inList && /^(?: {4,}|\t)/.test(line)) {
163
+ out.push("");
164
+ continue;
165
+ }
166
+
167
+ if (/^\s*(?:[-*+]|\d+[.)])\s/.test(line)) {
168
+ inList = true;
169
+ out.push(line);
170
+ continue;
171
+ }
172
+
173
+ out.push(line);
174
+ }
175
+
176
+ return out.join("\n");
177
+ }
178
+
179
+ /** `## Heading` → its level, or 0 for a line that is not a heading. */
180
+ function headingLevel(line: string): number {
181
+ const m = line.match(/^(#{1,6})\s+\S/);
182
+ return m ? m[1]!.length : 0;
183
+ }
184
+
185
+ /**
186
+ * The lines under the Success Criteria heading, or null when there is none.
187
+ *
188
+ * Ends at the next heading of the SAME OR HIGHER level, so a `### Phase 2`
189
+ * subsection inside the criteria is kept and the next `## Acceptance` is not.
190
+ */
191
+ export function successCriteriaSection(body: string): string | null {
192
+ const lines = String(body ?? "").split("\n");
193
+ for (let i = 0; i < lines.length; i++) {
194
+ const level = headingLevel(lines[i]!);
195
+ if (!level) continue;
196
+ if (!/^#{1,6}\s+success\s+criteria\b/i.test(lines[i]!)) continue;
197
+ const taken: string[] = [];
198
+ for (let j = i + 1; j < lines.length; j++) {
199
+ const nextLevel = headingLevel(lines[j]!);
200
+ if (nextLevel > 0 && nextLevel <= level) break;
201
+ taken.push(lines[j]!);
202
+ }
203
+ return taken.join("\n");
204
+ }
205
+ return null;
206
+ }
207
+
208
+ /**
209
+ * Is this criterion made of placeholders rather than of a requirement?
210
+ *
211
+ * `SC-N`, `[MARKER-NAME]` and `<thing>` are how this repo writes a TEMPLATE.
212
+ * An AC list consisting of them is the #210 reading exactly, and it reached
213
+ * the point of being briefed.
214
+ */
215
+ export function isPlaceholder(text: string): boolean {
216
+ const s = String(text ?? "").trim();
217
+ if (!s) return true;
218
+ // SC- followed by anything that is not a digit: SC-N, SC-X, SC-NNN.
219
+ if (/\bSC-(?![0-9])[A-Za-z]/.test(s) && !/\bSC-A\d/.test(s)) return true;
220
+ // A bracketed ALL-CAPS slug, which is this repo's template notation.
221
+ if (/\[[A-Z][A-Z0-9_-]{2,}\]/.test(s)) return true;
222
+ // An angle-bracketed placeholder that is not an HTML tag or an email.
223
+ if (/<[a-z][a-z0-9 _-]*>/i.test(s) && !/<\/|@/.test(s)) return true;
224
+ if (/\b(?:TBD|TODO|FIXME|XXX)\b/.test(s)) return true;
225
+ return false;
226
+ }
227
+
228
+ /**
229
+ * Acceptance criteria, from the Success Criteria section only.
230
+ *
231
+ * **Section-scoped, not body-scoped.** The ship skill's own contract says
232
+ * "all lines matching `- [ ] SC-` … under '## Success Criteria'"; the regex
233
+ * it replaced ignored the heading and matched the whole document, so a
234
+ * checklist under "Scope" or "Open questions" was silently promoted to an
235
+ * acceptance criterion (#210).
236
+ *
237
+ * **No section is `[]`, not a refusal.** Most issues in this repo have none,
238
+ * and Discovery authoring the ACs is the designed path. The refusal is for
239
+ * criteria that were found and cannot be briefed.
240
+ */
241
+ export function extractSuccessCriteria(body: string): CriteriaReading {
242
+ const section = successCriteriaSection(stripCodeBlocks(body));
243
+ if (section === null) {
244
+ return { criteria: [], source: "none", refusal: null };
245
+ }
246
+
247
+ const criteria: string[] = [];
248
+ // `SC-` is required, not decorative. The old pattern's `|.+` branch made
249
+ // every `- [ ]` line qualify, so the prefix tested nothing.
250
+ const line = /^\s*-\s*\[[ xX]\]\s*(.+)$/gm;
251
+ let m: RegExpExecArray | null;
252
+ while ((m = line.exec(section)) !== null) {
253
+ const text = m[1]!.trim();
254
+ if (text) criteria.push(text);
255
+ }
256
+
257
+ if (criteria.length === 0) {
258
+ return { criteria, source: "success-criteria-section", refusal: null };
259
+ }
260
+
261
+ const fake = criteria.filter(isPlaceholder);
262
+ if (fake.length === criteria.length) {
263
+ return {
264
+ criteria: [],
265
+ source: "success-criteria-section",
266
+ refusal:
267
+ `every acceptance criterion read from this issue is a placeholder — ` +
268
+ `${JSON.stringify(fake)}. Briefing an agent with a criterion made of ` +
269
+ `placeholders is worse than briefing it with none, because Discovery ` +
270
+ `writes criteria when there are none and accepts these when there are (#210).`,
271
+ };
272
+ }
273
+
274
+ return { criteria, source: "success-criteria-section", refusal: null };
275
+ }
276
+
277
+ /** Does this blockquote carry a marker that makes it harness metadata? */
278
+ function isHarnessBanner(block: string): boolean {
279
+ return HARNESS_BANNER_MARKERS.some(marker => block.includes(marker));
280
+ }
281
+
282
+ /**
283
+ * The goal paragraph: the first prose paragraph the author wrote.
284
+ *
285
+ * Steps over a leading blockquote ONLY when it carries a harness banner
286
+ * marker, so an author who opens with a quote keeps their goal (#202 AC-3 —
287
+ * the parser has to be correct for prose it did not write, because consumer
288
+ * repos have their own conventions).
289
+ */
290
+ export function extractGoal(body: string, title: string): GoalReading {
291
+ const stripped = stripCodeBlocks(body);
292
+ const lines = stripped.split("\n");
293
+ let i = 0;
294
+ let skippedBanner = false;
295
+
296
+ const skipBanner = () => {
297
+ while (i < lines.length && lines[i]!.trim() === "") i++;
298
+ if (i >= lines.length || !/^\s*>/.test(lines[i]!)) return false;
299
+ const start = i;
300
+ while (i < lines.length && (/^\s*>/.test(lines[i]!) || lines[i]!.trim() === "")) {
301
+ // A blank line ends the quote unless the next line resumes it.
302
+ if (lines[i]!.trim() === "" && !/^\s*>/.test(lines[i + 1] ?? "")) break;
303
+ i++;
304
+ }
305
+ const block = lines.slice(start, i).join("\n");
306
+ if (isHarnessBanner(block)) return true;
307
+ i = start; // the author's own quote — keep it
308
+ return false;
309
+ };
310
+
311
+ if (skipBanner()) skippedBanner = true;
312
+
313
+ /** The next paragraph's lines, without consuming them. */
314
+ const peekParagraph = (from: number): { lines: string[]; next: number } => {
315
+ let k = from;
316
+ while (k < lines.length && lines[k]!.trim() === "") k++;
317
+ const taken: string[] = [];
318
+ for (; k < lines.length; k++) {
319
+ if (lines[k]!.trim() === "") break;
320
+ if (headingLevel(lines[k]!) > 0) break;
321
+ taken.push(lines[k]!);
322
+ }
323
+ return { lines: taken, next: k };
324
+ };
325
+
326
+ // Step over harness front matter the same way, and for the same reason. A
327
+ // paragraph counts only if EVERY line is a short marker line, so prose that
328
+ // opens with one of the words is kept.
329
+ for (;;) {
330
+ const { lines: para, next } = peekParagraph(i);
331
+ if (para.length === 0) break;
332
+ const allMetadata = para.every(
333
+ l =>
334
+ l.trim().length <= MAX_PREAMBLE_LINE_CHARS &&
335
+ HARNESS_PREAMBLE_MARKERS.some(marker => l.trim().startsWith(marker)),
336
+ );
337
+ if (!allMetadata) break;
338
+ skippedBanner = true;
339
+ i = next;
340
+ }
341
+
342
+ const paragraph = peekParagraph(i).lines;
343
+ const goal = paragraph.join("\n").trim();
344
+
345
+ if (!goal) {
346
+ return {
347
+ goal: null,
348
+ skippedBanner,
349
+ refusal:
350
+ `this issue body yielded no goal paragraph${skippedBanner ? " after its harness banner" : ""} — ` +
351
+ `the title alone is not a problem statement, and a brief written from it would ` +
352
+ `describe the work by its name rather than by what is wrong (#202).`,
353
+ };
354
+ }
355
+
356
+ if (goal.length < MIN_GOAL_CHARS) {
357
+ return {
358
+ goal: null,
359
+ skippedBanner,
360
+ refusal:
361
+ `the goal paragraph read from this issue is ${goal.length} characters, under the ` +
362
+ `${MIN_GOAL_CHARS} a problem statement needs — ${JSON.stringify(goal)}. A short goal and a ` +
363
+ `missing goal are not the same state, and neither is a goal (#202).`,
364
+ };
365
+ }
366
+
367
+ return { goal, refusal: null, skippedBanner };
368
+ }
package/lib/organize.ts CHANGED
@@ -21,6 +21,7 @@ import {
21
21
  } from "fs";
22
22
  import { join, relative, dirname, basename } from "path";
23
23
  import { loadComplianceConfig, type OrganizeExternalSource } from "./config-loader.js";
24
+ import { matchesAnyAlias, projectAliases } from "./project-aliases.js";
24
25
 
25
26
  // ── Types ────────────────────────────────────────────────────────
26
27
 
@@ -64,6 +65,27 @@ export interface OrganizeOptions {
64
65
  harnessRoot?: string;
65
66
  }
66
67
 
68
+ /**
69
+ * What a scan covered, and what it could not (#442).
70
+ *
71
+ * `scanExternalSources` had three fail-opens in one loop: a declared path
72
+ * that is not on disk was `continue`d, an unreadable file was `catch {}`ed
73
+ * into a non-match, and the whole directory walk sat inside another bare
74
+ * `catch`. A user who declares where their documents live and is told
75
+ * nothing when that place is not there has been given `0`, `{}` and `SKIP`
76
+ * — which `.claude/rules/config-access.md` names as indistinguishable from
77
+ * success. `UNMEASURED` is not.
78
+ */
79
+ export interface OrganizeReport {
80
+ proposals: OrganizeProposal[];
81
+ /** Declared external paths that are not on disk. Named, not skipped. */
82
+ unmeasured: string[];
83
+ /** Paths that exist and could not be read. A non-match is not the answer. */
84
+ unreadable: string[];
85
+ /** The names matching was actually done against, so a miss is diagnosable. */
86
+ aliases: string[];
87
+ }
88
+
67
89
  // ── Well-known root files to skip ────────────────────────────────
68
90
 
69
91
  const WELL_KNOWN_ROOT_FILES = new Set([
@@ -288,7 +310,14 @@ export function classifyDocument(
288
310
 
289
311
  // ── Main organize function ───────────────────────────────────────
290
312
 
291
- export function organizeProject(root: string, options: OrganizeOptions = {}): OrganizeProposal[] {
313
+ /**
314
+ * The full result: what was proposed, and what could not be looked at.
315
+ *
316
+ * `organizeProject` below is a view onto this, not a second implementation —
317
+ * eight call sites want only the proposals and changing them all would have
318
+ * bought nothing. One decider, one convenience accessor.
319
+ */
320
+ export function organizeProjectReport(root: string, options: OrganizeOptions = {}): OrganizeReport {
292
321
  const complianceConfig = options.harnessRoot
293
322
  ? loadComplianceConfig(options.harnessRoot)
294
323
  : null;
@@ -316,71 +345,131 @@ export function organizeProject(root: string, options: OrganizeOptions = {}): Or
316
345
  });
317
346
  }
318
347
 
348
+ const aliases = projectAliases(root, organizeConfig?.aliases ?? []);
349
+ const report: OrganizeReport = { proposals, unmeasured: [], unreadable: [], aliases };
350
+
319
351
  // Scan external sources from config
320
352
  if (organizeConfig?.externalSources) {
321
- const externalProposals = scanExternalSources(root, organizeConfig.externalSources);
322
- proposals.push(...externalProposals);
353
+ proposals.push(...scanExternalSources(root, organizeConfig.externalSources, aliases, report));
323
354
  }
324
355
 
325
356
  if (options.apply && !options.dryRun) {
326
357
  executeProposals(root, proposals);
327
358
  }
328
359
 
329
- return proposals;
360
+ return report;
361
+ }
362
+
363
+ export function organizeProject(root: string, options: OrganizeOptions = {}): OrganizeProposal[] {
364
+ return organizeProjectReport(root, options).proposals;
330
365
  }
331
366
 
332
367
  // ── External source scanning ────────────────────────────────────
333
368
 
334
- function scanExternalSources(projectRoot: string, sources: OrganizeExternalSource[]): OrganizeProposal[] {
369
+ function scanExternalSources(
370
+ projectRoot: string,
371
+ sources: OrganizeExternalSource[],
372
+ aliases: string[],
373
+ report: { unmeasured: string[]; unreadable: string[] },
374
+ ): OrganizeProposal[] {
335
375
  const proposals: OrganizeProposal[] = [];
336
- const projectName = basename(projectRoot).toLowerCase();
337
376
 
338
377
  for (const source of sources) {
339
- const expandedPath = source.path.replace("~", process.env.HOME || "");
340
- if (!existsSync(expandedPath)) continue;
378
+ const expandedPath = expandHome(source.path);
379
+ if (!existsSync(expandedPath)) {
380
+ // UNMEASURED, not skipped. The user said their documents live here.
381
+ report.unmeasured.push(source.path);
382
+ continue;
383
+ }
341
384
  if (source.target === null) continue;
342
385
 
343
- if (lstatSync(expandedPath).isDirectory()) {
344
- try {
345
- for (const entry of readdirSync(expandedPath)) {
346
- const entryPath = join(expandedPath, entry);
347
- if (!lstatSync(entryPath).isDirectory() && !entry.endsWith(".md")) continue;
348
-
349
- let matches = false;
350
- if (source.matchBy === "project-name") {
351
- matches = entry.toLowerCase().includes(projectName) ||
352
- (lstatSync(entryPath).isDirectory() && readdirSync(entryPath).some(f =>
353
- f.toLowerCase().includes(projectName)));
354
- } else if (source.matchBy === "content-reference") {
355
- if (entry.endsWith(".md")) {
356
- try {
357
- const content = readFileSync(entryPath, "utf-8");
358
- matches = content.toLowerCase().includes(projectName);
359
- } catch {}
360
- }
361
- }
386
+ let stat;
387
+ try {
388
+ stat = lstatSync(expandedPath);
389
+ } catch {
390
+ report.unreadable.push(source.path);
391
+ continue;
392
+ }
393
+ if (!stat.isDirectory()) continue;
394
+
395
+ let entries: string[];
396
+ try {
397
+ entries = readdirSync(expandedPath);
398
+ } catch {
399
+ // Was a bare `catch {}` around the whole walk. A declared source the
400
+ // process cannot list is the single most important thing to say.
401
+ report.unreadable.push(source.path);
402
+ continue;
403
+ }
362
404
 
363
- if (matches) {
364
- const targetPath = join(source.target, entry);
365
- const targetFull = join(projectRoot, targetPath);
366
- if (existsSync(targetFull)) continue;
367
-
368
- proposals.push({
369
- source: entryPath,
370
- target: targetPath,
371
- classification: source.type,
372
- reasoning: `external ${source.type} matched by ${source.matchBy} in ${source.path}`,
373
- confidence: 0.7,
374
- });
405
+ for (const entry of entries) {
406
+ const entryPath = join(expandedPath, entry);
407
+ let isDir: boolean;
408
+ try {
409
+ isDir = lstatSync(entryPath).isDirectory();
410
+ } catch {
411
+ report.unreadable.push(entryPath);
412
+ continue;
413
+ }
414
+ if (!isDir && !entry.endsWith(".md")) continue;
415
+
416
+ let matches = false;
417
+ if (source.matchBy === "project-name") {
418
+ matches = matchesAnyAlias(entry, aliases);
419
+ if (!matches && isDir) {
420
+ try {
421
+ matches = readdirSync(entryPath).some(f => matchesAnyAlias(f, aliases));
422
+ } catch {
423
+ report.unreadable.push(entryPath);
424
+ continue;
375
425
  }
376
426
  }
377
- } catch {}
427
+ } else if (source.matchBy === "content-reference") {
428
+ if (!entry.endsWith(".md")) continue;
429
+ let content: string;
430
+ try {
431
+ content = readFileSync(entryPath, "utf-8");
432
+ } catch {
433
+ // Was `catch {}`, which made unreadable and not-a-match the same
434
+ // answer. They are opposite answers: one is "no", the other is
435
+ // "I could not look".
436
+ report.unreadable.push(entryPath);
437
+ continue;
438
+ }
439
+ matches = matchesAnyAlias(content, aliases);
440
+ }
441
+
442
+ if (!matches) continue;
443
+
444
+ const targetPath = join(source.target, entry);
445
+ if (existsSync(join(projectRoot, targetPath))) continue;
446
+
447
+ proposals.push({
448
+ source: entryPath,
449
+ target: targetPath,
450
+ classification: source.type,
451
+ reasoning: `external ${source.type} matched by ${source.matchBy} in ${source.path}`,
452
+ confidence: 0.7,
453
+ });
378
454
  }
379
455
  }
380
456
 
381
457
  return proposals;
382
458
  }
383
459
 
460
+ /**
461
+ * `~` expansion, anchored.
462
+ *
463
+ * `source.path.replace("~", HOME)` replaces the FIRST `~` anywhere in the
464
+ * string, so `/srv/backup~old/docs` became `/srv/backup<HOME>old/docs`. Only
465
+ * a leading `~` is a home reference.
466
+ */
467
+ export function expandHome(path: string): string {
468
+ if (path !== "~" && !path.startsWith("~/")) return path;
469
+ const home = process.env.HOME || "";
470
+ return home ? home + path.slice(1) : path;
471
+ }
472
+
384
473
  // ── Execute proposals (move files) ───────────────────────────────
385
474
 
386
475
  function executeProposals(root: string, proposals: OrganizeProposal[]): void {