etymd 0.13.0 → 0.15.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/CHANGELOG.md +108 -0
- package/README.md +30 -19
- package/dist/{approve-YUT43YLC.js → approve-MA4Z3TBT.js} +5 -5
- package/dist/audit-YSALDC2L.js +11 -0
- package/dist/{brief-Z5S6OY2M.js → brief-DPZYSAMC.js} +5 -5
- package/dist/{chunk-PXRLOEN5.js → chunk-2RNQ6OLV.js} +146 -717
- package/dist/{chunk-DSAQ5S5D.js → chunk-CEO3BXQB.js} +3 -2
- package/dist/{chunk-D3R74TJ2.js → chunk-DBWDMIYO.js} +1 -1
- package/dist/{chunk-5BVKFJWM.js → chunk-HI7NWPRA.js} +79 -4
- package/dist/{chunk-2VLNI3L2.js → chunk-HOR4M6EC.js} +1 -1
- package/dist/chunk-IWG77WV3.js +758 -0
- package/dist/{chunk-DWL2IKZH.js → chunk-P6ATKV2R.js} +65 -3
- package/dist/{chunk-HRJJQCMT.js → chunk-UFNETE6P.js} +69 -20
- package/dist/{chunk-YXOAPMQH.js → chunk-Y6RZRED3.js} +2 -2
- package/dist/{chunk-LSZGCKIQ.js → chunk-YQZDYDAK.js} +1 -1
- package/dist/cli.js +42 -21
- package/dist/{config-XAH6PA5G.js → config-724Y3IOB.js} +1 -2
- package/dist/{context-F63RSIBH.js → context-JGKU4M7Z.js} +2 -4
- package/dist/doctor-Y3DWDEBT.js +18 -0
- package/dist/{fleet-YQ35KGEP.js → fleet-4FYZ3GBK.js} +11 -12
- package/dist/{gates-DU6SDH2M.js → gates-IBQ7HCAN.js} +6 -7
- package/dist/generate-KEX75XNG.js +5 -0
- package/dist/index.d.ts +173 -2
- package/dist/index.js +713 -198
- package/dist/{init-XJEYU2HC.js → init-H57H2JBE.js} +14 -13
- package/dist/ledger-T54JDHGP.js +6 -0
- package/dist/premise-VMC3UUIB.js +333 -0
- package/dist/scan-W4US23MU.js +5 -0
- package/dist/{scan-B24RZAV2.js → scan-XBOHDXAH.js} +5 -5
- package/dist/{screen-CS6PSG7U.js → screen-E4FC7W5N.js} +2 -1
- package/package.json +2 -2
- package/dist/audit-YRT4SWSQ.js +0 -12
- package/dist/chunk-F75Q43BC.js +0 -59
- package/dist/chunk-JHZ2BN4U.js +0 -67
- package/dist/doctor-7YKDXSKM.js +0 -19
- package/dist/generate-PMCP37DC.js +0 -6
- package/dist/ledger-4FOZ5HAB.js +0 -5
- package/dist/scan-QDNN7ER3.js +0 -5
|
@@ -0,0 +1,758 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import { expandFileGlobs } from './chunk-Y6RZRED3.js';
|
|
3
|
+
import { DEFAULT_CONFIG, CONFIG_FILE } from './chunk-P6ATKV2R.js';
|
|
4
|
+
import { readText, isDirectory, matchesAnyGlob, normalizeRelPath, readJson, pathExists, git } from './chunk-4VPBP6K6.js';
|
|
5
|
+
import path3 from 'path';
|
|
6
|
+
import { promises } from 'fs';
|
|
7
|
+
|
|
8
|
+
// src/engine/finding.ts
|
|
9
|
+
var TIER_ORDER = { risk: 0, gap: 1, polish: 2 };
|
|
10
|
+
var EFFORT_ORDER = { S: 0, M: 1, L: 2 };
|
|
11
|
+
function parseFailOnTier(value) {
|
|
12
|
+
if (value === "risk" || value === "gap" || value === "polish") return value;
|
|
13
|
+
throw new Error(`--fail-on must be risk|gap|polish, got \`${value}\``);
|
|
14
|
+
}
|
|
15
|
+
function meetsFailOn(findings, failOn) {
|
|
16
|
+
const threshold = TIER_ORDER[failOn];
|
|
17
|
+
return findings.some((f) => TIER_ORDER[f.tier] <= threshold);
|
|
18
|
+
}
|
|
19
|
+
function rankFindings(findings) {
|
|
20
|
+
return [...findings].sort(
|
|
21
|
+
(a, b) => TIER_ORDER[a.tier] - TIER_ORDER[b.tier] || EFFORT_ORDER[a.effort] - EFFORT_ORDER[b.effort]
|
|
22
|
+
);
|
|
23
|
+
}
|
|
24
|
+
var LENS_ID = "state-freshness";
|
|
25
|
+
var DECISIONS_FORMAT_MARKER = "<!-- decisions-format: 1 -->";
|
|
26
|
+
var KNOWN_FORMAT_VERSION = 1;
|
|
27
|
+
var MARKER_RE = /<!--\s*decisions-format:\s*(\d+)([^>]*?)-->/;
|
|
28
|
+
var FIELD_NAME_RE = /^[A-Za-z0-9 _-]+$/;
|
|
29
|
+
var BUILT_IN_FIELDS = /* @__PURE__ */ new Set(["scope"]);
|
|
30
|
+
var MS_PER_DAY = 864e5;
|
|
31
|
+
function parseDecisionsFormat(text) {
|
|
32
|
+
const m = MARKER_RE.exec(text);
|
|
33
|
+
if (!m) return null;
|
|
34
|
+
const problems = [];
|
|
35
|
+
const fields = [];
|
|
36
|
+
const offset = m.index ?? 0;
|
|
37
|
+
const version = Number(m[1]);
|
|
38
|
+
if (version !== KNOWN_FORMAT_VERSION) {
|
|
39
|
+
problems.push(
|
|
40
|
+
`declares decisions-format version ${version}; this etymd understands version ${KNOWN_FORMAT_VERSION} \u2014 checked as version ${KNOWN_FORMAT_VERSION}.`
|
|
41
|
+
);
|
|
42
|
+
}
|
|
43
|
+
const attrs = (m[2] ?? "").trim();
|
|
44
|
+
if (!attrs) return { fields, offset, problems };
|
|
45
|
+
const declared = /^fields=(.*)$/.exec(attrs);
|
|
46
|
+
if (!declared) {
|
|
47
|
+
problems.push(`marker attribute \`${attrs}\` is not understood \u2014 ignored (only \`fields=\`).`);
|
|
48
|
+
return { fields, offset, problems };
|
|
49
|
+
}
|
|
50
|
+
const seen = new Set(BUILT_IN_FIELDS);
|
|
51
|
+
for (const raw of declared[1].split(",")) {
|
|
52
|
+
const name = raw.trim();
|
|
53
|
+
if (!name) continue;
|
|
54
|
+
if (!FIELD_NAME_RE.test(name)) {
|
|
55
|
+
problems.push(
|
|
56
|
+
`declared field \`${name}\` is not a usable field name (letters, digits, spaces, \`-\`, \`_\`) \u2014 not checked.`
|
|
57
|
+
);
|
|
58
|
+
continue;
|
|
59
|
+
}
|
|
60
|
+
const key = name.toLowerCase();
|
|
61
|
+
if (seen.has(key)) continue;
|
|
62
|
+
seen.add(key);
|
|
63
|
+
fields.push(name);
|
|
64
|
+
}
|
|
65
|
+
if (fields.length === 0 && problems.length === 0) {
|
|
66
|
+
problems.push("marker declares `fields=` with no field names \u2014 no extra fields checked.");
|
|
67
|
+
}
|
|
68
|
+
return { fields, offset, problems };
|
|
69
|
+
}
|
|
70
|
+
function hasField(block, name) {
|
|
71
|
+
return new RegExp(`${name.replace(/[.*+?^${}()|[\]\\]/g, "\\$&")}[\\s*]*:`).test(block);
|
|
72
|
+
}
|
|
73
|
+
function parseDecisionEntries(text) {
|
|
74
|
+
const headings = [...text.matchAll(/^## .*$/gm)];
|
|
75
|
+
const entries = [];
|
|
76
|
+
for (let i = 0; i < headings.length; i++) {
|
|
77
|
+
const h = headings[i];
|
|
78
|
+
const m = /^## (D-(\d+))\b/.exec(h[0]);
|
|
79
|
+
if (!m) continue;
|
|
80
|
+
const offset = h.index ?? 0;
|
|
81
|
+
const start = offset + h[0].length;
|
|
82
|
+
const end = i + 1 < headings.length ? headings[i + 1].index : void 0;
|
|
83
|
+
entries.push({ id: m[1], num: Number(m[2]), block: text.slice(start, end), offset });
|
|
84
|
+
}
|
|
85
|
+
return entries;
|
|
86
|
+
}
|
|
87
|
+
function checkIdSequence(file, entries) {
|
|
88
|
+
const findings = [];
|
|
89
|
+
const nextFree = Math.max(0, ...entries.map((e) => e.num)) + 1;
|
|
90
|
+
const seen = /* @__PURE__ */ new Map();
|
|
91
|
+
const duplicated = /* @__PURE__ */ new Set();
|
|
92
|
+
let prev;
|
|
93
|
+
for (const entry of entries) {
|
|
94
|
+
if (seen.has(entry.num)) {
|
|
95
|
+
if (!duplicated.has(entry.num)) {
|
|
96
|
+
duplicated.add(entry.num);
|
|
97
|
+
findings.push({
|
|
98
|
+
id: `${LENS_ID}/duplicate-id:${file}:${entry.id}`,
|
|
99
|
+
lens: LENS_ID,
|
|
100
|
+
tier: "gap",
|
|
101
|
+
claim: `${file} carries more than one ${entry.id} entry`,
|
|
102
|
+
evidence: [`${file}: ${entry.id} appears twice`],
|
|
103
|
+
why: "Two decisions under one id cannot be cited, superseded, or dismissed unambiguously \u2014 append races collide exactly here.",
|
|
104
|
+
action: `Rename the later entry to D-${String(nextFree).padStart(3, "0")} (the next free id).`,
|
|
105
|
+
effort: "S",
|
|
106
|
+
confidence: "high"
|
|
107
|
+
});
|
|
108
|
+
}
|
|
109
|
+
} else {
|
|
110
|
+
seen.set(entry.num, entry);
|
|
111
|
+
if (prev && entry.num < prev.num) {
|
|
112
|
+
findings.push({
|
|
113
|
+
id: `${LENS_ID}/id-order:${file}:${entry.id}`,
|
|
114
|
+
lens: LENS_ID,
|
|
115
|
+
tier: "gap",
|
|
116
|
+
claim: `${file} lists ${entry.id} after ${prev.id} \u2014 ids out of append order`,
|
|
117
|
+
evidence: [`${file}: ${prev.id} precedes ${entry.id}`],
|
|
118
|
+
why: "An append-only record reads in id order; out-of-order ids make the newest decision hard to find and the next id hard to pick.",
|
|
119
|
+
action: `Rename the out-of-order entry into sequence (next free id: D-${String(nextFree).padStart(3, "0")}).`,
|
|
120
|
+
effort: "S",
|
|
121
|
+
confidence: "high"
|
|
122
|
+
});
|
|
123
|
+
}
|
|
124
|
+
prev = entry;
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
return findings;
|
|
128
|
+
}
|
|
129
|
+
function checkFormatFields(file, entries, today, declaredFields, markerOffset) {
|
|
130
|
+
const findings = [];
|
|
131
|
+
for (const entry of entries) {
|
|
132
|
+
const bound = entry.offset >= markerOffset;
|
|
133
|
+
for (const field of bound ? declaredFields : []) {
|
|
134
|
+
if (hasField(entry.block, field)) continue;
|
|
135
|
+
findings.push({
|
|
136
|
+
id: `${LENS_ID}/field-missing:${file}:${entry.id}:${field}`,
|
|
137
|
+
lens: LENS_ID,
|
|
138
|
+
tier: "gap",
|
|
139
|
+
claim: `${file} ${entry.id} has no ${field}: field`,
|
|
140
|
+
evidence: [`${file}: ${entry.id}`, `${file} marker declares required field \`${field}\``],
|
|
141
|
+
why: "The file declares this field required on every entry after the marker; whatever reads the record for it finds nothing here.",
|
|
142
|
+
action: `Add a ${field}: line to ${entry.id}.`,
|
|
143
|
+
effort: "S",
|
|
144
|
+
confidence: "high"
|
|
145
|
+
});
|
|
146
|
+
}
|
|
147
|
+
if (bound && !/Scope[\s*]*:/.test(entry.block)) {
|
|
148
|
+
findings.push({
|
|
149
|
+
id: `${LENS_ID}/scope-missing:${file}:${entry.id}`,
|
|
150
|
+
lens: LENS_ID,
|
|
151
|
+
tier: "gap",
|
|
152
|
+
claim: `${file} ${entry.id} has no Scope: field`,
|
|
153
|
+
evidence: [`${file}: ${entry.id}`],
|
|
154
|
+
why: "A decision without a scope binds nobody \u2014 a reader cannot tell whether it covers the project they are working in.",
|
|
155
|
+
action: "Add a Scope: line naming what the decision binds.",
|
|
156
|
+
effort: "S",
|
|
157
|
+
confidence: "high"
|
|
158
|
+
});
|
|
159
|
+
}
|
|
160
|
+
const revisit = /Revisit[\s*]*:[\s*]*(\d{4}-\d{2}-\d{2})/.exec(entry.block);
|
|
161
|
+
if (revisit && revisit[1] < today) {
|
|
162
|
+
findings.push({
|
|
163
|
+
id: `${LENS_ID}/revisit-due:${file}:${entry.id}`,
|
|
164
|
+
lens: LENS_ID,
|
|
165
|
+
tier: "gap",
|
|
166
|
+
claim: `${file} ${entry.id} was due for revisit on ${revisit[1]}`,
|
|
167
|
+
evidence: [`${file}: ${entry.id} Revisit: ${revisit[1]}`],
|
|
168
|
+
why: "Review debt is due \u2014 a Revisit date is a promise to re-evaluate, and a past one silently hardens into policy.",
|
|
169
|
+
action: "Re-evaluate the decision: supersede it or move the Revisit date.",
|
|
170
|
+
effort: "S",
|
|
171
|
+
confidence: "high"
|
|
172
|
+
});
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
return findings;
|
|
176
|
+
}
|
|
177
|
+
var stateFreshnessLens = {
|
|
178
|
+
id: LENS_ID,
|
|
179
|
+
version: "1",
|
|
180
|
+
title: "State freshness",
|
|
181
|
+
kind: "truth",
|
|
182
|
+
async run(ctx) {
|
|
183
|
+
const budgets = ctx.config?.config.state ?? DEFAULT_CONFIG.state;
|
|
184
|
+
const findings = [];
|
|
185
|
+
const disclosures = [...ctx.config?.problems ?? []];
|
|
186
|
+
const outOfScope = [];
|
|
187
|
+
const today = (/* @__PURE__ */ new Date()).toISOString().slice(0, 10);
|
|
188
|
+
const stateArtifacts = ctx.facts.artifacts.filter((a) => a.kind === "state" && a.exists);
|
|
189
|
+
const decisionArtifacts = ctx.facts.artifacts.filter((a) => a.kind === "decisions" && a.exists);
|
|
190
|
+
if (stateArtifacts.length === 0 && decisionArtifacts.length === 0) {
|
|
191
|
+
disclosures.push("No state or decisions artifacts detected \u2014 nothing to check.");
|
|
192
|
+
}
|
|
193
|
+
const freshness = ctx.facts.freshness;
|
|
194
|
+
if (!freshness) {
|
|
195
|
+
disclosures.push("Scan carried no freshness facts \u2014 staleness unchecked.");
|
|
196
|
+
} else {
|
|
197
|
+
for (const u of freshness.unverifiable) {
|
|
198
|
+
disclosures.push(`Freshness of ${u.path} is unverifiable (${u.reason}) \u2014 not flagged.`);
|
|
199
|
+
}
|
|
200
|
+
for (const a of stateArtifacts) {
|
|
201
|
+
const fact = freshness.artifacts.find((f) => f.artifactId === a.id);
|
|
202
|
+
if (!fact || !freshness.repoLastCommit) continue;
|
|
203
|
+
if (fact.dirty) {
|
|
204
|
+
disclosures.push(
|
|
205
|
+
`${a.path} has uncommitted changes \u2014 modified since its last commit; treated fresh-now, not flagged.`
|
|
206
|
+
);
|
|
207
|
+
continue;
|
|
208
|
+
}
|
|
209
|
+
if (!fact.commitsSince) continue;
|
|
210
|
+
const gapDays = Math.floor(
|
|
211
|
+
(Date.parse(freshness.repoLastCommit) - Date.parse(fact.lastCommit)) / MS_PER_DAY
|
|
212
|
+
);
|
|
213
|
+
if (gapDays <= budgets.staleAfterDays) continue;
|
|
214
|
+
const escalated = gapDays > budgets.staleAfterDays * 3;
|
|
215
|
+
findings.push({
|
|
216
|
+
id: `${LENS_ID}/stale-state:${a.path}`,
|
|
217
|
+
lens: LENS_ID,
|
|
218
|
+
tier: escalated ? "risk" : "gap",
|
|
219
|
+
claim: `${a.path} trails the repo by ${gapDays} days of commit traffic \u2014 it says "now" but the repo moved on`,
|
|
220
|
+
evidence: [
|
|
221
|
+
`${a.path} last commit: ${fact.lastCommit}`,
|
|
222
|
+
`repo last commit: ${freshness.repoLastCommit}`
|
|
223
|
+
],
|
|
224
|
+
why: escalated ? `Over three times the ${budgets.staleAfterDays}-day threshold while commits kept landing \u2014 every session starts from a picture of the project that is no longer true.` : `A state doc more than ${budgets.staleAfterDays} days behind continued commit traffic misleads every session that loads it.`,
|
|
225
|
+
action: "Refresh the state doc (or record why it is still current).",
|
|
226
|
+
effort: "S",
|
|
227
|
+
confidence: "high"
|
|
228
|
+
});
|
|
229
|
+
}
|
|
230
|
+
}
|
|
231
|
+
for (const a of stateArtifacts) {
|
|
232
|
+
const text = await readText(path3.join(ctx.root, a.path));
|
|
233
|
+
if (text === null) {
|
|
234
|
+
disclosures.push(`${a.path} could not be read \u2014 unexamined, not clean.`);
|
|
235
|
+
outOfScope.push(a.path);
|
|
236
|
+
continue;
|
|
237
|
+
}
|
|
238
|
+
if (text.length > budgets.maxChars) {
|
|
239
|
+
findings.push({
|
|
240
|
+
id: `${LENS_ID}/state-over-budget:${a.path}`,
|
|
241
|
+
lens: LENS_ID,
|
|
242
|
+
tier: "gap",
|
|
243
|
+
claim: `${a.path} is ${text.length} chars \u2014 over the ${budgets.maxChars}-char state budget`,
|
|
244
|
+
evidence: [`${a.path}: ${text.length} chars`],
|
|
245
|
+
why: "Session-injection hooks truncate state around 10,000 chars \u2014 an over-budget state doc gets cut mid-sentence, and every session pays its full weight before the task begins.",
|
|
246
|
+
action: "Trim to budget; move overflow into decisions/docs and keep a pointer.",
|
|
247
|
+
effort: "M",
|
|
248
|
+
confidence: "high"
|
|
249
|
+
});
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
for (const a of decisionArtifacts) {
|
|
253
|
+
const text = await readText(path3.join(ctx.root, a.path));
|
|
254
|
+
if (text === null) {
|
|
255
|
+
disclosures.push(
|
|
256
|
+
`${a.path} recognized as a decisions convention (directory) \u2014 age-exempt; per-entry format checks apply only to marker-carrying decisions files.`
|
|
257
|
+
);
|
|
258
|
+
continue;
|
|
259
|
+
}
|
|
260
|
+
const entries = parseDecisionEntries(text);
|
|
261
|
+
findings.push(...checkIdSequence(a.path, entries));
|
|
262
|
+
const format = parseDecisionsFormat(text);
|
|
263
|
+
if (!format) {
|
|
264
|
+
disclosures.push(
|
|
265
|
+
`${a.path} carries no \`${DECISIONS_FORMAT_MARKER}\` marker \u2014 format checks skipped (forward-only, never retroactive); id-sequence checks still ran.`
|
|
266
|
+
);
|
|
267
|
+
outOfScope.push(a.path);
|
|
268
|
+
continue;
|
|
269
|
+
}
|
|
270
|
+
for (const problem of format.problems) disclosures.push(`${a.path}: ${problem}`);
|
|
271
|
+
const exempt = entries.filter((e) => e.offset < format.offset);
|
|
272
|
+
if (format.fields.length > 0) {
|
|
273
|
+
disclosures.push(
|
|
274
|
+
`${a.path} declares required entry fields: ${format.fields.join(", ")} \u2014 checked on every entry at or after the marker (etymd attaches no meaning to the names).`
|
|
275
|
+
);
|
|
276
|
+
}
|
|
277
|
+
if (exempt.length > 0) {
|
|
278
|
+
disclosures.push(
|
|
279
|
+
`${a.path}: ${exempt.length} entr${exempt.length === 1 ? "y" : "ies"} precede the format marker (${exempt[0]?.id}\u2026${exempt[exempt.length - 1]?.id}) \u2014 field presence not checked there (forward-only from the marker's position).`
|
|
280
|
+
);
|
|
281
|
+
}
|
|
282
|
+
findings.push(...checkFormatFields(a.path, entries, today, format.fields, format.offset));
|
|
283
|
+
}
|
|
284
|
+
disclosures.push(
|
|
285
|
+
`Thresholds: staleAfterDays ${budgets.staleAfterDays} (3x escalates to risk), state budget ${budgets.maxChars} chars (${budgets.staleAfterDays === DEFAULT_CONFIG.state.staleAfterDays && budgets.maxChars === DEFAULT_CONFIG.state.maxChars ? `defaults \u2014 override under \`state\` in ${CONFIG_FILE}` : `set in ${CONFIG_FILE}`}). Decisions artifacts are exempt from age \u2014 old decisions are history, not defects.`
|
|
286
|
+
);
|
|
287
|
+
return {
|
|
288
|
+
lens: LENS_ID,
|
|
289
|
+
version: "1",
|
|
290
|
+
title: "State freshness",
|
|
291
|
+
kind: "truth",
|
|
292
|
+
status: "ran",
|
|
293
|
+
disclosures,
|
|
294
|
+
findings,
|
|
295
|
+
...outOfScope.length ? { outOfScope } : {}
|
|
296
|
+
};
|
|
297
|
+
}
|
|
298
|
+
};
|
|
299
|
+
async function listInstructionFiles(root, facts, scope) {
|
|
300
|
+
const files = [];
|
|
301
|
+
const add = async (rel) => {
|
|
302
|
+
const text = await readText(path3.join(root, rel));
|
|
303
|
+
if (text !== null) files.push({ path: normalizeRelPath(rel), text });
|
|
304
|
+
};
|
|
305
|
+
const singleFileArtifacts = [
|
|
306
|
+
"agents",
|
|
307
|
+
"claude",
|
|
308
|
+
"gemini",
|
|
309
|
+
"copilot",
|
|
310
|
+
"cursorrules",
|
|
311
|
+
"cline",
|
|
312
|
+
"windsurf"
|
|
313
|
+
];
|
|
314
|
+
for (const id of singleFileArtifacts) {
|
|
315
|
+
const artifact = facts.artifacts.find((a) => a.id === id);
|
|
316
|
+
if (artifact?.exists) await add(artifact.path);
|
|
317
|
+
}
|
|
318
|
+
const rulesDir = path3.join(root, ".cursor", "rules");
|
|
319
|
+
if (await isDirectory(rulesDir)) {
|
|
320
|
+
try {
|
|
321
|
+
for (const entry of await promises.readdir(rulesDir)) {
|
|
322
|
+
if (entry.endsWith(".md") || entry.endsWith(".mdc"))
|
|
323
|
+
await add(path3.join(".cursor/rules", entry));
|
|
324
|
+
}
|
|
325
|
+
} catch {
|
|
326
|
+
}
|
|
327
|
+
}
|
|
328
|
+
const skillsDir = path3.join(root, ".claude", "skills");
|
|
329
|
+
if (await isDirectory(skillsDir)) {
|
|
330
|
+
try {
|
|
331
|
+
for (const entry of await promises.readdir(skillsDir)) {
|
|
332
|
+
const skill = path3.join(".claude/skills", entry, "SKILL.md");
|
|
333
|
+
await add(skill);
|
|
334
|
+
}
|
|
335
|
+
} catch {
|
|
336
|
+
}
|
|
337
|
+
}
|
|
338
|
+
const detected = new Set(files.map((f) => f.path));
|
|
339
|
+
const included = [];
|
|
340
|
+
for (const rel of await expandFileGlobs(root, scope?.include ?? [])) {
|
|
341
|
+
if (detected.has(rel)) continue;
|
|
342
|
+
const before = files.length;
|
|
343
|
+
await add(rel);
|
|
344
|
+
if (files.length > before) included.push(rel);
|
|
345
|
+
}
|
|
346
|
+
const exclude = scope?.exclude ?? [];
|
|
347
|
+
if (!exclude.length) return { files, excluded: [], included };
|
|
348
|
+
const kept = [];
|
|
349
|
+
const excluded = [];
|
|
350
|
+
for (const file of files) {
|
|
351
|
+
if (matchesAnyGlob(file.path, exclude)) excluded.push(file.path);
|
|
352
|
+
else kept.push(file);
|
|
353
|
+
}
|
|
354
|
+
return { files: kept, excluded, included };
|
|
355
|
+
}
|
|
356
|
+
async function listStateDocuments(root, facts) {
|
|
357
|
+
const docs = [];
|
|
358
|
+
for (const artifact of facts.artifacts) {
|
|
359
|
+
if (artifact.kind !== "state" || !artifact.exists) continue;
|
|
360
|
+
const text = await readText(path3.join(root, artifact.path));
|
|
361
|
+
if (text !== null) docs.push({ path: normalizeRelPath(artifact.path), text });
|
|
362
|
+
}
|
|
363
|
+
return docs;
|
|
364
|
+
}
|
|
365
|
+
function extractCodeTokens(text) {
|
|
366
|
+
const tokens = [];
|
|
367
|
+
for (const m of text.matchAll(/`([^`\n]+)`/g)) tokens.push(m[1].trim());
|
|
368
|
+
for (const block of text.matchAll(/```[a-z]*\n([\s\S]*?)```/g)) {
|
|
369
|
+
for (const line of block[1].split("\n")) {
|
|
370
|
+
const trimmed = line.trim();
|
|
371
|
+
if (trimmed && !trimmed.startsWith("#")) tokens.push(trimmed);
|
|
372
|
+
}
|
|
373
|
+
}
|
|
374
|
+
return tokens;
|
|
375
|
+
}
|
|
376
|
+
var PM_BUILTINS = /* @__PURE__ */ new Set([
|
|
377
|
+
"install",
|
|
378
|
+
"i",
|
|
379
|
+
"add",
|
|
380
|
+
"remove",
|
|
381
|
+
"rm",
|
|
382
|
+
"up",
|
|
383
|
+
"update",
|
|
384
|
+
"upgrade",
|
|
385
|
+
"dlx",
|
|
386
|
+
"exec",
|
|
387
|
+
"create",
|
|
388
|
+
"init",
|
|
389
|
+
"link",
|
|
390
|
+
"unlink",
|
|
391
|
+
"publish",
|
|
392
|
+
"pack",
|
|
393
|
+
"audit",
|
|
394
|
+
"outdated",
|
|
395
|
+
"why",
|
|
396
|
+
"list",
|
|
397
|
+
"ls",
|
|
398
|
+
"view",
|
|
399
|
+
"info",
|
|
400
|
+
"config",
|
|
401
|
+
"store",
|
|
402
|
+
"import",
|
|
403
|
+
"rebuild",
|
|
404
|
+
"prune",
|
|
405
|
+
"setup",
|
|
406
|
+
"env",
|
|
407
|
+
"bin",
|
|
408
|
+
"root",
|
|
409
|
+
"licenses",
|
|
410
|
+
"patch",
|
|
411
|
+
"approve-builds",
|
|
412
|
+
"workspaces",
|
|
413
|
+
"workspace",
|
|
414
|
+
"cache",
|
|
415
|
+
"version",
|
|
416
|
+
"help"
|
|
417
|
+
]);
|
|
418
|
+
function extractCommandClaims(text) {
|
|
419
|
+
const scripts = /* @__PURE__ */ new Map();
|
|
420
|
+
let filteredSkipped = 0;
|
|
421
|
+
for (const token of extractCodeTokens(text)) {
|
|
422
|
+
for (const m of token.matchAll(
|
|
423
|
+
/(?:^|&&\s*|\|\|\s*|;\s*|\|\s*|\$\s+|\(\s*)(pnpm|yarn|npm|bun)\s+(?:(run)\s+)?(-{0,2}[A-Za-z0-9:._@/[\]-]+)/g
|
|
424
|
+
)) {
|
|
425
|
+
const pm = m[1];
|
|
426
|
+
const ranExplicit = Boolean(m[2]);
|
|
427
|
+
const arg = m[3];
|
|
428
|
+
if (arg.startsWith("-")) {
|
|
429
|
+
filteredSkipped += 1;
|
|
430
|
+
continue;
|
|
431
|
+
}
|
|
432
|
+
if (pm === "npm" && !ranExplicit && arg !== "test" && arg !== "start") continue;
|
|
433
|
+
if ((pm === "bun" || pm === "yarn" || pm === "pnpm") && !ranExplicit && PM_BUILTINS.has(arg))
|
|
434
|
+
continue;
|
|
435
|
+
if (ranExplicit && PM_BUILTINS.has(arg)) continue;
|
|
436
|
+
scripts.set(arg, token);
|
|
437
|
+
}
|
|
438
|
+
}
|
|
439
|
+
return { scripts, filteredSkipped };
|
|
440
|
+
}
|
|
441
|
+
var PATH_TOKEN_RE = /^[A-Za-z0-9_.-]+(\/[A-Za-z0-9_.$-]+)+\/?$/;
|
|
442
|
+
var KNOWN_EXTENSIONS = /* @__PURE__ */ new Set([
|
|
443
|
+
..."ts tsx cts mts js jsx cjs mjs json jsonc json5 md mdx mdc yml yaml toml ini cfg conf env sh bash zsh fish ps1 bat cmd css scss sass less html htm xml svg sql prisma graphql gql proto py rb rs go java kt kts swift c h cc cpp hpp cs php vue svelte astro txt log lock csv tsv png jpg jpeg gif webp ico avif woff woff2 ttf otf wasm map pem key crt tf tfvars example sample local snap ejs hbs pug".split(" ")
|
|
444
|
+
]);
|
|
445
|
+
var CREATION_CONTEXT_RE = /\b(?:creat(?:e|es|ed|ing)|generat(?:e|es|ed|ing)|scaffold(?:s|ed|ing)?|quarantin(?:e|es|ed|ing)|(?:writ(?:e|es|ten|ing)|output(?:s|ted)?|emit(?:s|ted|ting)?|sav(?:e|es|ed|ing)|mov(?:e|es|ed|ing)|copy|copi(?:es|ed))\s+(?:it\s+|them\s+)?(?:to|into)|new\s+(?:file|directory|folder)|will\s+(?:be\s+)?(?:created|generated|written)|add(?:s|ed|ing)?\s+(?:a|the)\s+new)\b/i;
|
|
446
|
+
var PLACEHOLDER_SEGMENTS = /* @__PURE__ */ new Set(["placeholder", "foo", "bar", "baz", "qux"]);
|
|
447
|
+
var PLACEHOLDER_PREFIX_RE = /^(?:my|your)-/i;
|
|
448
|
+
function isPlaceholderClaim(token) {
|
|
449
|
+
return token.split("/").some((seg) => PLACEHOLDER_PREFIX_RE.test(seg) || PLACEHOLDER_SEGMENTS.has(seg.toLowerCase()));
|
|
450
|
+
}
|
|
451
|
+
function claimContext(text, index) {
|
|
452
|
+
const start = text.lastIndexOf("\n", index) + 1;
|
|
453
|
+
const endRaw = text.indexOf("\n", index);
|
|
454
|
+
const end = endRaw === -1 ? text.length : endRaw;
|
|
455
|
+
const line = text.slice(start, end);
|
|
456
|
+
if (!/^\s*(?:[-*+]|\d+[.)]|\|)/.test(line)) return line;
|
|
457
|
+
let cursor = start;
|
|
458
|
+
while (cursor > 0) {
|
|
459
|
+
const prevEnd = cursor - 1;
|
|
460
|
+
const prevStart = text.lastIndexOf("\n", prevEnd - 1) + 1;
|
|
461
|
+
const prev = text.slice(prevStart, prevEnd);
|
|
462
|
+
cursor = prevStart;
|
|
463
|
+
if (!prev.trim()) continue;
|
|
464
|
+
if (/^\s*(?:[-*+]|\d+[.)]|\|)/.test(prev)) continue;
|
|
465
|
+
return `${prev}
|
|
466
|
+
${line}`;
|
|
467
|
+
}
|
|
468
|
+
return line;
|
|
469
|
+
}
|
|
470
|
+
function extractPathClaims(text) {
|
|
471
|
+
const prospectiveOnly = /* @__PURE__ */ new Map();
|
|
472
|
+
const placeholder = /* @__PURE__ */ new Set();
|
|
473
|
+
for (const m of text.matchAll(/`([^`\n]+)`/g)) {
|
|
474
|
+
const token = m[1].trim();
|
|
475
|
+
if (token.includes(" ") || token.length > 120) continue;
|
|
476
|
+
if (token.startsWith("/") || token.startsWith("~") || token.startsWith("@") || token.startsWith("$"))
|
|
477
|
+
continue;
|
|
478
|
+
if (token.includes("://") || token.startsWith("www.")) continue;
|
|
479
|
+
if (/[*?{}<>|]/.test(token)) continue;
|
|
480
|
+
if (token.includes("@")) continue;
|
|
481
|
+
if (!PATH_TOKEN_RE.test(token)) continue;
|
|
482
|
+
if (token.split("/").some((seg) => seg.startsWith("$"))) continue;
|
|
483
|
+
const isDirClaim = token.endsWith("/");
|
|
484
|
+
const ext = token.toLowerCase().match(/\.([a-z0-9]{1,8})$/)?.[1];
|
|
485
|
+
if (!isDirClaim && !(ext && KNOWN_EXTENSIONS.has(ext))) continue;
|
|
486
|
+
const claim = token.replace(/\/$/, "");
|
|
487
|
+
if (isPlaceholderClaim(claim)) {
|
|
488
|
+
placeholder.add(claim);
|
|
489
|
+
continue;
|
|
490
|
+
}
|
|
491
|
+
const prospective2 = CREATION_CONTEXT_RE.test(claimContext(text, m.index ?? 0));
|
|
492
|
+
prospectiveOnly.set(claim, (prospectiveOnly.get(claim) ?? true) && prospective2);
|
|
493
|
+
}
|
|
494
|
+
const paths = [];
|
|
495
|
+
const prospective = [];
|
|
496
|
+
for (const [claim, only] of prospectiveOnly) {
|
|
497
|
+
if (only) prospective.push(claim);
|
|
498
|
+
else paths.push(claim);
|
|
499
|
+
}
|
|
500
|
+
return { paths, prospective, placeholder: [...placeholder] };
|
|
501
|
+
}
|
|
502
|
+
var LOCAL_REF_LEADINS = new Set(
|
|
503
|
+
"decision decisions entry entries ruling rulings record records ledger id ids item items see per in of on at by to as is was are were the a an and or but not with under over from via vs than after before since between through against latest newest earliest only also still now supersedes superseded superseding amends amended extends extended cites cited citing adds added adding wrote written writes locked locks closed closes opened opens resolves resolved reopened recorded number numbers".split(" ")
|
|
504
|
+
);
|
|
505
|
+
function extractDecisionRefs(text) {
|
|
506
|
+
const byNum = /* @__PURE__ */ new Map();
|
|
507
|
+
for (const m of text.matchAll(/\bD-(\d{1,4})\b/g)) {
|
|
508
|
+
const index = m.index ?? 0;
|
|
509
|
+
if (index > 0 && /[-/_.]/.test(text[index - 1])) continue;
|
|
510
|
+
const before = text.slice(Math.max(0, index - 48), index);
|
|
511
|
+
const lead = /([A-Za-z][A-Za-z0-9'’-]*)[ \t]+$/.exec(before)?.[1];
|
|
512
|
+
const local = !lead || LOCAL_REF_LEADINS.has(lead.toLowerCase());
|
|
513
|
+
const num = Number(m[1]);
|
|
514
|
+
const seen = byNum.get(num);
|
|
515
|
+
if (!seen) byNum.set(num, { asWritten: m[0], local });
|
|
516
|
+
else seen.local = seen.local || local;
|
|
517
|
+
}
|
|
518
|
+
const refs = /* @__PURE__ */ new Map();
|
|
519
|
+
let qualifiedSkipped = 0;
|
|
520
|
+
for (const [num, ref] of byNum) {
|
|
521
|
+
if (ref.local) refs.set(num, ref.asWritten);
|
|
522
|
+
else qualifiedSkipped += 1;
|
|
523
|
+
}
|
|
524
|
+
return { refs, qualifiedSkipped };
|
|
525
|
+
}
|
|
526
|
+
function packageManagerUsage(text) {
|
|
527
|
+
const counts = /* @__PURE__ */ new Map();
|
|
528
|
+
for (const token of extractCodeTokens(text)) {
|
|
529
|
+
for (const m of token.matchAll(/\b(pnpm|yarn|npm|bun)\s+(?:run\s+)?[A-Za-z-]/g)) {
|
|
530
|
+
const pm = m[1];
|
|
531
|
+
counts.set(pm, (counts.get(pm) ?? 0) + 1);
|
|
532
|
+
}
|
|
533
|
+
}
|
|
534
|
+
return counts;
|
|
535
|
+
}
|
|
536
|
+
var KNOWN_DOC_REFS = [
|
|
537
|
+
"AGENTS.md",
|
|
538
|
+
"CLAUDE.md",
|
|
539
|
+
"PROJECT_CONTEXT.md",
|
|
540
|
+
"DECISIONS.md",
|
|
541
|
+
"GEMINI.md"
|
|
542
|
+
];
|
|
543
|
+
var PATH_TOKEN_CHARS = /[A-Za-z0-9_.$~/-]/;
|
|
544
|
+
function extractDocRefs(text) {
|
|
545
|
+
const refs = [];
|
|
546
|
+
let tildeSkipped = 0;
|
|
547
|
+
for (const name of KNOWN_DOC_REFS) {
|
|
548
|
+
let claimed = false;
|
|
549
|
+
let at = text.indexOf(name);
|
|
550
|
+
while (at !== -1) {
|
|
551
|
+
let head = at;
|
|
552
|
+
while (head > 0 && PATH_TOKEN_CHARS.test(text[head - 1])) head -= 1;
|
|
553
|
+
if (text[head] === "~") tildeSkipped += 1;
|
|
554
|
+
else claimed = true;
|
|
555
|
+
at = text.indexOf(name, at + name.length);
|
|
556
|
+
}
|
|
557
|
+
if (claimed) refs.push(name);
|
|
558
|
+
}
|
|
559
|
+
return { refs, tildeSkipped };
|
|
560
|
+
}
|
|
561
|
+
async function buildTruthEnv(root, facts) {
|
|
562
|
+
const knownScripts = new Set(Object.keys(facts.commands.raw));
|
|
563
|
+
for (const pkg of facts.packages) {
|
|
564
|
+
const pkgJson = await readJson(
|
|
565
|
+
path3.join(root, pkg.dir, "package.json")
|
|
566
|
+
);
|
|
567
|
+
for (const key of Object.keys(pkgJson?.scripts ?? {})) knownScripts.add(key);
|
|
568
|
+
}
|
|
569
|
+
const bases = [root, ...facts.packages.map((p) => path3.join(root, p.dir))];
|
|
570
|
+
const pathResolves = async (claim) => {
|
|
571
|
+
for (const base of bases) {
|
|
572
|
+
if (await pathExists(path3.join(base, claim))) return true;
|
|
573
|
+
if (await pathExists(path3.join(base, "src", claim))) return true;
|
|
574
|
+
if (await pathExists(path3.join(base, "scripts", claim))) return true;
|
|
575
|
+
}
|
|
576
|
+
return false;
|
|
577
|
+
};
|
|
578
|
+
const binResolves = async (name) => {
|
|
579
|
+
for (const base of bases) {
|
|
580
|
+
if (await pathExists(path3.join(base, "node_modules", ".bin", name))) return true;
|
|
581
|
+
}
|
|
582
|
+
return false;
|
|
583
|
+
};
|
|
584
|
+
const nodeModulesInstalled = await pathExists(path3.join(root, "node_modules"));
|
|
585
|
+
const manifestExists = await pathExists(path3.join(root, "package.json")) || facts.packages.length > 0;
|
|
586
|
+
return {
|
|
587
|
+
root,
|
|
588
|
+
facts,
|
|
589
|
+
knownScripts,
|
|
590
|
+
nodeModulesInstalled,
|
|
591
|
+
manifestExists,
|
|
592
|
+
bases,
|
|
593
|
+
pathResolves,
|
|
594
|
+
binResolves
|
|
595
|
+
};
|
|
596
|
+
}
|
|
597
|
+
function emptyCounters() {
|
|
598
|
+
return {
|
|
599
|
+
filteredSkipped: 0,
|
|
600
|
+
tildeSkipped: 0,
|
|
601
|
+
binaryResolved: 0,
|
|
602
|
+
unverifiableCommands: 0,
|
|
603
|
+
gitignoredSkipped: 0,
|
|
604
|
+
prospectiveSkipped: 0,
|
|
605
|
+
placeholderSkipped: 0,
|
|
606
|
+
qualifiedRefsSkipped: 0,
|
|
607
|
+
unresolvableRefs: 0
|
|
608
|
+
};
|
|
609
|
+
}
|
|
610
|
+
async function checkTextClaims(env, file, opts, counters) {
|
|
611
|
+
const findings = [];
|
|
612
|
+
const disclosures = [];
|
|
613
|
+
const examined = [];
|
|
614
|
+
const subject = opts.subject ?? file.path;
|
|
615
|
+
const { scripts: claimed, filteredSkipped } = extractCommandClaims(file.text);
|
|
616
|
+
counters.filteredSkipped += filteredSkipped;
|
|
617
|
+
for (const [script, raw] of claimed) {
|
|
618
|
+
if (env.knownScripts.has(script)) {
|
|
619
|
+
examined.push({ kind: "script", value: script, exists: true });
|
|
620
|
+
continue;
|
|
621
|
+
}
|
|
622
|
+
if (await env.binResolves(script)) {
|
|
623
|
+
counters.binaryResolved += 1;
|
|
624
|
+
examined.push({ kind: "script", value: script, exists: true });
|
|
625
|
+
continue;
|
|
626
|
+
}
|
|
627
|
+
if (!env.nodeModulesInstalled && env.manifestExists) {
|
|
628
|
+
counters.unverifiableCommands += 1;
|
|
629
|
+
examined.push({ kind: "script", value: script, exists: null });
|
|
630
|
+
continue;
|
|
631
|
+
}
|
|
632
|
+
examined.push({ kind: "script", value: script, exists: false });
|
|
633
|
+
findings.push({
|
|
634
|
+
lens: opts.lensId,
|
|
635
|
+
id: `${opts.lensId}/stale-command:${file.path}:${script}`,
|
|
636
|
+
tier: "risk",
|
|
637
|
+
claim: `${subject} tells agents to run \`${script}\` \u2014 no such script exists`,
|
|
638
|
+
evidence: [`${file.path}: \`${raw}\``, "package.json scripts (root + workspaces)"],
|
|
639
|
+
why: opts.whyCommand ?? "An agent following this instruction runs a command that fails \u2014 or silently skips the check it was meant to run.",
|
|
640
|
+
action: opts.actionCommand ?? "Update the instruction to the current script name (or restore the script).",
|
|
641
|
+
effort: "S",
|
|
642
|
+
confidence: "high"
|
|
643
|
+
});
|
|
644
|
+
}
|
|
645
|
+
const { paths, prospective, placeholder } = extractPathClaims(file.text);
|
|
646
|
+
counters.prospectiveSkipped += prospective.length;
|
|
647
|
+
counters.placeholderSkipped += placeholder.length;
|
|
648
|
+
const missing = [];
|
|
649
|
+
for (const claim of paths) {
|
|
650
|
+
if (await env.pathResolves(claim)) examined.push({ kind: "path", value: claim, exists: true });
|
|
651
|
+
else missing.push(claim);
|
|
652
|
+
}
|
|
653
|
+
const ignoredOut = missing.length ? await git(env.root, ["check-ignore", ...missing]) : null;
|
|
654
|
+
const gitignored = new Set((ignoredOut ?? "").split("\n").filter(Boolean));
|
|
655
|
+
let pathFindings = 0;
|
|
656
|
+
for (const claim of missing) {
|
|
657
|
+
if (gitignored.has(claim)) {
|
|
658
|
+
counters.gitignoredSkipped += 1;
|
|
659
|
+
examined.push({ kind: "path", value: claim, exists: null });
|
|
660
|
+
continue;
|
|
661
|
+
}
|
|
662
|
+
examined.push({ kind: "path", value: claim, exists: false });
|
|
663
|
+
if (pathFindings >= opts.maxPathFindings) {
|
|
664
|
+
if (pathFindings === opts.maxPathFindings) {
|
|
665
|
+
disclosures.push(
|
|
666
|
+
`${file.path}: more than ${opts.maxPathFindings} missing-path claims \u2014 truncated.`
|
|
667
|
+
);
|
|
668
|
+
pathFindings += 1;
|
|
669
|
+
}
|
|
670
|
+
continue;
|
|
671
|
+
}
|
|
672
|
+
pathFindings += 1;
|
|
673
|
+
findings.push({
|
|
674
|
+
lens: opts.lensId,
|
|
675
|
+
id: `${opts.lensId}/stale-path:${file.path}:${claim}`,
|
|
676
|
+
tier: opts.missingPathTier,
|
|
677
|
+
claim: `${subject} references \`${claim}\` \u2014 it does not exist in the repo`,
|
|
678
|
+
evidence: [file.path, `missing: ${claim}`],
|
|
679
|
+
why: opts.whyPath ?? "Agents navigate by these references; a dead path wastes a lookup and erodes trust in the rest of the file.",
|
|
680
|
+
action: opts.actionPath ?? "Fix or remove the reference.",
|
|
681
|
+
effort: "S",
|
|
682
|
+
confidence: "medium"
|
|
683
|
+
});
|
|
684
|
+
}
|
|
685
|
+
return { findings, disclosures, examined };
|
|
686
|
+
}
|
|
687
|
+
async function checkDocRefs(env, file, lensId, counters, subject = file.path) {
|
|
688
|
+
const findings = [];
|
|
689
|
+
const examined = [];
|
|
690
|
+
const { refs, tildeSkipped } = extractDocRefs(file.text);
|
|
691
|
+
counters.tildeSkipped += tildeSkipped;
|
|
692
|
+
for (const ref of refs) {
|
|
693
|
+
const exists = await pathExists(path3.join(env.root, ref));
|
|
694
|
+
examined.push({ kind: "doc", value: ref, exists });
|
|
695
|
+
if (exists) continue;
|
|
696
|
+
findings.push({
|
|
697
|
+
lens: lensId,
|
|
698
|
+
id: `${lensId}/dangling-ref:${file.path}:${ref}`,
|
|
699
|
+
tier: "gap",
|
|
700
|
+
claim: `${subject} references ${ref} \u2014 no such file exists`,
|
|
701
|
+
evidence: [file.path, `missing: ${ref}`],
|
|
702
|
+
why: "The pointer chain agents follow breaks at a file they can never read.",
|
|
703
|
+
action: `Create ${ref} or remove the reference.`,
|
|
704
|
+
effort: "S",
|
|
705
|
+
confidence: "high"
|
|
706
|
+
});
|
|
707
|
+
}
|
|
708
|
+
return { findings, examined };
|
|
709
|
+
}
|
|
710
|
+
async function loadDecisionLedger(root, facts) {
|
|
711
|
+
let ids = null;
|
|
712
|
+
const sources = [];
|
|
713
|
+
for (const artifact of facts.artifacts) {
|
|
714
|
+
if (artifact.kind !== "decisions" || !artifact.exists) continue;
|
|
715
|
+
const text = await readText(path3.join(root, artifact.path));
|
|
716
|
+
if (text === null) continue;
|
|
717
|
+
const entries = parseDecisionEntries(text);
|
|
718
|
+
if (!entries.length) continue;
|
|
719
|
+
ids ??= /* @__PURE__ */ new Set();
|
|
720
|
+
for (const entry of entries) ids.add(entry.num);
|
|
721
|
+
sources.push(artifact.path);
|
|
722
|
+
}
|
|
723
|
+
return { ids, sources };
|
|
724
|
+
}
|
|
725
|
+
function checkDecisionRefs(file, ledger, opts, counters) {
|
|
726
|
+
const findings = [];
|
|
727
|
+
const examined = [];
|
|
728
|
+
const subject = opts.subject ?? file.path;
|
|
729
|
+
const { refs, qualifiedSkipped } = extractDecisionRefs(file.text);
|
|
730
|
+
counters.qualifiedRefsSkipped += qualifiedSkipped;
|
|
731
|
+
if (!refs.size) return { findings, examined };
|
|
732
|
+
if (!ledger.ids) {
|
|
733
|
+
counters.unresolvableRefs += refs.size;
|
|
734
|
+
for (const asWritten of refs.values()) {
|
|
735
|
+
examined.push({ kind: "decision", value: asWritten, exists: null });
|
|
736
|
+
}
|
|
737
|
+
return { findings, examined };
|
|
738
|
+
}
|
|
739
|
+
for (const [num, asWritten] of refs) {
|
|
740
|
+
const exists = ledger.ids.has(num);
|
|
741
|
+
examined.push({ kind: "decision", value: asWritten, exists });
|
|
742
|
+
if (exists) continue;
|
|
743
|
+
findings.push({
|
|
744
|
+
lens: opts.lensId,
|
|
745
|
+
id: `${opts.lensId}/dead-decision-ref:${file.path}:${asWritten}`,
|
|
746
|
+
tier: "gap",
|
|
747
|
+
claim: `${subject} cites ${asWritten} \u2014 no such entry exists in ${ledger.sources.join(", ")}`,
|
|
748
|
+
evidence: [file.path, `${ledger.sources.join(", ")}: no ${asWritten} entry`],
|
|
749
|
+
why: opts.why ?? "A state doc is read as ground truth on return; a citation the decision record cannot back sends readers to a ruling that was never written.",
|
|
750
|
+
action: opts.action ?? "Fix the reference \u2014 or record the missing decision.",
|
|
751
|
+
effort: "S",
|
|
752
|
+
confidence: "medium"
|
|
753
|
+
});
|
|
754
|
+
}
|
|
755
|
+
return { findings, examined };
|
|
756
|
+
}
|
|
757
|
+
|
|
758
|
+
export { KNOWN_EXTENSIONS, PATH_TOKEN_RE, buildTruthEnv, checkDecisionRefs, checkDocRefs, checkTextClaims, emptyCounters, listInstructionFiles, listStateDocuments, loadDecisionLedger, meetsFailOn, packageManagerUsage, parseFailOnTier, rankFindings, stateFreshnessLens };
|