supercode-harness 0.4.12

The optional native Supercode agent and tool harness
Documentation
//! P6 gate 1 (`docs/composable-harness/COMPOSABLE-HARNESS-DESIGN.md` §5.2):
//! "Preset×corpus audit runs (`audit_dir` per corpus, thresholds on
//! `Unmodeled`/`Dropped`)."
//!
//! This is the REGRESSION FLOOR on top of the audit harness
//! (`crates/harness/src/audit.rs`): every corpus this repo carries a committed,
//! always-available real/genuine fixture set for (Codex, Pi, OpenCode,
//! ClaudeCode — see each corpus's own `*_real_corpus.rs`/`*_sqlite_fixture.rs`
//! module doc for that fixture's provenance) is audited here, and BOTH
//! `Coverage::Unmodeled` and `Coverage::Dropped` are asserted to be no worse
//! than the CURRENT, measured-clean level. `Unmodeled` is already
//! independently pinned at exactly 0 by each corpus's own dev/01 test
//! (`codex_real_corpus_directory_audit_has_zero_gaps_over_genuine_output`,
//! `pi_real_corpus_directory_audit_has_zero_gaps_over_genuine_output`,
//! `opencode_fixture_has_no_unknown_records_or_parts`,
//! `fixtures_have_no_unknown_records`) — this file restates that pin
//! CONSOLIDATED, in one place, alongside the NEW assertion those tests don't
//! make: a `Dropped`-count ceiling. `Dropped` is the "parsed, understood,
//! intentionally not carried" bucket (audit.rs's `Coverage::Dropped` doc
//! comment) — today's Dropped set is enumerated and justified (UI echoes,
//! provider-private reasoning text, non-canonicalizable file residue, …); a
//! regression that makes the loader silently give up on something it used to
//! carry (demoting a `Normalized`/`Retained` record to `Dropped`, or growing
//! the Dropped tally on the SAME fixtures) trips this gate. A regression that
//! makes something fall all the way to `Unmodeled` trips the (stricter, ==0)
//! ceiling first.
//!
//! Every threshold below is the EXACT count measured against this worktree's
//! committed fixtures at HEAD — not a rounded-down/loosened guess (`cargo
//! test -p supercode-core --test audit_fidelity_gate -- --nocapture` prints
//! the measured breakdown any time this needs re-pinning after a deliberate,
//! reviewed fidelity change).

use std::path::{Path, PathBuf};

use supercode_harness::audit::{audit_dir, Corpus, Coverage};

fn fixture(name: &str) -> PathBuf {
    Path::new(env!("CARGO_MANIFEST_DIR"))
        .join("tests/fixtures")
        .join(name)
}

/// Copy `files` (each resolved via [`fixture`]) into a fresh temp dir and
/// audit it as `corpus`. Returns the report; caller owns cleanup of `tmp`.
fn audit_fixture_set(
    tag: &str,
    files: &[&str],
    corpus: Corpus,
) -> supercode_harness::audit::Report {
    let tmp = std::env::temp_dir().join(format!("sc-p6-audit-gate-{}-{}", std::process::id(), tag));
    std::fs::create_dir_all(&tmp).unwrap();
    for name in files {
        std::fs::copy(fixture(name), tmp.join(name)).unwrap();
    }
    let report = audit_dir(&tmp, corpus, None);
    std::fs::remove_dir_all(&tmp).ok();
    report
}

fn unmodeled_count(report: &supercode_harness::audit::Report) -> u64 {
    let mut n: u64 = 0;
    for (cov, tally) in report.records.values() {
        if *cov == Coverage::Unmodeled {
            n += tally.count;
        }
    }
    for ((_tag, cov), count) in &report.blocks {
        if *cov == Coverage::Unmodeled {
            n += count;
        }
    }
    n
}

fn dropped_count(report: &supercode_harness::audit::Report) -> u64 {
    let mut n: u64 = 0;
    for (cov, tally) in report.records.values() {
        if *cov == Coverage::Dropped {
            n += tally.count;
        }
    }
    for ((_tag, cov), count) in &report.blocks {
        if *cov == Coverage::Dropped {
            n += count;
        }
    }
    n
}

fn print_breakdown(tag: &str, report: &supercode_harness::audit::Report) {
    println!(
        "\n=== P6 audit gate: {tag} === files={} lines={} parse_errors={} unmodeled={} dropped={}",
        report.files,
        report.lines,
        report.parse_errors,
        unmodeled_count(report),
        dropped_count(report),
    );
    for (key, (cov, tally)) in &report.records {
        if matches!(cov, Coverage::Dropped | Coverage::Unmodeled) {
            println!("  {cov:?} record {key} x{}", tally.count);
        }
    }
    for ((tag2, cov), count) in &report.blocks {
        if matches!(cov, Coverage::Dropped | Coverage::Unmodeled) {
            println!("  {cov:?} block {tag2} x{count}");
        }
    }
}

/// Codex: the 3 genuine `codex-cli 0.142.5`-engine-produced rollouts
/// (`codex_real_corpus.rs`'s own provenance doc). `Unmodeled` is already
/// pinned at 0 by that file's own dev/01 test; the NEW bound here is
/// `Dropped`, measured (exactly, via `--nocapture`) at 29 — every one of
/// them an `event_msg` UI-only entry (`token_count` per turn, `task_started`/
/// `task_complete` per turn, `user_message` duplicating the already-
/// `Normalized` `response_item/message[user]`), i.e. the exact "genuinely
/// UI-only echo" category `event_msg_coverage`'s doc comment names as
/// honestly-Dropped, not a silent loss.
#[test]
fn codex_real_corpus_audit_thresholds() {
    const REAL_CORPUS_FILES: [&str; 3] = [
        "codex_real_rollout_compacted.jsonl",
        "codex_real_rollout_tools.jsonl",
        "codex_real_rollout_patch.jsonl",
    ];
    let report = audit_fixture_set("codex", &REAL_CORPUS_FILES, Corpus::Codex);
    print_breakdown("codex real corpus", &report);

    let unmodeled = unmodeled_count(&report);
    let dropped = dropped_count(&report);

    // Pinned at exactly the current-clean level (no worse than today).
    assert_eq!(
        unmodeled, 0,
        "Codex real-corpus Unmodeled regressed above the pinned floor of 0 — a record type \
         these 3 genuine codex-engine sessions used to fully model is now falling into the \
         Unknown bucket: {report:#?}"
    );
    assert!(
        dropped <= 29,
        "Codex real-corpus Dropped count regressed above the pinned ceiling of 29 (got \
         {dropped}) — something that used to be Normalized/Retained is now being silently \
         given up on: {report:#?}"
    );
}

#[test]
fn codex_audit_reports_review_entry_provenance_as_retained() {
    let report = audit_fixture_set(
        "codex-review-provenance",
        &["codex_session_eventmsg.jsonl"],
        Corpus::Codex,
    );
    let (coverage, tally) = report
        .records
        .get("event_msg/entered_review_mode")
        .expect("the real-shape fixture carries entered_review_mode");
    assert_eq!(
        *coverage,
        Coverage::Retained,
        "the portable provenance envelope keeps the exact review target/hint record"
    );
    assert_eq!(tally.count, 1);
}

/// Pi: the 5 genuine `pi@351efc82` agent-loop sessions (`pi_real_corpus.rs`'s
/// own provenance doc). `Unmodeled` already pinned at 0. `Dropped` measured
/// at 0 too on this fixture set (no `thinking_level_change`/`custom`/`label`
/// lines appear in these 5 genuine sessions) — pinned at 0 so ANY future
/// Dropped content on this exact fixture set is a real regression, not
/// margin-of-noise.
#[test]
fn pi_real_corpus_audit_thresholds() {
    const REAL_CORPUS_FILES: [&str; 5] = [
        "pi_real_corpus_plain_text.jsonl",
        "pi_real_corpus_thinking.jsonl",
        "pi_real_corpus_tool_call.jsonl",
        "pi_real_corpus_error_retry.jsonl",
        "pi_real_corpus_aborted.jsonl",
    ];
    let report = audit_fixture_set("pi", &REAL_CORPUS_FILES, Corpus::Pi);
    print_breakdown("pi real corpus", &report);

    let unmodeled = unmodeled_count(&report);
    let dropped = dropped_count(&report);

    assert_eq!(
        unmodeled, 0,
        "Pi real-corpus Unmodeled regressed above the pinned floor of 0: {report:#?}"
    );
    // Pinned ceiling is exactly 0 (this fixture set had zero Dropped content
    // at pin time) — `assert_eq!` rather than `<= 0` since 0 is u64's
    // minimum (clippy::absurd_extreme_comparisons); the intent is identical:
    // any Dropped content on this exact fixture set is a regression.
    assert_eq!(
        dropped, 0,
        "Pi real-corpus Dropped count regressed above the pinned ceiling of 0 (got {dropped}) \
         — this fixture set had zero Dropped content at pin time: {report:#?}"
    );
}

/// OpenCode: the committed SQLite fixture (`opencode_sqlite_fixture.rs`'s own
/// provenance doc — schema copied verbatim from `sst/opencode@fd9ee43`,
/// deliberately engineered to hit the hard cases: an unconvertible `https:`
/// file part, an `ignored:true` text part). `Unmodeled` pinned at 0
/// (`opencode_fixture_has_no_unknown_records_or_parts` already asserts this
/// on the sibling envelope-form fixture; this asserts it independently on
/// the SQLite surface). `Dropped` is measured at 2 — deliberately, by
/// fixture design: the `https:` file-part residue (`part/file:residue`) and
/// the `ignored:true` text part (`part/text:ignored`), both asserted
/// BY-DESIGN-Dropped in `opencode_sqlite_fixture.rs`'s own
/// `audit_classifies_non_image_file_and_ignored_text_as_dropped_not_normalized`.
#[test]
fn opencode_sqlite_fixture_audit_thresholds() {
    let dir = Path::new(env!("CARGO_MANIFEST_DIR")).join("tests/fixtures/opencode_fixture");
    let report = audit_dir(&dir, Corpus::OpenCode, None);
    print_breakdown("opencode sqlite fixture", &report);

    let unmodeled = unmodeled_count(&report);
    let dropped = dropped_count(&report);

    assert_eq!(
        unmodeled, 0,
        "OpenCode SQLite-fixture Unmodeled regressed above the pinned floor of 0: {report:#?}"
    );
    assert!(
        dropped <= 2,
        "OpenCode SQLite-fixture Dropped count regressed above the pinned ceiling of 2 (got \
         {dropped}) — the by-design residue (https: file part, ignored:true text part) grew or \
         something new started being silently dropped: {report:#?}"
    );
}

/// ClaudeCode: the 4 committed schema-coverage fixtures (`schema_coverage.rs`
/// already pins `parse_errors == 0` and zero `<line>/…` Unknown buckets on
/// each of these individually; this restates the same Unmodeled==0 floor
/// AGGREGATED, plus the NEW Dropped ceiling). `Dropped` measured (exactly,
/// via `--nocapture`) at 22 total, per-file: `claude_code_session.jsonl`=4
/// (system-message UI echoes — `permission-mode`/`mode`/etc.),
/// `codex_session.jsonl`=7, `codex_session_eventmsg.jsonl`=7,
/// `codex_session_eventmsg_precompact.jsonl`=4 (the Codex three: genuine
/// `event_msg` UI-only entries — `task_started`/`user_message`/
/// `mcp_tool_call_end`/`patch_apply_end`'s duplicated-elsewhere text portion
/// — the same honestly-enumerated Dropped category `event_msg_coverage`'s
/// doc comment names, not a silent loss).
#[test]
fn claude_and_codex_schema_fixtures_audit_thresholds() {
    // Each fixture is audited under ITS OWN corpus/format — mirrors
    // `schema_coverage.rs::fixtures_have_no_unknown_records`'s per-file
    // per-format loop, since a single `audit_dir` call can only parse a
    // directory as ONE corpus at a time.
    let fixtures: [(&str, Corpus); 4] = [
        ("claude_code_session.jsonl", Corpus::ClaudeCode),
        ("codex_session.jsonl", Corpus::Codex),
        ("codex_session_eventmsg.jsonl", Corpus::Codex),
        ("codex_session_eventmsg_precompact.jsonl", Corpus::Codex),
    ];

    let mut total_unmodeled: u64 = 0;
    let mut total_dropped: u64 = 0;
    for (file, corpus) in fixtures {
        let report = audit_fixture_set(
            &format!("schema-{}", file.replace('.', "_")),
            &[file],
            corpus,
        );
        print_breakdown(file, &report);
        total_unmodeled += unmodeled_count(&report);
        total_dropped += dropped_count(&report);
    }

    assert_eq!(
        total_unmodeled, 0,
        "ClaudeCode/Codex schema-fixture Unmodeled regressed above the pinned floor of 0 \
         (aggregate over all 4 fixtures)"
    );
    assert!(
        total_dropped <= 22,
        "ClaudeCode/Codex schema-fixture Dropped count regressed above the pinned ceiling of 22 \
         (aggregate over all 4 fixtures, got {total_dropped})"
    );
}