Repository Public
1/**2 * The ONLY file that touches `@aos/kernel`. It maps `KernelPort` onto the kernel's exports3 * (`packages/kernel/src/index.ts`, CONTRACT §2) with ordinary typed imports: a signature change in4 * the kernel is a compile error here. The server owns no kernel write — every one goes through5 * this port, on the adapter's own kernel handle (own pool, artifact root).6 */7import {8 createKernel, createMission, listenEvents, migrate, openArtifact, postOwnerMessage, requestCancel, resolveWait,9} from '@aos/kernel';10import type { AosEvent } from '@aos/shared';11import type { CreateMissionInput, KernelPort } from './kernel-port.js';1213/** Applies `db/migrations` (advisory-locked, idempotent). Migrations belong to the kernel. */14export async function kernelMigrate(databaseUrl: string): Promise<void> {15 await migrate(databaseUrl);16}1718export interface LoadedKernel {19 port: KernelPort;20 /** Ends the kernel's own pool. */21 close(): Promise<void>;22}2324export function loadKernel(opts: { databaseUrl: string; dataDir: string }): LoadedKernel {25 const k = createKernel({ databaseUrl: opts.databaseUrl, dataDir: opts.dataDir, poolMax: 5 });26 const port: KernelPort = {27 createMission: (input: CreateMissionInput) => createMission({ ...input }, k),28 async requestCancel(missionId) {29 await requestCancel(missionId, k); // a mission id or a run id; cascades30 },31 async resolveWait(waitId, resolution, actor) {32 // Throws WaitNotFoundError for an unknown id; resolved:false when the wait is no longer pending.33 const out = await resolveWait(waitId, resolution, { name: actor, via: 'console' }, k);34 return { resolved: out.resolved };35 },36 async postOwnerMessage(runId, text) {37 await postOwnerMessage(runId, text, k); // throws when the run is terminal38 },39 async listenEvents(cb: (event: AosEvent) => void) {40 // Only events created after the subscription; persisted ones exactly once in id order, transient ones with id -1.41 const sub = listenEvents(cb, { kernel: k });42 await sub.ready;43 return () => sub.close();44 },45 async openArtifact(sha256) {46 const out = await openArtifact(sha256, k);47 return out ? out.stream() : null;48 },49 };50 return { port, close: () => k.close() };51}52