@aibyzero/cm-workflow 0.11.0 → 0.13.2
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/.codex-plugin/plugin.json +1 -1
- package/AGENTS.md +3 -0
- package/CHANGELOG.md +64 -3
- package/README.md +45 -3
- package/VERSION +1 -1
- package/compat/claude-commands/cm-runtime.md +3 -0
- package/compat/claude-commands/cm-security.md +3 -0
- package/docs/architecture.md +22 -0
- package/docs/installation.md +56 -2
- package/docs/js-workflow-control.md +63 -6
- package/docs/runtime-failover.md +97 -0
- package/docs/user-guide.md +60 -3
- package/install-codex.sh +5 -0
- package/install.ps1 +10 -2
- package/install.sh +5 -0
- package/package.json +3 -1
- package/runtime/js/cm-ai/claude-developer-proposal.schema.json +151 -0
- package/runtime/js/cm-ai/claude-review-stream.mjs +69 -8
- package/runtime/js/cm-ai/claude-tool-preview.mjs +6 -4
- package/runtime/js/cm-ai/cm-ai-admission.mjs +38 -16
- package/runtime/js/cm-ai/cm-ai-conversation-entry.mjs +44 -17
- package/runtime/js/cm-ai/cm-ai-qa-log.mjs +105 -12
- package/runtime/js/cm-ai/codex-config.mjs +6 -0
- package/runtime/js/cm-ai/codex-review-adapter.mjs +6 -2
- package/runtime/js/cm-ai/developer-adapter.mjs +41 -16
- package/runtime/js/cm-ai/durable-runner-state.mjs +72 -15
- package/runtime/js/cm-ai/effect-contract.mjs +10 -1
- package/runtime/js/cm-ai/fix-code-association.mjs +4 -1
- package/runtime/js/cm-ai/host-conversation-execution.mjs +112 -27
- package/runtime/js/cm-ai/host-documentation.mjs +4 -2
- package/runtime/js/cm-ai/host-qa-executor.mjs +17 -4
- package/runtime/js/cm-ai/host-qa-policy.mjs +6 -3
- package/runtime/js/cm-ai/host-role-routing.mjs +9 -5
- package/runtime/js/cm-ai/host-tool-bridge.mjs +10 -7
- package/runtime/js/cm-ai/host-workflow-capabilities.mjs +9 -4
- package/runtime/js/cm-ai/host.mjs +1 -1
- package/runtime/js/cm-ai/provider-review-observation.mjs +8 -4
- package/runtime/js/cm-ai/qa-attachment.mjs +30 -0
- package/runtime/js/cm-ai/review-package.mjs +76 -18
- package/runtime/js/cm-ai/review-runner.mjs +15 -3
- package/runtime/js/cm-ai/runtime-failover.mjs +75 -0
- package/runtime/js/cm-ai/specification-material.mjs +98 -0
- package/runtime/js/cm-ai/task-runner.mjs +57 -20
- package/runtime/js/cm-ai/worker-claude-developer.mjs +174 -0
- package/runtime/js/cm-ai/worker-claude.mjs +18 -5
- package/runtime/js/cm-ai/worker-codex-developer.mjs +15 -4
- package/runtime/js/cm-ai/worker-codex.mjs +8 -12
- package/runtime/js/cm-init/draft-generation.mjs +24 -4
- package/runtime/js/cm-init/draft-inspection.mjs +29 -1
- package/runtime/js/cm-prd/session.mjs +19 -2
- package/runtime/js/cm-prd/summary.mjs +46 -20
- package/runtime/js/cm-security/scan.mjs +263 -0
- package/runtime/js/cm-test/branch-impact.mjs +8 -2
- package/runtime/review.md +11 -1
- package/runtime/workflow-config.md +39 -2
- package/runtime/workflow-routing.md +6 -1
- package/scripts/cm-ai-admission.test.mjs +99 -0
- package/scripts/cm-ai-batch-host.test.mjs +4 -1
- package/scripts/cm-ai-batch-run.test.mjs +7 -1
- package/scripts/cm-ai-bootstrap.test.mjs +2 -1
- package/scripts/cm-ai-fix-integration.test.mjs +2 -1
- package/scripts/cm-ai-host.mjs +81 -12
- package/scripts/cm-ai-host.test.mjs +520 -5
- package/scripts/cm-ai-multi-root.test.mjs +9 -3
- package/scripts/cm-ai-nested-execution.test.mjs +11 -4
- package/scripts/cm-ai-review-timeout.test.mjs +226 -0
- package/scripts/cm-ai-run.mjs +40 -13
- package/scripts/cm-ai-run.test.mjs +45 -9
- package/scripts/cm-check-host.test.mjs +1 -1
- package/scripts/cm-check-runtime.sh +56 -1
- package/scripts/cm-check-update.mjs +200 -0
- package/scripts/cm-check-update.test.mjs +186 -0
- package/scripts/cm-claude-developer-worker.test.mjs +236 -0
- package/scripts/cm-claude-probe.test.mjs +64 -1
- package/scripts/cm-claude-review-stream.test.mjs +236 -1
- package/scripts/cm-claude-worker.test.mjs +110 -1
- package/scripts/cm-codex-developer-adapter.test.mjs +50 -2
- package/scripts/cm-codex-developer-worker.test.mjs +84 -1
- package/scripts/cm-codex-review-progress.test.mjs +34 -6
- package/scripts/cm-failover.mjs +288 -0
- package/scripts/cm-failover.test.mjs +184 -0
- package/scripts/cm-host-qa-executor.test.mjs +277 -0
- package/scripts/cm-host-tool-bridge.test.mjs +31 -0
- package/scripts/cm-init-draft-generation.test.mjs +62 -0
- package/scripts/cm-init-draft-inspection.test.mjs +76 -0
- package/scripts/cm-init-host.mjs +4 -4
- package/scripts/cm-init-host.test.mjs +12 -7
- package/scripts/cm-init-session.test.mjs +50 -0
- package/scripts/cm-log-event.mjs +34 -5
- package/scripts/cm-prd-completion.test.mjs +2 -2
- package/scripts/cm-prd-host.mjs +39 -3
- package/scripts/cm-prd-host.test.mjs +52 -0
- package/scripts/cm-prd-review-gate.mjs +11 -2
- package/scripts/cm-prd-review-gate.test.mjs +62 -0
- package/scripts/cm-prd-session-recovery.test.mjs +167 -0
- package/scripts/cm-prd-summary.test.mjs +147 -0
- package/scripts/cm-review-dependency-snapshot.test.mjs +10 -0
- package/scripts/cm-review-inventory.test.mjs +189 -0
- package/scripts/cm-review-schema.test.mjs +35 -1
- package/scripts/cm-review-scope-recovery.test.mjs +63 -0
- package/scripts/cm-runtime-edit.mjs +104 -0
- package/scripts/cm-runtime-failover.test.mjs +130 -0
- package/scripts/cm-runtime-install.mjs +42 -0
- package/scripts/cm-runtime.mjs +113 -0
- package/scripts/cm-runtime.test.mjs +117 -0
- package/scripts/cm-security.mjs +32 -0
- package/scripts/cm-security.test.mjs +203 -0
- package/scripts/cm-spec-manifest.mjs +24 -21
- package/scripts/cm-spec-manifest.test.mjs +19 -0
- package/scripts/cm-workflow-config.mjs +113 -5
- package/scripts/cm-workflow-config.test.mjs +124 -1
- package/scripts/fixtures/prd-review-gate-python-oracle.py +18 -2
- package/scripts/test-cm-prd-review-gate.py +30 -0
- package/scripts/test-shell-compat.sh +34 -0
- package/scripts/validate-public-repo.py +7 -1
- package/skills/cm-ai/references/N4-review.md +5 -4
- package/skills/cm-ai/references/js-host.md +36 -2
- package/skills/cm-check/SKILL.md +49 -8
- package/skills/cm-check/references/js-host.md +4 -1
- package/skills/cm-init/SKILL.md +4 -0
- package/skills/cm-init/references/js-host.md +2 -0
- package/skills/cm-init/references/runtime-declaration.md +26 -0
- package/skills/cm-prd/SKILL.md +3 -3
- package/skills/cm-prd/references/js-change-recovery.md +24 -3
- package/skills/cm-prd/references/summary-card.md +7 -0
- package/skills/cm-runtime/SKILL.md +32 -0
- package/skills/cm-security/SKILL.md +55 -0
- package/skills/cm-security/references/scan-contract.md +43 -0
- package/templates/auto-update/cm-update.sh +1 -1
- package/templates/cm-workflow.yml +16 -0
|
@@ -0,0 +1,203 @@
|
|
|
1
|
+
import test from 'node:test';
|
|
2
|
+
import assert from 'node:assert/strict';
|
|
3
|
+
import fs from 'node:fs';
|
|
4
|
+
import path from 'node:path';
|
|
5
|
+
import os from 'node:os';
|
|
6
|
+
import crypto from 'node:crypto';
|
|
7
|
+
import {fileURLToPath} from 'node:url';
|
|
8
|
+
import {spawnSync,execFileSync} from 'node:child_process';
|
|
9
|
+
import {inventory,scan,normalize} from '../runtime/js/cm-security/scan.mjs';
|
|
10
|
+
import {main} from './cm-security.mjs';
|
|
11
|
+
|
|
12
|
+
const root=path.resolve(path.dirname(fileURLToPath(import.meta.url)),'..');
|
|
13
|
+
const gitBin=execFileSync('/bin/sh',['-c','command -v git'],{encoding:'utf8'}).trim();
|
|
14
|
+
function fixture(t){
|
|
15
|
+
const base=fs.mkdtempSync(path.join(os.tmpdir(),'cm-security-test-'));
|
|
16
|
+
const project=path.join(base,'project with spaces');fs.mkdirSync(project);
|
|
17
|
+
t.after(()=>fs.rmSync(base,{recursive:true,force:true}));
|
|
18
|
+
const git=(...args)=>execFileSync(gitBin,['-c','core.fsmonitor=false','-C',project,...args],{stdio:['ignore','pipe','pipe']}).toString();
|
|
19
|
+
const put=(file,text)=>{fs.mkdirSync(path.dirname(path.join(project,file)),{recursive:true});fs.writeFileSync(path.join(project,file),text);};
|
|
20
|
+
git('init','-b','main');git('config','user.name','Fixture');git('config','user.email','fixture@example.test');
|
|
21
|
+
put('app.py','print("base")\n');git('add','.');git('commit','-m','base');git('switch','-c','feature');
|
|
22
|
+
return {base,project,git,put};
|
|
23
|
+
}
|
|
24
|
+
function isolatedPath(t,f,programs={}){
|
|
25
|
+
const old=process.env.PATH,bin=path.join(f.base,'bin');fs.mkdirSync(bin);
|
|
26
|
+
fs.symlinkSync(gitBin,path.join(bin,'git'));
|
|
27
|
+
for(const [name,body] of Object.entries(programs))fs.writeFileSync(path.join(bin,name),`#!${process.execPath}\n${body}`,{mode:0o700});
|
|
28
|
+
process.env.PATH=bin;t.after(()=>{process.env.PATH=old;});
|
|
29
|
+
}
|
|
30
|
+
const fakeLeak=(body='')=>`
|
|
31
|
+
const fs=require('fs'),path=require('path'),args=process.argv.slice(2);
|
|
32
|
+
if(args.includes('--version')){console.log('9.0.0');process.exit(0);}
|
|
33
|
+
${body}
|
|
34
|
+
`;
|
|
35
|
+
|
|
36
|
+
test('scope includes branch commits, working/index differences, deletes and renames; excludes untracked',t=>{
|
|
37
|
+
const f=fixture(t);f.put('app.py','print("feature")\n');f.put('renamed.py','ok\n');f.git('add','.');f.git('commit','-m','change');
|
|
38
|
+
f.git('mv','renamed.py','new name.py');f.put('app.py','print("staged")\n');f.git('add','app.py');f.put('app.py','print("worktree")\n');
|
|
39
|
+
f.put('untracked-private.txt','DO NOT READ');
|
|
40
|
+
const result=inventory(f.project);
|
|
41
|
+
assert.deepEqual(result.selected,['app.py','new name.py','renamed.py']);
|
|
42
|
+
assert.equal(result.untracked,1);assert.ok(!result.files.some(v=>v.path==='untracked-private.txt'));
|
|
43
|
+
assert.deepEqual(result.files.filter(v=>v.path==='app.py').map(v=>v.revision),['worktree','index']);
|
|
44
|
+
assert.ok(result.gaps.some(v=>v.path==='renamed.py'&&v.reason==='deleted_review_baseline'));
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
test('full scope works without main; ambiguous/missing branch base blocks default',t=>{
|
|
48
|
+
const f=fixture(t);f.git('branch','master');assert.throws(()=>inventory(f.project),/main_ambiguous/);
|
|
49
|
+
assert.equal(inventory(f.project,{all:true}).files.length,1);
|
|
50
|
+
f.git('branch','-D','main','master');assert.throws(()=>inventory(f.project),/main_missing/);
|
|
51
|
+
assert.equal(inventory(f.project,{all:true}).files.length,1);
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
test('no change is distinct from partial and missing tools do not pass',t=>{
|
|
55
|
+
const f=fixture(t);isolatedPath(t,f);
|
|
56
|
+
assert.equal(scan(f.project).result,'NO_CHANGES');f.put('app.py','changed\n');
|
|
57
|
+
const r=scan(f.project);assert.equal(r.coverage,'PARTIAL');assert.equal(r.sourceUnchanged,true);
|
|
58
|
+
assert.ok(r.tools.every(v=>v.reason==='tool_missing'));assert.equal(r.aiReview,'pending');
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
test('symlink ancestor, protected files and scanner controls are visible gaps',t=>{
|
|
62
|
+
const f=fixture(t);f.put('dir/file.py','ok');f.put('.env','private fixture');f.put('.gitleaks.toml','malicious rule');f.git('add','.');
|
|
63
|
+
fs.renameSync(path.join(f.project,'dir'),path.join(f.base,'outside'));
|
|
64
|
+
fs.symlinkSync(path.join(f.base,'outside'),path.join(f.project,'dir'));
|
|
65
|
+
const r=inventory(f.project,{all:true});
|
|
66
|
+
assert.ok(r.gaps.some(v=>v.path==='dir/file.py'&&v.reason==='symlink'));
|
|
67
|
+
assert.ok(r.gaps.some(v=>v.path==='.env'&&v.reason==='protected_path_not_read'));
|
|
68
|
+
assert.ok(r.gaps.some(v=>v.reason==='scanner_control_excluded_review_manually'));
|
|
69
|
+
assert.ok(!r.files.some(v=>v.path==='.env'||v.path==='.gitleaks.toml'));
|
|
70
|
+
assert.ok(!r.files.some(v=>v.path==='dir/file.py'&&v.revision==='worktree'));
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
test('source/config limits and malformed CLI are blocked',t=>{
|
|
74
|
+
const f=fixture(t);f.put('large.py',Buffer.alloc(1024*1024+1));f.git('add','.');
|
|
75
|
+
assert.ok(inventory(f.project).gaps.some(v=>v.reason==='oversize'));
|
|
76
|
+
assert.throws(()=>main(['--all','--all']),/duplicate/);
|
|
77
|
+
assert.throws(()=>main(['--project']),/required/);
|
|
78
|
+
assert.throws(()=>main(['--untrusted-shell-command']),/unknown/);
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
test('Git fsmonitor configuration never executes during main-ref or scope reads',t=>{
|
|
82
|
+
const f=fixture(t),marker=path.join(f.base,'called');
|
|
83
|
+
const monitor=path.join(f.base,'monitor');fs.writeFileSync(monitor,`#!/bin/sh\ntouch '${marker}'\n`,{mode:0o700});
|
|
84
|
+
f.git('config','core.fsmonitor',monitor);inventory(f.project);assert.ok(!fs.existsSync(marker));
|
|
85
|
+
});
|
|
86
|
+
|
|
87
|
+
test('normalization strips secrets and rejects foreign file/line references',()=>{
|
|
88
|
+
const files=[{path:'a.py',revision:'worktree',bytes:Buffer.from('a\nb\n')}];const base='/snapshot';
|
|
89
|
+
const r=normalize('gitleaks',[{File:'worktree/a.py',StartLine:1,RuleID:'generic-api-key',Secret:'DO_NOT_OUTPUT',Match:'DO_NOT_OUTPUT'}],files,base);
|
|
90
|
+
assert.equal(r.findings.length,1);assert.ok(!JSON.stringify(r).includes('DO_NOT_OUTPUT'));
|
|
91
|
+
for(const [File,StartLine] of [['../../outside',1],['worktree/a.py',99]]){
|
|
92
|
+
assert.throws(()=>normalize('gitleaks',[{File,StartLine,RuleID:'rule'}],files,base),/location/);
|
|
93
|
+
}
|
|
94
|
+
assert.throws(()=>normalize('semgrep',{results:[],errors:[{}],paths:{scanned:[]}},files,base),/errors/);
|
|
95
|
+
const s=normalize('semgrep',{results:[],errors:[],paths:{scanned:[]}},files,base);assert.equal(s.unscanned.length,1);
|
|
96
|
+
const o=normalize('osv',{results:[{source:{path:'worktree/a.py'},packages:[{vulnerabilities:[{id:'CVE-2099-1234',details:'DO_NOT_OUTPUT'}]}]}]},files,base);
|
|
97
|
+
assert.equal(o.findings[0].rule,'CVE-2099-1234');assert.ok(!JSON.stringify(o).includes('DO_NOT_OUTPUT'));
|
|
98
|
+
});
|
|
99
|
+
|
|
100
|
+
test('adapter clears secrets/config environment and returns safe findings',t=>{
|
|
101
|
+
const f=fixture(t);f.put('app.py','new\n');
|
|
102
|
+
const prior=process.env.GITLEAKS_CONFIG,priorSecret=process.env.CM_TEST_PRIVATE_VALUE;
|
|
103
|
+
process.env.GITLEAKS_CONFIG='malicious';process.env.CM_TEST_PRIVATE_VALUE='private';
|
|
104
|
+
t.after(()=>{if(prior===undefined)delete process.env.GITLEAKS_CONFIG;else process.env.GITLEAKS_CONFIG=prior;
|
|
105
|
+
if(priorSecret===undefined)delete process.env.CM_TEST_PRIVATE_VALUE;else process.env.CM_TEST_PRIVATE_VALUE=priorSecret;});
|
|
106
|
+
isolatedPath(t,f,{gitleaks:fakeLeak(`
|
|
107
|
+
if(process.env.GITLEAKS_CONFIG||process.env.CM_TEST_PRIVATE_VALUE)process.exit(8);
|
|
108
|
+
if(!args.includes('--redact=100')||!args.includes('--ignore-gitleaks-allow'))process.exit(9);
|
|
109
|
+
fs.writeFileSync(args[args.indexOf('--report-path')+1],JSON.stringify([{File:'worktree/app.py',StartLine:1,RuleID:'fixture-rule',Secret:'PRIVATE_VALUE'}]));
|
|
110
|
+
console.error('PRIVATE_VALUE');process.exit(1);`)});
|
|
111
|
+
const r=scan(f.project);assert.equal(r.result,'FINDINGS');assert.equal(r.findings.length,1);
|
|
112
|
+
assert.equal(r.sourceUnchanged,true);assert.ok(!JSON.stringify(r).includes('PRIVATE_VALUE'));
|
|
113
|
+
});
|
|
114
|
+
|
|
115
|
+
for(const [name,body,reason] of [
|
|
116
|
+
['malformed',`fs.writeFileSync(args[args.indexOf('--report-path')+1],'{bad');`,'invalid_or_incomplete_report'],
|
|
117
|
+
['empty-nonzero',`fs.writeFileSync(args[args.indexOf('--report-path')+1],'[]');process.exit(1);`,'invalid_or_incomplete_report'],
|
|
118
|
+
['error',`console.error('sensitive-error');process.exit(2);`,'execution_failed'],
|
|
119
|
+
['timeout',`while(true){}`,'timeout'],
|
|
120
|
+
])test(`scanner ${name} cannot become a clean result`,t=>{
|
|
121
|
+
const f=fixture(t);f.put('app.py','new');isolatedPath(t,f,{gitleaks:fakeLeak(body)});
|
|
122
|
+
const r=scan(f.project,{timeoutMs:name==='timeout'?100:5000});
|
|
123
|
+
assert.equal(r.tools[0].status,'ERROR');assert.equal(r.tools[0].reason,reason);
|
|
124
|
+
assert.equal(r.coverage,'PARTIAL');assert.ok(!JSON.stringify(r).includes('sensitive-error'));
|
|
125
|
+
});
|
|
126
|
+
|
|
127
|
+
test('source mutation by a faulty external scanner blocks the result',t=>{
|
|
128
|
+
const f=fixture(t);f.put('app.py','new');
|
|
129
|
+
isolatedPath(t,f,{gitleaks:fakeLeak(`fs.writeFileSync(${JSON.stringify(path.join(f.project,'app.py'))},'changed again');fs.writeFileSync(args[args.indexOf('--report-path')+1],'[]');`)});
|
|
130
|
+
const r=scan(f.project);assert.equal(r.result,'BLOCKED');assert.equal(r.sourceUnchanged,false);
|
|
131
|
+
});
|
|
132
|
+
|
|
133
|
+
test('optional scanners require explicit local inputs and OSV remains offline',t=>{
|
|
134
|
+
const f=fixture(t);f.put('package-lock.json','{}\n');f.git('add','.');
|
|
135
|
+
const rules=path.join(f.base,'rules.yaml');fs.writeFileSync(rules,'rules: []');const db=path.join(f.base,'db');fs.mkdirSync(db);
|
|
136
|
+
isolatedPath(t,f,{
|
|
137
|
+
semgrep:fakeLeak(`if(!args.includes('--metrics=off')||!args.includes('--disable-nosem'))process.exit(8);console.log(JSON.stringify({results:[],errors:[],paths:{scanned:[]}}));`),
|
|
138
|
+
'osv-scanner':fakeLeak(`if(!args.includes('--offline')||!process.env.OSV_SCANNER_LOCAL_DB_CACHE_DIRECTORY)process.exit(8);console.log(JSON.stringify({results:[]}));`),
|
|
139
|
+
});
|
|
140
|
+
let r=scan(f.project);assert.equal(r.tools[1].reason,'local_rules_required');assert.equal(r.tools[2].reason,'offline_database_required');
|
|
141
|
+
r=scan(f.project,{semgrepRules:rules,osvDb:db});assert.equal(r.tools[1].reason,'unscanned_files');
|
|
142
|
+
assert.equal(r.tools[2].reason,'offline_database_freshness_and_ecosystem_coverage_unverified');
|
|
143
|
+
f.put('rules.yaml','rules: []');assert.throws(()=>scan(f.project,{semgrepRules:path.join(f.project,'rules.yaml')}),/external/);
|
|
144
|
+
});
|
|
145
|
+
|
|
146
|
+
test('installed Gitleaks smoke: synthetic key, negative control, source unchanged, no raw secret',t=>{
|
|
147
|
+
const probe=spawnSync('gitleaks',['version'],{encoding:'utf8'});
|
|
148
|
+
if(probe.error||probe.status!==0){t.skip('Gitleaks not installed; adapter fixtures still run');return;}
|
|
149
|
+
const f=fixture(t),secret='gh'+'p_'+crypto.createHash('sha256').update('cm-security-inert-example').digest('hex').slice(0,36);
|
|
150
|
+
f.put('fixture.txt',`value = "${secret}"\n`);f.put('safe.py','import os\nvalue = os.getenv("ACCESS_TOKEN")\n');f.git('add','.');
|
|
151
|
+
const r=scan(f.project);
|
|
152
|
+
assert.equal(r.tools[0].status,'FINDINGS');assert.ok(r.findings.some(v=>v.path==='fixture.txt'));
|
|
153
|
+
assert.ok(!r.findings.some(v=>v.path==='safe.py'));assert.ok(!JSON.stringify(r).includes(secret));assert.equal(r.sourceUnchanged,true);
|
|
154
|
+
});
|
|
155
|
+
|
|
156
|
+
test('CLI returns a machine-readable blocked result without echoing bad arguments',()=>{
|
|
157
|
+
const r=spawnSync(process.execPath,[path.join(root,'scripts/cm-security.mjs'),'--arbitrary-sensitive-value'],{encoding:'utf8'});
|
|
158
|
+
assert.equal(r.status,2);assert.equal(JSON.parse(r.stdout).result,'BLOCKED');assert.ok(!r.stdout.includes('sensitive-value'));
|
|
159
|
+
});
|
|
160
|
+
|
|
161
|
+
test('clean/process filters cannot execute and required filters are disabled',t=>{
|
|
162
|
+
const f=fixture(t),marker=path.join(f.base,'filter-called');
|
|
163
|
+
const driver=path.join(f.base,'driver');fs.writeFileSync(driver,`#!/bin/sh\ntouch '${marker}'\ncat\n`,{mode:0o700});
|
|
164
|
+
f.put('.gitattributes','app.py filter=probe\n');f.git('add','.');f.git('commit','-m','attributes');
|
|
165
|
+
f.put('app.py','modified raw content\n');
|
|
166
|
+
f.git('config','filter.probe.clean',driver);f.git('config','filter.probe.required','true');
|
|
167
|
+
assert.ok(inventory(f.project).selected.includes('app.py'));assert.ok(!fs.existsSync(marker));
|
|
168
|
+
f.git('config','filter.probe.process',driver);
|
|
169
|
+
assert.ok(inventory(f.project).selected.includes('app.py'));assert.ok(!fs.existsSync(marker));
|
|
170
|
+
});
|
|
171
|
+
|
|
172
|
+
test('Git lookup skips project binaries and child PATH excludes project directories',t=>{
|
|
173
|
+
const f=fixture(t),marker=path.join(f.base,'git-called');
|
|
174
|
+
f.put('bin/git',`#!/bin/sh\ntouch '${marker}'\nexec '${gitBin}' "$@"\n`);fs.chmodSync(path.join(f.project,'bin/git'),0o700);
|
|
175
|
+
const old=process.env.PATH;process.env.PATH=path.join(f.project,'bin')+path.delimiter+old;t.after(()=>{process.env.PATH=old;});
|
|
176
|
+
inventory(f.project);assert.ok(!fs.existsSync(marker));
|
|
177
|
+
fs.mkdirSync(path.join(f.project,'nested'));
|
|
178
|
+
assert.throws(()=>inventory(path.join(f.project,'nested')),/root_required/);
|
|
179
|
+
assert.ok(!fs.existsSync(marker));
|
|
180
|
+
});
|
|
181
|
+
|
|
182
|
+
test('untracked and ignored business maps are never read or hashed',t=>{
|
|
183
|
+
const f=fixture(t);f.put('docs/architecture.md','private map');
|
|
184
|
+
let r=inventory(f.project,{all:true});assert.equal(r.mapEvidence[0].sha256,null);
|
|
185
|
+
assert.equal(r.mapEvidence[0].status,'untracked_or_nonregular_excluded');
|
|
186
|
+
f.put('.gitignore','docs/architecture.md\n');r=inventory(f.project,{all:true});assert.equal(r.mapEvidence[0].sha256,null);
|
|
187
|
+
});
|
|
188
|
+
|
|
189
|
+
test('deleted tracked paths cannot pull same-name untracked private content into a scan',t=>{
|
|
190
|
+
const f=fixture(t);f.git('rm','--cached','app.py');f.put('app.py','NEW_UNTRACKED_PRIVATE');
|
|
191
|
+
let r=inventory(f.project);assert.equal(r.untracked,1);assert.ok(!r.files.some(v=>v.path==='app.py'));
|
|
192
|
+
assert.ok(r.gaps.some(v=>v.path==='app.py'&&v.reason==='deleted_review_baseline'));
|
|
193
|
+
f.git('commit','-m','remove');r=inventory(f.project);assert.ok(!r.files.some(v=>v.path==='app.py'));
|
|
194
|
+
assert.ok(!JSON.stringify(r).includes('NEW_UNTRACKED_PRIVATE'));
|
|
195
|
+
});
|
|
196
|
+
|
|
197
|
+
test('empty initial scope never overrides concurrent source drift with NO_CHANGES',t=>{
|
|
198
|
+
const f=fixture(t);isolatedPath(t,f);
|
|
199
|
+
const original=fs.mkdtempSync;
|
|
200
|
+
fs.mkdtempSync=(...args)=>{const result=original(...args);f.put('app.py','changed during scan');return result;};
|
|
201
|
+
try{const r=scan(f.project);assert.equal(r.sourceUnchanged,false);assert.equal(r.result,'BLOCKED');}
|
|
202
|
+
finally{fs.mkdtempSync=original;}
|
|
203
|
+
});
|
|
@@ -15,28 +15,31 @@ const record=v=>v!==null&&typeof v==='object'&&!Array.isArray(v);
|
|
|
15
15
|
const compare=(a,b)=>Buffer.compare(Buffer.from(a),Buffer.from(b));
|
|
16
16
|
const exists=p=>{try{fs.statSync(p);return true;}catch(e){if(['ENOENT','ENOTDIR'].includes(e.code))return false;throw e;}};
|
|
17
17
|
|
|
18
|
+
// Only actual task/AC declarations are runtime state; other files and code stay exact.
|
|
19
|
+
export function normalizeRuntimeMarks(text,name){
|
|
20
|
+
if(!['tasks.md','requirements.md'].includes(name))return text;
|
|
21
|
+
const marker=name==='tasks.md'?/^([ ]{0,3}-[ \t]+)\[[xX]\]([ \t]+T-[A-Za-z0-9][A-Za-z0-9._-]*[ \t]*:)/
|
|
22
|
+
:/^([ ]{0,3}-[ \t]+)\[[xX]\]([ \t]+\[AC-[A-Za-z0-9][A-Za-z0-9._-]*\])/;
|
|
23
|
+
let fenceChar=null,width=0;
|
|
24
|
+
const lines=text.match(/[^\r\n]*(?:\r\n|\r|\n|$)/g)??[];
|
|
25
|
+
return lines.map(line=>{
|
|
26
|
+
const fence=/^[ ]{0,3}(`{3,}|~{3,})/.exec(line);
|
|
27
|
+
if(fenceChar!==null){
|
|
28
|
+
if(fence&&fence[1][0]===fenceChar&&fence[1].length>=width
|
|
29
|
+
&&/^[\t\n\v\f\r ]*$/.test(line.slice(fence[0].length))){fenceChar=null;width=0;}
|
|
30
|
+
return line;
|
|
31
|
+
}
|
|
32
|
+
if(fence){fenceChar=fence[1][0];width=fence[1].length;return line;}
|
|
33
|
+
if(line.startsWith(' ')||line.startsWith('\t'))return line;
|
|
34
|
+
return line.replace(marker,'$1[ ]$2');
|
|
35
|
+
}).join('');
|
|
36
|
+
}
|
|
37
|
+
|
|
18
38
|
export function semanticDigest(file){
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
:/^([ ]{0,3}-[ \t]+)\[[xX]\]([ \t]+\[AC-[A-Za-z0-9][A-Za-z0-9._-]*\])/;
|
|
24
|
-
let fenceChar=null,width=0;
|
|
25
|
-
// Latin-1 is a reversible byte view, including invalid UTF-8 and CRLF.
|
|
26
|
-
const lines=bytes.toString('latin1').match(/[^\r\n]*(?:\r\n|\r|\n|$)/g)??[];
|
|
27
|
-
bytes=Buffer.from(lines.map(line=>{
|
|
28
|
-
const fence=/^[ ]{0,3}(`{3,}|~{3,})/.exec(line);
|
|
29
|
-
if(fenceChar!==null){
|
|
30
|
-
if(fence&&fence[1][0]===fenceChar&&fence[1].length>=width
|
|
31
|
-
&&/^[\t\n\v\f\r ]*$/.test(line.slice(fence[0].length))){fenceChar=null;width=0;}
|
|
32
|
-
return line;
|
|
33
|
-
}
|
|
34
|
-
if(fence){fenceChar=fence[1][0];width=fence[1].length;return line;}
|
|
35
|
-
if(line.startsWith(' ')||line.startsWith('\t'))return line;
|
|
36
|
-
return line.replace(marker,'$1[ ]$2');
|
|
37
|
-
}).join(''),'latin1');
|
|
38
|
-
}
|
|
39
|
-
return createHash('sha256').update(bytes).digest('hex');
|
|
39
|
+
const bytes=fs.readFileSync(file);
|
|
40
|
+
// Latin-1 is a reversible byte view, including invalid UTF-8 and CRLF.
|
|
41
|
+
const normalized=Buffer.from(normalizeRuntimeMarks(bytes.toString('latin1'),path.basename(file)),'latin1');
|
|
42
|
+
return createHash('sha256').update(normalized).digest('hex');
|
|
40
43
|
}
|
|
41
44
|
|
|
42
45
|
export function buildManifest(specsDir){
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import test from 'node:test';
|
|
2
|
+
import assert from 'node:assert/strict';
|
|
3
|
+
import {normalizeRuntimeMarks} from './cm-spec-manifest.mjs';
|
|
4
|
+
test('shared normalization preserves declaration, filename, fence and byte boundaries',()=>{
|
|
5
|
+
for(const [name,declaration,other] of [['tasks.md','T-A.1:','[AC-001]'],['requirements.md','[AC-A.1]','T-001:']]){
|
|
6
|
+
const lines=[`- [x] ${declaration} 中文`,` -\t[X]\t${declaration} [CHANGED]`,
|
|
7
|
+
`- [DROPPED] ${declaration} old`,`- [CHANGED] ${declaration} changed`,
|
|
8
|
+
`text - [x] ${declaration} prose`,`- [x] ${other} wrong file`,'- [x] ordinary',
|
|
9
|
+
` - [x] ${declaration} code`,`\t- [x] ${declaration} code`,
|
|
10
|
+
'```md',`- [x] ${declaration} fenced`,'```',
|
|
11
|
+
'~~~~',`- [X] ${declaration} fenced`,'~~~~',`- [X] ${declaration} final`];
|
|
12
|
+
for(const ending of ['\n','\r\n','\r']){
|
|
13
|
+
const text=lines.join(ending),expected=[...lines];
|
|
14
|
+
for(const i of [0,1,15])expected[i]=expected[i].replace(/\[[xX]\]/,'[ ]');
|
|
15
|
+
assert.equal(normalizeRuntimeMarks(text,name),expected.join(ending));
|
|
16
|
+
for(const otherName of ['design.md','test-cases.json','notes.md'])assert.equal(normalizeRuntimeMarks(text,otherName),text);
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
});
|
|
@@ -7,6 +7,9 @@ import {fileURLToPath} from 'node:url';
|
|
|
7
7
|
|
|
8
8
|
export const CONFIG_FILENAMES=Object.freeze(['.cm-workflow.yml','.cm-workflow.yaml','.cm-workflow.json']);
|
|
9
9
|
export const MAX_CONFIG_BYTES=64*1024;
|
|
10
|
+
// Out-of-band provenance keeps persisted config/role JSON contracts unchanged.
|
|
11
|
+
const RUNTIME_SOURCES=new WeakMap();
|
|
12
|
+
export const runtimesSource=config=>RUNTIME_SOURCES.get(config)??'none';
|
|
10
13
|
|
|
11
14
|
const PROJECT_TYPES=new Set(['auto','java-backend','web-frontend','custom']);
|
|
12
15
|
const WORKFLOWS=new Set(['cm-default','java-backend','web-frontend']);
|
|
@@ -22,6 +25,10 @@ const MODEL_POLICY_ALIASES=Object.freeze({'strict-Pro':'strict-pro'});
|
|
|
22
25
|
const AUTO_FIX_POLICIES=new Set(['explicit','never','auto']);
|
|
23
26
|
const DELIVERY_MODES=new Set(['diff','branch','draft-mr']);
|
|
24
27
|
const RUNTIMES=new Set(['codex','claude','unknown']);
|
|
28
|
+
// Declared runtime availability. `unknown` is only the default for configs that
|
|
29
|
+
// never declared it; raw configs must pick codex, claude or both.
|
|
30
|
+
const DECLARABLE_RUNTIMES=new Set(['codex','claude','both']);
|
|
31
|
+
const CLI_ADAPTER_RUNTIME=Object.freeze({'codex-cli':'codex','claude-cli':'claude','claude-api':'claude'});
|
|
25
32
|
const SECRET_KEY=/(?:api[_-]?key|access[_-]?token|secret|cookie|password|private[_-]?key|credential|authorization)/i;
|
|
26
33
|
const SECRET_VALUE=/(?:-----BEGIN [^-]+ PRIVATE KEY-----|\b(?:AKIA|ASIA)[0-9A-Z]{16}\b|\bAIza[0-9A-Za-z_-]{20,}\b|\bsk[-_](?:ant-api\d{2}[-_])?[A-Za-z0-9_-]{12,}\b|\b(?:ghp|gho|ghu|ghs|ghr|github_pat)[-_][A-Za-z0-9_-]{8,}\b|\bxox[baprs][-_][A-Za-z0-9_-]{8,}\b|\beyJ[A-Za-z0-9_-]{10,}\.[A-Za-z0-9_-]{10,}\.[A-Za-z0-9_-]{10,}\b)/i;
|
|
27
34
|
const IDENTIFIER=/^[A-Za-z0-9][A-Za-z0-9._:/+@-]{0,127}$/;
|
|
@@ -33,6 +40,7 @@ export class ConfigError extends Error {
|
|
|
33
40
|
const rawDefault={
|
|
34
41
|
version:1,
|
|
35
42
|
project:{type:'auto',workflow:'cm-default'},
|
|
43
|
+
runtimes:{available:'unknown'},
|
|
36
44
|
roles:{
|
|
37
45
|
analyst:{adapter:'current-ai',model:'default',source:'local'},
|
|
38
46
|
planner:{adapter:'current-ai',model:'default',source:'local'},
|
|
@@ -276,13 +284,17 @@ function requireString(value,location,choices=null){
|
|
|
276
284
|
}
|
|
277
285
|
|
|
278
286
|
function validateRaw(value){
|
|
279
|
-
const root=requireMapping(value,'config');rejectUnknown(root,new Set(['version','project','roles','policies']),'config');
|
|
287
|
+
const root=requireMapping(value,'config');rejectUnknown(root,new Set(['version','project','runtimes','roles','policies']),'config');
|
|
280
288
|
if(typeof root.version!=='bigint'||root.version!==1n)throw new ConfigError('config.version must equal integer 1');
|
|
281
289
|
if(Object.hasOwn(root,'project')){
|
|
282
290
|
const project=requireMapping(root.project,'config.project');rejectUnknown(project,new Set(['type','workflow']),'config.project');
|
|
283
291
|
if(Object.hasOwn(project,'type'))requireString(project.type,'config.project.type',PROJECT_TYPES);
|
|
284
292
|
if(Object.hasOwn(project,'workflow'))requireString(project.workflow,'config.project.workflow',WORKFLOWS);
|
|
285
293
|
}
|
|
294
|
+
if(Object.hasOwn(root,'runtimes')){
|
|
295
|
+
const runtimes=requireMapping(root.runtimes,'config.runtimes');rejectUnknown(runtimes,new Set(['available']),'config.runtimes');
|
|
296
|
+
if(Object.hasOwn(runtimes,'available'))requireString(runtimes.available,'config.runtimes.available',DECLARABLE_RUNTIMES);
|
|
297
|
+
}
|
|
286
298
|
if(Object.hasOwn(root,'roles')){
|
|
287
299
|
const roles=requireMapping(root.roles,'config.roles');rejectUnknown(roles,ROLE_NAMES,'config.roles');
|
|
288
300
|
for(const [role,rawRole] of Object.entries(roles)){
|
|
@@ -330,6 +342,7 @@ function validateEffective(config){
|
|
|
330
342
|
requireString(roleData.model_policy,'roles.external_expert.model_policy',MODEL_POLICIES);
|
|
331
343
|
}
|
|
332
344
|
}
|
|
345
|
+
validateRuntimeDeclaration(config);
|
|
333
346
|
if(!Array.isArray(config.policies.tests)||config.policies.tests.length===0)throw new ConfigError('policies.tests must be a non-empty list');
|
|
334
347
|
if(config.policies.tests.some(item=>typeof item!=='string'||!TEST_KINDS.has(item)))throw new ConfigError('policies.tests contains an unsupported test kind');
|
|
335
348
|
if(typeof config.policies.generate_cases!=='boolean')throw new ConfigError('policies.generate_cases must be boolean');
|
|
@@ -337,6 +350,47 @@ function validateEffective(config){
|
|
|
337
350
|
requireString(config.policies.delivery,'policies.delivery',DELIVERY_MODES);
|
|
338
351
|
}
|
|
339
352
|
|
|
353
|
+
// A declared availability only constrains roles that name a concrete runtime.
|
|
354
|
+
// `current-ai` means "whatever tool I am sitting in" and is never judged here,
|
|
355
|
+
// so single-runtime users with default roles stay valid. This validates the
|
|
356
|
+
// declaration; it does not dispatch across runtimes (see workflow-routing.md).
|
|
357
|
+
function validateRuntimeDeclaration(config){
|
|
358
|
+
const available=config.runtimes?.available;
|
|
359
|
+
if(available===undefined||available==='unknown')return;
|
|
360
|
+
requireString(available,'runtimes.available',DECLARABLE_RUNTIMES);
|
|
361
|
+
for(const [role,roleData] of Object.entries(config.roles)){
|
|
362
|
+
const runtime=CLI_ADAPTER_RUNTIME[roleData.adapter];
|
|
363
|
+
if(runtime&&available!=='both'&&runtime!==available)
|
|
364
|
+
throw new ConfigError(`roles.${role}.adapter ${roleData.adapter} needs the ${runtime} runtime, but runtimes.available declares ${available}`);
|
|
365
|
+
}
|
|
366
|
+
const coder=CLI_ADAPTER_RUNTIME[config.roles.coder.adapter],reviewer=CLI_ADAPTER_RUNTIME[config.roles.reviewer.adapter];
|
|
367
|
+
if(available==='both'&&coder&&reviewer&&coder===reviewer)
|
|
368
|
+
throw new ConfigError(`roles.coder and roles.reviewer both resolve to ${coder}; with runtimes.available both, review must run on the other runtime`);
|
|
369
|
+
}
|
|
370
|
+
|
|
371
|
+
// Runtimes a caller may start a role on, derived from the declaration. Unknown
|
|
372
|
+
// declarations permit both so undeclared projects keep today's behavior.
|
|
373
|
+
export function declaredRuntimes(config){
|
|
374
|
+
const available=config?.runtimes?.available??'unknown';
|
|
375
|
+
if(available==='codex'||available==='claude')return Object.freeze({available,allowed:Object.freeze([available])});
|
|
376
|
+
return Object.freeze({available,allowed:Object.freeze(['codex','claude'])});
|
|
377
|
+
}
|
|
378
|
+
export function runtimeForAdapter(adapter){return CLI_ADAPTER_RUNTIME[adapter]??null;}
|
|
379
|
+
|
|
380
|
+
// Pure selection only. The host records cli-dispatch after process creation.
|
|
381
|
+
export function resolveProtectedRuntimes(config,runtime){
|
|
382
|
+
if(!['codex','claude'].includes(runtime))throw new ConfigError('invalid_runtime');
|
|
383
|
+
const declared=declaredRuntimes(config);
|
|
384
|
+
const coderRuntime=runtimeForAdapter(config.roles.coder.adapter)??runtime;
|
|
385
|
+
const reviewerRuntime=runtimeForAdapter(config.roles.reviewer.adapter)
|
|
386
|
+
??(declared.allowed.length===1?declared.allowed[0]:coderRuntime==='codex'?'claude':'codex');
|
|
387
|
+
if(!declared.allowed.includes(coderRuntime)||!declared.allowed.includes(reviewerRuntime))
|
|
388
|
+
throw new ConfigError('runtime_not_declared');
|
|
389
|
+
if(declared.available==='both'&&coderRuntime===reviewerRuntime)
|
|
390
|
+
throw new ConfigError('cross_runtime_review_required');
|
|
391
|
+
return {coderRuntime,reviewerRuntime};
|
|
392
|
+
}
|
|
393
|
+
|
|
340
394
|
function expandUser(raw){
|
|
341
395
|
if(raw==='~')return os.homedir();
|
|
342
396
|
if(raw.startsWith(`~${path.sep}`))return path.join(os.homedir(),raw.slice(2));
|
|
@@ -356,13 +410,62 @@ export function findConfig(projectRoot){
|
|
|
356
410
|
return candidates[0]??null;
|
|
357
411
|
}
|
|
358
412
|
|
|
413
|
+
// One preset contract for installers, the runtime tool and user defaults.
|
|
414
|
+
export const RUNTIME_PRESETS=deepFreeze({
|
|
415
|
+
'codex-only':{available:'codex',coder:'codex-cli',reviewer:'codex-cli'},
|
|
416
|
+
'claude-only':{available:'claude',coder:'claude-cli',reviewer:'claude-cli'},
|
|
417
|
+
'codex-codes':{available:'both',coder:'codex-cli',reviewer:'claude-cli'},
|
|
418
|
+
'claude-codes':{available:'both',coder:'claude-cli',reviewer:'codex-cli'},
|
|
419
|
+
});
|
|
420
|
+
export function runtimePreset(preset){
|
|
421
|
+
if(!Object.hasOwn(RUNTIME_PRESETS,preset))throw new ConfigError('preset must be one of: '+Object.keys(RUNTIME_PRESETS).join(', '));
|
|
422
|
+
const value=RUNTIME_PRESETS[preset];
|
|
423
|
+
return {runtimes:{available:value.available},roles:{
|
|
424
|
+
coder:{adapter:value.coder,source:'subscription'},reviewer:{adapter:value.reviewer,source:'subscription'},
|
|
425
|
+
}};
|
|
426
|
+
}
|
|
427
|
+
export function userRuntimesPath(){
|
|
428
|
+
return path.join(path.resolve(expandUser(process.env.CM_WORKFLOW_HOME||path.join(os.homedir(),'.cm-workflow'))),'runtimes.yml');
|
|
429
|
+
}
|
|
430
|
+
export function readConfigText(file){
|
|
431
|
+
try{
|
|
432
|
+
const stat=fs.statSync(file);
|
|
433
|
+
if(!stat.isFile())throw new ConfigError('configuration must be a regular file');
|
|
434
|
+
if(stat.size>MAX_CONFIG_BYTES)throw new ConfigError(`configuration exceeds ${MAX_CONFIG_BYTES} bytes`);
|
|
435
|
+
return new TextDecoder('utf-8',{fatal:true,ignoreBOM:true}).decode(fs.readFileSync(file));
|
|
436
|
+
}catch(error){throw new ConfigError(`${file}: ${error.message}`);}
|
|
437
|
+
}
|
|
438
|
+
export function parseUserRuntimes(text){
|
|
439
|
+
try{
|
|
440
|
+
if(Buffer.byteLength(text)>MAX_CONFIG_BYTES)throw new ConfigError('file exceeds configuration size limit');
|
|
441
|
+
if(SECRET_VALUE.test(text))throw new ConfigError('secret-like value is forbidden');
|
|
442
|
+
const raw=requireMapping(parseDocument('runtimes.yml',text),'user');
|
|
443
|
+
walkForSecrets(raw,'user');rejectUnknown(raw,new Set(['runtimes','preset']),'user');
|
|
444
|
+
const runtimes=requireMapping(raw.runtimes,'user.runtimes');
|
|
445
|
+
rejectUnknown(runtimes,new Set(['available']),'user.runtimes');
|
|
446
|
+
requireString(runtimes.available,'user.runtimes.available',DECLARABLE_RUNTIMES);
|
|
447
|
+
requireString(raw.preset,'user.preset',new Set(Object.keys(RUNTIME_PRESETS)));
|
|
448
|
+
if(runtimePreset(raw.preset).runtimes.available!==runtimes.available)
|
|
449
|
+
throw new ConfigError('user.runtimes.available conflicts with user.preset');
|
|
450
|
+
return raw;
|
|
451
|
+
}catch(error){throw new ConfigError(`runtimes.yml: ${error.message}`);}
|
|
452
|
+
}
|
|
453
|
+
export function loadUserRuntimes(){
|
|
454
|
+
const file=userRuntimesPath();
|
|
455
|
+
try{fs.lstatSync(file);}catch(error){
|
|
456
|
+
if(error.code==='ENOENT'||error.code==='ENOTDIR')return null;
|
|
457
|
+
throw new ConfigError(`user.runtimes: ${error.message}`);
|
|
458
|
+
}
|
|
459
|
+
return parseUserRuntimes(readConfigText(file));
|
|
460
|
+
}
|
|
461
|
+
|
|
359
462
|
export function loadConfig(input){
|
|
360
463
|
if(!object(input)||!Object.hasOwn(input,'projectRoot')
|
|
361
464
|
||Object.keys(input).some(key=>!['projectRoot','configPath','text'].includes(key)))throw new ConfigError('invalid config input');
|
|
362
465
|
const root=realDirectory(input.projectRoot,'project root');
|
|
363
466
|
let configPath=Object.hasOwn(input,'configPath')?path.resolve(expandUser(input.configPath)):findConfig(root);
|
|
364
467
|
let text=Object.hasOwn(input,'text')?input.text:null;
|
|
365
|
-
if(text===null&&configPath===null)
|
|
468
|
+
if(text===null&&configPath===null){text='version: 1';configPath='.cm-workflow.yml';}
|
|
366
469
|
if(text===null){
|
|
367
470
|
try{
|
|
368
471
|
const stat=fs.statSync(configPath);if(!stat.isFile())throw new Error('not-file');
|
|
@@ -371,13 +474,18 @@ export function loadConfig(input){
|
|
|
371
474
|
}catch(error){if(error instanceof ConfigError)throw error;throw new ConfigError(`cannot read configuration ${configPath}: ${error.message}`);}
|
|
372
475
|
}else{
|
|
373
476
|
if(typeof text!=='string')throw new ConfigError('configuration text must be a string');
|
|
477
|
+
if(Buffer.byteLength(text)>MAX_CONFIG_BYTES)throw new ConfigError(`configuration text exceeds ${MAX_CONFIG_BYTES} bytes`);
|
|
374
478
|
configPath??='.cm-workflow.yml';
|
|
375
479
|
}
|
|
376
480
|
if(SECRET_VALUE.test(text))throw new ConfigError('configuration contains a secret-like value');
|
|
377
481
|
try{
|
|
378
482
|
const raw=parseDocument(configPath,text);walkForSecrets(raw);validateRaw(raw);
|
|
379
483
|
const normalizedRaw={...raw,version:Number(raw.version)};
|
|
380
|
-
const
|
|
484
|
+
const projectDeclared=raw.runtimes?.available!==undefined;
|
|
485
|
+
const user=projectDeclared?null:loadUserRuntimes();
|
|
486
|
+
const defaults=user?deepMerge(DEFAULT_CONFIG,runtimePreset(user.preset)):DEFAULT_CONFIG;
|
|
487
|
+
const effective=deepMerge(defaults,normalizedRaw),policy=effective.roles.external_expert.model_policy;
|
|
488
|
+
RUNTIME_SOURCES.set(effective,projectDeclared?'project':user?'user':'none');
|
|
381
489
|
if(Object.hasOwn(MODEL_POLICY_ALIASES,policy))effective.roles.external_expert.model_policy=MODEL_POLICY_ALIASES[policy];
|
|
382
490
|
validateEffective(effective);return effective;
|
|
383
491
|
}catch(error){if(error instanceof ConfigError)throw error;if(error instanceof RangeError)throw new ConfigError('configuration nesting is too deep');throw error;}
|
|
@@ -449,9 +557,9 @@ export function main(argv=process.argv.slice(2)){
|
|
|
449
557
|
catch(error){process.stderr.write(`FAIL: ${error.message}\n`);return 1;}
|
|
450
558
|
if(args.printRole&&!args.role){process.stderr.write('FAIL: --print-role requires --role\n');return 2;}
|
|
451
559
|
if(args.role){
|
|
452
|
-
const role=resolveRole(config,args.role,args.runtime),body=stableJson(role);
|
|
560
|
+
const role=resolveRole(config,args.role,args.runtime),body=stableJson({...role,runtimes_source:runtimesSource(config)});
|
|
453
561
|
process.stdout.write(args.printRole?`${body}\n`:`workflow role: ${body}\n`);
|
|
454
|
-
}else if(args.printEffective)process.stdout.write(`${stableJson(config)}\n`);
|
|
562
|
+
}else if(args.printEffective)process.stdout.write(`${stableJson({...config,runtimes_source:runtimesSource(config)})}\n`);
|
|
455
563
|
else process.stdout.write('workflow config: PASSED\n');
|
|
456
564
|
return 0;
|
|
457
565
|
}
|
|
@@ -38,7 +38,12 @@ policies:
|
|
|
38
38
|
|
|
39
39
|
const fixture=async fn=>{
|
|
40
40
|
const root=fs.realpathSync(fs.mkdtempSync(path.join(os.tmpdir(),'cm-workflow-config-js-')));
|
|
41
|
-
|
|
41
|
+
const old=process.env.CM_WORKFLOW_HOME;
|
|
42
|
+
process.env.CM_WORKFLOW_HOME=path.join(root,'user');
|
|
43
|
+
try{return await fn(root);}finally{
|
|
44
|
+
if(old===undefined)delete process.env.CM_WORKFLOW_HOME;else process.env.CM_WORKFLOW_HOME=old;
|
|
45
|
+
fs.rmSync(root,{recursive:true,force:true});
|
|
46
|
+
}
|
|
42
47
|
};
|
|
43
48
|
|
|
44
49
|
test('JS config authority preserves defaults, YAML merge, canonical aliases, and route states',()=>fixture(async root=>{
|
|
@@ -179,3 +184,121 @@ test('frozen Python authority vectors preserve numeric token and equals-form beh
|
|
|
179
184
|
assert.equal(emptyProject.status,0);
|
|
180
185
|
assert.equal(JSON.parse(emptyProject.stdout).version,1);
|
|
181
186
|
}));
|
|
187
|
+
|
|
188
|
+
// --- runtimes.available 声明 ---
|
|
189
|
+
const withRuntimes=(available,coder,reviewer)=>`version: 1
|
|
190
|
+
runtimes:
|
|
191
|
+
available: ${available}
|
|
192
|
+
roles:
|
|
193
|
+
coder:
|
|
194
|
+
adapter: ${coder}
|
|
195
|
+
model: default
|
|
196
|
+
source: subscription
|
|
197
|
+
reviewer:
|
|
198
|
+
adapter: ${reviewer}
|
|
199
|
+
model: default
|
|
200
|
+
source: subscription`;
|
|
201
|
+
|
|
202
|
+
test('runtimes.available 缺省为 unknown,四个预设都能通过校验',()=>fixture(async root=>{
|
|
203
|
+
const {loadConfig,declaredRuntimes}=await import('./cm-workflow-config.mjs');
|
|
204
|
+
const none=loadConfig({projectRoot:root,text:'version: 1'});
|
|
205
|
+
assert.equal(none.runtimes.available,'unknown');
|
|
206
|
+
assert.deepEqual(declaredRuntimes(none),{available:'unknown',allowed:['codex','claude']});
|
|
207
|
+
for(const [available,coder,reviewer] of [['codex','codex-cli','codex-cli'],['claude','claude-cli','claude-cli'],
|
|
208
|
+
['both','codex-cli','claude-cli'],['both','claude-cli','codex-cli']]){
|
|
209
|
+
const config=loadConfig({projectRoot:root,text:withRuntimes(available,coder,reviewer)});
|
|
210
|
+
assert.equal(config.runtimes.available,available);
|
|
211
|
+
assert.equal(config.roles.coder.adapter,coder);
|
|
212
|
+
}
|
|
213
|
+
assert.deepEqual(declaredRuntimes(loadConfig({projectRoot:root,text:withRuntimes('codex','codex-cli','codex-cli')})),
|
|
214
|
+
{available:'codex',allowed:['codex']});
|
|
215
|
+
}));
|
|
216
|
+
|
|
217
|
+
test('声明只有一家时,角色不能指向另一家',()=>fixture(async root=>{
|
|
218
|
+
const {loadConfig}=await import('./cm-workflow-config.mjs');
|
|
219
|
+
assert.throws(()=>loadConfig({projectRoot:root,text:withRuntimes('codex','codex-cli','claude-cli')}),
|
|
220
|
+
/roles\.reviewer\.adapter claude-cli needs the claude runtime, but runtimes\.available declares codex/);
|
|
221
|
+
assert.throws(()=>loadConfig({projectRoot:root,text:withRuntimes('claude','codex-cli','claude-cli')}),
|
|
222
|
+
/roles\.coder\.adapter codex-cli needs the codex runtime/);
|
|
223
|
+
}));
|
|
224
|
+
|
|
225
|
+
test('声明两家都有时,写代码和审代码不能落在同一家',()=>fixture(async root=>{
|
|
226
|
+
const {loadConfig}=await import('./cm-workflow-config.mjs');
|
|
227
|
+
assert.throws(()=>loadConfig({projectRoot:root,text:withRuntimes('both','codex-cli','codex-cli')}),
|
|
228
|
+
/roles\.coder and roles\.reviewer both resolve to codex/);
|
|
229
|
+
// current-ai 表示「我所在的工具」,无法判定归属,不参与该检查。
|
|
230
|
+
assert.doesNotThrow(()=>loadConfig({projectRoot:root,text:withRuntimes('both','codex-cli','current-ai')}));
|
|
231
|
+
assert.doesNotThrow(()=>loadConfig({projectRoot:root,text:'version: 1\nruntimes:\n available: both'}));
|
|
232
|
+
}));
|
|
233
|
+
|
|
234
|
+
test('runtimes 字段只接受 available,取值只接受 codex/claude/both',()=>fixture(async root=>{
|
|
235
|
+
const {loadConfig,runtimeForAdapter}=await import('./cm-workflow-config.mjs');
|
|
236
|
+
assert.throws(()=>loadConfig({projectRoot:root,text:'version: 1\nruntimes:\n available: unknown'}),/must be one of: both, claude, codex/);
|
|
237
|
+
assert.throws(()=>loadConfig({projectRoot:root,text:'version: 1\nruntimes:\n available: gemini'}),/must be one of/);
|
|
238
|
+
assert.throws(()=>loadConfig({projectRoot:root,text:'version: 1\nruntimes:\n preset: codex-codes'}),/config\.runtimes has unknown field\(s\): preset/);
|
|
239
|
+
assert.throws(()=>loadConfig({projectRoot:root,text:'version: 1\nruntimes: both'}),/config\.runtimes must be a mapping/);
|
|
240
|
+
assert.equal(runtimeForAdapter('codex-cli'),'codex');assert.equal(runtimeForAdapter('claude-api'),'claude');
|
|
241
|
+
assert.equal(runtimeForAdapter('current-ai'),null);
|
|
242
|
+
}));
|
|
243
|
+
|
|
244
|
+
test('--print-effective 输出包含 runtimes 声明,CLI 拒绝矛盾配置并给出字段路径',()=>fixture(async root=>{
|
|
245
|
+
fs.writeFileSync(path.join(root,'.cm-workflow.yml'),withRuntimes('both','codex-cli','claude-cli'));
|
|
246
|
+
const ok=spawnSync(process.execPath,[entryPath,'--project',root,'--print-effective'],{encoding:'utf8'});
|
|
247
|
+
assert.equal(ok.status,0,ok.stderr);
|
|
248
|
+
assert.equal(JSON.parse(ok.stdout).runtimes.available,'both');
|
|
249
|
+
fs.writeFileSync(path.join(root,'.cm-workflow.yml'),withRuntimes('codex','codex-cli','claude-cli'));
|
|
250
|
+
const bad=spawnSync(process.execPath,[entryPath,'--project',root],{encoding:'utf8'});
|
|
251
|
+
assert.notEqual(bad.status,0);
|
|
252
|
+
assert.match(bad.stderr+bad.stdout,/roles\.reviewer\.adapter/);
|
|
253
|
+
}));
|
|
254
|
+
|
|
255
|
+
test('protected runtime selection obeys all declarations without claiming process dispatch',async()=>{
|
|
256
|
+
const {loadConfig,resolveRole,resolveProtectedRuntimes}=await import('./cm-workflow-config.mjs');
|
|
257
|
+
for(const [available,coder,reviewer] of [['codex','codex','codex'],['claude','claude','claude'],['both','codex','claude'],['both','claude','codex']]){
|
|
258
|
+
const config=loadConfig({projectRoot:scriptsRoot,configPath:'fixture.json',text:JSON.stringify({version:1,runtimes:{available},
|
|
259
|
+
roles:{coder:{adapter:`${coder}-cli`},reviewer:{adapter:`${reviewer}-cli`}}})});
|
|
260
|
+
for(const runtime of ['codex','claude']){
|
|
261
|
+
assert.deepEqual(resolveProtectedRuntimes(config,runtime),{coderRuntime:coder,reviewerRuntime:reviewer});
|
|
262
|
+
assert.equal(resolveRole(config,'coder',runtime).route_state,coder===runtime?'current-runtime':'declared-adapter');
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
// Defense in depth for the late-bound current-ai and unvalidated input cases.
|
|
266
|
+
const config=loadConfig({projectRoot:scriptsRoot,configPath:'fixture.json',text:JSON.stringify({version:1,runtimes:{available:'both'},
|
|
267
|
+
roles:{coder:{adapter:'current-ai'},reviewer:{adapter:'codex-cli'}}})});
|
|
268
|
+
assert.throws(()=>resolveProtectedRuntimes(config,'codex'),/cross_runtime_review_required/);
|
|
269
|
+
assert.deepEqual(resolveProtectedRuntimes(config,'claude'),{coderRuntime:'claude',reviewerRuntime:'codex'});
|
|
270
|
+
config.runtimes.available='claude';
|
|
271
|
+
assert.throws(()=>resolveProtectedRuntimes(config,'claude'),/runtime_not_declared/);
|
|
272
|
+
config.roles.reviewer.adapter='current-ai';
|
|
273
|
+
assert.deepEqual(resolveProtectedRuntimes(config,'claude'),{coderRuntime:'claude',reviewerRuntime:'claude'});
|
|
274
|
+
});
|
|
275
|
+
|
|
276
|
+
|
|
277
|
+
test('user defaults supply preset roles, project fields win, declarations bypass user file',()=>fixture(async root=>{
|
|
278
|
+
const {loadConfig,resolveRole,runtimesSource}=await import('./cm-workflow-config.mjs');
|
|
279
|
+
fs.mkdirSync(process.env.CM_WORKFLOW_HOME);
|
|
280
|
+
const file=path.join(process.env.CM_WORKFLOW_HOME,'runtimes.yml');
|
|
281
|
+
fs.writeFileSync(file,'runtimes: {available: both}\npreset: claude-codes\n');
|
|
282
|
+
const inherited=loadConfig({projectRoot:root});
|
|
283
|
+
assert.equal(runtimesSource(inherited),'user');assert.equal(inherited.runtimes.available,'both');
|
|
284
|
+
assert.equal(inherited.roles.coder.adapter,'claude-cli');assert.equal(inherited.roles.reviewer.adapter,'codex-cli');
|
|
285
|
+
assert.equal(runtimesSource(inherited),'user');
|
|
286
|
+
const partial=loadConfig({projectRoot:root,text:'version: 1\nroles: {coder: {model: custom}}\n'});
|
|
287
|
+
assert.equal(partial.roles.coder.model,'custom');assert.equal(partial.roles.coder.source,'subscription');
|
|
288
|
+
assert.throws(()=>loadConfig({projectRoot:root,text:'version: 1\nroles: {coder: {adapter: codex-cli}}'}),/both resolve to codex/);
|
|
289
|
+
fs.writeFileSync(file,'runtimes: {available: nope}\npreset: invalid\n');
|
|
290
|
+
assert.throws(()=>loadConfig({projectRoot:root}),/user.runtimes.available/);
|
|
291
|
+
const project=loadConfig({projectRoot:root,text:'version: 1\nruntimes: {available: codex}'});
|
|
292
|
+
assert.equal(runtimesSource(project),'project');assert.equal(project.roles.coder.adapter,'current-ai');
|
|
293
|
+
for(const text of ['runtimes: {available: both}\npreset: invalid',
|
|
294
|
+
'runtimes: {available: codex}\npreset: claude-only','runtimes: {available: both}\npreset: codex-codes\nroles: {}',
|
|
295
|
+
'preset: codex-only','runtimes: {available: codex}']){
|
|
296
|
+
fs.writeFileSync(file,text);assert.throws(()=>loadConfig({projectRoot:root}),/runtimes.yml:.*user/);
|
|
297
|
+
}
|
|
298
|
+
fs.writeFileSync(file,'runtimes: {available: codex}\npreset: codex-only');
|
|
299
|
+
for(const args of [['--print-effective'],['--role','coder','--print-role']]){
|
|
300
|
+
const result=spawnSync(process.execPath,[entryPath,'--project',root,...args],{encoding:'utf8'});
|
|
301
|
+
assert.equal(result.status,0,result.stderr);assert.equal(JSON.parse(result.stdout).runtimes_source,'user');
|
|
302
|
+
}
|
|
303
|
+
fs.unlinkSync(file);assert.equal(runtimesSource(loadConfig({projectRoot:root})),'none');
|
|
304
|
+
}));
|