@deepwissen/preflight 0.0.0-stage → 0.4.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/README.md CHANGED
@@ -1,3 +1,104 @@
1
- # Temporary Holding Version
1
+ # preflight
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ **Get your AI agent flight-ready — see what skills it's carrying, what they're cleared to do, and what they cost, before it flies.**
4
+
5
+ `preflight` is a local CLI for managing the skills your AI coding agent carries
6
+ (`.claude/skills`, slash commands, subagents, plugin-bundled skills). It's the
7
+ companion to [`vet`](https://github.com/deepwissen/vet) (which scans your *code*) —
8
+ `preflight` manages your *agent*. Same engine
9
+ ([`@deepwissen/preflight-core`](https://github.com/deepwissen/preflight-core)).
10
+
11
+ ## Install
12
+
13
+ ```bash
14
+ npx @deepwissen/preflight manifest # run without installing
15
+ npm i -g @deepwissen/preflight # or install the `preflight` command
16
+ ```
17
+
18
+ ## `preflight manifest`
19
+
20
+ Inventory every skill your agent carries, with its **declared scope**, **static
21
+ token footprint**, and a **scan** of each:
22
+
23
+ ```bash
24
+ preflight manifest # scan the current project
25
+ preflight manifest ./path # scan a specific directory
26
+ preflight manifest --json # machine-readable
27
+ preflight manifest --sarif # SARIF 2.1.0 → GitHub code scanning
28
+ preflight manifest --fail-on warning # exit 1 on warning or worse
29
+ ```
30
+
31
+ Exit code is `0` clean, `1` if any finding meets the `--fail-on` threshold
32
+ (`error` by default; `warning` or `any` to be stricter) — so it doubles as a CI gate.
33
+
34
+ ### Collisions & ambiguity
35
+
36
+ `manifest` also reports **conflicts** between skills:
37
+
38
+ - **Name collisions** — two skills with the same name. Personal (`~/.claude`) silently
39
+ shadows project (`./.claude`) with no warning from Claude Code, so `manifest --all`
40
+ spells out which one actually runs and which is dead.
41
+ - **Ambiguous descriptions** — differently-named skills whose descriptions overlap, so
42
+ the agent's (model-driven) choice between them is unpredictable.
43
+
44
+ ## Gate skills in CI
45
+
46
+ Your CI already scans dependencies and secrets. Gate the agent skills your team
47
+ commits the same way — fail the PR if a committed skill is over-broad, injected,
48
+ or ships a secret in a bundled script:
49
+
50
+ ```yaml
51
+ # .github/workflows/skills.yml
52
+ - run: npx @deepwissen/preflight manifest --sarif > skills.sarif
53
+ - uses: github/codeql-action/upload-sarif@v3
54
+ with: { sarif_file: skills.sarif }
55
+ # or hard-fail the PR:
56
+ - run: npx @deepwissen/preflight manifest --fail-on warning
57
+ ```
58
+
59
+ ## Manage skills
60
+
61
+ ```bash
62
+ preflight list # compact list of installed skills
63
+ preflight add owner/repo # clone + scan, then CHOOSE which to install
64
+ preflight add owner/repo --pick 1,4,6 # install those by number (1-3 / all also work)
65
+ preflight add owner/repo --only tdd,pr # install these by name
66
+ preflight add ./some-skill # a single local skill
67
+ preflight add ./x --dry-run # scan and report, install nothing
68
+ preflight add ./x --force # install despite findings
69
+ preflight remove <name> # preview what would be removed
70
+ preflight remove <name> --yes # move it to .preflight-trash/ (reversible)
71
+ ```
72
+
73
+ ### Project vs personal skills
74
+
75
+ Skills live at two levels, and both load into your agent. By default preflight acts
76
+ on **project** skills (`./.claude/skills`); `--global` acts on **personal** skills
77
+ (`~/.claude/skills`); `--all` lists both together with a WHERE column.
78
+
79
+ ```bash
80
+ preflight list # project skills
81
+ preflight list --global # personal skills (~/.claude/skills)
82
+ preflight list --all # both, with a project/user column
83
+ preflight add ./x --global # install into personal skills
84
+ preflight remove foo --global # remove a personal skill
85
+ ```
86
+
87
+ `add` is a **security gate and a chooser, not a bulk copy**: a repo of skills is
88
+ staged and scanned, then shown as a numbered list (with each skill's risk and
89
+ per-prompt token cost) so you install only the few you want — not all 39. It prompts
90
+ interactively in a terminal, or takes `--pick <numbers>` / `--only <names>`. Chosen
91
+ skills are installed only if clean (gate defaults to `--fail-on warning`). `remove` never hard-deletes — it moves
92
+ the skill to `.preflight-trash/`, which discovery ignores.
93
+
94
+ ## Honest limits
95
+
96
+ - **Footprint is static** — a skill's description loads into context on every prompt,
97
+ its body on demand. The numbers are an estimate of that static cost, not live usage.
98
+ - **Scope is declared-only** — it reflects a skill's `allowed-tools`; prose in the body
99
+ can instruct the agent to do more, which a static read can't capture.
100
+ - **A scan never certifies a skill "safe"** — it surfaces known-bad patterns (prompt
101
+ injection, hidden unicode, role-tag spoofing, embedded secrets, over-broad scope).
102
+ A clean result means nothing was *found*, not that nothing is *there*.
103
+
104
+ All analysis is **local** — nothing is modified or sent anywhere.
package/dist/bin.js ADDED
@@ -0,0 +1,4 @@
1
+ #!/usr/bin/env node
2
+ import { run } from "./cli.js";
3
+ run(process.argv.slice(2)).then((code) => process.exit(code));
4
+ //# sourceMappingURL=bin.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"bin.js","sourceRoot":"","sources":["../src/bin.ts"],"names":[],"mappings":";AACA,OAAO,EAAE,GAAG,EAAE,MAAM,UAAU,CAAC;AAC/B,GAAG,CAAC,OAAO,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,OAAO,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC,CAAC"}
package/dist/bundle.js ADDED
@@ -0,0 +1,97 @@
1
+ import { readdirSync, statSync, readFileSync } from "node:fs";
2
+ import { join, dirname, relative, extname } from "node:path";
3
+ import { scanCode, scanMcpConfig, looksMcpConfig } from "./scan.js";
4
+ // Executable/script file types a skill may ship alongside its SKILL.md. The agent
5
+ // can be told to run these, so they're code the skill carries — worth scanning.
6
+ const SCRIPT_LANG = {
7
+ ".sh": "shellscript",
8
+ ".bash": "shellscript",
9
+ ".zsh": "shellscript",
10
+ ".py": "python",
11
+ ".rb": "ruby",
12
+ ".pl": "perl",
13
+ ".js": "javascript",
14
+ ".cjs": "javascript",
15
+ ".mjs": "javascript",
16
+ ".ts": "typescript",
17
+ ".ps1": "powershell",
18
+ };
19
+ const MAX_BYTES = 256 * 1024; // don't read very large files into memory
20
+ const MAX_FILES = 200; // bound the walk
21
+ const EMPTY = { scripts: [], otherFiles: 0 };
22
+ /**
23
+ * Enumerate the files a skill actually ships — ground truth, vs. the content-only
24
+ * `referencedScripts` heuristic (which only parses what the SKILL.md *mentions*).
25
+ * Only applies to SKILL.md-based skills (which own a directory); single-file
26
+ * commands/agents have no bundle. Each script found is scanned for secrets/traps.
27
+ */
28
+ export function enumerateBundle(skillAbsPath) {
29
+ if (!/(^|\/)skill\.md$/i.test(skillAbsPath))
30
+ return EMPTY;
31
+ const root = dirname(skillAbsPath);
32
+ const scripts = [];
33
+ let otherFiles = 0;
34
+ let count = 0;
35
+ const walk = (dir) => {
36
+ let entries;
37
+ try {
38
+ entries = readdirSync(dir);
39
+ }
40
+ catch {
41
+ return;
42
+ }
43
+ for (const name of entries) {
44
+ if (count >= MAX_FILES)
45
+ return;
46
+ const abs = join(dir, name);
47
+ let s;
48
+ try {
49
+ s = statSync(abs);
50
+ }
51
+ catch {
52
+ continue;
53
+ }
54
+ if (s.isDirectory()) {
55
+ if (name !== "node_modules" && name !== ".git")
56
+ walk(abs);
57
+ continue;
58
+ }
59
+ if (abs === skillAbsPath)
60
+ continue; // the SKILL.md prose is scanned elsewhere
61
+ count++;
62
+ const ext = extname(name).toLowerCase();
63
+ const lang = SCRIPT_LANG[ext];
64
+ const rel = relative(root, abs);
65
+ // 1. A bundled script — scan as code (secrets + traps).
66
+ if (lang) {
67
+ scripts.push({
68
+ path: rel,
69
+ language: lang,
70
+ bytes: s.size,
71
+ findings: s.size > MAX_BYTES ? [] : scanCode(rel, readFileSync(abs, "utf8"), lang),
72
+ });
73
+ continue;
74
+ }
75
+ // 2. A bundled MCP config (.json wiring the agent to a server) — scan for
76
+ // MCP risk: unpinned servers, embedded secrets, injected tool descriptions.
77
+ if (ext === ".json" && s.size <= MAX_BYTES) {
78
+ const content = readFileSync(abs, "utf8");
79
+ if (looksMcpConfig(rel, content)) {
80
+ scripts.push({
81
+ path: rel,
82
+ language: "mcp-config",
83
+ bytes: s.size,
84
+ findings: scanMcpConfig(rel, content),
85
+ });
86
+ continue;
87
+ }
88
+ }
89
+ // 3. Everything else — ships with the skill but isn't code we scan.
90
+ otherFiles++;
91
+ }
92
+ };
93
+ walk(root);
94
+ scripts.sort((a, b) => a.path.localeCompare(b.path));
95
+ return { scripts, otherFiles };
96
+ }
97
+ //# sourceMappingURL=bundle.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"bundle.js","sourceRoot":"","sources":["../src/bundle.ts"],"names":[],"mappings":"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"}
package/dist/cli.js ADDED
@@ -0,0 +1,454 @@
1
+ import { parseArgs } from "node:util";
2
+ import { resolve, dirname, join } from "node:path";
3
+ import { fileURLToPath } from "node:url";
4
+ import { readFileSync, existsSync } from "node:fs";
5
+ import { createInterface } from "node:readline";
6
+ import { buildScopes, buildRow, exitForReport, } from "./manifest.js";
7
+ import { projectScope, userScope, scopeSkillNames, shadowNotices, } from "./scopes.js";
8
+ import { skillName } from "./collisions.js";
9
+ import { resolveSkill } from "./discover.js";
10
+ import { renderManifest, renderJson, renderCheck, renderCheckJson, renderList, renderPickList, renderPlugins, } from "./render.js";
11
+ import { renderManifestSarif } from "./sarif.js";
12
+ import { discoverPlugins } from "./plugins.js";
13
+ import { stageSource, planInstall, installItem, removeSkill, parseSelection, selectByNames, } from "./install.js";
14
+ const FAIL_ON = ["error", "warning", "any"];
15
+ /** Shadowing + hidden-scope hints for a single-scope view (not --all). */
16
+ function crossScopeNotices(flags, projectRoot, report, command) {
17
+ if (flags.all)
18
+ return [];
19
+ const shown = report.rows.map((r) => skillName(r).toLowerCase());
20
+ const other = scopeSkillNames(flags.global ? projectScope(projectRoot) : userScope());
21
+ return shadowNotices({ shown, other, global: Boolean(flags.global), command });
22
+ }
23
+ /** Scopes to act on, honoring --global / --all and an optional explicit project path. */
24
+ function scopesFromFlags(flags, projectRoot) {
25
+ if (flags.all)
26
+ return [projectScope(projectRoot), userScope()];
27
+ if (flags.global)
28
+ return [userScope()];
29
+ return [projectScope(projectRoot)];
30
+ }
31
+ const VERSION = "0.4.0"; // keep in sync with package.json
32
+ /** Read one line from stdin (interactive skill selection). */
33
+ function promptLine(question) {
34
+ return new Promise((res) => {
35
+ const rl = createInterface({ input: process.stdin, output: process.stderr });
36
+ rl.question(question, (answer) => {
37
+ rl.close();
38
+ res(answer);
39
+ });
40
+ });
41
+ }
42
+ /** Resolve the @deepwissen/preflight-core version actually in use, for --version. */
43
+ function engineVersion() {
44
+ try {
45
+ let dir = dirname(fileURLToPath(import.meta.url));
46
+ for (let i = 0; i < 8; i++) {
47
+ const pkg = join(dir, "node_modules", "@deepwissen", "preflight-core", "package.json");
48
+ if (existsSync(pkg)) {
49
+ const v = JSON.parse(readFileSync(pkg, "utf8")).version;
50
+ return typeof v === "string" ? v : null;
51
+ }
52
+ const parent = dirname(dir);
53
+ if (parent === dir)
54
+ break;
55
+ dir = parent;
56
+ }
57
+ return null;
58
+ }
59
+ catch {
60
+ return null;
61
+ }
62
+ }
63
+ const HELP = `preflight — see and scan the skills your AI agent carries, before you trust them.
64
+
65
+ A "skill" is a file your Claude Code agent loads and obeys (in .claude/skills/). You
66
+ install them from the internet — so preflight lets you SEE what you have, SCAN each
67
+ for risks (secrets, prompt-injection, over-broad access), and CHOOSE what to keep.
68
+ Everything runs locally — nothing is sent anywhere, and a clean scan never means "safe".
69
+
70
+ New here? Try:
71
+ preflight list What skills do I have?
72
+ preflight check <name> Is this one safe? (shows a HEALTH verdict)
73
+ preflight add owner/repo Scan a repo's skills, then pick which to install
74
+
75
+ Examples:
76
+ preflight list --all project AND personal (~/.claude) skills
77
+ preflight add anthropics/skills --pick 1,3 clone, scan, install just #1 and #3
78
+ preflight manifest --fail-on warning fail CI if a committed skill is risky
79
+ preflight plugins audit your installed plugins too
80
+
81
+ Commands:
82
+ list [path] Quick list of your skills (name · scope · check).
83
+ manifest [path] Full inventory — scope, token cost, and a scan of each.
84
+ check <skill> Drill into one skill: scope, cost, and every finding + its fix.
85
+ add <source> Scan skills from a path or owner/repo, then choose what to install.
86
+ remove <name> Move an installed skill to .preflight-trash/ (needs --yes).
87
+ plugins Audit installed plugins (~/.claude/plugins): where each came from + a scan.
88
+
89
+ Scope (list / manifest / check / add / remove):
90
+ (default) this project's skills — ./.claude/skills
91
+ --global your personal skills — ~/.claude/skills
92
+ --all both at once (list / manifest), with a WHERE column
93
+
94
+ Options:
95
+ --json Machine-readable output
96
+ --sarif SARIF 2.1.0 → GitHub code scanning annotations
97
+ --fail-on <level> Exit non-zero on: error (default) | warning | any
98
+ --only <names> (add) install only these skills, comma-separated
99
+ --pick <nums> (add) install these by number: 1,4,6 / 1-3 / all
100
+ --force (add) install despite findings
101
+ --overwrite (add) replace a skill that's already installed
102
+ --dry-run (add) scan and report, install nothing
103
+ --yes (remove) actually move the skill to trash
104
+ -v, --version Print version
105
+ -h, --help Print this help
106
+
107
+ A scan finds known-bad patterns but never certifies a skill "safe" — a clean result
108
+ means nothing was *found*, not that nothing is there.
109
+ Docs: https://github.com/deepwissen/preflight`;
110
+ function runManifest(argv, out, err) {
111
+ let values;
112
+ let positionals;
113
+ try {
114
+ const parsed = parseArgs({
115
+ args: argv,
116
+ allowPositionals: true,
117
+ options: {
118
+ json: { type: "boolean" },
119
+ sarif: { type: "boolean" },
120
+ global: { type: "boolean" },
121
+ all: { type: "boolean" },
122
+ "fail-on": { type: "string", default: "error" },
123
+ },
124
+ });
125
+ values = parsed.values;
126
+ positionals = parsed.positionals;
127
+ }
128
+ catch (e) {
129
+ err(`preflight: ${e.message}\n`);
130
+ return 2;
131
+ }
132
+ const failOn = String(values["fail-on"]);
133
+ if (!FAIL_ON.includes(failOn)) {
134
+ err(`preflight: --fail-on must be one of ${FAIL_ON.join(", ")}\n`);
135
+ return 2;
136
+ }
137
+ const projectRoot = resolve(positionals[0] ?? ".");
138
+ if (!values.global && !existsSync(projectRoot)) {
139
+ err(`preflight: no such path: ${projectRoot}\n`);
140
+ return 2;
141
+ }
142
+ const report = buildScopes(scopesFromFlags(values, projectRoot));
143
+ if (values.sarif) {
144
+ out(renderManifestSarif(report) + "\n");
145
+ }
146
+ else if (values.json) {
147
+ out(renderJson(report) + "\n");
148
+ }
149
+ else {
150
+ out(renderManifest(report) + "\n");
151
+ const notices = crossScopeNotices(values, projectRoot, report, "manifest");
152
+ if (notices.length)
153
+ out(notices.join("\n") + "\n");
154
+ }
155
+ return exitForReport(report, failOn);
156
+ }
157
+ function runCheck(argv, out, err) {
158
+ let values;
159
+ let positionals;
160
+ try {
161
+ const parsed = parseArgs({
162
+ args: argv,
163
+ allowPositionals: true,
164
+ options: {
165
+ json: { type: "boolean" },
166
+ sarif: { type: "boolean" },
167
+ global: { type: "boolean" },
168
+ all: { type: "boolean" },
169
+ "fail-on": { type: "string", default: "error" },
170
+ },
171
+ });
172
+ values = parsed.values;
173
+ positionals = parsed.positionals;
174
+ }
175
+ catch (e) {
176
+ err(`preflight: ${e.message}\n`);
177
+ return 2;
178
+ }
179
+ const failOn = String(values["fail-on"]);
180
+ if (!FAIL_ON.includes(failOn)) {
181
+ err(`preflight: --fail-on must be one of ${FAIL_ON.join(", ")}\n`);
182
+ return 2;
183
+ }
184
+ const arg = positionals[0];
185
+ if (!arg) {
186
+ err("preflight check: name a skill — preflight check <name|path>\n");
187
+ return 2;
188
+ }
189
+ let resolved = null;
190
+ let lastErr = null;
191
+ for (const s of scopesFromFlags(values, resolve("."))) {
192
+ const r = resolveSkill(s.scanRoot, arg, s.skip);
193
+ if ("error" in r)
194
+ lastErr = r;
195
+ else {
196
+ resolved = r;
197
+ break;
198
+ }
199
+ }
200
+ if (!resolved) {
201
+ err(`preflight: ${lastErr?.error ?? `no skill matching "${arg}"`}\n`);
202
+ for (const c of lastErr?.candidates ?? [])
203
+ err(` - ${c}\n`);
204
+ return 2;
205
+ }
206
+ const row = buildRow(resolved.absPath, resolved.displayPath);
207
+ const report = {
208
+ root: ".",
209
+ rows: [row],
210
+ contextFiles: [],
211
+ alwaysTotal: row.alwaysTokens,
212
+ onDemandTotal: row.onDemandTokens,
213
+ settingsRisks: [],
214
+ };
215
+ out((values.sarif
216
+ ? renderManifestSarif(report)
217
+ : values.json
218
+ ? renderCheckJson(row)
219
+ : renderCheck(row)) + "\n");
220
+ return exitForReport(report, failOn);
221
+ }
222
+ function runList(argv, out, err) {
223
+ let values;
224
+ let positionals;
225
+ try {
226
+ const parsed = parseArgs({
227
+ args: argv,
228
+ allowPositionals: true,
229
+ options: { global: { type: "boolean" }, all: { type: "boolean" } },
230
+ });
231
+ values = parsed.values;
232
+ positionals = parsed.positionals;
233
+ }
234
+ catch (e) {
235
+ err(`preflight: ${e.message}\n`);
236
+ return 2;
237
+ }
238
+ const projectRoot = resolve(positionals[0] ?? ".");
239
+ if (!values.global && !existsSync(projectRoot)) {
240
+ err(`preflight: no such path: ${projectRoot}\n`);
241
+ return 2;
242
+ }
243
+ const report = buildScopes(scopesFromFlags(values, projectRoot));
244
+ out(renderList(report) + "\n");
245
+ const notices = crossScopeNotices(values, projectRoot, report, "list");
246
+ if (notices.length)
247
+ out(notices.join("\n") + "\n");
248
+ return 0;
249
+ }
250
+ async function runAdd(argv, out, err) {
251
+ let values;
252
+ let positionals;
253
+ try {
254
+ const parsed = parseArgs({
255
+ args: argv,
256
+ allowPositionals: true,
257
+ options: {
258
+ force: { type: "boolean" },
259
+ overwrite: { type: "boolean" },
260
+ "dry-run": { type: "boolean" },
261
+ global: { type: "boolean" },
262
+ only: { type: "string" },
263
+ pick: { type: "string" },
264
+ "fail-on": { type: "string", default: "warning" },
265
+ },
266
+ });
267
+ values = parsed.values;
268
+ positionals = parsed.positionals;
269
+ }
270
+ catch (e) {
271
+ err(`preflight: ${e.message}\n`);
272
+ return 2;
273
+ }
274
+ const failOn = String(values["fail-on"]);
275
+ if (!FAIL_ON.includes(failOn)) {
276
+ err(`preflight: --fail-on must be one of ${FAIL_ON.join(", ")}\n`);
277
+ return 2;
278
+ }
279
+ if (values.only !== undefined && values.pick !== undefined) {
280
+ err("preflight add: use --only or --pick, not both\n");
281
+ return 2;
282
+ }
283
+ const src = positionals[0];
284
+ if (!src) {
285
+ err("preflight add: name a source — preflight add <path|owner/repo>\n");
286
+ return 2;
287
+ }
288
+ let staging;
289
+ try {
290
+ staging = stageSource(src);
291
+ }
292
+ catch (e) {
293
+ err(`preflight: could not stage "${src}": ${e.message.split("\n")[0]}\n`);
294
+ return 2;
295
+ }
296
+ const target = values.global ? userScope() : projectScope();
297
+ try {
298
+ const items = planInstall(staging.dir, target.skillsDir);
299
+ if (items.length === 0) {
300
+ err(`preflight: no skills (SKILL.md) found in "${src}"\n`);
301
+ return 2;
302
+ }
303
+ // --- choose which skills to install (not the whole repo) ---
304
+ let selected;
305
+ if (values.only !== undefined) {
306
+ const names = String(values.only).split(",").map((s) => s.trim()).filter(Boolean);
307
+ const r = selectByNames(items, names);
308
+ if (r.notFound.length) {
309
+ err(`preflight: not found in "${src}": ${r.notFound.join(", ")}\n`);
310
+ out(renderPickList(items, src) + "\n");
311
+ return 2;
312
+ }
313
+ selected = r.selected;
314
+ }
315
+ else if (values.pick !== undefined) {
316
+ const r = parseSelection(String(values.pick), items.length);
317
+ if ("error" in r) {
318
+ err(`preflight: ${r.error}\n`);
319
+ return 2;
320
+ }
321
+ selected = r.indices.map((i) => items[i]);
322
+ }
323
+ else if (items.length === 1) {
324
+ selected = items;
325
+ }
326
+ else {
327
+ out(renderPickList(items, src) + "\n");
328
+ if (process.stdin.isTTY) {
329
+ const answer = await promptLine("\nInstall which? (e.g. 1,4,6 or 1-3 · all · none): ");
330
+ const r = parseSelection(answer, items.length);
331
+ if ("error" in r) {
332
+ err(`preflight: ${r.error}\n`);
333
+ return 2;
334
+ }
335
+ selected = r.indices.map((i) => items[i]);
336
+ }
337
+ else {
338
+ out("\nChoose skills to install: --pick 1,4,6 (or --pick all) · --only <names>\n");
339
+ return 0;
340
+ }
341
+ }
342
+ if (selected.length === 0) {
343
+ out("nothing selected — nothing installed.\n");
344
+ return 0;
345
+ }
346
+ // --- gate the chosen skills, then install ---
347
+ const report = {
348
+ root: src,
349
+ rows: selected.map((s) => s.row),
350
+ contextFiles: [],
351
+ alwaysTotal: selected.reduce((n, s) => n + s.row.alwaysTokens, 0),
352
+ onDemandTotal: selected.reduce((n, s) => n + s.row.onDemandTokens, 0),
353
+ settingsRisks: [],
354
+ };
355
+ if (exitForReport(report, failOn) === 1 && !values.force) {
356
+ const bad = selected.filter((s) => s.row.errorCount || s.row.warnCount).map((s) => s.name);
357
+ err(`\npreflight: blocked — ${bad.join(", ")} ha${bad.length > 1 ? "ve" : "s"} findings at/above "${failOn}". Re-run with --force to install anyway.\n`);
358
+ return 1;
359
+ }
360
+ if (values["dry-run"]) {
361
+ out(`\ndry run — would install ${selected.length}: ${selected.map((s) => s.name).join(", ")}\n`);
362
+ return 0;
363
+ }
364
+ const clash = selected.filter((s) => s.exists);
365
+ if (clash.length && !values.overwrite) {
366
+ err(`preflight: already installed: ${clash.map((s) => s.name).join(", ")} (use --overwrite)\n`);
367
+ return 1;
368
+ }
369
+ for (const item of selected)
370
+ installItem(item, { overwrite: Boolean(values.overwrite) });
371
+ out(`\ninstalled ${selected.length} skill(s) into ${target.name} scope (${target.skillsDir}): ${selected
372
+ .map((s) => s.name)
373
+ .join(", ")}\n`);
374
+ return 0;
375
+ }
376
+ finally {
377
+ staging.cleanup();
378
+ }
379
+ }
380
+ function runRemove(argv, out, err) {
381
+ let values;
382
+ let positionals;
383
+ try {
384
+ const parsed = parseArgs({
385
+ args: argv,
386
+ allowPositionals: true,
387
+ options: { yes: { type: "boolean" }, global: { type: "boolean" } },
388
+ });
389
+ values = parsed.values;
390
+ positionals = parsed.positionals;
391
+ }
392
+ catch (e) {
393
+ err(`preflight: ${e.message}\n`);
394
+ return 2;
395
+ }
396
+ const name = positionals[0];
397
+ if (!name) {
398
+ err("preflight remove: name a skill — preflight remove <name>\n");
399
+ return 2;
400
+ }
401
+ const target = values.global ? userScope() : projectScope();
402
+ const res = removeSkill(target.scanRoot, name, { yes: Boolean(values.yes), skip: target.skip });
403
+ if ("error" in res) {
404
+ err(`preflight: ${res.error}\n`);
405
+ return 2;
406
+ }
407
+ if ("wouldRemove" in res) {
408
+ out(`would remove ${res.wouldRemove} (${res.files} file(s)). Re-run with --yes to move it to .preflight-trash/.\n`);
409
+ return 2;
410
+ }
411
+ out(`removed ${res.removed} → .preflight-trash/\n`);
412
+ return 0;
413
+ }
414
+ function runPlugins(argv, out, err) {
415
+ let values;
416
+ try {
417
+ values = parseArgs({ args: argv, allowPositionals: false, options: { json: { type: "boolean" } } }).values;
418
+ }
419
+ catch (e) {
420
+ err(`preflight: ${e.message}\n`);
421
+ return 2;
422
+ }
423
+ const plugins = discoverPlugins();
424
+ out((values.json ? JSON.stringify({ plugins }, null, 2) : renderPlugins(plugins)) + "\n");
425
+ // Advisory audit: exit 1 if any plugin has an error-level finding.
426
+ return plugins.some((p) => p.errorCount > 0) ? 1 : 0;
427
+ }
428
+ export async function run(argv, out = (s) => void process.stdout.write(s), err = (s) => void process.stderr.write(s)) {
429
+ const sub = argv[0];
430
+ if (!sub || sub === "-h" || sub === "--help") {
431
+ out(HELP + "\n");
432
+ return 0;
433
+ }
434
+ if (sub === "-v" || sub === "--version") {
435
+ const engine = engineVersion();
436
+ out(VERSION + (engine ? ` (engine @deepwissen/preflight-core ${engine})` : "") + "\n");
437
+ return 0;
438
+ }
439
+ if (sub === "list")
440
+ return runList(argv.slice(1), out, err);
441
+ if (sub === "manifest")
442
+ return runManifest(argv.slice(1), out, err);
443
+ if (sub === "check")
444
+ return runCheck(argv.slice(1), out, err);
445
+ if (sub === "add")
446
+ return runAdd(argv.slice(1), out, err);
447
+ if (sub === "remove")
448
+ return runRemove(argv.slice(1), out, err);
449
+ if (sub === "plugins")
450
+ return runPlugins(argv.slice(1), out, err);
451
+ err(`preflight: unknown command "${sub}" (try: preflight --help)\n`);
452
+ return 2;
453
+ }
454
+ //# sourceMappingURL=cli.js.map