The Open Framework for Building Agents
perf(eve): cut eve dev startup by about 40% (#4078)
Andrew Barba21e11d0ced7d
diff --git a/.changeset/faster-dev-startup.md b/.changeset/faster-dev-startup.mdnew file mode 100644index 000000000..48aeae02a--- /dev/null+++ b/.changeset/faster-dev-startup.md@@ -0,0 +1,5 @@+---+"eve": patch+---++`eve dev` now reaches a ready server about 40% sooner. The bundled self-modification extension no longer re-bundles eve's internals for each module it loads, and dev builds no longer re-parse large output chunks. Installing eve also no longer reports `npm audit` advisories for `undici`, which is now 8.10.2.diff --git a/packages/eve/package.json b/packages/eve/package.jsonindex f51ed6631..e540ec232 100644--- a/packages/eve/package.json+++ b/packages/eve/package.json@@ -522,7 +522,7 @@},"dependencies": {"nitro": "3.0.260903-beta",- "undici": "8.9.0"+ "undici": "8.10.2"},"devDependencies": {"@agentclientprotocol/sdk": "1.3.0",diff --git a/packages/eve/src/internal/application/package.ts b/packages/eve/src/internal/application/package.tsindex c2700b325..c018cf3d2 100644--- a/packages/eve/src/internal/application/package.ts+++ b/packages/eve/src/internal/application/package.ts@@ -65,7 +65,9 @@ function resolveCurrentModulePathFromStack(): string {}}-const require = createRequire(resolveCurrentModulePath());+// Not named `require`: bundles inline this module, and a top-level `require`+// binding makes the Node ESM compatibility banner re-parse the whole chunk.+const packageRequire = createRequire(resolveCurrentModulePath());function tryResolveVerifiedPackageRoot(packageJsonPath: string): string | undefined {try {@@ -267,7 +269,7 @@ export function resolvePackageSourceDirectoryPath(relativeSourcePath: string): sexport function resolvePackageDependencyPath(specifier: string): string {try {- return require.resolve(specifier);+ return packageRequire.resolve(specifier);} catch (error) {const packageRoot = tryResolvePackageRoot();const sourcePath =@@ -374,7 +376,7 @@ export function resolveInstalledPackageInfo(): InstalledPackageInfo {}try {- const resolvedPackageJsonPath = require.resolve(`${EVE_PACKAGE_NAME}/package.json`);+ const resolvedPackageJsonPath = packageRequire.resolve(`${EVE_PACKAGE_NAME}/package.json`);const resolvedPackageInfo = tryReadInstalledPackageInfo(resolvedPackageJsonPath,EVE_PACKAGE_NAME,@@ -446,7 +448,7 @@ export function resolveExpectedWorkflowVersion(): string | undefined {try {return readWorkflowVersionFromManifest(- JSON.parse(readFileSync(require.resolve(`${EVE_PACKAGE_NAME}/package.json`), "utf8")),+ JSON.parse(readFileSync(packageRequire.resolve(`${EVE_PACKAGE_NAME}/package.json`), "utf8")),);} catch {return undefined;@@ -479,5 +481,5 @@ export function resolveWorkflowModulePath(specifier: string): string {return resolvePackageCompiledFilePath(alias);}- return require.resolve(specifier);+ return packageRequire.resolve(specifier);}diff --git a/packages/eve/src/internal/authored-package-boundary.test.ts b/packages/eve/src/internal/authored-package-boundary.test.tsindex 082337900..068ed91a6 100644--- a/packages/eve/src/internal/authored-package-boundary.test.ts+++ b/packages/eve/src/internal/authored-package-boundary.test.ts@@ -3,6 +3,7 @@ import { fileURLToPath } from "node:url";import { describe, expect, it } from "vitest";+import { normalizeEsmImportSpecifier } from "#internal/application/import-specifier.js";import { resolvePackageDependencyPath } from "#internal/application/package.js";import {createGenerationPackageBoundaryPlugin,@@ -111,7 +112,7 @@ describe("createRuntimeLoaderPackageBoundaryPlugin", () => {});});- it("resolves eve package imports through the published dist mapping", async () => {+ it("binds eve package imports to the published dist modules", async () => {const plugin = createRuntimeLoaderPackageBoundaryPlugin({externalDependencies: [],packageRoot: PACKAGE_ROOT,@@ -136,7 +137,8 @@ describe("createRuntimeLoaderPackageBoundaryPlugin", () => {{ kind: "import-statement" },),).resolves.toEqual({- id: join(PACKAGE_ROOT, "dist/src/shared/git.js"),+ external: true,+ id: normalizeEsmImportSpecifier(join(PACKAGE_ROOT, "dist/src/shared/git.js")),});});diff --git a/packages/eve/src/internal/authored-package-boundary.ts b/packages/eve/src/internal/authored-package-boundary.tsindex a97b1c09c..205512f69 100644--- a/packages/eve/src/internal/authored-package-boundary.ts+++ b/packages/eve/src/internal/authored-package-boundary.ts@@ -5,6 +5,7 @@ import { dirname, join, resolve, sep } from "node:path";import { normalizeEsmImportSpecifier } from "#internal/application/import-specifier.js";import {resolvePackageDependencyPath,+ resolvePackageRoot,resolveWorkflowModulePath,} from "#internal/application/package.js";@@ -122,10 +123,14 @@ export function createRuntimeLoaderPackageBoundaryPlugin(input: {// condition used by the app build maps them to TypeScript source.// Resolve through Node's default package-import conditions here so a// packed eve installation does not leak #shared/* into the bundle.+ // eve's own compiled modules load by path: inlining them re-bundles most+ // of the framework for every eve-owned module this loader evaluates.if (source.startsWith("#")) {const resolvedPackageImport = resolvePackageImport(source, importer, input.packageRoot);if (resolvedPackageImport !== undefined) {- return { id: resolvedPackageImport };+ return isFrameworkPackagePath(resolvedPackageImport)+ ? { external: true, id: normalizeEsmImportSpecifier(resolvedPackageImport) }+ : { id: resolvedPackageImport };}}@@ -365,6 +370,13 @@ function resolvePackageImport(}}+let canonicalFrameworkPackageRoot: string | undefined;++function isFrameworkPackagePath(path: string): boolean {+ canonicalFrameworkPackageRoot ??= toCanonicalPath(resolvePackageRoot());+ return nearestPackageRoot(path) === canonicalFrameworkPackageRoot;+}+function resolveExistingExternalFilePath(id: string): string | undefined {if (existsSync(id)) {return id;diff --git a/packages/eve/src/internal/bundler/extension-mount-plugin.ts b/packages/eve/src/internal/bundler/extension-mount-plugin.tsindex 64ae3e792..f51471da3 100644--- a/packages/eve/src/internal/bundler/extension-mount-plugin.ts+++ b/packages/eve/src/internal/bundler/extension-mount-plugin.ts@@ -12,7 +12,7 @@ interface Mount {readonly programmaticImport?: { readonly specifier: string; readonly entryPath: string };}-function canonical(path: string): string {+function canonicalize(path: string): string {try {return realpathSync(path);} catch {@@ -30,6 +30,17 @@ export function createExtensionMountPlugin(overridePaths: ReadonlyMap<string, string> = new Map(),): Record<string, unknown> | null {if (mounts.length === 0) return null;+ // Every import in the bundle resolves through this plugin, and each JS+ // realpath walks the whole path. One plugin instance serves one build.+ const canonicalPaths = new Map<string, string>();+ const canonical = (path: string): string => {+ let canonicalPath = canonicalPaths.get(path);+ if (canonicalPath === undefined) {+ canonicalPath = canonicalize(path);+ canonicalPaths.set(path, canonicalPath);+ }+ return canonicalPath;+ };const roots = mounts.map((mount) => ({ ...mount, root: canonical(mount.sourceRoot) }));const declarationPaths = new Map(rootsdiff --git a/packages/eve/src/internal/node-esm-compat-banner.test.ts b/packages/eve/src/internal/node-esm-compat-banner.test.tsindex e1555212f..60b7df003 100644--- a/packages/eve/src/internal/node-esm-compat-banner.test.ts+++ b/packages/eve/src/internal/node-esm-compat-banner.test.ts@@ -134,6 +134,7 @@ describe("createNodeEsmCompatBannerPlugin", () => {"var // comment\n__dirname = '/';","const first = (() => { return 1; })(), /* next */ __dirname = '/';",String.raw`const __dirn\u0061me = '/';`,+ String.raw`let value = 1, \u{5f}_filename = '/x';`,])("parses possible declarations: %s", (code) => {const parse = vi.fn(() => programWithTopLevelBindings("__dirname"));const result = createNodeEsmCompatBannerPlugin().renderChunk.call({ parse }, code);@@ -141,6 +142,47 @@ describe("createNodeEsmCompatBannerPlugin", () => {expect(result?.code).not.toContain("const __dirname = __eveDirname");});+ it.each([+ "const line = `const __filename = fileURLToPath(import.meta.url);`; export const value = __dirname;",+ "const lines = ['var __dirname = x', \"let require = y\"]; export const value = __dirname;",+ ])("does not parse chunks that only quote a declaration: %s", (code) => {+ const parse = vi.fn(() => EMPTY_PROGRAM);+ const result = createNodeEsmCompatBannerPlugin({ includeRequire: true }).renderChunk.call(+ { parse },+ code,+ );+ expect(parse).not.toHaveBeenCalled();+ expect(result?.code).toContain("const __dirname =");+ });++ it.each([+ String.raw`const separators = /[\u2028,\u2029]/; export const value = __dirname;`,+ String.raw`const label = "caf\u00e9", other\u0041 = 1; export const value = __dirname;`,+ String.raw`const requir\u0065 = customRequire; export const value = __dirname;`,+ ])("does not parse chunks whose unicode escapes cannot name a binding: %s", (code) => {+ const parse = vi.fn(() => EMPTY_PROGRAM);+ const result = createNodeEsmCompatBannerPlugin().renderChunk.call({ parse }, code);+ expect(parse).not.toHaveBeenCalled();+ expect(result?.code).toContain("const __dirname =");+ });++ it("scans adjacent comments after a comma in linear time", () => {+ // Each boundary between adjacent comments must have exactly one parse;+ // otherwise a failed match retries 2^n comment partitions.+ const comments = Array.from({ length: 26 }, (_, index) => `/** Timer ${index}. */`);+ const code = `export const timers = [first,\n${comments.join("\n")}\nsecond];\nexport const directory = __dirname;`;+ const parse = vi.fn(() => EMPTY_PROGRAM);+ const startedAt = performance.now();+ const result = createNodeEsmCompatBannerPlugin({ includeRequire: true }).renderChunk.call(+ { parse },+ code,+ );++ expect(performance.now() - startedAt).toBeLessThan(1_000);+ expect(parse).not.toHaveBeenCalled();+ expect(result?.code).toContain("const __dirname =");+ });+it("checks require declarations only when providing a require shim", () => {const parse = vi.fn(() => programWithTopLevelBindings("require"));const code = "const require = customRequire;";diff --git a/packages/eve/src/internal/node-esm-compat-banner.ts b/packages/eve/src/internal/node-esm-compat-banner.tsindex 723e0f64a..02b3f1c2d 100644--- a/packages/eve/src/internal/node-esm-compat-banner.ts+++ b/packages/eve/src/internal/node-esm-compat-banner.ts@@ -171,27 +171,61 @@ export function createNodeEsmCompatBannerPlugin(};}-const DECLARATION_TRIVIA = String.raw`(?:\s|/\*[\s\S]*?\*/|//[^\r\n\u2028\u2029]*[\r\n\u2028\u2029])*`;+// A block comment body must not be able to span `*/`: a lazy `[\s\S]*?`+// lets a failed match re-end the comment at every later `*/`, which+// backtracks catastrophically on large JSDoc-heavy chunks.+const DECLARATION_TRIVIA = String.raw`(?:\s|/\*(?:[^*]|\*(?!/))*\*/|//[^\r\n\u2028\u2029]*[\r\n\u2028\u2029])*`;+// A keyword directly after a quote can only be string content. This plugin's+// own banner templates are bundled into every dev host, so without the+// lookbehind they would force a full parse of eve's largest chunk.+const DECLARATION_START = String.raw`(?:(?<![\w$"'\x60])(?:var|let|const|using)\b|,)${DECLARATION_TRIVIA}`;const PATH_BINDING_DECLARATION = new RegExp(- String.raw`(?:\b(?:var|let|const|using)\b|,)${DECLARATION_TRIVIA}(?:__filename|__dirname)\b`,+ String.raw`${DECLARATION_START}(?:__filename|__dirname)\b`,);-const REQUIRE_BINDING_DECLARATION = new RegExp(- String.raw`(?:\b(?:var|let|const|using)\b|,)${DECLARATION_TRIVIA}require\b`,+const REQUIRE_BINDING_DECLARATION = new RegExp(String.raw`${DECLARATION_START}require\b`);+const IDENTIFIER_ESCAPE = String.raw`\\u(?:[\da-fA-F]{4}|\{[\da-fA-F]+\})`;+const ESCAPED_IDENTIFIER_DECLARATION = new RegExp(+ String.raw`${DECLARATION_START}([\w$]*(?:${IDENTIFIER_ESCAPE}[\w$]*)+)`,+ "g",);+const IDENTIFIER_ESCAPE_PATTERN = new RegExp(IDENTIFIER_ESCAPE, "g");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.+ // never a missed declaration. Nearly every large bundle contains `\u` in+ // strings or regexes, so escaped names are decoded rather than treated as a+ // reason to parse the whole chunk.return (- code.includes("\\u") ||PATH_BINDING_DECLARATION.test(code) ||- (options.includeRequire === true && REQUIRE_BINDING_DECLARATION.test(code))+ (options.includeRequire === true && REQUIRE_BINDING_DECLARATION.test(code)) ||+ declaresEscapedCompatibilityBinding(code, options));}+function declaresEscapedCompatibilityBinding(+ code: string,+ options: NodeEsmCompatBannerOptions,+): boolean {+ if (!code.includes("\\u")) return false;+ for (const match of code.matchAll(ESCAPED_IDENTIFIER_DECLARATION)) {+ const name = match[1]!.replace(IDENTIFIER_ESCAPE_PATTERN, (escape) => {+ const codePoint = Number.parseInt(escape.slice(2).replace(/[{}]/g, ""), 16);+ return codePoint <= 0x10ffff ? String.fromCodePoint(codePoint) : "\uFFFD";+ });+ if (+ name === FILENAME_BANNER_LINE.bindingName ||+ name === DIRNAME_BANNER_LINE.bindingName ||+ (options.includeRequire === true && name === REQUIRE_LINE.bindingName)+ ) {+ return true;+ }+ }+ return false;+}+function createPrependedLineSourceMap({insertedLineCount,source,diff --git a/pnpm-lock.yaml b/pnpm-lock.yamlindex d12d8c7c4..67ac4200f 100644--- a/pnpm-lock.yaml+++ b/pnpm-lock.yaml@@ -1581,8 +1581,8 @@ importers:specifier: 3.0.260903-betaversion: 3.0.260903-betaundici:- specifier: 8.9.0- version: 8.9.0+ specifier: 8.10.2+ version: 8.10.2devDependencies:'@agentclientprotocol/sdk':specifier: 1.3.0@@ -29781,7 +29781,8 @@ snapshots:- [email protected]: {}+ optional: truedependencies: