Spaces:
Running
Running
File size: 11,075 Bytes
518343a | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 | /**
* @file runtime/src/rae1/validate.test.ts
* @description Vitest unit tests for RAE-1 schema validation.
*
* Lean ref: SZL.AGI.PACBayes.capability_improvement_rate_bound
* Lean file: Lutar/PACBayes/CapabilityImprovementRate.lean
* Lean commit: c4d1379568
*
* Acceptance criteria (per PR-1 spec):
* ✓ valid envelope passes
* ✓ missing lean_theorem_name fails
* ✓ sorry_undisclosed fails Doctrine v6 check
* ✓ < 3 judges fails
*/
import { describe, it, expect } from "vitest";
import {
validateRAE1Schema,
encodePayload,
decodePayload,
} from "./validate.js";
import type { DSSEEnvelope, RAE1Payload } from "./schema.js";
import { RAE1_PAYLOAD_TYPE, RAE1_SCHEMA_VERSION } from "./schema.js";
// ─── Test fixture helpers ─────────────────────────────────────────────────────
function makeValidPayload(overrides: Partial<RAE1Payload> = {}): RAE1Payload {
const base: RAE1Payload = {
schema_version: "rae1.0",
run_id: "f3a1b2c4-d5e6-7890-abcd-ef1234567890",
run_timestamp: "2026-05-27T18:34:00Z",
benchmark_name: "bench-2024",
benchmark_year: 2024,
harness_version: "v2.0.0",
harness_commit_sha: "3672670ee8be63aa5f116ca6124f3f3a4545b4e0",
problem_id: "bench-2024-A1",
problem_sha256: "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855",
domain: "combinatorics",
judges: [
{
judge_id: "judge-0-rigorous",
model_name: "claude-3-5-sonnet-20241022",
system_prompt_variant: "rigorous",
verdict: "SOLVED",
confidence_01: 0.91,
latency_ms: 1243,
token_usage: { input: 512, output: 384, total: 896 },
},
{
judge_id: "judge-1-creative",
model_name: "claude-3-5-sonnet-20241022",
system_prompt_variant: "creative",
verdict: "SOLVED",
confidence_01: 0.87,
latency_ms: 1109,
token_usage: { input: 512, output: 311, total: 823 },
},
{
judge_id: "judge-2-verification",
model_name: "gpt-4o-2024-11-20",
system_prompt_variant: "verification",
verdict: "UNCLEAR",
confidence_01: 0.55,
latency_ms: 987,
token_usage: { input: 512, output: 201, total: 713 },
},
],
ensemble_verdict: "SOLVED",
votes_solved: 2,
votes_unclear: 1,
votes_wrong: 0,
is_solved: true,
prev_hash: "GENESIS",
receipt_index: 0,
lean_theorem_name: "SZL.AGI.PACBayes.capability_improvement_rate_bound",
lean_theorem_file: "Lutar/PACBayes/CapabilityImprovementRate.lean",
lean_commit_sha: "c4d1379568abcdef1234567890abcdef12345678",
lean_repo: "szl-holdings/lutar-lean",
lean_build_status: "sorry_disclosed",
lean_sorry_count: 2,
staged_advisory: false,
};
return { ...base, ...overrides };
}
function makeValidEnvelope(payloadOverrides: Partial<RAE1Payload> = {}): DSSEEnvelope {
const payload = makeValidPayload(payloadOverrides);
return {
payloadType: RAE1_PAYLOAD_TYPE,
payload: encodePayload(payload),
signatures: [
{
keyid: "hmac-sha256:abc123",
sig: "dGVzdHNpZ25hdHVyZQ",
},
],
};
}
// ─── Tests ────────────────────────────────────────────────────────────────────
describe("validateRAE1Schema — valid envelopes", () => {
it("accepts a fully valid envelope with sorry_disclosed", () => {
const result = validateRAE1Schema(makeValidEnvelope());
expect(result.valid).toBe(true);
expect(result.errors).toHaveLength(0);
expect(result.schema_version).toBe("rae1.0");
expect(result.run_id).toBe("f3a1b2c4-d5e6-7890-abcd-ef1234567890");
});
it("accepts a green build (sorry_count=0)", () => {
const result = validateRAE1Schema(
makeValidEnvelope({ lean_build_status: "green", lean_sorry_count: 0 })
);
expect(result.valid).toBe(true);
expect(result.errors).toHaveLength(0);
});
it("accepts staged_advisory=true with notes", () => {
const result = validateRAE1Schema(
makeValidEnvelope({
staged_advisory: true,
staged_notes: "Mock judges — no real API keys",
})
);
expect(result.valid).toBe(true);
});
it("accepts non-GENESIS prev_hash for chained receipts", () => {
const prevHash = "a".repeat(64);
const result = validateRAE1Schema(
makeValidEnvelope({ prev_hash: prevHash, receipt_index: 1 })
);
expect(result.valid).toBe(true);
});
it("returns decoded payload object on success", () => {
const result = validateRAE1Schema(makeValidEnvelope());
expect(result.payload).not.toBeNull();
expect(result.payload?.benchmark_name).toBe("bench-2024");
});
});
describe("validateRAE1Schema — Lean theorem violations", () => {
it("fails when lean_theorem_name is missing", () => {
const payload = makeValidPayload();
// Cast to any to force deletion of required field
delete (payload as Record<string, unknown>)["lean_theorem_name"];
const envelope: DSSEEnvelope = {
payloadType: RAE1_PAYLOAD_TYPE,
payload: Buffer.from(JSON.stringify(payload), "utf8").toString("base64url"),
signatures: [{ keyid: "k1", sig: "s1" }],
};
const result = validateRAE1Schema(envelope);
expect(result.valid).toBe(false);
expect(result.errors.some((e) => e.includes("lean_theorem_name"))).toBe(true);
});
it("fails when lean_commit_sha is not a valid hex SHA", () => {
const result = validateRAE1Schema(
makeValidEnvelope({ lean_commit_sha: "not-a-sha" })
);
expect(result.valid).toBe(false);
expect(result.errors.some((e) => e.includes("lean_commit_sha"))).toBe(true);
});
it("fails when lean_build_status is 'sorry_undisclosed' (Doctrine v6)", () => {
const result = validateRAE1Schema(
makeValidEnvelope({
lean_build_status: "sorry_undisclosed",
lean_sorry_count: 3,
})
);
expect(result.valid).toBe(false);
const hasDoctrineError = result.errors.some(
(e) => e.includes("sorry_undisclosed") && e.includes("Doctrine v6")
);
expect(hasDoctrineError).toBe(true);
});
it("fails when lean_build_status is 'green' but lean_sorry_count > 0", () => {
const result = validateRAE1Schema(
makeValidEnvelope({ lean_build_status: "green", lean_sorry_count: 1 })
);
expect(result.valid).toBe(false);
expect(result.errors.some((e) => e.includes("lean_sorry_count"))).toBe(true);
});
it("fails when lean_build_status is an invalid value", () => {
const result = validateRAE1Schema(
makeValidEnvelope({ lean_build_status: "unknown_status" as never })
);
expect(result.valid).toBe(false);
});
});
describe("validateRAE1Schema — judge ensemble violations", () => {
it("fails with fewer than 3 judges", () => {
const twoJudges = makeValidPayload().judges.slice(0, 2);
const result = validateRAE1Schema(makeValidEnvelope({ judges: twoJudges }));
expect(result.valid).toBe(false);
const hasJudgeError = result.errors.some(
(e) => e.includes("judges") && e.includes("≥ 3")
);
expect(hasJudgeError).toBe(true);
});
it("fails with empty judges array", () => {
const result = validateRAE1Schema(makeValidEnvelope({ judges: [] }));
expect(result.valid).toBe(false);
});
it("fails when a judge has an invalid system_prompt_variant", () => {
const judges = makeValidPayload().judges.map((j, i) =>
i === 0 ? { ...j, system_prompt_variant: "adversarial" as never } : j
);
const result = validateRAE1Schema(makeValidEnvelope({ judges }));
expect(result.valid).toBe(false);
expect(result.errors.some((e) => e.includes("system_prompt_variant"))).toBe(true);
});
it("fails when confidence_01 is out of [0,1]", () => {
const judges = makeValidPayload().judges.map((j, i) =>
i === 0 ? { ...j, confidence_01: 1.5 } : j
);
const result = validateRAE1Schema(makeValidEnvelope({ judges }));
expect(result.valid).toBe(false);
expect(result.errors.some((e) => e.includes("confidence_01"))).toBe(true);
});
});
describe("validateRAE1Schema — envelope structure violations", () => {
it("fails when payloadType is wrong", () => {
const env = makeValidEnvelope();
const result = validateRAE1Schema({ ...env, payloadType: "application/json" });
expect(result.valid).toBe(false);
expect(result.errors.some((e) => e.includes("payloadType"))).toBe(true);
});
it("fails when payload is not a string", () => {
const env = makeValidEnvelope();
const result = validateRAE1Schema({ ...env, payload: 42 });
expect(result.valid).toBe(false);
});
it("fails when payload is corrupt base64url", () => {
const env = makeValidEnvelope();
const result = validateRAE1Schema({ ...env, payload: "!!!not-base64url!!!" });
expect(result.valid).toBe(false);
expect(result.errors.some((e) => e.includes("payload"))).toBe(true);
});
it("fails when signatures is empty array", () => {
const env = makeValidEnvelope();
const result = validateRAE1Schema({ ...env, signatures: [] });
expect(result.valid).toBe(false);
});
it("fails when envelope is null", () => {
const result = validateRAE1Schema(null);
expect(result.valid).toBe(false);
expect(result.schema_version).toBe("unknown");
});
it("fails when envelope is a string", () => {
const result = validateRAE1Schema("not-an-object");
expect(result.valid).toBe(false);
});
});
describe("validateRAE1Schema — chain linkage violations", () => {
it("fails when prev_hash is not GENESIS and not 64 hex chars", () => {
const result = validateRAE1Schema(
makeValidEnvelope({ prev_hash: "short", receipt_index: 1 })
);
expect(result.valid).toBe(false);
expect(result.errors.some((e) => e.includes("prev_hash"))).toBe(true);
});
it("fails when receipt_index is negative", () => {
const result = validateRAE1Schema(
makeValidEnvelope({ receipt_index: -1 })
);
expect(result.valid).toBe(false);
});
it("fails when is_solved is inconsistent with ensemble_verdict", () => {
const result = validateRAE1Schema(
makeValidEnvelope({ ensemble_verdict: "WRONG", is_solved: true })
);
expect(result.valid).toBe(false);
expect(result.errors.some((e) => e.includes("is_solved") && e.includes("inconsistent"))).toBe(true);
});
});
describe("encodePayload / decodePayload round-trip", () => {
it("round-trips a valid payload", () => {
const original = makeValidPayload();
const encoded = encodePayload(original);
const decoded = decodePayload(encoded);
expect(decoded).toEqual(original);
});
it("encodes to valid base64url (no + or / chars)", () => {
const encoded = encodePayload(makeValidPayload());
expect(encoded).not.toMatch(/[+/=]/);
});
});
|