Skip to main content

magi/
consult.rs

1//! Handing an open question to the chat conversation its task came from.
2//!
3//! A task filed by the standing chat (`Source::Agent` with
4//! [`crate::queue::CHAT_NODE`], whose `run` is the conversation's id) can raise
5//! a question the owner would rather talk through where the task started. The
6//! owner passes it on; the chat agent carries out the owner's decision with
7//! `magi answer`, or puts the decision points to the owner in the conversation.
8//!
9//! Three rules, each with a reason:
10//!
11//! - **One place decides.** [`origin_talk`] is the only judge of "does this
12//!   question have a chat to ask"; the web view and both entry points use it.
13//!   [`associated_talk`] resolves typed ownership links for navigation,
14//!   independently of consultation eligibility.
15//! - **Not a choice.** The hand-over never goes into `Question::choices` and
16//!   never touches `Question::answer`: the question stays `Open`, its
17//!   validation and the choice actions stay as they were.
18//! - **No new seat, no new waiter.** The text is queued as a draft of the
19//!   existing talk, so the chat's own turn machinery (its session, its turn
20//!   gate) runs it exactly as if the owner had typed it.
21
22use anyhow::{Context, Result, bail};
23use jiff::Timestamp;
24
25use crate::ask::{ChatConsult, Question, Questions};
26use crate::config::Config;
27use crate::queue::Task;
28use crate::talk::{self, Talk, Talks};
29
30/// The conversation `q` may be handed to, if any.
31///
32/// Requires an open question whose task was filed from a chat. The chat may
33/// be closed: [`begin`] reopens it. Release notices are left out.
34pub fn origin_talk(tasks: &[Task], talks: &[Talk], q: &Question) -> Option<Talk> {
35    if !q.status.open() || q.node == crate::bump::NOTICE_NODE {
36        return None;
37    }
38    let task = crate::daemon::task_of_question(tasks, q)?;
39    let chat = chat_talk_of(tasks, task)?;
40    talks.iter().find(|talk| talk.id == chat).cloned()
41}
42
43/// The existing conversation associated with a question, regardless of whether
44/// the question is still eligible for consultation.
45pub fn associated_talk(tasks: &[Task], talks: &[Talk], q: &Question) -> Option<Talk> {
46    associated_talk_with_watches(tasks, talks, q, &[])
47}
48
49/// A typed ownership link, shared by association and the web's selective loader.
50#[derive(Clone, Copy)]
51pub(crate) enum QuestionTaskRef<'a> {
52    Task(&'a str),
53    Run(&'a str),
54}
55
56pub(crate) fn question_task_refs<'a>(
57    q: &'a Question,
58    watches: &'a [crate::release_watch::WatchState],
59) -> Vec<QuestionTaskRef<'a>> {
60    // Review-start reconciliation is filed under a task id, unlike normal
61    // review questions; both the node and seat identify that producer.
62    let names_task = q.run_names_task()
63        || (q.node == crate::reconcile::NODE && q.seat == crate::reconcile::SEAT);
64    let mut refs = vec![if names_task {
65        QuestionTaskRef::Task(&q.run)
66    } else {
67        QuestionTaskRef::Run(&q.run)
68    }];
69    let mut matches = watches
70        .iter()
71        .filter(|w| w.question.as_deref() == Some(&q.id));
72    // Duplicate records are ambiguous even if they currently name one owner.
73    if let Some(watch) = matches.next()
74        && matches.next().is_none()
75    {
76        refs.extend(watch.run.as_deref().map(QuestionTaskRef::Run));
77        refs.extend(watch.held_task.as_deref().map(QuestionTaskRef::Task));
78    }
79    refs
80}
81
82/// Explicit links may fill a missing owner, but conflicting owners fail closed.
83/// This navigation-only resolution never changes consultation eligibility.
84pub(crate) fn associated_talk_with_watches(
85    tasks: &[Task],
86    talks: &[Talk],
87    q: &Question,
88    watches: &[crate::release_watch::WatchState],
89) -> Option<Talk> {
90    let mut owner: Option<&Task> = None;
91    for reference in question_task_refs(q, watches) {
92        for task in tasks.iter().filter(|task| match reference {
93            QuestionTaskRef::Task(id) => task.id == id,
94            QuestionTaskRef::Run(id) => task.runs.iter().any(|run| run == id),
95        }) {
96            if owner.is_some_and(|previous| previous.id != task.id) {
97                return None;
98            }
99            owner = Some(task);
100        }
101    }
102    let chat = chat_talk_of(tasks, owner?)?;
103    talks.iter().find(|talk| talk.id == chat).cloned()
104}
105
106/// The talk `start` descends from: the id recorded on a task
107/// ([`Task::chat_talk`]) wins, so deleted ancestors do not matter; tasks
108/// written before it existed are resolved by walking the ancestry.
109///
110/// Walk the provenance of `start` to the task a chat filed.
111///
112/// A follow-up goes to the task its merged run served: `FollowUp::origin_task`
113/// when recorded (a missing one ends the walk - guessing another task could
114/// hand the question to an unrelated chat), else the task whose `runs` hold
115/// `FollowUp::run`. At most `MAX_FOLLOWUP_GENERATION + 1` tasks are looked at,
116/// each once, so a cycle ends in `None`.
117pub fn chat_talk_of(tasks: &[Task], start: &Task) -> Option<String> {
118    let mut seen = std::collections::HashSet::new();
119    let mut cur = start;
120    for _ in 0..=crate::followup::MAX_FOLLOWUP_GENERATION {
121        if !seen.insert(cur.id.as_str()) {
122            return None;
123        }
124        if let Some(id) = cur.chat_talk() {
125            return Some(id.to_owned());
126        }
127        let f = cur.followup.as_ref()?;
128        cur = match &f.origin_task {
129            Some(id) => tasks.iter().find(|t| &t.id == id)?,
130            None => tasks.iter().find(|t| t.runs.contains(&f.run))?,
131        };
132    }
133    None
134}
135
136/// Is any question handed to talk `talk_id` still open?
137///
138/// Decided from the question store, not from the newest turn's wording: the
139/// owner's decision usually arrives in a later turn that carries no hand-over
140/// heading. Answered or abandoned questions and ended consultations stop
141/// counting.
142pub fn pending_consults(questions: &Questions, talk_id: &str) -> bool {
143    questions
144        .list_for_effects()
145        .iter()
146        .any(|q| active_consult(q, talk_id))
147}
148
149/// Consultation activity is separate from the question's waiting state.
150pub fn active_consult(q: &Question, talk_id: &str) -> bool {
151    q.routed_task.is_none()
152        && q.status.open()
153        && q.consult_outcome.is_none()
154        && q.consult.as_ref().is_some_and(|c| c.talk == talk_id)
155}
156
157/// A non-answer never wakes a waiter or executes a choice action.
158#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
159pub struct ConsultOutcome {
160    /// The owner's outcome, with no execution effect.
161    pub text: String,
162    /// Originating conversation.
163    pub talk: String,
164    /// Time consultation ended.
165    pub at: Timestamp,
166}
167
168/// Store the owner's alternative as a note and end only the consultation:
169/// the question stays open and its waiter stays held.
170pub fn record_other(q: &mut Question, text: &str) -> Result<()> {
171    if text.trim().is_empty() || q.free_text() || q.choices.iter().any(|c| c == text) {
172        bail!("--reply-other requires a nonempty outcome outside the offered choices");
173    }
174    let talk = match &q.consult {
175        Some(consult) if active_consult(q, &consult.talk) => consult.talk.clone(),
176        _ => bail!("question {} has no active consultation to end", q.short()),
177    };
178    q.schema = crate::ask::SCHEMA;
179    q.consult_outcome = Some(ConsultOutcome {
180        text: text.to_owned(),
181        talk,
182        at: Timestamp::now(),
183    });
184    Ok(())
185}
186
187/// `magi answer --reply-other` on question `id` (a full id): [`record_other`]
188/// under the store's lock, then what to print. Shared by the terminal's direct
189/// path and a chat turn's control bridge.
190pub fn reply_other(store: &Questions, id: &str, text: &str) -> Result<String> {
191    let (q, ()) = store.update(id, |r| record_other(r, text))?;
192    Ok(format!(
193        "consultation ended for {}; decision recorded as a note. Question remains open and its waiter remains held; no choice action executed.",
194        q.short()
195    ))
196}
197
198/// Record the hand-over and queue the question as the chat's next message.
199///
200/// Returns `false` (and changes nothing) when the question was already handed
201/// over. The question is not otherwise touched: still `Open`, no thread turn.
202/// When queueing fails the record is withdrawn, so the owner can try again.
203pub fn begin(questions: &Questions, talks: &Talks, q: &Question, talk: &Talk) -> Result<bool> {
204    let (q, fresh) = questions.update(&q.id, |r| {
205        anyhow::ensure!(
206            r.routed_task.is_none(),
207            "task decision is delivered by its own chat turn"
208        );
209        if !r.status.open() {
210            bail!("question {} is already {}", r.short(), r.status.as_str());
211        }
212        if r.consult.is_some() {
213            return Ok(false);
214        }
215        r.consult = Some(ChatConsult {
216            talk: talk.id.clone(),
217            at: Timestamp::now(),
218        });
219        Ok(true)
220    })?;
221    if !fresh {
222        return Ok(false);
223    }
224    // Serialize dispatch with answers, but never wait for conversation locks
225    // under the expiring question lock. Each retry reloads the latest question.
226    let deadline = std::time::Instant::now() + std::time::Duration::from_secs(20);
227    let dispatch = loop {
228        let mut busy = false;
229        let updated = questions.update(&q.id, |r| {
230            if !r.status.open() || r.consult != q.consult {
231                return Ok(false);
232            }
233            if talk::try_queue_consult(
234                talks,
235                &talk.id,
236                &crate::prompt::chat_consult(r),
237                Some(crate::prompt::chat_consult_display(r)),
238            )?
239            .is_none()
240            {
241                busy = true;
242                bail!("the conversation store is locked by another process");
243            }
244            Ok(true)
245        });
246        if busy && std::time::Instant::now() < deadline {
247            std::thread::sleep(std::time::Duration::from_millis(10));
248            continue;
249        }
250        break updated;
251    };
252    if let Err(e) = dispatch {
253        let _ = questions.update(&q.id, |r| {
254            if r.status.open() && r.consult == q.consult {
255                r.consult = None;
256            }
257            Ok(())
258        });
259        return Err(e).context("queue the question into the chat");
260    }
261    Ok(dispatch?.1)
262}
263
264/// What [`start_turn`] did.
265#[derive(Debug, PartialEq, Eq)]
266pub enum Started {
267    /// The question was handed over and the chat's turn ran to its end.
268    Answered,
269    /// Another process holds the talk's turn lease; nothing was changed.
270    Busy,
271    /// The question had already been handed to the chat; nothing was changed.
272    Nothing,
273}
274
275/// [`begin`], then run the chat's turn here, under the talk's own lease.
276///
277/// The lease is taken *before* anything is written, so a talk somebody else is
278/// running leaves no consult record and no draft behind (`Started::Busy`) and
279/// the caller can simply try again later. A turn that fails after `begin`
280/// leaves the record and the draft, so the chat can resume it.
281pub async fn start_turn(
282    questions: &Questions,
283    talks: &Talks,
284    q: &Question,
285    talk: &Talk,
286    cfg: &Config,
287) -> Result<Started> {
288    if q.consult.is_some() {
289        return Ok(Started::Nothing);
290    }
291    let Some(lease) = talks.claim_turn(&talk.id)? else {
292        return Ok(Started::Busy);
293    };
294    if !begin(questions, talks, q, talk)? {
295        return Ok(Started::Nothing);
296    }
297    crate::chat_worker::drain_turns(talks, &talk.id, lease, cfg).await?;
298    Ok(Started::Answered)
299}
300
301/// 表示専用の生存確認。ワーカーストアの排他ロックやレコード修復を行わない。
302/// 送信可否の判断には引き続き [`crate::chat_worker::turn_worker_active`] を使う。
303pub fn turn_worker_observed(talks: &Talks, talk_id: &str) -> bool {
304    crate::worker::observe_active(talks.home()).is_ok_and(|jobs| {
305        jobs.iter().any(|job| {
306            job.kind == "chat"
307                && job.payload.get("talk").and_then(serde_json::Value::as_str) == Some(talk_id)
308        })
309    })
310}
311
312#[derive(serde::Serialize, serde::Deserialize)]
313struct ServiceRequest {
314    service: String,
315    repo: std::path::PathBuf,
316    cfg: Option<Config>,
317    #[serde(default)]
318    explicit_config: Option<std::path::PathBuf>,
319}
320
321fn service_due(home: &std::path::Path, service: &str, now: Timestamp) -> bool {
322    let interval = match service {
323        "waiter" => crate::waiter::TICK,
324        "deputies" => crate::deputy::TICK,
325        "release-watch" => crate::release_watch::LAP,
326        _ => return false,
327    };
328    std::fs::read_to_string(home.join("service-ticks").join(service))
329        .ok()
330        .and_then(|text| text.parse::<i64>().ok())
331        .is_none_or(|last| now.as_second().saturating_sub(last) >= interval.as_secs() as i64)
332}
333
334fn service_has_work(
335    home: &std::path::Path,
336    repo: &std::path::Path,
337    cfg: Option<&Config>,
338    service: &str,
339    now: Timestamp,
340) -> bool {
341    match service {
342        "waiter" => crate::waiter::Waiter::new(
343            Questions::at(home.join("questions")),
344            home.to_path_buf(),
345            cfg.cloned(),
346        )
347        .has_work(now),
348        "deputies" => crate::deputy::Deputies::new(
349            Questions::at(home.join("questions")),
350            home.to_path_buf(),
351            cfg.cloned(),
352            repo.to_path_buf(),
353            cfg.map_or(0, |cfg| cfg.daemon.max_deputies),
354            std::sync::Arc::new(|| false),
355        )
356        .has_work(now),
357        "release-watch" => cfg.is_some_and(|cfg| cfg.daemon.release_stall_minutes > 0),
358        _ => false,
359    }
360}
361
362/// Finite service work outlives the scheduler, but never pins a service host
363/// to an obsolete executable. Its next tick is started by the new scheduler.
364pub async fn start_services(
365    home: &std::path::Path,
366    repo: &std::path::Path,
367    cfg: Option<&Config>,
368    explicit_config: Option<&std::path::Path>,
369) -> Result<()> {
370    let active = crate::worker::active(home)?;
371    for service in ["waiter", "deputies", "release-watch"] {
372        let key = format!("service:{service}");
373        // Adoption is by operation key, not by the new scheduler's config
374        // payload/schema. An old invocation owns its started request.
375        if active.iter().any(|job| job.key == key) {
376            continue;
377        }
378        if !service_due(home, service, Timestamp::now()) {
379            continue;
380        }
381        let service_cfg = if service == "release-watch" {
382            // The original watcher reloaded settings each lap. Do this cheap
383            // admission read here so disabled/idle services never fork a CLI.
384            Config::discover(repo, explicit_config)
385                .ok()
386                .map(|(cfg, _)| cfg)
387        } else {
388            cfg.cloned()
389        };
390        if !service_has_work(home, repo, service_cfg.as_ref(), service, Timestamp::now()) {
391            let stamps = home.join("service-ticks");
392            std::fs::create_dir_all(&stamps)?;
393            std::fs::write(
394                stamps.join(service),
395                Timestamp::now().as_second().to_string(),
396            )?;
397            continue;
398        }
399        let payload = serde_json::to_value(ServiceRequest {
400            service: service.to_owned(),
401            repo: repo.to_path_buf(),
402            cfg: service_cfg,
403            explicit_config: explicit_config.map(std::path::Path::to_path_buf),
404        })?;
405        if let Err(error) = crate::worker::spawn(home, "service", &key, payload).await {
406            tracing::warn!("service {service} worker could not start: {error:#}");
407        }
408    }
409    Ok(())
410}
411
412/// One leased service tick. Existing question leases, seat records, and release
413/// receipts remain the only authorities for delivery and once-only effects.
414pub async fn run_service_worker(home: &std::path::Path, payload: serde_json::Value) -> Result<()> {
415    let request: ServiceRequest = serde_json::from_value(payload)?;
416    anyhow::ensure!(
417        matches!(
418            request.service.as_str(),
419            "waiter" | "deputies" | "release-watch"
420        ),
421        "unknown conversation service"
422    );
423    let stamps = home.join("service-ticks");
424    std::fs::create_dir_all(&stamps)?;
425    std::fs::write(
426        stamps.join(&request.service),
427        Timestamp::now().as_second().to_string(),
428    )?;
429    let questions = Questions::at(home.join("questions"));
430    match request.service.as_str() {
431        "waiter" => {
432            crate::waiter::Waiter::new(questions, home.to_path_buf(), request.cfg)
433                .tick(Timestamp::now(), &|| false)
434                .await;
435        }
436        "deputies" => {
437            let max = request.cfg.as_ref().map_or_else(
438                || Config::default().daemon.max_deputies,
439                |cfg| cfg.daemon.max_deputies,
440            );
441            let deputies = crate::deputy::Deputies::new(
442                questions,
443                home.to_path_buf(),
444                request.cfg,
445                request.repo,
446                max,
447                std::sync::Arc::new(|| false),
448            );
449            deputies.dispatch(Timestamp::now()).await?;
450        }
451        "release-watch" => {
452            if let Ok((cfg, _)) =
453                Config::discover(&request.repo, request.explicit_config.as_deref())
454            {
455                let (repos, minutes) =
456                    crate::daemon::release_watch_settings_from_config(home, &request.repo, &cfg);
457                if minutes > 0 {
458                    crate::release_watch::Watcher::new(
459                        Box::new(crate::release_watch::GhForge),
460                        home.to_path_buf(),
461                    )
462                    .lap(
463                        &repos,
464                        (minutes as i64).saturating_mul(60),
465                        Timestamp::now().as_second(),
466                        &|| false,
467                    )
468                    .await;
469                }
470            }
471        }
472        _ => unreachable!("validated service"),
473    }
474    // Preserve the delay after completion even when the scheduler changes
475    // executable or restarts while this tick is still running.
476    std::fs::write(
477        stamps.join(&request.service),
478        Timestamp::now().as_second().to_string(),
479    )?;
480    Ok(())
481}
482
483#[cfg(test)]
484mod tests {
485    use std::collections::BTreeMap;
486    use std::path::PathBuf;
487
488    use super::*;
489    use crate::config::{AgentKind, AgentSpec, Config};
490    use crate::queue::{CHAT_NODE, Source};
491    use crate::talk::TalkStatus;
492
493    #[test]
494    fn service_cadence_is_durable_and_keeps_the_watchers_own_interval() {
495        let home = tempfile::tempdir().expect("home");
496        let stamps = home.path().join("service-ticks");
497        std::fs::create_dir_all(&stamps).expect("stamps");
498        for service in ["waiter", "deputies", "release-watch"] {
499            std::fs::write(stamps.join(service), "100").expect("completion timestamp");
500        }
501        let at = |seconds| Timestamp::from_second(seconds).unwrap();
502        assert!(!service_due(home.path(), "waiter", at(104)));
503        assert!(service_due(home.path(), "waiter", at(105)));
504        assert!(!service_due(home.path(), "deputies", at(104)));
505        assert!(service_due(home.path(), "deputies", at(105)));
506        assert!(!service_due(home.path(), "release-watch", at(399)));
507        assert!(service_due(home.path(), "release-watch", at(400)));
508    }
509
510    #[test]
511    fn idle_services_do_not_need_detached_processes() {
512        let home = tempfile::tempdir().expect("home");
513        for service in ["waiter", "deputies", "release-watch"] {
514            assert!(!service_has_work(
515                home.path(),
516                home.path(),
517                None,
518                service,
519                Timestamp::now(),
520            ));
521        }
522        let mut disabled = Config::default();
523        disabled.daemon.release_stall_minutes = 0;
524        assert!(!service_has_work(
525            home.path(),
526            home.path(),
527            Some(&disabled),
528            "release-watch",
529            Timestamp::now(),
530        ));
531    }
532
533    #[tokio::test]
534    async fn explicit_release_config_overrides_the_repository_without_launching_disabled_watchers()
535    {
536        let home = tempfile::tempdir().expect("home");
537        let repo = tempfile::tempdir().expect("repo");
538        let explicit = home.path().join("serve-config.toml");
539        std::fs::write(
540            repo.path().join("magi.toml"),
541            "[daemon]\nrelease_stall_minutes = 60\n",
542        )
543        .expect("repository watcher enabled");
544        std::fs::write(&explicit, "[daemon]\nrelease_stall_minutes = 0\n")
545            .expect("explicit watcher disabled");
546        start_services(home.path(), repo.path(), None, Some(&explicit))
547            .await
548            .expect("idle service admission");
549        let jobs = home.path().join("workers").join("jobs");
550        assert!(
551            std::fs::read_dir(jobs)
552                .map(|mut entries| entries.next().is_none())
553                .unwrap_or(true),
554            "serve --config must not launch the repository-configured watcher"
555        );
556    }
557
558    const RUN: &str = "20260902-000000-beef";
559
560    fn talks() -> (tempfile::TempDir, Talks, Talk) {
561        let tmp = tempfile::tempdir().expect("tempdir");
562        let store = Talks::at(tmp.path().join("talks"));
563        let cfg = Config {
564            agents: vec![AgentSpec {
565                id: "mock".to_owned(),
566                kind: AgentKind::Command,
567                model: None,
568                command: vec!["true".to_owned()],
569                extra_args: Vec::new(),
570                env: BTreeMap::new(),
571                prompt_delivery: None,
572                max_turn_secs: None,
573            }],
574            ..Config::default()
575        };
576        let talk = talk::begin(&store, &cfg, tmp.path().to_path_buf(), Some("mock")).expect("talk");
577        (tmp, store, talk)
578    }
579
580    fn task(source: Source) -> Task {
581        let mut t = Task::new(
582            "t".to_owned(),
583            "Do it".to_owned(),
584            PathBuf::from("/repo"),
585            source,
586        );
587        t.start(RUN.to_owned());
588        t
589    }
590
591    fn from_chat(talk: &Talk) -> Task {
592        task(Source::Agent {
593            run: talk.id.clone(),
594            node: CHAT_NODE.to_owned(),
595        })
596    }
597
598    fn question(node: &str) -> Question {
599        Question::new(
600            RUN.to_owned(),
601            node.to_owned(),
602            "impl-A".to_owned(),
603            "Which backend?".to_owned(),
604            "SQLite is simpler.".to_owned(),
605            vec!["SQLite".to_owned(), "Redis".to_owned()],
606        )
607    }
608
609    #[tokio::test]
610    async fn start_turn_is_busy_and_writes_nothing_while_the_lease_is_held() {
611        let (tmp, store, talk) = talks();
612        let questions = Questions::at(tmp.path().join("questions"));
613        let mut q = question("implement");
614        questions.put(&mut q).expect("put");
615        let held = store.claim_turn(&talk.id).expect("claim").expect("first");
616
617        let got = start_turn(&questions, &store, &q, &talk, &Config::default())
618            .await
619            .expect("start");
620        assert_eq!(got, Started::Busy);
621        assert!(questions.get(&q.id).unwrap().consult.is_none());
622        assert!(store.get(&talk.id).unwrap().pending.is_empty());
623        drop(held);
624    }
625
626    /// A `kind = "command"` agent running `script`; `LEASE` names the turn
627    /// lease file so the script can look at it while the turn is in flight.
628    fn scripted(tmp: &std::path::Path, script: &str, lease: &std::path::Path) -> Config {
629        let path = tmp.join("mock-consult-agent.sh");
630        std::fs::write(&path, script).expect("write mock");
631        Config {
632            agents: vec![AgentSpec {
633                id: "mock".to_owned(),
634                kind: AgentKind::Command,
635                model: None,
636                command: vec!["sh".to_owned(), path.to_string_lossy().into_owned()],
637                extra_args: Vec::new(),
638                env: BTreeMap::from([("LEASE".to_owned(), lease.to_string_lossy().into_owned())]),
639                prompt_delivery: None,
640                max_turn_secs: None,
641            }],
642            ..Config::default()
643        }
644    }
645
646    /// Run `start_turn` with `script` as the agent and hand back its result,
647    /// the store and the talk, for the caller to claim the lease afterwards.
648    async fn run_turn(
649        script: &str,
650    ) -> (
651        tempfile::TempDir,
652        Result<Started>,
653        Questions,
654        Question,
655        Talk,
656    ) {
657        crate::run::set_home(crate::run::test_home());
658        let (tmp, store, talk) = talks();
659        let questions = Questions::at(tmp.path().join("questions"));
660        let mut q = question("implement");
661        questions.put(&mut q).expect("put");
662        let cfg = scripted(tmp.path(), script, &store.turn_path(&talk.id));
663        let got = start_turn(&questions, &store, &q, &talk, &cfg).await;
664        (tmp, got, questions, q, talk)
665    }
666
667    #[tokio::test]
668    async fn the_lease_is_held_during_the_turn_and_free_once_it_ends() {
669        // exit 7 if the lease file is missing while the agent runs.
670        let (tmp, got, _questions, _q, talk) =
671            run_turn("#!/bin/sh\ncat >/dev/null\n[ -f \"$LEASE\" ] || exit 7\nprintf 'ok\\n'\n")
672                .await;
673        assert_eq!(got.expect("turn"), Started::Answered);
674        let other = Talks::at(tmp.path().join("talks"));
675        assert!(
676            other.claim_turn(&talk.id).expect("claim").is_some(),
677            "free once the turn ended"
678        );
679    }
680
681    #[tokio::test]
682    async fn the_lease_is_free_again_after_a_failed_turn() {
683        let (tmp, got, questions, q, talk) = run_turn("#!/bin/sh\ncat >/dev/null\nexit 3\n").await;
684        assert!(got.is_err(), "the agent failed");
685        assert!(
686            questions.get(&q.id).unwrap().consult.is_some(),
687            "the turn got as far as running"
688        );
689        let other = Talks::at(tmp.path().join("talks"));
690        assert!(
691            other.claim_turn(&talk.id).expect("claim").is_some(),
692            "free after the failed turn"
693        );
694    }
695
696    #[test]
697    fn origin_talk_is_decided_in_one_table() {
698        let (_tmp, _store, talk) = talks();
699        let mut closed = talk.clone();
700        closed.status = TalkStatus::Closed;
701        let chat = from_chat(&talk);
702        let q = question("implement");
703
704        let hit = |tasks: &[Task], talks: &[Talk], q: &Question| origin_talk(tasks, talks, q);
705        assert_eq!(
706            hit(std::slice::from_ref(&chat), std::slice::from_ref(&talk), &q).map(|t| t.id),
707            Some(talk.id.clone())
708        );
709        // Not filed from a chat.
710        assert!(hit(&[task(Source::Human)], std::slice::from_ref(&talk), &q).is_none());
711        let other = task(Source::Agent {
712            run: talk.id.clone(),
713            node: "implement".to_owned(),
714        });
715        assert!(hit(&[other], std::slice::from_ref(&talk), &q).is_none());
716        // The conversation is gone, or no longer takes turns.
717        assert!(hit(std::slice::from_ref(&chat), &[], &q).is_none());
718        // A closed conversation is reopened by `begin`, so it still qualifies.
719        assert!(hit(std::slice::from_ref(&chat), &[closed], &q).is_some());
720        // No task owns the question.
721        assert!(hit(&[], std::slice::from_ref(&talk), &q).is_none());
722        // Merge approvals can be consulted; release notices cannot.
723        assert!(
724            hit(
725                std::slice::from_ref(&chat),
726                std::slice::from_ref(&talk),
727                &question(crate::land::APPROVAL_NODE)
728            )
729            .is_some()
730        );
731        assert!(
732            hit(
733                std::slice::from_ref(&chat),
734                std::slice::from_ref(&talk),
735                &question(crate::bump::NOTICE_NODE)
736            )
737            .is_none()
738        );
739        // A settled question has nothing left to ask.
740        let mut answered = question("implement");
741        answered
742            .answer(crate::ask::Answer::Choice("Redis".to_owned()))
743            .unwrap();
744        assert!(hit(&[chat], &[talk], &answered).is_none());
745    }
746
747    #[test]
748    fn associated_chat_remains_navigable_after_consultation_and_settlement() {
749        let (tmp, store, mut talk) = talks();
750        let chat = from_chat(&talk);
751        let tasks = std::slice::from_ref(&chat);
752        let mut q = question("implement");
753        let questions = Questions::at(tmp.path().join("questions"));
754        questions.put(&mut q).unwrap();
755        assert!(begin(&questions, &store, &q, &talk).unwrap());
756        q = questions.get(&q.id).unwrap();
757        let associated = |q: &Question, talk: &Talk| {
758            associated_talk(tasks, std::slice::from_ref(talk), q).map(|t| t.id)
759        };
760        assert_eq!(associated(&q, &talk), Some(talk.id.clone()));
761        assert!(q.status.open());
762        assert!(q.answer.is_none());
763        assert!(origin_talk(tasks, std::slice::from_ref(&talk), &q).is_some());
764        q.answer(crate::ask::Answer::Choice("Redis".into()))
765            .unwrap();
766        assert_eq!(associated(&q, &talk), Some(talk.id.clone()));
767        assert!(origin_talk(tasks, std::slice::from_ref(&talk), &q).is_none());
768        talk.status = TalkStatus::Closed;
769        assert_eq!(associated(&q, &talk), Some(talk.id.clone()));
770        let mut q = question("implement");
771        q.abandon("expired");
772        assert_eq!(associated(&q, &talk), Some(talk.id.clone()));
773        assert!(origin_talk(tasks, std::slice::from_ref(&talk), &q).is_none());
774        assert!(associated_talk(tasks, &[], &q).is_none());
775        assert!(associated_talk(&[], std::slice::from_ref(&talk), &q).is_none());
776        let notice = question(crate::bump::NOTICE_NODE);
777        assert_eq!(associated(&notice, &talk), Some(talk.id.clone()));
778        assert!(origin_talk(tasks, &[talk], &notice).is_none());
779    }
780
781    #[test]
782    fn approval_origin_requires_an_open_chat_task_and_question() {
783        let (_tmp, _store, talk) = talks();
784        let chat = from_chat(&talk);
785        let mut q = question(crate::land::APPROVAL_NODE);
786        assert!(origin_talk(&[task(Source::Human)], std::slice::from_ref(&talk), &q).is_none());
787        assert!(
788            origin_talk(
789                &[task(Source::Agent {
790                    run: talk.id.clone(),
791                    node: "implement".into(),
792                })],
793                std::slice::from_ref(&talk),
794                &q
795            )
796            .is_none()
797        );
798        assert!(origin_talk(std::slice::from_ref(&chat), &[], &q).is_none());
799        let mut closed = talk.clone();
800        closed.status = TalkStatus::Closed;
801        assert!(origin_talk(std::slice::from_ref(&chat), &[closed], &q).is_some());
802        q.abandon("expired");
803        assert!(
804            origin_talk(std::slice::from_ref(&chat), std::slice::from_ref(&talk), &q).is_none()
805        );
806        let mut q = question(crate::land::APPROVAL_NODE);
807        q.answer(crate::ask::Answer::Choice("Redis".into()))
808            .unwrap();
809        assert!(origin_talk(&[chat], &[talk], &q).is_none());
810    }
811
812    #[test]
813    fn a_conductor_question_is_found_through_the_task_id() {
814        let (_tmp, _store, talk) = talks();
815        let chat = from_chat(&talk);
816        let mut q = question(crate::conduct::NODE);
817        q.run = chat.id.clone();
818        assert!(origin_talk(&[chat], &[talk], &q).is_some());
819    }
820
821    fn followup_of(parent: &Task, n: u32) -> Task {
822        let mut t = Task::new(
823            "f".to_owned(),
824            "Fix".to_owned(),
825            PathBuf::from("/repo"),
826            Source::Agent {
827                run: format!("merged-{n}"),
828                node: "followup".to_owned(),
829            },
830        );
831        t.start(format!("run-f{n}"));
832        t.followup = Some(crate::queue::FollowUp {
833            run: format!("merged-{n}"),
834            origin_task: Some(parent.id.clone()),
835            pr: "https://example.invalid/pr/1".to_owned(),
836            findings: Vec::new(),
837            generation: n,
838        });
839        t
840    }
841
842    fn q_for(t: &Task) -> Question {
843        let mut q = question("implement");
844        q.run = t.runs[0].clone();
845        q
846    }
847
848    #[test]
849    fn a_followup_traces_back_to_the_chat() {
850        let (_tmp, _store, talk) = talks();
851        let mut chat = from_chat(&talk);
852        chat.runs = vec!["chat-run".to_owned()];
853        let f1 = followup_of(&chat, 1);
854        let f2 = followup_of(&f1, 2);
855        let ts = [chat, f1.clone(), f2.clone()];
856        let id = Some(talk.id.clone());
857        let tk = std::slice::from_ref(&talk);
858        assert_eq!(origin_talk(&ts, tk, &q_for(&f1)).map(|t| t.id), id);
859        assert_eq!(origin_talk(&ts, tk, &q_for(&f2)).map(|t| t.id), id);
860    }
861
862    #[test]
863    fn a_followup_without_origin_task_is_found_through_runs() {
864        let (_tmp, _store, talk) = talks();
865        let mut chat = from_chat(&talk);
866        chat.runs = vec!["merged-1".to_owned()];
867        let mut f1 = followup_of(&chat, 1);
868        f1.followup.as_mut().unwrap().origin_task = None;
869        let q = q_for(&f1);
870        assert!(origin_talk(&[chat, f1], &[talk], &q).is_some());
871    }
872
873    #[test]
874    fn a_dangling_origin_task_has_no_chat() {
875        let (_tmp, _store, talk) = talks();
876        let mut chat = from_chat(&talk);
877        chat.runs = vec!["chat-run".to_owned()];
878        let mut f1 = followup_of(&chat, 1);
879        f1.followup.as_mut().unwrap().origin_task = Some("gone".to_owned());
880        let q = q_for(&f1);
881        assert!(origin_talk(&[chat, f1], &[talk], &q).is_none());
882    }
883
884    #[test]
885    fn a_recorded_chat_survives_deleted_ancestors() {
886        let (_tmp, _store, talk) = talks();
887        let chat = from_chat(&talk);
888        assert_eq!(chat.origin_chat.as_deref(), Some(talk.id.as_str()));
889        let mut f1 = followup_of(&chat, 1);
890        f1.origin_chat = chat.origin_chat.clone();
891        let mut f2 = followup_of(&f1, 2);
892        f2.origin_chat = f1.origin_chat.clone();
893        // Neither the chat task nor f1 is in the queue any more.
894        let q = q_for(&f2);
895        let got = origin_talk(std::slice::from_ref(&f2), std::slice::from_ref(&talk), &q);
896        assert_eq!(got.map(|t| t.id), Some(talk.id.clone()));
897        // The recorded id still has to name an open chat.
898        assert!(origin_talk(&[f2], &[], &q).is_none());
899    }
900
901    #[test]
902    fn a_task_without_the_field_still_walks_the_ancestry() {
903        let (_tmp, _store, talk) = talks();
904        let mut chat = from_chat(&talk);
905        chat.origin_chat = None;
906        let mut f1 = followup_of(&chat, 1);
907        f1.origin_chat = None;
908        let q = q_for(&f1);
909        assert!(origin_talk(&[chat, f1.clone()], std::slice::from_ref(&talk), &q).is_some());
910        // And the old JSON shape reads with the field absent.
911        let mut v = serde_json::to_value(&f1).unwrap();
912        v.as_object_mut().unwrap().remove("origin_chat");
913        let back: Task = serde_json::from_value(v).unwrap();
914        assert!(back.origin_chat.is_none());
915    }
916
917    #[test]
918    fn a_followup_cycle_ends_without_a_chat() {
919        let (_tmp, _store, talk) = talks();
920        let mut a = followup_of(&from_chat(&talk), 1);
921        let mut b = followup_of(&a, 2);
922        a.followup.as_mut().unwrap().origin_task = Some(b.id.clone());
923        b.followup.as_mut().unwrap().origin_task = Some(a.id.clone());
924        let q = q_for(&a);
925        assert!(origin_talk(&[a, b], &[talk], &q).is_none());
926    }
927
928    #[test]
929    fn pending_consults_follow_the_store_not_the_turn_text() {
930        let (tmp, store, talk) = talks();
931        let questions = Questions::at(tmp.path().join("questions"));
932        assert!(!pending_consults(&questions, &talk.id), "empty store");
933
934        let mut plain = question("implement");
935        questions.put(&mut plain).unwrap();
936        assert!(!pending_consults(&questions, &talk.id), "no consult record");
937
938        let mut q = question("implement");
939        questions.put(&mut q).unwrap();
940        begin(&questions, &store, &q, &talk).unwrap();
941        assert!(pending_consults(&questions, &talk.id));
942        assert!(!pending_consults(&questions, "other-talk"));
943
944        questions
945            .update(&q.id, |q| {
946                q.abandon("test");
947                Ok(())
948            })
949            .unwrap();
950        assert!(!pending_consults(&questions, &talk.id), "closed question");
951    }
952
953    #[test]
954    fn begin_reopens_a_closed_chat_before_queueing() {
955        let (tmp, store, mut talk) = talks();
956        let questions = Questions::at(tmp.path().join("questions"));
957        let mut q = question("implement");
958        questions.put(&mut q).unwrap();
959        talk::close(&mut talk, &store).unwrap();
960        assert!(!store.get(&talk.id).unwrap().status.open());
961
962        assert!(begin(&questions, &store, &q, &talk).unwrap());
963
964        let after = store.get(&talk.id).unwrap();
965        assert!(after.status.open(), "the chat was reopened");
966        assert!(after.pending.contains(&q.id), "{}", after.pending);
967        assert!(questions.get(&q.id).unwrap().consult.is_some());
968    }
969
970    #[test]
971    fn begin_queues_once_and_leaves_the_question_open() {
972        let (tmp, store, talk) = talks();
973        let questions = Questions::at(tmp.path().join("questions"));
974        let mut q = question("implement");
975        questions.put(&mut q).unwrap();
976
977        assert!(begin(&questions, &store, &q, &talk).unwrap());
978        assert!(!begin(&questions, &store, &q, &talk).unwrap(), "idempotent");
979
980        let after = questions.get(&q.id).unwrap();
981        assert!(after.status.open());
982        assert!(after.thread.is_empty());
983        assert_eq!(after.choices, q.choices, "choices are not touched");
984        assert_eq!(
985            after.consult.as_ref().map(|c| c.talk.as_str()),
986            Some(talk.id.as_str())
987        );
988
989        let destination = store.get(&talk.id).unwrap();
990        assert_eq!(destination.pending_messages.len(), 1);
991        assert_eq!(
992            destination.pending_messages[0].display,
993            Some(crate::prompt::chat_consult_display(&q))
994        );
995        let queued = destination.pending;
996        assert_eq!(
997            queued.matches(&q.id).count(),
998            1 + 3,
999            "id once per use: {queued}"
1000        );
1001        assert!(queued.contains("Which backend?"));
1002        assert!(queued.contains("SQLite is simpler."));
1003        assert!(queued.contains("- Redis"));
1004        // Queued once, so the first-operator-turn title is not involved.
1005        assert_eq!(
1006            queued.matches(crate::prompt::CHAT_CONSULT_HEADING).count(),
1007            1
1008        );
1009    }
1010
1011    #[test]
1012    fn chat_consult_has_each_marker_once() {
1013        use crate::prompt::{CHAT_CONSULT_END as E, CHAT_CONSULT_HEADING as H};
1014        for node in ["implement", crate::land::APPROVAL_NODE] {
1015            let mut q = question(node);
1016            let quoted = format!("{H} {E}");
1017            q.summary = quoted.clone();
1018            q.detail = quoted.clone();
1019            q.choices = vec![quoted.clone(), "b".into()];
1020            let s = crate::prompt::chat_consult(&q);
1021            assert_eq!(s.matches(H).count(), 1, "{s}");
1022            assert_eq!(s.matches(E).count(), 1, "{s}");
1023        }
1024    }
1025
1026    /// What `magi answer --reply` does under the question lock.
1027    fn record(questions: &Questions, id: &str, reply: &str) -> Result<Question> {
1028        questions
1029            .update(id, |r| {
1030                let label = r.resolve_reply(reply)?.to_owned();
1031                r.answer(crate::ask::Answer::Choice(label))
1032            })
1033            .map(|(q, ())| q)
1034    }
1035
1036    #[test]
1037    fn a_consulted_posting_question_records_the_chats_choice_without_a_quote() {
1038        use crate::github_text::{ASK_NODE, POST_ORIGINAL, USE_FALLBACK, USE_REWRITE};
1039        for choice in [POST_ORIGINAL, USE_REWRITE] {
1040            let (tmp, store, talk) = talks();
1041            let questions = Questions::at(tmp.path().join("questions"));
1042            let mut q = question(ASK_NODE);
1043            q.choices = vec![USE_FALLBACK.into(), choice.into()];
1044            questions.put(&mut q).unwrap();
1045            assert!(begin(&questions, &store, &q, &talk).unwrap());
1046            let queued = store.get(&talk.id).unwrap().pending;
1047            assert!(queued.contains("magi answer"), "{queued}");
1048            assert!(!queued.contains("--quote"), "{queued}");
1049            let other = if choice == POST_ORIGINAL {
1050                USE_REWRITE
1051            } else {
1052                POST_ORIGINAL
1053            };
1054            assert!(record(&questions, &q.id, other).is_err(), "not offered");
1055            let answered = record(&questions, &q.id, choice).unwrap();
1056            assert_eq!(answered.resolution().as_deref(), Some(choice));
1057            assert!(
1058                record(&questions, &q.id, USE_FALLBACK).is_err(),
1059                "a question is answered once"
1060            );
1061        }
1062    }
1063
1064    #[test]
1065    fn merge_repair_consult_records_only_an_offered_choice() {
1066        use crate::land::{APPROVAL_NODE, APPROVE, HOLD, REPAIR};
1067        let (tmp, store, talk) = talks();
1068        let questions = Questions::at(tmp.path().join("questions"));
1069        let mut q = question(APPROVAL_NODE);
1070        q.choices = vec![APPROVE.into(), HOLD.into()];
1071        questions.put(&mut q).unwrap();
1072        begin(&questions, &store, &q, &talk).unwrap();
1073        assert!(record(&questions, &q.id, REPAIR).is_err(), "not offered");
1074        questions
1075            .update(&q.id, |r| {
1076                r.choices.push(REPAIR.into());
1077                Ok(())
1078            })
1079            .unwrap();
1080        let answered = record(&questions, &q.id, REPAIR).unwrap();
1081        assert_eq!(answered.resolution().as_deref(), Some(REPAIR));
1082    }
1083
1084    #[test]
1085    fn approval_consult_records_the_chats_merge_answer() {
1086        let (tmp, store, talk) = talks();
1087        let questions = Questions::at(tmp.path().join("questions"));
1088        let mut q = question(crate::land::APPROVAL_NODE);
1089        q.choices = vec![crate::land::APPROVE.into(), crate::land::HOLD.into()];
1090        questions.put(&mut q).unwrap();
1091        assert!(begin(&questions, &store, &q, &talk).unwrap());
1092        assert!(!begin(&questions, &store, &q, &talk).unwrap());
1093        q = questions.get(&q.id).unwrap();
1094        assert!(q.status.open());
1095        assert!(q.answer.is_none());
1096        assert!(q.thread.is_empty());
1097        let queued = store.get(&talk.id).unwrap().pending;
1098        assert!(queued.contains("Silence holds"), "{queued}");
1099        assert!(queued.contains("--reply merge"), "{queued}");
1100        assert!(!queued.contains("--quote"), "{queued}");
1101
1102        // The chat records the owner's "merge it" as the approval; nothing
1103        // else is needed. The land approval waiter consumes it unchanged.
1104        let answered = record(&questions, &q.id, crate::land::APPROVE).unwrap();
1105        assert_eq!(answered.resolution().as_deref(), Some(crate::land::APPROVE));
1106        assert!(record(&questions, &q.id, crate::land::HOLD).is_err());
1107
1108        // An expired approval cannot be answered.
1109        let mut expired = question(crate::land::APPROVAL_NODE);
1110        expired.choices = q.choices.clone();
1111        expired.abandon("approval expired or head changed");
1112        assert!(
1113            expired
1114                .answer(crate::ask::Answer::Choice(crate::land::APPROVE.into()))
1115                .is_err()
1116        );
1117    }
1118
1119    #[test]
1120    fn reply_other_is_a_note_that_ends_only_the_consultation() {
1121        let (tmp, store, talk) = talks();
1122        let questions = Questions::at(tmp.path().join("questions"));
1123        let mut q = question("implement");
1124        questions.put(&mut q).unwrap();
1125        assert!(
1126            questions
1127                .update(&q.id, |r| record_other(r, "Use Postgres"))
1128                .is_err(),
1129            "nothing to end before consultation"
1130        );
1131        begin(&questions, &store, &q, &talk).unwrap();
1132        assert!(pending_consults(&questions, &talk.id));
1133        for refused in ["", "  ", "SQLite"] {
1134            assert!(
1135                questions
1136                    .update(&q.id, |r| record_other(r, refused))
1137                    .is_err(),
1138                "{refused:?}"
1139            );
1140        }
1141        questions
1142            .update(&q.id, |r| record_other(r, "Use Postgres"))
1143            .unwrap();
1144        let after = questions.get(&q.id).unwrap();
1145        assert!(after.status.open());
1146        assert!(after.answer.is_none());
1147        let outcome = after.consult_outcome.unwrap();
1148        assert_eq!(outcome.text, "Use Postgres");
1149        assert_eq!(outcome.talk, talk.id);
1150        assert!(!pending_consults(&questions, &talk.id));
1151    }
1152
1153    #[test]
1154    fn begin_withdraws_its_record_when_the_talk_cannot_take_it() {
1155        let (tmp, store, talk) = talks();
1156        let questions = Questions::at(tmp.path().join("questions"));
1157        let mut q = question("implement");
1158        questions.put(&mut q).unwrap();
1159        std::fs::remove_dir_all(tmp.path().join("talks")).unwrap();
1160        assert!(begin(&questions, &store, &q, &talk).is_err());
1161        assert!(questions.get(&q.id).unwrap().consult.is_none());
1162    }
1163
1164    #[test]
1165    fn an_older_question_file_reads_without_a_consult() {
1166        let mut q = question("implement");
1167        q.schema = 5;
1168        let mut v = serde_json::to_value(&q).unwrap();
1169        v.as_object_mut().unwrap().remove("consult");
1170        let back: Question = serde_json::from_value(v).unwrap();
1171        assert!(back.consult.is_none());
1172    }
1173}