@devries-cameron20 / t3code Mirror · read-only / commits / f460136f3f
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 22, 08:15 UTC

perf(server): avoid building unused peer HTTP clients (#28405)

Adamulek123 committed Nov 1, 2026 at 22:51 UTC f460136f3fdefea0d1164a5345bcd0cd93d210c6
2 files changed +61 -1
apps/server/src/peer/PeerLinks.test.ts
+54
@@ -2,12 +2,15 @@ import { NodeHttpServer } from "@effect/platform-node";
2 import { type ExecutionEnvironmentDescriptor, OrchestratorMcpFailure } from "@t3tools/contracts";
3 import { expect, it } from "@effect/vitest";
4 import * as Context from "effect/Context";
5 +import * as Deferred from "effect/Deferred";
6 import * as Duration from "effect/Duration";
7 import * as Effect from "effect/Effect";
8 +import * as Fiber from "effect/Fiber";
9 import * as Layer from "effect/Layer";
10 import * as Option from "effect/Option";
11 import * as Ref from "effect/Ref";
12 import * as Schema from "effect/Schema";
13 +import * as HttpRouter from "effect/http/HttpRouter";
14 import * as SqlClient from "effect/sql/SqlClient";
15 import * as TestClock from "effect/testing/TestClock";
16 import { Tool, Toolkit } from "effect/ai";
@@ -339,6 +342,57 @@ it.effect("never sends a link's token to an address that stops answering as that
342 ).pipe(Effect.provide(NodeHttpServer.layerTest)),
343 );
344
345 +it.effect("cancelling a descriptor probe aborts its request and leaves later resolves fresh", () =>
346 + Effect.scoped(
347 + Effect.gen(function* () {
348 + const hold = yield* Ref.make(false);
349 + const requests = yield* Ref.make(0);
350 + const entered = yield* Deferred.make<void>();
351 + const interrupted = yield* Deferred.make<void>();
352 + const holdDescriptor = HttpRouter.middleware(
353 + (httpEffect) =>
354 + Effect.gen(function* () {
355 + const request = yield* HttpServerRequest.HttpServerRequest;
356 + if (request.url.startsWith("/.well-known/t3/environment")) {
357 + yield* Ref.update(requests, (count) => count + 1);
358 + if (yield* Ref.get(hold)) {
359 + return yield* Deferred.succeed(entered, undefined).pipe(
360 + Effect.andThen(Effect.never),
361 + Effect.onInterrupt(() =>
362 + Deferred.succeed(interrupted, undefined).pipe(Effect.asVoid),
363 + ),
364 + Effect.interruptible,
365 + );
366 + }
367 + }
368 + return yield* httpEffect;
369 + }),
370 + { global: true },
371 + );
372 + const b = yield* servePeer(
373 + box,
374 + Layer.merge(McpHttpServer.toolkitRegistration(ProbeToolkit, probeHandlers), holdDescriptor),
375 + );
376 + const a = yield* makeA(laptop);
377 + const pairing = yield* b.auth.issuePairingCredential();
378 + yield* a.links.link({ url: b.url, pairingCode: pairing.credential, access: "auto" });
379 + yield* Ref.set(b.bearers, []);
380 + yield* Ref.set(requests, 0);
381 + yield* Ref.set(hold, true);
382 +
383 + const resolving = yield* a.links.resolve(box.environmentId).pipe(Effect.forkScoped);
384 + yield* Deferred.await(entered);
385 + yield* Fiber.interrupt(resolving);
386 + yield* Deferred.await(interrupted);
387 +
388 + yield* Ref.set(hold, false);
389 + expect((yield* a.links.resolve(box.environmentId)).url).toBe(b.url);
390 + expect(yield* Ref.get(requests)).toBe(2);
391 + expect(yield* Ref.get(b.bearers)).toEqual([]);
392 + }),
393 + ).pipe(Effect.provide(NodeHttpServer.layerTest)),
394 +);
395 +
396 it.effect("reads one stored link without asking the peer", () =>
397 Effect.scoped(
398 Effect.gen(function* () {
apps/server/src/peer/PeerLinks.ts
+7 -1
@@ -27,6 +27,7 @@ import * as Schema from "effect/Schema";
27 import * as SqlClient from "effect/sql/SqlClient";
28 import * as FetchHttpClient from "effect/http/FetchHttpClient";
29 import * as HttpClient from "effect/http/HttpClient";
30 +import * as HttpApi from "effect/http-api/HttpApi";
31 import * as HttpApiClient from "effect/http-api/HttpApiClient";
32 import { isLocalLoopbackHost, isTailnetHost } from "@t3tools/shared/hostClassification";
33
@@ -34,6 +35,11 @@ import * as ServerSecretStore from "../auth/ServerSecretStore.ts";
35 import * as ServerEnvironment from "../environment/ServerEnvironment.ts";
36 import * as ServerSettings from "../serverSettings.ts";
37
38 +const PeerHttpApi = HttpApi.make(EnvironmentHttpApi.identifier)
39 + .add(EnvironmentHttpApi.groups.metadata)
40 + .add(EnvironmentHttpApi.groups.mcpOAuth)
41 + .annotateMerge(EnvironmentHttpApi.annotations);
42 +
43 /** The redirect registered for a link. Nothing ever listens there: the code comes back in the response. */
44 const LINK_REDIRECT_URI = "http://127.0.0.1/t3code-peer-link";
45 /**
@@ -185,7 +191,7 @@ const make = Effect.gen(function* () {
191 );
192
193 const clientFor = (origin: string) =>
188 - HttpApiClient.make(EnvironmentHttpApi, { baseUrl: origin }).pipe(
194 + HttpApiClient.make(PeerHttpApi, { baseUrl: origin }).pipe(
195 Effect.provideService(HttpClient.HttpClient, peerHttpClient),
196 );
197