neosian 3.0.3

The state layer for LLM agents, as one static binary: the record, memory and the state process
//! Antigravity's stream as the record reads it (X6a), with lines trimmed
//! from agy 1.5.20's live captures (record/investigation-2026-11-29/
//! streams/agy-stdin-turn1.jsonl and agy-readonly-deny-turn2-continue.jsonl).

use serde_json::{Value, json};

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

const C: &str = "6aa84135-411e-4380-8301-636d6c27badb";

fn step(index: u64, state: &str, extra: Value) -> Value {
    let mut step = json!({"conversation_id": C, "step_index": index, "state": state, "step_type": "tool", "tool_name": "read_file"});
    step.as_object_mut()
        .unwrap()
        .extend(extra.as_object().unwrap().clone());
    json!({"event": "step_update", "step_update": step})
}

fn turn() -> Vec<Value> {
    vec![
        json!({"event": "init", "conversation_id": C, "init": {"model": "gemini-3.8-flash-low"}}),
        json!({"event": "step_update", "step_update": {"conversation_id": C, "step_index": 0, "state": "DONE", "step_type": "user_input"}}),
        step(
            2,
            "ACTIVE",
            json!({"tool_info": {"name": "read_file", "parameters": {"Path": "note.txt"}}}),
        ),
        step(
            2,
            "DONE",
            json!({"tool_info": {"name": "read_file", "parameters": {"Path": "note.txt"}, "output": "2 lines, 25 bytes"}}),
        ),
        json!({"event": "result", "result": {"conversation_id": C, "status": "SUCCESS", "response": "banana\n", "num_turns": 1}}),
    ]
}

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_successful_result_closes_the_turn_with_its_settled_tool() {
    assert_eq!(
        read(&turn()),
        vec![
            Said::Session(C.into()),
            Said::Tool {
                id: Some("step-2".into()),
                name: "read_file".into(),
                input: json!({"Path": "note.txt"}),
                response: json!({"output": "2 lines, 25 bytes"}),
            },
            Said::Answer("banana\n".into()),
            Said::Completed,
        ]
    );
    let mut denied = turn();
    denied[3] = step(
        2,
        "ERROR",
        json!({"tool_info": {"name": "write_file", "parameters": {"Path": "probe.txt"}, "error": {"type": "TOOL_ERROR", "message": "Permission denied"}}}),
    );
    assert!(read(&denied).iter().any(
        |s| matches!(s, Said::Tool { response, .. } if response["error"]["type"] == "TOOL_ERROR")
    ));
}

#[test]
fn no_result_another_conversation_or_an_unsettled_tool_never_completes() {
    let mut unfinished = turn();
    unfinished.pop();
    assert!(!read(&unfinished).contains(&Said::Completed));
    let mut changed = turn();
    changed[4]["result"]["conversation_id"] = json!("762985d9-0000-0000-0000-000000000000");
    assert_eq!(read(&changed).last(), Some(&Said::Failed));
    let mut open = turn();
    open.remove(3);
    assert_eq!(read(&open).last(), Some(&Said::Failed), "never settled");
    let mut orphan = turn();
    orphan.remove(2);
    assert_eq!(read(&orphan).last(), Some(&Said::Failed), "never started");
    let mut failed = turn();
    failed[4]["result"]["status"] = json!("ERROR");
    assert_eq!(read(&failed).last(), Some(&Said::Failed));
}