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