// Quality Intelligence run plan + versioned run-event envelope (Epic #270, Issue #277).
//
// The run plan describes the deterministic stage sequence a QI run will execute. The
// run-event envelope is the wire-stable record of what actually happened. Both are
// versioned via the `eventSchemaVersion` literal so future evolutions land as new
// literal members rather than mutating "1" (same rule as MEMORY_SCHEMA_VERSION /
// CONNECTED_CONTEXT_SCHEMA_VERSION).
//
// Events carry a monotonic per-run `sequence` integer and an ISO 8601 `timestamp`.
// The runtime (#273) is responsible for assigning sequences in the order events are
// observed.
export const QUALITY_INTELLIGENCE_EVENT_SCHEMA_VERSION = 1;
export const QUALITY_INTELLIGENCE_PLANNER_KINDS = [
    "scripted",
    "model-routed",
];
export const QUALITY_INTELLIGENCE_RUN_EVENT_KINDS = [
    "run:queued",
    "run:started",
    "stage:started",
    "stage:completed",
    "stage:failed",
    "candidate:proposed",
    "finding:recorded",
    "review:requested",
    "review:completed",
    "run:succeeded",
    "run:failed",
    "run:cancelled",
];
/**
 * Assert sequence numbers in `events` are strictly increasing. Returns `void` on
 * success; throws `RangeError` on the first violation (including non-integer or
 * negative values).
 */
export const assertRunEventSequenceMonotonic = (events) => {
    let previous = -1;
    for (let index = 0; index < events.length; index += 1) {
        const event = events[index];
        if (event === undefined) {
            throw new RangeError(`Run event[${String(index)}] is missing`);
        }
        if (!Number.isInteger(event.sequence) || event.sequence < 0) {
            throw new RangeError(`Run event[${String(index)}] has invalid sequence ${String(event.sequence)}`);
        }
        if (event.sequence <= previous) {
            throw new RangeError(`Run event[${String(index)}] sequence ${String(event.sequence)} is not strictly greater than previous ${String(previous)}`);
        }
        previous = event.sequence;
    }
};
