@sema-agent/core
Version:
Stateless, task-oriented AI agent core
108 lines (107 loc) • 5.8 kB
TypeScript
import { type MemorySyncClientConflict, type MemorySyncClientResult } from "./sync-client.js";
import type { PatchReport } from "./types.js";
/** What a layer's calls landed, both halves, in call order (`serverApplied` / `localApplied` verbatim). A replayed
* call lands nothing and is still synced: whether a layer arrived is its `outcome`, never these counts. */
type LayerApplied = {
readonly server: PatchReport["applied"];
readonly local: PatchReport["applied"];
};
/** One layer of a memory sync — one scope, one outcome, and what all its calls landed. */
export type MemorySyncLayerReport = {
readonly scope: string;
readonly applied: LayerApplied;
} & ({
readonly outcome: "synced";
} | {
readonly outcome: "incomplete";
readonly why: "conflicts" | "truncated";
/** The last call's conflicts (server-reported and local), never hidden. */
readonly conflicts: readonly MemorySyncClientConflict[];
/** The answers the client refused to take from the server, as the call disclosed them. */
readonly warnings?: readonly string[];
} | {
readonly outcome: "refused";
readonly code: "config.memory_sync_cursor_mismatch" | "config.memory_sync_store";
/** Present on a cursor refusal: the axis it was refused on, whether the cursor carried no binding there
* (`absent`) or another one (`other`), and this call's value on it (`want`: the scope, the transport's peer,
* the local store's identity, or the incarnation of the peer state that answered — as the sentence shows it: one
* that holds a credential is the credentials mark alone). */
readonly detail?: {
readonly axis: "scope" | "peer" | "store" | "incarnation";
readonly found: "absent" | "other";
readonly want: string;
};
/** The two ways out the sentence names — both the host's to take, the engine takes neither. */
readonly outs: readonly [string, string];
readonly message: string;
} | {
readonly outcome: "failed";
readonly error: string;
});
/** A memory sync as a host reports it: the destination it was run for, the source revision it was run against, the
* source session's read set, and either its layers or its own refusal (no layer run, no request sent). */
export type MemorySyncRoundReport = {
/** The engine declaration the session goes to, as the host named it. */
readonly target: string;
/** The source session's revision the sync was run against, as the host named it. */
readonly sourceRevision: string;
/** The source session's read set — every scope key it reads, in its order. A scope with no layer was not synced. */
readonly scopes: readonly string[];
} & ({
readonly layers: readonly MemorySyncLayerReport[];
} | {
readonly refused: {
/** `config.memory_sync_target_mismatch`: the machine's sync reaches another peer than the engine the session
* goes to. `session_active`: the source session has a run in flight. */
readonly code: "config.memory_sync_target_mismatch" | "session_active";
readonly message: string;
};
});
/** One call of a layer as the host ran it: the scope, and how `syncMemoryScope` settled — its result or what it threw. */
type LayerCall = {
readonly scope: string;
} & ({
readonly result: MemorySyncClientResult;
} | {
readonly thrown: unknown;
});
/** A sync refused before any layer ran: the target mismatch carries the peer the machine's sync reaches (the sentence is
* minted here); a session in flight carries the host's own sentence for its code. */
type RoundRefusal = {
readonly code: "config.memory_sync_target_mismatch";
readonly reaches: string;
} | {
readonly code: "session_active";
readonly message: string;
};
type GapWhy = "local_only" | "refused" | "failed" | "incomplete" | "not_replicated" | "not_read_at_destination";
/**
* THE report of a memory sync — the one reading of how its `syncMemoryScope` calls settled, for every host that runs or
* relays one. Give it the destination (`target`), the source revision, the source session's read set (`scopes`), and
* either each call of each layer in the order they ran (`layers`; a layer continued after a cut appears once per call)
* or the sync's own refusal (`refused`; no layer ran). A layer outside the read set, a call carrying both or neither of
* `result` / `thrown`, or a name that is not a non-empty string is refused with a `TypeError`.
*/
export declare function memorySyncReportOf(round: {
readonly target: string;
readonly sourceRevision: string;
readonly scopes: readonly string[];
} & ({
readonly layers: readonly LayerCall[];
} | {
readonly refused: RoundRefusal;
})): MemorySyncRoundReport;
/**
* What of a session's memory does not travel with it: every scope of the report's read set that did not arrive whole,
* once, with why — `local_only` (the scope never replicates, {@link isReplicableScope}: whatever the sync did, its layer
* stays in the store it is written in), `refused` (the sync, or its layer's last call, was refused and wrote nothing on either side), `failed`, `incomplete`,
* `not_replicated` (no layer was run for it), or `not_read_at_destination` (its layer synced, and the destination's
* read set, when given, does not hold it). A scope absent from the result travelled. Keys are compared as written. A
* report that is not one this engine's `memorySyncReportOf` could have minted (a layer with no scope, outside the read
* set, listed twice, or with an outcome this engine does not read) is refused with a `TypeError`.
*/
export declare function memoryTravelGapOf(report: MemorySyncRoundReport, readAtDestination?: readonly string[]): Array<{
readonly scope: string;
readonly why: GapWhy;
}>;
export {};