Skip to content

armgit / eve

Public Imported from GitHub
0

The Open Framework for Building Agents

fix(eve): let each extension mount own its distribution's shared chunks (#4171)

e0-gh-vercel-connect[bot]11e7cad9ac51
diff --git a/.changeset/extension-shared-chunk-mounts.md b/.changeset/extension-shared-chunk-mounts.md
new file mode 100644
index 000000000..7376a5b5f
--- /dev/null
+++ b/.changeset/extension-shared-chunk-mounts.md
@@ -0,0 +1,5 @@
+---
+"eve": patch
+---
+
+An installed extension mounted in both the root agent and a subagent now builds when its distribution has shared chunks in `dist/_chunks`. Each mount owns those chunks, so they read that mount's configuration and state instead of failing with `refers to multiple extension mounts`.
diff --git a/packages/eve/src/internal/authored-module-loader.ts b/packages/eve/src/internal/authored-module-loader.ts
index f06e1a439..44f1b6438 100644
--- a/packages/eve/src/internal/authored-module-loader.ts
+++ b/packages/eve/src/internal/authored-module-loader.ts
@@ -6,7 +6,10 @@ import { extensionOverridePaths } from "#compiler/extension-mount-bindings.js";
import type { CompiledAgentManifest } from "#compiler/manifest.js";
import { createCompiledModuleMapSource } from "#compiler/module-map.js";
import { createAuthoredAssetImportPlugin } from "#internal/authored-asset-import-plugin.js";
-import { createExtensionMountPlugin } from "#internal/bundler/extension-mount-plugin.js";
+import {
+ createExtensionMountPlugin,
+ EXTENSION_CHUNK_DIRECTORY,
+} from "#internal/bundler/extension-mount-plugin.js";
import { authoredModuleConditions } from "#internal/authored-module-conditions.js";
import { createAuthoredModuleBundleError } from "#internal/authored-module-bundle.js";
import { createAuthoredModuleEvaluationError } from "#internal/authored-module-evaluation-error.js";
@@ -270,7 +273,7 @@ export async function bundleExtensionDistributionGraph(input: {
tsconfig: resolveAuthoredTsConfigPath(input.packageRoot),
write: false,
output: {
- chunkFileNames: "_chunks/[name]-[hash].mjs",
+ chunkFileNames: `${EXTENSION_CHUNK_DIRECTORY}/[name]-[hash].mjs`,
codeSplitting: true,
comments: false,
entryFileNames: "[name].mjs",
diff --git a/packages/eve/src/internal/bundler/extension-mount-plugin.ts b/packages/eve/src/internal/bundler/extension-mount-plugin.ts
index 97c2d845f..0e8e62c31 100644
--- a/packages/eve/src/internal/bundler/extension-mount-plugin.ts
+++ b/packages/eve/src/internal/bundler/extension-mount-plugin.ts
@@ -1,8 +1,11 @@
import { readFileSync, realpathSync } from "node:fs";
-import { resolve, sep } from "node:path";
+import { dirname, join, resolve, sep } from "node:path";
const MOUNT_QUERY = "?eve-mount=";
+/** Directory beside an extension distribution's source root that holds its shared chunks. */
+export const EXTENSION_CHUNK_DIRECTORY = "_chunks";
+
interface Mount {
readonly mountId: string;
readonly sourceRoot: string;
@@ -24,6 +27,11 @@ function within(path: string, root: string): boolean {
return path === root || path.startsWith(`${root}${sep}`);
}
+/** Whether `path` is the extension's own code: its source, or a chunk shared by that source. */
+export function isExtensionModule(path: string, root: string): boolean {
+ return within(path, root) || within(path, join(dirname(root), EXTENSION_CHUNK_DIRECTORY));
+}
+
/** Isolates extension-owned source modules, but leaves ordinary dependencies shared. */
export function createExtensionMountPlugin(
mounts: readonly Mount[],
@@ -114,7 +122,7 @@ export function createExtensionMountPlugin(
const override =
mount !== undefined &&
importerPath !== undefined &&
- !within(importerPath, mount.root) &&
+ !isExtensionModule(importerPath, mount.root) &&
overrideMounts.get(importerPath) === mount;
const sourceMount =
mount !== undefined &&
@@ -142,7 +150,7 @@ export function createExtensionMountPlugin(
const path = canonical(resolved.id);
if (
mount !== undefined &&
- (sourceMount !== undefined || (!override && within(path, mount.root)))
+ (sourceMount !== undefined || (!override && isExtensionModule(path, mount.root)))
) {
return { id: `${resolved.id}${MOUNT_QUERY}${encodeURIComponent(mountId!)}` };
}
@@ -150,7 +158,7 @@ export function createExtensionMountPlugin(
if (mount === undefined && (importer === undefined || importer.startsWith("\0"))) {
return undefined;
}
- const owners = roots.filter((root) => within(path, root.root));
+ const owners = roots.filter((root) => isExtensionModule(path, root.root));
if (mount !== undefined) {
// Source owned by another extension keeps that extension's mount identity, so a mount
// declared inside an extension reads the configuration its enclosing mount bound. When
diff --git a/packages/eve/src/internal/bundler/extension-scope-plugin.test.ts b/packages/eve/src/internal/bundler/extension-scope-plugin.test.ts
index f878cd070..b038039da 100644
--- a/packages/eve/src/internal/bundler/extension-scope-plugin.test.ts
+++ b/packages/eve/src/internal/bundler/extension-scope-plugin.test.ts
@@ -42,6 +42,19 @@ describe("createExtensionScopePlugin (path containment)", () => {
);
});
+ it("scopes a shared chunk emitted beside the source root to its tagged mount", () => {
+ const plugin = createExtensionScopePlugin([
+ ...SCOPES,
+ { ...SCOPES[0]!, mountId: "subagents/research/extensions/crm" },
+ ])!;
+ expect(
+ plugin.resolveId(
+ "eve/context",
+ "/pkg/crm/_chunks/budget-abc123.mjs?eve-mount=subagents%2Fresearch%2Fextensions%2Fcrm",
+ ),
+ ).toBe("\0eve-ext-scope:context:subagents%2Fresearch%2Fextensions%2Fcrm");
+ });
+
it("leaves eve/extension unscoped for extension-owned importers", () => {
const id = pathPlugin().resolveId("eve/extension", "/pkg/crm/extension/config.ts");
expect(id).toBeUndefined();
diff --git a/packages/eve/src/internal/bundler/extension-scope-plugin.ts b/packages/eve/src/internal/bundler/extension-scope-plugin.ts
index 7e840016f..cfc1f1d23 100644
--- a/packages/eve/src/internal/bundler/extension-scope-plugin.ts
+++ b/packages/eve/src/internal/bundler/extension-scope-plugin.ts
@@ -1,5 +1,7 @@
import { realpathSync } from "node:fs";
-import { resolve, sep } from "node:path";
+import { resolve } from "node:path";
+
+import { isExtensionModule } from "#internal/bundler/extension-mount-plugin.js";
/**
* One extension's on-disk source root paired with its logical mount identity.
@@ -33,10 +35,6 @@ function importerPath(importer: string): string {
return canonicalize(queryIndex === -1 ? importer : importer.slice(0, queryIndex));
}
-function isUnder(path: string, root: string): boolean {
- return path === root || path.startsWith(`${root}${sep}`);
-}
-
function shimSource(mountId: string): string {
return [
`import { defineMountedState } from "eve/internal/mount-state";`,
@@ -103,10 +101,10 @@ export function createExtensionScopePlugin(
const mountId = decodeURIComponent(importer.slice(mountQuery + "?eve-mount=".length));
const owned = canonicalScopes.find((scope) => scope.mountId === mountId);
if (owned === undefined) throw new Error(`Unknown extension mount "${mountId}".`);
- return isUnder(importerPath(importer), owned.root) ? owned.mountId : undefined;
+ return isExtensionModule(importerPath(importer), owned.root) ? owned.mountId : undefined;
}
const path = importerPath(importer);
- const matches = canonicalScopes.filter((scope) => isUnder(path, scope.root));
+ const matches = canonicalScopes.filter((scope) => isExtensionModule(path, scope.root));
if (matches.length === 0) return undefined;
if (matches.length > 1) {
throw new Error(`Ambiguous extension scope for "${path}".`);
diff --git a/packages/eve/test/scenarios/mounted-extension-installed.scenario.test.ts b/packages/eve/test/scenarios/mounted-extension-installed.scenario.test.ts
index e9f7a00de..865610711 100644
--- a/packages/eve/test/scenarios/mounted-extension-installed.scenario.test.ts
+++ b/packages/eve/test/scenarios/mounted-extension-installed.scenario.test.ts
@@ -197,7 +197,9 @@ const EXT_TREE: Readonly<Record<string, string>> = {
* is placed under the consumer's node_modules, so the consumer must discover
* and normalize the emitted agent-shaped distribution.
*/
-async function buildInstalledExtensionFiles(): Promise<Record<string, string>> {
+async function buildInstalledExtensionFiles(
+ tree: Readonly<Record<string, string>> = EXT_TREE,
+): Promise<Record<string, string>> {
const extRoot = await createScratchDirectory("eve-ext-src-");
await writeFile(
join(extRoot, "package.json"),
@@ -221,7 +223,7 @@ async function buildInstalledExtensionFiles(): Promise<Record<string, string>> {
join(extRoot, "node_modules", "eve"),
"dir",
);
- for (const [path, contents] of Object.entries(EXT_TREE)) {
+ for (const [path, contents] of Object.entries(tree)) {
await mkdir(dirname(join(extRoot, path)), { recursive: true });
await writeFile(join(extRoot, path), contents, "utf8");
}
@@ -417,4 +419,71 @@ describe("mounted extension installed under node_modules", () => {
"Prefer the CRM tools for account questions.",
);
});
+
+ it("binds each mount's config in a shared chunk when mounted in the root and a subagent", async () => {
+ const tool = (description: string) =>
+ [
+ 'import { defineTool } from "eve/tools";',
+ 'import { apiKey } from "../../lib/api-key.js";',
+ "export default defineTool({",
+ ` description: "${description}",`,
+ ' inputSchema: { type: "object", properties: {}, additionalProperties: false },',
+ " async execute() { return { apiKey: apiKey() }; },",
+ "});",
+ "",
+ ].join("\n");
+ // A helper outside the extension source shared by two tools is emitted to `dist/_chunks`.
+ const extensionFiles = await buildInstalledExtensionFiles({
+ "extension/extension.ts": EXT_TREE["extension/extension.ts"]!,
+ "lib/api-key.ts": [
+ 'import extension from "../extension/extension.js";',
+ "export const apiKey = () => extension.config.apiKey;",
+ "",
+ ].join("\n"),
+ "extension/tools/echo.ts": tool("Echo the configured API key."),
+ "extension/tools/shout.ts": tool("Shout the configured API key."),
+ });
+ expect(
+ Object.keys(extensionFiles).some((path) =>
+ path.startsWith(`node_modules/${PACKAGE_NAME}/dist/_chunks/`),
+ ),
+ ).toBe(true);
+ const app = await createAppRoot("eve-mounted-extension-chunks-", {
+ files: {
+ "agent/agent.mjs": 'export default { model: "openai/gpt-5.4" };\n',
+ "agent/instructions.md": "You are a precise assistant.\n",
+ "agent/extensions/crm.mjs": [
+ `import crm from "${PACKAGE_NAME}";`,
+ 'export default crm({ apiKey: "sk-root" });',
+ "",
+ ].join("\n"),
+ "agent/subagents/manager/agent.mjs": [
+ "export default {",
+ ' model: "openai/gpt-5.4",',
+ ' description: "Manage CRM reviews.",',
+ "};",
+ "",
+ ].join("\n"),
+ "agent/subagents/manager/extensions/crm.mjs": [
+ `import crm from "${PACKAGE_NAME}";`,
+ 'export default crm({ apiKey: "sk-manager" });',
+ "",
+ ].join("\n"),
+ ...extensionFiles,
+ },
+ });
+
+ const { graph } = await compileRuntimeGraph(app.appRoot);
+
+ const rootEcho = graph.root.agent.tools.find((entry) => entry.name === "crm__echo");
+ await expect(rootEcho?.execute?.({}, { messages: [], toolCallId: "call_1" })).resolves.toEqual({
+ apiKey: "sk-root",
+ });
+ const manager = graph.root.subagentRegistry.subagentsByName.get("manager");
+ const managerNode = graph.nodesByNodeId.get(manager!.definition.nodeId);
+ const managerShout = managerNode?.agent.tools.find((entry) => entry.name === "crm__shout");
+ await expect(
+ managerShout?.execute?.({}, { messages: [], toolCallId: "call_2" }),
+ ).resolves.toEqual({ apiKey: "sk-manager" });
+ });
});