11import {
2+ AuthRelayReadScope ,
23 AuthRelayWriteScope ,
4+ type AuthEnvironmentScope ,
5+ type EnvironmentConnectReachability ,
36 EnvironmentHttpApi ,
7+ formatConnectReachable ,
48 type RelayClientInstallProgressEvent ,
59 type RelayClientInstallProgressStage ,
610} from "@t3tools/contracts" ;
@@ -9,6 +13,7 @@ import * as HostProcess from "@t3tools/shared/HostProcess";
913import * as RelayClient from "@t3tools/shared/relayClient" ;
1014import { withRelayClientTracing } from "@t3tools/shared/relayTracing" ;
1115import * as Cause from "effect/Cause" ;
16+ import * as Clock from "effect/Clock" ;
1217import * as Config from "effect/Config" ;
1318import * as Console from "effect/Console" ;
1419import * as Crypto from "effect/Crypto" ;
@@ -29,6 +34,7 @@ import * as ServerSecretStore from "../auth/ServerSecretStore.ts";
2934import * as BootService from "../cloud/bootService.ts" ;
3035import * as CliState from "../cloud/CliState.ts" ;
3136import * as CliTokenManager from "../cloud/CliTokenManager.ts" ;
37+ import { formatReachabilityFailure } from "../cloud/ConnectReachability.ts" ;
3238import { filterRelayResponse } from "../cloud/relayResponse.ts" ;
3339import {
3440 CLOUD_LINK_REVOKED ,
@@ -142,6 +148,9 @@ interface CloudCliStatus {
142148 readonly relayUrl : string | null ;
143149 readonly publishAgentActivity : boolean ;
144150 readonly relayClient : RelayClient . RelayClientStatus ;
151+ readonly linkMode : CliState . CliDesiredLinkMode ;
152+ /** `null` when no server answered the live check, or none ran because no tunnel is wanted. */
153+ readonly reachability : EnvironmentConnectReachability | null ;
145154}
146155
147156function formatRelayClientStatus ( executable : RelayClient . RelayClientStatus ) : ReadonlyArray < string > {
@@ -199,8 +208,19 @@ function formatCloudStatus(status: CloudCliStatus, options?: { readonly json?: b
199208 ` Relay: ${ status . relayUrl ?? "not provisioned" } ` ,
200209 ` Publish agent activity: ${ status . publishAgentActivity ? "enabled" : "disabled" } ` ,
201210 ...formatRelayClientStatus ( status . relayClient ) ,
202- "" ,
203- "This is saved setup, not a live connection check. Check the background service with `t3 service status`." ,
211+ ...( ! status . desired || status . linkMode !== "managed"
212+ ? [ ]
213+ : status . reachability === null
214+ ? [
215+ "" ,
216+ "No running server answered, so reachability was not checked. Check the background service with `t3 service status`." ,
217+ ]
218+ : status . reachability . status === "reachable"
219+ ? [
220+ "" ,
221+ `✓ Other devices can reach this machine · ${ formatConnectReachable ( status . reachability ) } ` ,
222+ ]
223+ : [ "" , formatReachabilityFailure ( status . reachability ) ] ) ,
204224 ...( nextStep ? [ "" , `Next: ${ nextStep } ` ] : [ ] ) ,
205225 ] . join ( "\n" ) ;
206226}
@@ -261,11 +281,12 @@ export const acquireRelayClientForLink = Effect.fn("cloud.cli.acquire_relay_clie
261281
262282const withCloudCliSessionToken = < A , E , R > (
263283 environmentAuth : EnvironmentAuth . EnvironmentAuth [ "Service" ] ,
284+ scope : AuthEnvironmentScope ,
264285 run : ( token : string ) => Effect . Effect < A , E , R > ,
265286) =>
266287 Effect . acquireUseRelease (
267288 environmentAuth . issueSession ( {
268- scopes : [ AuthRelayWriteScope ] ,
289+ scopes : [ scope ] ,
269290 subject : "cloud-cli" ,
270291 label : "t3 connect cli" ,
271292 } ) ,
@@ -287,7 +308,7 @@ const runLiveCloudUnlink = Effect.fn("cloud.cli.run_live_unlink")(function* () {
287308
288309 const environmentAuth = yield * EnvironmentAuth . EnvironmentAuth ;
289310 const result = yield * Effect . exit (
290- withCloudCliSessionToken ( environmentAuth , ( token ) =>
311+ withCloudCliSessionToken ( environmentAuth , AuthRelayWriteScope , ( token ) =>
291312 HttpApiClient . make ( EnvironmentHttpApi , {
292313 baseUrl : runtimeState . value . origin ,
293314 } ) . pipe (
@@ -303,6 +324,73 @@ const runLiveCloudUnlink = Effect.fn("cloud.cli.run_live_unlink")(function* () {
303324 : ( { status : "failed" , cause : result . cause } satisfies LiveCloudActionResult ) ;
304325} ) ;
305326
327+ // One check probes the endpoint, the tunnel edge, and Tailscale, each with its own timeout.
328+ const CLOUD_CLI_REACHABILITY_TIMEOUT = Duration . seconds ( 30 ) ;
329+ const CLOUD_CLI_REACHABILITY_WAIT = Duration . seconds ( 60 ) ;
330+ const CLOUD_CLI_REACHABILITY_RETRY = Duration . seconds ( 5 ) ;
331+
332+ /** Asks the running server to check T3 Connect now. `null` when no server answers. */
333+ const checkLiveReachability = Effect . fn ( "cloud.cli.check_live_reachability" ) ( function * ( ) {
334+ const config = yield * ServerConfig . ServerConfig ;
335+ const runtimeState = yield * readPersistedServerRuntimeState ( config . serverRuntimeStatePath ) ;
336+ if ( Option . isNone ( runtimeState ) ) return null ;
337+ const environmentAuth = yield * EnvironmentAuth . EnvironmentAuth ;
338+ return yield * withCloudCliSessionToken ( environmentAuth , AuthRelayReadScope , ( token ) =>
339+ HttpApiClient . make ( EnvironmentHttpApi , { baseUrl : runtimeState . value . origin } ) . pipe (
340+ Effect . flatMap ( ( client ) =>
341+ client . connect . checkReachability ( { headers : { authorization : `Bearer ${ token } ` } } ) ,
342+ ) ,
343+ Effect . timeout ( CLOUD_CLI_REACHABILITY_TIMEOUT ) ,
344+ ) ,
345+ ) . pipe ( Effect . orElseSucceed ( ( ) => null ) ) ;
346+ } ) ;
347+
348+ function formatAlertBox ( text : string ) : string {
349+ const lines = text . split ( "\n" ) ;
350+ const width = Math . max ( ...lines . map ( ( line ) => line . length ) ) ;
351+ const color = process . stdout . isTTY === true && ! process . env . NO_COLOR ;
352+ const red = ( line : string ) => ( color ? `\x1b[1;31m${ line } \x1b[0m` : line ) ;
353+ return [
354+ `┌${ "─" . repeat ( width + 2 ) } ┐` ,
355+ ...lines . map ( ( line ) => `│ ${ line . padEnd ( width ) } │` ) ,
356+ `└${ "─" . repeat ( width + 2 ) } ┘` ,
357+ ]
358+ . map ( red )
359+ . join ( "\n" ) ;
360+ }
361+
362+ /**
363+ * Waits for the server that `t3 connect` just started to prove other devices
364+ * can reach it, so a broken tunnel fails setup instead of looking green.
365+ */
366+ const waitForReachability = Effect . fn ( "cloud.cli.wait_for_reachability" ) ( function * ( ) {
367+ yield * Console . log ( "\nChecking that other devices can reach this machine..." ) ;
368+ const deadline =
369+ ( yield * Clock . currentTimeMillis ) + Duration . toMillis ( CLOUD_CLI_REACHABILITY_WAIT ) ;
370+ let last : EnvironmentConnectReachability | null = null ;
371+ while ( true ) {
372+ last = ( yield * checkLiveReachability ( ) ) ?? last ;
373+ if ( last ?. status === "reachable" ) {
374+ yield * Console . log (
375+ `✓ Other devices can reach this machine · ${ formatConnectReachable ( last ) } ` ,
376+ ) ;
377+ return ;
378+ }
379+ if ( ( yield * Clock . currentTimeMillis ) >= deadline ) break ;
380+ yield * Effect . sleep ( CLOUD_CLI_REACHABILITY_RETRY ) ;
381+ }
382+ yield * Console . error (
383+ formatAlertBox (
384+ last === null
385+ ? "✕ Could not check T3 Connect\n The T3 server on this machine did not answer.\n Fix: run `t3 service status`, then `t3 connect status`."
386+ : formatReachabilityFailure ( last ) ,
387+ ) ,
388+ ) ;
389+ yield * Effect . sync ( ( ) => {
390+ process . exitCode = 1 ;
391+ } ) ;
392+ } ) ;
393+
306394type RelayUnlinkResult =
307395 | { readonly status : "not-authenticated" }
308396 | { readonly status : "revoked" }
@@ -551,7 +639,9 @@ const connectStatusCommand = Command.make("status", {
551639 ...projectLocationFlags ,
552640 json : jsonFlag ,
553641} ) . pipe (
554- Command . withDescription ( "Show persisted T3 Connect and relay client state." ) ,
642+ Command . withDescription (
643+ "Show T3 Connect setup and check that other devices can reach this machine." ,
644+ ) ,
555645 Command . withHandler ( ( flags ) =>
556646 runCloudCommand (
557647 flags ,
@@ -567,6 +657,7 @@ const connectStatusCommand = Command.make("status", {
567657 publishAgentActivity ,
568658 revoked ,
569659 executable ,
660+ linkMode ,
570661 ] = yield * Effect . all (
571662 [
572663 CliState . readCliDesiredCloudLink ,
@@ -576,9 +667,13 @@ const connectStatusCommand = Command.make("status", {
576667 secrets . get ( PUBLISH_AGENT_ACTIVITY_SECRET ) ,
577668 secrets . get ( CLOUD_LINK_REVOKED ) ,
578669 relayClient . resolve ,
670+ CliState . readCliDesiredLinkMode ,
579671 ] ,
580672 { concurrency : "unbounded" } ,
581673 ) ;
674+ // A publish-only link has no tunnel for other devices to reach.
675+ const reachability =
676+ desired && linkMode === "managed" ? yield * checkLiveReachability ( ) : null ;
582677 const status : CloudCliStatus = {
583678 desired,
584679 authenticated,
@@ -590,6 +685,8 @@ const connectStatusCommand = Command.make("status", {
590685 Option . isSome ( publishAgentActivity ) ? bytesToString ( publishAgentActivity . value ) : null ,
591686 ) ,
592687 relayClient : executable ,
688+ linkMode,
689+ reachability,
593690 } ;
594691 yield * Console . log ( formatCloudStatus ( status , { json : flags . json } ) ) ;
595692 } ) ,
@@ -703,7 +800,7 @@ export const reportServiceOnboarding = Effect.fn("cli.connect.report_service_onb
703800 }
704801 const serveCommand = yield * resolveCliCommand ( "serve" ) ;
705802 yield * Console . log (
706- `\nNext\n Start the server with \`${ serveCommand } \` to make this machine reachable.` ,
803+ `\nNext\n Start the server with \`${ serveCommand } \` to make this machine reachable.\n It checks that other devices can reach this machine when it starts. ` ,
707804 ) ;
708805 } ,
709806) ;
@@ -724,9 +821,13 @@ export const connectCommand = Command.make("connect", {
724821 }
725822 yield * Console . log ( `✓ Authorized${ connectedAs ( linked . identity ) } ` ) ;
726823
727- yield * reportServiceOnboarding (
728- yield * CliService . recoverServiceOnboardingOffer ( CliService . offerServiceDuringOnboarding ) ,
824+ const outcome = yield * CliService . recoverServiceOnboardingOffer (
825+ CliService . offerServiceDuringOnboarding ,
729826 ) ;
827+ yield * reportServiceOnboarding ( outcome ) ;
828+ if ( outcome !== "skipped" ) {
829+ yield * waitForReachability ( ) ;
830+ }
730831 } ) ,
731832 ) ,
732833 ) ,
0 commit comments