@aibyzero/cm-workflow 0.15.5 → 0.16.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -28,8 +28,9 @@ test('host handoff uses existing gate and binds actual changed bytes',()=>fixtur
28
28
  const handoff=loadHandoff(input.handoffPath,{task:'T-001',attempt:1});
29
29
  assert.deepEqual(handoff.changed_files,['a.mjs']);
30
30
  assert.equal(handoff.implementation_sha256,implementationSha256(input.root,['a.mjs']));
31
+ // Republishing the same bytes is the crash-after-link case: already published.
31
32
  const before=fs.readFileSync(input.handoffPath);
32
- assert.throws(()=>createHostHandoff(input),{code:'EEXIST'});
33
+ assert.equal(createHostHandoff(input).status,'ready_for_review');
33
34
  assert.deepEqual(fs.readFileSync(input.handoffPath),before);
34
35
  }));
35
36
  test('failed host check produces blocked handoff rather than invented success',()=>fixture(input=>{
@@ -56,3 +57,117 @@ test('new host handoff feeds existing Learning writer and verifier',()=>fixture(
56
57
  const evidence=loadHandoff(input.handoffPath).evidence;
57
58
  assert.equal(evidence.filter(x=>x.startsWith('cm-learning-')).length,2);
58
59
  }));
60
+
61
+ // A run that dies before review leaves a handoff behind. Refusing to replace it
62
+ // blocks every later attempt of that task forever, which is what #81 reported.
63
+ // Approval evidence still must never be overwritten, so the receipt decides.
64
+ function collisionFixture(fn){
65
+ const temp=fs.realpathSync(fs.mkdtempSync(path.join(os.tmpdir(),'cm-handoff-collision-')));
66
+ const root=path.join(temp,'code'),reviews=path.join(temp,'reviews');fs.mkdirSync(root);fs.mkdirSync(reviews);
67
+ fs.writeFileSync(path.join(root,'a.mjs'),'old');fs.writeFileSync(path.join(root,'requirements.md'),'synthetic task');
68
+ const handoffPath=path.join(reviews,'9.demo-T-001-a1-handoff.json');
69
+ const build=body=>{
70
+ fs.writeFileSync(path.join(root,'a.mjs'),'old');
71
+ const baseline=captureReviewBaseline({root,identity:{repositoryId:'test',runId:'run',taskId:'T-001',attempt:1},
72
+ scope:['a.mjs'],requirements:['requirements.md']});
73
+ fs.writeFileSync(path.join(root,'a.mjs'),body);
74
+ return {root,baseline,handoffPath,
75
+ checks:[{id:'unit',command:['node','--test'],outcome:'passed',exitCode:0,evidence:'host fixture check'}]};
76
+ };
77
+ try{fn({reviews,handoffPath,build});}finally{fs.rmSync(temp,{recursive:true,force:true});}
78
+ }
79
+
80
+ test('an unreviewed handoff from a dead run is archived so the retry can publish',()=>collisionFixture(f=>{
81
+ createHostHandoff(f.build('first attempt'));
82
+ const stale=fs.readFileSync(f.handoffPath);
83
+ assert.equal(createHostHandoff(f.build('second attempt')).status,'ready_for_review');
84
+ const republished=fs.readFileSync(f.handoffPath);
85
+ assert.notDeepEqual(republished,stale);
86
+ const archive=path.join(f.reviews,'.superseded');
87
+ const archived=fs.readdirSync(archive);
88
+ assert.equal(archived.length,1);
89
+ assert.match(archived[0],/^9\.demo-T-001-a1-handoff\.json\.[0-9a-f]{16}$/);
90
+ assert.deepEqual(fs.readFileSync(path.join(archive,archived[0])),stale);
91
+ }));
92
+
93
+ test('a handoff the matching receipt names is never replaced',()=>collisionFixture(f=>{
94
+ createHostHandoff(f.build('first attempt'));
95
+ const reviewed=fs.readFileSync(f.handoffPath);
96
+ fs.writeFileSync(path.join(f.reviews,'9.demo-T-001-r1.md'),
97
+ ['---','at: 2026-01-01T00:00:00.000Z','reviewer: claude-cli','independent: true','task: T-001',
98
+ 'attempt: 1','round: 1','verdict: approved','handoff: 9.demo-T-001-a1-handoff.json',
99
+ 'handoff_sha256: '+'0'.repeat(64),'blocking_findings: 0','---',''].join('\n'));
100
+ assert.throws(()=>createHostHandoff(f.build('second attempt')),error=>error.code==='handoff_exists');
101
+ assert.deepEqual(fs.readFileSync(f.handoffPath),reviewed);
102
+ assert.equal(fs.existsSync(path.join(f.reviews,'.superseded')),false);
103
+ }));
104
+
105
+ test('a receipt for another handoff does not protect this one',()=>collisionFixture(f=>{
106
+ createHostHandoff(f.build('first attempt'));
107
+ fs.writeFileSync(path.join(f.reviews,'9.demo-T-001-r1.md'),
108
+ ['---','verdict: approved','handoff: 9.demo-T-002-a1-handoff.json','---',''].join('\n'));
109
+ assert.equal(createHostHandoff(f.build('second attempt')).status,'ready_for_review');
110
+ assert.equal(fs.readdirSync(path.join(f.reviews,'.superseded')).length,1);
111
+ }));
112
+
113
+ // The receipt names the handoff it reviewed inside its front matter, and the
114
+ // scope list after that line is as long as the task's changed file list. Reading
115
+ // a fixed number of lines made the decision depend on field order; a reordering
116
+ // would silently turn reviewed evidence into a supersedable leftover.
117
+ const receipt=(handoffName,scopeLength,{order='handoff-first',terminated=true}={})=>{
118
+ const head=['---','at: 2026-01-01T00:00:00.000Z','reviewer: claude-cli','independent: true',
119
+ 'task: T-001','attempt: 1','round: 1','verdict: approved'];
120
+ const bound=[`handoff: ${handoffName}`,'handoff_sha256: '+'0'.repeat(64),'blocking_findings: 0'];
121
+ const scope=['scope:',...Array.from({length:scopeLength},(_,i)=>` - src/file${i}.mjs`)];
122
+ return [...head,...(order==='handoff-first'?[...bound,...scope]:[...scope,...bound]),
123
+ ...(terminated?['---','','No blocking findings.']:[])].join('\n');
124
+ };
125
+
126
+ test('a reviewed handoff stays protected however long the scope list is',()=>collisionFixture(f=>{
127
+ createHostHandoff(f.build('first attempt'));
128
+ const reviewed=fs.readFileSync(f.handoffPath);
129
+ // 200 scope entries push the handoff line far past any fixed window.
130
+ fs.writeFileSync(path.join(f.reviews,'9.demo-T-001-r1.md'),
131
+ receipt('9.demo-T-001-a1-handoff.json',200));
132
+ assert.throws(()=>createHostHandoff(f.build('second attempt')),error=>error.code==='handoff_exists');
133
+ assert.deepEqual(fs.readFileSync(f.handoffPath),reviewed);
134
+ }));
135
+
136
+ test('a reviewed handoff stays protected if the front matter is reordered',()=>collisionFixture(f=>{
137
+ createHostHandoff(f.build('first attempt'));
138
+ const reviewed=fs.readFileSync(f.handoffPath);
139
+ fs.writeFileSync(path.join(f.reviews,'9.demo-T-001-r1.md'),
140
+ receipt('9.demo-T-001-a1-handoff.json',40,{order:'scope-first'}));
141
+ assert.throws(()=>createHostHandoff(f.build('second attempt')),error=>error.code==='handoff_exists');
142
+ assert.deepEqual(fs.readFileSync(f.handoffPath),reviewed);
143
+ }));
144
+
145
+ test('a receipt that is not recognisable front matter fails closed',()=>collisionFixture(f=>{
146
+ createHostHandoff(f.build('first attempt'));
147
+ const reviewed=fs.readFileSync(f.handoffPath);
148
+ for(const body of [
149
+ receipt('9.demo-T-001-a1-handoff.json',3,{terminated:false}), // 未闭合
150
+ 'no front matter at all\nhandoff: 9.demo-T-001-a1-handoff.json\n', // 没有起始 ---
151
+ ]){
152
+ fs.writeFileSync(path.join(f.reviews,'9.demo-T-001-r1.md'),body);
153
+ assert.throws(()=>createHostHandoff(f.build('second attempt')),error=>error.code==='handoff_exists');
154
+ assert.deepEqual(fs.readFileSync(f.handoffPath),reviewed);
155
+ }
156
+ }));
157
+
158
+ test('a handoff line in the body rather than the front matter does not protect',()=>collisionFixture(f=>{
159
+ // Only the front matter binds a receipt to a handoff; prose must not.
160
+ createHostHandoff(f.build('first attempt'));
161
+ fs.writeFileSync(path.join(f.reviews,'9.demo-T-001-r1.md'),
162
+ ['---','verdict: approved','---','','handoff: 9.demo-T-001-a1-handoff.json'].join('\n'));
163
+ assert.equal(createHostHandoff(f.build('second attempt')).status,'ready_for_review');
164
+ assert.equal(fs.readdirSync(path.join(f.reviews,'.superseded')).length,1);
165
+ }));
166
+
167
+ test('an oversized receipt fails closed instead of being parsed',()=>collisionFixture(f=>{
168
+ createHostHandoff(f.build('first attempt'));
169
+ const reviewed=fs.readFileSync(f.handoffPath);
170
+ fs.writeFileSync(path.join(f.reviews,'9.demo-T-001-r1.md'),'x'.repeat(256*1024+1));
171
+ assert.throws(()=>createHostHandoff(f.build('second attempt')),error=>error.code==='handoff_exists');
172
+ assert.deepEqual(fs.readFileSync(f.handoffPath),reviewed);
173
+ }));
@@ -0,0 +1,82 @@
1
+ import assert from 'node:assert/strict';
2
+ import test from 'node:test';
3
+ import {PassThrough} from 'node:stream';
4
+
5
+ import {serveCmAiHost} from '../runtime/js/cm-ai/host-session.mjs';
6
+
7
+ // Drives one request through the real JSONL transport and returns both channels.
8
+ async function exchange(request,handle){
9
+ const input=new PassThrough(),output=new PassThrough(),errorOutput=new PassThrough();
10
+ const replies=[],diagnostics=[];
11
+ output.on('data',chunk=>{for(const line of chunk.toString().split('\n'))if(line.trim())replies.push(JSON.parse(line));});
12
+ errorOutput.on('data',chunk=>{for(const line of chunk.toString().split('\n'))if(line.trim())diagnostics.push(JSON.parse(line));});
13
+ const serving=serveCmAiHost({host:{handle},input,output,errorOutput});
14
+ input.write(JSON.stringify(request)+'\n');
15
+ input.end();
16
+ await serving;
17
+ await new Promise(resolve=>setImmediate(resolve));
18
+ return {replies,diagnostics};
19
+ }
20
+
21
+ const failing=code=>()=>{throw Object.assign(new Error(code),{code});};
22
+
23
+ test('the redacted reply is unchanged and the real code goes to stderr', async () => {
24
+ // The peer must keep seeing host_request_failed: which codes may cross that
25
+ // boundary is a deliberate design, not an oversight.
26
+ const {replies,diagnostics}=await exchange({operation:'save_draft',requestId:'r1'},
27
+ failing('prd_review_sections_missing'));
28
+ assert.deepEqual(replies,[{requestId:'r1',error:{code:'host_request_failed'}}]);
29
+ assert.deepEqual(diagnostics,[{diagnostic:'host_request_failed',operation:'save_draft',
30
+ code:'prd_review_sections_missing'}]);
31
+ });
32
+
33
+ test('the failing operation is named so a strict field set can be told apart', async () => {
34
+ const {diagnostics}=await exchange({operation:'final_review_package',requestId:'r2'},
35
+ failing('invalid_input'));
36
+ assert.equal(diagnostics[0].operation,'final_review_package');
37
+ assert.equal(diagnostics[0].code,'invalid_input');
38
+ });
39
+
40
+ test('a success emits no diagnostic', async () => {
41
+ const {replies,diagnostics}=await exchange({operation:'status',requestId:'r3'},
42
+ async()=>({outcome:'reported'}));
43
+ assert.deepEqual(replies,[{requestId:'r3',result:{outcome:'reported'}}]);
44
+ assert.deepEqual(diagnostics,[]);
45
+ });
46
+
47
+ test('an error carrying no code still produces one line', async () => {
48
+ const {replies,diagnostics}=await exchange({operation:'advance',requestId:'r4'},
49
+ ()=>{throw new Error('anonymous failure');});
50
+ assert.deepEqual(replies,[{requestId:'r4',error:{code:'host_request_failed'}}]);
51
+ assert.deepEqual(diagnostics,[{diagnostic:'host_request_failed',operation:'advance',detail:'unavailable'}]);
52
+ });
53
+
54
+ test('the raw message never reaches either channel', async () => {
55
+ // Assembled at runtime so the safety scanner does not see a key-shaped literal.
56
+ const secret=['sk','live','0123456789abcdef'].join('-')+' leaked from provider output';
57
+ const {replies,diagnostics}=await exchange({operation:'advance',requestId:'r5'},
58
+ ()=>{throw Object.assign(new Error(secret),{code:'invalid_input'});});
59
+ const text=JSON.stringify(replies)+JSON.stringify(diagnostics);
60
+ assert.equal(text.includes(['sk','live'].join('-')),false);
61
+ assert.equal(text.includes('leaked'),false);
62
+ });
63
+
64
+ test('a malformed request is rejected without a diagnostic', async () => {
65
+ // invalid_request already says what is wrong; it never reaches host.handle.
66
+ const {replies,diagnostics}=await exchange({operation:'not_an_operation',requestId:'r6'},
67
+ ()=>{throw new Error('unreachable');});
68
+ assert.deepEqual(replies,[{requestId:null,error:{code:'invalid_request'}}]);
69
+ assert.deepEqual(diagnostics,[]);
70
+ });
71
+
72
+ test('a stderr that throws does not change the reply', async () => {
73
+ const input=new PassThrough(),output=new PassThrough();
74
+ const replies=[];
75
+ output.on('data',chunk=>{for(const line of chunk.toString().split('\n'))if(line.trim())replies.push(JSON.parse(line));});
76
+ const errorOutput={write(){throw new Error('stderr is gone');}};
77
+ const serving=serveCmAiHost({host:{handle:failing('invalid_input')},input,output,errorOutput});
78
+ input.write(JSON.stringify({operation:'advance',requestId:'r7'})+'\n');
79
+ input.end();
80
+ await serving;
81
+ assert.deepEqual(replies,[{requestId:'r7',error:{code:'host_request_failed'}}]);
82
+ });
@@ -106,6 +106,16 @@ AGENTS.md 增量写回;将学习结果、文件摘要记入已有交接字段
106
106
  `../../../runtime/task-gates.md`。子代理只能返回候选字段;由主执行者核对并落盘,
107
107
  不得让子代理写 `.reviews/`。`ready_for_review` 必须所有 verification 都是 `passed`,
108
108
  且 blockers/scope_deviation 为空;`changed_files` 使用正斜杠分隔的项目相对路径。
109
+
110
+ **交付前逐条核对任务的 `verification`**:开发请求的 `specification.task.verification`
111
+ 来自 `tasks.md` 的「验证要求」段(由 `runtime/js/cm-ai/specification-material.mjs` 提取)。
112
+ 把它当检查清单,**逐条**写出满足它的证据位置——哪个文件哪一行、哪次命令的哪段输出。
113
+ 有一条对不上就补做,补不了就写 `blocked` 说明缺口,不得交付。要求里写「分别记录
114
+ A、B、C 三种状态」就必须三种都在,写「在 X 路径上走查」就必须是 X 而不是等价替代;
115
+ 用了替身(命令桩、脚本派发事件、非指定服务器)时,在证据里显式标注边界,不要让
116
+ 结论读起来像端到端验证过。这一步不是形式:本仓库实跑统计显示末任务
117
+ (承担 feature 级走查与证据文档的那个)第一轮审查通过率仅 1/5,被打回的 P2 全部是
118
+ 「证据没满足任务里已写明的验证要求」——不是代码缺陷,是本可在此自查掉的遗漏。
109
119
  否则写 `blocked` 并停止,不进入 N4。写 handoff 前,必须以完全相同的
110
120
  `changed_files` 计算实现内容摘要:
111
121