@@ -13,6 +13,7 @@ import {
1313 type DevicePlatformAvailability ,
1414 type SimlockDeviceHostConfig ,
1515} from "@t3tools/contracts" ;
16+ import * as Cause from "effect/Cause" ;
1617import * as Clock from "effect/Clock" ;
1718import * as Duration from "effect/Duration" ;
1819import * as Effect from "effect/Effect" ;
@@ -105,13 +106,17 @@ export const machineSimlockToken = (environment: NodeJS.ProcessEnv) => {
105106 return URL . canParse ( gatewayUrl ) ? { gatewayUrl, agentToken } : null ;
106107} ;
107108
108- /** The saved token, else this machine's agent-device token, but only for the gateway it targets. */
109+ /**
110+ * The saved token, else this machine's agent-device token; each only for the gateway it belongs to,
111+ * so a URL being edited never receives either.
112+ */
109113export const resolveSimlockToken = (
110114 gatewayUrl : string ,
111- saved : string ,
115+ saved : { readonly gatewayUrl : string ; readonly agentToken : string } | undefined ,
112116 environment : NodeJS . ProcessEnv ,
113117) => {
114- if ( saved ) return { agentToken : saved , source : "settings" as const } ;
118+ if ( saved ?. agentToken && sameOrigin ( saved . gatewayUrl , gatewayUrl ) )
119+ return { agentToken : saved . agentToken , source : "settings" as const } ;
115120 const machine = machineSimlockToken ( environment ) ;
116121 return machine && sameOrigin ( machine . gatewayUrl , gatewayUrl )
117122 ? { agentToken : machine . agentToken , source : "env" as const }
@@ -120,12 +125,18 @@ export const resolveSimlockToken = (
120125
121126/** What went wrong talking to a gateway, without the gateway's own text. */
122127export const describeGatewayFailure = ( cause : unknown , gatewayUrl : string , tokenOwner : string ) => {
123- const status = HttpClientError . isHttpClientError ( cause ) ? cause . response ?. status : undefined ;
124- return status === 401 || status === 403
125- ? `The Simlock gateway refused the agent token for ${ tokenOwner } .`
126- : status !== undefined
127- ? `The Simlock gateway at ${ gatewayUrl } answered HTTP ${ status } .`
128- : `Could not reach the Simlock gateway at ${ gatewayUrl } .` ;
128+ if ( Cause . isTimeoutError ( cause ) ) return `Could not reach the Simlock gateway at ${ gatewayUrl } .` ;
129+ if ( ! HttpClientError . isHttpClientError ( cause ) )
130+ return `The Simlock gateway at ${ gatewayUrl } sent an answer T3 could not read.` ;
131+ if ( cause . reason . _tag === "StatusCodeError" ) {
132+ const status = cause . reason . response . status ;
133+ return status === 401 || status === 403
134+ ? `The Simlock gateway refused the agent token for ${ tokenOwner } .`
135+ : `The Simlock gateway at ${ gatewayUrl } answered HTTP ${ status } .` ;
136+ }
137+ return cause . reason . _tag === "TransportError"
138+ ? `Could not reach the Simlock gateway at ${ gatewayUrl } .`
139+ : `The Simlock gateway at ${ gatewayUrl } sent an answer T3 could not read.` ;
129140} ;
130141
131142const decodeIdentity = Schema . decodeUnknownEffect (
@@ -172,8 +183,7 @@ export const make = Effect.fn("SimlockDeviceHost.make")(function* (
172183 const environment = yield * HostProcess . Environment ;
173184 const hostId = config . id ;
174185 const gateway = config . gatewayUrl . replace ( / \/ $ / , "" ) ;
175- const agentToken =
176- resolveSimlockToken ( config . gatewayUrl , config . agentToken , environment ) ?. agentToken ?? "" ;
186+ const agentToken = resolveSimlockToken ( config . gatewayUrl , config , environment ) ?. agentToken ?? "" ;
177187 const agentDeviceUrl = `${ gateway } /v1/agent-device` ;
178188 const hostDir = path . join ( server . stateDir , "device" , "simlock" , hostId ) ;
179189 const leases = new Map < string , Lease > ( ) ;
@@ -280,14 +290,11 @@ export const make = Effect.fn("SimlockDeviceHost.make")(function* (
280290 const decoded = yield * request ( "GET" , "/v1/catalog" ) . pipe (
281291 Effect . flatMap ( decodeCatalog ) ,
282292 Effect . mapError ( ( cause ) =>
283- unavailable (
284- `${ describeGatewayFailure ( cause , gateway , config . label ) } Check it in Settings.` ,
285- cause ,
286- ) ,
293+ unavailable ( describeGatewayFailure ( cause , gateway , config . label ) , cause ) ,
287294 ) ,
288295 ) ;
289- const devices = decoded . platforms . flatMap ( ( entry ) => {
290- if ( entry . platform !== "ios" && entry . platform !== "android" ) return [ ] ;
296+ const byPlatform = decoded . platforms . map ( ( entry ) => {
297+ if ( entry . platform !== "ios" && entry . platform !== "android" ) return null ;
291298 const platform : DevicePlatform = entry . platform ;
292299 return Object . entries ( entry . modelRuntimes ) . flatMap ( ( [ model , runtimes ] ) => {
293300 const kind = entry . modelClasses ?. [ model ] ;
@@ -306,8 +313,12 @@ export const make = Effect.fn("SimlockDeviceHost.make")(function* (
306313 ] ;
307314 } ) ;
308315 } ) ;
309- // An empty catalog is usually workers reconnecting; caching it would hide the pool for the TTL.
310- catalog = devices . length > 0 ? { at : now , devices } : null ;
316+ const devices = byPlatform . flatMap ( ( entry ) => entry ?? [ ] ) ;
317+ // A platform with nothing to offer is usually its workers reconnecting; caching that would
318+ // hide it for the TTL, so only a catalog with devices on every platform is kept.
319+ catalog = byPlatform . every ( ( entry ) => entry === null || entry . length > 0 )
320+ ? { at : now , devices }
321+ : null ;
311322 return devices ;
312323 } ) ;
313324
0 commit comments