UNPKG

@sema-agent/core

Version:

Stateless, task-oriented AI agent core

75 lines (74 loc) • 5.23 kB
/** One stored row as the store's owner hands it to a move. Only these members are read; the rest of the row is the owner's and comes back untouched. */ export interface SchedulerStoredRow { /** The key the row is filed under now. */ readonly scope: unknown; /** The row's stored `ScheduledIntent.lifetime` (absent when the store kept none). */ readonly lifetime?: unknown; /** The row's stored `ScheduledIntent.mode` (absent when the store kept none). */ readonly mode?: unknown; /** The row's stored label. */ readonly label?: unknown; /** The session the row's firings go to (the `SchedulerContext.sessionId` it was scheduled with): the owner of a session-lane row's key. */ readonly sessionId?: unknown; /** The owner's own word on the row's lane (`"session"` / `"durable"`), for a row whose stored members do not name it. Read by the forward move only. */ readonly lane?: unknown; } /** Why a move hands a row back unmoved — the closed set of {@link SchedulerStoreMove.refused}'s `code`. */ export type SchedulerRowRefusalCode = /** Its key is not a non-empty string (forward) or not a lane key (rollback), or a `lifetime` / `mode` / `lane` is outside the seam's words. */ "row_invalid" /** Neither its stored members nor a stated `lane` name its lane: no `lifetime: "session"`, no `mode`, and the wakeup's label — a session * wakeup on a store that kept no `mode`, or a job that was given that label. */ | "lane_unknown" /** The stated `lane` is not the lane its stored members name. */ | "lane_contradicted" /** A session-lane row whose firing destination (`sessionId`) is not a non-empty string: it has no owner to key. */ | "destination_unknown" /** Rollback: a session-lane row and a durable-lane row of one owner would share one bare key, where an older release lists and stops * them as one lane's rows. */ | "bare_key_shared" /** Forward: rows filed under different bare keys would share one lane key (two explicit session scopes over one session id — two * tenants' `A/42` and `B/42` — where the lane key is the session id alone): the move does not merge them. */ | "lane_key_shared"; /** What a move answers: the rows it placed, each with the key to file it under, in input order; and the rows it did not, each with why. */ export interface SchedulerStoreMove<R extends SchedulerStoredRow> { readonly moved: ReadonlyArray<{ readonly row: R; readonly scope: string; }>; readonly refused: ReadonlyArray<{ readonly row: R; readonly code: SchedulerRowRefusalCode; readonly reason: string; }>; } /** A whole store, as its owner hands it to a move: its rows and its own key declaration (the `scopeKeys` it declares today). */ export interface SchedulerStoreRows<R extends SchedulerStoredRow> { readonly scopeKeys?: unknown; readonly rows: readonly R[]; } /** * Pure forward placement: each row filed under bare keys (a release before 7.72.0: a key was its owner alone) gets one lane key or one * refusal — `session:<sessionId>` for a row of the session lane (keyed by the session it fires into: an older factory took an explicit * session scope, so the bare key need not be the session), `durable:<bare key>` for a row of the durable lane. A row's lane is read off its * stored `lifetime` and `mode` as scheduling picks it (`lifetime: "session"` or `mode: "session-wakeup"` ⇒ session); a row with no `mode`, * a `lifetime` absent or `"durable"` and the wakeup's label is refused `lane_unknown` unless the owner states its `lane`. A bare key is never * judged by its head. Rows of different bare keys that would land on one lane key are not merged: each is refused `lane_key_shared` (only * the rows of this call are compared). * * Pure and write-free: call it once to move a store (writing `moved` and `scopeKeys: "lane"` in one write once `refused` is empty), or on * every read in a backend that keeps bare keys stored and answers lane keys at its port (that backend's duties are written at * `SchedulerCapability.schedulerCapabilities.scopeKeys`). `store.scopeKeys` is the stored rows' form (absent or `"bare"`; `"lane"` is refused * whole, `config.invalid_argument`). */ export declare function migrateSchedulerStore<R extends SchedulerStoredRow>(store: SchedulerStoreRows<R>): SchedulerStoreMove<R>; /** * The rollback: every row of a store that declares lane keys with its bare key (the lane name and its `:` taken off), for an older release. * Two lanes' rows that would share one bare key — a session whose id is some isolation scope's name — are both refused `bare_key_shared`: * an older release reads them as one lane's rows, so one owner's stop or delete reaches the other's. Settle them (remove one, or keep * the newer release) before rolling back. A row whose key is not a lane key is refused `row_invalid`. * * `store.scopeKeys` must be `"lane"` (anything else is refused whole, `config.invalid_argument`). Write `moved` together with the * declaration `scopeKeys: "bare"`, and only when `refused` is empty. */ export declare function rollbackSchedulerStore<R extends SchedulerStoredRow>(store: SchedulerStoreRows<R>): SchedulerStoreMove<R>;