neosian 3.0.1

The state layer for LLM agents, as one static binary: the record, memory and the state process
//! Vibe's stream as the record reads it (X6a), with lines trimmed from
//! Vibe 3.1.5's live captures (record/investigation-2026-11-29/streams/
//! vibe-stdin-turn1.jsonl and vibe-nested-readonly-write-attempt.jsonl).

use serde_json::{Value, json};

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

const S: &str = "75b8b800-ec70-b483-6df9-80bb3d5f29d0";
const T: &str = "turn-67d84fb34e6e81bba2ee33a25e4d755c";

fn entry(kind: &str, extra: Value) -> Value {
    let mut entry =
        json!({"sessionId": S, "turnId": T, "type": kind, "generationStatus": "completed"});
    entry
        .as_object_mut()
        .unwrap()
        .extend(extra.as_object().unwrap().clone());
    entry
}

fn assistant(text: &str) -> Value {
    entry(
        "message",
        json!({"role": "assistant", "content": [{"type": "text", "text": text}], "outcome": {"type": "committed"}}),
    )
}

fn effect(status: &str) -> Value {
    entry(
        "effect",
        json!({"toolCallId": "4XnaCz1Fg", "detail": {"toolName": "file_system.read_file", "input": {"filePath": "note.txt"}}, "state": {"status": status, "output": {"content": "banana split\nsecond line\n"}}}),
    )
}

fn notice() -> Value {
    json!({"sessionId": S, "turnId": null, "type": "notice", "message": "Hooks completed"})
}

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

fn turn() -> Vec<Value> {
    vec![
        notice(),
        entry(
            "message",
            json!({"role": "user", "content": [{"type": "text", "text": "Read note.txt"}], "outcome": null}),
        ),
        assistant("Reading it."),
        effect("completed"),
        entry("reasoning", json!({"text": "The first word is banana."})),
        assistant("banana"),
    ]
}

#[test]
fn a_turn_ending_with_a_committed_answer_completes_with_its_tool() {
    let said = read(&turn());
    assert_eq!(said[0], Said::Session(S.into()));
    assert!(
        matches!(&said[1], Said::Tool { id: Some(id), name, .. } if id == "4XnaCz1Fg" && name == "file_system.read_file")
    );
    assert_eq!(said[2..], [Said::Answer("banana".into()), Said::Completed]);
    // A trailing notice does not change the end.
    let mut noticed = turn();
    noticed.push(notice());
    assert_eq!(read(&noticed).last(), Some(&Said::Completed));
}

#[test]
fn a_mid_turn_answer_another_session_or_turn_does_not_complete() {
    let mut cut = turn();
    cut.truncate(4);
    assert_eq!(read(&cut).last(), Some(&Said::Failed), "ended on a tool");
    let mut open = turn();
    open.truncate(3);
    open.push(effect("running"));
    assert_eq!(read(&open).last(), Some(&Said::Failed), "ended mid-call");
    let mut other = turn();
    other[3]["sessionId"] = json!("5e3a2f97-ca81-f232-b8ae-819bc77c448e");
    assert_eq!(read(&other).last(), Some(&Said::Failed), "another session");
    let mut later = turn();
    later[5]["turnId"] = json!("turn-other");
    assert_eq!(read(&later).last(), Some(&Said::Failed), "another turn");
    let mut pending = turn();
    pending[5]["outcome"] = json!(null);
    assert_eq!(read(&pending).last(), Some(&Said::Failed), "not committed");
    assert_eq!(read(&[]), [Said::Failed]);
}

#[test]
fn a_turn_completes_only_on_a_clean_end_of_its_stream() {
    // X6c U7: the output reached its end, and every line was readable.
    let lines = turn();
    let mut cut = Reader::default();
    for line in &lines {
        cut.line(line.as_object().unwrap());
    }
    assert_eq!(cut.finish(false), vec![Said::Failed], "the drain deadline");
    let mut garbled = Reader::default();
    for line in &lines {
        garbled.line(line.as_object().unwrap());
    }
    garbled.malformed();
    assert_eq!(
        garbled.finish(true),
        vec![Said::Failed],
        "a line after the answer"
    );
    // Through the capture: plain text, then truncated JSON, after the answer.
    for tail in ["Error: provider went away\n", "{\"sessionId\": \"75b8"] {
        let mut capture =
            crate::harness::capture::Capture::new(crate::harness::adapter::Agent::Vibe);
        for line in &lines {
            capture.push(format!("{line}\n").as_bytes());
        }
        capture.push(tail.as_bytes());
        assert!(!capture.finish(true).completed, "{tail}");
    }
    let mut clean = crate::harness::capture::Capture::new(crate::harness::adapter::Agent::Vibe);
    for line in &lines {
        clean.push(format!("{line}\n\n").as_bytes());
    }
    assert!(
        clean.finish(true).completed,
        "blank lines are not malformed"
    );
}