@sema-agent/core
Version:
Stateless, task-oriented AI agent core
101 lines (100 loc) • 7.74 kB
TypeScript
/**
* PrepareTask's Artifact phase: the `RunnerDeps.artifactHost` seat is read once; an absent seat mounts
* nothing and announces nothing; a seat whose declaration fails an invariant mounts nothing and announces
* `config.artifact_host_invalid` (operator audience) so a bad wiring can never read as "no hosting"; a
* valid seat mounts ONE `Artifact` tool assembled from the declaration, on the same optional band the LSP
* seat uses. The tool's local-file read goes through the run's fs fence (root containment, read-deny, read
* face) exactly as `Read` does, then reads the bytes once from the object at the judged key, as `Read` does. A local stub's store
* (`capabilities.storageDir`) must lie OUTSIDE every root the run may write or read — a ledger the model can
* edit or read through the fs tools is not a trust boundary — judged by {@link rootAdmittingArtifactStore} at
* every point a root is admitted: here over the prepare-time roots (a store that overlaps one refuses the mount,
* naming both), and at an in-flight read-root grant over the granted root (the grant is refused). The phase
* publishes the declared store into the run's {@link ArtifactStoreRef} for that second point.
* Synchronous: the stretch has no await (the file read is a closure the tool calls later).
*/
import type { PermissionModeController } from "../mode-policy.js";
import type { ExecutionEnv } from "../../internal/harness.js";
import { type OnAsk } from "../tool-policy.js";
import { type ArtifactCallBinding, type ArtifactPlane } from "../artifact-host.js";
import { RosterBuilder } from "../tool-roster.js";
import type { AgentTool, RunnerDeps } from "../types.js";
import type { ContainmentFace } from "../../tools/fs/index.js";
import type { PlacedReadDenyMatcher } from "../../tools/fs/read-deny-home.js";
import type { ArtifactStoreRef } from "./read-root-grant.js";
import type { ReadRootsSeat } from "./person-path-rows.js";
import { type ResolvedSharedArtifactReads } from "../shared-artifact-reads.js";
import { type AutoModeDecider, type AutoModeDenialTracker } from "../auto-mode.js";
import type { Prepared } from "./contracts.js";
import type { ToolOccurrenceTable } from "./tool-occurrence.js";
import type { LegNotice } from "../leg-identity.js";
export interface PrepareArtifactInput {
/** borrowed-readonly — the deployment's hosting seat and the notice sink the refusal arm announces on. */
deps: Pick<RunnerDeps, "artifactHost" | "onNotice" | "tracer" | "sharedArtifactReads">;
/** borrowed-readonly — the once-per-session dedupe the `sharedArtifactReads` account rides, and the leg's notice seat it is addressed
* and admitted by (`Prepared.legNotice`; said by whichever leg read the seat — a root leg or a woken one). */
announce: {
readonly oncePerSession: (digest: string, deliver: () => void) => void;
readonly legNotice: Pick<LegNotice, "route" | "admission">;
};
/** borrowed-mutable — the run's one reading of the setting (`PrepareInheritedGateResult.sharedArtifactReadsCell`): a born child's is
* its parent's and is used as it is; otherwise this phase reads the seat once and writes it here, for this leg's children. */
sharedReads: {
current?: ResolvedSharedArtifactReads;
};
/** borrowed-readonly — the engine-minted run id: the host's `onMount` key and every call context's `runId`. */
runId: string;
/** borrowed-readonly — the host task id, the call context's audit attribution. */
taskId: string;
/** borrowed-readonly — the acquired session id (notice attribution and the call context's `sessionId`). */
sessionId: string;
/** borrowed-readonly — the spawned name this run carries, when any (call-context attribution only). */
agentName: string | undefined;
/** borrowed-readonly — every host call's authorization binding: the run's identity (`ownerIdentityOf(spec)`, the same value
* the run's tool-result view records) and the delegation tree's root anchor (`rootAnchorOf`, the trusted tool context's). */
binding: ArtifactCallBinding;
/** borrowed-readonly — the run's effective approver (`spec.onAsk ?? deps.onAsk`, the seat the tool gate resolves at):
* the tool's shared-read consent gate rides it as a mount dep (a core mount's execute ctx is minimal). */
onAsk: OnAsk | undefined;
/** borrowed-mutable — the leg's occurrence table: a consent the tool asks DURING its execution is a wait on the person
* for that call, counted through the table's one bracket (each confirmation one entry and one exit). */
toolOccurrences: ToolOccurrenceTable;
/** borrowed-readonly — the leg's live permission-mode seat; the tool's shared-read consent gate arbitrates at it before
* the approver is consulted (a released read is delivered, undisclosed to nobody: the gate records it). */
permissionMode: PermissionModeController;
/** borrowed-readonly — the leg's auto-mode decider and denial tracker, read per question (armed later in prepare): under `auto` the
* consent gate asks the classifier first, as the gate's classifier station answers an ask of a class it may answer. */
autoMode: {
readonly decider: () => AutoModeDecider | undefined;
readonly tracking: () => AutoModeDenialTracker | undefined;
};
/** borrowed-readonly — the run's resolved env: the byte source of a published local file. */
executionEnv: ExecutionEnv;
/** borrowed-readonly — the canonical task root the fs fence contains reads to; undefined ⇒ no local-file publish. */
rootCanonical: string | undefined;
/** borrowed-readonly — the extra containment roots (write and read-only) the fence admits, read per call (the run's
* live set: an in-flight read-root grant widens it); `ready` settles once the run's newest tables are compiled — this
* reader is no call, and reads them only then. */
additionalRootsCanonical: (() => readonly string[]) & Pick<ReadRootsSeat, "ready">;
/** borrowed-readonly — what a refusal of a local file outside those roots names in place of the escape hint (the run's read
* face's remedy on a leg that refuses reads outside its directories); absent ⇒ the escape hint. */
outsideReadRemedy: string | undefined;
/** borrowed-readonly — the read-deny matcher in force for the run, when any. */
readDenyMatcher: PlacedReadDenyMatcher | undefined;
/** borrowed-readonly — the resolved read face (`"open"` lifts root containment for reads). */
resolvedReadFace: ContainmentFace | undefined;
/** borrowed-readonly — the tracked-cwd cell; `current` is read per call as the relative-path base. */
handsCwdRef: Prepared["cwdRef"];
/** borrowed-mutable — the run's roster builder; writer here: ONE mount when the seat is valid. */
roster: RosterBuilder;
/** borrowed-readonly — the first-party result-offload wrapper every core mount passes through (a large read or a conflict snapshot is offloaded, not inlined). */
firstPartyOffload: (tool: AgentTool) => AgentTool;
/** borrowed-mutable — the run's store cell the in-flight read-root grant reads (minted by the hands phase; absent on
* a hands-less leg, which has no grant); writer here: the declared `storageDir`'s physical path once the
* declaration validates. */
artifactStoreRef?: ArtifactStoreRef;
}
export interface PrepareArtifactResult {
/** owned — the mounted face's plane, or undefined when nothing mounted (absent seat, or an invalid one). */
artifactPlane: ArtifactPlane | undefined;
}
export declare function prepareArtifact(input: PrepareArtifactInput): PrepareArtifactResult;