Skip to main content

magi/
deputy.rs

1//! Deputies: the seat that waits on a conductor's question, so the conductor
2//! never has to - and on land's merge approval, whose free-text reply used to
3//! reach nobody either.
4//!
5//! [`crate::conduct`] files a question for the operator and moves on; it is
6//! non-blocking by construction, because one task's question must not park the
7//! whole polling loop. The price used to be that nothing at all was waiting on
8//! it: the owner's free-text reply landed in a thread no agent read, while the
9//! phone said "waiting for the agent". Question f1dc on task 684d sat open
10//! forever on a "setup done".
11//!
12//! A deputy is the answer. For each open conductor question, `magi serve`
13//! runs one short-lived agent seat that is handed what the conductor knew
14//! ([`brief`]: the task, why it asked, what each option leads to), blocks on
15//! the question with `magi ask --wait`, answers back in the thread with
16//! `magi ask --thread`, and - when the owner's own words clearly pick an
17//! option - records that with `magi ask --settle`. Applying the outcome is not
18//! the deputy's job: an answered conductor question goes through the daemon's
19//! existing `resolve_blockers` / action path, exactly as one tapped on the
20//! phone does.
21//!
22//! # Invariants
23//!
24//! - **One seat per question, keyed by seat name.** `deputy-<question id>`,
25//!   never the conductor's shared seat (`conduct/seat.json`, overwritten by
26//!   every cycle) and never an agent id.
27//! - **Persisted on the question** ([`crate::ask::Deputy`]), always through
28//!   [`Questions::update`]: the owner's say and answer write the same file. A
29//!   restarted daemon resumes the seat only when [`agent::has_session`] says
30//!   it can; otherwise it starts a fresh seat and re-sends the whole context,
31//!   never assuming memory.
32//! - **Bounded.** At most `daemon.max_deputies` at once; at most
33//!   [`MAX_STARTS`] starts per question, and a restart does not reset that. A
34//!   deputy never outlives the question's `answer_timeout`: the waiter retires
35//!   the question at its deadline ([`crate::waiter`]) and the task goes to a
36//!   machine hold (`daemon::resolve_blockers`).
37//! - **Never a second agent on one question.** A fresh lease or the claim file
38//!   means somebody is already on it.
39//! - **A merge approval is served too, but stays land's.** Its deputy has no
40//!   `cwd` (so the waiter never touches it), its deadline is `asked_at +
41//!   answer_timeout` and never moves on a reply ([`deadline`]), and the only
42//!   thing that retires it is `daemon::land_resume_state`. A say alone never
43//!   merges: `--settle` accepts `merge` only for the owner's own word `merge`.
44//! - **A release-watch question is served too, and stays the watcher's.** The
45//!   escalation, local-mode approval and failed-release questions
46//!   ([`crate::release_watch`], node `release-bump`, seat `release-watch`) have
47//!   no asker, so a say reached nobody. Its deputy has no `cwd` either, a
48//!   fixed `asked_at + answer_timeout` deadline ([`deadline`]), and applies
49//!   nothing: `--settle` records a choice and the watcher applies it on its next
50//!   lap. A local approval's `merge` / `hold` go through the merge-approval
51//!   rules ([`merge_gated`]). A deputy never closes or merges the pull request.
52//! - **Every open question the owner can say something to has a listener.**
53//!   [`Kind::Triage`] serves the triage questions; [`Kind::Generic`] is the
54//!   fallback for any other question with choices and no `cwd` (the divergence
55//!   question today, a node nobody has written yet tomorrow). It is decided by
56//!   the missing `cwd`, never by node name. Only a conductor question is ever
57//!   given a `cwd` ([`Deputies::attach`]); the other kinds would otherwise be
58//!   resumed and expired by the waiter beside the deputy. A test enumerates
59//!   every node filed without an asker, so a new kind of question cannot ship
60//!   without one.
61//! - **No new authority.** A deputy does not edit, merge or touch the queue,
62//!   and `--settle` accepts only an offered label backed by a verbatim quote
63//!   of the owner, never on a task the operator holds.
64
65use std::collections::{BTreeMap, HashMap, HashSet};
66use std::path::{Path, PathBuf};
67use std::sync::Arc;
68use std::time::{Duration, Instant};
69
70use anyhow::{Context as _, Result};
71use jiff::Timestamp;
72use tokio::task::JoinSet;
73
74use crate::agent::{self, Invocation, SeatState};
75use crate::ask::{ChoiceAction, Deputy, Question, Questions, Waiter as Note, WaiterKind, Who};
76use crate::config::Config;
77use crate::notices::{self, Notice};
78use crate::prompt;
79
80/// Node name a deputy runs under (`MAGI_NODE`); `magi ask --settle` accepts
81/// it and nothing else.
82pub const NODE: &str = "deputy";
83
84/// How often the runner looks at the store.
85pub const TICK: Duration = Duration::from_secs(5);
86
87/// Most turns ever started for one question. A deputy that keeps ending
88/// without an answer is a problem for the owner, not something to retry until
89/// the quota is gone.
90pub const MAX_STARTS: u32 = 3;
91
92/// Least gap between two starts for one question.
93const RESTART_AFTER: Duration = Duration::from_secs(30);
94
95/// Added to the question's remaining time for the invocation's wall clock, so
96/// the deputy's own `magi ask --wait` reaches the deadline first.
97const SLACK_SECS: u64 = 120;
98
99/// A claim file older than this was left by a process that died.
100const CLAIM_STALE: Duration = Duration::from_secs(60);
101
102/// How long the context-handover turn may take.
103const HANDOVER_TIMEOUT: Duration = Duration::from_secs(10 * 60);
104
105/// Stops the deputy when true: the daemon is parking.
106pub type Halt = Arc<dyn Fn() -> bool + Send + Sync>;
107
108/// What the conductor knew about one question, for its deputy.
109///
110/// `reason` is the conductor's own one-line reasoning (may be empty). Each
111/// choice is listed with what picking it does: an attached action, or - for
112/// the usual conductor question - that the answer is recorded on the task and
113/// the task is unblocked with it in its instructions.
114pub fn brief(
115    task_id: &str,
116    reason: &str,
117    choices: &[String],
118    actions: &BTreeMap<String, ChoiceAction>,
119) -> String {
120    let reason = reason.trim();
121    let mut s = format!(
122        "Task {task_id} (`magi task show {task_id}`) is blocked on this question. \
123         The conductor asked because: {}\n\n\
124         When the owner answers, magi records the question and the answer on the \
125         task and unblocks it, and the answer becomes part of the instructions of \
126         the task's next attempt.",
127        if reason.is_empty() {
128            "(it recorded no reasoning)"
129        } else {
130            reason
131        }
132    );
133    if !choices.is_empty() {
134        s.push_str("\n\nWhat each option does:");
135        for c in choices {
136            match actions.get(c) {
137                Some(a) => s.push_str(&format!("\n- `{c}`: also {}", a.describe())),
138                None => s.push_str(&format!(
139                    "\n- `{c}`: recorded on the task as the answer and the task is unblocked"
140                )),
141            }
142        }
143    }
144    s
145}
146
147/// What a deputy serves: the question kinds it is attached to.
148#[derive(Debug, Clone, Copy, PartialEq, Eq)]
149pub enum Kind {
150    /// A conductor's question ([`crate::conduct::NODE`]).
151    Conduct,
152    /// The merge approval ([`crate::land::APPROVAL_NODE`]).
153    Land,
154    /// A question the release watcher filed ([`crate::release_watch`]).
155    Release,
156    /// A triage question about a held task or a stuck dependency root
157    /// ([`crate::triage::NODE`], [`crate::triage::DEPS_NODE`]).
158    Triage,
159    /// Any other question nobody asked from inside a run (no `cwd`) that offers
160    /// choices: the fallback, so a node nobody has met yet still has a listener.
161    Generic,
162}
163
164/// The kind of question a deputy serves for `q`, `None` for every other.
165///
166/// The fallback is decided by the absence of a `cwd`, never by the node name: a
167/// question filed with `magi ask` inside a run records its `cwd` and has an
168/// asker (and the waiter), and the node a seat asks from is the seat's own name
169/// (a reviewer's `review` is also the divergence question's node). A question
170/// with no choices cannot be settled and is a notice, not something to answer.
171pub fn kind_of(q: &Question) -> Option<Kind> {
172    match q.node.as_str() {
173        crate::conduct::NODE => Some(Kind::Conduct),
174        crate::land::APPROVAL_NODE => Some(Kind::Land),
175        // The seat matters: `bump` files choice-less notices on the same node,
176        // which nobody can answer and which must not cost a deputy.
177        crate::bump::NOTICE_NODE if q.seat == "release-watch" => Some(Kind::Release),
178        crate::bump::NOTICE_NODE => None,
179        crate::triage::NODE | crate::triage::DEPS_NODE => Some(Kind::Triage),
180        _ if q.cwd.is_none() && !q.choices.is_empty() => Some(Kind::Generic),
181        _ => None,
182    }
183}
184
185/// What a deputy is told about a question of no known kind: only what the
186/// question itself stored.
187pub fn generic_brief(
188    q: &Question,
189    actions: &BTreeMap<String, ChoiceAction>,
190    home: &Path,
191) -> String {
192    // `Question::run` may also hold a task id; only a run id can be shown.
193    let run = crate::run::is_run_id(&q.run).then_some(q.run.as_str());
194    let knows = if run.is_some() {
195        "You know what the question itself says and what you can learn from \
196         its run by the read-only investigation described below."
197    } else {
198        "You know only what the question itself says."
199    };
200    let mut s = format!(
201        "This question was filed by magi (node `{}`, seat `{}`) with no agent \
202         waiting on it. Magi records the owner's answer and the component that \
203         asked applies it; you apply nothing. {knows} A choice whose effect you \
204         cannot read is not yours to guess: do not settle it, ask the owner \
205         what they mean with `--thread` instead.",
206        q.node, q.seat
207    );
208    if let Some(run) = run {
209        let artifacts = home.join("runs").join(run).join("artifacts");
210        s.push_str(&format!(
211            "\n\nThis question concerns run `{run}`. You may investigate it on \
212             your own, read-only: run `magi show {run}` and read files under \
213             `{}`. When the owner asks about details (for example which part \
214             was wrong), look it up yourself and answer; do not reply that you \
215             cannot see the details, and do not tell the owner to run `magi show` \
216             themselves. Investigating never changes anything: edit, commit, push \
217             and apply nothing. If the run cannot be read, say exactly that and do \
218             not guess at the cause or at what an option does.",
219            artifacts.display()
220        ));
221    }
222    if q.node == crate::github_text::ASK_NODE {
223        s.push_str("\n\n");
224        s.push_str(&crate::github_text::deputy_brief());
225    }
226    if !q.choices.is_empty() {
227        s.push_str("\n\nWhat each option does:");
228        for c in &q.choices {
229            match actions.get(c) {
230                Some(a) => s.push_str(&format!("\n- `{c}`: also {}", a.describe())),
231                None => s.push_str(&format!(
232                    "\n- `{c}`: recorded as the answer (nothing more is known)"
233                )),
234            }
235        }
236    }
237    s
238}
239
240/// Is picking `label` on `q` something that cannot be taken back, so that
241/// `--settle` must hold the owner's words to the same mechanical standard as a
242/// merge (`land::unhedged`)? Discarding a task deletes it; a divergence answer
243/// drops commits.
244pub fn destructive(q: &Question, label: &str) -> bool {
245    let at = q.choices.iter().position(|c| c == label);
246    // By label, not position: the retry question orders its choices differently.
247    if q.node == crate::github_text::ASK_NODE {
248        return matches!(
249            label,
250            crate::github_text::POST_ORIGINAL | crate::github_text::USE_REWRITE
251        );
252    }
253    match q.node.as_str() {
254        crate::triage::NODE => at == Some(2),
255        crate::triage::DEPS_NODE => at == Some(1),
256        n => n == crate::reconcile::NODE && q.seat == crate::reconcile::SEAT,
257    }
258}
259
260/// Does `--settle` hold `q` to the merge-approval rules (the verbatim quote of
261/// the latest message for `merge`, the whole message for `hold`)? A merge
262/// approval, and any other served question that offers `merge` (the release
263/// watcher's local-mode approval merges just as irreversibly).
264pub fn merge_gated(q: &Question) -> bool {
265    q.node == crate::land::APPROVAL_NODE
266        || (kind_of(q).is_some_and(|k| k != Kind::Conduct)
267            && q.choices.iter().any(|c| c == crate::land::APPROVE))
268}
269
270/// Does `q`'s clock run from `asked_at` and never move on a reply? True for the
271/// questions that something other than the waiter retires: a merge approval
272/// (land) and a release-watch question (the watcher, by silence being a hold).
273pub fn fixed_clock(q: &Question) -> bool {
274    matches!(kind_of(q), Some(Kind::Land | Kind::Release)) || q.node == crate::github_text::ASK_NODE
275}
276
277/// Second after which nobody is to be started or kept on `q`.
278///
279/// A conductor question runs from its last activity (`magi ask --thread`
280/// re-arms it, and the waiter retires it). A merge approval never moves: it
281/// runs from `asked_at`, exactly where `daemon::land_resume_state` abandons it,
282/// so a conversation cannot stretch the hold and land stays the only place that
283/// retires one.
284pub fn deadline(q: &Question, default_timeout: u64) -> i64 {
285    let secs = if q.answer_timeout > 0 {
286        q.answer_timeout
287    } else {
288        default_timeout
289    };
290    let from = if fixed_clock(q) {
291        q.asked_at.as_second()
292    } else {
293        q.last_activity()
294    };
295    from.saturating_add(secs as i64)
296}
297
298/// Can `magi serve` start a deputy at all under `cfg`? Not when deputies are
299/// switched off (`daemon.max_deputies = 0`) or the config could not be read.
300///
301/// Also false when the agent the deputy would run as cannot be resolved
302/// (`agent` is the deputy's recorded agent, empty when it has none): `turn`
303/// fails on that before it counts a start, so it would never reach
304/// [`MAX_STARTS`].
305pub fn can_start(cfg: Option<&Config>, agent: &str) -> bool {
306    cfg.is_some_and(|c| {
307        c.daemon.max_deputies > 0
308            && ((!agent.is_empty() && c.agent(agent).is_ok()) || c.resolve_roles().is_ok())
309    })
310}
311
312/// The agent a question's deputy records, empty when it has none yet.
313pub fn agent_of(q: &Question) -> &str {
314    q.deputy.as_ref().map_or("", |d| d.agent.as_str())
315}
316
317/// Has this question's deputy run out of starts, or can none ever start, with
318/// the deadline gone?
319///
320/// Then nothing will ever read an unread say, and the waiter must retire the
321/// question anyway instead of deferring to a deputy that no longer starts.
322/// `startable` is [`can_start`]; a fresh lease is the caller's to check.
323pub fn exhausted_past_deadline(
324    q: &Question,
325    startable: bool,
326    default_timeout: u64,
327    now: Timestamp,
328) -> bool {
329    q.status.open()
330        && q.deputy
331            .as_ref()
332            .is_some_and(|d| d.starts >= MAX_STARTS || !startable)
333        && now.as_second() > deadline(q, default_timeout)
334}
335
336/// The deputy runner: its own task inside `magi serve`, beside the waiter.
337pub struct Deputies {
338    store: Questions,
339    home: PathBuf,
340    cfg: Option<Config>,
341    /// Working directory for a question that recorded none.
342    fallback_repo: PathBuf,
343    max: usize,
344    halt: Halt,
345    tasks: JoinSet<String>,
346    inflight: HashSet<String>,
347    /// Earliest next start per question.
348    memo: HashMap<String, Instant>,
349}
350
351impl Deputies {
352    /// A runner over `store`, with at most `max` deputies at once.
353    pub fn new(
354        store: Questions,
355        home: PathBuf,
356        cfg: Option<Config>,
357        fallback_repo: PathBuf,
358        max: usize,
359        halt: Halt,
360    ) -> Self {
361        Self {
362            store,
363            home,
364            cfg,
365            fallback_repo,
366            max,
367            halt,
368            tasks: JoinSet::new(),
369            inflight: HashSet::new(),
370            memo: HashMap::new(),
371        }
372    }
373
374    fn default_timeout(&self) -> u64 {
375        self.cfg
376            .as_ref()
377            .map_or(Config::default().graph.answer_timeout, |c| {
378                c.graph.answer_timeout
379            })
380    }
381
382    fn reap(&mut self) {
383        while let Some(done) = self.tasks.try_join_next() {
384            if let Ok(id) = done {
385                self.inflight.remove(&id);
386            }
387        }
388        if self.tasks.is_empty() {
389            self.inflight.clear();
390        }
391    }
392
393    /// Give a question the record a deputy needs: the deputy itself (with a
394    /// brief rebuilt from what the question stored, when it was filed before
395    /// deputies existed - a lost reason is not invented) and the deadline.
396    ///
397    /// A conductor question also gets the working directory the waiter and
398    /// `magi ask --wait` use. Every other kind never does: `cwd` is what makes a
399    /// question the waiter's (it would resume and expire it beside the deputy),
400    /// and a release-watch question, a merge approval, a triage question and a
401    /// fallback one have no asker to resume.
402    fn attach(&self, q: &Question, kind: Kind) -> Option<Question> {
403        let default_timeout = self.default_timeout();
404        let repo = self.fallback_repo.to_string_lossy().into_owned();
405        let state = match kind {
406            Kind::Land => crate::run::RunState::load(&q.run).ok(),
407            Kind::Conduct | Kind::Release | Kind::Triage | Kind::Generic => None,
408        };
409        // The deadline land itself enforces for this run.
410        let timeout = state
411            .as_ref()
412            .map_or(default_timeout, |s| s.config.graph.answer_timeout);
413        self.store
414            .update(&q.id, |r| {
415                if r.deputy.is_none() {
416                    r.deputy = Some(Deputy::new(match kind {
417                        Kind::Conduct => brief(&r.run, &r.detail, &r.choices, &r.actions),
418                        Kind::Land => crate::land::deputy_brief(r, state.as_ref()),
419                        Kind::Release => crate::release_watch::deputy_brief(r, &self.home),
420                        Kind::Triage => crate::triage::deputy_brief(
421                            r,
422                            &crate::queue::Queue::at(self.home.join("queue")),
423                        ),
424                        Kind::Generic => generic_brief(r, &r.actions, &self.home),
425                    }));
426                }
427                if kind == Kind::Conduct && r.cwd.is_none() {
428                    r.cwd = Some(repo.clone());
429                }
430                if r.answer_timeout == 0 {
431                    r.answer_timeout = match kind {
432                        Kind::Conduct => default_timeout,
433                        Kind::Land => timeout,
434                        Kind::Release | Kind::Triage | Kind::Generic => default_timeout,
435                    };
436                }
437                Ok(())
438            })
439            .map(|(r, ())| r)
440            .map_err(|e| tracing::warn!("question {}: cannot attach a deputy: {e:#}", q.short()))
441            .ok()
442    }
443
444    /// Look at every open conductor question once and start a deputy on each
445    /// that has none, within the limits. Returns without waiting for them.
446    pub fn tick(&mut self, now: Timestamp) {
447        self.reap();
448        for q in self.store.list() {
449            if (self.halt)() {
450                return;
451            }
452            let Some(kind) = kind_of(&q) else {
453                continue;
454            };
455            if !q.status.open() {
456                continue;
457            }
458            let needs_cwd = kind == Kind::Conduct && q.cwd.is_none();
459            let q = if q.deputy.is_none() || needs_cwd || q.answer_timeout == 0 {
460                match self.attach(&q, kind) {
461                    Some(q) => q,
462                    None => continue,
463                }
464            } else {
465                q
466            };
467            let Some(dep) = q.deputy.as_ref() else {
468                continue;
469            };
470            if self.inflight.contains(&q.id)
471                || self.store.read_lease(&q.id).is_some_and(|l| l.fresh(now))
472            {
473                continue;
474            }
475            if dep.starts >= MAX_STARTS {
476                self.give_up(&q);
477                continue;
478            }
479            // Past the deadline the waiter retires the question; a say that
480            // arrived in time is still read first.
481            if now.as_second() > deadline(&q, self.default_timeout())
482                && q.unread_from_owner().is_none()
483            {
484                continue;
485            }
486            if self.inflight.len() >= self.max || !can_start(self.cfg.as_ref(), dep.agent.as_str())
487            {
488                continue;
489            }
490            if matches!(self.memo.get(&q.id), Some(until) if Instant::now() < *until) {
491                continue;
492            }
493            self.memo
494                .insert(q.id.clone(), Instant::now() + RESTART_AFTER);
495            self.inflight.insert(q.id.clone());
496            let job = Job {
497                store: self.store.clone(),
498                home: self.home.clone(),
499                cfg: self.cfg.clone(),
500                fallback_repo: self.fallback_repo.clone(),
501                halt: Arc::clone(&self.halt),
502            };
503            let id = q.id.clone();
504            self.tasks.spawn(async move {
505                if let Err(e) = job.turn(&id).await {
506                    tracing::warn!("deputy for question {}: {e:#}", crate::ask::short_id(&id));
507                }
508                id
509            });
510        }
511    }
512
513    /// Wait for every deputy turn in flight. The daemon never calls this; the
514    /// tests do, to look at the record once a turn is over.
515    pub async fn drain(&mut self) {
516        while let Some(done) = self.tasks.join_next().await {
517            if let Ok(id) = done {
518                self.inflight.remove(&id);
519            }
520        }
521        self.inflight.clear();
522    }
523
524    /// Say once that nobody is listening any more.
525    fn give_up(&self, q: &Question) {
526        notices::raise_in(
527            &self.home,
528            Notice::warn(
529                &format!("deputy:{}", q.id),
530                format!(
531                    "Question {} \"{}\": its follow-up agent ended {MAX_STARTS} times \
532                     without an answer and is not restarted. What you say is recorded \
533                     but nothing will read it; answer with one of the choices instead.",
534                    q.short(),
535                    q.summary
536                ),
537            ),
538        );
539    }
540}
541
542/// Everything one deputy turn needs, owned so it can run detached.
543struct Job {
544    store: Questions,
545    home: PathBuf,
546    cfg: Option<Config>,
547    fallback_repo: PathBuf,
548    halt: Halt,
549}
550
551impl Job {
552    /// Run one invocation, beating the lease; `None` when the daemon is
553    /// parking and the turn was dropped.
554    async fn drive(
555        &self,
556        spec: &crate::config::AgentSpec,
557        seat: &mut SeatState,
558        inv: &Invocation<'_>,
559        id: &str,
560    ) -> Option<Result<agent::AgentOutput>> {
561        let fut = agent::invoke(spec, seat, inv);
562        tokio::pin!(fut);
563        let mut beat = tokio::time::interval(Duration::from_secs(1));
564        let mut beats = 0u32;
565        loop {
566            tokio::select! {
567                r = &mut fut => break Some(r),
568                _ = beat.tick() => {
569                    if (self.halt)() {
570                        break None;
571                    }
572                    beats += 1;
573                    if beats % 20 == 0 {
574                        self.store.beat(id, WaiterKind::Deputy);
575                    }
576                }
577            }
578        }
579    }
580
581    /// The question settled or expired during the handover: release it without
582    /// starting the long turn. The start stays counted - the handover ran.
583    fn park_quietly(&self, id: &str) {
584        let _ = self.store.update(id, |r| {
585            r.waiter = None;
586            Ok(())
587        });
588        self.store.drop_lease(id);
589    }
590
591    /// Parking: the turn is dropped and the start refunded. The seat stays as
592    /// last persisted - after the handover turn, resumable.
593    fn park(&self, id: &str) {
594        let _ = self.store.update(id, |r| {
595            if let Some(d) = r.deputy.as_mut() {
596                d.starts = d.starts.saturating_sub(1);
597            }
598            r.waiter = None;
599            Ok(())
600        });
601        self.store.drop_lease(id);
602    }
603
604    async fn turn(&self, id: &str) -> Result<()> {
605        let claim = self.store.root().join(format!("{id}.deputy-claim"));
606        // The claim only covers the decision to start and the write that records
607        // it, so a daemon that dies holding it blocks a restart for a minute,
608        // not for a turn's length. The lease guards the turn itself.
609        if !crate::waiter::take_claim(&claim, CLAIM_STALE) {
610            return Ok(());
611        }
612        let release = crate::waiter::Release(claim);
613
614        // Decided again under the claim, on the record as it is now.
615        let q = self.store.get(id)?;
616        let now = Timestamp::now();
617        let Some(dep) = q.deputy.clone() else {
618            return Ok(());
619        };
620        if !q.status.open()
621            || dep.starts >= MAX_STARTS
622            || self.store.read_lease(&q.id).is_some_and(|l| l.fresh(now))
623        {
624            return Ok(());
625        }
626        let cfg = self
627            .cfg
628            .as_ref()
629            .context("the deputy's configuration is not available")?;
630        let spec = match cfg.agent(&dep.agent) {
631            Ok(s) if !dep.agent.is_empty() => s.clone(),
632            _ => {
633                cfg.resolve_roles()
634                    .context("resolving the deputy's agent")?
635                    .conductor
636            }
637        };
638        // The same CLI conversation when it can be resumed; otherwise a fresh
639        // seat - and either way the prompt carries the whole context.
640        let key = crate::ask::deputy_seat_key(&q.id);
641        let (mut seat, resumed) = match dep.seat.clone() {
642            Some(s)
643                if s.agent == spec.id && agent::has_session(spec.kind, &s, cfg.graph.sessions) =>
644            {
645                (s, true)
646            }
647            _ => (SeatState::new(&key, &spec.id, crate::rng::entropy()), false),
648        };
649        // A release-watch question has no `cwd`; its watch record names the
650        // checkout the pull request belongs to.
651        let recorded = q.cwd.clone().or_else(|| match kind_of(&q) {
652            Some(Kind::Release) => {
653                crate::release_watch::state_for_question(&self.home, &q.id).map(|st| st.repo)
654            }
655            // These record a task id in `run` (or a run id for a divergence's
656            // sibling kinds): the task's repository, when it can be found.
657            Some(Kind::Triage | Kind::Generic) => crate::queue::Queue::at(self.home.join("queue"))
658                .get(&q.run)
659                .ok()
660                .map(|t| t.repo.to_string_lossy().into_owned())
661                .filter(|r| !r.is_empty()),
662            _ => None,
663        });
664        let cwd = recorded
665            .as_deref()
666            .map(PathBuf::from)
667            .filter(|p| p.is_dir())
668            .unwrap_or_else(|| self.fallback_repo.clone());
669
670        // A settle's note is part of what the seat said, so a resumed seat
671        // reads its own report back.
672        let bodies: Vec<String> = q
673            .thread
674            .iter()
675            .map(|t| match &t.note {
676                Some(n) => format!("{}\n(note: {n})", t.body),
677                None => t.body.clone(),
678            })
679            .collect();
680        let thread: Vec<(&str, &str)> = q
681            .thread
682            .iter()
683            .zip(&bodies)
684            .map(|(t, body)| {
685                (
686                    if t.who == Who::Operator {
687                        "operator"
688                    } else {
689                        "agent"
690                    },
691                    body.as_str(),
692                )
693            })
694            .collect();
695        let read = &thread[..q.delivered_turns.min(thread.len())];
696        let unread = q.unread_from_owner();
697        let snapshot = q.thread.len();
698        let body = prompt::deputy(&prompt::DeputyPrompt {
699            id: &q.id,
700            summary: &q.summary,
701            detail: &q.detail,
702            brief: &dep.brief,
703            choices: &q.choices,
704            thread: read,
705            unread: unread.as_deref(),
706            resumed,
707            handover: false,
708            kind: kind_of(&q).unwrap_or(Kind::Conduct),
709            language: &cfg.graph.language,
710        });
711
712        // Recorded before the turn starts, so a daemon that dies mid-turn
713        // leaves a start counted and the seat on the record.
714        self.store.beat(&q.id, WaiterKind::Deputy);
715        let starts = dep.starts + 1;
716        let first = seat.clone();
717        self.store.update(&q.id, |r| {
718            if let Some(d) = r.deputy.as_mut() {
719                d.agent = spec.id.clone();
720                d.seat = Some(first);
721                d.starts = starts;
722            }
723            r.waiter = Some(Note {
724                kind: WaiterKind::Deputy,
725                since: now,
726            });
727            Ok(())
728        })?;
729        drop(release);
730        tracing::info!(
731            "question {}: deputy seat {} {} (start {starts}/{MAX_STARTS})",
732            q.short(),
733            seat.key,
734            if resumed { "resuming" } else { "starting" }
735        );
736
737        let left = (deadline(&q, cfg.graph.answer_timeout) - now.as_second()).max(0) as u64;
738        let artifacts = self.store.root().join(format!("{}.deputy", q.id));
739        let cache_dir = cfg.cache_dir();
740        // `magi ask` writes the question record and its lock, which a read-only
741        // sandbox refuses, so the seat cannot be read-only. It is told never to
742        // edit anything; the daemon, not the deputy, applies outcomes.
743        let allow_write = true;
744        // The question store is outside the repository, and `magi ask` writes it.
745        // A merge approval's deputy also files follow-up tasks with `magi task
746        // add`, which writes the queue; that is the one other place it writes.
747        let writable = [
748            self.store.root().to_path_buf(),
749            crate::queue::Queue::open().root().to_path_buf(),
750        ];
751        macro_rules! invocation {
752            ($prompt:expr, $stem:expr, $timeout:expr) => {
753                Invocation {
754                    cwd: &cwd,
755                    prompt: $prompt,
756                    timeout: $timeout,
757                    allow_write,
758                    unsandboxed: false,
759                    sessions: cfg.graph.sessions,
760                    artifacts: &artifacts,
761                    stem: $stem,
762                    // The question's own run key (the task id) is what lets this
763                    // seat's `magi ask` pass the ownership check on a conductor
764                    // question.
765                    run: &q.run,
766                    node: NODE,
767                    cache_dir: cache_dir.as_deref(),
768                    attachments: &[],
769                    writable: &writable,
770                }
771            };
772        }
773
774        // A fresh seat first takes a short turn that ends: `agent::invoke` only
775        // learns the CLI's session id when it returns, and the real turn blocks
776        // in `magi ask` for hours, so a daemon stopped mid-wait would otherwise
777        // leave nothing to resume. This turn persists the seat before the wait.
778        let mut early = None;
779        if !resumed && cfg.graph.sessions {
780            let hbody = prompt::deputy(&prompt::DeputyPrompt {
781                id: &q.id,
782                summary: &q.summary,
783                detail: &q.detail,
784                brief: &dep.brief,
785                choices: &q.choices,
786                thread: read,
787                unread: None,
788                resumed: false,
789                handover: true,
790                kind: kind_of(&q).unwrap_or(Kind::Conduct),
791                language: &cfg.graph.language,
792            });
793            let hstem = format!("handover-{starts}");
794            // Bounded by the question's own deadline, not only by its own cap: the
795            // lease it beats would otherwise keep an expired question alive.
796            let hlimit = HANDOVER_TIMEOUT.min(Duration::from_secs(left.max(1)));
797            let hinv = invocation!(&hbody, &hstem, hlimit);
798            let Some(done) = self.drive(&spec, &mut seat, &hinv, &q.id).await else {
799                self.park(&q.id);
800                return Ok(());
801            };
802            let kept = seat.clone();
803            self.store.update(&q.id, |r| {
804                if let Some(d) = r.deputy.as_mut() {
805                    d.seat = Some(kept);
806                }
807                Ok(())
808            })?;
809            if !matches!(&done, Ok(o) if o.usable()) {
810                early = Some(done);
811            }
812        }
813
814        // The handover may have been slow: look at the question again, and
815        // measure the long turn from what is left now.
816        let left = if early.is_none() && !resumed && cfg.graph.sessions {
817            let now = Timestamp::now();
818            let again = self.store.get(&q.id)?;
819            let left = (deadline(&again, cfg.graph.answer_timeout) - now.as_second()).max(0) as u64;
820            if !again.status.open() || (left == 0 && again.unread_from_owner().is_none()) {
821                self.park_quietly(&q.id);
822                return Ok(());
823            }
824            left
825        } else {
826            left
827        };
828        let out = match early {
829            Some(done) => done,
830            None => {
831                let stem = format!("turn-{starts}");
832                let timeout = Duration::from_secs(left.max(60) + SLACK_SECS);
833                let inv = invocation!(&body, &stem, timeout);
834                match self.drive(&spec, &mut seat, &inv, &q.id).await {
835                    Some(out) => out,
836                    None => {
837                        self.park(&q.id);
838                        return Ok(());
839                    }
840                }
841            }
842        };
843
844        let (text, why) = match out {
845            Ok(o) if o.usable() => (Some(o.text.trim().to_owned()), None),
846            Ok(o) if o.timed_out => (None, Some("its turn timed out".to_owned())),
847            Ok(o) if o.quota_exhausted() => (None, Some("the agent is out of quota".to_owned())),
848            Ok(o) => (
849                None,
850                Some(format!("its turn failed (exit {:?})", o.exit_code)),
851            ),
852            Err(e) => (None, Some(format!("the agent could not be started: {e:#}"))),
853        };
854        let kept = seat.clone();
855        self.store.update(&q.id, |r| {
856            if let Some(d) = r.deputy.as_mut() {
857                d.seat = Some(kept);
858            }
859            r.waiter = None;
860            // The owner's words were in the prompt. An agent that read them but
861            // answered in prose instead of `magi ask --thread` still spoke;
862            // keep it on the record so the owner reads it.
863            if let Some(text) = text
864                && unread.is_some()
865                && !text.is_empty()
866                && r.status.open()
867                && r.thread.len() == snapshot
868            {
869                r.delivered_turns = r.delivered_turns.max(snapshot);
870                let choices = r.choices.clone();
871                r.reply(text, choices)?;
872            }
873            Ok(())
874        })?;
875        self.store.drop_lease(&q.id);
876        if let Some(why) = why {
877            tracing::warn!("question {}: the deputy ended: {why}", q.short());
878            notices::raise_in(
879                &self.home,
880                Notice::warn(
881                    &format!("deputy-turn:{}", q.id),
882                    format!(
883                        "Question {} \"{}\": its follow-up agent stopped ({why}).",
884                        q.short(),
885                        q.summary
886                    ),
887                ),
888            );
889        }
890        Ok(())
891    }
892}
893
894/// The runner's loop, until `stop` is asked for.
895pub async fn run(mut deputies: Deputies, stop: crate::daemon::Stop) {
896    while !stop.stopped() {
897        deputies.tick(Timestamp::now());
898        tokio::time::sleep(TICK).await;
899    }
900}