Skip to content

Commit 56dba87

Browse files
committed
fix(server): list peer links without waiting for reachability probes
1 parent 7195b69 commit 56dba87

4 files changed

Lines changed: 61 additions & 25 deletions

File tree

‎apps/server/src/peer/PeerLinks.test.ts‎

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { NodeHttpServer } from "@effect/platform-node";
22
import { type ExecutionEnvironmentDescriptor, OrchestratorMcpFailure } from "@t3tools/contracts";
33
import { expect, it } from "@effect/vitest";
44
import * as Context from "effect/Context";
5+
import * as Deferred from "effect/Deferred";
56
import * as Duration from "effect/Duration";
67
import * as Effect from "effect/Effect";
78
import * as Layer from "effect/Layer";
@@ -279,6 +280,53 @@ const serveRaw = <E, R>(
279280
),
280281
);
281282

283+
it.effect("lists stored outcomes without waiting for an unreachable peer", () =>
284+
Effect.scoped(
285+
Effect.gen(function* () {
286+
const b = yield* serveB(box);
287+
const a = yield* makeA(laptop);
288+
const pairing = yield* b.auth.issuePairingCredential();
289+
const linked = yield* a.links.link({
290+
url: b.url,
291+
pairingCode: pairing.credential,
292+
access: "auto",
293+
});
294+
const probed = yield* Deferred.make<void>();
295+
const release = yield* Deferred.make<void>();
296+
const asleep = yield* serveRaw(() =>
297+
Deferred.succeed(probed, undefined).pipe(
298+
Effect.andThen(Deferred.await(release)),
299+
Effect.as(HttpServerResponse.empty({ status: 503 })),
300+
),
301+
);
302+
yield* a.sql`UPDATE peer_environment_links SET urls_json = ${JSON.stringify([asleep])}`;
303+
304+
const listed = yield* Effect.raceFirst(
305+
a.links.list,
306+
Deferred.await(probed).pipe(Effect.as("blocked on peer")),
307+
);
308+
yield* Deferred.succeed(release, undefined);
309+
expect(listed).toEqual([{ ...linked, urls: [asleep], status: "reachable" }]);
310+
311+
yield* a.links.recordOutcome(box.environmentId, { error: "Peer did not answer" });
312+
expect(yield* a.links.list).toMatchObject([
313+
{
314+
status: "unreachable",
315+
lastError: "Peer did not answer",
316+
lastReachedAt: linked.lastReachedAt,
317+
},
318+
]);
319+
yield* a.links.recordOutcome(box.environmentId, { error: null });
320+
expect(yield* a.links.list).toMatchObject([{ status: "reachable", lastError: null }]);
321+
yield* a.sql`UPDATE peer_environment_links SET last_reached_at = NULL`;
322+
expect(yield* a.links.list).toMatchObject([{ status: "unreachable" }]);
323+
yield* TestClock.adjust(Duration.days(31));
324+
expect(yield* a.links.list).toMatchObject([{ status: "expired" }]);
325+
expect(yield* Deferred.isDone(probed)).toBe(false);
326+
}),
327+
).pipe(Effect.provide(NodeHttpServer.layerTest)),
328+
);
329+
282330
it.effect("never follows a peer's redirect with the pairing code", () =>
283331
Effect.scoped(
284332
Effect.gen(function* () {
@@ -335,6 +383,10 @@ it.effect("never sends a link's token to an address that stops answering as that
335383
expect(failed.message).toContain("Box did not answer");
336384
expect(yield* Ref.get(b.bearers)).toEqual([]);
337385
expect((yield* a.links.list).map((link) => link.status)).toEqual(["unreachable"]);
386+
387+
yield* Ref.set(b.descriptor, box);
388+
yield* probe(a);
389+
expect((yield* a.links.list).map((link) => link.status)).toEqual(["reachable"]);
338390
}),
339391
).pipe(Effect.provide(NodeHttpServer.layerTest)),
340392
);

‎apps/server/src/peer/PeerLinks.ts‎

Lines changed: 7 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ export class PeerLinks extends Context.Service<
7575
) => Effect.Effect<
7676
Option.Option<{ readonly environmentId: EnvironmentId; readonly label: string }>
7777
>;
78+
/** Stored outcomes and current expiry; does not probe peers. */
7879
readonly list: Effect.Effect<ReadonlyArray<PeerLinkSummary>, PeerLinkError>;
7980
/** The stored link, without asking the peer whether it answers. */
8081
readonly get: (
@@ -486,29 +487,17 @@ const make = Effect.gen(function* () {
486487
Effect.orElseSucceed(() => Option.none()),
487488
);
488489

489-
const statusOf = (link: PeerLink, now: DateTime.Utc) =>
490+
const statusOf = (link: PeerLink, now: DateTime.Utc): PeerLinkStatus =>
490491
DateTime.isLessThanOrEqualTo(link.expiresAt, now)
491-
? Effect.succeed<PeerLinkStatus>("expired")
492-
: Effect.findFirst(link.urls, (url) =>
493-
describe(url).pipe(
494-
Effect.map(
495-
(found) => Option.isSome(found) && found.value.environmentId === link.environmentId,
496-
),
497-
),
498-
).pipe(
499-
Effect.map((answered): PeerLinkStatus =>
500-
Option.isSome(answered) ? "reachable" : "unreachable",
501-
),
502-
);
492+
? "expired"
493+
: link.lastReachedAt !== null && link.lastError === null
494+
? "reachable"
495+
: "unreachable";
503496

504497
const list: PeerLinks["Service"]["list"] = Effect.gen(function* () {
505498
const links = yield* Effect.forEach(yield* readRows, toLink);
506499
const now = yield* DateTime.now;
507-
return yield* Effect.forEach(
508-
links,
509-
(link) => statusOf(link, now).pipe(Effect.map((status) => ({ ...link, status }))),
510-
{ concurrency: "unbounded" },
511-
);
500+
return links.map((link) => ({ ...link, status: statusOf(link, now) }));
512501
});
513502

514503
const get: PeerLinks["Service"]["get"] = (environmentId) =>

‎packages/client-runtime/src/state/peerLinks.ts‎

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ export function createPeerLinkEnvironmentAtoms<R, E>(
1313
const list = createEnvironmentRpcQueryAtomFamily(runtime, {
1414
label: "environment-data:peer-links:list",
1515
tag: WS_METHODS.peerLinksList,
16-
// Listing probes every linked environment, so a list stays fresh briefly.
1716
staleTimeMs: 10_000,
1817
});
1918
return {
@@ -53,11 +52,7 @@ export type PeerLinkHealth =
5352
| { readonly kind: "failing"; readonly detail: string }
5453
| { readonly kind: "expired" };
5554

56-
/**
57-
* Listing only checks that the peer answers, so a link whose last call failed
58-
* (the peer revoked it, say) is `failing` with the recorded reason; the next
59-
* successful call clears it.
60-
*/
55+
/** Stored outcomes describe the last call; expiry is checked against the client's clock too. */
6156
export function peerLinkHealth(link: PeerLinkSummary, now: DateTime.Utc): PeerLinkHealth {
6257
if (link.status === "expired" || DateTime.isLessThanOrEqualTo(link.expiresAt, now)) {
6358
return { kind: "expired" };

‎packages/contracts/src/peerLink.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ export const PeerLink = Schema.Struct({
2222
});
2323
export type PeerLink = typeof PeerLink.Type;
2424

25-
/** Whether a link can be used now, as worked out when it is listed. */
25+
/** Last recorded call outcome, with current expiry taking precedence; not a live probe. */
2626
export const PeerLinkStatus = Schema.Literals(["reachable", "unreachable", "expired"]);
2727
export type PeerLinkStatus = typeof PeerLinkStatus.Type;
2828

0 commit comments

Comments
 (0)