import { S as Scenario, C as CampaignResult, G as GateResult, D as DispatchFn, a as JudgeConfig, L as LabeledScenarioStore, c as CampaignTraceWriter, d as GenerationRecord, M as MutableSurface, P as ParetoParent, e as SurfaceProposer, f as Gate } from './types-D9tuReDv.js';
import { L as LlmClientOptions } from './llm-client-Bj7g0rqu.js';

/**
 * `openAutoPr` — thin shell-out helper for the `runImprovementLoop` preset's
 * `autoOnPromote: 'pr'` mode. Substitutes for the per-product PR-opening
 * code consumers duplicated 4 times. The PR body includes the campaign's
 * manifest hash, gate verdict, and scorecard summary so reviewers can see
 * exactly what was promoted + why.
 *
 * NOT a deploy mechanism — this only OPENS a PR. The human reviews + merges.
 * The Shape B (`autoOnPromote: 'config'`) live-runtime-mutation path is
 * deferred to Pass B with the full shadow / canary / rollback stack.
 */

interface OpenAutoPrOptions<TArtifact, TScenario extends Scenario> {
    /** Campaign result to attach to the PR. */
    result: CampaignResult<TArtifact, TScenario>;
    /** Gate verdict explaining the promotion. Substrate refuses to open a PR
     *  when `gate.decision !== 'ship'` — fails loud. */
    gate: GateResult;
    /** Promoted surface diff — typically the new system prompt addendum or
     *  full profile diff. Substrate writes it as the PR body. */
    promotedDiff: string;
    /** GH owner/repo target (e.g., `tangle-network/gtm-agent`). */
    ghOwner: string;
    ghRepo: string;
    /** Branch name for the PR. Default `auto/<manifestHash[:12]>`. */
    branch?: string;
    /** PR title. Default includes manifest hash. */
    title?: string;
    /** Whether to actually open the PR or just dry-run. Default reads
     *  `GH_AUTO_PR_TOKEN` env — present = open, absent = dry-run. */
    dryRun?: boolean;
    /** Test seam — substitute `gh pr create` invocation. */
    ghExec?: (args: string[]) => {
        stdout: string;
        stderr: string;
        status: number;
    };
}
interface OpenAutoPrResult {
    opened: boolean;
    prUrl?: string;
    dryRun: boolean;
    reason: string;
}
declare function openAutoPr<TArtifact, TScenario extends Scenario>(options: OpenAutoPrOptions<TArtifact, TScenario>): OpenAutoPrResult;

/**
 * `CampaignStorage` — the filesystem seam `runCampaign` writes through
 * (run/cell dirs, the resumability cache, per-cell artifacts, trace spans).
 *
 * The default (`fsCampaignStorage`) is the Node filesystem — identical
 * behavior to the inline `node:fs` calls it replaces, so existing CLI
 * consumers are unaffected. `inMemoryCampaignStorage` keeps everything in a
 * `Map`, so the substrate runs in environments WITHOUT a filesystem
 * (Cloudflare Workers, Deno Deploy, other edge runtimes) — the campaign
 * still produces its `CampaignResult` (cells + aggregates) in memory;
 * artifacts/traces simply aren't persisted to disk.
 *
 * Paths are opaque keys to the in-memory adapter — it does not parse them,
 * so the same `join(...)`-built paths work unchanged across both adapters.
 */
interface CampaignStorage {
    /** Ensure a directory exists (recursive). No-op for in-memory. */
    ensureDir(dir: string): void;
    /** Does this path exist (as a written file or an ensured dir)? */
    exists(path: string): boolean;
    /** Read a UTF-8 file; `undefined` when missing or unreadable. */
    read(path: string): string | undefined;
    /** Write a file (string or bytes). Parent dir is assumed ensured. */
    write(path: string, content: string | Uint8Array): void;
}
/** Node-filesystem storage — the default. Lazily requires `node:fs` so the
 *  module imports cleanly in non-Node runtimes (where the caller passes
 *  `inMemoryCampaignStorage` instead and never constructs this).
 *
 *  `createRequire(import.meta.url)` is the ESM-native lazy require — a bare
 *  `require` is a ReferenceError under `"type": "module"`, which is exactly
 *  the shape this package publishes. */
declare function fsCampaignStorage(): CampaignStorage;
/** In-memory storage for filesystem-less runtimes. Artifacts + trace spans
 *  live in a `Map` for the duration of the run; the `CampaignResult` is
 *  fully populated, but nothing is persisted to disk. */
declare function inMemoryCampaignStorage(): CampaignStorage;

/**
 * `runCampaign` — Pass A substrate primitive. ONE function that orchestrates
 * scenarios → dispatch → artifacts → judges → aggregates, with full
 * reproducibility (seed + manifest hash), cell-level resumability, bootstrap
 * CIs, and the `LabeledScenarioStore` capture flywheel.
 *
 * Improvement loops (optimizer / gate / autoOnPromote) ride on top of this
 * primitive but live in `presets/run-improvement-loop.ts`. This file keeps
 * the core orchestrator minimal — Phase 1 of the Pass A track.
 */

interface RunCampaignOptions<TScenario extends Scenario, TArtifact> {
    scenarios: TScenario[];
    dispatch: DispatchFn<TScenario, TArtifact>;
    judges?: JudgeConfig<TArtifact, TScenario>[];
    /** Required for reproducibility. Default 42. */
    seed?: number;
    /** Per-scenario replicates for CI bands. Default 1; raise to 5+ for
     *  bootstrap-tight intervals on critical eval. */
    reps?: number;
    /** When true (default), completed cells are cached by
     *  (manifestHash, scenarioId, rep, generation). Re-runs skip cached cells. */
    resumable?: boolean;
    /** Optional store — when present, every artifact + judge score is captured
     *  with the configured `captureSource`. Capture is default ON; pass `'off'`
     *  to disable. */
    labeledStore?: LabeledScenarioStore | 'off';
    captureSource?: 'production-trace' | 'eval-run' | 'manual' | 'red-team' | 'synthetic';
    captureSourceVersionHash?: string;
    /** Wall-clock cost cap across all cells. Cells beyond ceiling are skipped. */
    costCeiling?: number;
    /** Max concurrent cells. Default 2. */
    maxConcurrency?: number;
    /**
     * Per-cell dispatch deadline in ms. A `dispatch` that neither resolves nor
     * rejects within this window is a hang (a stalled model request, an
     * exhausted runtime resource, a backend that never closes its stream). When
     * set, the cell's `ctx.signal` is aborted and the cell is recorded as a LOUD
     * error (`dispatch exceeded <N>ms`) so the campaign proceeds and the failure
     * is visible — instead of one wedged cell silently hanging the whole run (and
     * every loop/CI job above it) forever. `undefined`/`0` = unbounded (legacy).
     */
    dispatchTimeoutMs?: number;
    /** Required: where artifacts + traces land. */
    runDir: string;
    /** Tracing posture. Default is the substrate's `FileSystemTraceStore` rooted
     *  at `<runDir>/traces/`. `'off'` disables capture entirely — substrate
     *  refuses this when the caller wires `autoOnPromote !== 'none'`. */
    tracing?: 'on' | 'off';
    /**
     * Per-cell usage expectation — the early, fine-grained sibling of the
     * batch `assertRealBackend` guard. A cell that produced an artifact (no
     * error) but reported `costUsd === 0` AND zero tokens is a stub: the
     * dispatch never reported LLM activity via `ctx.cost`. Modes:
     *   - `'warn'` (default) — log the offending cell loudly, keep going.
     *   - `'assert'` — throw `BackendIntegrityError` on the first such cell
     *     (fail-fast; recommended for CI campaigns expecting real LLM calls).
     *   - `'off'` — no check (replay / deterministic-only / offline analysis).
     */
    expectUsage?: 'assert' | 'warn' | 'off';
    /** Test seam — override the wall clock for deterministic tests. */
    now?: () => Date;
    /** Test seam — override per-cell trace writer factory. */
    buildTraceWriter?: (cellId: string, dir: string) => CampaignTraceWriter;
    /** Storage backend for run/cell dirs, the resumability cache, artifacts,
     *  and trace spans. Default: the Node filesystem (`fsCampaignStorage`).
     *  Pass `inMemoryCampaignStorage()` to run in a filesystem-less runtime
     *  (Cloudflare Workers, Deno, edge) — the `CampaignResult` is still
     *  produced; artifacts/traces just aren't persisted to disk. */
    storage?: CampaignStorage;
    /**
     * Optional per-cell placement strategy. Returns an opaque string the
     * substrate forwards as `ctx.placement` to the Dispatch — placement-aware
     * Dispatches (e.g. `httpDispatch` from `/adapters/http`) use it to route
     * each cell to the right worker, region, or sandbox. When unset, every
     * cell receives `ctx.placement = undefined` and behaves identically to
     * the in-process case.
     *
     * @example
     *   cellPlacement: ({ scenario }) => scenario.tags?.includes('eu') ? 'eu-west' : 'us-east'
     */
    cellPlacement?: (input: {
        scenario: TScenario;
        rep: number;
        generation?: number;
    }) => string | undefined;
}
declare function runCampaign<TScenario extends Scenario, TArtifact>(opts: RunCampaignOptions<TScenario, TArtifact>): Promise<CampaignResult<TArtifact, TScenario>>;

/**
 * `runOptimization` — the improvement loop body. Runs N generations: the
 * `SurfaceProposer` proposes K candidate surfaces per generation, each
 * candidate runs a campaign (the measurement), top-scoring promote to the
 * next generation. Proposer-agnostic — the same loop runs an evolutionary
 * population mutator (`evolutionaryProposer`) or any reflective / agentic
 * proposer; they differ only in how `propose()` picks candidates.
 *
 * This is `runLoop`'s shape (plan → measure → decide) specialized to surface
 * improvement: `proposer.propose` = plan, `runCampaign` = the measurement
 * (which runs the worker behind `dispatch`), the mean-composite ranking = the
 * validator, `proposer.decide` = the stop check.
 *
 * The gated-promotion shell (`runImprovementLoop`) wraps this with a holdout
 * re-score + release gate + optional PR.
 */

interface RunOptimizationBaseOptions<TScenario extends Scenario, TArtifact> extends Omit<RunCampaignOptions<TScenario, TArtifact>, 'dispatch'> {
    /** Initial mutable surface (typically system prompt or addendum). */
    baselineSurface: MutableSurface;
    /** Dispatcher that takes the CURRENT surface + scenario → artifact. */
    dispatchWithSurface: (surface: MutableSurface, scenario: TScenario, ctx: Parameters<RunCampaignOptions<TScenario, TArtifact>['dispatch']>[1]) => Promise<TArtifact>;
    /** The candidate-generation strategy. Wrap a population `Mutator` via
     *  `evolutionaryProposer({ mutator })`, or pass any reflective / agentic
     *  proposer that implements `SurfaceProposer`. */
    proposer: SurfaceProposer;
    populationSize: number;
    maxGenerations: number;
    /** How many top-scoring candidates carry to the next generation. Default 2. */
    promoteTopK?: number;
    /** DEPTH knob forwarded to the proposer's `propose()` — max iterations the
     *  agentic generator may take per candidate. */
    maxImprovementShots?: number;
    /** Optional analysis report forwarded to `propose()`. Opaque here; the
     *  proposer types it. */
    report?: unknown;
    /** Structured findings forwarded to `propose()` as `ctx.findings`. A
     *  findings producer emits these from the
     *  generation's traces; findings-grounded proposers consume them. Opaque here;
     *  the proposer types its `TFindings`. Empty when no producer is wired. */
    findings?: unknown[];
    /** Per-generation findings producer. After each
     *  generation's candidates are scored, this is called with that generation's
     *  results; whatever it returns REPLACES `ctx.findings` for the NEXT
     *  generation's `propose()`, so the diagnosis is refreshed each round instead
     *  of being a static one-shot. Generic by design: the substrate does not
     *  import an analyst — the consumer plugs its trace-analyst registry / HALO
     *  here (reading the per-candidate `runDir` traces). When absent, findings
     *  stay the static `opts.findings`. */
    analyzeGeneration?: (input: {
        generation: number;
        runDir: string;
        candidates: Array<{
            surfaceHash: string;
            campaign: CampaignResult<TArtifact, TScenario>;
            composite: number;
        }>;
        history: GenerationRecord[];
    }) => Promise<unknown[]>;
}
type RunOptimizationOptions<TScenario extends Scenario, TArtifact> = RunOptimizationBaseOptions<TScenario, TArtifact>;
interface RunOptimizationResult<TArtifact, TScenario extends Scenario> {
    generations: Array<{
        record: GenerationRecord;
        surfaces: Array<{
            surfaceHash: string;
            surface: MutableSurface;
            campaign: CampaignResult<TArtifact, TScenario>;
        }>;
    }>;
    winnerSurface: MutableSurface;
    winnerSurfaceHash: string;
    /** Proposer label for the promoted surface. Present when the winning
     *  candidate came from a `ProposedCandidate` (a reflective proposer);
     *  absent when the winner is the baseline or a bare-surface mutator. */
    winnerLabel?: string;
    /** Proposer rationale for the promoted surface — the "because Z" that
     *  motivated the winning change. Survives to `SelfImproveResult` and the
     *  emitted provenance record. Absent when the winner is the baseline. */
    winnerRationale?: string;
    baselineCampaign: CampaignResult<TArtifact, TScenario>;
    /** The GEPA Pareto frontier across every scored surface (baseline + all
     *  generations) by per-scenario objective vector — the non-dominated set.
     *  Each generation's `propose()` received the frontier-so-far as
     *  `ctx.paretoParents`; this is the final frontier. A surface here that is
     *  NOT the winner is uniquely best on some scenario the winner loses on. */
    paretoFrontier: ParetoParent[];
}
declare function runOptimization<TScenario extends Scenario, TArtifact>(opts: RunOptimizationOptions<TScenario, TArtifact>): Promise<RunOptimizationResult<TArtifact, TScenario>>;
declare function surfaceHash(surface: MutableSurface): string;

/**
 * `runImprovementLoop` — the gated-promotion shell around the improvement
 * loop body (`runOptimization`). Proposes candidate surfaces via the
 * `SurfaceProposer`, re-scores the winner against the baseline on a
 * holdout set, runs the release gate, and optionally opens a PR.
 *
 * Role vocabulary (see docs/design/loop-taxonomy.md):
 *   - PROPOSER   = the `SurfaceProposer` (evolutionary GEPA mutator OR
 *                  reflective analyst). Proposes candidate SURFACES — the
 *                  worker's system prompt / tool config — NOT conversation
 *                  turns.
 *   - MEASUREMENT= `runCampaign`. Scores one surface by running the worker
 *                  (via `dispatch`) over scenarios and judging the output.
 *   - WORKER     = the agent harness in the sandbox, invoked behind the
 *                  topology-opaque `dispatch` seam — never referenced here.
 *
 * Distinct from `runLoop` in `@tangle-network/agent-runtime`, which is the
 * INNER conversation loop (execution driver ↔ workers in a sandbox). `runImprovementLoop`
 * is the OUTER loop: it improves the surface that those workers run.
 *
 * Hard-refuses unsafe configurations:
 *   - `tracing: 'off'` when a proposer is wired (improvement is unattributable)
 *   - `autoOnPromote: 'config'` — DEFERRED to Pass B; v0.40 only ships
 *     `'pr'` and `'none'`.
 */

type RunImprovementLoopOptions<TScenario extends Scenario, TArtifact> = RunOptimizationOptions<TScenario, TArtifact> & {
    /** Holdout scenarios kept OUT of the training optimization pool — used
     *  ONLY to score baseline vs winner for the gate. */
    holdoutScenarios: TScenario[];
    /** Promotion gate. Substrate strongly recommends `defaultProductionGate`
     *  for production wiring (composes red-team / reward-hacking / canary /
     *  heldout). */
    gate: Gate<TArtifact, TScenario>;
    /** What to do when the gate ships:
     *   - `'pr'`: open a PR via `openAutoPr`
     *   - `'none'`: just report — caller decides what to do with the winner
     *  v0.40 does NOT support `'config'` (live-runtime self-mutation) —
     *  deferred to Pass B behind safety stack. */
    autoOnPromote: 'pr' | 'none';
    /** GH owner / repo for the auto-PR. Required when autoOnPromote === 'pr'. */
    ghOwner?: string;
    ghRepo?: string;
    /** Optional render override — substrate writes a diff-shaped surface; pass
     *  a function to format the promoted surface differently. */
    renderPromotedDiff?: (winnerSurface: MutableSurface, baselineSurface: MutableSurface) => string;
};
interface RunImprovementLoopResult<TArtifact, TScenario extends Scenario> extends RunOptimizationResult<TArtifact, TScenario> {
    baselineOnHoldout: CampaignResult<TArtifact, TScenario>;
    winnerOnHoldout: CampaignResult<TArtifact, TScenario>;
    gateResult: Awaited<ReturnType<Gate<TArtifact, TScenario>['decide']>>;
    /** Unified baseline→winner surface diff. Computed UNCONDITIONALLY (not only
     *  when `autoOnPromote === 'pr'`) so the diff that the gate decided on is
     *  always present on the result + in the emitted provenance record. Empty
     *  string when winner == baseline (no change to diff). */
    promotedDiff: string;
    prResult?: ReturnType<typeof openAutoPr>;
}
declare function runImprovementLoop<TScenario extends Scenario, TArtifact>(opts: RunImprovementLoopOptions<TScenario, TArtifact>): Promise<RunImprovementLoopResult<TArtifact, TScenario>>;
declare function defaultRenderDiff(winnerSurface: MutableSurface, baselineSurface: MutableSurface): string;

/**
 * `gepaProposer` — a reflective `SurfaceProposer` for prompt-tier surfaces.
 * Each generation it reflects on the prior best candidate's per-scenario
 * scores + weakest dimensions, asks an LLM to propose targeted rewrites of
 * the current surface, and returns them as the next population.
 *
 * Maps onto the GEPA paper (Agrawal et al., arXiv:2507.19457):
 *   - *Reflection*: each generation reflects on the best parent's weakest
 *     dimensions + per-scenario top/bottom scores to propose targeted rewrites.
 *   - *Pareto frontier*: `runOptimization` maintains the non-dominated set of
 *     surfaces across generations (per-scenario objective vectors) and supplies
 *     it as `ctx.paretoParents`. A surface uniquely best on one hard scenario
 *     survives even when its mean composite is lower.
 *   - *Combine complementary lessons*: when the frontier has >1 member, the
 *     first population slot is a merge of those parents' strengths (one LLM
 *     call citing each parent's winning scenarios). Toggle via `combineParents`.
 * Dominance is computed by the package-canonical `paretoFrontier` (`pareto.ts`).
 *
 * Optional `constraints` move structured-doc guards into the proposer
 * (preserve H2 section headings, cap sentence-level edits) — useful when
 * the surface IS a structured procedure like a SKILL.md / runbook /
 * judge rubric. When `constraints` is omitted, behavior is unchanged.
 *
 * The proposer is surface-agnostic — any string surface in any consumer opts
 * in by selecting it. Reuses the generic reflection primitive
 * (`buildReflectionPrompt` / `parseReflectionResponse`) and the router client.
 *
 * Earns its keep where there is real per-instance signal (which the
 * dimensional + per-scenario evidence + the `LabeledScenarioStore` flywheel
 * now provide). For thin-signal surfaces it degrades to plain reflection.
 * On generation 0 (no history) it reflects on the current surface against
 * the mutation primitives alone.
 */

interface GepaProposerConstraints {
    /** H2 section headings that MUST appear unchanged in every candidate.
     *  When set, the proposer auto-detects current H2s if this is empty AND
     *  rejects any candidate that drops or renames a preserved heading.
     *  Use when the surface is a structured doc (SKILL.md, runbook,
     *  sectioned system prompt, judge rubric). */
    preserveSections?: string[];
    /** Maximum sentence-level edits per candidate vs the parent surface.
     *  Rejection threshold = maxSentenceEdits × 2 (counts adds + removes).
     *  Inspired by SkillOpt's edit-budget as a "textual learning rate."
     *  Cap prevents an LLM rewrite from overwriting useful prior rules. */
    maxSentenceEdits?: number;
}
interface GepaProposerOptions {
    /** Router transport (apiKey/baseUrl). */
    llm: LlmClientOptions;
    /** Model that performs the reflection. */
    model: string;
    /** What is being optimized — appears in the reflection prompt for orientation. */
    target: string;
    /** Surface-specific mutation levers offered to the model. */
    mutationPrimitives?: string[];
    /** Top/bottom scenarios surfaced as evidence each generation. Default 3. */
    evidenceK?: number;
    /** Reflection sampling temperature. Default 0.7. */
    temperature?: number;
    /** Reflection max tokens. Default 6000. */
    maxTokens?: number;
    /** Structured-doc constraints. Candidates violating any are rejected
     *  post-parse and dropped from the returned population. */
    constraints?: GepaProposerConstraints;
    /** GEPA combine-complementary-lessons: when the loop supplies a Pareto
     *  frontier of >1 non-dominated parents (`ctx.paretoParents`), spend one
     *  slot of the population on a merge of their strengths. Default `true` —
     *  this is the GEPA-faithful behavior; the merge only fires once the
     *  frontier has more than one member (generation ≥ 1). Set `false` for
     *  pure single-parent reflection. */
    combineParents?: boolean;
    /** Cap on how many frontier parents feed one combine prompt (highest
     *  composite first), to bound prompt size. Default 4. */
    combineMaxParents?: number;
}
declare function gepaProposer(opts: GepaProposerOptions): SurfaceProposer;
/** Extract H2 headings (`## Foo`) from a markdown surface. Exported for
 *  consumers building custom mutators that share the same invariant. */
declare function extractH2Sections(text: string): string[];
/** Sentence-level edit distance — count distinct add/remove ops between
 *  two surfaces via a normalised line-by-line set diff. Treats trivial
 *  whitespace as identical. Exported for tests + consumer-side validators. */
declare function countSentenceEdits(baseline: string, candidate: string): number;

export { type CampaignStorage as C, type GepaProposerOptions as G, type OpenAutoPrOptions as O, type RunCampaignOptions as R, type RunOptimizationOptions as a, type RunImprovementLoopResult as b, type RunImprovementLoopOptions as c, runImprovementLoop as d, type GepaProposerConstraints as e, fsCampaignStorage as f, gepaProposer as g, type OpenAutoPrResult as h, inMemoryCampaignStorage as i, type RunOptimizationResult as j, countSentenceEdits as k, defaultRenderDiff as l, extractH2Sections as m, runOptimization as n, openAutoPr as o, runCampaign as r, surfaceHash as s };
