codebase-snapshot 0.21.0 → 0.22.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 +14 -2
- package/README.md +9 -4
- package/SECURITY.md +2 -2
- package/dist/bin.js +974 -609
- package/dist/{chunk-TDPYLX7X.js → chunk-NPCDJBE4.js} +49 -1
- package/dist/cli.cjs +1065 -703
- package/dist/cli.d.cts +20 -2
- package/dist/cli.d.ts +20 -2
- package/dist/cli.js +415 -101
- package/dist/{generate-worker-D56JeXwx.d.cts → generate-worker-BUNhTXkj.d.cts} +1 -1
- package/dist/{generate-worker-DakxZs-l.d.ts → generate-worker-TUMvHlgz.d.ts} +1 -1
- package/dist/index.cjs +49 -1
- package/dist/index.d.cts +180 -180
- package/dist/index.d.ts +180 -180
- package/dist/index.js +1 -1
- package/dist/mcp.cjs +49 -1
- package/dist/mcp.d.cts +2 -2
- package/dist/mcp.d.ts +2 -2
- package/dist/mcp.js +1 -1
- package/dist/{types-D5XYzksd.d.ts → types-DQXKiaXk.d.cts} +1 -1
- package/dist/{types-D5XYzksd.d.cts → types-DQXKiaXk.d.ts} +1 -1
- package/docs/cli.md +41 -15
- package/docs/contents.md +2 -0
- package/docs/deploy.md +2 -2
- package/docs/guard.md +154 -0
- package/docs/mcp-server-for-codebase.md +10 -2
- package/docs/mcp.md +9 -5
- package/package.json +1 -1
package/dist/cli.js
CHANGED
|
@@ -1,28 +1,230 @@
|
|
|
1
1
|
import './chunk-WMEZFLUT.js';
|
|
2
|
-
import { shouldCheck, checkForUpdate, GENERATOR_VERSION, cacheFilePath, formatUpdateNotice, installKind, buildAgentContract, startWebServer, startHttpTransport, buildTools, runStdioServer, parseRemoteSource, assertPublicHost, cloneRepository, makeCloneDirectory, loadConfigFileDetailed, resolveConfig, validateSnapshot, readConfinedText, auditSnapshot, describeManifestShapeProblem, loadManifest, buildLiveIndex, resolveImpactChanges, computeImpact, DEFAULT_MAX_PROGRAM_FILES, findMultipleSymbolReferences, planTestRun, renderImpactText, runTestPlan, briefFromPaths, resolveDiffBase, filterAndRedactDiff, matchesExclude, defaultIgnoreReason, isSensitivePath, findSection, parsePartSections, buildBrief, explainPath, compareManifests, renderDiffText, generateSnapshot, getGitInfo, loadSigningKey, SIGNATURE_FILE, formatNumber, isDiscoveryMode, ARTIFACT_NAMES, COMMAND_OPTION_SCOPE, commandsForOption, getPreset, unknownPresetMessage, GENERATOR_NAME, runInit, installPreCommitHook, PromptCancelled, SKILL_NAME, renderSkill, resolveLatest, isNewer, updateCommand, findConfigFile, planFirstRun, isEffectivelyEmpty, isInteractive, defaultIO, hasPriorSnapshot, DEFAULT_ANSWERS, firstRunMessage, escapeControls, stripJsonComments, parseByteSize, gitignorePath, appendGitignoreEntries, runWithStdoutCapture, inlineCode, findSecretInPath, MIN_MAX_PART_TOKENS, MIN_MAX_PART_BYTES, renderContractText, renderExplainText } from './chunk-
|
|
3
|
-
export { buildAgentContract } from './chunk-
|
|
2
|
+
import { shouldCheck, checkForUpdate, GENERATOR_VERSION, cacheFilePath, formatUpdateNotice, installKind, buildAgentContract, startWebServer, startHttpTransport, buildTools, runStdioServer, parseRemoteSource, assertPublicHost, cloneRepository, makeCloneDirectory, loadConfigFileDetailed, resolveConfig, validateSnapshot, readConfinedText, auditSnapshot, describeManifestShapeProblem, loadManifest, buildLiveIndex, resolveImpactChanges, computeImpact, DEFAULT_MAX_PROGRAM_FILES, findMultipleSymbolReferences, planTestRun, renderImpactText, runTestPlan, briefFromPaths, resolveDiffBase, filterAndRedactDiff, matchesExclude, defaultIgnoreReason, isSensitivePath, findSection, parsePartSections, buildBrief, explainPath, compareManifests, renderDiffText, generateSnapshot, getGitInfo, loadSigningKey, SIGNATURE_FILE, formatNumber, isDiscoveryMode, ARTIFACT_NAMES, COMMAND_OPTION_SCOPE, commandsForOption, getPreset, unknownPresetMessage, GENERATOR_NAME, runInit, installPreCommitHook, PromptCancelled, SKILL_NAME, renderSkill, resolveLatest, isNewer, updateCommand, findConfigFile, planFirstRun, isEffectivelyEmpty, isInteractive, defaultIO, hasPriorSnapshot, DEFAULT_ANSWERS, firstRunMessage, escapeControls, stripJsonComments, parseByteSize, gitignorePath, appendGitignoreEntries, runWithStdoutCapture, inlineCode, findSecretInPath, MIN_MAX_PART_TOKENS, MIN_MAX_PART_BYTES, renderContractText, renderExplainText } from './chunk-NPCDJBE4.js';
|
|
3
|
+
export { buildAgentContract } from './chunk-NPCDJBE4.js';
|
|
4
4
|
import { spawnSync } from 'child_process';
|
|
5
5
|
import * as fs2 from 'fs';
|
|
6
|
-
import * as
|
|
6
|
+
import * as path3 from 'path';
|
|
7
7
|
import { fileURLToPath } from 'url';
|
|
8
8
|
import { parseArgs } from 'util';
|
|
9
9
|
|
|
10
|
+
var MAX_CONSUMERS = 10;
|
|
11
|
+
var MAX_TESTS = 5;
|
|
12
|
+
var MAX_ROUTES = 5;
|
|
13
|
+
var MAX_HOOK_CHARS = 1200;
|
|
14
|
+
function take(values, limit) {
|
|
15
|
+
return { taken: values.slice(0, limit), omitted: Math.max(0, values.length - limit) };
|
|
16
|
+
}
|
|
17
|
+
function toGap(entry) {
|
|
18
|
+
return {
|
|
19
|
+
kind: "consumer",
|
|
20
|
+
path: entry.path,
|
|
21
|
+
distance: entry.distance,
|
|
22
|
+
via: entry.via,
|
|
23
|
+
...entry.confidence === void 0 ? {} : { confidence: entry.confidence }
|
|
24
|
+
};
|
|
25
|
+
}
|
|
26
|
+
function testGap(entry) {
|
|
27
|
+
return { kind: "test", path: entry.path, covers: [...entry.covers] };
|
|
28
|
+
}
|
|
29
|
+
function routeGap(entry) {
|
|
30
|
+
return { kind: "route", path: entry.path, methods: [...entry.methods] };
|
|
31
|
+
}
|
|
32
|
+
function buildGuardReport(impact) {
|
|
33
|
+
const changed = impact.changed.filter((entry) => entry.deleted !== true).map((entry) => entry.path);
|
|
34
|
+
const changedSet = new Set(changed);
|
|
35
|
+
const consumers = impact.affected.filter((entry) => !changedSet.has(entry.path));
|
|
36
|
+
const tests = impact.tests.filter((entry) => !changedSet.has(entry.path));
|
|
37
|
+
const routes = (impact.routesAffected ?? []).filter((entry) => !changedSet.has(entry.path));
|
|
38
|
+
const consumerPart = take(consumers, MAX_CONSUMERS);
|
|
39
|
+
const testPart = take(tests, MAX_TESTS);
|
|
40
|
+
const routePart = take(routes, MAX_ROUTES);
|
|
41
|
+
const gaps = [
|
|
42
|
+
...consumerPart.taken.map(toGap),
|
|
43
|
+
...testPart.taken.map(testGap),
|
|
44
|
+
...routePart.taken.map(routeGap)
|
|
45
|
+
];
|
|
46
|
+
return {
|
|
47
|
+
schemaVersion: 1,
|
|
48
|
+
changed,
|
|
49
|
+
gaps,
|
|
50
|
+
trust: impact.trust,
|
|
51
|
+
truncated: impact.truncated || consumerPart.omitted > 0 || testPart.omitted > 0 || routePart.omitted > 0,
|
|
52
|
+
omitted: {
|
|
53
|
+
consumers: consumerPart.omitted,
|
|
54
|
+
tests: testPart.omitted,
|
|
55
|
+
routes: routePart.omitted
|
|
56
|
+
}
|
|
57
|
+
};
|
|
58
|
+
}
|
|
59
|
+
function label(gap) {
|
|
60
|
+
if (gap.kind === "test") return "test";
|
|
61
|
+
if (gap.kind === "route") return "route";
|
|
62
|
+
return "consumer";
|
|
63
|
+
}
|
|
64
|
+
function renderGuardText(report) {
|
|
65
|
+
if (report.gaps.length === 0) {
|
|
66
|
+
return "Change Guard: no gaps in this change.\n changed: " + report.changed.length + " file(s); coverage: " + report.trust.coverage + "\n";
|
|
67
|
+
}
|
|
68
|
+
const lines = [];
|
|
69
|
+
lines.push("Change Guard: " + report.gaps.length + " gap(s) in this change");
|
|
70
|
+
for (const gap of report.gaps) {
|
|
71
|
+
const parts = [gap.path];
|
|
72
|
+
if (gap.kind === "consumer") {
|
|
73
|
+
const meta = [];
|
|
74
|
+
if (gap.distance !== void 0) meta.push(gap.distance + " hop" + (gap.distance === 1 ? "" : "s"));
|
|
75
|
+
if (gap.confidence !== void 0) meta.push(gap.confidence);
|
|
76
|
+
if (gap.via !== void 0) meta.push("via " + gap.via);
|
|
77
|
+
if (meta.length > 0) parts.push("(" + meta.join(", ") + ")");
|
|
78
|
+
} else if (gap.kind === "test") {
|
|
79
|
+
parts.push("(test" + (gap.covers && gap.covers.length > 0 ? ": " + gap.covers.join(", ") : "") + ")");
|
|
80
|
+
} else {
|
|
81
|
+
parts.push("(route" + (gap.methods && gap.methods.length > 0 ? ": " + gap.methods.join(", ") : "") + ")");
|
|
82
|
+
}
|
|
83
|
+
lines.push(" " + label(gap).padEnd(9) + parts.join(" "));
|
|
84
|
+
}
|
|
85
|
+
if (report.truncated) {
|
|
86
|
+
const omitted = report.omitted;
|
|
87
|
+
lines.push(
|
|
88
|
+
" \u2026 truncated: " + omitted.consumers + " consumer(s), " + omitted.tests + " test(s), " + omitted.routes + " route(s) omitted"
|
|
89
|
+
);
|
|
90
|
+
}
|
|
91
|
+
lines.push(" coverage: " + report.trust.coverage + " \xB7 confidence: symbol-pruned");
|
|
92
|
+
return lines.join("\n");
|
|
93
|
+
}
|
|
94
|
+
function truncateGuardText(text, maxChars) {
|
|
95
|
+
if (text.length <= maxChars) return text;
|
|
96
|
+
const marker = "\n\u2026 run codebase-snapshot guard --changed for the full list";
|
|
97
|
+
const room = Math.max(0, maxChars - marker.length);
|
|
98
|
+
return text.slice(0, room).trimEnd() + marker;
|
|
99
|
+
}
|
|
100
|
+
function renderGuardHookText(report, maxChars = MAX_HOOK_CHARS) {
|
|
101
|
+
if (report.gaps.length === 0) return "";
|
|
102
|
+
const lines = ["[change-guard] " + report.gaps.length + " gap(s) in this change:"];
|
|
103
|
+
for (const gap of report.gaps) {
|
|
104
|
+
if (gap.kind === "consumer") {
|
|
105
|
+
lines.push(
|
|
106
|
+
" " + gap.path + (gap.distance === void 0 ? "" : " (" + gap.distance + " hop" + (gap.distance === 1 ? "" : "s") + ")")
|
|
107
|
+
);
|
|
108
|
+
} else if (gap.kind === "test") {
|
|
109
|
+
lines.push(" test " + gap.path);
|
|
110
|
+
} else {
|
|
111
|
+
lines.push(" route " + gap.path + (gap.methods && gap.methods.length > 0 ? " (" + gap.methods.join(", ") + ")" : ""));
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
lines.push("coverage: " + report.trust.coverage + " \xB7 confidence: symbol-pruned");
|
|
115
|
+
lines.push("run: npx codebase-snapshot brief --diff HEAD");
|
|
116
|
+
return truncateGuardText(lines.join("\n"), maxChars);
|
|
117
|
+
}
|
|
118
|
+
function renderGuardHook(report, client, event, maxChars = MAX_HOOK_CHARS) {
|
|
119
|
+
const text = renderGuardHookText(report, maxChars);
|
|
120
|
+
if (text.length === 0) return "{}";
|
|
121
|
+
if (client === "claude") {
|
|
122
|
+
return JSON.stringify({
|
|
123
|
+
hookSpecificOutput: {
|
|
124
|
+
hookEventName: event,
|
|
125
|
+
additionalContext: text
|
|
126
|
+
}
|
|
127
|
+
});
|
|
128
|
+
}
|
|
129
|
+
return JSON.stringify({ additionalContext: text });
|
|
130
|
+
}
|
|
131
|
+
function normalizePath(cwd, raw) {
|
|
132
|
+
const value = raw.trim();
|
|
133
|
+
if (value.length === 0 || value.includes("\0")) return null;
|
|
134
|
+
const absolute = path3.isAbsolute(value) ? value : path3.resolve(cwd, value);
|
|
135
|
+
const relative4 = path3.relative(cwd, absolute).split(path3.sep).join("/");
|
|
136
|
+
if (relative4 === "" || relative4 === ".." || relative4.startsWith("../") || path3.isAbsolute(relative4)) {
|
|
137
|
+
return null;
|
|
138
|
+
}
|
|
139
|
+
return relative4;
|
|
140
|
+
}
|
|
141
|
+
function collectPaths(value, out, depth = 0) {
|
|
142
|
+
if (depth > 6 || value === null || typeof value !== "object") return;
|
|
143
|
+
if (Array.isArray(value)) {
|
|
144
|
+
for (const item of value) collectPaths(item, out, depth + 1);
|
|
145
|
+
return;
|
|
146
|
+
}
|
|
147
|
+
const record = value;
|
|
148
|
+
for (const [key, item] of Object.entries(record)) {
|
|
149
|
+
if (typeof item === "string" && /^(file_path|filePath|path|file|filename|fileName)$/i.test(key)) {
|
|
150
|
+
out.add(item);
|
|
151
|
+
continue;
|
|
152
|
+
}
|
|
153
|
+
if (Array.isArray(item) && /^(files|paths|file_paths|filePaths)$/i.test(key)) {
|
|
154
|
+
for (const entry of item) if (typeof entry === "string") out.add(entry);
|
|
155
|
+
continue;
|
|
156
|
+
}
|
|
157
|
+
if (typeof item === "object" && item !== null) collectPaths(item, out, depth + 1);
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
function parseGuardHookInput(client, raw, cwd) {
|
|
161
|
+
let payload;
|
|
162
|
+
try {
|
|
163
|
+
payload = JSON.parse(raw);
|
|
164
|
+
} catch {
|
|
165
|
+
throw new Error("guard --hook expects one JSON object on stdin.");
|
|
166
|
+
}
|
|
167
|
+
if (payload === null || typeof payload !== "object" || Array.isArray(payload)) {
|
|
168
|
+
throw new Error("guard --hook expects one JSON object on stdin.");
|
|
169
|
+
}
|
|
170
|
+
const record = payload;
|
|
171
|
+
const candidates = /* @__PURE__ */ new Set();
|
|
172
|
+
if (client === "claude") {
|
|
173
|
+
const toolInput = record.tool_input;
|
|
174
|
+
if (toolInput !== null && typeof toolInput === "object") collectPaths(toolInput, candidates);
|
|
175
|
+
const toolResponse = record.tool_response;
|
|
176
|
+
if (toolResponse !== null && typeof toolResponse === "object") collectPaths(toolResponse, candidates);
|
|
177
|
+
if (typeof record.file_path === "string") candidates.add(record.file_path);
|
|
178
|
+
} else if (client === "copilot") {
|
|
179
|
+
if (typeof record.toolArgs === "string") {
|
|
180
|
+
try {
|
|
181
|
+
collectPaths(JSON.parse(record.toolArgs), candidates);
|
|
182
|
+
} catch {
|
|
183
|
+
}
|
|
184
|
+
} else if (record.toolArgs !== null && typeof record.toolArgs === "object") {
|
|
185
|
+
collectPaths(record.toolArgs, candidates);
|
|
186
|
+
}
|
|
187
|
+
if (record.tool_input !== null && typeof record.tool_input === "object") {
|
|
188
|
+
collectPaths(record.tool_input, candidates);
|
|
189
|
+
}
|
|
190
|
+
} else {
|
|
191
|
+
if (record.tool_input !== null && typeof record.tool_input === "object") {
|
|
192
|
+
collectPaths(record.tool_input, candidates);
|
|
193
|
+
}
|
|
194
|
+
if (record.toolArgs !== null && typeof record.toolArgs === "object") {
|
|
195
|
+
collectPaths(record.toolArgs, candidates);
|
|
196
|
+
}
|
|
197
|
+
if (typeof record.path === "string") candidates.add(record.path);
|
|
198
|
+
}
|
|
199
|
+
const paths = [];
|
|
200
|
+
for (const candidate of candidates) {
|
|
201
|
+
const normalized = normalizePath(cwd, candidate);
|
|
202
|
+
if (normalized !== null) paths.push(normalized);
|
|
203
|
+
}
|
|
204
|
+
const sessionId = typeof record.session_id === "string" ? record.session_id : typeof record.sessionId === "string" ? record.sessionId : void 0;
|
|
205
|
+
const event = typeof record.hook_event_name === "string" ? record.hook_event_name : typeof record.hookEventName === "string" ? record.hookEventName : "PostToolUse";
|
|
206
|
+
return {
|
|
207
|
+
paths: [...new Set(paths)].sort((left, right) => left.localeCompare(right)),
|
|
208
|
+
...sessionId === void 0 ? {} : { sessionId },
|
|
209
|
+
event
|
|
210
|
+
};
|
|
211
|
+
}
|
|
10
212
|
var SKILL_TARGETS = ["agents", "claude", "cursor", "all"];
|
|
11
213
|
var REFUSED = "Refusing to install the agent skill";
|
|
12
214
|
var LEGACY_AGENTS_MARKER = "<!-- codebase-snapshot:skill:start -->";
|
|
13
215
|
function targetPath(cwd, target) {
|
|
14
216
|
switch (target) {
|
|
15
217
|
case "agents":
|
|
16
|
-
return
|
|
218
|
+
return path3.join(cwd, ".agents", "skills", SKILL_NAME, "SKILL.md");
|
|
17
219
|
case "claude":
|
|
18
|
-
return
|
|
220
|
+
return path3.join(cwd, ".claude", "skills", SKILL_NAME, "SKILL.md");
|
|
19
221
|
case "cursor":
|
|
20
|
-
return
|
|
222
|
+
return path3.join(cwd, ".cursor", "rules", `${SKILL_NAME}.mdc`);
|
|
21
223
|
}
|
|
22
224
|
}
|
|
23
225
|
function assertWritableInsideRepository(cwd, filePath) {
|
|
24
226
|
const root = fs2.realpathSync(cwd);
|
|
25
|
-
const shown =
|
|
227
|
+
const shown = path3.relative(root, filePath) || filePath;
|
|
26
228
|
let stat = null;
|
|
27
229
|
try {
|
|
28
230
|
stat = fs2.lstatSync(filePath);
|
|
@@ -42,14 +244,14 @@ function assertWritableInsideRepository(cwd, filePath) {
|
|
|
42
244
|
let existing = filePath;
|
|
43
245
|
const missing = [];
|
|
44
246
|
while (!fs2.existsSync(existing)) {
|
|
45
|
-
missing.unshift(
|
|
46
|
-
const parent =
|
|
247
|
+
missing.unshift(path3.basename(existing));
|
|
248
|
+
const parent = path3.dirname(existing);
|
|
47
249
|
if (parent === existing) break;
|
|
48
250
|
existing = parent;
|
|
49
251
|
}
|
|
50
252
|
const realAncestor = fs2.realpathSync(existing);
|
|
51
|
-
const resolved = missing.length === 0 ? realAncestor :
|
|
52
|
-
if (resolved !== root && !resolved.startsWith(`${root}${
|
|
253
|
+
const resolved = missing.length === 0 ? realAncestor : path3.join(realAncestor, ...missing);
|
|
254
|
+
if (resolved !== root && !resolved.startsWith(`${root}${path3.sep}`)) {
|
|
53
255
|
throw new Error(
|
|
54
256
|
`${REFUSED}: ${shown} resolves outside the repository (${resolved}). Move it back inside ${root}, or remove the link, and run again.`
|
|
55
257
|
);
|
|
@@ -58,12 +260,12 @@ function assertWritableInsideRepository(cwd, filePath) {
|
|
|
58
260
|
function writeIfChanged(target, filePath, content) {
|
|
59
261
|
const existing = fs2.existsSync(filePath) ? fs2.readFileSync(filePath, "utf8") : null;
|
|
60
262
|
if (existing === content) return { target, path: filePath, action: "unchanged" };
|
|
61
|
-
fs2.mkdirSync(
|
|
263
|
+
fs2.mkdirSync(path3.dirname(filePath), { recursive: true });
|
|
62
264
|
fs2.writeFileSync(filePath, content, "utf8");
|
|
63
265
|
return { target, path: filePath, action: existing === null ? "created" : "updated" };
|
|
64
266
|
}
|
|
65
267
|
function legacyAgentsNote(cwd) {
|
|
66
|
-
const filePath =
|
|
268
|
+
const filePath = path3.join(cwd, "AGENTS.md");
|
|
67
269
|
try {
|
|
68
270
|
if (!fs2.existsSync(filePath)) return null;
|
|
69
271
|
if (!fs2.readFileSync(filePath, "utf8").includes(LEGACY_AGENTS_MARKER)) return null;
|
|
@@ -80,7 +282,7 @@ function installSkill(cwd, target = "all", options = {}) {
|
|
|
80
282
|
for (const item of wanted) assertWritableInsideRepository(cwd, targetPath(cwd, item));
|
|
81
283
|
const createsSomething = wanted.some((item) => !fs2.existsSync(targetPath(cwd, item)));
|
|
82
284
|
if (createsSomething) {
|
|
83
|
-
assertWritableInsideRepository(cwd,
|
|
285
|
+
assertWritableInsideRepository(cwd, path3.join(cwd, ".gitignore"));
|
|
84
286
|
}
|
|
85
287
|
const pendingMcp = options.withMcp === true ? prepareMcpConfig(cwd) : null;
|
|
86
288
|
for (const item of wanted) {
|
|
@@ -102,7 +304,7 @@ function installSkill(cwd, target = "all", options = {}) {
|
|
|
102
304
|
return { cwd, written, gitignored: ignore.added, notes, ...mcp === void 0 ? {} : { mcp } };
|
|
103
305
|
}
|
|
104
306
|
function prepareMcpConfig(cwd) {
|
|
105
|
-
const filePath =
|
|
307
|
+
const filePath = path3.join(cwd, ".mcp.json");
|
|
106
308
|
assertWritableInsideRepository(cwd, filePath);
|
|
107
309
|
const desired = { command: "npx", args: ["-y", "codebase-snapshot@latest", "mcp"] };
|
|
108
310
|
let parsed = {};
|
|
@@ -157,6 +359,7 @@ var COMMAND_ALIASES = /* @__PURE__ */ new Map([
|
|
|
157
359
|
["health", "health"],
|
|
158
360
|
["report", "report"],
|
|
159
361
|
["impact", "impact"],
|
|
362
|
+
["guard", "guard"],
|
|
160
363
|
["brief", "brief"],
|
|
161
364
|
["init", "init"],
|
|
162
365
|
["setup", "setup"],
|
|
@@ -174,6 +377,7 @@ var COMMAND_ARITY = {
|
|
|
174
377
|
health: 0,
|
|
175
378
|
report: 0,
|
|
176
379
|
impact: 0,
|
|
380
|
+
guard: 0,
|
|
177
381
|
brief: 1,
|
|
178
382
|
init: 0,
|
|
179
383
|
setup: 0,
|
|
@@ -220,6 +424,9 @@ var CLI_OPTIONS = {
|
|
|
220
424
|
"changed-since": { type: "string" },
|
|
221
425
|
diff: { type: "string" },
|
|
222
426
|
paths: { type: "string" },
|
|
427
|
+
hook: { type: "string" },
|
|
428
|
+
changed: { type: "boolean" },
|
|
429
|
+
"max-chars": { type: "string" },
|
|
223
430
|
depth: { type: "string" },
|
|
224
431
|
"max-results": { type: "string" },
|
|
225
432
|
"no-cache": { type: "boolean" },
|
|
@@ -294,6 +501,7 @@ Commands:
|
|
|
294
501
|
init Generate a starter snapshot.config file
|
|
295
502
|
report Print codebase health, layer metrics, and secret audit
|
|
296
503
|
impact Report changed files, transitive importers, routes and candidate tests
|
|
504
|
+
guard Check a change for untouched consumers, tests and routes (hook-aware)
|
|
297
505
|
brief One redacted review pack: impact, diff hunks and bounded source windows
|
|
298
506
|
explain <path> Show how one path would be treated: layer, kind, part, skips
|
|
299
507
|
diff <a> <b> Compare two snapshots (directories or manifest.json files)
|
|
@@ -389,6 +597,11 @@ Options:
|
|
|
389
597
|
--tests-paths Print candidate test paths, one per line
|
|
390
598
|
--run Run the detected test runner (text format only)
|
|
391
599
|
--precise Add bounded compiler-resolved symbol references
|
|
600
|
+
|
|
601
|
+
guard only:
|
|
602
|
+
--hook <client> Read one hook payload from stdin: claude | copilot | codex | raw
|
|
603
|
+
--changed Check the working tree against HEAD (default when no --hook)
|
|
604
|
+
--max-chars <n> Cap the model-facing hook text (default 1200, max 10000)
|
|
392
605
|
--include-imports Include outgoing imports of changed files
|
|
393
606
|
--include-cycles Include import cycles in the impacted subgraph
|
|
394
607
|
--no-live Require the persisted snapshot instead of the live index
|
|
@@ -459,6 +672,9 @@ function parseCountOption(raw, optionName, minimum = 0) {
|
|
|
459
672
|
}
|
|
460
673
|
return value;
|
|
461
674
|
}
|
|
675
|
+
function isGuardClient(value) {
|
|
676
|
+
return value === "claude" || value === "copilot" || value === "codex" || value === "raw";
|
|
677
|
+
}
|
|
462
678
|
function parsePromptTask(value) {
|
|
463
679
|
if (value === void 0) return void 0;
|
|
464
680
|
if (value === "audit" || value === "review" || value === "explain") return value;
|
|
@@ -625,6 +841,26 @@ function parseCliArgs(argv) {
|
|
|
625
841
|
);
|
|
626
842
|
}
|
|
627
843
|
}
|
|
844
|
+
const rawHook = values.hook === void 0 ? void 0 : String(values.hook);
|
|
845
|
+
if (rawHook !== void 0 && !isGuardClient(rawHook)) {
|
|
846
|
+
throw new UsageError(
|
|
847
|
+
`--hook must be one of: claude, copilot, codex, raw (received "${rawHook}").`
|
|
848
|
+
);
|
|
849
|
+
}
|
|
850
|
+
if (rawHook !== void 0) {
|
|
851
|
+
if (paths !== void 0 || rawDiff !== void 0) {
|
|
852
|
+
throw new UsageError(
|
|
853
|
+
"guard --hook reads the changed paths from its stdin payload; do not pass --paths or --diff."
|
|
854
|
+
);
|
|
855
|
+
}
|
|
856
|
+
if (values.changed === true) {
|
|
857
|
+
throw new UsageError("guard --hook already runs on the payload; do not pass --changed.");
|
|
858
|
+
}
|
|
859
|
+
}
|
|
860
|
+
const maxChars = parseCountOption(values["max-chars"], "max-chars", 200);
|
|
861
|
+
if (maxChars !== void 0 && maxChars > 1e4) {
|
|
862
|
+
throw new UsageError(`--max-chars must be at most 10000, received ${maxChars}.`);
|
|
863
|
+
}
|
|
628
864
|
if (command === "impact" || command === "brief") {
|
|
629
865
|
if (rawDiff !== void 0 && paths !== void 0) {
|
|
630
866
|
throw new UsageError("impact accepts either --diff <ref> or --paths <a,b>, not both.");
|
|
@@ -743,7 +979,7 @@ function parseCliArgs(argv) {
|
|
|
743
979
|
install: values.install === true,
|
|
744
980
|
noInstall: values["no-install"] === true,
|
|
745
981
|
target: values.target === void 0 ? void 0 : String(values.target),
|
|
746
|
-
cwd: values.cwd === void 0 ? process.cwd() :
|
|
982
|
+
cwd: values.cwd === void 0 ? process.cwd() : path3.resolve(String(values.cwd)),
|
|
747
983
|
outDir: values["out-dir"] === void 0 ? void 0 : String(values["out-dir"]),
|
|
748
984
|
includeMonolith: values["include-monolith"] === true || onlyNamesMonolith === true ? true : void 0,
|
|
749
985
|
includeSensitiveFiles: values["include-sensitive-files"] === true ? true : void 0,
|
|
@@ -786,6 +1022,9 @@ function parseCliArgs(argv) {
|
|
|
786
1022
|
changedSince: values["changed-since"] === void 0 ? void 0 : String(values["changed-since"]),
|
|
787
1023
|
diff: rawDiff,
|
|
788
1024
|
paths,
|
|
1025
|
+
hook: rawHook === void 0 ? void 0 : rawHook,
|
|
1026
|
+
changed: values.changed === true,
|
|
1027
|
+
maxChars,
|
|
789
1028
|
depth,
|
|
790
1029
|
maxResults,
|
|
791
1030
|
// `undefined` when the flag was not given, never `false`.
|
|
@@ -923,20 +1162,20 @@ function renderImpactMarkdown(report) {
|
|
|
923
1162
|
}
|
|
924
1163
|
function realPathInside(realRoot, candidate) {
|
|
925
1164
|
const fold = (value) => process.platform === "win32" || process.platform === "darwin" ? value.toLowerCase() : value;
|
|
926
|
-
const
|
|
927
|
-
return
|
|
1165
|
+
const relative4 = path3.relative(fold(realRoot), fold(candidate));
|
|
1166
|
+
return relative4.length === 0 || relative4 !== ".." && !relative4.startsWith(".." + path3.sep) && !path3.isAbsolute(relative4);
|
|
928
1167
|
}
|
|
929
|
-
function writeInside(root,
|
|
930
|
-
const target =
|
|
931
|
-
const rel =
|
|
932
|
-
if (rel.length === 0 || rel === ".." || rel.startsWith(".." +
|
|
1168
|
+
function writeInside(root, relative4, content) {
|
|
1169
|
+
const target = path3.resolve(root, relative4);
|
|
1170
|
+
const rel = path3.relative(path3.resolve(root), target);
|
|
1171
|
+
if (rel.length === 0 || rel === ".." || rel.startsWith(".." + path3.sep) || path3.isAbsolute(rel)) {
|
|
933
1172
|
throw new Error("--out must resolve inside the repository.");
|
|
934
1173
|
}
|
|
935
|
-
const parent =
|
|
1174
|
+
const parent = path3.dirname(target);
|
|
936
1175
|
const realRoot = fs2.realpathSync(root);
|
|
937
1176
|
let existingAncestor = parent;
|
|
938
1177
|
while (!fs2.existsSync(existingAncestor)) {
|
|
939
|
-
const next =
|
|
1178
|
+
const next = path3.dirname(existingAncestor);
|
|
940
1179
|
if (next === existingAncestor) break;
|
|
941
1180
|
existingAncestor = next;
|
|
942
1181
|
}
|
|
@@ -955,7 +1194,7 @@ function writeInside(root, relative3, content) {
|
|
|
955
1194
|
} catch (error) {
|
|
956
1195
|
if (error.code !== "ENOENT") throw error;
|
|
957
1196
|
}
|
|
958
|
-
const temporary =
|
|
1197
|
+
const temporary = path3.join(parent, "." + path3.basename(target) + ".tmp-" + process.pid + "-" + Math.random().toString(16).slice(2));
|
|
959
1198
|
fs2.writeFileSync(temporary, content, { encoding: "utf8", flag: "wx", mode: 384 });
|
|
960
1199
|
try {
|
|
961
1200
|
fs2.renameSync(temporary, target);
|
|
@@ -984,10 +1223,10 @@ function splitList(value) {
|
|
|
984
1223
|
return value.split(",").map((entry) => entry.trim()).filter(Boolean);
|
|
985
1224
|
}
|
|
986
1225
|
function displayOutPath(cwd, outDir, ...segments) {
|
|
987
|
-
const absolute =
|
|
988
|
-
const
|
|
989
|
-
if (
|
|
990
|
-
return
|
|
1226
|
+
const absolute = path3.resolve(cwd, outDir, ...segments);
|
|
1227
|
+
const relative4 = path3.relative(cwd, absolute);
|
|
1228
|
+
if (relative4 !== "" && relative4 !== ".." && !relative4.startsWith(`..${path3.sep}`) && !relative4.startsWith("../") && !path3.isAbsolute(relative4)) {
|
|
1229
|
+
return relative4;
|
|
991
1230
|
}
|
|
992
1231
|
return absolute;
|
|
993
1232
|
}
|
|
@@ -996,7 +1235,7 @@ function nextStepHint(cwd, scriptsAddedNow) {
|
|
|
996
1235
|
if (!hasScript) {
|
|
997
1236
|
try {
|
|
998
1237
|
const parsed = JSON.parse(
|
|
999
|
-
stripJsonComments(fs2.readFileSync(
|
|
1238
|
+
stripJsonComments(fs2.readFileSync(path3.join(cwd, "package.json"), "utf8"))
|
|
1000
1239
|
);
|
|
1001
1240
|
hasScript = typeof parsed.scripts?.snapshot === "string";
|
|
1002
1241
|
} catch {
|
|
@@ -1004,7 +1243,7 @@ function nextStepHint(cwd, scriptsAddedNow) {
|
|
|
1004
1243
|
}
|
|
1005
1244
|
}
|
|
1006
1245
|
if (!hasScript) return `npx ${GENERATOR_NAME} generate`;
|
|
1007
|
-
const bin =
|
|
1246
|
+
const bin = path3.join(cwd, "node_modules", ".bin", "codebase-snapshot");
|
|
1008
1247
|
const installed = fs2.existsSync(bin) || fs2.existsSync(`${bin}.cmd`);
|
|
1009
1248
|
return installed ? "npm run snapshot" : "npm install && npm run snapshot";
|
|
1010
1249
|
}
|
|
@@ -1041,7 +1280,7 @@ async function runInitCommand(invocation) {
|
|
|
1041
1280
|
if (hook) for (const note of hook.notes) info(` ${note}`);
|
|
1042
1281
|
return 0;
|
|
1043
1282
|
}
|
|
1044
|
-
info(`\u2705 Created ${
|
|
1283
|
+
info(`\u2705 Created ${path3.basename(result.configPath)} (${presetSummary(result)})`);
|
|
1045
1284
|
if (result.scripts.length > 0) info(` Added npm scripts: ${result.scripts.join(", ")}`);
|
|
1046
1285
|
if (result.dependency.added && result.dependency.range !== null) {
|
|
1047
1286
|
info(` Added devDependency: ${result.dependency.name} ${result.dependency.range}`);
|
|
@@ -1051,10 +1290,10 @@ async function runInitCommand(invocation) {
|
|
|
1051
1290
|
}
|
|
1052
1291
|
if (result.briefWritten) {
|
|
1053
1292
|
info(
|
|
1054
|
-
` Wrote ${
|
|
1293
|
+
` Wrote ${path3.basename(result.briefPath)}: a scaffold for the half of PROMPT.md only you can write. Fill it in, then set brief: ${JSON.stringify(path3.basename(result.briefPath))} in the config to merge it.`
|
|
1055
1294
|
);
|
|
1056
1295
|
} else if (fs2.existsSync(result.briefPath)) {
|
|
1057
|
-
info(` Kept ${
|
|
1296
|
+
info(` Kept ${path3.basename(result.briefPath)}: it already exists, and is never overwritten.`);
|
|
1058
1297
|
}
|
|
1059
1298
|
for (const note of result.notes) info(` \u26A0 ${note}`);
|
|
1060
1299
|
if (hook) for (const note of hook.notes) info(` ${note}`);
|
|
@@ -1091,18 +1330,18 @@ function renderHealthText(report) {
|
|
|
1091
1330
|
function generatorOrigin(cwd) {
|
|
1092
1331
|
try {
|
|
1093
1332
|
const pkg = JSON.parse(
|
|
1094
|
-
stripJsonComments(fs2.readFileSync(
|
|
1333
|
+
stripJsonComments(fs2.readFileSync(path3.join(cwd, "package.json"), "utf8"))
|
|
1095
1334
|
);
|
|
1096
1335
|
const spec = pkg.dependencies?.["codebase-snapshot"] ?? pkg.devDependencies?.["codebase-snapshot"];
|
|
1097
1336
|
if (typeof spec === "string") return `project dependency ${spec}`;
|
|
1098
1337
|
} catch {
|
|
1099
1338
|
}
|
|
1100
1339
|
const entry = process.argv[1] ?? "";
|
|
1101
|
-
if (entry.includes("_npx") || entry.includes(`${
|
|
1340
|
+
if (entry.includes("_npx") || entry.includes(`${path3.sep}npx${path3.sep}`)) return "via npx";
|
|
1102
1341
|
return "global or direct install";
|
|
1103
1342
|
}
|
|
1104
1343
|
function detectPackageManager(cwd) {
|
|
1105
|
-
const has = (name) => fs2.existsSync(
|
|
1344
|
+
const has = (name) => fs2.existsSync(path3.join(cwd, name));
|
|
1106
1345
|
if (has("bun.lockb") || has("bun.lock")) return { name: "bun", command: "bun", args: ["install"] };
|
|
1107
1346
|
if (has("pnpm-lock.yaml")) return { name: "pnpm", command: "pnpm", args: ["install"] };
|
|
1108
1347
|
if (has("yarn.lock")) return { name: "yarn", command: "yarn", args: ["install"] };
|
|
@@ -1219,7 +1458,7 @@ async function runSetupCommand(invocation, options = {}) {
|
|
|
1219
1458
|
}
|
|
1220
1459
|
info("\n\u{1F9EA} Dry run \u2014 setup would:");
|
|
1221
1460
|
info(
|
|
1222
|
-
` 1. ${init.reused ? `keep ${
|
|
1461
|
+
` 1. ${init.reused ? `keep ${path3.basename(keptConfigPath)}` : `write ${path3.basename(keptConfigPath)} (preset: ${appliedPreset})`}`
|
|
1223
1462
|
);
|
|
1224
1463
|
info(" 2. add the npm scripts, the devDependency and the .gitignore entries");
|
|
1225
1464
|
if (manager !== null) info(` 3. install with ${manager}`);
|
|
@@ -1232,9 +1471,9 @@ async function runSetupCommand(invocation, options = {}) {
|
|
|
1232
1471
|
if (invocation.format !== "json") {
|
|
1233
1472
|
if (!interactive) info(`\u{1F680} Setup for: ${invocation.cwd}`);
|
|
1234
1473
|
if (init.reused) {
|
|
1235
|
-
info(`\u267B Keeping ${
|
|
1474
|
+
info(`\u267B Keeping ${path3.basename(keptConfigPath)}`);
|
|
1236
1475
|
} else {
|
|
1237
|
-
info(`\u2705 Created ${
|
|
1476
|
+
info(`\u2705 Created ${path3.basename(keptConfigPath)} (${presetSummary(init)})`);
|
|
1238
1477
|
}
|
|
1239
1478
|
if (init.scripts.length > 0) info(` Added npm scripts: ${init.scripts.join(", ")}`);
|
|
1240
1479
|
if (init.dependency.added && init.dependency.range !== null) {
|
|
@@ -1245,7 +1484,7 @@ async function runSetupCommand(invocation, options = {}) {
|
|
|
1245
1484
|
}
|
|
1246
1485
|
if (init.briefWritten) {
|
|
1247
1486
|
info(
|
|
1248
|
-
` Wrote ${
|
|
1487
|
+
` Wrote ${path3.basename(init.briefPath)}: a scaffold for the half of PROMPT.md only you can write. Fill it in, then set brief: ${JSON.stringify(path3.basename(init.briefPath))} in the config to merge it.`
|
|
1249
1488
|
);
|
|
1250
1489
|
}
|
|
1251
1490
|
if (hook) for (const note of hook.notes) info(` ${note}`);
|
|
@@ -1253,7 +1492,7 @@ async function runSetupCommand(invocation, options = {}) {
|
|
|
1253
1492
|
info(` \u26A0 --preset ${invocation.preset} was not applied: the existing config is kept.`);
|
|
1254
1493
|
}
|
|
1255
1494
|
}
|
|
1256
|
-
const hasManifest = fs2.existsSync(
|
|
1495
|
+
const hasManifest = fs2.existsSync(path3.join(invocation.cwd, "package.json"));
|
|
1257
1496
|
let install = null;
|
|
1258
1497
|
if (invocation.noInstall) {
|
|
1259
1498
|
if (invocation.format !== "json") info(" Skipped the install (--no-install)");
|
|
@@ -1418,7 +1657,7 @@ function runSkillCommand(invocation) {
|
|
|
1418
1657
|
}
|
|
1419
1658
|
info(`\u2705 Agent skill installed (${SKILL_NAME}):`);
|
|
1420
1659
|
for (const item of result.written) {
|
|
1421
|
-
const shown =
|
|
1660
|
+
const shown = path3.relative(invocation.cwd, item.path) || item.path;
|
|
1422
1661
|
info(` - ${item.action.padEnd(9)} ${shown}`);
|
|
1423
1662
|
}
|
|
1424
1663
|
if (result.gitignored.length > 0) {
|
|
@@ -1427,7 +1666,7 @@ function runSkillCommand(invocation) {
|
|
|
1427
1666
|
info(" These files are this tool's own documentation, so they stay out of git.");
|
|
1428
1667
|
}
|
|
1429
1668
|
if (result.mcp !== void 0) {
|
|
1430
|
-
const shown =
|
|
1669
|
+
const shown = path3.relative(invocation.cwd, result.mcp.path) || result.mcp.path;
|
|
1431
1670
|
info(` - ${result.mcp.action.padEnd(9)} ${shown} (codebase-snapshot MCP entry)`);
|
|
1432
1671
|
}
|
|
1433
1672
|
for (const note of result.notes) info(` \u26A0 ${note}`);
|
|
@@ -1498,6 +1737,78 @@ function guardClosedStdout() {
|
|
|
1498
1737
|
throw error;
|
|
1499
1738
|
});
|
|
1500
1739
|
}
|
|
1740
|
+
async function readStdin(limit = 1024 * 1024) {
|
|
1741
|
+
const chunks = [];
|
|
1742
|
+
let size = 0;
|
|
1743
|
+
for await (const chunk of process.stdin) {
|
|
1744
|
+
const buffer = Buffer.isBuffer(chunk) ? chunk : Buffer.from(String(chunk));
|
|
1745
|
+
size += buffer.length;
|
|
1746
|
+
if (size > limit) throw new Error("the hook payload exceeds 1 MiB");
|
|
1747
|
+
chunks.push(buffer);
|
|
1748
|
+
}
|
|
1749
|
+
return Buffer.concat(chunks).toString("utf8");
|
|
1750
|
+
}
|
|
1751
|
+
async function runGuardCommand(invocation, resolved) {
|
|
1752
|
+
const client = invocation.hook;
|
|
1753
|
+
try {
|
|
1754
|
+
let paths = invocation.paths;
|
|
1755
|
+
let event = "PostToolUse";
|
|
1756
|
+
if (client !== void 0) {
|
|
1757
|
+
const parsed = parseGuardHookInput(client, await readStdin(), resolved.cwd);
|
|
1758
|
+
paths = parsed.paths;
|
|
1759
|
+
event = parsed.event;
|
|
1760
|
+
if (paths.length === 0) {
|
|
1761
|
+
process.stdout.write("{}\n");
|
|
1762
|
+
return 0;
|
|
1763
|
+
}
|
|
1764
|
+
}
|
|
1765
|
+
const changes = resolveImpactChanges(resolved.cwd, {
|
|
1766
|
+
...paths === void 0 ? { diff: invocation.diff ?? "HEAD" } : { paths }
|
|
1767
|
+
});
|
|
1768
|
+
const manifestPath = path3.resolve(resolved.cwd, resolved.outDir, "manifest.json");
|
|
1769
|
+
let manifest;
|
|
1770
|
+
let trust;
|
|
1771
|
+
if (invocation.noLive) {
|
|
1772
|
+
if (!fs2.existsSync(manifestPath)) {
|
|
1773
|
+
if (client !== void 0) {
|
|
1774
|
+
warn("guard: no snapshot manifest; the hook was skipped.");
|
|
1775
|
+
process.stdout.write("{}\n");
|
|
1776
|
+
return 0;
|
|
1777
|
+
}
|
|
1778
|
+
return fail(
|
|
1779
|
+
invocation.format,
|
|
1780
|
+
"Manifest not found at " + manifestPath + ". Run generate first, or omit --no-live."
|
|
1781
|
+
);
|
|
1782
|
+
}
|
|
1783
|
+
manifest = loadManifest(manifestPath);
|
|
1784
|
+
} else {
|
|
1785
|
+
const live = await buildLiveIndex(resolved, { useDiskCache: !invocation.noCache });
|
|
1786
|
+
manifest = live.manifest;
|
|
1787
|
+
trust = live.trust;
|
|
1788
|
+
}
|
|
1789
|
+
const impact = computeImpact({
|
|
1790
|
+
manifest,
|
|
1791
|
+
cwd: resolved.cwd,
|
|
1792
|
+
changes,
|
|
1793
|
+
manifestPath,
|
|
1794
|
+
...trust === void 0 ? {} : { trust }
|
|
1795
|
+
});
|
|
1796
|
+
const report = buildGuardReport(impact);
|
|
1797
|
+
if (client !== void 0) {
|
|
1798
|
+
process.stdout.write(renderGuardHook(report, client, event, invocation.maxChars ?? 1200) + "\n");
|
|
1799
|
+
return 0;
|
|
1800
|
+
}
|
|
1801
|
+
emit(invocation.format, report, () => info(renderGuardText(report)));
|
|
1802
|
+
return 0;
|
|
1803
|
+
} catch (err) {
|
|
1804
|
+
if (client !== void 0) {
|
|
1805
|
+
warn("guard: " + err.message + " (hook skipped)");
|
|
1806
|
+
process.stdout.write("{}\n");
|
|
1807
|
+
return 0;
|
|
1808
|
+
}
|
|
1809
|
+
return fail(invocation.format, err.message);
|
|
1810
|
+
}
|
|
1811
|
+
}
|
|
1501
1812
|
async function main(argv = process.argv.slice(2)) {
|
|
1502
1813
|
guardClosedStdout();
|
|
1503
1814
|
const pending = process.stdout.isTTY === true && shouldCheck(argv, process.env) ? checkForUpdate({ current: GENERATOR_VERSION, cacheFile: cacheFilePath() }) : null;
|
|
@@ -1585,7 +1896,7 @@ ${USAGE}
|
|
|
1585
1896
|
}
|
|
1586
1897
|
if (invocation.command === "serve") {
|
|
1587
1898
|
const server = await startWebServer({
|
|
1588
|
-
cwd:
|
|
1899
|
+
cwd: path3.resolve(invocation.cwd),
|
|
1589
1900
|
...invocation.host === void 0 ? {} : { host: invocation.host },
|
|
1590
1901
|
...invocation.port === void 0 ? {} : { port: invocation.port },
|
|
1591
1902
|
...tokenOption,
|
|
@@ -1600,9 +1911,9 @@ ${USAGE}
|
|
|
1600
1911
|
} else {
|
|
1601
1912
|
warn(" WARNING: --no-auth is on. Any page on this machine can read the repository.");
|
|
1602
1913
|
}
|
|
1603
|
-
await new Promise((
|
|
1914
|
+
await new Promise((resolve3) => {
|
|
1604
1915
|
const stop = () => {
|
|
1605
|
-
void server.close().then(
|
|
1916
|
+
void server.close().then(resolve3);
|
|
1606
1917
|
};
|
|
1607
1918
|
process.once("SIGINT", stop);
|
|
1608
1919
|
process.once("SIGTERM", stop);
|
|
@@ -1612,7 +1923,7 @@ ${USAGE}
|
|
|
1612
1923
|
if (invocation.command === "mcp") {
|
|
1613
1924
|
if (invocation.http === true) {
|
|
1614
1925
|
const transport = await startHttpTransport({
|
|
1615
|
-
cwd:
|
|
1926
|
+
cwd: path3.resolve(invocation.cwd),
|
|
1616
1927
|
noConfig: invocation.noConfig,
|
|
1617
1928
|
...invocation.host === void 0 ? {} : { host: invocation.host },
|
|
1618
1929
|
...invocation.port === void 0 ? {} : { port: invocation.port },
|
|
@@ -1628,9 +1939,9 @@ ${USAGE}
|
|
|
1628
1939
|
} else {
|
|
1629
1940
|
warn(" WARNING: --no-auth is on. Any page on this machine can read the repository.");
|
|
1630
1941
|
}
|
|
1631
|
-
await new Promise((
|
|
1942
|
+
await new Promise((resolve3) => {
|
|
1632
1943
|
const stop = () => {
|
|
1633
|
-
void transport.close().then(
|
|
1944
|
+
void transport.close().then(resolve3);
|
|
1634
1945
|
};
|
|
1635
1946
|
process.once("SIGINT", stop);
|
|
1636
1947
|
process.once("SIGTERM", stop);
|
|
@@ -1638,7 +1949,7 @@ ${USAGE}
|
|
|
1638
1949
|
return 0;
|
|
1639
1950
|
}
|
|
1640
1951
|
return await runStdioServer({
|
|
1641
|
-
cwd:
|
|
1952
|
+
cwd: path3.resolve(invocation.cwd),
|
|
1642
1953
|
noConfig: invocation.noConfig
|
|
1643
1954
|
});
|
|
1644
1955
|
}
|
|
@@ -1668,7 +1979,7 @@ ${USAGE}
|
|
|
1668
1979
|
log: (line) => info(" " + line)
|
|
1669
1980
|
});
|
|
1670
1981
|
sourceProvenance = clone.provenance;
|
|
1671
|
-
const outDir =
|
|
1982
|
+
const outDir = path3.resolve(invocation.cwd, invocation.outDir ?? "snapshot");
|
|
1672
1983
|
invocation = { ...invocation, cwd: clone.directory, outDir, noConfig: true };
|
|
1673
1984
|
info(
|
|
1674
1985
|
" Source: " + parsed.source.owner + "/" + parsed.source.repo + "@" + clone.commit.slice(0, 12) + " (fetched " + clone.provenance.fetchedAt + ")"
|
|
@@ -1698,7 +2009,7 @@ ${USAGE}
|
|
|
1698
2009
|
);
|
|
1699
2010
|
}
|
|
1700
2011
|
for (const warning of loaded.warnings) warn(`\u26A0 Config: ${warning}`);
|
|
1701
|
-
if (loaded.file) info(`\u2699 Using config ${
|
|
2012
|
+
if (loaded.file) info(`\u2699 Using config ${path3.relative(process.cwd(), loaded.file) || loaded.file}`);
|
|
1702
2013
|
let resolved;
|
|
1703
2014
|
try {
|
|
1704
2015
|
resolved = resolveConfig(
|
|
@@ -1709,6 +2020,9 @@ ${USAGE}
|
|
|
1709
2020
|
} catch (err) {
|
|
1710
2021
|
return fail(invocation.format, err.message);
|
|
1711
2022
|
}
|
|
2023
|
+
if (invocation.command === "guard") {
|
|
2024
|
+
return await runGuardCommand(invocation, resolved);
|
|
2025
|
+
}
|
|
1712
2026
|
if (invocation.command === "validate") {
|
|
1713
2027
|
const result = validateSnapshot(resolved);
|
|
1714
2028
|
emit(invocation.format, result, () => renderValidateText(result));
|
|
@@ -1716,8 +2030,8 @@ ${USAGE}
|
|
|
1716
2030
|
}
|
|
1717
2031
|
if (invocation.command === "health") {
|
|
1718
2032
|
if (invocation.allMembers === true) {
|
|
1719
|
-
const outRoot =
|
|
1720
|
-
const indexPath =
|
|
2033
|
+
const outRoot = path3.resolve(resolved.cwd, resolved.outDir);
|
|
2034
|
+
const indexPath = path3.join(outRoot, "WORKSPACE.json");
|
|
1721
2035
|
if (!fs2.existsSync(indexPath)) {
|
|
1722
2036
|
return fail(
|
|
1723
2037
|
invocation.format,
|
|
@@ -1736,15 +2050,15 @@ ${USAGE}
|
|
|
1736
2050
|
}
|
|
1737
2051
|
const rows = [];
|
|
1738
2052
|
const insidePath = (parent, child) => {
|
|
1739
|
-
const
|
|
1740
|
-
return
|
|
2053
|
+
const relative4 = path3.relative(parent, child);
|
|
2054
|
+
return relative4 === "" || relative4 !== ".." && !relative4.startsWith(`..${path3.sep}`) && !relative4.startsWith("../") && !path3.isAbsolute(relative4);
|
|
1741
2055
|
};
|
|
1742
2056
|
for (const member of index.members ?? []) {
|
|
1743
2057
|
const name = member.name ?? "?";
|
|
1744
|
-
const snapshotDir2 =
|
|
2058
|
+
const snapshotDir2 = path3.resolve(outRoot, name);
|
|
1745
2059
|
const repoPath = member.path ?? name;
|
|
1746
|
-
const repoDir =
|
|
1747
|
-
if (name.length === 0 ||
|
|
2060
|
+
const repoDir = path3.resolve(resolved.cwd, repoPath);
|
|
2061
|
+
if (name.length === 0 || path3.isAbsolute(name) || name.split(/[\\/]/).includes("..") || !insidePath(outRoot, snapshotDir2) || path3.isAbsolute(repoPath) || repoPath.split(/[\\/]/).includes("..") || !insidePath(resolved.cwd, repoDir)) {
|
|
1748
2062
|
rows.push({
|
|
1749
2063
|
name,
|
|
1750
2064
|
score: 0,
|
|
@@ -1775,7 +2089,7 @@ ${USAGE}
|
|
|
1775
2089
|
canonicalSnapshot = snapshotDir2;
|
|
1776
2090
|
canonicalRepo = repoDir;
|
|
1777
2091
|
}
|
|
1778
|
-
if (!fs2.existsSync(
|
|
2092
|
+
if (!fs2.existsSync(path3.join(snapshotDir2, "manifest.json"))) {
|
|
1779
2093
|
rows.push({ name, score: 0, counts: { pass: 0, warn: 0, fail: 1 }, failed: [], error: "no snapshot at " + snapshotDir2 });
|
|
1780
2094
|
continue;
|
|
1781
2095
|
}
|
|
@@ -1822,7 +2136,7 @@ ${USAGE}
|
|
|
1822
2136
|
if (invocation.strict === true && totals.warn > 0) return 1;
|
|
1823
2137
|
return 0;
|
|
1824
2138
|
}
|
|
1825
|
-
const snapshotDir =
|
|
2139
|
+
const snapshotDir = path3.resolve(resolved.cwd, resolved.outDir);
|
|
1826
2140
|
const report = auditSnapshot({
|
|
1827
2141
|
snapshotDir,
|
|
1828
2142
|
repoDir: resolved.cwd,
|
|
@@ -1835,7 +2149,7 @@ ${USAGE}
|
|
|
1835
2149
|
return 0;
|
|
1836
2150
|
}
|
|
1837
2151
|
if (invocation.command === "report") {
|
|
1838
|
-
const manifestPath =
|
|
2152
|
+
const manifestPath = path3.resolve(resolved.cwd, resolved.outDir, "manifest.json");
|
|
1839
2153
|
if (!fs2.existsSync(manifestPath)) {
|
|
1840
2154
|
return fail(invocation.format, `Manifest not found at ${manifestPath}. Run generate first.`);
|
|
1841
2155
|
}
|
|
@@ -1898,7 +2212,7 @@ ${USAGE}
|
|
|
1898
2212
|
if (manifest.delta !== void 0) {
|
|
1899
2213
|
const delta = manifest.delta;
|
|
1900
2214
|
info("\n Selection (delta):");
|
|
1901
|
-
info(` - Against: ${
|
|
2215
|
+
info(` - Against: ${path3.basename(delta.base.path)} (${delta.base.fingerprint.slice(0, 12)})`);
|
|
1902
2216
|
info(
|
|
1903
2217
|
` - Changed: ${delta.changed.length} changed, ${delta.added.length} added, ${delta.removed.length} removed, ${delta.unchanged.length} unchanged`
|
|
1904
2218
|
);
|
|
@@ -1908,7 +2222,7 @@ ${USAGE}
|
|
|
1908
2222
|
}
|
|
1909
2223
|
if (invocation.command === "impact") {
|
|
1910
2224
|
try {
|
|
1911
|
-
const manifestPath =
|
|
2225
|
+
const manifestPath = path3.resolve(resolved.cwd, resolved.outDir, "manifest.json");
|
|
1912
2226
|
let manifest;
|
|
1913
2227
|
let live = null;
|
|
1914
2228
|
if (invocation.noLive) {
|
|
@@ -2030,7 +2344,7 @@ ${USAGE}
|
|
|
2030
2344
|
}
|
|
2031
2345
|
if (invocation.command === "brief") {
|
|
2032
2346
|
try {
|
|
2033
|
-
const manifestPath =
|
|
2347
|
+
const manifestPath = path3.resolve(resolved.cwd, resolved.outDir, "manifest.json");
|
|
2034
2348
|
let manifest;
|
|
2035
2349
|
let live = null;
|
|
2036
2350
|
if (invocation.noLive) {
|
|
@@ -2086,10 +2400,10 @@ ${USAGE}
|
|
|
2086
2400
|
if (isSensitivePath(relativePath)) return null;
|
|
2087
2401
|
const file = manifest.files.find((candidate) => candidate.relativePath === relativePath);
|
|
2088
2402
|
if (file === void 0) return null;
|
|
2089
|
-
const snapshotDir =
|
|
2403
|
+
const snapshotDir = path3.resolve(resolved.cwd, resolved.outDir);
|
|
2090
2404
|
const read = readConfinedText(
|
|
2091
2405
|
snapshotDir,
|
|
2092
|
-
|
|
2406
|
+
path3.posix.join("parts", file.part),
|
|
2093
2407
|
16 * 1024 * 1024
|
|
2094
2408
|
);
|
|
2095
2409
|
if (!read.ok) return null;
|
|
@@ -2114,7 +2428,7 @@ ${USAGE}
|
|
|
2114
2428
|
});
|
|
2115
2429
|
if (invocation.out !== void 0) {
|
|
2116
2430
|
const written = writeInside(resolved.cwd, invocation.out, brief.markdown);
|
|
2117
|
-
info("Wrote " +
|
|
2431
|
+
info("Wrote " + path3.relative(resolved.cwd, written));
|
|
2118
2432
|
} else if (invocation.format !== "json") {
|
|
2119
2433
|
process.stdout.write(brief.markdown);
|
|
2120
2434
|
}
|
|
@@ -2190,10 +2504,10 @@ ${USAGE}
|
|
|
2190
2504
|
"--workspace writes one snapshot per member; --remote, --delta, --slice, --budget-tokens and --sign do not combine with it."
|
|
2191
2505
|
);
|
|
2192
2506
|
}
|
|
2193
|
-
const outRoot =
|
|
2194
|
-
const workspaceCwd =
|
|
2195
|
-
const outRootRelative =
|
|
2196
|
-
if (outRootRelative === "" || outRootRelative !== ".." && !outRootRelative.startsWith(`..${
|
|
2507
|
+
const outRoot = path3.resolve(invocation.cwd, invocation.outDir ?? "snapshot");
|
|
2508
|
+
const workspaceCwd = path3.resolve(invocation.cwd);
|
|
2509
|
+
const outRootRelative = path3.relative(outRoot, workspaceCwd);
|
|
2510
|
+
if (outRootRelative === "" || outRootRelative !== ".." && !outRootRelative.startsWith(`..${path3.sep}`) && !outRootRelative.startsWith("../") && !path3.isAbsolute(outRootRelative)) {
|
|
2197
2511
|
return fail(
|
|
2198
2512
|
invocation.format,
|
|
2199
2513
|
`--workspace refuses --out-dir ${outRoot}: it is the working directory or one of its ancestors.`
|
|
@@ -2201,8 +2515,8 @@ ${USAGE}
|
|
|
2201
2515
|
}
|
|
2202
2516
|
const plannedNames = /* @__PURE__ */ new Map();
|
|
2203
2517
|
for (const member of members) {
|
|
2204
|
-
const directory =
|
|
2205
|
-
const name =
|
|
2518
|
+
const directory = path3.resolve(invocation.cwd, member);
|
|
2519
|
+
const name = path3.basename(directory);
|
|
2206
2520
|
const previous = plannedNames.get(name);
|
|
2207
2521
|
if (previous !== void 0) {
|
|
2208
2522
|
return fail(
|
|
@@ -2215,8 +2529,8 @@ ${USAGE}
|
|
|
2215
2529
|
const records = [];
|
|
2216
2530
|
let failed = 0;
|
|
2217
2531
|
for (const member of members) {
|
|
2218
|
-
const directory =
|
|
2219
|
-
const name =
|
|
2532
|
+
const directory = path3.resolve(invocation.cwd, member);
|
|
2533
|
+
const name = path3.basename(directory);
|
|
2220
2534
|
let stat;
|
|
2221
2535
|
try {
|
|
2222
2536
|
stat = fs2.statSync(directory);
|
|
@@ -2236,7 +2550,7 @@ ${USAGE}
|
|
|
2236
2550
|
{
|
|
2237
2551
|
...generateCliOptions(invocation),
|
|
2238
2552
|
cwd: directory,
|
|
2239
|
-
outDir:
|
|
2553
|
+
outDir: path3.join(outRoot, name)
|
|
2240
2554
|
},
|
|
2241
2555
|
directory
|
|
2242
2556
|
);
|
|
@@ -2263,10 +2577,10 @@ ${USAGE}
|
|
|
2263
2577
|
for (const entry of record.imports) {
|
|
2264
2578
|
for (const specifier of entry.imports) {
|
|
2265
2579
|
if (!specifier.startsWith(".")) continue;
|
|
2266
|
-
const resolved2 =
|
|
2580
|
+
const resolved2 = path3.resolve(record.path, path3.dirname(entry.path), specifier);
|
|
2267
2581
|
for (const other of records) {
|
|
2268
2582
|
if (other === record) continue;
|
|
2269
|
-
const prefix = other.path +
|
|
2583
|
+
const prefix = other.path + path3.sep;
|
|
2270
2584
|
if (!resolved2.startsWith(prefix) && resolved2 !== other.path) continue;
|
|
2271
2585
|
const bucket = counts.get(other.name) ?? { count: 0, examples: [] };
|
|
2272
2586
|
bucket.count += 1;
|
|
@@ -2291,12 +2605,12 @@ ${USAGE}
|
|
|
2291
2605
|
// Store the member path relative to the workspace cwd, as manifest paths already are.
|
|
2292
2606
|
members: records.map(({ imports: _imports, ...rest }) => ({
|
|
2293
2607
|
...rest,
|
|
2294
|
-
path:
|
|
2608
|
+
path: path3.relative(workspaceCwd, rest.path).split(path3.sep).join("/") || "."
|
|
2295
2609
|
})),
|
|
2296
2610
|
links
|
|
2297
2611
|
};
|
|
2298
2612
|
if (!invocation.dryRun) {
|
|
2299
|
-
const workspacePath =
|
|
2613
|
+
const workspacePath = path3.join(outRoot, "WORKSPACE.json");
|
|
2300
2614
|
try {
|
|
2301
2615
|
const stats = fs2.lstatSync(workspacePath);
|
|
2302
2616
|
if (stats.isSymbolicLink() || !stats.isFile()) {
|
|
@@ -2339,8 +2653,8 @@ ${USAGE}
|
|
|
2339
2653
|
try {
|
|
2340
2654
|
let deltaBase;
|
|
2341
2655
|
if (invocation.delta !== void 0) {
|
|
2342
|
-
const basePath =
|
|
2343
|
-
const manifestPath = fs2.existsSync(basePath) && fs2.statSync(basePath).isDirectory() ?
|
|
2656
|
+
const basePath = path3.resolve(invocation.cwd, invocation.delta);
|
|
2657
|
+
const manifestPath = fs2.existsSync(basePath) && fs2.statSync(basePath).isDirectory() ? path3.join(basePath, "manifest.json") : basePath;
|
|
2344
2658
|
if (!fs2.existsSync(manifestPath)) {
|
|
2345
2659
|
return fail(invocation.format, "No manifest at " + manifestPath + ". Point --delta at a snapshot directory or its manifest.json.");
|
|
2346
2660
|
}
|
|
@@ -2354,8 +2668,8 @@ ${USAGE}
|
|
|
2354
2668
|
if (shape !== null) {
|
|
2355
2669
|
return fail(invocation.format, manifestPath + " is not a snapshot manifest: " + shape);
|
|
2356
2670
|
}
|
|
2357
|
-
const relativeBase =
|
|
2358
|
-
const recordedBase = relativeBase === "" || relativeBase === ".." || relativeBase.startsWith(`..${
|
|
2671
|
+
const relativeBase = path3.relative(invocation.cwd, manifestPath).split(path3.sep).join("/");
|
|
2672
|
+
const recordedBase = relativeBase === "" || relativeBase === ".." || relativeBase.startsWith(`..${path3.sep}`) || relativeBase.startsWith("../") || path3.isAbsolute(relativeBase) ? path3.basename(manifestPath) : relativeBase;
|
|
2359
2673
|
deltaBase = { manifest: base, path: recordedBase };
|
|
2360
2674
|
info(" Delta base: " + recordedBase + " (" + base.files.length + " files, fingerprint " + base.fingerprint.slice(0, 12) + ")");
|
|
2361
2675
|
}
|
|
@@ -2373,7 +2687,7 @@ ${USAGE}
|
|
|
2373
2687
|
...invocation.sign === true && !invocation.dryRun ? { sign: { key: loadSigningKey() } } : {}
|
|
2374
2688
|
});
|
|
2375
2689
|
if (invocation.sign === true && !invocation.dryRun) {
|
|
2376
|
-
const signaturePath =
|
|
2690
|
+
const signaturePath = path3.resolve(resolved.cwd, resolved.outDir, SIGNATURE_FILE);
|
|
2377
2691
|
try {
|
|
2378
2692
|
const record = JSON.parse(fs2.readFileSync(signaturePath, "utf8"));
|
|
2379
2693
|
info(
|
|
@@ -2528,19 +2842,19 @@ function finishWatchRun(state, now) {
|
|
|
2528
2842
|
};
|
|
2529
2843
|
}
|
|
2530
2844
|
function comparablePath(value) {
|
|
2531
|
-
const absolute =
|
|
2845
|
+
const absolute = path3.resolve(value);
|
|
2532
2846
|
return process.platform === "win32" ? absolute.toLowerCase() : absolute;
|
|
2533
2847
|
}
|
|
2534
2848
|
function isSameOrInside(parent, candidate) {
|
|
2535
|
-
const
|
|
2536
|
-
return
|
|
2849
|
+
const relative4 = path3.relative(parent, candidate);
|
|
2850
|
+
return relative4 === "" || relative4 !== ".." && !relative4.startsWith(`..${path3.sep}`) && !path3.isAbsolute(relative4);
|
|
2537
2851
|
}
|
|
2538
2852
|
function isGeneratedWatchPath(candidate, paths) {
|
|
2539
|
-
const absolute = comparablePath(
|
|
2540
|
-
const outDir = comparablePath(
|
|
2541
|
-
const tmpDir = comparablePath(
|
|
2853
|
+
const absolute = comparablePath(path3.resolve(paths.cwd, candidate));
|
|
2854
|
+
const outDir = comparablePath(path3.resolve(paths.cwd, paths.outDir));
|
|
2855
|
+
const tmpDir = comparablePath(path3.resolve(paths.cwd, paths.tmpDir));
|
|
2542
2856
|
if (isSameOrInside(outDir, absolute) || isSameOrInside(tmpDir, absolute)) return true;
|
|
2543
|
-
const offsets = [
|
|
2857
|
+
const offsets = [path3.basename(outDir), path3.basename(tmpDir)].filter((name) => name.length > 0);
|
|
2544
2858
|
const leftover = absolute.split(/[\\/]+/).some(
|
|
2545
2859
|
(segment) => offsets.some(
|
|
2546
2860
|
(name) => segment.startsWith(`${name}.backup-`) || segment.startsWith(`${name}.partial-`)
|
|
@@ -2563,8 +2877,8 @@ function decideWatchEvent(state, event, paths) {
|
|
|
2563
2877
|
function watchPathsFor(config) {
|
|
2564
2878
|
return {
|
|
2565
2879
|
cwd: config.cwd,
|
|
2566
|
-
outDir:
|
|
2567
|
-
tmpDir:
|
|
2880
|
+
outDir: path3.resolve(config.cwd, config.outDir),
|
|
2881
|
+
tmpDir: path3.resolve(config.cwd, config.tmpDir)
|
|
2568
2882
|
};
|
|
2569
2883
|
}
|
|
2570
2884
|
function watchSupportProblem(platform = process.platform, version = process.versions.node) {
|
|
@@ -2642,7 +2956,7 @@ async function runWatchLoop(invocation, resolved, options) {
|
|
|
2642
2956
|
const decision = decideWatchEvent(
|
|
2643
2957
|
state,
|
|
2644
2958
|
{
|
|
2645
|
-
path: filename ?
|
|
2959
|
+
path: filename ? path3.resolve(resolved.cwd, filename.toString()) : null,
|
|
2646
2960
|
now: performance.now()
|
|
2647
2961
|
},
|
|
2648
2962
|
paths
|
|
@@ -2668,12 +2982,12 @@ async function runWatchLoop(invocation, resolved, options) {
|
|
|
2668
2982
|
info(`\u26A0 Watch error: ${err.message}`);
|
|
2669
2983
|
info(" The watcher may have missed changes. Restart with Ctrl+C if they stop arriving.");
|
|
2670
2984
|
});
|
|
2671
|
-
await new Promise((
|
|
2985
|
+
await new Promise((resolve3) => {
|
|
2672
2986
|
const stop = () => {
|
|
2673
2987
|
stopped = true;
|
|
2674
2988
|
if (timer) clearTimeout(timer);
|
|
2675
2989
|
watcher.close();
|
|
2676
|
-
|
|
2990
|
+
resolve3();
|
|
2677
2991
|
};
|
|
2678
2992
|
process.once("SIGINT", stop);
|
|
2679
2993
|
process.once("SIGTERM", stop);
|
|
@@ -2683,12 +2997,12 @@ async function runWatchLoop(invocation, resolved, options) {
|
|
|
2683
2997
|
var entryFile = process.argv[1] ?? "";
|
|
2684
2998
|
var sameFile = (left, right) => {
|
|
2685
2999
|
try {
|
|
2686
|
-
return fs2.realpathSync(
|
|
3000
|
+
return fs2.realpathSync(path3.resolve(left)) === fs2.realpathSync(path3.resolve(right));
|
|
2687
3001
|
} catch {
|
|
2688
3002
|
return false;
|
|
2689
3003
|
}
|
|
2690
3004
|
};
|
|
2691
|
-
var entryName =
|
|
3005
|
+
var entryName = path3.basename(entryFile).toLowerCase();
|
|
2692
3006
|
var invokedDirectly = (entryName === "cli.js" || entryName === "cli.cjs") && entryFile.length > 0 && sameFile(entryFile, fileURLToPath(import.meta.url));
|
|
2693
3007
|
if (invokedDirectly) {
|
|
2694
3008
|
void main().then(
|