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, Copy, PartialEq, Eq, Serialize, Deserialize)]
100#[serde(rename_all = "lowercase")]
101pub enum Sender {
102 Operator,
104 Persona,
106 Magi,
108}
109
110pub fn sender(turn: &Turn, message: Option<&Message>) -> Sender {
112 message
113 .and_then(|m| m.sender)
114 .or(turn.sender)
115 .unwrap_or_else(|| {
116 if turn.posted_by.is_some()
117 || (turn.who == Who::Agent && turn.body.starts_with(MAGI_NOTE))
118 {
119 Sender::Magi
120 } else if turn.who == Who::Operator {
121 Sender::Operator
122 } else {
123 Sender::Persona
124 }
125 })
126}
127
128#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
136#[serde(deny_unknown_fields)]
137pub struct Attachment {
138 pub id: String,
140 pub name: String,
142 pub mime: String,
145 pub bytes: u64,
147}
148
149#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
151#[serde(deny_unknown_fields)]
152pub struct ReplyTarget {
153 pub turn: usize,
155 pub message: usize,
157}
158
159#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
161pub struct Reply {
162 #[serde(default, skip_serializing_if = "Option::is_none")]
164 pub sender: Option<Sender>,
165 pub target: ReplyTarget,
167 pub who: Who,
169 pub excerpt: String,
171}
172
173#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
175pub struct Message {
176 #[serde(default, skip_serializing_if = "Option::is_none")]
178 pub sender: Option<Sender>,
179 pub body: String,
181 pub attachments: Vec<Attachment>,
183 pub reply: Option<Reply>,
185}
186
187fn resolve_reply(talk: &Talk, target: Option<ReplyTarget>) -> Result<Option<Reply>> {
188 let Some(target) = target else {
189 return Ok(None);
190 };
191 let turn = talk
192 .turns
193 .get(target.turn)
194 .context("invalid reply target")?;
195 let (body, attachments) = if turn.messages.is_empty() {
196 if target.message != 0 {
197 bail!("invalid reply target");
198 }
199 (&turn.body, &turn.attachments)
200 } else {
201 let message = turn
202 .messages
203 .get(target.message)
204 .context("invalid reply target")?;
205 (&message.body, &message.attachments)
206 };
207 let excerpt = if body.trim().is_empty() {
208 format!("{} attachment(s)", attachments.len())
209 } else {
210 body.chars().take(240).collect()
211 };
212 Ok(Some(Reply {
213 sender: Some(sender(turn, turn.messages.get(target.message))),
214 target,
215 who: turn.who,
216 excerpt,
217 }))
218}
219
220fn message_prompt(turn: &Turn, talk: &Talk, store: &Talks) -> String {
222 if turn.messages.is_empty() {
223 return turn.body.clone();
224 }
225 turn.messages
226 .iter()
227 .map(|message| match &message.reply {
228 Some(reply) => {
229 let mut quote = format!(
230 "[Reply to {}: {}]",
231 if reply.sender == Some(Sender::Magi) {
232 "MAGI".to_owned()
233 } else {
234 format!("{:?}", reply.who)
235 },
236 reply.excerpt.lines().collect::<Vec<_>>().join(" "),
237 );
238 if let Some(target) = talk.turns.get(reply.target.turn) {
239 let attachments: &[Attachment] = if target.messages.is_empty() {
240 &target.attachments
241 } else if let Some(message) = target.messages.get(reply.target.message) {
242 &message.attachments
243 } else {
244 &[]
245 };
246 for attachment in attachments {
247 if let Some(path) = store.attachment_path(&talk.id, attachment) {
248 quote.push_str(&format!(
249 "\nReplied-to image: {} ({})",
250 path.display(),
251 attachment.mime
252 ));
253 }
254 }
255 }
256 format!("{quote}\n{}", message.body)
257 }
258 None => message.body.clone(),
259 })
260 .collect::<Vec<_>>()
261 .join("\n\n")
262}
263
264fn messages_need_no_metadata(messages: &[Message]) -> bool {
266 messages.iter().all(|message| {
267 message.sender.is_none() && message.reply.is_none() && message.attachments.is_empty()
268 })
269}
270
271#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
273#[serde(deny_unknown_fields)]
274pub struct Turn {
275 #[serde(default, skip_serializing_if = "Option::is_none")]
277 pub sender: Option<Sender>,
278 pub who: Who,
280 pub body: String,
282 pub at: Timestamp,
284 #[serde(default)]
287 pub attachments: Vec<Attachment>,
288 #[serde(default, skip_serializing_if = "Option::is_none")]
293 pub usage: Option<TurnUsage>,
294 #[serde(default, skip_serializing_if = "Option::is_none")]
301 pub breaks: Option<Vec<usize>>,
302 #[serde(default, skip_serializing_if = "Vec::is_empty")]
303 pub messages: Vec<Message>,
305 #[serde(default, skip_serializing_if = "Option::is_none")]
310 pub posted_by: Option<String>,
311}
312
313#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
321pub struct TurnUsage {
322 pub context_tokens: u64,
324 pub agent: String,
326 #[serde(default)]
328 pub model: Option<String>,
329}
330
331#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize)]
336pub struct ContextUsage {
337 pub tokens: Option<u64>,
339 pub window: Option<u64>,
341 pub percent: Option<u64>,
343 pub warn: bool,
345 pub since_switch: bool,
349 pub model: Option<String>,
351 #[serde(default)]
354 pub estimated: bool,
355}
356
357const CONTEXT_WARN_PERCENT: u64 = 80;
359
360const STANDING_PROMPT_FALLBACK_CHARS: u64 = 7000;
363
364pub fn estimate_context_tokens(talk: &Talk, standing_chars: u64) -> Option<u64> {
377 let mut counted = false;
378 let mut chars = standing_chars;
379 for t in talk.turns.iter().filter(|t| !t.body.starts_with(MAGI_NOTE)) {
380 counted = true;
381 chars += t.body.chars().count() as u64;
382 }
383 counted.then(|| (chars * 2).div_ceil(7))
385}
386
387pub fn context_usage(talk: &Talk, cfg: Option<&Config>) -> ContextUsage {
400 let current = cfg.and_then(|c| c.agents.iter().find(|a| a.id == talk.agent));
401 let model = current.and_then(|a| a.model.clone());
402 let window = cfg
403 .zip(model.as_deref())
404 .and_then(|(c, m)| c.context_window(m))
405 .filter(|w| *w > 0);
406 let usage = talk
407 .turns
408 .iter()
409 .rev()
410 .find(|t| t.who == Who::Agent && !t.body.starts_with(MAGI_NOTE))
411 .and_then(|t| t.usage.as_ref());
412 let measured = usage.map(|u| u.context_tokens);
413 let tokens = measured.or_else(|| {
414 let standing = cfg.map_or(STANDING_PROMPT_FALLBACK_CHARS, |c| {
415 briefing_for(
416 &talk.repo,
417 &c.graph.language,
418 c.talk.allow_write,
419 crate::persona::active(&c.talk.personas, &talk.persona).as_ref(),
420 c.talk.operator_name(),
421 talk.implementers,
422 )
423 .chars()
424 .count() as u64
425 });
426 estimate_context_tokens(talk, standing)
427 });
428 let estimated = measured.is_none() && tokens.is_some();
429 let since_switch =
430 usage.is_some_and(|u| u.agent != talk.agent || (current.is_some() && u.model != model));
431 let (percent, warn) = match (tokens, window) {
432 (Some(t), Some(w)) => (
433 Some(t.saturating_mul(100) / w),
434 t.saturating_mul(100) >= w.saturating_mul(CONTEXT_WARN_PERCENT),
435 ),
436 _ => (None, false),
437 };
438 ContextUsage {
439 tokens,
440 window,
441 percent,
442 warn,
443 since_switch,
444 model,
445 estimated,
446 }
447}
448
449#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
453#[serde(rename_all = "lowercase")]
454pub enum TalkStatus {
455 Open,
458 Closed,
460}
461
462impl TalkStatus {
463 pub fn open(self) -> bool {
465 matches!(self, Self::Open)
466 }
467
468 pub fn as_str(self) -> &'static str {
470 match self {
471 Self::Open => "open",
472 Self::Closed => "closed",
473 }
474 }
475}
476
477#[derive(Debug, Clone, Serialize, Deserialize)]
479#[serde(deny_unknown_fields)]
480pub struct Talk {
481 pub schema: u32,
483 pub id: String,
485 pub repo: PathBuf,
487 pub agent: String,
489 pub status: TalkStatus,
491 pub turns: Vec<Turn>,
493 #[serde(default, skip_serializing_if = "Option::is_none")]
497 pub posts_read_through: Option<usize>,
498 #[serde(default)]
501 pub pending: String,
502 #[serde(default)]
504 pub pending_attachments: Vec<Attachment>,
505 #[serde(default, skip_serializing_if = "Option::is_none")]
507 pub pending_breaks: Option<Vec<usize>>,
508 #[serde(default, skip_serializing_if = "Vec::is_empty")]
509 pub pending_messages: Vec<Message>,
511 #[serde(default)]
516 pub fallback: bool,
517 #[serde(default)]
520 pub persona: String,
521 #[serde(default)]
525 pub persona_dirty: bool,
526 #[serde(default = "solo_implementers")]
529 pub implementers: u8,
530 #[serde(default)]
533 pub implementers_dirty: bool,
534 #[serde(default)]
539 pub notified: Vec<String>,
540 #[serde(default, skip_serializing_if = "Vec::is_empty")]
542 pub completion_notices: Vec<crate::chat_notify::CompletionReference>,
543 pub created_at: Timestamp,
545 pub updated_at: Timestamp,
547 seat: SeatState,
552}
553
554impl Talk {
555 pub fn short(&self) -> &str {
557 short(&self.id)
558 }
559}
560
561#[derive(Debug, Clone)]
563pub struct Talks {
564 root: PathBuf,
565 lock: Arc<Mutex<()>>,
574}
575
576impl Talks {
577 pub fn open() -> Self {
579 Self::at(crate::run::home().join("talks"))
580 }
581
582 pub fn at(root: PathBuf) -> Self {
585 Self {
586 root,
587 lock: Arc::new(Mutex::new(())),
588 }
589 }
590
591 fn guard(&self) -> Result<StoreGuard<'_>> {
606 let mutex = self.lock.lock().unwrap_or_else(PoisonError::into_inner);
607 std::fs::create_dir_all(&self.root)
608 .with_context(|| format!("create {}", self.root.display()))?;
609 let path = self.root.join(".store.turn");
610 let deadline = std::time::Instant::now() + TAKEOVER_LOCK_TTL * 2;
611 loop {
612 if let Some(file) = TurnLock::take(&path)? {
613 return Ok(StoreGuard {
614 _file: file,
615 _mutex: mutex,
616 });
617 }
618 if std::time::Instant::now() >= deadline {
619 bail!("the talk store is locked by another process");
620 }
621 std::thread::sleep(Duration::from_millis(10));
622 }
623 }
624
625 pub fn root(&self) -> &Path {
627 &self.root
628 }
629
630 pub fn path_of(&self, id: &str) -> PathBuf {
632 self.root.join(format!("{id}.json"))
633 }
634
635 pub fn artifacts_of(&self, id: &str) -> PathBuf {
638 self.root.join(format!("{id}.artifacts"))
639 }
640
641 pub fn attachments_dir(&self, id: &str) -> PathBuf {
645 self.artifacts_of(id).join("attachments")
646 }
647
648 pub fn put_attachment(
657 &self,
658 id: &str,
659 mime: &str,
660 name: &str,
661 data: &[u8],
662 ) -> Result<Attachment> {
663 let dir = self.attachments_dir(id);
664 std::fs::create_dir_all(&dir).with_context(|| format!("create {}", dir.display()))?;
665 let ext = attachment_ext(mime).with_context(|| format!("unsupported mime `{mime}`"))?;
666 let att = Attachment {
667 id: new_attachment_id(),
668 name: name.to_owned(),
669 mime: mime.to_owned(),
670 bytes: data.len() as u64,
671 };
672 std::fs::write(dir.join(format!("{}.{ext}", att.id)), data)
673 .with_context(|| format!("write attachment {}", att.id))?;
674 std::fs::write(
675 dir.join(format!("{}.json", att.id)),
676 serde_json::to_string(&att).context("serialize attachment")?,
677 )
678 .with_context(|| format!("write attachment metadata {}", att.id))?;
679 Ok(att)
680 }
681
682 pub fn attachment_meta(&self, id: &str, att_id: &str) -> Result<Option<Attachment>> {
691 if !valid_attachment_id(att_id) {
692 return Ok(None);
693 }
694 let meta_path = self.attachments_dir(id).join(format!("{att_id}.json"));
695 if !meta_path.is_file() {
696 return Ok(None);
697 }
698 let att = serde_json::from_str(
699 &std::fs::read_to_string(&meta_path)
700 .with_context(|| format!("read {}", meta_path.display()))?,
701 )
702 .with_context(|| format!("parse {}", meta_path.display()))?;
703 Ok(Some(att))
704 }
705
706 pub fn read_attachment(&self, id: &str, att_id: &str) -> Result<Option<(Attachment, Vec<u8>)>> {
710 let Some(att) = self.attachment_meta(id, att_id)? else {
711 return Ok(None);
712 };
713 let ext = attachment_ext(&att.mime).with_context(|| {
714 format!("attachment {att_id} has an unsupported mime `{}`", att.mime)
715 })?;
716 let data_path = self.attachments_dir(id).join(format!("{att_id}.{ext}"));
717 let data =
718 std::fs::read(&data_path).with_context(|| format!("read {}", data_path.display()))?;
719 Ok(Some((att, data)))
720 }
721
722 fn attachment_path(&self, id: &str, att: &Attachment) -> Option<PathBuf> {
739 let ext = attachment_ext(&att.mime)?;
740 let path = self.attachments_dir(id).join(format!("{}.{ext}", att.id));
741 std::path::absolute(&path).ok()
742 }
743
744 pub fn put(&self, t: &mut Talk) -> Result<()> {
753 std::fs::create_dir_all(&self.root)
754 .with_context(|| format!("create {}", self.root.display()))?;
755 t.updated_at = Timestamp::now();
756 let mut persisted = t.clone();
759 for turn in &mut persisted.turns {
760 if turn.attachments.is_empty() && messages_need_no_metadata(&turn.messages) {
761 turn.messages.clear();
762 }
763 }
764 if persisted.pending_attachments.is_empty()
765 && messages_need_no_metadata(&persisted.pending_messages)
766 {
767 persisted.pending_messages.clear();
768 }
769 let body = serde_json::to_string_pretty(&persisted).context("serialize talk")?;
770 let path = self.path_of(&t.id);
771 let tmp = path.with_extension("json.tmp");
772 write_atomic(&tmp, &path, &body)
773 }
774
775 pub fn get(&self, id: &str) -> Result<Talk> {
777 let resolved = self.resolve_id(id)?;
778 read_path(&self.path_of(&resolved))
779 }
780
781 pub fn list(&self) -> Vec<Talk> {
784 self.list_counting_unreadable().0
785 }
786
787 pub fn list_counting_unreadable(&self) -> (Vec<Talk>, usize) {
790 let mut unreadable = 0;
791 let mut all: Vec<Talk> = std::fs::read_dir(&self.root)
792 .into_iter()
793 .flatten()
794 .flatten()
795 .map(|e| e.path())
796 .filter(|p| p.extension().is_some_and(|x| x == "json"))
797 .filter_map(|p| {
798 let talk = read_path(&p).ok();
799 if talk.is_none() {
800 unreadable += 1;
801 }
802 talk
803 })
804 .collect();
805 all.sort_unstable_by(|a, b| {
806 let rank = |t: &Talk| u8::from(!t.status.open());
807 rank(a).cmp(&rank(b)).then_with(|| b.id.cmp(&a.id))
808 });
809 (all, unreadable)
810 }
811
812 pub fn resolve_id(&self, prefix: &str) -> Result<String> {
814 if self.path_of(prefix).is_file() {
815 return Ok(prefix.to_owned());
816 }
817 let hits: Vec<String> = self
818 .list()
819 .into_iter()
820 .map(|t| t.id)
821 .filter(|id| id.starts_with(prefix) || id.ends_with(prefix))
822 .collect();
823 match hits.len() {
824 1 => Ok(hits.into_iter().next().expect("exactly one hit")),
825 0 => bail!("no talk matches `{prefix}`"),
826 _ => bail!(
827 "`{prefix}` matches {} talks: {}",
828 hits.len(),
829 hits.join(", ")
830 ),
831 }
832 }
833
834 pub fn revision(&self) -> u64 {
837 std::fs::read_dir(&self.root)
838 .into_iter()
839 .flatten()
840 .flatten()
841 .filter(|e| e.path().extension().is_none_or(|x| x != "turn"))
842 .filter(|e| !e.file_name().to_string_lossy().starts_with('.'))
843 .filter_map(|e| e.metadata().ok())
844 .filter_map(|m| m.modified().ok())
845 .filter_map(|t| t.duration_since(std::time::UNIX_EPOCH).ok())
846 .map(|d| d.as_millis() as u64)
847 .max()
848 .unwrap_or(0)
849 }
850
851 pub fn count_open(&self) -> usize {
853 self.list().iter().filter(|t| t.status.open()).count()
854 }
855
856 pub fn turn_path(&self, id: &str) -> PathBuf {
859 self.root.join(format!("{id}.turn"))
860 }
861
862 pub fn claim_turn(&self, id: &str) -> Result<Option<TurnLease>> {
871 self.claim_turn_at(id, Timestamp::now())
872 }
873
874 fn claim_turn_at(&self, id: &str, now: Timestamp) -> Result<Option<TurnLease>> {
875 std::fs::create_dir_all(&self.root)
876 .with_context(|| format!("create {}", self.root.display()))?;
877 let path = self.turn_path(id);
878 let token = fresh_token();
879 if create_turn(&path, &token, now)? {
880 return Ok(Some(TurnLease {
881 talk: id.to_owned(),
882 path,
883 token,
884 }));
885 }
886 if lease_blocks(&path, now) {
887 return Ok(None);
888 }
889 let Some(_lock) = TurnLock::take(&path)? else {
894 return Ok(None);
895 };
896 if lease_blocks(&path, now) {
897 return Ok(None);
898 }
899 let _ = std::fs::remove_file(&path);
900 Ok(create_turn(&path, &token, now)?.then_some(TurnLease {
901 talk: id.to_owned(),
902 path,
903 token,
904 }))
905 }
906
907 pub fn turn_held(&self, id: &str) -> bool {
909 read_turn(&self.turn_path(id)).is_some_and(|r| r.fresh(Timestamp::now()))
910 }
911
912 pub fn remove(&self, id: &str) -> Result<()> {
925 let _guard = self.guard()?;
926 let resolved = self.resolve_id(id)?;
927 let path = self.path_of(&resolved);
928 std::fs::remove_file(&path).with_context(|| format!("remove {}", path.display()))?;
929 let artifacts = self.artifacts_of(&resolved);
930 if artifacts.is_dir() {
931 std::fs::remove_dir_all(&artifacts)
932 .with_context(|| format!("remove {}", artifacts.display()))?;
933 }
934 let _ = std::fs::remove_file(self.turn_path(&resolved));
935 Ok(())
936 }
937}
938
939struct StoreGuard<'a> {
942 _file: TurnLock,
943 _mutex: MutexGuard<'a, ()>,
944}
945
946#[derive(Debug, Serialize, Deserialize)]
949struct TurnRecord {
950 token: String,
951 pid: u32,
952 beat_at: Timestamp,
953}
954
955impl TurnRecord {
956 fn fresh(&self, now: Timestamp) -> bool {
957 now.as_second() - self.beat_at.as_second() <= crate::ask::LEASE_TTL.as_secs() as i64
958 }
959}
960
961fn fresh_token() -> String {
965 static SEQ: std::sync::atomic::AtomicU64 = std::sync::atomic::AtomicU64::new(0);
966 let n = SEQ.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
967 let seed = crate::rng::entropy() ^ n.wrapping_mul(0x9E37_79B9_7F4A_7C15);
968 crate::rng::SplitMix64::new(seed).uuid_v4()
969}
970
971fn read_turn(path: &Path) -> Option<TurnRecord> {
972 serde_json::from_str(&std::fs::read_to_string(path).ok()?).ok()
973}
974
975const TAKEOVER_LOCK_TTL: Duration = Duration::from_secs(10);
977
978const TICKET_TTL: Duration = Duration::from_secs(10);
981
982const TICKET_GENERATIONS: u32 = 16;
984
985const TICKET_SWEEP_AGE: Duration = Duration::from_secs(3600);
987
988fn create_exclusive(path: &Path, body: &str) -> Result<bool> {
990 use std::io::Write as _;
991 match std::fs::OpenOptions::new()
992 .write(true)
993 .create_new(true)
994 .open(path)
995 {
996 Ok(mut f) => {
997 if let Err(e) = f.write_all(body.as_bytes()) {
998 drop(f);
999 let _ = std::fs::remove_file(path);
1000 return Err(e).with_context(|| format!("write {}", path.display()));
1001 }
1002 Ok(true)
1003 }
1004 Err(e) if e.kind() == std::io::ErrorKind::AlreadyExists => Ok(false),
1005 Err(e) => Err(e).with_context(|| format!("create {}", path.display())),
1006 }
1007}
1008
1009fn create_turn(path: &Path, token: &str, now: Timestamp) -> Result<bool> {
1010 let record = TurnRecord {
1011 token: token.to_owned(),
1012 pid: std::process::id(),
1013 beat_at: now,
1014 };
1015 let body = serde_json::to_string(&record).context("serialize turn lease")?;
1016 let tmp = path.with_extension(format!("turn.{token}.new"));
1019 publish_exclusive(path, &tmp, &body)
1020}
1021
1022fn lease_blocks(path: &Path, now: Timestamp) -> bool {
1026 if read_turn(path).is_some_and(|r| r.fresh(now)) {
1027 return true;
1028 }
1029 std::fs::metadata(path).is_ok_and(|m| {
1030 m.len() == 0
1031 && m.modified()
1032 .ok()
1033 .and_then(|t| t.elapsed().ok())
1034 .is_some_and(|age| age <= TAKEOVER_LOCK_TTL)
1035 })
1036}
1037
1038fn link_unsupported(e: &std::io::Error) -> bool {
1041 e.kind() == std::io::ErrorKind::Unsupported || (cfg!(windows) && e.raw_os_error() == Some(1))
1042}
1043
1044fn create_in_place(path: &Path, body: &str) -> Result<bool> {
1062 use std::io::Write as _;
1063 let mut f = match std::fs::OpenOptions::new()
1064 .write(true)
1065 .create_new(true)
1066 .open(path)
1067 {
1068 Ok(f) => f,
1069 Err(e) if e.kind() == std::io::ErrorKind::AlreadyExists => return Ok(false),
1070 Err(e) => return Err(e).with_context(|| format!("create {}", path.display())),
1071 };
1072 f.write_all(body.as_bytes())
1073 .with_context(|| format!("write {}", path.display()))?;
1074 Ok(std::fs::read_to_string(path).is_ok_and(|t| t == body))
1077}
1078
1079fn publish_exclusive(path: &Path, tmp: &Path, body: &str) -> Result<bool> {
1080 std::fs::write(tmp, body).with_context(|| format!("write {}", tmp.display()))?;
1081 #[cfg(test)]
1082 let linked = if failpoint::no_link_forced() {
1083 Err(std::io::Error::from(std::io::ErrorKind::Unsupported))
1084 } else {
1085 std::fs::hard_link(tmp, path)
1086 };
1087 #[cfg(not(test))]
1088 let linked = std::fs::hard_link(tmp, path);
1089 let out = match linked {
1090 Ok(()) => Ok(true),
1091 Err(e) if e.kind() == std::io::ErrorKind::AlreadyExists => Ok(false),
1092 Err(e) if link_unsupported(&e) => create_in_place(path, body),
1093 Err(e) => Err(e).with_context(|| format!("create {}", path.display())),
1094 };
1095 let _ = std::fs::remove_file(tmp);
1096 out
1097}
1098
1099fn remove_if_carries(path: &Path, expected: &str) -> bool {
1110 let gone = path.with_extension(format!("gone.{}", fresh_token()));
1111 if std::fs::rename(path, &gone).is_err() {
1112 return false;
1113 }
1114 let found = std::fs::read_to_string(&gone);
1115 if found.as_ref().is_ok_and(|t| t == expected) {
1116 let _ = std::fs::remove_file(&gone);
1117 return true;
1118 }
1119 if let Ok(body) = found {
1120 let back = path.with_extension(format!("back.{}", fresh_token()));
1121 match publish_exclusive(path, &back, &body) {
1122 Ok(true) => {}
1123 Ok(false) | Err(_) => tracing::warn!(
1124 "{} was replaced while a stale removal had it aside; \
1125 the displaced file is dropped",
1126 path.display()
1127 ),
1128 }
1129 }
1130 let _ = std::fs::remove_file(&gone);
1131 false
1132}
1133
1134struct TurnLock {
1138 path: PathBuf,
1139 token: String,
1140}
1141
1142impl TurnLock {
1143 fn token() -> String {
1144 fresh_token()
1145 }
1146
1147 fn publish(path: &Path, token: &str) -> Result<bool> {
1151 let tmp = path.with_extension(format!("lock.{token}.new"));
1152 publish_exclusive(path, &tmp, token)
1153 }
1154
1155 fn take(lease: &Path) -> Result<Option<Self>> {
1156 let path = lease.with_extension("turn.lock");
1157 let token = Self::token();
1158 if Self::publish(&path, &token)? {
1159 return Ok(Some(Self { path, token }));
1160 }
1161 let Ok(seen) = std::fs::read_to_string(&path) else {
1162 return Ok(None);
1163 };
1164 let key = if !seen.is_empty() && seen.chars().all(|c| c.is_ascii_alphanumeric() || c == '-')
1167 {
1168 seen.as_str()
1169 } else {
1170 "invalid"
1171 };
1172 let aged = std::fs::metadata(&path)
1173 .and_then(|m| m.modified())
1174 .ok()
1175 .and_then(|t| t.elapsed().ok())
1176 .is_some_and(|age| age > TAKEOVER_LOCK_TTL);
1177 if !aged {
1178 return Ok(None);
1179 }
1180 let mut won = false;
1192 for n in 0..TICKET_GENERATIONS {
1193 let ticket = path.with_extension(format!("lock.{key}.break.{n}"));
1194 if create_exclusive(&ticket, "")? {
1195 won = true;
1196 break;
1197 }
1198 let stale = std::fs::metadata(&ticket)
1199 .and_then(|m| m.modified())
1200 .ok()
1201 .and_then(|t| t.elapsed().ok())
1202 .is_some_and(|age| age > TICKET_TTL);
1203 if !stale {
1204 return Ok(None);
1205 }
1206 }
1207 if !won {
1208 Self::sweep_tickets(&path);
1213 return Ok(None);
1214 }
1215 Self::sweep_tickets(&path);
1216 if !remove_if_carries(&path, &seen) {
1222 return Ok(None);
1223 }
1224 if Self::publish(&path, &token)? {
1225 return Ok(Some(Self { path, token }));
1226 }
1227 Ok(None)
1228 }
1229
1230 fn sweep_tickets(path: &Path) {
1233 let (Some(dir), Some(name)) = (path.parent(), path.file_name().and_then(|n| n.to_str()))
1234 else {
1235 return;
1236 };
1237 let prefix = format!("{name}.");
1238 let Ok(entries) = std::fs::read_dir(dir) else {
1239 return;
1240 };
1241 for entry in entries.flatten() {
1242 let file = entry.file_name();
1243 let Some(file) = file.to_str() else { continue };
1244 if !(file.starts_with(&prefix) && file.contains(".break.")) {
1245 continue;
1246 }
1247 let old = entry
1248 .metadata()
1249 .and_then(|m| m.modified())
1250 .ok()
1251 .and_then(|t| t.elapsed().ok())
1252 .is_some_and(|age| age > TICKET_SWEEP_AGE);
1253 if old {
1254 let _ = std::fs::remove_file(entry.path());
1255 }
1256 }
1257 }
1258
1259 fn take_patiently(lease: &Path) -> Option<Self> {
1261 for _ in 0..50 {
1262 match Self::take(lease) {
1263 Ok(Some(lock)) => return Some(lock),
1264 Ok(None) => std::thread::sleep(Duration::from_millis(10)),
1265 Err(_) => return None,
1266 }
1267 }
1268 None
1269 }
1270}
1271
1272impl Drop for TurnLock {
1273 fn drop(&mut self) {
1274 remove_if_carries(&self.path, &self.token);
1276 }
1277}
1278
1279pub const TURN_BEAT: Duration = Duration::from_secs(20);
1282
1283#[derive(Debug)]
1288pub struct TurnLease {
1289 talk: String,
1290 path: PathBuf,
1291 token: String,
1292}
1293
1294impl TurnLease {
1295 pub fn holds(&self) -> bool {
1297 read_turn(&self.path).is_some_and(|r| r.token == self.token)
1298 }
1299
1300 pub fn beat(&self) -> Result<bool> {
1304 let _lock = TurnLock::take_patiently(&self.path)
1305 .with_context(|| format!("lock {} to renew it", self.path.display()))?;
1306 let Some(mut record) = read_turn(&self.path).filter(|r| r.token == self.token) else {
1307 return Ok(false);
1308 };
1309 record.beat_at = Timestamp::now();
1310 let body = serde_json::to_string(&record).context("serialize turn lease")?;
1311 let tmp = self.path.with_extension(format!("turn.{}.tmp", self.token));
1312 write_atomic(&tmp, &self.path, &body)?;
1313 Ok(true)
1314 }
1315
1316 pub async fn beating<T>(&self, fut: impl std::future::Future<Output = T>) -> Result<T> {
1321 self.beating_every(TURN_BEAT, fut).await
1322 }
1323
1324 async fn beating_every<T>(
1325 &self,
1326 period: Duration,
1327 fut: impl std::future::Future<Output = T>,
1328 ) -> Result<T> {
1329 tokio::pin!(fut);
1330 loop {
1331 match tokio::time::timeout(period, &mut fut).await {
1332 Ok(out) => return Ok(out),
1333 Err(_) => match self.beat() {
1334 Ok(true) => {}
1335 Ok(false) => bail!(
1336 "the turn lease {} was taken over; this turn is stopped",
1337 self.path.display()
1338 ),
1339 Err(e) => tracing::warn!("{e:#}"),
1340 },
1341 }
1342 }
1343 }
1344}
1345
1346impl Drop for TurnLease {
1347 fn drop(&mut self) {
1348 if let Some(_lock) = TurnLock::take_patiently(&self.path) {
1351 if read_turn(&self.path).is_some_and(|r| r.token == self.token) {
1352 let _ = std::fs::remove_file(&self.path);
1353 }
1354 }
1355 }
1356}
1357
1358pub fn begin(store: &Talks, cfg: &Config, repo: PathBuf, agent: Option<&str>) -> Result<Talk> {
1368 begin_with(store, cfg, repo, agent, &Preferred::default())
1369}
1370
1371#[derive(Debug, Default, Clone)]
1375pub struct Preferred {
1376 pub agent: Option<String>,
1378 pub persona: Option<String>,
1380 pub implementers: Option<i64>,
1382}
1383
1384pub fn begin_with(
1391 store: &Talks,
1392 cfg: &Config,
1393 repo: PathBuf,
1394 agent: Option<&str>,
1395 preferred: &Preferred,
1396) -> Result<Talk> {
1397 let repo = repo.canonicalize().unwrap_or(repo);
1400 let head = || -> Result<AgentSpec> {
1403 Ok(agent::pick_chain(
1404 &cfg.agents,
1405 cfg.roles.chatter.as_ref(),
1406 &agent::installed,
1407 "chatter",
1408 )?
1409 .remove(0))
1410 };
1411 let mut fallback = agent.is_none();
1412 let spec = match agent {
1413 Some(id) => agent::pick(&cfg.agents, Some(id), &agent::installed)?,
1414 None => {
1415 match preferred
1416 .agent
1417 .as_deref()
1418 .map(str::trim)
1419 .filter(|id| !id.is_empty())
1420 {
1421 Some(id) => match agent::pick(&cfg.agents, Some(id), &agent::installed) {
1422 Ok(spec) => {
1423 fallback = head().is_ok_and(|head| spec.id == head.id);
1424 spec
1425 }
1426 Err(_) => head()?,
1427 },
1428 None => head()?,
1429 }
1430 }
1431 };
1432 let persona = preferred
1433 .persona
1434 .as_deref()
1435 .and_then(|id| crate::persona::find(&cfg.talk.personas, id))
1436 .filter(|p| !p.is_default())
1437 .map(|p| p.id)
1438 .unwrap_or_default();
1439 let implementers = preferred
1440 .implementers
1441 .and_then(|n| u8::try_from(n).ok())
1442 .and_then(|n| check_implementers(n, cfg).ok())
1443 .unwrap_or(1);
1444
1445 let now = Timestamp::now();
1446 let mut talk = Talk {
1447 posts_read_through: Some(0),
1448 pending_breaks: None,
1449 pending_messages: Vec::new(),
1450 schema: SCHEMA,
1451 id: new_id(),
1452 repo,
1453 agent: spec.id.clone(),
1454 status: TalkStatus::Open,
1455 turns: Vec::new(),
1456 pending: String::new(),
1457 pending_attachments: Vec::new(),
1458 fallback,
1459 persona,
1460 persona_dirty: false,
1461 implementers,
1462 implementers_dirty: false,
1463 notified: Vec::new(),
1464 completion_notices: Vec::new(),
1465 created_at: now,
1466 updated_at: now,
1467 seat: SeatState::new(SEAT, &spec.id, crate::rng::entropy()),
1468 };
1469 store.put(&mut talk)?;
1470 Ok(talk)
1471}
1472
1473pub fn record(
1480 talk: &mut Talk,
1481 store: &Talks,
1482 text: &str,
1483 attachments: Vec<Attachment>,
1484) -> Result<String> {
1485 record_reply(talk, store, text, attachments, None)
1486}
1487
1488pub fn record_reply(
1490 talk: &mut Talk,
1491 store: &Talks,
1492 text: &str,
1493 attachments: Vec<Attachment>,
1494 target: Option<ReplyTarget>,
1495) -> Result<String> {
1496 let _guard = store.guard()?;
1504 let Ok(fresh) = store.get(&talk.id) else {
1509 bail!("talk {} was deleted", talk.short());
1510 };
1511 let reply = resolve_reply(&fresh, target)?;
1512 talk.turns = fresh.turns;
1513 talk.status = fresh.status;
1514 talk.pending = fresh.pending;
1517 talk.pending_breaks = fresh.pending_breaks;
1518 talk.pending_messages = fresh.pending_messages;
1519 talk.pending_attachments = fresh.pending_attachments;
1520 talk.notified = fresh.notified;
1521 if !talk.status.open() {
1522 bail!(
1523 "talk {} is {} and takes no more turns",
1524 talk.short(),
1525 talk.status.as_str()
1526 );
1527 }
1528 if text.trim().is_empty() && attachments.is_empty() {
1530 bail!("nothing to say");
1531 }
1532 talk.turns.push(Turn {
1533 sender: Some(Sender::Operator),
1534 who: Who::Operator,
1535 body: text.to_owned(),
1536 at: Timestamp::now(),
1537 messages: vec![Message {
1538 sender: Some(Sender::Operator),
1539 body: text.to_owned(),
1540 attachments: attachments.clone(),
1541 reply,
1542 }],
1543 attachments,
1544 usage: None,
1545 breaks: Some(Vec::new()),
1546 posted_by: None,
1547 });
1548 store.put(talk)?;
1549 Ok(text.to_owned())
1550}
1551
1552pub fn queue(
1554 talk: &mut Talk,
1555 store: &Talks,
1556 text: &str,
1557 attachments: Vec<Attachment>,
1558) -> Result<()> {
1559 queue_reply(talk, store, text, attachments, None)
1560}
1561
1562pub fn queue_reply(
1564 talk: &mut Talk,
1565 store: &Talks,
1566 text: &str,
1567 attachments: Vec<Attachment>,
1568 target: Option<ReplyTarget>,
1569) -> Result<()> {
1570 queue_keyed(
1571 talk,
1572 store,
1573 text,
1574 attachments,
1575 None,
1576 &|_| false,
1577 (target, Sender::Operator),
1578 )
1579 .map(|_| ())
1580}
1581
1582pub fn queue_once(talk: &mut Talk, store: &Talks, text: &str, key: &str) -> Result<bool> {
1587 queue_once_matching(talk, store, text, key, &|_| false, None)
1588}
1589
1590pub(crate) fn queue_once_matching(
1593 talk: &mut Talk,
1594 store: &Talks,
1595 text: &str,
1596 key: &str,
1597 previous: &dyn Fn(&str) -> bool,
1598 reference: Option<&crate::chat_notify::CompletionReference>,
1599) -> Result<bool> {
1600 queue_keyed(
1601 talk,
1602 store,
1603 text,
1604 Vec::new(),
1605 Some((key, reference)),
1606 previous,
1607 (None, Sender::Magi),
1608 )
1609}
1610
1611pub fn queue_magi(talk: &mut Talk, store: &Talks, text: &str) -> Result<()> {
1613 queue_keyed(
1614 talk,
1615 store,
1616 text,
1617 Vec::new(),
1618 None,
1619 &|_| false,
1620 (None, Sender::Magi),
1621 )
1622 .map(|_| ())
1623}
1624
1625fn queue_keyed(
1626 talk: &mut Talk,
1627 store: &Talks,
1628 text: &str,
1629 attachments: Vec<Attachment>,
1630 key: Option<(&str, Option<&crate::chat_notify::CompletionReference>)>,
1631 previous: &dyn Fn(&str) -> bool,
1632 source: (Option<ReplyTarget>, Sender),
1633) -> Result<bool> {
1634 if text.trim().is_empty() && attachments.is_empty() {
1636 bail!("nothing to say");
1637 }
1638 let _guard = store.guard()?;
1639 let mut fresh = store
1640 .get(&talk.id)
1641 .with_context(|| format!("talk {} was deleted", talk.short()))?;
1642 if !fresh.status.open() {
1643 bail!(
1644 "talk {} is {} and takes no more turns",
1645 fresh.short(),
1646 fresh.status.as_str()
1647 );
1648 }
1649 if let Some((key, reference)) = key {
1650 if fresh.notified.iter().any(|k| k == key || previous(k)) {
1651 *talk = fresh;
1652 return Ok(false);
1653 }
1654 fresh.notified.push(key.to_owned());
1655 if let Some(reference) = reference {
1656 fresh.completion_notices.push(reference.clone());
1657 }
1658 }
1659 let (target, provenance) = source;
1660 let reply = resolve_reply(&fresh, target)?;
1661 if fresh.pending_messages.is_empty()
1662 && (!fresh.pending.is_empty() || !fresh.pending_attachments.is_empty())
1663 {
1664 fresh.pending_messages.push(Message {
1665 sender: None,
1666 body: fresh.pending.clone(),
1667 attachments: fresh.pending_attachments.clone(),
1668 reply: None,
1669 });
1670 }
1671 fresh.pending_messages.push(Message {
1672 sender: Some(provenance),
1673 body: text.to_owned(),
1674 attachments: attachments.clone(),
1675 reply,
1676 });
1677 if !text.is_empty() {
1678 if fresh.pending.is_empty() {
1679 fresh.pending = text.to_owned();
1680 fresh.pending_breaks = Some(Vec::new());
1681 } else {
1682 fresh.pending.push_str("\n\n");
1683 if let Some(b) = fresh.pending_breaks.as_mut() {
1684 b.push(fresh.pending.len());
1685 }
1686 fresh.pending.push_str(text);
1687 }
1688 }
1689 fresh.pending_attachments.extend(attachments);
1690 store.put(&mut fresh)?;
1691 *talk = fresh;
1692 Ok(true)
1693}
1694
1695pub fn post(store: &Talks, talk_id: &str, body: &str, by: &str) -> Result<()> {
1700 let body = body.trim();
1701 if body.is_empty() {
1702 bail!("nothing to post");
1703 }
1704 let _guard = store.guard()?;
1705 let mut fresh = store
1706 .get(talk_id)
1707 .with_context(|| format!("talk {talk_id} does not exist"))?;
1708 if !fresh.status.open() {
1709 bail!(
1710 "talk {} is closed and takes no more messages",
1711 fresh.short()
1712 );
1713 }
1714 fresh.turns.push(Turn {
1715 sender: Some(Sender::Magi),
1716 who: Who::Agent,
1717 body: body.to_owned(),
1718 at: Timestamp::now(),
1719 attachments: Vec::new(),
1720 usage: None,
1721 breaks: None,
1722 messages: Vec::new(),
1723 posted_by: Some(by.to_owned()),
1724 });
1725 store.put(&mut fresh)
1726}
1727
1728fn posts_read_through(talk: &Talk) -> usize {
1730 talk.posts_read_through.unwrap_or_else(|| {
1731 talk.turns
1732 .iter()
1733 .rposition(|t| t.who != Who::Operator && t.posted_by.is_none())
1734 .map_or(0, |i| i + 1)
1735 })
1736}
1737
1738pub fn posts_since_last_reply(talk: &Talk) -> Option<String> {
1740 let posts: Vec<&Turn> = talk
1741 .turns
1742 .iter()
1743 .skip(posts_read_through(talk))
1744 .filter(|t| t.posted_by.is_some())
1745 .collect();
1746 if posts.is_empty() {
1747 return None;
1748 }
1749 let mut out = String::from("# Posted by a task\n\n");
1750 for t in posts {
1751 out.push_str(&format!(
1752 "Task seat {} posted to this conversation: {}\n\n",
1753 short(t.posted_by.as_deref().unwrap_or_default()),
1754 t.body
1755 ));
1756 }
1757 Some(out)
1758}
1759
1760pub fn drain(talk: &mut Talk, store: &Talks) -> Result<Option<String>> {
1762 let _guard = store.guard()?;
1763 let mut fresh = store
1764 .get(&talk.id)
1765 .with_context(|| format!("talk {} was deleted", talk.short()))?;
1766 if !fresh.status.open() || (fresh.pending.is_empty() && fresh.pending_attachments.is_empty()) {
1767 *talk = fresh;
1768 return Ok(None);
1769 }
1770 let text = std::mem::take(&mut fresh.pending);
1771 let attachments = std::mem::take(&mut fresh.pending_attachments);
1772 let breaks = fresh.pending_breaks.take();
1773 let messages = std::mem::take(&mut fresh.pending_messages);
1774 fresh.turns.push(Turn {
1775 sender: Some(Sender::Operator),
1776 who: Who::Operator,
1777 body: text.clone(),
1778 at: Timestamp::now(),
1779 attachments,
1780 usage: None,
1781 breaks,
1782 messages,
1783 posted_by: None,
1784 });
1785 store.put(&mut fresh)?;
1786 *talk = fresh;
1787 Ok(Some(text))
1788}
1789
1790pub async fn say(
1793 lease: &TurnLease,
1794 talk: &mut Talk,
1795 store: &Talks,
1796 cfg: &Config,
1797 text: &str,
1798 attachments: Vec<Attachment>,
1799) -> Result<()> {
1800 check_lease(lease, talk)?;
1801 let text = record(talk, store, text, attachments)?;
1802 respond(lease, talk, store, cfg, &text).await
1803}
1804
1805pub async fn respond(
1811 lease: &TurnLease,
1812 talk: &mut Talk,
1813 store: &Talks,
1814 cfg: &Config,
1815 text: &str,
1816) -> Result<()> {
1817 check_lease(lease, talk)?;
1818 lease
1819 .beating(turn(talk, store, cfg, text))
1820 .await
1821 .and_then(|done| done)
1822}
1823
1824fn check_lease(lease: &TurnLease, talk: &Talk) -> Result<()> {
1825 if lease.talk != talk.id {
1826 bail!("the turn lease is for talk {}, not {}", lease.talk, talk.id);
1827 }
1828 if !lease.holds() {
1831 bail!("the turn lease for talk {} is no longer held", talk.short());
1832 }
1833 Ok(())
1834}
1835
1836pub fn close(talk: &mut Talk, store: &Talks) -> Result<()> {
1854 let _guard = store.guard()?;
1855 let mut fresh = store
1856 .get(&talk.id)
1857 .with_context(|| format!("talk {} was deleted", talk.short()))?;
1858 fresh.status = TalkStatus::Closed;
1859 fresh.pending.clear();
1861 fresh.pending_breaks = None;
1862 fresh.pending_messages.clear();
1863 fresh.pending_attachments.clear();
1864 store.put(&mut fresh)?;
1865 *talk = fresh;
1866 Ok(())
1867}
1868
1869pub fn reopen(talk: &mut Talk, store: &Talks) -> Result<()> {
1880 let _guard = store.guard()?;
1881 let mut fresh = store
1882 .get(&talk.id)
1883 .with_context(|| format!("talk {} was deleted", talk.short()))?;
1884 fresh.status = TalkStatus::Open;
1885 store.put(&mut fresh)?;
1886 *talk = fresh;
1887 Ok(())
1888}
1889
1890pub fn switch_agent(talk: &mut Talk, store: &Talks, spec: &AgentSpec) -> Result<bool> {
1903 let _guard = store.guard()?;
1904 let mut fresh = store
1905 .get(&talk.id)
1906 .with_context(|| format!("talk {} was deleted", talk.short()))?;
1907 if fresh.agent == spec.id {
1908 *talk = fresh;
1909 return Ok(false);
1910 }
1911 let from = std::mem::replace(&mut fresh.agent, spec.id.clone());
1912 fresh.seat = SeatState::new(SEAT, &spec.id, crate::rng::entropy());
1913 fresh.fallback = false;
1915 fresh.turns.push(Turn {
1916 sender: Some(Sender::Magi),
1917 breaks: None,
1918 messages: Vec::new(),
1919 posted_by: None,
1920 who: Who::Agent,
1921 body: format!("{MAGI_NOTE}agent changed from {from} to {}", spec.id),
1922 at: Timestamp::now(),
1923 attachments: Vec::new(),
1924 usage: None,
1925 });
1926 store.put(&mut fresh)?;
1927 *talk = fresh;
1928 Ok(true)
1929}
1930
1931pub fn switch_persona(talk: &mut Talk, store: &Talks, id: &str) -> Result<bool> {
1939 let id = if id.trim() == crate::persona::DEFAULT_ID {
1940 ""
1941 } else {
1942 id.trim()
1943 };
1944 let _guard = store.guard()?;
1945 let mut fresh = store
1946 .get(&talk.id)
1947 .with_context(|| format!("talk {} was deleted", talk.short()))?;
1948 if fresh.persona == id {
1949 *talk = fresh;
1950 return Ok(false);
1951 }
1952 let from = std::mem::replace(&mut fresh.persona, id.to_owned());
1953 fresh.persona_dirty = true;
1954 let label = |p: &str| {
1955 if p.is_empty() {
1956 crate::persona::DEFAULT_ID.to_owned()
1957 } else {
1958 p.to_owned()
1959 }
1960 };
1961 fresh.turns.push(Turn {
1962 sender: Some(Sender::Magi),
1963 breaks: None,
1964 messages: Vec::new(),
1965 posted_by: None,
1966 who: Who::Agent,
1967 body: format!(
1968 "{MAGI_NOTE}persona changed from {} to {}",
1969 label(&from),
1970 label(id)
1971 ),
1972 at: Timestamp::now(),
1973 attachments: Vec::new(),
1974 usage: None,
1975 });
1976 store.put(&mut fresh)?;
1977 *talk = fresh;
1978 Ok(true)
1979}
1980
1981pub fn clear_pending(talk: &mut Talk, store: &Talks) -> Result<()> {
1983 let _guard = store.guard()?;
1984 let mut fresh = store
1985 .get(&talk.id)
1986 .with_context(|| format!("talk {} was deleted", talk.short()))?;
1987 fresh.pending.clear();
1988 fresh.pending_breaks = None;
1989 fresh.pending_messages.clear();
1990 fresh.pending_attachments.clear();
1991 store.put(&mut fresh)?;
1992 *talk = fresh;
1993 Ok(())
1994}
1995
1996pub fn clear_pending_if_matches(
1998 talk: &mut Talk,
1999 store: &Talks,
2000 expected_text: &str,
2001 expected_attachments: &[String],
2002) -> Result<bool> {
2003 let _guard = store.guard()?;
2004 let mut fresh = store
2005 .get(&talk.id)
2006 .with_context(|| format!("talk {} was deleted", talk.short()))?;
2007 if !pending_matches(&fresh, expected_text, expected_attachments) {
2008 *talk = fresh;
2009 return Ok(false);
2010 }
2011 fresh.pending.clear();
2012 fresh.pending_breaks = None;
2013 fresh.pending_messages.clear();
2014 fresh.pending_attachments.clear();
2015 store.put(&mut fresh)?;
2016 *talk = fresh;
2017 Ok(true)
2018}
2019
2020pub fn edit_pending_text(
2024 talk: &mut Talk,
2025 store: &Talks,
2026 text: &str,
2027 expected_text: &str,
2028 expected_attachments: &[String],
2029) -> Result<bool> {
2030 edit_pending_message(talk, store, text, expected_text, expected_attachments, None)
2031}
2032
2033pub fn edit_pending_message(
2035 talk: &mut Talk,
2036 store: &Talks,
2037 text: &str,
2038 expected_text: &str,
2039 expected_attachments: &[String],
2040 message: Option<usize>,
2041) -> Result<bool> {
2042 let _guard = store.guard()?;
2043 let mut fresh = store
2044 .get(&talk.id)
2045 .with_context(|| format!("talk {} was deleted", talk.short()))?;
2046 if !pending_matches(&fresh, expected_text, expected_attachments) {
2047 *talk = fresh;
2048 return Ok(false);
2049 }
2050 if fresh
2051 .pending_messages
2052 .iter()
2053 .enumerate()
2054 .any(|(i, m)| m.sender == Some(Sender::Magi) && message.is_none_or(|index| index == i))
2055 {
2056 bail!("MAGI messages cannot be edited");
2057 }
2058 if let Some(index) = message {
2059 let edited = fresh
2060 .pending_messages
2061 .get_mut(index)
2062 .context("invalid queued message")?;
2063 if text.trim().is_empty() && edited.attachments.is_empty() {
2064 fresh.pending_messages.remove(index);
2065 } else {
2066 edited.body = text.to_owned();
2067 }
2068 fresh.pending.clear();
2069 fresh.pending_breaks = Some(Vec::new());
2070 fresh.pending_attachments.clear();
2071 for message in &fresh.pending_messages {
2072 if !message.body.is_empty() {
2073 if !fresh.pending.is_empty() {
2074 fresh.pending.push_str("\n\n");
2075 fresh
2076 .pending_breaks
2077 .as_mut()
2078 .expect("initialized")
2079 .push(fresh.pending.len());
2080 }
2081 fresh.pending.push_str(&message.body);
2082 }
2083 fresh
2084 .pending_attachments
2085 .extend(message.attachments.clone());
2086 }
2087 } else {
2088 if fresh.pending_messages.len() > 1
2089 && fresh.pending_messages.iter().any(|m| m.reply.is_some())
2090 {
2091 bail!("edit queued replies individually");
2092 }
2093 let reply = fresh.pending_messages.first().and_then(|m| m.reply.clone());
2094 fresh.pending_messages = vec![Message {
2095 sender: Some(Sender::Operator),
2096 body: text.to_owned(),
2097 attachments: fresh.pending_attachments.clone(),
2098 reply,
2099 }];
2100 fresh.pending = text.to_owned();
2101 fresh.pending_breaks = Some(Vec::new());
2102 }
2103 store.put(&mut fresh)?;
2104 *talk = fresh;
2105 Ok(true)
2106}
2107
2108fn pending_matches(talk: &Talk, expected_text: &str, expected_attachments: &[String]) -> bool {
2109 talk.pending == expected_text
2110 && talk
2111 .pending_attachments
2112 .iter()
2113 .map(|attachment| &attachment.id)
2114 .eq(expected_attachments.iter())
2115}
2116
2117async fn turn(talk: &mut Talk, store: &Talks, cfg: &Config, text: &str) -> Result<()> {
2124 let text = talk
2128 .turns
2129 .last()
2130 .filter(|t| t.who == Who::Operator && t.body == text)
2131 .map(|turn| message_prompt(turn, talk, store))
2132 .unwrap_or_else(|| text.to_owned());
2133 let evidence = crate::chat_notify::response_evidence(talk).await;
2134 let text = format!("{text}{evidence}");
2135 let prompt_turns = talk.turns.len();
2136 talk.posts_read_through = Some(posts_read_through(talk));
2137 let spec = cfg
2138 .agents
2139 .iter()
2140 .find(|a| a.id == talk.agent)
2141 .with_context(|| {
2142 format!(
2143 "talk {} was opened with agent `{}`, which is no longer in \
2144 the roster; restore it in magi.toml or start a new \
2145 conversation",
2146 talk.short(),
2147 talk.agent
2148 )
2149 })?;
2150
2151 let last_note = attachment_note(
2155 store,
2156 &talk.id,
2157 talk.turns
2158 .last()
2159 .map_or(&[][..], |t| t.attachments.as_slice()),
2160 );
2161
2162 let attachment_paths: Vec<PathBuf> = talk
2168 .turns
2169 .iter()
2170 .flat_map(|t| t.attachments.iter())
2171 .filter_map(|a| store.attachment_path(&talk.id, a))
2172 .collect();
2173
2174 let questions =
2184 crate::run::try_home().map(|home| crate::ask::Questions::at(home.join("questions")));
2185 let consulted = questions
2186 .as_ref()
2187 .is_some_and(|q| crate::consult::pending_consults(q, &talk.id));
2188 let consult_roots: Vec<PathBuf> = match &questions {
2189 Some(q) if consulted => vec![q.root().to_path_buf()],
2190 _ => Vec::new(),
2191 };
2192
2193 let (allow_write, unsandboxed) = turn_access(cfg.talk.allow_write, consulted);
2194
2195 let artifacts = store.artifacts_of(&talk.id);
2196 let operator_turns = talk.turns.iter().filter(|t| t.who == Who::Operator).count();
2199 let stem = format!("turn-{}", operator_turns.max(1));
2200 let cache_dir = cfg.cache_dir();
2203
2204 let mut chain = vec![spec.clone()];
2207 if let Some(choice) = cfg.roles.chatter.as_ref()
2208 && talk.fallback
2209 {
2210 for id in choice.ids() {
2211 if id == talk.agent || chain.iter().any(|s| s.id == id) {
2212 continue;
2213 }
2214 match agent::pick(&cfg.agents, Some(id), &agent::installed) {
2215 Ok(s) => chain.push(s),
2216 Err(e) => tracing::warn!("[roles] chatter: skipping `{id}`: {e:#}"),
2217 }
2218 }
2219 }
2220
2221 let persona = crate::persona::active(&cfg.talk.personas, &talk.persona);
2222 let operator_name = cfg.talk.operator_name();
2223 let persona_update = if talk.persona_dirty {
2224 format!(
2225 "{}\n\n",
2226 crate::persona::update_block_for(persona.as_ref(), operator_name)
2227 )
2228 } else {
2229 String::new()
2230 };
2231
2232 let filing_update = if talk.implementers_dirty {
2235 format!("{}\n\n", filing_update_block(talk.implementers))
2236 } else {
2237 String::new()
2238 };
2239
2240 let mut outcome = None;
2241 let mut fell_back_from: Option<String> = None;
2242 let mut first_try: Option<(String, SeatState)> = None;
2245 for (n, spec) in chain.iter().enumerate() {
2246 if n > 0 {
2247 if first_try.is_none() {
2248 first_try = Some((talk.agent.clone(), talk.seat.clone()));
2249 }
2250 tracing::warn!("chat: falling back from `{}` to `{}`", talk.agent, spec.id);
2251 fell_back_from.get_or_insert_with(|| talk.agent.clone());
2254 talk.agent = spec.id.clone();
2255 talk.seat = SeatState::new(SEAT, &spec.id, crate::rng::entropy());
2256 }
2257 let resuming = agent::has_session(spec.kind, &talk.seat, cfg.graph.sessions);
2258 let first_ever = talk.turns.len() <= 1;
2259 let body = if talk.seat.turns == 0 && first_ever {
2260 format!(
2261 "{}\n\n# Operator\n\n{text}{last_note}",
2262 briefing_for(
2263 &talk.repo,
2264 &cfg.graph.language,
2265 cfg.talk.allow_write,
2266 persona.as_ref(),
2267 operator_name,
2268 talk.implementers
2269 )
2270 )
2271 } else if talk.seat.turns == 0 {
2272 format!(
2275 "{}\n\n{}\n\n# Operator\n\n{text}{last_note}",
2276 briefing_for(
2277 &talk.repo,
2278 &cfg.graph.language,
2279 cfg.talk.allow_write,
2280 persona.as_ref(),
2281 operator_name,
2282 talk.implementers
2283 ),
2284 transcript(talk, store)
2285 )
2286 } else if resuming {
2287 let posts = posts_since_last_reply(talk).unwrap_or_default();
2288 format!("{persona_update}{filing_update}{posts}{text}{last_note}")
2289 } else {
2290 let mut standing = match (&persona, talk.persona_dirty) {
2294 (Some(p), false) => format!("{}\n", crate::persona::section_for(p, operator_name)),
2295 _ => persona_update.clone(),
2296 };
2297 if let (None, Some(n)) = (&persona, operator_name) {
2300 standing.push_str(&format!(
2301 "# Addressing the operator\n{}\n",
2302 crate::persona::addressing(n)
2303 ));
2304 }
2305 if talk.implementers_dirty || talk.implementers != 1 {
2306 standing.push_str(&format!("{}\n\n", filing_update_block(talk.implementers)));
2307 }
2308 format!("{}\n\n{standing}{text}{last_note}", transcript(talk, store))
2309 };
2310 let current_approvals = questions
2313 .as_ref()
2314 .map(|store| {
2315 store
2316 .list()
2317 .iter()
2318 .filter(|q| {
2319 q.status.open()
2320 && q.node == crate::land::APPROVAL_NODE
2321 && q.consult.as_ref().is_some_and(|c| c.talk == talk.id)
2322 })
2323 .map(crate::prompt::chat_consult)
2324 .collect::<Vec<_>>()
2325 .join("\n\n")
2326 })
2327 .unwrap_or_default();
2328 let body = if current_approvals.is_empty() {
2329 body
2330 } else {
2331 format!("{current_approvals}\n\n# Current operator message\n\n{body}")
2332 };
2333 let attempt_stem = if n == 0 {
2334 stem.clone()
2335 } else {
2336 format!("{stem}-{}", spec.id)
2337 };
2338 let inv = Invocation {
2339 cwd: &talk.repo,
2340 prompt: &body,
2341 timeout: turn_timeout(cfg),
2342 allow_write,
2347 unsandboxed,
2348 sessions: cfg.graph.sessions,
2349 artifacts: &artifacts,
2350 stem: &attempt_stem,
2351 run: &talk.id,
2354 node: crate::queue::CHAT_NODE,
2355 cache_dir: cache_dir.as_deref(),
2356 attachments: &attachment_paths,
2357 writable: &consult_roots,
2358 };
2359 let result = agent::invoke(spec, &mut talk.seat, &inv).await;
2360 let advance = agent::chain_advances(&result);
2361 if n == 0 || !advance {
2362 outcome = Some(result);
2363 } else {
2364 tracing::warn!("chat: fallback agent `{}` also failed", spec.id);
2366 }
2367 if !advance {
2368 break;
2369 }
2370 }
2371 if outcome.as_ref().is_some_and(agent::chain_advances) {
2372 if let Some((id, seat)) = first_try {
2375 talk.agent = id;
2376 talk.seat = seat;
2377 fell_back_from = None;
2378 }
2379 }
2380 let outcome = outcome.expect("a chain holds at least one agent");
2381 let note = |why: String| Turn {
2382 sender: Some(Sender::Magi),
2383 breaks: None,
2384 messages: Vec::new(),
2385 posted_by: None,
2386 who: Who::Agent,
2387 body: format!("{MAGI_NOTE}{why}"),
2388 at: Timestamp::now(),
2389 attachments: Vec::new(),
2390 usage: None,
2391 };
2392 let (reply, failure) = match outcome {
2393 Err(e) => (
2394 note(format!("could not run agent `{}`: {e}", talk.agent)),
2395 Some(format!("could not run agent `{}`: {e}", talk.agent)),
2396 ),
2397 Ok(out) if out.quota_exhausted() => {
2398 let reset = out
2399 .quota
2400 .as_ref()
2401 .and_then(|q| q.reset.clone())
2402 .map_or_else(String::new, |r| format!(" (resets {r})"));
2403 let why = format!(
2404 "agent `{}` is out of quota{reset}; your message is saved, so \
2405 say it again when the window reopens",
2406 talk.agent
2407 );
2408 (note(why.clone()), Some(why))
2409 }
2410 Ok(out) if out.timed_out => {
2411 let why = format!(
2412 "agent `{}` did not answer within {}s; your message is saved",
2413 talk.agent,
2414 turn_timeout(cfg).as_secs()
2415 );
2416 (note(why.clone()), Some(why))
2417 }
2418 Ok(out) if !out.usable() => {
2419 let why = format!(
2420 "agent `{}` produced no answer (exit {}); your message is saved",
2421 talk.agent,
2422 out.exit_code
2423 .map_or_else(|| "unknown".to_owned(), |c| c.to_string())
2424 );
2425 (note(why.clone()), Some(why))
2426 }
2427 Ok(out) => (
2428 Turn {
2429 sender: Some(Sender::Persona),
2430 breaks: None,
2431 messages: Vec::new(),
2432 posted_by: None,
2433 who: Who::Agent,
2434 body: out.text.trim().to_owned(),
2435 at: Timestamp::now(),
2436 attachments: Vec::new(),
2437 usage: out.context_tokens.map(|context_tokens| TurnUsage {
2440 context_tokens,
2441 agent: talk.agent.clone(),
2442 model: cfg
2443 .agents
2444 .iter()
2445 .find(|a| a.id == talk.agent)
2446 .and_then(|a| a.model.clone()),
2447 }),
2448 },
2449 None,
2450 ),
2451 };
2452
2453 let _guard = store.guard()?;
2465 let Ok(fresh) = store.get(&talk.id) else {
2471 return Ok(());
2472 };
2473 talk.status = fresh.status;
2474 talk.pending = fresh.pending;
2478 talk.pending_breaks = fresh.pending_breaks;
2479 talk.pending_messages = fresh.pending_messages;
2480 talk.pending_attachments = fresh.pending_attachments;
2481 talk.notified = fresh.notified;
2482 if fresh.turns.len() > talk.turns.len() {
2484 let posted: Vec<Turn> = fresh.turns[talk.turns.len()..]
2485 .iter()
2486 .filter(|t| t.posted_by.is_some())
2487 .cloned()
2488 .collect();
2489 talk.turns.extend(posted);
2490 }
2491 if let Some(from) = fell_back_from.filter(|_| failure.is_none()) {
2492 talk.turns.push(note(format!(
2495 "agent changed from {from} to {} (fallback)",
2496 talk.agent
2497 )));
2498 }
2499 if failure.is_none() {
2502 talk.persona_dirty = false;
2503 talk.implementers_dirty = false;
2504 talk.posts_read_through = Some(prompt_turns);
2505 }
2506 talk.turns.push(reply);
2507 if let Err(put_err) = store.put(talk) {
2508 let lost = talk.turns.pop().expect("just pushed above");
2517 let stash = stash_lost_turn(store, &talk.id, &stem, &lost);
2518 let why = match &stash {
2519 Ok(path) => format!(
2520 "agent `{}` answered, but the reply could not be saved to \
2521 this conversation ({put_err:#}); the raw text was kept at \
2522 {} - your message is saved, ask again",
2523 talk.agent,
2524 path.display()
2525 ),
2526 Err(stash_err) => format!(
2527 "agent `{}` answered, but the reply could not be saved to \
2528 this conversation ({put_err:#}), and it could not be kept \
2529 anywhere else either ({stash_err:#}); your message is \
2530 saved, ask again",
2531 talk.agent
2532 ),
2533 };
2534 talk.turns.push(note(why.clone()));
2535 return match store.put(talk) {
2542 Ok(()) => bail!("{why}"),
2543 Err(note_err) => {
2544 talk.turns.pop();
2564 Err(note_err).context(why)
2565 }
2566 };
2567 }
2568
2569 match failure {
2570 Some(why) => bail!("{why}"),
2571 None => Ok(()),
2572 }
2573}
2574
2575fn transcript(talk: &Talk, store: &Talks) -> String {
2578 let mut out = String::from(
2579 "This conversation cannot resume on the CLI's side, so here is \
2580 everything said so far; answer only the last message.\n",
2581 );
2582 for t in &talk.turns {
2583 let who = match t.who {
2584 Who::Operator => "operator",
2585 Who::Agent if t.body.starts_with(MAGI_NOTE) => "magi",
2586 Who::Agent => "you",
2587 };
2588 out.push_str(&format!(
2589 "\n## {who}\n\n{}\n",
2590 message_prompt(t, talk, store).trim()
2591 ));
2592 out.push_str(&attachment_note(store, &talk.id, &t.attachments));
2593 }
2594 out
2595}
2596
2597fn attachment_note(store: &Talks, talk_id: &str, attachments: &[Attachment]) -> String {
2602 if attachments.is_empty() {
2603 return String::new();
2604 }
2605 let mut out = String::from(
2606 "\n\nThe operator attached the image(s) below to this message. Open \
2607 and look at each one before you answer.\n",
2608 );
2609 for att in attachments {
2610 if let Some(path) = store.attachment_path(talk_id, att) {
2611 out.push_str(&format!("\n- {} ({})", path.display(), att.mime));
2612 }
2613 }
2614 out.push('\n');
2615 out
2616}
2617
2618pub(crate) fn turn_access(talk_allow_write: bool, consulted: bool) -> (bool, bool) {
2629 (talk_allow_write || consulted, talk_allow_write)
2630}
2631
2632pub fn briefing(repo: &Path, language: &str, allow_write: bool) -> String {
2648 briefing_with(repo, language, allow_write, None, None)
2649}
2650
2651pub fn briefing_with(
2656 repo: &Path,
2657 language: &str,
2658 allow_write: bool,
2659 persona: Option<&crate::persona::Persona>,
2660 operator_name: Option<&str>,
2661) -> String {
2662 briefing_for(repo, language, allow_write, persona, operator_name, 1)
2663}
2664
2665pub const MAX_IMPLEMENTERS: u8 = 3;
2667
2668fn solo_implementers() -> u8 {
2669 1
2670}
2671
2672fn filing_flag(n: u8) -> String {
2675 if n <= 1 {
2676 "--solo".to_owned()
2677 } else {
2678 format!("--implementers {n}")
2679 }
2680}
2681
2682pub fn filing_update_block(n: u8) -> String {
2685 let flag = filing_flag(n);
2686 if n <= 1 {
2687 format!(
2688 "# Task filing update\n\nThe operator changed how many implementers tasks use. \
2689 From now on file with `magi task add {flag} --repo <repo> <instruction>` \
2690 (one implementer, straight into review).\n"
2691 )
2692 } else {
2693 format!(
2694 "# Task filing update\n\nThe operator changed how many implementers tasks use. \
2695 From now on file with `magi task add {flag} --repo <repo> <instruction>` \
2696 ({n} independent implementations compete). Do not pass `--solo` \
2697 together with `--implementers`; drop any earlier `--solo`.\n"
2698 )
2699 }
2700}
2701
2702pub fn check_implementers(n: u8, cfg: &crate::config::Config) -> std::result::Result<u8, String> {
2705 if !(1..=MAX_IMPLEMENTERS).contains(&n) {
2706 return Err(format!(
2707 "implementers must be 1..={MAX_IMPLEMENTERS}, got {n}"
2708 ));
2709 }
2710 if n > 1 {
2711 let mut cfg = cfg.clone();
2712 crate::queue::RunOverrides {
2713 candidates: Some(usize::from(n)),
2714 ..Default::default()
2715 }
2716 .apply(&mut cfg);
2717 cfg.resolve_roles()
2718 .map_err(|e| format!("{n} implementers is not allowed here: {e:#}"))?;
2719 }
2720 Ok(n)
2721}
2722
2723pub fn switch_implementers(talk: &mut Talk, store: &Talks, n: u8) -> Result<bool> {
2726 let _guard = store.guard()?;
2727 let mut fresh = store
2728 .get(&talk.id)
2729 .with_context(|| format!("talk {} was deleted", talk.short()))?;
2730 if fresh.implementers == n {
2731 *talk = fresh;
2732 return Ok(false);
2733 }
2734 let from = std::mem::replace(&mut fresh.implementers, n);
2735 fresh.implementers_dirty = true;
2736 fresh.turns.push(Turn {
2737 sender: Some(Sender::Magi),
2738 breaks: None,
2739 messages: Vec::new(),
2740 posted_by: None,
2741 who: Who::Agent,
2742 body: format!("{MAGI_NOTE}implementers changed from {from} to {n}"),
2743 at: Timestamp::now(),
2744 attachments: Vec::new(),
2745 usage: None,
2746 });
2747 store.put(&mut fresh)?;
2748 *talk = fresh;
2749 Ok(true)
2750}
2751
2752pub fn briefing_for(
2755 repo: &Path,
2756 language: &str,
2757 allow_write: bool,
2758 persona: Option<&crate::persona::Persona>,
2759 operator_name: Option<&str>,
2760 implementers: u8,
2761) -> String {
2762 let flag = filing_flag(implementers);
2763 let shape = if implementers <= 1 {
2764 "Use --solo: it runs the task through one implementer \
2765 straight into review instead of the usual multi-agent competition, \
2766 which is the right shape for a change this conversation has already \
2767 settled, rather than one still worth several independent takes."
2768 .to_owned()
2769 } else {
2770 format!(
2771 "Use --implementers {implementers}: it has {implementers} \
2772 implementers work on the task independently and compete, which \
2773 is the right shape when several independent takes are worth \
2774 having. Never add --solo to it; the two do not go together."
2775 )
2776 };
2777 let write_policy = if allow_write {
2778 "Write access is enabled for this conversation (`allow_write = \
2779 true`), so you may write files - but only a small, \
2780 already-decided edit the operator names outright in this \
2781 conversation, not an implementation. This is a permission on the \
2782 conversation as a whole, not a property of whichever repository \
2783 it happened to start in: if the operator names a different \
2784 repository for that small edit, the policy allows it there too. \
2785 Your own tool may still confine writes to the repository this \
2786 conversation started in regardless - if a write elsewhere is \
2787 refused, say so plainly rather than working around it. Once you \
2788 have made an edit, say plainly what you edited. Anything bigger, \
2789 or anything still open-ended, still goes through the queue below \
2790 rather than being done here."
2791 } else {
2792 "Do not write files. Implementing a change is not this \
2793 conversation's job; a separate, blind competition of agents does \
2794 that, and a repository this conversation has already edited would \
2795 make their diffs unjudgeable."
2796 };
2797 let mut out = format!(
2798 "You are magi's standing conversation partner for its operator, who \
2799 usually has this open on a phone. Keep replies short: no preamble, \
2800 no restating what they just said.\n\n\
2801 # Repository\n\n{repo}\n\n\
2802 You may look around: read files, run shell commands, search history, \
2803 run tests - whatever answers the question. {write_policy}\n\n\
2804 A short, command-shaped message (\"list\", \"info <id>\", \"show \
2805 3cbf\") is almost always the operator asking you to look something \
2806 up, not an instruction to file - answer it yourself with `magi \
2807 list`, `magi show <id>`, `magi task list`, or the like, the same way \
2808 you would answer any other question in this conversation.\n\n\
2809 # When the operator wants something done\n\n\
2810 Run:\n\n\
2811 magi task add {flag} --repo {repo} <instruction>\n\n\
2812 and tell the operator the task id it prints, so they can follow it \
2813 from the Queue. If it refuses with a duplicate warning (the \
2814 instruction names a branch, commit or pull request that an \
2815 unfinished task, run or PR already owns), do not repeat it with \
2816 --force yourself: tell the operator what it matched and let them \
2817 decide. Write <instruction> so that an implementer who has \
2818 never seen this conversation can act on it alone - it is everything \
2819 they get. {shape}\n\n\
2820 If the operator asks for something in a different repository, \
2821 --repo does not have to be a full path: --repo owner/repo (or just \
2822 repo, when that is unambiguous) is resolved against local checkouts \
2823 the same way `magi repos` lists them. If the command fails because \
2824 nothing matches or more than one checkout shares that name, ask the \
2825 operator which repository they mean (or run `magi repos` yourself \
2826 to see the candidates) rather than guessing.\n\n\
2827 You cannot wait, and you cannot send the operator a message on your \
2828 own: you only speak when they write. A task you file reports back \
2829 into this conversation automatically when it finishes, is held or \
2830 fails, as a message headed \"{notice}\".\n\n\
2831 For a background command you run yourself, register it with \
2832 `magi talk watch --timeout 900 -- <executable> <arguments...>`, for \
2833 example `magi talk watch -- cargo test --locked`. It uses your \
2834 current directory; arguments are passed literally. For shell syntax, \
2835 explicitly call `sh -c`. Only after watch registration succeeds may \
2836 you promise a completion report. Watches default to 900 seconds, \
2837 accept at most 3600 seconds, and share four concurrent slots. Their \
2838 automatic reports resume this conversation through its own turn. \
2839 Never promise to report back on work that is neither a filed task \
2840 nor a successfully registered watch.\n\n\
2841 The current state of the code is whatever origin/main holds, not \
2842 whatever a working tree shows: a primary checkout often lags \
2843 upstream, sits on a detached HEAD and carries uncommitted changes. \
2844 Before answering about code, run `git fetch origin` in that \
2845 repository if it is cheap, then read through \
2846 `git show origin/main:<path>` or `git grep <pattern> origin/main`. \
2847 If the working tree differs, say so; if the fetch fails, say that \
2848 too, so the operator knows the answer may be stale.\n\n\
2849 If the operator attached an image (a screenshot, say) that the task \
2850 is about, pass it with `--attach <path>`, using the absolute path \
2851 the turn's attachment note gives; repeat the flag for several. \
2852 `magi task add {flag} --attach <path> <instruction>` copies the \
2853 file into the task, so the implementer receives it. Do not paste the \
2854 path into <instruction> instead: deleting this conversation deletes \
2855 its attachments, and then that path reaches no one.\n",
2856 repo = repo.display(),
2857 notice = crate::prompt::CHAT_TASK_NOTICE_HEADING,
2858 );
2859 out.push_str(&language_note(language));
2860 match (persona, operator_name) {
2861 (Some(p), n) => out.push_str(&crate::persona::section_for(p, n)),
2862 (None, Some(n)) => {
2863 out.push_str("\n# Addressing the operator\n");
2864 out.push_str(&crate::persona::addressing(n));
2865 }
2866 (None, None) => {}
2867 }
2868 out
2869}
2870
2871fn language_note(language: &str) -> String {
2874 if language.trim().is_empty() || language.eq_ignore_ascii_case("en") {
2875 String::new()
2876 } else {
2877 format!("\nHold this conversation in {language}.\n")
2878 }
2879}
2880
2881pub fn tasks_of(queue: &Queue, talk_id: &str) -> Vec<Task> {
2888 let mut tasks: Vec<Task> = queue
2889 .list()
2890 .into_iter()
2891 .filter(|t| matches!(&t.source, Source::Agent { run, .. } if run == talk_id))
2892 .collect();
2893 tasks.sort_unstable_by(|a, b| a.id.cmp(&b.id));
2894 tasks
2895}
2896
2897fn read_path(path: &Path) -> Result<Talk> {
2898 let body = std::fs::read_to_string(path).with_context(|| format!("read {}", path.display()))?;
2899 let mut talk: Talk =
2900 serde_json::from_str(&body).with_context(|| format!("parse {}", path.display()))?;
2901 for turn in &mut talk.turns {
2902 if turn.who == Who::Operator && turn.messages.is_empty() && turn.attachments.is_empty() {
2903 turn.messages = plain_messages(&turn.body, turn.breaks.as_deref());
2904 }
2905 }
2906 if talk.pending_messages.is_empty() && talk.pending_attachments.is_empty() {
2907 talk.pending_messages = plain_messages(&talk.pending, talk.pending_breaks.as_deref());
2908 }
2909 Ok(talk)
2910}
2911
2912fn plain_messages(body: &str, breaks: Option<&[usize]>) -> Vec<Message> {
2914 let Some(breaks) = breaks else {
2915 return Vec::new();
2916 };
2917 if body.is_empty() {
2918 return Vec::new();
2919 }
2920 let mut messages = Vec::new();
2921 let mut start = 0;
2922 for end in breaks
2923 .iter()
2924 .copied()
2925 .chain(std::iter::once(body.len() + 2))
2926 {
2927 let Some(text) = end.checked_sub(2).and_then(|end| body.get(start..end)) else {
2928 return Vec::new();
2929 };
2930 messages.push(Message {
2931 sender: None,
2932 body: text.to_owned(),
2933 attachments: Vec::new(),
2934 reply: None,
2935 });
2936 start = end;
2937 }
2938 messages
2939}
2940
2941const PUT_RETRIES: u32 = 5;
2944
2945fn write_atomic(tmp: &Path, path: &Path, body: &str) -> Result<()> {
2957 let mut last_err = None;
2958 for attempt in 0..PUT_RETRIES {
2959 if attempt > 0 {
2960 std::thread::sleep(Duration::from_millis(20 * u64::from(attempt)));
2961 }
2962 match try_write_atomic(tmp, path, body) {
2963 Ok(()) => return Ok(()),
2964 Err(e) => last_err = Some(e),
2965 }
2966 }
2967 Err(last_err.expect("the loop above always runs at least once"))
2968}
2969
2970fn try_write_atomic(tmp: &Path, path: &Path, body: &str) -> Result<()> {
2971 #[cfg(test)]
2972 if failpoint::take_forced_put_failure() {
2973 bail!("simulated write failure (test)");
2974 }
2975 std::fs::write(tmp, body).with_context(|| format!("write {}", tmp.display()))?;
2976 std::fs::rename(tmp, path).with_context(|| format!("replace {}", path.display()))?;
2977 Ok(())
2978}
2979
2980fn stash_lost_turn(store: &Talks, id: &str, stem: &str, reply: &Turn) -> Result<PathBuf> {
2985 let dir = store.artifacts_of(id);
2986 std::fs::create_dir_all(&dir).with_context(|| format!("create {}", dir.display()))?;
2987 let path = dir.join(format!("{stem}-lost.txt"));
2988 std::fs::write(&path, &reply.body).with_context(|| format!("write {}", path.display()))?;
2989 Ok(path)
2990}
2991
2992#[cfg(test)]
2999mod failpoint {
3000 use std::cell::Cell;
3001
3002 thread_local! {
3003 static FORCE_PUT_FAILURES: Cell<u32> = const { Cell::new(0) };
3004 static FORCE_NO_LINK: Cell<bool> = const { Cell::new(false) };
3005 }
3006
3007 pub(super) fn force_no_link(on: bool) {
3010 FORCE_NO_LINK.with(|c| c.set(on));
3011 }
3012
3013 pub(super) fn no_link_forced() -> bool {
3014 FORCE_NO_LINK.with(Cell::get)
3015 }
3016
3017 pub(super) fn force_put_failures(count: u32) {
3020 FORCE_PUT_FAILURES.with(|c| c.set(count));
3021 }
3022
3023 pub(super) fn take_forced_put_failure() -> bool {
3026 FORCE_PUT_FAILURES.with(|c| {
3027 let n = c.get();
3028 if n == 0 {
3029 false
3030 } else {
3031 c.set(n - 1);
3032 true
3033 }
3034 })
3035 }
3036}
3037
3038fn short(id: &str) -> &str {
3039 id.split('-').next_back().unwrap_or(id)
3040}
3041
3042fn new_id() -> String {
3043 let stamp = jiff::Zoned::now().strftime("%Y%m%d-%H%M%S");
3044 let seed = crate::rng::entropy();
3045 format!("{stamp}-{:04x}", (seed ^ (seed >> 32)) & 0xffff)
3046}
3047
3048fn attachment_ext(mime: &str) -> Option<&'static str> {
3053 match mime {
3054 "image/png" => Some("png"),
3055 "image/jpeg" => Some("jpg"),
3056 "image/gif" => Some("gif"),
3057 "image/webp" => Some("webp"),
3058 _ => None,
3059 }
3060}
3061
3062pub fn valid_attachment_id(id: &str) -> bool {
3067 id.len() == 32
3068 && id
3069 .bytes()
3070 .all(|b| b.is_ascii_digit() || (b'a'..=b'f').contains(&b))
3071}
3072
3073fn new_attachment_id() -> String {
3077 let mut r = crate::rng::SplitMix64::new(crate::rng::entropy());
3078 format!("{:016x}{:016x}", r.next_u64(), r.next_u64())
3079}
3080
3081#[cfg(test)]
3082mod tests {
3083 #[test]
3084 fn turn_access_lifts_the_sandbox_only_for_the_talk_opt_in() {
3085 use super::turn_access;
3086 assert_eq!(turn_access(false, false), (false, false));
3087 assert_eq!(turn_access(true, false), (true, true));
3088 assert_eq!(turn_access(false, true), (true, false));
3090 assert_eq!(turn_access(true, true), (true, true));
3091 }
3092
3093 #[test]
3094 fn the_briefing_points_at_origin_main_not_the_working_tree() {
3095 let b = briefing(Path::new("/r"), "en", false);
3096 assert!(b.contains("origin/main"));
3097 assert!(b.contains("git show origin/main:"));
3098 }
3099 use std::collections::BTreeMap;
3100
3101 use crate::config::{AgentChoice, AgentKind, AgentSpec, Graph};
3102 use crate::queue::{Queue, Source, Task};
3103
3104 use super::*;
3105
3106 fn ctx_agent(id: &str, model: Option<&str>) -> AgentSpec {
3107 AgentSpec {
3108 id: id.to_owned(),
3109 kind: AgentKind::Command,
3110 model: model.map(str::to_owned),
3111 command: Vec::new(),
3112 extra_args: Vec::new(),
3113 env: BTreeMap::new(),
3114 prompt_delivery: None,
3115 }
3116 }
3117
3118 fn ctx_talk(agent: &str, turns: Vec<Turn>) -> Talk {
3119 Talk {
3120 posts_read_through: None,
3121 pending_breaks: None,
3122 pending_messages: Vec::new(),
3123 schema: SCHEMA,
3124 id: "20260904-014455-ab12".to_owned(),
3125 repo: PathBuf::from("."),
3126 agent: agent.to_owned(),
3127 status: TalkStatus::Open,
3128 turns,
3129 pending: String::new(),
3130 pending_attachments: Vec::new(),
3131 fallback: false,
3132 persona: String::new(),
3133 persona_dirty: false,
3134 implementers: 1,
3135 implementers_dirty: false,
3136 notified: Vec::new(),
3137 completion_notices: Vec::new(),
3138 created_at: Timestamp::now(),
3139 updated_at: Timestamp::now(),
3140 seat: SeatState::new(SEAT, agent, 1),
3141 }
3142 }
3143
3144 fn reply(body: &str, usage: Option<(u64, &str, Option<&str>)>) -> Turn {
3145 Turn {
3146 sender: None,
3147 breaks: None,
3148 messages: Vec::new(),
3149 posted_by: None,
3150 who: Who::Agent,
3151 body: body.to_owned(),
3152 at: Timestamp::now(),
3153 attachments: Vec::new(),
3154 usage: usage.map(|(t, a, m)| TurnUsage {
3155 context_tokens: t,
3156 agent: a.to_owned(),
3157 model: m.map(str::to_owned),
3158 }),
3159 }
3160 }
3161
3162 fn ctx_config(windows: &[(&str, u64)]) -> Config {
3163 Config {
3164 agents: vec![
3165 ctx_agent("small", Some("small-model")),
3166 ctx_agent("big", Some("big-model")),
3167 ctx_agent("plain", None),
3168 ],
3169 context_windows: windows.iter().map(|(k, v)| ((*k).to_owned(), *v)).collect(),
3170 ..Config::default()
3171 }
3172 }
3173
3174 #[test]
3175 fn context_usage_computes_percent_and_warns_at_eighty() {
3176 let cfg = ctx_config(&[("small-model", 1000)]);
3177 let at = |tokens| {
3178 let t = ctx_talk(
3179 "small",
3180 vec![reply("hi", Some((tokens, "small", Some("small-model"))))],
3181 );
3182 context_usage(&t, Some(&cfg))
3183 };
3184 let u = at(799);
3185 assert_eq!((u.percent, u.warn, u.window), (Some(79), false, Some(1000)));
3186 let u = at(800);
3187 assert_eq!((u.percent, u.warn), (Some(80), true));
3188 let u = at(1500);
3189 assert_eq!((u.percent, u.warn), (Some(150), true));
3190 assert!(!u.since_switch);
3191 }
3192
3193 #[test]
3194 fn context_usage_is_unknown_without_usage_and_never_looks_back() {
3195 let cfg = ctx_config(&[("small-model", 1000)]);
3196 let t = ctx_talk(
3197 "small",
3198 vec![
3199 reply("old", Some((900, "small", Some("small-model")))),
3200 reply("new", None),
3201 ],
3202 );
3203 let u = context_usage(&t, Some(&cfg));
3204 assert!(u.estimated);
3206 assert_ne!(u.tokens, Some(900));
3207 assert!(u.tokens.is_some());
3208 let t = ctx_talk(
3210 "small",
3211 vec![
3212 reply("old", Some((900, "small", Some("small-model")))),
3213 reply("magi: could not run agent", None),
3214 ],
3215 );
3216 assert_eq!(context_usage(&t, Some(&cfg)).tokens, Some(900));
3217 assert_eq!(
3218 context_usage(&ctx_talk("small", Vec::new()), Some(&cfg)).tokens,
3219 None
3220 );
3221 }
3222
3223 #[test]
3224 fn estimate_counts_chars_both_sides_and_standing_prompt() {
3225 let mut t = ctx_talk("small", vec![reply("abcdefg", None)]);
3226 assert_eq!(estimate_context_tokens(&t, 0), Some(2)); let op = Turn {
3228 sender: None,
3229 who: Who::Operator,
3230 ..reply("abcdefg", None)
3231 };
3232 t.turns.push(op);
3233 assert_eq!(estimate_context_tokens(&t, 0), Some(4));
3234 assert!(
3235 estimate_context_tokens(&t, 700).unwrap() > estimate_context_tokens(&t, 0).unwrap()
3236 );
3237 let ja = ctx_talk("small", vec![reply("日本語日本語日", None)]);
3239 assert_eq!(estimate_context_tokens(&ja, 0), Some(2));
3240 let note = ctx_talk("small", vec![reply("magi: could not run agent", None)]);
3242 assert_eq!(estimate_context_tokens(¬e, 1000), None);
3243 assert_eq!(
3244 estimate_context_tokens(&ctx_talk("small", Vec::new()), 1000),
3245 None
3246 );
3247 }
3248
3249 #[test]
3250 fn context_usage_measured_wins_and_estimate_gets_percent_and_warn() {
3251 let cfg = ctx_config(&[("small-model", 1000)]);
3252 let t = ctx_talk(
3253 "small",
3254 vec![reply(
3255 &"x".repeat(5000),
3256 Some((10, "small", Some("small-model"))),
3257 )],
3258 );
3259 let u = context_usage(&t, Some(&cfg));
3260 assert_eq!((u.tokens, u.estimated), (Some(10), false));
3261 let t = ctx_talk("small", vec![reply(&"x".repeat(5000), None)]);
3262 let u = context_usage(&t, Some(&cfg));
3263 assert!(u.estimated && !u.since_switch);
3264 assert_eq!(u.window, Some(1000));
3265 assert!(u.warn && u.percent.unwrap() >= 80);
3266 let t = ctx_talk("small", vec![reply("hi", None)]);
3267 let u = context_usage(&t, Some(&cfg));
3268 assert!(u.estimated && u.percent.is_some());
3269 }
3270
3271 #[test]
3272 fn context_usage_without_a_window_shows_tokens_only() {
3273 let cfg = ctx_config(&[]);
3274 let t = ctx_talk("plain", vec![reply("hi", Some((5000, "plain", None)))]);
3276 let u = context_usage(&t, Some(&cfg));
3277 assert_eq!(
3278 (u.tokens, u.window, u.percent, u.warn),
3279 (Some(5000), None, None, false)
3280 );
3281 let t = ctx_talk(
3282 "small",
3283 vec![reply("hi", Some((5000, "small", Some("small-model"))))],
3284 );
3285 assert_eq!(context_usage(&t, Some(&cfg)).percent, None);
3286 assert_eq!(context_usage(&t, None).window, None);
3288 }
3289
3290 #[test]
3291 fn context_usage_switching_model_changes_the_denominator() {
3292 let cfg = ctx_config(&[("small-model", 1000), ("big-model", 10_000)]);
3293 let used = reply("hi", Some((900, "small", Some("small-model"))));
3294 let before = context_usage(&ctx_talk("small", vec![used.clone()]), Some(&cfg));
3295 assert_eq!(
3296 (before.percent, before.warn, before.since_switch),
3297 (Some(90), true, false)
3298 );
3299 let after = context_usage(&ctx_talk("big", vec![used]), Some(&cfg));
3302 assert_eq!(after.window, Some(10_000));
3303 assert_eq!(
3304 (after.percent, after.warn, after.since_switch),
3305 (Some(9), false, true)
3306 );
3307 assert_eq!(after.model.as_deref(), Some("big-model"));
3308 }
3309
3310 #[test]
3311 fn a_turn_recorded_before_usage_existed_still_reads() {
3312 let old = r#"{"who":"agent","body":"hi","at":"2026-09-04T01:44:55Z"}"#;
3313 let turn: Turn = serde_json::from_str(old).expect("old turn reads");
3314 assert!(turn.usage.is_none());
3315 let json = serde_json::to_string(&turn).expect("serialize");
3316 assert!(
3317 !json.contains("usage"),
3318 "absent usage is not written: {json}"
3319 );
3320 }
3321
3322 fn store() -> (tempfile::TempDir, Talks) {
3324 let tmp = tempfile::tempdir().expect("tempdir");
3325 let talks = Talks::at(tmp.path().join("talks"));
3326 (tmp, talks)
3327 }
3328
3329 fn mock_agent(dir: &Path, script: &str, env: BTreeMap<String, String>) -> AgentSpec {
3333 let path = dir.join("mock-talk-agent.sh");
3334 std::fs::write(&path, script).expect("write mock");
3335 AgentSpec {
3336 id: "mock".to_owned(),
3337 kind: AgentKind::Command,
3338 model: None,
3339 command: vec!["sh".to_owned(), path.to_string_lossy().into_owned()],
3340 extra_args: Vec::new(),
3341 env,
3342 prompt_delivery: None,
3343 }
3344 }
3345
3346 fn config(spec: AgentSpec) -> Config {
3347 Config {
3348 agents: vec![spec],
3349 graph: Graph {
3350 language: "en".to_owned(),
3351 ..Graph::default()
3352 },
3353 ..Config::default()
3354 }
3355 }
3356
3357 const REPLY: &str = "#!/bin/sh\ncat >/dev/null\nprintf '%s\\n' \"$MOCK_REPLY\"\n";
3359
3360 const BROKEN: &str = "#!/bin/sh\ncat >/dev/null\nexit 3\n";
3362
3363 const ECHO: &str = "#!/bin/sh\ncat\n";
3366
3367 fn env(reply: &str) -> BTreeMap<String, String> {
3368 BTreeMap::from([("MOCK_REPLY".to_owned(), reply.to_owned())])
3369 }
3370
3371 #[test]
3372 fn the_frozen_json_field_names_round_trip_through_disk() {
3373 let (tmp, talks) = store();
3374 let mut talk = Talk {
3375 posts_read_through: None,
3376 pending_breaks: None,
3377 pending_messages: Vec::new(),
3378 schema: SCHEMA,
3379 id: "20260904-014455-ab12".to_owned(),
3380 repo: tmp.path().to_owned(),
3381 agent: "sonnet".to_owned(),
3382 status: TalkStatus::Open,
3383 turns: Vec::new(),
3384 pending: String::new(),
3385 pending_attachments: Vec::new(),
3386 fallback: false,
3387 persona: String::new(),
3388 persona_dirty: false,
3389 implementers: 1,
3390 implementers_dirty: false,
3391 notified: Vec::new(),
3392 completion_notices: Vec::new(),
3393 created_at: Timestamp::now(),
3394 updated_at: Timestamp::now(),
3395 seat: SeatState::new(SEAT, "sonnet", 7),
3396 };
3397 talks.put(&mut talk).expect("put");
3398
3399 let raw = std::fs::read_to_string(talks.path_of(&talk.id)).expect("read back");
3400 let v: serde_json::Value = serde_json::from_str(&raw).expect("parse");
3401 for field in [
3402 "schema",
3403 "id",
3404 "repo",
3405 "agent",
3406 "status",
3407 "turns",
3408 "created_at",
3409 "updated_at",
3410 ] {
3411 assert!(v.get(field).is_some(), "missing field `{field}`");
3412 }
3413 assert_eq!(v["schema"], 1);
3414 assert_eq!(v["status"], "open");
3415
3416 let back = talks.get(&talk.id).expect("get");
3417 assert_eq!(back.posts_read_through, None);
3418 assert_eq!(back.id, talk.id);
3419 assert_eq!(back.status, TalkStatus::Open);
3420 }
3421
3422 #[test]
3423 fn opening_a_talk_takes_no_agent_turn() {
3424 let (tmp, talks) = store();
3425 let spec = mock_agent(tmp.path(), BROKEN, BTreeMap::new());
3429 let cfg = config(spec);
3430
3431 let talk = begin(&talks, &cfg, tmp.path().to_owned(), None).expect("begin");
3432 assert_eq!(talk.status, TalkStatus::Open);
3433 assert!(talk.turns.is_empty(), "nothing has been said yet");
3434
3435 let on_disk = talks.get(&talk.id).expect("get");
3436 assert_eq!(on_disk.turns.len(), 0);
3437 }
3438
3439 #[test]
3447 fn chatter_wins_when_set_and_falls_back_to_pick_s_default_order_otherwise() {
3448 let (tmp, talks) = store();
3449 let first_spec = mock_agent(tmp.path(), BROKEN, BTreeMap::new());
3450 let mut chatter_spec = mock_agent(tmp.path(), BROKEN, BTreeMap::new());
3451 chatter_spec.id = "chatter-mock".to_owned();
3452
3453 let mut cfg = Config {
3454 agents: vec![first_spec.clone(), chatter_spec.clone()],
3455 graph: Graph {
3456 language: "en".to_owned(),
3457 ..Graph::default()
3458 },
3459 ..Config::default()
3460 };
3461 cfg.roles.chatter = Some(chatter_spec.id.as_str().into());
3462
3463 let talk =
3464 begin(&talks, &cfg, tmp.path().to_owned(), None).expect("begin with chatter set");
3465 assert_eq!(talk.agent, chatter_spec.id, "an explicit chatter must win");
3466
3467 cfg.roles.chatter = None;
3468 let fallback =
3469 begin(&talks, &cfg, tmp.path().to_owned(), None).expect("begin with chatter unset");
3470 assert_eq!(
3471 fallback.agent, first_spec.id,
3472 "unset chatter must fall back to agent::pick's own default order"
3473 );
3474 }
3475
3476 #[test]
3479 fn a_talk_recorded_without_attachments_still_reads() {
3480 let (tmp, talks) = store();
3481 let path = talks.path_of("20260904-014455-ab12");
3482 std::fs::create_dir_all(talks.root()).expect("talks dir");
3483 std::fs::write(
3484 &path,
3485 serde_json::json!({
3486 "schema": 1,
3487 "id": "20260904-014455-ab12",
3488 "repo": tmp.path(),
3489 "agent": "sonnet",
3490 "status": "open",
3491 "turns": [
3492 { "who": "operator", "body": "still there?",
3493 "at": Timestamp::now().to_string() },
3494 ],
3495 "created_at": Timestamp::now().to_string(),
3496 "updated_at": Timestamp::now().to_string(),
3497 "seat": SeatState::new(SEAT, "sonnet", 7),
3498 })
3499 .to_string(),
3500 )
3501 .expect("write pre-attachments talk");
3502
3503 let talk = talks.get("20260904-014455-ab12").expect("must still read");
3504 assert!(talk.turns[0].attachments.is_empty());
3505 }
3506
3507 fn lease_store() -> (tempfile::TempDir, Talks, Talks) {
3508 let tmp = tempfile::TempDir::new().expect("tmp");
3509 let root = tmp.path().join("talks");
3510 (tmp, Talks::at(root.clone()), Talks::at(root))
3511 }
3512
3513 #[test]
3514 fn two_starters_on_one_talk_one_wins_and_the_other_is_refused() {
3515 let (_tmp, a, b) = lease_store();
3516 let won = a.claim_turn("t1").expect("claim").expect("first wins");
3517 assert!(
3518 b.claim_turn("t1").expect("claim").is_none(),
3519 "second is refused"
3520 );
3521 assert!(b.turn_held("t1"));
3522 assert!(
3523 b.claim_turn("t2").expect("claim").is_some(),
3524 "other talks are free"
3525 );
3526 drop(won);
3527 }
3528
3529 #[test]
3530 fn a_stale_lease_is_taken_over_and_the_old_guard_cannot_release_it() {
3531 let (_tmp, a, b) = lease_store();
3532 let old = a.claim_turn("t1").expect("claim").expect("held");
3533 let later = Timestamp::now()
3534 .checked_add(jiff::SignedDuration::from_secs(
3535 crate::ask::LEASE_TTL.as_secs() as i64 + 5,
3536 ))
3537 .expect("later");
3538 let new = b
3539 .claim_turn_at("t1", later)
3540 .expect("claim")
3541 .expect("a stale lease is taken over");
3542 drop(old);
3543 assert!(a.turn_held("t1"), "the old guard left the new lease alone");
3544 assert!(new.beat().expect("beat"), "the new owner still beats");
3545 drop(new);
3546 assert!(!a.turn_held("t1"));
3547 }
3548
3549 #[tokio::test]
3550 async fn a_turn_whose_lease_was_taken_over_is_stopped() {
3551 let (_tmp, a, b) = lease_store();
3552 let old = a.claim_turn("t1").expect("claim").expect("held");
3553 let later = Timestamp::now()
3554 .checked_add(jiff::SignedDuration::from_secs(
3555 crate::ask::LEASE_TTL.as_secs() as i64 + 5,
3556 ))
3557 .expect("later");
3558 let _new = b
3559 .claim_turn_at("t1", later)
3560 .expect("claim")
3561 .expect("taken over");
3562 let out = old
3563 .beating_every(Duration::from_millis(10), std::future::pending::<()>())
3564 .await;
3565 assert!(out.is_err(), "the displaced turn must stop, not run on");
3566 }
3567
3568 #[tokio::test]
3569 async fn a_turn_that_finishes_is_returned_and_keeps_its_lease_beating() {
3570 let (_tmp, a, _b) = lease_store();
3571 let lease = a.claim_turn("t1").expect("claim").expect("held");
3572 let out = lease
3573 .beating_every(Duration::from_millis(5), async {
3574 tokio::time::sleep(Duration::from_millis(40)).await;
3575 7
3576 })
3577 .await
3578 .expect("still ours");
3579 assert_eq!(out, 7);
3580 assert!(a.turn_held("t1"));
3581 }
3582
3583 #[test]
3584 fn an_unreadable_lease_counts_as_stale() {
3585 let (_tmp, a, b) = lease_store();
3586 std::fs::create_dir_all(a.root()).expect("dir");
3587 std::fs::write(a.turn_path("t1"), "not json").expect("write");
3588 assert!(!a.turn_held("t1"));
3589 assert!(b.claim_turn("t1").expect("claim").is_some());
3590 }
3591
3592 #[test]
3593 fn without_hard_links_a_claim_is_still_exclusive_and_a_young_placeholder_blocks() {
3594 let (_tmp, a, b) = lease_store();
3595 failpoint::force_no_link(true);
3596 let held = a.claim_turn("t1").expect("claim").expect("first wins");
3597 assert!(b.claim_turn("t1").expect("claim").is_none());
3598 assert!(a.turn_held("t1"));
3599 drop(held);
3600 assert!(!a.turn_held("t1"));
3601 let path = a.turn_path("t1");
3603 assert!(create_exclusive(&path, "").expect("placeholder"));
3604 assert!(b.claim_turn("t1").expect("claim").is_none(), "young: held");
3605 age_file(&path);
3606 assert!(b.claim_turn("t1").expect("claim").is_some(), "old: stale");
3607 let lease = a.turn_path("t2");
3609 let lock = TurnLock::take(&lease).expect("take").expect("lock");
3610 assert!(TurnLock::take(&lease).expect("take").is_none());
3611 drop(lock);
3612 assert!(TurnLock::take(&lease).expect("take").is_some());
3613 failpoint::force_no_link(false);
3614 }
3615
3616 #[test]
3617 fn remove_if_carries_removes_only_the_expected_content() {
3618 let (_tmp, a, _b) = lease_store();
3619 std::fs::create_dir_all(a.root()).expect("dir");
3620 let p = a.root().join("x.turn.lock");
3621 std::fs::write(&p, "mine").expect("write");
3622 assert!(!remove_if_carries(&p, "other"));
3623 assert_eq!(std::fs::read_to_string(&p).expect("kept"), "mine");
3624 assert!(remove_if_carries(&p, "mine"));
3625 assert!(!p.exists());
3626 assert!(!remove_if_carries(&p, "mine"), "absent is not a removal");
3627 }
3628
3629 #[test]
3630 fn a_dropped_lock_does_not_remove_a_lock_taken_over_since() {
3631 let (_tmp, a, _b) = lease_store();
3632 std::fs::create_dir_all(a.root()).expect("dir");
3633 let lease = a.turn_path("t1");
3634 let lock = TurnLock::take(&lease).expect("take").expect("lock");
3635 let path = lock.path.clone();
3636 std::fs::write(&path, "someone-else").expect("replace");
3637 drop(lock);
3638 assert_eq!(
3639 std::fs::read_to_string(&path).expect("kept"),
3640 "someone-else"
3641 );
3642 }
3643
3644 #[test]
3645 fn concurrent_takeovers_of_a_stale_lease_have_one_winner() {
3646 let (_tmp, a, _b) = lease_store();
3647 drop(a.claim_turn("t1").expect("claim").expect("held"));
3648 std::fs::write(
3649 a.turn_path("t1"),
3650 serde_json::to_string(&TurnRecord {
3651 token: "gone".into(),
3652 pid: 1,
3653 beat_at: Timestamp::from_second(1).expect("ts"),
3654 })
3655 .expect("json"),
3656 )
3657 .expect("write");
3658 let wins: Vec<_> = std::thread::scope(|sc| {
3659 let hs: Vec<_> = (0..8)
3660 .map(|_| {
3661 let s = a.clone();
3662 sc.spawn(move || s.claim_turn("t1").expect("claim"))
3663 })
3664 .collect();
3665 hs.into_iter().map(|h| h.join().expect("join")).collect()
3666 });
3667 assert_eq!(wins.iter().filter(|w| w.is_some()).count(), 1);
3668 }
3669
3670 fn age_file(path: &Path) {
3671 let f = std::fs::OpenOptions::new()
3672 .write(true)
3673 .open(path)
3674 .expect("open");
3675 f.set_modified(std::time::SystemTime::now() - std::time::Duration::from_secs(60))
3676 .expect("age");
3677 }
3678
3679 #[test]
3680 fn a_late_taker_of_a_broken_lock_cannot_disturb_its_replacement() {
3681 let (_tmp, a, _b) = lease_store();
3682 let lease = a.turn_path("t1");
3683 let lock = lease.with_extension("turn.lock");
3684 std::fs::create_dir_all(lock.parent().expect("dir")).expect("dir");
3685 std::fs::write(&lock, "t1-dead").expect("dead lock");
3686 age_file(&lock);
3687 let b = TurnLock::take(&lease).expect("take").expect("b wins");
3689 let fresh = std::fs::read_to_string(&lock).expect("read");
3690 assert_eq!(fresh, b.token);
3691 let ticket = std::fs::read_dir(lock.parent().expect("dir"))
3694 .expect("dir")
3695 .flatten()
3696 .map(|e| e.path())
3697 .find(|p| p.to_string_lossy().ends_with(".break.0"))
3698 .expect("ticket");
3699 assert!(!create_exclusive(&ticket, "").expect("ticket"));
3700 assert!(TurnLock::take(&lease).expect("take").is_none());
3701 assert_eq!(std::fs::read_to_string(&lock).expect("read"), fresh);
3702 age_file(&lock);
3704 age_file(&ticket);
3705 std::mem::forget(b);
3706 let c = TurnLock::take(&lease)
3707 .expect("take")
3708 .expect("next generation");
3709 assert_ne!(c.token, fresh);
3710 }
3711
3712 #[test]
3713 fn a_live_ticket_blocks_and_a_stale_one_hands_over_to_the_next_generation() {
3714 let (_tmp, a, _b) = lease_store();
3715 let lease = a.turn_path("t1");
3716 let lock = lease.with_extension("turn.lock");
3717 std::fs::create_dir_all(lock.parent().expect("dir")).expect("dir");
3718 std::fs::write(&lock, "t1-dead").expect("dead lock");
3719 age_file(&lock);
3720 let t0 = lock.with_extension("lock.t1-dead.break.0");
3721 assert!(create_exclusive(&t0, "").expect("ticket"));
3722 assert!(TurnLock::take(&lease).expect("take").is_none());
3724 assert_eq!(std::fs::read_to_string(&lock).expect("read"), "t1-dead");
3725 age_file(&t0);
3727 let c = TurnLock::take(&lease).expect("take").expect("generation 1");
3728 assert_eq!(std::fs::read_to_string(&lock).expect("read"), c.token);
3729 assert!(lock.with_extension("lock.t1-dead.break.1").exists());
3730 assert!(TurnLock::take(&lease).expect("take").is_none());
3731 }
3732
3733 #[test]
3734 fn exhausted_ticket_generations_recover_once_the_sweep_ages_them_out() {
3735 let (_tmp, a, _b) = lease_store();
3736 let lease = a.turn_path("t1");
3737 let lock = lease.with_extension("turn.lock");
3738 std::fs::create_dir_all(lock.parent().expect("dir")).expect("dir");
3739 std::fs::write(&lock, "t1-dead").expect("dead lock");
3740 age_file(&lock);
3741 let tickets: Vec<_> = (0..TICKET_GENERATIONS)
3742 .map(|n| lock.with_extension(format!("lock.t1-dead.break.{n}")))
3743 .collect();
3744 for t in &tickets {
3745 assert!(create_exclusive(t, "").expect("ticket"));
3746 age_file(t);
3747 }
3748 assert!(TurnLock::take(&lease).expect("take").is_none());
3750 assert!(tickets.iter().all(|t| t.exists()));
3751 for t in &tickets {
3753 let f = std::fs::OpenOptions::new()
3754 .write(true)
3755 .open(t)
3756 .expect("open");
3757 f.set_modified(std::time::SystemTime::now() - TICKET_SWEEP_AGE * 2)
3758 .expect("age");
3759 }
3760 assert!(TurnLock::take(&lease).expect("take").is_none());
3761 assert!(TurnLock::take(&lease).expect("take").is_some());
3762 }
3763
3764 #[test]
3765 fn an_aged_empty_lock_is_broken() {
3766 let (_tmp, a, _b) = lease_store();
3767 let lease = a.turn_path("t1");
3768 let lock = lease.with_extension("turn.lock");
3769 std::fs::create_dir_all(lock.parent().expect("dir")).expect("dir");
3770 std::fs::write(&lock, "").expect("empty lock");
3771 age_file(&lock);
3772 assert!(TurnLock::take(&lease).expect("take").is_some());
3773 }
3774
3775 #[test]
3776 fn reply_plain_text_sends_keep_provenance_and_boundaries() {
3777 let (tmp, talks) = store();
3778 let cfg = config(mock_agent(tmp.path(), REPLY, env("reply")));
3779 let mut talk = begin(&talks, &cfg, tmp.path().to_owned(), None).expect("begin");
3780 record(&mut talk, &talks, "original", Vec::new()).expect("record");
3781 queue(&mut talk, &talks, "first", Vec::new()).expect("queue");
3782 queue(&mut talk, &talks, "second", Vec::new()).expect("queue");
3783 let wire: serde_json::Value =
3784 serde_json::from_str(&std::fs::read_to_string(talks.path_of(&talk.id)).unwrap())
3785 .unwrap();
3786 assert_eq!(wire["pending_messages"][0]["sender"], "operator");
3787 assert_eq!(wire["turns"][0]["messages"][0]["sender"], "operator");
3788 drain(&mut talk, &talks).expect("drain");
3789 let saved = talks.get(&talk.id).expect("reload");
3790 assert_eq!(saved.turns[1].body, "first\n\nsecond");
3791 assert_eq!(saved.turns[1].breaks, Some(vec![7]));
3792 let wire: serde_json::Value =
3793 serde_json::from_str(&std::fs::read_to_string(talks.path_of(&saved.id)).unwrap())
3794 .unwrap();
3795 for turn in wire["turns"].as_array().expect("turns") {
3797 for field in turn.as_object().expect("turn").keys() {
3798 assert!(
3799 [
3800 "who",
3801 "body",
3802 "at",
3803 "attachments",
3804 "usage",
3805 "breaks",
3806 "posted_by",
3807 "sender",
3808 "messages"
3809 ]
3810 .contains(&field.as_str()),
3811 "unknown turn field: {field}"
3812 );
3813 }
3814 }
3815 record_reply(
3816 &mut talk,
3817 &talks,
3818 "reply",
3819 Vec::new(),
3820 Some(ReplyTarget {
3821 turn: 1,
3822 message: 1,
3823 }),
3824 )
3825 .expect("reply to reloaded plain text");
3826 assert_eq!(
3827 talk.turns[2].messages[0].reply.as_ref().unwrap().excerpt,
3828 "second"
3829 );
3830 }
3831
3832 #[tokio::test]
3833 async fn reply_image_identity_reaches_fresh_and_followup_prompts_after_reload() {
3834 let (tmp, talks) = store();
3835 let cfg = config(mock_agent(tmp.path(), ECHO, BTreeMap::new()));
3836 let mut talk = begin(&talks, &cfg, tmp.path().to_owned(), None).expect("begin");
3837 let first = talks
3838 .put_attachment(&talk.id, "image/png", "first.png", b"first")
3839 .expect("first");
3840 let second = talks
3841 .put_attachment(&talk.id, "image/png", "second.png", b"second")
3842 .expect("second");
3843 let first_path = talks.attachment_path(&talk.id, &first).unwrap();
3844 let second_path = talks.attachment_path(&talk.id, &second).unwrap();
3845 queue(&mut talk, &talks, "", vec![first]).expect("queue first");
3847 queue(&mut talk, &talks, "", vec![second]).expect("queue second");
3848 drain(&mut talk, &talks).expect("drain");
3849 let lease = talks.claim_turn(&talk.id).expect("claim").expect("lease");
3850 for (message, selected, other) in [
3851 (0, &first_path, &second_path),
3852 (1, &second_path, &first_path),
3853 ] {
3854 record_reply(
3855 &mut talk,
3856 &talks,
3857 "describe this",
3858 Vec::new(),
3859 Some(ReplyTarget { turn: 0, message }),
3860 )
3861 .expect("reply");
3862 talk = talks.get(&talk.id).expect("reload");
3863 let quote = message_prompt(talk.turns.last().unwrap(), &talk, &talks);
3864 assert!(quote.contains(selected.to_str().unwrap()));
3865 assert!(!quote.contains(other.to_str().unwrap()));
3866 respond(&lease, &mut talk, &talks, &cfg, "describe this")
3867 .await
3868 .expect("respond");
3869 assert!(
3870 talk.turns
3871 .last()
3872 .unwrap()
3873 .body
3874 .contains(&format!("Replied-to image: {}", selected.display()))
3875 );
3876 }
3877 }
3878
3879 #[test]
3880 fn reply_sends_survive_queue_edit_drain_reload_and_legacy_records() {
3881 let (tmp, talks) = store();
3882 let cfg = config(mock_agent(tmp.path(), REPLY, env("reply")));
3883 let mut talk = begin(&talks, &cfg, tmp.path().to_owned(), None).expect("begin");
3884 record(&mut talk, &talks, &"猫".repeat(300), Vec::new()).expect("record");
3885 talk.turns[0].messages.clear();
3887 talks.put(&mut talk).expect("legacy save");
3888 let target = ReplyTarget {
3889 turn: 0,
3890 message: 0,
3891 };
3892 queue_reply(&mut talk, &talks, "first", Vec::new(), Some(target.clone())).expect("first");
3893 queue_reply(&mut talk, &talks, "second", Vec::new(), None).expect("second");
3894 assert!(
3895 edit_pending_message(&mut talk, &talks, "edited", "first\n\nsecond", &[], Some(0))
3896 .expect("edit")
3897 );
3898 drain(&mut talk, &talks).expect("drain");
3899 let saved = talks.get(&talk.id).expect("reload");
3900 assert_eq!(saved.turns[1].body, "edited\n\nsecond");
3901 assert_eq!(saved.turns[1].messages.len(), 2);
3902 let reply = saved.turns[1].messages[0].reply.as_ref().expect("reply");
3903 assert_eq!(reply.target, target);
3904 assert_eq!(reply.excerpt, "猫".repeat(240));
3905 assert!(message_prompt(&saved.turns[1], &saved, &talks).contains("[Reply to Operator:"));
3906 assert!(transcript(&saved, &talks).contains("[Reply to Operator:"));
3907 queue_reply(
3908 &mut talk,
3909 &talks,
3910 "third",
3911 Vec::new(),
3912 Some(ReplyTarget {
3913 turn: 1,
3914 message: 1,
3915 }),
3916 )
3917 .expect("specific send");
3918 assert_eq!(
3919 talk.pending_messages[0]
3920 .reply
3921 .as_ref()
3922 .expect("reply")
3923 .excerpt,
3924 "second"
3925 );
3926 clear_pending(&mut talk, &talks).expect("clear");
3927 assert!(talk.pending_messages.is_empty());
3928 assert!(
3929 record_reply(
3930 &mut talk,
3931 &talks,
3932 "invalid",
3933 Vec::new(),
3934 Some(ReplyTarget {
3935 turn: 1,
3936 message: 2
3937 })
3938 )
3939 .is_err()
3940 );
3941 assert_eq!(talks.get(&talk.id).expect("reload").turns.len(), 2);
3942 }
3943
3944 #[tokio::test]
3945 async fn reply_quotes_reach_fresh_and_followup_prompts_without_changing_body() {
3946 let (tmp, talks) = store();
3947 let cfg = config(mock_agent(tmp.path(), ECHO, BTreeMap::new()));
3948 let mut talk = begin(&talks, &cfg, tmp.path().to_owned(), None).expect("begin");
3949 record(&mut talk, &talks, "original", Vec::new()).expect("record");
3950 let target = ReplyTarget {
3951 turn: 0,
3952 message: 0,
3953 };
3954 record_reply(
3955 &mut talk,
3956 &talks,
3957 "answer",
3958 Vec::new(),
3959 Some(target.clone()),
3960 )
3961 .expect("reply");
3962 let lease = talks.claim_turn(&talk.id).expect("claim").expect("lease");
3963 respond(&lease, &mut talk, &talks, &cfg, "answer")
3964 .await
3965 .expect("respond");
3966 assert!(
3967 talk.turns
3968 .last()
3969 .expect("response")
3970 .body
3971 .contains("[Reply to Operator: original]\nanswer")
3972 );
3973 assert_eq!(talk.turns[1].body, "answer");
3974 record_reply(&mut talk, &talks, "followup", Vec::new(), Some(target)).expect("reply");
3975 respond(&lease, &mut talk, &talks, &cfg, "followup")
3976 .await
3977 .expect("respond");
3978 assert!(
3979 talk.turns
3980 .last()
3981 .expect("response")
3982 .body
3983 .contains("[Reply to Operator: original]\nfollowup")
3984 );
3985 }
3986
3987 #[test]
3988 fn owner_indentation_survives_record_queue_and_edit() {
3989 let (tmp, talks) = store();
3990 let cfg = config(mock_agent(tmp.path(), REPLY, env("reply")));
3991 let mut talk = begin(&talks, &cfg, tmp.path().to_owned(), None).expect("begin");
3992 for text in [" Post original", "\tUse rewrite"] {
3993 record_reply(&mut talk, &talks, text, Vec::new(), None).unwrap();
3994 assert_eq!(
3995 talks.get(&talk.id).unwrap().turns.last().unwrap().body,
3996 text
3997 );
3998 queue(&mut talk, &talks, "draft", Vec::new()).unwrap();
3999 assert!(edit_pending_message(&mut talk, &talks, text, "draft", &[], Some(0)).unwrap());
4000 let saved = talks.get(&talk.id).unwrap();
4001 assert_eq!(saved.pending, text);
4002 assert_eq!(saved.pending_messages[0].body, text);
4003 assert!(edit_pending_message(&mut talk, &talks, text, text, &[], None).unwrap());
4004 let saved = talks.get(&talk.id).unwrap();
4005 assert_eq!(saved.pending, text);
4006 assert_eq!(saved.pending_messages[0].body, text);
4007 assert_eq!(drain(&mut talk, &talks).unwrap().as_deref(), Some(text));
4008 }
4009 }
4010
4011 #[test]
4012 fn queued_text_is_durable_combined_and_drained_as_one_operator_turn() {
4013 let (tmp, talks) = store();
4014 let cfg = config(mock_agent(tmp.path(), REPLY, env("reply")));
4015 let mut talk = begin(&talks, &cfg, tmp.path().to_owned(), None).expect("begin");
4016
4017 queue(&mut talk, &talks, "first", Vec::new()).expect("queue first");
4018 queue(&mut talk, &talks, "second", Vec::new()).expect("queue second");
4019 let saved = talks.get(&talk.id).expect("reload queued talk");
4020 assert_eq!(saved.pending, "first\n\nsecond");
4021 assert_eq!(saved.pending_breaks, Some(vec!["first\n\n".len()]));
4022 assert!(saved.turns.is_empty(), "a draft is not a transcript turn");
4023
4024 let drained = drain(&mut talk, &talks).expect("drain");
4025 assert_eq!(drained.as_deref(), Some("first\n\nsecond"));
4026 let saved = talks.get(&talk.id).expect("reload drained talk");
4027 assert!(saved.pending.is_empty());
4028 assert_eq!(saved.turns.len(), 1);
4029 assert_eq!(saved.turns[0].body, "first\n\nsecond");
4030 assert_eq!(saved.turns[0].breaks, Some(vec!["first\n\n".len()]));
4031 assert_eq!(saved.pending_breaks, None);
4032 }
4033
4034 #[test]
4035 fn editing_a_queued_draft_preserves_its_attachments_and_rejects_a_stale_snapshot() {
4036 let (tmp, talks) = store();
4037 let cfg = config(mock_agent(tmp.path(), REPLY, env("reply")));
4038 let mut talk = begin(&talks, &cfg, tmp.path().to_owned(), None).expect("begin");
4039 let attachment = Attachment {
4040 id: "a".repeat(32),
4041 name: "shot.png".to_owned(),
4042 mime: "image/png".to_owned(),
4043 bytes: 3,
4044 };
4045
4046 queue(&mut talk, &talks, "first", vec![attachment.clone()]).expect("queue");
4047 assert!(
4048 edit_pending_text(
4049 &mut talk,
4050 &talks,
4051 "corrected",
4052 "first",
4053 std::slice::from_ref(&attachment.id),
4054 )
4055 .expect("edit")
4056 );
4057 let saved = talks.get(&talk.id).expect("reload edited draft");
4058 assert_eq!(saved.pending, "corrected");
4059 assert_eq!(saved.pending_attachments, vec![attachment]);
4060
4061 queue(&mut talk, &talks, "later", Vec::new()).expect("queue concurrent draft");
4062 assert!(
4063 !edit_pending_text(
4064 &mut talk,
4065 &talks,
4066 "stale edit",
4067 "corrected",
4068 &["a".repeat(32)],
4069 )
4070 .expect("stale edit is a conflict")
4071 );
4072 assert_eq!(
4073 talks.get(&talk.id).expect("reload after conflict").pending,
4074 "corrected\n\nlater"
4075 );
4076 assert!(
4077 !clear_pending_if_matches(&mut talk, &talks, "corrected", &["a".repeat(32)])
4078 .expect("stale clear is a conflict")
4079 );
4080 assert_eq!(
4081 talks
4082 .get(&talk.id)
4083 .expect("reload after stale clear")
4084 .pending,
4085 "corrected\n\nlater"
4086 );
4087 }
4088
4089 #[tokio::test]
4090 async fn a_reply_save_preserves_pending_accepted_while_the_cli_runs() {
4091 let (tmp, talks) = store();
4092 let slow = "#!/bin/sh\ncat >/dev/null\nsleep 0.1\nprintf reply\n";
4093 let cfg = config(mock_agent(tmp.path(), slow, BTreeMap::new()));
4094 let mut running = begin(&talks, &cfg, tmp.path().to_owned(), None).expect("begin");
4095 let id = running.id.clone();
4096 let first = record(&mut running, &talks, "first", Vec::new()).expect("record");
4097
4098 let response_talks = talks.clone();
4099 let response_cfg = cfg.clone();
4100 let reply = tokio::spawn(async move {
4101 respond(
4102 &response_talks.claim_turn(&running.id).unwrap().unwrap(),
4103 &mut running,
4104 &response_talks,
4105 &response_cfg,
4106 &first,
4107 )
4108 .await
4109 });
4110 tokio::time::sleep(std::time::Duration::from_millis(20)).await;
4111
4112 let mut queued = talks.get(&id).expect("queued handle");
4113 queue(&mut queued, &talks, "next", Vec::new()).expect("queue");
4114 reply.await.expect("join").expect("reply");
4115
4116 let saved = talks.get(&id).expect("reload");
4117 assert_eq!(saved.pending, "next");
4118 assert_eq!(saved.pending_breaks, Some(Vec::new()));
4119 assert_eq!(saved.turns.len(), 2, "operator message and reply remain");
4120 }
4121
4122 #[tokio::test]
4123 async fn posts_during_a_resumed_turn_are_replayed_once_even_after_a_reply_save_failure() {
4124 for lose_reply in [false, true] {
4125 let (tmp, talks) = store();
4126 let cfg = config(mock_agent(tmp.path(), ECHO, BTreeMap::new()));
4127 let mut talk = begin(&talks, &cfg, tmp.path().to_owned(), None).expect("begin");
4128 say(
4129 &talks.claim_turn(&talk.id).unwrap().unwrap(),
4130 &mut talk,
4131 &talks,
4132 &cfg,
4133 "establish the session",
4134 Vec::new(),
4135 )
4136 .await
4137 .expect("first turn");
4138 assert!(agent::has_session(
4139 AgentKind::Command,
4140 &talk.seat,
4141 cfg.graph.sessions
4142 ));
4143
4144 post(&talks, &talk.id, "already available", "task-earlier").expect("earlier post");
4145 talk = talks.get(&talk.id).expect("reload earlier post");
4146 let ready = tmp.path().join("ready");
4147 let release = tmp.path().join("release");
4148 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";
4149 let slow_cfg = config(mock_agent(
4150 tmp.path(),
4151 slow,
4152 BTreeMap::from([
4153 ("READY".to_owned(), ready.to_string_lossy().into_owned()),
4154 ("RELEASE".to_owned(), release.to_string_lossy().into_owned()),
4155 ]),
4156 ));
4157 let text = record(&mut talk, &talks, "keep going", Vec::new()).expect("record");
4158 let prompt_turns = talk.turns.len();
4159 let id = talk.id.clone();
4160 let response_talks = talks.clone();
4161 let running = tokio::spawn(async move {
4162 respond(
4163 &response_talks.claim_turn(&talk.id).unwrap().unwrap(),
4164 &mut talk,
4165 &response_talks,
4166 &slow_cfg,
4167 &text,
4168 )
4169 .await
4170 });
4171 tokio::time::timeout(std::time::Duration::from_secs(5), async {
4172 while !ready.exists() {
4173 tokio::time::sleep(std::time::Duration::from_millis(10)).await;
4174 }
4175 })
4176 .await
4177 .expect("CLI read its prompt");
4178 post(&talks, &id, "concurrent task result", "task-one").expect("post");
4179 post(&talks, &id, "second task result", "task-two").expect("second post");
4180 if lose_reply {
4181 failpoint::force_put_failures(PUT_RETRIES);
4182 }
4183 std::fs::write(release, "go").expect("release CLI");
4184 let result = running.await.expect("join");
4185 if lose_reply {
4186 assert!(
4187 result
4188 .unwrap_err()
4189 .to_string()
4190 .contains("could not be saved")
4191 );
4192 } else {
4193 result.expect("reply");
4194 }
4195
4196 let mut saved = talks.get(&id).expect("reload");
4197 assert_eq!(saved.posts_read_through, Some(prompt_turns));
4198 assert_eq!(saved.turns[prompt_turns].body, "concurrent task result");
4199 assert_eq!(saved.turns[prompt_turns + 1].body, "second task result");
4200 assert!(posts_since_last_reply(&saved).is_some());
4201 let cfg = config(mock_agent(tmp.path(), ECHO, BTreeMap::new()));
4202 say(
4203 &talks.claim_turn(&id).unwrap().unwrap(),
4204 &mut saved,
4205 &talks,
4206 &cfg,
4207 "read the results",
4208 Vec::new(),
4209 )
4210 .await
4211 .expect("replay turn");
4212 let prompt = &saved.turns.last().unwrap().body;
4213 assert!(prompt.contains("# Posted by a task"), "{prompt}");
4214 assert!(!prompt.contains("already available"), "{prompt}");
4215 assert!(
4216 prompt
4217 .find("concurrent task result")
4218 .expect("first post in prompt")
4219 < prompt
4220 .find("second task result")
4221 .expect("second post in prompt")
4222 );
4223 let mut saved = talks.get(&id).expect("reload after replay");
4224 assert!(posts_since_last_reply(&saved).is_none());
4225 say(
4226 &talks.claim_turn(&id).unwrap().unwrap(),
4227 &mut saved,
4228 &talks,
4229 &cfg,
4230 "continue",
4231 Vec::new(),
4232 )
4233 .await
4234 .expect("next turn");
4235 assert!(!saved.turns.last().unwrap().body.contains("task result"));
4236 }
4237 }
4238
4239 fn counting_agent(dir: &Path, id: &str, body: &str) -> AgentSpec {
4242 let calls = dir.join(format!("{id}.calls"));
4243 let script = format!(
4244 "#!/bin/sh\necho x >> '{}'\n{body}\n",
4245 calls.to_string_lossy()
4246 );
4247 let path = dir.join(format!("mock-{id}.sh"));
4248 std::fs::write(&path, script).expect("write mock");
4249 AgentSpec {
4250 id: id.to_owned(),
4251 kind: AgentKind::Command,
4252 model: None,
4253 command: vec!["sh".to_owned(), path.to_string_lossy().into_owned()],
4254 extra_args: Vec::new(),
4255 env: BTreeMap::new(),
4256 prompt_delivery: None,
4257 }
4258 }
4259
4260 fn calls(dir: &Path, id: &str) -> usize {
4261 std::fs::read_to_string(dir.join(format!("{id}.calls"))).map_or(0, |s| s.lines().count())
4262 }
4263
4264 fn chain_config(specs: Vec<AgentSpec>, ids: &[&str]) -> Config {
4265 let mut cfg = config(specs[0].clone());
4266 cfg.agents = specs;
4267 cfg.roles.chatter = Some(AgentChoice::Chain(
4268 ids.iter().map(|s| (*s).to_owned()).collect(),
4269 ));
4270 cfg
4271 }
4272
4273 #[tokio::test]
4274 async fn completion_evidence_reaches_fresh_resumed_and_fallback_prompts() {
4275 for fallback in [false, true] {
4276 let (tmp, talks) = store();
4277 let cfg = if fallback {
4278 chain_config(
4279 vec![
4280 counting_agent(tmp.path(), "a", "cat >/dev/null\nexit 3"),
4281 counting_agent(tmp.path(), "b", "cat"),
4282 ],
4283 &["a", "b"],
4284 )
4285 } else {
4286 config(mock_agent(tmp.path(), ECHO, BTreeMap::new()))
4287 };
4288 let mut talk = begin(&talks, &cfg, tmp.path().to_owned(), None).unwrap();
4289 let lease = talks.claim_turn(&talk.id).unwrap().unwrap();
4290 for n in 0..2 {
4291 let text = format!(
4292 "# {}\n\n- Task: `unavailable-{n}`",
4293 crate::prompt::CHAT_TASK_NOTICE_HEADING
4294 );
4295 let reference = crate::chat_notify::CompletionReference {
4296 task: format!("unavailable-{n}"),
4297 run: None,
4298 notice: text.clone(),
4299 };
4300 let key = format!("unavailable-{n}:done");
4301 assert!(
4302 queue_once_matching(
4303 &mut talk,
4304 &talks,
4305 &text,
4306 &key,
4307 &|_| false,
4308 Some(&reference)
4309 )
4310 .unwrap()
4311 );
4312 assert!(
4313 !queue_once_matching(
4314 &mut talk,
4315 &talks,
4316 &text,
4317 &key,
4318 &|_| false,
4319 Some(&reference)
4320 )
4321 .unwrap()
4322 );
4323 talk = talks.get(&talk.id).unwrap();
4324 assert_eq!(talk.completion_notices.len(), n + 1);
4325 let text = drain(&mut talk, &talks).unwrap().unwrap();
4326 respond(&lease, &mut talk, &talks, &cfg, &text)
4327 .await
4328 .unwrap();
4329 let reply = &talk.turns.last().unwrap().body;
4330 assert!(reply.contains("# Verified outcome evidence"), "{reply}");
4331 assert!(reply.contains("Outcome evidence unavailable"));
4332 assert!(reply.contains("Do not file new tasks or edit repositories solely"));
4333 assert!(reply.contains("Explicitly say when no action remains"));
4334 }
4335 record(&mut talk, &talks, "ordinary follow-up", vec![]).unwrap();
4336 assert!(
4337 crate::chat_notify::response_evidence(&talk)
4338 .await
4339 .is_empty(),
4340 "history does not trigger lookups"
4341 );
4342 record(
4343 &mut talk,
4344 &talks,
4345 &format!("# {}\n\nlegacy", crate::prompt::CHAT_TASK_NOTICE_HEADING),
4346 vec![],
4347 )
4348 .unwrap();
4349 assert!(
4350 crate::chat_notify::response_evidence(&talk)
4351 .await
4352 .contains("Legacy notification has no structured task/run reference")
4353 );
4354 }
4355 }
4356
4357 #[tokio::test]
4358 async fn a_chatter_chain_falls_back_resends_the_transcript_and_sticks() {
4359 let (tmp, talks) = store();
4360 let a = counting_agent(tmp.path(), "a", "cat >/dev/null\nexit 3");
4361 let b = counting_agent(tmp.path(), "b", "cat");
4362 let cfg = chain_config(vec![a, b], &["a", "b"]);
4363 let mut talk = begin(&talks, &cfg, tmp.path().to_owned(), None).expect("begin");
4364 assert_eq!(talk.agent, "a");
4365 post(&talks, &talk.id, "fallback task result", "task").expect("post");
4366 talk = talks.get(&talk.id).expect("reload");
4367
4368 say(
4369 &talks.claim_turn(&talk.id).unwrap().unwrap(),
4370 &mut talk,
4371 &talks,
4372 &cfg,
4373 "hello there",
4374 Vec::new(),
4375 )
4376 .await
4377 .expect("turn");
4378 assert_eq!(calls(tmp.path(), "a"), 1, "each id is tried once");
4379 assert_eq!(calls(tmp.path(), "b"), 1);
4380 assert_eq!(talk.agent, "b", "the switch persists");
4381 assert!(talks.get(&talk.id).unwrap().agent == "b");
4382 let reply = talk.turns.last().unwrap();
4383 assert!(reply.body.contains("hello there"));
4384 assert!(reply.body.contains("fallback task result"));
4385 let saved = talks.get(&talk.id).expect("reload fallback");
4386 assert_eq!(saved.posts_read_through, Some(2));
4387 assert!(posts_since_last_reply(&saved).is_none());
4388 assert!(
4389 reply.body.contains("magi task add --solo"),
4390 "a fresh seat gets the full briefing"
4391 );
4392 assert!(
4393 talk.turns
4394 .iter()
4395 .any(|t| t.body.contains("agent changed from a to b")),
4396 "the switch is noted"
4397 );
4398 }
4399
4400 #[tokio::test]
4401 async fn an_exhausted_chatter_chain_fails_like_a_single_seat_and_stays_put() {
4402 let (tmp, talks) = store();
4403 let a = counting_agent(tmp.path(), "a", "cat >/dev/null\nexit 3");
4404 let b = counting_agent(tmp.path(), "b", "cat >/dev/null\nexit 4");
4405 let cfg = chain_config(vec![a, b], &["a", "b", "a"]);
4406 let mut talk = begin(&talks, &cfg, tmp.path().to_owned(), None).expect("begin");
4407
4408 let err = say(
4409 &talks.claim_turn(&talk.id).unwrap().unwrap(),
4410 &mut talk,
4411 &talks,
4412 &cfg,
4413 "hi",
4414 Vec::new(),
4415 )
4416 .await
4417 .expect_err("every agent failed");
4418 assert!(err.to_string().contains("`a`"), "{err:#}");
4419 assert_eq!(calls(tmp.path(), "a"), 1);
4420 assert_eq!(calls(tmp.path(), "b"), 1);
4421 assert_eq!(talk.agent, "a", "an exhausted chain leaves the agent alone");
4422 }
4423
4424 #[test]
4425 fn a_chatter_chain_skips_an_unknown_id_at_begin() {
4426 let (tmp, talks) = store();
4427 let b = counting_agent(tmp.path(), "b", "cat");
4428 let cfg = chain_config(vec![b], &["ghost", "b"]);
4429 let talk = begin(&talks, &cfg, tmp.path().to_owned(), None).expect("begin");
4430 assert_eq!(talk.agent, "b");
4431 }
4432
4433 #[tokio::test]
4434 async fn an_explicit_agent_inside_the_chatter_chain_stays_pinned() {
4435 let (tmp, talks) = store();
4436 let a = counting_agent(tmp.path(), "a", "cat >/dev/null\nexit 3");
4437 let b = counting_agent(tmp.path(), "b", "cat");
4438 let cfg = chain_config(vec![a, b], &["a", "b"]);
4439 let mut talk = begin(&talks, &cfg, tmp.path().to_owned(), Some("a")).expect("begin");
4440 say(
4441 &talks.claim_turn(&talk.id).unwrap().unwrap(),
4442 &mut talk,
4443 &talks,
4444 &cfg,
4445 "hi",
4446 Vec::new(),
4447 )
4448 .await
4449 .expect_err("a alone, and it fails");
4450 assert_eq!(calls(tmp.path(), "b"), 0);
4451 assert_eq!(talk.agent, "a");
4452 }
4453
4454 #[tokio::test]
4455 async fn an_explicit_agent_does_not_borrow_the_chatter_chain() {
4456 let (tmp, talks) = store();
4457 let a = counting_agent(tmp.path(), "a", "cat >/dev/null\nexit 3");
4458 let b = counting_agent(tmp.path(), "b", "cat");
4459 let c = counting_agent(tmp.path(), "c", "cat >/dev/null\nexit 3");
4460 let cfg = chain_config(vec![a, b, c], &["a", "b"]);
4461 let mut talk = begin(&talks, &cfg, tmp.path().to_owned(), Some("c")).expect("begin");
4462 say(
4463 &talks.claim_turn(&talk.id).unwrap().unwrap(),
4464 &mut talk,
4465 &talks,
4466 &cfg,
4467 "hi",
4468 Vec::new(),
4469 )
4470 .await
4471 .expect_err("c alone, and it fails");
4472 assert_eq!(calls(tmp.path(), "b"), 0);
4473 }
4474
4475 #[test]
4476 fn briefing_names_the_operator_with_or_without_a_persona() {
4477 let plain = briefing(Path::new("/repo"), "en", false);
4478 let named = briefing_with(Path::new("/repo"), "en", false, None, Some("Commander"));
4479 assert!(named.starts_with(&plain));
4480 assert!(named.contains("# Addressing the operator"));
4481 assert!(named.contains("\"Commander\""));
4482 let rei = crate::persona::builtin_catalog()
4483 .into_iter()
4484 .find(|p| p.id == "rei")
4485 .expect("rei");
4486 let with = briefing_with(
4487 Path::new("/repo"),
4488 "en",
4489 false,
4490 Some(&rei),
4491 Some("Commander"),
4492 );
4493 assert!(with.contains("\"Commander\""));
4494 assert!(!with.contains("# Addressing the operator"));
4495 }
4496
4497 #[test]
4498 fn briefing_carries_a_persona_section_only_when_one_is_chosen() {
4499 let plain = briefing(Path::new("/repo"), "en", false);
4500 assert_eq!(
4501 plain,
4502 briefing_with(Path::new("/repo"), "en", false, None, None)
4503 );
4504 assert!(!plain.contains("Persona"));
4505 let rei = crate::persona::builtin_catalog()
4506 .into_iter()
4507 .find(|p| p.id == "rei")
4508 .expect("rei");
4509 let with = briefing_with(Path::new("/repo"), "en", false, Some(&rei), None);
4510 assert!(with.starts_with(&plain), "the plain briefing is untouched");
4511 assert!(with.contains("# Persona (tone only)"));
4512 assert!(with.contains("TONE ONLY"));
4513 assert!(with.contains("task ids"));
4514 assert!(with.contains("write policy"));
4515 assert!(with.contains("`magi task add`"));
4516 assert!(with.contains("Rei Ayanami"));
4517 }
4518
4519 #[test]
4520 fn a_talk_written_before_personas_still_loads() {
4521 let (tmp, talks) = store();
4522 let spec = mock_agent(tmp.path(), ECHO, BTreeMap::new());
4523 let cfg = config(spec);
4524 let talk = begin(&talks, &cfg, tmp.path().to_owned(), None).expect("begin");
4525 let path = talks.root.join(format!("{}.json", talk.id));
4526 let mut v: serde_json::Value =
4527 serde_json::from_str(&std::fs::read_to_string(&path).unwrap()).unwrap();
4528 v.as_object_mut().unwrap().remove("persona");
4529 v.as_object_mut().unwrap().remove("persona_dirty");
4530 std::fs::write(&path, v.to_string()).unwrap();
4531 let loaded = talks.get(&talk.id).expect("old record loads");
4532 assert_eq!(loaded.persona, "");
4533 assert!(!loaded.persona_dirty);
4534 }
4535
4536 #[test]
4537 fn the_briefing_files_with_solo_or_the_chosen_implementer_count() {
4538 let repo = Path::new("/repo");
4539 let solo = briefing_for(repo, "", false, None, None, 1);
4540 assert_eq!(solo, briefing(repo, "", false));
4541 assert!(solo.contains("magi task add --solo --repo"));
4542 assert!(solo.contains("magi task add --solo --attach"));
4543 for n in [2u8, 3] {
4544 let b = briefing_for(repo, "", false, None, None, n);
4545 assert!(
4546 b.contains(&format!("magi task add --implementers {n} --repo")),
4547 "{b}"
4548 );
4549 assert!(b.contains(&format!("magi task add --implementers {n} --attach")));
4550 assert!(!b.contains("task add --solo"), "{b}");
4551 assert!(!b.contains("Use --solo"), "{b}");
4552 }
4553 }
4554
4555 #[test]
4556 fn begin_with_applies_remembered_choices_and_drops_unfit_ones_alone() {
4557 let (tmp, talks) = store();
4558 let first = mock_agent(tmp.path(), ECHO, BTreeMap::new());
4559 let mut second = first.clone();
4560 second.id = "second".to_owned();
4561 let mut cfg = config(first.clone());
4562 cfg.agents.push(second.clone());
4563 let open = |p: &Preferred| {
4564 begin_with(&talks, &cfg, tmp.path().to_owned(), None, p).expect("begin")
4565 };
4566
4567 let talk = open(&Preferred {
4568 agent: Some("second".into()),
4569 persona: Some("rei".into()),
4570 implementers: Some(i64::from(check_implementers(3, &cfg).unwrap_or(1))),
4571 });
4572 assert_eq!(talk.agent, "second");
4573 assert!(
4574 !talk.fallback,
4575 "a remembered non-head agent is an explicit choice"
4576 );
4577 assert_eq!(talk.persona, "rei");
4578 assert_eq!(talk.implementers, check_implementers(3, &cfg).unwrap_or(1));
4579 assert!(!talk.persona_dirty && !talk.implementers_dirty);
4580
4581 let head = open(&Preferred {
4583 agent: Some(first.id.clone()),
4584 ..Preferred::default()
4585 });
4586 assert!(head.fallback);
4587
4588 for implementers in [0, 4, -1, 1000] {
4590 let t = open(&Preferred {
4591 agent: Some("gone".into()),
4592 persona: Some("no-such-persona".into()),
4593 implementers: Some(implementers),
4594 });
4595 assert_eq!(t.agent, first.id);
4596 assert!(t.fallback);
4597 assert_eq!(t.persona, "");
4598 assert_eq!(t.implementers, 1);
4599 }
4600
4601 let t = open(&Preferred {
4603 agent: Some("second".into()),
4604 persona: Some("default".into()),
4605 implementers: Some(9),
4606 });
4607 assert_eq!(
4608 (t.agent.as_str(), t.persona.as_str(), t.implementers),
4609 ("second", "", 1)
4610 );
4611
4612 let strict = begin_with(
4614 &talks,
4615 &cfg,
4616 tmp.path().to_owned(),
4617 Some("first-unknown"),
4618 &Preferred {
4619 agent: Some("second".into()),
4620 ..Preferred::default()
4621 },
4622 );
4623 assert!(strict.is_err());
4624 }
4625
4626 #[test]
4627 fn remembered_agent_can_open_when_the_chatter_chain_cannot_resolve() {
4628 let (tmp, talks) = store();
4629 let spec = mock_agent(tmp.path(), ECHO, BTreeMap::new());
4630 let cfg = chain_config(vec![spec.clone()], &["missing-chatter"]);
4633 assert!(begin(&talks, &cfg, tmp.path().to_owned(), None).is_err());
4634 let talk = begin_with(
4635 &talks,
4636 &cfg,
4637 tmp.path().to_owned(),
4638 None,
4639 &Preferred {
4640 agent: Some(spec.id.clone()),
4641 ..Preferred::default()
4642 },
4643 )
4644 .expect("a valid remembered agent does not require a default chain");
4645 assert_eq!(talk.agent, spec.id);
4646 assert!(!talk.fallback);
4647 assert_eq!(talks.get(&talk.id).unwrap().agent, spec.id);
4648 assert!(
4649 begin_with(
4650 &talks,
4651 &cfg,
4652 tmp.path().to_owned(),
4653 None,
4654 &Preferred {
4655 agent: Some("missing-preference".into()),
4656 ..Preferred::default()
4657 },
4658 )
4659 .is_err()
4660 );
4661 }
4662
4663 #[test]
4664 fn implementers_are_validated_and_old_records_load_as_solo() {
4665 let (tmp, talks) = store();
4666 let cfg = config(mock_agent(tmp.path(), ECHO, BTreeMap::new()));
4667 assert_eq!(check_implementers(1, &cfg), Ok(1));
4668 assert_eq!(check_implementers(3, &cfg), Ok(3));
4669 assert!(check_implementers(0, &cfg).is_err());
4670 assert!(check_implementers(4, &cfg).is_err());
4671 let mut empty = cfg.clone();
4672 empty.agents.clear();
4673 assert!(check_implementers(2, &empty).is_err());
4674
4675 let talk = begin(&talks, &cfg, tmp.path().to_owned(), None).expect("begin");
4676 let path = talks.path_of(&talk.id);
4677 let mut v: serde_json::Value =
4678 serde_json::from_str(&std::fs::read_to_string(&path).unwrap()).unwrap();
4679 v.as_object_mut().unwrap().remove("implementers");
4680 v.as_object_mut().unwrap().remove("implementers_dirty");
4681 std::fs::write(&path, v.to_string()).unwrap();
4682 let loaded = talks.get(&talk.id).expect("load");
4683 assert_eq!(loaded.implementers, 1);
4684 assert!(!loaded.implementers_dirty);
4685 }
4686
4687 #[tokio::test]
4688 async fn an_implementers_switch_is_noted_once_and_kept_across_a_failed_turn() {
4689 let (tmp, talks) = store();
4690 let spec = mock_agent(tmp.path(), ECHO, BTreeMap::new());
4691 let cfg = config(spec);
4692 let mut talk = begin(&talks, &cfg, tmp.path().to_owned(), None).expect("begin");
4693 let lease = talks.claim_turn(&talk.id).unwrap().unwrap();
4694 say(&lease, &mut talk, &talks, &cfg, "hello", Vec::new())
4695 .await
4696 .expect("first turn");
4697
4698 assert!(switch_implementers(&mut talk, &talks, 2).expect("switch"));
4699 assert!(!switch_implementers(&mut talk, &talks, 2).expect("same"));
4700 assert!(talk.implementers_dirty);
4701 say(&lease, &mut talk, &talks, &cfg, "next", Vec::new())
4702 .await
4703 .expect("turn");
4704 let prompt = &talk.turns.last().unwrap().body;
4705 assert!(prompt.contains("# Task filing update"), "{prompt}");
4706 assert!(prompt.contains("--implementers 2"));
4707 assert!(!talk.implementers_dirty, "cleared after a successful turn");
4708
4709 say(&lease, &mut talk, &talks, &cfg, "again", Vec::new())
4710 .await
4711 .expect("turn");
4712 assert!(
4713 !talk
4714 .turns
4715 .last()
4716 .unwrap()
4717 .body
4718 .contains("# Task filing update")
4719 );
4720
4721 let mut no_sessions = cfg.clone();
4723 no_sessions.graph.sessions = false;
4724 say(&lease, &mut talk, &talks, &no_sessions, "one", Vec::new())
4725 .await
4726 .expect("turn");
4727 say(&lease, &mut talk, &talks, &no_sessions, "two", Vec::new())
4728 .await
4729 .expect("turn");
4730 assert!(talk.turns.last().unwrap().body.contains("--implementers 2"));
4731 }
4732
4733 #[tokio::test]
4734 async fn a_persona_switch_notes_marks_dirty_and_updates_the_next_turn_once() {
4735 let (tmp, talks) = store();
4736 let spec = mock_agent(tmp.path(), ECHO, BTreeMap::new());
4737 let cfg = config(spec);
4738 let mut talk = begin(&talks, &cfg, tmp.path().to_owned(), None).expect("begin");
4739 let lease = talks.claim_turn(&talk.id).unwrap().unwrap();
4740 say(&lease, &mut talk, &talks, &cfg, "hello", Vec::new())
4741 .await
4742 .expect("first turn");
4743 assert!(!talk.turns[1].body.contains("Persona"));
4744
4745 assert!(switch_persona(&mut talk, &talks, "misato").expect("switch"));
4746 assert!(!switch_persona(&mut talk, &talks, "misato").expect("same"));
4747 assert!(talk.persona_dirty);
4748 assert!(talk.turns.last().unwrap().body.contains("persona changed"));
4749
4750 say(&lease, &mut talk, &talks, &cfg, "next", Vec::new())
4751 .await
4752 .expect("turn");
4753 let prompt = &talk.turns.last().unwrap().body;
4754 assert!(prompt.contains("# Persona update"), "{prompt}");
4755 assert!(prompt.contains("Misato Katsuragi"));
4756 assert!(!talk.persona_dirty, "cleared after a successful turn");
4757
4758 say(&lease, &mut talk, &talks, &cfg, "again", Vec::new())
4759 .await
4760 .expect("turn");
4761 assert!(!talk.turns.last().unwrap().body.contains("# Persona update"));
4762
4763 assert!(switch_persona(&mut talk, &talks, "default").expect("back"));
4764 assert_eq!(talk.persona, "");
4765 say(&lease, &mut talk, &talks, &cfg, "plain", Vec::new())
4766 .await
4767 .expect("turn");
4768 assert!(
4769 talk.turns
4770 .last()
4771 .unwrap()
4772 .body
4773 .contains("turned the persona off")
4774 );
4775
4776 assert!(switch_persona(&mut talk, &talks, "rei").expect("rei"));
4778 let mut no_sessions = cfg.clone();
4779 no_sessions.graph.sessions = false;
4780 say(&lease, &mut talk, &talks, &no_sessions, "one", Vec::new())
4781 .await
4782 .expect("turn");
4783 say(&lease, &mut talk, &talks, &no_sessions, "two", Vec::new())
4784 .await
4785 .expect("turn");
4786 let last = &talk.turns.last().unwrap().body;
4787 assert!(!talk.persona_dirty);
4788 assert!(last.contains("# Persona (tone only)"), "{last}");
4789 assert!(last.contains("Rei Ayanami"));
4790 }
4791
4792 #[tokio::test]
4793 async fn the_first_turn_carries_the_briefing_and_later_turns_do_not() {
4794 let (tmp, talks) = store();
4795 let spec = mock_agent(tmp.path(), ECHO, BTreeMap::new());
4796 let cfg = config(spec);
4797 let mut talk = begin(&talks, &cfg, tmp.path().to_owned(), None).expect("begin");
4798
4799 say(
4800 &talks.claim_turn(&talk.id).unwrap().unwrap(),
4801 &mut talk,
4802 &talks,
4803 &cfg,
4804 "what does the queue module do?",
4805 Vec::new(),
4806 )
4807 .await
4808 .expect("first turn");
4809 let first_prompt = &talk.turns[1].body;
4810 assert!(first_prompt.contains("magi task add --solo"));
4811 assert!(first_prompt.contains("what does the queue module do?"));
4812
4813 say(
4814 &talks.claim_turn(&talk.id).unwrap().unwrap(),
4815 &mut talk,
4816 &talks,
4817 &cfg,
4818 "and how is it locked?",
4819 Vec::new(),
4820 )
4821 .await
4822 .expect("second turn");
4823 let second_prompt = &talk.turns[3].body;
4824 assert!(
4825 !second_prompt.contains("magi task add --solo"),
4826 "the briefing is sent once, not on every turn: {second_prompt}"
4827 );
4828 assert!(second_prompt.contains("and how is it locked?"));
4829 }
4830
4831 #[tokio::test]
4832 async fn switching_agent_resets_the_seat_notes_it_and_resends_the_transcript() {
4833 let (tmp, talks) = store();
4834 let a = mock_agent(tmp.path(), ECHO, BTreeMap::new());
4835 let mut b = a.clone();
4836 b.id = "other".to_owned();
4837 let mut cfg = config(a.clone());
4838 cfg.agents.push(b.clone());
4839 let mut talk = begin(&talks, &cfg, tmp.path().to_owned(), Some(&a.id)).expect("begin");
4840 say(
4841 &talks.claim_turn(&talk.id).unwrap().unwrap(),
4842 &mut talk,
4843 &talks,
4844 &cfg,
4845 "remember the walrus",
4846 Vec::new(),
4847 )
4848 .await
4849 .expect("first turn");
4850 let old_session = talk.seat.claude_session.clone();
4851 assert_eq!(talk.seat.turns, 1);
4852
4853 assert!(switch_agent(&mut talk, &talks, &b).expect("switch"));
4854 assert_eq!(talk.agent, "other");
4855 assert_eq!(talk.seat.turns, 0);
4856 assert_eq!(talk.seat.agent, "other");
4857 assert_ne!(talk.seat.claude_session, old_session);
4858 let note = talk.turns.last().expect("note");
4859 assert_eq!(note.who, Who::Agent);
4860 assert!(note.body.starts_with(MAGI_NOTE), "{}", note.body);
4861 assert!(note.body.contains("changed from"), "{}", note.body);
4862 assert_eq!(talks.get(&talk.id).expect("reload").agent, "other");
4863
4864 let before = talk.turns.len();
4865 assert!(!switch_agent(&mut talk, &talks, &b).expect("same agent"));
4866 assert_eq!(talk.turns.len(), before, "a no-op writes no note");
4867
4868 say(
4869 &talks.claim_turn(&talk.id).unwrap().unwrap(),
4870 &mut talk,
4871 &talks,
4872 &cfg,
4873 "what did I say?",
4874 Vec::new(),
4875 )
4876 .await
4877 .expect("turn after switch");
4878 let prompt = &talk.turns.last().expect("reply").body;
4879 assert!(prompt.contains("remember the walrus"), "{prompt}");
4880 assert!(prompt.contains("## magi"), "{prompt}");
4881 assert!(prompt.contains("what did I say?"), "{prompt}");
4882 }
4883
4884 #[tokio::test]
4885 async fn say_appends_the_operator_turn_then_the_agent_turn() {
4886 let (tmp, talks) = store();
4887 let spec = mock_agent(tmp.path(), REPLY, env("go ahead"));
4888 let cfg = config(spec);
4889 let mut talk = begin(&talks, &cfg, tmp.path().to_owned(), None).expect("begin");
4890
4891 say(
4892 &talks.claim_turn(&talk.id).unwrap().unwrap(),
4893 &mut talk,
4894 &talks,
4895 &cfg,
4896 "can I rename this function?",
4897 Vec::new(),
4898 )
4899 .await
4900 .expect("say");
4901
4902 assert_eq!(talk.turns.len(), 2);
4903 assert_eq!(talk.turns[0].who, Who::Operator);
4904 assert_eq!(talk.turns[0].body, "can I rename this function?");
4905 assert_eq!(talk.turns[1].who, Who::Agent);
4906 assert_eq!(talk.turns[1].body, "go ahead");
4907 assert_eq!(talks.get(&talk.id).expect("get").turns, talk.turns);
4908 }
4909
4910 #[tokio::test]
4911 async fn a_failed_turn_keeps_the_operator_message_and_says_what_happened() {
4912 let (tmp, talks) = store();
4913 let spec = mock_agent(tmp.path(), BROKEN, BTreeMap::new());
4914 let cfg = config(spec);
4915 let mut talk = begin(&talks, &cfg, tmp.path().to_owned(), None).expect("begin");
4916
4917 talk.posts_read_through = None;
4918 talks.put(&mut talk).expect("save old-format boundary");
4919 post(&talks, &talk.id, "unread task result", "task").expect("post");
4920 talk = talks.get(&talk.id).expect("reload");
4921
4922 let err = say(
4923 &talks.claim_turn(&talk.id).unwrap().unwrap(),
4924 &mut talk,
4925 &talks,
4926 &cfg,
4927 "check the tests",
4928 Vec::new(),
4929 )
4930 .await
4931 .expect_err("a turn with no answer is an error");
4932 assert!(err.to_string().contains("no answer"), "{err}");
4933
4934 let on_disk = talks.get(&talk.id).expect("get");
4935 assert_eq!(on_disk.turns.len(), 3);
4936 assert_eq!(on_disk.posts_read_through, Some(0));
4937 assert!(
4938 posts_since_last_reply(&on_disk)
4939 .unwrap()
4940 .contains("unread task result")
4941 );
4942 assert_eq!(on_disk.turns[1].body, "check the tests");
4943 let note = &on_disk.turns[2];
4944 assert_eq!(note.who, Who::Agent);
4945 assert!(note.body.starts_with(MAGI_NOTE), "{}", note.body);
4946 assert!(note.body.contains("your message is saved"));
4947 }
4948
4949 #[tokio::test]
4955 async fn a_passing_write_failure_while_saving_the_reply_does_not_lose_it() {
4956 let (tmp, talks) = store();
4957 let spec = mock_agent(tmp.path(), REPLY, env("go ahead"));
4958 let cfg = config(spec);
4959 let mut talk = begin(&talks, &cfg, tmp.path().to_owned(), None).expect("begin");
4960
4961 let text =
4962 record(&mut talk, &talks, "can I rename this function?", Vec::new()).expect("record");
4963 failpoint::force_put_failures(PUT_RETRIES - 1);
4966 respond(
4967 &talks.claim_turn(&talk.id).unwrap().unwrap(),
4968 &mut talk,
4969 &talks,
4970 &cfg,
4971 &text,
4972 )
4973 .await
4974 .expect("respond must survive a write failure its own retries can outlast");
4975
4976 assert_eq!(talk.turns.len(), 2);
4977 assert_eq!(talk.turns[1].who, Who::Agent);
4978 assert_eq!(talk.turns[1].body, "go ahead");
4979 let on_disk = talks.get(&talk.id).expect("get");
4980 assert_eq!(
4981 on_disk.turns, talk.turns,
4982 "the reply must reach disk despite the early write failures"
4983 );
4984 }
4985
4986 #[tokio::test]
4992 async fn a_persistent_write_failure_while_saving_the_reply_is_never_silent() {
4993 let (tmp, talks) = store();
4994 let spec = mock_agent(tmp.path(), REPLY, env("go ahead"));
4995 let cfg = config(spec);
4996 let mut talk = begin(&talks, &cfg, tmp.path().to_owned(), None).expect("begin");
4997
4998 let text = record(&mut talk, &talks, "check the tests", Vec::new()).expect("record");
4999 failpoint::force_put_failures(PUT_RETRIES);
5004 let err = respond(
5005 &talks.claim_turn(&talk.id).unwrap().unwrap(),
5006 &mut talk,
5007 &talks,
5008 &cfg,
5009 &text,
5010 )
5011 .await
5012 .expect_err("a reply that cannot be saved must be reported, not swallowed");
5013 assert!(err.to_string().contains("could not be saved"), "{err}");
5014
5015 let on_disk = talks.get(&talk.id).expect("get");
5016 assert_eq!(
5017 on_disk.turns.len(),
5018 2,
5019 "the operator turn plus a visible note"
5020 );
5021 assert_eq!(on_disk.turns[0].body, "check the tests");
5022 let note = &on_disk.turns[1];
5023 assert_eq!(note.who, Who::Agent);
5024 assert!(note.body.starts_with(MAGI_NOTE), "{}", note.body);
5025 assert!(
5026 note.body.contains("could not be saved"),
5027 "the operator must be told the reply is missing, not left staring \
5028 at a gap with no explanation: {}",
5029 note.body
5030 );
5031 assert_eq!(
5032 talk.turns, on_disk.turns,
5033 "the in-memory talk must match what actually landed on disk"
5034 );
5035
5036 let artifacts = talks.artifacts_of(&talk.id);
5039 let stash = std::fs::read_dir(&artifacts)
5040 .expect("artifacts dir")
5041 .filter_map(|e| e.ok())
5042 .find(|e| e.file_name().to_string_lossy().ends_with("-lost.txt"))
5043 .expect("a stash file for the lost reply");
5044 let stashed = std::fs::read_to_string(stash.path()).expect("read stash");
5045 assert_eq!(stashed, "go ahead");
5046
5047 assert_eq!(
5055 on_disk.seat.turns, 1,
5056 "the note's write must carry the turn the CLI actually took"
5057 );
5058 assert_eq!(
5059 on_disk.seat.claude_session, talk.seat.claude_session,
5060 "the session id handed to the CLI must survive the failed reply"
5061 );
5062 assert_eq!(on_disk.seat.captured_session, talk.seat.captured_session);
5063 assert!(
5064 agent::has_session(AgentKind::Command, &on_disk.seat, cfg.graph.sessions),
5065 "the next turn must resume, not open the same session id twice"
5066 );
5067 }
5068
5069 #[tokio::test]
5074 async fn a_write_failure_that_also_loses_the_note_still_reports_it() {
5075 let (tmp, talks) = store();
5076 let spec = mock_agent(tmp.path(), REPLY, env("go ahead"));
5077 let cfg = config(spec);
5078 let mut talk = begin(&talks, &cfg, tmp.path().to_owned(), None).expect("begin");
5079
5080 let text = record(&mut talk, &talks, "check the tests", Vec::new()).expect("record");
5081 failpoint::force_put_failures(PUT_RETRIES * 2);
5084 let err = respond(
5085 &talks.claim_turn(&talk.id).unwrap().unwrap(),
5086 &mut talk,
5087 &talks,
5088 &cfg,
5089 &text,
5090 )
5091 .await
5092 .expect_err("neither the reply nor the note could be saved");
5093 assert!(err.to_string().contains("could not be saved"), "{err}");
5094
5095 assert_eq!(talk.turns.len(), 1, "only the operator's own turn");
5096 let on_disk = talks.get(&talk.id).expect("get");
5097 assert_eq!(on_disk.turns.len(), 1);
5098
5099 assert_eq!(
5109 on_disk.seat.turns, 0,
5110 "an unwritable file cannot record the turn the CLI took"
5111 );
5112 assert_eq!(
5113 talk.seat.turns, 1,
5114 "the in-memory seat still reports the turn the CLI actually took"
5115 );
5116 assert_eq!(
5117 on_disk.seat.claude_session, talk.seat.claude_session,
5118 "the session id was minted at `begin` and never changes here"
5119 );
5120 }
5121
5122 #[tokio::test]
5126 async fn attachments_reach_the_prompt_and_an_empty_body_is_still_a_turn() {
5127 let (tmp, talks) = store();
5128 let spec = mock_agent(tmp.path(), ECHO, BTreeMap::new());
5129 let cfg = config(spec);
5130 let mut talk = begin(&talks, &cfg, tmp.path().to_owned(), None).expect("begin");
5131
5132 let att = talks
5133 .put_attachment(
5134 &talk.id,
5135 "image/png",
5136 "screenshot.png",
5137 b"pretend-png-bytes",
5138 )
5139 .expect("put attachment");
5140
5141 say(
5142 &talks.claim_turn(&talk.id).unwrap().unwrap(),
5143 &mut talk,
5144 &talks,
5145 &cfg,
5146 "",
5147 vec![att.clone()],
5148 )
5149 .await
5150 .expect("an empty body with an attachment is still a turn");
5151
5152 let operator_turn = &talk.turns[0];
5153 assert_eq!(operator_turn.who, Who::Operator);
5154 assert_eq!(operator_turn.body, "");
5155 assert_eq!(operator_turn.attachments, vec![att.clone()]);
5156
5157 let prompt = &talk.turns[1].body;
5158 let expected_path = talks
5159 .attachments_dir(&talk.id)
5160 .join(format!("{}.png", att.id));
5161 assert!(
5162 prompt.contains(&expected_path.display().to_string()),
5163 "the agent must be told the attachment's absolute path: {prompt}"
5164 );
5165 assert!(prompt.contains("image/png"), "and its mime: {prompt}");
5166 }
5167
5168 #[test]
5177 fn attachment_path_is_absolute_even_when_the_store_root_is_relative() {
5178 let talks = Talks::at(PathBuf::from("relative-talks-root-for-this-test"));
5179 let att = Attachment {
5180 id: "0".repeat(32),
5181 name: "shot.png".to_owned(),
5182 mime: "image/png".to_owned(),
5183 bytes: 3,
5184 };
5185 let path = talks
5186 .attachment_path("some-talk-id", &att)
5187 .expect("a supported mime always yields a path");
5188 assert!(
5189 path.is_absolute(),
5190 "must be absolute even off a relative store root: {}",
5191 path.display()
5192 );
5193 }
5194
5195 #[tokio::test]
5196 async fn a_turn_past_the_configured_talk_timeout_is_reported_with_that_timeout() {
5197 let (tmp, talks) = store();
5202 let slow = mock_agent(
5203 tmp.path(),
5204 "#!/bin/sh\ncat >/dev/null\nsleep 2\n",
5205 BTreeMap::new(),
5206 );
5207 let mut cfg = config(slow);
5208 cfg.graph.timeout_talk = 1;
5209 let mut talk = begin(&talks, &cfg, tmp.path().to_owned(), None).expect("begin");
5210
5211 let err = say(
5212 &talks.claim_turn(&talk.id).unwrap().unwrap(),
5213 &mut talk,
5214 &talks,
5215 &cfg,
5216 "check the tests",
5217 Vec::new(),
5218 )
5219 .await
5220 .expect_err("a turn that never answers is an error");
5221 assert!(
5222 err.to_string().contains("did not answer within 1s"),
5223 "{err}"
5224 );
5225
5226 let on_disk = talks.get(&talk.id).expect("get");
5227 let note = on_disk.turns.last().expect("a note turn was recorded");
5228 assert!(
5229 note.body.contains("did not answer within 1s"),
5230 "the transcript must show the configured timeout: {}",
5231 note.body
5232 );
5233 }
5234
5235 #[test]
5236 fn closing_is_idempotent_and_a_closed_talk_takes_no_more_turns() {
5237 let (tmp, talks) = store();
5238 let spec = mock_agent(tmp.path(), REPLY, env("hi"));
5239 let cfg = config(spec);
5240 let mut talk = begin(&talks, &cfg, tmp.path().to_owned(), None).expect("begin");
5241
5242 close(&mut talk, &talks).expect("close");
5243 assert_eq!(talk.status, TalkStatus::Closed);
5244 close(&mut talk, &talks).expect("closing twice is not an error");
5245
5246 let err =
5247 record(&mut talk, &talks, "still there?", Vec::new()).expect_err("closed talks refuse");
5248 assert!(err.to_string().contains("closed"));
5249 let _ = &cfg; }
5251
5252 #[tokio::test]
5253 async fn a_close_that_lands_while_a_turn_is_in_flight_is_not_undone_by_the_reply() {
5254 let (tmp, talks) = store();
5255 let spec = mock_agent(tmp.path(), REPLY, env("here you go"));
5256 let cfg = config(spec);
5257 let mut in_flight = begin(&talks, &cfg, tmp.path().to_owned(), None).expect("begin");
5260
5261 let mut closed_elsewhere = talks.get(&in_flight.id).expect("reread");
5265 close(&mut closed_elsewhere, &talks).expect("close");
5266 assert_eq!(
5267 talks.get(&in_flight.id).expect("reread").status,
5268 TalkStatus::Closed,
5269 "the close landed on disk before the turn finished"
5270 );
5271
5272 assert_eq!(in_flight.status, TalkStatus::Open);
5276 respond(
5277 &talks.claim_turn(&in_flight.id).unwrap().unwrap(),
5278 &mut in_flight,
5279 &talks,
5280 &cfg,
5281 "one more question",
5282 )
5283 .await
5284 .expect("the turn itself still completes");
5285
5286 let on_disk = talks.get(&in_flight.id).expect("reread");
5287 assert_eq!(
5288 on_disk.status,
5289 TalkStatus::Closed,
5290 "a close must stick even when a turn that started before it finishes after it"
5291 );
5292 assert!(
5295 on_disk.turns.iter().any(|t| t.body == "here you go"),
5296 "the in-flight turn's own reply is still recorded: {:?}",
5297 on_disk.turns
5298 );
5299 }
5300
5301 #[test]
5302 fn a_close_that_lands_before_record_is_called_is_not_undone_by_it() {
5303 let (tmp, talks) = store();
5304 let spec = mock_agent(tmp.path(), REPLY, env("hi"));
5305 let cfg = config(spec);
5306 let mut stale = begin(&talks, &cfg, tmp.path().to_owned(), None).expect("begin");
5309
5310 let mut closed_elsewhere = talks.get(&stale.id).expect("reread");
5313 close(&mut closed_elsewhere, &talks).expect("close");
5314 assert_eq!(
5315 talks.get(&stale.id).expect("reread").status,
5316 TalkStatus::Closed,
5317 "the close landed on disk before record was called"
5318 );
5319
5320 assert_eq!(stale.status, TalkStatus::Open);
5324 let err = record(&mut stale, &talks, "still there?", Vec::new())
5325 .expect_err("a close that landed first must be honored, not overwritten");
5326 assert!(err.to_string().contains("closed"));
5327
5328 let on_disk = talks.get(&stale.id).expect("reread");
5329 assert_eq!(
5330 on_disk.status,
5331 TalkStatus::Closed,
5332 "record must not resurrect a conversation closed while its snapshot was stale"
5333 );
5334 assert!(
5335 on_disk.turns.is_empty(),
5336 "the rejected turn must not have been appended: {:?}",
5337 on_disk.turns
5338 );
5339 let _ = &cfg; }
5341
5342 #[test]
5343 fn close_blocks_on_records_guard_rather_than_interleaving_with_it() {
5344 let (tmp, talks) = store();
5345 let spec = mock_agent(tmp.path(), REPLY, env("hi"));
5346 let cfg = config(spec);
5347 let talk = begin(&talks, &cfg, tmp.path().to_owned(), None).expect("begin");
5348
5349 let held = talks.guard().unwrap();
5353
5354 let talks2 = talks.clone();
5355 let id = talk.id.clone();
5356 let closing = std::thread::spawn(move || {
5357 let mut talk = talks2.get(&id).expect("get");
5358 close(&mut talk, &talks2).expect("close");
5359 });
5360
5361 std::thread::sleep(Duration::from_millis(50));
5362 assert!(
5363 !closing.is_finished(),
5364 "close must wait for the guard, not read and write while it is held - \
5365 a re-read alone narrows this window without closing it"
5366 );
5367
5368 drop(held);
5369 closing.join().expect("close thread panicked");
5370
5371 assert_eq!(
5372 talks.get(&talk.id).expect("reread").status,
5373 TalkStatus::Closed,
5374 "once the guard is free, close still lands"
5375 );
5376 let _ = &cfg; }
5378
5379 #[test]
5380 fn reopening_a_closed_talk_lets_it_take_turns_again_and_reopening_twice_is_not_an_error() {
5381 let (tmp, talks) = store();
5382 let spec = mock_agent(tmp.path(), REPLY, env("hi"));
5383 let cfg = config(spec);
5384 let mut talk = begin(&talks, &cfg, tmp.path().to_owned(), None).expect("begin");
5385
5386 close(&mut talk, &talks).expect("close");
5387 assert_eq!(talk.status, TalkStatus::Closed);
5388
5389 reopen(&mut talk, &talks).expect("reopen");
5390 assert_eq!(talk.status, TalkStatus::Open);
5391 assert_eq!(
5392 talks.get(&talk.id).expect("reread").status,
5393 TalkStatus::Open
5394 );
5395
5396 reopen(&mut talk, &talks).expect("reopening an open talk is not an error");
5398 assert_eq!(talk.status, TalkStatus::Open);
5399
5400 record(&mut talk, &talks, "one more thing", Vec::new())
5401 .expect("a reopened talk takes turns again");
5402 let _ = &cfg; }
5404
5405 #[test]
5406 fn removing_a_talk_deletes_its_record_and_artifacts_and_refuses_an_unknown_id() {
5407 let (tmp, talks) = store();
5408 let spec = mock_agent(tmp.path(), REPLY, env("hi"));
5409 let cfg = config(spec);
5410 let talk = begin(&talks, &cfg, tmp.path().to_owned(), None).expect("begin");
5411
5412 let artifacts = talks.artifacts_of(&talk.id);
5413 std::fs::create_dir_all(&artifacts).expect("create artifacts dir");
5414 std::fs::write(artifacts.join("turn-1.txt"), "hello").expect("write artifact");
5415
5416 talks.remove(&talk.id).expect("remove");
5417 assert!(!talks.path_of(&talk.id).is_file(), "the record is gone");
5418 assert!(!artifacts.is_dir(), "the artifacts directory is gone");
5419 assert!(
5420 talks.get(&talk.id).is_err(),
5421 "a removed talk cannot be read back"
5422 );
5423
5424 let err = talks
5425 .remove("nonexistent-id")
5426 .expect_err("unknown id refused");
5427 assert!(err.to_string().contains("no talk matches"), "{err}");
5428 let _ = &cfg; }
5430
5431 #[tokio::test]
5432 async fn a_delete_that_lands_while_a_turn_is_in_flight_is_not_undone_by_the_reply() {
5433 let (tmp, talks) = store();
5434 let spec = mock_agent(tmp.path(), REPLY, env("here you go"));
5435 let cfg = config(spec);
5436 let mut in_flight = begin(&talks, &cfg, tmp.path().to_owned(), None).expect("begin");
5439
5440 talks.remove(&in_flight.id).expect("remove");
5441 assert!(
5442 talks.get(&in_flight.id).is_err(),
5443 "the delete landed on disk before the turn finished"
5444 );
5445
5446 respond(
5449 &talks.claim_turn(&in_flight.id).unwrap().unwrap(),
5450 &mut in_flight,
5451 &talks,
5452 &cfg,
5453 "one more question",
5454 )
5455 .await
5456 .expect("the turn itself still completes rather than erroring");
5457
5458 assert!(
5459 talks.get(&in_flight.id).is_err(),
5460 "a delete must stick even when a turn that started before it finishes after it"
5461 );
5462 }
5463
5464 #[test]
5465 fn a_delete_that_lands_before_record_is_called_is_not_undone_by_it() {
5466 let (tmp, talks) = store();
5467 let spec = mock_agent(tmp.path(), REPLY, env("hi"));
5468 let cfg = config(spec);
5469 let mut stale = begin(&talks, &cfg, tmp.path().to_owned(), None).expect("begin");
5472
5473 talks.remove(&stale.id).expect("remove");
5474
5475 let err = record(&mut stale, &talks, "still there?", Vec::new())
5479 .expect_err("a delete that landed first must be honored, not overwritten");
5480 assert!(err.to_string().contains("deleted"), "{err}");
5481
5482 assert!(
5483 talks.get(&stale.id).is_err(),
5484 "record must not resurrect a conversation deleted while its snapshot was stale"
5485 );
5486 let _ = &cfg; }
5488
5489 #[test]
5490 fn a_delete_that_lands_before_close_is_called_is_not_undone_by_it() {
5491 let (tmp, talks) = store();
5492 let spec = mock_agent(tmp.path(), REPLY, env("hi"));
5493 let cfg = config(spec);
5494 let mut stale = begin(&talks, &cfg, tmp.path().to_owned(), None).expect("begin");
5497
5498 talks.remove(&stale.id).expect("remove");
5499
5500 let err = close(&mut stale, &talks)
5504 .expect_err("a delete that landed first must be honored, not overwritten");
5505 assert!(err.to_string().contains("deleted"), "{err}");
5506
5507 assert!(
5508 talks.get(&stale.id).is_err(),
5509 "close must not resurrect a conversation deleted while its snapshot was stale"
5510 );
5511 let _ = &cfg; }
5513
5514 #[test]
5515 fn a_delete_that_lands_before_reopen_is_called_is_not_undone_by_it() {
5516 let (tmp, talks) = store();
5517 let spec = mock_agent(tmp.path(), REPLY, env("hi"));
5518 let cfg = config(spec);
5519 let mut stale = begin(&talks, &cfg, tmp.path().to_owned(), None).expect("begin");
5522 close(&mut stale, &talks).expect("close");
5523
5524 talks.remove(&stale.id).expect("remove");
5525
5526 let err = reopen(&mut stale, &talks)
5530 .expect_err("a delete that landed first must be honored, not overwritten");
5531 assert!(err.to_string().contains("deleted"), "{err}");
5532
5533 assert!(
5534 talks.get(&stale.id).is_err(),
5535 "reopen must not resurrect a conversation deleted while its snapshot was stale"
5536 );
5537 let _ = &cfg; }
5539
5540 #[test]
5541 fn list_puts_open_talks_before_closed_ones() {
5542 let (tmp, talks) = store();
5543 let make = |id: &str, status: TalkStatus| {
5544 let mut t = Talk {
5545 posts_read_through: None,
5546 pending_breaks: None,
5547 pending_messages: Vec::new(),
5548 schema: SCHEMA,
5549 id: id.to_owned(),
5550 repo: tmp.path().to_owned(),
5551 agent: "mock".to_owned(),
5552 status,
5553 turns: Vec::new(),
5554 pending: String::new(),
5555 pending_attachments: Vec::new(),
5556 fallback: false,
5557 persona: String::new(),
5558 persona_dirty: false,
5559 implementers: 1,
5560 implementers_dirty: false,
5561 notified: Vec::new(),
5562 completion_notices: Vec::new(),
5563 created_at: Timestamp::now(),
5564 updated_at: Timestamp::now(),
5565 seat: SeatState::new(SEAT, "mock", 7),
5566 };
5567 talks.put(&mut t).expect("put");
5568 };
5569 make("20260901-000000-0001", TalkStatus::Open);
5570 make("20260902-000000-0002", TalkStatus::Open);
5571 make("20260903-000000-0003", TalkStatus::Closed);
5572
5573 let ids: Vec<String> = talks.list().into_iter().map(|t| t.id).collect();
5574 assert_eq!(
5575 ids,
5576 [
5577 "20260902-000000-0002",
5578 "20260901-000000-0001",
5579 "20260903-000000-0003"
5580 ]
5581 );
5582 assert_eq!(talks.count_open(), 2);
5583 }
5584
5585 #[test]
5586 fn tasks_of_finds_only_this_talks_own_tasks() {
5587 let dir = tempfile::tempdir().expect("tempdir");
5588 let queue = Queue::at(dir.path().join("queue"));
5589
5590 let mut mine = Task::new(
5591 "rework the loader".to_owned(),
5592 "rework the loader".to_owned(),
5593 PathBuf::from("/repo"),
5594 Source::Agent {
5595 run: "20260904-014455-ab12".to_owned(),
5596 node: "chat".to_owned(),
5597 },
5598 );
5599 queue.put(&mut mine).expect("put mine");
5600
5601 let mut theirs = Task::new(
5602 "unrelated".to_owned(),
5603 "unrelated".to_owned(),
5604 PathBuf::from("/repo"),
5605 Source::Agent {
5606 run: "20260904-090000-zz99".to_owned(),
5607 node: "implement".to_owned(),
5608 },
5609 );
5610 queue.put(&mut theirs).expect("put theirs");
5611
5612 let mut human = Task::new(
5613 "typed by hand".to_owned(),
5614 "typed by hand".to_owned(),
5615 PathBuf::from("/repo"),
5616 Source::Human,
5617 );
5618 queue.put(&mut human).expect("put human");
5619
5620 let found = tasks_of(&queue, "20260904-014455-ab12");
5621 assert_eq!(found.len(), 1);
5622 assert_eq!(found[0].id, mine.id);
5623 }
5624
5625 #[test]
5626 fn the_briefing_names_solo_task_add() {
5627 let brief = briefing(Path::new("/repo"), "en", false);
5628 assert!(brief.contains("magi task add --solo"));
5629 assert!(brief.contains("/repo"));
5630 assert!(!brief.contains("Hold this conversation in"));
5631 }
5632
5633 #[test]
5639 fn the_briefing_explains_targeting_a_different_repository_by_name() {
5640 let brief = briefing(Path::new("/repo"), "en", false);
5641 assert!(brief.contains("--repo does not have to be a full path"));
5642 assert!(brief.contains("owner/repo"));
5643 assert!(brief.contains("magi repos"));
5644 assert!(brief.contains("ask the operator"));
5645 }
5646
5647 #[test]
5648 fn the_briefing_tells_the_assistant_to_pass_images_with_attach() {
5649 let brief = briefing(Path::new("/repo"), "en", false);
5650 assert!(brief.contains("--attach <path>"), "{brief}");
5651 assert!(brief.contains("deleting this conversation"), "{brief}");
5652 }
5653
5654 #[test]
5655 fn the_briefing_names_the_language_when_it_is_not_english() {
5656 let brief = briefing(Path::new("/repo"), "Japanese", false);
5657 assert!(brief.contains("Hold this conversation in Japanese"));
5658 }
5659
5660 #[test]
5661 fn the_briefing_forbids_writes_unless_the_repository_opted_in() {
5662 let read_only = briefing(Path::new("/repo"), "en", false);
5663 assert!(read_only.contains("Do not write files"));
5664 assert!(!read_only.contains("allow_write"));
5665
5666 let writable = briefing(Path::new("/repo"), "en", true);
5667 assert!(!writable.contains("Do not write files"));
5668 assert!(writable.contains("allow_write = true"));
5669 assert!(writable.contains("magi task add --solo"));
5672 assert!(writable.contains("say plainly what you"));
5673 }
5674
5675 #[test]
5676 fn the_briefing_says_the_chat_cannot_push_and_tasks_report_back() {
5677 let b = briefing_for(Path::new("/r"), "en", false, None, None, 1);
5678 assert!(
5679 b.contains("cannot send the operator a message on your own"),
5680 "{b}"
5681 );
5682 assert!(b.contains(crate::prompt::CHAT_TASK_NOTICE_HEADING));
5683 assert!(b.contains("magi talk watch --timeout 900"));
5684 assert!(b.contains("nor a successfully registered watch"));
5685 }
5686
5687 #[test]
5688 fn a_post_is_replayed_until_the_agent_answers_again() {
5689 let (_tmp, talks) = store();
5690 let mut talk = ctx_talk("mock", Vec::new());
5691 talks.put(&mut talk).expect("put");
5692 post(&talks, &talk.id, " found it ", "20260902-000000-aaaa").expect("post");
5693 let mut talk = talks.get(&talk.id).expect("get");
5694 let block = posts_since_last_reply(&talk).expect("block");
5695 assert!(block.starts_with("# Posted by a task"), "{block}");
5696 assert!(block.contains("found it"));
5697 talk.turns.push(reply("ok", None));
5698 assert!(posts_since_last_reply(&talk).is_none());
5699 assert!(post(&talks, &talk.id, " ", "r").is_err());
5700 close(&mut talk, &talks).expect("close");
5701 assert!(post(&talks, &talk.id, "late", "r").is_err());
5702 }
5703
5704 #[test]
5705 fn queue_once_accepts_a_key_a_single_time_even_after_a_drain() {
5706 let (_tmp, talks) = store();
5707 let mut talk = ctx_talk("mock", Vec::new());
5708 talks.put(&mut talk).expect("put");
5709 assert!(queue_once(&mut talk, &talks, "one", "k").expect("first"));
5710 drain(&mut talk, &talks).expect("drain");
5711 assert!(!queue_once(&mut talk, &talks, "one", "k").expect("again"));
5712 assert!(talks.get(&talk.id).expect("get").pending.is_empty());
5713 }
5714 #[test]
5715 fn sender_mixed_notifications_survive_reload_drain_and_reply() {
5716 let (_tmp, talks) = store();
5717 let mut talk = ctx_talk("mock", Vec::new());
5718 talks.put(&mut talk).unwrap();
5719 queue(&mut talk, &talks, "owner", Vec::new()).unwrap();
5720 assert!(queue_once(&mut talk, &talks, "notice", "n").unwrap());
5721 let mut talk = talks.get(&talk.id).unwrap();
5722 assert_eq!(talk.pending_messages[0].sender, Some(Sender::Operator));
5723 assert_eq!(talk.pending_messages[1].sender, Some(Sender::Magi));
5724 assert!(
5725 edit_pending_message(&mut talk, &talks, "forged", "owner\n\nnotice", &[], Some(1))
5726 .is_err()
5727 );
5728 drain(&mut talk, &talks).unwrap();
5729 let mut talk = talks.get(&talk.id).unwrap();
5730 let turn = talk.turns.last().unwrap();
5731 assert_eq!(turn.who, Who::Operator);
5732 assert_eq!(sender(turn, turn.messages.first()), Sender::Operator);
5733 assert_eq!(sender(turn, turn.messages.get(1)), Sender::Magi);
5734 let reply = resolve_reply(
5735 &talk,
5736 Some(ReplyTarget {
5737 turn: 0,
5738 message: 1,
5739 }),
5740 )
5741 .unwrap()
5742 .unwrap();
5743 assert_eq!(reply.sender, Some(Sender::Magi));
5744 assert!(!queue_once(&mut talk, &talks, "notice", "n").unwrap());
5745 }
5746
5747 #[test]
5748 fn sender_legacy_operator_prose_is_never_migrated_by_heading() {
5749 let mut turn = reply("# Task finished\n\nmagi: operator typed this", None);
5750 turn.who = Who::Operator;
5751 assert_eq!(sender(&turn, None), Sender::Operator);
5752 turn.who = Who::Agent;
5753 turn.body = "magi: failed".into();
5754 assert_eq!(sender(&turn, None), Sender::Magi);
5755 turn.body = "persona reply".into();
5756 assert_eq!(sender(&turn, None), Sender::Persona);
5757 turn.posted_by = Some("task".into());
5758 assert_eq!(sender(&turn, None), Sender::Magi);
5759 turn.posted_by = None;
5760 turn.body = "magi: actual persona words".into();
5761 turn.sender = Some(Sender::Persona);
5762 assert_eq!(sender(&turn, None), Sender::Persona);
5763 }
5764}