@@ -5,9 +5,11 @@ import * as Effect from "effect/Effect";
55import * as Exit from "effect/Exit" ;
66import * as Fiber from "effect/Fiber" ;
77import * as Layer from "effect/Layer" ;
8+ import * as Logger from "effect/Logger" ;
89import * as Option from "effect/Option" ;
910import * as PlatformError from "effect/PlatformError" ;
1011import * as Queue from "effect/Queue" ;
12+ import * as References from "effect/References" ;
1113import * as Sink from "effect/Sink" ;
1214import * as Stream from "effect/Stream" ;
1315import * as TestClock from "effect/testing/TestClock" ;
@@ -148,6 +150,28 @@ describe("CloudManagedEndpointRuntime", () => {
148150 ) . toBe ( "warning" ) ;
149151 } ) ;
150152
153+ it ( "classifies connection unregistration apart from registration and routine output" , ( ) => {
154+ expect (
155+ ManagedEndpointRuntime . classifyRelayClientOutput (
156+ "2026-10-06T00:00:00Z INF Unregistered tunnel connection connIndex=0 event=0 ip=198.41.200.23" ,
157+ ) ,
158+ ) . toBe ( "unregistered" ) ;
159+ for ( const level of [ "ERR" , "WRN" , "FTL" , "PNC" ] ) {
160+ expect (
161+ ManagedEndpointRuntime . classifyRelayClientOutput (
162+ `2026-10-06T00:00:00Z ${ level } Unregistered tunnel connection connIndex=0` ,
163+ ) ,
164+ ) . toBe ( "warning" ) ;
165+ }
166+ for ( const line of [
167+ "2026-10-06T00:00:00Z INF Initiating graceful shutdown due to signal terminated ..." ,
168+ "2026-10-06T00:00:00Z INF Starting metrics server on 127.0.0.1:20241/metrics" ,
169+ "2026-10-06T00:00:00Z DBG Unregistered tunnel connection connIndex=0" ,
170+ ] ) {
171+ expect ( ManagedEndpointRuntime . classifyRelayClientOutput ( line ) ) . toBe ( "debug" ) ;
172+ }
173+ } ) ;
174+
151175 it ( "recognizes tunnel authorization failures without matching ordinary transport errors" , ( ) => {
152176 expect (
153177 ManagedEndpointRuntime . isRejectedRelayClientTunnelOutput (
@@ -203,6 +227,83 @@ describe("CloudManagedEndpointRuntime", () => {
203227 } ) ,
204228 ) ;
205229
230+ it . effect ( "logs a connection unregistration at info with the connector token redacted" , ( ) => {
231+ const logs : Array < { readonly level : string ; readonly message : unknown } > = [ ] ;
232+ const unregistrationLogged = Deferred . makeUnsafe < void > ( ) ;
233+ const logger = Logger . make ( ( { logLevel, message } ) => {
234+ logs . push ( { level : logLevel , message } ) ;
235+ if ( Array . isArray ( message ) && message [ 0 ] === "Relay client tunnel connection unregistered" ) {
236+ Deferred . doneUnsafe ( unregistrationLogged , Effect . void ) ;
237+ }
238+ } ) ;
239+
240+ return Effect . gen ( function * ( ) {
241+ const config = {
242+ providerKind : "cloudflare_tunnel" as const ,
243+ connectorToken : "secret-connector-token" ,
244+ tunnelId : "tunnel-1" ,
245+ } ;
246+ const encoder = new TextEncoder ( ) ;
247+ const output = yield * Queue . unbounded < Uint8Array > ( ) ;
248+ const spawner = ChildProcessSpawner . make ( ( ) =>
249+ Effect . gen ( function * ( ) {
250+ const handle = makeHandle ( {
251+ pid : 701 ,
252+ onKill : ( ) => { } ,
253+ output : Stream . fromQueue ( output ) ,
254+ } ) ;
255+ yield * Effect . addFinalizer ( ( ) => handle . kill ( ) . pipe ( Effect . ignore ) ) ;
256+ return handle ;
257+ } ) ,
258+ ) ;
259+ const runtime = yield * buildCloudManagedEndpointRuntime ( spawner ) ;
260+ yield * runtime . applyConfig ( config ) ;
261+ yield * Queue . offer (
262+ output ,
263+ encoder . encode (
264+ "2026-10-06T00:00:00Z INF Unregistered tunnel connection connIndex=0 token=secret-connector-token\n" ,
265+ ) ,
266+ ) ;
267+ yield * Deferred . await ( unregistrationLogged ) ;
268+
269+ // An unregistration is not a registration, so the registration timeout still recovers.
270+ yield * TestClock . adjust ( Duration . minutes ( 3 ) ) ;
271+ expect ( Option . getOrNull ( yield * Stream . runHead ( runtime . recoveryRequests ) ) ) . toEqual ( config ) ;
272+
273+ yield * Queue . offer (
274+ output ,
275+ encoder . encode (
276+ "2026-10-06T00:00:01Z INF Starting metrics server on 127.0.0.1:20241/metrics\n" +
277+ "2026-10-06T00:00:01Z INF Registered tunnel connection connIndex=0\n" ,
278+ ) ,
279+ ) ;
280+ yield * Stream . runHead ( runtime . tunnelConnected ) ;
281+
282+ const relayLogs = logs . filter (
283+ ( { message } ) =>
284+ Array . isArray ( message ) && String ( message [ 0 ] ) . startsWith ( "Relay client tunnel" ) ,
285+ ) ;
286+ expect ( relayLogs . map ( ( { level, message } ) => [ level , ( message as unknown [ ] ) [ 0 ] ] ) ) . toEqual ( [
287+ [ "Info" , "Relay client tunnel connection unregistered" ] ,
288+ [ "Info" , "Relay client tunnel connection registered" ] ,
289+ ] ) ;
290+ expect ( ( relayLogs [ 0 ] ! . message as unknown [ ] ) [ 1 ] ) . toMatchObject ( {
291+ output :
292+ "2026-10-06T00:00:00Z INF Unregistered tunnel connection connIndex=0 token=<redacted>" ,
293+ } ) ;
294+ const logged = JSON . stringify ( logs . map ( ( { message } ) => message ) ) ;
295+ expect ( logged ) . not . toContain ( "secret-connector-token" ) ;
296+ expect ( logged ) . not . toContain ( "Starting metrics server" ) ;
297+ } ) . pipe (
298+ Effect . provide (
299+ Layer . merge (
300+ Logger . layer ( [ logger ] , { mergeWithExisting : false } ) ,
301+ Layer . succeed ( References . MinimumLogLevel , "Info" ) ,
302+ ) ,
303+ ) ,
304+ ) ;
305+ } ) ;
306+
206307 const tunnelConfig = {
207308 providerKind : "cloudflare_tunnel" as const ,
208309 connectorToken : "token" ,
0 commit comments