codebase-snapshot 0.0.0-stage → 4.5.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/docs/cli.md ADDED
@@ -0,0 +1,437 @@
1
+ # CLI reference
2
+
3
+ ```bash
4
+ npx codebase-snapshot <command> [options]
5
+ ```
6
+
7
+ The package installs one executable, **`codebase-snapshot`**, with a short alias **`snap`**.
8
+ Both are the same file. After `npx codebase-snapshot generate` the command
9
+ becomes available as `npx codebase-snapshot` for the rest of that session; after
10
+ `npm install -D` use `npx codebase-snapshot` or `npx snap` from the project.
11
+
12
+ Running with no command is the same as `generate`.
13
+
14
+ ## Contents
15
+
16
+ - [Commands](#commands)
17
+ - [`generate`](#generate)
18
+ - [`validate` / `check`](#validate--check)
19
+ - [`report`](#report)
20
+ - [`init`](#init)
21
+ - [`explain <path>`](#explain-path)
22
+ - [`diff <before> <after>`](#diff-before-after)
23
+ - [`describe`](#describe)
24
+ - [`skill`](#skill)
25
+ - [Options](#options)
26
+ - [Exit codes](#exit-codes)
27
+ - [Output streams](#output-streams)
28
+ - [Examples](#examples)
29
+
30
+ ## Commands
31
+
32
+ ### `generate`
33
+
34
+ ```bash
35
+ codebase-snapshot generate [options]
36
+ ```
37
+
38
+ Scans the repository and writes the snapshot to `outDir` (default `./snapshot`). This is the
39
+ default command, so `codebase-snapshot` alone does the same thing.
40
+
41
+ Detection is automatic: a preset is chosen from the 25 in the
42
+ [preset table](configuration.md#project-presets), which supplies layers, kinds and ecosystem
43
+ ignores. The preset's *recommended* part budget is not applied by `generate` — that runs on
44
+ `--max-part-bytes` (default 307200) or your config. `init` is what writes the recommended
45
+ budget into a fresh config, so a project set up with `init` gets the preset's figure.
46
+
47
+ Exit `0` when the snapshot passes its own integrity check, `1` when it does not.
48
+
49
+ ```
50
+ 📸 Generating snapshot for: /path/to/repo
51
+ Branch: main | Part budget: 300 KB
52
+
53
+ ✅ Snapshot generated in 0.14s:
54
+ - Tracked files: 128
55
+ - Total lines: 9,431
56
+ - Total size: 0.41 MB
57
+ - Parts written: 3 under snapshot/parts/
58
+ - Reports: snapshot/reports/ (project-tree, navigation, audit)
59
+ - Manifest: snapshot/manifest.json
60
+ - Fingerprint: e4607b570268c8def0911babbace97e4a0196d9eaf2cac22c8847500883d70aa
61
+ - Integrity: passed
62
+ - Redacted: 2 line(s) — values masked; see snapshot/reports/audit.md
63
+ ```
64
+
65
+ The fingerprint is printed in full — all 64 hex characters, never elided — and it matches
66
+ `manifest.fingerprint`.
67
+
68
+ The summary only lists artifacts that were actually written, so a run with
69
+ `--only-artifacts parts` does not claim a manifest exists.
70
+
71
+ **`Redacted` and `Warnings` are different lines, and that is deliberate.** A redaction means the
72
+ tool masked a secret-shaped value: it is the tool doing its job, and it is counted **once per
73
+ line** even when two rules match that line. The per-line list — file, line, every rule that
74
+ matched — is `reports/audit.md`, and `manifest.totals.redactedLines` carries the count.
75
+
76
+ `Warnings` is for things you may want to act on: a disabled artifact, a file too large to parse
77
+ for a summary, a non-UTF-8 file that was transcoded. The summary shows the first ten and then
78
+ says `… and N more in manifest.warnings[]`; nothing is dropped silently.
79
+
80
+ ### `validate` / `check`
81
+
82
+ ```bash
83
+ codebase-snapshot validate [options]
84
+ ```
85
+
86
+ Compares the snapshot on disk with the repository. Reports five classes of drift — modified
87
+ files, deleted files, files added since the snapshot, damaged or missing parts, and config
88
+ changes — plus bookkeeping mismatches in the manifest.
89
+
90
+ Exit `0` when the snapshot is current, `1` on any drift. It never writes anything.
91
+
92
+ ### `report`
93
+
94
+ ```bash
95
+ codebase-snapshot report [options]
96
+ ```
97
+
98
+ Prints what the snapshot says about itself: counts, layer breakdown, warnings, fingerprint.
99
+ It reads `manifest.json` and nothing else, so it is fast on any repository size — a 20 000-file
100
+ snapshot reports in about half a second. Requires an existing `manifest.json` (exit `1` if
101
+ missing); exits `0` otherwise. It does **not** check whether the snapshot is still true — that
102
+ is `validate`, which re-reads the repository and exits `1` on drift.
103
+
104
+ ### `setup`
105
+
106
+ ```bash
107
+ codebase-snapshot setup
108
+ ```
109
+
110
+ The whole first run in one command, with defaults and no prompts:
111
+
112
+ 1. Detects the project preset.
113
+ 2. Writes `snapshot.config.ts` (or `.mjs`) — **only when the repository has none**. An existing
114
+ config is kept byte for byte and the output says `Keeping …`.
115
+ 3. Adds `scripts.snapshot`, `scripts.snapshot:validate`, the `devDependencies` entry and the
116
+ `.gitignore` entries, exactly as `init` does.
117
+ 4. Installs dependencies with the package manager the repository already uses — a lockfile
118
+ decides (`bun.lockb`, `pnpm-lock.yaml`, `yarn.lock`, otherwise npm). A failed install is a
119
+ warning, not a dead end: the snapshot is still generated.
120
+ 5. Generates through the normal `generate` command, so the summary, the output-directory guard and
121
+ the `--format json` document are the ones a plain run produces.
122
+
123
+ ```
124
+ 🚀 Setup for: /path/to/repo
125
+ ✅ Created snapshot.config.ts (preset: nextjs)
126
+ Added npm scripts: snapshot, snapshot:validate
127
+ Added devDependency: codebase-snapshot ^4.4.0
128
+ Added to .gitignore: snapshot/, snapshot.tmp/
129
+ ⏳ Installing dependencies…
130
+ ✅ Installed dependencies (npm install, 2.1s)
131
+ ✅ Snapshot generated in 0.16s:
132
+ - Tracked files: 128
133
+ …
134
+ Next: npm run snapshot
135
+ ```
136
+
137
+ - `--no-install` skips step 4 — CI, an offline machine, or a lockfile you would rather no package
138
+ manager touched.
139
+ - `--dry-run` prints the plan and the config it would write, and writes nothing.
140
+ - `--force` rewrites an existing config; `--preset`, `--include`, `--exclude`, `--out-dir`,
141
+ `--max-part-bytes`, `--include-sensitive-files` and `--skip-artifacts` behave as they do for
142
+ `generate` and `init`.
143
+ - `--format json` emits **one** document:
144
+ `{ "setup": { configPath, configReused, preset, scripts, dependency, gitignoreEntries, install, notes }, "manifest": { … } }`,
145
+ where `manifest` is the same object `generate --format json` writes.
146
+ - If `outDir` holds files this tool did not write, `setup` still refuses to replace it (exit 1,
147
+ naming the entries) after writing the config — the guard is not weakened by the convenience.
148
+ ### `init`
149
+
150
+ ```bash
151
+ codebase-snapshot init [options]
152
+ ```
153
+
154
+ Detects the project and writes `snapshot.config.ts` (or `.mjs`, see below) containing
155
+ **every** setting at its current value, commented and grouped. It is a reference you can
156
+ keep, not just a diff — there is no shorter variant, because the file that documents the
157
+ settings is the file you edit.
158
+
159
+ It also has three side effects on files you own, and it says so when it does them:
160
+
161
+ ```
162
+ ✅ Created snapshot.config.ts (preset: node-lib)
163
+ Added npm scripts: snapshot, snapshot:validate
164
+ Added devDependency: codebase-snapshot ^4.1.1
165
+ Added to .gitignore: snapshot/, snapshot.tmp/
166
+ ```
167
+
168
+ - `scripts.snapshot` and `scripts.snapshot:validate` are added to `package.json` if those keys
169
+ are absent. Existing values are left alone, and the rewrite keeps the file's own indentation
170
+ and line endings. Without a `package.json` it notes that scripts were not added.
171
+ - `codebase-snapshot` is added to `devDependencies` when no dependency field
172
+ declares it yet. That is what puts a real copy in `node_modules` and stops `npm run
173
+ snapshot` from resolving `@latest` out of the registry on every run. A range you already
174
+ declared — including a pin to an older version — is never overwritten.
175
+ - `snapshot/` and `snapshot.tmp/` are appended to `.gitignore` if they are not already listed;
176
+ the file is created if missing, and entries already present are not duplicated.
177
+
178
+ Nothing else in the repository is touched: no `AGENTS.md`, no `.claude/`, no `.cursor/`. In a
179
+ repository with a `package.json` and no config, `git status` shows the config as the only new
180
+ file.
181
+
182
+ Interactive by default; with `--yes` it accepts every default and never prompts, which is
183
+ what agents and release checks should use.
184
+
185
+ The filename adapts to the project: `.ts` only when the running Node can strip types
186
+ (≥ 22.6) **and** the project is an ES module; otherwise `.mjs`, which loads everywhere. The
187
+ generated file is a plain object with no imports, so it works even when the package is only
188
+ run through `npx` and never installed.
189
+
190
+ Related options: `--preset`, `--include`, `--exclude`, `--yes` and `--force`
191
+ apply to `init` only. `init` also accepts `--dry-run`, `--max-part-bytes`, `--out-dir`,
192
+ `--include-sensitive-files`, `--skip-artifacts` and `--only-artifacts`, which it shares with
193
+ the commands that re-read a snapshot, and records their effect in the generated file. The
194
+ `Applies to` column is in the [options table](#options).
195
+
196
+ ### `explain <path>`
197
+
198
+ ```bash
199
+ codebase-snapshot explain src/server.ts
200
+ ```
201
+
202
+ Answers "what would happen to this file?" — layer, kind, language, size, which part it lands
203
+ in, and the exact reason if it is skipped (`sensitive-file`, `binary`, `default-ignore`,
204
+ `excluded`, and the other reasons in [Troubleshooting](troubleshooting.md#a-file-is-missing-from-the-snapshot)).
205
+
206
+ Exit `0` whenever the path could be inspected, whether or not it would be included — the
207
+ answer is in the output. Exit `1` only when the command itself could not run.
208
+
209
+ This is the fastest way to debug a file that is unexpectedly missing from a snapshot.
210
+
211
+ With `--format json` the same answer is one object on stdout, with only the keys that apply:
212
+
213
+ ```json
214
+ {
215
+ "path": "src/math.ts",
216
+ "exists": true,
217
+ "included": true,
218
+ "layer": "src",
219
+ "kind": "other",
220
+ "language": "typescript",
221
+ "layerMatch": "function (custom predicate)",
222
+ "bytes": 63,
223
+ "lines": 3,
224
+ "secretCount": 0,
225
+ "secrets": [],
226
+ "part": "part-001.md",
227
+ "inclusionMode": "full"
228
+ }
229
+ ```
230
+
231
+ | Key | Meaning |
232
+ | :--- | :--- |
233
+ | `path` | The path you asked about, relative to `--cwd` and with POSIX separators. |
234
+ | `exists` | Whether anything is on disk at that path. |
235
+ | `included` | Whether the scanner would include it in the next snapshot. |
236
+ | `reason`, `detail` | Why not, when `included` is `false`. The same strings `manifest.skipped[]` uses, plus `missing` and `not-discovered`. A path with no record of its own inherits the record of the nearest skipped ancestor directory, and `detail` names that ancestor — for example ``ignored by .gitignore — inherited from the skipped directory `ignored` `` or ``built-in ignore: directory "node_modules" (not descended) — inherited from the skipped directory `node_modules` ``. |
237
+ | `layer`, `kind`, `language` | The classification, present only when `included` is `true`. |
238
+ | `layerMatch`, `kindMatch` | Which rule chose the layer or kind. A glob is printed as written (`src/api/**`); a `RegExp` as its literal form (`/\.tsx$/i`); a function matcher, which is what the presets use, as `function (custom predicate)`. Absent when no rule matched and the `other` fallback was used. |
239
+ | `bytes`, `lines` | Size of the file on disk. |
240
+ | `encoding` | Present only when the file is not plain UTF-8 — `utf-16le`, `utf-16be` or `utf-8-lossy`. |
241
+ | `secretCount`, `secrets` | How many secret rules fire, and the first 20 as `{ rule, line }` — never the matched text. |
242
+ | `part` | The part filename from the last written manifest, or `null` when it is not in that snapshot. |
243
+ | `inclusionMode` | `full` or `preview`, when a part entry exists. |
244
+
245
+ When `included` is `false` the object stops after `path`, `exists`, `included`, `reason` and
246
+ `detail`.
247
+
248
+ ### `diff <before> <after>`
249
+
250
+ ```bash
251
+ codebase-snapshot diff ./snapshot-old ./snapshot
252
+ codebase-snapshot diff ./a/manifest.json ./b/manifest.json
253
+ ```
254
+
255
+ Compares two snapshots and lists added, removed and changed files. Either argument may be a
256
+ snapshot directory or a `manifest.json`. Useful in review: "what changed in the context we
257
+ hand to the model?"
258
+
259
+ Comparison is per file (`relativePath` + `sha256`), so on its own it cannot see a run that
260
+ repacked the *same* sources differently. `diff` therefore also compares the two manifests'
261
+ `generator.version`, `config.digest`, content fingerprint, part count and layer totals, and
262
+ prints a `payload/configuration differs:` section when the file set is identical but the
263
+ emitted payload — or the configuration that produced it — is not. That covers `--large-file-bytes`
264
+ (a preview becomes a full body), the preview head/tail counts, redaction rules, artifact
265
+ selection and per-layer budgets, none of which move a source hash. The per-file lists and the
266
+ JSON fields `added` / `removed` / `changed` / `unchanged` are unchanged; the extra data is
267
+ under `payload` in `--format json`.
268
+
269
+ Manifests written before 4.0.5 have no `config.digest`. In that case `diff` compares the
270
+ content fingerprint and the part count only, and prints `configuration not compared: a
271
+ manifest has no config digest (written before 4.0.5).` rather than implying the settings match.
272
+
273
+ ### `describe`
274
+
275
+ ```bash
276
+ codebase-snapshot describe # human-readable
277
+ codebase-snapshot describe --format json # machine-readable contract
278
+ ```
279
+
280
+ Prints the tool's complete contract: the supported invocation, an ordered workflow, every
281
+ command, every option with its default, the exit-code meanings, all presets, the artifact
282
+ list, config filenames, output layout, safety rules and a troubleshooting table. The JSON
283
+ form is what an agent should read instead of `--help`. See [Using this with an AI
284
+ agent](agent.md).
285
+
286
+ ### `skill`
287
+
288
+ ```bash
289
+ codebase-snapshot skill # print on stdout
290
+ codebase-snapshot skill --install # write both registries
291
+ codebase-snapshot skill --install --target claude
292
+ ```
293
+
294
+ Prints or installs the bundled agent skill. Installation writes
295
+ `.claude/skills/codebase-snapshot/SKILL.md` and `.cursor/rules/codebase-snapshot.mdc`;
296
+ `--target claude` or `--target cursor` limits it to one. Both are **registries keyed by
297
+ skill name**, so the file cannot be mistaken for your own documentation.
298
+
299
+ It does not write `AGENTS.md`. That file is your project's agent contract — build commands,
300
+ conventions, house rules — and a tool has no business editing it. If you want the skill
301
+ there, print it and paste it:
302
+
303
+ ```bash
304
+ codebase-snapshot skill >> AGENTS.md
305
+ ```
306
+
307
+ Whatever the install *creates* is appended to `.gitignore` in the same run, so a one-line
308
+ command does not leave three untracked files behind. Re-running is idempotent, and a file that
309
+ already existed is never ignored. `--format json` reports what was created, updated or
310
+ unchanged, plus the ignore entries it added.
311
+
312
+ Printing goes to **stdout** so it can be redirected: `codebase-snapshot skill > SKILL.md`.
313
+
314
+ ## Options
315
+
316
+ An option a command does not accept is **rejected**, not ignored: exit `2` and a message
317
+ naming the commands that do accept it. Options marked *init only* or *skill only* are the
318
+ narrowest cases of the same rule.
319
+
320
+ | Option | Type | Default | Applies to | Meaning |
321
+ | :--- | :--- | :--- | :--- | :--- |
322
+ | `--cwd <path>` | path | current directory | generate, validate, report, explain, init, skill | Repository to operate on. Must exist and be a directory. `diff` and `describe` take no `--cwd`. |
323
+ | `--out-dir <path>` | path | `snapshot` | generate, validate, report, explain, init | Where the snapshot is written or read. May be absolute. |
324
+ | `--max-part-bytes <n>` | size | `307200` (300 KB) | generate, validate, report, explain, init | Hard ceiling per markdown part. Accepts `120k`, `1.5mb`. Minimum 4096. |
325
+ | `--large-file-bytes <n>` | size | `204800` (200 KB) | generate, validate, report, explain | Above this, a file becomes a head/tail preview. |
326
+ | `--max-monolith-bytes <n>` | size | unset | generate, validate, report, explain | Skip the single-file monolith when it would exceed this. |
327
+ | `--include-monolith` | flag | off | generate, validate, report, explain | Also write `full-codebase.json` and `full-codebase.md`. |
328
+ | `--include-sensitive-files` | flag | off | generate, validate, report, explain, init | Emit `.env`, `*.pem`, `.npmrc` and similar. Off by default; they are reported as skipped. |
329
+ | `--skip-artifacts <names>` | list | unset | generate, validate, report, explain, init | Comma-separated artifacts to omit. |
330
+ | `--only-artifacts <names>` | list | unset | generate, validate, report, explain, init | Write only these. Cannot be combined with `--skip-artifacts`. |
331
+ | `--format <text\|json>` | enum | `text` | generate, validate, report, explain, init, diff, describe, skill | `json` writes a pure JSON payload to stdout. Every command honours it, and `describe` reports the same eight-command list. |
332
+ | `--dry-run` | flag | off | generate, init | Compute everything, print it, write nothing. |
333
+ | `--watch` | flag | off | generate | Regenerate on every change until interrupted. |
334
+ | `--no-config` | flag | off | generate, validate, report, explain, diff | Ignore every `snapshot.config.*` file and run on the built-in defaults. `diff` never loads a config anyway and accepts the flag for consistency. |
335
+ | `--discovery <mode>` | enum | `auto` | generate, validate, explain | How files are found: `auto` lists through git when git works and refuses when a `.git` directory cannot be listed; `git` requires git; `walk` scans the tree and reads `.gitignore` files directly. The escape hatch for a broken `.git`. |
336
+ | `--preset <name>` | name | detected | *init only* | Force a preset instead of detecting one. |
337
+ | `--include <globs>` | list | unset | *init only* | Allow-list recorded in the generated config. |
338
+ | `--exclude <globs>` | list | unset | *init only* | Exclude globs recorded in the generated config. |
339
+ | `--yes` | flag | off | *init only* | Never prompt; accept every default. |
340
+ | `--force` | flag | off | *init only* | Replace an existing `.ts`/`.js`/`.mjs` config in place. A JSON-family config is refused, naming the file to remove. |
341
+ | `--install` | flag | off | *skill only* | Write the skill into the repository instead of printing it. |
342
+ | `--target <name>` | enum | `all` | *skill only* | `claude`, `cursor` or `all`. |
343
+ | `-h`, `--help` | flag | — | every command | Print usage. |
344
+ | `-v`, `--version` | flag | — | every command | Print the version on stdout: a bare `v`-prefixed semver such as `v4.0.0`, with no package name. |
345
+
346
+ The artifact and size options apply to `validate`, `report` and `explain` as well as
347
+ `generate` because those commands re-read the snapshot: pass the selection you generated
348
+ with and the comparison is like for like. `validate --skip-artifacts parts` on a snapshot
349
+ built without parts therefore agrees, while the same flag on a full snapshot reports a
350
+ config mismatch.
351
+
352
+ Every option also accepts the `--option=value` form. That form is required for negative
353
+ numbers, which otherwise look like flags.
354
+
355
+ ### `--mode`
356
+
357
+ `--mode <command>` names the command instead of passing it positionally, which is handy when
358
+ a wrapper builds the argument list. `--mode validate` and `codebase-snapshot validate` are the
359
+ same run. It accepts the nine command names, and it is deliberately strict: asking for two
360
+ different commands is an error rather than a silent winner.
361
+
362
+ ```
363
+ ❌ --mode validate contradicts the command "generate". Pass one or the other.
364
+ ❌ Unknown --mode "bogus". Expected one of: generate, validate, check, report, init, explain, diff, describe, skill.
365
+ ```
366
+
367
+ `--mode` is not listed in `describe`'s option table or `--help`, on purpose: an agent has no
368
+ reason to use it, and the positional form is the documented one.
369
+
370
+ ## Exit codes
371
+
372
+ | Code | Meaning |
373
+ | :--- | :--- |
374
+ | `0` | Success. For `validate`/`check`, no drift. |
375
+ | `1` | Drift detected, generation refused or failed (nothing was written), or the state the command needed was not there. |
376
+ | `2` | Usage error: unknown flag or command, an option the command does not accept, bad value, wrong argument count. |
377
+
378
+ `1` covers the cases where the tool did the right thing but the state is not what you wanted
379
+ — drift, a refused output directory, an unparseable config. `2` means the command itself was
380
+ wrong and retrying it unchanged will fail the same way.
381
+
382
+ ## Output streams
383
+
384
+ - **stdout** carries the payload. With `--format json` it is pure JSON, safe to pipe into
385
+ `jq` or `JSON.parse`.
386
+ - **stderr** carries progress, the human summary, warnings and errors.
387
+ - **In text mode the human result is on stderr, and stdout is empty.** That holds for every
388
+ command: `report`, `explain`, `diff` and `validate` print their answer — tables, verdicts,
389
+ drift lines — to stderr, and `generate` prints its summary there too. Redirecting
390
+ `command > file.txt` therefore writes an empty file in text mode; use `--format json` when
391
+ you want the output on stdout.
392
+
393
+ A failing invocation still writes one JSON document to stdout, so an agent never has to parse an
394
+ empty string:
395
+
396
+ ```json
397
+ { "error": { "message": "Manifest not found at /repo/snapshot/manifest.json. Run generate first.", "exitCode": 1 } }
398
+ ```
399
+
400
+ The human line stays on stderr and the exit code is unchanged — `{ error: … }` is an addition, not a
401
+ replacement. A successful run never carries an `error` key.
402
+
403
+ So `codebase-snapshot describe --format json > contract.json`,
404
+ `codebase-snapshot validate --format json | jq .drift` and
405
+ `codebase-snapshot report --format json || jq -r .error.message` all work.
406
+
407
+ ## Examples
408
+
409
+ ```bash
410
+ # The common path
411
+ npx codebase-snapshot generate
412
+
413
+ # A tighter budget for a small model context, in a separate directory
414
+ npx codebase-snapshot generate --out-dir .context --max-part-bytes 120k
415
+
416
+ # Just the code parts, no reports
417
+ npx codebase-snapshot generate --only-artifacts parts,manifest
418
+
419
+ # One file per part, with everything else left alone
420
+ npx codebase-snapshot generate --only-artifacts parts
421
+
422
+ # Why is this file missing?
423
+ npx codebase-snapshot explain src/generated/schema.ts
424
+
425
+ # What changed between two snapshots?
426
+ npx codebase-snapshot diff old-snapshot snapshot
427
+
428
+ # Non-interactive setup, then a JSON dry run to inspect the result
429
+ npx codebase-snapshot init --yes
430
+ npx codebase-snapshot generate --dry-run --format json | jq .config
431
+
432
+ # Keep the snapshot fresh while editing
433
+ npx codebase-snapshot generate --watch
434
+
435
+ # Give an agent everything it needs
436
+ npx codebase-snapshot skill --install
437
+ ```