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, 23:30 UTC
fix(server): listing linked environments no longer waits out an asleep peer (#28543)
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Julius Marminge committed
Nov 11, 2026 at 11:50 UTC
7c12233d028d99705aec6178b1be01bfce4935dd
3 files changed
+82
-10
apps/server/src/peer/PeerLinks.test.ts
+57
@@ -12,6 +12,7 @@ import * as Effect from "effect/Effect";
12
import * as Fiber from "effect/Fiber";
13
import * as Layer from "effect/Layer";
14
import * as Option from "effect/Option";
15
+import * as Queue from "effect/Queue";
16
import * as Ref from "effect/Ref";
17
import * as Schema from "effect/Schema";
18
import * as HttpRouter from "effect/http/HttpRouter";
@@ -389,6 +390,62 @@ it.effect("never sends a link's token to an address that stops answering as that
390
).pipe(Effect.provide(NodeHttpServer.layerTest)),
391
);
392
393
+it.effect("lists a peer asleep at every address as unreachable within one probe budget", () =>
394
+ Effect.scoped(
395
+ Effect.gen(function* () {
396
+ const b = yield* serveB(box);
397
+ // Another address of the same machine, asleep too.
398
+ const wake = yield* Deferred.make<void>();
399
+ const elsewhere = yield* serveRaw(() =>
400
+ Deferred.await(wake).pipe(Effect.as(HttpServerResponse.empty({ status: 503 }))),
401
+ );
402
+ yield* Effect.addFinalizer(() => Deferred.succeed(wake, undefined));
403
+ const a = yield* makeA(laptop);
404
+ const pairing = yield* b.auth.issuePairingCredential();
405
+ yield* a.links.link({
406
+ url: b.url,
407
+ alternateUrls: [elsewhere],
408
+ pairingCode: pairing.credential,
409
+ access: "auto",
410
+ });
411
+
412
+ yield* Ref.set(b.asleep, true);
413
+ const listing = yield* a.links.list.pipe(Effect.forkScoped);
414
+ yield* Queue.take(b.unanswered);
415
+ yield* TestClock.adjust(Duration.seconds(3));
416
+ const listed = yield* Fiber.join(listing);
417
+ expect(listed.map((link) => link.status)).toEqual(["unreachable"]);
418
+ }),
419
+ ).pipe(Effect.provide(NodeHttpServer.layerTest)),
420
+);
421
+
422
+it.effect("lists a peer as reachable at another address while its first one sleeps", () =>
423
+ Effect.scoped(
424
+ Effect.gen(function* () {
425
+ const b = yield* serveB(box);
426
+ const described = yield* HttpClient.get(`${b.url}/.well-known/t3/environment`).pipe(
427
+ Effect.flatMap((response) => response.text),
428
+ Effect.provide(FetchHttpClient.layer),
429
+ );
430
+ // Another address of the same machine that answers.
431
+ const elsewhere = yield* serveRaw(() =>
432
+ Effect.succeed(HttpServerResponse.text(described, { contentType: "application/json" })),
433
+ );
434
+ const a = yield* makeA(laptop);
435
+ const pairing = yield* b.auth.issuePairingCredential();
436
+ yield* a.links.link({
437
+ url: b.url,
438
+ alternateUrls: [elsewhere],
439
+ pairingCode: pairing.credential,
440
+ access: "auto",
441
+ });
442
+
443
+ yield* Ref.set(b.asleep, true);
444
+ expect((yield* a.links.list).map((link) => link.status)).toEqual(["reachable"]);
445
+ }),
446
+ ).pipe(Effect.provide(NodeHttpServer.layerTest)),
447
+);
448
+
449
it.effect("cancelling a descriptor probe aborts its request and leaves later resolves fresh", () =>
450
Effect.scoped(
451
Effect.gen(function* () {
apps/server/src/peer/PeerLinks.testkit.ts
+16
@@ -7,8 +7,10 @@ import {
7
type ExecutionEnvironmentDescriptor,
8
} from "@t3tools/contracts";
9
import * as Context from "effect/Context";
10
+import * as Deferred from "effect/Deferred";
11
import * as Effect from "effect/Effect";
12
import * as Layer from "effect/Layer";
13
+import * as Queue from "effect/Queue";
14
import * as Ref from "effect/Ref";
15
import * as FetchHttpClient from "effect/http/FetchHttpClient";
16
import * as HttpRouter from "effect/http/HttpRouter";
@@ -70,6 +72,9 @@ export const servePeer = <E, R>(
72
const descriptor = yield* Ref.make(initial);
73
const bearers = yield* Ref.make<ReadonlyArray<string>>([]);
74
const probes = yield* Ref.make(0);
75
+ const asleep = yield* Ref.make(false);
76
+ const unanswered = yield* Queue.unbounded<void>();
77
+ const wake = yield* Deferred.make<void>();
78
const layerEnvironment = Layer.succeed(ServerEnvironment.ServerEnvironment, {
79
getEnvironmentId: Ref.get(descriptor).pipe(Effect.map((current) => current.environmentId)),
80
getDescriptor: Ref.get(descriptor),
@@ -91,6 +96,11 @@ export const servePeer = <E, R>(
96
const request = yield* HttpServerRequest.HttpServerRequest;
97
if (request.url.startsWith("/.well-known/t3/environment")) {
98
yield* Ref.update(probes, (count) => count + 1);
99
+ if (yield* Ref.get(asleep)) {
100
+ yield* Queue.offer(unanswered, undefined);
101
+ yield* Deferred.await(wake);
102
+ return yield* Effect.die("the peer was closed while asleep");
103
+ }
104
}
105
const authorization = request.headers.authorization;
106
if (authorization !== undefined) {
@@ -141,6 +151,8 @@ export const servePeer = <E, R>(
151
Layer.fresh,
152
Layer.build,
153
);
154
+ // An unanswered request would hold the server open past the test.
155
+ yield* Effect.addFinalizer(() => Deferred.succeed(wake, undefined));
156
const address = (yield* HttpServer.HttpServer).address;
157
if (address._tag === "UnixPathAddress") return yield* Effect.die("expected a TCP address");
158
const url = `http://127.0.0.1:${address.port}`;
@@ -154,6 +166,10 @@ export const servePeer = <E, R>(
166
bearers,
167
/** How often its descriptor was asked for, as a probe of whether it answers. */
168
probes,
169
+ /** While set, its descriptor never answers, as a machine that is asleep. */
170
+ asleep,
171
+ /** One entry per descriptor request it left unanswered while asleep. */
172
+ unanswered,
173
/** The sessions linking created there, which its Connections lists. */
174
linkedSessions: auth
175
.listSessions()
apps/server/src/peer/PeerLinks.ts
+9
-10
@@ -52,6 +52,8 @@ const LINK_REDIRECT_URI = "http://127.0.0.1/t3code-peer-link";
52
*/
53
const probeTimeout = (origin: string) =>
54
isLocalLoopbackHost(new URL(origin).hostname) ? Duration.seconds(3) : Duration.seconds(10);
55
+/** How long a listing waits on a link's addresses; a real call still gives a waking peer `probeTimeout`. */
56
+const LIST_PROBE_TIMEOUT = Duration.seconds(3);
57
58
/** Node's TLS verification code in a failed request's cause chain, if any. */
59
export const certificateErrorCode = (error: unknown) => {
@@ -574,18 +576,15 @@ const make = Effect.gen(function* () {
576
now: DateTime.Utc,
577
): Effect.fn.Return<PeerLinkSummary> {
578
if (DateTime.isLessThanOrEqualTo(link.expiresAt, now)) return { ...link, status: "expired" };
577
- let label = link.label;
578
- const answered = yield* Effect.findFirst(link.urls, (url) =>
579
- answersAs(url, link.environmentId).pipe(
580
- Effect.map((found) => {
581
- if (Option.isNone(found)) return false;
582
- label = found.value.label;
583
- return true;
584
- }),
579
+ // A race over no addresses never settles.
580
+ if (link.urls.length === 0) return { ...link, status: "unreachable" };
581
+ const answered = yield* Effect.raceAll(
582
+ link.urls.map((url) =>
583
+ answersAs(url, link.environmentId).pipe(Effect.flatMap(Effect.fromOption)),
584
),
586
- );
585
+ ).pipe(Effect.timeoutOption(LIST_PROBE_TIMEOUT), Effect.orElseSucceed(Option.none));
586
if (Option.isNone(answered)) return { ...link, status: "unreachable" };
588
- return { ...(yield* withLabel(link, label)), status: "reachable" };
587
+ return { ...(yield* withLabel(link, answered.value.label)), status: "reachable" };
588
});
589
590
const list: PeerLinks["Service"]["list"] = Effect.gen(function* () {