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
66 type DrizzleDb,66 type DrizzleDb,
67 type DbServiceShape,67 type DbServiceShape,
68} from "../db/db";68} from "../db/db";
69import { makeExecutionStack } from "../engine/execution-stack";
70import { preloadQuickJs } from "../quickjs";
71import { CloudMeteredExecutionStackLayer } from "../engine/execution-stack-metered";
72import { AutumnService } from "../extensions/billing/service";69import { AutumnService } from "../extensions/billing/service";
73import { DoTelemetryLive, flushTracerProvider } from "../observability/telemetry";70import { DoTelemetryLive, flushTracerProvider } from "../observability/telemetry";
74import {71import {
@@ -242,6 +239,31 @@ export class McpSessionDOSqlite extends McpAgentSessionDOBase<Env, CloudSessionD · 167 unmodified lines
242 ): Effect.Effect<BuiltMcpServer> {239 ): Effect.Effect<BuiltMcpServer> {
243 const self = this;240 const self = this;
244 return Effect.gen(function* () {241 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
245 // QuickJS-WASM must be loaded before anything asks for a sandbox: the267 // QuickJS-WASM must be loaded before anything asks for a sandbox: the
246 // default variant cannot fetch its own `.wasm` on Workers. Cloud runs268 // default variant cannot fetch its own `.wasm` on Workers. Cloud runs
247 // user `execute` code on the dynamic-worker runtime, but the artifact269 // user `execute` code on the dynamic-worker runtime, but the artifact