import { Type } from "typebox"; import { type ToolDefinition } from "./extensions/types.ts"; export type MetricDirection = "lower" | "higher"; export type ImprovementDecision = "keep" | "discard" | "retry" | "blocked"; export type ImprovementDecisionReason = "baseline" | "metric_improved" | "checks_failed" | "metric_missing" | "metric_invalid" | "not_better_than_best" | "below_min_delta" | "below_min_relative_delta" | "insufficient_noise_evidence" | "below_confidence"; export type ConfidenceMode = "none" | "mad"; export type LowConfidenceAction = "retry" | "discard"; export interface MetricComparison { current: number; best: number | null; direction: MetricDirection; delta: number; relativeDelta: number | null; improved: boolean; } export interface ConfidenceResult { mode: ConfidenceMode; value: number | null; noiseFloor: number | null; sampleCount: number; } export interface ImprovementDecisionInput { currentMetric: number | null | undefined; bestMetric?: number | null; baselineMetric?: number | null; direction?: MetricDirection; checksPass?: boolean | null; minDelta?: number; minRelativeDelta?: number; confidenceMode?: ConfidenceMode; minConfidence?: number; lowConfidenceAction?: LowConfidenceAction; noiseMetrics?: number[]; } export interface ImprovementDecisionResult { decision: ImprovementDecision; reason: ImprovementDecisionReason; comparison: MetricComparison | null; confidence: ConfidenceResult; } export interface ImprovementRunRecord { runId: string | number; objective?: string; hypothesis?: string; metricName: string; metricUnit?: string; direction: MetricDirection; metric: number | null; secondaryMetrics?: Record; checksPass: boolean | null; decision: ImprovementDecision; reason: ImprovementDecisionReason; confidence?: number | null; changedFiles?: string[]; evidenceRef?: string; nextHint?: string; timestamp: number; } export interface GitStatusEntry { index: string; workingTree: string; path: string; origPath?: string; } export interface OwnedDiscardPlanInput { beforeStatus: string | GitStatusEntry[]; afterStatus: string | GitStatusEntry[]; ownedPaths: string[]; preservePaths?: string[]; } export interface OwnedDiscardPlan { revertPaths: string[]; preservePaths: string[]; protectedUserDirtyPaths: string[]; unownedChangedPaths: string[]; canDiscardOwnedChanges: boolean; } export interface ImprovementLoopConfig { loopId: string; objective: string; metricName: string; metricUnit?: string; direction: MetricDirection; minDelta: number; minRelativeDelta: number; confidenceMode: ConfidenceMode; minConfidence: number; lowConfidenceAction: LowConfidenceAction; createdAt: number; cwd: string; } export interface ImprovementSandboxRecord { sandboxId: string; status: "active" | "cleaned"; repoPath: string; worktreePath: string; baseRef: string; createdAt: number; cleanedAt?: number; exportedAt?: number; patchPath?: string; patchBytes?: number; reason?: string; } export interface ImprovementLoopState { config: ImprovementLoopConfig; runs: ImprovementRunRecord[]; sandboxes: ImprovementSandboxRecord[]; activeSandbox: ImprovementSandboxRecord | null; baselineMetric: number | null; bestMetric: number | null; bestRunId: string | number | null; lastDecision: ImprovementDecisionResult | null; logPath: string; } export interface ImprovementLoopPaths { rootDir: string; workspaceDir: string; logPath: string; sandboxDir: string; artifactDir: string; workspaceKey: string; loopId: string; } export interface ImprovementLoopInitInput { cwd: string; objective: string; metricName: string; metricUnit?: string; direction?: MetricDirection; loopId?: string; minDelta?: number; minRelativeDelta?: number; confidenceMode?: ConfidenceMode; minConfidence?: number; lowConfidenceAction?: LowConfidenceAction; agentDir?: string; reset?: boolean; } export interface ImprovementLoopRecordInput { cwd: string; loopId?: string; runId?: string | number; metric: number | null | undefined; secondaryMetrics?: Record; checksPass?: boolean | null; hypothesis?: string; changedFiles?: string[]; evidenceRef?: string; nextHint?: string; agentDir?: string; } export interface ImprovementLoopStatusInput { cwd: string; loopId?: string; agentDir?: string; } export interface ImprovementSandboxCreateInput extends ImprovementLoopStatusInput { exec: ImprovementLoopExec; baseRef?: string; allowDirtyRepo?: boolean; sandboxId?: string; signal?: AbortSignal; } export interface ImprovementSandboxCleanupInput extends ImprovementLoopStatusInput { exec: ImprovementLoopExec; sandboxId?: string; reason?: string; signal?: AbortSignal; } export interface ImprovementSandboxExportInput extends ImprovementLoopStatusInput { exec: ImprovementLoopExec; sandboxId?: string; allowEmptyPatch?: boolean; signal?: AbortSignal; } export type ImprovementLoopToolAction = "init" | "status" | "record" | "measure" | "sandbox_create" | "sandbox_export" | "sandbox_cleanup"; export interface ImprovementMeasurementCommandResult { stdout: string; stderr: string; code: number; killed: boolean; stdoutTruncated?: boolean; stderrTruncated?: boolean; } export interface ImprovementMeasurementResult { command: string; exitCode: number; timedOut: boolean; durationMs: number; stdout: string; stderr: string; stdoutTruncated: boolean; stderrTruncated: boolean; parsedMetrics: Record; primaryMetric: number | null; checksCommand?: string; checksExitCode?: number; checksTimedOut?: boolean; checksPass: boolean; checksStdout?: string; checksStderr?: string; } export type ImprovementLoopExec = (command: string, args: string[], options?: { cwd?: string; timeout?: number; signal?: AbortSignal; maxBuffer?: number; }) => Promise; export interface ImprovementLoopToolDetails { action: ImprovementLoopToolAction; state: ImprovementLoopState | null; decision?: ImprovementDecisionResult; measurement?: ImprovementMeasurementResult; sandbox?: ImprovementSandboxRecord; logPath: string; } export declare function parseMetricLines(output: string): Map; export declare function metricMapFromOutput(output: string): Record; export declare function selectPrimaryMetric(metrics: Map | Record, metricName: string): number | null; export declare function compareMetric(current: number, best: number | null | undefined, direction?: MetricDirection): MetricComparison; export declare function median(values: number[]): number | null; export declare function medianAbsoluteDeviation(values: number[]): number | null; export declare function computeMadConfidence(improvementDelta: number, samples: number[]): ConfidenceResult; export declare function decideImprovement(input: ImprovementDecisionInput): ImprovementDecisionResult; export declare function serializeRunRecord(record: ImprovementRunRecord): string; export declare function appendRunRecord(filePath: string, record: ImprovementRunRecord): Promise; export declare function readRunRecords(filePath: string): Promise; export declare function improvementLoopPaths(input: ImprovementLoopStatusInput): ImprovementLoopPaths; export declare function initImprovementLoop(input: ImprovementLoopInitInput): Promise; export declare function readImprovementLoopState(input: ImprovementLoopStatusInput): Promise; export declare function createImprovementSandbox(input: ImprovementSandboxCreateInput): Promise; export declare function exportImprovementSandboxPatch(input: ImprovementSandboxExportInput): Promise; export declare function cleanupImprovementSandbox(input: ImprovementSandboxCleanupInput): Promise; export declare function recordImprovementRun(input: ImprovementLoopRecordInput): Promise; export declare function runImprovementMeasurement(input: { exec: ImprovementLoopExec; cwd: string; command: string; metricName: string; checksCommand?: string; timeoutSeconds?: number; checksTimeoutSeconds?: number; maxOutputBytes?: number; signal?: AbortSignal; }): Promise; export declare function parseGitPorcelainStatus(status: string): GitStatusEntry[]; export declare function planOwnedDiscard(input: OwnedDiscardPlanInput): OwnedDiscardPlan; declare const DecisionToolParams: Type.TObject<{ currentMetric: Type.TNumber; bestMetric: Type.TOptional; direction: Type.TUnsafe<"higher" | "lower">; checksPass: Type.TOptional; minDelta: Type.TOptional; minRelativeDelta: Type.TOptional; confidenceMode: Type.TOptional>; minConfidence: Type.TOptional; lowConfidenceAction: Type.TOptional>; noiseMetrics: Type.TOptional>; }>; export declare function createImprovementDecisionTool(): ToolDefinition; declare const LoopToolParams: Type.TObject<{ action: Type.TUnsafe<"init" | "measure" | "record" | "sandbox_cleanup" | "sandbox_create" | "sandbox_export" | "status">; loopId: Type.TOptional; objective: Type.TOptional; metricName: Type.TOptional; metricUnit: Type.TOptional; direction: Type.TOptional>; reset: Type.TOptional; minDelta: Type.TOptional; minRelativeDelta: Type.TOptional; confidenceMode: Type.TOptional>; minConfidence: Type.TOptional; lowConfidenceAction: Type.TOptional>; currentMetric: Type.TOptional; checksPass: Type.TOptional; hypothesis: Type.TOptional; secondaryMetrics: Type.TOptional>; changedFiles: Type.TOptional>; evidenceRef: Type.TOptional; nextHint: Type.TOptional; command: Type.TOptional; checksCommand: Type.TOptional; timeoutSeconds: Type.TOptional; checksTimeoutSeconds: Type.TOptional; maxOutputBytes: Type.TOptional; useSandbox: Type.TOptional; sandboxId: Type.TOptional; baseRef: Type.TOptional; allowDirtyRepo: Type.TOptional; cleanupReason: Type.TOptional; allowEmptyPatch: Type.TOptional; }>; export declare function createImprovementLoopTool(exec?: ImprovementLoopExec): ToolDefinition; export {}; //# sourceMappingURL=improvement-loop.d.ts.map