@@ -19,19 +19,12 @@ const STATE_DIR = path.join(WORKSPACE_ROOT, '.devcodex', '.memory', 'hooks')
1919const STATE_FILE = path . join ( STATE_DIR , 'lifecycle-state.json' )
2020const FINAL_PAYLOAD_FLAG = path . join ( STATE_DIR , 'capture-final-payload.flag' )
2121const FINAL_PAYLOAD_LOG = path . join ( STATE_DIR , 'captured-final-payloads.ndjson' )
22- const PROFILE_CONFIG_FILE = path . join ( WORKSPACE_ROOT , '.devcodex' , 'profile' , 'config.json' )
2322const PAYLOAD_PREVIEW_LIMIT = 160
2423
2524// ─── CP Gate constants ────────────────────────────────────────────────────────
2625const CP1_FILE = '01-需求概述.md'
2726const CP2_FILE = '02-技术方案.md'
2827const CP3_FILE = '04-实施计划.md'
29- const 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- ]
3528const EXECUTION_MODE = { CONFIRM : 'confirm' , AUTO : 'auto' }
3629const AUTO_ALLOWED_PATH_PATTERNS = [
3730 / ^ \. d e v c o d e x \/ / ,
@@ -82,14 +75,41 @@ function readJsonFile(p) {
8275 try { return JSON . parse ( raw ) } catch { return null }
8376}
8477
85- function readProfileMode ( ) {
86- const cfg = readJsonFile ( PROFILE_CONFIG_FILE )
78+ function getWorkspaceProfileConfigPath ( ) {
79+ return path . join ( WORKSPACE_ROOT , '.devcodex' , 'profile' , 'config.json' )
80+ }
81+
82+ function getProjectRoot ( projectName ) {
83+ return projectName ? path . join ( WORKSPACE_ROOT , projectName ) : WORKSPACE_ROOT
84+ }
85+
86+ function getActiveProjectRoot ( state ) {
87+ return getProjectRoot ( state ?. activeProject || '' )
88+ }
89+
90+ function getProfileSearchRoots ( state , explicitProject ) {
91+ const roots = [ ]
92+ const projectRoot = getProjectRoot ( explicitProject || state ?. activeProject || '' )
93+ roots . push ( projectRoot )
94+ if ( projectRoot !== WORKSPACE_ROOT ) roots . push ( WORKSPACE_ROOT )
95+ return [ ...new Set ( roots ) ]
96+ }
97+
98+ function readResolvedProfileConfig ( state , explicitProject ) {
99+ for ( const root of getProfileSearchRoots ( state , explicitProject ) ) {
100+ const cfg = readJsonFile ( path . join ( root , '.devcodex' , 'profile' , 'config.json' ) )
101+ if ( cfg ) return cfg
102+ }
103+ return null
104+ }
105+
106+ function readProfileMode ( state , explicitProject ) {
107+ const cfg = readResolvedProfileConfig ( state , explicitProject )
87108 return String ( cfg ?. mode ?? 'prod' ) . trim ( ) . toLowerCase ( ) === 'dev' ? 'dev' : 'prod'
88109}
89110
90- function readProjectProfileConfig ( state ) {
91- const projectRoot = getActiveProjectRoot ( state )
92- return readJsonFile ( path . join ( projectRoot , '.devcodex' , 'profile' , 'config.json' ) ) || null
111+ function readProjectProfileConfig ( state , explicitProject ) {
112+ return readResolvedProfileConfig ( state , explicitProject )
93113}
94114
95115function listMemoryAgents ( state ) {
@@ -322,11 +342,6 @@ function touchesPath(payload, ...needles) {
322342 return strings . some ( s => needles . some ( n => s . includes ( normalizeText ( n ) ) ) )
323343}
324344
325- function getActiveProjectRoot ( state ) {
326- if ( state ?. activeProject ) return path . join ( WORKSPACE_ROOT , state . activeProject )
327- return WORKSPACE_ROOT
328- }
329-
330345function buildProjectScopedNeedles ( projectRoot , segments ) {
331346 const absolute = path . join ( projectRoot , ...segments )
332347 const relativeRoot = path . relative ( WORKSPACE_ROOT , projectRoot )
@@ -378,9 +393,9 @@ function buildDefaultState(mode) {
378393}
379394
380395function loadState ( modeHint ) {
381- const mode = modeHint || readProfileMode ( )
382- const current = buildDefaultState ( mode )
383396 const saved = readJsonFile ( STATE_FILE )
397+ const mode = modeHint || readProfileMode ( saved || null )
398+ const current = buildDefaultState ( mode )
384399 if ( ! saved || typeof saved !== 'object' ) return current
385400 return {
386401 ...current , ...saved , mode,
@@ -526,9 +541,10 @@ function hasTaskArtifact(task, phase) {
526541 return false
527542}
528543
529- function listTaskDirs ( ) {
544+ function listTaskDirs ( state ) {
545+ const taskRoots = getTaskRoots ( state )
530546 const out = [ ]
531- for ( const root of TASK_ROOTS ) {
547+ for ( const root of taskRoots ) {
532548 if ( ! fs . existsSync ( root . dir ) ) continue
533549 let entries
534550 try { entries = fs . readdirSync ( root . dir ) } catch { continue }
@@ -543,8 +559,16 @@ function listTaskDirs() {
543559 return out . sort ( ( a , b ) => ( b . mtimeMs || 0 ) - ( a . mtimeMs || 0 ) )
544560}
545561
546- function findIncompleteTask ( ) {
547- const dirs = listTaskDirs ( )
562+ function getTaskRoots ( state ) {
563+ const projectRoot = getActiveProjectRoot ( state )
564+ return [
565+ { kind : 'requirements' , dir : path . join ( projectRoot , '.devcodex' , 'requirements' ) } ,
566+ { kind : 'bugs' , dir : path . join ( projectRoot , '.devcodex' , 'bugs' ) }
567+ ]
568+ }
569+
570+ function findIncompleteTask ( state ) {
571+ const dirs = listTaskDirs ( state )
548572 if ( ! dirs . length ) return null
549573
550574 return dirs . find ( d => {
@@ -597,12 +621,12 @@ function extractToolPaths(payload) {
597621
598622// Map a file path to its owning task scope (.devcodex/requirements/<X>/... or .devcodex/bugs/<X>/...).
599623// returns null when the path is not under any supported task directory.
600- function getTaskScopeFromPath ( p ) {
624+ function getTaskScopeFromPath ( p , state ) {
601625 if ( ! p ) return null
602626 let abs = p
603627 if ( ! path . isAbsolute ( abs ) ) abs = path . resolve ( WORKSPACE_ROOT , abs )
604628 const norm = path . normalize ( abs )
605- for ( const root of TASK_ROOTS ) {
629+ for ( const root of getTaskRoots ( state ) ) {
606630 const rootDir = path . normalize ( root . dir )
607631 if ( ! norm . toLowerCase ( ) . startsWith ( rootDir . toLowerCase ( ) + path . sep ) ) continue
608632 const rel = path . relative ( rootDir , norm )
@@ -653,13 +677,13 @@ function checkAutoWhitelist(payload, platform, state) {
653677// Path-aware CP gate: when all tool paths belong to specific task dirs,
654678// only check those tasks' CP status (avoids cross-task deny).
655679// Falls back to global findIncompleteTask() for mixed/source-code paths.
656- function findIncompleteTaskForPaths ( payload ) {
680+ function findIncompleteTaskForPaths ( payload , state ) {
657681 const paths = extractToolPaths ( payload )
658- if ( paths . length === 0 ) return findIncompleteTask ( )
682+ if ( paths . length === 0 ) return findIncompleteTask ( state )
659683
660- const taskScopes = paths . map ( getTaskScopeFromPath )
684+ const taskScopes = paths . map ( p => getTaskScopeFromPath ( p , state ) )
661685 const allInTask = taskScopes . every ( scope => scope !== null )
662- if ( ! allInTask ) return findIncompleteTask ( ) // mixed or source-code → preserve original behavior
686+ if ( ! allInTask ) return findIncompleteTask ( state ) // mixed or source-code → preserve original behavior
663687
664688 const targetMap = new Map ( )
665689 for ( const scope of taskScopes ) targetMap . set ( `${ scope . kind } :${ scope . name } ` , scope )
@@ -675,10 +699,10 @@ function findIncompleteTaskForPaths(payload) {
675699 return null // all target tasks have CP3 confirmed → allow
676700}
677701
678- function checkCpGate ( payload ) {
702+ function checkCpGate ( payload , state ) {
679703 const task = ( payload && extractToolPaths ( payload ) . length > 0 )
680- ? findIncompleteTaskForPaths ( payload )
681- : findIncompleteTask ( )
704+ ? findIncompleteTaskForPaths ( payload , state )
705+ : findIncompleteTask ( state )
682706 if ( ! task ) return null
683707 const confirmed = readCpConfirmations ( task . fullPath )
684708 if ( ! hasTaskArtifact ( task , 'CP2' ) || ! confirmed . CP2 ) {
@@ -886,16 +910,19 @@ async function main() {
886910
887911 const eventName = getEventName ( payload )
888912 const platform = detectPlatform ( payload )
889- const mode = readProfileMode ( )
890- let state = loadState ( mode )
913+ const prompt = eventName === 'UserPromptSubmit' ? extractUserPrompt ( payload ) : ''
914+ const explicitProject = eventName === 'UserPromptSubmit' ? detectProjectFromPrompt ( prompt ) : ''
915+ const modeHint = eventName === 'UserPromptSubmit' && explicitProject
916+ ? readProfileMode ( null , explicitProject )
917+ : undefined
918+ let state = loadState ( modeHint )
919+ const mode = state . mode
891920
892921 updateVisibleReplyState ( state , payload , eventName )
893922 state . lastEvent = eventName || state . lastEvent
894923
895924 // ── UserPromptSubmit ───────────────────────────────────────────────────────
896925 if ( eventName === 'UserPromptSubmit' ) {
897- const prompt = extractUserPrompt ( payload )
898- const explicitProject = detectProjectFromPrompt ( prompt )
899926 state = resetState ( mode , state )
900927 state . executionMode = detectExecutionMode ( payload )
901928 if ( hasMultiProjectExemption ( prompt ) ) {
@@ -906,7 +933,7 @@ async function main() {
906933 // Multi-project workspace guard (v1.9.8+):
907934 // when no workspace-root profile exists and ≥2 sibling projects detected,
908935 // require the user to specify the target project explicitly.
909- const hasWorkspaceProfile = fs . existsSync ( PROFILE_CONFIG_FILE )
936+ const hasWorkspaceProfile = fs . existsSync ( getWorkspaceProfileConfigPath ( ) )
910937 if ( ! hasWorkspaceProfile && isMultiProjectWorkspace ( ) ) {
911938 if ( ! hasMultiProjectExemption ( prompt ) && ! state . activeProject ) {
912939 state . lastReason = 'multi-project-workspace-block'
@@ -977,7 +1004,7 @@ async function main() {
9771004
9781005 // 3. CP gate — block source code mutations until checkpoints confirmed
9791006 // payload-aware: when tool paths target specific requirement dirs, only that requirement's CP is checked
980- const gate = checkCpGate ( payload )
1007+ const gate = checkCpGate ( payload , state )
9811008 if ( gate && isSourceCodeMutation ( payload , platform ) ) {
9821009 state . lastReason = `cp-gate-${ gate . phase } `
9831010 saveState ( state )
0 commit comments