1use std::path::{Path, PathBuf};
52use std::sync::{Arc, Mutex, MutexGuard, PoisonError};
53use std::time::Duration;
54
55use anyhow::{Context, Result, bail};
56use jiff::Timestamp;
57use serde::{Deserialize, Serialize};
58
59use crate::agent::{self, Invocation, SeatState};
60use crate::config::{AgentSpec, Config};
61use crate::queue::{Queue, Source, Task};
62
63pub const SCHEMA: u32 = 1;
65
66fn turn_timeout(cfg: &Config) -> Duration {
77 Duration::from_secs(cfg.graph.timeout_talk)
78}
79
80const SEAT: &str = "talk";
83
84const MAGI_NOTE: &str = "magi: ";
86
87#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
89#[serde(rename_all = "lowercase")]
90pub enum Who {
91 Operator,
93 Agent,
96}
97
98#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
106#[serde(deny_unknown_fields)]
107pub struct Attachment {
108 pub id: String,
110 pub name: String,
112 pub mime: String,
115 pub bytes: u64,
117}
118
119#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
121#[serde(deny_unknown_fields)]
122pub struct Turn {
123 pub who: Who,
125 pub body: String,
127 pub at: Timestamp,
129 #[serde(default)]
132 pub attachments: Vec<Attachment>,
133 #[serde(default, skip_serializing_if = "Option::is_none")]
138 pub usage: Option<TurnUsage>,
139 #[serde(default, skip_serializing_if = "Option::is_none")]
146 pub breaks: Option<Vec<usize>>,
147}
148
149#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
157pub struct TurnUsage {
158 pub context_tokens: u64,
160 pub agent: String,
162 #[serde(default)]
164 pub model: Option<String>,
165}
166
167#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize)]
172pub struct ContextUsage {
173 pub tokens: Option<u64>,
175 pub window: Option<u64>,
177 pub percent: Option<u64>,
179 pub warn: bool,
181 pub since_switch: bool,
185 pub model: Option<String>,
187 #[serde(default)]
190 pub estimated: bool,
191}
192
193const CONTEXT_WARN_PERCENT: u64 = 80;
195
196const STANDING_PROMPT_FALLBACK_CHARS: u64 = 7000;
199
200pub fn estimate_context_tokens(talk: &Talk, standing_chars: u64) -> Option<u64> {
213 let mut counted = false;
214 let mut chars = standing_chars;
215 for t in talk.turns.iter().filter(|t| !t.body.starts_with(MAGI_NOTE)) {
216 counted = true;
217 chars += t.body.chars().count() as u64;
218 }
219 counted.then(|| (chars * 2).div_ceil(7))
221}
222
223pub fn context_usage(talk: &Talk, cfg: Option<&Config>) -> ContextUsage {
236 let current = cfg.and_then(|c| c.agents.iter().find(|a| a.id == talk.agent));
237 let model = current.and_then(|a| a.model.clone());
238 let window = cfg
239 .zip(model.as_deref())
240 .and_then(|(c, m)| c.context_window(m))
241 .filter(|w| *w > 0);
242 let usage = talk
243 .turns
244 .iter()
245 .rev()
246 .find(|t| t.who == Who::Agent && !t.body.starts_with(MAGI_NOTE))
247 .and_then(|t| t.usage.as_ref());
248 let measured = usage.map(|u| u.context_tokens);
249 let tokens = measured.or_else(|| {
250 let standing = cfg.map_or(STANDING_PROMPT_FALLBACK_CHARS, |c| {
251 briefing_for(
252 &talk.repo,
253 &c.graph.language,
254 c.talk.allow_write,
255 crate::persona::active(&c.talk.personas, &talk.persona).as_ref(),
256 c.talk.operator_name(),
257 talk.implementers,
258 )
259 .chars()
260 .count() as u64
261 });
262 estimate_context_tokens(talk, standing)
263 });
264 let estimated = measured.is_none() && tokens.is_some();
265 let since_switch =
266 usage.is_some_and(|u| u.agent != talk.agent || (current.is_some() && u.model != model));
267 let (percent, warn) = match (tokens, window) {
268 (Some(t), Some(w)) => (
269 Some(t.saturating_mul(100) / w),
270 t.saturating_mul(100) >= w.saturating_mul(CONTEXT_WARN_PERCENT),
271 ),
272 _ => (None, false),
273 };
274 ContextUsage {
275 tokens,
276 window,
277 percent,
278 warn,
279 since_switch,
280 model,
281 estimated,
282 }
283}
284
285#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
289#[serde(rename_all = "lowercase")]
290pub enum TalkStatus {
291 Open,
294 Closed,
296}
297
298impl TalkStatus {
299 pub fn open(self) -> bool {
301 matches!(self, Self::Open)
302 }
303
304 pub fn as_str(self) -> &'static str {
306 match self {
307 Self::Open => "open",
308 Self::Closed => "closed",
309 }
310 }
311}
312
313#[derive(Debug, Clone, Serialize, Deserialize)]
315#[serde(deny_unknown_fields)]
316pub struct Talk {
317 pub schema: u32,
319 pub id: String,
321 pub repo: PathBuf,
323 pub agent: String,
325 pub status: TalkStatus,
327 pub turns: Vec<Turn>,
329 #[serde(default)]
332 pub pending: String,
333 #[serde(default)]
335 pub pending_attachments: Vec<Attachment>,
336 #[serde(default, skip_serializing_if = "Option::is_none")]
338 pub pending_breaks: Option<Vec<usize>>,
339 #[serde(default)]
344 pub fallback: bool,
345 #[serde(default)]
348 pub persona: String,
349 #[serde(default)]
353 pub persona_dirty: bool,
354 #[serde(default = "solo_implementers")]
357 pub implementers: u8,
358 #[serde(default)]
361 pub implementers_dirty: bool,
362 pub created_at: Timestamp,
364 pub updated_at: Timestamp,
366 seat: SeatState,
371}
372
373impl Talk {
374 pub fn short(&self) -> &str {
376 short(&self.id)
377 }
378}
379
380#[derive(Debug, Clone)]
382pub struct Talks {
383 root: PathBuf,
384 lock: Arc<Mutex<()>>,
393}
394
395impl Talks {
396 pub fn open() -> Self {
398 Self::at(crate::run::home().join("talks"))
399 }
400
401 pub fn at(root: PathBuf) -> Self {
404 Self {
405 root,
406 lock: Arc::new(Mutex::new(())),
407 }
408 }
409
410 fn guard(&self) -> Result<StoreGuard<'_>> {
425 let mutex = self.lock.lock().unwrap_or_else(PoisonError::into_inner);
426 std::fs::create_dir_all(&self.root)
427 .with_context(|| format!("create {}", self.root.display()))?;
428 let path = self.root.join(".store.turn");
429 let deadline = std::time::Instant::now() + TAKEOVER_LOCK_TTL * 2;
430 loop {
431 if let Some(file) = TurnLock::take(&path)? {
432 return Ok(StoreGuard {
433 _file: file,
434 _mutex: mutex,
435 });
436 }
437 if std::time::Instant::now() >= deadline {
438 bail!("the talk store is locked by another process");
439 }
440 std::thread::sleep(Duration::from_millis(10));
441 }
442 }
443
444 pub fn root(&self) -> &Path {
446 &self.root
447 }
448
449 pub fn path_of(&self, id: &str) -> PathBuf {
451 self.root.join(format!("{id}.json"))
452 }
453
454 pub fn artifacts_of(&self, id: &str) -> PathBuf {
457 self.root.join(format!("{id}.artifacts"))
458 }
459
460 pub fn attachments_dir(&self, id: &str) -> PathBuf {
464 self.artifacts_of(id).join("attachments")
465 }
466
467 pub fn put_attachment(
476 &self,
477 id: &str,
478 mime: &str,
479 name: &str,
480 data: &[u8],
481 ) -> Result<Attachment> {
482 let dir = self.attachments_dir(id);
483 std::fs::create_dir_all(&dir).with_context(|| format!("create {}", dir.display()))?;
484 let ext = attachment_ext(mime).with_context(|| format!("unsupported mime `{mime}`"))?;
485 let att = Attachment {
486 id: new_attachment_id(),
487 name: name.to_owned(),
488 mime: mime.to_owned(),
489 bytes: data.len() as u64,
490 };
491 std::fs::write(dir.join(format!("{}.{ext}", att.id)), data)
492 .with_context(|| format!("write attachment {}", att.id))?;
493 std::fs::write(
494 dir.join(format!("{}.json", att.id)),
495 serde_json::to_string(&att).context("serialize attachment")?,
496 )
497 .with_context(|| format!("write attachment metadata {}", att.id))?;
498 Ok(att)
499 }
500
501 pub fn attachment_meta(&self, id: &str, att_id: &str) -> Result<Option<Attachment>> {
510 if !valid_attachment_id(att_id) {
511 return Ok(None);
512 }
513 let meta_path = self.attachments_dir(id).join(format!("{att_id}.json"));
514 if !meta_path.is_file() {
515 return Ok(None);
516 }
517 let att = serde_json::from_str(
518 &std::fs::read_to_string(&meta_path)
519 .with_context(|| format!("read {}", meta_path.display()))?,
520 )
521 .with_context(|| format!("parse {}", meta_path.display()))?;
522 Ok(Some(att))
523 }
524
525 pub fn read_attachment(&self, id: &str, att_id: &str) -> Result<Option<(Attachment, Vec<u8>)>> {
529 let Some(att) = self.attachment_meta(id, att_id)? else {
530 return Ok(None);
531 };
532 let ext = attachment_ext(&att.mime).with_context(|| {
533 format!("attachment {att_id} has an unsupported mime `{}`", att.mime)
534 })?;
535 let data_path = self.attachments_dir(id).join(format!("{att_id}.{ext}"));
536 let data =
537 std::fs::read(&data_path).with_context(|| format!("read {}", data_path.display()))?;
538 Ok(Some((att, data)))
539 }
540
541 fn attachment_path(&self, id: &str, att: &Attachment) -> Option<PathBuf> {
558 let ext = attachment_ext(&att.mime)?;
559 let path = self.attachments_dir(id).join(format!("{}.{ext}", att.id));
560 std::path::absolute(&path).ok()
561 }
562
563 pub fn put(&self, t: &mut Talk) -> Result<()> {
572 std::fs::create_dir_all(&self.root)
573 .with_context(|| format!("create {}", self.root.display()))?;
574 t.updated_at = Timestamp::now();
575 let body = serde_json::to_string_pretty(t).context("serialize talk")?;
576 let path = self.path_of(&t.id);
577 let tmp = path.with_extension("json.tmp");
578 write_atomic(&tmp, &path, &body)
579 }
580
581 pub fn get(&self, id: &str) -> Result<Talk> {
583 let resolved = self.resolve_id(id)?;
584 read_path(&self.path_of(&resolved))
585 }
586
587 pub fn list(&self) -> Vec<Talk> {
590 self.list_counting_unreadable().0
591 }
592
593 pub fn list_counting_unreadable(&self) -> (Vec<Talk>, usize) {
596 let mut unreadable = 0;
597 let mut all: Vec<Talk> = std::fs::read_dir(&self.root)
598 .into_iter()
599 .flatten()
600 .flatten()
601 .map(|e| e.path())
602 .filter(|p| p.extension().is_some_and(|x| x == "json"))
603 .filter_map(|p| {
604 let talk = read_path(&p).ok();
605 if talk.is_none() {
606 unreadable += 1;
607 }
608 talk
609 })
610 .collect();
611 all.sort_unstable_by(|a, b| {
612 let rank = |t: &Talk| u8::from(!t.status.open());
613 rank(a).cmp(&rank(b)).then_with(|| b.id.cmp(&a.id))
614 });
615 (all, unreadable)
616 }
617
618 pub fn resolve_id(&self, prefix: &str) -> Result<String> {
620 if self.path_of(prefix).is_file() {
621 return Ok(prefix.to_owned());
622 }
623 let hits: Vec<String> = self
624 .list()
625 .into_iter()
626 .map(|t| t.id)
627 .filter(|id| id.starts_with(prefix) || id.ends_with(prefix))
628 .collect();
629 match hits.len() {
630 1 => Ok(hits.into_iter().next().expect("exactly one hit")),
631 0 => bail!("no talk matches `{prefix}`"),
632 _ => bail!(
633 "`{prefix}` matches {} talks: {}",
634 hits.len(),
635 hits.join(", ")
636 ),
637 }
638 }
639
640 pub fn revision(&self) -> u64 {
643 std::fs::read_dir(&self.root)
644 .into_iter()
645 .flatten()
646 .flatten()
647 .filter(|e| e.path().extension().is_none_or(|x| x != "turn"))
648 .filter(|e| !e.file_name().to_string_lossy().starts_with('.'))
649 .filter_map(|e| e.metadata().ok())
650 .filter_map(|m| m.modified().ok())
651 .filter_map(|t| t.duration_since(std::time::UNIX_EPOCH).ok())
652 .map(|d| d.as_millis() as u64)
653 .max()
654 .unwrap_or(0)
655 }
656
657 pub fn count_open(&self) -> usize {
659 self.list().iter().filter(|t| t.status.open()).count()
660 }
661
662 pub fn turn_path(&self, id: &str) -> PathBuf {
665 self.root.join(format!("{id}.turn"))
666 }
667
668 pub fn claim_turn(&self, id: &str) -> Result<Option<TurnLease>> {
677 self.claim_turn_at(id, Timestamp::now())
678 }
679
680 fn claim_turn_at(&self, id: &str, now: Timestamp) -> Result<Option<TurnLease>> {
681 std::fs::create_dir_all(&self.root)
682 .with_context(|| format!("create {}", self.root.display()))?;
683 let path = self.turn_path(id);
684 let token = fresh_token();
685 if create_turn(&path, &token, now)? {
686 return Ok(Some(TurnLease {
687 talk: id.to_owned(),
688 path,
689 token,
690 }));
691 }
692 if lease_blocks(&path, now) {
693 return Ok(None);
694 }
695 let Some(_lock) = TurnLock::take(&path)? else {
700 return Ok(None);
701 };
702 if lease_blocks(&path, now) {
703 return Ok(None);
704 }
705 let _ = std::fs::remove_file(&path);
706 Ok(create_turn(&path, &token, now)?.then_some(TurnLease {
707 talk: id.to_owned(),
708 path,
709 token,
710 }))
711 }
712
713 pub fn turn_held(&self, id: &str) -> bool {
715 read_turn(&self.turn_path(id)).is_some_and(|r| r.fresh(Timestamp::now()))
716 }
717
718 pub fn remove(&self, id: &str) -> Result<()> {
731 let _guard = self.guard()?;
732 let resolved = self.resolve_id(id)?;
733 let path = self.path_of(&resolved);
734 std::fs::remove_file(&path).with_context(|| format!("remove {}", path.display()))?;
735 let artifacts = self.artifacts_of(&resolved);
736 if artifacts.is_dir() {
737 std::fs::remove_dir_all(&artifacts)
738 .with_context(|| format!("remove {}", artifacts.display()))?;
739 }
740 let _ = std::fs::remove_file(self.turn_path(&resolved));
741 Ok(())
742 }
743}
744
745struct StoreGuard<'a> {
748 _file: TurnLock,
749 _mutex: MutexGuard<'a, ()>,
750}
751
752#[derive(Debug, Serialize, Deserialize)]
755struct TurnRecord {
756 token: String,
757 pid: u32,
758 beat_at: Timestamp,
759}
760
761impl TurnRecord {
762 fn fresh(&self, now: Timestamp) -> bool {
763 now.as_second() - self.beat_at.as_second() <= crate::ask::LEASE_TTL.as_secs() as i64
764 }
765}
766
767fn fresh_token() -> String {
771 static SEQ: std::sync::atomic::AtomicU64 = std::sync::atomic::AtomicU64::new(0);
772 let n = SEQ.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
773 let seed = crate::rng::entropy() ^ n.wrapping_mul(0x9E37_79B9_7F4A_7C15);
774 crate::rng::SplitMix64::new(seed).uuid_v4()
775}
776
777fn read_turn(path: &Path) -> Option<TurnRecord> {
778 serde_json::from_str(&std::fs::read_to_string(path).ok()?).ok()
779}
780
781const TAKEOVER_LOCK_TTL: Duration = Duration::from_secs(10);
783
784const TICKET_TTL: Duration = Duration::from_secs(10);
787
788const TICKET_GENERATIONS: u32 = 16;
790
791const TICKET_SWEEP_AGE: Duration = Duration::from_secs(3600);
793
794fn create_exclusive(path: &Path, body: &str) -> Result<bool> {
796 use std::io::Write as _;
797 match std::fs::OpenOptions::new()
798 .write(true)
799 .create_new(true)
800 .open(path)
801 {
802 Ok(mut f) => {
803 if let Err(e) = f.write_all(body.as_bytes()) {
804 drop(f);
805 let _ = std::fs::remove_file(path);
806 return Err(e).with_context(|| format!("write {}", path.display()));
807 }
808 Ok(true)
809 }
810 Err(e) if e.kind() == std::io::ErrorKind::AlreadyExists => Ok(false),
811 Err(e) => Err(e).with_context(|| format!("create {}", path.display())),
812 }
813}
814
815fn create_turn(path: &Path, token: &str, now: Timestamp) -> Result<bool> {
816 let record = TurnRecord {
817 token: token.to_owned(),
818 pid: std::process::id(),
819 beat_at: now,
820 };
821 let body = serde_json::to_string(&record).context("serialize turn lease")?;
822 let tmp = path.with_extension(format!("turn.{token}.new"));
825 publish_exclusive(path, &tmp, &body)
826}
827
828fn lease_blocks(path: &Path, now: Timestamp) -> bool {
832 if read_turn(path).is_some_and(|r| r.fresh(now)) {
833 return true;
834 }
835 std::fs::metadata(path).is_ok_and(|m| {
836 m.len() == 0
837 && m.modified()
838 .ok()
839 .and_then(|t| t.elapsed().ok())
840 .is_some_and(|age| age <= TAKEOVER_LOCK_TTL)
841 })
842}
843
844fn link_unsupported(e: &std::io::Error) -> bool {
847 e.kind() == std::io::ErrorKind::Unsupported || (cfg!(windows) && e.raw_os_error() == Some(1))
848}
849
850fn create_in_place(path: &Path, body: &str) -> Result<bool> {
868 use std::io::Write as _;
869 let mut f = match std::fs::OpenOptions::new()
870 .write(true)
871 .create_new(true)
872 .open(path)
873 {
874 Ok(f) => f,
875 Err(e) if e.kind() == std::io::ErrorKind::AlreadyExists => return Ok(false),
876 Err(e) => return Err(e).with_context(|| format!("create {}", path.display())),
877 };
878 f.write_all(body.as_bytes())
879 .with_context(|| format!("write {}", path.display()))?;
880 Ok(std::fs::read_to_string(path).is_ok_and(|t| t == body))
883}
884
885fn publish_exclusive(path: &Path, tmp: &Path, body: &str) -> Result<bool> {
886 std::fs::write(tmp, body).with_context(|| format!("write {}", tmp.display()))?;
887 #[cfg(test)]
888 let linked = if failpoint::no_link_forced() {
889 Err(std::io::Error::from(std::io::ErrorKind::Unsupported))
890 } else {
891 std::fs::hard_link(tmp, path)
892 };
893 #[cfg(not(test))]
894 let linked = std::fs::hard_link(tmp, path);
895 let out = match linked {
896 Ok(()) => Ok(true),
897 Err(e) if e.kind() == std::io::ErrorKind::AlreadyExists => Ok(false),
898 Err(e) if link_unsupported(&e) => create_in_place(path, body),
899 Err(e) => Err(e).with_context(|| format!("create {}", path.display())),
900 };
901 let _ = std::fs::remove_file(tmp);
902 out
903}
904
905fn remove_if_carries(path: &Path, expected: &str) -> bool {
916 let gone = path.with_extension(format!("gone.{}", fresh_token()));
917 if std::fs::rename(path, &gone).is_err() {
918 return false;
919 }
920 let found = std::fs::read_to_string(&gone);
921 if found.as_ref().is_ok_and(|t| t == expected) {
922 let _ = std::fs::remove_file(&gone);
923 return true;
924 }
925 if let Ok(body) = found {
926 let back = path.with_extension(format!("back.{}", fresh_token()));
927 match publish_exclusive(path, &back, &body) {
928 Ok(true) => {}
929 Ok(false) | Err(_) => tracing::warn!(
930 "{} was replaced while a stale removal had it aside; \
931 the displaced file is dropped",
932 path.display()
933 ),
934 }
935 }
936 let _ = std::fs::remove_file(&gone);
937 false
938}
939
940struct TurnLock {
944 path: PathBuf,
945 token: String,
946}
947
948impl TurnLock {
949 fn token() -> String {
950 fresh_token()
951 }
952
953 fn publish(path: &Path, token: &str) -> Result<bool> {
957 let tmp = path.with_extension(format!("lock.{token}.new"));
958 publish_exclusive(path, &tmp, token)
959 }
960
961 fn take(lease: &Path) -> Result<Option<Self>> {
962 let path = lease.with_extension("turn.lock");
963 let token = Self::token();
964 if Self::publish(&path, &token)? {
965 return Ok(Some(Self { path, token }));
966 }
967 let Ok(seen) = std::fs::read_to_string(&path) else {
968 return Ok(None);
969 };
970 let key = if !seen.is_empty() && seen.chars().all(|c| c.is_ascii_alphanumeric() || c == '-')
973 {
974 seen.as_str()
975 } else {
976 "invalid"
977 };
978 let aged = std::fs::metadata(&path)
979 .and_then(|m| m.modified())
980 .ok()
981 .and_then(|t| t.elapsed().ok())
982 .is_some_and(|age| age > TAKEOVER_LOCK_TTL);
983 if !aged {
984 return Ok(None);
985 }
986 let mut won = false;
998 for n in 0..TICKET_GENERATIONS {
999 let ticket = path.with_extension(format!("lock.{key}.break.{n}"));
1000 if create_exclusive(&ticket, "")? {
1001 won = true;
1002 break;
1003 }
1004 let stale = std::fs::metadata(&ticket)
1005 .and_then(|m| m.modified())
1006 .ok()
1007 .and_then(|t| t.elapsed().ok())
1008 .is_some_and(|age| age > TICKET_TTL);
1009 if !stale {
1010 return Ok(None);
1011 }
1012 }
1013 if !won {
1014 Self::sweep_tickets(&path);
1019 return Ok(None);
1020 }
1021 Self::sweep_tickets(&path);
1022 if !remove_if_carries(&path, &seen) {
1028 return Ok(None);
1029 }
1030 if Self::publish(&path, &token)? {
1031 return Ok(Some(Self { path, token }));
1032 }
1033 Ok(None)
1034 }
1035
1036 fn sweep_tickets(path: &Path) {
1039 let (Some(dir), Some(name)) = (path.parent(), path.file_name().and_then(|n| n.to_str()))
1040 else {
1041 return;
1042 };
1043 let prefix = format!("{name}.");
1044 let Ok(entries) = std::fs::read_dir(dir) else {
1045 return;
1046 };
1047 for entry in entries.flatten() {
1048 let file = entry.file_name();
1049 let Some(file) = file.to_str() else { continue };
1050 if !(file.starts_with(&prefix) && file.contains(".break.")) {
1051 continue;
1052 }
1053 let old = entry
1054 .metadata()
1055 .and_then(|m| m.modified())
1056 .ok()
1057 .and_then(|t| t.elapsed().ok())
1058 .is_some_and(|age| age > TICKET_SWEEP_AGE);
1059 if old {
1060 let _ = std::fs::remove_file(entry.path());
1061 }
1062 }
1063 }
1064
1065 fn take_patiently(lease: &Path) -> Option<Self> {
1067 for _ in 0..50 {
1068 match Self::take(lease) {
1069 Ok(Some(lock)) => return Some(lock),
1070 Ok(None) => std::thread::sleep(Duration::from_millis(10)),
1071 Err(_) => return None,
1072 }
1073 }
1074 None
1075 }
1076}
1077
1078impl Drop for TurnLock {
1079 fn drop(&mut self) {
1080 remove_if_carries(&self.path, &self.token);
1082 }
1083}
1084
1085pub const TURN_BEAT: Duration = Duration::from_secs(20);
1088
1089#[derive(Debug)]
1094pub struct TurnLease {
1095 talk: String,
1096 path: PathBuf,
1097 token: String,
1098}
1099
1100impl TurnLease {
1101 pub fn holds(&self) -> bool {
1103 read_turn(&self.path).is_some_and(|r| r.token == self.token)
1104 }
1105
1106 pub fn beat(&self) -> Result<bool> {
1110 let _lock = TurnLock::take_patiently(&self.path)
1111 .with_context(|| format!("lock {} to renew it", self.path.display()))?;
1112 let Some(mut record) = read_turn(&self.path).filter(|r| r.token == self.token) else {
1113 return Ok(false);
1114 };
1115 record.beat_at = Timestamp::now();
1116 let body = serde_json::to_string(&record).context("serialize turn lease")?;
1117 let tmp = self.path.with_extension(format!("turn.{}.tmp", self.token));
1118 write_atomic(&tmp, &self.path, &body)?;
1119 Ok(true)
1120 }
1121
1122 pub async fn beating<T>(&self, fut: impl std::future::Future<Output = T>) -> Result<T> {
1127 self.beating_every(TURN_BEAT, fut).await
1128 }
1129
1130 async fn beating_every<T>(
1131 &self,
1132 period: Duration,
1133 fut: impl std::future::Future<Output = T>,
1134 ) -> Result<T> {
1135 tokio::pin!(fut);
1136 loop {
1137 match tokio::time::timeout(period, &mut fut).await {
1138 Ok(out) => return Ok(out),
1139 Err(_) => match self.beat() {
1140 Ok(true) => {}
1141 Ok(false) => bail!(
1142 "the turn lease {} was taken over; this turn is stopped",
1143 self.path.display()
1144 ),
1145 Err(e) => tracing::warn!("{e:#}"),
1146 },
1147 }
1148 }
1149 }
1150}
1151
1152impl Drop for TurnLease {
1153 fn drop(&mut self) {
1154 if let Some(_lock) = TurnLock::take_patiently(&self.path) {
1157 if read_turn(&self.path).is_some_and(|r| r.token == self.token) {
1158 let _ = std::fs::remove_file(&self.path);
1159 }
1160 }
1161 }
1162}
1163
1164pub fn begin(store: &Talks, cfg: &Config, repo: PathBuf, agent: Option<&str>) -> Result<Talk> {
1174 begin_with(store, cfg, repo, agent, &Preferred::default())
1175}
1176
1177#[derive(Debug, Default, Clone)]
1181pub struct Preferred {
1182 pub agent: Option<String>,
1184 pub persona: Option<String>,
1186 pub implementers: Option<i64>,
1188}
1189
1190pub fn begin_with(
1197 store: &Talks,
1198 cfg: &Config,
1199 repo: PathBuf,
1200 agent: Option<&str>,
1201 preferred: &Preferred,
1202) -> Result<Talk> {
1203 let repo = repo.canonicalize().unwrap_or(repo);
1206 let head = || -> Result<AgentSpec> {
1209 Ok(agent::pick_chain(
1210 &cfg.agents,
1211 cfg.roles.chatter.as_ref(),
1212 &agent::installed,
1213 "chatter",
1214 )?
1215 .remove(0))
1216 };
1217 let mut fallback = agent.is_none();
1218 let spec = match agent {
1219 Some(id) => agent::pick(&cfg.agents, Some(id), &agent::installed)?,
1220 None => {
1221 match preferred
1222 .agent
1223 .as_deref()
1224 .map(str::trim)
1225 .filter(|id| !id.is_empty())
1226 {
1227 Some(id) => match agent::pick(&cfg.agents, Some(id), &agent::installed) {
1228 Ok(spec) => {
1229 fallback = head().is_ok_and(|head| spec.id == head.id);
1230 spec
1231 }
1232 Err(_) => head()?,
1233 },
1234 None => head()?,
1235 }
1236 }
1237 };
1238 let persona = preferred
1239 .persona
1240 .as_deref()
1241 .and_then(|id| crate::persona::find(&cfg.talk.personas, id))
1242 .filter(|p| !p.is_default())
1243 .map(|p| p.id)
1244 .unwrap_or_default();
1245 let implementers = preferred
1246 .implementers
1247 .and_then(|n| u8::try_from(n).ok())
1248 .and_then(|n| check_implementers(n, cfg).ok())
1249 .unwrap_or(1);
1250
1251 let now = Timestamp::now();
1252 let mut talk = Talk {
1253 pending_breaks: None,
1254 schema: SCHEMA,
1255 id: new_id(),
1256 repo,
1257 agent: spec.id.clone(),
1258 status: TalkStatus::Open,
1259 turns: Vec::new(),
1260 pending: String::new(),
1261 pending_attachments: Vec::new(),
1262 fallback,
1263 persona,
1264 persona_dirty: false,
1265 implementers,
1266 implementers_dirty: false,
1267 created_at: now,
1268 updated_at: now,
1269 seat: SeatState::new(SEAT, &spec.id, crate::rng::entropy()),
1270 };
1271 store.put(&mut talk)?;
1272 Ok(talk)
1273}
1274
1275pub fn record(
1282 talk: &mut Talk,
1283 store: &Talks,
1284 text: &str,
1285 attachments: Vec<Attachment>,
1286) -> Result<String> {
1287 let _guard = store.guard()?;
1295 let Ok(fresh) = store.get(&talk.id) else {
1300 bail!("talk {} was deleted", talk.short());
1301 };
1302 talk.status = fresh.status;
1303 talk.pending = fresh.pending;
1306 talk.pending_breaks = fresh.pending_breaks;
1307 talk.pending_attachments = fresh.pending_attachments;
1308 if !talk.status.open() {
1309 bail!(
1310 "talk {} is {} and takes no more turns",
1311 talk.short(),
1312 talk.status.as_str()
1313 );
1314 }
1315 let text = text.trim();
1316 if text.is_empty() && attachments.is_empty() {
1317 bail!("nothing to say");
1318 }
1319 talk.turns.push(Turn {
1320 who: Who::Operator,
1321 body: text.to_owned(),
1322 at: Timestamp::now(),
1323 attachments,
1324 usage: None,
1325 breaks: Some(Vec::new()),
1326 });
1327 store.put(talk)?;
1328 Ok(text.to_owned())
1329}
1330
1331pub fn queue(
1333 talk: &mut Talk,
1334 store: &Talks,
1335 text: &str,
1336 attachments: Vec<Attachment>,
1337) -> Result<()> {
1338 let text = text.trim();
1339 if text.is_empty() && attachments.is_empty() {
1340 bail!("nothing to say");
1341 }
1342 let _guard = store.guard()?;
1343 let mut fresh = store
1344 .get(&talk.id)
1345 .with_context(|| format!("talk {} was deleted", talk.short()))?;
1346 if !fresh.status.open() {
1347 bail!(
1348 "talk {} is {} and takes no more turns",
1349 fresh.short(),
1350 fresh.status.as_str()
1351 );
1352 }
1353 if !text.is_empty() {
1354 if fresh.pending.is_empty() {
1355 fresh.pending = text.to_owned();
1356 fresh.pending_breaks = Some(Vec::new());
1357 } else {
1358 fresh.pending.push_str("\n\n");
1359 if let Some(b) = fresh.pending_breaks.as_mut() {
1360 b.push(fresh.pending.len());
1361 }
1362 fresh.pending.push_str(text);
1363 }
1364 }
1365 fresh.pending_attachments.extend(attachments);
1366 store.put(&mut fresh)?;
1367 *talk = fresh;
1368 Ok(())
1369}
1370
1371pub fn drain(talk: &mut Talk, store: &Talks) -> Result<Option<String>> {
1373 let _guard = store.guard()?;
1374 let mut fresh = store
1375 .get(&talk.id)
1376 .with_context(|| format!("talk {} was deleted", talk.short()))?;
1377 if !fresh.status.open() || (fresh.pending.is_empty() && fresh.pending_attachments.is_empty()) {
1378 *talk = fresh;
1379 return Ok(None);
1380 }
1381 let text = std::mem::take(&mut fresh.pending);
1382 let attachments = std::mem::take(&mut fresh.pending_attachments);
1383 let breaks = fresh.pending_breaks.take();
1384 fresh.turns.push(Turn {
1385 who: Who::Operator,
1386 body: text.clone(),
1387 at: Timestamp::now(),
1388 attachments,
1389 usage: None,
1390 breaks,
1391 });
1392 store.put(&mut fresh)?;
1393 *talk = fresh;
1394 Ok(Some(text))
1395}
1396
1397pub async fn say(
1400 lease: &TurnLease,
1401 talk: &mut Talk,
1402 store: &Talks,
1403 cfg: &Config,
1404 text: &str,
1405 attachments: Vec<Attachment>,
1406) -> Result<()> {
1407 check_lease(lease, talk)?;
1408 let text = record(talk, store, text, attachments)?;
1409 respond(lease, talk, store, cfg, &text).await
1410}
1411
1412pub async fn respond(
1418 lease: &TurnLease,
1419 talk: &mut Talk,
1420 store: &Talks,
1421 cfg: &Config,
1422 text: &str,
1423) -> Result<()> {
1424 check_lease(lease, talk)?;
1425 lease
1426 .beating(turn(talk, store, cfg, text))
1427 .await
1428 .and_then(|done| done)
1429}
1430
1431fn check_lease(lease: &TurnLease, talk: &Talk) -> Result<()> {
1432 if lease.talk != talk.id {
1433 bail!("the turn lease is for talk {}, not {}", lease.talk, talk.id);
1434 }
1435 if !lease.holds() {
1438 bail!("the turn lease for talk {} is no longer held", talk.short());
1439 }
1440 Ok(())
1441}
1442
1443pub fn close(talk: &mut Talk, store: &Talks) -> Result<()> {
1461 let _guard = store.guard()?;
1462 let mut fresh = store
1463 .get(&talk.id)
1464 .with_context(|| format!("talk {} was deleted", talk.short()))?;
1465 fresh.status = TalkStatus::Closed;
1466 fresh.pending.clear();
1468 fresh.pending_breaks = None;
1469 fresh.pending_attachments.clear();
1470 store.put(&mut fresh)?;
1471 *talk = fresh;
1472 Ok(())
1473}
1474
1475pub fn reopen(talk: &mut Talk, store: &Talks) -> Result<()> {
1486 let _guard = store.guard()?;
1487 let mut fresh = store
1488 .get(&talk.id)
1489 .with_context(|| format!("talk {} was deleted", talk.short()))?;
1490 fresh.status = TalkStatus::Open;
1491 store.put(&mut fresh)?;
1492 *talk = fresh;
1493 Ok(())
1494}
1495
1496pub fn switch_agent(talk: &mut Talk, store: &Talks, spec: &AgentSpec) -> Result<bool> {
1509 let _guard = store.guard()?;
1510 let mut fresh = store
1511 .get(&talk.id)
1512 .with_context(|| format!("talk {} was deleted", talk.short()))?;
1513 if fresh.agent == spec.id {
1514 *talk = fresh;
1515 return Ok(false);
1516 }
1517 let from = std::mem::replace(&mut fresh.agent, spec.id.clone());
1518 fresh.seat = SeatState::new(SEAT, &spec.id, crate::rng::entropy());
1519 fresh.fallback = false;
1521 fresh.turns.push(Turn {
1522 breaks: None,
1523 who: Who::Agent,
1524 body: format!("{MAGI_NOTE}agent changed from {from} to {}", spec.id),
1525 at: Timestamp::now(),
1526 attachments: Vec::new(),
1527 usage: None,
1528 });
1529 store.put(&mut fresh)?;
1530 *talk = fresh;
1531 Ok(true)
1532}
1533
1534pub fn switch_persona(talk: &mut Talk, store: &Talks, id: &str) -> Result<bool> {
1542 let id = if id.trim() == crate::persona::DEFAULT_ID {
1543 ""
1544 } else {
1545 id.trim()
1546 };
1547 let _guard = store.guard()?;
1548 let mut fresh = store
1549 .get(&talk.id)
1550 .with_context(|| format!("talk {} was deleted", talk.short()))?;
1551 if fresh.persona == id {
1552 *talk = fresh;
1553 return Ok(false);
1554 }
1555 let from = std::mem::replace(&mut fresh.persona, id.to_owned());
1556 fresh.persona_dirty = true;
1557 let label = |p: &str| {
1558 if p.is_empty() {
1559 crate::persona::DEFAULT_ID.to_owned()
1560 } else {
1561 p.to_owned()
1562 }
1563 };
1564 fresh.turns.push(Turn {
1565 breaks: None,
1566 who: Who::Agent,
1567 body: format!(
1568 "{MAGI_NOTE}persona changed from {} to {}",
1569 label(&from),
1570 label(id)
1571 ),
1572 at: Timestamp::now(),
1573 attachments: Vec::new(),
1574 usage: None,
1575 });
1576 store.put(&mut fresh)?;
1577 *talk = fresh;
1578 Ok(true)
1579}
1580
1581pub fn clear_pending(talk: &mut Talk, store: &Talks) -> Result<()> {
1583 let _guard = store.guard()?;
1584 let mut fresh = store
1585 .get(&talk.id)
1586 .with_context(|| format!("talk {} was deleted", talk.short()))?;
1587 fresh.pending.clear();
1588 fresh.pending_breaks = None;
1589 fresh.pending_attachments.clear();
1590 store.put(&mut fresh)?;
1591 *talk = fresh;
1592 Ok(())
1593}
1594
1595pub fn clear_pending_if_matches(
1597 talk: &mut Talk,
1598 store: &Talks,
1599 expected_text: &str,
1600 expected_attachments: &[String],
1601) -> Result<bool> {
1602 let _guard = store.guard()?;
1603 let mut fresh = store
1604 .get(&talk.id)
1605 .with_context(|| format!("talk {} was deleted", talk.short()))?;
1606 if !pending_matches(&fresh, expected_text, expected_attachments) {
1607 *talk = fresh;
1608 return Ok(false);
1609 }
1610 fresh.pending.clear();
1611 fresh.pending_breaks = None;
1612 fresh.pending_attachments.clear();
1613 store.put(&mut fresh)?;
1614 *talk = fresh;
1615 Ok(true)
1616}
1617
1618pub fn edit_pending_text(
1622 talk: &mut Talk,
1623 store: &Talks,
1624 text: &str,
1625 expected_text: &str,
1626 expected_attachments: &[String],
1627) -> Result<bool> {
1628 let _guard = store.guard()?;
1629 let mut fresh = store
1630 .get(&talk.id)
1631 .with_context(|| format!("talk {} was deleted", talk.short()))?;
1632 if !pending_matches(&fresh, expected_text, expected_attachments) {
1633 *talk = fresh;
1634 return Ok(false);
1635 }
1636 fresh.pending = text.trim().to_owned();
1637 fresh.pending_breaks = Some(Vec::new());
1638 store.put(&mut fresh)?;
1639 *talk = fresh;
1640 Ok(true)
1641}
1642
1643fn pending_matches(talk: &Talk, expected_text: &str, expected_attachments: &[String]) -> bool {
1644 talk.pending == expected_text
1645 && talk
1646 .pending_attachments
1647 .iter()
1648 .map(|attachment| &attachment.id)
1649 .eq(expected_attachments.iter())
1650}
1651
1652async fn turn(talk: &mut Talk, store: &Talks, cfg: &Config, text: &str) -> Result<()> {
1659 let spec = cfg
1660 .agents
1661 .iter()
1662 .find(|a| a.id == talk.agent)
1663 .with_context(|| {
1664 format!(
1665 "talk {} was opened with agent `{}`, which is no longer in \
1666 the roster; restore it in magi.toml or start a new \
1667 conversation",
1668 talk.short(),
1669 talk.agent
1670 )
1671 })?;
1672
1673 let last_note = attachment_note(
1677 store,
1678 &talk.id,
1679 talk.turns
1680 .last()
1681 .map_or(&[][..], |t| t.attachments.as_slice()),
1682 );
1683
1684 let attachment_paths: Vec<PathBuf> = talk
1690 .turns
1691 .iter()
1692 .flat_map(|t| t.attachments.iter())
1693 .filter_map(|a| store.attachment_path(&talk.id, a))
1694 .collect();
1695
1696 let questions =
1706 crate::run::try_home().map(|home| crate::ask::Questions::at(home.join("questions")));
1707 let consulted = questions
1708 .as_ref()
1709 .is_some_and(|q| crate::consult::pending_consults(q, &talk.id));
1710 let consult_roots: Vec<PathBuf> = match &questions {
1711 Some(q) if consulted => vec![q.root().to_path_buf()],
1712 _ => Vec::new(),
1713 };
1714
1715 let (allow_write, unsandboxed) = turn_access(cfg.talk.allow_write, consulted);
1716
1717 let artifacts = store.artifacts_of(&talk.id);
1718 let operator_turns = talk.turns.iter().filter(|t| t.who == Who::Operator).count();
1721 let stem = format!("turn-{}", operator_turns.max(1));
1722 let cache_dir = cfg.cache_dir();
1725
1726 let mut chain = vec![spec.clone()];
1729 if let Some(choice) = cfg.roles.chatter.as_ref()
1730 && talk.fallback
1731 {
1732 for id in choice.ids() {
1733 if id == talk.agent || chain.iter().any(|s| s.id == id) {
1734 continue;
1735 }
1736 match agent::pick(&cfg.agents, Some(id), &agent::installed) {
1737 Ok(s) => chain.push(s),
1738 Err(e) => tracing::warn!("[roles] chatter: skipping `{id}`: {e:#}"),
1739 }
1740 }
1741 }
1742
1743 let persona = crate::persona::active(&cfg.talk.personas, &talk.persona);
1744 let operator_name = cfg.talk.operator_name();
1745 let persona_update = if talk.persona_dirty {
1746 format!(
1747 "{}\n\n",
1748 crate::persona::update_block_for(persona.as_ref(), operator_name)
1749 )
1750 } else {
1751 String::new()
1752 };
1753
1754 let filing_update = if talk.implementers_dirty {
1757 format!("{}\n\n", filing_update_block(talk.implementers))
1758 } else {
1759 String::new()
1760 };
1761
1762 let mut outcome = None;
1763 let mut fell_back_from: Option<String> = None;
1764 let mut first_try: Option<(String, SeatState)> = None;
1767 for (n, spec) in chain.iter().enumerate() {
1768 if n > 0 {
1769 if first_try.is_none() {
1770 first_try = Some((talk.agent.clone(), talk.seat.clone()));
1771 }
1772 tracing::warn!("chat: falling back from `{}` to `{}`", talk.agent, spec.id);
1773 fell_back_from.get_or_insert_with(|| talk.agent.clone());
1776 talk.agent = spec.id.clone();
1777 talk.seat = SeatState::new(SEAT, &spec.id, crate::rng::entropy());
1778 }
1779 let resuming = agent::has_session(spec.kind, &talk.seat, cfg.graph.sessions);
1780 let first_ever = talk.turns.len() <= 1;
1781 let body = if talk.seat.turns == 0 && first_ever {
1782 format!(
1783 "{}\n\n# Operator\n\n{text}{last_note}",
1784 briefing_for(
1785 &talk.repo,
1786 &cfg.graph.language,
1787 cfg.talk.allow_write,
1788 persona.as_ref(),
1789 operator_name,
1790 talk.implementers
1791 )
1792 )
1793 } else if talk.seat.turns == 0 {
1794 format!(
1797 "{}\n\n{}\n\n# Operator\n\n{text}{last_note}",
1798 briefing_for(
1799 &talk.repo,
1800 &cfg.graph.language,
1801 cfg.talk.allow_write,
1802 persona.as_ref(),
1803 operator_name,
1804 talk.implementers
1805 ),
1806 transcript(talk, store)
1807 )
1808 } else if resuming {
1809 format!("{persona_update}{filing_update}{text}{last_note}")
1810 } else {
1811 let mut standing = match (&persona, talk.persona_dirty) {
1815 (Some(p), false) => format!("{}\n", crate::persona::section_for(p, operator_name)),
1816 _ => persona_update.clone(),
1817 };
1818 if let (None, Some(n)) = (&persona, operator_name) {
1821 standing.push_str(&format!(
1822 "# Addressing the operator\n{}\n",
1823 crate::persona::addressing(n)
1824 ));
1825 }
1826 if talk.implementers_dirty || talk.implementers != 1 {
1827 standing.push_str(&format!("{}\n\n", filing_update_block(talk.implementers)));
1828 }
1829 format!("{}\n\n{standing}{text}{last_note}", transcript(talk, store))
1830 };
1831 let attempt_stem = if n == 0 {
1832 stem.clone()
1833 } else {
1834 format!("{stem}-{}", spec.id)
1835 };
1836 let inv = Invocation {
1837 cwd: &talk.repo,
1838 prompt: &body,
1839 timeout: turn_timeout(cfg),
1840 allow_write,
1845 unsandboxed,
1846 sessions: cfg.graph.sessions,
1847 artifacts: &artifacts,
1848 stem: &attempt_stem,
1849 run: &talk.id,
1852 node: crate::queue::CHAT_NODE,
1853 cache_dir: cache_dir.as_deref(),
1854 attachments: &attachment_paths,
1855 writable: &consult_roots,
1856 };
1857 let result = agent::invoke(spec, &mut talk.seat, &inv).await;
1858 let advance = agent::chain_advances(&result);
1859 if n == 0 || !advance {
1860 outcome = Some(result);
1861 } else {
1862 tracing::warn!("chat: fallback agent `{}` also failed", spec.id);
1864 }
1865 if !advance {
1866 break;
1867 }
1868 }
1869 if outcome.as_ref().is_some_and(agent::chain_advances) {
1870 if let Some((id, seat)) = first_try {
1873 talk.agent = id;
1874 talk.seat = seat;
1875 fell_back_from = None;
1876 }
1877 }
1878 let outcome = outcome.expect("a chain holds at least one agent");
1879 let note = |why: String| Turn {
1880 breaks: None,
1881 who: Who::Agent,
1882 body: format!("{MAGI_NOTE}{why}"),
1883 at: Timestamp::now(),
1884 attachments: Vec::new(),
1885 usage: None,
1886 };
1887 let (reply, failure) = match outcome {
1888 Err(e) => (
1889 note(format!("could not run agent `{}`: {e}", talk.agent)),
1890 Some(format!("could not run agent `{}`: {e}", talk.agent)),
1891 ),
1892 Ok(out) if out.quota_exhausted() => {
1893 let reset = out
1894 .quota
1895 .as_ref()
1896 .and_then(|q| q.reset.clone())
1897 .map_or_else(String::new, |r| format!(" (resets {r})"));
1898 let why = format!(
1899 "agent `{}` is out of quota{reset}; your message is saved, so \
1900 say it again when the window reopens",
1901 talk.agent
1902 );
1903 (note(why.clone()), Some(why))
1904 }
1905 Ok(out) if out.timed_out => {
1906 let why = format!(
1907 "agent `{}` did not answer within {}s; your message is saved",
1908 talk.agent,
1909 turn_timeout(cfg).as_secs()
1910 );
1911 (note(why.clone()), Some(why))
1912 }
1913 Ok(out) if !out.usable() => {
1914 let why = format!(
1915 "agent `{}` produced no answer (exit {}); your message is saved",
1916 talk.agent,
1917 out.exit_code
1918 .map_or_else(|| "unknown".to_owned(), |c| c.to_string())
1919 );
1920 (note(why.clone()), Some(why))
1921 }
1922 Ok(out) => (
1923 Turn {
1924 breaks: None,
1925 who: Who::Agent,
1926 body: out.text.trim().to_owned(),
1927 at: Timestamp::now(),
1928 attachments: Vec::new(),
1929 usage: out.context_tokens.map(|context_tokens| TurnUsage {
1932 context_tokens,
1933 agent: talk.agent.clone(),
1934 model: cfg
1935 .agents
1936 .iter()
1937 .find(|a| a.id == talk.agent)
1938 .and_then(|a| a.model.clone()),
1939 }),
1940 },
1941 None,
1942 ),
1943 };
1944
1945 let _guard = store.guard()?;
1957 let Ok(fresh) = store.get(&talk.id) else {
1963 return Ok(());
1964 };
1965 talk.status = fresh.status;
1966 talk.pending = fresh.pending;
1970 talk.pending_breaks = fresh.pending_breaks;
1971 talk.pending_attachments = fresh.pending_attachments;
1972 if let Some(from) = fell_back_from.filter(|_| failure.is_none()) {
1973 talk.turns.push(note(format!(
1976 "agent changed from {from} to {} (fallback)",
1977 talk.agent
1978 )));
1979 }
1980 if failure.is_none() {
1983 talk.persona_dirty = false;
1984 talk.implementers_dirty = false;
1985 }
1986 talk.turns.push(reply);
1987 if let Err(put_err) = store.put(talk) {
1988 let lost = talk.turns.pop().expect("just pushed above");
1997 let stash = stash_lost_turn(store, &talk.id, &stem, &lost);
1998 let why = match &stash {
1999 Ok(path) => format!(
2000 "agent `{}` answered, but the reply could not be saved to \
2001 this conversation ({put_err:#}); the raw text was kept at \
2002 {} - your message is saved, ask again",
2003 talk.agent,
2004 path.display()
2005 ),
2006 Err(stash_err) => format!(
2007 "agent `{}` answered, but the reply could not be saved to \
2008 this conversation ({put_err:#}), and it could not be kept \
2009 anywhere else either ({stash_err:#}); your message is \
2010 saved, ask again",
2011 talk.agent
2012 ),
2013 };
2014 talk.turns.push(note(why.clone()));
2015 return match store.put(talk) {
2022 Ok(()) => bail!("{why}"),
2023 Err(note_err) => {
2024 talk.turns.pop();
2044 Err(note_err).context(why)
2045 }
2046 };
2047 }
2048
2049 match failure {
2050 Some(why) => bail!("{why}"),
2051 None => Ok(()),
2052 }
2053}
2054
2055fn transcript(talk: &Talk, store: &Talks) -> String {
2058 let mut out = String::from(
2059 "This conversation cannot resume on the CLI's side, so here is \
2060 everything said so far; answer only the last message.\n",
2061 );
2062 for t in &talk.turns {
2063 let who = match t.who {
2064 Who::Operator => "operator",
2065 Who::Agent if t.body.starts_with(MAGI_NOTE) => "magi",
2066 Who::Agent => "you",
2067 };
2068 out.push_str(&format!("\n## {who}\n\n{}\n", t.body.trim()));
2069 out.push_str(&attachment_note(store, &talk.id, &t.attachments));
2070 }
2071 out
2072}
2073
2074fn attachment_note(store: &Talks, talk_id: &str, attachments: &[Attachment]) -> String {
2079 if attachments.is_empty() {
2080 return String::new();
2081 }
2082 let mut out = String::from(
2083 "\n\nThe operator attached the image(s) below to this message. Open \
2084 and look at each one before you answer.\n",
2085 );
2086 for att in attachments {
2087 if let Some(path) = store.attachment_path(talk_id, att) {
2088 out.push_str(&format!("\n- {} ({})", path.display(), att.mime));
2089 }
2090 }
2091 out.push('\n');
2092 out
2093}
2094
2095pub(crate) fn turn_access(talk_allow_write: bool, consulted: bool) -> (bool, bool) {
2106 (talk_allow_write || consulted, talk_allow_write)
2107}
2108
2109pub fn briefing(repo: &Path, language: &str, allow_write: bool) -> String {
2125 briefing_with(repo, language, allow_write, None, None)
2126}
2127
2128pub fn briefing_with(
2133 repo: &Path,
2134 language: &str,
2135 allow_write: bool,
2136 persona: Option<&crate::persona::Persona>,
2137 operator_name: Option<&str>,
2138) -> String {
2139 briefing_for(repo, language, allow_write, persona, operator_name, 1)
2140}
2141
2142pub const MAX_IMPLEMENTERS: u8 = 3;
2144
2145fn solo_implementers() -> u8 {
2146 1
2147}
2148
2149fn filing_flag(n: u8) -> String {
2152 if n <= 1 {
2153 "--solo".to_owned()
2154 } else {
2155 format!("--implementers {n}")
2156 }
2157}
2158
2159pub fn filing_update_block(n: u8) -> String {
2162 let flag = filing_flag(n);
2163 if n <= 1 {
2164 format!(
2165 "# Task filing update\n\nThe operator changed how many implementers tasks use. \
2166 From now on file with `magi task add {flag} --repo <repo> <instruction>` \
2167 (one implementer, straight into review).\n"
2168 )
2169 } else {
2170 format!(
2171 "# Task filing update\n\nThe operator changed how many implementers tasks use. \
2172 From now on file with `magi task add {flag} --repo <repo> <instruction>` \
2173 ({n} independent implementations compete). Do not pass `--solo` \
2174 together with `--implementers`; drop any earlier `--solo`.\n"
2175 )
2176 }
2177}
2178
2179pub fn check_implementers(n: u8, cfg: &crate::config::Config) -> std::result::Result<u8, String> {
2182 if !(1..=MAX_IMPLEMENTERS).contains(&n) {
2183 return Err(format!(
2184 "implementers must be 1..={MAX_IMPLEMENTERS}, got {n}"
2185 ));
2186 }
2187 if n > 1 {
2188 let mut cfg = cfg.clone();
2189 crate::queue::RunOverrides {
2190 candidates: Some(usize::from(n)),
2191 ..Default::default()
2192 }
2193 .apply(&mut cfg);
2194 cfg.resolve_roles()
2195 .map_err(|e| format!("{n} implementers is not allowed here: {e:#}"))?;
2196 }
2197 Ok(n)
2198}
2199
2200pub fn switch_implementers(talk: &mut Talk, store: &Talks, n: u8) -> Result<bool> {
2203 let _guard = store.guard()?;
2204 let mut fresh = store
2205 .get(&talk.id)
2206 .with_context(|| format!("talk {} was deleted", talk.short()))?;
2207 if fresh.implementers == n {
2208 *talk = fresh;
2209 return Ok(false);
2210 }
2211 let from = std::mem::replace(&mut fresh.implementers, n);
2212 fresh.implementers_dirty = true;
2213 fresh.turns.push(Turn {
2214 breaks: None,
2215 who: Who::Agent,
2216 body: format!("{MAGI_NOTE}implementers changed from {from} to {n}"),
2217 at: Timestamp::now(),
2218 attachments: Vec::new(),
2219 usage: None,
2220 });
2221 store.put(&mut fresh)?;
2222 *talk = fresh;
2223 Ok(true)
2224}
2225
2226pub fn briefing_for(
2229 repo: &Path,
2230 language: &str,
2231 allow_write: bool,
2232 persona: Option<&crate::persona::Persona>,
2233 operator_name: Option<&str>,
2234 implementers: u8,
2235) -> String {
2236 let flag = filing_flag(implementers);
2237 let shape = if implementers <= 1 {
2238 "Use --solo: it runs the task through one implementer \
2239 straight into review instead of the usual multi-agent competition, \
2240 which is the right shape for a change this conversation has already \
2241 settled, rather than one still worth several independent takes."
2242 .to_owned()
2243 } else {
2244 format!(
2245 "Use --implementers {implementers}: it has {implementers} \
2246 implementers work on the task independently and compete, which \
2247 is the right shape when several independent takes are worth \
2248 having. Never add --solo to it; the two do not go together."
2249 )
2250 };
2251 let write_policy = if allow_write {
2252 "Write access is enabled for this conversation (`allow_write = \
2253 true`), so you may write files - but only a small, \
2254 already-decided edit the operator names outright in this \
2255 conversation, not an implementation. This is a permission on the \
2256 conversation as a whole, not a property of whichever repository \
2257 it happened to start in: if the operator names a different \
2258 repository for that small edit, the policy allows it there too. \
2259 Your own tool may still confine writes to the repository this \
2260 conversation started in regardless - if a write elsewhere is \
2261 refused, say so plainly rather than working around it. Once you \
2262 have made an edit, say plainly what you edited. Anything bigger, \
2263 or anything still open-ended, still goes through the queue below \
2264 rather than being done here."
2265 } else {
2266 "Do not write files. Implementing a change is not this \
2267 conversation's job; a separate, blind competition of agents does \
2268 that, and a repository this conversation has already edited would \
2269 make their diffs unjudgeable."
2270 };
2271 let mut out = format!(
2272 "You are magi's standing conversation partner for its operator, who \
2273 usually has this open on a phone. Keep replies short: no preamble, \
2274 no restating what they just said.\n\n\
2275 # Repository\n\n{repo}\n\n\
2276 You may look around: read files, run shell commands, search history, \
2277 run tests - whatever answers the question. {write_policy}\n\n\
2278 A short, command-shaped message (\"list\", \"info <id>\", \"show \
2279 3cbf\") is almost always the operator asking you to look something \
2280 up, not an instruction to file - answer it yourself with `magi \
2281 list`, `magi show <id>`, `magi task list`, or the like, the same way \
2282 you would answer any other question in this conversation.\n\n\
2283 # When the operator wants something done\n\n\
2284 Run:\n\n\
2285 magi task add {flag} --repo {repo} <instruction>\n\n\
2286 and tell the operator the task id it prints, so they can follow it \
2287 from the Queue. If it refuses with a duplicate warning (the \
2288 instruction names a branch, commit or pull request that an \
2289 unfinished task, run or PR already owns), do not repeat it with \
2290 --force yourself: tell the operator what it matched and let them \
2291 decide. Write <instruction> so that an implementer who has \
2292 never seen this conversation can act on it alone - it is everything \
2293 they get. {shape}\n\n\
2294 If the operator asks for something in a different repository, \
2295 --repo does not have to be a full path: --repo owner/repo (or just \
2296 repo, when that is unambiguous) is resolved against local checkouts \
2297 the same way `magi repos` lists them. If the command fails because \
2298 nothing matches or more than one checkout shares that name, ask the \
2299 operator which repository they mean (or run `magi repos` yourself \
2300 to see the candidates) rather than guessing.\n\n\
2301 The current state of the code is whatever origin/main holds, not \
2302 whatever a working tree shows: a primary checkout often lags \
2303 upstream, sits on a detached HEAD and carries uncommitted changes. \
2304 Before answering about code, run `git fetch origin` in that \
2305 repository if it is cheap, then read through \
2306 `git show origin/main:<path>` or `git grep <pattern> origin/main`. \
2307 If the working tree differs, say so; if the fetch fails, say that \
2308 too, so the operator knows the answer may be stale.\n\n\
2309 If the operator attached an image (a screenshot, say) that the task \
2310 is about, pass it with `--attach <path>`, using the absolute path \
2311 the turn's attachment note gives; repeat the flag for several. \
2312 `magi task add {flag} --attach <path> <instruction>` copies the \
2313 file into the task, so the implementer receives it. Do not paste the \
2314 path into <instruction> instead: deleting this conversation deletes \
2315 its attachments, and then that path reaches no one.\n",
2316 repo = repo.display(),
2317 );
2318 out.push_str(&language_note(language));
2319 match (persona, operator_name) {
2320 (Some(p), n) => out.push_str(&crate::persona::section_for(p, n)),
2321 (None, Some(n)) => {
2322 out.push_str("\n# Addressing the operator\n");
2323 out.push_str(&crate::persona::addressing(n));
2324 }
2325 (None, None) => {}
2326 }
2327 out
2328}
2329
2330fn language_note(language: &str) -> String {
2333 if language.trim().is_empty() || language.eq_ignore_ascii_case("en") {
2334 String::new()
2335 } else {
2336 format!("\nHold this conversation in {language}.\n")
2337 }
2338}
2339
2340pub fn tasks_of(queue: &Queue, talk_id: &str) -> Vec<Task> {
2347 let mut tasks: Vec<Task> = queue
2348 .list()
2349 .into_iter()
2350 .filter(|t| matches!(&t.source, Source::Agent { run, .. } if run == talk_id))
2351 .collect();
2352 tasks.sort_unstable_by(|a, b| a.id.cmp(&b.id));
2353 tasks
2354}
2355
2356fn read_path(path: &Path) -> Result<Talk> {
2357 let body = std::fs::read_to_string(path).with_context(|| format!("read {}", path.display()))?;
2358 serde_json::from_str(&body).with_context(|| format!("parse {}", path.display()))
2359}
2360
2361const PUT_RETRIES: u32 = 5;
2364
2365fn write_atomic(tmp: &Path, path: &Path, body: &str) -> Result<()> {
2377 let mut last_err = None;
2378 for attempt in 0..PUT_RETRIES {
2379 if attempt > 0 {
2380 std::thread::sleep(Duration::from_millis(20 * u64::from(attempt)));
2381 }
2382 match try_write_atomic(tmp, path, body) {
2383 Ok(()) => return Ok(()),
2384 Err(e) => last_err = Some(e),
2385 }
2386 }
2387 Err(last_err.expect("the loop above always runs at least once"))
2388}
2389
2390fn try_write_atomic(tmp: &Path, path: &Path, body: &str) -> Result<()> {
2391 #[cfg(test)]
2392 if failpoint::take_forced_put_failure() {
2393 bail!("simulated write failure (test)");
2394 }
2395 std::fs::write(tmp, body).with_context(|| format!("write {}", tmp.display()))?;
2396 std::fs::rename(tmp, path).with_context(|| format!("replace {}", path.display()))?;
2397 Ok(())
2398}
2399
2400fn stash_lost_turn(store: &Talks, id: &str, stem: &str, reply: &Turn) -> Result<PathBuf> {
2405 let dir = store.artifacts_of(id);
2406 std::fs::create_dir_all(&dir).with_context(|| format!("create {}", dir.display()))?;
2407 let path = dir.join(format!("{stem}-lost.txt"));
2408 std::fs::write(&path, &reply.body).with_context(|| format!("write {}", path.display()))?;
2409 Ok(path)
2410}
2411
2412#[cfg(test)]
2419mod failpoint {
2420 use std::cell::Cell;
2421
2422 thread_local! {
2423 static FORCE_PUT_FAILURES: Cell<u32> = const { Cell::new(0) };
2424 static FORCE_NO_LINK: Cell<bool> = const { Cell::new(false) };
2425 }
2426
2427 pub(super) fn force_no_link(on: bool) {
2430 FORCE_NO_LINK.with(|c| c.set(on));
2431 }
2432
2433 pub(super) fn no_link_forced() -> bool {
2434 FORCE_NO_LINK.with(Cell::get)
2435 }
2436
2437 pub(super) fn force_put_failures(count: u32) {
2440 FORCE_PUT_FAILURES.with(|c| c.set(count));
2441 }
2442
2443 pub(super) fn take_forced_put_failure() -> bool {
2446 FORCE_PUT_FAILURES.with(|c| {
2447 let n = c.get();
2448 if n == 0 {
2449 false
2450 } else {
2451 c.set(n - 1);
2452 true
2453 }
2454 })
2455 }
2456}
2457
2458fn short(id: &str) -> &str {
2459 id.split('-').next_back().unwrap_or(id)
2460}
2461
2462fn new_id() -> String {
2463 let stamp = jiff::Zoned::now().strftime("%Y%m%d-%H%M%S");
2464 let seed = crate::rng::entropy();
2465 format!("{stamp}-{:04x}", (seed ^ (seed >> 32)) & 0xffff)
2466}
2467
2468fn attachment_ext(mime: &str) -> Option<&'static str> {
2473 match mime {
2474 "image/png" => Some("png"),
2475 "image/jpeg" => Some("jpg"),
2476 "image/gif" => Some("gif"),
2477 "image/webp" => Some("webp"),
2478 _ => None,
2479 }
2480}
2481
2482pub fn valid_attachment_id(id: &str) -> bool {
2487 id.len() == 32
2488 && id
2489 .bytes()
2490 .all(|b| b.is_ascii_digit() || (b'a'..=b'f').contains(&b))
2491}
2492
2493fn new_attachment_id() -> String {
2497 let mut r = crate::rng::SplitMix64::new(crate::rng::entropy());
2498 format!("{:016x}{:016x}", r.next_u64(), r.next_u64())
2499}
2500
2501#[cfg(test)]
2502mod tests {
2503 #[test]
2504 fn turn_access_lifts_the_sandbox_only_for_the_talk_opt_in() {
2505 use super::turn_access;
2506 assert_eq!(turn_access(false, false), (false, false));
2507 assert_eq!(turn_access(true, false), (true, true));
2508 assert_eq!(turn_access(false, true), (true, false));
2510 assert_eq!(turn_access(true, true), (true, true));
2511 }
2512
2513 #[test]
2514 fn the_briefing_points_at_origin_main_not_the_working_tree() {
2515 let b = briefing(Path::new("/r"), "en", false);
2516 assert!(b.contains("origin/main"));
2517 assert!(b.contains("git show origin/main:"));
2518 }
2519 use std::collections::BTreeMap;
2520
2521 use crate::config::{AgentChoice, AgentKind, AgentSpec, Graph};
2522 use crate::queue::{Queue, Source, Task};
2523
2524 use super::*;
2525
2526 fn ctx_agent(id: &str, model: Option<&str>) -> AgentSpec {
2527 AgentSpec {
2528 id: id.to_owned(),
2529 kind: AgentKind::Command,
2530 model: model.map(str::to_owned),
2531 command: Vec::new(),
2532 extra_args: Vec::new(),
2533 env: BTreeMap::new(),
2534 prompt_delivery: None,
2535 }
2536 }
2537
2538 fn ctx_talk(agent: &str, turns: Vec<Turn>) -> Talk {
2539 Talk {
2540 pending_breaks: None,
2541 schema: SCHEMA,
2542 id: "20260904-014455-ab12".to_owned(),
2543 repo: PathBuf::from("."),
2544 agent: agent.to_owned(),
2545 status: TalkStatus::Open,
2546 turns,
2547 pending: String::new(),
2548 pending_attachments: Vec::new(),
2549 fallback: false,
2550 persona: String::new(),
2551 persona_dirty: false,
2552 implementers: 1,
2553 implementers_dirty: false,
2554 created_at: Timestamp::now(),
2555 updated_at: Timestamp::now(),
2556 seat: SeatState::new(SEAT, agent, 1),
2557 }
2558 }
2559
2560 fn reply(body: &str, usage: Option<(u64, &str, Option<&str>)>) -> Turn {
2561 Turn {
2562 breaks: None,
2563 who: Who::Agent,
2564 body: body.to_owned(),
2565 at: Timestamp::now(),
2566 attachments: Vec::new(),
2567 usage: usage.map(|(t, a, m)| TurnUsage {
2568 context_tokens: t,
2569 agent: a.to_owned(),
2570 model: m.map(str::to_owned),
2571 }),
2572 }
2573 }
2574
2575 fn ctx_config(windows: &[(&str, u64)]) -> Config {
2576 Config {
2577 agents: vec![
2578 ctx_agent("small", Some("small-model")),
2579 ctx_agent("big", Some("big-model")),
2580 ctx_agent("plain", None),
2581 ],
2582 context_windows: windows.iter().map(|(k, v)| ((*k).to_owned(), *v)).collect(),
2583 ..Config::default()
2584 }
2585 }
2586
2587 #[test]
2588 fn context_usage_computes_percent_and_warns_at_eighty() {
2589 let cfg = ctx_config(&[("small-model", 1000)]);
2590 let at = |tokens| {
2591 let t = ctx_talk(
2592 "small",
2593 vec![reply("hi", Some((tokens, "small", Some("small-model"))))],
2594 );
2595 context_usage(&t, Some(&cfg))
2596 };
2597 let u = at(799);
2598 assert_eq!((u.percent, u.warn, u.window), (Some(79), false, Some(1000)));
2599 let u = at(800);
2600 assert_eq!((u.percent, u.warn), (Some(80), true));
2601 let u = at(1500);
2602 assert_eq!((u.percent, u.warn), (Some(150), true));
2603 assert!(!u.since_switch);
2604 }
2605
2606 #[test]
2607 fn context_usage_is_unknown_without_usage_and_never_looks_back() {
2608 let cfg = ctx_config(&[("small-model", 1000)]);
2609 let t = ctx_talk(
2610 "small",
2611 vec![
2612 reply("old", Some((900, "small", Some("small-model")))),
2613 reply("new", None),
2614 ],
2615 );
2616 let u = context_usage(&t, Some(&cfg));
2617 assert!(u.estimated);
2619 assert_ne!(u.tokens, Some(900));
2620 assert!(u.tokens.is_some());
2621 let t = ctx_talk(
2623 "small",
2624 vec![
2625 reply("old", Some((900, "small", Some("small-model")))),
2626 reply("magi: could not run agent", None),
2627 ],
2628 );
2629 assert_eq!(context_usage(&t, Some(&cfg)).tokens, Some(900));
2630 assert_eq!(
2631 context_usage(&ctx_talk("small", Vec::new()), Some(&cfg)).tokens,
2632 None
2633 );
2634 }
2635
2636 #[test]
2637 fn estimate_counts_chars_both_sides_and_standing_prompt() {
2638 let mut t = ctx_talk("small", vec![reply("abcdefg", None)]);
2639 assert_eq!(estimate_context_tokens(&t, 0), Some(2)); let op = Turn {
2641 who: Who::Operator,
2642 ..reply("abcdefg", None)
2643 };
2644 t.turns.push(op);
2645 assert_eq!(estimate_context_tokens(&t, 0), Some(4));
2646 assert!(
2647 estimate_context_tokens(&t, 700).unwrap() > estimate_context_tokens(&t, 0).unwrap()
2648 );
2649 let ja = ctx_talk("small", vec![reply("日本語日本語日", None)]);
2651 assert_eq!(estimate_context_tokens(&ja, 0), Some(2));
2652 let note = ctx_talk("small", vec![reply("magi: could not run agent", None)]);
2654 assert_eq!(estimate_context_tokens(¬e, 1000), None);
2655 assert_eq!(
2656 estimate_context_tokens(&ctx_talk("small", Vec::new()), 1000),
2657 None
2658 );
2659 }
2660
2661 #[test]
2662 fn context_usage_measured_wins_and_estimate_gets_percent_and_warn() {
2663 let cfg = ctx_config(&[("small-model", 1000)]);
2664 let t = ctx_talk(
2665 "small",
2666 vec![reply(
2667 &"x".repeat(5000),
2668 Some((10, "small", Some("small-model"))),
2669 )],
2670 );
2671 let u = context_usage(&t, Some(&cfg));
2672 assert_eq!((u.tokens, u.estimated), (Some(10), false));
2673 let t = ctx_talk("small", vec![reply(&"x".repeat(5000), None)]);
2674 let u = context_usage(&t, Some(&cfg));
2675 assert!(u.estimated && !u.since_switch);
2676 assert_eq!(u.window, Some(1000));
2677 assert!(u.warn && u.percent.unwrap() >= 80);
2678 let t = ctx_talk("small", vec![reply("hi", None)]);
2679 let u = context_usage(&t, Some(&cfg));
2680 assert!(u.estimated && u.percent.is_some());
2681 }
2682
2683 #[test]
2684 fn context_usage_without_a_window_shows_tokens_only() {
2685 let cfg = ctx_config(&[]);
2686 let t = ctx_talk("plain", vec![reply("hi", Some((5000, "plain", None)))]);
2688 let u = context_usage(&t, Some(&cfg));
2689 assert_eq!(
2690 (u.tokens, u.window, u.percent, u.warn),
2691 (Some(5000), None, None, false)
2692 );
2693 let t = ctx_talk(
2694 "small",
2695 vec![reply("hi", Some((5000, "small", Some("small-model"))))],
2696 );
2697 assert_eq!(context_usage(&t, Some(&cfg)).percent, None);
2698 assert_eq!(context_usage(&t, None).window, None);
2700 }
2701
2702 #[test]
2703 fn context_usage_switching_model_changes_the_denominator() {
2704 let cfg = ctx_config(&[("small-model", 1000), ("big-model", 10_000)]);
2705 let used = reply("hi", Some((900, "small", Some("small-model"))));
2706 let before = context_usage(&ctx_talk("small", vec![used.clone()]), Some(&cfg));
2707 assert_eq!(
2708 (before.percent, before.warn, before.since_switch),
2709 (Some(90), true, false)
2710 );
2711 let after = context_usage(&ctx_talk("big", vec![used]), Some(&cfg));
2714 assert_eq!(after.window, Some(10_000));
2715 assert_eq!(
2716 (after.percent, after.warn, after.since_switch),
2717 (Some(9), false, true)
2718 );
2719 assert_eq!(after.model.as_deref(), Some("big-model"));
2720 }
2721
2722 #[test]
2723 fn a_turn_recorded_before_usage_existed_still_reads() {
2724 let old = r#"{"who":"agent","body":"hi","at":"2026-09-04T01:44:55Z"}"#;
2725 let turn: Turn = serde_json::from_str(old).expect("old turn reads");
2726 assert!(turn.usage.is_none());
2727 let json = serde_json::to_string(&turn).expect("serialize");
2728 assert!(
2729 !json.contains("usage"),
2730 "absent usage is not written: {json}"
2731 );
2732 }
2733
2734 fn store() -> (tempfile::TempDir, Talks) {
2736 let tmp = tempfile::tempdir().expect("tempdir");
2737 let talks = Talks::at(tmp.path().join("talks"));
2738 (tmp, talks)
2739 }
2740
2741 fn mock_agent(dir: &Path, script: &str, env: BTreeMap<String, String>) -> AgentSpec {
2745 let path = dir.join("mock-talk-agent.sh");
2746 std::fs::write(&path, script).expect("write mock");
2747 AgentSpec {
2748 id: "mock".to_owned(),
2749 kind: AgentKind::Command,
2750 model: None,
2751 command: vec!["sh".to_owned(), path.to_string_lossy().into_owned()],
2752 extra_args: Vec::new(),
2753 env,
2754 prompt_delivery: None,
2755 }
2756 }
2757
2758 fn config(spec: AgentSpec) -> Config {
2759 Config {
2760 agents: vec![spec],
2761 graph: Graph {
2762 language: "en".to_owned(),
2763 ..Graph::default()
2764 },
2765 ..Config::default()
2766 }
2767 }
2768
2769 const REPLY: &str = "#!/bin/sh\ncat >/dev/null\nprintf '%s\\n' \"$MOCK_REPLY\"\n";
2771
2772 const BROKEN: &str = "#!/bin/sh\ncat >/dev/null\nexit 3\n";
2774
2775 const ECHO: &str = "#!/bin/sh\ncat\n";
2778
2779 fn env(reply: &str) -> BTreeMap<String, String> {
2780 BTreeMap::from([("MOCK_REPLY".to_owned(), reply.to_owned())])
2781 }
2782
2783 #[test]
2784 fn the_frozen_json_field_names_round_trip_through_disk() {
2785 let (tmp, talks) = store();
2786 let mut talk = Talk {
2787 pending_breaks: None,
2788 schema: SCHEMA,
2789 id: "20260904-014455-ab12".to_owned(),
2790 repo: tmp.path().to_owned(),
2791 agent: "sonnet".to_owned(),
2792 status: TalkStatus::Open,
2793 turns: Vec::new(),
2794 pending: String::new(),
2795 pending_attachments: Vec::new(),
2796 fallback: false,
2797 persona: String::new(),
2798 persona_dirty: false,
2799 implementers: 1,
2800 implementers_dirty: false,
2801 created_at: Timestamp::now(),
2802 updated_at: Timestamp::now(),
2803 seat: SeatState::new(SEAT, "sonnet", 7),
2804 };
2805 talks.put(&mut talk).expect("put");
2806
2807 let raw = std::fs::read_to_string(talks.path_of(&talk.id)).expect("read back");
2808 let v: serde_json::Value = serde_json::from_str(&raw).expect("parse");
2809 for field in [
2810 "schema",
2811 "id",
2812 "repo",
2813 "agent",
2814 "status",
2815 "turns",
2816 "created_at",
2817 "updated_at",
2818 ] {
2819 assert!(v.get(field).is_some(), "missing field `{field}`");
2820 }
2821 assert_eq!(v["schema"], 1);
2822 assert_eq!(v["status"], "open");
2823
2824 let back = talks.get(&talk.id).expect("get");
2825 assert_eq!(back.id, talk.id);
2826 assert_eq!(back.status, TalkStatus::Open);
2827 }
2828
2829 #[test]
2830 fn opening_a_talk_takes_no_agent_turn() {
2831 let (tmp, talks) = store();
2832 let spec = mock_agent(tmp.path(), BROKEN, BTreeMap::new());
2836 let cfg = config(spec);
2837
2838 let talk = begin(&talks, &cfg, tmp.path().to_owned(), None).expect("begin");
2839 assert_eq!(talk.status, TalkStatus::Open);
2840 assert!(talk.turns.is_empty(), "nothing has been said yet");
2841
2842 let on_disk = talks.get(&talk.id).expect("get");
2843 assert_eq!(on_disk.turns.len(), 0);
2844 }
2845
2846 #[test]
2854 fn chatter_wins_when_set_and_falls_back_to_pick_s_default_order_otherwise() {
2855 let (tmp, talks) = store();
2856 let first_spec = mock_agent(tmp.path(), BROKEN, BTreeMap::new());
2857 let mut chatter_spec = mock_agent(tmp.path(), BROKEN, BTreeMap::new());
2858 chatter_spec.id = "chatter-mock".to_owned();
2859
2860 let mut cfg = Config {
2861 agents: vec![first_spec.clone(), chatter_spec.clone()],
2862 graph: Graph {
2863 language: "en".to_owned(),
2864 ..Graph::default()
2865 },
2866 ..Config::default()
2867 };
2868 cfg.roles.chatter = Some(chatter_spec.id.as_str().into());
2869
2870 let talk =
2871 begin(&talks, &cfg, tmp.path().to_owned(), None).expect("begin with chatter set");
2872 assert_eq!(talk.agent, chatter_spec.id, "an explicit chatter must win");
2873
2874 cfg.roles.chatter = None;
2875 let fallback =
2876 begin(&talks, &cfg, tmp.path().to_owned(), None).expect("begin with chatter unset");
2877 assert_eq!(
2878 fallback.agent, first_spec.id,
2879 "unset chatter must fall back to agent::pick's own default order"
2880 );
2881 }
2882
2883 #[test]
2886 fn a_talk_recorded_without_attachments_still_reads() {
2887 let (tmp, talks) = store();
2888 let path = talks.path_of("20260904-014455-ab12");
2889 std::fs::create_dir_all(talks.root()).expect("talks dir");
2890 std::fs::write(
2891 &path,
2892 serde_json::json!({
2893 "schema": 1,
2894 "id": "20260904-014455-ab12",
2895 "repo": tmp.path(),
2896 "agent": "sonnet",
2897 "status": "open",
2898 "turns": [
2899 { "who": "operator", "body": "still there?",
2900 "at": Timestamp::now().to_string() },
2901 ],
2902 "created_at": Timestamp::now().to_string(),
2903 "updated_at": Timestamp::now().to_string(),
2904 "seat": SeatState::new(SEAT, "sonnet", 7),
2905 })
2906 .to_string(),
2907 )
2908 .expect("write pre-attachments talk");
2909
2910 let talk = talks.get("20260904-014455-ab12").expect("must still read");
2911 assert!(talk.turns[0].attachments.is_empty());
2912 }
2913
2914 fn lease_store() -> (tempfile::TempDir, Talks, Talks) {
2915 let tmp = tempfile::TempDir::new().expect("tmp");
2916 let root = tmp.path().join("talks");
2917 (tmp, Talks::at(root.clone()), Talks::at(root))
2918 }
2919
2920 #[test]
2921 fn two_starters_on_one_talk_one_wins_and_the_other_is_refused() {
2922 let (_tmp, a, b) = lease_store();
2923 let won = a.claim_turn("t1").expect("claim").expect("first wins");
2924 assert!(
2925 b.claim_turn("t1").expect("claim").is_none(),
2926 "second is refused"
2927 );
2928 assert!(b.turn_held("t1"));
2929 assert!(
2930 b.claim_turn("t2").expect("claim").is_some(),
2931 "other talks are free"
2932 );
2933 drop(won);
2934 }
2935
2936 #[test]
2937 fn a_stale_lease_is_taken_over_and_the_old_guard_cannot_release_it() {
2938 let (_tmp, a, b) = lease_store();
2939 let old = a.claim_turn("t1").expect("claim").expect("held");
2940 let later = Timestamp::now()
2941 .checked_add(jiff::SignedDuration::from_secs(
2942 crate::ask::LEASE_TTL.as_secs() as i64 + 5,
2943 ))
2944 .expect("later");
2945 let new = b
2946 .claim_turn_at("t1", later)
2947 .expect("claim")
2948 .expect("a stale lease is taken over");
2949 drop(old);
2950 assert!(a.turn_held("t1"), "the old guard left the new lease alone");
2951 assert!(new.beat().expect("beat"), "the new owner still beats");
2952 drop(new);
2953 assert!(!a.turn_held("t1"));
2954 }
2955
2956 #[tokio::test]
2957 async fn a_turn_whose_lease_was_taken_over_is_stopped() {
2958 let (_tmp, a, b) = lease_store();
2959 let old = a.claim_turn("t1").expect("claim").expect("held");
2960 let later = Timestamp::now()
2961 .checked_add(jiff::SignedDuration::from_secs(
2962 crate::ask::LEASE_TTL.as_secs() as i64 + 5,
2963 ))
2964 .expect("later");
2965 let _new = b
2966 .claim_turn_at("t1", later)
2967 .expect("claim")
2968 .expect("taken over");
2969 let out = old
2970 .beating_every(Duration::from_millis(10), std::future::pending::<()>())
2971 .await;
2972 assert!(out.is_err(), "the displaced turn must stop, not run on");
2973 }
2974
2975 #[tokio::test]
2976 async fn a_turn_that_finishes_is_returned_and_keeps_its_lease_beating() {
2977 let (_tmp, a, _b) = lease_store();
2978 let lease = a.claim_turn("t1").expect("claim").expect("held");
2979 let out = lease
2980 .beating_every(Duration::from_millis(5), async {
2981 tokio::time::sleep(Duration::from_millis(40)).await;
2982 7
2983 })
2984 .await
2985 .expect("still ours");
2986 assert_eq!(out, 7);
2987 assert!(a.turn_held("t1"));
2988 }
2989
2990 #[test]
2991 fn an_unreadable_lease_counts_as_stale() {
2992 let (_tmp, a, b) = lease_store();
2993 std::fs::create_dir_all(a.root()).expect("dir");
2994 std::fs::write(a.turn_path("t1"), "not json").expect("write");
2995 assert!(!a.turn_held("t1"));
2996 assert!(b.claim_turn("t1").expect("claim").is_some());
2997 }
2998
2999 #[test]
3000 fn without_hard_links_a_claim_is_still_exclusive_and_a_young_placeholder_blocks() {
3001 let (_tmp, a, b) = lease_store();
3002 failpoint::force_no_link(true);
3003 let held = a.claim_turn("t1").expect("claim").expect("first wins");
3004 assert!(b.claim_turn("t1").expect("claim").is_none());
3005 assert!(a.turn_held("t1"));
3006 drop(held);
3007 assert!(!a.turn_held("t1"));
3008 let path = a.turn_path("t1");
3010 assert!(create_exclusive(&path, "").expect("placeholder"));
3011 assert!(b.claim_turn("t1").expect("claim").is_none(), "young: held");
3012 age_file(&path);
3013 assert!(b.claim_turn("t1").expect("claim").is_some(), "old: stale");
3014 let lease = a.turn_path("t2");
3016 let lock = TurnLock::take(&lease).expect("take").expect("lock");
3017 assert!(TurnLock::take(&lease).expect("take").is_none());
3018 drop(lock);
3019 assert!(TurnLock::take(&lease).expect("take").is_some());
3020 failpoint::force_no_link(false);
3021 }
3022
3023 #[test]
3024 fn remove_if_carries_removes_only_the_expected_content() {
3025 let (_tmp, a, _b) = lease_store();
3026 std::fs::create_dir_all(a.root()).expect("dir");
3027 let p = a.root().join("x.turn.lock");
3028 std::fs::write(&p, "mine").expect("write");
3029 assert!(!remove_if_carries(&p, "other"));
3030 assert_eq!(std::fs::read_to_string(&p).expect("kept"), "mine");
3031 assert!(remove_if_carries(&p, "mine"));
3032 assert!(!p.exists());
3033 assert!(!remove_if_carries(&p, "mine"), "absent is not a removal");
3034 }
3035
3036 #[test]
3037 fn a_dropped_lock_does_not_remove_a_lock_taken_over_since() {
3038 let (_tmp, a, _b) = lease_store();
3039 std::fs::create_dir_all(a.root()).expect("dir");
3040 let lease = a.turn_path("t1");
3041 let lock = TurnLock::take(&lease).expect("take").expect("lock");
3042 let path = lock.path.clone();
3043 std::fs::write(&path, "someone-else").expect("replace");
3044 drop(lock);
3045 assert_eq!(
3046 std::fs::read_to_string(&path).expect("kept"),
3047 "someone-else"
3048 );
3049 }
3050
3051 #[test]
3052 fn concurrent_takeovers_of_a_stale_lease_have_one_winner() {
3053 let (_tmp, a, _b) = lease_store();
3054 drop(a.claim_turn("t1").expect("claim").expect("held"));
3055 std::fs::write(
3056 a.turn_path("t1"),
3057 serde_json::to_string(&TurnRecord {
3058 token: "gone".into(),
3059 pid: 1,
3060 beat_at: Timestamp::from_second(1).expect("ts"),
3061 })
3062 .expect("json"),
3063 )
3064 .expect("write");
3065 let wins: Vec<_> = std::thread::scope(|sc| {
3066 let hs: Vec<_> = (0..8)
3067 .map(|_| {
3068 let s = a.clone();
3069 sc.spawn(move || s.claim_turn("t1").expect("claim"))
3070 })
3071 .collect();
3072 hs.into_iter().map(|h| h.join().expect("join")).collect()
3073 });
3074 assert_eq!(wins.iter().filter(|w| w.is_some()).count(), 1);
3075 }
3076
3077 fn age_file(path: &Path) {
3078 let f = std::fs::OpenOptions::new()
3079 .write(true)
3080 .open(path)
3081 .expect("open");
3082 f.set_modified(std::time::SystemTime::now() - std::time::Duration::from_secs(60))
3083 .expect("age");
3084 }
3085
3086 #[test]
3087 fn a_late_taker_of_a_broken_lock_cannot_disturb_its_replacement() {
3088 let (_tmp, a, _b) = lease_store();
3089 let lease = a.turn_path("t1");
3090 let lock = lease.with_extension("turn.lock");
3091 std::fs::create_dir_all(lock.parent().expect("dir")).expect("dir");
3092 std::fs::write(&lock, "t1-dead").expect("dead lock");
3093 age_file(&lock);
3094 let b = TurnLock::take(&lease).expect("take").expect("b wins");
3096 let fresh = std::fs::read_to_string(&lock).expect("read");
3097 assert_eq!(fresh, b.token);
3098 let ticket = std::fs::read_dir(lock.parent().expect("dir"))
3101 .expect("dir")
3102 .flatten()
3103 .map(|e| e.path())
3104 .find(|p| p.to_string_lossy().ends_with(".break.0"))
3105 .expect("ticket");
3106 assert!(!create_exclusive(&ticket, "").expect("ticket"));
3107 assert!(TurnLock::take(&lease).expect("take").is_none());
3108 assert_eq!(std::fs::read_to_string(&lock).expect("read"), fresh);
3109 age_file(&lock);
3111 age_file(&ticket);
3112 std::mem::forget(b);
3113 let c = TurnLock::take(&lease)
3114 .expect("take")
3115 .expect("next generation");
3116 assert_ne!(c.token, fresh);
3117 }
3118
3119 #[test]
3120 fn a_live_ticket_blocks_and_a_stale_one_hands_over_to_the_next_generation() {
3121 let (_tmp, a, _b) = lease_store();
3122 let lease = a.turn_path("t1");
3123 let lock = lease.with_extension("turn.lock");
3124 std::fs::create_dir_all(lock.parent().expect("dir")).expect("dir");
3125 std::fs::write(&lock, "t1-dead").expect("dead lock");
3126 age_file(&lock);
3127 let t0 = lock.with_extension("lock.t1-dead.break.0");
3128 assert!(create_exclusive(&t0, "").expect("ticket"));
3129 assert!(TurnLock::take(&lease).expect("take").is_none());
3131 assert_eq!(std::fs::read_to_string(&lock).expect("read"), "t1-dead");
3132 age_file(&t0);
3134 let c = TurnLock::take(&lease).expect("take").expect("generation 1");
3135 assert_eq!(std::fs::read_to_string(&lock).expect("read"), c.token);
3136 assert!(lock.with_extension("lock.t1-dead.break.1").exists());
3137 assert!(TurnLock::take(&lease).expect("take").is_none());
3138 }
3139
3140 #[test]
3141 fn exhausted_ticket_generations_recover_once_the_sweep_ages_them_out() {
3142 let (_tmp, a, _b) = lease_store();
3143 let lease = a.turn_path("t1");
3144 let lock = lease.with_extension("turn.lock");
3145 std::fs::create_dir_all(lock.parent().expect("dir")).expect("dir");
3146 std::fs::write(&lock, "t1-dead").expect("dead lock");
3147 age_file(&lock);
3148 let tickets: Vec<_> = (0..TICKET_GENERATIONS)
3149 .map(|n| lock.with_extension(format!("lock.t1-dead.break.{n}")))
3150 .collect();
3151 for t in &tickets {
3152 assert!(create_exclusive(t, "").expect("ticket"));
3153 age_file(t);
3154 }
3155 assert!(TurnLock::take(&lease).expect("take").is_none());
3157 assert!(tickets.iter().all(|t| t.exists()));
3158 for t in &tickets {
3160 let f = std::fs::OpenOptions::new()
3161 .write(true)
3162 .open(t)
3163 .expect("open");
3164 f.set_modified(std::time::SystemTime::now() - TICKET_SWEEP_AGE * 2)
3165 .expect("age");
3166 }
3167 assert!(TurnLock::take(&lease).expect("take").is_none());
3168 assert!(TurnLock::take(&lease).expect("take").is_some());
3169 }
3170
3171 #[test]
3172 fn an_aged_empty_lock_is_broken() {
3173 let (_tmp, a, _b) = lease_store();
3174 let lease = a.turn_path("t1");
3175 let lock = lease.with_extension("turn.lock");
3176 std::fs::create_dir_all(lock.parent().expect("dir")).expect("dir");
3177 std::fs::write(&lock, "").expect("empty lock");
3178 age_file(&lock);
3179 assert!(TurnLock::take(&lease).expect("take").is_some());
3180 }
3181
3182 #[test]
3183 fn queued_text_is_durable_combined_and_drained_as_one_operator_turn() {
3184 let (tmp, talks) = store();
3185 let cfg = config(mock_agent(tmp.path(), REPLY, env("reply")));
3186 let mut talk = begin(&talks, &cfg, tmp.path().to_owned(), None).expect("begin");
3187
3188 queue(&mut talk, &talks, "first", Vec::new()).expect("queue first");
3189 queue(&mut talk, &talks, "second", Vec::new()).expect("queue second");
3190 let saved = talks.get(&talk.id).expect("reload queued talk");
3191 assert_eq!(saved.pending, "first\n\nsecond");
3192 assert_eq!(saved.pending_breaks, Some(vec!["first\n\n".len()]));
3193 assert!(saved.turns.is_empty(), "a draft is not a transcript turn");
3194
3195 let drained = drain(&mut talk, &talks).expect("drain");
3196 assert_eq!(drained.as_deref(), Some("first\n\nsecond"));
3197 let saved = talks.get(&talk.id).expect("reload drained talk");
3198 assert!(saved.pending.is_empty());
3199 assert_eq!(saved.turns.len(), 1);
3200 assert_eq!(saved.turns[0].body, "first\n\nsecond");
3201 assert_eq!(saved.turns[0].breaks, Some(vec!["first\n\n".len()]));
3202 assert_eq!(saved.pending_breaks, None);
3203 }
3204
3205 #[test]
3206 fn editing_a_queued_draft_preserves_its_attachments_and_rejects_a_stale_snapshot() {
3207 let (tmp, talks) = store();
3208 let cfg = config(mock_agent(tmp.path(), REPLY, env("reply")));
3209 let mut talk = begin(&talks, &cfg, tmp.path().to_owned(), None).expect("begin");
3210 let attachment = Attachment {
3211 id: "a".repeat(32),
3212 name: "shot.png".to_owned(),
3213 mime: "image/png".to_owned(),
3214 bytes: 3,
3215 };
3216
3217 queue(&mut talk, &talks, "first", vec![attachment.clone()]).expect("queue");
3218 assert!(
3219 edit_pending_text(
3220 &mut talk,
3221 &talks,
3222 "corrected",
3223 "first",
3224 std::slice::from_ref(&attachment.id),
3225 )
3226 .expect("edit")
3227 );
3228 let saved = talks.get(&talk.id).expect("reload edited draft");
3229 assert_eq!(saved.pending, "corrected");
3230 assert_eq!(saved.pending_attachments, vec![attachment]);
3231
3232 queue(&mut talk, &talks, "later", Vec::new()).expect("queue concurrent draft");
3233 assert!(
3234 !edit_pending_text(
3235 &mut talk,
3236 &talks,
3237 "stale edit",
3238 "corrected",
3239 &["a".repeat(32)],
3240 )
3241 .expect("stale edit is a conflict")
3242 );
3243 assert_eq!(
3244 talks.get(&talk.id).expect("reload after conflict").pending,
3245 "corrected\n\nlater"
3246 );
3247 assert!(
3248 !clear_pending_if_matches(&mut talk, &talks, "corrected", &["a".repeat(32)])
3249 .expect("stale clear is a conflict")
3250 );
3251 assert_eq!(
3252 talks
3253 .get(&talk.id)
3254 .expect("reload after stale clear")
3255 .pending,
3256 "corrected\n\nlater"
3257 );
3258 }
3259
3260 #[tokio::test]
3261 async fn a_reply_save_preserves_pending_accepted_while_the_cli_runs() {
3262 let (tmp, talks) = store();
3263 let slow = "#!/bin/sh\ncat >/dev/null\nsleep 0.1\nprintf reply\n";
3264 let cfg = config(mock_agent(tmp.path(), slow, BTreeMap::new()));
3265 let mut running = begin(&talks, &cfg, tmp.path().to_owned(), None).expect("begin");
3266 let id = running.id.clone();
3267 let first = record(&mut running, &talks, "first", Vec::new()).expect("record");
3268
3269 let response_talks = talks.clone();
3270 let response_cfg = cfg.clone();
3271 let reply = tokio::spawn(async move {
3272 respond(
3273 &response_talks.claim_turn(&running.id).unwrap().unwrap(),
3274 &mut running,
3275 &response_talks,
3276 &response_cfg,
3277 &first,
3278 )
3279 .await
3280 });
3281 tokio::time::sleep(std::time::Duration::from_millis(20)).await;
3282
3283 let mut queued = talks.get(&id).expect("queued handle");
3284 queue(&mut queued, &talks, "next", Vec::new()).expect("queue");
3285 reply.await.expect("join").expect("reply");
3286
3287 let saved = talks.get(&id).expect("reload");
3288 assert_eq!(saved.pending, "next");
3289 assert_eq!(saved.pending_breaks, Some(Vec::new()));
3290 assert_eq!(saved.turns.len(), 2, "operator message and reply remain");
3291 }
3292
3293 fn counting_agent(dir: &Path, id: &str, body: &str) -> AgentSpec {
3296 let calls = dir.join(format!("{id}.calls"));
3297 let script = format!(
3298 "#!/bin/sh\necho x >> '{}'\n{body}\n",
3299 calls.to_string_lossy()
3300 );
3301 let path = dir.join(format!("mock-{id}.sh"));
3302 std::fs::write(&path, script).expect("write mock");
3303 AgentSpec {
3304 id: id.to_owned(),
3305 kind: AgentKind::Command,
3306 model: None,
3307 command: vec!["sh".to_owned(), path.to_string_lossy().into_owned()],
3308 extra_args: Vec::new(),
3309 env: BTreeMap::new(),
3310 prompt_delivery: None,
3311 }
3312 }
3313
3314 fn calls(dir: &Path, id: &str) -> usize {
3315 std::fs::read_to_string(dir.join(format!("{id}.calls"))).map_or(0, |s| s.lines().count())
3316 }
3317
3318 fn chain_config(specs: Vec<AgentSpec>, ids: &[&str]) -> Config {
3319 let mut cfg = config(specs[0].clone());
3320 cfg.agents = specs;
3321 cfg.roles.chatter = Some(AgentChoice::Chain(
3322 ids.iter().map(|s| (*s).to_owned()).collect(),
3323 ));
3324 cfg
3325 }
3326
3327 #[tokio::test]
3328 async fn a_chatter_chain_falls_back_resends_the_transcript_and_sticks() {
3329 let (tmp, talks) = store();
3330 let a = counting_agent(tmp.path(), "a", "cat >/dev/null\nexit 3");
3331 let b = counting_agent(tmp.path(), "b", "cat");
3332 let cfg = chain_config(vec![a, b], &["a", "b"]);
3333 let mut talk = begin(&talks, &cfg, tmp.path().to_owned(), None).expect("begin");
3334 assert_eq!(talk.agent, "a");
3335
3336 say(
3337 &talks.claim_turn(&talk.id).unwrap().unwrap(),
3338 &mut talk,
3339 &talks,
3340 &cfg,
3341 "hello there",
3342 Vec::new(),
3343 )
3344 .await
3345 .expect("turn");
3346 assert_eq!(calls(tmp.path(), "a"), 1, "each id is tried once");
3347 assert_eq!(calls(tmp.path(), "b"), 1);
3348 assert_eq!(talk.agent, "b", "the switch persists");
3349 assert!(talks.get(&talk.id).unwrap().agent == "b");
3350 let reply = talk.turns.last().unwrap();
3351 assert!(reply.body.contains("hello there"));
3352 assert!(
3353 reply.body.contains("magi task add --solo"),
3354 "a fresh seat gets the full briefing"
3355 );
3356 assert!(
3357 talk.turns
3358 .iter()
3359 .any(|t| t.body.contains("agent changed from a to b")),
3360 "the switch is noted"
3361 );
3362 }
3363
3364 #[tokio::test]
3365 async fn an_exhausted_chatter_chain_fails_like_a_single_seat_and_stays_put() {
3366 let (tmp, talks) = store();
3367 let a = counting_agent(tmp.path(), "a", "cat >/dev/null\nexit 3");
3368 let b = counting_agent(tmp.path(), "b", "cat >/dev/null\nexit 4");
3369 let cfg = chain_config(vec![a, b], &["a", "b", "a"]);
3370 let mut talk = begin(&talks, &cfg, tmp.path().to_owned(), None).expect("begin");
3371
3372 let err = say(
3373 &talks.claim_turn(&talk.id).unwrap().unwrap(),
3374 &mut talk,
3375 &talks,
3376 &cfg,
3377 "hi",
3378 Vec::new(),
3379 )
3380 .await
3381 .expect_err("every agent failed");
3382 assert!(err.to_string().contains("`a`"), "{err:#}");
3383 assert_eq!(calls(tmp.path(), "a"), 1);
3384 assert_eq!(calls(tmp.path(), "b"), 1);
3385 assert_eq!(talk.agent, "a", "an exhausted chain leaves the agent alone");
3386 }
3387
3388 #[test]
3389 fn a_chatter_chain_skips_an_unknown_id_at_begin() {
3390 let (tmp, talks) = store();
3391 let b = counting_agent(tmp.path(), "b", "cat");
3392 let cfg = chain_config(vec![b], &["ghost", "b"]);
3393 let talk = begin(&talks, &cfg, tmp.path().to_owned(), None).expect("begin");
3394 assert_eq!(talk.agent, "b");
3395 }
3396
3397 #[tokio::test]
3398 async fn an_explicit_agent_inside_the_chatter_chain_stays_pinned() {
3399 let (tmp, talks) = store();
3400 let a = counting_agent(tmp.path(), "a", "cat >/dev/null\nexit 3");
3401 let b = counting_agent(tmp.path(), "b", "cat");
3402 let cfg = chain_config(vec![a, b], &["a", "b"]);
3403 let mut talk = begin(&talks, &cfg, tmp.path().to_owned(), Some("a")).expect("begin");
3404 say(
3405 &talks.claim_turn(&talk.id).unwrap().unwrap(),
3406 &mut talk,
3407 &talks,
3408 &cfg,
3409 "hi",
3410 Vec::new(),
3411 )
3412 .await
3413 .expect_err("a alone, and it fails");
3414 assert_eq!(calls(tmp.path(), "b"), 0);
3415 assert_eq!(talk.agent, "a");
3416 }
3417
3418 #[tokio::test]
3419 async fn an_explicit_agent_does_not_borrow_the_chatter_chain() {
3420 let (tmp, talks) = store();
3421 let a = counting_agent(tmp.path(), "a", "cat >/dev/null\nexit 3");
3422 let b = counting_agent(tmp.path(), "b", "cat");
3423 let c = counting_agent(tmp.path(), "c", "cat >/dev/null\nexit 3");
3424 let cfg = chain_config(vec![a, b, c], &["a", "b"]);
3425 let mut talk = begin(&talks, &cfg, tmp.path().to_owned(), Some("c")).expect("begin");
3426 say(
3427 &talks.claim_turn(&talk.id).unwrap().unwrap(),
3428 &mut talk,
3429 &talks,
3430 &cfg,
3431 "hi",
3432 Vec::new(),
3433 )
3434 .await
3435 .expect_err("c alone, and it fails");
3436 assert_eq!(calls(tmp.path(), "b"), 0);
3437 }
3438
3439 #[test]
3440 fn briefing_names_the_operator_with_or_without_a_persona() {
3441 let plain = briefing(Path::new("/repo"), "en", false);
3442 let named = briefing_with(Path::new("/repo"), "en", false, None, Some("Commander"));
3443 assert!(named.starts_with(&plain));
3444 assert!(named.contains("# Addressing the operator"));
3445 assert!(named.contains("\"Commander\""));
3446 let rei = crate::persona::builtin_catalog()
3447 .into_iter()
3448 .find(|p| p.id == "rei")
3449 .expect("rei");
3450 let with = briefing_with(
3451 Path::new("/repo"),
3452 "en",
3453 false,
3454 Some(&rei),
3455 Some("Commander"),
3456 );
3457 assert!(with.contains("\"Commander\""));
3458 assert!(!with.contains("# Addressing the operator"));
3459 }
3460
3461 #[test]
3462 fn briefing_carries_a_persona_section_only_when_one_is_chosen() {
3463 let plain = briefing(Path::new("/repo"), "en", false);
3464 assert_eq!(
3465 plain,
3466 briefing_with(Path::new("/repo"), "en", false, None, None)
3467 );
3468 assert!(!plain.contains("Persona"));
3469 let rei = crate::persona::builtin_catalog()
3470 .into_iter()
3471 .find(|p| p.id == "rei")
3472 .expect("rei");
3473 let with = briefing_with(Path::new("/repo"), "en", false, Some(&rei), None);
3474 assert!(with.starts_with(&plain), "the plain briefing is untouched");
3475 assert!(with.contains("# Persona (tone only)"));
3476 assert!(with.contains("TONE ONLY"));
3477 assert!(with.contains("task ids"));
3478 assert!(with.contains("write policy"));
3479 assert!(with.contains("`magi task add`"));
3480 assert!(with.contains("Rei Ayanami"));
3481 }
3482
3483 #[test]
3484 fn a_talk_written_before_personas_still_loads() {
3485 let (tmp, talks) = store();
3486 let spec = mock_agent(tmp.path(), ECHO, BTreeMap::new());
3487 let cfg = config(spec);
3488 let talk = begin(&talks, &cfg, tmp.path().to_owned(), None).expect("begin");
3489 let path = talks.root.join(format!("{}.json", talk.id));
3490 let mut v: serde_json::Value =
3491 serde_json::from_str(&std::fs::read_to_string(&path).unwrap()).unwrap();
3492 v.as_object_mut().unwrap().remove("persona");
3493 v.as_object_mut().unwrap().remove("persona_dirty");
3494 std::fs::write(&path, v.to_string()).unwrap();
3495 let loaded = talks.get(&talk.id).expect("old record loads");
3496 assert_eq!(loaded.persona, "");
3497 assert!(!loaded.persona_dirty);
3498 }
3499
3500 #[test]
3501 fn the_briefing_files_with_solo_or_the_chosen_implementer_count() {
3502 let repo = Path::new("/repo");
3503 let solo = briefing_for(repo, "", false, None, None, 1);
3504 assert_eq!(solo, briefing(repo, "", false));
3505 assert!(solo.contains("magi task add --solo --repo"));
3506 assert!(solo.contains("magi task add --solo --attach"));
3507 for n in [2u8, 3] {
3508 let b = briefing_for(repo, "", false, None, None, n);
3509 assert!(
3510 b.contains(&format!("magi task add --implementers {n} --repo")),
3511 "{b}"
3512 );
3513 assert!(b.contains(&format!("magi task add --implementers {n} --attach")));
3514 assert!(!b.contains("task add --solo"), "{b}");
3515 assert!(!b.contains("Use --solo"), "{b}");
3516 }
3517 }
3518
3519 #[test]
3520 fn begin_with_applies_remembered_choices_and_drops_unfit_ones_alone() {
3521 let (tmp, talks) = store();
3522 let first = mock_agent(tmp.path(), ECHO, BTreeMap::new());
3523 let mut second = first.clone();
3524 second.id = "second".to_owned();
3525 let mut cfg = config(first.clone());
3526 cfg.agents.push(second.clone());
3527 let open = |p: &Preferred| {
3528 begin_with(&talks, &cfg, tmp.path().to_owned(), None, p).expect("begin")
3529 };
3530
3531 let talk = open(&Preferred {
3532 agent: Some("second".into()),
3533 persona: Some("rei".into()),
3534 implementers: Some(i64::from(check_implementers(3, &cfg).unwrap_or(1))),
3535 });
3536 assert_eq!(talk.agent, "second");
3537 assert!(
3538 !talk.fallback,
3539 "a remembered non-head agent is an explicit choice"
3540 );
3541 assert_eq!(talk.persona, "rei");
3542 assert_eq!(talk.implementers, check_implementers(3, &cfg).unwrap_or(1));
3543 assert!(!talk.persona_dirty && !talk.implementers_dirty);
3544
3545 let head = open(&Preferred {
3547 agent: Some(first.id.clone()),
3548 ..Preferred::default()
3549 });
3550 assert!(head.fallback);
3551
3552 for implementers in [0, 4, -1, 1000] {
3554 let t = open(&Preferred {
3555 agent: Some("gone".into()),
3556 persona: Some("no-such-persona".into()),
3557 implementers: Some(implementers),
3558 });
3559 assert_eq!(t.agent, first.id);
3560 assert!(t.fallback);
3561 assert_eq!(t.persona, "");
3562 assert_eq!(t.implementers, 1);
3563 }
3564
3565 let t = open(&Preferred {
3567 agent: Some("second".into()),
3568 persona: Some("default".into()),
3569 implementers: Some(9),
3570 });
3571 assert_eq!(
3572 (t.agent.as_str(), t.persona.as_str(), t.implementers),
3573 ("second", "", 1)
3574 );
3575
3576 let strict = begin_with(
3578 &talks,
3579 &cfg,
3580 tmp.path().to_owned(),
3581 Some("first-unknown"),
3582 &Preferred {
3583 agent: Some("second".into()),
3584 ..Preferred::default()
3585 },
3586 );
3587 assert!(strict.is_err());
3588 }
3589
3590 #[test]
3591 fn remembered_agent_can_open_when_the_chatter_chain_cannot_resolve() {
3592 let (tmp, talks) = store();
3593 let spec = mock_agent(tmp.path(), ECHO, BTreeMap::new());
3594 let cfg = chain_config(vec![spec.clone()], &["missing-chatter"]);
3597 assert!(begin(&talks, &cfg, tmp.path().to_owned(), None).is_err());
3598 let talk = begin_with(
3599 &talks,
3600 &cfg,
3601 tmp.path().to_owned(),
3602 None,
3603 &Preferred {
3604 agent: Some(spec.id.clone()),
3605 ..Preferred::default()
3606 },
3607 )
3608 .expect("a valid remembered agent does not require a default chain");
3609 assert_eq!(talk.agent, spec.id);
3610 assert!(!talk.fallback);
3611 assert_eq!(talks.get(&talk.id).unwrap().agent, spec.id);
3612 assert!(
3613 begin_with(
3614 &talks,
3615 &cfg,
3616 tmp.path().to_owned(),
3617 None,
3618 &Preferred {
3619 agent: Some("missing-preference".into()),
3620 ..Preferred::default()
3621 },
3622 )
3623 .is_err()
3624 );
3625 }
3626
3627 #[test]
3628 fn implementers_are_validated_and_old_records_load_as_solo() {
3629 let (tmp, talks) = store();
3630 let cfg = config(mock_agent(tmp.path(), ECHO, BTreeMap::new()));
3631 assert_eq!(check_implementers(1, &cfg), Ok(1));
3632 assert_eq!(check_implementers(3, &cfg), Ok(3));
3633 assert!(check_implementers(0, &cfg).is_err());
3634 assert!(check_implementers(4, &cfg).is_err());
3635 let mut empty = cfg.clone();
3636 empty.agents.clear();
3637 assert!(check_implementers(2, &empty).is_err());
3638
3639 let talk = begin(&talks, &cfg, tmp.path().to_owned(), None).expect("begin");
3640 let path = talks.path_of(&talk.id);
3641 let mut v: serde_json::Value =
3642 serde_json::from_str(&std::fs::read_to_string(&path).unwrap()).unwrap();
3643 v.as_object_mut().unwrap().remove("implementers");
3644 v.as_object_mut().unwrap().remove("implementers_dirty");
3645 std::fs::write(&path, v.to_string()).unwrap();
3646 let loaded = talks.get(&talk.id).expect("load");
3647 assert_eq!(loaded.implementers, 1);
3648 assert!(!loaded.implementers_dirty);
3649 }
3650
3651 #[tokio::test]
3652 async fn an_implementers_switch_is_noted_once_and_kept_across_a_failed_turn() {
3653 let (tmp, talks) = store();
3654 let spec = mock_agent(tmp.path(), ECHO, BTreeMap::new());
3655 let cfg = config(spec);
3656 let mut talk = begin(&talks, &cfg, tmp.path().to_owned(), None).expect("begin");
3657 let lease = talks.claim_turn(&talk.id).unwrap().unwrap();
3658 say(&lease, &mut talk, &talks, &cfg, "hello", Vec::new())
3659 .await
3660 .expect("first turn");
3661
3662 assert!(switch_implementers(&mut talk, &talks, 2).expect("switch"));
3663 assert!(!switch_implementers(&mut talk, &talks, 2).expect("same"));
3664 assert!(talk.implementers_dirty);
3665 say(&lease, &mut talk, &talks, &cfg, "next", Vec::new())
3666 .await
3667 .expect("turn");
3668 let prompt = &talk.turns.last().unwrap().body;
3669 assert!(prompt.contains("# Task filing update"), "{prompt}");
3670 assert!(prompt.contains("--implementers 2"));
3671 assert!(!talk.implementers_dirty, "cleared after a successful turn");
3672
3673 say(&lease, &mut talk, &talks, &cfg, "again", Vec::new())
3674 .await
3675 .expect("turn");
3676 assert!(
3677 !talk
3678 .turns
3679 .last()
3680 .unwrap()
3681 .body
3682 .contains("# Task filing update")
3683 );
3684
3685 let mut no_sessions = cfg.clone();
3687 no_sessions.graph.sessions = false;
3688 say(&lease, &mut talk, &talks, &no_sessions, "one", Vec::new())
3689 .await
3690 .expect("turn");
3691 say(&lease, &mut talk, &talks, &no_sessions, "two", Vec::new())
3692 .await
3693 .expect("turn");
3694 assert!(talk.turns.last().unwrap().body.contains("--implementers 2"));
3695 }
3696
3697 #[tokio::test]
3698 async fn a_persona_switch_notes_marks_dirty_and_updates_the_next_turn_once() {
3699 let (tmp, talks) = store();
3700 let spec = mock_agent(tmp.path(), ECHO, BTreeMap::new());
3701 let cfg = config(spec);
3702 let mut talk = begin(&talks, &cfg, tmp.path().to_owned(), None).expect("begin");
3703 let lease = talks.claim_turn(&talk.id).unwrap().unwrap();
3704 say(&lease, &mut talk, &talks, &cfg, "hello", Vec::new())
3705 .await
3706 .expect("first turn");
3707 assert!(!talk.turns[1].body.contains("Persona"));
3708
3709 assert!(switch_persona(&mut talk, &talks, "misato").expect("switch"));
3710 assert!(!switch_persona(&mut talk, &talks, "misato").expect("same"));
3711 assert!(talk.persona_dirty);
3712 assert!(talk.turns.last().unwrap().body.contains("persona changed"));
3713
3714 say(&lease, &mut talk, &talks, &cfg, "next", Vec::new())
3715 .await
3716 .expect("turn");
3717 let prompt = &talk.turns.last().unwrap().body;
3718 assert!(prompt.contains("# Persona update"), "{prompt}");
3719 assert!(prompt.contains("Misato Katsuragi"));
3720 assert!(!talk.persona_dirty, "cleared after a successful turn");
3721
3722 say(&lease, &mut talk, &talks, &cfg, "again", Vec::new())
3723 .await
3724 .expect("turn");
3725 assert!(!talk.turns.last().unwrap().body.contains("# Persona update"));
3726
3727 assert!(switch_persona(&mut talk, &talks, "default").expect("back"));
3728 assert_eq!(talk.persona, "");
3729 say(&lease, &mut talk, &talks, &cfg, "plain", Vec::new())
3730 .await
3731 .expect("turn");
3732 assert!(
3733 talk.turns
3734 .last()
3735 .unwrap()
3736 .body
3737 .contains("turned the persona off")
3738 );
3739
3740 assert!(switch_persona(&mut talk, &talks, "rei").expect("rei"));
3742 let mut no_sessions = cfg.clone();
3743 no_sessions.graph.sessions = false;
3744 say(&lease, &mut talk, &talks, &no_sessions, "one", Vec::new())
3745 .await
3746 .expect("turn");
3747 say(&lease, &mut talk, &talks, &no_sessions, "two", Vec::new())
3748 .await
3749 .expect("turn");
3750 let last = &talk.turns.last().unwrap().body;
3751 assert!(!talk.persona_dirty);
3752 assert!(last.contains("# Persona (tone only)"), "{last}");
3753 assert!(last.contains("Rei Ayanami"));
3754 }
3755
3756 #[tokio::test]
3757 async fn the_first_turn_carries_the_briefing_and_later_turns_do_not() {
3758 let (tmp, talks) = store();
3759 let spec = mock_agent(tmp.path(), ECHO, BTreeMap::new());
3760 let cfg = config(spec);
3761 let mut talk = begin(&talks, &cfg, tmp.path().to_owned(), None).expect("begin");
3762
3763 say(
3764 &talks.claim_turn(&talk.id).unwrap().unwrap(),
3765 &mut talk,
3766 &talks,
3767 &cfg,
3768 "what does the queue module do?",
3769 Vec::new(),
3770 )
3771 .await
3772 .expect("first turn");
3773 let first_prompt = &talk.turns[1].body;
3774 assert!(first_prompt.contains("magi task add --solo"));
3775 assert!(first_prompt.contains("what does the queue module do?"));
3776
3777 say(
3778 &talks.claim_turn(&talk.id).unwrap().unwrap(),
3779 &mut talk,
3780 &talks,
3781 &cfg,
3782 "and how is it locked?",
3783 Vec::new(),
3784 )
3785 .await
3786 .expect("second turn");
3787 let second_prompt = &talk.turns[3].body;
3788 assert!(
3789 !second_prompt.contains("magi task add --solo"),
3790 "the briefing is sent once, not on every turn: {second_prompt}"
3791 );
3792 assert!(second_prompt.contains("and how is it locked?"));
3793 }
3794
3795 #[tokio::test]
3796 async fn switching_agent_resets_the_seat_notes_it_and_resends_the_transcript() {
3797 let (tmp, talks) = store();
3798 let a = mock_agent(tmp.path(), ECHO, BTreeMap::new());
3799 let mut b = a.clone();
3800 b.id = "other".to_owned();
3801 let mut cfg = config(a.clone());
3802 cfg.agents.push(b.clone());
3803 let mut talk = begin(&talks, &cfg, tmp.path().to_owned(), Some(&a.id)).expect("begin");
3804 say(
3805 &talks.claim_turn(&talk.id).unwrap().unwrap(),
3806 &mut talk,
3807 &talks,
3808 &cfg,
3809 "remember the walrus",
3810 Vec::new(),
3811 )
3812 .await
3813 .expect("first turn");
3814 let old_session = talk.seat.claude_session.clone();
3815 assert_eq!(talk.seat.turns, 1);
3816
3817 assert!(switch_agent(&mut talk, &talks, &b).expect("switch"));
3818 assert_eq!(talk.agent, "other");
3819 assert_eq!(talk.seat.turns, 0);
3820 assert_eq!(talk.seat.agent, "other");
3821 assert_ne!(talk.seat.claude_session, old_session);
3822 let note = talk.turns.last().expect("note");
3823 assert_eq!(note.who, Who::Agent);
3824 assert!(note.body.starts_with(MAGI_NOTE), "{}", note.body);
3825 assert!(note.body.contains("changed from"), "{}", note.body);
3826 assert_eq!(talks.get(&talk.id).expect("reload").agent, "other");
3827
3828 let before = talk.turns.len();
3829 assert!(!switch_agent(&mut talk, &talks, &b).expect("same agent"));
3830 assert_eq!(talk.turns.len(), before, "a no-op writes no note");
3831
3832 say(
3833 &talks.claim_turn(&talk.id).unwrap().unwrap(),
3834 &mut talk,
3835 &talks,
3836 &cfg,
3837 "what did I say?",
3838 Vec::new(),
3839 )
3840 .await
3841 .expect("turn after switch");
3842 let prompt = &talk.turns.last().expect("reply").body;
3843 assert!(prompt.contains("remember the walrus"), "{prompt}");
3844 assert!(prompt.contains("## magi"), "{prompt}");
3845 assert!(prompt.contains("what did I say?"), "{prompt}");
3846 }
3847
3848 #[tokio::test]
3849 async fn say_appends_the_operator_turn_then_the_agent_turn() {
3850 let (tmp, talks) = store();
3851 let spec = mock_agent(tmp.path(), REPLY, env("go ahead"));
3852 let cfg = config(spec);
3853 let mut talk = begin(&talks, &cfg, tmp.path().to_owned(), None).expect("begin");
3854
3855 say(
3856 &talks.claim_turn(&talk.id).unwrap().unwrap(),
3857 &mut talk,
3858 &talks,
3859 &cfg,
3860 "can I rename this function?",
3861 Vec::new(),
3862 )
3863 .await
3864 .expect("say");
3865
3866 assert_eq!(talk.turns.len(), 2);
3867 assert_eq!(talk.turns[0].who, Who::Operator);
3868 assert_eq!(talk.turns[0].body, "can I rename this function?");
3869 assert_eq!(talk.turns[1].who, Who::Agent);
3870 assert_eq!(talk.turns[1].body, "go ahead");
3871 assert_eq!(talks.get(&talk.id).expect("get").turns, talk.turns);
3872 }
3873
3874 #[tokio::test]
3875 async fn a_failed_turn_keeps_the_operator_message_and_says_what_happened() {
3876 let (tmp, talks) = store();
3877 let spec = mock_agent(tmp.path(), BROKEN, BTreeMap::new());
3878 let cfg = config(spec);
3879 let mut talk = begin(&talks, &cfg, tmp.path().to_owned(), None).expect("begin");
3880
3881 let err = say(
3882 &talks.claim_turn(&talk.id).unwrap().unwrap(),
3883 &mut talk,
3884 &talks,
3885 &cfg,
3886 "check the tests",
3887 Vec::new(),
3888 )
3889 .await
3890 .expect_err("a turn with no answer is an error");
3891 assert!(err.to_string().contains("no answer"), "{err}");
3892
3893 let on_disk = talks.get(&talk.id).expect("get");
3894 assert_eq!(on_disk.turns.len(), 2);
3895 assert_eq!(on_disk.turns[0].body, "check the tests");
3896 let note = &on_disk.turns[1];
3897 assert_eq!(note.who, Who::Agent);
3898 assert!(note.body.starts_with(MAGI_NOTE), "{}", note.body);
3899 assert!(note.body.contains("your message is saved"));
3900 }
3901
3902 #[tokio::test]
3908 async fn a_passing_write_failure_while_saving_the_reply_does_not_lose_it() {
3909 let (tmp, talks) = store();
3910 let spec = mock_agent(tmp.path(), REPLY, env("go ahead"));
3911 let cfg = config(spec);
3912 let mut talk = begin(&talks, &cfg, tmp.path().to_owned(), None).expect("begin");
3913
3914 let text =
3915 record(&mut talk, &talks, "can I rename this function?", Vec::new()).expect("record");
3916 failpoint::force_put_failures(PUT_RETRIES - 1);
3919 respond(
3920 &talks.claim_turn(&talk.id).unwrap().unwrap(),
3921 &mut talk,
3922 &talks,
3923 &cfg,
3924 &text,
3925 )
3926 .await
3927 .expect("respond must survive a write failure its own retries can outlast");
3928
3929 assert_eq!(talk.turns.len(), 2);
3930 assert_eq!(talk.turns[1].who, Who::Agent);
3931 assert_eq!(talk.turns[1].body, "go ahead");
3932 let on_disk = talks.get(&talk.id).expect("get");
3933 assert_eq!(
3934 on_disk.turns, talk.turns,
3935 "the reply must reach disk despite the early write failures"
3936 );
3937 }
3938
3939 #[tokio::test]
3945 async fn a_persistent_write_failure_while_saving_the_reply_is_never_silent() {
3946 let (tmp, talks) = store();
3947 let spec = mock_agent(tmp.path(), REPLY, env("go ahead"));
3948 let cfg = config(spec);
3949 let mut talk = begin(&talks, &cfg, tmp.path().to_owned(), None).expect("begin");
3950
3951 let text = record(&mut talk, &talks, "check the tests", Vec::new()).expect("record");
3952 failpoint::force_put_failures(PUT_RETRIES);
3957 let err = respond(
3958 &talks.claim_turn(&talk.id).unwrap().unwrap(),
3959 &mut talk,
3960 &talks,
3961 &cfg,
3962 &text,
3963 )
3964 .await
3965 .expect_err("a reply that cannot be saved must be reported, not swallowed");
3966 assert!(err.to_string().contains("could not be saved"), "{err}");
3967
3968 let on_disk = talks.get(&talk.id).expect("get");
3969 assert_eq!(
3970 on_disk.turns.len(),
3971 2,
3972 "the operator turn plus a visible note"
3973 );
3974 assert_eq!(on_disk.turns[0].body, "check the tests");
3975 let note = &on_disk.turns[1];
3976 assert_eq!(note.who, Who::Agent);
3977 assert!(note.body.starts_with(MAGI_NOTE), "{}", note.body);
3978 assert!(
3979 note.body.contains("could not be saved"),
3980 "the operator must be told the reply is missing, not left staring \
3981 at a gap with no explanation: {}",
3982 note.body
3983 );
3984 assert_eq!(
3985 talk.turns, on_disk.turns,
3986 "the in-memory talk must match what actually landed on disk"
3987 );
3988
3989 let artifacts = talks.artifacts_of(&talk.id);
3992 let stash = std::fs::read_dir(&artifacts)
3993 .expect("artifacts dir")
3994 .filter_map(|e| e.ok())
3995 .find(|e| e.file_name().to_string_lossy().ends_with("-lost.txt"))
3996 .expect("a stash file for the lost reply");
3997 let stashed = std::fs::read_to_string(stash.path()).expect("read stash");
3998 assert_eq!(stashed, "go ahead");
3999
4000 assert_eq!(
4008 on_disk.seat.turns, 1,
4009 "the note's write must carry the turn the CLI actually took"
4010 );
4011 assert_eq!(
4012 on_disk.seat.claude_session, talk.seat.claude_session,
4013 "the session id handed to the CLI must survive the failed reply"
4014 );
4015 assert_eq!(on_disk.seat.captured_session, talk.seat.captured_session);
4016 assert!(
4017 agent::has_session(AgentKind::Command, &on_disk.seat, cfg.graph.sessions),
4018 "the next turn must resume, not open the same session id twice"
4019 );
4020 }
4021
4022 #[tokio::test]
4027 async fn a_write_failure_that_also_loses_the_note_still_reports_it() {
4028 let (tmp, talks) = store();
4029 let spec = mock_agent(tmp.path(), REPLY, env("go ahead"));
4030 let cfg = config(spec);
4031 let mut talk = begin(&talks, &cfg, tmp.path().to_owned(), None).expect("begin");
4032
4033 let text = record(&mut talk, &talks, "check the tests", Vec::new()).expect("record");
4034 failpoint::force_put_failures(PUT_RETRIES * 2);
4037 let err = respond(
4038 &talks.claim_turn(&talk.id).unwrap().unwrap(),
4039 &mut talk,
4040 &talks,
4041 &cfg,
4042 &text,
4043 )
4044 .await
4045 .expect_err("neither the reply nor the note could be saved");
4046 assert!(err.to_string().contains("could not be saved"), "{err}");
4047
4048 assert_eq!(talk.turns.len(), 1, "only the operator's own turn");
4049 let on_disk = talks.get(&talk.id).expect("get");
4050 assert_eq!(on_disk.turns.len(), 1);
4051
4052 assert_eq!(
4062 on_disk.seat.turns, 0,
4063 "an unwritable file cannot record the turn the CLI took"
4064 );
4065 assert_eq!(
4066 talk.seat.turns, 1,
4067 "the in-memory seat still reports the turn the CLI actually took"
4068 );
4069 assert_eq!(
4070 on_disk.seat.claude_session, talk.seat.claude_session,
4071 "the session id was minted at `begin` and never changes here"
4072 );
4073 }
4074
4075 #[tokio::test]
4079 async fn attachments_reach_the_prompt_and_an_empty_body_is_still_a_turn() {
4080 let (tmp, talks) = store();
4081 let spec = mock_agent(tmp.path(), ECHO, BTreeMap::new());
4082 let cfg = config(spec);
4083 let mut talk = begin(&talks, &cfg, tmp.path().to_owned(), None).expect("begin");
4084
4085 let att = talks
4086 .put_attachment(
4087 &talk.id,
4088 "image/png",
4089 "screenshot.png",
4090 b"pretend-png-bytes",
4091 )
4092 .expect("put attachment");
4093
4094 say(
4095 &talks.claim_turn(&talk.id).unwrap().unwrap(),
4096 &mut talk,
4097 &talks,
4098 &cfg,
4099 "",
4100 vec![att.clone()],
4101 )
4102 .await
4103 .expect("an empty body with an attachment is still a turn");
4104
4105 let operator_turn = &talk.turns[0];
4106 assert_eq!(operator_turn.who, Who::Operator);
4107 assert_eq!(operator_turn.body, "");
4108 assert_eq!(operator_turn.attachments, vec![att.clone()]);
4109
4110 let prompt = &talk.turns[1].body;
4111 let expected_path = talks
4112 .attachments_dir(&talk.id)
4113 .join(format!("{}.png", att.id));
4114 assert!(
4115 prompt.contains(&expected_path.display().to_string()),
4116 "the agent must be told the attachment's absolute path: {prompt}"
4117 );
4118 assert!(prompt.contains("image/png"), "and its mime: {prompt}");
4119 }
4120
4121 #[test]
4130 fn attachment_path_is_absolute_even_when_the_store_root_is_relative() {
4131 let talks = Talks::at(PathBuf::from("relative-talks-root-for-this-test"));
4132 let att = Attachment {
4133 id: "0".repeat(32),
4134 name: "shot.png".to_owned(),
4135 mime: "image/png".to_owned(),
4136 bytes: 3,
4137 };
4138 let path = talks
4139 .attachment_path("some-talk-id", &att)
4140 .expect("a supported mime always yields a path");
4141 assert!(
4142 path.is_absolute(),
4143 "must be absolute even off a relative store root: {}",
4144 path.display()
4145 );
4146 }
4147
4148 #[tokio::test]
4149 async fn a_turn_past_the_configured_talk_timeout_is_reported_with_that_timeout() {
4150 let (tmp, talks) = store();
4155 let slow = mock_agent(
4156 tmp.path(),
4157 "#!/bin/sh\ncat >/dev/null\nsleep 2\n",
4158 BTreeMap::new(),
4159 );
4160 let mut cfg = config(slow);
4161 cfg.graph.timeout_talk = 1;
4162 let mut talk = begin(&talks, &cfg, tmp.path().to_owned(), None).expect("begin");
4163
4164 let err = say(
4165 &talks.claim_turn(&talk.id).unwrap().unwrap(),
4166 &mut talk,
4167 &talks,
4168 &cfg,
4169 "check the tests",
4170 Vec::new(),
4171 )
4172 .await
4173 .expect_err("a turn that never answers is an error");
4174 assert!(
4175 err.to_string().contains("did not answer within 1s"),
4176 "{err}"
4177 );
4178
4179 let on_disk = talks.get(&talk.id).expect("get");
4180 let note = on_disk.turns.last().expect("a note turn was recorded");
4181 assert!(
4182 note.body.contains("did not answer within 1s"),
4183 "the transcript must show the configured timeout: {}",
4184 note.body
4185 );
4186 }
4187
4188 #[test]
4189 fn closing_is_idempotent_and_a_closed_talk_takes_no_more_turns() {
4190 let (tmp, talks) = store();
4191 let spec = mock_agent(tmp.path(), REPLY, env("hi"));
4192 let cfg = config(spec);
4193 let mut talk = begin(&talks, &cfg, tmp.path().to_owned(), None).expect("begin");
4194
4195 close(&mut talk, &talks).expect("close");
4196 assert_eq!(talk.status, TalkStatus::Closed);
4197 close(&mut talk, &talks).expect("closing twice is not an error");
4198
4199 let err =
4200 record(&mut talk, &talks, "still there?", Vec::new()).expect_err("closed talks refuse");
4201 assert!(err.to_string().contains("closed"));
4202 let _ = &cfg; }
4204
4205 #[tokio::test]
4206 async fn a_close_that_lands_while_a_turn_is_in_flight_is_not_undone_by_the_reply() {
4207 let (tmp, talks) = store();
4208 let spec = mock_agent(tmp.path(), REPLY, env("here you go"));
4209 let cfg = config(spec);
4210 let mut in_flight = begin(&talks, &cfg, tmp.path().to_owned(), None).expect("begin");
4213
4214 let mut closed_elsewhere = talks.get(&in_flight.id).expect("reread");
4218 close(&mut closed_elsewhere, &talks).expect("close");
4219 assert_eq!(
4220 talks.get(&in_flight.id).expect("reread").status,
4221 TalkStatus::Closed,
4222 "the close landed on disk before the turn finished"
4223 );
4224
4225 assert_eq!(in_flight.status, TalkStatus::Open);
4229 respond(
4230 &talks.claim_turn(&in_flight.id).unwrap().unwrap(),
4231 &mut in_flight,
4232 &talks,
4233 &cfg,
4234 "one more question",
4235 )
4236 .await
4237 .expect("the turn itself still completes");
4238
4239 let on_disk = talks.get(&in_flight.id).expect("reread");
4240 assert_eq!(
4241 on_disk.status,
4242 TalkStatus::Closed,
4243 "a close must stick even when a turn that started before it finishes after it"
4244 );
4245 assert!(
4248 on_disk.turns.iter().any(|t| t.body == "here you go"),
4249 "the in-flight turn's own reply is still recorded: {:?}",
4250 on_disk.turns
4251 );
4252 }
4253
4254 #[test]
4255 fn a_close_that_lands_before_record_is_called_is_not_undone_by_it() {
4256 let (tmp, talks) = store();
4257 let spec = mock_agent(tmp.path(), REPLY, env("hi"));
4258 let cfg = config(spec);
4259 let mut stale = begin(&talks, &cfg, tmp.path().to_owned(), None).expect("begin");
4262
4263 let mut closed_elsewhere = talks.get(&stale.id).expect("reread");
4266 close(&mut closed_elsewhere, &talks).expect("close");
4267 assert_eq!(
4268 talks.get(&stale.id).expect("reread").status,
4269 TalkStatus::Closed,
4270 "the close landed on disk before record was called"
4271 );
4272
4273 assert_eq!(stale.status, TalkStatus::Open);
4277 let err = record(&mut stale, &talks, "still there?", Vec::new())
4278 .expect_err("a close that landed first must be honored, not overwritten");
4279 assert!(err.to_string().contains("closed"));
4280
4281 let on_disk = talks.get(&stale.id).expect("reread");
4282 assert_eq!(
4283 on_disk.status,
4284 TalkStatus::Closed,
4285 "record must not resurrect a conversation closed while its snapshot was stale"
4286 );
4287 assert!(
4288 on_disk.turns.is_empty(),
4289 "the rejected turn must not have been appended: {:?}",
4290 on_disk.turns
4291 );
4292 let _ = &cfg; }
4294
4295 #[test]
4296 fn close_blocks_on_records_guard_rather_than_interleaving_with_it() {
4297 let (tmp, talks) = store();
4298 let spec = mock_agent(tmp.path(), REPLY, env("hi"));
4299 let cfg = config(spec);
4300 let talk = begin(&talks, &cfg, tmp.path().to_owned(), None).expect("begin");
4301
4302 let held = talks.guard().unwrap();
4306
4307 let talks2 = talks.clone();
4308 let id = talk.id.clone();
4309 let closing = std::thread::spawn(move || {
4310 let mut talk = talks2.get(&id).expect("get");
4311 close(&mut talk, &talks2).expect("close");
4312 });
4313
4314 std::thread::sleep(Duration::from_millis(50));
4315 assert!(
4316 !closing.is_finished(),
4317 "close must wait for the guard, not read and write while it is held - \
4318 a re-read alone narrows this window without closing it"
4319 );
4320
4321 drop(held);
4322 closing.join().expect("close thread panicked");
4323
4324 assert_eq!(
4325 talks.get(&talk.id).expect("reread").status,
4326 TalkStatus::Closed,
4327 "once the guard is free, close still lands"
4328 );
4329 let _ = &cfg; }
4331
4332 #[test]
4333 fn reopening_a_closed_talk_lets_it_take_turns_again_and_reopening_twice_is_not_an_error() {
4334 let (tmp, talks) = store();
4335 let spec = mock_agent(tmp.path(), REPLY, env("hi"));
4336 let cfg = config(spec);
4337 let mut talk = begin(&talks, &cfg, tmp.path().to_owned(), None).expect("begin");
4338
4339 close(&mut talk, &talks).expect("close");
4340 assert_eq!(talk.status, TalkStatus::Closed);
4341
4342 reopen(&mut talk, &talks).expect("reopen");
4343 assert_eq!(talk.status, TalkStatus::Open);
4344 assert_eq!(
4345 talks.get(&talk.id).expect("reread").status,
4346 TalkStatus::Open
4347 );
4348
4349 reopen(&mut talk, &talks).expect("reopening an open talk is not an error");
4351 assert_eq!(talk.status, TalkStatus::Open);
4352
4353 record(&mut talk, &talks, "one more thing", Vec::new())
4354 .expect("a reopened talk takes turns again");
4355 let _ = &cfg; }
4357
4358 #[test]
4359 fn removing_a_talk_deletes_its_record_and_artifacts_and_refuses_an_unknown_id() {
4360 let (tmp, talks) = store();
4361 let spec = mock_agent(tmp.path(), REPLY, env("hi"));
4362 let cfg = config(spec);
4363 let talk = begin(&talks, &cfg, tmp.path().to_owned(), None).expect("begin");
4364
4365 let artifacts = talks.artifacts_of(&talk.id);
4366 std::fs::create_dir_all(&artifacts).expect("create artifacts dir");
4367 std::fs::write(artifacts.join("turn-1.txt"), "hello").expect("write artifact");
4368
4369 talks.remove(&talk.id).expect("remove");
4370 assert!(!talks.path_of(&talk.id).is_file(), "the record is gone");
4371 assert!(!artifacts.is_dir(), "the artifacts directory is gone");
4372 assert!(
4373 talks.get(&talk.id).is_err(),
4374 "a removed talk cannot be read back"
4375 );
4376
4377 let err = talks
4378 .remove("nonexistent-id")
4379 .expect_err("unknown id refused");
4380 assert!(err.to_string().contains("no talk matches"), "{err}");
4381 let _ = &cfg; }
4383
4384 #[tokio::test]
4385 async fn a_delete_that_lands_while_a_turn_is_in_flight_is_not_undone_by_the_reply() {
4386 let (tmp, talks) = store();
4387 let spec = mock_agent(tmp.path(), REPLY, env("here you go"));
4388 let cfg = config(spec);
4389 let mut in_flight = begin(&talks, &cfg, tmp.path().to_owned(), None).expect("begin");
4392
4393 talks.remove(&in_flight.id).expect("remove");
4394 assert!(
4395 talks.get(&in_flight.id).is_err(),
4396 "the delete landed on disk before the turn finished"
4397 );
4398
4399 respond(
4402 &talks.claim_turn(&in_flight.id).unwrap().unwrap(),
4403 &mut in_flight,
4404 &talks,
4405 &cfg,
4406 "one more question",
4407 )
4408 .await
4409 .expect("the turn itself still completes rather than erroring");
4410
4411 assert!(
4412 talks.get(&in_flight.id).is_err(),
4413 "a delete must stick even when a turn that started before it finishes after it"
4414 );
4415 }
4416
4417 #[test]
4418 fn a_delete_that_lands_before_record_is_called_is_not_undone_by_it() {
4419 let (tmp, talks) = store();
4420 let spec = mock_agent(tmp.path(), REPLY, env("hi"));
4421 let cfg = config(spec);
4422 let mut stale = begin(&talks, &cfg, tmp.path().to_owned(), None).expect("begin");
4425
4426 talks.remove(&stale.id).expect("remove");
4427
4428 let err = record(&mut stale, &talks, "still there?", Vec::new())
4432 .expect_err("a delete that landed first must be honored, not overwritten");
4433 assert!(err.to_string().contains("deleted"), "{err}");
4434
4435 assert!(
4436 talks.get(&stale.id).is_err(),
4437 "record must not resurrect a conversation deleted while its snapshot was stale"
4438 );
4439 let _ = &cfg; }
4441
4442 #[test]
4443 fn a_delete_that_lands_before_close_is_called_is_not_undone_by_it() {
4444 let (tmp, talks) = store();
4445 let spec = mock_agent(tmp.path(), REPLY, env("hi"));
4446 let cfg = config(spec);
4447 let mut stale = begin(&talks, &cfg, tmp.path().to_owned(), None).expect("begin");
4450
4451 talks.remove(&stale.id).expect("remove");
4452
4453 let err = close(&mut stale, &talks)
4457 .expect_err("a delete that landed first must be honored, not overwritten");
4458 assert!(err.to_string().contains("deleted"), "{err}");
4459
4460 assert!(
4461 talks.get(&stale.id).is_err(),
4462 "close must not resurrect a conversation deleted while its snapshot was stale"
4463 );
4464 let _ = &cfg; }
4466
4467 #[test]
4468 fn a_delete_that_lands_before_reopen_is_called_is_not_undone_by_it() {
4469 let (tmp, talks) = store();
4470 let spec = mock_agent(tmp.path(), REPLY, env("hi"));
4471 let cfg = config(spec);
4472 let mut stale = begin(&talks, &cfg, tmp.path().to_owned(), None).expect("begin");
4475 close(&mut stale, &talks).expect("close");
4476
4477 talks.remove(&stale.id).expect("remove");
4478
4479 let err = reopen(&mut stale, &talks)
4483 .expect_err("a delete that landed first must be honored, not overwritten");
4484 assert!(err.to_string().contains("deleted"), "{err}");
4485
4486 assert!(
4487 talks.get(&stale.id).is_err(),
4488 "reopen must not resurrect a conversation deleted while its snapshot was stale"
4489 );
4490 let _ = &cfg; }
4492
4493 #[test]
4494 fn list_puts_open_talks_before_closed_ones() {
4495 let (tmp, talks) = store();
4496 let make = |id: &str, status: TalkStatus| {
4497 let mut t = Talk {
4498 pending_breaks: None,
4499 schema: SCHEMA,
4500 id: id.to_owned(),
4501 repo: tmp.path().to_owned(),
4502 agent: "mock".to_owned(),
4503 status,
4504 turns: Vec::new(),
4505 pending: String::new(),
4506 pending_attachments: Vec::new(),
4507 fallback: false,
4508 persona: String::new(),
4509 persona_dirty: false,
4510 implementers: 1,
4511 implementers_dirty: false,
4512 created_at: Timestamp::now(),
4513 updated_at: Timestamp::now(),
4514 seat: SeatState::new(SEAT, "mock", 7),
4515 };
4516 talks.put(&mut t).expect("put");
4517 };
4518 make("20260901-000000-0001", TalkStatus::Open);
4519 make("20260902-000000-0002", TalkStatus::Open);
4520 make("20260903-000000-0003", TalkStatus::Closed);
4521
4522 let ids: Vec<String> = talks.list().into_iter().map(|t| t.id).collect();
4523 assert_eq!(
4524 ids,
4525 [
4526 "20260902-000000-0002",
4527 "20260901-000000-0001",
4528 "20260903-000000-0003"
4529 ]
4530 );
4531 assert_eq!(talks.count_open(), 2);
4532 }
4533
4534 #[test]
4535 fn tasks_of_finds_only_this_talks_own_tasks() {
4536 let dir = tempfile::tempdir().expect("tempdir");
4537 let queue = Queue::at(dir.path().join("queue"));
4538
4539 let mut mine = Task::new(
4540 "rework the loader".to_owned(),
4541 "rework the loader".to_owned(),
4542 PathBuf::from("/repo"),
4543 Source::Agent {
4544 run: "20260904-014455-ab12".to_owned(),
4545 node: "chat".to_owned(),
4546 },
4547 );
4548 queue.put(&mut mine).expect("put mine");
4549
4550 let mut theirs = Task::new(
4551 "unrelated".to_owned(),
4552 "unrelated".to_owned(),
4553 PathBuf::from("/repo"),
4554 Source::Agent {
4555 run: "20260904-090000-zz99".to_owned(),
4556 node: "implement".to_owned(),
4557 },
4558 );
4559 queue.put(&mut theirs).expect("put theirs");
4560
4561 let mut human = Task::new(
4562 "typed by hand".to_owned(),
4563 "typed by hand".to_owned(),
4564 PathBuf::from("/repo"),
4565 Source::Human,
4566 );
4567 queue.put(&mut human).expect("put human");
4568
4569 let found = tasks_of(&queue, "20260904-014455-ab12");
4570 assert_eq!(found.len(), 1);
4571 assert_eq!(found[0].id, mine.id);
4572 }
4573
4574 #[test]
4575 fn the_briefing_names_solo_task_add() {
4576 let brief = briefing(Path::new("/repo"), "en", false);
4577 assert!(brief.contains("magi task add --solo"));
4578 assert!(brief.contains("/repo"));
4579 assert!(!brief.contains("Hold this conversation in"));
4580 }
4581
4582 #[test]
4588 fn the_briefing_explains_targeting_a_different_repository_by_name() {
4589 let brief = briefing(Path::new("/repo"), "en", false);
4590 assert!(brief.contains("--repo does not have to be a full path"));
4591 assert!(brief.contains("owner/repo"));
4592 assert!(brief.contains("magi repos"));
4593 assert!(brief.contains("ask the operator"));
4594 }
4595
4596 #[test]
4597 fn the_briefing_tells_the_assistant_to_pass_images_with_attach() {
4598 let brief = briefing(Path::new("/repo"), "en", false);
4599 assert!(brief.contains("--attach <path>"), "{brief}");
4600 assert!(brief.contains("deleting this conversation"), "{brief}");
4601 }
4602
4603 #[test]
4604 fn the_briefing_names_the_language_when_it_is_not_english() {
4605 let brief = briefing(Path::new("/repo"), "Japanese", false);
4606 assert!(brief.contains("Hold this conversation in Japanese"));
4607 }
4608
4609 #[test]
4610 fn the_briefing_forbids_writes_unless_the_repository_opted_in() {
4611 let read_only = briefing(Path::new("/repo"), "en", false);
4612 assert!(read_only.contains("Do not write files"));
4613 assert!(!read_only.contains("allow_write"));
4614
4615 let writable = briefing(Path::new("/repo"), "en", true);
4616 assert!(!writable.contains("Do not write files"));
4617 assert!(writable.contains("allow_write = true"));
4618 assert!(writable.contains("magi task add --solo"));
4621 assert!(writable.contains("say plainly what you"));
4622 }
4623}