UNPKG

@sema-agent/core

Version:

Stateless, task-oriented AI agent core

241 lines (240 loc) • 20.1 kB
/** * The synthetic-call judge — "would this call be refused outright?", asked without a model call and without an * approver. One judge, one set of layers, for every consumer that needs the answer for a file the * model did not itself ask to touch. The layer walk below is written once; two independent choices make a face — the probed * tool (`Read` / `Write`) and which asks the walk holds as an objection (only mandated ones / every one): * * READ — a `Read` of a file the model did not ask for (the changed-file notice; a user's attachment). An ordinary * `ask` — a person's own ask rule — is not a refusal: the person who would answer it is the one asking. * SERVED READ — a `Read`, every `ask` held: would the model's own `Read` of this path be served without anyone being asked? * (a write tool's approval card showing the target's current contents: nobody asked for that read, so a * person's own ask on it is an objection like any other). The one face that may also hear the leg's own * `classic.PreToolUse` hook (a seated {@link SyntheticReadLayers.screening}). * WRITE — a `Write` of a path the engine itself writes in the background (the memory consolidation run). Every `ask` * is an objection here: nobody is present to answer it, so a layer that would ask a person refuses the write. * FETCH — a `WebFetch` of `https://<host>/`, every `ask` held with its classes, a layer that could not answer outranking a * held ask: what the run's rules say about reaching a host for a question that is no model's call (a sandboxed * command's connection to a host its sandbox does not list). * * It never picks a direction. The read face answers in FOUR states: * * deny — some layer refuses it; * mandated_ask — no layer refuses it, and some layer would put the read to a person no rule may answer for * (the ask's own mandate reading, {@link ruleClearanceOf}: a real-approval demand, the deployment's * own question, a hook's; an organization rule's ask; an ancestor's, save one a person's own stored allow * row clears on a leg in its tree's rule session) — the person who must answer is not necessarily the one asking; * no_objection — every layer it consulted answered and none objected; * unanswerable — no layer objected, and at least one could not answer (see the table below). * * The served-read and write faces answer the same four, with `ask` in place of `mandated_ask`: any layer's ask, mandated or not. * * ONE PRECEDENCE on every face: a deny anywhere wins over a held ask, and a held ask over an unanswerable layer; the walk * goes on past an ask (a later layer's deny is the stronger answer). The faces differ only in the probed tool and which asks are held. The * CONSUMER decides where `mandated_ask` and `unanswerable` go: a notice that merely tells the model a file changed reads * both as no objection (the worst case is a notice about a file it then cannot re-read); a face that would put a file's * contents in front of the model reads both as a refusal; a background write reads everything but `no_objection` as a * refusal. Every answer but `unanswerable` names the location the layers judged (`judged`, the canonical view of the * stamped target, when a stamp was taken), so a consumer that resolved the file itself can tell the judged file from the * one it opens. * * THE LAYERS (every one that can refuse a model's own call): the run's skill deny rows; the * deny-narrowing fold (session rules, inherited ancestor rules, frozen constraint projections); the * deployment's caller policy; each live inherited parent policy; the person's persisted-rule lane; the org * lane; and, on a served-read judge that seats it, the leg's own `classic.PreToolUse` screening (`hooks`, {@link SyntheticScreening}): * the model's own `Read` meets it, so "served with nobody asked" hears it. A leg seats it on the judge a write card's target and a * report's files ask (an open leg); not on the instruction-file lane's, which first loads while the leg is prepared, before its * hooks' session opens, nor on the read-first waiver's, asked before the gate has run the hooks on the write. Not consulted: `tool.check`, an inherited installation's * screening, and the screening on the other faces (not in this reading yet); by design, the permission mode's write * gate, the integrity guards and the write-protection table (a live `Write` meets them; the read face never needs them and the * write face's background run is not asked), and the read-deny table and containment roots (the path's own resolution judges those first). * * FAULT → STATE (the live gate's own readings, through the same functions — never a second copy): * | layer | fault | state | * | skill rows | throws | unanswerable `threw` | * | narrowing fold | throws / exceeds the judgment's budget | unanswerable `threw`/`timed_out` | * | caller / inherited policy | throws / times out | unanswerable `threw`/`timed_out` | * | caller / inherited policy | its deny carries an engine settlement (an approval | unanswerable `approval_seat` | * | | seat that ended unanswered; in a combinePolicies | | * | | composition a rule deny anywhere is returned instead)| | * | persisted lane | a store partition (or all of it) is unreadable, the lane throws, times out (readPersistedRuleLane ⇒ `{unreadable}`) | unanswerable `store_unreadable` (both faces: a row nobody could read may forbid the call) | * | persisted lane | the call cannot be read against the rows (`reason`) — applyPersistedTightening ⇒ `unreadable` | unanswerable `call_unreadable` | * | org lane | unavailable: throws, garbage (readOrgRuleLane) | unanswerable `org_unavailable` | * | any policy layer | answers with a rewrite of the arguments (`updatedInput`) | unanswerable `rewritten` | * | inherited parent policy | answers `ask` (the ancestor's approver's question) that no person's stored allow row clears — on a leg in its tree's rule session the persisted lane is heard first, as at its stations | mandated_ask (write: ask) | * | hooks (served-read face) | the screening failed (a handler threw, ran past its budget, answered out of form) / was cut | unanswerable `threw`/`timed_out` | * | hooks (served-read face) | answers with a rewrite of the arguments | unanswerable `rewritten` | * | any layer | the judgment's budget (one dependency timeout, or a | unanswerable `timed_out` | * | | batch's shared deadline) is spent | | * * NEVER AN APPROVER. Every probe STATES that nobody can be asked about it — `approvalReach: "none"` on the request * (`ToolCallRequest.approvalReach`) — and every policy layer is handed a LIVE signal of its own: it ends when the judge stops * waiting for that layer (the layer answered, or its share of the judgment's budget is spent) or when the run's signal ends. * An abort means only that: the task ended, or the judge no longer waits — never "there is no approver". Every engine * policy factory reads the statement before reaching a seat (the approval factory answers a deny attested `no_approver`, * read here as `approval_seat`; an inherited screening installation is no policy layer here, as a leg's own PreToolUse is not), and the * engine's own guards composed into a deployment's policy judge the probe as they judge a call — a lookup bounded by a * live deadline answers. A deny that carries no engine settlement is a rule's deny and stands. RESIDUALS (stated, not * closed): a deployment closure that ignores the statement and reaches an approver IS asked — the judge waits for it at * most the judgment's budget (`timed_out`: the write face refuses, the notice face stays silent), then its signal ends * so a closure that honors it releases its approval; a deployment wrapper that answers the statement with a deny of its * own (no settlement) is read as a deny — the attachment face then withholds, the notice face stays silent. * * THE PROBE'S IDENTITY. Every layer sees the probed tool's name (`Read` / `Write`), the probed arguments, the statement * above, and a stable synthetic call id (`<face>-visibility-probe` for a read, `<face>-write-probe` for a write; * {@link syntheticReadProbeId}, {@link syntheticWriteProbeId}), so a deployment's audit can tell a probe from a model's call. The write face stamps the * facts the live gate stamps on a write (the roster's `Write` face, the tracked cwd and root, the advertised writable * directories, the data root's exemption): a containment fence and a data-root exemption judge the probe as they judge the call. */ import type { CallFacts, ToolCallRequest, ToolCallFace, ToolPolicy } from "./tool-policy.js"; import { type CallTargetStamp } from "./effective-path-target.js"; import { type AskClass } from "./ask-origin.js"; import { type SkillGrantLane } from "./skill-grant.js"; import { type HookEnvCapabilities, type HookInvocationIdentity, type OrgGateVerdict, type PersistedRuleAnswer } from "./hooks.js"; import type { PreToolUseScreen } from "./runner/gate-exit.js"; import type { LegModBus } from "./mods/stations.js"; import type { PermissionMode } from "./ask-origin.js"; import type { CwdRef } from "../tools/fs/fs-shared.js"; /** The layers the judge consults, as named in its verdicts. */ export declare const SYNTHETIC_READ_LAYERS: readonly ["skill_rows", "narrowing", "caller_policy", "inherited_constraint", "persisted_rules", "org_rules", "hooks"]; export type SyntheticReadLayer = (typeof SYNTHETIC_READ_LAYERS)[number]; /** Why a layer could not answer — the consumer's disclosure word. */ export declare const SYNTHETIC_READ_UNANSWERABLE_CAUSES: readonly ["threw", "timed_out", "approval_seat", "call_unreadable", "org_unavailable", "rewritten", "store_unreadable"]; export type SyntheticReadUnanswerableCause = (typeof SYNTHETIC_READ_UNANSWERABLE_CAUSES)[number]; export type SyntheticReadVerdict = { verdict: "deny"; layer: SyntheticReadLayer; message?: string; judged?: string; } | { verdict: "mandated_ask"; layer: SyntheticReadLayer; message?: string; judged?: string; } | { verdict: "no_objection"; judged?: string; } | { verdict: "unanswerable"; layer: SyntheticReadLayer; cause: SyntheticReadUnanswerableCause; reason: string; }; /** The write face's answer: the read face's states with `ask` — any layer's ask, mandated or not — in place of `mandated_ask`. */ export type SyntheticWriteVerdict = Exclude<SyntheticReadVerdict, { verdict: "mandated_ask"; }> | { verdict: "ask"; layer: SyntheticReadLayer; message?: string; judged?: string; }; export interface SyntheticReadLayers { skillGrants?: Pick<SkillGrantLane, "denying">; denyNarrowingPolicy?: ToolPolicy; callerPolicy?: ToolPolicy; inheritedPolicies?: readonly ToolPolicy[]; /** The leg is in its delegation tree's rule session ({@link import("./leg-identity.js").ruleSessionOf}`.inTree`, stated by the leg's assembly): * its stations put an inherited policy's ask to the person's rule lane first, so a person's stored allow row may clear that ask here too. * Absent ⇒ the ask is the ancestor's, at its own approver (a leg outside its tree, and a layer set no leg assembled). */ inTreeRuleSession?: true; persistedRuleLane?: { adjudicate: (req: ToolCallRequest) => PersistedRuleAnswer | Promise<PersistedRuleAnswer>; }; orgRuleLane?: { adjudicate: (req: ToolCallRequest) => Promise<OrgGateVerdict>; }; /** The leg's own `classic.PreToolUse` screening of the probe ({@link SyntheticScreening}). Seated on a served-read judge only, on an open leg. */ screening?: SyntheticScreening; /** The leg's target-reading stamp: the probe's request carries a reading of its target, as the live gate's does. */ stampCallTarget?: CallTargetStamp; /** The run's signal: its end ends the judgment's every wait (each policy layer's own signal ends with it). */ signal?: AbortSignal; } /** The leg's own `classic.PreToolUse` screening of a probe, as the model's own call of the probed tool meets it at the live gate's * first phase: what the screen came to, or undefined when no handler hangs on the event (nothing is raised). `signal` ends when * the judge stops waiting for it. */ export type SyntheticScreening = (probe: ToolCallRequest, signal: AbortSignal) => Promise<PreToolUseScreen | undefined>; /** A leg's {@link SyntheticScreening}: `classic.PreToolUse` raised on the leg's own bus under its identity, its observed mode, its * tracked cwd and its hooks' environment face — the live gate's raise — with no hook-progress frame (no model call is screened) and * none of the inherited installations. `leg` is read at each probe; a leg not yet prepared cannot say (a refused screen). */ export declare function legScreeningOf(leg: () => { readonly modBus: LegModBus; readonly hookIdentity: HookInvocationIdentity; readonly permissionMode: { readonly observed: PermissionMode; }; } | undefined, cwd: CwdRef | undefined, env: HookEnvCapabilities | undefined): SyntheticScreening; /** The arguments a synthetic `Read` carries. The request's other facts (the tracked cwd a relative path resolves against, the root, the * roster's `Read` face, the budget snapshot, the advertised and exempt roots) come from the judge's {@link SyntheticCallFacts}. */ export interface SyntheticReadCall { file_path: string; offset?: number; limit?: number; } /** The path a synthetic `Write` probes. The request's other facts come from the judge's {@link SyntheticWriteFacts}. */ export interface SyntheticWriteCall { file_path: string; } /** The facts the live gate stamps on a write, read at the moment of each judgment ({@link CallFacts} less the budget: a background * write runs once the run has ended, past every leg's budget snapshot). */ export type SyntheticWriteFacts = Omit<CallFacts, "budget">; /** The facts the live gate stamps on any call it judges — a background write's ({@link SyntheticWriteFacts}) and the leg's budget * snapshot (`ToolCallRequest.budget`). A probe carries them through {@link callFactsSeat}, the reader the live gate's own request * goes through, so a layer judges it as it judges the probed tool's own call; `face` is the probed tool's roster face. */ export type SyntheticCallFacts = CallFacts; /** The stable synthetic call id a probe carries: `<face>-visibility-probe`, `face` a lowercase word * (letters and hyphens) naming the consumer. */ export declare function syntheticReadProbeId(face: string): string; /** The write face's stable synthetic call id: `<face>-write-probe`, `face` the same lowercase word. */ export declare function syntheticWriteProbeId(face: string): string; /** One judgment's wait budget. `deadlineAt` (epoch ms) bounds the WHOLE judgment — every layer waits at most * what is left of it, and a layer reached after it has passed is not consulted (`timed_out`). Absent ⇒ the * dependency timeout from the start of this judgment. A consumer judging a batch passes one shared deadline, * so a hung layer costs the batch one bound, not one per file per layer. */ export interface SyntheticReadBudget { deadlineAt?: number; /** The consumer's own wait (a tool call's signal): its end ends the leg's hook screening too, as the run's end does. */ signal?: AbortSignal; } export type SyntheticReadJudge = (call: SyntheticReadCall, face: string, budget?: SyntheticReadBudget) => Promise<SyntheticReadVerdict>; /** The path a served-read probe reads, and the face the run's roster gives `Read` (the face the model's own `Read` carries) — * absent on a roster that mounts no `Read`: the probe then carries no face, as the read face's does, and every layer judges it as * it judges a request with none (a deny-list reads the catalog's slot for the name, tighten-only). The request's other facts come * from the judge's {@link SyntheticCallFacts}. */ export interface SyntheticServedReadCall { file_path: string; face?: ToolCallFace; } export type SyntheticServedReadJudge = (call: SyntheticServedReadCall, face: string, budget?: SyntheticReadBudget) => Promise<SyntheticWriteVerdict>; export type SyntheticWriteJudge = (call: SyntheticWriteCall, face: string, budget?: SyntheticReadBudget) => Promise<SyntheticWriteVerdict>; /** The READ face: only a mandated ask is held (a person's own ask is that person's question). The probe carries what the model's own * `Read` carries — the leg's facts (`facts`, read at each judgment, the one reading the live gate's request goes through), so a * layer judges the notice of a changed file, a person's attachment and a compaction's restore as it judges the model's own read of * that file. A judge built without `facts` has no leg (a bare layer set): its probe carries none. */ export declare function createSyntheticReadJudge(layers: SyntheticReadLayers, facts?: () => SyntheticCallFacts): SyntheticReadJudge; /** The SERVED-READ face: the `Read` probe with every ask held — `no_objection` only when the model's own `Read` of the path * would be served without anyone being asked. The probe carries what the model's own `Read` carries: the roster's `Read` * face (the call's) and the leg's facts the live gate stamps (`facts`, read at each judgment). */ export declare function createSyntheticServedReadJudge(layers: SyntheticReadLayers, facts: () => Omit<SyntheticCallFacts, "face">): SyntheticServedReadJudge; /** The WRITE face: every ask is held as an objection (no approver is present), and a persisted store that cannot be read is * unanswerable rather than silent. */ export declare function createSyntheticWriteJudge(layers: SyntheticReadLayers, facts: () => SyntheticWriteFacts): SyntheticWriteJudge; /** The fetch face's stable synthetic call id: `<face>-fetch-probe`, `face` the same lowercase word. */ export declare function syntheticFetchProbeId(face: string): string; /** The FETCH face's answer: a deny, or a layer that could not answer (which outranks a held ask here — a layer nobody could read may * forbid the host); otherwise every ask held, with the classes of every ask the layers held (none: empty), whether one of them * demands a real approval, and the probe the layers judged — the request a consumer reads the run's allow members and the card's * offer on, so every reading is of one call. */ export type SyntheticFetchVerdict = Extract<SyntheticWriteVerdict, { verdict: "deny" | "unanswerable"; }> | { verdict: "ask" | "no_objection"; asks: readonly AskClass[]; realApproval: boolean; probe: ToolCallRequest; }; export type SyntheticFetchJudge = (host: string, face: string, budget?: SyntheticReadBudget) => Promise<SyntheticFetchVerdict>; /** The FETCH face: the run's own `WebFetch` of `https://<host>/`, every ask held — what the run's rule layers say about reaching a * host, for a question about a host that is no model's call (a sandboxed command's connection). The probe carries the face the * run's roster gives `WebFetch` and the leg's facts (`facts`, read at each judgment). */ export declare function createSyntheticFetchJudge(layers: SyntheticReadLayers, facts: () => SyntheticCallFacts): SyntheticFetchJudge;