Skip to content

A self-improving RLM agent for coding workflows and long-running autonomous tasks.

Rust
12.7 KB
Raw Blame
//! Latency guard for the operator's `/context` timeout (2026-09-24):
//! `get_context_tree` must answer from memory, never by walking the artifact
//! tree inline (measured 14-19s per call before the cache). Every round trip
//! stays under the sub-second ceiling, and the seeded children DO appear.
#![allow(clippy::large_futures)]
// 64-bit-only targets; the narrowing casts sit at bounded OS boundaries.
#![allow(
    clippy::cast_possible_truncation,
    clippy::cast_possible_wrap,
    clippy::cast_sign_loss,
    clippy::cast_precision_loss
)]
// Fn length is a style gate, not correctness.
#![allow(clippy::too_many_lines)]
// API-shape opinions, not defects; the surfaces are deliberate.
#![allow(
    clippy::unnecessary_wraps,
    clippy::zero_sized_map_values,
    clippy::struct_excessive_bools,
    clippy::struct_field_names
)]
#![cfg(unix)]

use std::io::{BufRead, BufReader, Write};
use std::os::unix::net::UnixStream;
use std::path::PathBuf;
use std::process::{Child, Command, Stdio};
use std::time::{Duration, Instant};

/// The response ceiling: `/context` is in-memory data, so a healthy round trip is
/// milliseconds — 750ms is an order of magnitude under the 10s client timeout,
/// calibrated so the seeded tree's INLINE walk cannot squeeze under it.
const CONTEXT_RESPONSE_CEILING: Duration = Duration::from_millis(750);

/// How long the test waits for the background refresh to fill the cache (the
/// warm fires at create/attach; the walk is a bounded disk read).
const CACHE_FILL_DEADLINE: Duration = Duration::from_secs(30);

/// The seeded tree: enough child session bytes (~300MB) that an inline walk blows
/// the ceiling by a wide margin (the pre-cache code parsed every child file per
/// request), while the seeded write stays a bounded test-setup cost.
const SEEDED_CHILDREN: usize = 60;
const SEEDED_MESSAGES_PER_CHILD: usize = 800;
const SEEDED_ASSISTANT_CONTENT_KB: usize = 12;

struct Daemon {
    child: Child,
    #[allow(dead_code)]
    socket: PathBuf,
}

impl Drop for Daemon {
    fn drop(&mut self) {
        let _ = self.child.kill();
        let _ = self.child.wait();
    }
}

// The timeout panic path cannot wait on the child; the test process exits and reaps it.
#[allow(clippy::zombie_processes)]
fn spawn_daemon(socket: &std::path::Path, agent_dir: &std::path::Path) -> Daemon {
    let binary = env!("CARGO_BIN_EXE_pa-daemon");
    let child = Command::new(binary)
        .arg("supervisor")
        .arg("--socket")
        .arg(socket)
        .arg("--agent-dir")
        .arg(agent_dir)
        .stdout(Stdio::null())
        .stderr(Stdio::null())
        .spawn()
        .expect("spawn pa-daemon supervisor");
    let deadline = Instant::now() + Duration::from_secs(10);
    while Instant::now() < deadline {
        if socket.exists() {
            return Daemon {
                child,
                socket: socket.to_path_buf(),
            };
        }
        std::thread::sleep(Duration::from_millis(20));
    }
    panic!("supervisor socket never appeared");
}

struct Client {
    reader: BufReader<UnixStream>,
    writer: UnixStream,
}

impl Client {
    fn connect(socket: &std::path::Path) -> (Self, serde_json::Value) {
        let deadline = Instant::now() + Duration::from_secs(5);
        loop {
            if let Ok(stream) = UnixStream::connect(socket) {
                let writer = stream.try_clone().expect("clone stream");
                let mut client = Client {
                    reader: BufReader::new(stream),
                    writer,
                };
                let hello = client.read_line();
                assert_eq!(hello["type"], "daemon_hello", "expected hello: {hello}");
                return (client, hello);
            } else if Instant::now() > deadline {
                panic!("daemon socket never accepted");
            }
            std::thread::sleep(Duration::from_millis(20));
        }
    }

    fn send(&mut self, value: &serde_json::Value) {
        let mut line = serde_json::to_string(value).expect("serialize");
        line.push('\n');
        self.writer.write_all(line.as_bytes()).expect("write");
        self.writer.flush().expect("flush");
    }

    fn send_command(&mut self, id: &str, command: &serde_json::Value) {
        self.send(&serde_json::json!({
            "type": "command",
            "id": id,
            "protocol": { "name": "prime-agent.daemon", "version": 7 },
            "command": command,
        }));
    }

    fn read_line(&mut self) -> serde_json::Value {
        let mut line = String::new();
        let read = self
            .reader
            .read_line(&mut line)
            .expect("read daemon line within the deadline");
        assert!(read > 0, "daemon closed the socket");
        serde_json::from_str(line.trim()).expect("a valid daemon line")
    }

    /// One command round trip with its elapsed wall time; non-response frames are skipped.
    fn read_response(&mut self, id: &str) -> (Duration, serde_json::Value) {
        let deadline = Instant::now() + Duration::from_secs(60);
        loop {
            assert!(Instant::now() < deadline, "no response for {id} arrived");
            let started = Instant::now();
            let line = self.read_line();
            if line["type"] == "response" && line["id"] == id {
                return (started.elapsed(), line);
            }
        }
    }
}

/// One scripted turn: prompt then drain until the turn-done boundary (the `session_tree_e2e`
/// contract).
fn scripted_turn(client: &mut Client, session_id: &str, text: &str, id: &str) {
    client.send_command(
        id,
        &serde_json::json!({ "type": "prompt", "activeSessionId": session_id, "message": text }),
    );
    let response = client.read_response(id);
    assert_eq!(
        response.1["success"], true,
        "prompt {id} failed: {}",
        response.1
    );
}

/// Seed one persisted child session file: a version-3 header plus a parent-
/// chained message run with usage records, sized in the megabytes so an inline
/// walk pays a real parse cost per child.
fn seed_child_session(dir: &std::path::Path, child_id: &str) {
    std::fs::create_dir_all(dir).expect("child dir");
    let file = dir.join(format!("{child_id}.jsonl"));
    let filler = "x".repeat(SEEDED_ASSISTANT_CONTENT_KB * 1024);
    let mut content = String::with_capacity(SEEDED_MESSAGES_PER_CHILD * (filler.len() + 256) + 512);
    content.push_str(
        &serde_json::json!({
            "type": "session", "version": 3, "id": child_id,
            "timestamp": "2024-01-01T00:00:00.000Z", "cwd": "/tmp",
        })
        .to_string(),
    );
    content.push('\n');
    let mut parent_id: Option<String> = None;
    for index in 0..SEEDED_MESSAGES_PER_CHILD {
        let entry_id = format!("{child_id}-m{index}");
        content.push_str(&serde_json::json!({
            "type": "message",
            "id": entry_id,
            "parentId": parent_id,
            "timestamp": "2024-01-01T00:00:00.000Z",
            "message": {
                "role": if index % 2 == 0 { "user" } else { "assistant" },
                "content": if index % 2 == 0 { "seed question" } else { &filler },
                "usage": if index % 2 == 1 {
                    serde_json::json!({
                        "input": 1000, "output": 100, "cacheRead": 0, "cacheWrite": 0,
                        "totalTokens": 1100,
                        "cost": { "input": 1, "output": 1, "cacheRead": 0, "cacheWrite": 0, "total": 2 },
                    })
                } else { serde_json::Value::Null },
            },
        }).to_string());
        content.push('\n');
        parent_id = Some(entry_id);
    }
    std::fs::write(&file, content).expect("write seeded child session");
}

#[test]
fn get_context_tree_answers_from_memory_on_a_grown_store() {
    let dir = tempfile::TempDir::new().expect("temp dir");
    let socket = dir.path().join("daemon.sock");
    let agent_dir = dir.path().join("agent");
    let session_dir = agent_dir.join("sessions");
    std::fs::create_dir_all(&session_dir).expect("agent dir");
    let _daemon = spawn_daemon(&socket, &agent_dir);
    let (mut client, _hello) = Client::connect(&socket);

    let script_path = dir.path().join("script.json");
    std::fs::write(
        &script_path,
        serde_json::json!({
            "responses": [
                { "text": "first answer", "delayMs": 10 },
                { "text": "second answer", "delayMs": 10 },
            ],
        })
        .to_string(),
    )
    .expect("write script");
    client.send_command(
        "c1",
        &serde_json::json!({
            "type": "create",
            "config": {
                "cwd": dir.path().to_string_lossy(),
                "sessionDir": session_dir.to_string_lossy(),
                "script": script_path.to_string_lossy(),
            },
        }),
    );
    let (_, created) = client.read_response("c1");
    assert_eq!(created["success"], true, "create failed: {created}");
    let active_session_id = created["data"]["id"]
        .as_str()
        .or_else(|| created["data"]["activeSessionId"].as_str())
        .expect("active session id")
        .to_string();
    let durable_session_id = created["data"]["sessionId"]
        .as_str()
        .expect("durable session id")
        .to_string();

    scripted_turn(&mut client, &active_session_id, "first question", "p1");
    scripted_turn(&mut client, &active_session_id, "second question", "p2");

    // The grown artifact tree: the durable id's `sub-*` children, each a
    // multi-megabyte session file (the walk's per-child parse cost).
    let artifacts_root = agent_dir
        .join("session-artifacts")
        .join(&durable_session_id);
    for index in 0..SEEDED_CHILDREN {
        let child_dir = artifacts_root.join(format!("sub-latency-{index:03}"));
        seed_child_session(&child_dir, &format!("latency-child-{index:03}"));
    }
    let seeded_bytes: u64 = (0..SEEDED_CHILDREN)
        .map(|index| {
            std::fs::metadata(
                artifacts_root
                    .join(format!("sub-latency-{index:03}"))
                    .join(format!("latency-child-{index:03}.jsonl")),
            )
            .expect("seeded file")
            .len()
        })
        .sum();

    // The attach warms the cache (the background walk fires here), like the
    // operator's chat surface attaching at open.
    client.send_command(
        "a1",
        &serde_json::json!({
            "type": "attach",
            "activeSessionId": active_session_id,
            "clientId": "latency-guard",
        }),
    );
    let (_, attached) = client.read_response("a1");
    assert_eq!(attached["success"], true, "attach failed: {attached}");

    // Every read must answer under the ceiling, cold or warm: the root is in-memory
    // and the children come from the cache (a cold cache serves the live rows alone).
    let mut saw_seeded_children = false;
    let deadline = Instant::now() + CACHE_FILL_DEADLINE;
    let mut reads = 0;
    while Instant::now() < deadline {
        reads += 1;
        let id = format!("ctx{reads}");
        client.send_command(
            &id,
            &serde_json::json!({
                "type": "get_context_tree",
                "activeSessionId": active_session_id,
            }),
        );
        let (elapsed, tree) = client.read_response(&id);
        assert_eq!(tree["success"], true, "get_context_tree failed: {tree}");
        let children = tree["data"]["children"].as_array().expect("children");
        eprintln!(
            "context-latency guard: read #{reads} took {elapsed:?} ({} children), \
             seeded tree {seeded_bytes} bytes",
            children.len()
        );
        assert!(
            elapsed <= CONTEXT_RESPONSE_CEILING,
            "get_context_tree read #{reads} took {elapsed:?} (ceiling {CONTEXT_RESPONSE_CEILING:?}) — \
             the inline artifact walk is back (the operator's 10s /context timeout class); \
             seeded tree: {seeded_bytes} bytes",
        );
        if children.len() >= SEEDED_CHILDREN {
            // The persisted rows carry the seeded files' real usage (the background walk parsed
            // them, not the request path).
            let with_usage = children
                .iter()
                .filter(|node| node["totalUsage"]["input"].as_u64().unwrap_or(0) > 0)
                .count();
            assert_eq!(
                with_usage, SEEDED_CHILDREN,
                "persisted children must carry the seeded usage"
            );
            saw_seeded_children = true;
            break;
        }
        std::thread::sleep(Duration::from_millis(100));
    }
    assert!(
        saw_seeded_children,
        "the background refresh never filled the cache with the seeded children \
         (expected {SEEDED_CHILDREN} within {CACHE_FILL_DEADLINE:?})"
    );
}