@memnexus-ai/mx-agent-cli 0.1.242 → 0.1.244
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__/agents-md-canonicalization.test.js +17 -5
- package/dist/__tests__/agents-md-canonicalization.test.js.map +1 -1
- package/dist/__tests__/behavioral-git-read.test.d.ts +21 -0
- package/dist/__tests__/behavioral-git-read.test.d.ts.map +1 -0
- package/dist/__tests__/behavioral-git-read.test.js +955 -0
- package/dist/__tests__/behavioral-git-read.test.js.map +1 -0
- package/dist/__tests__/event-shipper.test.d.ts +16 -0
- package/dist/__tests__/event-shipper.test.d.ts.map +1 -0
- package/dist/__tests__/event-shipper.test.js +287 -0
- package/dist/__tests__/event-shipper.test.js.map +1 -0
- package/dist/__tests__/start-report-integration.test.js +4 -0
- package/dist/__tests__/start-report-integration.test.js.map +1 -1
- package/dist/agent-config/AGENTS.md.template +29 -0
- package/dist/agent-config/claude-md/CLAUDE.overlay.memnexus.md +29 -0
- package/dist/commands/start.d.ts +22 -0
- package/dist/commands/start.d.ts.map +1 -1
- package/dist/commands/start.js +44 -21
- package/dist/commands/start.js.map +1 -1
- package/dist/commands/sync.d.ts.map +1 -1
- package/dist/commands/sync.js +9 -5
- package/dist/commands/sync.js.map +1 -1
- package/dist/lib/behavioral-injection.d.ts +190 -13
- package/dist/lib/behavioral-injection.d.ts.map +1 -1
- package/dist/lib/behavioral-injection.js +653 -37
- package/dist/lib/behavioral-injection.js.map +1 -1
- package/dist/lib/claude.d.ts +77 -1
- package/dist/lib/claude.d.ts.map +1 -1
- package/dist/lib/claude.js +223 -4
- package/dist/lib/claude.js.map +1 -1
- package/dist/lib/startup-shipper.d.ts +108 -20
- package/dist/lib/startup-shipper.d.ts.map +1 -1
- package/dist/lib/startup-shipper.js +133 -29
- package/dist/lib/startup-shipper.js.map +1 -1
- package/package.json +1 -1
|
@@ -0,0 +1,955 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Tests for #4707 — the behavioral block is read from origin/<baseBranch>, the
|
|
3
|
+
* delivered CLAUDE.local.md is verified for CONTENT, and repeated markers fail
|
|
4
|
+
* loudly.
|
|
5
|
+
*
|
|
6
|
+
* WHAT WENT WRONG. `mx-agent start` resolved the team catalog and the roleguide
|
|
7
|
+
* from `projectRoot` — `/workspace`, the main repo working tree — which was 82
|
|
8
|
+
* commits behind origin/main. The markers eval's roleguide gained in #4521 were
|
|
9
|
+
* in git and had never been read, so eval shipped an isolation-only
|
|
10
|
+
* CLAUDE.local.md for four weeks while the same startup sequence printed
|
|
11
|
+
* "Agent config generated from … (origin/main)".
|
|
12
|
+
*
|
|
13
|
+
* HOW THESE TESTS ARE PINNED. Every git-vs-local test builds a repository where
|
|
14
|
+
* the two sources DISAGREE: origin/<branch> carries one roleguide, the
|
|
15
|
+
* projectRoot working tree carries a different one. Asserting on the CONTENT
|
|
16
|
+
* (not merely on "a block exists") means reverting the read to projectRoot fails
|
|
17
|
+
* the test rather than passing it by luck — which is exactly how the previous
|
|
18
|
+
* iteration's verification passed while the outcome it claimed was false.
|
|
19
|
+
*/
|
|
20
|
+
import { describe, it, expect, beforeEach, afterEach, vi } from 'vitest';
|
|
21
|
+
import { mkdirSync, rmSync, readFileSync, writeFileSync, symlinkSync } from 'fs';
|
|
22
|
+
import { spawnSync } from 'child_process';
|
|
23
|
+
import { join, dirname } from 'path';
|
|
24
|
+
import { tmpdir } from 'os';
|
|
25
|
+
import { checkRoleguideMarkers, extractMarkerBlock, checkBehavioralCompleteness, findMissingBehavioralElements, describeBehavioralProblem, resolveBehavioralInjection, REQUIRED_BEHAVIORAL_ELEMENTS, BEHAVIORAL_START_MARKER, BEHAVIORAL_END_MARKER, MAX_ROLEGUIDE_BYTES, MAX_BEHAVIORAL_BLOCK_BYTES, sanitizeForMessage, } from '../lib/behavioral-injection.js';
|
|
26
|
+
import { writeClaudeLocal, writeClaudeLocalWithBehavioralSelfHeal, extractInjectedBehavioralBlock, countBehavioralHeaders, } from '../lib/claude.js';
|
|
27
|
+
import { startupReport } from '../lib/startup-report.js';
|
|
28
|
+
/** A behavioral block carrying all four required elements, minus `omit`. */
|
|
29
|
+
function block(opts = {}) {
|
|
30
|
+
const omit = opts.omit ?? [];
|
|
31
|
+
const marker = opts.marker ?? '';
|
|
32
|
+
const sections = [
|
|
33
|
+
['prior-art', [
|
|
34
|
+
'## Prior Art Search — Mandatory Before Every Decision',
|
|
35
|
+
'',
|
|
36
|
+
'Run a search before proposing any approach.',
|
|
37
|
+
].join('\n')],
|
|
38
|
+
['decision-authority', [
|
|
39
|
+
'## Decision Authority',
|
|
40
|
+
'',
|
|
41
|
+
'- Full authority: iteration selection, tooling, merge timing.',
|
|
42
|
+
].join('\n')],
|
|
43
|
+
['anti-approval', [
|
|
44
|
+
'### Anti-pattern: approval-seeking',
|
|
45
|
+
'',
|
|
46
|
+
'When the data is clear, decide and execute.',
|
|
47
|
+
].join('\n')],
|
|
48
|
+
['delegation', [
|
|
49
|
+
'## Delegation and Stop Conditions',
|
|
50
|
+
'',
|
|
51
|
+
'- Delegate implementation to teammates.',
|
|
52
|
+
'- Escalate to the product owner on cost-impacting changes.',
|
|
53
|
+
].join('\n')],
|
|
54
|
+
];
|
|
55
|
+
const body = sections
|
|
56
|
+
.filter(([id]) => !omit.includes(id))
|
|
57
|
+
.map(([, text]) => text)
|
|
58
|
+
.join('\n\n');
|
|
59
|
+
return marker ? `${marker}\n\n${body}` : body;
|
|
60
|
+
}
|
|
61
|
+
/** A full roleguide file wrapping `inner` in one line-anchored marker pair. */
|
|
62
|
+
function roleguide(inner) {
|
|
63
|
+
return [
|
|
64
|
+
'# Roleguide',
|
|
65
|
+
'',
|
|
66
|
+
'Prose above the block is never injected.',
|
|
67
|
+
'',
|
|
68
|
+
BEHAVIORAL_START_MARKER,
|
|
69
|
+
inner,
|
|
70
|
+
BEHAVIORAL_END_MARKER,
|
|
71
|
+
'',
|
|
72
|
+
'## Reference (not injected)',
|
|
73
|
+
'',
|
|
74
|
+
].join('\n');
|
|
75
|
+
}
|
|
76
|
+
const RG_REL = 'mx-agent-system/roleguides/eval-leader-v3.md';
|
|
77
|
+
function teamFile(slug, displayName, rgRel = RG_REL) {
|
|
78
|
+
return `# Team: ${slug}\n\nDisplay name: ${displayName}\nRoleguide: \`${rgRel}\`\n`;
|
|
79
|
+
}
|
|
80
|
+
// ── Git helpers ───────────────────────────────────────────────────────────
|
|
81
|
+
let tmpRoot;
|
|
82
|
+
function git(cwd, ...args) {
|
|
83
|
+
const r = spawnSync('git', args, { cwd, encoding: 'utf-8', stdio: 'pipe' });
|
|
84
|
+
if (r.status !== 0) {
|
|
85
|
+
throw new Error(`git ${args.join(' ')} failed in ${cwd}: ${r.stderr}`);
|
|
86
|
+
}
|
|
87
|
+
return r.stdout ?? '';
|
|
88
|
+
}
|
|
89
|
+
function write(root, rel, content) {
|
|
90
|
+
const abs = join(root, rel);
|
|
91
|
+
mkdirSync(dirname(abs), { recursive: true });
|
|
92
|
+
writeFileSync(abs, content);
|
|
93
|
+
}
|
|
94
|
+
function commitAll(repo, message) {
|
|
95
|
+
git(repo, 'add', '-A');
|
|
96
|
+
// --allow-empty: some fixtures deliberately commit identical bytes on both
|
|
97
|
+
// sides, and a "nothing to commit" exit would fail the setup, not the code.
|
|
98
|
+
git(repo, '-c', 'user.email=t@t', '-c', 'user.name=T', 'commit', '-q', '--allow-empty', '-m', message);
|
|
99
|
+
}
|
|
100
|
+
/**
|
|
101
|
+
* Build the #4707 topology: a bare origin, a STALE projectRoot checkout, and a
|
|
102
|
+
* worktree whose origin/<branch> is current.
|
|
103
|
+
*
|
|
104
|
+
* The two checkouts deliberately hold DIFFERENT bytes for the same roleguide —
|
|
105
|
+
* that disagreement is what makes "which source did we read?" a testable
|
|
106
|
+
* question rather than an assumption.
|
|
107
|
+
*/
|
|
108
|
+
function makeRepos(opts) {
|
|
109
|
+
const branch = opts.branch ?? 'main';
|
|
110
|
+
const slug = opts.teamSlug ?? 'eval';
|
|
111
|
+
const origin = join(tmpRoot, 'origin.git');
|
|
112
|
+
const projectRoot = join(tmpRoot, 'stale-checkout');
|
|
113
|
+
const worktreePath = join(tmpRoot, 'team-worktree');
|
|
114
|
+
git(tmpRoot, 'init', '--bare', '-q', '-b', branch, origin);
|
|
115
|
+
// 1. projectRoot: the stale main-repo working tree.
|
|
116
|
+
mkdirSync(projectRoot, { recursive: true });
|
|
117
|
+
git(projectRoot, 'init', '-q', '-b', branch);
|
|
118
|
+
write(projectRoot, `mx-agent-system/teams/${slug}.md`, opts.teamFileContent ?? teamFile(slug, opts.displayName ?? 'Eval Integrity'));
|
|
119
|
+
write(projectRoot, RG_REL, opts.staleRoleguide);
|
|
120
|
+
commitAll(projectRoot, 'stale state');
|
|
121
|
+
git(projectRoot, 'remote', 'add', 'origin', origin);
|
|
122
|
+
git(projectRoot, 'push', '-q', 'origin', branch);
|
|
123
|
+
// 2. worktree: clone, advance the roleguide, push. projectRoot is NOT updated,
|
|
124
|
+
// so its working tree stays behind exactly as /workspace did.
|
|
125
|
+
git(tmpRoot, 'clone', '-q', origin, worktreePath);
|
|
126
|
+
write(worktreePath, RG_REL, opts.currentRoleguide);
|
|
127
|
+
commitAll(worktreePath, 'add behavioral markers');
|
|
128
|
+
git(worktreePath, 'push', '-q', 'origin', branch);
|
|
129
|
+
if (opts.otherBranch) {
|
|
130
|
+
git(worktreePath, 'checkout', '-q', '-b', opts.otherBranch.name);
|
|
131
|
+
write(worktreePath, RG_REL, opts.otherBranch.roleguide);
|
|
132
|
+
commitAll(worktreePath, 'other branch roleguide');
|
|
133
|
+
git(worktreePath, 'push', '-q', 'origin', opts.otherBranch.name);
|
|
134
|
+
git(worktreePath, 'checkout', '-q', branch);
|
|
135
|
+
}
|
|
136
|
+
git(worktreePath, 'fetch', '-q', 'origin');
|
|
137
|
+
return { projectRoot, worktreePath, branch };
|
|
138
|
+
}
|
|
139
|
+
beforeEach(() => {
|
|
140
|
+
tmpRoot = join(tmpdir(), `mx-4707-${Date.now()}-${Math.random().toString(36).slice(2)}`);
|
|
141
|
+
mkdirSync(tmpRoot, { recursive: true });
|
|
142
|
+
startupReport._reset();
|
|
143
|
+
});
|
|
144
|
+
afterEach(() => {
|
|
145
|
+
rmSync(tmpRoot, { recursive: true, force: true });
|
|
146
|
+
});
|
|
147
|
+
function warnings() {
|
|
148
|
+
return startupReport._entries().filter((e) => e.level === 'warning');
|
|
149
|
+
}
|
|
150
|
+
/**
|
|
151
|
+
* Run `fn` while capturing what actually reaches the terminal.
|
|
152
|
+
*
|
|
153
|
+
* `reportWarning` does two things with one string: `record()` it into the
|
|
154
|
+
* startup report AFTER stripping control sequences, and `printOut()` it to the
|
|
155
|
+
* console RAW. So the report is clean by construction and proves nothing about
|
|
156
|
+
* escape injection; only the printed bytes do.
|
|
157
|
+
*/
|
|
158
|
+
function capturePrinted(fn) {
|
|
159
|
+
const printed = [];
|
|
160
|
+
const log = vi.spyOn(console, 'log').mockImplementation((...args) => {
|
|
161
|
+
printed.push(args.map(String).join(' '));
|
|
162
|
+
});
|
|
163
|
+
try {
|
|
164
|
+
fn();
|
|
165
|
+
}
|
|
166
|
+
finally {
|
|
167
|
+
log.mockRestore();
|
|
168
|
+
}
|
|
169
|
+
return printed.join('\n');
|
|
170
|
+
}
|
|
171
|
+
// ── 1. The read resolves from origin/<baseBranch>, not projectRoot ────────
|
|
172
|
+
describe('#4707 — roleguide bytes come from origin/<baseBranch>', () => {
|
|
173
|
+
it('reads the ref, NOT the stale projectRoot working tree', () => {
|
|
174
|
+
const { projectRoot, worktreePath } = makeRepos({
|
|
175
|
+
staleRoleguide: roleguide(block({ marker: 'STALE-WORKING-TREE-CONTENT' })),
|
|
176
|
+
currentRoleguide: roleguide(block({ marker: 'CURRENT-REF-CONTENT' })),
|
|
177
|
+
});
|
|
178
|
+
const res = resolveBehavioralInjection(projectRoot, 'eval', {
|
|
179
|
+
repoPath: worktreePath, baseBranch: 'main',
|
|
180
|
+
});
|
|
181
|
+
expect(res.source).toBe('git');
|
|
182
|
+
expect(res.ref).toBe('origin/main');
|
|
183
|
+
expect(res.fallbackReason).toBeNull();
|
|
184
|
+
expect(res.result.ok).toBe(true);
|
|
185
|
+
// MUTATION PIN: a read from projectRoot returns a block that is present and
|
|
186
|
+
// non-empty too, so only the CONTENT distinguishes the two sources.
|
|
187
|
+
expect(res.result.content).toContain('CURRENT-REF-CONTENT');
|
|
188
|
+
expect(res.result.content).not.toContain('STALE-WORKING-TREE-CONTENT');
|
|
189
|
+
});
|
|
190
|
+
it('uses the CONFIGURED base branch, not a hardcoded "main"', () => {
|
|
191
|
+
const { projectRoot, worktreePath } = makeRepos({
|
|
192
|
+
staleRoleguide: roleguide(block({ marker: 'STALE-WORKING-TREE-CONTENT' })),
|
|
193
|
+
currentRoleguide: roleguide(block({ marker: 'MAIN-BRANCH-CONTENT' })),
|
|
194
|
+
otherBranch: { name: 'develop', roleguide: roleguide(block({ marker: 'DEVELOP-BRANCH-CONTENT' })) },
|
|
195
|
+
});
|
|
196
|
+
const res = resolveBehavioralInjection(projectRoot, 'eval', {
|
|
197
|
+
repoPath: worktreePath, baseBranch: 'develop',
|
|
198
|
+
});
|
|
199
|
+
expect(res.ref).toBe('origin/develop');
|
|
200
|
+
expect(res.result.content).toContain('DEVELOP-BRANCH-CONTENT');
|
|
201
|
+
expect(res.result.content).not.toContain('MAIN-BRANCH-CONTENT');
|
|
202
|
+
});
|
|
203
|
+
it('the ref read is not fooled by a symlinked roleguide committed into git', () => {
|
|
204
|
+
// A tree entry records the mode: a symlink is 120000 with the target as its
|
|
205
|
+
// blob content. Reading it would put out-of-repo bytes into every turn of
|
|
206
|
+
// this team's context while showing nothing in a diff.
|
|
207
|
+
const { projectRoot, worktreePath } = makeRepos({
|
|
208
|
+
staleRoleguide: roleguide(block()),
|
|
209
|
+
currentRoleguide: roleguide(block()),
|
|
210
|
+
});
|
|
211
|
+
const secret = join(tmpRoot, 'outside-secret.md');
|
|
212
|
+
writeFileSync(secret, roleguide(block({ marker: 'OUT-OF-REPO-CONTENT' })));
|
|
213
|
+
const rgAbs = join(worktreePath, RG_REL);
|
|
214
|
+
rmSync(rgAbs);
|
|
215
|
+
symlinkSync(secret, rgAbs);
|
|
216
|
+
commitAll(worktreePath, 'replace roleguide with a symlink');
|
|
217
|
+
git(worktreePath, 'push', '-q', 'origin', 'main');
|
|
218
|
+
git(worktreePath, 'fetch', '-q', 'origin');
|
|
219
|
+
const res = resolveBehavioralInjection(projectRoot, 'eval', {
|
|
220
|
+
repoPath: worktreePath, baseBranch: 'main',
|
|
221
|
+
});
|
|
222
|
+
expect(res.source).toBe('git');
|
|
223
|
+
expect(res.result.ok).toBe(false);
|
|
224
|
+
expect(res.result.problem).toBe('roleguide-symlink');
|
|
225
|
+
expect(res.result.content).toBeNull();
|
|
226
|
+
});
|
|
227
|
+
it('rejects a team slug that could escape the pathspec, without touching disk', () => {
|
|
228
|
+
const { projectRoot, worktreePath } = makeRepos({
|
|
229
|
+
staleRoleguide: roleguide(block()),
|
|
230
|
+
currentRoleguide: roleguide(block()),
|
|
231
|
+
});
|
|
232
|
+
for (const slug of ['../../etc/passwd', '/etc/passwd', 'a/b', '-x', '..']) {
|
|
233
|
+
const res = resolveBehavioralInjection(projectRoot, slug, {
|
|
234
|
+
repoPath: worktreePath, baseBranch: 'main',
|
|
235
|
+
});
|
|
236
|
+
expect(res.result.ok).toBe(false);
|
|
237
|
+
expect(res.result.problem).toBe('invalid-team-slug');
|
|
238
|
+
expect(res.source).toBe('git');
|
|
239
|
+
}
|
|
240
|
+
});
|
|
241
|
+
});
|
|
242
|
+
// ── 2. The eval scenario, end to end ─────────────────────────────────────
|
|
243
|
+
describe('#4707 — eval scenario end to end', () => {
|
|
244
|
+
it('markers on origin/main but NOT in the stale checkout still yield a populated block', () => {
|
|
245
|
+
const { projectRoot, worktreePath } = makeRepos({
|
|
246
|
+
// Exactly eval's pre-#4521 roleguide: no markers at all.
|
|
247
|
+
staleRoleguide: '# Eval Roleguide\n\nNo behavioral markers in this checkout.\n',
|
|
248
|
+
currentRoleguide: roleguide(block({ marker: 'EVAL-BLOCK-FROM-REF' })),
|
|
249
|
+
});
|
|
250
|
+
writeClaudeLocal({
|
|
251
|
+
projectRoot,
|
|
252
|
+
worktreePath,
|
|
253
|
+
worktreeName: 'eval-20260628',
|
|
254
|
+
branch: 'worktree/eval-20260628',
|
|
255
|
+
slot: 0,
|
|
256
|
+
teamSlug: 'eval',
|
|
257
|
+
baseBranch: 'main',
|
|
258
|
+
});
|
|
259
|
+
const localMd = readFileSync(join(worktreePath, 'CLAUDE.local.md'), 'utf-8');
|
|
260
|
+
expect(localMd).toContain('# Eval Integrity — Behavioral Checks (every-turn reinforcement)');
|
|
261
|
+
expect(localMd).toContain('EVAL-BLOCK-FROM-REF');
|
|
262
|
+
expect(localMd).toContain('## Decision Authority');
|
|
263
|
+
expect(localMd).toContain('# Worktree Isolation Context');
|
|
264
|
+
// The 24-line isolation-only file is what the bug shipped for four weeks.
|
|
265
|
+
expect(localMd.split('\n').length).toBeGreaterThan(24);
|
|
266
|
+
// And no warning about a missing block, because there is not one.
|
|
267
|
+
expect(warnings().find((e) => e.code === 'behavioral-injection-missing')).toBeUndefined();
|
|
268
|
+
expect(warnings().find((e) => e.code === 'behavioral-git-read-fallback')).toBeUndefined();
|
|
269
|
+
});
|
|
270
|
+
it('self-heal verifies the DELIVERED file and reports the ref it came from', () => {
|
|
271
|
+
const { projectRoot, worktreePath } = makeRepos({
|
|
272
|
+
staleRoleguide: '# Eval Roleguide\n\nNo markers here.\n',
|
|
273
|
+
currentRoleguide: roleguide(block()),
|
|
274
|
+
});
|
|
275
|
+
const heal = writeClaudeLocalWithBehavioralSelfHeal({
|
|
276
|
+
projectRoot,
|
|
277
|
+
worktreePath,
|
|
278
|
+
worktreeName: 'eval-20260628',
|
|
279
|
+
branch: 'worktree/eval-20260628',
|
|
280
|
+
slot: 0,
|
|
281
|
+
teamSlug: 'eval',
|
|
282
|
+
baseBranch: 'main',
|
|
283
|
+
});
|
|
284
|
+
expect(heal.ok).toBe(true);
|
|
285
|
+
expect(heal.source).toBe('git');
|
|
286
|
+
expect(heal.ref).toBe('origin/main');
|
|
287
|
+
expect(heal.missing).toEqual([]);
|
|
288
|
+
expect(warnings()).toEqual([]);
|
|
289
|
+
const delivered = extractInjectedBehavioralBlock(readFileSync(join(worktreePath, 'CLAUDE.local.md'), 'utf-8'));
|
|
290
|
+
expect(delivered).toContain('## Decision Authority');
|
|
291
|
+
expect(delivered).not.toContain('# Worktree Isolation Context');
|
|
292
|
+
expect(delivered).not.toContain('Prose above the block is never injected');
|
|
293
|
+
});
|
|
294
|
+
});
|
|
295
|
+
// ── 3. Content assertion — presence is not content ───────────────────────
|
|
296
|
+
describe('#4707 — content completeness names what is missing', () => {
|
|
297
|
+
const cases = [
|
|
298
|
+
['prior-art', 'prior-art gate'],
|
|
299
|
+
['decision-authority', 'decision authority'],
|
|
300
|
+
['anti-approval', 'anti-approval-seeking'],
|
|
301
|
+
['delegation', 'delegation / stop conditions'],
|
|
302
|
+
];
|
|
303
|
+
it('a complete block passes with nothing missing', () => {
|
|
304
|
+
const r = checkBehavioralCompleteness(block(), 'rg.md');
|
|
305
|
+
expect(r.ok).toBe(true);
|
|
306
|
+
expect(r.missing).toEqual([]);
|
|
307
|
+
});
|
|
308
|
+
for (const [omitted, label] of cases) {
|
|
309
|
+
it(`a block missing the ${label} fails and names it`, () => {
|
|
310
|
+
const r = checkBehavioralCompleteness(block({ omit: [omitted] }), 'rg.md');
|
|
311
|
+
expect(r.ok).toBe(false);
|
|
312
|
+
expect(r.problem).toBe('content-incomplete');
|
|
313
|
+
expect(r.missing).toEqual([label]);
|
|
314
|
+
expect(r.detail).toContain(label);
|
|
315
|
+
expect(describeBehavioralProblem(r)).toContain(label);
|
|
316
|
+
});
|
|
317
|
+
}
|
|
318
|
+
it('reports EVERY missing element, not just the first', () => {
|
|
319
|
+
const r = checkBehavioralCompleteness(block({ omit: ['prior-art', 'delegation'] }), 'rg.md');
|
|
320
|
+
expect(r.missing).toEqual(['prior-art gate', 'delegation / stop conditions']);
|
|
321
|
+
expect(r.detail).toContain('2 of 4');
|
|
322
|
+
});
|
|
323
|
+
it('the mcp proof case — a long block with none of the four is INCOMPLETE, not OK', () => {
|
|
324
|
+
// mcp shipped 57 lines carrying 0 of 4. Length is not content.
|
|
325
|
+
const filler = Array.from({ length: 57 }, (_, i) => `Line ${i} of mechanisms reference text.`).join('\n');
|
|
326
|
+
const r = checkBehavioralCompleteness(filler, 'mcp-experience-leader.md');
|
|
327
|
+
expect(r.ok).toBe(false);
|
|
328
|
+
expect(r.problem).toBe('content-incomplete');
|
|
329
|
+
expect(r.missing).toHaveLength(REQUIRED_BEHAVIORAL_ELEMENTS.length);
|
|
330
|
+
expect(findMissingBehavioralElements(filler)).toHaveLength(4);
|
|
331
|
+
});
|
|
332
|
+
it("recognises eval's live wording — no false 'missing' on a compliant block", () => {
|
|
333
|
+
// Verbatim phrasing from eval-leader-v3.md on origin/main. The first draft
|
|
334
|
+
// of the element matchers reported this block as missing BOTH decision
|
|
335
|
+
// authority and anti-approval-seeking, when it carries both in its own
|
|
336
|
+
// words. A checker that fires on a compliant roleguide is the thing teams
|
|
337
|
+
// learn to ignore, so the live wording is pinned here.
|
|
338
|
+
const evalStyle = [
|
|
339
|
+
'## Authority and Accountability',
|
|
340
|
+
'',
|
|
341
|
+
'The leader is the DRI for the question, design, integration, execution, and',
|
|
342
|
+
'interpretation. Once the product owner authorizes a direction and budget, the',
|
|
343
|
+
'leader proceeds without repeatedly requesting approval. Only a decision outside',
|
|
344
|
+
'that authority, a material change to the question, or an invalidating blocker',
|
|
345
|
+
'requires escalation.',
|
|
346
|
+
'',
|
|
347
|
+
'## Minimum Team and Delegation',
|
|
348
|
+
'',
|
|
349
|
+
'No more than two subagents may be active. No recursive delegation.',
|
|
350
|
+
].join('\n');
|
|
351
|
+
expect(findMissingBehavioralElements(evalStyle)).toEqual(['prior-art gate']);
|
|
352
|
+
});
|
|
353
|
+
it('an empty block is content-empty, not content-incomplete', () => {
|
|
354
|
+
expect(checkBehavioralCompleteness(' \n\t ', 'rg.md').problem).toBe('content-empty');
|
|
355
|
+
expect(checkBehavioralCompleteness(null, 'rg.md').problem).toBe('content-empty');
|
|
356
|
+
});
|
|
357
|
+
it('self-heal warns with content-incomplete and names the element, and still starts', () => {
|
|
358
|
+
const { projectRoot, worktreePath } = makeRepos({
|
|
359
|
+
staleRoleguide: roleguide(block()),
|
|
360
|
+
currentRoleguide: roleguide(block({ omit: ['prior-art'] })),
|
|
361
|
+
});
|
|
362
|
+
const heal = writeClaudeLocalWithBehavioralSelfHeal({
|
|
363
|
+
projectRoot,
|
|
364
|
+
worktreePath,
|
|
365
|
+
worktreeName: 'eval-20260628',
|
|
366
|
+
branch: 'worktree/eval-20260628',
|
|
367
|
+
slot: 0,
|
|
368
|
+
teamSlug: 'eval',
|
|
369
|
+
baseBranch: 'main',
|
|
370
|
+
});
|
|
371
|
+
expect(heal.ok).toBe(false);
|
|
372
|
+
expect(heal.problem).toBe('content-incomplete');
|
|
373
|
+
expect(heal.missing).toEqual(['prior-art gate']);
|
|
374
|
+
const warn = warnings().find((e) => e.code === 'behavioral-content-incomplete');
|
|
375
|
+
expect(warn).toBeDefined();
|
|
376
|
+
expect(warn.message).toContain('prior-art gate');
|
|
377
|
+
expect(warn.message).toContain('eval');
|
|
378
|
+
// FAIL-OPEN (#4123): the file exists and the session may start.
|
|
379
|
+
const localMd = readFileSync(join(worktreePath, 'CLAUDE.local.md'), 'utf-8');
|
|
380
|
+
expect(localMd).toContain('# Worktree Isolation Context');
|
|
381
|
+
expect(localMd).toContain('## Decision Authority');
|
|
382
|
+
});
|
|
383
|
+
it('asserts the DELIVERED file, not the resolver result — a failed write is caught', () => {
|
|
384
|
+
// The resolve succeeds and returns a complete block, but the write cannot
|
|
385
|
+
// land (CLAUDE.local.md is a directory). A check that trusted the resolver's
|
|
386
|
+
// return value would report a healthy session while the model reads nothing:
|
|
387
|
+
// that is #4707's own failure — verifying the artifact you edited instead of
|
|
388
|
+
// the outcome you claimed — repeated one layer down.
|
|
389
|
+
const { projectRoot, worktreePath } = makeRepos({
|
|
390
|
+
staleRoleguide: roleguide(block()),
|
|
391
|
+
currentRoleguide: roleguide(block()),
|
|
392
|
+
});
|
|
393
|
+
mkdirSync(join(worktreePath, 'CLAUDE.local.md'), { recursive: true });
|
|
394
|
+
const heal = writeClaudeLocalWithBehavioralSelfHeal({
|
|
395
|
+
projectRoot,
|
|
396
|
+
worktreePath,
|
|
397
|
+
worktreeName: 'eval-20260628',
|
|
398
|
+
branch: 'worktree/eval-20260628',
|
|
399
|
+
slot: 0,
|
|
400
|
+
teamSlug: 'eval',
|
|
401
|
+
baseBranch: 'main',
|
|
402
|
+
});
|
|
403
|
+
expect(heal.ok).toBe(false);
|
|
404
|
+
expect(warnings().find((e) => e.code === 'behavioral-selfheal-unverified')).toBeDefined();
|
|
405
|
+
});
|
|
406
|
+
it('self-heal warns when the delivered file has NO block at all', () => {
|
|
407
|
+
const { projectRoot, worktreePath } = makeRepos({
|
|
408
|
+
staleRoleguide: '# R\n\nno markers\n',
|
|
409
|
+
currentRoleguide: '# R\n\nno markers on the ref either\n',
|
|
410
|
+
});
|
|
411
|
+
const heal = writeClaudeLocalWithBehavioralSelfHeal({
|
|
412
|
+
projectRoot,
|
|
413
|
+
worktreePath,
|
|
414
|
+
worktreeName: 'eval-20260628',
|
|
415
|
+
branch: 'worktree/eval-20260628',
|
|
416
|
+
slot: 0,
|
|
417
|
+
teamSlug: 'eval',
|
|
418
|
+
baseBranch: 'main',
|
|
419
|
+
});
|
|
420
|
+
expect(heal.ok).toBe(false);
|
|
421
|
+
expect(heal.problem).toBe('content-empty');
|
|
422
|
+
const warn = warnings().find((e) => e.code === 'behavioral-selfheal-missing');
|
|
423
|
+
expect(warn).toBeDefined();
|
|
424
|
+
expect(warn.message).toContain('NO every-turn behavioral reinforcement');
|
|
425
|
+
});
|
|
426
|
+
});
|
|
427
|
+
// ── 4. Repeated / unbalanced markers fail loudly ─────────────────────────
|
|
428
|
+
describe('#4707 — extractMarkerBlock rejects ambiguous markers', () => {
|
|
429
|
+
it('two marker pairs → markers-repeated, no partial extraction', () => {
|
|
430
|
+
const text = [
|
|
431
|
+
BEHAVIORAL_START_MARKER, 'first block', BEHAVIORAL_END_MARKER,
|
|
432
|
+
'', 'prose', '',
|
|
433
|
+
BEHAVIORAL_START_MARKER, 'second block', BEHAVIORAL_END_MARKER,
|
|
434
|
+
].join('\n');
|
|
435
|
+
const r = extractMarkerBlock(text, 'rg.md');
|
|
436
|
+
expect(r.ok).toBe(false);
|
|
437
|
+
expect(r.problem).toBe('markers-repeated');
|
|
438
|
+
// MUTATION PIN: the old indexOf code returned ok with 'first block'.
|
|
439
|
+
expect(r.content).toBeNull();
|
|
440
|
+
expect(r.detail).toContain('2');
|
|
441
|
+
expect(describeBehavioralProblem(r)).toContain('exactly ONE');
|
|
442
|
+
});
|
|
443
|
+
it('nested markers → markers-repeated', () => {
|
|
444
|
+
const text = [
|
|
445
|
+
BEHAVIORAL_START_MARKER,
|
|
446
|
+
'outer',
|
|
447
|
+
BEHAVIORAL_START_MARKER,
|
|
448
|
+
'inner',
|
|
449
|
+
BEHAVIORAL_END_MARKER,
|
|
450
|
+
'tail',
|
|
451
|
+
BEHAVIORAL_END_MARKER,
|
|
452
|
+
].join('\n');
|
|
453
|
+
const r = extractMarkerBlock(text, 'rg.md');
|
|
454
|
+
expect(r.problem).toBe('markers-repeated');
|
|
455
|
+
expect(r.content).toBeNull();
|
|
456
|
+
});
|
|
457
|
+
it('unbalanced 2 starts / 1 end → markers-repeated naming both counts', () => {
|
|
458
|
+
const text = [
|
|
459
|
+
BEHAVIORAL_START_MARKER, 'a', BEHAVIORAL_START_MARKER, 'b', BEHAVIORAL_END_MARKER,
|
|
460
|
+
].join('\n');
|
|
461
|
+
const r = extractMarkerBlock(text, 'rg.md');
|
|
462
|
+
expect(r.problem).toBe('markers-repeated');
|
|
463
|
+
expect(r.detail).toContain('2');
|
|
464
|
+
expect(r.detail).toContain('1');
|
|
465
|
+
});
|
|
466
|
+
it('prose mentions of the markers are NOT counted — the team-builder case', () => {
|
|
467
|
+
// team-builder.md on origin/main names both markers in a sentence, in
|
|
468
|
+
// backticks and in a table row. Substring counting reads 4 starts / 3 ends
|
|
469
|
+
// for a file whose real block is one correct pair.
|
|
470
|
+
const text = [
|
|
471
|
+
'# Team Builder',
|
|
472
|
+
'',
|
|
473
|
+
BEHAVIORAL_START_MARKER,
|
|
474
|
+
block(),
|
|
475
|
+
BEHAVIORAL_END_MARKER,
|
|
476
|
+
'',
|
|
477
|
+
`A roleguide with no \`${BEHAVIORAL_START_MARKER}\` / \`${BEHAVIORAL_END_MARKER}\` block is refused.`,
|
|
478
|
+
`| Shipping a roleguide with no \`${BEHAVIORAL_START_MARKER}\` block | The team gets nothing. |`,
|
|
479
|
+
` wrap the sections in ${BEHAVIORAL_START_MARKER} and ${BEHAVIORAL_END_MARKER}. The CLI then generates`,
|
|
480
|
+
].join('\n');
|
|
481
|
+
const r = extractMarkerBlock(text, 'team-builder.md');
|
|
482
|
+
expect(r.ok).toBe(true);
|
|
483
|
+
expect(r.content).toContain('## Decision Authority');
|
|
484
|
+
expect(r.content).not.toContain('Shipping a roleguide');
|
|
485
|
+
});
|
|
486
|
+
it('an indented marker line still counts as a marker', () => {
|
|
487
|
+
const text = ` ${BEHAVIORAL_START_MARKER}\ncontent line\n\t${BEHAVIORAL_END_MARKER}\n`;
|
|
488
|
+
const r = extractMarkerBlock(text, 'rg.md');
|
|
489
|
+
expect(r.ok).toBe(true);
|
|
490
|
+
expect(r.content).toBe('content line');
|
|
491
|
+
});
|
|
492
|
+
it('a marker sharing its line with other text is not a marker', () => {
|
|
493
|
+
const text = `${BEHAVIORAL_START_MARKER} ## Heading\ncontent\n${BEHAVIORAL_END_MARKER}\n`;
|
|
494
|
+
const r = extractMarkerBlock(text, 'rg.md');
|
|
495
|
+
expect(r.ok).toBe(false);
|
|
496
|
+
expect(r.problem).toBe('markers-missing');
|
|
497
|
+
expect(r.detail).toContain(BEHAVIORAL_START_MARKER);
|
|
498
|
+
});
|
|
499
|
+
});
|
|
500
|
+
// ── 4b. Size, byte and tree-entry limits (security review SF-2) ──────────
|
|
501
|
+
describe('#4707 SF-2 — size and byte limits never route to the stale fallback', () => {
|
|
502
|
+
it('an oversize roleguide on the ref is REJECTED, not treated as unavailable', () => {
|
|
503
|
+
// The failure this pins: spawnSync returns a null status on maxBuffer
|
|
504
|
+
// overrun, which naive mapping turns into "git failed" → `unavailable` →
|
|
505
|
+
// LOCAL FALLBACK. A size problem would then hand the session the stale tree
|
|
506
|
+
// this whole change exists to stop reading.
|
|
507
|
+
const { projectRoot, worktreePath } = makeRepos({
|
|
508
|
+
staleRoleguide: roleguide(block({ marker: 'LOCAL-TREE-CONTENT' })),
|
|
509
|
+
currentRoleguide: roleguide(block()),
|
|
510
|
+
});
|
|
511
|
+
const padding = 'x'.repeat(MAX_ROLEGUIDE_BYTES + 64 * 1024);
|
|
512
|
+
write(worktreePath, RG_REL, roleguide(block()) + '\n' + padding);
|
|
513
|
+
commitAll(worktreePath, 'oversize roleguide');
|
|
514
|
+
git(worktreePath, 'push', '-q', 'origin', 'main');
|
|
515
|
+
git(worktreePath, 'fetch', '-q', 'origin');
|
|
516
|
+
const res = resolveBehavioralInjection(projectRoot, 'eval', {
|
|
517
|
+
repoPath: worktreePath, baseBranch: 'main',
|
|
518
|
+
});
|
|
519
|
+
expect(res.source).toBe('git'); // NOT 'local'
|
|
520
|
+
expect(res.fallbackReason).toBeNull();
|
|
521
|
+
expect(res.result.ok).toBe(false);
|
|
522
|
+
expect(res.result.problem).toBe('content-too-large');
|
|
523
|
+
expect(res.result.content).toBeNull();
|
|
524
|
+
// And nothing from the stale tree leaked in.
|
|
525
|
+
expect(res.result.detail).not.toContain('LOCAL-TREE-CONTENT');
|
|
526
|
+
});
|
|
527
|
+
it('an oversize BLOCK is rejected on the local path too', () => {
|
|
528
|
+
const huge = `${block()}\n${'y'.repeat(MAX_BEHAVIORAL_BLOCK_BYTES + 1024)}`;
|
|
529
|
+
const r = extractMarkerBlock(roleguide(huge), 'rg.md');
|
|
530
|
+
expect(r.ok).toBe(false);
|
|
531
|
+
expect(r.problem).toBe('content-too-large');
|
|
532
|
+
expect(r.content).toBeNull();
|
|
533
|
+
expect(describeBehavioralProblem(r)).toContain('every turn');
|
|
534
|
+
});
|
|
535
|
+
it('an oversize roleguide FILE is refused on the local path before it is read', () => {
|
|
536
|
+
// The fallback path was an unbounded readFileSync straight into memory and,
|
|
537
|
+
// one step later, into per-turn context. The size is checked off the same
|
|
538
|
+
// descriptor the bytes would come from.
|
|
539
|
+
const abs = join(tmpRoot, 'rg-huge.md');
|
|
540
|
+
mkdirSync(dirname(abs), { recursive: true });
|
|
541
|
+
writeFileSync(abs, roleguide(block()) + 'q'.repeat(MAX_ROLEGUIDE_BYTES));
|
|
542
|
+
const r = checkRoleguideMarkers(abs, { allowedRoots: [tmpRoot], label: 'rg-huge.md' });
|
|
543
|
+
expect(r.ok).toBe(false);
|
|
544
|
+
expect(r.problem).toBe('content-too-large');
|
|
545
|
+
expect(r.raw).toBeNull();
|
|
546
|
+
});
|
|
547
|
+
it('a block just under the limit is still accepted', () => {
|
|
548
|
+
const filler = 'z'.repeat(MAX_BEHAVIORAL_BLOCK_BYTES - Buffer.byteLength(block()) - 16);
|
|
549
|
+
const r = extractMarkerBlock(roleguide(`${block()}\n${filler}`), 'rg.md');
|
|
550
|
+
expect(r.ok).toBe(true);
|
|
551
|
+
});
|
|
552
|
+
it('control characters in the block are named and nothing is injected', () => {
|
|
553
|
+
// ESC reaches the operator's terminal through the warning itself.
|
|
554
|
+
const r = extractMarkerBlock(roleguide(`${block()}\n\x1B[2J evil`), 'rg.md');
|
|
555
|
+
expect(r.ok).toBe(false);
|
|
556
|
+
expect(r.problem).toBe('content-control-characters');
|
|
557
|
+
expect(r.content).toBeNull();
|
|
558
|
+
expect(r.detail).toContain('0x1B');
|
|
559
|
+
expect(describeBehavioralProblem(r)).toContain('terminal');
|
|
560
|
+
});
|
|
561
|
+
it('a NUL byte in the block is rejected', () => {
|
|
562
|
+
const r = extractMarkerBlock(roleguide(`${block()}\x00`), 'rg.md');
|
|
563
|
+
expect(r.problem).toBe('content-control-characters');
|
|
564
|
+
});
|
|
565
|
+
it('tab, newline and carriage return are NOT control characters here', () => {
|
|
566
|
+
const r = extractMarkerBlock(roleguide(`${block()}\n\tindented\r\nline`), 'rg.md');
|
|
567
|
+
expect(r.ok).toBe(true);
|
|
568
|
+
});
|
|
569
|
+
it('a control character in the Display name falls back to the slug', () => {
|
|
570
|
+
// The display name lands in the CLAUDE.local.md header AND in warnings.
|
|
571
|
+
// Losing a label is the right trade; losing the block is not.
|
|
572
|
+
const { projectRoot, worktreePath } = makeRepos({
|
|
573
|
+
staleRoleguide: roleguide(block()),
|
|
574
|
+
currentRoleguide: roleguide(block()),
|
|
575
|
+
teamFileContent: `# Team: eval\n\nDisplay name: Eval\x1B[31m Integrity\nRoleguide: \`${RG_REL}\`\n`,
|
|
576
|
+
});
|
|
577
|
+
const res = resolveBehavioralInjection(projectRoot, 'eval', {
|
|
578
|
+
repoPath: worktreePath, baseBranch: 'main',
|
|
579
|
+
});
|
|
580
|
+
expect(res.result.displayName).toBe('eval');
|
|
581
|
+
expect(res.result.ok).toBe(true); // the block still ships
|
|
582
|
+
});
|
|
583
|
+
it('a gitlink (mode 160000) tree entry is rejected, not read', () => {
|
|
584
|
+
const { projectRoot, worktreePath } = makeRepos({
|
|
585
|
+
staleRoleguide: roleguide(block({ marker: 'LOCAL-TREE-CONTENT' })),
|
|
586
|
+
currentRoleguide: roleguide(block()),
|
|
587
|
+
});
|
|
588
|
+
// Replace the roleguide entry with a submodule pointer. `git show ref:path`
|
|
589
|
+
// on a gitlink does not yield file bytes, and the mode is not 100644/100755.
|
|
590
|
+
const oid = git(worktreePath, 'rev-parse', 'HEAD').trim();
|
|
591
|
+
git(worktreePath, 'rm', '-q', '--cached', RG_REL);
|
|
592
|
+
git(worktreePath, 'update-index', '--add', '--cacheinfo', `160000,${oid},${RG_REL}`);
|
|
593
|
+
git(worktreePath, '-c', 'user.email=t@t', '-c', 'user.name=T', 'commit', '-q', '-m', 'roleguide replaced by a gitlink');
|
|
594
|
+
git(worktreePath, 'push', '-q', 'origin', 'main');
|
|
595
|
+
git(worktreePath, 'fetch', '-q', 'origin');
|
|
596
|
+
const res = resolveBehavioralInjection(projectRoot, 'eval', {
|
|
597
|
+
repoPath: worktreePath, baseBranch: 'main',
|
|
598
|
+
});
|
|
599
|
+
expect(res.source).toBe('git');
|
|
600
|
+
expect(res.result.ok).toBe(false);
|
|
601
|
+
expect(res.result.problem).toBe('roleguide-not-regular-file');
|
|
602
|
+
expect(res.result.detail).toContain('160000');
|
|
603
|
+
});
|
|
604
|
+
});
|
|
605
|
+
// ── 4c. Integrity of the delivered file (security review SF-1) ───────────
|
|
606
|
+
describe('#4707 SF-1 — the verifier checks integrity, not keywords', () => {
|
|
607
|
+
it('a stale projectRoot CLAUDE.local.md carrying the header triggers a MISMATCH', () => {
|
|
608
|
+
const { projectRoot, worktreePath } = makeRepos({
|
|
609
|
+
staleRoleguide: roleguide(block()),
|
|
610
|
+
currentRoleguide: roleguide(block()),
|
|
611
|
+
});
|
|
612
|
+
// A leftover from an earlier generation: same header, different body. It is
|
|
613
|
+
// merged ABOVE the generated content, so it is the first header in the file.
|
|
614
|
+
writeFileSync(join(projectRoot, 'CLAUDE.local.md'), [
|
|
615
|
+
'# Some Other Team — Behavioral Checks (every-turn reinforcement)',
|
|
616
|
+
'',
|
|
617
|
+
'## Prior Art Search',
|
|
618
|
+
'## Decision Authority',
|
|
619
|
+
'Delegate and escalate to whoever. Decide and execute.',
|
|
620
|
+
'',
|
|
621
|
+
].join('\n'));
|
|
622
|
+
const heal = writeClaudeLocalWithBehavioralSelfHeal({
|
|
623
|
+
projectRoot, worktreePath,
|
|
624
|
+
worktreeName: 'eval-20260628', branch: 'worktree/eval-20260628', slot: 0,
|
|
625
|
+
teamSlug: 'eval', baseBranch: 'main',
|
|
626
|
+
});
|
|
627
|
+
// The foreign block carries all four elements, so a keyword check would have
|
|
628
|
+
// passed it. Integrity is what catches it.
|
|
629
|
+
expect(heal.ok).toBe(false);
|
|
630
|
+
const warn = warnings().find((e) => e.code === 'behavioral-selfheal-mismatch');
|
|
631
|
+
expect(warn).toBeDefined();
|
|
632
|
+
expect(warn.message).toContain('Behavioral Checks');
|
|
633
|
+
expect(warn.message).toContain(join(projectRoot, 'CLAUDE.local.md'));
|
|
634
|
+
});
|
|
635
|
+
it('a foreign header with NO resolved block is a mismatch, not a quiet pass', () => {
|
|
636
|
+
const { projectRoot, worktreePath } = makeRepos({
|
|
637
|
+
staleRoleguide: '# R\n\nno markers\n',
|
|
638
|
+
currentRoleguide: '# R\n\nno markers on the ref either\n',
|
|
639
|
+
});
|
|
640
|
+
writeFileSync(join(projectRoot, 'CLAUDE.local.md'), '# Ghost Team — Behavioral Checks (every-turn reinforcement)\n\nnothing real here\n');
|
|
641
|
+
const heal = writeClaudeLocalWithBehavioralSelfHeal({
|
|
642
|
+
projectRoot, worktreePath,
|
|
643
|
+
worktreeName: 'eval-20260628', branch: 'worktree/eval-20260628', slot: 0,
|
|
644
|
+
teamSlug: 'eval', baseBranch: 'main',
|
|
645
|
+
});
|
|
646
|
+
expect(heal.ok).toBe(false);
|
|
647
|
+
expect(warnings().find((e) => e.code === 'behavioral-selfheal-mismatch')).toBeDefined();
|
|
648
|
+
});
|
|
649
|
+
it('the normal case has exactly one header and passes integrity', () => {
|
|
650
|
+
const { projectRoot, worktreePath } = makeRepos({
|
|
651
|
+
staleRoleguide: roleguide(block()),
|
|
652
|
+
currentRoleguide: roleguide(block()),
|
|
653
|
+
});
|
|
654
|
+
const heal = writeClaudeLocalWithBehavioralSelfHeal({
|
|
655
|
+
projectRoot, worktreePath,
|
|
656
|
+
worktreeName: 'eval-20260628', branch: 'worktree/eval-20260628', slot: 0,
|
|
657
|
+
teamSlug: 'eval', baseBranch: 'main',
|
|
658
|
+
});
|
|
659
|
+
expect(heal.ok).toBe(true);
|
|
660
|
+
expect(countBehavioralHeaders(readFileSync(join(worktreePath, 'CLAUDE.local.md'), 'utf-8'))).toBe(1);
|
|
661
|
+
expect(warnings()).toEqual([]);
|
|
662
|
+
});
|
|
663
|
+
});
|
|
664
|
+
// ── 4d. Rejected values are not printed raw (Codex finding 1) ────────────
|
|
665
|
+
const ESC = '\x1B';
|
|
666
|
+
describe('#4707 Codex-1 — refusing a value is not licence to echo it', () => {
|
|
667
|
+
it('an ESC-bearing team slug produces a warning with no ESC byte', () => {
|
|
668
|
+
// CLAUDE_TEAM_NAME=$'bad\x1b[2J' reaches the invalid-slug path. The slug is
|
|
669
|
+
// correctly refused — and the old code then quoted it into the warning, so
|
|
670
|
+
// the refusal path WAS the injection path. Same defect the repo already
|
|
671
|
+
// recorded as KI-STARTUPREPORT-PRINTOUT-UNSANITIZED.
|
|
672
|
+
const { projectRoot, worktreePath } = makeRepos({
|
|
673
|
+
staleRoleguide: roleguide(block()),
|
|
674
|
+
currentRoleguide: roleguide(block()),
|
|
675
|
+
});
|
|
676
|
+
const printed = capturePrinted(() => writeClaudeLocal({
|
|
677
|
+
projectRoot, worktreePath,
|
|
678
|
+
worktreeName: 'eval-20260628', branch: 'worktree/eval-20260628', slot: 0,
|
|
679
|
+
teamSlug: `bad${ESC}[2J`, baseBranch: 'main',
|
|
680
|
+
}));
|
|
681
|
+
// The terminal is the sink that matters here — see capturePrinted.
|
|
682
|
+
expect(printed).not.toContain(`${ESC}[2J`);
|
|
683
|
+
expect(printed).toContain('bad�[2J');
|
|
684
|
+
const warn = warnings().find((e) => e.code === 'behavioral-injection-missing');
|
|
685
|
+
expect(warn).toBeDefined();
|
|
686
|
+
// The property is the absence of the ESC BYTE. What remains — "bad�[2J" —
|
|
687
|
+
// is inert text: without ESC there is no CSI sequence for a terminal to
|
|
688
|
+
// execute, and the operator can still see which value was refused.
|
|
689
|
+
expect(warn.message).not.toContain(ESC);
|
|
690
|
+
expect(warn.message).toContain('bad');
|
|
691
|
+
expect(warn.message).toContain('�');
|
|
692
|
+
});
|
|
693
|
+
it('an unsafe backticked Roleguide path is refused without echoing its bytes', () => {
|
|
694
|
+
const { projectRoot, worktreePath } = makeRepos({
|
|
695
|
+
staleRoleguide: roleguide(block()),
|
|
696
|
+
currentRoleguide: roleguide(block()),
|
|
697
|
+
teamFileContent: `# Team: eval\n\nDisplay name: Eval\nRoleguide: \`../outside${ESC}[2K/evil.md\`\n`,
|
|
698
|
+
});
|
|
699
|
+
const res = resolveBehavioralInjection(projectRoot, 'eval', {
|
|
700
|
+
repoPath: worktreePath, baseBranch: 'main',
|
|
701
|
+
});
|
|
702
|
+
expect(res.result.ok).toBe(false);
|
|
703
|
+
expect(res.result.detail).not.toContain(ESC);
|
|
704
|
+
expect(res.result.detail).toContain('�');
|
|
705
|
+
});
|
|
706
|
+
it('sanitizeForMessage strips C0, C1 and DEL, and caps length', () => {
|
|
707
|
+
expect(sanitizeForMessage(`a${ESC}[31mb`)).toBe('a�[31mb');
|
|
708
|
+
expect(sanitizeForMessage('a\x00\x07\x7F\x9Bb')).toBe('a����b');
|
|
709
|
+
expect(sanitizeForMessage('tab\tand\nnewline')).toBe('tab�and�newline');
|
|
710
|
+
const long = sanitizeForMessage('x'.repeat(500));
|
|
711
|
+
expect(long.length).toBeLessThan(240);
|
|
712
|
+
expect(long).toContain('500 chars');
|
|
713
|
+
});
|
|
714
|
+
it('git stderr reaching a fallback reason is sanitized too', () => {
|
|
715
|
+
// The reason text is composed from git's own stderr, which quotes the path.
|
|
716
|
+
const res = resolveBehavioralInjection(join(tmpRoot, 'nope'), 'eval', {
|
|
717
|
+
repoPath: join(tmpRoot, 'not-a-repo'), baseBranch: `main${ESC}[2J`,
|
|
718
|
+
});
|
|
719
|
+
expect(res.source).toBe('local');
|
|
720
|
+
expect(res.fallbackReason).not.toContain(ESC);
|
|
721
|
+
});
|
|
722
|
+
});
|
|
723
|
+
// ── 4e. The completeness claim matches what is measured (Codex finding 2) ─
|
|
724
|
+
describe('#4707 Codex-2 — keyword match, stated as a keyword match', () => {
|
|
725
|
+
it('a block whose mentions are all negated is reported missing them', () => {
|
|
726
|
+
// Codex's example: every required phrase appears and every one is negated.
|
|
727
|
+
// A pure keyword check calls this complete.
|
|
728
|
+
const negated = [
|
|
729
|
+
'## Rules',
|
|
730
|
+
'',
|
|
731
|
+
'There is no prior-art gate here.',
|
|
732
|
+
'This team has no decision authority.',
|
|
733
|
+
// NOTE: kept as a single clause deliberately. Writing this as
|
|
734
|
+
// "Do not decide and execute; approval-seeking is required." would put a
|
|
735
|
+
// BARE mention of "approval-seeking" after the clause break, and a bare
|
|
736
|
+
// mention counts as present — see the known-gap test below. That is the
|
|
737
|
+
// documented limit interacting with SF-D's clause boundary, not a defect.
|
|
738
|
+
'Do not decide and execute.',
|
|
739
|
+
'Do not delegate, and never escalate to anyone.',
|
|
740
|
+
].join('\n');
|
|
741
|
+
expect(findMissingBehavioralElements(negated)).toEqual([
|
|
742
|
+
'prior-art gate', 'decision authority', 'anti-approval-seeking',
|
|
743
|
+
'delegation / stop conditions',
|
|
744
|
+
]);
|
|
745
|
+
});
|
|
746
|
+
it('DOES NOT catch a bare mention asserting the opposite policy — stated, not hidden', () => {
|
|
747
|
+
// Codex's line "approval-seeking is allowed" contains NO negator, so the
|
|
748
|
+
// narrow rule cannot see it: the phrase is mentioned and the sentence
|
|
749
|
+
// asserts the opposite of the control. Catching this needs semantics, and a
|
|
750
|
+
// matcher loose enough to try would fire on compliant roleguides — the
|
|
751
|
+
// false positive this session already paid for once.
|
|
752
|
+
//
|
|
753
|
+
// This test exists so the limit is a recorded property rather than a
|
|
754
|
+
// surprise, and it is why the success line says "matched by keyword" instead
|
|
755
|
+
// of "verified".
|
|
756
|
+
const bare = [
|
|
757
|
+
'There is no prior-art gate here.',
|
|
758
|
+
'This team has no decision authority.',
|
|
759
|
+
'Approval-seeking is allowed.',
|
|
760
|
+
'Do not delegate or escalate.',
|
|
761
|
+
].join('\n');
|
|
762
|
+
const missing = findMissingBehavioralElements(bare);
|
|
763
|
+
expect(missing).toContain('prior-art gate');
|
|
764
|
+
expect(missing).toContain('decision authority');
|
|
765
|
+
expect(missing).toContain('delegation / stop conditions');
|
|
766
|
+
expect(missing).not.toContain('anti-approval-seeking'); // the known gap
|
|
767
|
+
});
|
|
768
|
+
it('an incidental negation near a required phrase does NOT make it missing', () => {
|
|
769
|
+
// The false-positive boundary. eval's real block says "Do not delegate broad
|
|
770
|
+
// exploration" a few lines under its "Minimum Team and Delegation" heading;
|
|
771
|
+
// customer-portal's says "**No prior art found** — State explicitly…" inside
|
|
772
|
+
// a section that IS the prior-art gate. One negated mention among clean ones
|
|
773
|
+
// must not read as absence.
|
|
774
|
+
const compliant = [
|
|
775
|
+
'## Prior Art Search',
|
|
776
|
+
'**No prior art found** — say so explicitly; the search is still required.',
|
|
777
|
+
'',
|
|
778
|
+
'## Decision Authority',
|
|
779
|
+
'Full authority on iteration selection. Do not exceed that authority.',
|
|
780
|
+
'',
|
|
781
|
+
'### Anti-pattern: approval-seeking',
|
|
782
|
+
'Decide and execute. Not every call needs a second opinion.',
|
|
783
|
+
'',
|
|
784
|
+
'## Delegation and Stop Conditions',
|
|
785
|
+
'Delegate implementation. Do not delegate broad exploration.',
|
|
786
|
+
'Escalate to the product owner on cost.',
|
|
787
|
+
].join('\n');
|
|
788
|
+
expect(findMissingBehavioralElements(compliant)).toEqual([]);
|
|
789
|
+
});
|
|
790
|
+
it('every roleguide-style block keeps its pre-negation-rule verdict', () => {
|
|
791
|
+
// Regression guard for the rule's blast radius: the four fixture shapes used
|
|
792
|
+
// throughout this file must be unaffected by the negation logic.
|
|
793
|
+
expect(findMissingBehavioralElements(block())).toEqual([]);
|
|
794
|
+
expect(findMissingBehavioralElements(block({ omit: ['prior-art'] })))
|
|
795
|
+
.toEqual(['prior-art gate']);
|
|
796
|
+
});
|
|
797
|
+
it('the success line claims a keyword match, not a verification', () => {
|
|
798
|
+
const { projectRoot, worktreePath } = makeRepos({
|
|
799
|
+
staleRoleguide: roleguide(block()),
|
|
800
|
+
currentRoleguide: roleguide(block()),
|
|
801
|
+
});
|
|
802
|
+
const heal = writeClaudeLocalWithBehavioralSelfHeal({
|
|
803
|
+
projectRoot, worktreePath,
|
|
804
|
+
worktreeName: 'eval-20260628', branch: 'worktree/eval-20260628', slot: 0,
|
|
805
|
+
teamSlug: 'eval', baseBranch: 'main',
|
|
806
|
+
});
|
|
807
|
+
expect(heal.ok).toBe(true);
|
|
808
|
+
const line = startupReport._entries().map((e) => e.message).join('\n');
|
|
809
|
+
expect(line).toContain('matched by keyword');
|
|
810
|
+
expect(line).toContain('presence, not semantics');
|
|
811
|
+
// "verified" would invite every downstream reader to treat a heuristic as a
|
|
812
|
+
// verification — the misreading that produced #4541's false outcome claim.
|
|
813
|
+
expect(line).not.toContain('elements present');
|
|
814
|
+
expect(line).not.toMatch(/Behavioral block verified/);
|
|
815
|
+
});
|
|
816
|
+
});
|
|
817
|
+
// ── 4f. Security re-review items SF-A … SF-D ────────────────────────────
|
|
818
|
+
describe('#4707 SF-A/SF-B — no value is sanitized in one clause and raw in the next', () => {
|
|
819
|
+
it('the fallback warning sanitizes ref, remote, base branch and both paths', () => {
|
|
820
|
+
// `ref` is BUILT from gitRemote + baseBranch. Sanitizing the composed value
|
|
821
|
+
// and printing its ingredients raw in the same sentence is not a control.
|
|
822
|
+
const projectRoot = join(tmpRoot, 'plain-root');
|
|
823
|
+
const worktreePath = join(tmpRoot, 'plain-wt');
|
|
824
|
+
mkdirSync(worktreePath, { recursive: true });
|
|
825
|
+
write(projectRoot, 'mx-agent-system/teams/eval.md', teamFile('eval', 'Eval Integrity'));
|
|
826
|
+
write(projectRoot, RG_REL, roleguide(block()));
|
|
827
|
+
const printed = capturePrinted(() => writeClaudeLocal({
|
|
828
|
+
projectRoot, worktreePath,
|
|
829
|
+
worktreeName: 'eval-20260628', branch: 'worktree/eval-20260628', slot: 0,
|
|
830
|
+
teamSlug: 'eval',
|
|
831
|
+
baseBranch: `main${ESC}[2J`,
|
|
832
|
+
gitRemote: `origin${ESC}[1m`,
|
|
833
|
+
}));
|
|
834
|
+
const warn = warnings().find((e) => e.code === 'behavioral-git-read-fallback');
|
|
835
|
+
expect(warn).toBeDefined();
|
|
836
|
+
// The report copy is stripped on the way in, so it is the PRINTED bytes that
|
|
837
|
+
// carry the property. Assert on the injected sequences specifically, so a
|
|
838
|
+
// colour code from chalk cannot make this pass or fail by accident.
|
|
839
|
+
expect(printed).not.toContain(`${ESC}[2J`);
|
|
840
|
+
expect(printed).not.toContain(`${ESC}[1m`);
|
|
841
|
+
expect(printed).toContain('main�[2J'); // named, defanged
|
|
842
|
+
expect(printed).toContain('origin�[1m');
|
|
843
|
+
});
|
|
844
|
+
it('the containment rejection sanitizes the resolved path and the allowed roots', () => {
|
|
845
|
+
// create.ts passes an operator-supplied --roleguide here, and an escaping
|
|
846
|
+
// path is exactly what lands on this branch.
|
|
847
|
+
const outside = join(tmpRoot, `outside${ESC}[2K`);
|
|
848
|
+
mkdirSync(outside, { recursive: true });
|
|
849
|
+
const rg = join(outside, 'evil.md');
|
|
850
|
+
writeFileSync(rg, roleguide(block()));
|
|
851
|
+
const allowed = join(tmpRoot, 'allowed');
|
|
852
|
+
mkdirSync(allowed, { recursive: true });
|
|
853
|
+
const r = checkRoleguideMarkers(rg, { allowedRoots: [allowed], label: 'evil.md' });
|
|
854
|
+
expect(r.ok).toBe(false);
|
|
855
|
+
expect(r.problem).toBe('roleguide-path-escapes-root');
|
|
856
|
+
expect(r.detail).not.toContain(ESC);
|
|
857
|
+
expect(r.detail).toContain('�');
|
|
858
|
+
});
|
|
859
|
+
});
|
|
860
|
+
describe('#4707 SF-D — a negator cannot reach across a clause boundary', () => {
|
|
861
|
+
it('"Do not ask the PO; decide and execute." is COMPLIANT, not missing', () => {
|
|
862
|
+
// The reviewer's case. Before the fix the only matching phrase was judged
|
|
863
|
+
// negated by "Do not" 13 characters back, and anti-approval-seeking was
|
|
864
|
+
// reported absent from a roleguide that states it plainly.
|
|
865
|
+
const sentence = 'Do not ask the PO; decide and execute.';
|
|
866
|
+
expect(findMissingBehavioralElements(sentence)).not.toContain('anti-approval-seeking');
|
|
867
|
+
});
|
|
868
|
+
it('an em dash ends the clause too', () => {
|
|
869
|
+
expect(findMissingBehavioralElements('Do not wait — decide and execute.'))
|
|
870
|
+
.not.toContain('anti-approval-seeking');
|
|
871
|
+
});
|
|
872
|
+
it('the same sentence inside a full block leaves the block complete', () => {
|
|
873
|
+
const withSemicolon = block({ omit: ['anti-approval'] }) +
|
|
874
|
+
'\n\n## Working style\n\nDo not ask the PO; decide and execute.\n';
|
|
875
|
+
expect(findMissingBehavioralElements(withSemicolon)).toEqual([]);
|
|
876
|
+
});
|
|
877
|
+
it('a genuine negation with no clause break is still caught', () => {
|
|
878
|
+
// The fix must not blunt the rule: same negator, no separator.
|
|
879
|
+
expect(findMissingBehavioralElements('Do not decide and execute.'))
|
|
880
|
+
.toContain('anti-approval-seeking');
|
|
881
|
+
});
|
|
882
|
+
it('a negator beyond the window does not negate (SF-C bound is behaviour-preserving)', () => {
|
|
883
|
+
const far = `Never mind that. ${'padding '.repeat(12)}decide and execute.`;
|
|
884
|
+
expect(findMissingBehavioralElements(far)).not.toContain('anti-approval-seeking');
|
|
885
|
+
});
|
|
886
|
+
it('a negator at the far end of the 24-char tail is still seen', () => {
|
|
887
|
+
// Pins the 64-character window itself. NEGATOR allows 24 characters between
|
|
888
|
+
// the negator and the phrase, and the negator word can be several more, so
|
|
889
|
+
// a window materially smaller than 64 would start missing negations that the
|
|
890
|
+
// rule is specified to catch — a silent weakening with no test to notice.
|
|
891
|
+
// Prefix here is 'Do not ever, ever, ever ' — 24 characters of tail.
|
|
892
|
+
expect(findMissingBehavioralElements('Do not ever, ever, ever decide and execute.'))
|
|
893
|
+
.toContain('anti-approval-seeking');
|
|
894
|
+
});
|
|
895
|
+
});
|
|
896
|
+
// ── 5. Fallback — used when git cannot serve, and never silent ───────────
|
|
897
|
+
describe('#4707 — local fallback warns loudly', () => {
|
|
898
|
+
it('no origin remote → falls back to the local tree AND warns with the reason', () => {
|
|
899
|
+
// A worktree with no remote at all: git cannot serve the bytes, so the local
|
|
900
|
+
// read is the only option — and the operator must be told it happened.
|
|
901
|
+
const projectRoot = join(tmpRoot, 'plain-root');
|
|
902
|
+
const worktreePath = join(tmpRoot, 'plain-wt');
|
|
903
|
+
mkdirSync(worktreePath, { recursive: true });
|
|
904
|
+
write(projectRoot, 'mx-agent-system/teams/eval.md', teamFile('eval', 'Eval Integrity'));
|
|
905
|
+
write(projectRoot, RG_REL, roleguide(block({ marker: 'LOCAL-TREE-CONTENT' })));
|
|
906
|
+
const res = resolveBehavioralInjection(projectRoot, 'eval', {
|
|
907
|
+
repoPath: worktreePath, baseBranch: 'main',
|
|
908
|
+
});
|
|
909
|
+
expect(res.source).toBe('local');
|
|
910
|
+
expect(res.fallbackReason).toContain('origin/main');
|
|
911
|
+
expect(res.result.ok).toBe(true);
|
|
912
|
+
expect(res.result.content).toContain('LOCAL-TREE-CONTENT');
|
|
913
|
+
writeClaudeLocal({
|
|
914
|
+
projectRoot, worktreePath,
|
|
915
|
+
worktreeName: 'eval-20260628', branch: 'worktree/eval-20260628', slot: 0,
|
|
916
|
+
teamSlug: 'eval', baseBranch: 'main',
|
|
917
|
+
});
|
|
918
|
+
const warn = warnings().find((e) => e.code === 'behavioral-git-read-fallback');
|
|
919
|
+
expect(warn).toBeDefined();
|
|
920
|
+
expect(warn.message).toContain('origin/main');
|
|
921
|
+
expect(warn.message).toContain('may be stale');
|
|
922
|
+
expect(warn.message).toContain(projectRoot);
|
|
923
|
+
// The block is still delivered — fallback is a degraded read, not a refusal.
|
|
924
|
+
expect(readFileSync(join(worktreePath, 'CLAUDE.local.md'), 'utf-8'))
|
|
925
|
+
.toContain('LOCAL-TREE-CONTENT');
|
|
926
|
+
});
|
|
927
|
+
it('team not yet merged (absent from the ref, present locally) → fallback, warned', () => {
|
|
928
|
+
const { projectRoot, worktreePath } = makeRepos({
|
|
929
|
+
staleRoleguide: roleguide(block()),
|
|
930
|
+
currentRoleguide: roleguide(block()),
|
|
931
|
+
});
|
|
932
|
+
// A brand-new team: catalog entry exists only in the local tree.
|
|
933
|
+
write(projectRoot, 'mx-agent-system/teams/newteam.md', teamFile('newteam', 'New Team', 'mx-agent-system/roleguides/new-leader.md'));
|
|
934
|
+
write(projectRoot, 'mx-agent-system/roleguides/new-leader.md', roleguide(block({ marker: 'UNMERGED-TEAM-CONTENT' })));
|
|
935
|
+
const res = resolveBehavioralInjection(projectRoot, 'newteam', {
|
|
936
|
+
repoPath: worktreePath, baseBranch: 'main',
|
|
937
|
+
});
|
|
938
|
+
expect(res.source).toBe('local');
|
|
939
|
+
expect(res.fallbackReason).toContain('does not exist in origin/main');
|
|
940
|
+
expect(res.result.content).toContain('UNMERGED-TEAM-CONTENT');
|
|
941
|
+
});
|
|
942
|
+
it('a missing base branch on the remote falls back rather than failing the start', () => {
|
|
943
|
+
const { projectRoot, worktreePath } = makeRepos({
|
|
944
|
+
staleRoleguide: roleguide(block({ marker: 'LOCAL-TREE-CONTENT' })),
|
|
945
|
+
currentRoleguide: roleguide(block({ marker: 'REF-CONTENT' })),
|
|
946
|
+
});
|
|
947
|
+
const res = resolveBehavioralInjection(projectRoot, 'eval', {
|
|
948
|
+
repoPath: worktreePath, baseBranch: 'no-such-branch',
|
|
949
|
+
});
|
|
950
|
+
expect(res.source).toBe('local');
|
|
951
|
+
expect(res.fallbackReason).toContain('origin/no-such-branch');
|
|
952
|
+
expect(res.result.content).toContain('LOCAL-TREE-CONTENT');
|
|
953
|
+
});
|
|
954
|
+
});
|
|
955
|
+
//# sourceMappingURL=behavioral-git-read.test.js.map
|