peaks-cli 1.1.2 → 1.2.1

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 (45) hide show
  1. package/README.md +97 -3
  2. package/dist/src/cli/commands/core-artifact-commands.js +47 -3
  3. package/dist/src/cli/commands/gate-commands.d.ts +3 -0
  4. package/dist/src/cli/commands/gate-commands.js +103 -0
  5. package/dist/src/cli/commands/hooks-commands.d.ts +3 -0
  6. package/dist/src/cli/commands/hooks-commands.js +75 -0
  7. package/dist/src/cli/commands/request-commands.js +1 -1
  8. package/dist/src/cli/commands/sop-commands.d.ts +3 -0
  9. package/dist/src/cli/commands/sop-commands.js +215 -0
  10. package/dist/src/cli/index.js +12 -0
  11. package/dist/src/cli/program.js +47 -2
  12. package/dist/src/services/dashboard/project-dashboard-service.js +5 -3
  13. package/dist/src/services/doctor/doctor-service.d.ts +4 -0
  14. package/dist/src/services/doctor/doctor-service.js +66 -3
  15. package/dist/src/services/mode/mode-enforcement.js +2 -1
  16. package/dist/src/services/skills/hooks-settings-service.d.ts +45 -0
  17. package/dist/src/services/skills/hooks-settings-service.js +167 -0
  18. package/dist/src/services/skills/skill-presence-service.d.ts +1 -0
  19. package/dist/src/services/skills/skill-presence-service.js +12 -0
  20. package/dist/src/services/skills/skill-statusline-service.d.ts +2 -0
  21. package/dist/src/services/skills/skill-statusline-service.js +11 -1
  22. package/dist/src/services/sop/gate-enforce-service.d.ts +33 -0
  23. package/dist/src/services/sop/gate-enforce-service.js +168 -0
  24. package/dist/src/services/sop/sop-advance-service.d.ts +74 -0
  25. package/dist/src/services/sop/sop-advance-service.js +115 -0
  26. package/dist/src/services/sop/sop-check-service.d.ts +29 -0
  27. package/dist/src/services/sop/sop-check-service.js +148 -0
  28. package/dist/src/services/sop/sop-paths.d.ts +62 -0
  29. package/dist/src/services/sop/sop-paths.js +92 -0
  30. package/dist/src/services/sop/sop-registry-service.d.ts +46 -0
  31. package/dist/src/services/sop/sop-registry-service.js +89 -0
  32. package/dist/src/services/sop/sop-service.d.ts +47 -0
  33. package/dist/src/services/sop/sop-service.js +211 -0
  34. package/dist/src/services/sop/sop-types.d.ts +88 -0
  35. package/dist/src/services/sop/sop-types.js +11 -0
  36. package/dist/src/shared/paths.d.ts +1 -1
  37. package/dist/src/shared/paths.js +2 -1
  38. package/dist/src/shared/version.d.ts +1 -1
  39. package/dist/src/shared/version.js +1 -1
  40. package/package.json +1 -1
  41. package/schemas/doctor-report.schema.json +1 -1
  42. package/schemas/sop-manifest.schema.json +52 -0
  43. package/skills/peaks-solo/SKILL.md +32 -6
  44. package/skills/peaks-sop/SKILL.md +192 -0
  45. package/skills/peaks-sop/references/sop-authoring.md +161 -0
@@ -0,0 +1,52 @@
1
+ {
2
+ "$schema": "https://json-schema.org/draft/2020-12/schema",
3
+ "title": "Peaks SOP Manifest",
4
+ "type": "object",
5
+ "required": ["id", "name", "phases", "gates"],
6
+ "properties": {
7
+ "id": { "type": "string", "pattern": "^[a-z0-9][a-z0-9-]*$" },
8
+ "name": { "type": "string" },
9
+ "description": { "type": "string" },
10
+ "phases": {
11
+ "type": "array",
12
+ "minItems": 1,
13
+ "items": { "type": "string" }
14
+ },
15
+ "guards": {
16
+ "type": "array",
17
+ "description": "Optional Bash-action guards enforced by the PreToolUse hook (peaks gate enforce).",
18
+ "items": {
19
+ "type": "object",
20
+ "required": ["phase", "bash"],
21
+ "properties": {
22
+ "phase": { "type": "string" },
23
+ "bash": { "type": "string", "description": "JS regex tested against the Bash tool_input.command" }
24
+ }
25
+ }
26
+ },
27
+ "gates": {
28
+ "type": "array",
29
+ "items": {
30
+ "type": "object",
31
+ "required": ["id", "phase", "check"],
32
+ "properties": {
33
+ "id": { "type": "string", "pattern": "^[a-z0-9][a-z0-9-]*$" },
34
+ "phase": { "type": "string" },
35
+ "check": {
36
+ "type": "object",
37
+ "required": ["type"],
38
+ "properties": {
39
+ "type": { "type": "string", "enum": ["file-exists", "grep", "command"] },
40
+ "path": { "type": "string" },
41
+ "file": { "type": "string" },
42
+ "pattern": { "type": "string" },
43
+ "absent": { "type": "boolean", "description": "grep only: pass when the pattern is NOT found (\"must not contain X\")" },
44
+ "run": { "type": "array", "items": { "type": "string" } },
45
+ "expectExitZero": { "type": "boolean" }
46
+ }
47
+ }
48
+ }
49
+ }
50
+ }
51
+ }
52
+ }
@@ -44,9 +44,35 @@ peaks-solo (orchestrate only)
44
44
 
45
45
  ## Peaks-Cli Startup sequence (MANDATORY — execute in order)
46
46
 
47
- ### Peaks-Cli Step 1: Mode selection (MUST run before presence:set)
47
+ ### Peaks-Cli Step 0: Anchor the workflow (MANDATORY FIRST ACTIONS — no bail-out)
48
48
 
49
- When the user invokes Peaks-Cli Solo without explicitly naming an execution profile, use `AskUserQuestion` BEFORE any other action. Present the recommended full-auto path as the first/default option with a practical description for each:
49
+ The instant Peaks-Cli Solo is invoked, **before** the mode-selection question, before any analysis, and before you decide whether the request "needs" the full pipeline, you MUST run these two commands and see their output:
50
+
51
+ ```bash
52
+ # Session ID is auto-generated when omitted; the command returns it in the JSON output
53
+ peaks workspace init --project <repo> --json
54
+ peaks skill presence:set peaks-solo --project <repo> --gate startup
55
+ ```
56
+
57
+ If `workspace init` fails with "required option '--session-id' not specified", the CLI version predates auto-generation. Generate a session ID manually and pass it:
58
+
59
+ ```bash
60
+ SESSION_ID="$(date +%Y-%m-%d)-session-$(openssl rand -hex 3)"
61
+ peaks workspace init --project <repo> --session-id "$SESSION_ID" --json
62
+ peaks skill presence:set peaks-solo --project <repo> --gate startup
63
+ ```
64
+
65
+ > `<repo>` is the **git project root** (the directory containing `.git`). In a monorepo / single-repo-multi-package layout, this is the repo root, NOT a sub-package — `.peaks/` lives at the repo root so every package shares one workspace. If unsure, run `git rev-parse --show-toplevel` and use that path. Never let `.peaks/` land inside a sub-package directory.
66
+
67
+ **There is no request too lightweight to skip this.** "分析下这个项目", "看一下代码", "分析项目", "解释一下架构", a one-line question — all of them still create the workspace and set presence first. The workspace is cheap; a missing `.peaks/` is the #1 reported failure.
68
+
69
+ **Anti-bail-out rule (BLOCKING):** You MUST NOT exit the peaks-solo workflow, hand control back, or produce a final answer before Step 0 has run. If you catch yourself thinking "this is just analysis, I don't need the workflow" — STOP. Run Step 0, set presence, then continue. A pure-analysis request runs the **lightweight analysis branch** (project scan + standards dry-run + handoff with a Standards-increment section), but it still anchors the workspace and keeps presence active. Declining to anchor is a workflow violation.
70
+
71
+ `presence:set` accepts no `--mode` here on purpose — mode is unknown until Step 1. It is re-run with the selected mode in Step 2. Setting presence early guarantees the status header/line shows `peaks-solo` from the very first turn even if the user never reaches mode selection.
72
+
73
+ ### Peaks-Cli Step 1: Mode selection
74
+
75
+ After Step 0 has anchored the workspace and presence, when the user invokes Peaks-Cli Solo without explicitly naming an execution profile, use `AskUserQuestion` to pick the profile. Present the recommended full-auto path as the first/default option with a practical description for each:
50
76
 
51
77
  1. **Full auto (Recommended)** — Peaks-Cli handles planning, role coordination, validation, and compact handoff end-to-end while preserving required confirmation gates for risky or shared-state actions.
52
78
  2. **Assisted** — Peaks-Cli proposes plans, artifacts, and checks, then pauses for user decisions at major workflow boundaries.
@@ -66,9 +92,9 @@ Map the user's selection to the `--mode` flag value (used by `peaks skill presen
66
92
 
67
93
  If the user already names a profile in their invocation (e.g. `/peaks-solo --full-auto`, "用全自动模式"), skip this question and use the named profile directly.
68
94
 
69
- ### Peaks-Cli Step 2: Set skill presence
95
+ ### Peaks-Cli Step 2: Re-set skill presence with the chosen mode
70
96
 
71
- Only after the mode is known (user selected or explicitly named), run:
97
+ Step 0 already set presence with no mode. Now that the mode is known (user selected or explicitly named), re-run presence:set so the header/status line shows the profile:
72
98
 
73
99
  ```bash
74
100
  peaks skill presence:set peaks-solo --project <repo> --mode <mode-value> --gate startup
@@ -144,7 +170,7 @@ For frontend workflows, RD and QA must use Playwright MCP (`mcp__playwright__` t
144
170
 
145
171
  ### Workspace initialization gate
146
172
 
147
- Before ANY role handoff or artifact write, Peaks-Cli Solo MUST create the workspace. Session IDs are now **auto-generated** with the format `YYYY-MM-DD-session-<6位hex>` (e.g. `2026-05-26-session-a3f8b1`). The user does not provide a session ID — the system creates and persists it in `.peaks/.session.json`.
173
+ The workspace is created in Step 0 (Startup sequence) as a mandatory first action — before any analysis, role handoff, or artifact write, and regardless of how lightweight the request is. Session IDs are now **auto-generated** with the format `YYYY-MM-DD-session-<6位hex>` (e.g. `2026-05-26-session-a3f8b1`). The user does not provide a session ID — the system creates and persists it in `.peaks/.session.json`.
148
174
 
149
175
  When `peaks workspace init` is run without `--session-id`, it automatically generates a new session ID using today's date and a random hex suffix. If `.peaks/.session.json` already exists with a valid session, the existing session is reused.
150
176
 
@@ -760,7 +786,7 @@ Use `standards init` for first-time creation and `standards update` for existing
760
786
 
761
787
  Do not hand-write standards file mutations inside the skill.
762
788
 
763
- For project-analysis requests such as "分析项目", the handoff must include an explicit **Standards increment** section. Report the current `CLAUDE.md` and `.claude/rules/**` status from the dry-run output as incremental deltas, not just a generic preflight note:
789
+ For project-analysis requests such as "分析项目" / "分析下这个项目", Step 0 still applies: the workspace is initialized and `peaks-solo` presence is set before any analysis output. These requests run the lightweight analysis branch (project scan + standards dry-run) rather than the full RD/QA pipeline, but they never skip workspace anchoring or exit the workflow. The handoff must include an explicit **Standards increment** section. Report the current `CLAUDE.md` and `.claude/rules/**` status from the dry-run output as incremental deltas, not just a generic preflight note:
764
790
 
765
791
  - whether `CLAUDE.md` is missing, existing, planned, skipped, appended, or review-only;
766
792
  - which `.claude/rules/**` files are planned, existing, skipped, appended, or review-only;
@@ -0,0 +1,192 @@
1
+ ---
2
+ name: peaks-sop
3
+ description: Authoring skill for user-defined SOPs (standard operating procedures) in Peaks. Use when a user wants to create, edit, debug, or register their own gated workflow — ordered phases plus gates that block advancement until conditions are met — by describing it in natural language instead of hand-writing JSON or memorizing CLI commands. DOMAIN-AGNOSTIC: not just software/release flows — equally for content publishing, compliance and approval checklists, data pipelines, onboarding, ops runbooks, or any personal repeatable procedure, wherever "don't enter the next stage until X is true" applies and X is checkable via a file, file content, or a command.
4
+ ---
5
+
6
+ # Peaks-Cli SOP Authoring
7
+
8
+ Peaks-Cli SOP turns a natural-language workflow description into a validated, registered custom SOP, then helps the user debug it until each gate behaves as intended. The user describes the process in plain language; this skill drives the `peaks sop` CLI on their behalf — they never have to hand-write `sop.json` or remember the command sequence.
9
+
10
+ **This is a general workflow-gating tool, not a developer-only tool.** A SOP is any repeatable process with ordered stages where you must not skip ahead until conditions are met. Software release is just one example; content publishing, compliance/approval checklists, data validation pipelines, employee onboarding, ops runbooks, and personal procedures are all first-class — often the more valuable use. When you interview the user, do not assume code: ask about *their* process in *their* domain.
11
+
12
+ ## What to tell the user BEFORE running any command
13
+
14
+ This skill helps you create, test, and enforce a repeatable workflow (a "SOP")
15
+ with gates that physically block advancement until conditions are met. It works
16
+ for ANY domain — content publishing, compliance, onboarding, data pipelines,
17
+ software releases, personal procedures.
18
+
19
+ Before you run `peaks skill presence:set` or any other setup command, tell the
20
+ user (in your own words, one or two sentences):
21
+
22
+ "I'll help you turn your process into a Peaks SOP — ordered stages plus gates
23
+ that make sure you never skip a step. I'll interview you about your workflow,
24
+ generate the definition, test it, and register it. Ready?"
25
+
26
+ If the user has never used Peaks before, offer a 30-second demo:
27
+
28
+ "Want me to create a quick demo SOP first? It takes 30 seconds — I'll
29
+ scaffold one, show you how a gate blocks, then we can replace it with your
30
+ real workflow."
31
+
32
+ When they accept, use `demo-sop` as the id (NOT `peaks-sop` — the `peaks-` prefix
33
+ is reserved), name it "Demo SOP", make two phases (draft → done), one gate
34
+ (file-exists README.md), then run the full init→lint→check→register→hooks
35
+ install flow so they see the whole thing. Then offer to replace it with their
36
+ real process.
37
+
38
+ ## Skill presence (MANDATORY first action — run AFTER telling the user what you're doing)
39
+
40
+ If the user invoked this skill directly ("peaks-sop") without mentioning a
41
+ specific project, assume `--project .` (current directory). If they are NOT in
42
+ the middle of a multi-skill peaks-solo/prd/rd/qa pipeline, skip `statusline
43
+ install` — the status bar is for long-running orchestrations; a standalone SOP
44
+ authoring session does not need it.
45
+
46
+ ```bash
47
+ peaks skill presence:set peaks-sop --project <repo> --mode <mode> --gate startup
48
+ peaks project memories --project <repo> --json
49
+ ```
50
+
51
+ Then display: `Peaks-Cli Skill: peaks-sop | Peaks-Cli Gate: startup | Next: <one short action>`. Update gates with `peaks skill presence:set peaks-sop --project <repo> --mode <mode> --gate <gate>` when they change. When the SOP is registered and the user is satisfied, run `peaks skill presence:clear --project <repo>`.
52
+
53
+ ## Responsibilities
54
+
55
+ - interview the user to turn a natural-language workflow into ordered **phases** and the **gates** that guard entry into each phase;
56
+ - generate a valid SOP manifest (`sop.json`) and the registrable `SKILL.md` via the CLI — never make the user hand-author JSON;
57
+ - run the lint → fix → re-lint debug loop until the manifest is clean;
58
+ - test each gate (pass/fail/blocked) and dry-run advancement so the user sees the SOP behave before committing;
59
+ - register the SOP so it joins skill presence / statusline like a built-in skill;
60
+ - explain the three gate types and the security posture of command gates.
61
+
62
+ ## What a SOP is (explain this to the user)
63
+
64
+ A SOP is an **ordered list of phases** plus **gates** bound to phases. A gate that does not pass blocks advancement into its phase — this is how "don't drop steps" applies to the user's own workflow. The SOP lives at `.peaks/sops/<sop-id>/` (manifest `sop.json` + a registrable `SKILL.md`). Gate checks come in three types:
65
+
66
+ | type | fields | passes when |
67
+ |------|--------|-------------|
68
+ | `file-exists` | `path` | the file exists (path pinned inside the project) |
69
+ | `grep` | `file` + `pattern` (+ `absent`) | the regex matches in the file — or, with `absent: true`, does NOT match |
70
+ | `command` | `run` (argv array) + `expectExitZero` | the command exits as expected |
71
+
72
+ Prefer **`grep` with `absent: true`** for any "must not contain X" gate (no leftover `TODO`, no placeholder, no unresolved marker). It is a pure-text check — no `--allow-commands`, cross-platform, no shell. Reach for a `command` gate only when the check genuinely needs to run a program.
73
+
74
+ `command` gates run user-defined processes and are **refused by default** — they require explicit `--allow-commands`, run with no shell (argv array, no injection), a timeout, and cwd pinned to the project. Always tell the user when a SOP needs `--allow-commands` and why.
75
+
76
+ ### Where SOP files live (two definition layers, per-project execution)
77
+
78
+ A SOP *definition* (manifest + SKILL.md) can live in two layers:
79
+ - **Global** `~/.peaks/sops/<sop-id>/` — your personal SOPs, reusable across every project. `init` / `lint` / `register` default here (no `--project`).
80
+ - **Project** `<project>/.peaks/sops/<sop-id>/` — **committed into the repo**, so a teammate who clones it gets the SOP (and, with the hook installed, is enforced). Pass `--project <repo>` to `init` / `lint` / `register` to use this layer.
81
+
82
+ The **project layer takes precedence** over global for the same id. Execution reads see the merged view (project wins). A SOP's *run-state* (current phase, history) is always **per-project**: `<project>/.peaks/sop-state/<sop-id>/`. `check` / `advance` take `--project` (default: current directory) to say which project the gates evaluate against, whose progress advances, and which definition layer wins.
83
+
84
+ `advance` also enforces **phase order**: you may re-enter the current phase, step back, or move to the immediately-next phase, but not skip ahead — a forward jump returns `SOP_PHASE_SKIP` (bypassable, like a gate, with `--allow-incomplete --reason`).
85
+
86
+ ### Where SOPs apply (lead with the user's domain, not code)
87
+
88
+ The three gate primitives are domain-neutral, so the same engine governs very different workflows:
89
+
90
+ | domain | phases (example) | gate idea |
91
+ |--------|------------------|-----------|
92
+ | content / publishing | draft → edit → publish | `file-exists` the draft; `grep` no `TODO`/`TKTK`; `command` runs a spell/word-count check |
93
+ | compliance / approval | prepare → review → sign-off | `file-exists` `approval.md`; `grep` the doc contains "Approved" |
94
+ | data pipeline | raw → cleaned → validated | `command` runs a validator script that exits 0 |
95
+ | onboarding / ops | request → provision → done | `file-exists` each checklist artifact; `command` verifies a config |
96
+ | personal procedure | any repeatable steps | whatever "don't forget step X" means, expressed as a file/grep/command |
97
+
98
+ The one boundary to explain: a gate must reduce to **a file existing, text matching in a file, or a command's exit code**. A purely human-judgment gate ("did the editor approve?") is expressed by reifying it into a signal — e.g. require an `approved.md` file, or that a status file contains "approved". The `command` gate is the universal adapter for anything scriptable.
99
+
100
+ ## Un-bypassable enforcement (optional, opt-in)
101
+
102
+ By default a SOP gate only blocks the `peaks sop advance` command — nothing forces the agent through it. To make a gate **physically un-bypassable**, a SOP can declare **guards** that bind a concrete irreversible Bash action to a phase, and the user installs a PreToolUse hook:
103
+
104
+ ```jsonc
105
+ // in sop.json
106
+ "guards": [ { "phase": "publish", "bash": "git +push" } ]
107
+ ```
108
+ Meaning: running a Bash command matching `git +push` IS entering the publish phase, so publish's gates must pass first. Then:
109
+
110
+ ```bash
111
+ peaks hooks install --project <repo> # explicit, opt-in; writes one PreToolUse entry
112
+ ```
113
+
114
+ Now when the agent tries `git push` while the publish gate fails, Claude Code receives `permissionDecision: "deny"` and the command is blocked **before any permission check — it holds even under `--dangerously-skip-permissions`**. CI only blocks at merge; CLAUDE.md instructions are advisory; this blocks in-conversation and cannot be skipped.
115
+
116
+ - `bash` is a **JS regex inside JSON** — escape backslashes (`"git\\s+push"`) or just use `"git +push"`. `peaks sop lint` rejects an invalid regex (`GUARD_INVALID_PATTERN`).
117
+ - Emergency override: `peaks gate bypass --sop <id> --phase <phase> --reason "<why>"` records a **one-shot** token consumed by the next blocked command (capped per project per SOP, reason audited).
118
+ - Trust: enforcement **fails open** — any internal error allows the command (a Peaks bug never bricks Claude Code); only a real gate failure denies. Installing the hook is an explicit user command; this skill never writes `settings.json` itself.
119
+ - `peaks hooks status` / `peaks hooks uninstall` manage the hook.
120
+
121
+ > Team enforcement: register the SOP into the **project layer** (`peaks sop init/register --project <repo>`) so the definition is committed in the repo. A teammate who clones it — even with an empty global `~/.peaks` — is enforced by the same gates once they install the hook. (A SOP that lives only in your global `~/.peaks` enforces only on your machine.)
122
+
123
+ ## Default runbook
124
+
125
+ The default sequence this skill executes on the user's behalf. The natural-language → generate → debug loop IS this runbook.
126
+
127
+ ```bash
128
+ # self-check (required by doctor; no user-visible effect)
129
+ peaks skill runbook peaks-sop --json
130
+
131
+ # 1. interview the user FIRST (before any scaffold): what are the ordered stages?
132
+ # For each stage, what must be true before entering it? Translate answers into
133
+ # file-exists / grep / grep-absent / command gates.
134
+
135
+ # 2. scaffold the SOP based on the interview (preview first, then apply)
136
+ # definitions are global (~/.peaks/sops) — init/lint/register take no --project
137
+ peaks sop init --id <sop-id> --name "<human name>" --json
138
+ peaks sop init --id <sop-id> --name "<human name>" --apply --json
139
+
140
+ # 3. write the phases/gates from the interview into ~/.peaks/sops/<sop-id>/sop.json
141
+ # (edit the manifest directly — the user described it in natural language)
142
+
143
+ # 4. DEBUG LOOP: lint, fix the reported findings, re-lint until clean
144
+ peaks sop lint --id <sop-id> --json
145
+ peaks sop lint --id <sop-id> --allow-commands --json # when the SOP uses command gates
146
+
147
+ # 5. test each gate behaves as intended (pass / fail / blocked) against a project
148
+ peaks sop check --id <sop-id> --gate <gate-id> --project <repo> --json
149
+
150
+ # 6. dry-run the flow to confirm gates + phase order block/allow the right phases
151
+ peaks sop advance --id <sop-id> --to <phase> --project <repo> --dry-run --json
152
+
153
+ # 7. register the SOP (preview, then apply) so it joins presence/statusline
154
+ peaks sop register --id <sop-id> --dry-run --json
155
+ peaks sop register --id <sop-id> --json
156
+ peaks sop registry --json
157
+
158
+ # 8. (optional) make a gate un-bypassable: declare guards in sop.json, then install the hook
159
+ peaks hooks install --project <repo>
160
+ peaks hooks status --project <repo>
161
+ # emergency one-shot override when a guarded action must proceed despite a failing gate:
162
+ peaks gate bypass --sop <sop-id> --phase <phase> --reason "<why>" --project <repo>
163
+
164
+ # 9. hand the SOP to the user; clear presence when done
165
+ peaks skill presence:clear --project <repo>
166
+ ```
167
+
168
+ ### Transition verification gates (MANDATORY — run the command, see the output)
169
+
170
+ You cannot declare a SOP ready from memory. Each gate below is a command you **MUST run** and whose output you **MUST see**.
171
+
172
+ **Peaks-Cli Gate A — the manifest lints clean before register:**
173
+ ```bash
174
+ peaks sop lint --id <sop-id> --json
175
+ # Expected: ok:true. If ok:false, fix the findings and re-lint — do NOT register a SOP that does not lint.
176
+ ```
177
+
178
+ **Peaks-Cli Gate B — gates behave as intended before handing off:**
179
+ ```bash
180
+ peaks sop check --id <sop-id> --gate <gate-id> --project <repo> --json
181
+ # Confirm each gate returns the verdict the user expects (pass on the good state, fail/blocked otherwise).
182
+ ```
183
+
184
+ ## Debugging guidance
185
+
186
+ When `sop lint` reports findings, fix them in `sop.json` and re-lint — common findings: duplicate gate id, gate bound to an undefined phase, missing check fields, or a command gate without `--allow-commands`. When `sop check` returns `blocked`, the check could not be evaluated (path escaped the project, target file unreadable, command not permitted or failed to spawn) — distinct from `fail` (evaluated, condition not met). Use `sop advance --dry-run` to confirm the blocking behaves before any real advance.
187
+
188
+ Concrete manifest reference, gate cookbook, and the debug loop: `references/sop-authoring.md`.
189
+
190
+ ## Boundaries
191
+
192
+ Do not implement the user's business code, run their real release, or modify runtime configuration. This skill authors and validates the SOP definition; the SOP's gates then govern the user's own workflow. `command` gates execute user-defined commands only with explicit `--allow-commands` consent. Do not register a SOP that does not lint clean.
@@ -0,0 +1,161 @@
1
+ # SOP Authoring Reference
2
+
3
+ Concrete reference for the `peaks-sop` skill: manifest shape, gate cookbook, the
4
+ interview → generate → debug loop, and security notes. The skill drives the
5
+ `peaks sop` CLI on the user's behalf — this file is the detail behind that.
6
+
7
+ ## Where files live
8
+
9
+ SOP **definitions** live in one of two layers:
10
+ - **Global** `~/.peaks/sops/<sop-id>/sop.json` (+ `SKILL.md`) — personal, reusable across every project. `init` / `lint` / `register` default here.
11
+ - **Project** `<project>/.peaks/sops/<sop-id>/sop.json` — committed into the repo and team-shared. Pass `--project <repo>` to `init` / `lint` / `register` to use this layer. The project layer **wins** over global for the same id; execution reads (`check`/`advance`/enforce) and `sop registry --project` see the merged view.
12
+
13
+ A SOP's **run-state** is always per-project: `<project>/.peaks/sop-state/<sop-id>/state.json` (git-ignored — runtime, not shared). `check` / `advance` take `--project` (default: current directory) — that says which project the gate paths resolve against, whose progress advances, and which definition layer wins.
14
+
15
+ Use the **project layer** when you want a workflow's gates to bind the whole team (commit the SOP, install the hook in the repo's `.claude/settings.json`); use **global** for your own repeatable procedures across many repos.
16
+
17
+ ## Manifest shape (`~/.peaks/sops/<sop-id>/sop.json`)
18
+
19
+ ```json
20
+ {
21
+ "id": "team-release",
22
+ "name": "Team Release",
23
+ "description": "Gates that must pass before we ship a release.",
24
+ "phases": ["draft", "review", "ship"],
25
+ "gates": [
26
+ { "id": "changelog", "phase": "ship", "check": { "type": "file-exists", "path": "CHANGELOG.md" } },
27
+ { "id": "no-fixme", "phase": "review", "check": { "type": "grep", "file": "src/index.ts", "pattern": "FIXME" } },
28
+ { "id": "tests", "phase": "ship", "check": { "type": "command", "run": ["npm", "test"] } }
29
+ ]
30
+ }
31
+ ```
32
+
33
+ Rules the lint enforces (so generate the manifest to satisfy them):
34
+
35
+ - `id` — lowercase kebab, starts alphanumeric; must NOT collide with the reserved `peaks-` / `peaks` namespace; must match the directory name.
36
+ - `phases` — at least one; no duplicates.
37
+ - each gate `id` — lowercase kebab, unique within the SOP.
38
+ - each gate `phase` — must be one of the declared `phases`.
39
+ - each gate `check` — a known type with its required fields present.
40
+
41
+ ## Gate cookbook
42
+
43
+ | intent | check |
44
+ |--------|-------|
45
+ | a file must exist before a phase | `{ "type": "file-exists", "path": "CHANGELOG.md" }` |
46
+ | a doc must contain a marker | `{ "type": "grep", "file": "README.md", "pattern": "## Release notes" }` |
47
+ | there must be NO leftover markers | `{ "type": "grep", "file": "post.md", "pattern": "TODO", "absent": true }` — passes only when the pattern is absent. Pure text, no `--allow-commands`, cross-platform. Prefer this over a `! grep` command gate. |
48
+ | tests must pass | `{ "type": "command", "run": ["npm", "test"] }` (requires `--allow-commands`) |
49
+ | a build must succeed | `{ "type": "command", "run": ["npm", "run", "build"] }` (requires `--allow-commands`) |
50
+ | a command must FAIL to pass the gate | add `"expectExitZero": false` to the command check |
51
+
52
+ Gate verdicts:
53
+
54
+ - `pass` — the condition is met.
55
+ - `fail` — evaluated, condition not met (e.g. file missing, pattern absent, command exited wrong).
56
+ - `blocked` — could not evaluate: path escaped the project root, target file unreadable, command not permitted (`--allow-commands` missing) or failed to spawn / timed out.
57
+
58
+ ## Cross-domain examples (SOPs are not just for code)
59
+
60
+ The release example above is one domain. The same engine governs any gated workflow — often the higher-value use. Lead the interview with the user's own domain.
61
+
62
+ **Content publishing** (`~/.peaks/sops/blog-publish/sop.json`):
63
+
64
+ ```json
65
+ {
66
+ "id": "blog-publish",
67
+ "name": "Blog Publish",
68
+ "phases": ["draft", "edit", "publish"],
69
+ "gates": [
70
+ { "id": "draft-exists", "phase": "edit", "check": { "type": "file-exists", "path": "posts/current.md" } },
71
+ { "id": "no-placeholders", "phase": "publish", "check": { "type": "grep", "file": "posts/current.md", "pattern": "TODO|TKTK", "absent": true } }
72
+ ]
73
+ }
74
+ ```
75
+
76
+ Note `no-placeholders` uses `grep` with `absent: true` — "must not contain a placeholder" — so it needs no `--allow-commands` and works on any OS. This is the single most common non-engineering gate.
77
+
78
+ **Compliance / approval** (`~/.peaks/sops/vendor-approval/sop.json`):
79
+
80
+ ```json
81
+ {
82
+ "id": "vendor-approval",
83
+ "name": "Vendor Approval",
84
+ "phases": ["submitted", "reviewed", "approved"],
85
+ "gates": [
86
+ { "id": "review-notes", "phase": "reviewed", "check": { "type": "file-exists", "path": "vendors/acme/review.md" } },
87
+ { "id": "signed-off", "phase": "approved", "check": { "type": "grep", "file": "vendors/acme/review.md", "pattern": "Status:\\s*Approved" } }
88
+ ]
89
+ }
90
+ ```
91
+
92
+ **Data pipeline** (`~/.peaks/sops/dataset-release/sop.json`):
93
+
94
+ ```json
95
+ {
96
+ "id": "dataset-release",
97
+ "name": "Dataset Release",
98
+ "phases": ["raw", "cleaned", "validated"],
99
+ "gates": [
100
+ { "id": "schema-valid", "phase": "validated", "check": { "type": "command", "run": ["python", "scripts/validate.py", "data/cleaned.csv"] } }
101
+ ]
102
+ }
103
+ ```
104
+
105
+ These reuse the same three gate types — only the phases and the file/command targets differ. A human-judgment step ("the editor approved") is reified into a file/grep signal (an `approval.md` file, or a status line matching "Approved"), as shown above.
106
+
107
+ ## Guards: un-bypassable enforcement (optional)
108
+
109
+ A gate normally only blocks `peaks sop advance`. To make it physically un-bypassable by the agent, add **guards** that bind an irreversible Bash action to a phase, and have the user install the PreToolUse hook.
110
+
111
+ ```json
112
+ {
113
+ "id": "team-release",
114
+ "name": "Team Release",
115
+ "phases": ["draft", "review", "ship"],
116
+ "gates": [
117
+ { "id": "no-fixme", "phase": "ship", "check": { "type": "grep", "file": "src/index.ts", "pattern": "FIXME", "absent": true } }
118
+ ],
119
+ "guards": [
120
+ { "phase": "ship", "bash": "git +push" },
121
+ { "phase": "ship", "bash": "npm +publish" }
122
+ ]
123
+ }
124
+ ```
125
+
126
+ Semantics: a Bash command matching a guard's `bash` regex = entering that phase, so the phase's gates must pass first. With the hook installed (`peaks hooks install --project <repo>`), the agent literally cannot run `git push` / `npm publish` while `no-fixme` fails — Claude Code receives `permissionDecision: "deny"` before any permission check (holds even under `--dangerously-skip-permissions`).
127
+
128
+ `bash` rules:
129
+ - It is a **JS regex written inside JSON**. Escape backslashes: `"git\\s+push"`, or sidestep escaping with `"git +push"` (one-or-more spaces). `peaks sop lint` rejects an uncompilable regex (`GUARD_INVALID_PATTERN`) and a guard on an undeclared phase (`GUARD_PHASE_UNKNOWN`).
130
+ - Keep patterns specific to the irreversible action (`git +push`, `npm +publish`, `gh +release`, a deploy script name) — not broad verbs.
131
+
132
+ Override once (hotfix): `peaks gate bypass --sop team-release --phase ship --reason "<why>" --project <repo>` — consumed by the next blocked command, capped per project per SOP.
133
+
134
+ `command`-type gates run during enforcement with commands enabled (installing the hook is the consent); each gate keeps its 30s timeout. Enforcement **fails open** on any internal error — only a real gate failure denies.
135
+
136
+ ## Interview → generate → debug loop
137
+
138
+ 1. **Interview.** Ask: what are the ordered stages of this workflow? For each stage, what must be true before you're allowed to enter it? Translate "must be true" into file-exists / grep / command checks. For "must NOT contain X", reach for `grep` + `absent: true`.
139
+ 2. **Scaffold.** `peaks sop init --id <id> --name "<name>" --apply --json`. Writes a starter `sop.json` + `SKILL.md` into the global `~/.peaks/sops/<id>/` (no `--project`).
140
+ 3. **Write the real manifest.** Replace the scaffold's example phases/gates with the interviewed ones by editing `sop.json` directly.
141
+ 4. **Lint loop.** `peaks sop lint --id <id> --json` → read findings → fix in `sop.json` → re-lint until `ok:true`. Add `--allow-commands` when the SOP uses command gates.
142
+ 5. **Gate test.** `peaks sop check --id <id> --gate <gate-id> --project <repo> --json` for each gate, in both the good state (expect `pass`) and a bad state (expect `fail`/`blocked`). `--project` is the project the gate paths resolve against (default: current directory).
143
+ 6. **Dry-run the flow.** `peaks sop advance --id <id> --to <phase> --project <repo> --dry-run --json` — confirms a failing gate (or a forward phase skip) truly blocks, without recording anything.
144
+ 7. **Register.** `peaks sop register --id <id> --json` (preview with `--dry-run` first). Now the SOP is enumerable in the global registry and can be set as the active skill via presence.
145
+
146
+ ## Security notes (always surface to the user)
147
+
148
+ - `command` gates run user-defined commands. They are refused unless the user explicitly passes `--allow-commands` to `lint` / `register` / `check` / `advance`. Tell the user a SOP needs `--allow-commands` and what the command does before running it.
149
+ - Commands run with an argv array (no shell, no injection), a timeout cap, and cwd pinned to the project root. The command executable itself is not sandboxed — the trust boundary is whoever authored the SOP, the same as an npm script or Makefile target.
150
+ - `file-exists` / `grep` paths are confined inside the project root; an out-of-bounds path returns `blocked`, never reads outside the project.
151
+ - Side-effecting commands (`init` / `register` / `advance`) support `--dry-run` to preview without writing.
152
+
153
+ ## Bypassing a blocked advance
154
+
155
+ If the user must move forward despite a failing gate **or a forward phase skip** (e.g. a hotfix that skips review), advancement can be bypassed explicitly:
156
+
157
+ ```bash
158
+ peaks sop advance --id <id> --to <phase> --project <repo> --allow-incomplete --reason "<why>" --json
159
+ ```
160
+
161
+ `--allow-incomplete` bypasses both the gate checks and the phase-order check. In assisted/strict mode this also requires `--confirm`, and each SOP has a per-project bypass cap. Always record a real reason — the bypass is logged in that project's SOP history.