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::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}
129
130/// Where a conversation is in its life: this conversation can file any
131/// number of tasks without ending, so it only ever moves once, from open to
132/// closed.
133#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
134#[serde(rename_all = "lowercase")]
135pub enum TalkStatus {
136    /// Still open; the operator may say more, and may have already filed work
137    /// out of it.
138    Open,
139    /// Closed by hand. Kept on disk as a record.
140    Closed,
141}
142
143impl TalkStatus {
144    /// Is this conversation still live?
145    pub fn open(self) -> bool {
146        matches!(self, Self::Open)
147    }
148
149    /// Wire form, for the phone and for logs.
150    pub fn as_str(self) -> &'static str {
151        match self {
152            Self::Open => "open",
153            Self::Closed => "closed",
154        }
155    }
156}
157
158/// One standing conversation.
159#[derive(Debug, Clone, Serialize, Deserialize)]
160#[serde(deny_unknown_fields)]
161pub struct Talk {
162    /// On-disk format version.
163    pub schema: u32,
164    /// Conversation id, e.g. `20260904-014455-ab12`.
165    pub id: String,
166    /// Repository this conversation is about.
167    pub repo: PathBuf,
168    /// Roster agent id holding the conversation.
169    pub agent: String,
170    /// Current state.
171    pub status: TalkStatus,
172    /// Everything said, oldest first.
173    pub turns: Vec<Turn>,
174    /// Text and attachments accepted while the single CLI turn is busy.
175    /// They are durable, but become a real turn only when [`drain`] records them.
176    #[serde(default)]
177    pub pending: String,
178    /// Attachments paired with [`Self::pending`].
179    #[serde(default)]
180    pub pending_attachments: Vec<Attachment>,
181    /// When the conversation was opened.
182    pub created_at: Timestamp,
183    /// Last change to this file.
184    pub updated_at: Timestamp,
185    /// The CLI-side conversation, so a turn after the first costs one
186    /// sentence instead of the whole transcript. Not `pub`: it is magi's
187    /// bookkeeping, and a caller that edited it would detach the record from
188    /// the conversation the model actually holds.
189    seat: SeatState,
190}
191
192impl Talk {
193    /// Short form used in lists and notifications, matching a run's short id.
194    pub fn short(&self) -> &str {
195        short(&self.id)
196    }
197}
198
199/// A conversation store on disk.
200#[derive(Debug, Clone)]
201pub struct Talks {
202    root: PathBuf,
203    /// Serializes the read-modify-write cycle that reads a talk, decides
204    /// something from its `status`, and writes the whole record back.
205    /// [`close`], [`record`] and the tail of [`turn`] all take this before
206    /// that cycle rather than after just the read: a re-read narrows the
207    /// window another writer can land in, but does not close it, since
208    /// nothing stopped that other writer's own put from landing between this
209    /// call's re-read and its own put. Shared across every clone, since every
210    /// clone is a handle onto the same files.
211    lock: Arc<Mutex<()>>,
212}
213
214impl Talks {
215    /// The operator's conversations, `<home>/talks`.
216    pub fn open() -> Self {
217        Self::at(crate::run::home().join("talks"))
218    }
219
220    /// A store at an explicit root. Tests use this, which is why none of them
221    /// need the operator's real home.
222    pub fn at(root: PathBuf) -> Self {
223        Self {
224            root,
225            lock: Arc::new(Mutex::new(())),
226        }
227    }
228
229    /// Claim the right to read-modify-write a talk's `status`. A plain
230    /// `std::sync::Mutex`, not an async one: every caller holds it across a
231    /// handful of small file operations and never across an `.await`, so
232    /// blocking the thread briefly is the right tool, not a reason to reach
233    /// for `tokio::sync::Mutex`. Poisoning recovers rather than propagates -
234    /// one panicking caller must not wedge every talk in the store the way it
235    /// would wedge the loop's own lock; see [`crate::web`]'s `lock_or_recover`,
236    /// which this mirrors.
237    fn guard(&self) -> MutexGuard<'_, ()> {
238        self.lock.lock().unwrap_or_else(PoisonError::into_inner)
239    }
240
241    /// Directory holding the conversation files.
242    pub fn root(&self) -> &Path {
243        &self.root
244    }
245
246    /// Path for one conversation id.
247    pub fn path_of(&self, id: &str) -> PathBuf {
248        self.root.join(format!("{id}.json"))
249    }
250
251    /// Where one conversation's prompts and CLI output are kept, beside the
252    /// record rather than inside it.
253    pub fn artifacts_of(&self, id: &str) -> PathBuf {
254        self.root.join(format!("{id}.artifacts"))
255    }
256
257    /// Where this conversation's attached images live: a subdirectory of
258    /// `artifacts_of`, so deleting the conversation deletes its attachments
259    /// too and nothing here needs its own cleanup path.
260    pub fn attachments_dir(&self, id: &str) -> PathBuf {
261        self.artifacts_of(id).join("attachments")
262    }
263
264    /// Persist one already-validated attachment and return its metadata.
265    ///
266    /// `web::talk_attachment_post` is the only caller: it has already
267    /// checked `mime` against the whitelist and sniffed the bytes, so an
268    /// unrecognised mime reaching here is a bug in that caller, not
269    /// something an operator did. The id is minted here and never taken
270    /// from the client; `name` is stored for display only and never used to
271    /// build a path.
272    pub fn put_attachment(
273        &self,
274        id: &str,
275        mime: &str,
276        name: &str,
277        data: &[u8],
278    ) -> Result<Attachment> {
279        let dir = self.attachments_dir(id);
280        std::fs::create_dir_all(&dir).with_context(|| format!("create {}", dir.display()))?;
281        let ext = attachment_ext(mime).with_context(|| format!("unsupported mime `{mime}`"))?;
282        let att = Attachment {
283            id: new_attachment_id(),
284            name: name.to_owned(),
285            mime: mime.to_owned(),
286            bytes: data.len() as u64,
287        };
288        std::fs::write(dir.join(format!("{}.{ext}", att.id)), data)
289            .with_context(|| format!("write attachment {}", att.id))?;
290        std::fs::write(
291            dir.join(format!("{}.json", att.id)),
292            serde_json::to_string(&att).context("serialize attachment")?,
293        )
294        .with_context(|| format!("write attachment metadata {}", att.id))?;
295        Ok(att)
296    }
297
298    /// Just the metadata, without reading the image bytes back off disk -
299    /// what `web::talk_say` uses to turn an id the operator referenced into
300    /// an [`Attachment`] before appending a [`Turn`], where the bytes
301    /// themselves are of no interest. `None` for an id this conversation
302    /// never stored - including one that merely looks plausible:
303    /// [`valid_attachment_id`] is checked here too, not only by the caller,
304    /// the same defence-in-depth `Questions::panel_asset` uses for its own
305    /// asset ids.
306    pub fn attachment_meta(&self, id: &str, att_id: &str) -> Result<Option<Attachment>> {
307        if !valid_attachment_id(att_id) {
308            return Ok(None);
309        }
310        let meta_path = self.attachments_dir(id).join(format!("{att_id}.json"));
311        if !meta_path.is_file() {
312            return Ok(None);
313        }
314        let att = serde_json::from_str(
315            &std::fs::read_to_string(&meta_path)
316                .with_context(|| format!("read {}", meta_path.display()))?,
317        )
318        .with_context(|| format!("parse {}", meta_path.display()))?;
319        Ok(Some(att))
320    }
321
322    /// A stored attachment's metadata and its bytes together, for serving it
323    /// back on `GET`. `None` under the same conditions as
324    /// [`Talks::attachment_meta`], which this is built on.
325    pub fn read_attachment(&self, id: &str, att_id: &str) -> Result<Option<(Attachment, Vec<u8>)>> {
326        let Some(att) = self.attachment_meta(id, att_id)? else {
327            return Ok(None);
328        };
329        let ext = attachment_ext(&att.mime).with_context(|| {
330            format!("attachment {att_id} has an unsupported mime `{}`", att.mime)
331        })?;
332        let data_path = self.attachments_dir(id).join(format!("{att_id}.{ext}"));
333        let data =
334            std::fs::read(&data_path).with_context(|| format!("read {}", data_path.display()))?;
335        Ok(Some((att, data)))
336    }
337
338    /// Absolute path of one attachment's bytes, for the prompt note [`turn`]
339    /// appends and for [`Invocation::attachments`]. `None` only for a mime
340    /// [`put_attachment`] could never have written, which means the
341    /// attachment did not come from this store.
342    ///
343    /// `self.root` (and so `attachments_dir`) is not guaranteed absolute on
344    /// its own - `run::home()` returns a bare relative `PathBuf` verbatim
345    /// when the operator sets `MAGI_HOME` to a relative path, and nothing
346    /// canonicalizes it on the way in. That is harmless for every other use
347    /// of this store, since its own I/O runs in this process against this
348    /// process's cwd - but this path is handed to a CLI invoked with `cwd:
349    /// &talk.repo`, a different directory, so a relative path here would
350    /// resolve against the wrong place once it reached the prompt.
351    /// `std::path::absolute` fixes it against *this* process's cwd before
352    /// that happens; see `disk::free_bytes_by_os` for the same function used
353    /// the same way elsewhere in this codebase.
354    fn attachment_path(&self, id: &str, att: &Attachment) -> Option<PathBuf> {
355        let ext = attachment_ext(&att.mime)?;
356        let path = self.attachments_dir(id).join(format!("{}.{ext}", att.id));
357        std::path::absolute(&path).ok()
358    }
359
360    /// Write a conversation, atomically, so a process killed mid-write leaves
361    /// the previous state readable rather than a truncated file.
362    pub fn put(&self, t: &mut Talk) -> Result<()> {
363        std::fs::create_dir_all(&self.root)
364            .with_context(|| format!("create {}", self.root.display()))?;
365        t.updated_at = Timestamp::now();
366        let body = serde_json::to_string_pretty(t).context("serialize talk")?;
367        let path = self.path_of(&t.id);
368        let tmp = path.with_extension("json.tmp");
369        std::fs::write(&tmp, &body).with_context(|| format!("write {}", tmp.display()))?;
370        std::fs::rename(&tmp, &path).with_context(|| format!("replace {}", path.display()))?;
371        Ok(())
372    }
373
374    /// Load a conversation by id or unambiguous id prefix.
375    pub fn get(&self, id: &str) -> Result<Talk> {
376        let resolved = self.resolve_id(id)?;
377        read_path(&self.path_of(&resolved))
378    }
379
380    /// Every conversation on disk: open first, then newest first, so what the
381    /// operator is still using belongs above what they are done with.
382    pub fn list(&self) -> Vec<Talk> {
383        let mut all: Vec<Talk> = std::fs::read_dir(&self.root)
384            .into_iter()
385            .flatten()
386            .flatten()
387            .map(|e| e.path())
388            .filter(|p| p.extension().is_some_and(|x| x == "json"))
389            .filter_map(|p| read_path(&p).ok())
390            .collect();
391        all.sort_unstable_by(|a, b| {
392            let rank = |t: &Talk| u8::from(!t.status.open());
393            rank(a).cmp(&rank(b)).then_with(|| b.id.cmp(&a.id))
394        });
395        all
396    }
397
398    /// Expand an id prefix to exactly one conversation id.
399    pub fn resolve_id(&self, prefix: &str) -> Result<String> {
400        if self.path_of(prefix).is_file() {
401            return Ok(prefix.to_owned());
402        }
403        let hits: Vec<String> = self
404            .list()
405            .into_iter()
406            .map(|t| t.id)
407            .filter(|id| id.starts_with(prefix) || id.ends_with(prefix))
408            .collect();
409        match hits.len() {
410            1 => Ok(hits.into_iter().next().expect("exactly one hit")),
411            0 => bail!("no talk matches `{prefix}`"),
412            _ => bail!(
413                "`{prefix}` matches {} talks: {}",
414                hits.len(),
415                hits.join(", ")
416            ),
417        }
418    }
419
420    /// Change detection token: the newest modification time in the store, in
421    /// milliseconds.
422    pub fn revision(&self) -> u64 {
423        std::fs::read_dir(&self.root)
424            .into_iter()
425            .flatten()
426            .flatten()
427            .filter_map(|e| e.metadata().ok())
428            .filter_map(|m| m.modified().ok())
429            .filter_map(|t| t.duration_since(std::time::UNIX_EPOCH).ok())
430            .map(|d| d.as_millis() as u64)
431            .max()
432            .unwrap_or(0)
433    }
434
435    /// How many conversations are still open.
436    pub fn count_open(&self) -> usize {
437        self.list().iter().filter(|t| t.status.open()).count()
438    }
439
440    /// Remove a conversation from disk, record and artifacts both. The
441    /// operator's way of saying "not just done, gone" - [`close`] alone
442    /// leaves the record as history.
443    ///
444    /// Takes [`Talks::guard`] for the same reason [`close`] does: a delete
445    /// racing a [`record`] or the tail of [`turn`] must not land between
446    /// their own read and write, or the file removed here would look, to
447    /// them, like a record that simply has not been written yet. The other
448    /// half of that story is on their side - both check under this same
449    /// guard that the record they are about to write is still there, and
450    /// give up without writing if it is not, which is what stops their `put`
451    /// from resurrecting a conversation this call already removed.
452    pub fn remove(&self, id: &str) -> Result<()> {
453        let _guard = self.guard();
454        let resolved = self.resolve_id(id)?;
455        let path = self.path_of(&resolved);
456        std::fs::remove_file(&path).with_context(|| format!("remove {}", path.display()))?;
457        let artifacts = self.artifacts_of(&resolved);
458        if artifacts.is_dir() {
459            std::fs::remove_dir_all(&artifacts)
460                .with_context(|| format!("remove {}", artifacts.display()))?;
461        }
462        Ok(())
463    }
464}
465
466/// Open a conversation. Takes no agent turn: there is no idea to answer yet,
467/// and a conversation the operator has not said anything into yet is a
468/// normal, valid thing to have sitting on the phone.
469///
470/// `agent` beats `[roles] chatter`, which beats [`agent::pick`]'s own default
471/// order (a claude seat, else the first runnable agent in roster order) when
472/// nothing names a seat at all - see `[roles] chatter`'s own doc in
473/// [`crate::config`] for why a dedicated field exists rather than reusing a
474/// judge seat.
475pub fn begin(store: &Talks, cfg: &Config, repo: PathBuf, agent: Option<&str>) -> Result<Talk> {
476    // Absolute: a relative path means the wrong repository once anything
477    // other than this process reads it back.
478    let repo = repo.canonicalize().unwrap_or(repo);
479    let want = agent.or(cfg.roles.chatter.as_deref());
480    let spec = agent::pick(&cfg.agents, want, &agent::installed)?;
481
482    let now = Timestamp::now();
483    let mut talk = Talk {
484        schema: SCHEMA,
485        id: new_id(),
486        repo,
487        agent: spec.id.clone(),
488        status: TalkStatus::Open,
489        turns: Vec::new(),
490        pending: String::new(),
491        pending_attachments: Vec::new(),
492        created_at: now,
493        updated_at: now,
494        seat: SeatState::new(SEAT, &spec.id, crate::rng::entropy()),
495    };
496    store.put(&mut talk)?;
497    Ok(talk)
498}
499
500/// Append the operator's turn and flush it, without invoking anything.
501///
502/// Split out of [`say`] so `POST /api/talks/{id}/say` can answer once the
503/// message is safely on disk, and run the agent's half in the background -
504/// holding the connection for a turn that can run fifteen minutes is the
505/// wrong shape for a phone.
506pub fn record(
507    talk: &mut Talk,
508    store: &Talks,
509    text: &str,
510    attachments: Vec<Attachment>,
511) -> Result<String> {
512    // `web::talk_say` reads the talk, then awaits config discovery before
513    // calling this - a gap a concurrent `POST /api/talks/{id}/close` can land
514    // in. The guard held for the rest of this function is what actually closes
515    // that gap: re-reading status without it only shrinks the window a
516    // concurrent `close` could land in between this call's own read and its
517    // `put`, it does not remove it. See [`Talks::guard`] and the matching
518    // guard in `turn`, which this mirrors.
519    let _guard = store.guard();
520    // A concurrent `Talks::remove` can have landed in that same gap. `put`
521    // writes unconditionally, so trusting the stale `talk` here would recreate
522    // the file a delete just removed - the record must still be there for a
523    // turn to have anywhere to append to.
524    let Ok(fresh) = store.get(&talk.id) else {
525        bail!("talk {} was deleted", talk.short());
526    };
527    talk.status = fresh.status;
528    // Do not let this older handle overwrite a draft accepted while it was
529    // waiting for configuration discovery.
530    talk.pending = fresh.pending;
531    talk.pending_attachments = fresh.pending_attachments;
532    if !talk.status.open() {
533        bail!(
534            "talk {} is {} and takes no more turns",
535            talk.short(),
536            talk.status.as_str()
537        );
538    }
539    let text = text.trim();
540    if text.is_empty() && attachments.is_empty() {
541        bail!("nothing to say");
542    }
543    talk.turns.push(Turn {
544        who: Who::Operator,
545        body: text.to_owned(),
546        at: Timestamp::now(),
547        attachments,
548    });
549    store.put(talk)?;
550    Ok(text.to_owned())
551}
552
553/// Add an unrecorded message to the durable draft while another turn runs.
554pub fn queue(
555    talk: &mut Talk,
556    store: &Talks,
557    text: &str,
558    attachments: Vec<Attachment>,
559) -> Result<()> {
560    let text = text.trim();
561    if text.is_empty() && attachments.is_empty() {
562        bail!("nothing to say");
563    }
564    let _guard = store.guard();
565    let mut fresh = store
566        .get(&talk.id)
567        .with_context(|| format!("talk {} was deleted", talk.short()))?;
568    if !fresh.status.open() {
569        bail!(
570            "talk {} is {} and takes no more turns",
571            fresh.short(),
572            fresh.status.as_str()
573        );
574    }
575    if !text.is_empty() {
576        if fresh.pending.is_empty() {
577            fresh.pending = text.to_owned();
578        } else {
579            fresh.pending.push_str("\n\n");
580            fresh.pending.push_str(text);
581        }
582    }
583    fresh.pending_attachments.extend(attachments);
584    store.put(&mut fresh)?;
585    *talk = fresh;
586    Ok(())
587}
588
589/// Promote the current durable draft to one operator turn.
590pub fn drain(talk: &mut Talk, store: &Talks) -> Result<Option<String>> {
591    let _guard = store.guard();
592    let mut fresh = store
593        .get(&talk.id)
594        .with_context(|| format!("talk {} was deleted", talk.short()))?;
595    if !fresh.status.open() || (fresh.pending.is_empty() && fresh.pending_attachments.is_empty()) {
596        *talk = fresh;
597        return Ok(None);
598    }
599    let text = std::mem::take(&mut fresh.pending);
600    let attachments = std::mem::take(&mut fresh.pending_attachments);
601    fresh.turns.push(Turn {
602        who: Who::Operator,
603        body: text.clone(),
604        at: Timestamp::now(),
605        attachments,
606    });
607    store.put(&mut fresh)?;
608    *talk = fresh;
609    Ok(Some(text))
610}
611
612/// One operator turn and one agent turn, appended - the synchronous form, used
613/// by tests and by anything that is fine waiting out the turn itself.
614pub async fn say(
615    talk: &mut Talk,
616    store: &Talks,
617    cfg: &Config,
618    text: &str,
619    attachments: Vec<Attachment>,
620) -> Result<()> {
621    let text = record(talk, store, text, attachments)?;
622    turn(talk, store, cfg, &text).await
623}
624
625/// The agent's half of a turn: invoke, append, flush. Pairs with [`record`].
626pub async fn respond(talk: &mut Talk, store: &Talks, cfg: &Config, text: &str) -> Result<()> {
627    turn(talk, store, cfg, text).await
628}
629
630/// Close a conversation. Idempotent: closing an already-closed conversation is
631/// not an error, since the operator's intent - "I am done with this" - is
632/// already satisfied.
633///
634/// Re-reads the record under [`Talks::guard`] rather than trusting the
635/// caller's copy of `talk`, and writes that fresh copy back rather than the
636/// one passed in. `web::talk_close` loads `talk` and calls this right after
637/// with no gap of its own, but without the guard that load can still land
638/// between a `record` or `turn` elsewhere reading the file and writing it
639/// back - and a close built on the older snapshot would put it right back,
640/// silently dropping whatever turn the other call had just appended.
641///
642/// If the re-read fails, this errors rather than falling back to the
643/// caller's stale copy: `talk::begin` always `put`s the record before handing
644/// out a `Talk`, so the only way a re-read can fail is a concurrent
645/// [`Talks::remove`] having deleted it, and writing the stale copy back would
646/// resurrect exactly what that delete removed.
647pub fn close(talk: &mut Talk, store: &Talks) -> Result<()> {
648    let _guard = store.guard();
649    let mut fresh = store
650        .get(&talk.id)
651        .with_context(|| format!("talk {} was deleted", talk.short()))?;
652    fresh.status = TalkStatus::Closed;
653    // A closed conversation must not replay a draft if it is reopened later.
654    fresh.pending.clear();
655    fresh.pending_attachments.clear();
656    store.put(&mut fresh)?;
657    *talk = fresh;
658    Ok(())
659}
660
661/// Reopen a closed conversation. Idempotent for the same reason [`close`] is:
662/// reopening an already-open conversation is not an error, since the
663/// operator's intent - "I want to keep talking about this" - is already
664/// satisfied.
665///
666/// Written symmetrically with [`close`]: re-reads the record under
667/// [`Talks::guard`] rather than trusting the caller's copy of `talk`, writes
668/// that fresh copy back rather than the one passed in, and errors rather than
669/// falling back to the stale copy if the re-read fails, for the same reasons
670/// `close`'s doc gives.
671pub fn reopen(talk: &mut Talk, store: &Talks) -> Result<()> {
672    let _guard = store.guard();
673    let mut fresh = store
674        .get(&talk.id)
675        .with_context(|| format!("talk {} was deleted", talk.short()))?;
676    fresh.status = TalkStatus::Open;
677    store.put(&mut fresh)?;
678    *talk = fresh;
679    Ok(())
680}
681
682/// Discard the durable draft without adding a transcript turn.
683pub fn clear_pending(talk: &mut Talk, store: &Talks) -> Result<()> {
684    let _guard = store.guard();
685    let mut fresh = store
686        .get(&talk.id)
687        .with_context(|| format!("talk {} was deleted", talk.short()))?;
688    fresh.pending.clear();
689    fresh.pending_attachments.clear();
690    store.put(&mut fresh)?;
691    *talk = fresh;
692    Ok(())
693}
694
695/// Clear a draft only when the caller still sees its complete snapshot.
696pub fn clear_pending_if_matches(
697    talk: &mut Talk,
698    store: &Talks,
699    expected_text: &str,
700    expected_attachments: &[String],
701) -> Result<bool> {
702    let _guard = store.guard();
703    let mut fresh = store
704        .get(&talk.id)
705        .with_context(|| format!("talk {} was deleted", talk.short()))?;
706    if !pending_matches(&fresh, expected_text, expected_attachments) {
707        *talk = fresh;
708        return Ok(false);
709    }
710    fresh.pending.clear();
711    fresh.pending_attachments.clear();
712    store.put(&mut fresh)?;
713    *talk = fresh;
714    Ok(true)
715}
716
717/// Replace just the text of the durable draft, but only if the caller's
718/// snapshot still identifies the entire draft. This refuses to overwrite a
719/// message another client queued or a draft the drain already promoted.
720pub fn edit_pending_text(
721    talk: &mut Talk,
722    store: &Talks,
723    text: &str,
724    expected_text: &str,
725    expected_attachments: &[String],
726) -> Result<bool> {
727    let _guard = store.guard();
728    let mut fresh = store
729        .get(&talk.id)
730        .with_context(|| format!("talk {} was deleted", talk.short()))?;
731    if !pending_matches(&fresh, expected_text, expected_attachments) {
732        *talk = fresh;
733        return Ok(false);
734    }
735    fresh.pending = text.trim().to_owned();
736    store.put(&mut fresh)?;
737    *talk = fresh;
738    Ok(true)
739}
740
741fn pending_matches(talk: &Talk, expected_text: &str, expected_attachments: &[String]) -> bool {
742    talk.pending == expected_text
743        && talk
744            .pending_attachments
745            .iter()
746            .map(|attachment| &attachment.id)
747            .eq(expected_attachments.iter())
748}
749
750/// Invoke the conversation's agent once and append what it said.
751///
752/// The first turn ever taken carries the full [`briefing`], because nothing
753/// else has told the agent what this conversation is or what it may do.
754/// Every turn after that resends nothing when the CLI can resume its own
755/// session, and falls back to [`transcript`] only when it cannot.
756async fn turn(talk: &mut Talk, store: &Talks, cfg: &Config, text: &str) -> Result<()> {
757    let spec = cfg
758        .agents
759        .iter()
760        .find(|a| a.id == talk.agent)
761        .with_context(|| {
762            format!(
763                "talk {} was opened with agent `{}`, which is no longer in \
764                 the roster; restore it in magi.toml or start a new \
765                 conversation",
766                talk.short(),
767                talk.agent
768            )
769        })?;
770
771    let resuming = agent::has_session(spec.kind, &talk.seat, cfg.graph.sessions);
772    // The newest turn is always the operator message this call is answering
773    // - `record` appended it before `turn` was ever called - so its own
774    // attachments are what belong at the end of *this* prompt.
775    let last_note = attachment_note(
776        store,
777        &talk.id,
778        talk.turns
779            .last()
780            .map_or(&[][..], |t| t.attachments.as_slice()),
781    );
782    let body = if talk.seat.turns == 0 {
783        format!(
784            "{}\n\n# Operator\n\n{text}{last_note}",
785            briefing(&talk.repo, &cfg.graph.language, cfg.talk.allow_write)
786        )
787    } else if resuming {
788        format!("{text}{last_note}")
789    } else {
790        format!("{}\n\n{text}{last_note}", transcript(talk, store))
791    };
792
793    // Every attachment this conversation has ever held, not only this
794    // turn's: a resumed session gets a fresh process every turn, so a CLI
795    // whose sandbox needs `--add-dir` (see `agent::build_command`) needs the
796    // grant again to open an image from an earlier turn, even when nothing
797    // new was attached just now.
798    let attachment_paths: Vec<PathBuf> = talk
799        .turns
800        .iter()
801        .flat_map(|t| t.attachments.iter())
802        .filter_map(|a| store.attachment_path(&talk.id, a))
803        .collect();
804
805    let artifacts = store.artifacts_of(&talk.id);
806    let stem = format!("turn-{}", talk.seat.turns + 1);
807    // The chat's build cache is the same shared one the graph's seats get, so
808    // a conversation that compiles does not mint another multi-GB target dir.
809    let cache_dir = cfg.cache_dir();
810    let inv = Invocation {
811        cwd: &talk.repo,
812        prompt: &body,
813        timeout: turn_timeout(cfg),
814        // Off unless this repository's own config opts in - see
815        // `crate::config::Talk::allow_write` and this module's doc for why
816        // the default keeps a conversational edit from landing in a checkout
817        // no run or review can claim.
818        allow_write: cfg.talk.allow_write,
819        sessions: cfg.graph.sessions,
820        artifacts: &artifacts,
821        stem: &stem,
822        // The conversation's own id, so `magi task add` run from inside it is
823        // attributed to this conversation - see `Source::Agent`.
824        run: &talk.id,
825        node: "chat",
826        cache_dir: cache_dir.as_deref(),
827        attachments: &attachment_paths,
828    };
829
830    let outcome = agent::invoke(spec, &mut talk.seat, &inv).await;
831    let note = |why: String| Turn {
832        who: Who::Agent,
833        body: format!("{MAGI_NOTE}{why}"),
834        at: Timestamp::now(),
835        attachments: Vec::new(),
836    };
837    let (reply, failure) = match outcome {
838        Err(e) => (
839            note(format!("could not run agent `{}`: {e}", talk.agent)),
840            Some(format!("could not run agent `{}`: {e}", talk.agent)),
841        ),
842        Ok(out) if out.quota_exhausted() => {
843            let reset = out
844                .quota
845                .as_ref()
846                .and_then(|q| q.reset.clone())
847                .map_or_else(String::new, |r| format!(" (resets {r})"));
848            let why = format!(
849                "agent `{}` is out of quota{reset}; your message is saved, so \
850                 say it again when the window reopens",
851                talk.agent
852            );
853            (note(why.clone()), Some(why))
854        }
855        Ok(out) if out.timed_out => {
856            let why = format!(
857                "agent `{}` did not answer within {}s; your message is saved",
858                talk.agent,
859                turn_timeout(cfg).as_secs()
860            );
861            (note(why.clone()), Some(why))
862        }
863        Ok(out) if !out.usable() => {
864            let why = format!(
865                "agent `{}` produced no answer (exit {}); your message is saved",
866                talk.agent,
867                out.exit_code
868                    .map_or_else(|| "unknown".to_owned(), |c| c.to_string())
869            );
870            (note(why.clone()), Some(why))
871        }
872        Ok(out) => (
873            Turn {
874                who: Who::Agent,
875                body: out.text.trim().to_owned(),
876                at: Timestamp::now(),
877                attachments: Vec::new(),
878            },
879            None,
880        ),
881    };
882
883    // A close landed on disk while this turn was in flight is read back here
884    // rather than trusted from the snapshot this call started with. `store`
885    // holds nothing else this function does not itself own - the turn guard
886    // in `web::Ui::begin_talk_turn` keeps `turns` and `seat` this call's
887    // alone to mutate - but `status` is not behind that guard, and an
888    // operator's close must stick: the whole point of ending a conversation
889    // is that an agent's answer to the last message before the close cannot
890    // silently reopen it. The guard is what makes that read-then-write
891    // section atomic with `close`'s own - taken only for this tail and not
892    // for the whole invocation above, so one talk's fifteen-minute turn does
893    // not block another talk's close from proceeding.
894    let _guard = store.guard();
895    // A delete is the more final version of that same race: `put` writes
896    // unconditionally, so a talk removed while this turn was in flight must
897    // stay removed rather than being written back with this turn's reply
898    // appended to it. The reply is simply given up on - there is no
899    // conversation left for it to belong to.
900    let Ok(fresh) = store.get(&talk.id) else {
901        return Ok(());
902    };
903    talk.status = fresh.status;
904    // `queue` may have accepted another operator message while the CLI was
905    // running. This handle predates that write, so preserving only `status`
906    // would overwrite the durable draft when the reply is appended below.
907    talk.pending = fresh.pending;
908    talk.pending_attachments = fresh.pending_attachments;
909    talk.turns.push(reply);
910    store.put(talk)?;
911
912    match failure {
913        Some(why) => bail!("{why}"),
914        None => Ok(()),
915    }
916}
917
918/// Everything said so far, as prose, for a CLI that cannot resume its own
919/// conversation.
920fn transcript(talk: &Talk, store: &Talks) -> String {
921    let mut out = String::from(
922        "This conversation cannot resume on the CLI's side, so here is \
923         everything said so far; answer only the last message.\n",
924    );
925    for t in &talk.turns {
926        let who = match t.who {
927            Who::Operator => "operator",
928            Who::Agent => "you",
929        };
930        out.push_str(&format!("\n## {who}\n\n{}\n", t.body.trim()));
931        out.push_str(&attachment_note(store, &talk.id, &t.attachments));
932    }
933    out
934}
935
936/// The section named at the end of a turn's body, listing every attachment's
937/// absolute path and mime so the agent knows exactly what to open. Empty
938/// when `attachments` is, which is every turn but the rare one carrying an
939/// image, so a turn with none changes nothing about the prompt.
940fn attachment_note(store: &Talks, talk_id: &str, attachments: &[Attachment]) -> String {
941    if attachments.is_empty() {
942        return String::new();
943    }
944    let mut out = String::from(
945        "\n\nThe operator attached the image(s) below to this message. Open \
946         and look at each one before you answer.\n",
947    );
948    for att in attachments {
949        if let Some(path) = store.attachment_path(talk_id, att) {
950            out.push_str(&format!("\n- {} ({})", path.display(), att.mime));
951        }
952    }
953    out.push('\n');
954    out
955}
956
957/// The briefing the agent opens with, sent once as part of its first turn.
958///
959/// Pure, so the properties that matter can be asserted without an interview:
960/// it names `magi task add --solo` (the route this conversation always has to
961/// changing anything) and it never tells the agent to write a task *file* of
962/// its own - that would compete with filing through the queue.
963/// `allow_write` only ever adds an extra permission on top of that; it never
964/// removes the queue as an option, which is why both branches keep the same
965/// `# When the operator wants something done` section - `write_policy` is
966/// the only part that changes.
967pub fn briefing(repo: &Path, language: &str, allow_write: bool) -> String {
968    let write_policy = if allow_write {
969        "Write access is enabled for this conversation (`allow_write = \
970         true`), so you may write files - but only a small, \
971         already-decided edit the operator names outright in this \
972         conversation, not an implementation. This is a permission on the \
973         conversation as a whole, not a property of whichever repository \
974         it happened to start in: if the operator names a different \
975         repository for that small edit, the policy allows it there too. \
976         Your own tool may still confine writes to the repository this \
977         conversation started in regardless - if a write elsewhere is \
978         refused, say so plainly rather than working around it. Once you \
979         have made an edit, say plainly what you edited. Anything bigger, \
980         or anything still open-ended, still goes through the queue below \
981         rather than being done here."
982    } else {
983        "Do not write files. Implementing a change is not this \
984         conversation's job; a separate, blind competition of agents does \
985         that, and a repository this conversation has already edited would \
986         make their diffs unjudgeable."
987    };
988    let mut out = format!(
989        "You are magi's standing conversation partner for its operator, who \
990         usually has this open on a phone. Keep replies short: no preamble, \
991         no restating what they just said.\n\n\
992         # Repository\n\n{repo}\n\n\
993         You may look around: read files, run shell commands, search history, \
994         run tests - whatever answers the question. {write_policy}\n\n\
995         # When the operator wants something done\n\n\
996         Run:\n\n\
997         magi task add --solo --repo {repo} <instruction>\n\n\
998         and tell the operator the task id it prints, so they can follow it \
999         from the Queue. Write <instruction> so that an implementer who has \
1000         never seen this conversation can act on it alone - it is everything \
1001         they get. Use --solo: it runs the task through one implementer \
1002         straight into review instead of the usual multi-agent competition, \
1003         which is the right shape for a change this conversation has already \
1004         settled, rather than one still worth several independent takes.\n",
1005        repo = repo.display(),
1006    );
1007    out.push_str(&language_note(language));
1008    out
1009}
1010
1011/// The operator is talking, so their language matters here more than in most
1012/// prompts magi sends.
1013fn language_note(language: &str) -> String {
1014    if language.trim().is_empty() || language.eq_ignore_ascii_case("en") {
1015        String::new()
1016    } else {
1017        format!("\nHold this conversation in {language}.\n")
1018    }
1019}
1020
1021/// Queue tasks this conversation has filed, oldest first.
1022///
1023/// A task is this conversation's when its [`Source::Agent`] names this
1024/// conversation's id as `run` - which is exactly what happens when
1025/// `magi task add` is run from inside a turn, because [`turn`] passes the
1026/// conversation's own id as [`Invocation::run`].
1027pub fn tasks_of(queue: &Queue, talk_id: &str) -> Vec<Task> {
1028    let mut tasks: Vec<Task> = queue
1029        .list()
1030        .into_iter()
1031        .filter(|t| matches!(&t.source, Source::Agent { run, .. } if run == talk_id))
1032        .collect();
1033    tasks.sort_unstable_by(|a, b| a.id.cmp(&b.id));
1034    tasks
1035}
1036
1037fn read_path(path: &Path) -> Result<Talk> {
1038    let body = std::fs::read_to_string(path).with_context(|| format!("read {}", path.display()))?;
1039    serde_json::from_str(&body).with_context(|| format!("parse {}", path.display()))
1040}
1041
1042fn short(id: &str) -> &str {
1043    id.split('-').next_back().unwrap_or(id)
1044}
1045
1046fn new_id() -> String {
1047    let stamp = jiff::Zoned::now().strftime("%Y%m%d-%H%M%S");
1048    let seed = crate::rng::entropy();
1049    format!("{stamp}-{:04x}", (seed ^ (seed >> 32)) & 0xffff)
1050}
1051
1052/// Extension an attachment's bytes are stored under, from its (already
1053/// validated) mime. The one place this mapping exists on the write side;
1054/// `web`'s own whitelist is what actually decides which mimes are accepted
1055/// in the first place.
1056fn attachment_ext(mime: &str) -> Option<&'static str> {
1057    match mime {
1058        "image/png" => Some("png"),
1059        "image/jpeg" => Some("jpg"),
1060        "image/gif" => Some("gif"),
1061        "image/webp" => Some("webp"),
1062        _ => None,
1063    }
1064}
1065
1066/// Is `id` a shape [`put_attachment`](Talks::put_attachment) could have
1067/// produced? 32 lowercase hex digits and nothing else, checked before an id
1068/// that came from the client is ever allowed to build a path - so `..` and a
1069/// path separator are never even possible.
1070pub fn valid_attachment_id(id: &str) -> bool {
1071    id.len() == 32
1072        && id
1073            .bytes()
1074            .all(|b| b.is_ascii_digit() || (b'a'..=b'f').contains(&b))
1075}
1076
1077/// A fresh attachment id: 128 bits of process entropy as lowercase hex - the
1078/// same "mint it, never take it from the client" rule [`new_id`] follows for
1079/// conversation ids.
1080fn new_attachment_id() -> String {
1081    let mut r = crate::rng::SplitMix64::new(crate::rng::entropy());
1082    format!("{:016x}{:016x}", r.next_u64(), r.next_u64())
1083}
1084
1085#[cfg(test)]
1086mod tests {
1087    use std::collections::BTreeMap;
1088
1089    use crate::config::{AgentKind, AgentSpec, Graph};
1090    use crate::queue::{Queue, Source, Task};
1091
1092    use super::*;
1093
1094    /// A store of its own, with no process-global state.
1095    fn store() -> (tempfile::TempDir, Talks) {
1096        let tmp = tempfile::tempdir().expect("tempdir");
1097        let talks = Talks::at(tmp.path().join("talks"));
1098        (tmp, talks)
1099    }
1100
1101    /// A `kind = "command"` agent whose whole behaviour is a POSIX shell
1102    /// script - see `chat`'s tests for why no test here may spawn a real
1103    /// agent CLI.
1104    fn mock_agent(dir: &Path, script: &str, env: BTreeMap<String, String>) -> AgentSpec {
1105        let path = dir.join("mock-talk-agent.sh");
1106        std::fs::write(&path, script).expect("write mock");
1107        AgentSpec {
1108            id: "mock".to_owned(),
1109            kind: AgentKind::Command,
1110            model: None,
1111            command: vec!["sh".to_owned(), path.to_string_lossy().into_owned()],
1112            extra_args: Vec::new(),
1113            env,
1114            prompt_delivery: None,
1115        }
1116    }
1117
1118    fn config(spec: AgentSpec) -> Config {
1119        Config {
1120            agents: vec![spec],
1121            graph: Graph {
1122                language: "en".to_owned(),
1123                ..Graph::default()
1124            },
1125            ..Config::default()
1126        }
1127    }
1128
1129    /// Echo a canned reply, ignoring the prompt on stdin.
1130    const REPLY: &str = "#!/bin/sh\ncat >/dev/null\nprintf '%s\\n' \"$MOCK_REPLY\"\n";
1131
1132    /// Say nothing and fail, the way a CLI that cannot start does.
1133    const BROKEN: &str = "#!/bin/sh\ncat >/dev/null\nexit 3\n";
1134
1135    /// Reply with the prompt it was given, so a test can inspect exactly what
1136    /// the agent received on stdin.
1137    const ECHO: &str = "#!/bin/sh\ncat\n";
1138
1139    fn env(reply: &str) -> BTreeMap<String, String> {
1140        BTreeMap::from([("MOCK_REPLY".to_owned(), reply.to_owned())])
1141    }
1142
1143    #[test]
1144    fn the_frozen_json_field_names_round_trip_through_disk() {
1145        let (tmp, talks) = store();
1146        let mut talk = Talk {
1147            schema: SCHEMA,
1148            id: "20260904-014455-ab12".to_owned(),
1149            repo: tmp.path().to_owned(),
1150            agent: "sonnet".to_owned(),
1151            status: TalkStatus::Open,
1152            turns: Vec::new(),
1153            pending: String::new(),
1154            pending_attachments: Vec::new(),
1155            created_at: Timestamp::now(),
1156            updated_at: Timestamp::now(),
1157            seat: SeatState::new(SEAT, "sonnet", 7),
1158        };
1159        talks.put(&mut talk).expect("put");
1160
1161        let raw = std::fs::read_to_string(talks.path_of(&talk.id)).expect("read back");
1162        let v: serde_json::Value = serde_json::from_str(&raw).expect("parse");
1163        for field in [
1164            "schema",
1165            "id",
1166            "repo",
1167            "agent",
1168            "status",
1169            "turns",
1170            "created_at",
1171            "updated_at",
1172        ] {
1173            assert!(v.get(field).is_some(), "missing field `{field}`");
1174        }
1175        assert_eq!(v["schema"], 1);
1176        assert_eq!(v["status"], "open");
1177
1178        let back = talks.get(&talk.id).expect("get");
1179        assert_eq!(back.id, talk.id);
1180        assert_eq!(back.status, TalkStatus::Open);
1181    }
1182
1183    #[test]
1184    fn opening_a_talk_takes_no_agent_turn() {
1185        let (tmp, talks) = store();
1186        // A script that would fail loudly if it were ever run: `begin` must
1187        // not invoke anything, since there is nothing yet for an agent to
1188        // answer.
1189        let spec = mock_agent(tmp.path(), BROKEN, BTreeMap::new());
1190        let cfg = config(spec);
1191
1192        let talk = begin(&talks, &cfg, tmp.path().to_owned(), None).expect("begin");
1193        assert_eq!(talk.status, TalkStatus::Open);
1194        assert!(talk.turns.is_empty(), "nothing has been said yet");
1195
1196        let on_disk = talks.get(&talk.id).expect("get");
1197        assert_eq!(on_disk.turns.len(), 0);
1198    }
1199
1200    /// `[roles] chatter`, when set, decides who holds this conversation; unset,
1201    /// it falls back to [`agent::pick`]'s own default order (a claude seat,
1202    /// else the first runnable agent in roster order) rather than to any
1203    /// other role - see `[roles] chatter`'s own doc in [`crate::config`] for
1204    /// why a dedicated field exists at all: opening this against the same
1205    /// seat as a judge is what produced the `agent ... did not answer within
1206    /// 300s` timeout that led to it.
1207    #[test]
1208    fn chatter_wins_when_set_and_falls_back_to_pick_s_default_order_otherwise() {
1209        let (tmp, talks) = store();
1210        let first_spec = mock_agent(tmp.path(), BROKEN, BTreeMap::new());
1211        let mut chatter_spec = mock_agent(tmp.path(), BROKEN, BTreeMap::new());
1212        chatter_spec.id = "chatter-mock".to_owned();
1213
1214        let mut cfg = Config {
1215            agents: vec![first_spec.clone(), chatter_spec.clone()],
1216            graph: Graph {
1217                language: "en".to_owned(),
1218                ..Graph::default()
1219            },
1220            ..Config::default()
1221        };
1222        cfg.roles.chatter = Some(chatter_spec.id.clone());
1223
1224        let talk =
1225            begin(&talks, &cfg, tmp.path().to_owned(), None).expect("begin with chatter set");
1226        assert_eq!(talk.agent, chatter_spec.id, "an explicit chatter must win");
1227
1228        cfg.roles.chatter = None;
1229        let fallback =
1230            begin(&talks, &cfg, tmp.path().to_owned(), None).expect("begin with chatter unset");
1231        assert_eq!(
1232            fallback.agent, first_spec.id,
1233            "unset chatter must fall back to agent::pick's own default order"
1234        );
1235    }
1236
1237    /// A conversation recorded before attachments existed - schema 1, no
1238    /// `attachments` key on any turn - must still read.
1239    #[test]
1240    fn a_talk_recorded_without_attachments_still_reads() {
1241        let (tmp, talks) = store();
1242        let path = talks.path_of("20260904-014455-ab12");
1243        std::fs::create_dir_all(talks.root()).expect("talks dir");
1244        std::fs::write(
1245            &path,
1246            serde_json::json!({
1247                "schema": 1,
1248                "id": "20260904-014455-ab12",
1249                "repo": tmp.path(),
1250                "agent": "sonnet",
1251                "status": "open",
1252                "turns": [
1253                    { "who": "operator", "body": "still there?",
1254                      "at": Timestamp::now().to_string() },
1255                ],
1256                "created_at": Timestamp::now().to_string(),
1257                "updated_at": Timestamp::now().to_string(),
1258                "seat": SeatState::new(SEAT, "sonnet", 7),
1259            })
1260            .to_string(),
1261        )
1262        .expect("write pre-attachments talk");
1263
1264        let talk = talks.get("20260904-014455-ab12").expect("must still read");
1265        assert!(talk.turns[0].attachments.is_empty());
1266    }
1267
1268    #[test]
1269    fn queued_text_is_durable_combined_and_drained_as_one_operator_turn() {
1270        let (tmp, talks) = store();
1271        let cfg = config(mock_agent(tmp.path(), REPLY, env("reply")));
1272        let mut talk = begin(&talks, &cfg, tmp.path().to_owned(), None).expect("begin");
1273
1274        queue(&mut talk, &talks, "first", Vec::new()).expect("queue first");
1275        queue(&mut talk, &talks, "second", Vec::new()).expect("queue second");
1276        let saved = talks.get(&talk.id).expect("reload queued talk");
1277        assert_eq!(saved.pending, "first\n\nsecond");
1278        assert!(saved.turns.is_empty(), "a draft is not a transcript turn");
1279
1280        let drained = drain(&mut talk, &talks).expect("drain");
1281        assert_eq!(drained.as_deref(), Some("first\n\nsecond"));
1282        let saved = talks.get(&talk.id).expect("reload drained talk");
1283        assert!(saved.pending.is_empty());
1284        assert_eq!(saved.turns.len(), 1);
1285        assert_eq!(saved.turns[0].body, "first\n\nsecond");
1286    }
1287
1288    #[test]
1289    fn editing_a_queued_draft_preserves_its_attachments_and_rejects_a_stale_snapshot() {
1290        let (tmp, talks) = store();
1291        let cfg = config(mock_agent(tmp.path(), REPLY, env("reply")));
1292        let mut talk = begin(&talks, &cfg, tmp.path().to_owned(), None).expect("begin");
1293        let attachment = Attachment {
1294            id: "a".repeat(32),
1295            name: "shot.png".to_owned(),
1296            mime: "image/png".to_owned(),
1297            bytes: 3,
1298        };
1299
1300        queue(&mut talk, &talks, "first", vec![attachment.clone()]).expect("queue");
1301        assert!(
1302            edit_pending_text(
1303                &mut talk,
1304                &talks,
1305                "corrected",
1306                "first",
1307                std::slice::from_ref(&attachment.id),
1308            )
1309            .expect("edit")
1310        );
1311        let saved = talks.get(&talk.id).expect("reload edited draft");
1312        assert_eq!(saved.pending, "corrected");
1313        assert_eq!(saved.pending_attachments, vec![attachment]);
1314
1315        queue(&mut talk, &talks, "later", Vec::new()).expect("queue concurrent draft");
1316        assert!(
1317            !edit_pending_text(
1318                &mut talk,
1319                &talks,
1320                "stale edit",
1321                "corrected",
1322                &["a".repeat(32)],
1323            )
1324            .expect("stale edit is a conflict")
1325        );
1326        assert_eq!(
1327            talks.get(&talk.id).expect("reload after conflict").pending,
1328            "corrected\n\nlater"
1329        );
1330        assert!(
1331            !clear_pending_if_matches(&mut talk, &talks, "corrected", &["a".repeat(32)])
1332                .expect("stale clear is a conflict")
1333        );
1334        assert_eq!(
1335            talks
1336                .get(&talk.id)
1337                .expect("reload after stale clear")
1338                .pending,
1339            "corrected\n\nlater"
1340        );
1341    }
1342
1343    #[tokio::test]
1344    async fn a_reply_save_preserves_pending_accepted_while_the_cli_runs() {
1345        let (tmp, talks) = store();
1346        let slow = "#!/bin/sh\ncat >/dev/null\nsleep 0.1\nprintf reply\n";
1347        let cfg = config(mock_agent(tmp.path(), slow, BTreeMap::new()));
1348        let mut running = begin(&talks, &cfg, tmp.path().to_owned(), None).expect("begin");
1349        let id = running.id.clone();
1350        let first = record(&mut running, &talks, "first", Vec::new()).expect("record");
1351
1352        let response_talks = talks.clone();
1353        let response_cfg = cfg.clone();
1354        let reply = tokio::spawn(async move {
1355            respond(&mut running, &response_talks, &response_cfg, &first).await
1356        });
1357        tokio::time::sleep(std::time::Duration::from_millis(20)).await;
1358
1359        let mut queued = talks.get(&id).expect("queued handle");
1360        queue(&mut queued, &talks, "next", Vec::new()).expect("queue");
1361        reply.await.expect("join").expect("reply");
1362
1363        let saved = talks.get(&id).expect("reload");
1364        assert_eq!(saved.pending, "next");
1365        assert_eq!(saved.turns.len(), 2, "operator message and reply remain");
1366    }
1367
1368    #[tokio::test]
1369    async fn the_first_turn_carries_the_briefing_and_later_turns_do_not() {
1370        let (tmp, talks) = store();
1371        let spec = mock_agent(tmp.path(), ECHO, BTreeMap::new());
1372        let cfg = config(spec);
1373        let mut talk = begin(&talks, &cfg, tmp.path().to_owned(), None).expect("begin");
1374
1375        say(
1376            &mut talk,
1377            &talks,
1378            &cfg,
1379            "what does the queue module do?",
1380            Vec::new(),
1381        )
1382        .await
1383        .expect("first turn");
1384        let first_prompt = &talk.turns[1].body;
1385        assert!(first_prompt.contains("magi task add --solo"));
1386        assert!(first_prompt.contains("what does the queue module do?"));
1387
1388        say(&mut talk, &talks, &cfg, "and how is it locked?", Vec::new())
1389            .await
1390            .expect("second turn");
1391        let second_prompt = &talk.turns[3].body;
1392        assert!(
1393            !second_prompt.contains("magi task add --solo"),
1394            "the briefing is sent once, not on every turn: {second_prompt}"
1395        );
1396        assert!(second_prompt.contains("and how is it locked?"));
1397    }
1398
1399    #[tokio::test]
1400    async fn say_appends_the_operator_turn_then_the_agent_turn() {
1401        let (tmp, talks) = store();
1402        let spec = mock_agent(tmp.path(), REPLY, env("go ahead"));
1403        let cfg = config(spec);
1404        let mut talk = begin(&talks, &cfg, tmp.path().to_owned(), None).expect("begin");
1405
1406        say(
1407            &mut talk,
1408            &talks,
1409            &cfg,
1410            "can I rename this function?",
1411            Vec::new(),
1412        )
1413        .await
1414        .expect("say");
1415
1416        assert_eq!(talk.turns.len(), 2);
1417        assert_eq!(talk.turns[0].who, Who::Operator);
1418        assert_eq!(talk.turns[0].body, "can I rename this function?");
1419        assert_eq!(talk.turns[1].who, Who::Agent);
1420        assert_eq!(talk.turns[1].body, "go ahead");
1421        assert_eq!(talks.get(&talk.id).expect("get").turns, talk.turns);
1422    }
1423
1424    #[tokio::test]
1425    async fn a_failed_turn_keeps_the_operator_message_and_says_what_happened() {
1426        let (tmp, talks) = store();
1427        let spec = mock_agent(tmp.path(), BROKEN, BTreeMap::new());
1428        let cfg = config(spec);
1429        let mut talk = begin(&talks, &cfg, tmp.path().to_owned(), None).expect("begin");
1430
1431        let err = say(&mut talk, &talks, &cfg, "check the tests", Vec::new())
1432            .await
1433            .expect_err("a turn with no answer is an error");
1434        assert!(err.to_string().contains("no answer"), "{err}");
1435
1436        let on_disk = talks.get(&talk.id).expect("get");
1437        assert_eq!(on_disk.turns.len(), 2);
1438        assert_eq!(on_disk.turns[0].body, "check the tests");
1439        let note = &on_disk.turns[1];
1440        assert_eq!(note.who, Who::Agent);
1441        assert!(note.body.starts_with(MAGI_NOTE), "{}", note.body);
1442        assert!(note.body.contains("your message is saved"));
1443    }
1444
1445    /// An attachment lets the operator send an otherwise-empty message, and
1446    /// its absolute path is what actually reaches the agent's prompt - here
1447    /// on the very first turn, where it has to share the briefing.
1448    #[tokio::test]
1449    async fn attachments_reach_the_prompt_and_an_empty_body_is_still_a_turn() {
1450        let (tmp, talks) = store();
1451        let spec = mock_agent(tmp.path(), ECHO, BTreeMap::new());
1452        let cfg = config(spec);
1453        let mut talk = begin(&talks, &cfg, tmp.path().to_owned(), None).expect("begin");
1454
1455        let att = talks
1456            .put_attachment(
1457                &talk.id,
1458                "image/png",
1459                "screenshot.png",
1460                b"pretend-png-bytes",
1461            )
1462            .expect("put attachment");
1463
1464        say(&mut talk, &talks, &cfg, "", vec![att.clone()])
1465            .await
1466            .expect("an empty body with an attachment is still a turn");
1467
1468        let operator_turn = &talk.turns[0];
1469        assert_eq!(operator_turn.who, Who::Operator);
1470        assert_eq!(operator_turn.body, "");
1471        assert_eq!(operator_turn.attachments, vec![att.clone()]);
1472
1473        let prompt = &talk.turns[1].body;
1474        let expected_path = talks
1475            .attachments_dir(&talk.id)
1476            .join(format!("{}.png", att.id));
1477        assert!(
1478            prompt.contains(&expected_path.display().to_string()),
1479            "the agent must be told the attachment's absolute path: {prompt}"
1480        );
1481        assert!(prompt.contains("image/png"), "and its mime: {prompt}");
1482    }
1483
1484    /// See `chat`'s test of the same name: `run::home()` returns a bare
1485    /// relative `PathBuf` verbatim when `MAGI_HOME` is set to a relative
1486    /// path, so a `Talks` store built on it has a relative `root` too. That
1487    /// is fine for this store's own I/O, which runs in this process against
1488    /// this process's cwd, but `attachment_path` hands its result to a
1489    /// *different* process invoked with `cwd: &talk.repo` - an uncorrected
1490    /// relative path would resolve against the repository instead of
1491    /// wherever the attachment actually landed.
1492    #[test]
1493    fn attachment_path_is_absolute_even_when_the_store_root_is_relative() {
1494        let talks = Talks::at(PathBuf::from("relative-talks-root-for-this-test"));
1495        let att = Attachment {
1496            id: "0".repeat(32),
1497            name: "shot.png".to_owned(),
1498            mime: "image/png".to_owned(),
1499            bytes: 3,
1500        };
1501        let path = talks
1502            .attachment_path("some-talk-id", &att)
1503            .expect("a supported mime always yields a path");
1504        assert!(
1505            path.is_absolute(),
1506            "must be absolute even off a relative store root: {}",
1507            path.display()
1508        );
1509    }
1510
1511    #[tokio::test]
1512    async fn a_turn_past_the_configured_talk_timeout_is_reported_with_that_timeout() {
1513        // `[graph] timeout_talk` must be the number this module actually
1514        // waits, not a leftover hardcoded fifteen minutes - so the mock
1515        // sleeps past a deliberately tiny override and the failure note is
1516        // checked against that same override, not the old default.
1517        let (tmp, talks) = store();
1518        let slow = mock_agent(
1519            tmp.path(),
1520            "#!/bin/sh\ncat >/dev/null\nsleep 2\n",
1521            BTreeMap::new(),
1522        );
1523        let mut cfg = config(slow);
1524        cfg.graph.timeout_talk = 1;
1525        let mut talk = begin(&talks, &cfg, tmp.path().to_owned(), None).expect("begin");
1526
1527        let err = say(&mut talk, &talks, &cfg, "check the tests", Vec::new())
1528            .await
1529            .expect_err("a turn that never answers is an error");
1530        assert!(
1531            err.to_string().contains("did not answer within 1s"),
1532            "{err}"
1533        );
1534
1535        let on_disk = talks.get(&talk.id).expect("get");
1536        let note = on_disk.turns.last().expect("a note turn was recorded");
1537        assert!(
1538            note.body.contains("did not answer within 1s"),
1539            "the transcript must show the configured timeout: {}",
1540            note.body
1541        );
1542    }
1543
1544    #[test]
1545    fn closing_is_idempotent_and_a_closed_talk_takes_no_more_turns() {
1546        let (tmp, talks) = store();
1547        let spec = mock_agent(tmp.path(), REPLY, env("hi"));
1548        let cfg = config(spec);
1549        let mut talk = begin(&talks, &cfg, tmp.path().to_owned(), None).expect("begin");
1550
1551        close(&mut talk, &talks).expect("close");
1552        assert_eq!(talk.status, TalkStatus::Closed);
1553        close(&mut talk, &talks).expect("closing twice is not an error");
1554
1555        let err =
1556            record(&mut talk, &talks, "still there?", Vec::new()).expect_err("closed talks refuse");
1557        assert!(err.to_string().contains("closed"));
1558        let _ = &cfg; // config kept only to build the agent above
1559    }
1560
1561    #[tokio::test]
1562    async fn a_close_that_lands_while_a_turn_is_in_flight_is_not_undone_by_the_reply() {
1563        let (tmp, talks) = store();
1564        let spec = mock_agent(tmp.path(), REPLY, env("here you go"));
1565        let cfg = config(spec);
1566        // The in-flight turn's own handle: loaded once, the way a spawned
1567        // background task in `web::talk_say` holds one for the whole turn.
1568        let mut in_flight = begin(&talks, &cfg, tmp.path().to_owned(), None).expect("begin");
1569
1570        // The operator closes the conversation through a *different* handle
1571        // while the turn above is still running - exactly what a close typed
1572        // on the phone while an agent is mid-answer looks like.
1573        let mut closed_elsewhere = talks.get(&in_flight.id).expect("reread");
1574        close(&mut closed_elsewhere, &talks).expect("close");
1575        assert_eq!(
1576            talks.get(&in_flight.id).expect("reread").status,
1577            TalkStatus::Closed,
1578            "the close landed on disk before the turn finished"
1579        );
1580
1581        // The turn's own handle still says `open` - it was loaded before the
1582        // close - and finishing it must not resurrect the conversation the
1583        // operator already ended.
1584        assert_eq!(in_flight.status, TalkStatus::Open);
1585        respond(&mut in_flight, &talks, &cfg, "one more question")
1586            .await
1587            .expect("the turn itself still completes");
1588
1589        let on_disk = talks.get(&in_flight.id).expect("reread");
1590        assert_eq!(
1591            on_disk.status,
1592            TalkStatus::Closed,
1593            "a close must stick even when a turn that started before it finishes after it"
1594        );
1595        // The reply is not lost either: a turn already in flight when the
1596        // operator closed still gets its answer recorded.
1597        assert!(
1598            on_disk.turns.iter().any(|t| t.body == "here you go"),
1599            "the in-flight turn's own reply is still recorded: {:?}",
1600            on_disk.turns
1601        );
1602    }
1603
1604    #[test]
1605    fn a_close_that_lands_before_record_is_called_is_not_undone_by_it() {
1606        let (tmp, talks) = store();
1607        let spec = mock_agent(tmp.path(), REPLY, env("hi"));
1608        let cfg = config(spec);
1609        // The handle `web::talk_say` would have read before awaiting config
1610        // discovery, then carried across that await into `record`.
1611        let mut stale = begin(&talks, &cfg, tmp.path().to_owned(), None).expect("begin");
1612
1613        // The operator closes the conversation through a *different* handle
1614        // in the gap between that read and the call to `record` below.
1615        let mut closed_elsewhere = talks.get(&stale.id).expect("reread");
1616        close(&mut closed_elsewhere, &talks).expect("close");
1617        assert_eq!(
1618            talks.get(&stale.id).expect("reread").status,
1619            TalkStatus::Closed,
1620            "the close landed on disk before record was called"
1621        );
1622
1623        // The stale handle still says `open` - it was loaded before the
1624        // close - so a `record` that trusted it would append a turn and
1625        // write the conversation back open, undoing the close.
1626        assert_eq!(stale.status, TalkStatus::Open);
1627        let err = record(&mut stale, &talks, "still there?", Vec::new())
1628            .expect_err("a close that landed first must be honored, not overwritten");
1629        assert!(err.to_string().contains("closed"));
1630
1631        let on_disk = talks.get(&stale.id).expect("reread");
1632        assert_eq!(
1633            on_disk.status,
1634            TalkStatus::Closed,
1635            "record must not resurrect a conversation closed while its snapshot was stale"
1636        );
1637        assert!(
1638            on_disk.turns.is_empty(),
1639            "the rejected turn must not have been appended: {:?}",
1640            on_disk.turns
1641        );
1642        let _ = &cfg; // config kept only to build the agent above
1643    }
1644
1645    #[test]
1646    fn close_blocks_on_records_guard_rather_than_interleaving_with_it() {
1647        let (tmp, talks) = store();
1648        let spec = mock_agent(tmp.path(), REPLY, env("hi"));
1649        let cfg = config(spec);
1650        let talk = begin(&talks, &cfg, tmp.path().to_owned(), None).expect("begin");
1651
1652        // Hold the same guard `record`'s read-modify-write section holds for
1653        // the whole of its own read-then-write, standing in for `record`
1654        // being paused between its read and its `put`.
1655        let held = talks.guard();
1656
1657        let talks2 = talks.clone();
1658        let id = talk.id.clone();
1659        let closing = std::thread::spawn(move || {
1660            let mut talk = talks2.get(&id).expect("get");
1661            close(&mut talk, &talks2).expect("close");
1662        });
1663
1664        std::thread::sleep(Duration::from_millis(50));
1665        assert!(
1666            !closing.is_finished(),
1667            "close must wait for the guard, not read and write while it is held - \
1668             a re-read alone narrows this window without closing it"
1669        );
1670
1671        drop(held);
1672        closing.join().expect("close thread panicked");
1673
1674        assert_eq!(
1675            talks.get(&talk.id).expect("reread").status,
1676            TalkStatus::Closed,
1677            "once the guard is free, close still lands"
1678        );
1679        let _ = &cfg; // config kept only to build the agent above
1680    }
1681
1682    #[test]
1683    fn reopening_a_closed_talk_lets_it_take_turns_again_and_reopening_twice_is_not_an_error() {
1684        let (tmp, talks) = store();
1685        let spec = mock_agent(tmp.path(), REPLY, env("hi"));
1686        let cfg = config(spec);
1687        let mut talk = begin(&talks, &cfg, tmp.path().to_owned(), None).expect("begin");
1688
1689        close(&mut talk, &talks).expect("close");
1690        assert_eq!(talk.status, TalkStatus::Closed);
1691
1692        reopen(&mut talk, &talks).expect("reopen");
1693        assert_eq!(talk.status, TalkStatus::Open);
1694        assert_eq!(
1695            talks.get(&talk.id).expect("reread").status,
1696            TalkStatus::Open
1697        );
1698
1699        // Idempotent: reopening an already-open talk is not an error.
1700        reopen(&mut talk, &talks).expect("reopening an open talk is not an error");
1701        assert_eq!(talk.status, TalkStatus::Open);
1702
1703        record(&mut talk, &talks, "one more thing", Vec::new())
1704            .expect("a reopened talk takes turns again");
1705        let _ = &cfg; // config kept only to build the agent above
1706    }
1707
1708    #[test]
1709    fn removing_a_talk_deletes_its_record_and_artifacts_and_refuses_an_unknown_id() {
1710        let (tmp, talks) = store();
1711        let spec = mock_agent(tmp.path(), REPLY, env("hi"));
1712        let cfg = config(spec);
1713        let talk = begin(&talks, &cfg, tmp.path().to_owned(), None).expect("begin");
1714
1715        let artifacts = talks.artifacts_of(&talk.id);
1716        std::fs::create_dir_all(&artifacts).expect("create artifacts dir");
1717        std::fs::write(artifacts.join("turn-1.txt"), "hello").expect("write artifact");
1718
1719        talks.remove(&talk.id).expect("remove");
1720        assert!(!talks.path_of(&talk.id).is_file(), "the record is gone");
1721        assert!(!artifacts.is_dir(), "the artifacts directory is gone");
1722        assert!(
1723            talks.get(&talk.id).is_err(),
1724            "a removed talk cannot be read back"
1725        );
1726
1727        let err = talks
1728            .remove("nonexistent-id")
1729            .expect_err("unknown id refused");
1730        assert!(err.to_string().contains("no talk matches"), "{err}");
1731        let _ = &cfg; // config kept only to build the agent above
1732    }
1733
1734    #[tokio::test]
1735    async fn a_delete_that_lands_while_a_turn_is_in_flight_is_not_undone_by_the_reply() {
1736        let (tmp, talks) = store();
1737        let spec = mock_agent(tmp.path(), REPLY, env("here you go"));
1738        let cfg = config(spec);
1739        // The in-flight turn's own handle, loaded before the delete lands -
1740        // the same shape as the matching close test above.
1741        let mut in_flight = begin(&talks, &cfg, tmp.path().to_owned(), None).expect("begin");
1742
1743        talks.remove(&in_flight.id).expect("remove");
1744        assert!(
1745            talks.get(&in_flight.id).is_err(),
1746            "the delete landed on disk before the turn finished"
1747        );
1748
1749        // The turn's own handle has no way to know the record is gone -
1750        // finishing it must not write the file back into existence.
1751        respond(&mut in_flight, &talks, &cfg, "one more question")
1752            .await
1753            .expect("the turn itself still completes rather than erroring");
1754
1755        assert!(
1756            talks.get(&in_flight.id).is_err(),
1757            "a delete must stick even when a turn that started before it finishes after it"
1758        );
1759    }
1760
1761    #[test]
1762    fn a_delete_that_lands_before_record_is_called_is_not_undone_by_it() {
1763        let (tmp, talks) = store();
1764        let spec = mock_agent(tmp.path(), REPLY, env("hi"));
1765        let cfg = config(spec);
1766        // The handle `web::talk_say` would have read before awaiting config
1767        // discovery, then carried across that await into `record`.
1768        let mut stale = begin(&talks, &cfg, tmp.path().to_owned(), None).expect("begin");
1769
1770        talks.remove(&stale.id).expect("remove");
1771
1772        // The stale handle has no way to know the record is gone - a
1773        // `record` that trusted it would append a turn and write the
1774        // conversation back into existence.
1775        let err = record(&mut stale, &talks, "still there?", Vec::new())
1776            .expect_err("a delete that landed first must be honored, not overwritten");
1777        assert!(err.to_string().contains("deleted"), "{err}");
1778
1779        assert!(
1780            talks.get(&stale.id).is_err(),
1781            "record must not resurrect a conversation deleted while its snapshot was stale"
1782        );
1783        let _ = &cfg; // config kept only to build the agent above
1784    }
1785
1786    #[test]
1787    fn a_delete_that_lands_before_close_is_called_is_not_undone_by_it() {
1788        let (tmp, talks) = store();
1789        let spec = mock_agent(tmp.path(), REPLY, env("hi"));
1790        let cfg = config(spec);
1791        // `web::talk_close` loads `talk` and calls `close` right after - this
1792        // stands in for a delete landing in that gap.
1793        let mut stale = begin(&talks, &cfg, tmp.path().to_owned(), None).expect("begin");
1794
1795        talks.remove(&stale.id).expect("remove");
1796
1797        // The stale handle has no way to know the record is gone - a `close`
1798        // that fell back to it would write the conversation back into
1799        // existence, closed.
1800        let err = close(&mut stale, &talks)
1801            .expect_err("a delete that landed first must be honored, not overwritten");
1802        assert!(err.to_string().contains("deleted"), "{err}");
1803
1804        assert!(
1805            talks.get(&stale.id).is_err(),
1806            "close must not resurrect a conversation deleted while its snapshot was stale"
1807        );
1808        let _ = &cfg; // config kept only to build the agent above
1809    }
1810
1811    #[test]
1812    fn a_delete_that_lands_before_reopen_is_called_is_not_undone_by_it() {
1813        let (tmp, talks) = store();
1814        let spec = mock_agent(tmp.path(), REPLY, env("hi"));
1815        let cfg = config(spec);
1816        // `web::talk_reopen` loads `talk` and calls `reopen` right after -
1817        // this stands in for a delete landing in that gap.
1818        let mut stale = begin(&talks, &cfg, tmp.path().to_owned(), None).expect("begin");
1819        close(&mut stale, &talks).expect("close");
1820
1821        talks.remove(&stale.id).expect("remove");
1822
1823        // The stale handle has no way to know the record is gone - a
1824        // `reopen` that fell back to it would write the conversation back
1825        // into existence, open.
1826        let err = reopen(&mut stale, &talks)
1827            .expect_err("a delete that landed first must be honored, not overwritten");
1828        assert!(err.to_string().contains("deleted"), "{err}");
1829
1830        assert!(
1831            talks.get(&stale.id).is_err(),
1832            "reopen must not resurrect a conversation deleted while its snapshot was stale"
1833        );
1834        let _ = &cfg; // config kept only to build the agent above
1835    }
1836
1837    #[test]
1838    fn list_puts_open_talks_before_closed_ones() {
1839        let (tmp, talks) = store();
1840        let make = |id: &str, status: TalkStatus| {
1841            let mut t = Talk {
1842                schema: SCHEMA,
1843                id: id.to_owned(),
1844                repo: tmp.path().to_owned(),
1845                agent: "mock".to_owned(),
1846                status,
1847                turns: Vec::new(),
1848                pending: String::new(),
1849                pending_attachments: Vec::new(),
1850                created_at: Timestamp::now(),
1851                updated_at: Timestamp::now(),
1852                seat: SeatState::new(SEAT, "mock", 7),
1853            };
1854            talks.put(&mut t).expect("put");
1855        };
1856        make("20260901-000000-0001", TalkStatus::Open);
1857        make("20260902-000000-0002", TalkStatus::Open);
1858        make("20260903-000000-0003", TalkStatus::Closed);
1859
1860        let ids: Vec<String> = talks.list().into_iter().map(|t| t.id).collect();
1861        assert_eq!(
1862            ids,
1863            [
1864                "20260902-000000-0002",
1865                "20260901-000000-0001",
1866                "20260903-000000-0003"
1867            ]
1868        );
1869        assert_eq!(talks.count_open(), 2);
1870    }
1871
1872    #[test]
1873    fn tasks_of_finds_only_this_talks_own_tasks() {
1874        let dir = tempfile::tempdir().expect("tempdir");
1875        let queue = Queue::at(dir.path().join("queue"));
1876
1877        let mut mine = Task::new(
1878            "rework the loader".to_owned(),
1879            "rework the loader".to_owned(),
1880            PathBuf::from("/repo"),
1881            Source::Agent {
1882                run: "20260904-014455-ab12".to_owned(),
1883                node: "chat".to_owned(),
1884            },
1885        );
1886        queue.put(&mut mine).expect("put mine");
1887
1888        let mut theirs = Task::new(
1889            "unrelated".to_owned(),
1890            "unrelated".to_owned(),
1891            PathBuf::from("/repo"),
1892            Source::Agent {
1893                run: "20260904-090000-zz99".to_owned(),
1894                node: "implement".to_owned(),
1895            },
1896        );
1897        queue.put(&mut theirs).expect("put theirs");
1898
1899        let mut human = Task::new(
1900            "typed by hand".to_owned(),
1901            "typed by hand".to_owned(),
1902            PathBuf::from("/repo"),
1903            Source::Human,
1904        );
1905        queue.put(&mut human).expect("put human");
1906
1907        let found = tasks_of(&queue, "20260904-014455-ab12");
1908        assert_eq!(found.len(), 1);
1909        assert_eq!(found[0].id, mine.id);
1910    }
1911
1912    #[test]
1913    fn the_briefing_names_solo_task_add() {
1914        let brief = briefing(Path::new("/repo"), "en", false);
1915        assert!(brief.contains("magi task add --solo"));
1916        assert!(brief.contains("/repo"));
1917        assert!(!brief.contains("Hold this conversation in"));
1918    }
1919
1920    #[test]
1921    fn the_briefing_names_the_language_when_it_is_not_english() {
1922        let brief = briefing(Path::new("/repo"), "Japanese", false);
1923        assert!(brief.contains("Hold this conversation in Japanese"));
1924    }
1925
1926    #[test]
1927    fn the_briefing_forbids_writes_unless_the_repository_opted_in() {
1928        let read_only = briefing(Path::new("/repo"), "en", false);
1929        assert!(read_only.contains("Do not write files"));
1930        assert!(!read_only.contains("allow_write"));
1931
1932        let writable = briefing(Path::new("/repo"), "en", true);
1933        assert!(!writable.contains("Do not write files"));
1934        assert!(writable.contains("allow_write = true"));
1935        // Still names the queue for anything past a small named edit, and
1936        // still tells the agent to report what it changed.
1937        assert!(writable.contains("magi task add --solo"));
1938        assert!(writable.contains("say plainly what you"));
1939    }
1940}