@@ -22,6 +22,7 @@ const YW = _useColor ? "\x1b[1;33m" : "";
2222
2323const { ROOT , SCRIPTS , run, runCapture : _runCapture , runInteractive, shellQuote, validateName } = require ( "./lib/runner" ) ;
2424const { resolveOpenshell } = require ( "./lib/resolve-openshell" ) ;
25+ const { startGatewayForRecovery } = require ( "./lib/onboard" ) ;
2526const {
2627 ensureApiKey,
2728 ensureGithubToken,
@@ -113,7 +114,7 @@ function getNamedGatewayLifecycleState() {
113114 return { state : "missing_named" , status : status . output , gatewayInfo : gatewayInfo . output } ;
114115}
115116
116- function recoverNamedGatewayRuntime ( ) {
117+ async function recoverNamedGatewayRuntime ( ) {
117118 const before = getNamedGatewayLifecycleState ( ) ;
118119 if ( before . state === "healthy_named" ) {
119120 return { recovered : true , before, after : before , attempted : false } ;
@@ -126,12 +127,23 @@ function recoverNamedGatewayRuntime() {
126127 return { recovered : true , before, after, attempted : true , via : "select" } ;
127128 }
128129
129- runOpenshell ( [ "gateway" , "start" , "--name" , "nemoclaw" ] , { ignoreError : true } ) ;
130- runOpenshell ( [ "gateway" , "select" , "nemoclaw" ] , { ignoreError : true } ) ;
131- after = getNamedGatewayLifecycleState ( ) ;
132- if ( after . state === "healthy_named" ) {
133- process . env . OPENSHELL_GATEWAY = "nemoclaw" ;
134- return { recovered : true , before, after, attempted : true , via : "start" } ;
130+ const shouldStartGateway = [ before . state , after . state ] . some ( ( state ) =>
131+ [ "named_unhealthy" , "named_unreachable" , "connected_other" ] . includes ( state )
132+ ) ;
133+
134+ if ( shouldStartGateway ) {
135+ try {
136+ await startGatewayForRecovery ( ) ;
137+ } catch {
138+ // Fall through to the lifecycle re-check below so we preserve the
139+ // existing recovery result shape and emit the correct classification.
140+ }
141+ runOpenshell ( [ "gateway" , "select" , "nemoclaw" ] , { ignoreError : true } ) ;
142+ after = getNamedGatewayLifecycleState ( ) ;
143+ if ( after . state === "healthy_named" ) {
144+ process . env . OPENSHELL_GATEWAY = "nemoclaw" ;
145+ return { recovered : true , before, after, attempted : true , via : "start" } ;
146+ }
135147 }
136148
137149 return { recovered : false , before, after, attempted : true } ;
@@ -183,7 +195,7 @@ function printGatewayLifecycleHint(output = "", sandboxName = "", writer = conso
183195 }
184196}
185197
186- function getReconciledSandboxGatewayState ( sandboxName ) {
198+ async function getReconciledSandboxGatewayState ( sandboxName ) {
187199 let lookup = getSandboxGatewayState ( sandboxName ) ;
188200 if ( lookup . state === "present" ) {
189201 return lookup ;
@@ -193,7 +205,7 @@ function getReconciledSandboxGatewayState(sandboxName) {
193205 }
194206
195207 if ( lookup . state === "gateway_error" ) {
196- const recovery = recoverNamedGatewayRuntime ( ) ;
208+ const recovery = await recoverNamedGatewayRuntime ( ) ;
197209 if ( recovery . recovered ) {
198210 const retried = getSandboxGatewayState ( sandboxName ) ;
199211 if ( retried . state === "present" || retried . state === "missing" ) {
@@ -235,8 +247,8 @@ function getReconciledSandboxGatewayState(sandboxName) {
235247 return lookup ;
236248}
237249
238- function ensureLiveSandboxOrExit ( sandboxName ) {
239- const lookup = getReconciledSandboxGatewayState ( sandboxName ) ;
250+ async function ensureLiveSandboxOrExit ( sandboxName ) {
251+ const lookup = await getReconciledSandboxGatewayState ( sandboxName ) ;
240252 if ( lookup . state === "present" ) {
241253 return lookup ;
242254 }
@@ -539,8 +551,8 @@ function listSandboxes() {
539551
540552// ── Sandbox-scoped actions ───────────────────────────────────────
541553
542- function sandboxConnect ( sandboxName ) {
543- ensureLiveSandboxOrExit ( sandboxName ) ;
554+ async function sandboxConnect ( sandboxName ) {
555+ await ensureLiveSandboxOrExit ( sandboxName ) ;
544556 // Ensure port forward is alive before connecting
545557 runOpenshell ( [ "forward" , "start" , "--background" , "18789" , sandboxName ] , { ignoreError : true } ) ;
546558 const result = spawnSync ( getOpenshellBinary ( ) , [ "sandbox" , "connect" , sandboxName ] , {
@@ -551,7 +563,7 @@ function sandboxConnect(sandboxName) {
551563 exitWithSpawnResult ( result ) ;
552564}
553565
554- function sandboxStatus ( sandboxName ) {
566+ async function sandboxStatus ( sandboxName ) {
555567 const sb = registry . getSandbox ( sandboxName ) ;
556568 const live = parseGatewayInference (
557569 _runCapture ( "openshell inference get 2>/dev/null" , { ignoreError : true } )
@@ -565,7 +577,7 @@ function sandboxStatus(sandboxName) {
565577 console . log ( ` Policies: ${ ( sb . policies || [ ] ) . join ( ", " ) || "none" } ` ) ;
566578 }
567579
568- const lookup = getReconciledSandboxGatewayState ( sandboxName ) ;
580+ const lookup = await getReconciledSandboxGatewayState ( sandboxName ) ;
569581 if ( lookup . state === "present" ) {
570582 console . log ( "" ) ;
571583 if ( lookup . recoveredGateway ) {
@@ -778,8 +790,8 @@ const [cmd, ...args] = process.argv.slice(2);
778790 const actionArgs = args . slice ( 1 ) ;
779791
780792 switch ( action ) {
781- case "connect" : sandboxConnect ( cmd ) ; break ;
782- case "status" : sandboxStatus ( cmd ) ; break ;
793+ case "connect" : await sandboxConnect ( cmd ) ; break ;
794+ case "status" : await sandboxStatus ( cmd ) ; break ;
783795 case "logs" : sandboxLogs ( cmd , actionArgs . includes ( "--follow" ) ) ; break ;
784796 case "policy-add" : await sandboxPolicyAdd ( cmd ) ; break ;
785797 case "policy-list" : sandboxPolicyList ( cmd ) ; break ;
0 commit comments