@ybotok/loam 0.0.0 → 0.1.0-beta.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.
- package/CHANGELOG.md +89 -0
- package/COMPARISON.md +63 -0
- package/LICENSE +21 -0
- package/MIGRATING-from-OpenSpec.md +202 -0
- package/README.md +241 -9
- package/SCHEMA.md +425 -0
- package/SECURITY.md +9 -0
- package/dist/cli.js +138 -0
- package/dist/cli.js.map +1 -0
- package/dist/commands/adopt.js +226 -0
- package/dist/commands/adopt.js.map +1 -0
- package/dist/commands/archive.js +1297 -0
- package/dist/commands/archive.js.map +1 -0
- package/dist/commands/delta.js +361 -0
- package/dist/commands/delta.js.map +1 -0
- package/dist/commands/dependencies.js +78 -0
- package/dist/commands/dependencies.js.map +1 -0
- package/dist/commands/doctor.js +85 -0
- package/dist/commands/doctor.js.map +1 -0
- package/dist/commands/gherkin.js +270 -0
- package/dist/commands/gherkin.js.map +1 -0
- package/dist/commands/init.js +250 -0
- package/dist/commands/init.js.map +1 -0
- package/dist/commands/list.js +361 -0
- package/dist/commands/list.js.map +1 -0
- package/dist/commands/migrate-openspec.js +933 -0
- package/dist/commands/migrate-openspec.js.map +1 -0
- package/dist/commands/new.js +412 -0
- package/dist/commands/new.js.map +1 -0
- package/dist/commands/rebase.js +358 -0
- package/dist/commands/rebase.js.map +1 -0
- package/dist/commands/show.js +325 -0
- package/dist/commands/show.js.map +1 -0
- package/dist/commands/status.js +270 -0
- package/dist/commands/status.js.map +1 -0
- package/dist/commands/unarchive.js +310 -0
- package/dist/commands/unarchive.js.map +1 -0
- package/dist/commands/validate.js +1609 -0
- package/dist/commands/validate.js.map +1 -0
- package/dist/commands/verify.js +759 -0
- package/dist/commands/verify.js.map +1 -0
- package/dist/commands/vouch.js +325 -0
- package/dist/commands/vouch.js.map +1 -0
- package/dist/core/agent.js +1823 -0
- package/dist/core/agent.js.map +1 -0
- package/dist/core/agents-stamp.js +68 -0
- package/dist/core/agents-stamp.js.map +1 -0
- package/dist/core/arch.js +104 -0
- package/dist/core/arch.js.map +1 -0
- package/dist/core/brief.js +733 -0
- package/dist/core/brief.js.map +1 -0
- package/dist/core/coherence.js +553 -0
- package/dist/core/coherence.js.map +1 -0
- package/dist/core/config.js +172 -0
- package/dist/core/config.js.map +1 -0
- package/dist/core/delta.js +457 -0
- package/dist/core/delta.js.map +1 -0
- package/dist/core/dependencies.js +352 -0
- package/dist/core/dependencies.js.map +1 -0
- package/dist/core/docs.js +143 -0
- package/dist/core/docs.js.map +1 -0
- package/dist/core/doctor.js +544 -0
- package/dist/core/doctor.js.map +1 -0
- package/dist/core/fleet-context.js +272 -0
- package/dist/core/fleet-context.js.map +1 -0
- package/dist/core/frontmatter.js +203 -0
- package/dist/core/frontmatter.js.map +1 -0
- package/dist/core/gherkin.js +521 -0
- package/dist/core/gherkin.js.map +1 -0
- package/dist/core/health.js +71 -0
- package/dist/core/health.js.map +1 -0
- package/dist/core/ids.js +93 -0
- package/dist/core/ids.js.map +1 -0
- package/dist/core/issue.js +9 -0
- package/dist/core/issue.js.map +1 -0
- package/dist/core/json.js +67 -0
- package/dist/core/json.js.map +1 -0
- package/dist/core/likec4.js +474 -0
- package/dist/core/likec4.js.map +1 -0
- package/dist/core/openapi-merge.js +519 -0
- package/dist/core/openapi-merge.js.map +1 -0
- package/dist/core/openapi.js +243 -0
- package/dist/core/openapi.js.map +1 -0
- package/dist/core/openspec-inventory.js +1403 -0
- package/dist/core/openspec-inventory.js.map +1 -0
- package/dist/core/path-safety.js +109 -0
- package/dist/core/path-safety.js.map +1 -0
- package/dist/core/provenance.js +695 -0
- package/dist/core/provenance.js.map +1 -0
- package/dist/core/repo.js +362 -0
- package/dist/core/repo.js.map +1 -0
- package/dist/core/report.js +38 -0
- package/dist/core/report.js.map +1 -0
- package/dist/core/results.js +201 -0
- package/dist/core/results.js.map +1 -0
- package/dist/core/spec.js +481 -0
- package/dist/core/spec.js.map +1 -0
- package/dist/core/staging.js +783 -0
- package/dist/core/staging.js.map +1 -0
- package/dist/core/status.js +1015 -0
- package/dist/core/status.js.map +1 -0
- package/dist/core/verify.js +793 -0
- package/dist/core/verify.js.map +1 -0
- package/dist/core/version.js +8 -0
- package/dist/core/version.js.map +1 -0
- package/package.json +63 -4
|
@@ -0,0 +1,695 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Provenance checks: is this artifact anchored to anything, and has anyone
|
|
3
|
+
* vouched for it?
|
|
4
|
+
*
|
|
5
|
+
* SCHEMA.md has declared `status` / `owner` / `sources` / `last_verified` since
|
|
6
|
+
* the beginning and nothing read them. They matter more now that the prose is
|
|
7
|
+
* written by an agent: coherence proves the corpus agrees with itself, and only
|
|
8
|
+
* `sources` says it has anything to do with the code.
|
|
9
|
+
*
|
|
10
|
+
* Absence and contradiction are graded differently on purpose. A missing field
|
|
11
|
+
* is incompleteness — a warning, something to work through. A field that says
|
|
12
|
+
* the wrong thing (a spec claiming to be another service, a status nobody
|
|
13
|
+
* defined) is a bug, and gates.
|
|
14
|
+
*/
|
|
15
|
+
import { spawn } from "node:child_process";
|
|
16
|
+
import { createHash } from "node:crypto";
|
|
17
|
+
import { existsSync } from "node:fs";
|
|
18
|
+
import { readdir, readFile, realpath, stat } from "node:fs/promises";
|
|
19
|
+
import { join, relative, resolve, sep } from "node:path";
|
|
20
|
+
import { listField, parseFrontmatter, rawBody, readFrontmatter, stringField, FEATURE_STATUSES, SERVICE_STATUSES, } from "./frontmatter.js";
|
|
21
|
+
import { featurePaths, servicePaths } from "./repo.js";
|
|
22
|
+
import { isPathInside, resolveInside } from "./path-safety.js";
|
|
23
|
+
/** Fields every artifact is expected to carry, beyond its identity and status. */
|
|
24
|
+
const EXPECTED = ["owner"];
|
|
25
|
+
export async function serviceProvenance(docsDir, service, opts = {}) {
|
|
26
|
+
const paths = servicePaths(docsDir, service);
|
|
27
|
+
const findings = [];
|
|
28
|
+
// Both requirement-carrying specs get the same pass: arch.spec.md follows
|
|
29
|
+
// spec.md's frontmatter conventions exactly, so the checks are one loop —
|
|
30
|
+
// only the label tells a reader which file moved.
|
|
31
|
+
for (const { path, file } of [
|
|
32
|
+
{ path: paths.spec, file: "spec.md" },
|
|
33
|
+
{ path: paths.archSpec, file: "arch.spec.md" },
|
|
34
|
+
]) {
|
|
35
|
+
if (!existsSync(path))
|
|
36
|
+
continue;
|
|
37
|
+
const raw = await readFile(path, "utf8");
|
|
38
|
+
const fm = parseFrontmatter(raw);
|
|
39
|
+
const label = `${service}: ${file}`;
|
|
40
|
+
findings.push(...identityFindings(fm, {
|
|
41
|
+
label,
|
|
42
|
+
idField: "service",
|
|
43
|
+
idValue: service,
|
|
44
|
+
statuses: [...SERVICE_STATUSES],
|
|
45
|
+
}));
|
|
46
|
+
// With no frontmatter at all, "it names no sources" says nothing the missing
|
|
47
|
+
// header did not already say — and a malformed one proves nothing either:
|
|
48
|
+
// `sources.absent` against a header nobody can read is the same false
|
|
49
|
+
// advice as the field cascade, so frontmatter.malformed stands alone.
|
|
50
|
+
if (fm.present && !fm.malformed) {
|
|
51
|
+
findings.push(...(await sourceFindings(fm, service, label, opts.repoDir)));
|
|
52
|
+
findings.push(...contentFindings(fm, raw, service, label));
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
return findings;
|
|
56
|
+
}
|
|
57
|
+
export async function featureProvenance(featureDir, featureId) {
|
|
58
|
+
const path = featurePaths(featureDir).intent;
|
|
59
|
+
if (!existsSync(path))
|
|
60
|
+
return [];
|
|
61
|
+
const fm = await readFrontmatter(path);
|
|
62
|
+
return identityFindings(fm, {
|
|
63
|
+
label: "intent.md",
|
|
64
|
+
idField: "feature",
|
|
65
|
+
idValue: featureId,
|
|
66
|
+
statuses: [...FEATURE_STATUSES],
|
|
67
|
+
});
|
|
68
|
+
}
|
|
69
|
+
function identityFindings(fm, spec) {
|
|
70
|
+
const findings = [];
|
|
71
|
+
if (!fm.present) {
|
|
72
|
+
return [
|
|
73
|
+
{
|
|
74
|
+
severity: "warn",
|
|
75
|
+
code: "frontmatter.missing",
|
|
76
|
+
subject: spec.idValue,
|
|
77
|
+
message: `${spec.label} has no frontmatter — no owner, no status, no link to the code it describes`,
|
|
78
|
+
},
|
|
79
|
+
];
|
|
80
|
+
}
|
|
81
|
+
// One honest error instead of the cascade: with an unreadable header the
|
|
82
|
+
// field checks below would grade owner/status/service "missing" and send the
|
|
83
|
+
// author adding fields to a block YAML refuses to parse. Nothing can be
|
|
84
|
+
// concluded from a header nobody can read — including that it lacks fields.
|
|
85
|
+
if (fm.malformed) {
|
|
86
|
+
return [
|
|
87
|
+
{
|
|
88
|
+
severity: "error",
|
|
89
|
+
code: "frontmatter.malformed",
|
|
90
|
+
subject: spec.idValue,
|
|
91
|
+
message: `${spec.label} has frontmatter that does not parse as YAML — owner, status and sources are unreadable until the header is fixed`,
|
|
92
|
+
},
|
|
93
|
+
];
|
|
94
|
+
}
|
|
95
|
+
const id = stringField(fm, spec.idField);
|
|
96
|
+
if (id !== undefined && id !== spec.idValue) {
|
|
97
|
+
findings.push({
|
|
98
|
+
severity: "error",
|
|
99
|
+
code: "frontmatter.field-mismatch",
|
|
100
|
+
subject: spec.idValue,
|
|
101
|
+
message: `${spec.label} declares ${spec.idField}: ${id}, but it lives under ${spec.idValue}`,
|
|
102
|
+
});
|
|
103
|
+
}
|
|
104
|
+
const status = stringField(fm, "status");
|
|
105
|
+
if (status !== undefined && !spec.statuses.includes(status)) {
|
|
106
|
+
findings.push({
|
|
107
|
+
severity: "error",
|
|
108
|
+
code: "frontmatter.status-unknown",
|
|
109
|
+
subject: spec.idValue,
|
|
110
|
+
message: `${spec.label} has status '${status}' — expected one of ${spec.statuses.join(", ")}`,
|
|
111
|
+
});
|
|
112
|
+
}
|
|
113
|
+
const missing = [
|
|
114
|
+
...(id === undefined ? [spec.idField] : []),
|
|
115
|
+
...(status === undefined ? ["status"] : []),
|
|
116
|
+
...EXPECTED.filter((f) => stringField(fm, f) === undefined),
|
|
117
|
+
];
|
|
118
|
+
if (missing.length > 0) {
|
|
119
|
+
findings.push({
|
|
120
|
+
severity: "warn",
|
|
121
|
+
code: "frontmatter.field-missing",
|
|
122
|
+
subject: spec.idValue,
|
|
123
|
+
message: `${spec.label} is missing ${missing.length} frontmatter field(s)`,
|
|
124
|
+
details: missing,
|
|
125
|
+
text: { detailPrefix: "- " },
|
|
126
|
+
});
|
|
127
|
+
}
|
|
128
|
+
return findings;
|
|
129
|
+
}
|
|
130
|
+
async function sourceFindings(fm, service, label, repoDir) {
|
|
131
|
+
const sources = listField(fm, "sources");
|
|
132
|
+
if (sources.length === 0) {
|
|
133
|
+
return [
|
|
134
|
+
{
|
|
135
|
+
severity: "warn",
|
|
136
|
+
code: "sources.absent",
|
|
137
|
+
message: `${label} names no sources — nothing ties it to the code, so nothing can tell you when it goes out of date`,
|
|
138
|
+
},
|
|
139
|
+
];
|
|
140
|
+
}
|
|
141
|
+
// Someone else's repository: the paths describe a tree loam is not standing in.
|
|
142
|
+
if (repoDir === undefined)
|
|
143
|
+
return [];
|
|
144
|
+
// Before existence: a pattern entry would "not exist" as a literal path, and
|
|
145
|
+
// grading it missing would send the author fixing the wrong thing.
|
|
146
|
+
const patterns = patternSources(sources);
|
|
147
|
+
if (patterns.length > 0) {
|
|
148
|
+
return [
|
|
149
|
+
{
|
|
150
|
+
severity: "error",
|
|
151
|
+
code: "sources.path-missing",
|
|
152
|
+
message: `${label}: ${patterns.length} source(s) are glob patterns — ${patterns.join(", ")}. Patterns are no longer supported: name files or directories (a directory already covers everything beneath it).`,
|
|
153
|
+
details: patterns,
|
|
154
|
+
text: { detailPrefix: "- " },
|
|
155
|
+
},
|
|
156
|
+
];
|
|
157
|
+
}
|
|
158
|
+
const unsafe = unsafeSources(repoDir, sources);
|
|
159
|
+
if (unsafe.length > 0) {
|
|
160
|
+
return [
|
|
161
|
+
{
|
|
162
|
+
severity: "error",
|
|
163
|
+
code: "sources.path-outside",
|
|
164
|
+
message: `${label}: ${unsafe.length} source(s) escape the service repo — ${unsafe.join(", ")}. Sources must be relative paths contained by this repository, including through symlinks.`,
|
|
165
|
+
details: unsafe,
|
|
166
|
+
text: { detailPrefix: "- " },
|
|
167
|
+
},
|
|
168
|
+
];
|
|
169
|
+
}
|
|
170
|
+
const missing = missingSources(repoDir, sources);
|
|
171
|
+
if (missing.length > 0) {
|
|
172
|
+
return [
|
|
173
|
+
{
|
|
174
|
+
severity: "error",
|
|
175
|
+
code: "sources.path-missing",
|
|
176
|
+
message: `${label}: ${missing.length} source(s) do not exist — ${missing.join(", ")}`,
|
|
177
|
+
details: missing,
|
|
178
|
+
text: { detailPrefix: "- " },
|
|
179
|
+
},
|
|
180
|
+
];
|
|
181
|
+
}
|
|
182
|
+
const resolved = {
|
|
183
|
+
severity: "ok",
|
|
184
|
+
code: "sources.resolved",
|
|
185
|
+
message: `${label}: ${sources.length} source(s) resolve`,
|
|
186
|
+
};
|
|
187
|
+
// The walk happens here rather than inside the staleness branch below, even
|
|
188
|
+
// though only staleness needs the hashes: what the walk REFUSED to follow is
|
|
189
|
+
// a hole in the tie to the code whether or not anybody has vouched yet, and
|
|
190
|
+
// an unvouched service is exactly where a reader is about to decide the
|
|
191
|
+
// sources list is honest.
|
|
192
|
+
const { digest, files, index, skipped } = await sourcesDigest(repoDir, sources);
|
|
193
|
+
const findings = [resolved, ...skippedFindings(skipped, label)];
|
|
194
|
+
// The paths are there; the question staleness answers is whether what is AT
|
|
195
|
+
// them is still what somebody read.
|
|
196
|
+
const stamped = stringField(fm, "sources_digest");
|
|
197
|
+
if (stamped === undefined) {
|
|
198
|
+
return [
|
|
199
|
+
...findings,
|
|
200
|
+
{
|
|
201
|
+
severity: "warn",
|
|
202
|
+
code: "sources.unvouched",
|
|
203
|
+
message: `${label}: no sources_digest — nobody has vouched for this against the code, so nothing can tell you when it goes stale. Run \`loam vouch --service ${service}\`.`,
|
|
204
|
+
},
|
|
205
|
+
];
|
|
206
|
+
}
|
|
207
|
+
const since = stringField(fm, "last_verified") ?? "it was stamped";
|
|
208
|
+
if (digest === stamped) {
|
|
209
|
+
return [
|
|
210
|
+
...findings,
|
|
211
|
+
{
|
|
212
|
+
severity: "ok",
|
|
213
|
+
code: "sources.current",
|
|
214
|
+
message: `${label}: sources unchanged since ${since} (${files.length} file(s), digest ${digest})`,
|
|
215
|
+
},
|
|
216
|
+
];
|
|
217
|
+
}
|
|
218
|
+
// What MOVED, when the stamp recorded enough to say. Repeating the frontmatter's
|
|
219
|
+
// own `sources` entries back at a reader — which is all this finding could do
|
|
220
|
+
// before `sources_files` existed — answers a question nobody asked: they wrote
|
|
221
|
+
// that list, and "one of these five directories changed somehow" is where the
|
|
222
|
+
// search starts, not where it ends.
|
|
223
|
+
const moved = movedSources(decodeSourceIndex(stringField(fm, "sources_files")), index);
|
|
224
|
+
return [
|
|
225
|
+
...findings,
|
|
226
|
+
{
|
|
227
|
+
// A warning, not an error: the doc may still be right, and only a person
|
|
228
|
+
// can say. What loam knows is that nobody has looked since the code moved.
|
|
229
|
+
severity: "warn",
|
|
230
|
+
code: "sources.stale",
|
|
231
|
+
message: `${label}: sources changed since ${since}${moved.summary} — re-read them and \`loam vouch --service ${service}\``,
|
|
232
|
+
details: moved.paths ?? sources,
|
|
233
|
+
text: { detailPrefix: "- " },
|
|
234
|
+
},
|
|
235
|
+
];
|
|
236
|
+
}
|
|
237
|
+
/**
|
|
238
|
+
* The paths the digest walk would not follow. Never silent: a symlink dropped
|
|
239
|
+
* on the floor is a file whose content nobody is watching, while the document
|
|
240
|
+
* beside it says `verified` — the digest cannot go stale over bytes it never
|
|
241
|
+
* hashed, so the one thing the stamp promises is quietly untrue for them.
|
|
242
|
+
*
|
|
243
|
+
* A warning, not an error, and for the same reason staleness is: the doc may be
|
|
244
|
+
* perfectly right about the code behind that link. What loam can say is that it
|
|
245
|
+
* is not watching it.
|
|
246
|
+
*/
|
|
247
|
+
function skippedFindings(skipped, label) {
|
|
248
|
+
if (skipped.length === 0)
|
|
249
|
+
return [];
|
|
250
|
+
return [
|
|
251
|
+
{
|
|
252
|
+
severity: "warn",
|
|
253
|
+
code: "sources.skipped",
|
|
254
|
+
message: `${label}: ${skipped.length} path(s) under the listed sources were not hashed — the digest says nothing about what is behind them`,
|
|
255
|
+
details: skipped.map((s) => `${s.path} — ${s.reason}`),
|
|
256
|
+
text: { detailPrefix: "- " },
|
|
257
|
+
},
|
|
258
|
+
];
|
|
259
|
+
}
|
|
260
|
+
/**
|
|
261
|
+
* The doc-side half of the freshness check. `sources_digest` says whether the
|
|
262
|
+
* CODE moved since a person looked, and only that service's repo can recompute
|
|
263
|
+
* it; `content_digest` says whether the DOCUMENT did, and needs nothing but
|
|
264
|
+
* the document — so this runs wherever the spec is readable, `--service` and
|
|
265
|
+
* `--all` alike. Without it, editing a spec after it was vouched left
|
|
266
|
+
* `status: verified` standing over words nobody read — forged freshness, in
|
|
267
|
+
* the exact agent-written-prose threat model this layer exists for.
|
|
268
|
+
*/
|
|
269
|
+
function contentFindings(fm, raw, service, label) {
|
|
270
|
+
if (stringField(fm, "status") !== "verified")
|
|
271
|
+
return [];
|
|
272
|
+
const stamped = stringField(fm, "content_digest");
|
|
273
|
+
// A verified doc with no stamp predates the field. Quiet on purpose: the fix
|
|
274
|
+
// is re-vouching, and grading years of pre-feature vouches as suspect would
|
|
275
|
+
// drown the fleet in warnings nobody chose.
|
|
276
|
+
if (stamped === undefined)
|
|
277
|
+
return [];
|
|
278
|
+
if (contentDigest(raw) === stamped)
|
|
279
|
+
return [];
|
|
280
|
+
const since = stringField(fm, "last_verified") ?? "it was vouched";
|
|
281
|
+
return [
|
|
282
|
+
{
|
|
283
|
+
// A warning, not an error — the sources.stale doctrine: the doc changed
|
|
284
|
+
// since it was vouched, and only a person can say whether verified still
|
|
285
|
+
// holds of the new words.
|
|
286
|
+
severity: "warn",
|
|
287
|
+
code: "content.stale",
|
|
288
|
+
message: `${label} changed since ${since} — the doc moved under its vouch, and only a person can say whether 'verified' still holds. Re-read it and \`loam vouch --service ${service}\`.`,
|
|
289
|
+
},
|
|
290
|
+
];
|
|
291
|
+
}
|
|
292
|
+
/* ------------------------------------------------------------------ */
|
|
293
|
+
/* The content digest */
|
|
294
|
+
/* ------------------------------------------------------------------ */
|
|
295
|
+
/** How much of the sha256 is written into the document. */
|
|
296
|
+
const DIGEST_LENGTH = 16;
|
|
297
|
+
/**
|
|
298
|
+
* Digest the CONTENT of the files `sources` names. The recipe is spelled out
|
|
299
|
+
* because the value is written into documents, and anything reading them has to
|
|
300
|
+
* be able to reproduce it:
|
|
301
|
+
*
|
|
302
|
+
* 1. expand each entry to repo-relative file paths (`/` separators) — a file
|
|
303
|
+
* is itself, a directory everything beneath it, minus what the repository
|
|
304
|
+
* does not consider its own (see collectSources) — sorted and de-duplicated;
|
|
305
|
+
* 2. per file, `sha256(bytes)`;
|
|
306
|
+
* 3. feed `<path>\0<hex>\n` for each file, in that order, into an outer
|
|
307
|
+
* sha256;
|
|
308
|
+
* 4. keep the first 16 hex characters.
|
|
309
|
+
*
|
|
310
|
+
* Content, not mtime: git does not preserve modification times, so after a
|
|
311
|
+
* fresh clone every file looks changed and the check would be false positives
|
|
312
|
+
* end to end. Bytes survive the clone. Hashing the path alongside the content
|
|
313
|
+
* means a rename registers, which is what a reader of the doc would want.
|
|
314
|
+
*
|
|
315
|
+
* 64 bits is a change detector, not a seal — it answers "did this move?", and
|
|
316
|
+
* an adversary who wants a collision can have one.
|
|
317
|
+
*/
|
|
318
|
+
export async function sourcesDigest(repoDir, sources) {
|
|
319
|
+
const { found, skipped } = await collectSources(repoDir, sources);
|
|
320
|
+
const outer = createHash("sha256");
|
|
321
|
+
const index = [];
|
|
322
|
+
for (const file of found) {
|
|
323
|
+
const content = createHash("sha256").update(await readFile(file.abs)).digest("hex");
|
|
324
|
+
outer.update(`${file.rel}\0${content}\n`);
|
|
325
|
+
index.push({ path: file.rel, sha: content.slice(0, DIGEST_LENGTH) });
|
|
326
|
+
}
|
|
327
|
+
// `empty` is deliberately not set here: it is a sentence about a named
|
|
328
|
+
// document, and a digest has no name to put in it. Callers that need it say
|
|
329
|
+
// emptySourcesMessage(label, sources) — the same words, from one definition.
|
|
330
|
+
return { digest: outer.digest("hex").slice(0, DIGEST_LENGTH), files: index.map((e) => e.path), index, skipped };
|
|
331
|
+
}
|
|
332
|
+
/**
|
|
333
|
+
* What a `sources` list actually covers — the digest's file set without the
|
|
334
|
+
* hashing.
|
|
335
|
+
*
|
|
336
|
+
* Exported because two commands must give the SAME answer to "does this list
|
|
337
|
+
* cover anything?". `loam vouch` refuses to stamp an expansion that covers no
|
|
338
|
+
* files (a digest over nothing never changes, so the stamp would read as
|
|
339
|
+
* current forever), and until `loam validate` could say the same thing in the
|
|
340
|
+
* same words, an author got a green validate followed by a vouch that refused —
|
|
341
|
+
* two commands contradicting each other about one document, with nothing in the
|
|
342
|
+
* green run hinting at it.
|
|
343
|
+
*/
|
|
344
|
+
export async function expandSourceFiles(repoDir, sources, label) {
|
|
345
|
+
const { found, skipped } = await collectSources(repoDir, sources);
|
|
346
|
+
const files = found.map((f) => f.rel);
|
|
347
|
+
return { files, skipped, ...(files.length === 0 ? { empty: emptySourcesMessage(label, sources) } : {}) };
|
|
348
|
+
}
|
|
349
|
+
/**
|
|
350
|
+
* The one sentence for "these paths exist and cover no file". One definition,
|
|
351
|
+
* because `vouch`'s refusal and `validate`'s finding are the same diagnosis
|
|
352
|
+
* about the same document and must not drift into two descriptions of it — the
|
|
353
|
+
* author fixes it once, in the sources list.
|
|
354
|
+
*/
|
|
355
|
+
export function emptySourcesMessage(label, sources) {
|
|
356
|
+
return `${label}: the sources listed match no files — ${sources.join(", ")}. A digest over nothing would read as current forever.`;
|
|
357
|
+
}
|
|
358
|
+
/**
|
|
359
|
+
* The stamp `loam vouch` writes into `content_digest`: sha256 of the
|
|
360
|
+
* document's own BODY — every byte after the frontmatter block (below the
|
|
361
|
+
* closing `---` line and its newline; `rawBody` is the one definition of that
|
|
362
|
+
* cut) — first 16 hex characters, the sources recipe's length.
|
|
363
|
+
*
|
|
364
|
+
* Byte-exact, no normalization. Body-only is load-bearing: vouch itself
|
|
365
|
+
* rewrites the frontmatter as it stamps, and a later frontmatter-only edit
|
|
366
|
+
* (another vouch, a corrected owner) must not read as the document moving.
|
|
367
|
+
*/
|
|
368
|
+
export function contentDigest(source) {
|
|
369
|
+
return createHash("sha256").update(rawBody(source), "utf8").digest("hex").slice(0, DIGEST_LENGTH);
|
|
370
|
+
}
|
|
371
|
+
/**
|
|
372
|
+
* The files a `sources` list names: a file is itself, a directory is everything
|
|
373
|
+
* beneath it. Nothing else — glob patterns used to be matched here, by a
|
|
374
|
+
* hand-rolled dialect that silently differed from the gitignore/minimatch
|
|
375
|
+
* conventions authors assume, so a pattern quietly digested a DIFFERENT file
|
|
376
|
+
* set than its author intended. Pattern-looking entries are now refused loudly
|
|
377
|
+
* upstream (see patternSources); this function only ever sees literal paths.
|
|
378
|
+
*
|
|
379
|
+
* Three exclusions, all part of the digest recipe's contract:
|
|
380
|
+
*
|
|
381
|
+
* - dot-entries are skipped while walking — `.git` is not what the doc was
|
|
382
|
+
* written from — though a path naming one outright is still honoured;
|
|
383
|
+
* - `node_modules` is skipped the same way, unconditionally: it is the one
|
|
384
|
+
* build input every ecosystem agrees is not source, and it is the one that
|
|
385
|
+
* makes the walk take minutes;
|
|
386
|
+
* - anything the repository's own `.gitignore` covers is dropped, when the
|
|
387
|
+
* repo is a git checkout and git is on the PATH (see gitIgnoredPaths).
|
|
388
|
+
*
|
|
389
|
+
* The third is the one worth stating a reason for. `sources_digest` answers
|
|
390
|
+
* "did the code move since a person read it", and BUILD OUTPUT moves on every
|
|
391
|
+
* CI run without anybody touching the code — a `sources: [src/]` over a repo
|
|
392
|
+
* that compiles into `src/generated/` went stale on a schedule, so the warning
|
|
393
|
+
* that means "re-read this" arrived when nothing had been written. A signal
|
|
394
|
+
* that fires every night is a signal people learn to close. Git's answer is
|
|
395
|
+
* used rather than a list of our own because it is the answer the repository
|
|
396
|
+
* already gives every other tool, and it is the author's to change.
|
|
397
|
+
*
|
|
398
|
+
* The fallback is deliberate: no git, no checkout, a git that errors — hash
|
|
399
|
+
* everything. Missing an exclusion costs noise; inventing one costs a file the
|
|
400
|
+
* stamp silently stops watching, and this walk exists to have nothing in that
|
|
401
|
+
* category.
|
|
402
|
+
*/
|
|
403
|
+
async function collectSources(repoDir, sources) {
|
|
404
|
+
const found = new Map();
|
|
405
|
+
const skipped = new Map();
|
|
406
|
+
const relOf = (abs) => relative(repoDir, abs).split(sep).join("/");
|
|
407
|
+
// realpath once, up front: every containment question below is asked against
|
|
408
|
+
// the resolved root, so a repo reached through a symlink (a worktree under
|
|
409
|
+
// /var -> /private/var on macOS, say) does not make its own files look external.
|
|
410
|
+
const repoReal = await realpath(repoDir).catch(() => resolve(repoDir));
|
|
411
|
+
// The realpaths of the directories currently being walked — the cycle guard.
|
|
412
|
+
// A STACK rather than a set of everything ever entered, because the two rules
|
|
413
|
+
// answer different questions: a directory reachable by two spellings (a
|
|
414
|
+
// `current -> versions/3` link beside the real tree) is content under both
|
|
415
|
+
// names and belongs in the digest twice, while a directory reachable from
|
|
416
|
+
// INSIDE itself is a loop. A global visited-set conflates them, and then which
|
|
417
|
+
// spelling survives depends on the order readdir happened to return — a
|
|
418
|
+
// digest that differs between two machines holding identical bytes.
|
|
419
|
+
const walking = [];
|
|
420
|
+
const walk = async (dir) => {
|
|
421
|
+
const entries = await readdir(dir, { withFileTypes: true }).catch(() => []);
|
|
422
|
+
// Sorted, so a tree walks the same way on every filesystem.
|
|
423
|
+
entries.sort((a, b) => (a.name < b.name ? -1 : a.name > b.name ? 1 : 0));
|
|
424
|
+
for (const entry of entries) {
|
|
425
|
+
if (entry.name.startsWith(".") || entry.name === "node_modules")
|
|
426
|
+
continue;
|
|
427
|
+
const child = join(dir, entry.name);
|
|
428
|
+
if (entry.isDirectory())
|
|
429
|
+
await enterDir(child);
|
|
430
|
+
else if (entry.isFile())
|
|
431
|
+
found.set(relOf(child), child);
|
|
432
|
+
// readdir reports symlinks by lstat, so isDirectory() and isFile() are
|
|
433
|
+
// BOTH false for one: before this branch existed a symlinked file, and a
|
|
434
|
+
// whole subtree behind a symlinked directory, fell between the two arms
|
|
435
|
+
// and left the digest without a word said. That is the failure this
|
|
436
|
+
// module is here to prevent, arriving through its own walk.
|
|
437
|
+
else if (entry.isSymbolicLink())
|
|
438
|
+
await follow(child);
|
|
439
|
+
}
|
|
440
|
+
};
|
|
441
|
+
const enterDir = async (dir) => {
|
|
442
|
+
const real = await realpath(dir).catch(() => null);
|
|
443
|
+
if (real === null || walking.includes(real))
|
|
444
|
+
return;
|
|
445
|
+
walking.push(real);
|
|
446
|
+
try {
|
|
447
|
+
await walk(dir);
|
|
448
|
+
}
|
|
449
|
+
finally {
|
|
450
|
+
walking.pop();
|
|
451
|
+
}
|
|
452
|
+
};
|
|
453
|
+
const follow = async (link) => {
|
|
454
|
+
const real = await realpath(link).catch(() => null);
|
|
455
|
+
if (real === null) {
|
|
456
|
+
skipped.set(relOf(link), "a symlink that does not resolve");
|
|
457
|
+
return;
|
|
458
|
+
}
|
|
459
|
+
// Outside the repo the file is not this service's to vouch for, and the
|
|
460
|
+
// same rule already refuses it as a top-level `sources` entry
|
|
461
|
+
// (sources.path-outside). Reported rather than dropped: an author who
|
|
462
|
+
// vendored a sibling repo through a symlink needs to know the stamp stops
|
|
463
|
+
// at the link.
|
|
464
|
+
if (!isPathInside(repoReal, real)) {
|
|
465
|
+
skipped.set(relOf(link), "a symlink whose target is outside this repository");
|
|
466
|
+
return;
|
|
467
|
+
}
|
|
468
|
+
const info = await stat(link).catch(() => null);
|
|
469
|
+
if (info === null) {
|
|
470
|
+
skipped.set(relOf(link), "a symlink that does not resolve");
|
|
471
|
+
return;
|
|
472
|
+
}
|
|
473
|
+
// A link's own spelling is what the doc's author wrote, so that is the path
|
|
474
|
+
// that goes into the digest — the content comes from the target either way.
|
|
475
|
+
if (info.isFile())
|
|
476
|
+
found.set(relOf(link), link);
|
|
477
|
+
// A link that closes a loop is not reported: everything behind it is already
|
|
478
|
+
// in the digest under the spelling it was reached by, so nothing has stopped
|
|
479
|
+
// being watched. Only unhashed CONTENT is worth a warning.
|
|
480
|
+
else if (info.isDirectory())
|
|
481
|
+
await enterDir(link);
|
|
482
|
+
else
|
|
483
|
+
skipped.set(relOf(link), "a symlink to neither a file nor a directory");
|
|
484
|
+
};
|
|
485
|
+
for (const source of sources) {
|
|
486
|
+
const cleaned = source.trim();
|
|
487
|
+
if (cleaned.length === 0)
|
|
488
|
+
continue;
|
|
489
|
+
const root = resolveInside(repoDir, cleaned, `source '${source}'`);
|
|
490
|
+
const info = await stat(root).catch(() => null);
|
|
491
|
+
if (info === null)
|
|
492
|
+
continue;
|
|
493
|
+
if (info.isFile())
|
|
494
|
+
found.set(relOf(root), root);
|
|
495
|
+
else if (info.isDirectory())
|
|
496
|
+
await enterDir(root);
|
|
497
|
+
}
|
|
498
|
+
// One git invocation for the whole expansion, not one per file: the skipped
|
|
499
|
+
// list rides along so an ignored symlink does not warn about a path the
|
|
500
|
+
// repository already says is not its own.
|
|
501
|
+
const ignored = await gitIgnoredPaths(repoDir, [...found.keys(), ...skipped.keys()]);
|
|
502
|
+
for (const rel of ignored) {
|
|
503
|
+
found.delete(rel);
|
|
504
|
+
skipped.delete(rel);
|
|
505
|
+
}
|
|
506
|
+
return {
|
|
507
|
+
// Plain codepoint order, not locale order: the digest has to be the same
|
|
508
|
+
// everywhere it is computed.
|
|
509
|
+
found: [...found.entries()]
|
|
510
|
+
.map(([rel, abs]) => ({ rel, abs }))
|
|
511
|
+
.sort((a, b) => (a.rel < b.rel ? -1 : a.rel > b.rel ? 1 : 0)),
|
|
512
|
+
skipped: [...skipped.entries()]
|
|
513
|
+
.map(([path, reason]) => ({ path, reason }))
|
|
514
|
+
.sort((a, b) => (a.path < b.path ? -1 : a.path > b.path ? 1 : 0)),
|
|
515
|
+
};
|
|
516
|
+
}
|
|
517
|
+
/** How long the digest waits on git before deciding it learned nothing. */
|
|
518
|
+
const GIT_TIMEOUT_MS = 10_000;
|
|
519
|
+
/**
|
|
520
|
+
* Which of these repo-relative paths the repository ignores, asked once for the
|
|
521
|
+
* whole list. `git check-ignore` is the authority on purpose — it reads
|
|
522
|
+
* `.gitignore` at every level, `.git/info/exclude` and the user's global
|
|
523
|
+
* excludes, and it already knows that a TRACKED file is not ignored no matter
|
|
524
|
+
* what a pattern says, which is the distinction a re-implementation always gets
|
|
525
|
+
* wrong.
|
|
526
|
+
*
|
|
527
|
+
* Every failure answers "nothing is ignored": exit 1 is git saying so, exit 128
|
|
528
|
+
* is "not a git repository", a spawn error is git not being installed, and a
|
|
529
|
+
* timeout is a repository too strange to wait for. None of them justify
|
|
530
|
+
* dropping a file from a digest a person is going to sign.
|
|
531
|
+
*/
|
|
532
|
+
async function gitIgnoredPaths(repoDir, rels) {
|
|
533
|
+
if (rels.length === 0)
|
|
534
|
+
return new Set();
|
|
535
|
+
return new Promise((done) => {
|
|
536
|
+
const child = spawn("git", ["check-ignore", "--stdin", "-z"], {
|
|
537
|
+
cwd: repoDir,
|
|
538
|
+
stdio: ["pipe", "pipe", "ignore"],
|
|
539
|
+
timeout: GIT_TIMEOUT_MS,
|
|
540
|
+
});
|
|
541
|
+
let out = "";
|
|
542
|
+
child.stdout.setEncoding("utf8");
|
|
543
|
+
child.stdout.on("data", (chunk) => {
|
|
544
|
+
out += chunk;
|
|
545
|
+
});
|
|
546
|
+
child.on("error", () => done(new Set()));
|
|
547
|
+
child.on("close", (code) => {
|
|
548
|
+
// 0: some paths are ignored and are on stdout. 1: none are — an empty
|
|
549
|
+
// answer, not a failure. Anything else: we learned nothing.
|
|
550
|
+
done(code === 0 || code === 1 ? new Set(out.split("\0").filter((s) => s.length > 0)) : new Set());
|
|
551
|
+
});
|
|
552
|
+
// A git that died before reading its input hands us EPIPE; the close
|
|
553
|
+
// handler above is what decides the outcome either way.
|
|
554
|
+
child.stdin.on("error", () => undefined);
|
|
555
|
+
child.stdin.end(rels.join("\0") + "\0");
|
|
556
|
+
});
|
|
557
|
+
}
|
|
558
|
+
/* ------------------------------------------------------------------ */
|
|
559
|
+
/* The per-file index: what `sources.stale` names */
|
|
560
|
+
/* ------------------------------------------------------------------ */
|
|
561
|
+
/**
|
|
562
|
+
* How many files `sources_files` will list one by one before it gives up and
|
|
563
|
+
* records only how many there were.
|
|
564
|
+
*
|
|
565
|
+
* A readability budget, not a correctness one: the index lives in the header of
|
|
566
|
+
* a document a person reads, and a `sources: [src/]` over a legacy service can
|
|
567
|
+
* expand to thousands of files. Past the limit `sources.stale` falls back to
|
|
568
|
+
* repeating the `sources` entries — worse advice, but a spec.md whose
|
|
569
|
+
* frontmatter is longer than its requirements is worse still.
|
|
570
|
+
*/
|
|
571
|
+
const SOURCE_INDEX_LIMIT = 100;
|
|
572
|
+
/**
|
|
573
|
+
* The `sources_files` value `loam vouch` stamps beside `sources_digest`: one
|
|
574
|
+
* `<sha> <path>` line per file (sha256sum's layout, and its column order, so
|
|
575
|
+
* the path may contain spaces), or just the file count once the list would run
|
|
576
|
+
* past SOURCE_INDEX_LIMIT.
|
|
577
|
+
*
|
|
578
|
+
* Its whole purpose is the next `sources.stale`. `sources_digest` can say THAT
|
|
579
|
+
* the code moved and never which part of it; with the index a reader gets the
|
|
580
|
+
* added, removed and changed paths, which is the difference between re-reading
|
|
581
|
+
* one file and re-reading a directory. It is also a readable git diff in the
|
|
582
|
+
* docs repo: the vouch commit shows exactly which source files the stamp now
|
|
583
|
+
* stands over.
|
|
584
|
+
*/
|
|
585
|
+
export function encodeSourceIndex(index) {
|
|
586
|
+
if (index.length > SOURCE_INDEX_LIMIT)
|
|
587
|
+
return String(index.length);
|
|
588
|
+
return index.map((e) => `${e.sha} ${e.path}`).join("\n");
|
|
589
|
+
}
|
|
590
|
+
/**
|
|
591
|
+
* Read back what `encodeSourceIndex` wrote. Anything unrecognised reads as "the
|
|
592
|
+
* stamp said nothing" rather than as an empty index: a header nobody can parse
|
|
593
|
+
* must not turn into a `sources.stale` claiming every file was deleted.
|
|
594
|
+
*/
|
|
595
|
+
function decodeSourceIndex(stamped) {
|
|
596
|
+
if (stamped === undefined)
|
|
597
|
+
return { entries: null };
|
|
598
|
+
const text = stamped.trim();
|
|
599
|
+
if (text.length === 0)
|
|
600
|
+
return { entries: null };
|
|
601
|
+
if (/^\d+$/.test(text))
|
|
602
|
+
return { entries: null, count: Number(text) };
|
|
603
|
+
const entries = new Map();
|
|
604
|
+
for (const line of text.split("\n")) {
|
|
605
|
+
const match = /^([0-9a-f]{16})\s+(.+)$/.exec(line.trim());
|
|
606
|
+
if (match === null)
|
|
607
|
+
return { entries: null };
|
|
608
|
+
entries.set(match[2], match[1]);
|
|
609
|
+
}
|
|
610
|
+
return { entries, count: entries.size };
|
|
611
|
+
}
|
|
612
|
+
/**
|
|
613
|
+
* added / removed / changed, from the stamped index against the current one.
|
|
614
|
+
* The annotation travels IN the detail line rather than in three separate
|
|
615
|
+
* lists: a reader scanning a stale report wants the paths in one column, and
|
|
616
|
+
* "which of the three" is one word wide.
|
|
617
|
+
*/
|
|
618
|
+
function movedSources(stamp, now) {
|
|
619
|
+
if (stamp.entries === null) {
|
|
620
|
+
// No index, but the count alone still beats silence: "12 files then, 14
|
|
621
|
+
// now" tells a reader to go looking for two new files.
|
|
622
|
+
return {
|
|
623
|
+
summary: stamp.count === undefined ? "" : ` (${stamp.count} file(s) then, ${now.length} now)`,
|
|
624
|
+
};
|
|
625
|
+
}
|
|
626
|
+
const paths = [];
|
|
627
|
+
const current = new Map(now.map((e) => [e.path, e.sha]));
|
|
628
|
+
for (const [path, sha] of current) {
|
|
629
|
+
const before = stamp.entries.get(path);
|
|
630
|
+
if (before === undefined)
|
|
631
|
+
paths.push(`added ${path}`);
|
|
632
|
+
else if (before !== sha)
|
|
633
|
+
paths.push(`changed ${path}`);
|
|
634
|
+
}
|
|
635
|
+
for (const path of stamp.entries.keys())
|
|
636
|
+
if (!current.has(path))
|
|
637
|
+
paths.push(`removed ${path}`);
|
|
638
|
+
paths.sort();
|
|
639
|
+
// An index that agrees with the current tree while the digest does not is a
|
|
640
|
+
// contradiction (a truncation collision, or a hand-edited header). Say
|
|
641
|
+
// nothing rather than print an empty list under a staleness warning.
|
|
642
|
+
if (paths.length === 0)
|
|
643
|
+
return { summary: "" };
|
|
644
|
+
return { paths, summary: ` (${paths.length} path(s) moved)` };
|
|
645
|
+
}
|
|
646
|
+
/** The characters that made an entry a pattern under the glob dialect loam no longer ships. */
|
|
647
|
+
const PATTERN_CHARS = /[*?[]/;
|
|
648
|
+
/**
|
|
649
|
+
* The `sources` entries that look like glob patterns — anything containing
|
|
650
|
+
* `*`, `?` or `[`. Patterns are refused loudly everywhere sources are consumed
|
|
651
|
+
* (`loam vouch` refuses the run, `loam validate` grades an error), never
|
|
652
|
+
* resolved: the glob dialect loam used to ship silently differed from the
|
|
653
|
+
* gitignore/minimatch conventions authors assume — bracket classes literal,
|
|
654
|
+
* its own `**` handling — so a pattern quietly digested a different file set
|
|
655
|
+
* than intended, corrupting the staleness signal in both directions. A
|
|
656
|
+
* directory already means "everything beneath it", so the fix is to name one.
|
|
657
|
+
*
|
|
658
|
+
* The cost accepted with the rule: a real file with `[` in its name (a Next.js
|
|
659
|
+
* route, say) cannot be listed on its own — its parent directory covers it.
|
|
660
|
+
*/
|
|
661
|
+
export function patternSources(sources) {
|
|
662
|
+
return sources.filter((s) => PATTERN_CHARS.test(s.trim()));
|
|
663
|
+
}
|
|
664
|
+
/** The `sources` entries that resolve to nothing in this repo. */
|
|
665
|
+
export function missingSources(repoDir, sources) {
|
|
666
|
+
return sources.filter((s) => !sourceExists(repoDir, s));
|
|
667
|
+
}
|
|
668
|
+
/** Entries that are absolute, traverse upward, or escape through a symlink. */
|
|
669
|
+
export function unsafeSources(repoDir, sources) {
|
|
670
|
+
return sources.filter((source) => {
|
|
671
|
+
const cleaned = source.trim();
|
|
672
|
+
if (cleaned.length === 0)
|
|
673
|
+
return false;
|
|
674
|
+
try {
|
|
675
|
+
resolveInside(repoDir, cleaned, `source '${source}'`);
|
|
676
|
+
return false;
|
|
677
|
+
}
|
|
678
|
+
catch {
|
|
679
|
+
return true;
|
|
680
|
+
}
|
|
681
|
+
});
|
|
682
|
+
}
|
|
683
|
+
/** Does a `sources` entry point at something real? Literal paths only, checked exactly. */
|
|
684
|
+
function sourceExists(repoDir, source) {
|
|
685
|
+
const cleaned = source.trim();
|
|
686
|
+
if (cleaned.length === 0)
|
|
687
|
+
return false;
|
|
688
|
+
try {
|
|
689
|
+
return existsSync(resolveInside(repoDir, cleaned, `source '${source}'`));
|
|
690
|
+
}
|
|
691
|
+
catch {
|
|
692
|
+
return false;
|
|
693
|
+
}
|
|
694
|
+
}
|
|
695
|
+
//# sourceMappingURL=provenance.js.map
|