@aibyzero/cm-workflow 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.
- package/.codex-plugin/plugin.json +1 -1
- package/AGENTS.md +4 -0
- package/CHANGELOG.md +147 -0
- package/README.md +44 -3
- package/VERSION +1 -1
- package/docs/installation.md +1 -1
- package/docs/js-workflow-control.md +46 -7
- package/docs/next-steps.md +100 -0
- package/docs/untested-branches.md +24 -18
- package/package.json +1 -1
- package/runtime/js/cm-ai/claude-review-stream.mjs +18 -0
- package/runtime/js/cm-ai/claude-tool-preview.mjs +45 -6
- package/runtime/js/cm-ai/cm-ai-admission.mjs +7 -1
- package/runtime/js/cm-ai/cm-ai-conversation-entry.mjs +22 -12
- package/runtime/js/cm-ai/cm-ai-qa-log.mjs +70 -5
- package/runtime/js/cm-ai/cm-ai-run-finalizer.mjs +5 -5
- package/runtime/js/cm-ai/drive-core.mjs +68 -0
- package/runtime/js/cm-ai/durable-runner-state.mjs +42 -9
- package/runtime/js/cm-ai/effect-contract.mjs +4 -3
- package/runtime/js/cm-ai/execution-snapshot.mjs +40 -0
- package/runtime/js/cm-ai/host-conversation-execution.mjs +8 -5
- package/runtime/js/cm-ai/host-handoff.mjs +9 -14
- package/runtime/js/cm-ai/host-qa-fix-owner.mjs +12 -7
- package/runtime/js/cm-ai/host-session.mjs +10 -3
- package/runtime/js/cm-ai/host.mjs +1 -1
- package/runtime/js/cm-ai/qa-config-revision.mjs +85 -0
- package/runtime/js/cm-ai/review-evidence-file.mjs +18 -1
- package/runtime/js/cm-ai/reviewed-evidence-supersede.mjs +144 -0
- package/runtime/js/cm-ai/reviewed-evidence-supersession-record.mjs +34 -0
- package/runtime/js/cm-ai/task-runner.mjs +41 -8
- package/runtime/js/cm-ai/worker-claude-developer.mjs +16 -7
- package/runtime/js/cm-fix/cause-invocation.mjs +9 -5
- package/runtime/js/cm-fix/cause-package.mjs +21 -0
- package/runtime/js/cm-fix/dossier.mjs +40 -1
- package/runtime/js/cm-fix/execution.mjs +360 -83
- package/runtime/js/cm-fix/host.mjs +7 -3
- package/runtime/js/cm-fix/layout.mjs +14 -6
- package/runtime/js/cm-fix/progress.mjs +14 -0
- package/runtime/js/cm-fix/protected-edits.mjs +1 -1
- package/runtime/js/cm-fix/red-test.mjs +7 -6
- package/runtime/js/cm-fix/regression-evidence.mjs +6 -3
- package/runtime/js/cm-fix/regression.mjs +16 -10
- package/runtime/js/cm-fix/repair.mjs +16 -13
- package/runtime/js/cm-fix/test-author.mjs +2 -1
- package/runtime/js/cm-fix/test-extension.mjs +45 -0
- package/runtime/js/cm-prd/accepted-design.mjs +28 -9
- package/runtime/js/cm-prd/analysis.mjs +68 -16
- package/runtime/js/cm-prd/change.mjs +21 -1
- package/runtime/js/cm-prd/draft-save.mjs +96 -2
- package/runtime/js/cm-prd/failed-correction.mjs +51 -0
- package/runtime/js/cm-prd/inputs-replaced.mjs +122 -0
- package/runtime/js/cm-prd/review-correction.mjs +3 -0
- package/runtime/js/cm-prd/review-disposition.mjs +33 -15
- package/runtime/js/cm-prd/review-findings.mjs +2 -2
- package/runtime/js/cm-prd/review-host.mjs +2 -0
- package/runtime/js/cm-prd/review-preparation.mjs +28 -1
- package/runtime/js/cm-prd/review-publication.mjs +4 -2
- package/runtime/js/cm-prd/self-check-revision.mjs +81 -0
- package/runtime/js/cm-prd/session.mjs +5 -5
- package/runtime/js/cm-prd/split-design.mjs +67 -0
- package/runtime/js/cm-prd/summary.mjs +57 -10
- package/runtime/js/cm-refactor/workflow.mjs +100 -19
- package/runtime/js/specs-status.mjs +6 -2
- package/scripts/cm-ai-admission.mjs +2 -0
- package/scripts/cm-ai-batch-drive.mjs +251 -0
- package/scripts/cm-ai-batch-drive.test.mjs +159 -0
- package/scripts/cm-ai-batch-host.mjs +13 -5
- package/scripts/cm-ai-batch-host.test.mjs +25 -0
- package/scripts/cm-ai-contracts.test.mjs +30 -0
- package/scripts/cm-ai-conversation-entry-ops.test.mjs +62 -0
- package/scripts/cm-ai-drive.mjs +335 -0
- package/scripts/cm-ai-drive.test.mjs +258 -0
- package/scripts/cm-ai-fix-integration.test.mjs +1 -1
- package/scripts/cm-ai-host.mjs +48 -20
- package/scripts/cm-ai-host.test.mjs +79 -0
- package/scripts/cm-ai-qa-config-revision.test.mjs +328 -0
- package/scripts/cm-ai-qa-fix-cross-session.test.mjs +255 -0
- package/scripts/cm-ai-qa-fix-policy.test.mjs +79 -0
- package/scripts/cm-ai-review-timeout.test.mjs +30 -0
- package/scripts/cm-ai-run.mjs +59 -11
- package/scripts/cm-ai-runner-error-code-regression.test.mjs +29 -0
- package/scripts/cm-ai-session-chain.test.mjs +261 -0
- package/scripts/cm-ai-supersede.test.mjs +320 -0
- package/scripts/cm-check-drive.mjs +94 -0
- package/scripts/cm-check-drive.test.mjs +78 -0
- package/scripts/cm-check-entry.mjs +13 -2
- package/scripts/cm-check-host.mjs +2 -2
- package/scripts/cm-check-host.test.mjs +103 -0
- package/scripts/cm-check-runtime.sh +15 -6
- package/scripts/cm-claude-developer-worker.test.mjs +40 -1
- package/scripts/cm-claude-probe.test.mjs +117 -2
- package/scripts/cm-claude-review-stream.test.mjs +43 -0
- package/scripts/cm-fix-abandon.test.mjs +383 -0
- package/scripts/cm-fix-cause-package.test.mjs +31 -1
- package/scripts/cm-fix-cross-session-host.test.mjs +17 -5
- package/scripts/cm-fix-drive.mjs +168 -0
- package/scripts/cm-fix-drive.test.mjs +117 -0
- package/scripts/cm-fix-escalation.test.mjs +214 -0
- package/scripts/cm-fix-execution.test.mjs +26 -0
- package/scripts/cm-fix-host.mjs +4 -2
- package/scripts/cm-fix-observation-dossier.test.mjs +34 -9
- package/scripts/cm-fix-qa-source.test.mjs +3 -3
- package/scripts/cm-fix-session-chain.test.mjs +244 -0
- package/scripts/cm-fix-test-extension.test.mjs +246 -0
- package/scripts/cm-fix-visual-carrier.test.mjs +66 -0
- package/scripts/cm-host-handoff.test.mjs +37 -0
- package/scripts/cm-idea-drive.mjs +99 -0
- package/scripts/cm-idea-drive.test.mjs +50 -0
- package/scripts/cm-init-drive.mjs +191 -0
- package/scripts/cm-init-drive.test.mjs +59 -0
- package/scripts/cm-log-event.mjs +9 -0
- package/scripts/cm-prd-completion.test.mjs +30 -2
- package/scripts/cm-prd-design-phase.test.mjs +355 -15
- package/scripts/cm-prd-drive.mjs +314 -0
- package/scripts/cm-prd-drive.test.mjs +164 -0
- package/scripts/cm-prd-host.mjs +46 -17
- package/scripts/cm-prd-inputs-replaced.test.mjs +237 -0
- package/scripts/cm-prd-review-disposition.test.mjs +133 -7
- package/scripts/cm-prd-review-gate.mjs +23 -4
- package/scripts/cm-prd-self-check-revision.test.mjs +302 -0
- package/scripts/cm-prd-summary.test.mjs +79 -4
- package/scripts/cm-refactor-drive.mjs +237 -0
- package/scripts/cm-refactor-drive.test.mjs +77 -0
- package/scripts/cm-refactor-gaps.test.mjs +276 -0
- package/scripts/cm-refactor-host.test.mjs +1 -48
- package/scripts/cm-refactor-judge-revision.test.mjs +253 -0
- package/scripts/cm-specs-status.test.mjs +19 -0
- package/scripts/cm-test-drive.mjs +132 -0
- package/scripts/cm-test-drive.test.mjs +91 -0
- package/scripts/cm-test-session.test.mjs +117 -0
- package/scripts/correction-check.test.mjs +38 -0
- package/scripts/fixtures/claude-model-marker.mjs +6 -0
- package/scripts/fixtures/claude-review-process.mjs +7 -0
- package/scripts/fixtures/cm-refactor.mjs +56 -0
- package/scripts/test-shell-compat.sh +67 -0
- package/skills/cm-ai/SKILL.md +1 -1
- package/skills/cm-ai/references/N4-review.md +2 -0
- package/skills/cm-ai/references/N8-finish.md +1 -0
- package/skills/cm-ai/references/js-host.md +85 -3
- package/skills/cm-check/SKILL.md +6 -1
- package/skills/cm-check/references/js-host.md +4 -2
- package/skills/cm-fix/SKILL.md +11 -4
- package/skills/cm-fix/references/js-host.md +67 -2
- package/skills/cm-fix/references/test-extension.md +57 -0
- package/skills/cm-idea/references/js-host.md +2 -0
- package/skills/cm-init/references/js-host.md +2 -0
- package/skills/cm-prd/SKILL.md +8 -8
- package/skills/cm-prd/references/js-change-recovery.md +71 -0
- package/skills/cm-prd/references/js-host.md +64 -2
- package/skills/cm-prd/references/spec-self-check.md +14 -2
- package/skills/cm-refactor/SKILL.md +5 -0
- package/skills/cm-refactor/references/js-batch.md +3 -1
- package/skills/cm-refactor/references/js-host.md +49 -0
- package/skills/cm-test/references/js-host.md +5 -0
- package/skills/cm-test/references/unit-coverage.md +2 -2
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
// Current-host analysis controller. No provider, specification writer or approval authority.
|
|
2
2
|
import path from 'node:path';
|
|
3
|
+
import {assertPrdBatchActive} from './inputs-replaced.mjs';
|
|
4
|
+
import {createPrdSelfCheckRevision,inspectPrdSelfCheckRevision,readPrdSelfCheckRevision} from './self-check-revision.mjs';
|
|
3
5
|
import fs from 'node:fs';
|
|
4
6
|
import {inspectCmPrdAdmission,inspectCmPrdSources} from '../../../scripts/cm-prd-entry.mjs';
|
|
5
7
|
import {loadConfig,resolveRole} from '../../../scripts/cm-workflow-config.mjs';
|
|
@@ -7,46 +9,78 @@ import {need,shape,json,digest} from '../cm-ai/effect-contract.mjs';
|
|
|
7
9
|
import {inspectPrdMaterialResults} from './materials.mjs';
|
|
8
10
|
import {prdFeatureInventory,nextPrdFeatureIndex,inspectPrdDraft,inspectPrdDesignDraft} from './draft.mjs';
|
|
9
11
|
import {checkPrdDraftMechanics,inspectPrdContextCheck} from './self-check.mjs';
|
|
12
|
+
import {acceptedPrdDraftForSave} from './draft-save.mjs';
|
|
10
13
|
import {preparePrdReview} from './review-preparation.mjs';
|
|
11
14
|
import {inspectAcceptedPrdDesign} from './accepted-design.mjs';
|
|
12
15
|
import {inspectPrdDesignRiskSelection} from './design-risk.mjs';
|
|
13
16
|
import {readCmInitSource} from '../cm-init/draft-inspection.mjs';
|
|
14
17
|
|
|
15
|
-
export function createCmPrdAnalysis({input,runtime,analyze,record,processMaterials,generate,checkContext,restored=null}){
|
|
18
|
+
export function createCmPrdAnalysis({input,runtime,analyze,record,processMaterials,generate,checkContext,restored=null,allowInputDrift=false}){
|
|
16
19
|
need(['codex','claude'].includes(runtime)&&typeof analyze==='function'&&typeof record==='function','prd_host_invalid');
|
|
17
20
|
input=json(input);
|
|
18
21
|
const admission=inspectCmPrdAdmission(input);
|
|
19
22
|
need(admission.status==='ready'&&admission.mode==='new','prd_admission_blocked');
|
|
23
|
+
assertPrdBatchActive(admission.specs);
|
|
20
24
|
// Invalid routing must fail before source bodies or analysis are consumed.
|
|
21
25
|
const config=loadConfig({projectRoot:admission.project});
|
|
22
26
|
const roles=json(Object.fromEntries(['analyst','planner'].map(role=>[role,resolveRole(config,role,runtime)])));
|
|
23
|
-
const snapshot=inspectCmPrdSources(input),sourceDigest=digest(snapshot);
|
|
27
|
+
const snapshot=inspectCmPrdSources(input),sourceDigest=restored?.sourceDigest??digest(snapshot);
|
|
28
|
+
const configDigest=restored?.configDigest??digest(config);
|
|
24
29
|
let stage='ready',result=null,materialEvidence=null,draft=null;const messages=[],planning=[],controller=new AbortController();
|
|
25
30
|
let selfCheckRound=0,contextCheck=null;const selfCheckHistory=[];
|
|
26
31
|
let designDraft=null,designPlanning=false;
|
|
27
32
|
let acceptedDesign=null;
|
|
33
|
+
let selfCheckRevision=null,selfCheckRevisionSaved=false;
|
|
28
34
|
let designRiskSelection=null;
|
|
29
35
|
let designPromotion=null;
|
|
30
36
|
let promotionOriginal=null;
|
|
31
37
|
const materials=[...snapshot.sourceInspection.sources,
|
|
32
38
|
...(snapshot.sourceInspection.userCases?[snapshot.sourceInspection.userCases]:[])];
|
|
33
39
|
const nonempty=value=>typeof value==='string'&&value.trim().length>0;
|
|
34
|
-
const current=()=>{
|
|
40
|
+
const current=(pendingSplit=null,selfCheckRevisionSave=false)=>{
|
|
41
|
+
assertPrdBatchActive(admission.specs);
|
|
35
42
|
need(digest(inspectCmPrdSources(input))===sourceDigest
|
|
36
|
-
&&digest(loadConfig({projectRoot:admission.project}))===
|
|
37
|
-
if(acceptedDesign!==null)
|
|
38
|
-
|
|
43
|
+
&&digest(loadConfig({projectRoot:admission.project}))===configDigest,'prd_inputs_changed');
|
|
44
|
+
if(acceptedDesign!==null){
|
|
45
|
+
if(selfCheckRevision!==null){
|
|
46
|
+
inspectPrdSelfCheckRevision(selfCheckRevision);
|
|
47
|
+
need(selfCheckRevision.acceptedDesignDigest===acceptedDesign.bindingDigest
|
|
48
|
+
&&selfCheckRevision.designDraftDigest===designDraft.draftDigest
|
|
49
|
+
&&selfCheckRevision.draftDigest===draft.draftDigest&&selfCheckRevision.round===selfCheckRound
|
|
50
|
+
&&digest(selfCheckHistory.find(item=>item.round===selfCheckRevision.failedSelfCheck.round))===selfCheckRevision.failedSelfCheckDigest,
|
|
51
|
+
'prd_self_check_revision_binding_changed');
|
|
52
|
+
}
|
|
53
|
+
for(const feature of acceptedDesign.features){
|
|
54
|
+
const saved=readPrdSelfCheckRevision(admission.specs,feature.directory);
|
|
55
|
+
const changed=selfCheckRevision?.features.find(f=>f.directory===feature.directory)?.changedFiles.length;
|
|
56
|
+
need(changed?((saved===null&&!selfCheckRevisionSaved)||digest(saved)===digest(selfCheckRevision)):saved===null,
|
|
57
|
+
'prd_self_check_revision_binding_changed');
|
|
58
|
+
}
|
|
59
|
+
const observed=inspectAcceptedPrdDesign(admission.specs,designDraft,designRiskSelection,{draftDigest:draft?.draftDigest,pendingSplit,selfCheckRevision,
|
|
60
|
+
pendingSelfCheckSave:selfCheckRevisionSave&&!selfCheckRevisionSaved});
|
|
61
|
+
// Validate new bytes above, then compare all original binding fields. Only
|
|
62
|
+
// the authorized requirements/design content is projected back to the saved baseline.
|
|
63
|
+
const features=observed.features.map(feature=>({...feature,documents:feature.documents.map(doc=>['requirements.md','design.md'].includes(doc.path)?
|
|
64
|
+
acceptedDesign.features.find(item=>item.directory===feature.directory).documents.find(item=>item.path===doc.path):doc)}));
|
|
65
|
+
const {bindingDigest,...binding}=observed;
|
|
66
|
+
need(digest({...binding,features})===acceptedDesign.bindingDigest,'prd_accepted_design_changed');
|
|
67
|
+
}
|
|
39
68
|
};
|
|
40
69
|
if(restored!==null){
|
|
41
|
-
need(
|
|
70
|
+
need(['sourceDigest','configDigest'].every(key=>typeof restored[key]==='string'&&/^[a-f0-9]{64}$/.test(restored[key])),
|
|
71
|
+
'prd_inputs_changed');
|
|
72
|
+
need(allowInputDrift||(restored.sourceDigest===digest(snapshot)&&restored.configDigest===digest(config)),'prd_inputs_changed');
|
|
42
73
|
({stage,result,materialEvidence,draft,selfCheckRound,contextCheck,designDraft,designPlanning,
|
|
43
74
|
acceptedDesign,designRiskSelection,designPromotion,promotionOriginal}=restored);
|
|
75
|
+
selfCheckRevision=restored.selfCheckRevision??null;selfCheckRevisionSaved=restored.selfCheckRevisionSaved??false;
|
|
44
76
|
messages.push(...restored.messages);planning.push(...restored.planning);selfCheckHistory.push(...restored.selfCheckHistory);
|
|
45
77
|
if(stage==='cancelled')controller.abort();
|
|
46
78
|
}
|
|
47
79
|
return Object.freeze({
|
|
48
|
-
|
|
49
|
-
|
|
80
|
+
validateCurrent:pendingSplit=>current(pendingSplit),
|
|
81
|
+
checkpoint:()=>json({stage,result,sourceDigest,configDigest,materialEvidence,draft,designDraft,designPlanning,
|
|
82
|
+
acceptedDesign,designRiskSelection,designPromotion,promotionOriginal,selfCheckRound,contextCheck,selfCheckHistory,messages,planning,
|
|
83
|
+
...(selfCheckRevision===null?{}:{selfCheckRevision,selfCheckRevisionSaved})},4*1024*1024),
|
|
50
84
|
status:()=>json({stage,result,sourceDigest,materialEvidence,draft,designDraft,designRiskSelection,designPromotion,selfCheckRound,contextCheck,selfCheckHistory,writeAuthorized:false,completionAuthorized:false}),
|
|
51
85
|
cancel(){controller.abort();stage='cancelled';},
|
|
52
86
|
promoteDesign(value){
|
|
@@ -89,13 +123,17 @@ export function createCmPrdAnalysis({input,runtime,analyze,record,processMateria
|
|
|
89
123
|
},
|
|
90
124
|
prepareReview(reviewStage,feature){
|
|
91
125
|
need(stage==='self_check_reported_passed'||(stage==='design_ready'&&reviewStage==='design'),'prd_review_not_ready');current();
|
|
126
|
+
if(reviewStage==='split'&&selfCheckRevision!==null)need(selfCheckRevisionSaved,'prd_self_check_revision_save_required');
|
|
92
127
|
if(reviewStage==='design'&&designRiskSelection!==null)
|
|
93
128
|
need(Object.values(designRiskSelection.risks.find(item=>item.feature===feature)?.signals??{}).some(Boolean),
|
|
94
129
|
'prd_low_risk_design_review_not_required');
|
|
95
130
|
return preparePrdReview({specs:admission.specs,draft:stage==='design_ready'?designDraft:draft,stage:reviewStage,feature});
|
|
96
131
|
},
|
|
97
|
-
currentDraftForSave(){
|
|
98
|
-
need(stage==='self_check_reported_passed','prd_spec_save_not_ready');current();return draft;
|
|
132
|
+
currentDraftForSave({selfCheckRevisionSave=false}={}){
|
|
133
|
+
need(stage==='self_check_reported_passed','prd_spec_save_not_ready');current(null,selfCheckRevisionSave);return acceptedPrdDraftForSave(admission.specs,draft);
|
|
134
|
+
},
|
|
135
|
+
acceptSelfCheckRevisionSave(){
|
|
136
|
+
need(selfCheckRevision!==null,'prd_self_check_revision_required');current();selfCheckRevisionSaved=true;
|
|
99
137
|
},
|
|
100
138
|
originalPromotedDraft(){
|
|
101
139
|
need(designPromotion?.saved===true&&stage==='self_check_reported_passed','prd_saved_promotion_not_ready');
|
|
@@ -124,6 +162,8 @@ export function createCmPrdAnalysis({input,runtime,analyze,record,processMateria
|
|
|
124
162
|
&&typeof generate==='function','prd_planning_not_ready');
|
|
125
163
|
need(selfCheckRound<2,'prd_self_check_round_limit');
|
|
126
164
|
current();
|
|
165
|
+
const canRevise=acceptedDesign!==null&&draft!==null
|
|
166
|
+
&&(draft.mechanicalSelfCheck.status==='failed'||contextCheck?.status==='failed');
|
|
127
167
|
if(stage==='design_ready')acceptedDesign=inspectAcceptedPrdDesign(admission.specs,designDraft,designRiskSelection);
|
|
128
168
|
const inventory=prdFeatureInventory(admission.specs),nextIndex=acceptedDesign===null?nextPrdFeatureIndex(inventory):
|
|
129
169
|
Number(acceptedDesign.features[0].directory.split('.')[0]);
|
|
@@ -142,6 +182,8 @@ export function createCmPrdAnalysis({input,runtime,analyze,record,processMateria
|
|
|
142
182
|
userAnswers:messages.filter(message=>message.role==='user'),messages:next,role:roles.planner,nextIndex,
|
|
143
183
|
phase:designOnly?'design':acceptedDesign!==null?'tasks_after_design':'full_draft',
|
|
144
184
|
acceptedDesign,
|
|
185
|
+
...(canRevise?{selfCheckRevision:{failedDraftDigest:draft.draftDigest,round:selfCheckRound+1,
|
|
186
|
+
instructions:'The whole-draft self-check failed. You may revise requirements.md/design.md content within the exact existing feature and file inventory. If changed, return a non-empty top-level selfCheckRevisionReason explaining the failed check addressed. Preserve user intent. These changes go to the original split review, not another design review. The two-round limit is unchanged.'}}:{}),
|
|
145
187
|
riskDiscovery:acceptedDesign===null&&draft===null?{
|
|
146
188
|
instructions:'If original Step 9.5 risk is discovered before producing the first full draft, stop before tasks. Return {status:design,summary:actual risk and basis,features:[{name,documents:[{path:requirements.md|design.md,content}]}]} preserving the complete feature scope. This transfers the same conversation to design review; do not generate tasks, call reviewers, write files or claim approval.'}:null,
|
|
147
189
|
sourcePaths:materials.map(source=>({path:source.path,sha256:source.sha256})),
|
|
@@ -149,7 +191,8 @@ export function createCmPrdAnalysis({input,runtime,analyze,record,processMateria
|
|
|
149
191
|
revision:draft===null?null:{draft,contextCheck,round:selfCheckRound+1,
|
|
150
192
|
instructions:'Correct findings only within these existing feature names/directories. Do not add, remove or rename features; retain user intent and tests. No review invocation. Two self-check rounds maximum.'},
|
|
151
193
|
reference:path.join(admission.skillDir,'SKILL.md'),testContract:path.join(admission.workflowRoot,'runtime/test-contract.md'),
|
|
152
|
-
instructions:
|
|
194
|
+
instructions:canRevise?
|
|
195
|
+
'Correct the failed whole-draft self-check in the existing draft schema. Keep exactly the same ordered features and file inventory. Requirements/design changes require selfCheckRevisionReason. Return question or blocked if needed. No writes, review dispatch, invented approval or extra rounds.':acceptedDesign!==null?
|
|
153
196
|
'Follow original cm-prd Step 10 and 10.4 on acceptedDesign. Return the existing draft schema with the exact same ordered features and exact requirements.md/design.md bytes supplied there; generate tasks.md and applicable test-cases.json only. Do not regenerate or revise accepted requirements/design, change scope or call a reviewer. Preserve original user cases and unresolved design findings for human review. Ask material questions with {status:question,question}; no invented user decisions. No writes, provider calls, installation, development or approval. Subsequent original self-check and split review remain required.':designOnly?
|
|
154
197
|
'Follow cm-prd Steps 6-9 only, before original Step 9.5 design review. Read required references and actual relevant project context. Preserve user cases and requirements. No tasks or test-contract generation yet, even embedded in another document. Resolve material or human UI baseline decisions with {status:question,question}; never infer consent. Return {status:design,summary,features:[{name:kebab_slug,documents:[{path:requirements.md|design.md,content:string}]}]} with exactly both documents per feature, or {status:blocked,reason}. No writes, provider calls, browser launch, installation, review claims or development. These are unreviewed memory drafts, not authority to generate tasks.':
|
|
155
198
|
'Follow cm-prd Steps 6-10 for in-memory DRAFTS ONLY. Read required references and relevant project context. Resolve material questions and human UI baseline choices before design: return {status:question,question} and wait, never infer consent. No writes, provider calls, browser launch, installation, review claims or development. Return {status:draft,summary,features:[{name:kebab_slug,documents:[{path:requirements.md|design.md|tasks.md|test-cases.json,content:string}],testCasesReason:null|no_observable_behavior|generation_disabled}]} or {status:blocked,reason}. Each feature needs the triad. Apply functional task granularity <=15, AC/Task references, applicable test contract and original user-case intent; generateCases false forbids generated cases only. This draft is unreviewed; original conditional design review, split review, self-check and human approval remain pending.'},controller.signal),64*1024);
|
|
@@ -163,14 +206,23 @@ export function createCmPrdAnalysis({input,runtime,analyze,record,processMateria
|
|
|
163
206
|
need(draft===null&&acceptedDesign===null&&selfCheckRound===0,'prd_late_design_transition_not_ready');
|
|
164
207
|
designDraft=inspectPrdDesignDraft(reply,{nextIndex});designPlanning=true;stage='design_ready';
|
|
165
208
|
}else{
|
|
166
|
-
const
|
|
209
|
+
const {selfCheckRevisionReason,...draftReply}=reply;
|
|
210
|
+
if(acceptedDesign!==null&&draft!==null){
|
|
211
|
+
const inventory=features=>features.map(f=>({name:f.name,files:f.documents.map(d=>d.path).sort()}));
|
|
212
|
+
need(digest(inventory(reply.features))===digest(inventory(draft.features)),'prd_accepted_design_scope_changed');
|
|
213
|
+
}
|
|
214
|
+
const inspected=inspectPrdDraft(draftReply,{nextIndex,generateCases:config.policies.generate_cases,
|
|
167
215
|
userCasesProvided:snapshot.sourceInspection.userCases!==null});
|
|
168
216
|
if(acceptedDesign!==null){
|
|
169
217
|
need(digest(inspected.features.map(item=>item.directory))===digest(acceptedDesign.features.map(item=>item.directory)),
|
|
170
218
|
'prd_accepted_design_scope_changed');
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
219
|
+
const changed=acceptedDesign.features.some(feature=>feature.documents.some(doc=>
|
|
220
|
+
inspected.features.find(item=>item.directory===feature.directory).documents.find(item=>item.path===doc.path)?.content!==doc.content));
|
|
221
|
+
if(changed){
|
|
222
|
+
need(canRevise,'prd_accepted_design_rewritten');
|
|
223
|
+
selfCheckRevision=createPrdSelfCheckRevision({designDraft,acceptedDesign,draft,contextCheck,round:selfCheckRound,
|
|
224
|
+
revised:inspected,reason:selfCheckRevisionReason});
|
|
225
|
+
}
|
|
174
226
|
}
|
|
175
227
|
if(draft!==null){
|
|
176
228
|
need(digest(inspected.features.map(item=>item.directory))===digest(draft.features.map(item=>item.directory)),
|
|
@@ -1,6 +1,8 @@
|
|
|
1
|
+
import {assertPrdBatchActive} from './inputs-replaced.mjs';
|
|
1
2
|
// Original C1–C8, also used for explicitly confirmed revisions of saved drafts.
|
|
2
3
|
// Proposals are not writes; old review evidence and completed work stay history.
|
|
3
4
|
import fs from 'node:fs';
|
|
5
|
+
import {prdSelfCheckRevisionPath} from './self-check-revision.mjs';
|
|
4
6
|
import path from 'node:path';
|
|
5
7
|
import {createHash} from 'node:crypto';
|
|
6
8
|
import {loadConfig,resolveRole} from '../../../scripts/cm-workflow-config.mjs';
|
|
@@ -35,6 +37,19 @@ export function inspectPrdChangeSnapshot(specs){
|
|
|
35
37
|
const file=`.reviews/prd-${directory.replace(/^\d+\./,'')}-${stage}${suffix}`,bytes=readCmInitSource(specs,file);
|
|
36
38
|
if(bytes!==null)reviews[file]=sha(bytes);
|
|
37
39
|
}
|
|
40
|
+
// Only the new failure outcome adds bindings; legacy snapshots stay identical.
|
|
41
|
+
for(const directory of directories){
|
|
42
|
+
const prefix=`.reviews/prd-${directory.replace(/^\d+\./,'')}-split`;
|
|
43
|
+
const receipt=JSON.parse(read(specs,prefix+'-disposition.json')??'null');
|
|
44
|
+
if(receipt?.disposition==='self_check_failed')for(const suffix of ['-correction-check-start.json','-correction-check-result.json']){
|
|
45
|
+
const file=prefix+suffix,bytes=readCmInitSource(specs,file);
|
|
46
|
+
need(bytes!==null,'prd_failed_check_evidence_required');reviews[file]=sha(bytes);
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
for(const directory of directories){
|
|
50
|
+
const file=prdSelfCheckRevisionPath(directory),bytes=readCmInitSource(specs,file);
|
|
51
|
+
if(bytes!==null)reviews[file]=sha(bytes);
|
|
52
|
+
}
|
|
38
53
|
const status=read(specs,'.cm-specs-status');
|
|
39
54
|
return json({directories,files,trees,reviews,status},4*1024*1024);
|
|
40
55
|
}
|
|
@@ -104,6 +119,7 @@ export function inspectPrdChangeProposal(before,raw,selected,config){
|
|
|
104
119
|
}
|
|
105
120
|
|
|
106
121
|
export function createPrdChange({admission,runtime,call,restored=null,selected=[admission.feature],reason=null}){
|
|
122
|
+
assertPrdBatchActive(admission.specs,selected);
|
|
107
123
|
const config=loadConfig({projectRoot:admission.project}),configDigest=digest(config);
|
|
108
124
|
const readUserCases=()=>admission.cases===null?null:{path:admission.cases,
|
|
109
125
|
content:new TextDecoder('utf8',{fatal:true}).decode(readCmInitSource(path.dirname(admission.cases),path.basename(admission.cases)))};
|
|
@@ -198,7 +214,11 @@ export function assertPrdReviewsSettled(specs,selected,{requireSplit=false}={}){
|
|
|
198
214
|
const feature=directory.replace(/^\d+\./,''),prefix=`prd-${feature}-${stage}`;
|
|
199
215
|
for(const suffix of ['-dispatch.json','-r1.md','-r2.md','-disposition.json'])readCmInitSource(specs,`.reviews/${prefix}${suffix}`);
|
|
200
216
|
const evidencePath=`.reviews/${prefix}-r1.md`,receiptPath=`.reviews/${prefix}-disposition.json`;
|
|
201
|
-
if(historical?.[evidencePath]&&historical?.[receiptPath]&&[evidencePath,receiptPath].every(file=>sha(readCmInitSource(specs,file))===historical[file]))
|
|
217
|
+
if(historical?.[evidencePath]&&historical?.[receiptPath]&&[evidencePath,receiptPath].every(file=>sha(readCmInitSource(specs,file))===historical[file])){
|
|
218
|
+
for(const [file,hash] of Object.entries(historical).filter(([file])=>file.startsWith(`.reviews/${prefix}-correction-check-`)||file===prdSelfCheckRevisionPath(directory)))
|
|
219
|
+
need(sha(readCmInitSource(specs,file))===hash,'prd_change_review_changed');
|
|
220
|
+
continue;
|
|
221
|
+
}
|
|
202
222
|
const gate=inspectPrdReview({stage,feature,evidence:path.join(reviews,`${prefix}-r1.md`),receipt:path.join(reviews,`${prefix}-disposition.json`)});
|
|
203
223
|
need(['dispatch_once','completed'].includes(gate.outcome),'prd_revision_prior_review_unresolved');
|
|
204
224
|
if(requireSplit&&stage==='split')need(gate.outcome==='completed','prd_revision_original_split_required');
|
|
@@ -1,25 +1,38 @@
|
|
|
1
1
|
// Save the current self-checked draft; never issue specification approval.
|
|
2
2
|
import fs from 'node:fs';
|
|
3
|
+
import {assertPrdBatchActive} from './inputs-replaced.mjs';
|
|
4
|
+
import {inspectPrdSelfCheckRevision,readPrdSelfCheckRevision,prdSelfCheckRevisionPath} from './self-check-revision.mjs';
|
|
3
5
|
import path from 'node:path';
|
|
6
|
+
import {TextDecoder} from 'node:util';
|
|
4
7
|
import {createHash} from 'node:crypto';
|
|
5
8
|
import {readCmInitSource} from '../cm-init/draft-inspection.mjs';
|
|
6
9
|
import {writeImmutableWorkflowFile} from '../cm-ai/review-evidence-file.mjs';
|
|
7
10
|
import {need,json,digest} from '../cm-ai/effect-contract.mjs';
|
|
11
|
+
import {inspectPrdFindings} from './review-findings.mjs';
|
|
8
12
|
import {replacePrdDocument} from './review-correction.mjs';
|
|
9
13
|
|
|
10
|
-
export function savePrdDraft(
|
|
11
|
-
|
|
14
|
+
export function savePrdDraft(input){
|
|
15
|
+
assertPrdBatchActive(input.specs);
|
|
16
|
+
if(input.getSelfCheckRevision?.())return saveSelfCheckRevision(input);
|
|
17
|
+
return saveDocuments(input,false);
|
|
12
18
|
}
|
|
13
19
|
export function savePrdDesign({specs,writeEnabled,getDraft}){
|
|
20
|
+
assertPrdBatchActive(specs);
|
|
14
21
|
return saveDocuments({specs,writeEnabled,getDraft},true);
|
|
15
22
|
}
|
|
16
23
|
// Only the analysis owner can supply the original saved promotion and the
|
|
17
24
|
// self-checked successor. Requirements/design are never rewritten here.
|
|
18
25
|
export function savePrdPromotedDraft({specs,writeEnabled,getDraft,getOriginal}){
|
|
26
|
+
assertPrdBatchActive(specs);
|
|
19
27
|
need(writeEnabled===true&&typeof getDraft==='function'&&typeof getOriginal==='function','prd_spec_save_not_enabled');
|
|
20
28
|
const draft=json(getDraft(),256*1024),original=json(getOriginal(),256*1024);
|
|
21
29
|
const inventory=value=>value.features.map(feature=>feature.directory);
|
|
22
30
|
need(digest(inventory(draft))===digest(inventory(original)),'prd_promoted_save_scope_changed');
|
|
31
|
+
if(draft.features.every(feature=>readCmInitSource(specs,`.reviews/prd-${feature.name}-split-disposition.json`)!==null)){
|
|
32
|
+
for(const feature of draft.features)need(inspectPrdFindings({specs,stage:'split',feature:feature.directory}).gate.outcome==='completed',
|
|
33
|
+
'prd_spec_save_split_disposition_required');
|
|
34
|
+
return savePrdDraft({specs,writeEnabled,getDraft});
|
|
35
|
+
}
|
|
23
36
|
const files=[];
|
|
24
37
|
for(const feature of draft.features){
|
|
25
38
|
const prior=original.features.find(item=>item.directory===feature.directory);
|
|
@@ -120,3 +133,84 @@ function saveDocuments({specs,writeEnabled,getDraft},designOnly){
|
|
|
120
133
|
next:'inspect_current_files_before_explicit_resume',completionAuthorized:false});
|
|
121
134
|
}
|
|
122
135
|
}
|
|
136
|
+
|
|
137
|
+
// Re-save a completed split's exact on-disk result without overwriting it with
|
|
138
|
+
// the pre-review memory draft. Unchanged runs return the identical original.
|
|
139
|
+
export function acceptedPrdDraftForSave(specs,draft){
|
|
140
|
+
let changed=false;
|
|
141
|
+
const features=draft.features.map(feature=>{
|
|
142
|
+
const differs=feature.documents.some(doc=>{
|
|
143
|
+
const bytes=readCmInitSource(specs,`${feature.directory}/${doc.path}`);
|
|
144
|
+
return bytes!==null&&!bytes.equals(Buffer.from(doc.content));
|
|
145
|
+
});
|
|
146
|
+
if(!differs||readCmInitSource(specs,`.reviews/prd-${feature.name}-split-disposition.json`)===null)return feature;
|
|
147
|
+
const review=inspectPrdFindings({specs,stage:'split',feature:feature.directory});
|
|
148
|
+
need(review.gate.outcome==='completed'&&review.verdict!=='blocked'&&review.draftDigest===draft.draftDigest,
|
|
149
|
+
'prd_spec_save_split_disposition_required');
|
|
150
|
+
const originals=feature.documents.map(doc=>({path:`${feature.directory}/${doc.path}`,
|
|
151
|
+
sha256:createHash('sha256').update(doc.content).digest('hex')}));
|
|
152
|
+
const order=items=>[...items].sort((a,b)=>a.path.localeCompare(b.path));
|
|
153
|
+
need(digest(order(originals))===digest(order(review.reviewedArtifacts)),'prd_spec_save_split_binding_changed');
|
|
154
|
+
const receipt=JSON.parse(readCmInitSource(specs,review.evidence.replace(/-r1\.md$/,'-disposition.json')).toString('utf8'));
|
|
155
|
+
need(digest(receipt.artifacts.map(item=>item.path).sort())===digest(originals.map(item=>item.path).sort()),
|
|
156
|
+
'prd_spec_save_split_binding_changed');
|
|
157
|
+
const documents=feature.documents.map(doc=>{
|
|
158
|
+
const relative=`${feature.directory}/${doc.path}`,bytes=readCmInitSource(specs,relative);
|
|
159
|
+
need(bytes!==null&&createHash('sha256').update(bytes).digest('hex')===receipt.artifacts.find(item=>item.path===relative)?.sha256,
|
|
160
|
+
'prd_spec_save_split_artifact_changed');
|
|
161
|
+
return {path:doc.path,content:new TextDecoder('utf-8',{fatal:true,ignoreBOM:true}).decode(bytes)};
|
|
162
|
+
});
|
|
163
|
+
changed=true;return {...feature,documents};
|
|
164
|
+
});
|
|
165
|
+
return changed?json({...draft,features,draftDigest:digest({summary:draft.summary,features})},256*1024):draft;
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
// Save only the owner-recorded self-check revision. The immutable record precedes
|
|
169
|
+
// replacement, so a partial write can resume against exactly the same two versions.
|
|
170
|
+
function saveSelfCheckRevision({specs,writeEnabled,getDraft,getSelfCheckRevision,onSelfCheckRevisionSaved}){
|
|
171
|
+
need(writeEnabled===true&&typeof onSelfCheckRevisionSaved==='function','prd_spec_save_not_enabled');
|
|
172
|
+
const revision=inspectPrdSelfCheckRevision(getSelfCheckRevision()),draft=json(getDraft(),256*1024);
|
|
173
|
+
const changed=revision.features.filter(f=>f.changedFiles.length);
|
|
174
|
+
const existing=changed.map(f=>readPrdSelfCheckRevision(specs,f.directory));
|
|
175
|
+
need(existing.every(value=>value===null||digest(value)===digest(revision)),'prd_self_check_revision_binding_changed');
|
|
176
|
+
const files=changed.flatMap(f=>f.documents.filter(d=>f.changedFiles.includes(d.path)).map(d=>({...d,relative:`${f.directory}/${d.path}`})));
|
|
177
|
+
const current=()=>{
|
|
178
|
+
need(digest(getSelfCheckRevision())===digest(revision),'prd_self_check_revision_binding_changed');
|
|
179
|
+
// The owner validates sources, original review bindings, exact revision and
|
|
180
|
+
// partial-save tolerance. Unrelated disk edits still stop the operation.
|
|
181
|
+
need(getDraft().draftDigest===draft.draftDigest,'prd_spec_save_draft_changed');
|
|
182
|
+
for(const file of files){
|
|
183
|
+
const bytes=readCmInitSource(specs,file.relative),observed=bytes===null?null:createHash('sha256').update(bytes).digest('hex');
|
|
184
|
+
need([file.beforeSha256,file.sha256].includes(observed)
|
|
185
|
+
&&(fs.lstatSync(path.join(specs,file.relative)).mode&0o777)===0o600,'prd_spec_save_conflict');
|
|
186
|
+
}
|
|
187
|
+
};
|
|
188
|
+
// Completed split receipts own the latest saved version. Never roll them back.
|
|
189
|
+
if(draft.features.some(f=>readCmInitSource(specs,`.reviews/prd-${f.name}-split-disposition.json`)!==null))
|
|
190
|
+
return saveDocuments({specs,writeEnabled,getDraft},false);
|
|
191
|
+
current();
|
|
192
|
+
need(draft.draftDigest===revision.draftDigest,'prd_self_check_revision_binding_changed');
|
|
193
|
+
for(const feature of draft.features)for(const suffix of ['-r1.md','-r2.md','-dispatch.json','-disposition.json'])
|
|
194
|
+
need(readCmInitSource(specs,`.reviews/prd-${feature.name}-split${suffix}`)===null,'prd_self_check_revision_split_started');
|
|
195
|
+
// Preflight tasks/contracts as well, before replacing accepted documents.
|
|
196
|
+
for(const feature of draft.features)for(const doc of feature.documents){
|
|
197
|
+
const relative=`${feature.directory}/${doc.path}`;if(files.some(f=>f.relative===relative))continue;
|
|
198
|
+
const before=readCmInitSource(specs,relative);
|
|
199
|
+
need(before===null||before.equals(Buffer.from(doc.content)),'prd_spec_save_conflict');
|
|
200
|
+
}
|
|
201
|
+
const reviews=path.join(specs,'.reviews');
|
|
202
|
+
try{fs.mkdirSync(reviews,{mode:0o700});}catch(error){if(error.code!=='EEXIST')throw error;}
|
|
203
|
+
need(fs.realpathSync(reviews)===reviews&&fs.lstatSync(reviews).isDirectory(),'prd_review_path_invalid');
|
|
204
|
+
for(const feature of changed)writeImmutableWorkflowFile({reviewsDir:reviews,
|
|
205
|
+
name:path.basename(prdSelfCheckRevisionPath(feature.directory)),bytes:Buffer.from(JSON.stringify(revision)+'\n'),
|
|
206
|
+
validate:file=>need((fs.lstatSync(file).mode&0o777)===0o600,'prd_spec_save_permissions')});
|
|
207
|
+
try{
|
|
208
|
+
for(const file of files){
|
|
209
|
+
current();if(readCmInitSource(specs,file.relative).equals(Buffer.from(file.content)))continue;
|
|
210
|
+
replacePrdDocument({specs,relative:file.relative,content:file.content,current});
|
|
211
|
+
}
|
|
212
|
+
const result=saveDocuments({specs,writeEnabled,getDraft},false);
|
|
213
|
+
if(result.status==='draft_saved')onSelfCheckRevisionSaved();return result;
|
|
214
|
+
}catch{return json({status:'draft_save_unknown',draftDigest:draft.draftDigest,
|
|
215
|
+
next:'inspect_recorded_self_check_revision_before_explicit_resume',completionAuthorized:false});}
|
|
216
|
+
}
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
// Validate a failed, single correction check without re-entering the review gate.
|
|
2
|
+
import {createHash} from 'node:crypto';
|
|
3
|
+
import {readCmInitSource} from '../cm-init/draft-inspection.mjs';
|
|
4
|
+
import {need,shape,digest,json} from '../cm-ai/effect-contract.mjs';
|
|
5
|
+
import {correctionCheckStore} from './correction-check.mjs';
|
|
6
|
+
import {inspectPrdDispositionPlan} from './disposition-plan.mjs';
|
|
7
|
+
import {checkPrdDraftMechanics,inspectPrdContextCheck} from './self-check.mjs';
|
|
8
|
+
const sha=bytes=>createHash('sha256').update(bytes).digest('hex');
|
|
9
|
+
|
|
10
|
+
export function inspectPrdFailedCorrection({specs,receipt}){
|
|
11
|
+
need(receipt.stage==='split','prd_failed_check_split_only');
|
|
12
|
+
const proof=receipt.correction_check;
|
|
13
|
+
need(proof&&typeof proof==='object','prd_failed_check_evidence_required');
|
|
14
|
+
shape(proof,['input','resultDigest']);shape(proof.input,['draft','decisions','review']);
|
|
15
|
+
const {draft,decisions,review}=proof.input,feature=review.feature;
|
|
16
|
+
need(review.stage==='split'&&feature.replace(/^\d+\./,'')===receipt.feature
|
|
17
|
+
&&review.gate.outcome==='resume_disposition','prd_failed_check_binding');
|
|
18
|
+
const bytes=readCmInitSource(specs,`.reviews/${receipt.evidence}`);
|
|
19
|
+
const line=bytes?.toString('utf8').match(/\n```json\n([^\n]+)\n```\n$/);
|
|
20
|
+
need(line,'prd_failed_check_binding');
|
|
21
|
+
const archive=JSON.parse(line[1]),pkg=archive.reviewPackage,result=archive.response?.result;
|
|
22
|
+
need(archive.version===1&&pkg?.workflow==='cm-prd'&&pkg.stage==='split'&&pkg.feature===feature
|
|
23
|
+
&&digest(pkg)===review.packageDigest&&review.gate.package_sha256===review.packageDigest
|
|
24
|
+
&&pkg.draftDigest===review.draftDigest&&review.verdict!=='blocked'&&review.verdict===result?.verdict
|
|
25
|
+
&&digest(pkg.artifacts)===digest(review.reviewedArtifacts)&&digest(result.findings)===digest(review.findings),
|
|
26
|
+
'prd_failed_check_binding');
|
|
27
|
+
const plan=inspectPrdDispositionPlan(review,decisions,receipt.artifacts);
|
|
28
|
+
need(plan.changed.size>0&&receipt.finding_count===decisions.length&&receipt.unresolved_count===decisions.length,
|
|
29
|
+
'prd_failed_check_binding');
|
|
30
|
+
need(draft.draftDigest===digest(receipt.artifacts)&&draft.features.length===1
|
|
31
|
+
&&draft.features[0].directory===feature&&draft.features[0].name===receipt.feature,'prd_failed_check_binding');
|
|
32
|
+
const artifacts=draft.features[0].documents.map(doc=>({path:`${feature}/${doc.path}`,sha256:sha(Buffer.from(doc.content))}));
|
|
33
|
+
need(digest(artifacts)===digest(receipt.artifacts),'prd_failed_check_binding');
|
|
34
|
+
const mechanical=checkPrdDraftMechanics(draft);
|
|
35
|
+
need(digest(mechanical)===digest(draft.mechanicalSelfCheck),'prd_failed_check_binding');
|
|
36
|
+
const stored=correctionCheckStore({specs,feature,packageDigest:review.packageDigest,inputDigest:digest(proof.input)}).inspect();
|
|
37
|
+
need(stored.status==='recorded','prd_failed_check_evidence_required');
|
|
38
|
+
need(digest(stored.result)===proof.resultDigest,'prd_failed_check_result_changed');
|
|
39
|
+
let failedChecks;
|
|
40
|
+
if(stored.result.outcome==='mechanical_failed'){
|
|
41
|
+
need(mechanical.status==='failed'&&digest(stored.result.result)===digest(mechanical),'prd_failed_check_not_failed');
|
|
42
|
+
failedChecks=mechanical.findings;
|
|
43
|
+
}else{
|
|
44
|
+
need(mechanical.status==='mechanical_subset_passed','prd_failed_check_binding');
|
|
45
|
+
const context=inspectPrdContextCheck(stored.result.result,draft);
|
|
46
|
+
need(context.status==='failed','prd_failed_check_not_failed');
|
|
47
|
+
failedChecks=context.features.flatMap(f=>f.checks.filter(c=>c.status==='failed').map(c=>({feature:f.directory,...c})));
|
|
48
|
+
}
|
|
49
|
+
return json({status:'failed',outcome:stored.result.outcome,failedChecks,decisions,
|
|
50
|
+
independentReview:false,completionAuthorized:false});
|
|
51
|
+
}
|
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
// A terminal history record, never a revision or an approval transfer.
|
|
2
|
+
import fs from 'node:fs';
|
|
3
|
+
import path from 'node:path';
|
|
4
|
+
import {createHash} from 'node:crypto';
|
|
5
|
+
import {need,json,digest} from '../cm-ai/effect-contract.mjs';
|
|
6
|
+
import {readCmInitSource} from '../cm-init/draft-inspection.mjs';
|
|
7
|
+
import {replaceSessionFile,readPrdSessionFile} from './session.mjs';
|
|
8
|
+
import {inspectCmPrdSources} from '../../../scripts/cm-prd-entry.mjs';
|
|
9
|
+
import {loadConfig} from '../../../scripts/cm-workflow-config.mjs';
|
|
10
|
+
import {inspectPrdReview} from '../../../scripts/cm-prd-review-gate.mjs';
|
|
11
|
+
const limit=16*1024*1024;
|
|
12
|
+
const sha=bytes=>createHash('sha256').update(bytes).digest('hex');
|
|
13
|
+
const validId=id=>typeof id==='string'&&/^prd-[a-zA-Z0-9-]{1,80}$/.test(id);
|
|
14
|
+
const directory=(specs,id)=>{
|
|
15
|
+
need(validId(id),'prd_session_id_invalid');return path.join(specs,'.reviews/prd-sessions',id);
|
|
16
|
+
};
|
|
17
|
+
const inputs=entry=>({sourceDigest:digest(inspectCmPrdSources(entry)),configDigest:digest(loadConfig({projectRoot:entry.project}))});
|
|
18
|
+
export function readPrdInputsReplacement(specs,sessionId){
|
|
19
|
+
const dir=directory(specs,sessionId);
|
|
20
|
+
if(!fs.existsSync(dir))return null;
|
|
21
|
+
need(fs.realpathSync(dir)===dir,'prd_session_path_invalid');
|
|
22
|
+
const bytes=readPrdSessionFile(dir,'inputs-replaced.json');if(bytes===null)return null;
|
|
23
|
+
const value=JSON.parse(bytes),{receiptDigest,...body}=value;
|
|
24
|
+
need(value.version===1&&value.status==='inputs_replaced'&&value.sessionId===sessionId
|
|
25
|
+
&&value.sessionState.identity.entry.specs===specs&&digest(body)===receiptDigest,'prd_replacement_changed');
|
|
26
|
+
return value;
|
|
27
|
+
}
|
|
28
|
+
export function assertPrdBatchActive(specs,features){
|
|
29
|
+
const base=path.join(specs,'.reviews/prd-sessions');if(!fs.existsSync(base))return;
|
|
30
|
+
need(fs.realpathSync(base)===base,'prd_session_path_invalid');
|
|
31
|
+
for(const id of fs.readdirSync(base).filter(validId)){
|
|
32
|
+
const receipt=readPrdInputsReplacement(specs,id);
|
|
33
|
+
need(!receipt||features&&features.every(f=>!receipt.features.includes(f)),'prd_batch_inputs_replaced');
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
function fresh(specs,allowEmptyReviews=false){
|
|
37
|
+
need(path.isAbsolute(specs)&&fs.realpathSync(specs)===specs,'prd_successor_not_fresh');
|
|
38
|
+
need(!fs.readdirSync(specs).some(name=>/^\d+\./.test(name)||name==='.cm-specs-status'||name==='.reviews'&&!(allowEmptyReviews&&fs.realpathSync(path.join(specs,name))===path.join(specs,name)&&fs.readdirSync(path.join(specs,name)).length===0)),
|
|
39
|
+
'prd_successor_not_fresh');
|
|
40
|
+
}
|
|
41
|
+
export function replacePrdInputs({session,sessionId,approved,reason,successorSpecs,successorSessionId}){
|
|
42
|
+
need(approved===true&&typeof reason==='string'&&reason.trim(),'prd_replacement_authorization_required');
|
|
43
|
+
need(validId(successorSessionId)&&successorSessionId.length>=8&&successorSessionId!==sessionId,'prd_predecessor_binding');
|
|
44
|
+
const sessionState=session.state,{entry,runtime}=sessionState.identity;
|
|
45
|
+
const request={approved:true,reason,successorSpecs,successorSessionId};
|
|
46
|
+
const existing=readPrdInputsReplacement(entry.specs,sessionId);
|
|
47
|
+
if(existing){need(digest(existing.request)===digest(request),'prd_replacement_conflict');return existing;}
|
|
48
|
+
// Unknown calls remain archived as unknown; this does not resolve or replay them.
|
|
49
|
+
const checkpoint=sessionState.active?.before??sessionState.checkpoint,analysis=checkpoint?.analysis;
|
|
50
|
+
need(analysis&&checkpoint.change===null&&!checkpoint.publishedSummary&&analysis.stage!=='cancelled'
|
|
51
|
+
&&!sessionState.checkpoint?.publishedSummary&&sessionState.checkpoint?.analysis?.stage!=='cancelled',
|
|
52
|
+
'prd_replacement_not_ready');
|
|
53
|
+
const oldInputs={sourceDigest:analysis.sourceDigest,configDigest:analysis.configDigest},replacementInputs=inputs(entry);
|
|
54
|
+
need(digest(oldInputs)!==digest(replacementInputs),'prd_inputs_not_changed');
|
|
55
|
+
need(successorSpecs!==entry.specs&&!successorSpecs.startsWith(entry.specs+path.sep)
|
|
56
|
+
&&!entry.specs.startsWith(successorSpecs+path.sep),'prd_successor_not_fresh');fresh(successorSpecs);
|
|
57
|
+
const nextEntry={...entry,specs:successorSpecs};
|
|
58
|
+
need(inspectCmPrdSources(nextEntry).status==='ready','prd_successor_inputs_invalid');
|
|
59
|
+
need(digest(inspectCmPrdSources(nextEntry).sourceInspection)===digest(inspectCmPrdSources(entry).sourceInspection),
|
|
60
|
+
'prd_successor_inputs_changed');
|
|
61
|
+
const features=(analysis.draft??analysis.designDraft)?.features.map(f=>f.directory)??[];
|
|
62
|
+
const published=readCmInitSource(entry.specs,'.cm-specs-status');
|
|
63
|
+
if(published!==null){
|
|
64
|
+
const status=JSON.parse(published);
|
|
65
|
+
need(Array.isArray(status.features)&&!status.features.some(feature=>features.includes(feature)),
|
|
66
|
+
'prd_replacement_not_ready');
|
|
67
|
+
}
|
|
68
|
+
const reviewRecords=[],unreviewed=[];
|
|
69
|
+
for(const feature of features){
|
|
70
|
+
const slug=feature.replace(/^\d+\./,'');
|
|
71
|
+
for(const stage of ['design','split']){
|
|
72
|
+
const prefix=`prd-${slug}-${stage}`,base=path.join(entry.specs,'.reviews');
|
|
73
|
+
const names=fs.existsSync(base)?fs.readdirSync(base).filter(n=>n.startsWith(prefix+'-')).sort():[];
|
|
74
|
+
for(const name of names){const relative=`.reviews/${name}`,bytes=readCmInitSource(entry.specs,relative);
|
|
75
|
+
reviewRecords.push({path:relative,sha256:sha(bytes),bytes:bytes.toString('base64')});}
|
|
76
|
+
const required=stage==='split'||names.length>0||analysis.designDraft&&!analysis.designRiskSelection||analysis.designRiskSelection?.risks?.some(r=>r.feature===feature&&Object.values(r.signals).some(Boolean));
|
|
77
|
+
if(required){const gate=inspectPrdReview({stage,feature:slug,evidence:path.join(base,prefix+'-r1.md'),receipt:path.join(base,prefix+'-disposition.json')});
|
|
78
|
+
if(gate.outcome!=='completed')unreviewed.push({feature,stage,outcome:gate.outcome});}
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
const body=json({version:1,status:'inputs_replaced',sessionId,reason,request,oldInputs,replacementInputs,
|
|
82
|
+
successor:{sessionId:successorSessionId,entry:nextEntry,runtime,inputs:inputs(nextEntry)},features,reviewRecords,unreviewed,sessionState},limit);
|
|
83
|
+
const receipt={...body,receiptDigest:digest(body)};
|
|
84
|
+
// Compare again before the exclusive durable append. No old file is rewritten.
|
|
85
|
+
need(digest(JSON.parse(readPrdSessionFile(directory(entry.specs,sessionId),'state.json')))===digest(sessionState)
|
|
86
|
+
&&digest(session.state)===digest(sessionState)&&digest(inputs(entry))===digest(replacementInputs)
|
|
87
|
+
&&digest(inputs(nextEntry))===digest(body.successor.inputs),'prd_replacement_inputs_changed');
|
|
88
|
+
for(const item of reviewRecords)need(sha(readCmInitSource(entry.specs,item.path))===item.sha256,'prd_replacement_review_changed');
|
|
89
|
+
replaceSessionFile(directory(entry.specs,sessionId),'inputs-replaced.json',null,JSON.stringify(receipt));
|
|
90
|
+
return receipt;
|
|
91
|
+
}
|
|
92
|
+
export function readPrdPredecessor(specs){
|
|
93
|
+
const bytes=readCmInitSource(specs,'.reviews/prd-predecessor.json');if(bytes===null)return null;
|
|
94
|
+
const link=JSON.parse(bytes),file=link.path;
|
|
95
|
+
need(path.isAbsolute(file)&&path.basename(file)==='inputs-replaced.json','prd_predecessor_binding');
|
|
96
|
+
const oldSpecs=path.resolve(path.dirname(file),'../../..'),id=path.basename(path.dirname(file));
|
|
97
|
+
const receipt=readPrdInputsReplacement(oldSpecs,id);
|
|
98
|
+
need(receipt&&receipt.receiptDigest===link.receiptDigest&&receipt.successor.entry.specs===specs
|
|
99
|
+
&&digest(link)===digest({sessionId:id,path:file,receiptDigest:receipt.receiptDigest,reason:receipt.reason}),
|
|
100
|
+
'prd_predecessor_binding');return link;
|
|
101
|
+
}
|
|
102
|
+
export function bindPrdPredecessor({entry,runtime,sessionId,predecessor}){
|
|
103
|
+
const existing=readPrdPredecessor(entry.specs);
|
|
104
|
+
if(existing){need(!predecessor||predecessor===existing.path,'prd_predecessor_binding');}
|
|
105
|
+
const file=predecessor??existing?.path;if(!file)return null;
|
|
106
|
+
need(path.isAbsolute(file)&&path.basename(file)==='inputs-replaced.json'&&fs.realpathSync(file)===file,'prd_predecessor_binding');
|
|
107
|
+
const oldSpecs=path.resolve(path.dirname(file),'../../..'),oldId=path.basename(path.dirname(file));
|
|
108
|
+
const receipt=readPrdInputsReplacement(oldSpecs,oldId);
|
|
109
|
+
need(receipt&&receipt.successor.sessionId===sessionId&&receipt.successor.runtime===runtime
|
|
110
|
+
&&digest(receipt.successor.entry)===digest(entry),'prd_predecessor_binding');
|
|
111
|
+
const link={sessionId:oldId,path:file,receiptDigest:receipt.receiptDigest,reason:receipt.reason};
|
|
112
|
+
if(existing){
|
|
113
|
+
need(digest(existing)===digest(link),'prd_predecessor_binding');
|
|
114
|
+
if(!fs.existsSync(path.join(directory(entry.specs,sessionId),'state.json')))
|
|
115
|
+
need(digest(inputs(entry))===digest(receipt.successor.inputs),'prd_predecessor_inputs_changed');
|
|
116
|
+
return existing;
|
|
117
|
+
}
|
|
118
|
+
need(digest(inputs(entry))===digest(receipt.successor.inputs),'prd_predecessor_inputs_changed');fresh(entry.specs,true);
|
|
119
|
+
try{fs.mkdirSync(path.join(entry.specs,'.reviews'),{mode:0o700});}catch(error){if(error.code!=='EEXIST')throw error;}
|
|
120
|
+
replaceSessionFile(path.join(entry.specs,'.reviews'),'prd-predecessor.json',null,JSON.stringify(link));
|
|
121
|
+
return link;
|
|
122
|
+
}
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import {assertPrdBatchActive} from './inputs-replaced.mjs';
|
|
1
2
|
// Current host proposes a correction; only the owner writes reviewed spec paths.
|
|
2
3
|
import fs from 'node:fs';
|
|
3
4
|
import path from 'node:path';
|
|
@@ -41,6 +42,7 @@ function inspectProposal(review,proposal){
|
|
|
41
42
|
export function createPrdCorrectionOwner({correct}){
|
|
42
43
|
need(typeof correct==='function','prd_correction_host_required');const attempts=new Set();
|
|
43
44
|
return async({specs,stage,feature,writeEnabled},signal)=>{
|
|
45
|
+
assertPrdBatchActive(specs,[feature]);
|
|
44
46
|
need(writeEnabled===true&&!signal.aborted,'prd_correction_not_enabled');
|
|
45
47
|
const review=inspectPrdFindings({specs,stage,feature});
|
|
46
48
|
const evidenceHash=sha(readCmInitSource(specs,review.evidence));
|
|
@@ -144,6 +146,7 @@ export function inspectPrdCorrectionRecovery(input){
|
|
|
144
146
|
}
|
|
145
147
|
|
|
146
148
|
export function resumePrdCorrection(input,signal){
|
|
149
|
+
assertPrdBatchActive(input.specs,[input.feature]);
|
|
147
150
|
need(input.writeEnabled===true&&!signal.aborted,'prd_correction_not_enabled');
|
|
148
151
|
const value=loadCorrection(input),{specs,stage,feature}=input;
|
|
149
152
|
const current=()=>{
|