mpt-workflow 0.6.0 → 0.7.1
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/CHANGELOG.md +91 -1
- package/README.md +64 -17
- package/README.zh-CN.md +30 -11
- package/dist/cli.js +68 -32
- package/dist/cli.js.map +1 -1
- package/dist/core/acl.js +32 -9
- package/dist/core/acl.js.map +1 -1
- package/dist/core/config.js +2 -2
- package/dist/core/config.js.map +1 -1
- package/dist/core/demo.js +1 -1
- package/dist/core/demo.js.map +1 -1
- package/dist/core/docs.js +10 -8
- package/dist/core/docs.js.map +1 -1
- package/dist/core/fsutil.js +15 -0
- package/dist/core/fsutil.js.map +1 -1
- package/dist/core/gc.js +20 -7
- package/dist/core/gc.js.map +1 -1
- package/dist/core/git.js +17 -2
- package/dist/core/git.js.map +1 -1
- package/dist/core/hooks.js +128 -55
- package/dist/core/hooks.js.map +1 -1
- package/dist/core/init.js +58 -13
- package/dist/core/init.js.map +1 -1
- package/dist/core/markdown.js +1 -3
- package/dist/core/markdown.js.map +1 -1
- package/dist/core/messages.js +28 -15
- package/dist/core/messages.js.map +1 -1
- package/dist/core/questions.js +2 -2
- package/dist/core/questions.js.map +1 -1
- package/dist/core/state.js +11 -3
- package/dist/core/state.js.map +1 -1
- package/dist/core/types.js +4 -1
- package/dist/core/types.js.map +1 -1
- package/dist/core/workflow.js +195 -91
- package/dist/core/workflow.js.map +1 -1
- package/dist/mcp/server.js +95 -24
- package/dist/mcp/server.js.map +1 -1
- package/dist/version.js +1 -1
- package/dist/web/public/index.html +168 -45
- package/dist/web/server.js +93 -46
- package/dist/web/server.js.map +1 -1
- package/package.json +8 -4
package/dist/core/workflow.js
CHANGED
|
@@ -1,15 +1,16 @@
|
|
|
1
|
-
import path from 'node:path';
|
|
2
1
|
import { spawn } from 'node:child_process';
|
|
3
|
-
import
|
|
4
|
-
import { loadConfig, saveConfig, findRoot, runtimeDir, evidenceDir, } from './config.js';
|
|
5
|
-
import { listTodoFiles, parsePlan, parseTodo, parseEvidence, setTodoStatus, setTodoSha, appendEvidenceSection, statusLabel, } from './docs.js';
|
|
6
|
-
import { parseQuestionsFile, questionsFile, updateQuestion, } from './questions.js';
|
|
7
|
-
import { gitInfo, allChangedFiles, workingTreeHash, workingTreeOid, stageAll, commit as gitCommit, diffStat, } from './git.js';
|
|
2
|
+
import path from 'node:path';
|
|
8
3
|
import { checkAcl } from './acl.js';
|
|
9
|
-
import {
|
|
4
|
+
import { evidenceDir, findRoot, loadConfig, runtimeDir, saveConfig } from './config.js';
|
|
5
|
+
import { appendEvidenceSection, listTodoFiles, parseEvidence, parsePlan, parseTodo, setTodoSha, setTodoStatus, statusLabel, } from './docs.js';
|
|
6
|
+
import { ensureDir, exists, isoNow, readText, readTextRequired, truncate, within, writeAtomic, } from './fsutil.js';
|
|
10
7
|
import { dirSize, formatBytes } from './gc.js';
|
|
11
|
-
import {
|
|
12
|
-
import {
|
|
8
|
+
import { allChangedFiles, diffStat, commit as gitCommit, gitInfo, stageAll, stagedTreeHash, workingTreeHash, workingTreeOid, } from './git.js';
|
|
9
|
+
import { matchGlob } from './glob.js';
|
|
10
|
+
import { appendToSection } from './markdown.js';
|
|
11
|
+
import { actorFromRole, actorKey, MessageStore } from './messages.js';
|
|
12
|
+
import { parseQuestionsFile, questionsFile, updateQuestion } from './questions.js';
|
|
13
|
+
import { createApproval, listApprovals, logEvent, readApproval, withLock, writeApproval, } from './state.js';
|
|
13
14
|
export class Workflow {
|
|
14
15
|
root;
|
|
15
16
|
cfg;
|
|
@@ -119,7 +120,7 @@ export class Workflow {
|
|
|
119
120
|
}
|
|
120
121
|
/** Answer a question: update the file, notify the asker, clear the blocker. */
|
|
121
122
|
answerQuestion(q, answer, resolution, by) {
|
|
122
|
-
updateQuestion(q.file, q.id, { status: 'answered', answer, resolution });
|
|
123
|
+
withLock(this.root, 'questions', () => updateQuestion(q.file, q.id, { status: 'answered', answer, resolution }));
|
|
123
124
|
const asker = actorFromRole(String(q.from), q.repo);
|
|
124
125
|
try {
|
|
125
126
|
this.msgs.send({
|
|
@@ -146,7 +147,7 @@ export class Workflow {
|
|
|
146
147
|
}
|
|
147
148
|
/** Close a question: update the file, notify the other party. */
|
|
148
149
|
closeQuestion(q, resolution, by) {
|
|
149
|
-
updateQuestion(q.file, q.id, { status: 'closed', resolution });
|
|
150
|
+
withLock(this.root, 'questions', () => updateQuestion(q.file, q.id, { status: 'closed', resolution }));
|
|
150
151
|
const asker = actorFromRole(String(q.from), q.repo);
|
|
151
152
|
const addressee = actorFromRole(String(q.to), q.repo);
|
|
152
153
|
const other = actorKey(by) === actorKey(asker) ? addressee : asker;
|
|
@@ -232,14 +233,15 @@ export class Workflow {
|
|
|
232
233
|
problems.push('PLAN 中未解析到任何 M,且无 TODO-Mn.md');
|
|
233
234
|
if (git.dirty)
|
|
234
235
|
problems.push(`工作区有未提交改动: ${git.changed.length + git.untracked.length} 个文件`);
|
|
235
|
-
|
|
236
|
-
|
|
236
|
+
const acceptedMilestones = milestones.filter((m) => m.status === 'accepted');
|
|
237
|
+
if (acceptedMilestones.length) {
|
|
238
|
+
problems.push(`${acceptedMilestones.map((m) => m.id).join(', ')} 已验收但尚未提交`);
|
|
239
|
+
}
|
|
237
240
|
for (const q of questions.filter((q) => q.blocking && q.status === 'open')) {
|
|
238
241
|
problems.push(`阻塞问题未回答: ${q.id}`);
|
|
239
242
|
}
|
|
240
243
|
// phase: explicit PLAN phase, else the phase that lists this repo
|
|
241
|
-
const phase = plan?.phase ||
|
|
242
|
-
this.cfg.phases.find((p) => p.repos.includes(id))?.id;
|
|
244
|
+
const phase = plan?.phase || this.cfg.phases.find((p) => p.repos.includes(id))?.id;
|
|
243
245
|
return {
|
|
244
246
|
id,
|
|
245
247
|
path: conf.path,
|
|
@@ -288,12 +290,13 @@ export class Workflow {
|
|
|
288
290
|
}
|
|
289
291
|
nextActions(repos, blocking) {
|
|
290
292
|
const out = [];
|
|
291
|
-
const
|
|
292
|
-
|
|
293
|
+
for (const q of blocking) {
|
|
294
|
+
const to = String(q.to).toLowerCase();
|
|
293
295
|
out.push({
|
|
294
|
-
actor:
|
|
296
|
+
actor: to === 'plan' ? 'plan' : to === 'user' ? 'user' : 'main',
|
|
295
297
|
action: '回答阻塞问题',
|
|
296
298
|
detail: `${q.id} (${q.from} → ${q.to}):${q.context.split('\n')[0].slice(0, 120)}`,
|
|
299
|
+
repo: q.repo,
|
|
297
300
|
question: q.id,
|
|
298
301
|
blocking: true,
|
|
299
302
|
});
|
|
@@ -321,6 +324,18 @@ export class Workflow {
|
|
|
321
324
|
});
|
|
322
325
|
return out;
|
|
323
326
|
}
|
|
327
|
+
const pendingCommit = active.milestones.filter((x) => x.status === 'accepted');
|
|
328
|
+
if (pendingCommit.length) {
|
|
329
|
+
out.push({
|
|
330
|
+
actor: 'user',
|
|
331
|
+
action: '评审并 commit',
|
|
332
|
+
detail: `${pendingCommit.map((x) => x.id).join(', ')} 已验收,等待 review diff 后批准提交`,
|
|
333
|
+
repo: activeId,
|
|
334
|
+
milestone: pendingCommit[pendingCommit.length - 1].id,
|
|
335
|
+
blocking: false,
|
|
336
|
+
});
|
|
337
|
+
return out;
|
|
338
|
+
}
|
|
324
339
|
const m = active.currentMilestone;
|
|
325
340
|
if (active.milestones.length === 0) {
|
|
326
341
|
out.push({
|
|
@@ -408,6 +423,9 @@ export class Workflow {
|
|
|
408
423
|
throw new Error(`仓库目录不存在: ${ref.abs}`);
|
|
409
424
|
}
|
|
410
425
|
withLock(this.root, 'program', () => {
|
|
426
|
+
const fresh = loadConfig(this.root);
|
|
427
|
+
if (fresh)
|
|
428
|
+
this.cfg = fresh;
|
|
411
429
|
this.cfg.active_repo = id;
|
|
412
430
|
this.save();
|
|
413
431
|
});
|
|
@@ -427,18 +445,20 @@ export class Workflow {
|
|
|
427
445
|
}
|
|
428
446
|
}
|
|
429
447
|
updatePhase(id, status) {
|
|
430
|
-
const p = this.cfg.phases.find((x) => x.id === id);
|
|
431
|
-
if (!p)
|
|
432
|
-
throw new Error(`未知阶段 ${id}`);
|
|
433
448
|
withLock(this.root, 'program', () => {
|
|
434
|
-
const
|
|
449
|
+
const fresh = loadConfig(this.root);
|
|
450
|
+
if (fresh)
|
|
451
|
+
this.cfg = fresh;
|
|
452
|
+
const p = this.cfg.phases.find((x) => x.id === id);
|
|
453
|
+
if (!p)
|
|
454
|
+
throw new Error(`未知阶段 ${id}`);
|
|
435
455
|
// keep only one active phase
|
|
436
456
|
if (status === 'active') {
|
|
437
457
|
for (const q of this.cfg.phases)
|
|
438
458
|
if (q.status === 'active')
|
|
439
459
|
q.status = 'accepted';
|
|
440
460
|
}
|
|
441
|
-
|
|
461
|
+
p.status = status;
|
|
442
462
|
this.save();
|
|
443
463
|
});
|
|
444
464
|
}
|
|
@@ -452,12 +472,23 @@ export class Workflow {
|
|
|
452
472
|
throw new Error(`${repoId} 缺少 TODO-${milestone}.md;先由 PLAN agent 生成`);
|
|
453
473
|
return { abs, doc };
|
|
454
474
|
}
|
|
475
|
+
assertActiveRepo(repoId) {
|
|
476
|
+
const active = this.cfg.active_repo;
|
|
477
|
+
if (active && active !== repoId) {
|
|
478
|
+
throw new Error(`仓库 ${repoId} 不是 active_repo(当前 ${active});先 activate ${repoId}`);
|
|
479
|
+
}
|
|
480
|
+
}
|
|
455
481
|
claim(repoId, milestone, role) {
|
|
482
|
+
return withLock(this.root, `todo-${repoId}-${milestone}`, () => this.claimUnlocked(repoId, milestone, role));
|
|
483
|
+
}
|
|
484
|
+
claimUnlocked(repoId, milestone, role) {
|
|
485
|
+
this.assertActiveRepo(repoId);
|
|
456
486
|
this.assertNotBlocked(role, repoId);
|
|
457
487
|
const { abs, doc } = this.requireTodo(repoId, milestone);
|
|
458
488
|
if (!['todo_ready', 'rejected', 'planned'].includes(doc.status)) {
|
|
459
489
|
throw new Error(`${milestone} 当前状态为 ${doc.statusRaw},不能 claim(需 待实现/已打回)`);
|
|
460
490
|
}
|
|
491
|
+
const attempts = (doc.body.match(/^\s*-\s.*\*\*(?:通过|打回)\*\*/gm) ?? []).length;
|
|
461
492
|
writeAtomic(abs, setTodoStatus(readTextRequired(abs), 'implementing'));
|
|
462
493
|
logEvent(this.root, { role, tool: 'milestone_claim', repo: repoId, milestone, ok: true });
|
|
463
494
|
this.notifyEvent({
|
|
@@ -467,11 +498,15 @@ export class Workflow {
|
|
|
467
498
|
kind: 'event',
|
|
468
499
|
subject: `${repoId}/${milestone} 开始实现`,
|
|
469
500
|
refs: { repo: repoId, milestone },
|
|
470
|
-
key: `${repoId}:${milestone}:claim`,
|
|
501
|
+
key: `${repoId}:${milestone}:claim:${attempts}`,
|
|
471
502
|
});
|
|
472
503
|
return this.milestone(repoId, milestone);
|
|
473
504
|
}
|
|
474
505
|
complete(repoId, milestone, role) {
|
|
506
|
+
return withLock(this.root, `todo-${repoId}-${milestone}`, () => this.completeUnlocked(repoId, milestone, role));
|
|
507
|
+
}
|
|
508
|
+
completeUnlocked(repoId, milestone, role) {
|
|
509
|
+
this.assertActiveRepo(repoId);
|
|
475
510
|
this.assertNotBlocked(role, repoId);
|
|
476
511
|
const { abs, doc } = this.requireTodo(repoId, milestone);
|
|
477
512
|
if (doc.status !== 'implementing') {
|
|
@@ -507,6 +542,10 @@ export class Workflow {
|
|
|
507
542
|
return m;
|
|
508
543
|
}
|
|
509
544
|
accept(repoId, milestone, role, opts = {}) {
|
|
545
|
+
return withLock(this.root, `todo-${repoId}-${milestone}`, () => this.acceptUnlocked(repoId, milestone, role, opts));
|
|
546
|
+
}
|
|
547
|
+
acceptUnlocked(repoId, milestone, role, opts = {}) {
|
|
548
|
+
this.assertActiveRepo(repoId);
|
|
510
549
|
if (role !== 'plan' && role !== 'main') {
|
|
511
550
|
throw new Error(`只有 PLAN/主 agent 能验收;当前角色 ${role}`);
|
|
512
551
|
}
|
|
@@ -516,14 +555,17 @@ export class Workflow {
|
|
|
516
555
|
throw new Error(`${milestone} 当前状态为 ${doc.statusRaw},需先 milestone_complete 进入待验收`);
|
|
517
556
|
}
|
|
518
557
|
const evidence = parseEvidence(doc);
|
|
519
|
-
const
|
|
520
|
-
if (
|
|
521
|
-
|
|
558
|
+
const latest = evidence[evidence.length - 1];
|
|
559
|
+
if (!opts.force) {
|
|
560
|
+
if (!latest)
|
|
561
|
+
throw new Error(`${milestone} 没有证据;拒绝验收`);
|
|
562
|
+
if (latest.exitCode !== 0) {
|
|
563
|
+
throw new Error(`${milestone} 最新证据 exit=${latest.exitCode};拒绝验收`);
|
|
564
|
+
}
|
|
522
565
|
}
|
|
523
566
|
const ref = this.repoRef(repoId);
|
|
524
567
|
const gi = gitInfo(ref.abs);
|
|
525
568
|
const currentHash = workingTreeHash(ref.abs);
|
|
526
|
-
const latest = passing[passing.length - 1];
|
|
527
569
|
let note = opts.note || '';
|
|
528
570
|
if (latest?.tree && latest.tree !== workingTreeOid(ref.abs)) {
|
|
529
571
|
note += '\n⚠️ 最新证据之后工作区又发生了变化,建议重新跑 evidence_run。';
|
|
@@ -562,6 +604,10 @@ export class Workflow {
|
|
|
562
604
|
return this.milestone(repoId, milestone);
|
|
563
605
|
}
|
|
564
606
|
reject(repoId, milestone, role, reason) {
|
|
607
|
+
return withLock(this.root, `todo-${repoId}-${milestone}`, () => this.rejectUnlocked(repoId, milestone, role, reason));
|
|
608
|
+
}
|
|
609
|
+
rejectUnlocked(repoId, milestone, role, reason) {
|
|
610
|
+
this.assertActiveRepo(repoId);
|
|
565
611
|
if (role !== 'plan' && role !== 'main') {
|
|
566
612
|
throw new Error(`只有 PLAN/主 agent 能打回;当前角色 ${role}`);
|
|
567
613
|
}
|
|
@@ -609,7 +655,8 @@ export class Workflow {
|
|
|
609
655
|
async runEvidence(input) {
|
|
610
656
|
const { repoId, milestone, command, role } = input;
|
|
611
657
|
const ref = this.repoRef(repoId);
|
|
612
|
-
|
|
658
|
+
this.assertActiveRepo(repoId);
|
|
659
|
+
this.requireTodo(repoId, milestone); // validate early
|
|
613
660
|
const cwd = input.cwd ? path.resolve(ref.abs, input.cwd) : ref.abs;
|
|
614
661
|
if (!within(ref.abs, cwd))
|
|
615
662
|
throw new Error(`evidence cwd 必须在仓库内: ${cwd}`);
|
|
@@ -617,36 +664,40 @@ export class Workflow {
|
|
|
617
664
|
const result = await runCommand(command, cwd, input.timeoutMs ?? 900_000);
|
|
618
665
|
const gi = gitInfo(ref.abs);
|
|
619
666
|
const tree = workingTreeOid(ref.abs);
|
|
620
|
-
const
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
|
|
641
|
-
|
|
642
|
-
|
|
643
|
-
|
|
644
|
-
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
|
|
667
|
+
const recorded = withLock(this.root, `todo-${repoId}-${milestone}`, () => {
|
|
668
|
+
const { abs, doc } = this.requireTodo(repoId, milestone);
|
|
669
|
+
const list = parseEvidence(doc);
|
|
670
|
+
const id = `E${list.length + 1}`;
|
|
671
|
+
const at = isoNow();
|
|
672
|
+
const fullOutput = [
|
|
673
|
+
`$ ${command}`,
|
|
674
|
+
`exit: ${result.code}`,
|
|
675
|
+
`head: ${gi.head}`,
|
|
676
|
+
`tree: ${tree}`,
|
|
677
|
+
`dirty: ${gi.dirty}`,
|
|
678
|
+
'',
|
|
679
|
+
'--- stdout ---',
|
|
680
|
+
result.stdout,
|
|
681
|
+
'--- stderr ---',
|
|
682
|
+
result.stderr,
|
|
683
|
+
].join('\n');
|
|
684
|
+
const dir = path.join(evidenceDir(this.root), repoId, milestone);
|
|
685
|
+
ensureDir(dir);
|
|
686
|
+
writeAtomic(path.join(dir, `${id}-${Date.now()}.log`), fullOutput);
|
|
687
|
+
const tail = truncate([result.stdout, result.stderr].filter(Boolean).join('\n'), 4000);
|
|
688
|
+
const text = appendEvidenceSection(readTextRequired(abs), {
|
|
689
|
+
id,
|
|
690
|
+
at,
|
|
691
|
+
command,
|
|
692
|
+
exitCode: result.code,
|
|
693
|
+
head: gi.shortHead,
|
|
694
|
+
tree,
|
|
695
|
+
dirty: gi.dirty,
|
|
696
|
+
output: tail,
|
|
697
|
+
});
|
|
698
|
+
writeAtomic(abs, text);
|
|
699
|
+
return { id, output: tail, todoFile: path.relative(this.root, abs) };
|
|
648
700
|
});
|
|
649
|
-
writeAtomic(abs, text);
|
|
650
701
|
logEvent(this.root, {
|
|
651
702
|
role,
|
|
652
703
|
tool: 'evidence_run',
|
|
@@ -656,13 +707,13 @@ export class Workflow {
|
|
|
656
707
|
detail: { command, exit: result.code, ms: Date.now() - started },
|
|
657
708
|
});
|
|
658
709
|
return {
|
|
659
|
-
id,
|
|
710
|
+
id: recorded.id,
|
|
660
711
|
exitCode: result.code,
|
|
661
712
|
head: gi.shortHead,
|
|
662
713
|
tree,
|
|
663
714
|
dirty: gi.dirty,
|
|
664
|
-
output:
|
|
665
|
-
todoFile:
|
|
715
|
+
output: recorded.output,
|
|
716
|
+
todoFile: recorded.todoFile,
|
|
666
717
|
};
|
|
667
718
|
}
|
|
668
719
|
/* --------------------------- scope check --------------------------- */
|
|
@@ -671,9 +722,7 @@ export class Workflow {
|
|
|
671
722
|
const ref = this.repoRef(repoId);
|
|
672
723
|
const changed = allChangedFiles(ref.abs);
|
|
673
724
|
const st = this.repoState(repoId);
|
|
674
|
-
const m = milestone
|
|
675
|
-
? st.milestones.find((x) => x.id === milestone)
|
|
676
|
-
: st.currentMilestone;
|
|
725
|
+
const m = milestone ? st.milestones.find((x) => x.id === milestone) : st.currentMilestone;
|
|
677
726
|
const todo = m ? parseTodo(path.join(ref.abs, `TODO-${m.id}.md`)) : null;
|
|
678
727
|
const allowed = todo?.allowedFiles || [];
|
|
679
728
|
const violations = [];
|
|
@@ -694,7 +743,7 @@ export class Workflow {
|
|
|
694
743
|
for (const f of changed) {
|
|
695
744
|
if (isWorkflowManaged(f))
|
|
696
745
|
continue;
|
|
697
|
-
const ok = allowed.some((a) =>
|
|
746
|
+
const ok = allowed.some((a) => matchGlob(f, a.replace(/^\.\//, '')));
|
|
698
747
|
if (!ok) {
|
|
699
748
|
outOfScope.push(f);
|
|
700
749
|
warnings.push(`${f} 不在 TODO 声明的「影响文件」里`);
|
|
@@ -712,20 +761,42 @@ export class Workflow {
|
|
|
712
761
|
outOfScope,
|
|
713
762
|
violations,
|
|
714
763
|
warnings,
|
|
715
|
-
pass: violations.length === 0,
|
|
764
|
+
pass: violations.length === 0 && !(role === 'todo' && outOfScope.length > 0),
|
|
716
765
|
};
|
|
717
766
|
}
|
|
718
767
|
/* ----------------------------- commit ------------------------------ */
|
|
768
|
+
/** The milestone being committed: explicit, accepted-but-uncommitted, or current. */
|
|
769
|
+
milestoneForCommit(repoId, explicit) {
|
|
770
|
+
const st = this.repoState(repoId);
|
|
771
|
+
if (explicit) {
|
|
772
|
+
const found = st.milestones.find((m) => m.id === explicit);
|
|
773
|
+
if (!found)
|
|
774
|
+
throw new Error(`${repoId} 没有里程碑 ${explicit}`);
|
|
775
|
+
return found;
|
|
776
|
+
}
|
|
777
|
+
const accepted = st.milestones.filter((m) => m.status === 'accepted');
|
|
778
|
+
if (accepted.length)
|
|
779
|
+
return accepted[accepted.length - 1];
|
|
780
|
+
return st.currentMilestone;
|
|
781
|
+
}
|
|
719
782
|
requestCommit(input) {
|
|
720
783
|
const ref = this.repoRef(input.repoId);
|
|
721
784
|
const files = allChangedFiles(ref.abs);
|
|
722
785
|
if (files.length === 0)
|
|
723
786
|
throw new Error('没有可提交的改动');
|
|
724
|
-
const
|
|
787
|
+
const milestone = this.milestoneForCommit(input.repoId, input.milestone);
|
|
788
|
+
const scope = this.scopeCheck(input.repoId, milestone?.id, input.role, {
|
|
789
|
+
enforceAcl: false,
|
|
790
|
+
});
|
|
725
791
|
assertCommitScope(scope, input.role);
|
|
792
|
+
const stage = stageAll(ref.abs);
|
|
793
|
+
if (!stage.ok)
|
|
794
|
+
throw new Error(`git add 失败: ${stage.err}`);
|
|
726
795
|
const a = createApproval(this.root, {
|
|
727
796
|
repo: input.repoId,
|
|
797
|
+
milestone: milestone?.id,
|
|
728
798
|
tree_hash: workingTreeHash(ref.abs),
|
|
799
|
+
staged_hash: stagedTreeHash(ref.abs),
|
|
729
800
|
message: input.message,
|
|
730
801
|
files,
|
|
731
802
|
});
|
|
@@ -758,10 +829,20 @@ export class Workflow {
|
|
|
758
829
|
throw new Error(`审批 ${id} 不存在`);
|
|
759
830
|
if (a.used_at)
|
|
760
831
|
throw new Error(`审批 ${id} 已使用`);
|
|
832
|
+
const current = workingTreeHash(this.repoRef(a.repo).abs);
|
|
833
|
+
if (current !== a.tree_hash) {
|
|
834
|
+
throw new Error(`工作区内容已变化(审批时 ${a.tree_hash},现在 ${current});请重新 commit_request`);
|
|
835
|
+
}
|
|
761
836
|
a.approved_at = isoNow();
|
|
762
837
|
a.approved_by = by;
|
|
763
838
|
writeApproval(this.root, a);
|
|
764
|
-
logEvent(this.root, {
|
|
839
|
+
logEvent(this.root, {
|
|
840
|
+
role: 'user',
|
|
841
|
+
tool: 'approve',
|
|
842
|
+
repo: a.repo,
|
|
843
|
+
ok: true,
|
|
844
|
+
detail: { approval: id },
|
|
845
|
+
});
|
|
765
846
|
// Approving is how the human handles the notification; clear it from the inbox.
|
|
766
847
|
try {
|
|
767
848
|
this.msgs.ackByRef({ role: 'user' }, 'approval_id', id);
|
|
@@ -786,7 +867,7 @@ export class Workflow {
|
|
|
786
867
|
if (hash !== a.tree_hash) {
|
|
787
868
|
throw new Error(`工作区内容已变化(审批时 ${a.tree_hash},现在 ${hash});请重新 commit_request 并批准`);
|
|
788
869
|
}
|
|
789
|
-
const scope = this.scopeCheck(input.repoId,
|
|
870
|
+
const scope = this.scopeCheck(input.repoId, a.milestone ?? this.milestoneForCommit(input.repoId)?.id, input.role, { enforceAcl: false });
|
|
790
871
|
assertCommitScope(scope, input.role);
|
|
791
872
|
const files = allChangedFiles(ref.abs);
|
|
792
873
|
if (input.dryRun) {
|
|
@@ -795,6 +876,12 @@ export class Workflow {
|
|
|
795
876
|
const stage = stageAll(ref.abs);
|
|
796
877
|
if (!stage.ok)
|
|
797
878
|
throw new Error(`git add 失败: ${stage.err}`);
|
|
879
|
+
if (a.staged_hash) {
|
|
880
|
+
const staged = stagedTreeHash(ref.abs);
|
|
881
|
+
if (staged !== a.staged_hash) {
|
|
882
|
+
throw new Error(`暂存区内容已变化(审批时 ${a.staged_hash},现在 ${staged});请重新 commit_request 并批准`);
|
|
883
|
+
}
|
|
884
|
+
}
|
|
798
885
|
const res = gitCommit(ref.abs, a.message);
|
|
799
886
|
if (!res.ok)
|
|
800
887
|
throw new Error(`git commit 失败: ${res.err || res.out}`);
|
|
@@ -833,20 +920,40 @@ export class Workflow {
|
|
|
833
920
|
}
|
|
834
921
|
const active = this.cfg.active_repo;
|
|
835
922
|
if (active && !this.cfg.repos.some((r) => r.id === active)) {
|
|
836
|
-
push({
|
|
923
|
+
push({
|
|
924
|
+
level: 'FAIL',
|
|
925
|
+
code: 'active-repo',
|
|
926
|
+
message: `active_repo=${active} 不在 repos 列表`,
|
|
927
|
+
});
|
|
837
928
|
}
|
|
838
929
|
if (!active) {
|
|
839
|
-
push({
|
|
930
|
+
push({
|
|
931
|
+
level: 'WARN',
|
|
932
|
+
code: 'active-repo',
|
|
933
|
+
message: '没有激活的仓库,所有实现工作都会停滞',
|
|
934
|
+
hint: 'mpt-workflow activate <repo>',
|
|
935
|
+
});
|
|
840
936
|
}
|
|
841
937
|
for (const ref of this.repoRefs()) {
|
|
842
938
|
if (!exists(ref.abs)) {
|
|
843
|
-
push({
|
|
939
|
+
push({
|
|
940
|
+
level: 'FAIL',
|
|
941
|
+
code: 'repo-missing',
|
|
942
|
+
repo: ref.id,
|
|
943
|
+
message: `仓库目录不存在: ${ref.abs}`,
|
|
944
|
+
});
|
|
844
945
|
continue;
|
|
845
946
|
}
|
|
846
947
|
const conf = this.repoConfig(ref.id);
|
|
847
948
|
const gi = gitInfo(ref.abs);
|
|
848
949
|
if (!gi.available) {
|
|
849
|
-
push({
|
|
950
|
+
push({
|
|
951
|
+
level: 'FAIL',
|
|
952
|
+
code: 'git',
|
|
953
|
+
repo: ref.id,
|
|
954
|
+
message: '不是 git 仓库或无法读取 HEAD',
|
|
955
|
+
hint: gi.error,
|
|
956
|
+
});
|
|
850
957
|
continue;
|
|
851
958
|
}
|
|
852
959
|
if (conf.branch && gi.branch && conf.branch !== gi.branch) {
|
|
@@ -891,12 +998,17 @@ export class Workflow {
|
|
|
891
998
|
if (exists(path.join(ref.abs, 'rust-toolchain.toml'))) {
|
|
892
999
|
const tc = readText(path.join(ref.abs, 'rust-toolchain.toml'));
|
|
893
1000
|
const ch = /channel\s*=\s*"([^"]+)"/.exec(tc)?.[1];
|
|
894
|
-
if (ch
|
|
1001
|
+
if (ch?.includes('nightly')) {
|
|
895
1002
|
push({ level: 'OK', code: 'toolchain', repo: ref.id, message: `rust-toolchain: ${ch}` });
|
|
896
1003
|
}
|
|
897
1004
|
}
|
|
898
1005
|
if (exists(path.join(ref.abs, 'Cargo.toml')) && !exists(path.join(ref.abs, 'Cargo.lock'))) {
|
|
899
|
-
push({
|
|
1006
|
+
push({
|
|
1007
|
+
level: 'WARN',
|
|
1008
|
+
code: 'cargo-lock-missing',
|
|
1009
|
+
repo: ref.id,
|
|
1010
|
+
message: 'Cargo.toml 存在但没有 Cargo.lock',
|
|
1011
|
+
});
|
|
900
1012
|
}
|
|
901
1013
|
// questions parse health
|
|
902
1014
|
const qs = exists(questionsFile(ref.abs))
|
|
@@ -913,7 +1025,12 @@ export class Workflow {
|
|
|
913
1025
|
}
|
|
914
1026
|
const st = this.repoState(ref.id);
|
|
915
1027
|
if (st.milestones.length === 0) {
|
|
916
|
-
push({
|
|
1028
|
+
push({
|
|
1029
|
+
level: 'WARN',
|
|
1030
|
+
code: 'milestones',
|
|
1031
|
+
repo: ref.id,
|
|
1032
|
+
message: '没有 M(PLAN 表未解析到,也没有 TODO)',
|
|
1033
|
+
});
|
|
917
1034
|
}
|
|
918
1035
|
const acceptedDirty = st.milestones.filter((m) => m.status === 'accepted');
|
|
919
1036
|
if (acceptedDirty.length && gi.dirty) {
|
|
@@ -930,9 +1047,7 @@ export class Workflow {
|
|
|
930
1047
|
level: a.approved_at ? 'WARN' : 'OK',
|
|
931
1048
|
code: 'approval',
|
|
932
1049
|
repo: a.repo,
|
|
933
|
-
message: a.approved_at
|
|
934
|
-
? `审批 ${a.id} 已批准但未使用`
|
|
935
|
-
: `审批 ${a.id} 等待用户批准`,
|
|
1050
|
+
message: a.approved_at ? `审批 ${a.id} 已批准但未使用` : `审批 ${a.id} 等待用户批准`,
|
|
936
1051
|
hint: `mpt-workflow approve ${a.id}`,
|
|
937
1052
|
});
|
|
938
1053
|
}
|
|
@@ -971,11 +1086,7 @@ export class Workflow {
|
|
|
971
1086
|
* questions board change as a normal part of the workflow.
|
|
972
1087
|
*/
|
|
973
1088
|
export function isMachineManagedFile(file) {
|
|
974
|
-
return
|
|
975
|
-
file === 'docs/agent-questions.md' ||
|
|
976
|
-
file === '.mcp.json' ||
|
|
977
|
-
file.startsWith('.claude/') ||
|
|
978
|
-
file.startsWith('.workflow/'));
|
|
1089
|
+
return /^TODO-M\d+\.md$/.test(file) || file === 'docs/agent-questions.md';
|
|
979
1090
|
}
|
|
980
1091
|
/** Machine-managed files plus the plan itself (derived-scope checks only). */
|
|
981
1092
|
export function isWorkflowManaged(file) {
|
|
@@ -1069,13 +1180,6 @@ function runCommand(command, cwd, timeoutMs) {
|
|
|
1069
1180
|
});
|
|
1070
1181
|
});
|
|
1071
1182
|
}
|
|
1072
|
-
function globMatchPath(file, pattern) {
|
|
1073
|
-
const p = pattern.replace(/^\.\//, '');
|
|
1074
|
-
if (p === file)
|
|
1075
|
-
return true;
|
|
1076
|
-
const esc = p.replace(/[.+^${}()|[\]\\]/g, '\\$&').replace(/\*\*/g, '\u0000').replace(/\*/g, '[^/]*').replace(/\u0000/g, '.*');
|
|
1077
|
-
return new RegExp('^' + esc + '$').test(file);
|
|
1078
|
-
}
|
|
1079
1183
|
function appendReview(text, r) {
|
|
1080
1184
|
const header = '## 验收记录';
|
|
1081
1185
|
const line = [
|