@@ -23,6 +23,16 @@ const ERROR_CODES = Object.freeze({
2323 * yes-implement / Batch mutation must not skip CP3 materialization (ESC-01 / P0-1 process gate).
2424 */
2525 IMPLEMENT_START_WITHOUT_PROCESS : 'implement-start-without-process' ,
26+ /**
27+ * Control-plane mutation with no bound requirement/bug task root.
28+ * Fail-closed so "do the feature" cannot mutate scripts/hooks without a task package
29+ * (promise-to-follow-process then skip was an unbound-task loophole).
30+ */
31+ IMPLEMENT_START_WITHOUT_TASK_BINDING : 'implement-start-without-task-binding' ,
32+ /**
33+ * Control-plane mutation without CP1/CP2 design artifacts (01 需求确认 and/or 02 技术方案方案).
34+ */
35+ IMPLEMENT_START_WITHOUT_DESIGN : 'implement-start-without-design' ,
2636 /** Completion claim without discoverable stage report path */
2737 STAGE_REPORT_MISSING : 'stage-report-missing' ,
2838 /** Over-claim progress without validation evidence rows */
@@ -281,6 +291,43 @@ function probeProcessTriad (taskRoot, fsys = null) {
281291 * }} input
282292 * @returns {{ ok: boolean, code: string|null, missing: string[], triad: object|null } }
283293 */
294+ function listDirNames ( root , fsImpl ) {
295+ try {
296+ if ( ! fsImpl || typeof fsImpl . readdirSync !== 'function' ) return [ ]
297+ return fsImpl . readdirSync ( root )
298+ } catch {
299+ return [ ]
300+ }
301+ }
302+
303+ function probeDesignArtifacts ( taskRoot , fsImpl ) {
304+ const fsx = fsImpl || { existsSync : ( ) => false , readdirSync : ( ) => [ ] }
305+ const names = listDirNames ( taskRoot , fsx )
306+ const has01 = names . some ( n =>
307+ / ^ 0 1 - .* 需 求 确 认 / i. test ( n ) ||
308+ / ^ 0 1 - .* 问 题 确 认 / i. test ( n ) ||
309+ / ^ 0 1 - .* 产 品 需 求 / i. test ( n ) ||
310+ n === '01-需求确认.md' ||
311+ n === '01-问题确认.md'
312+ ) || ( fsx . existsSync && (
313+ fsx . existsSync ( path . join ( taskRoot , '01-需求确认.md' ) ) ||
314+ fsx . existsSync ( path . join ( taskRoot , '01-问题确认.md' ) )
315+ ) )
316+ const has02 = names . some ( n =>
317+ / ^ 0 2 - .* 技 术 方 案 / i. test ( n ) ||
318+ n === '02-技术方案.md'
319+ ) || ( fsx . existsSync && fsx . existsSync ( path . join ( taskRoot , '02-技术方案.md' ) ) )
320+ const has00 = names . some ( n =>
321+ / ^ 0 0 - .* 概 况 / i. test ( n ) ||
322+ n === '00-需求概况.md' ||
323+ n === '00-问题概况.md'
324+ ) || ( fsx . existsSync && (
325+ fsx . existsSync ( path . join ( taskRoot , '00-需求概况.md' ) ) ||
326+ fsx . existsSync ( path . join ( taskRoot , '00-问题概况.md' ) )
327+ ) )
328+ return { has00, has01, has02 }
329+ }
330+
284331function classifyImplementStartGate ( input = { } ) {
285332 if ( input . skip === true ) {
286333 return { ok : true , code : null , missing : [ ] , triad : null }
@@ -293,10 +340,18 @@ function classifyImplementStartGate (input = {}) {
293340 }
294341 const root = input . taskRoot
295342 if ( ! root ) {
296- // No bound task: cannot enforce triad; caller may still require task binding elsewhere
297- return { ok : true , code : null , missing : [ ] , triad : null , unbound : true }
343+ // Fail-closed: unbound control-plane mutation was the loophole for
344+ // "will follow process" then edit scripts/hooks without any requirement dir.
345+ return {
346+ ok : false ,
347+ code : ERROR_CODES . IMPLEMENT_START_WITHOUT_TASK_BINDING ,
348+ missing : [ 'bound-requirement-or-bug-task' ] ,
349+ triad : null ,
350+ unbound : true
351+ }
298352 }
299- const triad = probeProcessTriad ( root , input . fs || null )
353+ const fsImpl = input . fs || null
354+ const triad = probeProcessTriad ( root , fsImpl )
300355 if ( triad . missing . length ) {
301356 return {
302357 ok : false ,
@@ -305,6 +360,24 @@ function classifyImplementStartGate (input = {}) {
305360 triad
306361 }
307362 }
363+ // Control-plane / implement-start also need design artifacts (概况/确认/方案 at least 01+02 or 02)
364+ const requireDesign = input . requireDesignArtifacts !== false
365+ if ( requireDesign && input . controlPlaneMutation === true ) {
366+ const design = probeDesignArtifacts ( root , fsImpl )
367+ const missingDesign = [ ]
368+ if ( ! design . has02 ) missingDesign . push ( '02-技术方案.md' )
369+ if ( ! design . has01 && ! design . has00 ) missingDesign . push ( '00-概况-or-01-确认' )
370+ // Prefer 02 always; also require 00 or 01 so pure-02 without product intake is not enough alone for full CP story
371+ if ( ! design . has02 || ( ! design . has01 && ! design . has00 ) ) {
372+ return {
373+ ok : false ,
374+ code : ERROR_CODES . IMPLEMENT_START_WITHOUT_DESIGN ,
375+ missing : missingDesign . length ? missingDesign : [ '01-需求确认.md' , '02-技术方案.md' ] ,
376+ triad,
377+ design
378+ }
379+ }
380+ }
308381 return { ok : true , code : null , missing : [ ] , triad }
309382}
310383
0 commit comments