mpt-workflow 0.5.0 → 0.7.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/CHANGELOG.md +86 -1
- package/README.md +79 -17
- package/README.zh-CN.md +43 -11
- package/dist/cli.js +48 -14
- 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 +226 -0
- package/dist/core/gc.js.map +1 -0
- package/dist/core/git.js +17 -2
- package/dist/core/git.js.map +1 -1
- package/dist/core/hooks.js +127 -54
- package/dist/core/hooks.js.map +1 -1
- package/dist/core/init.js +51 -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 +18 -12
- 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 +223 -87
- 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,14 +1,16 @@
|
|
|
1
|
-
import path from 'node:path';
|
|
2
1
|
import { spawn } from 'node:child_process';
|
|
3
|
-
import
|
|
4
|
-
import { loadConfig, saveConfig, findRoot, 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';
|
|
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';
|
|
7
|
+
import { dirSize, formatBytes } from './gc.js';
|
|
8
|
+
import { allChangedFiles, diffStat, commit as gitCommit, gitInfo, stageAll, stagedTreeHash, workingTreeHash, workingTreeOid, } from './git.js';
|
|
9
|
+
import { matchGlob } from './glob.js';
|
|
9
10
|
import { appendToSection } from './markdown.js';
|
|
10
|
-
import {
|
|
11
|
-
import {
|
|
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';
|
|
12
14
|
export class Workflow {
|
|
13
15
|
root;
|
|
14
16
|
cfg;
|
|
@@ -118,7 +120,7 @@ export class Workflow {
|
|
|
118
120
|
}
|
|
119
121
|
/** Answer a question: update the file, notify the asker, clear the blocker. */
|
|
120
122
|
answerQuestion(q, answer, resolution, by) {
|
|
121
|
-
updateQuestion(q.file, q.id, { status: 'answered', answer, resolution });
|
|
123
|
+
withLock(this.root, 'questions', () => updateQuestion(q.file, q.id, { status: 'answered', answer, resolution }));
|
|
122
124
|
const asker = actorFromRole(String(q.from), q.repo);
|
|
123
125
|
try {
|
|
124
126
|
this.msgs.send({
|
|
@@ -145,7 +147,7 @@ export class Workflow {
|
|
|
145
147
|
}
|
|
146
148
|
/** Close a question: update the file, notify the other party. */
|
|
147
149
|
closeQuestion(q, resolution, by) {
|
|
148
|
-
updateQuestion(q.file, q.id, { status: 'closed', resolution });
|
|
150
|
+
withLock(this.root, 'questions', () => updateQuestion(q.file, q.id, { status: 'closed', resolution }));
|
|
149
151
|
const asker = actorFromRole(String(q.from), q.repo);
|
|
150
152
|
const addressee = actorFromRole(String(q.to), q.repo);
|
|
151
153
|
const other = actorKey(by) === actorKey(asker) ? addressee : asker;
|
|
@@ -231,14 +233,15 @@ export class Workflow {
|
|
|
231
233
|
problems.push('PLAN 中未解析到任何 M,且无 TODO-Mn.md');
|
|
232
234
|
if (git.dirty)
|
|
233
235
|
problems.push(`工作区有未提交改动: ${git.changed.length + git.untracked.length} 个文件`);
|
|
234
|
-
|
|
235
|
-
|
|
236
|
+
const acceptedMilestones = milestones.filter((m) => m.status === 'accepted');
|
|
237
|
+
if (acceptedMilestones.length) {
|
|
238
|
+
problems.push(`${acceptedMilestones.map((m) => m.id).join(', ')} 已验收但尚未提交`);
|
|
239
|
+
}
|
|
236
240
|
for (const q of questions.filter((q) => q.blocking && q.status === 'open')) {
|
|
237
241
|
problems.push(`阻塞问题未回答: ${q.id}`);
|
|
238
242
|
}
|
|
239
243
|
// phase: explicit PLAN phase, else the phase that lists this repo
|
|
240
|
-
const phase = plan?.phase ||
|
|
241
|
-
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;
|
|
242
245
|
return {
|
|
243
246
|
id,
|
|
244
247
|
path: conf.path,
|
|
@@ -320,6 +323,18 @@ export class Workflow {
|
|
|
320
323
|
});
|
|
321
324
|
return out;
|
|
322
325
|
}
|
|
326
|
+
const pendingCommit = active.milestones.filter((x) => x.status === 'accepted');
|
|
327
|
+
if (pendingCommit.length) {
|
|
328
|
+
out.push({
|
|
329
|
+
actor: 'user',
|
|
330
|
+
action: '评审并 commit',
|
|
331
|
+
detail: `${pendingCommit.map((x) => x.id).join(', ')} 已验收,等待 review diff 后批准提交`,
|
|
332
|
+
repo: activeId,
|
|
333
|
+
milestone: pendingCommit[pendingCommit.length - 1].id,
|
|
334
|
+
blocking: false,
|
|
335
|
+
});
|
|
336
|
+
return out;
|
|
337
|
+
}
|
|
323
338
|
const m = active.currentMilestone;
|
|
324
339
|
if (active.milestones.length === 0) {
|
|
325
340
|
out.push({
|
|
@@ -407,6 +422,9 @@ export class Workflow {
|
|
|
407
422
|
throw new Error(`仓库目录不存在: ${ref.abs}`);
|
|
408
423
|
}
|
|
409
424
|
withLock(this.root, 'program', () => {
|
|
425
|
+
const fresh = loadConfig(this.root);
|
|
426
|
+
if (fresh)
|
|
427
|
+
this.cfg = fresh;
|
|
410
428
|
this.cfg.active_repo = id;
|
|
411
429
|
this.save();
|
|
412
430
|
});
|
|
@@ -426,18 +444,20 @@ export class Workflow {
|
|
|
426
444
|
}
|
|
427
445
|
}
|
|
428
446
|
updatePhase(id, status) {
|
|
429
|
-
const p = this.cfg.phases.find((x) => x.id === id);
|
|
430
|
-
if (!p)
|
|
431
|
-
throw new Error(`未知阶段 ${id}`);
|
|
432
447
|
withLock(this.root, 'program', () => {
|
|
433
|
-
const
|
|
448
|
+
const fresh = loadConfig(this.root);
|
|
449
|
+
if (fresh)
|
|
450
|
+
this.cfg = fresh;
|
|
451
|
+
const p = this.cfg.phases.find((x) => x.id === id);
|
|
452
|
+
if (!p)
|
|
453
|
+
throw new Error(`未知阶段 ${id}`);
|
|
434
454
|
// keep only one active phase
|
|
435
455
|
if (status === 'active') {
|
|
436
456
|
for (const q of this.cfg.phases)
|
|
437
457
|
if (q.status === 'active')
|
|
438
458
|
q.status = 'accepted';
|
|
439
459
|
}
|
|
440
|
-
|
|
460
|
+
p.status = status;
|
|
441
461
|
this.save();
|
|
442
462
|
});
|
|
443
463
|
}
|
|
@@ -452,11 +472,15 @@ export class Workflow {
|
|
|
452
472
|
return { abs, doc };
|
|
453
473
|
}
|
|
454
474
|
claim(repoId, milestone, role) {
|
|
475
|
+
return withLock(this.root, `todo-${repoId}-${milestone}`, () => this.claimUnlocked(repoId, milestone, role));
|
|
476
|
+
}
|
|
477
|
+
claimUnlocked(repoId, milestone, role) {
|
|
455
478
|
this.assertNotBlocked(role, repoId);
|
|
456
479
|
const { abs, doc } = this.requireTodo(repoId, milestone);
|
|
457
480
|
if (!['todo_ready', 'rejected', 'planned'].includes(doc.status)) {
|
|
458
481
|
throw new Error(`${milestone} 当前状态为 ${doc.statusRaw},不能 claim(需 待实现/已打回)`);
|
|
459
482
|
}
|
|
483
|
+
const attempts = (doc.body.match(/^\s*-\s.*\*\*(?:通过|打回)\*\*/gm) ?? []).length;
|
|
460
484
|
writeAtomic(abs, setTodoStatus(readTextRequired(abs), 'implementing'));
|
|
461
485
|
logEvent(this.root, { role, tool: 'milestone_claim', repo: repoId, milestone, ok: true });
|
|
462
486
|
this.notifyEvent({
|
|
@@ -466,11 +490,14 @@ export class Workflow {
|
|
|
466
490
|
kind: 'event',
|
|
467
491
|
subject: `${repoId}/${milestone} 开始实现`,
|
|
468
492
|
refs: { repo: repoId, milestone },
|
|
469
|
-
key: `${repoId}:${milestone}:claim`,
|
|
493
|
+
key: `${repoId}:${milestone}:claim:${attempts}`,
|
|
470
494
|
});
|
|
471
495
|
return this.milestone(repoId, milestone);
|
|
472
496
|
}
|
|
473
497
|
complete(repoId, milestone, role) {
|
|
498
|
+
return withLock(this.root, `todo-${repoId}-${milestone}`, () => this.completeUnlocked(repoId, milestone, role));
|
|
499
|
+
}
|
|
500
|
+
completeUnlocked(repoId, milestone, role) {
|
|
474
501
|
this.assertNotBlocked(role, repoId);
|
|
475
502
|
const { abs, doc } = this.requireTodo(repoId, milestone);
|
|
476
503
|
if (doc.status !== 'implementing') {
|
|
@@ -506,6 +533,9 @@ export class Workflow {
|
|
|
506
533
|
return m;
|
|
507
534
|
}
|
|
508
535
|
accept(repoId, milestone, role, opts = {}) {
|
|
536
|
+
return withLock(this.root, `todo-${repoId}-${milestone}`, () => this.acceptUnlocked(repoId, milestone, role, opts));
|
|
537
|
+
}
|
|
538
|
+
acceptUnlocked(repoId, milestone, role, opts = {}) {
|
|
509
539
|
if (role !== 'plan' && role !== 'main') {
|
|
510
540
|
throw new Error(`只有 PLAN/主 agent 能验收;当前角色 ${role}`);
|
|
511
541
|
}
|
|
@@ -515,14 +545,17 @@ export class Workflow {
|
|
|
515
545
|
throw new Error(`${milestone} 当前状态为 ${doc.statusRaw},需先 milestone_complete 进入待验收`);
|
|
516
546
|
}
|
|
517
547
|
const evidence = parseEvidence(doc);
|
|
518
|
-
const
|
|
519
|
-
if (
|
|
520
|
-
|
|
548
|
+
const latest = evidence[evidence.length - 1];
|
|
549
|
+
if (!opts.force) {
|
|
550
|
+
if (!latest)
|
|
551
|
+
throw new Error(`${milestone} 没有证据;拒绝验收`);
|
|
552
|
+
if (latest.exitCode !== 0) {
|
|
553
|
+
throw new Error(`${milestone} 最新证据 exit=${latest.exitCode};拒绝验收`);
|
|
554
|
+
}
|
|
521
555
|
}
|
|
522
556
|
const ref = this.repoRef(repoId);
|
|
523
557
|
const gi = gitInfo(ref.abs);
|
|
524
558
|
const currentHash = workingTreeHash(ref.abs);
|
|
525
|
-
const latest = passing[passing.length - 1];
|
|
526
559
|
let note = opts.note || '';
|
|
527
560
|
if (latest?.tree && latest.tree !== workingTreeOid(ref.abs)) {
|
|
528
561
|
note += '\n⚠️ 最新证据之后工作区又发生了变化,建议重新跑 evidence_run。';
|
|
@@ -561,6 +594,9 @@ export class Workflow {
|
|
|
561
594
|
return this.milestone(repoId, milestone);
|
|
562
595
|
}
|
|
563
596
|
reject(repoId, milestone, role, reason) {
|
|
597
|
+
return withLock(this.root, `todo-${repoId}-${milestone}`, () => this.rejectUnlocked(repoId, milestone, role, reason));
|
|
598
|
+
}
|
|
599
|
+
rejectUnlocked(repoId, milestone, role, reason) {
|
|
564
600
|
if (role !== 'plan' && role !== 'main') {
|
|
565
601
|
throw new Error(`只有 PLAN/主 agent 能打回;当前角色 ${role}`);
|
|
566
602
|
}
|
|
@@ -608,7 +644,7 @@ export class Workflow {
|
|
|
608
644
|
async runEvidence(input) {
|
|
609
645
|
const { repoId, milestone, command, role } = input;
|
|
610
646
|
const ref = this.repoRef(repoId);
|
|
611
|
-
|
|
647
|
+
this.requireTodo(repoId, milestone); // validate early
|
|
612
648
|
const cwd = input.cwd ? path.resolve(ref.abs, input.cwd) : ref.abs;
|
|
613
649
|
if (!within(ref.abs, cwd))
|
|
614
650
|
throw new Error(`evidence cwd 必须在仓库内: ${cwd}`);
|
|
@@ -616,36 +652,40 @@ export class Workflow {
|
|
|
616
652
|
const result = await runCommand(command, cwd, input.timeoutMs ?? 900_000);
|
|
617
653
|
const gi = gitInfo(ref.abs);
|
|
618
654
|
const tree = workingTreeOid(ref.abs);
|
|
619
|
-
const
|
|
620
|
-
|
|
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
|
-
|
|
655
|
+
const recorded = withLock(this.root, `todo-${repoId}-${milestone}`, () => {
|
|
656
|
+
const { abs, doc } = this.requireTodo(repoId, milestone);
|
|
657
|
+
const list = parseEvidence(doc);
|
|
658
|
+
const id = `E${list.length + 1}`;
|
|
659
|
+
const at = isoNow();
|
|
660
|
+
const fullOutput = [
|
|
661
|
+
`$ ${command}`,
|
|
662
|
+
`exit: ${result.code}`,
|
|
663
|
+
`head: ${gi.head}`,
|
|
664
|
+
`tree: ${tree}`,
|
|
665
|
+
`dirty: ${gi.dirty}`,
|
|
666
|
+
'',
|
|
667
|
+
'--- stdout ---',
|
|
668
|
+
result.stdout,
|
|
669
|
+
'--- stderr ---',
|
|
670
|
+
result.stderr,
|
|
671
|
+
].join('\n');
|
|
672
|
+
const dir = path.join(evidenceDir(this.root), repoId, milestone);
|
|
673
|
+
ensureDir(dir);
|
|
674
|
+
writeAtomic(path.join(dir, `${id}-${Date.now()}.log`), fullOutput);
|
|
675
|
+
const tail = truncate([result.stdout, result.stderr].filter(Boolean).join('\n'), 4000);
|
|
676
|
+
const text = appendEvidenceSection(readTextRequired(abs), {
|
|
677
|
+
id,
|
|
678
|
+
at,
|
|
679
|
+
command,
|
|
680
|
+
exitCode: result.code,
|
|
681
|
+
head: gi.shortHead,
|
|
682
|
+
tree,
|
|
683
|
+
dirty: gi.dirty,
|
|
684
|
+
output: tail,
|
|
685
|
+
});
|
|
686
|
+
writeAtomic(abs, text);
|
|
687
|
+
return { id, output: tail, todoFile: path.relative(this.root, abs) };
|
|
647
688
|
});
|
|
648
|
-
writeAtomic(abs, text);
|
|
649
689
|
logEvent(this.root, {
|
|
650
690
|
role,
|
|
651
691
|
tool: 'evidence_run',
|
|
@@ -655,13 +695,13 @@ export class Workflow {
|
|
|
655
695
|
detail: { command, exit: result.code, ms: Date.now() - started },
|
|
656
696
|
});
|
|
657
697
|
return {
|
|
658
|
-
id,
|
|
698
|
+
id: recorded.id,
|
|
659
699
|
exitCode: result.code,
|
|
660
700
|
head: gi.shortHead,
|
|
661
701
|
tree,
|
|
662
702
|
dirty: gi.dirty,
|
|
663
|
-
output:
|
|
664
|
-
todoFile:
|
|
703
|
+
output: recorded.output,
|
|
704
|
+
todoFile: recorded.todoFile,
|
|
665
705
|
};
|
|
666
706
|
}
|
|
667
707
|
/* --------------------------- scope check --------------------------- */
|
|
@@ -670,9 +710,7 @@ export class Workflow {
|
|
|
670
710
|
const ref = this.repoRef(repoId);
|
|
671
711
|
const changed = allChangedFiles(ref.abs);
|
|
672
712
|
const st = this.repoState(repoId);
|
|
673
|
-
const m = milestone
|
|
674
|
-
? st.milestones.find((x) => x.id === milestone)
|
|
675
|
-
: st.currentMilestone;
|
|
713
|
+
const m = milestone ? st.milestones.find((x) => x.id === milestone) : st.currentMilestone;
|
|
676
714
|
const todo = m ? parseTodo(path.join(ref.abs, `TODO-${m.id}.md`)) : null;
|
|
677
715
|
const allowed = todo?.allowedFiles || [];
|
|
678
716
|
const violations = [];
|
|
@@ -693,7 +731,7 @@ export class Workflow {
|
|
|
693
731
|
for (const f of changed) {
|
|
694
732
|
if (isWorkflowManaged(f))
|
|
695
733
|
continue;
|
|
696
|
-
const ok = allowed.some((a) =>
|
|
734
|
+
const ok = allowed.some((a) => matchGlob(f, a.replace(/^\.\//, '')));
|
|
697
735
|
if (!ok) {
|
|
698
736
|
outOfScope.push(f);
|
|
699
737
|
warnings.push(`${f} 不在 TODO 声明的「影响文件」里`);
|
|
@@ -711,20 +749,42 @@ export class Workflow {
|
|
|
711
749
|
outOfScope,
|
|
712
750
|
violations,
|
|
713
751
|
warnings,
|
|
714
|
-
pass: violations.length === 0,
|
|
752
|
+
pass: violations.length === 0 && !(role === 'todo' && outOfScope.length > 0),
|
|
715
753
|
};
|
|
716
754
|
}
|
|
717
755
|
/* ----------------------------- commit ------------------------------ */
|
|
756
|
+
/** The milestone being committed: explicit, accepted-but-uncommitted, or current. */
|
|
757
|
+
milestoneForCommit(repoId, explicit) {
|
|
758
|
+
const st = this.repoState(repoId);
|
|
759
|
+
if (explicit) {
|
|
760
|
+
const found = st.milestones.find((m) => m.id === explicit);
|
|
761
|
+
if (!found)
|
|
762
|
+
throw new Error(`${repoId} 没有里程碑 ${explicit}`);
|
|
763
|
+
return found;
|
|
764
|
+
}
|
|
765
|
+
const accepted = st.milestones.filter((m) => m.status === 'accepted');
|
|
766
|
+
if (accepted.length)
|
|
767
|
+
return accepted[accepted.length - 1];
|
|
768
|
+
return st.currentMilestone;
|
|
769
|
+
}
|
|
718
770
|
requestCommit(input) {
|
|
719
771
|
const ref = this.repoRef(input.repoId);
|
|
720
772
|
const files = allChangedFiles(ref.abs);
|
|
721
773
|
if (files.length === 0)
|
|
722
774
|
throw new Error('没有可提交的改动');
|
|
723
|
-
const
|
|
775
|
+
const milestone = this.milestoneForCommit(input.repoId, input.milestone);
|
|
776
|
+
const scope = this.scopeCheck(input.repoId, milestone?.id, input.role, {
|
|
777
|
+
enforceAcl: false,
|
|
778
|
+
});
|
|
724
779
|
assertCommitScope(scope, input.role);
|
|
780
|
+
const stage = stageAll(ref.abs);
|
|
781
|
+
if (!stage.ok)
|
|
782
|
+
throw new Error(`git add 失败: ${stage.err}`);
|
|
725
783
|
const a = createApproval(this.root, {
|
|
726
784
|
repo: input.repoId,
|
|
785
|
+
milestone: milestone?.id,
|
|
727
786
|
tree_hash: workingTreeHash(ref.abs),
|
|
787
|
+
staged_hash: stagedTreeHash(ref.abs),
|
|
728
788
|
message: input.message,
|
|
729
789
|
files,
|
|
730
790
|
});
|
|
@@ -757,10 +817,20 @@ export class Workflow {
|
|
|
757
817
|
throw new Error(`审批 ${id} 不存在`);
|
|
758
818
|
if (a.used_at)
|
|
759
819
|
throw new Error(`审批 ${id} 已使用`);
|
|
820
|
+
const current = workingTreeHash(this.repoRef(a.repo).abs);
|
|
821
|
+
if (current !== a.tree_hash) {
|
|
822
|
+
throw new Error(`工作区内容已变化(审批时 ${a.tree_hash},现在 ${current});请重新 commit_request`);
|
|
823
|
+
}
|
|
760
824
|
a.approved_at = isoNow();
|
|
761
825
|
a.approved_by = by;
|
|
762
826
|
writeApproval(this.root, a);
|
|
763
|
-
logEvent(this.root, {
|
|
827
|
+
logEvent(this.root, {
|
|
828
|
+
role: 'user',
|
|
829
|
+
tool: 'approve',
|
|
830
|
+
repo: a.repo,
|
|
831
|
+
ok: true,
|
|
832
|
+
detail: { approval: id },
|
|
833
|
+
});
|
|
764
834
|
// Approving is how the human handles the notification; clear it from the inbox.
|
|
765
835
|
try {
|
|
766
836
|
this.msgs.ackByRef({ role: 'user' }, 'approval_id', id);
|
|
@@ -785,7 +855,7 @@ export class Workflow {
|
|
|
785
855
|
if (hash !== a.tree_hash) {
|
|
786
856
|
throw new Error(`工作区内容已变化(审批时 ${a.tree_hash},现在 ${hash});请重新 commit_request 并批准`);
|
|
787
857
|
}
|
|
788
|
-
const scope = this.scopeCheck(input.repoId,
|
|
858
|
+
const scope = this.scopeCheck(input.repoId, a.milestone ?? this.milestoneForCommit(input.repoId)?.id, input.role, { enforceAcl: false });
|
|
789
859
|
assertCommitScope(scope, input.role);
|
|
790
860
|
const files = allChangedFiles(ref.abs);
|
|
791
861
|
if (input.dryRun) {
|
|
@@ -794,6 +864,12 @@ export class Workflow {
|
|
|
794
864
|
const stage = stageAll(ref.abs);
|
|
795
865
|
if (!stage.ok)
|
|
796
866
|
throw new Error(`git add 失败: ${stage.err}`);
|
|
867
|
+
if (a.staged_hash) {
|
|
868
|
+
const staged = stagedTreeHash(ref.abs);
|
|
869
|
+
if (staged !== a.staged_hash) {
|
|
870
|
+
throw new Error(`暂存区内容已变化(审批时 ${a.staged_hash},现在 ${staged});请重新 commit_request 并批准`);
|
|
871
|
+
}
|
|
872
|
+
}
|
|
797
873
|
const res = gitCommit(ref.abs, a.message);
|
|
798
874
|
if (!res.ok)
|
|
799
875
|
throw new Error(`git commit 失败: ${res.err || res.out}`);
|
|
@@ -832,20 +908,40 @@ export class Workflow {
|
|
|
832
908
|
}
|
|
833
909
|
const active = this.cfg.active_repo;
|
|
834
910
|
if (active && !this.cfg.repos.some((r) => r.id === active)) {
|
|
835
|
-
push({
|
|
911
|
+
push({
|
|
912
|
+
level: 'FAIL',
|
|
913
|
+
code: 'active-repo',
|
|
914
|
+
message: `active_repo=${active} 不在 repos 列表`,
|
|
915
|
+
});
|
|
836
916
|
}
|
|
837
917
|
if (!active) {
|
|
838
|
-
push({
|
|
918
|
+
push({
|
|
919
|
+
level: 'WARN',
|
|
920
|
+
code: 'active-repo',
|
|
921
|
+
message: '没有激活的仓库,所有实现工作都会停滞',
|
|
922
|
+
hint: 'mpt-workflow activate <repo>',
|
|
923
|
+
});
|
|
839
924
|
}
|
|
840
925
|
for (const ref of this.repoRefs()) {
|
|
841
926
|
if (!exists(ref.abs)) {
|
|
842
|
-
push({
|
|
927
|
+
push({
|
|
928
|
+
level: 'FAIL',
|
|
929
|
+
code: 'repo-missing',
|
|
930
|
+
repo: ref.id,
|
|
931
|
+
message: `仓库目录不存在: ${ref.abs}`,
|
|
932
|
+
});
|
|
843
933
|
continue;
|
|
844
934
|
}
|
|
845
935
|
const conf = this.repoConfig(ref.id);
|
|
846
936
|
const gi = gitInfo(ref.abs);
|
|
847
937
|
if (!gi.available) {
|
|
848
|
-
push({
|
|
938
|
+
push({
|
|
939
|
+
level: 'FAIL',
|
|
940
|
+
code: 'git',
|
|
941
|
+
repo: ref.id,
|
|
942
|
+
message: '不是 git 仓库或无法读取 HEAD',
|
|
943
|
+
hint: gi.error,
|
|
944
|
+
});
|
|
849
945
|
continue;
|
|
850
946
|
}
|
|
851
947
|
if (conf.branch && gi.branch && conf.branch !== gi.branch) {
|
|
@@ -890,12 +986,17 @@ export class Workflow {
|
|
|
890
986
|
if (exists(path.join(ref.abs, 'rust-toolchain.toml'))) {
|
|
891
987
|
const tc = readText(path.join(ref.abs, 'rust-toolchain.toml'));
|
|
892
988
|
const ch = /channel\s*=\s*"([^"]+)"/.exec(tc)?.[1];
|
|
893
|
-
if (ch
|
|
989
|
+
if (ch?.includes('nightly')) {
|
|
894
990
|
push({ level: 'OK', code: 'toolchain', repo: ref.id, message: `rust-toolchain: ${ch}` });
|
|
895
991
|
}
|
|
896
992
|
}
|
|
897
993
|
if (exists(path.join(ref.abs, 'Cargo.toml')) && !exists(path.join(ref.abs, 'Cargo.lock'))) {
|
|
898
|
-
push({
|
|
994
|
+
push({
|
|
995
|
+
level: 'WARN',
|
|
996
|
+
code: 'cargo-lock-missing',
|
|
997
|
+
repo: ref.id,
|
|
998
|
+
message: 'Cargo.toml 存在但没有 Cargo.lock',
|
|
999
|
+
});
|
|
899
1000
|
}
|
|
900
1001
|
// questions parse health
|
|
901
1002
|
const qs = exists(questionsFile(ref.abs))
|
|
@@ -912,7 +1013,12 @@ export class Workflow {
|
|
|
912
1013
|
}
|
|
913
1014
|
const st = this.repoState(ref.id);
|
|
914
1015
|
if (st.milestones.length === 0) {
|
|
915
|
-
push({
|
|
1016
|
+
push({
|
|
1017
|
+
level: 'WARN',
|
|
1018
|
+
code: 'milestones',
|
|
1019
|
+
repo: ref.id,
|
|
1020
|
+
message: '没有 M(PLAN 表未解析到,也没有 TODO)',
|
|
1021
|
+
});
|
|
916
1022
|
}
|
|
917
1023
|
const acceptedDirty = st.milestones.filter((m) => m.status === 'accepted');
|
|
918
1024
|
if (acceptedDirty.length && gi.dirty) {
|
|
@@ -929,12 +1035,32 @@ export class Workflow {
|
|
|
929
1035
|
level: a.approved_at ? 'WARN' : 'OK',
|
|
930
1036
|
code: 'approval',
|
|
931
1037
|
repo: a.repo,
|
|
932
|
-
message: a.approved_at
|
|
933
|
-
? `审批 ${a.id} 已批准但未使用`
|
|
934
|
-
: `审批 ${a.id} 等待用户批准`,
|
|
1038
|
+
message: a.approved_at ? `审批 ${a.id} 已批准但未使用` : `审批 ${a.id} 等待用户批准`,
|
|
935
1039
|
hint: `mpt-workflow approve ${a.id}`,
|
|
936
1040
|
});
|
|
937
1041
|
}
|
|
1042
|
+
const runtimeBytes = dirSize(runtimeDir(this.root));
|
|
1043
|
+
push(runtimeBytes > 100 * 1024 * 1024
|
|
1044
|
+
? {
|
|
1045
|
+
level: 'WARN',
|
|
1046
|
+
code: 'runtime-size',
|
|
1047
|
+
message: `runtime 已占用 ${formatBytes(runtimeBytes)}`,
|
|
1048
|
+
hint: 'mpt-workflow gc --apply 清理已处理的旧消息/审批',
|
|
1049
|
+
}
|
|
1050
|
+
: {
|
|
1051
|
+
level: 'OK',
|
|
1052
|
+
code: 'runtime-size',
|
|
1053
|
+
message: `runtime ${formatBytes(runtimeBytes)}`,
|
|
1054
|
+
});
|
|
1055
|
+
const stale = staleCliPaths(this.root, this.repoRefs().map((r) => r.abs));
|
|
1056
|
+
if (stale.length) {
|
|
1057
|
+
push({
|
|
1058
|
+
level: 'WARN',
|
|
1059
|
+
code: 'stale-cli',
|
|
1060
|
+
message: `配置指向不存在的 CLI: ${stale.join(', ')}`,
|
|
1061
|
+
hint: '重新运行 mpt-workflow init --install-hooks,或先 npm run build',
|
|
1062
|
+
});
|
|
1063
|
+
}
|
|
938
1064
|
if (!out.some((f) => f.level === 'FAIL')) {
|
|
939
1065
|
push({ level: 'OK', code: 'overall', message: '没有阻断性问题' });
|
|
940
1066
|
}
|
|
@@ -948,16 +1074,33 @@ export class Workflow {
|
|
|
948
1074
|
* questions board change as a normal part of the workflow.
|
|
949
1075
|
*/
|
|
950
1076
|
export function isMachineManagedFile(file) {
|
|
951
|
-
return
|
|
952
|
-
file === 'docs/agent-questions.md' ||
|
|
953
|
-
file === '.mcp.json' ||
|
|
954
|
-
file.startsWith('.claude/') ||
|
|
955
|
-
file.startsWith('.workflow/'));
|
|
1077
|
+
return /^TODO-M\d+\.md$/.test(file) || file === 'docs/agent-questions.md';
|
|
956
1078
|
}
|
|
957
1079
|
/** Machine-managed files plus the plan itself (derived-scope checks only). */
|
|
958
1080
|
export function isWorkflowManaged(file) {
|
|
959
1081
|
return file === 'PLAN.md' || isMachineManagedFile(file);
|
|
960
1082
|
}
|
|
1083
|
+
/**
|
|
1084
|
+
* Find absolute CLI paths in generated MCP/hook config that no longer exist.
|
|
1085
|
+
* This catches an evicted npx cache, a moved checkout, or a missing build.
|
|
1086
|
+
*/
|
|
1087
|
+
function staleCliPaths(root, repoDirs) {
|
|
1088
|
+
const targets = ['.mcp.json', '.claude/settings.json', '.git/hooks/pre-commit'];
|
|
1089
|
+
const missing = new Set();
|
|
1090
|
+
for (const dir of [root, ...repoDirs]) {
|
|
1091
|
+
for (const rel of targets) {
|
|
1092
|
+
const p = path.join(dir, rel);
|
|
1093
|
+
if (!exists(p))
|
|
1094
|
+
continue;
|
|
1095
|
+
const text = readText(p);
|
|
1096
|
+
for (const m of text.matchAll(/["']?(\/[^"'\s]+?cli\.js)["']?/g)) {
|
|
1097
|
+
if (!exists(m[1]))
|
|
1098
|
+
missing.add(m[1]);
|
|
1099
|
+
}
|
|
1100
|
+
}
|
|
1101
|
+
}
|
|
1102
|
+
return [...missing];
|
|
1103
|
+
}
|
|
961
1104
|
function assertCommitScope(scope, role) {
|
|
962
1105
|
// The TODO file list describes implementation scope. A plan/main commit that
|
|
963
1106
|
// adds planning docs is not scope creep, so only enforce it for the implementer.
|
|
@@ -1025,13 +1168,6 @@ function runCommand(command, cwd, timeoutMs) {
|
|
|
1025
1168
|
});
|
|
1026
1169
|
});
|
|
1027
1170
|
}
|
|
1028
|
-
function globMatchPath(file, pattern) {
|
|
1029
|
-
const p = pattern.replace(/^\.\//, '');
|
|
1030
|
-
if (p === file)
|
|
1031
|
-
return true;
|
|
1032
|
-
const esc = p.replace(/[.+^${}()|[\]\\]/g, '\\$&').replace(/\*\*/g, '\u0000').replace(/\*/g, '[^/]*').replace(/\u0000/g, '.*');
|
|
1033
|
-
return new RegExp('^' + esc + '$').test(file);
|
|
1034
|
-
}
|
|
1035
1171
|
function appendReview(text, r) {
|
|
1036
1172
|
const header = '## 验收记录';
|
|
1037
1173
|
const line = [
|