@@ -10,6 +10,7 @@ import {
1010 type DesktopServerExposureState ,
1111} from "@t3tools/contracts" ;
1212import { isTailscaleIpv4Address , readTailscaleStatus } from "@t3tools/tailscale" ;
13+ import * as Cache from "effect/Cache" ;
1314import * as Context from "effect/Context" ;
1415import * as Duration from "effect/Duration" ;
1516import * as Effect from "effect/Effect" ;
@@ -416,6 +417,25 @@ function resolveRuntimeState(input: {
416417 } ;
417418}
418419
420+ const tailscaleIpv4AddressKey = (
421+ networkInterfaces : DesktopNetworkInterfaces . NetworkInterfaces ,
422+ ) : string =>
423+ [
424+ ...new Set (
425+ Object . values ( networkInterfaces )
426+ . flatMap ( ( addresses ) => addresses ?? [ ] )
427+ . filter (
428+ ( address ) =>
429+ ! address . internal &&
430+ address . family === "IPv4" &&
431+ isTailscaleIpv4Address ( address . address ) ,
432+ )
433+ . map ( ( address ) => address . address ) ,
434+ ) ,
435+ ]
436+ . toSorted ( )
437+ . join ( "," ) ;
438+
419439const requiresBackendRelaunch = ( previous : RuntimeState , next : RuntimeState ) : boolean =>
420440 previous . port !== next . port ||
421441 previous . bindHost !== next . bindHost ||
@@ -434,16 +454,17 @@ export const make = Effect.gen(function* () {
434454 // with values it read before that change persisted.
435455 const changePermit = yield * Semaphore . make ( 1 ) ;
436456
437- // Cache the `tailscale status` spawn for the TTL. On macOS, the Mac App
438- // Store Tailscale CLI lives inside Tailscale's sandbox container, so each
439- // spawn re-triggers the "Other apps" TCC prompt.
440- const cachedReadTailscaleStatus = yield * Effect . cachedWithTTL (
441- readTailscaleStatus . pipe (
442- Effect . orElseSucceed ( ( ) => null ) ,
443- Effect . provideService ( ChildProcessSpawner . ChildProcessSpawner , childProcessSpawner ) ,
444- ) ,
445- TAILSCALE_STATUS_CACHE_TTL ,
446- ) ;
457+ // Key status by interface addresses so network changes bypass stale DNS and
458+ // identity. Cache unchanged addresses to avoid repeated macOS TCC prompts.
459+ const tailscaleStatusCache = yield * Cache . make ( {
460+ capacity : 1 ,
461+ timeToLive : TAILSCALE_STATUS_CACHE_TTL ,
462+ lookup : ( _addresses : string ) =>
463+ readTailscaleStatus . pipe (
464+ Effect . orElseSucceed ( ( ) => null ) ,
465+ Effect . provideService ( ChildProcessSpawner . ChildProcessSpawner , childProcessSpawner ) ,
466+ ) ,
467+ } ) ;
447468
448469 const readNetworkInterfaces = networkInterfaces . read ;
449470
@@ -564,7 +585,10 @@ export const make = Effect.gen(function* () {
564585 serveEnabled : state . tailscaleServeEnabled ,
565586 servePort : state . tailscaleServePort ,
566587 networkInterfaces : currentNetworkInterfaces ,
567- readStatus : cachedReadTailscaleStatus ,
588+ readStatus : Cache . get (
589+ tailscaleStatusCache ,
590+ tailscaleIpv4AddressKey ( currentNetworkInterfaces ) ,
591+ ) ,
568592 } ) . pipe (
569593 Effect . provideService ( ChildProcessSpawner . ChildProcessSpawner , childProcessSpawner ) ,
570594 Effect . provideService ( HttpClient . HttpClient , httpClient ) ,
0 commit comments