Skip to content

armgit / eve

Public Imported from GitHub
0

The Open Framework for Building Agents

fix(eve): startup - remove duplicate build work and parallelize preparation (#3514)

Andrew Barba5f17320ee05f
diff --git a/.changeset/quick-agents-build-once.md b/.changeset/quick-agents-build-once.md
new file mode 100644
index 000000000..c0dc9c47d
--- /dev/null
+++ b/.changeset/quick-agents-build-once.md
@@ -0,0 +1,5 @@
+---
+"eve": patch
+---
+
+Reduce development startup and production build time by removing duplicate bundler work, skipping unnecessary parsing, and overlapping independent preparation. Development terminal inspection now has a deadline, and `Client.info()` accepts an abort signal so slow inspection requests do not hold up startup.
diff --git a/docs/guides/client/overview.mdx b/docs/guides/client/overview.mdx
index 5a82a5aa0..fb50c75fa 100644
--- a/docs/guides/client/overview.mdx
+++ b/docs/guides/client/overview.mdx
@@ -41,6 +41,8 @@ const info = await client.info();
console.log(info.agent.name, info.agent.model.id);
```
+Pass `info({ signal })` to cancel the HTTP request. The signal is checked only after custom header and credential callbacks resolve; it does not limit how long those callbacks or the complete `info()` call can take.
+
Version 4 separates active static definitions from dynamic resolvers, includes binding-backed source ownership and composition history, reports first-class memory slots and their provider-tool wrapper dependencies, reports local and remote agents separately, and returns the exact compiled channel route order. A non-success response throws `ClientError`; invalid JSON, an earlier schema version, duplicate identities, inconsistent totals, or mismatched source provenance throws `AgentInfoResponseError`.
## Authentication
diff --git a/e2e/fixtures/agent-basic-runtime/evals/runtime/inspection-cancellation.eval.ts b/e2e/fixtures/agent-basic-runtime/evals/runtime/inspection-cancellation.eval.ts
new file mode 100644
index 000000000..98fe24365
--- /dev/null
+++ b/e2e/fixtures/agent-basic-runtime/evals/runtime/inspection-cancellation.eval.ts
@@ -0,0 +1,27 @@
+import { Client } from "eve/client";
+import { defineEval } from "eve/evals";
+import { equals } from "eve/evals/expect";
+
+export default defineEval({
+ description: "Cancelled inspection does not prevent an agent's first chat.",
+ async test(t) {
+ const client = new Client({ host: t.target.url });
+ // Keep the harness's authorization for both local and deployed targets.
+ client.fetch = (path, init) => t.target.fetch(path, init);
+ const controller = new AbortController();
+ controller.abort();
+ const cancelled = await client.info({ signal: controller.signal }).then(
+ () => false,
+ (error: unknown) => error instanceof Error && error.name === "AbortError",
+ );
+ await t.require(cancelled, equals(true));
+ const info = await client.info();
+ await t.require(typeof info.agent.name, equals("string"));
+ const turn = await t.send(
+ "Alice has finished checking the agent details. Greet her briefly without calling tools.",
+ );
+ turn.expectOk();
+ t.usedNoTools();
+ turn.event("message.completed", { count: 1 });
+ },
+});
diff --git a/packages/eve/package.json b/packages/eve/package.json
index 065509aed..c09ab90e5 100644
--- a/packages/eve/package.json
+++ b/packages/eve/package.json
@@ -461,7 +461,7 @@
"clean": "node -e \"fs.rmSync('dist', { recursive: true, force: true })\"",
"build": "pnpm run check:bin-runtime-dependencies && pnpm run build:js && node ./scripts/copy-docs.mjs && node ./scripts/stamp-version-tokens.mjs",
"build:compiled": "node ./scripts/vendor-compiled.mjs",
- "build:js": "pnpm run build:compiled && pnpm run clean && pnpm run build:types && node ./scripts/copy-compiled-assets.mjs && node --conditions=eve-source ./scripts/build-rolldown.mjs && node ./scripts/copy-runtime-assets.mjs",
+ "build:js": "node ./scripts/build-js.mjs",
"build:types": "pnpm run build:compiled && tsc -p tsconfig.build.json",
"check:bin-runtime-dependencies": "node ./scripts/check-bin-runtime-dependencies.mjs",
"dev": "pnpm run build:compiled && node ./scripts/copy-runtime-assets.mjs && tsc -p tsconfig.dev.json --watch --preserveWatchOutput",
diff --git a/packages/eve/scripts/benchmark-startup.mjs b/packages/eve/scripts/benchmark-startup.mjs
new file mode 100644
index 000000000..e4820f1cb
--- /dev/null
+++ b/packages/eve/scripts/benchmark-startup.mjs
@@ -0,0 +1,173 @@
+import { spawn } from "node:child_process";
+import { mkdir, mkdtemp, readFile, symlink, writeFile } from "node:fs/promises";
+import { createServer } from "node:net";
+import { tmpdir } from "node:os";
+import { join, resolve } from "node:path";
+import { setTimeout as delay } from "node:timers/promises";
+
+// Run against two separately built package directories, sequentially, on the same machine.
+// Each process is fresh; subsequent runs retain the fixture's caches and build output.
+const packageRoot = resolve(process.argv[2] ?? "packages/eve");
+const runs = Number(process.argv[3] ?? 5);
+const toolCount = Number(process.argv[4] ?? 0);
+if (!Number.isInteger(runs) || runs < 1) throw new Error("Expected a positive run count.");
+if (!Number.isInteger(toolCount) || toolCount < 0 || toolCount > 1000)
+ throw new Error("Expected 0–1000 tools.");
+const root = await mkdtemp(join(tmpdir(), "eve-startup-benchmark-"));
+await mkdir(join(root, "agent", "channels"), { recursive: true });
+await mkdir(join(root, "node_modules"));
+await symlink(packageRoot, join(root, "node_modules", "eve"), "junction");
+await writeFile(
+ join(root, "package.json"),
+ JSON.stringify({ private: true, type: "module", dependencies: { eve: "*" } }),
+);
+await writeFile(join(root, "agent", "instructions.md"), "You are a helpful assistant.\n");
+await writeFile(
+ join(root, "agent", "agent.ts"),
+ 'import { defineAgent } from "eve";\nexport default defineAgent({ model: "openai/gpt-5.4-mini" });\n',
+);
+await writeFile(
+ join(root, "agent", "channels", "eve.ts"),
+ 'import { eveChannel } from "eve/channels/eve";\nimport { localDev } from "eve/channels/auth";\nexport default eveChannel({ auth: [localDev()] });\n',
+);
+if (toolCount > 0) {
+ await mkdir(join(root, "agent", "tools"));
+ for (let i = 0; i < toolCount; i++) {
+ await writeFile(
+ join(root, "agent", "tools", `tool_${i}.ts`),
+ `export default { description: "Return fixture value ${i}.", execute: async () => ${i} };\n`,
+ );
+ }
+}
+const env = {
+ ...process.env,
+ EVE_TELEMETRY_DISABLED: "1",
+ AI_GATEWAY_API_KEY: "benchmark-placeholder-no-model-calls",
+};
+delete env.VERCEL;
+delete env.PORT;
+delete env.EVE_DEV;
+const results = [];
+console.log(
+ JSON.stringify({
+ root,
+ packageRoot,
+ toolCount,
+ node: process.version,
+ platform: process.platform,
+ arch: process.arch,
+ }),
+);
+
+for (let run = 1; run <= runs; run++) {
+ for (const mode of ["dev", "build"]) {
+ const port = await availablePort();
+ const profilePath = join(root, `build-${run}.json`);
+ const args =
+ mode === "dev"
+ ? ["dev", "--no-ui", "--host", "127.0.0.1", "--port", String(port)]
+ : ["build", "--profile", profilePath];
+ let log = "";
+ const start = performance.now();
+ const child = spawn(process.execPath, [join(packageRoot, "bin", "eve.js"), ...args], {
+ cwd: root,
+ env,
+ stdio: ["ignore", "pipe", "pipe"],
+ });
+ child.stdout.on("data", (data) => {
+ log += data;
+ });
+ child.stderr.on("data", (data) => {
+ log += data;
+ });
+ const exited = new Promise((resolveExit, reject) => {
+ child.once("error", reject);
+ child.once("exit", (code, signal) => resolveExit({ code, signal }));
+ });
+ const timeout = setTimeout(() => child.kill("SIGKILL"), 60000);
+ try {
+ if (mode === "dev") {
+ let ready = false;
+ while (performance.now() - start < 60000) {
+ if (child.exitCode !== null || child.signalCode !== null) throw new Error(log);
+ try {
+ const response = await fetch(`http://127.0.0.1:${port}/eve/v1/info`, {
+ signal: AbortSignal.timeout(5000),
+ });
+ const body = await response.text();
+ if (response.ok) {
+ JSON.parse(body);
+ ready = true;
+ break;
+ }
+ } catch {}
+ await delay(20);
+ }
+ if (!ready) throw new Error(`Readiness timeout: ${log}`);
+ const wallMs = Math.round(performance.now() - start);
+ const accepted = await fetch(`http://127.0.0.1:${port}/eve/v1/session`, {
+ method: "POST",
+ signal: AbortSignal.timeout(10000),
+ });
+ const session = await accepted.json();
+ if (!accepted.ok || typeof session.sessionId !== "string") {
+ throw new Error(
+ `Session creation failed (${accepted.status}): ${JSON.stringify(session)}`,
+ );
+ }
+ results.push({
+ run,
+ mode,
+ wallMs,
+ sessionAcceptedMs: Math.round(performance.now() - start),
+ status: 200,
+ });
+ } else {
+ const { code } = await exited;
+ if (code !== 0) throw new Error(log);
+ results.push({
+ run,
+ mode,
+ wallMs: Math.round(performance.now() - start),
+ profile: JSON.parse(await readFile(profilePath, "utf8")),
+ });
+ }
+ console.log(JSON.stringify(results.at(-1)));
+ } finally {
+ clearTimeout(timeout);
+ if (child.exitCode === null && child.signalCode === null) child.kill("SIGINT");
+ const killer = setTimeout(() => child.kill("SIGKILL"), 10000);
+ await exited;
+ clearTimeout(killer);
+ await writeFile(join(root, `${mode}-${run}.log`), log);
+ }
+ }
+}
+await writeFile(join(root, "results.json"), JSON.stringify(results, null, 2));
+for (const mode of ["dev", "build"]) {
+ const times = results
+ .filter((result) => result.mode === mode)
+ .map((result) => result.wallMs)
+ .sort((a, b) => a - b);
+ console.log(
+ JSON.stringify({
+ mode,
+ medianMs: times[Math.floor(times.length / 2)],
+ minMs: times[0],
+ maxMs: times.at(-1),
+ }),
+ );
+}
+
+async function availablePort() {
+ const server = createServer();
+ await new Promise((resolveListen, reject) => {
+ server.once("error", reject);
+ server.listen(0, "127.0.0.1", resolveListen);
+ });
+ const { port } = server.address();
+ await new Promise((resolveClose, reject) =>
+ server.close((error) => (error ? reject(error) : resolveClose())),
+ );
+ return port;
+}
diff --git a/packages/eve/scripts/build-js.mjs b/packages/eve/scripts/build-js.mjs
new file mode 100644
index 000000000..1517adc61
--- /dev/null
+++ b/packages/eve/scripts/build-js.mjs
@@ -0,0 +1,39 @@
+import { spawn } from "node:child_process";
+import { rm } from "node:fs/promises";
+import { createRequire } from "node:module";
+import { dirname, join } from "node:path";
+import { fileURLToPath } from "node:url";
+
+import { copyCompiledAssets } from "./copy-compiled-assets.mjs";
+import { copyRuntimeAssets } from "./copy-runtime-assets.mjs";
+
+const packageRoot = fileURLToPath(new URL("..", import.meta.url));
+const require = createRequire(import.meta.url);
+
+await run(["./scripts/vendor-compiled.mjs"]);
+await rm(new URL("../dist", import.meta.url), { recursive: true, force: true });
+// Declarations and JavaScript have disjoint outputs. Drain both before packaging.
+const results = await Promise.allSettled([
+ run([
+ join(dirname(require.resolve("typescript/package.json")), "bin", "tsc"),
+ "-p",
+ "tsconfig.build.json",
+ ]),
+ run(["--conditions=eve-source", "./scripts/build-rolldown.mjs"]),
+ copyCompiledAssets(),
+]);
+const errors = results
+ .filter((result) => result.status === "rejected")
+ .map((result) => result.reason);
+if (errors.length > 0) throw new AggregateError(errors, "Failed to build eve.");
+await copyRuntimeAssets();
+
+async function run(args) {
+ await new Promise((resolve, reject) => {
+ const child = spawn(process.execPath, args, { cwd: packageRoot, stdio: "inherit" });
+ child.once("error", reject);
+ child.once("exit", (code, signal) =>
+ code === 0 ? resolve() : reject(new Error(`${args.join(" ")} exited with ${signal ?? code}`)),
+ );
+ });
+}
diff --git a/packages/eve/src/cli/agent-command.test.ts b/packages/eve/src/cli/agent-command.test.ts
index bfa968f05..820951701 100644
--- a/packages/eve/src/cli/agent-command.test.ts
+++ b/packages/eve/src/cli/agent-command.test.ts
@@ -24,6 +24,22 @@ function workspaceContext(): CliApplicationContext {
}
describe("agentCommand", () => {
+ it("reuses the initial standalone selection after resolving the app", async () => {
+ const context = workspaceContext();
+ context.resolveAgent = vi.fn(async () => ({
+ kind: "standalone" as const,
+ appRoot: "/repo",
+ environmentRoot: "/repo",
+ }));
+ const action = vi.fn();
+ const program = new Command().exitOverride();
+ agentCommand(program.command("info"), context).action(action);
+ await program.parseAsync(["info"], { from: "user" });
+ expect(context.resolveAgent).toHaveBeenCalledOnce();
+ expect(context.resolve).toHaveBeenCalledOnce();
+ expect(action).toHaveBeenCalledOnce();
+ });
+
it("resolves an explicit workspace agent before the action", async () => {
const context = workspaceContext();
const action = vi.fn(() => expect(context.root).toBe("/repo/agents/support"));
diff --git a/packages/eve/src/cli/agent-command.ts b/packages/eve/src/cli/agent-command.ts
index 03fd53c2c..e5e755f24 100644
--- a/packages/eve/src/cli/agent-command.ts
+++ b/packages/eve/src/cli/agent-command.ts
@@ -61,11 +61,10 @@ export function agentCommand(
}
await applicationContext.resolve();
- const resolvedSelection = await applicationContext.resolveAgent();
if (requestedName !== undefined) {
if (
- resolvedSelection.kind !== "workspace-member" ||
- resolvedSelection.member.name !== requestedName
+ initialSelection.kind !== "workspace-member" ||
+ initialSelection.member.name !== requestedName
) {
throw new Error("--agent can only select a member of the enclosing agents/ workspace.");
}
diff --git a/packages/eve/src/cli/dev/local-server-child.ts b/packages/eve/src/cli/dev/local-server-child.ts
index 193f9c81b..9c74d28c6 100644
--- a/packages/eve/src/cli/dev/local-server-child.ts
+++ b/packages/eve/src/cli/dev/local-server-child.ts
@@ -1,4 +1,4 @@
-import { createDevelopmentServer } from "#internal/nitro/host.js";
+import { createDevelopmentServer } from "#internal/nitro/host/start-development-server.js";
import type { DevelopmentServerOptions } from "#internal/nitro/host/types.js";
import { getDevelopmentSandboxRunId } from "#execution/sandbox/development-run.js";
import { reconcileCleanupIntents, recordCleanupIntent } from "#cli/dev/local-server-cleanup.js";
diff --git a/packages/eve/src/cli/dev/tui/runner.test.ts b/packages/eve/src/cli/dev/tui/runner.test.ts
index 26676c5d4..8d0e51f10 100644
--- a/packages/eve/src/cli/dev/tui/runner.test.ts
+++ b/packages/eve/src/cli/dev/tui/runner.test.ts
@@ -590,6 +590,28 @@ async function settleAsyncWork(): Promise<void> {
}
describe("EveTUIRunner agent header", () => {
+ it("opens the prompt after two seconds when startup inspection stalls", async () => {
+ vi.useFakeTimers();
+ const client = stubClient();
+ vi.spyOn(client, "info").mockImplementation(
+ async () => await new Promise<AgentInfoResult>(() => {}),
+ );
+ const renderer = fakeRenderer();
+ const runner = new EveTUIRunner({
+ session: stubSession(),
+ client,
+ renderer,
+ serverUrl: "http://localhost:3000",
+ });
+ const run = runner.run();
+ await vi.advanceTimersByTimeAsync(1999);
+ expect(renderer.readPrompt).not.toHaveBeenCalled();
+ await vi.advanceTimersByTimeAsync(1);
+ await run;
+ expect(renderer.readPrompt).toHaveBeenCalled();
+ expect(client.info).toHaveBeenCalledOnce();
+ });
+
it("reports the paint boundary before rendering the startup header", async () => {
const order: string[] = [];
const client = stubClient();
@@ -785,6 +807,7 @@ describe("EveTUIRunner agent header", () => {
const run = runner.run();
await settleAsyncWork();
+ await vi.advanceTimersByTimeAsync(0);
expect(headers).toHaveLength(1);
expect(fetchMock).toHaveBeenCalledTimes(1);
diff --git a/packages/eve/src/cli/dev/tui/runner.ts b/packages/eve/src/cli/dev/tui/runner.ts
index dfd2ebf86..d27b2347f 100644
--- a/packages/eve/src/cli/dev/tui/runner.ts
+++ b/packages/eve/src/cli/dev/tui/runner.ts
@@ -724,7 +724,7 @@ export class EveTUIRunner {
try {
const probe = await devBootPhase(
"connecting to agent",
- () => probeAgentInfo({ client }),
+ () => probeAgentInfo({ client, timeoutMs: 2000 }),
this.#onBootProgress,
);
if (probe.kind === "ready") info = probe.info;
diff --git a/packages/eve/src/client/client.test.ts b/packages/eve/src/client/client.test.ts
index 59a8b4d15..3c1209893 100644
--- a/packages/eve/src/client/client.test.ts
+++ b/packages/eve/src/client/client.test.ts
@@ -39,6 +39,18 @@ afterEach(() => {
});
describe("Client request policy", () => {
+ it("forwards inspection cancellation without changing authentication", async () => {
+ const fetchMock = vi
+ .spyOn(globalThis, "fetch")
+ .mockResolvedValueOnce(Response.json(AGENT_INFO));
+ const controller = new AbortController();
+ const client = new Client({ host: "https://eve.test", auth: { bearer: "test-token" } });
+ await client.info({ signal: controller.signal });
+ const init = fetchMock.mock.calls[0]?.[1];
+ expect(init?.signal).toBe(controller.signal);
+ expect(new Headers(init?.headers).get("authorization")).toBe("Bearer test-token");
+ });
+
it("rejects malformed health payloads", async () => {
vi.spyOn(globalThis, "fetch").mockResolvedValueOnce(
Response.json({ ok: true, status: "ready", workflowId: "wf", extra: true }),
diff --git a/packages/eve/src/client/client.ts b/packages/eve/src/client/client.ts
index 2c52c4fbf..6c1d98e82 100644
--- a/packages/eve/src/client/client.ts
+++ b/packages/eve/src/client/client.ts
@@ -92,8 +92,8 @@ export class Client {
* shape). Inspection is best-effort: a working connection does not depend on
* this route, so connection probes treat this distinctly from a failed request.
*/
- async info(): Promise<AgentInfoResult> {
- const response = await this.fetch(EVE_INFO_ROUTE_PATH);
+ async info(options: { readonly signal?: AbortSignal } = {}): Promise<AgentInfoResult> {
+ const response = await this.fetch(EVE_INFO_ROUTE_PATH, options);
if (!response.ok) {
const body = await response.text();
diff --git a/packages/eve/src/internal/authored-runtime-modules.ts b/packages/eve/src/internal/authored-runtime-modules.ts
index 362ca34fe..370b1ca62 100644
--- a/packages/eve/src/internal/authored-runtime-modules.ts
+++ b/packages/eve/src/internal/authored-runtime-modules.ts
@@ -5,6 +5,7 @@ import {
bundleAuthoredModuleMapForGeneration,
} from "#internal/authored-module-loader.js";
import { resolveInstrumentationLayout } from "#internal/instrumentation-layout.js";
+import { mapConcurrent } from "#shared/map-concurrent.js";
interface PreparedAuthoredRuntimeInstrumentation {
readonly kind: "directory";
@@ -39,10 +40,12 @@ export async function prepareAuthoredRuntimeModules(input: {
let instrumentation: PreparedAuthoredRuntimeInstrumentation | undefined;
if (layout?.kind === "directory") {
- const moduleCodeBySlot: Record<string, string> = {};
- for (const [slot, sourcePath] of Object.entries(layout.modulePathsBySlot)) {
- moduleCodeBySlot[slot] = await bundleInstrumentationModule(sourcePath);
- }
+ const moduleCodeBySlot = Object.fromEntries(
+ await mapConcurrent(Object.entries(layout.modulePathsBySlot), async ([slot, sourcePath]) => [
+ slot,
+ await bundleInstrumentationModule(sourcePath),
+ ]),
+ );
instrumentation = { kind: "directory", moduleCodeBySlot };
}
diff --git a/packages/eve/src/internal/nitro/dev-runtime-artifacts.ts b/packages/eve/src/internal/nitro/dev-runtime-artifacts.ts
index 138428f24..d0f6a915f 100644
--- a/packages/eve/src/internal/nitro/dev-runtime-artifacts.ts
+++ b/packages/eve/src/internal/nitro/dev-runtime-artifacts.ts
@@ -43,6 +43,7 @@ export interface DevelopmentRuntimeArtifactsRevision {
}
export interface DevelopmentRuntimeArtifactsSnapshot {
+ readonly sourceWatchPaths?: readonly string[];
readonly runtimeAppRoot: string;
readonly snapshotRoot: string;
readonly snapshotSourceRoot: string;
@@ -139,6 +140,7 @@ export async function stageDevelopmentRuntimeArtifactsSnapshot(
}
return {
+ sourceWatchPaths: sourceSnapshotPlan.watchPaths,
runtimeAppRoot: sourceSnapshotPlan.runtimeAppRoot,
snapshotRoot,
snapshotSourceRoot: sourceSnapshotPlan.snapshotSourceRoot,
diff --git a/packages/eve/src/internal/nitro/development-generation.test.ts b/packages/eve/src/internal/nitro/development-generation.test.ts
index 1b51e95b0..dfe5cb7fa 100644
--- a/packages/eve/src/internal/nitro/development-generation.test.ts
+++ b/packages/eve/src/internal/nitro/development-generation.test.ts
@@ -1,20 +1,36 @@
import { beforeEach, describe, expect, it, vi } from "vitest";
import type { DevelopmentGeneration } from "#internal/nitro/development-generation.js";
+import type { CompileAgentResult } from "#compiler/compile-agent.js";
const mocks = vi.hoisted(() => ({
activateTransaction: vi.fn(),
prune: vi.fn(async () => undefined),
+ prepare: vi.fn(),
+ stage: vi.fn(),
+ materialize: vi.fn(),
+ rm: vi.fn(async () => undefined),
+}));
+
+vi.mock("node:fs/promises", () => ({ rm: mocks.rm }));
+vi.mock("#internal/authored-runtime-modules.js", () => ({
+ prepareAuthoredRuntimeModules: mocks.prepare,
+}));
+vi.mock("#internal/materialized-authored-modules.js", () => ({
+ writeMaterializedAuthoredModules: mocks.materialize,
}));
vi.mock("#internal/nitro/dev-runtime-artifacts.js", () => ({
activateDevelopmentRuntimeArtifactsSnapshotTransaction: mocks.activateTransaction,
pruneDevelopmentRuntimeArtifactsSnapshots: mocks.prune,
- stageDevelopmentRuntimeArtifactsSnapshot: vi.fn(),
+ stageDevelopmentRuntimeArtifactsSnapshot: mocks.stage,
}));
-const { activateDevelopmentGeneration, activateDevelopmentGenerationTransaction } =
- await import("#internal/nitro/development-generation.js");
+const {
+ activateDevelopmentGeneration,
+ activateDevelopmentGenerationTransaction,
+ stageDevelopmentGeneration,
+} = await import("#internal/nitro/development-generation.js");
function createGeneration(id: string): DevelopmentGeneration {
return {
@@ -26,6 +42,61 @@ function createGeneration(id: string): DevelopmentGeneration {
};
}
+describe("development generation staging", () => {
+ const compileResult = {
+ manifest: {},
+ paths: { moduleMapPath: "/tmp/app/modules.mjs" },
+ } as CompileAgentResult;
+ beforeEach(() => {
+ mocks.prepare.mockReset();
+ mocks.stage.mockReset();
+ mocks.materialize.mockReset();
+ mocks.rm.mockClear();
+ });
+
+ it("overlaps preparation with staging and materializes only after both finish", async () => {
+ const preparation = Promise.withResolvers<object>();
+ const staging = Promise.withResolvers<DevelopmentGeneration>();
+ mocks.prepare.mockReturnValue(preparation.promise);
+ mocks.stage.mockReturnValue(staging.promise);
+ mocks.materialize.mockResolvedValue({ fingerprint: "ready" });
+ const result = stageDevelopmentGeneration(compileResult);
+ expect(mocks.prepare).toHaveBeenCalledOnce();
+ expect(mocks.stage).toHaveBeenCalledOnce();
+ preparation.resolve({ authoredWorkflowModules: {} });
+ await Promise.resolve();
+ expect(mocks.materialize).not.toHaveBeenCalled();
+ staging.resolve(createGeneration("parallel"));
+ expect(await result).toMatchObject({ fingerprint: "ready" });
+ expect(mocks.rm).not.toHaveBeenCalled();
+ });
+
+ it("waits for staging before cleaning up a rejected preparation", async () => {
+ const staging = Promise.withResolvers<DevelopmentGeneration>();
+ const error = new Error("bundle failed");
+ mocks.prepare.mockRejectedValue(error);
+ mocks.stage.mockReturnValue(staging.promise);
+ const result = stageDevelopmentGeneration(compileResult);
+ const observed = result.catch((failure) => failure);
+ await Promise.resolve();
+ expect(mocks.rm).not.toHaveBeenCalled();
+ staging.resolve(createGeneration("discard"));
+ expect(await observed).toBe(error);
+ expect(mocks.materialize).not.toHaveBeenCalled();
+ expect(mocks.rm).toHaveBeenCalledWith("/tmp/discard", { force: true, recursive: true });
+ });
+
+ it("retains both failures when parallel operations reject", async () => {
+ const errors = [new Error("prepare"), new Error("stage")];
+ mocks.prepare.mockRejectedValue(errors[0]);
+ mocks.stage.mockRejectedValue(errors[1]);
+ const error = await stageDevelopmentGeneration(compileResult).catch((failure) => failure);
+ expect(error).toBeInstanceOf(AggregateError);
+ expect(error.errors).toEqual(errors);
+ expect(mocks.rm).not.toHaveBeenCalled();
+ });
+});
+
describe("development generation activation", () => {
beforeEach(() => {
mocks.activateTransaction.mockReset();
diff --git a/packages/eve/src/internal/nitro/development-generation.ts b/packages/eve/src/internal/nitro/development-generation.ts
index 1a5fe35bb..54ddb1c37 100644
--- a/packages/eve/src/internal/nitro/development-generation.ts
+++ b/packages/eve/src/internal/nitro/development-generation.ts
@@ -29,13 +29,28 @@ const developmentGenerationPruneStates = new Map<string, DevelopmentGenerationPr
export async function stageDevelopmentGeneration(
compileResult: CompileAgentResult,
): Promise<DevelopmentGeneration> {
- const prepared = await prepareAuthoredRuntimeModules({
- manifest: compileResult.manifest,
- moduleMapPath: compileResult.paths.moduleMapPath,
- });
- const snapshot = await stageDevelopmentRuntimeArtifactsSnapshot(compileResult);
+ // Drain both operations before discarding a failed candidate's snapshot.
+ const [preparation, staging] = await Promise.allSettled([
+ prepareAuthoredRuntimeModules({
+ manifest: compileResult.manifest,
+ moduleMapPath: compileResult.paths.moduleMapPath,
+ }),
+ stageDevelopmentRuntimeArtifactsSnapshot(compileResult),
+ ]);
+ if (staging.status === "rejected") {
+ if (preparation.status === "rejected") {
+ throw new AggregateError(
+ [preparation.reason, staging.reason],
+ "Failed to stage development generation.",
+ );
+ }
+ throw staging.reason;
+ }
+ const snapshot = staging.value;
try {
+ if (preparation.status === "rejected") throw preparation.reason;
+ const prepared = preparation.value;
const materialized = await writeMaterializedAuthoredModules({
prepared,
runtimeAppRoot: snapshot.runtimeAppRoot,
diff --git a/packages/eve/src/internal/nitro/host/create-application-nitro.scenario.test.ts b/packages/eve/src/internal/nitro/host/create-application-nitro.scenario.test.ts
index c9468311f..ade10565c 100644
--- a/packages/eve/src/internal/nitro/host/create-application-nitro.scenario.test.ts
+++ b/packages/eve/src/internal/nitro/host/create-application-nitro.scenario.test.ts
@@ -655,7 +655,7 @@ describe("application Nitro creation", () => {
const traceDeps = createNitroMock.mock.calls[0]?.[0].traceDeps;
expect(traceDeps).toEqual(expect.arrayContaining(["zod*", "sharp", "sharp*"]));
- const plugins = createNitroMock.mock.calls[0]?.[0].rolldownConfig.plugins;
+ const plugins = createNitroMock.mock.calls[0]?.[0].rollupConfig.plugins;
const externalPlugin = plugins.find(
(plugin: { name?: string }) => plugin.name === "eve-extension-external-dependency",
);
@@ -847,6 +847,18 @@ describe("application Nitro creation", () => {
await createProductionApplicationNitro(productionHost, createProductionOptions(productionHost));
await createDevelopmentApplicationNitro(await createPreparedHost());
+ for (const [options] of createNitroMock.mock.calls) {
+ const plugins = [
+ ...(options.rolldownConfig?.plugins ?? []),
+ ...(options.rollupConfig?.plugins ?? []),
+ ];
+ expect(plugins.filter((plugin) => plugin.name === "eve-node-esm-compat-banner")).toHaveLength(
+ 1,
+ );
+ const names = plugins.map((plugin) => plugin.name);
+ expect(new Set(names).size).toBe(names.length);
+ }
+
const productionConfig = { plugins: [] };
for (const hook of productionNitroStub.hookHandlers.get("rollup:before") ?? []) {
await hook(productionNitroStub.nitro, productionConfig);
diff --git a/packages/eve/src/internal/nitro/host/create-application-nitro.ts b/packages/eve/src/internal/nitro/host/create-application-nitro.ts
index c513f8c4e..9cbe0c53b 100644
--- a/packages/eve/src/internal/nitro/host/create-application-nitro.ts
+++ b/packages/eve/src/internal/nitro/host/create-application-nitro.ts
@@ -389,29 +389,8 @@ function addWorkflowModuleSideEffectsPlugin(nitro: Nitro, workflowBuildDir: stri
function addNitroStepModuleSideEffectsPlugin(
nitro: Nitro,
- input: {
- stepEntrypointPath: string;
- },
-): () => void {
- let cachedStepTransformTargets: Set<string> | null = null;
-
- const getStepTransformTargets = async (): Promise<Set<string>> => {
- if (cachedStepTransformTargets !== null) {
- return cachedStepTransformTargets;
- }
-
- cachedStepTransformTargets = await collectNitroStepTransformTargets(
- input.stepEntrypointPath,
- nitro.options.rootDir,
- );
- return cachedStepTransformTargets;
- };
-
- const clearCachedStepTransformTargets = () => {
- cachedStepTransformTargets = null;
- };
- nitro.hooks.hook("build:before", clearCachedStepTransformTargets);
-
+ getStepTransformTargets: () => Promise<Set<string>>,
+): void {
nitro.hooks.hook("rollup:before", (_nitro, config) => {
if (!Array.isArray(config.plugins)) {
return;
@@ -438,8 +417,6 @@ function addNitroStepModuleSideEffectsPlugin(
},
});
});
-
- return clearCachedStepTransformTargets;
}
/**
@@ -449,29 +426,8 @@ function addNitroStepModuleSideEffectsPlugin(
*/
function addNitroStepTransformPlugin(
nitro: Nitro,
- input: {
- stepEntrypointPath: string;
- },
-): () => void {
- let cachedStepTransformTargets: Set<string> | null = null;
-
- const getStepTransformTargets = async (): Promise<Set<string>> => {
- if (cachedStepTransformTargets !== null) {
- return cachedStepTransformTargets;
- }
-
- cachedStepTransformTargets = await collectNitroStepTransformTargets(
- input.stepEntrypointPath,
- nitro.options.rootDir,
- );
- return cachedStepTransformTargets;
- };
-
- const clearCachedStepTransformTargets = () => {
- cachedStepTransformTargets = null;
- };
- nitro.hooks.hook("build:before", clearCachedStepTransformTargets);
-
+ getStepTransformTargets: () => Promise<Set<string>>,
+): void {
nitro.hooks.hook("rollup:before", (_nitro, config) => {
if (!Array.isArray(config.plugins)) {
return;
@@ -503,8 +459,6 @@ function addNitroStepTransformPlugin(
name: "eve:workflow-step-transform",
});
});
-
- return clearCachedStepTransformTargets;
}
/**
@@ -639,9 +593,9 @@ function createApplicationNitroBundlerConfiguration(
extensionScopePlugin,
].filter((plugin) => plugin !== null);
const nitroRolldownConfig = {
- ...createNitroBundlerConfig(nitroBundlerPlugins),
tsconfig: resolveAuthoredTsConfigPath(preparedHost.appRoot),
};
+ // Nitro inherits rollupConfig in its Rolldown builder, concatenating plugin arrays.
const nitroRollupConfig = createNitroBundlerConfig(nitroBundlerPlugins);
const tracedAppDependencies = collectHostedTraceDependencies(
preparedHost,
@@ -697,10 +651,16 @@ function configureSharedApplicationNitro(
}
function configureNitroStepPlugins(nitro: Nitro, stepEntrypointPath: string): Array<() => void> {
- return [
- addNitroStepModuleSideEffectsPlugin(nitro, { stepEntrypointPath }),
- addNitroStepTransformPlugin(nitro, { stepEntrypointPath }),
- ];
+ let targets: Promise<Set<string>> | undefined;
+ const getTargets = () =>
+ (targets ??= collectNitroStepTransformTargets(stepEntrypointPath, nitro.options.rootDir));
+ const invalidate = () => {
+ targets = undefined;
+ };
+ nitro.hooks.hook("build:before", invalidate);
+ addNitroStepModuleSideEffectsPlugin(nitro, getTargets);
+ addNitroStepTransformPlugin(nitro, getTargets);
+ return [invalidate];
}
function externalizeDevelopmentWorkflowBundle(
diff --git a/packages/eve/src/internal/nitro/host/dev-authored-source-watcher.ts b/packages/eve/src/internal/nitro/host/dev-authored-source-watcher.ts
index 4e6d15546..6cbebb4f6 100644
--- a/packages/eve/src/internal/nitro/host/dev-authored-source-watcher.ts
+++ b/packages/eve/src/internal/nitro/host/dev-authored-source-watcher.ts
@@ -219,7 +219,9 @@ async function resolveAuthoredWatchPaths(
join(host.appRoot, TS_CONFIG_GLOB_NAME),
]);
const tsconfigPaths = await resolveTsConfigWatchPaths(host.appRoot);
- const sourceSnapshotWatchPaths = await resolveDevelopmentSourceSnapshotWatchPaths(host.appRoot);
+ const sourceSnapshotWatchPaths =
+ host.generation.sourceWatchPaths ??
+ (await resolveDevelopmentSourceSnapshotWatchPaths(host.appRoot));
for (const extension of host.workspaceExtensions) {
watchPaths.add(extension.config.sourceRoot);
diff --git a/packages/eve/src/internal/nitro/host/extension-distribution.ts b/packages/eve/src/internal/nitro/host/extension-distribution.ts
index f75283d55..e4a29532d 100644
--- a/packages/eve/src/internal/nitro/host/extension-distribution.ts
+++ b/packages/eve/src/internal/nitro/host/extension-distribution.ts
@@ -39,23 +39,28 @@ export async function emitExtensionDistribution(input: {
stagedDistRoot: input.stagedDistRoot,
});
const entries = await createDistributionEntries({ ...input, moduleFiles });
- const emitted = await bundleExtensionDistributionGraph({
- entries,
- packageRoot: input.appRoot,
- runtimeDependencies: input.runtimeDependencies,
- });
+ const [bundle, declarations] = await Promise.allSettled([
+ bundleExtensionDistributionGraph({
+ entries,
+ packageRoot: input.appRoot,
+ runtimeDependencies: input.runtimeDependencies,
+ }),
+ emitExtensionDeclarations({
+ appRoot: input.appRoot,
+ declarationsRoot: input.declarationsRoot,
+ moduleLogicalPaths: moduleFiles.map((file) => file.logicalPath),
+ sourceRoot: input.sourceRoot,
+ }),
+ ]);
+ if (bundle.status === "rejected") throw bundle.reason;
+ if (declarations.status === "rejected") throw declarations.reason;
+ const emitted = bundle.value;
for (const [fileName, code] of emitted.files) {
const outputPath = join(input.stagedOutDir, fileName);
await mkdir(dirname(outputPath), { recursive: true });
await writeFile(outputPath, code, "utf8");
}
- await emitExtensionDeclarations({
- appRoot: input.appRoot,
- declarationsRoot: input.declarationsRoot,
- moduleLogicalPaths: moduleFiles.map((file) => file.logicalPath),
- sourceRoot: input.sourceRoot,
- });
const sourceRelativePath = relative(input.appRoot, input.sourceRoot);
try {
await cp(join(input.declarationsRoot, sourceRelativePath), input.stagedDistRoot, {
diff --git a/packages/eve/src/internal/node-esm-compat-banner.scenario.test.ts b/packages/eve/src/internal/node-esm-compat-banner.scenario.test.ts
index a93fc0c9e..c2473865a 100644
--- a/packages/eve/src/internal/node-esm-compat-banner.scenario.test.ts
+++ b/packages/eve/src/internal/node-esm-compat-banner.scenario.test.ts
@@ -6,6 +6,36 @@ import { buildSingleRolldownChunk } from "#internal/bundler/nitro-rolldown.js";
import { createNodeEsmCompatBannerPlugin } from "#internal/node-esm-compat-banner.js";
describe("Node ESM compatibility banner bundling", () => {
+ it.each(["using", "await using"])(
+ "loads a chunk with %s compatibility bindings",
+ async (declaration) => {
+ const source = `
+${declaration} __filename = { value: "filename", [Symbol.dispose]() {} };
+${declaration} __dirname = { value: "dirname", [Symbol.dispose]() {} };
+${declaration} require = { value: "require", [Symbol.dispose]() {} };
+export const value = [__filename.value, __dirname.value, require.value];`;
+ const chunk = await buildSingleRolldownChunk("resource compatibility bindings", {
+ input: "virtual:resources",
+ platform: "node",
+ plugins: [
+ {
+ name: "resource-fixture",
+ resolveId: (id: string) => (id === "virtual:resources" ? id : undefined),
+ load: () => source,
+ renderChunk: () => source,
+ },
+ createNodeEsmCompatBannerPlugin({ includeRequire: true }),
+ ],
+ output: { format: "esm" },
+ });
+
+ const namespace = await import(
+ `data:text/javascript;base64,${Buffer.from(chunk.code).toString("base64")}`
+ );
+ expect(namespace.value).toEqual(["filename", "dirname", "require"]);
+ },
+ );
+
it("loads a chunk with comma-separated compatibility bindings", async () => {
const source = `const logs = [], require = () => "required", __filename = "/agent/index.mjs", __dirname = "/agent";
export const value = [logs, require(), __filename, __dirname];`;
diff --git a/packages/eve/src/internal/node-esm-compat-banner.test.ts b/packages/eve/src/internal/node-esm-compat-banner.test.ts
index dfb31c9d8..e1555212f 100644
--- a/packages/eve/src/internal/node-esm-compat-banner.test.ts
+++ b/packages/eve/src/internal/node-esm-compat-banner.test.ts
@@ -1,4 +1,4 @@
-import { describe, expect, it } from "vitest";
+import { describe, expect, it, vi } from "vitest";
import {
buildNodeEsmCompatBanner,
@@ -121,6 +121,56 @@ describe("buildNodeEsmCompatBanner", () => {
});
describe("createNodeEsmCompatBannerPlugin", () => {
+ it("does not parse chunks without possible compatibility declarations", () => {
+ const parse = vi.fn(() => EMPTY_PROGRAM);
+ const plugin = createNodeEsmCompatBannerPlugin();
+ const result = plugin.renderChunk.call({ parse }, "export const value = __dirname;");
+ expect(parse).not.toHaveBeenCalled();
+ expect(result?.code).toContain("const __dirname =");
+ });
+
+ it.each([
+ "let /* comment */ __dirname = '/';",
+ "var // comment\n__dirname = '/';",
+ "const first = (() => { return 1; })(), /* next */ __dirname = '/';",
+ String.raw`const __dirn\u0061me = '/';`,
+ ])("parses possible declarations: %s", (code) => {
+ const parse = vi.fn(() => programWithTopLevelBindings("__dirname"));
+ const result = createNodeEsmCompatBannerPlugin().renderChunk.call({ parse }, code);
+ expect(parse).toHaveBeenCalledOnce();
+ expect(result?.code).not.toContain("const __dirname = __eveDirname");
+ });
+
+ it("checks require declarations only when providing a require shim", () => {
+ const parse = vi.fn(() => programWithTopLevelBindings("require"));
+ const code = "const require = customRequire;";
+ createNodeEsmCompatBannerPlugin().renderChunk.call({ parse }, code);
+ expect(parse).not.toHaveBeenCalled();
+ const result = createNodeEsmCompatBannerPlugin({ includeRequire: true }).renderChunk.call(
+ { parse },
+ code,
+ );
+ expect(parse).toHaveBeenCalledOnce();
+ expect(result?.code).not.toContain("__eveCreateRequire");
+ });
+
+ describe.each(["using", "await using"])("%s declarations", (declaration) => {
+ it.each(["__filename", "__dirname", "require"])(
+ "parses an existing %s binding instead of injecting a duplicate",
+ (name) => {
+ const parse = vi.fn(() => programWithTopLevelBindings(name));
+ const code = `${declaration} /* resource */ ${name} = { [Symbol.dispose]() {} };`;
+ const result = createNodeEsmCompatBannerPlugin({ includeRequire: true }).renderChunk.call(
+ { parse },
+ code,
+ );
+
+ expect(parse).toHaveBeenCalledOnce();
+ expect(result?.code).not.toContain(`const ${name} =`);
+ },
+ );
+ });
+
it("omits bindings declared later in a top-level variable list", () => {
const plugin = createNodeEsmCompatBannerPlugin({ includeRequire: true });
const chunk =
diff --git a/packages/eve/src/internal/node-esm-compat-banner.ts b/packages/eve/src/internal/node-esm-compat-banner.ts
index 1bf5773fd..723e0f64a 100644
--- a/packages/eve/src/internal/node-esm-compat-banner.ts
+++ b/packages/eve/src/internal/node-esm-compat-banner.ts
@@ -150,7 +150,10 @@ export function createNodeEsmCompatBannerPlugin(
return {
name: "eve-node-esm-compat-banner",
renderChunk(code, chunk) {
- const banner = buildNodeEsmCompatBanner(this.parse(code), options);
+ const program = mayDeclareCompatibilityBinding(code, options)
+ ? this.parse(code)
+ : { body: [] };
+ const banner = buildNodeEsmCompatBanner(program, options);
if (banner === "") {
return null;
@@ -168,6 +171,27 @@ export function createNodeEsmCompatBannerPlugin(
};
}
+const DECLARATION_TRIVIA = String.raw`(?:\s|/\*[\s\S]*?\*/|//[^\r\n\u2028\u2029]*[\r\n\u2028\u2029])*`;
+const PATH_BINDING_DECLARATION = new RegExp(
+ String.raw`(?:\b(?:var|let|const|using)\b|,)${DECLARATION_TRIVIA}(?:__filename|__dirname)\b`,
+);
+const REQUIRE_BINDING_DECLARATION = new RegExp(
+ String.raw`(?:\b(?:var|let|const|using)\b|,)${DECLARATION_TRIVIA}require\b`,
+);
+
+function mayDeclareCompatibilityBinding(
+ code: string,
+ options: NodeEsmCompatBannerOptions,
+): boolean {
+ // This is only a negative filter: comments/strings can cause extra parsing,
+ // never a missed declaration. Escaped identifiers always use the parser.
+ return (
+ code.includes("\\u") ||
+ PATH_BINDING_DECLARATION.test(code) ||
+ (options.includeRequire === true && REQUIRE_BINDING_DECLARATION.test(code))
+ );
+}
+
function createPrependedLineSourceMap({
insertedLineCount,
source,
@@ -178,43 +202,12 @@ function createPrependedLineSourceMap({
sourceContent: string;
}): SourceMap {
const originalLineCount = sourceContent.split("\n").length;
- const lineMappings = Array.from({ length: originalLineCount }, (_, index) =>
- encodeVlqFields(index === 0 ? [0, 0, 0, 0] : [0, 0, 1, 0]),
- );
return {
version: 3,
sources: [source],
sourcesContent: [sourceContent],
names: [],
- mappings: `${";".repeat(insertedLineCount)}${lineMappings.join(";")}`,
+ mappings: `${";".repeat(insertedLineCount)}AAAA${";AACA".repeat(originalLineCount - 1)}`,
};
}
-
-const BASE64_VLQ_CHARS = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
-const VLQ_BASE_SHIFT = 5;
-const VLQ_BASE = 1 << VLQ_BASE_SHIFT;
-const VLQ_BASE_MASK = VLQ_BASE - 1;
-const VLQ_CONTINUATION_BIT = VLQ_BASE;
-
-function encodeVlqFields(fields: readonly number[]): string {
- return fields.map((field) => encodeVlqInteger(field)).join("");
-}
-
-function encodeVlqInteger(value: number): string {
- let vlq = value < 0 ? (-value << 1) + 1 : value << 1;
- let encoded = "";
-
- do {
- let digit = vlq & VLQ_BASE_MASK;
- vlq >>>= VLQ_BASE_SHIFT;
-
- if (vlq > 0) {
- digit |= VLQ_CONTINUATION_BIT;
- }
-
- encoded += BASE64_VLQ_CHARS[digit];
- } while (vlq > 0);
-
- return encoded;
-}
diff --git a/packages/eve/src/internal/workflow-bundle/authored-workflow-directives.ts b/packages/eve/src/internal/workflow-bundle/authored-workflow-directives.ts
index 43ae20906..a5f2bc73b 100644
--- a/packages/eve/src/internal/workflow-bundle/authored-workflow-directives.ts
+++ b/packages/eve/src/internal/workflow-bundle/authored-workflow-directives.ts
@@ -1,5 +1,8 @@
import { parseWithNitroRolldownAst } from "#internal/bundler/nitro-rolldown.js";
-import { readWorkflowDirective } from "#internal/workflow-bundle/workflow-directive-ast.js";
+import {
+ mayContainWorkflowDirective,
+ readWorkflowDirective,
+} from "#internal/workflow-bundle/workflow-directive-ast.js";
const HOISTED_EXECUTE_NAME = "execute";
@@ -54,6 +57,9 @@ export async function prepareAuthoredWorkflowDirectives(input: {
readonly filePath: string;
readonly source: string;
}): Promise<AuthoredWorkflowDirectiveSource> {
+ if (!mayContainWorkflowDirective(input.source) && !input.source.includes("eve/workflow")) {
+ return { hasDirectives: false, hasWorkflowDirective: false, source: input.source };
+ }
const parsePath = /\.[cm]?js$/.test(input.filePath) ? `${input.filePath}.jsx` : input.filePath;
const program = (await parseWithNitroRolldownAst(parsePath, input.source)) as AstProgram;
const body = program.body ?? [];
diff --git a/packages/eve/src/internal/workflow-bundle/builder-support.ts b/packages/eve/src/internal/workflow-bundle/builder-support.ts
index e9bcfdc68..5efd8f884 100644
--- a/packages/eve/src/internal/workflow-bundle/builder-support.ts
+++ b/packages/eve/src/internal/workflow-bundle/builder-support.ts
@@ -576,7 +576,7 @@ async function writeWorkflowBundleAtomically(outfile: string, source: string): P
await atomicWriteFile(outfile, source);
}
-function mergeWorkflowManifest(target: WorkflowManifest, source: WorkflowManifest): void {
+export function mergeWorkflowManifest(target: WorkflowManifest, source: WorkflowManifest): void {
target.steps = mergeWorkflowManifestSection(target.steps, source.steps);
target.workflows = mergeWorkflowManifestSection(target.workflows, source.workflows);
target.classes = mergeWorkflowManifestSection(target.classes, source.classes);
diff --git a/packages/eve/src/internal/workflow-bundle/builder.ts b/packages/eve/src/internal/workflow-bundle/builder.ts
index d6c095bc5..ce931cded 100644
--- a/packages/eve/src/internal/workflow-bundle/builder.ts
+++ b/packages/eve/src/internal/workflow-bundle/builder.ts
@@ -1,5 +1,6 @@
import { mkdir, readFile, writeFile } from "node:fs/promises";
import { dirname, join, resolve } from "node:path";
+import { mapConcurrent } from "#shared/map-concurrent.js";
import {
resolvePackageSourceDirectoryPath,
@@ -17,6 +18,7 @@ import {
bundleFinalWorkflowOutput,
collectWorkflowInputFiles,
composeWorkflowDriverCode,
+ mergeWorkflowManifest,
convertClassesManifest,
convertStepsManifest,
convertWorkflowsManifest,
@@ -116,11 +118,12 @@ export class WorkflowBundleBuilder {
const workflowsOutfile = join(this.#outDir, "workflows.mjs");
const nitroStepOutfile = options.nitroStepOutfile;
const nitroWorkflowOutfile = options.nitroWorkflowOutfile;
- const writeStepEntry = (outfile: string) =>
+ const writeStepEntry = (outfile: string, precomputedManifest?: WorkflowManifest) =>
writeNitroStepEntrypoint({
builtinsPath: resolveWorkflowModulePath("workflow/internal/builtins"),
discoveredEntries: stepEntries,
outfile,
+ precomputedManifest,
preferAbsoluteFileImports: true,
projectRoot: this.config.projectRoot ?? this.config.workingDir,
sideEffectFiles: [this.#compiledArtifactsBootstrapPath],
@@ -128,7 +131,7 @@ export class WorkflowBundleBuilder {
});
const stepsManifest = await writeStepEntry(stepsOutfile);
if (nitroStepOutfile !== undefined && nitroStepOutfile !== stepsOutfile) {
- await writeStepEntry(nitroStepOutfile);
+ await writeStepEntry(nitroStepOutfile, stepsManifest);
}
const { manifest: workflowsManifest } = await this.createWorkflowsBundle({
additionalOutputs:
@@ -215,10 +218,13 @@ export class WorkflowBundleBuilder {
discoveredWorkflows: [],
};
- for (const filePath of inputs) {
+ const entries = await mapConcurrent(inputs, async (filePath) => {
const source = await readFile(filePath, "utf8");
const patterns = await findWorkflowPatterns(filePath, source);
+ return { filePath, patterns };
+ });
+ for (const { filePath, patterns } of entries) {
if (patterns.hasUseStep) discovered.discoveredSteps.push(filePath);
if (patterns.hasUseWorkflow) discovered.discoveredWorkflows.push(filePath);
if (patterns.hasSerde) discovered.discoveredSerdeFiles.push(filePath);
@@ -237,21 +243,27 @@ export class WorkflowBundleBuilder {
tsconfigPath,
}: WorkflowBundleCreateWorkflowsBundleOptions): Promise<WorkflowBundleCreateWorkflowsBundleResult> {
const manifest: WorkflowManifest = {};
- const frameworkChunk = await this.#buildDriverChunk({
- label: "framework",
- manifest,
- serdeFiles: frameworkSerdeFiles,
- tsconfigPath,
- workflowFiles: frameworkWorkflowFiles,
- });
- const appChunk = await this.#buildDriverChunk({
- label: "app",
- manifest,
- serdeFiles: [],
- tsconfigPath,
- workflowFiles: appWorkflowFiles,
- });
- const workflowCode = composeWorkflowDriverCode([frameworkChunk, appChunk]);
+ const chunks = await mapConcurrent(
+ [
+ {
+ label: "framework",
+ serdeFiles: frameworkSerdeFiles,
+ workflowFiles: frameworkWorkflowFiles,
+ },
+ { label: "app", serdeFiles: [], workflowFiles: appWorkflowFiles },
+ ],
+ async (layer) => {
+ const layerManifest: WorkflowManifest = {};
+ const code = await this.#buildDriverChunk({
+ ...layer,
+ manifest: layerManifest,
+ tsconfigPath,
+ });
+ return { code, manifest: layerManifest };
+ },
+ );
+ for (const chunk of chunks) mergeWorkflowManifest(manifest, chunk.manifest);
+ const workflowCode = composeWorkflowDriverCode(chunks.map((chunk) => chunk.code));
await Promise.all(
[{ outfile, stepRegistrationsPath }, ...additionalOutputs].map((output) =>
diff --git a/packages/eve/src/internal/workflow-bundle/nitro-step-entry.scenario.test.ts b/packages/eve/src/internal/workflow-bundle/nitro-step-entry.scenario.test.ts
index 74fc83342..bfee38b94 100644
--- a/packages/eve/src/internal/workflow-bundle/nitro-step-entry.scenario.test.ts
+++ b/packages/eve/src/internal/workflow-bundle/nitro-step-entry.scenario.test.ts
@@ -7,6 +7,27 @@ import { describe, expect, it } from "vitest";
import { writeNitroStepEntrypoint } from "#internal/workflow-bundle/nitro-step-entry.js";
describe("writeNitroStepEntrypoint", () => {
+ it("reuses discovered metadata without rereading step sources for a second output", async () => {
+ const root = await mkdtemp(join(tmpdir(), "eve-step-metadata-"));
+ try {
+ const manifest = { steps: { "step.ts": { work: { stepId: "step//work" } } } };
+ const outfile = join(root, "second", "steps.mjs");
+ const result = await writeNitroStepEntrypoint({
+ discoveredEntries: { discoveredSerdeFiles: [], discoveredSteps: [join(root, "step.ts")] },
+ precomputedManifest: manifest,
+ outfile,
+ preferAbsoluteFileImports: true,
+ projectRoot: root,
+ sideEffectFiles: [],
+ workingDir: root,
+ });
+ expect(result).toBe(manifest);
+ expect(await readFile(outfile, "utf8")).toContain("step.ts");
+ } finally {
+ await rm(root, { recursive: true, force: true });
+ }
+ });
+
it("serializes Windows absolute imports as file URLs", async () => {
const tempRoot = await mkdtemp(join(tmpdir(), "eve-nitro-step-entry-windows-"));
const outfile = join(tempRoot, "build", "steps.mjs");
diff --git a/packages/eve/src/internal/workflow-bundle/nitro-step-entry.ts b/packages/eve/src/internal/workflow-bundle/nitro-step-entry.ts
index 432aca63c..46b1a535a 100644
--- a/packages/eve/src/internal/workflow-bundle/nitro-step-entry.ts
+++ b/packages/eve/src/internal/workflow-bundle/nitro-step-entry.ts
@@ -20,6 +20,7 @@ export async function writeNitroStepEntrypoint(input: {
readonly builtinsPath?: string;
readonly discoveredEntries: NitroStepEntrypointDiscoveredEntries;
readonly outfile: string;
+ readonly precomputedManifest?: WorkflowManifest | undefined;
readonly preferAbsoluteFileImports?: boolean;
readonly projectRoot: string;
readonly sideEffectFiles: readonly string[];
@@ -30,12 +31,14 @@ export async function writeNitroStepEntrypoint(input: {
const serdeOnlyFiles = [...input.discoveredEntries.discoveredSerdeFiles]
.sort()
.filter((filePath) => !stepFileSet.has(filePath));
- const manifest = await collectNitroStepManifest({
- projectRoot: input.projectRoot,
- stepFiles,
- serdeOnlyFiles,
- workingDir: input.workingDir,
- });
+ const manifest =
+ input.precomputedManifest ??
+ (await collectNitroStepManifest({
+ projectRoot: input.projectRoot,
+ stepFiles,
+ serdeOnlyFiles,
+ workingDir: input.workingDir,
+ }));
const outfileDirectory = dirname(input.outfile);
const builtinsImportSpecifier =
input.builtinsPath === undefined
diff --git a/packages/eve/src/internal/workflow-bundle/workflow-builders.test.ts b/packages/eve/src/internal/workflow-bundle/workflow-builders.test.ts
index f15dc4ce5..ea9b8003d 100644
--- a/packages/eve/src/internal/workflow-bundle/workflow-builders.test.ts
+++ b/packages/eve/src/internal/workflow-bundle/workflow-builders.test.ts
@@ -10,10 +10,36 @@ import {
} from "#internal/application/package.js";
import { applyWorkflowTransform } from "./workflow-builders.js";
-import { transformWorkflowDirectives } from "./workflow-transformer.js";
+import {
+ findWorkflowDirectiveFunctions,
+ transformWorkflowDirectives,
+} from "./workflow-transformer.js";
import { withWorkflowStepAuthorization } from "#execution/tools/workflow/step-execution.js";
describe("applyWorkflowTransform", () => {
+ it.each([
+ ['"use step"', true],
+ [String.raw`"use\x20step"`, false],
+ [String.raw`"use \u0073tep"`, false],
+ ['"use \\\nstep"', false],
+ ])("preserves parser semantics for directive literals: %s", async (directive, recognized) => {
+ const source = `export async function work() { ${directive}; return 1; }`;
+ expect(await findWorkflowDirectiveFunctions("fixture.ts", source)).toEqual(
+ recognized ? [{ directive: "use step", name: "work" }] : [],
+ );
+ const transformed = await applyWorkflowTransform("fixture.ts", source, "step");
+ expect(transformed.workflowManifest.steps !== undefined).toBe(recognized);
+ });
+
+ it("leaves directive-free modules unchanged", async () => {
+ const source = "export function work() { return 1; }";
+ expect(await findWorkflowDirectiveFunctions("fixture.ts", source)).toEqual([]);
+ expect(await applyWorkflowTransform("fixture.ts", source, "step")).toEqual({
+ code: source,
+ workflowManifest: {},
+ });
+ });
+
it("preserves native arguments and receivers for Workflow built-in steps", async () => {
const filename = "src/internal/workflow/builtins.ts";
const source = readFileSync(resolvePackageSourceFilePath(filename), "utf8");
diff --git a/packages/eve/src/internal/workflow-bundle/workflow-directive-ast.ts b/packages/eve/src/internal/workflow-bundle/workflow-directive-ast.ts
index 21773880c..ed7b041b6 100644
--- a/packages/eve/src/internal/workflow-bundle/workflow-directive-ast.ts
+++ b/packages/eve/src/internal/workflow-bundle/workflow-directive-ast.ts
@@ -1,5 +1,10 @@
export type WorkflowDirective = "use step" | "use workflow";
+export function mayContainWorkflowDirective(source: string): boolean {
+ // Escapes can hide directive text; leave those sources to the syntax parser.
+ return source.includes("use step") || source.includes("use workflow") || source.includes("\\");
+}
+
export interface DirectiveStatementNode {
readonly directive?: unknown;
readonly expression?: unknown;
diff --git a/packages/eve/src/internal/workflow-bundle/workflow-transformer.ts b/packages/eve/src/internal/workflow-bundle/workflow-transformer.ts
index 1927da6e3..a04062fcb 100644
--- a/packages/eve/src/internal/workflow-bundle/workflow-transformer.ts
+++ b/packages/eve/src/internal/workflow-bundle/workflow-transformer.ts
@@ -1,19 +1,15 @@
import { WORKFLOW_REGISTRY_GLOBAL } from "#execution/workflow-registry.js";
import {
readWorkflowDirective,
+ mayContainWorkflowDirective,
type WorkflowDirective,
} from "#internal/workflow-bundle/workflow-directive-ast.js";
import { parseWithNitroRolldownAst } from "#internal/bundler/nitro-rolldown.js";
import type { WorkflowManifest } from "./workflow-builders.js";
-// Step names whose `stepId` must be emitted as the bare function name,
-// not `step//<idBase>//<name>`. The workflow-body shim's
-// `setAttributes` (and Workflow's compiled equivalent)
-// dispatches builtins via `useStep("<name>")` with the unqualified
-// identifier — if the registry stored these under their version-stamped
-// `step//...//name` ids, the lookup would miss and the runtime would
-// raise `Step "__builtin_*" is not registered in the current deployment`.
+// Workflow dispatches builtins using bare names; version-stamped step ids
+// would break registry lookups for these functions.
const BUILTIN_STEP_NAMES = new Set([
"__builtin_response_array_buffer",
"__builtin_response_json",
@@ -84,6 +80,7 @@ export async function findWorkflowDirectiveFunctions(
filename: string,
source: string,
): Promise<readonly { readonly directive: WorkflowDirective; readonly name: string }[]> {
+ if (!mayContainWorkflowDirective(source)) return [];
const ast = await parseWorkflowSource(filename, source);
return findDirectiveFunctions(ast).map((fn) => ({ directive: fn.directive, name: fn.name }));
}
@@ -114,7 +111,7 @@ export async function transformWorkflowDirectives(input: {
code: string;
workflowManifest: WorkflowManifest;
}> {
- if (input.mode === false) {
+ if (input.mode === false || !mayContainWorkflowDirective(input.source)) {
return { code: input.source, workflowManifest: {} };
}
diff --git a/packages/eve/src/services/dev-client/agent-info-probe.test.ts b/packages/eve/src/services/dev-client/agent-info-probe.test.ts
index 1806cb6d6..19ef3bdf0 100644
--- a/packages/eve/src/services/dev-client/agent-info-probe.test.ts
+++ b/packages/eve/src/services/dev-client/agent-info-probe.test.ts
@@ -22,6 +22,27 @@ async function advanceRetry(): Promise<void> {
}
describe("probeAgentInfo", () => {
+ it("bounds stalled inspection, aborts the request, and does not retry", async () => {
+ vi.useFakeTimers();
+ try {
+ let signal: AbortSignal | undefined;
+ const info = vi.fn(async (options?: { signal?: AbortSignal }) => {
+ signal = options?.signal;
+ return await new Promise<AgentInfoResult>(() => {});
+ });
+ const probe = probeAgentInfo({ client: { info }, timeoutMs: 2000 });
+ await vi.advanceTimersByTimeAsync(1999);
+ expect(signal?.aborted).toBe(false);
+ await vi.advanceTimersByTimeAsync(1);
+ expect(await probe).toMatchObject({ kind: "unavailable" });
+ expect(signal?.aborted).toBe(true);
+ expect(info).toHaveBeenCalledOnce();
+ expect(vi.getTimerCount()).toBe(0);
+ } finally {
+ vi.useRealTimers();
+ }
+ });
+
it("retries a transient server failure and returns inspection once the server is ready", async () => {
vi.useFakeTimers();
try {
diff --git a/packages/eve/src/services/dev-client/agent-info-probe.ts b/packages/eve/src/services/dev-client/agent-info-probe.ts
index 877e919b3..f14b66a99 100644
--- a/packages/eve/src/services/dev-client/agent-info-probe.ts
+++ b/packages/eve/src/services/dev-client/agent-info-probe.ts
@@ -28,17 +28,41 @@ function sleep(delayMs: number): Promise<void> {
*/
export async function probeAgentInfo(input: {
readonly client: Pick<Client, "info">;
+ readonly timeoutMs?: number;
}): Promise<AgentInfoProbeResult> {
+ const controller = new AbortController();
+ let timer: ReturnType<typeof setTimeout> | undefined;
+ const deadline = new Promise<AgentInfoProbeResult>((resolve) => {
+ timer = setTimeout(() => {
+ const error = new Error("Agent inspection timed out.");
+ controller.abort(error);
+ resolve({ kind: "unavailable", error });
+ }, input.timeoutMs ?? 5000);
+ });
try {
- return { kind: "ready", info: await input.client.info() };
+ // The deadline also bounds user-provided credential callbacks, which may ignore abort.
+ return await Promise.race([readAgentInfo(input.client, controller.signal), deadline]);
+ } finally {
+ clearTimeout(timer);
+ }
+}
+
+async function readAgentInfo(
+ client: Pick<Client, "info">,
+ signal: AbortSignal,
+): Promise<AgentInfoProbeResult> {
+ try {
+ return { kind: "ready", info: await client.info({ signal }) };
} catch (error) {
- if (!isRetryableAgentInfoFailure(error)) return { kind: "unavailable", error };
+ if (signal.aborted || !isRetryableAgentInfoFailure(error))
+ return { kind: "unavailable", error };
}
await sleep(RETRY_DELAY_MS);
try {
- return { kind: "ready", info: await input.client.info() };
+ signal.throwIfAborted();
+ return { kind: "ready", info: await client.info({ signal }) };
} catch (error) {
return { kind: "unavailable", error };
}
diff --git a/packages/eve/src/shared/map-concurrent.test.ts b/packages/eve/src/shared/map-concurrent.test.ts
new file mode 100644
index 000000000..c00c5b567
--- /dev/null
+++ b/packages/eve/src/shared/map-concurrent.test.ts
@@ -0,0 +1,55 @@
+import { describe, expect, it } from "vitest";
+import { mapConcurrent } from "#shared/map-concurrent.js";
+
+describe("mapConcurrent", () => {
+ it("bounds in-flight work and retains input order", async () => {
+ let active = 0;
+ let maximum = 0;
+ const result = await mapConcurrent(
+ [3, 2, 1, 0],
+ async (value) => {
+ maximum = Math.max(maximum, ++active);
+ for (let i = 0; i < value; i++) await Promise.resolve();
+ active--;
+ return value * 2;
+ },
+ 2,
+ );
+ expect(result).toEqual([6, 4, 2, 0]);
+ expect(maximum).toBe(2);
+ expect(active).toBe(0);
+ });
+
+ it("drains started work before rejecting and stops scheduling", async () => {
+ const running = Promise.withResolvers<void>();
+ const started: number[] = [];
+ const error = new Error("failed");
+ let settled = false;
+ const result = mapConcurrent(
+ [0, 1, 2],
+ async (value) => {
+ started.push(value);
+ if (value === 0) throw error;
+ await running.promise;
+ return value;
+ },
+ 2,
+ );
+ const observed = result.catch((failure) => {
+ settled = true;
+ return failure;
+ });
+ await Promise.resolve();
+ await Promise.resolve();
+ expect(settled).toBe(false);
+ expect(started).toEqual([0, 1]);
+ running.resolve();
+ expect(await observed).toBe(error);
+ });
+
+ it("handles empty inputs and rejects invalid concurrency", async () => {
+ expect(await mapConcurrent([], async () => 1)).toEqual([]);
+ await expect(mapConcurrent([], async () => 1, 0)).rejects.toThrow();
+ await expect(mapConcurrent([], async () => 1, 1.5)).rejects.toThrow();
+ });
+});
diff --git a/packages/eve/src/shared/map-concurrent.ts b/packages/eve/src/shared/map-concurrent.ts
new file mode 100644
index 000000000..0cf03a144
--- /dev/null
+++ b/packages/eve/src/shared/map-concurrent.ts
@@ -0,0 +1,29 @@
+/** Maps independent work in order, draining every started task before rejecting. */
+export async function mapConcurrent<T, R>(
+ values: readonly T[],
+ operation: (value: T, index: number) => Promise<R>,
+ concurrency = 8,
+): Promise<R[]> {
+ if (!Number.isInteger(concurrency) || concurrency < 1) {
+ throw new Error("Concurrency must be a positive integer.");
+ }
+ const results: R[] = [];
+ let next = 0;
+ let failed = false;
+ let failure: unknown;
+ await Promise.all(
+ Array.from({ length: Math.min(concurrency, values.length) }, async () => {
+ while (!failed && next < values.length) {
+ const index = next++;
+ try {
+ results[index] = await operation(values[index]!, index);
+ } catch (error) {
+ if (!failed) failure = error;
+ failed = true;
+ }
+ }
+ }),
+ );
+ if (failed) throw failure;
+ return results;
+}