// RagSample — entry in exports/rag/playbooks.jsonl. Spec shape exactly, // plus provenance + success_score (so the index can re-rank by quality). import { ValidationResult, requireString, requireNumber, requireIsoTimestamp, requireProvenance, requireStringArray, } from "./types"; export const RAG_SAMPLE_SCHEMA_VERSION = 1; export interface RagSample { schema_version: number; id: string; title: string; content: string; tags: string[]; source_run_id: string; success_score: "accepted" | "partially_accepted"; // RAG only ships from these two embedding_text: string; // the text to embed (often == content but can be shorter) exported_at: string; provenance: { source_file: string; line_offset?: number; sig_hash: string; recorded_at: string }; } export function validateRagSample(input: unknown): ValidationResult { const errors: string[] = []; if (typeof input !== "object" || input === null) return { valid: false, errors: ["expected object"] }; const r = input as Record; let ok = true; if (r.schema_version !== RAG_SAMPLE_SCHEMA_VERSION) { errors.push(`schema_version: expected ${RAG_SAMPLE_SCHEMA_VERSION}, got ${JSON.stringify(r.schema_version)}`); ok = false; } ok = requireString(r.id, "id", errors) && ok; ok = requireString(r.title, "title", errors) && ok; ok = requireString(r.content, "content", errors) && ok; ok = requireString(r.embedding_text, "embedding_text", errors) && ok; ok = requireString(r.source_run_id, "source_run_id", errors) && ok; ok = requireIsoTimestamp(r.exported_at, "exported_at", errors) && ok; ok = requireStringArray(r.tags, "tags", errors) && ok; ok = requireProvenance(r.provenance, "provenance", errors) && ok; if (!["accepted", "partially_accepted"].includes(r.success_score as string)) { errors.push("success_score: must be accepted|partially_accepted (rejected/needs_human never enter RAG)"); ok = false; } if (typeof r.content === "string" && (r.content as string).trim().length === 0) { errors.push("content: must be non-whitespace"); ok = false; } if (!ok) return { valid: false, errors }; return { valid: true, value: r as unknown as RagSample }; }