11import { createHash } from "node:crypto" ;
22import { lstat , readFile , realpath } from "node:fs/promises" ;
3- import { dirname , relative , resolve } from "node:path" ;
3+ import { dirname , join , relative , resolve } from "node:path" ;
44import {
55 inspectInstallation as inspectBaseInstallation ,
66 reasoningWarnings ,
@@ -131,11 +131,39 @@ async function semanticCrossScope(options) {
131131 return entries ;
132132}
133133
134+ function flagPresent ( argv , name ) {
135+ return argv . some ( ( token ) => token === name || token . startsWith ( `${ name } =` ) ) ;
136+ }
137+
134138function isStatus ( argv ) {
135139 if ( argv [ 0 ] === "v2" ) return [ "status" , "doctor" ] . includes ( argv [ 1 ] ) ;
136140 return [ "status" , "doctor" ] . includes ( argv [ 0 ] ) ;
137141}
138142
143+ function isOperational ( argv ) {
144+ if ( argv [ 0 ] === "v2" ) {
145+ return [ "enable" , "disable" , "status" , "install" , "uninstall" , "doctor" ] . includes ( argv [ 1 ] ) ;
146+ }
147+ return [ "enable" , "disable" , "status" , "install" , "uninstall" , "doctor" , "adopt" , "repair" ] . includes ( argv [ 0 ] ) ;
148+ }
149+
150+ function samePath ( left , right ) {
151+ if ( process . platform === "win32" ) return left . toLowerCase ( ) === right . toLowerCase ( ) ;
152+ return left === right ;
153+ }
154+
155+ function scopeAwareOptions ( argv , normalized ) {
156+ if ( ! flagPresent ( argv , "--global" ) ) return normalized ;
157+
158+ // Global commands select only the user-level installation. Give the enhanced
159+ // diagnostics a non-overlapping synthetic project view so a cwd that happens
160+ // to be the user home cannot block the explicitly selected global scope.
161+ return {
162+ ...normalized ,
163+ cwd : join ( normalized . home , ".codex-model-router-global-command" )
164+ } ;
165+ }
166+
139167export { reasoningWarnings } ;
140168
141169export async function resolveLocations ( options = { } , global = false ) {
@@ -144,27 +172,38 @@ export async function resolveLocations(options = {}, global = false) {
144172
145173export async function inspectInstallation ( options = { } , global = false ) {
146174 const normalized = await canonicalOptions ( options ) ;
147- const report = await inspectBaseInstallation ( normalized , global ) ;
148- report . cross_scope = await semanticCrossScope ( normalized ) ;
175+ const scoped = global ? scopeAwareOptions ( [ "status" , "--global" ] , normalized ) : normalized ;
176+ const report = await inspectBaseInstallation ( scoped , global ) ;
177+ report . roots . project = normalized . cwd ;
178+ report . cross_scope = await semanticCrossScope ( scoped ) ;
149179 return report ;
150180}
151181
152182export async function runCli ( argv , options = { } ) {
153183 const normalized = await canonicalOptions ( options ) ;
154- if ( ! isStatus ( argv ) ) return runEnhancedCli ( argv , normalized ) ;
155-
156184 const output = normalized . output ?? console . log ;
157- const json = argv . some ( ( token ) => token === "--json" || token . startsWith ( "--json=" ) ) ;
185+ const global = flagPresent ( argv , "--global" ) ;
186+
187+ if ( isOperational ( argv ) && ! global && samePath ( normalized . cwd , normalized . home ) ) {
188+ output ( "fail: project scope cannot use the user home as its project root; use --global or change to an actual project directory" ) ;
189+ return 1 ;
190+ }
191+
192+ const scoped = scopeAwareOptions ( argv , normalized ) ;
193+ if ( ! isStatus ( argv ) ) return runEnhancedCli ( argv , scoped ) ;
194+
195+ const json = flagPresent ( argv , "--json" ) ;
158196 const captured = [ ] ;
159197 const baseExit = await runEnhancedCli ( argv , {
160- ...normalized ,
198+ ...scoped ,
161199 output : json ? ( line ) => captured . push ( String ( line ) ) : output
162200 } ) ;
163- const crossScope = await semanticCrossScope ( normalized ) ;
201+ const crossScope = await semanticCrossScope ( scoped ) ;
164202 const conflict = crossScope . some ( ( entry ) => entry . status === "conflict" ) ;
165203
166204 if ( json ) {
167205 const report = JSON . parse ( captured . join ( "\n" ) ) ;
206+ report . roots . project = normalized . cwd ;
168207 report . cross_scope = crossScope ;
169208 output ( JSON . stringify ( report , null , 2 ) ) ;
170209 } else {
0 commit comments