A module named from the entry repo's root is that repo's module
Misha Kaletsky, 2026-11-26 10:57 UTC · c1e4df30d0efc557740c8f2e80866c713bb99610 · parent 854506c
4 files changed
- A core/itx/src/context/worker-loader.test.ts
- M core/itx/src/context/worker-loader.ts
- M core/mind-worker/src/context/module-resolution.test.ts
- M core/mind-worker/src/context/module-resolution.ts
A core/itx/src/context/worker-loader.test.ts
@@ -0,0 +1,53 @@ +import { expect, test } from "vitest"; +import { namedWorkerLoad } from "./worker-loader.ts"; + +test("a module of the published config is found by its name in the tree and by its name from the entry repo's root", () => { + const byRootName = namedWorkerLoad( + { spec: pointer(), vouched: true }, + "apps/agents/backend.ts", + "test", + "AgentsDurableObject", + ); + const byTreeName = namedWorkerLoad( + { spec: pointer(), vouched: true }, + "config/apps/agents/backend.ts", + "test", + ); + + expect(byRootName).toMatchObject({ moduleIdentity: "identity of the backend", generation: 7 }); + expect(byTreeName).toMatchObject({ moduleIdentity: "identity of the backend", generation: 7 }); + // a name that two repos have: with no folder it is the entry repo's, with a folder that repo's + expect(namedWorkerLoad({ spec: pointer(), vouched: true }, "agents.ts", "test")).toMatchObject({ + moduleIdentity: "identity of config's agents", + }); + expect( + namedWorkerLoad({ spec: pointer(), vouched: true }, "stdlib/agents.ts", "test"), + ).toMatchObject({ moduleIdentity: "identity of stdlib's agents" }); + expect(() => namedWorkerLoad({ spec: pointer(), vouched: true }, "apps/none.ts", "test")).toThrow( + 'test: the worker its source names publishes no module "apps/none.ts"', + ); + expect(() => + namedWorkerLoad({ spec: pointer(), vouched: true }, "apps/agents/backend.ts", "test", "Other"), + ).toThrow('publishes no Durable Object class "Other" in "apps/agents/backend.ts"'); +}); + +/** The config pointer of a mind whose entry repo is `config`, as a publication writes it. */ +function pointer() { + return { + source: ["itx", "config", ["modules", {}]], + cacheKey: "tree", + mainModule: "config/worker.ts", + manifest: { + generation: 7, + modules: { + "config/worker.ts": { identity: "identity of the entry", classes: [] }, + "config/agents.ts": { identity: "identity of config's agents", classes: [] }, + "stdlib/agents.ts": { identity: "identity of stdlib's agents", classes: [] }, + "config/apps/agents/backend.ts": { + identity: "identity of the backend", + classes: ["AgentsDurableObject"], + }, + }, + }, + }; +}
M core/itx/src/context/worker-loader.ts
@@ -33,6 +33,8 @@ "a worker's source is its files or an itx expression that produces them", ), cacheKey: z.string().optional(), + // the module the worker loads as its main one when a load names no other + mainModule: z.string().optional(), // read only from a rule the platform wrote (`vouched`) manifest: z.unknown().optional(), }); @@ -77,10 +79,18 @@ where: string, className?: string, ): { source: WorkerSource; cacheKey?: string; moduleIdentity?: string; generation?: number } { - const { source, cacheKey, manifest: given } = NamedWorkerSpec.parse(named.spec); + const { source, cacheKey, mainModule: main, manifest: given } = NamedWorkerSpec.parse(named.spec); const manifest = named.vouched && given !== undefined ? WorkerManifest.parse(given) : undefined; if (!manifest || !mainModule) return { source, cacheKey, generation: manifest?.generation }; - const published = manifest.modules[mainModule]; + // A MODULE'S NAME WITH NO REPO'S FOLDER IS THE ENTRY REPO'S. The manifest names each module by + // its place in the tree of the mind's repos (`config/apps/agents/backend.ts`), and a mind whose + // code was written as one repo names it from that repo's root (`apps/agents/backend.ts`), in + // rows that outlive every commit. The loader finds the file the same way (core/mind-worker + // src/context/module-resolution.ts `moduleNamed`), so both names are one module, one identity. + const entryFolder = main?.includes("/") ? main.slice(0, main.indexOf("/")) : undefined; + const published = + manifest.modules[mainModule] || + (entryFolder ? manifest.modules[`${entryFolder}/${mainModule}`] : undefined); if (!published) throw new Error( `${where}: the worker its source names publishes no module ${JSON.stringify(mainModule)}`,
M core/mind-worker/src/context/module-resolution.test.ts
@@ -66,6 +66,40 @@ ); }); +test("a main module named from the entry repo's root loads that repo's file, in a tree of repos", async () => { + const tree = { + "package.json": JSON.stringify({ main: "config/worker.ts" }), + "config/worker.ts": "export default {};\n", + "config/apps/agents/backend.ts": 'import { step } from "@repos/stdlib/step.ts";\nexport const run = step;\n', + "stdlib/step.ts": "export const step = () => {};\n", + "stdlib/agents.ts": "export const agents = 1;\n", + "config/agents.ts": "export const agents = 2;\n", + }; + + const byRootName = await resolveModules(tree, { ...options(), mainModule: "apps/agents/backend.ts" }); + const byTreeName = await resolveModules(tree, { + ...options(), + mainModule: "config/apps/agents/backend.ts", + }); + + expect(byRootName).toMatchObject({ + mainModule: "config/apps/agents/backend.js", + modules: { "config/apps/agents/backend.js": expect.stringContaining('from "../../../stdlib/step.js"') }, + }); + expect(byRootName.modules).toEqual(byTreeName.modules); + // a name that two repos have, with no folder, is the entry repo's + expect(await resolveModules(tree, { ...options(), mainModule: "agents.ts" })).toMatchObject({ + mainModule: "config/agents.js", + }); + // and with another repo's folder it is that repo's + expect(await resolveModules(tree, { ...options(), mainModule: "stdlib/agents.ts" })).toMatchObject({ + mainModule: "stdlib/agents.js", + }); + await expect(resolveModules(tree, { ...options(), mainModule: "apps/none.ts" })).rejects.toThrow( + 'no module "apps/none.ts" to load as the main module', + ); +}); + /** A config whose worker.ts is `worker`, beside one code file in a folder. */ function sourceWith(worker: string) { return {
M core/mind-worker/src/context/module-resolution.ts
@@ -296,6 +296,18 @@ return [...folders].map(([folder, importedBy]) => ({ folder, importedBy })); } +/** A MODULE'S NAME WITH NO REPO'S FOLDER IS THE ENTRY REPO'S. The tree of a mind's repos holds + * each file under its repo's folder (`config/apps/agents/backend.ts`). A mind whose code was + * written as one repo names its modules from that repo's root (`apps/agents/backend.ts`), in + * rows that outlive every commit. So a name that is no file of the source is looked for in the + * folder of the source's main module, and found there it is that file. A name that is a file + * is that file, whatever the entry's folder holds. */ +export function moduleNamed(source: SourceFiles, name: string, main: string): string { + if (Object.hasOwn(source, name) || !main.includes("/")) return name; + const inEntryRepo = `${main.slice(0, main.indexOf("/"))}/${name}`; + return Object.hasOwn(source, inEntryRepo) ? inEntryRepo : name; +} + export async function resolveModules( source: SourceFiles, opts: ResolveOptions, @@ -308,7 +320,7 @@ const { where, platform } = opts; const { entry: main, dependencies, compatibilityFlags } = readPackage(source, where); const nodejsCompat = compatibilityFlags?.includes("nodejs_compat") ?? false; - const entry = opts.mainModule ? joinPath("", opts.mainModule) : main; + const entry = opts.mainModule ? moduleNamed(source, joinPath("", opts.mainModule), main) : main; if (!Object.hasOwn(source, entry)) throw new Error( `${where}: no module ${JSON.stringify(opts.mainModule)} to load as the main module`,