diff --git a/Cargo.lock b/Cargo.lock index 04c05d1..64af826 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1126,8 +1126,10 @@ dependencies = [ "quote", "rust_decimal", "serde", + "serde_json", "syn 2.0.119", "tempfile", + "uuid", ] [[package]] @@ -1755,6 +1757,12 @@ dependencies = [ "serde_core", ] +[[package]] +name = "sha1_smol" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bbfa15b3dddfee50a0fff136974b3e1bde555604ba463834a7eb7deb6417705d" + [[package]] name = "sha2" version = "0.10.9" @@ -2097,6 +2105,7 @@ checksum = "2ef6dac1e96601b4fb3acccccff2139741fcb757cb9a36089bf5be91cfb285ce" dependencies = [ "getrandom 0.4.3", "js-sys", + "sha1_smol", "wasm-bindgen", ] diff --git a/crates/mind-body/Cargo.toml b/crates/mind-body/Cargo.toml index 14da03e..efdcc4b 100644 --- a/crates/mind-body/Cargo.toml +++ b/crates/mind-body/Cargo.toml @@ -11,11 +11,15 @@ publish.workspace = true # binary) and the one fail-closed reader every acting organ calls # (`control::read_effective`). It also holds the launch primitive (`launch`, # `queue`): the one way her organs open a run and start its unit. The waker -# (`wake`, the `mind-wake` binary) is one cycle over them. +# (`wake`, the `mind-wake` binary) is one cycle over them. The actuator +# (`self_run`, the `mind-self-run` binary) carries her one open Self run. # # `rust_decimal` is the dial's number type: heat, cadence and the run cap are # money-adjacent and the tests pin exact products, which a float would blur. It # serialises as a string, so the stored document carries no float. +# +# `serde_json` reads Claude Code's stream-json, a foreign owner's wire (`self_run`); +# `uuid`'s v5 makes a Self run's session id derivable from the run's id. [dependencies] anyhow = "1" chrono = { version = "0.4.44", features = ["serde"] } @@ -26,6 +30,8 @@ eureka-state = { path = "../eureka-state", default-features = false } huginn-mind = { path = "../huginn-mind" } rust_decimal = { version = "1", default-features = false, features = ["std", "serde"] } serde = { version = "1", features = ["derive"] } +serde_json = "1" +uuid = { version = "1", default-features = false, features = ["v5"] } [target.'cfg(unix)'.dependencies] libc = "0.2" @@ -50,3 +56,7 @@ path = "src/main.rs" [[bin]] name = "mind-wake" path = "src/bin/mind-wake.rs" + +[[bin]] +name = "mind-self-run" +path = "src/bin/mind-self-run.rs" diff --git a/crates/mind-body/README.md b/crates/mind-body/README.md index 5838632..1310d49 100644 --- a/crates/mind-body/README.md +++ b/crates/mind-body/README.md @@ -1,7 +1,14 @@ # mind-body -The Mind's body: the control store, the launch primitive and the `mind-wake` -one-shot. +The Mind's body: the control store, the launch primitive, the `mind-wake` +one-shot and the `mind-self-run` actuator. + +`mind-self-run --instance [--endpoint rudp://:]` runs inside +`mind-self@`. It finds her one open Self run, starts `claude` over +stream-json with a fixed argument list, asks for the flush once at the context +ceiling and interrupts after a grace. It exits 0 when the child exited 0, 1 +otherwise, 3 when she has no single open Self run and 2 when the arguments are +wrong or the mind cannot be reached. ## The container lane @@ -25,3 +32,17 @@ Mutation testing of the waker needs the glob form, because `-f src/wake.rs` finds no mutants in this workspace: `cargo mutants -p mind-body -f "**/src/wake.rs" -f "**/src/bin/mind-wake.rs"`. The second glob reaches `host()`, whose mutants only the container lane kills. + +The actuator has its own ignored test, `the_real_bin_finds_her_run_...` in +`tests/self_run.rs`: the real `mind-self-run` against the real daemon with a +fake `claude` first on `PATH`. It needs the daemon built beside the binary and a +POSIX shell, not root: + +```sh +cargo build -p huginn-daemon -p mind-body --all-targets && cargo test -p mind-body --test self_run -- --include-ignored +``` + +Mutation testing of the actuator, with the globs that reach the library and the +binary: `cargo mutants -p mind-body -f "**/src/self_run.rs" -f +"**/src/bin/mind-self-run.rs"`. The binary's mutants are killed only by the +lane above. diff --git a/crates/mind-body/src/bin/mind-self-run.rs b/crates/mind-body/src/bin/mind-self-run.rs new file mode 100644 index 0000000..04bc20d --- /dev/null +++ b/crates/mind-body/src/bin/mind-self-run.rs @@ -0,0 +1,44 @@ +//! `mind-self-run --instance [--endpoint rudp://:]`: carries +//! her one open Self run (`mind_body::self_run::carry`) inside +//! `mind-self@`. This reads the process's arguments and environment, +//! the things the library never reads, and prints the request lines and the +//! outcome. It exits 0 when the child exited 0, 1 when it exited otherwise or by +//! a signal, 3 when she has no single open Self run (no child started), and 2 +//! when the mind cannot be reached or the arguments are wrong. Without +//! `--endpoint` the daemon is `HUGINN_ENDPOINT`. + +use std::time::Duration; + +use anyhow::Result; +use eureka_state::HuginnClient; +use mind_body::cli::instance_and_endpoint; +use mind_body::launch::SystemClock; +use mind_body::queue::HuginnMind; +use mind_body::self_run::{ClaudeSpawner, carry}; + +/// How long one call to the mind may take. +const TIMEOUT: Duration = Duration::from_secs(15); + +const USAGE: &str = "usage: mind-self-run --instance [--endpoint rudp://:]"; + +// The binary is the one place that reads the process: its arguments and the +// daemon's address. The library reads neither (clippy.toml, `#![forbid]` in lib.rs). +#[allow(clippy::disallowed_methods, reason = "the binary reads its own arguments and the daemon's address, and hands them to the library")] +fn run() -> Result { + let args: Vec = std::env::args().skip(1).collect(); + let (instance, address) = instance_and_endpoint(&args, std::env::var("HUGINN_ENDPOINT").ok(), USAGE)?; + let mind = HuginnMind::new(HuginnClient::new(address, instance, TIMEOUT)); + let outcome = carry(&mind, &SystemClock, &ClaudeSpawner, &mut std::io::stdout())?; + println!("{outcome}"); + Ok(outcome.exit_status()) +} + +fn main() { + match run() { + Ok(status) => std::process::exit(status), + Err(error) => { + eprintln!("mind-self-run: {error:#}"); + std::process::exit(2); + } + } +} diff --git a/crates/mind-body/src/launch.rs b/crates/mind-body/src/launch.rs index e135c2c..9b7d45b 100644 --- a/crates/mind-body/src/launch.rs +++ b/crates/mind-body/src/launch.rs @@ -273,8 +273,9 @@ fn close_dead(mind: &dyn MindPort, run: &PipelineRef, now: DateTime) -> Res /// The in-force runs of hers (operated by the mind) of `turn` on `instance`, /// read from typed facts: the holders of that turn's slot. The one reader of -/// the slot, for step 0 and for checking a holder admission names. -fn holders(mind: &dyn MindPort, instance: &Slug, turn: RunTurn) -> Result> { +/// the slot, for step 0 and for checking a holder admission names. `self_run` +/// reads her open Self run through it too. +pub(crate) fn holders(mind: &dyn MindPort, instance: &Slug, turn: RunTurn) -> Result> { let runs = headers(mind, &in_force(any_of(of_kind(PipelineKind::Run), "root", &[&instance.0])))?; Ok(runs .into_iter() diff --git a/crates/mind-body/src/lib.rs b/crates/mind-body/src/lib.rs index 4a86f31..8b56146 100644 --- a/crates/mind-body/src/lib.rs +++ b/crates/mind-body/src/lib.rs @@ -10,6 +10,10 @@ //! opens a run in the mind and only then starts its unit. //! //! `wake` is the waker's one cycle over them: the `mind-wake` binary. +//! +//! `self_run` carries one Self run inside `mind-self@`: the +//! `mind-self-run` binary runs `claude` over stream-json for her one open Self +//! run, asks for the flush at the context ceiling and interrupts after a grace. // The control root is a constant, so the library reads no part of the process // environment (clippy.toml lists the readers); `forbid` keeps an `allow` from @@ -23,6 +27,7 @@ pub mod cli; pub mod control; pub mod launch; pub mod queue; +pub mod self_run; pub mod wake; #[cfg(test)] diff --git a/crates/mind-body/src/self_run.rs b/crates/mind-body/src/self_run.rs new file mode 100644 index 0000000..422dafe --- /dev/null +++ b/crates/mind-body/src/self_run.rs @@ -0,0 +1,331 @@ +//! The Self-run actuator: one `claude` child carries her one open Self run. +//! +//! `carry` finds her run (the one in-force Self run operated by the mind, read +//! through `launch::holders`: the run is the grant), starts `claude` with the +//! fixed argument list, writes the start message and watches the child's +//! stream-json output. It counts the main thread's requests, asks for the flush +//! once when the context prefix reaches the ceiling (ruling +//! `eureka-body:ruling:self-lifecycle-flush-rehydrate`: a Self run admits what +//! the mind lacks and ends, it never summarises), closes the child's input at +//! the first result, and interrupts at most once, after a grace or at the hard +//! limit. +//! +//! The actuator admits nothing, reads neither the brake nor the environment and +//! stops no unit: the gate and the brake hold own those. It never echoes an +//! argument, an endpoint or a stream line. Stream-json is Claude Code's wire, a +//! foreign owner's JSON: it is parsed into `serde_json::Value` here and nothing +//! parsed is stored. + +use std::collections::HashSet; +use std::fmt; +use std::io::{BufRead, BufReader, Write}; +use std::path::Path; +use std::process::{Child as OsChild, ChildStdin, Command, Stdio}; +use std::sync::mpsc::{Receiver, RecvTimeoutError, channel}; +use std::time::Duration as StdDuration; + +use anyhow::{Context, Result, anyhow, bail}; +use chrono::{DateTime, Duration, Utc}; +use cultnet_rs::Selection; +use eureka_pipeline::{PipelineDocument, PipelineRef, RunTurn, Slug}; +use huginn_mind::PipelinePageItems; +use serde_json::{Value, json}; +use uuid::Uuid; + +use crate::launch::{Clock, holders}; +use crate::queue::MindPort; + +/// The measured Self ceiling: at this prefix the flush is asked for. +pub const CEILING_TOKENS: u64 = 300_000; +/// What the flush may add to the prefix before the actuator interrupts. +pub const FLUSH_ALLOWANCE_TOKENS: u64 = 60_000; +/// How long the child may take over the flush before it is interrupted. +pub const FLUSH_GRACE_S: i64 = 15 * 60; +/// A backstop `claude` enforces; the ceiling is reached first. +pub const MAX_TURNS: u32 = 500; +/// The name the start message tells her to admit as. +pub const AGENT: &str = "mind-self-run"; +/// The namespace of the session id: the run id is the name in it. +pub const SESSION_NAMESPACE: Uuid = Uuid::NAMESPACE_OID; + +/// The longest the loop waits on the child when no deadline is nearer. +const POLL: StdDuration = StdDuration::from_secs(60); + +/// What the child is told at the start. The contract with the `mind-self` +/// skill: it names the skill, the instance and the run id. +pub fn start_message(instance: &Slug, run: &PipelineRef) -> String { + format!( + "Use the mind-self skill. You are Self for the instance `{}`; your run is `{}`. Admit as faculty SelfFaculty, agent {AGENT}.", + instance.0, run.id.0 + ) +} + +/// What the child is told once, at the context ceiling. +pub const FLUSH_MESSAGE: &str = "The context ceiling is reached. Admit to the mind what it lacks, close your run, and end."; + +/// The session id of a run: UUIDv5 over the run's id. Derived, never stored. +pub fn session_id(run: &PipelineRef) -> Uuid { + Uuid::new_v5(&SESSION_NAMESPACE, run.id.0.as_bytes()) +} + +/// The argument list of the child, and nothing else: no resume, continue or +/// fork, no model, no permission mode and no settings belong to this actuator. +/// `budget_usd` is the run's, as stored. +pub fn claude_args(session: Uuid, budget_usd: &str) -> Vec { + [ + "-p", + "--input-format", + "stream-json", + "--output-format", + "stream-json", + "--verbose", + "--session-id", + &session.to_string(), + "--max-budget-usd", + budget_usd, + "--max-turns", + &MAX_TURNS.to_string(), + "--permission-prompts", + "none", + ] + .map(String::from) + .to_vec() +} + +/// One line of the child's stream, as far as the actuator reads it. +#[derive(Clone, Debug, PartialEq, Eq)] +pub enum Usage { + /// A request of the main thread: the message's id and the context it + /// read (input, cache read and cache write tokens). + Request { id: String, prefix: u64 }, + Result, + /// Valid JSON that is none of the above, or a request of a subagent. + Other, + /// Not a JSON object. + Unreadable, +} + +/// Reads one stream line. +pub fn read_usage(line: &str) -> Usage { + let Ok(Value::Object(event)) = serde_json::from_str::(line) else { return Usage::Unreadable }; + match event.get("type").and_then(Value::as_str) { + Some("result") => Usage::Result, + Some("assistant") if event.get("parent_tool_use_id").is_none_or(Value::is_null) => { + let message = event.get("message"); + let Some(id) = message.and_then(|message| message.get("id")).and_then(Value::as_str) else { return Usage::Other }; + let usage = message.and_then(|message| message.get("usage")); + let field = |name: &str| usage.and_then(|usage| usage.get(name)).and_then(Value::as_u64).unwrap_or(0); + Usage::Request { id: id.to_string(), prefix: field("input_tokens") + field("cache_read_input_tokens") + field("cache_creation_input_tokens") } + } + _ => Usage::Other, + } +} + +/// What the child's stdout gave in one wait. +#[derive(Clone, Debug, PartialEq, Eq)] +pub enum Read { + Line(String), + Timeout, + /// The stream ended: the child is gone or going. + Closed, +} + +/// The running `claude`. A test hands in a scripted one. +pub trait Child { + /// Writes one line to its input. A dead child's stream closes, which is + /// how `carry` learns of it, so a failed write is not an error here. + fn write_line(&mut self, line: &str); + /// Closes its input; later writes go nowhere. + fn close_input(&mut self); + fn next_line(&mut self, timeout: StdDuration) -> Read; + /// Asks it to stop as an operator's Ctrl-C does. + fn interrupt(&mut self); + /// Its exit code, none when a signal ended it. + fn wait(&mut self) -> Option; +} + +pub trait Spawner { + fn spawn(&self, args: &[String]) -> Result>; +} + +/// Why a run ended. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub enum Cause { + /// The child ended with no flush asked for and no interrupt. + Own, + /// The ceiling was reached and the flush asked for; the child ended itself. + Flushed, + /// The actuator interrupted the child, after the grace or at the hard limit. + Interrupted, +} + +#[derive(Clone, Debug, PartialEq, Eq)] +pub enum SelfRunOutcome { + /// She has not exactly one open Self run: nothing was started. + NoRun, + Ended { cause: Cause, code: Option, requests: u64, prefix: u64 }, +} + +impl SelfRunOutcome { + /// 0 when the child exited 0, 3 when no child was started, else 1 (a + /// child killed by a signal has no code). + pub fn exit_status(&self) -> i32 { + match self { + Self::NoRun => 3, + Self::Ended { code: Some(0), .. } => 0, + Self::Ended { .. } => 1, + } + } +} + +impl fmt::Display for SelfRunOutcome { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + match self { + Self::NoRun => write!(f, "no run: she has no single open Self run"), + Self::Ended { cause, code, requests, prefix } => { + let cause = match cause { + Cause::Own => "own", + Cause::Flushed => "flushed", + Cause::Interrupted => "interrupted", + }; + match code { + Some(code) => write!(f, "ended: {cause} code {code} requests {requests} prefix {prefix}"), + None => write!(f, "ended: {cause} signal requests {requests} prefix {prefix}"), + } + } + } + } +} + +/// Her one open Self run and its budget, read through the launch primitive's +/// holder reader. Anything but exactly one run is none. +fn her_run(mind: &dyn MindPort) -> Result> { + let mut held = holders(mind, mind.instance(), RunTurn::SelfRun)?; + let (Some(run), None) = (held.pop(), held.pop()) else { return Ok(None) }; + let one = Selection { keys: Some(vec![run.id.id.0.clone()]), projection: "document".into(), ..Selection::default() }; + let PipelinePageItems::Documents(views) = mind.query(&one)?.items else { bail!("the mind answered headers to a document selection") }; + match views.into_iter().next().map(|view| view.document) { + Some(PipelineDocument::Run(document)) => Ok(Some((run.id, document.budget_usd.0))), + _ => Err(anyhow!("the run's document could not be read")), + } +} + +/// Carries her one open Self run: one `claude` child from the start message to +/// its end. The request lines go to `out`. A mind that cannot be reached or a +/// child that cannot be started is an error; every other end is an outcome. +pub fn carry(mind: &dyn MindPort, clock: &dyn Clock, spawner: &dyn Spawner, out: &mut dyn Write) -> Result { + let Some((run, budget)) = her_run(mind)? else { return Ok(SelfRunOutcome::NoRun) }; + let mut child = spawner.spawn(&claude_args(session_id(&run), &budget))?; + let user_line = |text: &str| json!({"type": "user", "message": {"role": "user", "content": text}}).to_string(); + child.write_line(&user_line(&start_message(mind.instance(), &run))); + + let (mut seen, mut requests, mut prefix) = (HashSet::new(), 0u64, 0u64); + let (mut result_seen, mut flushed, mut interrupted) = (false, false, false); + let mut grace_ends: Option> = None; + loop { + let wait = match grace_ends.filter(|_| !interrupted) { + Some(ends) => (ends - clock.now()).to_std().unwrap_or_default().min(POLL), + None => POLL, + }; + match child.next_line(wait) { + Read::Line(line) => match read_usage(&line) { + Usage::Request { id, prefix: newest } => { + if seen.insert(id) { + requests += 1; + prefix = newest; + writeln!(out, "request {requests} prefix {prefix}")?; + } + } + Usage::Result if !result_seen => { + result_seen = true; + child.close_input(); + } + _ => {} + }, + Read::Timeout => {} + Read::Closed => break, + } + if !flushed && !result_seen && prefix >= CEILING_TOKENS { + child.write_line(&user_line(FLUSH_MESSAGE)); + flushed = true; + grace_ends = Some(clock.now() + Duration::seconds(FLUSH_GRACE_S)); + } + if !interrupted && (prefix >= CEILING_TOKENS + FLUSH_ALLOWANCE_TOKENS || grace_ends.is_some_and(|ends| clock.now() >= ends)) { + child.interrupt(); + interrupted = true; + } + } + let code = child.wait(); + let cause = if interrupted { + Cause::Interrupted + } else if flushed { + Cause::Flushed + } else { + Cause::Own + }; + Ok(SelfRunOutcome::Ended { cause, code, requests, prefix }) +} + +/// The real spawner: the `claude` on the unit's `PATH`, with the unit's +/// environment (the library names no variable), its stderr in the journal. +pub struct ClaudeSpawner; + +impl Spawner for ClaudeSpawner { + fn spawn(&self, args: &[String]) -> Result> { + spawn_program(Path::new("claude"), args) + } +} + +fn spawn_program(program: &Path, args: &[String]) -> Result> { + let mut process = Command::new(program).args(args).stdin(Stdio::piped()).stdout(Stdio::piped()).stderr(Stdio::inherit()).spawn().context("claude could not be started")?; + let stdin = process.stdin.take(); + let stdout = process.stdout.take().ok_or_else(|| anyhow!("claude has no stdout"))?; + let (sender, lines) = channel(); + std::thread::spawn(move || { + for line in BufReader::new(stdout).split(b'\n') { + let Ok(bytes) = line else { return }; + if sender.send(String::from_utf8_lossy(&bytes).into_owned()).is_err() { + return; + } + } + }); + Ok(Box::new(ClaudeProcess { process, stdin, lines })) +} + +struct ClaudeProcess { + process: OsChild, + stdin: Option, + lines: Receiver, +} + +impl Child for ClaudeProcess { + fn write_line(&mut self, line: &str) { + if let Some(stdin) = self.stdin.as_mut() { + let _ = stdin.write_all(line.as_bytes()).and_then(|()| stdin.write_all(b"\n")).and_then(|()| stdin.flush()); + } + } + + fn close_input(&mut self) { + self.stdin = None; + } + + fn next_line(&mut self, timeout: StdDuration) -> Read { + match self.lines.recv_timeout(timeout) { + Ok(line) => Read::Line(line), + Err(RecvTimeoutError::Timeout) => Read::Timeout, + Err(RecvTimeoutError::Disconnected) => Read::Closed, + } + } + + fn interrupt(&mut self) { + // `kill` the program, not a libc call: the library forbids unsafe code. + let _ = Command::new("kill").args(["-INT", &self.process.id().to_string()]).status(); + } + + fn wait(&mut self) -> Option { + self.process.wait().ok().and_then(|status| status.code()) + } +} + +#[cfg(test)] +mod tests; diff --git a/crates/mind-body/src/self_run/claude-2.1.268-assistant-usage-records.jsonl b/crates/mind-body/src/self_run/claude-2.1.268-assistant-usage-records.jsonl new file mode 100644 index 0000000..fc0a08d --- /dev/null +++ b/crates/mind-body/src/self_run/claude-2.1.268-assistant-usage-records.jsonl @@ -0,0 +1,12 @@ +{"type": "assistant", "parent_tool_use_id": null, "message": {"id": "msg_011CgHNLSfEPL5Th8GSs7vkj", "model": "claude-opus-5-5", "stop_reason": "tool_use", "usage": {"input_tokens": 2, "cache_creation_input_tokens": 39033, "cache_read_input_tokens": 45175, "output_tokens": 73, "output_tokens_details": {"thinking_tokens": 22}, "server_tool_use": {"web_search_requests": 0, "web_fetch_requests": 0}, "service_tier": "standard", "cache_creation": {"ephemeral_1h_input_tokens": 39033, "ephemeral_5m_input_tokens": 0}, "inference_geo": "global", "iterations": [{"input_tokens": 2, "output_tokens": 73, "cache_read_input_tokens": 45175, "cache_creation_input_tokens": 39033, "cache_creation": {"ephemeral_5m_input_tokens": 0, "ephemeral_1h_input_tokens": 39033}, "type": "message"}], "speed": "standard", "fallback_credit": null}, "content": [{"type": "thinking"}]}} +{"type": "assistant", "parent_tool_use_id": null, "message": {"id": "msg_011CgHNLSfEPL5Th8GSs7vkj", "model": "claude-opus-5-5", "stop_reason": "tool_use", "usage": {"input_tokens": 2, "cache_creation_input_tokens": 39033, "cache_read_input_tokens": 45175, "output_tokens": 73, "output_tokens_details": {"thinking_tokens": 22}, "server_tool_use": {"web_search_requests": 0, "web_fetch_requests": 0}, "service_tier": "standard", "cache_creation": {"ephemeral_1h_input_tokens": 39033, "ephemeral_5m_input_tokens": 0}, "inference_geo": "global", "iterations": [{"input_tokens": 2, "output_tokens": 73, "cache_read_input_tokens": 45175, "cache_creation_input_tokens": 39033, "cache_creation": {"ephemeral_5m_input_tokens": 0, "ephemeral_1h_input_tokens": 39033}, "type": "message"}], "speed": "standard", "fallback_credit": null}, "content": [{"type": "tool_use"}]}} +{"type": "assistant", "parent_tool_use_id": null, "message": {"id": "msg_011CgHNLcAxtUhNghfqBmEjA", "model": "claude-opus-5-5", "stop_reason": "tool_use", "usage": {"input_tokens": 2, "cache_creation_input_tokens": 16541, "cache_read_input_tokens": 84208, "output_tokens": 136, "output_tokens_details": {"thinking_tokens": 0}, "server_tool_use": {"web_search_requests": 0, "web_fetch_requests": 0}, "service_tier": "standard", "cache_creation": {"ephemeral_1h_input_tokens": 16541, "ephemeral_5m_input_tokens": 0}, "inference_geo": "global", "iterations": [{"input_tokens": 2, "output_tokens": 136, "cache_read_input_tokens": 84208, "cache_creation_input_tokens": 16541, "cache_creation": {"ephemeral_5m_input_tokens": 0, "ephemeral_1h_input_tokens": 16541}, "type": "message"}], "speed": "standard", "fallback_credit": null}, "content": [{"type": "tool_use"}]}} +{"type": "assistant", "parent_tool_use_id": null, "message": {"id": "msg_011CgHNLjffqfsnx2t6FFAh9", "model": "claude-opus-5-5", "stop_reason": "tool_use", "usage": {"input_tokens": 2, "cache_creation_input_tokens": 16423, "cache_read_input_tokens": 100749, "output_tokens": 282, "output_tokens_details": {"thinking_tokens": 0}, "server_tool_use": {"web_search_requests": 0, "web_fetch_requests": 0}, "service_tier": "standard", "cache_creation": {"ephemeral_1h_input_tokens": 16423, "ephemeral_5m_input_tokens": 0}, "inference_geo": "global", "iterations": [{"input_tokens": 2, "output_tokens": 282, "cache_read_input_tokens": 100749, "cache_creation_input_tokens": 16423, "cache_creation": {"ephemeral_5m_input_tokens": 0, "ephemeral_1h_input_tokens": 16423}, "type": "message"}], "speed": "standard", "fallback_credit": null}, "content": [{"type": "tool_use"}]}} +{"type": "assistant", "parent_tool_use_id": null, "message": {"id": "msg_011CgHNLjffqfsnx2t6FFAh9", "model": "claude-opus-5-5", "stop_reason": "tool_use", "usage": {"input_tokens": 2, "cache_creation_input_tokens": 16423, "cache_read_input_tokens": 100749, "output_tokens": 282, "output_tokens_details": {"thinking_tokens": 0}, "server_tool_use": {"web_search_requests": 0, "web_fetch_requests": 0}, "service_tier": "standard", "cache_creation": {"ephemeral_1h_input_tokens": 16423, "ephemeral_5m_input_tokens": 0}, "inference_geo": "global", "iterations": [{"input_tokens": 2, "output_tokens": 282, "cache_read_input_tokens": 100749, "cache_creation_input_tokens": 16423, "cache_creation": {"ephemeral_5m_input_tokens": 0, "ephemeral_1h_input_tokens": 16423}, "type": "message"}], "speed": "standard", "fallback_credit": null}, "content": [{"type": "tool_use"}]}} +{"type": "assistant", "parent_tool_use_id": null, "message": {"id": "msg_011CgHNMiXFAsUftWZLhShSJ", "model": "claude-opus-5-5", "stop_reason": "tool_use", "usage": {"input_tokens": 2, "cache_creation_input_tokens": 1162, "cache_read_input_tokens": 117172, "output_tokens": 303, "output_tokens_details": {"thinking_tokens": 26}, "server_tool_use": {"web_search_requests": 0, "web_fetch_requests": 0}, "service_tier": "standard", "cache_creation": {"ephemeral_1h_input_tokens": 1162, "ephemeral_5m_input_tokens": 0}, "inference_geo": "global", "iterations": [{"input_tokens": 2, "output_tokens": 303, "cache_read_input_tokens": 117172, "cache_creation_input_tokens": 1162, "cache_creation": {"ephemeral_5m_input_tokens": 0, "ephemeral_1h_input_tokens": 1162}, "type": "message"}], "speed": "standard", "fallback_credit": null}, "content": [{"type": "thinking"}]}} +{"type": "assistant", "parent_tool_use_id": null, "message": {"id": "msg_011CgHNMiXFAsUftWZLhShSJ", "model": "claude-opus-5-5", "stop_reason": "tool_use", "usage": {"input_tokens": 2, "cache_creation_input_tokens": 1162, "cache_read_input_tokens": 117172, "output_tokens": 303, "output_tokens_details": {"thinking_tokens": 26}, "server_tool_use": {"web_search_requests": 0, "web_fetch_requests": 0}, "service_tier": "standard", "cache_creation": {"ephemeral_1h_input_tokens": 1162, "ephemeral_5m_input_tokens": 0}, "inference_geo": "global", "iterations": [{"input_tokens": 2, "output_tokens": 303, "cache_read_input_tokens": 117172, "cache_creation_input_tokens": 1162, "cache_creation": {"ephemeral_5m_input_tokens": 0, "ephemeral_1h_input_tokens": 1162}, "type": "message"}], "speed": "standard", "fallback_credit": null}, "content": [{"type": "tool_use"}]}} +{"type": "assistant", "parent_tool_use_id": null, "message": {"id": "msg_011CgHNN8DmkfNuiMD3mFFg1", "model": "claude-opus-5-5", "stop_reason": "tool_use", "usage": {"input_tokens": 2, "cache_creation_input_tokens": 8410, "cache_read_input_tokens": 118334, "output_tokens": 299, "output_tokens_details": {"thinking_tokens": 166}, "server_tool_use": {"web_search_requests": 0, "web_fetch_requests": 0}, "service_tier": "standard", "cache_creation": {"ephemeral_1h_input_tokens": 8410, "ephemeral_5m_input_tokens": 0}, "inference_geo": "global", "iterations": [{"input_tokens": 2, "output_tokens": 299, "cache_read_input_tokens": 118334, "cache_creation_input_tokens": 8410, "cache_creation": {"ephemeral_5m_input_tokens": 0, "ephemeral_1h_input_tokens": 8410}, "type": "message"}], "speed": "standard", "fallback_credit": null}, "content": [{"type": "thinking"}]}} +{"type": "assistant", "parent_tool_use_id": null, "message": {"id": "msg_011CgHNN8DmkfNuiMD3mFFg1", "model": "claude-opus-5-5", "stop_reason": "tool_use", "usage": {"input_tokens": 2, "cache_creation_input_tokens": 8410, "cache_read_input_tokens": 118334, "output_tokens": 299, "output_tokens_details": {"thinking_tokens": 166}, "server_tool_use": {"web_search_requests": 0, "web_fetch_requests": 0}, "service_tier": "standard", "cache_creation": {"ephemeral_1h_input_tokens": 8410, "ephemeral_5m_input_tokens": 0}, "inference_geo": "global", "iterations": [{"input_tokens": 2, "output_tokens": 299, "cache_read_input_tokens": 118334, "cache_creation_input_tokens": 8410, "cache_creation": {"ephemeral_5m_input_tokens": 0, "ephemeral_1h_input_tokens": 8410}, "type": "message"}], "speed": "standard", "fallback_credit": null}, "content": [{"type": "tool_use"}]}} +{"type": "assistant", "parent_tool_use_id": null, "message": {"id": "msg_011CgHNNRZ46gcJVp99ra6zp", "model": "claude-opus-5-5", "stop_reason": "tool_use", "usage": {"input_tokens": 2, "cache_creation_input_tokens": 504, "cache_read_input_tokens": 126744, "output_tokens": 137, "output_tokens_details": {"thinking_tokens": 0}, "server_tool_use": {"web_search_requests": 0, "web_fetch_requests": 0}, "service_tier": "standard", "cache_creation": {"ephemeral_1h_input_tokens": 504, "ephemeral_5m_input_tokens": 0}, "inference_geo": "global", "iterations": [{"input_tokens": 2, "output_tokens": 137, "cache_read_input_tokens": 126744, "cache_creation_input_tokens": 504, "cache_creation": {"ephemeral_5m_input_tokens": 0, "ephemeral_1h_input_tokens": 504}, "type": "message"}], "speed": "standard", "fallback_credit": null}, "content": [{"type": "text"}]}} +{"type": "assistant", "parent_tool_use_id": null, "message": {"id": "msg_011CgHNNRZ46gcJVp99ra6zp", "model": "claude-opus-5-5", "stop_reason": "tool_use", "usage": {"input_tokens": 2, "cache_creation_input_tokens": 504, "cache_read_input_tokens": 126744, "output_tokens": 137, "output_tokens_details": {"thinking_tokens": 0}, "server_tool_use": {"web_search_requests": 0, "web_fetch_requests": 0}, "service_tier": "standard", "cache_creation": {"ephemeral_1h_input_tokens": 504, "ephemeral_5m_input_tokens": 0}, "inference_geo": "global", "iterations": [{"input_tokens": 2, "output_tokens": 137, "cache_read_input_tokens": 126744, "cache_creation_input_tokens": 504, "cache_creation": {"ephemeral_5m_input_tokens": 0, "ephemeral_1h_input_tokens": 504}, "type": "message"}], "speed": "standard", "fallback_credit": null}, "content": [{"type": "tool_use"}]}} +{"type": "assistant", "parent_tool_use_id": null, "message": {"id": "msg_011CgHNNaiDHie99GoJBYBDt", "model": "claude-opus-5-5", "stop_reason": "tool_use", "usage": {"input_tokens": 2, "cache_creation_input_tokens": 2603, "cache_read_input_tokens": 127248, "output_tokens": 553, "output_tokens_details": {"thinking_tokens": 0}, "server_tool_use": {"web_search_requests": 0, "web_fetch_requests": 0}, "service_tier": "standard", "cache_creation": {"ephemeral_1h_input_tokens": 2603, "ephemeral_5m_input_tokens": 0}, "inference_geo": "global", "iterations": [{"input_tokens": 2, "output_tokens": 553, "cache_read_input_tokens": 127248, "cache_creation_input_tokens": 2603, "cache_creation": {"ephemeral_5m_input_tokens": 0, "ephemeral_1h_input_tokens": 2603}, "type": "message"}], "speed": "standard", "fallback_credit": null}, "content": [{"type": "tool_use"}]}} diff --git a/crates/mind-body/src/self_run/claude-2.1.268-stream-json-probe.jsonl b/crates/mind-body/src/self_run/claude-2.1.268-stream-json-probe.jsonl new file mode 100644 index 0000000..81e249c --- /dev/null +++ b/crates/mind-body/src/self_run/claude-2.1.268-stream-json-probe.jsonl @@ -0,0 +1,3 @@ +{"type":"system","subtype":"init","cwd":"C:\\Users\\Meta\\AppData\\Local\\Temp\\claude\\F--Projects-CultLib\\21443208-468a-45eb-ab1d-a731c1c79772\\scratchpad\\imag-self-run\\p1","session_id":"7a1c2d3e-4b5a-5c6d-8e7f-0a1b2c3d4e5f","tools":["Task","Bash","CronCreate","CronDelete","CronList","DesignSync","Edit","EnterWorktree","ExitWorktree","Glob","Grep","ListAgents","Monitor","NotebookEdit","PowerShell","PushNotification","Read","ReportFindings","ScheduleWakeup","SendMessage","Skill","TaskCreate","TaskGet","TaskList","TaskOutput","TaskStop","TaskUpdate","ToolSearch","WebFetch","WebSearch","Workflow","Write"],"mcp_servers":[],"model":"claude-haiku-4-5-20251001","permissionMode":"default","slash_commands":["deep-research","design","design-sync","dataviz","update-config","verify","debug","code-review","simplify","batch","fewer-permission-prompts","doctor","loop","claude-api","workflow-authoring","run","run-skill-generator","plugin-authoring","advisor","agents","auto-mode-setup","autocompact","clear","color","compact","config","context","effort","fast","heapdump","init","mcp","import","model","__remote-workflow","workflow-launch-exec","plugin-types","reload-plugins","reload-skills","rename","security-review","usage","insights","recap","skill-doctor","goal","design-consent","design-revoke","list-agents","team-onboarding"],"terminal_slash_commands":["doctor","color","reload-plugins"],"apiKeySource":"none","claude_code_version":"2.1.268","output_style":"Eureka Self","agents":["claude","Explore","general-purpose","Plan"],"skills":["deep-research","design","design-sync","dataviz","update-config","verify","debug","code-review","simplify","batch","fewer-permission-prompts","doctor","loop","claude-api","workflow-authoring","run","run-skill-generator","plugin-authoring"],"plugins":[{"name":"plugin-authoring","path":"builtin","source":"plugin-authoring@builtin"}],"capabilities":["interrupt_receipt_v1","interrupt_cancel_queued_v1","msg_lifecycle_v1"],"analytics_disabled":false,"product_feedback_disabled":false,"uuid":"b8f04cea-6d04-476d-84e7-bd71a903691d","messaging_socket_path":"\\\\.\\pipe\\LOCAL\\cc-msg-08f60b361528ebff88fceca2e74f13fd","fast_mode_state":"off","fast_mode_disabled_reason":"sdk_opt_in_required","powershell_path":"C:\\WINDOWS\\System32\\WindowsPowerShell\\v1.0\\powershell.exe"} +{"type":"assistant","message":{"diagnostics":null,"id":"3af6018f-a66e-4e87-a97a-8a534e41ac02","container":null,"model":"","role":"assistant","stop_details":null,"stop_reason":"stop_sequence","stop_sequence":"","type":"message","usage":{"output_tokens_details":null,"input_tokens":0,"output_tokens":0,"cache_creation_input_tokens":0,"cache_read_input_tokens":0,"server_tool_use":{"web_search_requests":0,"web_fetch_requests":0},"service_tier":null,"cache_creation":{"ephemeral_1h_input_tokens":0,"ephemeral_5m_input_tokens":0},"inference_geo":null,"iterations":null,"speed":null},"content":[{"type":"text","text":"Failed to authenticate: OAuth session expired and could not be refreshed"}],"context_management":null},"parent_tool_use_id":null,"session_id":"7a1c2d3e-4b5a-5c6d-8e7f-0a1b2c3d4e5f","uuid":"39600172-ebb4-4b39-9a7e-160b49591050","timestamp":"2026-10-22T15:23:04.474Z","error":"authentication_failed","is_api_error_message":true} +{"duration_api_ms":0,"stop_reason":"stop_sequence","session_id":"7a1c2d3e-4b5a-5c6d-8e7f-0a1b2c3d4e5f","total_cost_usd":0,"usage":{"output_tokens_details":{"thinking_tokens":0},"input_tokens":0,"cache_creation_input_tokens":0,"cache_read_input_tokens":0,"output_tokens":0,"server_tool_use":{"web_search_requests":0,"web_fetch_requests":0},"service_tier":"standard","cache_creation":{"ephemeral_1h_input_tokens":0,"ephemeral_5m_input_tokens":0},"inference_geo":"","iterations":[],"speed":"standard"},"modelUsage":{},"permission_denials":[],"terminal_reason":"api_error","fast_mode_state":"off","fast_mode_disabled_reason":"sdk_opt_in_required","subagent_stats":{"spawned":0,"requested":{"background":0,"foreground":0,"unset":0},"started_in_background":0,"max_depth":0,"spawned_by_subagents":0,"completed":0,"failed":0,"killed":{"parent":0,"user":0,"system":0},"refused":{"depth_limit":0,"concurrency_limit":0,"budget":0},"by_type":{}},"is_error":true,"num_turns":1,"subtype":"success","api_error_status":null,"result":"Failed to authenticate: OAuth session expired and could not be refreshed","type":"result","duration_ms":222,"uuid":"41e16004-c8b3-4218-af94-69aa2fc52190","queued_turn_count":0,"result_index":0} diff --git a/crates/mind-body/src/self_run/tests.rs b/crates/mind-body/src/self_run/tests.rs new file mode 100644 index 0000000..9c2612d --- /dev/null +++ b/crates/mind-body/src/self_run/tests.rs @@ -0,0 +1,709 @@ +//! The actuator's tests: a real mind over a temporary directory holds her run, +//! a scripted `Child` plays the stream and a settable clock plays time. The +//! stream lines are built from Claude Code 2.1.268's own records (the two +//! fixture files beside this one), varying only ids and usage numbers. The real +//! `ClaudeProcess` is driven through a fake `claude` script on Unix. + +use std::cell::{Cell, RefCell}; +use std::collections::VecDeque; +use std::rc::Rc; + +use chrono::Duration as ChronoDuration; +use eureka_pipeline::{PipelineDocument, PipelineKind as K, PipelineRef, RunOperator, RunTurn, Slug}; +use huginn_mind::{PipelineAdmissionOutcome, PipelineSelectionPage}; + +use super::*; +use crate::testkit::*; + +const RECORDS: &str = include_str!("claude-2.1.268-assistant-usage-records.jsonl"); +const PROBE: &str = include_str!("claude-2.1.268-stream-json-probe.jsonl"); + +// Pinned against Python's uuid.uuid5(uuid.NAMESPACE_OID, ), not against the code under test. +const SESSION_OF_MIND_RUN: &str = "00cb82a4-c0ac-5ad4-83e2-8e685589fb67"; +const SESSION_OF_RUN_A: &str = "d6db8da1-224c-5430-91b5-17aa4df8ae7a"; +const SESSION_OF_RUN_B: &str = "546bec88-f7a9-5fa0-bf07-0fada54f071b"; + +fn foreign_run(id: &str) -> PipelineRef { + PipelineRef { kind: K::Run, id: id.into() } +} + +/// The first real assistant record with its id and usage replaced. +fn request(id: &str, input: u64, read: u64, write: u64) -> String { + let mut event: Value = serde_json::from_str(RECORDS.lines().next().unwrap()).unwrap(); + event["message"]["id"] = json!(id); + event["message"]["usage"]["input_tokens"] = json!(input); + event["message"]["usage"]["cache_read_input_tokens"] = json!(read); + event["message"]["usage"]["cache_creation_input_tokens"] = json!(write); + event.to_string() +} + +/// A request whose whole prefix is `prefix`, under a message id of its own. +fn at(id: &str, prefix: u64) -> String { + request(id, 0, prefix, 0) +} + +fn subagent(id: &str, prefix: u64) -> String { + let mut event: Value = serde_json::from_str(&at(id, prefix)).unwrap(); + event["parent_tool_use_id"] = json!("toolu_0001"); + event.to_string() +} + +fn result_line() -> String { + PROBE.lines().nth(2).unwrap().to_string() +} + +fn init_line() -> String { + PROBE.lines().next().unwrap().to_string() +} + +fn self_run(label: &str, budget: &str) -> PipelineDocument { + let PipelineDocument::Run(mut document) = run(label, RunTurn::SelfRun, RunOperator::Mind, &[]) else { unreachable!() }; + document.budget_usd = budget.into(); + PipelineDocument::Run(document) +} + +#[derive(Clone, Debug, PartialEq, Eq)] +enum Event { + Wrote(String), + InputClosed, + Interrupted, + Asked(StdDuration), + Waited, +} + +#[derive(Clone)] +enum Step { + Line(String), + /// Time passes with no output: the clock moves and the read times out. + Idle(i64), +} + +use Step::{Idle, Line as Out}; + +struct Scripted { + steps: VecDeque, + log: Rc>>, + clock: Rc, + code: Option, + dies_on_interrupt: bool, +} + +impl Child for Scripted { + fn write_line(&mut self, line: &str) { + self.log.borrow_mut().push(Event::Wrote(line.to_string())); + } + + fn close_input(&mut self) { + self.log.borrow_mut().push(Event::InputClosed); + } + + fn next_line(&mut self, timeout: StdDuration) -> Read { + self.log.borrow_mut().push(Event::Asked(timeout)); + match self.steps.pop_front() { + Some(Step::Line(line)) => Read::Line(line), + Some(Step::Idle(seconds)) => { + self.clock.0.set(self.clock.0.get() + ChronoDuration::seconds(seconds)); + Read::Timeout + } + None => Read::Closed, + } + } + + fn interrupt(&mut self) { + self.log.borrow_mut().push(Event::Interrupted); + if self.dies_on_interrupt { + self.steps.clear(); + } + } + + fn wait(&mut self) -> Option { + self.log.borrow_mut().push(Event::Waited); + self.code + } +} + +struct Fake { + child: RefCell>, + spawns: RefCell>>, + refuse: bool, +} + +impl Spawner for Fake { + fn spawn(&self, args: &[String]) -> Result> { + self.spawns.borrow_mut().push(args.to_vec()); + if self.refuse { + return Err(anyhow!("refused")); + } + Ok(Box::new(self.child.borrow_mut().take().expect("spawned twice"))) + } +} + +/// The mind as the actuator sees it, over a `TestMind`: another instance's name, +/// doubled run headers, and a count of admissions it must never receive. +struct Seen<'a> { + inner: &'a TestMind, + instance: Slug, + doubled: bool, + admits: Cell, +} + +impl<'a> Seen<'a> { + fn of(inner: &'a TestMind) -> Self { + Self { inner, instance: inner.instance().clone(), doubled: false, admits: Cell::new(0) } + } +} + +impl MindPort for Seen<'_> { + fn instance(&self) -> &Slug { + &self.instance + } + + fn query(&self, selection: &Selection) -> Result { + let mut page = self.inner.query(selection)?; + if self.doubled + && let PipelinePageItems::Headers(items) = &mut page.items + { + let again = items.clone(); + items.extend(again); + } + Ok(page) + } + + fn admit(&self, _agent: &str, _session: &str, _documents: Vec) -> Result { + self.admits.set(self.admits.get() + 1); + Err(anyhow!("the actuator admits nothing")) + } +} + +struct Ran { + outcome: Result, + out: String, + events: Vec, + spawns: Vec>, + admits: usize, +} + +impl Ran { + fn ended(&self) -> (Cause, Option, u64, u64) { + match self.outcome.as_ref().unwrap() { + SelfRunOutcome::Ended { cause, code, requests, prefix } => (*cause, *code, *requests, *prefix), + other => panic!("not ended: {other:?}"), + } + } + + /// The text of every user line written to the child, in order. + fn written(&self) -> Vec { + self.events + .iter() + .filter_map(|event| match event { + Event::Wrote(line) => { + let line: Value = serde_json::from_str(line).unwrap(); + assert_eq!((line["type"].as_str(), line["message"]["role"].as_str()), (Some("user"), Some("user")), "{line}"); + Some(line["message"]["content"].as_str().unwrap().to_string()) + } + _ => None, + }) + .collect() + } + + fn count(&self, wanted: &Event) -> usize { + self.events.iter().filter(|event| *event == wanted).count() + } + + fn asked(&self) -> Vec { + self.events + .iter() + .filter_map(|event| match event { + Event::Asked(timeout) => Some(timeout.as_secs()), + _ => None, + }) + .collect() + } +} + +fn carry_with(seen: &Seen, steps: Vec, code: Option, dies_on_interrupt: bool) -> Ran { + let log = Rc::new(RefCell::new(Vec::new())); + let clock = Rc::new(Fixed(Cell::new(at_start()))); + let fake = Fake { + child: RefCell::new(Some(Scripted { steps: steps.into(), log: log.clone(), clock: clock.clone(), code, dies_on_interrupt })), + spawns: RefCell::new(Vec::new()), + refuse: false, + }; + let mut out = Vec::new(); + let outcome = carry(seen, &*clock, &fake, &mut out); + let events = log.borrow().clone(); + let spawns = fake.spawns.borrow().clone(); + Ran { outcome, out: String::from_utf8(out).unwrap(), events, spawns, admits: seen.admits.get() } +} + +fn at_start() -> chrono::DateTime { + crate::testkit::at(0) +} + +/// A mind holding her one open Self run `selfrun-a` with budget 5. +fn with_run() -> TestMind { + let mind = TestMind::seeded(); + mind.committed(vec![self_run("selfrun-a", "5")]); + mind +} + +fn go(steps: Vec) -> Ran { + let mind = with_run(); + carry_with(&Seen::of(&mind), steps, Some(0), false) +} + +fn flushes(ran: &Ran) -> usize { + ran.written().iter().filter(|text| text.as_str() == FLUSH_MESSAGE).count() +} + +// ---- who is carried ----------------------------------------------------- + +#[test] +fn without_exactly_one_open_self_run_of_hers_it_is_no_run_and_no_child_is_spawned() { + let cases: Vec<(&str, Box)> = vec![ + ("no run at all", Box::new(|_| {})), + ("a Self run the operator operates", Box::new(|mind| mind.committed(vec![run("op", RunTurn::SelfRun, RunOperator::Operator, &[])]))), + ("her Persona turn", Box::new(|mind| mind.committed(vec![run("persona", RunTurn::PersonaTurn, RunOperator::Mind, &[])]))), + ( + "a Self run of hers that is closed", + Box::new(|mind| { + mind.committed(vec![self_run("selfrun-a", "5")]); + mind.committed(vec![close(run_ref("selfrun-a"), recorded())]); + }), + ), + ]; + for (name, seed) in cases { + let mind = TestMind::seeded(); + seed(&mind); + let ran = carry_with(&Seen::of(&mind), vec![Out(result_line())], Some(0), false); + assert_eq!(ran.outcome.unwrap(), SelfRunOutcome::NoRun, "{name}"); + assert!(ran.spawns.is_empty() && ran.events.is_empty() && ran.out.is_empty(), "{name}"); + } + + // Another instance's mind holds none of this instance's runs. + let mind = with_run(); + let mut other = Seen::of(&mind); + other.instance = Slug("another".into()); + assert_eq!(carry_with(&other, vec![], Some(0), false).outcome.unwrap(), SelfRunOutcome::NoRun); + + // Two open runs are not one: nothing is spawned for either. + let mut two = Seen::of(&mind); + two.doubled = true; + let ran = carry_with(&two, vec![], Some(0), false); + assert_eq!(ran.outcome.unwrap(), SelfRunOutcome::NoRun); + assert!(ran.spawns.is_empty()); +} + +#[test] +fn a_mind_that_cannot_be_read_or_a_child_that_cannot_start_is_an_error_not_an_outcome() { + let mind = with_run(); + let clock = Rc::new(Fixed(Cell::new(at_start()))); + let refusing = Fake { child: RefCell::new(None), spawns: RefCell::new(Vec::new()), refuse: true }; + assert!(carry(&Seen::of(&mind), &*clock, &refusing, &mut Vec::new()).is_err()); + assert_eq!(refusing.spawns.borrow().len(), 1, "it tried exactly once"); + + struct Down; + impl MindPort for Down { + fn instance(&self) -> &Slug { + static INSTANCE: std::sync::OnceLock = std::sync::OnceLock::new(); + INSTANCE.get_or_init(|| Slug("yggdrasil".into())) + } + fn query(&self, _selection: &Selection) -> Result { + Err(anyhow!("down")) + } + fn admit(&self, _agent: &str, _session: &str, _documents: Vec) -> Result { + unreachable!() + } + } + let never = Fake { child: RefCell::new(None), spawns: RefCell::new(Vec::new()), refuse: false }; + assert!(carry(&Down, &*clock, &never, &mut Vec::new()).is_err()); + assert!(never.spawns.borrow().is_empty()); +} + +// ---- what is started ------------------------------------------------------ + +#[test] +fn claude_args_is_exactly_the_fixed_list_for_a_session_and_a_budget() { + let session = Uuid::parse_str(SESSION_OF_RUN_A).unwrap(); + let args = claude_args(session, "12.25"); + assert_eq!( + args, + [ + "-p", + "--input-format", + "stream-json", + "--output-format", + "stream-json", + "--verbose", + "--session-id", + SESSION_OF_RUN_A, + "--max-budget-usd", + "12.25", + "--max-turns", + "500", + "--permission-prompts", + "none", + ] + ); + // Those belong to the run measure or to the unit's managed settings. + for forbidden in ["--resume", "--continue", "--fork-session", "--model", "--permission-mode", "--dangerously-skip-permissions", "--settings", "--system-prompt"] { + assert!(!args.iter().any(|arg| arg.starts_with(forbidden) || arg.contains("dangerously") || arg == "bypassPermissions"), "{forbidden}"); + } + assert_eq!(MAX_TURNS, 500); +} + +#[test] +fn the_session_id_is_uuid_v5_of_the_run_id_and_differs_per_run() { + assert_eq!(session_id(&foreign_run("eureka:run:mind-20261022T120000000Z")).to_string(), SESSION_OF_MIND_RUN); + assert_eq!(session_id(&run_ref("selfrun-a")).to_string(), SESSION_OF_RUN_A); + assert_eq!(session_id(&run_ref("selfrun-b")).to_string(), SESSION_OF_RUN_B); +} + +#[test] +fn the_child_is_started_for_her_run_with_the_runs_own_budget_verbatim() { + for budget in ["0.5", "5", "12.25"] { + let mind = TestMind::seeded(); + mind.committed(vec![self_run("selfrun-b", budget)]); + let ran = carry_with(&Seen::of(&mind), vec![Out(result_line())], Some(0), false); + assert_eq!(ran.spawns.len(), 1, "{budget}"); + assert_eq!(ran.spawns[0], claude_args(Uuid::parse_str(SESSION_OF_RUN_B).unwrap(), budget), "{budget}"); + } +} + +#[test] +fn the_start_message_is_the_first_thing_written_and_names_the_skill_the_instance_and_the_run() { + let ran = go(vec![Out(result_line())]); + assert!(matches!(&ran.events[0], Event::Wrote(_)), "the start precedes the first read: {:?}", ran.events[0]); + let written = ran.written(); + assert_eq!(written[0], start_message(&Slug("yggdrasil".into()), &run_ref("selfrun-a"))); + for needle in ["mind-self", "yggdrasil", "yggdrasil:run:selfrun-a", AGENT] { + assert!(written[0].contains(needle), "{needle}: {}", written[0]); + } + assert_eq!(written.len(), 1, "no flush without the ceiling"); +} + +// ---- what is read ----------------------------------------------------------- + +#[test] +fn read_usage_sums_input_cache_read_and_cache_write_and_reads_absent_fields_as_zero() { + assert_eq!(read_usage(&request("m1", 1, 20, 300)), Usage::Request { id: "m1".into(), prefix: 321 }); + // Each term alone, so dropping or doubling any one is seen. + assert_eq!(read_usage(&request("m", 7, 0, 0)), Usage::Request { id: "m".into(), prefix: 7 }); + assert_eq!(read_usage(&request("m", 0, 7, 0)), Usage::Request { id: "m".into(), prefix: 7 }); + assert_eq!(read_usage(&request("m", 0, 0, 7)), Usage::Request { id: "m".into(), prefix: 7 }); + for absent in ["input_tokens", "cache_read_input_tokens", "cache_creation_input_tokens"] { + let mut event: Value = serde_json::from_str(&request("m", 1, 20, 300)).unwrap(); + event["message"]["usage"].as_object_mut().unwrap().remove(absent); + let want = 321 - [("input_tokens", 1), ("cache_read_input_tokens", 20), ("cache_creation_input_tokens", 300)].iter().find(|(name, _)| *name == absent).unwrap().1; + assert_eq!(read_usage(&event.to_string()), Usage::Request { id: "m".into(), prefix: want }, "{absent} absent"); + } + let mut no_usage: Value = serde_json::from_str(&request("m", 1, 1, 1)).unwrap(); + no_usage["message"].as_object_mut().unwrap().remove("usage"); + assert_eq!(read_usage(&no_usage.to_string()), Usage::Request { id: "m".into(), prefix: 0 }); +} + +#[test] +fn read_usage_classifies_the_owners_real_records() { + let sums = [84210, 100751, 117174, 118336, 126746, 127250, 129853]; + let mut seen = Vec::new(); + for line in RECORDS.lines() { + let Usage::Request { id, prefix } = read_usage(line) else { panic!("an assistant record of the main thread is a request: {line}") }; + if !seen.iter().any(|(known, _)| *known == id) { + seen.push((id, prefix)); + } + } + assert_eq!(seen.iter().map(|(_, prefix)| *prefix).collect::>(), sums); + assert_eq!(read_usage(&result_line()), Usage::Result); + assert_eq!(read_usage(&init_line()), Usage::Other); + assert_eq!(read_usage(&subagent("m", 5)), Usage::Other, "a subagent's request is not the main thread's"); + let mut no_id: Value = serde_json::from_str(&at("m", 5)).unwrap(); + no_id["message"].as_object_mut().unwrap().remove("id"); + assert_eq!(read_usage(&no_id.to_string()), Usage::Other); + let mut null_parent: Value = serde_json::from_str(&at("m", 5)).unwrap(); + null_parent["parent_tool_use_id"] = Value::Null; + assert_eq!(read_usage(&null_parent.to_string()), Usage::Request { id: "m".into(), prefix: 5 }); + let mut no_parent: Value = serde_json::from_str(&at("m", 5)).unwrap(); + no_parent.as_object_mut().unwrap().remove("parent_tool_use_id"); + assert_eq!(read_usage(&no_parent.to_string()), Usage::Request { id: "m".into(), prefix: 5 }); + for unreadable in ["", "not json", "[1,2]", "\"assistant\"", "12", "null", "{\"type\":"] { + assert_eq!(read_usage(unreadable), Usage::Unreadable, "{unreadable:?}"); + } +} + +#[test] +fn records_that_share_a_message_id_count_once_and_subagent_records_never_move_the_prefix() { + let mut steps: Vec = RECORDS.lines().map(|line| Out(line.to_string())).collect(); + steps.insert(3, Out(subagent("msg_sub", 999_999))); + let ran = go(steps); + assert_eq!( + ran.out, + "request 1 prefix 84210\nrequest 2 prefix 100751\nrequest 3 prefix 117174\nrequest 4 prefix 118336\nrequest 5 prefix 126746\nrequest 6 prefix 127250\nrequest 7 prefix 129853\n" + ); + let (cause, _, requests, prefix) = ran.ended(); + assert_eq!((cause, requests, prefix), (Cause::Own, 7, 129_853), "the newest prefix, 129853, not a subagent's 999999"); + assert_eq!(flushes(&ran), 0); +} + +#[test] +fn the_prefix_is_the_newest_requests_not_the_largest() { + let ran = go(vec![Out(at("m1", 200_000)), Out(at("m2", 150_000))]); + assert_eq!(ran.ended().3, 150_000); +} + +// ---- the flush -------------------------------------------------------------- + +#[test] +fn below_the_ceiling_no_flush_is_written_and_at_it_exactly_one() { + let below = go(vec![Out(at("m1", CEILING_TOKENS - 1))]); + assert_eq!(flushes(&below), 0); + assert_eq!(below.ended().0, Cause::Own); + + let at_it = go(vec![Out(at("m1", CEILING_TOKENS - 1)), Out(at("m2", CEILING_TOKENS)), Out(at("m3", CEILING_TOKENS + 10_000))]); + assert_eq!(flushes(&at_it), 1, "{:?}", at_it.written()); + assert_eq!(at_it.written().len(), 2, "the start and the flush"); + assert_eq!(at_it.ended().0, Cause::Flushed); + assert_eq!(CEILING_TOKENS, 300_000); +} + +#[test] +fn a_result_before_the_ceiling_means_no_flush_ever() { + let ran = go(vec![Out(at("m1", 10)), Out(result_line()), Out(at("m2", CEILING_TOKENS + 1))]); + assert_eq!(flushes(&ran), 0); + assert_eq!(ran.ended().0, Cause::Own); + assert_eq!(ran.count(&Event::Interrupted), 0); +} + +#[test] +fn stdin_closes_once_at_the_first_result_flush_or_no_flush_and_nothing_is_written_after() { + let plain = go(vec![Out(at("m1", 10)), Out(result_line()), Out(result_line())]); + assert_eq!(plain.count(&Event::InputClosed), 1); + let closed_at = plain.events.iter().position(|event| *event == Event::InputClosed).unwrap(); + assert!(plain.events[closed_at..].iter().all(|event| !matches!(event, Event::Wrote(_))), "no input after the close"); + assert!(plain.events[..closed_at].iter().any(|event| matches!(event, Event::Wrote(_))), "the start went before"); + + let flushed = go(vec![Out(at("m1", CEILING_TOKENS)), Out(result_line())]); + assert_eq!(flushed.count(&Event::InputClosed), 1); + let closed_at = flushed.events.iter().position(|event| *event == Event::InputClosed).unwrap(); + assert_eq!(flushed.events[..closed_at].iter().filter(|event| matches!(event, Event::Wrote(_))).count(), 2, "start and flush before the close"); + assert!(flushed.events[closed_at..].iter().all(|event| !matches!(event, Event::Wrote(_)))); + + let never = go(vec![Out(at("m1", 10))]); + assert_eq!(never.count(&Event::InputClosed), 0, "no result, no close"); +} + +// ---- the interrupt ------------------------------------------------------------ + +#[test] +fn after_the_flush_the_grace_interrupts_exactly_once_and_not_a_second_early() { + let grace = FLUSH_GRACE_S; + // Just short of the grace: no interrupt, and the child ended on its own. + let early = go(vec![Out(at("m1", CEILING_TOKENS)), Idle(grace - 1)]); + assert_eq!(early.count(&Event::Interrupted), 0); + assert_eq!(early.ended().0, Cause::Flushed); + + // At the grace, and long past it: one interrupt. + let on_time = carry_with(&Seen::of(&with_run()), vec![Out(at("m1", CEILING_TOKENS)), Idle(grace), Idle(grace), Idle(grace)], Some(130), true); + assert_eq!(on_time.count(&Event::Interrupted), 1); + assert_eq!(on_time.ended().0, Cause::Interrupted); + + // A child that ignores it is interrupted once and still waited for. + let stubborn = carry_with(&Seen::of(&with_run()), vec![Out(at("m1", CEILING_TOKENS)), Idle(grace), Idle(grace), Idle(grace)], Some(0), false); + assert_eq!(stubborn.count(&Event::Interrupted), 1); + assert_eq!(stubborn.count(&Event::Waited), 1); + assert_eq!(FLUSH_GRACE_S, 900); +} + +#[test] +fn the_wait_for_the_child_never_passes_the_grace_deadline_and_never_spins_after_the_interrupt() { + let ran = go(vec![Out(at("m1", CEILING_TOKENS)), Idle(840), Idle(59), Idle(1)]); + assert_eq!(ran.asked(), [60, 60, 60, 1, 60], "{:?}", ran.asked()); +} + +#[test] +fn a_prefix_at_the_ceiling_plus_the_allowance_interrupts_at_once_and_one_short_of_it_does_not() { + let hard = CEILING_TOKENS + FLUSH_ALLOWANCE_TOKENS; + let short = go(vec![Out(at("m1", CEILING_TOKENS)), Out(at("m2", hard - 1))]); + assert_eq!(short.count(&Event::Interrupted), 0); + assert_eq!(short.ended().0, Cause::Flushed); + + let at_hard = carry_with(&Seen::of(&with_run()), vec![Out(at("m1", CEILING_TOKENS)), Out(at("m2", hard)), Out(at("m3", hard + 1))], Some(130), true); + assert_eq!(at_hard.count(&Event::Interrupted), 1); + assert_eq!(at_hard.ended().0, Cause::Interrupted); + assert_eq!(FLUSH_ALLOWANCE_TOKENS, 60_000); + + // The first request already past both: the flush is written, then the interrupt. + let jump = carry_with(&Seen::of(&with_run()), vec![Out(at("m1", hard + 5))], Some(130), true); + assert_eq!((flushes(&jump), jump.count(&Event::Interrupted)), (1, 1)); +} + +#[test] +fn a_child_that_ends_before_the_grace_is_never_interrupted() { + let ran = go(vec![Out(at("m1", CEILING_TOKENS)), Out(result_line())]); + assert_eq!(ran.count(&Event::Interrupted), 0); + assert_eq!(ran.count(&Event::Waited), 1); +} + +// ---- how it ends -------------------------------------------------------------- + +#[test] +fn the_outcome_names_how_it_ended_and_the_exit_status_follows_the_childs_code() { + let own = carry_with(&Seen::of(&with_run()), vec![Out(at("m1", 10)), Out(result_line())], Some(0), false); + assert_eq!(own.outcome.as_ref().unwrap(), &SelfRunOutcome::Ended { cause: Cause::Own, code: Some(0), requests: 1, prefix: 10 }); + assert_eq!(own.outcome.as_ref().unwrap().to_string(), "ended: own code 0 requests 1 prefix 10"); + assert_eq!(own.outcome.unwrap().exit_status(), 0); + + let flushed = carry_with(&Seen::of(&with_run()), vec![Out(at("m1", CEILING_TOKENS)), Out(result_line())], Some(0), false); + assert_eq!(flushed.outcome.as_ref().unwrap().to_string(), "ended: flushed code 0 requests 1 prefix 300000"); + assert_eq!(flushed.outcome.unwrap().exit_status(), 0); + + let nonzero = carry_with(&Seen::of(&with_run()), vec![Out(at("m1", 10))], Some(2), false); + assert_eq!(nonzero.outcome.as_ref().unwrap().to_string(), "ended: own code 2 requests 1 prefix 10"); + assert_eq!(nonzero.outcome.unwrap().exit_status(), 1); + + let signalled = carry_with(&Seen::of(&with_run()), vec![Out(at("m1", CEILING_TOKENS + FLUSH_ALLOWANCE_TOKENS))], None, true); + assert_eq!(signalled.outcome.as_ref().unwrap().to_string(), "ended: interrupted signal requests 1 prefix 360000"); + assert_eq!(signalled.outcome.unwrap().exit_status(), 1, "a child killed by a signal has no code"); + + assert_eq!(SelfRunOutcome::NoRun.exit_status(), 3); + assert_eq!(SelfRunOutcome::NoRun.to_string(), "no run: she has no single open Self run"); +} + +#[test] +fn unreadable_and_foreign_lines_are_skipped_and_never_printed() { + let canary = "canary-stream-line-8c41"; + let mut foreign: Value = serde_json::from_str(&init_line()).unwrap(); + foreign["cwd"] = json!(canary); + foreign["type"] = json!(canary); + let ran = go(vec![ + Out(format!("not json {canary}")), + Out(foreign.to_string()), + Out(format!("[\"{canary}\"]")), + Out(at("m1", 10)), + Out(result_line()), + ]); + assert_eq!(ran.out, "request 1 prefix 10\n"); + let outcome = ran.outcome.as_ref().unwrap().to_string(); + assert!(!ran.out.contains(canary) && !outcome.contains(canary)); + assert!(!ran.written().iter().any(|text| text.contains(canary)), "nothing from the stream is written back"); +} + +#[test] +fn the_actuator_admits_nothing_to_the_mind_through_a_whole_flushed_run() { + let mind = with_run(); + let seen = Seen::of(&mind); + let ran = carry_with(&seen, vec![Out(at("m1", CEILING_TOKENS)), Out(result_line())], Some(0), false); + assert_eq!(ran.admits, 0); + assert!(ran.outcome.is_ok()); + // And the run is exactly as she left it: still in force until her flush, or the next launch, closes it. + assert_eq!(mind.ids(K::Resolution), Vec::::new()); +} + +// ---- the real child ----------------------------------------------------------- + +#[cfg(unix)] +mod process { + use std::os::unix::fs::PermissionsExt; + use std::path::PathBuf; + + use super::*; + use crate::launch::SystemClock; + + /// A fake `claude`: the shell script `body`, in a directory that outlives the child. + fn script(dir: &tempfile::TempDir, body: &str) -> PathBuf { + let path = dir.path().join("claude"); + std::fs::write(&path, format!("#!/bin/sh\n{body}\n")).unwrap(); + std::fs::set_permissions(&path, std::fs::Permissions::from_mode(0o755)).unwrap(); + path + } + + /// Spawns it, retrying the one error a freshly written executable can give + /// while another test's fork still holds its write handle. + fn start(program: &Path, args: &[String]) -> Box { + for _ in 0..50 { + match spawn_program(program, args) { + Ok(child) => return child, + Err(error) if format!("{error:#}").contains("busy") => std::thread::sleep(StdDuration::from_millis(100)), + Err(error) => panic!("{error:#}"), + } + } + panic!("the script stayed busy"); + } + + fn drain(child: &mut dyn Child) -> Vec { + let mut lines = Vec::new(); + loop { + match child.next_line(StdDuration::from_secs(20)) { + Read::Line(line) => lines.push(line), + Read::Closed => return lines, + Read::Timeout => panic!("the fake claude hung"), + } + } + } + + #[test] + fn the_child_gets_its_arguments_and_lines_in_order_and_ends_when_its_input_closes() { + let dir = tempfile::tempdir().unwrap(); + let program = script(&dir, "echo \"args: $*\"\nwhile read line; do echo \"got: $line\"; done\necho bye\nexit 7"); + let mut child = start(&program, &["-p".to_string(), "--max-turns".to_string(), "500".to_string()]); + child.write_line("first"); + child.write_line("second"); + child.close_input(); + assert_eq!(drain(&mut *child), ["args: -p --max-turns 500", "got: first", "got: second", "bye"]); + assert_eq!(child.wait(), Some(7)); + } + + #[test] + fn a_silent_child_times_out_instead_of_blocking_and_a_signalled_child_has_no_code() { + let dir = tempfile::tempdir().unwrap(); + let program = script(&dir, "kill -9 $$"); + let mut child = start(&program, &[]); + assert_eq!(child.next_line(StdDuration::from_secs(20)), Read::Closed); + assert_eq!(child.wait(), None); + + let quiet = script(&dir, "exec sleep 5"); + let mut child = start(&quiet, &[]); + assert_eq!(child.next_line(StdDuration::from_millis(100)), Read::Timeout); + child.interrupt(); + assert_eq!(child.next_line(StdDuration::from_secs(20)), Read::Closed); + assert_eq!(child.wait(), None, "SIGINT ended the unprepared child"); + } + + #[test] + fn interrupt_is_sigint_the_childs_own_handler_can_catch() { + let dir = tempfile::tempdir().unwrap(); + let program = script(&dir, "trap 'echo interrupted; exit 130' INT\necho ready\nwhile :; do sleep 0.05; done"); + let mut child = start(&program, &[]); + assert_eq!(child.next_line(StdDuration::from_secs(20)), Read::Line("ready".into())); + child.interrupt(); + assert_eq!(drain(&mut *child), ["interrupted"]); + assert_eq!(child.wait(), Some(130)); + } + + #[test] + fn a_program_that_cannot_start_is_an_error_that_names_no_argument() { + let canary = "canary-argument-6b0d"; + let error = spawn_program(Path::new("/nonexistent/claude"), &[canary.to_string()]).err().unwrap(); + assert!(!format!("{error:#}").contains(canary) && !format!("{error:#}").contains("nonexistent"), "{error:#}"); + } + + #[test] + fn a_whole_run_through_the_real_child_flushes_at_the_ceiling_and_ends_on_the_result() { + let dir = tempfile::tempdir().unwrap(); + let lines = [at("m1", 10), at("m2", CEILING_TOKENS)].join("\n"); + let result = result_line(); + // Prints two requests, waits for the flush on stdin, then the result, as a stand-in for a Self that obeys. + let body = format!("cat <<'EOF'\n{lines}\nEOF\nread start\nread flush\ncat <<'EOF'\n{result}\nEOF\nwhile read more; do :; done\nexit 0"); + let program = script(&dir, &body); + let mind = with_run(); + struct Spawn(PathBuf); + impl Spawner for Spawn { + fn spawn(&self, args: &[String]) -> Result> { + Ok(start(&self.0, args)) + } + } + let mut out = Vec::new(); + let outcome = carry(&Seen::of(&mind), &SystemClock, &Spawn(program), &mut out).unwrap(); + assert_eq!(outcome, SelfRunOutcome::Ended { cause: Cause::Flushed, code: Some(0), requests: 2, prefix: 300_000 }); + assert_eq!(String::from_utf8(out).unwrap(), "request 1 prefix 10\nrequest 2 prefix 300000\n"); + } +} diff --git a/crates/mind-body/tests/self_run.rs b/crates/mind-body/tests/self_run.rs new file mode 100644 index 0000000..6e5a84d --- /dev/null +++ b/crates/mind-body/tests/self_run.rs @@ -0,0 +1,239 @@ +//! The `mind-self-run` binary as a process: its arguments, its exit codes and +//! what it never echoes. The carrying itself is tested in the library against a +//! real mind and a scripted child. One test, `#[ignore]`d outside a container, +//! puts the real binary, the real daemon and a fake `claude` first on `PATH` +//! together, which is where the real child's argument list and stream can be +//! seen end to end. + +// The test hands the binary its environment, and reads the one variable the +// harness gives it. That is test plumbing: it reaches no library code. +#![allow(clippy::disallowed_methods, reason = "the suite sets the binary's environment")] + +use std::process::{Command, Output}; + +const NO_SUCH_INSTANCE: &str = "self-run-test-no-such-instance"; + +fn self_run(args: &[&str]) -> Output { + Command::new(env!("CARGO_BIN_EXE_mind-self-run")).args(args).env_remove("HUGINN_ENDPOINT").output().unwrap() +} + +fn self_run_with_env_endpoint(args: &[&str], endpoint: &str) -> Output { + Command::new(env!("CARGO_BIN_EXE_mind-self-run")).args(args).env("HUGINN_ENDPOINT", endpoint).output().unwrap() +} + +fn all_output(output: &Output) -> String { + format!("{}{}", String::from_utf8_lossy(&output.stdout), String::from_utf8_lossy(&output.stderr)) +} + +#[test] +fn without_an_instance_or_with_a_stray_argument_it_exits_2_with_the_usage_and_echoes_nothing() { + let canary = "canary-argument-3f9a"; + for args in [ + vec![], + vec!["--endpoint", "rudp://127.0.0.1:9"], + vec!["--instance", NO_SUCH_INSTANCE, canary], + vec!["--instance", NO_SUCH_INSTANCE, "--instance", canary], + vec![canary], + vec!["--resume", canary], + ] { + let output = self_run(&args); + assert_eq!(output.status.code(), Some(2), "{args:?}"); + let stderr = String::from_utf8_lossy(&output.stderr); + assert!(stderr.contains("usage: mind-self-run"), "{args:?}: {stderr}"); + assert!(!all_output(&output).contains(canary) && output.stdout.is_empty(), "{args:?}"); + } +} + +#[test] +fn an_instance_that_is_not_a_slug_is_exit_2_and_is_not_echoed() { + let canary = "Canary/../Instance-5e2b"; + let output = self_run(&["--instance", canary, "--endpoint", "rudp://127.0.0.1:9"]); + assert_eq!(output.status.code(), Some(2)); + assert!(!all_output(&output).contains("anary"), "{}", all_output(&output)); + assert!(output.stdout.is_empty()); +} + +#[test] +fn a_malformed_endpoint_or_none_is_exit_2_and_the_endpoint_is_not_echoed() { + let canary = "canary-endpoint-77c1"; + let bad = self_run(&["--instance", NO_SUCH_INSTANCE, "--endpoint", canary]); + assert_eq!(bad.status.code(), Some(2)); + assert!(!all_output(&bad).contains(canary) && bad.stdout.is_empty()); + let none = self_run(&["--instance", NO_SUCH_INSTANCE]); + assert_eq!(none.status.code(), Some(2)); + assert!(none.stdout.is_empty()); + + let from_env = self_run_with_env_endpoint(&["--instance", NO_SUCH_INSTANCE], "canary-env-endpoint-4d02"); + assert_eq!(from_env.status.code(), Some(2)); + assert!(!all_output(&from_env).contains("canary-env-endpoint-4d02") && from_env.stdout.is_empty()); +} + +#[test] +fn a_mind_that_cannot_be_reached_is_exit_2_before_any_child_and_names_no_value() { + // Port 9 on loopback answers no one; PATH is empty, so a child could not start either. + let output = Command::new(env!("CARGO_BIN_EXE_mind-self-run")) + .args(["--instance", NO_SUCH_INSTANCE, "--endpoint", "rudp://127.0.0.1:9"]) + .env("PATH", "") + .output() + .unwrap(); + assert_eq!(output.status.code(), Some(2), "{}", all_output(&output)); + assert!(output.stdout.is_empty()); + assert!(!all_output(&output).contains("127.0.0.1"), "{}", all_output(&output)); +} + +#[cfg(unix)] +mod lane { + use std::net::UdpSocket; + use std::path::Path; + use std::process::{Child, Command, Stdio}; + use std::time::Duration; + + use chrono::Utc; + use eureka_pipeline::{ + Date, DocRef, Label, PipelineCampaign, PipelineDocument, PipelineInstance, PipelineRun, PipelineStewardship, PipelineTarget, RunOperator, RunTurn, Sha, + Slug, TargetInvariant, + }; + use huginn_mind::{Faculty, Mind, PipelineAdmissionBatch, PipelineAdmissionOutcome, PipelineProvenance}; + use mind_body::self_run::FLUSH_MESSAGE; + use tempfile::TempDir; + + const INSTANCE: &str = "yggdrasil"; + const CAMPAIGN: &str = "eureka-body"; + const REPO: &str = "GameCult/Huginn"; + // Pinned against Python's uuid.uuid5(uuid.NAMESPACE_OID, "yggdrasil:run:selfrun-lane"). + const LANE_SESSION: &str = "6bbfe181-08ee-5c29-8e32-0ebe48ec4b33"; + + fn doc_ref() -> DocRef { + DocRef { path: "docs/eureka-body-target.md".into(), start_line: 1, end_line: 9, commit: Sha("df7507b".into()) } + } + + /// The identity, stewardship, campaign and target a run cites, and her one + /// open Self run with budget 7.25. + fn seed() -> Vec { + let date = || Date("2026-10-22".into()); + vec![ + PipelineDocument::Instance(PipelineInstance { instance: Slug(INSTANCE.into()), display_name: "test mind".into(), created_at: date(), host: "test".into() }), + PipelineDocument::Stewardship(PipelineStewardship { instance: Slug(INSTANCE.into()), repo: REPO.into(), sequence: 1, assigned_on: date(), note: "assigned".into() }), + PipelineDocument::Campaign(PipelineCampaign { + slug: Slug(CAMPAIGN.into()), + title: "Eureka body".into(), + repos: vec![REPO.into()], + working_branch: "main".into(), + target_doc: doc_ref(), + }), + PipelineDocument::Target(PipelineTarget { + campaign: Slug(CAMPAIGN.into()), + revision: 1, + invariants: vec![TargetInvariant { label: "run-is-the-grant".into(), statement: "A run is the grant.".into() }], + not_in_scope: vec![], + canonical_implementations: vec![], + doc: doc_ref(), + }), + PipelineDocument::Run(PipelineRun { + instance: Slug(INSTANCE.into()), + label: Label("selfrun-lane".into()), + operated_by: RunOperator::Mind, + turn: RunTurn::SelfRun, + host: "test".into(), + started_on: date(), + budget_usd: "7.25".into(), + claims: vec![], + campaigns: vec![Slug(CAMPAIGN.into())], + }), + ] + } + + /// The daemon, ended with the test whether it passes or not. + struct Daemon(Child); + + impl Drop for Daemon { + fn drop(&mut self) { + let _ = self.0.kill(); + let _ = self.0.wait(); + } + } + + /// The real binary against the real daemon, with a fake `claude` first on `PATH`. + /// The fake prints two requests (the second reaches the ceiling), waits for the start + /// and the flush on stdin, records its argument list and what it read, prints a + /// result and exits 0. It needs a built `huginn-daemon` beside the binary + /// (`cargo build -p huginn-daemon`), so it runs in the container lane only; `cargo test` and + /// `cargo mutants` skip it. Run it with the README's lane command. + #[test] + #[ignore = "container only: a built huginn-daemon beside the binary, and a POSIX shell"] + fn the_real_bin_finds_her_run_starts_claude_for_it_asks_for_the_flush_and_exits_0() { + let slug = Slug(INSTANCE.into()); + let state = TempDir::new().unwrap(); + { + let mut mind = Mind::open(state.path(), &slug).unwrap(); + let batch = PipelineAdmissionBatch { + instance: slug.clone(), + provenance: PipelineProvenance { faculty: Faculty::Hands, agent: "test".into(), session: "test".into(), tool: "test".into() }, + documents: seed(), + }; + assert!(matches!(mind.admit(batch, Utc::now()), PipelineAdmissionOutcome::Committed { .. })); + } + + let bin = TempDir::new().unwrap(); + let record = bin.path().join("record"); + let request = |id: &str, tokens: u64| { + format!( + r#"{{"type":"assistant","parent_tool_use_id":null,"message":{{"id":"{id}","usage":{{"input_tokens":1,"cache_read_input_tokens":{tokens},"cache_creation_input_tokens":0}}}}}}"# + ) + }; + let script = format!( + "#!/bin/sh\necho \"$@\" > {record}\necho '{first}'\necho '{second}'\nread start\nread flush\necho \"$start\" >> {record}\necho \"$flush\" >> {record}\necho '{{\"type\":\"result\"}}'\nwhile read more; do :; done\nexit 0\n", + record = record.display(), + first = request("msg_1", 100), + second = request("msg_2", 299_999), + ); + let claude = bin.path().join("claude"); + { + use std::os::unix::fs::PermissionsExt; + std::fs::write(&claude, script).unwrap(); + std::fs::set_permissions(&claude, std::fs::Permissions::from_mode(0o755)).unwrap(); + } + + let port = UdpSocket::bind("127.0.0.1:0").unwrap().local_addr().unwrap().port(); + let endpoint = format!("rudp://127.0.0.1:{port}"); + let exe = Path::new(env!("CARGO_BIN_EXE_mind-self-run")); + let daemon_exe = exe.with_file_name("huginn-daemon"); + assert!(daemon_exe.exists(), "build huginn-daemon first: {}", daemon_exe.display()); + let daemon = Daemon( + Command::new(&daemon_exe) + .args(["--state-root", state.path().to_str().unwrap(), "--instance", INSTANCE, "--bind", &format!("127.0.0.1:{port}")]) + .args(["--qdrant-url", "http://127.0.0.1:1", "--ollama-url", "http://127.0.0.1:1", "--embedding-model", "x"]) + .stdout(Stdio::null()) + .stderr(Stdio::null()) + .spawn() + .unwrap(), + ); + + let path = format!("{}:/usr/bin:/bin", bin.path().display()); + let mut output = None; + for _ in 0..40 { + let attempt = Command::new(exe).args(["--instance", INSTANCE, "--endpoint", &endpoint]).env("PATH", &path).output().unwrap(); + // Exit 2 before the daemon answers has started nothing; try again until it does. + if attempt.status.code() != Some(2) { + output = Some(attempt); + break; + } + std::thread::sleep(Duration::from_millis(500)); + } + let output = output.expect("the daemon never answered"); + drop(daemon); + + let stdout = String::from_utf8_lossy(&output.stdout).to_string(); + assert_eq!(output.status.code(), Some(0), "{stdout}\n{}", String::from_utf8_lossy(&output.stderr)); + assert_eq!(stdout, "request 1 prefix 101\nrequest 2 prefix 299999\nended: own code 0 requests 2 prefix 299999\n"); + + let recorded = std::fs::read_to_string(&record).unwrap(); + let mut lines = recorded.lines(); + let argv = lines.next().unwrap(); + assert!(argv.contains(&format!("--session-id {LANE_SESSION}")) && argv.contains("--max-budget-usd 7.25"), "{argv}"); + assert!(argv.starts_with("-p --input-format stream-json"), "{argv}"); + assert!(lines.next().unwrap().contains("yggdrasil:run:selfrun-lane"), "the start message names her run"); + let flush = lines.next().unwrap(); + assert!(flush.contains(FLUSH_MESSAGE) && flush.contains("\"type\":\"user\""), "the flush went to the child's stdin: {flush}"); + } +}