UNPKG

@sema-agent/core

Version:

Stateless, task-oriented AI agent core

268 lines (267 loc) • 20.1 kB
/** * A leg's identity readings, as pure functions with no runtime imports — so a store's projection reads them exactly as * the prepare mint and the stations do (one predicate each, never a second spelling at a reader that cannot reach the * mods layer). */ import type { HookInvocationIdentity } from "./hooks.js"; import type { EngineNotice, NoticeTold } from "./engine-notice.js"; import type { NoticeDestinationJudge, SessionAddress } from "./task-notification.js"; import type { AskSourceIdentity } from "./tool-policy.js"; /** * The id a delegated leg's events carry as `agentId`: the leg's task id ({@link legTaskIdOf}) — for a background child, the * registry task id its spawner registered it under (the string its row, launch card, notifications, `TaskStop` and * `SendMessage` use); a foreground child has no registry identity (INTEGRATION-CORE §1.6a), so it is its session id. A * top-level leg carries none. It reads two facts of the leg's identity, so a face minted before the leg's hook envelope (the * ask source) asks it with the same two, and a parked row's summary asks it with the two the row records. */ export declare function agentIdOf(identity: Pick<HookInvocationIdentity, "taskId" | "isDelegatedChild"> | undefined): string | undefined; /** A leg's task id — the row a delegation lane runs it as (`internals.rowTaskId`), else its host's `spec.taskId`, else its * session id. Minted once at prepare (`Prepared.taskId`); a reader before prepare asks this same function (the resume screen; * the throw backstop, which has no session yet). The session id is the leg's transcript address. */ export declare function legTaskIdOf(spec: { readonly taskId?: string | undefined; }, internals: { readonly rowTaskId?: string | undefined; } | undefined, sessionId: string): string; export declare function legTaskIdOf(spec: { readonly taskId?: string | undefined; }, internals: { readonly rowTaskId?: string | undefined; } | undefined, sessionId: string | undefined): string | undefined; /** The delegation tree's root anchor as a leg reads it: the tree's recorded root and that row's occupant when the leg is a * descendant (`internals.rootSessionId` set), else the leg's own session and its own occupant — the leg IS the root. The one * reading the trusted tool context's `rootSessionId ?? sessionId` / `rootSessionIncarnation` pair, the workflow lane's root and * an Artifact call's `binding.root` all take. `occupant` absent = not read. */ export declare function rootAnchorOf(internals: { readonly rootSessionId?: string | undefined; readonly rootSessionIncarnation?: string | undefined; } | undefined, sessionId: string, ownOccupant: string | undefined): { sessionId: string; occupant?: string; }; /** The principal of the delegation tree's root as a leg hands it to its children (`RunInternals.rootPrincipal`), by the root anchor's * fixed point: the anchor's when the leg is a descendant (`internals.rootSessionId` set — absent there stays absent: not stated), else * the leg's own principal name (the leg IS the root; `undefined` for an anonymous one). */ export declare function rootPrincipalOf(internals: { readonly rootSessionId?: string | undefined; readonly rootPrincipal?: string | undefined; } | undefined, ownPrincipalName: string | undefined): string | undefined; /** * The session a leg reads and writes the person's session-scope permission rules at, and whether the leg is in its * delegation tree's rule session (`inTree`). One rule: a leg shares its tree root's session iff the root anchor carries the * root's principal (`internals.rootPrincipal`, which the anchor's producer states) and it is a name equal to the leg's own * principal name. A top-level leg (no `internals.rootSessionId`) is its own tree (`inTree: true`); every other leg — an anchor * that states no root principal, an anonymous root, another person, an anonymous leg — reads its own `sessionId`, `inTree: false`. * Unknown is never the same person. A resumed leg does not ask this: it reads the session its row records ({@link recordedRuleSessionOf}). */ export declare function ruleSessionOf(internals: { readonly rootSessionId?: string | undefined; readonly rootPrincipal?: string | undefined; } | undefined, sessionId: string, principalName: string | undefined): { readonly sessionId: string; readonly inTree: boolean; }; /** * The session a RESUMED leg reads and writes the person's session-scope rules at: the one its parked row records * (`legIdentity.ruleSessionId`), else — a row that records none: one parked before the member existed, or by a leg that read its * own — the leg's own `sessionId`, as every such row was parked. Never re-derived from the resuming call's anchor. `inTree`: that * session is the tree's root anchor ({@link rootAnchorOf} over the resumed internals). The parked card's `sessionScope` * (`CheckpointSummary`) is the same record. */ export declare function recordedRuleSessionOf(record: Pick<LegIdentityRecord, "ruleSessionId"> | undefined, internals: { readonly rootSessionId?: string | undefined; } | undefined, sessionId: string): { readonly sessionId: string; readonly inTree: boolean; }; /** * The root session id a leg's {@link HookInvocationIdentity} carries — the session the delegation tree's shared task list * belongs to (`task_list.partition_is_root_session`): the tree's root anchor (`internals.rootSessionId`) when present, else * the leg's own `sessionId` on a top-level leg (`!isDelegatedChild` and not `observerLeg`), else `undefined` (honestly absent * when a delegated or observer leg was handed no root anchor — never fabricating a child's own session as the root). */ export declare function hookRootSessionIdOf(internals: { readonly rootSessionId?: string | undefined; readonly observerLeg?: true | undefined; } | undefined, sessionId: string, isDelegatedChild: boolean): string | undefined; /** * The identity facts a parked row records of its leg (`CheckpointState.legIdentity`, reader word `leg_identity`), so a resume * reads them back instead of guessing them from the resuming turn: the task id its host declared (`spec.taskId`, after a member * ledger's rebind), the task that spawned it (`internals.parentTaskId`), the delegation tree's root and that root's occupant * (`internals.rootSessionId` with the scope its box lives in, `rootSessionScope`, written only together, and the root's occupant * `rootSessionIncarnation` beside them), its explicit spawn name (`internals.explicitAgentName` — never the display name a model * chose, `sourceAgentName`) and the session it read the person's session-scope rules at (`ruleSessionId`, {@link ruleSessionOf}), * written only where that is not the leg's own session. Each member is a non-empty string or absent; a leg with none records no * section. A row carrying `ruleSessionId` declares the reader word `rule_session`: an engine before the member refuses it as newer * (`version_newer`, the row pending) and does not resume it. */ export interface LegIdentityRecord { readonly declaredTaskId?: string; readonly parentTaskId?: string; readonly rootSessionId?: string; readonly rootSessionIncarnation?: string; readonly rootSessionScope?: string; readonly spawnName?: string; readonly ruleSessionId?: string; } /** The leg's {@link LegIdentityRecord} as its park mints it, off the leg's own spec and trusted internals and the rule session its asks * name (`ruleSession`: the session read, and the leg's own); `undefined` when it has none. */ export declare function legIdentityRecordOf(declaredTaskId: string | undefined, internals: { readonly parentTaskId?: string | undefined; readonly rootSessionId?: string | undefined; readonly rootSessionIncarnation?: string | undefined; readonly rootSessionScope?: string | undefined; readonly explicitAgentName?: string | undefined; } | undefined, ruleSession: { readonly read: string | undefined; readonly own: string; }): LegIdentityRecord | undefined; /** A row's `state.legIdentity` read back: absent ⇒ `undefined` (a row of a leg that had none, or of an earlier release); a record * whose every member is a non-empty string, with an occupant and a box scope only beside a root and no other key ⇒ that record (a * root recorded without its scope — an earlier mint of this line — is read but restores no root); anything else ⇒ * `"damaged"` — never read as absent, which would resume the leg under identities its park did not record. */ export declare function readLegIdentityRecord(raw: unknown): LegIdentityRecord | undefined | "damaged"; /** The members of the SYNCHRONOUS-CHILD BLOCK a parked row of an earlier release records beside its identity: the spawning run's session * and task, and the delegation tree's root session with the occupant of its row. One tuple, so the row's member-shape table, the summary * and the row judge (a row holding any of them is a synchronous child's — a leg nobody resumes) name the same four. No release mints * them any more: a synchronous child's parks are not minted (`approvalParkNotResumableOf`). */ export declare const SYNC_CHILD_BLOCK_MEMBERS: readonly ["parentSessionId", "parentTaskId", "rootSessionId", "rootSessionIncarnation"]; export type SyncChildBlock = { readonly [K in (typeof SYNC_CHILD_BLOCK_MEMBERS)[number]]?: string; }; /** The workflow member a leg runs as ({@link import("./runner/contracts.js").RunInternals}.workflowMember): the run's id, the * member's label (a display word — not unique within a run, never a key), and `askedAgain` when the call runs a step again that * the run it resumes already took up (its earlier approval card stopped waiting without a decision — expired or removed — or * the same call completed there and the replay stopped before it; never a step approved and not run). */ export interface WorkflowMemberIdentity { readonly runId: string; readonly label: string; readonly askedAgain?: true; } /** The facts a leg's ask source is minted from — all the leg's own, read once its session and task id are minted. */ export interface AskSourceFacts { /** The leg's end-user principal (its spec's). */ readonly principal: string | undefined; /** The leg's session id: the conversation every ask of the leg is raised in. */ readonly sessionId: string; /** The session a session-scope rule saved from a card of this leg is read and written at ({@link ruleSessionOf}). */ readonly ruleSessionId: string; /** The leg's one task id ({@link legTaskIdOf}, as minted at prepare). */ readonly taskId: string; /** The leg's effective delegation fact — forks included, the checkpoint's record on a deps-only resume. */ readonly isDelegatedChild: boolean; /** The leg's trusted internals: the parent link and the display names. */ readonly internals: { readonly parentTaskId?: string | undefined; readonly explicitAgentName?: string | undefined; readonly agentName?: string | undefined; readonly workflowMember?: WorkflowMemberIdentity | undefined; } | undefined; /** On a resumed leg: the attribution bit the parked row records (`CheckpointState.fromSubagent`) — what the leg's earlier asks * carried, read back for a resume that hands no parent task. */ readonly recordedFromSubagent?: boolean; /** On a resumed leg: the display name the parked row records (`CheckpointState.sourceAgentName`) — what the leg's earlier asks * carried, read back for a resume that hands no spawn facts. */ readonly recordedSourceAgentName?: string; /** On a resumed leg: the workflow member the parked row records (`CheckpointState.workflowMember`), read back for a resume that * hands no member (a host resuming the card directly); a resume that hands one names the run continuing the step. */ readonly recordedWorkflowMember?: WorkflowMemberIdentity; } /** * The leg's ask source, minted ONCE per leg and read by every face that names the issuing leg: the four `resolveAsk` * construction sites (the main gate, both inherited wrapper arms, the approved-edit re-ask) spread it, the question tool and * the gate's question routing project it, and the approval and plan_review parks stamp its session onto the row — so the * explicit "from a background agent" discriminator can never be dropped on one leg, and the same leg is never named two ways. * Every member is a fact of THIS leg's own prepare (an inherited ask fires in the child's gate): `internals` never rides a * TaskSpec, so a worker cannot self-declare or suppress them. * * - `sourceSessionId` — the leg's session: the conversation, never a task. * - `sessionScope` — the session a session-scope rule saved from this card is read and written at ({@link ruleSessionOf}). * - `agentId` — {@link agentIdOf} over the leg's task id and delegation fact: present exactly on a delegated child. * - `fromSubagent` — the attribution discriminator: trusted `parentTaskId` presence (a directly-started workflow's agent has * none, so it carries `agentId` without it), joined with the parked row's record on a resumed leg — a resume handed no * parent task keeps the bit its leg's earlier asks carried, and a record never takes away a parent task the resume hands. * Every row the leg parks records the result (`CheckpointState.fromSubagent`), so the row and the leg's asks answer it once. * - `isDelegatedChild` — the refusal posture: the RAW delegation fact, forks included (a fork's interaction contract is * one-shot — "no waiting for the user" — so a stop-and-wait refusal would ask it the impossible), so `resolveAsk`'s human * refusal texts speak the right posture (parent thread: stop and wait for the user; delegated child: adapt or report). */ export declare function mintAskSourceIdentity(leg: AskSourceFacts): AskSourceIdentity; /** The members that name a workflow member on a face — an ask, a pending row's source, a forwarded frame: one spelling. */ export declare function workflowMemberFace(member: WorkflowMemberIdentity): { workflowRunId: string; workflowAgentLabel: string; workflowStepAskedAgain?: true; }; /** A frame bubbling out of a member's leg, named for that member as one set: the member names it already carries (a nested * run's member, its re-ask bit included) are replaced, never left beside this member's. */ export declare function stampWorkflowMember<E extends { workflowRunId?: string; workflowAgentLabel?: string; workflowStepAskedAgain?: true; }>(e: E, member: WorkflowMemberIdentity): E; /** The workflow member an ask source names, as a row records it — the inverse of the spread in {@link mintAskSourceIdentity}. */ export declare function workflowMemberOf(source: Pick<AskSourceIdentity, "workflowRunId" | "workflowAgentLabel" | "workflowStepAskedAgain">): WorkflowMemberIdentity | undefined; /** Where a leg's user-face notice is addressed: {@link noticeAddressOf}. Spread into the notice's detail as-is. */ export interface NoticeAddress { /** The session the notice routes to (lifted onto the typed key at the one throat): the leg's own on a top-level leg, the * delegation tree's root on a delegated one. */ readonly sessionId: string; /** Present exactly on a delegated leg: the leg whose call the notice is about (its ask source's session). */ readonly sourceSessionId?: string; /** Present on a delegated leg that carries one ({@link agentIdOf}): the agent id the leg's events and its spawner's card name. */ readonly agentId?: string; } /** Where a leg's user-face notice goes ({@link noticeAddressOf}): the keys it carries, and the session address it is for. */ export interface NoticeRoute { /** The keys the notice carries — spread into its detail as-is. */ readonly address: NoticeAddress; /** The session address a notice routed away from the leg's own session is for: on a delegated leg addressed to its tree's * root, the root's id, the root occupant the tree was born under (absent = unknown) and the scope the root leg works in (its * `rootSessionScope`; absent = not recorded — the scope a notice may name a scope's entries to, `withheldEnvUserNotice`). A delivery to it is judged like any * frame addressed there (`PendingSessionNotifications.discardsStaleNotice`). Never spread into a notice: an occupant token * is an addressing fact, not a word the wire carries. Absent when the notice goes to the leg's own session. */ readonly destination?: SessionAddress; } /** * The address of a leg's user-face notice — one rule: a user-face notice goes to the end of the delegation tree a person * watches, and says which leg it came from. A top-level leg's notice is addressed to its own session and carries no source * keys. A delegated leg's notice is addressed to the tree's root session (`internals.rootSessionId`: an intermediate parent * at depth ≥ 2 has no host leg of its own to read it on) and carries the delegated leg's session and agent id — the shape a * subagent's permission denial takes upstream (the top-level session id plus the agent id). The root is a session address * like every other destination: its id and its occupant (`internals.rootSessionIncarnation`), so a notice of a tree whose * root was deleted and created again is not delivered to the root's later occupant. The id is read as the run's internals carry * it — a non-empty string or absent (`readRootOccupant` refuses a run whose root id is anything else) — so it is asked nothing here. */ export declare function noticeAddressOf(askSource: AskSourceIdentity, root: { readonly rootSessionId?: string; readonly rootSessionIncarnation?: string; readonly rootSessionScope?: string; } | undefined): NoticeRoute; /** A leg's user-face notice seat: where its notices go ({@link NoticeRoute}) and the admission every one is delivered under. */ export interface LegNotice { readonly route: NoticeRoute; /** Judged against the tree's root occupant (below); absent for a top-level leg and for a leg prepared outside a Runner. */ readonly admission: ((notice: EngineNotice) => boolean) | undefined; /** What the leg's own session has been told through the throat's fact keys (`deliverEngineNotice`'s `told`): the session's * announced record. A delegated leg's is its own session's, not its tree root's. */ readonly told: NoticeTold; } /** * The admission every user-face notice of a leg is delivered under: a notice routed to the tree's root is judged against the * root's current occupant, named by the leg it came from and its code — one addressed to an occupant the root no longer holds is * not admitted (the judge discards it and announces the discard). A top-level leg's notices, and every notice of a leg prepared * outside a Runner, are admitted as they are (no predicate). A notice a delegation lane says on a leg's behalf (a spawn's account, * `RunnerSelfSeat.emitNotice` with a route) is admitted by this same reading. */ export declare function legNoticeAdmission(route: NoticeRoute, judge: NoticeDestinationJudge | undefined): ((notice: EngineNotice) => boolean) | undefined; /** * The leg's one notice seat, minted ONCE at prepare beside the ask source (`Prepared.legNotice`): every seat that tells the * person something about this leg — the gate's stations, the shell's withheld-variables line — reads this address and this * admission, so a delegated leg's notice is never addressed one way here and another way there. */ export declare function mintLegNotice(askSource: AskSourceIdentity, root: { readonly rootSessionId?: string; readonly rootSessionIncarnation?: string; readonly rootSessionScope?: string; } | undefined, judge: NoticeDestinationJudge | undefined, told: NoticeTold): LegNotice;