History

Load the code-execution stack on demand in the MCP session DO (#1699)

Rhys Sullivan committed 2 months ago

Commit f08e84d1 parent 4cc5c4d1 file · +25 −3Browse files

Unified · Split

apps/cloud/src/mcp/session-durable-object.ts+25−3
@@ -66,9 +66,6 @@ import { · 65 unmodified lines
6666 type DrizzleDb,
6767 type DbServiceShape,
6868 } from "../db/db";
69−import { makeExecutionStack } from "../engine/execution-stack";
70−import { preloadQuickJs } from "../quickjs";
71−import { CloudMeteredExecutionStackLayer } from "../engine/execution-stack-metered";
7269 import { AutumnService } from "../extensions/billing/service";
7370 import { DoTelemetryLive, flushTracerProvider } from "../observability/telemetry";
7471 import {
@@ -242,6 +239,31 @@ export class McpSessionDOSqlite extends McpAgentSessionDOBase<Env, CloudSessionD · 167 unmodified lines
242239 ): Effect.Effect<BuiltMcpServer> {
243240 const self = this;
244241 return Effect.gen(function* () {
242+ // Imported here rather than at module scope. Cloudflare requires a
243+ // Durable Object class to be exported from the Worker entry, so every
244+ // static import this module makes is evaluated by *every* cold isolate —
245+ // including the ones that only render a page or forward a passthrough
246+ // proxy and never open an MCP session. These three roots pull the whole
247+ // code-execution stack (sucrase, ajv, QuickJS-WASM): measured at 1.9 MB
248+ // of the Worker's startup closure, for code only a real session runs.
249+ // `apps/cloud/scripts/start-closure.mjs` reports that number and will
250+ // show it moving back if these become static again.
251+ const [{ preloadQuickJs }, { makeExecutionStack }, { CloudMeteredExecutionStackLayer }] =
252+ yield* Effect.promise(
253+ () =>
254+ Promise.all([
255+ import("../quickjs"),
256+ import("../engine/execution-stack"),
257+ import("../engine/execution-stack-metered"),
258+ ]) as Promise<
259+ [
260+ typeof import("../quickjs"),
261+ typeof import("../engine/execution-stack"),
262+ typeof import("../engine/execution-stack-metered"),
263+ ]
264+ >,
265+ );
266+
245267 // QuickJS-WASM must be loaded before anything asks for a sandbox: the
246268 // default variant cannot fetch its own `.wasm` on Workers. Cloud runs
247269 // user `execute` code on the dynamic-worker runtime, but the artifact