1use std::collections::BTreeMap;
33use std::path::{Path, PathBuf};
34use std::process::Stdio;
35use std::time::{Duration, Instant};
36
37use anyhow::{Context as _, Result, bail};
38use serde::{Deserialize, Serialize};
39use std::sync::{Arc, Mutex};
40
41use tokio::io::{AsyncReadExt as _, AsyncWriteExt as _};
42use tokio::process::Command;
43
44use crate::config::{AgentChoice, AgentKind, AgentSpec, Delivery};
45use crate::proc::Quiet as _;
46use crate::rng::SplitMix64;
47
48#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
51pub struct SeatState {
52 pub key: String,
54 pub agent: String,
56 pub turns: usize,
58 pub claude_session: Option<String>,
61 pub captured_session: Option<String>,
63}
64
65impl SeatState {
66 pub fn new(key: &str, agent: &str, run_seed: u64) -> Self {
68 let mut rng = SplitMix64::new(run_seed ^ crate::rng::fnv1a(key));
69 Self {
70 key: key.to_owned(),
71 agent: agent.to_owned(),
72 turns: 0,
73 claude_session: Some(rng.uuid_v4()),
74 captured_session: None,
75 }
76 }
77}
78
79pub fn has_session(kind: AgentKind, seat: &SeatState, sessions_enabled: bool) -> bool {
81 if !sessions_enabled || seat.turns == 0 {
82 return false;
83 }
84 match kind {
85 AgentKind::Claude => seat.claude_session.is_some(),
86 AgentKind::Opencode | AgentKind::Antigravity | AgentKind::Codex | AgentKind::Omp => {
87 seat.captured_session.is_some()
88 }
89 AgentKind::Command => true,
90 }
91}
92
93#[derive(Debug, Clone)]
95pub struct Invocation<'a> {
96 pub coordination: Option<&'a crate::config::Coordination>,
98 pub fetched_base: Option<&'a crate::run::FetchedBase>,
100 pub cwd: &'a Path,
103 pub prompt: &'a str,
105 pub timeout: Duration,
107 pub allow_write: bool,
112 pub sessions: bool,
114 pub artifacts: &'a Path,
116 pub stem: &'a str,
118 pub run: &'a str,
122 pub node: &'a str,
126 pub cache_dir: Option<&'a Path>,
132 pub attachments: &'a [PathBuf],
139 pub writable: &'a [PathBuf],
146}
147
148fn repository_writer(inv: &Invocation<'_>) -> bool {
157 inv.allow_write
158 && (matches!(
159 inv.node,
160 "implement"
161 | "fix"
162 | "graft"
163 | "gate-fix"
164 | "land"
165 | "rebase"
166 | "base-conflict"
167 | "external-chat"
168 ) || inv.node == crate::queue::CHAT_NODE)
169}
170
171fn repository_seat(inv: &Invocation<'_>) -> bool {
172 !matches!(
178 inv.node,
179 crate::queue::CHAT_NODE | "external-chat" | "talk-title" | "deputy" | "conduct" | "dupes"
180 )
181}
182
183pub async fn worktree_writable_roots(cwd: &Path, home: &Path) -> Result<Vec<PathBuf>> {
190 let mut roots = Vec::new();
191 if let Some(common) = crate::repo_lock::common_dir(cwd).await? {
192 let cwd = std::fs::canonicalize(cwd).context("canonicalize seat working directory")?;
193 if !common.starts_with(&cwd) {
194 roots.push(common);
195 }
196 }
197 for name in ["questions", "notifications"] {
201 let dir = home.join(name);
202 std::fs::create_dir_all(&dir).with_context(|| format!("prepare seat {name} directory"))?;
203 roots.push(std::fs::canonicalize(dir)?);
204 }
205 Ok(roots)
206}
207
208fn editing_access_note(base: Option<&crate::run::FetchedBase>) -> String {
210 let mut note = String::from(
211 "Launcher repository access contract (applies to this turn and overrides earlier fetch instructions):\n",
212 );
213 if let Some(base) = base {
214 note.push_str(&format!(
215 "The launcher successfully fetched `{}/{}` at {} and pinned the run base to `{}`. Use this exact local commit as the verified fetched base; this is a snapshot, not a claim that the remote has not moved since. Do not fetch again as a prerequisite, move the base, or refresh it on resume. Base updates belong to the launcher's existing base-sync path.\n",
216 base.remote, base.branch, base.fetched_at, base.commit,
217 ));
218 } else {
219 note.push_str("No successful launcher fetch evidence is supplied for this invocation. Do not describe local tracking refs as freshly fetched. If fresh remote evidence is essential, report an infrastructure blocker requiring a launcher fetch and a new prepared turn.\n");
220 }
221 note
222}
223
224#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
231pub struct Quota {
232 #[serde(default)]
234 pub cause: QuotaCause,
235 #[serde(default)]
237 pub agent: String,
238 #[serde(default)]
240 pub reset: Option<String>,
241}
242
243#[derive(Debug, Default, Clone, Copy, Serialize, Deserialize, PartialEq, Eq)]
245#[serde(rename_all = "snake_case")]
246pub enum QuotaCause {
247 #[default]
249 UsageLimit,
250 Authentication,
252}
253
254#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
261pub struct Dropped {
262 pub why: String,
264 pub output_tokens: u64,
267}
268
269#[derive(Debug, Clone, Serialize, Deserialize)]
283pub struct CommandEvidence {
284 pub id: String,
286 pub description: String,
288 pub exit_code: Option<i32>,
290 pub result_summary: String,
292 pub source: String,
294}
295
296#[derive(Debug, Clone, Serialize, Deserialize)]
298pub struct AgentOutput {
299 pub text: String,
301 pub exit_code: Option<i32>,
303 pub timed_out: bool,
305 pub duration_ms: u64,
307 pub artifacts: Vec<String>,
309 #[serde(default)]
313 pub quota: Option<Quota>,
314 #[serde(default)]
318 pub dropped: Option<Dropped>,
319 #[serde(default)]
323 pub commands: Vec<CommandEvidence>,
324 #[serde(default)]
329 pub context_tokens: Option<u64>,
330}
331
332impl AgentOutput {
333 pub fn usable(&self) -> bool {
335 !self.quota_exhausted()
336 && !self.timed_out
337 && self.exit_code == Some(0)
338 && !self.text.trim().is_empty()
339 }
340
341 pub fn quota_exhausted(&self) -> bool {
343 self.quota.is_some()
344 }
345
346 pub fn work_undelivered(&self) -> bool {
351 self.dropped.is_some()
352 }
353}
354
355#[derive(Debug)]
358pub(crate) struct ExecutionInterrupted(pub String);
359
360impl std::fmt::Display for ExecutionInterrupted {
361 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
362 write!(f, "execution interrupted: {}", self.0)
363 }
364}
365
366impl std::error::Error for ExecutionInterrupted {}
367
368const PIPE_GRACE: Duration = Duration::from_secs(3);
373
374pub(crate) type Captured = Arc<Mutex<Vec<u8>>>;
376
377pub(crate) fn drain<R>(pipe: Option<R>) -> (Captured, Option<tokio::task::JoinHandle<()>>)
387where
388 R: tokio::io::AsyncRead + Unpin + Send + 'static,
389{
390 let buf: Captured = Arc::new(Mutex::new(Vec::new()));
391 let Some(mut pipe) = pipe else {
392 return (buf, None);
393 };
394 let sink = Arc::clone(&buf);
395 let handle = tokio::spawn(async move {
396 let mut chunk = [0u8; 8192];
397 loop {
398 match pipe.read(&mut chunk).await {
399 Ok(0) | Err(_) => break,
400 Ok(n) => {
401 if let Ok(mut guard) = sink.lock() {
402 guard.extend_from_slice(&chunk[..n]);
403 }
404 }
405 }
406 }
407 });
408 (buf, Some(handle))
409}
410
411pub(crate) async fn collect(
416 buf: &Captured,
417 handle: Option<tokio::task::JoinHandle<()>>,
418 grace: Duration,
419) -> String {
420 if let Some(handle) = handle {
421 if tokio::time::timeout(grace, handle).await.is_err() {
422 tracing::debug!("a pipe is still held open after the child exited");
423 }
424 }
425 let bytes = buf.lock().map(|g| g.clone()).unwrap_or_default();
426 String::from_utf8_lossy(&bytes).into_owned()
427}
428
429pub async fn invoke(
431 spec: &AgentSpec,
432 seat: &mut SeatState,
433 inv: &Invocation<'_>,
434) -> Result<AgentOutput> {
435 let lease = if repository_seat(inv) {
440 crate::repo_lock::SeatLease::for_checkout(inv.cwd, inv.allow_write, inv.coordination)
441 .await?
442 } else {
443 None
444 };
445 let inv = &Invocation {
446 timeout: spec.turn_timeout(inv.timeout),
447 ..inv.clone()
448 };
449 let mut outputs = Vec::new();
450 if repository_writer(inv) {
451 let min_free = crate::run::RunState::load_under(inv.run, &crate::run::home())
452 .map_or(0, |state| state.config.disk.min_free_bytes);
453 let mut paths = vec![inv.cwd.to_owned()];
454 if let Some(cache) = inv.cache_dir {
455 paths.push(cache.to_owned());
456 }
457 outputs.extend(
458 crate::retention::OutputLease::admit(&crate::run::home(), &paths, min_free)
459 .map_err(|e| crate::repo_lock::ResourceBlocked(format!("disk admission: {e:#}")))?,
460 );
461 }
462 if repository_seat(inv) {
463 if let Some(guard) = crate::retention::OutputLease::borrow(inv.cwd).map_err(|e| {
464 crate::repo_lock::ResourceBlocked(format!("worktree output ownership: {e:#}"))
465 })? {
466 outputs.push(guard);
467 }
468 if let Some(cache) = inv.cache_dir {
469 outputs.push(crate::retention::OutputLease::cache(
470 &crate::run::home(),
471 cache,
472 false,
473 )?);
474 }
475 }
476
477 let mut prompt = inv.prompt.to_owned();
478 if inv.node == crate::queue::CHAT_NODE {
479 prompt.push_str("\n\n");
480 prompt.push_str(crate::prompt::CHAT_FOLLOW_THROUGH);
481 }
482 if let Some(lease) = lease.as_ref().filter(|lease| lease.delegated) {
483 prompt.push_str(&format!("\n\nRepository coordination: the launcher holds the exclusive repository lock at {} for this invocation. Do not acquire a second lock or release/delete it. Repository access remains read-only.", lease.path.display()));
484 }
485 let home = repository_writer(inv).then(crate::run::home);
488 let mut writable = inv.writable.to_vec();
489 if let Some(home) = &home {
490 for root in worktree_writable_roots(inv.cwd, home).await? {
491 if !writable.contains(&root) {
492 writable.push(root);
493 }
494 }
495 }
496 if repository_writer(inv) {
497 prompt.push_str("\n\n");
498 prompt.push_str(&editing_access_note(inv.fetched_base));
499 }
500 let inv = &Invocation {
501 writable: &writable,
502 prompt: &prompt,
503 ..inv.clone()
504 };
505 tokio::fs::create_dir_all(inv.artifacts)
506 .await
507 .with_context(|| format!("create {}", inv.artifacts.display()))?;
508 let prompt_path = inv.artifacts.join(format!("{}.prompt.md", inv.stem));
509 tokio::fs::write(&prompt_path, inv.prompt)
510 .await
511 .with_context(|| format!("write {}", prompt_path.display()))?;
512
513 let plan = build_command(spec, seat, inv, &prompt_path)?;
514 tracing::debug!(seat = %seat.key, agent = %spec.id, argv = ?plan.logged_argv(), "spawning agent");
515 tokio::fs::write(
517 inv.artifacts.join(format!("{}.access.json", inv.stem)),
518 serde_json::to_vec_pretty(&serde_json::json!({
519 "launcher_version": env!("CARGO_PKG_VERSION"),
520 "kind": spec.kind,
521 "allow_write": inv.allow_write,
522 "unsandboxed": repository_writer(inv),
523 "writable_roots": inv.writable,
524 "fetched_base": inv.fetched_base,
525 }))?,
526 )
527 .await
528 .context("write seat access evidence")?;
529
530 let started = Instant::now();
531 let child_path = spec
534 .env
535 .iter()
536 .find(|(k, _)| k.eq_ignore_ascii_case("PATH"))
537 .map(|(_, v)| std::ffi::OsString::from(v))
538 .or_else(|| std::env::var_os("PATH"))
539 .unwrap_or_default();
540 let program = crate::config::find_program_on(&plan.argv[0], &child_path).map_or_else(
541 || plan.argv[0].clone().into(),
542 std::path::PathBuf::into_os_string,
543 );
544 let mut cmd = Command::new(program);
545 cmd.args(&plan.argv[1..])
546 .current_dir(inv.cwd)
547 .envs(&spec.env)
548 .env("MAGI_SEAT", &seat.key)
549 .env("MAGI_TURN", seat.turns.to_string())
550 .env("MAGI_RUN", inv.run)
551 .env("MAGI_NODE", inv.node)
552 .env("MAGI_PROMPT_FILE", &prompt_path)
553 .env("MAGI_ALLOW_WRITE", if inv.allow_write { "1" } else { "0" })
554 .env("GIT_TERMINAL_PROMPT", "0")
555 .stdin(if plan.stdin.is_some() {
556 Stdio::piped()
557 } else {
558 Stdio::null()
559 })
560 .stdout(Stdio::piped())
561 .stderr(Stdio::piped())
562 .kill_on_drop(true)
563 .quiet();
566 crate::incremental::default_environment(&mut cmd);
567 if repository_writer(inv) {
568 crate::incremental::configure(&mut cmd, inv.cwd)?;
569 }
570 if let Some(home) = home {
571 cmd.env("MAGI_HOME", std::fs::canonicalize(home)?);
574 }
575 if let Some(cache) = inv.cache_dir {
576 cmd.env("CARGO_TARGET_DIR", cache);
579 } else {
580 cmd.env_remove("CARGO_TARGET_DIR");
588 }
589
590 #[cfg(any(unix, windows))]
591 let _chat_controls = {
592 use crate::chat_control::bridge::{self, Bridge};
593 bridge::clear_environment(&mut cmd);
596 let bridge = if inv.node == crate::queue::CHAT_NODE {
597 crate::run::try_home().and_then(|home| {
598 match Bridge::start(&home, inv.run, inv.artifacts) {
599 Ok(bridge) => Some(bridge),
600 Err(error) => {
601 tracing::warn!(%error, "chat controls unavailable; continuing conversation");
602 None
603 }
604 }
605 })
606 } else {
607 None
608 };
609 if let Some(bridge) = &bridge {
610 cmd.env("MAGI_HOME", std::fs::canonicalize(crate::run::home())?);
614 bridge.attach(&mut cmd);
615 }
616 bridge
617 };
618
619 let mut running = crate::seat_process::Running::spawn(cmd, lease, outputs)
620 .with_context(|| format!("spawn `{}` for seat {}", plan.argv[0], seat.key))?;
621 if let (Some(body), Some(mut sink)) = (plan.stdin.clone(), running.child().stdin.take()) {
625 tokio::spawn(async move {
626 sink.write_all(body.as_bytes()).await.ok();
627 sink.shutdown().await.ok();
628 });
629 }
630
631 let (out_buf, out_reader) = drain(running.child().stdout.take());
648 let (err_buf, err_reader) = drain(running.child().stderr.take());
649
650 let min_free = crate::run::RunState::load_under(inv.run, &crate::run::home())
651 .ok()
652 .map_or(0, |state| state.config.disk.min_free_bytes);
653 #[cfg(all(test, unix))]
654 let min_free = test_watchdog::floor(inv.run, min_free);
655 let mut paths = vec![inv.cwd.to_owned()];
656 if let Some(cache) = inv.cache_dir {
657 paths.push(cache.to_owned());
658 }
659 let mut pressure = None;
660 let waited = tokio::select! {
661 waited = tokio::time::timeout(inv.timeout, running.child().wait()) => Some(waited),
662 reason = crate::retention::emergency(paths, min_free) => {
663 pressure = Some(reason);
664 None
665 }
666 };
667 let (code, timed_out) = match waited {
668 Some(Ok(res)) => {
669 let status = res.with_context(|| format!("wait for seat {}", seat.key))?;
670 (status.code(), false)
671 }
672 Some(Err(_)) | None => {
673 if let Some(reason) = &pressure {
674 tracing::warn!(seat = %seat.key, %reason, "agent execution interrupted");
675 } else {
676 tracing::warn!(seat = %seat.key, secs = inv.timeout.as_secs(), "agent timed out");
677 }
678 if !running.coordinated() {
680 running
681 .child()
682 .kill()
683 .await
684 .context("stop timed-out seat")?;
685 }
686 (None, true)
687 }
688 };
689 running.finish().await;
690
691 let stdout = collect(&out_buf, out_reader, PIPE_GRACE).await;
696 let stderr = collect(&err_buf, err_reader, PIPE_GRACE).await;
697
698 let out_path = inv.artifacts.join(format!("{}.out", inv.stem));
699 let err_path = inv.artifacts.join(format!("{}.err", inv.stem));
700 tokio::fs::write(&out_path, &stdout).await.ok();
701 tokio::fs::write(&err_path, &stderr).await.ok();
702 if let Some(reason) = pressure {
703 return Err(
704 anyhow::Error::new(crate::repo_lock::ResourceBlocked(reason.clone()))
705 .context(ExecutionInterrupted(reason)),
706 );
707 }
708
709 if code != Some(0)
710 && (stdout.contains(crate::incremental::BLOCKED)
711 || stderr.contains(crate::incremental::BLOCKED))
712 {
713 let detail = if stderr.contains(crate::incremental::BLOCKED) {
714 &stderr
715 } else {
716 &stdout
717 };
718 return Err(crate::repo_lock::ResourceBlocked(tail_chars(detail, 2000)).into());
719 }
720
721 let mut extracted = extract(spec.kind, &stdout);
722 if spec.kind == AgentKind::Antigravity && extracted.quota.is_none() {
723 extracted.quota = agy_quota(&stdout, &stderr);
724 }
725 if let Some(quota) = &mut extracted.quota {
726 quota.agent.clone_from(&spec.id);
727 if quota.cause == QuotaCause::Authentication {
728 let _ = crate::notices::Notices::open().raise(crate::notices::Notice::warn(
729 &format!("agent-authentication:{}", spec.id),
730 format!("Agent `{}` cannot authenticate. Re-authenticate this agent, then resume the held task.", spec.id),
731 ));
732 }
733 extracted.dropped = None;
736 tracing::warn!(
737 seat = %seat.key,
738 agent = %spec.id,
739 reset = ?quota.reset,
740 cause = ?quota.cause,
741 "agent needs machine-side recovery"
742 );
743 }
744 if let Some(session) = extracted.session {
745 match spec.kind {
746 AgentKind::Claude => seat.claude_session = Some(session),
747 AgentKind::Opencode | AgentKind::Antigravity | AgentKind::Codex | AgentKind::Omp => {
748 seat.captured_session = Some(session);
749 }
750 AgentKind::Command => {}
751 }
752 }
753 if let Some(status) = &extracted.status
754 && !status.eq_ignore_ascii_case("success")
755 {
756 tracing::warn!(seat = %seat.key, status = %status, "agent reported a non-success status");
757 }
758 let text = if extracted.text.trim().is_empty() {
759 if stdout.trim().is_empty() {
761 stderr.trim().to_owned()
762 } else {
763 stdout.trim().to_owned()
764 }
765 } else {
766 extracted.text
767 };
768 seat.turns += 1;
769
770 Ok(AgentOutput {
771 text,
772 exit_code: code,
773 timed_out,
774 duration_ms: started.elapsed().as_millis() as u64,
775 artifacts: vec![
776 file_name(&prompt_path),
777 file_name(&out_path),
778 file_name(&err_path),
779 ],
780 quota: extracted.quota,
781 dropped: extracted.dropped,
782 commands: extracted.commands,
783 context_tokens: extracted.context_tokens,
784 })
785}
786
787fn file_name(p: &Path) -> String {
788 p.file_name()
789 .unwrap_or_default()
790 .to_string_lossy()
791 .into_owned()
792}
793
794#[derive(Debug)]
796struct Plan {
797 argv: Vec<String>,
798 stdin: Option<String>,
799}
800
801impl Plan {
802 fn logged_argv(&self) -> Vec<&str> {
805 let mut settings_value = false;
806 self.argv
807 .iter()
808 .map(|arg| {
809 if settings_value {
810 settings_value = false;
811 "[redacted]"
812 } else if arg == "--settings" {
813 settings_value = true;
814 arg.as_str()
815 } else if arg.starts_with("--settings=") {
816 "--settings=[redacted]"
817 } else {
818 arg.as_str()
819 }
820 })
821 .collect()
822 }
823}
824
825fn pointer(kind: AgentKind, prompt_path: &Path) -> String {
836 if matches!(kind, AgentKind::Antigravity) {
837 return format!("@{}", prompt_path.display());
838 }
839 format!(
840 "Read the file at {} and follow every instruction in it exactly. That \
841 file is your complete task description; this message contains nothing \
842 else.",
843 prompt_path.display()
844 )
845}
846
847fn build_command(
848 spec: &AgentSpec,
849 seat: &SeatState,
850 inv: &Invocation<'_>,
851 prompt_path: &Path,
852) -> Result<Plan> {
853 let mut argv: Vec<String> = Vec::new();
854 let mut stdin: Option<String> = None;
855 let delivery = spec.delivery();
856 let resuming = has_session(spec.kind, seat, inv.sessions);
857
858 match spec.kind {
859 AgentKind::Claude => {
860 argv.push("claude".to_owned());
865 argv.push("-p".to_owned());
866 argv.push("--output-format".to_owned());
867 argv.push("json".to_owned());
868 if let Some(m) = &spec.model {
869 argv.push("--model".to_owned());
870 argv.push(m.clone());
871 }
872 if inv.sessions {
873 let uuid = seat
874 .claude_session
875 .as_deref()
876 .context("claude seat is missing its session uuid")?;
877 argv.push(if resuming { "--resume" } else { "--session-id" }.to_owned());
878 argv.push(uuid.to_owned());
879 }
880 argv.push("--permission-mode".to_owned());
881 argv.push("bypassPermissions".to_owned());
882 if !inv.allow_write {
883 argv.push("--disallowed-tools".to_owned());
884 argv.push("Edit,Write,MultiEdit,NotebookEdit".to_owned());
885 }
886 }
887 AgentKind::Opencode => {
888 argv.push("opencode".to_owned());
892 argv.push("run".to_owned());
893 argv.push("--format".to_owned());
894 argv.push("json".to_owned());
895 argv.push("--dir".to_owned());
896 argv.push(inv.cwd.to_string_lossy().into_owned());
897 argv.push("--auto".to_owned());
906 if let Some(m) = &spec.model {
907 argv.push("-m".to_owned());
908 argv.push(m.clone());
909 }
910 if resuming {
911 argv.push("-s".to_owned());
912 argv.push(
913 seat.captured_session
914 .clone()
915 .expect("has_session checked the id is present"),
916 );
917 }
918 }
919 AgentKind::Antigravity => {
920 argv.push("agy".to_owned());
921 argv.push("--output-format".to_owned());
922 argv.push("json".to_owned());
923 argv.push("--print-timeout".to_owned());
926 argv.push(format!("{}s", inv.timeout.as_secs()));
927 argv.push("--mode".to_owned());
928 argv.push(
929 if inv.allow_write {
930 "accept-edits"
931 } else {
932 "plan"
933 }
934 .to_owned(),
935 );
936 if inv.allow_write {
937 argv.push("--dangerously-skip-permissions".to_owned());
938 }
939 if let Some(m) = &spec.model {
940 argv.push("--model".to_owned());
941 argv.push(m.clone());
942 }
943 if resuming {
944 argv.push("--conversation".to_owned());
945 argv.push(
946 seat.captured_session
947 .clone()
948 .expect("has_session checked the id is present"),
949 );
950 }
951 let mut add_dirs: Vec<String> = Vec::new();
961 if delivery == Delivery::File || !inv.attachments.is_empty() {
962 add_dirs.push(inv.artifacts.to_string_lossy().into_owned());
963 }
964 for path in inv.attachments {
965 let Some(parent) = path.parent() else {
966 continue;
967 };
968 if parent.starts_with(inv.artifacts) {
969 continue;
970 }
971 let dir = parent.to_string_lossy().into_owned();
972 if !add_dirs.contains(&dir) {
973 add_dirs.push(dir);
974 }
975 }
976 if inv.allow_write {
977 for path in inv.writable {
978 let dir = path.to_string_lossy().into_owned();
979 if !add_dirs.contains(&dir) {
980 add_dirs.push(dir);
981 }
982 }
983 }
984 for dir in add_dirs {
985 argv.push("--add-dir".to_owned());
986 argv.push(dir);
987 }
988 }
989 AgentKind::Codex => {
990 argv.push("codex".to_owned());
996 argv.push("exec".to_owned());
997 argv.push("--json".to_owned());
998 argv.push("--skip-git-repo-check".to_owned());
1001 argv.push("-C".to_owned());
1002 argv.push(inv.cwd.to_string_lossy().into_owned());
1003 if repository_writer(inv) {
1013 argv.push("--dangerously-bypass-approvals-and-sandbox".to_owned());
1014 } else {
1015 argv.push("--sandbox".to_owned());
1016 argv.push(
1017 if inv.allow_write {
1018 "workspace-write"
1019 } else {
1020 "read-only"
1021 }
1022 .to_owned(),
1023 );
1024 argv.push("-c".to_owned());
1027 argv.push("approval_policy=\"never\"".to_owned());
1028 if inv.allow_write && !inv.writable.is_empty() {
1029 let roots: Vec<String> = inv
1030 .writable
1031 .iter()
1032 .map(|p| p.to_string_lossy().into_owned())
1033 .collect();
1034 argv.push("-c".to_owned());
1035 argv.push(format!(
1036 "sandbox_workspace_write.writable_roots={}",
1037 serde_json::to_string(&roots).unwrap_or_else(|_| "[]".to_owned())
1038 ));
1039 }
1040 }
1041 if let Some(m) = &spec.model {
1042 argv.push("-m".to_owned());
1043 argv.push(m.clone());
1044 }
1045 if resuming {
1051 argv.push("resume".to_owned());
1052 argv.push(
1053 seat.captured_session
1054 .clone()
1055 .expect("has_session checked the id is present"),
1056 );
1057 }
1058 }
1059 AgentKind::Omp => {
1060 argv.push("omp".to_owned());
1064 argv.push("-p".to_owned());
1065 argv.push("--mode=json".to_owned());
1066 argv.push("--auto-approve".to_owned());
1076 if let Some(m) = &spec.model {
1077 argv.push("--model".to_owned());
1078 argv.push(m.clone());
1079 }
1080 if resuming {
1086 argv.push("--resume".to_owned());
1087 argv.push(
1088 seat.captured_session
1089 .clone()
1090 .expect("has_session checked the id is present"),
1091 );
1092 }
1093 }
1094 AgentKind::Command => {
1095 if spec.command.is_empty() {
1100 bail!("agent `{}` has kind = \"command\" but no command", spec.id);
1101 }
1102 let vars: BTreeMap<&str, String> = BTreeMap::from([
1103 ("{prompt_file}", prompt_path.to_string_lossy().into_owned()),
1104 ("{cwd}", inv.cwd.to_string_lossy().into_owned()),
1105 ("{label}", seat.key.clone()),
1106 ("{session}", seat.claude_session.clone().unwrap_or_default()),
1107 ]);
1108 for raw in &spec.command {
1109 let mut arg = raw.clone();
1110 for (k, v) in &vars {
1111 if arg.contains(k) {
1112 arg = arg.replace(k, v);
1113 }
1114 }
1115 argv.push(arg);
1116 }
1117 }
1118 }
1119
1120 argv.extend(spec.extra_args.iter().cloned());
1121
1122 if spec.kind == AgentKind::Antigravity {
1125 argv.push("-p".to_owned());
1126 }
1127 if spec.kind == AgentKind::Codex && delivery == Delivery::Stdin {
1130 argv.push("-".to_owned());
1131 }
1132 match delivery {
1133 Delivery::Stdin if spec.kind == AgentKind::Antigravity => {
1134 argv.push(pointer(spec.kind, prompt_path));
1136 }
1137 Delivery::Stdin => stdin = Some(inv.prompt.to_owned()),
1138 Delivery::Argv => argv.push(inv.prompt.to_owned()),
1139 Delivery::File => argv.push(pointer(spec.kind, prompt_path)),
1140 }
1141
1142 Ok(Plan { argv, stdin })
1143}
1144
1145#[derive(Debug, Default)]
1147struct Extracted {
1148 text: String,
1149 session: Option<String>,
1150 status: Option<String>,
1151 quota: Option<Quota>,
1152 dropped: Option<Dropped>,
1153 commands: Vec<CommandEvidence>,
1154 context_tokens: Option<u64>,
1155}
1156
1157fn extract(kind: AgentKind, stdout: &str) -> Extracted {
1159 let mut extracted = extract_answer(kind, stdout);
1160 extracted.context_tokens = context_tokens(kind, stdout);
1161 extracted
1162}
1163
1164fn uint(v: &serde_json::Value, key: &str) -> Option<u64> {
1166 v.get(key).and_then(serde_json::Value::as_u64)
1167}
1168
1169fn input_side(usage: &serde_json::Value, input: &[&str], cache: &[&str]) -> Option<u64> {
1174 let first = |keys: &[&str]| keys.iter().find_map(|k| uint(usage, k));
1175 let base = first(input)?;
1176 let cached: u64 = cache.iter().filter_map(|k| uint(usage, k)).sum();
1177 Some(base.saturating_add(cached))
1178}
1179
1180fn context_tokens(kind: AgentKind, stdout: &str) -> Option<u64> {
1201 use serde_json::Value;
1202 let lines = || {
1203 stdout
1204 .lines()
1205 .filter_map(|l| serde_json::from_str::<Value>(l.trim()).ok())
1206 };
1207 match kind {
1208 AgentKind::Claude => None,
1209 AgentKind::Opencode => lines()
1210 .filter(|v| {
1211 v.get("type").and_then(|t| t.as_str()) == Some("step_finish")
1213 || v.get("part")
1214 .and_then(|p| p.get("type"))
1215 .and_then(|t| t.as_str())
1216 == Some("step-finish")
1217 })
1218 .filter_map(|v| {
1219 let tokens = v.get("part")?.get("tokens")?;
1220 let base = uint(tokens, "input")?;
1221 let cache = tokens.get("cache");
1222 let cached = ["read", "write"]
1223 .iter()
1224 .filter_map(|k| cache.and_then(|c| uint(c, k)))
1225 .sum::<u64>();
1226 Some(base.saturating_add(cached))
1227 })
1228 .next_back(),
1229 AgentKind::Antigravity => None,
1230 AgentKind::Codex => None,
1231 AgentKind::Omp => lines()
1232 .flat_map(|v| {
1233 match v.get("type").and_then(|t| t.as_str()) {
1236 Some("agent_end") => v
1237 .get("messages")
1238 .and_then(|m| m.as_array())
1239 .cloned()
1240 .unwrap_or_default(),
1241 Some("turn_end") | Some("message_end") => {
1242 v.get("message").cloned().into_iter().collect()
1243 }
1244 _ => Vec::new(),
1245 }
1246 })
1247 .filter(|m| m.get("role").and_then(|r| r.as_str()) == Some("assistant"))
1248 .filter_map(|m| {
1249 input_side(
1250 m.get("usage")?,
1251 &["input", "input_tokens"],
1252 &[
1253 "cacheRead",
1254 "cacheWrite",
1255 "cache_read_tokens",
1256 "cache_write_tokens",
1257 ],
1258 )
1259 })
1260 .next_back(),
1261 AgentKind::Command => None,
1262 }
1263}
1264
1265pub(crate) fn answer_text(kind: AgentKind, stdout: &str) -> String {
1268 extract_answer(kind, stdout).text
1269}
1270
1271fn extract_answer(kind: AgentKind, stdout: &str) -> Extracted {
1272 match kind {
1273 AgentKind::Claude => {
1274 let Ok(v) = serde_json::from_str::<serde_json::Value>(stdout.trim()) else {
1275 return Extracted {
1276 text: stdout.trim().to_owned(),
1277 ..Extracted::default()
1278 };
1279 };
1280 Extracted {
1281 text: v
1282 .get("result")
1283 .and_then(|r| r.as_str())
1284 .unwrap_or_default()
1285 .to_owned(),
1286 session: v
1287 .get("session_id")
1288 .and_then(|s| s.as_str())
1289 .map(str::to_owned),
1290 status: v.get("is_error").and_then(|e| e.as_bool()).map(|e| {
1291 if e {
1292 "error".to_owned()
1293 } else {
1294 "success".to_owned()
1295 }
1296 }),
1297 quota: claude_quota(&v),
1298 dropped: None,
1301 commands: Vec::new(),
1302 context_tokens: None,
1303 }
1304 }
1305 AgentKind::Opencode => {
1306 let mut text = String::new();
1308 let mut session = None;
1309 for line in stdout.lines() {
1310 let Ok(v) = serde_json::from_str::<serde_json::Value>(line.trim()) else {
1311 continue;
1312 };
1313 if session.is_none() {
1314 session = v
1315 .get("sessionID")
1316 .and_then(|s| s.as_str())
1317 .map(str::to_owned);
1318 }
1319 let part = v.get("part").unwrap_or(&serde_json::Value::Null);
1320 if part.get("type").and_then(|t| t.as_str()) == Some("text")
1321 && let Some(t) = part.get("text").and_then(|t| t.as_str())
1322 {
1323 if !text.is_empty() {
1324 text.push('\n');
1325 }
1326 text.push_str(t);
1327 }
1328 }
1329 Extracted {
1330 text,
1331 session,
1332 status: None,
1333 quota: None,
1334 dropped: None,
1335 commands: Vec::new(),
1336 context_tokens: None,
1337 }
1338 }
1339 AgentKind::Antigravity => {
1340 let obj = stdout
1343 .lines()
1344 .rev()
1345 .find_map(|l| serde_json::from_str::<serde_json::Value>(l.trim()).ok());
1346 let Some(v) = obj else {
1347 return Extracted {
1348 text: stdout.trim().to_owned(),
1349 ..Extracted::default()
1350 };
1351 };
1352 Extracted {
1353 text: v
1354 .get("response")
1355 .and_then(|r| r.as_str())
1356 .unwrap_or_default()
1357 .trim()
1358 .to_owned(),
1359 session: v
1360 .get("conversation_id")
1361 .and_then(|s| s.as_str())
1362 .map(str::to_owned),
1363 status: v.get("status").and_then(|s| s.as_str()).map(str::to_owned),
1364 quota: None,
1365 dropped: dropped_stream(&v),
1366 commands: Vec::new(),
1367 context_tokens: None,
1368 }
1369 }
1370 AgentKind::Codex => {
1371 let mut text = String::new();
1393 let mut session = None;
1394 let mut status = None;
1395 let mut commands = Vec::new();
1396 for line in stdout.lines() {
1397 let Ok(v) = serde_json::from_str::<serde_json::Value>(line.trim()) else {
1398 continue;
1399 };
1400 match v.get("type").and_then(|t| t.as_str()) {
1401 Some("thread.started") => {
1402 session = v
1403 .get("thread_id")
1404 .and_then(|s| s.as_str())
1405 .map(str::to_owned);
1406 }
1407 Some("item.completed") => {
1408 let item = v.get("item").unwrap_or(&serde_json::Value::Null);
1409 match item.get("type").and_then(|t| t.as_str()) {
1410 Some("agent_message") => {
1411 if let Some(t) = item.get("text").and_then(|t| t.as_str()) {
1412 text = t.trim().to_owned();
1413 }
1414 }
1415 Some("command_execution") => {
1416 commands.push(command_evidence(item));
1417 }
1418 _ => {}
1419 }
1420 }
1421 Some("turn.completed") => status = Some("success".to_owned()),
1422 Some("turn.failed") => status = Some("error".to_owned()),
1423 _ => {}
1424 }
1425 }
1426 Extracted {
1427 text,
1428 session,
1429 status,
1430 quota: codex_quota(stdout),
1431 dropped: None,
1432 commands,
1433 context_tokens: None,
1434 }
1435 }
1436 AgentKind::Omp => {
1437 let mut text = String::new();
1458 let mut session = None;
1459 for line in stdout.lines() {
1460 let Ok(v) = serde_json::from_str::<serde_json::Value>(line.trim()) else {
1461 continue;
1462 };
1463 if v.get("type").and_then(|t| t.as_str()) == Some("session") {
1464 session = v.get("id").and_then(|s| s.as_str()).map(str::to_owned);
1465 continue;
1466 }
1467 let messages: Vec<&serde_json::Value> = match v.get("type").and_then(|t| t.as_str())
1471 {
1472 Some("agent_end") => v
1473 .get("messages")
1474 .and_then(|m| m.as_array())
1475 .map(|m| m.iter().collect())
1476 .unwrap_or_default(),
1477 Some("turn_end") | Some("message_end") => {
1478 v.get("message").into_iter().collect()
1479 }
1480 _ => continue,
1481 };
1482 for message in messages {
1483 if message.get("role").and_then(|r| r.as_str()) != Some("assistant") {
1484 continue;
1485 }
1486 let Some(parts) = message.get("content").and_then(|c| c.as_array()) else {
1487 continue;
1488 };
1489 for part in parts {
1490 if part.get("type").and_then(|t| t.as_str()) != Some("text") {
1491 continue;
1492 }
1493 if let Some(t) = part.get("text").and_then(|t| t.as_str())
1494 && !t.trim().is_empty()
1495 {
1496 text = t.trim().to_owned();
1497 }
1498 }
1499 }
1500 }
1501 Extracted {
1502 text,
1503 session,
1504 status: None,
1505 quota: None,
1506 dropped: None,
1507 commands: Vec::new(),
1508 context_tokens: None,
1509 }
1510 }
1511 AgentKind::Command => {
1512 let parsed = serde_json::from_str::<serde_json::Value>(stdout.trim()).ok();
1517 let quota = parsed.as_ref().and_then(claude_quota);
1518 let dropped = parsed.as_ref().and_then(dropped_stream);
1521 Extracted {
1522 text: stdout.trim().to_owned(),
1523 session: None,
1524 status: None,
1525 quota,
1526 dropped,
1527 commands: Vec::new(),
1528 context_tokens: None,
1529 }
1530 }
1531 }
1532}
1533
1534fn command_evidence(item: &serde_json::Value) -> CommandEvidence {
1541 let description = match item.get("command") {
1542 Some(serde_json::Value::String(s)) => s.clone(),
1543 Some(serde_json::Value::Array(parts)) => parts
1544 .iter()
1545 .filter_map(|p| p.as_str())
1546 .collect::<Vec<_>>()
1547 .join(" "),
1548 _ => String::new(),
1549 };
1550 let result_summary = item
1551 .get("aggregated_output")
1552 .and_then(|o| o.as_str())
1553 .map(|s| tail_chars(s.trim(), 400))
1554 .unwrap_or_default();
1555 CommandEvidence {
1556 id: item
1557 .get("id")
1558 .and_then(|s| s.as_str())
1559 .unwrap_or_default()
1560 .to_owned(),
1561 description,
1562 exit_code: item
1563 .get("exit_code")
1564 .and_then(serde_json::Value::as_i64)
1565 .map(|e| e as i32),
1566 result_summary,
1567 source: "codex".to_owned(),
1568 }
1569}
1570
1571fn tail_chars(s: &str, max: usize) -> String {
1573 let count = s.chars().count();
1574 if count <= max {
1575 return s.to_owned();
1576 }
1577 s.chars().skip(count - max).collect()
1578}
1579
1580fn codex_quota(stdout: &str) -> Option<Quota> {
1583 let mut message = None;
1584 let mut failed = false;
1585 for line in stdout.lines() {
1586 let Ok(v) = serde_json::from_str::<serde_json::Value>(line) else {
1587 continue;
1588 };
1589 match v.get("type").and_then(serde_json::Value::as_str) {
1590 Some("error") => {
1591 if let Some(m) = v.get("message").and_then(serde_json::Value::as_str)
1592 && m.starts_with("You've hit your usage limit. Visit https://chatgpt.com/settings/usage to purchase more credits")
1593 {
1594 message = Some(m.to_owned());
1595 }
1596 }
1597 Some("turn.failed") => failed = true,
1598 _ => {}
1599 }
1600 }
1601 if !failed {
1602 return None;
1603 }
1604 let message = message?;
1605 Some(Quota {
1606 reset: message
1607 .split_once("try again at ")
1608 .map(|(_, hint)| hint.trim().trim_end_matches('.').trim().to_owned())
1609 .filter(|hint| !hint.is_empty()),
1610 cause: QuotaCause::UsageLimit,
1611 agent: String::new(),
1612 })
1613}
1614
1615fn claude_quota(v: &serde_json::Value) -> Option<Quota> {
1622 let is_err = v.get("is_error").and_then(|e| e.as_bool()).unwrap_or(false);
1623 if !is_err {
1624 return None;
1625 }
1626 let result = v.get("result").and_then(|r| r.as_str()).unwrap_or("");
1627 if v.get("terminal_reason").and_then(serde_json::Value::as_str) == Some("api_error")
1628 && result.starts_with("Failed to authenticate:")
1629 && result.contains("OAuth session expired and could not be refreshed")
1630 {
1631 return Some(Quota {
1632 reset: None,
1633 cause: QuotaCause::Authentication,
1634 agent: String::new(),
1635 });
1636 }
1637 let status = v.get("api_error_status");
1638 let usage_limit = (status.and_then(serde_json::Value::as_u64) == Some(429)
1639 || status.and_then(serde_json::Value::as_str) == Some("429"))
1640 && v.get("api_error").and_then(serde_json::Value::as_str) == Some("usage_limit_reached");
1641 if !usage_limit && !result.to_lowercase().contains("session limit") {
1642 return None;
1643 }
1644 let reset = result
1647 .split("resets ")
1648 .nth(1)
1649 .map(str::trim)
1650 .filter(|s| !s.is_empty())
1651 .map(str::to_owned);
1652 Some(Quota {
1653 reset,
1654 cause: QuotaCause::UsageLimit,
1655 agent: String::new(),
1656 })
1657}
1658
1659fn agy_quota(stdout: &str, stderr: &str) -> Option<Quota> {
1674 let from_stdout = stdout
1675 .lines()
1676 .rev()
1677 .find_map(|l| serde_json::from_str::<serde_json::Value>(l.trim()).ok())
1678 .and_then(|v| {
1679 let status = v.get("status").and_then(|s| s.as_str()).unwrap_or("");
1680 let error = v.get("error").and_then(|e| e.as_str()).unwrap_or("");
1681 (status.eq_ignore_ascii_case("error") && error.to_lowercase().contains("quota reached"))
1682 .then(|| error.to_owned())
1683 });
1684 let from_stderr = || {
1685 stderr.lines().find_map(|l| {
1686 let v: serde_json::Value =
1687 serde_json::from_str(l.trim().strip_prefix("AGY_ERROR:")?.trim()).ok()?;
1688 let exhausted = v.get("status").and_then(|s| s.as_str()) == Some("RESOURCE_EXHAUSTED");
1689 let code = v.get("error_code").and_then(serde_json::Value::as_u64) == Some(429);
1690 (exhausted && code).then(|| {
1691 v.get("short_error")
1692 .and_then(|e| e.as_str())
1693 .unwrap_or_default()
1694 .to_owned()
1695 })
1696 })
1697 };
1698 let text = from_stdout.or_else(from_stderr)?;
1699 let reset = text
1700 .split_once("Resets ")
1701 .map(|(_, rest)| rest.trim().trim_end_matches('.').trim())
1702 .filter(|s| !s.is_empty())
1703 .map(str::to_owned);
1704 Some(Quota {
1705 reset,
1706 cause: QuotaCause::UsageLimit,
1707 agent: String::new(),
1708 })
1709}
1710
1711fn dropped_stream(v: &serde_json::Value) -> Option<Dropped> {
1742 let status = v.get("status").and_then(|s| s.as_str()).unwrap_or("");
1743 if !status.eq_ignore_ascii_case("error") {
1744 return None;
1745 }
1746 let response = v.get("response").and_then(|r| r.as_str()).unwrap_or("");
1747 if !response.trim().is_empty() {
1748 return None;
1750 }
1751 let produced = v
1752 .get("usage")
1753 .and_then(|u| u.get("output_tokens"))
1754 .and_then(serde_json::Value::as_u64)
1755 .unwrap_or(0);
1756 if produced == 0 {
1757 return None;
1759 }
1760 Some(Dropped {
1761 why: v
1762 .get("error")
1763 .and_then(|e| e.as_str())
1764 .unwrap_or("the CLI ended the stream without delivering its answer")
1765 .trim()
1766 .to_owned(),
1767 output_tokens: produced,
1768 })
1769}
1770
1771pub fn missing_programs(specs: &[AgentSpec]) -> Vec<String> {
1773 let mut missing = Vec::new();
1774 for s in specs {
1775 let program = match s.kind {
1776 AgentKind::Command => s.command.first().map(String::as_str),
1777 other => other.program(),
1778 };
1779 if let Some(p) = program
1780 && !crate::config::which(p)
1781 && !Path::new(p).is_file()
1782 && !missing.iter().any(|m: &String| m == p)
1783 {
1784 missing.push(p.to_owned());
1785 }
1786 }
1787 missing
1788}
1789
1790pub fn artifacts_dir(run_dir: &Path) -> PathBuf {
1792 run_dir.join("artifacts")
1793}
1794
1795pub fn installed(spec: &AgentSpec) -> bool {
1797 spec.kind.program().is_none_or(crate::config::which)
1800}
1801
1802pub fn pick(
1824 agents: &[AgentSpec],
1825 want: Option<&str>,
1826 available: &dyn Fn(&AgentSpec) -> bool,
1827) -> Result<AgentSpec> {
1828 if let Some(id) = want {
1829 let spec = agents
1830 .iter()
1831 .find(|a| a.id == id)
1832 .with_context(|| format!("no agent `{id}` in the roster; it has {}", ids(agents)))?;
1833 if !available(spec) {
1834 bail!(
1835 "agent `{}` needs `{}` on PATH; install it or pass a different \
1836 --agent",
1837 spec.id,
1838 spec.kind.program().unwrap_or("its command")
1839 );
1840 }
1841 return Ok(spec.clone());
1842 }
1843
1844 if agents.is_empty() {
1845 bail!(
1846 "the agent roster is empty, so there is nobody to ask: install one \
1847 of claude, opencode or agy - magi derives a roster from what is on \
1848 PATH - or add an [[agents]] entry to magi.toml."
1849 );
1850 }
1851
1852 if let Some(spec) = agents
1853 .iter()
1854 .find(|a| a.kind == AgentKind::Claude && available(a))
1855 {
1856 return Ok(spec.clone());
1857 }
1858
1859 agents
1860 .iter()
1861 .find(|a| available(a))
1862 .cloned()
1863 .with_context(|| {
1864 let missing = agents
1865 .iter()
1866 .filter_map(|a| a.kind.program())
1867 .collect::<Vec<_>>()
1868 .join(", ");
1869 format!(
1870 "no agent in the roster can be run here: install one of \
1871 {missing}, or add an [[agents]] entry to magi.toml for a CLI \
1872 you do have"
1873 )
1874 })
1875}
1876
1877pub fn for_github_text(spec: &AgentSpec, github_language: &str, cwd: &Path) -> Result<AgentSpec> {
1890 let mut spec = spec.clone();
1891 if spec.kind == AgentKind::Claude {
1892 let mut existing = None;
1895 let mut args = spec.extra_args.iter().enumerate();
1896 while let Some((index, arg)) = args.next() {
1897 if arg == "--settings" {
1898 let (value_index, value) = args.next().context("--settings requires a value")?;
1899 existing = Some((value_index, value.as_str(), false));
1900 } else if let Some(value) = arg.strip_prefix("--settings=") {
1901 existing = Some((index, value, true));
1902 }
1903 }
1904 let mut settings = if let Some((_, value, _)) = existing {
1905 let json = if value.trim_start().starts_with('{') {
1906 value.to_owned()
1907 } else {
1908 std::fs::read_to_string(cwd.join(value))
1909 .context("cannot read existing seat --settings file")?
1910 };
1911 serde_json::from_str::<serde_json::Value>(&json)
1912 .context("cannot parse existing seat --settings JSON")?
1913 } else {
1914 serde_json::json!({})
1915 };
1916 settings
1917 .as_object_mut()
1918 .context("seat --settings must be a JSON object")?
1919 .insert(
1920 "language".to_owned(),
1921 serde_json::json!(crate::lang::name(github_language)),
1922 );
1923 let settings = settings.to_string();
1924 if let Some((index, _, equals)) = existing {
1925 spec.extra_args[index] = if equals {
1926 format!("--settings={settings}")
1927 } else {
1928 settings
1929 };
1930 } else {
1931 spec.extra_args.extend(["--settings".to_owned(), settings]);
1932 }
1933 }
1934 Ok(spec)
1935}
1936
1937pub fn pick_chain(
1945 agents: &[AgentSpec],
1946 choice: Option<&AgentChoice>,
1947 available: &dyn Fn(&AgentSpec) -> bool,
1948 role: &str,
1949) -> Result<Vec<AgentSpec>> {
1950 let wanted = choice.map(AgentChoice::ids).unwrap_or_default();
1951 if wanted.is_empty() {
1952 return pick(agents, None, available)
1953 .map(|s| vec![s])
1954 .with_context(|| format!("choose an agent for the {role} role"));
1955 }
1956 let mut chain: Vec<AgentSpec> = Vec::new();
1957 for id in wanted {
1958 if chain.iter().any(|s| s.id == id) {
1959 continue;
1960 }
1961 match pick(agents, Some(id), available) {
1962 Ok(spec) => chain.push(spec),
1963 Err(e) => tracing::warn!("[roles] {role}: skipping `{id}`: {e:#}"),
1964 }
1965 }
1966 if chain.is_empty() {
1967 bail!(
1968 "[roles] {role} names no agent that can run here; the roster has {}",
1969 ids(agents)
1970 );
1971 }
1972 Ok(chain)
1973}
1974
1975pub fn chain_advances(outcome: &Result<AgentOutput>) -> bool {
1981 match outcome {
1982 Err(e) if e.is::<crate::repo_lock::ResourceBlocked>() => false,
1983 Err(_) => true,
1984 Ok(out) => output_advances(out),
1985 }
1986}
1987
1988pub fn output_advances(out: &AgentOutput) -> bool {
1992 out.quota_exhausted() || !out.usable()
1993}
1994
1995fn ids(agents: &[AgentSpec]) -> String {
1996 if agents.is_empty() {
1997 return "no agents at all".to_owned();
1998 }
1999 agents
2000 .iter()
2001 .map(|a| a.id.clone())
2002 .collect::<Vec<_>>()
2003 .join(", ")
2004}
2005
2006#[cfg(all(test, unix))]
2009pub(crate) mod test_watchdog {
2010 use super::*;
2011 static FLOORS: Mutex<BTreeMap<String, u64>> = Mutex::new(BTreeMap::new());
2012
2013 pub(crate) struct Guard(String);
2014
2015 pub(crate) fn set(run: &str, floor: u64) -> Guard {
2016 assert!(
2017 FLOORS
2018 .lock()
2019 .unwrap()
2020 .insert(run.to_owned(), floor)
2021 .is_none()
2022 );
2023 Guard(run.to_owned())
2024 }
2025
2026 pub(super) fn floor(run: &str, configured: u64) -> u64 {
2027 FLOORS
2028 .lock()
2029 .unwrap()
2030 .get(run)
2031 .copied()
2032 .unwrap_or(configured)
2033 }
2034
2035 impl Drop for Guard {
2036 fn drop(&mut self) {
2037 FLOORS.lock().unwrap().remove(&self.0);
2038 }
2039 }
2040}
2041
2042#[cfg(test)]
2043mod tests {
2044 use super::*;
2045
2046 const COMMAND_HELPER_MODE: &str = "MAGI_TEST_COMMAND_HELPER_MODE";
2047
2048 fn command_helper(mode: &str) -> AgentSpec {
2051 crate::run::pin_test_home();
2053 AgentSpec {
2054 id: "helper".to_owned(),
2055 kind: AgentKind::Command,
2056 model: None,
2057 command: vec![
2058 std::env::current_exe()
2059 .expect("locate test helper")
2060 .to_string_lossy()
2061 .into_owned(),
2062 "--exact".to_owned(),
2063 "agent::tests::command_agent_test_helper".to_owned(),
2064 "--nocapture".to_owned(),
2065 ],
2066 extra_args: Vec::new(),
2067 env: BTreeMap::from([(COMMAND_HELPER_MODE.to_owned(), mode.to_owned())]),
2068 prompt_delivery: None,
2069 max_turn_secs: None,
2070 }
2071 }
2072
2073 #[test]
2074 fn command_agent_test_helper() {
2075 match std::env::var(COMMAND_HELPER_MODE).as_deref() {
2076 Ok("reply") => println!("hello {}", std::env::var("MAGI_SEAT").unwrap()),
2077 Ok("cache") => println!("{}", std::env::var("CARGO_TARGET_DIR").unwrap()),
2078 Ok("no-cache") => println!(
2079 "{}",
2080 std::env::var("CARGO_TARGET_DIR").unwrap_or_else(|_| "ABSENT".to_owned())
2081 ),
2082 Ok("ignore-stdin") => println!("done"),
2083 Ok("chatty-sleep") => {
2084 println!("i-said-something");
2085 std::thread::sleep(Duration::from_secs(30));
2086 }
2087 Ok("sleep") => std::thread::sleep(Duration::from_secs(30)),
2088 Ok(other) => panic!("unknown command helper mode {other}"),
2089 Err(_) => {}
2090 }
2091 }
2092
2093 fn spec(kind: AgentKind, model: Option<&str>) -> AgentSpec {
2094 AgentSpec {
2095 id: "a".to_owned(),
2096 kind,
2097 model: model.map(str::to_owned),
2098 command: vec!["echo".to_owned(), "{label}".to_owned()],
2099 extra_args: Vec::new(),
2100 env: BTreeMap::new(),
2101 prompt_delivery: None,
2102 max_turn_secs: None,
2103 }
2104 }
2105
2106 fn inv<'a>(cwd: &'a Path, art: &'a Path, allow_write: bool) -> Invocation<'a> {
2107 Invocation {
2108 coordination: None,
2109 fetched_base: None,
2110 cwd,
2111 prompt: "do the thing",
2112 timeout: Duration::from_secs(900),
2113 allow_write,
2114 sessions: true,
2115 artifacts: art,
2116 stem: "t",
2117 run: "test-run",
2118 node: "test",
2119 cache_dir: None,
2120 attachments: &[],
2121 writable: &[],
2122 }
2123 }
2124
2125 fn plan_for(kind: AgentKind, seat: &SeatState, allow_write: bool) -> Plan {
2126 build_command(
2127 &spec(kind, None),
2128 seat,
2129 &inv(Path::new("."), Path::new("/art"), allow_write),
2130 Path::new("/art/p.md"),
2131 )
2132 .unwrap()
2133 }
2134
2135 #[test]
2136 fn editing_access_requires_fetch_evidence_and_keeps_snapshot() {
2137 let missing = editing_access_note(None);
2138 assert!(missing.contains("No successful launcher fetch evidence"));
2139 let base = crate::run::FetchedBase {
2140 remote: "origin".into(),
2141 branch: "main".into(),
2142 commit: "0123456789abcdef".into(),
2143 fetched_at: "2026-10-18T00:00:00Z".parse().unwrap(),
2144 };
2145 let note = editing_access_note(Some(&base));
2146 for expected in [
2147 "origin/main",
2148 "0123456789abcdef",
2149 "2026-10-18T00:00:00Z",
2150 "Do not fetch again",
2151 ] {
2152 assert!(note.contains(expected), "missing {expected}");
2153 }
2154 for text in [&missing, ¬e] {
2157 assert!(!text.to_lowercase().contains("sandbox"), "{text}");
2158 assert!(!text.contains("Network access is restricted"), "{text}");
2159 }
2160 let roundtrip: crate::run::FetchedBase =
2161 serde_json::from_str(&serde_json::to_string(&base).unwrap()).unwrap();
2162 assert_eq!(editing_access_note(Some(&roundtrip)), note);
2163 }
2164
2165 #[test]
2166 fn codex_editing_seats_bypass_the_sandbox_on_initial_and_resume() {
2167 const BYPASS: &str = "--dangerously-bypass-approvals-and-sandbox";
2168 let roots = [PathBuf::from("/shared/git"), PathBuf::from("/questions")];
2169 for resumed in [false, true] {
2170 let mut seat = SeatState::new("impl-A", "a", 7);
2171 if resumed {
2172 seat.turns = 1;
2173 seat.captured_session = Some("fixture-thread".into());
2174 }
2175 let build = |invocation: &Invocation<'_>| {
2176 build_command(
2177 &spec(AgentKind::Codex, None),
2178 &seat,
2179 invocation,
2180 Path::new("/art/p.md"),
2181 )
2182 .unwrap()
2183 };
2184 for node in [
2185 "implement",
2186 "fix",
2187 "graft",
2188 "gate-fix",
2189 "land",
2190 "rebase",
2191 "base-conflict",
2192 crate::queue::CHAT_NODE,
2193 "external-chat",
2194 ] {
2195 let mut invocation = inv(Path::new("."), Path::new("/art"), true);
2196 invocation.node = node;
2197 invocation.writable = &roots;
2198 let plan = build(&invocation);
2199 let at = plan.argv.iter().position(|s| s == BYPASS);
2200 assert!(at.is_some(), "{node}: {:?}", plan.argv);
2201 if resumed {
2202 let resume = plan.argv.iter().position(|s| s == "resume").unwrap();
2203 assert!(at.unwrap() < resume, "{node}: {:?}", plan.argv);
2204 }
2205 assert!(!plan.argv.iter().any(|s| s == "--sandbox"), "{node}");
2207 assert!(!plan.argv.iter().any(|s| s == "-c"), "{node}");
2208 }
2209 for (node, write) in [
2210 ("review", false),
2211 ("judge", false),
2212 ("advise", false),
2213 ("implement", false),
2214 ("fix", false),
2215 ("dupes", false),
2216 ("conduct", false),
2217 ("waiter", false),
2218 ("talk-title", false),
2219 (crate::queue::CHAT_NODE, false),
2220 ("external-chat", false),
2221 ("deputy", true),
2222 ] {
2223 let mut invocation = inv(Path::new("."), Path::new("/art"), write);
2224 invocation.node = node;
2225 let plan = build(&invocation);
2226 assert!(!plan.argv.iter().any(|s| s == BYPASS), "{node}");
2227 let mode = if write {
2228 "workspace-write"
2229 } else {
2230 "read-only"
2231 };
2232 assert!(
2233 plan.argv.windows(2).any(|w| w == ["--sandbox", mode]),
2234 "{node}: {:?}",
2235 plan.argv
2236 );
2237 assert!(
2238 plan.argv
2239 .windows(2)
2240 .any(|w| w == ["-c", "approval_policy=\"never\""]),
2241 "{node}"
2242 );
2243 }
2244 }
2245 }
2246
2247 #[test]
2248 fn claude_mints_then_resumes_the_same_uuid() {
2249 let mut seat = SeatState::new("judge-1", "a", 7);
2250 let uuid = seat.claude_session.clone().unwrap();
2251 let first = plan_for(AgentKind::Claude, &seat, true);
2252 assert!(first.argv.windows(2).any(|w| w == ["--session-id", &uuid]));
2253 assert!(!first.argv.iter().any(|a| a == "--resume"));
2254
2255 seat.turns = 1;
2256 let second = plan_for(AgentKind::Claude, &seat, true);
2257 assert!(second.argv.windows(2).any(|w| w == ["--resume", &uuid]));
2258 assert!(!second.argv.iter().any(|a| a == "--session-id"));
2259 }
2260
2261 #[test]
2262 fn github_bound_claude_seats_override_the_operators_answer_language() {
2263 let seat = SeatState::new("change-summary", "a", 7);
2264 let art = Path::new("/art");
2265 for (language, name) in [("en", "English"), ("", "English"), ("ja", "Japanese")] {
2266 let claude =
2267 for_github_text(&spec(AgentKind::Claude, None), language, Path::new(".")).unwrap();
2268 let plan = build_command(
2269 &claude,
2270 &seat,
2271 &inv(Path::new("."), art, false),
2272 Path::new("/art/p.md"),
2273 )
2274 .unwrap();
2275 let settings = format!(r#"{{"language":"{name}"}}"#);
2276 assert!(
2277 plan.argv
2278 .windows(2)
2279 .any(|w| w == ["--settings", settings.as_str()]),
2280 "{:?}",
2281 plan.argv
2282 );
2283 assert_eq!(plan.stdin.as_deref(), Some("do the thing"));
2286 }
2287 for kind in [
2288 AgentKind::Codex,
2289 AgentKind::Opencode,
2290 AgentKind::Antigravity,
2291 AgentKind::Omp,
2292 ] {
2293 assert!(
2294 for_github_text(&spec(kind, None), "en", Path::new("."))
2295 .unwrap()
2296 .extra_args
2297 .is_empty()
2298 );
2299 }
2300 }
2301
2302 #[test]
2303 fn github_text_preserves_existing_settings() {
2304 let dir = tempfile::tempdir().unwrap();
2305 let original = serde_json::json!({
2306 "language": "Japanese",
2307 "env": { "SEAT_MODE": "configured" },
2308 "apiKeyHelper": "credential-helper",
2309 "hooks": { "Stop": [] }
2310 });
2311 std::fs::write(dir.path().join("seat-settings.json"), original.to_string()).unwrap();
2312 for value in [original.to_string(), "./seat-settings.json".to_owned()] {
2313 for equals in [false, true] {
2314 let mut spec = spec(AgentKind::Claude, None);
2315 spec.extra_args = vec!["--settings={\"language\":\"French\"}".to_owned()];
2317 if equals {
2318 spec.extra_args.push(format!("--settings={value}"));
2319 } else {
2320 spec.extra_args
2321 .extend(["--settings".to_owned(), value.clone()]);
2322 }
2323 spec.extra_args.push("--verbose".to_owned());
2324 let updated = for_github_text(&spec, "en", dir.path()).unwrap();
2325 assert_eq!(updated.extra_args.len(), spec.extra_args.len());
2326 assert_eq!(updated.extra_args[0], spec.extra_args[0]);
2327 assert_eq!(updated.extra_args.last(), spec.extra_args.last());
2328 let value = &updated.extra_args[updated.extra_args.len() - 2];
2329 let actual: serde_json::Value =
2330 serde_json::from_str(value.strip_prefix("--settings=").unwrap_or(value))
2331 .unwrap();
2332 let mut expected = original.clone();
2333 expected["language"] = serde_json::json!("English");
2334 assert_eq!(actual, expected);
2335 }
2336 }
2337 let on_disk: serde_json::Value = serde_json::from_str(
2338 &std::fs::read_to_string(dir.path().join("seat-settings.json")).unwrap(),
2339 )
2340 .unwrap();
2341 assert_eq!(on_disk, original);
2342 }
2343
2344 #[test]
2345 fn github_text_settings_are_redacted_from_command_logging() {
2346 let dir = tempfile::tempdir().unwrap();
2347 let settings = serde_json::json!({ "env": { "AUTH_TOKEN": "test-only-secret" } });
2348 std::fs::write(dir.path().join("seat-settings.json"), settings.to_string()).unwrap();
2349 for equals in [false, true] {
2350 let mut spec = spec(AgentKind::Claude, None);
2351 spec.extra_args =
2352 vec!["--settings={\"env\":{\"AUTH_TOKEN\":\"earlier-secret\"}}".to_owned()];
2353 if equals {
2354 spec.extra_args
2355 .push("--settings=./seat-settings.json".to_owned());
2356 } else {
2357 spec.extra_args
2358 .extend(["--settings".to_owned(), "./seat-settings.json".to_owned()]);
2359 }
2360 let spec = for_github_text(&spec, "en", dir.path()).unwrap();
2361 let plan = build_command(
2362 &spec,
2363 &SeatState::new("change-summary", "a", 7),
2364 &inv(dir.path(), dir.path(), false),
2365 &dir.path().join("prompt.md"),
2366 )
2367 .unwrap();
2368 let actual = plan.argv.last().unwrap();
2370 let actual: serde_json::Value =
2371 serde_json::from_str(actual.strip_prefix("--settings=").unwrap_or(actual)).unwrap();
2372 assert_eq!(actual["env"]["AUTH_TOKEN"], "test-only-secret");
2373 assert_eq!(actual["language"], "English");
2374 let logged = format!("{:?}", plan.logged_argv());
2375 assert!(!logged.contains("secret"));
2376 assert!(!logged.contains("AUTH_TOKEN"));
2377 assert!(logged.contains("[redacted]"));
2378 assert_eq!(plan.logged_argv()[0], "claude");
2379 assert!(plan.logged_argv().contains(&"--output-format"));
2380 }
2381 }
2382
2383 #[test]
2384 fn github_text_rejects_unreadable_or_invalid_settings() {
2385 let dir = tempfile::tempdir().unwrap();
2386 std::fs::write(dir.path().join("bad.json"), "invalid json").unwrap();
2387 std::fs::write(dir.path().join("array.json"), "[]").unwrap();
2388 for args in [
2389 vec!["--settings"],
2390 vec!["--settings", "missing.json"],
2391 vec!["--settings=bad.json"],
2392 vec!["--settings", "array.json"],
2393 ] {
2394 let mut spec = spec(AgentKind::Claude, None);
2395 spec.extra_args = args.into_iter().map(str::to_owned).collect();
2396 assert!(for_github_text(&spec, "en", dir.path()).is_err());
2397 }
2398 }
2399
2400 #[test]
2401 fn read_only_seats_cannot_edit() {
2402 let seat = SeatState::new("judge-1", "a", 7);
2403 let claude = plan_for(AgentKind::Claude, &seat, false);
2404 assert!(claude.argv.iter().any(|a| a == "--disallowed-tools"));
2405 assert!(
2406 !plan_for(AgentKind::Claude, &seat, true)
2407 .argv
2408 .iter()
2409 .any(|a| a == "--disallowed-tools")
2410 );
2411
2412 let agy = plan_for(AgentKind::Antigravity, &seat, false);
2413 assert!(agy.argv.windows(2).any(|w| w == ["--mode", "plan"]));
2414 assert!(
2415 !agy.argv
2416 .iter()
2417 .any(|a| a == "--dangerously-skip-permissions")
2418 );
2419 let agy_rw = plan_for(AgentKind::Antigravity, &seat, true);
2420 assert!(
2421 agy_rw
2422 .argv
2423 .windows(2)
2424 .any(|w| w == ["--mode", "accept-edits"])
2425 );
2426 assert!(
2427 agy_rw
2428 .argv
2429 .iter()
2430 .any(|a| a == "--dangerously-skip-permissions")
2431 );
2432 let agy_prompt = agy_rw
2437 .argv
2438 .iter()
2439 .position(|a| a == "-p")
2440 .map(|i| agy_rw.argv[i + 1].clone())
2441 .expect("agy takes its prompt with -p");
2442 assert!(
2443 agy_prompt.starts_with('@'),
2444 "agy must get a file reference, got {agy_prompt:?}"
2445 );
2446 assert!(
2447 !agy_prompt.contains("Read the file at"),
2448 "the prose pointer is for CLIs with no file syntax"
2449 );
2450
2451 for allow_write in [false, true] {
2456 assert!(
2457 plan_for(AgentKind::Opencode, &seat, allow_write)
2458 .argv
2459 .iter()
2460 .any(|a| a == "--auto"),
2461 "opencode needs --auto even to read (allow_write = {allow_write})"
2462 );
2463 }
2464 }
2465
2466 #[test]
2469 fn codex_gets_extra_writable_roots_only_when_it_may_write() {
2470 let seat = SeatState::new("deputy-x", "a", 7);
2471 let roots = [PathBuf::from("/data/questions")];
2472 let mk = |allow_write: bool| {
2473 let mut i = inv(Path::new("."), Path::new("/art"), allow_write);
2474 i.writable = &roots;
2475 build_command(
2476 &spec(AgentKind::Codex, None),
2477 &seat,
2478 &i,
2479 Path::new("/art/p.md"),
2480 )
2481 .unwrap()
2482 };
2483 let want = "sandbox_workspace_write.writable_roots=[\"/data/questions\"]";
2484 assert!(mk(true).argv.windows(2).any(|w| w == ["-c", want]));
2485 assert!(!mk(false).argv.iter().any(|a| a.contains("writable_roots")));
2486 }
2487
2488 #[test]
2489 fn repository_write_scope_covers_editing_nodes_without_changing_other_roles() {
2490 let mut i = inv(Path::new("."), Path::new("/art"), true);
2491 for node in [
2492 "implement",
2493 "fix",
2494 "graft",
2495 "gate-fix",
2496 "land",
2497 "rebase",
2498 "base-conflict",
2499 "chat",
2500 "external-chat",
2501 ] {
2502 i.node = node;
2503 assert!(repository_writer(&i), "{node}");
2504 i.allow_write = false;
2505 assert!(!repository_writer(&i), "{node}");
2506 i.allow_write = true;
2507 }
2508 for node in ["deputy", "judge", "review", "bump", "conduct", "talk-title"] {
2509 i.node = node;
2510 assert!(!repository_writer(&i), "{node}");
2511 }
2512 i.node = "chat";
2513 let roots = [PathBuf::from("/questions")];
2514 i.writable = &roots;
2515 assert!(
2516 repository_writer(&i),
2517 "explicit roots do not change the role"
2518 );
2519 assert!(
2520 !repository_seat(&i),
2521 "chat turns bypass repository scheduling"
2522 );
2523 }
2524
2525 #[test]
2526 fn coordination_classification_is_independent_of_write_grants() {
2527 let roots = [PathBuf::from("/questions")];
2528 for node in [
2529 crate::queue::CHAT_NODE,
2530 "external-chat",
2531 "implement",
2532 "fix",
2533 "graft",
2534 "gate-fix",
2535 "land",
2536 "rebase",
2537 "base-conflict",
2538 "review",
2539 "judge",
2540 "advise",
2541 "deputy",
2542 "waiter",
2543 "conduct",
2544 "dupes",
2545 "talk-title",
2546 ] {
2547 for allow_write in [false, true] {
2548 for writable in [&[][..], &roots[..]] {
2549 let mut i = inv(Path::new("."), Path::new("/art"), allow_write);
2550 i.node = node;
2551 i.writable = writable;
2552 assert_eq!(
2553 repository_seat(&i),
2554 !matches!(
2555 node,
2556 crate::queue::CHAT_NODE
2557 | "external-chat"
2558 | "talk-title"
2559 | "deputy"
2560 | "conduct"
2561 | "dupes"
2562 ),
2563 "{node}, allow_write={allow_write}, writable={writable:?}"
2564 );
2565 if matches!(node, crate::queue::CHAT_NODE | "external-chat") {
2566 assert_eq!(repository_writer(&i), allow_write);
2567 }
2568 }
2569 }
2570 }
2571 }
2572
2573 #[test]
2574 fn writable_directories_reach_initial_and_resumed_adapter_commands() {
2575 let roots = [
2576 PathBuf::from("/shared/git"),
2577 PathBuf::from("/state/questions"),
2578 ];
2579 for kind in [AgentKind::Codex, AgentKind::Antigravity] {
2580 for resumed in [false, true] {
2581 let mut seat = SeatState::new("impl-A", "a", 7);
2582 if resumed {
2583 seat.turns = 1;
2584 seat.captured_session = Some("session-id".into());
2585 }
2586 for allow_write in [false, true] {
2587 let mut i = inv(Path::new("."), Path::new("/art"), allow_write);
2588 i.writable = &roots;
2589 let plan = build_command(&spec(kind, None), &seat, &i, Path::new("/art/p.md"))
2590 .unwrap();
2591 for root in &roots {
2592 if kind == AgentKind::Antigravity {
2593 assert_eq!(
2594 plan.argv.windows(2).any(|w| {
2595 w[0] == "--add-dir" && w[1] == root.to_str().unwrap()
2596 }),
2597 allow_write,
2598 "resumed={resumed}: {:?}",
2599 plan.argv
2600 );
2601 }
2602 assert_eq!(
2603 plan.argv
2604 .iter()
2605 .any(|arg| arg.contains(root.to_str().unwrap())),
2606 allow_write,
2607 "{kind:?}, resumed={resumed}: {:?}",
2608 plan.argv
2609 );
2610 }
2611 if kind == AgentKind::Codex && resumed {
2612 let at = plan.argv.iter().position(|s| s == "resume").unwrap();
2613 assert!(!plan.argv[at..].iter().any(|s| s == "-c"));
2614 }
2615 }
2616 }
2617 }
2618 }
2619
2620 #[test]
2621 fn codex_is_sandboxed_reads_stdin_and_puts_resume_last() {
2622 let mut seat = SeatState::new("judge-1", "a", 7);
2623
2624 let ro = plan_for(AgentKind::Codex, &seat, false);
2630 assert!(ro.argv.windows(2).any(|w| w == ["--sandbox", "read-only"]));
2631 let rw = plan_for(AgentKind::Codex, &seat, true);
2632 assert!(
2633 rw.argv
2634 .windows(2)
2635 .any(|w| w == ["--sandbox", "workspace-write"])
2636 );
2637 for p in [&ro, &rw] {
2638 assert!(
2639 !p.argv
2640 .iter()
2641 .any(|a| a == "--dangerously-bypass-approvals-and-sandbox"),
2642 "the bypass defeats the only enforced read-only mode we have"
2643 );
2644 assert!(
2646 p.argv
2647 .windows(2)
2648 .any(|w| w == ["-c", "approval_policy=\"never\""]),
2649 "an unattended seat that asks for approval blocks until timeout"
2650 );
2651 }
2652
2653 assert_eq!(ro.stdin.as_deref(), Some("do the thing"));
2655 assert_eq!(
2656 ro.argv.last().map(String::as_str),
2657 Some("-"),
2658 "without the `-` argument codex waits for a prompt it never gets"
2659 );
2660
2661 seat.turns = 1;
2665 assert!(!has_session(AgentKind::Codex, &seat, true));
2666 assert!(
2667 !plan_for(AgentKind::Codex, &seat, true)
2668 .argv
2669 .iter()
2670 .any(|a| a == "resume")
2671 );
2672 seat.captured_session = Some("01a07440-4545-7492-85c1-024e3259a90a".to_owned());
2673 let resumed = plan_for(AgentKind::Codex, &seat, true);
2674 let at = resumed
2675 .argv
2676 .iter()
2677 .position(|a| a == "resume")
2678 .expect("resumes by subcommand");
2679 assert_eq!(resumed.argv[at + 1], "01a07440-4545-7492-85c1-024e3259a90a");
2680 assert!(
2681 resumed.argv[..at].iter().any(|a| a == "--sandbox"),
2682 "every option precedes the subcommand"
2683 );
2684 assert_eq!(resumed.argv.last().map(String::as_str), Some("-"));
2685 }
2686
2687 #[test]
2690 fn omp_reads_stdin_auto_approves_and_resumes_by_id() {
2691 let mut seat = SeatState::new("review-1", "a", 7);
2692
2693 let first = plan_for(AgentKind::Omp, &seat, false);
2697 assert!(first.argv.iter().any(|a| a == "-p"));
2698 assert!(first.argv.iter().any(|a| a == "--mode=json"));
2699 assert_eq!(first.stdin.as_deref(), Some("do the thing"));
2700 assert!(
2701 !first.argv.iter().any(|a| a == "do the thing"),
2702 "the prompt reached argv, where Windows caps it"
2703 );
2704
2705 for allow_write in [false, true] {
2711 let p = plan_for(AgentKind::Omp, &seat, allow_write);
2712 assert!(
2713 p.argv.iter().any(|a| a == "--auto-approve"),
2714 "omp needs --auto-approve even to read (allow_write = {allow_write})"
2715 );
2716 assert!(
2717 !p.argv
2718 .iter()
2719 .any(|a| a == "--dangerously-bypass-approvals-and-sandbox"),
2720 "nothing ever asks for the bypass"
2721 );
2722 }
2723
2724 seat.turns = 1;
2727 assert!(!has_session(AgentKind::Omp, &seat, true));
2728 assert!(
2729 !plan_for(AgentKind::Omp, &seat, true)
2730 .argv
2731 .iter()
2732 .any(|a| a == "--resume")
2733 );
2734 seat.captured_session = Some("01a09fe9-4e31-7226-85b3-fda6f46689d5".to_owned());
2735 let resumed = plan_for(AgentKind::Omp, &seat, true);
2736 assert!(
2737 resumed
2738 .argv
2739 .windows(2)
2740 .any(|w| w == ["--resume", "01a09fe9-4e31-7226-85b3-fda6f46689d5"]),
2741 "a captured id is what makes the next turn a resume"
2742 );
2743 assert!(!resumed.argv.iter().any(|a| a == "--continue"));
2746 assert_eq!(resumed.stdin.as_deref(), Some("do the thing"));
2748 }
2749
2750 #[test]
2755 fn omp_takes_the_answer_without_an_agent_end_line() {
2756 let stream = concat!(
2757 r#"{"type":"session","version":3,"id":"01a09fe9-4e31-7226-85b3-fda6f46689d5","cwd":"C:\\w"}"#,
2758 "\n",
2759 r#"{"type":"agent_start"}"#,
2760 "\n",
2761 r#"{"type":"turn_start"}"#,
2762 "\n",
2763 r#"{"type":"message_update","assistantMessageEvent":{"type":"text_delta","contentIndex":1,"delta":"."}}"#,
2764 "\n",
2765 r#"{"type":"message_end","message":{"role":"assistant","content":[{"type":"thinking","thinking":"checking"},{"type":"text","text":"."}]}}"#,
2766 "\n",
2767 r#"{"type":"turn_end","message":{"role":"assistant","content":[{"type":"thinking","thinking":"done"},{"type":"text","text":"{\"vote\":\"approve\"}"}]}}"#,
2768 "\n",
2769 );
2770 let out = extract(AgentKind::Omp, stream);
2771 assert_eq!(
2772 out.text, "{\"vote\":\"approve\"}",
2773 "the last assistant text block is the answer even with no agent_end"
2774 );
2775 assert_eq!(
2776 out.session.as_deref(),
2777 Some("01a09fe9-4e31-7226-85b3-fda6f46689d5")
2778 );
2779 }
2780
2781 #[test]
2785 fn omp_walks_agent_end_and_ignores_tool_loop_narration() {
2786 let stream = concat!(
2787 r#"{"type":"session","version":3,"id":"s1"}"#,
2788 "\n",
2789 "{\"type\":\"agent_end\",\"messages\":[{\"role\":\"user\",\"content\":[{\"type\":\"text\",\"text\":\"review this\"}]},{\"role\":\"assistant\",\"content\":[{\"type\":\"text\",\"text\":\"Looking at the diff…\"}]},{\"role\":\"assistant\",\"content\":[{\"type\":\"thinking\",\"thinking\":\"…\"},{\"type\":\"text\",\"text\":\"## 判定\\n\\n問題ありません。\"}]}]}",
2790 "\n",
2791 );
2792 let out = extract(AgentKind::Omp, stream);
2793 assert_eq!(
2794 out.text, "## 判定\n\n問題ありません。",
2795 "the narration is not the answer, and non-ASCII survives intact"
2796 );
2797 assert_eq!(out.session.as_deref(), Some("s1"));
2798 }
2799
2800 #[test]
2803 fn omp_skips_non_json_lines() {
2804 let stream = concat!(
2805 "Warning: some omp notice\n",
2806 r#"{"type":"session","version":3,"id":"s2"}"#,
2807 "\n",
2808 r#"{"type":"message_end","message":{"role":"assistant","content":[{"type":"text","text":"the answer"}]}}"#,
2809 "\n",
2810 "trailing junk",
2811 "\n",
2812 );
2813 let out = extract(AgentKind::Omp, stream);
2814 assert_eq!(out.text, "the answer");
2815 assert_eq!(out.session.as_deref(), Some("s2"));
2816 }
2817
2818 #[test]
2820 fn codex_takes_the_last_agent_message_and_the_thread_id() {
2821 let stream = concat!(
2822 "2026-09-06T01:05:49.394445Z ERROR codex_models_manager: failed to load models cache\n",
2823 r#"{"type":"thread.started","thread_id":"01a07440-4545-7492-85c1-024e3259a90a"}"#,
2824 "\n",
2825 r#"{"type":"turn.started"}"#,
2826 "\n",
2827 r#"{"type":"item.completed","item":{"id":"item_0","type":"agent_message","text":"Looking into it."}}"#,
2828 "\n",
2829 r#"{"type":"item.completed","item":{"id":"item_1","type":"command_execution","text":"cargo test"}}"#,
2830 "\n",
2831 r#"{"type":"item.completed","item":{"id":"item_2","type":"agent_message","text":"{\"verdict\": \"ok\"}"}}"#,
2832 "\n",
2833 r#"{"type":"turn.completed","usage":{"input_tokens":17137}}"#,
2834 "\n",
2835 );
2836 let out = extract(AgentKind::Codex, stream);
2837 assert_eq!(
2838 out.text, "{\"verdict\": \"ok\"}",
2839 "the last agent message is the answer; earlier ones narrate"
2840 );
2841 assert_eq!(
2842 out.session.as_deref(),
2843 Some("01a07440-4545-7492-85c1-024e3259a90a")
2844 );
2845 assert_eq!(out.status.as_deref(), Some("success"));
2846
2847 let failed = concat!(
2848 r#"{"type":"thread.started","thread_id":"t1"}"#,
2849 "\n",
2850 r#"{"type":"turn.failed","error":{"message":"nope"}}"#,
2851 "\n",
2852 );
2853 assert_eq!(
2854 extract(AgentKind::Codex, failed).status.as_deref(),
2855 Some("error")
2856 );
2857 }
2858
2859 #[test]
2860 fn captured_sessions_resume_only_once_reported() {
2861 let mut seat = SeatState::new("impl-A", "a", 7);
2862 seat.turns = 1;
2863 for kind in [AgentKind::Opencode, AgentKind::Antigravity] {
2864 assert!(!has_session(kind, &seat, true));
2865 let p = plan_for(kind, &seat, true);
2866 assert!(!p.argv.iter().any(|a| a == "-s" || a == "--conversation"));
2867 }
2868
2869 seat.captured_session = Some("sid".to_owned());
2870 assert!(has_session(AgentKind::Opencode, &seat, true));
2871 assert!(
2872 plan_for(AgentKind::Opencode, &seat, true)
2873 .argv
2874 .windows(2)
2875 .any(|w| w == ["-s", "sid"])
2876 );
2877 assert!(
2878 plan_for(AgentKind::Antigravity, &seat, true)
2879 .argv
2880 .windows(2)
2881 .any(|w| w == ["--conversation", "sid"])
2882 );
2883 }
2884
2885 #[test]
2886 fn sessions_disabled_never_resumes() {
2887 let mut seat = SeatState::new("impl-A", "a", 7);
2888 seat.turns = 3;
2889 seat.captured_session = Some("sid".to_owned());
2890 for kind in [
2891 AgentKind::Claude,
2892 AgentKind::Opencode,
2893 AgentKind::Antigravity,
2894 ] {
2895 assert!(!has_session(kind, &seat, false));
2896 }
2897 }
2898
2899 #[test]
2900 fn long_prompts_never_reach_argv_for_file_delivery_clis() {
2901 let seat = SeatState::new("judge-1", "a", 7);
2902 for kind in [AgentKind::Opencode, AgentKind::Antigravity] {
2903 let p = plan_for(kind, &seat, false);
2904 assert!(
2905 p.argv.iter().all(|a| a != "do the thing"),
2906 "{kind:?} put the prompt on the command line"
2907 );
2908 assert!(p.argv.iter().any(|a| a.contains("/art/p.md")));
2909 }
2910 let p = plan_for(AgentKind::Antigravity, &seat, false);
2912 let at = p.argv.iter().position(|a| a == "-p").unwrap();
2913 assert!(p.argv.get(at + 1).is_some_and(|v| v.contains("p.md")));
2914 assert!(p.stdin.is_none());
2915 }
2916
2917 #[test]
2918 fn agy_print_timeout_tracks_the_node_budget() {
2919 let seat = SeatState::new("impl-A", "a", 7);
2920 let p = build_command(
2921 &spec(AgentKind::Antigravity, None),
2922 &seat,
2923 &Invocation {
2924 coordination: None,
2925 fetched_base: None,
2926 cwd: Path::new("."),
2927 prompt: "p",
2928 timeout: Duration::from_secs(3600),
2929 allow_write: true,
2930 sessions: true,
2931 artifacts: Path::new("/art"),
2932 stem: "t",
2933 run: "test-run",
2934 node: "test",
2935 cache_dir: None,
2936 attachments: &[],
2937 writable: &[],
2938 },
2939 Path::new("/art/p.md"),
2940 )
2941 .unwrap();
2942 assert!(p.argv.windows(2).any(|w| w == ["--print-timeout", "3600s"]));
2943 }
2944
2945 #[test]
2952 fn attachments_widen_antigravitys_add_dir_even_off_file_delivery() {
2953 let mut s = spec(AgentKind::Antigravity, None);
2954 s.prompt_delivery = Some(Delivery::Argv);
2955 let seat = SeatState::new("talk", "a", 7);
2956 let atts = [PathBuf::from("/art/attachments/abc.png")];
2957
2958 let without = build_command(
2959 &s,
2960 &seat,
2961 &Invocation {
2962 attachments: &[],
2963 writable: &[],
2964 ..inv(Path::new("."), Path::new("/art"), true)
2965 },
2966 Path::new("/art/p.md"),
2967 )
2968 .unwrap();
2969 assert!(
2970 !without.argv.iter().any(|a| a == "--add-dir"),
2971 "no attachment, no reason to widen the sandbox: {without:?}"
2972 );
2973
2974 let with = build_command(
2975 &s,
2976 &seat,
2977 &Invocation {
2978 attachments: &atts,
2979 writable: &[],
2980 ..inv(Path::new("."), Path::new("/art"), true)
2981 },
2982 Path::new("/art/p.md"),
2983 )
2984 .unwrap();
2985 assert!(
2986 with.argv.windows(2).any(|w| w == ["--add-dir", "/art"]),
2987 "an attachment outside cwd must widen the sandbox even off File delivery: {with:?}"
2988 );
2989 }
2990
2991 #[test]
2997 fn an_inherited_attachment_outside_this_conversations_artifacts_dir_gets_its_own_add_dir() {
2998 let seat = SeatState::new("plan", "a", 7);
2999 let atts = [
3000 PathBuf::from("/art/attachments/own.png"),
3001 PathBuf::from("/other-chat/attachments/inherited.png"),
3002 ];
3003
3004 let p = build_command(
3005 &spec(AgentKind::Antigravity, None),
3006 &seat,
3007 &Invocation {
3008 attachments: &atts,
3009 writable: &[],
3010 ..inv(Path::new("."), Path::new("/art"), true)
3011 },
3012 Path::new("/art/p.md"),
3013 )
3014 .unwrap();
3015
3016 assert!(
3017 p.argv.windows(2).any(|w| w == ["--add-dir", "/art"]),
3018 "this conversation's own artifacts dir must still be granted: {p:?}"
3019 );
3020 assert!(
3021 p.argv
3022 .windows(2)
3023 .any(|w| w == ["--add-dir", "/other-chat/attachments"]),
3024 "the inherited attachment's own directory must be granted too: {p:?}"
3025 );
3026 }
3027
3028 #[test]
3029 fn command_agents_get_placeholders_substituted() {
3030 let seat = SeatState::new("impl-A", "a", 7);
3031 let p = plan_for(AgentKind::Command, &seat, true);
3032 assert_eq!(p.argv[0], "echo");
3033 assert_eq!(p.argv[1], "impl-A");
3034 assert_eq!(p.stdin.as_deref(), Some("do the thing"));
3035 }
3036
3037 const EXPIRED_AUTH: &str = r#"{"is_error":true,"terminal_reason":"api_error","result":"Failed to authenticate: OAuth session expired and could not be refreshed"}"#;
3038 const CODEX_LIMIT: &str = r#"{"type":"error","message":"You've hit your usage limit. Visit https://chatgpt.com/settings/usage to purchase more credits or try again at Oct 27th, 2026 6:18 PM."}
3039{"type":"turn.failed","error":{"message":"You've hit your usage limit."}}"#;
3040
3041 #[test]
3042 fn observed_machine_failures_are_detected_conservatively() {
3043 let auth = extract(AgentKind::Claude, EXPIRED_AUTH).quota.unwrap();
3044 assert_eq!(auth.cause, QuotaCause::Authentication);
3045 assert_eq!(auth.reset, None);
3046 let limit = extract(AgentKind::Codex, CODEX_LIMIT).quota.unwrap();
3047 assert_eq!(limit.cause, QuotaCause::UsageLimit);
3048 assert_eq!(limit.reset.as_deref(), Some("Oct 27th, 2026 6:18 PM"));
3049 assert!(
3051 extract(AgentKind::Codex, &format!("{CODEX_LIMIT}\n{CODEX_LIMIT}"))
3052 .quota
3053 .is_some()
3054 );
3055 let without_reset = CODEX_LIMIT.replace(" or try again at Oct 27th, 2026 6:18 PM", "");
3056 assert!(
3057 extract(AgentKind::Codex, &without_reset)
3058 .quota
3059 .unwrap()
3060 .reset
3061 .is_none()
3062 );
3063 for text in [
3064 EXPIRED_AUTH.replace("true", "false"),
3065 EXPIRED_AUTH.replace("api_error", "unknown"),
3066 EXPIRED_AUTH.replace(
3067 "OAuth session expired and could not be refreshed",
3068 "unknown error",
3069 ),
3070 ] {
3071 assert!(extract(AgentKind::Claude, &text).quota.is_none());
3072 }
3073 assert!(
3074 extract(AgentKind::Codex, CODEX_LIMIT.lines().next().unwrap())
3075 .quota
3076 .is_none()
3077 );
3078 assert!(
3079 extract(
3080 AgentKind::Codex,
3081 &CODEX_LIMIT.replace("You've hit your usage limit.", "Something failed.")
3082 )
3083 .quota
3084 .is_none()
3085 );
3086 let quoted = serde_json::json!({"type":"item.completed","item":{"type":"agent_message","text":CODEX_LIMIT}}).to_string();
3087 assert!(extract(AgentKind::Codex, "ed).quota.is_none());
3088 assert!(extract(AgentKind::Codex, EXPIRED_AUTH).quota.is_none());
3089 assert!(
3090 extract(AgentKind::Antigravity, EXPIRED_AUTH)
3091 .quota
3092 .is_none()
3093 );
3094 let legacy: Quota = serde_json::from_str(r#"{"reset":null}"#).unwrap();
3095 assert_eq!(legacy.cause, QuotaCause::UsageLimit);
3096 }
3097
3098 #[test]
3099 fn claude_rate_limit_is_detected_and_reset_read_when_present() {
3100 let stdout = r#"{"is_error": true, "terminal_reason": "api_error",
3102 "result": "You've hit your session limit · resets 4:50am (Asia/Tokyo)",
3103 "session_id": "b8e928f1-754e-4bd3-86c5-0567763654e3"}"#;
3104 let out = extract(AgentKind::Claude, stdout);
3105 let quota = out.quota.as_ref().expect("rate limit must be detected");
3106 assert_eq!(
3107 quota.reset.as_deref(),
3108 Some("4:50am (Asia/Tokyo)"),
3109 "reset time read from the body"
3110 );
3111 }
3112
3113 #[test]
3114 fn claude_rate_limit_without_a_readable_reset_is_still_detected() {
3115 let out = extract(
3116 AgentKind::Claude,
3117 r#"{"is_error":true,"result":"session limit reached"}"#,
3118 );
3119 let quota = out.quota.expect("rate limit detected without a reset");
3120 assert!(quota.reset.is_none(), "unknown reset is kept as unknown");
3121 }
3122
3123 #[test]
3124 fn claude_structured_usage_limit_requires_the_error_and_exact_pair() {
3125 for kind in [AgentKind::Claude, AgentKind::Command] {
3126 for reply in [
3127 r#"{"is_error":true,"api_error_status":429,"api_error":"usage_limit_reached"}"#,
3128 r#"{"is_error":true,"api_error_status":"429","api_error":"usage_limit_reached"}"#,
3129 ] {
3130 assert!(extract(kind, reply).quota.is_some(), "{reply}");
3131 }
3132 for reply in [
3133 r#"{"is_error":false,"api_error_status":429,"api_error":"usage_limit_reached"}"#,
3134 r#"{"is_error":true,"api_error_status":500,"api_error":"usage_limit_reached"}"#,
3135 r#"{"is_error":true,"api_error_status":429,"api_error":"other_error"}"#,
3136 r#"{"is_error":false,"api_error_status":"429","api_error":"usage_limit_reached"}"#,
3137 r#"{"is_error":true,"api_error_status":"500","api_error":"usage_limit_reached"}"#,
3138 r#"{"is_error":true,"api_error_status":"429","api_error":"other_error"}"#,
3139 r#"{"is_error":true,"api_error":"usage_limit_reached"}"#,
3140 ] {
3141 assert!(extract(kind, reply).quota.is_none(), "{reply}");
3142 }
3143 }
3144 }
3145
3146 #[test]
3147 fn ordinary_failures_are_never_quota() {
3148 let claude_fail = extract(
3150 AgentKind::Claude,
3151 r#"{"is_error":true,"result":"account does not exist"}"#,
3152 );
3153 assert!(claude_fail.quota.is_none());
3154
3155 let cmd_fail = extract(AgentKind::Command, "boom");
3157 assert!(cmd_fail.quota.is_none());
3158
3159 let success = extract(
3161 AgentKind::Command,
3162 r#"{"is_error":false,"result":"session limit is fine"}"#,
3163 );
3164 assert!(success.quota.is_none());
3165 }
3166
3167 #[test]
3175 fn codex_command_execution_events_are_captured_alongside_the_final_message() {
3176 let stream = concat!(
3177 r#"{"type":"thread.started","thread_id":"t1"}"#,
3178 "\n",
3179 r#"{"type":"item.completed","item":{"id":"item49","type":"command_execution","command":["bash","-lc","cargo test --test graph_cached_gate"],"exit_code":1,"aggregated_output":"test result: 1 passed; 1 failed"}}"#,
3180 "\n",
3181 r#"{"type":"item.completed","item":{"id":"item52","type":"command_execution","command":["bash","-lc","cargo test --test graph_cached_gate a_single_test"],"exit_code":0,"aggregated_output":"test result: 1 passed; 0 failed"}}"#,
3182 "\n",
3183 r#"{"type":"item.completed","item":{"id":"item99","type":"agent_message","text":"Both tests in the target pass."}}"#,
3184 "\n",
3185 r#"{"type":"turn.completed"}"#,
3186 "\n",
3187 );
3188 let out = extract(AgentKind::Codex, stream);
3189 assert_eq!(out.text, "Both tests in the target pass.");
3190 assert_eq!(out.commands.len(), 2, "{:?}", out.commands);
3191
3192 let paired = &out.commands[0];
3193 assert_eq!(paired.id, "item49");
3194 assert_eq!(paired.exit_code, Some(1));
3195 assert!(paired.description.contains("graph_cached_gate"));
3196 assert!(paired.result_summary.contains("1 failed"));
3197
3198 let solo = &out.commands[1];
3199 assert_eq!(solo.exit_code, Some(0));
3200
3201 assert!(
3205 out.commands
3206 .iter()
3207 .any(|c| c.exit_code != Some(0) && c.description.contains("graph_cached_gate")),
3208 "a failed run of the actual target must still be visible: {:?}",
3209 out.commands
3210 );
3211 }
3212
3213 #[test]
3214 fn command_agent_can_carry_the_claude_quota_shape() {
3215 let out = extract(
3216 AgentKind::Command,
3217 r#"{"is_error":true,"result":"You've hit your session limit · resets 1:00am (UTC)"}"#,
3218 );
3219 assert!(
3220 out.quota.is_some(),
3221 "a wrapper emitting the claude shape counts as quota"
3222 );
3223 }
3224
3225 #[test]
3226 fn claude_json_result_is_extracted() {
3227 let out = extract(
3228 AgentKind::Claude,
3229 r#"{"result":"all done","session_id":"abc","is_error":false}"#,
3230 );
3231 assert_eq!(out.text, "all done");
3232 assert_eq!(out.session.as_deref(), Some("abc"));
3233 assert_eq!(out.status.as_deref(), Some("success"));
3234 }
3235
3236 #[test]
3251 fn a_clean_cli_turn_is_not_the_same_fact_as_the_nodes_own_work_being_done() {
3252 let stdout = r#"{"type":"result","subtype":"success","is_error":false,"terminal_reason":"completed","stop_reason":"end_turn","result":"I'll pause here until the `cargo make check` background run reports back.","session_id":"11111111-1111-1111-1111-111111111111"}"#;
3253 let out = extract(AgentKind::Claude, stdout);
3254 assert_eq!(out.status.as_deref(), Some("success"));
3255 assert!(out.quota.is_none());
3256 assert!(!out.text.trim().is_empty());
3257
3258 let agent_out = AgentOutput {
3259 text: out.text.clone(),
3260 exit_code: Some(0),
3261 timed_out: false,
3262 duration_ms: 500,
3263 artifacts: Vec::new(),
3264 quota: out.quota,
3265 dropped: out.dropped,
3266 commands: out.commands,
3267 context_tokens: None,
3268 };
3269 assert!(
3270 agent_out.usable(),
3271 "the CLI turn itself ended cleanly and must read as usable"
3272 );
3273 assert!(
3274 crate::verdict::extract_json::<crate::verdict::FixReport>(&agent_out.text).is_err(),
3275 "a clean CLI turn is not proof the node's own report ever arrived"
3276 );
3277 }
3278
3279 #[test]
3280 fn opencode_event_stream_is_concatenated() {
3281 let stream = concat!(
3282 r#"{"type":"step_start","sessionID":"ses_1","part":{"type":"step-start"}}"#,
3283 "\n",
3284 r#"{"type":"text","sessionID":"ses_1","part":{"type":"text","text":"first"}}"#,
3285 "\n",
3286 "garbage line\n",
3287 r#"{"type":"text","sessionID":"ses_1","part":{"type":"text","text":"second"}}"#,
3288 "\n"
3289 );
3290 let out = extract(AgentKind::Opencode, stream);
3291 assert_eq!(out.text, "first\nsecond");
3292 assert_eq!(out.session.as_deref(), Some("ses_1"));
3293 }
3294
3295 #[test]
3296 fn agy_json_survives_a_leading_warning_line() {
3297 let stdout = concat!(
3298 "warning: --mode plan has no effect while slash commands are disabled.\n",
3299 r#"{"conversation_id":"eaf2d00a","status":"SUCCESS","response":"persimmon\n"}"#,
3300 "\n"
3301 );
3302 let out = extract(AgentKind::Antigravity, stdout);
3303 assert_eq!(out.text, "persimmon");
3304 assert_eq!(out.session.as_deref(), Some("eaf2d00a"));
3305 assert_eq!(out.status.as_deref(), Some("SUCCESS"));
3306 }
3307
3308 const AGY_DROPPED: &str = concat!(
3315 r#"{"conversation_id":"36743d06-c0b3-4b79-9fa2-23869289d7b6","status":"ERROR","#,
3316 r#""response":"","error":"the connection to the agent was interrupted before "#,
3317 r#"the response finished: subscriber fell behind updates, stalled for 5s","#,
3318 r#""duration_seconds":431.1941803,"num_turns":1,"usage":{"input_tokens":260113,"#,
3319 r#""output_tokens":14267,"thinking_tokens":9695,"cache_read_tokens":2200925,"#,
3320 r#""total_tokens":274380}}"#
3321 );
3322
3323 const AGY_QUOTA_OUT: &str = concat!(
3325 r#"{"conversation_id":"323c3b5b-0000","status":"ERROR","response":"","#,
3326 r#""error":"Individual quota reached. Please upgrade your subscription to "#,
3327 r#"increase your limits. Resets in 1h2m49s.","duration_seconds":265.9,"#,
3328 r#""num_turns":2,"usage":{"input_tokens":1000,"output_tokens":50}}"#
3329 );
3330 const AGY_QUOTA_ERR: &str = concat!(
3331 "error: Individual quota reached. Resets in 1h2m49s.\n",
3332 r#"AGY_ERROR: {"short_error":"RESOURCE_EXHAUSTED (code 429): Individual quota "#,
3333 r#"reached.","status":"RESOURCE_EXHAUSTED","error_code":429,"code_kind":"http","#,
3334 r#""retryable":true}"#,
3335 "\n"
3336 );
3337
3338 #[test]
3339 fn agy_out_of_quota_is_a_quota_with_the_reset_hint() {
3340 let both = agy_quota(AGY_QUOTA_OUT, AGY_QUOTA_ERR).expect("both streams");
3341 assert_eq!(both.reset.as_deref(), Some("in 1h2m49s"));
3342 let stdout_only = agy_quota(AGY_QUOTA_OUT, "").expect("stdout alone");
3343 assert_eq!(stdout_only.reset.as_deref(), Some("in 1h2m49s"));
3344 let stderr_only = agy_quota("not json", AGY_QUOTA_ERR).expect("stderr alone");
3346 assert!(stderr_only.reset.is_none());
3347 }
3348
3349 #[test]
3350 fn ordinary_agy_failures_are_not_a_quota() {
3351 assert!(agy_quota(AGY_DROPPED, "").is_none());
3352 assert!(agy_quota(r#"{"status":"ERROR","error":"boom"}"#, "").is_none());
3353 assert!(
3354 agy_quota(
3355 "",
3356 r#"AGY_ERROR: {"status":"RESOURCE_EXHAUSTED","error_code":500}"#
3357 )
3358 .is_none()
3359 );
3360 assert!(
3361 agy_quota(
3362 "",
3363 r#"AGY_ERROR: {"status":"UNAVAILABLE","error_code":429}"#
3364 )
3365 .is_none()
3366 );
3367 assert!(agy_quota(r#"{"status":"SUCCESS","response":"ok"}"#, "").is_none());
3368 }
3369
3370 #[test]
3371 fn a_cli_that_hangs_up_on_billed_work_is_not_an_agent_that_produced_nothing() {
3372 let out = extract(AgentKind::Antigravity, AGY_DROPPED);
3373 let dropped = out.dropped.expect("recognised as undelivered work");
3374 assert_eq!(dropped.output_tokens, 14267);
3375 assert!(
3376 dropped.why.contains("subscriber fell behind"),
3377 "the CLI's own words are kept for the record: {}",
3378 dropped.why
3379 );
3380 assert_eq!(
3383 out.session.as_deref(),
3384 Some("36743d06-c0b3-4b79-9fa2-23869289d7b6")
3385 );
3386 assert!(out.quota.is_none(), "a dropped stream is not a rate limit");
3387 }
3388
3389 #[test]
3390 fn an_error_with_nothing_produced_stays_an_ordinary_failure() {
3391 let bare = r#"{"conversation_id":"c1","status":"ERROR","response":"","error":"boom"}"#;
3395 assert!(extract(AgentKind::Antigravity, bare).dropped.is_none());
3396
3397 let answered = concat!(
3400 r#"{"conversation_id":"c2","status":"ERROR","response":"here it is","#,
3401 r#""usage":{"output_tokens":10}}"#
3402 );
3403 assert!(extract(AgentKind::Antigravity, answered).dropped.is_none());
3404
3405 let ok = concat!(
3407 r#"{"conversation_id":"c3","status":"SUCCESS","response":"done","#,
3408 r#""usage":{"output_tokens":10}}"#
3409 );
3410 assert!(extract(AgentKind::Antigravity, ok).dropped.is_none());
3411 }
3412
3413 #[test]
3414 fn an_undelivered_output_is_not_usable_but_is_worth_asking_again() {
3415 let out = AgentOutput {
3416 text: String::new(),
3417 exit_code: Some(1),
3418 timed_out: false,
3419 duration_ms: 431_194,
3420 artifacts: Vec::new(),
3421 quota: None,
3422 dropped: Some(Dropped {
3423 why: "subscriber fell behind updates".to_owned(),
3424 output_tokens: 14267,
3425 }),
3426 commands: Vec::new(),
3427 context_tokens: None,
3428 };
3429 assert!(!out.usable());
3430 assert!(out.work_undelivered());
3431 assert!(!out.quota_exhausted());
3434 }
3435
3436 #[test]
3437 fn non_json_stdout_falls_back_to_raw_text() {
3438 let out = extract(AgentKind::Antigravity, "plain answer\n");
3439 assert_eq!(out.text, "plain answer");
3440 assert!(out.session.is_none());
3441 }
3442
3443 #[tokio::test]
3444 async fn command_agent_round_trip_writes_artifacts() {
3445 let dir = tempfile::tempdir().unwrap();
3446 let art = dir.path().join("artifacts");
3447 let mut seat = SeatState::new("impl-A", "a", 7);
3448 let s = command_helper("reply");
3449 let out = invoke(
3450 &s,
3451 &mut seat,
3452 &Invocation {
3453 coordination: None,
3454 fetched_base: None,
3455 cwd: dir.path(),
3456 prompt: "unused",
3457 timeout: Duration::from_secs(30),
3458 allow_write: true,
3459 sessions: true,
3460 artifacts: &art,
3461 stem: "impl-A",
3462 run: "test-run",
3463 node: "test",
3464 cache_dir: None,
3465 attachments: &[],
3466 writable: &[],
3467 },
3468 )
3469 .await
3470 .unwrap();
3471 assert!(out.usable(), "{out:?}");
3472 assert!(out.text.contains("hello impl-A"), "{}", out.text);
3473 assert_eq!(seat.turns, 1);
3474 assert!(art.join("impl-A.prompt.md").is_file());
3475 assert!(art.join("impl-A.out").is_file());
3476 }
3477
3478 #[tokio::test]
3479 async fn the_invocation_cache_dir_reaches_the_seat_as_cargo_target_dir() {
3480 crate::run::pin_test_home();
3481 let dir = tempfile::tempdir().unwrap();
3485 let cache = dir.path().join("magi-cache");
3486 let mut seat = SeatState::new("impl-A", "a", 7);
3487 let s = command_helper("cache");
3488 let out = invoke(
3489 &s,
3490 &mut seat,
3491 &Invocation {
3492 coordination: None,
3493 fetched_base: None,
3494 cwd: dir.path(),
3495 prompt: "unused",
3496 timeout: Duration::from_secs(30),
3497 allow_write: true,
3498 sessions: true,
3499 artifacts: &dir.path().join("artifacts"),
3500 stem: "cache",
3501 run: "test-run",
3502 node: "test",
3503 cache_dir: Some(&cache),
3504 attachments: &[],
3505 writable: &[],
3506 },
3507 )
3508 .await
3509 .unwrap();
3510 assert!(out.usable(), "{out:?}");
3511 assert!(
3512 out.text.contains(cache.to_string_lossy().as_ref()),
3513 "the seat must see CARGO_TARGET_DIR = the shared cache"
3514 );
3515 }
3516
3517 #[tokio::test]
3518 async fn cache_dir_none_strips_a_cargo_target_dir_inherited_from_this_process() {
3519 let previous = std::env::var("CARGO_TARGET_DIR").ok();
3527 unsafe {
3532 std::env::set_var("CARGO_TARGET_DIR", "/should/never/reach/a/read-only/seat");
3533 }
3534 let dir = tempfile::tempdir().unwrap();
3535 let mut seat = SeatState::new("review-1", "a", 7);
3536 let s = command_helper("no-cache");
3537 let result = invoke(
3538 &s,
3539 &mut seat,
3540 &Invocation {
3541 coordination: None,
3542 fetched_base: None,
3543 cwd: dir.path(),
3544 prompt: "unused",
3545 timeout: Duration::from_secs(30),
3546 allow_write: false,
3547 sessions: true,
3548 artifacts: &dir.path().join("artifacts"),
3549 stem: "no-cache",
3550 run: "test-run",
3551 node: "test",
3552 cache_dir: None,
3553 attachments: &[],
3554 writable: &[],
3555 },
3556 )
3557 .await;
3558 unsafe {
3563 match &previous {
3564 Some(v) => std::env::set_var("CARGO_TARGET_DIR", v),
3565 None => std::env::remove_var("CARGO_TARGET_DIR"),
3566 }
3567 }
3568 let out = result.unwrap();
3569 assert!(out.usable(), "{out:?}");
3570 assert!(
3571 out.text.contains("ABSENT"),
3572 "a read-only seat must never inherit the process's own CARGO_TARGET_DIR: {}",
3573 out.text
3574 );
3575 }
3576
3577 #[tokio::test]
3578 async fn a_prompt_larger_than_the_pipe_buffer_does_not_deadlock() {
3579 let dir = tempfile::tempdir().unwrap();
3580 let mut seat = SeatState::new("impl-A", "a", 7);
3581 let s = command_helper("ignore-stdin");
3584 let big = "x".repeat(1_000_000);
3585 let out = invoke(
3586 &s,
3587 &mut seat,
3588 &Invocation {
3589 coordination: None,
3590 fetched_base: None,
3591 cwd: dir.path(),
3592 prompt: &big,
3593 timeout: Duration::from_secs(60),
3594 allow_write: true,
3595 sessions: true,
3596 artifacts: &dir.path().join("artifacts"),
3597 stem: "big",
3598 run: "test-run",
3599 node: "test",
3600 cache_dir: None,
3601 attachments: &[],
3602 writable: &[],
3603 },
3604 )
3605 .await
3606 .unwrap();
3607 assert!(out.usable(), "{out:?}");
3608 assert!(out.text.contains("done"), "{}", out.text);
3609 }
3610
3611 #[tokio::test]
3612 async fn agent_turn_cap_limits_a_direct_invocation() {
3613 let dir = tempfile::tempdir().unwrap();
3614 let artifacts = dir.path().join("artifacts");
3615 let mut seat = SeatState::new("impl-A", "helper", 7);
3616 let mut s = command_helper("sleep");
3617 s.max_turn_secs = Some(2);
3618 let out = invoke(&s, &mut seat, &inv(dir.path(), &artifacts, false))
3619 .await
3620 .unwrap();
3621 assert!(out.timed_out, "{out:?}");
3622 assert!(out.duration_ms < 20_000, "{out:?}");
3625 }
3626
3627 #[tokio::test]
3628 async fn timeout_is_reported_not_hung() {
3629 let dir = tempfile::tempdir().unwrap();
3630 let mut seat = SeatState::new("impl-A", "a", 7);
3631 let s = command_helper("sleep");
3632 let out = invoke(
3633 &s,
3634 &mut seat,
3635 &Invocation {
3636 coordination: None,
3637 fetched_base: None,
3638 cwd: dir.path(),
3639 prompt: "unused",
3640 timeout: Duration::from_millis(300),
3641 allow_write: true,
3642 sessions: true,
3643 artifacts: &dir.path().join("artifacts"),
3644 stem: "slow",
3645 run: "test-run",
3646 node: "test",
3647 cache_dir: None,
3648 attachments: &[],
3649 writable: &[],
3650 },
3651 )
3652 .await
3653 .unwrap();
3654 assert!(out.timed_out);
3655 assert!(!out.usable());
3656 }
3657
3658 #[tokio::test]
3659 async fn a_timeout_keeps_what_the_agent_had_already_printed() {
3660 let dir = tempfile::tempdir().unwrap();
3666 let artifacts = dir.path().join("artifacts");
3667 let mut seat = SeatState::new("impl-A", "a", 7);
3668 let s = command_helper("chatty-sleep");
3669 let out = invoke(
3670 &s,
3671 &mut seat,
3672 &Invocation {
3673 coordination: None,
3674 fetched_base: None,
3675 cwd: dir.path(),
3676 prompt: "unused",
3677 timeout: Duration::from_secs(10),
3682 allow_write: true,
3683 sessions: true,
3684 artifacts: &artifacts,
3685 stem: "chatty",
3686 run: "test-run",
3687 node: "test",
3688 cache_dir: None,
3689 attachments: &[],
3690 writable: &[],
3691 },
3692 )
3693 .await
3694 .unwrap();
3695
3696 assert!(out.timed_out, "{out:?}");
3697 assert!(!out.usable(), "a cut-off answer is still not an answer");
3698 let recorded = std::fs::read_to_string(artifacts.join("chatty.out")).unwrap();
3699 assert!(
3700 recorded.contains("i-said-something"),
3701 "the artifact must keep what arrived before the kill, got {recorded:?}"
3702 );
3703 assert!(
3704 out.text.contains("i-said-something"),
3705 "and the graph must be able to see it too, got {:?}",
3706 out.text
3707 );
3708 }
3709
3710 #[test]
3711 fn missing_programs_reports_command_binaries() {
3712 let mut s = spec(AgentKind::Command, None);
3713 s.command = vec!["definitely-not-a-real-binary-xyz".to_owned()];
3714 assert_eq!(
3715 missing_programs(&[s]),
3716 ["definitely-not-a-real-binary-xyz".to_owned()]
3717 );
3718 }
3719
3720 fn pick_spec(id: &str, kind: AgentKind) -> AgentSpec {
3721 AgentSpec {
3722 id: id.to_owned(),
3723 kind,
3724 model: None,
3725 command: Vec::new(),
3726 extra_args: Vec::new(),
3727 env: BTreeMap::new(),
3728 prompt_delivery: None,
3729 max_turn_secs: None,
3730 }
3731 }
3732
3733 fn without<'a>(missing: &'a [&'a str]) -> impl Fn(&AgentSpec) -> bool + 'a {
3736 move |a: &AgentSpec| !missing.contains(&a.id.as_str())
3737 }
3738
3739 #[test]
3740 fn pick_prefers_the_claude_seat_even_when_it_is_not_first_in_the_roster() {
3741 let agents = [
3742 pick_spec("oc", AgentKind::Opencode),
3743 pick_spec("opus", AgentKind::Claude),
3744 pick_spec("agy", AgentKind::Antigravity),
3745 ];
3746 let got = pick(&agents, None, &without(&[])).expect("a pick");
3747 assert_eq!(got.id, "opus");
3748 }
3749
3750 #[test]
3751 fn pick_falls_back_to_the_first_installed_agent_in_roster_order() {
3752 let agents = [
3753 pick_spec("opus", AgentKind::Claude),
3754 pick_spec("oc", AgentKind::Opencode),
3755 pick_spec("agy", AgentKind::Antigravity),
3756 ];
3757 let got = pick(&agents, None, &without(&["opus", "oc"])).expect("a pick");
3758 assert_eq!(got.id, "agy");
3759 }
3760
3761 #[test]
3762 fn pick_on_an_empty_roster_says_what_to_install() {
3763 let msg = pick(&[], None, &without(&[]))
3764 .expect_err("nobody to ask")
3765 .to_string();
3766 assert!(msg.contains("roster is empty"), "{msg}");
3767 assert!(msg.contains("claude"), "{msg}");
3768 assert!(msg.contains("magi.toml"), "{msg}");
3769 }
3770
3771 #[test]
3772 fn pick_on_a_roster_with_nothing_installed_names_the_programs_that_are_missing() {
3773 let agents = [
3774 pick_spec("opus", AgentKind::Claude),
3775 pick_spec("oc", AgentKind::Opencode),
3776 ];
3777 let err = pick(&agents, None, &without(&["opus", "oc"])).expect_err("nothing runnable");
3778 let msg = format!("{err:#}");
3779 assert!(msg.contains("claude"), "{msg}");
3780 assert!(msg.contains("opencode"), "{msg}");
3781 }
3782
3783 #[test]
3784 fn an_explicitly_named_agent_wins_over_the_claude_preference() {
3785 let agents = [
3786 pick_spec("opus", AgentKind::Claude),
3787 pick_spec("oc", AgentKind::Opencode),
3788 ];
3789 let got = pick(&agents, Some("oc"), &without(&[])).expect("a pick");
3790 assert_eq!(got.id, "oc");
3791 }
3792
3793 #[test]
3794 fn an_unknown_agent_id_lists_the_ids_that_do_exist() {
3795 let agents = [
3796 pick_spec("opus", AgentKind::Claude),
3797 pick_spec("oc", AgentKind::Opencode),
3798 ];
3799 let msg = pick(&agents, Some("gemini"), &without(&[]))
3800 .expect_err("no such agent")
3801 .to_string();
3802 assert!(msg.contains("gemini"), "{msg}");
3803 assert!(msg.contains("opus, oc"), "{msg}");
3804 }
3805
3806 #[test]
3807 fn an_explicitly_named_agent_that_is_not_installed_is_an_error_not_a_fallback() {
3808 let agents = [
3809 pick_spec("opus", AgentKind::Claude),
3810 pick_spec("oc", AgentKind::Opencode),
3811 ];
3812 let msg = pick(&agents, Some("oc"), &without(&["oc"]))
3813 .expect_err("must not silently substitute another model")
3814 .to_string();
3815 assert!(msg.contains("opencode"), "{msg}");
3816 assert!(msg.contains("--agent"), "{msg}");
3817 }
3818
3819 fn named(id: &str) -> AgentSpec {
3820 AgentSpec {
3821 id: id.to_owned(),
3822 ..spec(AgentKind::Command, None)
3823 }
3824 }
3825
3826 fn output(text: &str, exit: i32, quota: bool) -> AgentOutput {
3827 AgentOutput {
3828 text: text.to_owned(),
3829 exit_code: Some(exit),
3830 timed_out: false,
3831 duration_ms: 0,
3832 artifacts: Vec::new(),
3833 quota: quota.then_some(Quota {
3834 reset: None,
3835 cause: QuotaCause::UsageLimit,
3836 agent: String::new(),
3837 }),
3838 dropped: None,
3839 commands: Vec::new(),
3840 context_tokens: None,
3841 }
3842 }
3843
3844 #[test]
3845 fn a_chain_keeps_the_written_order_and_a_string_is_a_chain_of_one() {
3846 let agents = [named("a"), named("b"), named("c")];
3847 let all = |_: &AgentSpec| true;
3848 let chain = AgentChoice::Chain(vec!["c".into(), "a".into()]);
3849 let got = pick_chain(&agents, Some(&chain), &all, "synthesizer").unwrap();
3850 assert_eq!(
3851 got.iter().map(|s| s.id.as_str()).collect::<Vec<_>>(),
3852 ["c", "a"]
3853 );
3854
3855 let one = AgentChoice::from("b");
3856 let got = pick_chain(&agents, Some(&one), &all, "synthesizer").unwrap();
3857 assert_eq!(got.len(), 1);
3858 assert_eq!(got[0].id, "b");
3859
3860 let got = pick_chain(&agents, None, &all, "synthesizer").unwrap();
3861 assert_eq!(got.len(), 1, "unset keeps pick's default");
3862 assert_eq!(got[0].id, "a");
3863 let empty = AgentChoice::Chain(Vec::new());
3864 assert_eq!(
3865 pick_chain(&agents, Some(&empty), &all, "x").unwrap()[0].id,
3866 "a"
3867 );
3868 }
3869
3870 #[test]
3871 fn a_chain_skips_unknown_and_uninstalled_ids_and_tries_each_once() {
3872 let agents = [named("a"), named("b")];
3873 let not_a = |s: &AgentSpec| s.id != "a";
3874 let chain = AgentChoice::Chain(
3875 ["a", "ghost", "b", "b"]
3876 .iter()
3877 .map(|s| (*s).to_owned())
3878 .collect(),
3879 );
3880 let got = pick_chain(&agents, Some(&chain), ¬_a, "chatter").unwrap();
3881 assert_eq!(got.iter().map(|s| s.id.as_str()).collect::<Vec<_>>(), ["b"]);
3882
3883 let dup = AgentChoice::Chain(vec!["b".into(), "a".into(), "b".into()]);
3884 let got = pick_chain(&agents, Some(&dup), &|_| true, "chatter").unwrap();
3885 assert_eq!(
3886 got.iter().map(|s| s.id.as_str()).collect::<Vec<_>>(),
3887 ["b", "a"]
3888 );
3889 }
3890
3891 #[test]
3892 fn a_chain_with_nothing_runnable_names_the_role() {
3893 let agents = [named("a")];
3894 let chain = AgentChoice::Chain(vec!["a".into(), "ghost".into()]);
3895 let err = pick_chain(&agents, Some(&chain), &|_| false, "conductor")
3896 .unwrap_err()
3897 .to_string();
3898 assert!(err.contains("conductor"), "{err}");
3899 }
3900
3901 #[test]
3902 fn a_chain_advances_on_error_quota_or_an_unusable_answer_only() {
3903 assert!(chain_advances(&Err(anyhow::anyhow!("spawn failed"))));
3904 assert!(chain_advances(&Ok(output("limit", 0, true))));
3905 assert!(chain_advances(&Ok(output("", 0, false))));
3906 assert!(chain_advances(&Ok(output("x", 1, false))));
3907 assert!(!chain_advances(&Ok(output("answer", 0, false))));
3908 }
3909
3910 #[test]
3911 fn claude_context_tokens_are_unknown_because_usage_is_aggregated() {
3912 for out in [
3913 r#"{"result":"ok","num_turns":1,"usage":{"input_tokens":10,"cache_read_input_tokens":3000}}"#,
3914 r#"{"result":"ok","num_turns":3,"usage":{"input_tokens":10}}"#,
3915 r#"{"result":"ok"}"#,
3916 "not json",
3917 ] {
3918 assert_eq!(context_tokens(AgentKind::Claude, out), None, "{out}");
3919 }
3920 }
3921
3922 #[test]
3923 fn opencode_context_tokens_take_the_last_step_finish() {
3924 let out = concat!(
3925 r#"{"type":"step_finish","sessionID":"s","part":{"type":"step-finish","tokens":{"input":100,"output":5,"cache":{"read":1000,"write":50}}}}"#,
3926 "\n",
3927 r#"{"type":"text","part":{"type":"text","text":"hi"}}"#,
3928 "\n",
3929 r#"{"type":"step_finish","sessionID":"s","part":{"type":"step-finish","tokens":{"input":120,"output":9,"cache":{"read":1500}}}}"#,
3930 "\n"
3931 );
3932 assert_eq!(context_tokens(AgentKind::Opencode, out), Some(1620));
3933 let none = r#"{"type":"step_finish","part":{"type":"step-finish"}}"#;
3934 assert_eq!(context_tokens(AgentKind::Opencode, none), None);
3935 assert_eq!(context_tokens(AgentKind::Opencode, ""), None);
3936 }
3937
3938 #[test]
3939 fn agy_context_tokens_are_unknown_because_usage_is_aggregated() {
3940 let out = r#"{"conversation_id":"c","status":"OK","response":"x","usage":{"input_tokens":260113,"cache_read_tokens":2200925}}"#;
3941 assert_eq!(context_tokens(AgentKind::Antigravity, out), None);
3942 }
3943
3944 #[test]
3945 fn codex_context_tokens_are_unknown_because_usage_is_cumulative() {
3946 let out = concat!(
3947 r#"{"type":"turn.completed","usage":{"input_tokens":1000,"cached_input_tokens":900}}"#,
3948 "\n"
3949 );
3950 assert_eq!(context_tokens(AgentKind::Codex, out), None);
3951 }
3952
3953 #[test]
3954 fn omp_context_tokens_come_from_messages_without_an_agent_end() {
3955 let out = concat!(
3956 r#"{"type":"session","id":"s"}"#,
3957 "\n",
3958 r#"{"type":"message_end","message":{"role":"assistant","content":[],"usage":{"input":50,"cacheRead":400,"cacheWrite":10}}}"#,
3959 "\n",
3960 r#"{"type":"turn_end","message":{"role":"assistant","content":[],"usage":{"input":70,"cacheRead":500}}}"#,
3961 "\n"
3962 );
3963 assert_eq!(context_tokens(AgentKind::Omp, out), Some(570));
3964 let user_only = r#"{"type":"message_end","message":{"role":"user","usage":{"input":9}}}"#;
3965 assert_eq!(context_tokens(AgentKind::Omp, user_only), None);
3966 let no_usage = r#"{"type":"message_end","message":{"role":"assistant","content":[]}}"#;
3967 assert_eq!(context_tokens(AgentKind::Omp, no_usage), None);
3968 }
3969
3970 #[test]
3971 fn command_agents_report_no_context_tokens() {
3972 let out = r#"{"usage":{"input_tokens":5}}"#;
3973 assert_eq!(context_tokens(AgentKind::Command, out), None);
3974 }
3975}