@sema-agent/core
Version:
Stateless, task-oriented AI agent core
65 lines (64 loc) • 6.67 kB
TypeScript
import type { AgentTool, ExecutionEnv } from "../../internal/harness-types.js";
import type { BeforeWriteHook, FileEditedHook, TrackFileEditHook } from "../../core/types.js";
import { type ReadFileState, resolveKey } from "./safety.js";
import type { ContainmentFace } from "./containment-face.js";
import { type ReadDenyMatcher, type WriteDenyMatcher } from "./read-deny.js";
import { type CwdRef } from "./fs-shared.js";
import type { LegToolResultView } from "../../core/tool-result-store.js";
/**
* The `beforeWrite` CONTENT hook, mounted here rather than in ToolPolicy because only this band sees
* the FINAL text an Edit/NotebookEdit produces — the applied old→new full text is what actually lands
* on disk. Called before EVERY `env.writeFile` in Write/Edit/NotebookEdit with the resolved
* containment key and the exact text about to be written. Returning `{ ok:false }` makes the tool fail
* with a structured error and NOTHING is written. The Runner wires this to the MemoryEngine's write
* gate (`MemoryEngine.gateWrite`), where a non-memory path passes on one string prefix comparison.
* Absent hook ⇒ byte-identical behavior.
*/
export type { BeforeWriteRequest, BeforeWriteResult, BeforeWriteHook, TrackEditRequest, TrackEditResult, TrackFileEditHook } from "../../core/types.js";
/** A write tool's primary containment root: fixed, or a getter over the leg's live write location (read once per call, so an
* EnterWorktree switch moves where the next write may land). Canonical in both forms. */
export type WriteRootSeat = string | (() => string);
/** The file write tools' ONE root-fence judgment of a target, the step each runs before it touches anything (its pre-ask
* validation and its execution): the spelling against the tracked cwd, the write location, the additional write roots, the
* call's write deny table and the write face in force. The gate's write station asks the same function over the same seats
* (`file-write-check.ts`), so the gate puts no question about a target this judgment refuses. */
export type WriteContainment = (path: string, signal: AbortSignal | undefined, toolCallId: string | undefined) => ReturnType<typeof resolveKey>;
export declare function writeContainmentOf(env: ExecutionEnv, rootCanonical: WriteRootSeat, cwdRef?: CwdRef, additionalRoots?: readonly string[], writeDeny?: WriteDenyMatcher, writeFace?: () => ContainmentFace): WriteContainment;
/**
* The run's answer to "may this write change a file the model has never read" — asked by Edit and Write only where the
* read-first rule ({@link requireRead}) would refuse a file with no read at all. The reference holds its older models to the
* rule and lets every other model change such a file when the model's own Read of it would be served now with nobody asked;
* the Runner fills the seat with that judgment for a model that does not declare `read_before_edit` (the trait the
* reference's older ids carry). Empty, or an answer that throws ⇒ the rule refuses as it always did (a library-direct mount,
* an older model). `key`: the write's canonical target; `path`: the call's spelling.
*/
export interface ReadFirstWaiverSeat {
current?: (target: {
readonly path: string;
readonly key: string;
}, signal: AbortSignal | undefined) => Promise<boolean>;
}
export declare function createEditFileTool(env: ExecutionEnv, state: ReadFileState, rootCanonical: WriteRootSeat, cwdRef?: CwdRef, additionalRoots?: readonly string[], beforeWrite?: BeforeWriteHook, trackEdit?: TrackFileEditHook, onEdited?: FileEditedHook, writeFace?: () => ContainmentFace, writeDeny?: WriteDenyMatcher, readDeny?: ReadDenyMatcher, readFirst?: ReadFirstWaiverSeat): AgentTool;
export declare function createWriteFileTool(env: ExecutionEnv, state: ReadFileState, rootCanonical: WriteRootSeat, cwdRef?: CwdRef, additionalRoots?: readonly string[], beforeWrite?: BeforeWriteHook, trackEdit?: TrackFileEditHook, onEdited?: FileEditedHook, writeFace?: () => ContainmentFace, writeDeny?: WriteDenyMatcher, readFirst?: ReadFirstWaiverSeat): AgentTool;
/** The store SaveToolResult reads: the run's view of its tool-result store ({@link LegToolResultView}), so a ref this run may not
* read reads as absent here exactly as it does to ReadToolResult. Read the one way a ref is read (text first, then bytes). */
export type KeptBytesReader = LegToolResultView;
/**
* SaveToolResult — writes the bytes a tool result kept in the run's tool-result store (the ref its line named) into a NEW file.
* The lane is Edit's create lane step for step: the path resolved and confined as every write tool's (`resolveKey`), an existing
* path refused, the write gate over the bytes themselves (`beforeWrite`), the first-touch history ("did not exist"), and the
* final write exclusive where the environment can make it so — a lost create race answers `already_exists`, a moved target
* `precondition_failed`, each in its own sentence; an environment with neither atomic seam keeps Edit's two-step create (the
* same accepted residue). The ref is read the way every reader reads one: a ref holding text is refused (it is ReadToolResult's),
* and one holding neither is the honest miss — as is a ref this run may not read, which the run's view answers as absent before
* its kind is looked at (so the reply says nothing about a ref of another identity, not even that it holds text).
*/
export declare function createSaveToolResultTool(env: ExecutionEnv, store: KeptBytesReader, rootCanonical: WriteRootSeat, cwdRef?: CwdRef, additionalRoots?: readonly string[], beforeWrite?: BeforeWriteHook, trackEdit?: TrackFileEditHook, onEdited?: FileEditedHook, writeFace?: () => ContainmentFace, writeDeny?: WriteDenyMatcher): AgentTool;
/**
* design v1.163 — NotebookEdit: replace/insert/delete a single cell in a .ipynb. CC-parity tool over the SAME hand-band
* safety skeleton as Edit/Write (resolveKey containment → requireRead read-before-edit → checkStale content-hash
* freshness → mutate → writeFile → state.set), with object ops swapped for ipynb-JSON cell mutation. Read serves
* .ipynb as the cell projection but records read state (hash/totalLines) in the RAW notebook-text
* coordinate — that raw-coordinate read record is what this tool's freshness check depends on.
*/
export declare function createNotebookEditTool(env: ExecutionEnv, state: ReadFileState, rootCanonical: WriteRootSeat, cwdRef?: CwdRef, additionalRoots?: readonly string[], beforeWrite?: BeforeWriteHook, trackEdit?: TrackFileEditHook, onEdited?: FileEditedHook, writeFace?: () => ContainmentFace, writeDeny?: WriteDenyMatcher): AgentTool;