@sema-agent/core
Version:
Stateless, task-oriented AI agent core
146 lines (145 loc) • 9.49 kB
TypeScript
/**
* The ONE exit for an auto-mode classification that did not RULE ({@link rungDidNotRule}: the classifier leg failed or its
* budget ran out — `error` / `timeout` — or it replied with no verdict at all — `parse_error`) — shared by both classifier
* stations, the leg's gate and an ancestor's frozen classifier in the policy fold, so the rule is written once.
*
* ONE fork, on the APPROVAL ROUTE the station states (`ApprovalRoute`, the one seat decision — `live_approver`,
* `policy_seat`, `durable_park` or `none`): with a decider on the route, the ask goes on carrying why (`classifierNoRuling`, its
* text saying it too), and that seat settles it — a person's refusal counts nothing (it is a person's), a deployment policy's
* refusal counts on the denial limit ({@link settleNoRulingAsk}); without one, an unavailable classification is refused with CC's `x1t`
* sentence (its FORM, `unavailable`, attested on the deny) and is never counted, while a reply with no verdict goes back to
* the station's ruling path (`counted`): refused with its own sentence and counted on the denial limit, so the bound falls
* back to a person (CC: `Cr` excludes unavailable; failureMode is counted). The handed-on ask carries `requiresRealApproval`:
* no automatic lane may answer it in the classifier's stead — not a blanket `onAsk:"allow"`, not an allow from an approver the
* deployment declared a policy, not the sandbox admission, not a budget resolver over its park. Asking where a person can be asked is a declared departure (CC refuses).
*
* The observer hears every such classification with where the call went, except while the call is being aborted — a halt,
* or the run's end. It is observe-only: a throw never changes the arm.
*
* The two delegation sites (a sub-agent's spawn, its hand-back) read the same predicate and tell the same observer, but go
* ahead without a ruling ({@link readDelegationReview}); the child's own gated calls are still judged one by one.
*/
import { type AutoModeClassifierRound, type AutoModeDenialTracker, type AutoModeFallbackCause, type AutoModeVerdict, type UnarmedDenialLimitFallback } from "./auto-mode.js";
import { type PermissionResult, type ResolvedAsk, type ToolCallRequest } from "./tool-policy.js";
import { type Settlement } from "./gate-outcome.js";
import { type ApprovalRoute } from "./approval-route.js";
/** The closed set behind the `classifier.unavailable` notice's `detail.site`: `call` = a per-call station (the leg's gate,
* or an ancestor's frozen classifier); `spawn` = a sub-agent's prompt and tools before it starts; `handback` = the work a
* sub-agent hands back. */
export declare const CLASSIFIER_REVIEW_SITES: readonly ["call", "spawn", "handback"];
export type ClassifierReviewSite = (typeof CLASSIFIER_REVIEW_SITES)[number];
/** The two delegation sites ({@link readDelegationReview}). */
export type DelegationReviewSite = Exclude<ClassifierReviewSite, "call">;
/** Where a classification that did not rule sent the call: `asked` = on to the approval seat, `denied` = refused — the
* projection of the route ({@link classifierUnavailableDispositionOf}), never a second reading of it — and `proceeded` = a
* delegation site went ahead without a ruling (`site` `spawn` / `handback` only). */
export type ClassifierUnavailableDisposition = "asked" | "denied" | "proceeded";
/** The disposition a route projects: a route with a decider on it is `asked`, `none` is `denied`. */
export declare function classifierUnavailableDispositionOf(route: ApprovalRoute): ClassifierUnavailableDisposition;
/** What the observer receives — the facts the `classifier.unavailable` notice is composed from, one shape per site: `cause`
* ({@link AutoModeFallbackCause}: `error` / `timeout`, or `parse_error` for a reply with no verdict) on both; at `call` (a
* per-call station) the `route` it sent the call on, projected into the detail's disposition; at `spawn` / `handback` (a
* delegation site, which goes ahead) the `subject` — the sub-agent as the user knows it, bounded and escaped by its site,
* for the sentence only. `refusedBySpend` (at `call`): the classification did not run because the spend account refused its
* request — the cause word stays `error` (the closed set does not grow), the sentence says the true reason. */
export type ClassifierUnavailableInfo = {
toolName: string;
toolCallId: string;
cause: AutoModeFallbackCause;
site: "call";
route: ApprovalRoute;
refusedBySpend?: "ceiling" | "closed";
} | {
toolName: string;
toolCallId: string;
cause: AutoModeFallbackCause;
site: DelegationReviewSite;
subject?: string;
};
export type ClassifierUnavailableObserver = (info: ClassifierUnavailableInfo) => void;
/** The station's two facts for the fork: the route an ask it hands on takes (`approvalRouteOf` / `frozenApproverRoute` —
* a value outside the two person routes, or none stated, is `none`), and who hears it. */
export interface ClassifierUnavailableSeat {
route?: ApprovalRoute;
onUnavailable?: ClassifierUnavailableObserver;
}
/** The arm the fork chose: the ask to hand on, the refusal, or — a reply with no verdict and no seat — the station's own
* counted ruling path. */
export type ClassifierNoRulingArm<A> = {
kind: "asked";
ask: A;
} | {
kind: "denied";
deny: PermissionResult;
} | {
kind: "counted";
};
export declare function classifierNoRulingArm<A extends Extract<PermissionResult, {
action: "ask";
}>>(args: {
ask: A;
req: ToolCallRequest;
cause: AutoModeFallbackCause;
/** The ladder's attribution, for the refusal's sentence (it names both rungs when the ladder moved). */
attribution?: AutoModeClassifierRound;
seat: ClassifierUnavailableSeat | undefined;
/** The call is being aborted (its per-call signal — a halt, or the run's end): the observer is not told (the arm is chosen
* as ever). */
aborted: boolean;
/** The classification did not run because the spend account refused its request (`AutoModeVerdict` `refusedBySpend`): the
* arm is chosen as for any classification that could not run; every sentence it writes says the true reason. */
refusedBySpend?: "ceiling" | "closed";
}): ClassifierNoRulingArm<A>;
/** Which delegation steps went ahead without a ruling, each with its cause — the record a delegation's result carries (the
* Agent card, the launch receipt, a background completion frame) beside the notice minted from the same value. Present only
* when a slot is filled; absence never means "reviewed" (a run without auto mode reviews nothing). */
export interface DelegationUnreviewed {
readonly spawn?: AutoModeFallbackCause;
readonly handback?: AutoModeFallbackCause;
}
/** A delegation site's reading: `block` = ruled against (a block verdict, or a reply that answered both ways — a ruling by
* {@link rungDidNotRule}, refused as at the gate; `reason` = the classifier's text, or the engine's parse-failure sentence,
* unsanitized); `proceed` = allowed, or no ruling (`unreviewed` = the cause, present exactly when the user was told). */
export type DelegationReviewReading = {
readonly kind: "proceed";
readonly unreviewed?: AutoModeFallbackCause;
} | {
readonly kind: "block";
readonly reason: string;
readonly category?: string;
};
/**
* The delegation sites' one reading of a classification (a sub-agent's spawn — the Agent tool's or a workflow's — and its
* hand-back). "Did not rule" is {@link rungDidNotRule}; here the delegation goes ahead: the seat's observer is told (site, no
* route, `subject` already bounded and escaped by the caller) and the cause comes back for {@link DelegationUnreviewed} (and,
* at hand-back, the note). A ruling is acted on: a block, and a reply that answered both ways, refuse. While the lane is
* being aborted nothing is told or recorded — a stopped delegation settles on its stop.
*/
export declare function readDelegationReview(args: {
verdict: AutoModeVerdict;
toolName: string;
toolCallId: string;
site: DelegationReviewSite;
subject?: string;
seat: ClassifierUnavailableSeat | undefined;
aborted: boolean;
}): DelegationReviewReading;
/**
* The no-ruling card's settlement (both stations): a card THIS station's no-ruling arm handed on for this call (`mintedHere` —
* a policy's own `classifierNoRuling`, or a card another station minted, is not this station's classification and counts
* nothing here) refused with nobody judging it ({@link SETTLEMENT_NOBODY_JUDGED}: the deployment's policy refused it, a policy's
* allow was refused on the card's real-approval bit, or nobody could be asked) counts on the denial limit like a ruling refusal,
* because "handed to a person" means a person settled it. Returns the fallback the count minted when it reached the
* bound — the station then puts the same call to the same seat as the denial-limit fallback, and a deny nobody judged there
* stops the run through the existing headless arm. A person's answer (allow or refusal), a fallback card, and every other
* settlement count nothing.
*/
export declare function settleNoRulingAsk(opts: {
ask: PermissionResult;
mintedHere: boolean;
resolved: {
action: ResolvedAsk["action"];
settlement?: Settlement;
};
tracker: AutoModeDenialTracker | undefined;
}): UnarmedDenialLimitFallback | undefined;