kernel / core / mind-worker / src / mind / durable-object.tsmain at 949894f
import { StreamProcessorDurableObject, type ItxEntrypointService } from "iterate/sdk";
import { contextAt, runningCause, type ItxEntrypointScope } from "core-itx";
import { MindRepos } from "../config.ts";
import type { SourceFiles } from "../context/module-resolution.ts";
import { keepModulesOfIdentity, moduleIdentityOf } from "../context/worker-loader.ts";
import { EntityCollectionRpcTarget } from "./collection.ts";
import type { MindState } from "./contract.ts";
import type { EntitySlug } from "./entity-lifecycle.ts";
import { MindProcessor } from "./processor.ts";
import { repoCall, type MindPublisher } from "./publication.ts";
import { mergedTree } from "./tree.ts";
import { z } from "zod";
/** What `tree` is asked: the commit of each repo, and the entry that the tree's root names. */
const TreeAsked = z.object({
commits: z.record(z.string(), z.string().min(1)),
entry: MindRepos.shape.entry,
});
export class MindFacet extends StreamProcessorDurableObject<
MindState,
{ ITX?: ItxEntrypointService },
ItxEntrypointScope
> {
static override publicMethods = [
...super.publicMethods,
"repos",
"workspaces",
"sandboxes",
"sandboxImage",
"tree",
];
processor = new MindProcessor(
() => this.getItx(),
() => this.#publisher(),
);
async #filesOf(repoPath: string, commitOid: string): Promise<SourceFiles> {
using itx = this.getItx();
// the repo facet answers an itx expression, which is typed `unknown`
return (await itx.invoke(repoCall(repoPath, "files", { commitOid }))) as SourceFiles;
}
/** THE FILES OF A SET OF COMMITS AS ONE TREE (tree.ts): what the published code's pointer names
* as its source. A load whose modules are not kept yet reads it; it reads each repo at the
* commit that the publication recorded, so the same pointer always answers the same tree. */
async tree(asked: unknown): Promise<SourceFiles> {
const { commits, entry } = TreeAsked.parse(asked);
const read = await Promise.all(
Object.entries(commits).map(
async ([repoPath, commitOid]) =>
[repoPath, await this.#filesOf(repoPath, commitOid)] as const,
),
);
return mergedTree(entry, Object.fromEntries(read));
}
#publisher(): MindPublisher {
const root = contextAt("/");
return {
head: async (repoPath) => {
using itx = this.getItx();
return (await itx.invoke(repoCall(repoPath, "tip"))) as string | null;
},
files: (repoPath, commitOid) => this.#filesOf(repoPath, commitOid),
identityOf: (files, mainModule) =>
moduleIdentityOf(files, mainModule, `the mind's ${mainModule}`),
keepModules: (files, mainModule) =>
keepModulesOfIdentity(files, mainModule, `the mind's ${mainModule}`),
probe: async (files, mainModule) => {
using itx = this.getItx();
return await itx.invoke([
"itx",
"builtins",
"workers",
["get", { source: files, mainModule }],
["probe"],
]);
},
// caused by what the follower reacts to, so the init it sets off runs one deeper than the commit
appendAsPlatform: (...events) =>
root.invoke(["itx", "builtins", ["append", ...events]], [], {
principal: null,
platform: true,
cause: runningCause(),
}),
};
}
// Methods, not fields: Workers RPC reaches only what the prototype declares.
#collections = new Map<string, EntityCollectionRpcTarget>();
#collection(slug: EntitySlug): EntityCollectionRpcTarget {
const known = this.#collections.get(slug);
if (known) return known;
const collection = new EntityCollectionRpcTarget(
slug,
() => this.getItx(),
async () => (await this.snapshot()).state,
);
this.#collections.set(slug, collection);
return collection;
}
repos(): EntityCollectionRpcTarget {
return this.#collection("repo");
}
workspaces(): EntityCollectionRpcTarget {
return this.#collection("workspace");
}
sandboxes(): EntityCollectionRpcTarget {
return this.#collection("sandbox");
}
/** The image that `events.iterate.com/mind/sandbox-image-configured` names, else null. Here and
* not by a path into the snapshot: the state of a root reduced before the field existed has no
* such key, and a path to a missing key fails the call. */
async sandboxImage(): Promise<string | null> {
return (await this.snapshot()).state.sandboxImage ?? null;
}
}