@sema-agent/core
Version:
Stateless, task-oriented AI agent core
174 lines (173 loc) • 12.8 kB
TypeScript
import type { ExecutionEnv } from "../../internal/harness.js";
/** The code a declared session plan file that cannot be held as stated refuses the run with (`TaskSpec.planFile`). */
export declare const PLAN_FILE_INVALID_CODE = "config.plan_file_invalid";
/** The coded refusal for `TaskSpec.planFile`: the value as given and why it cannot be the session's plan file. */
export declare function planFileInvalid(value: unknown, why: string): Error & {
code: string;
detail: {
planFile: unknown;
reason: string;
};
};
/** The declared plan file's SHAPE (no I/O): a non-empty absolute string in lexical normal form, else the coded refusal. */
export declare function checkedPlanFileShape(value: unknown): string | undefined;
/** How long a plan file's path may be: the longest path a platform spells (Linux PATH_MAX) — it is told to the model whole. */
export declare const PLAN_PATH_MAX = 4096;
/** How the plan file is told to the model (the plan-exit tool, the planning frames, the post-compaction reference): whole
* and verbatim — {@link checkedPlanFileShape} refuses a declared path this would re-spell, so for an admitted file this is
* the path itself; the defusing stays as the trusted line's guard. The ONE renderer: every teller reads its answer. */
export declare function planFileShown(path: string): string;
/** The most of a plan file the plan's readers take (1 MiB): a file past it is refused, never truncated into a plan. */
export declare const PLAN_FILE_MAX_BYTES: number;
/** What the session plan file holds: its text (`undefined` = not written yet — absent, or blank), or why it could not be read. */
export type PlanFileReading = {
readonly ok: true;
readonly text: string | undefined;
} | {
readonly ok: false;
readonly reason: string;
};
export interface AdvertisedWritableDir {
/** The directory as the advertisement spells it. */
path: string;
/** Who advertises it — names the skip line and the trace. */
advertiser: "scratchpad" | "memory" | "plan";
/**
* How the directory comes to exist when the env reports it absent. The advertiser that owns the
* directory's creation supplies the marker file name for the atomic exclusive-create
* materialization (parent dirs created like writeFile; a marker that already exists — planted
* concurrently, symlink or not — is never written through; an env without an exclusive primitive
* narrows instead of emulating one with an overwriting write). An advertiser whose directory is
* created elsewhere (the memory engine materializes its own root) passes nothing — absent narrows.
*/
materializeMarker?: string;
/**
* The canonical spelling the advertiser's OWN gates key on, when it has one (the memory engine keys its
* write gate and its harvest boundary on `MemorySessionHandle.writableRoot`). A fence that resolves the
* directory to a different canonical path would admit writes those gates never see — such a mismatch is
* a skip, announced with both spellings, never an admission.
*/
expectCanonical?: string;
/**
* The advertiser's own write gate refuses every write into this directory for the whole run (the memory store, with
* persistence declared unavailable — `TaskSpec.memoryPersistenceCapable: false`). The FILE TOOLS still reach it, so that gate
* is what answers their write — loudly, with nothing asked — and their reads of it stay inside; a COMMAND passes no such
* gate, so the directory is not among the roots a command may write ({@link AdvertisedWritableDirs.commandRoots}). Absent ⇒
* every writer's root.
*/
writesRefusedByOwnGate?: true;
}
/** The skip announcement — the one loud outlet for a directory that was advertised but could not be admitted. */
export type AdvertisedWritableDirSkipped = (dir: AdvertisedWritableDir, reason: string) => void;
/**
* ONE rule for both halves of the auto-approved write surface: a directory offered as a WRITABLE DOMAIN
* (an advertised writable directory here; a configured `exemptDirs`/`acceptDirs` entry at the write
* gate) must not SWALLOW a root the fence already holds. Returns the first root the candidate equals or
* is an ancestor of — the one to name in the refusal — else `undefined`.
*
* Why a domain that contains a root is not a domain: the point of the domain is to exempt a small area
* the prompt (or the deployment) named. A candidate resolving onto the task root — a symlinked
* scratchpad or memory home is all it takes, and the resolution is what makes it invisible to the
* spelling — exempts the ENTIRE workspace instead, turning every ordinary source write from `ask` into
* `allow`. Refusing the admission is the fix; a special case inside the gate ("ignore this domain when
* it contains the root") would be the same rule written twice, in the place where it is easiest to
* forget. Both arguments must already be CANONICAL — this compares spellings, it does not resolve them.
*/
export declare function swallowedFenceRoot(domainCanonical: string, fenceRootsCanonical: readonly string[]): string | undefined;
export declare class AdvertisedWritableDirs {
private readonly env;
private readonly onSkipped;
/** The extra fence roots the SPEC declares (`additionalDirectories` / `additionalReadDirectories`), as
* spelled. Resolved by this seat rather than awaited from the hands phase — the memory advertiser is
* admitted BEFORE that phase, so a list that only grew there made the rule depend on phase order. An
* entry that does not resolve is not a fence root at all (the hands phase reads the SAME memoized
* answer and skips it too, announcing it there), so there is nothing here for it to guard. */
private readonly declaredRootSpellings;
private readonly admitted;
/** The admitted roots an advertiser's own gate refuses writes into ({@link AdvertisedWritableDir.writesRefusedByOwnGate}) —
* held from the commands whichever other advertiser names the same directory: the gate refuses the file tools there either way. */
private readonly heldFromCommands;
private readonly verdicts;
/** The CANONICAL roots the fence holds: the task root at construction, plus every root the spec
* DECLARES, resolved by this seat before it judges anything (see {@link resolveDeclaredRoots}) —
* so which advertiser runs first cannot decide what the rule sees. */
private readonly fenceRoots;
/** Latched after the declared spellings have been resolved once (the resolution is per seat, not per call). */
private declaredRootsResolved;
/** ONE answer per declared spelling, for this seat AND for the hands phase that folds the same entries
* into the allowlist ({@link canonicalizeDeclaredRoot}): two independent lookups of one path can
* DISAGREE, and a root outside this rule's list while inside the fence is exactly the gap. */
private readonly declaredRootAnswers;
constructor(env: ExecutionEnv, onSkipped: AdvertisedWritableDirSkipped,
/** The run's task root, CANONICAL (the driver's one resolution — see `task-root-canonical.ts`). */
taskRootCanonical: string,
/** The extra fence roots the SPEC declares (`additionalDirectories` / `additionalReadDirectories`), as
* spelled. Resolved by this seat rather than awaited from the hands phase — the memory advertiser is
* admitted BEFORE that phase, so a list that only grew there made the rule depend on phase order. An
* entry that does not resolve is not a fence root at all (the hands phase reads the SAME memoized
* answer and skips it too, announcing it there), so there is nothing here for it to guard. */
declaredRootSpellings?: readonly string[]);
/** Declare one more CANONICAL root the fence holds (an `additionalDirectories` /
* `additionalReadDirectories` entry, once the hands phase has resolved it). Declaring is
* idempotent and order-free; a root declared after an advertiser was judged does not re-open it. */
declareFenceRoot(canonical: string): void;
/**
* The canonical form of a DECLARED fence-root spelling — resolved at most once per spelling and
* served from the memo thereafter. The hands phase calls this instead of canonicalizing the same
* entries again, so the list this rule judges against and the allowlist the fence ends up holding
* are built from ONE answer per path: a lookup that fails for this seat fails for that phase too,
* and a root cannot be outside the rule's view while being inside the fence.
*/
canonicalizeDeclaredRoot(spelled: string): Promise<Awaited<ReturnType<ExecutionEnv["canonicalPath"]>>>;
/** Resolve the spec-declared root spellings into {@link fenceRoots}, exactly once per seat. */
private resolveDeclaredRoots;
/** The admitted roots, CANONICAL, in admission order, deduplicated. The hands phase folds this list into the
* write-capable containment allowlist before the toolkit compiles; it is not a live view after that. */
get roots(): readonly string[];
/** The admitted roots a COMMAND may write (a shell command's write targets, the command sandbox's write list): {@link roots}
* less a directory an admission said its advertiser's own gate refuses writes into
* ({@link AdvertisedWritableDir.writesRefusedByOwnGate}), whoever else advertises it — nothing stands between a command and
* such a directory to refuse it, so it is no command's write root. Same order, same keys. */
get commandRoots(): readonly string[];
/** The session plan file as admitted — its declared spelling and its frozen canonical key (the key is also on
* {@link roots}) — or `undefined` when none was declared. The write-protection judge reads it to exempt exactly
* this file; nothing else holds a copy. */
get planFile(): {
readonly spelled: string;
readonly key: string;
} | undefined;
private admittedPlanFile;
/**
* Admit the session plan file (`TaskSpec.planFile`, already shape-checked by {@link checkedPlanFileShape}): a FILE, so
* its key is the CANONICAL PARENT joined with the declared name — frozen here like every key this seat holds, never
* re-resolved — and the leaf is looked at without following links. The file need not exist yet (the model creates it),
* and it is never materialized: its directory is the host's to create.
*
* Refusing, not skipping: the leaf is a symbolic link (admitting where it points would let the declaration exempt any
* file the link names), a directory or another non-file kind; the parent does not resolve, or resolves to something
* other than a directory; or the key would hold a root the fence already holds (the seat's one containment rule).
* Each throws {@link planFileInvalid} — the plan file decides what the plan-exit tool reads, so a declared file the
* fence narrowed away would teach a tool over a file nobody can write.
*/
admitPlanFile(spelled: string): Promise<string>;
/**
* What the admitted session plan file holds right now — the ONE reader of the plan's contents (the plan-exit tool and the
* post-compaction reference both read here), through the environment the fence judges with, at the key admitted (frozen,
* never re-resolved), the leaf looked at without following a link: a file later replaced by a link, a directory or
* another kind is refused with the reason, never read through.
*/
readPlanFile(signal?: AbortSignal): Promise<PlanFileReading>;
/** What became of an advertiser's directory: admitted, skipped (announced), or never asked (the advertiser
* had nothing to advertise, or decided not to). The prompt side reads this to withhold an advertisement. */
verdictOf(advertiser: AdvertisedWritableDir["advertiser"]): "admitted" | "skipped" | "not-asked";
/**
* Admit one advertised directory. TRUE ⇔ the fence will admit writes under it (its canonical form is on
* {@link roots}); FALSE ⇔ it was skipped (announced through the seat's outlet) and the advertiser must not
* advertise it. The env's answers are Result-shaped, so the judgment itself never throws; the skip outlet is
* the caller's own sink (`deps.onError` in the driver) and is deliberately not guarded here — every phase
* lets that sink's own exceptions propagate, and a seat that swallowed them would hide the deployment's bug.
*/
admit(dir: AdvertisedWritableDir): Promise<boolean>;
/** The admitted key, or `undefined` (skipped, announced). */
private judge;
}