@@ -6,12 +6,15 @@ import * as Duration from "effect/Duration";
66import * as Effect from "effect/Effect" ;
77import * as Exit from "effect/Exit" ;
88import * as Fiber from "effect/Fiber" ;
9+ import * as FileSystem from "effect/FileSystem" ;
910import * as Layer from "effect/Layer" ;
1011import * as Logger from "effect/Logger" ;
1112import * as Option from "effect/Option" ;
13+ import * as Path from "effect/Path" ;
1214import * as PlatformError from "effect/PlatformError" ;
1315import * as Queue from "effect/Queue" ;
1416import * as References from "effect/References" ;
17+ import * as Schema from "effect/Schema" ;
1518import * as Sink from "effect/Sink" ;
1619import * as Stream from "effect/Stream" ;
1720import * as TestClock from "effect/testing/TestClock" ;
@@ -42,6 +45,15 @@ const layerRelayClientAvailable = Layer.succeed(
4245
4346const unusedHttpClient = HttpClient . make ( ( ) => Effect . die ( "unused" ) ) ;
4447
48+ const decodeLogEntry = Schema . decodeUnknownSync (
49+ Schema . fromJsonString (
50+ Schema . Struct ( {
51+ level : Schema . String ,
52+ message : Schema . Tuple ( [ Schema . String , Schema . Unknown ] ) ,
53+ } ) ,
54+ ) ,
55+ ) ;
56+
4557const emptySecretStoreLayer = Layer . mock ( ServerSecretStore . ServerSecretStore ) ( {
4658 get : ( ) => Effect . succeedNone ,
4759 set : ( ) => Effect . void ,
@@ -59,7 +71,8 @@ const runtimeDependencies = (
5971 Layer . succeed ( HttpClient . HttpClient , httpClient ) ,
6072 relayClientLayer ,
6173 secretStoreLayer ,
62- ) ;
74+ ServerConfig . layerTest ( process . cwd ( ) , { prefix : "t3-managed-endpoint-" } ) ,
75+ ) . pipe ( Layer . provideMerge ( NodeServices . layer ) ) ;
6376
6477const buildCloudManagedEndpointRuntime = (
6578 spawner : ReturnType < typeof ChildProcessSpawner . make > ,
@@ -543,6 +556,97 @@ describe("CloudManagedEndpointRuntime", () => {
543556 } ) ,
544557 ) ;
545558
559+ it . effect ( "keeps redacted connector logs bounded even when rotation fails" , ( ) =>
560+ Effect . gen ( function * ( ) {
561+ const fs = yield * FileSystem . FileSystem ;
562+ const path = yield * Path . Path ;
563+ for ( const rotationBlocked of [ false , true ] ) {
564+ const baseDir = yield * fs . makeTempDirectoryScoped ( { prefix : "t3-relay-client-log-" } ) ;
565+ const logPath = path . join ( baseDir , "userdata" , "logs" , "relay-client.ndjson" ) ;
566+ const existingLog = "x" . repeat ( 1024 * 1024 ) ;
567+ if ( rotationBlocked ) {
568+ yield * fs . makeDirectory ( `${ logPath } .2` , { recursive : true } ) ;
569+ yield * fs . writeFileString ( `${ logPath } .2/blocked` , "blocks rotation" ) ;
570+ yield * fs . writeFileString ( logPath , existingLog ) ;
571+ }
572+ const warnings : unknown [ ] = [ ] ;
573+ const logger = Logger . make ( ( { message } ) => {
574+ if ( Array . isArray ( message ) && message [ 0 ] === "Relay client log unavailable" ) {
575+ warnings . push ( message ) ;
576+ }
577+ } ) ;
578+ const fixture = yield * makeTransportSpawner ( ) ;
579+ yield * Effect . gen ( function * ( ) {
580+ const context = yield * Layer . build (
581+ ManagedEndpointRuntime . layer . pipe (
582+ Layer . provide ( ServerConfig . layerTest ( process . cwd ( ) , baseDir ) ) ,
583+ Layer . provide ( runtimeDependencies ( fixture . spawner ) ) ,
584+ ) ,
585+ ) ;
586+ const runtime = yield * Effect . service (
587+ ManagedEndpointRuntime . CloudManagedEndpointRuntime ,
588+ ) . pipe ( Effect . provide ( context ) ) ;
589+ yield * runtime . applyConfig ( { ...tunnelConfig , connectorToken : "connector-secret" } ) ;
590+ yield * fixture . observe (
591+ runtime ,
592+ 0 ,
593+ '2026-10-16T20:29:40Z ERR Failed to serve tunnel connection error="rejected connector-secret" connIndex=0\n' ,
594+ ) ;
595+ } ) . pipe ( Effect . scoped , Effect . withLogger ( logger ) ) ;
596+
597+ const log = yield * fs . readFileString ( logPath ) ;
598+ if ( rotationBlocked ) {
599+ expect ( log . length ) . toBe ( existingLog . length ) ;
600+ expect ( warnings ) . toHaveLength ( 1 ) ;
601+ continue ;
602+ }
603+ const entries = log
604+ . trim ( )
605+ . split ( "\n" )
606+ . map ( ( line ) => decodeLogEntry ( line ) ) ;
607+ expect ( entries . map ( ( entry ) => [ entry . level , entry . message [ 0 ] ] ) ) . toEqual ( [
608+ [ "INFO" , "Relay client process started; waiting for tunnel connection" ] ,
609+ [ "WARN" , "Relay client reported a transport warning" ] ,
610+ [ "INFO" , "Relay client tunnel connection registered" ] ,
611+ [ "INFO" , "Relay client stopped" ] ,
612+ ] ) ;
613+ expect ( log ) . toContain ( "rejected <redacted>" ) ;
614+ expect ( log ) . not . toContain ( "connector-secret" ) ;
615+ const oversizedFixture = yield * makeTransportSpawner ( ) ;
616+ yield * Effect . gen ( function * ( ) {
617+ const context = yield * Layer . build (
618+ ManagedEndpointRuntime . layer . pipe (
619+ Layer . provide ( ServerConfig . layerTest ( process . cwd ( ) , baseDir ) ) ,
620+ Layer . provide ( runtimeDependencies ( oversizedFixture . spawner ) ) ,
621+ ) ,
622+ ) ;
623+ const runtime = yield * Effect . service (
624+ ManagedEndpointRuntime . CloudManagedEndpointRuntime ,
625+ ) . pipe ( Effect . provide ( context ) ) ;
626+ yield * runtime . applyConfig ( tunnelConfig ) ;
627+ yield * oversizedFixture . observe ( runtime , 0 , "ERR " + "界" . repeat ( 1024 * 1024 ) + "\n" ) ;
628+ } ) . pipe ( Effect . scoped ) ;
629+ const files = yield * fs . readDirectory ( path . dirname ( logPath ) ) ;
630+ const allEntries = [ ] ;
631+ for ( const file of files . filter ( ( name ) => name . startsWith ( "relay-client.ndjson" ) ) ) {
632+ const contents = yield * fs . readFileString ( path . join ( path . dirname ( logPath ) , file ) ) ;
633+ expect ( Buffer . byteLength ( contents ) ) . toBeLessThanOrEqual ( 1024 * 1024 ) ;
634+ allEntries . push (
635+ ...contents
636+ . trim ( )
637+ . split ( "\n" )
638+ . map ( ( line ) => decodeLogEntry ( line ) ) ,
639+ ) ;
640+ }
641+ expect (
642+ allEntries . some (
643+ ( entry ) => entry . message [ 0 ] === "Relay client log entry omitted: exceeds 1 MiB" ,
644+ ) ,
645+ ) . toBe ( true ) ;
646+ }
647+ } ) . pipe ( Effect . provide ( NodeServices . layer ) ) ,
648+ ) ;
649+
546650 it . effect ( "discards a pending fallback when its connector is replaced or removed" , ( ) =>
547651 Effect . gen ( function * ( ) {
548652 for ( const nextConfig of [ { ...tunnelConfig , connectorToken : "replacement" } , null ] ) {
0 commit comments