codebase-snapshot 0.12.15 → 0.14.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.
Files changed (44) hide show
  1. package/CHANGELOG.md +1063 -3
  2. package/README.md +14 -3
  3. package/SECURITY.md +2 -2
  4. package/dist/bin.js +10092 -5998
  5. package/dist/{chunk-FZB2FTHF.js → chunk-2YNQ42B3.js} +7971 -865
  6. package/dist/chunk-7WSZUMT6.js +306 -0
  7. package/dist/cli.cjs +9763 -5718
  8. package/dist/cli.d.cts +115 -2
  9. package/dist/cli.d.ts +115 -2
  10. package/dist/cli.js +389 -8
  11. package/dist/{generate-worker-BK4uJx7Z.d.cts → generate-worker-B79yGkQO.d.cts} +16 -0
  12. package/dist/{generate-worker-BK4uJx7Z.d.ts → generate-worker-B79yGkQO.d.ts} +16 -0
  13. package/dist/index.cjs +7655 -744
  14. package/dist/index.d.cts +145 -6
  15. package/dist/index.d.ts +145 -6
  16. package/dist/index.js +1 -1
  17. package/dist/mcp.cjs +3595 -1093
  18. package/dist/mcp.d.cts +92 -19
  19. package/dist/mcp.d.ts +92 -19
  20. package/dist/mcp.js +2 -2
  21. package/dist/{types-c0tLF2WC.d.cts → types-CbD8yQRZ.d.cts} +145 -1
  22. package/dist/{types-c0tLF2WC.d.ts → types-CbD8yQRZ.d.ts} +145 -1
  23. package/docs/agent.md +1 -1
  24. package/docs/benchmarks.md +80 -0
  25. package/docs/budget.md +59 -0
  26. package/docs/cli.md +41 -4
  27. package/docs/configuration.md +27 -0
  28. package/docs/delta.md +50 -0
  29. package/docs/deploy.md +75 -0
  30. package/docs/health.md +44 -0
  31. package/docs/mcp.md +116 -11
  32. package/docs/migration.md +68 -0
  33. package/docs/output.md +9 -3
  34. package/docs/remote.md +60 -0
  35. package/docs/security-review.md +117 -0
  36. package/docs/serve.md +66 -0
  37. package/docs/signing.md +61 -0
  38. package/docs/slice.md +57 -0
  39. package/docs/tools.md +161 -0
  40. package/docs/troubleshooting.md +63 -0
  41. package/docs/vs-repomix.md +1 -1
  42. package/docs/workspace.md +93 -0
  43. package/package.json +1 -1
  44. package/dist/chunk-EPAXW6QJ.js +0 -3749
@@ -20,8 +20,10 @@ type TextEncoding = "utf-8" | "utf-8-lossy" | "utf-16le" | "utf-16be";
20
20
  * How much of a file made it into the snapshot.
21
21
  * `full` – the complete file body was emitted.
22
22
  * `preview` – the body was truncated (by lines and/or bytes) before emission.
23
+ * `signatures` – function and method bodies were collapsed to an empty block.
24
+ * `comments-stripped` – comment text was removed (line numbers preserved) before emission.
23
25
  */
24
- type InclusionMode = "full" | "preview" | "signatures";
26
+ type InclusionMode = "full" | "preview" | "signatures" | "comments-stripped";
25
27
  interface SourceSummary {
26
28
  directives: string[];
27
29
  imports: string[];
@@ -115,6 +117,13 @@ type SkipReason = "binary" | "read-error" | "not-file"
115
117
  * `excluded` would send a reader looking for an exclude pattern they never wrote.
116
118
  */
117
119
  | "unchanged"
120
+ /**
121
+ * Scanned, but the `--slice` planner did not choose it for the question.
122
+ *
123
+ * Different from `unchanged`: the file may have changed and may still matter; it simply is not
124
+ * part of the budgeted answer to the question this snapshot was asked to serve.
125
+ */
126
+ | "not-selected"
118
127
  /**
119
128
  * Staged for deletion: the path is in the next commit's parent, not in the next commit.
120
129
  *
@@ -163,6 +172,14 @@ interface FileEntry {
163
172
  * would be the same silence this field exists to end.
164
173
  */
165
174
  omittedRanges?: Array<[number, number]>;
175
+ /**
176
+ * Blank lines were dropped from this body, so its line numbers do not match the source.
177
+ *
178
+ * Only `--remove-empty-lines` sets it. It is a separate field rather than an `inclusionMode`
179
+ * because the two describe different things: the mode says how the body was produced (full,
180
+ * preview, signatures) and this says the numbering moved.
181
+ */
182
+ emptyLinesRemoved?: true;
166
183
  /** Line count of the file on disk. */
167
184
  originalLines: number;
168
185
  /** Lines actually emitted into the snapshot. */
@@ -374,6 +391,8 @@ interface ManifestConfigSnapshot {
374
391
  */
375
392
  maxMonolithBytes?: number | null;
376
393
  includeSensitiveFiles: boolean;
394
+ /** Whether comment text was stripped from JS/TS bodies in this run. */
395
+ stripComments?: boolean;
377
396
  /**
378
397
  * The file-slot ceiling `PROMPT.md` was planned for; `0` for no limit.
379
398
  *
@@ -454,6 +473,21 @@ interface Manifest {
454
473
  commit: string;
455
474
  isDirty: boolean;
456
475
  };
476
+ /**
477
+ * Where the described tree came from, when it was fetched rather than found on disk.
478
+ *
479
+ * Absent for a local run. Present for `--remote`, with the commit that was actually cloned so
480
+ * a reader can trace the snapshot back to a revision rather than to a URL that may have moved.
481
+ */
482
+ source?: {
483
+ url: string;
484
+ host: string;
485
+ owner: string;
486
+ repo: string;
487
+ ref: string | null;
488
+ commit: string;
489
+ fetchedAt: string;
490
+ };
457
491
  budgets: {
458
492
  maxPartBytes: number;
459
493
  largeFileBytes: number;
@@ -489,8 +523,16 @@ interface Manifest {
489
523
  navigation?: ManifestArtifact;
490
524
  projectTree?: ManifestArtifact;
491
525
  audit?: ManifestArtifact;
526
+ /** `reports/diff.md`, when `diff` was requested and the repository had changes. */
527
+ diff?: ManifestArtifact;
528
+ /** `reports/log.md`, when `log` was requested and the repository had commits. */
529
+ log?: ManifestArtifact;
530
+ /** `reports/tokens.md`, when `tokenTree` was requested. */
531
+ tokenTree?: ManifestArtifact;
492
532
  fullJson: ManifestArtifact | null;
493
533
  fullMarkdown: ManifestArtifact | null;
534
+ /** `full-codebase.xml`, when `--pack xml` asked for it. */
535
+ fullXml?: ManifestArtifact | null;
494
536
  /**
495
537
  * `reports/audit.sarif`, when `--sarif` asked for it.
496
538
  *
@@ -500,6 +542,68 @@ interface Manifest {
500
542
  */
501
543
  sarif?: ManifestArtifact;
502
544
  };
545
+ /**
546
+ * What this snapshot contains relative to a base snapshot (`--delta`).
547
+ *
548
+ * Present only when a base was given. `files[]` then contains the changed and added files and
549
+ * their one-hop dependents; every unchanged file is accounted in `delta.unchanged` and in
550
+ * `skipped[]` with reason "unchanged", so nothing disappears without a record.
551
+ */
552
+ /**
553
+ * A task-scoped slice (`--slice`): which question the snapshot was narrowed for.
554
+ *
555
+ * `files[]` then contains the planner selection; every other scanned path is in `skipped[]`
556
+ * with reason "slice-dropped", so the accounting contract still holds.
557
+ */
558
+ /**
559
+ * A token-budget selection (`--budget-tokens`): the plan that fit the repository into a budget.
560
+ *
561
+ * `files[]` holds the selection; the rest are in `skipped[]` with reason "not-selected".
562
+ */
563
+ budget?: {
564
+ maxTokens: number;
565
+ usedTokens: number;
566
+ /** What survived, by layer and by symbol, so a reader can judge the cut. */
567
+ coverage?: {
568
+ byLayer: Record<string, {
569
+ kept: number;
570
+ total: number;
571
+ }>;
572
+ entryPoints: {
573
+ kept: number;
574
+ total: number;
575
+ };
576
+ symbols: {
577
+ kept: number;
578
+ total: number;
579
+ };
580
+ };
581
+ /** Paths chosen, in the order they were selected. */
582
+ files: string[];
583
+ /** Scanned paths that did not fit. */
584
+ dropped: number;
585
+ };
586
+ slice?: {
587
+ question: string;
588
+ budgetTokens: number;
589
+ usedTokens: number;
590
+ /** The paths the planner chose, in reading order. */
591
+ files: string[];
592
+ /** Scanned paths the planner did not choose. */
593
+ dropped: number;
594
+ };
595
+ delta?: {
596
+ base: {
597
+ path: string;
598
+ fingerprint: string;
599
+ generatedAt: string;
600
+ };
601
+ added: string[];
602
+ changed: string[];
603
+ removed: string[];
604
+ /** Paths present in both snapshots with the same hash; omitted from parts, listed here. */
605
+ unchanged: string[];
606
+ };
503
607
  layers: Record<string, number>;
504
608
  kinds: Record<string, number>;
505
609
  indexes: ManifestIndexes;
@@ -570,6 +674,12 @@ interface ArtifactSelection {
570
674
  projectTree?: boolean;
571
675
  /** `reports/audit.md` — security, layer, kind and skip findings. */
572
676
  audit?: boolean;
677
+ /** `reports/diff.md` — the uncommitted diff, when the repository has one. */
678
+ diff?: boolean;
679
+ /** `reports/log.md` — recent commits. */
680
+ log?: boolean;
681
+ /** `reports/tokens.md` — per-file token estimates. */
682
+ tokenTree?: boolean;
573
683
  /** `full-codebase.{json,md}` — one file per format, the whole payload again. */
574
684
  monolith?: boolean;
575
685
  }
@@ -678,6 +788,23 @@ interface SnapshotConfig {
678
788
  skeleton?: boolean;
679
789
  /** Also write `reports/audit.sarif` so a CI security gate can read the findings. */
680
790
  sarif?: boolean;
791
+ /** How many commits `reports/log.md` carries when it is enabled (default 20). */
792
+ logCount?: number;
793
+ /** Remove comment text from JavaScript/TypeScript bodies (line numbers preserved). */
794
+ stripComments?: boolean;
795
+ /**
796
+ * Drop blank and whitespace-only lines from emitted bodies.
797
+ *
798
+ * Unlike `stripComments`, this moves line numbers: the body no longer lines up with the source.
799
+ * Each affected file records `emptyLinesRemoved: true`, and the brief says so.
800
+ */
801
+ removeEmptyLines?: boolean;
802
+ /**
803
+ * Which single-file pack to write: `single` (markdown), `xml` or `json`.
804
+ *
805
+ * Unset keeps the historical `--include-monolith` behaviour: markdown and JSON together.
806
+ */
807
+ pack?: "single" | "xml" | "json";
681
808
  /**
682
809
  * With `changedSince` or `staged`, also include the files that import a changed one.
683
810
  *
@@ -745,6 +872,13 @@ interface ResolvedSnapshotConfig {
745
872
  * ingest is a finding nobody's build can act on.
746
873
  */
747
874
  sarif?: boolean;
875
+ /** How many commits `reports/log.md` carries when it is enabled. */
876
+ logCount: number;
877
+ /** Comment stripping for JS/TS bodies. */
878
+ stripComments: boolean;
879
+ removeEmptyLines: boolean;
880
+ /** The chosen single-file pack, or null for the historical pair. */
881
+ pack: "single" | "xml" | "json" | null;
748
882
  /** Also include the files that import a changed one. */
749
883
  withDependents?: boolean;
750
884
  /** Empty means "no allow-list"; every discovered file is a candidate. */
@@ -779,6 +913,16 @@ interface CliOptions {
779
913
  brief?: string;
780
914
  /** `--prompt-task`: which job the brief is written for. */
781
915
  promptTask?: PromptTask;
916
+ /** `--include-diffs`: write reports/diff.md from the uncommitted diff. */
917
+ includeDiffs?: boolean;
918
+ /** `--include-logs <n>`: write reports/log.md with the last n commits. */
919
+ includeLogs?: number;
920
+ /** `--token-tree`: write reports/tokens.md. */
921
+ tokenTree?: boolean;
922
+ /** `--strip-comments`: remove comment text from JS/TS bodies. */
923
+ stripComments?: boolean;
924
+ /** `--remove-empty-lines`: drop blank lines from emitted bodies. */
925
+ removeEmptyLines?: boolean;
782
926
  maxPartBytes?: number;
783
927
  maxPartTokens?: ByteSize | null;
784
928
  largeFileBytes?: number;
package/docs/agent.md CHANGED
@@ -115,7 +115,7 @@ devDependency and the `.gitignore` entries for the snapshot directory, and nothi
115
115
 
116
116
  ## When the client speaks MCP
117
117
 
118
- `codebase-snapshot mcp` serves the repository to a Model Context Protocol client over stdio: nine
118
+ `codebase-snapshot mcp` serves the repository to a Model Context Protocol client over stdio: eleven
119
119
  tools, `snapshot://` resources and two prompts. Prefer it over shelling out when the agent already
120
120
  has an MCP connection, for two reasons that are properties of the design rather than of the wiring:
121
121
 
@@ -0,0 +1,80 @@
1
+ # Benchmarks
2
+
3
+ Three ways to pack the same repository, measured on this repository itself. The numbers are generated
4
+ by [`scripts/benchmark.mjs`](https://github.com/ankurhalder/codebase-snapshot/blob/main/scripts/benchmark.mjs), which
5
+ anyone can run:
6
+
7
+ ```bash
8
+ node scripts/benchmark.mjs /path/to/repo "the question to slice for"
9
+ ```
10
+
11
+ The run below used the default question, on the repository at 0.12.34: 313 files, 987,750 estimated
12
+ tokens in full.
13
+
14
+ | Mode | Files | Parts | Tokens | vs full | Time |
15
+ | :-- | --: | --: | --: | --: | --: |
16
+ | full | 313 | 11 | 987,750 | 100% | 1.66 s |
17
+ | slice | 4 | 1 | 66,430 | 6.7% | 1.57 s |
18
+ | budget (30,000) | 21 | 1 | 36,315 | 3.7% | 1.55 s |
19
+
20
+ ## What the columns mean
21
+
22
+ - **Files** — entries in `manifest.files`, i.e. what the parts actually carry.
23
+ - **Parts** — part files written. A budget/slice snapshot tends to be one part because it fits.
24
+ - **Tokens** — `manifest.totals.estimatedTokens`, the same estimate the product reports. It is one
25
+ token per four bytes of emitted body, not a tokenizer count; every model tokenizes differently.
26
+ - **vs full** — token share of the full snapshot.
27
+ - **Time** — wall clock for the whole `generate` process, including scan, parse, partition and write.
28
+
29
+ ## What to take from it
30
+
31
+ 1. **The full snapshot is usually too large to paste.** Under a million tokens for this repository is
32
+ typical for a mature codebase; a 200k-token window cannot hold it.
33
+ 2. **A slice is two orders of magnitude smaller and still coherent**: four files that the planner
34
+ judged relevant, with their bodies intact.
35
+ 3. **A budget is the "fit the window" answer**: 21 whole files at 3.7% of the tokens, chosen by entry
36
+ point, importer count and layer rather than by alphabetical order.
37
+ 4. **Generation is not the bottleneck.** All three modes take about the same time; the saving is in
38
+ what gets written, not in what gets read.
39
+
40
+ ## Honest limits
41
+
42
+ - Self-benchmark. This repository is TypeScript with a clean import graph; a vendored monorepo with
43
+ a thousand dependencies will score differently, and a repository with no imports at all gives the
44
+ planner little to rank.
45
+ - One machine, one run. The times are indicative, not a controlled benchmark; the token counts are
46
+ deterministic for a fixed repository and configuration.
47
+ - These are **our** modes. The head-to-head is below, with both tools' own accounting.
48
+
49
+ ## Head-to-head against repomix
50
+
51
+ ```bash
52
+ node scripts/compare.mjs --repomix # add --json for machine-readable output
53
+ ```
54
+
55
+ repomix is fetched through `npx` at run time; it is never a dependency of this package. Measured on
56
+ this repository at 0.12.65, repomix 1.19.0, same machine:
57
+
58
+ | Tool / mode | Files | Content bytes | Tokens | vs full | Time |
59
+ | :-- | --: | --: | --: | --: | --: |
60
+ | codebase-snapshot (full) | 330 | 3,197,091 | 1,027,911 | 100% | 2.14 s |
61
+ | codebase-snapshot (slice) | 4 | 239,395 | 66,653 | 6.5% | 2.13 s |
62
+ | codebase-snapshot (budget 30,000) | 24 | 119,701 | 36,261 | 3.5% | 2.07 s |
63
+ | repomix 1.19.0 | 329 | 3,386,284 | 854,178 | 83.1% | n/a |
64
+
65
+ Read that honestly:
66
+
67
+ - **repomix reports fewer tokens for a similar pack** (854k against our 1.03M, 83%). The cause is
68
+ the estimator, not the content: we count one token per four bytes, repomix uses a tokenizer.
69
+ Our number is conservative, which is the right direction for a budget — a plan that fits
70
+ 1,027,911 tokens fits a tokenizer count of 854,178 far more often than the reverse.
71
+ - **Our pack is smaller in bytes** (3.20 MB of file content against repomix's 3.39 MB of output,
72
+ which includes its own wrappers), and the file sets differ slightly: 330 against 329.
73
+ - **The modes are what the other tool does not have.** repomix packs the repository; a slice and a
74
+ budget are the same snapshot narrowed after scanning, at 6.5% and 3.5% of the tokens.
75
+ - **gitingest is not installed here** (`python -m gitingest` is missing). The harness supports it —
76
+ `--gitingest` — and reports its digest size, not a token count, because it does not publish one.
77
+
78
+ The same caveats apply to any comparison: different ignore rules, different wrappers, different
79
+ tokenizers. What is comparable is the shape — full packs are too large for a context window, and a
80
+ selection is one to two orders of magnitude smaller.
package/docs/budget.md ADDED
@@ -0,0 +1,59 @@
1
+ # Fit a repository into a token budget
2
+
3
+ ```bash
4
+ codebase-snapshot generate --budget-tokens 100000
5
+ ```
6
+
7
+ A part budget truncates files to fit; a **token budget** selects whole files to fit. The difference
8
+ matters: a torn file gives a reader half a function, while a selected set gives them fewer
9
+ subsystems that are each complete.
10
+
11
+ ## How it chooses
12
+
13
+ Every scanned file is scored once:
14
+
15
+ | Signal | Weight | Why |
16
+ | :--- | :--- | :--- |
17
+ | Entry point (`main`, `index`, `app`, `server`, `cli`, `package.json`, `go.mod`, `Cargo.toml`, `pyproject.toml`) | +100 | Without it, nothing else makes sense. |
18
+ | Configuration layer | +40 | What the project is, before what it does. |
19
+ | Source layer | +20 | The code. |
20
+ | Test layer | −10 | Useful, but after the code it tests. |
21
+ | Importer count | up to +60, 6 per importer | A file many others import is load-bearing. |
22
+ | Cost | up to −40 | A cheap file that matters beats an expensive one that barely does. |
23
+
24
+ Files are taken in score order while the running token estimate fits; a file that would exceed the
25
+ budget is skipped whole, not cut. The estimate is one token per four bytes of emitted body — the
26
+ same estimate the manifest reports.
27
+
28
+ ## What it records
29
+
30
+ ```json
31
+ "budget": {
32
+ "maxTokens": 100000,
33
+ "usedTokens": 96812,
34
+ "files": ["package.json", "src/main.ts", "src/core.ts", "..."],
35
+ "dropped": 418,
36
+ "coverage": {
37
+ "byLayer": { "config": { "kept": 3, "total": 3 }, "src": { "kept": 12, "total": 40 } },
38
+ "entryPoints": { "kept": 2, "total": 2 },
39
+ "symbols": { "kept": 18, "total": 61 }
40
+ }
41
+ }
42
+ ```
43
+
44
+ `coverage` is the honest half of the report: which layers kept what, how many entry points
45
+ survived, and how many exported symbols made it in. When an entry point is lost the run warns —
46
+ a snapshot without its entry point is a tour without a front door.
47
+
48
+ Every scanned path that did not fit is in `skipped[]` with reason `not-selected` and the detail
49
+ "did not fit the --budget-tokens budget". The brief states the budget, the selection size and the
50
+ drop count, so a reader knows what they are missing.
51
+
52
+ ## When to use it
53
+
54
+ - "I have 128k of context and this repository is 2M tokens": this is the one-command answer.
55
+ - A first pass over an unfamiliar repository, before deciding what to read next.
56
+ - Pair it with `--delta` to spend the budget only on what changed.
57
+
58
+ It is not a replacement for `--slice`: a slice answers a question, a budget answers a limit, and
59
+ the CLI refuses both at once rather than guessing which narrowing you meant.
package/docs/cli.md CHANGED
@@ -97,7 +97,7 @@ Exit `0` when the snapshot passes its own integrity check, `1` when it does not.
97
97
 
98
98
  ```
99
99
  📸 Generating snapshot for: /path/to/repo
100
- Branch: main | Part budget: 300 KB | Generator: 0.12.15 (project dependency ^0.12.15)
100
+ Branch: main | Part budget: 300 KB | Generator: 0.14.0 (project dependency ^0.14.0)
101
101
 
102
102
  ✅ Snapshot generated in 0.14s:
103
103
  - Tracked files: 121 included of 128 paths found — 7 skipped (default-ignore 5, binary 1, sensitive-file 1); every one is listed in snapshot/reports/audit.md
@@ -152,6 +152,17 @@ like the two other path-bearing arrays.
152
152
 
153
153
  Exit `0` when the snapshot is current, `1` on any drift. It never writes anything.
154
154
 
155
+ ### `serve`
156
+
157
+ ```bash
158
+ codebase-snapshot serve [--port 4788] [--host 127.0.0.1] [--token <value>] [--no-auth]
159
+ ```
160
+
161
+ A browser UI on loopback: paste a path, generate, read the brief, download the snapshot as a
162
+ zip. The same process serves MCP at `POST /mcp` with the same bearer token, generated on first
163
+ run and printed once. The page asks for the token and keeps it in `localStorage`.
164
+ See [the browser page](serve.md).
165
+
155
166
  ### `health`
156
167
 
157
168
  ```bash
@@ -207,7 +218,7 @@ The whole first run in one command, with defaults and no prompts. This is the sa
207
218
  🚀 Setup for: /path/to/repo
208
219
  ✅ Created snapshot.config.ts (preset: nextjs)
209
220
  Added npm scripts: snapshot, snapshot:validate
210
- Added devDependency: codebase-snapshot ^0.12.15
221
+ Added devDependency: codebase-snapshot ^0.14.0
211
222
  Added to .gitignore: /snapshot/, /snapshot.tmp/
212
223
  ⏳ Installing dependencies…
213
224
  ✅ Installed dependencies (npm install, 2.1s)
@@ -259,7 +270,7 @@ It also has three side effects on files you own, and it says so when it does the
259
270
  ```
260
271
  ✅ Created snapshot.config.ts (preset: node-lib)
261
272
  Added npm scripts: snapshot, snapshot:validate
262
- Added devDependency: codebase-snapshot ^0.12.15
273
+ Added devDependency: codebase-snapshot ^0.14.0
263
274
  Added to .gitignore: /snapshot/, /snapshot.tmp/
264
275
  ```
265
276
 
@@ -439,7 +450,7 @@ codebase-snapshot mcp # serve the current directory over stdio
439
450
  codebase-snapshot mcp --cwd <path> # serve another repository
440
451
  ```
441
452
 
442
- Serves the repository to a Model Context Protocol client over stdio: nine tools, `snapshot://`
453
+ Serves the repository to a Model Context Protocol client over stdio: 24 tools, `snapshot://`
443
454
  resources and two prompts. Stdout carries JSON-RPC messages and nothing else; every note goes to
444
455
  stderr. It speaks both protocol eras — the legacy `initialize` handshake and the stateless
445
456
  `2026-07-28` revision — and answers whichever the client starts with.
@@ -466,7 +477,33 @@ for the two install flags, and the init-and-setup flags list both commands.
466
477
  | `--large-file-bytes <n>` | size | the part budget (307200) | generate, validate, explain, setup | Above this, a file becomes a head/tail preview. It may not exceed `--max-part-bytes`: a file between the two could not fit a part. |
467
478
  | `--max-monolith-bytes <n>` | size | unset | generate, validate, setup | Skip the single-file monolith when it would exceed this. |
468
479
  | `--brief <path>` | path | unset | generate, validate | The owner-written half of `PROMPT.md`: `Stakes`, `The system`, `What came before`, `The bar`, `Calibration`. Unknown headings are kept as Owner notes; measured headings cannot be redefined; the file is digested, so editing it is drift. |
480
+ | `--remote <url>` | url | unset | generate | Fetch a repository instead of scanning the current directory. https only, public addresses only, shallow clone; the manifest records the URL and the commit. See [remote](remote.md). |
481
+ | `--ref <branch>` | string | unset | generate | With `--remote`: the branch or tag to clone. |
482
+ | `--allow-private` | flag | off | generate | With `--remote`: allow private addresses and `file:` URLs. Refused by default. |
483
+ | `--keep-clone` | flag | off | generate | With `--remote`: keep the temporary clone and print where it is. |
484
+ | `--http` | flag | off | mcp | Serve MCP over HTTP (Streamable HTTP: POST a JSON-RPC message to `/mcp`) instead of stdio. Loopback by default; a bearer token is generated and printed unless `--token` or `--no-auth` is given. Health endpoint: `GET /healthz`. |
485
+ | `--port <n>` | string | `4789` | mcp, serve | `mcp --http`: port to bind. `0` picks an ephemeral port and prints it. |
486
+ | `--host <addr>` | string | `127.0.0.1` | mcp, serve | `mcp --http`: address to bind. Anything other than loopback requires a token; `--no-auth` is refused there. |
487
+ | `--token <secret>` | string | generated | mcp, serve | `mcp --http`: bearer token clients must send. Generated per run when omitted. |
488
+ | `--no-auth` | flag | off | mcp, serve | `mcp --http`: accept requests without `Authorization`. Refused on a non-loopback bind. |
489
+ | `--allow-origin <list>` | list | unset | mcp | `mcp --http`: comma-separated extra CORS origins. Loopback origins are always allowed. |
490
+ | `--all-members` | flag | off | health | Audit every snapshot a `--workspace` run wrote, not only the one at `outDir`. The combined score is the weakest member. |
491
+ | `--strict` | flag | off | health | `health` only: exit 2 when a critical check fails, and 1 when a warning is the only thing left. Without it a critical check exits 1 and warnings exit 0. |
492
+ | `--mode <command>` | string | unset | generate, validate, report, explain, init, setup, diff, describe, skill | Select the command when no command word is given. A command word and `--mode` must name the same command; a contradiction is a usage error (exit 2). |
493
+ | `--no-update-check` | flag | off | all commands | Skip the check for a newer published version. Already skipped when stdout is not a terminal, in CI, in a JSON invocation, and in `mcp`, which makes no network requests. |
469
494
  | `--prompt-task <task>` | string | `audit` | generate, validate | `audit`, `review` or `explain` — which job the brief is written for. Changes the opening section, not the standard. |
495
+ | `--budget-tokens <n>` | count | unset | generate | Fit the snapshot into n tokens by selecting whole files (entry points, then the most-imported). Records a `budget` block; the rest are `not-selected`. See [budget](budget.md). |
496
+ | `--sign` | flag | off | generate | Sign `manifest.json` with Ed25519 and write `signature.json`. Key from `SNAPSHOT_SIGNING_KEY`, else generated (trust on first use). See [signing](signing.md). |
497
+ | `--remove-empty-lines` | flag | off | generate, validate | Drop blank and whitespace-only lines from emitted bodies. Line numbers no longer match the source; every affected file records `emptyLinesRemoved: true` and the brief says so. |
498
+ | `--strip-comments` | flag | off | generate, validate | Remove comment text from JavaScript/TypeScript bodies. Newlines are kept, so line numbers do not move; files record `inclusionMode: "comments-stripped"`. |
499
+ | `--workspace <a,b>` | list | unset | generate | One snapshot per member under `<outDir>/<name>`, plus `WORKSPACE.json`. Exits 1 if any member failed. See [workspaces](workspace.md). |
500
+ | `--pack <mode>` | string | unset | generate, validate | Also write one full-repository file: `single` (markdown), `xml` or `json`. Unset keeps the `--include-monolith` pair. |
501
+ | `--slice <question>` | string | unset | generate | Narrow the snapshot to what the retrieval planner picks for this question. Records a `slice` block and `skipped[]` reasons `not-selected`. See [slices](slice.md). |
502
+ | `--slice-tokens <n>` | count | `30000` | generate | The token budget for `--slice`. |
503
+ | `--delta <path>` | path | unset | generate | Narrow the snapshot to what changed since a base snapshot, plus the one-hop importers. Unchanged files are recorded in `delta.unchanged` and `skipped[]`. See [delta](delta.md). |
504
+ | `--include-diffs` | flag | off | generate | Also write `reports/diff.md`: the unstaged and staged diff, with the commands, bounded to 200 KiB. Absent when the tree is clean. |
505
+ | `--include-logs <n>` | count | unset | generate | Also write `reports/log.md`: the last n commits (1–500), one line each. |
506
+ | `--token-tree` | flag | off | generate | Also write `reports/tokens.md`: per-file token estimates sorted by cost, with directory rollups. |
470
507
  | `--max-parts <n>` | count | unset | generate, validate | **Experimental.** Grow the per-part budget until the snapshot has at most `n` parts, so the file count stays fixed as the code grows. The budget it settles on is recorded in `manifest.budgets.maxPartBytes` and may exceed `--max-part-bytes`; a warning says so. `0` is a usage error. |
471
508
  | `--attachment-slots <n>` | count | `10` | generate | Files the destination accepts in one message; `0` means no limit. `PROMPT.md` plans the delivery against it — which parts to attach, what to leave behind, and the `--max-part-bytes` value that would merge them. It changes no part and no file, so it is recorded in the manifest but is not part of the config digest. |
472
509
  | `--include-monolith` | flag | off | generate, validate, setup | Also write `full-codebase.json` and `full-codebase.md`. |
@@ -13,6 +13,7 @@ Nothing here is required: with no config at all the tool runs on the defaults be
13
13
  - [Classification: layers and kinds](#classification-layers-and-kinds)
14
14
  - [Sizing: byte budgets](#sizing-byte-budgets)
15
15
  - [Artifacts: what gets written](#artifacts-what-gets-written)
16
+ - [Transforms and opt-in reports](#transforms-and-opt-in-reports)
16
17
  - [Secrets: redaction](#secrets-redaction)
17
18
  - [Output directories](#output-directories)
18
19
  - [Hooks](#hooks)
@@ -247,6 +248,32 @@ on leaves links pointing at files that were never written. Both are allowed.
247
248
 
248
249
  The `.codebase-snapshot` ownership marker is always written, whatever the selection.
249
250
 
251
+ ## Transforms and opt-in reports
252
+
253
+ Four settings change the *bytes* of a body or add a report. All four are part of the config digest,
254
+ so `validate` must be given the same flags when it compares a snapshot with a configuration:
255
+ without them it reports a configuration mismatch, which is the point of recording them.
256
+
257
+ ```js
258
+ export default {
259
+ stripComments: false, // remove comment text from JS/TS; newlines kept, so line numbers still match
260
+ removeEmptyLines: false, // drop blank lines; line numbers move, and each file records emptyLinesRemoved
261
+ logCount: 20, // commits in reports/log.md when `log` is on (1–500)
262
+ pack: null, // "single" | "xml" | "json": one full-repository file
263
+ artifacts: {
264
+ diff: false, // reports/diff.md, bounded to 200 KiB; absent when the tree is clean
265
+ log: false, // reports/log.md
266
+ tokenTree: false, // reports/tokens.md
267
+ sarif: false, // reports/audit.sarif
268
+ },
269
+ };
270
+ ```
271
+
272
+ `removeEmptyLines` is the one transform that moves line numbers. Every affected file is marked
273
+ `emptyLinesRemoved: true` in the manifest, and `PROMPT.md` says how many files were renumbered and
274
+ that a line number cited from one of them does not point at the source. Prefer `stripComments` when
275
+ a reader will quote line numbers.
276
+
250
277
  ## Delivering it: how many files the destination takes
251
278
 
252
279
  ```js
package/docs/delta.md ADDED
@@ -0,0 +1,50 @@
1
+ # Delta packs
2
+
3
+ ```bash
4
+ codebase-snapshot generate --delta ./snapshot --out-dir ./snapshot-delta
5
+ ```
6
+
7
+ A delta pack is the snapshot you would want for "what changed?" — the files that changed since a
8
+ base snapshot, plus the files that import them, and nothing else. The rest are accounted for rather
9
+ than dropped.
10
+
11
+ ## What it does
12
+
13
+ 1. Reads the base `manifest.json` (a directory or the file itself).
14
+ 2. Hashes the current tree as usual and compares **content hashes**, not mtimes.
15
+ 3. Keeps: added files, changed files, and the **one-hop importers** of those files.
16
+ 4. Drops: unchanged files. Each goes into `skipped[]` with reason `unchanged` and into
17
+ `delta.unchanged`, so the accounting rule still holds — nothing disappears without a record.
18
+ 5. Records the comparison in the manifest:
19
+
20
+ ```json
21
+ "delta": {
22
+ "base": { "path": "/repo/snapshot/manifest.json", "fingerprint": "…", "generatedAt": "…" },
23
+ "added": ["src/new.ts"],
24
+ "changed": ["src/user.ts"],
25
+ "removed": ["src/gone.ts"],
26
+ "unchanged": ["package.json", "src/unrelated.ts"]
27
+ }
28
+ ```
29
+
30
+ The brief (`PROMPT.md`) states the delta at the top, with the counts and the note that unchanged
31
+ files are not in the parts — so a model reading it cannot describe them from memory.
32
+
33
+ ## Why it matters
34
+
35
+ A full snapshot of a large repository is mostly text the reader already has. A delta is small
36
+ enough to send on every turn, and it carries the two things a change review actually needs: the
37
+ new bytes and the callers that might break.
38
+
39
+ ## Limits, stated
40
+
41
+ | Limit | Value | Why |
42
+ | :--- | :--- | :--- |
43
+ | Base format | a manifest from this tool | The shape is checked; a foreign JSON is refused with the reason. |
44
+ | Importer closure | one hop | The same rule as `--with-dependents`; deeper closure is a graph query, not a pack. |
45
+ | Removed files | listed, not shown | The bytes are gone; the manifest can only say which paths disappeared. |
46
+ | Unchanged files | listed in `delta.unchanged` and `skipped[]` | Dropping them silently would break the accounting contract. |
47
+
48
+ A delta against a base from a different repository is not an error: nothing matches, so every file
49
+ is "added". The base fingerprint is recorded, and comparing it with the current one is the reader's
50
+ job.
package/docs/deploy.md ADDED
@@ -0,0 +1,75 @@
1
+ # Run it as a service
2
+
3
+ Two shapes: a container for an MCP endpoint and the browser page, and a plain process under systemd
4
+ for a CLI that runs on a schedule.
5
+
6
+ ## Container
7
+
8
+ ```bash
9
+ npm run build # the image copies dist/, it does not build it
10
+ docker build -t codebase-snapshot .
11
+ docker run --rm -p 4788:4788 -e SNAPSHOT_TOKEN=change-me codebase-snapshot
12
+ ```
13
+
14
+ The entrypoint **refuses to start without `SNAPSHOT_TOKEN`**: `serve` on `0.0.0.0` without a token
15
+ would expose the repository to anything that can reach the port. Inside the container the process
16
+ runs as the `node` user, listens on `PORT` (default 4788), and serves:
17
+
18
+ | Path | What |
19
+ | :--- | :--- |
20
+ | `GET /` | The page: generate, preview, download the zip |
21
+ | `GET /healthz` | `{ ok, name, tools }` |
22
+ | `POST /mcp` | MCP over Streamable HTTP |
23
+ | `GET /sse`, `POST /messages` | Legacy SSE transport |
24
+
25
+ ### Compose
26
+
27
+ ```yaml
28
+ services:
29
+ snapshot:
30
+ build: .
31
+ ports: ["4788:4788"]
32
+ environment:
33
+ SNAPSHOT_TOKEN: change-me
34
+ PORT: "4788"
35
+ read_only: true
36
+ tmpfs: ["/tmp"]
37
+ security_opt: ["no-new-privileges:true"]
38
+ ```
39
+
40
+ The page generates **into the container**, which is ephemeral. Mount a volume and paste that path
41
+ into the page if the snapshot needs to outlive the container:
42
+
43
+ ```yaml
44
+ volumes: ["snapshot-data:/data"]
45
+ ```
46
+
47
+ ### What the image is not
48
+
49
+ - It is not published to a registry by this repository. Build it where you need it; a public image
50
+ would need a signing and tagging policy that does not exist yet.
51
+ - It is not a sandbox. It reads the repository you give it with the permissions of the `node` user.
52
+
53
+ ## systemd, for a schedule
54
+
55
+ ```ini
56
+ [Unit]
57
+ Description=codebase-snapshot refresh
58
+ After=network.target
59
+
60
+ [Service]
61
+ Type=oneshot
62
+ User=deploy
63
+ WorkingDirectory=/srv/app
64
+ ExecStart=/usr/bin/npx -y codebase-snapshot@0.14.0 generate --cwd /srv/app
65
+ ExecStartPost=/usr/bin/npx -y codebase-snapshot@0.14.0 validate --cwd /srv/app
66
+ ```
67
+
68
+ ```ini
69
+ [Timer]
70
+ OnCalendar=hourly
71
+ Persistent=true
72
+ ```
73
+
74
+ `validate` exits 1 on drift, so a unit that runs it after `generate` fails when the regeneration did
75
+ not produce a current snapshot — a gate, not a log line.