/** * TrellisKernel — Generic Graph Kernel * * The composition root for the Trellis semantic kernel. Orchestrates the * EAV store, persistence backend, middleware chain, and snapshot lifecycle. * * The VCS engine (`TrellisVcsEngine`) sits on top of this generic kernel. * Non-VCS consumers can use TrellisKernel directly for pure graph CRUD. * * @module trellis/core */ import { EAVStore } from '../store/eav-store.js'; import type { Fact, FactMeta, Link, Atom } from '../store/eav-store.js'; import type { OpProvenance } from '../persist/canonical-op.js'; import type { KernelOp, KernelOpKind, KernelBackend } from '../persist/backend.js'; import type { KernelMiddleware, MiddlewareContext } from './middleware.js'; import { QueryEngine } from '../query/engine.js'; import type { Query } from '../query/types.js'; import type { QueryResult } from '../query/engine.js'; import type { SchemaDefinition, WorkspaceConfig } from '../ontology/types.js'; export interface KernelConfig { /** Persistence backend (SQLite or in-memory). */ backend: KernelBackend; /** Agent ID for attributing operations. */ agentId: string; /** Middleware chain applied to every mutation. */ middleware?: KernelMiddleware[]; /** Auto-snapshot after this many ops (0 = disabled). */ snapshotThreshold?: number; /** Auto-replay ops from backend on boot (default: true). */ autoReplay?: boolean; /** * Fallback provenance for ops minted without a per-call `ctx.provenance` * (ADR 0021 §2). Defaults to `{ actorType: 'machine', origin: 'sdk' }` — * the honest description of "something called the kernel API directly and * did not say who it was". * * Surfaces that know better (CLI, HTTP, MCP, sync, import) must pass * `ctx.provenance` per call rather than relying on this, since one kernel * can serve several surfaces at once. */ provenance?: OpProvenance; } export interface MutateResult { op: KernelOp; factsDelta: { added: number; deleted: number; }; linksDelta: { added: number; deleted: number; }; } export interface EntityRecord { id: string; type: string; facts: Fact[]; links: Link[]; } export declare class TrellisKernel { private store; private backend; private middleware; private agentId; private snapshotThreshold; private opsSinceSnapshot; private _booted; private ontologies; private workspaceConfig; private autoReplay; private defaultProvenance; constructor(config: KernelConfig); /** * Initialize the backend and replay persisted state. * Loads latest snapshot if available, then replays ops after it. */ boot(): { opsReplayed: number; fromSnapshot: boolean; }; /** * Close the backend connection. */ close(): void; isBooted(): boolean; /** * Apply a mutation to the graph. Creates an op, runs it through middleware, * decomposes into EAV primitives, persists, and returns the result. */ mutate(kind: KernelOpKind | string, payload: { facts?: Fact[]; links?: Link[]; deleteFacts?: Fact[]; deleteLinks?: Link[]; provenance?: OpProvenance; }, ctx?: Partial): Promise; /** * Create a snapshot of the current store state. */ checkpoint(): void; /** * Get the underlying EAV store for direct queries. */ getStore(): EAVStore; /** * Get the persistence backend. */ getBackend(): KernelBackend; /** * Get the agent ID. */ getAgentId(): string; /** * Read all persisted ops. */ readAllOps(): KernelOp[]; /** * Get the last persisted op. */ getLastOp(): KernelOp | undefined; /** * Create a QueryEngine bound to this kernel's store. */ createQueryEngine(): QueryEngine; /** * Execute an EQL-S query, routing through middleware handleQuery hooks. * If no middleware intercepts, the query runs directly against the store. */ query(q: Query): Promise; /** * Time-travel: reconstruct the store state at a specific op hash. * Returns a new EAVStore with state replayed up to (and including) that op. */ timeTravel(opHash: string): EAVStore; /** * Create a new entity with the given type and attributes. * Returns the entity ID. */ createEntity(entityId: string, type: string, attributes?: Record, links?: Array<{ attribute: string; targetEntityId: string; }>, ctx?: Partial): Promise; /** * Get an entity by ID, returning all its facts and links. */ getEntity(entityId: string): EntityRecord | null; /** * Update an entity's attributes. Deletes old values and adds new ones. */ updateEntity(entityId: string, updates: Record, ctx?: Partial): Promise; /** * Delete an entity and all its facts and links. */ deleteEntity(entityId: string, ctx?: Partial): Promise; /** * List entities by type, with optional attribute filters. */ listEntities(type?: string, filters?: Record): EntityRecord[]; /** * Add a link between two entities. */ addLink(sourceId: string, attribute: string, targetId: string, ctx?: Partial): Promise; /** * Remove a link between two entities. */ removeLink(sourceId: string, attribute: string, targetId: string, ctx?: Partial): Promise; /** * Add a fact to an entity. */ addFact(entityId: string, attribute: string, value: Atom, ctx?: Partial, meta?: FactMeta): Promise; /** * Remove a fact from an entity. */ removeFact(entityId: string, attribute: string, value: Atom, ctx?: Partial): Promise; /** * Get an ontology schema by ID. */ getOntology(id: string): SchemaDefinition | undefined; /** * List all ontologies. */ listOntologies(): SchemaDefinition[]; /** * Create a new ontology schema. */ createOntology(schema: SchemaDefinition): void; /** * Update an existing ontology schema. */ updateOntology(id: string, updates: Partial): void; /** * Delete an ontology schema. */ deleteOntology(id: string): void; /** * Boot the kernel with a workspace configuration. * Loads ontologies, projections, and seed data. */ bootWorkspace(config: WorkspaceConfig): void; /** * Export the current workspace configuration. */ exportWorkspace(): WorkspaceConfig; /** * Create a node (alias for createEntity with schema validation). */ createNode(id: string, data: Record, type: string, ctx?: Partial): Promise; /** * Update a node (alias for updateEntity with schema validation). */ updateNode(id: string, data: Record, type: string, ctx?: Partial): Promise; /** * Delete a node (alias for deleteEntity). */ deleteNode(id: string, ctx?: Partial): Promise; /** * Link two nodes (alias for addLink). */ link(e1: string, a: string, e2: string, ctx?: Partial): Promise; /** * Unlink two nodes (alias for removeLink). */ unlink(e1: string, a: string, e2: string, ctx?: Partial): Promise; addMiddleware(mw: KernelMiddleware): void; removeMiddleware(name: string): void; private _runMiddleware; private _replayOp; } //# sourceMappingURL=trellis-kernel.d.ts.map