11import { sendToDaemon } from '../daemon/client/daemon-client.ts' ;
22import { prepareMetroRuntime , reloadMetro } from '../metro/client-metro.ts' ;
33import {
4+ clearMetroSessionHints ,
45 readMetroSessionHints ,
56 writeMetroSessionHints ,
67 type MetroSessionHints ,
@@ -51,9 +52,14 @@ import type {
5152 Lease ,
5253 MaterializationReleaseOptions ,
5354 MetroPrepareOptions ,
55+ MetroPrepareResult ,
5456} from './client-types.ts' ;
5557import type { CommandResult } from '../core/command-descriptor/command-result.ts' ;
56- import { isNonDefaultResponseLevel , type ResponseLevel } from '../kernel/contracts.ts' ;
58+ import {
59+ isNonDefaultResponseLevel ,
60+ type ResponseLevel ,
61+ type SessionRuntimeHints ,
62+ } from '../kernel/contracts.ts' ;
5763import { readSerializedSnapshotCaptureAnnotations } from '../snapshot-capture-annotations.ts' ;
5864import { readSnapshotDiagnosticsSummary } from '../snapshot-diagnostics.ts' ;
5965import type { CommandFlags } from '../core/dispatch-context.ts' ;
@@ -163,13 +169,18 @@ export function createAgentDeviceClient(
163169 } ,
164170 close : async ( options = { } ) => {
165171 const session = resolveRequestSession ( options ) ;
166- const data = await executeCommand < Record < string , unknown > > ( 'close' , options ) ;
167- return {
168- session,
169- shutdown : normalizeTargetShutdownResult ( data . shutdown ) ,
170- provider : readObject ( data . provider ) ,
171- identifiers : { session } ,
172- } ;
172+ try {
173+ const data = await executeCommand < Record < string , unknown > > ( 'close' , options ) ;
174+ return {
175+ session,
176+ shutdown : normalizeTargetShutdownResult ( data . shutdown ) ,
177+ provider : readObject ( data . provider ) ,
178+ identifiers : { session } ,
179+ } ;
180+ } finally {
181+ // Close is teardown intent: drop the dev-server binding even if the daemon call fails.
182+ clearMetroSessionHintsQuietly ( config , options ) ;
183+ }
173184 } ,
174185 artifacts : async ( options = { } ) =>
175186 await executeCommand < AgentArtifactsResult > ( 'artifacts' , options ) ,
@@ -199,6 +210,12 @@ export function createAgentDeviceClient(
199210 open : async ( options : AppOpenOptions ) => {
200211 const session = resolveRequestSession ( options ) ;
201212 const data = await executeCommand < Record < string , unknown > > ( 'open' , options ) ;
213+ recordMetroSessionHintsAfterOpen ( {
214+ config,
215+ options,
216+ runtime : mergeClientOptions ( config , options ) . runtime ,
217+ sessionReused : data . sessionReused === true ,
218+ } ) ;
202219 const device = normalizeOpenDevice ( data ) ;
203220 const appBundleId = readOptionalString ( data , 'appBundleId' ) ;
204221 const appId = appBundleId ;
@@ -283,7 +300,7 @@ export function createAgentDeviceClient(
283300 runtimeFilePath : options . runtimeFilePath ,
284301 logPath : options . logPath ,
285302 } ) ;
286- persistMetroSessionHints ( config , result . statusUrl ) ;
303+ persistMetroSessionHints ( config , result ) ;
287304 return result ;
288305 } ,
289306 reload : async ( options = { } ) =>
@@ -445,23 +462,33 @@ function mergeClientOptions(
445462 return { ...config , ...options } ;
446463}
447464
448- function metroSessionHintsStateDir ( config : AgentDeviceClientConfig ) : string {
449- return resolveDaemonPaths ( config . stateDir ?? process . env . AGENT_DEVICE_STATE_DIR ) . baseDir ;
465+ function metroSessionHintsScope (
466+ config : AgentDeviceClientConfig ,
467+ options : InternalRequestOptions = { } ,
468+ ) : { stateDir : string ; session : string } {
469+ const merged = mergeClientOptions ( config , options ) ;
470+ return {
471+ stateDir : resolveDaemonPaths ( merged . stateDir ?? process . env . AGENT_DEVICE_STATE_DIR ) . baseDir ,
472+ session : resolveSessionName ( merged . session ) ,
473+ } ;
450474}
451475
452- // `metro prepare` and ` metro reload` are local-only (they never round-trip through the daemon),
453- // so session-scoped Metro coordinates can't live in the daemon's session store. Persisting them
454- // to a small per-session file lets a plain `metro reload --session <s>` find the dev server a
455- // prior `metro prepare --session <s>` bound, instead of silently defaulting to localhost:8081.
456- function persistMetroSessionHints ( config : AgentDeviceClientConfig , statusUrl : string ) : void {
476+ // The metro-sessions file is the session's dev-server binding; see MetroSessionHints.
477+ function persistMetroSessionHints (
478+ config : AgentDeviceClientConfig ,
479+ result : Pick < MetroPrepareResult , 'statusUrl' | 'bridge' | 'iosRuntime' | 'androidRuntime' > ,
480+ ) : void {
457481 try {
458- const url = new URL ( statusUrl ) ;
482+ const url = new URL ( result . statusUrl ) ;
459483 const port = Number . parseInt ( url . port , 10 ) ;
460484 if ( ! url . hostname || ! Number . isInteger ( port ) ) return ;
485+ // Bridge runtimes carry remote bundle URLs; persist local-flow bundle URLs only.
486+ const bundleUrl = result . bridge
487+ ? undefined
488+ : ( result . iosRuntime . bundleUrl ?? result . androidRuntime . bundleUrl ) ;
461489 writeMetroSessionHints ( {
462- stateDir : metroSessionHintsStateDir ( config ) ,
463- session : resolveSessionName ( config . session ) ,
464- hints : { metroHost : url . hostname , metroPort : port } ,
490+ ...metroSessionHintsScope ( config ) ,
491+ hints : { metroHost : url . hostname , metroPort : port , bundleUrl } ,
465492 } ) ;
466493 } catch {
467494 // Session-hint persistence is best-effort; reload still works with explicit flags.
@@ -470,15 +497,57 @@ function persistMetroSessionHints(config: AgentDeviceClientConfig, statusUrl: st
470497
471498function resolveMetroSessionHints ( config : AgentDeviceClientConfig ) : MetroSessionHints | undefined {
472499 try {
473- return readMetroSessionHints ( {
474- stateDir : metroSessionHintsStateDir ( config ) ,
475- session : resolveSessionName ( config . session ) ,
476- } ) ;
500+ return readMetroSessionHints ( metroSessionHintsScope ( config ) ) ;
477501 } catch {
478502 return undefined ;
479503 }
480504}
481505
506+ function metroHintsFromRuntime (
507+ runtime : SessionRuntimeHints | undefined ,
508+ ) : MetroSessionHints | undefined {
509+ if ( ! runtime ) return undefined ;
510+ const { metroHost, metroPort, bundleUrl } = runtime ;
511+ if ( metroHost === undefined && metroPort === undefined && bundleUrl === undefined ) {
512+ return undefined ;
513+ }
514+ return { metroHost, metroPort, bundleUrl } ;
515+ }
516+
517+ // Hint flags rebind the session's dev server; a hintless open that created the session clears
518+ // any leftover binding from a previous same-name session.
519+ function recordMetroSessionHintsAfterOpen ( params : {
520+ config : AgentDeviceClientConfig ;
521+ options : InternalRequestOptions ;
522+ runtime : SessionRuntimeHints | undefined ;
523+ sessionReused : boolean ;
524+ } ) : void {
525+ try {
526+ const scope = metroSessionHintsScope ( params . config , params . options ) ;
527+ const hints = metroHintsFromRuntime ( params . runtime ) ;
528+ if ( hints ) {
529+ writeMetroSessionHints ( { ...scope , hints } ) ;
530+ return ;
531+ }
532+ if ( ! params . sessionReused ) {
533+ clearMetroSessionHints ( scope ) ;
534+ }
535+ } catch {
536+ // Session-hint sync is best-effort; reload still works with explicit flags.
537+ }
538+ }
539+
540+ function clearMetroSessionHintsQuietly (
541+ config : AgentDeviceClientConfig ,
542+ options : InternalRequestOptions ,
543+ ) : void {
544+ try {
545+ clearMetroSessionHints ( metroSessionHintsScope ( config , options ) ) ;
546+ } catch {
547+ // Session-hint cleanup is best-effort; close must not fail on local file state.
548+ }
549+ }
550+
482551function normalizeLease ( data : Record < string , unknown > ) : Lease {
483552 const rawLease = data . lease ;
484553 if ( ! isRecord ( rawLease ) ) {
0 commit comments