Skip to main content

magi/
talk.rs

1//! The standing conversation: a place to think out loud with an agent between
2//! tasks, reachable from a phone.
3//!
4//! This is a conversation that stays open. Ask a question, have the agent
5//! read a file or run a command to check something, talk through an idea,
6//! and when it is time to act, tell it to file the work rather than do it
7//! here. The conversation does not end; it is what the operator opens the
8//! next time something comes up.
9//!
10//! # Talking is not implementing
11//!
12//! Every turn here runs with `allow_write: false` by default, for a reason
13//! that is not security, but attribution. An agent that edits a checkout
14//! mid-conversation leaves a diff that belongs to no run and passed no
15//! review, and on a repository entered into magi's blind competition that
16//! makes every candidate's diff unjudgeable. That is why the default holds
17//! regardless of what a repository's own `magi.toml` says about anything
18//! else. When the operator wants a change made, the agent is told to run
19//! `magi task add --solo` ([`briefing`]) rather than reach for an editor: the
20//! change goes through magi's own queue, on the repository's own terms, and
21//! the operator can watch it happen instead of trusting that it did.
22//!
23//! `[talk] allow_write` ([`crate::config::Talk::allow_write`]) lets a
24//! specific repository opt out of that default - a dotfiles or personal
25//! config checkout that is never entered into a competition and never
26//! reviewed has nothing for the restriction to protect, and filing a task for
27//! a one-line edit there is pure overhead. Turning it on does not turn this
28//! conversation into an implementer: [`briefing`] still sends everything
29//! bigger than a small, operator-named edit to the queue, and still tells the
30//! agent to say what it changed.
31//!
32//! `--solo` rather than a plain `magi task add` is the point of pairing this
33//! module with [`crate::queue::Task::solo`]. A task that came out of a
34//! conversation the operator just had is a decision already made, not a
35//! design question worth three independent takes - so it runs through one
36//! implementer and straight into review, the way [`crate::graph::Runner`]
37//! already degrades a single-candidate run.
38//!
39//! # Shape
40//!
41//! The same split [`crate::queue`] uses: [`Talk`] is data plus pure helpers,
42//! [`Talks`] owns the I/O and is constructed with its root, so every test
43//! here drives a real store in a temp directory rather than the operator's
44//! own home.
45
46use std::path::{Path, PathBuf};
47use std::sync::{Arc, Mutex, MutexGuard, PoisonError};
48use std::time::Duration;
49
50use anyhow::{Context, Result, bail};
51use jiff::Timestamp;
52use serde::{Deserialize, Serialize};
53
54use crate::agent::{self, Invocation, SeatState};
55use crate::config::{AgentSpec, Config};
56use crate::queue::{Queue, Source, Task};
57
58/// On-disk format for a conversation. Bumped when a field's meaning changes.
59pub const SCHEMA: u32 = 1;
60
61/// Wall-clock limit for one agent turn. See [`crate::config::Graph::timeout_talk`].
62///
63/// An hour by default. This turn is expected to run several shell commands
64/// and read their output before answering one - "what does this function
65/// do", "is this still true", "run the tests and tell me" - which argues for
66/// an hour rather than the five minutes a short budget once assumed, because
67/// the thing that made a short budget matter - an operator watching a
68/// spinner - is not how this conversation gets used: the operator moves on
69/// to something else while a turn runs and checks back later, so a long turn
70/// spends a held seat, not anyone's attention.
71fn turn_timeout(cfg: &Config) -> Duration {
72    Duration::from_secs(cfg.graph.timeout_talk)
73}
74
75/// Seat name for the conversation's agent, scoping its CLI-side session away
76/// from every other seat magi ever opens.
77const SEAT: &str = "talk";
78
79/// Prefix on a turn magi wrote rather than an agent.
80const MAGI_NOTE: &str = "magi: ";
81
82/// Who said something.
83#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
84#[serde(rename_all = "lowercase")]
85pub enum Who {
86    /// The operator.
87    Operator,
88    /// The conversation's agent - or magi itself, reporting that a turn
89    /// failed. See [`MAGI_NOTE`].
90    Agent,
91}
92
93/// One image the operator attached to a turn.
94///
95/// Never carries the bytes themselves: the picture lives on disk under
96/// [`Talks::attachments_dir`], named by `id` alone. `name` is the filename
97/// the operator's browser reported, kept only for display - it never
98/// contributes to a path, which is what keeps an upload from being able to
99/// traverse outside its own directory.
100#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
101#[serde(deny_unknown_fields)]
102pub struct Attachment {
103    /// Server-minted id; also the file's stem under `attachments_dir`.
104    pub id: String,
105    /// The operator's own filename, for display only.
106    pub name: String,
107    /// Validated by `web` at upload time against a closed whitelist:
108    /// `image/png`, `image/jpeg`, `image/gif`, `image/webp`.
109    pub mime: String,
110    /// Size in bytes, so the phone can show it without a second request.
111    pub bytes: u64,
112}
113
114/// One message in the conversation.
115#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
116#[serde(deny_unknown_fields)]
117pub struct Turn {
118    /// Who wrote it.
119    pub who: Who,
120    /// What they said.
121    pub body: String,
122    /// When it was said.
123    pub at: Timestamp,
124    /// Images attached to this turn. `#[serde(default)]` so a conversation
125    /// recorded before attachments existed still reads.
126    #[serde(default)]
127    pub attachments: Vec<Attachment>,
128    /// What the agent's CLI reported reading for this reply; only ever set on
129    /// an agent reply that carried usage. `#[serde(default)]` so a
130    /// conversation recorded before this field existed still reads, which is
131    /// why [`SCHEMA`] stays put: no existing field changed meaning.
132    #[serde(default, skip_serializing_if = "Option::is_none")]
133    pub usage: Option<TurnUsage>,
134}
135
136/// Raw usage of one agent reply, stored as the CLI reported it.
137///
138/// Counts only, never a percentage: the window is configuration
139/// ([`Config::context_window`]) and the conversation's model can change, so a
140/// stored percentage would go stale the moment either did. `agent` and
141/// `model` record who read that many tokens, which is how [`context_usage`]
142/// notices the figure predates a switch.
143#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
144pub struct TurnUsage {
145    /// Input-side tokens the CLI reported - see `agent::context_tokens`.
146    pub context_tokens: u64,
147    /// Roster id that answered.
148    pub agent: String,
149    /// That agent's model at the time (`None`: the CLI's own default).
150    #[serde(default)]
151    pub model: Option<String>,
152}
153
154/// How full the conversation's context window is, as the phone shows it.
155///
156/// Derived at read time and never persisted. `None` means unknown, which the
157/// UI says in words - it is never a made-up 0.
158#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize)]
159pub struct ContextUsage {
160    /// Tokens the last agent reply's turn read, if its CLI reported any.
161    pub tokens: Option<u64>,
162    /// Window of the conversation's *current* model, if one is configured.
163    pub window: Option<u64>,
164    /// `tokens / window`, rounded down, not capped (over 100 is real news).
165    pub percent: Option<u64>,
166    /// 80% or more, judged before rounding: the conversation is getting long.
167    pub warn: bool,
168    /// The conversation's agent or model is not the one that produced
169    /// `tokens`: the figure describes the old session and the next turn
170    /// re-measures it.
171    pub since_switch: bool,
172    /// The current model, if the roster names one.
173    pub model: Option<String>,
174    /// `tokens` is a transcript-length estimate, not a CLI measurement (see
175    /// [`estimate_context_tokens`]). A measured figure is never overridden.
176    #[serde(default)]
177    pub estimated: bool,
178}
179
180/// Fraction (in percent) of the window at which [`ContextUsage::warn`] fires.
181const CONTEXT_WARN_PERCENT: u64 = 80;
182
183/// Allowance for the standing prompt when no config is readable to render
184/// the real [`briefing`].
185const STANDING_PROMPT_FALLBACK_CHARS: u64 = 7000;
186
187/// Rough estimate of the context a conversation occupies, for CLIs whose usage
188/// is cumulative and so is never measured (claude / codex / agy).
189///
190/// Counts the characters (not bytes) of every operator and agent turn, skips
191/// magi's own notes (never sent to the agent), adds `standing_chars` for the
192/// standing prompt, and divides by ~3.5 chars per token, rounding up. `None`
193/// when no turn counts: an empty conversation stays unknown.
194///
195/// Known biases: CJK runs near one token per character, and tool output,
196/// images and the CLI's own system prompt are not counted, so this tends to
197/// under-estimate (the 80% warning comes late); a compacted session is still
198/// counted in full, which over-estimates.
199pub fn estimate_context_tokens(talk: &Talk, standing_chars: u64) -> Option<u64> {
200    let mut counted = false;
201    let mut chars = standing_chars;
202    for t in talk.turns.iter().filter(|t| !t.body.starts_with(MAGI_NOTE)) {
203        counted = true;
204        chars += t.body.chars().count() as u64;
205    }
206    // chars / 3.5, rounded up, in integers.
207    counted.then(|| (chars * 2).div_ceil(7))
208}
209
210/// Context usage of `talk`, measured against its current model.
211///
212/// Only the latest agent reply counts (magi's own notes are skipped) and a
213/// reply without usage makes the answer unknown - older replies are never
214/// consulted, since a stale count passed off as current is worse than "unknown".
215/// The window comes from the *current* agent's model, so switching model moves
216/// the denominator at once.
217///
218/// Switching agent (or model, which is an agent change) mints a fresh CLI
219/// session, so the next turn re-sends the whole transcript and the count
220/// resets or jumps. Until that turn lands, the old figure is reported with
221/// `since_switch` set. Deterministic: same talk and config, same answer.
222pub fn context_usage(talk: &Talk, cfg: Option<&Config>) -> ContextUsage {
223    let current = cfg.and_then(|c| c.agents.iter().find(|a| a.id == talk.agent));
224    let model = current.and_then(|a| a.model.clone());
225    let window = cfg
226        .zip(model.as_deref())
227        .and_then(|(c, m)| c.context_window(m))
228        .filter(|w| *w > 0);
229    let usage = talk
230        .turns
231        .iter()
232        .rev()
233        .find(|t| t.who == Who::Agent && !t.body.starts_with(MAGI_NOTE))
234        .and_then(|t| t.usage.as_ref());
235    let measured = usage.map(|u| u.context_tokens);
236    let tokens = measured.or_else(|| {
237        let standing = cfg.map_or(STANDING_PROMPT_FALLBACK_CHARS, |c| {
238            briefing(&talk.repo, &c.graph.language, c.talk.allow_write)
239                .chars()
240                .count() as u64
241        });
242        estimate_context_tokens(talk, standing)
243    });
244    let estimated = measured.is_none() && tokens.is_some();
245    let since_switch =
246        usage.is_some_and(|u| u.agent != talk.agent || (current.is_some() && u.model != model));
247    let (percent, warn) = match (tokens, window) {
248        (Some(t), Some(w)) => (
249            Some(t.saturating_mul(100) / w),
250            t.saturating_mul(100) >= w.saturating_mul(CONTEXT_WARN_PERCENT),
251        ),
252        _ => (None, false),
253    };
254    ContextUsage {
255        tokens,
256        window,
257        percent,
258        warn,
259        since_switch,
260        model,
261        estimated,
262    }
263}
264
265/// Where a conversation is in its life: this conversation can file any
266/// number of tasks without ending, so it only ever moves once, from open to
267/// closed.
268#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
269#[serde(rename_all = "lowercase")]
270pub enum TalkStatus {
271    /// Still open; the operator may say more, and may have already filed work
272    /// out of it.
273    Open,
274    /// Closed by hand. Kept on disk as a record.
275    Closed,
276}
277
278impl TalkStatus {
279    /// Is this conversation still live?
280    pub fn open(self) -> bool {
281        matches!(self, Self::Open)
282    }
283
284    /// Wire form, for the phone and for logs.
285    pub fn as_str(self) -> &'static str {
286        match self {
287            Self::Open => "open",
288            Self::Closed => "closed",
289        }
290    }
291}
292
293/// One standing conversation.
294#[derive(Debug, Clone, Serialize, Deserialize)]
295#[serde(deny_unknown_fields)]
296pub struct Talk {
297    /// On-disk format version.
298    pub schema: u32,
299    /// Conversation id, e.g. `20260904-014455-ab12`.
300    pub id: String,
301    /// Repository this conversation is about.
302    pub repo: PathBuf,
303    /// Roster agent id holding the conversation.
304    pub agent: String,
305    /// Current state.
306    pub status: TalkStatus,
307    /// Everything said, oldest first.
308    pub turns: Vec<Turn>,
309    /// Text and attachments accepted while the single CLI turn is busy.
310    /// They are durable, but become a real turn only when [`drain`] records them.
311    #[serde(default)]
312    pub pending: String,
313    /// Attachments paired with [`Self::pending`].
314    #[serde(default)]
315    pub pending_attachments: Vec<Attachment>,
316    /// May a failed turn fall back through the rest of `[roles] chatter`?
317    /// True only while the agent was chosen by that chain; an explicit
318    /// `--agent` or an operator's switch pins the conversation to its agent
319    /// even when that agent also appears in the chain.
320    #[serde(default)]
321    pub fallback: bool,
322    /// When the conversation was opened.
323    pub created_at: Timestamp,
324    /// Last change to this file.
325    pub updated_at: Timestamp,
326    /// The CLI-side conversation, so a turn after the first costs one
327    /// sentence instead of the whole transcript. Not `pub`: it is magi's
328    /// bookkeeping, and a caller that edited it would detach the record from
329    /// the conversation the model actually holds.
330    seat: SeatState,
331}
332
333impl Talk {
334    /// Short form used in lists and notifications, matching a run's short id.
335    pub fn short(&self) -> &str {
336        short(&self.id)
337    }
338}
339
340/// A conversation store on disk.
341#[derive(Debug, Clone)]
342pub struct Talks {
343    root: PathBuf,
344    /// Serializes the read-modify-write cycle that reads a talk, decides
345    /// something from its `status`, and writes the whole record back.
346    /// [`close`], [`record`] and the tail of [`turn`] all take this before
347    /// that cycle rather than after just the read: a re-read narrows the
348    /// window another writer can land in, but does not close it, since
349    /// nothing stopped that other writer's own put from landing between this
350    /// call's re-read and its own put. Shared across every clone, since every
351    /// clone is a handle onto the same files.
352    lock: Arc<Mutex<()>>,
353}
354
355impl Talks {
356    /// The operator's conversations, `<home>/talks`.
357    pub fn open() -> Self {
358        Self::at(crate::run::home().join("talks"))
359    }
360
361    /// A store at an explicit root. Tests use this, which is why none of them
362    /// need the operator's real home.
363    pub fn at(root: PathBuf) -> Self {
364        Self {
365            root,
366            lock: Arc::new(Mutex::new(())),
367        }
368    }
369
370    /// Claim the right to read-modify-write a talk's `status`. A plain
371    /// `std::sync::Mutex`, not an async one: every caller holds it across a
372    /// handful of small file operations and never across an `.await`, so
373    /// blocking the thread briefly is the right tool, not a reason to reach
374    /// for `tokio::sync::Mutex`. Poisoning recovers rather than propagates -
375    /// one panicking caller must not wedge every talk in the store the way it
376    /// would wedge the loop's own lock; see [`crate::web`]'s `lock_or_recover`,
377    /// which this mirrors.
378    fn guard(&self) -> MutexGuard<'_, ()> {
379        self.lock.lock().unwrap_or_else(PoisonError::into_inner)
380    }
381
382    /// Directory holding the conversation files.
383    pub fn root(&self) -> &Path {
384        &self.root
385    }
386
387    /// Path for one conversation id.
388    pub fn path_of(&self, id: &str) -> PathBuf {
389        self.root.join(format!("{id}.json"))
390    }
391
392    /// Where one conversation's prompts and CLI output are kept, beside the
393    /// record rather than inside it.
394    pub fn artifacts_of(&self, id: &str) -> PathBuf {
395        self.root.join(format!("{id}.artifacts"))
396    }
397
398    /// Where this conversation's attached images live: a subdirectory of
399    /// `artifacts_of`, so deleting the conversation deletes its attachments
400    /// too and nothing here needs its own cleanup path.
401    pub fn attachments_dir(&self, id: &str) -> PathBuf {
402        self.artifacts_of(id).join("attachments")
403    }
404
405    /// Persist one already-validated attachment and return its metadata.
406    ///
407    /// `web::talk_attachment_post` is the only caller: it has already
408    /// checked `mime` against the whitelist and sniffed the bytes, so an
409    /// unrecognised mime reaching here is a bug in that caller, not
410    /// something an operator did. The id is minted here and never taken
411    /// from the client; `name` is stored for display only and never used to
412    /// build a path.
413    pub fn put_attachment(
414        &self,
415        id: &str,
416        mime: &str,
417        name: &str,
418        data: &[u8],
419    ) -> Result<Attachment> {
420        let dir = self.attachments_dir(id);
421        std::fs::create_dir_all(&dir).with_context(|| format!("create {}", dir.display()))?;
422        let ext = attachment_ext(mime).with_context(|| format!("unsupported mime `{mime}`"))?;
423        let att = Attachment {
424            id: new_attachment_id(),
425            name: name.to_owned(),
426            mime: mime.to_owned(),
427            bytes: data.len() as u64,
428        };
429        std::fs::write(dir.join(format!("{}.{ext}", att.id)), data)
430            .with_context(|| format!("write attachment {}", att.id))?;
431        std::fs::write(
432            dir.join(format!("{}.json", att.id)),
433            serde_json::to_string(&att).context("serialize attachment")?,
434        )
435        .with_context(|| format!("write attachment metadata {}", att.id))?;
436        Ok(att)
437    }
438
439    /// Just the metadata, without reading the image bytes back off disk -
440    /// what `web::talk_say` uses to turn an id the operator referenced into
441    /// an [`Attachment`] before appending a [`Turn`], where the bytes
442    /// themselves are of no interest. `None` for an id this conversation
443    /// never stored - including one that merely looks plausible:
444    /// [`valid_attachment_id`] is checked here too, not only by the caller,
445    /// the same defence-in-depth `Questions::panel_asset` uses for its own
446    /// asset ids.
447    pub fn attachment_meta(&self, id: &str, att_id: &str) -> Result<Option<Attachment>> {
448        if !valid_attachment_id(att_id) {
449            return Ok(None);
450        }
451        let meta_path = self.attachments_dir(id).join(format!("{att_id}.json"));
452        if !meta_path.is_file() {
453            return Ok(None);
454        }
455        let att = serde_json::from_str(
456            &std::fs::read_to_string(&meta_path)
457                .with_context(|| format!("read {}", meta_path.display()))?,
458        )
459        .with_context(|| format!("parse {}", meta_path.display()))?;
460        Ok(Some(att))
461    }
462
463    /// A stored attachment's metadata and its bytes together, for serving it
464    /// back on `GET`. `None` under the same conditions as
465    /// [`Talks::attachment_meta`], which this is built on.
466    pub fn read_attachment(&self, id: &str, att_id: &str) -> Result<Option<(Attachment, Vec<u8>)>> {
467        let Some(att) = self.attachment_meta(id, att_id)? else {
468            return Ok(None);
469        };
470        let ext = attachment_ext(&att.mime).with_context(|| {
471            format!("attachment {att_id} has an unsupported mime `{}`", att.mime)
472        })?;
473        let data_path = self.attachments_dir(id).join(format!("{att_id}.{ext}"));
474        let data =
475            std::fs::read(&data_path).with_context(|| format!("read {}", data_path.display()))?;
476        Ok(Some((att, data)))
477    }
478
479    /// Absolute path of one attachment's bytes, for the prompt note [`turn`]
480    /// appends and for [`Invocation::attachments`]. `None` only for a mime
481    /// [`put_attachment`] could never have written, which means the
482    /// attachment did not come from this store.
483    ///
484    /// `self.root` (and so `attachments_dir`) is not guaranteed absolute on
485    /// its own - `run::home()` returns a bare relative `PathBuf` verbatim
486    /// when the operator sets `MAGI_HOME` to a relative path, and nothing
487    /// canonicalizes it on the way in. That is harmless for every other use
488    /// of this store, since its own I/O runs in this process against this
489    /// process's cwd - but this path is handed to a CLI invoked with `cwd:
490    /// &talk.repo`, a different directory, so a relative path here would
491    /// resolve against the wrong place once it reached the prompt.
492    /// `std::path::absolute` fixes it against *this* process's cwd before
493    /// that happens; see `disk::free_bytes_by_os` for the same function used
494    /// the same way elsewhere in this codebase.
495    fn attachment_path(&self, id: &str, att: &Attachment) -> Option<PathBuf> {
496        let ext = attachment_ext(&att.mime)?;
497        let path = self.attachments_dir(id).join(format!("{}.{ext}", att.id));
498        std::path::absolute(&path).ok()
499    }
500
501    /// Write a conversation, atomically, so a process killed mid-write leaves
502    /// the previous state readable rather than a truncated file.
503    ///
504    /// The write-then-rename itself is retried a handful of times - see
505    /// [`write_atomic`] - because a reader with the destination file briefly
506    /// open is exactly the kind of failure that must not cost an agent's
507    /// whole reply; see `turn`'s own tail for what happens when even that is
508    /// not enough.
509    pub fn put(&self, t: &mut Talk) -> Result<()> {
510        std::fs::create_dir_all(&self.root)
511            .with_context(|| format!("create {}", self.root.display()))?;
512        t.updated_at = Timestamp::now();
513        let body = serde_json::to_string_pretty(t).context("serialize talk")?;
514        let path = self.path_of(&t.id);
515        let tmp = path.with_extension("json.tmp");
516        write_atomic(&tmp, &path, &body)
517    }
518
519    /// Load a conversation by id or unambiguous id prefix.
520    pub fn get(&self, id: &str) -> Result<Talk> {
521        let resolved = self.resolve_id(id)?;
522        read_path(&self.path_of(&resolved))
523    }
524
525    /// Every conversation on disk: open first, then newest first, so what the
526    /// operator is still using belongs above what they are done with.
527    pub fn list(&self) -> Vec<Talk> {
528        self.list_counting_unreadable().0
529    }
530
531    /// [`Talks::list`], plus how many `*.json` files could not be read, so a
532    /// caller that reports to the operator can say what it skipped.
533    pub fn list_counting_unreadable(&self) -> (Vec<Talk>, usize) {
534        let mut unreadable = 0;
535        let mut all: Vec<Talk> = std::fs::read_dir(&self.root)
536            .into_iter()
537            .flatten()
538            .flatten()
539            .map(|e| e.path())
540            .filter(|p| p.extension().is_some_and(|x| x == "json"))
541            .filter_map(|p| {
542                let talk = read_path(&p).ok();
543                if talk.is_none() {
544                    unreadable += 1;
545                }
546                talk
547            })
548            .collect();
549        all.sort_unstable_by(|a, b| {
550            let rank = |t: &Talk| u8::from(!t.status.open());
551            rank(a).cmp(&rank(b)).then_with(|| b.id.cmp(&a.id))
552        });
553        (all, unreadable)
554    }
555
556    /// Expand an id prefix to exactly one conversation id.
557    pub fn resolve_id(&self, prefix: &str) -> Result<String> {
558        if self.path_of(prefix).is_file() {
559            return Ok(prefix.to_owned());
560        }
561        let hits: Vec<String> = self
562            .list()
563            .into_iter()
564            .map(|t| t.id)
565            .filter(|id| id.starts_with(prefix) || id.ends_with(prefix))
566            .collect();
567        match hits.len() {
568            1 => Ok(hits.into_iter().next().expect("exactly one hit")),
569            0 => bail!("no talk matches `{prefix}`"),
570            _ => bail!(
571                "`{prefix}` matches {} talks: {}",
572                hits.len(),
573                hits.join(", ")
574            ),
575        }
576    }
577
578    /// Change detection token: the newest modification time in the store, in
579    /// milliseconds.
580    pub fn revision(&self) -> u64 {
581        std::fs::read_dir(&self.root)
582            .into_iter()
583            .flatten()
584            .flatten()
585            .filter_map(|e| e.metadata().ok())
586            .filter_map(|m| m.modified().ok())
587            .filter_map(|t| t.duration_since(std::time::UNIX_EPOCH).ok())
588            .map(|d| d.as_millis() as u64)
589            .max()
590            .unwrap_or(0)
591    }
592
593    /// How many conversations are still open.
594    pub fn count_open(&self) -> usize {
595        self.list().iter().filter(|t| t.status.open()).count()
596    }
597
598    /// Remove a conversation from disk, record and artifacts both. The
599    /// operator's way of saying "not just done, gone" - [`close`] alone
600    /// leaves the record as history.
601    ///
602    /// Takes [`Talks::guard`] for the same reason [`close`] does: a delete
603    /// racing a [`record`] or the tail of [`turn`] must not land between
604    /// their own read and write, or the file removed here would look, to
605    /// them, like a record that simply has not been written yet. The other
606    /// half of that story is on their side - both check under this same
607    /// guard that the record they are about to write is still there, and
608    /// give up without writing if it is not, which is what stops their `put`
609    /// from resurrecting a conversation this call already removed.
610    pub fn remove(&self, id: &str) -> Result<()> {
611        let _guard = self.guard();
612        let resolved = self.resolve_id(id)?;
613        let path = self.path_of(&resolved);
614        std::fs::remove_file(&path).with_context(|| format!("remove {}", path.display()))?;
615        let artifacts = self.artifacts_of(&resolved);
616        if artifacts.is_dir() {
617            std::fs::remove_dir_all(&artifacts)
618                .with_context(|| format!("remove {}", artifacts.display()))?;
619        }
620        Ok(())
621    }
622}
623
624/// Open a conversation. Takes no agent turn: there is no idea to answer yet,
625/// and a conversation the operator has not said anything into yet is a
626/// normal, valid thing to have sitting on the phone.
627///
628/// `agent` beats `[roles] chatter`, which beats [`agent::pick`]'s own default
629/// order (a claude seat, else the first runnable agent in roster order) when
630/// nothing names a seat at all - see `[roles] chatter`'s own doc in
631/// [`crate::config`] for why a dedicated field exists rather than reusing a
632/// judge seat.
633pub fn begin(store: &Talks, cfg: &Config, repo: PathBuf, agent: Option<&str>) -> Result<Talk> {
634    // Absolute: a relative path means the wrong repository once anything
635    // other than this process reads it back.
636    let repo = repo.canonicalize().unwrap_or(repo);
637    // An explicit agent is a chain of one; otherwise the first id of
638    // `[roles] chatter` that can run here (later ones are `turn`'s fallbacks).
639    let spec = match agent {
640        Some(id) => agent::pick(&cfg.agents, Some(id), &agent::installed)?,
641        None => agent::pick_chain(
642            &cfg.agents,
643            cfg.roles.chatter.as_ref(),
644            &agent::installed,
645            "chatter",
646        )?
647        .remove(0),
648    };
649
650    let now = Timestamp::now();
651    let mut talk = Talk {
652        schema: SCHEMA,
653        id: new_id(),
654        repo,
655        agent: spec.id.clone(),
656        status: TalkStatus::Open,
657        turns: Vec::new(),
658        pending: String::new(),
659        pending_attachments: Vec::new(),
660        fallback: agent.is_none(),
661        created_at: now,
662        updated_at: now,
663        seat: SeatState::new(SEAT, &spec.id, crate::rng::entropy()),
664    };
665    store.put(&mut talk)?;
666    Ok(talk)
667}
668
669/// Append the operator's turn and flush it, without invoking anything.
670///
671/// Split out of [`say`] so `POST /api/talks/{id}/say` can answer once the
672/// message is safely on disk, and run the agent's half in the background -
673/// holding the connection for a turn that can run fifteen minutes is the
674/// wrong shape for a phone.
675pub fn record(
676    talk: &mut Talk,
677    store: &Talks,
678    text: &str,
679    attachments: Vec<Attachment>,
680) -> Result<String> {
681    // `web::talk_say` reads the talk, then awaits config discovery before
682    // calling this - a gap a concurrent `POST /api/talks/{id}/close` can land
683    // in. The guard held for the rest of this function is what actually closes
684    // that gap: re-reading status without it only shrinks the window a
685    // concurrent `close` could land in between this call's own read and its
686    // `put`, it does not remove it. See [`Talks::guard`] and the matching
687    // guard in `turn`, which this mirrors.
688    let _guard = store.guard();
689    // A concurrent `Talks::remove` can have landed in that same gap. `put`
690    // writes unconditionally, so trusting the stale `talk` here would recreate
691    // the file a delete just removed - the record must still be there for a
692    // turn to have anywhere to append to.
693    let Ok(fresh) = store.get(&talk.id) else {
694        bail!("talk {} was deleted", talk.short());
695    };
696    talk.status = fresh.status;
697    // Do not let this older handle overwrite a draft accepted while it was
698    // waiting for configuration discovery.
699    talk.pending = fresh.pending;
700    talk.pending_attachments = fresh.pending_attachments;
701    if !talk.status.open() {
702        bail!(
703            "talk {} is {} and takes no more turns",
704            talk.short(),
705            talk.status.as_str()
706        );
707    }
708    let text = text.trim();
709    if text.is_empty() && attachments.is_empty() {
710        bail!("nothing to say");
711    }
712    talk.turns.push(Turn {
713        who: Who::Operator,
714        body: text.to_owned(),
715        at: Timestamp::now(),
716        attachments,
717        usage: None,
718    });
719    store.put(talk)?;
720    Ok(text.to_owned())
721}
722
723/// Add an unrecorded message to the durable draft while another turn runs.
724pub fn queue(
725    talk: &mut Talk,
726    store: &Talks,
727    text: &str,
728    attachments: Vec<Attachment>,
729) -> Result<()> {
730    let text = text.trim();
731    if text.is_empty() && attachments.is_empty() {
732        bail!("nothing to say");
733    }
734    let _guard = store.guard();
735    let mut fresh = store
736        .get(&talk.id)
737        .with_context(|| format!("talk {} was deleted", talk.short()))?;
738    if !fresh.status.open() {
739        bail!(
740            "talk {} is {} and takes no more turns",
741            fresh.short(),
742            fresh.status.as_str()
743        );
744    }
745    if !text.is_empty() {
746        if fresh.pending.is_empty() {
747            fresh.pending = text.to_owned();
748        } else {
749            fresh.pending.push_str("\n\n");
750            fresh.pending.push_str(text);
751        }
752    }
753    fresh.pending_attachments.extend(attachments);
754    store.put(&mut fresh)?;
755    *talk = fresh;
756    Ok(())
757}
758
759/// Promote the current durable draft to one operator turn.
760pub fn drain(talk: &mut Talk, store: &Talks) -> Result<Option<String>> {
761    let _guard = store.guard();
762    let mut fresh = store
763        .get(&talk.id)
764        .with_context(|| format!("talk {} was deleted", talk.short()))?;
765    if !fresh.status.open() || (fresh.pending.is_empty() && fresh.pending_attachments.is_empty()) {
766        *talk = fresh;
767        return Ok(None);
768    }
769    let text = std::mem::take(&mut fresh.pending);
770    let attachments = std::mem::take(&mut fresh.pending_attachments);
771    fresh.turns.push(Turn {
772        who: Who::Operator,
773        body: text.clone(),
774        at: Timestamp::now(),
775        attachments,
776        usage: None,
777    });
778    store.put(&mut fresh)?;
779    *talk = fresh;
780    Ok(Some(text))
781}
782
783/// One operator turn and one agent turn, appended - the synchronous form, used
784/// by tests and by anything that is fine waiting out the turn itself.
785pub async fn say(
786    talk: &mut Talk,
787    store: &Talks,
788    cfg: &Config,
789    text: &str,
790    attachments: Vec<Attachment>,
791) -> Result<()> {
792    let text = record(talk, store, text, attachments)?;
793    turn(talk, store, cfg, &text).await
794}
795
796/// The agent's half of a turn: invoke, append, flush. Pairs with [`record`].
797pub async fn respond(talk: &mut Talk, store: &Talks, cfg: &Config, text: &str) -> Result<()> {
798    turn(talk, store, cfg, text).await
799}
800
801/// Close a conversation. Idempotent: closing an already-closed conversation is
802/// not an error, since the operator's intent - "I am done with this" - is
803/// already satisfied.
804///
805/// Re-reads the record under [`Talks::guard`] rather than trusting the
806/// caller's copy of `talk`, and writes that fresh copy back rather than the
807/// one passed in. `web::talk_close` loads `talk` and calls this right after
808/// with no gap of its own, but without the guard that load can still land
809/// between a `record` or `turn` elsewhere reading the file and writing it
810/// back - and a close built on the older snapshot would put it right back,
811/// silently dropping whatever turn the other call had just appended.
812///
813/// If the re-read fails, this errors rather than falling back to the
814/// caller's stale copy: `talk::begin` always `put`s the record before handing
815/// out a `Talk`, so the only way a re-read can fail is a concurrent
816/// [`Talks::remove`] having deleted it, and writing the stale copy back would
817/// resurrect exactly what that delete removed.
818pub fn close(talk: &mut Talk, store: &Talks) -> Result<()> {
819    let _guard = store.guard();
820    let mut fresh = store
821        .get(&talk.id)
822        .with_context(|| format!("talk {} was deleted", talk.short()))?;
823    fresh.status = TalkStatus::Closed;
824    // A closed conversation must not replay a draft if it is reopened later.
825    fresh.pending.clear();
826    fresh.pending_attachments.clear();
827    store.put(&mut fresh)?;
828    *talk = fresh;
829    Ok(())
830}
831
832/// Reopen a closed conversation. Idempotent for the same reason [`close`] is:
833/// reopening an already-open conversation is not an error, since the
834/// operator's intent - "I want to keep talking about this" - is already
835/// satisfied.
836///
837/// Written symmetrically with [`close`]: re-reads the record under
838/// [`Talks::guard`] rather than trusting the caller's copy of `talk`, writes
839/// that fresh copy back rather than the one passed in, and errors rather than
840/// falling back to the stale copy if the re-read fails, for the same reasons
841/// `close`'s doc gives.
842pub fn reopen(talk: &mut Talk, store: &Talks) -> Result<()> {
843    let _guard = store.guard();
844    let mut fresh = store
845        .get(&talk.id)
846        .with_context(|| format!("talk {} was deleted", talk.short()))?;
847    fresh.status = TalkStatus::Open;
848    store.put(&mut fresh)?;
849    *talk = fresh;
850    Ok(())
851}
852
853/// Hand the conversation to another roster agent.
854///
855/// A CLI session belongs to one CLI and cannot be carried to another, so the
856/// seat is minted afresh rather than edited: the next turn finds
857/// `seat.turns == 0` and re-sends the transcript, since the new agent has
858/// heard none of it. A magi-written note records the change in the
859/// transcript. Returns `false` (and writes nothing, not even a note) when the
860/// stored talk already uses `spec`.
861///
862/// Re-reads under [`Talks::guard`], like [`close`], and errors rather than
863/// resurrecting a record a concurrent delete removed. Refusing a closed talk
864/// or a turn in flight is the caller's job: only it can see the latter.
865pub fn switch_agent(talk: &mut Talk, store: &Talks, spec: &AgentSpec) -> Result<bool> {
866    let _guard = store.guard();
867    let mut fresh = store
868        .get(&talk.id)
869        .with_context(|| format!("talk {} was deleted", talk.short()))?;
870    if fresh.agent == spec.id {
871        *talk = fresh;
872        return Ok(false);
873    }
874    let from = std::mem::replace(&mut fresh.agent, spec.id.clone());
875    fresh.seat = SeatState::new(SEAT, &spec.id, crate::rng::entropy());
876    // A deliberate switch pins the conversation to the agent chosen.
877    fresh.fallback = false;
878    fresh.turns.push(Turn {
879        who: Who::Agent,
880        body: format!("{MAGI_NOTE}agent changed from {from} to {}", spec.id),
881        at: Timestamp::now(),
882        attachments: Vec::new(),
883        usage: None,
884    });
885    store.put(&mut fresh)?;
886    *talk = fresh;
887    Ok(true)
888}
889
890/// Discard the durable draft without adding a transcript turn.
891pub fn clear_pending(talk: &mut Talk, store: &Talks) -> Result<()> {
892    let _guard = store.guard();
893    let mut fresh = store
894        .get(&talk.id)
895        .with_context(|| format!("talk {} was deleted", talk.short()))?;
896    fresh.pending.clear();
897    fresh.pending_attachments.clear();
898    store.put(&mut fresh)?;
899    *talk = fresh;
900    Ok(())
901}
902
903/// Clear a draft only when the caller still sees its complete snapshot.
904pub fn clear_pending_if_matches(
905    talk: &mut Talk,
906    store: &Talks,
907    expected_text: &str,
908    expected_attachments: &[String],
909) -> Result<bool> {
910    let _guard = store.guard();
911    let mut fresh = store
912        .get(&talk.id)
913        .with_context(|| format!("talk {} was deleted", talk.short()))?;
914    if !pending_matches(&fresh, expected_text, expected_attachments) {
915        *talk = fresh;
916        return Ok(false);
917    }
918    fresh.pending.clear();
919    fresh.pending_attachments.clear();
920    store.put(&mut fresh)?;
921    *talk = fresh;
922    Ok(true)
923}
924
925/// Replace just the text of the durable draft, but only if the caller's
926/// snapshot still identifies the entire draft. This refuses to overwrite a
927/// message another client queued or a draft the drain already promoted.
928pub fn edit_pending_text(
929    talk: &mut Talk,
930    store: &Talks,
931    text: &str,
932    expected_text: &str,
933    expected_attachments: &[String],
934) -> Result<bool> {
935    let _guard = store.guard();
936    let mut fresh = store
937        .get(&talk.id)
938        .with_context(|| format!("talk {} was deleted", talk.short()))?;
939    if !pending_matches(&fresh, expected_text, expected_attachments) {
940        *talk = fresh;
941        return Ok(false);
942    }
943    fresh.pending = text.trim().to_owned();
944    store.put(&mut fresh)?;
945    *talk = fresh;
946    Ok(true)
947}
948
949fn pending_matches(talk: &Talk, expected_text: &str, expected_attachments: &[String]) -> bool {
950    talk.pending == expected_text
951        && talk
952            .pending_attachments
953            .iter()
954            .map(|attachment| &attachment.id)
955            .eq(expected_attachments.iter())
956}
957
958/// Invoke the conversation's agent once and append what it said.
959///
960/// The first turn ever taken carries the full [`briefing`], because nothing
961/// else has told the agent what this conversation is or what it may do.
962/// Every turn after that resends nothing when the CLI can resume its own
963/// session, and falls back to [`transcript`] only when it cannot.
964async fn turn(talk: &mut Talk, store: &Talks, cfg: &Config, text: &str) -> Result<()> {
965    let spec = cfg
966        .agents
967        .iter()
968        .find(|a| a.id == talk.agent)
969        .with_context(|| {
970            format!(
971                "talk {} was opened with agent `{}`, which is no longer in \
972                 the roster; restore it in magi.toml or start a new \
973                 conversation",
974                talk.short(),
975                talk.agent
976            )
977        })?;
978
979    // The newest turn is always the operator message this call is answering
980    // - `record` appended it before `turn` was ever called - so its own
981    // attachments are what belong at the end of *this* prompt.
982    let last_note = attachment_note(
983        store,
984        &talk.id,
985        talk.turns
986            .last()
987            .map_or(&[][..], |t| t.attachments.as_slice()),
988    );
989
990    // Every attachment this conversation has ever held, not only this
991    // turn's: a resumed session gets a fresh process every turn, so a CLI
992    // whose sandbox needs `--add-dir` (see `agent::build_command`) needs the
993    // grant again to open an image from an earlier turn, even when nothing
994    // new was attached just now.
995    let attachment_paths: Vec<PathBuf> = talk
996        .turns
997        .iter()
998        .flat_map(|t| t.attachments.iter())
999        .filter_map(|a| store.attachment_path(&talk.id, a))
1000        .collect();
1001
1002    // A question handed to this conversation is answered with `magi answer`,
1003    // which writes the question store; a read-only sandbox refuses that. So the
1004    // turn that answers a hand-over - and only that turn, never the rest of
1005    // the conversation - may write, with the question store as a writable
1006    // root. As for the deputy, what the seat may touch beyond that rests on
1007    // the prompt, not the sandbox.
1008    let consulted = talk
1009        .turns
1010        .last()
1011        .is_some_and(|t| t.who == Who::Operator && crate::consult::is_consult_text(&t.body));
1012    let consult_roots: Vec<PathBuf> = if consulted {
1013        vec![crate::ask::Questions::open().root().to_path_buf()]
1014    } else {
1015        Vec::new()
1016    };
1017
1018    let artifacts = store.artifacts_of(&talk.id);
1019    // From the transcript, not the seat: a switched agent's seat restarts at
1020    // zero and must not overwrite an earlier turn's artifacts.
1021    let operator_turns = talk.turns.iter().filter(|t| t.who == Who::Operator).count();
1022    let stem = format!("turn-{}", operator_turns.max(1));
1023    // The chat's build cache is the same shared one the graph's seats get, so
1024    // a conversation that compiles does not mint another multi-GB target dir.
1025    let cache_dir = cfg.cache_dir();
1026
1027    // The agent holding the conversation, then - only when it came from
1028    // `[roles] chatter` - the rest of that chain, each at most once.
1029    let mut chain = vec![spec.clone()];
1030    if let Some(choice) = cfg.roles.chatter.as_ref()
1031        && talk.fallback
1032    {
1033        for id in choice.ids() {
1034            if id == talk.agent || chain.iter().any(|s| s.id == id) {
1035                continue;
1036            }
1037            match agent::pick(&cfg.agents, Some(id), &agent::installed) {
1038                Ok(s) => chain.push(s),
1039                Err(e) => tracing::warn!("[roles] chatter: skipping `{id}`: {e:#}"),
1040            }
1041        }
1042    }
1043
1044    let mut outcome = None;
1045    let mut fell_back_from: Option<String> = None;
1046    // What the conversation looked like after the first agent's failed try,
1047    // so an exhausted chain leaves exactly what a single failed seat would.
1048    let mut first_try: Option<(String, SeatState)> = None;
1049    for (n, spec) in chain.iter().enumerate() {
1050        if n > 0 {
1051            if first_try.is_none() {
1052                first_try = Some((talk.agent.clone(), talk.seat.clone()));
1053            }
1054            tracing::warn!("chat: falling back from `{}` to `{}`", talk.agent, spec.id);
1055            // A new CLI has none of the old one's conversation: a fresh seat
1056            // puts `has_session` at false and the full transcript is re-sent.
1057            fell_back_from.get_or_insert_with(|| talk.agent.clone());
1058            talk.agent = spec.id.clone();
1059            talk.seat = SeatState::new(SEAT, &spec.id, crate::rng::entropy());
1060        }
1061        let resuming = agent::has_session(spec.kind, &talk.seat, cfg.graph.sessions);
1062        let first_ever = talk.turns.len() <= 1;
1063        let body = if talk.seat.turns == 0 && first_ever {
1064            format!(
1065                "{}\n\n# Operator\n\n{text}{last_note}",
1066                briefing(&talk.repo, &cfg.graph.language, cfg.talk.allow_write)
1067            )
1068        } else if talk.seat.turns == 0 {
1069            // A fresh seat on a conversation that already has history (the
1070            // agent was switched): the briefing, then everything said so far.
1071            format!(
1072                "{}\n\n{}\n\n# Operator\n\n{text}{last_note}",
1073                briefing(&talk.repo, &cfg.graph.language, cfg.talk.allow_write),
1074                transcript(talk, store)
1075            )
1076        } else if resuming {
1077            format!("{text}{last_note}")
1078        } else {
1079            format!("{}\n\n{text}{last_note}", transcript(talk, store))
1080        };
1081        let attempt_stem = if n == 0 {
1082            stem.clone()
1083        } else {
1084            format!("{stem}-{}", spec.id)
1085        };
1086        let inv = Invocation {
1087            cwd: &talk.repo,
1088            prompt: &body,
1089            timeout: turn_timeout(cfg),
1090            // Off unless this repository's own config opts in - see
1091            // `crate::config::Talk::allow_write` and this module's doc for why
1092            // the default keeps a conversational edit from landing in a checkout
1093            // no run or review can claim.
1094            allow_write: cfg.talk.allow_write || consulted,
1095            sessions: cfg.graph.sessions,
1096            artifacts: &artifacts,
1097            stem: &attempt_stem,
1098            // The conversation's own id, so `magi task add` run from inside it is
1099            // attributed to this conversation - see `Source::Agent`.
1100            run: &talk.id,
1101            node: crate::queue::CHAT_NODE,
1102            cache_dir: cache_dir.as_deref(),
1103            attachments: &attachment_paths,
1104            writable: &consult_roots,
1105        };
1106        let result = agent::invoke(spec, &mut talk.seat, &inv).await;
1107        let advance = agent::chain_advances(&result);
1108        if n == 0 || !advance {
1109            outcome = Some(result);
1110        } else {
1111            // A later failure is only logged; the note describes the first.
1112            tracing::warn!("chat: fallback agent `{}` also failed", spec.id);
1113        }
1114        if !advance {
1115            break;
1116        }
1117    }
1118    if outcome.as_ref().is_some_and(agent::chain_advances) {
1119        // Exhausted: back to the agent the conversation had, so the note
1120        // below names it and the next turn starts from it again.
1121        if let Some((id, seat)) = first_try {
1122            talk.agent = id;
1123            talk.seat = seat;
1124            fell_back_from = None;
1125        }
1126    }
1127    let outcome = outcome.expect("a chain holds at least one agent");
1128    let note = |why: String| Turn {
1129        who: Who::Agent,
1130        body: format!("{MAGI_NOTE}{why}"),
1131        at: Timestamp::now(),
1132        attachments: Vec::new(),
1133        usage: None,
1134    };
1135    let (reply, failure) = match outcome {
1136        Err(e) => (
1137            note(format!("could not run agent `{}`: {e}", talk.agent)),
1138            Some(format!("could not run agent `{}`: {e}", talk.agent)),
1139        ),
1140        Ok(out) if out.quota_exhausted() => {
1141            let reset = out
1142                .quota
1143                .as_ref()
1144                .and_then(|q| q.reset.clone())
1145                .map_or_else(String::new, |r| format!(" (resets {r})"));
1146            let why = format!(
1147                "agent `{}` is out of quota{reset}; your message is saved, so \
1148                 say it again when the window reopens",
1149                talk.agent
1150            );
1151            (note(why.clone()), Some(why))
1152        }
1153        Ok(out) if out.timed_out => {
1154            let why = format!(
1155                "agent `{}` did not answer within {}s; your message is saved",
1156                talk.agent,
1157                turn_timeout(cfg).as_secs()
1158            );
1159            (note(why.clone()), Some(why))
1160        }
1161        Ok(out) if !out.usable() => {
1162            let why = format!(
1163                "agent `{}` produced no answer (exit {}); your message is saved",
1164                talk.agent,
1165                out.exit_code
1166                    .map_or_else(|| "unknown".to_owned(), |c| c.to_string())
1167            );
1168            (note(why.clone()), Some(why))
1169        }
1170        Ok(out) => (
1171            Turn {
1172                who: Who::Agent,
1173                body: out.text.trim().to_owned(),
1174                at: Timestamp::now(),
1175                attachments: Vec::new(),
1176                // `talk.agent` is whoever actually answered: a fallback has
1177                // already moved it, and an exhausted chain never reaches here.
1178                usage: out.context_tokens.map(|context_tokens| TurnUsage {
1179                    context_tokens,
1180                    agent: talk.agent.clone(),
1181                    model: cfg
1182                        .agents
1183                        .iter()
1184                        .find(|a| a.id == talk.agent)
1185                        .and_then(|a| a.model.clone()),
1186                }),
1187            },
1188            None,
1189        ),
1190    };
1191
1192    // A close landed on disk while this turn was in flight is read back here
1193    // rather than trusted from the snapshot this call started with. `store`
1194    // holds nothing else this function does not itself own - the turn guard
1195    // in `web::Ui::begin_talk_turn` keeps `turns` and `seat` this call's
1196    // alone to mutate - but `status` is not behind that guard, and an
1197    // operator's close must stick: the whole point of ending a conversation
1198    // is that an agent's answer to the last message before the close cannot
1199    // silently reopen it. The guard is what makes that read-then-write
1200    // section atomic with `close`'s own - taken only for this tail and not
1201    // for the whole invocation above, so one talk's fifteen-minute turn does
1202    // not block another talk's close from proceeding.
1203    let _guard = store.guard();
1204    // A delete is the more final version of that same race: `put` writes
1205    // unconditionally, so a talk removed while this turn was in flight must
1206    // stay removed rather than being written back with this turn's reply
1207    // appended to it. The reply is simply given up on - there is no
1208    // conversation left for it to belong to.
1209    let Ok(fresh) = store.get(&talk.id) else {
1210        return Ok(());
1211    };
1212    talk.status = fresh.status;
1213    // `queue` may have accepted another operator message while the CLI was
1214    // running. This handle predates that write, so preserving only `status`
1215    // would overwrite the durable draft when the reply is appended below.
1216    talk.pending = fresh.pending;
1217    talk.pending_attachments = fresh.pending_attachments;
1218    if let Some(from) = fell_back_from.filter(|_| failure.is_none()) {
1219        // The switch persists: quota coming back does not move the chat
1220        // home, an operator's switch does.
1221        talk.turns.push(note(format!(
1222            "agent changed from {from} to {} (fallback)",
1223            talk.agent
1224        )));
1225    }
1226    talk.turns.push(reply);
1227    if let Err(put_err) = store.put(talk) {
1228        // `Talks::put` already retried the write itself - reaching here
1229        // means a passing race is not what this is. An agent's answer,
1230        // possibly the result of an hour-long call, must not vanish with
1231        // nothing to show for it just because the very last step failed:
1232        // pop it back off, stash its text beside the conversation, and
1233        // replace it with a note the operator can actually see, the same
1234        // mechanism the failure branches above already use for a quota or a
1235        // timeout.
1236        let lost = talk.turns.pop().expect("just pushed above");
1237        let stash = stash_lost_turn(store, &talk.id, &stem, &lost);
1238        let why = match &stash {
1239            Ok(path) => format!(
1240                "agent `{}` answered, but the reply could not be saved to \
1241                 this conversation ({put_err:#}); the raw text was kept at \
1242                 {} - your message is saved, ask again",
1243                talk.agent,
1244                path.display()
1245            ),
1246            Err(stash_err) => format!(
1247                "agent `{}` answered, but the reply could not be saved to \
1248                 this conversation ({put_err:#}), and it could not be kept \
1249                 anywhere else either ({stash_err:#}); your message is \
1250                 saved, ask again",
1251                talk.agent
1252            ),
1253        };
1254        talk.turns.push(note(why.clone()));
1255        // Writing the note also carries the seat this call already advanced -
1256        // `agent::invoke` incremented `turns` and, for a vendor that reports
1257        // its own session id, recorded that too. That is what keeps the next
1258        // turn resuming the session the CLI is already holding instead of
1259        // re-opening it, so losing the reply costs the transcript a turn but
1260        // not the conversation.
1261        return match store.put(talk) {
1262            Ok(()) => bail!("{why}"),
1263            Err(note_err) => {
1264                // Even the short note failed to save, which means this
1265                // conversation's file cannot be written at all right now -
1266                // nothing is left for this call to retry or record. Pop the
1267                // note so `talk.turns` matches the transcript on disk, and
1268                // surface both failures for whoever reads the log.
1269                //
1270                // `talk.seat` is deliberately not wound back to match. The
1271                // CLI really did take the turn and really did consume this
1272                // seat's session id; pretending otherwise would be a second
1273                // untruth on top of the unwritable file, and the handle is
1274                // reloaded from disk by the next `drain` or `get` anyway -
1275                // see `web::drain_loop`. What the seat cannot do is reach
1276                // disk, so the record stays a turn behind the CLI until some
1277                // later write lands, and a turn taken before then re-opens a
1278                // session id the CLI already holds. That is the desync
1279                // `20260907-011805-fb57` is about, and tolerating it belongs
1280                // there rather than here: no write this branch could make
1281                // would help, since a failed write is exactly what put it in
1282                // this position twice over.
1283                talk.turns.pop();
1284                Err(note_err).context(why)
1285            }
1286        };
1287    }
1288
1289    match failure {
1290        Some(why) => bail!("{why}"),
1291        None => Ok(()),
1292    }
1293}
1294
1295/// Everything said so far, as prose, for a CLI that cannot resume its own
1296/// conversation.
1297fn transcript(talk: &Talk, store: &Talks) -> String {
1298    let mut out = String::from(
1299        "This conversation cannot resume on the CLI's side, so here is \
1300         everything said so far; answer only the last message.\n",
1301    );
1302    for t in &talk.turns {
1303        let who = match t.who {
1304            Who::Operator => "operator",
1305            Who::Agent if t.body.starts_with(MAGI_NOTE) => "magi",
1306            Who::Agent => "you",
1307        };
1308        out.push_str(&format!("\n## {who}\n\n{}\n", t.body.trim()));
1309        out.push_str(&attachment_note(store, &talk.id, &t.attachments));
1310    }
1311    out
1312}
1313
1314/// The section named at the end of a turn's body, listing every attachment's
1315/// absolute path and mime so the agent knows exactly what to open. Empty
1316/// when `attachments` is, which is every turn but the rare one carrying an
1317/// image, so a turn with none changes nothing about the prompt.
1318fn attachment_note(store: &Talks, talk_id: &str, attachments: &[Attachment]) -> String {
1319    if attachments.is_empty() {
1320        return String::new();
1321    }
1322    let mut out = String::from(
1323        "\n\nThe operator attached the image(s) below to this message. Open \
1324         and look at each one before you answer.\n",
1325    );
1326    for att in attachments {
1327        if let Some(path) = store.attachment_path(talk_id, att) {
1328            out.push_str(&format!("\n- {} ({})", path.display(), att.mime));
1329        }
1330    }
1331    out.push('\n');
1332    out
1333}
1334
1335/// The briefing the agent opens with, sent once as part of its first turn.
1336///
1337/// Pure, so the properties that matter can be asserted without an interview:
1338/// it names `magi task add --solo` (the route this conversation always has to
1339/// changing anything) and it never tells the agent to write a task *file* of
1340/// its own - that would compete with filing through the queue.
1341/// `allow_write` only ever adds an extra permission on top of that; it never
1342/// removes the queue as an option, which is why both branches keep the same
1343/// `# When the operator wants something done` section - `write_policy` is
1344/// the only part that changes.
1345///
1346/// It also tells the agent that `--repo` is not stuck naming this
1347/// conversation's own directory: `resolve_repo` (`src/main.rs`) now accepts a
1348/// short `owner/repo` or bare `repo` name and resolves it against
1349/// `[repos] roots`, the same local checkouts `magi repos` lists. Without this
1350/// line an agent asked to change some other repository has no way to know
1351/// that option exists, and the only path it can see - asking the operator to
1352/// dictate a full path - is exactly the friction this change exists to
1353/// remove. A miss or an ambiguous name still fails the command outright, so
1354/// the instruction is to ask rather than guess when that happens - the
1355/// silent-decision line this task must not cross.
1356pub fn briefing(repo: &Path, language: &str, allow_write: bool) -> String {
1357    let write_policy = if allow_write {
1358        "Write access is enabled for this conversation (`allow_write = \
1359         true`), so you may write files - but only a small, \
1360         already-decided edit the operator names outright in this \
1361         conversation, not an implementation. This is a permission on the \
1362         conversation as a whole, not a property of whichever repository \
1363         it happened to start in: if the operator names a different \
1364         repository for that small edit, the policy allows it there too. \
1365         Your own tool may still confine writes to the repository this \
1366         conversation started in regardless - if a write elsewhere is \
1367         refused, say so plainly rather than working around it. Once you \
1368         have made an edit, say plainly what you edited. Anything bigger, \
1369         or anything still open-ended, still goes through the queue below \
1370         rather than being done here."
1371    } else {
1372        "Do not write files. Implementing a change is not this \
1373         conversation's job; a separate, blind competition of agents does \
1374         that, and a repository this conversation has already edited would \
1375         make their diffs unjudgeable."
1376    };
1377    let mut out = format!(
1378        "You are magi's standing conversation partner for its operator, who \
1379         usually has this open on a phone. Keep replies short: no preamble, \
1380         no restating what they just said.\n\n\
1381         # Repository\n\n{repo}\n\n\
1382         You may look around: read files, run shell commands, search history, \
1383         run tests - whatever answers the question. {write_policy}\n\n\
1384         A short, command-shaped message (\"list\", \"info <id>\", \"show \
1385         3cbf\") is almost always the operator asking you to look something \
1386         up, not an instruction to file - answer it yourself with `magi \
1387         list`, `magi show <id>`, `magi task list`, or the like, the same way \
1388         you would answer any other question in this conversation.\n\n\
1389         # When the operator wants something done\n\n\
1390         Run:\n\n\
1391         magi task add --solo --repo {repo} <instruction>\n\n\
1392         and tell the operator the task id it prints, so they can follow it \
1393         from the Queue. If it refuses with a duplicate warning (the \
1394         instruction names a branch, commit or pull request that an \
1395         unfinished task, run or PR already owns), do not repeat it with \
1396         --force yourself: tell the operator what it matched and let them \
1397         decide. Write <instruction> so that an implementer who has \
1398         never seen this conversation can act on it alone - it is everything \
1399         they get. Use --solo: it runs the task through one implementer \
1400         straight into review instead of the usual multi-agent competition, \
1401         which is the right shape for a change this conversation has already \
1402         settled, rather than one still worth several independent takes.\n\n\
1403         If the operator asks for something in a different repository, \
1404         --repo does not have to be a full path: --repo owner/repo (or just \
1405         repo, when that is unambiguous) is resolved against local checkouts \
1406         the same way `magi repos` lists them. If the command fails because \
1407         nothing matches or more than one checkout shares that name, ask the \
1408         operator which repository they mean (or run `magi repos` yourself \
1409         to see the candidates) rather than guessing.\n\n\
1410         The current state of the code is whatever origin/main holds, not \
1411         whatever a working tree shows: a primary checkout often lags \
1412         upstream, sits on a detached HEAD and carries uncommitted changes. \
1413         Before answering about code, run `git fetch origin` in that \
1414         repository if it is cheap, then read through \
1415         `git show origin/main:<path>` or `git grep <pattern> origin/main`. \
1416         If the working tree differs, say so; if the fetch fails, say that \
1417         too, so the operator knows the answer may be stale.\n\n\
1418         If the operator attached an image (a screenshot, say) that the task \
1419         is about, pass it with `--attach <path>`, using the absolute path \
1420         the turn's attachment note gives; repeat the flag for several. \
1421         `magi task add --solo --attach <path> <instruction>` copies the \
1422         file into the task, so the implementer receives it. Do not paste the \
1423         path into <instruction> instead: deleting this conversation deletes \
1424         its attachments, and then that path reaches no one.\n",
1425        repo = repo.display(),
1426    );
1427    out.push_str(&language_note(language));
1428    out
1429}
1430
1431/// The operator is talking, so their language matters here more than in most
1432/// prompts magi sends.
1433fn language_note(language: &str) -> String {
1434    if language.trim().is_empty() || language.eq_ignore_ascii_case("en") {
1435        String::new()
1436    } else {
1437        format!("\nHold this conversation in {language}.\n")
1438    }
1439}
1440
1441/// Queue tasks this conversation has filed, oldest first.
1442///
1443/// A task is this conversation's when its [`Source::Agent`] names this
1444/// conversation's id as `run` - which is exactly what happens when
1445/// `magi task add` is run from inside a turn, because [`turn`] passes the
1446/// conversation's own id as [`Invocation::run`].
1447pub fn tasks_of(queue: &Queue, talk_id: &str) -> Vec<Task> {
1448    let mut tasks: Vec<Task> = queue
1449        .list()
1450        .into_iter()
1451        .filter(|t| matches!(&t.source, Source::Agent { run, .. } if run == talk_id))
1452        .collect();
1453    tasks.sort_unstable_by(|a, b| a.id.cmp(&b.id));
1454    tasks
1455}
1456
1457fn read_path(path: &Path) -> Result<Talk> {
1458    let body = std::fs::read_to_string(path).with_context(|| format!("read {}", path.display()))?;
1459    serde_json::from_str(&body).with_context(|| format!("parse {}", path.display()))
1460}
1461
1462/// How many times [`write_atomic`] retries a failed write-then-rename before
1463/// giving up.
1464const PUT_RETRIES: u32 = 5;
1465
1466/// Write `body` to `tmp` and rename it onto `path`, retrying the whole thing
1467/// a handful of times with a short sleep in between.
1468///
1469/// The only failure this is meant to absorb is a passing one - most
1470/// concretely, a reader elsewhere in this process (or another `magi`
1471/// process) with `path` briefly open for `read_to_string` at the exact
1472/// moment this call tries to rename over it. That clears in milliseconds
1473/// once the reader lets go; a caller still failing after several short
1474/// sleeps has something more durable wrong (a full disk, a permissions
1475/// change) that a longer sleep would not fix either, and is left to report
1476/// it.
1477fn write_atomic(tmp: &Path, path: &Path, body: &str) -> Result<()> {
1478    let mut last_err = None;
1479    for attempt in 0..PUT_RETRIES {
1480        if attempt > 0 {
1481            std::thread::sleep(Duration::from_millis(20 * u64::from(attempt)));
1482        }
1483        match try_write_atomic(tmp, path, body) {
1484            Ok(()) => return Ok(()),
1485            Err(e) => last_err = Some(e),
1486        }
1487    }
1488    Err(last_err.expect("the loop above always runs at least once"))
1489}
1490
1491fn try_write_atomic(tmp: &Path, path: &Path, body: &str) -> Result<()> {
1492    #[cfg(test)]
1493    if failpoint::take_forced_put_failure() {
1494        bail!("simulated write failure (test)");
1495    }
1496    std::fs::write(tmp, body).with_context(|| format!("write {}", tmp.display()))?;
1497    std::fs::rename(tmp, path).with_context(|| format!("replace {}", path.display()))?;
1498    Ok(())
1499}
1500
1501/// Last resort when `turn`'s own `store.put` fails even after
1502/// [`write_atomic`]'s retries: keep the generated text somewhere still
1503/// findable rather than let the whole of an agent's answer disappear along
1504/// with the write that was supposed to record it.
1505fn stash_lost_turn(store: &Talks, id: &str, stem: &str, reply: &Turn) -> Result<PathBuf> {
1506    let dir = store.artifacts_of(id);
1507    std::fs::create_dir_all(&dir).with_context(|| format!("create {}", dir.display()))?;
1508    let path = dir.join(format!("{stem}-lost.txt"));
1509    std::fs::write(&path, &reply.body).with_context(|| format!("write {}", path.display()))?;
1510    Ok(path)
1511}
1512
1513/// A test-only seam that lets [`try_write_atomic`] simulate the kind of
1514/// passing I/O race [`write_atomic`] is meant to retry through, without
1515/// depending on real OS-level file-locking behaviour, which differs across
1516/// the three platforms this crate ships on (and, on the one platform where a
1517/// reader really does block a rename, is awkward to trigger deterministically
1518/// in a unit test).
1519#[cfg(test)]
1520mod failpoint {
1521    use std::cell::Cell;
1522
1523    thread_local! {
1524        static FORCE_PUT_FAILURES: Cell<u32> = const { Cell::new(0) };
1525    }
1526
1527    /// Arrange for the next `count` calls into [`super::try_write_atomic`] to
1528    /// fail before touching the filesystem at all.
1529    pub(super) fn force_put_failures(count: u32) {
1530        FORCE_PUT_FAILURES.with(|c| c.set(count));
1531    }
1532
1533    /// Consumed once per attempt inside [`super::try_write_atomic`]; `true`
1534    /// means simulate this attempt failing.
1535    pub(super) fn take_forced_put_failure() -> bool {
1536        FORCE_PUT_FAILURES.with(|c| {
1537            let n = c.get();
1538            if n == 0 {
1539                false
1540            } else {
1541                c.set(n - 1);
1542                true
1543            }
1544        })
1545    }
1546}
1547
1548fn short(id: &str) -> &str {
1549    id.split('-').next_back().unwrap_or(id)
1550}
1551
1552fn new_id() -> String {
1553    let stamp = jiff::Zoned::now().strftime("%Y%m%d-%H%M%S");
1554    let seed = crate::rng::entropy();
1555    format!("{stamp}-{:04x}", (seed ^ (seed >> 32)) & 0xffff)
1556}
1557
1558/// Extension an attachment's bytes are stored under, from its (already
1559/// validated) mime. The one place this mapping exists on the write side;
1560/// `web`'s own whitelist is what actually decides which mimes are accepted
1561/// in the first place.
1562fn attachment_ext(mime: &str) -> Option<&'static str> {
1563    match mime {
1564        "image/png" => Some("png"),
1565        "image/jpeg" => Some("jpg"),
1566        "image/gif" => Some("gif"),
1567        "image/webp" => Some("webp"),
1568        _ => None,
1569    }
1570}
1571
1572/// Is `id` a shape [`put_attachment`](Talks::put_attachment) could have
1573/// produced? 32 lowercase hex digits and nothing else, checked before an id
1574/// that came from the client is ever allowed to build a path - so `..` and a
1575/// path separator are never even possible.
1576pub fn valid_attachment_id(id: &str) -> bool {
1577    id.len() == 32
1578        && id
1579            .bytes()
1580            .all(|b| b.is_ascii_digit() || (b'a'..=b'f').contains(&b))
1581}
1582
1583/// A fresh attachment id: 128 bits of process entropy as lowercase hex - the
1584/// same "mint it, never take it from the client" rule [`new_id`] follows for
1585/// conversation ids.
1586fn new_attachment_id() -> String {
1587    let mut r = crate::rng::SplitMix64::new(crate::rng::entropy());
1588    format!("{:016x}{:016x}", r.next_u64(), r.next_u64())
1589}
1590
1591#[cfg(test)]
1592mod tests {
1593    #[test]
1594    fn the_briefing_points_at_origin_main_not_the_working_tree() {
1595        let b = briefing(Path::new("/r"), "en", false);
1596        assert!(b.contains("origin/main"));
1597        assert!(b.contains("git show origin/main:"));
1598    }
1599    use std::collections::BTreeMap;
1600
1601    use crate::config::{AgentChoice, AgentKind, AgentSpec, Graph};
1602    use crate::queue::{Queue, Source, Task};
1603
1604    use super::*;
1605
1606    fn ctx_agent(id: &str, model: Option<&str>) -> AgentSpec {
1607        AgentSpec {
1608            id: id.to_owned(),
1609            kind: AgentKind::Command,
1610            model: model.map(str::to_owned),
1611            command: Vec::new(),
1612            extra_args: Vec::new(),
1613            env: BTreeMap::new(),
1614            prompt_delivery: None,
1615        }
1616    }
1617
1618    fn ctx_talk(agent: &str, turns: Vec<Turn>) -> Talk {
1619        Talk {
1620            schema: SCHEMA,
1621            id: "20260904-014455-ab12".to_owned(),
1622            repo: PathBuf::from("."),
1623            agent: agent.to_owned(),
1624            status: TalkStatus::Open,
1625            turns,
1626            pending: String::new(),
1627            pending_attachments: Vec::new(),
1628            fallback: false,
1629            created_at: Timestamp::now(),
1630            updated_at: Timestamp::now(),
1631            seat: SeatState::new(SEAT, agent, 1),
1632        }
1633    }
1634
1635    fn reply(body: &str, usage: Option<(u64, &str, Option<&str>)>) -> Turn {
1636        Turn {
1637            who: Who::Agent,
1638            body: body.to_owned(),
1639            at: Timestamp::now(),
1640            attachments: Vec::new(),
1641            usage: usage.map(|(t, a, m)| TurnUsage {
1642                context_tokens: t,
1643                agent: a.to_owned(),
1644                model: m.map(str::to_owned),
1645            }),
1646        }
1647    }
1648
1649    fn ctx_config(windows: &[(&str, u64)]) -> Config {
1650        Config {
1651            agents: vec![
1652                ctx_agent("small", Some("small-model")),
1653                ctx_agent("big", Some("big-model")),
1654                ctx_agent("plain", None),
1655            ],
1656            context_windows: windows.iter().map(|(k, v)| ((*k).to_owned(), *v)).collect(),
1657            ..Config::default()
1658        }
1659    }
1660
1661    #[test]
1662    fn context_usage_computes_percent_and_warns_at_eighty() {
1663        let cfg = ctx_config(&[("small-model", 1000)]);
1664        let at = |tokens| {
1665            let t = ctx_talk(
1666                "small",
1667                vec![reply("hi", Some((tokens, "small", Some("small-model"))))],
1668            );
1669            context_usage(&t, Some(&cfg))
1670        };
1671        let u = at(799);
1672        assert_eq!((u.percent, u.warn, u.window), (Some(79), false, Some(1000)));
1673        let u = at(800);
1674        assert_eq!((u.percent, u.warn), (Some(80), true));
1675        let u = at(1500);
1676        assert_eq!((u.percent, u.warn), (Some(150), true));
1677        assert!(!u.since_switch);
1678    }
1679
1680    #[test]
1681    fn context_usage_is_unknown_without_usage_and_never_looks_back() {
1682        let cfg = ctx_config(&[("small-model", 1000)]);
1683        let t = ctx_talk(
1684            "small",
1685            vec![
1686                reply("old", Some((900, "small", Some("small-model")))),
1687                reply("new", None),
1688            ],
1689        );
1690        let u = context_usage(&t, Some(&cfg));
1691        // No measurement in the latest reply: an estimate, never the stale 900.
1692        assert!(u.estimated);
1693        assert_ne!(u.tokens, Some(900));
1694        assert!(u.tokens.is_some());
1695        // A magi note after the reply neither hides nor replaces it.
1696        let t = ctx_talk(
1697            "small",
1698            vec![
1699                reply("old", Some((900, "small", Some("small-model")))),
1700                reply("magi: could not run agent", None),
1701            ],
1702        );
1703        assert_eq!(context_usage(&t, Some(&cfg)).tokens, Some(900));
1704        assert_eq!(
1705            context_usage(&ctx_talk("small", Vec::new()), Some(&cfg)).tokens,
1706            None
1707        );
1708    }
1709
1710    #[test]
1711    fn estimate_counts_chars_both_sides_and_standing_prompt() {
1712        let mut t = ctx_talk("small", vec![reply("abcdefg", None)]);
1713        assert_eq!(estimate_context_tokens(&t, 0), Some(2)); // 7 chars -> 2
1714        let op = Turn {
1715            who: Who::Operator,
1716            ..reply("abcdefg", None)
1717        };
1718        t.turns.push(op);
1719        assert_eq!(estimate_context_tokens(&t, 0), Some(4));
1720        assert!(
1721            estimate_context_tokens(&t, 700).unwrap() > estimate_context_tokens(&t, 0).unwrap()
1722        );
1723        // Characters, not bytes: 7 kanji are 7 chars.
1724        let ja = ctx_talk("small", vec![reply("日本語日本語日", None)]);
1725        assert_eq!(estimate_context_tokens(&ja, 0), Some(2));
1726        // magi notes are not sent to the agent; with nothing else, unknown.
1727        let note = ctx_talk("small", vec![reply("magi: could not run agent", None)]);
1728        assert_eq!(estimate_context_tokens(&note, 1000), None);
1729        assert_eq!(
1730            estimate_context_tokens(&ctx_talk("small", Vec::new()), 1000),
1731            None
1732        );
1733    }
1734
1735    #[test]
1736    fn context_usage_measured_wins_and_estimate_gets_percent_and_warn() {
1737        let cfg = ctx_config(&[("small-model", 1000)]);
1738        let t = ctx_talk(
1739            "small",
1740            vec![reply(
1741                &"x".repeat(5000),
1742                Some((10, "small", Some("small-model"))),
1743            )],
1744        );
1745        let u = context_usage(&t, Some(&cfg));
1746        assert_eq!((u.tokens, u.estimated), (Some(10), false));
1747        let t = ctx_talk("small", vec![reply(&"x".repeat(5000), None)]);
1748        let u = context_usage(&t, Some(&cfg));
1749        assert!(u.estimated && !u.since_switch);
1750        assert_eq!(u.window, Some(1000));
1751        assert!(u.warn && u.percent.unwrap() >= 80);
1752        let t = ctx_talk("small", vec![reply("hi", None)]);
1753        let u = context_usage(&t, Some(&cfg));
1754        assert!(u.estimated && u.percent.is_some());
1755    }
1756
1757    #[test]
1758    fn context_usage_without_a_window_shows_tokens_only() {
1759        let cfg = ctx_config(&[]);
1760        // No model at all, and a model nothing matches.
1761        let t = ctx_talk("plain", vec![reply("hi", Some((5000, "plain", None)))]);
1762        let u = context_usage(&t, Some(&cfg));
1763        assert_eq!(
1764            (u.tokens, u.window, u.percent, u.warn),
1765            (Some(5000), None, None, false)
1766        );
1767        let t = ctx_talk(
1768            "small",
1769            vec![reply("hi", Some((5000, "small", Some("small-model"))))],
1770        );
1771        assert_eq!(context_usage(&t, Some(&cfg)).percent, None);
1772        // No readable config: same, and no panic.
1773        assert_eq!(context_usage(&t, None).window, None);
1774    }
1775
1776    #[test]
1777    fn context_usage_switching_model_changes_the_denominator() {
1778        let cfg = ctx_config(&[("small-model", 1000), ("big-model", 10_000)]);
1779        let used = reply("hi", Some((900, "small", Some("small-model"))));
1780        let before = context_usage(&ctx_talk("small", vec![used.clone()]), Some(&cfg));
1781        assert_eq!(
1782            (before.percent, before.warn, before.since_switch),
1783            (Some(90), true, false)
1784        );
1785        // Same turns, conversation now on the big model: new denominator, and
1786        // the figure is flagged as describing the previous session.
1787        let after = context_usage(&ctx_talk("big", vec![used]), Some(&cfg));
1788        assert_eq!(after.window, Some(10_000));
1789        assert_eq!(
1790            (after.percent, after.warn, after.since_switch),
1791            (Some(9), false, true)
1792        );
1793        assert_eq!(after.model.as_deref(), Some("big-model"));
1794    }
1795
1796    #[test]
1797    fn a_turn_recorded_before_usage_existed_still_reads() {
1798        let old = r#"{"who":"agent","body":"hi","at":"2026-09-04T01:44:55Z"}"#;
1799        let turn: Turn = serde_json::from_str(old).expect("old turn reads");
1800        assert!(turn.usage.is_none());
1801        let json = serde_json::to_string(&turn).expect("serialize");
1802        assert!(
1803            !json.contains("usage"),
1804            "absent usage is not written: {json}"
1805        );
1806    }
1807
1808    /// A store of its own, with no process-global state.
1809    fn store() -> (tempfile::TempDir, Talks) {
1810        let tmp = tempfile::tempdir().expect("tempdir");
1811        let talks = Talks::at(tmp.path().join("talks"));
1812        (tmp, talks)
1813    }
1814
1815    /// A `kind = "command"` agent whose whole behaviour is a POSIX shell
1816    /// script - see `chat`'s tests for why no test here may spawn a real
1817    /// agent CLI.
1818    fn mock_agent(dir: &Path, script: &str, env: BTreeMap<String, String>) -> AgentSpec {
1819        let path = dir.join("mock-talk-agent.sh");
1820        std::fs::write(&path, script).expect("write mock");
1821        AgentSpec {
1822            id: "mock".to_owned(),
1823            kind: AgentKind::Command,
1824            model: None,
1825            command: vec!["sh".to_owned(), path.to_string_lossy().into_owned()],
1826            extra_args: Vec::new(),
1827            env,
1828            prompt_delivery: None,
1829        }
1830    }
1831
1832    fn config(spec: AgentSpec) -> Config {
1833        Config {
1834            agents: vec![spec],
1835            graph: Graph {
1836                language: "en".to_owned(),
1837                ..Graph::default()
1838            },
1839            ..Config::default()
1840        }
1841    }
1842
1843    /// Echo a canned reply, ignoring the prompt on stdin.
1844    const REPLY: &str = "#!/bin/sh\ncat >/dev/null\nprintf '%s\\n' \"$MOCK_REPLY\"\n";
1845
1846    /// Say nothing and fail, the way a CLI that cannot start does.
1847    const BROKEN: &str = "#!/bin/sh\ncat >/dev/null\nexit 3\n";
1848
1849    /// Reply with the prompt it was given, so a test can inspect exactly what
1850    /// the agent received on stdin.
1851    const ECHO: &str = "#!/bin/sh\ncat\n";
1852
1853    fn env(reply: &str) -> BTreeMap<String, String> {
1854        BTreeMap::from([("MOCK_REPLY".to_owned(), reply.to_owned())])
1855    }
1856
1857    #[test]
1858    fn the_frozen_json_field_names_round_trip_through_disk() {
1859        let (tmp, talks) = store();
1860        let mut talk = Talk {
1861            schema: SCHEMA,
1862            id: "20260904-014455-ab12".to_owned(),
1863            repo: tmp.path().to_owned(),
1864            agent: "sonnet".to_owned(),
1865            status: TalkStatus::Open,
1866            turns: Vec::new(),
1867            pending: String::new(),
1868            pending_attachments: Vec::new(),
1869            fallback: false,
1870            created_at: Timestamp::now(),
1871            updated_at: Timestamp::now(),
1872            seat: SeatState::new(SEAT, "sonnet", 7),
1873        };
1874        talks.put(&mut talk).expect("put");
1875
1876        let raw = std::fs::read_to_string(talks.path_of(&talk.id)).expect("read back");
1877        let v: serde_json::Value = serde_json::from_str(&raw).expect("parse");
1878        for field in [
1879            "schema",
1880            "id",
1881            "repo",
1882            "agent",
1883            "status",
1884            "turns",
1885            "created_at",
1886            "updated_at",
1887        ] {
1888            assert!(v.get(field).is_some(), "missing field `{field}`");
1889        }
1890        assert_eq!(v["schema"], 1);
1891        assert_eq!(v["status"], "open");
1892
1893        let back = talks.get(&talk.id).expect("get");
1894        assert_eq!(back.id, talk.id);
1895        assert_eq!(back.status, TalkStatus::Open);
1896    }
1897
1898    #[test]
1899    fn opening_a_talk_takes_no_agent_turn() {
1900        let (tmp, talks) = store();
1901        // A script that would fail loudly if it were ever run: `begin` must
1902        // not invoke anything, since there is nothing yet for an agent to
1903        // answer.
1904        let spec = mock_agent(tmp.path(), BROKEN, BTreeMap::new());
1905        let cfg = config(spec);
1906
1907        let talk = begin(&talks, &cfg, tmp.path().to_owned(), None).expect("begin");
1908        assert_eq!(talk.status, TalkStatus::Open);
1909        assert!(talk.turns.is_empty(), "nothing has been said yet");
1910
1911        let on_disk = talks.get(&talk.id).expect("get");
1912        assert_eq!(on_disk.turns.len(), 0);
1913    }
1914
1915    /// `[roles] chatter`, when set, decides who holds this conversation; unset,
1916    /// it falls back to [`agent::pick`]'s own default order (a claude seat,
1917    /// else the first runnable agent in roster order) rather than to any
1918    /// other role - see `[roles] chatter`'s own doc in [`crate::config`] for
1919    /// why a dedicated field exists at all: opening this against the same
1920    /// seat as a judge is what produced the `agent ... did not answer within
1921    /// 300s` timeout that led to it.
1922    #[test]
1923    fn chatter_wins_when_set_and_falls_back_to_pick_s_default_order_otherwise() {
1924        let (tmp, talks) = store();
1925        let first_spec = mock_agent(tmp.path(), BROKEN, BTreeMap::new());
1926        let mut chatter_spec = mock_agent(tmp.path(), BROKEN, BTreeMap::new());
1927        chatter_spec.id = "chatter-mock".to_owned();
1928
1929        let mut cfg = Config {
1930            agents: vec![first_spec.clone(), chatter_spec.clone()],
1931            graph: Graph {
1932                language: "en".to_owned(),
1933                ..Graph::default()
1934            },
1935            ..Config::default()
1936        };
1937        cfg.roles.chatter = Some(chatter_spec.id.as_str().into());
1938
1939        let talk =
1940            begin(&talks, &cfg, tmp.path().to_owned(), None).expect("begin with chatter set");
1941        assert_eq!(talk.agent, chatter_spec.id, "an explicit chatter must win");
1942
1943        cfg.roles.chatter = None;
1944        let fallback =
1945            begin(&talks, &cfg, tmp.path().to_owned(), None).expect("begin with chatter unset");
1946        assert_eq!(
1947            fallback.agent, first_spec.id,
1948            "unset chatter must fall back to agent::pick's own default order"
1949        );
1950    }
1951
1952    /// A conversation recorded before attachments existed - schema 1, no
1953    /// `attachments` key on any turn - must still read.
1954    #[test]
1955    fn a_talk_recorded_without_attachments_still_reads() {
1956        let (tmp, talks) = store();
1957        let path = talks.path_of("20260904-014455-ab12");
1958        std::fs::create_dir_all(talks.root()).expect("talks dir");
1959        std::fs::write(
1960            &path,
1961            serde_json::json!({
1962                "schema": 1,
1963                "id": "20260904-014455-ab12",
1964                "repo": tmp.path(),
1965                "agent": "sonnet",
1966                "status": "open",
1967                "turns": [
1968                    { "who": "operator", "body": "still there?",
1969                      "at": Timestamp::now().to_string() },
1970                ],
1971                "created_at": Timestamp::now().to_string(),
1972                "updated_at": Timestamp::now().to_string(),
1973                "seat": SeatState::new(SEAT, "sonnet", 7),
1974            })
1975            .to_string(),
1976        )
1977        .expect("write pre-attachments talk");
1978
1979        let talk = talks.get("20260904-014455-ab12").expect("must still read");
1980        assert!(talk.turns[0].attachments.is_empty());
1981    }
1982
1983    #[test]
1984    fn queued_text_is_durable_combined_and_drained_as_one_operator_turn() {
1985        let (tmp, talks) = store();
1986        let cfg = config(mock_agent(tmp.path(), REPLY, env("reply")));
1987        let mut talk = begin(&talks, &cfg, tmp.path().to_owned(), None).expect("begin");
1988
1989        queue(&mut talk, &talks, "first", Vec::new()).expect("queue first");
1990        queue(&mut talk, &talks, "second", Vec::new()).expect("queue second");
1991        let saved = talks.get(&talk.id).expect("reload queued talk");
1992        assert_eq!(saved.pending, "first\n\nsecond");
1993        assert!(saved.turns.is_empty(), "a draft is not a transcript turn");
1994
1995        let drained = drain(&mut talk, &talks).expect("drain");
1996        assert_eq!(drained.as_deref(), Some("first\n\nsecond"));
1997        let saved = talks.get(&talk.id).expect("reload drained talk");
1998        assert!(saved.pending.is_empty());
1999        assert_eq!(saved.turns.len(), 1);
2000        assert_eq!(saved.turns[0].body, "first\n\nsecond");
2001    }
2002
2003    #[test]
2004    fn editing_a_queued_draft_preserves_its_attachments_and_rejects_a_stale_snapshot() {
2005        let (tmp, talks) = store();
2006        let cfg = config(mock_agent(tmp.path(), REPLY, env("reply")));
2007        let mut talk = begin(&talks, &cfg, tmp.path().to_owned(), None).expect("begin");
2008        let attachment = Attachment {
2009            id: "a".repeat(32),
2010            name: "shot.png".to_owned(),
2011            mime: "image/png".to_owned(),
2012            bytes: 3,
2013        };
2014
2015        queue(&mut talk, &talks, "first", vec![attachment.clone()]).expect("queue");
2016        assert!(
2017            edit_pending_text(
2018                &mut talk,
2019                &talks,
2020                "corrected",
2021                "first",
2022                std::slice::from_ref(&attachment.id),
2023            )
2024            .expect("edit")
2025        );
2026        let saved = talks.get(&talk.id).expect("reload edited draft");
2027        assert_eq!(saved.pending, "corrected");
2028        assert_eq!(saved.pending_attachments, vec![attachment]);
2029
2030        queue(&mut talk, &talks, "later", Vec::new()).expect("queue concurrent draft");
2031        assert!(
2032            !edit_pending_text(
2033                &mut talk,
2034                &talks,
2035                "stale edit",
2036                "corrected",
2037                &["a".repeat(32)],
2038            )
2039            .expect("stale edit is a conflict")
2040        );
2041        assert_eq!(
2042            talks.get(&talk.id).expect("reload after conflict").pending,
2043            "corrected\n\nlater"
2044        );
2045        assert!(
2046            !clear_pending_if_matches(&mut talk, &talks, "corrected", &["a".repeat(32)])
2047                .expect("stale clear is a conflict")
2048        );
2049        assert_eq!(
2050            talks
2051                .get(&talk.id)
2052                .expect("reload after stale clear")
2053                .pending,
2054            "corrected\n\nlater"
2055        );
2056    }
2057
2058    #[tokio::test]
2059    async fn a_reply_save_preserves_pending_accepted_while_the_cli_runs() {
2060        let (tmp, talks) = store();
2061        let slow = "#!/bin/sh\ncat >/dev/null\nsleep 0.1\nprintf reply\n";
2062        let cfg = config(mock_agent(tmp.path(), slow, BTreeMap::new()));
2063        let mut running = begin(&talks, &cfg, tmp.path().to_owned(), None).expect("begin");
2064        let id = running.id.clone();
2065        let first = record(&mut running, &talks, "first", Vec::new()).expect("record");
2066
2067        let response_talks = talks.clone();
2068        let response_cfg = cfg.clone();
2069        let reply = tokio::spawn(async move {
2070            respond(&mut running, &response_talks, &response_cfg, &first).await
2071        });
2072        tokio::time::sleep(std::time::Duration::from_millis(20)).await;
2073
2074        let mut queued = talks.get(&id).expect("queued handle");
2075        queue(&mut queued, &talks, "next", Vec::new()).expect("queue");
2076        reply.await.expect("join").expect("reply");
2077
2078        let saved = talks.get(&id).expect("reload");
2079        assert_eq!(saved.pending, "next");
2080        assert_eq!(saved.turns.len(), 2, "operator message and reply remain");
2081    }
2082
2083    /// A mock whose script counts its own calls in `<dir>/<id>.calls` before
2084    /// running `body`.
2085    fn counting_agent(dir: &Path, id: &str, body: &str) -> AgentSpec {
2086        let calls = dir.join(format!("{id}.calls"));
2087        let script = format!(
2088            "#!/bin/sh\necho x >> '{}'\n{body}\n",
2089            calls.to_string_lossy()
2090        );
2091        let path = dir.join(format!("mock-{id}.sh"));
2092        std::fs::write(&path, script).expect("write mock");
2093        AgentSpec {
2094            id: id.to_owned(),
2095            kind: AgentKind::Command,
2096            model: None,
2097            command: vec!["sh".to_owned(), path.to_string_lossy().into_owned()],
2098            extra_args: Vec::new(),
2099            env: BTreeMap::new(),
2100            prompt_delivery: None,
2101        }
2102    }
2103
2104    fn calls(dir: &Path, id: &str) -> usize {
2105        std::fs::read_to_string(dir.join(format!("{id}.calls"))).map_or(0, |s| s.lines().count())
2106    }
2107
2108    fn chain_config(specs: Vec<AgentSpec>, ids: &[&str]) -> Config {
2109        let mut cfg = config(specs[0].clone());
2110        cfg.agents = specs;
2111        cfg.roles.chatter = Some(AgentChoice::Chain(
2112            ids.iter().map(|s| (*s).to_owned()).collect(),
2113        ));
2114        cfg
2115    }
2116
2117    #[tokio::test]
2118    async fn a_chatter_chain_falls_back_resends_the_transcript_and_sticks() {
2119        let (tmp, talks) = store();
2120        let a = counting_agent(tmp.path(), "a", "cat >/dev/null\nexit 3");
2121        let b = counting_agent(tmp.path(), "b", "cat");
2122        let cfg = chain_config(vec![a, b], &["a", "b"]);
2123        let mut talk = begin(&talks, &cfg, tmp.path().to_owned(), None).expect("begin");
2124        assert_eq!(talk.agent, "a");
2125
2126        say(&mut talk, &talks, &cfg, "hello there", Vec::new())
2127            .await
2128            .expect("turn");
2129        assert_eq!(calls(tmp.path(), "a"), 1, "each id is tried once");
2130        assert_eq!(calls(tmp.path(), "b"), 1);
2131        assert_eq!(talk.agent, "b", "the switch persists");
2132        assert!(talks.get(&talk.id).unwrap().agent == "b");
2133        let reply = talk.turns.last().unwrap();
2134        assert!(reply.body.contains("hello there"));
2135        assert!(
2136            reply.body.contains("magi task add --solo"),
2137            "a fresh seat gets the full briefing"
2138        );
2139        assert!(
2140            talk.turns
2141                .iter()
2142                .any(|t| t.body.contains("agent changed from a to b")),
2143            "the switch is noted"
2144        );
2145    }
2146
2147    #[tokio::test]
2148    async fn an_exhausted_chatter_chain_fails_like_a_single_seat_and_stays_put() {
2149        let (tmp, talks) = store();
2150        let a = counting_agent(tmp.path(), "a", "cat >/dev/null\nexit 3");
2151        let b = counting_agent(tmp.path(), "b", "cat >/dev/null\nexit 4");
2152        let cfg = chain_config(vec![a, b], &["a", "b", "a"]);
2153        let mut talk = begin(&talks, &cfg, tmp.path().to_owned(), None).expect("begin");
2154
2155        let err = say(&mut talk, &talks, &cfg, "hi", Vec::new())
2156            .await
2157            .expect_err("every agent failed");
2158        assert!(err.to_string().contains("`a`"), "{err:#}");
2159        assert_eq!(calls(tmp.path(), "a"), 1);
2160        assert_eq!(calls(tmp.path(), "b"), 1);
2161        assert_eq!(talk.agent, "a", "an exhausted chain leaves the agent alone");
2162    }
2163
2164    #[test]
2165    fn a_chatter_chain_skips_an_unknown_id_at_begin() {
2166        let (tmp, talks) = store();
2167        let b = counting_agent(tmp.path(), "b", "cat");
2168        let cfg = chain_config(vec![b], &["ghost", "b"]);
2169        let talk = begin(&talks, &cfg, tmp.path().to_owned(), None).expect("begin");
2170        assert_eq!(talk.agent, "b");
2171    }
2172
2173    #[tokio::test]
2174    async fn an_explicit_agent_inside_the_chatter_chain_stays_pinned() {
2175        let (tmp, talks) = store();
2176        let a = counting_agent(tmp.path(), "a", "cat >/dev/null\nexit 3");
2177        let b = counting_agent(tmp.path(), "b", "cat");
2178        let cfg = chain_config(vec![a, b], &["a", "b"]);
2179        let mut talk = begin(&talks, &cfg, tmp.path().to_owned(), Some("a")).expect("begin");
2180        say(&mut talk, &talks, &cfg, "hi", Vec::new())
2181            .await
2182            .expect_err("a alone, and it fails");
2183        assert_eq!(calls(tmp.path(), "b"), 0);
2184        assert_eq!(talk.agent, "a");
2185    }
2186
2187    #[tokio::test]
2188    async fn an_explicit_agent_does_not_borrow_the_chatter_chain() {
2189        let (tmp, talks) = store();
2190        let a = counting_agent(tmp.path(), "a", "cat >/dev/null\nexit 3");
2191        let b = counting_agent(tmp.path(), "b", "cat");
2192        let c = counting_agent(tmp.path(), "c", "cat >/dev/null\nexit 3");
2193        let cfg = chain_config(vec![a, b, c], &["a", "b"]);
2194        let mut talk = begin(&talks, &cfg, tmp.path().to_owned(), Some("c")).expect("begin");
2195        say(&mut talk, &talks, &cfg, "hi", Vec::new())
2196            .await
2197            .expect_err("c alone, and it fails");
2198        assert_eq!(calls(tmp.path(), "b"), 0);
2199    }
2200
2201    #[tokio::test]
2202    async fn the_first_turn_carries_the_briefing_and_later_turns_do_not() {
2203        let (tmp, talks) = store();
2204        let spec = mock_agent(tmp.path(), ECHO, BTreeMap::new());
2205        let cfg = config(spec);
2206        let mut talk = begin(&talks, &cfg, tmp.path().to_owned(), None).expect("begin");
2207
2208        say(
2209            &mut talk,
2210            &talks,
2211            &cfg,
2212            "what does the queue module do?",
2213            Vec::new(),
2214        )
2215        .await
2216        .expect("first turn");
2217        let first_prompt = &talk.turns[1].body;
2218        assert!(first_prompt.contains("magi task add --solo"));
2219        assert!(first_prompt.contains("what does the queue module do?"));
2220
2221        say(&mut talk, &talks, &cfg, "and how is it locked?", Vec::new())
2222            .await
2223            .expect("second turn");
2224        let second_prompt = &talk.turns[3].body;
2225        assert!(
2226            !second_prompt.contains("magi task add --solo"),
2227            "the briefing is sent once, not on every turn: {second_prompt}"
2228        );
2229        assert!(second_prompt.contains("and how is it locked?"));
2230    }
2231
2232    #[tokio::test]
2233    async fn switching_agent_resets_the_seat_notes_it_and_resends_the_transcript() {
2234        let (tmp, talks) = store();
2235        let a = mock_agent(tmp.path(), ECHO, BTreeMap::new());
2236        let mut b = a.clone();
2237        b.id = "other".to_owned();
2238        let mut cfg = config(a.clone());
2239        cfg.agents.push(b.clone());
2240        let mut talk = begin(&talks, &cfg, tmp.path().to_owned(), Some(&a.id)).expect("begin");
2241        say(&mut talk, &talks, &cfg, "remember the walrus", Vec::new())
2242            .await
2243            .expect("first turn");
2244        let old_session = talk.seat.claude_session.clone();
2245        assert_eq!(talk.seat.turns, 1);
2246
2247        assert!(switch_agent(&mut talk, &talks, &b).expect("switch"));
2248        assert_eq!(talk.agent, "other");
2249        assert_eq!(talk.seat.turns, 0);
2250        assert_eq!(talk.seat.agent, "other");
2251        assert_ne!(talk.seat.claude_session, old_session);
2252        let note = talk.turns.last().expect("note");
2253        assert_eq!(note.who, Who::Agent);
2254        assert!(note.body.starts_with(MAGI_NOTE), "{}", note.body);
2255        assert!(note.body.contains("changed from"), "{}", note.body);
2256        assert_eq!(talks.get(&talk.id).expect("reload").agent, "other");
2257
2258        let before = talk.turns.len();
2259        assert!(!switch_agent(&mut talk, &talks, &b).expect("same agent"));
2260        assert_eq!(talk.turns.len(), before, "a no-op writes no note");
2261
2262        say(&mut talk, &talks, &cfg, "what did I say?", Vec::new())
2263            .await
2264            .expect("turn after switch");
2265        let prompt = &talk.turns.last().expect("reply").body;
2266        assert!(prompt.contains("remember the walrus"), "{prompt}");
2267        assert!(prompt.contains("## magi"), "{prompt}");
2268        assert!(prompt.contains("what did I say?"), "{prompt}");
2269    }
2270
2271    #[tokio::test]
2272    async fn say_appends_the_operator_turn_then_the_agent_turn() {
2273        let (tmp, talks) = store();
2274        let spec = mock_agent(tmp.path(), REPLY, env("go ahead"));
2275        let cfg = config(spec);
2276        let mut talk = begin(&talks, &cfg, tmp.path().to_owned(), None).expect("begin");
2277
2278        say(
2279            &mut talk,
2280            &talks,
2281            &cfg,
2282            "can I rename this function?",
2283            Vec::new(),
2284        )
2285        .await
2286        .expect("say");
2287
2288        assert_eq!(talk.turns.len(), 2);
2289        assert_eq!(talk.turns[0].who, Who::Operator);
2290        assert_eq!(talk.turns[0].body, "can I rename this function?");
2291        assert_eq!(talk.turns[1].who, Who::Agent);
2292        assert_eq!(talk.turns[1].body, "go ahead");
2293        assert_eq!(talks.get(&talk.id).expect("get").turns, talk.turns);
2294    }
2295
2296    #[tokio::test]
2297    async fn a_failed_turn_keeps_the_operator_message_and_says_what_happened() {
2298        let (tmp, talks) = store();
2299        let spec = mock_agent(tmp.path(), BROKEN, BTreeMap::new());
2300        let cfg = config(spec);
2301        let mut talk = begin(&talks, &cfg, tmp.path().to_owned(), None).expect("begin");
2302
2303        let err = say(&mut talk, &talks, &cfg, "check the tests", Vec::new())
2304            .await
2305            .expect_err("a turn with no answer is an error");
2306        assert!(err.to_string().contains("no answer"), "{err}");
2307
2308        let on_disk = talks.get(&talk.id).expect("get");
2309        assert_eq!(on_disk.turns.len(), 2);
2310        assert_eq!(on_disk.turns[0].body, "check the tests");
2311        let note = &on_disk.turns[1];
2312        assert_eq!(note.who, Who::Agent);
2313        assert!(note.body.starts_with(MAGI_NOTE), "{}", note.body);
2314        assert!(note.body.contains("your message is saved"));
2315    }
2316
2317    /// The failure this stands in for: a reader elsewhere briefly has the
2318    /// talk file open right when `turn` tries to save the reply, and the
2319    /// write-then-rename fails once or twice before the reader lets go.
2320    /// `write_atomic`'s own retries must absorb that with nobody the wiser -
2321    /// no gap in the transcript, no dropped turn.
2322    #[tokio::test]
2323    async fn a_passing_write_failure_while_saving_the_reply_does_not_lose_it() {
2324        let (tmp, talks) = store();
2325        let spec = mock_agent(tmp.path(), REPLY, env("go ahead"));
2326        let cfg = config(spec);
2327        let mut talk = begin(&talks, &cfg, tmp.path().to_owned(), None).expect("begin");
2328
2329        let text =
2330            record(&mut talk, &talks, "can I rename this function?", Vec::new()).expect("record");
2331        // One fewer failure than `write_atomic` will retry through, so the
2332        // very last attempt must succeed.
2333        failpoint::force_put_failures(PUT_RETRIES - 1);
2334        respond(&mut talk, &talks, &cfg, &text)
2335            .await
2336            .expect("respond must survive a write failure its own retries can outlast");
2337
2338        assert_eq!(talk.turns.len(), 2);
2339        assert_eq!(talk.turns[1].who, Who::Agent);
2340        assert_eq!(talk.turns[1].body, "go ahead");
2341        let on_disk = talks.get(&talk.id).expect("get");
2342        assert_eq!(
2343            on_disk.turns, talk.turns,
2344            "the reply must reach disk despite the early write failures"
2345        );
2346    }
2347
2348    /// When the write-then-rename never recovers - standing in for a disk
2349    /// that stays unwritable rather than a reader that eventually lets go -
2350    /// the reply must not disappear without a trace the way it did in the
2351    /// real incident this repository saw: no error on the phone, no note in
2352    /// the transcript, and the turn simply gone from `talks/<id>.json`.
2353    #[tokio::test]
2354    async fn a_persistent_write_failure_while_saving_the_reply_is_never_silent() {
2355        let (tmp, talks) = store();
2356        let spec = mock_agent(tmp.path(), REPLY, env("go ahead"));
2357        let cfg = config(spec);
2358        let mut talk = begin(&talks, &cfg, tmp.path().to_owned(), None).expect("begin");
2359
2360        let text = record(&mut talk, &talks, "check the tests", Vec::new()).expect("record");
2361        // Exactly enough forced failures to exhaust the reply's own retries;
2362        // the shorter note that replaces it then saves cleanly, which is the
2363        // common case this exercises - a large write racing something,
2364        // followed by a small one that does not.
2365        failpoint::force_put_failures(PUT_RETRIES);
2366        let err = respond(&mut talk, &talks, &cfg, &text)
2367            .await
2368            .expect_err("a reply that cannot be saved must be reported, not swallowed");
2369        assert!(err.to_string().contains("could not be saved"), "{err}");
2370
2371        let on_disk = talks.get(&talk.id).expect("get");
2372        assert_eq!(
2373            on_disk.turns.len(),
2374            2,
2375            "the operator turn plus a visible note"
2376        );
2377        assert_eq!(on_disk.turns[0].body, "check the tests");
2378        let note = &on_disk.turns[1];
2379        assert_eq!(note.who, Who::Agent);
2380        assert!(note.body.starts_with(MAGI_NOTE), "{}", note.body);
2381        assert!(
2382            note.body.contains("could not be saved"),
2383            "the operator must be told the reply is missing, not left staring \
2384             at a gap with no explanation: {}",
2385            note.body
2386        );
2387        assert_eq!(
2388            talk.turns, on_disk.turns,
2389            "the in-memory talk must match what actually landed on disk"
2390        );
2391
2392        // The generated answer itself must still be recoverable, not merely
2393        // reported as lost.
2394        let artifacts = talks.artifacts_of(&talk.id);
2395        let stash = std::fs::read_dir(&artifacts)
2396            .expect("artifacts dir")
2397            .filter_map(|e| e.ok())
2398            .find(|e| e.file_name().to_string_lossy().ends_with("-lost.txt"))
2399            .expect("a stash file for the lost reply");
2400        let stashed = std::fs::read_to_string(stash.path()).expect("read stash");
2401        assert_eq!(stashed, "go ahead");
2402
2403        // Losing the reply must not also lose the seat. The CLI took a turn
2404        // and consumed this seat's session id; if the note's write left the
2405        // record claiming otherwise, the next turn would re-open a session
2406        // the CLI is already holding - the `20260907-011805-fb57` desync -
2407        // and would re-send the whole briefing besides. Both decisions read
2408        // the seat straight off disk (`agent::has_session` and `turn`'s own
2409        // `seat.turns == 0` branch), so this is the field that has to match.
2410        assert_eq!(
2411            on_disk.seat.turns, 1,
2412            "the note's write must carry the turn the CLI actually took"
2413        );
2414        assert_eq!(
2415            on_disk.seat.claude_session, talk.seat.claude_session,
2416            "the session id handed to the CLI must survive the failed reply"
2417        );
2418        assert_eq!(on_disk.seat.captured_session, talk.seat.captured_session);
2419        assert!(
2420            agent::has_session(AgentKind::Command, &on_disk.seat, cfg.graph.sessions),
2421            "the next turn must resume, not open the same session id twice"
2422        );
2423    }
2424
2425    /// Even the note can fail to save, if the disk stays unwritable for long
2426    /// enough. `respond` must still report the failure rather than pretend
2427    /// the turn succeeded, and must not leave the in-memory `talk` claiming
2428    /// a turn that never reached disk.
2429    #[tokio::test]
2430    async fn a_write_failure_that_also_loses_the_note_still_reports_it() {
2431        let (tmp, talks) = store();
2432        let spec = mock_agent(tmp.path(), REPLY, env("go ahead"));
2433        let cfg = config(spec);
2434        let mut talk = begin(&talks, &cfg, tmp.path().to_owned(), None).expect("begin");
2435
2436        let text = record(&mut talk, &talks, "check the tests", Vec::new()).expect("record");
2437        // Enough forced failures to exhaust the retries for both the reply
2438        // and the note that would have replaced it.
2439        failpoint::force_put_failures(PUT_RETRIES * 2);
2440        let err = respond(&mut talk, &talks, &cfg, &text)
2441            .await
2442            .expect_err("neither the reply nor the note could be saved");
2443        assert!(err.to_string().contains("could not be saved"), "{err}");
2444
2445        assert_eq!(talk.turns.len(), 1, "only the operator's own turn");
2446        let on_disk = talks.get(&talk.id).expect("get");
2447        assert_eq!(on_disk.turns.len(), 1);
2448
2449        // Nothing at all reached disk, so the seat could not either: the CLI
2450        // took a turn the record does not know about. That is pinned here as
2451        // the known cost of a file that cannot be written twice over, not as
2452        // something this branch could do better - the only way to record the
2453        // seat is the write that just failed. It is also the point where
2454        // this meets `20260907-011805-fb57`: a turn taken before some later
2455        // write lands would re-open a session id the CLI already holds. The
2456        // in-memory seat keeps the truth the CLI reported, which is why it is
2457        // not wound back to match.
2458        assert_eq!(
2459            on_disk.seat.turns, 0,
2460            "an unwritable file cannot record the turn the CLI took"
2461        );
2462        assert_eq!(
2463            talk.seat.turns, 1,
2464            "the in-memory seat still reports the turn the CLI actually took"
2465        );
2466        assert_eq!(
2467            on_disk.seat.claude_session, talk.seat.claude_session,
2468            "the session id was minted at `begin` and never changes here"
2469        );
2470    }
2471
2472    /// An attachment lets the operator send an otherwise-empty message, and
2473    /// its absolute path is what actually reaches the agent's prompt - here
2474    /// on the very first turn, where it has to share the briefing.
2475    #[tokio::test]
2476    async fn attachments_reach_the_prompt_and_an_empty_body_is_still_a_turn() {
2477        let (tmp, talks) = store();
2478        let spec = mock_agent(tmp.path(), ECHO, BTreeMap::new());
2479        let cfg = config(spec);
2480        let mut talk = begin(&talks, &cfg, tmp.path().to_owned(), None).expect("begin");
2481
2482        let att = talks
2483            .put_attachment(
2484                &talk.id,
2485                "image/png",
2486                "screenshot.png",
2487                b"pretend-png-bytes",
2488            )
2489            .expect("put attachment");
2490
2491        say(&mut talk, &talks, &cfg, "", vec![att.clone()])
2492            .await
2493            .expect("an empty body with an attachment is still a turn");
2494
2495        let operator_turn = &talk.turns[0];
2496        assert_eq!(operator_turn.who, Who::Operator);
2497        assert_eq!(operator_turn.body, "");
2498        assert_eq!(operator_turn.attachments, vec![att.clone()]);
2499
2500        let prompt = &talk.turns[1].body;
2501        let expected_path = talks
2502            .attachments_dir(&talk.id)
2503            .join(format!("{}.png", att.id));
2504        assert!(
2505            prompt.contains(&expected_path.display().to_string()),
2506            "the agent must be told the attachment's absolute path: {prompt}"
2507        );
2508        assert!(prompt.contains("image/png"), "and its mime: {prompt}");
2509    }
2510
2511    /// See `chat`'s test of the same name: `run::home()` returns a bare
2512    /// relative `PathBuf` verbatim when `MAGI_HOME` is set to a relative
2513    /// path, so a `Talks` store built on it has a relative `root` too. That
2514    /// is fine for this store's own I/O, which runs in this process against
2515    /// this process's cwd, but `attachment_path` hands its result to a
2516    /// *different* process invoked with `cwd: &talk.repo` - an uncorrected
2517    /// relative path would resolve against the repository instead of
2518    /// wherever the attachment actually landed.
2519    #[test]
2520    fn attachment_path_is_absolute_even_when_the_store_root_is_relative() {
2521        let talks = Talks::at(PathBuf::from("relative-talks-root-for-this-test"));
2522        let att = Attachment {
2523            id: "0".repeat(32),
2524            name: "shot.png".to_owned(),
2525            mime: "image/png".to_owned(),
2526            bytes: 3,
2527        };
2528        let path = talks
2529            .attachment_path("some-talk-id", &att)
2530            .expect("a supported mime always yields a path");
2531        assert!(
2532            path.is_absolute(),
2533            "must be absolute even off a relative store root: {}",
2534            path.display()
2535        );
2536    }
2537
2538    #[tokio::test]
2539    async fn a_turn_past_the_configured_talk_timeout_is_reported_with_that_timeout() {
2540        // `[graph] timeout_talk` must be the number this module actually
2541        // waits, not a leftover hardcoded fifteen minutes - so the mock
2542        // sleeps past a deliberately tiny override and the failure note is
2543        // checked against that same override, not the old default.
2544        let (tmp, talks) = store();
2545        let slow = mock_agent(
2546            tmp.path(),
2547            "#!/bin/sh\ncat >/dev/null\nsleep 2\n",
2548            BTreeMap::new(),
2549        );
2550        let mut cfg = config(slow);
2551        cfg.graph.timeout_talk = 1;
2552        let mut talk = begin(&talks, &cfg, tmp.path().to_owned(), None).expect("begin");
2553
2554        let err = say(&mut talk, &talks, &cfg, "check the tests", Vec::new())
2555            .await
2556            .expect_err("a turn that never answers is an error");
2557        assert!(
2558            err.to_string().contains("did not answer within 1s"),
2559            "{err}"
2560        );
2561
2562        let on_disk = talks.get(&talk.id).expect("get");
2563        let note = on_disk.turns.last().expect("a note turn was recorded");
2564        assert!(
2565            note.body.contains("did not answer within 1s"),
2566            "the transcript must show the configured timeout: {}",
2567            note.body
2568        );
2569    }
2570
2571    #[test]
2572    fn closing_is_idempotent_and_a_closed_talk_takes_no_more_turns() {
2573        let (tmp, talks) = store();
2574        let spec = mock_agent(tmp.path(), REPLY, env("hi"));
2575        let cfg = config(spec);
2576        let mut talk = begin(&talks, &cfg, tmp.path().to_owned(), None).expect("begin");
2577
2578        close(&mut talk, &talks).expect("close");
2579        assert_eq!(talk.status, TalkStatus::Closed);
2580        close(&mut talk, &talks).expect("closing twice is not an error");
2581
2582        let err =
2583            record(&mut talk, &talks, "still there?", Vec::new()).expect_err("closed talks refuse");
2584        assert!(err.to_string().contains("closed"));
2585        let _ = &cfg; // config kept only to build the agent above
2586    }
2587
2588    #[tokio::test]
2589    async fn a_close_that_lands_while_a_turn_is_in_flight_is_not_undone_by_the_reply() {
2590        let (tmp, talks) = store();
2591        let spec = mock_agent(tmp.path(), REPLY, env("here you go"));
2592        let cfg = config(spec);
2593        // The in-flight turn's own handle: loaded once, the way a spawned
2594        // background task in `web::talk_say` holds one for the whole turn.
2595        let mut in_flight = begin(&talks, &cfg, tmp.path().to_owned(), None).expect("begin");
2596
2597        // The operator closes the conversation through a *different* handle
2598        // while the turn above is still running - exactly what a close typed
2599        // on the phone while an agent is mid-answer looks like.
2600        let mut closed_elsewhere = talks.get(&in_flight.id).expect("reread");
2601        close(&mut closed_elsewhere, &talks).expect("close");
2602        assert_eq!(
2603            talks.get(&in_flight.id).expect("reread").status,
2604            TalkStatus::Closed,
2605            "the close landed on disk before the turn finished"
2606        );
2607
2608        // The turn's own handle still says `open` - it was loaded before the
2609        // close - and finishing it must not resurrect the conversation the
2610        // operator already ended.
2611        assert_eq!(in_flight.status, TalkStatus::Open);
2612        respond(&mut in_flight, &talks, &cfg, "one more question")
2613            .await
2614            .expect("the turn itself still completes");
2615
2616        let on_disk = talks.get(&in_flight.id).expect("reread");
2617        assert_eq!(
2618            on_disk.status,
2619            TalkStatus::Closed,
2620            "a close must stick even when a turn that started before it finishes after it"
2621        );
2622        // The reply is not lost either: a turn already in flight when the
2623        // operator closed still gets its answer recorded.
2624        assert!(
2625            on_disk.turns.iter().any(|t| t.body == "here you go"),
2626            "the in-flight turn's own reply is still recorded: {:?}",
2627            on_disk.turns
2628        );
2629    }
2630
2631    #[test]
2632    fn a_close_that_lands_before_record_is_called_is_not_undone_by_it() {
2633        let (tmp, talks) = store();
2634        let spec = mock_agent(tmp.path(), REPLY, env("hi"));
2635        let cfg = config(spec);
2636        // The handle `web::talk_say` would have read before awaiting config
2637        // discovery, then carried across that await into `record`.
2638        let mut stale = begin(&talks, &cfg, tmp.path().to_owned(), None).expect("begin");
2639
2640        // The operator closes the conversation through a *different* handle
2641        // in the gap between that read and the call to `record` below.
2642        let mut closed_elsewhere = talks.get(&stale.id).expect("reread");
2643        close(&mut closed_elsewhere, &talks).expect("close");
2644        assert_eq!(
2645            talks.get(&stale.id).expect("reread").status,
2646            TalkStatus::Closed,
2647            "the close landed on disk before record was called"
2648        );
2649
2650        // The stale handle still says `open` - it was loaded before the
2651        // close - so a `record` that trusted it would append a turn and
2652        // write the conversation back open, undoing the close.
2653        assert_eq!(stale.status, TalkStatus::Open);
2654        let err = record(&mut stale, &talks, "still there?", Vec::new())
2655            .expect_err("a close that landed first must be honored, not overwritten");
2656        assert!(err.to_string().contains("closed"));
2657
2658        let on_disk = talks.get(&stale.id).expect("reread");
2659        assert_eq!(
2660            on_disk.status,
2661            TalkStatus::Closed,
2662            "record must not resurrect a conversation closed while its snapshot was stale"
2663        );
2664        assert!(
2665            on_disk.turns.is_empty(),
2666            "the rejected turn must not have been appended: {:?}",
2667            on_disk.turns
2668        );
2669        let _ = &cfg; // config kept only to build the agent above
2670    }
2671
2672    #[test]
2673    fn close_blocks_on_records_guard_rather_than_interleaving_with_it() {
2674        let (tmp, talks) = store();
2675        let spec = mock_agent(tmp.path(), REPLY, env("hi"));
2676        let cfg = config(spec);
2677        let talk = begin(&talks, &cfg, tmp.path().to_owned(), None).expect("begin");
2678
2679        // Hold the same guard `record`'s read-modify-write section holds for
2680        // the whole of its own read-then-write, standing in for `record`
2681        // being paused between its read and its `put`.
2682        let held = talks.guard();
2683
2684        let talks2 = talks.clone();
2685        let id = talk.id.clone();
2686        let closing = std::thread::spawn(move || {
2687            let mut talk = talks2.get(&id).expect("get");
2688            close(&mut talk, &talks2).expect("close");
2689        });
2690
2691        std::thread::sleep(Duration::from_millis(50));
2692        assert!(
2693            !closing.is_finished(),
2694            "close must wait for the guard, not read and write while it is held - \
2695             a re-read alone narrows this window without closing it"
2696        );
2697
2698        drop(held);
2699        closing.join().expect("close thread panicked");
2700
2701        assert_eq!(
2702            talks.get(&talk.id).expect("reread").status,
2703            TalkStatus::Closed,
2704            "once the guard is free, close still lands"
2705        );
2706        let _ = &cfg; // config kept only to build the agent above
2707    }
2708
2709    #[test]
2710    fn reopening_a_closed_talk_lets_it_take_turns_again_and_reopening_twice_is_not_an_error() {
2711        let (tmp, talks) = store();
2712        let spec = mock_agent(tmp.path(), REPLY, env("hi"));
2713        let cfg = config(spec);
2714        let mut talk = begin(&talks, &cfg, tmp.path().to_owned(), None).expect("begin");
2715
2716        close(&mut talk, &talks).expect("close");
2717        assert_eq!(talk.status, TalkStatus::Closed);
2718
2719        reopen(&mut talk, &talks).expect("reopen");
2720        assert_eq!(talk.status, TalkStatus::Open);
2721        assert_eq!(
2722            talks.get(&talk.id).expect("reread").status,
2723            TalkStatus::Open
2724        );
2725
2726        // Idempotent: reopening an already-open talk is not an error.
2727        reopen(&mut talk, &talks).expect("reopening an open talk is not an error");
2728        assert_eq!(talk.status, TalkStatus::Open);
2729
2730        record(&mut talk, &talks, "one more thing", Vec::new())
2731            .expect("a reopened talk takes turns again");
2732        let _ = &cfg; // config kept only to build the agent above
2733    }
2734
2735    #[test]
2736    fn removing_a_talk_deletes_its_record_and_artifacts_and_refuses_an_unknown_id() {
2737        let (tmp, talks) = store();
2738        let spec = mock_agent(tmp.path(), REPLY, env("hi"));
2739        let cfg = config(spec);
2740        let talk = begin(&talks, &cfg, tmp.path().to_owned(), None).expect("begin");
2741
2742        let artifacts = talks.artifacts_of(&talk.id);
2743        std::fs::create_dir_all(&artifacts).expect("create artifacts dir");
2744        std::fs::write(artifacts.join("turn-1.txt"), "hello").expect("write artifact");
2745
2746        talks.remove(&talk.id).expect("remove");
2747        assert!(!talks.path_of(&talk.id).is_file(), "the record is gone");
2748        assert!(!artifacts.is_dir(), "the artifacts directory is gone");
2749        assert!(
2750            talks.get(&talk.id).is_err(),
2751            "a removed talk cannot be read back"
2752        );
2753
2754        let err = talks
2755            .remove("nonexistent-id")
2756            .expect_err("unknown id refused");
2757        assert!(err.to_string().contains("no talk matches"), "{err}");
2758        let _ = &cfg; // config kept only to build the agent above
2759    }
2760
2761    #[tokio::test]
2762    async fn a_delete_that_lands_while_a_turn_is_in_flight_is_not_undone_by_the_reply() {
2763        let (tmp, talks) = store();
2764        let spec = mock_agent(tmp.path(), REPLY, env("here you go"));
2765        let cfg = config(spec);
2766        // The in-flight turn's own handle, loaded before the delete lands -
2767        // the same shape as the matching close test above.
2768        let mut in_flight = begin(&talks, &cfg, tmp.path().to_owned(), None).expect("begin");
2769
2770        talks.remove(&in_flight.id).expect("remove");
2771        assert!(
2772            talks.get(&in_flight.id).is_err(),
2773            "the delete landed on disk before the turn finished"
2774        );
2775
2776        // The turn's own handle has no way to know the record is gone -
2777        // finishing it must not write the file back into existence.
2778        respond(&mut in_flight, &talks, &cfg, "one more question")
2779            .await
2780            .expect("the turn itself still completes rather than erroring");
2781
2782        assert!(
2783            talks.get(&in_flight.id).is_err(),
2784            "a delete must stick even when a turn that started before it finishes after it"
2785        );
2786    }
2787
2788    #[test]
2789    fn a_delete_that_lands_before_record_is_called_is_not_undone_by_it() {
2790        let (tmp, talks) = store();
2791        let spec = mock_agent(tmp.path(), REPLY, env("hi"));
2792        let cfg = config(spec);
2793        // The handle `web::talk_say` would have read before awaiting config
2794        // discovery, then carried across that await into `record`.
2795        let mut stale = begin(&talks, &cfg, tmp.path().to_owned(), None).expect("begin");
2796
2797        talks.remove(&stale.id).expect("remove");
2798
2799        // The stale handle has no way to know the record is gone - a
2800        // `record` that trusted it would append a turn and write the
2801        // conversation back into existence.
2802        let err = record(&mut stale, &talks, "still there?", Vec::new())
2803            .expect_err("a delete that landed first must be honored, not overwritten");
2804        assert!(err.to_string().contains("deleted"), "{err}");
2805
2806        assert!(
2807            talks.get(&stale.id).is_err(),
2808            "record must not resurrect a conversation deleted while its snapshot was stale"
2809        );
2810        let _ = &cfg; // config kept only to build the agent above
2811    }
2812
2813    #[test]
2814    fn a_delete_that_lands_before_close_is_called_is_not_undone_by_it() {
2815        let (tmp, talks) = store();
2816        let spec = mock_agent(tmp.path(), REPLY, env("hi"));
2817        let cfg = config(spec);
2818        // `web::talk_close` loads `talk` and calls `close` right after - this
2819        // stands in for a delete landing in that gap.
2820        let mut stale = begin(&talks, &cfg, tmp.path().to_owned(), None).expect("begin");
2821
2822        talks.remove(&stale.id).expect("remove");
2823
2824        // The stale handle has no way to know the record is gone - a `close`
2825        // that fell back to it would write the conversation back into
2826        // existence, closed.
2827        let err = close(&mut stale, &talks)
2828            .expect_err("a delete that landed first must be honored, not overwritten");
2829        assert!(err.to_string().contains("deleted"), "{err}");
2830
2831        assert!(
2832            talks.get(&stale.id).is_err(),
2833            "close must not resurrect a conversation deleted while its snapshot was stale"
2834        );
2835        let _ = &cfg; // config kept only to build the agent above
2836    }
2837
2838    #[test]
2839    fn a_delete_that_lands_before_reopen_is_called_is_not_undone_by_it() {
2840        let (tmp, talks) = store();
2841        let spec = mock_agent(tmp.path(), REPLY, env("hi"));
2842        let cfg = config(spec);
2843        // `web::talk_reopen` loads `talk` and calls `reopen` right after -
2844        // this stands in for a delete landing in that gap.
2845        let mut stale = begin(&talks, &cfg, tmp.path().to_owned(), None).expect("begin");
2846        close(&mut stale, &talks).expect("close");
2847
2848        talks.remove(&stale.id).expect("remove");
2849
2850        // The stale handle has no way to know the record is gone - a
2851        // `reopen` that fell back to it would write the conversation back
2852        // into existence, open.
2853        let err = reopen(&mut stale, &talks)
2854            .expect_err("a delete that landed first must be honored, not overwritten");
2855        assert!(err.to_string().contains("deleted"), "{err}");
2856
2857        assert!(
2858            talks.get(&stale.id).is_err(),
2859            "reopen must not resurrect a conversation deleted while its snapshot was stale"
2860        );
2861        let _ = &cfg; // config kept only to build the agent above
2862    }
2863
2864    #[test]
2865    fn list_puts_open_talks_before_closed_ones() {
2866        let (tmp, talks) = store();
2867        let make = |id: &str, status: TalkStatus| {
2868            let mut t = Talk {
2869                schema: SCHEMA,
2870                id: id.to_owned(),
2871                repo: tmp.path().to_owned(),
2872                agent: "mock".to_owned(),
2873                status,
2874                turns: Vec::new(),
2875                pending: String::new(),
2876                pending_attachments: Vec::new(),
2877                fallback: false,
2878                created_at: Timestamp::now(),
2879                updated_at: Timestamp::now(),
2880                seat: SeatState::new(SEAT, "mock", 7),
2881            };
2882            talks.put(&mut t).expect("put");
2883        };
2884        make("20260901-000000-0001", TalkStatus::Open);
2885        make("20260902-000000-0002", TalkStatus::Open);
2886        make("20260903-000000-0003", TalkStatus::Closed);
2887
2888        let ids: Vec<String> = talks.list().into_iter().map(|t| t.id).collect();
2889        assert_eq!(
2890            ids,
2891            [
2892                "20260902-000000-0002",
2893                "20260901-000000-0001",
2894                "20260903-000000-0003"
2895            ]
2896        );
2897        assert_eq!(talks.count_open(), 2);
2898    }
2899
2900    #[test]
2901    fn tasks_of_finds_only_this_talks_own_tasks() {
2902        let dir = tempfile::tempdir().expect("tempdir");
2903        let queue = Queue::at(dir.path().join("queue"));
2904
2905        let mut mine = Task::new(
2906            "rework the loader".to_owned(),
2907            "rework the loader".to_owned(),
2908            PathBuf::from("/repo"),
2909            Source::Agent {
2910                run: "20260904-014455-ab12".to_owned(),
2911                node: "chat".to_owned(),
2912            },
2913        );
2914        queue.put(&mut mine).expect("put mine");
2915
2916        let mut theirs = Task::new(
2917            "unrelated".to_owned(),
2918            "unrelated".to_owned(),
2919            PathBuf::from("/repo"),
2920            Source::Agent {
2921                run: "20260904-090000-zz99".to_owned(),
2922                node: "implement".to_owned(),
2923            },
2924        );
2925        queue.put(&mut theirs).expect("put theirs");
2926
2927        let mut human = Task::new(
2928            "typed by hand".to_owned(),
2929            "typed by hand".to_owned(),
2930            PathBuf::from("/repo"),
2931            Source::Human,
2932        );
2933        queue.put(&mut human).expect("put human");
2934
2935        let found = tasks_of(&queue, "20260904-014455-ab12");
2936        assert_eq!(found.len(), 1);
2937        assert_eq!(found[0].id, mine.id);
2938    }
2939
2940    #[test]
2941    fn the_briefing_names_solo_task_add() {
2942        let brief = briefing(Path::new("/repo"), "en", false);
2943        assert!(brief.contains("magi task add --solo"));
2944        assert!(brief.contains("/repo"));
2945        assert!(!brief.contains("Hold this conversation in"));
2946    }
2947
2948    /// Talk fixes `repo` at the directory the conversation was opened in, so
2949    /// an agent asked to change some other checkout has no path to it unless
2950    /// the briefing itself says `--repo` can take a short name - see
2951    /// `resolve_repo_by_name` in `src/main.rs`, which is what actually
2952    /// resolves it.
2953    #[test]
2954    fn the_briefing_explains_targeting_a_different_repository_by_name() {
2955        let brief = briefing(Path::new("/repo"), "en", false);
2956        assert!(brief.contains("--repo does not have to be a full path"));
2957        assert!(brief.contains("owner/repo"));
2958        assert!(brief.contains("magi repos"));
2959        assert!(brief.contains("ask the operator"));
2960    }
2961
2962    #[test]
2963    fn the_briefing_tells_the_assistant_to_pass_images_with_attach() {
2964        let brief = briefing(Path::new("/repo"), "en", false);
2965        assert!(brief.contains("--attach <path>"), "{brief}");
2966        assert!(brief.contains("deleting this conversation"), "{brief}");
2967    }
2968
2969    #[test]
2970    fn the_briefing_names_the_language_when_it_is_not_english() {
2971        let brief = briefing(Path::new("/repo"), "Japanese", false);
2972        assert!(brief.contains("Hold this conversation in Japanese"));
2973    }
2974
2975    #[test]
2976    fn the_briefing_forbids_writes_unless_the_repository_opted_in() {
2977        let read_only = briefing(Path::new("/repo"), "en", false);
2978        assert!(read_only.contains("Do not write files"));
2979        assert!(!read_only.contains("allow_write"));
2980
2981        let writable = briefing(Path::new("/repo"), "en", true);
2982        assert!(!writable.contains("Do not write files"));
2983        assert!(writable.contains("allow_write = true"));
2984        // Still names the queue for anything past a small named edit, and
2985        // still tells the agent to report what it changed.
2986        assert!(writable.contains("magi task add --solo"));
2987        assert!(writable.contains("say plainly what you"));
2988    }
2989}