claude-coder 1.9.2 → 1.10.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +236 -214
- package/bin/cli.js +170 -155
- package/package.json +55 -55
- package/recipes/_shared/roles/developer.md +11 -11
- package/recipes/_shared/roles/product.md +12 -12
- package/recipes/_shared/roles/tester.md +12 -12
- package/recipes/_shared/test/report-format.md +86 -86
- package/recipes/backend/base.md +27 -27
- package/recipes/backend/components/auth.md +18 -18
- package/recipes/backend/components/crud-api.md +18 -18
- package/recipes/backend/components/file-service.md +15 -15
- package/recipes/backend/manifest.json +20 -20
- package/recipes/backend/test/api-test.md +25 -25
- package/recipes/console/base.md +37 -37
- package/recipes/console/components/modal-form.md +20 -20
- package/recipes/console/components/pagination.md +17 -17
- package/recipes/console/components/search.md +17 -17
- package/recipes/console/components/table-list.md +18 -18
- package/recipes/console/components/tabs.md +14 -14
- package/recipes/console/components/tree.md +15 -15
- package/recipes/console/components/upload.md +15 -15
- package/recipes/console/manifest.json +24 -24
- package/recipes/console/test/crud-e2e.md +47 -47
- package/recipes/h5/base.md +26 -26
- package/recipes/h5/components/animation.md +11 -11
- package/recipes/h5/components/countdown.md +11 -11
- package/recipes/h5/components/share.md +11 -11
- package/recipes/h5/components/swiper.md +11 -11
- package/recipes/h5/manifest.json +21 -21
- package/recipes/h5/test/h5-e2e.md +20 -20
- package/src/commands/auth.js +420 -420
- package/src/commands/setup-modules/helpers.js +100 -100
- package/src/commands/setup-modules/index.js +25 -25
- package/src/commands/setup-modules/mcp.js +115 -115
- package/src/commands/setup-modules/provider.js +260 -260
- package/src/commands/setup-modules/safety.js +47 -47
- package/src/commands/setup-modules/simplify.js +52 -52
- package/src/commands/setup.js +172 -172
- package/src/common/assets.js +259 -245
- package/src/common/config.js +147 -125
- package/src/common/constants.js +55 -55
- package/src/common/indicator.js +260 -260
- package/src/common/interaction.js +170 -170
- package/src/common/logging.js +77 -77
- package/src/common/sdk.js +48 -50
- package/src/common/tasks.js +88 -88
- package/src/common/utils.js +214 -213
- package/src/core/coding.js +35 -33
- package/src/core/design.js +268 -0
- package/src/core/go.js +264 -264
- package/src/core/hooks.js +514 -500
- package/src/core/init.js +175 -166
- package/src/core/plan.js +194 -188
- package/src/core/prompts.js +292 -247
- package/src/core/repair.js +36 -36
- package/src/core/runner.js +471 -471
- package/src/core/scan.js +94 -93
- package/src/core/session.js +294 -280
- package/src/core/simplify.js +76 -74
- package/src/core/state.js +120 -105
- package/src/index.js +80 -76
- package/templates/{codingSystem.md → coding/system.md} +65 -65
- package/templates/{codingUser.md → coding/user.md} +18 -17
- package/templates/design/base.md +103 -0
- package/templates/design/fixSystem.md +71 -0
- package/templates/design/fixUser.md +3 -0
- package/templates/design/init.md +304 -0
- package/templates/design/system.md +108 -0
- package/templates/design/user.md +11 -0
- package/templates/{goSystem.md → go/system.md} +130 -130
- package/templates/{bash-process.md → other/bash-process.md} +12 -12
- package/templates/{coreProtocol.md → other/coreProtocol.md} +30 -29
- package/templates/{guidance.json → other/guidance.json} +72 -72
- package/templates/{requirements.example.md → other/requirements.example.md} +57 -57
- package/templates/{test_rule.md → other/test_rule.md} +192 -194
- package/templates/{web-testing.md → other/web-testing.md} +17 -17
- package/templates/{planSystem.md → plan/system.md} +78 -78
- package/templates/{planUser.md → plan/user.md} +10 -9
- package/templates/{scanSystem.md → scan/system.md} +120 -120
- package/templates/{scanUser.md → scan/user.md} +10 -10
- package/types/index.d.ts +217 -217
package/src/core/runner.js
CHANGED
|
@@ -1,471 +1,471 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
const { execSync } = require('child_process');
|
|
4
|
-
const readline = require('readline');
|
|
5
|
-
const { log } = require('../common/config');
|
|
6
|
-
const { assets } = require('../common/assets');
|
|
7
|
-
const { loadTasks, saveTasks, getFeatures, getStats, printStats } = require('../common/tasks');
|
|
8
|
-
const { getGitHead, sleep, tryPush, killServices } = require('../common/utils');
|
|
9
|
-
const { RETRY } = require('../common/constants');
|
|
10
|
-
const {
|
|
11
|
-
loadState, saveState, selectNextTask, isAllDone,
|
|
12
|
-
appendProgress, incrementSession, markSimplifyDone,
|
|
13
|
-
} = require('./state');
|
|
14
|
-
|
|
15
|
-
const MAX_RETRY = RETRY.MAX_ATTEMPTS;
|
|
16
|
-
|
|
17
|
-
// ─── Display Helpers ──────────────────────────────────────
|
|
18
|
-
|
|
19
|
-
function printBanner(dryRun) {
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
});
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
if (
|
|
103
|
-
log('
|
|
104
|
-
return { valid: false, reason: '
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
const
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
}
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
log('
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
}
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
}
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
})
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
if (
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
}
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
return
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
}
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
}
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
}
|
|
361
|
-
|
|
362
|
-
// ───
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
} else {
|
|
453
|
-
state = await onFailure(session, { headBefore, taskId, sessionResult, validateResult, ...state });
|
|
454
|
-
}
|
|
455
|
-
|
|
456
|
-
if (pauseEvery > 0 && session % pauseEvery === 0) {
|
|
457
|
-
console.log('');
|
|
458
|
-
printStats();
|
|
459
|
-
if (!await promptContinue()) {
|
|
460
|
-
log('info', '手动停止');
|
|
461
|
-
break;
|
|
462
|
-
}
|
|
463
|
-
}
|
|
464
|
-
}
|
|
465
|
-
|
|
466
|
-
killServices(projectRoot);
|
|
467
|
-
printEndBanner();
|
|
468
|
-
printStats();
|
|
469
|
-
}
|
|
470
|
-
|
|
471
|
-
module.exports = { executeRun };
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const { execSync } = require('child_process');
|
|
4
|
+
const readline = require('readline');
|
|
5
|
+
const { log, COLOR, printModeBanner } = require('../common/config');
|
|
6
|
+
const { assets } = require('../common/assets');
|
|
7
|
+
const { loadTasks, saveTasks, getFeatures, getStats, printStats } = require('../common/tasks');
|
|
8
|
+
const { getGitHead, sleep, tryPush, killServices } = require('../common/utils');
|
|
9
|
+
const { RETRY } = require('../common/constants');
|
|
10
|
+
const {
|
|
11
|
+
loadState, saveState, selectNextTask, isAllDone,
|
|
12
|
+
appendProgress, incrementSession, markSimplifyDone,
|
|
13
|
+
} = require('./state');
|
|
14
|
+
|
|
15
|
+
const MAX_RETRY = RETRY.MAX_ATTEMPTS;
|
|
16
|
+
|
|
17
|
+
// ─── Display Helpers ──────────────────────────────────────
|
|
18
|
+
|
|
19
|
+
function printBanner(dryRun, config, maxSessions) {
|
|
20
|
+
const mode = dryRun ? '预览模式' : `max: ${maxSessions}`;
|
|
21
|
+
printModeBanner('run', mode, config?.model);
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
function _progressBar(done, total, width = 24) {
|
|
25
|
+
if (total === 0) return `${COLOR.dim}[${'░'.repeat(width)}]${COLOR.reset}`;
|
|
26
|
+
const filled = Math.round(done / total * width);
|
|
27
|
+
return `${COLOR.green}[${'█'.repeat(filled)}${COLOR.dim}${'░'.repeat(width - filled)}${COLOR.reset}${COLOR.green}]${COLOR.reset}`;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
function printSessionHeader(session, maxSessions, taskData, taskId) {
|
|
31
|
+
const stats = getStats(taskData);
|
|
32
|
+
const task = taskId ? getFeatures(taskData).find(f => f.id === taskId) : null;
|
|
33
|
+
const bar = _progressBar(stats.done, stats.total);
|
|
34
|
+
|
|
35
|
+
console.error('');
|
|
36
|
+
console.error(`${COLOR.cyan}┌─ Session ${session} / ${maxSessions} ${'─'.repeat(32)}┐${COLOR.reset}`);
|
|
37
|
+
if (task) {
|
|
38
|
+
console.error(`${COLOR.cyan}│${COLOR.reset} 任务: ${COLOR.bold}${task.id}${COLOR.reset} ${COLOR.dim}-${COLOR.reset} ${task.description || ''}`);
|
|
39
|
+
}
|
|
40
|
+
console.error(`${COLOR.cyan}│${COLOR.reset} 进度: ${bar} ${stats.done}/${stats.total} ${COLOR.green}✔${stats.done}${COLOR.reset} ${COLOR.yellow}○${stats.pending}${COLOR.reset} ${COLOR.red}✘${stats.failed}${COLOR.reset}`);
|
|
41
|
+
console.error(`${COLOR.cyan}└${'─'.repeat(46)}┘${COLOR.reset}`);
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
function printDryRun(taskData) {
|
|
45
|
+
const next = selectNextTask(taskData);
|
|
46
|
+
log('info', `[DRY-RUN] 下一个任务: ${next ? `${next.id} - ${next.description}` : '无待处理任务'}`);
|
|
47
|
+
|
|
48
|
+
if (!next) {
|
|
49
|
+
log('ok', '[DRY-RUN] 无可执行任务,预览结束');
|
|
50
|
+
return;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
console.log('');
|
|
54
|
+
log('info', '[DRY-RUN] 任务队列:');
|
|
55
|
+
const features = getFeatures(taskData);
|
|
56
|
+
for (const f of features) {
|
|
57
|
+
const st = f.status || 'unknown';
|
|
58
|
+
const icon = { done: '✓', in_progress: '▸', pending: '○', failed: '✗', testing: '◇' }[st] || '?';
|
|
59
|
+
const color = { done: COLOR.green, failed: COLOR.red, in_progress: COLOR.blue, testing: COLOR.yellow, pending: COLOR.dim }[st] || '';
|
|
60
|
+
log('info', ` ${color}${icon}${COLOR.reset} [${st.padEnd(11)}] ${f.id} - ${f.description || ''}`);
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
function printEndBanner() {
|
|
65
|
+
console.error('');
|
|
66
|
+
console.error(`${COLOR.cyan}╔══════════════════════════════════════════════╗${COLOR.reset}`);
|
|
67
|
+
console.error(`${COLOR.cyan}║${COLOR.reset} ${COLOR.bold}运行结束${COLOR.reset}`);
|
|
68
|
+
console.error(`${COLOR.cyan}╚══════════════════════════════════════════════╝${COLOR.reset}`);
|
|
69
|
+
console.error('');
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
async function promptContinue() {
|
|
73
|
+
if (!process.stdin.isTTY) return true;
|
|
74
|
+
const rl = readline.createInterface({ input: process.stdin, output: process.stdout });
|
|
75
|
+
return new Promise(resolve => {
|
|
76
|
+
rl.question('是否继续?(y/n) ', answer => {
|
|
77
|
+
rl.close();
|
|
78
|
+
resolve(/^[Yy]/.test(answer.trim()));
|
|
79
|
+
});
|
|
80
|
+
});
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
// ─── Lifecycle: Snapshot ──────────────────────────────────
|
|
84
|
+
|
|
85
|
+
function snapshot(projectRoot, taskData) {
|
|
86
|
+
const nextTask = selectNextTask(taskData);
|
|
87
|
+
const taskId = nextTask?.id || 'unknown';
|
|
88
|
+
|
|
89
|
+
const state = loadState();
|
|
90
|
+
state.current_task_id = taskId;
|
|
91
|
+
saveState(state);
|
|
92
|
+
|
|
93
|
+
return {
|
|
94
|
+
headBefore: getGitHead(projectRoot),
|
|
95
|
+
taskId,
|
|
96
|
+
};
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
// ─── Lifecycle: Validation ────────────────────────────────
|
|
100
|
+
|
|
101
|
+
function _validateSessionResult() {
|
|
102
|
+
if (!assets.exists('sessionResult')) {
|
|
103
|
+
log('error', 'Agent 未生成 session_result.json');
|
|
104
|
+
return { valid: false, reason: 'session_result.json 不存在' };
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
const raw = assets.readJson('sessionResult', null);
|
|
108
|
+
if (raw === null) {
|
|
109
|
+
log('warn', 'session_result.json 解析失败');
|
|
110
|
+
return { valid: false, reason: 'JSON 解析失败', rawContent: assets.read('sessionResult') };
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
const data = raw.current && typeof raw.current === 'object' ? raw.current : raw;
|
|
114
|
+
const { TASK_STATUSES } = require('../common/constants');
|
|
115
|
+
|
|
116
|
+
const required = ['session_result', 'status_after'];
|
|
117
|
+
const missing = required.filter(k => !(k in data));
|
|
118
|
+
if (missing.length > 0) {
|
|
119
|
+
log('warn', `session_result.json 缺少字段: ${missing.join(', ')}`);
|
|
120
|
+
return { valid: false, reason: `缺少字段: ${missing.join(', ')}`, data };
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
if (!['success', 'failed'].includes(data.session_result)) {
|
|
124
|
+
return { valid: false, reason: `无效 session_result: ${data.session_result}`, data };
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
if (!TASK_STATUSES.includes(data.status_after)) {
|
|
128
|
+
return { valid: false, reason: `无效 status_after: ${data.status_after}`, data };
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
const level = data.session_result === 'success' ? 'ok' : 'warn';
|
|
132
|
+
log(level, `session_result.json 合法 (${data.session_result})`);
|
|
133
|
+
return { valid: true, data };
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
function _checkGitProgress(headBefore, projectRoot) {
|
|
137
|
+
if (!headBefore) {
|
|
138
|
+
log('info', '未提供 head_before,跳过 git 检查');
|
|
139
|
+
return { hasCommit: false, warning: false };
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
const headAfter = getGitHead(projectRoot);
|
|
143
|
+
|
|
144
|
+
if (headBefore === headAfter) {
|
|
145
|
+
log('warn', '本次会话没有新的 git 提交');
|
|
146
|
+
return { hasCommit: false, warning: true };
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
try {
|
|
150
|
+
const msg = execSync('git log --oneline -1', { cwd: projectRoot, encoding: 'utf8' }).trim();
|
|
151
|
+
log('ok', `检测到新提交: ${msg}`);
|
|
152
|
+
} catch { /* ignore */ }
|
|
153
|
+
|
|
154
|
+
return { hasCommit: true, warning: false };
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
function _inferFromTasks(taskId) {
|
|
158
|
+
if (!taskId) return null;
|
|
159
|
+
const data = loadTasks();
|
|
160
|
+
if (!data) return null;
|
|
161
|
+
const task = getFeatures(data).find(f => f.id === taskId);
|
|
162
|
+
return task ? task.status : null;
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
async function validate(config, headBefore, taskId) {
|
|
166
|
+
const projectRoot = assets.projectRoot;
|
|
167
|
+
log('info', '校验中...');
|
|
168
|
+
|
|
169
|
+
let srResult = _validateSessionResult();
|
|
170
|
+
const gitResult = _checkGitProgress(headBefore, projectRoot);
|
|
171
|
+
|
|
172
|
+
if (!srResult.valid && srResult.rawContent) {
|
|
173
|
+
const srPath = assets.path('sessionResult');
|
|
174
|
+
if (srPath) {
|
|
175
|
+
const { executeRepair } = require('./repair');
|
|
176
|
+
await executeRepair(config, srPath);
|
|
177
|
+
srResult = _validateSessionResult();
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
let fatal = false;
|
|
182
|
+
let hasWarnings = false;
|
|
183
|
+
|
|
184
|
+
if (srResult.valid) {
|
|
185
|
+
hasWarnings = gitResult.warning;
|
|
186
|
+
} else {
|
|
187
|
+
if (gitResult.hasCommit) {
|
|
188
|
+
const taskStatus = _inferFromTasks(taskId);
|
|
189
|
+
if (taskStatus === 'done' || taskStatus === 'testing') {
|
|
190
|
+
log('warn', `session_result.json 异常,但 tasks.json 显示 ${taskId} 已 ${taskStatus},且有新提交,降级为警告`);
|
|
191
|
+
} else {
|
|
192
|
+
log('warn', 'session_result.json 异常,但有新提交,降级为警告(不回滚代码)');
|
|
193
|
+
}
|
|
194
|
+
hasWarnings = true;
|
|
195
|
+
} else {
|
|
196
|
+
log('error', '无新提交且 session_result.json 异常,视为致命');
|
|
197
|
+
fatal = true;
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
if (fatal) {
|
|
202
|
+
log('error', '校验失败 (致命)');
|
|
203
|
+
} else if (hasWarnings) {
|
|
204
|
+
log('warn', '校验通过 (有警告)');
|
|
205
|
+
} else {
|
|
206
|
+
log('ok', '校验通过 ✓');
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
const reason = fatal ? (srResult.reason || '无新提交且 session_result.json 异常') : '';
|
|
210
|
+
return { fatal, hasWarnings, sessionData: srResult.data, reason };
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
// ─── Lifecycle: Rollback ──────────────────────────────────
|
|
214
|
+
|
|
215
|
+
async function rollback(headBefore, reason) {
|
|
216
|
+
if (!headBefore || headBefore === 'none') return;
|
|
217
|
+
|
|
218
|
+
const projectRoot = assets.projectRoot;
|
|
219
|
+
killServices(projectRoot);
|
|
220
|
+
if (process.platform === 'win32') await sleep(1500);
|
|
221
|
+
|
|
222
|
+
const gitEnv = { ...process.env, GIT_TERMINAL_PROMPT: '0' };
|
|
223
|
+
|
|
224
|
+
log('warn', `回滚到 ${headBefore} ...`);
|
|
225
|
+
|
|
226
|
+
let success = false;
|
|
227
|
+
for (let attempt = 1; attempt <= 2; attempt++) {
|
|
228
|
+
try {
|
|
229
|
+
execSync(`git reset --hard ${headBefore}`, { cwd: projectRoot, stdio: 'pipe', env: gitEnv });
|
|
230
|
+
execSync('git clean -fd', { cwd: projectRoot, stdio: 'pipe', env: gitEnv });
|
|
231
|
+
log('ok', '回滚完成');
|
|
232
|
+
success = true;
|
|
233
|
+
break;
|
|
234
|
+
} catch (err) {
|
|
235
|
+
if (attempt === 1) {
|
|
236
|
+
log('warn', `回滚首次失败,等待后重试: ${err.message}`);
|
|
237
|
+
await sleep(2000);
|
|
238
|
+
} else {
|
|
239
|
+
log('error', `回滚失败: ${err.message}`);
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
appendProgress({
|
|
245
|
+
type: 'rollback',
|
|
246
|
+
timestamp: _timestamp(),
|
|
247
|
+
reason: reason || 'harness 校验失败',
|
|
248
|
+
rollbackTo: headBefore,
|
|
249
|
+
success,
|
|
250
|
+
});
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
// ─── Lifecycle: Retry / Skip ──────────────────────────────
|
|
254
|
+
|
|
255
|
+
function _markTaskFailed(taskId) {
|
|
256
|
+
if (!taskId) return;
|
|
257
|
+
const data = loadTasks();
|
|
258
|
+
if (!data) return;
|
|
259
|
+
const features = getFeatures(data);
|
|
260
|
+
const task = features.find(f => f.id === taskId);
|
|
261
|
+
if (task && task.status !== 'done') {
|
|
262
|
+
task.status = 'failed';
|
|
263
|
+
saveTasks(data);
|
|
264
|
+
log('warn', `已将任务 ${taskId} 强制标记为 failed`);
|
|
265
|
+
}
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
async function _handleRetryOrSkip(session, {
|
|
269
|
+
headBefore, taskId, sessionResult, consecutiveFailures, result, reason, lastFailMsg,
|
|
270
|
+
}) {
|
|
271
|
+
const newFailures = consecutiveFailures + 1;
|
|
272
|
+
const exceeded = newFailures >= MAX_RETRY;
|
|
273
|
+
|
|
274
|
+
await rollback(headBefore, reason);
|
|
275
|
+
|
|
276
|
+
if (exceeded) {
|
|
277
|
+
log('error', `连续失败 ${MAX_RETRY} 次,跳过当前任务`);
|
|
278
|
+
_markTaskFailed(taskId);
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
const entry = { session, timestamp: _timestamp(), result, cost: sessionResult.cost, taskId };
|
|
282
|
+
if (result === 'fatal') entry.reason = reason;
|
|
283
|
+
appendProgress(entry);
|
|
284
|
+
|
|
285
|
+
if (exceeded) return { consecutiveFailures: 0, lastFailReason: '' };
|
|
286
|
+
return { consecutiveFailures: newFailures, lastFailReason: lastFailMsg };
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
// ─── Lifecycle: Session Outcome ───────────────────────────
|
|
290
|
+
|
|
291
|
+
async function onSuccess(session, { taskId, sessionResult, validateResult }) {
|
|
292
|
+
incrementSession();
|
|
293
|
+
|
|
294
|
+
appendProgress({
|
|
295
|
+
session,
|
|
296
|
+
timestamp: _timestamp(),
|
|
297
|
+
result: 'success',
|
|
298
|
+
cost: sessionResult.cost,
|
|
299
|
+
taskId,
|
|
300
|
+
statusAfter: validateResult.sessionData?.status_after || null,
|
|
301
|
+
notes: validateResult.sessionData?.notes || null,
|
|
302
|
+
});
|
|
303
|
+
|
|
304
|
+
return { consecutiveFailures: 0, lastFailReason: '' };
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
async function onFailure(session, { headBefore, taskId, sessionResult, validateResult, consecutiveFailures }) {
|
|
308
|
+
const reason = validateResult.reason || '校验失败';
|
|
309
|
+
log('error', `Session ${session} 校验失败 (连续失败: ${consecutiveFailures + 1}/${MAX_RETRY})`);
|
|
310
|
+
return _handleRetryOrSkip(session, {
|
|
311
|
+
headBefore, taskId, sessionResult, consecutiveFailures,
|
|
312
|
+
result: 'fatal', reason,
|
|
313
|
+
lastFailMsg: `上次校验失败: ${reason},代码已回滚`,
|
|
314
|
+
});
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
async function onStall(session, { headBefore, taskId, sessionResult, consecutiveFailures, config }) {
|
|
318
|
+
log('warn', `Session ${session} 因停顿超时中断,尝试校验任务是否已完成...`);
|
|
319
|
+
|
|
320
|
+
const validateResult = await validate(config, headBefore, taskId);
|
|
321
|
+
|
|
322
|
+
if (!validateResult.fatal) {
|
|
323
|
+
log('ok', `停顿超时但任务已完成,按成功处理${validateResult.hasWarnings ? ' (有警告)' : ''}`);
|
|
324
|
+
return onSuccess(session, { taskId, sessionResult, validateResult });
|
|
325
|
+
}
|
|
326
|
+
|
|
327
|
+
log('warn', '停顿超时且校验未通过,回滚重试');
|
|
328
|
+
return _handleRetryOrSkip(session, {
|
|
329
|
+
headBefore, taskId, sessionResult, consecutiveFailures,
|
|
330
|
+
result: 'stalled', reason: '停顿超时',
|
|
331
|
+
lastFailMsg: '上次会话停顿超时,已回滚',
|
|
332
|
+
});
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
// ─── Lifecycle: Simplify Scheduling ───────────────────────
|
|
336
|
+
|
|
337
|
+
function shouldSimplify(config) {
|
|
338
|
+
const { simplifyInterval } = config;
|
|
339
|
+
if (simplifyInterval <= 0) return false;
|
|
340
|
+
const state = loadState();
|
|
341
|
+
return state.session_count % simplifyInterval === 0;
|
|
342
|
+
}
|
|
343
|
+
|
|
344
|
+
function needsFinalSimplify(config) {
|
|
345
|
+
const { simplifyInterval } = config;
|
|
346
|
+
if (simplifyInterval <= 0) return false;
|
|
347
|
+
const state = loadState();
|
|
348
|
+
return state.last_simplify_session < state.session_count;
|
|
349
|
+
}
|
|
350
|
+
|
|
351
|
+
async function tryRunSimplify(config, msg) {
|
|
352
|
+
log('info', msg || `每 ${config.simplifyInterval} 个成功 session 运行代码审查...`);
|
|
353
|
+
try {
|
|
354
|
+
const { executeSimplify } = require('./simplify');
|
|
355
|
+
await executeSimplify(config, null, { n: config.simplifyCommits });
|
|
356
|
+
markSimplifyDone();
|
|
357
|
+
} catch (err) {
|
|
358
|
+
log('warn', `代码审查失败,跳过: ${err.message}`);
|
|
359
|
+
}
|
|
360
|
+
}
|
|
361
|
+
|
|
362
|
+
// ─── Utilities ────────────────────────────────────────────
|
|
363
|
+
|
|
364
|
+
function _timestamp() {
|
|
365
|
+
return new Date().toISOString().replace(/[-:T]/g, '').slice(0, 12);
|
|
366
|
+
}
|
|
367
|
+
|
|
368
|
+
// ─── Main Orchestration Loop ──────────────────────────────
|
|
369
|
+
|
|
370
|
+
async function executeRun(config, opts = {}) {
|
|
371
|
+
if (!assets.exists('tasks')) {
|
|
372
|
+
throw new Error('tasks.json 不存在,请先运行 claude-coder plan 生成任务');
|
|
373
|
+
}
|
|
374
|
+
|
|
375
|
+
const projectRoot = assets.projectRoot;
|
|
376
|
+
const dryRun = opts.dryRun || false;
|
|
377
|
+
const maxSessions = opts.max || 50;
|
|
378
|
+
const pauseEvery = opts.pause ?? 0;
|
|
379
|
+
printBanner(dryRun, config, maxSessions);
|
|
380
|
+
|
|
381
|
+
printStats();
|
|
382
|
+
|
|
383
|
+
log('info', `开始编码循环 (最多 ${maxSessions} 个会话) ...`);
|
|
384
|
+
|
|
385
|
+
let state = { consecutiveFailures: 0, lastFailReason: '' };
|
|
386
|
+
|
|
387
|
+
for (let session = 1; session <= maxSessions; session++) {
|
|
388
|
+
let taskData = loadTasks();
|
|
389
|
+
if (!taskData) {
|
|
390
|
+
const tasksPath = assets.path('tasks');
|
|
391
|
+
if (tasksPath) {
|
|
392
|
+
const { executeRepair } = require('./repair');
|
|
393
|
+
await executeRepair(config, tasksPath);
|
|
394
|
+
}
|
|
395
|
+
taskData = loadTasks();
|
|
396
|
+
if (!taskData) {
|
|
397
|
+
log('error', 'tasks.json 无法读取且修复失败,终止循环');
|
|
398
|
+
break;
|
|
399
|
+
}
|
|
400
|
+
}
|
|
401
|
+
|
|
402
|
+
if (isAllDone(taskData)) {
|
|
403
|
+
if (!dryRun) {
|
|
404
|
+
if (needsFinalSimplify(config)) {
|
|
405
|
+
await tryRunSimplify(config, '所有任务完成,运行最终代码审查...');
|
|
406
|
+
}
|
|
407
|
+
tryPush(projectRoot);
|
|
408
|
+
}
|
|
409
|
+
console.error('');
|
|
410
|
+
log('ok', '所有任务已完成!');
|
|
411
|
+
printStats();
|
|
412
|
+
break;
|
|
413
|
+
}
|
|
414
|
+
|
|
415
|
+
const { headBefore, taskId } = dryRun ? { headBefore: null, taskId: null } : snapshot(projectRoot, taskData);
|
|
416
|
+
|
|
417
|
+
printSessionHeader(session, maxSessions, taskData, taskId);
|
|
418
|
+
|
|
419
|
+
if (dryRun) {
|
|
420
|
+
printDryRun(taskData);
|
|
421
|
+
break;
|
|
422
|
+
}
|
|
423
|
+
|
|
424
|
+
const { executeCoding } = require('./coding');
|
|
425
|
+
const sessionResult = await executeCoding(config, session, {
|
|
426
|
+
projectRoot,
|
|
427
|
+
taskId,
|
|
428
|
+
consecutiveFailures: state.consecutiveFailures,
|
|
429
|
+
maxSessions,
|
|
430
|
+
lastValidateLog: state.lastFailReason,
|
|
431
|
+
continue: true,
|
|
432
|
+
});
|
|
433
|
+
|
|
434
|
+
if (sessionResult.stalled) {
|
|
435
|
+
state = await onStall(session, { headBefore, taskId, sessionResult, config, ...state });
|
|
436
|
+
if (state.consecutiveFailures === 0) {
|
|
437
|
+
if (shouldSimplify(config)) await tryRunSimplify(config);
|
|
438
|
+
}
|
|
439
|
+
continue;
|
|
440
|
+
}
|
|
441
|
+
|
|
442
|
+
const validateResult = await validate(config, headBefore, taskId);
|
|
443
|
+
|
|
444
|
+
if (!validateResult.fatal) {
|
|
445
|
+
const level = validateResult.hasWarnings ? 'warn' : 'ok';
|
|
446
|
+
log(level, `Session ${session} ${validateResult.hasWarnings ? '校验通过 (有警告)' : '校验通过 ✓'}`);
|
|
447
|
+
state = await onSuccess(session, { taskId, sessionResult, validateResult });
|
|
448
|
+
|
|
449
|
+
if (shouldSimplify(config)) {
|
|
450
|
+
await tryRunSimplify(config);
|
|
451
|
+
}
|
|
452
|
+
} else {
|
|
453
|
+
state = await onFailure(session, { headBefore, taskId, sessionResult, validateResult, ...state });
|
|
454
|
+
}
|
|
455
|
+
|
|
456
|
+
if (pauseEvery > 0 && session % pauseEvery === 0) {
|
|
457
|
+
console.log('');
|
|
458
|
+
printStats();
|
|
459
|
+
if (!await promptContinue()) {
|
|
460
|
+
log('info', '手动停止');
|
|
461
|
+
break;
|
|
462
|
+
}
|
|
463
|
+
}
|
|
464
|
+
}
|
|
465
|
+
|
|
466
|
+
killServices(projectRoot);
|
|
467
|
+
printEndBanner();
|
|
468
|
+
printStats();
|
|
469
|
+
}
|
|
470
|
+
|
|
471
|
+
module.exports = { executeRun };
|