UNPKG

@sema-agent/core

Version:

Stateless, task-oriented AI agent core

134 lines (133 loc) • 11.1 kB
import type { ParsedScopeKey } from "./scope-contract.js"; import type { HarvestReport, MemoryBackend } from "./types.js"; import type { MemoryInjection } from "./engine.js"; import { type MemoryPartitionIncidentSink } from "./layout.js"; import { type MemoryErasureAttestation } from "./file-backend.js"; /** The personal plane's shared mount under the config root (ALL projects, one home — the plane is * keyed by scope, not by repo: `user:alice` is the same disk from every checkout). */ export declare function derivePersonalMemoryDir(configRoot: string): string; export declare function derivePersonalControlDir(configRoot: string): string; export interface ScopePlanes { /** proj:/legacy scopes — the repo/path-keyed plane (order preserved). */ project: string[]; /** user:/userlocal:/userproj:/org: scopes — the home-mount plane (order preserved). `org` is * CENTRAL-authority (§2 表), so it routes with the personal keys — under a pinned in-repo File * backend an org layer projected into the repo would leak tenant-shared memory into commit * history. The mount is still the "personal" directory (one home plane; the plane is about * WHERE-NOT-to-project, not about ownership — org's authority disk is the central DB anyway, and a * `userlocal:` layer's is the store it is written in: it is never replicated). */ personal: string[]; /** Which plane owns the write face (null = read-only layering everywhere). */ writePlane: "project" | "personal" | null; } /** Classify a v2 session's scopes into planes. writeScope routes by its OWN key kind (it need not * be listed in scopes — normalizeMemorySpec contract). */ export declare function classifyScopePlanes(scopes: readonly string[], writeScope: string | null): ScopePlanes; /** The session's planes, or `undefined` when it is not under the v2 contract — then every key is * opaque and the one plane is the project plane (the deployment's backend as configured). */ export declare function scopePlanesOf(scopes: readonly string[], writeScope: string | null, scopeContract: "v2" | undefined): ScopePlanes | undefined; /** The backend that holds a plane's entries. The project plane is the deployment's backend; the * personal plane shares a backend without a directory (the scope column partitions it) and gets * its OWN File store on the personal mount when the deployment's backend is directory-pinned — * opened only while the deployment's own store is not expected and absent (that store's * `memory.store_absent` is thrown instead: no personal store is opened, or recorded, in its place). */ export declare function memoryPlaneStore(plane: "project" | "personal", backend: MemoryBackend, engineRoot: string, onIncident?: MemoryPartitionIncidentSink): MemoryBackend; /** One scope's store, as {@link resolveMemoryScopeStore} answers it. */ export interface MemoryScopeStore { /** The plane that carries the scope. */ readonly plane: "project" | "personal"; /** The store holding the scope's committed entries — the backend a session over this scope * materializes from and harvests into. */ readonly backend: MemoryBackend; } export interface MemoryScopeStoreInput { /** The scope key as a session's `TaskSpec.memory` spells it — read through the Runner's own * normalization (surrounding whitespace trimmed; under v2 the scope contract's parse). */ scope: string; /** The session's `TaskSpec.memory.scopeContract`. A required key (checked at run time too): * absence of the opt-in is written as `undefined`, never left out, because it moves a typed key * to the other plane. */ scopeContract: "v2" | undefined; /** The deployment's backend — the instance given as `RunnerDeps.memoryBackend`. */ backend: MemoryBackend; /** The value given as `RunnerDeps.memoryEngineDir`; a required key (`undefined` written out reads * the Runner's same fallback, a left-out key is refused — it would silently pick that fallback). */ memoryEngineDir: string | undefined; /** Sink for incidents of a store this call constructs (the personal File store). */ onIncident?: MemoryPartitionIncidentSink; } /** * The store that carries `scope` — the same plane classification and the same per-plane backend * the Runner's session assembly uses. The session assembly (prepare-memory) and a host's sync * executor read this one resolution; a change to the rule is made here only. A host leg that reads * or writes a scope's entries outside a session (sync, operator faces, adoption) takes the store * from here; the deployment's backend is the answer only for the project plane. Bad input is * refused with `config.memory_scope_key`: a blank or non-string key, a contract other than `"v2"` / * `undefined`, a left-out `scopeContract` or `memoryEngineDir` key, an engine dir that is not a * non-empty string, and under v2 a typed key the scope contract cannot read. * On a directory-pinned deployment a personal-plane answer is a File store opened over the * personal mount (opening creates the mount's directories and finishes an interrupted transaction, * as a session's opening does — unless the deployment's own store, or the personal store, is expected * and absent: `memory.store_absent`, nothing created); resolve once per scope and keep it. Opening is not a write: a store * only opened or read is not in a deployment's store set (`memory.deployment.store_set`). */ export declare function resolveMemoryScopeStore(input: MemoryScopeStoreInput): MemoryScopeStore; /** The two deployment facts a store resolution reads besides the backend, judged as {@link resolveMemoryScopeStore} * states (required keys; `undefined` written out states absence) — one judgment for every face that takes them. */ export declare function judgeMemoryStoreSeat(face: string, input: { scopeContract: "v2" | undefined; memoryEngineDir: string | undefined; }): void; /** The plane carrying `scope`: the one reading of which plane a scope key belongs to, shared by the store resolution and the * deployment face and exported for a host that needs the answer without a store — the Runner's own normalization * (surrounding whitespace trimmed; under v2 the scope contract's parse), then {@link scopePlanesOf}. No store is opened and * no disk is read. `face` is the caller's own name, written into every refusal (`<face>: …`); `scopeContract` is the * session's `TaskSpec.memory.scopeContract`, `undefined` written out for the opt-in absent (anything but `"v2"` and * `undefined` is refused). A key the contract cannot read, a blank or non-string one, and a bad contract are refused * (`config.memory_scope_key`). */ export declare function memoryScopePlaneOf(face: string, scope: unknown, scopeContract: "v2" | undefined): "project" | "personal"; /** The store-resolution input refusal (`config.memory_scope_key`), named by the face that refused. */ export declare function refuseMemoryStoreInput(face: string, what: string, v: unknown, cause?: unknown): never; /** True when the classified session needs the dual-root assembly at all. */ export declare function needsDualRoots(planes: ScopePlanes): boolean; /** True when the classified session reads or writes the personal plane (alone or beside the project plane). */ export declare function reachesPersonalPlane(planes: ScopePlanes): boolean; /** The File stores a caller is about to open under the engine root `engineRoot`, asked whether they are expected and absent before it * writes anything there: the deployment's own (a backend pinned to its directory) and, when `personal`, the personal plane's store beside * it — so a write under the root (a lock, a hint) never makes the root again in front of a refusal. A backend without a directory keeps * no File store here, and nothing is asked. */ export declare function refuseAbsentPlaneStores(backend: MemoryBackend, engineRoot: string, personal: boolean): void; /** Convenience for the placement gate: the parsed keys that stay on the PROJECT plane. */ export declare function parsedProjectPlane(planes: ScopePlanes, writeScope: string | null): ParsedScopeKey[]; /** * Merge the two planes' injections into ONE block. Plane order is fixed: PROJECT first (the stable * prefix — repo knowledge changes less often than personal notes), PERSONAL second; the instruction * (trusted half, "where to write") comes from whichever plane owns the write face and leads the * block, exactly like the single-root layout. v1 deliberately merges at BLOCK granularity — scope * interleaving across planes is not reproduced (documented; the within-plane order is untouched). */ export declare function mergeInjections(project: MemoryInjection | undefined, personal: MemoryInjection | undefined): MemoryInjection; /** Merge the two planes' harvest reports into the single report the runner surfaces. Counters add, * lists concatenate, `ok` ANDs, the first incident wins the seat (both are still visible via the * merged lists). 独立轨 F7: pass the WRITE plane's report as `a` — first-argument fields (incident * seat, degraded seat) prefer `a`, and the write plane's degradation is the one that matters. */ export declare function mergeHarvestReports(a: HarvestReport, b: HarvestReport): HarvestReport; /** * Merge two stores' receipts for ONE erasure request (same requestId, same selector) into the one receipt the * request answers — the harvest merge's law on the erasure report: counters add, lists concatenate, and * `status` is `"complete"` only when both receipts are (each store's own `erasureStatusOf` answer): a * store that could not establish its share keeps the request partial, whatever the other store erased. Pass * the receipts in store order (the project plane's first). Member by member: * - `at` is the later of the two (the receipt stands as of the last store's answer); `requestId`, `select`, * `selectHash` are the request's (both receipts carry the same); * - `resolvedIds` is the union; `erased`, `erasedPreviously`, `conflicts` concatenate; a `notFound` id is * dropped when the other store claimed it (erased / erasedPreviously — the id had a row there), appears once * otherwise, and carries `historyUnknown` when either store that resolved it could not judge its history; * - `evidenceCapability` is `"journal"` only when both are; `custodyState` is the worse of the two * (`damaged` ≻ `capability-absent` ≻ `complete`); * - each carrier class adds `removed` / `remaining` (and `unreadable` when both stores answer it), and is * `unverified` when either store's answer is (or is missing); `quarantineUnattributed` / `derivedEntries` add their * counts the same way; `replay` is * `"converges"` only when both are; `propagation` stays `"local-store-only"` (every store merged is local). */ export declare function mergeErasureAttestations(a: MemoryErasureAttestation, b: MemoryErasureAttestation): MemoryErasureAttestation;