import { type OptimizeTargetSet } from "./targets.js"; /** * Replaces an optimized variable's initializer expression. `value` is * Agency source text including the quotes; `expected` optionally guards * against stale catalogs by matching the target's current value — the * decoded text for string targets, the formatter-exact source text * (quotes intact) for literal targets, i.e. exactly `OptimizeTarget.value`. * * ```ts * { target: "foo.agency:bar:prompt", kind: "variable", op: "replaceInitializer", * value: "\"new prompt\"", expected: "old prompt", rationale: "Clearer." } * ``` */ export type ReplaceVariableInitializerOperation = { target: string; kind: "variable"; op: "replaceInitializer"; value: string; expected?: string; rationale?: string; }; /** * Reserved for `optimize type` targets. Representable so LLM proposals can * carry it without a schema change when type support lands, but rejected by * validation in v1. */ export type ReplaceTypeDefinitionOperation = { target: string; kind: "type"; op: "replaceTypeDefinition"; value: string; expected?: string; rationale?: string; }; /** One declarative source edit, discriminated by target kind. This is the * shape LLM mutation proposals carry. */ export type OptimizeMutationOperation = ReplaceVariableInitializerOperation | ReplaceTypeDefinitionOperation; /** * A structured reason an operation was rejected, fed back into the mutator * prompt for the retry. * * ```ts * { target: "foo.agency:bar:prompt", code: "interpolation-mismatch", * message: "you removed ${text} from the prompt" } * ``` */ export type OptimizeMutationDiagnostic = { target?: string; code: "unknown-target" | "kind-mismatch" | "unsupported-operation" | "expected-mismatch" | "invalid-replacement-syntax" | "unsupported-value-domain" | "interpolation-mismatch" | "type-mismatch" | "duplicate-target-operation" | "parse-failed"; message: string; }; /** * The target-level record of one applied operation, as written to * `mutation.json`. Old/new values use the target's value representation: * decoded text (no quotes) for string targets, formatter-exact source * text for literal targets. * * ```ts * { target: "foo.agency:bar:prompt", kind: "variable", op: "replaceInitializer", * oldValue: "old prompt", newValue: "new prompt", rationale: "Clearer." } * ``` */ export type OptimizeAppliedChange = { target: string; kind: OptimizeMutationOperation["kind"]; op: OptimizeMutationOperation["op"]; oldValue: string; newValue: string; rationale?: string; }; /** * The in-memory result of applying one operation batch: everything needed * to materialize, inspect, or reject a candidate without touching disk. * * ```ts * { files: { "foo.agency": "optimize const prompt = \"new\"\n..." }, * changes: [...OptimizeAppliedChange], diff: "--- foo.agency\n...", * diagnostics: [], targetSet: {...updated catalog} } * ``` */ export type OptimizeMutationPreview = { /** Full candidate file set (changed and unchanged discovered Agency files), * keyed by relative path. Empty when `diagnostics` is non-empty. */ files: Record; changes: OptimizeAppliedChange[]; diff: string; diagnostics: OptimizeMutationDiagnostic[]; /** Target set for the candidate file set: changed entries refreshed, the * rest carried over. Equals the input set when validation fails. */ targetSet: OptimizeTargetSet; }; /** * Applies declarative mutation operations to a discovered optimize target * set. Validation and rendering happen against the sources captured at * discovery time; nothing is read from disk. Batches are atomic: one invalid * operation means no files are produced. */ export declare class OptimizeSourceMutator { private readonly targetSet; private readonly targetsById; constructor(args: { targetSet: OptimizeTargetSet; }); preview(operations: OptimizeMutationOperation[]): OptimizeMutationPreview; /** * Shorthand over the operation API: infers the operation from the * discovered target kind. Unknown or unsupported target kinds are * rejected through diagnostics rather than guessed at. */ mutate(target: string, value: string): OptimizeMutationPreview; /** * Writes a validated preview. With a destination directory, the full * candidate file set is written under it, preserving relative paths * (used to materialize `iter-N/agent/`). Without one, only changed * files are written back to their original source paths — callers own * any hash/staleness verification before asking for that. */ apply(preview: OptimizeMutationPreview, destination?: string): void; /** * Applies all resolved operations to in-memory parses of the touched * files, renders them, and round-trip-parses the rendered output to * refresh target entries. Only files named by operations are ever parsed * (2 parses each); everything else passes through as captured source. */ private renderPreview; private renderFile; private abortedPreview; private validateOperations; private validateOperation; private resolveReplacementValue; } /** Preview a set of operations against a target set. Shared by greedy and GEPA. */ export declare function defaultPreview(targetSet: OptimizeTargetSet, operations: OptimizeMutationOperation[]): OptimizeMutationPreview;