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 #[serde(default, skip_serializing_if = "Option::is_none")]
152 pub posted_by: Option<String>,
153}
154
155#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
163pub struct TurnUsage {
164 pub context_tokens: u64,
166 pub agent: String,
168 #[serde(default)]
170 pub model: Option<String>,
171}
172
173#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize)]
178pub struct ContextUsage {
179 pub tokens: Option<u64>,
181 pub window: Option<u64>,
183 pub percent: Option<u64>,
185 pub warn: bool,
187 pub since_switch: bool,
191 pub model: Option<String>,
193 #[serde(default)]
196 pub estimated: bool,
197}
198
199const CONTEXT_WARN_PERCENT: u64 = 80;
201
202const STANDING_PROMPT_FALLBACK_CHARS: u64 = 7000;
205
206pub fn estimate_context_tokens(talk: &Talk, standing_chars: u64) -> Option<u64> {
219 let mut counted = false;
220 let mut chars = standing_chars;
221 for t in talk.turns.iter().filter(|t| !t.body.starts_with(MAGI_NOTE)) {
222 counted = true;
223 chars += t.body.chars().count() as u64;
224 }
225 counted.then(|| (chars * 2).div_ceil(7))
227}
228
229pub fn context_usage(talk: &Talk, cfg: Option<&Config>) -> ContextUsage {
242 let current = cfg.and_then(|c| c.agents.iter().find(|a| a.id == talk.agent));
243 let model = current.and_then(|a| a.model.clone());
244 let window = cfg
245 .zip(model.as_deref())
246 .and_then(|(c, m)| c.context_window(m))
247 .filter(|w| *w > 0);
248 let usage = talk
249 .turns
250 .iter()
251 .rev()
252 .find(|t| t.who == Who::Agent && !t.body.starts_with(MAGI_NOTE))
253 .and_then(|t| t.usage.as_ref());
254 let measured = usage.map(|u| u.context_tokens);
255 let tokens = measured.or_else(|| {
256 let standing = cfg.map_or(STANDING_PROMPT_FALLBACK_CHARS, |c| {
257 briefing_for(
258 &talk.repo,
259 &c.graph.language,
260 c.talk.allow_write,
261 crate::persona::active(&c.talk.personas, &talk.persona).as_ref(),
262 c.talk.operator_name(),
263 talk.implementers,
264 )
265 .chars()
266 .count() as u64
267 });
268 estimate_context_tokens(talk, standing)
269 });
270 let estimated = measured.is_none() && tokens.is_some();
271 let since_switch =
272 usage.is_some_and(|u| u.agent != talk.agent || (current.is_some() && u.model != model));
273 let (percent, warn) = match (tokens, window) {
274 (Some(t), Some(w)) => (
275 Some(t.saturating_mul(100) / w),
276 t.saturating_mul(100) >= w.saturating_mul(CONTEXT_WARN_PERCENT),
277 ),
278 _ => (None, false),
279 };
280 ContextUsage {
281 tokens,
282 window,
283 percent,
284 warn,
285 since_switch,
286 model,
287 estimated,
288 }
289}
290
291#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
295#[serde(rename_all = "lowercase")]
296pub enum TalkStatus {
297 Open,
300 Closed,
302}
303
304impl TalkStatus {
305 pub fn open(self) -> bool {
307 matches!(self, Self::Open)
308 }
309
310 pub fn as_str(self) -> &'static str {
312 match self {
313 Self::Open => "open",
314 Self::Closed => "closed",
315 }
316 }
317}
318
319#[derive(Debug, Clone, Serialize, Deserialize)]
321#[serde(deny_unknown_fields)]
322pub struct Talk {
323 pub schema: u32,
325 pub id: String,
327 pub repo: PathBuf,
329 pub agent: String,
331 pub status: TalkStatus,
333 pub turns: Vec<Turn>,
335 #[serde(default, skip_serializing_if = "Option::is_none")]
339 pub posts_read_through: Option<usize>,
340 #[serde(default)]
343 pub pending: String,
344 #[serde(default)]
346 pub pending_attachments: Vec<Attachment>,
347 #[serde(default, skip_serializing_if = "Option::is_none")]
349 pub pending_breaks: Option<Vec<usize>>,
350 #[serde(default)]
355 pub fallback: bool,
356 #[serde(default)]
359 pub persona: String,
360 #[serde(default)]
364 pub persona_dirty: bool,
365 #[serde(default = "solo_implementers")]
368 pub implementers: u8,
369 #[serde(default)]
372 pub implementers_dirty: bool,
373 #[serde(default)]
378 pub notified: Vec<String>,
379 pub created_at: Timestamp,
381 pub updated_at: Timestamp,
383 seat: SeatState,
388}
389
390impl Talk {
391 pub fn short(&self) -> &str {
393 short(&self.id)
394 }
395}
396
397#[derive(Debug, Clone)]
399pub struct Talks {
400 root: PathBuf,
401 lock: Arc<Mutex<()>>,
410}
411
412impl Talks {
413 pub fn open() -> Self {
415 Self::at(crate::run::home().join("talks"))
416 }
417
418 pub fn at(root: PathBuf) -> Self {
421 Self {
422 root,
423 lock: Arc::new(Mutex::new(())),
424 }
425 }
426
427 fn guard(&self) -> Result<StoreGuard<'_>> {
442 let mutex = self.lock.lock().unwrap_or_else(PoisonError::into_inner);
443 std::fs::create_dir_all(&self.root)
444 .with_context(|| format!("create {}", self.root.display()))?;
445 let path = self.root.join(".store.turn");
446 let deadline = std::time::Instant::now() + TAKEOVER_LOCK_TTL * 2;
447 loop {
448 if let Some(file) = TurnLock::take(&path)? {
449 return Ok(StoreGuard {
450 _file: file,
451 _mutex: mutex,
452 });
453 }
454 if std::time::Instant::now() >= deadline {
455 bail!("the talk store is locked by another process");
456 }
457 std::thread::sleep(Duration::from_millis(10));
458 }
459 }
460
461 pub fn root(&self) -> &Path {
463 &self.root
464 }
465
466 pub fn path_of(&self, id: &str) -> PathBuf {
468 self.root.join(format!("{id}.json"))
469 }
470
471 pub fn artifacts_of(&self, id: &str) -> PathBuf {
474 self.root.join(format!("{id}.artifacts"))
475 }
476
477 pub fn attachments_dir(&self, id: &str) -> PathBuf {
481 self.artifacts_of(id).join("attachments")
482 }
483
484 pub fn put_attachment(
493 &self,
494 id: &str,
495 mime: &str,
496 name: &str,
497 data: &[u8],
498 ) -> Result<Attachment> {
499 let dir = self.attachments_dir(id);
500 std::fs::create_dir_all(&dir).with_context(|| format!("create {}", dir.display()))?;
501 let ext = attachment_ext(mime).with_context(|| format!("unsupported mime `{mime}`"))?;
502 let att = Attachment {
503 id: new_attachment_id(),
504 name: name.to_owned(),
505 mime: mime.to_owned(),
506 bytes: data.len() as u64,
507 };
508 std::fs::write(dir.join(format!("{}.{ext}", att.id)), data)
509 .with_context(|| format!("write attachment {}", att.id))?;
510 std::fs::write(
511 dir.join(format!("{}.json", att.id)),
512 serde_json::to_string(&att).context("serialize attachment")?,
513 )
514 .with_context(|| format!("write attachment metadata {}", att.id))?;
515 Ok(att)
516 }
517
518 pub fn attachment_meta(&self, id: &str, att_id: &str) -> Result<Option<Attachment>> {
527 if !valid_attachment_id(att_id) {
528 return Ok(None);
529 }
530 let meta_path = self.attachments_dir(id).join(format!("{att_id}.json"));
531 if !meta_path.is_file() {
532 return Ok(None);
533 }
534 let att = serde_json::from_str(
535 &std::fs::read_to_string(&meta_path)
536 .with_context(|| format!("read {}", meta_path.display()))?,
537 )
538 .with_context(|| format!("parse {}", meta_path.display()))?;
539 Ok(Some(att))
540 }
541
542 pub fn read_attachment(&self, id: &str, att_id: &str) -> Result<Option<(Attachment, Vec<u8>)>> {
546 let Some(att) = self.attachment_meta(id, att_id)? else {
547 return Ok(None);
548 };
549 let ext = attachment_ext(&att.mime).with_context(|| {
550 format!("attachment {att_id} has an unsupported mime `{}`", att.mime)
551 })?;
552 let data_path = self.attachments_dir(id).join(format!("{att_id}.{ext}"));
553 let data =
554 std::fs::read(&data_path).with_context(|| format!("read {}", data_path.display()))?;
555 Ok(Some((att, data)))
556 }
557
558 fn attachment_path(&self, id: &str, att: &Attachment) -> Option<PathBuf> {
575 let ext = attachment_ext(&att.mime)?;
576 let path = self.attachments_dir(id).join(format!("{}.{ext}", att.id));
577 std::path::absolute(&path).ok()
578 }
579
580 pub fn put(&self, t: &mut Talk) -> Result<()> {
589 std::fs::create_dir_all(&self.root)
590 .with_context(|| format!("create {}", self.root.display()))?;
591 t.updated_at = Timestamp::now();
592 let body = serde_json::to_string_pretty(t).context("serialize talk")?;
593 let path = self.path_of(&t.id);
594 let tmp = path.with_extension("json.tmp");
595 write_atomic(&tmp, &path, &body)
596 }
597
598 pub fn get(&self, id: &str) -> Result<Talk> {
600 let resolved = self.resolve_id(id)?;
601 read_path(&self.path_of(&resolved))
602 }
603
604 pub fn list(&self) -> Vec<Talk> {
607 self.list_counting_unreadable().0
608 }
609
610 pub fn list_counting_unreadable(&self) -> (Vec<Talk>, usize) {
613 let mut unreadable = 0;
614 let mut all: Vec<Talk> = std::fs::read_dir(&self.root)
615 .into_iter()
616 .flatten()
617 .flatten()
618 .map(|e| e.path())
619 .filter(|p| p.extension().is_some_and(|x| x == "json"))
620 .filter_map(|p| {
621 let talk = read_path(&p).ok();
622 if talk.is_none() {
623 unreadable += 1;
624 }
625 talk
626 })
627 .collect();
628 all.sort_unstable_by(|a, b| {
629 let rank = |t: &Talk| u8::from(!t.status.open());
630 rank(a).cmp(&rank(b)).then_with(|| b.id.cmp(&a.id))
631 });
632 (all, unreadable)
633 }
634
635 pub fn resolve_id(&self, prefix: &str) -> Result<String> {
637 if self.path_of(prefix).is_file() {
638 return Ok(prefix.to_owned());
639 }
640 let hits: Vec<String> = self
641 .list()
642 .into_iter()
643 .map(|t| t.id)
644 .filter(|id| id.starts_with(prefix) || id.ends_with(prefix))
645 .collect();
646 match hits.len() {
647 1 => Ok(hits.into_iter().next().expect("exactly one hit")),
648 0 => bail!("no talk matches `{prefix}`"),
649 _ => bail!(
650 "`{prefix}` matches {} talks: {}",
651 hits.len(),
652 hits.join(", ")
653 ),
654 }
655 }
656
657 pub fn revision(&self) -> u64 {
660 std::fs::read_dir(&self.root)
661 .into_iter()
662 .flatten()
663 .flatten()
664 .filter(|e| e.path().extension().is_none_or(|x| x != "turn"))
665 .filter(|e| !e.file_name().to_string_lossy().starts_with('.'))
666 .filter_map(|e| e.metadata().ok())
667 .filter_map(|m| m.modified().ok())
668 .filter_map(|t| t.duration_since(std::time::UNIX_EPOCH).ok())
669 .map(|d| d.as_millis() as u64)
670 .max()
671 .unwrap_or(0)
672 }
673
674 pub fn count_open(&self) -> usize {
676 self.list().iter().filter(|t| t.status.open()).count()
677 }
678
679 pub fn turn_path(&self, id: &str) -> PathBuf {
682 self.root.join(format!("{id}.turn"))
683 }
684
685 pub fn claim_turn(&self, id: &str) -> Result<Option<TurnLease>> {
694 self.claim_turn_at(id, Timestamp::now())
695 }
696
697 fn claim_turn_at(&self, id: &str, now: Timestamp) -> Result<Option<TurnLease>> {
698 std::fs::create_dir_all(&self.root)
699 .with_context(|| format!("create {}", self.root.display()))?;
700 let path = self.turn_path(id);
701 let token = fresh_token();
702 if create_turn(&path, &token, now)? {
703 return Ok(Some(TurnLease {
704 talk: id.to_owned(),
705 path,
706 token,
707 }));
708 }
709 if lease_blocks(&path, now) {
710 return Ok(None);
711 }
712 let Some(_lock) = TurnLock::take(&path)? else {
717 return Ok(None);
718 };
719 if lease_blocks(&path, now) {
720 return Ok(None);
721 }
722 let _ = std::fs::remove_file(&path);
723 Ok(create_turn(&path, &token, now)?.then_some(TurnLease {
724 talk: id.to_owned(),
725 path,
726 token,
727 }))
728 }
729
730 pub fn turn_held(&self, id: &str) -> bool {
732 read_turn(&self.turn_path(id)).is_some_and(|r| r.fresh(Timestamp::now()))
733 }
734
735 pub fn remove(&self, id: &str) -> Result<()> {
748 let _guard = self.guard()?;
749 let resolved = self.resolve_id(id)?;
750 let path = self.path_of(&resolved);
751 std::fs::remove_file(&path).with_context(|| format!("remove {}", path.display()))?;
752 let artifacts = self.artifacts_of(&resolved);
753 if artifacts.is_dir() {
754 std::fs::remove_dir_all(&artifacts)
755 .with_context(|| format!("remove {}", artifacts.display()))?;
756 }
757 let _ = std::fs::remove_file(self.turn_path(&resolved));
758 Ok(())
759 }
760}
761
762struct StoreGuard<'a> {
765 _file: TurnLock,
766 _mutex: MutexGuard<'a, ()>,
767}
768
769#[derive(Debug, Serialize, Deserialize)]
772struct TurnRecord {
773 token: String,
774 pid: u32,
775 beat_at: Timestamp,
776}
777
778impl TurnRecord {
779 fn fresh(&self, now: Timestamp) -> bool {
780 now.as_second() - self.beat_at.as_second() <= crate::ask::LEASE_TTL.as_secs() as i64
781 }
782}
783
784fn fresh_token() -> String {
788 static SEQ: std::sync::atomic::AtomicU64 = std::sync::atomic::AtomicU64::new(0);
789 let n = SEQ.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
790 let seed = crate::rng::entropy() ^ n.wrapping_mul(0x9E37_79B9_7F4A_7C15);
791 crate::rng::SplitMix64::new(seed).uuid_v4()
792}
793
794fn read_turn(path: &Path) -> Option<TurnRecord> {
795 serde_json::from_str(&std::fs::read_to_string(path).ok()?).ok()
796}
797
798const TAKEOVER_LOCK_TTL: Duration = Duration::from_secs(10);
800
801const TICKET_TTL: Duration = Duration::from_secs(10);
804
805const TICKET_GENERATIONS: u32 = 16;
807
808const TICKET_SWEEP_AGE: Duration = Duration::from_secs(3600);
810
811fn create_exclusive(path: &Path, body: &str) -> Result<bool> {
813 use std::io::Write as _;
814 match std::fs::OpenOptions::new()
815 .write(true)
816 .create_new(true)
817 .open(path)
818 {
819 Ok(mut f) => {
820 if let Err(e) = f.write_all(body.as_bytes()) {
821 drop(f);
822 let _ = std::fs::remove_file(path);
823 return Err(e).with_context(|| format!("write {}", path.display()));
824 }
825 Ok(true)
826 }
827 Err(e) if e.kind() == std::io::ErrorKind::AlreadyExists => Ok(false),
828 Err(e) => Err(e).with_context(|| format!("create {}", path.display())),
829 }
830}
831
832fn create_turn(path: &Path, token: &str, now: Timestamp) -> Result<bool> {
833 let record = TurnRecord {
834 token: token.to_owned(),
835 pid: std::process::id(),
836 beat_at: now,
837 };
838 let body = serde_json::to_string(&record).context("serialize turn lease")?;
839 let tmp = path.with_extension(format!("turn.{token}.new"));
842 publish_exclusive(path, &tmp, &body)
843}
844
845fn lease_blocks(path: &Path, now: Timestamp) -> bool {
849 if read_turn(path).is_some_and(|r| r.fresh(now)) {
850 return true;
851 }
852 std::fs::metadata(path).is_ok_and(|m| {
853 m.len() == 0
854 && m.modified()
855 .ok()
856 .and_then(|t| t.elapsed().ok())
857 .is_some_and(|age| age <= TAKEOVER_LOCK_TTL)
858 })
859}
860
861fn link_unsupported(e: &std::io::Error) -> bool {
864 e.kind() == std::io::ErrorKind::Unsupported || (cfg!(windows) && e.raw_os_error() == Some(1))
865}
866
867fn create_in_place(path: &Path, body: &str) -> Result<bool> {
885 use std::io::Write as _;
886 let mut f = match std::fs::OpenOptions::new()
887 .write(true)
888 .create_new(true)
889 .open(path)
890 {
891 Ok(f) => f,
892 Err(e) if e.kind() == std::io::ErrorKind::AlreadyExists => return Ok(false),
893 Err(e) => return Err(e).with_context(|| format!("create {}", path.display())),
894 };
895 f.write_all(body.as_bytes())
896 .with_context(|| format!("write {}", path.display()))?;
897 Ok(std::fs::read_to_string(path).is_ok_and(|t| t == body))
900}
901
902fn publish_exclusive(path: &Path, tmp: &Path, body: &str) -> Result<bool> {
903 std::fs::write(tmp, body).with_context(|| format!("write {}", tmp.display()))?;
904 #[cfg(test)]
905 let linked = if failpoint::no_link_forced() {
906 Err(std::io::Error::from(std::io::ErrorKind::Unsupported))
907 } else {
908 std::fs::hard_link(tmp, path)
909 };
910 #[cfg(not(test))]
911 let linked = std::fs::hard_link(tmp, path);
912 let out = match linked {
913 Ok(()) => Ok(true),
914 Err(e) if e.kind() == std::io::ErrorKind::AlreadyExists => Ok(false),
915 Err(e) if link_unsupported(&e) => create_in_place(path, body),
916 Err(e) => Err(e).with_context(|| format!("create {}", path.display())),
917 };
918 let _ = std::fs::remove_file(tmp);
919 out
920}
921
922fn remove_if_carries(path: &Path, expected: &str) -> bool {
933 let gone = path.with_extension(format!("gone.{}", fresh_token()));
934 if std::fs::rename(path, &gone).is_err() {
935 return false;
936 }
937 let found = std::fs::read_to_string(&gone);
938 if found.as_ref().is_ok_and(|t| t == expected) {
939 let _ = std::fs::remove_file(&gone);
940 return true;
941 }
942 if let Ok(body) = found {
943 let back = path.with_extension(format!("back.{}", fresh_token()));
944 match publish_exclusive(path, &back, &body) {
945 Ok(true) => {}
946 Ok(false) | Err(_) => tracing::warn!(
947 "{} was replaced while a stale removal had it aside; \
948 the displaced file is dropped",
949 path.display()
950 ),
951 }
952 }
953 let _ = std::fs::remove_file(&gone);
954 false
955}
956
957struct TurnLock {
961 path: PathBuf,
962 token: String,
963}
964
965impl TurnLock {
966 fn token() -> String {
967 fresh_token()
968 }
969
970 fn publish(path: &Path, token: &str) -> Result<bool> {
974 let tmp = path.with_extension(format!("lock.{token}.new"));
975 publish_exclusive(path, &tmp, token)
976 }
977
978 fn take(lease: &Path) -> Result<Option<Self>> {
979 let path = lease.with_extension("turn.lock");
980 let token = Self::token();
981 if Self::publish(&path, &token)? {
982 return Ok(Some(Self { path, token }));
983 }
984 let Ok(seen) = std::fs::read_to_string(&path) else {
985 return Ok(None);
986 };
987 let key = if !seen.is_empty() && seen.chars().all(|c| c.is_ascii_alphanumeric() || c == '-')
990 {
991 seen.as_str()
992 } else {
993 "invalid"
994 };
995 let aged = std::fs::metadata(&path)
996 .and_then(|m| m.modified())
997 .ok()
998 .and_then(|t| t.elapsed().ok())
999 .is_some_and(|age| age > TAKEOVER_LOCK_TTL);
1000 if !aged {
1001 return Ok(None);
1002 }
1003 let mut won = false;
1015 for n in 0..TICKET_GENERATIONS {
1016 let ticket = path.with_extension(format!("lock.{key}.break.{n}"));
1017 if create_exclusive(&ticket, "")? {
1018 won = true;
1019 break;
1020 }
1021 let stale = std::fs::metadata(&ticket)
1022 .and_then(|m| m.modified())
1023 .ok()
1024 .and_then(|t| t.elapsed().ok())
1025 .is_some_and(|age| age > TICKET_TTL);
1026 if !stale {
1027 return Ok(None);
1028 }
1029 }
1030 if !won {
1031 Self::sweep_tickets(&path);
1036 return Ok(None);
1037 }
1038 Self::sweep_tickets(&path);
1039 if !remove_if_carries(&path, &seen) {
1045 return Ok(None);
1046 }
1047 if Self::publish(&path, &token)? {
1048 return Ok(Some(Self { path, token }));
1049 }
1050 Ok(None)
1051 }
1052
1053 fn sweep_tickets(path: &Path) {
1056 let (Some(dir), Some(name)) = (path.parent(), path.file_name().and_then(|n| n.to_str()))
1057 else {
1058 return;
1059 };
1060 let prefix = format!("{name}.");
1061 let Ok(entries) = std::fs::read_dir(dir) else {
1062 return;
1063 };
1064 for entry in entries.flatten() {
1065 let file = entry.file_name();
1066 let Some(file) = file.to_str() else { continue };
1067 if !(file.starts_with(&prefix) && file.contains(".break.")) {
1068 continue;
1069 }
1070 let old = entry
1071 .metadata()
1072 .and_then(|m| m.modified())
1073 .ok()
1074 .and_then(|t| t.elapsed().ok())
1075 .is_some_and(|age| age > TICKET_SWEEP_AGE);
1076 if old {
1077 let _ = std::fs::remove_file(entry.path());
1078 }
1079 }
1080 }
1081
1082 fn take_patiently(lease: &Path) -> Option<Self> {
1084 for _ in 0..50 {
1085 match Self::take(lease) {
1086 Ok(Some(lock)) => return Some(lock),
1087 Ok(None) => std::thread::sleep(Duration::from_millis(10)),
1088 Err(_) => return None,
1089 }
1090 }
1091 None
1092 }
1093}
1094
1095impl Drop for TurnLock {
1096 fn drop(&mut self) {
1097 remove_if_carries(&self.path, &self.token);
1099 }
1100}
1101
1102pub const TURN_BEAT: Duration = Duration::from_secs(20);
1105
1106#[derive(Debug)]
1111pub struct TurnLease {
1112 talk: String,
1113 path: PathBuf,
1114 token: String,
1115}
1116
1117impl TurnLease {
1118 pub fn holds(&self) -> bool {
1120 read_turn(&self.path).is_some_and(|r| r.token == self.token)
1121 }
1122
1123 pub fn beat(&self) -> Result<bool> {
1127 let _lock = TurnLock::take_patiently(&self.path)
1128 .with_context(|| format!("lock {} to renew it", self.path.display()))?;
1129 let Some(mut record) = read_turn(&self.path).filter(|r| r.token == self.token) else {
1130 return Ok(false);
1131 };
1132 record.beat_at = Timestamp::now();
1133 let body = serde_json::to_string(&record).context("serialize turn lease")?;
1134 let tmp = self.path.with_extension(format!("turn.{}.tmp", self.token));
1135 write_atomic(&tmp, &self.path, &body)?;
1136 Ok(true)
1137 }
1138
1139 pub async fn beating<T>(&self, fut: impl std::future::Future<Output = T>) -> Result<T> {
1144 self.beating_every(TURN_BEAT, fut).await
1145 }
1146
1147 async fn beating_every<T>(
1148 &self,
1149 period: Duration,
1150 fut: impl std::future::Future<Output = T>,
1151 ) -> Result<T> {
1152 tokio::pin!(fut);
1153 loop {
1154 match tokio::time::timeout(period, &mut fut).await {
1155 Ok(out) => return Ok(out),
1156 Err(_) => match self.beat() {
1157 Ok(true) => {}
1158 Ok(false) => bail!(
1159 "the turn lease {} was taken over; this turn is stopped",
1160 self.path.display()
1161 ),
1162 Err(e) => tracing::warn!("{e:#}"),
1163 },
1164 }
1165 }
1166 }
1167}
1168
1169impl Drop for TurnLease {
1170 fn drop(&mut self) {
1171 if let Some(_lock) = TurnLock::take_patiently(&self.path) {
1174 if read_turn(&self.path).is_some_and(|r| r.token == self.token) {
1175 let _ = std::fs::remove_file(&self.path);
1176 }
1177 }
1178 }
1179}
1180
1181pub fn begin(store: &Talks, cfg: &Config, repo: PathBuf, agent: Option<&str>) -> Result<Talk> {
1191 begin_with(store, cfg, repo, agent, &Preferred::default())
1192}
1193
1194#[derive(Debug, Default, Clone)]
1198pub struct Preferred {
1199 pub agent: Option<String>,
1201 pub persona: Option<String>,
1203 pub implementers: Option<i64>,
1205}
1206
1207pub fn begin_with(
1214 store: &Talks,
1215 cfg: &Config,
1216 repo: PathBuf,
1217 agent: Option<&str>,
1218 preferred: &Preferred,
1219) -> Result<Talk> {
1220 let repo = repo.canonicalize().unwrap_or(repo);
1223 let head = || -> Result<AgentSpec> {
1226 Ok(agent::pick_chain(
1227 &cfg.agents,
1228 cfg.roles.chatter.as_ref(),
1229 &agent::installed,
1230 "chatter",
1231 )?
1232 .remove(0))
1233 };
1234 let mut fallback = agent.is_none();
1235 let spec = match agent {
1236 Some(id) => agent::pick(&cfg.agents, Some(id), &agent::installed)?,
1237 None => {
1238 match preferred
1239 .agent
1240 .as_deref()
1241 .map(str::trim)
1242 .filter(|id| !id.is_empty())
1243 {
1244 Some(id) => match agent::pick(&cfg.agents, Some(id), &agent::installed) {
1245 Ok(spec) => {
1246 fallback = head().is_ok_and(|head| spec.id == head.id);
1247 spec
1248 }
1249 Err(_) => head()?,
1250 },
1251 None => head()?,
1252 }
1253 }
1254 };
1255 let persona = preferred
1256 .persona
1257 .as_deref()
1258 .and_then(|id| crate::persona::find(&cfg.talk.personas, id))
1259 .filter(|p| !p.is_default())
1260 .map(|p| p.id)
1261 .unwrap_or_default();
1262 let implementers = preferred
1263 .implementers
1264 .and_then(|n| u8::try_from(n).ok())
1265 .and_then(|n| check_implementers(n, cfg).ok())
1266 .unwrap_or(1);
1267
1268 let now = Timestamp::now();
1269 let mut talk = Talk {
1270 posts_read_through: Some(0),
1271 pending_breaks: None,
1272 schema: SCHEMA,
1273 id: new_id(),
1274 repo,
1275 agent: spec.id.clone(),
1276 status: TalkStatus::Open,
1277 turns: Vec::new(),
1278 pending: String::new(),
1279 pending_attachments: Vec::new(),
1280 fallback,
1281 persona,
1282 persona_dirty: false,
1283 implementers,
1284 implementers_dirty: false,
1285 notified: Vec::new(),
1286 created_at: now,
1287 updated_at: now,
1288 seat: SeatState::new(SEAT, &spec.id, crate::rng::entropy()),
1289 };
1290 store.put(&mut talk)?;
1291 Ok(talk)
1292}
1293
1294pub fn record(
1301 talk: &mut Talk,
1302 store: &Talks,
1303 text: &str,
1304 attachments: Vec<Attachment>,
1305) -> Result<String> {
1306 let _guard = store.guard()?;
1314 let Ok(fresh) = store.get(&talk.id) else {
1319 bail!("talk {} was deleted", talk.short());
1320 };
1321 talk.status = fresh.status;
1322 talk.pending = fresh.pending;
1325 talk.pending_breaks = fresh.pending_breaks;
1326 talk.pending_attachments = fresh.pending_attachments;
1327 talk.notified = fresh.notified;
1328 if !talk.status.open() {
1329 bail!(
1330 "talk {} is {} and takes no more turns",
1331 talk.short(),
1332 talk.status.as_str()
1333 );
1334 }
1335 let text = text.trim();
1336 if text.is_empty() && attachments.is_empty() {
1337 bail!("nothing to say");
1338 }
1339 talk.turns.push(Turn {
1340 who: Who::Operator,
1341 body: text.to_owned(),
1342 at: Timestamp::now(),
1343 attachments,
1344 usage: None,
1345 breaks: Some(Vec::new()),
1346 posted_by: None,
1347 });
1348 store.put(talk)?;
1349 Ok(text.to_owned())
1350}
1351
1352pub fn queue(
1354 talk: &mut Talk,
1355 store: &Talks,
1356 text: &str,
1357 attachments: Vec<Attachment>,
1358) -> Result<()> {
1359 queue_keyed(talk, store, text, attachments, None, &|_| false).map(|_| ())
1360}
1361
1362pub fn queue_once(talk: &mut Talk, store: &Talks, text: &str, key: &str) -> Result<bool> {
1367 queue_once_matching(talk, store, text, key, &|_| false)
1368}
1369
1370pub(crate) fn queue_once_matching(
1373 talk: &mut Talk,
1374 store: &Talks,
1375 text: &str,
1376 key: &str,
1377 previous: &dyn Fn(&str) -> bool,
1378) -> Result<bool> {
1379 queue_keyed(talk, store, text, Vec::new(), Some(key), previous)
1380}
1381
1382fn queue_keyed(
1383 talk: &mut Talk,
1384 store: &Talks,
1385 text: &str,
1386 attachments: Vec<Attachment>,
1387 key: Option<&str>,
1388 previous: &dyn Fn(&str) -> bool,
1389) -> Result<bool> {
1390 let text = text.trim();
1391 if text.is_empty() && attachments.is_empty() {
1392 bail!("nothing to say");
1393 }
1394 let _guard = store.guard()?;
1395 let mut fresh = store
1396 .get(&talk.id)
1397 .with_context(|| format!("talk {} was deleted", talk.short()))?;
1398 if !fresh.status.open() {
1399 bail!(
1400 "talk {} is {} and takes no more turns",
1401 fresh.short(),
1402 fresh.status.as_str()
1403 );
1404 }
1405 if let Some(key) = key {
1406 if fresh.notified.iter().any(|k| k == key || previous(k)) {
1407 *talk = fresh;
1408 return Ok(false);
1409 }
1410 fresh.notified.push(key.to_owned());
1411 }
1412 if !text.is_empty() {
1413 if fresh.pending.is_empty() {
1414 fresh.pending = text.to_owned();
1415 fresh.pending_breaks = Some(Vec::new());
1416 } else {
1417 fresh.pending.push_str("\n\n");
1418 if let Some(b) = fresh.pending_breaks.as_mut() {
1419 b.push(fresh.pending.len());
1420 }
1421 fresh.pending.push_str(text);
1422 }
1423 }
1424 fresh.pending_attachments.extend(attachments);
1425 store.put(&mut fresh)?;
1426 *talk = fresh;
1427 Ok(true)
1428}
1429
1430pub fn post(store: &Talks, talk_id: &str, body: &str, by: &str) -> Result<()> {
1435 let body = body.trim();
1436 if body.is_empty() {
1437 bail!("nothing to post");
1438 }
1439 let _guard = store.guard()?;
1440 let mut fresh = store
1441 .get(talk_id)
1442 .with_context(|| format!("talk {talk_id} does not exist"))?;
1443 if !fresh.status.open() {
1444 bail!(
1445 "talk {} is closed and takes no more messages",
1446 fresh.short()
1447 );
1448 }
1449 fresh.turns.push(Turn {
1450 who: Who::Agent,
1451 body: body.to_owned(),
1452 at: Timestamp::now(),
1453 attachments: Vec::new(),
1454 usage: None,
1455 breaks: None,
1456 posted_by: Some(by.to_owned()),
1457 });
1458 store.put(&mut fresh)
1459}
1460
1461fn posts_read_through(talk: &Talk) -> usize {
1463 talk.posts_read_through.unwrap_or_else(|| {
1464 talk.turns
1465 .iter()
1466 .rposition(|t| t.who != Who::Operator && t.posted_by.is_none())
1467 .map_or(0, |i| i + 1)
1468 })
1469}
1470
1471pub fn posts_since_last_reply(talk: &Talk) -> Option<String> {
1473 let posts: Vec<&Turn> = talk
1474 .turns
1475 .iter()
1476 .skip(posts_read_through(talk))
1477 .filter(|t| t.posted_by.is_some())
1478 .collect();
1479 if posts.is_empty() {
1480 return None;
1481 }
1482 let mut out = String::from("# Posted by a task\n\n");
1483 for t in posts {
1484 out.push_str(&format!(
1485 "Task seat {} posted to this conversation: {}\n\n",
1486 short(t.posted_by.as_deref().unwrap_or_default()),
1487 t.body
1488 ));
1489 }
1490 Some(out)
1491}
1492
1493pub fn drain(talk: &mut Talk, store: &Talks) -> Result<Option<String>> {
1495 let _guard = store.guard()?;
1496 let mut fresh = store
1497 .get(&talk.id)
1498 .with_context(|| format!("talk {} was deleted", talk.short()))?;
1499 if !fresh.status.open() || (fresh.pending.is_empty() && fresh.pending_attachments.is_empty()) {
1500 *talk = fresh;
1501 return Ok(None);
1502 }
1503 let text = std::mem::take(&mut fresh.pending);
1504 let attachments = std::mem::take(&mut fresh.pending_attachments);
1505 let breaks = fresh.pending_breaks.take();
1506 fresh.turns.push(Turn {
1507 who: Who::Operator,
1508 body: text.clone(),
1509 at: Timestamp::now(),
1510 attachments,
1511 usage: None,
1512 breaks,
1513 posted_by: None,
1514 });
1515 store.put(&mut fresh)?;
1516 *talk = fresh;
1517 Ok(Some(text))
1518}
1519
1520pub async fn say(
1523 lease: &TurnLease,
1524 talk: &mut Talk,
1525 store: &Talks,
1526 cfg: &Config,
1527 text: &str,
1528 attachments: Vec<Attachment>,
1529) -> Result<()> {
1530 check_lease(lease, talk)?;
1531 let text = record(talk, store, text, attachments)?;
1532 respond(lease, talk, store, cfg, &text).await
1533}
1534
1535pub async fn respond(
1541 lease: &TurnLease,
1542 talk: &mut Talk,
1543 store: &Talks,
1544 cfg: &Config,
1545 text: &str,
1546) -> Result<()> {
1547 check_lease(lease, talk)?;
1548 lease
1549 .beating(turn(talk, store, cfg, text))
1550 .await
1551 .and_then(|done| done)
1552}
1553
1554fn check_lease(lease: &TurnLease, talk: &Talk) -> Result<()> {
1555 if lease.talk != talk.id {
1556 bail!("the turn lease is for talk {}, not {}", lease.talk, talk.id);
1557 }
1558 if !lease.holds() {
1561 bail!("the turn lease for talk {} is no longer held", talk.short());
1562 }
1563 Ok(())
1564}
1565
1566pub fn close(talk: &mut Talk, store: &Talks) -> Result<()> {
1584 let _guard = store.guard()?;
1585 let mut fresh = store
1586 .get(&talk.id)
1587 .with_context(|| format!("talk {} was deleted", talk.short()))?;
1588 fresh.status = TalkStatus::Closed;
1589 fresh.pending.clear();
1591 fresh.pending_breaks = None;
1592 fresh.pending_attachments.clear();
1593 store.put(&mut fresh)?;
1594 *talk = fresh;
1595 Ok(())
1596}
1597
1598pub fn reopen(talk: &mut Talk, store: &Talks) -> Result<()> {
1609 let _guard = store.guard()?;
1610 let mut fresh = store
1611 .get(&talk.id)
1612 .with_context(|| format!("talk {} was deleted", talk.short()))?;
1613 fresh.status = TalkStatus::Open;
1614 store.put(&mut fresh)?;
1615 *talk = fresh;
1616 Ok(())
1617}
1618
1619pub fn switch_agent(talk: &mut Talk, store: &Talks, spec: &AgentSpec) -> Result<bool> {
1632 let _guard = store.guard()?;
1633 let mut fresh = store
1634 .get(&talk.id)
1635 .with_context(|| format!("talk {} was deleted", talk.short()))?;
1636 if fresh.agent == spec.id {
1637 *talk = fresh;
1638 return Ok(false);
1639 }
1640 let from = std::mem::replace(&mut fresh.agent, spec.id.clone());
1641 fresh.seat = SeatState::new(SEAT, &spec.id, crate::rng::entropy());
1642 fresh.fallback = false;
1644 fresh.turns.push(Turn {
1645 breaks: None,
1646 posted_by: None,
1647 who: Who::Agent,
1648 body: format!("{MAGI_NOTE}agent changed from {from} to {}", spec.id),
1649 at: Timestamp::now(),
1650 attachments: Vec::new(),
1651 usage: None,
1652 });
1653 store.put(&mut fresh)?;
1654 *talk = fresh;
1655 Ok(true)
1656}
1657
1658pub fn switch_persona(talk: &mut Talk, store: &Talks, id: &str) -> Result<bool> {
1666 let id = if id.trim() == crate::persona::DEFAULT_ID {
1667 ""
1668 } else {
1669 id.trim()
1670 };
1671 let _guard = store.guard()?;
1672 let mut fresh = store
1673 .get(&talk.id)
1674 .with_context(|| format!("talk {} was deleted", talk.short()))?;
1675 if fresh.persona == id {
1676 *talk = fresh;
1677 return Ok(false);
1678 }
1679 let from = std::mem::replace(&mut fresh.persona, id.to_owned());
1680 fresh.persona_dirty = true;
1681 let label = |p: &str| {
1682 if p.is_empty() {
1683 crate::persona::DEFAULT_ID.to_owned()
1684 } else {
1685 p.to_owned()
1686 }
1687 };
1688 fresh.turns.push(Turn {
1689 breaks: None,
1690 posted_by: None,
1691 who: Who::Agent,
1692 body: format!(
1693 "{MAGI_NOTE}persona changed from {} to {}",
1694 label(&from),
1695 label(id)
1696 ),
1697 at: Timestamp::now(),
1698 attachments: Vec::new(),
1699 usage: None,
1700 });
1701 store.put(&mut fresh)?;
1702 *talk = fresh;
1703 Ok(true)
1704}
1705
1706pub fn clear_pending(talk: &mut Talk, store: &Talks) -> Result<()> {
1708 let _guard = store.guard()?;
1709 let mut fresh = store
1710 .get(&talk.id)
1711 .with_context(|| format!("talk {} was deleted", talk.short()))?;
1712 fresh.pending.clear();
1713 fresh.pending_breaks = None;
1714 fresh.pending_attachments.clear();
1715 store.put(&mut fresh)?;
1716 *talk = fresh;
1717 Ok(())
1718}
1719
1720pub fn clear_pending_if_matches(
1722 talk: &mut Talk,
1723 store: &Talks,
1724 expected_text: &str,
1725 expected_attachments: &[String],
1726) -> Result<bool> {
1727 let _guard = store.guard()?;
1728 let mut fresh = store
1729 .get(&talk.id)
1730 .with_context(|| format!("talk {} was deleted", talk.short()))?;
1731 if !pending_matches(&fresh, expected_text, expected_attachments) {
1732 *talk = fresh;
1733 return Ok(false);
1734 }
1735 fresh.pending.clear();
1736 fresh.pending_breaks = None;
1737 fresh.pending_attachments.clear();
1738 store.put(&mut fresh)?;
1739 *talk = fresh;
1740 Ok(true)
1741}
1742
1743pub fn edit_pending_text(
1747 talk: &mut Talk,
1748 store: &Talks,
1749 text: &str,
1750 expected_text: &str,
1751 expected_attachments: &[String],
1752) -> Result<bool> {
1753 let _guard = store.guard()?;
1754 let mut fresh = store
1755 .get(&talk.id)
1756 .with_context(|| format!("talk {} was deleted", talk.short()))?;
1757 if !pending_matches(&fresh, expected_text, expected_attachments) {
1758 *talk = fresh;
1759 return Ok(false);
1760 }
1761 fresh.pending = text.trim().to_owned();
1762 fresh.pending_breaks = Some(Vec::new());
1763 store.put(&mut fresh)?;
1764 *talk = fresh;
1765 Ok(true)
1766}
1767
1768fn pending_matches(talk: &Talk, expected_text: &str, expected_attachments: &[String]) -> bool {
1769 talk.pending == expected_text
1770 && talk
1771 .pending_attachments
1772 .iter()
1773 .map(|attachment| &attachment.id)
1774 .eq(expected_attachments.iter())
1775}
1776
1777async fn turn(talk: &mut Talk, store: &Talks, cfg: &Config, text: &str) -> Result<()> {
1784 let prompt_turns = talk.turns.len();
1788 talk.posts_read_through = Some(posts_read_through(talk));
1789 let spec = cfg
1790 .agents
1791 .iter()
1792 .find(|a| a.id == talk.agent)
1793 .with_context(|| {
1794 format!(
1795 "talk {} was opened with agent `{}`, which is no longer in \
1796 the roster; restore it in magi.toml or start a new \
1797 conversation",
1798 talk.short(),
1799 talk.agent
1800 )
1801 })?;
1802
1803 let last_note = attachment_note(
1807 store,
1808 &talk.id,
1809 talk.turns
1810 .last()
1811 .map_or(&[][..], |t| t.attachments.as_slice()),
1812 );
1813
1814 let attachment_paths: Vec<PathBuf> = talk
1820 .turns
1821 .iter()
1822 .flat_map(|t| t.attachments.iter())
1823 .filter_map(|a| store.attachment_path(&talk.id, a))
1824 .collect();
1825
1826 let questions =
1836 crate::run::try_home().map(|home| crate::ask::Questions::at(home.join("questions")));
1837 let consulted = questions
1838 .as_ref()
1839 .is_some_and(|q| crate::consult::pending_consults(q, &talk.id));
1840 let consult_roots: Vec<PathBuf> = match &questions {
1841 Some(q) if consulted => vec![q.root().to_path_buf()],
1842 _ => Vec::new(),
1843 };
1844
1845 let (allow_write, unsandboxed) = turn_access(cfg.talk.allow_write, consulted);
1846
1847 let artifacts = store.artifacts_of(&talk.id);
1848 let operator_turns = talk.turns.iter().filter(|t| t.who == Who::Operator).count();
1851 let stem = format!("turn-{}", operator_turns.max(1));
1852 let cache_dir = cfg.cache_dir();
1855
1856 let mut chain = vec![spec.clone()];
1859 if let Some(choice) = cfg.roles.chatter.as_ref()
1860 && talk.fallback
1861 {
1862 for id in choice.ids() {
1863 if id == talk.agent || chain.iter().any(|s| s.id == id) {
1864 continue;
1865 }
1866 match agent::pick(&cfg.agents, Some(id), &agent::installed) {
1867 Ok(s) => chain.push(s),
1868 Err(e) => tracing::warn!("[roles] chatter: skipping `{id}`: {e:#}"),
1869 }
1870 }
1871 }
1872
1873 let persona = crate::persona::active(&cfg.talk.personas, &talk.persona);
1874 let operator_name = cfg.talk.operator_name();
1875 let persona_update = if talk.persona_dirty {
1876 format!(
1877 "{}\n\n",
1878 crate::persona::update_block_for(persona.as_ref(), operator_name)
1879 )
1880 } else {
1881 String::new()
1882 };
1883
1884 let filing_update = if talk.implementers_dirty {
1887 format!("{}\n\n", filing_update_block(talk.implementers))
1888 } else {
1889 String::new()
1890 };
1891
1892 let mut outcome = None;
1893 let mut fell_back_from: Option<String> = None;
1894 let mut first_try: Option<(String, SeatState)> = None;
1897 for (n, spec) in chain.iter().enumerate() {
1898 if n > 0 {
1899 if first_try.is_none() {
1900 first_try = Some((talk.agent.clone(), talk.seat.clone()));
1901 }
1902 tracing::warn!("chat: falling back from `{}` to `{}`", talk.agent, spec.id);
1903 fell_back_from.get_or_insert_with(|| talk.agent.clone());
1906 talk.agent = spec.id.clone();
1907 talk.seat = SeatState::new(SEAT, &spec.id, crate::rng::entropy());
1908 }
1909 let resuming = agent::has_session(spec.kind, &talk.seat, cfg.graph.sessions);
1910 let first_ever = talk.turns.len() <= 1;
1911 let body = if talk.seat.turns == 0 && first_ever {
1912 format!(
1913 "{}\n\n# Operator\n\n{text}{last_note}",
1914 briefing_for(
1915 &talk.repo,
1916 &cfg.graph.language,
1917 cfg.talk.allow_write,
1918 persona.as_ref(),
1919 operator_name,
1920 talk.implementers
1921 )
1922 )
1923 } else if talk.seat.turns == 0 {
1924 format!(
1927 "{}\n\n{}\n\n# Operator\n\n{text}{last_note}",
1928 briefing_for(
1929 &talk.repo,
1930 &cfg.graph.language,
1931 cfg.talk.allow_write,
1932 persona.as_ref(),
1933 operator_name,
1934 talk.implementers
1935 ),
1936 transcript(talk, store)
1937 )
1938 } else if resuming {
1939 let posts = posts_since_last_reply(talk).unwrap_or_default();
1940 format!("{persona_update}{filing_update}{posts}{text}{last_note}")
1941 } else {
1942 let mut standing = match (&persona, talk.persona_dirty) {
1946 (Some(p), false) => format!("{}\n", crate::persona::section_for(p, operator_name)),
1947 _ => persona_update.clone(),
1948 };
1949 if let (None, Some(n)) = (&persona, operator_name) {
1952 standing.push_str(&format!(
1953 "# Addressing the operator\n{}\n",
1954 crate::persona::addressing(n)
1955 ));
1956 }
1957 if talk.implementers_dirty || talk.implementers != 1 {
1958 standing.push_str(&format!("{}\n\n", filing_update_block(talk.implementers)));
1959 }
1960 format!("{}\n\n{standing}{text}{last_note}", transcript(talk, store))
1961 };
1962 let attempt_stem = if n == 0 {
1963 stem.clone()
1964 } else {
1965 format!("{stem}-{}", spec.id)
1966 };
1967 let inv = Invocation {
1968 cwd: &talk.repo,
1969 prompt: &body,
1970 timeout: turn_timeout(cfg),
1971 allow_write,
1976 unsandboxed,
1977 sessions: cfg.graph.sessions,
1978 artifacts: &artifacts,
1979 stem: &attempt_stem,
1980 run: &talk.id,
1983 node: crate::queue::CHAT_NODE,
1984 cache_dir: cache_dir.as_deref(),
1985 attachments: &attachment_paths,
1986 writable: &consult_roots,
1987 };
1988 let result = agent::invoke(spec, &mut talk.seat, &inv).await;
1989 let advance = agent::chain_advances(&result);
1990 if n == 0 || !advance {
1991 outcome = Some(result);
1992 } else {
1993 tracing::warn!("chat: fallback agent `{}` also failed", spec.id);
1995 }
1996 if !advance {
1997 break;
1998 }
1999 }
2000 if outcome.as_ref().is_some_and(agent::chain_advances) {
2001 if let Some((id, seat)) = first_try {
2004 talk.agent = id;
2005 talk.seat = seat;
2006 fell_back_from = None;
2007 }
2008 }
2009 let outcome = outcome.expect("a chain holds at least one agent");
2010 let note = |why: String| Turn {
2011 breaks: None,
2012 posted_by: None,
2013 who: Who::Agent,
2014 body: format!("{MAGI_NOTE}{why}"),
2015 at: Timestamp::now(),
2016 attachments: Vec::new(),
2017 usage: None,
2018 };
2019 let (reply, failure) = match outcome {
2020 Err(e) => (
2021 note(format!("could not run agent `{}`: {e}", talk.agent)),
2022 Some(format!("could not run agent `{}`: {e}", talk.agent)),
2023 ),
2024 Ok(out) if out.quota_exhausted() => {
2025 let reset = out
2026 .quota
2027 .as_ref()
2028 .and_then(|q| q.reset.clone())
2029 .map_or_else(String::new, |r| format!(" (resets {r})"));
2030 let why = format!(
2031 "agent `{}` is out of quota{reset}; your message is saved, so \
2032 say it again when the window reopens",
2033 talk.agent
2034 );
2035 (note(why.clone()), Some(why))
2036 }
2037 Ok(out) if out.timed_out => {
2038 let why = format!(
2039 "agent `{}` did not answer within {}s; your message is saved",
2040 talk.agent,
2041 turn_timeout(cfg).as_secs()
2042 );
2043 (note(why.clone()), Some(why))
2044 }
2045 Ok(out) if !out.usable() => {
2046 let why = format!(
2047 "agent `{}` produced no answer (exit {}); your message is saved",
2048 talk.agent,
2049 out.exit_code
2050 .map_or_else(|| "unknown".to_owned(), |c| c.to_string())
2051 );
2052 (note(why.clone()), Some(why))
2053 }
2054 Ok(out) => (
2055 Turn {
2056 breaks: None,
2057 posted_by: None,
2058 who: Who::Agent,
2059 body: out.text.trim().to_owned(),
2060 at: Timestamp::now(),
2061 attachments: Vec::new(),
2062 usage: out.context_tokens.map(|context_tokens| TurnUsage {
2065 context_tokens,
2066 agent: talk.agent.clone(),
2067 model: cfg
2068 .agents
2069 .iter()
2070 .find(|a| a.id == talk.agent)
2071 .and_then(|a| a.model.clone()),
2072 }),
2073 },
2074 None,
2075 ),
2076 };
2077
2078 let _guard = store.guard()?;
2090 let Ok(fresh) = store.get(&talk.id) else {
2096 return Ok(());
2097 };
2098 talk.status = fresh.status;
2099 talk.pending = fresh.pending;
2103 talk.pending_breaks = fresh.pending_breaks;
2104 talk.pending_attachments = fresh.pending_attachments;
2105 talk.notified = fresh.notified;
2106 if fresh.turns.len() > talk.turns.len() {
2108 let posted: Vec<Turn> = fresh.turns[talk.turns.len()..]
2109 .iter()
2110 .filter(|t| t.posted_by.is_some())
2111 .cloned()
2112 .collect();
2113 talk.turns.extend(posted);
2114 }
2115 if let Some(from) = fell_back_from.filter(|_| failure.is_none()) {
2116 talk.turns.push(note(format!(
2119 "agent changed from {from} to {} (fallback)",
2120 talk.agent
2121 )));
2122 }
2123 if failure.is_none() {
2126 talk.persona_dirty = false;
2127 talk.implementers_dirty = false;
2128 talk.posts_read_through = Some(prompt_turns);
2129 }
2130 talk.turns.push(reply);
2131 if let Err(put_err) = store.put(talk) {
2132 let lost = talk.turns.pop().expect("just pushed above");
2141 let stash = stash_lost_turn(store, &talk.id, &stem, &lost);
2142 let why = match &stash {
2143 Ok(path) => format!(
2144 "agent `{}` answered, but the reply could not be saved to \
2145 this conversation ({put_err:#}); the raw text was kept at \
2146 {} - your message is saved, ask again",
2147 talk.agent,
2148 path.display()
2149 ),
2150 Err(stash_err) => format!(
2151 "agent `{}` answered, but the reply could not be saved to \
2152 this conversation ({put_err:#}), and it could not be kept \
2153 anywhere else either ({stash_err:#}); your message is \
2154 saved, ask again",
2155 talk.agent
2156 ),
2157 };
2158 talk.turns.push(note(why.clone()));
2159 return match store.put(talk) {
2166 Ok(()) => bail!("{why}"),
2167 Err(note_err) => {
2168 talk.turns.pop();
2188 Err(note_err).context(why)
2189 }
2190 };
2191 }
2192
2193 match failure {
2194 Some(why) => bail!("{why}"),
2195 None => Ok(()),
2196 }
2197}
2198
2199fn transcript(talk: &Talk, store: &Talks) -> String {
2202 let mut out = String::from(
2203 "This conversation cannot resume on the CLI's side, so here is \
2204 everything said so far; answer only the last message.\n",
2205 );
2206 for t in &talk.turns {
2207 let who = match t.who {
2208 Who::Operator => "operator",
2209 Who::Agent if t.body.starts_with(MAGI_NOTE) => "magi",
2210 Who::Agent => "you",
2211 };
2212 out.push_str(&format!("\n## {who}\n\n{}\n", t.body.trim()));
2213 out.push_str(&attachment_note(store, &talk.id, &t.attachments));
2214 }
2215 out
2216}
2217
2218fn attachment_note(store: &Talks, talk_id: &str, attachments: &[Attachment]) -> String {
2223 if attachments.is_empty() {
2224 return String::new();
2225 }
2226 let mut out = String::from(
2227 "\n\nThe operator attached the image(s) below to this message. Open \
2228 and look at each one before you answer.\n",
2229 );
2230 for att in attachments {
2231 if let Some(path) = store.attachment_path(talk_id, att) {
2232 out.push_str(&format!("\n- {} ({})", path.display(), att.mime));
2233 }
2234 }
2235 out.push('\n');
2236 out
2237}
2238
2239pub(crate) fn turn_access(talk_allow_write: bool, consulted: bool) -> (bool, bool) {
2250 (talk_allow_write || consulted, talk_allow_write)
2251}
2252
2253pub fn briefing(repo: &Path, language: &str, allow_write: bool) -> String {
2269 briefing_with(repo, language, allow_write, None, None)
2270}
2271
2272pub fn briefing_with(
2277 repo: &Path,
2278 language: &str,
2279 allow_write: bool,
2280 persona: Option<&crate::persona::Persona>,
2281 operator_name: Option<&str>,
2282) -> String {
2283 briefing_for(repo, language, allow_write, persona, operator_name, 1)
2284}
2285
2286pub const MAX_IMPLEMENTERS: u8 = 3;
2288
2289fn solo_implementers() -> u8 {
2290 1
2291}
2292
2293fn filing_flag(n: u8) -> String {
2296 if n <= 1 {
2297 "--solo".to_owned()
2298 } else {
2299 format!("--implementers {n}")
2300 }
2301}
2302
2303pub fn filing_update_block(n: u8) -> String {
2306 let flag = filing_flag(n);
2307 if n <= 1 {
2308 format!(
2309 "# Task filing update\n\nThe operator changed how many implementers tasks use. \
2310 From now on file with `magi task add {flag} --repo <repo> <instruction>` \
2311 (one implementer, straight into review).\n"
2312 )
2313 } else {
2314 format!(
2315 "# Task filing update\n\nThe operator changed how many implementers tasks use. \
2316 From now on file with `magi task add {flag} --repo <repo> <instruction>` \
2317 ({n} independent implementations compete). Do not pass `--solo` \
2318 together with `--implementers`; drop any earlier `--solo`.\n"
2319 )
2320 }
2321}
2322
2323pub fn check_implementers(n: u8, cfg: &crate::config::Config) -> std::result::Result<u8, String> {
2326 if !(1..=MAX_IMPLEMENTERS).contains(&n) {
2327 return Err(format!(
2328 "implementers must be 1..={MAX_IMPLEMENTERS}, got {n}"
2329 ));
2330 }
2331 if n > 1 {
2332 let mut cfg = cfg.clone();
2333 crate::queue::RunOverrides {
2334 candidates: Some(usize::from(n)),
2335 ..Default::default()
2336 }
2337 .apply(&mut cfg);
2338 cfg.resolve_roles()
2339 .map_err(|e| format!("{n} implementers is not allowed here: {e:#}"))?;
2340 }
2341 Ok(n)
2342}
2343
2344pub fn switch_implementers(talk: &mut Talk, store: &Talks, n: u8) -> Result<bool> {
2347 let _guard = store.guard()?;
2348 let mut fresh = store
2349 .get(&talk.id)
2350 .with_context(|| format!("talk {} was deleted", talk.short()))?;
2351 if fresh.implementers == n {
2352 *talk = fresh;
2353 return Ok(false);
2354 }
2355 let from = std::mem::replace(&mut fresh.implementers, n);
2356 fresh.implementers_dirty = true;
2357 fresh.turns.push(Turn {
2358 breaks: None,
2359 posted_by: None,
2360 who: Who::Agent,
2361 body: format!("{MAGI_NOTE}implementers changed from {from} to {n}"),
2362 at: Timestamp::now(),
2363 attachments: Vec::new(),
2364 usage: None,
2365 });
2366 store.put(&mut fresh)?;
2367 *talk = fresh;
2368 Ok(true)
2369}
2370
2371pub fn briefing_for(
2374 repo: &Path,
2375 language: &str,
2376 allow_write: bool,
2377 persona: Option<&crate::persona::Persona>,
2378 operator_name: Option<&str>,
2379 implementers: u8,
2380) -> String {
2381 let flag = filing_flag(implementers);
2382 let shape = if implementers <= 1 {
2383 "Use --solo: it runs the task through one implementer \
2384 straight into review instead of the usual multi-agent competition, \
2385 which is the right shape for a change this conversation has already \
2386 settled, rather than one still worth several independent takes."
2387 .to_owned()
2388 } else {
2389 format!(
2390 "Use --implementers {implementers}: it has {implementers} \
2391 implementers work on the task independently and compete, which \
2392 is the right shape when several independent takes are worth \
2393 having. Never add --solo to it; the two do not go together."
2394 )
2395 };
2396 let write_policy = if allow_write {
2397 "Write access is enabled for this conversation (`allow_write = \
2398 true`), so you may write files - but only a small, \
2399 already-decided edit the operator names outright in this \
2400 conversation, not an implementation. This is a permission on the \
2401 conversation as a whole, not a property of whichever repository \
2402 it happened to start in: if the operator names a different \
2403 repository for that small edit, the policy allows it there too. \
2404 Your own tool may still confine writes to the repository this \
2405 conversation started in regardless - if a write elsewhere is \
2406 refused, say so plainly rather than working around it. Once you \
2407 have made an edit, say plainly what you edited. Anything bigger, \
2408 or anything still open-ended, still goes through the queue below \
2409 rather than being done here."
2410 } else {
2411 "Do not write files. Implementing a change is not this \
2412 conversation's job; a separate, blind competition of agents does \
2413 that, and a repository this conversation has already edited would \
2414 make their diffs unjudgeable."
2415 };
2416 let mut out = format!(
2417 "You are magi's standing conversation partner for its operator, who \
2418 usually has this open on a phone. Keep replies short: no preamble, \
2419 no restating what they just said.\n\n\
2420 # Repository\n\n{repo}\n\n\
2421 You may look around: read files, run shell commands, search history, \
2422 run tests - whatever answers the question. {write_policy}\n\n\
2423 A short, command-shaped message (\"list\", \"info <id>\", \"show \
2424 3cbf\") is almost always the operator asking you to look something \
2425 up, not an instruction to file - answer it yourself with `magi \
2426 list`, `magi show <id>`, `magi task list`, or the like, the same way \
2427 you would answer any other question in this conversation.\n\n\
2428 # When the operator wants something done\n\n\
2429 Run:\n\n\
2430 magi task add {flag} --repo {repo} <instruction>\n\n\
2431 and tell the operator the task id it prints, so they can follow it \
2432 from the Queue. If it refuses with a duplicate warning (the \
2433 instruction names a branch, commit or pull request that an \
2434 unfinished task, run or PR already owns), do not repeat it with \
2435 --force yourself: tell the operator what it matched and let them \
2436 decide. Write <instruction> so that an implementer who has \
2437 never seen this conversation can act on it alone - it is everything \
2438 they get. {shape}\n\n\
2439 If the operator asks for something in a different repository, \
2440 --repo does not have to be a full path: --repo owner/repo (or just \
2441 repo, when that is unambiguous) is resolved against local checkouts \
2442 the same way `magi repos` lists them. If the command fails because \
2443 nothing matches or more than one checkout shares that name, ask the \
2444 operator which repository they mean (or run `magi repos` yourself \
2445 to see the candidates) rather than guessing.\n\n\
2446 You cannot wait, and you cannot send the operator a message on your \
2447 own: you only speak when they write. A task you file reports back \
2448 into this conversation automatically when it finishes, is held or \
2449 fails, as a message headed \"{notice}\".\n\n\
2450 For a background command you run yourself, register it with \
2451 `magi talk watch --timeout 900 -- <executable> <arguments...>`, for \
2452 example `magi talk watch -- cargo test --locked`. It uses your \
2453 current directory; arguments are passed literally. For shell syntax, \
2454 explicitly call `sh -c`. Only after watch registration succeeds may \
2455 you promise a completion report. Watches default to 900 seconds, \
2456 accept at most 3600 seconds, and share four concurrent slots. Their \
2457 automatic reports resume this conversation through its own turn. \
2458 Never promise to report back on work that is neither a filed task \
2459 nor a successfully registered watch.\n\n\
2460 The current state of the code is whatever origin/main holds, not \
2461 whatever a working tree shows: a primary checkout often lags \
2462 upstream, sits on a detached HEAD and carries uncommitted changes. \
2463 Before answering about code, run `git fetch origin` in that \
2464 repository if it is cheap, then read through \
2465 `git show origin/main:<path>` or `git grep <pattern> origin/main`. \
2466 If the working tree differs, say so; if the fetch fails, say that \
2467 too, so the operator knows the answer may be stale.\n\n\
2468 If the operator attached an image (a screenshot, say) that the task \
2469 is about, pass it with `--attach <path>`, using the absolute path \
2470 the turn's attachment note gives; repeat the flag for several. \
2471 `magi task add {flag} --attach <path> <instruction>` copies the \
2472 file into the task, so the implementer receives it. Do not paste the \
2473 path into <instruction> instead: deleting this conversation deletes \
2474 its attachments, and then that path reaches no one.\n",
2475 repo = repo.display(),
2476 notice = crate::prompt::CHAT_TASK_NOTICE_HEADING,
2477 );
2478 out.push_str(&language_note(language));
2479 match (persona, operator_name) {
2480 (Some(p), n) => out.push_str(&crate::persona::section_for(p, n)),
2481 (None, Some(n)) => {
2482 out.push_str("\n# Addressing the operator\n");
2483 out.push_str(&crate::persona::addressing(n));
2484 }
2485 (None, None) => {}
2486 }
2487 out
2488}
2489
2490fn language_note(language: &str) -> String {
2493 if language.trim().is_empty() || language.eq_ignore_ascii_case("en") {
2494 String::new()
2495 } else {
2496 format!("\nHold this conversation in {language}.\n")
2497 }
2498}
2499
2500pub fn tasks_of(queue: &Queue, talk_id: &str) -> Vec<Task> {
2507 let mut tasks: Vec<Task> = queue
2508 .list()
2509 .into_iter()
2510 .filter(|t| matches!(&t.source, Source::Agent { run, .. } if run == talk_id))
2511 .collect();
2512 tasks.sort_unstable_by(|a, b| a.id.cmp(&b.id));
2513 tasks
2514}
2515
2516fn read_path(path: &Path) -> Result<Talk> {
2517 let body = std::fs::read_to_string(path).with_context(|| format!("read {}", path.display()))?;
2518 serde_json::from_str(&body).with_context(|| format!("parse {}", path.display()))
2519}
2520
2521const PUT_RETRIES: u32 = 5;
2524
2525fn write_atomic(tmp: &Path, path: &Path, body: &str) -> Result<()> {
2537 let mut last_err = None;
2538 for attempt in 0..PUT_RETRIES {
2539 if attempt > 0 {
2540 std::thread::sleep(Duration::from_millis(20 * u64::from(attempt)));
2541 }
2542 match try_write_atomic(tmp, path, body) {
2543 Ok(()) => return Ok(()),
2544 Err(e) => last_err = Some(e),
2545 }
2546 }
2547 Err(last_err.expect("the loop above always runs at least once"))
2548}
2549
2550fn try_write_atomic(tmp: &Path, path: &Path, body: &str) -> Result<()> {
2551 #[cfg(test)]
2552 if failpoint::take_forced_put_failure() {
2553 bail!("simulated write failure (test)");
2554 }
2555 std::fs::write(tmp, body).with_context(|| format!("write {}", tmp.display()))?;
2556 std::fs::rename(tmp, path).with_context(|| format!("replace {}", path.display()))?;
2557 Ok(())
2558}
2559
2560fn stash_lost_turn(store: &Talks, id: &str, stem: &str, reply: &Turn) -> Result<PathBuf> {
2565 let dir = store.artifacts_of(id);
2566 std::fs::create_dir_all(&dir).with_context(|| format!("create {}", dir.display()))?;
2567 let path = dir.join(format!("{stem}-lost.txt"));
2568 std::fs::write(&path, &reply.body).with_context(|| format!("write {}", path.display()))?;
2569 Ok(path)
2570}
2571
2572#[cfg(test)]
2579mod failpoint {
2580 use std::cell::Cell;
2581
2582 thread_local! {
2583 static FORCE_PUT_FAILURES: Cell<u32> = const { Cell::new(0) };
2584 static FORCE_NO_LINK: Cell<bool> = const { Cell::new(false) };
2585 }
2586
2587 pub(super) fn force_no_link(on: bool) {
2590 FORCE_NO_LINK.with(|c| c.set(on));
2591 }
2592
2593 pub(super) fn no_link_forced() -> bool {
2594 FORCE_NO_LINK.with(Cell::get)
2595 }
2596
2597 pub(super) fn force_put_failures(count: u32) {
2600 FORCE_PUT_FAILURES.with(|c| c.set(count));
2601 }
2602
2603 pub(super) fn take_forced_put_failure() -> bool {
2606 FORCE_PUT_FAILURES.with(|c| {
2607 let n = c.get();
2608 if n == 0 {
2609 false
2610 } else {
2611 c.set(n - 1);
2612 true
2613 }
2614 })
2615 }
2616}
2617
2618fn short(id: &str) -> &str {
2619 id.split('-').next_back().unwrap_or(id)
2620}
2621
2622fn new_id() -> String {
2623 let stamp = jiff::Zoned::now().strftime("%Y%m%d-%H%M%S");
2624 let seed = crate::rng::entropy();
2625 format!("{stamp}-{:04x}", (seed ^ (seed >> 32)) & 0xffff)
2626}
2627
2628fn attachment_ext(mime: &str) -> Option<&'static str> {
2633 match mime {
2634 "image/png" => Some("png"),
2635 "image/jpeg" => Some("jpg"),
2636 "image/gif" => Some("gif"),
2637 "image/webp" => Some("webp"),
2638 _ => None,
2639 }
2640}
2641
2642pub fn valid_attachment_id(id: &str) -> bool {
2647 id.len() == 32
2648 && id
2649 .bytes()
2650 .all(|b| b.is_ascii_digit() || (b'a'..=b'f').contains(&b))
2651}
2652
2653fn new_attachment_id() -> String {
2657 let mut r = crate::rng::SplitMix64::new(crate::rng::entropy());
2658 format!("{:016x}{:016x}", r.next_u64(), r.next_u64())
2659}
2660
2661#[cfg(test)]
2662mod tests {
2663 #[test]
2664 fn turn_access_lifts_the_sandbox_only_for_the_talk_opt_in() {
2665 use super::turn_access;
2666 assert_eq!(turn_access(false, false), (false, false));
2667 assert_eq!(turn_access(true, false), (true, true));
2668 assert_eq!(turn_access(false, true), (true, false));
2670 assert_eq!(turn_access(true, true), (true, true));
2671 }
2672
2673 #[test]
2674 fn the_briefing_points_at_origin_main_not_the_working_tree() {
2675 let b = briefing(Path::new("/r"), "en", false);
2676 assert!(b.contains("origin/main"));
2677 assert!(b.contains("git show origin/main:"));
2678 }
2679 use std::collections::BTreeMap;
2680
2681 use crate::config::{AgentChoice, AgentKind, AgentSpec, Graph};
2682 use crate::queue::{Queue, Source, Task};
2683
2684 use super::*;
2685
2686 fn ctx_agent(id: &str, model: Option<&str>) -> AgentSpec {
2687 AgentSpec {
2688 id: id.to_owned(),
2689 kind: AgentKind::Command,
2690 model: model.map(str::to_owned),
2691 command: Vec::new(),
2692 extra_args: Vec::new(),
2693 env: BTreeMap::new(),
2694 prompt_delivery: None,
2695 }
2696 }
2697
2698 fn ctx_talk(agent: &str, turns: Vec<Turn>) -> Talk {
2699 Talk {
2700 posts_read_through: None,
2701 pending_breaks: None,
2702 schema: SCHEMA,
2703 id: "20260904-014455-ab12".to_owned(),
2704 repo: PathBuf::from("."),
2705 agent: agent.to_owned(),
2706 status: TalkStatus::Open,
2707 turns,
2708 pending: String::new(),
2709 pending_attachments: Vec::new(),
2710 fallback: false,
2711 persona: String::new(),
2712 persona_dirty: false,
2713 implementers: 1,
2714 implementers_dirty: false,
2715 notified: Vec::new(),
2716 created_at: Timestamp::now(),
2717 updated_at: Timestamp::now(),
2718 seat: SeatState::new(SEAT, agent, 1),
2719 }
2720 }
2721
2722 fn reply(body: &str, usage: Option<(u64, &str, Option<&str>)>) -> Turn {
2723 Turn {
2724 breaks: None,
2725 posted_by: None,
2726 who: Who::Agent,
2727 body: body.to_owned(),
2728 at: Timestamp::now(),
2729 attachments: Vec::new(),
2730 usage: usage.map(|(t, a, m)| TurnUsage {
2731 context_tokens: t,
2732 agent: a.to_owned(),
2733 model: m.map(str::to_owned),
2734 }),
2735 }
2736 }
2737
2738 fn ctx_config(windows: &[(&str, u64)]) -> Config {
2739 Config {
2740 agents: vec![
2741 ctx_agent("small", Some("small-model")),
2742 ctx_agent("big", Some("big-model")),
2743 ctx_agent("plain", None),
2744 ],
2745 context_windows: windows.iter().map(|(k, v)| ((*k).to_owned(), *v)).collect(),
2746 ..Config::default()
2747 }
2748 }
2749
2750 #[test]
2751 fn context_usage_computes_percent_and_warns_at_eighty() {
2752 let cfg = ctx_config(&[("small-model", 1000)]);
2753 let at = |tokens| {
2754 let t = ctx_talk(
2755 "small",
2756 vec![reply("hi", Some((tokens, "small", Some("small-model"))))],
2757 );
2758 context_usage(&t, Some(&cfg))
2759 };
2760 let u = at(799);
2761 assert_eq!((u.percent, u.warn, u.window), (Some(79), false, Some(1000)));
2762 let u = at(800);
2763 assert_eq!((u.percent, u.warn), (Some(80), true));
2764 let u = at(1500);
2765 assert_eq!((u.percent, u.warn), (Some(150), true));
2766 assert!(!u.since_switch);
2767 }
2768
2769 #[test]
2770 fn context_usage_is_unknown_without_usage_and_never_looks_back() {
2771 let cfg = ctx_config(&[("small-model", 1000)]);
2772 let t = ctx_talk(
2773 "small",
2774 vec![
2775 reply("old", Some((900, "small", Some("small-model")))),
2776 reply("new", None),
2777 ],
2778 );
2779 let u = context_usage(&t, Some(&cfg));
2780 assert!(u.estimated);
2782 assert_ne!(u.tokens, Some(900));
2783 assert!(u.tokens.is_some());
2784 let t = ctx_talk(
2786 "small",
2787 vec![
2788 reply("old", Some((900, "small", Some("small-model")))),
2789 reply("magi: could not run agent", None),
2790 ],
2791 );
2792 assert_eq!(context_usage(&t, Some(&cfg)).tokens, Some(900));
2793 assert_eq!(
2794 context_usage(&ctx_talk("small", Vec::new()), Some(&cfg)).tokens,
2795 None
2796 );
2797 }
2798
2799 #[test]
2800 fn estimate_counts_chars_both_sides_and_standing_prompt() {
2801 let mut t = ctx_talk("small", vec![reply("abcdefg", None)]);
2802 assert_eq!(estimate_context_tokens(&t, 0), Some(2)); let op = Turn {
2804 who: Who::Operator,
2805 ..reply("abcdefg", None)
2806 };
2807 t.turns.push(op);
2808 assert_eq!(estimate_context_tokens(&t, 0), Some(4));
2809 assert!(
2810 estimate_context_tokens(&t, 700).unwrap() > estimate_context_tokens(&t, 0).unwrap()
2811 );
2812 let ja = ctx_talk("small", vec![reply("日本語日本語日", None)]);
2814 assert_eq!(estimate_context_tokens(&ja, 0), Some(2));
2815 let note = ctx_talk("small", vec![reply("magi: could not run agent", None)]);
2817 assert_eq!(estimate_context_tokens(¬e, 1000), None);
2818 assert_eq!(
2819 estimate_context_tokens(&ctx_talk("small", Vec::new()), 1000),
2820 None
2821 );
2822 }
2823
2824 #[test]
2825 fn context_usage_measured_wins_and_estimate_gets_percent_and_warn() {
2826 let cfg = ctx_config(&[("small-model", 1000)]);
2827 let t = ctx_talk(
2828 "small",
2829 vec![reply(
2830 &"x".repeat(5000),
2831 Some((10, "small", Some("small-model"))),
2832 )],
2833 );
2834 let u = context_usage(&t, Some(&cfg));
2835 assert_eq!((u.tokens, u.estimated), (Some(10), false));
2836 let t = ctx_talk("small", vec![reply(&"x".repeat(5000), None)]);
2837 let u = context_usage(&t, Some(&cfg));
2838 assert!(u.estimated && !u.since_switch);
2839 assert_eq!(u.window, Some(1000));
2840 assert!(u.warn && u.percent.unwrap() >= 80);
2841 let t = ctx_talk("small", vec![reply("hi", None)]);
2842 let u = context_usage(&t, Some(&cfg));
2843 assert!(u.estimated && u.percent.is_some());
2844 }
2845
2846 #[test]
2847 fn context_usage_without_a_window_shows_tokens_only() {
2848 let cfg = ctx_config(&[]);
2849 let t = ctx_talk("plain", vec![reply("hi", Some((5000, "plain", None)))]);
2851 let u = context_usage(&t, Some(&cfg));
2852 assert_eq!(
2853 (u.tokens, u.window, u.percent, u.warn),
2854 (Some(5000), None, None, false)
2855 );
2856 let t = ctx_talk(
2857 "small",
2858 vec![reply("hi", Some((5000, "small", Some("small-model"))))],
2859 );
2860 assert_eq!(context_usage(&t, Some(&cfg)).percent, None);
2861 assert_eq!(context_usage(&t, None).window, None);
2863 }
2864
2865 #[test]
2866 fn context_usage_switching_model_changes_the_denominator() {
2867 let cfg = ctx_config(&[("small-model", 1000), ("big-model", 10_000)]);
2868 let used = reply("hi", Some((900, "small", Some("small-model"))));
2869 let before = context_usage(&ctx_talk("small", vec![used.clone()]), Some(&cfg));
2870 assert_eq!(
2871 (before.percent, before.warn, before.since_switch),
2872 (Some(90), true, false)
2873 );
2874 let after = context_usage(&ctx_talk("big", vec![used]), Some(&cfg));
2877 assert_eq!(after.window, Some(10_000));
2878 assert_eq!(
2879 (after.percent, after.warn, after.since_switch),
2880 (Some(9), false, true)
2881 );
2882 assert_eq!(after.model.as_deref(), Some("big-model"));
2883 }
2884
2885 #[test]
2886 fn a_turn_recorded_before_usage_existed_still_reads() {
2887 let old = r#"{"who":"agent","body":"hi","at":"2026-09-04T01:44:55Z"}"#;
2888 let turn: Turn = serde_json::from_str(old).expect("old turn reads");
2889 assert!(turn.usage.is_none());
2890 let json = serde_json::to_string(&turn).expect("serialize");
2891 assert!(
2892 !json.contains("usage"),
2893 "absent usage is not written: {json}"
2894 );
2895 }
2896
2897 fn store() -> (tempfile::TempDir, Talks) {
2899 let tmp = tempfile::tempdir().expect("tempdir");
2900 let talks = Talks::at(tmp.path().join("talks"));
2901 (tmp, talks)
2902 }
2903
2904 fn mock_agent(dir: &Path, script: &str, env: BTreeMap<String, String>) -> AgentSpec {
2908 let path = dir.join("mock-talk-agent.sh");
2909 std::fs::write(&path, script).expect("write mock");
2910 AgentSpec {
2911 id: "mock".to_owned(),
2912 kind: AgentKind::Command,
2913 model: None,
2914 command: vec!["sh".to_owned(), path.to_string_lossy().into_owned()],
2915 extra_args: Vec::new(),
2916 env,
2917 prompt_delivery: None,
2918 }
2919 }
2920
2921 fn config(spec: AgentSpec) -> Config {
2922 Config {
2923 agents: vec![spec],
2924 graph: Graph {
2925 language: "en".to_owned(),
2926 ..Graph::default()
2927 },
2928 ..Config::default()
2929 }
2930 }
2931
2932 const REPLY: &str = "#!/bin/sh\ncat >/dev/null\nprintf '%s\\n' \"$MOCK_REPLY\"\n";
2934
2935 const BROKEN: &str = "#!/bin/sh\ncat >/dev/null\nexit 3\n";
2937
2938 const ECHO: &str = "#!/bin/sh\ncat\n";
2941
2942 fn env(reply: &str) -> BTreeMap<String, String> {
2943 BTreeMap::from([("MOCK_REPLY".to_owned(), reply.to_owned())])
2944 }
2945
2946 #[test]
2947 fn the_frozen_json_field_names_round_trip_through_disk() {
2948 let (tmp, talks) = store();
2949 let mut talk = Talk {
2950 posts_read_through: None,
2951 pending_breaks: None,
2952 schema: SCHEMA,
2953 id: "20260904-014455-ab12".to_owned(),
2954 repo: tmp.path().to_owned(),
2955 agent: "sonnet".to_owned(),
2956 status: TalkStatus::Open,
2957 turns: Vec::new(),
2958 pending: String::new(),
2959 pending_attachments: Vec::new(),
2960 fallback: false,
2961 persona: String::new(),
2962 persona_dirty: false,
2963 implementers: 1,
2964 implementers_dirty: false,
2965 notified: Vec::new(),
2966 created_at: Timestamp::now(),
2967 updated_at: Timestamp::now(),
2968 seat: SeatState::new(SEAT, "sonnet", 7),
2969 };
2970 talks.put(&mut talk).expect("put");
2971
2972 let raw = std::fs::read_to_string(talks.path_of(&talk.id)).expect("read back");
2973 let v: serde_json::Value = serde_json::from_str(&raw).expect("parse");
2974 for field in [
2975 "schema",
2976 "id",
2977 "repo",
2978 "agent",
2979 "status",
2980 "turns",
2981 "created_at",
2982 "updated_at",
2983 ] {
2984 assert!(v.get(field).is_some(), "missing field `{field}`");
2985 }
2986 assert_eq!(v["schema"], 1);
2987 assert_eq!(v["status"], "open");
2988
2989 let back = talks.get(&talk.id).expect("get");
2990 assert_eq!(back.posts_read_through, None);
2991 assert_eq!(back.id, talk.id);
2992 assert_eq!(back.status, TalkStatus::Open);
2993 }
2994
2995 #[test]
2996 fn opening_a_talk_takes_no_agent_turn() {
2997 let (tmp, talks) = store();
2998 let spec = mock_agent(tmp.path(), BROKEN, BTreeMap::new());
3002 let cfg = config(spec);
3003
3004 let talk = begin(&talks, &cfg, tmp.path().to_owned(), None).expect("begin");
3005 assert_eq!(talk.status, TalkStatus::Open);
3006 assert!(talk.turns.is_empty(), "nothing has been said yet");
3007
3008 let on_disk = talks.get(&talk.id).expect("get");
3009 assert_eq!(on_disk.turns.len(), 0);
3010 }
3011
3012 #[test]
3020 fn chatter_wins_when_set_and_falls_back_to_pick_s_default_order_otherwise() {
3021 let (tmp, talks) = store();
3022 let first_spec = mock_agent(tmp.path(), BROKEN, BTreeMap::new());
3023 let mut chatter_spec = mock_agent(tmp.path(), BROKEN, BTreeMap::new());
3024 chatter_spec.id = "chatter-mock".to_owned();
3025
3026 let mut cfg = Config {
3027 agents: vec![first_spec.clone(), chatter_spec.clone()],
3028 graph: Graph {
3029 language: "en".to_owned(),
3030 ..Graph::default()
3031 },
3032 ..Config::default()
3033 };
3034 cfg.roles.chatter = Some(chatter_spec.id.as_str().into());
3035
3036 let talk =
3037 begin(&talks, &cfg, tmp.path().to_owned(), None).expect("begin with chatter set");
3038 assert_eq!(talk.agent, chatter_spec.id, "an explicit chatter must win");
3039
3040 cfg.roles.chatter = None;
3041 let fallback =
3042 begin(&talks, &cfg, tmp.path().to_owned(), None).expect("begin with chatter unset");
3043 assert_eq!(
3044 fallback.agent, first_spec.id,
3045 "unset chatter must fall back to agent::pick's own default order"
3046 );
3047 }
3048
3049 #[test]
3052 fn a_talk_recorded_without_attachments_still_reads() {
3053 let (tmp, talks) = store();
3054 let path = talks.path_of("20260904-014455-ab12");
3055 std::fs::create_dir_all(talks.root()).expect("talks dir");
3056 std::fs::write(
3057 &path,
3058 serde_json::json!({
3059 "schema": 1,
3060 "id": "20260904-014455-ab12",
3061 "repo": tmp.path(),
3062 "agent": "sonnet",
3063 "status": "open",
3064 "turns": [
3065 { "who": "operator", "body": "still there?",
3066 "at": Timestamp::now().to_string() },
3067 ],
3068 "created_at": Timestamp::now().to_string(),
3069 "updated_at": Timestamp::now().to_string(),
3070 "seat": SeatState::new(SEAT, "sonnet", 7),
3071 })
3072 .to_string(),
3073 )
3074 .expect("write pre-attachments talk");
3075
3076 let talk = talks.get("20260904-014455-ab12").expect("must still read");
3077 assert!(talk.turns[0].attachments.is_empty());
3078 }
3079
3080 fn lease_store() -> (tempfile::TempDir, Talks, Talks) {
3081 let tmp = tempfile::TempDir::new().expect("tmp");
3082 let root = tmp.path().join("talks");
3083 (tmp, Talks::at(root.clone()), Talks::at(root))
3084 }
3085
3086 #[test]
3087 fn two_starters_on_one_talk_one_wins_and_the_other_is_refused() {
3088 let (_tmp, a, b) = lease_store();
3089 let won = a.claim_turn("t1").expect("claim").expect("first wins");
3090 assert!(
3091 b.claim_turn("t1").expect("claim").is_none(),
3092 "second is refused"
3093 );
3094 assert!(b.turn_held("t1"));
3095 assert!(
3096 b.claim_turn("t2").expect("claim").is_some(),
3097 "other talks are free"
3098 );
3099 drop(won);
3100 }
3101
3102 #[test]
3103 fn a_stale_lease_is_taken_over_and_the_old_guard_cannot_release_it() {
3104 let (_tmp, a, b) = lease_store();
3105 let old = a.claim_turn("t1").expect("claim").expect("held");
3106 let later = Timestamp::now()
3107 .checked_add(jiff::SignedDuration::from_secs(
3108 crate::ask::LEASE_TTL.as_secs() as i64 + 5,
3109 ))
3110 .expect("later");
3111 let new = b
3112 .claim_turn_at("t1", later)
3113 .expect("claim")
3114 .expect("a stale lease is taken over");
3115 drop(old);
3116 assert!(a.turn_held("t1"), "the old guard left the new lease alone");
3117 assert!(new.beat().expect("beat"), "the new owner still beats");
3118 drop(new);
3119 assert!(!a.turn_held("t1"));
3120 }
3121
3122 #[tokio::test]
3123 async fn a_turn_whose_lease_was_taken_over_is_stopped() {
3124 let (_tmp, a, b) = lease_store();
3125 let old = a.claim_turn("t1").expect("claim").expect("held");
3126 let later = Timestamp::now()
3127 .checked_add(jiff::SignedDuration::from_secs(
3128 crate::ask::LEASE_TTL.as_secs() as i64 + 5,
3129 ))
3130 .expect("later");
3131 let _new = b
3132 .claim_turn_at("t1", later)
3133 .expect("claim")
3134 .expect("taken over");
3135 let out = old
3136 .beating_every(Duration::from_millis(10), std::future::pending::<()>())
3137 .await;
3138 assert!(out.is_err(), "the displaced turn must stop, not run on");
3139 }
3140
3141 #[tokio::test]
3142 async fn a_turn_that_finishes_is_returned_and_keeps_its_lease_beating() {
3143 let (_tmp, a, _b) = lease_store();
3144 let lease = a.claim_turn("t1").expect("claim").expect("held");
3145 let out = lease
3146 .beating_every(Duration::from_millis(5), async {
3147 tokio::time::sleep(Duration::from_millis(40)).await;
3148 7
3149 })
3150 .await
3151 .expect("still ours");
3152 assert_eq!(out, 7);
3153 assert!(a.turn_held("t1"));
3154 }
3155
3156 #[test]
3157 fn an_unreadable_lease_counts_as_stale() {
3158 let (_tmp, a, b) = lease_store();
3159 std::fs::create_dir_all(a.root()).expect("dir");
3160 std::fs::write(a.turn_path("t1"), "not json").expect("write");
3161 assert!(!a.turn_held("t1"));
3162 assert!(b.claim_turn("t1").expect("claim").is_some());
3163 }
3164
3165 #[test]
3166 fn without_hard_links_a_claim_is_still_exclusive_and_a_young_placeholder_blocks() {
3167 let (_tmp, a, b) = lease_store();
3168 failpoint::force_no_link(true);
3169 let held = a.claim_turn("t1").expect("claim").expect("first wins");
3170 assert!(b.claim_turn("t1").expect("claim").is_none());
3171 assert!(a.turn_held("t1"));
3172 drop(held);
3173 assert!(!a.turn_held("t1"));
3174 let path = a.turn_path("t1");
3176 assert!(create_exclusive(&path, "").expect("placeholder"));
3177 assert!(b.claim_turn("t1").expect("claim").is_none(), "young: held");
3178 age_file(&path);
3179 assert!(b.claim_turn("t1").expect("claim").is_some(), "old: stale");
3180 let lease = a.turn_path("t2");
3182 let lock = TurnLock::take(&lease).expect("take").expect("lock");
3183 assert!(TurnLock::take(&lease).expect("take").is_none());
3184 drop(lock);
3185 assert!(TurnLock::take(&lease).expect("take").is_some());
3186 failpoint::force_no_link(false);
3187 }
3188
3189 #[test]
3190 fn remove_if_carries_removes_only_the_expected_content() {
3191 let (_tmp, a, _b) = lease_store();
3192 std::fs::create_dir_all(a.root()).expect("dir");
3193 let p = a.root().join("x.turn.lock");
3194 std::fs::write(&p, "mine").expect("write");
3195 assert!(!remove_if_carries(&p, "other"));
3196 assert_eq!(std::fs::read_to_string(&p).expect("kept"), "mine");
3197 assert!(remove_if_carries(&p, "mine"));
3198 assert!(!p.exists());
3199 assert!(!remove_if_carries(&p, "mine"), "absent is not a removal");
3200 }
3201
3202 #[test]
3203 fn a_dropped_lock_does_not_remove_a_lock_taken_over_since() {
3204 let (_tmp, a, _b) = lease_store();
3205 std::fs::create_dir_all(a.root()).expect("dir");
3206 let lease = a.turn_path("t1");
3207 let lock = TurnLock::take(&lease).expect("take").expect("lock");
3208 let path = lock.path.clone();
3209 std::fs::write(&path, "someone-else").expect("replace");
3210 drop(lock);
3211 assert_eq!(
3212 std::fs::read_to_string(&path).expect("kept"),
3213 "someone-else"
3214 );
3215 }
3216
3217 #[test]
3218 fn concurrent_takeovers_of_a_stale_lease_have_one_winner() {
3219 let (_tmp, a, _b) = lease_store();
3220 drop(a.claim_turn("t1").expect("claim").expect("held"));
3221 std::fs::write(
3222 a.turn_path("t1"),
3223 serde_json::to_string(&TurnRecord {
3224 token: "gone".into(),
3225 pid: 1,
3226 beat_at: Timestamp::from_second(1).expect("ts"),
3227 })
3228 .expect("json"),
3229 )
3230 .expect("write");
3231 let wins: Vec<_> = std::thread::scope(|sc| {
3232 let hs: Vec<_> = (0..8)
3233 .map(|_| {
3234 let s = a.clone();
3235 sc.spawn(move || s.claim_turn("t1").expect("claim"))
3236 })
3237 .collect();
3238 hs.into_iter().map(|h| h.join().expect("join")).collect()
3239 });
3240 assert_eq!(wins.iter().filter(|w| w.is_some()).count(), 1);
3241 }
3242
3243 fn age_file(path: &Path) {
3244 let f = std::fs::OpenOptions::new()
3245 .write(true)
3246 .open(path)
3247 .expect("open");
3248 f.set_modified(std::time::SystemTime::now() - std::time::Duration::from_secs(60))
3249 .expect("age");
3250 }
3251
3252 #[test]
3253 fn a_late_taker_of_a_broken_lock_cannot_disturb_its_replacement() {
3254 let (_tmp, a, _b) = lease_store();
3255 let lease = a.turn_path("t1");
3256 let lock = lease.with_extension("turn.lock");
3257 std::fs::create_dir_all(lock.parent().expect("dir")).expect("dir");
3258 std::fs::write(&lock, "t1-dead").expect("dead lock");
3259 age_file(&lock);
3260 let b = TurnLock::take(&lease).expect("take").expect("b wins");
3262 let fresh = std::fs::read_to_string(&lock).expect("read");
3263 assert_eq!(fresh, b.token);
3264 let ticket = std::fs::read_dir(lock.parent().expect("dir"))
3267 .expect("dir")
3268 .flatten()
3269 .map(|e| e.path())
3270 .find(|p| p.to_string_lossy().ends_with(".break.0"))
3271 .expect("ticket");
3272 assert!(!create_exclusive(&ticket, "").expect("ticket"));
3273 assert!(TurnLock::take(&lease).expect("take").is_none());
3274 assert_eq!(std::fs::read_to_string(&lock).expect("read"), fresh);
3275 age_file(&lock);
3277 age_file(&ticket);
3278 std::mem::forget(b);
3279 let c = TurnLock::take(&lease)
3280 .expect("take")
3281 .expect("next generation");
3282 assert_ne!(c.token, fresh);
3283 }
3284
3285 #[test]
3286 fn a_live_ticket_blocks_and_a_stale_one_hands_over_to_the_next_generation() {
3287 let (_tmp, a, _b) = lease_store();
3288 let lease = a.turn_path("t1");
3289 let lock = lease.with_extension("turn.lock");
3290 std::fs::create_dir_all(lock.parent().expect("dir")).expect("dir");
3291 std::fs::write(&lock, "t1-dead").expect("dead lock");
3292 age_file(&lock);
3293 let t0 = lock.with_extension("lock.t1-dead.break.0");
3294 assert!(create_exclusive(&t0, "").expect("ticket"));
3295 assert!(TurnLock::take(&lease).expect("take").is_none());
3297 assert_eq!(std::fs::read_to_string(&lock).expect("read"), "t1-dead");
3298 age_file(&t0);
3300 let c = TurnLock::take(&lease).expect("take").expect("generation 1");
3301 assert_eq!(std::fs::read_to_string(&lock).expect("read"), c.token);
3302 assert!(lock.with_extension("lock.t1-dead.break.1").exists());
3303 assert!(TurnLock::take(&lease).expect("take").is_none());
3304 }
3305
3306 #[test]
3307 fn exhausted_ticket_generations_recover_once_the_sweep_ages_them_out() {
3308 let (_tmp, a, _b) = lease_store();
3309 let lease = a.turn_path("t1");
3310 let lock = lease.with_extension("turn.lock");
3311 std::fs::create_dir_all(lock.parent().expect("dir")).expect("dir");
3312 std::fs::write(&lock, "t1-dead").expect("dead lock");
3313 age_file(&lock);
3314 let tickets: Vec<_> = (0..TICKET_GENERATIONS)
3315 .map(|n| lock.with_extension(format!("lock.t1-dead.break.{n}")))
3316 .collect();
3317 for t in &tickets {
3318 assert!(create_exclusive(t, "").expect("ticket"));
3319 age_file(t);
3320 }
3321 assert!(TurnLock::take(&lease).expect("take").is_none());
3323 assert!(tickets.iter().all(|t| t.exists()));
3324 for t in &tickets {
3326 let f = std::fs::OpenOptions::new()
3327 .write(true)
3328 .open(t)
3329 .expect("open");
3330 f.set_modified(std::time::SystemTime::now() - TICKET_SWEEP_AGE * 2)
3331 .expect("age");
3332 }
3333 assert!(TurnLock::take(&lease).expect("take").is_none());
3334 assert!(TurnLock::take(&lease).expect("take").is_some());
3335 }
3336
3337 #[test]
3338 fn an_aged_empty_lock_is_broken() {
3339 let (_tmp, a, _b) = lease_store();
3340 let lease = a.turn_path("t1");
3341 let lock = lease.with_extension("turn.lock");
3342 std::fs::create_dir_all(lock.parent().expect("dir")).expect("dir");
3343 std::fs::write(&lock, "").expect("empty lock");
3344 age_file(&lock);
3345 assert!(TurnLock::take(&lease).expect("take").is_some());
3346 }
3347
3348 #[test]
3349 fn queued_text_is_durable_combined_and_drained_as_one_operator_turn() {
3350 let (tmp, talks) = store();
3351 let cfg = config(mock_agent(tmp.path(), REPLY, env("reply")));
3352 let mut talk = begin(&talks, &cfg, tmp.path().to_owned(), None).expect("begin");
3353
3354 queue(&mut talk, &talks, "first", Vec::new()).expect("queue first");
3355 queue(&mut talk, &talks, "second", Vec::new()).expect("queue second");
3356 let saved = talks.get(&talk.id).expect("reload queued talk");
3357 assert_eq!(saved.pending, "first\n\nsecond");
3358 assert_eq!(saved.pending_breaks, Some(vec!["first\n\n".len()]));
3359 assert!(saved.turns.is_empty(), "a draft is not a transcript turn");
3360
3361 let drained = drain(&mut talk, &talks).expect("drain");
3362 assert_eq!(drained.as_deref(), Some("first\n\nsecond"));
3363 let saved = talks.get(&talk.id).expect("reload drained talk");
3364 assert!(saved.pending.is_empty());
3365 assert_eq!(saved.turns.len(), 1);
3366 assert_eq!(saved.turns[0].body, "first\n\nsecond");
3367 assert_eq!(saved.turns[0].breaks, Some(vec!["first\n\n".len()]));
3368 assert_eq!(saved.pending_breaks, None);
3369 }
3370
3371 #[test]
3372 fn editing_a_queued_draft_preserves_its_attachments_and_rejects_a_stale_snapshot() {
3373 let (tmp, talks) = store();
3374 let cfg = config(mock_agent(tmp.path(), REPLY, env("reply")));
3375 let mut talk = begin(&talks, &cfg, tmp.path().to_owned(), None).expect("begin");
3376 let attachment = Attachment {
3377 id: "a".repeat(32),
3378 name: "shot.png".to_owned(),
3379 mime: "image/png".to_owned(),
3380 bytes: 3,
3381 };
3382
3383 queue(&mut talk, &talks, "first", vec![attachment.clone()]).expect("queue");
3384 assert!(
3385 edit_pending_text(
3386 &mut talk,
3387 &talks,
3388 "corrected",
3389 "first",
3390 std::slice::from_ref(&attachment.id),
3391 )
3392 .expect("edit")
3393 );
3394 let saved = talks.get(&talk.id).expect("reload edited draft");
3395 assert_eq!(saved.pending, "corrected");
3396 assert_eq!(saved.pending_attachments, vec![attachment]);
3397
3398 queue(&mut talk, &talks, "later", Vec::new()).expect("queue concurrent draft");
3399 assert!(
3400 !edit_pending_text(
3401 &mut talk,
3402 &talks,
3403 "stale edit",
3404 "corrected",
3405 &["a".repeat(32)],
3406 )
3407 .expect("stale edit is a conflict")
3408 );
3409 assert_eq!(
3410 talks.get(&talk.id).expect("reload after conflict").pending,
3411 "corrected\n\nlater"
3412 );
3413 assert!(
3414 !clear_pending_if_matches(&mut talk, &talks, "corrected", &["a".repeat(32)])
3415 .expect("stale clear is a conflict")
3416 );
3417 assert_eq!(
3418 talks
3419 .get(&talk.id)
3420 .expect("reload after stale clear")
3421 .pending,
3422 "corrected\n\nlater"
3423 );
3424 }
3425
3426 #[tokio::test]
3427 async fn a_reply_save_preserves_pending_accepted_while_the_cli_runs() {
3428 let (tmp, talks) = store();
3429 let slow = "#!/bin/sh\ncat >/dev/null\nsleep 0.1\nprintf reply\n";
3430 let cfg = config(mock_agent(tmp.path(), slow, BTreeMap::new()));
3431 let mut running = begin(&talks, &cfg, tmp.path().to_owned(), None).expect("begin");
3432 let id = running.id.clone();
3433 let first = record(&mut running, &talks, "first", Vec::new()).expect("record");
3434
3435 let response_talks = talks.clone();
3436 let response_cfg = cfg.clone();
3437 let reply = tokio::spawn(async move {
3438 respond(
3439 &response_talks.claim_turn(&running.id).unwrap().unwrap(),
3440 &mut running,
3441 &response_talks,
3442 &response_cfg,
3443 &first,
3444 )
3445 .await
3446 });
3447 tokio::time::sleep(std::time::Duration::from_millis(20)).await;
3448
3449 let mut queued = talks.get(&id).expect("queued handle");
3450 queue(&mut queued, &talks, "next", Vec::new()).expect("queue");
3451 reply.await.expect("join").expect("reply");
3452
3453 let saved = talks.get(&id).expect("reload");
3454 assert_eq!(saved.pending, "next");
3455 assert_eq!(saved.pending_breaks, Some(Vec::new()));
3456 assert_eq!(saved.turns.len(), 2, "operator message and reply remain");
3457 }
3458
3459 #[tokio::test]
3460 async fn posts_during_a_resumed_turn_are_replayed_once_even_after_a_reply_save_failure() {
3461 for lose_reply in [false, true] {
3462 let (tmp, talks) = store();
3463 let cfg = config(mock_agent(tmp.path(), ECHO, BTreeMap::new()));
3464 let mut talk = begin(&talks, &cfg, tmp.path().to_owned(), None).expect("begin");
3465 say(
3466 &talks.claim_turn(&talk.id).unwrap().unwrap(),
3467 &mut talk,
3468 &talks,
3469 &cfg,
3470 "establish the session",
3471 Vec::new(),
3472 )
3473 .await
3474 .expect("first turn");
3475 assert!(agent::has_session(
3476 AgentKind::Command,
3477 &talk.seat,
3478 cfg.graph.sessions
3479 ));
3480
3481 post(&talks, &talk.id, "already available", "task-earlier").expect("earlier post");
3482 talk = talks.get(&talk.id).expect("reload earlier post");
3483 let ready = tmp.path().join("ready");
3484 let release = tmp.path().join("release");
3485 let slow = "#!/bin/sh\ncat >/dev/null\ntouch \"$READY\"\nn=0\nwhile [ ! -f \"$RELEASE\" ] && [ \"$n\" -lt 200 ]; do sleep 0.01; n=$((n + 1)); done\nprintf reply\n";
3486 let slow_cfg = config(mock_agent(
3487 tmp.path(),
3488 slow,
3489 BTreeMap::from([
3490 ("READY".to_owned(), ready.to_string_lossy().into_owned()),
3491 ("RELEASE".to_owned(), release.to_string_lossy().into_owned()),
3492 ]),
3493 ));
3494 let text = record(&mut talk, &talks, "keep going", Vec::new()).expect("record");
3495 let prompt_turns = talk.turns.len();
3496 let id = talk.id.clone();
3497 let response_talks = talks.clone();
3498 let running = tokio::spawn(async move {
3499 respond(
3500 &response_talks.claim_turn(&talk.id).unwrap().unwrap(),
3501 &mut talk,
3502 &response_talks,
3503 &slow_cfg,
3504 &text,
3505 )
3506 .await
3507 });
3508 tokio::time::timeout(std::time::Duration::from_secs(5), async {
3509 while !ready.exists() {
3510 tokio::time::sleep(std::time::Duration::from_millis(10)).await;
3511 }
3512 })
3513 .await
3514 .expect("CLI read its prompt");
3515 post(&talks, &id, "concurrent task result", "task-one").expect("post");
3516 post(&talks, &id, "second task result", "task-two").expect("second post");
3517 if lose_reply {
3518 failpoint::force_put_failures(PUT_RETRIES);
3519 }
3520 std::fs::write(release, "go").expect("release CLI");
3521 let result = running.await.expect("join");
3522 if lose_reply {
3523 assert!(
3524 result
3525 .unwrap_err()
3526 .to_string()
3527 .contains("could not be saved")
3528 );
3529 } else {
3530 result.expect("reply");
3531 }
3532
3533 let mut saved = talks.get(&id).expect("reload");
3534 assert_eq!(saved.posts_read_through, Some(prompt_turns));
3535 assert_eq!(saved.turns[prompt_turns].body, "concurrent task result");
3536 assert_eq!(saved.turns[prompt_turns + 1].body, "second task result");
3537 assert!(posts_since_last_reply(&saved).is_some());
3538 let cfg = config(mock_agent(tmp.path(), ECHO, BTreeMap::new()));
3539 say(
3540 &talks.claim_turn(&id).unwrap().unwrap(),
3541 &mut saved,
3542 &talks,
3543 &cfg,
3544 "read the results",
3545 Vec::new(),
3546 )
3547 .await
3548 .expect("replay turn");
3549 let prompt = &saved.turns.last().unwrap().body;
3550 assert!(prompt.contains("# Posted by a task"), "{prompt}");
3551 assert!(!prompt.contains("already available"), "{prompt}");
3552 assert!(
3553 prompt
3554 .find("concurrent task result")
3555 .expect("first post in prompt")
3556 < prompt
3557 .find("second task result")
3558 .expect("second post in prompt")
3559 );
3560 let mut saved = talks.get(&id).expect("reload after replay");
3561 assert!(posts_since_last_reply(&saved).is_none());
3562 say(
3563 &talks.claim_turn(&id).unwrap().unwrap(),
3564 &mut saved,
3565 &talks,
3566 &cfg,
3567 "continue",
3568 Vec::new(),
3569 )
3570 .await
3571 .expect("next turn");
3572 assert!(!saved.turns.last().unwrap().body.contains("task result"));
3573 }
3574 }
3575
3576 fn counting_agent(dir: &Path, id: &str, body: &str) -> AgentSpec {
3579 let calls = dir.join(format!("{id}.calls"));
3580 let script = format!(
3581 "#!/bin/sh\necho x >> '{}'\n{body}\n",
3582 calls.to_string_lossy()
3583 );
3584 let path = dir.join(format!("mock-{id}.sh"));
3585 std::fs::write(&path, script).expect("write mock");
3586 AgentSpec {
3587 id: id.to_owned(),
3588 kind: AgentKind::Command,
3589 model: None,
3590 command: vec!["sh".to_owned(), path.to_string_lossy().into_owned()],
3591 extra_args: Vec::new(),
3592 env: BTreeMap::new(),
3593 prompt_delivery: None,
3594 }
3595 }
3596
3597 fn calls(dir: &Path, id: &str) -> usize {
3598 std::fs::read_to_string(dir.join(format!("{id}.calls"))).map_or(0, |s| s.lines().count())
3599 }
3600
3601 fn chain_config(specs: Vec<AgentSpec>, ids: &[&str]) -> Config {
3602 let mut cfg = config(specs[0].clone());
3603 cfg.agents = specs;
3604 cfg.roles.chatter = Some(AgentChoice::Chain(
3605 ids.iter().map(|s| (*s).to_owned()).collect(),
3606 ));
3607 cfg
3608 }
3609
3610 #[tokio::test]
3611 async fn a_chatter_chain_falls_back_resends_the_transcript_and_sticks() {
3612 let (tmp, talks) = store();
3613 let a = counting_agent(tmp.path(), "a", "cat >/dev/null\nexit 3");
3614 let b = counting_agent(tmp.path(), "b", "cat");
3615 let cfg = chain_config(vec![a, b], &["a", "b"]);
3616 let mut talk = begin(&talks, &cfg, tmp.path().to_owned(), None).expect("begin");
3617 assert_eq!(talk.agent, "a");
3618 post(&talks, &talk.id, "fallback task result", "task").expect("post");
3619 talk = talks.get(&talk.id).expect("reload");
3620
3621 say(
3622 &talks.claim_turn(&talk.id).unwrap().unwrap(),
3623 &mut talk,
3624 &talks,
3625 &cfg,
3626 "hello there",
3627 Vec::new(),
3628 )
3629 .await
3630 .expect("turn");
3631 assert_eq!(calls(tmp.path(), "a"), 1, "each id is tried once");
3632 assert_eq!(calls(tmp.path(), "b"), 1);
3633 assert_eq!(talk.agent, "b", "the switch persists");
3634 assert!(talks.get(&talk.id).unwrap().agent == "b");
3635 let reply = talk.turns.last().unwrap();
3636 assert!(reply.body.contains("hello there"));
3637 assert!(reply.body.contains("fallback task result"));
3638 let saved = talks.get(&talk.id).expect("reload fallback");
3639 assert_eq!(saved.posts_read_through, Some(2));
3640 assert!(posts_since_last_reply(&saved).is_none());
3641 assert!(
3642 reply.body.contains("magi task add --solo"),
3643 "a fresh seat gets the full briefing"
3644 );
3645 assert!(
3646 talk.turns
3647 .iter()
3648 .any(|t| t.body.contains("agent changed from a to b")),
3649 "the switch is noted"
3650 );
3651 }
3652
3653 #[tokio::test]
3654 async fn an_exhausted_chatter_chain_fails_like_a_single_seat_and_stays_put() {
3655 let (tmp, talks) = store();
3656 let a = counting_agent(tmp.path(), "a", "cat >/dev/null\nexit 3");
3657 let b = counting_agent(tmp.path(), "b", "cat >/dev/null\nexit 4");
3658 let cfg = chain_config(vec![a, b], &["a", "b", "a"]);
3659 let mut talk = begin(&talks, &cfg, tmp.path().to_owned(), None).expect("begin");
3660
3661 let err = say(
3662 &talks.claim_turn(&talk.id).unwrap().unwrap(),
3663 &mut talk,
3664 &talks,
3665 &cfg,
3666 "hi",
3667 Vec::new(),
3668 )
3669 .await
3670 .expect_err("every agent failed");
3671 assert!(err.to_string().contains("`a`"), "{err:#}");
3672 assert_eq!(calls(tmp.path(), "a"), 1);
3673 assert_eq!(calls(tmp.path(), "b"), 1);
3674 assert_eq!(talk.agent, "a", "an exhausted chain leaves the agent alone");
3675 }
3676
3677 #[test]
3678 fn a_chatter_chain_skips_an_unknown_id_at_begin() {
3679 let (tmp, talks) = store();
3680 let b = counting_agent(tmp.path(), "b", "cat");
3681 let cfg = chain_config(vec![b], &["ghost", "b"]);
3682 let talk = begin(&talks, &cfg, tmp.path().to_owned(), None).expect("begin");
3683 assert_eq!(talk.agent, "b");
3684 }
3685
3686 #[tokio::test]
3687 async fn an_explicit_agent_inside_the_chatter_chain_stays_pinned() {
3688 let (tmp, talks) = store();
3689 let a = counting_agent(tmp.path(), "a", "cat >/dev/null\nexit 3");
3690 let b = counting_agent(tmp.path(), "b", "cat");
3691 let cfg = chain_config(vec![a, b], &["a", "b"]);
3692 let mut talk = begin(&talks, &cfg, tmp.path().to_owned(), Some("a")).expect("begin");
3693 say(
3694 &talks.claim_turn(&talk.id).unwrap().unwrap(),
3695 &mut talk,
3696 &talks,
3697 &cfg,
3698 "hi",
3699 Vec::new(),
3700 )
3701 .await
3702 .expect_err("a alone, and it fails");
3703 assert_eq!(calls(tmp.path(), "b"), 0);
3704 assert_eq!(talk.agent, "a");
3705 }
3706
3707 #[tokio::test]
3708 async fn an_explicit_agent_does_not_borrow_the_chatter_chain() {
3709 let (tmp, talks) = store();
3710 let a = counting_agent(tmp.path(), "a", "cat >/dev/null\nexit 3");
3711 let b = counting_agent(tmp.path(), "b", "cat");
3712 let c = counting_agent(tmp.path(), "c", "cat >/dev/null\nexit 3");
3713 let cfg = chain_config(vec![a, b, c], &["a", "b"]);
3714 let mut talk = begin(&talks, &cfg, tmp.path().to_owned(), Some("c")).expect("begin");
3715 say(
3716 &talks.claim_turn(&talk.id).unwrap().unwrap(),
3717 &mut talk,
3718 &talks,
3719 &cfg,
3720 "hi",
3721 Vec::new(),
3722 )
3723 .await
3724 .expect_err("c alone, and it fails");
3725 assert_eq!(calls(tmp.path(), "b"), 0);
3726 }
3727
3728 #[test]
3729 fn briefing_names_the_operator_with_or_without_a_persona() {
3730 let plain = briefing(Path::new("/repo"), "en", false);
3731 let named = briefing_with(Path::new("/repo"), "en", false, None, Some("Commander"));
3732 assert!(named.starts_with(&plain));
3733 assert!(named.contains("# Addressing the operator"));
3734 assert!(named.contains("\"Commander\""));
3735 let rei = crate::persona::builtin_catalog()
3736 .into_iter()
3737 .find(|p| p.id == "rei")
3738 .expect("rei");
3739 let with = briefing_with(
3740 Path::new("/repo"),
3741 "en",
3742 false,
3743 Some(&rei),
3744 Some("Commander"),
3745 );
3746 assert!(with.contains("\"Commander\""));
3747 assert!(!with.contains("# Addressing the operator"));
3748 }
3749
3750 #[test]
3751 fn briefing_carries_a_persona_section_only_when_one_is_chosen() {
3752 let plain = briefing(Path::new("/repo"), "en", false);
3753 assert_eq!(
3754 plain,
3755 briefing_with(Path::new("/repo"), "en", false, None, None)
3756 );
3757 assert!(!plain.contains("Persona"));
3758 let rei = crate::persona::builtin_catalog()
3759 .into_iter()
3760 .find(|p| p.id == "rei")
3761 .expect("rei");
3762 let with = briefing_with(Path::new("/repo"), "en", false, Some(&rei), None);
3763 assert!(with.starts_with(&plain), "the plain briefing is untouched");
3764 assert!(with.contains("# Persona (tone only)"));
3765 assert!(with.contains("TONE ONLY"));
3766 assert!(with.contains("task ids"));
3767 assert!(with.contains("write policy"));
3768 assert!(with.contains("`magi task add`"));
3769 assert!(with.contains("Rei Ayanami"));
3770 }
3771
3772 #[test]
3773 fn a_talk_written_before_personas_still_loads() {
3774 let (tmp, talks) = store();
3775 let spec = mock_agent(tmp.path(), ECHO, BTreeMap::new());
3776 let cfg = config(spec);
3777 let talk = begin(&talks, &cfg, tmp.path().to_owned(), None).expect("begin");
3778 let path = talks.root.join(format!("{}.json", talk.id));
3779 let mut v: serde_json::Value =
3780 serde_json::from_str(&std::fs::read_to_string(&path).unwrap()).unwrap();
3781 v.as_object_mut().unwrap().remove("persona");
3782 v.as_object_mut().unwrap().remove("persona_dirty");
3783 std::fs::write(&path, v.to_string()).unwrap();
3784 let loaded = talks.get(&talk.id).expect("old record loads");
3785 assert_eq!(loaded.persona, "");
3786 assert!(!loaded.persona_dirty);
3787 }
3788
3789 #[test]
3790 fn the_briefing_files_with_solo_or_the_chosen_implementer_count() {
3791 let repo = Path::new("/repo");
3792 let solo = briefing_for(repo, "", false, None, None, 1);
3793 assert_eq!(solo, briefing(repo, "", false));
3794 assert!(solo.contains("magi task add --solo --repo"));
3795 assert!(solo.contains("magi task add --solo --attach"));
3796 for n in [2u8, 3] {
3797 let b = briefing_for(repo, "", false, None, None, n);
3798 assert!(
3799 b.contains(&format!("magi task add --implementers {n} --repo")),
3800 "{b}"
3801 );
3802 assert!(b.contains(&format!("magi task add --implementers {n} --attach")));
3803 assert!(!b.contains("task add --solo"), "{b}");
3804 assert!(!b.contains("Use --solo"), "{b}");
3805 }
3806 }
3807
3808 #[test]
3809 fn begin_with_applies_remembered_choices_and_drops_unfit_ones_alone() {
3810 let (tmp, talks) = store();
3811 let first = mock_agent(tmp.path(), ECHO, BTreeMap::new());
3812 let mut second = first.clone();
3813 second.id = "second".to_owned();
3814 let mut cfg = config(first.clone());
3815 cfg.agents.push(second.clone());
3816 let open = |p: &Preferred| {
3817 begin_with(&talks, &cfg, tmp.path().to_owned(), None, p).expect("begin")
3818 };
3819
3820 let talk = open(&Preferred {
3821 agent: Some("second".into()),
3822 persona: Some("rei".into()),
3823 implementers: Some(i64::from(check_implementers(3, &cfg).unwrap_or(1))),
3824 });
3825 assert_eq!(talk.agent, "second");
3826 assert!(
3827 !talk.fallback,
3828 "a remembered non-head agent is an explicit choice"
3829 );
3830 assert_eq!(talk.persona, "rei");
3831 assert_eq!(talk.implementers, check_implementers(3, &cfg).unwrap_or(1));
3832 assert!(!talk.persona_dirty && !talk.implementers_dirty);
3833
3834 let head = open(&Preferred {
3836 agent: Some(first.id.clone()),
3837 ..Preferred::default()
3838 });
3839 assert!(head.fallback);
3840
3841 for implementers in [0, 4, -1, 1000] {
3843 let t = open(&Preferred {
3844 agent: Some("gone".into()),
3845 persona: Some("no-such-persona".into()),
3846 implementers: Some(implementers),
3847 });
3848 assert_eq!(t.agent, first.id);
3849 assert!(t.fallback);
3850 assert_eq!(t.persona, "");
3851 assert_eq!(t.implementers, 1);
3852 }
3853
3854 let t = open(&Preferred {
3856 agent: Some("second".into()),
3857 persona: Some("default".into()),
3858 implementers: Some(9),
3859 });
3860 assert_eq!(
3861 (t.agent.as_str(), t.persona.as_str(), t.implementers),
3862 ("second", "", 1)
3863 );
3864
3865 let strict = begin_with(
3867 &talks,
3868 &cfg,
3869 tmp.path().to_owned(),
3870 Some("first-unknown"),
3871 &Preferred {
3872 agent: Some("second".into()),
3873 ..Preferred::default()
3874 },
3875 );
3876 assert!(strict.is_err());
3877 }
3878
3879 #[test]
3880 fn remembered_agent_can_open_when_the_chatter_chain_cannot_resolve() {
3881 let (tmp, talks) = store();
3882 let spec = mock_agent(tmp.path(), ECHO, BTreeMap::new());
3883 let cfg = chain_config(vec![spec.clone()], &["missing-chatter"]);
3886 assert!(begin(&talks, &cfg, tmp.path().to_owned(), None).is_err());
3887 let talk = begin_with(
3888 &talks,
3889 &cfg,
3890 tmp.path().to_owned(),
3891 None,
3892 &Preferred {
3893 agent: Some(spec.id.clone()),
3894 ..Preferred::default()
3895 },
3896 )
3897 .expect("a valid remembered agent does not require a default chain");
3898 assert_eq!(talk.agent, spec.id);
3899 assert!(!talk.fallback);
3900 assert_eq!(talks.get(&talk.id).unwrap().agent, spec.id);
3901 assert!(
3902 begin_with(
3903 &talks,
3904 &cfg,
3905 tmp.path().to_owned(),
3906 None,
3907 &Preferred {
3908 agent: Some("missing-preference".into()),
3909 ..Preferred::default()
3910 },
3911 )
3912 .is_err()
3913 );
3914 }
3915
3916 #[test]
3917 fn implementers_are_validated_and_old_records_load_as_solo() {
3918 let (tmp, talks) = store();
3919 let cfg = config(mock_agent(tmp.path(), ECHO, BTreeMap::new()));
3920 assert_eq!(check_implementers(1, &cfg), Ok(1));
3921 assert_eq!(check_implementers(3, &cfg), Ok(3));
3922 assert!(check_implementers(0, &cfg).is_err());
3923 assert!(check_implementers(4, &cfg).is_err());
3924 let mut empty = cfg.clone();
3925 empty.agents.clear();
3926 assert!(check_implementers(2, &empty).is_err());
3927
3928 let talk = begin(&talks, &cfg, tmp.path().to_owned(), None).expect("begin");
3929 let path = talks.path_of(&talk.id);
3930 let mut v: serde_json::Value =
3931 serde_json::from_str(&std::fs::read_to_string(&path).unwrap()).unwrap();
3932 v.as_object_mut().unwrap().remove("implementers");
3933 v.as_object_mut().unwrap().remove("implementers_dirty");
3934 std::fs::write(&path, v.to_string()).unwrap();
3935 let loaded = talks.get(&talk.id).expect("load");
3936 assert_eq!(loaded.implementers, 1);
3937 assert!(!loaded.implementers_dirty);
3938 }
3939
3940 #[tokio::test]
3941 async fn an_implementers_switch_is_noted_once_and_kept_across_a_failed_turn() {
3942 let (tmp, talks) = store();
3943 let spec = mock_agent(tmp.path(), ECHO, BTreeMap::new());
3944 let cfg = config(spec);
3945 let mut talk = begin(&talks, &cfg, tmp.path().to_owned(), None).expect("begin");
3946 let lease = talks.claim_turn(&talk.id).unwrap().unwrap();
3947 say(&lease, &mut talk, &talks, &cfg, "hello", Vec::new())
3948 .await
3949 .expect("first turn");
3950
3951 assert!(switch_implementers(&mut talk, &talks, 2).expect("switch"));
3952 assert!(!switch_implementers(&mut talk, &talks, 2).expect("same"));
3953 assert!(talk.implementers_dirty);
3954 say(&lease, &mut talk, &talks, &cfg, "next", Vec::new())
3955 .await
3956 .expect("turn");
3957 let prompt = &talk.turns.last().unwrap().body;
3958 assert!(prompt.contains("# Task filing update"), "{prompt}");
3959 assert!(prompt.contains("--implementers 2"));
3960 assert!(!talk.implementers_dirty, "cleared after a successful turn");
3961
3962 say(&lease, &mut talk, &talks, &cfg, "again", Vec::new())
3963 .await
3964 .expect("turn");
3965 assert!(
3966 !talk
3967 .turns
3968 .last()
3969 .unwrap()
3970 .body
3971 .contains("# Task filing update")
3972 );
3973
3974 let mut no_sessions = cfg.clone();
3976 no_sessions.graph.sessions = false;
3977 say(&lease, &mut talk, &talks, &no_sessions, "one", Vec::new())
3978 .await
3979 .expect("turn");
3980 say(&lease, &mut talk, &talks, &no_sessions, "two", Vec::new())
3981 .await
3982 .expect("turn");
3983 assert!(talk.turns.last().unwrap().body.contains("--implementers 2"));
3984 }
3985
3986 #[tokio::test]
3987 async fn a_persona_switch_notes_marks_dirty_and_updates_the_next_turn_once() {
3988 let (tmp, talks) = store();
3989 let spec = mock_agent(tmp.path(), ECHO, BTreeMap::new());
3990 let cfg = config(spec);
3991 let mut talk = begin(&talks, &cfg, tmp.path().to_owned(), None).expect("begin");
3992 let lease = talks.claim_turn(&talk.id).unwrap().unwrap();
3993 say(&lease, &mut talk, &talks, &cfg, "hello", Vec::new())
3994 .await
3995 .expect("first turn");
3996 assert!(!talk.turns[1].body.contains("Persona"));
3997
3998 assert!(switch_persona(&mut talk, &talks, "misato").expect("switch"));
3999 assert!(!switch_persona(&mut talk, &talks, "misato").expect("same"));
4000 assert!(talk.persona_dirty);
4001 assert!(talk.turns.last().unwrap().body.contains("persona changed"));
4002
4003 say(&lease, &mut talk, &talks, &cfg, "next", Vec::new())
4004 .await
4005 .expect("turn");
4006 let prompt = &talk.turns.last().unwrap().body;
4007 assert!(prompt.contains("# Persona update"), "{prompt}");
4008 assert!(prompt.contains("Misato Katsuragi"));
4009 assert!(!talk.persona_dirty, "cleared after a successful turn");
4010
4011 say(&lease, &mut talk, &talks, &cfg, "again", Vec::new())
4012 .await
4013 .expect("turn");
4014 assert!(!talk.turns.last().unwrap().body.contains("# Persona update"));
4015
4016 assert!(switch_persona(&mut talk, &talks, "default").expect("back"));
4017 assert_eq!(talk.persona, "");
4018 say(&lease, &mut talk, &talks, &cfg, "plain", Vec::new())
4019 .await
4020 .expect("turn");
4021 assert!(
4022 talk.turns
4023 .last()
4024 .unwrap()
4025 .body
4026 .contains("turned the persona off")
4027 );
4028
4029 assert!(switch_persona(&mut talk, &talks, "rei").expect("rei"));
4031 let mut no_sessions = cfg.clone();
4032 no_sessions.graph.sessions = false;
4033 say(&lease, &mut talk, &talks, &no_sessions, "one", Vec::new())
4034 .await
4035 .expect("turn");
4036 say(&lease, &mut talk, &talks, &no_sessions, "two", Vec::new())
4037 .await
4038 .expect("turn");
4039 let last = &talk.turns.last().unwrap().body;
4040 assert!(!talk.persona_dirty);
4041 assert!(last.contains("# Persona (tone only)"), "{last}");
4042 assert!(last.contains("Rei Ayanami"));
4043 }
4044
4045 #[tokio::test]
4046 async fn the_first_turn_carries_the_briefing_and_later_turns_do_not() {
4047 let (tmp, talks) = store();
4048 let spec = mock_agent(tmp.path(), ECHO, BTreeMap::new());
4049 let cfg = config(spec);
4050 let mut talk = begin(&talks, &cfg, tmp.path().to_owned(), None).expect("begin");
4051
4052 say(
4053 &talks.claim_turn(&talk.id).unwrap().unwrap(),
4054 &mut talk,
4055 &talks,
4056 &cfg,
4057 "what does the queue module do?",
4058 Vec::new(),
4059 )
4060 .await
4061 .expect("first turn");
4062 let first_prompt = &talk.turns[1].body;
4063 assert!(first_prompt.contains("magi task add --solo"));
4064 assert!(first_prompt.contains("what does the queue module do?"));
4065
4066 say(
4067 &talks.claim_turn(&talk.id).unwrap().unwrap(),
4068 &mut talk,
4069 &talks,
4070 &cfg,
4071 "and how is it locked?",
4072 Vec::new(),
4073 )
4074 .await
4075 .expect("second turn");
4076 let second_prompt = &talk.turns[3].body;
4077 assert!(
4078 !second_prompt.contains("magi task add --solo"),
4079 "the briefing is sent once, not on every turn: {second_prompt}"
4080 );
4081 assert!(second_prompt.contains("and how is it locked?"));
4082 }
4083
4084 #[tokio::test]
4085 async fn switching_agent_resets_the_seat_notes_it_and_resends_the_transcript() {
4086 let (tmp, talks) = store();
4087 let a = mock_agent(tmp.path(), ECHO, BTreeMap::new());
4088 let mut b = a.clone();
4089 b.id = "other".to_owned();
4090 let mut cfg = config(a.clone());
4091 cfg.agents.push(b.clone());
4092 let mut talk = begin(&talks, &cfg, tmp.path().to_owned(), Some(&a.id)).expect("begin");
4093 say(
4094 &talks.claim_turn(&talk.id).unwrap().unwrap(),
4095 &mut talk,
4096 &talks,
4097 &cfg,
4098 "remember the walrus",
4099 Vec::new(),
4100 )
4101 .await
4102 .expect("first turn");
4103 let old_session = talk.seat.claude_session.clone();
4104 assert_eq!(talk.seat.turns, 1);
4105
4106 assert!(switch_agent(&mut talk, &talks, &b).expect("switch"));
4107 assert_eq!(talk.agent, "other");
4108 assert_eq!(talk.seat.turns, 0);
4109 assert_eq!(talk.seat.agent, "other");
4110 assert_ne!(talk.seat.claude_session, old_session);
4111 let note = talk.turns.last().expect("note");
4112 assert_eq!(note.who, Who::Agent);
4113 assert!(note.body.starts_with(MAGI_NOTE), "{}", note.body);
4114 assert!(note.body.contains("changed from"), "{}", note.body);
4115 assert_eq!(talks.get(&talk.id).expect("reload").agent, "other");
4116
4117 let before = talk.turns.len();
4118 assert!(!switch_agent(&mut talk, &talks, &b).expect("same agent"));
4119 assert_eq!(talk.turns.len(), before, "a no-op writes no note");
4120
4121 say(
4122 &talks.claim_turn(&talk.id).unwrap().unwrap(),
4123 &mut talk,
4124 &talks,
4125 &cfg,
4126 "what did I say?",
4127 Vec::new(),
4128 )
4129 .await
4130 .expect("turn after switch");
4131 let prompt = &talk.turns.last().expect("reply").body;
4132 assert!(prompt.contains("remember the walrus"), "{prompt}");
4133 assert!(prompt.contains("## magi"), "{prompt}");
4134 assert!(prompt.contains("what did I say?"), "{prompt}");
4135 }
4136
4137 #[tokio::test]
4138 async fn say_appends_the_operator_turn_then_the_agent_turn() {
4139 let (tmp, talks) = store();
4140 let spec = mock_agent(tmp.path(), REPLY, env("go ahead"));
4141 let cfg = config(spec);
4142 let mut talk = begin(&talks, &cfg, tmp.path().to_owned(), None).expect("begin");
4143
4144 say(
4145 &talks.claim_turn(&talk.id).unwrap().unwrap(),
4146 &mut talk,
4147 &talks,
4148 &cfg,
4149 "can I rename this function?",
4150 Vec::new(),
4151 )
4152 .await
4153 .expect("say");
4154
4155 assert_eq!(talk.turns.len(), 2);
4156 assert_eq!(talk.turns[0].who, Who::Operator);
4157 assert_eq!(talk.turns[0].body, "can I rename this function?");
4158 assert_eq!(talk.turns[1].who, Who::Agent);
4159 assert_eq!(talk.turns[1].body, "go ahead");
4160 assert_eq!(talks.get(&talk.id).expect("get").turns, talk.turns);
4161 }
4162
4163 #[tokio::test]
4164 async fn a_failed_turn_keeps_the_operator_message_and_says_what_happened() {
4165 let (tmp, talks) = store();
4166 let spec = mock_agent(tmp.path(), BROKEN, BTreeMap::new());
4167 let cfg = config(spec);
4168 let mut talk = begin(&talks, &cfg, tmp.path().to_owned(), None).expect("begin");
4169
4170 talk.posts_read_through = None;
4171 talks.put(&mut talk).expect("save old-format boundary");
4172 post(&talks, &talk.id, "unread task result", "task").expect("post");
4173 talk = talks.get(&talk.id).expect("reload");
4174
4175 let err = say(
4176 &talks.claim_turn(&talk.id).unwrap().unwrap(),
4177 &mut talk,
4178 &talks,
4179 &cfg,
4180 "check the tests",
4181 Vec::new(),
4182 )
4183 .await
4184 .expect_err("a turn with no answer is an error");
4185 assert!(err.to_string().contains("no answer"), "{err}");
4186
4187 let on_disk = talks.get(&talk.id).expect("get");
4188 assert_eq!(on_disk.turns.len(), 3);
4189 assert_eq!(on_disk.posts_read_through, Some(0));
4190 assert!(
4191 posts_since_last_reply(&on_disk)
4192 .unwrap()
4193 .contains("unread task result")
4194 );
4195 assert_eq!(on_disk.turns[1].body, "check the tests");
4196 let note = &on_disk.turns[2];
4197 assert_eq!(note.who, Who::Agent);
4198 assert!(note.body.starts_with(MAGI_NOTE), "{}", note.body);
4199 assert!(note.body.contains("your message is saved"));
4200 }
4201
4202 #[tokio::test]
4208 async fn a_passing_write_failure_while_saving_the_reply_does_not_lose_it() {
4209 let (tmp, talks) = store();
4210 let spec = mock_agent(tmp.path(), REPLY, env("go ahead"));
4211 let cfg = config(spec);
4212 let mut talk = begin(&talks, &cfg, tmp.path().to_owned(), None).expect("begin");
4213
4214 let text =
4215 record(&mut talk, &talks, "can I rename this function?", Vec::new()).expect("record");
4216 failpoint::force_put_failures(PUT_RETRIES - 1);
4219 respond(
4220 &talks.claim_turn(&talk.id).unwrap().unwrap(),
4221 &mut talk,
4222 &talks,
4223 &cfg,
4224 &text,
4225 )
4226 .await
4227 .expect("respond must survive a write failure its own retries can outlast");
4228
4229 assert_eq!(talk.turns.len(), 2);
4230 assert_eq!(talk.turns[1].who, Who::Agent);
4231 assert_eq!(talk.turns[1].body, "go ahead");
4232 let on_disk = talks.get(&talk.id).expect("get");
4233 assert_eq!(
4234 on_disk.turns, talk.turns,
4235 "the reply must reach disk despite the early write failures"
4236 );
4237 }
4238
4239 #[tokio::test]
4245 async fn a_persistent_write_failure_while_saving_the_reply_is_never_silent() {
4246 let (tmp, talks) = store();
4247 let spec = mock_agent(tmp.path(), REPLY, env("go ahead"));
4248 let cfg = config(spec);
4249 let mut talk = begin(&talks, &cfg, tmp.path().to_owned(), None).expect("begin");
4250
4251 let text = record(&mut talk, &talks, "check the tests", Vec::new()).expect("record");
4252 failpoint::force_put_failures(PUT_RETRIES);
4257 let err = respond(
4258 &talks.claim_turn(&talk.id).unwrap().unwrap(),
4259 &mut talk,
4260 &talks,
4261 &cfg,
4262 &text,
4263 )
4264 .await
4265 .expect_err("a reply that cannot be saved must be reported, not swallowed");
4266 assert!(err.to_string().contains("could not be saved"), "{err}");
4267
4268 let on_disk = talks.get(&talk.id).expect("get");
4269 assert_eq!(
4270 on_disk.turns.len(),
4271 2,
4272 "the operator turn plus a visible note"
4273 );
4274 assert_eq!(on_disk.turns[0].body, "check the tests");
4275 let note = &on_disk.turns[1];
4276 assert_eq!(note.who, Who::Agent);
4277 assert!(note.body.starts_with(MAGI_NOTE), "{}", note.body);
4278 assert!(
4279 note.body.contains("could not be saved"),
4280 "the operator must be told the reply is missing, not left staring \
4281 at a gap with no explanation: {}",
4282 note.body
4283 );
4284 assert_eq!(
4285 talk.turns, on_disk.turns,
4286 "the in-memory talk must match what actually landed on disk"
4287 );
4288
4289 let artifacts = talks.artifacts_of(&talk.id);
4292 let stash = std::fs::read_dir(&artifacts)
4293 .expect("artifacts dir")
4294 .filter_map(|e| e.ok())
4295 .find(|e| e.file_name().to_string_lossy().ends_with("-lost.txt"))
4296 .expect("a stash file for the lost reply");
4297 let stashed = std::fs::read_to_string(stash.path()).expect("read stash");
4298 assert_eq!(stashed, "go ahead");
4299
4300 assert_eq!(
4308 on_disk.seat.turns, 1,
4309 "the note's write must carry the turn the CLI actually took"
4310 );
4311 assert_eq!(
4312 on_disk.seat.claude_session, talk.seat.claude_session,
4313 "the session id handed to the CLI must survive the failed reply"
4314 );
4315 assert_eq!(on_disk.seat.captured_session, talk.seat.captured_session);
4316 assert!(
4317 agent::has_session(AgentKind::Command, &on_disk.seat, cfg.graph.sessions),
4318 "the next turn must resume, not open the same session id twice"
4319 );
4320 }
4321
4322 #[tokio::test]
4327 async fn a_write_failure_that_also_loses_the_note_still_reports_it() {
4328 let (tmp, talks) = store();
4329 let spec = mock_agent(tmp.path(), REPLY, env("go ahead"));
4330 let cfg = config(spec);
4331 let mut talk = begin(&talks, &cfg, tmp.path().to_owned(), None).expect("begin");
4332
4333 let text = record(&mut talk, &talks, "check the tests", Vec::new()).expect("record");
4334 failpoint::force_put_failures(PUT_RETRIES * 2);
4337 let err = respond(
4338 &talks.claim_turn(&talk.id).unwrap().unwrap(),
4339 &mut talk,
4340 &talks,
4341 &cfg,
4342 &text,
4343 )
4344 .await
4345 .expect_err("neither the reply nor the note could be saved");
4346 assert!(err.to_string().contains("could not be saved"), "{err}");
4347
4348 assert_eq!(talk.turns.len(), 1, "only the operator's own turn");
4349 let on_disk = talks.get(&talk.id).expect("get");
4350 assert_eq!(on_disk.turns.len(), 1);
4351
4352 assert_eq!(
4362 on_disk.seat.turns, 0,
4363 "an unwritable file cannot record the turn the CLI took"
4364 );
4365 assert_eq!(
4366 talk.seat.turns, 1,
4367 "the in-memory seat still reports the turn the CLI actually took"
4368 );
4369 assert_eq!(
4370 on_disk.seat.claude_session, talk.seat.claude_session,
4371 "the session id was minted at `begin` and never changes here"
4372 );
4373 }
4374
4375 #[tokio::test]
4379 async fn attachments_reach_the_prompt_and_an_empty_body_is_still_a_turn() {
4380 let (tmp, talks) = store();
4381 let spec = mock_agent(tmp.path(), ECHO, BTreeMap::new());
4382 let cfg = config(spec);
4383 let mut talk = begin(&talks, &cfg, tmp.path().to_owned(), None).expect("begin");
4384
4385 let att = talks
4386 .put_attachment(
4387 &talk.id,
4388 "image/png",
4389 "screenshot.png",
4390 b"pretend-png-bytes",
4391 )
4392 .expect("put attachment");
4393
4394 say(
4395 &talks.claim_turn(&talk.id).unwrap().unwrap(),
4396 &mut talk,
4397 &talks,
4398 &cfg,
4399 "",
4400 vec![att.clone()],
4401 )
4402 .await
4403 .expect("an empty body with an attachment is still a turn");
4404
4405 let operator_turn = &talk.turns[0];
4406 assert_eq!(operator_turn.who, Who::Operator);
4407 assert_eq!(operator_turn.body, "");
4408 assert_eq!(operator_turn.attachments, vec![att.clone()]);
4409
4410 let prompt = &talk.turns[1].body;
4411 let expected_path = talks
4412 .attachments_dir(&talk.id)
4413 .join(format!("{}.png", att.id));
4414 assert!(
4415 prompt.contains(&expected_path.display().to_string()),
4416 "the agent must be told the attachment's absolute path: {prompt}"
4417 );
4418 assert!(prompt.contains("image/png"), "and its mime: {prompt}");
4419 }
4420
4421 #[test]
4430 fn attachment_path_is_absolute_even_when_the_store_root_is_relative() {
4431 let talks = Talks::at(PathBuf::from("relative-talks-root-for-this-test"));
4432 let att = Attachment {
4433 id: "0".repeat(32),
4434 name: "shot.png".to_owned(),
4435 mime: "image/png".to_owned(),
4436 bytes: 3,
4437 };
4438 let path = talks
4439 .attachment_path("some-talk-id", &att)
4440 .expect("a supported mime always yields a path");
4441 assert!(
4442 path.is_absolute(),
4443 "must be absolute even off a relative store root: {}",
4444 path.display()
4445 );
4446 }
4447
4448 #[tokio::test]
4449 async fn a_turn_past_the_configured_talk_timeout_is_reported_with_that_timeout() {
4450 let (tmp, talks) = store();
4455 let slow = mock_agent(
4456 tmp.path(),
4457 "#!/bin/sh\ncat >/dev/null\nsleep 2\n",
4458 BTreeMap::new(),
4459 );
4460 let mut cfg = config(slow);
4461 cfg.graph.timeout_talk = 1;
4462 let mut talk = begin(&talks, &cfg, tmp.path().to_owned(), None).expect("begin");
4463
4464 let err = say(
4465 &talks.claim_turn(&talk.id).unwrap().unwrap(),
4466 &mut talk,
4467 &talks,
4468 &cfg,
4469 "check the tests",
4470 Vec::new(),
4471 )
4472 .await
4473 .expect_err("a turn that never answers is an error");
4474 assert!(
4475 err.to_string().contains("did not answer within 1s"),
4476 "{err}"
4477 );
4478
4479 let on_disk = talks.get(&talk.id).expect("get");
4480 let note = on_disk.turns.last().expect("a note turn was recorded");
4481 assert!(
4482 note.body.contains("did not answer within 1s"),
4483 "the transcript must show the configured timeout: {}",
4484 note.body
4485 );
4486 }
4487
4488 #[test]
4489 fn closing_is_idempotent_and_a_closed_talk_takes_no_more_turns() {
4490 let (tmp, talks) = store();
4491 let spec = mock_agent(tmp.path(), REPLY, env("hi"));
4492 let cfg = config(spec);
4493 let mut talk = begin(&talks, &cfg, tmp.path().to_owned(), None).expect("begin");
4494
4495 close(&mut talk, &talks).expect("close");
4496 assert_eq!(talk.status, TalkStatus::Closed);
4497 close(&mut talk, &talks).expect("closing twice is not an error");
4498
4499 let err =
4500 record(&mut talk, &talks, "still there?", Vec::new()).expect_err("closed talks refuse");
4501 assert!(err.to_string().contains("closed"));
4502 let _ = &cfg; }
4504
4505 #[tokio::test]
4506 async fn a_close_that_lands_while_a_turn_is_in_flight_is_not_undone_by_the_reply() {
4507 let (tmp, talks) = store();
4508 let spec = mock_agent(tmp.path(), REPLY, env("here you go"));
4509 let cfg = config(spec);
4510 let mut in_flight = begin(&talks, &cfg, tmp.path().to_owned(), None).expect("begin");
4513
4514 let mut closed_elsewhere = talks.get(&in_flight.id).expect("reread");
4518 close(&mut closed_elsewhere, &talks).expect("close");
4519 assert_eq!(
4520 talks.get(&in_flight.id).expect("reread").status,
4521 TalkStatus::Closed,
4522 "the close landed on disk before the turn finished"
4523 );
4524
4525 assert_eq!(in_flight.status, TalkStatus::Open);
4529 respond(
4530 &talks.claim_turn(&in_flight.id).unwrap().unwrap(),
4531 &mut in_flight,
4532 &talks,
4533 &cfg,
4534 "one more question",
4535 )
4536 .await
4537 .expect("the turn itself still completes");
4538
4539 let on_disk = talks.get(&in_flight.id).expect("reread");
4540 assert_eq!(
4541 on_disk.status,
4542 TalkStatus::Closed,
4543 "a close must stick even when a turn that started before it finishes after it"
4544 );
4545 assert!(
4548 on_disk.turns.iter().any(|t| t.body == "here you go"),
4549 "the in-flight turn's own reply is still recorded: {:?}",
4550 on_disk.turns
4551 );
4552 }
4553
4554 #[test]
4555 fn a_close_that_lands_before_record_is_called_is_not_undone_by_it() {
4556 let (tmp, talks) = store();
4557 let spec = mock_agent(tmp.path(), REPLY, env("hi"));
4558 let cfg = config(spec);
4559 let mut stale = begin(&talks, &cfg, tmp.path().to_owned(), None).expect("begin");
4562
4563 let mut closed_elsewhere = talks.get(&stale.id).expect("reread");
4566 close(&mut closed_elsewhere, &talks).expect("close");
4567 assert_eq!(
4568 talks.get(&stale.id).expect("reread").status,
4569 TalkStatus::Closed,
4570 "the close landed on disk before record was called"
4571 );
4572
4573 assert_eq!(stale.status, TalkStatus::Open);
4577 let err = record(&mut stale, &talks, "still there?", Vec::new())
4578 .expect_err("a close that landed first must be honored, not overwritten");
4579 assert!(err.to_string().contains("closed"));
4580
4581 let on_disk = talks.get(&stale.id).expect("reread");
4582 assert_eq!(
4583 on_disk.status,
4584 TalkStatus::Closed,
4585 "record must not resurrect a conversation closed while its snapshot was stale"
4586 );
4587 assert!(
4588 on_disk.turns.is_empty(),
4589 "the rejected turn must not have been appended: {:?}",
4590 on_disk.turns
4591 );
4592 let _ = &cfg; }
4594
4595 #[test]
4596 fn close_blocks_on_records_guard_rather_than_interleaving_with_it() {
4597 let (tmp, talks) = store();
4598 let spec = mock_agent(tmp.path(), REPLY, env("hi"));
4599 let cfg = config(spec);
4600 let talk = begin(&talks, &cfg, tmp.path().to_owned(), None).expect("begin");
4601
4602 let held = talks.guard().unwrap();
4606
4607 let talks2 = talks.clone();
4608 let id = talk.id.clone();
4609 let closing = std::thread::spawn(move || {
4610 let mut talk = talks2.get(&id).expect("get");
4611 close(&mut talk, &talks2).expect("close");
4612 });
4613
4614 std::thread::sleep(Duration::from_millis(50));
4615 assert!(
4616 !closing.is_finished(),
4617 "close must wait for the guard, not read and write while it is held - \
4618 a re-read alone narrows this window without closing it"
4619 );
4620
4621 drop(held);
4622 closing.join().expect("close thread panicked");
4623
4624 assert_eq!(
4625 talks.get(&talk.id).expect("reread").status,
4626 TalkStatus::Closed,
4627 "once the guard is free, close still lands"
4628 );
4629 let _ = &cfg; }
4631
4632 #[test]
4633 fn reopening_a_closed_talk_lets_it_take_turns_again_and_reopening_twice_is_not_an_error() {
4634 let (tmp, talks) = store();
4635 let spec = mock_agent(tmp.path(), REPLY, env("hi"));
4636 let cfg = config(spec);
4637 let mut talk = begin(&talks, &cfg, tmp.path().to_owned(), None).expect("begin");
4638
4639 close(&mut talk, &talks).expect("close");
4640 assert_eq!(talk.status, TalkStatus::Closed);
4641
4642 reopen(&mut talk, &talks).expect("reopen");
4643 assert_eq!(talk.status, TalkStatus::Open);
4644 assert_eq!(
4645 talks.get(&talk.id).expect("reread").status,
4646 TalkStatus::Open
4647 );
4648
4649 reopen(&mut talk, &talks).expect("reopening an open talk is not an error");
4651 assert_eq!(talk.status, TalkStatus::Open);
4652
4653 record(&mut talk, &talks, "one more thing", Vec::new())
4654 .expect("a reopened talk takes turns again");
4655 let _ = &cfg; }
4657
4658 #[test]
4659 fn removing_a_talk_deletes_its_record_and_artifacts_and_refuses_an_unknown_id() {
4660 let (tmp, talks) = store();
4661 let spec = mock_agent(tmp.path(), REPLY, env("hi"));
4662 let cfg = config(spec);
4663 let talk = begin(&talks, &cfg, tmp.path().to_owned(), None).expect("begin");
4664
4665 let artifacts = talks.artifacts_of(&talk.id);
4666 std::fs::create_dir_all(&artifacts).expect("create artifacts dir");
4667 std::fs::write(artifacts.join("turn-1.txt"), "hello").expect("write artifact");
4668
4669 talks.remove(&talk.id).expect("remove");
4670 assert!(!talks.path_of(&talk.id).is_file(), "the record is gone");
4671 assert!(!artifacts.is_dir(), "the artifacts directory is gone");
4672 assert!(
4673 talks.get(&talk.id).is_err(),
4674 "a removed talk cannot be read back"
4675 );
4676
4677 let err = talks
4678 .remove("nonexistent-id")
4679 .expect_err("unknown id refused");
4680 assert!(err.to_string().contains("no talk matches"), "{err}");
4681 let _ = &cfg; }
4683
4684 #[tokio::test]
4685 async fn a_delete_that_lands_while_a_turn_is_in_flight_is_not_undone_by_the_reply() {
4686 let (tmp, talks) = store();
4687 let spec = mock_agent(tmp.path(), REPLY, env("here you go"));
4688 let cfg = config(spec);
4689 let mut in_flight = begin(&talks, &cfg, tmp.path().to_owned(), None).expect("begin");
4692
4693 talks.remove(&in_flight.id).expect("remove");
4694 assert!(
4695 talks.get(&in_flight.id).is_err(),
4696 "the delete landed on disk before the turn finished"
4697 );
4698
4699 respond(
4702 &talks.claim_turn(&in_flight.id).unwrap().unwrap(),
4703 &mut in_flight,
4704 &talks,
4705 &cfg,
4706 "one more question",
4707 )
4708 .await
4709 .expect("the turn itself still completes rather than erroring");
4710
4711 assert!(
4712 talks.get(&in_flight.id).is_err(),
4713 "a delete must stick even when a turn that started before it finishes after it"
4714 );
4715 }
4716
4717 #[test]
4718 fn a_delete_that_lands_before_record_is_called_is_not_undone_by_it() {
4719 let (tmp, talks) = store();
4720 let spec = mock_agent(tmp.path(), REPLY, env("hi"));
4721 let cfg = config(spec);
4722 let mut stale = begin(&talks, &cfg, tmp.path().to_owned(), None).expect("begin");
4725
4726 talks.remove(&stale.id).expect("remove");
4727
4728 let err = record(&mut stale, &talks, "still there?", Vec::new())
4732 .expect_err("a delete that landed first must be honored, not overwritten");
4733 assert!(err.to_string().contains("deleted"), "{err}");
4734
4735 assert!(
4736 talks.get(&stale.id).is_err(),
4737 "record must not resurrect a conversation deleted while its snapshot was stale"
4738 );
4739 let _ = &cfg; }
4741
4742 #[test]
4743 fn a_delete_that_lands_before_close_is_called_is_not_undone_by_it() {
4744 let (tmp, talks) = store();
4745 let spec = mock_agent(tmp.path(), REPLY, env("hi"));
4746 let cfg = config(spec);
4747 let mut stale = begin(&talks, &cfg, tmp.path().to_owned(), None).expect("begin");
4750
4751 talks.remove(&stale.id).expect("remove");
4752
4753 let err = close(&mut stale, &talks)
4757 .expect_err("a delete that landed first must be honored, not overwritten");
4758 assert!(err.to_string().contains("deleted"), "{err}");
4759
4760 assert!(
4761 talks.get(&stale.id).is_err(),
4762 "close must not resurrect a conversation deleted while its snapshot was stale"
4763 );
4764 let _ = &cfg; }
4766
4767 #[test]
4768 fn a_delete_that_lands_before_reopen_is_called_is_not_undone_by_it() {
4769 let (tmp, talks) = store();
4770 let spec = mock_agent(tmp.path(), REPLY, env("hi"));
4771 let cfg = config(spec);
4772 let mut stale = begin(&talks, &cfg, tmp.path().to_owned(), None).expect("begin");
4775 close(&mut stale, &talks).expect("close");
4776
4777 talks.remove(&stale.id).expect("remove");
4778
4779 let err = reopen(&mut stale, &talks)
4783 .expect_err("a delete that landed first must be honored, not overwritten");
4784 assert!(err.to_string().contains("deleted"), "{err}");
4785
4786 assert!(
4787 talks.get(&stale.id).is_err(),
4788 "reopen must not resurrect a conversation deleted while its snapshot was stale"
4789 );
4790 let _ = &cfg; }
4792
4793 #[test]
4794 fn list_puts_open_talks_before_closed_ones() {
4795 let (tmp, talks) = store();
4796 let make = |id: &str, status: TalkStatus| {
4797 let mut t = Talk {
4798 posts_read_through: None,
4799 pending_breaks: None,
4800 schema: SCHEMA,
4801 id: id.to_owned(),
4802 repo: tmp.path().to_owned(),
4803 agent: "mock".to_owned(),
4804 status,
4805 turns: Vec::new(),
4806 pending: String::new(),
4807 pending_attachments: Vec::new(),
4808 fallback: false,
4809 persona: String::new(),
4810 persona_dirty: false,
4811 implementers: 1,
4812 implementers_dirty: false,
4813 notified: Vec::new(),
4814 created_at: Timestamp::now(),
4815 updated_at: Timestamp::now(),
4816 seat: SeatState::new(SEAT, "mock", 7),
4817 };
4818 talks.put(&mut t).expect("put");
4819 };
4820 make("20260901-000000-0001", TalkStatus::Open);
4821 make("20260902-000000-0002", TalkStatus::Open);
4822 make("20260903-000000-0003", TalkStatus::Closed);
4823
4824 let ids: Vec<String> = talks.list().into_iter().map(|t| t.id).collect();
4825 assert_eq!(
4826 ids,
4827 [
4828 "20260902-000000-0002",
4829 "20260901-000000-0001",
4830 "20260903-000000-0003"
4831 ]
4832 );
4833 assert_eq!(talks.count_open(), 2);
4834 }
4835
4836 #[test]
4837 fn tasks_of_finds_only_this_talks_own_tasks() {
4838 let dir = tempfile::tempdir().expect("tempdir");
4839 let queue = Queue::at(dir.path().join("queue"));
4840
4841 let mut mine = Task::new(
4842 "rework the loader".to_owned(),
4843 "rework the loader".to_owned(),
4844 PathBuf::from("/repo"),
4845 Source::Agent {
4846 run: "20260904-014455-ab12".to_owned(),
4847 node: "chat".to_owned(),
4848 },
4849 );
4850 queue.put(&mut mine).expect("put mine");
4851
4852 let mut theirs = Task::new(
4853 "unrelated".to_owned(),
4854 "unrelated".to_owned(),
4855 PathBuf::from("/repo"),
4856 Source::Agent {
4857 run: "20260904-090000-zz99".to_owned(),
4858 node: "implement".to_owned(),
4859 },
4860 );
4861 queue.put(&mut theirs).expect("put theirs");
4862
4863 let mut human = Task::new(
4864 "typed by hand".to_owned(),
4865 "typed by hand".to_owned(),
4866 PathBuf::from("/repo"),
4867 Source::Human,
4868 );
4869 queue.put(&mut human).expect("put human");
4870
4871 let found = tasks_of(&queue, "20260904-014455-ab12");
4872 assert_eq!(found.len(), 1);
4873 assert_eq!(found[0].id, mine.id);
4874 }
4875
4876 #[test]
4877 fn the_briefing_names_solo_task_add() {
4878 let brief = briefing(Path::new("/repo"), "en", false);
4879 assert!(brief.contains("magi task add --solo"));
4880 assert!(brief.contains("/repo"));
4881 assert!(!brief.contains("Hold this conversation in"));
4882 }
4883
4884 #[test]
4890 fn the_briefing_explains_targeting_a_different_repository_by_name() {
4891 let brief = briefing(Path::new("/repo"), "en", false);
4892 assert!(brief.contains("--repo does not have to be a full path"));
4893 assert!(brief.contains("owner/repo"));
4894 assert!(brief.contains("magi repos"));
4895 assert!(brief.contains("ask the operator"));
4896 }
4897
4898 #[test]
4899 fn the_briefing_tells_the_assistant_to_pass_images_with_attach() {
4900 let brief = briefing(Path::new("/repo"), "en", false);
4901 assert!(brief.contains("--attach <path>"), "{brief}");
4902 assert!(brief.contains("deleting this conversation"), "{brief}");
4903 }
4904
4905 #[test]
4906 fn the_briefing_names_the_language_when_it_is_not_english() {
4907 let brief = briefing(Path::new("/repo"), "Japanese", false);
4908 assert!(brief.contains("Hold this conversation in Japanese"));
4909 }
4910
4911 #[test]
4912 fn the_briefing_forbids_writes_unless_the_repository_opted_in() {
4913 let read_only = briefing(Path::new("/repo"), "en", false);
4914 assert!(read_only.contains("Do not write files"));
4915 assert!(!read_only.contains("allow_write"));
4916
4917 let writable = briefing(Path::new("/repo"), "en", true);
4918 assert!(!writable.contains("Do not write files"));
4919 assert!(writable.contains("allow_write = true"));
4920 assert!(writable.contains("magi task add --solo"));
4923 assert!(writable.contains("say plainly what you"));
4924 }
4925
4926 #[test]
4927 fn the_briefing_says_the_chat_cannot_push_and_tasks_report_back() {
4928 let b = briefing_for(Path::new("/r"), "en", false, None, None, 1);
4929 assert!(
4930 b.contains("cannot send the operator a message on your own"),
4931 "{b}"
4932 );
4933 assert!(b.contains(crate::prompt::CHAT_TASK_NOTICE_HEADING));
4934 assert!(b.contains("magi talk watch --timeout 900"));
4935 assert!(b.contains("nor a successfully registered watch"));
4936 }
4937
4938 #[test]
4939 fn a_post_is_replayed_until_the_agent_answers_again() {
4940 let (_tmp, talks) = store();
4941 let mut talk = ctx_talk("mock", Vec::new());
4942 talks.put(&mut talk).expect("put");
4943 post(&talks, &talk.id, " found it ", "20260902-000000-aaaa").expect("post");
4944 let mut talk = talks.get(&talk.id).expect("get");
4945 let block = posts_since_last_reply(&talk).expect("block");
4946 assert!(block.starts_with("# Posted by a task"), "{block}");
4947 assert!(block.contains("found it"));
4948 talk.turns.push(reply("ok", None));
4949 assert!(posts_since_last_reply(&talk).is_none());
4950 assert!(post(&talks, &talk.id, " ", "r").is_err());
4951 close(&mut talk, &talks).expect("close");
4952 assert!(post(&talks, &talk.id, "late", "r").is_err());
4953 }
4954
4955 #[test]
4956 fn queue_once_accepts_a_key_a_single_time_even_after_a_drain() {
4957 let (_tmp, talks) = store();
4958 let mut talk = ctx_talk("mock", Vec::new());
4959 talks.put(&mut talk).expect("put");
4960 assert!(queue_once(&mut talk, &talks, "one", "k").expect("first"));
4961 drain(&mut talk, &talks).expect("drain");
4962 assert!(!queue_once(&mut talk, &talks, "one", "k").expect("again"));
4963 assert!(talks.get(&talk.id).expect("get").pending.is_empty());
4964 }
4965}