1use std::path::{Path, PathBuf};
19use std::time::{Duration, Instant};
20
21use anyhow::{Context, Result, bail};
22use jiff::Timestamp;
23
24use crate::config::Config;
25use crate::queue::{Claim, Queue, RunOverrides, Source, Task, TaskStatus};
26
27#[derive(Debug, Clone)]
31pub struct Filing {
32 pub instruction: String,
34 pub title: String,
36 pub repo: PathBuf,
38 pub source: Source,
40 pub solo: bool,
42 pub overrides: RunOverrides,
44 pub review_of: Option<String>,
46 pub sources: crate::refs::Sources,
48}
49
50impl Filing {
51 fn into_task(self) -> Task {
52 let mut task = Task::new(self.title, self.instruction, self.repo, self.source);
53 task.sources = self.sources;
54 task.solo = self.solo;
55 task.review_of = self.review_of;
56 let mut overrides = self.overrides;
57 overrides.inherit_daemon_options = false;
58 task.overrides = Some(overrides);
59 task
60 }
61}
62
63#[derive(Debug)]
65pub enum Filed {
66 Daemon {
69 task: Task,
71 pid: Option<u32>,
73 },
74 Standalone {
77 task: Task,
79 claim: Claim,
81 },
82}
83
84pub fn file(
89 queue: &Queue,
90 home: &Path,
91 now: Timestamp,
92 own_pid: u32,
93 filing: Filing,
94 force_local: bool,
95) -> Result<Filed> {
96 let mut task = filing.into_task();
97 let owner = if force_local {
98 None
99 } else {
100 crate::daemon::foreign_loop(crate::daemon::read_status(home).as_ref(), now, own_pid)
101 };
102 match owner {
103 Some(pid) => {
104 task.urgent = true;
105 queue.put(&mut task).context("file the task")?;
106 Ok(Filed::Daemon { task, pid })
107 }
108 None => {
109 let claim = queue.file_claimed(&mut task).context("file the task")?;
112 Ok(Filed::Standalone { task, claim })
113 }
114 }
115}
116
117pub fn config_for(task: &Task, repo: &Path) -> Result<Config> {
120 let path = task.overrides.as_ref().and_then(|o| o.config.as_deref());
121 let (mut cfg, _) = Config::discover(repo, path)?;
122 if let Some(o) = &task.overrides {
123 o.apply(&mut cfg);
124 }
125 if task.solo {
126 cfg.graph.implementers = 1;
127 }
128 Ok(cfg)
129}
130
131#[derive(Debug)]
133pub struct Followed {
134 pub task: Task,
136 pub finished: bool,
139}
140
141impl Followed {
142 pub fn unfinished(&self) -> Option<anyhow::Error> {
146 if self.finished {
147 return None;
148 }
149 Some(anyhow::anyhow!(
150 "task {id} has not finished: it is still running under the loop. \
151 This is not a success; the outcome is not known yet. Read it with \
152 `magi task show {id}`. The loop keeps the task, so do not run it again.",
153 id = self.task.short()
154 ))
155 }
156}
157
158pub async fn follow(
169 queue: &Queue,
170 home: &Path,
171 id: &str,
172 poll: Duration,
173 max_wait: Option<Duration>,
174 mut seen: impl FnMut(&str),
175 mut progress: impl FnMut(&str),
176) -> Result<Followed> {
177 let began = Instant::now();
178 let mut announced = 0usize;
179 let mut last_status = String::new();
180 loop {
181 let task = queue.get(id)?;
182 for run in task.runs.iter().skip(announced) {
183 seen(run);
184 }
185 announced = task.runs.len();
186 if let Some(run) = task.runs.last()
187 && crate::run::try_home().is_some()
188 && let Ok(state) = crate::run::RunState::load(run)
189 {
190 let line = format!("run {}: {}", state.short(), state.status.as_str());
191 if line != last_status {
192 progress(&line);
193 last_status = line;
194 }
195 }
196 if matches!(
197 task.status,
198 TaskStatus::Done | TaskStatus::Held | TaskStatus::Blocked
199 ) {
200 return Ok(Followed {
201 task,
202 finished: true,
203 });
204 }
205 let reading = crate::daemon::read_status(home);
206 if crate::daemon::foreign_loop(reading.as_ref(), Timestamp::now(), std::process::id())
207 .is_none()
208 && !crate::daemon::is_working_on_task(home, id, Timestamp::now())
209 {
210 bail!(
211 "no magi loop is serving the queue any more, and task {} is still {}; it was \
212 not taken over here (the loop may only be slow to heartbeat). Start `magi \
213 serve` to carry on, or follow it with `magi task show {}`",
214 task.short(),
215 task.status.as_str(),
216 task.short()
217 );
218 }
219 if max_wait.is_some_and(|m| began.elapsed() >= m) {
220 return Ok(Followed {
221 task,
222 finished: false,
223 });
224 }
225 tokio::time::sleep(poll).await;
226 }
227}
228
229#[must_use]
231pub fn resume_owner(queue: &Queue, run: &str) -> Option<Task> {
232 queue
233 .list()
234 .into_iter()
235 .find(|task| task.runs.iter().any(|id| id == run))
236}
237
238pub fn claim_resume_owner(
242 queue: &Queue,
243 run: &str,
244 instruction: &str,
245 repo: &Path,
246 source: Source,
247 sources: &crate::refs::Sources,
248) -> Result<(Task, Claim)> {
249 if let Some(listed) = resume_owner(queue, run) {
250 let claim = queue.claim(&listed.id)?;
251 let task = queue.get(&listed.id)?;
252 anyhow::ensure!(task.execution_run(run), "run queue owner changed");
253 return Ok((task, claim));
254 }
255 let mut task = Task::new(
256 crate::queue::title_from(instruction, 72),
257 instruction.to_owned(),
258 repo.to_path_buf(),
259 source,
260 );
261 task.sources = sources.clone();
262 task.link_run(run);
263 let claim = queue.file_claimed(&mut task)?;
264 Ok((task, claim))
265}
266
267#[derive(Debug)]
270pub struct Adopted {
271 queue: Queue,
272 task: Option<Task>,
275 claim: Option<Claim>,
276 quota_before: Vec<crate::run::QuotaLoss>,
277}
278
279pub fn adopt(
288 state: &crate::run::RunState,
289 parent_task: Option<&str>,
290) -> anyhow::Result<Option<Adopted>> {
291 if crate::run::try_home().is_none() {
292 return Ok(None);
293 }
294 adopt_in(Queue::open(), state, parent_task)
295}
296
297fn adopt_in(
298 queue: Queue,
299 state: &crate::run::RunState,
300 parent_task: Option<&str>,
301) -> anyhow::Result<Option<Adopted>> {
302 use anyhow::Context as _;
303 if let Some(parent) = parent_task
304 && let Ok(id) = queue.resolve_id(parent)
305 {
306 return Ok(match queue.claim(&id) {
308 Ok(claim) => {
309 let started = queue.get(&id).and_then(|mut task| {
310 if !(task.status.runnable() || task.status == TaskStatus::Held) {
318 return Ok(None);
319 }
320 task.start(state.id.clone());
321 queue.put(&mut task)?;
322 Ok(Some(task))
323 });
324 match started {
325 Ok(None) => {
326 drop(claim);
327 let _ = queue.link_run(&id, &state.id);
328 None
329 }
330 Ok(Some(task)) => Some(Adopted {
331 queue,
332 task: Some(task),
333 claim: Some(claim),
334 quota_before: state.quota.clone(),
335 }),
336 Err(e) => {
337 tracing::warn!("could not start task {id} for run {}: {e:#}", state.id);
338 drop(claim);
339 let _ = queue.link_run(&id, &state.id);
340 None
341 }
342 }
343 }
344 Err(e) => {
345 tracing::warn!("task {id} is claimed elsewhere ({e:#}); linking run only");
347 let _ = queue.link_run(&id, &state.id);
348 None
349 }
350 });
351 }
352 let mut task = Task::new(
353 crate::queue::title_from(&state.instruction, 72),
354 state.instruction.clone(),
355 state.repo.clone(),
356 Source::Human,
357 );
358 task.start(state.id.clone());
359 let claim = queue
360 .file_claimed(&mut task)
361 .with_context(|| format!("could not file a new task for run {}", state.id))?;
362 Ok(Some(Adopted {
363 queue,
364 task: Some(task),
365 claim: Some(claim),
366 quota_before: state.quota.clone(),
367 }))
368}
369
370impl Adopted {
371 pub fn finish(mut self, state: &crate::run::RunState, result: Result<()>) {
373 if let Some(task) = &mut self.task {
374 crate::daemon::finish_attempt(
375 crate::daemon::Opts::default().max_attempts,
376 &self.queue,
377 task,
378 state,
379 &self.quota_before,
380 result,
381 );
382 crate::daemon::hold_if_runnable(&self.queue, task);
383 }
384 drop(self.claim.take());
385 }
386}
387
388#[cfg(test)]
389mod tests {
390 use super::*;
391 use crate::daemon::Status;
392
393 fn filing(repo: &Path) -> Filing {
394 Filing {
395 instruction: "review the branch".to_owned(),
396 title: "review x".to_owned(),
397 repo: repo.to_path_buf(),
398 source: Source::Human,
399 solo: false,
400 overrides: RunOverrides {
401 merge: Some("none".to_owned()),
402 ..RunOverrides::default()
403 },
404 sources: Default::default(),
405 review_of: Some("feat/x".to_owned()),
406 }
407 }
408
409 fn heartbeat(home: &Path, pid: u32, age_secs: i64) {
410 let mut status = Status::new();
411 status.pid = pid;
412 status.updated_at = Timestamp::now()
413 .checked_sub(jiff::SignedDuration::from_secs(age_secs))
414 .unwrap();
415 crate::daemon::write_status_to(&home.join("daemon.json"), &status).unwrap();
416 }
417
418 #[test]
419 fn explicit_sources_survive_filing_queue_storage_and_release() {
420 let dir = tempfile::tempdir().unwrap();
421 let queue = Queue::at(dir.path().join("queue"));
422 let mut direct = filing(dir.path());
423 direct.review_of = None;
424 direct.instruction = "Do not import the historical example abc1234".into();
425 direct.sources = crate::refs::Sources {
426 start_from: Some(crate::refs::Input {
427 token: "feature/selected".into(),
428 sha: "1".repeat(40),
429 provenance: crate::refs::Provenance::ExplicitCli,
430 }),
431 pick_commits: vec![crate::refs::Input {
432 token: "abc5678".into(),
433 sha: "2".repeat(40),
434 provenance: crate::refs::Provenance::ExplicitCli,
435 }],
436 };
437 let selected = direct.sources.clone();
438 let mut task = direct.into_task();
439 queue.put(&mut task).unwrap();
440 let mut restored = queue.get(&task.id).unwrap();
441 assert_eq!(restored.sources, selected);
442 restored.hold_manual(Some("retry preparation".into()));
443 restored.release_operator("test", None);
444 queue.put(&mut restored).unwrap();
445 assert_eq!(queue.get(&task.id).unwrap().sources, selected);
446 assert_eq!(restored.instruction, task.instruction);
447 }
448
449 #[test]
450 fn task_implementers_direct_filing_keeps_options_isolated() {
451 let dir = tempfile::tempdir().unwrap();
452 let mut direct = filing(dir.path());
453 direct.overrides.inherit_daemon_options = true;
454 let task = direct.into_task();
455 assert!(!task.overrides.unwrap().inherit_daemon_options);
456 }
457
458 #[test]
459 fn without_a_loop_the_task_is_claimed_before_anyone_else_can_take_it() {
460 let dir = tempfile::tempdir().unwrap();
461 let q = Queue::at(dir.path().join("queue"));
462 let filed = file(
463 &q,
464 dir.path(),
465 Timestamp::now(),
466 1,
467 filing(dir.path()),
468 false,
469 )
470 .unwrap();
471 let Filed::Standalone { task, claim } = filed else {
472 panic!("no loop is alive");
473 };
474 assert!(!task.urgent);
475 assert_eq!(task.review_of.as_deref(), Some("feat/x"));
476 assert_eq!(
477 task.overrides.as_ref().unwrap().merge.as_deref(),
478 Some("none")
479 );
480 assert!(
481 q.claim(&task.id).is_err(),
482 "a second process must not be able to claim a standalone run's task"
483 );
484 drop(claim);
485 assert!(q.claim(&task.id).is_ok(), "released with the claim");
486 }
487
488 #[test]
489 fn a_live_foreign_loop_gets_an_urgent_unclaimed_task() {
490 let dir = tempfile::tempdir().unwrap();
491 let q = Queue::at(dir.path().join("queue"));
492 heartbeat(dir.path(), 4242, 0);
493 let filed = file(
494 &q,
495 dir.path(),
496 Timestamp::now(),
497 1,
498 filing(dir.path()),
499 false,
500 )
501 .unwrap();
502 let Filed::Daemon { task, pid } = filed else {
503 panic!("a loop is alive");
504 };
505 assert_eq!(pid, Some(4242));
506 assert!(task.urgent);
507 let stored = q.get(&task.id).unwrap();
508 assert_eq!(stored.status, TaskStatus::Queued);
509 assert!(stored.runs.is_empty(), "nothing ran in this process");
510 assert!(q.claim(&task.id).is_ok(), "and this process holds no claim");
511 }
512
513 #[test]
514 fn a_stale_heartbeat_our_own_pid_and_a_dry_run_do_not_count_as_a_loop() {
515 let dir = tempfile::tempdir().unwrap();
516 let q = Queue::at(dir.path().join("queue"));
517 heartbeat(dir.path(), 4242, 3600);
518 assert!(matches!(
519 file(
520 &q,
521 dir.path(),
522 Timestamp::now(),
523 1,
524 filing(dir.path()),
525 false
526 )
527 .unwrap(),
528 Filed::Standalone { .. }
529 ));
530 heartbeat(dir.path(), 7, 0);
531 assert!(matches!(
532 file(
533 &q,
534 dir.path(),
535 Timestamp::now(),
536 7,
537 filing(dir.path()),
538 false
539 )
540 .unwrap(),
541 Filed::Standalone { .. }
542 ));
543 heartbeat(dir.path(), 4242, 0);
544 assert!(matches!(
545 file(
546 &q,
547 dir.path(),
548 Timestamp::now(),
549 1,
550 filing(dir.path()),
551 true
552 )
553 .unwrap(),
554 Filed::Standalone { .. }
555 ));
556 }
557
558 #[tokio::test]
559 async fn following_gives_up_on_a_task_nobody_is_serving() {
560 let dir = tempfile::tempdir().unwrap();
561 let q = Queue::at(dir.path().join("queue"));
562 let mut t = filing(dir.path()).into_task();
563 q.put(&mut t).unwrap();
564 let err = follow(
565 &q,
566 dir.path(),
567 &t.id,
568 Duration::from_millis(5),
569 None,
570 |_| {},
571 |_| {},
572 )
573 .await
574 .unwrap_err();
575 assert!(format!("{err}").contains("no magi loop"), "{err}");
576 assert!(q.claim(&t.id).is_ok(), "never taken over");
577 }
578
579 #[tokio::test]
580 async fn following_returns_when_the_loop_finishes_the_task_and_times_out_otherwise() {
581 let dir = tempfile::tempdir().unwrap();
582 let q = Queue::at(dir.path().join("queue"));
583 heartbeat(dir.path(), 4242, 0);
584 let mut t = filing(dir.path()).into_task();
585 q.put(&mut t).unwrap();
586 let waited = follow(
587 &q,
588 dir.path(),
589 &t.id,
590 Duration::from_millis(5),
591 Some(Duration::from_millis(30)),
592 |_| {},
593 |_| {},
594 )
595 .await
596 .unwrap();
597 assert!(!waited.finished, "the wait ran out, the loop still owns it");
598 let err = waited
599 .unfinished()
600 .expect("an unfinished wait is an error")
601 .to_string();
602 assert!(err.contains(t.short()), "{err}");
603 assert!(
604 err.contains("not finished") || err.contains("not a success"),
605 "{err}"
606 );
607 assert!(err.contains("magi task show"), "{err}");
608 assert_eq!(
609 q.get(&t.id).unwrap().status,
610 TaskStatus::Queued,
611 "the queue is untouched"
612 );
613 t.link_run("20260101-000000-abcd");
614 t.succeed();
615 q.put(&mut t).unwrap();
616 let mut runs = Vec::new();
617 let done = follow(
618 &q,
619 dir.path(),
620 &t.id,
621 Duration::from_millis(5),
622 Some(Duration::from_secs(5)),
623 |r| runs.push(r.to_owned()),
624 |_| {},
625 )
626 .await
627 .unwrap();
628 assert_eq!(done.task.status, TaskStatus::Done);
629 assert!(done.unfinished().is_none());
630 assert_eq!(runs, ["20260101-000000-abcd"]);
631 }
632
633 fn parent_in(q: &Queue, dir: &Path) -> Task {
634 let mut t = filing(dir).into_task();
635 q.put(&mut t).unwrap();
636 t
637 }
638
639 fn follow_up_state(dir: &Path) -> crate::run::RunState {
640 crate::run::RunState::new(
641 dir.to_path_buf(),
642 "main".to_owned(),
643 "abc1234".to_owned(),
644 "review the branch".to_owned(),
645 Config::default(),
646 )
647 }
648
649 #[test]
650 fn an_adopted_review_claims_starts_and_settles_a_runnable_task() {
651 let dir = tempfile::tempdir().unwrap();
652 let q = Queue::at(dir.path().join("queue"));
653 let parent = parent_in(&q, dir.path());
654 let state = follow_up_state(dir.path());
655 let adopted = adopt_in(q.clone(), &state, Some(&parent.id))
656 .unwrap()
657 .expect("adopted");
658 let running = q.get(&parent.id).unwrap();
659 assert_eq!(running.status, TaskStatus::Running);
660 assert_eq!(running.attempts, parent.attempts + 1);
661 assert_eq!(running.runs, std::slice::from_ref(&state.id));
662 assert!(q.claim(&parent.id).is_err(), "exclusive while it runs");
663 adopted.finish(&state, Err(anyhow::anyhow!("boom")));
664 let settled = q.get(&parent.id).unwrap();
665 assert_eq!(settled.status, TaskStatus::Held, "{settled:?}");
666 assert_eq!(
667 settled.runs,
668 std::slice::from_ref(&state.id),
669 "no duplicate run"
670 );
671 assert!(q.claim(&parent.id).is_ok(), "claim released");
672 assert_eq!(q.list().len(), 1, "no second owner was filed");
673 }
674
675 #[test]
676 fn an_ownerless_run_whose_task_cannot_be_filed_is_an_error() {
677 let dir = tempfile::tempdir().unwrap();
678 let blocker = dir.path().join("blocker");
680 std::fs::write(&blocker, "x").unwrap();
681 let q = Queue::at(blocker.join("q"));
682 let state = follow_up_state(dir.path());
683 let err = adopt_in(q, &state, None).expect_err("must not be swallowed");
684 assert!(format!("{err:#}").contains(&state.id), "{err:#}");
685 }
686
687 #[test]
688 fn a_task_claimed_elsewhere_only_gains_the_run() {
689 let dir = tempfile::tempdir().unwrap();
690 let q = Queue::at(dir.path().join("queue"));
691 let parent = parent_in(&q, dir.path());
692 let _theirs = q.claim(&parent.id).unwrap();
693 let state = follow_up_state(dir.path());
694 assert!(
695 adopt_in(q.clone(), &state, Some(&parent.id))
696 .unwrap()
697 .is_none()
698 );
699 let after = q.get(&parent.id).unwrap();
700 assert_eq!(after.status, parent.status);
701 assert_eq!(after.attempts, parent.attempts);
702 assert_eq!(after.runs, std::slice::from_ref(&state.id));
703 assert_eq!(q.list().len(), 1, "no second owner was filed");
704 }
705
706 #[test]
707 fn a_done_task_only_gains_the_run() {
708 let dir = tempfile::tempdir().unwrap();
709 let q = Queue::at(dir.path().join("queue"));
710 let mut parent = parent_in(&q, dir.path());
711 parent.succeed();
712 q.put(&mut parent).unwrap();
713 let state = follow_up_state(dir.path());
714 assert!(
715 adopt_in(q.clone(), &state, Some(&parent.id))
716 .unwrap()
717 .is_none()
718 );
719 let after = q.get(&parent.id).unwrap();
720 assert_eq!(after.status, TaskStatus::Done);
721 assert_eq!(after.attempts, parent.attempts);
722 assert_eq!(after.runs, std::slice::from_ref(&state.id));
723 assert!(q.claim(&parent.id).is_ok(), "claim released");
724 }
725
726 #[test]
727 fn a_held_task_is_claimed_and_settled_by_its_follow_up_review() {
728 let dir = tempfile::tempdir().unwrap();
729 let q = Queue::at(dir.path().join("queue"));
730 let mut parent = parent_in(&q, dir.path());
731 parent.hold_manual(Some("the run did not finish: stale".to_owned()));
732 q.put(&mut parent).unwrap();
733 let state = follow_up_state(dir.path());
734 let adopted = adopt_in(q.clone(), &state, Some(&parent.id))
735 .unwrap()
736 .expect("adopted");
737 assert_eq!(q.get(&parent.id).unwrap().status, TaskStatus::Running);
738 assert!(q.claim(&parent.id).is_err(), "claimed while it runs");
739 adopted.finish(&state, Err(anyhow::anyhow!("fresh failure")));
740 let after = q.get(&parent.id).unwrap();
741 assert_eq!(after.status, TaskStatus::Held);
742 assert!(
743 after
744 .hold_reason
745 .as_deref()
746 .unwrap()
747 .contains("fresh failure"),
748 "{after:?}"
749 );
750 assert_eq!(after.runs, std::slice::from_ref(&state.id));
751 assert!(q.claim(&parent.id).is_ok(), "claim released");
752 }
753}