@memnexus-ai/mx-agent-cli 0.1.292 → 0.1.294-next.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/dist/__tests__/config-sync.test.js +54 -1
- package/dist/__tests__/config-sync.test.js.map +1 -1
- package/dist/__tests__/decision-check.test.d.ts +2 -0
- package/dist/__tests__/decision-check.test.d.ts.map +1 -0
- package/dist/__tests__/decision-check.test.js +105 -0
- package/dist/__tests__/decision-check.test.js.map +1 -0
- package/dist/__tests__/doctor-framework-version.test.d.ts +2 -0
- package/dist/__tests__/doctor-framework-version.test.d.ts.map +1 -0
- package/dist/__tests__/doctor-framework-version.test.js +146 -0
- package/dist/__tests__/doctor-framework-version.test.js.map +1 -0
- package/dist/__tests__/harness-extract.test.d.ts +19 -0
- package/dist/__tests__/harness-extract.test.d.ts.map +1 -0
- package/dist/__tests__/harness-extract.test.js +566 -0
- package/dist/__tests__/harness-extract.test.js.map +1 -0
- package/dist/__tests__/harness-wiring.test.d.ts +16 -0
- package/dist/__tests__/harness-wiring.test.d.ts.map +1 -0
- package/dist/__tests__/harness-wiring.test.js +469 -0
- package/dist/__tests__/harness-wiring.test.js.map +1 -0
- package/dist/__tests__/mx-agent-cli-pin-validation.test.d.ts +2 -0
- package/dist/__tests__/mx-agent-cli-pin-validation.test.d.ts.map +1 -0
- package/dist/__tests__/mx-agent-cli-pin-validation.test.js +256 -0
- package/dist/__tests__/mx-agent-cli-pin-validation.test.js.map +1 -0
- package/dist/__tests__/mx-agent-cli-version-pin.test.d.ts +2 -0
- package/dist/__tests__/mx-agent-cli-version-pin.test.d.ts.map +1 -0
- package/dist/__tests__/mx-agent-cli-version-pin.test.js +290 -0
- package/dist/__tests__/mx-agent-cli-version-pin.test.js.map +1 -0
- package/dist/__tests__/report-format.test.d.ts +2 -0
- package/dist/__tests__/report-format.test.d.ts.map +1 -0
- package/dist/__tests__/report-format.test.js +219 -0
- package/dist/__tests__/report-format.test.js.map +1 -0
- package/dist/__tests__/standards-injection.test.d.ts +2 -0
- package/dist/__tests__/standards-injection.test.d.ts.map +1 -0
- package/dist/__tests__/standards-injection.test.js +157 -0
- package/dist/__tests__/standards-injection.test.js.map +1 -0
- package/dist/agent-config/hooks/auto-capture-precompact.sh +74 -4
- package/dist/agent-config/hooks/auto-capture-session-end.sh +106 -4
- package/dist/agent-config/hooks/auto-checkpoint.sh +75 -5
- package/dist/agent-config/hooks/observability-sync.sh +46 -9
- package/dist/commands/config-sync.d.ts +37 -0
- package/dist/commands/config-sync.d.ts.map +1 -1
- package/dist/commands/config-sync.js +36 -18
- package/dist/commands/config-sync.js.map +1 -1
- package/dist/commands/dashboard.d.ts.map +1 -1
- package/dist/commands/dashboard.js +10 -2
- package/dist/commands/dashboard.js.map +1 -1
- package/dist/commands/decision.d.ts +20 -0
- package/dist/commands/decision.d.ts.map +1 -0
- package/dist/commands/decision.js +54 -0
- package/dist/commands/decision.js.map +1 -0
- package/dist/commands/doctor.d.ts +18 -0
- package/dist/commands/doctor.d.ts.map +1 -1
- package/dist/commands/doctor.js +41 -0
- package/dist/commands/doctor.js.map +1 -1
- package/dist/commands/monitor.d.ts.map +1 -1
- package/dist/commands/monitor.js +8 -2
- package/dist/commands/monitor.js.map +1 -1
- package/dist/commands/new.d.ts.map +1 -1
- package/dist/commands/new.js +15 -1
- package/dist/commands/new.js.map +1 -1
- package/dist/commands/report.d.ts +33 -0
- package/dist/commands/report.d.ts.map +1 -0
- package/dist/commands/report.js +73 -0
- package/dist/commands/report.js.map +1 -0
- package/dist/commands/start.d.ts +4 -0
- package/dist/commands/start.d.ts.map +1 -1
- package/dist/commands/start.js +6 -0
- package/dist/commands/start.js.map +1 -1
- package/dist/commands/update.d.ts +13 -1
- package/dist/commands/update.d.ts.map +1 -1
- package/dist/commands/update.js +50 -1
- package/dist/commands/update.js.map +1 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +67 -3
- package/dist/index.js.map +1 -1
- package/dist/lib/agent-session.d.ts +12 -0
- package/dist/lib/agent-session.d.ts.map +1 -1
- package/dist/lib/agent-session.js +183 -41
- package/dist/lib/agent-session.js.map +1 -1
- package/dist/lib/claude.d.ts +12 -0
- package/dist/lib/claude.d.ts.map +1 -1
- package/dist/lib/claude.js +29 -2
- package/dist/lib/claude.js.map +1 -1
- package/dist/lib/decision-check.d.ts +48 -0
- package/dist/lib/decision-check.d.ts.map +1 -0
- package/dist/lib/decision-check.js +149 -0
- package/dist/lib/decision-check.js.map +1 -0
- package/dist/lib/framework-version.d.ts +144 -0
- package/dist/lib/framework-version.d.ts.map +1 -0
- package/dist/lib/framework-version.js +270 -0
- package/dist/lib/framework-version.js.map +1 -0
- package/dist/lib/harness/console/announce.d.ts +45 -0
- package/dist/lib/harness/console/announce.d.ts.map +1 -0
- package/dist/lib/harness/console/announce.js +48 -0
- package/dist/lib/harness/console/announce.js.map +1 -0
- package/dist/lib/harness/console/announce.test.d.ts +2 -0
- package/dist/lib/harness/console/announce.test.d.ts.map +1 -0
- package/dist/lib/harness/console/announce.test.js +104 -0
- package/dist/lib/harness/console/announce.test.js.map +1 -0
- package/dist/lib/harness/console/by-session.d.ts +50 -0
- package/dist/lib/harness/console/by-session.d.ts.map +1 -0
- package/dist/lib/harness/console/by-session.js +83 -0
- package/dist/lib/harness/console/by-session.js.map +1 -0
- package/dist/lib/harness/console/by-session.test.d.ts +2 -0
- package/dist/lib/harness/console/by-session.test.d.ts.map +1 -0
- package/dist/lib/harness/console/by-session.test.js +86 -0
- package/dist/lib/harness/console/by-session.test.js.map +1 -0
- package/dist/lib/harness/console/console.d.ts +67 -0
- package/dist/lib/harness/console/console.d.ts.map +1 -0
- package/dist/lib/harness/console/console.js +373 -0
- package/dist/lib/harness/console/console.js.map +1 -0
- package/dist/lib/harness/console/console.test.d.ts +2 -0
- package/dist/lib/harness/console/console.test.d.ts.map +1 -0
- package/dist/lib/harness/console/console.test.js +269 -0
- package/dist/lib/harness/console/console.test.js.map +1 -0
- package/dist/lib/harness/console/decision-request.d.ts +32 -0
- package/dist/lib/harness/console/decision-request.d.ts.map +1 -0
- package/dist/lib/harness/console/decision-request.js +112 -0
- package/dist/lib/harness/console/decision-request.js.map +1 -0
- package/dist/lib/harness/console/decision-request.test.d.ts +2 -0
- package/dist/lib/harness/console/decision-request.test.d.ts.map +1 -0
- package/dist/lib/harness/console/decision-request.test.js +75 -0
- package/dist/lib/harness/console/decision-request.test.js.map +1 -0
- package/dist/lib/harness/console/fixtures.d.ts +16 -0
- package/dist/lib/harness/console/fixtures.d.ts.map +1 -0
- package/dist/lib/harness/console/fixtures.js +49 -0
- package/dist/lib/harness/console/fixtures.js.map +1 -0
- package/dist/lib/harness/console/index.d.ts +15 -0
- package/dist/lib/harness/console/index.d.ts.map +1 -0
- package/dist/lib/harness/console/index.js +14 -0
- package/dist/lib/harness/console/index.js.map +1 -0
- package/dist/lib/harness/console/rank.d.ts +73 -0
- package/dist/lib/harness/console/rank.d.ts.map +1 -0
- package/dist/lib/harness/console/rank.js +113 -0
- package/dist/lib/harness/console/rank.js.map +1 -0
- package/dist/lib/harness/console/rank.test.d.ts +2 -0
- package/dist/lib/harness/console/rank.test.d.ts.map +1 -0
- package/dist/lib/harness/console/rank.test.js +124 -0
- package/dist/lib/harness/console/rank.test.js.map +1 -0
- package/dist/lib/harness/console/render.d.ts +22 -0
- package/dist/lib/harness/console/render.d.ts.map +1 -0
- package/dist/lib/harness/console/render.js +65 -0
- package/dist/lib/harness/console/render.js.map +1 -0
- package/dist/lib/harness/console/render.test.d.ts +2 -0
- package/dist/lib/harness/console/render.test.d.ts.map +1 -0
- package/dist/lib/harness/console/render.test.js +96 -0
- package/dist/lib/harness/console/render.test.js.map +1 -0
- package/dist/lib/harness/console/replay-cli.test.d.ts +2 -0
- package/dist/lib/harness/console/replay-cli.test.d.ts.map +1 -0
- package/dist/lib/harness/console/replay-cli.test.js +96 -0
- package/dist/lib/harness/console/replay-cli.test.js.map +1 -0
- package/dist/lib/harness/console/replay.d.ts +36 -0
- package/dist/lib/harness/console/replay.d.ts.map +1 -0
- package/dist/lib/harness/console/replay.js +59 -0
- package/dist/lib/harness/console/replay.js.map +1 -0
- package/dist/lib/harness/console/replay.test.d.ts +2 -0
- package/dist/lib/harness/console/replay.test.d.ts.map +1 -0
- package/dist/lib/harness/console/replay.test.js +81 -0
- package/dist/lib/harness/console/replay.test.js.map +1 -0
- package/dist/lib/harness/console/turn-reader.d.ts +50 -0
- package/dist/lib/harness/console/turn-reader.d.ts.map +1 -0
- package/dist/lib/harness/console/turn-reader.js +184 -0
- package/dist/lib/harness/console/turn-reader.js.map +1 -0
- package/dist/lib/harness/console/turn-reader.test.d.ts +2 -0
- package/dist/lib/harness/console/turn-reader.test.d.ts.map +1 -0
- package/dist/lib/harness/console/turn-reader.test.js +108 -0
- package/dist/lib/harness/console/turn-reader.test.js.map +1 -0
- package/dist/lib/harness/console/types.d.ts +109 -0
- package/dist/lib/harness/console/types.d.ts.map +1 -0
- package/dist/lib/harness/console/types.js +2 -0
- package/dist/lib/harness/console/types.js.map +1 -0
- package/dist/lib/harness/contract.d.ts +229 -0
- package/dist/lib/harness/contract.d.ts.map +1 -0
- package/dist/lib/harness/contract.js +22 -0
- package/dist/lib/harness/contract.js.map +1 -0
- package/dist/lib/harness/credential.d.ts +23 -0
- package/dist/lib/harness/credential.d.ts.map +1 -0
- package/dist/lib/harness/credential.js +81 -0
- package/dist/lib/harness/credential.js.map +1 -0
- package/dist/lib/harness/credential.test.d.ts +2 -0
- package/dist/lib/harness/credential.test.d.ts.map +1 -0
- package/dist/lib/harness/credential.test.js +74 -0
- package/dist/lib/harness/credential.test.js.map +1 -0
- package/dist/lib/harness/evaluate.d.ts +390 -0
- package/dist/lib/harness/evaluate.d.ts.map +1 -0
- package/dist/lib/harness/evaluate.js +988 -0
- package/dist/lib/harness/evaluate.js.map +1 -0
- package/dist/lib/harness/evaluate.test.d.ts +2 -0
- package/dist/lib/harness/evaluate.test.d.ts.map +1 -0
- package/dist/lib/harness/evaluate.test.js +621 -0
- package/dist/lib/harness/evaluate.test.js.map +1 -0
- package/dist/lib/harness/extract.d.ts +152 -0
- package/dist/lib/harness/extract.d.ts.map +1 -0
- package/dist/lib/harness/extract.js +1601 -0
- package/dist/lib/harness/extract.js.map +1 -0
- package/dist/lib/harness/gate.d.ts +48 -0
- package/dist/lib/harness/gate.d.ts.map +1 -0
- package/dist/lib/harness/gate.js +60 -0
- package/dist/lib/harness/gate.js.map +1 -0
- package/dist/lib/harness/gate.test.d.ts +2 -0
- package/dist/lib/harness/gate.test.d.ts.map +1 -0
- package/dist/lib/harness/gate.test.js +111 -0
- package/dist/lib/harness/gate.test.js.map +1 -0
- package/dist/lib/harness/self-check.d.ts +59 -0
- package/dist/lib/harness/self-check.d.ts.map +1 -0
- package/dist/lib/harness/self-check.js +56 -0
- package/dist/lib/harness/self-check.js.map +1 -0
- package/dist/lib/harness/self-check.test.d.ts +2 -0
- package/dist/lib/harness/self-check.test.d.ts.map +1 -0
- package/dist/lib/harness/self-check.test.js +43 -0
- package/dist/lib/harness/self-check.test.js.map +1 -0
- package/dist/lib/harness/transcript.d.ts +65 -0
- package/dist/lib/harness/transcript.d.ts.map +1 -0
- package/dist/lib/harness/transcript.js +144 -0
- package/dist/lib/harness/transcript.js.map +1 -0
- package/dist/lib/harness/transcript.test.d.ts +2 -0
- package/dist/lib/harness/transcript.test.d.ts.map +1 -0
- package/dist/lib/harness/transcript.test.js +118 -0
- package/dist/lib/harness/transcript.test.js.map +1 -0
- package/dist/lib/harness-wiring.d.ts +145 -0
- package/dist/lib/harness-wiring.d.ts.map +1 -0
- package/dist/lib/harness-wiring.js +280 -0
- package/dist/lib/harness-wiring.js.map +1 -0
- package/dist/lib/report-format.d.ts +132 -0
- package/dist/lib/report-format.d.ts.map +1 -0
- package/dist/lib/report-format.js +317 -0
- package/dist/lib/report-format.js.map +1 -0
- package/dist/lib/standards.d.ts +90 -0
- package/dist/lib/standards.d.ts.map +1 -0
- package/dist/lib/standards.js +194 -0
- package/dist/lib/standards.js.map +1 -0
- package/dist/lib/templates.d.ts +26 -0
- package/dist/lib/templates.d.ts.map +1 -1
- package/dist/lib/templates.js +81 -3
- package/dist/lib/templates.js.map +1 -1
- package/dist/lib/terminal-ui.d.ts.map +1 -1
- package/dist/lib/terminal-ui.js +12 -1
- package/dist/lib/terminal-ui.js.map +1 -1
- package/dist/standards/decision-request.md +90 -0
- package/dist/standards/fail-loud-standard.md +495 -0
- package/dist/standards/report-format.md +120 -0
- package/dist/standards/writing-standard.md +240 -0
- package/package.json +3 -2
|
@@ -0,0 +1,621 @@
|
|
|
1
|
+
import { readFileSync } from 'fs';
|
|
2
|
+
import { join } from 'path';
|
|
3
|
+
import { describe, it, expect } from 'vitest';
|
|
4
|
+
// The extraction stream owns this file. The end-to-end block at the bottom runs
|
|
5
|
+
// real tool calls through it, so this test needs that stream's commit as well
|
|
6
|
+
// as this one.
|
|
7
|
+
import { extract } from './extract.js';
|
|
8
|
+
import { attributes, checkRuleset, compileRuleset, NO_DESTRUCTIVE_GIT, RULES_ARE_NOT_SELF_EDITABLE, RulesetRefused, SECTION_5_RULES, STARTER_RULES, SURFACES, TEAMS_STAY_IN_OWN_WORKTREE, TESTER_WORKS_FROM_THE_REQUIREMENT, TESTS_FIRST_UNDER_SCOPED_PATHS, } from './evaluate.js';
|
|
9
|
+
const CTX = {
|
|
10
|
+
worktreeRoot: '/repo/worktrees/product',
|
|
11
|
+
repoRoot: '/repo',
|
|
12
|
+
governingRulePaths: ['/repo/.claude/settings.json', '/repo/.claude/hooks/**', '/repo/.claude/rules/**'],
|
|
13
|
+
testGuardedPaths: ['/repo/worktrees/product/core-api/src'],
|
|
14
|
+
};
|
|
15
|
+
const LEAD = {
|
|
16
|
+
team: 'product',
|
|
17
|
+
role: 'implementation-engineer',
|
|
18
|
+
subagentId: null,
|
|
19
|
+
sessionId: 's1',
|
|
20
|
+
};
|
|
21
|
+
const SUBAGENT = { ...LEAD, subagentId: 'sub-7' };
|
|
22
|
+
/** A rule that passes every structural check, so a test can break one field at a time. */
|
|
23
|
+
function fixture(overrides = {}) {
|
|
24
|
+
return {
|
|
25
|
+
id: 'fixture',
|
|
26
|
+
statement: 'A fixture decides nothing interesting.',
|
|
27
|
+
rationale: 'It exists so a test can remove one field and see the refusal.',
|
|
28
|
+
surfaces: ['permission-callback'],
|
|
29
|
+
mode: 'block',
|
|
30
|
+
effect: 'deny',
|
|
31
|
+
match: { operations: ['git.reset-hard'] },
|
|
32
|
+
records: ['operation', 'decision'],
|
|
33
|
+
changeControl: { owner: 'platform', subjectMayPropose: true, subjectMayApply: false },
|
|
34
|
+
examples: [{ attrs: attributes({ operation: 'git.reset-hard' }), expect: 'deny' }],
|
|
35
|
+
...overrides,
|
|
36
|
+
};
|
|
37
|
+
}
|
|
38
|
+
function codesFrom(sources, ctx = CTX) {
|
|
39
|
+
return checkRuleset(sources, ctx).refusals.map((r) => r.code);
|
|
40
|
+
}
|
|
41
|
+
// ── The three refusals the task names ────────────────────────────────────────
|
|
42
|
+
describe('the compiler refuses a rule bound to a surface that cannot enforce it', () => {
|
|
43
|
+
it('refuses the tester rule on the permission callback, which never receives a read', () => {
|
|
44
|
+
const outcome = checkRuleset([TESTER_WORKS_FROM_THE_REQUIREMENT], CTX);
|
|
45
|
+
const refusal = outcome.refusals.find((r) => r.code === 'unenforceable-surface');
|
|
46
|
+
expect(outcome.evaluator).toBeNull();
|
|
47
|
+
expect(refusal).toBeDefined();
|
|
48
|
+
expect(refusal.ruleId).toBe('tester-works-from-the-requirement');
|
|
49
|
+
expect(refusal.detail).toContain('permission-callback observes');
|
|
50
|
+
expect(refusal.detail).toContain('report healthy and refuse nothing');
|
|
51
|
+
expect(refusal.detail).toContain('POC-1c');
|
|
52
|
+
});
|
|
53
|
+
it('refuses that rule on every surface the contract defines, not only the callback', () => {
|
|
54
|
+
for (const surface of Object.keys(SURFACES)) {
|
|
55
|
+
const codes = codesFrom([{ ...TESTER_WORKS_FROM_THE_REQUIREMENT, surfaces: [surface] }]);
|
|
56
|
+
expect(codes, `surface ${surface}`).toContain('unenforceable-surface');
|
|
57
|
+
}
|
|
58
|
+
});
|
|
59
|
+
it('refuses rather than warns: no evaluator comes back', () => {
|
|
60
|
+
expect(() => compileRuleset([TESTER_WORKS_FROM_THE_REQUIREMENT], CTX)).toThrow(RulesetRefused);
|
|
61
|
+
});
|
|
62
|
+
it('allows a surface that observes some of the rule\'s operations', () => {
|
|
63
|
+
// The pre-tool hook observes a shell write and not a Write tool call, and
|
|
64
|
+
// both fold to `write`. Partial coverage enforces something, so it compiles.
|
|
65
|
+
expect(codesFrom([TEAMS_STAY_IN_OWN_WORKTREE])).not.toContain('unenforceable-surface');
|
|
66
|
+
});
|
|
67
|
+
it('refuses a blocking rule whose only observing surfaces are not built', () => {
|
|
68
|
+
const codes = codesFrom([fixture({ surfaces: ['integration-lane', 'remote'], match: { operations: ['change'] } })]);
|
|
69
|
+
expect(codes).toContain('blocking-on-unavailable-surface');
|
|
70
|
+
});
|
|
71
|
+
it('refuses a rule that names no surface at all', () => {
|
|
72
|
+
expect(codesFrom([fixture({ surfaces: [] })])).toContain('no-surface');
|
|
73
|
+
});
|
|
74
|
+
});
|
|
75
|
+
describe('the compiler refuses two rules on one team claiming the same write scope', () => {
|
|
76
|
+
const claimant = (id, team) => fixture({
|
|
77
|
+
id,
|
|
78
|
+
...(team ? { principals: { team } } : {}),
|
|
79
|
+
match: {
|
|
80
|
+
operations: ['write'],
|
|
81
|
+
writes: { outside: ['${worktree}'] },
|
|
82
|
+
onUncertain: 'escalate',
|
|
83
|
+
},
|
|
84
|
+
claims: { writeScope: ['${worktree}'] },
|
|
85
|
+
examples: [
|
|
86
|
+
{
|
|
87
|
+
attrs: attributes({ operation: 'write', writes: ['${worktree}/../elsewhere/x.ts'] }),
|
|
88
|
+
expect: 'deny',
|
|
89
|
+
},
|
|
90
|
+
],
|
|
91
|
+
});
|
|
92
|
+
it('refuses when both rules bind the same team', () => {
|
|
93
|
+
const refusal = checkRuleset([claimant('first', 'product'), claimant('second', 'product')], CTX).refusals.find((r) => r.code === 'duplicate-write-scope');
|
|
94
|
+
expect(refusal).toBeDefined();
|
|
95
|
+
expect(refusal.detail).toContain('/repo/worktrees/product');
|
|
96
|
+
expect(refusal.detail).toContain('declaration order');
|
|
97
|
+
});
|
|
98
|
+
it('refuses when one rule binds no team, because it applies to every team', () => {
|
|
99
|
+
expect(codesFrom([claimant('first'), claimant('second', 'product')])).toContain('duplicate-write-scope');
|
|
100
|
+
});
|
|
101
|
+
it('allows two rules on different teams to claim the same scope', () => {
|
|
102
|
+
expect(codesFrom([claimant('first', 'product'), claimant('second', 'platform')])).not.toContain('duplicate-write-scope');
|
|
103
|
+
});
|
|
104
|
+
it('allows a narrower scope nested inside a broader one, which is how a rule tightens', () => {
|
|
105
|
+
const broad = claimant('broad', 'product');
|
|
106
|
+
const narrow = {
|
|
107
|
+
...claimant('narrow', 'product'),
|
|
108
|
+
claims: { writeScope: ['${worktree}/src'] },
|
|
109
|
+
};
|
|
110
|
+
expect(codesFrom([broad, narrow])).not.toContain('duplicate-write-scope');
|
|
111
|
+
});
|
|
112
|
+
it('refuses a write rule that claims no scope, so the check cannot be evaded by silence', () => {
|
|
113
|
+
const silent = { ...claimant('silent', 'product') };
|
|
114
|
+
delete silent.claims;
|
|
115
|
+
expect(codesFrom([silent])).toContain('missing-write-scope');
|
|
116
|
+
});
|
|
117
|
+
});
|
|
118
|
+
describe('the compiler refuses a rule with no rationale', () => {
|
|
119
|
+
it('refuses an absent rationale', () => {
|
|
120
|
+
const missing = fixture();
|
|
121
|
+
missing.rationale = undefined;
|
|
122
|
+
const refusal = checkRuleset([missing], CTX).refusals.find((r) => r.code === 'missing-rationale');
|
|
123
|
+
expect(refusal).toBeDefined();
|
|
124
|
+
expect(refusal.detail).toContain('challenge');
|
|
125
|
+
});
|
|
126
|
+
it('refuses whitespace as a rationale', () => {
|
|
127
|
+
expect(codesFrom([fixture({ rationale: ' \n ' })])).toContain('missing-rationale');
|
|
128
|
+
});
|
|
129
|
+
it('refuses rather than warns', () => {
|
|
130
|
+
expect(() => compileRuleset([fixture({ rationale: '' })], CTX)).toThrow(RulesetRefused);
|
|
131
|
+
});
|
|
132
|
+
});
|
|
133
|
+
// ── The rest of the refusals ─────────────────────────────────────────────────
|
|
134
|
+
describe('the other compiler refusals', () => {
|
|
135
|
+
it('reports every refusal at once rather than the first', () => {
|
|
136
|
+
const broken = fixture({ id: 'broken', rationale: '', statement: '', surfaces: [] });
|
|
137
|
+
const codes = codesFrom([broken]);
|
|
138
|
+
expect(codes).toContain('missing-rationale');
|
|
139
|
+
expect(codes).toContain('missing-statement');
|
|
140
|
+
expect(codes).toContain('no-surface');
|
|
141
|
+
});
|
|
142
|
+
it('refuses a rule with a path condition and no uncertainty handling', () => {
|
|
143
|
+
const unsure = fixture({
|
|
144
|
+
match: { operations: ['write'], writes: { outside: ['${worktree}'] } },
|
|
145
|
+
claims: { writeScope: ['${worktree}'] },
|
|
146
|
+
});
|
|
147
|
+
expect(codesFrom([unsure])).toContain('missing-uncertainty-handling');
|
|
148
|
+
});
|
|
149
|
+
it('refuses a path condition that expands to nothing in this context', () => {
|
|
150
|
+
const empty = { ...CTX, testGuardedPaths: [] };
|
|
151
|
+
expect(codesFrom([TESTS_FIRST_UNDER_SCOPED_PATHS], empty)).toContain('empty-path-condition');
|
|
152
|
+
});
|
|
153
|
+
it('refuses an unknown path token', () => {
|
|
154
|
+
const bad = fixture({
|
|
155
|
+
match: { operations: ['write'], writes: { outside: ['${nowhere}'] }, onUncertain: 'match' },
|
|
156
|
+
claims: { writeScope: ['${nowhere}'] },
|
|
157
|
+
});
|
|
158
|
+
expect(codesFrom([bad])).toContain('bad-path-token');
|
|
159
|
+
});
|
|
160
|
+
it('refuses two rules that share an id', () => {
|
|
161
|
+
expect(codesFrom([fixture(), fixture()])).toContain('duplicate-id');
|
|
162
|
+
});
|
|
163
|
+
it('refuses a rule with no operations, because the surface check reads them', () => {
|
|
164
|
+
expect(codesFrom([fixture({ match: { operations: [] } })])).toContain('no-operations');
|
|
165
|
+
});
|
|
166
|
+
it('refuses a rule with no owner', () => {
|
|
167
|
+
const unowned = fixture();
|
|
168
|
+
unowned.changeControl = undefined;
|
|
169
|
+
expect(codesFrom([unowned])).toContain('missing-change-control');
|
|
170
|
+
});
|
|
171
|
+
it('refuses a rule with no examples', () => {
|
|
172
|
+
expect(codesFrom([fixture({ examples: [] })])).toContain('no-examples');
|
|
173
|
+
});
|
|
174
|
+
it('refuses an example that disagrees with the ruleset', () => {
|
|
175
|
+
const wrong = fixture({
|
|
176
|
+
examples: [{ attrs: attributes({ operation: 'git.reset-hard' }), expect: 'allow' }],
|
|
177
|
+
});
|
|
178
|
+
const refusal = checkRuleset([wrong], CTX).refusals.find((r) => r.code === 'example-disagrees');
|
|
179
|
+
expect(refusal).toBeDefined();
|
|
180
|
+
expect(refusal.detail).toContain('returned deny');
|
|
181
|
+
});
|
|
182
|
+
it('refuses when a new rule breaks an existing rule\'s allow example', () => {
|
|
183
|
+
const catchAll = fixture({
|
|
184
|
+
id: 'catch-all',
|
|
185
|
+
match: { operations: ['write'], writes: { inside: ['${repoRoot}'] }, onUncertain: 'match' },
|
|
186
|
+
claims: { writeScope: ['${repoRoot}'] },
|
|
187
|
+
examples: [
|
|
188
|
+
{ attrs: attributes({ operation: 'write', writes: ['${repoRoot}/x.ts'] }), expect: 'deny' },
|
|
189
|
+
],
|
|
190
|
+
});
|
|
191
|
+
const codes = codesFrom([...STARTER_RULES, catchAll]);
|
|
192
|
+
expect(codes).toContain('example-disagrees');
|
|
193
|
+
});
|
|
194
|
+
it('warns rather than refuses on a role-only binding, with the measurement', () => {
|
|
195
|
+
const roleOnly = fixture({ principals: { role: 'qa-tester' } });
|
|
196
|
+
const outcome = checkRuleset([roleOnly], CTX);
|
|
197
|
+
expect(outcome.refusals).toEqual([]);
|
|
198
|
+
expect(outcome.warnings[0].detail).toContain('92%');
|
|
199
|
+
});
|
|
200
|
+
});
|
|
201
|
+
// ── The starter set ──────────────────────────────────────────────────────────
|
|
202
|
+
describe('the starter set', () => {
|
|
203
|
+
it('compiles, which runs every example in every rule', () => {
|
|
204
|
+
const evaluator = compileRuleset(STARTER_RULES, CTX);
|
|
205
|
+
expect(evaluator.ruleIds).toEqual([
|
|
206
|
+
'teams-stay-in-own-worktree',
|
|
207
|
+
'no-destructive-git',
|
|
208
|
+
'tests-first-under-scoped-paths',
|
|
209
|
+
'rules-are-not-self-editable',
|
|
210
|
+
]);
|
|
211
|
+
});
|
|
212
|
+
it('refuses when the fifth rule of section 5 joins it, and refuses only that rule', () => {
|
|
213
|
+
const outcome = checkRuleset(SECTION_5_RULES, CTX);
|
|
214
|
+
expect(outcome.evaluator).toBeNull();
|
|
215
|
+
expect(new Set(outcome.refusals.map((r) => r.ruleId))).toEqual(new Set(['tester-works-from-the-requirement']));
|
|
216
|
+
// Two refusals, both true: the callback observes no read, and blocking on a
|
|
217
|
+
// surface that cannot observe the rule leaves nothing to run it.
|
|
218
|
+
expect(outcome.refusals.map((r) => r.code)).toEqual([
|
|
219
|
+
'blocking-on-unavailable-surface',
|
|
220
|
+
'unenforceable-surface',
|
|
221
|
+
]);
|
|
222
|
+
});
|
|
223
|
+
it('warns that the tests-first rule has no surface that exists', () => {
|
|
224
|
+
const outcome = checkRuleset(STARTER_RULES, CTX);
|
|
225
|
+
const warning = outcome.warnings.find((w) => w.ruleId === 'tests-first-under-scoped-paths');
|
|
226
|
+
expect(warning.detail).toContain('not built');
|
|
227
|
+
});
|
|
228
|
+
});
|
|
229
|
+
describe('(a) a team writes only inside its own worktree', () => {
|
|
230
|
+
const evaluator = compileRuleset(STARTER_RULES, CTX);
|
|
231
|
+
it('refuses a write to another team\'s tree and names the rule', () => {
|
|
232
|
+
const d = evaluator.evaluate(attributes({ operation: 'write', writes: ['/repo/worktrees/other-team/src/index.ts'] }), LEAD, 'permission-callback');
|
|
233
|
+
expect(d.effect).toBe('deny');
|
|
234
|
+
expect(d.matchedRules).toEqual(['teams-stay-in-own-worktree']);
|
|
235
|
+
expect(d.reason).toContain('teams-stay-in-own-worktree');
|
|
236
|
+
expect(d.reason).toContain('/repo/worktrees/other-team/src/index.ts');
|
|
237
|
+
});
|
|
238
|
+
it('allows a write inside the worktree, with no rule matching', () => {
|
|
239
|
+
const d = evaluator.evaluate(attributes({ operation: 'write', writes: ['/repo/worktrees/product/src/index.ts'] }), LEAD, 'permission-callback');
|
|
240
|
+
expect(d.effect).toBe('allow');
|
|
241
|
+
expect(d.matchedRules).toEqual([]);
|
|
242
|
+
});
|
|
243
|
+
it('binds the subagent as well as the lead, which is 92% of the measured writes', () => {
|
|
244
|
+
const d = evaluator.evaluate(attributes({ operation: 'write', writes: ['/repo/worktrees/other-team/x.ts'] }), SUBAGENT, 'permission-callback');
|
|
245
|
+
expect(d.effect).toBe('deny');
|
|
246
|
+
});
|
|
247
|
+
it('does not treat a sibling with a shared prefix as inside', () => {
|
|
248
|
+
const d = evaluator.evaluate(attributes({ operation: 'write', writes: ['/repo/worktrees/product-archive/x.ts'] }), LEAD, 'permission-callback');
|
|
249
|
+
expect(d.effect).toBe('deny');
|
|
250
|
+
});
|
|
251
|
+
it('resolves .. before comparing, so a traversal does not read as inside', () => {
|
|
252
|
+
const d = evaluator.evaluate(attributes({ operation: 'write', writes: ['/repo/worktrees/product/../other-team/x.ts'] }), LEAD, 'permission-callback');
|
|
253
|
+
expect(d.effect).toBe('deny');
|
|
254
|
+
});
|
|
255
|
+
it('covers a git command pointed at another tree, which writes that directory', () => {
|
|
256
|
+
const d = evaluator.evaluate(attributes({ operation: 'git.commit', writes: ['/repo/worktrees/other-team'] }), LEAD, 'pre-tool-hook');
|
|
257
|
+
expect(d.effect).toBe('deny');
|
|
258
|
+
});
|
|
259
|
+
it('escalates a write whose target the extractor could not resolve', () => {
|
|
260
|
+
const d = evaluator.evaluate(attributes({ operation: 'write', uncertain: true, uncertainReason: 'nested substitution' }), LEAD, 'permission-callback');
|
|
261
|
+
expect(d.effect).toBe('ask');
|
|
262
|
+
expect(d.reason).toContain('nested substitution');
|
|
263
|
+
});
|
|
264
|
+
it('does not escalate a command the extractor does not model', () => {
|
|
265
|
+
// `npm test` folds here. Escalating would prompt on every test run, and the
|
|
266
|
+
// rule's rationale states that gap rather than hiding it.
|
|
267
|
+
const d = evaluator.evaluate(attributes({ operation: 'unknown', uncertain: true, uncertainReason: 'npm is not modelled' }), LEAD, 'permission-callback');
|
|
268
|
+
expect(d.effect).toBe('allow');
|
|
269
|
+
expect(d.matchedRules).toEqual([]);
|
|
270
|
+
});
|
|
271
|
+
});
|
|
272
|
+
describe('(b) destructive git operations are refused', () => {
|
|
273
|
+
const evaluator = compileRuleset(STARTER_RULES, CTX);
|
|
274
|
+
const decide = (operation, surface = 'permission-callback') => evaluator.evaluate(attributes({ operation }), LEAD, surface).effect;
|
|
275
|
+
it('refuses the destructive verbs, named as extract.ts emits them', () => {
|
|
276
|
+
expect(decide('git.reset-hard')).toBe('deny');
|
|
277
|
+
expect(decide('git.reset')).toBe('deny');
|
|
278
|
+
expect(decide('git.clean')).toBe('deny');
|
|
279
|
+
expect(decide('git.checkout-path')).toBe('deny');
|
|
280
|
+
expect(decide('git.restore')).toBe('deny');
|
|
281
|
+
expect(decide('git.stash-drop')).toBe('deny');
|
|
282
|
+
expect(decide('git.branch-delete')).toBe('deny');
|
|
283
|
+
expect(decide('git.filter-branch')).toBe('deny');
|
|
284
|
+
expect(decide('git.push-force')).toBe('deny');
|
|
285
|
+
});
|
|
286
|
+
it('refuses a push that names main, which the extractor resolves', () => {
|
|
287
|
+
expect(decide('git.push-main')).toBe('deny');
|
|
288
|
+
expect(decide('git.push-force-main')).toBe('deny');
|
|
289
|
+
expect(decide('git.push-main', 'remote')).toBe('deny');
|
|
290
|
+
});
|
|
291
|
+
it('allows the non-destructive variants of the same verbs', () => {
|
|
292
|
+
expect(decide('git.reset-soft')).toBe('allow');
|
|
293
|
+
expect(decide('git.clean-dry-run')).toBe('allow');
|
|
294
|
+
expect(decide('git.checkout-new-branch')).toBe('allow');
|
|
295
|
+
expect(decide('git.stash-push')).toBe('allow');
|
|
296
|
+
});
|
|
297
|
+
it('refuses a bare merge, which passes both string layers today', () => {
|
|
298
|
+
expect(decide('git.merge')).toBe('deny');
|
|
299
|
+
expect(decide('git.merge', 'pre-tool-hook')).toBe('deny');
|
|
300
|
+
});
|
|
301
|
+
it('allows the git operations that discard nothing', () => {
|
|
302
|
+
expect(decide('git.status')).toBe('allow');
|
|
303
|
+
expect(decide('git.rebase')).toBe('allow');
|
|
304
|
+
expect(decide('git.push')).toBe('allow');
|
|
305
|
+
expect(decide('git.commit')).toBe('allow');
|
|
306
|
+
});
|
|
307
|
+
});
|
|
308
|
+
describe('(d) tests first, in observe mode', () => {
|
|
309
|
+
const evaluator = compileRuleset(STARTER_RULES, CTX);
|
|
310
|
+
it('reports what it would have caught and enforces nothing', () => {
|
|
311
|
+
const d = evaluator.evaluate(attributes({ operation: 'change', writes: ['/repo/worktrees/product/core-api/src/billing.ts'] }), LEAD, 'integration-lane');
|
|
312
|
+
expect(d.effect).toBe('allow');
|
|
313
|
+
expect(d.observedOnly).toBe(true);
|
|
314
|
+
expect(d.matchedRules).toEqual(['tests-first-under-scoped-paths']);
|
|
315
|
+
expect(d.reason).toContain('would have escalated');
|
|
316
|
+
expect(d.reason).toContain('enforced nothing');
|
|
317
|
+
});
|
|
318
|
+
it('does not match a change outside the assigned paths', () => {
|
|
319
|
+
const d = evaluator.evaluate(attributes({ operation: 'change', writes: ['/repo/docs/notes.md'] }), LEAD, 'integration-lane');
|
|
320
|
+
expect(d.effect).toBe('allow');
|
|
321
|
+
expect(d.matchedRules).toEqual([]);
|
|
322
|
+
expect(d.observedOnly).toBeUndefined();
|
|
323
|
+
});
|
|
324
|
+
});
|
|
325
|
+
describe('(e) an agent does not edit the rules that bind it', () => {
|
|
326
|
+
const evaluator = compileRuleset(STARTER_RULES, CTX);
|
|
327
|
+
it('refuses every operation that resolves a write to a governing path', () => {
|
|
328
|
+
// Write, Edit, MultiEdit, NotebookEdit and `sed -i` all fold to `write`,
|
|
329
|
+
// which is how one decision closes the Write hole in today's deny list.
|
|
330
|
+
for (const operation of ['write', 'chmod', 'git.commit']) {
|
|
331
|
+
const d = evaluator.evaluate(attributes({ operation, writes: ['/repo/.claude/settings.json'] }), LEAD, 'permission-callback');
|
|
332
|
+
expect(d.effect, operation).toBe('deny');
|
|
333
|
+
expect(d.matchedRules, operation).toContain('rules-are-not-self-editable');
|
|
334
|
+
}
|
|
335
|
+
});
|
|
336
|
+
it('refuses a shell write to a hook, matched by glob', () => {
|
|
337
|
+
const d = evaluator.evaluate(attributes({ operation: 'write', writes: ['/repo/.claude/hooks/worktree-guard.sh'] }), LEAD, 'pre-tool-hook');
|
|
338
|
+
expect(d.effect).toBe('deny');
|
|
339
|
+
expect(d.matchedRules).toContain('rules-are-not-self-editable');
|
|
340
|
+
});
|
|
341
|
+
it('allows a proposal written inside the team\'s own tree', () => {
|
|
342
|
+
const d = evaluator.evaluate(attributes({ operation: 'write', writes: ['/repo/worktrees/product/docs/proposed-rule-change.md'] }), LEAD, 'permission-callback');
|
|
343
|
+
expect(d.effect).toBe('allow');
|
|
344
|
+
});
|
|
345
|
+
it('records that the subject may propose a change and may not apply one', () => {
|
|
346
|
+
expect(evaluator.changeControlFor('rules-are-not-self-editable')).toEqual({
|
|
347
|
+
owner: 'platform',
|
|
348
|
+
subjectMayPropose: true,
|
|
349
|
+
subjectMayApply: false,
|
|
350
|
+
});
|
|
351
|
+
});
|
|
352
|
+
});
|
|
353
|
+
// ── Observe mode and the meaning of observedOnly ─────────────────────────────
|
|
354
|
+
describe('observedOnly means a rule matched in observe mode and did not enforce', () => {
|
|
355
|
+
const observed = fixture({
|
|
356
|
+
id: 'observed',
|
|
357
|
+
mode: 'observe',
|
|
358
|
+
match: { operations: ['git.reset-hard'] },
|
|
359
|
+
examples: [{ attrs: attributes({ operation: 'git.reset-hard' }), expect: 'allow', expectObservedOnly: true }],
|
|
360
|
+
});
|
|
361
|
+
const blocking = fixture({
|
|
362
|
+
id: 'blocking',
|
|
363
|
+
mode: 'block',
|
|
364
|
+
match: { operations: ['git.reset-hard'] },
|
|
365
|
+
});
|
|
366
|
+
it('is true when every matching rule observes', () => {
|
|
367
|
+
const d = compileRuleset([observed], CTX).evaluate(attributes({ operation: 'git.reset-hard' }), LEAD, 'permission-callback');
|
|
368
|
+
expect(d.effect).toBe('allow');
|
|
369
|
+
expect(d.observedOnly).toBe(true);
|
|
370
|
+
expect(d.matchedRules).toEqual(['observed']);
|
|
371
|
+
});
|
|
372
|
+
it('is unset when a blocking rule enforced, even if an observe rule also matched', () => {
|
|
373
|
+
// The observe rule states deny here because examples test the ruleset the
|
|
374
|
+
// rule ships in, and in this ruleset the blocking rule decides the call.
|
|
375
|
+
const observedAlongside = fixture({
|
|
376
|
+
id: 'observed',
|
|
377
|
+
mode: 'observe',
|
|
378
|
+
match: { operations: ['git.reset-hard'] },
|
|
379
|
+
examples: [{ attrs: attributes({ operation: 'git.reset-hard' }), expect: 'deny' }],
|
|
380
|
+
});
|
|
381
|
+
const d = compileRuleset([observedAlongside, blocking], CTX).evaluate(attributes({ operation: 'git.reset-hard' }), LEAD, 'permission-callback');
|
|
382
|
+
expect(d.effect).toBe('deny');
|
|
383
|
+
expect(d.observedOnly).toBeUndefined();
|
|
384
|
+
expect(d.matchedRules).toEqual(['blocking']);
|
|
385
|
+
expect(d.reason).toContain('observed also matched in observe mode and enforced nothing');
|
|
386
|
+
});
|
|
387
|
+
it('is unset when no rule matched, which is not the same as a rule allowing', () => {
|
|
388
|
+
const d = compileRuleset([observed], CTX).evaluate(attributes({ operation: 'git.status' }), LEAD, 'permission-callback');
|
|
389
|
+
expect(d.observedOnly).toBeUndefined();
|
|
390
|
+
expect(d.matchedRules).toEqual([]);
|
|
391
|
+
});
|
|
392
|
+
it('reports the effect the rule would have produced', () => {
|
|
393
|
+
const wouldDeny = compileRuleset([observed], CTX).evaluate(attributes({ operation: 'git.reset-hard' }), LEAD, 'permission-callback');
|
|
394
|
+
expect(wouldDeny.reason).toContain('would have refused');
|
|
395
|
+
});
|
|
396
|
+
});
|
|
397
|
+
// ── Precedence, scoping and uncertainty ──────────────────────────────────────
|
|
398
|
+
describe('precedence and scoping', () => {
|
|
399
|
+
it('lets the most restrictive blocking effect win, so a narrow allow opens no hole', () => {
|
|
400
|
+
const deny = fixture({ id: 'wide-deny', effect: 'deny' });
|
|
401
|
+
const allow = fixture({
|
|
402
|
+
id: 'narrow-allow',
|
|
403
|
+
effect: 'allow',
|
|
404
|
+
principals: { team: 'product', role: 'implementation-engineer' },
|
|
405
|
+
examples: [{ attrs: attributes({ operation: 'git.reset-hard' }), expect: 'deny' }],
|
|
406
|
+
});
|
|
407
|
+
const d = compileRuleset([deny, allow], CTX).evaluate(attributes({ operation: 'git.reset-hard' }), LEAD, 'permission-callback');
|
|
408
|
+
expect(d.effect).toBe('deny');
|
|
409
|
+
});
|
|
410
|
+
it('reports the most specific matching rule first', () => {
|
|
411
|
+
const wide = fixture({ id: 'wide' });
|
|
412
|
+
const narrow = fixture({ id: 'narrow', principals: { team: 'product', role: 'implementation-engineer' } });
|
|
413
|
+
const d = compileRuleset([wide, narrow], CTX).evaluate(attributes({ operation: 'git.reset-hard' }), LEAD, 'permission-callback');
|
|
414
|
+
expect(d.matchedRules).toEqual(['narrow', 'wide']);
|
|
415
|
+
});
|
|
416
|
+
it('skips a rule the principal does not match', () => {
|
|
417
|
+
const other = fixture({ id: 'platform-only', principals: { team: 'platform' } });
|
|
418
|
+
const d = compileRuleset([other], CTX).evaluate(attributes({ operation: 'git.reset-hard' }), LEAD, 'permission-callback');
|
|
419
|
+
expect(d.effect).toBe('allow');
|
|
420
|
+
expect(d.matchedRules).toEqual([]);
|
|
421
|
+
});
|
|
422
|
+
it('skips a rule not bound to the surface asking', () => {
|
|
423
|
+
const d = compileRuleset(STARTER_RULES, CTX).evaluate(attributes({ operation: 'git.reset-hard' }), LEAD, 'integration-lane');
|
|
424
|
+
expect(d.effect).toBe('allow');
|
|
425
|
+
});
|
|
426
|
+
});
|
|
427
|
+
describe('uncertainty never reads as safety', () => {
|
|
428
|
+
const build = (onUncertain) => compileRuleset([
|
|
429
|
+
fixture({
|
|
430
|
+
id: `on-${onUncertain}`,
|
|
431
|
+
match: { operations: ['unknown'], writes: { outside: ['${worktree}'] }, onUncertain },
|
|
432
|
+
claims: { writeScope: ['${worktree}'] },
|
|
433
|
+
examples: [
|
|
434
|
+
{
|
|
435
|
+
attrs: attributes({ operation: 'unknown', writes: ['${worktree}/../x'] }),
|
|
436
|
+
expect: 'deny',
|
|
437
|
+
},
|
|
438
|
+
],
|
|
439
|
+
}),
|
|
440
|
+
], CTX);
|
|
441
|
+
const unsure = attributes({ operation: 'unknown', uncertain: true, uncertainReason: 'eval in the argument' });
|
|
442
|
+
it('escalate turns an unresolved target into ask', () => {
|
|
443
|
+
expect(build('escalate').evaluate(unsure, LEAD, 'permission-callback').effect).toBe('ask');
|
|
444
|
+
});
|
|
445
|
+
it('match applies the rule\'s own effect to an unresolved target', () => {
|
|
446
|
+
expect(build('match').evaluate(unsure, LEAD, 'permission-callback').effect).toBe('deny');
|
|
447
|
+
});
|
|
448
|
+
it('skip is available and states the choice, rather than leaving it implied', () => {
|
|
449
|
+
const d = build('skip').evaluate(unsure, LEAD, 'permission-callback');
|
|
450
|
+
expect(d.effect).toBe('allow');
|
|
451
|
+
expect(d.matchedRules).toEqual([]);
|
|
452
|
+
});
|
|
453
|
+
it('does not read an empty writes array as touching nothing when uncertain', () => {
|
|
454
|
+
// A Write tool call with no file path. #5434 is what an empty array reading
|
|
455
|
+
// as "touches nothing" costs.
|
|
456
|
+
const d = compileRuleset(STARTER_RULES, CTX).evaluate(attributes({ operation: 'write', writes: [], uncertain: true }), LEAD, 'permission-callback');
|
|
457
|
+
expect(d.effect).toBe('ask');
|
|
458
|
+
});
|
|
459
|
+
});
|
|
460
|
+
// ── Coverage reporting ───────────────────────────────────────────────────────
|
|
461
|
+
describe('unenforceableOn reports what a surface is not enforcing', () => {
|
|
462
|
+
const evaluator = compileRuleset(STARTER_RULES, CTX);
|
|
463
|
+
it('names the rules the permission callback does not run', () => {
|
|
464
|
+
const ids = evaluator.unenforceableOn('permission-callback').map((r) => r.id);
|
|
465
|
+
expect(ids).toEqual(['tests-first-under-scoped-paths']);
|
|
466
|
+
});
|
|
467
|
+
it('names the rules the integration lane does not run', () => {
|
|
468
|
+
const ids = evaluator.unenforceableOn('integration-lane').map((r) => r.id);
|
|
469
|
+
expect(ids).toEqual([
|
|
470
|
+
'teams-stay-in-own-worktree',
|
|
471
|
+
'no-destructive-git',
|
|
472
|
+
'rules-are-not-self-editable',
|
|
473
|
+
]);
|
|
474
|
+
});
|
|
475
|
+
it('is empty of nothing: every compiled rule has at least one surface that runs it', () => {
|
|
476
|
+
const everywhere = Object.keys(SURFACES).flatMap((s) => evaluator.ruleIds.filter((id) => !evaluator.unenforceableOn(s).some((r) => r.id === id)));
|
|
477
|
+
expect(new Set(everywhere).size).toBe(evaluator.ruleIds.length);
|
|
478
|
+
});
|
|
479
|
+
it('reports the write scopes each rule claims', () => {
|
|
480
|
+
expect(evaluator.claimedWriteScopes()).toEqual([
|
|
481
|
+
// The scratch roots are part of the scope this rule claims: an agent
|
|
482
|
+
// writing to its own scratchpad has not left its scope, and refusing that
|
|
483
|
+
// was 10 of the 600 ordinary commands the first version of the rule
|
|
484
|
+
// refused (docs/gate-coverage.md section 4).
|
|
485
|
+
{ ruleId: 'teams-stay-in-own-worktree', scope: ['/repo/worktrees/product', '/tmp', '/var/tmp'] },
|
|
486
|
+
{ ruleId: 'tests-first-under-scoped-paths', scope: ['/repo/worktrees/product/core-api/src'] },
|
|
487
|
+
{
|
|
488
|
+
ruleId: 'rules-are-not-self-editable',
|
|
489
|
+
scope: ['/repo/.claude/settings.json', '/repo/.claude/hooks/**', '/repo/.claude/rules/**'],
|
|
490
|
+
},
|
|
491
|
+
]);
|
|
492
|
+
});
|
|
493
|
+
});
|
|
494
|
+
// ── The evaluator owns policy and nothing else ───────────────────────────────
|
|
495
|
+
describe('the evaluator never parses a command string', () => {
|
|
496
|
+
const SOURCE = readFileSync(join(__dirname, 'evaluate.ts'), 'utf-8');
|
|
497
|
+
const CODE = SOURCE.split('\n')
|
|
498
|
+
.filter((line) => !/^\s*(\/\/|\/\*|\*)/.test(line))
|
|
499
|
+
.join('\n');
|
|
500
|
+
it('reads no command field anywhere in its executable lines', () => {
|
|
501
|
+
expect(CODE).not.toMatch(/\.command\b/);
|
|
502
|
+
});
|
|
503
|
+
it('decides on the operation even when the command text looks destructive', () => {
|
|
504
|
+
const evaluator = compileRuleset(STARTER_RULES, CTX);
|
|
505
|
+
const d = evaluator.evaluate({ operation: 'read', writes: [], reads: ['/repo/README.md'], uncertain: false, command: 'git reset --hard' }, LEAD, 'permission-callback');
|
|
506
|
+
expect(d.effect).toBe('allow');
|
|
507
|
+
expect(d.matchedRules).toEqual([]);
|
|
508
|
+
});
|
|
509
|
+
it('refuses on the operation even when the command text looks harmless', () => {
|
|
510
|
+
const evaluator = compileRuleset(STARTER_RULES, CTX);
|
|
511
|
+
const d = evaluator.evaluate({ operation: 'git.reset-hard', writes: [], reads: [], uncertain: false, command: 'git status' }, LEAD, 'permission-callback');
|
|
512
|
+
expect(d.effect).toBe('deny');
|
|
513
|
+
});
|
|
514
|
+
});
|
|
515
|
+
describe('the surface table states its basis', () => {
|
|
516
|
+
it('gives every surface a measured or configured basis', () => {
|
|
517
|
+
for (const [name, capability] of Object.entries(SURFACES)) {
|
|
518
|
+
expect(capability.basis.length, name).toBeGreaterThan(20);
|
|
519
|
+
expect(capability.observes.length, name).toBeGreaterThan(0);
|
|
520
|
+
}
|
|
521
|
+
});
|
|
522
|
+
it('records that the permission callback does not observe a read', () => {
|
|
523
|
+
expect(SURFACES['permission-callback'].observes).not.toContain('read');
|
|
524
|
+
expect(SURFACES['permission-callback'].basis).toContain('Grep was not measured');
|
|
525
|
+
expect(SURFACES['permission-callback'].basis).toContain('partial observation');
|
|
526
|
+
});
|
|
527
|
+
it('records that a hook observes the shell half of a write and not the tool half', () => {
|
|
528
|
+
expect(SURFACES['pre-tool-hook'].observes).toContain('write');
|
|
529
|
+
expect(SURFACES['pre-tool-hook'].basis).toContain('reaches no hook');
|
|
530
|
+
expect(SURFACES['pre-tool-hook'].observes).not.toContain('read');
|
|
531
|
+
});
|
|
532
|
+
});
|
|
533
|
+
// ── The two halves together ──────────────────────────────────────────────────
|
|
534
|
+
/**
|
|
535
|
+
* The starter rules name operations `extract.ts` produces. Asserting that by
|
|
536
|
+
* reading the extractor's source would prove nothing, so these run real tool
|
|
537
|
+
* calls through the real extractor and decide the attributes it returns. A rule
|
|
538
|
+
* naming an operation nothing emits fails here.
|
|
539
|
+
*/
|
|
540
|
+
describe('the starter rules decide real tool calls, through the real extractor', () => {
|
|
541
|
+
const evaluator = compileRuleset(STARTER_RULES, CTX);
|
|
542
|
+
const world = { cwd: '/repo/worktrees/product', worktreeRoot: '/repo/worktrees/product' };
|
|
543
|
+
const decide = (call, surface = 'permission-callback') => evaluator.evaluate(extract(call, world), LEAD, surface);
|
|
544
|
+
const bash = (command) => ({ name: 'Bash', input: { command } });
|
|
545
|
+
it('refuses the destructive git commands', () => {
|
|
546
|
+
expect(decide(bash('git reset --hard HEAD~1')).effect).toBe('deny');
|
|
547
|
+
expect(decide(bash('git clean -fdx .')).effect).toBe('deny');
|
|
548
|
+
expect(decide(bash('git push --force origin main')).effect).toBe('deny');
|
|
549
|
+
expect(decide(bash('git checkout -- src/index.ts')).effect).toBe('deny');
|
|
550
|
+
});
|
|
551
|
+
it('refuses a bare git merge, which passes both string layers today', () => {
|
|
552
|
+
const d = decide(bash('git merge'));
|
|
553
|
+
expect(d.effect).toBe('deny');
|
|
554
|
+
expect(d.matchedRules).toContain('no-destructive-git');
|
|
555
|
+
});
|
|
556
|
+
it('allows the git commands that discard nothing', () => {
|
|
557
|
+
expect(decide(bash('git status')).effect).toBe('allow');
|
|
558
|
+
expect(decide(bash('git rebase origin/main')).effect).toBe('allow');
|
|
559
|
+
expect(decide(bash('git push origin product/feature')).effect).toBe('allow');
|
|
560
|
+
expect(decide(bash('git commit -m "work"')).effect).toBe('allow');
|
|
561
|
+
});
|
|
562
|
+
it('refuses a write outside the worktree, whichever tool performs it', () => {
|
|
563
|
+
expect(decide(bash('echo x > ../other-team/notes.md')).effect).toBe('deny');
|
|
564
|
+
expect(decide(bash('git -C ../other-team commit -m x')).effect).toBe('deny');
|
|
565
|
+
expect(decide({ name: 'Write', input: { file_path: '/repo/worktrees/other-team/src/index.ts' } }).effect).toBe('deny');
|
|
566
|
+
});
|
|
567
|
+
it('refuses a write to the rules, through Write and through sed', () => {
|
|
568
|
+
const viaTool = decide({ name: 'Write', input: { file_path: '/repo/.claude/settings.json' } });
|
|
569
|
+
expect(viaTool.effect).toBe('deny');
|
|
570
|
+
expect(viaTool.matchedRules).toContain('rules-are-not-self-editable');
|
|
571
|
+
const viaSed = decide(bash("sed -i 's/deny/allow/' /repo/.claude/settings.json"));
|
|
572
|
+
expect(viaSed.effect).toBe('deny');
|
|
573
|
+
expect(viaSed.matchedRules).toContain('rules-are-not-self-editable');
|
|
574
|
+
});
|
|
575
|
+
it('allows ordinary work inside the worktree', () => {
|
|
576
|
+
expect(decide({ name: 'Write', input: { file_path: 'src/index.ts' } }).effect).toBe('allow');
|
|
577
|
+
expect(decide(bash('cd ./src && npm test')).effect).toBe('allow');
|
|
578
|
+
expect(decide(bash('git status && echo done > notes.txt')).effect).toBe('allow');
|
|
579
|
+
});
|
|
580
|
+
it('escalates a write whose target the extractor could not resolve', () => {
|
|
581
|
+
expect(decide(bash('echo hi > $(pick)')).effect).toBe('ask');
|
|
582
|
+
expect(decide(bash('sed -i s/a/b/ $TARGET')).effect).toBe('ask');
|
|
583
|
+
// The dangerous form of the unmodelled case: the working directory is lost
|
|
584
|
+
// and the command writes, so it folds to write and escalates.
|
|
585
|
+
expect(decide(bash('cd $X && rm -rf build')).effect).toBe('ask');
|
|
586
|
+
expect(decide({ name: 'Write', input: {} }).effect).toBe('ask');
|
|
587
|
+
});
|
|
588
|
+
it('leaves an unmodelled command alone, which is the stated gap', () => {
|
|
589
|
+
// `npm test` and `eval` fold to unknown, and no starter rule lists it. The
|
|
590
|
+
// alternative is a prompt on every test run.
|
|
591
|
+
expect(decide(bash('npm test')).effect).toBe('allow');
|
|
592
|
+
expect(decide(bash('pytest -q')).effect).toBe('allow');
|
|
593
|
+
expect(decide(bash('eval "$CMD"')).effect).toBe('allow');
|
|
594
|
+
});
|
|
595
|
+
it('allows a read, because no read rule ships and no surface could run one', () => {
|
|
596
|
+
expect(decide({ name: 'Read', input: { file_path: 'src/billing.ts' } }).effect).toBe('allow');
|
|
597
|
+
});
|
|
598
|
+
it('does not escalate a delegation, which the extractor marks uncertain', () => {
|
|
599
|
+
// `tool.Task` is uncertain and has no write target. Listing it in a
|
|
600
|
+
// write rule would escalate every delegation the fleet makes.
|
|
601
|
+
const d = decide({ name: 'Task', input: { prompt: 'go' } });
|
|
602
|
+
expect(d.effect).toBe('allow');
|
|
603
|
+
expect(d.matchedRules).toEqual([]);
|
|
604
|
+
});
|
|
605
|
+
});
|
|
606
|
+
describe('the rules state the fields the format argued for', () => {
|
|
607
|
+
it('every starter rule names its surfaces, its owner and its rationale', () => {
|
|
608
|
+
for (const rule of [...STARTER_RULES, ...SECTION_5_RULES]) {
|
|
609
|
+
expect(rule.surfaces.length, rule.id).toBeGreaterThan(0);
|
|
610
|
+
expect(rule.changeControl.owner, rule.id).not.toBe('');
|
|
611
|
+
expect(rule.rationale.length, rule.id).toBeGreaterThan(40);
|
|
612
|
+
expect(rule.records.length, rule.id).toBeGreaterThan(0);
|
|
613
|
+
}
|
|
614
|
+
});
|
|
615
|
+
it('the mutating rules bind every principal rather than one role', () => {
|
|
616
|
+
for (const rule of [TEAMS_STAY_IN_OWN_WORKTREE, NO_DESTRUCTIVE_GIT, RULES_ARE_NOT_SELF_EDITABLE]) {
|
|
617
|
+
expect(rule.principals, rule.id).toBeUndefined();
|
|
618
|
+
}
|
|
619
|
+
});
|
|
620
|
+
});
|
|
621
|
+
//# sourceMappingURL=evaluate.test.js.map
|