kernel / core / mind-worker / src / approved-acts.tsmain at 949894f
// approved-acts.ts — AN ACT A MEMBER APPROVED AFTER ITS CALLER STOPPED WAITING (iterate/acts). The
// vault names it to the boundary with the approval, and the boundary hands it here. The mind does
// it now, and writes what came of it on the context its caller named, so that an agent which went
// on reads "your mail was approved and sent". The gate decides whether the act is still to do: it
// lets one go once, so a second hand-over does nothing.
import type { ReachableContext } from "core-itx";
import { ACT_DONE, actOfText, APPROVALS_CONTEXT, isMailAct } from "iterate/acts";
import { normalizedItxExpression } from "iterate/expression";
import type { StreamEventInput } from "iterate/stream/processor";
import type { Door } from "iterate/workers";
import { EMAIL_PATH, sendApprovedMail } from "./integrations/email.ts";
export async function runApprovedAct(
deps: {
files: R2Bucket;
egress: Pick<Door, "sendEmail" | "act">;
contextAt: (path: string) => ReachableContext;
appendAsPlatform: (path: string, events: StreamEventInput[]) => Promise<unknown>;
},
held: { digest: string; text: string; from?: string },
): Promise<void> {
const act = actOfText(held.text);
if (!act) throw new Error(`the approved act ${held.digest} is no act this mind can do`);
// `from` is the caller's claim: any context of the mind, or the gate's own when it named none
const caller = held.from?.startsWith("/") ? held.from : APPROVALS_CONTEXT;
const outcome = await (async () => {
if (isMailAct(act)) {
const sent = await sendApprovedMail(
{ files: deps.files, egress: () => deps.egress, emailContext: deps.contextAt(EMAIL_PATH) },
act,
caller,
);
return { ok: true as const, sent: { path: EMAIL_PATH, offset: sent.offset } };
}
// A verb, called at the context that read its name (iterate/acts `VerbAct`, `at`). The call
// arrives there as the caller's call did: with the caller's context as its origin, as
// loaded code's when loaded code asked, and as a row's run when a stored row's run asked. An act
// that names no context was held by a build before this one: it is called from its caller's
// context, and the kernel refuses it, since the text it makes now names one. The gate lets
// the act go once, and the kernel says whether the call still is that act
// (core/itx/src/context/itx-expression-rewriting.ts `#heldUntilApproved`).
const gate = await deps.egress.act({ text: held.text, from: caller });
if (gate.state !== "go") throw new Error(`the gate says "${gate.state}": it is not to do now`);
const names = normalizedItxExpression(act.act);
const at = act.at || caller;
const result = await deps
.contextAt(at)
.invoke([...names.slice(0, -1), [String(names.at(-1)), ...act.args]], [], {
principal: null,
approvedAct: held.text,
...(at !== caller && { path: caller }),
...(act.byLoadedCode && { app: true as const }),
...(act.byRow && { runsRow: true as const }),
});
// what a verb answers may be anything: the fact keeps what JSON can say of it
return { ok: true as const, result: jsonOf(result) };
})().catch((error: unknown) => ({
ok: false as const,
error: error instanceof Error ? error.message : String(error),
}));
console.info({ event: "approval.act-done", digest: held.digest, act: act.act, ok: outcome.ok });
await deps.appendAsPlatform(caller, [
{
type: ACT_DONE,
idempotencyKey: `approval/act-done:${held.digest}`,
payload: { digest: held.digest, act: act.act, ...outcome },
},
]);
}
function jsonOf(value: unknown): unknown {
try {
return JSON.parse(JSON.stringify(value ?? null));
} catch {
return String(value);
}
}