UNPKG

@sema-agent/core

Version:

Stateless, task-oriented AI agent core

108 lines (107 loc) • 8.57 kB
import { type ExecutionEnvPurpose, type PlacementTarget } from "../remote-env.js"; import type { ExecutionEnv } from "../../internal/harness.js"; import type { RunnerDeps, TaskSpec } from "../types.js"; import type { ChildPlacement, PrepareResume, RollbackStack, RunInternals } from "./contracts.js"; /** * The ONE reference to a factory-minted env, shared by the driver's detach guard and the rollback entry * this phase registers. `current` is the minted env, or `undefined` when the run has none / after the * driver detached an aliasing one. Written by: this phase (once, at the mint) and the driver's worktree * guard (the detach). Read by: the rollback entry (at unwind), and the driver, which settles it into a * plain `ownedEnv` binding once the guard has passed. */ export interface OwnedEnvSeat { current: ExecutionEnv | undefined; } export interface PrepareExecutionEnvInput { /** borrowed-readonly — the deployment's env seats: the per-task factory (wins), what the deployment states the factory * answers, the static env, the declared root fence, and the incident sink for the bounded teardown legs. */ deps: Pick<RunnerDeps, "executionEnvFactory" | "executionEnvFactoryHonorsReadChanges" | "executionEnv" | "rootPath" | "onError" | "onNotice">; /** borrowed-readonly — why this env is acquired: handed to the factory as `ExecutionEnvFactoryContext.purpose`, and read by * {@link factoryAskOf}. */ purpose: ExecutionEnvPurpose; /** borrowed-readonly — the REBOUND spec; only the caller's task id rides into the factory ctx. */ spec: Pick<TaskSpec, "taskId">; /** borrowed-readonly — the trusted spawn-side requests: the explicit cwd (a contract), the isolation * request and the parent's cwd (both threaded to the factory ctx). */ internals: Pick<RunInternals, "requestedCwd" | "isolation" | "parentCwd" | "callerRoot" | "worktreeOffered" | "placementTarget"> | undefined; /** borrowed-readonly — the resume leg: read to refuse an explicit cwd a checkpoint restore would overrule, and for the workspace its row recorded — read once, as the row handle's frozen copy: the fence's first member source and the factory's `priorHandle`. */ resume: Pick<PrepareResume, "workspaceHandle"> | undefined; /** borrowed-readonly — the run tree's placement fixed point, resolved ONCE by the driver; the factory * door refuses an empty value loudly before the factory is asked. */ placementRootResolved: string; /** borrowed-readonly — the acquired session id (the factory ctx's identity, and the incident phase tag). */ sessionId: string; /** borrowed-readonly — the driver's session-view forget leg; every refusal here runs it, bounded, before * throwing (the acquired view must not outlive a prepare the caller never gets a handle to). */ forgetOnThrow: () => Promise<void>; /** borrowed-mutable — the driver-owned prepare-failure stack; this phase pushes ONE entry (the minted * env's destroy, read through the seat) and nothing else touches it here. */ rollback: RollbackStack; /** borrowed-readonly — may the factory be asked? `false` exactly when the leg's mount plan read that it needs no * environment (`ToolMountPlan.executionEnv.state === "unneeded"`); the fold below is unchanged, so a static env still * stands and the stub answers otherwise. A caller that always needs one (the rewind verb) passes `true`. */ startsEnv: boolean; /** borrowed-readonly — present on a resume leg sent back to its row's workspace (the row recorded one): a refusal here hands the * minted env to the leg's exit through it instead of destroying it (the exit keeps or destroys it by what became of the row). */ toRowExit?: (env: ExecutionEnv) => void; } export interface PrepareExecutionEnvResult { /** borrowed-readonly — the run's resolved env: the minted one, else the static one, else the no-I/O stub. */ executionEnv: ExecutionEnv; /** ownership-ref — the seat holding the factory-minted env (`current` undefined when none was minted). * The exception-path protocol stays with the driver (D-8 案①): its worktree guard may detach through * this seat before throwing; the rollback entry registered here reads it at unwind. */ ownedEnvSeat: OwnedEnvSeat; /** borrowed-readonly — epoch ms at which the factory-minted env was obtained (the lifetime anchor of * last resort); `undefined` for a static env or no env. */ ownedEnvObservedAt: number | undefined; /** borrowed-readonly — whether a real fs env is present (a minted env, or a static one). */ handsEnabled: boolean; /** borrowed-readonly — the INITIAL task root (the workspace-restore phase may re-root it and returns the * settled root under the NEW name `taskRootFinal`). */ taskRootInitial: string; /** borrowed-readonly — where a CHILD landed against where its caller asked ({@link childPlacementOnLeg}); absent on a leg no * delegation caller placed (no `callerRoot`). */ placement: ChildPlacement | undefined; /** borrowed-readonly — the fence this leg was minted under (folded once here; absent when unfenced): the restore phase checks the env against it. */ placementFence: PlacementTarget | undefined; /** borrowed-readonly — a factory is wired and was not asked because the leg needs no environment ({@link factoryAskOf} * answered `"not_needed"`): the hands seam's `not_needed`. */ skippedFactory: boolean; } /** * Whether a run of this deployment has a workspace (a real fs env: the hands fact), as its seats answer it before anything is minted: * a static env ⇒ `true` (a factory that hands back none leaves the static one standing); no factory and no static env ⇒ `false`; * a factory without a static env ⇒ `undefined` — the factory decides when it runs (it may hand back none). The hands fact below is * this answer, or, where it is `undefined`, whether the factory minted an env: one reading, before the mint and after it. */ export declare function workspaceOfAssembly(deps: Pick<RunnerDeps, "executionEnvFactory" | "executionEnv">): boolean | undefined; /** The environment a run of this deployment works in, as its seats answer before anything is minted: the static env on a * deployment with no factory; `undefined` where a factory decides when it runs, and where there is no env at all. */ export declare function envOfAssembly(deps: Pick<RunnerDeps, "executionEnvFactory" | "executionEnv">): ExecutionEnv | undefined; /** The answers of {@link factoryAskOf}. */ export type FactoryAsk = "ask" | "not_needed" | "not_answered"; /** * Whether THIS acquisition asks the deployment's factory — the one judgment of it, read on a deployment that has a factory * (a Runner with none has nothing to ask; the env fold is its answer, whatever the purpose): * · `"not_answered"` — the purpose is `"read_changes"` (read-only out-of-run reads: `readSessionChanges`, `previewInstructions`) and the * deployment does not state its factory answers it (`RunnerDeps.executionEnvFactoryHonorsReadChanges` is not `true`): no hands — the * no-I/O stub, never the static env (it is not where the session's runs work), no cwd door, the factory not called; * · `"not_needed"` — the leg needs no environment (`startsEnv` false): the factory is not asked and the fold stands (a static * env, else the stub); * · `"ask"` — the factory is called. */ export declare function factoryAskOf(input: { purpose: ExecutionEnvPurpose; startsEnv: boolean; deps: Pick<RunnerDeps, "executionEnvFactoryHonorsReadChanges">; }): FactoryAsk; /** * The place `p` names in `env`, as the requested-cwd doors compare it — `undefined` when the env does not read `p` as an absolute * path of its own: its `absolutePath` (the seam's anchor law: absolute as written, relative joined under its cwd) answers anything * but `p` as written, or nothing. So a path of another family than the env's (`C:\…`, `\\srv\…` handed to a posix env) names no * place, and neither does a relative cwd the env stands on. Otherwise the placement judgments' one reading ({@link canonicalOrSelf}): * resolved, or — when the env cannot resolve it (a directory not made yet) — that absolute address. */ export declare function placeIn(env: ExecutionEnv, p: string): Promise<string | undefined>; export declare function prepareExecutionEnv(input: PrepareExecutionEnvInput): Promise<PrepareExecutionEnvResult>;