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    ///
363    /// The write-then-rename itself is retried a handful of times - see
364    /// [`write_atomic`] - because a reader with the destination file briefly
365    /// open is exactly the kind of failure that must not cost an agent's
366    /// whole reply; see `turn`'s own tail for what happens when even that is
367    /// not enough.
368    pub fn put(&self, t: &mut Talk) -> Result<()> {
369        std::fs::create_dir_all(&self.root)
370            .with_context(|| format!("create {}", self.root.display()))?;
371        t.updated_at = Timestamp::now();
372        let body = serde_json::to_string_pretty(t).context("serialize talk")?;
373        let path = self.path_of(&t.id);
374        let tmp = path.with_extension("json.tmp");
375        write_atomic(&tmp, &path, &body)
376    }
377
378    /// Load a conversation by id or unambiguous id prefix.
379    pub fn get(&self, id: &str) -> Result<Talk> {
380        let resolved = self.resolve_id(id)?;
381        read_path(&self.path_of(&resolved))
382    }
383
384    /// Every conversation on disk: open first, then newest first, so what the
385    /// operator is still using belongs above what they are done with.
386    pub fn list(&self) -> Vec<Talk> {
387        let mut all: Vec<Talk> = std::fs::read_dir(&self.root)
388            .into_iter()
389            .flatten()
390            .flatten()
391            .map(|e| e.path())
392            .filter(|p| p.extension().is_some_and(|x| x == "json"))
393            .filter_map(|p| read_path(&p).ok())
394            .collect();
395        all.sort_unstable_by(|a, b| {
396            let rank = |t: &Talk| u8::from(!t.status.open());
397            rank(a).cmp(&rank(b)).then_with(|| b.id.cmp(&a.id))
398        });
399        all
400    }
401
402    /// Expand an id prefix to exactly one conversation id.
403    pub fn resolve_id(&self, prefix: &str) -> Result<String> {
404        if self.path_of(prefix).is_file() {
405            return Ok(prefix.to_owned());
406        }
407        let hits: Vec<String> = self
408            .list()
409            .into_iter()
410            .map(|t| t.id)
411            .filter(|id| id.starts_with(prefix) || id.ends_with(prefix))
412            .collect();
413        match hits.len() {
414            1 => Ok(hits.into_iter().next().expect("exactly one hit")),
415            0 => bail!("no talk matches `{prefix}`"),
416            _ => bail!(
417                "`{prefix}` matches {} talks: {}",
418                hits.len(),
419                hits.join(", ")
420            ),
421        }
422    }
423
424    /// Change detection token: the newest modification time in the store, in
425    /// milliseconds.
426    pub fn revision(&self) -> u64 {
427        std::fs::read_dir(&self.root)
428            .into_iter()
429            .flatten()
430            .flatten()
431            .filter_map(|e| e.metadata().ok())
432            .filter_map(|m| m.modified().ok())
433            .filter_map(|t| t.duration_since(std::time::UNIX_EPOCH).ok())
434            .map(|d| d.as_millis() as u64)
435            .max()
436            .unwrap_or(0)
437    }
438
439    /// How many conversations are still open.
440    pub fn count_open(&self) -> usize {
441        self.list().iter().filter(|t| t.status.open()).count()
442    }
443
444    /// Remove a conversation from disk, record and artifacts both. The
445    /// operator's way of saying "not just done, gone" - [`close`] alone
446    /// leaves the record as history.
447    ///
448    /// Takes [`Talks::guard`] for the same reason [`close`] does: a delete
449    /// racing a [`record`] or the tail of [`turn`] must not land between
450    /// their own read and write, or the file removed here would look, to
451    /// them, like a record that simply has not been written yet. The other
452    /// half of that story is on their side - both check under this same
453    /// guard that the record they are about to write is still there, and
454    /// give up without writing if it is not, which is what stops their `put`
455    /// from resurrecting a conversation this call already removed.
456    pub fn remove(&self, id: &str) -> Result<()> {
457        let _guard = self.guard();
458        let resolved = self.resolve_id(id)?;
459        let path = self.path_of(&resolved);
460        std::fs::remove_file(&path).with_context(|| format!("remove {}", path.display()))?;
461        let artifacts = self.artifacts_of(&resolved);
462        if artifacts.is_dir() {
463            std::fs::remove_dir_all(&artifacts)
464                .with_context(|| format!("remove {}", artifacts.display()))?;
465        }
466        Ok(())
467    }
468}
469
470/// Open a conversation. Takes no agent turn: there is no idea to answer yet,
471/// and a conversation the operator has not said anything into yet is a
472/// normal, valid thing to have sitting on the phone.
473///
474/// `agent` beats `[roles] chatter`, which beats [`agent::pick`]'s own default
475/// order (a claude seat, else the first runnable agent in roster order) when
476/// nothing names a seat at all - see `[roles] chatter`'s own doc in
477/// [`crate::config`] for why a dedicated field exists rather than reusing a
478/// judge seat.
479pub fn begin(store: &Talks, cfg: &Config, repo: PathBuf, agent: Option<&str>) -> Result<Talk> {
480    // Absolute: a relative path means the wrong repository once anything
481    // other than this process reads it back.
482    let repo = repo.canonicalize().unwrap_or(repo);
483    let want = agent.or(cfg.roles.chatter.as_deref());
484    let spec = agent::pick(&cfg.agents, want, &agent::installed)?;
485
486    let now = Timestamp::now();
487    let mut talk = Talk {
488        schema: SCHEMA,
489        id: new_id(),
490        repo,
491        agent: spec.id.clone(),
492        status: TalkStatus::Open,
493        turns: Vec::new(),
494        pending: String::new(),
495        pending_attachments: Vec::new(),
496        created_at: now,
497        updated_at: now,
498        seat: SeatState::new(SEAT, &spec.id, crate::rng::entropy()),
499    };
500    store.put(&mut talk)?;
501    Ok(talk)
502}
503
504/// Append the operator's turn and flush it, without invoking anything.
505///
506/// Split out of [`say`] so `POST /api/talks/{id}/say` can answer once the
507/// message is safely on disk, and run the agent's half in the background -
508/// holding the connection for a turn that can run fifteen minutes is the
509/// wrong shape for a phone.
510pub fn record(
511    talk: &mut Talk,
512    store: &Talks,
513    text: &str,
514    attachments: Vec<Attachment>,
515) -> Result<String> {
516    // `web::talk_say` reads the talk, then awaits config discovery before
517    // calling this - a gap a concurrent `POST /api/talks/{id}/close` can land
518    // in. The guard held for the rest of this function is what actually closes
519    // that gap: re-reading status without it only shrinks the window a
520    // concurrent `close` could land in between this call's own read and its
521    // `put`, it does not remove it. See [`Talks::guard`] and the matching
522    // guard in `turn`, which this mirrors.
523    let _guard = store.guard();
524    // A concurrent `Talks::remove` can have landed in that same gap. `put`
525    // writes unconditionally, so trusting the stale `talk` here would recreate
526    // the file a delete just removed - the record must still be there for a
527    // turn to have anywhere to append to.
528    let Ok(fresh) = store.get(&talk.id) else {
529        bail!("talk {} was deleted", talk.short());
530    };
531    talk.status = fresh.status;
532    // Do not let this older handle overwrite a draft accepted while it was
533    // waiting for configuration discovery.
534    talk.pending = fresh.pending;
535    talk.pending_attachments = fresh.pending_attachments;
536    if !talk.status.open() {
537        bail!(
538            "talk {} is {} and takes no more turns",
539            talk.short(),
540            talk.status.as_str()
541        );
542    }
543    let text = text.trim();
544    if text.is_empty() && attachments.is_empty() {
545        bail!("nothing to say");
546    }
547    talk.turns.push(Turn {
548        who: Who::Operator,
549        body: text.to_owned(),
550        at: Timestamp::now(),
551        attachments,
552    });
553    store.put(talk)?;
554    Ok(text.to_owned())
555}
556
557/// Add an unrecorded message to the durable draft while another turn runs.
558pub fn queue(
559    talk: &mut Talk,
560    store: &Talks,
561    text: &str,
562    attachments: Vec<Attachment>,
563) -> Result<()> {
564    let text = text.trim();
565    if text.is_empty() && attachments.is_empty() {
566        bail!("nothing to say");
567    }
568    let _guard = store.guard();
569    let mut fresh = store
570        .get(&talk.id)
571        .with_context(|| format!("talk {} was deleted", talk.short()))?;
572    if !fresh.status.open() {
573        bail!(
574            "talk {} is {} and takes no more turns",
575            fresh.short(),
576            fresh.status.as_str()
577        );
578    }
579    if !text.is_empty() {
580        if fresh.pending.is_empty() {
581            fresh.pending = text.to_owned();
582        } else {
583            fresh.pending.push_str("\n\n");
584            fresh.pending.push_str(text);
585        }
586    }
587    fresh.pending_attachments.extend(attachments);
588    store.put(&mut fresh)?;
589    *talk = fresh;
590    Ok(())
591}
592
593/// Promote the current durable draft to one operator turn.
594pub fn drain(talk: &mut Talk, store: &Talks) -> Result<Option<String>> {
595    let _guard = store.guard();
596    let mut fresh = store
597        .get(&talk.id)
598        .with_context(|| format!("talk {} was deleted", talk.short()))?;
599    if !fresh.status.open() || (fresh.pending.is_empty() && fresh.pending_attachments.is_empty()) {
600        *talk = fresh;
601        return Ok(None);
602    }
603    let text = std::mem::take(&mut fresh.pending);
604    let attachments = std::mem::take(&mut fresh.pending_attachments);
605    fresh.turns.push(Turn {
606        who: Who::Operator,
607        body: text.clone(),
608        at: Timestamp::now(),
609        attachments,
610    });
611    store.put(&mut fresh)?;
612    *talk = fresh;
613    Ok(Some(text))
614}
615
616/// One operator turn and one agent turn, appended - the synchronous form, used
617/// by tests and by anything that is fine waiting out the turn itself.
618pub async fn say(
619    talk: &mut Talk,
620    store: &Talks,
621    cfg: &Config,
622    text: &str,
623    attachments: Vec<Attachment>,
624) -> Result<()> {
625    let text = record(talk, store, text, attachments)?;
626    turn(talk, store, cfg, &text).await
627}
628
629/// The agent's half of a turn: invoke, append, flush. Pairs with [`record`].
630pub async fn respond(talk: &mut Talk, store: &Talks, cfg: &Config, text: &str) -> Result<()> {
631    turn(talk, store, cfg, text).await
632}
633
634/// Close a conversation. Idempotent: closing an already-closed conversation is
635/// not an error, since the operator's intent - "I am done with this" - is
636/// already satisfied.
637///
638/// Re-reads the record under [`Talks::guard`] rather than trusting the
639/// caller's copy of `talk`, and writes that fresh copy back rather than the
640/// one passed in. `web::talk_close` loads `talk` and calls this right after
641/// with no gap of its own, but without the guard that load can still land
642/// between a `record` or `turn` elsewhere reading the file and writing it
643/// back - and a close built on the older snapshot would put it right back,
644/// silently dropping whatever turn the other call had just appended.
645///
646/// If the re-read fails, this errors rather than falling back to the
647/// caller's stale copy: `talk::begin` always `put`s the record before handing
648/// out a `Talk`, so the only way a re-read can fail is a concurrent
649/// [`Talks::remove`] having deleted it, and writing the stale copy back would
650/// resurrect exactly what that delete removed.
651pub fn close(talk: &mut Talk, store: &Talks) -> Result<()> {
652    let _guard = store.guard();
653    let mut fresh = store
654        .get(&talk.id)
655        .with_context(|| format!("talk {} was deleted", talk.short()))?;
656    fresh.status = TalkStatus::Closed;
657    // A closed conversation must not replay a draft if it is reopened later.
658    fresh.pending.clear();
659    fresh.pending_attachments.clear();
660    store.put(&mut fresh)?;
661    *talk = fresh;
662    Ok(())
663}
664
665/// Reopen a closed conversation. Idempotent for the same reason [`close`] is:
666/// reopening an already-open conversation is not an error, since the
667/// operator's intent - "I want to keep talking about this" - is already
668/// satisfied.
669///
670/// Written symmetrically with [`close`]: re-reads the record under
671/// [`Talks::guard`] rather than trusting the caller's copy of `talk`, writes
672/// that fresh copy back rather than the one passed in, and errors rather than
673/// falling back to the stale copy if the re-read fails, for the same reasons
674/// `close`'s doc gives.
675pub fn reopen(talk: &mut Talk, store: &Talks) -> Result<()> {
676    let _guard = store.guard();
677    let mut fresh = store
678        .get(&talk.id)
679        .with_context(|| format!("talk {} was deleted", talk.short()))?;
680    fresh.status = TalkStatus::Open;
681    store.put(&mut fresh)?;
682    *talk = fresh;
683    Ok(())
684}
685
686/// Discard the durable draft without adding a transcript turn.
687pub fn clear_pending(talk: &mut Talk, store: &Talks) -> Result<()> {
688    let _guard = store.guard();
689    let mut fresh = store
690        .get(&talk.id)
691        .with_context(|| format!("talk {} was deleted", talk.short()))?;
692    fresh.pending.clear();
693    fresh.pending_attachments.clear();
694    store.put(&mut fresh)?;
695    *talk = fresh;
696    Ok(())
697}
698
699/// Clear a draft only when the caller still sees its complete snapshot.
700pub fn clear_pending_if_matches(
701    talk: &mut Talk,
702    store: &Talks,
703    expected_text: &str,
704    expected_attachments: &[String],
705) -> Result<bool> {
706    let _guard = store.guard();
707    let mut fresh = store
708        .get(&talk.id)
709        .with_context(|| format!("talk {} was deleted", talk.short()))?;
710    if !pending_matches(&fresh, expected_text, expected_attachments) {
711        *talk = fresh;
712        return Ok(false);
713    }
714    fresh.pending.clear();
715    fresh.pending_attachments.clear();
716    store.put(&mut fresh)?;
717    *talk = fresh;
718    Ok(true)
719}
720
721/// Replace just the text of the durable draft, but only if the caller's
722/// snapshot still identifies the entire draft. This refuses to overwrite a
723/// message another client queued or a draft the drain already promoted.
724pub fn edit_pending_text(
725    talk: &mut Talk,
726    store: &Talks,
727    text: &str,
728    expected_text: &str,
729    expected_attachments: &[String],
730) -> Result<bool> {
731    let _guard = store.guard();
732    let mut fresh = store
733        .get(&talk.id)
734        .with_context(|| format!("talk {} was deleted", talk.short()))?;
735    if !pending_matches(&fresh, expected_text, expected_attachments) {
736        *talk = fresh;
737        return Ok(false);
738    }
739    fresh.pending = text.trim().to_owned();
740    store.put(&mut fresh)?;
741    *talk = fresh;
742    Ok(true)
743}
744
745fn pending_matches(talk: &Talk, expected_text: &str, expected_attachments: &[String]) -> bool {
746    talk.pending == expected_text
747        && talk
748            .pending_attachments
749            .iter()
750            .map(|attachment| &attachment.id)
751            .eq(expected_attachments.iter())
752}
753
754/// Invoke the conversation's agent once and append what it said.
755///
756/// The first turn ever taken carries the full [`briefing`], because nothing
757/// else has told the agent what this conversation is or what it may do.
758/// Every turn after that resends nothing when the CLI can resume its own
759/// session, and falls back to [`transcript`] only when it cannot.
760async fn turn(talk: &mut Talk, store: &Talks, cfg: &Config, text: &str) -> Result<()> {
761    let spec = cfg
762        .agents
763        .iter()
764        .find(|a| a.id == talk.agent)
765        .with_context(|| {
766            format!(
767                "talk {} was opened with agent `{}`, which is no longer in \
768                 the roster; restore it in magi.toml or start a new \
769                 conversation",
770                talk.short(),
771                talk.agent
772            )
773        })?;
774
775    let resuming = agent::has_session(spec.kind, &talk.seat, cfg.graph.sessions);
776    // The newest turn is always the operator message this call is answering
777    // - `record` appended it before `turn` was ever called - so its own
778    // attachments are what belong at the end of *this* prompt.
779    let last_note = attachment_note(
780        store,
781        &talk.id,
782        talk.turns
783            .last()
784            .map_or(&[][..], |t| t.attachments.as_slice()),
785    );
786    let body = if talk.seat.turns == 0 {
787        format!(
788            "{}\n\n# Operator\n\n{text}{last_note}",
789            briefing(&talk.repo, &cfg.graph.language, cfg.talk.allow_write)
790        )
791    } else if resuming {
792        format!("{text}{last_note}")
793    } else {
794        format!("{}\n\n{text}{last_note}", transcript(talk, store))
795    };
796
797    // Every attachment this conversation has ever held, not only this
798    // turn's: a resumed session gets a fresh process every turn, so a CLI
799    // whose sandbox needs `--add-dir` (see `agent::build_command`) needs the
800    // grant again to open an image from an earlier turn, even when nothing
801    // new was attached just now.
802    let attachment_paths: Vec<PathBuf> = talk
803        .turns
804        .iter()
805        .flat_map(|t| t.attachments.iter())
806        .filter_map(|a| store.attachment_path(&talk.id, a))
807        .collect();
808
809    let artifacts = store.artifacts_of(&talk.id);
810    let stem = format!("turn-{}", talk.seat.turns + 1);
811    // The chat's build cache is the same shared one the graph's seats get, so
812    // a conversation that compiles does not mint another multi-GB target dir.
813    let cache_dir = cfg.cache_dir();
814    let inv = Invocation {
815        cwd: &talk.repo,
816        prompt: &body,
817        timeout: turn_timeout(cfg),
818        // Off unless this repository's own config opts in - see
819        // `crate::config::Talk::allow_write` and this module's doc for why
820        // the default keeps a conversational edit from landing in a checkout
821        // no run or review can claim.
822        allow_write: cfg.talk.allow_write,
823        sessions: cfg.graph.sessions,
824        artifacts: &artifacts,
825        stem: &stem,
826        // The conversation's own id, so `magi task add` run from inside it is
827        // attributed to this conversation - see `Source::Agent`.
828        run: &talk.id,
829        node: "chat",
830        cache_dir: cache_dir.as_deref(),
831        attachments: &attachment_paths,
832    };
833
834    let outcome = agent::invoke(spec, &mut talk.seat, &inv).await;
835    let note = |why: String| Turn {
836        who: Who::Agent,
837        body: format!("{MAGI_NOTE}{why}"),
838        at: Timestamp::now(),
839        attachments: Vec::new(),
840    };
841    let (reply, failure) = match outcome {
842        Err(e) => (
843            note(format!("could not run agent `{}`: {e}", talk.agent)),
844            Some(format!("could not run agent `{}`: {e}", talk.agent)),
845        ),
846        Ok(out) if out.quota_exhausted() => {
847            let reset = out
848                .quota
849                .as_ref()
850                .and_then(|q| q.reset.clone())
851                .map_or_else(String::new, |r| format!(" (resets {r})"));
852            let why = format!(
853                "agent `{}` is out of quota{reset}; your message is saved, so \
854                 say it again when the window reopens",
855                talk.agent
856            );
857            (note(why.clone()), Some(why))
858        }
859        Ok(out) if out.timed_out => {
860            let why = format!(
861                "agent `{}` did not answer within {}s; your message is saved",
862                talk.agent,
863                turn_timeout(cfg).as_secs()
864            );
865            (note(why.clone()), Some(why))
866        }
867        Ok(out) if !out.usable() => {
868            let why = format!(
869                "agent `{}` produced no answer (exit {}); your message is saved",
870                talk.agent,
871                out.exit_code
872                    .map_or_else(|| "unknown".to_owned(), |c| c.to_string())
873            );
874            (note(why.clone()), Some(why))
875        }
876        Ok(out) => (
877            Turn {
878                who: Who::Agent,
879                body: out.text.trim().to_owned(),
880                at: Timestamp::now(),
881                attachments: Vec::new(),
882            },
883            None,
884        ),
885    };
886
887    // A close landed on disk while this turn was in flight is read back here
888    // rather than trusted from the snapshot this call started with. `store`
889    // holds nothing else this function does not itself own - the turn guard
890    // in `web::Ui::begin_talk_turn` keeps `turns` and `seat` this call's
891    // alone to mutate - but `status` is not behind that guard, and an
892    // operator's close must stick: the whole point of ending a conversation
893    // is that an agent's answer to the last message before the close cannot
894    // silently reopen it. The guard is what makes that read-then-write
895    // section atomic with `close`'s own - taken only for this tail and not
896    // for the whole invocation above, so one talk's fifteen-minute turn does
897    // not block another talk's close from proceeding.
898    let _guard = store.guard();
899    // A delete is the more final version of that same race: `put` writes
900    // unconditionally, so a talk removed while this turn was in flight must
901    // stay removed rather than being written back with this turn's reply
902    // appended to it. The reply is simply given up on - there is no
903    // conversation left for it to belong to.
904    let Ok(fresh) = store.get(&talk.id) else {
905        return Ok(());
906    };
907    talk.status = fresh.status;
908    // `queue` may have accepted another operator message while the CLI was
909    // running. This handle predates that write, so preserving only `status`
910    // would overwrite the durable draft when the reply is appended below.
911    talk.pending = fresh.pending;
912    talk.pending_attachments = fresh.pending_attachments;
913    talk.turns.push(reply);
914    if let Err(put_err) = store.put(talk) {
915        // `Talks::put` already retried the write itself - reaching here
916        // means a passing race is not what this is. An agent's answer,
917        // possibly the result of an hour-long call, must not vanish with
918        // nothing to show for it just because the very last step failed:
919        // pop it back off, stash its text beside the conversation, and
920        // replace it with a note the operator can actually see, the same
921        // mechanism the failure branches above already use for a quota or a
922        // timeout.
923        let lost = talk.turns.pop().expect("just pushed above");
924        let stash = stash_lost_turn(store, &talk.id, &stem, &lost);
925        let why = match &stash {
926            Ok(path) => format!(
927                "agent `{}` answered, but the reply could not be saved to \
928                 this conversation ({put_err:#}); the raw text was kept at \
929                 {} - your message is saved, ask again",
930                talk.agent,
931                path.display()
932            ),
933            Err(stash_err) => format!(
934                "agent `{}` answered, but the reply could not be saved to \
935                 this conversation ({put_err:#}), and it could not be kept \
936                 anywhere else either ({stash_err:#}); your message is \
937                 saved, ask again",
938                talk.agent
939            ),
940        };
941        talk.turns.push(note(why.clone()));
942        // Writing the note also carries the seat this call already advanced -
943        // `agent::invoke` incremented `turns` and, for a vendor that reports
944        // its own session id, recorded that too. That is what keeps the next
945        // turn resuming the session the CLI is already holding instead of
946        // re-opening it, so losing the reply costs the transcript a turn but
947        // not the conversation.
948        return match store.put(talk) {
949            Ok(()) => bail!("{why}"),
950            Err(note_err) => {
951                // Even the short note failed to save, which means this
952                // conversation's file cannot be written at all right now -
953                // nothing is left for this call to retry or record. Pop the
954                // note so `talk.turns` matches the transcript on disk, and
955                // surface both failures for whoever reads the log.
956                //
957                // `talk.seat` is deliberately not wound back to match. The
958                // CLI really did take the turn and really did consume this
959                // seat's session id; pretending otherwise would be a second
960                // untruth on top of the unwritable file, and the handle is
961                // reloaded from disk by the next `drain` or `get` anyway -
962                // see `web::drain_loop`. What the seat cannot do is reach
963                // disk, so the record stays a turn behind the CLI until some
964                // later write lands, and a turn taken before then re-opens a
965                // session id the CLI already holds. That is the desync
966                // `20260907-011805-fb57` is about, and tolerating it belongs
967                // there rather than here: no write this branch could make
968                // would help, since a failed write is exactly what put it in
969                // this position twice over.
970                talk.turns.pop();
971                Err(note_err).context(why)
972            }
973        };
974    }
975
976    match failure {
977        Some(why) => bail!("{why}"),
978        None => Ok(()),
979    }
980}
981
982/// Everything said so far, as prose, for a CLI that cannot resume its own
983/// conversation.
984fn transcript(talk: &Talk, store: &Talks) -> String {
985    let mut out = String::from(
986        "This conversation cannot resume on the CLI's side, so here is \
987         everything said so far; answer only the last message.\n",
988    );
989    for t in &talk.turns {
990        let who = match t.who {
991            Who::Operator => "operator",
992            Who::Agent => "you",
993        };
994        out.push_str(&format!("\n## {who}\n\n{}\n", t.body.trim()));
995        out.push_str(&attachment_note(store, &talk.id, &t.attachments));
996    }
997    out
998}
999
1000/// The section named at the end of a turn's body, listing every attachment's
1001/// absolute path and mime so the agent knows exactly what to open. Empty
1002/// when `attachments` is, which is every turn but the rare one carrying an
1003/// image, so a turn with none changes nothing about the prompt.
1004fn attachment_note(store: &Talks, talk_id: &str, attachments: &[Attachment]) -> String {
1005    if attachments.is_empty() {
1006        return String::new();
1007    }
1008    let mut out = String::from(
1009        "\n\nThe operator attached the image(s) below to this message. Open \
1010         and look at each one before you answer.\n",
1011    );
1012    for att in attachments {
1013        if let Some(path) = store.attachment_path(talk_id, att) {
1014            out.push_str(&format!("\n- {} ({})", path.display(), att.mime));
1015        }
1016    }
1017    out.push('\n');
1018    out
1019}
1020
1021/// The briefing the agent opens with, sent once as part of its first turn.
1022///
1023/// Pure, so the properties that matter can be asserted without an interview:
1024/// it names `magi task add --solo` (the route this conversation always has to
1025/// changing anything) and it never tells the agent to write a task *file* of
1026/// its own - that would compete with filing through the queue.
1027/// `allow_write` only ever adds an extra permission on top of that; it never
1028/// removes the queue as an option, which is why both branches keep the same
1029/// `# When the operator wants something done` section - `write_policy` is
1030/// the only part that changes.
1031///
1032/// It also tells the agent that `--repo` is not stuck naming this
1033/// conversation's own directory: `resolve_repo` (`src/main.rs`) now accepts a
1034/// short `owner/repo` or bare `repo` name and resolves it against
1035/// `[repos] roots`, the same local checkouts `magi repos` lists. Without this
1036/// line an agent asked to change some other repository has no way to know
1037/// that option exists, and the only path it can see - asking the operator to
1038/// dictate a full path - is exactly the friction this change exists to
1039/// remove. A miss or an ambiguous name still fails the command outright, so
1040/// the instruction is to ask rather than guess when that happens - the
1041/// silent-decision line this task must not cross.
1042pub fn briefing(repo: &Path, language: &str, allow_write: bool) -> String {
1043    let write_policy = if allow_write {
1044        "Write access is enabled for this conversation (`allow_write = \
1045         true`), so you may write files - but only a small, \
1046         already-decided edit the operator names outright in this \
1047         conversation, not an implementation. This is a permission on the \
1048         conversation as a whole, not a property of whichever repository \
1049         it happened to start in: if the operator names a different \
1050         repository for that small edit, the policy allows it there too. \
1051         Your own tool may still confine writes to the repository this \
1052         conversation started in regardless - if a write elsewhere is \
1053         refused, say so plainly rather than working around it. Once you \
1054         have made an edit, say plainly what you edited. Anything bigger, \
1055         or anything still open-ended, still goes through the queue below \
1056         rather than being done here."
1057    } else {
1058        "Do not write files. Implementing a change is not this \
1059         conversation's job; a separate, blind competition of agents does \
1060         that, and a repository this conversation has already edited would \
1061         make their diffs unjudgeable."
1062    };
1063    let mut out = format!(
1064        "You are magi's standing conversation partner for its operator, who \
1065         usually has this open on a phone. Keep replies short: no preamble, \
1066         no restating what they just said.\n\n\
1067         # Repository\n\n{repo}\n\n\
1068         You may look around: read files, run shell commands, search history, \
1069         run tests - whatever answers the question. {write_policy}\n\n\
1070         A short, command-shaped message (\"list\", \"info <id>\", \"show \
1071         3cbf\") is almost always the operator asking you to look something \
1072         up, not an instruction to file - answer it yourself with `magi \
1073         list`, `magi show <id>`, `magi task list`, or the like, the same way \
1074         you would answer any other question in this conversation.\n\n\
1075         # When the operator wants something done\n\n\
1076         Run:\n\n\
1077         magi task add --solo --repo {repo} <instruction>\n\n\
1078         and tell the operator the task id it prints, so they can follow it \
1079         from the Queue. If it refuses with a duplicate warning (the \
1080         instruction names a branch, commit or pull request that an \
1081         unfinished task, run or PR already owns), do not repeat it with \
1082         --force yourself: tell the operator what it matched and let them \
1083         decide. Write <instruction> so that an implementer who has \
1084         never seen this conversation can act on it alone - it is everything \
1085         they get. Use --solo: it runs the task through one implementer \
1086         straight into review instead of the usual multi-agent competition, \
1087         which is the right shape for a change this conversation has already \
1088         settled, rather than one still worth several independent takes.\n\n\
1089         If the operator asks for something in a different repository, \
1090         --repo does not have to be a full path: --repo owner/repo (or just \
1091         repo, when that is unambiguous) is resolved against local checkouts \
1092         the same way `magi repos` lists them. If the command fails because \
1093         nothing matches or more than one checkout shares that name, ask the \
1094         operator which repository they mean (or run `magi repos` yourself \
1095         to see the candidates) rather than guessing.\n\n\
1096         If the operator attached an image (a screenshot, say) that the task \
1097         is about, pass it with `--attach <path>`, using the absolute path \
1098         the turn's attachment note gives; repeat the flag for several. \
1099         `magi task add --solo --attach <path> <instruction>` copies the \
1100         file into the task, so the implementer receives it. Do not paste the \
1101         path into <instruction> instead: deleting this conversation deletes \
1102         its attachments, and then that path reaches no one.\n",
1103        repo = repo.display(),
1104    );
1105    out.push_str(&language_note(language));
1106    out
1107}
1108
1109/// The operator is talking, so their language matters here more than in most
1110/// prompts magi sends.
1111fn language_note(language: &str) -> String {
1112    if language.trim().is_empty() || language.eq_ignore_ascii_case("en") {
1113        String::new()
1114    } else {
1115        format!("\nHold this conversation in {language}.\n")
1116    }
1117}
1118
1119/// Queue tasks this conversation has filed, oldest first.
1120///
1121/// A task is this conversation's when its [`Source::Agent`] names this
1122/// conversation's id as `run` - which is exactly what happens when
1123/// `magi task add` is run from inside a turn, because [`turn`] passes the
1124/// conversation's own id as [`Invocation::run`].
1125pub fn tasks_of(queue: &Queue, talk_id: &str) -> Vec<Task> {
1126    let mut tasks: Vec<Task> = queue
1127        .list()
1128        .into_iter()
1129        .filter(|t| matches!(&t.source, Source::Agent { run, .. } if run == talk_id))
1130        .collect();
1131    tasks.sort_unstable_by(|a, b| a.id.cmp(&b.id));
1132    tasks
1133}
1134
1135fn read_path(path: &Path) -> Result<Talk> {
1136    let body = std::fs::read_to_string(path).with_context(|| format!("read {}", path.display()))?;
1137    serde_json::from_str(&body).with_context(|| format!("parse {}", path.display()))
1138}
1139
1140/// How many times [`write_atomic`] retries a failed write-then-rename before
1141/// giving up.
1142const PUT_RETRIES: u32 = 5;
1143
1144/// Write `body` to `tmp` and rename it onto `path`, retrying the whole thing
1145/// a handful of times with a short sleep in between.
1146///
1147/// The only failure this is meant to absorb is a passing one - most
1148/// concretely, a reader elsewhere in this process (or another `magi`
1149/// process) with `path` briefly open for `read_to_string` at the exact
1150/// moment this call tries to rename over it. That clears in milliseconds
1151/// once the reader lets go; a caller still failing after several short
1152/// sleeps has something more durable wrong (a full disk, a permissions
1153/// change) that a longer sleep would not fix either, and is left to report
1154/// it.
1155fn write_atomic(tmp: &Path, path: &Path, body: &str) -> Result<()> {
1156    let mut last_err = None;
1157    for attempt in 0..PUT_RETRIES {
1158        if attempt > 0 {
1159            std::thread::sleep(Duration::from_millis(20 * u64::from(attempt)));
1160        }
1161        match try_write_atomic(tmp, path, body) {
1162            Ok(()) => return Ok(()),
1163            Err(e) => last_err = Some(e),
1164        }
1165    }
1166    Err(last_err.expect("the loop above always runs at least once"))
1167}
1168
1169fn try_write_atomic(tmp: &Path, path: &Path, body: &str) -> Result<()> {
1170    #[cfg(test)]
1171    if failpoint::take_forced_put_failure() {
1172        bail!("simulated write failure (test)");
1173    }
1174    std::fs::write(tmp, body).with_context(|| format!("write {}", tmp.display()))?;
1175    std::fs::rename(tmp, path).with_context(|| format!("replace {}", path.display()))?;
1176    Ok(())
1177}
1178
1179/// Last resort when `turn`'s own `store.put` fails even after
1180/// [`write_atomic`]'s retries: keep the generated text somewhere still
1181/// findable rather than let the whole of an agent's answer disappear along
1182/// with the write that was supposed to record it.
1183fn stash_lost_turn(store: &Talks, id: &str, stem: &str, reply: &Turn) -> Result<PathBuf> {
1184    let dir = store.artifacts_of(id);
1185    std::fs::create_dir_all(&dir).with_context(|| format!("create {}", dir.display()))?;
1186    let path = dir.join(format!("{stem}-lost.txt"));
1187    std::fs::write(&path, &reply.body).with_context(|| format!("write {}", path.display()))?;
1188    Ok(path)
1189}
1190
1191/// A test-only seam that lets [`try_write_atomic`] simulate the kind of
1192/// passing I/O race [`write_atomic`] is meant to retry through, without
1193/// depending on real OS-level file-locking behaviour, which differs across
1194/// the three platforms this crate ships on (and, on the one platform where a
1195/// reader really does block a rename, is awkward to trigger deterministically
1196/// in a unit test).
1197#[cfg(test)]
1198mod failpoint {
1199    use std::cell::Cell;
1200
1201    thread_local! {
1202        static FORCE_PUT_FAILURES: Cell<u32> = const { Cell::new(0) };
1203    }
1204
1205    /// Arrange for the next `count` calls into [`super::try_write_atomic`] to
1206    /// fail before touching the filesystem at all.
1207    pub(super) fn force_put_failures(count: u32) {
1208        FORCE_PUT_FAILURES.with(|c| c.set(count));
1209    }
1210
1211    /// Consumed once per attempt inside [`super::try_write_atomic`]; `true`
1212    /// means simulate this attempt failing.
1213    pub(super) fn take_forced_put_failure() -> bool {
1214        FORCE_PUT_FAILURES.with(|c| {
1215            let n = c.get();
1216            if n == 0 {
1217                false
1218            } else {
1219                c.set(n - 1);
1220                true
1221            }
1222        })
1223    }
1224}
1225
1226fn short(id: &str) -> &str {
1227    id.split('-').next_back().unwrap_or(id)
1228}
1229
1230fn new_id() -> String {
1231    let stamp = jiff::Zoned::now().strftime("%Y%m%d-%H%M%S");
1232    let seed = crate::rng::entropy();
1233    format!("{stamp}-{:04x}", (seed ^ (seed >> 32)) & 0xffff)
1234}
1235
1236/// Extension an attachment's bytes are stored under, from its (already
1237/// validated) mime. The one place this mapping exists on the write side;
1238/// `web`'s own whitelist is what actually decides which mimes are accepted
1239/// in the first place.
1240fn attachment_ext(mime: &str) -> Option<&'static str> {
1241    match mime {
1242        "image/png" => Some("png"),
1243        "image/jpeg" => Some("jpg"),
1244        "image/gif" => Some("gif"),
1245        "image/webp" => Some("webp"),
1246        _ => None,
1247    }
1248}
1249
1250/// Is `id` a shape [`put_attachment`](Talks::put_attachment) could have
1251/// produced? 32 lowercase hex digits and nothing else, checked before an id
1252/// that came from the client is ever allowed to build a path - so `..` and a
1253/// path separator are never even possible.
1254pub fn valid_attachment_id(id: &str) -> bool {
1255    id.len() == 32
1256        && id
1257            .bytes()
1258            .all(|b| b.is_ascii_digit() || (b'a'..=b'f').contains(&b))
1259}
1260
1261/// A fresh attachment id: 128 bits of process entropy as lowercase hex - the
1262/// same "mint it, never take it from the client" rule [`new_id`] follows for
1263/// conversation ids.
1264fn new_attachment_id() -> String {
1265    let mut r = crate::rng::SplitMix64::new(crate::rng::entropy());
1266    format!("{:016x}{:016x}", r.next_u64(), r.next_u64())
1267}
1268
1269#[cfg(test)]
1270mod tests {
1271    use std::collections::BTreeMap;
1272
1273    use crate::config::{AgentKind, AgentSpec, Graph};
1274    use crate::queue::{Queue, Source, Task};
1275
1276    use super::*;
1277
1278    /// A store of its own, with no process-global state.
1279    fn store() -> (tempfile::TempDir, Talks) {
1280        let tmp = tempfile::tempdir().expect("tempdir");
1281        let talks = Talks::at(tmp.path().join("talks"));
1282        (tmp, talks)
1283    }
1284
1285    /// A `kind = "command"` agent whose whole behaviour is a POSIX shell
1286    /// script - see `chat`'s tests for why no test here may spawn a real
1287    /// agent CLI.
1288    fn mock_agent(dir: &Path, script: &str, env: BTreeMap<String, String>) -> AgentSpec {
1289        let path = dir.join("mock-talk-agent.sh");
1290        std::fs::write(&path, script).expect("write mock");
1291        AgentSpec {
1292            id: "mock".to_owned(),
1293            kind: AgentKind::Command,
1294            model: None,
1295            command: vec!["sh".to_owned(), path.to_string_lossy().into_owned()],
1296            extra_args: Vec::new(),
1297            env,
1298            prompt_delivery: None,
1299        }
1300    }
1301
1302    fn config(spec: AgentSpec) -> Config {
1303        Config {
1304            agents: vec![spec],
1305            graph: Graph {
1306                language: "en".to_owned(),
1307                ..Graph::default()
1308            },
1309            ..Config::default()
1310        }
1311    }
1312
1313    /// Echo a canned reply, ignoring the prompt on stdin.
1314    const REPLY: &str = "#!/bin/sh\ncat >/dev/null\nprintf '%s\\n' \"$MOCK_REPLY\"\n";
1315
1316    /// Say nothing and fail, the way a CLI that cannot start does.
1317    const BROKEN: &str = "#!/bin/sh\ncat >/dev/null\nexit 3\n";
1318
1319    /// Reply with the prompt it was given, so a test can inspect exactly what
1320    /// the agent received on stdin.
1321    const ECHO: &str = "#!/bin/sh\ncat\n";
1322
1323    fn env(reply: &str) -> BTreeMap<String, String> {
1324        BTreeMap::from([("MOCK_REPLY".to_owned(), reply.to_owned())])
1325    }
1326
1327    #[test]
1328    fn the_frozen_json_field_names_round_trip_through_disk() {
1329        let (tmp, talks) = store();
1330        let mut talk = Talk {
1331            schema: SCHEMA,
1332            id: "20260904-014455-ab12".to_owned(),
1333            repo: tmp.path().to_owned(),
1334            agent: "sonnet".to_owned(),
1335            status: TalkStatus::Open,
1336            turns: Vec::new(),
1337            pending: String::new(),
1338            pending_attachments: Vec::new(),
1339            created_at: Timestamp::now(),
1340            updated_at: Timestamp::now(),
1341            seat: SeatState::new(SEAT, "sonnet", 7),
1342        };
1343        talks.put(&mut talk).expect("put");
1344
1345        let raw = std::fs::read_to_string(talks.path_of(&talk.id)).expect("read back");
1346        let v: serde_json::Value = serde_json::from_str(&raw).expect("parse");
1347        for field in [
1348            "schema",
1349            "id",
1350            "repo",
1351            "agent",
1352            "status",
1353            "turns",
1354            "created_at",
1355            "updated_at",
1356        ] {
1357            assert!(v.get(field).is_some(), "missing field `{field}`");
1358        }
1359        assert_eq!(v["schema"], 1);
1360        assert_eq!(v["status"], "open");
1361
1362        let back = talks.get(&talk.id).expect("get");
1363        assert_eq!(back.id, talk.id);
1364        assert_eq!(back.status, TalkStatus::Open);
1365    }
1366
1367    #[test]
1368    fn opening_a_talk_takes_no_agent_turn() {
1369        let (tmp, talks) = store();
1370        // A script that would fail loudly if it were ever run: `begin` must
1371        // not invoke anything, since there is nothing yet for an agent to
1372        // answer.
1373        let spec = mock_agent(tmp.path(), BROKEN, BTreeMap::new());
1374        let cfg = config(spec);
1375
1376        let talk = begin(&talks, &cfg, tmp.path().to_owned(), None).expect("begin");
1377        assert_eq!(talk.status, TalkStatus::Open);
1378        assert!(talk.turns.is_empty(), "nothing has been said yet");
1379
1380        let on_disk = talks.get(&talk.id).expect("get");
1381        assert_eq!(on_disk.turns.len(), 0);
1382    }
1383
1384    /// `[roles] chatter`, when set, decides who holds this conversation; unset,
1385    /// it falls back to [`agent::pick`]'s own default order (a claude seat,
1386    /// else the first runnable agent in roster order) rather than to any
1387    /// other role - see `[roles] chatter`'s own doc in [`crate::config`] for
1388    /// why a dedicated field exists at all: opening this against the same
1389    /// seat as a judge is what produced the `agent ... did not answer within
1390    /// 300s` timeout that led to it.
1391    #[test]
1392    fn chatter_wins_when_set_and_falls_back_to_pick_s_default_order_otherwise() {
1393        let (tmp, talks) = store();
1394        let first_spec = mock_agent(tmp.path(), BROKEN, BTreeMap::new());
1395        let mut chatter_spec = mock_agent(tmp.path(), BROKEN, BTreeMap::new());
1396        chatter_spec.id = "chatter-mock".to_owned();
1397
1398        let mut cfg = Config {
1399            agents: vec![first_spec.clone(), chatter_spec.clone()],
1400            graph: Graph {
1401                language: "en".to_owned(),
1402                ..Graph::default()
1403            },
1404            ..Config::default()
1405        };
1406        cfg.roles.chatter = Some(chatter_spec.id.clone());
1407
1408        let talk =
1409            begin(&talks, &cfg, tmp.path().to_owned(), None).expect("begin with chatter set");
1410        assert_eq!(talk.agent, chatter_spec.id, "an explicit chatter must win");
1411
1412        cfg.roles.chatter = None;
1413        let fallback =
1414            begin(&talks, &cfg, tmp.path().to_owned(), None).expect("begin with chatter unset");
1415        assert_eq!(
1416            fallback.agent, first_spec.id,
1417            "unset chatter must fall back to agent::pick's own default order"
1418        );
1419    }
1420
1421    /// A conversation recorded before attachments existed - schema 1, no
1422    /// `attachments` key on any turn - must still read.
1423    #[test]
1424    fn a_talk_recorded_without_attachments_still_reads() {
1425        let (tmp, talks) = store();
1426        let path = talks.path_of("20260904-014455-ab12");
1427        std::fs::create_dir_all(talks.root()).expect("talks dir");
1428        std::fs::write(
1429            &path,
1430            serde_json::json!({
1431                "schema": 1,
1432                "id": "20260904-014455-ab12",
1433                "repo": tmp.path(),
1434                "agent": "sonnet",
1435                "status": "open",
1436                "turns": [
1437                    { "who": "operator", "body": "still there?",
1438                      "at": Timestamp::now().to_string() },
1439                ],
1440                "created_at": Timestamp::now().to_string(),
1441                "updated_at": Timestamp::now().to_string(),
1442                "seat": SeatState::new(SEAT, "sonnet", 7),
1443            })
1444            .to_string(),
1445        )
1446        .expect("write pre-attachments talk");
1447
1448        let talk = talks.get("20260904-014455-ab12").expect("must still read");
1449        assert!(talk.turns[0].attachments.is_empty());
1450    }
1451
1452    #[test]
1453    fn queued_text_is_durable_combined_and_drained_as_one_operator_turn() {
1454        let (tmp, talks) = store();
1455        let cfg = config(mock_agent(tmp.path(), REPLY, env("reply")));
1456        let mut talk = begin(&talks, &cfg, tmp.path().to_owned(), None).expect("begin");
1457
1458        queue(&mut talk, &talks, "first", Vec::new()).expect("queue first");
1459        queue(&mut talk, &talks, "second", Vec::new()).expect("queue second");
1460        let saved = talks.get(&talk.id).expect("reload queued talk");
1461        assert_eq!(saved.pending, "first\n\nsecond");
1462        assert!(saved.turns.is_empty(), "a draft is not a transcript turn");
1463
1464        let drained = drain(&mut talk, &talks).expect("drain");
1465        assert_eq!(drained.as_deref(), Some("first\n\nsecond"));
1466        let saved = talks.get(&talk.id).expect("reload drained talk");
1467        assert!(saved.pending.is_empty());
1468        assert_eq!(saved.turns.len(), 1);
1469        assert_eq!(saved.turns[0].body, "first\n\nsecond");
1470    }
1471
1472    #[test]
1473    fn editing_a_queued_draft_preserves_its_attachments_and_rejects_a_stale_snapshot() {
1474        let (tmp, talks) = store();
1475        let cfg = config(mock_agent(tmp.path(), REPLY, env("reply")));
1476        let mut talk = begin(&talks, &cfg, tmp.path().to_owned(), None).expect("begin");
1477        let attachment = Attachment {
1478            id: "a".repeat(32),
1479            name: "shot.png".to_owned(),
1480            mime: "image/png".to_owned(),
1481            bytes: 3,
1482        };
1483
1484        queue(&mut talk, &talks, "first", vec![attachment.clone()]).expect("queue");
1485        assert!(
1486            edit_pending_text(
1487                &mut talk,
1488                &talks,
1489                "corrected",
1490                "first",
1491                std::slice::from_ref(&attachment.id),
1492            )
1493            .expect("edit")
1494        );
1495        let saved = talks.get(&talk.id).expect("reload edited draft");
1496        assert_eq!(saved.pending, "corrected");
1497        assert_eq!(saved.pending_attachments, vec![attachment]);
1498
1499        queue(&mut talk, &talks, "later", Vec::new()).expect("queue concurrent draft");
1500        assert!(
1501            !edit_pending_text(
1502                &mut talk,
1503                &talks,
1504                "stale edit",
1505                "corrected",
1506                &["a".repeat(32)],
1507            )
1508            .expect("stale edit is a conflict")
1509        );
1510        assert_eq!(
1511            talks.get(&talk.id).expect("reload after conflict").pending,
1512            "corrected\n\nlater"
1513        );
1514        assert!(
1515            !clear_pending_if_matches(&mut talk, &talks, "corrected", &["a".repeat(32)])
1516                .expect("stale clear is a conflict")
1517        );
1518        assert_eq!(
1519            talks
1520                .get(&talk.id)
1521                .expect("reload after stale clear")
1522                .pending,
1523            "corrected\n\nlater"
1524        );
1525    }
1526
1527    #[tokio::test]
1528    async fn a_reply_save_preserves_pending_accepted_while_the_cli_runs() {
1529        let (tmp, talks) = store();
1530        let slow = "#!/bin/sh\ncat >/dev/null\nsleep 0.1\nprintf reply\n";
1531        let cfg = config(mock_agent(tmp.path(), slow, BTreeMap::new()));
1532        let mut running = begin(&talks, &cfg, tmp.path().to_owned(), None).expect("begin");
1533        let id = running.id.clone();
1534        let first = record(&mut running, &talks, "first", Vec::new()).expect("record");
1535
1536        let response_talks = talks.clone();
1537        let response_cfg = cfg.clone();
1538        let reply = tokio::spawn(async move {
1539            respond(&mut running, &response_talks, &response_cfg, &first).await
1540        });
1541        tokio::time::sleep(std::time::Duration::from_millis(20)).await;
1542
1543        let mut queued = talks.get(&id).expect("queued handle");
1544        queue(&mut queued, &talks, "next", Vec::new()).expect("queue");
1545        reply.await.expect("join").expect("reply");
1546
1547        let saved = talks.get(&id).expect("reload");
1548        assert_eq!(saved.pending, "next");
1549        assert_eq!(saved.turns.len(), 2, "operator message and reply remain");
1550    }
1551
1552    #[tokio::test]
1553    async fn the_first_turn_carries_the_briefing_and_later_turns_do_not() {
1554        let (tmp, talks) = store();
1555        let spec = mock_agent(tmp.path(), ECHO, BTreeMap::new());
1556        let cfg = config(spec);
1557        let mut talk = begin(&talks, &cfg, tmp.path().to_owned(), None).expect("begin");
1558
1559        say(
1560            &mut talk,
1561            &talks,
1562            &cfg,
1563            "what does the queue module do?",
1564            Vec::new(),
1565        )
1566        .await
1567        .expect("first turn");
1568        let first_prompt = &talk.turns[1].body;
1569        assert!(first_prompt.contains("magi task add --solo"));
1570        assert!(first_prompt.contains("what does the queue module do?"));
1571
1572        say(&mut talk, &talks, &cfg, "and how is it locked?", Vec::new())
1573            .await
1574            .expect("second turn");
1575        let second_prompt = &talk.turns[3].body;
1576        assert!(
1577            !second_prompt.contains("magi task add --solo"),
1578            "the briefing is sent once, not on every turn: {second_prompt}"
1579        );
1580        assert!(second_prompt.contains("and how is it locked?"));
1581    }
1582
1583    #[tokio::test]
1584    async fn say_appends_the_operator_turn_then_the_agent_turn() {
1585        let (tmp, talks) = store();
1586        let spec = mock_agent(tmp.path(), REPLY, env("go ahead"));
1587        let cfg = config(spec);
1588        let mut talk = begin(&talks, &cfg, tmp.path().to_owned(), None).expect("begin");
1589
1590        say(
1591            &mut talk,
1592            &talks,
1593            &cfg,
1594            "can I rename this function?",
1595            Vec::new(),
1596        )
1597        .await
1598        .expect("say");
1599
1600        assert_eq!(talk.turns.len(), 2);
1601        assert_eq!(talk.turns[0].who, Who::Operator);
1602        assert_eq!(talk.turns[0].body, "can I rename this function?");
1603        assert_eq!(talk.turns[1].who, Who::Agent);
1604        assert_eq!(talk.turns[1].body, "go ahead");
1605        assert_eq!(talks.get(&talk.id).expect("get").turns, talk.turns);
1606    }
1607
1608    #[tokio::test]
1609    async fn a_failed_turn_keeps_the_operator_message_and_says_what_happened() {
1610        let (tmp, talks) = store();
1611        let spec = mock_agent(tmp.path(), BROKEN, BTreeMap::new());
1612        let cfg = config(spec);
1613        let mut talk = begin(&talks, &cfg, tmp.path().to_owned(), None).expect("begin");
1614
1615        let err = say(&mut talk, &talks, &cfg, "check the tests", Vec::new())
1616            .await
1617            .expect_err("a turn with no answer is an error");
1618        assert!(err.to_string().contains("no answer"), "{err}");
1619
1620        let on_disk = talks.get(&talk.id).expect("get");
1621        assert_eq!(on_disk.turns.len(), 2);
1622        assert_eq!(on_disk.turns[0].body, "check the tests");
1623        let note = &on_disk.turns[1];
1624        assert_eq!(note.who, Who::Agent);
1625        assert!(note.body.starts_with(MAGI_NOTE), "{}", note.body);
1626        assert!(note.body.contains("your message is saved"));
1627    }
1628
1629    /// The failure this stands in for: a reader elsewhere briefly has the
1630    /// talk file open right when `turn` tries to save the reply, and the
1631    /// write-then-rename fails once or twice before the reader lets go.
1632    /// `write_atomic`'s own retries must absorb that with nobody the wiser -
1633    /// no gap in the transcript, no dropped turn.
1634    #[tokio::test]
1635    async fn a_passing_write_failure_while_saving_the_reply_does_not_lose_it() {
1636        let (tmp, talks) = store();
1637        let spec = mock_agent(tmp.path(), REPLY, env("go ahead"));
1638        let cfg = config(spec);
1639        let mut talk = begin(&talks, &cfg, tmp.path().to_owned(), None).expect("begin");
1640
1641        let text =
1642            record(&mut talk, &talks, "can I rename this function?", Vec::new()).expect("record");
1643        // One fewer failure than `write_atomic` will retry through, so the
1644        // very last attempt must succeed.
1645        failpoint::force_put_failures(PUT_RETRIES - 1);
1646        respond(&mut talk, &talks, &cfg, &text)
1647            .await
1648            .expect("respond must survive a write failure its own retries can outlast");
1649
1650        assert_eq!(talk.turns.len(), 2);
1651        assert_eq!(talk.turns[1].who, Who::Agent);
1652        assert_eq!(talk.turns[1].body, "go ahead");
1653        let on_disk = talks.get(&talk.id).expect("get");
1654        assert_eq!(
1655            on_disk.turns, talk.turns,
1656            "the reply must reach disk despite the early write failures"
1657        );
1658    }
1659
1660    /// When the write-then-rename never recovers - standing in for a disk
1661    /// that stays unwritable rather than a reader that eventually lets go -
1662    /// the reply must not disappear without a trace the way it did in the
1663    /// real incident this repository saw: no error on the phone, no note in
1664    /// the transcript, and the turn simply gone from `talks/<id>.json`.
1665    #[tokio::test]
1666    async fn a_persistent_write_failure_while_saving_the_reply_is_never_silent() {
1667        let (tmp, talks) = store();
1668        let spec = mock_agent(tmp.path(), REPLY, env("go ahead"));
1669        let cfg = config(spec);
1670        let mut talk = begin(&talks, &cfg, tmp.path().to_owned(), None).expect("begin");
1671
1672        let text = record(&mut talk, &talks, "check the tests", Vec::new()).expect("record");
1673        // Exactly enough forced failures to exhaust the reply's own retries;
1674        // the shorter note that replaces it then saves cleanly, which is the
1675        // common case this exercises - a large write racing something,
1676        // followed by a small one that does not.
1677        failpoint::force_put_failures(PUT_RETRIES);
1678        let err = respond(&mut talk, &talks, &cfg, &text)
1679            .await
1680            .expect_err("a reply that cannot be saved must be reported, not swallowed");
1681        assert!(err.to_string().contains("could not be saved"), "{err}");
1682
1683        let on_disk = talks.get(&talk.id).expect("get");
1684        assert_eq!(
1685            on_disk.turns.len(),
1686            2,
1687            "the operator turn plus a visible note"
1688        );
1689        assert_eq!(on_disk.turns[0].body, "check the tests");
1690        let note = &on_disk.turns[1];
1691        assert_eq!(note.who, Who::Agent);
1692        assert!(note.body.starts_with(MAGI_NOTE), "{}", note.body);
1693        assert!(
1694            note.body.contains("could not be saved"),
1695            "the operator must be told the reply is missing, not left staring \
1696             at a gap with no explanation: {}",
1697            note.body
1698        );
1699        assert_eq!(
1700            talk.turns, on_disk.turns,
1701            "the in-memory talk must match what actually landed on disk"
1702        );
1703
1704        // The generated answer itself must still be recoverable, not merely
1705        // reported as lost.
1706        let artifacts = talks.artifacts_of(&talk.id);
1707        let stash = std::fs::read_dir(&artifacts)
1708            .expect("artifacts dir")
1709            .filter_map(|e| e.ok())
1710            .find(|e| e.file_name().to_string_lossy().ends_with("-lost.txt"))
1711            .expect("a stash file for the lost reply");
1712        let stashed = std::fs::read_to_string(stash.path()).expect("read stash");
1713        assert_eq!(stashed, "go ahead");
1714
1715        // Losing the reply must not also lose the seat. The CLI took a turn
1716        // and consumed this seat's session id; if the note's write left the
1717        // record claiming otherwise, the next turn would re-open a session
1718        // the CLI is already holding - the `20260907-011805-fb57` desync -
1719        // and would re-send the whole briefing besides. Both decisions read
1720        // the seat straight off disk (`agent::has_session` and `turn`'s own
1721        // `seat.turns == 0` branch), so this is the field that has to match.
1722        assert_eq!(
1723            on_disk.seat.turns, 1,
1724            "the note's write must carry the turn the CLI actually took"
1725        );
1726        assert_eq!(
1727            on_disk.seat.claude_session, talk.seat.claude_session,
1728            "the session id handed to the CLI must survive the failed reply"
1729        );
1730        assert_eq!(on_disk.seat.captured_session, talk.seat.captured_session);
1731        assert!(
1732            agent::has_session(AgentKind::Command, &on_disk.seat, cfg.graph.sessions),
1733            "the next turn must resume, not open the same session id twice"
1734        );
1735    }
1736
1737    /// Even the note can fail to save, if the disk stays unwritable for long
1738    /// enough. `respond` must still report the failure rather than pretend
1739    /// the turn succeeded, and must not leave the in-memory `talk` claiming
1740    /// a turn that never reached disk.
1741    #[tokio::test]
1742    async fn a_write_failure_that_also_loses_the_note_still_reports_it() {
1743        let (tmp, talks) = store();
1744        let spec = mock_agent(tmp.path(), REPLY, env("go ahead"));
1745        let cfg = config(spec);
1746        let mut talk = begin(&talks, &cfg, tmp.path().to_owned(), None).expect("begin");
1747
1748        let text = record(&mut talk, &talks, "check the tests", Vec::new()).expect("record");
1749        // Enough forced failures to exhaust the retries for both the reply
1750        // and the note that would have replaced it.
1751        failpoint::force_put_failures(PUT_RETRIES * 2);
1752        let err = respond(&mut talk, &talks, &cfg, &text)
1753            .await
1754            .expect_err("neither the reply nor the note could be saved");
1755        assert!(err.to_string().contains("could not be saved"), "{err}");
1756
1757        assert_eq!(talk.turns.len(), 1, "only the operator's own turn");
1758        let on_disk = talks.get(&talk.id).expect("get");
1759        assert_eq!(on_disk.turns.len(), 1);
1760
1761        // Nothing at all reached disk, so the seat could not either: the CLI
1762        // took a turn the record does not know about. That is pinned here as
1763        // the known cost of a file that cannot be written twice over, not as
1764        // something this branch could do better - the only way to record the
1765        // seat is the write that just failed. It is also the point where
1766        // this meets `20260907-011805-fb57`: a turn taken before some later
1767        // write lands would re-open a session id the CLI already holds. The
1768        // in-memory seat keeps the truth the CLI reported, which is why it is
1769        // not wound back to match.
1770        assert_eq!(
1771            on_disk.seat.turns, 0,
1772            "an unwritable file cannot record the turn the CLI took"
1773        );
1774        assert_eq!(
1775            talk.seat.turns, 1,
1776            "the in-memory seat still reports the turn the CLI actually took"
1777        );
1778        assert_eq!(
1779            on_disk.seat.claude_session, talk.seat.claude_session,
1780            "the session id was minted at `begin` and never changes here"
1781        );
1782    }
1783
1784    /// An attachment lets the operator send an otherwise-empty message, and
1785    /// its absolute path is what actually reaches the agent's prompt - here
1786    /// on the very first turn, where it has to share the briefing.
1787    #[tokio::test]
1788    async fn attachments_reach_the_prompt_and_an_empty_body_is_still_a_turn() {
1789        let (tmp, talks) = store();
1790        let spec = mock_agent(tmp.path(), ECHO, BTreeMap::new());
1791        let cfg = config(spec);
1792        let mut talk = begin(&talks, &cfg, tmp.path().to_owned(), None).expect("begin");
1793
1794        let att = talks
1795            .put_attachment(
1796                &talk.id,
1797                "image/png",
1798                "screenshot.png",
1799                b"pretend-png-bytes",
1800            )
1801            .expect("put attachment");
1802
1803        say(&mut talk, &talks, &cfg, "", vec![att.clone()])
1804            .await
1805            .expect("an empty body with an attachment is still a turn");
1806
1807        let operator_turn = &talk.turns[0];
1808        assert_eq!(operator_turn.who, Who::Operator);
1809        assert_eq!(operator_turn.body, "");
1810        assert_eq!(operator_turn.attachments, vec![att.clone()]);
1811
1812        let prompt = &talk.turns[1].body;
1813        let expected_path = talks
1814            .attachments_dir(&talk.id)
1815            .join(format!("{}.png", att.id));
1816        assert!(
1817            prompt.contains(&expected_path.display().to_string()),
1818            "the agent must be told the attachment's absolute path: {prompt}"
1819        );
1820        assert!(prompt.contains("image/png"), "and its mime: {prompt}");
1821    }
1822
1823    /// See `chat`'s test of the same name: `run::home()` returns a bare
1824    /// relative `PathBuf` verbatim when `MAGI_HOME` is set to a relative
1825    /// path, so a `Talks` store built on it has a relative `root` too. That
1826    /// is fine for this store's own I/O, which runs in this process against
1827    /// this process's cwd, but `attachment_path` hands its result to a
1828    /// *different* process invoked with `cwd: &talk.repo` - an uncorrected
1829    /// relative path would resolve against the repository instead of
1830    /// wherever the attachment actually landed.
1831    #[test]
1832    fn attachment_path_is_absolute_even_when_the_store_root_is_relative() {
1833        let talks = Talks::at(PathBuf::from("relative-talks-root-for-this-test"));
1834        let att = Attachment {
1835            id: "0".repeat(32),
1836            name: "shot.png".to_owned(),
1837            mime: "image/png".to_owned(),
1838            bytes: 3,
1839        };
1840        let path = talks
1841            .attachment_path("some-talk-id", &att)
1842            .expect("a supported mime always yields a path");
1843        assert!(
1844            path.is_absolute(),
1845            "must be absolute even off a relative store root: {}",
1846            path.display()
1847        );
1848    }
1849
1850    #[tokio::test]
1851    async fn a_turn_past_the_configured_talk_timeout_is_reported_with_that_timeout() {
1852        // `[graph] timeout_talk` must be the number this module actually
1853        // waits, not a leftover hardcoded fifteen minutes - so the mock
1854        // sleeps past a deliberately tiny override and the failure note is
1855        // checked against that same override, not the old default.
1856        let (tmp, talks) = store();
1857        let slow = mock_agent(
1858            tmp.path(),
1859            "#!/bin/sh\ncat >/dev/null\nsleep 2\n",
1860            BTreeMap::new(),
1861        );
1862        let mut cfg = config(slow);
1863        cfg.graph.timeout_talk = 1;
1864        let mut talk = begin(&talks, &cfg, tmp.path().to_owned(), None).expect("begin");
1865
1866        let err = say(&mut talk, &talks, &cfg, "check the tests", Vec::new())
1867            .await
1868            .expect_err("a turn that never answers is an error");
1869        assert!(
1870            err.to_string().contains("did not answer within 1s"),
1871            "{err}"
1872        );
1873
1874        let on_disk = talks.get(&talk.id).expect("get");
1875        let note = on_disk.turns.last().expect("a note turn was recorded");
1876        assert!(
1877            note.body.contains("did not answer within 1s"),
1878            "the transcript must show the configured timeout: {}",
1879            note.body
1880        );
1881    }
1882
1883    #[test]
1884    fn closing_is_idempotent_and_a_closed_talk_takes_no_more_turns() {
1885        let (tmp, talks) = store();
1886        let spec = mock_agent(tmp.path(), REPLY, env("hi"));
1887        let cfg = config(spec);
1888        let mut talk = begin(&talks, &cfg, tmp.path().to_owned(), None).expect("begin");
1889
1890        close(&mut talk, &talks).expect("close");
1891        assert_eq!(talk.status, TalkStatus::Closed);
1892        close(&mut talk, &talks).expect("closing twice is not an error");
1893
1894        let err =
1895            record(&mut talk, &talks, "still there?", Vec::new()).expect_err("closed talks refuse");
1896        assert!(err.to_string().contains("closed"));
1897        let _ = &cfg; // config kept only to build the agent above
1898    }
1899
1900    #[tokio::test]
1901    async fn a_close_that_lands_while_a_turn_is_in_flight_is_not_undone_by_the_reply() {
1902        let (tmp, talks) = store();
1903        let spec = mock_agent(tmp.path(), REPLY, env("here you go"));
1904        let cfg = config(spec);
1905        // The in-flight turn's own handle: loaded once, the way a spawned
1906        // background task in `web::talk_say` holds one for the whole turn.
1907        let mut in_flight = begin(&talks, &cfg, tmp.path().to_owned(), None).expect("begin");
1908
1909        // The operator closes the conversation through a *different* handle
1910        // while the turn above is still running - exactly what a close typed
1911        // on the phone while an agent is mid-answer looks like.
1912        let mut closed_elsewhere = talks.get(&in_flight.id).expect("reread");
1913        close(&mut closed_elsewhere, &talks).expect("close");
1914        assert_eq!(
1915            talks.get(&in_flight.id).expect("reread").status,
1916            TalkStatus::Closed,
1917            "the close landed on disk before the turn finished"
1918        );
1919
1920        // The turn's own handle still says `open` - it was loaded before the
1921        // close - and finishing it must not resurrect the conversation the
1922        // operator already ended.
1923        assert_eq!(in_flight.status, TalkStatus::Open);
1924        respond(&mut in_flight, &talks, &cfg, "one more question")
1925            .await
1926            .expect("the turn itself still completes");
1927
1928        let on_disk = talks.get(&in_flight.id).expect("reread");
1929        assert_eq!(
1930            on_disk.status,
1931            TalkStatus::Closed,
1932            "a close must stick even when a turn that started before it finishes after it"
1933        );
1934        // The reply is not lost either: a turn already in flight when the
1935        // operator closed still gets its answer recorded.
1936        assert!(
1937            on_disk.turns.iter().any(|t| t.body == "here you go"),
1938            "the in-flight turn's own reply is still recorded: {:?}",
1939            on_disk.turns
1940        );
1941    }
1942
1943    #[test]
1944    fn a_close_that_lands_before_record_is_called_is_not_undone_by_it() {
1945        let (tmp, talks) = store();
1946        let spec = mock_agent(tmp.path(), REPLY, env("hi"));
1947        let cfg = config(spec);
1948        // The handle `web::talk_say` would have read before awaiting config
1949        // discovery, then carried across that await into `record`.
1950        let mut stale = begin(&talks, &cfg, tmp.path().to_owned(), None).expect("begin");
1951
1952        // The operator closes the conversation through a *different* handle
1953        // in the gap between that read and the call to `record` below.
1954        let mut closed_elsewhere = talks.get(&stale.id).expect("reread");
1955        close(&mut closed_elsewhere, &talks).expect("close");
1956        assert_eq!(
1957            talks.get(&stale.id).expect("reread").status,
1958            TalkStatus::Closed,
1959            "the close landed on disk before record was called"
1960        );
1961
1962        // The stale handle still says `open` - it was loaded before the
1963        // close - so a `record` that trusted it would append a turn and
1964        // write the conversation back open, undoing the close.
1965        assert_eq!(stale.status, TalkStatus::Open);
1966        let err = record(&mut stale, &talks, "still there?", Vec::new())
1967            .expect_err("a close that landed first must be honored, not overwritten");
1968        assert!(err.to_string().contains("closed"));
1969
1970        let on_disk = talks.get(&stale.id).expect("reread");
1971        assert_eq!(
1972            on_disk.status,
1973            TalkStatus::Closed,
1974            "record must not resurrect a conversation closed while its snapshot was stale"
1975        );
1976        assert!(
1977            on_disk.turns.is_empty(),
1978            "the rejected turn must not have been appended: {:?}",
1979            on_disk.turns
1980        );
1981        let _ = &cfg; // config kept only to build the agent above
1982    }
1983
1984    #[test]
1985    fn close_blocks_on_records_guard_rather_than_interleaving_with_it() {
1986        let (tmp, talks) = store();
1987        let spec = mock_agent(tmp.path(), REPLY, env("hi"));
1988        let cfg = config(spec);
1989        let talk = begin(&talks, &cfg, tmp.path().to_owned(), None).expect("begin");
1990
1991        // Hold the same guard `record`'s read-modify-write section holds for
1992        // the whole of its own read-then-write, standing in for `record`
1993        // being paused between its read and its `put`.
1994        let held = talks.guard();
1995
1996        let talks2 = talks.clone();
1997        let id = talk.id.clone();
1998        let closing = std::thread::spawn(move || {
1999            let mut talk = talks2.get(&id).expect("get");
2000            close(&mut talk, &talks2).expect("close");
2001        });
2002
2003        std::thread::sleep(Duration::from_millis(50));
2004        assert!(
2005            !closing.is_finished(),
2006            "close must wait for the guard, not read and write while it is held - \
2007             a re-read alone narrows this window without closing it"
2008        );
2009
2010        drop(held);
2011        closing.join().expect("close thread panicked");
2012
2013        assert_eq!(
2014            talks.get(&talk.id).expect("reread").status,
2015            TalkStatus::Closed,
2016            "once the guard is free, close still lands"
2017        );
2018        let _ = &cfg; // config kept only to build the agent above
2019    }
2020
2021    #[test]
2022    fn reopening_a_closed_talk_lets_it_take_turns_again_and_reopening_twice_is_not_an_error() {
2023        let (tmp, talks) = store();
2024        let spec = mock_agent(tmp.path(), REPLY, env("hi"));
2025        let cfg = config(spec);
2026        let mut talk = begin(&talks, &cfg, tmp.path().to_owned(), None).expect("begin");
2027
2028        close(&mut talk, &talks).expect("close");
2029        assert_eq!(talk.status, TalkStatus::Closed);
2030
2031        reopen(&mut talk, &talks).expect("reopen");
2032        assert_eq!(talk.status, TalkStatus::Open);
2033        assert_eq!(
2034            talks.get(&talk.id).expect("reread").status,
2035            TalkStatus::Open
2036        );
2037
2038        // Idempotent: reopening an already-open talk is not an error.
2039        reopen(&mut talk, &talks).expect("reopening an open talk is not an error");
2040        assert_eq!(talk.status, TalkStatus::Open);
2041
2042        record(&mut talk, &talks, "one more thing", Vec::new())
2043            .expect("a reopened talk takes turns again");
2044        let _ = &cfg; // config kept only to build the agent above
2045    }
2046
2047    #[test]
2048    fn removing_a_talk_deletes_its_record_and_artifacts_and_refuses_an_unknown_id() {
2049        let (tmp, talks) = store();
2050        let spec = mock_agent(tmp.path(), REPLY, env("hi"));
2051        let cfg = config(spec);
2052        let talk = begin(&talks, &cfg, tmp.path().to_owned(), None).expect("begin");
2053
2054        let artifacts = talks.artifacts_of(&talk.id);
2055        std::fs::create_dir_all(&artifacts).expect("create artifacts dir");
2056        std::fs::write(artifacts.join("turn-1.txt"), "hello").expect("write artifact");
2057
2058        talks.remove(&talk.id).expect("remove");
2059        assert!(!talks.path_of(&talk.id).is_file(), "the record is gone");
2060        assert!(!artifacts.is_dir(), "the artifacts directory is gone");
2061        assert!(
2062            talks.get(&talk.id).is_err(),
2063            "a removed talk cannot be read back"
2064        );
2065
2066        let err = talks
2067            .remove("nonexistent-id")
2068            .expect_err("unknown id refused");
2069        assert!(err.to_string().contains("no talk matches"), "{err}");
2070        let _ = &cfg; // config kept only to build the agent above
2071    }
2072
2073    #[tokio::test]
2074    async fn a_delete_that_lands_while_a_turn_is_in_flight_is_not_undone_by_the_reply() {
2075        let (tmp, talks) = store();
2076        let spec = mock_agent(tmp.path(), REPLY, env("here you go"));
2077        let cfg = config(spec);
2078        // The in-flight turn's own handle, loaded before the delete lands -
2079        // the same shape as the matching close test above.
2080        let mut in_flight = begin(&talks, &cfg, tmp.path().to_owned(), None).expect("begin");
2081
2082        talks.remove(&in_flight.id).expect("remove");
2083        assert!(
2084            talks.get(&in_flight.id).is_err(),
2085            "the delete landed on disk before the turn finished"
2086        );
2087
2088        // The turn's own handle has no way to know the record is gone -
2089        // finishing it must not write the file back into existence.
2090        respond(&mut in_flight, &talks, &cfg, "one more question")
2091            .await
2092            .expect("the turn itself still completes rather than erroring");
2093
2094        assert!(
2095            talks.get(&in_flight.id).is_err(),
2096            "a delete must stick even when a turn that started before it finishes after it"
2097        );
2098    }
2099
2100    #[test]
2101    fn a_delete_that_lands_before_record_is_called_is_not_undone_by_it() {
2102        let (tmp, talks) = store();
2103        let spec = mock_agent(tmp.path(), REPLY, env("hi"));
2104        let cfg = config(spec);
2105        // The handle `web::talk_say` would have read before awaiting config
2106        // discovery, then carried across that await into `record`.
2107        let mut stale = begin(&talks, &cfg, tmp.path().to_owned(), None).expect("begin");
2108
2109        talks.remove(&stale.id).expect("remove");
2110
2111        // The stale handle has no way to know the record is gone - a
2112        // `record` that trusted it would append a turn and write the
2113        // conversation back into existence.
2114        let err = record(&mut stale, &talks, "still there?", Vec::new())
2115            .expect_err("a delete that landed first must be honored, not overwritten");
2116        assert!(err.to_string().contains("deleted"), "{err}");
2117
2118        assert!(
2119            talks.get(&stale.id).is_err(),
2120            "record must not resurrect a conversation deleted while its snapshot was stale"
2121        );
2122        let _ = &cfg; // config kept only to build the agent above
2123    }
2124
2125    #[test]
2126    fn a_delete_that_lands_before_close_is_called_is_not_undone_by_it() {
2127        let (tmp, talks) = store();
2128        let spec = mock_agent(tmp.path(), REPLY, env("hi"));
2129        let cfg = config(spec);
2130        // `web::talk_close` loads `talk` and calls `close` right after - this
2131        // stands in for a delete landing in that gap.
2132        let mut stale = begin(&talks, &cfg, tmp.path().to_owned(), None).expect("begin");
2133
2134        talks.remove(&stale.id).expect("remove");
2135
2136        // The stale handle has no way to know the record is gone - a `close`
2137        // that fell back to it would write the conversation back into
2138        // existence, closed.
2139        let err = close(&mut stale, &talks)
2140            .expect_err("a delete that landed first must be honored, not overwritten");
2141        assert!(err.to_string().contains("deleted"), "{err}");
2142
2143        assert!(
2144            talks.get(&stale.id).is_err(),
2145            "close must not resurrect a conversation deleted while its snapshot was stale"
2146        );
2147        let _ = &cfg; // config kept only to build the agent above
2148    }
2149
2150    #[test]
2151    fn a_delete_that_lands_before_reopen_is_called_is_not_undone_by_it() {
2152        let (tmp, talks) = store();
2153        let spec = mock_agent(tmp.path(), REPLY, env("hi"));
2154        let cfg = config(spec);
2155        // `web::talk_reopen` loads `talk` and calls `reopen` right after -
2156        // this stands in for a delete landing in that gap.
2157        let mut stale = begin(&talks, &cfg, tmp.path().to_owned(), None).expect("begin");
2158        close(&mut stale, &talks).expect("close");
2159
2160        talks.remove(&stale.id).expect("remove");
2161
2162        // The stale handle has no way to know the record is gone - a
2163        // `reopen` that fell back to it would write the conversation back
2164        // into existence, open.
2165        let err = reopen(&mut stale, &talks)
2166            .expect_err("a delete that landed first must be honored, not overwritten");
2167        assert!(err.to_string().contains("deleted"), "{err}");
2168
2169        assert!(
2170            talks.get(&stale.id).is_err(),
2171            "reopen must not resurrect a conversation deleted while its snapshot was stale"
2172        );
2173        let _ = &cfg; // config kept only to build the agent above
2174    }
2175
2176    #[test]
2177    fn list_puts_open_talks_before_closed_ones() {
2178        let (tmp, talks) = store();
2179        let make = |id: &str, status: TalkStatus| {
2180            let mut t = Talk {
2181                schema: SCHEMA,
2182                id: id.to_owned(),
2183                repo: tmp.path().to_owned(),
2184                agent: "mock".to_owned(),
2185                status,
2186                turns: Vec::new(),
2187                pending: String::new(),
2188                pending_attachments: Vec::new(),
2189                created_at: Timestamp::now(),
2190                updated_at: Timestamp::now(),
2191                seat: SeatState::new(SEAT, "mock", 7),
2192            };
2193            talks.put(&mut t).expect("put");
2194        };
2195        make("20260901-000000-0001", TalkStatus::Open);
2196        make("20260902-000000-0002", TalkStatus::Open);
2197        make("20260903-000000-0003", TalkStatus::Closed);
2198
2199        let ids: Vec<String> = talks.list().into_iter().map(|t| t.id).collect();
2200        assert_eq!(
2201            ids,
2202            [
2203                "20260902-000000-0002",
2204                "20260901-000000-0001",
2205                "20260903-000000-0003"
2206            ]
2207        );
2208        assert_eq!(talks.count_open(), 2);
2209    }
2210
2211    #[test]
2212    fn tasks_of_finds_only_this_talks_own_tasks() {
2213        let dir = tempfile::tempdir().expect("tempdir");
2214        let queue = Queue::at(dir.path().join("queue"));
2215
2216        let mut mine = Task::new(
2217            "rework the loader".to_owned(),
2218            "rework the loader".to_owned(),
2219            PathBuf::from("/repo"),
2220            Source::Agent {
2221                run: "20260904-014455-ab12".to_owned(),
2222                node: "chat".to_owned(),
2223            },
2224        );
2225        queue.put(&mut mine).expect("put mine");
2226
2227        let mut theirs = Task::new(
2228            "unrelated".to_owned(),
2229            "unrelated".to_owned(),
2230            PathBuf::from("/repo"),
2231            Source::Agent {
2232                run: "20260904-090000-zz99".to_owned(),
2233                node: "implement".to_owned(),
2234            },
2235        );
2236        queue.put(&mut theirs).expect("put theirs");
2237
2238        let mut human = Task::new(
2239            "typed by hand".to_owned(),
2240            "typed by hand".to_owned(),
2241            PathBuf::from("/repo"),
2242            Source::Human,
2243        );
2244        queue.put(&mut human).expect("put human");
2245
2246        let found = tasks_of(&queue, "20260904-014455-ab12");
2247        assert_eq!(found.len(), 1);
2248        assert_eq!(found[0].id, mine.id);
2249    }
2250
2251    #[test]
2252    fn the_briefing_names_solo_task_add() {
2253        let brief = briefing(Path::new("/repo"), "en", false);
2254        assert!(brief.contains("magi task add --solo"));
2255        assert!(brief.contains("/repo"));
2256        assert!(!brief.contains("Hold this conversation in"));
2257    }
2258
2259    /// Talk fixes `repo` at the directory the conversation was opened in, so
2260    /// an agent asked to change some other checkout has no path to it unless
2261    /// the briefing itself says `--repo` can take a short name - see
2262    /// `resolve_repo_by_name` in `src/main.rs`, which is what actually
2263    /// resolves it.
2264    #[test]
2265    fn the_briefing_explains_targeting_a_different_repository_by_name() {
2266        let brief = briefing(Path::new("/repo"), "en", false);
2267        assert!(brief.contains("--repo does not have to be a full path"));
2268        assert!(brief.contains("owner/repo"));
2269        assert!(brief.contains("magi repos"));
2270        assert!(brief.contains("ask the operator"));
2271    }
2272
2273    #[test]
2274    fn the_briefing_tells_the_assistant_to_pass_images_with_attach() {
2275        let brief = briefing(Path::new("/repo"), "en", false);
2276        assert!(brief.contains("--attach <path>"), "{brief}");
2277        assert!(brief.contains("deleting this conversation"), "{brief}");
2278    }
2279
2280    #[test]
2281    fn the_briefing_names_the_language_when_it_is_not_english() {
2282        let brief = briefing(Path::new("/repo"), "Japanese", false);
2283        assert!(brief.contains("Hold this conversation in Japanese"));
2284    }
2285
2286    #[test]
2287    fn the_briefing_forbids_writes_unless_the_repository_opted_in() {
2288        let read_only = briefing(Path::new("/repo"), "en", false);
2289        assert!(read_only.contains("Do not write files"));
2290        assert!(!read_only.contains("allow_write"));
2291
2292        let writable = briefing(Path::new("/repo"), "en", true);
2293        assert!(!writable.contains("Do not write files"));
2294        assert!(writable.contains("allow_write = true"));
2295        // Still names the queue for anything past a small named edit, and
2296        // still tells the agent to report what it changed.
2297        assert!(writable.contains("magi task add --solo"));
2298        assert!(writable.contains("say plainly what you"));
2299    }
2300}