@sema-agent/core
Version:
Stateless, task-oriented AI agent core
94 lines (93 loc) • 7.07 kB
TypeScript
/**
* The two rule LANES' answers — the org overlay's and the person's persisted rules' — screened and read for one
* call, shared by every consumer that judges a call against them (the live gate's lane station, the resumed
* execution boundary, the synthetic-Read judge). One normalizer and one bounded read per lane: a second copy of
* either would be a second answer to "what did the lane say", and the gate's fail-closed (org) / skip
* (persisted store) readings would drift apart between consumers.
*/
import type { ToolCallRequest } from "./tool-policy.js";
import type { OrgGateVerdict, PersistedRuleAnswer, PersistedRuleHit } from "./hooks.js";
import { type RuleStoreFault } from "./rule-store-fault.js";
/**
* design/252 review r3 — read a foreign {@link OrgGateVerdict} the way the personal-rule answer is read:
* OWN DATA properties only, never the prototype chain, never an accessor.
*
* Unlike the personal-rule normalizer below, this one accepts ANY non-array object as the carrier —
* not only a plain record. The two seams tighten in opposite directions when a shape is refused: the
* personal lane is a LOOSENING seam, so refusing a class instance degrades toward asking; this answer
* carries the org's DENY, and folding a structurally valid `{status:"available", verdict:{behavior:
* "deny"}}` class instance into `unavailable` would LOOSEN it — from a deny nobody can approve into a
* real-approval ask a person can clear. A class instance's fields are its own data properties, so the
* own-data read below already gives the full pollution guarantee (inherited members never authorize);
* the prototype test added nothing here but the downgrade.
*
* The two seams are the same class of trust boundary and were not being read the same way. What that
* cost here is worse than on the personal lane, because this answer is GOVERNANCE:
* · an inherited `revision` (a polluted `Object.prototype`) stamped a fabricated snapshot version onto
* a human's approval request — a WRONG record, which is worse than an absent one;
* · an inherited `verdict` invented an org rule, and with it an org ask or deny the lane never gave;
* · an answer that is not a record at all read as "available, nothing to say", i.e. the governance
* fail-open the availability contract exists to prevent.
* Anything this function cannot read as a well-formed answer becomes `unavailable` — the fail-CLOSED
* word, never the empty one.
*/
export declare function normalizeOrgGateVerdict(answer: unknown, unreadable: string): OrgGateVerdict;
/**
* Normalize the accepted {@link ToolGateInput.persistedRules} answers into one reading.
*
* Four readings, and the lane's own words pick them. `{}` = the lane SAID no rule speaks: the `undefined` answer
* (the store was read and holds nothing for this call) or a coverage answer (design/375 §5.2②, whose decision
* meaning is "no rule"). `{ hit }` = a match: a `behavior` of the closed set and a NON-EMPTY `rules` whose members each
* carry a text (design/375). `{ unreadable: true }` = the lane said it could not read its source (the store form) — with
* `hit` beside it when a readable ask row reaches the call too (the answer's `ask`, read as a hit is: the call carries both).
* `{ unreadable: true, reason }` = the lane could not read the call against its rows (its own call form), OR the lane
* answered something this seam cannot read — `null`, a string or number, an object that is none of the shapes above, a
* `behavior` outside the closed set or absent, `rules` absent / not an array / empty, a member that is not a record or
* whose `rule` is not a string, a carrier that is not a plain object. The last arm is the one that matters: an answer the
* lane gave and nobody can read may be a DENY or an ASK row that did not survive the seam, and reading it as "no rule"
* would let the person's standing "never run this" fail silently — so it is read as the unreadable word (a
* real-approval ask no mode releases; headless, a refusal), the same word the org lane's unreadable answer takes.
* All-or-nothing across the set for the same reason: a set with one unreadable member is a different (smaller) claim than
* the lane made, and a decision must not stand on a claim nobody made. Per-member DOTS stay individually optional
* (identity lost ⇒ `"not_reported"`, exactly the single-rule contract).
*
* The hit's dots are COPIED, not aliased. The array travels onto an ask that may sit in front of a
* person for a long time; a lane that retains and mutates its own array would otherwise change what the
* approver is looking at, and what an audit later reads, after the evidence was stamped.
*/
export declare function normalizePersistedRuleHit(hit: PersistedRuleAnswer): {
hit?: PersistedRuleHit;
unreadable?: true;
reason?: string;
faults?: readonly RuleStoreFault[];
coverage?: readonly import("./permission-rule-model.js").SegmentCoverage[];
};
/**
* The ORG lane's one read for one call — every consumer's (the live gate's org layer, the synthetic-Read
* judge). The CALL is inside the boundary (a synchronous throw — a non-function face, a revoked proxy —
* happens before any `.catch` on its result could be installed), it is invoked directly (a lane that reads
* its own state before its first `await` sees the same turn), it is BOUNDED by the dependency timeout and
* the signal, and everything that is not a well-formed answer — a throw, a hang, a garbage value — reads
* as `unavailable`, the fail-closed word, never as "no org rule spoke".
*/
export declare function readOrgRuleLane(org: {
adjudicate: (req: ToolCallRequest) => Promise<OrgGateVerdict>;
}, req: ToolCallRequest, signal: AbortSignal | undefined, timeoutMs?: number): Promise<OrgGateVerdict>;
/**
* The persisted-rule lane's one read for one call — every consumer's (the live gate, the synthetic-Read
* judge), normalized once. A lane that throws or hangs past the dependency timeout is a lane that could not answer: a row
* nobody could read may forbid the call, so for the DECISION it fails closed (`applyPersistedTightening` reads `{ unreadable }`
* without a reason as the store form: a real-approval ask), and for the RECORD it is `"unavailable"`. A lane that ANSWERS
* something this seam cannot read, and a call the lane could not read against the rows, are `unreadable` with a `reason`
* (the normalizer's words, naming the field): they fail closed in the arms the same way, as the call form. Absent lane,
* or the lane's own `undefined` ⇒ the clean negative.
*/
export declare function readPersistedRuleLane(lane: {
adjudicate: (req: ToolCallRequest) => PersistedRuleAnswer | Promise<PersistedRuleAnswer>;
} | undefined, req: ToolCallRequest, signal: AbortSignal | undefined, timeoutMs?: number): Promise<{
hit?: PersistedRuleHit;
unreadable?: true;
reason?: string;
faults?: readonly RuleStoreFault[];
coverage?: readonly import("./permission-rule-model.js").SegmentCoverage[];
}>;