UNPKG

@sema-agent/core

Version:

Stateless, task-oriented AI agent core

73 lines (72 loc) • 5.35 kB
import type { SessionTreeEntry } from "../../internal/harness-types.js"; import { DATE_ANCHOR_CUSTOM_TYPE } from "../leg-close-records.js"; import type { DateAnchorRead, MemoryTailSnapshot, PrefixHeadDigests, Prepared } from "./contracts.js"; /** The conversation's epoch ANCHOR (engine-owned; never model-visible): what the head of the session's requests was * frozen to for the current segment — the date the system prefix renders, the head's own digests, and the name of the * memory store content the memory band was composed from (never the content: that is kept in process memory only). Its entry * is a leg-close record (`leg-close-records.ts`, where the word is declared). */ export { DATE_ANCHOR_CUSTOM_TYPE }; /** The most index text (all planes together, in characters) a segment's memory band freezes; a larger index is served * live on every leg. */ export declare const MEMORY_TAIL_MAX_CHARS: number; /** The id of the active branch's latest compaction entry — the segment a fact is recorded for — or `null` before any. */ export declare function conversationSegmentOf(branch: ReadonlyArray<SessionTreeEntry>): string | null; /** A seat that tells the model something once per segment: the frame its leg's first request still owes, and the segment that * was judged in (`undefined` = the branch could not be read, and nothing is recorded). */ export interface ToldSeat<V> { owed?: { readonly body: string; readonly value: V; }; segment: string | null | undefined; } /** * The compaction arm every told seat shares. The text the compaction summarized away is appended as one engine-minted message * right after it lands (the posture's rule, halt included: no engine message enters the transcript ahead of a person's next * words), unless the leg's own pending frame IS that text (`carried`): it rides the next message into the new segment. Then the * record moves to the new segment and is written at once — an end-of-task compaction lands after the leg's own snapshot write. * A failed append is reported and records nothing (the next leg tells it again); an unreadable branch records nothing. Never throws. */ export declare function restateToldSeat<V>(prepared: Pick<Prepared, "reminderMark" | "session" | "admitRows" | "batchHaltRef" | "postureFrameRef" | "announcedListingsRef" | "frameReceipts" | "announcedSnapshotRecovered">, seat: ToldSeat<V>, told: { body: string; value: V; what: string; carried: boolean; record: (value: V, segment: string | null) => void; }): Promise<void>; /** * Read the session's epoch anchor off the active branch: the nearest anchor entry NEWER than the nearest compaction * that carries each half (the date pair, the head, the memory content's name), honored only when it was written for that * same segment. An anchor written by a leg whose prefix predates a compaction that landed in between therefore reads as * absent — the one rule that makes a compaction re-anchor the epoch: the first leg of the new segment renders today's * date and the memory store as it stands, and anchors them. An unreadable branch returns undefined (the leg renders * today and the live store, compares nothing, writes nothing). */ export declare function readDateAnchor(session: { getBranch(): Promise<ReadonlyArray<SessionTreeEntry>>; }): Promise<DateAnchorRead | undefined>; /** The head's per-block digests the anchor keeps — one short unsalted sha256 per prompt block, keyed by block id (a * repeated id takes its position as a suffix, so two blocks never share a key). */ export declare function prefixHeadBlocksOf(blocks: ReadonlyArray<{ id: string; text: string; }>): Readonly<Record<string, string>>; /** The head segments that differ between the anchored head and this leg's (block ids, then `tools`), sorted; empty ⇒ same * head. A block counts as moved when its digest differs or its position does (the record keeps the blocks' order). */ export declare function headSegmentsChanged(anchored: PrefixHeadDigests, now: PrefixHeadDigests): string[]; /** * The anchor write a leg owes when it settles, or undefined. Owed when the leg rendered a date and prepare found no * anchored date for the segment or the model has since been told a newer one, when the leg's head is not the one * the anchor recorded (the leg opened an epoch: its own head becomes the anchor), or when the leg read the memory store * live and what it read is not what the segment's anchor names (`memory`: the name of what it read); never after an * unreadable branch. The date and head halves are small and ride every write (a half this leg did not produce is * carried over from the anchor it read); the memory half is written only when owed — the halves are read one by one, so * an entry without it never hides an earlier one. Best-effort — a lost write costs one re-rendered prefix, one repeated * reminder, or one un-attributed leg start next leg, never a wrong date. */ export declare function owedDateAnchor(session: { appendCustomEntry(customType: string, data?: unknown): Promise<string>; }, read: DateAnchorRead | undefined, date: { legDate: string; announcedDate: string; } | undefined, head: PrefixHeadDigests | undefined, memory: MemoryTailSnapshot | undefined): (() => Promise<void>) | undefined;