@devries-cameron20 / t3code-1 / commits / e45ddea067
Fork 0

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

fix(client-runtime): connection attempts record their outcome and failure reason (#22081)

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>

Julius Marminge committed Oct 19, 2026 at 19:33 UTC e45ddea067e8d2337fe92845c5eb6d4c02255b9c
3 files changed +71 -12
packages/client-runtime/src/connection/supervisor.test.ts
+23 -3
@@ -1,8 +1,10 @@
1 import { AuthStandardClientScopes, EnvironmentId, type ServerConfig } from "@t3tools/contracts";
2 import { RelayClientTracer } from "@t3tools/shared/relayTracing";
3 import { describe, expect, it } from "@effect/vitest";
4 +import * as Cause from "effect/Cause";
5 import * as Deferred from "effect/Deferred";
6 import * as Effect from "effect/Effect";
7 +import * as Exit from "effect/Exit";
8 import * as Layer from "effect/Layer";
9 import * as Option from "effect/Option";
10 import * as Random from "effect/Random";
@@ -294,7 +296,7 @@ describe("retryDelayMs", () => {
296 });
297
298 describe("EnvironmentSupervisor", () => {
297 - it.effect("exports each relay setup as a standalone linked trace that ends at readiness", () =>
299 + it.effect("exports each relay setup as a standalone linked trace with its outcome", () =>
300 Effect.gen(function* () {
301 const spans: Array<Tracer.NativeSpan> = [];
302 const tracer = Tracer.make({
@@ -308,9 +310,10 @@ describe("EnvironmentSupervisor", () => {
310 return span;
311 },
312 });
313 + const firstFailure = transient();
314 const harness = yield* makeHarness({
312 - prepare: (attempt) =>
313 - attempt === 1 ? Effect.fail(transient()) : Effect.succeed(PREPARED_CONNECTION),
315 + prepare: (attempt, target) =>
316 + attempt === 1 ? Effect.fail(firstFailure) : Effect.succeed(preparedFor(target)),
317 });
318 const supervisor = yield* EnvironmentSupervisor.make(RELAY_ENTRY, {
319 initiallyDesired: true,
@@ -334,6 +337,23 @@ describe("EnvironmentSupervisor", () => {
337 expect(attempts[0]?.traceId).not.toBe(attempts[1]?.traceId);
338 expect(attempts[1]?.links.map((link) => link.span.spanId)).toContain(attempts[0]?.spanId);
339 expect(yield* Ref.get(harness.releaseCount)).toBe(0);
340 +
341 + const [failed, connected] = attempts;
342 + // The exporter derives the status message from the failure, so it must
343 + // be the connection error rather than a wrapper object.
344 + const failedExit = failed?.status._tag === "Ended" ? failed.status.exit : undefined;
345 + expect(failedExit && Exit.isFailure(failedExit) ? Cause.squash(failedExit.cause) : null).toBe(
346 + firstFailure,
347 + );
348 + expect(Object.fromEntries(failed?.attributes ?? [])).toMatchObject({
349 + "connection.outcome": "failed",
350 + "connection.failure.tag": "ConnectionTransientError",
351 + "connection.failure.reason": "transport",
352 + });
353 + expect(Object.fromEntries(connected?.attributes ?? [])).toMatchObject({
354 + "connection.outcome": "connected",
355 + "connection.route": "relay",
356 + });
357 }).pipe(Effect.provide(TestClock.layer())),
358 );
359
packages/client-runtime/src/connection/supervisor.ts
+40 -9
@@ -32,6 +32,7 @@ import { NETWORK_BLOCKING_HINT } from "../errors/network.ts";
32 import * as ConnectionWakeups from "./wakeups.ts";
33 import {
34 connectionRouteId,
35 + connectionRouteKind,
36 connectionRoutes,
37 entryWithRoutes,
38 type ReportedEndpoint,
@@ -405,6 +406,34 @@ export const make = Effect.fn("EnvironmentSupervisor.make")(function* (
406 );
407 });
408
409 + // Low-cardinality dimensions for time-to-connected dashboards; the error
410 + // detail stays in the span status, never in an attribute.
411 + const annotateAttemptOutcome = Effect.fnUntraced(function* (
412 + exit: Exit.Exit<ConnectionDriver.EnvironmentConnectionLease, ConnectionAttemptError>,
413 + ) {
414 + if (Exit.isSuccess(exit)) {
415 + const leaseTarget = exit.value.prepared.target;
416 + const route = (yield* currentRoutes).find(
417 + (candidate) => connectionRouteId(candidate.target) === connectionRouteId(leaseTarget),
418 + );
419 + return yield* Effect.annotateCurrentSpan({
420 + "connection.outcome": "connected",
421 + "connection.route": connectionRouteKind(
422 + route ?? { target: leaseTarget, profile: Option.none() },
423 + ),
424 + });
425 + }
426 + if (Cause.hasInterruptsOnly(exit.cause)) {
427 + return yield* Effect.annotateCurrentSpan("connection.outcome", "interrupted");
428 + }
429 + const failure = exit.cause.reasons.find(Cause.isFailReason)?.error;
430 + yield* Effect.annotateCurrentSpan({
431 + "connection.outcome": "failed",
432 + "connection.failure.tag": failure?._tag ?? "defect",
433 + "connection.failure.reason": failure?.reason ?? "defect",
434 + });
435 + });
436 +
437 const traceRelayEstablishment = (
438 effect: Effect.Effect<
439 ConnectionDriver.EnvironmentConnectionLease,
@@ -415,8 +444,12 @@ export const make = Effect.fn("EnvironmentSupervisor.make")(function* (
444 generation: number,
445 pendingRetry: Option.Option<PendingRetryTrace>,
446 ) => {
447 + // The span must end with the ConnectionAttemptError itself: the exporter
448 + // can only describe an Error-like failure, and a TracedAttemptFailure
449 + // wrapper reached it as "[object Object]". The wrapper is added outside.
450 + let attemptSpan = Option.none<Tracer.Span>();
451 const traced = Effect.gen(function* () {
419 - const attemptSpan = yield* Effect.currentSpan.pipe(Effect.orDie);
452 + attemptSpan = Option.some(yield* Effect.currentSpan.pipe(Effect.orDie));
453 yield* annotateTarget(target);
454 yield* Effect.annotateCurrentSpan({
455 "connection.attempt": attempt,
@@ -426,13 +459,7 @@ export const make = Effect.fn("EnvironmentSupervisor.make")(function* (
459 onSome: (retry) => retry.failureCount,
460 }),
461 });
429 - const lease = yield* effect.pipe(
430 - Effect.mapError((error): TracedAttemptFailure => ({
431 - error,
432 - attemptSpan: Option.some(attemptSpan),
433 - })),
434 - );
435 - return { attemptSpan: Option.some(attemptSpan), lease };
462 + return yield* effect.pipe(Effect.onExit(annotateAttemptOutcome));
463 }).pipe(Effect.withSpan("relay.connection.attempt", { root: true }));
464
465 return Option.match(pendingRetry, {
@@ -444,7 +471,11 @@ export const make = Effect.fn("EnvironmentSupervisor.make")(function* (
471 "connection.retry.reason": retry.reason,
472 }),
473 ),
447 - }).pipe(withRelayClientTracing);
474 + }).pipe(
475 + Effect.map((lease) => ({ attemptSpan, lease })),
476 + Effect.mapError((error): TracedAttemptFailure => ({ error, attemptSpan })),
477 + withRelayClientTracing,
478 + );
479 };
480
481 const establishTracedConnection = Effect.fnUntraced(function* (
packages/client-runtime/src/rpc/session.ts
+8
@@ -214,6 +214,14 @@ export const make = Effect.fn("RpcSessionFactory.make")(function* (
214 const protocolContext = yield* Layer.build(layerProtocol).pipe(
215 Effect.withSpan("environment.websocket.connect"),
216 );
217 + // Building the protocol only starts the socket; this span times the
218 + // handshake up to the first open, without making `connect` wait for it.
219 + yield* Deferred.await(connected).pipe(
220 + Effect.raceFirst(Deferred.await(disconnected)),
221 + Effect.withSpan("environment.websocket.open"),
222 + Effect.ignore,
223 + Effect.forkScoped,
224 + );
225 const protocolClient = yield* makeWsRpcProtocolClient.pipe(Effect.provide(protocolContext));
226 const initialConfigDeferred = yield* Deferred.make<ServerConfig>();
227 const serverConfigExit = yield* Deferred.make<void, ServerConfigSubscriptionError>();