@devries-cameron20 / t3code Mirror · read-only
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 03:37 UTC

main
ts 1,532 lines 60.4 KB
Raw
1 import * as NodeCrypto from "@effect/platform-node/NodeCrypto";
2 import * as NodeServices from "@effect/platform-node/NodeServices";
3 import { describe, expect, it } from "@effect/vitest";
4 import * as HostProcess from "@t3tools/shared/HostProcess";
5 import * as ConfigProvider from "effect/ConfigProvider";
6 import * as Context from "effect/Context";
7 import * as Deferred from "effect/Deferred";
8 import * as Duration from "effect/Duration";
9 import * as Effect from "effect/Effect";
10 import * as Exit from "effect/Exit";
11 import * as Fiber from "effect/Fiber";
12 import * as FileSystem from "effect/FileSystem";
13 import * as Layer from "effect/Layer";
14 import * as Logger from "effect/Logger";
15 import * as Option from "effect/Option";
16 import * as Path from "effect/Path";
17 import * as PlatformError from "effect/PlatformError";
18 import * as Queue from "effect/Queue";
19 import * as References from "effect/References";
20 import * as Schema from "effect/Schema";
21 import * as Sink from "effect/Sink";
22 import * as Stream from "effect/Stream";
23 import * as TestClock from "effect/testing/TestClock";
24 import { ChildProcess, ChildProcessSpawner } from "effect/process";
25 import { HttpClient, HttpClientResponse } from "effect/http";
26 import type { RelayManagedEndpointRuntimeConfig } from "@t3tools/contracts/relay";
27 import * as RelayClient from "@t3tools/shared/relayClient";
28
29 import * as ServerSecretStore from "../auth/ServerSecretStore.ts";
30 import * as ServerConfig from "../config.ts";
31 import * as ManagedEndpointRuntime from "./ManagedEndpointRuntime.ts";
32 import * as ManagedPolicy from "../policy/ManagedPolicy.ts";
33 import * as RelayClientLedger from "./RelayClientLedger.ts";
34
35 const layerRelayClientAvailable = Layer.succeed(
36 RelayClient.RelayClient,
37 RelayClient.RelayClient.of({
38 resolve: Effect.succeed({
39 status: "available",
40 executablePath: "cloudflared",
41 source: "path",
42 version: RelayClient.CLOUDFLARED_VERSION,
43 }),
44 install: Effect.die("unused"),
45 installWithProgress: () => Effect.die("unused"),
46 pruneManagedVersions: Effect.void,
47 }),
48 );
49
50 const unusedHttpClient = HttpClient.make(() => Effect.die("unused"));
51
52 const decodeLogEntry = Schema.decodeUnknownSync(
53 Schema.fromJsonString(
54 Schema.Struct({
55 level: Schema.String,
56 message: Schema.Tuple([Schema.String, Schema.Unknown]),
57 }),
58 ),
59 );
60
61 const emptySecretStoreLayer = Layer.mock(ServerSecretStore.ServerSecretStore)({
62 get: () => Effect.succeedNone,
63 set: () => Effect.void,
64 });
65
66 const runtimeDependencies = (
67 spawner: ReturnType<typeof ChildProcessSpawner.make>,
68 relayClientLayer = layerRelayClientAvailable,
69 httpClient = unusedHttpClient,
70 secretStoreLayer = emptySecretStoreLayer,
71 ) =>
72 Layer.mergeAll(
73 ManagedPolicy.layerTest(),
74 Layer.succeed(ChildProcessSpawner.ChildProcessSpawner, spawner),
75 Layer.succeed(HttpClient.HttpClient, httpClient),
76 relayClientLayer,
77 secretStoreLayer,
78 RelayClientLedger.layerTest,
79 ServerConfig.layerTest(process.cwd(), { prefix: "t3-managed-endpoint-" }),
80 ).pipe(Layer.provideMerge(Layer.mergeAll(NodeServices.layer, NodeCrypto.layer)));
81
82 const buildCloudManagedEndpointRuntime = (
83 spawner: ReturnType<typeof ChildProcessSpawner.make>,
84 relayClientLayer = layerRelayClientAvailable,
85 httpClient = unusedHttpClient,
86 secretStoreLayer = emptySecretStoreLayer,
87 env: Record<string, string> = {},
88 ) =>
89 Effect.gen(function* () {
90 const context = yield* Layer.build(
91 ManagedEndpointRuntime.layer.pipe(
92 Layer.provide(runtimeDependencies(spawner, relayClientLayer, httpClient, secretStoreLayer)),
93 ),
94 );
95 return yield* Effect.service(ManagedEndpointRuntime.CloudManagedEndpointRuntime).pipe(
96 Effect.provide(context),
97 );
98 }).pipe(
99 Effect.provideService(HostProcess.Environment, env),
100 Effect.provide(ConfigProvider.layer(ConfigProvider.fromEnv({ env }))),
101 );
102
103 function makeHandle(input: {
104 readonly pid: number;
105 readonly onKill: () => void;
106 readonly isRunning?: () => boolean;
107 readonly exitCode?: Effect.Effect<ChildProcessSpawner.ExitCode>;
108 readonly output?: Stream.Stream<Uint8Array>;
109 }) {
110 return ChildProcessSpawner.makeHandle({
111 pid: ChildProcessSpawner.ProcessId(input.pid),
112 exitCode: input.exitCode ?? Effect.never,
113 isRunning: Effect.sync(() => input.isRunning?.() ?? true),
114 kill: () =>
115 Effect.sync(() => {
116 input.onKill();
117 }),
118 unref: Effect.succeed(Effect.void),
119 stdin: Sink.drain,
120 stdout: Stream.empty,
121 stderr: Stream.empty,
122 all: input.output ?? Stream.empty,
123 getInputFd: () => Sink.drain,
124 getOutputFd: () => Stream.empty,
125 });
126 }
127
128 describe("CloudManagedEndpointRuntime", () => {
129 it.effect("serializes updates to persisted cloud link state", () =>
130 Effect.gen(function* () {
131 const firstEntered = yield* Deferred.make<void>();
132 const releaseFirst = yield* Deferred.make<void>();
133 const secondEntered = yield* Deferred.make<void>();
134 const runtime = yield* buildCloudManagedEndpointRuntime(
135 ChildProcessSpawner.make(() => Effect.die("unused")),
136 );
137
138 const first = yield* runtime
139 .withLinkStateLock(
140 Deferred.succeed(firstEntered, undefined).pipe(
141 Effect.andThen(Deferred.await(releaseFirst)),
142 ),
143 )
144 .pipe(Effect.forkChild);
145 yield* Deferred.await(firstEntered);
146
147 const second = yield* runtime
148 .withLinkStateLock(Deferred.succeed(secondEntered, undefined))
149 .pipe(Effect.forkChild);
150 expect(yield* Deferred.isDone(secondEntered)).toBe(false);
151
152 yield* Deferred.succeed(releaseFirst, undefined);
153 yield* Fiber.join(first);
154 yield* Fiber.join(second);
155 expect(yield* Deferred.isDone(secondEntered)).toBe(true);
156 }),
157 );
158
159 it("classifies Cloudflare connection and warning output", () => {
160 expect(
161 ManagedEndpointRuntime.classifyRelayClientOutput(
162 "2026-06-17T02:00:00Z INF Registered tunnel connection connIndex=0",
163 ),
164 ).toBe("connected");
165 expect(
166 ManagedEndpointRuntime.classifyRelayClientOutput(
167 "2026-06-17T02:00:00Z ERR Failed to serve tunnel connection",
168 ),
169 ).toBe("warning");
170 expect(
171 ManagedEndpointRuntime.classifyRelayClientOutput(
172 "2026-06-17T02:00:00Z INF Starting metrics server",
173 ),
174 ).toBe("debug");
175 expect(
176 ManagedEndpointRuntime.classifyRelayClientOutput(
177 "2026-06-17T02:00:00Z FTL Cannot determine default origin certificate path",
178 ),
179 ).toBe("warning");
180 expect(
181 ManagedEndpointRuntime.classifyRelayClientOutput("2026-06-17T02:00:00Z PNC runtime panic"),
182 ).toBe("warning");
183 for (const prefix of ["", "Request failed "]) {
184 expect(
185 ManagedEndpointRuntime.classifyRelayClientOutput(
186 `2026-06-17T02:00:00Z ERR ${prefix}error="Incoming request ended abruptly: context canceled" connIndex=0`,
187 ),
188 ).toBe("debug");
189 }
190 for (const line of [
191 '2026-06-17T02:00:00Z ERR error="Incoming request ended abruptly: context deadline exceeded" connIndex=0',
192 '2026-06-17T02:00:00Z ERR Failed to serve tunnel connection error="context canceled"',
193 '2026-06-17T02:00:00Z FTL error="Incoming request ended abruptly: context canceled"',
194 '2026-06-17T02:00:00Z PNC error="Incoming request ended abruptly: context canceled"',
195 ]) {
196 expect(ManagedEndpointRuntime.classifyRelayClientOutput(line)).toBe("warning");
197 }
198 });
199
200 it("classifies connection unregistration apart from registration and routine output", () => {
201 expect(
202 ManagedEndpointRuntime.classifyRelayClientOutput(
203 "2026-10-06T00:00:00Z INF Unregistered tunnel connection connIndex=0 event=0 ip=198.41.200.23",
204 ),
205 ).toBe("unregistered");
206 for (const level of ["ERR", "WRN", "FTL", "PNC"]) {
207 expect(
208 ManagedEndpointRuntime.classifyRelayClientOutput(
209 `2026-10-06T00:00:00Z ${level} Unregistered tunnel connection connIndex=0`,
210 ),
211 ).toBe("warning");
212 }
213 for (const line of [
214 "2026-10-06T00:00:00Z INF Initiating graceful shutdown due to signal terminated ...",
215 "2026-10-06T00:00:00Z INF Starting metrics server on 127.0.0.1:20241/metrics",
216 "2026-10-06T00:00:00Z DBG Unregistered tunnel connection connIndex=0",
217 ]) {
218 expect(ManagedEndpointRuntime.classifyRelayClientOutput(line)).toBe("debug");
219 }
220 });
221
222 it("recognizes tunnel authorization failures without matching ordinary transport errors", () => {
223 expect(
224 ManagedEndpointRuntime.isRejectedRelayClientTunnelOutput(
225 '2026-09-15T06:30:43Z ERR Register tunnel error from server side error="Failed to get tunnel" connIndex=0 event=0 ip=198.41.200.23',
226 ),
227 ).toBe(true);
228 expect(
229 ManagedEndpointRuntime.isRejectedRelayClientTunnelOutput(
230 '2026-06-17T02:00:00Z ERR Register tunnel error from server side error="Unauthorized: Record for tunnel not found" connIndex=0',
231 ),
232 ).toBe(true);
233 expect(
234 ManagedEndpointRuntime.isRejectedRelayClientTunnelOutput(
235 '2026-06-17T02:00:00Z ERR Register tunnel error from server side error="Unauthorized: Invalid tunnel secret" connIndex=0',
236 ),
237 ).toBe(true);
238 expect(
239 ManagedEndpointRuntime.isRejectedRelayClientTunnelOutput(
240 '2026-10-06T12:00:00Z ERR Register tunnel error from server side error="Unauthorized: Tunnel not found" connIndex=0 event=0 ip=198.41.200.23',
241 ),
242 ).toBe(true);
243 expect(
244 ManagedEndpointRuntime.isRejectedRelayClientTunnelOutput(
245 '2026-10-06T12:00:00Z ERR Register tunnel error from server side error="Tunnel not found" connIndex=0',
246 ),
247 ).toBe(true);
248 expect(
249 ManagedEndpointRuntime.isRejectedRelayClientTunnelOutput(
250 '2026-06-17T02:00:00Z ERR Register tunnel error from server side error="connection timed out" connIndex=0',
251 ),
252 ).toBe(false);
253 expect(
254 ManagedEndpointRuntime.isRejectedRelayClientTunnelOutput(
255 '2026-06-17T02:00:00Z ERR Failed to serve tunnel connection error="Unauthorized: Tunnel not found" connIndex=0',
256 ),
257 ).toBe(false);
258 });
259
260 it.effect("keeps recovery requests sent before the server starts consuming them", () =>
261 Effect.gen(function* () {
262 const runtime = yield* buildCloudManagedEndpointRuntime(
263 ChildProcessSpawner.make(() => Effect.die("unused")),
264 );
265 const config = {
266 providerKind: "cloudflare_tunnel" as const,
267 connectorToken: "token",
268 tunnelId: "tunnel-1",
269 };
270
271 yield* runtime.requestRecovery(config);
272
273 expect(Option.getOrNull(yield* Stream.runHead(runtime.recoveryRequests))).toEqual(config);
274 }),
275 );
276
277 it.effect("reads the public hostname and its origin from cloudflared's remote config", () =>
278 Effect.gen(function* () {
279 let ingress: unknown = [
280 { hostname: "env.example.t3.codes", service: "http://127.0.0.1:3773" },
281 { service: "http_status:404" },
282 ];
283 const httpClient = HttpClient.make((request) =>
284 Effect.succeed(
285 HttpClientResponse.fromWeb(
286 request,
287 Response.json(
288 request.url.endsWith("/config")
289 ? { config: ingress === undefined ? {} : { ingress } }
290 : { readyConnections: 4 },
291 ),
292 ),
293 ),
294 );
295 const output = yield* Queue.unbounded<Uint8Array>();
296 const spawner = ChildProcessSpawner.make(() =>
297 Effect.gen(function* () {
298 const handle = makeHandle({
299 pid: 702,
300 onKill: () => {},
301 output: Stream.fromQueue(output),
302 });
303 yield* Effect.addFinalizer(() => handle.kill().pipe(Effect.ignore));
304 return handle;
305 }),
306 );
307 const runtime = yield* buildCloudManagedEndpointRuntime(
308 spawner,
309 layerRelayClientAvailable,
310 httpClient,
311 );
312 yield* runtime.applyConfig({
313 providerKind: "cloudflare_tunnel",
314 connectorToken: "connector-token",
315 tunnelId: "tunnel-1",
316 });
317 yield* Queue.offer(
318 output,
319 new TextEncoder().encode(
320 "2026-10-06T00:00:01Z INF Starting metrics server on 127.0.0.1:20241/metrics\n" +
321 "2026-10-06T00:00:01Z INF Registered tunnel connection connIndex=0\n",
322 ),
323 );
324 yield* Stream.runHead(runtime.tunnelConnected);
325
326 expect(yield* runtime.inspectConnector).toMatchObject({
327 readyConnections: 4,
328 hostname: "env.example.t3.codes",
329 origin: "http://127.0.0.1:3773",
330 });
331 // Before the edge pushes a config, the origin is unknown rather than wrong.
332 for (const missing of [null, undefined]) {
333 ingress = missing;
334 expect(yield* runtime.inspectConnector).toMatchObject({ hostname: null, origin: null });
335 }
336 }),
337 );
338
339 it.effect("logs a connection unregistration at info with the connector token redacted", () => {
340 const logs: Array<{ readonly level: string; readonly message: unknown }> = [];
341 const unregistrationLogged = Deferred.makeUnsafe<void>();
342 const logger = Logger.make(({ logLevel, message }) => {
343 logs.push({ level: logLevel, message });
344 if (Array.isArray(message) && message[0] === "Relay client tunnel connection unregistered") {
345 Deferred.doneUnsafe(unregistrationLogged, Effect.void);
346 }
347 });
348
349 return Effect.gen(function* () {
350 const config = {
351 providerKind: "cloudflare_tunnel" as const,
352 connectorToken: "secret-connector-token",
353 tunnelId: "tunnel-1",
354 };
355 const encoder = new TextEncoder();
356 const output = yield* Queue.unbounded<Uint8Array>();
357 const spawner = ChildProcessSpawner.make(() =>
358 Effect.gen(function* () {
359 const handle = makeHandle({
360 pid: 701,
361 onKill: () => {},
362 output: Stream.fromQueue(output),
363 });
364 yield* Effect.addFinalizer(() => handle.kill().pipe(Effect.ignore));
365 return handle;
366 }),
367 );
368 const runtime = yield* buildCloudManagedEndpointRuntime(spawner);
369 yield* runtime.applyConfig(config);
370 yield* Queue.offer(
371 output,
372 encoder.encode(
373 "2026-10-06T00:00:00Z INF Unregistered tunnel connection connIndex=0 token=secret-connector-token\n",
374 ),
375 );
376 yield* Deferred.await(unregistrationLogged);
377
378 // An unregistration is not a registration, so the registration timeout still recovers.
379 yield* TestClock.adjust(Duration.minutes(3));
380 expect(Option.getOrNull(yield* Stream.runHead(runtime.recoveryRequests))).toEqual(config);
381
382 yield* Queue.offer(
383 output,
384 encoder.encode(
385 "2026-10-06T00:00:01Z INF Starting metrics server on 127.0.0.1:20241/metrics\n" +
386 "2026-10-06T00:00:01Z INF Registered tunnel connection connIndex=0\n",
387 ),
388 );
389 yield* Stream.runHead(runtime.tunnelConnected);
390
391 const relayLogs = logs.filter(
392 ({ message }) =>
393 Array.isArray(message) && String(message[0]).startsWith("Relay client tunnel"),
394 );
395 expect(relayLogs.map(({ level, message }) => [level, (message as unknown[])[0]])).toEqual([
396 ["Info", "Relay client tunnel connection unregistered"],
397 ["Info", "Relay client tunnel connection registered"],
398 ]);
399 expect((relayLogs[0]!.message as unknown[])[1]).toMatchObject({
400 output:
401 "2026-10-06T00:00:00Z INF Unregistered tunnel connection connIndex=0 token=<redacted>",
402 });
403 const logged = JSON.stringify(logs.map(({ message }) => message));
404 expect(logged).not.toContain("secret-connector-token");
405 expect(logged).not.toContain("Starting metrics server");
406 }).pipe(
407 Effect.provide(
408 Layer.merge(
409 Logger.layer([logger], { mergeWithExisting: false }),
410 Layer.succeed(References.MinimumLogLevel, "Info"),
411 ),
412 ),
413 );
414 });
415
416 const tunnelConfig = {
417 providerKind: "cloudflare_tunnel" as const,
418 connectorToken: "token",
419 tunnelId: "tunnel-1",
420 };
421 const quicDialWarning =
422 '2026-10-16T20:29:43Z ERR Failed to dial a quic connection error="failed to dial to edge with quic: timeout: no recent network activity" connIndex=1\n';
423 const quicDatagramWarning =
424 '2026-10-16T20:29:41Z ERR Connection terminated error="datagram manager error: timeout: no recent network activity" connIndex=1\n';
425
426 const makeTransportSpawner = Effect.fnUntraced(function* (
427 options: {
428 transport?: string;
429 failSpawn?: number;
430 isRunning?: Effect.Effect<boolean>;
431 } = {},
432 ) {
433 const spawned: Array<ChildProcess.StandardCommand> = [];
434 const killed: Array<number> = [];
435 const outputs: Array<Queue.Queue<Uint8Array>> = [];
436 const exits: Array<Deferred.Deferred<ChildProcessSpawner.ExitCode>> = [];
437 const started = yield* Queue.unbounded<number>();
438 const spawner = ChildProcessSpawner.make((command) =>
439 Effect.gen(function* () {
440 if (!ChildProcess.isStandardCommand(command)) {
441 return yield* Effect.die("Expected standard command.");
442 }
443 const index = spawned.length;
444 spawned.push(command);
445 if (index === options.failSpawn) {
446 return yield* PlatformError.systemError({
447 _tag: "NotFound",
448 module: "ChildProcess",
449 method: "spawn",
450 description: "cloudflared temporarily unavailable",
451 });
452 }
453 const output = yield* Queue.unbounded<Uint8Array>();
454 const exit = yield* Deferred.make<ChildProcessSpawner.ExitCode>();
455 outputs[index] = output;
456 exits[index] = exit;
457 const handle = ChildProcessSpawner.makeHandle({
458 ...makeHandle({
459 pid: 800 + index,
460 exitCode: Deferred.await(exit),
461 output: Stream.fromQueue(output),
462 onKill: () => {
463 killed.push(800 + index);
464 Deferred.doneUnsafe(exit, Exit.succeed(ChildProcessSpawner.ExitCode(143)));
465 },
466 }),
467 isRunning: options.isRunning ?? Effect.succeed(true),
468 });
469 yield* Effect.addFinalizer(() => handle.kill().pipe(Effect.ignore));
470 yield* Queue.offer(started, index);
471 return handle;
472 }),
473 );
474 const emit = (index: number, lines: string) =>
475 Queue.offer(outputs[index]!, new TextEncoder().encode(lines));
476 const observe = (
477 runtime: ManagedEndpointRuntime.CloudManagedEndpointRuntime["Service"],
478 index: number,
479 lines = "",
480 ) =>
481 emit(
482 index,
483 lines + "2026-10-16T20:30:00Z INF Registered tunnel connection connIndex=0\n",
484 ).pipe(Effect.andThen(Stream.runHead(runtime.tunnelConnected)));
485 const env: Record<string, string> =
486 options.transport === undefined ? {} : { TUNNEL_TRANSPORT_PROTOCOL: options.transport };
487 return { spawner, env, spawned, killed, exits, started, emit, observe };
488 });
489
490 it.effect("restarts only the inspected connector and supervises its replacement", () =>
491 Effect.gen(function* () {
492 const fixture = yield* makeTransportSpawner();
493 const runtime = yield* buildCloudManagedEndpointRuntime(fixture.spawner);
494 expect(yield* runtime.restartConnector(800, Effect.succeed(true))).toBe(false);
495 yield* runtime.applyConfig(tunnelConfig);
496 yield* fixture.observe(runtime, 0);
497 expect(yield* runtime.inspectConnector).toMatchObject({ pid: 800 });
498 expect(yield* runtime.restartConnector(799, Effect.succeed(true))).toBe(false);
499 // A newer check saw the tunnel recover while this one waited for the lock.
500 expect(yield* runtime.restartConnector(800, Effect.succeed(false))).toBe(false);
501 expect(fixture.killed).toEqual([]);
502 expect(yield* runtime.restartConnector(800, Effect.succeed(true))).toBe(true);
503 expect(yield* Queue.take(fixture.started)).toBe(0);
504 expect(yield* Queue.take(fixture.started)).toBe(1);
505 yield* fixture.observe(runtime, 1);
506 expect(fixture.killed).toContain(800);
507 expect(yield* runtime.restartConnector(800, Effect.succeed(true))).toBe(false);
508 expect(yield* runtime.inspectConnector).toMatchObject({ pid: 801 });
509 }),
510 );
511
512 it.effect("still owns a relay client when activation is interrupted while recording it", () =>
513 Effect.gen(function* () {
514 const fixture = yield* makeTransportSpawner();
515 const recording = yield* Deferred.make<void>();
516 const recorded = yield* Deferred.make<void>();
517 const ledger = Layer.succeed(
518 RelayClientLedger.RelayClientLedger,
519 RelayClientLedger.RelayClientLedger.of({
520 track: () =>
521 Deferred.succeed(recording, undefined).pipe(
522 Effect.andThen(Deferred.await(recorded)),
523 Effect.as(Effect.void),
524 ),
525 }),
526 );
527 const context = yield* Layer.build(
528 ManagedEndpointRuntime.layer.pipe(
529 Layer.provide(Layer.merge(runtimeDependencies(fixture.spawner), ledger)),
530 ),
531 );
532 const runtime = yield* Effect.service(
533 ManagedEndpointRuntime.CloudManagedEndpointRuntime,
534 ).pipe(Effect.provide(context));
535
536 const activation = yield* Effect.forkChild(runtime.applyConfig(tunnelConfig));
537 yield* Deferred.await(recording);
538 const interrupting = yield* Effect.forkChild(Fiber.interrupt(activation), {
539 startImmediately: true,
540 });
541 yield* Deferred.succeed(recorded, undefined);
542 yield* Fiber.join(interrupting);
543
544 // The handed-over connector is supervised: its exit starts a replacement.
545 expect(yield* Queue.take(fixture.started)).toBe(0);
546 yield* Deferred.succeed(fixture.exits[0]!, ChildProcessSpawner.ExitCode(1));
547 expect(yield* Queue.take(fixture.started)).toBe(1);
548
549 yield* runtime.applyConfig(null);
550 expect(fixture.killed).toEqual([800, 801]);
551 }).pipe(
552 Effect.provideService(HostProcess.Environment, {}),
553 Effect.provide(ConfigProvider.layer(ConfigProvider.fromEnv({ env: {} }))),
554 ),
555 );
556
557 it.effect("preserves an explicit transport choice despite repeated QUIC stalls", () =>
558 Effect.gen(function* () {
559 const fixture = yield* makeTransportSpawner({ transport: "quic" });
560 const runtime = yield* buildCloudManagedEndpointRuntime(
561 fixture.spawner,
562 layerRelayClientAvailable,
563 unusedHttpClient,
564 emptySecretStoreLayer,
565 fixture.env,
566 );
567 yield* runtime.applyConfig(tunnelConfig);
568 yield* fixture.observe(runtime, 0, quicDialWarning.repeat(30));
569 yield* fixture.observe(runtime, 0, quicDatagramWarning.repeat(30));
570 expect(fixture.spawned).toHaveLength(1);
571 expect(fixture.killed).toEqual([]);
572 expect(fixture.spawned[0]!.args).not.toContain("--protocol");
573 expect(fixture.spawned[0]!.options.env?.TUNNEL_TRANSPORT_PROTOCOL).toBe("quic");
574 }),
575 );
576
577 it.effect(
578 "retains automatic HTTP/2 fallback across restarts until expiry and respects overrides",
579 () =>
580 Effect.gen(function* () {
581 const context = yield* Layer.build(
582 ServerSecretStore.layer.pipe(
583 Layer.provide(ServerConfig.layerTest(process.cwd(), { prefix: "t3-transport-test-" })),
584 Layer.provide(Layer.mergeAll(NodeServices.layer, NodeCrypto.layer)),
585 ),
586 );
587 const secretStoreLayer = Layer.succeed(
588 ServerSecretStore.ServerSecretStore,
589 context.pipe(Context.get(ServerSecretStore.ServerSecretStore)),
590 );
591 const fixture = yield* makeTransportSpawner();
592 const start = (spawner = fixture.spawner, env = fixture.env) =>
593 buildCloudManagedEndpointRuntime(
594 spawner,
595 layerRelayClientAvailable,
596 unusedHttpClient,
597 secretStoreLayer,
598 env,
599 );
600 yield* Effect.gen(function* () {
601 const runtime = yield* start();
602 yield* runtime.applyConfig(tunnelConfig);
603 expect(yield* Queue.take(fixture.started)).toBe(0);
604 expect(fixture.spawned[0]!.args).not.toContain("--protocol");
605 yield* fixture.emit(0, quicDialWarning.repeat(30));
606 expect(yield* Queue.take(fixture.started)).toBe(1);
607 yield* fixture.observe(runtime, 1);
608 expect(fixture.spawned[1]!.args.slice(-3)).toEqual(["--protocol", "http2", "run"]);
609 }).pipe(Effect.scoped);
610
611 yield* TestClock.adjust(Duration.hours(1));
612 for (const transport of [undefined, "quic", "http2", "auto", undefined]) {
613 yield* Effect.gen(function* () {
614 const next = yield* makeTransportSpawner(transport ? { transport } : {});
615 const restarted = yield* start(next.spawner, next.env);
616 yield* restarted.applyConfig(tunnelConfig);
617 if (transport === undefined) {
618 expect(next.spawned[0]!.args.slice(-3)).toEqual(["--protocol", "http2", "run"]);
619 } else {
620 expect(next.spawned[0]!.args).not.toContain("--protocol");
621 expect(next.spawned[0]!.options.env?.TUNNEL_TRANSPORT_PROTOCOL).toBe(transport);
622 yield* next.observe(restarted, 0, quicDialWarning.repeat(30));
623 expect(next.spawned).toHaveLength(1);
624 }
625 }).pipe(Effect.scoped);
626 }
627
628 // Reusing the hint one hour later must not extend its original lifetime.
629 yield* TestClock.adjust(Duration.hours(23));
630 yield* Effect.gen(function* () {
631 const next = yield* makeTransportSpawner();
632 const restarted = yield* start(next.spawner);
633 yield* restarted.applyConfig(tunnelConfig);
634 expect(next.spawned[0]!.args).not.toContain("--protocol");
635 expect(yield* Queue.take(next.started)).toBe(0);
636 yield* next.emit(0, quicDatagramWarning.repeat(30));
637 expect(yield* Queue.take(next.started)).toBe(1);
638 expect(next.spawned[1]!.args.slice(-3)).toEqual(["--protocol", "http2", "run"]);
639 }).pipe(Effect.scoped);
640 }),
641 );
642
643 it.effect("remembers an HTTP/2 fallback the relay client made on its own", () =>
644 Effect.gen(function* () {
645 const context = yield* Layer.build(
646 ServerSecretStore.layer.pipe(
647 Layer.provide(ServerConfig.layerTest(process.cwd(), { prefix: "t3-transport-test-" })),
648 Layer.provide(Layer.mergeAll(NodeServices.layer, NodeCrypto.layer)),
649 ),
650 );
651 const secretStoreLayer = Layer.succeed(
652 ServerSecretStore.ServerSecretStore,
653 context.pipe(Context.get(ServerSecretStore.ServerSecretStore)),
654 );
655 const start = (spawner: ReturnType<typeof ChildProcessSpawner.make>) =>
656 buildCloudManagedEndpointRuntime(
657 spawner,
658 layerRelayClientAvailable,
659 unusedHttpClient,
660 secretStoreLayer,
661 );
662 const registered = (protocol: string) =>
663 `2026-11-10T01:43:17Z INF Registered tunnel connection connIndex=0 connection=c1 event=0 ip=198.41.192.7 location=fra08 protocol=${protocol}\n`;
664
665 yield* Effect.gen(function* () {
666 const fixture = yield* makeTransportSpawner();
667 const runtime = yield* start(fixture.spawner);
668 yield* runtime.applyConfig(tunnelConfig);
669 yield* fixture.emit(0, registered("quic"));
670 yield* Stream.runHead(runtime.tunnelConnected);
671 }).pipe(Effect.scoped);
672 yield* Effect.gen(function* () {
673 const fixture = yield* makeTransportSpawner();
674 const runtime = yield* start(fixture.spawner);
675 yield* runtime.applyConfig(tunnelConfig);
676 expect(fixture.spawned[0]!.args).not.toContain("--protocol");
677 // cloudflared, started with `auto`, gave up on QUIC itself.
678 yield* fixture.emit(0, registered("http2"));
679 yield* Stream.runHead(runtime.tunnelConnected);
680 // The connector already runs on HTTP/2, so it is not restarted.
681 expect(fixture.spawned).toHaveLength(1);
682 expect(fixture.killed).toEqual([]);
683 }).pipe(Effect.scoped);
684
685 yield* Effect.gen(function* () {
686 const fixture = yield* makeTransportSpawner();
687 const runtime = yield* start(fixture.spawner);
688 yield* runtime.applyConfig(tunnelConfig);
689 expect(fixture.spawned[0]!.args.slice(-3)).toEqual(["--protocol", "http2", "run"]);
690 }).pipe(Effect.scoped);
691 }),
692 );
693
694 it.effect(
695 "ignores invalid or unreadable fallback state and still falls back if persistence fails",
696 () =>
697 Effect.gen(function* () {
698 for (const cached of [
699 "not json",
700 '{"fellBackAt":"invalid"}',
701 '{"fellBackAt":9007199254740991}',
702 null,
703 ]) {
704 yield* Effect.gen(function* () {
705 const fixture = yield* makeTransportSpawner();
706 const runtime = yield* buildCloudManagedEndpointRuntime(
707 fixture.spawner,
708 layerRelayClientAvailable,
709 unusedHttpClient,
710 Layer.mock(ServerSecretStore.ServerSecretStore)({
711 get: () =>
712 cached === null
713 ? Effect.fail(
714 new ServerSecretStore.SecretStoreReadError({
715 resource: "test",
716 cause: "unreadable",
717 }),
718 )
719 : Effect.succeedSome(new TextEncoder().encode(cached)),
720 set: () =>
721 Effect.fail(
722 new ServerSecretStore.SecretStorePersistError({
723 resource: "test",
724 cause: "read-only",
725 }),
726 ),
727 }),
728 );
729 yield* runtime.applyConfig(tunnelConfig);
730 expect(yield* Queue.take(fixture.started)).toBe(0);
731 expect(fixture.spawned[0]!.args).not.toContain("--protocol");
732 yield* fixture.emit(0, quicDialWarning.repeat(30));
733 expect(yield* Queue.take(fixture.started)).toBe(1);
734 expect(fixture.spawned[1]!.args.slice(-3)).toEqual(["--protocol", "http2", "run"]);
735 }).pipe(Effect.scoped);
736 }
737 }),
738 );
739
740 it.effect("requests recovery after a failed HTTP/2 spawn and retries with HTTP/2", () =>
741 Effect.gen(function* () {
742 const fixture = yield* makeTransportSpawner({ failSpawn: 1 });
743 const runtime = yield* buildCloudManagedEndpointRuntime(fixture.spawner);
744 yield* runtime.applyConfig(tunnelConfig);
745 expect(yield* Queue.take(fixture.started)).toBe(0);
746 yield* fixture.emit(0, quicDialWarning.repeat(30));
747
748 const requested = Option.getOrThrow(yield* Stream.runHead(runtime.recoveryRequests));
749 expect(requested).toEqual(tunnelConfig);
750 expect(fixture.killed).toEqual([800]);
751 expect(fixture.spawned).toHaveLength(2);
752 expect(fixture.spawned[1]!.args.slice(-3)).toEqual(["--protocol", "http2", "run"]);
753 expect(yield* runtime.applyConfig(requested)).toMatchObject({ status: "running", pid: 802 });
754 yield* fixture.observe(runtime, 2);
755 expect(fixture.spawned[2]!.args.slice(-3)).toEqual(["--protocol", "http2", "run"]);
756 }),
757 );
758
759 it.effect("decides a restart only once it holds the reconcile lock", () =>
760 Effect.gen(function* () {
761 const checking = yield* Deferred.make<void>();
762 const releaseCheck = yield* Deferred.make<void>();
763 const fixture = yield* makeTransportSpawner({
764 isRunning: Deferred.succeed(checking, undefined).pipe(
765 Effect.andThen(Deferred.await(releaseCheck)),
766 Effect.as(true),
767 ),
768 });
769 const runtime = yield* buildCloudManagedEndpointRuntime(fixture.spawner);
770 yield* runtime.applyConfig(tunnelConfig);
771 yield* fixture.observe(runtime, 0);
772
773 // Hold reconciliation in a process probe so the restart waits for the lock.
774 const probe = yield* runtime.applyConfig(tunnelConfig).pipe(Effect.forkChild);
775 yield* Deferred.await(checking);
776 let stillDead = true;
777 const restart = yield* runtime
778 .restartConnector(
779 800,
780 Effect.sync(() => stillDead),
781 )
782 .pipe(Effect.forkChild({ startImmediately: true }));
783 stillDead = false;
784 yield* Deferred.succeed(releaseCheck, undefined);
785 yield* Fiber.join(probe);
786
787 expect(yield* Fiber.join(restart)).toBe(false);
788 expect(fixture.killed).toEqual([]);
789 }),
790 );
791
792 it.effect("keeps redacted connector logs bounded even when rotation fails", () =>
793 Effect.gen(function* () {
794 const fs = yield* FileSystem.FileSystem;
795 const path = yield* Path.Path;
796 for (const rotationBlocked of [false, true]) {
797 const baseDir = yield* fs.makeTempDirectoryScoped({ prefix: "t3-relay-client-log-" });
798 const logPath = path.join(baseDir, "userdata", "logs", "relay-client.ndjson");
799 const existingLog = "x".repeat(1024 * 1024);
800 if (rotationBlocked) {
801 yield* fs.makeDirectory(`${logPath}.2`, { recursive: true });
802 yield* fs.writeFileString(`${logPath}.2/blocked`, "blocks rotation");
803 yield* fs.writeFileString(logPath, existingLog);
804 }
805 const warnings: unknown[] = [];
806 const logger = Logger.make(({ message }) => {
807 if (Array.isArray(message) && message[0] === "Relay client log unavailable") {
808 warnings.push(message);
809 }
810 });
811 const fixture = yield* makeTransportSpawner();
812 yield* Effect.gen(function* () {
813 const context = yield* Layer.build(
814 ManagedEndpointRuntime.layer.pipe(
815 Layer.provide(ServerConfig.layerTest(process.cwd(), baseDir)),
816 Layer.provide(runtimeDependencies(fixture.spawner)),
817 ),
818 );
819 const runtime = yield* Effect.service(
820 ManagedEndpointRuntime.CloudManagedEndpointRuntime,
821 ).pipe(Effect.provide(context));
822 yield* runtime.applyConfig({ ...tunnelConfig, connectorToken: "connector-secret" });
823 yield* fixture.observe(
824 runtime,
825 0,
826 '2026-10-16T20:29:40Z ERR Failed to serve tunnel connection error="rejected connector-secret" connIndex=0\n',
827 );
828 }).pipe(Effect.scoped, Effect.withLogger(logger));
829
830 const log = yield* fs.readFileString(logPath);
831 if (rotationBlocked) {
832 expect(log.length).toBe(existingLog.length);
833 expect(warnings).toHaveLength(1);
834 continue;
835 }
836 const entries = log
837 .trim()
838 .split("\n")
839 .map((line) => decodeLogEntry(line));
840 expect(entries.map((entry) => [entry.level, entry.message[0]])).toEqual([
841 ["INFO", "Relay client process started; waiting for tunnel connection"],
842 ["WARN", "Relay client reported a transport warning"],
843 ["INFO", "Relay client tunnel connection registered"],
844 ["INFO", "Relay client stopped"],
845 ]);
846 expect(log).toContain("rejected <redacted>");
847 expect(log).not.toContain("connector-secret");
848 const oversizedFixture = yield* makeTransportSpawner();
849 yield* Effect.gen(function* () {
850 const context = yield* Layer.build(
851 ManagedEndpointRuntime.layer.pipe(
852 Layer.provide(ServerConfig.layerTest(process.cwd(), baseDir)),
853 Layer.provide(runtimeDependencies(oversizedFixture.spawner)),
854 ),
855 );
856 const runtime = yield* Effect.service(
857 ManagedEndpointRuntime.CloudManagedEndpointRuntime,
858 ).pipe(Effect.provide(context));
859 yield* runtime.applyConfig(tunnelConfig);
860 yield* oversizedFixture.observe(runtime, 0, "ERR " + "界".repeat(1024 * 1024) + "\n");
861 }).pipe(Effect.scoped);
862 const files = yield* fs.readDirectory(path.dirname(logPath));
863 const allEntries = [];
864 for (const file of files.filter((name) => name.startsWith("relay-client.ndjson"))) {
865 const contents = yield* fs.readFileString(path.join(path.dirname(logPath), file));
866 expect(Buffer.byteLength(contents)).toBeLessThanOrEqual(1024 * 1024);
867 allEntries.push(
868 ...contents
869 .trim()
870 .split("\n")
871 .map((line) => decodeLogEntry(line)),
872 );
873 }
874 expect(
875 allEntries.some(
876 (entry) => entry.message[0] === "Relay client log entry omitted: exceeds 1 MiB",
877 ),
878 ).toBe(true);
879 }
880 }).pipe(Effect.provide(Layer.mergeAll(NodeServices.layer, NodeCrypto.layer))),
881 );
882
883 it.effect("discards a pending fallback when its connector is replaced or removed", () =>
884 Effect.gen(function* () {
885 for (const nextConfig of [{ ...tunnelConfig, connectorToken: "replacement" }, null]) {
886 yield* Effect.gen(function* () {
887 const checking = yield* Deferred.make<void>();
888 const releaseCheck = yield* Deferred.make<void>();
889 const fixture = yield* makeTransportSpawner({
890 isRunning: Deferred.succeed(checking, undefined).pipe(
891 Effect.andThen(Deferred.await(releaseCheck)),
892 Effect.as(true),
893 ),
894 });
895 const runtime = yield* buildCloudManagedEndpointRuntime(fixture.spawner);
896 yield* runtime.applyConfig(tunnelConfig);
897 yield* fixture.observe(runtime, 0);
898
899 // Hold reconciliation in a process probe so config changes queue before fallback.
900 const probe = yield* runtime.applyConfig(tunnelConfig).pipe(Effect.forkChild);
901 yield* Deferred.await(checking);
902 const change = yield* runtime
903 .applyConfig(nextConfig)
904 .pipe(Effect.forkChild({ startImmediately: true }));
905 yield* fixture.observe(runtime, 0, quicDialWarning.repeat(30));
906 yield* Deferred.succeed(releaseCheck, undefined);
907 yield* Fiber.join(probe);
908 yield* Fiber.join(change);
909 yield* TestClock.adjust(Duration.zero);
910
911 expect(fixture.killed).toEqual([800]);
912 expect(fixture.spawned).toHaveLength(nextConfig ? 2 : 1);
913 if (nextConfig) {
914 expect(fixture.spawned[1]!.options.env?.TUNNEL_TOKEN).toBe("replacement");
915 expect(fixture.spawned[1]!.args).not.toContain("--protocol");
916 }
917 yield* runtime.applyConfig(null);
918 // Buffered output and the old exit cannot revive a removed configuration.
919 yield* fixture.emit(0, quicDialWarning.repeat(30));
920 yield* TestClock.adjust(Duration.minutes(5));
921 expect(fixture.spawned).toHaveLength(nextConfig ? 2 : 1);
922 expect(fixture.killed).toEqual(nextConfig ? [800, 801] : [800]);
923 }).pipe(Effect.scoped);
924 }
925 }).pipe(Effect.provide(TestClock.layer())),
926 );
927
928 it.effect("recovers a rejected tunnel without waiting for the connector to exit", () =>
929 Effect.gen(function* () {
930 const output = yield* Queue.unbounded<Uint8Array>();
931 const firstBatchObserved = yield* Deferred.make<void>();
932 const secondBatchObserved = yield* Deferred.make<void>();
933 const recoveryRequested = yield* Deferred.make<RelayManagedEndpointRuntimeConfig>();
934 const recoveryRetried = yield* Deferred.make<RelayManagedEndpointRuntimeConfig>();
935 let recoveryRequestCount = 0;
936 const spawned: Array<number> = [];
937 const encoder = new TextEncoder();
938 const connectorOutput = Stream.fromQueue(output).pipe(
939 Stream.tap((chunk) => {
940 const line = new TextDecoder().decode(chunk);
941 if (line === "first checkpoint\n") {
942 return Deferred.succeed(firstBatchObserved, undefined).pipe(Effect.asVoid);
943 }
944 if (line === "second checkpoint\n") {
945 return Deferred.succeed(secondBatchObserved, undefined).pipe(Effect.asVoid);
946 }
947 return Effect.void;
948 }),
949 );
950 const spawner = ChildProcessSpawner.make(() =>
951 Effect.gen(function* () {
952 const pid = 600;
953 spawned.push(pid);
954 const handle = makeHandle({ pid, onKill: () => {}, output: connectorOutput });
955 yield* Effect.addFinalizer(() => handle.kill().pipe(Effect.ignore));
956 return handle;
957 }),
958 );
959 const runtime = yield* buildCloudManagedEndpointRuntime(spawner);
960 const config = {
961 providerKind: "cloudflare_tunnel" as const,
962 connectorToken: "token",
963 tunnelId: "deleted-tunnel",
964 };
965 const rejectedLine =
966 '2026-09-15T06:30:43Z ERR Register tunnel error from server side error="Failed to get tunnel" connIndex=0 event=0 ip=198.41.200.23\n';
967
968 yield* runtime.recoveryRequests.pipe(
969 Stream.runForEach((requested) => {
970 recoveryRequestCount += 1;
971 return Deferred.succeed(
972 recoveryRequestCount === 1 ? recoveryRequested : recoveryRetried,
973 requested,
974 ).pipe(Effect.asVoid);
975 }),
976 Effect.forkChild,
977 );
978 yield* runtime.applyConfig(config);
979
980 yield* Queue.offer(output, encoder.encode(rejectedLine.repeat(3)));
981 yield* Queue.offer(output, encoder.encode("first checkpoint\n"));
982 yield* Deferred.await(firstBatchObserved);
983 expect(yield* Deferred.isDone(recoveryRequested)).toBe(false);
984
985 yield* Queue.offer(
986 output,
987 encoder.encode(
988 "2026-06-17T02:00:00Z INF Registered tunnel connection connIndex=0\n" +
989 rejectedLine.repeat(3),
990 ),
991 );
992 yield* Queue.offer(output, encoder.encode("second checkpoint\n"));
993 yield* Deferred.await(secondBatchObserved);
994 expect(yield* Deferred.isDone(recoveryRequested)).toBe(false);
995
996 yield* Queue.offer(output, encoder.encode(rejectedLine));
997
998 expect(yield* Deferred.await(recoveryRequested)).toEqual(config);
999
1000 yield* Queue.offer(output, encoder.encode(rejectedLine.repeat(4)));
1001
1002 expect(yield* Deferred.await(recoveryRetried)).toEqual(config);
1003 expect(spawned).toEqual([600]);
1004 }),
1005 );
1006
1007 it.effect("starts, deduplicates, rotates, and stops the Cloudflare connector", () =>
1008 Effect.gen(function* () {
1009 const spawned: Array<ChildProcess.StandardCommand> = [];
1010 const killed: Array<number> = [];
1011 let nextPid = 100;
1012 const spawner = ChildProcessSpawner.make((command) =>
1013 Effect.gen(function* () {
1014 if (!ChildProcess.isStandardCommand(command)) {
1015 throw new Error("Expected standard command.");
1016 }
1017 spawned.push(command);
1018 const pid = nextPid;
1019 nextPid += 1;
1020 const handle = makeHandle({
1021 pid,
1022 onKill: () => {
1023 killed.push(pid);
1024 },
1025 });
1026 yield* Effect.addFinalizer(() => handle.kill().pipe(Effect.ignore));
1027 return handle;
1028 }),
1029 );
1030 const runtime = yield* buildCloudManagedEndpointRuntime(spawner);
1031
1032 yield* runtime.applyConfig({
1033 providerKind: "cloudflare_tunnel",
1034 connectorToken: "token-1",
1035 tunnelId: "tunnel-1",
1036 tunnelName: "t3-code-env-1",
1037 });
1038 yield* runtime.applyConfig({
1039 providerKind: "cloudflare_tunnel",
1040 connectorToken: "token-1",
1041 tunnelId: "tunnel-1",
1042 tunnelName: "t3-code-env-1",
1043 });
1044 yield* runtime.applyConfig({
1045 providerKind: "cloudflare_tunnel",
1046 connectorToken: "token-2",
1047 tunnelId: "tunnel-1",
1048 tunnelName: "t3-code-env-1",
1049 });
1050 const stopped = yield* runtime.applyConfig(null);
1051
1052 expect(spawned.map((command) => command.command)).toEqual(["cloudflared", "cloudflared"]);
1053 expect(spawned.map((command) => command.args)).toEqual([
1054 [
1055 "tunnel",
1056 "--no-autoupdate",
1057 "--loglevel",
1058 "info",
1059 "--output",
1060 "default",
1061 "--metrics",
1062 "127.0.0.1:0",
1063 "run",
1064 ],
1065 [
1066 "tunnel",
1067 "--no-autoupdate",
1068 "--loglevel",
1069 "info",
1070 "--output",
1071 "default",
1072 "--metrics",
1073 "127.0.0.1:0",
1074 "run",
1075 ],
1076 ]);
1077 expect(spawned.map((command) => command.options.env?.TUNNEL_TOKEN)).toEqual([
1078 "token-1",
1079 "token-2",
1080 ]);
1081 expect(spawned.map((command) => command.options.stdout)).toEqual(["pipe", "pipe"]);
1082 expect(spawned.map((command) => command.options.stderr)).toEqual(["pipe", "pipe"]);
1083 expect(spawned.map((command) => command.options.detached)).toEqual([false, false]);
1084 expect(spawned.map((command) => command.options.shell)).toEqual([false, false]);
1085 expect(killed).toEqual([100, 101]);
1086 expect(stopped).toEqual({ status: "disabled" });
1087 }),
1088 );
1089
1090 it.effect("supervises the active connector and restarts it after process exit", () =>
1091 Effect.gen(function* () {
1092 const spawned: Array<number> = [];
1093 const killed: Array<number> = [];
1094 const firstExit = yield* Deferred.make<ChildProcessSpawner.ExitCode>();
1095 const secondSpawned = yield* Deferred.make<void>();
1096 const spawner = ChildProcessSpawner.make(() =>
1097 Effect.gen(function* () {
1098 const pid = spawned.length === 0 ? 400 : 401;
1099 spawned.push(pid);
1100 if (pid === 401) {
1101 yield* Deferred.succeed(secondSpawned, undefined);
1102 }
1103 const handle = makeHandle({
1104 pid,
1105 exitCode:
1106 pid === 400
1107 ? Deferred.await(firstExit)
1108 : (Effect.never as Effect.Effect<ChildProcessSpawner.ExitCode>),
1109 onKill: () => {
1110 killed.push(pid);
1111 },
1112 });
1113 yield* Effect.addFinalizer(() => handle.kill().pipe(Effect.ignore));
1114 return handle;
1115 }),
1116 );
1117 const runtime = yield* buildCloudManagedEndpointRuntime(spawner);
1118
1119 const started = yield* runtime.applyConfig({
1120 providerKind: "cloudflare_tunnel",
1121 connectorToken: "token",
1122 tunnelId: "tunnel-1",
1123 });
1124 yield* Deferred.succeed(firstExit, ChildProcessSpawner.ExitCode(1));
1125 yield* Deferred.await(secondSpawned);
1126
1127 expect(started).toMatchObject({ status: "running", pid: 400 });
1128 expect(spawned).toEqual([400, 401]);
1129 expect(killed).toEqual([400]);
1130 }),
1131 );
1132
1133 it.effect.each(["SIGINT", "SIGTERM"] as const)(
1134 "does not restart a connector that exits after the server receives %s",
1135 (signal) =>
1136 Effect.gen(function* () {
1137 const spawned: Array<number> = [];
1138 const firstExit = yield* Deferred.make<ChildProcessSpawner.ExitCode>();
1139 const firstKilled = yield* Deferred.make<void>();
1140 const spawner = ChildProcessSpawner.make(() =>
1141 Effect.gen(function* () {
1142 const pid = 400 + spawned.length;
1143 spawned.push(pid);
1144 const handle = makeHandle({
1145 pid,
1146 exitCode:
1147 pid === 400
1148 ? Deferred.await(firstExit)
1149 : (Effect.never as Effect.Effect<ChildProcessSpawner.ExitCode>),
1150 onKill: () => {
1151 if (pid === 400) Deferred.doneUnsafe(firstKilled, Exit.void);
1152 },
1153 });
1154 yield* Effect.addFinalizer(() => handle.kill().pipe(Effect.ignore));
1155 return handle;
1156 }),
1157 );
1158 // Invoke only the runtime's own listeners; emitting would reach the test worker's too.
1159 const listenersBefore = new Set(process.listeners(signal));
1160 const runtime = yield* buildCloudManagedEndpointRuntime(spawner);
1161 const runtimeListeners = process
1162 .listeners(signal)
1163 .filter((listener) => !listenersBefore.has(listener));
1164
1165 yield* runtime.applyConfig({
1166 providerKind: "cloudflare_tunnel",
1167 connectorToken: "token",
1168 tunnelId: "tunnel-1",
1169 });
1170 for (const listener of runtimeListeners) listener(signal);
1171 // The process group receives the signal too, so the connector exits cleanly.
1172 yield* Deferred.succeed(firstExit, ChildProcessSpawner.ExitCode(0));
1173 yield* Deferred.await(firstKilled);
1174 // Holding the reconcile lock waits out the supervisor's restart decision.
1175 yield* runtime.restartConnector(400, Effect.succeed(true));
1176
1177 expect(spawned).toEqual([400]);
1178 }),
1179 );
1180
1181 it.effect("serializes concurrent connector config changes", () =>
1182 Effect.gen(function* () {
1183 const spawned: Array<number> = [];
1184 const killed: Array<number> = [];
1185 const firstSpawnEntered = yield* Deferred.make<void>();
1186 const releaseFirstSpawn = yield* Deferred.make<void>();
1187 const spawner = ChildProcessSpawner.make(() =>
1188 Effect.gen(function* () {
1189 const pid = 500 + spawned.length;
1190 spawned.push(pid);
1191 if (pid === 500) {
1192 yield* Deferred.succeed(firstSpawnEntered, undefined);
1193 yield* Deferred.await(releaseFirstSpawn);
1194 }
1195 const handle = makeHandle({
1196 pid,
1197 onKill: () => {
1198 killed.push(pid);
1199 },
1200 });
1201 yield* Effect.addFinalizer(() => handle.kill().pipe(Effect.ignore));
1202 return handle;
1203 }),
1204 );
1205 const runtime = yield* buildCloudManagedEndpointRuntime(spawner);
1206
1207 const first = yield* runtime
1208 .applyConfig({
1209 providerKind: "cloudflare_tunnel",
1210 connectorToken: "token-1",
1211 })
1212 .pipe(Effect.forkChild);
1213 yield* Deferred.await(firstSpawnEntered);
1214 const second = yield* runtime
1215 .applyConfig({
1216 providerKind: "cloudflare_tunnel",
1217 connectorToken: "token-2",
1218 })
1219 .pipe(Effect.forkChild);
1220 yield* Deferred.succeed(releaseFirstSpawn, undefined);
1221
1222 yield* Fiber.join(first);
1223 const status = yield* Fiber.join(second);
1224
1225 expect(status).toMatchObject({ status: "running", pid: 501 });
1226 expect(spawned).toEqual([500, 501]);
1227 expect(killed).toEqual([500]);
1228 }),
1229 );
1230
1231 it.effect(
1232 "runs an older relay client while the pinned one installs, then hands off to it and prunes",
1233 () =>
1234 Effect.gen(function* () {
1235 const olderClient = {
1236 status: "available",
1237 executablePath: "/managed/2025.9.0/cloudflared",
1238 source: "managed",
1239 version: "2025.9.0",
1240 } as const;
1241 const pinnedClient = {
1242 status: "available",
1243 executablePath: "/managed/pinned/cloudflared",
1244 source: "managed",
1245 version: RelayClient.CLOUDFLARED_VERSION,
1246 } as const;
1247 let installed = false;
1248 const releaseInstall = yield* Deferred.make<void>();
1249 const pruned = yield* Deferred.make<void>();
1250 const spawned = yield* Queue.unbounded<string>();
1251 const olderStopped = yield* Deferred.make<void>();
1252 const output = yield* Queue.unbounded<Uint8Array>();
1253 const spawner = ChildProcessSpawner.make((command) =>
1254 Effect.gen(function* () {
1255 const executable = ChildProcess.isStandardCommand(command) ? command.command : "";
1256 yield* Queue.offer(spawned, executable);
1257 const handle = makeHandle({
1258 pid: executable === pinnedClient.executablePath ? 2 : 1,
1259 onKill: () => {
1260 if (executable === olderClient.executablePath) {
1261 Deferred.doneUnsafe(olderStopped, Effect.void);
1262 }
1263 },
1264 ...(executable === pinnedClient.executablePath
1265 ? { output: Stream.fromQueue(output) }
1266 : {}),
1267 });
1268 yield* Effect.addFinalizer(() => handle.kill().pipe(Effect.ignore));
1269 return handle;
1270 }),
1271 );
1272 const runtime = yield* buildCloudManagedEndpointRuntime(
1273 spawner,
1274 Layer.succeed(
1275 RelayClient.RelayClient,
1276 RelayClient.RelayClient.of({
1277 resolve: Effect.sync(() => (installed ? pinnedClient : olderClient)),
1278 install: Deferred.await(releaseInstall).pipe(
1279 Effect.andThen(
1280 Effect.sync(() => {
1281 installed = true;
1282 return pinnedClient;
1283 }),
1284 ),
1285 ),
1286 installWithProgress: () => Effect.die("unused"),
1287 pruneManagedVersions: Deferred.succeed(pruned, undefined).pipe(Effect.asVoid),
1288 }),
1289 ),
1290 );
1291
1292 const status = yield* runtime.applyConfig({
1293 providerKind: "cloudflare_tunnel",
1294 connectorToken: "token",
1295 });
1296 expect(status).toMatchObject({ status: "running", pid: 1 });
1297 expect(yield* Queue.take(spawned)).toBe(olderClient.executablePath);
1298
1299 yield* Deferred.succeed(releaseInstall, undefined);
1300 expect(yield* Queue.take(spawned)).toBe(pinnedClient.executablePath);
1301 expect(Option.isNone(yield* Deferred.poll(olderStopped))).toBe(true);
1302
1303 yield* Queue.offer(
1304 output,
1305 new TextEncoder().encode(
1306 "2026-10-08T00:00:00Z INF Registered tunnel connection connIndex=0\n",
1307 ),
1308 );
1309 yield* Deferred.await(olderStopped);
1310 yield* Deferred.await(pruned);
1311 }),
1312 );
1313
1314 it.effect("installs a missing relay client in the background and then starts it", () =>
1315 Effect.gen(function* () {
1316 const pinnedClient = {
1317 status: "available",
1318 executablePath: "/managed/pinned/cloudflared",
1319 source: "managed",
1320 version: RelayClient.CLOUDFLARED_VERSION,
1321 } as const;
1322 let installed = false;
1323 const spawned = yield* Deferred.make<string>();
1324 const spawner = ChildProcessSpawner.make((command) =>
1325 Effect.gen(function* () {
1326 yield* Deferred.succeed(
1327 spawned,
1328 ChildProcess.isStandardCommand(command) ? command.command : "",
1329 );
1330 const handle = makeHandle({ pid: 3, onKill: () => {} });
1331 yield* Effect.addFinalizer(() => handle.kill().pipe(Effect.ignore));
1332 return handle;
1333 }),
1334 );
1335 const runtime = yield* buildCloudManagedEndpointRuntime(
1336 spawner,
1337 Layer.succeed(
1338 RelayClient.RelayClient,
1339 RelayClient.RelayClient.of({
1340 resolve: Effect.sync(() =>
1341 installed
1342 ? pinnedClient
1343 : { status: "missing" as const, version: RelayClient.CLOUDFLARED_VERSION },
1344 ),
1345 install: Effect.sync(() => {
1346 installed = true;
1347 return pinnedClient;
1348 }),
1349 installWithProgress: () => Effect.die("unused"),
1350 pruneManagedVersions: Effect.void,
1351 }),
1352 ),
1353 );
1354
1355 const status = yield* runtime.applyConfig({
1356 providerKind: "cloudflare_tunnel",
1357 connectorToken: "token",
1358 });
1359 expect(status).toMatchObject({ status: "failed", failure: "not-installed" });
1360 expect(yield* Deferred.await(spawned)).toBe(pinnedClient.executablePath);
1361 }),
1362 );
1363
1364 it.effect("keeps the older connector when the updated relay client fails to start", () =>
1365 Effect.gen(function* () {
1366 const olderClient = {
1367 status: "available",
1368 executablePath: "/managed/2025.9.0/cloudflared",
1369 source: "managed",
1370 version: "2025.9.0",
1371 } as const;
1372 const pinnedClient = {
1373 status: "available",
1374 executablePath: "/managed/pinned/cloudflared",
1375 source: "managed",
1376 version: RelayClient.CLOUDFLARED_VERSION,
1377 } as const;
1378 let installed = false;
1379 let olderKilled = false;
1380 const releaseInstall = yield* Deferred.make<void>();
1381 const pinnedAttempted = yield* Deferred.make<void>();
1382 const spawner = ChildProcessSpawner.make((command) =>
1383 Effect.gen(function* () {
1384 const executable = ChildProcess.isStandardCommand(command) ? command.command : "";
1385 if (executable === pinnedClient.executablePath) {
1386 yield* Deferred.succeed(pinnedAttempted, undefined);
1387 return yield* PlatformError.systemError({
1388 _tag: "PermissionDenied",
1389 module: "ChildProcess",
1390 method: "spawn",
1391 });
1392 }
1393 const handle = makeHandle({
1394 pid: 6,
1395 onKill: () => {
1396 olderKilled = true;
1397 },
1398 });
1399 yield* Effect.addFinalizer(() => handle.kill().pipe(Effect.ignore));
1400 return handle;
1401 }),
1402 );
1403 const runtime = yield* buildCloudManagedEndpointRuntime(
1404 spawner,
1405 Layer.succeed(
1406 RelayClient.RelayClient,
1407 RelayClient.RelayClient.of({
1408 resolve: Effect.sync(() => (installed ? pinnedClient : olderClient)),
1409 install: Deferred.await(releaseInstall).pipe(
1410 Effect.andThen(
1411 Effect.sync(() => {
1412 installed = true;
1413 return pinnedClient;
1414 }),
1415 ),
1416 ),
1417 installWithProgress: () => Effect.die("unused"),
1418 pruneManagedVersions: Effect.void,
1419 }),
1420 ),
1421 );
1422 const config = { providerKind: "cloudflare_tunnel", connectorToken: "token" } as const;
1423 expect(yield* runtime.applyConfig(config)).toMatchObject({ status: "running", pid: 6 });
1424
1425 yield* Deferred.succeed(releaseInstall, undefined);
1426 yield* Deferred.await(pinnedAttempted);
1427 expect(yield* runtime.applyConfig(config)).toMatchObject({ status: "running", pid: 6 });
1428 expect(olderKilled).toBe(false);
1429 }),
1430 );
1431
1432 it.effect("requests recovery when the older connector exited during a failed swap", () =>
1433 Effect.gen(function* () {
1434 const olderClient = {
1435 status: "available",
1436 executablePath: "/managed/2025.9.0/cloudflared",
1437 source: "managed",
1438 version: "2025.9.0",
1439 } as const;
1440 const pinnedClient = {
1441 status: "available",
1442 executablePath: "/managed/pinned/cloudflared",
1443 source: "managed",
1444 version: RelayClient.CLOUDFLARED_VERSION,
1445 } as const;
1446 let installed = false;
1447 const releaseInstall = yield* Deferred.make<void>();
1448 const spawner = ChildProcessSpawner.make((command) =>
1449 Effect.gen(function* () {
1450 const executable = ChildProcess.isStandardCommand(command) ? command.command : "";
1451 if (executable === pinnedClient.executablePath) {
1452 return yield* PlatformError.systemError({
1453 _tag: "PermissionDenied",
1454 module: "ChildProcess",
1455 method: "spawn",
1456 });
1457 }
1458 const handle = makeHandle({ pid: 12, onKill: () => {}, isRunning: () => !installed });
1459 yield* Effect.addFinalizer(() => handle.kill().pipe(Effect.ignore));
1460 return handle;
1461 }),
1462 );
1463 const runtime = yield* buildCloudManagedEndpointRuntime(
1464 spawner,
1465 Layer.succeed(
1466 RelayClient.RelayClient,
1467 RelayClient.RelayClient.of({
1468 resolve: Effect.sync(() => (installed ? pinnedClient : olderClient)),
1469 install: Deferred.await(releaseInstall).pipe(
1470 Effect.andThen(
1471 Effect.sync(() => {
1472 installed = true;
1473 return pinnedClient;
1474 }),
1475 ),
1476 ),
1477 installWithProgress: () => Effect.die("unused"),
1478 pruneManagedVersions: Effect.void,
1479 }),
1480 ),
1481 );
1482 const config = { providerKind: "cloudflare_tunnel", connectorToken: "token" } as const;
1483 expect(yield* runtime.applyConfig(config)).toMatchObject({ status: "running", pid: 12 });
1484
1485 yield* Deferred.succeed(releaseInstall, undefined);
1486 expect(Option.getOrThrow(yield* Stream.runHead(runtime.recoveryRequests))).toEqual(config);
1487 }),
1488 );
1489
1490 it.effect("restarts a self-updated pinned-folder connector once the pin is reinstalled", () =>
1491 Effect.gen(function* () {
1492 const executablePath = "/managed/pinned/cloudflared";
1493 let version = "2026.9.3";
1494 const releaseInstall = yield* Deferred.make<void>();
1495 const spawned = yield* Queue.unbounded<string>();
1496 const spawner = ChildProcessSpawner.make(() =>
1497 Effect.gen(function* () {
1498 yield* Queue.offer(spawned, version);
1499 const handle = makeHandle({ pid: 7, onKill: () => {} });
1500 yield* Effect.addFinalizer(() => handle.kill().pipe(Effect.ignore));
1501 return handle;
1502 }),
1503 );
1504 const current = () =>
1505 ({ status: "available", executablePath, source: "managed", version }) as const;
1506 const runtime = yield* buildCloudManagedEndpointRuntime(
1507 spawner,
1508 Layer.succeed(
1509 RelayClient.RelayClient,
1510 RelayClient.RelayClient.of({
1511 resolve: Effect.sync(current),
1512 install: Deferred.await(releaseInstall).pipe(
1513 Effect.andThen(
1514 Effect.sync(() => {
1515 version = RelayClient.CLOUDFLARED_VERSION;
1516 return current();
1517 }),
1518 ),
1519 ),
1520 installWithProgress: () => Effect.die("unused"),
1521 pruneManagedVersions: Effect.void,
1522 }),
1523 ),
1524 );
1525
1526 yield* runtime.applyConfig({ providerKind: "cloudflare_tunnel", connectorToken: "token" });
1527 expect(yield* Queue.take(spawned)).toBe("2026.9.3");
1528 yield* Deferred.succeed(releaseInstall, undefined);
1529 expect(yield* Queue.take(spawned)).toBe(RelayClient.CLOUDFLARED_VERSION);
1530 }),
1531 );
1532 });