Some checks failed
lakehouse/auditor 1 blocking issue: todo!() macro call in tests/real-world/scrum_master_pipeline.ts
Build the contamination firewall: RAG, SFT, and Preference exporters
that turn scored evidence into clean training datasets without
leaking rejected, unvalidated, hallucinated, or provenance-free
records.
Files (8 new + 4 schema updates):
scripts/distillation/quarantine.ts shared QuarantineWriter, 11-reason taxonomy
scripts/distillation/export_rag.ts RAG exporter (--include-review opt-in)
scripts/distillation/export_sft.ts SFT exporter (--include-partial opt-in, SFT_NEVER constant)
scripts/distillation/export_preference.ts preference exporter, same task_id pairing
scripts/distillation/distill.ts CLI dispatcher (build-evidence/score/export-*)
tests/distillation/exports.test.ts 15 contamination-firewall tests
reports/distillation/phase4-export-report.md acceptance report
Schema field-name alignment with now.md:
rag_sample.ts +source_category, exported_at→created_at
sft_sample.ts +id, exported_at→created_at, partially_accepted at schema (CLI gates)
preference_sample.ts +id, source_run_ids→chosen_run_id+rejected_run_id, +created_at
Test metrics: 117 distillation tests pass · 0 fail · 315 expects · 327ms
Real-data export run (1052 scored input rows):
RAG: 446 exported (351 acc + 95 partial), 606 quarantined
SFT: 351 exported (all 'accepted'), 701 quarantined
Preference: 83 pairs exported, 16 quarantined
CONTAMINATION FIREWALL — verified held on real data:
- SFT output: 351/351 quality_score='accepted' (ZERO leaked)
- RAG output: 351 acc + 95 partial (ZERO rejected leaked)
- Preference: 0 self-pairs (chosen_run_id != rejected_run_id)
- 536 rejected+needs_human_review records caught at unsafe_sft_category
gate, exact match to scored-runs forbidden-category total
Defense in depth (the firewall is two layers, not one):
1. Schema layer (Phase 1): SftSample.quality_score enum forbids
rejected/needs_human at write time
2. Exporter layer: SFT_NEVER constant in export_sft.ts checks
category before synthesis. Even if synthesis produced a row
with quality_score=rejected, validateSftSample would reject it.
Quarantine reasons (11): missing_provenance, missing_source_run_id,
empty_content, schema_violation, unsafe_sft_category,
unsafe_rag_category, invalid_preference_pairing,
hallucinated_file_path, duplicate_id, self_pairing,
category_disallowed.
Bug surfaced + fixed during testing: module-level evidenceCache
shared state across test runs (tests wipe TMP, cache holds stale
empty Map). Moved cache to per-call scope. Same pattern bit Phase 2
materializer would have hit if its tests had multiple runs sharing
state — preventive fix.
Pairing logic v1: same task_id with category gap. accepted×rejected
preferred, accepted×partially_accepted as fallback. MAX_PAIRS_PER_TASK=5
cap prevents one hot task from dominating. Future: cross-source
pairing (scrum_reviews chosen vs observer_reviews rejected on same
file) to grow dataset beyond 83.
CLI: ./scripts/distill.ts {build-evidence|score|export-rag|export-sft|export-preference|export-all|health}
Flags: --dry-run, --include-partial (SFT only), --include-review (RAG only)
Carry-overs to Phase 5 (Receipts Harness):
- Each exporter currently writes results but no per-stage receipt.json.
Phase 5 wraps build_evidence_index + score_runs + export_* in a
withReceipt() helper that captures git_sha + sha256 of inputs/outputs
+ record_counts + validation_pass.
- reports/distillation/latest.md aggregating most-recent run of each stage.
Carry-overs to Phase 3 v2:
- mode_experiments scoring (168 needs_human_review): derive markers from
validation_results.grounded_fraction
- extraction-class JOIN: distilled_*/audit_facts/observer_escalations
→ JOIN to verdict-bearing parent by task_id
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
70 lines
3.2 KiB
TypeScript
70 lines
3.2 KiB
TypeScript
// SftSample — entry in exports/sft/instruction_response.jsonl. Spec
|
|
// non-negotiable: ONLY accepted runs, never partial/rejected/needs_human.
|
|
// Validator enforces that invariant — exporters can't bypass.
|
|
import {
|
|
ValidationResult, requireString, requireIsoTimestamp, requireProvenance, requireNumber,
|
|
} from "./types";
|
|
|
|
export const SFT_SAMPLE_SCHEMA_VERSION = 1;
|
|
|
|
// SFT default: only `accepted` ships. With --include-partial CLI flag,
|
|
// `partially_accepted` becomes legal. `rejected` and `needs_human_review`
|
|
// NEVER ship to SFT — that's the contamination firewall.
|
|
export const SFT_QUALITY_SCORES = ["accepted", "partially_accepted"] as const;
|
|
export type SftQualityScore = (typeof SFT_QUALITY_SCORES)[number];
|
|
|
|
export interface SftSample {
|
|
schema_version: number;
|
|
id: string;
|
|
instruction: string; // the prompt / user message
|
|
context: string; // retrieved context that was visible (empty string allowed; null/undefined not)
|
|
response: string; // the model output that was accepted
|
|
source_run_id: string;
|
|
quality_score: SftQualityScore;
|
|
created_at: string;
|
|
provenance: { source_file: string; line_offset?: number; sig_hash: string; recorded_at: string };
|
|
}
|
|
|
|
export function validateSftSample(input: unknown): ValidationResult<SftSample> {
|
|
const errors: string[] = [];
|
|
if (typeof input !== "object" || input === null) return { valid: false, errors: ["expected object"] };
|
|
const r = input as Record<string, unknown>;
|
|
let ok = true;
|
|
|
|
if (r.schema_version !== SFT_SAMPLE_SCHEMA_VERSION) {
|
|
errors.push(`schema_version: expected ${SFT_SAMPLE_SCHEMA_VERSION}, got ${JSON.stringify(r.schema_version)}`);
|
|
ok = false;
|
|
}
|
|
ok = requireString(r.id, "id", errors) && ok;
|
|
ok = requireString(r.instruction, "instruction", errors) && ok;
|
|
ok = requireString(r.response, "response", errors) && ok;
|
|
ok = requireString(r.source_run_id, "source_run_id", errors) && ok;
|
|
ok = requireIsoTimestamp(r.created_at, "created_at", errors) && ok;
|
|
ok = requireProvenance(r.provenance, "provenance", errors) && ok;
|
|
|
|
// Empty pair guard.
|
|
if (typeof r.instruction === "string" && (r.instruction as string).trim().length === 0) {
|
|
errors.push("instruction: must be non-whitespace (no empty pairs)");
|
|
ok = false;
|
|
}
|
|
if (typeof r.response === "string" && (r.response as string).trim().length === 0) {
|
|
errors.push("response: must be non-whitespace (no empty pairs)");
|
|
ok = false;
|
|
}
|
|
// Context is required-string but empty is allowed (some SFT samples
|
|
// are pure instruction→response with no retrieval context).
|
|
if (typeof r.context !== "string") {
|
|
errors.push("context: expected string (use empty string for no-context samples)");
|
|
ok = false;
|
|
}
|
|
// The non-negotiable: SFT samples MUST have quality_score in
|
|
// SFT_QUALITY_SCORES. Anything else is a leak.
|
|
if (!SFT_QUALITY_SCORES.includes(r.quality_score as SftQualityScore)) {
|
|
errors.push(`quality_score: must be one of ${SFT_QUALITY_SCORES.join("|")} (no rejected/needs_human leak into SFT — spec non-negotiable). Got ${JSON.stringify(r.quality_score)}`);
|
|
ok = false;
|
|
}
|
|
|
|
if (!ok) return { valid: false, errors };
|
|
return { valid: true, value: r as unknown as SftSample };
|
|
}
|