import { a as Executor } from './types-Driepl87.js';
import { L as LocalHarness } from './local-harness-sI0S_XNA.js';

/**
 *
 * The completion-oracle: **settled ⟺ DELIVERED.**
 *
 * Foreman's one hard lesson (0/18 self-improvement deliverables) — "done" must mean a check
 * PASSED, not the agent's say-so. `gateOnDeliverable` wraps an `Executor` so its settlement
 * is `valid` ONLY when the deliverable check passes. The child still RUNS and settles (its
 * spend is conserved into the pool either way), but a child that ran WITHOUT delivering
 * settles `valid:false` — so a keep-best driver never counts it as done, and a gate never
 * inflates with self-judged wins.
 *
 * Dual-purpose by construction:
 *  - product: the agent fleet only advances on real, checked deliverables.
 *  - proof: the gate's `valid` is the honest settle — equal-k comparisons can't be gamed by an
 *    arm that "ran" without producing the artifact.
 *
 * The check is a DEPLOYABLE oracle — a test command, a state verifier, the commit0 judge —
 * read off the child's output, never the model judging itself. A throwing check is
 * fail-closed (not delivered), never a crash.
 *
 * @experimental
 */

/**
 * The deployable completion oracle passed to {@link gateOnDeliverable}: a `check` that
 * decides DELIVERED (settles `valid` ⟺ it resolves true) plus an optional `describe` of
 * what the spawn was supposed to produce. The check reads the child's output — never the
 * model judging itself.
 */
interface DeliverableSpec<Out = unknown> {
    /** The deployable check that decides DELIVERED. `settled.valid ⟺ this resolves true`. */
    check: (out: Out) => boolean | Promise<boolean>;
    /** What the spawn was supposed to produce — surfaced in traces/reports. */
    describe?: string;
}
/**
 * Wrap an `Executor` so its settlement `valid` reflects the deliverable check, not the
 * inner verdict. Handles both `execute` shapes (one-shot `Promise<ExecutorResult>` and
 * streaming `AsyncIterable<UsageEvent>` + `resultArtifact()`); the check runs once the inner
 * executor has produced its output. The inner `score` is preserved; only `valid` is gated.
 */
declare function gateOnDeliverable<Out>(inner: Executor<Out>, deliverable: DeliverableSpec<Out>): Executor<Out>;

/**
 *
 * Git worktree helpers for the in-process delegation executor. Each
 * delegation runs in its own worktree so multiple parallel harness
 * subprocesses (claude / codex / opencode in a 3-way fanout) don't clobber
 * each other's edits on the shared workspace.
 *
 * Worktrees live under `<repoRoot>/.agent-worktrees/<runId>/`. After the
 * harness exits + the diff is captured, the worktree is removed.
 *
 * All operations spawn `git` via `child_process.spawn` synchronously
 * (via a `runGit` helper). Stays narrow on purpose: no commits, no rebases.
 * Diff capture stages all changes (`git add -A`) into the ephemeral worktree's
 * index so created (untracked) files appear in the `--cached` diff.
 *
 * @experimental
 */
/** @experimental */
interface WorktreeHandle {
    /** Absolute path to the worktree directory. */
    path: string;
    /** SHA the worktree was created at. */
    baseSha: string;
    /** Branch name created for this worktree (typically `delegate/<runId>`). */
    branch: string;
}
/** @experimental */
interface CreateWorktreeOptions {
    /** Absolute path to the main git checkout. */
    repoRoot: string;
    /** Unique id for the worktree path + branch. Use the delegation run id. */
    runId: string;
    /** Parent directory the worktree lives under. Defaults to `.agent-worktrees`. */
    variantsDir?: string;
    /** Override the base ref (default `HEAD`). */
    baseRef?: string;
    /** Test seam — inject a custom git runner. */
    runGit?: GitRunner;
}
/** @experimental */
interface DiffOptions {
    /** Worktree to diff. */
    worktree: WorktreeHandle;
    /** What to compare against. Default `worktree.baseSha`. */
    baseRef?: string;
    /** Test seam. */
    runGit?: GitRunner;
}
/** @experimental */
interface DiffResult {
    patch: string;
    stats: {
        filesChanged: number;
        insertions: number;
        deletions: number;
    };
}
/** @experimental */
interface RemoveWorktreeOptions {
    worktree: WorktreeHandle;
    repoRoot: string;
    /** Force removal even if dirty (default true; the loser of a fanout has uncommitted changes). */
    force?: boolean;
    /** Test seam. */
    runGit?: GitRunner;
}
/** Pluggable git runner (sync) — replaceable in tests. */
type GitRunner = (args: ReadonlyArray<string>, opts: {
    cwd: string;
}) => {
    stdout: string;
    stderr: string;
    exitCode: number;
};
/** Checkout a fresh git worktree for a delegation run on a new branch under `variantsDir`. @experimental */
declare function createWorktree(options: CreateWorktreeOptions): Promise<WorktreeHandle>;
/** Stage all changes in a worktree and return the diff patch + shortstat against the base ref. @experimental */
declare function captureWorktreeDiff(options: DiffOptions): Promise<DiffResult>;
/** Remove a git worktree and delete its branch; tolerates already-removed paths. @experimental */
declare function removeWorktree(options: RemoveWorktreeOptions): Promise<void>;

/**
 *
 * The ONE worktree-harness execution core. The physical act — run a supervisor-authored
 * `AgentProfile` on a local coding-harness CLI (claude / codex / opencode) against a fresh git
 * worktree off `repoRoot`, capture the diff, derive the test/typecheck PASS signals, then clean
 * up — lives here ONCE. Two executors adapt it to two ports without re-implementing it:
 *   - `createWorktreeCliExecutor` — the `Scope`/`Supervisor` leaf `Executor`.
 *   - `createInProcessExecutor`   — the `runLoop` `SandboxClient` / coder-delegate path.
 *
 * §1.5 by construction: the authored `profile.prompt.systemPrompt` + `profile.model.default`
 * reach the harness through `harnessInvocation` HERE, so neither port can drop them — the exact
 * bug that existed while the in-process path called `runLocalHarness` with only the task prompt.
 *
 * Lifecycle: `createWorktree` → `harnessInvocation` + `runLocalHarness` → `captureWorktreeDiff`
 * (BEFORE checks, so the patch is the harness's output, not polluted by files a test run writes)
 * → the configured test/typecheck commands in the live worktree → return the result + a `cleanup`
 * the caller invokes at its own teardown point. A throw cleans up before propagating, so a failed
 * run never leaks a worktree.
 *
 * @experimental
 */

/** Outcome of one verification command run in the worktree (test or typecheck). */
interface WorktreeCommandResult {
    /** The shell command line that was run. */
    command: string;
    /** Did the command exit 0? The PASS signal a deliverable gate / coder output reads. */
    passed: boolean;
    /** OS exit code, or `null` when killed before exit. */
    exitCode: number | null;
    /** Combined stdout+stderr (capped) — surfaced in traces for diagnosis. */
    output: string;
}
/** The canonical result of one worktree-harness run, projected by each port to its own shape. */
interface WorktreeHarnessResult {
    /** The branch the worktree was cut on (`delegate/<runId>`). */
    branch: string;
    /** `git diff` of the worktree against its base — the unified patch the harness produced. */
    patch: string;
    /** Shortstat-derived change counts. */
    stats: {
        filesChanged: number;
        insertions: number;
        deletions: number;
    };
    /** The harness subprocess outcome. */
    harness: {
        name: LocalHarness | 'bridge';
        exitCode: number | null;
        timedOut: boolean;
        killedBySignal: NodeJS.Signals | null;
        durationMs: number;
        stdout: string;
        stderr: string;
    };
    /** Verification signals derived in the live worktree (present only when commands were given). */
    checks?: {
        tests?: WorktreeCommandResult;
        typecheck?: WorktreeCommandResult;
    };
}
/** The single shell-command-in-worktree runner seam (replaces the per-executor copies). */
type WorktreeCheckRunner = (opts: {
    command: string;
    cwd: string;
    timeoutMs: number;
    signal?: AbortSignal;
}) => Promise<{
    exitCode: number | null;
    output: string;
}>;

export { type CreateWorktreeOptions as C, type DeliverableSpec as D, type GitRunner as G, type RemoveWorktreeOptions as R, type WorktreeHarnessResult as W, type WorktreeCheckRunner as a, type DiffOptions as b, type DiffResult as c, type WorktreeHandle as d, captureWorktreeDiff as e, createWorktree as f, type WorktreeCommandResult as g, gateOnDeliverable as h, removeWorktree as r };
