@hexagen-monaco/sync 0.12.0 → 0.13.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 (99) hide show
  1. package/README.md +429 -0
  2. package/dist/cli.js +6253 -439
  3. package/dist/cli.js.map +1 -1
  4. package/dist/commands/arch/edit.d.ts.map +1 -1
  5. package/dist/commands/contract/evaluate.d.ts +45 -0
  6. package/dist/commands/contract/evaluate.d.ts.map +1 -0
  7. package/dist/commands/contract/index.d.ts +24 -0
  8. package/dist/commands/contract/index.d.ts.map +1 -0
  9. package/dist/commands/evidence/check.d.ts +31 -0
  10. package/dist/commands/evidence/check.d.ts.map +1 -0
  11. package/dist/commands/evidence/index.d.ts +3 -0
  12. package/dist/commands/evidence/index.d.ts.map +1 -0
  13. package/dist/commands/evidence/pack.d.ts +41 -0
  14. package/dist/commands/evidence/pack.d.ts.map +1 -0
  15. package/dist/commands/grant/build.d.ts +53 -0
  16. package/dist/commands/grant/build.d.ts.map +1 -0
  17. package/dist/commands/grant/canonical.d.ts +27 -0
  18. package/dist/commands/grant/canonical.d.ts.map +1 -0
  19. package/dist/commands/grant/check.d.ts +26 -0
  20. package/dist/commands/grant/check.d.ts.map +1 -0
  21. package/dist/commands/grant/duration.d.ts +8 -0
  22. package/dist/commands/grant/duration.d.ts.map +1 -0
  23. package/dist/commands/grant/index.d.ts +2 -0
  24. package/dist/commands/grant/index.d.ts.map +1 -0
  25. package/dist/commands/grant/issue.d.ts +23 -0
  26. package/dist/commands/grant/issue.d.ts.map +1 -0
  27. package/dist/commands/grant/key-init.d.ts +16 -0
  28. package/dist/commands/grant/key-init.d.ts.map +1 -0
  29. package/dist/commands/grant/revoke.d.ts +35 -0
  30. package/dist/commands/grant/revoke.d.ts.map +1 -0
  31. package/dist/commands/grant/show.d.ts +18 -0
  32. package/dist/commands/grant/show.d.ts.map +1 -0
  33. package/dist/commands/grant/sign.d.ts +3 -0
  34. package/dist/commands/grant/sign.d.ts.map +1 -0
  35. package/dist/commands/grant/signing-key.d.ts +22 -0
  36. package/dist/commands/grant/signing-key.d.ts.map +1 -0
  37. package/dist/commands/grant/verify.d.ts +45 -0
  38. package/dist/commands/grant/verify.d.ts.map +1 -0
  39. package/dist/commands/grant/workspace.d.ts +11 -0
  40. package/dist/commands/grant/workspace.d.ts.map +1 -0
  41. package/dist/commands/observe/imports/jsonc.d.ts +6 -0
  42. package/dist/commands/observe/imports/jsonc.d.ts.map +1 -0
  43. package/dist/commands/observe/imports/pass.d.ts +45 -0
  44. package/dist/commands/observe/imports/pass.d.ts.map +1 -0
  45. package/dist/commands/observe/imports/resolve.d.ts +44 -0
  46. package/dist/commands/observe/imports/resolve.d.ts.map +1 -0
  47. package/dist/commands/observe/imports/safe-read.d.ts +30 -0
  48. package/dist/commands/observe/imports/safe-read.d.ts.map +1 -0
  49. package/dist/commands/observe/imports/scan.d.ts +59 -0
  50. package/dist/commands/observe/imports/scan.d.ts.map +1 -0
  51. package/dist/commands/observe/index.d.ts +59 -0
  52. package/dist/commands/observe/index.d.ts.map +1 -0
  53. package/dist/commands/observe/same-path.d.ts +9 -0
  54. package/dist/commands/observe/same-path.d.ts.map +1 -0
  55. package/dist/commands/observe/walk.d.ts +29 -0
  56. package/dist/commands/observe/walk.d.ts.map +1 -0
  57. package/dist/commands/shared/brownfield-sidecar.d.ts +68 -0
  58. package/dist/commands/shared/brownfield-sidecar.d.ts.map +1 -0
  59. package/dist/commands/shared/git-exclude.d.ts +36 -0
  60. package/dist/commands/shared/git-exclude.d.ts.map +1 -0
  61. package/dist/commands/shared/glob.d.ts +29 -0
  62. package/dist/commands/shared/glob.d.ts.map +1 -0
  63. package/dist/commands/shared/ignore.d.ts +32 -0
  64. package/dist/commands/shared/ignore.d.ts.map +1 -0
  65. package/dist/commands/shared/sidecar-out.d.ts +6 -0
  66. package/dist/commands/shared/sidecar-out.d.ts.map +1 -0
  67. package/dist/commands/shared/sidecar-write.d.ts +2 -0
  68. package/dist/commands/shared/sidecar-write.d.ts.map +1 -0
  69. package/dist/commands/slice/index.d.ts +23 -0
  70. package/dist/commands/slice/index.d.ts.map +1 -0
  71. package/dist/commands/workbook/allow-list.d.ts +27 -0
  72. package/dist/commands/workbook/allow-list.d.ts.map +1 -0
  73. package/dist/commands/workbook/export.d.ts +41 -0
  74. package/dist/commands/workbook/export.d.ts.map +1 -0
  75. package/dist/commands/workbook/index.d.ts +3 -0
  76. package/dist/commands/workbook/index.d.ts.map +1 -0
  77. package/dist/commands/workbook/zip-read.d.ts +7 -0
  78. package/dist/commands/workbook/zip-read.d.ts.map +1 -0
  79. package/dist/fs-utils.d.ts.map +1 -1
  80. package/dist/generators/barrels/recursive.d.ts.map +1 -1
  81. package/dist/generators/root-file-templates.d.ts +5 -4
  82. package/dist/generators/root-file-templates.d.ts.map +1 -1
  83. package/dist/generators/root-files.d.ts.map +1 -1
  84. package/dist/index.js +355 -75
  85. package/dist/index.js.map +1 -1
  86. package/dist/templates/agents-md/findings/0001-session-log-grows-unbounded.md +35 -0
  87. package/dist/templates/ci-github-actions/findings/0001-ci-runners-have-no-zsh.md +34 -0
  88. package/dist/templates/orchestration/README.md +88 -0
  89. package/dist/templates/orchestration/files/.agents/skills/orchestrate-wave/SKILL.md +511 -0
  90. package/dist/templates/orchestration/files/.agents/skills/orchestrate-wave/references/briefs.md +178 -0
  91. package/dist/templates/orchestration/files/.agents/skills/orchestrate-wave/references/rationale.md +520 -0
  92. package/dist/templates/orchestration/files/.agents/skills/orchestrate-wave/scripts/wave-event.sh +17 -0
  93. package/dist/templates/orchestration/files/.github/workflows/gate.yml +66 -0
  94. package/dist/templates/orchestration/findings/0001-relative-runbook-links-escape-the-skill.md +48 -0
  95. package/dist/templates/orchestration/findings/0002-interpolation-hazard-in-graphql-line.md +46 -0
  96. package/dist/templates/orchestration/manifest.json +41 -0
  97. package/dist/types/manifest/monorepo.d.ts +4 -0
  98. package/dist/types/manifest/monorepo.d.ts.map +1 -1
  99. package/package.json +1 -1
package/README.md CHANGED
@@ -52,6 +52,435 @@ npx hexagen sync
52
52
 
53
53
  Consult `npx hexagen arch --help` for the full list of manifest operations.
54
54
 
55
+ ### `hexagen grant issue`
56
+
57
+ Mints a signed Grant — the file `hexagen_accept_transaction` trusts as its
58
+ `grant` argument (see `docs/kernel/GRANT.md`). Local, no server:
59
+
60
+ ```bash
61
+ npx hexagen grant issue \
62
+ --principal martin \
63
+ --agent lane-ow3b \
64
+ --paths .architecture/,packages/billing/ \
65
+ --tools hexagen_accept_transaction,hexagen_create_port \
66
+ --mode write \
67
+ --expires-in 4h \
68
+ --out .hexagen/grants/<id>.json
69
+ ```
70
+
71
+ Signs with an HMAC-SHA256 key. Which key, and where it lives, depends on the
72
+ mode, and `hexagen_accept_transaction`'s server resolves it the same way
73
+ (one shared resolver), in this order:
74
+
75
+ 1. `--key-file <path>`
76
+ 2. `HEXAGEN_GRANT_KEY_FILE`
77
+ 3. **Repo mode** (`.architecture/manifest.yaml` exists): `.hexagen/grant-signing.key`,
78
+ created on first use if missing, with a `.gitignore` entry added for it.
79
+ Never commit it. **Brownfield** (no manifest, a client repo you do not
80
+ control): `~/.hexagen/keys/<engagement>.key`, never inside the repo. There
81
+ is no fallback to an in-repo key.
82
+
83
+ The key must be a full 32-byte hex value, so a hand-edited or truncated key
84
+ file is rejected by the issuer and denied by the server rather than silently
85
+ used. Keys are shown by path and
86
+ fingerprint (first 16 hex chars of the SHA-256 of the key bytes), never
87
+ printed.
88
+
89
+ #### Brownfield: `grant key init` and `--engagement`
90
+
91
+ ```bash
92
+ npx hexagen grant key init --engagement acme-q3 [--key-file <path>]
93
+ ```
94
+
95
+ The only command that mints a key outside repo mode. It writes 32 random bytes
96
+ as hex at mode 0600 in `~/.hexagen/keys/` (0700, tightened if it was loose),
97
+ refuses to overwrite an existing key (exit 1), and accepts engagement ids
98
+ matching `^[A-Za-z0-9._-]{1,64}$` with no `..`. `grant issue` in a repo with no
99
+ manifest never mints and never edits `.gitignore`:
100
+
101
+ - The engagement id is `--engagement <id>`, or the `id` of a valid
102
+ `.hexagen/slice.json`. Without either, exit 2.
103
+ - `--paths` defaults to the slice's paths; every entry must be a valid slice
104
+ path inside the slice, and no slice exclude may sit beneath a directory entry
105
+ (grants carry no excludes, so issue narrower paths; exit 2). `--contexts` is
106
+ refused (exit 2), and the grant omits `contexts`.
107
+ - The root is the git toplevel of the current directory when the discovered
108
+ manifest root lies outside it, so a client repo nested under a directory with
109
+ a manifest is still brownfield. A manifest at or below the toplevel keeps repo
110
+ mode. `--workspace-root` overrides both.
111
+ - `--out` must be a new file under `<root>/.hexagen/` (symlink escapes are
112
+ refused, exit 2; an existing file is never overwritten, exit 1) and is written
113
+ via a temp file plus a hard link.
114
+ - `.hexagen/` is added to `.git/info/exclude`. When a write is pending (the
115
+ `--out` file or an exclude change) the preflight lists it and `--yes` is
116
+ required; otherwise exit 2 without writing. With nothing to write, `--yes`
117
+ is not needed.
118
+ - It prints the workspace root, key path and fingerprint on stderr. If
119
+ `--key-file` points somewhere the server would not look by default, it warns
120
+ with both paths and fingerprints (likewise when `--engagement` differs from the
121
+ slice's id). The grant is validated and signed before `.git/info/exclude` is
122
+ touched. The MCP server takes `--key-file` and
123
+ `--engagement` too, and logs the same line at startup.
124
+
125
+ `--contexts <name[,name...]>` is a monaco-only convenience (repo mode): it
126
+ looks each name up in `manifest.yaml` and expands it to `packages/<name>/`,
127
+ appended to `--paths`.
128
+
129
+ `hexagen_accept_transaction`'s enforcement (`checkMutationAgainstGrant`)
130
+ requires `grant.contexts` to independently name every context a mutation
131
+ targets — a grant's `paths` alone never satisfies that check, and an empty
132
+ `contexts` denies everything. So the CLI also reverse-matches any
133
+ `packages/<name>/` prefix already present in `--paths` against the
134
+ manifest, folding matching names into `grant.contexts` automatically
135
+ (`--contexts` can usually be omitted when `--paths` already names the
136
+ context's package directory, as in the example above). Symmetrically,
137
+ `.architecture/` — the one path every manifest mutation actually writes
138
+ through — is always added to the grant's `paths` whenever it authorizes any
139
+ context, so `--contexts` alone is enough without repeating
140
+ `.architecture/` in `--paths`. This reconciliation only works where a
141
+ manifest exists: a grant that names no context at all (no manifest to
142
+ derive contexts from, and no `--contexts` given) is paths-only in the
143
+ literal sense, but since every manifest-mutation tool's enforcement
144
+ requires a context match, such a grant cannot authorize any of those seven
145
+ tools — it is scoped only for a future, generic Field Kit adapter that
146
+ doesn't check `contexts`, not for `hexagen_accept_transaction` as it exists
147
+ today. Prints the grant JSON to stdout, or to `--out <file>` if given.
148
+
149
+ ### `hexagen grant show` and `hexagen grant check`
150
+
151
+ ```bash
152
+ npx hexagen grant show .hexagen/grants/<id>.json
153
+ npx hexagen grant check .hexagen/grants/<id>.json \
154
+ --tool hexagen_propose_patch --path src/a.ts src/b.ts
155
+ ```
156
+
157
+ Both resolve the verification key exactly as `grant issue` and the MCP server
158
+ do (`--key-file`, then `HEXAGEN_GRANT_KEY_FILE`, then the engagement from
159
+ `.hexagen/slice.json` or `--engagement`, or the in-repo key in repo mode), and
160
+ print the workspace root, the key path and its fingerprint, never the key.
161
+
162
+ - `show` pretty-prints id, principal, agent, contexts (when present), paths,
163
+ tools, mode, `max_files`, `expires_at` and `revoked_at`, the window status,
164
+ and whether the signature verifies. Exit 0 verified, 1 not verified (the
165
+ reason is printed), 2 bad input. A revoked or expired grant with a valid
166
+ signature still exits 0; `check` enforces the window.
167
+ - `check` verifies the signature, then the window, then the tool, the paths
168
+ and `max_files` (`checkWriteAgainstGrant`). It never runs the mode check, so
169
+ a propose-only grant is allowed. In a client repo (no manifest) it also
170
+ denies a path outside the slice or inside its excludes, even if the grant
171
+ allows it, and with no `.hexagen/slice.json` it denies (the slice bounds every
172
+ write). Paths are judged by text only; the MCP propose tool also checks the
173
+ on-disk spelling. `--path` may be repeated or list several paths, and every one is checked.
174
+ The Field Kit form is for client repos; in a repo with a manifest it exits 2.
175
+ The root is the git toplevel unless `--workspace-root` is given. Exit 0
176
+ allow, 1 deny, 2 bad input (including an invalid `.hexagen/slice.json` or
177
+ timestamp, which `show` also rejects). A missing or weak key, or a
178
+ signature made under another key, is a denial; with `--key-file` or
179
+ `--engagement` it names both keys' fingerprints.
180
+ - The monaco form, `grant check <grant-file> <transaction-id>`, is not built:
181
+ pending transactions live in the MCP server's memory. It exits 2.
182
+ - `revoke` sets `revoked_at` and re-signs the grant:
183
+ `hexagen grant revoke .hexagen/grants/<id>.json [--at <iso>] --yes`. It
184
+ verifies the grant under the resolved key first and refuses one that does not
185
+ verify (exit 1). It previews the write and needs `--yes`; the file is replaced
186
+ by a temp file and an atomic rename; in a client repo it must be under
187
+ `.hexagen/`. Revoking again is a no-op ("already revoked at ..."), except an
188
+ earlier `--at` moves the date earlier. Hand-editing `revoked_at` breaks the
189
+ signature, so `check` denies the grant as a signature failure; `revoke`
190
+ re-signs it, so `check` reports `grant_revoked`. Deleting the engagement key
191
+ revokes every grant in that engagement (an emergency stop).
192
+ A future `--at` schedules the revocation (the grant stays valid until then,
193
+ and the preflight warns); a value at or after `expires_at` has no effect. In
194
+ a repo with a manifest it re-signs with the in-repo key.
195
+ Concurrent revokes are serialised by `<grant>.lock` (a held lock exits 2, never
196
+ auto-broken), and `--key-file`/`--engagement` pointing at a key other than the
197
+ server default produces a warning naming both keys.
198
+
199
+ ### `hexagen observe`
200
+
201
+ A read-only scan of a repo you do not control. It reports what is already
202
+ there, in the repo's own names, and never runs `adopt`, `bootstrap`, `sync`
203
+ or `hexagen-lint`:
204
+
205
+ ```bash
206
+ npx hexagen observe --root ../client-repo # JSON to stdout
207
+ npx hexagen observe --root ../client-repo \
208
+ --out .hexagen/observed.json --yes # write the file
209
+ npx hexagen observe --dont-touch src/legacy/ vendor-patches/ # report-only
210
+ ```
211
+
212
+ - `--root <dir>` is used exactly as given (default: cwd); it is never searched
213
+ upward, and it must be the repo top level (exit 2 otherwise, naming the
214
+ top level git reports). The directory must be a git checkout with at least one
215
+ commit, whose `HEAD` is recorded in `repo.commit`. Credentials, query and fragment in the `origin` URL are stripped.
216
+ - `--out <file>` must resolve under `<root>/.hexagen/` (symlinks included);
217
+ anything else exits 2, as does a `.hexagen` that is a regular file. It
218
+ prints a `will write:` line for `observed.json` and one for the exclude file,
219
+ and writes only with `--yes` (without it, exit 2). The report is written as a
220
+ temp file plus rename. `.hexagen/` is then kept out of `git status` by
221
+ appending it, if absent, to the file `git rev-parse --git-path info/exclude`
222
+ prints (correct in linked worktrees and submodules); the tracked
223
+ `.gitignore` is never edited, and `git add -f` can still stage the
224
+ directory. If that update fails, the command exits 2 before writing the
225
+ report.
226
+ - `--max-files <n>` (default 50000) and `--max-ms <n>` (default 30000) cap the
227
+ walk. A tripped cap marks `packages`, `languages`, `build` and `generated` as
228
+ `collected: false` with the reason, and sets `limits.truncated`.
229
+ - The output validates against `ObservedReport` (`docs/kernel/observed.schema.json`)
230
+ and has no `type`, layer, plane or context key.
231
+ - `--max-import-files <n>` (default 20000: the pass refuses to run when there
232
+ are more than `n` JS/TS files), `--max-import-bytes <n>` (default 268435456)
233
+ and `--max-import-ms <n>` (default 30000) cap the import pass. It has its own
234
+ clock, started when it begins, and runs after `generated` is collected, so it
235
+ cannot use up the time `--max-ms` gives the walk and `generated`. A tripped
236
+ cap sets `edges` and `unresolved` to `collected: false` with the reason, and
237
+ sets `limits.truncated`; the other sections keep what they collected. If the
238
+ walk itself was truncated, the pass does not run and both sections carry the
239
+ walk's reason.
240
+
241
+ What it reads:
242
+
243
+ - **Walk.** Skips `node_modules`, `vendor`, `bower_components`, `jspm_packages`,
244
+ `.yarn`, `.git`, `.hg`, `.svn` and `.hexagen`,
245
+ does not follow symlinks, and honours the root `.gitignore` and nested
246
+ `.gitignore` files (not `info/exclude` or `core.excludesFile`). A path that
247
+ fails the slice-path rules (a backslash or control character) is skipped
248
+ with a note.
249
+ - **Import pass (`edges`, `unresolved`).** A lexical scan of `.ts .tsx .mts
250
+ .cts .js .jsx .mjs .cjs` files from the same walk. It is a tokenizer, not a
251
+ parser, and never uses ts-morph or the TypeScript compiler API. It reads
252
+ `import … from 'x'`, `import 'x'`, `export … from 'x'`, `import('x')`,
253
+ `require('x')` (also `require?.('x')` and `(require)('x')`) and
254
+ `import x = require('x')`; comments, strings and
255
+ template literals (including `${…}` contents) are skipped. Source files and
256
+ tsconfigs are opened without following a symlink in the last path component
257
+ (`O_NOFOLLOW`; on Windows an `lstat` check instead), and the size limit and the
258
+ read both use the opened handle. Bytes read from tsconfigs count toward
259
+ `--max-import-bytes`, and the time cap is checked while they load. Only files
260
+ up to
261
+ 1 MiB are read. A larger or unreadable file is skipped, noted in
262
+ `limits.reasons` (the notes are capped at 50), and given its own `unresolved`
263
+ row, so its missing edges never read as clean. The scan is linear in the file
264
+ size. Known limits: JSX text with an apostrophe opens a string that ends at the
265
+ line end; a regex literal right after `)` (`if (x) /re/.test(y)`) is read as
266
+ division, so a specifier-shaped string inside it can appear as a phantom
267
+ import; a wrong regex or string guess can swallow a backtick and misread the
268
+ template state until the next one; and a `/` after `>` starts a regex (so
269
+ `x => /re/.test(x)` works) but after `<` it does not (`</p>`); a postfix
270
+ `++`/`--` followed by an unbalanced `[` in a divisor string can make the line
271
+ guard hide a later quote-bearing regex on the same line
272
+ (`x = i++ / "["; y = /it's/.test(s);`); a block statement after a
273
+ semicolon-less non-literal `require(x)` (ASI only) is dropped. A class method named `require` or `import`
274
+ (`require(id) {}`) is not reported.
275
+ - **Resolution order, per specifier:** (1) a relative specifier (`./`, `../`)
276
+ against the walked files: the exact file, then `.js`→`.ts/.tsx`
277
+ (`.jsx`→`.tsx`, `.mjs`→`.mts`, `.cjs`→`.cts`), then each extension, then
278
+ `/index.*` (a trailing slash, `.` and `..` name a directory and match only
279
+ its `index.*`; `C:/x` and `C:\x` are `outside-repo`; `.\x` is `not-found`); (2) a workspace package name, exactly as declared including
280
+ scope, mapped to that package's root (`"."` for the root package); (3) the
281
+ nearest `tsconfig.json` at or above the file, within the repo:
282
+ `compilerOptions.paths` (longest matching prefix wins) and `baseUrl`,
283
+ following `extends` only to repo-relative files, at most 5 levels past the
284
+ nearest file, with a note on a cycle, an unreadable or over-1-MiB file or an
285
+ `extends` that leaves the repo; for an `extends` array only the last entry
286
+ is followed (noted), not TypeScript's merge of all of them;
287
+ (4) a `#` specifier (package `imports`). A tsconfig further up the tree is
288
+ not consulted when the nearest one has no `paths`/`baseUrl`.
289
+ - **`edges[]`** are `{from, to, specifier}` with `to` a file or a package
290
+ root, deduplicated.
291
+ - **`unresolved[]`** are `{from, specifier, reason}`. Reasons:
292
+ `not-found` (a relative or alias target that is not in the walk),
293
+ `exports-subpath` (`@scope/pkg/sub`, when no tsconfig `paths` alias matches it;
294
+ `exports` maps are not read),
295
+ `non-literal` (`import(x)`, `require(a + b)`, and an escaped or empty static
296
+ specifier such as `import '\u0061'`; recorded as `import(<non-literal>)`,
297
+ `require(<non-literal>)`, `import <non-literal>` or `export <non-literal>`), `outside-repo` (the
298
+ target leaves the repo root, or is an absolute or drive path),
299
+ `package-imports` (a `#` specifier) and `not-scanned` (the file itself was
300
+ skipped: the specifier reads `<not scanned: larger than 1 MiB>` or
301
+ `<not scanned: unreadable>`).
302
+ - **External specifiers** (node builtins, `node:` URLs, dependencies) are
303
+ neither edges nor unresolved. They are counted in one note in
304
+ `limits.reasons`.
305
+ - **`edgesComplete`.** `edges.unreadLanguages` lists the file extension of
306
+ every counted language the pass does not read (`go`, `py`, `rs`, `vue`, …).
307
+ These are extensions, whereas `languages[].name` holds display names
308
+ (`Go`, `Python`); a consumer must join on the extension, not the name.
309
+ A non-empty list makes `edgesComplete(edges)` false, so an empty edge list
310
+ for such a repo never reads as a clean bill. Consumers must call
311
+ `edgesComplete`, never read `collected` alone.
312
+ - **Metadata files** (`package.json`, `pnpm-workspace.yaml`, `.gitattributes`,
313
+ `CODEOWNERS`) are never read through a symlink; a link is noted and skipped.
314
+ - **Packages.** Every `package.json` the walk reaches is a package: a repo with no
315
+ workspaces has one at `"."`. Names are exactly as written, scope included; a
316
+ manifest with no `name` is reported under its directory path (the root
317
+ directory's basename for `"."`). Workspace `!` exclusions from `workspaces`
318
+ (array or `{packages}`) and `pnpm-workspace.yaml` are honoured, with full glob
319
+ syntax (`*`, `**`, `?`, `[..]`, `{a,b}`); include globs add nothing the walk
320
+ has not already found.
321
+ - **Languages** by extension (file counts), **build markers** (`package.json`,
322
+ `nx.json`, `turbo.json`, `go.mod`, `Cargo.toml`, `pom.xml`, `build.gradle*`,
323
+ `pyproject.toml`, `Makefile`).
324
+ - **Generated** paths: root `.gitattributes` `linguist-generated`, `@generated`
325
+ in the first 5 lines of a source file, and gitignored `dist/` or `build/`.
326
+ - **dontTouch**: `--dont-touch` plus literal anchored `CODEOWNERS` paths
327
+ (`.github/CODEOWNERS`, then `CODEOWNERS`, then `docs/CODEOWNERS`; the first found is read), reported with their
328
+ owners and never turned into a rule. Glob and unanchored CODEOWNERS patterns
329
+ cannot be a path, so they are skipped and noted in `limits.reasons`.
330
+ `limits.reasons` may carry `note:` lines (an unreadable directory, an
331
+ unnamed package, a pattern that does not compile) while `truncated` is
332
+ `false`; only a tripped cap sets `truncated`.
333
+
334
+ ### `hexagen slice` and `hexagen contract`
335
+
336
+ Bind work to part of a repo you do not control, then gate it. Both read the
337
+ `.hexagen/observed.json` that `hexagen observe` wrote, so run `observe` first
338
+ (and again after the repo moves). Neither runs `adopt`, `bootstrap`, `sync` or
339
+ `hexagen-lint`, and neither needs a manifest. Every command takes
340
+ `--root <dir>` (default cwd, never searched upward, must be the repo top level).
341
+
342
+ ```bash
343
+ npx hexagen observe --out .hexagen/observed.json --yes
344
+ npx hexagen slice init --path src/billing/ --exclude src/billing/generated/ --yes
345
+ npx hexagen slice check # drift report
346
+ npx hexagen contract propose # candidate rules; writes nothing
347
+ npx hexagen contract add-rule --kind forbid --from src/billing/ --to src/auth/ --yes
348
+ npx hexagen contract check # gate; exit 1 on a new violation
349
+ npx hexagen contract check --baseline --yes # record today's violations
350
+ ```
351
+
352
+ **`slice`** writes `.hexagen/slice.json`.
353
+
354
+ - `slice init --path <p>… [--exclude <p>…] [--id <id>] [--by <who>]`. Each entry
355
+ must pass the slice-path rules (a trailing `/` is a directory prefix, anything
356
+ else is one file). `--id` matches `[A-Za-z0-9._-]{1,64}` without `..` and
357
+ defaults to a random slug. `repo.commit` is `HEAD` and `repo.remote` is
358
+ `origin` with credentials stripped. `createdBy` is `--by`, else
359
+ `git config user.email`. It never overwrites an existing slice.
360
+ - `slice show` prints the slice.
361
+ - `slice check` reports drift (exit 1): a `paths` entry that matches no file; a
362
+ file under the slice changed between `repo.commit` and `HEAD` (`excludes`
363
+ applied; uncommitted working-tree edits are not drift, only commits since
364
+ `repo.commit` are); and incomplete edges (`edgesComplete` is false: an unread
365
+ language, or edges not collected), which are never clean. An `excludes` entry
366
+ that matches no file only gets a `note:` line. Observed edges that cross the
367
+ slice boundary are always listed (`leaves` and `enters`, with counts; a
368
+ package-root target is judged with excludes first, under both spellings) but
369
+ fail the check only with `--closed`. `--strict` is described below.
370
+
371
+ **`contract`** writes `.hexagen/contract.json`, whose `sliceId` comes from
372
+ `slice.json`.
373
+
374
+ - `contract propose` prints each pair of slice `paths` entries joined by an
375
+ in-slice edge (both ends inside the slice, different entries) as a candidate
376
+ `forbid` rule. It writes nothing.
377
+ - `contract add-rule --kind forbid|allow-only --from <prefix> --to <prefix>
378
+ [--severity error|warn] [--id <id>]` appends a rule (`error` by default). The
379
+ built-in id `unresolved-import` and duplicate ids are refused.
380
+ - `contract show` prints the contract.
381
+ - `contract check [--baseline]` evaluates the rules against the observed edges
382
+ whose `from` is inside the slice. A `forbid` rule fails on an edge from its
383
+ `from` prefix to its `to` prefix. An `allow-only` rule fails on an edge from
384
+ its `from` prefix to anywhere that is neither its `to` prefix nor its own
385
+ `from` prefix. A `warn` rule is printed but does not fail the check. The
386
+ built-in rule `unresolved-import` fails on **every** `unresolved` row whose
387
+ `from` is in the slice (including `not-scanned`, `package-imports` and
388
+ `non-literal`) and on every in-slice file whose extension is in
389
+ `edges.unreadLanguages` (`go`, `py`, … joined on the extension, never on the
390
+ display name), so a green result never means "the pass could not see the
391
+ imports". If edges were not collected at all the check cannot be clean (exit
392
+ code 1) and cannot be baselined. `--baseline` writes the current failing violations
393
+ to `knownViolations` (keeping any `reason`/`expires` already there) and needs
394
+ `--yes`; without it, any violation not in the baseline names its rule, file
395
+ and specifier and exits 1. A known violation is matched on rule, file and
396
+ specifier, and an entry whose `expires` date has passed (inclusive to the end
397
+ of that UTC day) no longer hides its violation. A root-package target (`.`)
398
+ is never inside a `to` prefix, so it always violates an `allow-only` rule.
399
+
400
+ **Concurrent edits.** `add-rule` and `check --baseline` (with `--yes`) hold
401
+ `.hexagen/contract.json.lock`, created exclusively with the process id inside,
402
+ while they read and rewrite `contract.json`, and remove it when they finish. If
403
+ the lock is already there they exit 2 ("another contract command is running").
404
+ A lock left behind by a killed command is never broken automatically: delete
405
+ the file yourself. A `knownViolations[].expires` that is not a real calendar
406
+ date is refused at load (exit 2).
407
+
408
+ **Writes.** `slice init`, `contract add-rule` and `contract check --baseline`
409
+ print a `will write:` line for each file, including the git exclude file when
410
+ `.hexagen/` is not yet in it, and write nothing without `--yes` (exit 2). The
411
+ exclude is updated first (a failure stops the command), then the file is written
412
+ as a temp file plus a link (a new file, never replacing one) or a rename (an
413
+ update of `contract.json`). The client's `.gitignore` is never edited, and
414
+ `git add -f` can still stage `.hexagen/`.
415
+
416
+ **Stale inputs.** `observed.json` must exist and its `repo.commit` must be the
417
+ slice's `repo.commit` or a descendant of it. If it was read at a commit other
418
+ than `HEAD` the commands warn, and with `--strict` fail.
419
+
420
+ **Exit codes** (`slice check`, `contract check`, `contract propose`):
421
+
422
+ | Code | Meaning |
423
+ | ---- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
424
+ | 0 | Clean (`slice check`, `contract check`), or the command succeeded |
425
+ | 1 | Drift (`slice check`) or a violation not in the baseline, or incomplete edges (`contract check`) |
426
+ | 2 | Bad input or refused: a missing or invalid file, a bad path or id, a refused overwrite, no `--yes`, a slice commit not in the repository, a stale `observed.json`, `--strict` and a different HEAD |
427
+
428
+ `init`, `add-rule`, `show` and `--baseline` return 0 on success and 2 otherwise.
429
+
430
+ ---
431
+
432
+ ### `hexagen workbook export`
433
+
434
+ Two separate modes:
435
+
436
+ - `--out` creates one new file under `.hexagen/` in the working tree. It changes
437
+ nothing else.
438
+ - `--stage` changes the git index and nothing else: it writes no file and
439
+ never commits.
440
+
441
+ ```bash
442
+ # the bundle: one new file under .hexagen/, never overwritten
443
+ npx hexagen workbook export --out .hexagen/workbook.zip [--root <dir>] [--key-file <path>] [--engagement <id>]
444
+
445
+ # the only way anything from .hexagen/ reaches the client's history (BW-D1)
446
+ npx hexagen workbook export --stage .hexagen/slice.json .hexagen/contract.json # prints the diff, stages nothing
447
+ npx hexagen workbook export --stage .hexagen/slice.json .hexagen/contract.json --yes # git add -f on exactly those files
448
+ ```
449
+
450
+ **Bundle.** `bundle.json` (the index, with an HMAC from the engagement key,
451
+ resolved the same way as `grant` and `evidence pack`), `observed.json`,
452
+ `slice.json`, `contract.json` (the last two optional; `slice.json` is required),
453
+ `grants/<id>.json` (every grant in `.hexagen/grants/`, byte for byte, each
454
+ signature verified first), `proposals/<id>.patch` and `<id>.json`, the packed
455
+ evidence (`evidence/trace.jsonl`, `evidence/verdicts.json`, with the denials in
456
+ `verdicts.json`) and `tip.json`. Export runs the `evidence pack` logic, so a
457
+ broken chain, a truncated tail or a bad grant fails the export (exit 1, nothing
458
+ written) and the anchored tip advances as it does for `evidence pack`, even when
459
+ the final bundle write then fails.
460
+
461
+ **Allow-list.** The bundle is built from named files only; nothing else in
462
+ `.hexagen/` is read. A key or env file is never included: `grant-signing.key`,
463
+ any `*.key`, anything under `keys/` or `~/.hexagen/keys/`, any `.env*`. The
464
+ check runs on the source path and again on the bundle path. A key or env file
465
+ found inside `.hexagen/grants/` or `.hexagen/proposals/` refuses the whole
466
+ export (exit 2); a symlink there is refused too.
467
+
468
+ **`--out`** must resolve under `<root>/.hexagen/`, not under `evidence/`, and must
469
+ not exist; the bundle is written to a temporary file and hard-linked.
470
+
471
+ **`--stage`** accepts only allow-listed `.hexagen/` files (`observed.json`,
472
+ `slice.json`, `contract.json`, `grants/*.json`, `proposals/*.patch|json`,
473
+ `evidence/trace.jsonl`, `evidence/tip.json`). Without `--yes` it prints the
474
+ unified diff and stages nothing. With `--yes` it runs `git add -f` on exactly
475
+ those files; it never commits. One refused path (a key, a file off the list, a
476
+ path outside `.hexagen/`, a symlink) refuses the whole call and stages nothing.
477
+
478
+ | Code | Meaning |
479
+ | ---- | -------------------------------------------------------------------------------------------- |
480
+ | 0 | Bundle written, or the stage diff printed / files staged |
481
+ | 1 | A grant signature, the trace chain or the anchored tip is invalid; nothing written |
482
+ | 2 | Bad input or refused: a bad `--out`, an existing file, a key or off-list path, no slice, ... |
483
+
55
484
  ---
56
485
 
57
486
  ## Programmatic Usage