UNPKG

@sema-agent/core

Version:

Stateless, task-oriented AI agent core

147 lines (146 loc) • 11.7 kB
import type { RunnerDeps } from "../types.js"; import type { ExecutionEnv } from "../../internal/harness.js"; import type { PathRuleBases } from "../permission-rule-model.js"; import type { ChildPlacement, InheritedGate, RunInternals } from "./contracts.js"; import type { ToolPolicy } from "../tool-policy.js"; import { CheckpointError } from "../checkpoint-store.js"; /** * Whether this deployment can place a child agent in a tree OTHER than the caller's — the one fact behind * every request of that shape: an explicit `cwd`, a workflow agent's `isolation: "worktree"`, and the * delegation tool's per-call or per-definition `isolation: "worktree"`. * * Only an `executionEnvFactory` can: it builds one execution env per task, so a child can be rooted * wherever the request says. A static `executionEnv` is ONE shared env with ONE cwd — every child that * runs on it runs in the caller's root, whatever tree it was asked to work in. A request the deployment * cannot honor is refused before the child starts (and, for a worktree, before one is minted); each site * keeps its own code and sentence for the refusal, but none decides the condition itself. */ export declare function deploymentCanRerootChild(deps: Pick<RunnerDeps, "executionEnvFactory">): boolean; /** * Where a child the caller asked to place at `asked` actually landed (`childRoot`, the root its environment gave it) — the * ONE judgment every reader of that fact takes * (the delegation tool's worktree request, and the anchors of the rules the child inherits): * · `honored` — the environment's root is where the child was asked to be (canonical on both sides); * · `caller_tree` — it is not, and it lies inside the caller's own tree (`callerRoot`, or the checkout the caller has switched * to — `callerCheckout`, which can lie outside the root): the same filesystem, the wrong * directory — a worktree request then fails (the child would write the caller's checkout); * · `elsewhere` — neither: another filesystem namespace (a sandbox the factory minted). The child runs there, it is not * in the worktree it was offered, and it takes the caller's path rules again at its own anchors. * A path the environment cannot canonicalize is compared as spelled. Residual: two namespaces that hold the same path * string, with a factory claiming to honor it, read `honored`. */ export declare function childPlacementOf(env: Pick<ExecutionEnv, "canonicalPath">, childRoot: string, asked: string | undefined, callerRoot: string, callerCheckout?: string): Promise<ChildPlacement>; /** `p` as the environment resolves it, or as spelled when it cannot (the placement judgments' one spelling reading). */ export declare function canonicalOrSelf(env: Pick<ExecutionEnv, "canonicalPath">, p: string): Promise<string>; /** * The managed tree a delegated child was placed outside of — `undefined` when nothing binds it there. A caller whose root lies * in a managed tree (a session's own tree, or a worktree the caller itself was started in) works in that tree; a child its * environment did not place where it was asked (`placement` other than `honored`) must still start inside it (at or below the * tree's root, canonical), or it would read and write the checkout the tree isolates. The caller's tree is read on the canonical * spelling of its root (a symlink alias or a `.` segment names the same tree). Asked only on a placed leg (one a delegation * caller placed; any other has no placement). Not bound: a child placed where asked, a caller outside every managed tree, and an * environment that is a declared sandbox off the host's disk ({@link isDeclaredSandboxOffHost} — another namespace, the same * reading the worktree request's guard exempts by). */ export declare function callerTreeMissed(env: ExecutionEnv, placement: ChildPlacement, callerRoot: string, childRoot: string): Promise<string | undefined>; /** The refusal of a child placed outside the managed tree its caller works in ({@link callerTreeMissed}); `envCwd` is named too * when the child's root is not its environment's own directory. */ export declare function factoryIgnoredHint(childRoot: string, envCwd: string, tree: string): Error & { code: "workspace.isolation_unavailable"; }; /** * The placement seats a delegation caller hands its child (`RunInternals.callerRoot` / `callerCheckout`), read from where the * caller works (`parentCwd`) and its task root (`callerRoot`) — the ONE reading every spawn lane takes (the delegation tool and * both workflow lanes). A caller that names only where it works is its own root; the checkout seat is filled only when the caller * has switched to a tree other than its root (it can lie outside that root). Neither named ⇒ no seats: no placement is judged. */ export declare function callerPlacementSeats(parentCwd: string | undefined, callerRoot: string | undefined): Pick<RunInternals, "callerRoot" | "callerCheckout">; /** A delegated leg's placement: the place asked for is the hint (a minted worktree, an explicit cwd, the caller's own cwd) * — or, for a worktree the factory mints itself (`isolation`), that worktree on the caller's host when the environment's * paths are the host's (`pathsAreHostLocal`; any other environment's is another namespace). A leg no delegation caller * placed (no `callerRoot`) has no placement and reads nothing. */ export declare function childPlacementOnLeg(env: ExecutionEnv, internals: Pick<RunInternals, "isolation" | "parentCwd" | "callerCheckout"> & { readonly callerRoot: string; }, root: string): Promise<ChildPlacement>; /** The refusal of a worktree the caller minted for a child its environment placed in the caller's own tree. */ export declare function worktreeNotHonored(cwd: string): Error & { code: "isolation.unavailable"; }; /** * The chain's top leg's anchors as THIS leg reads them (the person's global / session path rows and every `~/` row anchor * there): the chain's own (`live` as recorded, else the checkpoint's `seed` rebased — {@link recordedAnchors}), unless this * leg is a child placed `elsewhere` or no chain names one — then this leg is the top. A recorded root that is not an * absolute path anchors nothing: the rows it would anchor read `unreadable`. */ export declare function chainTopAnchors(cfg: { leg: PathRuleBases; live: InheritedGate["pathRuleAnchors"]; seed: InheritedGate["pathRuleAnchors"]; placement: ChildPlacement | undefined; rebase: (p: string) => string; }): PathRuleBases; /** The installation records a leg reads: the live chain's (as recorded), else the checkpoint's (through the workspace rebase). */ export interface ChainInstallations { readonly live: InheritedGate["pathRuleInstallations"]; readonly seed: InheritedGate["pathRuleInstallations"]; } /** Why no usable record says where a chain entry was installed: no record of that entry (no record at all, or its member * `null`), a record that does not cover the chain position for position, or a member that names no absolute root. */ export type ChainInstallationGap = "absent" | "misaligned" | "invalid"; /** One chain installation gap: the entry, why its record is not usable, and which record was read — the chain handed live * (`chain`) or the checkpoint's (`checkpoint`). */ export interface ChainInstallationGapAt { readonly position: number; readonly gap: ChainInstallationGap; readonly record: "chain" | "checkpoint"; } /** * The chain entries a leg could not install where they were installed: each UNINSTALLED entry (a host re-supplying a * chain, or a resume rebuilding one) whose path rules depend on the installing leg, with no usable record of that leg. Such * a chain is refused — installing those entries anywhere else (the chain's top leg, this leg) would move what they protect. * Entries whose rules do not depend on it, and installed entries, never need a record. */ export declare function chainInstallationGaps(chain: readonly (ToolPolicy | undefined)[], installations: ChainInstallations, rebase: (p: string) => string): ChainInstallationGapAt[]; /** The refusal of a chain with {@link chainInstallationGaps} (`resume.parent_constraint_mismatch`, `detail.reason` naming * the first gap's form); `pending` says the checkpoint is still pending (the refusal came before the claim). */ export declare function chainInstallationRefusal(gaps: ReadonlyArray<ChainInstallationGapAt>, pending: boolean): CheckpointError; /** * How a leg holds a caller policy — its own (`TaskSpec.toolPolicy` / `RunnerDeps.toolPolicy`) or one its delegation chain * hands it: a policy is installed where it ENTERS the chain, and a child holds its ancestor's installation (one object, * the same anchors) — its own caller slot included when that slot names the policy an ancestor installed. Installed again: * on a child placed `elsewhere` (another filesystem namespace — the ancestor's anchors name another namespace's files), at * this leg's anchors; a chain entry handed uninstalled (a host re-supplying a chain), where its installing leg recorded it * (`InheritedGate.pathRuleInstallations`, live or from the checkpoint). An uninstalled entry whose rules depend on that * leg and whose record is not usable refuses the leg ({@link chainInstallationRefusal}); one whose rules do not, reads * the same wherever it is installed (the chain's top leg). */ export declare function chainPolicyOnLeg(cfg: { leg: PathRuleBases; chainTop: PathRuleBases; placement: ChildPlacement | undefined; chain: readonly (ToolPolicy | undefined)[]; installations: ChainInstallations; rebase: (p: string) => string; }): ChainPolicyOnLeg; /** How a leg holds its chain's policies ({@link chainPolicyOnLeg}). */ export interface ChainPolicyOnLeg { /** The leg's own caller policy as it holds it. */ readonly own: (policy: ToolPolicy | undefined) => ToolPolicy | undefined; /** The chain entry at `position` as the leg holds it. */ readonly chained: (policy: ToolPolicy, position: number) => ToolPolicy; /** Where the leg installs a policy it holds at `position` of the chain it hands down (a position past the inherited chain is * one of its own) — whether or not its rules depend on it: `undefined` for a policy an ancestor installed (its own record * says where), else the anchors {@link chained} / {@link own} install it at. The record a park keeps of every layer. */ readonly siteOf: (policy: ToolPolicy, position: number) => PathRuleBases | undefined; } /** The installation record of a chain entry (its root and home), `null` for an entry with no path rules installed. */ export declare function installationRecordOf(policy: ToolPolicy | undefined): { readonly root: string; readonly home?: string; } | null; /** What a leg hands its children about anchors (data half): the chain's top leg's anchors — the person's global / session * rows and every `~/` row anchor there — and where each policy entry handed down is installed, position for position, * whether or not its rules depend on it today (`siteOf`: the leg's own installation rule; a chain re-supplied uninstalled — a * resume's rebuild — is installed there again, so a rule the policy gains later is anchored where the entry was). `null` for a * screening installation and for a site with no root; absent when no entry has a record. */ export declare function chainAnchorRecords(chainTop: PathRuleBases, chain: NonNullable<InheritedGate["parentConstraints"]>, siteOf: ChainPolicyOnLeg["siteOf"]): Pick<InheritedGate, "pathRuleAnchors" | "pathRuleInstallations">;