@@ -20,6 +20,8 @@ import {
2020 writeRuntimePort ,
2121} from "../config" ;
2222import { collectStatus } from "./status" ;
23+ import { dispatchInternalCliCommand , type InternalCliCommand } from "./internal-dispatch" ;
24+ import { runTrayProxyRestart , runTrayProxyStart } from "./tray-proxy" ;
2325import { installCrashGuards } from "../lib/crash-guard" ;
2426import { hasHelpFlag , printSubcommandUsage , printUsage , printVersion } from "./help" ;
2527import { findAvailablePort , isAddrInUse , PortUnavailableError , shouldPersistSelectedPort , waitForPortAvailable } from "../server/ports" ;
@@ -305,45 +307,43 @@ async function handleEnsure() {
305307
306308/** Fixed tray action: start the proxy without depending on codexAutoStart. */
307309async function handleTrayProxyStart ( ) : Promise < void > {
308- const live = await findLiveProxy ( ) ;
309- if ( live ) {
310- console . log ( `Proxy already running on port ${ live . port } .` ) ;
311- return ;
312- }
313- const service = diagnoseService ( ) ;
314- const serviceStartable = serviceStartableFromTray ( service ) ;
315- if ( service . installed && ! serviceStartable ) {
316- console . error ( `Cannot start from the tray because the installed service is not viable: ${ service . summary } ` ) ;
317- console . error ( "Repair or remove the service before starting a direct proxy." ) ;
318- process . exitCode = 1 ;
319- return ;
320- }
321- if ( serviceStartable ) {
322- await serviceCommand ( "start" ) ;
323- } else {
324- const config = loadConfig ( ) ;
325- const port = ( config . port ?? 10100 ) > 0 ? ( config . port ?? 10100 ) : 10100 ;
326- const child = spawn ( process . execPath , startArgv ( port ) , {
327- detached : true ,
328- stdio : "ignore" ,
329- windowsHide : true ,
330- env : { ...process . env , OCX_SERVICE : "1" } ,
331- } ) ;
332- child . unref ( ) ;
333- }
334- const started = await waitForProxy ( ) ;
335- if ( ! started ) {
336- console . error ( "Proxy did not become healthy after the tray start action." ) ;
337- process . exitCode = 1 ;
338- return ;
339- }
340- console . log ( `Proxy running on port ${ started . port } .` ) ;
310+ const ok = await runTrayProxyStart ( {
311+ findLive : findLiveProxy ,
312+ diagnoseService : ( ) => {
313+ const service = diagnoseService ( ) ;
314+ return { installed : service . installed , startable : serviceStartableFromTray ( service ) , summary : service . summary } ;
315+ } ,
316+ startService : ( ) => serviceCommand ( "start" ) ,
317+ startDirect : ( ) => {
318+ const config = loadConfig ( ) ;
319+ const port = ( config . port ?? 10100 ) > 0 ? ( config . port ?? 10100 ) : 10100 ;
320+ const child = spawn ( process . execPath , startArgv ( port ) , {
321+ detached : true ,
322+ stdio : "ignore" ,
323+ windowsHide : true ,
324+ env : { ...process . env , OCX_SERVICE : "1" } ,
325+ } ) ;
326+ child . unref ( ) ;
327+ } ,
328+ waitForProxy,
329+ info : message => console . log ( message ) ,
330+ error : message => console . error ( message ) ,
331+ } ) ;
332+ if ( ! ok ) process . exitCode = 1 ;
341333}
342334
343335async function handleTrayProxyRestart ( ) : Promise < void > {
344- await handleStop ( ) ;
345- if ( process . exitCode && process . exitCode !== 0 ) return ;
346- await handleTrayProxyStart ( ) ;
336+ const ok = await runTrayProxyRestart ( {
337+ stop : async ( ) => {
338+ await handleStop ( ) ;
339+ return ! process . exitCode || process . exitCode === 0 ;
340+ } ,
341+ start : async ( ) => {
342+ await handleTrayProxyStart ( ) ;
343+ return ! process . exitCode || process . exitCode === 0 ;
344+ } ,
345+ } ) ;
346+ if ( ! ok ) process . exitCode = 1 ;
347347}
348348
349349async function handleStop ( ) {
@@ -696,21 +696,22 @@ switch (command) {
696696 break ;
697697 }
698698 case "__tray-start" :
699- await handleTrayProxyStart ( ) ;
700- break ;
701699 case "__tray-restart" :
702- await handleTrayProxyRestart ( ) ;
700+ case "__startup-health" :
701+ await dispatchInternalCliCommand ( command as InternalCliCommand , {
702+ trayStart : handleTrayProxyStart ,
703+ trayRestart : handleTrayProxyRestart ,
704+ startupHealth : async ( ) => {
705+ const { collectStartupHealth } = await import ( "../codex/autostart-health" ) ;
706+ console . log ( JSON . stringify ( collectStartupHealth ( loadConfig ( ) ) ) ) ;
707+ } ,
708+ } ) ;
703709 break ;
704710 case "__tray-host" : {
705711 const { runWindowsTrayHost } = await import ( "../tray/windows" ) ;
706712 await runWindowsTrayHost ( ) ;
707713 break ;
708714 }
709- case "__startup-health" : {
710- const { collectStartupHealth } = await import ( "../codex/autostart-health" ) ;
711- console . log ( JSON . stringify ( collectStartupHealth ( loadConfig ( ) ) ) ) ;
712- break ;
713- }
714715 case "__gui-update-worker" : {
715716 const jobId = args [ 1 ] ;
716717 if ( ! jobId ) process . exit ( 1 ) ;
0 commit comments