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