root 1b433a9308 distillation: Phase 6 — acceptance gate suite
End-to-end fixture-driven gate. Runs the entire pipeline (collect →
score → export-rag → export-sft → export-preference) on a deterministic
fixture, asserts 22 invariants, runs a SECOND time with the same
recorded_at, and verifies hash reproducibility. Exits non-zero on any
failure. Pure observability — no scoring/filtering/schema changes.

Files (3 new + 1 modified + 6 fixture jsonls):
  scripts/distillation/acceptance.ts                    330 lines, runner + 22 checks
  reports/distillation/phase6-acceptance-report.md       autogenerated by run
  scripts/distillation/distill.ts                        +run-all, +receipts, +acceptance subcommands

  tests/fixtures/distillation/acceptance/data/_kb/
    scrum_reviews.jsonl    5 rows (accepted/partial/needs_human/scratchpad/missing-provenance)
    audits.jsonl           3 rows (info/high+PRD-drift/medium severity)
    auto_apply.jsonl       2 rows (committed, build_red_reverted)
    contract_analyses.jsonl 2 rows (accept, reject)
    observer_reviews.jsonl 2 rows (accept, reject — pair candidates)
    distilled_facts.jsonl  1 extraction-class row

Spec cases covered (now.md Phase 6):
  ✓ accepted          — Row #1 scrum, #6 audit-info, #11 contract-accept, #14 obs-accept
  ✓ partially_accepted — Row #2 scrum (3 attempts), #8 audit-medium
  ✓ rejected           — #7 audit-high, #10 auto_apply build_red, #12 contract-reject, #15 obs-reject
  ✓ needs_human_review — #3 scrum (no markers), #13 distilled extraction-class
  ✓ missing provenance — Row #5 scrum (no reviewed_at) → routed to skips
  ✓ valid preference pair — observer_reviews accept+reject on same file
  ✓ invalid preference pair — quarantine reasons populated when generated
  ✓ scratchpad / tree-split — Row #4 scrum tree_split_fired=true with multi-shard text
  ✓ PRD drift — Row #7 audit severity=high, topic="PRD drift: circuit breaker shipped claim"

Acceptance run results (run_id: acceptance-run-1-stable):
  22/22 invariants PASS
  Pipeline counts:
    collect:           14 records out, 1 skipped (missing-provenance fixture)
    score:             accepted=6 rejected=4 quarantined=4
    export-rag:        7 rows (5 acc + 2 partial, ZERO rejected)
    export-sft:        5 rows (all 'accepted', ZERO partial without --include-partial)
    export-preference: 2 pairs (zero self-pairs, zero identical-text)

Hash reproducibility — bit-for-bit identical:
  run_hash: 3ea12b160ee9099a3c52fe6e7fffd3076de7920d2704d24c789260d63cb1a5a2
  Two runs of the entire pipeline on the same fixture with the same
  recorded_at produce byte-identical outputs.

The 22 invariants:
  1-4.  Receipts + summary.json + summary.md + drift.json exist
  5-7.  StageReceipt + RunSummary + DriftReport schemas all valid
  8-10. SFT contains accepted only — no rejected/needs_human/partial leak
  11-12. RAG contains accepted+partial — zero rejected
  13-15. Preference: ≥1 pair, zero self-pairs, zero identical text
  16.   Every export row has 64-char hex provenance.sig_hash
  17.   Phase 2 missing-provenance row routed to distillation_skips.jsonl
  18.   SFT quarantine populated (6 unsafe_sft_category entries)
  19.   Scratchpad/tree-split fixture row materialized
  20.   PRD drift fixture row materialized
  21.   Per-stage output_hash identical across runs (0 mismatches)
  22.   run_hash identical across runs (bit-for-bit)

CLI:
  ./scripts/distill.ts acceptance     # exits 0 on pass, 1 on fail
  ./scripts/distill.ts run-all        # full pipeline with receipts
  ./scripts/distill.ts receipts --run-id <id>

Cumulative test metrics:
  135 distillation tests pass · 0 fail · 353 expect() calls · 1411ms
  (Phase 6 adds the runtime acceptance gate, not new unit tests —
   the acceptance script IS the integration test, callable from CI.)

What this proves:
- Distillation pipeline is SAFE (contamination firewall held under
  adversarial fixture)
- Distillation pipeline is REPRODUCIBLE (identical input → bit-identical
  output across two runs)
- Distillation pipeline is GATED (every now.md invariant has a
  deterministic assertion that exits non-zero on failure)

The 6-phase distillation substrate is now training-safe. RAG (446),
SFT (351 strict-accepted), and Preference (83 paired) datasets on
real lakehouse data each carry full provenance back to source rows
through the verified Phase 2 → Phase 3 → Phase 4 chain, with Phase 5
receipts capturing every input/output sha256 + per-stage validation,
and Phase 6 proving the whole chain is gate-tight on a deterministic
fixture.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-04-26 23:19:56 -05:00

139 lines
6.8 KiB
TypeScript

// distill.ts — single-entry CLI dispatcher for the distillation
// pipeline. Mirrors the spec's `./scripts/distill <command>` shape.
//
// USAGE
// bun run scripts/distillation/distill.ts <command> [flags]
//
// COMMANDS
// build-evidence materialize EvidenceRecord rows from data/_kb/*.jsonl
// score run deterministic Success Scorer
// export-rag RAG export (--include-review opt-in)
// export-sft SFT export (--include-partial opt-in)
// export-preference preference export
// export-all RAG + SFT + preference (no opt-ins by default)
// health evidence health audit
//
// All commands accept --dry-run.
import { materializeAll } from "./build_evidence_index";
import { scoreAll } from "./score_runs";
import { exportRag } from "./export_rag";
import { exportSft } from "./export_sft";
import { exportPreference } from "./export_preference";
import { runAllWithReceipts } from "./receipts";
import { TRANSFORMS } from "./transforms";
import { spawnSync } from "node:child_process";
const DEFAULT_ROOT = process.env.LH_DISTILL_ROOT ?? "/home/profit/lakehouse";
async function main() {
const cmd = process.argv[2];
const dry_run = process.argv.includes("--dry-run");
const include_partial = process.argv.includes("--include-partial");
const include_review = process.argv.includes("--include-review");
const recorded_at = new Date().toISOString();
switch (cmd) {
case "build-evidence": {
const r = await materializeAll({ root: DEFAULT_ROOT, transforms: TRANSFORMS, recorded_at, dry_run });
console.log(`[build-evidence] in=${r.totals.rows_read} out=${r.totals.rows_written} skip=${r.totals.rows_skipped} dedup=${r.totals.rows_deduped}`);
if (!dry_run) console.log(`[build-evidence] receipt: ${r.receipt_path}`);
if (!r.receipt.validation_pass) process.exit(1);
break;
}
case "score": {
const r = await scoreAll({ root: DEFAULT_ROOT, recorded_at, dry_run });
const c = r.totals.by_category;
console.log(`[score] in=${r.totals.rows_read} out=${r.totals.rows_written} acc=${c.accepted ?? 0} part=${c.partially_accepted ?? 0} rej=${c.rejected ?? 0} hum=${c.needs_human_review ?? 0}`);
if (!dry_run) console.log(`[score] receipt: ${r.receipt_path}`);
break;
}
case "export-rag": {
const r = await exportRag({ root: DEFAULT_ROOT, recorded_at, include_review, dry_run });
console.log(`[export-rag] in=${r.records_read} out=${r.records_exported} ${r.quarantine_summary}`);
console.log(`[export-rag] output: ${r.output_path}${include_review ? " (review included)" : ""}`);
break;
}
case "export-sft": {
const r = await exportSft({ root: DEFAULT_ROOT, recorded_at, include_partial, dry_run });
console.log(`[export-sft] in=${r.records_read} out=${r.records_exported} ${r.quarantine_summary}`);
console.log(`[export-sft] output: ${r.output_path}${include_partial ? " (partial included)" : ""}`);
break;
}
case "export-preference": {
const r = await exportPreference({ root: DEFAULT_ROOT, recorded_at, dry_run });
console.log(`[export-preference] in=${r.records_read} pairs=${r.pairs_exported} task_ids_paired=${r.task_ids_with_pairs} ${r.quarantine_summary}`);
console.log(`[export-preference] output: ${r.output_path}`);
break;
}
case "export-all": {
const rRag = await exportRag({ root: DEFAULT_ROOT, recorded_at, include_review, dry_run });
const rSft = await exportSft({ root: DEFAULT_ROOT, recorded_at, include_partial, dry_run });
const rPref = await exportPreference({ root: DEFAULT_ROOT, recorded_at, dry_run });
console.log("");
console.log("─── export-all summary ───");
console.log(` RAG: in=${rRag.records_read} out=${rRag.records_exported} ${rRag.quarantine_summary}`);
console.log(` SFT: in=${rSft.records_read} out=${rSft.records_exported} ${rSft.quarantine_summary}`);
console.log(` Preference: in=${rPref.records_read} pairs=${rPref.pairs_exported} ${rPref.quarantine_summary}`);
break;
}
case "run-all": {
// Phase 5 entry — full pipeline with structured receipts.
const r = await runAllWithReceipts({ root: DEFAULT_ROOT, include_partial, include_review });
console.log(`[run-all] run_id=${r.run_id} overall_passed=${r.summary.overall_passed}`);
console.log(`[run-all] datasets: rag=${r.summary.rag_records} sft=${r.summary.sft_records} pref=${r.summary.preference_pairs}`);
console.log(`[run-all] drift severity=${r.drift.severity}`);
console.log(`[run-all] reports/distillation/${r.run_id}/summary.md`);
if (!r.summary.overall_passed) process.exit(1);
break;
}
case "acceptance": {
// Phase 6 — fixture-driven end-to-end gate. Spawns the dedicated
// acceptance script so its non-zero exit propagates.
const r = spawnSync("bun", ["run", "scripts/distillation/acceptance.ts"], {
cwd: DEFAULT_ROOT, stdio: "inherit",
});
process.exit(r.status ?? 1);
}
case "receipts": {
// Read receipts for a previously-run pipeline.
const idx = process.argv.indexOf("--run-id");
if (idx < 0 || !process.argv[idx + 1]) {
console.error("usage: distill.ts receipts --run-id <id>");
process.exit(2);
}
const run_id = process.argv[idx + 1];
const path = `${DEFAULT_ROOT}/reports/distillation/${run_id}/summary.md`;
// Defer to bun's file APIs to keep this lean.
const { readFileSync } = await import("node:fs");
try { console.log(readFileSync(path, "utf8")); }
catch { console.error(`run not found: ${path}`); process.exit(2); }
break;
}
case "health":
case "help":
case undefined: {
console.log("Usage: bun run scripts/distillation/distill.ts <command> [flags]");
console.log("");
console.log("Commands:");
console.log(" build-evidence materialize EvidenceRecord rows");
console.log(" score run deterministic Success Scorer");
console.log(" export-rag RAG export (--include-review opt-in)");
console.log(" export-sft SFT export (--include-partial opt-in)");
console.log(" export-preference preference export");
console.log(" export-all RAG + SFT + preference");
console.log(" run-all full pipeline with structured receipts (Phase 5)");
console.log(" receipts read summary for a run (--run-id <id>)");
console.log(" acceptance fixture-driven end-to-end gate (Phase 6)");
console.log("");
console.log("Flags: --dry-run, --include-partial, --include-review");
break;
}
default:
console.error(`unknown command: ${cmd}. Try 'help'.`);
process.exit(2);
}
}
main().catch(e => { console.error(e); process.exit(1); });