iterate.com / kernel

git clone https://git.iterate.com/repos/kernel

A module named from the entry repo's root is that repo's module

iterate.com moved to this kernel on 26 Nov 2026. Its config was written as one repo, so
its rows name a module from that repo's root: `mainModule: "apps/agents/backend.ts"`,
with the published config as the source. A publication here holds each repo's files
under the repo's folder, so the manifest and the tree know that module as
`config/apps/agents/backend.ts`. After the first publication under this kernel
(10:49 UTC) `itx.agents` and `itx.aiSubscriptions` found no such module.

Now a name that the tree does not hold is looked for in the entry repo's folder, in the
two places a name is read:

- core/itx worker-loader.ts `parseNamedWorkerLoad`: the manifest's lookup. The entry's
  folder is read from the pointer's own `mainModule`, so the publication that is live
  already answers, with no new commit.
- core/mind-worker module-resolution.ts `moduleNamed`: the file that loads as the main
  module, and so its identity, which is the same under both names.

A name that is a file of the tree is that file, as before: `stdlib/agents.ts` stays
stdlib's, and `agents.ts` is the entry repo's.

Checked: every package type-checks; 3,114 vitest tests and 85 node tests pass.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_0132GvceJB67nsAyUdjKauve

Misha Kaletsky, 2026-11-26 10:57 UTC · c1e4df30d0efc557740c8f2e80866c713bb99610 · parent 854506c

4 files changed

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`,