Open History to see the last commit
1import type { OrchestrationShellSnapshot } from "@v12/contracts";
2import * as Cause from "effect/Cause";
3import * as Context from "effect/Context";
4import * as Effect from "effect/Effect";
5import * as Layer from "effect/Layer";
6import * as Option from "effect/Option";
7import { HttpClient } from "effect/unstable/http";
8
9import type { PreparedConnection } from "../connection/model.ts";
10import { environmentEndpointUrl } from "../environment/endpoint.ts";
11import { ManagedRelayDpopSigner } from "../relay/managedRelay.ts";
12import { executeEnvironmentHttpRequest, makeEnvironmentHttpApiClient } from "../rpc/http.ts";
13import { buildEnvironmentAuthHeaders, withEnvironmentCredentials } from "./environmentHttpAuth.ts";
14
15// Bounded so a pathologically slow endpoint cannot block the (cheaper) socket
16// fallback for long. The cached shell renders while this runs.
17const DEFAULT_SHELL_SNAPSHOT_TIMEOUT_MS = 6_000;
18
19/**
20 * Load the environment shell snapshot (projects + thread shells) over HTTP
21 * instead of as the WebSocket subscription's first frame. The response is
22 * gzip-compressible by the transport and keeps the (potentially large) list off
23 * the socket.
24 */
25export const fetchEnvironmentShellSnapshot = Effect.fn(
26 "clientRuntime.state.fetchEnvironmentShellSnapshot",
27)(function* (input: {
28 readonly prepared: PreparedConnection;
29 readonly signer: Option.Option<ManagedRelayDpopSigner["Service"]>;
30 readonly timeoutMs?: number;
31}) {
32 const requestUrl = environmentEndpointUrl(input.prepared.httpBaseUrl, "/api/orchestration/shell");
33 const client = yield* makeEnvironmentHttpApiClient(input.prepared.httpBaseUrl);
34 const headers = yield* buildEnvironmentAuthHeaders(
35 input.prepared.httpAuthorization,
36 "GET",
37 requestUrl,
38 input.signer,
39 );
40 return yield* executeEnvironmentHttpRequest(
41 requestUrl,
42 input.timeoutMs ?? DEFAULT_SHELL_SNAPSHOT_TIMEOUT_MS,
43 withEnvironmentCredentials(
44 input.prepared.httpAuthorization,
45 client.orchestration.shellSnapshot({ headers }),
46 ),
47 );
48});
49
50/**
51 * Loads the environment shell snapshot over HTTP, returning `Option.none()` when
52 * it cannot be loaded (so the caller falls back to the socket-embedded snapshot).
53 * Decouples the shell state machine from the underlying HTTP + DPoP details and
54 * keeps them out of test contexts.
55 */
56export class ShellSnapshotLoader extends Context.Service<
57 ShellSnapshotLoader,
58 {
59 readonly load: (
60 prepared: PreparedConnection,
61 ) => Effect.Effect<Option.Option<OrchestrationShellSnapshot>>;
62 }
63>()("@v12/client-runtime/state/shellSnapshotHttp/ShellSnapshotLoader") {}
64
65export const shellSnapshotLoaderLayer: Layer.Layer<
66 ShellSnapshotLoader,
67 never,
68 HttpClient.HttpClient
69> = Layer.effect(
70 ShellSnapshotLoader,
71 Effect.gen(function* () {
72 const httpClient = yield* HttpClient.HttpClient;
73 // Resolve the DPoP signer optionally: it is only needed for relay/DPoP
74 // connections, so the loader must not hard-require it.
75 const signer = yield* Effect.serviceOption(ManagedRelayDpopSigner);
76 return ShellSnapshotLoader.of({
77 load: (prepared: PreparedConnection) =>
78 fetchEnvironmentShellSnapshot({ prepared, signer }).pipe(
79 Effect.map(Option.some<OrchestrationShellSnapshot>),
80 Effect.provideService(HttpClient.HttpClient, httpClient),
81 Effect.catchCause((cause) =>
82 Effect.logWarning(
83 "Could not load the environment shell snapshot over HTTP; using the socket snapshot instead.",
84 ).pipe(
85 Effect.annotateLogs({ cause: Cause.pretty(cause) }),
86 Effect.as(Option.none<OrchestrationShellSnapshot>()),
87 ),
88 ),
89 ),
90 });
91 }),
92);