import { FallbackLlmClient } from './fallback-llm.js';
import { GatewayConfig } from './types.js';

/**
 * calibration.ts — Prediction-Error-Driven Model-User Calibration
 *
 * Analyzes patterns in user corrections to identify systematic miscalibration
 * between the model's predictions and the user's actual expectations.
 * During consolidation, replays chains of similar corrections through an LLM
 * to synthesize CalibrationRules that adjust model behavior for this specific user.
 *
 * Inspired by:
 * - Cerebellar motor calibration (prediction errors drive lasting adjustments)
 * - Temporal difference learning (dopamine signals prediction error)
 * - Tesla FSD shadow mode (divergence between prediction and reality = training signal)
 */

interface CalibrationRule {
    id: string;
    ruleType: "model_tendency" | "user_expectation" | "scope_boundary" | "verification_required";
    condition: string;
    modelTendency: string;
    userExpectation: string;
    calibration: string;
    confidence: number;
    evidenceCount: number;
    evidenceCorrectionIds: string[];
    createdAt: string;
    lastReinforcedAt: string;
}
interface CalibrationIndex {
    rules: CalibrationRule[];
    updatedAt: string;
    totalCorrectionsAnalyzed: number;
}
declare function readCalibrationIndex(memoryDir: string): Promise<CalibrationIndex>;
interface CorrectionMemory {
    id: string;
    content: string;
    created: string;
    confidence: number;
    entityRefs: string[];
    tags: string[];
}
/**
 * Exported for entity-contamination R-11 regression coverage (#682
 * PR 2/3 — codex review).  Tests can drive the real correction-reading
 * path instead of duplicating the regex inline, so calibration parser
 * regressions surface via the contamination suite.
 */
declare function readCalibrationCorrections(memoryDir: string): Promise<CorrectionMemory[]>;
declare function synthesizeCalibrationRules(corrections: CorrectionMemory[], llm: FallbackLlmClient, existingRules: CalibrationRule[], agentId?: string): Promise<CalibrationRule[]>;
/**
 * Build a recall section from calibration rules relevant to the current query.
 * Uses the LLM to select which rules apply to the current context.
 */
declare function buildCalibrationRecallSection(rules: CalibrationRule[], query: string, maxChars?: number): string | null;
/**
 * Run the full calibration pipeline:
 * 1. Read all corrections
 * 2. Send to LLM for clustering and rule synthesis
 * 3. Merge with existing rules
 * 4. Write updated index
 */
declare function runCalibrationConsolidation(options: {
    memoryDir: string;
    gatewayConfig?: GatewayConfig;
    gatewayAgentId?: string;
}): Promise<CalibrationRule[]>;
/**
 * Standalone entry point for calibration consolidation that can be called
 * independently of weekly compounding. The compounding engine's
 * `synthesizeWeekly()` is one trigger, but orchestrators or periodic
 * maintenance jobs should call this directly so calibration is not gated
 * on weekly compounding being enabled.
 */
declare function runCalibrationIfEnabled(options: {
    memoryDir: string;
    calibrationEnabled: boolean;
    gatewayConfig?: GatewayConfig;
}): Promise<CalibrationRule[]>;
/**
 * Get calibration rules for recall injection.
 * Reads the pre-computed calibration index.
 */
declare function getCalibrationRulesForRecall(memoryDir: string): Promise<CalibrationRule[]>;

export { type CalibrationIndex, type CalibrationRule, type CorrectionMemory, buildCalibrationRecallSection, getCalibrationRulesForRecall, readCalibrationCorrections, readCalibrationIndex, runCalibrationConsolidation, runCalibrationIfEnabled, synthesizeCalibrationRules };
