@@ -27,6 +27,11 @@ const CP1_FILE = '01-需求概述.md'
2727const CP2_FILE = '02-技术方案.md'
2828const CP3_FILE = '04-实施计划.md'
2929const REQUIREMENTS_DIR = path . join ( WORKSPACE_ROOT , '.devcodex' , 'requirements' )
30+ const BUGS_DIR = path . join ( WORKSPACE_ROOT , '.devcodex' , 'bugs' )
31+ const TASK_ROOTS = [
32+ { kind : 'requirements' , dir : REQUIREMENTS_DIR } ,
33+ { kind : 'bugs' , dir : BUGS_DIR }
34+ ]
3035const EXECUTION_MODE = { CONFIRM : 'confirm' , AUTO : 'auto' }
3136const AUTO_ALLOWED_PATH_PATTERNS = [
3237 / ^ \. d e v c o d e x \/ / ,
@@ -483,37 +488,71 @@ function readCpConfirmations(reqPath) {
483488 return confirmed
484489}
485490
486- function findIncompleteRequirement ( ) {
487- if ( ! fs . existsSync ( REQUIREMENTS_DIR ) ) return null
491+ function directoryContainsFileMatching ( dir , matcher , depth = 4 ) {
492+ if ( ! fs . existsSync ( dir ) || depth < 0 ) return false
488493 let entries
489- try { entries = fs . readdirSync ( REQUIREMENTS_DIR ) } catch { return null }
490- const dirs = entries
491- . map ( name => {
492- const fullPath = path . join ( REQUIREMENTS_DIR , name )
493- try { const s = fs . statSync ( fullPath ) ; return s . isDirectory ( ) ? { name, fullPath } : null }
494- catch { return null }
495- } )
496- . filter ( Boolean )
497-
498- // v1.9.4+ 跨需求阻断缓解:先扫描"是否存在任意已 CP3 通过的需求"
499- // 若存在 → 视为有需求已进入实施阶段 → 全局放行(避免旧未完成需求阻断当前实施)
500- // 配合 `.archived` 旁路(v1.9.3+)共同工作;archived 视为"已归档",不参与 CP3 通过统计
501- const hasAnyCp3Done = dirs . some ( d => {
502- if ( fs . existsSync ( path . join ( d . fullPath , '.archived' ) ) ) return false
503- if ( ! fs . existsSync ( path . join ( d . fullPath , CP1_FILE ) ) ) return false
504- const cp = readCpConfirmations ( d . fullPath )
505- if ( cp . CP3Exempt ) return false
506- if ( ! fs . existsSync ( path . join ( d . fullPath , CP3_FILE ) ) ) return false
507- return cp . CP3
508- } )
509- if ( hasAnyCp3Done ) return null // 已有需求进入实施 → 全局放行(跨需求场景)
494+ try { entries = fs . readdirSync ( dir , { withFileTypes : true } ) } catch { return false }
495+ for ( const entry of entries ) {
496+ const full = path . join ( dir , entry . name )
497+ if ( entry . isFile ( ) && matcher ( entry . name , full ) ) return true
498+ if ( entry . isDirectory ( ) && directoryContainsFileMatching ( full , matcher , depth - 1 ) ) return true
499+ }
500+ return false
501+ }
502+
503+ function hasTaskArtifact ( task , phase ) {
504+ const fullPath = task . fullPath
505+ if ( phase === 'CP1' ) {
506+ if ( fs . existsSync ( path . join ( fullPath , CP1_FILE ) ) ) return true
507+ if ( task . kind === 'bugs' ) {
508+ return directoryContainsFileMatching (
509+ path . join ( fullPath , 'reports' ) ,
510+ name => / ^ 0 1 - - .* C P 1 .* \. m d $ / i. test ( name )
511+ )
512+ }
513+ return false
514+ }
515+ if ( phase === 'CP2' ) {
516+ if ( fs . existsSync ( path . join ( fullPath , CP2_FILE ) ) ) return true
517+ if ( task . kind === 'bugs' ) {
518+ return directoryContainsFileMatching (
519+ path . join ( fullPath , 'reports' ) ,
520+ name => / ^ 0 2 - - .* C P 2 .* \. m d $ / i. test ( name )
521+ )
522+ }
523+ return false
524+ }
525+ if ( phase === 'CP3' ) return fs . existsSync ( path . join ( fullPath , CP3_FILE ) )
526+ return false
527+ }
528+
529+ function listTaskDirs ( ) {
530+ const out = [ ]
531+ for ( const root of TASK_ROOTS ) {
532+ if ( ! fs . existsSync ( root . dir ) ) continue
533+ let entries
534+ try { entries = fs . readdirSync ( root . dir ) } catch { continue }
535+ for ( const name of entries ) {
536+ const fullPath = path . join ( root . dir , name )
537+ try {
538+ const s = fs . statSync ( fullPath )
539+ if ( s . isDirectory ( ) ) out . push ( { kind : root . kind , name, fullPath, mtimeMs : s . mtimeMs || 0 } )
540+ } catch { }
541+ }
542+ }
543+ return out . sort ( ( a , b ) => ( b . mtimeMs || 0 ) - ( a . mtimeMs || 0 ) )
544+ }
545+
546+ function findIncompleteTask ( ) {
547+ const dirs = listTaskDirs ( )
548+ if ( ! dirs . length ) return null
510549
511550 return dirs . find ( d => {
512551 if ( fs . existsSync ( path . join ( d . fullPath , '.archived' ) ) ) return false
513- if ( ! fs . existsSync ( path . join ( d . fullPath , CP1_FILE ) ) ) return false
552+ if ( ! hasTaskArtifact ( d , 'CP1' ) ) return false
514553 const cp = readCpConfirmations ( d . fullPath )
515554 if ( cp . CP3 ) return false
516- if ( ! fs . existsSync ( path . join ( d . fullPath , CP3_FILE ) ) ) return true
555+ if ( ! hasTaskArtifact ( d , 'CP3' ) ) return true
517556 return ! cp . CP3
518557 } ) || null
519558}
@@ -556,18 +595,26 @@ function extractToolPaths(payload) {
556595 return out . map ( p => { try { return path . normalize ( p ) } catch { return p } } ) . filter ( Boolean )
557596}
558597
559- // Map a file path to its owning requirement name (.devcodex/requirements/<X>/...);
560- // returns null when the path is not under any requirement directory.
561- function getRequirementNameFromPath ( p ) {
598+ // Map a file path to its owning task scope (.devcodex/requirements/<X>/... or .devcodex/bugs/<X>/...).
599+ // returns null when the path is not under any supported task directory.
600+ function getTaskScopeFromPath ( p ) {
562601 if ( ! p ) return null
563602 let abs = p
564603 if ( ! path . isAbsolute ( abs ) ) abs = path . resolve ( WORKSPACE_ROOT , abs )
565604 const norm = path . normalize ( abs )
566- const reqsDir = path . normalize ( REQUIREMENTS_DIR )
567- if ( ! norm . toLowerCase ( ) . startsWith ( reqsDir . toLowerCase ( ) + path . sep ) ) return null
568- const rel = path . relative ( reqsDir , norm )
569- const parts = rel . split ( path . sep ) . filter ( Boolean )
570- return parts . length > 0 ? parts [ 0 ] : null
605+ for ( const root of TASK_ROOTS ) {
606+ const rootDir = path . normalize ( root . dir )
607+ if ( ! norm . toLowerCase ( ) . startsWith ( rootDir . toLowerCase ( ) + path . sep ) ) continue
608+ const rel = path . relative ( rootDir , norm )
609+ const parts = rel . split ( path . sep ) . filter ( Boolean )
610+ if ( ! parts . length ) return null
611+ return {
612+ kind : root . kind ,
613+ name : parts [ 0 ] ,
614+ fullPath : path . join ( root . dir , parts [ 0 ] )
615+ }
616+ }
617+ return null
571618}
572619
573620function toWorkspaceRelativePath ( p ) {
@@ -603,41 +650,41 @@ function checkAutoWhitelist(payload, platform, state) {
603650 }
604651}
605652
606- // Path-aware CP gate: when all tool paths belong to specific requirement dirs,
607- // only check those requirements ' CP status (avoids cross-requirement deny).
608- // Falls back to global findIncompleteRequirement () for mixed/source-code paths.
609- function findIncompleteRequirementForPaths ( payload ) {
653+ // Path-aware CP gate: when all tool paths belong to specific task dirs,
654+ // only check those tasks ' CP status (avoids cross-task deny).
655+ // Falls back to global findIncompleteTask () for mixed/source-code paths.
656+ function findIncompleteTaskForPaths ( payload ) {
610657 const paths = extractToolPaths ( payload )
611- if ( paths . length === 0 ) return findIncompleteRequirement ( )
612-
613- const reqNames = paths . map ( getRequirementNameFromPath )
614- const allInReq = reqNames . every ( n => n !== null )
615- if ( ! allInReq ) return findIncompleteRequirement ( ) // mixed or source-code → preserve original behavior
616-
617- const targetSet = new Set ( reqNames )
618- for ( const reqName of targetSet ) {
619- const fullPath = path . join ( REQUIREMENTS_DIR , reqName )
620- if ( ! fs . existsSync ( fullPath ) ) continue
621- if ( fs . existsSync ( path . join ( fullPath , '.archived' ) ) ) continue
622- if ( ! fs . existsSync ( path . join ( fullPath , CP1_FILE ) ) ) continue
623- const cp = readCpConfirmations ( fullPath )
658+ if ( paths . length === 0 ) return findIncompleteTask ( )
659+
660+ const taskScopes = paths . map ( getTaskScopeFromPath )
661+ const allInTask = taskScopes . every ( scope => scope !== null )
662+ if ( ! allInTask ) return findIncompleteTask ( ) // mixed or source-code → preserve original behavior
663+
664+ const targetMap = new Map ( )
665+ for ( const scope of taskScopes ) targetMap . set ( ` ${ scope . kind } : ${ scope . name } ` , scope )
666+ for ( const task of targetMap . values ( ) ) {
667+ if ( ! fs . existsSync ( task . fullPath ) ) continue
668+ if ( fs . existsSync ( path . join ( task . fullPath , '.archived' ) ) ) continue
669+ if ( ! hasTaskArtifact ( task , 'CP1' ) ) continue
670+ const cp = readCpConfirmations ( task . fullPath )
624671 if ( cp . CP3 ) continue
625- if ( ! fs . existsSync ( path . join ( fullPath , CP3_FILE ) ) ) return { name : reqName , fullPath }
626- if ( ! cp . CP3 ) return { name : reqName , fullPath }
672+ if ( ! hasTaskArtifact ( task , 'CP3' ) ) return task
673+ if ( ! cp . CP3 ) return task
627674 }
628- return null // all target requirements have CP3 confirmed → allow
675+ return null // all target tasks have CP3 confirmed → allow
629676}
630677
631678function checkCpGate ( payload ) {
632- const req = ( payload && extractToolPaths ( payload ) . length > 0 )
633- ? findIncompleteRequirementForPaths ( payload )
634- : findIncompleteRequirement ( )
635- if ( ! req ) return null
636- const confirmed = readCpConfirmations ( req . fullPath )
637- if ( ! fs . existsSync ( path . join ( req . fullPath , CP2_FILE ) ) || ! confirmed . CP2 ) {
638- return { phase : 'CP2' , reqName : req . name , reqPath : req . fullPath }
679+ const task = ( payload && extractToolPaths ( payload ) . length > 0 )
680+ ? findIncompleteTaskForPaths ( payload )
681+ : findIncompleteTask ( )
682+ if ( ! task ) return null
683+ const confirmed = readCpConfirmations ( task . fullPath )
684+ if ( ! hasTaskArtifact ( task , 'CP2' ) || ! confirmed . CP2 ) {
685+ return { phase : 'CP2' , reqName : task . name , reqPath : task . fullPath , kind : task . kind }
639686 }
640- return { phase : 'CP3' , reqName : req . name , reqPath : req . fullPath }
687+ return { phase : 'CP3' , reqName : task . name , reqPath : task . fullPath , kind : task . kind }
641688}
642689
643690// Source file extensions that indicate code/config being written
@@ -698,7 +745,7 @@ function isSourceCodeMutation(payload, platform) {
698745
699746function buildCpDenyOutput ( platform , eventName , gate , toolName ) {
700747 const msgs = {
701- CP2 : ` CP2 (技术方案) 未完成 — 请先输出 ${ CP2_FILE } 并在 .memory/sessions.md 记录用户确认(✅)后再编码。` ,
748+ CP2 : ' CP2 (技术方案) 未完成 — 请先输出对应的 CP2 方案产物(如 02-技术方案.md 或 CP2 报告产物), 并在 .memory/sessions.md 记录用户确认(✅)后再编码。' ,
702749 CP3 : `CP3 (实施计划) 未完成 — 请先输出 ${ CP3_FILE } 并在 .memory/sessions.md 记录用户确认(✅)后再编码。`
703750 }
704751 const msg = msgs [ gate . phase ]
0 commit comments