Skip to content

Commit 9516f24

Browse files
committed
perf: reduce environment connection latency
1 parent 125b619 commit 9516f24

30 files changed

Lines changed: 976 additions & 106 deletions

‎apps/mobile/src/connection/environment-cache-store.test.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -498,10 +498,11 @@ describe("mobile SQLite environment cache store", () => {
498498
Effect.provideService(MobileDatabase.MobileDatabase, memory.database),
499499
);
500500

501-
yield* store.saveShell(ENVIRONMENT_ID, SHELL_SNAPSHOT);
501+
yield* store.saveShell(ENVIRONMENT_ID, SHELL_SNAPSHOT, "1.0.0");
502502
yield* store.saveThread(ENVIRONMENT_ID, THREAD_SNAPSHOT);
503503

504504
const shell = Option.getOrThrow(yield* store.loadShell(ENVIRONMENT_ID));
505+
expect(shell.verifiedServerVersion).toBe("1.0.0");
505506
const thread = Option.getOrThrow(yield* store.loadThread(ENVIRONMENT_ID, THREAD_ID));
506507

507508
expect(DateTime.formatIso(shell.threads[0]!.updatedAt)).toBe("2026-07-29T12:00:00.000Z");

‎apps/mobile/src/connection/environment-cache-store.ts‎

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {
55
encodeStoredThreadSnapshot,
66
Persistence,
77
makeStoredShellSnapshotEncoder,
8+
withVerifiedServerVersion,
89
} from "@t3tools/client-runtime/platform";
910
import { type EnvironmentId, ServerConfig, VcsListRefsResult } from "@t3tools/contracts";
1011
import * as Effect from "effect/Effect";
@@ -96,7 +97,7 @@ export const make = Effect.fn("MobileEnvironmentCacheStore.make")(function* () {
9697
const database = yield* MobileDatabase.MobileDatabase;
9798
attachProjectFaviconDatabase(database);
9899
const encodeStoredShellSnapshot = makeStoredShellSnapshotEncoder();
99-
return Persistence.EnvironmentCacheStore.of({
100+
const store = Persistence.EnvironmentCacheStore.of({
100101
loadShell: Effect.fn("MobileEnvironmentCache.loadShell")((environmentId) =>
101102
loadDecodedCache({
102103
database,
@@ -106,7 +107,9 @@ export const make = Effect.fn("MobileEnvironmentCacheStore.make")(function* () {
106107
operation: "load-shell",
107108
decode: decodeStoredShellSnapshot,
108109
select: (stored) =>
109-
stored.environmentId === environmentId ? Option.some(stored.snapshot) : Option.none(),
110+
stored.environmentId === environmentId
111+
? Option.some(withVerifiedServerVersion(stored))
112+
: Option.none(),
110113
}).pipe(
111114
Effect.zipWith(
112115
Effect.promise(() => projectFaviconDatabaseCache.hydrate()),
@@ -115,16 +118,25 @@ export const make = Effect.fn("MobileEnvironmentCacheStore.make")(function* () {
115118
),
116119
),
117120
),
118-
saveShell: Effect.fn("MobileEnvironmentCache.saveShell")(function* (environmentId, snapshot) {
119-
const payload = yield* encodeStoredShellSnapshot({
120-
schemaVersion: ORCHESTRATION_CACHE_SCHEMA_VERSION,
121-
environmentId,
122-
snapshot,
123-
}).pipe(Effect.mapError((cause) => persistenceError("save-shell", cause)));
124-
yield* database
125-
.saveCache(environmentId, "shell", "snapshot", ORCHESTRATION_CACHE_SCHEMA_VERSION, payload)
126-
.pipe(Effect.mapError(mapDatabaseError("save-shell")));
127-
}),
121+
saveShell: Effect.fn("MobileEnvironmentCache.saveShell")(
122+
function* (environmentId, snapshot, verifiedServerVersion) {
123+
const payload = yield* encodeStoredShellSnapshot({
124+
schemaVersion: ORCHESTRATION_CACHE_SCHEMA_VERSION,
125+
environmentId,
126+
snapshot,
127+
...(verifiedServerVersion === undefined ? {} : { verifiedServerVersion }),
128+
}).pipe(Effect.mapError((cause) => persistenceError("save-shell", cause)));
129+
yield* database
130+
.saveCache(
131+
environmentId,
132+
"shell",
133+
"snapshot",
134+
ORCHESTRATION_CACHE_SCHEMA_VERSION,
135+
payload,
136+
)
137+
.pipe(Effect.mapError(mapDatabaseError("save-shell")));
138+
},
139+
),
128140
loadThread: Effect.fn("MobileEnvironmentCache.loadThread")((environmentId, threadId) =>
129141
loadDecodedCache({
130142
database,
@@ -229,6 +241,7 @@ export const make = Effect.fn("MobileEnvironmentCacheStore.make")(function* () {
229241
),
230242
),
231243
});
244+
return Persistence.shareServerConfigReads(store);
232245
});
233246

234247
export const layer = Layer.effect(Persistence.EnvironmentCacheStore, make());
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import * as Layer from "effect/Layer";
2+
import * as Tracer from "effect/Tracer";
3+
4+
// Simulators share the host clock, so these lines line up with the server's trace file.
5+
const MIN_LOGGED_SPAN_NANOS = 1_000_000n;
6+
7+
class LoggedSpan extends Tracer.NativeSpan {
8+
override end(endTime: bigint, exit: Parameters<Tracer.NativeSpan["end"]>[1]): void {
9+
super.end(endTime, exit);
10+
if (endTime - this.startTime < MIN_LOGGED_SPAN_NANOS) return;
11+
console.log(
12+
`[t3-span] ${JSON.stringify({
13+
name: this.name,
14+
startMs: Number(this.startTime / 1_000_000n),
15+
endMs: Number(endTime / 1_000_000n),
16+
ok: exit._tag === "Success",
17+
...(this._attributes === undefined
18+
? {}
19+
: { attributes: Object.fromEntries(this._attributes) }),
20+
})}`,
21+
);
22+
}
23+
}
24+
25+
/** Dev builds started with `EXPO_PUBLIC_T3CODE_LOG_SPANS=1` print every span of 1 ms or more. */
26+
export const layer =
27+
__DEV__ && process.env.EXPO_PUBLIC_T3CODE_LOG_SPANS === "1"
28+
? Layer.succeed(Tracer.Tracer, Tracer.make({ span: (options) => new LoggedSpan(options) }))
29+
: Layer.empty;

‎apps/mobile/src/lib/runtime.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { layerRemoteHttpClient } from "@t3tools/client-runtime/rpc";
88
import * as Dpop from "../features/cloud/dpop";
99
import * as ManagedRelayLayer from "../features/cloud/managedRelayLayer";
1010
import { resolveCloudPublicConfig } from "../features/cloud/publicConfig";
11+
import * as DevSpanLog from "../features/observability/devSpanLog";
1112
import * as Tracing from "../features/observability/tracing";
1213
import * as Persistence from "../persistence/layer";
1314
import { disposeOnFoundationReplace, type FoundationHotModule } from "./foundation-fast-refresh";
@@ -33,6 +34,7 @@ const layerRuntime = Layer.merge(
3334
ManagedRelayLayer.layer(configuredRelayUrl()),
3435
Socket.layerWebSocketConstructorGlobal,
3536
).pipe(
37+
Layer.provideMerge(DevSpanLog.layer),
3638
Layer.provideMerge(Dpop.layer),
3739
Layer.provideMerge(layerHttpClient),
3840
Layer.provideMerge(Tracing.layer.pipe(Layer.provide(layerHttpClient))),

‎apps/mobile/src/persistence/mobile-secure-storage.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ const make = MobileSecureStorage.of({
3333
Effect.tryPromise({
3434
try: () => SecureStore.getItemAsync(key),
3535
catch: (cause) => new MobileSecureStorageError({ operation: "read", key, cause }),
36-
}),
36+
}).pipe(Effect.tap(() => Effect.annotateCurrentSpan("secure_storage.key", key))),
3737
),
3838
setItem: Effect.fn("MobileSecureStorage.setItem")((key, value) =>
3939
Effect.tryPromise({

‎apps/mobile/src/persistence/mobile-storage.test.ts‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,10 @@ describe("mobile device identity", () => {
6262
const storage = yield* makeStorage;
6363

6464
expect(yield* storage.loadOrCreateAgentAwarenessDeviceId).toBe("existing-device");
65+
// Every connect asks again; only the first may pay for a keychain read.
66+
expect(yield* storage.loadOrCreateAgentAwarenessDeviceId).toBe("existing-device");
67+
expect(yield* storage.loadAgentAwarenessDeviceId).toBe("existing-device");
68+
expect(getItemAsync).toHaveBeenCalledTimes(1);
6569
expect(uuidv4).not.toHaveBeenCalled();
6670
expect(setItemAsync).not.toHaveBeenCalled();
6771
}),

‎apps/mobile/src/persistence/mobile-storage.ts‎

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -125,20 +125,28 @@ export const make = Effect.fn("MobileStorage.make")(function* () {
125125
yield* secureStorage.setItem(key, encoded);
126126
});
127127

128+
// The id never changes once written, and every connect reads it; a keychain read can take
129+
// hundreds of milliseconds while the app starts.
130+
let knownDeviceId: string | null = null;
128131
const loadOrCreateAgentAwarenessDeviceId = Effect.gen(function* () {
132+
if (knownDeviceId !== null) return knownDeviceId;
129133
const existing = yield* secureStorage.getItem(AGENT_AWARENESS_DEVICE_ID_KEY);
130-
if (existing?.trim()) return existing;
134+
if (existing?.trim()) return (knownDeviceId = existing);
131135
const deviceId = yield* Effect.tryPromise({
132136
try: () => import("../lib/uuid").then(({ uuidv4 }) => uuidv4()),
133137
catch: (cause) => new MobileDeviceIdGenerationError({ cause }),
134138
});
135139
yield* secureStorage.setItem(AGENT_AWARENESS_DEVICE_ID_KEY, deviceId);
136-
return deviceId;
140+
return (knownDeviceId = deviceId);
137141
}).pipe(deviceIdLock.withPermits(1));
138142

139-
const loadAgentAwarenessDeviceId = secureStorage
140-
.getItem(AGENT_AWARENESS_DEVICE_ID_KEY)
141-
.pipe(Effect.map((existing) => (existing?.trim() ? existing : null)));
143+
const loadAgentAwarenessDeviceId = Effect.suspend(() =>
144+
knownDeviceId !== null
145+
? Effect.succeed(knownDeviceId)
146+
: secureStorage
147+
.getItem(AGENT_AWARENESS_DEVICE_ID_KEY)
148+
.pipe(Effect.map((existing) => (existing?.trim() ? (knownDeviceId = existing) : null))),
149+
);
142150

143151
const loadAgentAwarenessRegistrationRecord = readJson<AgentAwarenessRegistrationRecord>(
144152
AGENT_AWARENESS_REGISTRATION_KEY,

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

Lines changed: 29 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,13 @@ import {
2424
isWildcardHost,
2525
VIRTUAL_INTERFACE,
2626
} from "../startupAccess.ts";
27+
import { makeBackgroundRefreshed } from "./backgroundRefresh.ts";
2728

2829
type NetworkInterfacesMap = ReturnType<typeof NodeOS.networkInterfaces>;
2930

3031
// Stay below the config discovery timeout so slow Tailscale drops only the tailnet name.
3132
const TAILSCALE_ENDPOINT_TIMEOUT = Duration.seconds(3);
33+
const SERVED_URL_MAX_AGE = Duration.seconds(30);
3234

3335
export class DirectEndpoints extends Context.Service<
3436
DirectEndpoints,
@@ -119,6 +121,30 @@ export const make = Effect.gen(function* () {
119121
: Effect.succeed(false);
120122
yield* Effect.forkDetach(onCloudVm);
121123

124+
// Serve setup can fail without stopping startup; advertise only after it answers as this server.
125+
const servedUrl = yield* makeBackgroundRefreshed(
126+
readTailscaleStatus.pipe(
127+
Effect.map((status) => status.magicDnsName),
128+
Effect.flatMap((magicDnsName) => {
129+
if (magicDnsName === null) return Effect.succeed(null);
130+
const httpBaseUrl = buildTailscaleHttpsBaseUrl({
131+
magicDnsName,
132+
servePort: config.tailscaleServePort,
133+
});
134+
return probeTailscaleHttpsEndpoint({ baseUrl: httpBaseUrl }).pipe(
135+
Effect.map((reachable) => (reachable ? httpBaseUrl : null)),
136+
);
137+
}),
138+
Effect.timeoutOption(TAILSCALE_ENDPOINT_TIMEOUT),
139+
Effect.map(Option.flatMap(Option.fromNullishOr)),
140+
Effect.orElseSucceed(() => Option.none<string>()),
141+
Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, spawner),
142+
Effect.provideService(HttpClient.HttpClient, httpClient),
143+
),
144+
SERVED_URL_MAX_AGE,
145+
{ warm: config.tailscaleServeEnabled },
146+
);
147+
122148
const resolve = Effect.gen(function* () {
123149
const endpoints = [
124150
...resolveBoundEndpoints({
@@ -128,28 +154,10 @@ export const make = Effect.gen(function* () {
128154
onCloudVm: yield* onCloudVm,
129155
}),
130156
];
131-
// Serve setup can fail without stopping startup; advertise only after it answers as this server.
132157
if (config.tailscaleServeEnabled) {
133-
const servedUrl = yield* readTailscaleStatus.pipe(
134-
Effect.map((status) => status.magicDnsName),
135-
Effect.flatMap((magicDnsName) => {
136-
if (magicDnsName === null) return Effect.succeed(null);
137-
const httpBaseUrl = buildTailscaleHttpsBaseUrl({
138-
magicDnsName,
139-
servePort: config.tailscaleServePort,
140-
});
141-
return probeTailscaleHttpsEndpoint({ baseUrl: httpBaseUrl }).pipe(
142-
Effect.map((reachable) => (reachable ? httpBaseUrl : null)),
143-
);
144-
}),
145-
Effect.timeoutOption(TAILSCALE_ENDPOINT_TIMEOUT),
146-
Effect.map(Option.flatMap(Option.fromNullishOr)),
147-
Effect.orElseSucceed(() => Option.none<string>()),
148-
Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, spawner),
149-
Effect.provideService(HttpClient.HttpClient, httpClient),
150-
);
151-
if (Option.isSome(servedUrl)) {
152-
endpoints.push({ kind: "tailnet", httpBaseUrl: servedUrl.value });
158+
const served = yield* servedUrl;
159+
if (Option.isSome(served)) {
160+
endpoints.push({ kind: "tailnet", httpBaseUrl: served.value });
153161
}
154162
}
155163
return endpoints;

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,15 +68,16 @@ const resolveTargets = (input: {
6868
readonly hostname: string;
6969
readonly username?: string | null;
7070
}) => {
71+
// The service warms its cache while the layer builds, so host facts must reach construction.
7172
const resolve = Effect.flatMap(RemoteOpenTargets.RemoteOpenTargets, (service) =>
7273
service.resolveTargets(),
7374
).pipe(
74-
Effect.provideService(HostProcess.Hostname, input.hostname),
7575
Effect.provide(
7676
RemoteOpenTargets.layer.pipe(
7777
Layer.provide(Layer.mergeAll(layerNet(input.sshd), layerSpawner(input.tailscale))),
7878
),
7979
),
80+
Effect.provideService(HostProcess.Hostname, input.hostname),
8081
);
8182

8283
return input.username === undefined

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,15 @@ import * as HostProcess from "@t3tools/shared/HostProcess";
33
import * as NetService from "@t3tools/shared/Net";
44
import { readTailscaleSshEnabled, readTailscaleStatus } from "@t3tools/tailscale";
55
import * as Context from "effect/Context";
6+
import * as Duration from "effect/Duration";
67
import * as Effect from "effect/Effect";
78
import * as Layer from "effect/Layer";
89
import * as ChildProcessSpawner from "effect/process/ChildProcessSpawner";
910

11+
import { makeBackgroundRefreshed } from "./backgroundRefresh.ts";
12+
1013
const SSH_PORT = 22;
14+
const TARGETS_MAX_AGE = Duration.seconds(30);
1115

1216
export class RemoteOpenTargets extends Context.Service<
1317
RemoteOpenTargets,
@@ -77,10 +81,13 @@ export const make = Effect.gen(function* () {
7781

7882
return targets;
7983
});
84+
const cachedTargets = yield* makeBackgroundRefreshed(resolveTargets, TARGETS_MAX_AGE, {
85+
warm: true,
86+
});
8087

8188
return RemoteOpenTargets.of({
8289
resolveTargets: Effect.fn("RemoteOpenTargets.resolveTargets")(function () {
83-
return resolveTargets;
90+
return cachedTargets;
8491
}),
8592
});
8693
});

0 commit comments

Comments
 (0)