@@ -29,6 +29,7 @@ const STICKY_PROJECT_TTL_MS = 30 * 60 * 1000
2929const CP1_FILE = '01-需求概述.md'
3030const CP2_FILE = '02-技术方案.md'
3131const CP3_FILE = '04-实施计划.md'
32+ const CP3_RUNTIME_FILE_THRESHOLD = 5
3233const EXECUTION_MODE = { CONFIRM : 'confirm' , AUTO : 'auto' }
3334const ENFORCEMENT_MODE = ( ( ) => {
3435 const mode = String ( process . env . DEVCODEX_HOOK_ENFORCEMENT || 'safety-only' ) . trim ( ) . toLowerCase ( )
@@ -398,6 +399,8 @@ function toolBlockOutput(eventName, reason, detail) {
398399function blockOutput ( platform , eventName , reason , detail ) {
399400 const event = normalizeHookEvent ( eventName )
400401 const message = detail || reason
402+ // ConfirmationRequest is a host-agnostic semantic contract; runtime outputs
403+ // only need to match each host's actual hook schema, not emit a literal object.
401404 if ( platform === 'codex' ) {
402405 if ( [ 'pretooluse' , 'permissionrequest' ] . includes ( event ) ) {
403406 return toolBlockOutput ( eventName , reason , detail )
@@ -979,6 +982,7 @@ function buildDefaultState(mode) {
979982 precheckStatus : 'unverified' , precheck : false , compliance : false , artifactPaths : false
980983 } ,
981984 stickyProject : { project : CONTEXT_PROJECT || '' , source : CONTEXT_PROJECT ? 'context' : '' , sessionKey : '' , updatedAt : '' , updatedAtMs : 0 } ,
985+ cp3Runtime : { } ,
982986 mutated : false , reportTouched : false , memoryTouched : false ,
983987 dangerousApprovals : { } ,
984988 lastEvent : '' , lastReason : ''
@@ -1007,6 +1011,7 @@ function loadState(modeHint) {
10071011 bootstrap : { ...current . bootstrap , ...( saved . bootstrap || { } ) } ,
10081012 visible : { ...current . visible , ...( saved . visible || { } ) } ,
10091013 stickyProject : { ...current . stickyProject , ...( saved . stickyProject || { } ) , ...( metaState ?. stickyProject || { } ) } ,
1014+ cp3Runtime : { ...current . cp3Runtime , ...( saved . cp3Runtime || { } ) } ,
10101015 dangerousApprovals : { ...current . dangerousApprovals , ...( saved . dangerousApprovals || { } ) }
10111016 }
10121017}
@@ -1037,6 +1042,7 @@ function resetState(mode, previousState) {
10371042 state . activeProjectSource = previousState ?. activeProjectSource || ( CONTEXT_PROJECT ? 'context' : '' )
10381043 state . lastMultiProjectWarningKey = previousState ?. lastMultiProjectWarningKey || ''
10391044 state . stickyProject = { ...state . stickyProject , ...( previousState ?. stickyProject || { } ) }
1045+ state . cp3Runtime = { ...( previousState ?. cp3Runtime || { } ) }
10401046 state . dangerousApprovals = { ...( previousState ?. dangerousApprovals || { } ) }
10411047 saveState ( state )
10421048 return state
@@ -1275,12 +1281,44 @@ function findIncompleteTask(state) {
12751281 if ( fs . existsSync ( path . join ( d . fullPath , '.archived' ) ) ) return false
12761282 if ( ! hasTaskArtifact ( d , 'CP1' ) ) return false
12771283 const cp = readCpConfirmations ( d . fullPath )
1278- if ( cp . CP3 ) return false
1284+ if ( cp . CP3 ) {
1285+ clearTaskCp3RuntimeRecord ( state , d )
1286+ return false
1287+ }
12791288 if ( ! hasTaskArtifact ( d , 'CP3' ) ) return true
12801289 return ! cp . CP3
12811290 } ) || null
12821291}
12831292
1293+ function getTaskRuntimeKey ( task ) {
1294+ return `${ task . kind } :${ path . normalize ( task . fullPath ) . toLowerCase ( ) } `
1295+ }
1296+
1297+ function getTaskCp3RuntimeRecord ( state , task ) {
1298+ if ( ! isPlainObject ( state . cp3Runtime ) ) state . cp3Runtime = { }
1299+ const key = getTaskRuntimeKey ( task )
1300+ if ( ! isPlainObject ( state . cp3Runtime [ key ] ) ) {
1301+ state . cp3Runtime [ key ] = {
1302+ kind : task . kind ,
1303+ name : task . name ,
1304+ reqPath : task . fullPath ,
1305+ trackedFiles : [ ] ,
1306+ triggered : false ,
1307+ triggerType : '' ,
1308+ triggerReason : '' ,
1309+ triggerCount : 0 ,
1310+ triggeredAt : '' ,
1311+ updatedAt : ''
1312+ }
1313+ }
1314+ return state . cp3Runtime [ key ]
1315+ }
1316+
1317+ function clearTaskCp3RuntimeRecord ( state , task ) {
1318+ if ( ! isPlainObject ( state . cp3Runtime ) ) return
1319+ delete state . cp3Runtime [ getTaskRuntimeKey ( task ) ]
1320+ }
1321+
12841322// Extract file paths from a PreToolUse payload (Claude Code + Copilot field names)
12851323function extractToolPaths ( payload ) {
12861324 if ( ! payload ) return [ ]
@@ -1319,6 +1357,72 @@ function extractToolPaths(payload) {
13191357 return out . map ( p => { try { return path . normalize ( p ) } catch { return p } } ) . filter ( Boolean )
13201358}
13211359
1360+ function extractSourceMutationTargets ( payload , state ) {
1361+ return [ ...new Set ( extractToolPaths ( payload ) ) ]
1362+ . map ( resolveRelativeToContext )
1363+ . filter ( target => SOURCE_EXT_RE . test ( target ) && ! isDevCodexManagedPath ( target , state ) )
1364+ }
1365+
1366+ function isHighRiskCp3RuntimeTarget ( target ) {
1367+ const rel = toWorkspaceRelativePath ( target )
1368+ return [
1369+ / ( ^ | \/ ) p a c k a g e \. j s o n $ / i,
1370+ / ( ^ | \/ ) ( p a c k a g e - l o c k \. j s o n | p n p m - l o c k \. y a m l | y a r n \. l o c k ) $ / i,
1371+ / ( ^ | \/ ) \. e n v (?: \. [ ^ \/ ] + ) ? $ / i,
1372+ / ( ^ | \/ ) ( D o c k e r f i l e | d o c k e r - c o m p o s e (?: \. [ ^ \/ ] + ) ? \. y a ? m l ) $ / i,
1373+ / ( ^ | \/ ) \. g i t h u b \/ w o r k f l o w s \/ .+ \. y a ? m l $ / i,
1374+ / ( ^ | \/ ) (?: p r i s m a \/ s c h e m a \. p r i s m a | s c h e m a \. p r i s m a ) $ / i,
1375+ / ( ^ | \/ ) (?: m i g r a t i o n s ? | d b \/ m i g r a t i o n s ? ) \/ / i
1376+ ] . some ( re => re . test ( rel ) )
1377+ }
1378+
1379+ function assessBugCp3RuntimeEscalation ( task , payload , state ) {
1380+ const targets = extractSourceMutationTargets ( payload , state )
1381+ if ( ! targets . length ) return null
1382+
1383+ const record = getTaskCp3RuntimeRecord ( state , task )
1384+ const tracked = new Set ( Array . isArray ( record . trackedFiles ) ? record . trackedFiles : [ ] )
1385+ const nextTargets = targets . map ( toWorkspaceRelativePath ) . filter ( Boolean )
1386+ for ( const rel of nextTargets ) tracked . add ( rel )
1387+
1388+ record . trackedFiles = [ ...tracked ] . sort ( )
1389+ record . updatedAt = new Date ( ) . toISOString ( )
1390+
1391+ const highRiskTarget = targets . find ( isHighRiskCp3RuntimeTarget )
1392+ if ( highRiskTarget ) {
1393+ const rel = toWorkspaceRelativePath ( highRiskTarget )
1394+ record . triggered = true
1395+ record . triggerType = 'high-risk'
1396+ record . triggerReason = `执行中新增高风险文件 ${ rel } `
1397+ record . triggerCount = tracked . size
1398+ record . triggeredAt = record . updatedAt
1399+ return {
1400+ type : 'high-risk' ,
1401+ reason : record . triggerReason ,
1402+ count : tracked . size ,
1403+ threshold : CP3_RUNTIME_FILE_THRESHOLD ,
1404+ trackedFiles : record . trackedFiles
1405+ }
1406+ }
1407+
1408+ if ( tracked . size >= CP3_RUNTIME_FILE_THRESHOLD ) {
1409+ record . triggered = true
1410+ record . triggerType = 'file-threshold'
1411+ record . triggerReason = `执行中已触达 ${ tracked . size } 个源码/配置文件(阈值 ${ CP3_RUNTIME_FILE_THRESHOLD } )`
1412+ record . triggerCount = tracked . size
1413+ record . triggeredAt = record . updatedAt
1414+ return {
1415+ type : 'file-threshold' ,
1416+ reason : record . triggerReason ,
1417+ count : tracked . size ,
1418+ threshold : CP3_RUNTIME_FILE_THRESHOLD ,
1419+ trackedFiles : record . trackedFiles
1420+ }
1421+ }
1422+
1423+ return null
1424+ }
1425+
13221426// Map a file path to its owning task scope (.devcodex/requirements/<X>/... or .devcodex/bugs/<X>/...).
13231427// returns null when the path is not under any supported task directory.
13241428function getTaskScopeFromPath ( p , state ) {
@@ -1390,7 +1494,10 @@ function findIncompleteTaskForPaths(payload, state) {
13901494 if ( fs . existsSync ( path . join ( task . fullPath , '.archived' ) ) ) continue
13911495 if ( ! hasTaskArtifact ( task , 'CP1' ) ) continue
13921496 const cp = readCpConfirmations ( task . fullPath )
1393- if ( cp . CP3 ) continue
1497+ if ( cp . CP3 ) {
1498+ clearTaskCp3RuntimeRecord ( state , task )
1499+ continue
1500+ }
13941501 if ( ! hasTaskArtifact ( task , 'CP3' ) ) return task
13951502 if ( ! cp . CP3 ) return task
13961503 }
@@ -1406,6 +1513,20 @@ function checkCpGate(payload, state) {
14061513 if ( ! hasTaskArtifact ( task , 'CP2' ) || ! confirmed . CP2 ) {
14071514 return { phase : 'CP2' , reqName : task . name , reqPath : task . fullPath , kind : task . kind }
14081515 }
1516+ if ( task . kind === 'bugs' && ! confirmed . CP3 ) {
1517+ const runtimeTrigger = assessBugCp3RuntimeEscalation ( task , payload , state )
1518+ if ( ! runtimeTrigger ) return null
1519+ return {
1520+ phase : 'CP3' ,
1521+ reqName : task . name ,
1522+ reqPath : task . fullPath ,
1523+ kind : task . kind ,
1524+ code : runtimeTrigger . type === 'high-risk'
1525+ ? 'cp-gate-CP3-runtime-risk'
1526+ : 'cp-gate-CP3-runtime-threshold' ,
1527+ runtimeTrigger
1528+ }
1529+ }
14091530 return { phase : 'CP3' , reqName : task . name , reqPath : task . fullPath , kind : task . kind }
14101531}
14111532
@@ -1505,23 +1626,28 @@ function buildCpDenyOutput(state, platform, eventName, gate, toolName) {
15051626 CP2 : 'CP2 (技术方案) 未完成 — 请先输出对应的 CP2 方案产物(如 02-技术方案.md 或 CP2 报告产物),并在 .memory/sessions.md 记录用户确认(✅)后再编码。' ,
15061627 CP3 : `CP3 (实施计划) 未完成 — 请先输出 ${ CP3_FILE } 并在 .memory/sessions.md 记录用户确认(✅)后再编码。`
15071628 }
1508- const msg = msgs [ gate . phase ]
1629+ const runtimeDetail = gate . runtimeTrigger
1630+ ? `${ gate . runtimeTrigger . reason } ,请先回到 CP3 更新实施计划并获得确认后再继续。`
1631+ : ''
1632+ const msg = runtimeDetail || msgs [ gate . phase ]
15091633 return buildInterceptionOutput (
1510- state , platform , eventName , INTERCEPTION_ACTION . REQUIRE_COMPLETION , `cp-gate-${ gate . phase } ` ,
1634+ state , platform , eventName , INTERCEPTION_ACTION . REQUIRE_COMPLETION , gate . code || `cp-gate-${ gate . phase } ` ,
15111635 `CP gate: ${ gate . phase } not confirmed for "${ gate . reqName } " — ${ toolName } denied` ,
15121636 msg ,
15131637 `Complete and confirm ${ gate . phase } , then retry the source mutation.`
15141638 )
15151639}
15161640
15171641function buildCpWarningOutput ( state , platform , eventName , gate , toolName ) {
1518- const detail = gate . phase === 'CP2'
1642+ const detail = gate . runtimeTrigger
1643+ ? `${ gate . runtimeTrigger . reason } ,请先回到 CP3 更新实施计划并获得确认。 Tool allowed in safety-only mode.`
1644+ : gate . phase === 'CP2'
15191645 ? 'CP2 (技术方案) 未完成;请尽快补齐方案产物与用户确认记录。'
15201646 : `CP3 (实施计划) 未完成;请尽快补齐 ${ CP3_FILE } 与用户确认记录。`
15211647 return buildInterceptionOutput (
1522- state , platform , eventName , INTERCEPTION_ACTION . REQUIRE_COMPLETION , `cp-gate-${ gate . phase } ` ,
1648+ state , platform , eventName , INTERCEPTION_ACTION . REQUIRE_COMPLETION , gate . code || `cp-gate-${ gate . phase } ` ,
15231649 `CP gate warning: ${ gate . phase } not confirmed for "${ gate . reqName } " before ${ toolName } ` ,
1524- `${ detail } Tool allowed in safety-only mode.` ,
1650+ gate . runtimeTrigger ? detail : `${ detail } Tool allowed in safety-only mode.` ,
15251651 `Complete and confirm ${ gate . phase } , then retry the source mutation.`
15261652 )
15271653}
0 commit comments