@sema-agent/core
Version:
Stateless, task-oriented AI agent core
140 lines (139 loc) • 9.6 kB
TypeScript
import type { ReadFileState } from "../../tools/fs/safety.js";
import type { SeenFileRegistry } from "../existing-target.js";
/**
* The Runner's per-session read-file-state seats (CC parity: readFileState is minted
* once per REPL session and mutated across turns; it has NO turn-boundary and NO abort clear site —
* its only clears are compaction/fork/subagent-cleanup/dispose). sema's map stays per-task (the §11
* ruling-A closure wiring), so the SESSION scope lives here instead: prepare adopts each task's live
* map under its sessionId, and the next same-session, same-Runner task INHERITS it (through the same
* containment judgment the transcript-replay seed applies — see `inheritSessionReadFileState`).
*
* This seat is a CACHE, never an authority:
* - a durable resume's checkpoint seed stays the sole authority on that leg (the seed was cut
* atomically with the leaf; this seat may be older OR newer than it);
* - a cross-process continuation misses here and falls back to the transcript replay (rung-2);
* - eviction (the LRU cap, a Runner restart) degrades to that same replay — never to wrong state.
*
* Bounded: an LRU over sessionIds (insertion-order refresh on read), because the SessionStore's own
* eviction is not observable from here — a dropped seat costs one transcript replay, nothing else.
* The stored value is the task's LIVE map (object identity), so in-turn mutations — tool write-backs,
* and above all the compaction clear — are the seat's truth by construction; a snapshot copy
* here would resurrect summarized-away reads on the next turn.
*
* BRANCH BINDING: a sessionId alone does not name a transcript — the same id
* can be rebranched (`resumeAt`), rewound out-of-band, or DELETED and re-created (`release()` + the
* default create-on-miss acquire), and the seat's map would then vouch for reads the CURRENT branch
* never saw — and another Runner's turn on the same session reads files this seat never heard of. Each seat therefore
* records `anchorLeafId` — a leaf its task produced — and inherits only while what landed after it is nothing the
* transcript replay reads (see {@link sessionSeatAnchorHolds}). The anchor is written twice: at ADOPT (the leaf at prepare;
* `null` when the branch was still empty) and SEALED at the task's settle to the task's own tail
* (`reanchor`). A `null` anchor is NEVER inheritable ("the branch is non-empty now" proves
* nothing about WHOSE messages those are — another Runner repopulating a released id, or a session
* import, makes a non-empty branch that never contained this seat's reads). An unprovable anchor
* falls back to the transcript replay, which reads the current branch by construction — cost, never
* wrongness.
*/
export declare class SessionReadFileStates {
private readonly seats;
/**
* The ONE handoff of "which files did a file-history restore of this session move" to the read state that
* must stop vouching for their old bytes — written by every restore (the run's restore seat and the
* out-of-run `Runner.rewindFiles`), read by the next leg's read face, which checks each path against the disk
* after every seeding rung and settles the batch only once its own map is adopted as the seat. A leg that
* throws before adopting, or skips the adoption, leaves the batch for the next one. Bounded like the seats
* (LRU over session ids): a dropped batch costs a refusal whose wording blames an outside edit, never an admission.
*/
private readonly restored;
private static readonly CAP;
/** Record the ABSOLUTE paths a restore of `sessionId`'s history wrote, deleted or failed to write. */
noteRestored(sessionId: string, absPaths: readonly string[]): void;
/** The paths still owed a read-state check on `sessionId` — a copy; nothing is cleared by reading. */
pendingRestored(sessionId: string): readonly string[];
/** Clear exactly the paths a leg checked and then adopted over; a path noted since stays owed. */
settleRestored(sessionId: string, checked: readonly string[]): void;
/** The session's seat row, or undefined (cross-process / evicted / first turn) ⇒ caller replays.
* The caller must verify the anchor against the session's CURRENT branch before using `state`. */
get(sessionId: string): {
state: ReadFileState;
anchorLeafId: string | null;
seen?: SeenFileRegistry;
} | undefined;
/** Adopt THIS task's live map as the session's seat (every hands leg: fresh, continuation, resume).
* `anchorLeafId` = the session's leaf as read at THIS prepare (null = the branch was empty; a
* null-anchored seat is not inheritable until the settle-time {@link reanchor} names a real leaf). `seen`: the task's
* registry of files seen without an entry, carried beside the map (by identity, as the map is). */
adopt(sessionId: string, state: ReadFileState, anchorLeafId: string | null, seen?: SeenFileRegistry): void;
/** The settle-time SEAL: re-anchor the seat to the finished task's own tail, so a
* first-task (null-anchor) seat becomes inheritable only through a leaf its task provably wrote.
* Identity-guarded: touches the row only while it still holds THIS task's map (a later prepare on
* the same session has already replaced the seat and owns its own anchor). */
reanchor(sessionId: string, state: ReadFileState, anchorLeafId: string): void;
}
/**
* Does the seat still describe the session as it stands? TRUE — the licence to inherit — only when walking parentIds from the
* session's CURRENT leaf reaches the seat's anchor across entries the transcript replay never reads: entries the context
* never shows (`entryRendersIntoContext` — the renderer's own table; an engine record such as a new segment's date anchor).
* Anything else answers FALSE and the leg replays the transcript, which reads the current branch by construction (a cost,
* never a wrong entry):
*
* - anchor `null`: never (r2). The seat was adopted on an empty branch and its task never sealed (crashed pre-settle, or the
* leaf read failed) — "the branch is non-empty now" is also true of a released-and-repopulated id or an imported log.
* - an entry after the anchor that the replay reads, whoever wrote it: another Runner's turn on the shared session (its
* reads are in the transcript and in no seat of this Runner — inheriting would answer for them as files never read), a
* compaction (its summary took reads out of the context the model is shown), a type the table does not know.
* - the walk misses the anchor (a rebranch or rewind moved the leaf off its lineage, a recreated or foreign log) or meets an
* entry it cannot read.
*/
export declare function sessionSeatAnchorHolds(getEntry: (id: string) => Promise<{
parentId: string | null;
type: string;
} | null | undefined>, anchorLeafId: string | null, currentLeafId: string | null): Promise<boolean>;
/** The session's leaf as a prepare reads it for the seat: `null` = the branch is empty, `undefined` = unreadable (no inherit,
* no adopt — the replay world, cost only). */
export declare function readSeatLeaf(session: {
getStorage(): {
getLeafId(): Promise<string | null>;
};
}): Promise<string | null | undefined>;
/** Whether the session's log, as its storage loaded it, is known to hold every record a branch read can show: the replay that loaded it
* dropped none (`SessionStorage.getReplayLoss`; a storage with no replay to lose from dropped nothing) and it woke no bounded window
* (`getFloorId`: what lies below a floor is not read). A storage that cannot say — no storage, a read that throws — is not known whole. */
export declare function sessionLogWhole(session: {
getStorage(): {
getReplayLoss?(): {
readonly dropped: number;
} | undefined;
getFloorId?(): string | null;
};
}): boolean;
/** Where a leg's read state is seeded from: a resume's checkpoint seed; none (a leg that continues no session); the session's
* live seat; the session's transcript. */
export type LegReadCarrier = {
readonly from: "checkpoint" | "none" | "transcript";
} | {
readonly from: "seat";
readonly state: ReadFileState;
readonly seen?: SeenFileRegistry;
};
/**
* Where a leg's read state is seeded from — ONE reading, for the hands phase that seeds it and for the reading before the
* environment starts that asks whether a compaction on the leg has reads to re-deliver. A resume leg's checkpoint seed is the
* authority (cut atomically with its leaf). Otherwise, on a session a leg already ran in: the session's live seat, where it is
* this Runner's, the leg is no `resumeAt` rebranch (the seat is the abandoned branch's tail) and its anchor holds
* ({@link sessionSeatAnchorHolds}: an entry the replay reads that landed since — another Runner's turn, a compaction — or a leaf
* off the anchor's lineage refuses it); else the transcript. `leaf` is read only where a seat stands.
*/
export declare function legReadCarrier(input: {
readonly resuming: boolean;
readonly spec: {
readonly sessionId?: string;
readonly resumeAt?: unknown;
};
readonly sessionId: string;
readonly seats: SessionReadFileStates | undefined;
readonly leaf: () => Promise<string | null | undefined>;
readonly getEntry: (id: string) => Promise<{
parentId: string | null;
type: string;
} | null | undefined>;
}): Promise<LegReadCarrier>;