|
| 1 | +import type { |
| 2 | + HistoricalReplayCalibrationInput, |
| 3 | + Phase7CalibrationConfig, |
| 4 | + Phase7CalibrationLoopResult, |
| 5 | + Phase7CalibrationManifest, |
| 6 | + PrOutcomeCalibrationInput, |
| 7 | + ReplayHarnessStatus, |
| 8 | +} from "@jsonbored/gittensory-engine"; |
| 9 | + |
| 10 | +import type { AppendEventInput, LedgerEntry } from "./event-ledger.js"; |
| 11 | +import type { |
| 12 | + ObjectiveAnchorResult, |
| 13 | + ReplayPlanInput, |
| 14 | + RevealedHistoryEntry, |
| 15 | +} from "./replay-objective-anchor.js"; |
| 16 | + |
| 17 | +export const MINER_CALIBRATION_SNAPSHOT_EVENT: "calibration_snapshot"; |
| 18 | + |
| 19 | +/** One completed replay-run task result: what the replay targeted, and the revealed post-T history to score it. */ |
| 20 | +export interface ReplayTaskResult { |
| 21 | + replayPlan?: ReplayPlanInput | null; |
| 22 | + revealedHistory?: RevealedHistoryEntry[] | RevealedHistoryEntry | null; |
| 23 | +} |
| 24 | + |
| 25 | +export interface ScoreCompositeOptions { |
| 26 | + computeObjectiveAnchor?: ( |
| 27 | + input: { replayPlan?: ReplayPlanInput | null; revealedHistory?: RevealedHistoryEntry[] | RevealedHistoryEntry | null }, |
| 28 | + ) => ObjectiveAnchorResult; |
| 29 | +} |
| 30 | + |
| 31 | +export interface HistoricalReplayCompositeScore { |
| 32 | + compositeScore: number | null; |
| 33 | + sampleSize: number; |
| 34 | + scores: number[]; |
| 35 | +} |
| 36 | + |
| 37 | +export function scoreHistoricalReplayComposite( |
| 38 | + replayResults: readonly ReplayTaskResult[] | null | undefined, |
| 39 | + options?: ScoreCompositeOptions, |
| 40 | +): HistoricalReplayCompositeScore; |
| 41 | + |
| 42 | +/** A completed replay run's descriptor: its per-task results plus the run's identity/freshness/harness health. */ |
| 43 | +export interface ReplayRunDescriptor { |
| 44 | + replayResults?: readonly ReplayTaskResult[] | null; |
| 45 | + replayRunId?: string; |
| 46 | + observedAt?: string; |
| 47 | + harnessStatus?: ReplayHarnessStatus; |
| 48 | +} |
| 49 | + |
| 50 | +export interface BuiltHistoricalReplayInput { |
| 51 | + historicalReplay: HistoricalReplayCalibrationInput | null; |
| 52 | + compositeScore: number | null; |
| 53 | + sampleSize: number; |
| 54 | + scores: number[]; |
| 55 | +} |
| 56 | + |
| 57 | +export function buildHistoricalReplayCalibrationInput( |
| 58 | + replayRun: ReplayRunDescriptor | null | undefined, |
| 59 | + options?: ScoreCompositeOptions, |
| 60 | +): BuiltHistoricalReplayInput; |
| 61 | + |
| 62 | +/** The persisted, public-safe projection of a Phase7CalibrationLoopResult. */ |
| 63 | +export interface CalibrationSnapshotPayload { |
| 64 | + enabled: boolean; |
| 65 | + combinedAccuracy: number | null; |
| 66 | + baselineAccuracy: number; |
| 67 | + deltaFromBaseline: number | null; |
| 68 | + autonomyIncreasePermitted: boolean; |
| 69 | + replayHarnessHold: boolean; |
| 70 | + replayHarnessStatus: string; |
| 71 | + replayRunDue: boolean; |
| 72 | + holdReasons: string[]; |
| 73 | + contributingSources: string[]; |
| 74 | + replayRunId: string | null; |
| 75 | + observedAt: string | null; |
| 76 | + replaySampleSize: number; |
| 77 | +} |
| 78 | + |
| 79 | +export interface SnapshotMeta { |
| 80 | + replayRunId?: string | null; |
| 81 | + observedAt?: string | null; |
| 82 | + sampleSize?: number; |
| 83 | +} |
| 84 | + |
| 85 | +export function snapshotPayloadFromResult( |
| 86 | + result: Phase7CalibrationLoopResult, |
| 87 | + meta?: SnapshotMeta, |
| 88 | +): CalibrationSnapshotPayload; |
| 89 | + |
| 90 | +export function normalizeCalibrationSnapshotPayload(payload: unknown): CalibrationSnapshotPayload | null; |
| 91 | + |
| 92 | +export interface RecordCalibrationSnapshotOptions { |
| 93 | + /** Optional at the type level so a caller can pass an unusable ledger to exercise the fail-closed guard; the |
| 94 | + * writer throws `invalid_event_ledger` at runtime when this is absent or lacks `appendEvent`. */ |
| 95 | + eventLedger?: { appendEvent(event: AppendEventInput): LedgerEntry }; |
| 96 | + repoFullName?: string; |
| 97 | +} |
| 98 | + |
| 99 | +export function recordCalibrationSnapshot( |
| 100 | + input: unknown, |
| 101 | + options?: RecordCalibrationSnapshotOptions, |
| 102 | +): LedgerEntry | null; |
| 103 | + |
| 104 | +export interface CalibrationSnapshotReader { |
| 105 | + readEvents(filter?: { since?: number | null; repoFullName?: string | null }): unknown[]; |
| 106 | +} |
| 107 | + |
| 108 | +export interface CalibrationSnapshotFilter { |
| 109 | + since?: number | null; |
| 110 | + repoFullName?: string | null; |
| 111 | +} |
| 112 | + |
| 113 | +export interface PersistedCalibrationSnapshot extends CalibrationSnapshotPayload { |
| 114 | + repoFullName: string | null; |
| 115 | + seq: number | null; |
| 116 | + createdAt: string | null; |
| 117 | +} |
| 118 | + |
| 119 | +export function readCalibrationSnapshots( |
| 120 | + eventLedger: CalibrationSnapshotReader, |
| 121 | + filter?: CalibrationSnapshotFilter, |
| 122 | +): PersistedCalibrationSnapshot[]; |
| 123 | + |
| 124 | +export function latestCalibrationSnapshot( |
| 125 | + eventLedger: CalibrationSnapshotReader, |
| 126 | + filter?: CalibrationSnapshotFilter, |
| 127 | +): PersistedCalibrationSnapshot | null; |
| 128 | + |
| 129 | +export interface RunCalibrationCycleInput { |
| 130 | + config?: Phase7CalibrationConfig | Phase7CalibrationManifest | Record<string, unknown> | null; |
| 131 | + prOutcome?: PrOutcomeCalibrationInput | null; |
| 132 | + replayRun?: ReplayRunDescriptor | null; |
| 133 | + now?: string | Date | null; |
| 134 | + observedAt?: string | null; |
| 135 | + repoFullName?: string; |
| 136 | +} |
| 137 | + |
| 138 | +export interface RunCalibrationCycleDeps extends ScoreCompositeOptions { |
| 139 | + computeLoop?: (input: { |
| 140 | + config?: Phase7CalibrationConfig | Phase7CalibrationManifest | Record<string, unknown> | null; |
| 141 | + prOutcome?: PrOutcomeCalibrationInput | null; |
| 142 | + historicalReplay?: HistoricalReplayCalibrationInput | null; |
| 143 | + now?: string | Date | null; |
| 144 | + }) => Phase7CalibrationLoopResult; |
| 145 | + eventLedger?: { appendEvent(event: AppendEventInput): LedgerEntry }; |
| 146 | +} |
| 147 | + |
| 148 | +export interface RunCalibrationCycleResult { |
| 149 | + result: Phase7CalibrationLoopResult; |
| 150 | + snapshot: CalibrationSnapshotPayload; |
| 151 | + recorded: LedgerEntry | null; |
| 152 | + historicalReplay: HistoricalReplayCalibrationInput | null; |
| 153 | + compositeScore: number | null; |
| 154 | + sampleSize: number; |
| 155 | + scores: number[]; |
| 156 | +} |
| 157 | + |
| 158 | +export function runHistoricalReplayCalibrationCycle( |
| 159 | + input?: RunCalibrationCycleInput, |
| 160 | + deps?: RunCalibrationCycleDeps, |
| 161 | +): RunCalibrationCycleResult; |
0 commit comments