supercode-harness 0.4.6

The optional native Supercode agent and tool harness
Documentation
use std::path::{Path, PathBuf};

fn repo_root() -> PathBuf {
    Path::new(env!("CARGO_MANIFEST_DIR"))
        .join("../..")
        .canonicalize()
        .unwrap()
}

fn production_code(source: &str) -> String {
    let source = source
        .find("#[cfg(test)]\nmod tests")
        .map_or(source, |index| &source[..index]);
    let mut code = String::with_capacity(source.len());
    let mut chars = source.chars().peekable();
    let mut block_comment = false;
    while let Some(ch) = chars.next() {
        if block_comment {
            if ch == '*' && chars.peek() == Some(&'/') {
                chars.next();
                block_comment = false;
            }
            continue;
        }
        if ch == '/' && chars.peek() == Some(&'*') {
            chars.next();
            block_comment = true;
            continue;
        }
        if ch == '/' && chars.peek() == Some(&'/') {
            chars.next();
            for next in chars.by_ref() {
                if next == '\n' {
                    code.push('\n');
                    break;
                }
            }
            continue;
        }
        code.push(ch);
    }
    code
}

fn rust_sources(path: &Path, files: &mut Vec<PathBuf>) {
    for entry in std::fs::read_dir(path).unwrap() {
        let path = entry.unwrap().path();
        if path.is_dir() {
            rust_sources(&path, files);
        } else if path.extension().and_then(|value| value.to_str()) == Some("rs") {
            files.push(path);
        }
    }
}

#[test]
fn reduction_engine_does_not_depend_on_the_native_agent_runtime() {
    let root = repo_root().join("crates/reduce/src");
    let mut sources = Vec::new();
    rust_sources(&root, &mut sources);

    for path in sources {
        let code = production_code(&std::fs::read_to_string(&path).unwrap());
        for forbidden in [
            "crate::agent",
            "crate::provider",
            "crate::tools",
            "crate::sdk",
            "crate::harness_service",
        ] {
            assert!(
                !code.contains(forbidden),
                "{} couples the reduction engine to runtime module `{forbidden}`",
                path.display()
            );
        }
    }
}

#[test]
fn reduction_engine_operates_on_canonical_messages_not_session_codecs() {
    let path = repo_root().join("crates/reduce/src/engine/mod.rs");
    let code = production_code(&std::fs::read_to_string(&path).unwrap());
    for forbidden in ["crate::session", "SessionFormat"] {
        assert!(
            !code.contains(forbidden),
            "{} couples the reduction engine to session adapter `{forbidden}`",
            path.display()
        );
    }
}

#[test]
fn reduction_engine_uses_its_standalone_error_contract() {
    let root = repo_root().join("crates/reduce/src/engine");
    let sources = [
        root.join("mod.rs"),
        root.join("handoff.rs"),
        root.join("rehydrate.rs"),
        root.join("summarize.rs"),
        root.join("supersede.rs"),
    ];
    for path in sources {
        let code = production_code(&std::fs::read_to_string(&path).unwrap());
        for forbidden in ["crate::error", "crate::{Error", "crate::{Result"] {
            assert!(
                !code.contains(forbidden),
                "{} couples the reduction engine to core error path `{forbidden}`",
                path.display()
            );
        }
    }
}

#[test]
fn canonical_token_estimator_does_not_depend_on_runtime_or_reduction() {
    let path = repo_root().join("crates/interchange/src/token_estimator.rs");
    let code = production_code(&std::fs::read_to_string(&path).unwrap());
    for forbidden in [
        "crate::agent",
        "crate::provider",
        "crate::reduce",
        "crate::tools",
        "crate::sdk",
        "crate::harness_service",
    ] {
        assert!(
            !code.contains(forbidden),
            "{} couples canonical token estimates to `{forbidden}`",
            path.display()
        );
    }
}

#[test]
fn canonical_messages_do_not_depend_on_runtime_or_reduction() {
    let path = repo_root().join("crates/interchange/src/message.rs");
    let code = production_code(&std::fs::read_to_string(&path).unwrap());
    for forbidden in [
        "crate::agent",
        "crate::provider",
        "crate::reduce",
        "crate::tools",
        "crate::sdk",
        "crate::harness_service",
    ] {
        assert!(
            !code.contains(forbidden),
            "{} couples canonical messages to `{forbidden}`",
            path.display()
        );
    }
}

#[test]
fn fidelity_measurement_does_not_depend_on_runtime_or_reduction() {
    let path = repo_root().join("crates/interchange/src/fidelity.rs");
    let code = production_code(&std::fs::read_to_string(&path).unwrap());
    for forbidden in [
        "crate::agent",
        "crate::provider",
        "crate::reduce",
        "crate::tools",
        "crate::sdk",
        "crate::harness_service",
    ] {
        assert!(
            !code.contains(forbidden),
            "{} gives canonical fidelity measurement a forbidden dependency on `{forbidden}`",
            path.display()
        );
    }
}

#[test]
fn canonical_sessions_and_codecs_live_in_interchange_only() {
    let root = repo_root();
    for relative in [
        "crates/interchange/src/session.rs",
        "crates/interchange/src/session_tree.rs",
        "crates/interchange/src/sidecar.rs",
        "crates/interchange/src/catalog.rs",
        "crates/interchange/src/watch.rs",
    ] {
        let path = root.join(relative);
        let code = production_code(&std::fs::read_to_string(&path).unwrap());
        for forbidden in [
            "crate::agent",
            "crate::provider",
            "crate::reduce",
            "crate::tools",
            "crate::sdk",
            "crate::harness_service",
        ] {
            assert!(
                !code.contains(forbidden),
                "{} couples session interchange to `{forbidden}`",
                path.display()
            );
        }
    }

    for shim in [
        "session.rs",
        "session_tree.rs",
        "sidecar.rs",
        "catalog.rs",
        "watch.rs",
    ] {
        let path = root.join("crates/harness/src").join(shim);
        let code = production_code(&std::fs::read_to_string(&path).unwrap());
        assert!(
            code.lines().count() <= 5,
            "{} must remain a compatibility re-export, not a second implementation",
            path.display()
        );
    }
}

#[test]
fn native_provider_implementation_lives_in_runtime() {
    let root = repo_root();
    let runtime_path = root.join("crates/runtime/src/provider.rs");
    let runtime = production_code(&std::fs::read_to_string(&runtime_path).unwrap());
    assert!(runtime.contains("pub struct OpenAiProvider"));
    assert!(runtime.contains("pub trait Provider"));
    for forbidden in ["supercode_core", "crate::agent", "crate::sdk"] {
        assert!(
            !runtime.contains(forbidden),
            "{} couples the native provider to `{forbidden}`",
            runtime_path.display()
        );
    }

    let bridge_path = root.join("crates/harness/src/provider.rs");
    let bridge = production_code(&std::fs::read_to_string(&bridge_path).unwrap());
    assert!(!bridge.contains("reqwest::"));
    assert!(!bridge.contains("bytes_stream"));
    assert!(bridge.contains("supercode_runtime::Provider::complete"));
}

#[test]
fn interchange_manifest_has_no_addon_or_runtime_dependency() {
    let path = repo_root().join("crates/interchange/Cargo.toml");
    let manifest = std::fs::read_to_string(&path).unwrap();
    for forbidden in [
        "supercode-core",
        "supercode-reduce",
        "supercode-runtime",
        "reqwest",
        "tree-sitter",
        "landlock",
    ] {
        assert!(
            !manifest.contains(forbidden),
            "{} gives the interchange base forbidden dependency `{forbidden}`",
            path.display()
        );
    }
}

#[test]
fn reduction_manifest_has_no_core_or_runtime_dependency() {
    let path = repo_root().join("crates/reduce/Cargo.toml");
    let manifest = std::fs::read_to_string(&path).unwrap();
    assert!(manifest.contains("supercode-interchange"));
    for forbidden in [
        "supercode-core",
        "supercode-runtime",
        "reqwest",
        "tokio",
        "tree-sitter",
        "landlock",
        "rusqlite",
    ] {
        assert!(
            !manifest.contains(forbidden),
            "{} gives the optional reducer forbidden dependency `{forbidden}`",
            path.display()
        );
    }
}

#[test]
fn standalone_reduction_error_has_no_core_runtime_dependency() {
    let path = repo_root().join("crates/reduce/src/error.rs");
    let code = production_code(&std::fs::read_to_string(&path).unwrap());
    for forbidden in [
        "supercode_core",
        "crate::agent",
        "crate::provider",
        "crate::tools",
    ] {
        assert!(
            !code.contains(forbidden),
            "{} couples standalone reduction errors to `{forbidden}`",
            path.display()
        );
    }
}

#[test]
fn runtime_manifest_keeps_reduction_optional_and_never_depends_on_core() {
    let path = repo_root().join("crates/runtime/Cargo.toml");
    let manifest = std::fs::read_to_string(&path).unwrap();
    assert!(manifest.contains("supercode-interchange"));
    assert!(manifest.contains("supercode-reduce"));
    assert!(manifest.contains("optional = true"));
    assert!(!manifest.contains("supercode-core"));
}

#[test]
fn native_agent_and_tools_live_in_the_harness_package() {
    let root = repo_root();
    let manifest = std::fs::read_to_string(root.join("crates/harness/Cargo.toml")).unwrap();
    assert!(manifest.contains("name = \"supercode-harness\""));
    assert!(manifest.contains("supercode-runtime"));
    assert!(manifest.contains("supercode-reduce"));
    assert!(manifest.contains("supercode-interchange"));

    for implementation in [
        "agent.rs",
        "config.rs",
        "checkpoint.rs",
        "plugins.rs",
        "safe_path.rs",
        "sandbox.rs",
        "subagents.rs",
    ] {
        assert!(
            root.join("crates/harness/src")
                .join(implementation)
                .is_file(),
            "native harness implementation {implementation} must be owned by supercode-harness"
        );
    }
    assert!(root.join("crates/harness/src/tools/mod.rs").is_file());
    assert!(root.join("crates/harness/src/permissions/mod.rs").is_file());
}

#[test]
fn core_is_a_thin_compatibility_facade_without_an_agent_fallback() {
    let root = repo_root();
    let manifest = std::fs::read_to_string(root.join("crates/core/Cargo.toml")).unwrap();
    assert!(manifest.contains("name = \"supercode-core\""));
    assert!(manifest.contains("supercode-harness"));
    for forbidden in ["reqwest", "tokio", "rusqlite", "tree-sitter", "landlock"] {
        assert!(
            !manifest.contains(forbidden),
            "compatibility facade must not directly depend on {forbidden}"
        );
    }

    let source =
        production_code(&std::fs::read_to_string(root.join("crates/core/src/lib.rs")).unwrap());
    assert!(source.contains("pub use supercode_harness::*"));
    assert!(!source.contains("struct Agent"));
    assert!(!source.contains("trait Provider"));
    assert_eq!(
        std::fs::read_dir(root.join("crates/core/src"))
            .unwrap()
            .count(),
        1,
        "the compatibility facade must not grow a second implementation"
    );
}