neosian 3.0.1

The state layer for LLM agents, as one static binary: the record, memory and the state process
//! Muse Code's stream as the record reads it (X6b), with lines trimmed
//! from Muse 1.4.25's live captures (record/investigation-2026-11-29/
//! streams/muse-readonly.jsonl and muse-bad-model.jsonl).

use serde_json::{Value, json};

use super::super::Said;
use super::Reader;

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

fn line(kind: &str, id: &str, payload_type: &str, payload: Value) -> Value {
    json!({"schema_version": 1, "stream": {"kind": kind, "id": id}, "payload_type": payload_type, "payload": payload})
}

const C: &str = "01a22601-1bc9-7660-9e0e-580c99eff8e6";

/// A payload of the linked run, as every run event carries it.
fn run(mut payload: Value, command: &str) -> Value {
    payload["command_id"] = json!(command);
    payload["run_stream"] = json!({"kind": "run", "id": command});
    payload
}

fn turn() -> Vec<Value> {
    vec![
        line(
            "session",
            S,
            "runtime.command.accepted",
            json!({"command_kind": "turn.submit", "command_id": C}),
        ),
        line("session", S, "session.run.linked", run(json!({}), C)),
        line(
            "session",
            S,
            "turn.input.user",
            run(json!({"prompt": "Read note.txt"}), C),
        ),
        line(
            "session",
            S,
            "tool.result",
            run(
                json!({"call_id": "call_01", "text": "Read text file `note.txt`.\n1|banana split", "correlation_facts": {"tool_name": "read_file", "outcome": "success"}}),
                C,
            ),
        ),
        line("exec.usage", S, "run.usage", run(json!({"calls": 2}), C)),
        line(
            "session",
            S,
            "run.terminal.completed",
            run(
                json!({"terminal": "completed", "text": "banana", "reason": null}),
                C,
            ),
        ),
    ]
}

fn read(lines: &[Value]) -> Vec<Said> {
    let mut reader = Reader::default();
    lines
        .iter()
        .flat_map(|l| reader.line(l.as_object().unwrap()))
        .collect()
}

#[test]
fn a_completed_terminal_closes_the_turn_with_its_tool_result() {
    let said = read(&turn());
    assert_eq!(said[0], Said::Session(S.into()));
    assert!(
        matches!(&said[1], Said::Tool { id: Some(id), name, input, response }
        if id == "call_01" && name == "read_file" && *input == json!({}) && response["outcome"] == "success")
    );
    assert_eq!(said[2..], [Said::Answer("banana".into()), Said::Completed]);
}

#[test]
fn a_failed_or_missing_terminal_or_another_session_never_completes() {
    let mut failed = turn();
    failed[5] = line(
        "session",
        S,
        "run.terminal.failed",
        run(
            json!({"terminal": "failed", "text": "", "reason": "model `x` does not exist"}),
            C,
        ),
    );
    assert_eq!(read(&failed).last(), Some(&Said::Failed));
    let mut open = turn();
    open.pop();
    assert!(!read(&open).contains(&Said::Completed));
    let mut other = turn();
    other[4]["stream"]["id"] = json!("01a22601-18c9-7eb1-bb12-c182e2fb1bc5");
    assert_eq!(read(&other).last(), Some(&Said::Failed));
}

#[test]
fn only_the_linked_runs_events_on_the_sessions_stream_are_this_turns() {
    // X6c U6: another run in the same session, or a terminal under another
    // stream kind, never supplies the turn's tool calls or its completion.
    let other = "01a22601-0000-7660-9e0e-580c99eff8e6";
    let mut lines = turn();
    let last = lines.pop().unwrap();
    let mut foreign = lines.clone();
    foreign.push(line(
        "session",
        S,
        "tool.result",
        run(
            json!({"call_id": "x", "correlation_facts": {"tool_name": "write_file"}}),
            other,
        ),
    ));
    foreign.push(line(
        "session",
        S,
        "run.terminal.completed",
        run(json!({"text": "theirs"}), other),
    ));
    foreign.push(line(
        "run",
        C,
        "run.terminal.completed",
        run(json!({"text": "elsewhere"}), C),
    ));
    let said = read(&foreign);
    assert!(!said.contains(&Said::Completed), "{said:?}");
    assert_eq!(
        said.iter()
            .filter(|s| matches!(s, Said::Tool { .. }))
            .count(),
        1
    );
    foreign.push(last.clone());
    assert!(read(&foreign).contains(&Said::Answer("banana".into())));
    let unlinked: Vec<Value> = turn()
        .into_iter()
        .filter(|l| l["payload_type"] != "session.run.linked")
        .collect();
    assert!(!read(&unlinked).contains(&Said::Completed));
}