neosian 3.0.0

The state layer for LLM agents, as one static binary: the record, memory and the state process
//! Cursor's stream as the record reads it (X5b), in the order and shapes
//! of the installed 2026.09.10-fd3934a print handler (source-derived).

use serde_json::{Value, json};

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

const CHAT: &str = "0e6a1c2b-3f4d-4e5a-8b9c-0d1e2f3a4b5c";

fn init(chat: &str) -> Value {
    json!({"type": "system", "subtype": "init", "apiKeySource": "login", "cwd": "/w",
        "session_id": chat, "model": "Auto", "permissionMode": "default"})
}

fn call(subtype: &str, id: &str, chat: &str) -> Value {
    let mut value = json!({"args": {"path": "note.txt"}});
    if subtype == "completed" {
        value["result"] = json!({"success": {"content": "banana"}});
    }
    json!({"type": "tool_call", "subtype": subtype, "call_id": id,
        "tool_call": {"readToolCall": value}, "model_call_id": "m1",
        "session_id": chat, "timestamp_ms": 1})
}

fn result(chat: &str) -> Value {
    json!({"type": "result", "subtype": "success", "is_error": false, "duration_ms": 9,
        "result": "banana", "session_id": chat, "request_id": "r1"})
}

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

fn turn() -> Vec<Value> {
    vec![
        init(CHAT),
        json!({"type": "user", "message": {"role": "user",
            "content": [{"type": "text", "text": "q"}]}, "session_id": CHAT}),
        call("started", "c1", CHAT),
        call("completed", "c1", CHAT),
        json!({"type": "assistant", "message": {"role": "assistant",
            "content": [{"type": "text", "text": "banana"}]}, "session_id": CHAT}),
        result(CHAT),
    ]
}

#[test]
fn a_successful_result_closes_the_turn_with_its_tool_round() {
    assert_eq!(
        read(&turn()),
        vec![
            Said::Session(CHAT.into()),
            Said::Tool {
                id: Some("c1".into()),
                name: "readToolCall".into(),
                input: json!({"path": "note.txt"}),
                response: json!({"success": {"content": "banana"}}),
            },
            Said::Answer("banana".into()),
            Said::Completed,
        ]
    );
}

#[test]
fn no_result_a_changed_chat_or_an_odd_call_never_completes() {
    let mut unfinished = turn();
    unfinished.pop();
    assert!(!read(&unfinished).contains(&Said::Completed), "no result");
    let mut changed = turn();
    changed[5] = result("1f7b2d3c-4e5f-4a6b-9c8d-1e2f3a4b5c6d");
    assert_eq!(read(&changed).last(), Some(&Said::Failed), "another chat");
    let mut orphan = turn();
    orphan.remove(2);
    assert_eq!(read(&orphan).last(), Some(&Said::Failed), "no start");
    let mut open = turn();
    open.remove(3);
    assert_eq!(read(&open).last(), Some(&Said::Failed), "never completed");
    let mut odd = turn();
    let valid = json!({"args": {"path": "a"}, "result": {"success": {}}});
    odd[3]["tool_call"] = json!({"readToolCall": valid, "globToolCall": valid});
    assert_eq!(read(&odd).last(), Some(&Said::Failed), "two members");
    let mut swapped = turn();
    swapped[3]["tool_call"] = json!({"globToolCall": valid});
    assert_eq!(read(&swapped).last(), Some(&Said::Failed), "another kind");
    let mut kindless = turn();
    kindless[2]["tool_call"] = json!({});
    assert_eq!(
        read(&kindless).last(),
        Some(&Said::Failed),
        "a start of no kind"
    );
    let mut shapeless = turn();
    shapeless[3]["tool_call"]["readToolCall"]
        .as_object_mut()
        .unwrap()
        .remove("result");
    assert_eq!(
        read(&shapeless).last(),
        Some(&Said::Failed),
        "no result member"
    );
    let mut failed = turn();
    failed[5]["is_error"] = json!(true);
    assert_eq!(
        read(&failed).last(),
        Some(&Said::Failed),
        "a failure result"
    );
    let mut early = turn();
    early.remove(0);
    assert_eq!(read(&early).last(), Some(&Said::Failed), "no init");
}

#[test]
fn a_live_turn_with_its_extra_tool_call_members_completes() {
    // X7a: Cursor 2026.11.22-e9042b2's real stream (scrubbed): thinking
    // events, and `toolCallId`, `startedAtMs`, `completedAtMs` and
    // `hookAdditionalContexts` beside each call's kind.
    let lines: Vec<Value> =
        include_str!("../../../../../../tests/contract/fixtures/harness/cursor-read-turn.jsonl")
            .lines()
            .map(|line| serde_json::from_str(line).unwrap())
            .collect();
    let said = read(&lines);
    let tools: Vec<&str> = said
        .iter()
        .filter_map(|s| match s {
            Said::Tool { name, .. } => Some(name.as_str()),
            _ => None,
        })
        .collect();
    assert_eq!(tools, ["globToolCall", "readToolCall"]);
    assert!(said.contains(&Said::Answer("banana".into())) && said.contains(&Said::Completed));
}