@sleepy_panda_srl/portulan 0.1.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 (73) hide show
  1. package/LICENSE +201 -0
  2. package/NOTICE +5 -0
  3. package/README.md +87 -0
  4. package/cli/README.md +337 -0
  5. package/cli/compile.mjs +2209 -0
  6. package/cli/control-chars.mjs +570 -0
  7. package/cli/discover.mjs +985 -0
  8. package/cli/doctor.mjs +2803 -0
  9. package/cli/feedback.mjs +970 -0
  10. package/cli/gate.mjs +271 -0
  11. package/cli/index.mjs +1390 -0
  12. package/cli/init.mjs +1729 -0
  13. package/cli/librarian.mjs +1384 -0
  14. package/cli/manifest.mjs +59 -0
  15. package/cli/new.mjs +803 -0
  16. package/cli/pack-version.mjs +543 -0
  17. package/cli/plugin-lint.mjs +1566 -0
  18. package/cli/portulan.mjs +266 -0
  19. package/cli/recipe-set.mjs +425 -0
  20. package/cli/rule-carriers.mjs +489 -0
  21. package/cli/skills-set.mjs +715 -0
  22. package/cli/stop-gate.mjs +434 -0
  23. package/cli/upgrade.mjs +897 -0
  24. package/cli/vendor.mjs +1504 -0
  25. package/core/README.md +46 -0
  26. package/core/engine.md +47 -0
  27. package/core/operating/autonomy.md +100 -0
  28. package/core/operating/evolution.md +84 -0
  29. package/core/operating/loop.md +83 -0
  30. package/core/operating/memory.md +161 -0
  31. package/core/operating/safety.md +42 -0
  32. package/core/operating/verification.md +76 -0
  33. package/core/personas/README.md +40 -0
  34. package/core/personas/implementer.md +51 -0
  35. package/core/personas/librarian.md +73 -0
  36. package/core/personas/reviewer.md +52 -0
  37. package/core/skills/README.md +52 -0
  38. package/core/skills/clarify/SKILL.md +42 -0
  39. package/core/skills/codify/SKILL.md +65 -0
  40. package/core/skills/consolidate/SKILL.md +121 -0
  41. package/core/templates/gate-policy.md +76 -0
  42. package/core/templates/handoff.md +29 -0
  43. package/core/templates/memory-entry.md +44 -0
  44. package/core/templates/pack.md +94 -0
  45. package/core/templates/persona.md +69 -0
  46. package/core/templates/proposal.md +27 -0
  47. package/core/templates/repo-card.md +28 -0
  48. package/core/templates/skill.md +57 -0
  49. package/core/templates/task.md +26 -0
  50. package/core/templates/workspace.md +81 -0
  51. package/package.json +41 -0
  52. package/packs/.claude-plugin/plugin.json +22 -0
  53. package/packs/README.md +61 -0
  54. package/packs/rituals/README.md +13 -0
  55. package/packs/rituals/checkpoints/README.md +105 -0
  56. package/packs/rituals/checkpoints/pack.json +36 -0
  57. package/packs/rituals/checkpoints/personas/supervisor.md +64 -0
  58. package/packs/rituals/checkpoints/skills/milestone-close/SKILL.md +75 -0
  59. package/packs/rituals/checkpoints/skills/pre-commit/SKILL.md +75 -0
  60. package/packs/rituals/checkpoints/skills/session-open/SKILL.md +57 -0
  61. package/packs/stacks/README.md +8 -0
  62. package/packs/tools/README.md +17 -0
  63. package/packs/tools/github/README.md +65 -0
  64. package/packs/tools/github/pack.json +29 -0
  65. package/packs/tools/github/verify/README.md +51 -0
  66. package/packs/tools/github/verify/actions-pinned.sh +132 -0
  67. package/spec/README.md +399 -0
  68. package/spec/migrations/0001-repository-declares-its-tree.mjs +100 -0
  69. package/spec/migrations/0002-bundle-fallback-path.mjs +179 -0
  70. package/spec/migrations/README.md +95 -0
  71. package/spec/pack.schema.json +201 -0
  72. package/spec/slots.md +796 -0
  73. package/spec/workspace.schema.json +438 -0
package/cli/gate.mjs ADDED
@@ -0,0 +1,271 @@
1
+ #!/usr/bin/env node
2
+ // The PreToolUse gate runner — the *explanation* half of the enforcement compiler.
3
+ //
4
+ // Wired by `./compile.mjs` into `.claude/settings.json`. It reads the host's hook payload on
5
+ // stdin, finds which rule of the policy the workspace **yields** the attempted action matches, and
6
+ // returns that rule's own sentence as the decision reason. *Yields*, not *declares*: the rules in
7
+ // `.portulan/gates.json` plus the fragments its packs contribute — see "The policy this runner reads"
8
+ // below, which is the whole of what `#269` was.
9
+ //
10
+ // ## This file is not the gate. Read that twice.
11
+ //
12
+ // The gate is the `permissions` rule the same compiler emits. This runner exists because a bare
13
+ // "permission denied" teaches nobody anything, while `gates.json` already holds a sentence written
14
+ // for exactly this moment. The split is forced by a measurement, not by taste: on CLI 2.1.220 a
15
+ // PreToolUse hook that CRASHES **fails open** — the tool proceeds — on the identical wiring that
16
+ // blocks when the hook is healthy. So a hook can never be the load-bearing layer here, and this one
17
+ // is written to fail open *deliberately and visibly* rather than pretending otherwise:
18
+ //
19
+ // any internal error => exit 0, emit nothing => the permission rule governs, unchanged.
20
+ //
21
+ // Refusing loudly on error *is* available — a hook exiting 2 blocks — and it is still wrong here, for a
22
+ // reason worth stating precisely rather than with the hand-wave an earlier draft used. If this runner
23
+ // failed closed, a malformed `.portulan/gates.json` would deny EVERY matched tool call until someone fixed it:
24
+ // an undriveable session, whose only repair is inside the repository it can no longer edit. What is lost
25
+ // by stepping aside is bounded and known — the wrapper coverage and the sentences — because the
26
+ // permission layer never reads that file at runtime.
27
+ //
28
+ // ## Why the decision mirrors the permission rule
29
+ //
30
+ // `prohibited` => `deny`, `gated` => `ask`. The hook returns the SAME decision the permission rule
31
+ // carries. A hook returning `deny` for a Gated action would convert a per-action prompt into a hard
32
+ // block — that is the tier above it, and collapsing the two would make the constitution's
33
+ // protection indistinguishable from an ordinary push.
34
+ //
35
+ // ## What this layer is FOR, which is not what its author first assumed
36
+ //
37
+ // The first version of this runner existed to supply a better sentence than "permission denied".
38
+ // Measured, that was wrong: when a permission rule matches, **the host runs this hook and then
39
+ // discards its reason** — the permission layer's generic message is what the agent sees. Verified
40
+ // with a canary that recorded both the invocation and the command it saw. A hook emitted purely to
41
+ // improve a message it cannot reach would be an inert component that reads as an active one, which
42
+ // is the defect this repository already has a rule about.
43
+ //
44
+ // So the split earns its keep somewhere else: **this layer covers what the permission pattern
45
+ // cannot.** Two things, and in both the permission layer has nothing to say, so this decision AND
46
+ // its sentence are what the agent gets:
47
+ //
48
+ // 1. The WRAPPER spelling. `Bash(git push:*)` is a prefix match against the literal command, so
49
+ // `bash -c "git push …"` is invisible to it — measured, and the reason
50
+ // `../core/operating/autonomy.md` calls the platform floor the gate that holds when this
51
+ // layer fails. This runner unwraps one level of `sh -c` / `bash -c` / `zsh -c` before matching.
52
+ // 2. A SHELL WRITE to a path a `write:` rule protects. `Edit(./docs/vision.md)` denies three
53
+ // tools, and `echo x >> docs/vision.md` is a fourth way to the same bytes. `matchesRule` now
54
+ // answers for `Bash` on a write rule, by a table of redirections and file-writing commands
55
+ // that `./compile.mjs` states in full.
56
+ //
57
+ // Two layers, two jobs: the permission rule cannot fail open, and this one covers more ground.
58
+ // **The second case above is the uncomfortable one**, and it is named in `../gate-map.md`'s
59
+ // honest-holes list rather than left here: it is the only gate whose sole layer is this file, so
60
+ // every "fails open" sentence above is, for shell writes to the constitution, the whole story
61
+ // rather than a footnote about a lost message.
62
+ //
63
+ // **One level of unwrapping, and no more.** Deeper nesting, a heredoc, an interpolated variable, a
64
+ // command assembled at runtime — all still escape, and no amount of parsing here would close that.
65
+ // The same is true one layer over: a write spelled through a runtime (`python3 -c`) or a writer
66
+ // outside that table reaches the file. Anything that must not happen regardless of spelling belongs
67
+ // on the platform floor, not in a matcher.
68
+
69
+ import fs from "node:fs";
70
+ import path from "node:path";
71
+ import { fileURLToPath } from "node:url";
72
+
73
+ // The action vocabulary is defined ONCE, in the compiler, and imported here. Two implementations of
74
+ // one matcher is the drift this repository keeps finding — and a matcher that drifts does not look
75
+ // wrong, it looks like a gate that quietly stopped covering something.
76
+ //
77
+ // **The same argument reaches one level up, and for a milestone it was applied only to the matcher.**
78
+ // Importing `matchesRule` kept the two layers agreeing about what an action *is* while they disagreed
79
+ // about which rules *exist*: this file read the workspace's declared policy, and `./compile.mjs`
80
+ // enforced that policy plus its packs' contributions. `packContributions` and `composeFragments` are
81
+ // imported for the same reason as the matcher — a second composer would drift the same way — and
82
+ // `doctor` took this identical repair for this identical noun one session earlier.
83
+ import { matchesRule, policyPath, packContributions, composeFragments, parse } from "./compile.mjs";
84
+
85
+ // **The project root is TOLD to this runner, never derived from where this file sits.**
86
+ //
87
+ // It used to be derived: this file lived at `.portulan/compile/gate.mjs`, so `HERE/../..` was the
88
+ // repository and `basename(HERE/..)` was the workspace directory. That worked for exactly one layout —
89
+ // the author's — and it is why the file could not ship. Milestone 7 moves both runners into `cli/` so an
90
+ // adopter actually receives them (`package.json`'s `files` never carried `.portulan/`, so every compiled
91
+ // policy named a file the adopter did not have, and a missing hook fails open). From `cli/`, and even
92
+ // more so from `node_modules/@sleepy_panda_srl/portulan/cli/`, this file has no idea where the
93
+ // adopter's workspace is — and inferring one would be #131's class, paths resolved against the author's
94
+ // layout, in the tool with the most to lose from it.
95
+ //
96
+ // `CLAUDE_PROJECT_DIR` is what the host sets and what the emitted hook already interpolates; `cwd` is the
97
+ // honest fallback, because a hook runs from the project. `||` rather than `??` on purpose: an env var set
98
+ // to the empty string must fall through to `cwd`, not resolve every path against `""`.
99
+ //
100
+ // **What happens when no workspace is found differs between the two runners, and the first draft of this
101
+ // paragraph claimed the stricter behaviour for both.** THIS file steps aside silently — `main()` catches
102
+ // and returns without a decision — which is its own defended design: a PreToolUse hook that cannot read
103
+ // the policy must not block every tool call, and the permission rules compiled beside it still hold, so
104
+ // the layer degrades rather than disappearing. `./stop-gate.mjs` does the opposite and blocks loudly,
105
+ // because a Stop-gate that cannot read the workspace has nothing beneath it. Stated per file rather than
106
+ // as one sentence covering both, since they genuinely differ and the difference is the interesting part.
107
+ const PROJECT = process.env.CLAUDE_PROJECT_DIR || process.cwd();
108
+ const WORKSPACE_DIR = process.env.PORTULAN_WORKSPACE || ".portulan";
109
+ const POLICY = policyPath(PROJECT, WORKSPACE_DIR);
110
+
111
+ /** Exit without a decision. The permission rule still holds; only the sentence is lost. */
112
+ function stepAside() {
113
+ process.exit(0);
114
+ }
115
+
116
+ // ## The policy this runner reads
117
+ //
118
+ // **Declared plus composed, and until #269 it was declared alone.** A pack contributes gate fragments
119
+ // (`contributes.gates` in its `pack.json`); `./compile.mjs` composes them onto the workspace's own rules
120
+ // before it emits anything, so the permission rules sitting beside this hook are compiled from the
121
+ // composed set. This file read `.portulan/gates.json` and nothing else, so a rule a pack contributed was
122
+ // enforced by the layer that cannot say why and invisible to the layer whose whole job is the sentence —
123
+ // and for a `prohibited` fragment with a matcher, the two layers returned different decisions.
124
+ //
125
+ // **What this cost on THIS repository is nothing, and saying so is the point.** Its one gate-contributing
126
+ // pack (`rituals/checkpoints`) contributes two fragments and both carry `action: {none: …}`, which no
127
+ // matcher can reach — `compile --matrix` lists them among the gates no backend compiles. So the defect
128
+ // here was latent rather than live, and it is fixed on the shape rather than on an incident. The demonstration
129
+ // in `./gate.test.mjs` uses a fixture whose pack contributes a *matchable* fragment, because manufacturing
130
+ // a live case would have meant giving a `none` rule a matcher — reversing the ruling recorded in
131
+ // `../.portulan/proposals/0029-a-constraint-names-a-category-not-a-list.md` Q3, which removed one.
132
+ //
133
+ // **Three limits, named rather than left to be measured.** _(This said "Two" while enumerating three:
134
+ // the third arrived with the round-1 fix below and the count above it did not move. Exactly the
135
+ // undercount-by-enumeration `../.portulan/gate-map.md`'s own `action none` row was corrected for on
136
+ // 2026-08-14, repeated here within the week. Found by Copilot on #272, round 2.)_
137
+ //
138
+ // The three share one principle, and stating it is what keeps a later reader from deriving the wrong
139
+ // rule from the pair: **this runner degrades toward whatever intact policy remains — composed to
140
+ // declared, declared to nothing — and never toward a block.** It is never the load-bearing layer, so
141
+ // every arm below hands the decision back rather than taking it.
142
+ //
143
+ // 1. **No discovery.** `packContributions` is called with no `discovery` thunk, so `resolutionRoots`'
144
+ // unasked arm never consults the host's plugin cache and roots come from the workspace manifest's
145
+ // `tree` alone. Deliberate twice over: this runs on every tool call, and a gate whose answer moved
146
+ // with what is installed on the machine would be a gate nobody could review. A pack resolving ONLY
147
+ // from the host cache therefore contributes to what a bare `compile` yields and not to what this
148
+ // hook composes — the residual gap, stated in `../.portulan/gate-map.md` and owned by `#264`.
149
+ // 2. **A composition failure falls back to the DECLARED policy**, and does not step aside. Composition
150
+ // only ever adds a rule or raises a tier — `composeFragments` throws on a demotion rather than
151
+ // applying it — so the composed policy is never weaker than the declared one, and falling back can
152
+ // only lose coverage this file already had rather than drop below it. Stepping aside instead would
153
+ // let a malformed `pack.json` switch off gates the workspace declares in its own file, which hands a
154
+ // dependency a lever on the layer above it. `./compile.mjs` refuses to build in the same case, and
155
+ // the divergence is the intended one: it fails closed at build time against a file you can edit,
156
+ // while this runs on every tool call.
157
+ // 3. **The composed policy is VALIDATED, by the same `parse` the compiler validates with.** Composition
158
+ // checks a fragment's *tier* and, when tightening, that the action shape is unchanged; it does not
159
+ // check that an ADDED fragment is a well-formed rule. So a pack contributing
160
+ // `{id, tier: prohibited, action: {shell: curl}}` and no `reason` composed cleanly and this hook
161
+ // denied with the sentence `— undefined`, where `compile` refuses the same input at exit 2 with
162
+ // *"rule `no-reason` carries no reason — a gate with no sentence to show a human is not finished"*.
163
+ // Measured, on the runner. The two layers disagreeing about whether a policy is admissible is this
164
+ // file's own defect one step further in. Raised by Copilot on #272, round 1, in the suppressed
165
+ // channel. **The limit, since the fix is narrower than it reads:** this covers the COMPOSED arm. A
166
+ // rule missing its `reason` in the *declared* policy still denies with `— undefined`, byte-identically
167
+ // with every predecessor — because the fallback for an inadmissible declared policy is that same
168
+ // policy, and stepping aside there would drop a live prohibition over a missing string. `compile`
169
+ // refuses that shape at build time, which is where it belongs. Pinned in `./gate.test.mjs` so the
170
+ // arm cannot be quietly converted to a step-aside.
171
+ //
172
+ // **Validation only — the decision is still made on the composed policy, not on `parse`'s return.**
173
+ // Not because the parsed shape would not match: measured, `parse` returns rules carrying the SAME
174
+ // `action` object beside the added `kind`/`target`, and `matchesRule` answers correctly on them —
175
+ // a draft of this comment claimed otherwise and was wrong. The two real grounds: `parse` **refuses
176
+ // rather than normalises**, so once it passes, the composed and parsed forms are identical for
177
+ // matching and deciding on the parsed one buys nothing; and the fallback path has no parsed form to
178
+ // offer — an inadmissible policy produces none — so deciding on `parse`'s return would hand
179
+ // `decide()` two different shapes on its two paths, importing a representational seam rather than
180
+ // closing one. **If `parse` ever stops refusing and starts normalising, that change must sweep this
181
+ // hook too.** The check costs 0.003 ms against a ~30 ms invocation, 2000 iterations, so it is free.
182
+ function yielded(declared) {
183
+ try {
184
+ const { contributions } = packContributions(PROJECT, WORKSPACE_DIR);
185
+ const composed = composeFragments(declared, contributions).policy;
186
+ parse(composed);
187
+ return composed;
188
+ } catch {
189
+ return declared;
190
+ }
191
+ }
192
+
193
+ /**
194
+ * The rule this call answers to: the STRONGEST gate it falls under, not the first one listed.
195
+ *
196
+ * This returned the first match for one milestone, which is not the same rule whenever two of different
197
+ * tiers match one call: the weaker one wins if it is listed higher. The shape is ordinary rather than
198
+ * exotic — one dangerous spelling prohibited beneath a prefix the workspace already gates broadly,
199
+ * `git push` at `gated` and `git push --mirror` at `prohibited` — and it lands on `ask` for an action the
200
+ * policy prohibits.
201
+ *
202
+ * **Composition makes that systematic rather than causing it, and a draft of this paragraph had it the
203
+ * other way round** — *"indistinguishable from the strongest for as long as the policy came from one
204
+ * file"*. Measured on a workspace composing nothing, with those two rules in that order: the old scan
205
+ * answered `ask`, this one answers `deny`. What composition adds is that a contributed rule can never
206
+ * win the tie-break, because `composeFragments` APPENDS added rules after the workspace's own — so a
207
+ * pack-contributed `prohibited` rule is always the later one and always the one first-match discards.
208
+ * That is #269's case reached through the ordering rather than through the missing rules, and closing
209
+ * only the missing-rules half would have left it.
210
+ *
211
+ * Grounded in the policy's own semantics rather than in host precedence between two matching patterns,
212
+ * which this repository has never measured and which nothing here needs: `prohibited` means the action
213
+ * must not happen, so answering `ask` on a call the policy prohibits is wrong on the policy's terms
214
+ * whatever the host does with the compiled pair.
215
+ *
216
+ * **It only ever moves a decision UP, and that is the honest form of the guarantee.** `prohibited` is the
217
+ * top of the tier order and `gated` the only other tier considered, so "strongest, ties to the first" is
218
+ * exactly *the first `prohibited` match if there is one, otherwise the first `gated` match* — which is
219
+ * what the loop below is. Output is byte-identical wherever no two matching rules sit at different tiers,
220
+ * and `ask` becomes `deny` where they do. **Not** "byte-identical for any workspace that composes
221
+ * nothing" — that is the claim the counterexample above kills, and this repository is not evidence for it
222
+ * either: its own policy is unchanged only because `edit-the-constitution`, the one `prohibited` rule it
223
+ * declares, is listed FIRST, so first-match already returned it. Measured, on a command matching both it
224
+ * and a gated shell rule: `deny` from either runner. Move that rule down the file and the two disagree.
225
+ * The early return on `prohibited` also keeps the denied path's short-circuit: nothing later can outrank
226
+ * the top of the order.
227
+ */
228
+ function decide(payload, policy) {
229
+ const tool = payload.tool_name;
230
+ const input = payload.tool_input ?? {};
231
+ let gated = null;
232
+ for (const rule of policy.rules ?? []) {
233
+ if (rule.tier !== "gated" && rule.tier !== "prohibited") continue;
234
+ if (!matchesRule(rule, tool, input)) continue;
235
+ if (rule.tier === "prohibited") return rule;
236
+ if (gated === null) gated = rule;
237
+ }
238
+ return gated;
239
+ }
240
+
241
+ async function main() {
242
+ let raw = "";
243
+ for await (const chunk of process.stdin) raw += chunk;
244
+
245
+ let payload;
246
+ let policy;
247
+ try {
248
+ payload = JSON.parse(raw);
249
+ policy = JSON.parse(fs.readFileSync(POLICY, "utf8"));
250
+ } catch {
251
+ stepAside();
252
+ return;
253
+ }
254
+
255
+ const rule = decide(payload, yielded(policy));
256
+ if (!rule) stepAside();
257
+
258
+ const decision = rule.tier === "prohibited" ? "deny" : "ask";
259
+ process.stdout.write(
260
+ `${JSON.stringify({
261
+ hookSpecificOutput: {
262
+ hookEventName: "PreToolUse",
263
+ permissionDecision: decision,
264
+ permissionDecisionReason: `PORTULAN GATE \`${rule.id}\` (${rule.tier}) — ${rule.reason}`,
265
+ },
266
+ })}\n`,
267
+ );
268
+ process.exit(0);
269
+ }
270
+
271
+ main().catch(stepAside);