supercode-harness 0.4.21

The optional native Supercode agent and tool harness
Documentation
//! UNI-16 acceptance: the OpenClaw read-only tier against the CORRECTED
//! premise (openclaw >= 2026.7 stores sessions as pi-v3-dialect JSONL at
//! `agents/<agentId>/sessions/*.jsonl`; the SQLite transcript envelope from
//! the earlier research pin no longer exists).
//!
//! Fixtures: two SANITIZED REAL sessions written by an isolated openclaw
//! 2026.7.1-2 gateway during the UNI-5 live spike (provenance:
//! provisioned-real-runtime, mock model provider), plus two dialect fixtures
//! for the divergences the shipped writer proves (`createLeafEntry` in
//! dist: `{type:"leaf", ..., targetId}`; `appendMode:"side"`).

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

use supercode_harness::{
    DiscoveryQuery, HarnessCatalog, HarnessHomes, HarnessId, Role, Session, SessionSource,
};

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

fn sessions_dir() -> PathBuf {
    openclaw_home().join("agents/main/sessions")
}

#[test]
fn discovery_lists_real_sessions_and_skips_trajectory_files() {
    let query = DiscoveryQuery {
        harnesses: vec![HarnessId::new(HarnessId::OPENCLAW)],
        homes: HarnessHomes {
            openclaw: openclaw_home(),
            ..HarnessHomes::default()
        },
        ..DiscoveryQuery::default()
    };
    let found = HarnessCatalog::new().discover(&query).unwrap();
    assert_eq!(found.len(), 4, "{found:#?}");
    for descriptor in &found {
        assert_eq!(descriptor.locator.harness.as_str(), HarnessId::OPENCLAW);
        assert_eq!(
            descriptor.cwd.as_deref(),
            Some(Path::new("/workspace/project"))
        );
    }
    // Session ids come from the pi-v3 header, not the filename.
    assert!(found
        .iter()
        .any(|descriptor| descriptor.locator.session_id == "3dd577ae-a0a3-4b5b-8063-f402be4f5fd4"));
}

#[test]
fn real_session_loads_with_openclaw_provenance_and_vendor_metadata() {
    let path = sessions_dir().join("3dd577ae-a0a3-4b5b-8063-f402be4f5fd4.jsonl");
    let session = Session::load(&path).unwrap();
    assert_eq!(session.meta.source, SessionSource::OpenClaw);
    assert_eq!(session.parse_error_lines, 0);
    let users: Vec<&str> = session
        .messages
        .iter()
        .filter(|message| message.role == Role::User)
        .filter_map(|message| message.content.as_deref())
        .collect();
    assert!(!users.is_empty());
    // The vendor-namespaced `__openclaw` object is stamped as provenance
    // metadata on the loaded message, never interpreted.
    let stamped = session
        .messages
        .iter()
        .find(|message| message.metadata.contains_key("openclaw_senderIsOwner"))
        .expect("real acp-authored user messages carry __openclaw metadata");
    assert_eq!(
        stamped
            .metadata
            .get("openclaw_senderIsOwner")
            .map(String::as_str),
        Some("true")
    );
    // Raw stays byte-lossless like every pi-v3 loader path.
    assert!(session.raw_is_verbatim);
}

#[test]
fn leaf_entry_redirects_the_active_path_away_from_the_abandoned_branch() {
    let path = sessions_dir().join("aaaa1111-2222-3333-4444-555566667777.jsonl");
    let session = Session::load(&path).unwrap();
    assert_eq!(session.meta.source, SessionSource::OpenClaw);
    let texts: Vec<String> = session
        .messages
        .iter()
        .filter_map(|message| message.content.clone())
        .collect();
    // The trailing `leaf` entry redirects the anchor to e2: the abandoned
    // branch (e3/e4) is not on the active path. Pi's own last-entry rule
    // would have replayed the abandoned branch instead.
    assert!(texts.iter().any(|text| text.contains("first question")));
    assert!(texts.iter().any(|text| text.contains("first answer")));
    assert!(
        !texts.iter().any(|text| text.contains("abandoned branch")),
        "leaf redirect must exclude the abandoned branch: {texts:?}"
    );
    // The abandoned branch still survives byte-exact in raw.
    assert!(session
        .raw
        .iter()
        .any(|line| line.contains("abandoned branch answer")));
}

#[test]
fn side_append_entries_never_anchor_and_foreign_refs_stay_inert() {
    let path = sessions_dir().join("bbbb1111-2222-3333-4444-555566667777.jsonl");
    let session = Session::load(&path).unwrap();
    assert_eq!(session.meta.source, SessionSource::OpenClaw);
    let texts: Vec<String> = session
        .messages
        .iter()
        .filter_map(|message| message.content.clone())
        .collect();
    // The trailing appendMode:"side" entry must not anchor the walk — the
    // active path ends at the main answer.
    assert!(texts.iter().any(|text| text.contains("main answer")));
    assert!(
        !texts
            .iter()
            .any(|text| text.contains("side-channel annotation")),
        "a side-append entry anchored the walk: {texts:?}"
    );
    // dev/03: foreign-session references (sessionKey vendor metadata, the
    // delegate custom entry) decode as INERT provenance — stamped or raw —
    // and the loader never dereferences another session (the referenced
    // sessions do not exist on disk; the load stays clean and standalone).
    let stamped = session
        .messages
        .iter()
        .find(|message| message.metadata.contains_key("openclaw_sessionKey"))
        .expect("sessionKey vendor metadata must be stamped");
    assert!(stamped
        .metadata
        .get("openclaw_sessionKey")
        .unwrap()
        .contains("acp-bridge"));
    assert!(session
        .raw
        .iter()
        .any(|line| line.contains("delegateSession")));
    assert_eq!(session.parse_error_lines, 0);
}

#[test]
fn plain_pi_sessions_are_not_misdetected_as_openclaw() {
    // A pi session whose text merely MENTIONS openclaw markers in content
    // must still detect as Pi (the discriminants are structural).
    let jsonl = concat!(
        "{\"type\":\"session\",\"version\":3,\"id\":\"p1\",\"timestamp\":\"2026-08-31T02:20:00.000Z\",\"cwd\":\"/w\"}\n",
        "{\"type\":\"message\",\"id\":\"x1\",\"parentId\":null,\"timestamp\":\"2026-08-31T02:20:01.000Z\",\"message\":{\"role\":\"user\",\"content\":\"tell me about __openclaw and type leaf entries\"}}\n",
    );
    let session = Session::from_pi_str(jsonl).unwrap();
    assert_eq!(session.meta.source, SessionSource::Pi);
    // And the detector itself routes it to Pi: write to a temp file and load
    // through the auto-detecting path.
    let dir = std::env::temp_dir().join(format!(
        "supercode-uni16-detect-{}-{}",
        std::process::id(),
        std::time::SystemTime::now()
            .duration_since(std::time::UNIX_EPOCH)
            .unwrap()
            .as_nanos()
    ));
    std::fs::create_dir_all(&dir).unwrap();
    let file = dir.join("p1.jsonl");
    std::fs::write(&file, jsonl).unwrap();
    let detected = Session::load(&file).unwrap();
    assert_eq!(detected.meta.source, SessionSource::Pi);
}