UNPKG

@sema-agent/core

Version:

Stateless, task-oriented AI agent core

143 lines (142 loc) • 9.51 kB
import { isLoopbackHost } from "@sema-agent/primitives"; export { isLoopbackHost }; import { type HostReading } from "./host-target.js"; import type { ToolPolicy } from "./tool-policy.js"; /** * Is this host obviously internal — loopback ({@link isLoopbackHost}) / private ranges / link-local incl. cloud metadata / CGNAT / * IPv6 ULA, site-local, unspecified, IPv4-mapped, NAT64? Classifies with `net.isIP` rather than a string-prefix regex (a prefix regex * both under-matches — `::ffff:`, NAT64, CGNAT, `::` — and over-matches any DNS name starting `fc`/`fd`). Numeric IPv4 forms * (decimal/octal/hex) are normalized to dotted-decimal by `URL` before this is called. Checks the LITERAL host: it does NOT resolve * DNS, so a name that resolves to a private address is not caught here (a connection-layer concern). */ export declare function isPrivateHost(host: string): boolean; /** * `RunnerDeps.hostFloor` — the hosts this deployment's structured egress may reach, read by the gate and by every engine executor * that opens a connection (the fetch tool, task images, MCP http servers, A2A peers, WebSearch's SearXNG backend). Entries are host patterns: `example.com` (that * host) or `*.example.com` (every host below it, never itself). */ export interface HostFloorDeclaration { /** Hosts this deployment never reaches. */ readonly deny?: readonly string[]; /** Present ⇒ the ONLY hosts this deployment reaches (`[]` = none, legal and explicit). An entry that names a private address (or * `localhost`) also lifts the private-network floor for it — the deployment named it. */ readonly only?: readonly string[]; } /** Where a host stands on the floor. */ export type HostFloorReach = "denied" | "outside_only" | "admitted"; /** The floor's posture — what the wiring manifest reports (counts, never the entries' text). */ export interface HostFloorPosture { readonly onlyMode: boolean; readonly denyCount: number; readonly onlyCount: number; } /** A compiled host floor (frozen). */ export interface HostFloor { /** The entries in their one spelling (`example.com`, `*.example.com`) — what an enforcement point outside the engine (an egress * proxy, a container policy) is generated from. */ readonly entries: { readonly deny: readonly string[]; readonly only?: readonly string[]; }; readonly posture: HostFloorPosture; /** Where this host (as `hostOfUrl` reads it) stands: a `deny` entry reaches it ⇒ `denied`; an `only` list that reaches it not ⇒ * `outside_only`; else `admitted`. */ reach(host: string): HostFloorReach; /** Does an `only` entry name this host — the one way a private host becomes reachable? */ lifts(host: string): boolean; } /** The code a host floor that does not read refuses the prepare with (and a resume before its claim). */ export declare const HOST_FLOOR_INVALID_CODE = "config.host_floor_invalid"; /** * The declaration as the engine holds it: read ONCE through the engine's declaration receipt (`DECLARATION_RECEIPT_MODE`: every own * member off its captured descriptor, a getter called once, every level copied and frozen; a Proxy at any level, a non-plain object — * a `Map`, a class instance, an object of another prototype — and anything that is not data refused). A frozen copy received again is * the same data, so the Runner's construction (which keeps this copy) and every prepare after it read one declaration. */ export declare function receiveHostFloor(declared: unknown): unknown; /** * Compile `RunnerDeps.hostFloor` once (absent ⇒ undefined: no floor), from the declaration's receipt ({@link receiveHostFloor}). A * declaration that does not read — not plain data, not a record, a member other than `deny` / `only`, a member that is not a list, * an entry that is no host pattern, an `only` entry every host of which a `deny` entry also reaches (it could never admit anything) * — throws {@link HOST_FLOOR_INVALID_CODE}, naming the member and entry. */ export declare function compileHostFloor(declared: HostFloorDeclaration | undefined): HostFloor | undefined; /** The floor's half of the judge, on a host reading: a host it does not admit, or no host it can read, is refused (the sentence); * undefined when the floor admits it. The gate's narrowing layer reads this half alone (the private-network floor is the * connecting executor's). */ export declare function floorRefusal(reading: HostReading, floor: HostFloor): string | undefined; /** * The gate's reader of the floor — a deny-only narrowing layer core owns (no rule, mode or approver clears it): a call whose URL slot * (`callHostTargetOf`, the protective reading) names a host the floor does not admit, or names no host it can read, is refused before * anything else is asked; a call with no URL slot is not this layer's. Composed by the policy chain like the other narrowing layers, so * it also re-judges a rewritten call and an approved call on resume. */ export declare function hostFloorPolicy(floor: HostFloor): ToolPolicy; /** Which arm of {@link hopRefusal} refused a URL, carried non-enumerably on errors minted by {@link peerRequestRefusal}. */ export type HopRefusalKind = "unsupported_scheme" | "unreadable_host" | "host_floor" | "private_host"; /** * THE judge of one egress hop, before it is requested: the URL must be http(s) and name a host the rules can read (`hostOfUrl`), the * deployment's floor must admit that host, and — for an executor that holds the private-network floor (`privateFloor`: the fetch * tool and task images) — a private host must be one an `only` entry names. Returns the refusal sentence, or undefined. */ export declare function hopRefusal(url: URL, floor: HostFloor | undefined, privateFloor: boolean): string | undefined; /** The code a protocol peer's request is refused with when the hop judge refuses its URL — the declared endpoint at a dial, an A2A * card's endpoint, a redirect target (a `config.*` word: the deployment named a peer its own floor does not admit — and, floor or * none, a peer's redirect that names credentials or whose Location does not read gets it too). */ export declare const HOST_FLOOR_REFUSED_CODE = "config.host_floor_refused"; /** The producer-reported arm of {@link hopRefusal} carried by a {@link peerRequestRefusal} error, or `undefined`. */ export declare function hopRefusalKindOf(err: unknown): HopRefusalKind | undefined; /** The refusal a protocol request carries when {@link hopRefusal} refuses its URL (no private-network floor: an MCP endpoint and an * A2A peer's declared URL are the deployment's own declaration; an A2A card's endpoint and a redirect target are the peer's words and get none * today). `what` names the request (`mcp: server "x"`, `a2a: peer "y"`). */ export declare function peerRequestRefusal(what: string, url: URL, floor: HostFloor | undefined): (Error & { code: string; }) | undefined; /** A hop judge's refusal: its sentence, or its sentence and the code the request is refused with when that is not the host floor's. */ export type HopRefused = string | { readonly reason: string; readonly code: string; }; /** How a transport follows redirects: its hop judge, whether it takes a judged redirect, and its hop budget. */ export interface FollowLaw { /** The hop judge (each transport binds {@link hopRefusal} to its floor and its private-network posture; A2A asks the revocation ledger first). */ readonly refusal: (url: URL) => HopRefused | undefined; /** Whether a redirect the judge admitted is followed (else it is handed back as `unfollowed`). */ readonly follows: (from: URL, to: URL) => boolean; /** Redirects followed at most (the request after the last one is refused as `too_many`). */ readonly maxRedirects: number; /** Every hop's response, 3xx included, as it arrives. */ readonly onResponse?: (res: Response) => void; } /** One request's outcome under a {@link FollowLaw}. A `refused` outcome says whether an earlier hop of the request was already sent * (`afterSentHop`: the refusal stopped a redirect, not the request — what that earlier hop did is the peer's). */ export type FollowOutcome = { readonly kind: "answered"; readonly res: Response; } | { readonly kind: "refused"; readonly reason: string; readonly code?: string; readonly afterSentHop: boolean; } | { readonly kind: "unfollowed"; readonly res: Response; readonly from: URL; readonly to: URL; } | { readonly kind: "too_many"; }; /** The request init a followed hop is sent with. */ export interface FollowInit { readonly method: string; readonly headers: Readonly<Record<string, string>>; readonly body?: string; readonly signal?: AbortSignal; } /** * ONE request, following redirects by hand: every hop is judged ({@link FollowLaw.refusal}) BEFORE it is requested — the first URL * and every redirect target — so a hop the judge refuses is never contacted; a judged redirect the transport does not take is handed * back with its response; a status outside the fetch standard's redirect set (301, 302, 303, 307, 308), or one of them without a * Location, is the answer. A Location that does not parse is a refusal. A redirect's response that is followed or refused is released. */ export declare function fetchFollowing(doFetch: typeof fetch, start: URL, init: FollowInit, law: FollowLaw): Promise<FollowOutcome>;