use std::collections::{HashMap, HashSet};
use std::io::{BufRead, Seek, SeekFrom};
use std::path::{Path, PathBuf};
use std::sync::{Arc, Mutex};
use std::time::{Duration, Instant};
use anyhow::{Context, Result};
use serde_json::{Value, json};
use tokio::sync::{mpsc, oneshot};
use uuid::Uuid;
use super::rpc::{Incoming, Rpc, RpcError};
use super::{AgentEvent, Decision, Events, Inputs, Proc, StartSpec, Steer};
use crate::{identity, ipc::Tier, paths};
pub(super) fn initialize() -> Value {
json!({"protocolVersion":1,"clientCapabilities":{},
"clientInfo":{"name":"axiopera","version":env!("CARGO_PKG_VERSION")}})
}
pub(super) fn option<'a>(reply: &'a Value, id: &str) -> Option<&'a Value> {
reply["configOptions"]
.as_array()?
.iter()
.find(|o| o["id"] == id)
}
fn settings(reply: &Value, events: &Events) {
if option(reply, "model").is_some() {
let current = |id| option(reply, id).and_then(|v| v["currentValue"].as_str());
super::report_settings(events, current("model"), current("thought_level"));
}
}
fn data_dir(env: &[(String, String)]) -> PathBuf {
let home = if cfg!(windows) { "USERPROFILE" } else { "HOME" };
env.iter()
.find(|(k, _)| k.eq_ignore_ascii_case(home))
.map(|(_, v)| PathBuf::from(v))
.unwrap_or_else(super::home_dir)
.join(".gemini/antigravity-cli")
}
pub(super) fn saved_model(env: &[(String, String)]) -> Option<String> {
#[derive(serde::Deserialize)]
struct Preference {
model: String,
}
let file = std::fs::File::open(data_dir(env).join("settings.json")).ok()?;
let pref: Preference = serde_json::from_reader(std::io::BufReader::new(file)).ok()?;
Some(pref.model)
}
pub(super) fn preferred_variant<'a>(
session: &'a Value,
label: &str,
requested: Option<&str>,
) -> Option<&'a str> {
let mut matches = session["models"]["availableModels"]
.as_array()?
.iter()
.filter(|row| row["name"] == label || row["modelId"] == label);
let variant = matches.next()?;
if matches.next().is_some() {
return None;
}
if let Some(model) = requested {
let family = option(session, "model")?["options"]
.as_array()?
.iter()
.find(|entry| entry["value"] == model)?["name"]
.as_str()?;
let name = variant["name"].as_str()?;
if name != family
&& name
.rsplit_once(" (")
.filter(|(_, end)| end.ends_with(')'))
.map(|(base, _)| base)
!= Some(family)
{
return None;
}
}
variant["modelId"].as_str()
}
struct PendingSteer {
request: u64,
input: Option<u64>,
admitted: oneshot::Sender<Steer>,
}
#[derive(Default)]
struct State {
prompt: Option<u64>,
steering: Option<PendingSteer>,
inputs: Inputs,
text: String,
tools: HashSet<String>,
spawns: HashMap<String, Option<Instant>>,
children: HashSet<String>,
waking: bool,
}
impl State {
fn replied(&mut self, id: u64, reply: Result<Value, RpcError>, events: &Events) {
if self.prompt != Some(id) && !self.steering.as_ref().is_some_and(|s| s.request == id) {
return;
}
if let Some(steer) = self.steering.take() {
if id == steer.request && reply.is_err() {
if steer.input.is_some() {
self.inputs.0.retain(|(id, _)| *id != steer.input);
}
let _ = steer.admitted.send(Steer::NoActiveTurn);
return;
}
self.prompt = Some(steer.request);
let _ = steer.admitted.send(Steer::Accepted);
if id != steer.request {
return;
}
}
self.prompt = None;
let (error, usage) = match reply {
Ok(r) => {
let stop = r["stopReason"].as_str().unwrap_or("unknown");
(
(stop != "end_turn").then(|| stop.to_owned()),
r.get("usage").cloned(),
)
}
Err(e) => (Some(e.to_string()), None),
};
let _ = events.send(AgentEvent::TurnEnded {
ok: error.is_none(),
result: std::mem::take(&mut self.text),
error,
usage,
});
}
}
pub struct Agy {
pub proc: Proc,
rpc: Rpc,
session: String,
state: Arc<Mutex<State>>,
pump: tokio::task::JoinHandle<()>,
}
impl Drop for Agy {
fn drop(&mut self) {
self.pump.abort();
}
}
impl Agy {
pub async fn start(spec: &StartSpec, exe: &Path, events: Events) -> Result<Self> {
let home = spec
.env
.iter()
.find(|(k, _)| k == paths::HOME_VAR)
.map(|(_, v)| PathBuf::from(v))
.context("missing Axiopera home")?;
let mut args = vec!["acp".into()];
match spec.tier {
Tier::Read => {
let profile = home.join("tasks").join(&spec.task_id).join("agy-profile");
let name = format!("axiopera-reader-{}", spec.task_id);
let dir = profile.join(".agents/agents");
std::fs::create_dir_all(&dir)?;
std::fs::write(
dir.join(format!("{name}.md")),
format!(
"---\nname: {name}\ndescription: Read-only Axiopera task\ntools: [read_file]\ninheritMcp: false\nmainAgent: true\n---\n"
),
)?;
args.extend([
"--add-dir".into(),
spec.cwd.display().to_string(),
"--add-dir".into(),
profile.display().to_string(),
"--agent".into(),
name,
]);
}
Tier::Work => args.push("--sandbox".into()),
Tier::Full => args.push("--dangerously-skip-permissions".into()),
}
let (proc, stdout) = Proc::spawn(exe, &args, spec)?;
let (rpc, incoming) = Rpc::start(stdout, proc.stdin.clone());
rpc.call("initialize", initialize()).await?;
let mut mcp_args = vec![
"mcp".to_string(),
"--caller".into(),
format!("task:{}", spec.task_id),
];
if spec.tier == Tier::Read {
mcp_args.extend(["--list-root".into(), spec.cwd.display().to_string()]);
}
let server = json!({
"name": "axiopera",
"command": std::env::current_exe()?,
"args": mcp_args,
"env": [
{"name": identity::TASK_VAR, "value": spec.task_id},
{"name": paths::HOME_VAR, "value": home},
],
});
let instructions = format!(
"{}\nYour working directory is {}. Resolve relative task paths against it.",
super::protocol_text(spec),
spec.cwd.display()
);
let mut params = json!({
"cwd": spec.cwd,
"mcpServers": [server],
"_meta": {"antigravity": {"systemPrompt": {"append": instructions}}},
});
let session = match &spec.resume {
Some(id) => id.clone(),
None => rpc.call("session/new", params.clone()).await?["sessionId"]
.as_str()
.context("AGY returned no session ID")?
.to_owned(),
};
Uuid::parse_str(&session).context("invalid AGY session ID")?;
params["sessionId"] = json!(session);
let mut reply = rpc.call("session/resume", params).await?;
let mut model = spec.model.as_deref();
if spec.resume.is_none()
&& (model.is_none() || spec.effort.is_none())
&& let Some(label) = saved_model(&spec.env)
&& let Some(variant) = preferred_variant(&reply, &label, model)
{
let choice = json!({"sessionId":session,"modelId":variant});
if rpc.call("session/set_model", choice).await.is_ok() {
model = None;
}
} else if spec.resume.is_some()
&& option(&reply, "model").and_then(|v| v["currentValue"].as_str()) == model
{
model = None;
}
let mode = if spec.tier == Tier::Work {
"auto"
} else {
"default"
};
for (key, value) in [
("model", model),
("thought_level", spec.effort.as_deref()),
("mode", Some(mode)),
] {
if let Some(value) = value {
reply = rpc
.call(
"session/set_config_option",
json!({"sessionId":session,"configId":key,"value":value}),
)
.await
.with_context(|| format!("setting AGY {key} to {value}"))?;
}
}
settings(&reply, &events);
let brain = data_dir(&spec.env).join("brain");
let path = transcript(&brain, &session);
let cursor = std::fs::metadata(&path).map_or(0, |f| f.len());
let _ = events.send(AgentEvent::Session {
id: session.clone(),
file: Some(path),
});
let state = Arc::new(Mutex::new(State::default()));
let driver = Driver {
rpc: rpc.clone(),
root: session.clone(),
brain,
cwd: spec.cwd.clone(),
tier: spec.tier,
events,
state: state.clone(),
journals: HashMap::from([(
session.clone(),
Journal {
offset: cursor,
..Default::default()
},
)]),
candidates: HashMap::new(),
approvals: HashMap::new(),
parent_woke: false,
};
let pump = tokio::spawn(driver.run(incoming));
Ok(Self {
proc,
rpc,
session,
state,
pump,
})
}
pub async fn turn(&mut self, text: &str) -> Result<()> {
let request = self.rpc.reserve();
{
let mut state = self.state.lock().unwrap();
state.text.clear();
state.prompt = Some(request);
}
self.send_prompt(request, text).await
}
async fn send_prompt(&self, request: u64, text: &str) -> Result<()> {
Ok(self
.rpc
.request_in_order_as(
request,
"session/prompt",
json!({"sessionId":self.session,"prompt":[{"type":"text","text":text}]}),
)
.await?)
}
pub async fn steer(&mut self, text: &str, input: Option<u64>) -> Result<Steer> {
let request = self.rpc.reserve();
let (admitted, receipt) = oneshot::channel();
{
let mut state = self.state.lock().unwrap();
if state.prompt.is_none() {
return Ok(Steer::NoActiveTurn);
}
if input.is_some() {
state.inputs.push(input, text);
}
state.steering = Some(PendingSteer {
request,
input,
admitted,
});
}
self.send_prompt(request, text).await?;
receipt
.await
.context("AGY exited before admitting the message")
}
pub fn in_turn(&self) -> bool {
self.state.lock().unwrap().prompt.is_some()
}
pub fn busy(&self) -> bool {
let s = self.state.lock().unwrap();
s.prompt.is_some()
|| !s.tools.is_empty()
|| !s.spawns.is_empty()
|| !s.children.is_empty()
|| s.waking
}
pub async fn interrupt(&mut self) -> Result<()> {
self.rpc
.notify("session/cancel", json!({"sessionId":self.session}))
.await?;
Ok(())
}
}
fn transcript(brain: &Path, id: &str) -> PathBuf {
brain
.join(id)
.join(".system_generated/logs/transcript.jsonl")
}
#[derive(Default)]
struct Journal {
offset: u64,
user_inputs: Vec<String>,
spawn_slots: HashSet<u64>,
unmatched_children: usize,
delivered_after_pending: HashSet<u64>,
version: Option<(std::time::SystemTime, u64)>,
held: Vec<Value>,
}
impl Journal {
fn ready(&mut self, rows: Vec<Value>, waits: impl Fn(&Value) -> bool) -> Vec<Value> {
self.held.extend(rows);
let end = self.held.iter().position(waits).unwrap_or(self.held.len());
self.held.drain(..end).collect()
}
fn match_spawns(&mut self, parent: &str, spawns: &mut HashMap<String, Option<Instant>>) {
while self.unmatched_children > 0 {
let completed = spawns
.iter()
.filter_map(|(key, time)| {
let (owner, _) = key.split_once(':')?;
time.filter(|_| owner == parent).map(|time| (key, time))
})
.min_by_key(|(_, time)| *time);
let Some((key, _)) = completed else { break };
let key = key.clone();
spawns.remove(&key);
self.unmatched_children -= 1;
}
}
fn find_children(
&mut self,
parent: &str,
rows: &[Value],
candidates: &mut HashMap<String, String>,
) {
for row in rows {
let index = row["step_index"].as_u64().unwrap();
if row["type"] == "PLANNER_RESPONSE"
&& let Some(calls) = row["tool_calls"].as_array()
{
for (offset, call) in calls.iter().enumerate() {
if call["name"] == "invoke_subagent" {
self.spawn_slots.insert(index + offset as u64 + 1);
}
}
} else if row["type"] == "GENERIC"
&& self.spawn_slots.remove(&index)
&& let Some(id) = row["content"].as_str().and_then(child_id)
{
candidates.insert(id, parent.to_owned());
}
}
}
fn drain(&mut self, path: &Path) -> std::io::Result<Vec<Value>> {
let mut file = std::fs::File::open(path)?;
let metadata = file.metadata()?;
let version = metadata.modified().ok().map(|time| (time, metadata.len()));
if version.is_some() && self.version == version {
return Ok(Vec::new());
}
file.seek(SeekFrom::Start(self.offset))?;
let mut reader = std::io::BufReader::new(file);
let mut rows = Vec::new();
let mut buf = Vec::new();
let mut settled_prefix = true;
while reader.read_until(b'\n', &mut buf)? > 0 {
if !buf.ends_with(b"\n") {
break;
}
let Ok(row) = serde_json::from_slice::<Value>(&buf) else {
break;
};
let Some(index) = row["step_index"].as_u64() else {
break;
};
if row["status"] != "DONE" {
settled_prefix = false;
} else if settled_prefix {
self.offset += buf.len() as u64;
if !self.delivered_after_pending.remove(&index) {
rows.push(row);
}
} else if self.delivered_after_pending.insert(index) {
rows.push(row);
}
buf.clear();
}
self.version = version;
Ok(rows)
}
}
fn child_id(body: &str) -> Option<String> {
let mut lines = body
.lines()
.skip_while(|s| s.starts_with("Created At: ") || s.starts_with("Completed At: "));
if lines.next()? != "Created the following subagent:" {
return None;
}
let rest = lines.collect::<Vec<_>>().join("\n");
let data = serde_json::Deserializer::from_str(&rest)
.into_iter::<Value>()
.next()?
.ok()?;
Some(
Uuid::parse_str(data["conversationId"].as_str()?)
.ok()?
.to_string(),
)
}
fn system_sender(row: &Value) -> Option<&str> {
if row["type"] != "SYSTEM_MESSAGE" || row["source"] != "SYSTEM" {
return None;
}
row["content"]
.as_str()?
.lines()
.find(|l| l.starts_with("[Message] timestamp="))?
.split_whitespace()
.nth(2)?
.strip_prefix("sender=")
}
struct Driver {
rpc: Rpc,
root: String,
brain: PathBuf,
cwd: PathBuf,
tier: Tier,
events: Events,
state: Arc<Mutex<State>>,
journals: HashMap<String, Journal>,
candidates: HashMap<String, String>,
approvals: HashMap<(String, String), oneshot::Sender<()>>,
parent_woke: bool,
}
impl Drop for Driver {
fn drop(&mut self) {
let mut state = self.state.lock().unwrap();
state.prompt = None;
state.steering.take();
for (_, pending) in self.approvals.drain() {
let _ = pending.send(());
}
}
}
impl Driver {
async fn run(mut self, mut incoming: mpsc::UnboundedReceiver<Incoming>) {
let mut tick = tokio::time::interval(Duration::from_millis(300));
loop {
tokio::select! {
msg = incoming.recv() => match msg {
Some(msg) => self.receive(msg).await,
None => break,
},
_ = tick.tick() => self.inspect().await,
}
}
let _ = self.events.send(AgentEvent::Exited("agy exited".into()));
}
async fn receive(&mut self, msg: Incoming) {
match msg {
Incoming::Reply { id, result } => {
self.inspect().await;
self.state.lock().unwrap().replied(id, result, &self.events);
}
Incoming::Notification { method, params } if method == "session/update" => {
self.update(¶ms)
}
Incoming::Request { id, method, params } if method == "session/request_permission" => {
self.permission(id, ¶ms).await
}
Incoming::Request { id, .. } => {
let _ = self
.rpc
.respond_error(id, "unsupported client operation")
.await;
}
_ => {}
}
}
fn update(&mut self, p: &Value) {
let Some(session) = p["sessionId"].as_str() else {
return;
};
if !self.journals.contains_key(session) {
return;
}
let root = session == self.root;
let u = &p["update"];
let mut s = self.state.lock().unwrap();
match u["sessionUpdate"].as_str().unwrap_or("") {
"config_option_update" if root => settings(u, &self.events),
"user_message_chunk" if root => s
.inputs
.read(u["content"]["text"].as_str().unwrap_or(""), &self.events),
"agent_message_chunk" if root => {
if let Some(text) = u["content"]["text"].as_str().filter(|s| !s.is_empty()) {
self.events.progress();
s.text.push_str(text);
}
}
"tool_call" | "tool_call_update" => {
let Some(id) = u["toolCallId"].as_str() else {
return;
};
let key = format!("{session}:{id}");
if u["sessionUpdate"] == "tool_call_update" {
self.events.progress();
}
let name = u["name"].as_str().unwrap_or("tool");
let finish = matches!(u["status"].as_str(), Some("completed" | "failed"));
if finish {
if let Some(gone) = self.approvals.remove(&(session.to_owned(), id.to_owned()))
{
let _ = gone.send(());
}
if root && s.tools.remove(&key) && s.prompt.is_none() {
s.waking = true;
}
if u["status"] == "failed" {
s.spawns.remove(&key);
} else if let Some(completed) = s.spawns.get_mut(&key) {
*completed = Some(Instant::now());
}
} else if u["sessionUpdate"] == "tool_call" {
if root {
s.tools.insert(key.clone());
}
if name == "invoke_subagent" {
s.spawns.entry(key.clone()).or_insert(None);
}
}
if u["sessionUpdate"] == "tool_call" {
if root && !s.text.is_empty() {
let _ = self
.events
.send(AgentEvent::Said(std::mem::take(&mut s.text)));
}
let detail = u["rawInput"]["CommandLine"]
.as_str()
.or_else(|| u["rawInput"]["Path"].as_str())
.or_else(|| u["title"].as_str())
.unwrap_or("");
let _ = self.events.send(AgentEvent::Tool {
id: Some(key),
name: name.to_owned(),
detail: super::clip(detail, 300),
});
}
if u["status"] == "completed"
&& matches!(name, "write_file" | "edit_file")
&& let Some(path) = u["rawInput"]["Path"].as_str()
{
let _ = self.events.send(AgentEvent::FileChanged(path.to_owned()));
}
}
_ => {}
}
}
async fn permission(&mut self, id: Value, p: &Value) {
let session = p["sessionId"].as_str().unwrap_or("");
let tool = &p["toolCall"];
let name = tool["name"].as_str().unwrap_or("");
let title = tool["title"].as_str().unwrap_or(name);
let choice = |kind: &str| {
p["options"]
.as_array()?
.iter()
.find(|o| o["kind"] == kind)?
.get("optionId")
.cloned()
};
let allow = choice("allow_once");
let reject = choice("reject_once");
let known = self.journals.contains_key(session);
let bridge = matches!(
name,
"mcp_axiopera_ask" | "mcp_axiopera_tell" | "mcp_axiopera_answers"
) || (self.tier == Tier::Read && name == "mcp_axiopera_list_directory");
let reply = |option: Option<Value>| match option {
Some(o) => json!({"outcome":{"outcome":"selected","optionId":o}}),
None => json!({"outcome":{"outcome":"cancelled"}}),
};
if !known || bridge || self.tier == Tier::Read || allow.is_none() {
if !bridge && known {
let _ = self.events.send(AgentEvent::Blocked(title.to_owned()));
}
let answer = if known && bridge { allow } else { reject };
let _ = self.rpc.respond(id, reply(answer)).await;
return;
}
let (tx, rx) = oneshot::channel();
let key = (
session.to_owned(),
tool["toolCallId"].as_str().unwrap_or("").to_owned(),
);
if let Some(previous) = self.approvals.insert(key, tx) {
let _ = previous.send(());
}
let summary = if session == self.root {
title.to_owned()
} else {
format!("subagent {session}: {title}")
};
let decision = super::ask_withdrawable_approval(
&self.events,
super::clip(&summary, 200),
format!("{summary}\n{}", super::arguments(&tool["rawInput"])),
rx,
);
let rpc = self.rpc.clone();
tokio::spawn(async move {
if let Some(d) = decision.await {
let _ = rpc
.respond(
id,
reply(if matches!(d, Decision::Approve) {
allow
} else {
reject
}),
)
.await;
}
});
}
async fn inspect(&mut self) {
let mut updates = Vec::new();
for (sid, journal) in &mut self.journals {
let rows = journal
.drain(&transcript(&self.brain, sid))
.unwrap_or_default();
if rows.is_empty() && journal.held.is_empty() {
continue;
}
journal.find_children(sid, &rows, &mut self.candidates);
updates.push((sid.clone(), rows));
}
self.attach_children();
let known_children: HashSet<_> = self
.journals
.keys()
.filter(|id| *id != &self.root)
.cloned()
.collect();
for (sid, rows) in updates {
let journal = self.journals.get_mut(&sid).unwrap();
let rows = journal.ready(rows, |r| {
sid == self.root
&& system_sender(r).is_some_and(|id| self.candidates.contains_key(id))
});
for r in rows {
let body = r["content"].as_str().unwrap_or("");
match r["type"].as_str() {
Some("USER_INPUT") if sid == self.root && r["source"] == "USER_EXPLICIT" => {
if let Some(text) = body.strip_prefix("<USER_REQUEST>\n").and_then(|text| {
text.rsplit_once("\n</USER_REQUEST>")
.map(|(input, _)| input)
}) && self
.state
.lock()
.unwrap()
.inputs
.0
.iter()
.any(|(_, input)| input.trim_end() == text.trim_end())
{
journal.user_inputs.push(text.to_owned());
}
}
Some("PLANNER_RESPONSE") if r["status"] == "DONE" => {
if !journal.user_inputs.is_empty() {
let mut state = self.state.lock().unwrap();
for text in journal.user_inputs.drain(..) {
state.inputs.read(&text, &self.events);
}
}
let calls = r["tool_calls"]
.as_array()
.map(Vec::as_slice)
.unwrap_or_default();
if calls.is_empty() && !body.is_empty() {
let mut s = self.state.lock().unwrap();
if sid != self.root {
s.children.remove(&sid);
} else if self.parent_woke {
self.parent_woke = false;
s.waking = false;
if s.prompt.is_none() {
s.text.clear();
let _ = self.events.send(AgentEvent::TurnEnded {
ok: true,
result: body.to_owned(),
error: None,
usage: None,
});
}
}
}
}
Some("SYSTEM_MESSAGE") if sid == self.root && r["source"] == "SYSTEM" => {
let mut s = self.state.lock().unwrap();
let from_child =
system_sender(&r).is_some_and(|id| known_children.contains(id));
if !from_child && !s.waking && s.tools.is_empty() {
continue;
}
if body.contains("All your subagents and background tasks have been stopped due to server restart.") {
continue;
}
self.parent_woke = true;
s.waking = true;
if s.prompt.is_none() {
let _ = self.events.send(AgentEvent::TurnStarted);
}
}
_ => {}
}
}
}
let mut state = self.state.lock().unwrap();
for (parent, journal) in &mut self.journals {
journal.match_spawns(parent, &mut state.spawns);
}
if state
.spawns
.values()
.flatten()
.any(|t| t.elapsed() > Duration::from_secs(30))
{
let _ = self.events.send(AgentEvent::Exited(
"could not locate the AGY subagent in its parent's transcript".into(),
));
}
}
fn attach_children(&mut self) {
for (id, parent) in self.candidates.clone() {
let path = transcript(&self.brain, &id);
let Some(first) = std::fs::File::open(&path)
.ok()
.and_then(|f| std::io::BufReader::new(f).lines().next())
.and_then(Result::ok)
.and_then(|l| serde_json::from_str::<Value>(&l).ok())
else {
continue;
};
self.candidates.remove(&id);
if system_sender(&first) != Some(parent.as_str()) {
continue;
}
if self.journals.contains_key(&id) {
continue;
}
self.journals.insert(id.clone(), Journal::default());
self.journals.get_mut(&parent).unwrap().unmatched_children += 1;
self.state.lock().unwrap().children.insert(id.clone());
let rpc = self.rpc.clone();
let events = self.events.clone();
let params = json!({"sessionId":id,"cwd":self.cwd,"mcpServers":[]});
tokio::spawn(async move {
let load = rpc.call("session/load", params);
let error = match tokio::time::timeout(Duration::from_secs(30), load).await {
Ok(Ok(_)) => return,
Ok(Err(e)) => e.to_string(),
Err(_) => "session/load timed out".into(),
};
let _ = events.send(AgentEvent::Exited(format!(
"loading AGY subagent {id}: {error}"
)));
});
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[tokio::test]
async fn accepting_a_steer_keeps_the_turn_open_until_its_own_reply() {
let (events, mut rx) = Events::channel();
let (admitted, receipt) = oneshot::channel();
let mut state = State {
prompt: Some(4),
steering: Some(PendingSteer {
request: 5,
input: Some(1),
admitted,
}),
text: "answer".into(),
..Default::default()
};
let done = || Ok(json!({"stopReason":"end_turn"}));
state.replied(4, done(), &events);
assert!(matches!(receipt.await, Ok(Steer::Accepted)));
assert!(rx.try_recv().is_err());
state.replied(5, done(), &events);
assert!(
matches!(rx.try_recv(), Ok(AgentEvent::TurnEnded { ok: true, result, .. }) if result == "answer")
);
assert!(state.prompt.is_none());
}
#[test]
fn journal_tracks_completed_steps_across_rewrites_and_partial_lines() {
let path = std::env::temp_dir().join(format!("axiopera-agy-{}", Uuid::new_v4()));
let row = |index, status, body| {
format!(
"{}\n",
json!({"step_index":index, "status":status, "content":body})
)
};
let head = row(0, "DONE", "start");
let later = row(2, "DONE", "interjection");
std::fs::write(&path, format!("{head}{}{later}", row(1, "RUNNING", ""))).unwrap();
let mut journal = Journal::default();
let indices = |journal: &mut Journal| {
journal
.drain(&path)
.unwrap()
.iter()
.map(|row| row["step_index"].as_u64().unwrap())
.collect::<Vec<_>>()
};
assert_eq!(indices(&mut journal), [0, 2]);
assert!(indices(&mut journal).is_empty());
let done = format!("{head}{}{later}", row(1, "DONE", "longer result"));
let tail = row(3, "DONE", "final");
std::fs::write(&path, format!("{done}{}", tail.trim_end())).unwrap();
assert_eq!(indices(&mut journal), [1]);
std::fs::write(&path, format!("{done}{tail}")).unwrap();
assert_eq!(indices(&mut journal), [3]);
std::fs::remove_file(path).unwrap();
}
#[test]
fn terminal_selection_uses_live_ids_and_matches_the_requested_family() {
let mut session = json!({
"models": {"availableModels":[
{"name":"Future (Preview) (Deep)", "modelId":"opaque-variant"},
{"name":"Fixed (Built-in)", "modelId":"fixed-variant"}
]},
"configOptions":[{"id":"model","options":[
{"name":"Future (Preview)","value":"future-family"},
{"name":"Fixed (Built-in)","value":"fixed-family"}
]}]
});
let label = "Future (Preview) (Deep)";
for saved in [label, "opaque-variant"] {
assert_eq!(
preferred_variant(&session, saved, None),
Some("opaque-variant")
);
assert_eq!(
preferred_variant(&session, saved, Some("future-family")),
Some("opaque-variant")
);
assert_eq!(
preferred_variant(&session, saved, Some("fixed-family")),
None
);
}
assert_eq!(
preferred_variant(&session, "Fixed (Built-in)", Some("fixed-family")),
Some("fixed-variant")
);
assert_eq!(preferred_variant(&session, "removed", None), None);
session["models"]["availableModels"]
.as_array_mut()
.unwrap()
.push(json!({"name":label,"modelId":"another-variant"}));
assert_eq!(preferred_variant(&session, label, None), None);
}
#[test]
fn discovered_child_only_counts_against_a_completed_spawn_of_its_parent() {
let time = Instant::now();
let mut spawns = HashMap::from([
("parent:one".into(), None),
("parent:two".into(), None),
("other:one".into(), Some(time)),
]);
let mut journal = Journal {
unmatched_children: 1,
..Default::default()
};
journal.match_spawns("parent", &mut spawns);
assert_eq!(spawns.len(), 3);
spawns.remove("parent:one");
*spawns.get_mut("parent:two").unwrap() = Some(time);
journal.match_spawns("parent", &mut spawns);
assert_eq!(journal.unmatched_children, 0);
assert_eq!(spawns, HashMap::from([("other:one".into(), Some(time))]));
}
#[test]
fn child_routing_requires_native_spawn_output_and_a_system_sender() {
let parent = "6584e8a1-e918-4a0f-88b6-47148672c31a";
let id = "1cf609ad-ab02-4be5-af76-3bd314191878";
let body = format!(
"Created At: t\nCompleted At: t\nCreated the following subagent:\n{}\nMore information",
json!({"conversationId":id})
);
assert_eq!(child_id(&body).as_deref(), Some(id));
assert!(child_id(&format!("command output\n{body}")).is_none());
assert!(child_id(&body.replace(id, "../other")).is_none());
let mut row = json!({"type":"SYSTEM_MESSAGE","source":"SYSTEM",
"content":format!("header\n[Message] timestamp=t sender={parent} priority=p content=hello")});
assert_eq!(system_sender(&row), Some(parent));
row["source"] = json!("MODEL");
assert!(system_sender(&row).is_none());
}
#[tokio::test]
async fn child_wake_is_replayed_once_its_transcript_identifies_the_parent() {
let brain = std::env::temp_dir().join(format!("axiopera-agy-{}", Uuid::new_v4()));
let root = Uuid::new_v4().to_string();
let child = Uuid::new_v4().to_string();
let from = |sender: &str| {
json!({"type":"SYSTEM_MESSAGE","source":"SYSTEM",
"content":format!("[Message] timestamp=t sender={sender} priority=p content=hello")})
};
let write = |id: &str, rows: Vec<Value>| {
let path = transcript(&brain, id);
std::fs::create_dir_all(path.parent().unwrap()).unwrap();
let mut body = String::new();
for (i, mut row) in rows.into_iter().enumerate() {
row["step_index"] = json!(i);
row["status"] = json!("DONE");
body.push_str(&row.to_string());
body.push('\n');
}
std::fs::write(path, body).unwrap();
};
write(
&root,
vec![
json!({"type":"PLANNER_RESPONSE","tool_calls":[{"name":"invoke_subagent"}]}),
json!({"type":"GENERIC","content":format!("Created the following subagent:\n{}",json!({"conversationId":child}))}),
from(&child),
json!({"type":"PLANNER_RESPONSE","content":"child finished"}),
],
);
let (events, mut rx) = Events::channel();
let mut driver = Driver {
rpc: Rpc::closed(),
root: root.clone(),
brain: brain.clone(),
cwd: brain.clone(),
tier: Tier::Work,
events,
state: Arc::default(),
journals: HashMap::from([(root.clone(), Journal::default())]),
candidates: HashMap::new(),
approvals: HashMap::new(),
parent_woke: false,
};
driver.inspect().await;
assert!(rx.try_recv().is_err());
write(&child, vec![from(&root)]);
driver.inspect().await;
assert!(matches!(rx.try_recv(), Ok(AgentEvent::TurnStarted)));
assert!(
matches!(rx.try_recv(), Ok(AgentEvent::TurnEnded { ok: true, result, .. }) if result == "child finished")
);
driver.inspect().await;
assert!(rx.try_recv().is_err());
std::fs::remove_dir_all(brain).unwrap();
}
}