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