@aibyzero/cm-workflow 0.21.3 → 0.21.5
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/CHANGELOG.md +9 -1
- package/README.md +2 -2
- package/VERSION +1 -1
- package/docs/installation.md +1 -1
- package/docs/js-workflow-control.md +1 -0
- package/package.json +1 -1
- package/runtime/js/cm-ai/host-session.mjs +1 -1
- package/runtime/js/cm-prd/accepted-design.mjs +4 -0
- package/runtime/js/cm-prd/disposition-plan.mjs +4 -2
- package/runtime/js/cm-prd/narrow-review.mjs +210 -0
- package/runtime/js/cm-prd/review-correction.mjs +161 -8
- package/runtime/js/cm-prd/review-disposition.mjs +8 -1
- package/runtime/js/cm-prd/review-findings.mjs +8 -0
- package/runtime/js/notify.mjs +1 -0
- package/scripts/ci-test-weights.json +1 -0
- package/scripts/cm-prd-design-phase.test.mjs +36 -4
- package/scripts/cm-prd-drive.mjs +12 -6
- package/scripts/cm-prd-host.mjs +15 -5
- package/scripts/cm-prd-narrow-review.test.mjs +429 -0
- package/scripts/cm-prd-review-gate.mjs +3 -0
- package/skills/cm-prd/SKILL.md +1 -0
- package/skills/cm-prd/references/js-change-recovery.md +2 -0
- package/skills/cm-prd/references/js-host.md +17 -2
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "cm-workflow",
|
|
3
|
-
"version": "0.21.
|
|
3
|
+
"version": "0.21.5",
|
|
4
4
|
"description": "Spec-driven product development workflow for Codex: external research, PRD, implementation, read-only feature testing, review, QA, fixes, refactors, and documentation.",
|
|
5
5
|
"author": {
|
|
6
6
|
"name": "cm-workflow maintainers",
|
package/CHANGELOG.md
CHANGED
|
@@ -5,7 +5,15 @@
|
|
|
5
5
|
|
|
6
6
|
## 未发布
|
|
7
7
|
|
|
8
|
-
## 0.21.
|
|
8
|
+
## 0.21.5 — 2026-11-25
|
|
9
|
+
|
|
10
|
+
- **cm-prd 设计阶段支持修正后窄审,最多两轮**:设计审查返回问题、原修正保存后,同一份审查包下可用 `narrow_review` 记录窄审(审查方、会话、逐条分级、四个是否题、阻断触发场景),作者据此再修正,每轮修正必须有对应审查记录并逐项写处置;第三轮被拒,第二轮仍有阻断停在「待用户拍板」,不自动放行。有窄审记录时,原处置回执和旧入口 `record` 都要窄审通过。只对新写出的原修正存档生效,在途旧运行与 split 阶段不变。
|
|
11
|
+
- **审查模型不可用时可记降级窄审**:`self-degraded` 轮仅留痕,通过也不放行;审查模型恢复后若还剩一轮,可对同一份设计再记一轮独立审查(不得复用作者或降级会话)再放行。
|
|
12
|
+
- **修复窄审通过后拆分阶段改设计被误判为漂移**:窄审关卡改为核对设计回执冻结的字节,并带上已保存的拆分计划。
|
|
13
|
+
|
|
14
|
+
## 0.21.4 — 2026-11-24
|
|
15
|
+
|
|
16
|
+
0.21.3 未能发布到 npm(发布中断后该版本号被 npm 占用),内容原样改用 0.21.4 发布。
|
|
9
17
|
|
|
10
18
|
- **当前会话运行可用 `--call-timeout-minutes`(30–60)放宽单次宿主调用期限**(设备检查常超过检查整步固定的 30 分钟):只用于当前会话运行,不进运行指纹;已因超时停下的运行可在 resume 时补上,恢复时不得低于创建时的值,每次启动在 stderr 报出实际期限;`--protected-config` 运行不接受。起跑体检按该值算检查预算。(#276)
|
|
11
19
|
- **cm-ai 新运行支持软链**:快照和审查记录链接指向。开发可新建、改指或删除符合项目边界的软链,已有运行沿用原基线与审查规则。
|
package/README.md
CHANGED
|
@@ -17,7 +17,7 @@
|
|
|
17
17
|
|
|
18
18
|
## 最近更新
|
|
19
19
|
|
|
20
|
-
**0.21.
|
|
20
|
+
**0.21.5**
|
|
21
21
|
|
|
22
22
|
- **cm-ai 新任务支持软链**:项目里有软链不再一建基线就报错;开发可以新建、改指向或删除软链,CM 会核对它不指出项目、不指进 `.git` 和规格目录。已有任务不受影响。
|
|
23
23
|
- **当前会话运行可放宽单次调用期限**:设备检查等耗时长的步骤,可用 `--call-timeout-minutes`(30–60)放宽,已因超时停下的运行恢复时可补上。
|
|
@@ -293,7 +293,7 @@ $cm-check
|
|
|
293
293
|
|
|
294
294
|
仓库直接分发 Skills 和脚本,无需在仓库根目录运行 `npm install` 或构建。完整安装行为、覆盖范围和卸载说明见[安装指南](docs/installation.md)。
|
|
295
295
|
|
|
296
|
-
需要固定版本时可使用 `npx @aibyzero/cm-workflow@0.21.
|
|
296
|
+
需要固定版本时可使用 `npx @aibyzero/cm-workflow@0.21.5 install`,请在 CM Workflow 源码仓库以外的目录执行,例如用户主目录。npm 安装入口复用原安装器,要求与覆盖范围见[安装指南](docs/installation.md#npm-installation-macos-codex)。
|
|
297
297
|
|
|
298
298
|
## 升级旧版本
|
|
299
299
|
|
package/VERSION
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
0.21.
|
|
1
|
+
0.21.5
|
package/docs/installation.md
CHANGED
|
@@ -76,7 +76,7 @@ Install or upgrade with the same command:
|
|
|
76
76
|
npx @aibyzero/cm-workflow@latest install
|
|
77
77
|
```
|
|
78
78
|
|
|
79
|
-
To pin a version, use `npx @aibyzero/cm-workflow@0.21.
|
|
79
|
+
To pin a version, use `npx @aibyzero/cm-workflow@0.21.5 install` from outside
|
|
80
80
|
the CM Workflow source checkout (for example, your home directory). Inside a
|
|
81
81
|
checkout with the same package name and version, npm can select the local
|
|
82
82
|
uninstalled package and report `cm-workflow: command not found`.
|
|
@@ -1765,6 +1765,7 @@ evidenceDigest绑定完整feature清单、原状态文件和精确规格/审查
|
|
|
1765
1765
|
在改规格前写不可覆盖的`.reviews/prd-{slug}-{stage}-correction.md`,保存原文件正文、原包身份和逐项提案;它是私有host报告,不是批准或处置回执。每次替换前核对原r1精确SHA、原gate仍待处置及全部文件当前hash,0600临时文件替换后fsync/回读;失败保留correction_save_unknown现场,不回滚或重新生成。既有归档返回correction_recovery_required,须走下方显式恢复入口。correction_saved返回decisions/artifacts供上方原自检与处置,不完成审批。当前仅证明临时CLI产品代码完成原稿保存→修订写入→自检→原处置,宿主提案/自检/review仍为合成响应,未做真实语义验收。
|
|
1766
1766
|
`{requestId,operation:"inspect_correction",stage,feature}`只读原v1修订档案:严格UTF8、精确字节重构、原包/feature/stage、完整原文件sha和正文、逐项处置及机械检查后,对当前文件标记matches_correction或needs_correction。只接受原稿或档案修订稿;第三种内容/不安全路径/权限异常直接拒绝。此只读入口在人审停点后也可用。
|
|
1767
1767
|
`{requestId,operation:"resume_correction",stage,feature}`须同样显式启用spec和review写入,只补原档案未完成写入;复用初次写入函数,逐步核对档案/r1/gate/所有文件版本,已匹配文件不重写,已完成处置不得再补改。没有宿主生成或review调用;新进程可读取同一v1档案继续。已完整写入时返回原decisions/artifacts,原处置已完成则提示检查旧回执;这不是审批或新的自检证明。测试重建了全未写和多文件部分写入前缀并由新Node进程补齐,不等于断电/跨平台验收。无档案调用中断、修正自检轮次和完整会话恢复仍待收口。
|
|
1768
|
+
设计阶段窄审轮次(2026-11-24,仅新运行):原修正保存后,`{requestId,operation:"narrow_review",stage:"design",feature,packageDigest,round,designSha256,reviewer,sessionId,independent,at,verdict,summary,findings}` 在同一原 packageDigest 下写不可覆盖的 `.reviews/prd-{slug}-design-narrow-review-rN.json`;`narrow_status` 只读。链为 原修正档案→窄审1→修正1→窄审2,每份记录绑定上一份的摘要与被审 design.md 的 SHA;修正档案 `...-narrow-correction-rN.md` 绑定所答复的审查摘要并保存逐项处置。第 3 轮报 `prd_narrow_round_limit`;第 2 轮仍有阻断→`awaiting_user_decision`,修正、再审与 `review_disposition` 均拒绝,不自动放行。有窄审记录时 `review_disposition` 须窄审 approved(`prd_narrow_review_pending`);无窄审记录与旧流程完全一致。审查模型不可用时可记 `reviewer:"self-degraded"` 降级轮(须 `degradedReason`、`sessionId`=宿主作者上下文、`independent:false`):仅留痕,降级 approved 进入 `awaiting_user_decision` 不放行。若还剩一轮,可对同一份设计再记一轮独立审查(链到降级记录,不得降级、不得复用降级会话)后放行。split 阶段不扩展:已有强制自检,且改动含任务文件,评估后本次不做。
|
|
1768
1769
|
|
|
1769
1770
|
宿主启动时显式追加`--allow-review-write --host-context {真实作者上下文ID}`后,自检报告通过可发`{requestId,operation:"final_review",stage:"design"|"split",feature:"1.slug",mode:"independent"|"self-degraded"}`。这只启用原dispatch/r1写入,不授权provider/CLI启动或外发;当前宿主仍须有真实审查通道权限。独立/降级模式在占用前选定,占用后不能切换补审。
|
|
1770
1771
|
`review-host.mjs`串联重新准备/核对原包→原claim→`prd_review`宿主请求→再核对→原r1发布。请求给出精确packageDigest/examinedPaths、真实作者ID、代码项目及steelman-review参考;宿主核实真实独立上下文并原样转交结果,不得自行伪造来源。取消/status沿同一JSONL控制通道,reviewState单独展示审查进度。
|
package/package.json
CHANGED
|
@@ -17,7 +17,7 @@ export function parseHostInputLimit(raw){
|
|
|
17
17
|
return value;
|
|
18
18
|
}
|
|
19
19
|
const operationNames=new Set(['prepare_diagnosis_module_correction','revision_test_check','prepare_test_type_correction','prepare_command_correction','prepare_verification_recovery','repair_review_response','reconcile_review','retry_review','rediagnose','replace_inputs','read_batch','recover_final_review','abandon_step','abandon_review','rerun_blocked_step','abandon_effect','bootstrap_review_recover','completion_rework','development_rework','revise_scope','develop_redo','advance','start','plan_design','promote_design','select_design_reviews','status','fix_status','fix_advance','fix_action','fix_run','decision','complete','qa','qa_result','prepare_revision','prepare_save',
|
|
20
|
-
'context_refresh','finish','completion_evidence','run_finalize','review_findings','review_disposition','save_draft','save_design','correct_findings','prepare_summary','publish_summary','inspect_correction','resume_correction','cancel','resume','prepare_judge_revision','cause_review_package','cause_review','red_test','baseline','author_tests','repair','regression','retrospective','learning_writeback','handoff','final_review_package','final_review','publish_review','check_n5','post_review_regression','publish_dossier','walkthrough']);
|
|
20
|
+
'context_refresh','finish','completion_evidence','run_finalize','review_findings','review_disposition','save_draft','save_design','correct_findings','prepare_summary','publish_summary','inspect_correction','resume_correction','narrow_review','narrow_status','cancel','resume','prepare_judge_revision','cause_review_package','cause_review','red_test','baseline','author_tests','repair','regression','retrospective','learning_writeback','handoff','final_review_package','final_review','publish_review','check_n5','post_review_regression','publish_dossier','walkthrough']);
|
|
21
21
|
|
|
22
22
|
// Which contract codes may reach the peer is a deliberate boundary: the host
|
|
23
23
|
// exposes chosen ones as a blocked result with a reason and redacts the rest to
|
|
@@ -4,6 +4,7 @@ import {inspectPrdSplitDesign} from './split-design.mjs';
|
|
|
4
4
|
import {TextDecoder} from 'node:util';
|
|
5
5
|
import {createHash} from 'node:crypto';
|
|
6
6
|
import {inspectPrdFindings} from './review-findings.mjs';
|
|
7
|
+
import {assertPrdNarrowDispositionReady} from './review-correction.mjs';
|
|
7
8
|
import {readCmInitSource} from '../cm-init/draft-inspection.mjs';
|
|
8
9
|
import {prdFeatureInventory} from './draft.mjs';
|
|
9
10
|
import {inspectPrdDesignRiskSelection,requireUnreviewedPrdDesign} from './design-risk.mjs';
|
|
@@ -45,6 +46,9 @@ export function inspectAcceptedPrdDesign(specs,original,selection=null,{draftDig
|
|
|
45
46
|
const receiptBytes=readCmInitSource(specs,receiptPath),receipt=JSON.parse(receiptBytes.toString('utf8'));
|
|
46
47
|
need(receipt.artifacts.length===1&&receipt.artifacts[0].path===`${feature.directory}/design.md`,
|
|
47
48
|
'prd_design_receipt_scope_invalid');
|
|
49
|
+
// A receipt written by any path still needs the recorded narrow review chain to have approved the receipt's bytes;
|
|
50
|
+
// a saved split correction may have changed design.md since, which the checks below bind separately.
|
|
51
|
+
assertPrdNarrowDispositionReady({specs,feature:feature.directory,pendingSplit,designSha256:receipt.artifacts[0].sha256});
|
|
48
52
|
need(review.reviewedArtifacts.length===2,'prd_design_review_scope_invalid');
|
|
49
53
|
const documents=feature.documents.map(doc=>{
|
|
50
54
|
const relative=`${feature.directory}/${doc.path}`,bytes=readCmInitSource(specs,relative);
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
// Shared finding-to-artifact accounting for proposal and recorded disposition.
|
|
2
2
|
import {need,shape,json} from '../cm-ai/effect-contract.mjs';
|
|
3
|
-
|
|
3
|
+
// alsoChanged: paths a recorded narrow review chain legitimately changed after the original decisions.
|
|
4
|
+
export function inspectPrdDispositionPlan(review,rawDecisions,rawArtifacts,{alsoChanged=[]}={}){
|
|
4
5
|
const decisions=json(rawDecisions,64*1024),artifacts=json(rawArtifacts,64*1024);
|
|
5
6
|
need(Array.isArray(decisions)&&decisions.length===review.findings.length,'prd_disposition_coverage');
|
|
6
7
|
const originals=new Map(review.reviewedArtifacts.map(item=>[item.path,item.sha256]));
|
|
@@ -27,7 +28,8 @@ export function inspectPrdDispositionPlan(review,rawDecisions,rawArtifacts){
|
|
|
27
28
|
shape(item,['path','sha256']);
|
|
28
29
|
need(originals.has(item.path)&&!seen.has(item.path)&&typeof item.sha256==='string'
|
|
29
30
|
&&/^[0-9a-f]{64}$/.test(item.sha256),'prd_disposition_artifact_inventory');seen.add(item.path);
|
|
30
|
-
|
|
31
|
+
const differs=originals.get(item.path)!==item.sha256;
|
|
32
|
+
need(differs===changed.has(item.path)||(differs&&alsoChanged.includes(item.path)),'prd_disposition_unaccounted_change');
|
|
31
33
|
}
|
|
32
34
|
return {decisions,artifacts,changed,unresolved};
|
|
33
35
|
}
|
|
@@ -0,0 +1,210 @@
|
|
|
1
|
+
// Design-stage narrow review rounds after the single original correction.
|
|
2
|
+
// Evidence only: it never dispatches a reviewer, approves a spec or completes a gate.
|
|
3
|
+
// Chain per original package: original correction archive -> N1 -> (C1) -> N2.
|
|
4
|
+
// Every correction needs the recorded review it answers; a correction is never the
|
|
5
|
+
// last step, so no unreviewed bytes reach the original disposition (2026-11-24: the
|
|
6
|
+
// third design version after a narrow review could not enter CM).
|
|
7
|
+
import fs from 'node:fs';
|
|
8
|
+
import path from 'node:path';
|
|
9
|
+
import {createHash} from 'node:crypto';
|
|
10
|
+
import {TextDecoder} from 'node:util';
|
|
11
|
+
import {readCmInitSource} from '../cm-init/draft-inspection.mjs';
|
|
12
|
+
import {writeImmutableWorkflowFile} from '../cm-ai/review-evidence-file.mjs';
|
|
13
|
+
import {need,json,shape,digest,id} from '../cm-ai/effect-contract.mjs';
|
|
14
|
+
import {inspectPrdDispositionPlan} from './disposition-plan.mjs';
|
|
15
|
+
|
|
16
|
+
export const PRD_NARROW_MAX_ROUNDS=2;
|
|
17
|
+
const PRD_NARROW_SCAN_ROUNDS=8; // stray files of any later round name are refused, not ignored
|
|
18
|
+
const sha=bytes=>createHash('sha256').update(bytes).digest('hex');
|
|
19
|
+
const FLAGS=['security','dataLoss','silentFailure','violatesWrittenRule'];
|
|
20
|
+
const SEVERITIES=['blocker','major','minor'];
|
|
21
|
+
const FINDING_KEYS=['id','severity',...FLAGS,'scenario','message'];
|
|
22
|
+
const REVIEW_KEYS=['version','kind','feature','packageDigest','round','priorDigest','designSha256','reviewer','sessionId',
|
|
23
|
+
'independent','at','verdict','summary','findings'];
|
|
24
|
+
const hex=value=>typeof value==='string'&&/^[a-f0-9]{64}$/.test(value);
|
|
25
|
+
|
|
26
|
+
export function narrowNames(feature){
|
|
27
|
+
const name=feature.replace(/^\d+\./,'');
|
|
28
|
+
return {review:round=>`prd-${name}-design-narrow-review-r${round}.json`,
|
|
29
|
+
correction:round=>`prd-${name}-design-narrow-correction-r${round}.md`};
|
|
30
|
+
}
|
|
31
|
+
// Reviewer's grade plus four yes/no answers decide blocking; the author cannot reclassify.
|
|
32
|
+
export const isBlockingNarrowFinding=item=>item.severity!=='minor'||FLAGS.some(flag=>item[flag]===true);
|
|
33
|
+
|
|
34
|
+
const permissions=file=>need((fs.lstatSync(file).mode&0o777)===0o600,'prd_narrow_file_permissions');
|
|
35
|
+
function readFileBytes(specs,name){
|
|
36
|
+
const bytes=readCmInitSource(specs,`.reviews/${name}`);
|
|
37
|
+
if(bytes===null)return null;permissions(path.join(specs,'.reviews',name));return bytes;
|
|
38
|
+
}
|
|
39
|
+
// Any unexpected read failure (dangling link, unreadable file) becomes the fixed invalid-record code.
|
|
40
|
+
const guarded=read=>(...args)=>{try{return read(...args);}catch(error){
|
|
41
|
+
throw typeof error?.code==='string'&&error.code.startsWith('prd_narrow_')?error:Object.assign(new Error('prd_narrow_record_invalid'),{code:'prd_narrow_record_invalid'});}};
|
|
42
|
+
const readReview=guarded(function readReview(specs,name){
|
|
43
|
+
const bytes=readFileBytes(specs,name);if(bytes===null)return null;
|
|
44
|
+
const value=json(JSON.parse(new TextDecoder('utf-8',{fatal:true,ignoreBOM:true}).decode(bytes)),128*1024);
|
|
45
|
+
need(Buffer.from(JSON.stringify(value)+'\n').equals(bytes),'prd_narrow_record_invalid');return value;
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
export function narrowCorrectionBytes(value){
|
|
49
|
+
return Buffer.from('# Narrow design review correction\n\nNot review approval or completion. Private host-attested material.\n```json\n'
|
|
50
|
+
+JSON.stringify(value)+'\n```\n');
|
|
51
|
+
}
|
|
52
|
+
const readCorrection=guarded(function readCorrection(specs,name){
|
|
53
|
+
const bytes=readFileBytes(specs,name);if(bytes===null)return null;
|
|
54
|
+
const match=new TextDecoder('utf-8',{fatal:true,ignoreBOM:true}).decode(bytes).match(/\n```json\n([^\n]+)\n```\n$/);
|
|
55
|
+
need(match,'prd_narrow_record_invalid');
|
|
56
|
+
const value=json(JSON.parse(match[1]),256*1024);need(narrowCorrectionBytes(value).equals(bytes),'prd_narrow_record_invalid');
|
|
57
|
+
return {value,bytes};
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
function validateFindings(findings){
|
|
61
|
+
need(Array.isArray(findings)&&findings.length<=60,'prd_narrow_findings_invalid');const ids=new Set();
|
|
62
|
+
for(const item of findings){
|
|
63
|
+
shape(item,FINDING_KEYS);
|
|
64
|
+
need(typeof item.id==='string'&&/^[A-Za-z0-9][A-Za-z0-9._-]{0,39}$/.test(item.id)&&!ids.has(item.id),'prd_narrow_findings_invalid');ids.add(item.id);
|
|
65
|
+
need(SEVERITIES.includes(item.severity)&&FLAGS.every(flag=>typeof item[flag]==='boolean'),'prd_narrow_findings_invalid');
|
|
66
|
+
need(typeof item.message==='string'&&item.message.trim()&&item.message.length<=2000&&typeof item.scenario==='string'
|
|
67
|
+
&&item.scenario.length<=2000,'prd_narrow_findings_invalid');
|
|
68
|
+
// A blocking finding must name the concrete trigger; without one it is a suggestion.
|
|
69
|
+
need(!isBlockingNarrowFinding(item)||item.scenario.trim(),'prd_narrow_blocking_scenario_required');
|
|
70
|
+
}
|
|
71
|
+
return findings;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
function validateReviewRecord(value,{feature,packageDigest,round,priorDigest,designSha}){
|
|
75
|
+
const degraded=value?.reviewer==='self-degraded';
|
|
76
|
+
shape(value,degraded?[...REVIEW_KEYS,'degradedReason']:REVIEW_KEYS);
|
|
77
|
+
need(value.version===1&&value.kind==='prd_design_narrow_review'&&value.feature===feature&&value.packageDigest===packageDigest
|
|
78
|
+
&&value.round===round&&value.priorDigest===priorDigest&&value.designSha256===designSha,'prd_narrow_record_invalid');
|
|
79
|
+
// A degraded round (the review model was unavailable, the author model reviewed) is evidence only: it declares
|
|
80
|
+
// itself, carries the reason, and can never be the approving round (see inspectPrdNarrowState).
|
|
81
|
+
need(typeof value.reviewer==='string'&&value.reviewer.trim()&&(degraded?value.independent===false
|
|
82
|
+
&&typeof value.degradedReason==='string'&&value.degradedReason.trim()&&value.degradedReason.length<=1000:value.independent===true),
|
|
83
|
+
'prd_narrow_reviewer_not_independent');
|
|
84
|
+
id(value.sessionId);
|
|
85
|
+
need(typeof value.at==='string'&&/^\d{4}-\d\d-\d\dT\d\d:\d\d:\d\d(?:\.\d+)?Z$/.test(value.at)&&!Number.isNaN(Date.parse(value.at)),'prd_narrow_record_invalid');
|
|
86
|
+
need(typeof value.summary==='string'&&value.summary.trim()&&value.summary.length<=4000,'prd_narrow_record_invalid');
|
|
87
|
+
validateFindings(value.findings);
|
|
88
|
+
const blocking=value.findings.filter(isBlockingNarrowFinding).length;
|
|
89
|
+
need(value.verdict==='approved'?blocking===0:value.verdict==='changes_requested'&&blocking>0,'prd_narrow_verdict_findings_mismatch');
|
|
90
|
+
return value;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
// base: {designSha256 (design.md after the original correction), originalDesignSha256, archiveSha256}.
|
|
94
|
+
export function inspectPrdNarrowState({specs,feature,packageDigest,base,currentDesignSha256}){
|
|
95
|
+
need(hex(packageDigest)&&hex(base.designSha256)&&hex(base.archiveSha256)&&hex(base.requirementsSha256)&&hex(currentDesignSha256),'prd_narrow_binding_invalid');
|
|
96
|
+
const degradedSessions=new Set(),names=narrowNames(feature),rounds=[];let tip=base.designSha256,prior=base.archiveSha256,phase=null,last=null;
|
|
97
|
+
for(let round=1;round<=PRD_NARROW_MAX_ROUNDS;round++){
|
|
98
|
+
const review=readReview(specs,names.review(round)),correction=readCorrection(specs,names.correction(round));
|
|
99
|
+
if(review===null){need(correction===null,'prd_narrow_record_invalid');phase={status:'awaiting_review',round};break;}
|
|
100
|
+
validateReviewRecord(review,{feature,packageDigest,round,priorDigest:prior,designSha:tip});
|
|
101
|
+
// Read-back enforces the same independence the writer does: an independent record never reuses the original
|
|
102
|
+
// author's context or a context that signed a degraded round.
|
|
103
|
+
if(review.reviewer==='self-degraded')degradedSessions.add(review.sessionId);
|
|
104
|
+
else need(review.sessionId!==base.authorContextId&&!degradedSessions.has(review.sessionId),'prd_narrow_reviewer_not_independent');
|
|
105
|
+
const row={round,review,reviewDigest:digest(review),correction:null};rounds.push(row);
|
|
106
|
+
if(review.verdict==='approved'){
|
|
107
|
+
need(correction===null,'prd_narrow_record_invalid');
|
|
108
|
+
// Only an independent approval releases the design; a degraded one stops for the user.
|
|
109
|
+
if(review.reviewer!=='self-degraded'){phase={status:'approved',round};break;}
|
|
110
|
+
// Degraded approval never releases. While a round remains, one independent review of the same bytes may follow
|
|
111
|
+
// (chained to the degraded record) once the review model is back.
|
|
112
|
+
if(round<PRD_NARROW_MAX_ROUNDS){
|
|
113
|
+
prior=sha(Buffer.from(JSON.stringify(review)+'\n'));
|
|
114
|
+
if(readReview(specs,names.review(round+1))!==null)continue;
|
|
115
|
+
}
|
|
116
|
+
phase={status:'awaiting_user_decision',round,reason:'degraded_review_only',
|
|
117
|
+
independentFollowUpAllowed:round<PRD_NARROW_MAX_ROUNDS};break;
|
|
118
|
+
}
|
|
119
|
+
if(round===PRD_NARROW_MAX_ROUNDS){
|
|
120
|
+
need(correction===null,'prd_narrow_record_invalid');phase={status:'awaiting_user_decision',round};break;
|
|
121
|
+
}
|
|
122
|
+
if(correction===null){phase={status:'awaiting_correction',round};break;}
|
|
123
|
+
const value=correction.value;
|
|
124
|
+
shape(value,['version','kind','feature','packageDigest','round','narrowReviewDigest','beforeSha256','afterSha256','before','proposal']);
|
|
125
|
+
need(value.version===1&&value.kind==='prd_design_narrow_correction'&&value.feature===feature&&value.packageDigest===packageDigest
|
|
126
|
+
&&value.round===round&&value.narrowReviewDigest===row.reviewDigest&&value.beforeSha256===tip&&hex(value.afterSha256)
|
|
127
|
+
&&Array.isArray(value.before),'prd_narrow_record_invalid');
|
|
128
|
+
// The archived per-finding decisions must still satisfy the same plan rules as when they were saved.
|
|
129
|
+
const design=`${feature}/design.md`;
|
|
130
|
+
try{
|
|
131
|
+
const proposed=value.proposal;shape(proposed,['decisions','documents']);need(Array.isArray(proposed.documents),'x');
|
|
132
|
+
const requirements=`${feature}/requirements.md`,after=proposed.documents.find(item=>item.path===design);
|
|
133
|
+
// The archived originals must be exactly what the chain reviewed: this round's design tip and the unchanged requirements.
|
|
134
|
+
need(value.before.length===2&&proposed.documents.length===2,'x');
|
|
135
|
+
const beforeDesign=value.before.find(item=>item.path===design),beforeRequirements=value.before.find(item=>item.path===requirements);
|
|
136
|
+
need(beforeDesign&&beforeRequirements&&beforeDesign.sha256===tip&&sha(Buffer.from(beforeDesign.content))===tip
|
|
137
|
+
&&beforeRequirements.sha256===base.requirementsSha256&&sha(Buffer.from(beforeRequirements.content))===base.requirementsSha256,'x');
|
|
138
|
+
const requirementsAfter=proposed.documents.find(item=>item.path===requirements);
|
|
139
|
+
need(requirementsAfter&&sha(Buffer.from(requirementsAfter.content))===base.requirementsSha256,'x');
|
|
140
|
+
const doc=after;need(doc&&sha(Buffer.from(doc.content))===value.afterSha256,'x');
|
|
141
|
+
inspectPrdDispositionPlan({stage:'design',feature,findings:review.findings,
|
|
142
|
+
reviewedArtifacts:value.before.map(item=>({path:item.path,sha256:item.sha256}))},proposed.decisions,
|
|
143
|
+
value.before.map(item=>({path:item.path,sha256:item.path===design?value.afterSha256:item.sha256})));
|
|
144
|
+
}catch{need(false,'prd_narrow_record_invalid');}
|
|
145
|
+
row.correction=value;last=value;tip=value.afterSha256;prior=sha(correction.bytes);
|
|
146
|
+
}
|
|
147
|
+
// Nothing may exist beyond the chain's current phase (a dangling or stray later round is never ignored).
|
|
148
|
+
const exists=name=>{try{fs.lstatSync(path.join(specs,'.reviews',name));return true;}catch(error){if(error.code==='ENOENT')return false;return true;}};
|
|
149
|
+
for(let round=1;round<=PRD_NARROW_SCAN_ROUNDS;round++){
|
|
150
|
+
need(round<=rounds.length||!exists(names.review(round)),'prd_narrow_record_invalid');
|
|
151
|
+
need(rounds[round-1]?.correction||!exists(names.correction(round)),'prd_narrow_record_invalid');
|
|
152
|
+
}
|
|
153
|
+
if(currentDesignSha256!==tip){
|
|
154
|
+
// Only a crash between the durable archive and the design write may differ, and
|
|
155
|
+
// only before the next review exists.
|
|
156
|
+
if(phase.status==='awaiting_review'&&last!==null&&phase.round===last.round+1&¤tDesignSha256===last.beforeSha256)
|
|
157
|
+
phase={status:'correction_incomplete',round:last.round};
|
|
158
|
+
else need(false,rounds.length===0&¤tDesignSha256===base.originalDesignSha256&&base.originalDesignSha256!==base.designSha256
|
|
159
|
+
?'prd_narrow_original_correction_incomplete':'prd_narrow_design_changed');
|
|
160
|
+
}
|
|
161
|
+
return {feature,packageDigest,phase,rounds,tipDesignSha256:tip,priorDigest:prior,currentDesignSha256};
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
export function narrowPublicState(state){
|
|
165
|
+
const nextByPhase={awaiting_review:`record_narrow_review_r${state.phase.round}`,awaiting_correction:'correct_findings',
|
|
166
|
+
correction_incomplete:'correct_findings',approved:'review_disposition',awaiting_user_decision:'user_decision_required'};
|
|
167
|
+
return {status:'narrow_state',feature:state.feature,packageDigest:state.packageDigest,maxRounds:PRD_NARROW_MAX_ROUNDS,
|
|
168
|
+
phase:state.phase,designSha256:state.currentDesignSha256,
|
|
169
|
+
rounds:state.rounds.map(({round,review,reviewDigest,correction})=>({round,verdict:review.verdict,reviewer:review.reviewer,
|
|
170
|
+
sessionId:review.sessionId,reviewDigest,findingCount:review.findings.length,
|
|
171
|
+
blockingCount:review.findings.filter(isBlockingNarrowFinding).length,corrected:correction!==null,
|
|
172
|
+
degraded:review.reviewer==='self-degraded'})),
|
|
173
|
+
next:state.phase.independentFollowUpAllowed?`record_independent_narrow_review_r${state.phase.round+1}`:nextByPhase[state.phase.status],
|
|
174
|
+
independentFollowUpAllowed:state.phase.independentFollowUpAllowed===true,userDecisionRequired:state.phase.status==='awaiting_user_decision',
|
|
175
|
+
autoReleaseAllowed:false,completionAuthorized:false};
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
// The caller (review-correction) supplies the original-correction base and the
|
|
179
|
+
// current design bytes; this writes exactly one new round record, never overwrites.
|
|
180
|
+
export function recordPrdNarrowReview({specs,feature,packageDigest,state,input,authorContextId,originalAuthorContextId,writeEnabled}){
|
|
181
|
+
need(writeEnabled===true,'prd_narrow_not_enabled');
|
|
182
|
+
need(typeof authorContextId==='string','prd_review_host_context_required');
|
|
183
|
+
const degraded=input?.reviewer==='self-degraded';
|
|
184
|
+
shape(input,['round','designSha256','reviewer','sessionId','independent','at','verdict','summary','findings',...(degraded?['degradedReason']:[])]);
|
|
185
|
+
need(Number.isInteger(input.round)&&input.round>=1,'prd_narrow_round_invalid');
|
|
186
|
+
need(input.round<=PRD_NARROW_MAX_ROUNDS,'prd_narrow_round_limit');
|
|
187
|
+
const status=state.phase.status;
|
|
188
|
+
// After a degraded approval one independent follow-up on the same bytes is the only way past the stop.
|
|
189
|
+
const followUp=status==='awaiting_user_decision'&&state.phase.independentFollowUpAllowed===true;
|
|
190
|
+
need(status!=='awaiting_user_decision'||followUp,'prd_narrow_awaiting_user_decision');
|
|
191
|
+
need(status!=='approved','prd_narrow_already_approved');
|
|
192
|
+
need(followUp?input.round===state.phase.round+1&&!degraded:status==='awaiting_review'&&state.phase.round===input.round,
|
|
193
|
+
followUp&°raded?'prd_narrow_reviewer_not_independent':'prd_narrow_round_out_of_order');
|
|
194
|
+
need(input.designSha256===state.currentDesignSha256,'prd_narrow_design_changed');
|
|
195
|
+
// Degraded: the author context itself reviews, so it must declare exactly that context.
|
|
196
|
+
need(degraded?input.sessionId===authorContextId:input.sessionId!==authorContextId&&input.sessionId!==originalAuthorContextId,'prd_narrow_reviewer_not_independent');
|
|
197
|
+
// A context that already signed a degraded (author-model) round can never later sign an independent one, whatever host resumed.
|
|
198
|
+
need(degraded||!state.rounds.some(row=>row.review.reviewer==='self-degraded'&&row.review.sessionId===input.sessionId),
|
|
199
|
+
'prd_narrow_reviewer_not_independent');
|
|
200
|
+
const value=validateReviewRecord({version:1,kind:'prd_design_narrow_review',feature,packageDigest,round:input.round,
|
|
201
|
+
priorDigest:state.priorDigest,designSha256:input.designSha256,reviewer:input.reviewer,sessionId:input.sessionId,
|
|
202
|
+
independent:input.independent,...(degraded?{degradedReason:input.degradedReason}:{}),at:input.at,verdict:input.verdict,summary:input.summary,findings:input.findings},
|
|
203
|
+
{feature,packageDigest,round:input.round,priorDigest:state.priorDigest,designSha:state.currentDesignSha256});
|
|
204
|
+
const bytes=Buffer.from(JSON.stringify(value)+'\n');
|
|
205
|
+
// The reader accepts at most 128 KiB; never write a record the host cannot read back.
|
|
206
|
+
need(bytes.length<=96*1024,'prd_narrow_record_too_large');
|
|
207
|
+
writeImmutableWorkflowFile({reviewsDir:path.join(specs,'.reviews'),name:narrowNames(feature).review(input.round),
|
|
208
|
+
bytes,validate:permissions,exclusive:true});
|
|
209
|
+
return value;
|
|
210
|
+
}
|
|
@@ -4,7 +4,7 @@ import fs from 'node:fs';
|
|
|
4
4
|
import path from 'node:path';
|
|
5
5
|
import {createHash,randomUUID} from 'node:crypto';
|
|
6
6
|
import {TextDecoder} from 'node:util';
|
|
7
|
-
import {inspectPrdFindings} from './review-findings.mjs';
|
|
7
|
+
import {inspectPrdFindings,readPrdReviewAuthor} from './review-findings.mjs';
|
|
8
8
|
import {inspectPrdDispositionPlan} from './disposition-plan.mjs';
|
|
9
9
|
import {checkPrdDraftMechanics} from './self-check.mjs';
|
|
10
10
|
import {prdTaskScopeOptions} from './task-scope-record.mjs';
|
|
@@ -12,6 +12,8 @@ import {inspectPrdDesignDraft} from './draft.mjs';
|
|
|
12
12
|
import {readCmInitSource} from '../cm-init/draft-inspection.mjs';
|
|
13
13
|
import {writeReviewEvidence} from '../cm-ai/review-evidence-file.mjs';
|
|
14
14
|
import {need,json,shape,digest} from '../cm-ai/effect-contract.mjs';
|
|
15
|
+
import {inspectPrdNarrowState,narrowPublicState,narrowNames,narrowCorrectionBytes,recordPrdNarrowReview,
|
|
16
|
+
PRD_NARROW_MAX_ROUNDS} from './narrow-review.mjs';
|
|
15
17
|
const sha=bytes=>createHash('sha256').update(bytes).digest('hex');
|
|
16
18
|
const archiveBytes=value=>Buffer.from('# Original review correction proposal\n\nNot review approval or completion. Private host-attested material.\n```json\n'
|
|
17
19
|
+JSON.stringify(value)+'\n```\n');
|
|
@@ -51,8 +53,13 @@ export function createPrdCorrectionOwner({correct}){
|
|
|
51
53
|
need(review.gate.outcome==='resume_disposition'&&review.verdict!=='blocked'&&review.findings.length>0,
|
|
52
54
|
'prd_correction_not_ready');
|
|
53
55
|
const archiveName=`prd-${feature.replace(/^\d+\./,'')}-${stage}-correction.md`;
|
|
54
|
-
if(readCmInitSource(specs,`.reviews/${archiveName}`)!==null)
|
|
55
|
-
|
|
56
|
+
if(readCmInitSource(specs,`.reviews/${archiveName}`)!==null){
|
|
57
|
+
// Design only: the saved original correction continues through recorded narrow review rounds.
|
|
58
|
+
if(stage==='design'&&prdNarrowCorrectionFlow({specs,stage,feature}))
|
|
59
|
+
return narrowCorrection({specs,feature,correct,attempts,signal});
|
|
60
|
+
return json({status:'correction_recovery_required',
|
|
61
|
+
archive:`.reviews/${archiveName}`,next:'inspect_original_correction_archive_without_regeneration',completionAuthorized:false});
|
|
62
|
+
}
|
|
56
63
|
const read=file=>readCorrectionFile(specs,file);
|
|
57
64
|
const before=review.reviewedArtifacts.map(item=>{
|
|
58
65
|
const bytes=read(item.path);need(sha(bytes)===item.sha256,'prd_correction_inputs_changed');
|
|
@@ -70,7 +77,8 @@ export function createPrdCorrectionOwner({correct}){
|
|
|
70
77
|
for(const item of before)need(sha(read(item.path))===item.sha256,'prd_correction_inputs_changed');
|
|
71
78
|
};
|
|
72
79
|
current();
|
|
73
|
-
|
|
80
|
+
// design archives written by this version opt the run into narrow review rounds; older archives stay legacy
|
|
81
|
+
const archive=archiveBytes({version:1,packageDigest:review.packageDigest,stage,feature,before,proposal,...(stage==='design'?{narrowReview:1}:{})});
|
|
74
82
|
// Archive original bytes and per-finding decisions before replacing any spec.
|
|
75
83
|
writeReviewEvidence({reviewsDir:path.join(specs,'.reviews'),name:archiveName,bytes:archive,
|
|
76
84
|
validate:file=>need((fs.lstatSync(file).mode&0o777)===0o600,'prd_correction_archive_permissions')});
|
|
@@ -111,14 +119,15 @@ export function replacePrdDocument({specs,relative,content,current}){
|
|
|
111
119
|
}finally{if(fd!==undefined)fs.closeSync(fd);try{fs.unlinkSync(temp);}catch(error){if(error.code!=='ENOENT')throw error;}}
|
|
112
120
|
}
|
|
113
121
|
|
|
114
|
-
function loadCorrection({specs,stage,feature}){
|
|
115
|
-
const review=inspectPrdFindings({specs,stage,feature});
|
|
122
|
+
function loadCorrection({specs,stage,feature,pendingSplit=null}){
|
|
123
|
+
const review=inspectPrdFindings({specs,stage,feature,pendingSplit});
|
|
116
124
|
need(review.verdict!=='blocked'&&['resume_disposition','completed'].includes(review.gate.outcome),'prd_correction_not_ready');
|
|
117
125
|
const archiveName=`prd-${feature.replace(/^\d+\./,'')}-${stage}-correction.md`;
|
|
118
126
|
const bytes=readCorrectionFile(specs,`.reviews/${archiveName}`);
|
|
119
127
|
const match=new TextDecoder('utf-8',{fatal:true,ignoreBOM:true}).decode(bytes).match(/\n```json\n([^\n]+)\n```\n$/);
|
|
120
128
|
need(match,'prd_correction_archive_invalid');const value=json(JSON.parse(match[1]),256*1024);
|
|
121
|
-
shape(value,['version','packageDigest','stage','feature','before','proposal']);
|
|
129
|
+
shape(value,['version','packageDigest','stage','feature','before','proposal',...(Object.hasOwn(value,'narrowReview')?['narrowReview']:[])]);
|
|
130
|
+
need(!Object.hasOwn(value,'narrowReview')||(value.narrowReview===1&&stage==='design'),'prd_correction_archive_invalid');
|
|
122
131
|
need(value.version===1&&value.packageDigest===review.packageDigest&&value.stage===stage&&value.feature===feature
|
|
123
132
|
&&archiveBytes(value).equals(bytes)&&Array.isArray(value.before),'prd_correction_archive_invalid');
|
|
124
133
|
const originals=new Map(review.reviewedArtifacts.map(item=>[item.path,item.sha256])),seen=new Set();
|
|
@@ -140,7 +149,7 @@ function loadCorrection({specs,stage,feature}){
|
|
|
140
149
|
return {...item,sha256:currentHash};
|
|
141
150
|
});
|
|
142
151
|
return {specs,stage,feature,review,archiveName,archiveHash:sha(bytes),evidenceHash:sha(readCmInitSource(specs,review.evidence)),
|
|
143
|
-
before,proposal:value.proposal,after,plan,states};
|
|
152
|
+
before,proposal:value.proposal,after,plan,states,narrowEnabled:value.narrowReview===1};
|
|
144
153
|
}
|
|
145
154
|
|
|
146
155
|
export function inspectPrdCorrectionRecovery(input){
|
|
@@ -167,3 +176,147 @@ export function resumePrdCorrection(input,signal){
|
|
|
167
176
|
if(value.states.some(item=>item.status==='needs_correction'))need(value.review.gate.outcome==='resume_disposition','prd_correction_not_ready');
|
|
168
177
|
return writeCorrection({...value,current},signal);
|
|
169
178
|
}
|
|
179
|
+
|
|
180
|
+
// ---- Design-stage narrow review rounds (see narrow-review.mjs) ----
|
|
181
|
+
const designPath=feature=>`${feature}/design.md`;
|
|
182
|
+
// After the design receipt is written the design is frozen at the receipt's bytes; a later saved split correction
|
|
183
|
+
// legitimately changes the file. Callers past that point pass the receipt's design sha (and the saved split plan).
|
|
184
|
+
function narrowFound({specs,feature,pendingSplit=null,designSha256=null}){
|
|
185
|
+
need(typeof feature==='string'&&/^[1-9]\d*\.[a-z0-9]+(?:-[a-z0-9]+)*$/.test(feature),'prd_review_feature_invalid');
|
|
186
|
+
if(readCmInitSource(specs,`.reviews/prd-${feature.replace(/^\d+\./,'')}-design-correction.md`)===null)return null;
|
|
187
|
+
const value=loadCorrection({specs,stage:'design',feature,pendingSplit}),design=designPath(feature);
|
|
188
|
+
if(!value.narrowEnabled)return null; // run started before narrow rounds: unchanged legacy flow
|
|
189
|
+
const state=inspectPrdNarrowState({specs,feature,packageDigest:value.review.packageDigest,
|
|
190
|
+
base:{designSha256:value.after.find(item=>item.path===design).sha256,archiveSha256:value.archiveHash,
|
|
191
|
+
originalDesignSha256:value.review.reviewedArtifacts.find(item=>item.path===design).sha256,
|
|
192
|
+
requirementsSha256:value.review.reviewedArtifacts.find(item=>item.path===`${feature}/requirements.md`).sha256,
|
|
193
|
+
authorContextId:readPrdReviewAuthor({specs,stage:'design',feature})},
|
|
194
|
+
currentDesignSha256:designSha256??sha(readCorrectionFile(specs,design))});
|
|
195
|
+
return {value,state};
|
|
196
|
+
}
|
|
197
|
+
// True only when the original correction is fully saved (or a narrow round already exists),
|
|
198
|
+
// so an unfinished original write still goes through the original recovery.
|
|
199
|
+
export function prdNarrowCorrectionFlow({specs,stage,feature}){
|
|
200
|
+
if(stage!=='design')return false;
|
|
201
|
+
if(readCmInitSource(specs,`.reviews/${narrowNames(feature).review(1)}`)!==null)return true;
|
|
202
|
+
const value=loadCorrection({specs,stage,feature});
|
|
203
|
+
return value.narrowEnabled&&value.states.every(item=>item.status==='matches_correction');
|
|
204
|
+
}
|
|
205
|
+
export const prdNarrowChainStarted=({specs,feature})=>readCmInitSource(specs,`.reviews/${narrowNames(feature).review(1)}`)!==null;
|
|
206
|
+
// For drivers that pre-check a prd_correct answer: the review the answer must resolve, or null (use the original).
|
|
207
|
+
export function narrowCorrectionReview({specs,stage,feature}){
|
|
208
|
+
if(stage!=='design'||!prdNarrowChainStarted({specs,feature}))return null;
|
|
209
|
+
const found=narrowFound({specs,feature});
|
|
210
|
+
if(found===null||found.state.phase.status!=='awaiting_correction')return null;
|
|
211
|
+
return {stage,feature,findings:found.state.rounds.at(-1).review.findings,reviewedArtifacts:found.value.review.reviewedArtifacts.map(item=>
|
|
212
|
+
({path:item.path,sha256:sha(readCorrectionFile(specs,item.path))}))};
|
|
213
|
+
}
|
|
214
|
+
export function inspectPrdNarrowReview(input){
|
|
215
|
+
const found=narrowFound(input);
|
|
216
|
+
return found===null?json({status:'narrow_not_started',next:'correct_findings',completionAuthorized:false}):json(narrowPublicState(found.state));
|
|
217
|
+
}
|
|
218
|
+
export function recordPrdNarrowReviewHost({specs,feature,packageDigest,input,authorContextId,writeEnabled}){
|
|
219
|
+
assertPrdBatchActive(specs,[feature]);
|
|
220
|
+
const found=narrowFound({specs,feature});need(found,'prd_narrow_requires_original_correction');
|
|
221
|
+
need(found.value.review.gate.outcome==='resume_disposition','prd_narrow_closed');
|
|
222
|
+
need(packageDigest===found.value.review.packageDigest,'prd_narrow_package_changed');
|
|
223
|
+
recordPrdNarrowReview({specs,feature,packageDigest,state:found.state,input,authorContextId,
|
|
224
|
+
originalAuthorContextId:readPrdReviewAuthor({specs,stage:'design',feature}),writeEnabled});
|
|
225
|
+
const after=narrowFound({specs,feature}),row=after.state.rounds.at(-1);
|
|
226
|
+
const blocking=row.review.findings.filter(item=>item.severity!=='minor'||['security','dataLoss','silentFailure','violatesWrittenRule'].some(flag=>item[flag]));
|
|
227
|
+
return json({...narrowPublicState(after.state),status:'narrow_review_recorded',round:row.round,verdict:row.review.verdict,
|
|
228
|
+
reviewDigest:row.reviewDigest,degradedReview:row.review.reviewer==='self-degraded',backlog:row.review.findings.filter(item=>!blocking.includes(item)).map(item=>({id:item.id,message:item.message}))});
|
|
229
|
+
}
|
|
230
|
+
// Original disposition gate: once a narrow round exists the design must have been approved by it.
|
|
231
|
+
export function assertPrdNarrowDispositionReady({specs,feature,pendingSplit=null,designSha256=null}){
|
|
232
|
+
if(readCmInitSource(specs,`.reviews/${narrowNames(feature).review(1)}`)===null)return {used:false};
|
|
233
|
+
const found=narrowFound({specs,feature,pendingSplit,designSha256});need(found,'prd_narrow_requires_original_correction');
|
|
234
|
+
const status=found.state.phase.status;
|
|
235
|
+
need(status!=='awaiting_user_decision','prd_narrow_awaiting_user_decision');
|
|
236
|
+
need(status==='approved','prd_narrow_review_pending');
|
|
237
|
+
return {used:true,designPath:designPath(feature),originalDecisions:found.value.plan.decisions};
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
async function narrowCorrection({specs,feature,correct,attempts,signal}){
|
|
241
|
+
const found=narrowFound({specs,feature}),{state,value}=found,phase=state.phase,names=narrowNames(feature);
|
|
242
|
+
// A saved correction whose result was lost before the session commit: answer again from the archive, never regenerate.
|
|
243
|
+
const lastRow=state.rounds.at(-1);
|
|
244
|
+
const resumed=phase.status==='awaiting_review'&&lastRow?.correction&&phase.round===lastRow.round+1;
|
|
245
|
+
need(phase.status!=='awaiting_review'||resumed,'prd_narrow_review_required');
|
|
246
|
+
need(phase.status!=='approved','prd_narrow_already_approved');
|
|
247
|
+
need(phase.status!=='awaiting_user_decision','prd_narrow_awaiting_user_decision');
|
|
248
|
+
const round=resumed?lastRow.round:phase.round,archive=`.reviews/${names.correction(round)}`;
|
|
249
|
+
const designRelative=designPath(feature),packageDigest=value.review.packageDigest;
|
|
250
|
+
const current=()=>{
|
|
251
|
+
need(!signal.aborted,'cancelled');const latest=narrowFound({specs,feature});
|
|
252
|
+
need(latest.value.review.packageDigest===packageDigest&&latest.value.review.gate.outcome==='resume_disposition'
|
|
253
|
+
&&['awaiting_correction','correction_incomplete'].includes(latest.state.phase.status)&&latest.state.phase.round===round,
|
|
254
|
+
'prd_narrow_state_changed');
|
|
255
|
+
};
|
|
256
|
+
const finish=row=>{
|
|
257
|
+
const doc=row.correction.proposal.documents.find(item=>item.path===designRelative);
|
|
258
|
+
const written=[];
|
|
259
|
+
try{
|
|
260
|
+
if(row.correction.beforeSha256!==row.correction.afterSha256&&sha(readCorrectionFile(specs,designRelative))===row.correction.beforeSha256){
|
|
261
|
+
current();replacePrdDocument({specs,relative:designRelative,content:doc.content,current});written.push(designRelative);
|
|
262
|
+
}
|
|
263
|
+
const after=narrowFound({specs,feature});
|
|
264
|
+
need(after.state.phase.status==='awaiting_review'&&after.state.phase.round===round+1,'prd_narrow_state_changed');
|
|
265
|
+
return json({...narrowPublicState(after.state),status:'correction_saved',narrow:true,round,packageDigest,
|
|
266
|
+
decisions:row.correction.proposal.decisions,
|
|
267
|
+
artifacts:row.correction.before.map(item=>({path:item.path,sha256:item.path===designRelative?row.correction.afterSha256:item.sha256})),
|
|
268
|
+
archive});
|
|
269
|
+
}catch{return json({status:'correction_save_unknown',archive,round,observedWritten:written,
|
|
270
|
+
next:'rerun_correct_findings_to_resume_from_archive_without_regeneration',completionAuthorized:false});}
|
|
271
|
+
};
|
|
272
|
+
if(phase.status==='correction_incomplete'||resumed)return finish(state.rounds[round-1]);
|
|
273
|
+
const narrow=state.rounds[round-1];
|
|
274
|
+
const read=file=>readCorrectionFile(specs,file);
|
|
275
|
+
const before=value.review.reviewedArtifacts.map(item=>{
|
|
276
|
+
const bytes=read(item.path);
|
|
277
|
+
return {path:item.path,sha256:sha(bytes),content:new TextDecoder('utf-8',{fatal:true,ignoreBOM:true}).decode(bytes)};
|
|
278
|
+
});
|
|
279
|
+
// Requirements are frozen after the original review; a user edit must be undone before a narrow archive is written,
|
|
280
|
+
// otherwise the saved archive could never be read back (nothing is written or asked until then).
|
|
281
|
+
for(const item of value.review.reviewedArtifacts)
|
|
282
|
+
if(item.path!==designRelative)need(before.find(row=>row.path===item.path)?.sha256===item.sha256,'prd_correction_inputs_changed');
|
|
283
|
+
const key=digest({specs,packageDigest,round});need(!attempts.has(key),'prd_correction_already_attempted');attempts.add(key);
|
|
284
|
+
const pseudo={stage:'design',feature,findings:narrow.review.findings,
|
|
285
|
+
reviewedArtifacts:before.map(({path:file,sha256})=>({path:file,sha256}))};
|
|
286
|
+
const proposal=json(await correct(json({review:{stage:'design',feature,packageDigest,narrowRound:round,
|
|
287
|
+
maxNarrowRounds:PRD_NARROW_MAX_ROUNDS,verdict:narrow.review.verdict,findings:narrow.review.findings,
|
|
288
|
+
summary:narrow.review.summary,narrowReviewDigest:narrow.reviewDigest},documents:before,
|
|
289
|
+
instructions:'Resolve only the findings of this recorded narrow design review round. Return {decisions:[{id,status:applied|escalated,evidence:[reason],changedPaths:[path]}],documents:[{path,content}]} covering every finding id and the complete unchanged path inventory. Only design.md may change. Escalate disagreement or unprovable changes; do not invent human approval. Do not write files, change task status, call another reviewer/provider or retry. The corrected design needs another recorded narrow review (at most two rounds in total).'}),signal),256*1024);
|
|
290
|
+
need(!signal.aborted,'cancelled');
|
|
291
|
+
const {after,plan}=inspectProposal(specs,pseudo,proposal);
|
|
292
|
+
current();for(const item of before)need(sha(read(item.path))===item.sha256,'prd_correction_inputs_changed');
|
|
293
|
+
const afterDesign=after.find(item=>item.path===designRelative).sha256;
|
|
294
|
+
const record={version:1,kind:'prd_design_narrow_correction',feature,packageDigest,round,narrowReviewDigest:narrow.reviewDigest,
|
|
295
|
+
beforeSha256:state.tipDesignSha256,afterSha256:afterDesign,before,proposal:{decisions:plan.decisions,documents:proposal.documents}};
|
|
296
|
+
// Archive the answered review's per-finding decisions and original bytes before any spec write.
|
|
297
|
+
writeReviewEvidence({reviewsDir:path.join(specs,'.reviews'),name:names.correction(round),bytes:narrowCorrectionBytes(json(record,256*1024)),
|
|
298
|
+
exclusive:true,validate:file=>need((fs.lstatSync(file).mode&0o777)===0o600,'prd_correction_archive_permissions')});
|
|
299
|
+
return finish({correction:record});
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
// Receipt writers outside the host (cm-prd-review-gate record) get the same full chain validation as the host:
|
|
303
|
+
// any narrow review/correction file present means the chain must be valid, approved, and keep the original
|
|
304
|
+
// decision counts. Fails closed with fixed prd_narrow_* codes (never raw system errors or paths).
|
|
305
|
+
export function assertNarrowReceiptAllowed({specs,slug,findingCount,unresolvedCount}){
|
|
306
|
+
const invalid=()=>Object.assign(new Error('prd_narrow_record_invalid'),{code:'prd_narrow_record_invalid'});
|
|
307
|
+
if(typeof slug!=='string'||!/^[a-z0-9]+(?:-[a-z0-9]+)*$/.test(slug))return; // not a PRD feature slug: nothing narrow to check
|
|
308
|
+
const names=narrowNames(`1.${slug}`);
|
|
309
|
+
const present=[names.review(1),names.review(2),names.correction(1)].some(name=>{
|
|
310
|
+
try{fs.lstatSync(path.join(specs,'.reviews',name));return true;}catch(error){if(error.code==='ENOENT')return false;return true;}
|
|
311
|
+
});
|
|
312
|
+
if(!present)return;
|
|
313
|
+
try{
|
|
314
|
+
const dirs=fs.readdirSync(specs).filter(name=>new RegExp(`^[1-9]\\d*\\.${slug}$`).test(name));
|
|
315
|
+
need(dirs.length===1,'prd_narrow_record_invalid');
|
|
316
|
+
const found=narrowFound({specs,feature:dirs[0]});need(found,'prd_narrow_requires_original_correction');
|
|
317
|
+
need(found.state.phase.status==='approved','prd_narrow_review_pending');
|
|
318
|
+
const decisions=found.value.plan.decisions;
|
|
319
|
+
need(findingCount===decisions.length&&unresolvedCount===decisions.filter(item=>item.status==='escalated').length,
|
|
320
|
+
'prd_narrow_disposition_decisions_changed');
|
|
321
|
+
}catch(error){throw typeof error?.code==='string'&&error.code.startsWith('prd_narrow_')?error:invalid();}
|
|
322
|
+
}
|
|
@@ -11,6 +11,7 @@ import {inspectPrdDispositionPlan} from './disposition-plan.mjs';
|
|
|
11
11
|
import {checkPrdDraftMechanics,inspectPrdContextCheck} from './self-check.mjs';
|
|
12
12
|
import {prdTaskScopeOptions} from './task-scope-record.mjs';
|
|
13
13
|
import {correctionCheckStore} from './correction-check.mjs';
|
|
14
|
+
import {assertPrdNarrowDispositionReady} from './review-correction.mjs';
|
|
14
15
|
const sha=bytes=>createHash('sha256').update(bytes).digest('hex');
|
|
15
16
|
|
|
16
17
|
export function recordPrdHostDisposition(input){return disposition(input);}
|
|
@@ -21,7 +22,11 @@ function disposition({specs,stage,feature,packageDigest,decisions,artifacts,writ
|
|
|
21
22
|
const review=inspectPrdFindings({specs,stage,feature});
|
|
22
23
|
need(review.packageDigest===packageDigest,'prd_disposition_package_changed');
|
|
23
24
|
need(review.verdict!=='blocked','prd_disposition_review_blocked');
|
|
24
|
-
|
|
25
|
+
// Design only: a recorded narrow review chain must have approved the exact saved design first.
|
|
26
|
+
const narrow=stage==='design'?assertPrdNarrowDispositionReady({specs,feature}):{used:false};
|
|
27
|
+
const plan=inspectPrdDispositionPlan(review,decisions,artifacts,{alsoChanged:narrow.used?[narrow.designPath]:[]});
|
|
28
|
+
// Narrow rounds never reopen the original per-finding decisions: they must equal the archived ones.
|
|
29
|
+
if(narrow.used)need(digest(plan.decisions)===digest(narrow.originalDecisions),'prd_narrow_disposition_decisions_changed');
|
|
25
30
|
({decisions,artifacts}=plan);const {changed,unresolved}=plan;
|
|
26
31
|
for(const item of artifacts){
|
|
27
32
|
const bytes=readCmInitSource(specs,item.path);
|
|
@@ -60,6 +65,8 @@ function disposition({specs,stage,feature,packageDigest,decisions,artifacts,writ
|
|
|
60
65
|
const gate=inspectPrdReview(args);
|
|
61
66
|
need(gate.outcome==='completed'&&gate.disposition===disposition&&gate.package_sha256===packageDigest,
|
|
62
67
|
'prd_disposition_write_unknown');
|
|
68
|
+
// The receipt writer reads the files again: bytes that changed since the check above are not what was reviewed.
|
|
69
|
+
for(const item of selected){const bytes=readCmInitSource(specs,item.path);need(bytes!==null&&sha(bytes)===item.sha256,'prd_disposition_write_unknown');}
|
|
63
70
|
return json({status:'disposition_recorded',outcome:recorded.outcome,gate,decisions,artifacts,
|
|
64
71
|
unresolvedFindings:review.findings.filter(item=>decisions.some(decision=>decision.id===item.id&&(failedCheck||decision.status==='escalated'))),
|
|
65
72
|
next:failedCheck?'prepare_human_summary_with_failed_checks':unresolved?'include_unresolved_in_human_summary':stage==='design'?'continue_task_generation':'prepare_human_summary',
|
|
@@ -35,3 +35,11 @@ export function inspectPrdFindings({specs,stage,feature,pendingSplit=null,approv
|
|
|
35
35
|
result.findings.length?'resolve_original_findings':'record_no_findings_after_artifacts_saved',
|
|
36
36
|
dispatchAuthorized:false,writeAuthorized:false,completionAuthorized:false});
|
|
37
37
|
}
|
|
38
|
+
|
|
39
|
+
// Declared author of the original r1 attempt; separate so review results keep their exact shape.
|
|
40
|
+
export function readPrdReviewAuthor({specs,stage,feature}){
|
|
41
|
+
const match=feature.match(/^[1-9]\d*\.([a-z0-9]+(?:-[a-z0-9]+)*)$/);need(match,'prd_review_feature_invalid');
|
|
42
|
+
const bytes=readCmInitSource(specs,`.reviews/prd-${match[1]}-${stage}-r1.md`);need(bytes!==null,'prd_review_archive_unavailable');
|
|
43
|
+
const line=new TextDecoder('utf-8',{fatal:true}).decode(bytes).match(/\n```json\n([^\n]+)\n```\n$/);need(line,'prd_review_archive_unavailable');
|
|
44
|
+
return JSON.parse(line[1]).authorContextId;
|
|
45
|
+
}
|