Skip to content

Commit afdce2a

Browse files
NathanFlurryclaude
andcommitted
feat: US-056 - Add eager snapshot warm-up on module load
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent 541344f commit afdce2a

3 files changed

Lines changed: 54 additions & 2 deletions

File tree

packages/secure-exec-node/src/execution-driver.ts

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@ let sharedV8RuntimePromise: Promise<V8Runtime> | null = null;
99
async function getSharedV8Runtime(): Promise<V8Runtime> {
1010
if (sharedV8Runtime) return sharedV8Runtime;
1111
if (!sharedV8RuntimePromise) {
12-
sharedV8RuntimePromise = createV8Runtime().then((r) => {
12+
sharedV8RuntimePromise = createV8Runtime({
13+
warmupBridgeCode: composeBridgeCodeForWarmup(),
14+
}).then((r) => {
1315
sharedV8Runtime = r;
1416
return r;
1517
});
@@ -34,6 +36,46 @@ import { createProcessConfigForExecution } from "./bridge-setup.js";
3436

3537
export { NodeExecutionDriverOptions };
3638

39+
/**
40+
* Compose the default bridge code for snapshot warm-up.
41+
* Uses timingMitigation='none' and default budget values so the snapshot
42+
* is ready for the most common session configuration.
43+
*/
44+
export function composeBridgeCodeForWarmup(): string {
45+
const parts: string[] = [];
46+
47+
parts.push(getIvmCompatShimSource());
48+
49+
parts.push(`globalThis._maxTimers = ${DEFAULT_MAX_TIMERS};`);
50+
parts.push(`globalThis._maxHandles = ${DEFAULT_MAX_HANDLES};`);
51+
parts.push(`globalThis.__runtimeBridgeSetupConfig = ${JSON.stringify({
52+
initialCwd: DEFAULT_SANDBOX_CWD,
53+
jsonPayloadLimitBytes: DEFAULT_ISOLATE_JSON_PAYLOAD_BYTES,
54+
payloadLimitErrorCode: PAYLOAD_LIMIT_ERROR_CODE,
55+
})};`);
56+
57+
parts.push(getIsolateRuntimeSource("globalExposureHelpers"));
58+
parts.push(getInitialBridgeGlobalsSetupCode());
59+
parts.push(getConsoleSetupCode());
60+
parts.push(getIsolateRuntimeSource("setupFsFacade"));
61+
parts.push(getRawBridgeCode());
62+
parts.push(getBridgeAttachCode());
63+
64+
// Default: no timing mitigation
65+
parts.push(getIsolateRuntimeSource("applyTimingMitigationOff"));
66+
67+
parts.push(getRequireSetupCode());
68+
parts.push(getIsolateRuntimeSource("initCommonjsModuleGlobals"));
69+
70+
parts.push(`globalThis.__runtimeCustomGlobalPolicy = ${JSON.stringify({
71+
hardenedGlobals: HARDENED_NODE_CUSTOM_GLOBALS,
72+
mutableGlobals: MUTABLE_NODE_CUSTOM_GLOBALS,
73+
})};`);
74+
parts.push(getIsolateRuntimeSource("applyCustomGlobalPolicy"));
75+
76+
return parts.join("\n");
77+
}
78+
3779
const MAX_ERROR_MESSAGE_CHARS = 8192;
3880

3981
function boundErrorMessage(message: string): string {

packages/secure-exec-node/src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ export {
2626
} from "./polyfills.js";
2727

2828
// Node execution driver
29-
export { NodeExecutionDriver } from "./execution-driver.js";
29+
export { NodeExecutionDriver, composeBridgeCodeForWarmup } from "./execution-driver.js";
3030
export type { NodeExecutionDriverOptions } from "./isolate-bootstrap.js";
3131

3232
// Node system driver

packages/secure-exec-v8/src/runtime.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ export interface V8RuntimeOptions {
3131
binaryPath?: string;
3232
/** Maximum concurrent sessions. Passed via SECURE_EXEC_V8_MAX_SESSIONS. */
3333
maxSessions?: number;
34+
/** Bridge code to pre-warm the snapshot cache with (fire-and-forget). Skipped if SECURE_EXEC_NO_SNAPSHOT_WARMUP=1. */
35+
warmupBridgeCode?: string;
3436
}
3537

3638
/** Manages the Rust V8 child process and session lifecycle. */
@@ -178,6 +180,14 @@ export async function createV8Runtime(
178180
try {
179181
await ipcClient.connect();
180182
ipcClient.authenticate(authToken);
183+
184+
// Send warm-up snapshot request (fire-and-forget)
185+
if (options?.warmupBridgeCode && process.env.SECURE_EXEC_NO_SNAPSHOT_WARMUP !== "1") {
186+
ipcClient.send({
187+
type: "WarmSnapshot",
188+
bridgeCode: options.warmupBridgeCode,
189+
});
190+
}
181191
} catch (err) {
182192
// Connection failed — kill child and surface error
183193
child.kill("SIGTERM");

0 commit comments

Comments
 (0)