@@ -78,6 +78,25 @@ function claimNamesDifferentHome(
7878 return false ;
7979}
8080
81+ /**
82+ * Map a service-manager claim backend to the `ServiceInstallState.backend`
83+ * value it corresponds to. `scheduler` (Task Scheduler) and `winsw` (native)
84+ * are the two Windows manager backends; launchd/systemd claims have no Windows
85+ * backend and can never mismatch a v2 state file.
86+ */
87+ function claimBackendToStateBackend ( backend : ServiceManagerClaim [ "backend" ] ) : "scheduler" | "native" | null {
88+ if ( backend === "scheduler" ) return "scheduler" ;
89+ if ( backend === "winsw" ) return "native" ;
90+ return null ;
91+ }
92+
93+ /** True when the recorded state backend disagrees with the manager claim. Legacy v1 means scheduler. */
94+ function claimBackendMismatchesState ( claim : ServiceManagerClaim , state : { backend ?: "scheduler" | "native" } ) : boolean {
95+ const expected = claimBackendToStateBackend ( claim . backend ) ;
96+ if ( expected === null ) return false ;
97+ return ( state . backend ?? "scheduler" ) !== expected ;
98+ }
99+
81100export interface OwnershipDeps extends ProbeDeps {
82101 /**
83102 * Which state paths to consult. Injectable because the default set includes
@@ -125,7 +144,14 @@ export function inspectNativeCodexOwnership(deps: OwnershipDeps = {}): Ownership
125144 } ;
126145 }
127146
128- const manager = inspectServiceManagerInstallation ( deps ) ;
147+ // The manager assets live under the effective OPENCODEX_HOME. Production
148+ // callers do not inject ProbeDeps.configDir, so derive it from the same
149+ // current-home snapshot used for ownership comparison rather than silently
150+ // falling back to <homedir>/.opencodex.
151+ const manager = inspectServiceManagerInstallation ( {
152+ ...deps ,
153+ configDir : deps . configDir ?? current . opencodexHome ,
154+ } ) ;
129155 if ( manager . kind === "unknown" ) {
130156 return { ownership : "unknown" , reason : manager . reason } ;
131157 }
@@ -146,6 +172,17 @@ export function inspectNativeCodexOwnership(deps: OwnershipDeps = {}): Ownership
146172 reason : `${ disagreeing . backend } is installed from ${ disagreeing . definitionPath } , which names different homes than the recorded service state` ,
147173 } ;
148174 }
175+ // A manager backend that disagrees with the recorded state (e.g. state says
176+ // native/WinSW but a scheduler task is found) is an interrupted backend
177+ // switch: it does not prove which manager owns the installation. v1 state
178+ // predates the field and is scheduler by contract.
179+ const stateBackendMismatch = valid . find ( state => manager . claims . some ( claim => claimBackendMismatchesState ( claim , state . state ) ) ) ;
180+ if ( stateBackendMismatch ) {
181+ return {
182+ ownership : "unknown" ,
183+ reason : `the service state records backend ${ stateBackendMismatch . state . backend ?? "scheduler" } but ${ manager . claims [ 0 ] ?. backend ?? "a service manager" } is installed` ,
184+ } ;
185+ }
149186 // Definition agrees. Valid state agreeing with it is ownership; no state at
150187 // all beside an installed definition is not, because the definition is the
151188 // claim and nothing here recorded making it.
@@ -162,4 +199,4 @@ export function inspectNativeCodexOwnership(deps: OwnershipDeps = {}): Ownership
162199 return valid . length === 0
163200 ? { ownership : "owned" , reason : "no service state and no service manager claim" }
164201 : { ownership : "owned" , reason : "the recorded service state names these homes" } ;
165- }
202+ }
0 commit comments