taskforce-loop-engineering 0.16.2 → 0.16.4

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.
@@ -1,14 +1,129 @@
1
1
  import assert from 'node:assert/strict';
2
- import { buildFinalJudgement, dispatchFailureClassification, inferTaskScope, selectEffectiveAcceptanceReviews } from '../lib/core.mjs';
2
+ import { mkdtemp, mkdir, readFile, rm, utimes, writeFile } from 'node:fs/promises';
3
+ import { tmpdir } from 'node:os';
4
+ import path from 'node:path';
5
+ import { enqueueTask, queueRequeue, runQueueOnce, buildFinalJudgement, dispatchFailureClassification, inferTaskScope, refreshTaskAcceptance, selectEffectiveAcceptanceReviews, writeAcceptancePlan, writeDevPlan, writeTaskContract } from '../lib/core.mjs';
3
6
 
4
7
  const basePlan = { rubric: [], automation: [] };
5
8
  const baseContract = { task_id: 't1', risk_level: 'L1', requires_human_gate: false, task_scope: 'scoped_task' };
6
9
 
7
10
  assert.equal(inferTaskScope({ body: '开发完整项目并完成整体目标' }), 'project');
11
+ assert.equal(inferTaskScope({ body: '承接项目并先开始首个批次', taskScope: 'project' }), 'project');
12
+ assert.equal(inferTaskScope({ body: '开发完整项目并完成整体目标', taskScope: 'scoped_task' }), 'scoped_task');
13
+ assert.equal(inferTaskScope({ body: '开发完整项目并完成整体目标', taskScope: 'invalid' }), 'project');
8
14
  assert.equal(inferTaskScope({ body: '继续同一整体项目,仅推进 T-01 第 2 项;阶段完成后不能称整体项目完成' }), 'scoped_task');
9
15
  assert.equal(inferTaskScope({ body: 'Continue the project, only complete boundary item 2' }), 'scoped_task');
10
16
  assert.equal(inferTaskScope({ body: '继续同一整体项目,取消团队功能并从项目完成门槛中移除,其他 backlog 不变' }), 'scoped_task');
11
17
  assert.equal(inferTaskScope({ body: 'Continue the overall project with a product-scope amendment: remove the team feature from the completion gate.' }), 'scoped_task');
18
+ assert.equal(inferTaskScope({ body: '把外部证据列为项目级 deferred gates。本轮作为有界本地工程里程碑,完成 TECH-01030;不宣称整体项目完成。' }), 'scoped_task');
19
+ assert.equal(inferTaskScope({ body: 'Project-level evidence remains deferred; this round is a bounded local engineering milestone.' }), 'scoped_task');
20
+
21
+ for (const explicitScope of [true, false]) {
22
+ const root = await mkdtemp(path.join(tmpdir(), 'loop-project-plan-'));
23
+ try {
24
+ const task = {
25
+ id: 'project-scope-probe', title: 'First batch',
26
+ body: '开发完整项目并完成整体目标', queue: 'agent-tasks',
27
+ ...(explicitScope ? { taskScope: 'project' } : {}), projectId: 'probe', riskAssessment: 'model_assessed'
28
+ };
29
+ const dir = path.join(root, 'runtime/loops/agent-tasks/tasks', task.id);
30
+ await mkdir(path.join(dir, 'checkpoints'), { recursive: true });
31
+ await writeFile(path.join(dir, 'checkpoints/cp1.json'), '{}\n');
32
+ await writeFile(path.join(dir, 'task_contract.json'), JSON.stringify({
33
+ task_id: task.id, task_scope: 'project',
34
+ deliverables: ['preserved-project-deliverable'],
35
+ acceptance_summary: 'preserved-project-acceptance'
36
+ }));
37
+ await writeFile(path.join(dir, 'acceptance_plan.json'), JSON.stringify({
38
+ task_id: task.id,
39
+ generated_by: 'project_scope_worker_revision_after_deterministic_planner_v1',
40
+ functional_checks: ['preserved-project-check'],
41
+ regression_checks: [], negative_tests: [], manual_review: [], automation: [], rubric: []
42
+ }));
43
+ await writeFile(path.join(dir, 'dev_plan.json'), JSON.stringify({
44
+ task_id: task.id,
45
+ generated_by: 'project_scope_worker_revision_after_deterministic_planner_v1',
46
+ approach: 'preserved-project-approach',
47
+ checkpoints: [{ id: 'cp1', goal: 'preserved milestone', output_file: 'checkpoints/cp1.json' }]
48
+ }));
49
+ const contract = await writeTaskContract(root, 'agent-tasks', task, {
50
+ projectAuthorization: { project: 'probe', source: 'test',
51
+ production_authorized: false, paid_action_authorized: false }
52
+ });
53
+ const acceptance = await writeAcceptancePlan(root, 'agent-tasks', task, contract);
54
+ const dev = await writeDevPlan(root, 'agent-tasks', task, contract, acceptance);
55
+ assert.equal(contract.contract.task_scope, 'project');
56
+ assert.deepEqual(contract.contract.deliverables, ['preserved-project-deliverable']);
57
+ assert.deepEqual(acceptance.plan.functional_checks, ['preserved-project-check']);
58
+ assert.deepEqual(dev.plan.checkpoints.map((entry) => entry.id), ['cp1']);
59
+ assert.equal(dev.plan.approach, 'preserved-project-approach');
60
+ assert.equal(dev.plan.checkpoint_schema.checkpoint_id, 'cp2');
61
+ const acceptedReviews = { reviews: [{ checkpointId: 'cp1', sequence: 1, status: 'accepted', projectCompletion: { status: 'in_progress' } }] };
62
+ assert.equal(buildFinalJudgement(contract.contract, acceptance.plan, dev.plan, { count: 1 }, acceptedReviews, { dispatchStatus: 'completed' }).outcome, 'project_in_progress');
63
+ const noProgress = buildFinalJudgement(contract.contract, acceptance.plan, dev.plan, { count: 1 }, acceptedReviews, { dispatchStatus: 'completed', priorCheckpointCount: 1 });
64
+ assert.equal(noProgress.outcome, 'needs_revision');
65
+ assert.equal(noProgress.no_progress_continuation, true);
66
+ assert.match(noProgress.reasons[0], /no new committed and accepted milestone/);
67
+ assert.equal(buildFinalJudgement(contract.contract, acceptance.plan, dev.plan, { count: 1 }, acceptedReviews, { dispatchStatus: 'completed', priorCheckpointCount: 0 }).outcome, 'project_in_progress');
68
+
69
+ const repairedPlan = { checkpoints: [{ id: 'cp1' }, { id: 'cp2' }] };
70
+ const repairedReviews = { reviews: [
71
+ { checkpointId: 'cp1', sequence: 1, status: 'accepted' },
72
+ { checkpointId: 'cp2', sequence: 2, status: 'revise' },
73
+ { checkpointId: 'cp3', milestoneId: 'cp2', sequence: 3, status: 'accepted', projectCompletion: { status: 'in_progress' } }
74
+ ] };
75
+ const repairContext = { dispatchStatus: 'completed', priorCheckpointCount: 2, priorPlannedCheckpointCount: 2, priorAcceptedMilestoneIds: ['cp1'] };
76
+ assert.equal(buildFinalJudgement(contract.contract, basePlan, repairedPlan, { count: 3 }, repairedReviews, repairContext).outcome, 'project_in_progress');
77
+ // Re-emitting already accepted work is not progress, even with a new file.
78
+ assert.equal(buildFinalJudgement(contract.contract, basePlan, repairedPlan, { count: 3 }, repairedReviews, { ...repairContext, priorAcceptedMilestoneIds: ['cp1', 'cp2'] }).outcome, 'needs_revision');
79
+ // In-place repair also advances acceptance without increasing file count.
80
+ assert.equal(buildFinalJudgement(contract.contract, basePlan, repairedPlan, { count: 2 }, repairedReviews, repairContext).outcome, 'project_in_progress');
81
+ const completedReviews = { reviews: [{ checkpointId: 'cp1', sequence: 1, status: 'accepted', projectCompletion: { status: 'accepted' } }] };
82
+ assert.notEqual(buildFinalJudgement(contract.contract, acceptance.plan, dev.plan, { count: 1 }, completedReviews, { dispatchStatus: 'completed', priorCheckpointCount: 1 }).outcome, 'needs_revision');
83
+ const committedMissing = { ...dev.plan, checkpoints: [...dev.plan.checkpoints, { id: 'cp2', goal: 'specific committed batch', output_file: 'checkpoints/cp2.json' }] };
84
+ assert.equal(buildFinalJudgement(contract.contract, acceptance.plan, committedMissing, { count: 1 }, acceptedReviews, { dispatchStatus: 'completed' }).outcome, 'needs_revision');
85
+ const accepted170 = Array.from({ length: 170 }, (_, index) => ({ id: `cp${index + 1}`, goal: `bounded batch ${index + 1}` }));
86
+ const reviews170 = { reviews: accepted170.map((entry, index) => ({ checkpointId: entry.id, sequence: index + 1, status: 'accepted', projectCompletion: { status: 'in_progress' } })) };
87
+ const exact170 = { ...dev.plan, checkpoints: accepted170 };
88
+ assert.equal(buildFinalJudgement(contract.contract, acceptance.plan, exact170, { count: 170 }, reviews170, { dispatchStatus: 'completed' }).outcome, 'project_in_progress');
89
+ assert.equal(buildFinalJudgement(contract.contract, acceptance.plan, exact170, { count: 170 }, reviews170, { dispatchStatus: 'completed', priorCheckpointCount: 170 }).outcome, 'needs_revision');
90
+ assert.equal(buildFinalJudgement(contract.contract, acceptance.plan, exact170, { count: 171 }, reviews170, { dispatchStatus: 'completed', priorCheckpointCount: 170, priorPlannedCheckpointCount: 170, priorAcceptedMilestoneIds: accepted170.map(cp => cp.id) }).outcome, 'needs_revision');
91
+ const progressed171 = { ...exact170, checkpoints: [...accepted170, { id: 'cp171', goal: 'selected batch' }] };
92
+ const accepted171 = { reviews: [...reviews170.reviews, { checkpointId: 'cp171', sequence: 171, status: 'accepted', projectCompletion: { status: 'in_progress' } }] };
93
+ assert.equal(buildFinalJudgement(contract.contract, acceptance.plan, progressed171, { count: 171 }, accepted171, { dispatchStatus: 'completed', priorCheckpointCount: 170 }).outcome, 'project_in_progress');
94
+ assert.equal(buildFinalJudgement(contract.contract, acceptance.plan, progressed171, { count: 171 }, accepted171, { dispatchStatus: 'completed', priorCheckpointCount: 170, priorPlannedCheckpointCount: 170 }).outcome, 'project_in_progress');
95
+ assert.equal(buildFinalJudgement(contract.contract, acceptance.plan, progressed171, { count: 171 }, accepted171, { dispatchStatus: 'completed', priorCheckpointCount: 170, priorPlannedCheckpointCount: 171 }).outcome, 'project_in_progress');
96
+ assert.equal(buildFinalJudgement(contract.contract, acceptance.plan, { ...exact170, checkpoints: [...accepted170, { id: 'cp171', goal: 'unexecuted placeholder' }] }, { count: 170 }, reviews170, { dispatchStatus: 'completed' }).outcome, 'needs_revision');
97
+ await writeFile(path.join(dir, 'checkpoints/cp1.json'), JSON.stringify({
98
+ task_id: task.id, checkpoint_id: 'cp1', status: 'ready_for_acceptance', summary: 'Accepted local slice',
99
+ files_changed: [], verification: ['focused test passed'], blockers: [], risks: [],
100
+ project_completion: { status: 'in_progress' }
101
+ }));
102
+ await mkdir(path.join(root, 'runtime/loops/agent-tasks/failed'), { recursive: true });
103
+ await writeFile(path.join(root, 'runtime/loops/agent-tasks/failed', `${task.id}.json`), JSON.stringify({ ...task, status: 'needs_revision' }));
104
+ await writeFile(path.join(dir, 'final_judgement.json'), JSON.stringify(noProgress));
105
+ await writeFile(path.join(dir, 'dev_plan.json'), JSON.stringify(dev.plan));
106
+ const newer = new Date(Date.now() + 2000);
107
+ await utimes(path.join(dir, 'dev_plan.json'), newer, newer);
108
+ const refreshed = await refreshTaskAcceptance(root, { queue: 'agent-tasks', taskId: task.id });
109
+ assert.equal(refreshed.status, 'needs_revision');
110
+ assert.equal(JSON.parse(await readFile(path.join(dir, 'final_judgement.json'), 'utf8')).no_progress_continuation, true);
111
+ await writeFile(path.join(dir, 'checkpoints/cp2.json'), JSON.stringify({
112
+ task_id: task.id, checkpoint_id: 'cp2', status: 'ready_for_acceptance', summary: 'Selected next batch',
113
+ files_changed: [], verification: ['next batch test passed'], blockers: [], risks: [],
114
+ project_completion: { status: 'in_progress' }
115
+ }));
116
+ await writeFile(path.join(dir, 'dev_plan.json'), JSON.stringify({
117
+ ...dev.plan, checkpoints: [...dev.plan.checkpoints, { id: 'cp2', goal: 'Selected next batch', output_file: 'checkpoints/cp2.json' }]
118
+ }));
119
+ const latest = new Date(Date.now() + 4000);
120
+ await utimes(path.join(dir, 'dev_plan.json'), latest, latest);
121
+ const resumed = await refreshTaskAcceptance(root, { queue: 'agent-tasks', taskId: task.id });
122
+ assert.equal(resumed.status, 'project_in_progress');
123
+ } finally {
124
+ await rm(root, { recursive: true, force: true });
125
+ }
126
+ }
12
127
 
13
128
  {
14
129
  const devPlan = { checkpoints: [{ id: 'cp1' }] };
@@ -448,3 +563,455 @@ for (const status of ['blocked', 'revise']) {
448
563
  ];
449
564
  assert.deepEqual(selectEffectiveAcceptanceReviews({ checkpoints: [{ id: 'cp1' }] }, { reviews }).map(r => r.checkpointId), ['gate']);
450
565
  }
566
+
567
+ for (const body of [
568
+ '完成完整项目,不要将本轮缩成有界本地里程碑',
569
+ 'Complete the project-level objective, not a bounded milestone',
570
+ 'Complete the project-level objective; do not reduce this round to a bounded local milestone',
571
+ "Complete the project-level objective; don't reduce it to a bounded milestone",
572
+ '完成完整项目;本轮不是有界本地里程碑',
573
+ ]) assert.equal(inferTaskScope({ body }), 'project', body);
574
+
575
+ // Paired negation targets: a project prohibition must not negate a milestone.
576
+ for (const body of [
577
+ '本轮做有界本地里程碑而不是完整项目',
578
+ 'Implement a bounded milestone without completing the project-level objective',
579
+ 'Not the project-level objective, but a bounded milestone',
580
+ '不是完整项目,而是本轮做有界本地里程碑',
581
+ ]) assert.equal(inferTaskScope({ body }), 'scoped_task', body);
582
+ for (const body of [
583
+ '完成完整项目,本轮不做有界本地里程碑',
584
+ '完成完整项目,本轮不应仅做有界本地里程碑',
585
+ 'Complete the project-level objective without reducing it to a bounded milestone',
586
+ 'Complete the project-level objective, never just a bounded milestone',
587
+ ]) assert.equal(inferTaskScope({ body }), 'project', body);
588
+
589
+ // Unrelated prohibitions must not negate the requested object.
590
+ for (const body of [
591
+ '在不部署完整项目的前提下本轮完成有界本地里程碑',
592
+ 'Without deploying the project-level application implement a bounded milestone',
593
+ '完整项目不部署,本轮只完成第一步',
594
+ 'Complete the project-level objective later; only complete a backlog item',
595
+ ]) assert.equal(inferTaskScope({ body }), 'scoped_task', body);
596
+ for (const body of [
597
+ '完成完整项目,不要只完成第一步',
598
+ 'Complete the project-level objective, not merely a bounded milestone or backlog item',
599
+ '完成完整项目,不要仅推进第一步',
600
+ 'Complete the project-level objective, not a backlog item',
601
+ '完成完整项目,不要取消功能',
602
+ ]) assert.equal(inferTaskScope({ body }), 'project', body);
603
+
604
+ for (const body of [
605
+ '完整项目以后做,本轮完成有界可验收里程碑',
606
+ 'Project-level delivery later, implement a bounded local/testable milestone',
607
+ ]) assert.equal(inferTaskScope({ body }), 'scoped_task', body);
608
+
609
+ // Ordinal modifiers and stopping verbs remain local to the negated object.
610
+ for (const body of [
611
+ 'Complete the project-level objective, not just the first bounded milestone',
612
+ 'Complete the project-level objective, without stopping at a bounded milestone',
613
+ 'Complete the project-level objective, do not stop at the first bounded milestone or backlog item',
614
+ 'Complete the project-level objective, not only the next bounded milestone',
615
+ ]) assert.equal(inferTaskScope({ body }), 'project', body);
616
+ for (const body of [
617
+ 'Without deploying the project-level application implement the first bounded milestone',
618
+ 'Do not stop the project-level service while implementing a bounded milestone',
619
+ 'Project-level delivery later; stop at the first bounded milestone',
620
+ 'Complete the first bounded milestone without stopping the project-level service',
621
+ ]) assert.equal(inferTaskScope({ body }), 'scoped_task', body);
622
+
623
+ // Negated object phrases accept modifiers, not a fixed adjective vocabulary.
624
+ for (const modifier of ['initial', 'one', 'second', 'smallest useful', 'independently testable', 'newly requested']) {
625
+ for (const suffix of ['', ', or a backlog item', ', and a backlog item']) {
626
+ const body = `Complete the project-level objective, not merely ${modifier} bounded milestone${suffix}`;
627
+ assert.equal(inferTaskScope({ body }), 'project', body);
628
+ }
629
+ const body = `Project-level delivery later; implement ${modifier} bounded milestone`;
630
+ assert.equal(inferTaskScope({ body }), 'scoped_task', body);
631
+ }
632
+ for (const body of [
633
+ '完成完整项目,不应停留在有界里程碑',
634
+ '完成完整项目,不应仅仅满足于一个有界本地里程碑',
635
+ 'Complete the project-level objective, not a bounded milestone, or a backlog item',
636
+ 'Do not reduce the project-level objective to a bounded milestone',
637
+ 'Complete the project-level objective, not just the initial bounded milestone',
638
+ ]) assert.equal(inferTaskScope({ body }), 'project', body);
639
+ for (const body of [
640
+ 'Do not deploy, implement a bounded milestone for the project-level objective',
641
+ 'Do not stop the service while implementing a bounded milestone for the project-level objective',
642
+ 'Complete the project-level objective later, not today, implement a bounded milestone',
643
+ '完成完整项目之前,在不部署的前提下完成有界里程碑',
644
+ 'Complete the project-level objective later, implement a bounded milestone, or a backlog item',
645
+ ]) assert.equal(inferTaskScope({ body }), 'scoped_task', body);
646
+
647
+ // Independent boundary review: coordination, double negatives and context switches.
648
+ for (const [body, expected] of [
649
+ [
650
+ "Complete the project-level objective, not a bounded milestone, nor a backlog item",
651
+ "project"
652
+ ],
653
+ [
654
+ "Complete the project-level objective, neither a bounded milestone nor a backlog item",
655
+ "project"
656
+ ],
657
+ [
658
+ "Complete the project-level objective, not only a bounded milestone but also not merely a backlog item",
659
+ "project"
660
+ ],
661
+ [
662
+ "Complete the project-level objective, not only a bounded milestone but also the entire delivery",
663
+ "project"
664
+ ],
665
+ [
666
+ "完成完整项目,不仅是有界里程碑,而且不是仅完成第一步",
667
+ "project"
668
+ ],
669
+ [
670
+ "完成完整项目,不做有界里程碑,也不只完成第一步",
671
+ "project"
672
+ ],
673
+ [
674
+ "Project-level delivery later; do not fail to complete a bounded milestone",
675
+ "scoped_task"
676
+ ],
677
+ [
678
+ "Project-level delivery later; do not avoid implementing a bounded milestone",
679
+ "scoped_task"
680
+ ],
681
+ [
682
+ "完整项目以后做,本轮不能不完成有界里程碑",
683
+ "scoped_task"
684
+ ],
685
+ [
686
+ "完整项目以后做,本轮不是不完成有界里程碑",
687
+ "scoped_task"
688
+ ],
689
+ [
690
+ "Project-level delivery later; implement a bounded milestone, not a backlog item",
691
+ "scoped_task"
692
+ ],
693
+ [
694
+ "Complete the project-level objective, not a bounded milestone; implement a backlog item",
695
+ "scoped_task"
696
+ ],
697
+ [
698
+ "Complete the project-level objective, not a bounded milestone, and implement a backlog item",
699
+ "scoped_task"
700
+ ],
701
+ [
702
+ "完成完整项目,不做有界里程碑,改为只完成第一步",
703
+ "scoped_task"
704
+ ],
705
+ [
706
+ "Without deploying anything implement a bounded milestone for the project-level objective",
707
+ "scoped_task"
708
+ ],
709
+ [
710
+ "Do not deploy the service and then implement a bounded milestone for the project-level objective",
711
+ "scoped_task"
712
+ ],
713
+ [
714
+ "Complete the project-level objective, never stop at a bounded milestone, nor a backlog item",
715
+ "project"
716
+ ],
717
+ [
718
+ "Complete the project-level objective, not a bounded milestone, however implement a backlog item",
719
+ "scoped_task"
720
+ ],
721
+ [
722
+ "Complete the project-level objective, not a bounded milestone, nor a backlog item",
723
+ "project"
724
+ ],
725
+ [
726
+ "Complete the project-level objective, not a bounded milestone nor a backlog item",
727
+ "project"
728
+ ],
729
+ [
730
+ "Complete the project-level objective, not a bounded milestone, or a backlog item",
731
+ "project"
732
+ ],
733
+ [
734
+ "Complete the project-level objective, not a bounded milestone or a backlog item",
735
+ "project"
736
+ ],
737
+ [
738
+ "Complete the project-level objective, not a bounded milestone, and a backlog item",
739
+ "project"
740
+ ],
741
+ [
742
+ "Project-level delivery later; implement a bounded milestone",
743
+ "scoped_task"
744
+ ],
745
+ [
746
+ "Project-level delivery later; complete a bounded milestone",
747
+ "scoped_task"
748
+ ],
749
+ [
750
+ "Project-level delivery later; execute a bounded milestone",
751
+ "scoped_task"
752
+ ]
753
+ ]) assert.equal(inferTaskScope({ body }), expected, body);
754
+
755
+ for (const [body, expected] of [
756
+ [
757
+ "Project-level delivery later; do not fail to complete a bounded milestone",
758
+ "scoped_task"
759
+ ],
760
+ [
761
+ "Project-level delivery later; don't fail to complete a bounded milestone",
762
+ "scoped_task"
763
+ ],
764
+ [
765
+ "Project-level delivery later; don’t fail to complete a bounded milestone",
766
+ "scoped_task"
767
+ ],
768
+ [
769
+ "Project-level delivery later; never fail to complete a bounded milestone",
770
+ "scoped_task"
771
+ ],
772
+ [
773
+ "Project-level delivery later; do not avoid complete a bounded milestone",
774
+ "scoped_task"
775
+ ],
776
+ [
777
+ "Project-level delivery later; without failing to complete a bounded milestone",
778
+ "scoped_task"
779
+ ],
780
+ [
781
+ "Complete the project-level objective, neither a bounded milestone, nor a backlog item",
782
+ "project"
783
+ ],
784
+ [
785
+ "Complete the project-level objective, not a bounded milestone, nor a backlog item",
786
+ "project"
787
+ ],
788
+ [
789
+ "Complete the project-level objective, never stop at a bounded milestone, nor a backlog item",
790
+ "project"
791
+ ],
792
+ [
793
+ "Complete the project-level objective later; not a bounded milestone, and implement a bounded milestone",
794
+ "scoped_task"
795
+ ],
796
+ [
797
+ "Complete the project-level objective later; not a bounded milestone, and complete a bounded milestone",
798
+ "scoped_task"
799
+ ],
800
+ [
801
+ "Complete the project-level objective later; not a bounded milestone, and execute a bounded milestone",
802
+ "scoped_task"
803
+ ],
804
+ [
805
+ "Complete the project-level objective, neither a backlog item, nor a backlog item",
806
+ "project"
807
+ ],
808
+ [
809
+ "Complete the project-level objective, not a backlog item, nor a backlog item",
810
+ "project"
811
+ ],
812
+ [
813
+ "Complete the project-level objective, never stop at a backlog item, nor a backlog item",
814
+ "project"
815
+ ],
816
+ [
817
+ "Complete the project-level objective later; not a bounded milestone, and implement a backlog item",
818
+ "scoped_task"
819
+ ],
820
+ [
821
+ "Complete the project-level objective later; not a bounded milestone, and complete a backlog item",
822
+ "scoped_task"
823
+ ],
824
+ [
825
+ "Complete the project-level objective later; not a bounded milestone, and execute a backlog item",
826
+ "scoped_task"
827
+ ],
828
+ [
829
+ "Complete the project-level objective, neither the boundary item, nor a backlog item",
830
+ "project"
831
+ ],
832
+ [
833
+ "Complete the project-level objective, not the boundary item, nor a backlog item",
834
+ "project"
835
+ ],
836
+ [
837
+ "Complete the project-level objective, never stop at the boundary item, nor a backlog item",
838
+ "project"
839
+ ],
840
+ [
841
+ "Complete the project-level objective later; not a bounded milestone, and implement the boundary item",
842
+ "scoped_task"
843
+ ],
844
+ [
845
+ "Complete the project-level objective later; not a bounded milestone, and complete the boundary item",
846
+ "scoped_task"
847
+ ],
848
+ [
849
+ "Complete the project-level objective later; not a bounded milestone, and execute the boundary item",
850
+ "scoped_task"
851
+ ],
852
+ [
853
+ "完整项目以后做,本轮不能不完成有界里程碑",
854
+ "scoped_task"
855
+ ],
856
+ [
857
+ "完整项目以后做,本轮不得不完成有界里程碑",
858
+ "scoped_task"
859
+ ],
860
+ [
861
+ "完整项目以后做,本轮不是不完成有界里程碑",
862
+ "scoped_task"
863
+ ],
864
+ [
865
+ "Complete not only a bounded milestone but also the entire project-level objective",
866
+ "project"
867
+ ],
868
+ [
869
+ "Not only avoid deployment but also implement a bounded milestone for the project-level objective",
870
+ "scoped_task"
871
+ ],
872
+ [
873
+ "本轮不仅完成有界里程碑,而且完成完整项目",
874
+ "project"
875
+ ],
876
+ [
877
+ "完整项目以后做,本轮不仅不部署,而且只完成第一步",
878
+ "scoped_task"
879
+ ],
880
+ [
881
+ "Complete the project-level objective, not a bounded milestone and not a backlog item",
882
+ "project"
883
+ ],
884
+ [
885
+ "Complete the project-level objective, neither a bounded milestone, nor a backlog item",
886
+ "project"
887
+ ]
888
+ ]) assert.equal(inferTaskScope({ body }), expected, body);
889
+
890
+ // Plan labels cannot transfer accepted evidence to a different milestone.
891
+ for (const oldAccepted of [['cp1'], []]) {
892
+ for (const renamedId of ['cp1-renamed', 'cp1-renamed-again', 'cp1-third-name']) {
893
+ const result = buildFinalJudgement(
894
+ { ...baseContract, task_scope: 'project' }, basePlan,
895
+ { checkpoints: [{ id: renamedId }] }, { count: 1 },
896
+ { reviews: [{ checkpointId: 'cp1', milestoneId: 'cp1', sequence: 1, status: 'accepted', projectCompletion: { status: 'in_progress' } }] },
897
+ { dispatchStatus: 'completed', priorCheckpointCount: 1, priorAcceptedMilestoneIds: oldAccepted }
898
+ );
899
+ assert.equal(result.outcome, 'needs_revision', 'plan-only rename: ' + renamedId);
900
+ assert.equal(result.no_progress_continuation, true);
901
+ }
902
+ }
903
+
904
+ // A real queue must stop the renaming worker and never replay it on later ticks.
905
+ {
906
+ const root = await mkdtemp(path.join(tmpdir(), 'loop-rename-queue-'));
907
+ try {
908
+ const { task } = await enqueueTask(root, { queue: 'probe', title: 'rename-only', task: '开发完整项目并完成整体目标' });
909
+ const dir = path.join(root, 'runtime/loops/probe/tasks', task.id);
910
+ await mkdir(path.join(dir, 'checkpoints'), { recursive: true });
911
+ const cp = JSON.stringify({ task_id: task.id, checkpoint_id: 'cp1', milestone_id: 'cp1', sequence: 1, status: 'ready_for_acceptance', summary: 'Accepted local work', verification: ['passed'], blockers: [], risks: [], project_completion: { status: 'in_progress' } });
912
+ await writeFile(path.join(dir, 'checkpoints/cp1.json'), cp);
913
+ await writeFile(path.join(dir, 'dev_plan.json'), JSON.stringify({ task_id: task.id, generated_by: 'project_scope_worker_revision_after_deterministic_planner_v1', checkpoints: [{ id: 'cp1' }] }));
914
+ await writeFile(path.join(dir, 'acceptance_plan.json'), JSON.stringify({ task_id: task.id, generated_by: 'project_scope_worker_revision_after_deterministic_planner_v1', functional_checks: [], regression_checks: [], negative_tests: [], edge_cases: [], manual_review: [], automation: [], rubric: [] }));
915
+ const worker = path.join(root, 'worker.mjs');
916
+ await writeFile(worker, `import { readFile, writeFile, appendFile } from 'node:fs/promises';
917
+ const file = ${JSON.stringify(path.join(dir, 'dev_plan.json'))};
918
+ const p = JSON.parse(await readFile(file, 'utf8')); p.checkpoints[0].id += '-renamed';
919
+ await writeFile(file, JSON.stringify(p));
920
+ await appendFile(${JSON.stringify(path.join(root, 'dispatches'))}, 'dispatch\\n');`);
921
+ const options = { queue: 'probe', dispatcher: `node '${worker}'`, checkpointPollMs: 0, progressHeartbeatMs: 0 };
922
+ assert.equal((await runQueueOnce(root, options)).run.finalJudgement.outcome, 'needs_revision');
923
+ for (let tick = 0; tick < 3; tick++) assert.equal((await runQueueOnce(root, options)).processed, false);
924
+ assert.equal(await readFile(path.join(root, 'dispatches'), 'utf8'), 'dispatch\n');
925
+ assert.equal(await readFile(path.join(dir, 'checkpoints/cp1.json'), 'utf8'), cp);
926
+ } finally {
927
+ await rm(root, { recursive: true, force: true });
928
+ }
929
+ }
930
+
931
+ // Exercise the dispatch-time baseline through real disposable queue runs.
932
+ for (const plannedCount of [1, 2]) for (const scenario of ['repair', 'in-place', 'duplicate', 'empty', 'blocked']) {
933
+ const root = await mkdtemp(path.join(tmpdir(), 'loop-progress-queue-'));
934
+ try {
935
+ const { task } = await enqueueTask(root, { queue: 'probe', title: scenario, task: '开发完整项目并完成整体目标' });
936
+ const dir = path.join(root, 'runtime/loops/probe/tasks', task.id);
937
+ await mkdir(path.join(dir, 'checkpoints'), { recursive: true });
938
+ const checkpoint = (id, sequence, status) => ({ task_id: task.id, checkpoint_id: id, milestone_id: id, sequence, status, summary: 'Local validation', files_changed: [], verification: ['passed'], blockers: [], risks: [], project_completion: { status: 'in_progress' } });
939
+ if (plannedCount === 2) await writeFile(path.join(dir, 'checkpoints/cp1.json'), JSON.stringify(checkpoint('cp1', 1, 'ready_for_acceptance')));
940
+ await writeFile(path.join(dir, 'checkpoints/cp2.json'), JSON.stringify(checkpoint('cp2', 2, ['repair', 'in-place', 'blocked'].includes(scenario) ? 'in_progress' : 'ready_for_acceptance')));
941
+ await writeFile(path.join(dir, 'dev_plan.json'), JSON.stringify({ task_id: task.id, generated_by: 'project_scope_worker_revision_after_deterministic_planner_v1', checkpoints: plannedCount === 2 ? [{ id: 'cp1' }, { id: 'cp2' }] : [{ id: 'cp2' }] }));
942
+ await writeFile(path.join(dir, 'acceptance_plan.json'), JSON.stringify({ task_id: task.id, generated_by: 'project_scope_worker_revision_after_deterministic_planner_v1', functional_checks: [], regression_checks: [], negative_tests: [], edge_cases: [], manual_review: [], automation: [], rubric: [] }));
943
+ const worker = path.join(root, 'worker.mjs');
944
+ const cp = { ...checkpoint(scenario === 'in-place' ? 'cp2' : 'cp3', 3, scenario === 'blocked' ? 'blocked' : 'ready_for_acceptance'), milestone_id: 'cp2', revises_checkpoint_id: 'cp2', blockers: scenario === 'blocked' ? ['Need human approval'] : [] };
945
+ await writeFile(worker, `import { writeFile } from 'node:fs/promises';
946
+ import path from 'node:path';
947
+ if (${JSON.stringify(scenario)} !== 'empty') await writeFile(path.join(process.env.LOOP_CHECKPOINTS_DIR, ${JSON.stringify(cp.checkpoint_id + '.json')}), ${JSON.stringify(JSON.stringify(cp))});`);
948
+ const options = { queue: 'probe', dispatcher: `node '${worker}'`, checkpointPollMs: 0, progressHeartbeatMs: 0 };
949
+ const result = await runQueueOnce(root, options);
950
+ const expected = ['repair', 'in-place'].includes(scenario) ? 'project_in_progress' : scenario === 'blocked' ? 'blocked' : 'needs_revision';
951
+ assert.equal(result.run.finalJudgement.outcome, expected, scenario + JSON.stringify(result.run.finalJudgement));
952
+ if (expected === 'project_in_progress') {
953
+ const second = await runQueueOnce(root, options);
954
+ assert.equal(second.run.finalJudgement.outcome, 'needs_revision', 'repeated accepted milestone must stop');
955
+ } else {
956
+ assert.equal((await runQueueOnce(root, options)).processed, false, 'no automatic replay');
957
+ }
958
+ } finally {
959
+ await rm(root, { recursive: true, force: true });
960
+ }
961
+ }
962
+ console.log('project continuation queue regressions passed');
963
+
964
+ // Invalid history is an unknown baseline, not empty accepted coverage. Permit
965
+ // one repairing dispatch, then stop for explicit revision even if it repairs.
966
+ for (const scenario of ['repair', 'unchanged', 'new-terminal', 'blocked', 'invalid-shape', 'shape-unchanged']) {
967
+ const root = await mkdtemp(path.join(tmpdir(), 'loop-invalid-baseline-'));
968
+ try {
969
+ const { task } = await enqueueTask(root, { queue: 'probe', title: scenario, task: '开发完整项目并完成整体目标' });
970
+ const dir = path.join(root, 'runtime/loops/probe/tasks', task.id);
971
+ await mkdir(path.join(dir, 'checkpoints'), { recursive: true });
972
+ await writeFile(path.join(dir, 'checkpoints/cp1.json'), ['invalid-shape', 'shape-unchanged'].includes(scenario) ? 'null' : '{');
973
+ const cp = { task_id: task.id, checkpoint_id: 'cp1', milestone_id: 'cp1', status: scenario === 'blocked' ? 'blocked' : 'ready_for_acceptance', summary: 'Repair', verification: ['passed'], blockers: scenario === 'blocked' ? ['Need human approval'] : [], risks: [], project_completion: { status: 'in_progress' } };
974
+ const worker = path.join(root, 'worker.mjs');
975
+ const marker = path.join(root, 'worker-ran');
976
+ await writeFile(worker, `import { writeFile } from 'node:fs/promises';
977
+ await writeFile(${JSON.stringify(marker)}, 'yes');
978
+ ${['repair', 'blocked', 'invalid-shape'].includes(scenario) ? `await writeFile(${JSON.stringify(path.join(dir, 'checkpoints/cp1.json'))}, ${JSON.stringify(JSON.stringify(cp))});` : ''}
979
+ ${scenario === 'new-terminal' ? `await writeFile(${JSON.stringify(path.join(dir, 'checkpoints/cp2.json'))}, ${JSON.stringify(JSON.stringify({ ...cp, checkpoint_id: 'cp2', sequence: 99, project_completion: { status: 'accepted' } }))});` : ''}`);
980
+ // A consumed authorization is still not satisfied by unknown history, even
981
+ // with an apparently valid successor beside the corrupt record.
982
+ const gateFile = path.join(root, 'runtime/loops/probe/human-input/gates/consumed.json');
983
+ const gate = { task_id: task.id, checkpoint_id: 'cp1', status: 'consumed', authorization: { state: 'consumed' } };
984
+ await mkdir(path.dirname(gateFile), { recursive: true });
985
+ await writeFile(gateFile, JSON.stringify(gate));
986
+ const preserveGate = ['unchanged', 'new-terminal', 'shape-unchanged'].includes(scenario);
987
+ if (preserveGate) await writeFile(path.join(dir, 'checkpoints/successor.json'), JSON.stringify({ ...cp, checkpoint_id: 'cp-successor', revises_checkpoint_id: 'cp1', sequence: 2 }));
988
+ const options = { queue: 'probe', dispatcher: `node '${worker}'`, checkpointPollMs: 0, progressHeartbeatMs: 0 };
989
+ const result = await runQueueOnce(root, options);
990
+ if (preserveGate) assert.deepEqual(JSON.parse(await readFile(gateFile, 'utf8')), gate, 'corrupt history must preserve consumed gate byte semantics');
991
+ assert.equal(await readFile(marker, 'utf8'), 'yes', scenario + ': repair worker launches');
992
+ if (result.run.finalJudgement.outcome !== (scenario === 'blocked' ? 'blocked' : 'needs_revision')) console.error(JSON.stringify(result.run));
993
+ assert.equal(result.run.finalJudgement.outcome, scenario === 'blocked' ? 'blocked' : 'needs_revision', scenario);
994
+ assert.equal(JSON.parse(await readFile(path.join(dir, 'final_judgement.json'), 'utf8')).invalid_checkpoint_baseline, true, scenario);
995
+ assert.equal((await runQueueOnce(root, options)).processed, false, 'invalid baseline must not replay');
996
+ await refreshTaskAcceptance(root, { queue: 'probe', taskId: task.id });
997
+ const refreshed = JSON.parse(await readFile(path.join(dir, 'final_judgement.json'), 'utf8'));
998
+ assert.equal(refreshed.outcome, scenario === 'blocked' ? 'blocked' : 'needs_revision', 'refresh retains unknown baseline');
999
+ if (preserveGate) assert.deepEqual(JSON.parse(await readFile(gateFile, 'utf8')), gate, 'refresh must preserve consumed gate');
1000
+ if (scenario === 'repair') {
1001
+ // Explicit operator revision only after validated history: do not erase
1002
+ // the marker by refresh or count repairing JSON as new milestone work.
1003
+ await queueRequeue(root, 'probe', task.id, { from: 'failed' });
1004
+ const terminal = { ...cp, checkpoint_id: 'cp2', revises_checkpoint_id: 'cp1', sequence: 2, summary: 'New verified terminal revision', project_completion: { status: 'accepted' } };
1005
+ await writeFile(worker, `import { writeFile } from 'node:fs/promises'; await writeFile(${JSON.stringify(path.join(dir, 'checkpoints/cp2.json'))}, ${JSON.stringify(JSON.stringify(terminal))});`);
1006
+ const revised = await runQueueOnce(root, options);
1007
+ const contract = JSON.parse(await readFile(path.join(dir, 'task_contract.json'), 'utf8'));
1008
+ assert.equal(revised.run.finalJudgement.outcome, contract.requires_human_gate ? 'ready_for_human_review' : 'ready_to_apply', 'explicit revision restores acceptance without bypassing contract gate: ' + JSON.stringify({ judgement: revised.run.finalJudgement, plan: JSON.parse(await readFile(path.join(dir, 'dev_plan.json'), 'utf8')).checkpoints }));
1009
+ assert.equal(JSON.parse(await readFile(path.join(dir, 'final_judgement.json'), 'utf8')).invalid_checkpoint_baseline, false);
1010
+ assert.equal(JSON.parse(await readFile(gateFile, 'utf8')).status, 'satisfied', 'valid successor can satisfy consumed gate after repair');
1011
+ }
1012
+ } finally { await rm(root, { recursive: true, force: true }); }
1013
+ }
1014
+ console.log('invalid baseline controlled repair regressions passed');
1015
+
1016
+ // Durable phase closure protocol and conservative negative cases.
1017
+ await import('./revision-coverage-self-test.mjs');