Skip to main content

magi/
talk.rs

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