yadflow 4.6.0 → 4.8.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 +259 -0
- package/README.md +46 -4
- package/bin/commands.mjs +9 -2
- package/bin/yad.mjs +309 -30
- package/cli/agent-guard.mjs +511 -0
- package/cli/agent-guide.mjs +83 -0
- package/cli/agent-rules.mjs +381 -0
- package/cli/argv-flags.mjs +7 -0
- package/cli/artifact-status.mjs +88 -20
- package/cli/build.mjs +1372 -0
- package/cli/checkpoint.mjs +2 -2
- package/cli/ci-check.mjs +492 -0
- package/cli/commit.mjs +1 -0
- package/cli/dial.mjs +21 -1
- package/cli/docs.mjs +7 -2
- package/cli/doctor.mjs +176 -33
- package/cli/epic-state.mjs +360 -12
- package/cli/errors.mjs +3 -0
- package/cli/find-product.mjs +36 -2
- package/cli/fold.mjs +9 -5
- package/cli/gate-local.mjs +9 -1
- package/cli/gate.mjs +364 -22
- package/cli/guide.mjs +549 -0
- package/cli/history.mjs +22 -2
- package/cli/hook.mjs +35 -4
- package/cli/index-command.mjs +2 -2
- package/cli/ledger.mjs +60 -0
- package/cli/lib.mjs +46 -0
- package/cli/lifecycle/defaults/2.json +124 -0
- package/cli/lifecycle/lifecycle.schema.json +5 -2
- package/cli/lifecycle.mjs +16 -3
- package/cli/manifest.mjs +75 -0
- package/cli/marked-block.mjs +48 -0
- package/cli/members.mjs +19 -5
- package/cli/migrate.mjs +17 -19
- package/cli/next.mjs +35 -12
- package/cli/openpr.mjs +81 -5
- package/cli/plan.mjs +326 -22
- package/cli/platform.mjs +21 -0
- package/cli/pr-list.mjs +5 -3
- package/cli/product-index.mjs +6 -3
- package/cli/product-token.mjs +186 -0
- package/cli/productcommit.mjs +33 -0
- package/cli/protection.mjs +50 -0
- package/cli/recommend.mjs +3 -1
- package/cli/reconcile.mjs +187 -26
- package/cli/repo-publish.mjs +2 -2
- package/cli/report.mjs +62 -14
- package/cli/run-checks.mjs +586 -0
- package/cli/setup.mjs +244 -25
- package/cli/ship-drift.mjs +49 -18
- package/cli/signing.mjs +377 -0
- package/cli/standup.mjs +1 -1
- package/cli/status.mjs +282 -0
- package/cli/test-agents-file.mjs +993 -0
- package/cli/test-build.mjs +1559 -0
- package/cli/test-ci-check.mjs +616 -0
- package/cli/test-guide.mjs +1267 -0
- package/cli/test-node.mjs +129 -0
- package/cli/test-run-checks.mjs +603 -0
- package/cli/test-setup-agent.mjs +352 -0
- package/cli/test-signing.mjs +524 -0
- package/cli/test-worktree.mjs +520 -0
- package/cli/thread.mjs +4 -0
- package/cli/tidy.mjs +2 -2
- package/cli/toolbox.mjs +37 -6
- package/cli/update-commit.mjs +313 -11
- package/cli/workspace.mjs +38 -10
- package/cli/worktree-record.mjs +53 -0
- package/cli/worktree.mjs +379 -0
- package/package.json +6 -4
- package/skills/sdlc/config.yaml +5 -5
- package/skills/sdlc/module-help.csv +2 -1
- package/skills/yad-backfill/templates/checks/backfill-check.sh +9 -9
- package/skills/yad-change/SKILL.md +5 -1
- package/skills/yad-checks/SKILL.md +61 -12
- package/skills/yad-checks/references/check-gates.md +78 -14
- package/skills/yad-checks/templates/checks/build-test-lint.sh +22 -3
- package/skills/yad-checks/templates/checks/commit-message.sh +8 -2
- package/skills/yad-checks/templates/checks/contract-check.sh +61 -33
- package/skills/yad-checks/templates/checks/epic-open.sh +87 -14
- package/skills/yad-checks/templates/checks/ledger-guard.sh +3 -3
- package/skills/yad-checks/templates/checks/lineage-check.sh +88 -15
- package/skills/yad-checks/templates/checks/member-check.mjs +206 -35
- package/skills/yad-checks/templates/checks/package-manager.sh +42 -5
- package/skills/yad-checks/templates/checks/product-checkout.sh +161 -13
- package/skills/yad-checks/templates/checks/reconcile-debt-check.sh +94 -15
- package/skills/yad-checks/templates/checks/risk-map-check.sh +2 -2
- package/skills/yad-checks/templates/checks/spec-link.sh +63 -4
- package/skills/yad-checks/templates/checks/verified-commits.sh +23 -5
- package/skills/yad-checks/templates/github/yad-checks.yml +9 -9
- package/skills/yad-checks/templates/gitlab/.gitlab-ci.yml +4 -1
- package/skills/yad-checks/templates/gitlab/yad-checks.gitlab-ci.yml +5 -5
- package/skills/yad-checks/templates/gitlab/yad-product-checks.gitlab-ci.yml +16 -3
- package/skills/yad-checks/templates/gitlab/yad-update-guard.gitlab-ci.yml +5 -1
- package/skills/yad-checks/templates/gitlab/yad-verified-commits.gitlab-ci.yml +5 -1
- package/skills/yad-checks/templates/hooks/agent-guard.mjs +116 -0
- package/skills/yad-commit/SKILL.md +8 -5
- package/skills/yad-connect-repos/SKILL.md +5 -3
- package/skills/yad-connect-repos/references/code-context.md +2 -1
- package/skills/yad-connect-repos/references/repos-registry.md +2 -1
- package/skills/yad-docs/templates/app/package-lock.json +395 -106
- package/skills/yad-docs/templates/app/package.json +2 -2
- package/skills/yad-docs-overview/references/pipeline-model.md +2 -2
- package/skills/yad-engineer-review/SKILL.md +55 -25
- package/skills/yad-engineer-review/references/ship-and-record.md +49 -12
- package/skills/yad-epic/SKILL.md +2 -1
- package/skills/yad-epic/references/state-schema.md +76 -17
- package/skills/yad-guide/SKILL.md +192 -0
- package/skills/yad-implement/SKILL.md +75 -13
- package/skills/yad-implement/references/implement-conventions.md +8 -1
- package/skills/yad-implement/templates/.gitmessage +3 -0
- package/skills/yad-open-pr/SKILL.md +21 -0
- package/skills/yad-pair-review/SKILL.md +14 -5
- package/skills/yad-pr-template/SKILL.md +6 -2
- package/skills/yad-pr-template/templates/checks/pr-template.sh +85 -4
- package/skills/yad-pr-template/templates/checks/pr-title.sh +1 -1
- package/skills/yad-pr-template/templates/github/pull_request_template.md +1 -1
- package/skills/yad-pr-template/templates/gitlab/merge_request_templates/Default.md +1 -1
- package/skills/yad-product-bridge/SKILL.md +44 -12
- package/skills/yad-product-bridge/references/bridge.md +11 -5
- package/skills/yad-product-bridge/templates/github/yad-gate-sync.yml +2 -2
- package/skills/yad-product-bridge/templates/gitlab/yad-gate-sync.gitlab-ci.yml +9 -4
- package/skills/yad-reconcile/SKILL.md +9 -3
- package/skills/yad-report/SKILL.md +15 -0
- package/skills/yad-review-companion/SKILL.md +5 -3
- package/skills/yad-review-gate/SKILL.md +16 -8
- package/skills/yad-review-gate/references/gating.md +26 -1
- package/skills/yad-run/SKILL.md +61 -33
- package/skills/yad-run/references/run-loop.md +44 -22
- package/skills/yad-spec/SKILL.md +137 -16
- package/skills/yad-spec/references/spec-handoff.md +50 -3
- package/skills/yad-status/SKILL.md +7 -0
- package/skills/yad-stories/references/story-schema.md +1 -1
- package/skills/yad-sync-repos/SKILL.md +2 -1
- package/skills/yad-ui/SKILL.md +2 -2
|
@@ -0,0 +1,993 @@
|
|
|
1
|
+
// The rules block in AGENTS.md and CLAUDE.md (E158). The rules each test pins:
|
|
2
|
+
// - only the lines between yad's markers are written; the team's text around them is kept byte for byte;
|
|
3
|
+
// - a block that is not there is appended; markers yad cannot trust are never guessed around;
|
|
4
|
+
// - Windows line endings are kept;
|
|
5
|
+
// - CLAUDE.md (importing AGENTS.md) only for a project that installs for Claude Code;
|
|
6
|
+
// - a connected code repo gets the short block, the Product the full one;
|
|
7
|
+
// - `yad update --push` never stages the files: they are the team's;
|
|
8
|
+
// - `yad doctor` says when the block is missing, older, or cannot be written.
|
|
9
|
+
import { test } from 'node:test';
|
|
10
|
+
import assert from 'node:assert/strict';
|
|
11
|
+
import { execFileSync } from 'node:child_process';
|
|
12
|
+
import fs from 'node:fs';
|
|
13
|
+
import os from 'node:os';
|
|
14
|
+
import path from 'node:path';
|
|
15
|
+
|
|
16
|
+
// The #280 guard (cli/test.mjs): printed lines go to stderr while the runner owns stdout.
|
|
17
|
+
if (process.env.NODE_TEST_CONTEXT) {
|
|
18
|
+
const write = process.stdout.write;
|
|
19
|
+
for (const k of ['log', 'info']) {
|
|
20
|
+
const orig = console[k];
|
|
21
|
+
console[k] = (...a) => (process.stdout.write === write ? console.error(...a) : orig(...a));
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
process.env.YAD_NO_UPDATE_NOTIFIER = '1';
|
|
25
|
+
process.env.YAD_PLATFORM_LOGIN = '0';
|
|
26
|
+
process.env.YAD_PLATFORM_READ = '0';
|
|
27
|
+
process.env.YAD_CAPTURE = '0';
|
|
28
|
+
|
|
29
|
+
const { applyMarkedBlock } = await import('./marked-block.mjs');
|
|
30
|
+
const { AGENTS_MARKERS, agentsBlock, pointerBlock, claudeBlock, RULES, HUMAN_ONLY_ACTS, NEVER, humanOnlyCommands } = await import('./agent-rules.mjs');
|
|
31
|
+
const { agentFileActions, agentFilesState, repoActions } = await import('./plan.mjs');
|
|
32
|
+
const { agentFileChecks } = await import('./doctor.mjs');
|
|
33
|
+
|
|
34
|
+
const tmp = () => fs.mkdtempSync(path.join(os.tmpdir(), 'yad-agents-'));
|
|
35
|
+
const put = (file, data) => { fs.mkdirSync(path.dirname(file), { recursive: true }); fs.writeFileSync(file, data); };
|
|
36
|
+
const read = (file) => fs.readFileSync(file, 'utf8');
|
|
37
|
+
const opts = (lines = ['line one', 'line two']) => ({ ...AGENTS_MARKERS, lines });
|
|
38
|
+
|
|
39
|
+
test('a block not there is appended after the team\'s text, which is kept byte for byte', () => {
|
|
40
|
+
for (const team of ['# Team\n\nOur notes.\n', '# Team\nno newline at the end', '# Team\n\n\n']) {
|
|
41
|
+
const r = applyMarkedBlock(team, opts());
|
|
42
|
+
assert.equal(r.status, 'new');
|
|
43
|
+
assert.ok(r.text.startsWith(team), JSON.stringify(r.text));
|
|
44
|
+
assert.ok(r.text.endsWith(`${AGENTS_MARKERS.begin}\nline one\nline two\n${AGENTS_MARKERS.end}\n`));
|
|
45
|
+
}
|
|
46
|
+
const fresh = applyMarkedBlock(null, opts());
|
|
47
|
+
assert.equal(fresh.status, 'new');
|
|
48
|
+
assert.equal(fresh.text, `${AGENTS_MARKERS.begin}\nline one\nline two\n${AGENTS_MARKERS.end}\n`);
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
test('the block is rewritten in place, text on both sides kept; a second run changes nothing', () => {
|
|
52
|
+
const before = `# Team\n\n${AGENTS_MARKERS.begin}\nold line\n${AGENTS_MARKERS.end}\n\n## After\nkeep me\n`;
|
|
53
|
+
const r = applyMarkedBlock(before, opts());
|
|
54
|
+
assert.equal(r.status, 'outdated');
|
|
55
|
+
assert.equal(r.text, `# Team\n\n${AGENTS_MARKERS.begin}\nline one\nline two\n${AGENTS_MARKERS.end}\n\n## After\nkeep me\n`);
|
|
56
|
+
const again = applyMarkedBlock(r.text, opts());
|
|
57
|
+
assert.equal(again.status, 'ok');
|
|
58
|
+
assert.equal(again.text, r.text);
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
test('a begin marker with an older note is still yad\'s block', () => {
|
|
62
|
+
const before = `<!-- yadflow:begin — an older note -->\nold\n${AGENTS_MARKERS.end}\n`;
|
|
63
|
+
const r = applyMarkedBlock(before, opts());
|
|
64
|
+
assert.equal(r.status, 'outdated');
|
|
65
|
+
assert.ok(r.text.startsWith(AGENTS_MARKERS.begin));
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
test('markers yad cannot trust are refused, and the text is returned as it is', () => {
|
|
69
|
+
const { begin, end } = AGENTS_MARKERS;
|
|
70
|
+
for (const bad of [`${begin}\nno end\n`, `${end}\n${begin}\n`, `${begin}\n${end}\n${begin}\n${end}\n`, `team\n${end}\n`]) {
|
|
71
|
+
const r = applyMarkedBlock(bad, opts());
|
|
72
|
+
assert.ok(r.refused, bad);
|
|
73
|
+
assert.equal(r.text, bad);
|
|
74
|
+
}
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
test('mixed line endings outside the block are kept byte for byte', () => {
|
|
78
|
+
const { begin, end } = AGENTS_MARKERS;
|
|
79
|
+
const before = `a\r\nb\n${begin}\nold\n${end}\nc\r\n`;
|
|
80
|
+
assert.equal(applyMarkedBlock(before, opts()).text, `a\r\nb\n${begin}\nline one\nline two\n${end}\nc\r\n`);
|
|
81
|
+
const last = `${begin}\r\nold\r\n${end}`;
|
|
82
|
+
assert.equal(applyMarkedBlock(last, opts()).text, `${begin}\r\nline one\r\nline two\r\n${end}`);
|
|
83
|
+
});
|
|
84
|
+
|
|
85
|
+
test('Windows line endings are kept, and only the content counts as a change', () => {
|
|
86
|
+
const before = `# Team\r\n\r\n${AGENTS_MARKERS.begin}\r\nline one\r\nline two\r\n${AGENTS_MARKERS.end}\r\n`;
|
|
87
|
+
assert.equal(applyMarkedBlock(before, opts()).status, 'ok');
|
|
88
|
+
const r = applyMarkedBlock('# Team\r\n', opts());
|
|
89
|
+
assert.equal(r.text, `# Team\r\n\r\n${AGENTS_MARKERS.begin}\r\nline one\r\nline two\r\n${AGENTS_MARKERS.end}\r\n`);
|
|
90
|
+
});
|
|
91
|
+
|
|
92
|
+
test('the Product block carries every rule, every human-only command and act, and what is never done', () => {
|
|
93
|
+
const text = agentsBlock().join('\n');
|
|
94
|
+
for (const r of RULES) assert.ok(text.includes(r.text), r.id);
|
|
95
|
+
for (const a of HUMAN_ONLY_ACTS) assert.ok(text.includes(a.text), a.id);
|
|
96
|
+
for (const n of NEVER) assert.ok(text.includes(n.text), n.id);
|
|
97
|
+
const cmds = humanOnlyCommands();
|
|
98
|
+
for (const c of ['yad gate approve', 'yad gate advance', 'yad gate reopen', 'yad gate comment', 'yad commit --manual']) assert.ok(cmds.includes(c), c);
|
|
99
|
+
for (const c of cmds) assert.ok(text.includes(`\`${c}\``), c);
|
|
100
|
+
assert.match(text, /yad-guide/);
|
|
101
|
+
// The code repo's short block names no path: clones are laid out differently on each machine.
|
|
102
|
+
const pointer = pointerBlock().join('\n');
|
|
103
|
+
assert.match(pointer, /yad guide --json/);
|
|
104
|
+
assert.doesNotMatch(pointer, /\.\.\//);
|
|
105
|
+
assert.deepEqual(claudeBlock(), ['@AGENTS.md']);
|
|
106
|
+
});
|
|
107
|
+
|
|
108
|
+
test('check --fix writes AGENTS.md and CLAUDE.md for Claude Code, AGENTS.md alone otherwise; files are never staged', () => {
|
|
109
|
+
const T = tmp();
|
|
110
|
+
try {
|
|
111
|
+
put(path.join(T, 'AGENTS.md'), '# Our team rules\n');
|
|
112
|
+
const both = agentFileActions(T, ['.claude', '.agents']);
|
|
113
|
+
assert.deepEqual(both.map((a) => [a.item, a.status]), [['AGENTS.md', 'new'], ['CLAUDE.md', 'new']]);
|
|
114
|
+
for (const a of both) assert.deepEqual(a.paths, [], `${a.item}: --push never stages it`);
|
|
115
|
+
for (const a of both) a.apply();
|
|
116
|
+
assert.ok(read(path.join(T, 'AGENTS.md')).startsWith('# Our team rules\n\n'));
|
|
117
|
+
assert.match(read(path.join(T, 'CLAUDE.md')), /^<!-- yadflow:begin[^\n]*\n@AGENTS\.md\n<!-- yadflow:end -->\n$/);
|
|
118
|
+
assert.deepEqual(agentFileActions(T, ['.claude']).map((a) => a.status), ['ok', 'ok']);
|
|
119
|
+
assert.deepEqual(agentFileActions(T, ['.agents']).map((a) => a.item), ['AGENTS.md']);
|
|
120
|
+
} finally { fs.rmSync(T, { recursive: true, force: true }); }
|
|
121
|
+
});
|
|
122
|
+
|
|
123
|
+
test('broken markers and a file that is not a plain file are left alone', () => {
|
|
124
|
+
const T = tmp();
|
|
125
|
+
try {
|
|
126
|
+
const broken = `${AGENTS_MARKERS.begin}\nno end\n`;
|
|
127
|
+
put(path.join(T, 'AGENTS.md'), broken);
|
|
128
|
+
const [a] = agentFileActions(T, ['.agents']);
|
|
129
|
+
assert.equal(a.status, 'modified');
|
|
130
|
+
assert.equal(a.writesNothing, true, 'never applied, so no run says it was fixed');
|
|
131
|
+
a.apply();
|
|
132
|
+
assert.equal(read(path.join(T, 'AGENTS.md')), broken);
|
|
133
|
+
if (process.platform !== 'win32') {
|
|
134
|
+
const U = tmp();
|
|
135
|
+
try {
|
|
136
|
+
put(path.join(U, 'elsewhere.md'), 'theirs\n');
|
|
137
|
+
fs.symlinkSync(path.join(U, 'elsewhere.md'), path.join(T, 'CLAUDE.md'));
|
|
138
|
+
const claude = agentFileActions(T, ['.claude']).find((x) => x.item === 'CLAUDE.md');
|
|
139
|
+
assert.equal(claude.status, 'modified');
|
|
140
|
+
claude.apply();
|
|
141
|
+
assert.equal(read(path.join(U, 'elsewhere.md')), 'theirs\n', 'never written through a link');
|
|
142
|
+
} finally { fs.rmSync(U, { recursive: true, force: true }); }
|
|
143
|
+
}
|
|
144
|
+
} finally { fs.rmSync(T, { recursive: true, force: true }); }
|
|
145
|
+
});
|
|
146
|
+
|
|
147
|
+
test('a connected code repo cloned here gets the short block; one not cloned, or the Product itself, does not', () => {
|
|
148
|
+
const T = tmp();
|
|
149
|
+
try {
|
|
150
|
+
const P = path.join(T, 'product');
|
|
151
|
+
const R = path.join(T, 'api');
|
|
152
|
+
fs.mkdirSync(P); fs.mkdirSync(R);
|
|
153
|
+
put(path.join(P, '.sdlc/cli-version.json'), JSON.stringify({ version: '4.7.0', ideTargets: ['.claude'] }));
|
|
154
|
+
const items = (repo) => repoActions(P, repo).filter((a) => a.kind === 'text').map((a) => `${a.scope}/${a.item}`);
|
|
155
|
+
assert.deepEqual(items({ name: 'api', path: '../api', platform: 'github' }), ['api/AGENTS.md', 'api/CLAUDE.md']);
|
|
156
|
+
// On a repo yad has not wired, it stays `missing`: `yad update` never does a repo's one-time setup.
|
|
157
|
+
assert.ok(repoActions(P, { name: 'api', path: '../api', platform: 'github' }).filter((a) => a.kind === 'text').every((a) => a.status === 'missing'));
|
|
158
|
+
assert.deepEqual(items({ name: 'web', path: '../web', platform: 'github' }), []);
|
|
159
|
+
assert.deepEqual(items({ name: 'product', path: '.', platform: 'github' }), []);
|
|
160
|
+
// Review 3: the team's own AGENTS.md there, with no block, is `missing` too, so `yad update` leaves it.
|
|
161
|
+
put(path.join(R, 'AGENTS.md'), '# Team notes\n');
|
|
162
|
+
const team = repoActions(P, { name: 'api', path: '../api', platform: 'github' }).find((x) => x.item === 'AGENTS.md');
|
|
163
|
+
assert.equal(team.status, 'missing');
|
|
164
|
+
// On a wired repo the block is `new`, and update adds it.
|
|
165
|
+
put(path.join(R, 'checks/spec-link.sh'), '#!/usr/bin/env bash\n');
|
|
166
|
+
assert.equal(repoActions(P, { name: 'api', path: '../api', platform: 'github' }).find((x) => x.item === 'AGENTS.md').status, 'new');
|
|
167
|
+
team.apply();
|
|
168
|
+
assert.ok(read(path.join(R, 'AGENTS.md')).startsWith('# Team notes\n'));
|
|
169
|
+
assert.ok(read(path.join(R, 'AGENTS.md')).includes(pointerBlock()[0]));
|
|
170
|
+
} finally { fs.rmSync(T, { recursive: true, force: true }); }
|
|
171
|
+
});
|
|
172
|
+
|
|
173
|
+
test('yad doctor names a missing, older or unwritable rules block, and passes a current one', () => {
|
|
174
|
+
const T = tmp();
|
|
175
|
+
try {
|
|
176
|
+
put(path.join(T, '.sdlc/cli-version.json'), JSON.stringify({ version: '4.7.0', ideTargets: ['.agents'] }));
|
|
177
|
+
const run = () => { const checks = []; agentFileChecks(checks, T); return checks[0]; };
|
|
178
|
+
assert.match(run().message, /AGENTS\.md is missing/);
|
|
179
|
+
put(path.join(T, 'AGENTS.md'), '# Team\n');
|
|
180
|
+
assert.match(run().message, /has no yadflow rules block/);
|
|
181
|
+
put(path.join(T, 'AGENTS.md'), `${AGENTS_MARKERS.begin}\nold\n${AGENTS_MARKERS.end}\n`);
|
|
182
|
+
assert.match(run().message, /has an older yadflow rules block/);
|
|
183
|
+
assert.equal(run().status, 'warn');
|
|
184
|
+
put(path.join(T, 'AGENTS.md'), `${AGENTS_MARKERS.begin}\nold\n`);
|
|
185
|
+
assert.match(run().message, /needs exactly one line/);
|
|
186
|
+
agentFileActions(T, ['.agents']).forEach((a) => a.apply());
|
|
187
|
+
put(path.join(T, 'AGENTS.md'), applyMarkedBlock('# Team\n', { ...AGENTS_MARKERS, lines: agentsBlock() }).text);
|
|
188
|
+
assert.equal(run().status, 'ok');
|
|
189
|
+
assert.deepEqual(agentFilesState(T, ['.agents']).map((f) => f.state), ['ok']);
|
|
190
|
+
} finally { fs.rmSync(T, { recursive: true, force: true }); }
|
|
191
|
+
});
|
|
192
|
+
|
|
193
|
+
test('--overwrite-local never says it fixed a rules block it could not write', async () => {
|
|
194
|
+
const { reconcile } = await import('./reconcile.mjs');
|
|
195
|
+
const T = tmp();
|
|
196
|
+
const said = [];
|
|
197
|
+
const orig = { log: console.log, error: console.error };
|
|
198
|
+
try {
|
|
199
|
+
put(path.join(T, '.sdlc/product.json'), JSON.stringify({ platform: 'github', default_branch: 'main' }));
|
|
200
|
+
put(path.join(T, '.sdlc/cli-version.json'), JSON.stringify({ version: '4.0.0', ideTargets: ['.agents'] }));
|
|
201
|
+
const broken = `${AGENTS_MARKERS.begin}\nno end\n`;
|
|
202
|
+
put(path.join(T, 'AGENTS.md'), broken);
|
|
203
|
+
console.log = (...a) => said.push(a.join(' '));
|
|
204
|
+
console.error = (...a) => said.push(a.join(' '));
|
|
205
|
+
await reconcile(T, { fix: true, overwriteLocal: true });
|
|
206
|
+
Object.assign(console, orig);
|
|
207
|
+
assert.equal(read(path.join(T, 'AGENTS.md')), broken);
|
|
208
|
+
const text = said.join('\n');
|
|
209
|
+
assert.doesNotMatch(text, /fixed: [^\n]*AGENTS\.md/);
|
|
210
|
+
assert.doesNotMatch(text, /AGENTS\.md is locally modified/);
|
|
211
|
+
assert.match(text, /AGENTS\.md: the yadflow block needs exactly one line/);
|
|
212
|
+
} finally { Object.assign(console, orig); fs.rmSync(T, { recursive: true, force: true }); process.exitCode = 0; }
|
|
213
|
+
});
|
|
214
|
+
|
|
215
|
+
test('yad update brings the block to an install that predates it, beside the team\'s text', async () => {
|
|
216
|
+
const { reconcile } = await import('./reconcile.mjs');
|
|
217
|
+
const T = tmp();
|
|
218
|
+
const git = (args) => execFileSync('git', args, { cwd: T, stdio: 'pipe' }).toString().trim();
|
|
219
|
+
try {
|
|
220
|
+
git(['init', '-q', '-b', 'main']);
|
|
221
|
+
git(['config', 'user.name', 'Alice Smith']); git(['config', 'user.email', 'alice@work.com']); git(['config', 'commit.gpgsign', 'false']);
|
|
222
|
+
put(path.join(T, '.sdlc/product.json'), JSON.stringify({ platform: 'github', git_url: 'git@github.com:acme/product.git', default_branch: 'main' }));
|
|
223
|
+
put(path.join(T, '.sdlc/cli-version.json'), JSON.stringify({ version: '4.0.0', ideTargets: ['.claude'] }));
|
|
224
|
+
put(path.join(T, 'AGENTS.md'), '# Team\n');
|
|
225
|
+
git(['add', '-A']); git(['commit', '-q', '-m', 'chore: start']);
|
|
226
|
+
const r = await reconcile(T, { fix: true, scope: 'changed' });
|
|
227
|
+
assert.ok(r.items.some((i) => i.item === 'AGENTS.md' && i.status === 'new'), JSON.stringify(r.items));
|
|
228
|
+
assert.ok(r.items.some((i) => i.item === 'CLAUDE.md' && i.status === 'new'), JSON.stringify(r.items));
|
|
229
|
+
assert.ok(read(path.join(T, 'AGENTS.md')).includes(AGENTS_MARKERS.end));
|
|
230
|
+
assert.ok(fs.existsSync(path.join(T, 'CLAUDE.md')));
|
|
231
|
+
} finally { fs.rmSync(T, { recursive: true, force: true }); process.exitCode = 0; }
|
|
232
|
+
});
|
|
233
|
+
|
|
234
|
+
// ---- the agent guard (E158) --------------------------------------------------------------------------
|
|
235
|
+
|
|
236
|
+
const { agentGuardDecision, shellParts } = await import('./agent-guard.mjs');
|
|
237
|
+
const { agentGuardActions, orphanAgentGuardActions } = await import('./plan.mjs');
|
|
238
|
+
const { AGENT_GUARD_ADAPTERS } = await import('./manifest.mjs');
|
|
239
|
+
const { spawnSync } = await import('node:child_process');
|
|
240
|
+
|
|
241
|
+
test('the agent guard refuses every human-only yad command and every force push, however it is spelled', () => {
|
|
242
|
+
for (const cmd of [
|
|
243
|
+
'yad gate approve EP-a epic --by me',
|
|
244
|
+
'yad gate comment EP-a epic --by me --count 2',
|
|
245
|
+
'yad gate advance EP-a epic',
|
|
246
|
+
'yad gate reopen EP-a epic --reason "why"',
|
|
247
|
+
'yad commit -m "a b" --manual --reason x',
|
|
248
|
+
'cd x && yad gate approve EP-a epic',
|
|
249
|
+
'yad status; yad gate advance EP-a epic',
|
|
250
|
+
'npx -y yadflow@4 gate approve EP-a epic',
|
|
251
|
+
'node ./node_modules/yadflow/bin/yad.mjs gate reopen EP-a epic',
|
|
252
|
+
'YAD_X=1 yad gate approve EP-a epic',
|
|
253
|
+
'/usr/local/bin/yad gate approve EP-a epic',
|
|
254
|
+
'git push --force',
|
|
255
|
+
'git push -f origin main',
|
|
256
|
+
'git push -uf origin main',
|
|
257
|
+
'git push --force-with-lease',
|
|
258
|
+
'git push --force-with-lease=main:abc origin main',
|
|
259
|
+
'git push --force-if-includes origin main',
|
|
260
|
+
'git -C repo push origin +main',
|
|
261
|
+
'git status | cat && git push origin +feat/x',
|
|
262
|
+
// Review 1: yad's flags anywhere, a joined line, other plain spellings, and the platform's own approve and merge.
|
|
263
|
+
'yad --dir ../product gate approve EP-x epic',
|
|
264
|
+
'yad --json gate advance EP-x epic',
|
|
265
|
+
'yad -m s commit --manual --reason r',
|
|
266
|
+
'yad gate approve \\\nEP-x epic',
|
|
267
|
+
'yad \\\n gate reopen EP-x epic',
|
|
268
|
+
'./bin/yad.mjs gate approve EP-x epic',
|
|
269
|
+
'timeout 30 yad gate approve EP-x epic',
|
|
270
|
+
'npm exec yadflow -- gate approve EP-x epic',
|
|
271
|
+
'git --git-dir .git push -f',
|
|
272
|
+
'gh pr review 5 --approve',
|
|
273
|
+
'gh pr review 5 -a -b ok',
|
|
274
|
+
'gh pr merge 5 --squash',
|
|
275
|
+
'glab mr approve 3',
|
|
276
|
+
'glab mr merge 3',
|
|
277
|
+
'cat <<EOF > notes.md\nsome text\nEOF\nyad gate approve EP-x epic',
|
|
278
|
+
// Review 2: a here-string, a shift, an unclosed heredoc, CRLF, a comment, prefixes with options, shells.
|
|
279
|
+
'cat <<< "x"\nyad gate approve EP-x epic',
|
|
280
|
+
'echo $((1<<2))\nyad gate approve EP-x epic',
|
|
281
|
+
'cat <<EOF\r\nx\r\nEOF\r\nyad gate approve EP-x epic',
|
|
282
|
+
'# <<x\nyad gate approve EP-x epic',
|
|
283
|
+
'env -i yad gate approve EP-x epic',
|
|
284
|
+
'sudo -u me yad gate approve EP-x epic',
|
|
285
|
+
'nice -n 5 yad gate approve EP-x epic',
|
|
286
|
+
'time -p yad gate approve EP-x epic',
|
|
287
|
+
'npx -p yadflow yad gate approve EP-x epic',
|
|
288
|
+
'node -r ./x.js bin/yad.mjs gate approve EP-x epic',
|
|
289
|
+
'(yad gate approve EP-x epic)',
|
|
290
|
+
'{ yad gate approve EP-x epic; }',
|
|
291
|
+
'if true; then yad gate approve EP-x epic; fi',
|
|
292
|
+
'bash -c "yad gate approve EP-x epic"',
|
|
293
|
+
'gh -R o/r pr merge 5',
|
|
294
|
+
'glab -R g/p mr merge 1',
|
|
295
|
+
// Review 3: a command substitution runs its command; the package runners run yadflow's own `yad`.
|
|
296
|
+
'r=$(yad gate approve EP-x epic --json)',
|
|
297
|
+
'echo `yad gate approve EP-x epic`',
|
|
298
|
+
'echo "$(yad gate advance EP-x epic)"',
|
|
299
|
+
'x=$(echo $(yad gate reopen EP-x epic))',
|
|
300
|
+
'npx yad gate approve EP-x epic',
|
|
301
|
+
'npm exec yad -- gate approve EP-x epic',
|
|
302
|
+
'pnpm exec yad gate approve EP-x epic',
|
|
303
|
+
'pnpm yad gate approve EP-x epic',
|
|
304
|
+
'pnpm dlx yadflow gate approve EP-x epic',
|
|
305
|
+
'yarn yad gate approve EP-x epic',
|
|
306
|
+
'yarn exec yad gate approve EP-x epic',
|
|
307
|
+
'glab mr accept 5',
|
|
308
|
+
'git push --mirror',
|
|
309
|
+
'gh pr review 5 --approve=true',
|
|
310
|
+
'git commit -m "$(cat <<\'EOF\'\nsubject\n\nsays "hi\nEOF\n)" && git push -f',
|
|
311
|
+
// Review 4: an unquoted heredoc runs the commands in its text; runner options before the program.
|
|
312
|
+
'git commit -F - <<EOF\nfix: $(yad gate approve EP-a epic)\nEOF',
|
|
313
|
+
'cat <<EOF\n`yad gate advance EP-a epic`\nEOF',
|
|
314
|
+
'pnpm --dir x yad gate approve EP-a epic',
|
|
315
|
+
'pnpm -C x exec yad gate approve EP-a epic',
|
|
316
|
+
'yarn --cwd x yad gate approve EP-a epic',
|
|
317
|
+
// Review 5: a runner's -c holds a shell line; more runner forms.
|
|
318
|
+
'npx -c \'yad gate approve EP-x analysis\'',
|
|
319
|
+
'npm exec -c \'yad gate approve EP-x analysis\'',
|
|
320
|
+
'npx --call=\'yad gate advance EP-x analysis\'',
|
|
321
|
+
'pnpm exec -c yad gate approve EP-x analysis',
|
|
322
|
+
'yarn workspace web yad gate approve EP-x analysis',
|
|
323
|
+
'bun x yad gate approve EP-x analysis',
|
|
324
|
+
// Review 6: pnpm dlx's shell mode, yarn run, npm options before exec.
|
|
325
|
+
'pnpm dlx -c \'yad gate approve EP-x analysis\'',
|
|
326
|
+
'pnpm --package=yadflow dlx -c \'yad gate approve EP-x analysis\'',
|
|
327
|
+
'pnpm dlx --shell-mode \'yad gate approve EP-x analysis\'',
|
|
328
|
+
'pnpm --shell-mode exec yad gate approve EP-x analysis',
|
|
329
|
+
'yarn run yad gate approve EP-x analysis',
|
|
330
|
+
'yarn workspace web run yad gate approve EP-x analysis',
|
|
331
|
+
'npm --yes exec -c \'yad gate approve EP-x analysis\'',
|
|
332
|
+
'npm --yes exec yadflow gate approve EP-x analysis',
|
|
333
|
+
'npx -c="yad gate approve EP-x analysis"',
|
|
334
|
+
// Review 7: npm's -w <name> and other options that take a value.
|
|
335
|
+
'npm exec -w web -c \'yad gate approve EP-x analysis\'',
|
|
336
|
+
'npx -w web -c \'yad gate approve EP-x analysis\'',
|
|
337
|
+
'npm -w web exec yad gate approve EP-x analysis',
|
|
338
|
+
'npm exec -w web -- yad gate approve EP-x analysis',
|
|
339
|
+
'npx --loglevel warn -c \'yad gate approve EP-x analysis\'',
|
|
340
|
+
'pnpm --reporter silent exec yad gate approve EP-x analysis',
|
|
341
|
+
'pnpm -w exec yad gate approve EP-x analysis',
|
|
342
|
+
// Review 8: bun's options before run or x.
|
|
343
|
+
'bun --filter web run yad gate approve EP-x analysis',
|
|
344
|
+
'bun --cwd web run yad gate approve EP-x analysis',
|
|
345
|
+
'bun --bun run yad gate approve EP-x analysis',
|
|
346
|
+
'bun --bun x yadflow gate approve EP-x analysis',
|
|
347
|
+
// Review 9: bun's own options that take a value.
|
|
348
|
+
'bun --env-file .env run yad gate approve EP-x analysis',
|
|
349
|
+
'bun --preload ./s.ts run yad gate approve EP-x analysis',
|
|
350
|
+
'bun --config bunfig.toml x yadflow gate approve EP-x analysis',
|
|
351
|
+
// Review 10: node's options that take a value, the platform API, the git hooks' door, more wrappers.
|
|
352
|
+
'node --env-file .env bin/yad.mjs gate approve EP-x analysis',
|
|
353
|
+
'node --watch-path src bin/yad.mjs gate approve EP-x analysis',
|
|
354
|
+
'gh api -X PUT repos/o/r/pulls/5/merge',
|
|
355
|
+
'gh api --method PUT /repos/{owner}/{repo}/pulls/5/merge -f merge_method=squash',
|
|
356
|
+
'gh api repos/o/r/pulls/5/reviews -f event=APPROVE',
|
|
357
|
+
'gh api graphql -f query=\'mutation { mergePullRequest(input: {pullRequestId: "x"}) { clientMutationId } }\'',
|
|
358
|
+
'glab api -X PUT projects/1/merge_requests/3/merge',
|
|
359
|
+
'glab api -X POST projects/1/merge_requests/3/approve',
|
|
360
|
+
'git commit --no-verify -m x',
|
|
361
|
+
'git commit -n -m x',
|
|
362
|
+
'git commit -anm x',
|
|
363
|
+
'git -c core.hooksPath=/dev/null commit -m x',
|
|
364
|
+
// Review 11: setting it for good.
|
|
365
|
+
'git config core.hooksPath /dev/null',
|
|
366
|
+
'git config --local core.hooksPath .nohooks',
|
|
367
|
+
'gh api -XPUT repos/o/r/pulls/5/merge',
|
|
368
|
+
'gh api --method=PUT repos/o/r/pulls/5/merge',
|
|
369
|
+
'gh api --cache 1h repos/o/r/pulls/5/merge -X PUT',
|
|
370
|
+
'gh api graphql -f query=\'mutation { submitPullRequestReview(input: {pullRequestReviewId: "x", event: APPROVE}) { clientMutationId } }\'',
|
|
371
|
+
'glab api graphql -f query=\'mutation { mergeRequestAccept(input: {projectPath: "g/p", iid: "3"}) { errors } }\'',
|
|
372
|
+
'glab api projects/1/merge_requests/3/approve -f sha=abc',
|
|
373
|
+
// Review 12: xargs's switches take no value; a field in its `=` form.
|
|
374
|
+
'echo 5 | xargs -r gh pr merge',
|
|
375
|
+
'echo 5 | xargs -t gh pr merge',
|
|
376
|
+
'echo 5 | xargs -o gh pr merge',
|
|
377
|
+
'xargs -r git commit -n -m x',
|
|
378
|
+
'echo x | xargs -r git push -f',
|
|
379
|
+
'watch -t yad gate advance EP-x analysis',
|
|
380
|
+
'gh api repos/o/r/pulls/5/reviews --field=event=APPROVE',
|
|
381
|
+
'gh api repos/o/r/pulls/5/reviews --raw-field=event=APPROVE',
|
|
382
|
+
// Review 13: the BSD (macOS) xargs's options that take a value; a short field with its value attached.
|
|
383
|
+
'echo 5 | xargs -J % gh pr merge %',
|
|
384
|
+
'ls | xargs -R 1 -I % gh pr merge %',
|
|
385
|
+
'ls | xargs -S 255 -I % gh pr merge %',
|
|
386
|
+
'gh api repos/o/r/pulls/5/reviews -Fevent=APPROVE',
|
|
387
|
+
'gh api repos/o/r/pulls/5/reviews -fevent=APPROVE',
|
|
388
|
+
'gh api repos/o/r/pulls/5/merge -fmerge_method=squash -XPUT',
|
|
389
|
+
// Review 14: a short option's value after `=`.
|
|
390
|
+
'gh api repos/o/r/pulls/5/reviews -f=event=APPROVE',
|
|
391
|
+
'gh api repos/o/r/pulls/5/reviews -F=event=APPROVE',
|
|
392
|
+
'gh api repos/o/r/pulls/5/merge -X=PUT',
|
|
393
|
+
'YAD_HOOK_DISABLE=1 git commit -m x',
|
|
394
|
+
'env YAD_HOOK_DISABLE=1 git commit -m x',
|
|
395
|
+
'export YAD_HOOK_DISABLE=1 && git commit -m x',
|
|
396
|
+
'xargs yad gate approve',
|
|
397
|
+
'echo EP-x | xargs -I {} yad gate approve {} analysis',
|
|
398
|
+
'stdbuf -oL yad gate approve EP-x analysis',
|
|
399
|
+
'watch -n 5 yad gate advance EP-x analysis',
|
|
400
|
+
'sudo -E yad gate approve EP-x analysis',
|
|
401
|
+
'yarn workspaces foreach -A run yad gate approve EP-x analysis',
|
|
402
|
+
'yarn workspaces foreach --include web exec yad gate approve EP-x analysis',
|
|
403
|
+
]) assert.equal(agentGuardDecision(cmd).allow, false, cmd);
|
|
404
|
+
});
|
|
405
|
+
|
|
406
|
+
test('the agent guard lets the rest through: reads, the agent\'s own pushes, text that only mentions a command', () => {
|
|
407
|
+
for (const cmd of [
|
|
408
|
+
'yad gate status EP-a',
|
|
409
|
+
'yad guide --json',
|
|
410
|
+
'yad gate open EP-a epic',
|
|
411
|
+
'yad commit -m "fix it"',
|
|
412
|
+
'yad help gate approve',
|
|
413
|
+
'git push origin feat/x',
|
|
414
|
+
'git push -u origin feat/x',
|
|
415
|
+
'echo "yad gate approve EP-a"',
|
|
416
|
+
'grep -n "git push --force" docs/x.md',
|
|
417
|
+
'',
|
|
418
|
+
// A heredoc's text is written into a file, never run.
|
|
419
|
+
'cat > notes.md <<EOF\nyad gate approve EP-x epic\ngit push --force\nEOF',
|
|
420
|
+
'cat > notes.md <<-\'END\'\n\tyad gate advance EP-x epic\n\tEND',
|
|
421
|
+
'gh pr review 5 --comment -b "looks fine"',
|
|
422
|
+
'gh pr view 5',
|
|
423
|
+
'glab mr view 3',
|
|
424
|
+
'yad --json gate status EP-x',
|
|
425
|
+
'yad gate approve --help',
|
|
426
|
+
'gh pr merge --help',
|
|
427
|
+
'git log --oneline # yad gate approve',
|
|
428
|
+
'gh pr create --title t --body b',
|
|
429
|
+
// Review 3: single quotes keep a substitution as text; quotes inside one are its own.
|
|
430
|
+
'echo \'$(yad gate approve EP-x epic)\'',
|
|
431
|
+
'r=$(yad gate status EP-x --json)',
|
|
432
|
+
'npx yad gate status',
|
|
433
|
+
'pnpm install',
|
|
434
|
+
'yarn test',
|
|
435
|
+
'git commit -m "$(cat <<\'EOF\'\nfix: x\n\nA "quote and 1) item\nyad gate approve EP-x\ngit push -f\nEOF\n)"',
|
|
436
|
+
// Review 4: a quoted heredoc's text is only text; an option's value is not an option.
|
|
437
|
+
'git commit -F - <<\'EOF\'\nfix: $(yad gate approve EP-a epic)\nEOF',
|
|
438
|
+
'cat <<EOF\nescaped \\$(yad gate approve EP-a epic)\nEOF',
|
|
439
|
+
'gh pr review 5 --comment --body "-a"',
|
|
440
|
+
'pnpm --dir x test',
|
|
441
|
+
// Review 5: `<<\EOF` and `<<E"OF"` are quoted heredocs: their text is only text.
|
|
442
|
+
'cat <<\\EOF > n.md\nyad gate approve EP-x analysis\n$(yad gate advance EP-x analysis)\nEOF',
|
|
443
|
+
'cat <<E"OF" > n.md\nyad gate approve EP-x analysis\nEOF',
|
|
444
|
+
'npx -c \'yad status\'',
|
|
445
|
+
'yarn workspace web test',
|
|
446
|
+
// Review 6: a program's own -c is its own.
|
|
447
|
+
'pnpm exec vitest -c vitest.config.ts',
|
|
448
|
+
'npx eslint -c \'git push -f\' src',
|
|
449
|
+
'pnpm -c exec echo hi',
|
|
450
|
+
'npx -c \'eslint .\'',
|
|
451
|
+
'yarn run build',
|
|
452
|
+
'pnpm -w add -D x',
|
|
453
|
+
'npm -w web run build',
|
|
454
|
+
'bun --filter web run build',
|
|
455
|
+
'bun --env-file .env run dev',
|
|
456
|
+
'bun --watch run dev',
|
|
457
|
+
// Review 10: a push option's value is text; ordinary API reads, commits and wrappers.
|
|
458
|
+
'git push -o +x origin feat/x',
|
|
459
|
+
'git push --push-option +ci.skip origin feat/x',
|
|
460
|
+
'node --env-file .env server.js',
|
|
461
|
+
'gh api repos/o/r/pulls/5',
|
|
462
|
+
'gh api repos/o/r/pulls/5/reviews -f event=COMMENT -f body=ok',
|
|
463
|
+
'glab api projects/1/merge_requests/3',
|
|
464
|
+
'git commit -m "-n is a flag"',
|
|
465
|
+
'git commit -m x --no-edit',
|
|
466
|
+
'git commit -F msg.txt',
|
|
467
|
+
'git log -n 3',
|
|
468
|
+
'git -c user.name=x commit -m y',
|
|
469
|
+
'git config core.hooksPath',
|
|
470
|
+
'git config --get core.hooksPath',
|
|
471
|
+
'git config --unset core.hooksPath',
|
|
472
|
+
'git config user.name x',
|
|
473
|
+
'gh api repos/o/r/pulls/5/merge',
|
|
474
|
+
'gh api repos/o/r/pulls/5/merge --jq .message',
|
|
475
|
+
'glab api projects/1/merge_requests/3/approvals',
|
|
476
|
+
'git commit -uno -m x',
|
|
477
|
+
'gh pr merge --disable-auto 5',
|
|
478
|
+
'gh api graphql -f query=\'query { repository(owner: "o", name: "r") { pullRequest(number: 5) { merged } } }\'',
|
|
479
|
+
'xargs -n 1 echo',
|
|
480
|
+
'watch -d git status',
|
|
481
|
+
'yarn workspaces foreach -A run build',
|
|
482
|
+
]) assert.equal(agentGuardDecision(cmd).allow, true, cmd);
|
|
483
|
+
assert.equal(agentGuardDecision(undefined).allow, true);
|
|
484
|
+
assert.deepEqual(shellParts('a "b c" \'d e\' && f\\ g | h'), [['a', 'b c', 'd e'], ['f g'], ['h']]);
|
|
485
|
+
});
|
|
486
|
+
|
|
487
|
+
test('the refusal says what to do instead: the person runs it', () => {
|
|
488
|
+
const v = agentGuardDecision('yad gate approve EP-a epic');
|
|
489
|
+
assert.match(v.message, /`yad gate approve`/);
|
|
490
|
+
assert.match(v.message, /Ask the person to run it themselves/);
|
|
491
|
+
assert.match(v.message, /`!`/);
|
|
492
|
+
});
|
|
493
|
+
|
|
494
|
+
// The hook script, run as a harness runs it, with this checkout's yad.
|
|
495
|
+
function guardScript() {
|
|
496
|
+
const T = tmp();
|
|
497
|
+
fs.mkdirSync(path.join(T, 'hooks'));
|
|
498
|
+
fs.copyFileSync(path.join(path.dirname(new URL(import.meta.url).pathname), '..', 'skills/yad-checks/templates/hooks/agent-guard.mjs'), path.join(T, 'hooks/agent-guard.mjs'));
|
|
499
|
+
return T;
|
|
500
|
+
}
|
|
501
|
+
const YAD_MJS = path.join(path.dirname(new URL(import.meta.url).pathname), '..', 'bin', 'yad.mjs');
|
|
502
|
+
const runGuard = (T, payload, env = {}) => {
|
|
503
|
+
const r = spawnSync(process.execPath, [path.join(T, 'hooks/agent-guard.mjs')], {
|
|
504
|
+
input: typeof payload === 'string' ? payload : JSON.stringify(payload), encoding: 'utf8', cwd: T,
|
|
505
|
+
env: { ...process.env, YAD_BIN: `"${process.execPath}" "${YAD_MJS}"`, YAD_HOOK_DISABLE: '', ...env }, timeout: 30_000,
|
|
506
|
+
});
|
|
507
|
+
return { code: r.status, out: (r.stdout || '').trim(), err: r.stderr || '' };
|
|
508
|
+
};
|
|
509
|
+
|
|
510
|
+
test('hooks/agent-guard.mjs: Claude Code reads the exit code, Cursor a JSON verdict, always', () => {
|
|
511
|
+
const T = guardScript();
|
|
512
|
+
try {
|
|
513
|
+
const claude = (command) => runGuard(T, { hook_event_name: 'PreToolUse', tool_name: 'Bash', tool_input: { command } });
|
|
514
|
+
const denied = claude('yad gate approve EP-a epic --by me');
|
|
515
|
+
assert.equal(denied.code, 2);
|
|
516
|
+
assert.match(denied.err, /the person's to run/);
|
|
517
|
+
assert.equal(claude('yad status').code, 0);
|
|
518
|
+
const cursor = (command) => runGuard(T, { hook_event_name: 'beforeShellExecution', command });
|
|
519
|
+
const no = cursor('git push --force');
|
|
520
|
+
assert.equal(no.code, 0);
|
|
521
|
+
assert.equal(JSON.parse(no.out).permission, 'deny');
|
|
522
|
+
assert.match(JSON.parse(no.out).agent_message, /force push/);
|
|
523
|
+
assert.deepEqual(JSON.parse(cursor('ls').out), { permission: 'allow' });
|
|
524
|
+
// Fail-open: an unreadable payload, a switched-off guard, and no yad at all.
|
|
525
|
+
assert.equal(runGuard(T, 'not json').code, 0);
|
|
526
|
+
assert.equal(runGuard(T, { tool_input: { command: 'yad gate advance EP-a epic' } }, { YAD_HOOK_DISABLE: '1' }).code, 0);
|
|
527
|
+
const none = runGuard(T, { hook_event_name: 'beforeShellExecution', command: 'git push -f' }, { YAD_BIN: '', PATH: '' });
|
|
528
|
+
assert.deepEqual(JSON.parse(none.out), { permission: 'allow' });
|
|
529
|
+
assert.match(none.err, /allowing/);
|
|
530
|
+
} finally { fs.rmSync(T, { recursive: true, force: true }); }
|
|
531
|
+
});
|
|
532
|
+
|
|
533
|
+
test('check --fix wires the agent guard on every Product, beside the team\'s own hooks; a target that leaves takes its entry', () => {
|
|
534
|
+
const T = tmp();
|
|
535
|
+
try {
|
|
536
|
+
put(path.join(T, '.sdlc/product.json'), JSON.stringify({ platform: 'github', default_branch: 'main' }));
|
|
537
|
+
put(path.join(T, '.claude/settings.json'), JSON.stringify({ hooks: { PreToolUse: [{ matcher: 'Bash', hooks: [{ type: 'command', command: 'theirs.sh' }] }] } }));
|
|
538
|
+
const actions = agentGuardActions(T, ['.claude', '.cursor']);
|
|
539
|
+
assert.deepEqual(actions.map((a) => `${a.scope}/${a.item}:${a.status}`), ['product/hooks/agent-guard.mjs:new', '.claude/settings.json:outdated', '.cursor/hooks.json:new']);
|
|
540
|
+
for (const a of actions) a.apply();
|
|
541
|
+
const claude = JSON.parse(read(path.join(T, '.claude/settings.json')));
|
|
542
|
+
const commands = claude.hooks.PreToolUse.flatMap((e) => e.hooks.map((h) => h.command));
|
|
543
|
+
assert.ok(commands.includes('theirs.sh'), 'the team\'s hook is kept');
|
|
544
|
+
assert.ok(commands.includes(AGENT_GUARD_ADAPTERS['.claude'].command));
|
|
545
|
+
const cursor = JSON.parse(read(path.join(T, '.cursor/hooks.json')));
|
|
546
|
+
assert.equal(cursor.hooks.beforeShellExecution[0].command, 'node hooks/agent-guard.mjs');
|
|
547
|
+
assert.ok(fs.existsSync(path.join(T, 'hooks/agent-guard.mjs')));
|
|
548
|
+
assert.ok(agentGuardActions(T, ['.claude', '.cursor']).every((a) => a.status === 'ok'));
|
|
549
|
+
// `.cursor` leaves the targets: its entry comes out, the rest stays.
|
|
550
|
+
const orphans = orphanAgentGuardActions(T, ['.claude']);
|
|
551
|
+
assert.deepEqual(orphans.map((a) => a.scope), ['.cursor']);
|
|
552
|
+
orphans.forEach((a) => a.apply());
|
|
553
|
+
assert.equal(JSON.parse(read(path.join(T, '.cursor/hooks.json'))).hooks?.beforeShellExecution, undefined);
|
|
554
|
+
// No Product: nothing.
|
|
555
|
+
assert.deepEqual(agentGuardActions(tmp(), ['.claude']), []);
|
|
556
|
+
} finally { fs.rmSync(T, { recursive: true, force: true }); }
|
|
557
|
+
});
|
|
558
|
+
|
|
559
|
+
// ---- the read-only yad commands, pre-approved for Claude Code (E158, issue #353) ------------------------
|
|
560
|
+
|
|
561
|
+
const { readAllowList, COMMAND_CLASS, classify } = await import('./agent-rules.mjs');
|
|
562
|
+
const { mergeAllowList, allowListActions } = await import('./plan.mjs');
|
|
563
|
+
|
|
564
|
+
test('the allow list holds only commands that read, whatever flag they are given', () => {
|
|
565
|
+
const list = readAllowList();
|
|
566
|
+
assert.ok(list.length > 20, `${list.length} entries`);
|
|
567
|
+
assert.ok(list.includes('Bash(yad guide:*)') && list.includes('Bash(yad status:*)') && list.includes('Bash(yad gate status:*)'));
|
|
568
|
+
for (const entry of list) {
|
|
569
|
+
const key = entry.match(/^Bash\(yad (.+):\*\)$/)[1];
|
|
570
|
+
const words = key.split(' ');
|
|
571
|
+
// Every flag the command knows, and every command whose words start with these: all reads.
|
|
572
|
+
const flags = Object.keys(COMMAND_CLASS[key].flags ?? {});
|
|
573
|
+
assert.equal(classify(words, flags), 'read', key);
|
|
574
|
+
for (const other of Object.keys(COMMAND_CLASS)) if (other.startsWith(`${key} `)) assert.equal(COMMAND_CLASS[other].class, 'read', `${key} begins ${other}`);
|
|
575
|
+
}
|
|
576
|
+
for (const never of ['gate approve', 'gate advance', 'check', 'update', 'mode', 'dial', 'report', 'checkpoint', 'standup', 'commit', 'gate']) {
|
|
577
|
+
assert.ok(!list.includes(`Bash(yad ${never}:*)`), never);
|
|
578
|
+
}
|
|
579
|
+
});
|
|
580
|
+
|
|
581
|
+
test('the allow list is added beside the team\'s own entries, once; a shape it cannot read is refused', () => {
|
|
582
|
+
const mine = { permissions: { allow: ['Bash(npm test)'], deny: ['Bash(rm:*)'] }, env: { A: '1' } };
|
|
583
|
+
const once = mergeAllowList(mine);
|
|
584
|
+
assert.equal(once.changed, true);
|
|
585
|
+
assert.equal(once.settings.permissions.allow[0], 'Bash(npm test)');
|
|
586
|
+
assert.deepEqual(once.settings.permissions.deny, ['Bash(rm:*)']);
|
|
587
|
+
assert.deepEqual(once.settings.env, { A: '1' });
|
|
588
|
+
assert.equal(mergeAllowList(once.settings).changed, false);
|
|
589
|
+
assert.ok(mergeAllowList({ permissions: [] }).unreadable);
|
|
590
|
+
assert.ok(mergeAllowList({ permissions: { allow: 'x' } }).unreadable);
|
|
591
|
+
const T = tmp();
|
|
592
|
+
try {
|
|
593
|
+
put(path.join(T, '.sdlc/product.json'), JSON.stringify({ platform: 'github', default_branch: 'main' }));
|
|
594
|
+
assert.deepEqual(allowListActions(T, ['.agents']), [], 'only for Claude Code');
|
|
595
|
+
const [a] = allowListActions(T, ['.claude']);
|
|
596
|
+
assert.equal(a.status, 'new');
|
|
597
|
+
assert.deepEqual(a.paths, [], '--push never stages the team\'s settings');
|
|
598
|
+
a.apply();
|
|
599
|
+
assert.deepEqual(JSON.parse(read(path.join(T, '.claude/settings.json'))).permissions.allow, readAllowList());
|
|
600
|
+
assert.equal(allowListActions(T, ['.claude'])[0].status, 'ok');
|
|
601
|
+
} finally { fs.rmSync(T, { recursive: true, force: true }); }
|
|
602
|
+
});
|
|
603
|
+
|
|
604
|
+
// ---- yad report from the agent: --kind, and --post --confirm (E158) ---------------------------------------
|
|
605
|
+
|
|
606
|
+
const { runReport, confirmHash } = await import('./report.mjs');
|
|
607
|
+
|
|
608
|
+
test('yad report --kind enhancement: its label, title and body; a bug keeps its error block and command', async () => {
|
|
609
|
+
const quiet = { searcher: () => ({ ok: true, matches: [] }), interactive: false, opener: () => {} };
|
|
610
|
+
const idea = await runReport(tmp(), { ...quiet, message: 'guide could list skipped steps', kind: 'enhancement', argv: ['report', '-m', 'x', '--kind', 'enhancement', '--json'] });
|
|
611
|
+
assert.deepEqual(idea.labels, ['enhancement']);
|
|
612
|
+
assert.equal(idea.title, '[report] Enhancement: guide could list skipped steps');
|
|
613
|
+
assert.match(idea.body, /### What would help\?/);
|
|
614
|
+
assert.doesNotMatch(idea.body, /### Error|Command:/);
|
|
615
|
+
assert.equal(idea.confirm, confirmHash(idea));
|
|
616
|
+
const bug = await runReport(tmp(), { ...quiet, message: 'it broke', error: Object.assign(new Error('boom'), { code: 'YAD-CLI-001' }), argv: ['report', '-m', 'x', '--json'] });
|
|
617
|
+
assert.deepEqual(bug.labels, ['bug']);
|
|
618
|
+
assert.match(bug.body, /### Error/);
|
|
619
|
+
assert.match(bug.body, /Command: `yad report -m`/, 'the report\'s own flags never reach the posted command line');
|
|
620
|
+
});
|
|
621
|
+
|
|
622
|
+
test('yad report --post posts only the text the preview showed: a matching --confirm posts it, anything else posts nothing', async () => {
|
|
623
|
+
const D = tmp();
|
|
624
|
+
try {
|
|
625
|
+
const posted = [];
|
|
626
|
+
const seams = { searcher: () => ({ ok: true, matches: [] }), interactive: false, opener: () => {}, authed: () => true, filer: (p, repo, payload) => { posted.push(payload); return { ok: true, url: 'https://github.com/x/y/issues/9' }; } };
|
|
627
|
+
const preview = await runReport(D, { ...seams, message: 'an idea', kind: 'enhancement', argv: ['report', '-m', 'an idea', '--kind', 'enhancement', '--json'] });
|
|
628
|
+
assert.equal(preview.filed, false);
|
|
629
|
+
assert.equal(posted.length, 0, '--json never posts');
|
|
630
|
+
const postArgv = ['report', '-m', 'an idea', '--kind', 'enhancement', '--post', '--confirm', preview.confirm];
|
|
631
|
+
for (const confirm of ['0'.repeat(64), null]) {
|
|
632
|
+
const r = await runReport(D, { ...seams, message: 'an idea', kind: 'enhancement', post: true, confirm, argv: postArgv });
|
|
633
|
+
assert.equal(r.refused, 'confirm-mismatch');
|
|
634
|
+
assert.equal(posted.length, 0, `confirm ${confirm} posts nothing`);
|
|
635
|
+
}
|
|
636
|
+
// The text changed since the preview (another summary): refused.
|
|
637
|
+
const other = await runReport(D, { ...seams, message: 'another idea', kind: 'enhancement', post: true, confirm: preview.confirm, argv: postArgv });
|
|
638
|
+
assert.equal(other.refused, 'confirm-mismatch');
|
|
639
|
+
assert.equal(posted.length, 0);
|
|
640
|
+
const r = await runReport(D, { ...seams, message: 'an idea', kind: 'enhancement', post: true, confirm: preview.confirm, argv: postArgv });
|
|
641
|
+
assert.equal(r.filed, true);
|
|
642
|
+
assert.deepEqual(posted, [{ title: preview.title, body: preview.body, labels: preview.labels }]);
|
|
643
|
+
// No gh login: the prefilled link, nothing posted, no browser opened.
|
|
644
|
+
let opened = 0;
|
|
645
|
+
const link = await runReport(D, { ...seams, authed: () => false, opener: () => { opened++; }, message: 'an idea', kind: 'enhancement', post: true, confirm: preview.confirm, argv: postArgv });
|
|
646
|
+
assert.equal(link.filed, false);
|
|
647
|
+
assert.match(link.url, /issues\/new/);
|
|
648
|
+
assert.equal(opened, 0);
|
|
649
|
+
assert.equal(posted.length, 1);
|
|
650
|
+
} finally { fs.rmSync(D, { recursive: true, force: true }); process.exitCode = 0; }
|
|
651
|
+
});
|
|
652
|
+
|
|
653
|
+
test('yad report refuses --post without --confirm, --confirm without --post, and an unknown --kind', () => {
|
|
654
|
+
const YAD = path.join(path.dirname(new URL(import.meta.url).pathname), '..', 'bin', 'yad.mjs');
|
|
655
|
+
const env = { ...process.env, YAD_NO_REPORT: '1', SDLC_NONINTERACTIVE: '1' };
|
|
656
|
+
for (const [args, re] of [
|
|
657
|
+
[['report', '-m', 'x', '--post'], /needs --confirm/],
|
|
658
|
+
[['report', '-m', 'x', '--confirm', 'abc'], /--confirm is for --post/],
|
|
659
|
+
[['report', '-m', 'x', '--kind', 'feature'], /--kind takes bug or enhancement/],
|
|
660
|
+
]) {
|
|
661
|
+
const r = spawnSync(process.execPath, [YAD, ...args, '--json'], { encoding: 'utf8', env, cwd: os.tmpdir(), timeout: 30_000 });
|
|
662
|
+
assert.notEqual(r.status, 0, args.join(' '));
|
|
663
|
+
assert.match(r.stdout + r.stderr, re, args.join(' '));
|
|
664
|
+
}
|
|
665
|
+
});
|
|
666
|
+
|
|
667
|
+
// ---- the yad-guide skill (E158) ------------------------------------------------------------------------
|
|
668
|
+
|
|
669
|
+
const REPO = path.join(path.dirname(new URL(import.meta.url).pathname), '..');
|
|
670
|
+
const { SKILLS } = await import('./manifest.mjs');
|
|
671
|
+
const { syncGuideSkill, guideSections, GUIDE_SECTIONS } = await import('./agent-guide.mjs');
|
|
672
|
+
|
|
673
|
+
test('the yad-guide skill is shipped, and its generated sections agree with the rules (scripts/agent-guide.mjs --check)', () => {
|
|
674
|
+
assert.ok(SKILLS.includes('yad-guide'));
|
|
675
|
+
const file = path.join(REPO, 'skills/yad-guide/SKILL.md');
|
|
676
|
+
const r = syncGuideSkill(file, { check: true });
|
|
677
|
+
assert.deepEqual(r.refused, []);
|
|
678
|
+
assert.deepEqual(r.stale, [], 'run `node scripts/agent-guide.mjs`');
|
|
679
|
+
const text = read(file);
|
|
680
|
+
for (const name of GUIDE_SECTIONS) assert.ok(text.includes(`<!-- end: ${name} -->`), name);
|
|
681
|
+
for (const c of humanOnlyCommands()) assert.ok(text.includes(`\`${c}\``), c);
|
|
682
|
+
for (const n of NEVER) assert.ok(text.includes(n.text), n.id);
|
|
683
|
+
// Each generated line pairs its backticks, or every code span after an odd one reads wrongly.
|
|
684
|
+
for (const line of Object.values(guideSections()).flat()) assert.equal((line.match(/`/g) ?? []).length % 2, 0, line);
|
|
685
|
+
const cli = spawnSync(process.execPath, [path.join(REPO, 'scripts/agent-guide.mjs'), '--check'], { encoding: 'utf8' });
|
|
686
|
+
assert.equal(cli.status, 0, cli.stdout + cli.stderr);
|
|
687
|
+
// A stale section is found, and missing markers are refused rather than guessed.
|
|
688
|
+
const T = tmp();
|
|
689
|
+
try {
|
|
690
|
+
put(path.join(T, 'SKILL.md'), text.replace(NEVER[0].text, 'something else'));
|
|
691
|
+
assert.deepEqual(syncGuideSkill(path.join(T, 'SKILL.md'), { check: true }).stale, ['never']);
|
|
692
|
+
put(path.join(T, 'SKILL.md'), text.replace('<!-- end: rules -->', ''));
|
|
693
|
+
assert.equal(syncGuideSkill(path.join(T, 'SKILL.md'), { check: true }).refused.length, 1);
|
|
694
|
+
} finally { fs.rmSync(T, { recursive: true, force: true }); }
|
|
695
|
+
});
|
|
696
|
+
|
|
697
|
+
test('every skill\'s description fits the 1,024 characters an agent harness reads', () => {
|
|
698
|
+
for (const skill of fs.readdirSync(path.join(REPO, 'skills'))) {
|
|
699
|
+
const file = path.join(REPO, 'skills', skill, 'SKILL.md');
|
|
700
|
+
if (!fs.existsSync(file)) continue;
|
|
701
|
+
const m = read(file).match(/^description:\s*(.*)$/m);
|
|
702
|
+
if (!m) continue;
|
|
703
|
+
let v = m[1].trim();
|
|
704
|
+
if (v.startsWith('\'')) v = v.slice(1, -1).replace(/''/g, '\'');
|
|
705
|
+
else if (v.startsWith('"')) v = JSON.parse(v);
|
|
706
|
+
assert.ok(v.length <= 1024, `${skill}: ${v.length} characters`);
|
|
707
|
+
}
|
|
708
|
+
});
|
|
709
|
+
|
|
710
|
+
test('no skill tells the agent to run a human-only command: only the person runs it', () => {
|
|
711
|
+
const human = humanOnlyCommands().map((c) => c.replace(/^yad /, ''));
|
|
712
|
+
const bad = [];
|
|
713
|
+
for (const skill of fs.readdirSync(path.join(REPO, 'skills'))) {
|
|
714
|
+
const file = path.join(REPO, 'skills', skill, 'SKILL.md');
|
|
715
|
+
if (!fs.existsSync(file)) continue;
|
|
716
|
+
read(file).split('\n').forEach((line, i) => {
|
|
717
|
+
for (const c of human) {
|
|
718
|
+
const [words, flag] = c.split(' --');
|
|
719
|
+
const run = new RegExp(`\\b[Rr]uns? \`yad ${words}${flag ? `[^\`]*--${flag}` : ''}\\b`);
|
|
720
|
+
if (run.test(line) && !/person/.test(line)) bad.push(`skills/${skill}/SKILL.md:${i + 1}: ${line.trim()}`);
|
|
721
|
+
}
|
|
722
|
+
});
|
|
723
|
+
}
|
|
724
|
+
assert.deepEqual(bad, []);
|
|
725
|
+
// The review gate hands its human-only commands over, and says how to confirm them.
|
|
726
|
+
const gate = read(path.join(REPO, 'skills/yad-review-gate/SKILL.md'));
|
|
727
|
+
assert.match(gate, /are the person's to run, never yours \(E158\)/);
|
|
728
|
+
assert.match(gate, /yad gate status <epic>/);
|
|
729
|
+
assert.match(read(path.join(REPO, 'skills/yad-commit/SKILL.md')), /Never pass `--manual` yourself, even when asked/);
|
|
730
|
+
});
|
|
731
|
+
|
|
732
|
+
test('every `yad … --push` a skill gives to copy says to ask the person first', () => {
|
|
733
|
+
// A command in a code block is what an agent copies and runs; --push lands on the default branch, which the
|
|
734
|
+
// agent asks for every time (E158 review 3: `checkpoint --retro-ship … --push` had no such line).
|
|
735
|
+
const bad = [];
|
|
736
|
+
const walk = (dir) => {
|
|
737
|
+
for (const e of fs.readdirSync(dir, { withFileTypes: true })) {
|
|
738
|
+
const p = path.join(dir, e.name);
|
|
739
|
+
if (e.isDirectory()) { walk(p); continue; }
|
|
740
|
+
if (!e.name.endsWith('.md')) continue;
|
|
741
|
+
const lines = read(p).split('\n');
|
|
742
|
+
let inBlock = false;
|
|
743
|
+
lines.forEach((line, i) => {
|
|
744
|
+
if (line.trim().startsWith('```')) { inBlock = !inBlock; return; }
|
|
745
|
+
if (!inBlock || !/\byad .*--push\b/.test(line)) return;
|
|
746
|
+
if (!/\bask|person/i.test(lines.slice(Math.max(0, i - 12), i + 1).join('\n'))) bad.push(`${path.relative(REPO, p)}:${i + 1}: ${line.trim()}`);
|
|
747
|
+
});
|
|
748
|
+
}
|
|
749
|
+
};
|
|
750
|
+
walk(path.join(REPO, 'skills'));
|
|
751
|
+
assert.deepEqual(bad, []);
|
|
752
|
+
});
|
|
753
|
+
|
|
754
|
+
test('a bug report previews and posts the same text: its Command line too', async () => {
|
|
755
|
+
const D = tmp();
|
|
756
|
+
try {
|
|
757
|
+
const posted = [];
|
|
758
|
+
const seams = { searcher: () => ({ ok: true, matches: [] }), interactive: false, opener: () => {}, authed: () => true, filer: (p, repo, payload) => { posted.push(payload); return { ok: true, url: 'u' }; } };
|
|
759
|
+
const error = Object.assign(new Error('boom'), { code: 'YAD-CLI-001' });
|
|
760
|
+
const preview = await runReport(D, { ...seams, error, message: 'it broke', argv: ['report', '-m', 'it broke', '--json'] });
|
|
761
|
+
const r = await runReport(D, { ...seams, error, message: 'it broke', post: true, confirm: preview.confirm, argv: ['report', '-m', 'it broke', '--post', '--confirm', preview.confirm] });
|
|
762
|
+
assert.equal(r.filed, true);
|
|
763
|
+
assert.deepEqual(posted, [{ title: preview.title, body: preview.body, labels: ['bug'] }]);
|
|
764
|
+
} finally { fs.rmSync(D, { recursive: true, force: true }); process.exitCode = 0; }
|
|
765
|
+
});
|
|
766
|
+
|
|
767
|
+
test('yad report with no terminal never prompts, so it never posts', async () => {
|
|
768
|
+
let asked = 0;
|
|
769
|
+
// Only the terminal check may stop it: the switch that also would is cleared for this run.
|
|
770
|
+
const saved = process.env.SDLC_NONINTERACTIVE;
|
|
771
|
+
delete process.env.SDLC_NONINTERACTIVE;
|
|
772
|
+
assert.ok(!process.stdin.isTTY || !process.stdout.isTTY, 'the test runner has no terminal');
|
|
773
|
+
const r = await runReport(tmp(), { message: 'x', searcher: () => ({ ok: true, matches: [] }), opener: () => {}, prompter: async () => { asked++; return true; }, filer: () => { throw new Error('posted'); }, argv: ['report', '-m', 'x'] });
|
|
774
|
+
if (saved !== undefined) process.env.SDLC_NONINTERACTIVE = saved;
|
|
775
|
+
assert.equal(r.filed, false);
|
|
776
|
+
assert.equal(asked, 0);
|
|
777
|
+
});
|
|
778
|
+
|
|
779
|
+
const { orphanAllowListActions } = await import('./plan.mjs');
|
|
780
|
+
const { RETIRED_ALLOW_ENTRIES, SHIPPED_ALLOW_LISTS } = await import('./agent-rules.mjs');
|
|
781
|
+
|
|
782
|
+
test('the allow list comes out again when Claude Code leaves the targets; the team\'s entries stay', () => {
|
|
783
|
+
const T = tmp();
|
|
784
|
+
try {
|
|
785
|
+
put(path.join(T, '.sdlc/product.json'), JSON.stringify({ platform: 'github', default_branch: 'main' }));
|
|
786
|
+
const [a] = allowListActions(T, ['.claude']);
|
|
787
|
+
a.apply();
|
|
788
|
+
const file = path.join(T, '.claude/settings.json');
|
|
789
|
+
const s = JSON.parse(read(file));
|
|
790
|
+
s.permissions.allow.push('Bash(npm test)');
|
|
791
|
+
put(file, JSON.stringify(s));
|
|
792
|
+
assert.deepEqual(orphanAllowListActions(T, ['.claude']), [], 'nothing retired while .claude is a target');
|
|
793
|
+
// One of yad's strings the team typed by hand, without yad's whole list: theirs, kept.
|
|
794
|
+
const U = tmp();
|
|
795
|
+
put(path.join(U, '.sdlc/product.json'), JSON.stringify({ platform: 'github', default_branch: 'main' }));
|
|
796
|
+
put(path.join(U, '.claude/settings.json'), JSON.stringify({ permissions: { allow: ['Bash(yad status:*)'] } }));
|
|
797
|
+
assert.deepEqual(orphanAllowListActions(U, ['.cursor']), []);
|
|
798
|
+
fs.rmSync(U, { recursive: true, force: true });
|
|
799
|
+
const gone = orphanAllowListActions(T, ['.agents']);
|
|
800
|
+
assert.equal(gone.length, 1);
|
|
801
|
+
gone[0].apply();
|
|
802
|
+
assert.deepEqual(JSON.parse(read(file)).permissions.allow, ['Bash(npm test)']);
|
|
803
|
+
} finally { fs.rmSync(T, { recursive: true, force: true }); }
|
|
804
|
+
});
|
|
805
|
+
|
|
806
|
+
test('a command that leaves the allow list is retired, so check --fix takes it out', () => {
|
|
807
|
+
// Today's list is the last one shipped: when it changes, append it to SHIPPED_ALLOW_LISTS (cli/agent-rules.mjs),
|
|
808
|
+
// and put each entry that left in RETIRED_ALLOW_ENTRIES.
|
|
809
|
+
const now = readAllowList();
|
|
810
|
+
assert.deepEqual(now, SHIPPED_ALLOW_LISTS.at(-1), 'append the new list to SHIPPED_ALLOW_LISTS');
|
|
811
|
+
for (const e of SHIPPED_ALLOW_LISTS.flat()) assert.ok(now.includes(e) || RETIRED_ALLOW_ENTRIES.includes(e), `${e} left the list: add it to RETIRED_ALLOW_ENTRIES`);
|
|
812
|
+
});
|
|
813
|
+
|
|
814
|
+
test('no pre-approved command reads a flag bin/yad.mjs uses to write, push, post or approve', () => {
|
|
815
|
+
// The flags a command reads in bin/yad.mjs, by the `case` that runs it.
|
|
816
|
+
const src = fs.readFileSync(path.join(REPO, 'bin', 'yad.mjs'), 'utf8');
|
|
817
|
+
const sw = src.slice(src.indexOf('switch (cmd) {'));
|
|
818
|
+
const blocks = {};
|
|
819
|
+
const re = /^\s{4}case '([a-z-]+)':/gm;
|
|
820
|
+
const hits = [...sw.matchAll(re)];
|
|
821
|
+
hits.forEach((m, i) => { blocks[m[1]] = sw.slice(m.index, hits[i + 1]?.index ?? sw.length); });
|
|
822
|
+
// A write flag the command reads must be one the rules know, on the command or one of its actions: a flag
|
|
823
|
+
// the rules know raises that command's class, which keeps it off the list. One they do not know is a hole.
|
|
824
|
+
const WRITES = { out: 'out', fix: 'fix', push: 'push', apply: 'apply', post: 'post', openPr: 'open-pr', overwriteLocal: 'overwrite-local', to: 'to', ship: 'ship' };
|
|
825
|
+
const known = (cmd, flag) => Object.entries(COMMAND_CLASS).some(([k, e]) => (k === cmd || k.startsWith(`${cmd} `)) && Object.hasOwn(e.flags ?? {}, flag));
|
|
826
|
+
for (const entry of readAllowList()) {
|
|
827
|
+
const [cmd] = entry.match(/^Bash\(yad (.+):\*\)$/)[1].split(' ');
|
|
828
|
+
const body = blocks[cmd] ?? '';
|
|
829
|
+
for (const [prop, flag] of Object.entries(WRITES)) {
|
|
830
|
+
if (new RegExp(`\\bo\\.${prop}\\b`).test(body)) assert.ok(known(cmd, flag), `yad ${cmd} reads --${flag}, which the rules do not class`);
|
|
831
|
+
}
|
|
832
|
+
}
|
|
833
|
+
});
|
|
834
|
+
|
|
835
|
+
test('a PR/MR approval or merge in a skill is always handed to the person', () => {
|
|
836
|
+
const bad = [];
|
|
837
|
+
for (const skill of fs.readdirSync(path.join(REPO, 'skills'))) {
|
|
838
|
+
const file = path.join(REPO, 'skills', skill, 'SKILL.md');
|
|
839
|
+
if (!fs.existsSync(file)) continue;
|
|
840
|
+
for (const para of read(file).split(/\n\s*\n/)) {
|
|
841
|
+
if (/`(gh pr review[^`]*--approve|gh pr merge|glab mr (approve|merge))/.test(para) && !/the person|they submit|the human submits/.test(para)) bad.push(`${skill}: ${para.slice(0, 120)}`);
|
|
842
|
+
}
|
|
843
|
+
}
|
|
844
|
+
assert.deepEqual(bad, []);
|
|
845
|
+
});
|
|
846
|
+
|
|
847
|
+
// ---- E158b: the agent guard and the allow list in a connected code repo -------------------------------
|
|
848
|
+
// The case list is in the E158b roadmap row; each test names the cases it covers.
|
|
849
|
+
|
|
850
|
+
const { repoOrphanAgentGuardActions } = await import('./plan.mjs');
|
|
851
|
+
const { REPO_AGENT_GUARD_DEST } = await import('./manifest.mjs');
|
|
852
|
+
const guardRepo = () => {
|
|
853
|
+
const T = tmp();
|
|
854
|
+
const P = path.join(T, 'product');
|
|
855
|
+
const R = path.join(T, 'api');
|
|
856
|
+
fs.mkdirSync(P); fs.mkdirSync(R);
|
|
857
|
+
put(path.join(P, '.sdlc/product.json'), JSON.stringify({ platform: 'github', default_branch: 'main' }));
|
|
858
|
+
return { T, P, R, repo: { name: 'api', path: '../api', platform: 'github' } };
|
|
859
|
+
};
|
|
860
|
+
const stamp = (P, ideTargets) => put(path.join(P, '.sdlc/cli-version.json'), JSON.stringify({ version: '4.8.0', ideTargets }));
|
|
861
|
+
const guardItems = (actions) => actions.filter((a) => a.item.startsWith(REPO_AGENT_GUARD_DEST) || a.item.includes('(agent guard)') || a.allowList);
|
|
862
|
+
|
|
863
|
+
test('E158b cases 1, 2, 5, 7: a cloned code repo gets the guard and the allow list beside its own entries; a second run is all ok', () => {
|
|
864
|
+
const { T, P, R, repo } = guardRepo();
|
|
865
|
+
try {
|
|
866
|
+
stamp(P, ['.claude', '.cursor']);
|
|
867
|
+
const theirs = { hooks: { PreToolUse: [{ matcher: 'Bash', hooks: [{ type: 'command', command: 'theirs.sh' }] }] }, permissions: { allow: ['Bash(npm test)'] } };
|
|
868
|
+
put(path.join(R, '.claude/settings.json'), JSON.stringify(theirs));
|
|
869
|
+
const actions = guardItems(repoActions(P, repo));
|
|
870
|
+
assert.deepEqual(actions.map((a) => `${a.scope}/${a.item}`), [
|
|
871
|
+
'api/checks/agent-guard.mjs', 'api/.claude/settings.json (agent guard)', 'api/.cursor/hooks.json (agent guard)', 'api/.claude/settings.json (read-only yad commands)',
|
|
872
|
+
]);
|
|
873
|
+
// The team's files are never staged by --push; the script is a wired file like the other checks/ files.
|
|
874
|
+
assert.deepEqual(actions.map((a) => a.paths), [[REPO_AGENT_GUARD_DEST], [], [], []]);
|
|
875
|
+
for (const a of actions) a.apply();
|
|
876
|
+
const claude = JSON.parse(read(path.join(R, '.claude/settings.json')));
|
|
877
|
+
const commands = claude.hooks.PreToolUse.flatMap((e) => e.hooks.map((h) => h.command));
|
|
878
|
+
assert.ok(commands.includes('theirs.sh'), 'the team\'s hook is kept');
|
|
879
|
+
assert.ok(commands.includes('node "$CLAUDE_PROJECT_DIR/checks/agent-guard.mjs"'), commands.join(', '));
|
|
880
|
+
assert.equal(claude.permissions.allow[0], 'Bash(npm test)', 'the team\'s entry is kept, first');
|
|
881
|
+
assert.deepEqual(claude.permissions.allow.slice(1), readAllowList());
|
|
882
|
+
assert.equal(JSON.parse(read(path.join(R, '.cursor/hooks.json'))).hooks.beforeShellExecution[0].command, 'node checks/agent-guard.mjs');
|
|
883
|
+
assert.ok(fs.existsSync(path.join(R, REPO_AGENT_GUARD_DEST)));
|
|
884
|
+
assert.ok(guardItems(repoActions(P, repo)).every((a) => a.status === 'ok'), 'a second run changes nothing');
|
|
885
|
+
// Case 5: Claude Code only, or Cursor only — only that tool's files.
|
|
886
|
+
stamp(P, ['.claude']);
|
|
887
|
+
assert.ok(!guardItems(repoActions(P, repo)).some((a) => a.item.startsWith('.cursor')));
|
|
888
|
+
stamp(P, ['.cursor']);
|
|
889
|
+
assert.deepEqual(guardItems(repoActions(P, repo)).map((a) => a.item), ['checks/agent-guard.mjs', '.cursor/hooks.json (agent guard)']);
|
|
890
|
+
stamp(P, ['.agents']);
|
|
891
|
+
assert.deepEqual(guardItems(repoActions(P, repo)), [], 'no tool with hooks: no script either');
|
|
892
|
+
} finally { fs.rmSync(T, { recursive: true, force: true }); }
|
|
893
|
+
});
|
|
894
|
+
|
|
895
|
+
test('E158b cases 3, 4, 6: a settings file that does not parse, a repo not cloned, the Product itself, a linked target folder', () => {
|
|
896
|
+
const { T, P, R, repo } = guardRepo();
|
|
897
|
+
const said = [];
|
|
898
|
+
const orig = console.error;
|
|
899
|
+
try {
|
|
900
|
+
stamp(P, ['.claude']);
|
|
901
|
+
put(path.join(R, '.claude/settings.json'), '{ not json');
|
|
902
|
+
console.error = (...a) => said.push(a.join(' '));
|
|
903
|
+
const actions = guardItems(repoActions(P, repo)).filter((a) => a.item.startsWith('.claude'));
|
|
904
|
+
console.error = orig;
|
|
905
|
+
assert.ok(actions.length === 2 && actions.every((a) => a.status === 'modified' && a.writesNothing), JSON.stringify(actions.map((a) => a.status)));
|
|
906
|
+
assert.equal(said.filter((l) => /api\/\.claude\/settings\.json does not parse/.test(l)).length, 1, `said once, naming the repo: ${said.join(' | ')}`);
|
|
907
|
+
actions.forEach((a) => a.apply());
|
|
908
|
+
assert.equal(read(path.join(R, '.claude/settings.json')), '{ not json');
|
|
909
|
+
// Case 4: a repo not cloned here, and the Product registered as a repo.
|
|
910
|
+
assert.deepEqual(guardItems(repoActions(P, { name: 'web', path: '../web', platform: 'github' })), []);
|
|
911
|
+
assert.deepEqual(guardItems(repoActions(P, { name: 'product', path: '.', platform: 'github' })), []);
|
|
912
|
+
// Case 6: the repo's .claude is a link: named, and nothing is written through it.
|
|
913
|
+
fs.rmSync(path.join(R, '.claude'), { recursive: true });
|
|
914
|
+
const elsewhere = path.join(T, 'elsewhere');
|
|
915
|
+
fs.mkdirSync(elsewhere);
|
|
916
|
+
fs.symlinkSync(elsewhere, path.join(R, '.claude'));
|
|
917
|
+
said.length = 0;
|
|
918
|
+
console.error = (...a) => said.push(a.join(' '));
|
|
919
|
+
const linked = repoActions(P, repo);
|
|
920
|
+
console.error = orig;
|
|
921
|
+
assert.deepEqual(guardItems(linked), []);
|
|
922
|
+
assert.ok(said.some((l) => /api: unsafe IDE target '\.claude'/.test(l)), said.join(' | '));
|
|
923
|
+
assert.deepEqual(fs.readdirSync(elsewhere), []);
|
|
924
|
+
} finally { console.error = orig; fs.rmSync(T, { recursive: true, force: true }); }
|
|
925
|
+
});
|
|
926
|
+
|
|
927
|
+
test('E158b case 10: a target that leaves takes yad\'s entries out of the repo\'s files; the team\'s stay, and so does the script', () => {
|
|
928
|
+
const { T, P, R, repo } = guardRepo();
|
|
929
|
+
try {
|
|
930
|
+
stamp(P, ['.claude', '.cursor']);
|
|
931
|
+
put(path.join(R, '.claude/settings.json'), JSON.stringify({ permissions: { allow: ['Bash(npm test)'] } }));
|
|
932
|
+
guardItems(repoActions(P, repo)).forEach((a) => a.apply());
|
|
933
|
+
assert.deepEqual(repoOrphanAgentGuardActions(R, ['.claude', '.cursor'], { scope: 'api' }), [], 'nothing while both are targets');
|
|
934
|
+
const gone = repoOrphanAgentGuardActions(R, ['.agents'], { scope: 'api' });
|
|
935
|
+
assert.deepEqual(gone.map((a) => `${a.scope}/${a.item}`).sort(), [
|
|
936
|
+
'api/hooks.json (agent guard entry removed)', 'api/settings.json (agent guard entry removed)', 'api/settings.json (read-only yad commands removed)',
|
|
937
|
+
]);
|
|
938
|
+
gone.forEach((a) => a.apply());
|
|
939
|
+
const claude = JSON.parse(read(path.join(R, '.claude/settings.json')));
|
|
940
|
+
assert.deepEqual(claude.permissions.allow, ['Bash(npm test)']);
|
|
941
|
+
assert.equal(claude.hooks?.PreToolUse, undefined);
|
|
942
|
+
assert.equal(JSON.parse(read(path.join(R, '.cursor/hooks.json'))).hooks?.beforeShellExecution, undefined);
|
|
943
|
+
assert.ok(fs.existsSync(path.join(R, REPO_AGENT_GUARD_DEST)), 'the script stays');
|
|
944
|
+
assert.deepEqual(repoOrphanAgentGuardActions(path.join(T, 'nowhere'), ['.agents'], { scope: 'web' }), []);
|
|
945
|
+
// Review 1: a repo whose .claude is a link to another folder: nothing is removed through it, a target or not.
|
|
946
|
+
guardItems(repoActions(P, repo)).forEach((a) => a.apply());
|
|
947
|
+
assert.equal(repoOrphanAgentGuardActions(R, ['.agents'], { scope: 'api' }).filter((a) => a.item.startsWith('settings.json')).length, 2, 'yad\'s entries are back, so there is something to remove');
|
|
948
|
+
const shared = path.join(T, 'shared');
|
|
949
|
+
fs.renameSync(path.join(R, '.claude'), shared);
|
|
950
|
+
fs.symlinkSync(shared, path.join(R, '.claude'));
|
|
951
|
+
const before = read(path.join(shared, 'settings.json'));
|
|
952
|
+
assert.deepEqual(repoOrphanAgentGuardActions(R, ['.claude'], { scope: 'api' }).filter((a) => a.item.startsWith('settings.json')), []);
|
|
953
|
+
assert.deepEqual(repoOrphanAgentGuardActions(R, ['.agents'], { scope: 'api' }).filter((a) => a.item.startsWith('settings.json')), []);
|
|
954
|
+
assert.equal(read(path.join(shared, 'settings.json')), before);
|
|
955
|
+
// Review 2: the same for a linked .cursor.
|
|
956
|
+
const sharedCursor = path.join(T, 'shared-cursor');
|
|
957
|
+
fs.renameSync(path.join(R, '.cursor'), sharedCursor);
|
|
958
|
+
fs.symlinkSync(sharedCursor, path.join(R, '.cursor'));
|
|
959
|
+
const cursorBefore = read(path.join(sharedCursor, 'hooks.json'));
|
|
960
|
+
assert.deepEqual(repoOrphanAgentGuardActions(R, ['.agents'], { scope: 'api' }).filter((a) => a.item.startsWith('hooks.json')), []);
|
|
961
|
+
assert.equal(read(path.join(sharedCursor, 'hooks.json')), cursorBefore);
|
|
962
|
+
} finally { fs.rmSync(T, { recursive: true, force: true }); }
|
|
963
|
+
});
|
|
964
|
+
|
|
965
|
+
test('E158b cases 11, 12: checks/agent-guard.mjs in a code repo refuses and allows as in the Product, and fails open', () => {
|
|
966
|
+
const T = tmp();
|
|
967
|
+
try {
|
|
968
|
+
put(path.join(T, REPO_AGENT_GUARD_DEST), read(path.join(path.dirname(new URL(import.meta.url).pathname), '..', 'skills/yad-checks/templates/hooks/agent-guard.mjs')));
|
|
969
|
+
const run = (payload, env = {}) => {
|
|
970
|
+
const r = spawnSync(process.execPath, [path.join(T, REPO_AGENT_GUARD_DEST)], {
|
|
971
|
+
input: JSON.stringify(payload), encoding: 'utf8', cwd: T,
|
|
972
|
+
env: { ...process.env, YAD_BIN: `"${process.execPath}" "${YAD_MJS}"`, YAD_HOOK_DISABLE: '', ...env }, timeout: 30_000,
|
|
973
|
+
});
|
|
974
|
+
return { code: r.status, out: (r.stdout || '').trim() };
|
|
975
|
+
};
|
|
976
|
+
assert.equal(run({ hook_event_name: 'PreToolUse', tool_name: 'Bash', tool_input: { command: 'yad gate approve EP-x epic' } }).code, 2);
|
|
977
|
+
assert.equal(run({ hook_event_name: 'PreToolUse', tool_name: 'Bash', tool_input: { command: 'yad status' } }).code, 0);
|
|
978
|
+
assert.equal(JSON.parse(run({ hook_event_name: 'beforeShellExecution', command: 'gh pr merge 5' }).out).permission, 'deny');
|
|
979
|
+
assert.deepEqual(JSON.parse(run({ hook_event_name: 'beforeShellExecution', command: 'npm test' }).out), { permission: 'allow' });
|
|
980
|
+
// Case 12: no yad reachable from the repo — allowed.
|
|
981
|
+
assert.deepEqual(JSON.parse(run({ hook_event_name: 'beforeShellExecution', command: 'git push -f' }, { YAD_BIN: '', PATH: '' }).out), { permission: 'allow' });
|
|
982
|
+
} finally { fs.rmSync(T, { recursive: true, force: true }); }
|
|
983
|
+
});
|
|
984
|
+
|
|
985
|
+
test('E158b case 14: on a repo wired before E158b the guard files are new, so yad update adds them; on an unwired repo they stay missing', () => {
|
|
986
|
+
const { T, P, R, repo } = guardRepo();
|
|
987
|
+
try {
|
|
988
|
+
stamp(P, ['.claude']);
|
|
989
|
+
assert.ok(guardItems(repoActions(P, repo)).every((a) => a.status === 'missing'), 'update never does a repo\'s one-time setup');
|
|
990
|
+
put(path.join(R, 'checks/spec-link.sh'), '#!/usr/bin/env bash\n');
|
|
991
|
+
assert.deepEqual(guardItems(repoActions(P, repo)).map((a) => a.status), ['new', 'new', 'new']);
|
|
992
|
+
} finally { fs.rmSync(T, { recursive: true, force: true }); }
|
|
993
|
+
});
|