Skip to content

Commit 7115a0b

Browse files
saphidgithub-actions[bot]claude
authored
perf(server): connects no longer wait on editor and SSH target discovery (#14519)
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> Co-authored-by: Claude Opus 5.5 (1M context) <[email protected]> Co-authored-by: SAPHID <[email protected]>
1 parent 8473d15 commit 7115a0b

7 files changed

Lines changed: 422 additions & 78 deletions

File tree

‎apps/server/src/environment/RemoteOpenTargets.ts‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,10 @@ import * as Effect from "effect/Effect";
77
import * as Layer from "effect/Layer";
88
import * as ChildProcessSpawner from "effect/process/ChildProcessSpawner";
99

10+
import { makeStaleWhileRevalidate } from "./staleWhileRevalidate.ts";
11+
1012
const SSH_PORT = 22;
13+
const TARGETS_CACHE_TTL = "60 seconds";
1114

1215
export class RemoteOpenTargets extends Context.Service<
1316
RemoteOpenTargets,
@@ -21,7 +24,7 @@ export const make = Effect.gen(function* () {
2124
const spawner = yield* ChildProcessSpawner.ChildProcessSpawner;
2225
const net = yield* NetService.NetService;
2326

24-
const resolveTargets = Effect.gen(function* () {
27+
const discoverTargets = Effect.gen(function* () {
2528
// Check both loopback families: sshd can be bound IPv6-only.
2629
const sshdListening = yield* Effect.zipWith(
2730
net.hasListenerOnHost(SSH_PORT, "127.0.0.1"),
@@ -78,6 +81,10 @@ export const make = Effect.gen(function* () {
7881
return targets;
7982
});
8083

84+
// Advertised on every client connect; probing sshd and tailscaled is slow
85+
// on a loaded host, so serve the last result while it refreshes.
86+
const resolveTargets = yield* makeStaleWhileRevalidate(discoverTargets, TARGETS_CACHE_TTL);
87+
8188
return RemoteOpenTargets.of({
8289
resolveTargets: Effect.fn("RemoteOpenTargets.resolveTargets")(function () {
8390
return resolveTargets;
Lines changed: 177 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,177 @@
1+
import { assert, it } from "@effect/vitest";
2+
import * as Deferred from "effect/Deferred";
3+
import * as Effect from "effect/Effect";
4+
import * as Exit from "effect/Exit";
5+
import * as Fiber from "effect/Fiber";
6+
import * as Ref from "effect/Ref";
7+
import * as Scope from "effect/Scope";
8+
import * as TestClock from "effect/testing/TestClock";
9+
10+
import { makeStaleWhileRevalidate } from "./staleWhileRevalidate.ts";
11+
12+
it.effect("computes once, then answers from memory until the ttl lapses", () =>
13+
Effect.gen(function* () {
14+
const calls = yield* Ref.make(0);
15+
const read = yield* makeStaleWhileRevalidate(
16+
Ref.updateAndGet(calls, (count) => count + 1),
17+
"60 seconds",
18+
);
19+
20+
assert.equal(yield* read, 1);
21+
yield* TestClock.adjust("59 seconds");
22+
assert.equal(yield* read, 1);
23+
assert.equal(yield* Ref.get(calls), 1);
24+
}).pipe(Effect.scoped),
25+
);
26+
27+
it.effect("serves the stale value while one background refresh replaces it", () =>
28+
Effect.gen(function* () {
29+
const calls = yield* Ref.make(0);
30+
const release = yield* Deferred.make<void>();
31+
const started = yield* Deferred.make<void>();
32+
const refreshed = yield* Deferred.make<Fiber.Fiber<unknown, unknown>>();
33+
const read = yield* makeStaleWhileRevalidate(
34+
Effect.gen(function* () {
35+
const call = yield* Ref.updateAndGet(calls, (count) => count + 1);
36+
if (call > 1) {
37+
yield* Deferred.succeed(started, undefined);
38+
yield* Deferred.await(release);
39+
yield* Deferred.succeed(refreshed, yield* Effect.fiber);
40+
}
41+
return call;
42+
}),
43+
"60 seconds",
44+
);
45+
46+
assert.equal(yield* read, 1);
47+
yield* TestClock.adjust("61 seconds");
48+
// The refresh is blocked, yet callers are answered immediately and only
49+
// one refresh starts however many callers arrive.
50+
assert.equal(yield* read, 1);
51+
assert.equal(yield* read, 1);
52+
yield* Deferred.await(started);
53+
assert.equal(yield* Ref.get(calls), 2);
54+
55+
yield* Deferred.succeed(release, undefined);
56+
yield* Fiber.await(yield* Deferred.await(refreshed));
57+
assert.equal(yield* read, 2);
58+
}),
59+
);
60+
61+
it.effect("shares one first scan that an interrupted caller does not cancel", () =>
62+
Effect.gen(function* () {
63+
const calls = yield* Ref.make(0);
64+
const release = yield* Deferred.make<void>();
65+
const started = yield* Deferred.make<void>();
66+
const read = yield* makeStaleWhileRevalidate(
67+
Ref.updateAndGet(calls, (count) => count + 1).pipe(
68+
Effect.tap(() => Deferred.succeed(started, undefined)),
69+
Effect.tap(() => Deferred.await(release)),
70+
),
71+
"60 seconds",
72+
);
73+
74+
// A connect that times out or disconnects mid-scan leaves the scan running.
75+
const interrupted = yield* read.pipe(Effect.forkChild);
76+
yield* Deferred.await(started);
77+
yield* Fiber.interrupt(interrupted);
78+
79+
const next = yield* read.pipe(Effect.forkChild);
80+
yield* Deferred.succeed(release, undefined);
81+
assert.equal(yield* Fiber.join(next), 1);
82+
assert.equal(yield* Ref.get(calls), 1);
83+
}).pipe(Effect.scoped),
84+
);
85+
86+
it.effect("starts over after a failed first scan", () =>
87+
Effect.gen(function* () {
88+
const calls = yield* Ref.make(0);
89+
const read = yield* makeStaleWhileRevalidate(
90+
Ref.updateAndGet(calls, (count) => count + 1).pipe(
91+
Effect.filterOrElse(
92+
(call) => call !== 1,
93+
() => Effect.die("probe crashed"),
94+
),
95+
),
96+
"60 seconds",
97+
);
98+
99+
assert.isTrue(Exit.isFailure(yield* Effect.exit(read)));
100+
assert.equal(yield* read, 2);
101+
}).pipe(Effect.scoped),
102+
);
103+
104+
it.effect("abandons a hung refresh so a later call can start another", () =>
105+
Effect.gen(function* () {
106+
const calls = yield* Ref.make(0);
107+
const started = yield* Deferred.make<Fiber.Fiber<unknown, unknown>>();
108+
const interrupted = yield* Deferred.make<void>();
109+
const refreshed = yield* Deferred.make<Fiber.Fiber<unknown, unknown>>();
110+
const read = yield* makeStaleWhileRevalidate(
111+
Ref.updateAndGet(calls, (count) => count + 1).pipe(
112+
Effect.flatMap((call) =>
113+
call === 2
114+
? Effect.fiber.pipe(
115+
Effect.flatMap((fiber) => Deferred.succeed(started, fiber)),
116+
Effect.andThen(Effect.never),
117+
Effect.onInterrupt(() => Deferred.succeed(interrupted, undefined)),
118+
)
119+
: Effect.succeed(call).pipe(
120+
Effect.tap(() =>
121+
call === 3
122+
? Effect.fiber.pipe(
123+
Effect.flatMap((fiber) => Deferred.succeed(refreshed, fiber)),
124+
)
125+
: Effect.void,
126+
),
127+
),
128+
),
129+
),
130+
"60 seconds",
131+
);
132+
133+
assert.equal(yield* read, 1);
134+
yield* TestClock.adjust("61 seconds");
135+
assert.equal(yield* read, 1);
136+
yield* Deferred.await(started);
137+
assert.equal(yield* Ref.get(calls), 2);
138+
139+
// The hung refresh times out, releasing the claim for the next caller.
140+
yield* TestClock.adjust("30 seconds");
141+
yield* Deferred.await(interrupted);
142+
yield* Fiber.await(yield* Deferred.await(started));
143+
assert.equal(yield* read, 1);
144+
yield* Fiber.await(yield* Deferred.await(refreshed));
145+
assert.equal(yield* Ref.get(calls), 3);
146+
assert.equal(yield* read, 3);
147+
}).pipe(Effect.scoped),
148+
);
149+
150+
it.effect("interrupts a background refresh when the owning scope closes", () =>
151+
Effect.gen(function* () {
152+
const interrupted = yield* Deferred.make<void>();
153+
const started = yield* Deferred.make<void>();
154+
const scope = yield* Scope.make();
155+
const calls = yield* Ref.make(0);
156+
const read = yield* makeStaleWhileRevalidate(
157+
Ref.updateAndGet(calls, (count) => count + 1).pipe(
158+
Effect.filterOrElse(
159+
(call) => call === 1,
160+
() =>
161+
Deferred.succeed(started, undefined).pipe(
162+
Effect.andThen(Effect.never),
163+
Effect.onInterrupt(() => Deferred.succeed(interrupted, undefined)),
164+
),
165+
),
166+
),
167+
"60 seconds",
168+
).pipe(Scope.provide(scope));
169+
170+
yield* read;
171+
yield* TestClock.adjust("61 seconds");
172+
yield* read;
173+
yield* Deferred.await(started);
174+
yield* Scope.close(scope, Exit.void);
175+
yield* Deferred.await(interrupted);
176+
}),
177+
);
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
import * as Clock from "effect/Clock";
2+
import * as Deferred from "effect/Deferred";
3+
import * as Duration from "effect/Duration";
4+
import * as Effect from "effect/Effect";
5+
import * as Exit from "effect/Exit";
6+
import * as Ref from "effect/Ref";
7+
8+
const REFRESH_TIMEOUT = Duration.seconds(30);
9+
10+
type Entry<A> =
11+
| { readonly _tag: "Empty" }
12+
| { readonly _tag: "Scanning"; readonly scan: Deferred.Deferred<A> }
13+
| {
14+
readonly _tag: "Ready";
15+
readonly value: A;
16+
readonly expiresAtNanos: bigint;
17+
readonly refreshing: boolean;
18+
};
19+
20+
// Shared scans survive caller cancellation; monotonic TTL bounds cache age.
21+
// Warm reads return stale data while a scope-owned, bounded refresh runs.
22+
export const makeStaleWhileRevalidate = Effect.fn("StaleWhileRevalidate.make")(function* <A>(
23+
discover: Effect.Effect<A>,
24+
ttl: Duration.Input,
25+
) {
26+
const ttlNanos = Duration.toNanosUnsafe(Duration.fromInputUnsafe(ttl));
27+
const scope = yield* Effect.scope;
28+
const state = yield* Ref.make<Entry<A>>({ _tag: "Empty" });
29+
30+
const settle = Effect.fn("StaleWhileRevalidate.settle")(function* (exit: Exit.Exit<A>) {
31+
const now = yield* Clock.monotonicTimeNanos;
32+
yield* Ref.update(state, (entry): Entry<A> =>
33+
Exit.isSuccess(exit)
34+
? {
35+
_tag: "Ready",
36+
value: exit.value,
37+
expiresAtNanos: now + ttlNanos,
38+
refreshing: false,
39+
}
40+
: entry._tag === "Ready"
41+
? { ...entry, refreshing: false }
42+
: { _tag: "Empty" },
43+
);
44+
});
45+
const fork = (scan: Effect.Effect<unknown>) =>
46+
scan.pipe(Effect.ignoreCause({ log: true }), Effect.interruptible, Effect.forkIn(scope));
47+
const firstScan = (scan: Deferred.Deferred<A>) =>
48+
fork(
49+
discover.pipe(
50+
Effect.onExit((exit) => settle(exit).pipe(Effect.andThen(Deferred.done(scan, exit)))),
51+
),
52+
);
53+
const refresh = fork(discover.pipe(Effect.onExit(settle), Effect.timeoutOption(REFRESH_TIMEOUT)));
54+
55+
return Effect.gen(function* () {
56+
const now = yield* Clock.monotonicTimeNanos;
57+
// Claiming a scan and starting it are one uninterruptible step: a caller
58+
// interrupted between them would leave a claimed scan that never runs.
59+
const result = yield* Ref.modify(
60+
state,
61+
(entry): readonly [readonly [Effect.Effect<unknown>, Effect.Effect<A>], Entry<A>] => {
62+
switch (entry._tag) {
63+
case "Empty": {
64+
const scan = Deferred.makeUnsafe<A>();
65+
return [[firstScan(scan), Deferred.await(scan)], { _tag: "Scanning", scan }];
66+
}
67+
case "Scanning":
68+
return [[Effect.void, Deferred.await(entry.scan)], entry];
69+
case "Ready":
70+
return entry.refreshing || entry.expiresAtNanos > now
71+
? [[Effect.void, Effect.succeed(entry.value)], entry]
72+
: [[refresh, Effect.succeed(entry.value)], { ...entry, refreshing: true }];
73+
}
74+
},
75+
).pipe(
76+
Effect.flatMap(([start, result]) => Effect.as(start, result)),
77+
Effect.uninterruptible,
78+
);
79+
return yield* result;
80+
});
81+
});

‎apps/server/src/process/externalLauncher.test.ts‎

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import * as ConfigProvider from "effect/ConfigProvider";
55
import * as Deferred from "effect/Deferred";
66
import * as Cause from "effect/Cause";
77
import * as Effect from "effect/Effect";
8+
import * as TestClock from "effect/testing/TestClock";
89
import * as Exit from "effect/Exit";
910
import * as Fiber from "effect/Fiber";
1011
import * as FileSystem from "effect/FileSystem";
@@ -714,6 +715,79 @@ it.effect.skipIf(windowsHost)("ignores unusable app bundles and keeps PATH launc
714715
}).pipe(Effect.scoped, Effect.provide(NodeServices.layer)),
715716
);
716717

718+
it.effect(
719+
"memoizes editor discovery and refreshes in the background after the cache window",
720+
() => {
721+
let statCalls = 0;
722+
const rescanned = Deferred.makeUnsafe<void>();
723+
let initialScanDone = false;
724+
const fileInfo = { type: "File" } as FileSystem.File.Info;
725+
const layerLauncher = ExternalLauncher.layer.pipe(
726+
Layer.provide(
727+
Layer.mergeAll(
728+
FileSystem.layerNoop({
729+
stat: () =>
730+
Effect.sync(() => {
731+
statCalls += 1;
732+
return fileInfo;
733+
}).pipe(
734+
Effect.tap(() =>
735+
initialScanDone ? Deferred.succeed(rescanned, undefined) : Effect.void,
736+
),
737+
),
738+
}),
739+
Path.layer,
740+
Layer.succeed(
741+
ChildProcessSpawner.ChildProcessSpawner,
742+
ChildProcessSpawner.make(() => Effect.sync(() => makeMockDetachedHandle())),
743+
),
744+
),
745+
),
746+
);
747+
748+
return Effect.gen(function* () {
749+
const launcher = yield* ExternalLauncher.ExternalLauncher;
750+
751+
const first = yield* launcher.resolveAvailableEditors();
752+
assert.equal(first.includes("vscode"), true);
753+
const statCallsAfterFirstScan = statCalls;
754+
initialScanDone = true;
755+
assert.isAbove(statCallsAfterFirstScan, 0);
756+
757+
// Past the shared command-resolution cache TTL (30s) but within the
758+
// discovery cache window: the memoized set is reused without any scan.
759+
yield* TestClock.adjust("31 seconds");
760+
const second = yield* launcher.resolveAvailableEditors();
761+
assert.deepEqual([...second], [...first]);
762+
assert.equal(statCalls, statCallsAfterFirstScan);
763+
764+
// Past the discovery cache window the next call still answers from the
765+
// memoized set and rescans in the background.
766+
yield* TestClock.adjust("30 seconds");
767+
const third = yield* launcher.resolveAvailableEditors();
768+
assert.deepEqual([...third], [...first]);
769+
yield* Deferred.await(rescanned);
770+
assert.isAbove(statCalls, statCallsAfterFirstScan);
771+
}).pipe(
772+
Effect.provide(
773+
Layer.mergeAll(
774+
layerLauncher,
775+
Layer.succeed(HostProcess.Platform, "win32"),
776+
ConfigProvider.layer(
777+
ConfigProvider.fromEnv({
778+
env: {
779+
PATH: "C:\\t3-editor-discovery-cache-test",
780+
PATHEXT: ".COM;.EXE;.BAT;.CMD",
781+
},
782+
}),
783+
),
784+
TestClock.layer(),
785+
),
786+
),
787+
);
788+
},
789+
);
790+
717791
it.effect("keeps scanning after the caller is interrupted and shares that scan", () => {
718792
const fileInfo = { type: "File" } as FileSystem.File.Info;
719793
const release = Deferred.makeUnsafe<void>();

0 commit comments

Comments
 (0)