@se-studio/skills 1.10.27 → 1.14.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +37 -0
- package/agent-guard/README.md +34 -3
- package/agent-guard/bin.mjs +5 -1
- package/agent-guard/claude-pretool.mjs +21 -12
- package/agent-guard/doctor.mjs +369 -0
- package/agent-guard/merge-gate.mjs +416 -77
- package/agent-guard/rules.mjs +34 -2
- package/agent-guard/safe-merge.mjs +95 -0
- package/agent-guard/skill-links.mjs +130 -0
- package/package.json +1 -1
- package/references/agent-session/customer-agents-block.md +18 -8
- package/references/agent-session/customer-guardrails/README.md +4 -3
- package/references/agent-session/customer-guardrails/claude-merge-guard.mjs +239 -0
- package/references/agent-session/customer-guardrails/claude-settings.json +10 -1
- package/references/agent-session/fleet-audit.mjs +122 -22
- package/references/agent-session/merge-review-template.md +48 -0
- package/skills/coding-discipline/SKILL.md +17 -17
- package/skills/contentful-cms-generate-cms-guidelines/SKILL.md +1 -1
- package/skills/contentful-cms-schema-audit/SKILL.md +2 -2
- package/skills/contentful-cms-update-cms-guidelines/SKILL.md +1 -1
- package/skills/se-marketing-sites-customer-simplicity/SKILL.md +1 -0
- package/skills/se-marketing-sites-markdown-accuracy-review/SKILL.md +2 -2
- package/skills/se-marketing-sites-redirects/SKILL.md +67 -7
- package/skills/se-marketing-sites-smoke-test-setup/SKILL.md +1 -1
- package/skills/site-workflows-agent-session/SKILL.md +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,42 @@
|
|
|
1
1
|
# @se-studio/skills
|
|
2
2
|
|
|
3
|
+
## 1.14.0
|
|
4
|
+
|
|
5
|
+
### Minor Changes
|
|
6
|
+
|
|
7
|
+
- 4ec21f4: Merge gate fixes after the 19 Nov 2026 `pushed-through` merges (OM1 #106, Pedestal #128, PointMe #165, core #521/#522).
|
|
8
|
+
|
|
9
|
+
- **Precise failures.** `evaluateMergeGate` still returns `{ ok, problems }` and now also `checks: { id, ok, message, fix? }[]`. Every problem names the failed check (draft, do-not-merge title or label, PR not open, no approval, approval does not name the head, no review link, link at another PR or external, `#pullrequestreview-`, missing or minimized comment, self-link, no heading, heading without "review", no verdict, wrong or no SHA, review posted after the approval) and ends with a concrete `Fix:`. An approval that links several reviews reports each one; with several approvals at the head the newest is diagnosed. The CLI prints ` - [check-id] ...` bullets and a pointer to the templates.
|
|
10
|
+
- **Pre-merge check mode and no ungated merges.** `se-agent-guard merge-gate <pr> --check` (alias `--pre-merge`, plus `-R owner/repo` and `--json`) evaluates the open PR's current head, writes nothing, and exits 0, 1 or 2 (usage or lookup error with a clear message). New `se-agent-guard safe-merge <pr> [-R owner/repo] [--dry-run]` runs the check, then `gh pr merge --merge --match-head-commit <checked head>`, and refuses a production promote unless a human sets `SE_ALLOW_PRODUCTION_MERGE=1`. The Claude hook now fails closed when `gh` cannot be reached and denies MCP merge tools such as `mcp__github__merge_pull_request`; its matcher is `Bash|Edit|Write|NotebookEdit|mcp__.*`. New committed fallback `customer-guardrails/claude-merge-guard.mjs` (copy to `.claude/hooks/merge-guard.mjs`) guards merges when `node_modules` is missing. `doctor` and `fleet-audit` warn about a missing merge guard or a matcher without `mcp__`.
|
|
11
|
+
- **Mandatory review template.** The `## Code review of <head>` comment and `## Merge approval` comment, the run-the-check-first rule and the `safe-merge` path live in one canonical file, `references/agent-session/merge-review-template.md`, and `AGENTS.md` and the customer agents block (now v5) show the exact comments.
|
|
12
|
+
- **Review nits.** `gh pr -R o/r merge 5`, `gh pr --repo o/r merge 5` and `gh pr --repo=o/r merge 5` are now recognised as merges by the adapter and the fallback guard (flags and their values between `pr` and the action are skipped; the repo is captured). Any command that sets or passes `SE_ALLOW_PRODUCTION_MERGE` (inline, `env VAR=...`, `export`) is denied: it is for a human's own shell; the override is still read only from the process env. The MCP merge-tool matcher (same in both places) now denies merge verbs (`merge_pr`, `enable_auto_merge`, `pull_request_merge`, `mergePullRequest`, ...) and allows read-only `get_`/`list_`/`read_`/`search_` tools. The Claude hook's dead `?? shell` fallback is removed.
|
|
13
|
+
- **Behaviour change.** `merge-gate` now rejects unknown flags with exit 2; the old CLI silently ignored them. Customer scripts that pass extra flags must drop them.
|
|
14
|
+
|
|
15
|
+
## 1.13.1
|
|
16
|
+
|
|
17
|
+
### Patch Changes
|
|
18
|
+
|
|
19
|
+
- cd4c17c: Redirects skill: `skeleton:check` now enforces the live redirect path (the redirects-sync route and sitemap routes that call `getRedirectMap`), and the rollout checks can be skipped only with an expiry date and owner. PHILOSOPHY R6.9 to R6.11 (live redirects, the fail-closed sync route, project-scoped credentials) are approved.
|
|
20
|
+
|
|
21
|
+
## 1.13.0
|
|
22
|
+
|
|
23
|
+
### Minor Changes
|
|
24
|
+
|
|
25
|
+
- 24262f4: `fleet-audit` flags a site that still carries a local `scripts/check-cms-driven.mjs`, a `cms-driven.baseline.json` with more than 25 violations ("needs work"), and a baseline that has not changed in 90 days with debt still in it.
|
|
26
|
+
- 4163446: `fleet-audit` lists sites in remake mode with their deadline and flags a remake window that has passed but is still in `cms-driven.config.json`. The customer-simplicity skill points at the new `check-cms-driven` R3/R4 enforcement.
|
|
27
|
+
|
|
28
|
+
## 1.12.0
|
|
29
|
+
|
|
30
|
+
### Minor Changes
|
|
31
|
+
|
|
32
|
+
- dfa21ee: New `se-agent-guard doctor`: checks that a checkout is actually protected. It runs the guard-health checks from `fleet-audit` on the working tree and adds local-only ones: missing `node_modules`, installed version differs from the exact pin (or not installed), git not running the shipped hooks (or a hook not executable), the guard bin missing, dangling skill links in `.agents/skills` and the per-agent directories, stale copied skills left by `skills add`, and broken relative links in installed or repo-owned skills (resolved from the skill's real location). Exit 0 healthy, 1 problems, 2 could not run. It reads only small regular files whose real path stays under the repo or its `node_modules`, so a hostile repo can neither hang it with a device or FIFO symlink nor make it read files outside the repo. It reads `CI` the same way the guard does. `fleet-audit`'s file selection is now the exported `wantedPaths`, shared with the doctor.
|
|
33
|
+
|
|
34
|
+
## 1.11.0
|
|
35
|
+
|
|
36
|
+
### Minor Changes
|
|
37
|
+
|
|
38
|
+
- 3b685dd: `pnpm skills:validate` now fails on broken relative links in every markdown file of a skill (`SKILL.md` and nested references). A link must point at a file with exactly matching case, and a link in a published skill must point at something the package actually ships (its `files` list: `skills`, `references`, `agent-guard`, root `*.md`), so a core-repo path or a `core-only` skill can never be linked. The checker (`agent-guard/skill-links.mjs`) skips URLs, anchors, templates and code, and reports a malformed `%` escape as a broken link instead of crashing. Fixes two wrong paths and turns core-repo paths in `coding-discipline` and five other skills (plus one directory link) into plain code text.
|
|
39
|
+
|
|
3
40
|
## 1.10.27
|
|
4
41
|
|
|
5
42
|
### Patch Changes
|
package/agent-guard/README.md
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
Agent-neutral guards, published in `@se-studio/skills` as the `se-agent-guard` bin so customer repos run the version their lockfile pins instead of a copy.
|
|
4
4
|
|
|
5
5
|
```
|
|
6
|
-
se-agent-guard commit | push | claude-pretool | merge-gate <pr> [--audit] | worktrees-clean [--apply] [--remote]
|
|
6
|
+
se-agent-guard commit | push | claude-pretool | merge-gate <pr> [--check|--audit] [-R owner/repo] [--json] | safe-merge <pr> [-R owner/repo] [--dry-run] | worktrees-clean [--apply] [--remote]
|
|
7
7
|
```
|
|
8
8
|
|
|
9
9
|
Customer repos copy the thin files that call the bin, plus the site CI caller: see `references/agent-session/customer-guardrails/`. Core uses the same gate code from `packages/skills/agent-guard/`. Core's own lint, typecheck, and tests stay on CI Dev; customer repos call `.github/workflows/site-ci.yml`.
|
|
@@ -27,12 +27,42 @@ Grok and Cursor rely on the git hooks and the CI audit. Add thin adapters that c
|
|
|
27
27
|
- Denies a push or delete of `main`/`master`/`production` (including `--all`, `--mirror`, quoted refs, `HEAD:main`), manual `vercel deploy/promote/rollback/--prod` (bare `vercel` too), `cms-edit:deploy`, `--no-verify`, and `-c`/`--config`/`--config-env` `core.hooksPath=...` (any spelling).
|
|
28
28
|
- Denies `gh api .../pulls/N/merge`: merge through `gh pr merge` so the gate can check it.
|
|
29
29
|
- Asks before a force-push and before editing on a primary checkout that is on an integration branch.
|
|
30
|
-
- For `gh pr merge`, checks **every** merge in the command against the merge gate. Any failure denies.
|
|
30
|
+
- For `gh pr merge`, checks **every** merge in the command against the merge gate. Any failure denies, and the deny reason carries the gate's problems verbatim (each names the failed check and a `Fix:`). **Fail closed:** if `gh` cannot be reached (missing, offline, not authenticated) the merge is denied too, with a reason that says to run `se-agent-guard merge-gate <pr> --check` and fix `gh`. When all pass it allows without asking: the approval comment is the confirmation.
|
|
31
|
+
- Denies any MCP tool that merges a PR (`/^mcp__.*merge.*pull|^mcp__.*pull.*merge|^mcp__.*merge_pull_request/i`, e.g. `mcp__github__merge_pull_request`): merge only through `gh pr merge` or `se-agent-guard safe-merge` so the gate runs. Every other MCP tool is allowed silently. This needs the `mcp__.*` matcher in `.claude/settings.json`.
|
|
31
32
|
- A malformed hook payload exits 0 (never breaks the session).
|
|
32
33
|
|
|
34
|
+
## Doctor: is this checkout protected?
|
|
35
|
+
|
|
36
|
+
```bash
|
|
37
|
+
pnpm exec se-agent-guard doctor
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
Run it in a repo or worktree whose **root** `package.json` has `@se-studio/skills` as a dependency (the hooks run `pnpm exec se-agent-guard` from the repo root; without it you get a single `skills-dependency-missing` finding). It exits 0 when healthy, 1 with one line per problem, and 2 when it could not run (not a git repo, git missing, or an unexpected failure). `fleet-audit` finds the same class of problem from remote refs; the doctor finds it on the machine where it bites, including uncommitted state.
|
|
41
|
+
|
|
42
|
+
It reuses the guard-health checks from `fleet-audit` on the working tree (a hook that does not call the guard, a missing Claude hook or merge audit, a pin that is not exact, skills linked by the old sync, tracked skill symlinks) and adds checks only a local run can do:
|
|
43
|
+
|
|
44
|
+
| Finding | Meaning |
|
|
45
|
+
|---|---|
|
|
46
|
+
| `node-modules-missing` | no install: the git hooks fail closed, the Claude hook fails open |
|
|
47
|
+
| `pin-not-installed` | `node_modules` holds a different version than the exact pin in `package.json` |
|
|
48
|
+
| `guard-bin-missing` | `node_modules/.bin/se-agent-guard` is absent, so the hooks cannot run |
|
|
49
|
+
| `hooks-not-installed`, `hook-not-active` | the repo ships a hook but git is not running it (`core.hooksPath` unset, or no wrapper for it) |
|
|
50
|
+
| `hook-not-executable` | the active hook file lacks the execute bit |
|
|
51
|
+
| `skills-not-linked`, `skills-link-dangling` | `.agents/skills` is empty or has links to nothing, in it or in `.claude/skills`, `.cursor/skills`, `.grok/skills` (run `skills-npm --yes`) |
|
|
52
|
+
| `skills-stale-copy` | a package skill in `.agents/skills` or an agent skills directory is a real copy (left by `skills add`), not a link into the pinned package |
|
|
53
|
+
| `skill-link-broken`, `own-skill-link-broken`, `skill-unreadable` | a relative link in an installed skill, or in a skill the repo owns (including per-entry skills under `.claude/skills`), points at a missing file, or a `SKILL.md` cannot be read. Links are resolved from the skill's real location, the way an agent reads them |
|
|
54
|
+
|
|
55
|
+
The skills checks are skipped when `CI` is set (anything but `false`, the same rule the guard uses) or `VERCEL` is set, because `skills-npm` does not run there. Every file the doctor reads must be a regular file under 1 MB whose real path stays under the repo or its `node_modules` (which a worktree may symlink to a shared install), so a hook or `SKILL.md` that is a symlink to a device, a FIFO or a file outside the repo is not read. Review-doc copies, wording and deploy-script findings stay in `fleet-audit`: they are hygiene, not guard health.
|
|
56
|
+
|
|
33
57
|
## Merge gate
|
|
34
58
|
|
|
35
|
-
|
|
59
|
+
**Check mode (run this before any merge, by any bot, agent, tool or script).** `se-agent-guard merge-gate <pr> --check` (alias `--pre-merge`; running with neither `--check` nor `--audit` is the same) reads the open PR's current head and **writes nothing**. It prints `merge-gate: PR #N OK at <head 12> — safe to merge with: gh pr merge N --merge --match-head-commit <full head>` and exits 0, or exits 1 with one bullet per failed check, ` - [check-id] <what failed> Fix: <what to do>`, then a line pointing at the templates. Exit 2 means it could not run (usage error, `gh` missing, not authenticated, PR not found) and says which. `-R OWNER/REPO` picks the repository when not run inside it; `--json` prints `{ ok, pr, head, checks, problems }` (`checks` is `{ id, ok, message, fix? }[]` for every check that ran). `--check` with `--audit` is a usage error. Check ids: `state`, `draft`, `do-not-merge-title`, `do-not-merge-label`, `approval-missing`, `approval-head`, `approval-review-link`, `review-link-target`, `review-pr-review`, `review-exists`, `review-self`, `review-heading`, `review-verdict`, `review-sha`, `review-order`. When an approval links several reviews, every candidate's failure is reported; with several approvals at the head, the newest is diagnosed.
|
|
60
|
+
|
|
61
|
+
**`se-agent-guard safe-merge <pr> [-R owner/repo] [--dry-run]`** runs that check and, only if it passes, `gh pr merge <n> [-R repo] --merge --match-head-commit <the head it checked>`: merge commits only, and a push between the check and the merge makes the merge fail. It refuses a production promote (base `main` or `production`) unless a human set `SE_ALLOW_PRODUCTION_MERGE=1` in the process environment. `--dry-run` prints the command and does not run it.
|
|
62
|
+
|
|
63
|
+
The review comment and approval templates are in `references/agent-session/merge-review-template.md` (mandatory, Nick, 19 Nov 2026).
|
|
64
|
+
|
|
65
|
+
What the gate checks: the PR needs a `## Merge approval` comment that names the current head SHA and cites a review. **Anyone** may post it (Nick's decision, 30 Oct 2026: what matters is that the PR was reviewed, not who clicks approve), so the author is not checked. The review is: a link into this PR to a review comment (agent or human; its first heading says "review", it names the head SHA, it gives a verdict such as PASS/NITS/BLOCKER/LGTM, and it was posted strictly before the approval), or a line that is exactly `none, Nick override`. The head SHA match is the full SHA or a prefix of at least 12 hex characters. A later lint, typecheck, test, or format push may keep that review only when the review names the current head that way; a different hex string shared with the approval does not. A production promote (`dev` → `main`, or `develop` → `main` / `production`, and `isCrossRepository` false) may instead use a whole line `CI covers the changes since` and does not need a review comment on that PR (Nick, 4 Nov 2026: no second AI review; CI covers the commits since the feature review). `dev` → `production` and a fork head do not. Words after that line do not count. Links to PR reviews (`#pullrequestreview-N`) are not accepted because `gh` returns no numeric review id to match them to; post the review as a comment. A bare "reviewed", an external link, a link to a comment that does not exist, a review of an older SHA, a `#pullrequestreview-` link, a minimized comment, or the approval citing itself all fail. Also: not a draft, not "do not merge", and (check mode only) open. `--audit` labels `pushed-through` after a breach (same problem strings in the comment) and says that no post-merge AI review will run.
|
|
36
66
|
|
|
37
67
|
`gh` returns no `updatedAt` for comments, so a review comment edited after the approval (to add the SHA, say) cannot be detected. It cannot tell that the review was any good, or that the approver is Nick; someone determined can fabricate a review comment. It makes the honest path easy and the lazy fake fail. It reads comments through `gh pr view --json comments`; very long threads rely on `gh` paging them.
|
|
38
68
|
|
|
@@ -43,5 +73,6 @@ Read from the environment of the process that runs the hook, never from command
|
|
|
43
73
|
- `SE_ALLOW_PROTECTED_COMMIT=1` commit on an integration branch (explicit "work in place")
|
|
44
74
|
- `SE_ALLOW_PROTECTED_PUSH=1` push to a production branch (after ask + confirm)
|
|
45
75
|
- `SE_ALLOW_PRIMARY_EDIT=1` stop Claude asking before edits on a primary checkout
|
|
76
|
+
- `SE_ALLOW_PRODUCTION_MERGE=1` let `safe-merge` merge into `main` or `production` (after the ask + confirm gate in `production-merge.md`)
|
|
46
77
|
|
|
47
78
|
`CI` set to anything but `false` skips the commit and push hooks.
|
package/agent-guard/bin.mjs
CHANGED
|
@@ -5,9 +5,11 @@
|
|
|
5
5
|
* se-agent-guard commit git pre-commit hook
|
|
6
6
|
* se-agent-guard push git pre-push hook (ref lines on stdin)
|
|
7
7
|
* se-agent-guard claude-pretool Claude Code PreToolUse hook (payload on stdin)
|
|
8
|
-
* se-agent-guard merge-gate <pr> [--audit
|
|
8
|
+
* se-agent-guard merge-gate <pr> [--check] [-R owner/repo] [--json] | --audit pre-merge check (no writes) / post-merge audit
|
|
9
|
+
* se-agent-guard safe-merge <pr> [-R owner/repo] [--dry-run] check, then merge with --match-head-commit
|
|
9
10
|
* se-agent-guard core-request --from <key> --feature <slug> --why "..." file a tracked Core request
|
|
10
11
|
* se-agent-guard worktrees-clean [--apply] [--remote] remove merged worktrees and branches
|
|
12
|
+
* se-agent-guard doctor is this checkout protected? hooks, installs, pins, skills links
|
|
11
13
|
*/
|
|
12
14
|
import { spawnSync } from 'node:child_process';
|
|
13
15
|
import path from 'node:path';
|
|
@@ -18,8 +20,10 @@ const SUBCOMMANDS = {
|
|
|
18
20
|
push: ['cli.mjs', ['push']],
|
|
19
21
|
'claude-pretool': ['claude-pretool.mjs', []],
|
|
20
22
|
'merge-gate': ['merge-gate.mjs', []],
|
|
23
|
+
'safe-merge': ['safe-merge.mjs', []],
|
|
21
24
|
'core-request': ['core-request.mjs', []],
|
|
22
25
|
'worktrees-clean': ['worktrees-clean.mjs', []],
|
|
26
|
+
doctor: ['doctor.mjs', []],
|
|
23
27
|
};
|
|
24
28
|
|
|
25
29
|
const [sub, ...rest] = process.argv.slice(2);
|
|
@@ -7,8 +7,8 @@
|
|
|
7
7
|
import { execFileSync } from 'node:child_process';
|
|
8
8
|
import fs from 'node:fs';
|
|
9
9
|
import path from 'node:path';
|
|
10
|
-
import { evaluateMergeGate, fetchPr } from './merge-gate.mjs';
|
|
11
|
-
import { classifyEdit, classifyShellCommand } from './rules.mjs';
|
|
10
|
+
import { describeLookupError, evaluateMergeGate, fetchPr } from './merge-gate.mjs';
|
|
11
|
+
import { classifyEdit, classifyShellCommand, MCP_MERGE_REASON, MCP_MERGE_TOOL } from './rules.mjs';
|
|
12
12
|
|
|
13
13
|
function git(cwd, args) {
|
|
14
14
|
try {
|
|
@@ -53,23 +53,30 @@ try {
|
|
|
53
53
|
const cwd = payload.cwd || process.cwd();
|
|
54
54
|
const input = payload.tool_input || {};
|
|
55
55
|
|
|
56
|
-
/**
|
|
56
|
+
/**
|
|
57
|
+
* Every merge in the command must pass the gate; any failure denies. Fail closed: when the gate
|
|
58
|
+
* cannot run (gh missing, offline, not authenticated) the merge is denied too, never left to a prompt.
|
|
59
|
+
*/
|
|
57
60
|
function mergeDecision(merges) {
|
|
58
|
-
|
|
61
|
+
const lookupFailures = [];
|
|
59
62
|
for (const { target, repo } of merges) {
|
|
60
63
|
try {
|
|
61
|
-
const gate = evaluateMergeGate(fetchPr(target, { cwd, repo }));
|
|
64
|
+
const gate = evaluateMergeGate(fetchPr(target, { cwd, repo }), { requireOpen: true });
|
|
62
65
|
if (!gate.ok) {
|
|
63
66
|
return {
|
|
64
67
|
decision: 'deny',
|
|
65
|
-
reason: `Not mergeable yet (${target ?? 'current branch'})
|
|
68
|
+
reason: `Not mergeable yet (${target ?? 'current branch'}), do not merge:\n${gate.problems.map((p) => `- ${p}`).join('\n')}\nFix exactly what is named, then run \`se-agent-guard merge-gate ${target ?? '<pr>'} --check\` before merging.`,
|
|
66
69
|
};
|
|
67
70
|
}
|
|
68
|
-
} catch {
|
|
69
|
-
|
|
71
|
+
} catch (e) {
|
|
72
|
+
lookupFailures.push(`${target ?? 'current branch'}: ${describeLookupError(e, target, repo)}`);
|
|
70
73
|
}
|
|
71
74
|
}
|
|
72
|
-
|
|
75
|
+
if (lookupFailures.length === 0) return { decision: 'allow' };
|
|
76
|
+
return {
|
|
77
|
+
decision: 'deny',
|
|
78
|
+
reason: `The merge gate could not run, so the merge is denied (fail closed). ${lookupFailures.join(' ')} Fix gh, then run \`se-agent-guard merge-gate <pr> --check\` and merge only if it passes.`,
|
|
79
|
+
};
|
|
73
80
|
}
|
|
74
81
|
|
|
75
82
|
if (payload.tool_name === 'Bash') {
|
|
@@ -80,10 +87,12 @@ if (payload.tool_name === 'Bash') {
|
|
|
80
87
|
// A hard deny wins. Otherwise a merge is decided by the real state of every PR it names;
|
|
81
88
|
// when all pass the human confirm is intentionally skipped (the approval comment is the confirm).
|
|
82
89
|
const { decision, reason } =
|
|
83
|
-
shell.decision === 'deny' || shell.merges.length === 0
|
|
84
|
-
? shell
|
|
85
|
-
: (mergeDecision(shell.merges) ?? shell);
|
|
90
|
+
shell.decision === 'deny' || shell.merges.length === 0 ? shell : mergeDecision(shell.merges);
|
|
86
91
|
respond(decision, reason);
|
|
92
|
+
} else if (MCP_MERGE_TOOL.test(String(payload.tool_name || ''))) {
|
|
93
|
+
respond('deny', MCP_MERGE_REASON);
|
|
94
|
+
} else if (String(payload.tool_name || '').startsWith('mcp__')) {
|
|
95
|
+
// Every other MCP tool is allowed silently (the matcher sends them here only to catch merge tools).
|
|
87
96
|
} else {
|
|
88
97
|
const target = input.file_path || input.notebook_path;
|
|
89
98
|
if (target) {
|
|
@@ -0,0 +1,369 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
/**
|
|
3
|
+
* `se-agent-guard doctor`: is this checkout actually protected? Run it in a repo (or worktree)
|
|
4
|
+
* that depends on @se-studio/skills. `fleet-audit` finds these problems from remote refs; this
|
|
5
|
+
* finds them on the machine where they bite: a hook git is not running, a missing node_modules,
|
|
6
|
+
* a skills link that points at nothing.
|
|
7
|
+
*
|
|
8
|
+
* The policy checks are `auditFiles` from fleet-audit, fed the working tree instead of a remote
|
|
9
|
+
* ref, so there is one definition of "a correct customer repo". The rest are local-only.
|
|
10
|
+
* Everything it reads is repo-controlled, so every read goes through `isReadable`: the file must
|
|
11
|
+
* be regular (not a device or FIFO, which would hang a read), small, and its real path must stay
|
|
12
|
+
* under the repo or its node_modules. Content from outside is never read, so it cannot be printed.
|
|
13
|
+
*
|
|
14
|
+
* se-agent-guard doctor exit 0 healthy, 1 problems found, 2 could not run
|
|
15
|
+
*/
|
|
16
|
+
import { execFileSync } from 'node:child_process';
|
|
17
|
+
import fs from 'node:fs';
|
|
18
|
+
import os from 'node:os';
|
|
19
|
+
import path from 'node:path';
|
|
20
|
+
import { fileURLToPath } from 'node:url';
|
|
21
|
+
import { auditFiles, wantedPaths } from '../references/agent-session/fleet-audit.mjs';
|
|
22
|
+
import { inCi } from './rules.mjs';
|
|
23
|
+
import { findBrokenLinks } from './skill-links.mjs';
|
|
24
|
+
|
|
25
|
+
const SKILLS = '@se-studio/skills';
|
|
26
|
+
const HOOKS = ['pre-commit', 'pre-push'];
|
|
27
|
+
const AGENT_SKILL_DIRS = ['.claude/skills', '.cursor/skills', '.grok/skills'];
|
|
28
|
+
const MAX_FILE_BYTES = 1024 * 1024;
|
|
29
|
+
const MAX_GIT_OUTPUT = 64 * 1024 * 1024;
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* The fleet checks that say whether the guard and the pinned skills work. The rest of
|
|
33
|
+
* `auditFiles` (review-doc copies, wording, deploy scripts) is fleet hygiene: `fleet-audit`
|
|
34
|
+
* reports it, and burying a broken hook under it would hide the one thing doctor is for.
|
|
35
|
+
*/
|
|
36
|
+
export const GUARD_HEALTH_FINDINGS = new Set([
|
|
37
|
+
'hook-commit-missing',
|
|
38
|
+
'hook-push-missing',
|
|
39
|
+
'claude-hook-missing',
|
|
40
|
+
'claude-mcp-matcher-missing',
|
|
41
|
+
'merge-guard-missing',
|
|
42
|
+
'merge-audit-missing',
|
|
43
|
+
'pin-not-exact',
|
|
44
|
+
'skills-too-old',
|
|
45
|
+
'skills-npm-missing',
|
|
46
|
+
'skills-sync-legacy',
|
|
47
|
+
'skills-links-tracked',
|
|
48
|
+
'skills-update-script',
|
|
49
|
+
]);
|
|
50
|
+
|
|
51
|
+
const safeJson = (text) => {
|
|
52
|
+
try {
|
|
53
|
+
return JSON.parse(text);
|
|
54
|
+
} catch {
|
|
55
|
+
return null;
|
|
56
|
+
}
|
|
57
|
+
};
|
|
58
|
+
|
|
59
|
+
function git(root, args) {
|
|
60
|
+
return execFileSync('git', args, {
|
|
61
|
+
cwd: root,
|
|
62
|
+
encoding: 'utf8',
|
|
63
|
+
maxBuffer: MAX_GIT_OUTPUT,
|
|
64
|
+
stdio: ['ignore', 'pipe', 'ignore'],
|
|
65
|
+
});
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/** Does the path exist once symlinks are followed? A dangling link is false. */
|
|
69
|
+
const resolves = (p) => fs.existsSync(p);
|
|
70
|
+
|
|
71
|
+
const within = (real, base) => real === base || real.startsWith(base + path.sep);
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* Is it safe to read? A regular file (a device or FIFO would hang the read), not huge, and whose
|
|
75
|
+
* real path is under one of `bases`.
|
|
76
|
+
* @param {string} file
|
|
77
|
+
* @param {string[]} bases real paths
|
|
78
|
+
*/
|
|
79
|
+
function isReadable(file, bases) {
|
|
80
|
+
try {
|
|
81
|
+
const stat = fs.statSync(file);
|
|
82
|
+
if (!stat.isFile() || stat.size > MAX_FILE_BYTES) return false;
|
|
83
|
+
const real = fs.realpathSync(file);
|
|
84
|
+
return bases.some((base) => within(real, base));
|
|
85
|
+
} catch {
|
|
86
|
+
return false;
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
const readRegular = (file, bases) =>
|
|
91
|
+
isReadable(file, bases) ? fs.readFileSync(file, 'utf8') : null;
|
|
92
|
+
|
|
93
|
+
const readJson = (file, bases) => safeJson(readRegular(file, bases) ?? '');
|
|
94
|
+
|
|
95
|
+
const realOrNull = (p) => {
|
|
96
|
+
try {
|
|
97
|
+
return fs.realpathSync(p);
|
|
98
|
+
} catch {
|
|
99
|
+
return null;
|
|
100
|
+
}
|
|
101
|
+
};
|
|
102
|
+
|
|
103
|
+
function listDir(dir) {
|
|
104
|
+
try {
|
|
105
|
+
return fs.readdirSync(dir).filter((name) => !name.startsWith('.'));
|
|
106
|
+
} catch {
|
|
107
|
+
return []; // missing, or not a directory
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
/**
|
|
112
|
+
* The skills the agents can see: entries of .agents/skills and of the per-agent directories,
|
|
113
|
+
* each once (the agent directories usually link to .agents/skills).
|
|
114
|
+
* @returns {{ dangling: string[], skills: { name: string, rel: string, real: string, isLink: boolean }[] }}
|
|
115
|
+
*/
|
|
116
|
+
function visibleSkills(root) {
|
|
117
|
+
const dangling = [];
|
|
118
|
+
const skills = [];
|
|
119
|
+
const seen = new Set();
|
|
120
|
+
for (const dirRel of ['.agents/skills', ...AGENT_SKILL_DIRS]) {
|
|
121
|
+
const dir = path.join(root, dirRel);
|
|
122
|
+
try {
|
|
123
|
+
fs.lstatSync(dir);
|
|
124
|
+
} catch {
|
|
125
|
+
continue; // the directory is optional
|
|
126
|
+
}
|
|
127
|
+
if (!resolves(dir)) {
|
|
128
|
+
dangling.push(dirRel);
|
|
129
|
+
continue;
|
|
130
|
+
}
|
|
131
|
+
for (const name of listDir(dir)) {
|
|
132
|
+
const entry = path.join(dir, name);
|
|
133
|
+
const rel = `${dirRel}/${name}`;
|
|
134
|
+
if (!resolves(entry)) {
|
|
135
|
+
dangling.push(rel);
|
|
136
|
+
continue;
|
|
137
|
+
}
|
|
138
|
+
const real = fs.realpathSync(entry);
|
|
139
|
+
if (seen.has(real)) continue;
|
|
140
|
+
seen.add(real);
|
|
141
|
+
skills.push({ name, rel, real, isLink: fs.lstatSync(entry).isSymbolicLink() });
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
return { dangling, skills };
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
/**
|
|
148
|
+
* @param {{ root: string, env?: Record<string, string | undefined> }} input root: the repo top level
|
|
149
|
+
* @returns {{ findings: { id: string, text: string }[] }}
|
|
150
|
+
*/
|
|
151
|
+
export function diagnose({ root, env = process.env }) {
|
|
152
|
+
const findings = [];
|
|
153
|
+
const add = (id, text) => findings.push({ id, text });
|
|
154
|
+
|
|
155
|
+
// Paths come NUL-separated: without -z git quotes non-ASCII names and they would never match.
|
|
156
|
+
const tree = git(root, ['ls-files', '-s', '-z'])
|
|
157
|
+
.split('\0')
|
|
158
|
+
.filter(Boolean)
|
|
159
|
+
.map((entry) => ({
|
|
160
|
+
mode: entry.slice(0, entry.indexOf(' ')),
|
|
161
|
+
path: entry.slice(entry.indexOf('\t') + 1),
|
|
162
|
+
}));
|
|
163
|
+
const untracked = git(root, ['ls-files', '-o', '--exclude-standard', '-z'])
|
|
164
|
+
.split('\0')
|
|
165
|
+
.filter(Boolean);
|
|
166
|
+
const rootReal = fs.realpathSync(root);
|
|
167
|
+
const modules = path.join(root, 'node_modules');
|
|
168
|
+
const modulesReal = realOrNull(modules); // a worktree may symlink this to a shared install
|
|
169
|
+
const bases = modulesReal ? [rootReal, modulesReal] : [rootReal];
|
|
170
|
+
const files = {};
|
|
171
|
+
for (const rel of wantedPaths([...tree.map((t) => t.path), ...untracked])) {
|
|
172
|
+
const body = readRegular(path.join(root, rel), [rootReal]);
|
|
173
|
+
if (body !== null) files[rel] = body;
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
const pkg = files['package.json'] ? safeJson(files['package.json']) : null;
|
|
177
|
+
const deps = { ...pkg?.dependencies, ...pkg?.devDependencies };
|
|
178
|
+
if (!(SKILLS in deps)) {
|
|
179
|
+
// Everything below assumes the pinned guard is installed from the root package.json.
|
|
180
|
+
const source = readJson(path.join(root, 'packages/skills/package.json'), [rootReal]);
|
|
181
|
+
add(
|
|
182
|
+
'skills-dependency-missing',
|
|
183
|
+
!pkg
|
|
184
|
+
? 'package.json is missing or not valid JSON, so there is no pinned guard to check'
|
|
185
|
+
: source?.name === SKILLS
|
|
186
|
+
? `this is the source repo of ${SKILLS}: doctor is for repos that depend on it`
|
|
187
|
+
: `${SKILLS} is not a dependency in the root package.json. The hooks run \`pnpm exec se-agent-guard\` from the repo root, so it must be a root devDependency at an exact version`,
|
|
188
|
+
);
|
|
189
|
+
return { findings };
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
// 1. The shared policy checks, on the working tree (uncommitted state included).
|
|
193
|
+
// No remote to compare with: skip "behind core" and "block stale".
|
|
194
|
+
for (const f of auditFiles({ files, tree, coreBlockVersion: 0, coreVersions: {} }).findings) {
|
|
195
|
+
if (GUARD_HEALTH_FINDINGS.has(f.id)) add(f.id, f.text);
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
// 2. Dependencies are installed, and at the pinned version.
|
|
199
|
+
if (!fs.existsSync(modules)) {
|
|
200
|
+
add(
|
|
201
|
+
'node-modules-missing',
|
|
202
|
+
'node_modules is missing: run `pnpm install`. The git hooks fail closed without it (commits are refused); the Claude hook fails open.',
|
|
203
|
+
);
|
|
204
|
+
} else {
|
|
205
|
+
for (const [name, spec] of Object.entries(deps)) {
|
|
206
|
+
if (!name.startsWith('@se-studio/') || String(spec).startsWith('workspace:')) continue;
|
|
207
|
+
if (!/^\d+\.\d+\.\d+/.test(String(spec))) continue; // a range is reported by pin-not-exact
|
|
208
|
+
const installed = readJson(path.join(modules, name, 'package.json'), bases)?.version;
|
|
209
|
+
if (installed === undefined) {
|
|
210
|
+
add(
|
|
211
|
+
'pin-not-installed',
|
|
212
|
+
`${name} is pinned ${spec} but is not installed: run \`pnpm install\``,
|
|
213
|
+
);
|
|
214
|
+
} else if (installed !== spec) {
|
|
215
|
+
add(
|
|
216
|
+
'pin-not-installed',
|
|
217
|
+
`${name} is pinned ${spec} but ${installed} is installed: run \`pnpm install\``,
|
|
218
|
+
);
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
if (!resolves(path.join(modules, '.bin', 'se-agent-guard'))) {
|
|
222
|
+
add(
|
|
223
|
+
'guard-bin-missing',
|
|
224
|
+
'node_modules/.bin/se-agent-guard is missing: the hooks call it and will fail. Run `pnpm install`',
|
|
225
|
+
);
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
// 3. Git is actually running the hooks the repo ships.
|
|
230
|
+
const present = HOOKS.filter((name) => `.husky/${name}` in files || `.githooks/${name}` in files);
|
|
231
|
+
if (present.length > 0) {
|
|
232
|
+
let hooksPath = '';
|
|
233
|
+
try {
|
|
234
|
+
hooksPath = git(root, ['config', 'core.hooksPath']).trim();
|
|
235
|
+
} catch {
|
|
236
|
+
// unset
|
|
237
|
+
}
|
|
238
|
+
// Plain `git config` (--type=path needs git 2.18): expand a leading ~ the way git would.
|
|
239
|
+
if (hooksPath === '~' || hooksPath.startsWith('~/')) {
|
|
240
|
+
hooksPath = path.join(os.homedir(), hooksPath.slice(1));
|
|
241
|
+
}
|
|
242
|
+
if (!hooksPath) {
|
|
243
|
+
add(
|
|
244
|
+
'hooks-not-installed',
|
|
245
|
+
'git core.hooksPath is not set, so the pre-commit and pre-push guards do not run: run `pnpm install` (the `prepare` script sets it)',
|
|
246
|
+
);
|
|
247
|
+
} else {
|
|
248
|
+
const active = path.resolve(root, hooksPath);
|
|
249
|
+
for (const name of present) {
|
|
250
|
+
const hook = path.join(active, name);
|
|
251
|
+
if (!resolves(hook)) {
|
|
252
|
+
add(
|
|
253
|
+
'hook-not-active',
|
|
254
|
+
`the repo has a ${name} hook but core.hooksPath (${hooksPath}) has none, so git does not run it: run \`pnpm install\``,
|
|
255
|
+
);
|
|
256
|
+
} else if (!(fs.statSync(hook).mode & 0o111)) {
|
|
257
|
+
add(
|
|
258
|
+
'hook-not-executable',
|
|
259
|
+
`${hooksPath}/${name} is not executable: run \`chmod +x ${hooksPath}/${name}\``,
|
|
260
|
+
);
|
|
261
|
+
}
|
|
262
|
+
}
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
// 4. Skills are linked from the installed package. skills-npm does not run on Vercel or CI.
|
|
267
|
+
if (!inCi(env) && !env.VERCEL && fs.existsSync(path.join(modules, SKILLS))) {
|
|
268
|
+
if (listDir(path.join(root, '.agents/skills')).length === 0) {
|
|
269
|
+
add(
|
|
270
|
+
'skills-not-linked',
|
|
271
|
+
'.agents/skills is missing or empty: run `pnpm exec skills-npm --yes`',
|
|
272
|
+
);
|
|
273
|
+
}
|
|
274
|
+
const { dangling, skills } = visibleSkills(root);
|
|
275
|
+
if (dangling.length > 0) {
|
|
276
|
+
add(
|
|
277
|
+
'skills-link-dangling',
|
|
278
|
+
`${dangling.length} skill link(s) point at nothing (${dangling.slice(0, 3).join(', ')}): run \`pnpm install\`, then \`pnpm exec skills-npm --yes\``,
|
|
279
|
+
);
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
// A real directory in .agents/skills that shares a name with a package skill is a copy left
|
|
283
|
+
// by `skills add`, not a link: its relative links no longer resolve, and it never updates.
|
|
284
|
+
const packaged = new Set(listDir(path.join(modules, SKILLS, 'skills')));
|
|
285
|
+
const copies = skills.filter((s) => !s.isLink && packaged.has(s.name));
|
|
286
|
+
if (copies.length > 0) {
|
|
287
|
+
add(
|
|
288
|
+
'skills-stale-copy',
|
|
289
|
+
`${copies.length} skill(s) are copies, not links into the pinned package (${copies
|
|
290
|
+
.slice(0, 3)
|
|
291
|
+
.map((s) => s.name)
|
|
292
|
+
.join(', ')}): delete them and run \`pnpm exec skills-npm --yes\``,
|
|
293
|
+
);
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
// Links inside a skill resolve from the skill's real location: .agents/skills/<name> is a
|
|
297
|
+
// symlink into node_modules, and `../../references/x.md` means the package's references/.
|
|
298
|
+
const fromPackage = [];
|
|
299
|
+
const fromRepo = [];
|
|
300
|
+
const unreadable = [];
|
|
301
|
+
for (const skill of skills.filter((s) => !copies.includes(s))) {
|
|
302
|
+
const file = path.join(skill.real, 'SKILL.md');
|
|
303
|
+
if (!resolves(file)) continue;
|
|
304
|
+
// The link checker reads the file itself, so apply the same guard first.
|
|
305
|
+
if (!isReadable(file, bases)) {
|
|
306
|
+
unreadable.push(skill.rel);
|
|
307
|
+
continue;
|
|
308
|
+
}
|
|
309
|
+
const bucket = modulesReal && within(skill.real, modulesReal) ? fromPackage : fromRepo;
|
|
310
|
+
try {
|
|
311
|
+
for (const { link } of findBrokenLinks(file)) bucket.push(`${skill.name}: ${link}`);
|
|
312
|
+
} catch {
|
|
313
|
+
unreadable.push(skill.rel);
|
|
314
|
+
}
|
|
315
|
+
}
|
|
316
|
+
if (fromPackage.length > 0) {
|
|
317
|
+
add(
|
|
318
|
+
'skill-link-broken',
|
|
319
|
+
`${fromPackage.length} broken link(s) in installed skills (${fromPackage.slice(0, 3).join('; ')}): the pinned ${SKILLS} ships a bad link, update it`,
|
|
320
|
+
);
|
|
321
|
+
}
|
|
322
|
+
if (fromRepo.length > 0) {
|
|
323
|
+
add(
|
|
324
|
+
'own-skill-link-broken',
|
|
325
|
+
`${fromRepo.length} broken link(s) in this repo's own skills (${fromRepo.slice(0, 3).join('; ')}): they do not resolve from where the agent reads them`,
|
|
326
|
+
);
|
|
327
|
+
}
|
|
328
|
+
if (unreadable.length > 0) {
|
|
329
|
+
add(
|
|
330
|
+
'skill-unreadable',
|
|
331
|
+
`${unreadable.length} skill(s) have a SKILL.md that cannot be read (${unreadable.slice(0, 3).join(', ')})`,
|
|
332
|
+
);
|
|
333
|
+
}
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
return { findings };
|
|
337
|
+
}
|
|
338
|
+
|
|
339
|
+
function main() {
|
|
340
|
+
let root;
|
|
341
|
+
try {
|
|
342
|
+
root = git(process.cwd(), ['rev-parse', '--show-toplevel']).trim();
|
|
343
|
+
} catch (error) {
|
|
344
|
+
console.error(
|
|
345
|
+
error?.code === 'ENOENT'
|
|
346
|
+
? 'se-agent-guard doctor: git is not installed'
|
|
347
|
+
: 'se-agent-guard doctor: could not read a git repository here (not a repo, or a broken worktree)',
|
|
348
|
+
);
|
|
349
|
+
process.exit(2);
|
|
350
|
+
}
|
|
351
|
+
let findings;
|
|
352
|
+
try {
|
|
353
|
+
({ findings } = diagnose({ root }));
|
|
354
|
+
} catch (error) {
|
|
355
|
+
console.error(
|
|
356
|
+
`se-agent-guard doctor: could not finish: ${error instanceof Error ? error.message : error}`,
|
|
357
|
+
);
|
|
358
|
+
process.exit(2);
|
|
359
|
+
}
|
|
360
|
+
if (findings.length === 0) {
|
|
361
|
+
console.log('se-agent-guard doctor: ok');
|
|
362
|
+
return;
|
|
363
|
+
}
|
|
364
|
+
console.error(`se-agent-guard doctor: ${findings.length} problem(s)\n`);
|
|
365
|
+
for (const f of findings) console.error(` ${f.id}: ${f.text}`);
|
|
366
|
+
process.exit(1);
|
|
367
|
+
}
|
|
368
|
+
|
|
369
|
+
if (process.argv[1] && path.resolve(process.argv[1]) === fileURLToPath(import.meta.url)) main();
|