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//! With `[talk] allow_write` on, a codex turn also runs unsandboxed
33//! ([`turn_access`]), as a claude turn already does under `bypassPermissions`:
34//! otherwise `magi task add` (writes to magi's queue under the home directory)
35//! or `git fetch` is refused by the sandbox. Unattended seats never get this.
36//!
37//! `--solo` rather than a plain `magi task add` is the point of pairing this
38//! module with [`crate::queue::Task::solo`]. A task that came out of a
39//! conversation the operator just had is a decision already made, not a
40//! design question worth three independent takes - so it runs through one
41//! implementer and straight into review, the way [`crate::graph::Runner`]
42//! already degrades a single-candidate run.
43//!
44//! # Shape
45//!
46//! The same split [`crate::queue`] uses: [`Talk`] is data plus pure helpers,
47//! [`Talks`] owns the I/O and is constructed with its root, so every test
48//! here drives a real store in a temp directory rather than the operator's
49//! own home.
50
51use std::path::{Path, PathBuf};
52use std::sync::{Arc, Mutex, MutexGuard, PoisonError};
53use std::time::Duration;
54
55use anyhow::{Context, Result, bail};
56use jiff::Timestamp;
57use serde::{Deserialize, Serialize};
58
59use crate::agent::{self, Invocation, SeatState};
60use crate::config::{AgentSpec, Config};
61use crate::queue::{Queue, Source, Task};
62
63/// On-disk format for a conversation. Bumped when a field's meaning changes.
64pub const SCHEMA: u32 = 1;
65
66/// Wall-clock limit for one agent turn. See [`crate::config::Graph::timeout_talk`].
67///
68/// An hour by default. This turn is expected to run several shell commands
69/// and read their output before answering one - "what does this function
70/// do", "is this still true", "run the tests and tell me" - which argues for
71/// an hour rather than the five minutes a short budget once assumed, because
72/// the thing that made a short budget matter - an operator watching a
73/// spinner - is not how this conversation gets used: the operator moves on
74/// to something else while a turn runs and checks back later, so a long turn
75/// spends a held seat, not anyone's attention.
76fn turn_timeout(cfg: &Config) -> Duration {
77    Duration::from_secs(cfg.graph.timeout_talk)
78}
79
80/// Seat name for the conversation's agent, scoping its CLI-side session away
81/// from every other seat magi ever opens.
82const SEAT: &str = "talk";
83
84/// Prefix on a turn magi wrote rather than an agent.
85const MAGI_NOTE: &str = "magi: ";
86
87/// Who said something.
88#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
89#[serde(rename_all = "lowercase")]
90pub enum Who {
91    /// The operator.
92    Operator,
93    /// The conversation's agent - or magi itself, reporting that a turn
94    /// failed. See [`MAGI_NOTE`].
95    Agent,
96}
97
98/// One image the operator attached to a turn.
99///
100/// Never carries the bytes themselves: the picture lives on disk under
101/// [`Talks::attachments_dir`], named by `id` alone. `name` is the filename
102/// the operator's browser reported, kept only for display - it never
103/// contributes to a path, which is what keeps an upload from being able to
104/// traverse outside its own directory.
105#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
106#[serde(deny_unknown_fields)]
107pub struct Attachment {
108    /// Server-minted id; also the file's stem under `attachments_dir`.
109    pub id: String,
110    /// The operator's own filename, for display only.
111    pub name: String,
112    /// Validated by `web` at upload time against a closed whitelist:
113    /// `image/png`, `image/jpeg`, `image/gif`, `image/webp`.
114    pub mime: String,
115    /// Size in bytes, so the phone can show it without a second request.
116    pub bytes: u64,
117}
118
119/// One message in the conversation.
120#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
121#[serde(deny_unknown_fields)]
122pub struct Turn {
123    /// Who wrote it.
124    pub who: Who,
125    /// What they said.
126    pub body: String,
127    /// When it was said.
128    pub at: Timestamp,
129    /// Images attached to this turn. `#[serde(default)]` so a conversation
130    /// recorded before attachments existed still reads.
131    #[serde(default)]
132    pub attachments: Vec<Attachment>,
133    /// What the agent's CLI reported reading for this reply; only ever set on
134    /// an agent reply that carried usage. `#[serde(default)]` so a
135    /// conversation recorded before this field existed still reads, which is
136    /// why [`SCHEMA`] stays put: no existing field changed meaning.
137    #[serde(default, skip_serializing_if = "Option::is_none")]
138    pub usage: Option<TurnUsage>,
139    /// Byte offsets in [`Self::body`] where the second and later messages of
140    /// an operator turn begin. Replies queued while a turn runs are joined
141    /// with a blank line, which an owner's own blank lines cannot be told
142    /// apart from, so the boundary is recorded when it is made. `Some(empty)`
143    /// is one message; `None` is unknown (a turn stored before this field, or
144    /// not an operator turn) and readers fall back to paragraphs.
145    #[serde(default, skip_serializing_if = "Option::is_none")]
146    pub breaks: Option<Vec<usize>>,
147}
148
149/// Raw usage of one agent reply, stored as the CLI reported it.
150///
151/// Counts only, never a percentage: the window is configuration
152/// ([`Config::context_window`]) and the conversation's model can change, so a
153/// stored percentage would go stale the moment either did. `agent` and
154/// `model` record who read that many tokens, which is how [`context_usage`]
155/// notices the figure predates a switch.
156#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
157pub struct TurnUsage {
158    /// Input-side tokens the CLI reported - see `agent::context_tokens`.
159    pub context_tokens: u64,
160    /// Roster id that answered.
161    pub agent: String,
162    /// That agent's model at the time (`None`: the CLI's own default).
163    #[serde(default)]
164    pub model: Option<String>,
165}
166
167/// How full the conversation's context window is, as the phone shows it.
168///
169/// Derived at read time and never persisted. `None` means unknown, which the
170/// UI says in words - it is never a made-up 0.
171#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize)]
172pub struct ContextUsage {
173    /// Tokens the last agent reply's turn read, if its CLI reported any.
174    pub tokens: Option<u64>,
175    /// Window of the conversation's *current* model, if one is configured.
176    pub window: Option<u64>,
177    /// `tokens / window`, rounded down, not capped (over 100 is real news).
178    pub percent: Option<u64>,
179    /// 80% or more, judged before rounding: the conversation is getting long.
180    pub warn: bool,
181    /// The conversation's agent or model is not the one that produced
182    /// `tokens`: the figure describes the old session and the next turn
183    /// re-measures it.
184    pub since_switch: bool,
185    /// The current model, if the roster names one.
186    pub model: Option<String>,
187    /// `tokens` is a transcript-length estimate, not a CLI measurement (see
188    /// [`estimate_context_tokens`]). A measured figure is never overridden.
189    #[serde(default)]
190    pub estimated: bool,
191}
192
193/// Fraction (in percent) of the window at which [`ContextUsage::warn`] fires.
194const CONTEXT_WARN_PERCENT: u64 = 80;
195
196/// Allowance for the standing prompt when no config is readable to render
197/// the real [`briefing`].
198const STANDING_PROMPT_FALLBACK_CHARS: u64 = 7000;
199
200/// Rough estimate of the context a conversation occupies, for CLIs whose usage
201/// is cumulative and so is never measured (claude / codex / agy).
202///
203/// Counts the characters (not bytes) of every operator and agent turn, skips
204/// magi's own notes (never sent to the agent), adds `standing_chars` for the
205/// standing prompt, and divides by ~3.5 chars per token, rounding up. `None`
206/// when no turn counts: an empty conversation stays unknown.
207///
208/// Known biases: CJK runs near one token per character, and tool output,
209/// images and the CLI's own system prompt are not counted, so this tends to
210/// under-estimate (the 80% warning comes late); a compacted session is still
211/// counted in full, which over-estimates.
212pub fn estimate_context_tokens(talk: &Talk, standing_chars: u64) -> Option<u64> {
213    let mut counted = false;
214    let mut chars = standing_chars;
215    for t in talk.turns.iter().filter(|t| !t.body.starts_with(MAGI_NOTE)) {
216        counted = true;
217        chars += t.body.chars().count() as u64;
218    }
219    // chars / 3.5, rounded up, in integers.
220    counted.then(|| (chars * 2).div_ceil(7))
221}
222
223/// Context usage of `talk`, measured against its current model.
224///
225/// Only the latest agent reply counts (magi's own notes are skipped) and a
226/// reply without usage makes the answer unknown - older replies are never
227/// consulted, since a stale count passed off as current is worse than "unknown".
228/// The window comes from the *current* agent's model, so switching model moves
229/// the denominator at once.
230///
231/// Switching agent (or model, which is an agent change) mints a fresh CLI
232/// session, so the next turn re-sends the whole transcript and the count
233/// resets or jumps. Until that turn lands, the old figure is reported with
234/// `since_switch` set. Deterministic: same talk and config, same answer.
235pub fn context_usage(talk: &Talk, cfg: Option<&Config>) -> ContextUsage {
236    let current = cfg.and_then(|c| c.agents.iter().find(|a| a.id == talk.agent));
237    let model = current.and_then(|a| a.model.clone());
238    let window = cfg
239        .zip(model.as_deref())
240        .and_then(|(c, m)| c.context_window(m))
241        .filter(|w| *w > 0);
242    let usage = talk
243        .turns
244        .iter()
245        .rev()
246        .find(|t| t.who == Who::Agent && !t.body.starts_with(MAGI_NOTE))
247        .and_then(|t| t.usage.as_ref());
248    let measured = usage.map(|u| u.context_tokens);
249    let tokens = measured.or_else(|| {
250        let standing = cfg.map_or(STANDING_PROMPT_FALLBACK_CHARS, |c| {
251            briefing_for(
252                &talk.repo,
253                &c.graph.language,
254                c.talk.allow_write,
255                crate::persona::active(&c.talk.personas, &talk.persona).as_ref(),
256                c.talk.operator_name(),
257                talk.implementers,
258            )
259            .chars()
260            .count() as u64
261        });
262        estimate_context_tokens(talk, standing)
263    });
264    let estimated = measured.is_none() && tokens.is_some();
265    let since_switch =
266        usage.is_some_and(|u| u.agent != talk.agent || (current.is_some() && u.model != model));
267    let (percent, warn) = match (tokens, window) {
268        (Some(t), Some(w)) => (
269            Some(t.saturating_mul(100) / w),
270            t.saturating_mul(100) >= w.saturating_mul(CONTEXT_WARN_PERCENT),
271        ),
272        _ => (None, false),
273    };
274    ContextUsage {
275        tokens,
276        window,
277        percent,
278        warn,
279        since_switch,
280        model,
281        estimated,
282    }
283}
284
285/// Where a conversation is in its life: this conversation can file any
286/// number of tasks without ending, so it only ever moves once, from open to
287/// closed.
288#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
289#[serde(rename_all = "lowercase")]
290pub enum TalkStatus {
291    /// Still open; the operator may say more, and may have already filed work
292    /// out of it.
293    Open,
294    /// Closed by hand. Kept on disk as a record.
295    Closed,
296}
297
298impl TalkStatus {
299    /// Is this conversation still live?
300    pub fn open(self) -> bool {
301        matches!(self, Self::Open)
302    }
303
304    /// Wire form, for the phone and for logs.
305    pub fn as_str(self) -> &'static str {
306        match self {
307            Self::Open => "open",
308            Self::Closed => "closed",
309        }
310    }
311}
312
313/// One standing conversation.
314#[derive(Debug, Clone, Serialize, Deserialize)]
315#[serde(deny_unknown_fields)]
316pub struct Talk {
317    /// On-disk format version.
318    pub schema: u32,
319    /// Conversation id, e.g. `20260904-014455-ab12`.
320    pub id: String,
321    /// Repository this conversation is about.
322    pub repo: PathBuf,
323    /// Roster agent id holding the conversation.
324    pub agent: String,
325    /// Current state.
326    pub status: TalkStatus,
327    /// Everything said, oldest first.
328    pub turns: Vec<Turn>,
329    /// Text and attachments accepted while the single CLI turn is busy.
330    /// They are durable, but become a real turn only when [`drain`] records them.
331    #[serde(default)]
332    pub pending: String,
333    /// Attachments paired with [`Self::pending`].
334    #[serde(default)]
335    pub pending_attachments: Vec<Attachment>,
336    /// Message boundaries inside [`Self::pending`], as [`Turn::breaks`].
337    #[serde(default, skip_serializing_if = "Option::is_none")]
338    pub pending_breaks: Option<Vec<usize>>,
339    /// May a failed turn fall back through the rest of `[roles] chatter`?
340    /// True only while the agent was chosen by that chain; an explicit
341    /// `--agent` or an operator's switch pins the conversation to its agent
342    /// even when that agent also appears in the chain.
343    #[serde(default)]
344    pub fallback: bool,
345    /// Persona id (see [`crate::persona`]); empty is the plain default voice.
346    /// Independent of [`Self::agent`]: it survives an agent switch.
347    #[serde(default)]
348    pub persona: String,
349    /// The persona changed after the CLI session was last told about it. The
350    /// next resumed turn carries a persona update; cleared only once a turn
351    /// has succeeded (a fresh seat's briefing already holds the new persona).
352    #[serde(default)]
353    pub persona_dirty: bool,
354    /// How many implementers the tasks this chat files use (1..=3; 1 is
355    /// `--solo`). Only the briefing's wording changes; nothing enforces it.
356    #[serde(default = "solo_implementers")]
357    pub implementers: u8,
358    /// [`Self::implementers`] changed after the CLI session was last told;
359    /// cleared only once a turn has succeeded, like [`Self::persona_dirty`].
360    #[serde(default)]
361    pub implementers_dirty: bool,
362    /// When the conversation was opened.
363    pub created_at: Timestamp,
364    /// Last change to this file.
365    pub updated_at: Timestamp,
366    /// The CLI-side conversation, so a turn after the first costs one
367    /// sentence instead of the whole transcript. Not `pub`: it is magi's
368    /// bookkeeping, and a caller that edited it would detach the record from
369    /// the conversation the model actually holds.
370    seat: SeatState,
371}
372
373impl Talk {
374    /// Short form used in lists and notifications, matching a run's short id.
375    pub fn short(&self) -> &str {
376        short(&self.id)
377    }
378}
379
380/// A conversation store on disk.
381#[derive(Debug, Clone)]
382pub struct Talks {
383    root: PathBuf,
384    /// Serializes the read-modify-write cycle that reads a talk, decides
385    /// something from its `status`, and writes the whole record back.
386    /// [`close`], [`record`] and the tail of [`turn`] all take this before
387    /// that cycle rather than after just the read: a re-read narrows the
388    /// window another writer can land in, but does not close it, since
389    /// nothing stopped that other writer's own put from landing between this
390    /// call's re-read and its own put. Shared across every clone, since every
391    /// clone is a handle onto the same files.
392    lock: Arc<Mutex<()>>,
393}
394
395impl Talks {
396    /// The operator's conversations, `<home>/talks`.
397    pub fn open() -> Self {
398        Self::at(crate::run::home().join("talks"))
399    }
400
401    /// A store at an explicit root. Tests use this, which is why none of them
402    /// need the operator's real home.
403    pub fn at(root: PathBuf) -> Self {
404        Self {
405            root,
406            lock: Arc::new(Mutex::new(())),
407        }
408    }
409
410    /// Claim the right to read-modify-write a talk's `status`. A plain
411    /// `std::sync::Mutex`, not an async one: every caller holds it across a
412    /// handful of small file operations and never across an `.await`, so
413    /// blocking the thread briefly is the right tool, not a reason to reach
414    /// for `tokio::sync::Mutex`. Poisoning recovers rather than propagates -
415    /// one panicking caller must not wedge every talk in the store the way it
416    /// would wedge the loop's own lock; see [`crate::web`]'s `lock_or_recover`,
417    /// which this mirrors.
418    ///
419    /// The mutex only serializes this process. The cycle is also taken under a
420    /// short file lock beside the records, so the CLI (`magi answer
421    /// --ask-chat`) and the web server cannot overwrite each other's draft.
422    /// The file lock is never skipped: a cycle that cannot get it within
423    /// longer than the lock's own expiry fails instead of writing unguarded.
424    fn guard(&self) -> Result<StoreGuard<'_>> {
425        let mutex = self.lock.lock().unwrap_or_else(PoisonError::into_inner);
426        std::fs::create_dir_all(&self.root)
427            .with_context(|| format!("create {}", self.root.display()))?;
428        let path = self.root.join(".store.turn");
429        let deadline = std::time::Instant::now() + TAKEOVER_LOCK_TTL * 2;
430        loop {
431            if let Some(file) = TurnLock::take(&path)? {
432                return Ok(StoreGuard {
433                    _file: file,
434                    _mutex: mutex,
435                });
436            }
437            if std::time::Instant::now() >= deadline {
438                bail!("the talk store is locked by another process");
439            }
440            std::thread::sleep(Duration::from_millis(10));
441        }
442    }
443
444    /// Directory holding the conversation files.
445    pub fn root(&self) -> &Path {
446        &self.root
447    }
448
449    /// Path for one conversation id.
450    pub fn path_of(&self, id: &str) -> PathBuf {
451        self.root.join(format!("{id}.json"))
452    }
453
454    /// Where one conversation's prompts and CLI output are kept, beside the
455    /// record rather than inside it.
456    pub fn artifacts_of(&self, id: &str) -> PathBuf {
457        self.root.join(format!("{id}.artifacts"))
458    }
459
460    /// Where this conversation's attached images live: a subdirectory of
461    /// `artifacts_of`, so deleting the conversation deletes its attachments
462    /// too and nothing here needs its own cleanup path.
463    pub fn attachments_dir(&self, id: &str) -> PathBuf {
464        self.artifacts_of(id).join("attachments")
465    }
466
467    /// Persist one already-validated attachment and return its metadata.
468    ///
469    /// `web::talk_attachment_post` is the only caller: it has already
470    /// checked `mime` against the whitelist and sniffed the bytes, so an
471    /// unrecognised mime reaching here is a bug in that caller, not
472    /// something an operator did. The id is minted here and never taken
473    /// from the client; `name` is stored for display only and never used to
474    /// build a path.
475    pub fn put_attachment(
476        &self,
477        id: &str,
478        mime: &str,
479        name: &str,
480        data: &[u8],
481    ) -> Result<Attachment> {
482        let dir = self.attachments_dir(id);
483        std::fs::create_dir_all(&dir).with_context(|| format!("create {}", dir.display()))?;
484        let ext = attachment_ext(mime).with_context(|| format!("unsupported mime `{mime}`"))?;
485        let att = Attachment {
486            id: new_attachment_id(),
487            name: name.to_owned(),
488            mime: mime.to_owned(),
489            bytes: data.len() as u64,
490        };
491        std::fs::write(dir.join(format!("{}.{ext}", att.id)), data)
492            .with_context(|| format!("write attachment {}", att.id))?;
493        std::fs::write(
494            dir.join(format!("{}.json", att.id)),
495            serde_json::to_string(&att).context("serialize attachment")?,
496        )
497        .with_context(|| format!("write attachment metadata {}", att.id))?;
498        Ok(att)
499    }
500
501    /// Just the metadata, without reading the image bytes back off disk -
502    /// what `web::talk_say` uses to turn an id the operator referenced into
503    /// an [`Attachment`] before appending a [`Turn`], where the bytes
504    /// themselves are of no interest. `None` for an id this conversation
505    /// never stored - including one that merely looks plausible:
506    /// [`valid_attachment_id`] is checked here too, not only by the caller,
507    /// the same defence-in-depth `Questions::panel_asset` uses for its own
508    /// asset ids.
509    pub fn attachment_meta(&self, id: &str, att_id: &str) -> Result<Option<Attachment>> {
510        if !valid_attachment_id(att_id) {
511            return Ok(None);
512        }
513        let meta_path = self.attachments_dir(id).join(format!("{att_id}.json"));
514        if !meta_path.is_file() {
515            return Ok(None);
516        }
517        let att = serde_json::from_str(
518            &std::fs::read_to_string(&meta_path)
519                .with_context(|| format!("read {}", meta_path.display()))?,
520        )
521        .with_context(|| format!("parse {}", meta_path.display()))?;
522        Ok(Some(att))
523    }
524
525    /// A stored attachment's metadata and its bytes together, for serving it
526    /// back on `GET`. `None` under the same conditions as
527    /// [`Talks::attachment_meta`], which this is built on.
528    pub fn read_attachment(&self, id: &str, att_id: &str) -> Result<Option<(Attachment, Vec<u8>)>> {
529        let Some(att) = self.attachment_meta(id, att_id)? else {
530            return Ok(None);
531        };
532        let ext = attachment_ext(&att.mime).with_context(|| {
533            format!("attachment {att_id} has an unsupported mime `{}`", att.mime)
534        })?;
535        let data_path = self.attachments_dir(id).join(format!("{att_id}.{ext}"));
536        let data =
537            std::fs::read(&data_path).with_context(|| format!("read {}", data_path.display()))?;
538        Ok(Some((att, data)))
539    }
540
541    /// Absolute path of one attachment's bytes, for the prompt note [`turn`]
542    /// appends and for [`Invocation::attachments`]. `None` only for a mime
543    /// [`put_attachment`] could never have written, which means the
544    /// attachment did not come from this store.
545    ///
546    /// `self.root` (and so `attachments_dir`) is not guaranteed absolute on
547    /// its own - `run::home()` returns a bare relative `PathBuf` verbatim
548    /// when the operator sets `MAGI_HOME` to a relative path, and nothing
549    /// canonicalizes it on the way in. That is harmless for every other use
550    /// of this store, since its own I/O runs in this process against this
551    /// process's cwd - but this path is handed to a CLI invoked with `cwd:
552    /// &talk.repo`, a different directory, so a relative path here would
553    /// resolve against the wrong place once it reached the prompt.
554    /// `std::path::absolute` fixes it against *this* process's cwd before
555    /// that happens; see `disk::free_bytes_by_os` for the same function used
556    /// the same way elsewhere in this codebase.
557    fn attachment_path(&self, id: &str, att: &Attachment) -> Option<PathBuf> {
558        let ext = attachment_ext(&att.mime)?;
559        let path = self.attachments_dir(id).join(format!("{}.{ext}", att.id));
560        std::path::absolute(&path).ok()
561    }
562
563    /// Write a conversation, atomically, so a process killed mid-write leaves
564    /// the previous state readable rather than a truncated file.
565    ///
566    /// The write-then-rename itself is retried a handful of times - see
567    /// [`write_atomic`] - because a reader with the destination file briefly
568    /// open is exactly the kind of failure that must not cost an agent's
569    /// whole reply; see `turn`'s own tail for what happens when even that is
570    /// not enough.
571    pub fn put(&self, t: &mut Talk) -> Result<()> {
572        std::fs::create_dir_all(&self.root)
573            .with_context(|| format!("create {}", self.root.display()))?;
574        t.updated_at = Timestamp::now();
575        let body = serde_json::to_string_pretty(t).context("serialize talk")?;
576        let path = self.path_of(&t.id);
577        let tmp = path.with_extension("json.tmp");
578        write_atomic(&tmp, &path, &body)
579    }
580
581    /// Load a conversation by id or unambiguous id prefix.
582    pub fn get(&self, id: &str) -> Result<Talk> {
583        let resolved = self.resolve_id(id)?;
584        read_path(&self.path_of(&resolved))
585    }
586
587    /// Every conversation on disk: open first, then newest first, so what the
588    /// operator is still using belongs above what they are done with.
589    pub fn list(&self) -> Vec<Talk> {
590        self.list_counting_unreadable().0
591    }
592
593    /// [`Talks::list`], plus how many `*.json` files could not be read, so a
594    /// caller that reports to the operator can say what it skipped.
595    pub fn list_counting_unreadable(&self) -> (Vec<Talk>, usize) {
596        let mut unreadable = 0;
597        let mut all: Vec<Talk> = std::fs::read_dir(&self.root)
598            .into_iter()
599            .flatten()
600            .flatten()
601            .map(|e| e.path())
602            .filter(|p| p.extension().is_some_and(|x| x == "json"))
603            .filter_map(|p| {
604                let talk = read_path(&p).ok();
605                if talk.is_none() {
606                    unreadable += 1;
607                }
608                talk
609            })
610            .collect();
611        all.sort_unstable_by(|a, b| {
612            let rank = |t: &Talk| u8::from(!t.status.open());
613            rank(a).cmp(&rank(b)).then_with(|| b.id.cmp(&a.id))
614        });
615        (all, unreadable)
616    }
617
618    /// Expand an id prefix to exactly one conversation id.
619    pub fn resolve_id(&self, prefix: &str) -> Result<String> {
620        if self.path_of(prefix).is_file() {
621            return Ok(prefix.to_owned());
622        }
623        let hits: Vec<String> = self
624            .list()
625            .into_iter()
626            .map(|t| t.id)
627            .filter(|id| id.starts_with(prefix) || id.ends_with(prefix))
628            .collect();
629        match hits.len() {
630            1 => Ok(hits.into_iter().next().expect("exactly one hit")),
631            0 => bail!("no talk matches `{prefix}`"),
632            _ => bail!(
633                "`{prefix}` matches {} talks: {}",
634                hits.len(),
635                hits.join(", ")
636            ),
637        }
638    }
639
640    /// Change detection token: the newest modification time in the store, in
641    /// milliseconds.
642    pub fn revision(&self) -> u64 {
643        std::fs::read_dir(&self.root)
644            .into_iter()
645            .flatten()
646            .flatten()
647            .filter(|e| e.path().extension().is_none_or(|x| x != "turn"))
648            .filter(|e| !e.file_name().to_string_lossy().starts_with('.'))
649            .filter_map(|e| e.metadata().ok())
650            .filter_map(|m| m.modified().ok())
651            .filter_map(|t| t.duration_since(std::time::UNIX_EPOCH).ok())
652            .map(|d| d.as_millis() as u64)
653            .max()
654            .unwrap_or(0)
655    }
656
657    /// How many conversations are still open.
658    pub fn count_open(&self) -> usize {
659        self.list().iter().filter(|t| t.status.open()).count()
660    }
661
662    /// Where one conversation's turn lease lives. Not a `*.json`, so
663    /// [`Talks::list`] never sees it.
664    pub fn turn_path(&self, id: &str) -> PathBuf {
665        self.root.join(format!("{id}.turn"))
666    }
667
668    /// Claim the right to run one agent turn in `id`, across processes.
669    ///
670    /// `None` means somebody else holds a fresh lease - the caller reports
671    /// "a turn is already running" and must not start one. A lease is held
672    /// while its last beat is within [`crate::ask::LEASE_TTL`]; pids are never
673    /// consulted. A stale (or unreadable) lease is taken over: renamed to a
674    /// unique name first, so of several takers only the one whose rename wins
675    /// deletes it, and the claim itself is an exclusive create.
676    pub fn claim_turn(&self, id: &str) -> Result<Option<TurnLease>> {
677        self.claim_turn_at(id, Timestamp::now())
678    }
679
680    fn claim_turn_at(&self, id: &str, now: Timestamp) -> Result<Option<TurnLease>> {
681        std::fs::create_dir_all(&self.root)
682            .with_context(|| format!("create {}", self.root.display()))?;
683        let path = self.turn_path(id);
684        let token = fresh_token();
685        if create_turn(&path, &token, now)? {
686            return Ok(Some(TurnLease {
687                talk: id.to_owned(),
688                path,
689                token,
690            }));
691        }
692        if lease_blocks(&path, now) {
693            return Ok(None);
694        }
695        // Stale or unreadable. Every replacement happens under a short-lived
696        // exclusive lock file, so two takers cannot each delete the other's
697        // fresh lease. A taker that finds the lock held simply loses; the lock
698        // itself ages out, so a taker that died inside it cannot wedge the talk.
699        let Some(_lock) = TurnLock::take(&path)? else {
700            return Ok(None);
701        };
702        if lease_blocks(&path, now) {
703            return Ok(None);
704        }
705        let _ = std::fs::remove_file(&path);
706        Ok(create_turn(&path, &token, now)?.then_some(TurnLease {
707            talk: id.to_owned(),
708            path,
709            token,
710        }))
711    }
712
713    /// Is a turn running in `id` anywhere, by a fresh lease?
714    pub fn turn_held(&self, id: &str) -> bool {
715        read_turn(&self.turn_path(id)).is_some_and(|r| r.fresh(Timestamp::now()))
716    }
717
718    /// Remove a conversation from disk, record and artifacts both. The
719    /// operator's way of saying "not just done, gone" - [`close`] alone
720    /// leaves the record as history.
721    ///
722    /// Takes [`Talks::guard`] for the same reason [`close`] does: a delete
723    /// racing a [`record`] or the tail of [`turn`] must not land between
724    /// their own read and write, or the file removed here would look, to
725    /// them, like a record that simply has not been written yet. The other
726    /// half of that story is on their side - both check under this same
727    /// guard that the record they are about to write is still there, and
728    /// give up without writing if it is not, which is what stops their `put`
729    /// from resurrecting a conversation this call already removed.
730    pub fn remove(&self, id: &str) -> Result<()> {
731        let _guard = self.guard()?;
732        let resolved = self.resolve_id(id)?;
733        let path = self.path_of(&resolved);
734        std::fs::remove_file(&path).with_context(|| format!("remove {}", path.display()))?;
735        let artifacts = self.artifacts_of(&resolved);
736        if artifacts.is_dir() {
737            std::fs::remove_dir_all(&artifacts)
738                .with_context(|| format!("remove {}", artifacts.display()))?;
739        }
740        let _ = std::fs::remove_file(self.turn_path(&resolved));
741        Ok(())
742    }
743}
744
745/// What [`Talks::guard`] hands out: the in-process mutex plus the
746/// cross-process file lock. The file lock is released first.
747struct StoreGuard<'a> {
748    _file: TurnLock,
749    _mutex: MutexGuard<'a, ()>,
750}
751
752/// The body of a `<id>.turn` file. `pid` is for a human reading it; nothing
753/// decides on it.
754#[derive(Debug, Serialize, Deserialize)]
755struct TurnRecord {
756    token: String,
757    pid: u32,
758    beat_at: Timestamp,
759}
760
761impl TurnRecord {
762    fn fresh(&self, now: Timestamp) -> bool {
763        now.as_second() - self.beat_at.as_second() <= crate::ask::LEASE_TTL.as_secs() as i64
764    }
765}
766
767/// A token no other caller in this process shares: `rng::entropy` is the
768/// clock and the pid, so two threads in one clock tick would otherwise get the
769/// same token, and with it the same temp file name and the same identity.
770fn fresh_token() -> String {
771    static SEQ: std::sync::atomic::AtomicU64 = std::sync::atomic::AtomicU64::new(0);
772    let n = SEQ.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
773    let seed = crate::rng::entropy() ^ n.wrapping_mul(0x9E37_79B9_7F4A_7C15);
774    crate::rng::SplitMix64::new(seed).uuid_v4()
775}
776
777fn read_turn(path: &Path) -> Option<TurnRecord> {
778    serde_json::from_str(&std::fs::read_to_string(path).ok()?).ok()
779}
780
781/// A takeover lock older than this belonged to a taker that died inside it.
782const TAKEOVER_LOCK_TTL: Duration = Duration::from_secs(10);
783
784/// A break ticket older than this belonged to a taker that died holding it;
785/// the next generation's ticket may then be tried for the same dead token.
786const TICKET_TTL: Duration = Duration::from_secs(10);
787
788/// Most ticket generations tried for one dead token.
789const TICKET_GENERATIONS: u32 = 16;
790
791/// Tickets are forgotten only this long after their last write.
792const TICKET_SWEEP_AGE: Duration = Duration::from_secs(3600);
793
794/// Create `path` exclusively with `body`; `false` when it already exists.
795fn create_exclusive(path: &Path, body: &str) -> Result<bool> {
796    use std::io::Write as _;
797    match std::fs::OpenOptions::new()
798        .write(true)
799        .create_new(true)
800        .open(path)
801    {
802        Ok(mut f) => {
803            if let Err(e) = f.write_all(body.as_bytes()) {
804                drop(f);
805                let _ = std::fs::remove_file(path);
806                return Err(e).with_context(|| format!("write {}", path.display()));
807            }
808            Ok(true)
809        }
810        Err(e) if e.kind() == std::io::ErrorKind::AlreadyExists => Ok(false),
811        Err(e) => Err(e).with_context(|| format!("create {}", path.display())),
812    }
813}
814
815fn create_turn(path: &Path, token: &str, now: Timestamp) -> Result<bool> {
816    let record = TurnRecord {
817        token: token.to_owned(),
818        pid: std::process::id(),
819        beat_at: now,
820    };
821    let body = serde_json::to_string(&record).context("serialize turn lease")?;
822    // Written in full under a private name, then published exclusively: a
823    // reader never sees a half-written lease.
824    let tmp = path.with_extension(format!("turn.{token}.new"));
825    publish_exclusive(path, &tmp, &body)
826}
827
828/// Is there a lease at `path` that must not be taken over? A fresh beat, or
829/// an empty file young enough to be a [`publish_exclusive`] placeholder whose
830/// writer has not yet renamed the real record into place.
831fn lease_blocks(path: &Path, now: Timestamp) -> bool {
832    if read_turn(path).is_some_and(|r| r.fresh(now)) {
833        return true;
834    }
835    std::fs::metadata(path).is_ok_and(|m| {
836        m.len() == 0
837            && m.modified()
838                .ok()
839                .and_then(|t| t.elapsed().ok())
840                .is_some_and(|age| age <= TAKEOVER_LOCK_TTL)
841    })
842}
843
844/// Does this error mean the filesystem has no hard links (as opposed to an
845/// ordinary I/O failure, which must not be hidden by a fallback)?
846fn link_unsupported(e: &std::io::Error) -> bool {
847    e.kind() == std::io::ErrorKind::Unsupported || (cfg!(windows) && e.raw_os_error() == Some(1))
848}
849
850/// Publish the finished file `tmp` (holding `body`) at `path` unless `path`
851/// exists; `false` when it does. `tmp` is always removed.
852///
853/// The first choice is `hard_link`, which is exclusive and atomic. Where hard
854/// links are unsupported the fallback is an exclusive `create_new` of `path`
855/// itself, written through that handle (never a `rename` over `path`, which
856/// would let a stalled writer clobber a successor). Readers can briefly see
857/// the file empty: [`lease_blocks`] treats a young empty one as held, and a
858/// lock side treats it as an "invalid" token that cannot be broken for
859/// [`TAKEOVER_LOCK_TTL`]. Residual windows, not closable by path alone: a
860/// reader may see a partly written file for the length of one small write, and
861/// a writer stalled between create and write for longer than the TTL can have
862/// its file replaced; it then finds the path no longer carries its body and
863/// reports `false`.
864/// The no-hard-link path of [`publish_exclusive`]. A failed write never
865/// removes `path`: by then it may belong to a successor, so the half-made file
866/// is left to expire (a young empty one blocks, an unreadable one is stale).
867fn create_in_place(path: &Path, body: &str) -> Result<bool> {
868    use std::io::Write as _;
869    let mut f = match std::fs::OpenOptions::new()
870        .write(true)
871        .create_new(true)
872        .open(path)
873    {
874        Ok(f) => f,
875        Err(e) if e.kind() == std::io::ErrorKind::AlreadyExists => return Ok(false),
876        Err(e) => return Err(e).with_context(|| format!("create {}", path.display())),
877    };
878    f.write_all(body.as_bytes())
879        .with_context(|| format!("write {}", path.display()))?;
880    // Written through our own handle, so a stalled writer can only write into
881    // its own file; confirm the path still carries it before claiming it.
882    Ok(std::fs::read_to_string(path).is_ok_and(|t| t == body))
883}
884
885fn publish_exclusive(path: &Path, tmp: &Path, body: &str) -> Result<bool> {
886    std::fs::write(tmp, body).with_context(|| format!("write {}", tmp.display()))?;
887    #[cfg(test)]
888    let linked = if failpoint::no_link_forced() {
889        Err(std::io::Error::from(std::io::ErrorKind::Unsupported))
890    } else {
891        std::fs::hard_link(tmp, path)
892    };
893    #[cfg(not(test))]
894    let linked = std::fs::hard_link(tmp, path);
895    let out = match linked {
896        Ok(()) => Ok(true),
897        Err(e) if e.kind() == std::io::ErrorKind::AlreadyExists => Ok(false),
898        Err(e) if link_unsupported(&e) => create_in_place(path, body),
899        Err(e) => Err(e).with_context(|| format!("create {}", path.display())),
900    };
901    let _ = std::fs::remove_file(tmp);
902    out
903}
904
905/// Remove `path` only if it carries exactly `expected`; `true` when it did.
906///
907/// There is no atomic compare-and-unlink on a path, so the file is first
908/// renamed to a private name, which makes the read and the removal act on the
909/// same inode. If it carried something else (a newer holder published between
910/// the caller's read and now) it is put back exclusively, so the wrong lock is
911/// off the path for microseconds instead of deleted. Residual window: in those
912/// microseconds a fourth party can publish at `path`; the restore then loses,
913/// the displaced file is dropped (with a warning) and two holders can overlap
914/// until the survivor's TTL. Path operations alone cannot close that.
915fn remove_if_carries(path: &Path, expected: &str) -> bool {
916    let gone = path.with_extension(format!("gone.{}", fresh_token()));
917    if std::fs::rename(path, &gone).is_err() {
918        return false;
919    }
920    let found = std::fs::read_to_string(&gone);
921    if found.as_ref().is_ok_and(|t| t == expected) {
922        let _ = std::fs::remove_file(&gone);
923        return true;
924    }
925    if let Ok(body) = found {
926        let back = path.with_extension(format!("back.{}", fresh_token()));
927        match publish_exclusive(path, &back, &body) {
928            Ok(true) => {}
929            Ok(false) | Err(_) => tracing::warn!(
930                "{} was replaced while a stale removal had it aside; \
931                 the displaced file is dropped",
932                path.display()
933            ),
934        }
935    }
936    let _ = std::fs::remove_file(&gone);
937    false
938}
939
940/// The short exclusive lock every change to an existing lease (takeover, beat,
941/// release) happens under, so none of them can act on a stale reading. It
942/// ages out after [`TAKEOVER_LOCK_TTL`] in case its holder died inside it.
943struct TurnLock {
944    path: PathBuf,
945    token: String,
946}
947
948impl TurnLock {
949    fn token() -> String {
950        fresh_token()
951    }
952
953    /// Publish `token` at `path` complete (written privately, then published
954    /// exclusively), so nobody reads a half-written token; `false` if `path`
955    /// exists.
956    fn publish(path: &Path, token: &str) -> Result<bool> {
957        let tmp = path.with_extension(format!("lock.{token}.new"));
958        publish_exclusive(path, &tmp, token)
959    }
960
961    fn take(lease: &Path) -> Result<Option<Self>> {
962        let path = lease.with_extension("turn.lock");
963        let token = Self::token();
964        if Self::publish(&path, &token)? {
965            return Ok(Some(Self { path, token }));
966        }
967        let Ok(seen) = std::fs::read_to_string(&path) else {
968            return Ok(None);
969        };
970        // A lock that is not a whole token (an older build wrote it in two
971        // steps) is breakable too, under one fixed ticket name.
972        let key = if !seen.is_empty() && seen.chars().all(|c| c.is_ascii_alphanumeric() || c == '-')
973        {
974            seen.as_str()
975        } else {
976            "invalid"
977        };
978        let aged = std::fs::metadata(&path)
979            .and_then(|m| m.modified())
980            .ok()
981            .and_then(|t| t.elapsed().ok())
982            .is_some_and(|age| age > TAKEOVER_LOCK_TTL);
983        if !aged {
984            return Ok(None);
985        }
986        // Breaking a dead lock is decided by a ticket named after the token
987        // that was judged dead: exactly one taker per generation can create
988        // it. The lock itself is never moved aside, so a fresh lock made by
989        // the winner is never off the path, not even for an instant.
990        //
991        // The ticket name carries a generation, not the wall clock, so no
992        // time boundary can let a second taker in while a ticket is alive.
993        // Generation n+1 is tried only when ticket n is older than
994        // `TICKET_TTL` (its owner died). Residual risk, same kind as the lock's
995        // own TTL: an owner stalled past `TICKET_TTL` between creating its
996        // ticket and publishing can overlap with the next generation's taker.
997        let mut won = false;
998        for n in 0..TICKET_GENERATIONS {
999            let ticket = path.with_extension(format!("lock.{key}.break.{n}"));
1000            if create_exclusive(&ticket, "")? {
1001                won = true;
1002                break;
1003            }
1004            let stale = std::fs::metadata(&ticket)
1005                .and_then(|m| m.modified())
1006                .ok()
1007                .and_then(|t| t.elapsed().ok())
1008                .is_some_and(|age| age > TICKET_TTL);
1009            if !stale {
1010                return Ok(None);
1011            }
1012        }
1013        if !won {
1014            // Every generation was abandoned. A ticket is never unlinked by
1015            // name while it may still be someone's exclusion: only the sweep
1016            // removes tickets, and only ones far older than any taker lives,
1017            // so recovery resumes once they age out.
1018            Self::sweep_tickets(&path);
1019            return Ok(None);
1020        }
1021        Self::sweep_tickets(&path);
1022        // Only the ticket's owner reaches this point for `seen`, but the
1023        // lock's own holder may still release it (its `Drop`) and a third
1024        // taker publish a new one meanwhile, so the removal is verified
1025        // against the content (see [`remove_if_carries`] for the window that
1026        // remains); if the lock changed, leave it.
1027        if !remove_if_carries(&path, &seen) {
1028            return Ok(None);
1029        }
1030        if Self::publish(&path, &token)? {
1031            return Ok(Some(Self { path, token }));
1032        }
1033        Ok(None)
1034    }
1035
1036    /// Forget tickets far older than the lock's TTL. They stay that long so a
1037    /// slow taker that read the same dead token cannot break it a second time.
1038    fn sweep_tickets(path: &Path) {
1039        let (Some(dir), Some(name)) = (path.parent(), path.file_name().and_then(|n| n.to_str()))
1040        else {
1041            return;
1042        };
1043        let prefix = format!("{name}.");
1044        let Ok(entries) = std::fs::read_dir(dir) else {
1045            return;
1046        };
1047        for entry in entries.flatten() {
1048            let file = entry.file_name();
1049            let Some(file) = file.to_str() else { continue };
1050            if !(file.starts_with(&prefix) && file.contains(".break.")) {
1051                continue;
1052            }
1053            let old = entry
1054                .metadata()
1055                .and_then(|m| m.modified())
1056                .ok()
1057                .and_then(|t| t.elapsed().ok())
1058                .is_some_and(|age| age > TICKET_SWEEP_AGE);
1059            if old {
1060                let _ = std::fs::remove_file(entry.path());
1061            }
1062        }
1063    }
1064
1065    /// Wait briefly for the lock; the holders only do a read and a write.
1066    fn take_patiently(lease: &Path) -> Option<Self> {
1067        for _ in 0..50 {
1068            match Self::take(lease) {
1069                Ok(Some(lock)) => return Some(lock),
1070                Ok(None) => std::thread::sleep(Duration::from_millis(10)),
1071                Err(_) => return None,
1072            }
1073        }
1074        None
1075    }
1076}
1077
1078impl Drop for TurnLock {
1079    fn drop(&mut self) {
1080        // Only our own lock: one that aged out and was taken over is not ours.
1081        remove_if_carries(&self.path, &self.token);
1082    }
1083}
1084
1085/// How often a running turn renews its lease: well inside
1086/// [`crate::ask::LEASE_TTL`].
1087pub const TURN_BEAT: Duration = Duration::from_secs(20);
1088
1089/// One talk's cross-process turn slot, released on drop (success, error,
1090/// panic or a dropped handler future alike). Release only removes the file
1091/// while it still carries this lease's token, so a guard that outlived its
1092/// own expiry cannot delete the lease of whoever took over.
1093#[derive(Debug)]
1094pub struct TurnLease {
1095    talk: String,
1096    path: PathBuf,
1097    token: String,
1098}
1099
1100impl TurnLease {
1101    /// Does the lease file still carry this lease's token? A read only.
1102    pub fn holds(&self) -> bool {
1103        read_turn(&self.path).is_some_and(|r| r.token == self.token)
1104    }
1105
1106    /// Renew the lease. `Ok(false)` means it was taken over or removed, so
1107    /// this turn no longer owns the slot; `Err` is a transient failure (the
1108    /// lock stayed busy, a write failed) and the next beat tries again.
1109    pub fn beat(&self) -> Result<bool> {
1110        let _lock = TurnLock::take_patiently(&self.path)
1111            .with_context(|| format!("lock {} to renew it", self.path.display()))?;
1112        let Some(mut record) = read_turn(&self.path).filter(|r| r.token == self.token) else {
1113            return Ok(false);
1114        };
1115        record.beat_at = Timestamp::now();
1116        let body = serde_json::to_string(&record).context("serialize turn lease")?;
1117        let tmp = self.path.with_extension(format!("turn.{}.tmp", self.token));
1118        write_atomic(&tmp, &self.path, &body)?;
1119        Ok(true)
1120    }
1121
1122    /// Run `fut` while renewing this lease every [`TURN_BEAT`]. A transient
1123    /// beat failure is logged and the turn goes on; a lost lease drops `fut`
1124    /// (the turn stops) and is an error, since somebody else may now be
1125    /// running the same conversation.
1126    pub async fn beating<T>(&self, fut: impl std::future::Future<Output = T>) -> Result<T> {
1127        self.beating_every(TURN_BEAT, fut).await
1128    }
1129
1130    async fn beating_every<T>(
1131        &self,
1132        period: Duration,
1133        fut: impl std::future::Future<Output = T>,
1134    ) -> Result<T> {
1135        tokio::pin!(fut);
1136        loop {
1137            match tokio::time::timeout(period, &mut fut).await {
1138                Ok(out) => return Ok(out),
1139                Err(_) => match self.beat() {
1140                    Ok(true) => {}
1141                    Ok(false) => bail!(
1142                        "the turn lease {} was taken over; this turn is stopped",
1143                        self.path.display()
1144                    ),
1145                    Err(e) => tracing::warn!("{e:#}"),
1146                },
1147            }
1148        }
1149    }
1150}
1151
1152impl Drop for TurnLease {
1153    fn drop(&mut self) {
1154        // Under the lock, so a takeover cannot slip in between the check and
1155        // the removal. If the lock stays busy the lease just ages out.
1156        if let Some(_lock) = TurnLock::take_patiently(&self.path) {
1157            if read_turn(&self.path).is_some_and(|r| r.token == self.token) {
1158                let _ = std::fs::remove_file(&self.path);
1159            }
1160        }
1161    }
1162}
1163
1164/// Open a conversation. Takes no agent turn: there is no idea to answer yet,
1165/// and a conversation the operator has not said anything into yet is a
1166/// normal, valid thing to have sitting on the phone.
1167///
1168/// `agent` beats `[roles] chatter`, which beats [`agent::pick`]'s own default
1169/// order (a claude seat, else the first runnable agent in roster order) when
1170/// nothing names a seat at all - see `[roles] chatter`'s own doc in
1171/// [`crate::config`] for why a dedicated field exists rather than reusing a
1172/// judge seat.
1173pub fn begin(store: &Talks, cfg: &Config, repo: PathBuf, agent: Option<&str>) -> Result<Talk> {
1174    begin_with(store, cfg, repo, agent, &Preferred::default())
1175}
1176
1177/// Remembered choices for a new conversation (`POST /api/talks`). Every field
1178/// is soft: one that does not fit `cfg` is dropped on its own and the default
1179/// stands, so a stale remembered value never stops a conversation opening.
1180#[derive(Debug, Default, Clone)]
1181pub struct Preferred {
1182    /// Remembered agent id.
1183    pub agent: Option<String>,
1184    /// Remembered persona id.
1185    pub persona: Option<String>,
1186    /// Remembered implementer count.
1187    pub implementers: Option<i64>,
1188}
1189
1190/// [`begin`] with remembered choices. An explicit `agent` wins over
1191/// `preferred.agent` and keeps its strict error. A preferred agent that is on
1192/// the roster and runnable is an explicit choice (`fallback = false`) unless
1193/// it is the chatter chain's own head. Persona and implementers are applied
1194/// before the single save, so neither is marked dirty: the new seat's briefing
1195/// already carries them.
1196pub fn begin_with(
1197    store: &Talks,
1198    cfg: &Config,
1199    repo: PathBuf,
1200    agent: Option<&str>,
1201    preferred: &Preferred,
1202) -> Result<Talk> {
1203    // Absolute: a relative path means the wrong repository once anything
1204    // other than this process reads it back.
1205    let repo = repo.canonicalize().unwrap_or(repo);
1206    // An explicit agent is a chain of one; otherwise the first id of
1207    // `[roles] chatter` that can run here (later ones are `turn`'s fallbacks).
1208    let head = || -> Result<AgentSpec> {
1209        Ok(agent::pick_chain(
1210            &cfg.agents,
1211            cfg.roles.chatter.as_ref(),
1212            &agent::installed,
1213            "chatter",
1214        )?
1215        .remove(0))
1216    };
1217    let mut fallback = agent.is_none();
1218    let spec = match agent {
1219        Some(id) => agent::pick(&cfg.agents, Some(id), &agent::installed)?,
1220        None => {
1221            match preferred
1222                .agent
1223                .as_deref()
1224                .map(str::trim)
1225                .filter(|id| !id.is_empty())
1226            {
1227                Some(id) => match agent::pick(&cfg.agents, Some(id), &agent::installed) {
1228                    Ok(spec) => {
1229                        fallback = head().is_ok_and(|head| spec.id == head.id);
1230                        spec
1231                    }
1232                    Err(_) => head()?,
1233                },
1234                None => head()?,
1235            }
1236        }
1237    };
1238    let persona = preferred
1239        .persona
1240        .as_deref()
1241        .and_then(|id| crate::persona::find(&cfg.talk.personas, id))
1242        .filter(|p| !p.is_default())
1243        .map(|p| p.id)
1244        .unwrap_or_default();
1245    let implementers = preferred
1246        .implementers
1247        .and_then(|n| u8::try_from(n).ok())
1248        .and_then(|n| check_implementers(n, cfg).ok())
1249        .unwrap_or(1);
1250
1251    let now = Timestamp::now();
1252    let mut talk = Talk {
1253        pending_breaks: None,
1254        schema: SCHEMA,
1255        id: new_id(),
1256        repo,
1257        agent: spec.id.clone(),
1258        status: TalkStatus::Open,
1259        turns: Vec::new(),
1260        pending: String::new(),
1261        pending_attachments: Vec::new(),
1262        fallback,
1263        persona,
1264        persona_dirty: false,
1265        implementers,
1266        implementers_dirty: false,
1267        created_at: now,
1268        updated_at: now,
1269        seat: SeatState::new(SEAT, &spec.id, crate::rng::entropy()),
1270    };
1271    store.put(&mut talk)?;
1272    Ok(talk)
1273}
1274
1275/// Append the operator's turn and flush it, without invoking anything.
1276///
1277/// Split out of [`say`] so `POST /api/talks/{id}/say` can answer once the
1278/// message is safely on disk, and run the agent's half in the background -
1279/// holding the connection for a turn that can run fifteen minutes is the
1280/// wrong shape for a phone.
1281pub fn record(
1282    talk: &mut Talk,
1283    store: &Talks,
1284    text: &str,
1285    attachments: Vec<Attachment>,
1286) -> Result<String> {
1287    // `web::talk_say` reads the talk, then awaits config discovery before
1288    // calling this - a gap a concurrent `POST /api/talks/{id}/close` can land
1289    // in. The guard held for the rest of this function is what actually closes
1290    // that gap: re-reading status without it only shrinks the window a
1291    // concurrent `close` could land in between this call's own read and its
1292    // `put`, it does not remove it. See [`Talks::guard`] and the matching
1293    // guard in `turn`, which this mirrors.
1294    let _guard = store.guard()?;
1295    // A concurrent `Talks::remove` can have landed in that same gap. `put`
1296    // writes unconditionally, so trusting the stale `talk` here would recreate
1297    // the file a delete just removed - the record must still be there for a
1298    // turn to have anywhere to append to.
1299    let Ok(fresh) = store.get(&talk.id) else {
1300        bail!("talk {} was deleted", talk.short());
1301    };
1302    talk.status = fresh.status;
1303    // Do not let this older handle overwrite a draft accepted while it was
1304    // waiting for configuration discovery.
1305    talk.pending = fresh.pending;
1306    talk.pending_breaks = fresh.pending_breaks;
1307    talk.pending_attachments = fresh.pending_attachments;
1308    if !talk.status.open() {
1309        bail!(
1310            "talk {} is {} and takes no more turns",
1311            talk.short(),
1312            talk.status.as_str()
1313        );
1314    }
1315    let text = text.trim();
1316    if text.is_empty() && attachments.is_empty() {
1317        bail!("nothing to say");
1318    }
1319    talk.turns.push(Turn {
1320        who: Who::Operator,
1321        body: text.to_owned(),
1322        at: Timestamp::now(),
1323        attachments,
1324        usage: None,
1325        breaks: Some(Vec::new()),
1326    });
1327    store.put(talk)?;
1328    Ok(text.to_owned())
1329}
1330
1331/// Add an unrecorded message to the durable draft while another turn runs.
1332pub fn queue(
1333    talk: &mut Talk,
1334    store: &Talks,
1335    text: &str,
1336    attachments: Vec<Attachment>,
1337) -> Result<()> {
1338    let text = text.trim();
1339    if text.is_empty() && attachments.is_empty() {
1340        bail!("nothing to say");
1341    }
1342    let _guard = store.guard()?;
1343    let mut fresh = store
1344        .get(&talk.id)
1345        .with_context(|| format!("talk {} was deleted", talk.short()))?;
1346    if !fresh.status.open() {
1347        bail!(
1348            "talk {} is {} and takes no more turns",
1349            fresh.short(),
1350            fresh.status.as_str()
1351        );
1352    }
1353    if !text.is_empty() {
1354        if fresh.pending.is_empty() {
1355            fresh.pending = text.to_owned();
1356            fresh.pending_breaks = Some(Vec::new());
1357        } else {
1358            fresh.pending.push_str("\n\n");
1359            if let Some(b) = fresh.pending_breaks.as_mut() {
1360                b.push(fresh.pending.len());
1361            }
1362            fresh.pending.push_str(text);
1363        }
1364    }
1365    fresh.pending_attachments.extend(attachments);
1366    store.put(&mut fresh)?;
1367    *talk = fresh;
1368    Ok(())
1369}
1370
1371/// Promote the current durable draft to one operator turn.
1372pub fn drain(talk: &mut Talk, store: &Talks) -> Result<Option<String>> {
1373    let _guard = store.guard()?;
1374    let mut fresh = store
1375        .get(&talk.id)
1376        .with_context(|| format!("talk {} was deleted", talk.short()))?;
1377    if !fresh.status.open() || (fresh.pending.is_empty() && fresh.pending_attachments.is_empty()) {
1378        *talk = fresh;
1379        return Ok(None);
1380    }
1381    let text = std::mem::take(&mut fresh.pending);
1382    let attachments = std::mem::take(&mut fresh.pending_attachments);
1383    let breaks = fresh.pending_breaks.take();
1384    fresh.turns.push(Turn {
1385        who: Who::Operator,
1386        body: text.clone(),
1387        at: Timestamp::now(),
1388        attachments,
1389        usage: None,
1390        breaks,
1391    });
1392    store.put(&mut fresh)?;
1393    *talk = fresh;
1394    Ok(Some(text))
1395}
1396
1397/// One operator turn and one agent turn, appended - the synchronous form, used
1398/// by tests and by anything that is fine waiting out the turn itself.
1399pub async fn say(
1400    lease: &TurnLease,
1401    talk: &mut Talk,
1402    store: &Talks,
1403    cfg: &Config,
1404    text: &str,
1405    attachments: Vec<Attachment>,
1406) -> Result<()> {
1407    check_lease(lease, talk)?;
1408    let text = record(talk, store, text, attachments)?;
1409    respond(lease, talk, store, cfg, &text).await
1410}
1411
1412/// The agent's half of a turn: invoke, append, flush. Pairs with [`record`].
1413///
1414/// Requires the talk's [`TurnLease`] (from [`Talks::claim_turn`]), so no
1415/// starter can run a turn without holding the cross-process slot. The lease is
1416/// renewed while the turn runs; a lease lost mid-turn stops it with an error.
1417pub async fn respond(
1418    lease: &TurnLease,
1419    talk: &mut Talk,
1420    store: &Talks,
1421    cfg: &Config,
1422    text: &str,
1423) -> Result<()> {
1424    check_lease(lease, talk)?;
1425    lease
1426        .beating(turn(talk, store, cfg, text))
1427        .await
1428        .and_then(|done| done)
1429}
1430
1431fn check_lease(lease: &TurnLease, talk: &Talk) -> Result<()> {
1432    if lease.talk != talk.id {
1433        bail!("the turn lease is for talk {}, not {}", lease.talk, talk.id);
1434    }
1435    // A lease that aged out and was taken over must not start a turn (or
1436    // write the operator's text) at all.
1437    if !lease.holds() {
1438        bail!("the turn lease for talk {} is no longer held", talk.short());
1439    }
1440    Ok(())
1441}
1442
1443/// Close a conversation. Idempotent: closing an already-closed conversation is
1444/// not an error, since the operator's intent - "I am done with this" - is
1445/// already satisfied.
1446///
1447/// Re-reads the record under [`Talks::guard`] rather than trusting the
1448/// caller's copy of `talk`, and writes that fresh copy back rather than the
1449/// one passed in. `web::talk_close` loads `talk` and calls this right after
1450/// with no gap of its own, but without the guard that load can still land
1451/// between a `record` or `turn` elsewhere reading the file and writing it
1452/// back - and a close built on the older snapshot would put it right back,
1453/// silently dropping whatever turn the other call had just appended.
1454///
1455/// If the re-read fails, this errors rather than falling back to the
1456/// caller's stale copy: `talk::begin` always `put`s the record before handing
1457/// out a `Talk`, so the only way a re-read can fail is a concurrent
1458/// [`Talks::remove`] having deleted it, and writing the stale copy back would
1459/// resurrect exactly what that delete removed.
1460pub fn close(talk: &mut Talk, store: &Talks) -> Result<()> {
1461    let _guard = store.guard()?;
1462    let mut fresh = store
1463        .get(&talk.id)
1464        .with_context(|| format!("talk {} was deleted", talk.short()))?;
1465    fresh.status = TalkStatus::Closed;
1466    // A closed conversation must not replay a draft if it is reopened later.
1467    fresh.pending.clear();
1468    fresh.pending_breaks = None;
1469    fresh.pending_attachments.clear();
1470    store.put(&mut fresh)?;
1471    *talk = fresh;
1472    Ok(())
1473}
1474
1475/// Reopen a closed conversation. Idempotent for the same reason [`close`] is:
1476/// reopening an already-open conversation is not an error, since the
1477/// operator's intent - "I want to keep talking about this" - is already
1478/// satisfied.
1479///
1480/// Written symmetrically with [`close`]: re-reads the record under
1481/// [`Talks::guard`] rather than trusting the caller's copy of `talk`, writes
1482/// that fresh copy back rather than the one passed in, and errors rather than
1483/// falling back to the stale copy if the re-read fails, for the same reasons
1484/// `close`'s doc gives.
1485pub fn reopen(talk: &mut Talk, store: &Talks) -> Result<()> {
1486    let _guard = store.guard()?;
1487    let mut fresh = store
1488        .get(&talk.id)
1489        .with_context(|| format!("talk {} was deleted", talk.short()))?;
1490    fresh.status = TalkStatus::Open;
1491    store.put(&mut fresh)?;
1492    *talk = fresh;
1493    Ok(())
1494}
1495
1496/// Hand the conversation to another roster agent.
1497///
1498/// A CLI session belongs to one CLI and cannot be carried to another, so the
1499/// seat is minted afresh rather than edited: the next turn finds
1500/// `seat.turns == 0` and re-sends the transcript, since the new agent has
1501/// heard none of it. A magi-written note records the change in the
1502/// transcript. Returns `false` (and writes nothing, not even a note) when the
1503/// stored talk already uses `spec`.
1504///
1505/// Re-reads under [`Talks::guard`], like [`close`], and errors rather than
1506/// resurrecting a record a concurrent delete removed. Refusing a closed talk
1507/// or a turn in flight is the caller's job: only it can see the latter.
1508pub fn switch_agent(talk: &mut Talk, store: &Talks, spec: &AgentSpec) -> Result<bool> {
1509    let _guard = store.guard()?;
1510    let mut fresh = store
1511        .get(&talk.id)
1512        .with_context(|| format!("talk {} was deleted", talk.short()))?;
1513    if fresh.agent == spec.id {
1514        *talk = fresh;
1515        return Ok(false);
1516    }
1517    let from = std::mem::replace(&mut fresh.agent, spec.id.clone());
1518    fresh.seat = SeatState::new(SEAT, &spec.id, crate::rng::entropy());
1519    // A deliberate switch pins the conversation to the agent chosen.
1520    fresh.fallback = false;
1521    fresh.turns.push(Turn {
1522        breaks: None,
1523        who: Who::Agent,
1524        body: format!("{MAGI_NOTE}agent changed from {from} to {}", spec.id),
1525        at: Timestamp::now(),
1526        attachments: Vec::new(),
1527        usage: None,
1528    });
1529    store.put(&mut fresh)?;
1530    *talk = fresh;
1531    Ok(true)
1532}
1533
1534/// Pick the conversation's persona (`""` or `default` is the plain voice).
1535///
1536/// The CLI session keeps its seat: the change is recorded with
1537/// [`Talk::persona_dirty`] and a magi note, and the next turn tells the session
1538/// about it (or its fresh briefing already does). Returns `false` and writes
1539/// nothing when the stored persona is already `id`. Refusing a closed talk, an
1540/// unknown id or a turn in flight is the caller's job.
1541pub fn switch_persona(talk: &mut Talk, store: &Talks, id: &str) -> Result<bool> {
1542    let id = if id.trim() == crate::persona::DEFAULT_ID {
1543        ""
1544    } else {
1545        id.trim()
1546    };
1547    let _guard = store.guard()?;
1548    let mut fresh = store
1549        .get(&talk.id)
1550        .with_context(|| format!("talk {} was deleted", talk.short()))?;
1551    if fresh.persona == id {
1552        *talk = fresh;
1553        return Ok(false);
1554    }
1555    let from = std::mem::replace(&mut fresh.persona, id.to_owned());
1556    fresh.persona_dirty = true;
1557    let label = |p: &str| {
1558        if p.is_empty() {
1559            crate::persona::DEFAULT_ID.to_owned()
1560        } else {
1561            p.to_owned()
1562        }
1563    };
1564    fresh.turns.push(Turn {
1565        breaks: None,
1566        who: Who::Agent,
1567        body: format!(
1568            "{MAGI_NOTE}persona changed from {} to {}",
1569            label(&from),
1570            label(id)
1571        ),
1572        at: Timestamp::now(),
1573        attachments: Vec::new(),
1574        usage: None,
1575    });
1576    store.put(&mut fresh)?;
1577    *talk = fresh;
1578    Ok(true)
1579}
1580
1581/// Discard the durable draft without adding a transcript turn.
1582pub fn clear_pending(talk: &mut Talk, store: &Talks) -> Result<()> {
1583    let _guard = store.guard()?;
1584    let mut fresh = store
1585        .get(&talk.id)
1586        .with_context(|| format!("talk {} was deleted", talk.short()))?;
1587    fresh.pending.clear();
1588    fresh.pending_breaks = None;
1589    fresh.pending_attachments.clear();
1590    store.put(&mut fresh)?;
1591    *talk = fresh;
1592    Ok(())
1593}
1594
1595/// Clear a draft only when the caller still sees its complete snapshot.
1596pub fn clear_pending_if_matches(
1597    talk: &mut Talk,
1598    store: &Talks,
1599    expected_text: &str,
1600    expected_attachments: &[String],
1601) -> Result<bool> {
1602    let _guard = store.guard()?;
1603    let mut fresh = store
1604        .get(&talk.id)
1605        .with_context(|| format!("talk {} was deleted", talk.short()))?;
1606    if !pending_matches(&fresh, expected_text, expected_attachments) {
1607        *talk = fresh;
1608        return Ok(false);
1609    }
1610    fresh.pending.clear();
1611    fresh.pending_breaks = None;
1612    fresh.pending_attachments.clear();
1613    store.put(&mut fresh)?;
1614    *talk = fresh;
1615    Ok(true)
1616}
1617
1618/// Replace just the text of the durable draft, but only if the caller's
1619/// snapshot still identifies the entire draft. This refuses to overwrite a
1620/// message another client queued or a draft the drain already promoted.
1621pub fn edit_pending_text(
1622    talk: &mut Talk,
1623    store: &Talks,
1624    text: &str,
1625    expected_text: &str,
1626    expected_attachments: &[String],
1627) -> Result<bool> {
1628    let _guard = store.guard()?;
1629    let mut fresh = store
1630        .get(&talk.id)
1631        .with_context(|| format!("talk {} was deleted", talk.short()))?;
1632    if !pending_matches(&fresh, expected_text, expected_attachments) {
1633        *talk = fresh;
1634        return Ok(false);
1635    }
1636    fresh.pending = text.trim().to_owned();
1637    fresh.pending_breaks = Some(Vec::new());
1638    store.put(&mut fresh)?;
1639    *talk = fresh;
1640    Ok(true)
1641}
1642
1643fn pending_matches(talk: &Talk, expected_text: &str, expected_attachments: &[String]) -> bool {
1644    talk.pending == expected_text
1645        && talk
1646            .pending_attachments
1647            .iter()
1648            .map(|attachment| &attachment.id)
1649            .eq(expected_attachments.iter())
1650}
1651
1652/// Invoke the conversation's agent once and append what it said.
1653///
1654/// The first turn ever taken carries the full [`briefing`], because nothing
1655/// else has told the agent what this conversation is or what it may do.
1656/// Every turn after that resends nothing when the CLI can resume its own
1657/// session, and falls back to [`transcript`] only when it cannot.
1658async fn turn(talk: &mut Talk, store: &Talks, cfg: &Config, text: &str) -> Result<()> {
1659    let spec = cfg
1660        .agents
1661        .iter()
1662        .find(|a| a.id == talk.agent)
1663        .with_context(|| {
1664            format!(
1665                "talk {} was opened with agent `{}`, which is no longer in \
1666                 the roster; restore it in magi.toml or start a new \
1667                 conversation",
1668                talk.short(),
1669                talk.agent
1670            )
1671        })?;
1672
1673    // The newest turn is always the operator message this call is answering
1674    // - `record` appended it before `turn` was ever called - so its own
1675    // attachments are what belong at the end of *this* prompt.
1676    let last_note = attachment_note(
1677        store,
1678        &talk.id,
1679        talk.turns
1680            .last()
1681            .map_or(&[][..], |t| t.attachments.as_slice()),
1682    );
1683
1684    // Every attachment this conversation has ever held, not only this
1685    // turn's: a resumed session gets a fresh process every turn, so a CLI
1686    // whose sandbox needs `--add-dir` (see `agent::build_command`) needs the
1687    // grant again to open an image from an earlier turn, even when nothing
1688    // new was attached just now.
1689    let attachment_paths: Vec<PathBuf> = talk
1690        .turns
1691        .iter()
1692        .flat_map(|t| t.attachments.iter())
1693        .filter_map(|a| store.attachment_path(&talk.id, a))
1694        .collect();
1695
1696    // A question handed to this conversation is answered with `magi answer`,
1697    // which writes the question store; a read-only sandbox refuses that. So
1698    // while such a question is still open - the hand-over turn and the turns
1699    // in which the owner decides - the turn may write, with the question store
1700    // as a writable root. It goes back to read-only once the question is
1701    // answered or abandoned. As for the deputy, what the seat may touch beyond
1702    // that rests on the prompt, not the sandbox.
1703    // `try_home`, not `Questions::open`: a unit test that never pinned a home
1704    // has no store to consult, and must not abort the turn on `run::home()`.
1705    let questions =
1706        crate::run::try_home().map(|home| crate::ask::Questions::at(home.join("questions")));
1707    let consulted = questions
1708        .as_ref()
1709        .is_some_and(|q| crate::consult::pending_consults(q, &talk.id));
1710    let consult_roots: Vec<PathBuf> = match &questions {
1711        Some(q) if consulted => vec![q.root().to_path_buf()],
1712        _ => Vec::new(),
1713    };
1714
1715    let (allow_write, unsandboxed) = turn_access(cfg.talk.allow_write, consulted);
1716
1717    let artifacts = store.artifacts_of(&talk.id);
1718    // From the transcript, not the seat: a switched agent's seat restarts at
1719    // zero and must not overwrite an earlier turn's artifacts.
1720    let operator_turns = talk.turns.iter().filter(|t| t.who == Who::Operator).count();
1721    let stem = format!("turn-{}", operator_turns.max(1));
1722    // The chat's build cache is the same shared one the graph's seats get, so
1723    // a conversation that compiles does not mint another multi-GB target dir.
1724    let cache_dir = cfg.cache_dir();
1725
1726    // The agent holding the conversation, then - only when it came from
1727    // `[roles] chatter` - the rest of that chain, each at most once.
1728    let mut chain = vec![spec.clone()];
1729    if let Some(choice) = cfg.roles.chatter.as_ref()
1730        && talk.fallback
1731    {
1732        for id in choice.ids() {
1733            if id == talk.agent || chain.iter().any(|s| s.id == id) {
1734                continue;
1735            }
1736            match agent::pick(&cfg.agents, Some(id), &agent::installed) {
1737                Ok(s) => chain.push(s),
1738                Err(e) => tracing::warn!("[roles] chatter: skipping `{id}`: {e:#}"),
1739            }
1740        }
1741    }
1742
1743    let persona = crate::persona::active(&cfg.talk.personas, &talk.persona);
1744    let operator_name = cfg.talk.operator_name();
1745    let persona_update = if talk.persona_dirty {
1746        format!(
1747            "{}\n\n",
1748            crate::persona::update_block_for(persona.as_ref(), operator_name)
1749        )
1750    } else {
1751        String::new()
1752    };
1753
1754    // The filing command is not in the transcript either, so a non-Solo count
1755    // rides on every turn that has no session to remember it.
1756    let filing_update = if talk.implementers_dirty {
1757        format!("{}\n\n", filing_update_block(talk.implementers))
1758    } else {
1759        String::new()
1760    };
1761
1762    let mut outcome = None;
1763    let mut fell_back_from: Option<String> = None;
1764    // What the conversation looked like after the first agent's failed try,
1765    // so an exhausted chain leaves exactly what a single failed seat would.
1766    let mut first_try: Option<(String, SeatState)> = None;
1767    for (n, spec) in chain.iter().enumerate() {
1768        if n > 0 {
1769            if first_try.is_none() {
1770                first_try = Some((talk.agent.clone(), talk.seat.clone()));
1771            }
1772            tracing::warn!("chat: falling back from `{}` to `{}`", talk.agent, spec.id);
1773            // A new CLI has none of the old one's conversation: a fresh seat
1774            // puts `has_session` at false and the full transcript is re-sent.
1775            fell_back_from.get_or_insert_with(|| talk.agent.clone());
1776            talk.agent = spec.id.clone();
1777            talk.seat = SeatState::new(SEAT, &spec.id, crate::rng::entropy());
1778        }
1779        let resuming = agent::has_session(spec.kind, &talk.seat, cfg.graph.sessions);
1780        let first_ever = talk.turns.len() <= 1;
1781        let body = if talk.seat.turns == 0 && first_ever {
1782            format!(
1783                "{}\n\n# Operator\n\n{text}{last_note}",
1784                briefing_for(
1785                    &talk.repo,
1786                    &cfg.graph.language,
1787                    cfg.talk.allow_write,
1788                    persona.as_ref(),
1789                    operator_name,
1790                    talk.implementers
1791                )
1792            )
1793        } else if talk.seat.turns == 0 {
1794            // A fresh seat on a conversation that already has history (the
1795            // agent was switched): the briefing, then everything said so far.
1796            format!(
1797                "{}\n\n{}\n\n# Operator\n\n{text}{last_note}",
1798                briefing_for(
1799                    &talk.repo,
1800                    &cfg.graph.language,
1801                    cfg.talk.allow_write,
1802                    persona.as_ref(),
1803                    operator_name,
1804                    talk.implementers
1805                ),
1806                transcript(talk, store)
1807            )
1808        } else if resuming {
1809            format!("{persona_update}{filing_update}{text}{last_note}")
1810        } else {
1811            // No session to hold the persona: the transcript never stores it,
1812            // so a non-default persona is re-sent on every such turn (the
1813            // update block already carries it when the choice just changed).
1814            let mut standing = match (&persona, talk.persona_dirty) {
1815                (Some(p), false) => format!("{}\n", crate::persona::section_for(p, operator_name)),
1816                _ => persona_update.clone(),
1817            };
1818            // The default voice has no persona section to carry the name, and
1819            // the transcript never stores the briefing's addressing line.
1820            if let (None, Some(n)) = (&persona, operator_name) {
1821                standing.push_str(&format!(
1822                    "# Addressing the operator\n{}\n",
1823                    crate::persona::addressing(n)
1824                ));
1825            }
1826            if talk.implementers_dirty || talk.implementers != 1 {
1827                standing.push_str(&format!("{}\n\n", filing_update_block(talk.implementers)));
1828            }
1829            format!("{}\n\n{standing}{text}{last_note}", transcript(talk, store))
1830        };
1831        let attempt_stem = if n == 0 {
1832            stem.clone()
1833        } else {
1834            format!("{stem}-{}", spec.id)
1835        };
1836        let inv = Invocation {
1837            cwd: &talk.repo,
1838            prompt: &body,
1839            timeout: turn_timeout(cfg),
1840            // Off unless this repository's own config opts in - see
1841            // `crate::config::Talk::allow_write` and this module's doc for why
1842            // the default keeps a conversational edit from landing in a checkout
1843            // no run or review can claim.
1844            allow_write,
1845            unsandboxed,
1846            sessions: cfg.graph.sessions,
1847            artifacts: &artifacts,
1848            stem: &attempt_stem,
1849            // The conversation's own id, so `magi task add` run from inside it is
1850            // attributed to this conversation - see `Source::Agent`.
1851            run: &talk.id,
1852            node: crate::queue::CHAT_NODE,
1853            cache_dir: cache_dir.as_deref(),
1854            attachments: &attachment_paths,
1855            writable: &consult_roots,
1856        };
1857        let result = agent::invoke(spec, &mut talk.seat, &inv).await;
1858        let advance = agent::chain_advances(&result);
1859        if n == 0 || !advance {
1860            outcome = Some(result);
1861        } else {
1862            // A later failure is only logged; the note describes the first.
1863            tracing::warn!("chat: fallback agent `{}` also failed", spec.id);
1864        }
1865        if !advance {
1866            break;
1867        }
1868    }
1869    if outcome.as_ref().is_some_and(agent::chain_advances) {
1870        // Exhausted: back to the agent the conversation had, so the note
1871        // below names it and the next turn starts from it again.
1872        if let Some((id, seat)) = first_try {
1873            talk.agent = id;
1874            talk.seat = seat;
1875            fell_back_from = None;
1876        }
1877    }
1878    let outcome = outcome.expect("a chain holds at least one agent");
1879    let note = |why: String| Turn {
1880        breaks: None,
1881        who: Who::Agent,
1882        body: format!("{MAGI_NOTE}{why}"),
1883        at: Timestamp::now(),
1884        attachments: Vec::new(),
1885        usage: None,
1886    };
1887    let (reply, failure) = match outcome {
1888        Err(e) => (
1889            note(format!("could not run agent `{}`: {e}", talk.agent)),
1890            Some(format!("could not run agent `{}`: {e}", talk.agent)),
1891        ),
1892        Ok(out) if out.quota_exhausted() => {
1893            let reset = out
1894                .quota
1895                .as_ref()
1896                .and_then(|q| q.reset.clone())
1897                .map_or_else(String::new, |r| format!(" (resets {r})"));
1898            let why = format!(
1899                "agent `{}` is out of quota{reset}; your message is saved, so \
1900                 say it again when the window reopens",
1901                talk.agent
1902            );
1903            (note(why.clone()), Some(why))
1904        }
1905        Ok(out) if out.timed_out => {
1906            let why = format!(
1907                "agent `{}` did not answer within {}s; your message is saved",
1908                talk.agent,
1909                turn_timeout(cfg).as_secs()
1910            );
1911            (note(why.clone()), Some(why))
1912        }
1913        Ok(out) if !out.usable() => {
1914            let why = format!(
1915                "agent `{}` produced no answer (exit {}); your message is saved",
1916                talk.agent,
1917                out.exit_code
1918                    .map_or_else(|| "unknown".to_owned(), |c| c.to_string())
1919            );
1920            (note(why.clone()), Some(why))
1921        }
1922        Ok(out) => (
1923            Turn {
1924                breaks: None,
1925                who: Who::Agent,
1926                body: out.text.trim().to_owned(),
1927                at: Timestamp::now(),
1928                attachments: Vec::new(),
1929                // `talk.agent` is whoever actually answered: a fallback has
1930                // already moved it, and an exhausted chain never reaches here.
1931                usage: out.context_tokens.map(|context_tokens| TurnUsage {
1932                    context_tokens,
1933                    agent: talk.agent.clone(),
1934                    model: cfg
1935                        .agents
1936                        .iter()
1937                        .find(|a| a.id == talk.agent)
1938                        .and_then(|a| a.model.clone()),
1939                }),
1940            },
1941            None,
1942        ),
1943    };
1944
1945    // A close landed on disk while this turn was in flight is read back here
1946    // rather than trusted from the snapshot this call started with. `store`
1947    // holds nothing else this function does not itself own - the turn guard
1948    // in `web::Ui::begin_talk_turn` keeps `turns` and `seat` this call's
1949    // alone to mutate - but `status` is not behind that guard, and an
1950    // operator's close must stick: the whole point of ending a conversation
1951    // is that an agent's answer to the last message before the close cannot
1952    // silently reopen it. The guard is what makes that read-then-write
1953    // section atomic with `close`'s own - taken only for this tail and not
1954    // for the whole invocation above, so one talk's fifteen-minute turn does
1955    // not block another talk's close from proceeding.
1956    let _guard = store.guard()?;
1957    // A delete is the more final version of that same race: `put` writes
1958    // unconditionally, so a talk removed while this turn was in flight must
1959    // stay removed rather than being written back with this turn's reply
1960    // appended to it. The reply is simply given up on - there is no
1961    // conversation left for it to belong to.
1962    let Ok(fresh) = store.get(&talk.id) else {
1963        return Ok(());
1964    };
1965    talk.status = fresh.status;
1966    // `queue` may have accepted another operator message while the CLI was
1967    // running. This handle predates that write, so preserving only `status`
1968    // would overwrite the durable draft when the reply is appended below.
1969    talk.pending = fresh.pending;
1970    talk.pending_breaks = fresh.pending_breaks;
1971    talk.pending_attachments = fresh.pending_attachments;
1972    if let Some(from) = fell_back_from.filter(|_| failure.is_none()) {
1973        // The switch persists: quota coming back does not move the chat
1974        // home, an operator's switch does.
1975        talk.turns.push(note(format!(
1976            "agent changed from {from} to {} (fallback)",
1977            talk.agent
1978        )));
1979    }
1980    // The persona is in the session's context only once a turn has answered
1981    // (a failed one may never have reached the agent), so only then is it clean.
1982    if failure.is_none() {
1983        talk.persona_dirty = false;
1984        talk.implementers_dirty = false;
1985    }
1986    talk.turns.push(reply);
1987    if let Err(put_err) = store.put(talk) {
1988        // `Talks::put` already retried the write itself - reaching here
1989        // means a passing race is not what this is. An agent's answer,
1990        // possibly the result of an hour-long call, must not vanish with
1991        // nothing to show for it just because the very last step failed:
1992        // pop it back off, stash its text beside the conversation, and
1993        // replace it with a note the operator can actually see, the same
1994        // mechanism the failure branches above already use for a quota or a
1995        // timeout.
1996        let lost = talk.turns.pop().expect("just pushed above");
1997        let stash = stash_lost_turn(store, &talk.id, &stem, &lost);
1998        let why = match &stash {
1999            Ok(path) => format!(
2000                "agent `{}` answered, but the reply could not be saved to \
2001                 this conversation ({put_err:#}); the raw text was kept at \
2002                 {} - your message is saved, ask again",
2003                talk.agent,
2004                path.display()
2005            ),
2006            Err(stash_err) => format!(
2007                "agent `{}` answered, but the reply could not be saved to \
2008                 this conversation ({put_err:#}), and it could not be kept \
2009                 anywhere else either ({stash_err:#}); your message is \
2010                 saved, ask again",
2011                talk.agent
2012            ),
2013        };
2014        talk.turns.push(note(why.clone()));
2015        // Writing the note also carries the seat this call already advanced -
2016        // `agent::invoke` incremented `turns` and, for a vendor that reports
2017        // its own session id, recorded that too. That is what keeps the next
2018        // turn resuming the session the CLI is already holding instead of
2019        // re-opening it, so losing the reply costs the transcript a turn but
2020        // not the conversation.
2021        return match store.put(talk) {
2022            Ok(()) => bail!("{why}"),
2023            Err(note_err) => {
2024                // Even the short note failed to save, which means this
2025                // conversation's file cannot be written at all right now -
2026                // nothing is left for this call to retry or record. Pop the
2027                // note so `talk.turns` matches the transcript on disk, and
2028                // surface both failures for whoever reads the log.
2029                //
2030                // `talk.seat` is deliberately not wound back to match. The
2031                // CLI really did take the turn and really did consume this
2032                // seat's session id; pretending otherwise would be a second
2033                // untruth on top of the unwritable file, and the handle is
2034                // reloaded from disk by the next `drain` or `get` anyway -
2035                // see `web::drain_loop`. What the seat cannot do is reach
2036                // disk, so the record stays a turn behind the CLI until some
2037                // later write lands, and a turn taken before then re-opens a
2038                // session id the CLI already holds. That is the desync
2039                // `20260907-011805-fb57` is about, and tolerating it belongs
2040                // there rather than here: no write this branch could make
2041                // would help, since a failed write is exactly what put it in
2042                // this position twice over.
2043                talk.turns.pop();
2044                Err(note_err).context(why)
2045            }
2046        };
2047    }
2048
2049    match failure {
2050        Some(why) => bail!("{why}"),
2051        None => Ok(()),
2052    }
2053}
2054
2055/// Everything said so far, as prose, for a CLI that cannot resume its own
2056/// conversation.
2057fn transcript(talk: &Talk, store: &Talks) -> String {
2058    let mut out = String::from(
2059        "This conversation cannot resume on the CLI's side, so here is \
2060         everything said so far; answer only the last message.\n",
2061    );
2062    for t in &talk.turns {
2063        let who = match t.who {
2064            Who::Operator => "operator",
2065            Who::Agent if t.body.starts_with(MAGI_NOTE) => "magi",
2066            Who::Agent => "you",
2067        };
2068        out.push_str(&format!("\n## {who}\n\n{}\n", t.body.trim()));
2069        out.push_str(&attachment_note(store, &talk.id, &t.attachments));
2070    }
2071    out
2072}
2073
2074/// The section named at the end of a turn's body, listing every attachment's
2075/// absolute path and mime so the agent knows exactly what to open. Empty
2076/// when `attachments` is, which is every turn but the rare one carrying an
2077/// image, so a turn with none changes nothing about the prompt.
2078fn attachment_note(store: &Talks, talk_id: &str, attachments: &[Attachment]) -> String {
2079    if attachments.is_empty() {
2080        return String::new();
2081    }
2082    let mut out = String::from(
2083        "\n\nThe operator attached the image(s) below to this message. Open \
2084         and look at each one before you answer.\n",
2085    );
2086    for att in attachments {
2087        if let Some(path) = store.attachment_path(talk_id, att) {
2088            out.push_str(&format!("\n- {} ({})", path.display(), att.mime));
2089        }
2090    }
2091    out.push('\n');
2092    out
2093}
2094
2095/// The briefing the agent opens with, sent once as part of its first turn.
2096///
2097/// Pure, so the properties that matter can be asserted without an interview:
2098/// it names `magi task add --solo` (the route this conversation always has to
2099/// changing anything) and it never tells the agent to write a task *file* of
2100/// its own - that would compete with filing through the queue.
2101/// What a turn may do: `(allow_write, unsandboxed)`. Only the repository's own
2102/// `[talk] allow_write` lifts the CLI sandbox (codex's counterpart of claude's
2103/// `bypassPermissions`); a turn writable merely because a handed-over question
2104/// is open keeps the sandbox and gets just the question store as a writable root.
2105pub(crate) fn turn_access(talk_allow_write: bool, consulted: bool) -> (bool, bool) {
2106    (talk_allow_write || consulted, talk_allow_write)
2107}
2108
2109/// `allow_write` only ever adds an extra permission on top of that; it never
2110/// removes the queue as an option, which is why both branches keep the same
2111/// `# When the operator wants something done` section - `write_policy` is
2112/// the only part that changes.
2113///
2114/// It also tells the agent that `--repo` is not stuck naming this
2115/// conversation's own directory: `resolve_repo` (`src/main.rs`) now accepts a
2116/// short `owner/repo` or bare `repo` name and resolves it against
2117/// `[repos] roots`, the same local checkouts `magi repos` lists. Without this
2118/// line an agent asked to change some other repository has no way to know
2119/// that option exists, and the only path it can see - asking the operator to
2120/// dictate a full path - is exactly the friction this change exists to
2121/// remove. A miss or an ambiguous name still fails the command outright, so
2122/// the instruction is to ask rather than guess when that happens - the
2123/// silent-decision line this task must not cross.
2124pub fn briefing(repo: &Path, language: &str, allow_write: bool) -> String {
2125    briefing_with(repo, language, allow_write, None, None)
2126}
2127
2128/// [`briefing`] plus the selected persona's tone-only section and, when the
2129/// operator has a configured name, how to address them. `None` for both is
2130/// byte-for-byte the plain briefing. A name changed mid-session reaches a
2131/// running CLI session only on its next fresh seat (nothing is persisted).
2132pub fn briefing_with(
2133    repo: &Path,
2134    language: &str,
2135    allow_write: bool,
2136    persona: Option<&crate::persona::Persona>,
2137    operator_name: Option<&str>,
2138) -> String {
2139    briefing_for(repo, language, allow_write, persona, operator_name, 1)
2140}
2141
2142/// The most implementers a chat can ask its filed tasks to use.
2143pub const MAX_IMPLEMENTERS: u8 = 3;
2144
2145fn solo_implementers() -> u8 {
2146    1
2147}
2148
2149/// The `magi task add` flag for `n` implementers: `--solo` for one,
2150/// `--implementers N` otherwise (the two are never combined).
2151fn filing_flag(n: u8) -> String {
2152    if n <= 1 {
2153        "--solo".to_owned()
2154    } else {
2155        format!("--implementers {n}")
2156    }
2157}
2158
2159/// The note a resumed (or transcript-replayed) turn opens with after the
2160/// implementer count changed, or while it is not Solo.
2161pub fn filing_update_block(n: u8) -> String {
2162    let flag = filing_flag(n);
2163    if n <= 1 {
2164        format!(
2165            "# Task filing update\n\nThe operator changed how many implementers tasks use. \
2166             From now on file with `magi task add {flag} --repo <repo> <instruction>` \
2167             (one implementer, straight into review).\n"
2168        )
2169    } else {
2170        format!(
2171            "# Task filing update\n\nThe operator changed how many implementers tasks use. \
2172             From now on file with `magi task add {flag} --repo <repo> <instruction>` \
2173             ({n} independent implementations compete). Do not pass `--solo` \
2174             together with `--implementers`; drop any earlier `--solo`.\n"
2175        )
2176    }
2177}
2178
2179/// Validate a requested implementer count: 1..=3, and one the repository's
2180/// config can actually resolve. Never clamps.
2181pub fn check_implementers(n: u8, cfg: &crate::config::Config) -> std::result::Result<u8, String> {
2182    if !(1..=MAX_IMPLEMENTERS).contains(&n) {
2183        return Err(format!(
2184            "implementers must be 1..={MAX_IMPLEMENTERS}, got {n}"
2185        ));
2186    }
2187    if n > 1 {
2188        let mut cfg = cfg.clone();
2189        crate::queue::RunOverrides {
2190            candidates: Some(usize::from(n)),
2191            ..Default::default()
2192        }
2193        .apply(&mut cfg);
2194        cfg.resolve_roles()
2195            .map_err(|e| format!("{n} implementers is not allowed here: {e:#}"))?;
2196    }
2197    Ok(n)
2198}
2199
2200/// Set how many implementers this chat files tasks with. Same shape as
2201/// [`switch_persona`]: `false` and no write when unchanged.
2202pub fn switch_implementers(talk: &mut Talk, store: &Talks, n: u8) -> Result<bool> {
2203    let _guard = store.guard()?;
2204    let mut fresh = store
2205        .get(&talk.id)
2206        .with_context(|| format!("talk {} was deleted", talk.short()))?;
2207    if fresh.implementers == n {
2208        *talk = fresh;
2209        return Ok(false);
2210    }
2211    let from = std::mem::replace(&mut fresh.implementers, n);
2212    fresh.implementers_dirty = true;
2213    fresh.turns.push(Turn {
2214        breaks: None,
2215        who: Who::Agent,
2216        body: format!("{MAGI_NOTE}implementers changed from {from} to {n}"),
2217        at: Timestamp::now(),
2218        attachments: Vec::new(),
2219        usage: None,
2220    });
2221    store.put(&mut fresh)?;
2222    *talk = fresh;
2223    Ok(true)
2224}
2225
2226/// [`briefing_with`] for a chat that files tasks with `implementers`
2227/// implementers (1 is the unchanged `--solo` text).
2228pub fn briefing_for(
2229    repo: &Path,
2230    language: &str,
2231    allow_write: bool,
2232    persona: Option<&crate::persona::Persona>,
2233    operator_name: Option<&str>,
2234    implementers: u8,
2235) -> String {
2236    let flag = filing_flag(implementers);
2237    let shape = if implementers <= 1 {
2238        "Use --solo: it runs the task through one implementer \
2239         straight into review instead of the usual multi-agent competition, \
2240         which is the right shape for a change this conversation has already \
2241         settled, rather than one still worth several independent takes."
2242            .to_owned()
2243    } else {
2244        format!(
2245            "Use --implementers {implementers}: it has {implementers} \
2246             implementers work on the task independently and compete, which \
2247             is the right shape when several independent takes are worth \
2248             having. Never add --solo to it; the two do not go together."
2249        )
2250    };
2251    let write_policy = if allow_write {
2252        "Write access is enabled for this conversation (`allow_write = \
2253         true`), so you may write files - but only a small, \
2254         already-decided edit the operator names outright in this \
2255         conversation, not an implementation. This is a permission on the \
2256         conversation as a whole, not a property of whichever repository \
2257         it happened to start in: if the operator names a different \
2258         repository for that small edit, the policy allows it there too. \
2259         Your own tool may still confine writes to the repository this \
2260         conversation started in regardless - if a write elsewhere is \
2261         refused, say so plainly rather than working around it. Once you \
2262         have made an edit, say plainly what you edited. Anything bigger, \
2263         or anything still open-ended, still goes through the queue below \
2264         rather than being done here."
2265    } else {
2266        "Do not write files. Implementing a change is not this \
2267         conversation's job; a separate, blind competition of agents does \
2268         that, and a repository this conversation has already edited would \
2269         make their diffs unjudgeable."
2270    };
2271    let mut out = format!(
2272        "You are magi's standing conversation partner for its operator, who \
2273         usually has this open on a phone. Keep replies short: no preamble, \
2274         no restating what they just said.\n\n\
2275         # Repository\n\n{repo}\n\n\
2276         You may look around: read files, run shell commands, search history, \
2277         run tests - whatever answers the question. {write_policy}\n\n\
2278         A short, command-shaped message (\"list\", \"info <id>\", \"show \
2279         3cbf\") is almost always the operator asking you to look something \
2280         up, not an instruction to file - answer it yourself with `magi \
2281         list`, `magi show <id>`, `magi task list`, or the like, the same way \
2282         you would answer any other question in this conversation.\n\n\
2283         # When the operator wants something done\n\n\
2284         Run:\n\n\
2285         magi task add {flag} --repo {repo} <instruction>\n\n\
2286         and tell the operator the task id it prints, so they can follow it \
2287         from the Queue. If it refuses with a duplicate warning (the \
2288         instruction names a branch, commit or pull request that an \
2289         unfinished task, run or PR already owns), do not repeat it with \
2290         --force yourself: tell the operator what it matched and let them \
2291         decide. Write <instruction> so that an implementer who has \
2292         never seen this conversation can act on it alone - it is everything \
2293         they get. {shape}\n\n\
2294         If the operator asks for something in a different repository, \
2295         --repo does not have to be a full path: --repo owner/repo (or just \
2296         repo, when that is unambiguous) is resolved against local checkouts \
2297         the same way `magi repos` lists them. If the command fails because \
2298         nothing matches or more than one checkout shares that name, ask the \
2299         operator which repository they mean (or run `magi repos` yourself \
2300         to see the candidates) rather than guessing.\n\n\
2301         The current state of the code is whatever origin/main holds, not \
2302         whatever a working tree shows: a primary checkout often lags \
2303         upstream, sits on a detached HEAD and carries uncommitted changes. \
2304         Before answering about code, run `git fetch origin` in that \
2305         repository if it is cheap, then read through \
2306         `git show origin/main:<path>` or `git grep <pattern> origin/main`. \
2307         If the working tree differs, say so; if the fetch fails, say that \
2308         too, so the operator knows the answer may be stale.\n\n\
2309         If the operator attached an image (a screenshot, say) that the task \
2310         is about, pass it with `--attach <path>`, using the absolute path \
2311         the turn's attachment note gives; repeat the flag for several. \
2312         `magi task add {flag} --attach <path> <instruction>` copies the \
2313         file into the task, so the implementer receives it. Do not paste the \
2314         path into <instruction> instead: deleting this conversation deletes \
2315         its attachments, and then that path reaches no one.\n",
2316        repo = repo.display(),
2317    );
2318    out.push_str(&language_note(language));
2319    match (persona, operator_name) {
2320        (Some(p), n) => out.push_str(&crate::persona::section_for(p, n)),
2321        (None, Some(n)) => {
2322            out.push_str("\n# Addressing the operator\n");
2323            out.push_str(&crate::persona::addressing(n));
2324        }
2325        (None, None) => {}
2326    }
2327    out
2328}
2329
2330/// The operator is talking, so their language matters here more than in most
2331/// prompts magi sends.
2332fn language_note(language: &str) -> String {
2333    if language.trim().is_empty() || language.eq_ignore_ascii_case("en") {
2334        String::new()
2335    } else {
2336        format!("\nHold this conversation in {language}.\n")
2337    }
2338}
2339
2340/// Queue tasks this conversation has filed, oldest first.
2341///
2342/// A task is this conversation's when its [`Source::Agent`] names this
2343/// conversation's id as `run` - which is exactly what happens when
2344/// `magi task add` is run from inside a turn, because [`turn`] passes the
2345/// conversation's own id as [`Invocation::run`].
2346pub fn tasks_of(queue: &Queue, talk_id: &str) -> Vec<Task> {
2347    let mut tasks: Vec<Task> = queue
2348        .list()
2349        .into_iter()
2350        .filter(|t| matches!(&t.source, Source::Agent { run, .. } if run == talk_id))
2351        .collect();
2352    tasks.sort_unstable_by(|a, b| a.id.cmp(&b.id));
2353    tasks
2354}
2355
2356fn read_path(path: &Path) -> Result<Talk> {
2357    let body = std::fs::read_to_string(path).with_context(|| format!("read {}", path.display()))?;
2358    serde_json::from_str(&body).with_context(|| format!("parse {}", path.display()))
2359}
2360
2361/// How many times [`write_atomic`] retries a failed write-then-rename before
2362/// giving up.
2363const PUT_RETRIES: u32 = 5;
2364
2365/// Write `body` to `tmp` and rename it onto `path`, retrying the whole thing
2366/// a handful of times with a short sleep in between.
2367///
2368/// The only failure this is meant to absorb is a passing one - most
2369/// concretely, a reader elsewhere in this process (or another `magi`
2370/// process) with `path` briefly open for `read_to_string` at the exact
2371/// moment this call tries to rename over it. That clears in milliseconds
2372/// once the reader lets go; a caller still failing after several short
2373/// sleeps has something more durable wrong (a full disk, a permissions
2374/// change) that a longer sleep would not fix either, and is left to report
2375/// it.
2376fn write_atomic(tmp: &Path, path: &Path, body: &str) -> Result<()> {
2377    let mut last_err = None;
2378    for attempt in 0..PUT_RETRIES {
2379        if attempt > 0 {
2380            std::thread::sleep(Duration::from_millis(20 * u64::from(attempt)));
2381        }
2382        match try_write_atomic(tmp, path, body) {
2383            Ok(()) => return Ok(()),
2384            Err(e) => last_err = Some(e),
2385        }
2386    }
2387    Err(last_err.expect("the loop above always runs at least once"))
2388}
2389
2390fn try_write_atomic(tmp: &Path, path: &Path, body: &str) -> Result<()> {
2391    #[cfg(test)]
2392    if failpoint::take_forced_put_failure() {
2393        bail!("simulated write failure (test)");
2394    }
2395    std::fs::write(tmp, body).with_context(|| format!("write {}", tmp.display()))?;
2396    std::fs::rename(tmp, path).with_context(|| format!("replace {}", path.display()))?;
2397    Ok(())
2398}
2399
2400/// Last resort when `turn`'s own `store.put` fails even after
2401/// [`write_atomic`]'s retries: keep the generated text somewhere still
2402/// findable rather than let the whole of an agent's answer disappear along
2403/// with the write that was supposed to record it.
2404fn stash_lost_turn(store: &Talks, id: &str, stem: &str, reply: &Turn) -> Result<PathBuf> {
2405    let dir = store.artifacts_of(id);
2406    std::fs::create_dir_all(&dir).with_context(|| format!("create {}", dir.display()))?;
2407    let path = dir.join(format!("{stem}-lost.txt"));
2408    std::fs::write(&path, &reply.body).with_context(|| format!("write {}", path.display()))?;
2409    Ok(path)
2410}
2411
2412/// A test-only seam that lets [`try_write_atomic`] simulate the kind of
2413/// passing I/O race [`write_atomic`] is meant to retry through, without
2414/// depending on real OS-level file-locking behaviour, which differs across
2415/// the three platforms this crate ships on (and, on the one platform where a
2416/// reader really does block a rename, is awkward to trigger deterministically
2417/// in a unit test).
2418#[cfg(test)]
2419mod failpoint {
2420    use std::cell::Cell;
2421
2422    thread_local! {
2423        static FORCE_PUT_FAILURES: Cell<u32> = const { Cell::new(0) };
2424        static FORCE_NO_LINK: Cell<bool> = const { Cell::new(false) };
2425    }
2426
2427    /// While set, `hard_link` on this thread fails as unsupported, so the
2428    /// create-and-rename fallback runs.
2429    pub(super) fn force_no_link(on: bool) {
2430        FORCE_NO_LINK.with(|c| c.set(on));
2431    }
2432
2433    pub(super) fn no_link_forced() -> bool {
2434        FORCE_NO_LINK.with(Cell::get)
2435    }
2436
2437    /// Arrange for the next `count` calls into [`super::try_write_atomic`] to
2438    /// fail before touching the filesystem at all.
2439    pub(super) fn force_put_failures(count: u32) {
2440        FORCE_PUT_FAILURES.with(|c| c.set(count));
2441    }
2442
2443    /// Consumed once per attempt inside [`super::try_write_atomic`]; `true`
2444    /// means simulate this attempt failing.
2445    pub(super) fn take_forced_put_failure() -> bool {
2446        FORCE_PUT_FAILURES.with(|c| {
2447            let n = c.get();
2448            if n == 0 {
2449                false
2450            } else {
2451                c.set(n - 1);
2452                true
2453            }
2454        })
2455    }
2456}
2457
2458fn short(id: &str) -> &str {
2459    id.split('-').next_back().unwrap_or(id)
2460}
2461
2462fn new_id() -> String {
2463    let stamp = jiff::Zoned::now().strftime("%Y%m%d-%H%M%S");
2464    let seed = crate::rng::entropy();
2465    format!("{stamp}-{:04x}", (seed ^ (seed >> 32)) & 0xffff)
2466}
2467
2468/// Extension an attachment's bytes are stored under, from its (already
2469/// validated) mime. The one place this mapping exists on the write side;
2470/// `web`'s own whitelist is what actually decides which mimes are accepted
2471/// in the first place.
2472fn attachment_ext(mime: &str) -> Option<&'static str> {
2473    match mime {
2474        "image/png" => Some("png"),
2475        "image/jpeg" => Some("jpg"),
2476        "image/gif" => Some("gif"),
2477        "image/webp" => Some("webp"),
2478        _ => None,
2479    }
2480}
2481
2482/// Is `id` a shape [`put_attachment`](Talks::put_attachment) could have
2483/// produced? 32 lowercase hex digits and nothing else, checked before an id
2484/// that came from the client is ever allowed to build a path - so `..` and a
2485/// path separator are never even possible.
2486pub fn valid_attachment_id(id: &str) -> bool {
2487    id.len() == 32
2488        && id
2489            .bytes()
2490            .all(|b| b.is_ascii_digit() || (b'a'..=b'f').contains(&b))
2491}
2492
2493/// A fresh attachment id: 128 bits of process entropy as lowercase hex - the
2494/// same "mint it, never take it from the client" rule [`new_id`] follows for
2495/// conversation ids.
2496fn new_attachment_id() -> String {
2497    let mut r = crate::rng::SplitMix64::new(crate::rng::entropy());
2498    format!("{:016x}{:016x}", r.next_u64(), r.next_u64())
2499}
2500
2501#[cfg(test)]
2502mod tests {
2503    #[test]
2504    fn turn_access_lifts_the_sandbox_only_for_the_talk_opt_in() {
2505        use super::turn_access;
2506        assert_eq!(turn_access(false, false), (false, false));
2507        assert_eq!(turn_access(true, false), (true, true));
2508        // A handed-over question opens the question store, never the sandbox.
2509        assert_eq!(turn_access(false, true), (true, false));
2510        assert_eq!(turn_access(true, true), (true, true));
2511    }
2512
2513    #[test]
2514    fn the_briefing_points_at_origin_main_not_the_working_tree() {
2515        let b = briefing(Path::new("/r"), "en", false);
2516        assert!(b.contains("origin/main"));
2517        assert!(b.contains("git show origin/main:"));
2518    }
2519    use std::collections::BTreeMap;
2520
2521    use crate::config::{AgentChoice, AgentKind, AgentSpec, Graph};
2522    use crate::queue::{Queue, Source, Task};
2523
2524    use super::*;
2525
2526    fn ctx_agent(id: &str, model: Option<&str>) -> AgentSpec {
2527        AgentSpec {
2528            id: id.to_owned(),
2529            kind: AgentKind::Command,
2530            model: model.map(str::to_owned),
2531            command: Vec::new(),
2532            extra_args: Vec::new(),
2533            env: BTreeMap::new(),
2534            prompt_delivery: None,
2535        }
2536    }
2537
2538    fn ctx_talk(agent: &str, turns: Vec<Turn>) -> Talk {
2539        Talk {
2540            pending_breaks: None,
2541            schema: SCHEMA,
2542            id: "20260904-014455-ab12".to_owned(),
2543            repo: PathBuf::from("."),
2544            agent: agent.to_owned(),
2545            status: TalkStatus::Open,
2546            turns,
2547            pending: String::new(),
2548            pending_attachments: Vec::new(),
2549            fallback: false,
2550            persona: String::new(),
2551            persona_dirty: false,
2552            implementers: 1,
2553            implementers_dirty: false,
2554            created_at: Timestamp::now(),
2555            updated_at: Timestamp::now(),
2556            seat: SeatState::new(SEAT, agent, 1),
2557        }
2558    }
2559
2560    fn reply(body: &str, usage: Option<(u64, &str, Option<&str>)>) -> Turn {
2561        Turn {
2562            breaks: None,
2563            who: Who::Agent,
2564            body: body.to_owned(),
2565            at: Timestamp::now(),
2566            attachments: Vec::new(),
2567            usage: usage.map(|(t, a, m)| TurnUsage {
2568                context_tokens: t,
2569                agent: a.to_owned(),
2570                model: m.map(str::to_owned),
2571            }),
2572        }
2573    }
2574
2575    fn ctx_config(windows: &[(&str, u64)]) -> Config {
2576        Config {
2577            agents: vec![
2578                ctx_agent("small", Some("small-model")),
2579                ctx_agent("big", Some("big-model")),
2580                ctx_agent("plain", None),
2581            ],
2582            context_windows: windows.iter().map(|(k, v)| ((*k).to_owned(), *v)).collect(),
2583            ..Config::default()
2584        }
2585    }
2586
2587    #[test]
2588    fn context_usage_computes_percent_and_warns_at_eighty() {
2589        let cfg = ctx_config(&[("small-model", 1000)]);
2590        let at = |tokens| {
2591            let t = ctx_talk(
2592                "small",
2593                vec![reply("hi", Some((tokens, "small", Some("small-model"))))],
2594            );
2595            context_usage(&t, Some(&cfg))
2596        };
2597        let u = at(799);
2598        assert_eq!((u.percent, u.warn, u.window), (Some(79), false, Some(1000)));
2599        let u = at(800);
2600        assert_eq!((u.percent, u.warn), (Some(80), true));
2601        let u = at(1500);
2602        assert_eq!((u.percent, u.warn), (Some(150), true));
2603        assert!(!u.since_switch);
2604    }
2605
2606    #[test]
2607    fn context_usage_is_unknown_without_usage_and_never_looks_back() {
2608        let cfg = ctx_config(&[("small-model", 1000)]);
2609        let t = ctx_talk(
2610            "small",
2611            vec![
2612                reply("old", Some((900, "small", Some("small-model")))),
2613                reply("new", None),
2614            ],
2615        );
2616        let u = context_usage(&t, Some(&cfg));
2617        // No measurement in the latest reply: an estimate, never the stale 900.
2618        assert!(u.estimated);
2619        assert_ne!(u.tokens, Some(900));
2620        assert!(u.tokens.is_some());
2621        // A magi note after the reply neither hides nor replaces it.
2622        let t = ctx_talk(
2623            "small",
2624            vec![
2625                reply("old", Some((900, "small", Some("small-model")))),
2626                reply("magi: could not run agent", None),
2627            ],
2628        );
2629        assert_eq!(context_usage(&t, Some(&cfg)).tokens, Some(900));
2630        assert_eq!(
2631            context_usage(&ctx_talk("small", Vec::new()), Some(&cfg)).tokens,
2632            None
2633        );
2634    }
2635
2636    #[test]
2637    fn estimate_counts_chars_both_sides_and_standing_prompt() {
2638        let mut t = ctx_talk("small", vec![reply("abcdefg", None)]);
2639        assert_eq!(estimate_context_tokens(&t, 0), Some(2)); // 7 chars -> 2
2640        let op = Turn {
2641            who: Who::Operator,
2642            ..reply("abcdefg", None)
2643        };
2644        t.turns.push(op);
2645        assert_eq!(estimate_context_tokens(&t, 0), Some(4));
2646        assert!(
2647            estimate_context_tokens(&t, 700).unwrap() > estimate_context_tokens(&t, 0).unwrap()
2648        );
2649        // Characters, not bytes: 7 kanji are 7 chars.
2650        let ja = ctx_talk("small", vec![reply("日本語日本語日", None)]);
2651        assert_eq!(estimate_context_tokens(&ja, 0), Some(2));
2652        // magi notes are not sent to the agent; with nothing else, unknown.
2653        let note = ctx_talk("small", vec![reply("magi: could not run agent", None)]);
2654        assert_eq!(estimate_context_tokens(&note, 1000), None);
2655        assert_eq!(
2656            estimate_context_tokens(&ctx_talk("small", Vec::new()), 1000),
2657            None
2658        );
2659    }
2660
2661    #[test]
2662    fn context_usage_measured_wins_and_estimate_gets_percent_and_warn() {
2663        let cfg = ctx_config(&[("small-model", 1000)]);
2664        let t = ctx_talk(
2665            "small",
2666            vec![reply(
2667                &"x".repeat(5000),
2668                Some((10, "small", Some("small-model"))),
2669            )],
2670        );
2671        let u = context_usage(&t, Some(&cfg));
2672        assert_eq!((u.tokens, u.estimated), (Some(10), false));
2673        let t = ctx_talk("small", vec![reply(&"x".repeat(5000), None)]);
2674        let u = context_usage(&t, Some(&cfg));
2675        assert!(u.estimated && !u.since_switch);
2676        assert_eq!(u.window, Some(1000));
2677        assert!(u.warn && u.percent.unwrap() >= 80);
2678        let t = ctx_talk("small", vec![reply("hi", None)]);
2679        let u = context_usage(&t, Some(&cfg));
2680        assert!(u.estimated && u.percent.is_some());
2681    }
2682
2683    #[test]
2684    fn context_usage_without_a_window_shows_tokens_only() {
2685        let cfg = ctx_config(&[]);
2686        // No model at all, and a model nothing matches.
2687        let t = ctx_talk("plain", vec![reply("hi", Some((5000, "plain", None)))]);
2688        let u = context_usage(&t, Some(&cfg));
2689        assert_eq!(
2690            (u.tokens, u.window, u.percent, u.warn),
2691            (Some(5000), None, None, false)
2692        );
2693        let t = ctx_talk(
2694            "small",
2695            vec![reply("hi", Some((5000, "small", Some("small-model"))))],
2696        );
2697        assert_eq!(context_usage(&t, Some(&cfg)).percent, None);
2698        // No readable config: same, and no panic.
2699        assert_eq!(context_usage(&t, None).window, None);
2700    }
2701
2702    #[test]
2703    fn context_usage_switching_model_changes_the_denominator() {
2704        let cfg = ctx_config(&[("small-model", 1000), ("big-model", 10_000)]);
2705        let used = reply("hi", Some((900, "small", Some("small-model"))));
2706        let before = context_usage(&ctx_talk("small", vec![used.clone()]), Some(&cfg));
2707        assert_eq!(
2708            (before.percent, before.warn, before.since_switch),
2709            (Some(90), true, false)
2710        );
2711        // Same turns, conversation now on the big model: new denominator, and
2712        // the figure is flagged as describing the previous session.
2713        let after = context_usage(&ctx_talk("big", vec![used]), Some(&cfg));
2714        assert_eq!(after.window, Some(10_000));
2715        assert_eq!(
2716            (after.percent, after.warn, after.since_switch),
2717            (Some(9), false, true)
2718        );
2719        assert_eq!(after.model.as_deref(), Some("big-model"));
2720    }
2721
2722    #[test]
2723    fn a_turn_recorded_before_usage_existed_still_reads() {
2724        let old = r#"{"who":"agent","body":"hi","at":"2026-09-04T01:44:55Z"}"#;
2725        let turn: Turn = serde_json::from_str(old).expect("old turn reads");
2726        assert!(turn.usage.is_none());
2727        let json = serde_json::to_string(&turn).expect("serialize");
2728        assert!(
2729            !json.contains("usage"),
2730            "absent usage is not written: {json}"
2731        );
2732    }
2733
2734    /// A store of its own, with no process-global state.
2735    fn store() -> (tempfile::TempDir, Talks) {
2736        let tmp = tempfile::tempdir().expect("tempdir");
2737        let talks = Talks::at(tmp.path().join("talks"));
2738        (tmp, talks)
2739    }
2740
2741    /// A `kind = "command"` agent whose whole behaviour is a POSIX shell
2742    /// script - see `chat`'s tests for why no test here may spawn a real
2743    /// agent CLI.
2744    fn mock_agent(dir: &Path, script: &str, env: BTreeMap<String, String>) -> AgentSpec {
2745        let path = dir.join("mock-talk-agent.sh");
2746        std::fs::write(&path, script).expect("write mock");
2747        AgentSpec {
2748            id: "mock".to_owned(),
2749            kind: AgentKind::Command,
2750            model: None,
2751            command: vec!["sh".to_owned(), path.to_string_lossy().into_owned()],
2752            extra_args: Vec::new(),
2753            env,
2754            prompt_delivery: None,
2755        }
2756    }
2757
2758    fn config(spec: AgentSpec) -> Config {
2759        Config {
2760            agents: vec![spec],
2761            graph: Graph {
2762                language: "en".to_owned(),
2763                ..Graph::default()
2764            },
2765            ..Config::default()
2766        }
2767    }
2768
2769    /// Echo a canned reply, ignoring the prompt on stdin.
2770    const REPLY: &str = "#!/bin/sh\ncat >/dev/null\nprintf '%s\\n' \"$MOCK_REPLY\"\n";
2771
2772    /// Say nothing and fail, the way a CLI that cannot start does.
2773    const BROKEN: &str = "#!/bin/sh\ncat >/dev/null\nexit 3\n";
2774
2775    /// Reply with the prompt it was given, so a test can inspect exactly what
2776    /// the agent received on stdin.
2777    const ECHO: &str = "#!/bin/sh\ncat\n";
2778
2779    fn env(reply: &str) -> BTreeMap<String, String> {
2780        BTreeMap::from([("MOCK_REPLY".to_owned(), reply.to_owned())])
2781    }
2782
2783    #[test]
2784    fn the_frozen_json_field_names_round_trip_through_disk() {
2785        let (tmp, talks) = store();
2786        let mut talk = Talk {
2787            pending_breaks: None,
2788            schema: SCHEMA,
2789            id: "20260904-014455-ab12".to_owned(),
2790            repo: tmp.path().to_owned(),
2791            agent: "sonnet".to_owned(),
2792            status: TalkStatus::Open,
2793            turns: Vec::new(),
2794            pending: String::new(),
2795            pending_attachments: Vec::new(),
2796            fallback: false,
2797            persona: String::new(),
2798            persona_dirty: false,
2799            implementers: 1,
2800            implementers_dirty: false,
2801            created_at: Timestamp::now(),
2802            updated_at: Timestamp::now(),
2803            seat: SeatState::new(SEAT, "sonnet", 7),
2804        };
2805        talks.put(&mut talk).expect("put");
2806
2807        let raw = std::fs::read_to_string(talks.path_of(&talk.id)).expect("read back");
2808        let v: serde_json::Value = serde_json::from_str(&raw).expect("parse");
2809        for field in [
2810            "schema",
2811            "id",
2812            "repo",
2813            "agent",
2814            "status",
2815            "turns",
2816            "created_at",
2817            "updated_at",
2818        ] {
2819            assert!(v.get(field).is_some(), "missing field `{field}`");
2820        }
2821        assert_eq!(v["schema"], 1);
2822        assert_eq!(v["status"], "open");
2823
2824        let back = talks.get(&talk.id).expect("get");
2825        assert_eq!(back.id, talk.id);
2826        assert_eq!(back.status, TalkStatus::Open);
2827    }
2828
2829    #[test]
2830    fn opening_a_talk_takes_no_agent_turn() {
2831        let (tmp, talks) = store();
2832        // A script that would fail loudly if it were ever run: `begin` must
2833        // not invoke anything, since there is nothing yet for an agent to
2834        // answer.
2835        let spec = mock_agent(tmp.path(), BROKEN, BTreeMap::new());
2836        let cfg = config(spec);
2837
2838        let talk = begin(&talks, &cfg, tmp.path().to_owned(), None).expect("begin");
2839        assert_eq!(talk.status, TalkStatus::Open);
2840        assert!(talk.turns.is_empty(), "nothing has been said yet");
2841
2842        let on_disk = talks.get(&talk.id).expect("get");
2843        assert_eq!(on_disk.turns.len(), 0);
2844    }
2845
2846    /// `[roles] chatter`, when set, decides who holds this conversation; unset,
2847    /// it falls back to [`agent::pick`]'s own default order (a claude seat,
2848    /// else the first runnable agent in roster order) rather than to any
2849    /// other role - see `[roles] chatter`'s own doc in [`crate::config`] for
2850    /// why a dedicated field exists at all: opening this against the same
2851    /// seat as a judge is what produced the `agent ... did not answer within
2852    /// 300s` timeout that led to it.
2853    #[test]
2854    fn chatter_wins_when_set_and_falls_back_to_pick_s_default_order_otherwise() {
2855        let (tmp, talks) = store();
2856        let first_spec = mock_agent(tmp.path(), BROKEN, BTreeMap::new());
2857        let mut chatter_spec = mock_agent(tmp.path(), BROKEN, BTreeMap::new());
2858        chatter_spec.id = "chatter-mock".to_owned();
2859
2860        let mut cfg = Config {
2861            agents: vec![first_spec.clone(), chatter_spec.clone()],
2862            graph: Graph {
2863                language: "en".to_owned(),
2864                ..Graph::default()
2865            },
2866            ..Config::default()
2867        };
2868        cfg.roles.chatter = Some(chatter_spec.id.as_str().into());
2869
2870        let talk =
2871            begin(&talks, &cfg, tmp.path().to_owned(), None).expect("begin with chatter set");
2872        assert_eq!(talk.agent, chatter_spec.id, "an explicit chatter must win");
2873
2874        cfg.roles.chatter = None;
2875        let fallback =
2876            begin(&talks, &cfg, tmp.path().to_owned(), None).expect("begin with chatter unset");
2877        assert_eq!(
2878            fallback.agent, first_spec.id,
2879            "unset chatter must fall back to agent::pick's own default order"
2880        );
2881    }
2882
2883    /// A conversation recorded before attachments existed - schema 1, no
2884    /// `attachments` key on any turn - must still read.
2885    #[test]
2886    fn a_talk_recorded_without_attachments_still_reads() {
2887        let (tmp, talks) = store();
2888        let path = talks.path_of("20260904-014455-ab12");
2889        std::fs::create_dir_all(talks.root()).expect("talks dir");
2890        std::fs::write(
2891            &path,
2892            serde_json::json!({
2893                "schema": 1,
2894                "id": "20260904-014455-ab12",
2895                "repo": tmp.path(),
2896                "agent": "sonnet",
2897                "status": "open",
2898                "turns": [
2899                    { "who": "operator", "body": "still there?",
2900                      "at": Timestamp::now().to_string() },
2901                ],
2902                "created_at": Timestamp::now().to_string(),
2903                "updated_at": Timestamp::now().to_string(),
2904                "seat": SeatState::new(SEAT, "sonnet", 7),
2905            })
2906            .to_string(),
2907        )
2908        .expect("write pre-attachments talk");
2909
2910        let talk = talks.get("20260904-014455-ab12").expect("must still read");
2911        assert!(talk.turns[0].attachments.is_empty());
2912    }
2913
2914    fn lease_store() -> (tempfile::TempDir, Talks, Talks) {
2915        let tmp = tempfile::TempDir::new().expect("tmp");
2916        let root = tmp.path().join("talks");
2917        (tmp, Talks::at(root.clone()), Talks::at(root))
2918    }
2919
2920    #[test]
2921    fn two_starters_on_one_talk_one_wins_and_the_other_is_refused() {
2922        let (_tmp, a, b) = lease_store();
2923        let won = a.claim_turn("t1").expect("claim").expect("first wins");
2924        assert!(
2925            b.claim_turn("t1").expect("claim").is_none(),
2926            "second is refused"
2927        );
2928        assert!(b.turn_held("t1"));
2929        assert!(
2930            b.claim_turn("t2").expect("claim").is_some(),
2931            "other talks are free"
2932        );
2933        drop(won);
2934    }
2935
2936    #[test]
2937    fn a_stale_lease_is_taken_over_and_the_old_guard_cannot_release_it() {
2938        let (_tmp, a, b) = lease_store();
2939        let old = a.claim_turn("t1").expect("claim").expect("held");
2940        let later = Timestamp::now()
2941            .checked_add(jiff::SignedDuration::from_secs(
2942                crate::ask::LEASE_TTL.as_secs() as i64 + 5,
2943            ))
2944            .expect("later");
2945        let new = b
2946            .claim_turn_at("t1", later)
2947            .expect("claim")
2948            .expect("a stale lease is taken over");
2949        drop(old);
2950        assert!(a.turn_held("t1"), "the old guard left the new lease alone");
2951        assert!(new.beat().expect("beat"), "the new owner still beats");
2952        drop(new);
2953        assert!(!a.turn_held("t1"));
2954    }
2955
2956    #[tokio::test]
2957    async fn a_turn_whose_lease_was_taken_over_is_stopped() {
2958        let (_tmp, a, b) = lease_store();
2959        let old = a.claim_turn("t1").expect("claim").expect("held");
2960        let later = Timestamp::now()
2961            .checked_add(jiff::SignedDuration::from_secs(
2962                crate::ask::LEASE_TTL.as_secs() as i64 + 5,
2963            ))
2964            .expect("later");
2965        let _new = b
2966            .claim_turn_at("t1", later)
2967            .expect("claim")
2968            .expect("taken over");
2969        let out = old
2970            .beating_every(Duration::from_millis(10), std::future::pending::<()>())
2971            .await;
2972        assert!(out.is_err(), "the displaced turn must stop, not run on");
2973    }
2974
2975    #[tokio::test]
2976    async fn a_turn_that_finishes_is_returned_and_keeps_its_lease_beating() {
2977        let (_tmp, a, _b) = lease_store();
2978        let lease = a.claim_turn("t1").expect("claim").expect("held");
2979        let out = lease
2980            .beating_every(Duration::from_millis(5), async {
2981                tokio::time::sleep(Duration::from_millis(40)).await;
2982                7
2983            })
2984            .await
2985            .expect("still ours");
2986        assert_eq!(out, 7);
2987        assert!(a.turn_held("t1"));
2988    }
2989
2990    #[test]
2991    fn an_unreadable_lease_counts_as_stale() {
2992        let (_tmp, a, b) = lease_store();
2993        std::fs::create_dir_all(a.root()).expect("dir");
2994        std::fs::write(a.turn_path("t1"), "not json").expect("write");
2995        assert!(!a.turn_held("t1"));
2996        assert!(b.claim_turn("t1").expect("claim").is_some());
2997    }
2998
2999    #[test]
3000    fn without_hard_links_a_claim_is_still_exclusive_and_a_young_placeholder_blocks() {
3001        let (_tmp, a, b) = lease_store();
3002        failpoint::force_no_link(true);
3003        let held = a.claim_turn("t1").expect("claim").expect("first wins");
3004        assert!(b.claim_turn("t1").expect("claim").is_none());
3005        assert!(a.turn_held("t1"));
3006        drop(held);
3007        assert!(!a.turn_held("t1"));
3008        // A writer between its exclusive create and its write: an empty file.
3009        let path = a.turn_path("t1");
3010        assert!(create_exclusive(&path, "").expect("placeholder"));
3011        assert!(b.claim_turn("t1").expect("claim").is_none(), "young: held");
3012        age_file(&path);
3013        assert!(b.claim_turn("t1").expect("claim").is_some(), "old: stale");
3014        // The lock publishes through the same path.
3015        let lease = a.turn_path("t2");
3016        let lock = TurnLock::take(&lease).expect("take").expect("lock");
3017        assert!(TurnLock::take(&lease).expect("take").is_none());
3018        drop(lock);
3019        assert!(TurnLock::take(&lease).expect("take").is_some());
3020        failpoint::force_no_link(false);
3021    }
3022
3023    #[test]
3024    fn remove_if_carries_removes_only_the_expected_content() {
3025        let (_tmp, a, _b) = lease_store();
3026        std::fs::create_dir_all(a.root()).expect("dir");
3027        let p = a.root().join("x.turn.lock");
3028        std::fs::write(&p, "mine").expect("write");
3029        assert!(!remove_if_carries(&p, "other"));
3030        assert_eq!(std::fs::read_to_string(&p).expect("kept"), "mine");
3031        assert!(remove_if_carries(&p, "mine"));
3032        assert!(!p.exists());
3033        assert!(!remove_if_carries(&p, "mine"), "absent is not a removal");
3034    }
3035
3036    #[test]
3037    fn a_dropped_lock_does_not_remove_a_lock_taken_over_since() {
3038        let (_tmp, a, _b) = lease_store();
3039        std::fs::create_dir_all(a.root()).expect("dir");
3040        let lease = a.turn_path("t1");
3041        let lock = TurnLock::take(&lease).expect("take").expect("lock");
3042        let path = lock.path.clone();
3043        std::fs::write(&path, "someone-else").expect("replace");
3044        drop(lock);
3045        assert_eq!(
3046            std::fs::read_to_string(&path).expect("kept"),
3047            "someone-else"
3048        );
3049    }
3050
3051    #[test]
3052    fn concurrent_takeovers_of_a_stale_lease_have_one_winner() {
3053        let (_tmp, a, _b) = lease_store();
3054        drop(a.claim_turn("t1").expect("claim").expect("held"));
3055        std::fs::write(
3056            a.turn_path("t1"),
3057            serde_json::to_string(&TurnRecord {
3058                token: "gone".into(),
3059                pid: 1,
3060                beat_at: Timestamp::from_second(1).expect("ts"),
3061            })
3062            .expect("json"),
3063        )
3064        .expect("write");
3065        let wins: Vec<_> = std::thread::scope(|sc| {
3066            let hs: Vec<_> = (0..8)
3067                .map(|_| {
3068                    let s = a.clone();
3069                    sc.spawn(move || s.claim_turn("t1").expect("claim"))
3070                })
3071                .collect();
3072            hs.into_iter().map(|h| h.join().expect("join")).collect()
3073        });
3074        assert_eq!(wins.iter().filter(|w| w.is_some()).count(), 1);
3075    }
3076
3077    fn age_file(path: &Path) {
3078        let f = std::fs::OpenOptions::new()
3079            .write(true)
3080            .open(path)
3081            .expect("open");
3082        f.set_modified(std::time::SystemTime::now() - std::time::Duration::from_secs(60))
3083            .expect("age");
3084    }
3085
3086    #[test]
3087    fn a_late_taker_of_a_broken_lock_cannot_disturb_its_replacement() {
3088        let (_tmp, a, _b) = lease_store();
3089        let lease = a.turn_path("t1");
3090        let lock = lease.with_extension("turn.lock");
3091        std::fs::create_dir_all(lock.parent().expect("dir")).expect("dir");
3092        std::fs::write(&lock, "t1-dead").expect("dead lock");
3093        age_file(&lock);
3094        // B breaks the dead lock and holds a fresh one.
3095        let b = TurnLock::take(&lease).expect("take").expect("b wins");
3096        let fresh = std::fs::read_to_string(&lock).expect("read");
3097        assert_eq!(fresh, b.token);
3098        // C read the same dead token earlier: its ticket is already spent, and
3099        // the fresh lock is never moved, so a newcomer cannot slip in.
3100        let ticket = std::fs::read_dir(lock.parent().expect("dir"))
3101            .expect("dir")
3102            .flatten()
3103            .map(|e| e.path())
3104            .find(|p| p.to_string_lossy().ends_with(".break.0"))
3105            .expect("ticket");
3106        assert!(!create_exclusive(&ticket, "").expect("ticket"));
3107        assert!(TurnLock::take(&lease).expect("take").is_none());
3108        assert_eq!(std::fs::read_to_string(&lock).expect("read"), fresh);
3109        // A later generation is breakable once the old ticket is stale too.
3110        age_file(&lock);
3111        age_file(&ticket);
3112        std::mem::forget(b);
3113        let c = TurnLock::take(&lease)
3114            .expect("take")
3115            .expect("next generation");
3116        assert_ne!(c.token, fresh);
3117    }
3118
3119    #[test]
3120    fn a_live_ticket_blocks_and_a_stale_one_hands_over_to_the_next_generation() {
3121        let (_tmp, a, _b) = lease_store();
3122        let lease = a.turn_path("t1");
3123        let lock = lease.with_extension("turn.lock");
3124        std::fs::create_dir_all(lock.parent().expect("dir")).expect("dir");
3125        std::fs::write(&lock, "t1-dead").expect("dead lock");
3126        age_file(&lock);
3127        let t0 = lock.with_extension("lock.t1-dead.break.0");
3128        assert!(create_exclusive(&t0, "").expect("ticket"));
3129        // A fresh ticket 0 blocks the same dead token, whatever the clock says.
3130        assert!(TurnLock::take(&lease).expect("take").is_none());
3131        assert_eq!(std::fs::read_to_string(&lock).expect("read"), "t1-dead");
3132        // Once ticket 0 is stale, exactly one taker gets through via ticket 1.
3133        age_file(&t0);
3134        let c = TurnLock::take(&lease).expect("take").expect("generation 1");
3135        assert_eq!(std::fs::read_to_string(&lock).expect("read"), c.token);
3136        assert!(lock.with_extension("lock.t1-dead.break.1").exists());
3137        assert!(TurnLock::take(&lease).expect("take").is_none());
3138    }
3139
3140    #[test]
3141    fn exhausted_ticket_generations_recover_once_the_sweep_ages_them_out() {
3142        let (_tmp, a, _b) = lease_store();
3143        let lease = a.turn_path("t1");
3144        let lock = lease.with_extension("turn.lock");
3145        std::fs::create_dir_all(lock.parent().expect("dir")).expect("dir");
3146        std::fs::write(&lock, "t1-dead").expect("dead lock");
3147        age_file(&lock);
3148        let tickets: Vec<_> = (0..TICKET_GENERATIONS)
3149            .map(|n| lock.with_extension(format!("lock.t1-dead.break.{n}")))
3150            .collect();
3151        for t in &tickets {
3152            assert!(create_exclusive(t, "").expect("ticket"));
3153            age_file(t);
3154        }
3155        // Stale but not yet swept: no recovery, and no ticket is unlinked.
3156        assert!(TurnLock::take(&lease).expect("take").is_none());
3157        assert!(tickets.iter().all(|t| t.exists()));
3158        // Past the sweep age they go, and the next attempt recovers.
3159        for t in &tickets {
3160            let f = std::fs::OpenOptions::new()
3161                .write(true)
3162                .open(t)
3163                .expect("open");
3164            f.set_modified(std::time::SystemTime::now() - TICKET_SWEEP_AGE * 2)
3165                .expect("age");
3166        }
3167        assert!(TurnLock::take(&lease).expect("take").is_none());
3168        assert!(TurnLock::take(&lease).expect("take").is_some());
3169    }
3170
3171    #[test]
3172    fn an_aged_empty_lock_is_broken() {
3173        let (_tmp, a, _b) = lease_store();
3174        let lease = a.turn_path("t1");
3175        let lock = lease.with_extension("turn.lock");
3176        std::fs::create_dir_all(lock.parent().expect("dir")).expect("dir");
3177        std::fs::write(&lock, "").expect("empty lock");
3178        age_file(&lock);
3179        assert!(TurnLock::take(&lease).expect("take").is_some());
3180    }
3181
3182    #[test]
3183    fn queued_text_is_durable_combined_and_drained_as_one_operator_turn() {
3184        let (tmp, talks) = store();
3185        let cfg = config(mock_agent(tmp.path(), REPLY, env("reply")));
3186        let mut talk = begin(&talks, &cfg, tmp.path().to_owned(), None).expect("begin");
3187
3188        queue(&mut talk, &talks, "first", Vec::new()).expect("queue first");
3189        queue(&mut talk, &talks, "second", Vec::new()).expect("queue second");
3190        let saved = talks.get(&talk.id).expect("reload queued talk");
3191        assert_eq!(saved.pending, "first\n\nsecond");
3192        assert_eq!(saved.pending_breaks, Some(vec!["first\n\n".len()]));
3193        assert!(saved.turns.is_empty(), "a draft is not a transcript turn");
3194
3195        let drained = drain(&mut talk, &talks).expect("drain");
3196        assert_eq!(drained.as_deref(), Some("first\n\nsecond"));
3197        let saved = talks.get(&talk.id).expect("reload drained talk");
3198        assert!(saved.pending.is_empty());
3199        assert_eq!(saved.turns.len(), 1);
3200        assert_eq!(saved.turns[0].body, "first\n\nsecond");
3201        assert_eq!(saved.turns[0].breaks, Some(vec!["first\n\n".len()]));
3202        assert_eq!(saved.pending_breaks, None);
3203    }
3204
3205    #[test]
3206    fn editing_a_queued_draft_preserves_its_attachments_and_rejects_a_stale_snapshot() {
3207        let (tmp, talks) = store();
3208        let cfg = config(mock_agent(tmp.path(), REPLY, env("reply")));
3209        let mut talk = begin(&talks, &cfg, tmp.path().to_owned(), None).expect("begin");
3210        let attachment = Attachment {
3211            id: "a".repeat(32),
3212            name: "shot.png".to_owned(),
3213            mime: "image/png".to_owned(),
3214            bytes: 3,
3215        };
3216
3217        queue(&mut talk, &talks, "first", vec![attachment.clone()]).expect("queue");
3218        assert!(
3219            edit_pending_text(
3220                &mut talk,
3221                &talks,
3222                "corrected",
3223                "first",
3224                std::slice::from_ref(&attachment.id),
3225            )
3226            .expect("edit")
3227        );
3228        let saved = talks.get(&talk.id).expect("reload edited draft");
3229        assert_eq!(saved.pending, "corrected");
3230        assert_eq!(saved.pending_attachments, vec![attachment]);
3231
3232        queue(&mut talk, &talks, "later", Vec::new()).expect("queue concurrent draft");
3233        assert!(
3234            !edit_pending_text(
3235                &mut talk,
3236                &talks,
3237                "stale edit",
3238                "corrected",
3239                &["a".repeat(32)],
3240            )
3241            .expect("stale edit is a conflict")
3242        );
3243        assert_eq!(
3244            talks.get(&talk.id).expect("reload after conflict").pending,
3245            "corrected\n\nlater"
3246        );
3247        assert!(
3248            !clear_pending_if_matches(&mut talk, &talks, "corrected", &["a".repeat(32)])
3249                .expect("stale clear is a conflict")
3250        );
3251        assert_eq!(
3252            talks
3253                .get(&talk.id)
3254                .expect("reload after stale clear")
3255                .pending,
3256            "corrected\n\nlater"
3257        );
3258    }
3259
3260    #[tokio::test]
3261    async fn a_reply_save_preserves_pending_accepted_while_the_cli_runs() {
3262        let (tmp, talks) = store();
3263        let slow = "#!/bin/sh\ncat >/dev/null\nsleep 0.1\nprintf reply\n";
3264        let cfg = config(mock_agent(tmp.path(), slow, BTreeMap::new()));
3265        let mut running = begin(&talks, &cfg, tmp.path().to_owned(), None).expect("begin");
3266        let id = running.id.clone();
3267        let first = record(&mut running, &talks, "first", Vec::new()).expect("record");
3268
3269        let response_talks = talks.clone();
3270        let response_cfg = cfg.clone();
3271        let reply = tokio::spawn(async move {
3272            respond(
3273                &response_talks.claim_turn(&running.id).unwrap().unwrap(),
3274                &mut running,
3275                &response_talks,
3276                &response_cfg,
3277                &first,
3278            )
3279            .await
3280        });
3281        tokio::time::sleep(std::time::Duration::from_millis(20)).await;
3282
3283        let mut queued = talks.get(&id).expect("queued handle");
3284        queue(&mut queued, &talks, "next", Vec::new()).expect("queue");
3285        reply.await.expect("join").expect("reply");
3286
3287        let saved = talks.get(&id).expect("reload");
3288        assert_eq!(saved.pending, "next");
3289        assert_eq!(saved.pending_breaks, Some(Vec::new()));
3290        assert_eq!(saved.turns.len(), 2, "operator message and reply remain");
3291    }
3292
3293    /// A mock whose script counts its own calls in `<dir>/<id>.calls` before
3294    /// running `body`.
3295    fn counting_agent(dir: &Path, id: &str, body: &str) -> AgentSpec {
3296        let calls = dir.join(format!("{id}.calls"));
3297        let script = format!(
3298            "#!/bin/sh\necho x >> '{}'\n{body}\n",
3299            calls.to_string_lossy()
3300        );
3301        let path = dir.join(format!("mock-{id}.sh"));
3302        std::fs::write(&path, script).expect("write mock");
3303        AgentSpec {
3304            id: id.to_owned(),
3305            kind: AgentKind::Command,
3306            model: None,
3307            command: vec!["sh".to_owned(), path.to_string_lossy().into_owned()],
3308            extra_args: Vec::new(),
3309            env: BTreeMap::new(),
3310            prompt_delivery: None,
3311        }
3312    }
3313
3314    fn calls(dir: &Path, id: &str) -> usize {
3315        std::fs::read_to_string(dir.join(format!("{id}.calls"))).map_or(0, |s| s.lines().count())
3316    }
3317
3318    fn chain_config(specs: Vec<AgentSpec>, ids: &[&str]) -> Config {
3319        let mut cfg = config(specs[0].clone());
3320        cfg.agents = specs;
3321        cfg.roles.chatter = Some(AgentChoice::Chain(
3322            ids.iter().map(|s| (*s).to_owned()).collect(),
3323        ));
3324        cfg
3325    }
3326
3327    #[tokio::test]
3328    async fn a_chatter_chain_falls_back_resends_the_transcript_and_sticks() {
3329        let (tmp, talks) = store();
3330        let a = counting_agent(tmp.path(), "a", "cat >/dev/null\nexit 3");
3331        let b = counting_agent(tmp.path(), "b", "cat");
3332        let cfg = chain_config(vec![a, b], &["a", "b"]);
3333        let mut talk = begin(&talks, &cfg, tmp.path().to_owned(), None).expect("begin");
3334        assert_eq!(talk.agent, "a");
3335
3336        say(
3337            &talks.claim_turn(&talk.id).unwrap().unwrap(),
3338            &mut talk,
3339            &talks,
3340            &cfg,
3341            "hello there",
3342            Vec::new(),
3343        )
3344        .await
3345        .expect("turn");
3346        assert_eq!(calls(tmp.path(), "a"), 1, "each id is tried once");
3347        assert_eq!(calls(tmp.path(), "b"), 1);
3348        assert_eq!(talk.agent, "b", "the switch persists");
3349        assert!(talks.get(&talk.id).unwrap().agent == "b");
3350        let reply = talk.turns.last().unwrap();
3351        assert!(reply.body.contains("hello there"));
3352        assert!(
3353            reply.body.contains("magi task add --solo"),
3354            "a fresh seat gets the full briefing"
3355        );
3356        assert!(
3357            talk.turns
3358                .iter()
3359                .any(|t| t.body.contains("agent changed from a to b")),
3360            "the switch is noted"
3361        );
3362    }
3363
3364    #[tokio::test]
3365    async fn an_exhausted_chatter_chain_fails_like_a_single_seat_and_stays_put() {
3366        let (tmp, talks) = store();
3367        let a = counting_agent(tmp.path(), "a", "cat >/dev/null\nexit 3");
3368        let b = counting_agent(tmp.path(), "b", "cat >/dev/null\nexit 4");
3369        let cfg = chain_config(vec![a, b], &["a", "b", "a"]);
3370        let mut talk = begin(&talks, &cfg, tmp.path().to_owned(), None).expect("begin");
3371
3372        let err = say(
3373            &talks.claim_turn(&talk.id).unwrap().unwrap(),
3374            &mut talk,
3375            &talks,
3376            &cfg,
3377            "hi",
3378            Vec::new(),
3379        )
3380        .await
3381        .expect_err("every agent failed");
3382        assert!(err.to_string().contains("`a`"), "{err:#}");
3383        assert_eq!(calls(tmp.path(), "a"), 1);
3384        assert_eq!(calls(tmp.path(), "b"), 1);
3385        assert_eq!(talk.agent, "a", "an exhausted chain leaves the agent alone");
3386    }
3387
3388    #[test]
3389    fn a_chatter_chain_skips_an_unknown_id_at_begin() {
3390        let (tmp, talks) = store();
3391        let b = counting_agent(tmp.path(), "b", "cat");
3392        let cfg = chain_config(vec![b], &["ghost", "b"]);
3393        let talk = begin(&talks, &cfg, tmp.path().to_owned(), None).expect("begin");
3394        assert_eq!(talk.agent, "b");
3395    }
3396
3397    #[tokio::test]
3398    async fn an_explicit_agent_inside_the_chatter_chain_stays_pinned() {
3399        let (tmp, talks) = store();
3400        let a = counting_agent(tmp.path(), "a", "cat >/dev/null\nexit 3");
3401        let b = counting_agent(tmp.path(), "b", "cat");
3402        let cfg = chain_config(vec![a, b], &["a", "b"]);
3403        let mut talk = begin(&talks, &cfg, tmp.path().to_owned(), Some("a")).expect("begin");
3404        say(
3405            &talks.claim_turn(&talk.id).unwrap().unwrap(),
3406            &mut talk,
3407            &talks,
3408            &cfg,
3409            "hi",
3410            Vec::new(),
3411        )
3412        .await
3413        .expect_err("a alone, and it fails");
3414        assert_eq!(calls(tmp.path(), "b"), 0);
3415        assert_eq!(talk.agent, "a");
3416    }
3417
3418    #[tokio::test]
3419    async fn an_explicit_agent_does_not_borrow_the_chatter_chain() {
3420        let (tmp, talks) = store();
3421        let a = counting_agent(tmp.path(), "a", "cat >/dev/null\nexit 3");
3422        let b = counting_agent(tmp.path(), "b", "cat");
3423        let c = counting_agent(tmp.path(), "c", "cat >/dev/null\nexit 3");
3424        let cfg = chain_config(vec![a, b, c], &["a", "b"]);
3425        let mut talk = begin(&talks, &cfg, tmp.path().to_owned(), Some("c")).expect("begin");
3426        say(
3427            &talks.claim_turn(&talk.id).unwrap().unwrap(),
3428            &mut talk,
3429            &talks,
3430            &cfg,
3431            "hi",
3432            Vec::new(),
3433        )
3434        .await
3435        .expect_err("c alone, and it fails");
3436        assert_eq!(calls(tmp.path(), "b"), 0);
3437    }
3438
3439    #[test]
3440    fn briefing_names_the_operator_with_or_without_a_persona() {
3441        let plain = briefing(Path::new("/repo"), "en", false);
3442        let named = briefing_with(Path::new("/repo"), "en", false, None, Some("Commander"));
3443        assert!(named.starts_with(&plain));
3444        assert!(named.contains("# Addressing the operator"));
3445        assert!(named.contains("\"Commander\""));
3446        let rei = crate::persona::builtin_catalog()
3447            .into_iter()
3448            .find(|p| p.id == "rei")
3449            .expect("rei");
3450        let with = briefing_with(
3451            Path::new("/repo"),
3452            "en",
3453            false,
3454            Some(&rei),
3455            Some("Commander"),
3456        );
3457        assert!(with.contains("\"Commander\""));
3458        assert!(!with.contains("# Addressing the operator"));
3459    }
3460
3461    #[test]
3462    fn briefing_carries_a_persona_section_only_when_one_is_chosen() {
3463        let plain = briefing(Path::new("/repo"), "en", false);
3464        assert_eq!(
3465            plain,
3466            briefing_with(Path::new("/repo"), "en", false, None, None)
3467        );
3468        assert!(!plain.contains("Persona"));
3469        let rei = crate::persona::builtin_catalog()
3470            .into_iter()
3471            .find(|p| p.id == "rei")
3472            .expect("rei");
3473        let with = briefing_with(Path::new("/repo"), "en", false, Some(&rei), None);
3474        assert!(with.starts_with(&plain), "the plain briefing is untouched");
3475        assert!(with.contains("# Persona (tone only)"));
3476        assert!(with.contains("TONE ONLY"));
3477        assert!(with.contains("task ids"));
3478        assert!(with.contains("write policy"));
3479        assert!(with.contains("`magi task add`"));
3480        assert!(with.contains("Rei Ayanami"));
3481    }
3482
3483    #[test]
3484    fn a_talk_written_before_personas_still_loads() {
3485        let (tmp, talks) = store();
3486        let spec = mock_agent(tmp.path(), ECHO, BTreeMap::new());
3487        let cfg = config(spec);
3488        let talk = begin(&talks, &cfg, tmp.path().to_owned(), None).expect("begin");
3489        let path = talks.root.join(format!("{}.json", talk.id));
3490        let mut v: serde_json::Value =
3491            serde_json::from_str(&std::fs::read_to_string(&path).unwrap()).unwrap();
3492        v.as_object_mut().unwrap().remove("persona");
3493        v.as_object_mut().unwrap().remove("persona_dirty");
3494        std::fs::write(&path, v.to_string()).unwrap();
3495        let loaded = talks.get(&talk.id).expect("old record loads");
3496        assert_eq!(loaded.persona, "");
3497        assert!(!loaded.persona_dirty);
3498    }
3499
3500    #[test]
3501    fn the_briefing_files_with_solo_or_the_chosen_implementer_count() {
3502        let repo = Path::new("/repo");
3503        let solo = briefing_for(repo, "", false, None, None, 1);
3504        assert_eq!(solo, briefing(repo, "", false));
3505        assert!(solo.contains("magi task add --solo --repo"));
3506        assert!(solo.contains("magi task add --solo --attach"));
3507        for n in [2u8, 3] {
3508            let b = briefing_for(repo, "", false, None, None, n);
3509            assert!(
3510                b.contains(&format!("magi task add --implementers {n} --repo")),
3511                "{b}"
3512            );
3513            assert!(b.contains(&format!("magi task add --implementers {n} --attach")));
3514            assert!(!b.contains("task add --solo"), "{b}");
3515            assert!(!b.contains("Use --solo"), "{b}");
3516        }
3517    }
3518
3519    #[test]
3520    fn begin_with_applies_remembered_choices_and_drops_unfit_ones_alone() {
3521        let (tmp, talks) = store();
3522        let first = mock_agent(tmp.path(), ECHO, BTreeMap::new());
3523        let mut second = first.clone();
3524        second.id = "second".to_owned();
3525        let mut cfg = config(first.clone());
3526        cfg.agents.push(second.clone());
3527        let open = |p: &Preferred| {
3528            begin_with(&talks, &cfg, tmp.path().to_owned(), None, p).expect("begin")
3529        };
3530
3531        let talk = open(&Preferred {
3532            agent: Some("second".into()),
3533            persona: Some("rei".into()),
3534            implementers: Some(i64::from(check_implementers(3, &cfg).unwrap_or(1))),
3535        });
3536        assert_eq!(talk.agent, "second");
3537        assert!(
3538            !talk.fallback,
3539            "a remembered non-head agent is an explicit choice"
3540        );
3541        assert_eq!(talk.persona, "rei");
3542        assert_eq!(talk.implementers, check_implementers(3, &cfg).unwrap_or(1));
3543        assert!(!talk.persona_dirty && !talk.implementers_dirty);
3544
3545        // The chain head chosen again stays a fallback-capable talk.
3546        let head = open(&Preferred {
3547            agent: Some(first.id.clone()),
3548            ..Preferred::default()
3549        });
3550        assert!(head.fallback);
3551
3552        // Each unfit field falls back by itself.
3553        for implementers in [0, 4, -1, 1000] {
3554            let t = open(&Preferred {
3555                agent: Some("gone".into()),
3556                persona: Some("no-such-persona".into()),
3557                implementers: Some(implementers),
3558            });
3559            assert_eq!(t.agent, first.id);
3560            assert!(t.fallback);
3561            assert_eq!(t.persona, "");
3562            assert_eq!(t.implementers, 1);
3563        }
3564
3565        // `default` stays the empty string; a good field survives a bad one.
3566        let t = open(&Preferred {
3567            agent: Some("second".into()),
3568            persona: Some("default".into()),
3569            implementers: Some(9),
3570        });
3571        assert_eq!(
3572            (t.agent.as_str(), t.persona.as_str(), t.implementers),
3573            ("second", "", 1)
3574        );
3575
3576        // An explicit agent still wins and keeps its strict error.
3577        let strict = begin_with(
3578            &talks,
3579            &cfg,
3580            tmp.path().to_owned(),
3581            Some("first-unknown"),
3582            &Preferred {
3583                agent: Some("second".into()),
3584                ..Preferred::default()
3585            },
3586        );
3587        assert!(strict.is_err());
3588    }
3589
3590    #[test]
3591    fn remembered_agent_can_open_when_the_chatter_chain_cannot_resolve() {
3592        let (tmp, talks) = store();
3593        let spec = mock_agent(tmp.path(), ECHO, BTreeMap::new());
3594        // A missing chain member makes head resolution fail independently of
3595        // which subscription CLIs happen to be installed on this machine.
3596        let cfg = chain_config(vec![spec.clone()], &["missing-chatter"]);
3597        assert!(begin(&talks, &cfg, tmp.path().to_owned(), None).is_err());
3598        let talk = begin_with(
3599            &talks,
3600            &cfg,
3601            tmp.path().to_owned(),
3602            None,
3603            &Preferred {
3604                agent: Some(spec.id.clone()),
3605                ..Preferred::default()
3606            },
3607        )
3608        .expect("a valid remembered agent does not require a default chain");
3609        assert_eq!(talk.agent, spec.id);
3610        assert!(!talk.fallback);
3611        assert_eq!(talks.get(&talk.id).unwrap().agent, spec.id);
3612        assert!(
3613            begin_with(
3614                &talks,
3615                &cfg,
3616                tmp.path().to_owned(),
3617                None,
3618                &Preferred {
3619                    agent: Some("missing-preference".into()),
3620                    ..Preferred::default()
3621                },
3622            )
3623            .is_err()
3624        );
3625    }
3626
3627    #[test]
3628    fn implementers_are_validated_and_old_records_load_as_solo() {
3629        let (tmp, talks) = store();
3630        let cfg = config(mock_agent(tmp.path(), ECHO, BTreeMap::new()));
3631        assert_eq!(check_implementers(1, &cfg), Ok(1));
3632        assert_eq!(check_implementers(3, &cfg), Ok(3));
3633        assert!(check_implementers(0, &cfg).is_err());
3634        assert!(check_implementers(4, &cfg).is_err());
3635        let mut empty = cfg.clone();
3636        empty.agents.clear();
3637        assert!(check_implementers(2, &empty).is_err());
3638
3639        let talk = begin(&talks, &cfg, tmp.path().to_owned(), None).expect("begin");
3640        let path = talks.path_of(&talk.id);
3641        let mut v: serde_json::Value =
3642            serde_json::from_str(&std::fs::read_to_string(&path).unwrap()).unwrap();
3643        v.as_object_mut().unwrap().remove("implementers");
3644        v.as_object_mut().unwrap().remove("implementers_dirty");
3645        std::fs::write(&path, v.to_string()).unwrap();
3646        let loaded = talks.get(&talk.id).expect("load");
3647        assert_eq!(loaded.implementers, 1);
3648        assert!(!loaded.implementers_dirty);
3649    }
3650
3651    #[tokio::test]
3652    async fn an_implementers_switch_is_noted_once_and_kept_across_a_failed_turn() {
3653        let (tmp, talks) = store();
3654        let spec = mock_agent(tmp.path(), ECHO, BTreeMap::new());
3655        let cfg = config(spec);
3656        let mut talk = begin(&talks, &cfg, tmp.path().to_owned(), None).expect("begin");
3657        let lease = talks.claim_turn(&talk.id).unwrap().unwrap();
3658        say(&lease, &mut talk, &talks, &cfg, "hello", Vec::new())
3659            .await
3660            .expect("first turn");
3661
3662        assert!(switch_implementers(&mut talk, &talks, 2).expect("switch"));
3663        assert!(!switch_implementers(&mut talk, &talks, 2).expect("same"));
3664        assert!(talk.implementers_dirty);
3665        say(&lease, &mut talk, &talks, &cfg, "next", Vec::new())
3666            .await
3667            .expect("turn");
3668        let prompt = &talk.turns.last().unwrap().body;
3669        assert!(prompt.contains("# Task filing update"), "{prompt}");
3670        assert!(prompt.contains("--implementers 2"));
3671        assert!(!talk.implementers_dirty, "cleared after a successful turn");
3672
3673        say(&lease, &mut talk, &talks, &cfg, "again", Vec::new())
3674            .await
3675            .expect("turn");
3676        assert!(
3677            !talk
3678                .turns
3679                .last()
3680                .unwrap()
3681                .body
3682                .contains("# Task filing update")
3683        );
3684
3685        // Without a resumable session the current policy rides on every turn.
3686        let mut no_sessions = cfg.clone();
3687        no_sessions.graph.sessions = false;
3688        say(&lease, &mut talk, &talks, &no_sessions, "one", Vec::new())
3689            .await
3690            .expect("turn");
3691        say(&lease, &mut talk, &talks, &no_sessions, "two", Vec::new())
3692            .await
3693            .expect("turn");
3694        assert!(talk.turns.last().unwrap().body.contains("--implementers 2"));
3695    }
3696
3697    #[tokio::test]
3698    async fn a_persona_switch_notes_marks_dirty_and_updates_the_next_turn_once() {
3699        let (tmp, talks) = store();
3700        let spec = mock_agent(tmp.path(), ECHO, BTreeMap::new());
3701        let cfg = config(spec);
3702        let mut talk = begin(&talks, &cfg, tmp.path().to_owned(), None).expect("begin");
3703        let lease = talks.claim_turn(&talk.id).unwrap().unwrap();
3704        say(&lease, &mut talk, &talks, &cfg, "hello", Vec::new())
3705            .await
3706            .expect("first turn");
3707        assert!(!talk.turns[1].body.contains("Persona"));
3708
3709        assert!(switch_persona(&mut talk, &talks, "misato").expect("switch"));
3710        assert!(!switch_persona(&mut talk, &talks, "misato").expect("same"));
3711        assert!(talk.persona_dirty);
3712        assert!(talk.turns.last().unwrap().body.contains("persona changed"));
3713
3714        say(&lease, &mut talk, &talks, &cfg, "next", Vec::new())
3715            .await
3716            .expect("turn");
3717        let prompt = &talk.turns.last().unwrap().body;
3718        assert!(prompt.contains("# Persona update"), "{prompt}");
3719        assert!(prompt.contains("Misato Katsuragi"));
3720        assert!(!talk.persona_dirty, "cleared after a successful turn");
3721
3722        say(&lease, &mut talk, &talks, &cfg, "again", Vec::new())
3723            .await
3724            .expect("turn");
3725        assert!(!talk.turns.last().unwrap().body.contains("# Persona update"));
3726
3727        assert!(switch_persona(&mut talk, &talks, "default").expect("back"));
3728        assert_eq!(talk.persona, "");
3729        say(&lease, &mut talk, &talks, &cfg, "plain", Vec::new())
3730            .await
3731            .expect("turn");
3732        assert!(
3733            talk.turns
3734                .last()
3735                .unwrap()
3736                .body
3737                .contains("turned the persona off")
3738        );
3739
3740        // Without a resumable session every turn re-sends the persona.
3741        assert!(switch_persona(&mut talk, &talks, "rei").expect("rei"));
3742        let mut no_sessions = cfg.clone();
3743        no_sessions.graph.sessions = false;
3744        say(&lease, &mut talk, &talks, &no_sessions, "one", Vec::new())
3745            .await
3746            .expect("turn");
3747        say(&lease, &mut talk, &talks, &no_sessions, "two", Vec::new())
3748            .await
3749            .expect("turn");
3750        let last = &talk.turns.last().unwrap().body;
3751        assert!(!talk.persona_dirty);
3752        assert!(last.contains("# Persona (tone only)"), "{last}");
3753        assert!(last.contains("Rei Ayanami"));
3754    }
3755
3756    #[tokio::test]
3757    async fn the_first_turn_carries_the_briefing_and_later_turns_do_not() {
3758        let (tmp, talks) = store();
3759        let spec = mock_agent(tmp.path(), ECHO, BTreeMap::new());
3760        let cfg = config(spec);
3761        let mut talk = begin(&talks, &cfg, tmp.path().to_owned(), None).expect("begin");
3762
3763        say(
3764            &talks.claim_turn(&talk.id).unwrap().unwrap(),
3765            &mut talk,
3766            &talks,
3767            &cfg,
3768            "what does the queue module do?",
3769            Vec::new(),
3770        )
3771        .await
3772        .expect("first turn");
3773        let first_prompt = &talk.turns[1].body;
3774        assert!(first_prompt.contains("magi task add --solo"));
3775        assert!(first_prompt.contains("what does the queue module do?"));
3776
3777        say(
3778            &talks.claim_turn(&talk.id).unwrap().unwrap(),
3779            &mut talk,
3780            &talks,
3781            &cfg,
3782            "and how is it locked?",
3783            Vec::new(),
3784        )
3785        .await
3786        .expect("second turn");
3787        let second_prompt = &talk.turns[3].body;
3788        assert!(
3789            !second_prompt.contains("magi task add --solo"),
3790            "the briefing is sent once, not on every turn: {second_prompt}"
3791        );
3792        assert!(second_prompt.contains("and how is it locked?"));
3793    }
3794
3795    #[tokio::test]
3796    async fn switching_agent_resets_the_seat_notes_it_and_resends_the_transcript() {
3797        let (tmp, talks) = store();
3798        let a = mock_agent(tmp.path(), ECHO, BTreeMap::new());
3799        let mut b = a.clone();
3800        b.id = "other".to_owned();
3801        let mut cfg = config(a.clone());
3802        cfg.agents.push(b.clone());
3803        let mut talk = begin(&talks, &cfg, tmp.path().to_owned(), Some(&a.id)).expect("begin");
3804        say(
3805            &talks.claim_turn(&talk.id).unwrap().unwrap(),
3806            &mut talk,
3807            &talks,
3808            &cfg,
3809            "remember the walrus",
3810            Vec::new(),
3811        )
3812        .await
3813        .expect("first turn");
3814        let old_session = talk.seat.claude_session.clone();
3815        assert_eq!(talk.seat.turns, 1);
3816
3817        assert!(switch_agent(&mut talk, &talks, &b).expect("switch"));
3818        assert_eq!(talk.agent, "other");
3819        assert_eq!(talk.seat.turns, 0);
3820        assert_eq!(talk.seat.agent, "other");
3821        assert_ne!(talk.seat.claude_session, old_session);
3822        let note = talk.turns.last().expect("note");
3823        assert_eq!(note.who, Who::Agent);
3824        assert!(note.body.starts_with(MAGI_NOTE), "{}", note.body);
3825        assert!(note.body.contains("changed from"), "{}", note.body);
3826        assert_eq!(talks.get(&talk.id).expect("reload").agent, "other");
3827
3828        let before = talk.turns.len();
3829        assert!(!switch_agent(&mut talk, &talks, &b).expect("same agent"));
3830        assert_eq!(talk.turns.len(), before, "a no-op writes no note");
3831
3832        say(
3833            &talks.claim_turn(&talk.id).unwrap().unwrap(),
3834            &mut talk,
3835            &talks,
3836            &cfg,
3837            "what did I say?",
3838            Vec::new(),
3839        )
3840        .await
3841        .expect("turn after switch");
3842        let prompt = &talk.turns.last().expect("reply").body;
3843        assert!(prompt.contains("remember the walrus"), "{prompt}");
3844        assert!(prompt.contains("## magi"), "{prompt}");
3845        assert!(prompt.contains("what did I say?"), "{prompt}");
3846    }
3847
3848    #[tokio::test]
3849    async fn say_appends_the_operator_turn_then_the_agent_turn() {
3850        let (tmp, talks) = store();
3851        let spec = mock_agent(tmp.path(), REPLY, env("go ahead"));
3852        let cfg = config(spec);
3853        let mut talk = begin(&talks, &cfg, tmp.path().to_owned(), None).expect("begin");
3854
3855        say(
3856            &talks.claim_turn(&talk.id).unwrap().unwrap(),
3857            &mut talk,
3858            &talks,
3859            &cfg,
3860            "can I rename this function?",
3861            Vec::new(),
3862        )
3863        .await
3864        .expect("say");
3865
3866        assert_eq!(talk.turns.len(), 2);
3867        assert_eq!(talk.turns[0].who, Who::Operator);
3868        assert_eq!(talk.turns[0].body, "can I rename this function?");
3869        assert_eq!(talk.turns[1].who, Who::Agent);
3870        assert_eq!(talk.turns[1].body, "go ahead");
3871        assert_eq!(talks.get(&talk.id).expect("get").turns, talk.turns);
3872    }
3873
3874    #[tokio::test]
3875    async fn a_failed_turn_keeps_the_operator_message_and_says_what_happened() {
3876        let (tmp, talks) = store();
3877        let spec = mock_agent(tmp.path(), BROKEN, BTreeMap::new());
3878        let cfg = config(spec);
3879        let mut talk = begin(&talks, &cfg, tmp.path().to_owned(), None).expect("begin");
3880
3881        let err = say(
3882            &talks.claim_turn(&talk.id).unwrap().unwrap(),
3883            &mut talk,
3884            &talks,
3885            &cfg,
3886            "check the tests",
3887            Vec::new(),
3888        )
3889        .await
3890        .expect_err("a turn with no answer is an error");
3891        assert!(err.to_string().contains("no answer"), "{err}");
3892
3893        let on_disk = talks.get(&talk.id).expect("get");
3894        assert_eq!(on_disk.turns.len(), 2);
3895        assert_eq!(on_disk.turns[0].body, "check the tests");
3896        let note = &on_disk.turns[1];
3897        assert_eq!(note.who, Who::Agent);
3898        assert!(note.body.starts_with(MAGI_NOTE), "{}", note.body);
3899        assert!(note.body.contains("your message is saved"));
3900    }
3901
3902    /// The failure this stands in for: a reader elsewhere briefly has the
3903    /// talk file open right when `turn` tries to save the reply, and the
3904    /// write-then-rename fails once or twice before the reader lets go.
3905    /// `write_atomic`'s own retries must absorb that with nobody the wiser -
3906    /// no gap in the transcript, no dropped turn.
3907    #[tokio::test]
3908    async fn a_passing_write_failure_while_saving_the_reply_does_not_lose_it() {
3909        let (tmp, talks) = store();
3910        let spec = mock_agent(tmp.path(), REPLY, env("go ahead"));
3911        let cfg = config(spec);
3912        let mut talk = begin(&talks, &cfg, tmp.path().to_owned(), None).expect("begin");
3913
3914        let text =
3915            record(&mut talk, &talks, "can I rename this function?", Vec::new()).expect("record");
3916        // One fewer failure than `write_atomic` will retry through, so the
3917        // very last attempt must succeed.
3918        failpoint::force_put_failures(PUT_RETRIES - 1);
3919        respond(
3920            &talks.claim_turn(&talk.id).unwrap().unwrap(),
3921            &mut talk,
3922            &talks,
3923            &cfg,
3924            &text,
3925        )
3926        .await
3927        .expect("respond must survive a write failure its own retries can outlast");
3928
3929        assert_eq!(talk.turns.len(), 2);
3930        assert_eq!(talk.turns[1].who, Who::Agent);
3931        assert_eq!(talk.turns[1].body, "go ahead");
3932        let on_disk = talks.get(&talk.id).expect("get");
3933        assert_eq!(
3934            on_disk.turns, talk.turns,
3935            "the reply must reach disk despite the early write failures"
3936        );
3937    }
3938
3939    /// When the write-then-rename never recovers - standing in for a disk
3940    /// that stays unwritable rather than a reader that eventually lets go -
3941    /// the reply must not disappear without a trace the way it did in the
3942    /// real incident this repository saw: no error on the phone, no note in
3943    /// the transcript, and the turn simply gone from `talks/<id>.json`.
3944    #[tokio::test]
3945    async fn a_persistent_write_failure_while_saving_the_reply_is_never_silent() {
3946        let (tmp, talks) = store();
3947        let spec = mock_agent(tmp.path(), REPLY, env("go ahead"));
3948        let cfg = config(spec);
3949        let mut talk = begin(&talks, &cfg, tmp.path().to_owned(), None).expect("begin");
3950
3951        let text = record(&mut talk, &talks, "check the tests", Vec::new()).expect("record");
3952        // Exactly enough forced failures to exhaust the reply's own retries;
3953        // the shorter note that replaces it then saves cleanly, which is the
3954        // common case this exercises - a large write racing something,
3955        // followed by a small one that does not.
3956        failpoint::force_put_failures(PUT_RETRIES);
3957        let err = respond(
3958            &talks.claim_turn(&talk.id).unwrap().unwrap(),
3959            &mut talk,
3960            &talks,
3961            &cfg,
3962            &text,
3963        )
3964        .await
3965        .expect_err("a reply that cannot be saved must be reported, not swallowed");
3966        assert!(err.to_string().contains("could not be saved"), "{err}");
3967
3968        let on_disk = talks.get(&talk.id).expect("get");
3969        assert_eq!(
3970            on_disk.turns.len(),
3971            2,
3972            "the operator turn plus a visible note"
3973        );
3974        assert_eq!(on_disk.turns[0].body, "check the tests");
3975        let note = &on_disk.turns[1];
3976        assert_eq!(note.who, Who::Agent);
3977        assert!(note.body.starts_with(MAGI_NOTE), "{}", note.body);
3978        assert!(
3979            note.body.contains("could not be saved"),
3980            "the operator must be told the reply is missing, not left staring \
3981             at a gap with no explanation: {}",
3982            note.body
3983        );
3984        assert_eq!(
3985            talk.turns, on_disk.turns,
3986            "the in-memory talk must match what actually landed on disk"
3987        );
3988
3989        // The generated answer itself must still be recoverable, not merely
3990        // reported as lost.
3991        let artifacts = talks.artifacts_of(&talk.id);
3992        let stash = std::fs::read_dir(&artifacts)
3993            .expect("artifacts dir")
3994            .filter_map(|e| e.ok())
3995            .find(|e| e.file_name().to_string_lossy().ends_with("-lost.txt"))
3996            .expect("a stash file for the lost reply");
3997        let stashed = std::fs::read_to_string(stash.path()).expect("read stash");
3998        assert_eq!(stashed, "go ahead");
3999
4000        // Losing the reply must not also lose the seat. The CLI took a turn
4001        // and consumed this seat's session id; if the note's write left the
4002        // record claiming otherwise, the next turn would re-open a session
4003        // the CLI is already holding - the `20260907-011805-fb57` desync -
4004        // and would re-send the whole briefing besides. Both decisions read
4005        // the seat straight off disk (`agent::has_session` and `turn`'s own
4006        // `seat.turns == 0` branch), so this is the field that has to match.
4007        assert_eq!(
4008            on_disk.seat.turns, 1,
4009            "the note's write must carry the turn the CLI actually took"
4010        );
4011        assert_eq!(
4012            on_disk.seat.claude_session, talk.seat.claude_session,
4013            "the session id handed to the CLI must survive the failed reply"
4014        );
4015        assert_eq!(on_disk.seat.captured_session, talk.seat.captured_session);
4016        assert!(
4017            agent::has_session(AgentKind::Command, &on_disk.seat, cfg.graph.sessions),
4018            "the next turn must resume, not open the same session id twice"
4019        );
4020    }
4021
4022    /// Even the note can fail to save, if the disk stays unwritable for long
4023    /// enough. `respond` must still report the failure rather than pretend
4024    /// the turn succeeded, and must not leave the in-memory `talk` claiming
4025    /// a turn that never reached disk.
4026    #[tokio::test]
4027    async fn a_write_failure_that_also_loses_the_note_still_reports_it() {
4028        let (tmp, talks) = store();
4029        let spec = mock_agent(tmp.path(), REPLY, env("go ahead"));
4030        let cfg = config(spec);
4031        let mut talk = begin(&talks, &cfg, tmp.path().to_owned(), None).expect("begin");
4032
4033        let text = record(&mut talk, &talks, "check the tests", Vec::new()).expect("record");
4034        // Enough forced failures to exhaust the retries for both the reply
4035        // and the note that would have replaced it.
4036        failpoint::force_put_failures(PUT_RETRIES * 2);
4037        let err = respond(
4038            &talks.claim_turn(&talk.id).unwrap().unwrap(),
4039            &mut talk,
4040            &talks,
4041            &cfg,
4042            &text,
4043        )
4044        .await
4045        .expect_err("neither the reply nor the note could be saved");
4046        assert!(err.to_string().contains("could not be saved"), "{err}");
4047
4048        assert_eq!(talk.turns.len(), 1, "only the operator's own turn");
4049        let on_disk = talks.get(&talk.id).expect("get");
4050        assert_eq!(on_disk.turns.len(), 1);
4051
4052        // Nothing at all reached disk, so the seat could not either: the CLI
4053        // took a turn the record does not know about. That is pinned here as
4054        // the known cost of a file that cannot be written twice over, not as
4055        // something this branch could do better - the only way to record the
4056        // seat is the write that just failed. It is also the point where
4057        // this meets `20260907-011805-fb57`: a turn taken before some later
4058        // write lands would re-open a session id the CLI already holds. The
4059        // in-memory seat keeps the truth the CLI reported, which is why it is
4060        // not wound back to match.
4061        assert_eq!(
4062            on_disk.seat.turns, 0,
4063            "an unwritable file cannot record the turn the CLI took"
4064        );
4065        assert_eq!(
4066            talk.seat.turns, 1,
4067            "the in-memory seat still reports the turn the CLI actually took"
4068        );
4069        assert_eq!(
4070            on_disk.seat.claude_session, talk.seat.claude_session,
4071            "the session id was minted at `begin` and never changes here"
4072        );
4073    }
4074
4075    /// An attachment lets the operator send an otherwise-empty message, and
4076    /// its absolute path is what actually reaches the agent's prompt - here
4077    /// on the very first turn, where it has to share the briefing.
4078    #[tokio::test]
4079    async fn attachments_reach_the_prompt_and_an_empty_body_is_still_a_turn() {
4080        let (tmp, talks) = store();
4081        let spec = mock_agent(tmp.path(), ECHO, BTreeMap::new());
4082        let cfg = config(spec);
4083        let mut talk = begin(&talks, &cfg, tmp.path().to_owned(), None).expect("begin");
4084
4085        let att = talks
4086            .put_attachment(
4087                &talk.id,
4088                "image/png",
4089                "screenshot.png",
4090                b"pretend-png-bytes",
4091            )
4092            .expect("put attachment");
4093
4094        say(
4095            &talks.claim_turn(&talk.id).unwrap().unwrap(),
4096            &mut talk,
4097            &talks,
4098            &cfg,
4099            "",
4100            vec![att.clone()],
4101        )
4102        .await
4103        .expect("an empty body with an attachment is still a turn");
4104
4105        let operator_turn = &talk.turns[0];
4106        assert_eq!(operator_turn.who, Who::Operator);
4107        assert_eq!(operator_turn.body, "");
4108        assert_eq!(operator_turn.attachments, vec![att.clone()]);
4109
4110        let prompt = &talk.turns[1].body;
4111        let expected_path = talks
4112            .attachments_dir(&talk.id)
4113            .join(format!("{}.png", att.id));
4114        assert!(
4115            prompt.contains(&expected_path.display().to_string()),
4116            "the agent must be told the attachment's absolute path: {prompt}"
4117        );
4118        assert!(prompt.contains("image/png"), "and its mime: {prompt}");
4119    }
4120
4121    /// See `chat`'s test of the same name: `run::home()` returns a bare
4122    /// relative `PathBuf` verbatim when `MAGI_HOME` is set to a relative
4123    /// path, so a `Talks` store built on it has a relative `root` too. That
4124    /// is fine for this store's own I/O, which runs in this process against
4125    /// this process's cwd, but `attachment_path` hands its result to a
4126    /// *different* process invoked with `cwd: &talk.repo` - an uncorrected
4127    /// relative path would resolve against the repository instead of
4128    /// wherever the attachment actually landed.
4129    #[test]
4130    fn attachment_path_is_absolute_even_when_the_store_root_is_relative() {
4131        let talks = Talks::at(PathBuf::from("relative-talks-root-for-this-test"));
4132        let att = Attachment {
4133            id: "0".repeat(32),
4134            name: "shot.png".to_owned(),
4135            mime: "image/png".to_owned(),
4136            bytes: 3,
4137        };
4138        let path = talks
4139            .attachment_path("some-talk-id", &att)
4140            .expect("a supported mime always yields a path");
4141        assert!(
4142            path.is_absolute(),
4143            "must be absolute even off a relative store root: {}",
4144            path.display()
4145        );
4146    }
4147
4148    #[tokio::test]
4149    async fn a_turn_past_the_configured_talk_timeout_is_reported_with_that_timeout() {
4150        // `[graph] timeout_talk` must be the number this module actually
4151        // waits, not a leftover hardcoded fifteen minutes - so the mock
4152        // sleeps past a deliberately tiny override and the failure note is
4153        // checked against that same override, not the old default.
4154        let (tmp, talks) = store();
4155        let slow = mock_agent(
4156            tmp.path(),
4157            "#!/bin/sh\ncat >/dev/null\nsleep 2\n",
4158            BTreeMap::new(),
4159        );
4160        let mut cfg = config(slow);
4161        cfg.graph.timeout_talk = 1;
4162        let mut talk = begin(&talks, &cfg, tmp.path().to_owned(), None).expect("begin");
4163
4164        let err = say(
4165            &talks.claim_turn(&talk.id).unwrap().unwrap(),
4166            &mut talk,
4167            &talks,
4168            &cfg,
4169            "check the tests",
4170            Vec::new(),
4171        )
4172        .await
4173        .expect_err("a turn that never answers is an error");
4174        assert!(
4175            err.to_string().contains("did not answer within 1s"),
4176            "{err}"
4177        );
4178
4179        let on_disk = talks.get(&talk.id).expect("get");
4180        let note = on_disk.turns.last().expect("a note turn was recorded");
4181        assert!(
4182            note.body.contains("did not answer within 1s"),
4183            "the transcript must show the configured timeout: {}",
4184            note.body
4185        );
4186    }
4187
4188    #[test]
4189    fn closing_is_idempotent_and_a_closed_talk_takes_no_more_turns() {
4190        let (tmp, talks) = store();
4191        let spec = mock_agent(tmp.path(), REPLY, env("hi"));
4192        let cfg = config(spec);
4193        let mut talk = begin(&talks, &cfg, tmp.path().to_owned(), None).expect("begin");
4194
4195        close(&mut talk, &talks).expect("close");
4196        assert_eq!(talk.status, TalkStatus::Closed);
4197        close(&mut talk, &talks).expect("closing twice is not an error");
4198
4199        let err =
4200            record(&mut talk, &talks, "still there?", Vec::new()).expect_err("closed talks refuse");
4201        assert!(err.to_string().contains("closed"));
4202        let _ = &cfg; // config kept only to build the agent above
4203    }
4204
4205    #[tokio::test]
4206    async fn a_close_that_lands_while_a_turn_is_in_flight_is_not_undone_by_the_reply() {
4207        let (tmp, talks) = store();
4208        let spec = mock_agent(tmp.path(), REPLY, env("here you go"));
4209        let cfg = config(spec);
4210        // The in-flight turn's own handle: loaded once, the way a spawned
4211        // background task in `web::talk_say` holds one for the whole turn.
4212        let mut in_flight = begin(&talks, &cfg, tmp.path().to_owned(), None).expect("begin");
4213
4214        // The operator closes the conversation through a *different* handle
4215        // while the turn above is still running - exactly what a close typed
4216        // on the phone while an agent is mid-answer looks like.
4217        let mut closed_elsewhere = talks.get(&in_flight.id).expect("reread");
4218        close(&mut closed_elsewhere, &talks).expect("close");
4219        assert_eq!(
4220            talks.get(&in_flight.id).expect("reread").status,
4221            TalkStatus::Closed,
4222            "the close landed on disk before the turn finished"
4223        );
4224
4225        // The turn's own handle still says `open` - it was loaded before the
4226        // close - and finishing it must not resurrect the conversation the
4227        // operator already ended.
4228        assert_eq!(in_flight.status, TalkStatus::Open);
4229        respond(
4230            &talks.claim_turn(&in_flight.id).unwrap().unwrap(),
4231            &mut in_flight,
4232            &talks,
4233            &cfg,
4234            "one more question",
4235        )
4236        .await
4237        .expect("the turn itself still completes");
4238
4239        let on_disk = talks.get(&in_flight.id).expect("reread");
4240        assert_eq!(
4241            on_disk.status,
4242            TalkStatus::Closed,
4243            "a close must stick even when a turn that started before it finishes after it"
4244        );
4245        // The reply is not lost either: a turn already in flight when the
4246        // operator closed still gets its answer recorded.
4247        assert!(
4248            on_disk.turns.iter().any(|t| t.body == "here you go"),
4249            "the in-flight turn's own reply is still recorded: {:?}",
4250            on_disk.turns
4251        );
4252    }
4253
4254    #[test]
4255    fn a_close_that_lands_before_record_is_called_is_not_undone_by_it() {
4256        let (tmp, talks) = store();
4257        let spec = mock_agent(tmp.path(), REPLY, env("hi"));
4258        let cfg = config(spec);
4259        // The handle `web::talk_say` would have read before awaiting config
4260        // discovery, then carried across that await into `record`.
4261        let mut stale = begin(&talks, &cfg, tmp.path().to_owned(), None).expect("begin");
4262
4263        // The operator closes the conversation through a *different* handle
4264        // in the gap between that read and the call to `record` below.
4265        let mut closed_elsewhere = talks.get(&stale.id).expect("reread");
4266        close(&mut closed_elsewhere, &talks).expect("close");
4267        assert_eq!(
4268            talks.get(&stale.id).expect("reread").status,
4269            TalkStatus::Closed,
4270            "the close landed on disk before record was called"
4271        );
4272
4273        // The stale handle still says `open` - it was loaded before the
4274        // close - so a `record` that trusted it would append a turn and
4275        // write the conversation back open, undoing the close.
4276        assert_eq!(stale.status, TalkStatus::Open);
4277        let err = record(&mut stale, &talks, "still there?", Vec::new())
4278            .expect_err("a close that landed first must be honored, not overwritten");
4279        assert!(err.to_string().contains("closed"));
4280
4281        let on_disk = talks.get(&stale.id).expect("reread");
4282        assert_eq!(
4283            on_disk.status,
4284            TalkStatus::Closed,
4285            "record must not resurrect a conversation closed while its snapshot was stale"
4286        );
4287        assert!(
4288            on_disk.turns.is_empty(),
4289            "the rejected turn must not have been appended: {:?}",
4290            on_disk.turns
4291        );
4292        let _ = &cfg; // config kept only to build the agent above
4293    }
4294
4295    #[test]
4296    fn close_blocks_on_records_guard_rather_than_interleaving_with_it() {
4297        let (tmp, talks) = store();
4298        let spec = mock_agent(tmp.path(), REPLY, env("hi"));
4299        let cfg = config(spec);
4300        let talk = begin(&talks, &cfg, tmp.path().to_owned(), None).expect("begin");
4301
4302        // Hold the same guard `record`'s read-modify-write section holds for
4303        // the whole of its own read-then-write, standing in for `record`
4304        // being paused between its read and its `put`.
4305        let held = talks.guard().unwrap();
4306
4307        let talks2 = talks.clone();
4308        let id = talk.id.clone();
4309        let closing = std::thread::spawn(move || {
4310            let mut talk = talks2.get(&id).expect("get");
4311            close(&mut talk, &talks2).expect("close");
4312        });
4313
4314        std::thread::sleep(Duration::from_millis(50));
4315        assert!(
4316            !closing.is_finished(),
4317            "close must wait for the guard, not read and write while it is held - \
4318             a re-read alone narrows this window without closing it"
4319        );
4320
4321        drop(held);
4322        closing.join().expect("close thread panicked");
4323
4324        assert_eq!(
4325            talks.get(&talk.id).expect("reread").status,
4326            TalkStatus::Closed,
4327            "once the guard is free, close still lands"
4328        );
4329        let _ = &cfg; // config kept only to build the agent above
4330    }
4331
4332    #[test]
4333    fn reopening_a_closed_talk_lets_it_take_turns_again_and_reopening_twice_is_not_an_error() {
4334        let (tmp, talks) = store();
4335        let spec = mock_agent(tmp.path(), REPLY, env("hi"));
4336        let cfg = config(spec);
4337        let mut talk = begin(&talks, &cfg, tmp.path().to_owned(), None).expect("begin");
4338
4339        close(&mut talk, &talks).expect("close");
4340        assert_eq!(talk.status, TalkStatus::Closed);
4341
4342        reopen(&mut talk, &talks).expect("reopen");
4343        assert_eq!(talk.status, TalkStatus::Open);
4344        assert_eq!(
4345            talks.get(&talk.id).expect("reread").status,
4346            TalkStatus::Open
4347        );
4348
4349        // Idempotent: reopening an already-open talk is not an error.
4350        reopen(&mut talk, &talks).expect("reopening an open talk is not an error");
4351        assert_eq!(talk.status, TalkStatus::Open);
4352
4353        record(&mut talk, &talks, "one more thing", Vec::new())
4354            .expect("a reopened talk takes turns again");
4355        let _ = &cfg; // config kept only to build the agent above
4356    }
4357
4358    #[test]
4359    fn removing_a_talk_deletes_its_record_and_artifacts_and_refuses_an_unknown_id() {
4360        let (tmp, talks) = store();
4361        let spec = mock_agent(tmp.path(), REPLY, env("hi"));
4362        let cfg = config(spec);
4363        let talk = begin(&talks, &cfg, tmp.path().to_owned(), None).expect("begin");
4364
4365        let artifacts = talks.artifacts_of(&talk.id);
4366        std::fs::create_dir_all(&artifacts).expect("create artifacts dir");
4367        std::fs::write(artifacts.join("turn-1.txt"), "hello").expect("write artifact");
4368
4369        talks.remove(&talk.id).expect("remove");
4370        assert!(!talks.path_of(&talk.id).is_file(), "the record is gone");
4371        assert!(!artifacts.is_dir(), "the artifacts directory is gone");
4372        assert!(
4373            talks.get(&talk.id).is_err(),
4374            "a removed talk cannot be read back"
4375        );
4376
4377        let err = talks
4378            .remove("nonexistent-id")
4379            .expect_err("unknown id refused");
4380        assert!(err.to_string().contains("no talk matches"), "{err}");
4381        let _ = &cfg; // config kept only to build the agent above
4382    }
4383
4384    #[tokio::test]
4385    async fn a_delete_that_lands_while_a_turn_is_in_flight_is_not_undone_by_the_reply() {
4386        let (tmp, talks) = store();
4387        let spec = mock_agent(tmp.path(), REPLY, env("here you go"));
4388        let cfg = config(spec);
4389        // The in-flight turn's own handle, loaded before the delete lands -
4390        // the same shape as the matching close test above.
4391        let mut in_flight = begin(&talks, &cfg, tmp.path().to_owned(), None).expect("begin");
4392
4393        talks.remove(&in_flight.id).expect("remove");
4394        assert!(
4395            talks.get(&in_flight.id).is_err(),
4396            "the delete landed on disk before the turn finished"
4397        );
4398
4399        // The turn's own handle has no way to know the record is gone -
4400        // finishing it must not write the file back into existence.
4401        respond(
4402            &talks.claim_turn(&in_flight.id).unwrap().unwrap(),
4403            &mut in_flight,
4404            &talks,
4405            &cfg,
4406            "one more question",
4407        )
4408        .await
4409        .expect("the turn itself still completes rather than erroring");
4410
4411        assert!(
4412            talks.get(&in_flight.id).is_err(),
4413            "a delete must stick even when a turn that started before it finishes after it"
4414        );
4415    }
4416
4417    #[test]
4418    fn a_delete_that_lands_before_record_is_called_is_not_undone_by_it() {
4419        let (tmp, talks) = store();
4420        let spec = mock_agent(tmp.path(), REPLY, env("hi"));
4421        let cfg = config(spec);
4422        // The handle `web::talk_say` would have read before awaiting config
4423        // discovery, then carried across that await into `record`.
4424        let mut stale = begin(&talks, &cfg, tmp.path().to_owned(), None).expect("begin");
4425
4426        talks.remove(&stale.id).expect("remove");
4427
4428        // The stale handle has no way to know the record is gone - a
4429        // `record` that trusted it would append a turn and write the
4430        // conversation back into existence.
4431        let err = record(&mut stale, &talks, "still there?", Vec::new())
4432            .expect_err("a delete that landed first must be honored, not overwritten");
4433        assert!(err.to_string().contains("deleted"), "{err}");
4434
4435        assert!(
4436            talks.get(&stale.id).is_err(),
4437            "record must not resurrect a conversation deleted while its snapshot was stale"
4438        );
4439        let _ = &cfg; // config kept only to build the agent above
4440    }
4441
4442    #[test]
4443    fn a_delete_that_lands_before_close_is_called_is_not_undone_by_it() {
4444        let (tmp, talks) = store();
4445        let spec = mock_agent(tmp.path(), REPLY, env("hi"));
4446        let cfg = config(spec);
4447        // `web::talk_close` loads `talk` and calls `close` right after - this
4448        // stands in for a delete landing in that gap.
4449        let mut stale = begin(&talks, &cfg, tmp.path().to_owned(), None).expect("begin");
4450
4451        talks.remove(&stale.id).expect("remove");
4452
4453        // The stale handle has no way to know the record is gone - a `close`
4454        // that fell back to it would write the conversation back into
4455        // existence, closed.
4456        let err = close(&mut stale, &talks)
4457            .expect_err("a delete that landed first must be honored, not overwritten");
4458        assert!(err.to_string().contains("deleted"), "{err}");
4459
4460        assert!(
4461            talks.get(&stale.id).is_err(),
4462            "close must not resurrect a conversation deleted while its snapshot was stale"
4463        );
4464        let _ = &cfg; // config kept only to build the agent above
4465    }
4466
4467    #[test]
4468    fn a_delete_that_lands_before_reopen_is_called_is_not_undone_by_it() {
4469        let (tmp, talks) = store();
4470        let spec = mock_agent(tmp.path(), REPLY, env("hi"));
4471        let cfg = config(spec);
4472        // `web::talk_reopen` loads `talk` and calls `reopen` right after -
4473        // this stands in for a delete landing in that gap.
4474        let mut stale = begin(&talks, &cfg, tmp.path().to_owned(), None).expect("begin");
4475        close(&mut stale, &talks).expect("close");
4476
4477        talks.remove(&stale.id).expect("remove");
4478
4479        // The stale handle has no way to know the record is gone - a
4480        // `reopen` that fell back to it would write the conversation back
4481        // into existence, open.
4482        let err = reopen(&mut stale, &talks)
4483            .expect_err("a delete that landed first must be honored, not overwritten");
4484        assert!(err.to_string().contains("deleted"), "{err}");
4485
4486        assert!(
4487            talks.get(&stale.id).is_err(),
4488            "reopen must not resurrect a conversation deleted while its snapshot was stale"
4489        );
4490        let _ = &cfg; // config kept only to build the agent above
4491    }
4492
4493    #[test]
4494    fn list_puts_open_talks_before_closed_ones() {
4495        let (tmp, talks) = store();
4496        let make = |id: &str, status: TalkStatus| {
4497            let mut t = Talk {
4498                pending_breaks: None,
4499                schema: SCHEMA,
4500                id: id.to_owned(),
4501                repo: tmp.path().to_owned(),
4502                agent: "mock".to_owned(),
4503                status,
4504                turns: Vec::new(),
4505                pending: String::new(),
4506                pending_attachments: Vec::new(),
4507                fallback: false,
4508                persona: String::new(),
4509                persona_dirty: false,
4510                implementers: 1,
4511                implementers_dirty: false,
4512                created_at: Timestamp::now(),
4513                updated_at: Timestamp::now(),
4514                seat: SeatState::new(SEAT, "mock", 7),
4515            };
4516            talks.put(&mut t).expect("put");
4517        };
4518        make("20260901-000000-0001", TalkStatus::Open);
4519        make("20260902-000000-0002", TalkStatus::Open);
4520        make("20260903-000000-0003", TalkStatus::Closed);
4521
4522        let ids: Vec<String> = talks.list().into_iter().map(|t| t.id).collect();
4523        assert_eq!(
4524            ids,
4525            [
4526                "20260902-000000-0002",
4527                "20260901-000000-0001",
4528                "20260903-000000-0003"
4529            ]
4530        );
4531        assert_eq!(talks.count_open(), 2);
4532    }
4533
4534    #[test]
4535    fn tasks_of_finds_only_this_talks_own_tasks() {
4536        let dir = tempfile::tempdir().expect("tempdir");
4537        let queue = Queue::at(dir.path().join("queue"));
4538
4539        let mut mine = Task::new(
4540            "rework the loader".to_owned(),
4541            "rework the loader".to_owned(),
4542            PathBuf::from("/repo"),
4543            Source::Agent {
4544                run: "20260904-014455-ab12".to_owned(),
4545                node: "chat".to_owned(),
4546            },
4547        );
4548        queue.put(&mut mine).expect("put mine");
4549
4550        let mut theirs = Task::new(
4551            "unrelated".to_owned(),
4552            "unrelated".to_owned(),
4553            PathBuf::from("/repo"),
4554            Source::Agent {
4555                run: "20260904-090000-zz99".to_owned(),
4556                node: "implement".to_owned(),
4557            },
4558        );
4559        queue.put(&mut theirs).expect("put theirs");
4560
4561        let mut human = Task::new(
4562            "typed by hand".to_owned(),
4563            "typed by hand".to_owned(),
4564            PathBuf::from("/repo"),
4565            Source::Human,
4566        );
4567        queue.put(&mut human).expect("put human");
4568
4569        let found = tasks_of(&queue, "20260904-014455-ab12");
4570        assert_eq!(found.len(), 1);
4571        assert_eq!(found[0].id, mine.id);
4572    }
4573
4574    #[test]
4575    fn the_briefing_names_solo_task_add() {
4576        let brief = briefing(Path::new("/repo"), "en", false);
4577        assert!(brief.contains("magi task add --solo"));
4578        assert!(brief.contains("/repo"));
4579        assert!(!brief.contains("Hold this conversation in"));
4580    }
4581
4582    /// Talk fixes `repo` at the directory the conversation was opened in, so
4583    /// an agent asked to change some other checkout has no path to it unless
4584    /// the briefing itself says `--repo` can take a short name - see
4585    /// `resolve_repo_by_name` in `src/main.rs`, which is what actually
4586    /// resolves it.
4587    #[test]
4588    fn the_briefing_explains_targeting_a_different_repository_by_name() {
4589        let brief = briefing(Path::new("/repo"), "en", false);
4590        assert!(brief.contains("--repo does not have to be a full path"));
4591        assert!(brief.contains("owner/repo"));
4592        assert!(brief.contains("magi repos"));
4593        assert!(brief.contains("ask the operator"));
4594    }
4595
4596    #[test]
4597    fn the_briefing_tells_the_assistant_to_pass_images_with_attach() {
4598        let brief = briefing(Path::new("/repo"), "en", false);
4599        assert!(brief.contains("--attach <path>"), "{brief}");
4600        assert!(brief.contains("deleting this conversation"), "{brief}");
4601    }
4602
4603    #[test]
4604    fn the_briefing_names_the_language_when_it_is_not_english() {
4605        let brief = briefing(Path::new("/repo"), "Japanese", false);
4606        assert!(brief.contains("Hold this conversation in Japanese"));
4607    }
4608
4609    #[test]
4610    fn the_briefing_forbids_writes_unless_the_repository_opted_in() {
4611        let read_only = briefing(Path::new("/repo"), "en", false);
4612        assert!(read_only.contains("Do not write files"));
4613        assert!(!read_only.contains("allow_write"));
4614
4615        let writable = briefing(Path::new("/repo"), "en", true);
4616        assert!(!writable.contains("Do not write files"));
4617        assert!(writable.contains("allow_write = true"));
4618        // Still names the queue for anything past a small named edit, and
4619        // still tells the agent to report what it changed.
4620        assert!(writable.contains("magi task add --solo"));
4621        assert!(writable.contains("say plainly what you"));
4622    }
4623}