UNPKG

@sema-agent/core

Version:

Stateless, task-oriented AI agent core

642 lines (641 loc) • 50 kB
import { type BackgroundAgentRecord, type BackgroundAgentSettledStatus, type BackgroundAgentStore, type ParkedCardStore } from "./background-agent-store.js"; import { type StopSource, type TaskAccess, type TaskRetrievalStatus, type UnifiedTaskOutput, type UnifiedTaskResult, type BackgroundAgentTaskHandle, type ChildSessionStore, type RegisteredTaskHandle, type LoopTaskHandle, type DurableAgentCore, type ParkedClaimTicket, type ParkedClaimRollback, type RegisterBackgroundAgentInput, type LandedAgentTerminal } from "./task-registry-shared.js"; import type { ExtensionRequester } from "./task-event.js"; import { type AgentState } from "./agent-state.js"; import type { CheckpointSeat } from "./checkpoint-seat.js"; import { type MemberOutcome } from "./member-outcome.js"; /** * Subagent transcript persistence (delegation entry caps) — the ACTIVE handles of one delegation * tree in THIS process's registry: `background_agent` handles whose status is running/pending * (parked does not burn a concurrency slot — a suspension is not concurrency; terminal handles are * not active) under the `(scope, rootSessionId)` key (full depth: a grandchild's row carries the * tree's fixed-point root, so the whole tree shares one pool; a row whose rootSessionId * predates the floor falls back to its parentSessionId — same fallback the access predicate's root * arm rides). SYNCHRONOUS by contract: the caps enforcement hands an admitted child's slot to its row * in the synchronous segment that registers it, which is what makes the reservation race-free in-process. */ export declare function activeDelegationHandlesLane(core: DurableAgentCore, scope: string, rootSessionId: string): string[]; /** One running or pending row of a delegation tree, as its occupant is told of it: the task id every addressing tool takes, the row's * addressing name, and whether the caller asked for can address it (the registry's access predicate — a nested agent does not reach * its siblings' rows, so their ids and names are not its to be told). */ export interface DelegationOccupant { readonly id: string; readonly name: string; /** The extension that started the row, when one did (the row's `requestedBy`); absent for every other row. */ readonly requestedBy?: ExtensionRequester; readonly reachable: boolean; } /** The rows {@link activeDelegationHandlesLane} counts, with the name each is addressed by — the one selection, read in the same * synchronous segment. `caller` is the access the asking leg's own tools hold (`scope` is the pool's); without one no row is reachable. */ export declare function activeDelegationOccupantsLane(core: DurableAgentCore, scope: string, rootSessionId: string, caller?: TaskAccess): DelegationOccupant[]; /** Where one active row of a delegation tree lives: its task id and the scope it is registered in. */ export interface DelegationRowAddress { readonly handle: string; readonly scope: string; } /** The ACTIVE (running / pending) rows of one delegation tree in THIS process, in EVERY scope — the live twin of * `BackgroundAgentStore.listByRoot`: the same tree reading ({@link delegationRootOf}) and the same root judge * ({@link assertDelegationRoot}), each row with the scope a follow-up addresses it in. */ export declare function activeDelegationHandlesByRootLane(core: DurableAgentCore, rootSessionId: string): DelegationRowAddress[]; export declare function ensureDurableHeartbeatLane(core: DurableAgentCore): void; /** design/151 S1a — enqueue one durable-row write (see {@link DurableAgentLane} for the lane * contract). `patch` is captured at CALL time (the settle-site values), applied in chain order. * * hardening — failure taxonomy, decided by EVIDENCE not by exception class: * - thrown store errors get a bounded in-lane retry (a transient outage must not permanently * strand a terminal write — the settle is often the row's LAST write); * - `already_exists` on a put and a lost CAS both trigger a read-back writerId check first: an * AMBIGUOUS earlier commit (backend committed, response failed) or a stale-arm flip * (reap wrote rev up under a live run) is OUR row wearing a newer rev — ADOPT the rev and * re-apply (the real terminal truth must beat a reaper's guess); only a row carrying a * FOREIGN writerId poisons the lane (never clobber another writer). * * After each write of the row lands, the row's tree root is read again ({@link rootOccupantGone}): a root with no row, or one holding * another occupant than the row names, means the session the tree belongs to was deleted — the writer removes its row (under the * revision it just wrote) and writes no more of it, as for a row reaped under it. */ export declare function durableAgentWriteLane(handle: BackgroundAgentTaskHandle, patch: Partial<BackgroundAgentRecord>, clear?: readonly (keyof BackgroundAgentRecord)[]): void; /** design/151 S1b — is this row's durable lane ARMED (store wired at registration, not contaminated)? * Observability/test face. ⚠️ NOT the lifecycle-flip key: arming says a row was ATTEMPTED, not * that it EXISTS — release anchors gate on {@link durableAgentRowProbe}. */ export declare function durableAgentArmedLane(core: DurableAgentCore, id: string): boolean; /** design/151 S1b — the AWAITABLE lifecycle-flip key. Returns undefined when no * lane was armed; otherwise a probe CLOSURE over the HANDLE (outlives its eviction from the map — * retain-ledger TTL arms fire hours after terminal GC) that flushes the queued writes and answers * whether a durable row actually EXISTS right now (initial put confirmed, not contaminated). A store * that never accepted the put answers false — the caller then RELEASES the session (pre-151 path) * instead of stranding an unreachable transcript. * * The closure reads `handle.durable` at CALL time rather than capturing the lane: a revive's claim * hands the row to a fresh lane and retires the one that was current when the probe was minted, and * a probe bound to the retired object would answer "no row" for a row that plainly exists — and the * caller acts on that by releasing the child's transcript session, the exact strand this probe was * built to prevent. Reading through the handle also survives the map eviction (the closure keeps the * handle object alive), so the outlives-terminal-GC property the retain arms rely on is unchanged. */ export declare function durableAgentRowProbeLane(core: DurableAgentCore, id: string): (() => Promise<boolean>) | undefined; /** The row-side facts the durable rung admits a row on (`durableRungAdmitsRow`: its agent type, its transcript anchor, its * birth floor), read off this process's copy of the row's durable record — the facts tier-3 reads off the stored row. * Undefined when the row has no durable lane. Read after the row's probe resolved: the probe flushes the lane the record * is written through. */ export declare function durableAgentRowFactsLane(core: DurableAgentCore, id: string): Pick<BackgroundAgentRecord, "agentType" | "sessionId" | "policyFloor"> | undefined; /** design/151 §7.3 (F-8) — open the in-process claim window for a tier-3 revival of `id`. * SYNCHRONOUS admission check + mark in one call: refuses (false) while the handle is live * running/pending in THIS process (the live delivery ladder owns it), while a reap sweep is * adjudicating it, or while another claim holds the window. The caller MUST call * {@link endDurableClaim} in a finally — the window spans its whole claim-CAS→spawn/rollback * sequence, and a leaked mark would permanently fence the handle from revival, reap AND the * terminal-handle GC (which skips ids inside this window), i.e. it would also leak the handle. */ export declare function beginDurableClaimLane(core: DurableAgentCore, id: string): boolean; /** Close the {@link beginDurableClaim} window (idempotent). */ export declare function endDurableClaimLane(core: DurableAgentCore, id: string): void; /** design/151 §3.1 MED-7 — the BLESSED retention path: the joint reap that drops a * doomed row under its rev guard and THEN releases its transcript session (raw `store.reap` * strands transcripts — its contract says so). Policy semantics mirror the store sweep (double * bound + stale-running flip; running rows are never deleted). * * ORDER: conditional DELETE first, release only after the delete WINS. The * reverse (release→delete) destroyed a live transcript whenever a concurrent writer revived the * row between the release and the losing CAS (stale-flip heal / retained-session revive: row * back to running, session already gone). Failure economics of this order: a crash (or release * throw) after the winning delete LEAKS the session to the deployment's session TTL sweep — * recoverable — instead of stranding a live transcript — not. The release must therefore treat * a missing/already-released id as a no-op (idempotent — a release that throws on * not_found merely forfeits this bookkeeping, the TTL sweep still owns the orphan). An * IN-PROCESS handle running again gates the row out entirely (a revive flips memory before its * durable write flushes). `sessions` is structural (unpin/release) so any SessionStore fits * without an import cycle. * ⚠️ Review note: passing `sessions` DECLARES that its `release` truly ends the * transcript's addressability (durable deletion or an equivalent lifecycle handoff). A face * whose release only clears in-memory state must NOT be passed — omit `sessions` instead * (fail-closed: rows are kept and counted in `skippedNoSessions`, and the deployment's own * session GC owns the joint lifecycle). A half-true release deletes the row while the * transcript lives on unanchored — the exact strand this orchestration exists to prevent. */ export declare function reapDurableAgentsLane(core: DurableAgentCore, scope: string, deps: { store: BackgroundAgentStore; /** `listPlaced` (subagent transcript persistence, optional): arms the PARTITION leg below — * placed transcript sessions whose owning row is GONE (row never landed / deleted-but-release- * failed) are the orphan shape only a session-side enumeration can find. Absent = that leg is * unavailable (documented; the row-joined path still runs). */ sessions?: { unpin?(sessionId: string): unknown; release(sessionId: string): Promise<void> | void; listPlaced?(kind: "subagent", opts?: { olderThanMs?: number; scope?: string; }): Promise<Array<{ sessionId: string; placedAt: number; } & ({ scope: string; handle: string; } | { tupleIncomplete: true; })>>; }; /** design/151 §7.7 (F-13) — the row's mailbox dies with the row: a WINNING delete also drops * the (scope, handle) mailbox (advisory — a mailbox fault never blocks the reap; an orphaned * box is bounded by the mailbox's own `reap` policy). Structural face (drop only), so the * deployment passes its `RunnerDeps.mailboxStore` directly. */ mailbox?: { drop(scope: string, handle: string): Promise<void>; }; }, policy: { now?: number; maxAgeMs?: number; keep?: number; staleRunningMaxAgeMs?: number; }): Promise<{ rowsReaped: number; sessionsReleased: number; skippedNoSessions: number; orphanPlacedReleased: number; }>; /** A background-agent row's child session: its id and the store the child's run holds it through (`undefined` when a * caller outside the bind's types bound the id without one). */ export interface ChildSessionAnchor { readonly sessionId: string; readonly store: ChildSessionStore | undefined; } /** The child session a background-agent row is anchored to right now — the session its child runs on * or keeps (transcript / resume anchor), with the store that holds it — or `undefined` before the bind, * after the anchor's release, and on every other task type. The ONE reading of the anchor: the list * row's `sessionId`, the transcript read ({@link TaskRegistry.anchoredSession}) and the release and bind * matches below all read it here. */ export declare function childSessionAnchorOf(handle: RegisteredTaskHandle): ChildSessionAnchor | undefined; /** A successful release of the child session `sessionId` ends the row's claim on it: both copies of * the anchor go — the handle's (the list row reads it) and the durable record's (the heartbeat's F-1 * arm keeps re-driving a flush-failed lane, so a later successful flush would otherwise persist a row * pointing at the session the caller just released). The clear matches the PAIR (row, session): a late * clear for a session the row no longer names leaves the current anchor alone. State/result survive, * the transcript does not. */ export declare function releaseDurableTranscriptAnchorLane(core: DurableAgentCore, id: string, sessionId: string): void; /** design/151 S1a — bind the child's freshly-minted store session id onto the row (the transcript / * resume anchor; absent at register time because the child session is created after registration). * A row already anchored to that session is left as it is (one fact, no second durable write). A row anchored to ANOTHER session * is refused: an anchor changes only by its release ({@link releaseDurableTranscriptAnchorLane}) and a fresh bind, never by being * written over, so the earlier session is never silently left unowned. An id that is no anchor (not a string, empty, or carrying an * invisible character anywhere in it) is refused whatever the row holds, and so is a task id that names no background-agent row — * nothing is anchored to a row that is not there. Every refusal of the bind is one `config.invalid_argument` mint ({@link bindRefusal}). * `store` is the store the child's run holds that session through, bound with the id (the * store is process-local; the durable record keeps the id alone). The store is judged where it is read: the transcript read * (`$.session.messages`) refuses, naming why, one it cannot read through. */ export declare function bindBackgroundAgentSessionLane(core: DurableAgentCore, id: string, sessionId: string, store: ChildSessionStore): void; /** * #22 — record the org-memory admission verdict THIS leg adjudicated onto the durable row, so the * next CROSS-PROCESS revival (which rebuilds the spec from the reviving caller's mount and cannot * see this process's memory) starts from it instead of re-adjudicating unconstrained. Called once * per leg at the injector-ready barrier (prepare has succeeded by then, so the verdict exists); a * revival cycle overwrites the previous cycle's record with its own, which the admission door * guarantees is a subset. Same durable write lane as every other post-registration fact — a * store-less registration is a no-op, and a contaminated lane stays contaminated. * * A DROPPED write is disclosed. This record is the only carrier a narrowing has across a process * boundary, so what stands on the row when the write does not land is the PREVIOUS leg's wider * verdict — which the next revival then seeds from. Two drop shapes, one message each: a contaminated * lane refuses at its front door, and an exhausted flush leaves the value unwritten with the lane * still armed. The channel is the same durable-agents notice the lane uses for the contamination * itself (that one says writes stopped; this one says WHICH fact stopped with them). The write * remains best-effort: no leg faults because its bookkeeping failed. * * The FORK background lane deliberately does not call this: a fork row refuses the tier-3 claim * (subagent.ts, revival preflight), so it has no reader. */ export declare function recordBackgroundAgentOrgAdmissionLane(core: DurableAgentCore, id: string, verdict: import("./memory-admission.js").OwnOrgAdmissionVerdict): void; /** design/115 P3: register a background sub-agent run. The CALLER owns driving the child promise and * calling {@link settleBackgroundAgent} at the end; the registry provides the unified task_id, the * owner/scope guard, poll/stop dispatch, and terminal GC — exactly like the other two kinds. */ export declare function registerBackgroundAgentLane(core: DurableAgentCore, input: RegisterBackgroundAgentInput): string; /** * design/153 §7.1 — the SINGLE running→parked transition point (r4 F-02: every parked-related * transition goes through registry discipline, never a bare store-status write). Called by the bg * watcher's `suspended` branch AFTER the durable checkpoint exists and the deployment attested the * child session durable (§7.3 capability). First-writer-wins like settle: a TaskStop that already * flipped killed refuses the park (returns undefined — the CALLER must then EXPIRE the checkpoint, * the §7.1 no-orphans compensation). Releases the delivery channel like the other non-running * transitions (a parked child has no live lane; a SendMessage parks per session as with terminal). */ export declare function parkBackgroundAgentLane(core: DurableAgentCore, id: string, park: { checkpointToken: string; resolveStop?: () => Promise<boolean>; completionSink?: BackgroundAgentTaskHandle["parkedCompletionSink"]; }): "parked" | undefined; /** * design/153 §7.1 (件1 split-brain fence) — the registry-aware reconciliation entry an * IN-PROCESS deployment calls on its retention cadence. Two halves, one call: * 1. LIVE half: this instance's own parked handles are probed against their checkpoint and, on * expired/missing/DECIDED-ELSEWHERE, settled failed THROUGH the handle (poll/stop/durable row * stay coherent — quiescence poked, arbitration closure dropped, durable clear rides the settle * write). * 2. STORE half: delegates to the store-level {@link reconcileParkedAgents} with * `excludeWriterId` = this instance's writerId, so foreign (dead-process) rows are cleaned * without ever touching a row this process is live-managing. * * The live half used to skip EVERY non-expired/missing state, `resolved` included, and * that was the whole zombie: a parked row whose checkpoint was decided by the task-level * `resumeCheckpoint` leg (a serving layer's documented fallback when its parked-row discriminator * misses) is decided but still says `parked`, its side effects already executed by a path that never * touched this registry. Nothing else could reach it either — a re-claim answers * `checkpoint_not_pending`, and the store half excludes exactly the rows this process live-manages — * so it stayed `parked` for the life of the process. It now converges here. The one `resolved` case * that must still be LET THROUGH is the registry's own claim window (reservation → consume flip): that * skip is load-bearing, and it is now expressed as what it actually is — a claim in flight — instead * of as a blanket state filter that also swallowed the case with no claimant at all. */ export declare function reconcileParkedAgentsLane(core: DurableAgentCore, stores: { agentStore: import("./background-agent-store.js").BackgroundAgentStore; checkpointStore: ParkedCardStore; }, scope: string, now: number, opts?: { staleClaimMaxAgeMs?: number; }): Promise<{ failed: number; rolledBack: number; unreadable: number; }>; /** * design/153 §7.2c (件3b) — the RESERVATION half of the parked-resume claim. Pure reservation * (r6 H-1): the row STAYS `parked` — only `parkClaimId` is written (guarded CAS binding * rev+status+token+claim-absence, single-claimer by construction), the live handle keeps its * parked state AND its TaskStop arbitration closure (an expire during the reservation window * still wins — by design; the resume's token-consume CAS is the only thing that can beat it). * parked→running happens ONLY at the 件3c consume flip, never here. * * AUTHORIZATION LAYERING (r4 F-06, doc-pinned): `access` is ROW VISIBILITY only * ({@link canAccessAgentRecord}); the operator's approval authority (/decide) is the SERVER's * front gate — this verb must only be reached by an already-authorized decision path. * Binding (r6): the token comes FROM THE ROW (never caller-supplied), the checkpoint must be * `pending`, and `cp.sessionId === row.sessionId` (same child transcript — blocks swapping in a * different child's checkpoint). */ export declare function claimParkedAgentLane(stores: { agentStore: import("./background-agent-store.js").BackgroundAgentStore; checkpointStore: Pick<import("./checkpoint-store.js").CheckpointStore, "get">; }, handle: string, access: TaskAccess): Promise<{ ok: true; ticket: ParkedClaimTicket; } | { ok: false; reason: "not_found" | "not_parked" | "binding_broken" | "checkpoint_not_pending" | "claim_lost" | "store_unreachable"; }>; /** * design/153 §7.2c (件3b) — roll back / settle a reservation whose resume could not proceed. * Disposition follows the CHECKPOINT truth re-read at call time (r6: never blind): * - `pending` → the reservation is cleared, the row returns to plainly-redeemable `parked` * ("rolled_back"); no ownership change (a reservation never transferred it, so no epoch churn). * - `expired`/`missing` → honest terminal `failed` ("failed") — same shape reconciliation mints. * - `resolved` → the approval was CONSUMED (the resume died post-consume): NON-COMPENSABLE * (r6 H-4) — terminal `failed`/outcome-unknown ("failed"); NEVER back to parked. * - store unreachable → "retry" (nothing written; the reconciliation cadence is the backstop). * - another transition owns the row (the claim is not this ticket's, a CAS lost, the row is no longer parked) → "lost" when * the approval reads pending when the loss is observed (it may still be decided), "lost_unredeemable" when it does not (#4476: * the one word used to answer both, and a host could not tell "decide again" from "nothing left to decide"). * A LOCAL live parked handle follows the failed dispositions (poll/stop stay coherent). */ export declare function rollbackParkedClaimLane(core: DurableAgentCore, stores: { agentStore: import("./background-agent-store.js").BackgroundAgentStore; checkpointStore: Pick<import("./checkpoint-store.js").CheckpointStore, "get">; }, ticket: ParkedClaimTicket): Promise<ParkedClaimRollback>; /** * design/153 §7.2d (件3c 3c H-1) — the CONSUME FLIP, SERIALIZED THROUGH THE DURABLE LANE: * the guarded parked→running store CAS runs INSIDE the lane chain (ordered after every seeded * write — the session-bind CAS included), so no in-flight lane write can race it, CAS-fail on the * flip's rev bump, read the new epoch as foreign, and contaminate the lane (which would permanently * drop finalize/terminal/re-park writes). On a won CAS the lane record/rev swap happens in the * SAME chain step and the live handle adopts (running, arbitration dropped — the token is * consumed; `parkClaimId` stays visible until finalize). Returns the stop-cycle number the flip WROTE on the row (the * resumed cycle's, `nextCycleSeq`) — the caller reads its cycle from the claim that opened it, never from the registry * afterwards, where the handle it flipped may already have been replaced — or `undefined` when the row moved * (claim/status/token guard lost): the caller aborts the resume. */ export declare function consumeParkedFlipLane(core: DurableAgentCore, id: string, stores: { agentStore: import("./background-agent-store.js").BackgroundAgentStore; }, ticket: ParkedClaimTicket): Promise<number | undefined>; /** * design/153 §7.2d (件3c) — attach finalize: the resume's delivery lane is live, the claim is * complete — clear `parkClaimId` (handle + durable row) so reconciliation stops seeing an * in-flight claim. Idempotent; a terminal settle that lands first clears the same keys itself. */ export declare function finalizeParkedResumeLane(core: DurableAgentCore, id: string): void; /** What a background agent's terminal settle carries. `leg`: the child's result this settle speaks for, when the leg produced one — * the row's outcome is minted from it by the settle that wins the row (with the row's winning status), so the ending word and the * leg's refusals (asks nobody answered, calls refused, a verdict that never landed, a person's halt) ride the row together. */ export interface BackgroundAgentSettle { status: BackgroundAgentSettledStatus; result?: string; resultFull?: string; error?: string; errorCode?: string; retryable?: boolean; errorKind?: string; retryAfterMs?: number; stoppedBy?: StopSource; cycle?: number; leg?: import("./task-result.js").TaskResult; } /** Terminal update for a background agent (the spawn-side then/catch calls this exactly once). * Reap sentinel note: {@link BG_AGENT_REAP_STOP_ERROR} is never STORED as `error` — status "killed" * + stoppedBy already carry that fact, and an `error:` line beside a salvaged partial result reads * as "the child's work failed" (review A1 follow-up). Real errors (e.g. "session released", a * child crash) still land. * design/129-B: returns the WINNING terminal status — the earlier writer's when this settle is refused * (first-writer-wins), `undefined` when the handle is gone (evicted). Callers report THIS in their * notification/terminal event so the push never contradicts the registry row (a completed-notify over a * killed row — the TaskStop-then-late-resolve race — was an observable incoherence). */ export declare function settleBackgroundAgentLane(core: DurableAgentCore, id: string, outcome: BackgroundAgentSettle): BackgroundAgentSettledStatus | undefined; /** Abort every background agent belonging to `access` (parent-task teardown — a finished parent must * not leave orphan child runs burning tokens; mirrors clearBackgroundForOwner for bash). */ /** Whether the owner sweep ({@link abortBackgroundAgentsForOwnerLane}) under `access` and `opts` takes this row: a running loop-type * background task (`isLoopTask`: a background agent or a peer task) `access` reaches, of the lifetime `opts` names. The one selection * the sweep makes and a reader of what a run's end will stop asks ({@link TaskRegistry.runEndReading}). */ export declare function ownerSweepTakes(handle: RegisteredTaskHandle, access: TaskAccess, opts?: { skipSessionScoped?: boolean; sessionScopedOnly?: boolean; }): handle is LoopTaskHandle; export declare function abortBackgroundAgentsForOwnerLane(core: DurableAgentCore, access: TaskAccess, opts?: { skipSessionScoped?: boolean; sessionScopedOnly?: boolean; }, runEnd?: import("./member-outcome.js").RunEndCause): number; /** Name resolution over the caller-visible background_agent rows * (sema's single-lane subset: no teammate/name-registry branches). * * [c209-D] PUBLIC + explicit-name layer 0: the single resolver both TaskStop (:1839 leg) and * SendMessage consume, so the two verbs can never drift. Ladder: * 0a. exact `handle.name` match — LATEST-WINS (the reference's SendMessage doc: "a newer agent took the name * (latest wins)"; older rows stay task_id-addressable), any status (names keep working after * completion — the resume face is the CC "resumes it from its transcript" counterpart). * **Latest-wins is RESOLUTION, not permission to redirect a conversation**: CC pairs the same * resolver with a per-conversation pin guard (`send_message_pin_guard`), and SendMessage REFUSES * a send whose name has changed hands since this caller last used it. That guard lives at the * SendMessage face (`createSendMessageTool`), exactly as it does upstream — this resolver stays * a pure resolver, and TaskStop keeps its own (preferRunning) posture untouched; * 0b. normalized `handle.name` match — same latest-wins (the spawn regex bans whitespace, so * normalization here is effectively NFKC+lowercase: "Builder"/"builder" share the name pool); * 1+. the pre-[c209] description layers, UNCHANGED (exact → normalized, running-preferred + * ambiguous posture — TaskStop's anchored behavior does not regress). * Matching key of the legacy layers = normalized description (the spawn label that IS the agent's * name on this face). Running rows are preferred over terminal ones (CC `gvy`), so a re-used label * addresses the live agent, while a lone terminal match still resolves. */ /** NOTE (server[1523] 对等复审 4b, recorded): the name pool scans LIVE handles only — durable-only * rows (restart survivors) are deliberately NOT name-addressable through this rung (canonical-only * 裁决); tier-3 revival's name rung goes through the roster instead (design/151 §7.4). */ export declare function resolveBackgroundAgentByNameLane(core: DurableAgentCore, name: string, access: TaskAccess, opts?: { preferRunning?: boolean; }): { status: "found"; handle: BackgroundAgentTaskHandle; } | { status: "ambiguous"; message: string; } | { status: "not_found"; suggestion?: string; }; export declare function claimBackgroundAgentNameLane(core: DurableAgentCore, requested: string, access: TaskAccess): { name: string; release(): void; }; /** S2b': flip the park-redeemability bit AFTER retention is actually secured * (tryRetainChild success) — registration happens before the retain attempt, and session-scope * alone does NOT retain the child session, so declaring at register time acknowledged parks that * a capacity/pin failure (or plain session-scope) could never drain. No-op on unknown ids. */ export declare function markRetainedContinuationLane(core: DurableAgentCore, id: string): void; /** * The row's stop-cycle number as its last WON claim left it: fresh spawn = 1, a seeded registration = the number its * claim wrote on the row, a retained wake / a consume flip = the number that claim wrote ({@link nextCycleSeq}). Every * reader in this process reads this one number — the spawn lanes thread it into the child's `RunInternals.cycleSeq`, * the frames and the completion notification carry it, poll serves it. A parked row answers the cycle that parked: the * next number exists only once the consume flip has won, and a drive reads it then. `undefined` for an unknown id or * a non-agent row. */ export declare function backgroundAgentCycleSeqLane(core: DurableAgentCore, id: string): number | undefined; /** S2b RB-27② — REVIVE a settled background-agent row for a retained-session RESUME cycle: the * row flips back to "running" with a fresh "attaching" channel and a bumped revive-cycle stamp * (returned; the resume leg threads it through attach and settle so a stale cycle's late calls * can never clobber the new cycle's channel). Only terminal rows revive; running rows refuse * (the SendMessage still_running path owns that story). * * The DURABLE transition is a {@link claimTerminalRowForRevive} claim, awaited BEFORE the in-memory * flip — the two orders are not equivalent: flipping memory first (the pre-arbitration shape, a plain lane * write with no row guard) let a foreign claim win the row while this process kept serving * `running`, and every later write of this cycle then died in the contaminated lane without a trace. * Three lane shapes, three treatments: * - NO lane (store-less registration): the pre-arbitration in-memory-only transition, unchanged — there is * no row, hence no arbitration domain and no competitor; * - lane whose initial put never CREATED a row: same — nothing exists for a foreign claim to have * won, and the write lane keeps retrying the put. This is decided by the claim's own read INSIDE * the chain (row absent AND the put still unconfirmed after the chain drained), never by the * entry-time flag: a put merely in flight, or one that committed under a failed response, would * otherwise exempt a row that does exist — the split-brain window this claim exists to close; * - Contaminated lane (stale-flip ownership transfer / row reaped mid-write / heartbeat re-drive * ceiling): the claim runs DIRECTLY through the store — the dead lane's chain is no longer a * serialization point, the rev guard is. Re-arming that lane object in place is what is NOT * done: an in-flight write from it would then see its own writerId on the re-claimed row and * adopt it. It stays contaminated (its record keeps the pre-claim writerId/epoch, so its read-back * answers "foreign" and it stops), and a row that cannot be claimed — gone, or live under * another writer — REFUSES the revive rather than resuming with a durably unreachable row. * A WON claim always hands the row to a FRESH lane for the claimed generation (both paths), for the * reason spelled out at the handoff: a write appended while the claim was in flight speaks for the * cycle that just ended and must not be applied under the new ownership. * While the claim is in flight the handle holds the in-process claim window * ({@link beginDurableClaimLane}'s mark), so a tier-3 claim, a reap sweep and a second revive of * the same row all observe it and take their existing refusals. */ export declare function reviveBackgroundAgentLane(core: DurableAgentCore, id: string, access: TaskAccess, abort?: AbortController): Promise<{ ok: true; cycle: number; } | { ok: false; reason: "not_found" | "still_running" | "recycling"; }>; /** S2b RB-27② — settle a REVIVED cycle (cycle-stamped: a stale cycle's late settle is a no-op so * it can never flip a newer revived cycle back to terminal / clear its channel). */ export declare function settleRevivedAgentLane(core: DurableAgentCore, id: string, cycle: number, outcome: Omit<BackgroundAgentSettle, "stoppedBy" | "cycle">): BackgroundAgentSettledStatus | undefined; /** * design/164 — record LIVE ACTIVITY on a running background-agent row (one tool-lifecycle beat). * * Why this exists: retiring `SESSION_BG_DEFAULT_TIMEOUT_SEC` removed the wall-clock ceiling that used to * bound a session-scoped background child, and the design/129 F3 anti-zombie duty had to land somewhere * that does not punish a legitimately long task. Collecting by STALL needs a stall signal, and the * in-memory row had none: `updatedAt` moved only at registration / park / settle, so a child grinding * through a two-hour job looked exactly like one whose lane died. This is that signal — the same * tool-lifecycle beat the fleet view already samples, recorded on the row. * * Cheap and idempotent: a timestamp write on a running row, no-op for anything else. The row's silence is read on the process-monotonic * clock, so the beat records that clock (`atMono`, `performance.now()` milliseconds; default now) beside the wall-clock `updatedAt`. An instant * that is no reading of this process's clock (negative, non-finite, or past its now by more than a second) is refused, whatever the row; * one earlier than the row's stamp does not take the stamp back ({@link stampActivity}). */ export declare function noteBackgroundAgentActivityLane(core: DurableAgentCore, id: string, atMono?: number): void; /** * The row's running leg hands it a reader of the calls the leg holds at its gate for an approver * ({@link import("./runner/contracts.js").RunInternals.reportApprovalHolds}). It replaces the previous leg's (every leg reports its * own, as {@link attachYieldLegLane} does for the yield controller) and is read, not recorded: the handle keeps the closure, never a * copy of what it answered. A handle the registry does not hold, or one that is not a live background agent, takes none. */ export declare function attachBackgroundAgentApprovalHoldsLane(core: DurableAgentCore, id: string, read: () => readonly import("./agent-state.js").ApprovalHeldCall[]): void; /** The seat a delegation lane hands the leg it runs as registry row `id` ({@link import("./runner/contracts.js").RunInternals.reportApprovalHolds}): * the reader the leg reports of its own gate table becomes the row's. */ export declare function rowApprovalHoldsSeat(registry: { attachBackgroundAgentApprovalHolds(id: string, read: () => readonly import("./agent-state.js").ApprovalHeldCall[]): void; }, id: string): (read: () => readonly import("./agent-state.js").ApprovalHeldCall[]) => void; /** * design/164 — collect SESSION-SCOPED background agents that have gone SILENT, the replacement for the * retired 30-minute wall-clock ceiling on that lane. * * By STALL, not by age, deliberately: the old ceiling killed a child at thirty minutes whether it was * wedged or working, and "this task has run a long time" is not evidence of anything. `staleMs` is the * caller's policy — there is no default here either, matching every other retention policy in this file * (`maxAgeMs` / `keep` / `staleRunningMaxAgeMs` are all caller-supplied). Size it well above the child's * expected quiet stretches; the durable twin's guidance (≥10× the heartbeat) is the reference point. * * Silence is measured on the PROCESS-MONOTONIC clock (`lastActivityMono`, `nowMono` = `performance.now()` milliseconds): a wall-clock * step — an NTP correction, a manual change — or a machine that slept would otherwise read as silence on one row and hide it on * another. `updatedAt` stays the displayed wall instant. The durable sweep (`staleRunningMaxAgeMs`) is a different question — a row * another process wrote — and stays on the wall clock, which is the only one two processes share. * * A row whose leg holds a call at its gate for an approver right now ({@link approvalHeldOfHandle}, the reading the state faces * state as `awaiting approval`) is not silent: it waits on its approver, as a parked row does, and is not collected however long the * wait. Once nothing is held, its silence is read off the same clock as any other row's (the wait itself does not move it). * * A collected row is settled `killed` with attribution `"system"` and the SAME wording the durable * stale-running sweep writes, so an operator reading either face sees one explanation, and its run is * aborted. Returns how many rows were collected. */ export declare function reapStaleSessionBackgroundAgentsLane(core: DurableAgentCore, staleMs: number, nowMono?: number): number; /** * THE announcement of a background agent's terminal the REGISTRY landed without the cycle's attempt — the session or stale * reap of a running cycle (its attempt's unwind would report only once the in-flight tool honored the abort), a TaskStop * that won a parked cycle's arbitration, a parked cycle's approval found expired or decided elsewhere (the attempt that * parked returned at the park; nothing else would report it). Sent now, once: through the notifier the spawn lane hung on * the handle, under the one terminal once-latch the attempt's own unwind claims too (`claimAgentTerminalNotify`) — the * first sender wins, the other stays silent. A lane that hung no notifier (direct registry use) keeps its unwind's * announcement. A parked cycle's ending names the lane its park recorded the debt on (`ending.parkLane`); a park that * recorded none owes nothing in this process, and its caller announces nothing. */ export declare function announceLandedAgentTerminal(core: DurableAgentCore, handle: BackgroundAgentTaskHandle, notifier: BackgroundAgentTaskHandle["terminalNotifier"], ending: LandedAgentTerminal): void; /** S2b — retention is REVOCABLE: every path out of the retain ledger (TTL / LRU / abandon / the parent * run's or the session's disposal) calls the entry's `onLeave`, which the spawner hooks to call this — so a * park can never be acknowledged against a continuation that was already released (the write-once bit was * a lie window during parent teardown), and the terminal GC stops keeping the handle for it. */ export declare function unmarkRetainedContinuationLane(core: DurableAgentCore, id: string): void; /** The retained entry's departure is settled (its statement sent, or found unchanged): the terminal GC stops keeping the handle. */ export declare function endRetentionHoldLane(core: DurableAgentCore, id: string): void; /** design/147 S2a — park a RUNNING child's live notification injector on its handle (called by the * spawner once the child's lane binds). No-op for unknown / non-agent / already-terminal rows — * the lane can outrace a fast child, and a stale attach must never resurrect a settled handle. */ export declare function attachAgentNotifyLane(core: DurableAgentCore, id: string, notify: NonNullable<BackgroundAgentTaskHandle["notify"]>, cycle?: number): void; /** design/147 S2a — deliver a message TO a RUNNING background agent at its next turn boundary * (CC's in-memory pendingMessages pedestal). Access-checked like every registry verb; returns a * typed refusal instead of throwing so the SendMessage face can phrase honestly: * `not_running` (row isn't running any more — race with settle; caller re-reads status) or * `no_channel` (running but its lane never attached — e.g. a non-core spawner). */ export declare function deliverToRunningAgentLane(core: DurableAgentCore, id: string, access: TaskAccess, notification: import("./task-notification.js").TaskNotificationPayload, opts?: { priority?: import("./task-notification.js").SystemInjectionPriority; }): Promise<{ ok: true; disposition: "queued" | "parked" | "pending"; } | { ok: false; reason: "not_found" | "not_running" | "no_channel" | "queue_full" | "refused"; }>; /** CC `ugo`/`rxo` (206:575227-575243 = 220:467937-467951): `id (label)` rows for a not-found footer — * running bg agents the CALLER can address (canAccess scopes; CC's self/observer exclusions have no * counterpart rows here). This variant is the SENDMESSAGE footer's source and lists EVERY running * agent, named or not: SendMessage addresses by name AND by id, so dropping named rows here (the way * {@link runningAgentFooterLane} does for the task faces, per CC `rxo`'s `!n.has(i.id)`) would hide the * very rows a retry needs. The label is the name someone chose, else the description: the name an agent * carries only because it must carry one ({@link chosenAgentName}) says nothing about what it does. * The task faces' split lives in `runningAgentFooterLane` (RB-332). */ export declare function runningBackgroundAgentLabelsLane(core: DurableAgentCore, access: TaskAccess): string[]; /** * The not-found FOOTER's two rows, split the way the reference assembles them: * - `named` = CC `uG_` (220:467941-467953): the agentNameRegistry's KEYS whose task is a running local * agent, listed as BARE NAMES — the string a retry should pass verbatim. sema's counterpart * of "has a registry entry" is the handle carrying a CHOSEN name (the spawn-time label; the * name an agent carries only because every agent has one is not a registry entry — * {@link chosenAgentName}). * - `background` = CC `rxo` (220:467937-467951): the remaining running background agents as `id (label)` * rows. CC filters `!n.has(i.id)` — a NAMED agent is deliberately NOT repeated here, because * its addressable form is the name, not the id. The pre-RB-332 sema code merged both legs * into one list and justified it with "named has no counterpart", a claim [c209-D] retired * when the explicit `name` field landed. The id stays in these rows because the task faces * resolve exact ids: an agent nobody named is the one a retry can reach by its id alone. * CC's `YEd` "Running teammates:" leg has no sema counterpart (no teammate registry on this face) and is * honestly absent rather than faked. */ export declare function runningAgentFooterLane(core: DurableAgentCore, access: TaskAccess): { named: string[]; background: string[]; }; /** The not-found TAIL both task faces share (CC `rxo`, appended by `uMs` for TaskStop; * the host poll's not-found carries the same tail). Empty string when the caller has nothing running. */ export declare function notFoundRunningAgentsTail(footer: { named: string[]; background: string[]; }): string; /** design/151 S1a (RB-425) — the SINGLE construction of a background-agent row's poll `details`. The * live in-process poll ({@link pollBackgroundAgentLane}) and the cross-restart durable fallback * ({@link serveDurableAgentRowLane}) must serve the IDENTICAL field set: that symmetry IS the S1a * contract ("a consumer cannot tell a fallback read from a live one except by the row being older than * the process"), and two hand-maintained object literals had already drifted — `error` was live-only, * so a structured consumer lost the failure FACT at exactly the moment it could no longer reach the * live handle, left to scrape the prose body's `error:` line. * * The fact gates live here too (failed ⇒ error/errorCode/retryable/retryAfterMs; `stoppedBy` rides * whenever the ROW carries it, on any status — a reaper-settled `failed` row keeps its attribution, * see the widening note in the builder) rather than at the call sites: which facts a given row may * carry is part of the same contract, and a gate copied per face is the same drift with extra steps. `error` is model/provider-influenceable text, so * the fencing + bounding (RB-386②'s posture) happens once, here, for both faces. * The parked projection is this builder with fewer facts, not a third literal. */ export interface AgentPollDetailsInput { taskId: string; status: UnifiedTaskOutput["status"]; retrievalStatus: TaskRetrievalStatus; seq?: number; stoppedBy?: StopSource; error?: string; errorCode?: string; errorRetryable?: boolean; errorRetryAfterMs?: number; resultIsPartial?: boolean; completionId?: string; /** The row's ending (`MemberOutcome`), when it states one — rides as `outcome`. */ outcome?: MemberOutcome; /** The agent's current state ({@link import("./agent-state.js").readAgentState}) — rides as * `agentState` when given; the poll arms always give it, a serving layer may omit it. */ agentState?: AgentState; } /** A live handle's ending, read the way a stored row's is ({@link agentOutcomeOf} over the handle's own words, taken at one * synchronous instant) — the poll's live arm and the registry's listing read it here. `undefined` while running / parked. */ export declare function agentHandleOutcomeOf(handle: Pick<BackgroundAgentTaskHandle, "status" | "errorCode" | "stoppedBy" | "outcome">): MemberOutcome | undefined; /** * The ONE reading of a delegation outcome's tool-result error bit: a delegation whose child FAILED produced no usable result, * so the tool result that reports it is an error (`isError: true`) — on the synchronous card, the fork card, the live poll * and the durable poll alike, so a hook or a host reading the bit never sees `false` beside `status: failed`. Every other * outcome word answers `false` here: a completed, killed or still-running delegation is data, and the refusals that never * start a child (a refused call, a held park, an unavailable isolation) are their own constructors, not outcomes. */ export declare function agentOutcomeIsError(status: string): boolean; /** PUBLIC (see the doc above): a serving layer that renders its own protocol projection of a row * builds the structured facts HERE, so the status gates and the untrusted-text fencing apply to it * too — a third literal is exactly the drift this builder exists to prevent. Input only; it reads no * store and authorizes nothing, so the caller must already have passed the row through * {@link import("./background-agent-store.js").canAccessAgentRecord}. */ export declare function buildAgentPollDetails(input: AgentPollDetailsInput): UnifiedTaskOutput; /** design/151 S1a — serve a TERMINAL durable row in the exact pollBackgroundAgent terminal shape * (same body grammar, same details fields — {@link buildAgentPollDetails} is the shared source of * the latter), so a consumer cannot tell a fallback read from a live one except by the row being * older than the process. * * PUBLIC: a serving layer answering for a row whose writing process is gone renders it through THIS, * not through a hand-written projection that would drift from the live poll's. It takes a row the * caller has ALREADY read and authorized — it reads no store and applies no predicate, so a * deployment passes every row through * {@link import("./background-agent-store.js").canAccessAgentRecord} before it gets here. */ export declare function serveDurableAgentRowLane(row: BackgroundAgentRecord, /** The row's current state ({@link import("./agent-state.js").readAgentState} over * {@link import("./agent-state.js").agentStateSourceOfRecord}(row)): rendered as the `state:` line and * carried as `details.agentState`. Absent ⇒ neither appears (a serving layer that does not read it). */ agentState?: AgentState): UnifiedTaskResult; export declare function pollBackgroundAgentLane(handle: BackgroundAgentTaskHandle, deadline?: number, signal?: AbortSignal, /** The run's checkpoint seat — the store a parked row's park card is read from * ({@link import("./agent-state.js").readAgentState}). Absent ⇒ a parked row reads `park unreadable`. */ checkpointSeat?: CheckpointSeat): Promise<UnifiedTaskResult>; export declare function stopBackgroundAgentLane(core: DurableAgentCore, handle: BackgroundAgentTaskHandle, source?: StopSource): Promise<UnifiedTaskResult>;