import type { MatbotMachine, MatbotServices, MatbotPlugin, Store, Session, FileStore, StorageBackend, Vault, Notifier, KnowledgeIndex, PermissionGate, PluginSettings, ProviderConfig, ProviderAdapter, SessionRunner, SessionAppender, Tool, Usage, } from '@matatbread/matbot-plugin-api'; import { forwardingProxy, makeSwappable, createMountTable, scheduleAtEdge, createNotifier, recordUsage, singleTurnRequest, HookRegistry, } from '@matatbread/matbot-plugin-api/host'; import type { SwapFn } from '@matatbread/matbot-plugin-api/host'; import { notifyingStore, createMessage, describeProviders } from '@matatbread/matbot-plugin-api'; import { mediumGuard } from './storage-base/medium-guard.js'; import { MemoryStore } from './storage-base/memory-store.js'; import { unifyServices } from './plugin.js'; import { instantiateProvider, recordServiceKey, getPluginNameForSpecifier, getRegisteredPlugins, unloadPlugin } from './registry.js'; import { installSettingsDefaults, installSettingsNotifier, makePluginSettings, settingsDefaultNamespaces, storedSettingsData } from './settings.js'; import type { SettingsDoc } from './settings.js'; import { ToolRegistryImpl } from './tool-registry.js'; import type { ProviderRegistryImpl } from './provider-registry.js'; import { SystemContextRegistryImpl } from './system-context.js'; import { LookupKnowledgeIndex } from './knowledge/index.js'; import { createSessionRunner } from './session-runner.js'; import { createSessionAppender } from './session-append.js'; import { createSingleTurnTool } from './single-turn.js'; import { createAboutMatbotTool } from './about.js'; import { addUsage } from './usage.js'; import { defaultGate } from './default-gate/index.js'; /** A storage backend a configured plugin's manifest supplied at boot, and the config entry it came from. */ export interface PreScannedStorage { backend: StorageBackend; spec: string; } /** * Open the first configured plugin's manifest `storageBackend`, before any store exists. Import failures * are left for the loader to report. Node caches the imported modules, so the later load is free. */ export async function preScanStorage( specs: readonly { spec: string; importSpec: string }[], dir: string, ): Promise { for (const { spec, importSpec } of specs) { try { const mod = await import(/* @vite-ignore */ importSpec) as Record; const plug = (mod['plugin'] ?? (mod['default'] as Record | undefined)?.['plugin']) as MatbotPlugin | undefined; if (plug?.storageBackend !== undefined) return { backend: await plug.storageBackend.open(dir), spec }; } catch { /* the loader surfaces it */ } } return undefined; } /** A permission policy, and the tools that inspect it. Core's own is `defaultGate`. */ export interface HostGate { namespace: string; /** `stored` reads the namespace's stored document alone, with no configured default under it — for a * policy migrating a stored format, which must never copy a default into what is stored. */ create(settings: PluginSettings, stored: () => Promise>): PermissionGate; tools(settings: PluginSettings): Tool[]; } /** The machine members only a host can supply. */ export type HostMembers = Pick; export interface AssembleOptions { /** The host's own storage: the revert target when a plugin's StorageBackend is unregistered. */ bootBackend: StorageBackend; preScanned?: PreScannedStorage | undefined; /** The boot vault, behind a swappable proxy like every other swap-member. */ vault: Vault; providers: ProviderRegistryImpl; /** The installation's permission policy; `defaultGate` (ask, remember, honour) when absent. */ gate?: HostGate; defaultSettings?: ReadonlyMap>> | undefined; builtinTools?: Tool[]; /** Resolve one `${NAME}` reference; the browser prompts for a missing secret. Defaults to the vault. */ resolveSecret?(ref: string, vault: Vault): Promise; /** The application's own version, which `about_matbot` reports as the harness version (the CLI passes * its package's, which the lockstep release makes core's too). */ version: string; /** Called with a late-bound handle, since these members call back into the machine being built. */ host(machine: () => MatbotMachine): HostMembers; } export interface AssembledMachine { /** With the shared session store and runner, which a plugin's machine types as optional, present. */ services: MatbotMachine & { readonly sessions: Store; readonly run: SessionRunner }; /** A session runner over any store — the shared one is `services.run`. */ makeRunner(store: Store, opts?: { appender?: SessionAppender }): SessionRunner; /** Call once the configured plugins have loaded. */ loaded(): void; /** Unload every plugin, last loaded first, and let this process assemble another machine. */ dispose(): Promise; } // Whether a machine is assembled in this process now. See assembleMachine. let assembled = false; const NEVER_ABORT = new AbortController().signal; type AnyStore = Store<{ id: string; version: string }>; /** * Stand a machine up: the swappable services and their boot defaults, the stores, the registries, the * deferred storage swap and mount table, the session runner and the core tools. The one expression of * the boot graph, so a step added here reaches every host. What differs by platform — carriers, where * storage and secrets live, how a plugin is acquired — is the host's to pass in. * * The host installs its principal and usage carriers first: nothing here reads an identity, but the * runner and every plugin will. * * **One machine per process at a time.** Core's plugin registry and the installation's settings defaults * are the module's, and a plugin is a module instance whatever machine loads it, holding what it captured * from the one that set it up — so a second machine would share the first's plugins and replace its * defaults, half-working with nothing to say so. It throws instead. Several principals are one machine * (`runAs`); a machine configured differently is a process of its own, as background-jobs-node runs a * job. `dispose()` frees the slot, for a host (a test, in practice) done with the first. */ export function assembleMachine(opts: AssembleOptions): AssembledMachine { if (assembled) { throw new Error('A matbot machine is already assembled in this process, and plugins and core\'s registry are shared by ' + 'every machine in it. dispose() that one first, or run another machine in a process of its own.'); } assembled = true; const { bootBackend, preScanned, providers } = opts; installSettingsDefaults(opts.defaultSettings); // Swap-members sit behind capture-safe proxies, so a captured reference — including a destructure // like `const { KnowledgeIndex, StorageBackend } = services` — follows a register()-driven swap. let activeVault = opts.vault; const vault = forwardingProxy(() => activeVault); let activeNotifier: Notifier = createNotifier('core'); const notifier = forwardingProxy(() => activeNotifier); // Settings writes announce on the same bus. The proxy, not the impl, so a registered distributed // Notifier relays them too. installSettingsNotifier(notifier); let knowledgeImpl: KnowledgeIndex = new LookupKnowledgeIndex(); const knowledge = forwardingProxy(() => knowledgeImpl); // ── Storage ─────────────────────────────────────────────────────────────────────────────────────── let activeBackend: StorageBackend = preScanned?.backend ?? bootBackend; // Held only to say so, once, when a plugin displaces it — see swapStorage. let preScanBackend = preScanned?.backend; const backend = forwardingProxy(() => activeBackend); const storeProxies = new Map]>(); // Bumped by every swap, and read by the medium guard. A document handed out under one generation // cannot be written back under another: nothing is migrated between backends, so that write would // move it, silently, into a backend that never issued it. let storageGeneration = 0; const guarded = new Map(); const createStore = (namespace: string): Store => { let entry = storeProxies.get(namespace); if (entry === undefined) { entry = makeSwappable(activeBackend.createStore(namespace)); storeProxies.set(namespace, entry); } // Guard OUTSIDE the swap proxy: one stable object per namespace across every swap, so a captured // store reference keeps working and the guard sees each call whichever backend is current. let store = guarded.get(namespace); if (store === undefined) { store = mediumGuard<{ id: string; version: string }>(entry[0], () => storageGeneration, namespace); guarded.set(namespace, store); } return store as Store; }; const [fileStore, swapFiles] = makeSwappable(activeBackend.fileStore); // Every session write announces itself: the namespace has many writers (the turn pump, session_action, // session_edit, a session minted over HTTP), and one wrapper covers them all, including the next. const sessions = notifyingStore(createStore('sessions'), notifier, 'sessions', 'session'); // Re-point every store proxy and the file proxy at `next`. Synchronous, so readers see `next` at once; // the displaced backend is closed in the background, because a slow or throwing close() (node:sqlite's // rejecting on a still-open statement) must never gate the swap or suppress its mount notification. // Driven only from the quiescent edge, never mid-turn. const swapStorage = (next: StorageBackend): boolean => { const removed = activeBackend; if (removed === next) return false; // Only ONE backend is ever active, and a plugin registering one displaces whatever the boot pre-scan // opened — silently, since the swap is supported and the loser leaves nothing but an orphaned file. // Said once, naming the plugin, at the moment it stops being true. if (removed === preScanBackend && next !== bootBackend) { preScanBackend = undefined; console.warn( `[matbot] storage: the backend opened at startup by "${preScanned?.spec}" has been replaced by another ` + `plugin's storage backend. Only one is ever active and nothing is migrated between them, so anything ` + `it already wrote is no longer read. Configure just one storage plugin to remove this ambiguity.`, ); } activeBackend = next; storageGeneration++; for (const [ns, [, swap]] of storeProxies) swap(next.createStore(ns)); swapFiles(next.fileStore); void Promise.resolve(removed.close?.()).catch(e => console.error('[matbot] closing displaced StorageBackend:', e)); return true; }; // ── Registries ──────────────────────────────────────────────────────────────────────────────────── // The host's file area doubles as the media store, so attachments work with no plugin. Seeded into the // registry rather than spelled on the base object, because `unifyServices` resolves an own property // first — a member spelled there is one `register()` could never reach. // // The session appender is seeded the same way, in every process: it refuses in a background job, which // registers its own (forwarding to its parent) over it — and unloading that reverts to refusing, which // is the honest answer for a job with no channel. Late-bound, as `services` is built below. It writes // through the shared runner, the one writer of the sessions in `services.sessions`. // Not a host-supplied set: an embedder with a service of its own registers it on the returned // `services` before `loaded()`, which is the same moment and one mechanism rather than two. A `seed` // option existed for the node `FunctionRunner`, and went with it when that became a plugin. const seed: Partial = { MediaStore: fileStore, SessionAppender: createSessionAppender({ sessions: () => services.sessions, run: () => services.run, notifier: () => services.Notifier, isSubAgent: () => services.isSubAgent(), providers: () => services.providers, prompter: () => services.SessionPrompter, }), }; const serviceRegistry = new Map(Object.entries(seed)); // Validation wraps the executor, so every door — the runner, `POST /tools/:name`, `invokeTool` — is // covered by one wrapper. It asks the `validate` hooks per call: validators arrive long after the // builtins are seeded, and may be unloaded again. const hooks = new HookRegistry(); const tools = new ToolRegistryImpl(opts.builtinTools, notifier, (tool, parameters, signal) => hooks.runValidate({ tool, parameters, signal })); const systemContext = new SystemContextRegistryImpl(); // The installation's permission policy. Deliberately NOT behind a forwardingProxy: the documented way // to replace a policy is to capture the one displaced and delegate to it, and a capture-safe proxy // would make that capture resolve to the capturing gate itself — which then calls itself until the // stack overflows. A getter keeps the member read late-bound; every consumer resolves it per call. const gate = opts.gate ?? defaultGate; const gateStore = createStore('settings'); const gateSettings = makePluginSettings(gateStore, gate.namespace); let activeGate = gate.create(gateSettings, storedSettingsData(gateStore, gate.namespace)); const boot = { vault: activeVault, knowledge: knowledgeImpl, notifier: activeNotifier, gate: activeGate }; // ── The quiescent edge ──────────────────────────────────────────────────────────────────────────── // A StorageBackend swap waits for it, since swapping the system of record under a running turn would // split a compare-and-swap across two backends. The mount table batches mount notifications to it: // register/unregister mark a key dirty, and the edge multicasts each key's net transition, so a // reload within one turn collapses to a single remount. const mountTable = createMountTable(() => services); // A last-write-wins slot read at fire time, so three registers before an edge install one backend. One // callback also orders the swap ahead of the mount flush, so the remount it marks lands in that edge. let pendingSwap: StorageBackend | undefined; const scheduleEdge = scheduleAtEdge(() => { if (pendingSwap !== undefined) { const next = pendingSwap; pendingSwap = undefined; if (swapStorage(next)) mountTable.markDirty('StorageBackend'); } mountTable.flush(); }); const stageSwap = (next: StorageBackend): void => { pendingSwap = next; scheduleEdge(); }; const swapKnowledge = (next: KnowledgeIndex): void => { const prev = knowledgeImpl; if (prev === next) return; knowledgeImpl = next; if (prev.entries !== undefined) for (const e of prev.entries()) void next.index(e); }; const resolveSecret = (ref: string): Promise => opts.resolveSecret?.(ref, vault) ?? vault.resolve(ref); const resolveConfig = async (cfg: ProviderConfig): Promise => { let credentials: Record | undefined; if (cfg.credentials !== undefined) { credentials = {}; for (const [k, v] of Object.entries(cfg.credentials)) credentials[k] = await resolveSecret(v); } return { ...cfg, ...(credentials !== undefined ? { credentials } : {}), ...(cfg.endpoint !== undefined ? { endpoint: await resolveSecret(cfg.endpoint) } : {}), }; }; let runner: SessionRunner | undefined; const baseServices: MatbotMachine = { // Plugins always receive the plugin-scoped override built in setupPlugin; the base is never called. settings(): PluginSettings { throw new Error('settings() is only available within a plugin scope (use the services passed to setup()).'); }, createStore, get(key) { return serviceRegistry.get(key as string) as never; }, // StorageBackend is staged for the quiescent edge, which marks its mount dirty once the swap lands; // the other swap-keys repoint at once and mark dirty for the edge to multicast. async register(key, value) { if (key === 'StorageBackend') stageSwap(value as StorageBackend); else if (key === 'KnowledgeIndex') swapKnowledge(value as KnowledgeIndex); else if (key === 'Vault') activeVault = value as Vault; else if (key === 'Notifier') activeNotifier = value as Notifier; else if (key === 'PermissionGate') activeGate = value as PermissionGate; else serviceRegistry.set(key as string, value); if (key !== 'StorageBackend') { mountTable.markDirty(key); scheduleEdge(); } }, // Symmetric with register: a swap-key or seeded key reverts to its boot value rather than dangling on // the unloaded plugin's impl — unloading a plugin that put media on S3 leaves attachments working on // disk, not off until a restart. Anything else is deleted. unregister(key: string) { if (key === 'StorageBackend') stageSwap(bootBackend); else if (key === 'KnowledgeIndex') knowledgeImpl = boot.knowledge; else if (key === 'Vault') activeVault = boot.vault; else if (key === 'Notifier') activeNotifier = boot.notifier; else if (key === 'PermissionGate') activeGate = boot.gate; else if (key in seed) serviceRegistry.set(key, seed[key as keyof MatbotServices]); else serviceRegistry.delete(key); if (key !== 'StorageBackend') { mountTable.markDirty(key as keyof MatbotServices); scheduleEdge(); } }, registerFrontend() { /* bound per plugin in setupPlugin's scope */ }, async complete(req) { // An alias is resolved here, where the call is made, and nowhere above. const raw = providers.resolve(req.provider); if (raw === undefined) throw new Error(`complete(): unknown provider "${req.provider}". Available: ${describeProviders(providers)}`); const resolved = await resolveConfig({ ...raw, // Per-call overrides shallow-merged over the config's own parameters (request wins). ...(req.parameters !== undefined ? { parameters: { ...raw.parameters, ...req.parameters } } : {}), }); const adapter = await instantiateProvider(services, resolved); if (adapter === null) throw new Error(`complete(): provider "${req.provider}" has no loadable adapter (module "${resolved.module}").`); const messages = req.system !== undefined ? [createMessage({ role: 'system', content: [{ type: 'text', text: req.system }], traceId: crypto.randomUUID() }), ...req.messages] : req.messages; let text = ''; // Folded as the runner folds a turn's usage, because an adapter may report one call in several // parts: anthropic sends input + cache counts on `message_start` and output on `message_delta`. let usage: Usage = { inputTokens: 0, outputTokens: 0 }; const startedAt = Date.now(); for await (const ev of adapter.complete(messages, resolved, [], req.signal ?? NEVER_ABORT)) { if (ev.type === 'text-delta') text += ev.delta; if (ev.type === 'usage') usage = addUsage(usage, ev); } // Into the ambient usage sink, so a tool running this completion has its spend attributed to the // site in force. Measured here because this is where the call is issued. Under the profile that ran, // never an alias: the entry is priced by the model's rates, and an alias moves. recordUsage(raw.name, usage, { startedAt: new Date(startedAt).toISOString(), durationMs: Date.now() - startedAt }); return { text, usage }; }, async singleTurn(req) { return this.complete(singleTurnRequest(req)); }, ...opts.host(() => services), providers, mounted: mountTable.mounted, sessions, files: fileStore, get run() { return runner; }, ephemeral(opts) { const store = new MemoryStore(); return { sessions: store, run: makeRunner(store, opts) }; }, hooks, tools, systemContext, StorageBackend: backend, Vault: vault, Notifier: notifier, get PermissionGate() { return activeGate; }, get KnowledgeIndex() { return knowledge; }, }; const services: MatbotMachine = unifyServices(baseServices); // Resolved per turn, so it sees live `provider` edits, and an alias here, where the turn calls it: the // runner keeps the name it was given, which is what a message records. instantiateProvider force-loads // an adapter that is not registered yet, and yields null rather than throwing when it cannot. const resolveProvider = async (name: string): Promise<{ adapter: ProviderAdapter; config: ProviderConfig } | null> => { const cfg = providers.resolve(name); if (cfg === undefined) return null; const config = await resolveConfig(cfg); const adapter = await instantiateProvider(services, config); return adapter === null ? null : { adapter, config }; }; // Services a plugin registers after boot are resolved live, per turn. const makeRunner = (store: Store, extra?: { appender?: SessionAppender }): SessionRunner => createSessionRunner({ store, ...(extra?.appender !== undefined ? { appender: extra.appender } : {}), resolveProvider, tools, hooks, systemContext, vault, files: fileStore, toolTypeIndex: () => services.ToolTypeIndex, toolPresenter: () => services.ToolPresenter, steeringPolicy: () => services.SteeringPolicy, mediaStore: () => services.MediaStore, permissionGate: () => services.PermissionGate, ...(services.workdir !== undefined ? { workdir: services.workdir } : {}), loadPlugin: services.loadPlugin.bind(services), unloadPlugin: services.unloadPlugin.bind(services), }); runner = makeRunner(sessions); // Seeded rather than carried by configured plugins: `gate_action` because a minimal install's first act // (adding a plugin or provider) is gated, so inspecting an answer cannot depend on a config line. for (const tool of gate.tools(gateSettings)) tools.register(tool); tools.register(createSingleTurnTool(services)); // The harness is not a plugin (no `plugin list` row), so its version gets a tool of its own. tools.register(createAboutMatbotTool(opts.version, services)); const loaded = (): void => { // A defaults key is a plugin NAME — loader-derived, not necessarily what `plugins:` spells. One // naming nothing loaded is the feature's one silent failure. The gate's namespace is seeded at boot // rather than by a plugin, and reserved dunder namespaces are not plugins; both are exempt. const names = new Set(getRegisteredPlugins().map(p => p.name)); for (const ns of settingsDefaultNamespaces()) { if (names.has(ns) || ns === gate.namespace || (ns.startsWith('__') && ns.endsWith('__'))) continue; console.warn( `[matbot] default settings name "${ns}", which is not a loaded plugin — they apply only once a plugin ` + `of that name loads. Key them by the plugin's package name (\`plugin list\` reports them).`, ); } // The pre-scan opened a manifest backend before the loader knew the plugin's name, bypassing the // scoped register() that attributes a service key. Attributing it now makes unloading that plugin // revert storage to the host base, exactly as for a runtime register(). const name = preScanned !== undefined ? getPluginNameForSpecifier(preScanned.spec) : undefined; if (name !== undefined) recordServiceKey(name, 'StorageBackend'); }; const dispose = async (): Promise => { for (const plugin of [...getRegisteredPlugins()].reverse()) await unloadPlugin(plugin.name, services); assembled = false; }; return { services: services as AssembledMachine['services'], makeRunner, loaded, dispose }; }