@aibyzero/cm-workflow 0.16.7 → 0.16.8
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/CHANGELOG.md +18 -0
- package/README.md +26 -1
- package/VERSION +1 -1
- package/assets/readme/workflow-recovery-detailed.png +0 -0
- package/assets/readme/workflow-stages-detailed.png +0 -0
- package/docs/installation.md +1 -1
- package/docs/js-workflow-control.md +36 -3
- package/docs/user-guide.md +104 -0
- package/package.json +1 -1
- package/runtime/js/cm-ai/cm-ai-admission.mjs +3 -1
- package/runtime/js/cm-ai/cm-ai-conversation-entry.mjs +27 -6
- package/runtime/js/cm-ai/cm-ai-qa-log.mjs +82 -8
- package/runtime/js/cm-ai/codex-review-adapter.mjs +2 -1
- package/runtime/js/cm-ai/drive-core.mjs +17 -4
- package/runtime/js/cm-ai/execution-store.mjs +4 -2
- package/runtime/js/cm-ai/host-qa-executor.mjs +37 -13
- package/runtime/js/cm-ai/host-session.mjs +46 -6
- package/runtime/js/cm-ai/host-tool-bridge.mjs +15 -3
- package/runtime/js/cm-ai/host-workflow-capabilities.mjs +4 -2
- package/runtime/js/cm-ai/reviewed-deliveries.mjs +29 -6
- package/runtime/js/cm-fix/cause-package.mjs +37 -8
- package/runtime/js/cm-fix/diagnosis.mjs +43 -0
- package/runtime/js/cm-fix/execution.mjs +189 -148
- package/runtime/js/cm-fix/investigation.mjs +25 -9
- package/runtime/js/cm-fix/progress.mjs +3 -1
- package/runtime/js/notify-run.mjs +26 -0
- package/runtime/js/notify.mjs +388 -0
- package/runtime/model-efficiency.md +17 -0
- package/scripts/cm-ai-batch-run.mjs +5 -0
- package/scripts/cm-ai-batch-run.test.mjs +9 -1
- package/scripts/cm-ai-drive.mjs +4 -1
- package/scripts/cm-ai-drive.test.mjs +8 -0
- package/scripts/cm-ai-fix-run-delivery.test.mjs +138 -0
- package/scripts/cm-ai-host.mjs +2 -2
- package/scripts/cm-ai-project-qa-gate.test.mjs +71 -4
- package/scripts/cm-ai-qa-rerun-timeouts.test.mjs +77 -4
- package/scripts/cm-fix-abandon.test.mjs +89 -3
- package/scripts/cm-fix-cause-package.test.mjs +33 -0
- package/scripts/cm-fix-drive.mjs +9 -1
- package/scripts/cm-fix-host.mjs +1 -1
- package/scripts/cm-fix-rediagnosis.test.mjs +71 -4
- package/scripts/cm-host-qa-executor.test.mjs +121 -0
- package/scripts/cm-log-event.mjs +31 -5
- package/scripts/cm-notify.test.mjs +468 -0
- package/scripts/cm-patrol.mjs +83 -0
- package/scripts/cm-patrol.test.mjs +161 -0
- package/scripts/cm-token-report.mjs +497 -0
- package/scripts/cm-token-report.test.mjs +346 -0
- package/skills/cm-ai/SKILL.md +7 -3
- package/skills/cm-ai/references/N6-qa-eval.md +2 -2
- package/skills/cm-ai/references/js-host-modes.md +90 -0
- package/skills/cm-ai/references/js-host-qa-recovery.md +110 -0
- package/skills/cm-ai/references/js-host-recovery.md +199 -0
- package/skills/cm-ai/references/js-host.md +67 -329
- package/skills/cm-fix/SKILL.md +2 -0
- package/skills/cm-fix/references/js-host.md +16 -9
- package/skills/cm-prd/SKILL.md +2 -0
|
@@ -0,0 +1,161 @@
|
|
|
1
|
+
import test from 'node:test';
|
|
2
|
+
import assert from 'node:assert/strict';
|
|
3
|
+
import fs from 'node:fs';
|
|
4
|
+
import os from 'node:os';
|
|
5
|
+
import path from 'node:path';
|
|
6
|
+
import {spawn,spawnSync} from 'node:child_process';
|
|
7
|
+
import {fileURLToPath} from 'node:url';
|
|
8
|
+
import {patrol,inspectHosts,processAlive} from './cm-patrol.mjs';
|
|
9
|
+
import {hostRegistryDir,buildNotifyMessage,removeHostRegistry} from '../runtime/js/notify.mjs';
|
|
10
|
+
|
|
11
|
+
const HOST_SESSION=new URL('../runtime/js/cm-ai/host-session.mjs',import.meta.url).href;
|
|
12
|
+
const PATROL=fileURLToPath(new URL('./cm-patrol.mjs',import.meta.url));
|
|
13
|
+
const NOW=Date.parse('2026-10-09T08:00:00.000Z');
|
|
14
|
+
const KEYS=['11111111-1111-4111-8111-111111111111','22222222-2222-4222-8222-222222222222','33333333-3333-4333-8333-333333333333',
|
|
15
|
+
'44444444-4444-4444-8444-444444444444'];
|
|
16
|
+
|
|
17
|
+
// A temp CM_WORKFLOW_HOME whose notify.json points at a fake command that
|
|
18
|
+
// appends its message to sent.jsonl.
|
|
19
|
+
function home(t,{withConfig=true}={}){
|
|
20
|
+
const dir=fs.realpathSync(fs.mkdtempSync(path.join(os.tmpdir(),'cm-patrol-')));
|
|
21
|
+
t.after(()=>fs.rmSync(dir,{recursive:true,force:true}));
|
|
22
|
+
const sent=path.join(dir,'sent.jsonl'),script=path.join(dir,'fake-notify.mjs');
|
|
23
|
+
fs.writeFileSync(script,`import fs from 'node:fs';let input='';process.stdin.on('data',c=>input+=c);
|
|
24
|
+
process.stdin.on('end',()=>{fs.appendFileSync(${JSON.stringify(sent)},JSON.stringify({title:process.env.CM_NOTIFY_TITLE,
|
|
25
|
+
body:process.env.CM_NOTIFY_BODY,stdin:JSON.parse(input)})+'\\n');});`);
|
|
26
|
+
if(withConfig)fs.writeFileSync(path.join(dir,'notify.json'),JSON.stringify({version:1,command:[process.execPath,script]}));
|
|
27
|
+
const env={...process.env,CM_WORKFLOW_HOME:dir};
|
|
28
|
+
const rows=()=>fs.existsSync(sent)?fs.readFileSync(sent,'utf8').trim().split('\n').map(line=>JSON.parse(line)):[];
|
|
29
|
+
const until=async(check,ms=8000)=>{const end=Date.now()+ms;while(!check()&&Date.now()<end)await new Promise(r=>setTimeout(r,25));return check();};
|
|
30
|
+
const registry=()=>fs.existsSync(hostRegistryDir(env))?fs.readdirSync(hostRegistryDir(env)).filter(name=>name.endsWith('.json')).sort():[];
|
|
31
|
+
const write=(key,entry)=>{fs.mkdirSync(hostRegistryDir(env),{recursive:true});fs.writeFileSync(path.join(hostRegistryDir(env),`${key}.json`),
|
|
32
|
+
typeof entry==='string'?entry:JSON.stringify({version:1,sessionKey:key,workflow:'cm-ai',project:'/srv/work/demo-app',startedAt:new Date(NOW-3600000).toISOString(),
|
|
33
|
+
at:new Date(NOW-600000).toISOString(),runId:'run-1',task:'T-003',stage:'awaiting_review',operation:'advance',...entry}));};
|
|
34
|
+
return {dir,env,rows,until,registry,write};
|
|
35
|
+
}
|
|
36
|
+
// A pid that certainly existed and is gone now.
|
|
37
|
+
function gonePid(){const child=spawnSync(process.execPath,['-e','']);assert.equal(child.status,0);return child.pid;}
|
|
38
|
+
|
|
39
|
+
test('processAlive: own pid and a finished child',()=>{
|
|
40
|
+
assert.equal(processAlive(process.pid),true);
|
|
41
|
+
assert.equal(processAlive(gonePid()),false);
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
test('host session registers itself when notices are configured: written at start, updated per reply, removed at end',async t=>{
|
|
45
|
+
const h=home(t);
|
|
46
|
+
const script=path.join(h.dir,'cm-prd-host.mjs');
|
|
47
|
+
fs.writeFileSync(script,`import {serveCmAiHost} from ${JSON.stringify(HOST_SESSION)};
|
|
48
|
+
const host={handle:async request=>{if(request.slowMs)await new Promise(resolve=>setTimeout(resolve,request.slowMs));
|
|
49
|
+
return request.operation==='status'?{stage:'poll'}:{stage:request.stage,runId:'prd-9'};}};
|
|
50
|
+
await serveCmAiHost({host,input:process.stdin,output:process.stdout});`);
|
|
51
|
+
const env={...h.env};delete env.NODE_TEST_CONTEXT;
|
|
52
|
+
const child=spawn(process.execPath,[script],{env,stdio:['pipe','pipe','pipe']});
|
|
53
|
+
t.after(()=>{try{child.kill();}catch{}});
|
|
54
|
+
const exited=new Promise(resolve=>child.on('close',resolve));
|
|
55
|
+
let out='';child.stdout.on('data',chunk=>{out+=chunk;});
|
|
56
|
+
assert(await h.until(()=>h.registry().length===1),'registered at start');
|
|
57
|
+
const read=()=>JSON.parse(fs.readFileSync(path.join(hostRegistryDir(env),h.registry()[0]),'utf8'));
|
|
58
|
+
const first=read();
|
|
59
|
+
assert.equal(first.pid,child.pid);assert.equal(first.workflow,'cm-prd');assert.equal(first.project,process.cwd());
|
|
60
|
+
assert.equal(first.runId,null);assert.equal(first.operation,null);
|
|
61
|
+
child.stdin.write(JSON.stringify({requestId:'r1',operation:'advance',stage:'requirements_analysis'})+'\n');
|
|
62
|
+
assert(await h.until(()=>out.includes('"requestId":"r1"')&&read().operation==='advance'));
|
|
63
|
+
const after=read();assert.equal(after.runId,'prd-9');assert.equal(after.stage,'requirements_analysis');assert.equal(after.sessionKey,first.sessionKey);
|
|
64
|
+
child.stdin.write(JSON.stringify({requestId:'s1',operation:'status'})+'\n');
|
|
65
|
+
assert(await h.until(()=>out.includes('"requestId":"s1"')));
|
|
66
|
+
await new Promise(resolve=>setTimeout(resolve,100));
|
|
67
|
+
assert.equal(read().stage,'requirements_analysis','a status poll does not overwrite the registry');
|
|
68
|
+
child.stdin.end();assert.equal(await exited,0);
|
|
69
|
+
assert.deepEqual(h.registry(),[],'removed at session end');
|
|
70
|
+
// EOF while an operation is still running: its reply must not re-register after the removal.
|
|
71
|
+
const busy=spawn(process.execPath,[script],{env,stdio:['pipe','pipe','pipe']});
|
|
72
|
+
t.after(()=>{try{busy.kill();}catch{}});
|
|
73
|
+
const busyExit=new Promise(resolve=>busy.on('close',resolve));let busyOut='';busy.stdout.on('data',chunk=>{busyOut+=chunk;});
|
|
74
|
+
assert(await h.until(()=>h.registry().length===1));
|
|
75
|
+
busy.stdin.write(JSON.stringify({requestId:'r1',operation:'advance',stage:'slow',slowMs:600})+'\n');
|
|
76
|
+
await new Promise(resolve=>setTimeout(resolve,100));busy.stdin.end();
|
|
77
|
+
assert.equal(await busyExit,0);assert.match(busyOut,/"requestId":"r1"/,'the in-flight reply is still delivered');
|
|
78
|
+
assert.deepEqual(h.registry(),[],'removed after the in-flight reply');
|
|
79
|
+
// A session that ends immediately leaves nothing behind either.
|
|
80
|
+
const quick=spawnSync(process.execPath,[script],{env,input:''});assert.equal(quick.status,0,quick.stderr);
|
|
81
|
+
assert.deepEqual(h.registry(),[]);
|
|
82
|
+
// Without notify.json nothing is written.
|
|
83
|
+
const off=home(t,{withConfig:false});const offEnv={...off.env};delete offEnv.NODE_TEST_CONTEXT;
|
|
84
|
+
const silent=spawnSync(process.execPath,[script],{env:offEnv,input:JSON.stringify({requestId:'r1',operation:'advance',stage:'x'})+'\n'});
|
|
85
|
+
assert.equal(silent.status,0,silent.stderr);assert.equal(fs.existsSync(hostRegistryDir(offEnv)),false);
|
|
86
|
+
});
|
|
87
|
+
|
|
88
|
+
test('patrol: dead hosts are merged into one notice and their entries removed; live, stale and invalid entries are left alone',async t=>{
|
|
89
|
+
const h=home(t),gone=gonePid();
|
|
90
|
+
h.write(KEYS[0],{pid:process.pid});
|
|
91
|
+
h.write(KEYS[1],{pid:gone,workflow:'cm-ai',task:'T-003',at:new Date(NOW-900000).toISOString()});
|
|
92
|
+
h.write(KEYS[2],{pid:gone,workflow:'cm-fix',runId:'fix-2',task:null,stage:'repair_required',at:new Date(NOW-300000).toISOString()});
|
|
93
|
+
h.write(KEYS[3],{pid:gone,at:new Date(NOW-49*3600000).toISOString()});
|
|
94
|
+
h.write('55555555-5555-4555-8555-555555555555','{not json');
|
|
95
|
+
const seen=inspectHosts({env:h.env,now:NOW});
|
|
96
|
+
assert.deepEqual([seen.live.length,seen.dead.length,seen.stale.length,seen.invalid],[1,2,1,1]);
|
|
97
|
+
const first=patrol({env:h.env,now:NOW});
|
|
98
|
+
assert.equal(first.notice,'sent');assert.equal(first.dead,2);
|
|
99
|
+
assert(await h.until(()=>h.rows().length===1));
|
|
100
|
+
const [row]=h.rows();
|
|
101
|
+
assert.equal(row.title,'CM cm 宿主已退出未收尾 · demo-app');
|
|
102
|
+
assert.equal(row.stdin.code,'host_process_gone');assert.equal(row.stdin.stage,'2 个宿主进程已不在');
|
|
103
|
+
assert.match(row.body,/cm-ai run-1 T-003 awaiting_review 最后动静 07:45Z;cm-fix fix-2 repair_required 最后动静 07:55Z/);
|
|
104
|
+
assert.doesNotMatch(row.body+JSON.stringify(row.stdin),/srv|demo-app\//,'no paths in body or payload');
|
|
105
|
+
assert.deepEqual(h.registry(),[`${KEYS[0]}.json`,`${KEYS[3]}.json`,'55555555-5555-4555-8555-555555555555.json']);
|
|
106
|
+
const again=patrol({env:h.env,now:NOW+60000});
|
|
107
|
+
assert.equal(again.notice,'none');assert.equal(again.dead,0);
|
|
108
|
+
await new Promise(resolve=>setTimeout(resolve,300));assert.equal(h.rows().length,1);
|
|
109
|
+
});
|
|
110
|
+
|
|
111
|
+
test('patrol: a single dead host names its run and workflow; rate-limited or off keeps the entry for a later pass',async t=>{
|
|
112
|
+
const h=home(t),gone=gonePid();
|
|
113
|
+
h.write(KEYS[1],{pid:gone,workflow:'cm-prd',runId:'prd-7',task:null,stage:'awaiting_user'});
|
|
114
|
+
const state={version:1,keys:{},sends:Array.from({length:4},()=>NOW-1000)};
|
|
115
|
+
fs.writeFileSync(path.join(h.dir,'notify-state.json'),JSON.stringify(state));
|
|
116
|
+
assert.equal(patrol({env:h.env,now:NOW}).notice,'rate_minute');
|
|
117
|
+
assert.deepEqual(h.registry(),[`${KEYS[1]}.json`],'kept for the next pass');
|
|
118
|
+
assert.equal(patrol({env:h.env,now:NOW+61000}).notice,'sent');
|
|
119
|
+
assert(await h.until(()=>h.rows().length===1));
|
|
120
|
+
assert.equal(h.rows()[0].title,'CM cm-prd 宿主已退出未收尾 · demo-app');
|
|
121
|
+
assert.equal(h.rows()[0].stdin.runId,'prd-7');assert.equal(h.rows()[0].stdin.stage,'1 个宿主进程已不在');
|
|
122
|
+
assert.deepEqual(h.registry(),[]);
|
|
123
|
+
const off=home(t,{withConfig:false});off.write(KEYS[1],{pid:gone});
|
|
124
|
+
assert.equal(patrol({env:off.env,now:NOW}).notice,'off');assert.deepEqual(off.registry(),[`${KEYS[1]}.json`]);
|
|
125
|
+
});
|
|
126
|
+
|
|
127
|
+
test('--report prints the summary and neither notifies nor removes anything',t=>{
|
|
128
|
+
const h=home(t),gone=gonePid();
|
|
129
|
+
h.write(KEYS[1],{pid:gone});
|
|
130
|
+
const run=spawnSync(process.execPath,[PATROL,'--report'],{encoding:'utf8',env:{...h.env,NODE_TEST_CONTEXT:undefined}});
|
|
131
|
+
assert.equal(run.status,0,run.stderr);
|
|
132
|
+
const summary=JSON.parse(run.stdout);
|
|
133
|
+
assert.equal(summary.notice,'report_only');assert.equal(summary.dead,1);assert.equal(summary.deadHosts[0].pid,gone);
|
|
134
|
+
assert.deepEqual(h.registry(),[`${KEYS[1]}.json`]);assert.equal(h.rows().length,0);
|
|
135
|
+
const plain=spawnSync(process.execPath,[PATROL],{encoding:'utf8',env:{...h.env,NODE_TEST_CONTEXT:undefined}});
|
|
136
|
+
assert.equal(plain.status,0,plain.stderr);assert.equal(plain.stdout,'');
|
|
137
|
+
});
|
|
138
|
+
|
|
139
|
+
test('Windows paths are redacted like POSIX ones, also when glued to CJK text',()=>{
|
|
140
|
+
const {body}=buildNotifyMessage({workflow:'cm-ai',nextAction:'见 C:\\Users\\me\\proj\\a.txt 和 \\\\server\\share\\b 以及 /tmp/c'},{now:NOW});
|
|
141
|
+
assert.match(body,/下一步:见 <路径> 和 <路径> 以及 <路径>/);
|
|
142
|
+
const glued=buildNotifyMessage({workflow:'cm-ai',nextAction:'请检查C:\\Users\\me\\a.txt,再看\\\\srv\\s;或/tmp/c。相对 a/b 和 v1.2/x 保留'},{now:NOW}).body;
|
|
143
|
+
assert.match(glued,/下一步:请检查<路径>,再看<路径>;或<路径>。相对 a\/b 和 v1.2\/x 保留/);
|
|
144
|
+
assert.doesNotMatch(glued,/Users|srv|tmp/);
|
|
145
|
+
});
|
|
146
|
+
|
|
147
|
+
// Under node --test without an explicit CM_WORKFLOW_HOME nothing in the user's
|
|
148
|
+
// real directory is touched, not even an unlink of a registry entry.
|
|
149
|
+
test('registry removal honours the test guard',t=>{
|
|
150
|
+
const fakeHome=fs.realpathSync(fs.mkdtempSync(path.join(os.tmpdir(),'cm-patrol-home-')));
|
|
151
|
+
t.after(()=>fs.rmSync(fakeHome,{recursive:true,force:true}));
|
|
152
|
+
const dir=path.join(fakeHome,'.cm-workflow','hosts');fs.mkdirSync(dir,{recursive:true});
|
|
153
|
+
const file=path.join(dir,`${KEYS[0]}.json`);fs.writeFileSync(file,'{}');
|
|
154
|
+
// os.homedir() reads HOME from the live process environment, so point it at the fake home for this test only.
|
|
155
|
+
const realHome=process.env.HOME;process.env.HOME=fakeHome;t.after(()=>{process.env.HOME=realHome;});
|
|
156
|
+
assert.equal(os.homedir(),fakeHome);
|
|
157
|
+
const env={...process.env,NODE_TEST_CONTEXT:'1'};delete env.CM_WORKFLOW_HOME;
|
|
158
|
+
removeHostRegistry(KEYS[0],{env});assert.equal(fs.existsSync(file),true,'home left alone under node --test');
|
|
159
|
+
removeHostRegistry(KEYS[0],{env:{...env,CM_WORKFLOW_HOME:path.join(fakeHome,'.cm-workflow')}});
|
|
160
|
+
assert.equal(fs.existsSync(file),false,'explicit home is honoured');
|
|
161
|
+
});
|