neosian 3.0.2

The state layer for LLM agents, as one static binary: the record, memory and the state process
//! Muse Code's plan and display reading (X6b), against Muse 1.4.25's live
//! captures (record/investigation-2026-11-29/streams/muse-*.jsonl).

use std::io::Read;
use std::path::Path;

use serde_json::json;

use super::super::adapter::{Agent, Mode, Request, plan as adapter_plan};
use super::super::events::Event;

const SESSION: &str = "5eab6b3f-d0af-49c3-9c0c-a9b47c2c8ebe";

fn request<'a>(mode: Mode, resume: Option<&'a str>, confined: bool) -> Request<'a> {
    Request {
        agent: Agent::Muse,
        mode,
        model: Some("muse-spark-1.2"),
        directory: Path::new("/h/work"),
        resume,
        new_session: SESSION,
        private: Path::new("/tmp/neosian-harness-x"),
        codex_keys: &[],
        opencode_config: None,
        parent: Some("parent-1"),
        server: None,
        codex_off: &[],
        confined,
        pi: None,
        child_home: Some(Path::new("/t/neosian-harness-1-muse-h1")),
        memory: None,
    }
}

#[test]
fn both_modes_run_the_planned_argv_with_a_chosen_session_and_no_hooks() {
    let head = [
        "muse",
        "exec",
        "--json",
        "--prompt-file",
        "/dev/stdin",
        "--session-id",
        SESSION,
        "--approval-judge",
        "off",
        "--disable-reminders",
        "--no-foreign-personal-context",
        "-c",
        "hooks={}",
        "--approval-mode",
        "never",
    ];
    let read = adapter_plan(&request(Mode::ReadOnly, None, true)).unwrap();
    let tail = [
        "--disable-write",
        "--disable-shell",
        "--disable-sandbox",
        "--model",
        "muse-spark-1.2",
    ];
    assert_eq!(read.argv, [&head[..], &tail[..]].concat());
    let write = adapter_plan(&request(Mode::Write, Some(SESSION), false)).unwrap();
    assert_eq!(
        write.argv,
        [&head[..], &["--model", "muse-spark-1.2"][..]].concat()
    );
    for plan in [&read, &write] {
        let set = |k: &str, v: &str| plan.env.contains(&(k.to_string(), v.to_string()));
        assert!(set("XDG_CONFIG_HOME", "/t/neosian-harness-1-muse-h1"));
        assert!(set("MUSE_NO_AUTO_UPDATE", "1") && set("NEOSIAN_HARNESS_SESSION", SESSION));
    }
    assert_eq!(Agent::Muse.kind(), "muse-code");
}

#[cfg(unix)]
#[test]
fn the_prompt_is_an_anonymous_regular_file() {
    let mut file = super::prompt_file(b"Read note.txt").unwrap();
    let mut said = String::new();
    file.read_to_string(&mut said).unwrap();
    assert_eq!(said, "Read note.txt");
    assert!(file.metadata().unwrap().is_file());
    use std::os::unix::fs::MetadataExt;
    assert_eq!(file.metadata().unwrap().nlink(), 0, "no name anywhere");
}

#[test]
fn the_display_shows_the_session_deltas_tools_and_the_terminal() {
    let line = |kind: &str, payload: &str| {
        format!(
            r#"{{"schema_version":1,"stream":{{"kind":"session","id":"{SESSION}"}},"payload_type":"{kind}","payload":{payload}}}"#
        )
    };
    let delta = line("run.output.delta", r#"{"text":"banana"}"#);
    assert_eq!(
        Agent::Muse.read(&delta),
        [
            Event::Session(SESSION.into()),
            Event::Delta("banana".into())
        ]
    );
    let tool = line(
        "tool.result",
        r#"{"call_id":"call_01","text":"Read text file `note.txt`.","correlation_facts":{"tool_name":"read_file","outcome":"success"}}"#,
    );
    let Event::Observed(entry) = &Agent::Muse.read(&tool)[1] else {
        panic!("an entry")
    };
    assert_eq!(entry["name"], "read_file");
    let failed = line(
        "run.terminal.failed",
        r#"{"terminal":"failed","text":"","reason":"model `x` does not exist"}"#,
    );
    assert_eq!(
        Agent::Muse.read(&failed)[1],
        Event::Failure("model `x` does not exist".into())
    );
}

#[test]
fn a_keychain_sign_in_is_rebuilt_from_its_metadata_and_other_shapes_are_refused() {
    // X6c U9: never the original bytes; a token beside a Keychain storage
    // is not copied, and the person's name and address are not either.
    let person = json!({"schema_version": 2, "providers": {"meta": {
        "mechanism": "oauth", "storage": "keychain", "obtained_via": "device_code",
        "api_base_url": "https://api.example/v1", "user_email": "a@example.org",
        "user_full_name": "A", "subs_active": true, "access_token": "t0ken"}}});
    let rebuilt = super::keychain_only(person.to_string().as_bytes()).unwrap();
    assert_eq!(
        rebuilt,
        json!({"schema_version": 2, "providers": {"meta": {"mechanism": "oauth",
            "storage": "keychain", "obtained_via": "device_code",
            "api_base_url": "https://api.example/v1"}}})
    );
    for refused in [
        json!({"providers": {"meta": {"storage": "keychain"}}}),
        json!({"schema_version": "2", "providers": {"meta": {"storage": "keychain"}}}),
        json!({"schema_version": 2, "providers": {}}),
        json!({"schema_version": 2, "providers": {"meta": "keychain"}}),
        json!({"schema_version": 2, "providers": {"meta": {"storage": "file"}}}),
        json!({"schema_version": 2, "providers": {"meta": {"storage": "keychain", "mechanism": 1}}}),
        json!({"schema_version": 2, "providers": {"a": {"storage": "keychain"}, "b": {}}}),
        json!([1]),
    ] {
        assert!(
            super::keychain_only(refused.to_string().as_bytes()).is_err(),
            "{refused}"
        );
    }
    assert!(super::keychain_only(b"not json").is_err());
}