1use anyhow::{Result, ensure};
3use jiff::Timestamp;
4use serde::{Deserialize, Serialize};
5
6use crate::run::{CommandOutcome, ReviewRound, RunState, RunStatus};
7
8#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
9#[serde(rename_all = "snake_case")]
10pub enum StopKind {
12 Exhausted,
14 Conflict,
16 Publication,
18 Divergence,
20 ClosingIntegratedPr,
22 Unknown,
24}
25
26#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
27pub struct Stop {
29 pub kind: StopKind,
31 pub head: String,
33 pub tip: String,
35 pub attempts: usize,
37 pub detail: String,
39 pub at: Timestamp,
41 #[serde(default)]
43 pub unverified_sync: bool,
44}
45
46#[derive(Debug, Clone, Serialize, Deserialize)]
47pub struct Authorization {
49 #[serde(default)]
50 pub scope: Option<Scope>,
51 #[serde(default)]
53 pub request: Option<String>,
54 #[serde(default)]
56 pub decision: Option<Timestamp>,
57 #[serde(default)]
59 pub question: Option<String>,
60 #[serde(default)]
62 pub interval: bool,
63 #[serde(default)]
64 pub invalidated: Option<String>,
65 pub task: String,
67 pub run: String,
69 pub stop: Stop,
71 pub at: Timestamp,
73 pub consumed: bool,
75 #[serde(default)]
77 pub generation: Option<u64>,
78 pub reviews: Vec<ReviewRound>,
80 pub gate: Vec<CommandOutcome>,
82 pub gate_head: Option<String>,
84 pub gate_ran: bool,
86}
87
88impl Authorization {
89 pub(crate) fn refuse(&mut self, reason: impl Into<String>) {
91 self.invalidated = Some(reason.into());
92 self.consumed = true;
93 }
94}
95
96#[derive(Debug, Clone, Serialize, Deserialize)]
98pub struct Episode {
99 pub id: usize,
101 pub reason: String,
103 pub used: usize,
105 pub synced_head: Option<String>,
107 #[serde(default)]
109 pub last_sync_head: Option<String>,
110 pub boundary: Option<Boundary>,
112 #[serde(default)]
114 pub prior_boundaries: Vec<Boundary>,
115}
116
117#[derive(Debug, Clone, Serialize, Deserialize)]
119pub struct Boundary {
120 pub pass: usize,
121 pub at: Timestamp,
122 pub head: String,
123 pub consumed: bool,
124}
125
126#[derive(Debug, Clone, Serialize, Deserialize)]
128pub struct Operation {
129 pub id: usize,
130 pub episode: usize,
131 pub head: String,
132 pub tip: String,
133 pub finished: bool,
134 #[serde(default)]
136 pub validated_head: Option<String>,
137}
138
139#[derive(Debug, Clone, Serialize, Deserialize)]
141pub struct Renewal {
142 pub task: String,
143 pub run: String,
144 pub generation: u64,
145 pub instruction: String,
146 pub episode: usize,
147 pub boundary_pass: usize,
148 pub head: String,
149 pub tip: String,
150}
151
152#[derive(Debug, Clone, Serialize, Deserialize)]
154pub struct Scope {
155 pub tip: String,
156 pub instruction: String,
157 pub episode: usize,
158}
159
160#[derive(Debug, Clone, Default, Serialize, Deserialize)]
161pub struct Recovery {
163 #[serde(default)]
165 pub episodes: Vec<Episode>,
166 #[serde(default)]
167 pub operations: Vec<Operation>,
168 #[serde(default)]
170 pub stops: Vec<Stop>,
171 #[serde(default)]
172 pub renewals: Vec<Renewal>,
173 pub stop: Option<Stop>,
175 pub authorizations: Vec<Authorization>,
177}
178
179impl RunState {
180 pub fn base_sync_next_action(&self) -> String {
182 let episode = self.base_sync_recovery.episodes.last().map_or(0, |e| e.id);
183 let lifetime = self.base_sync.as_ref().map_or(0, |s| s.attempts);
184 format!(
185 "{} ({}/{} in episode {}, {} lifetime synchronizations)",
186 self.base_sync_action(),
187 self.base_episode_used(),
188 crate::graph::BASE_SYNC_ROUNDS,
189 episode,
190 lifetime
191 )
192 }
193
194 fn base_sync_action(&self) -> String {
195 if let Some(reason) = self
196 .base_sync_recovery
197 .authorizations
198 .last()
199 .and_then(|a| a.invalidated.as_ref())
200 {
201 return format!("Approval invalidated: {reason}");
202 }
203 if self.resource_block.is_some() {
204 return "Restore the required resource, then explicitly resume the retained run; automatic retries retain the synchronization budget".into();
205 }
206 if self.base_sync_recovery.stop.is_none()
207 && let Some(op) = self
208 .base_sync_recovery
209 .operations
210 .last()
211 .filter(|o| !o.finished)
212 {
213 return format!(
214 "Resume reserved synchronization {} against base {}; inspect retained Git state if reconciliation is refused",
215 op.id, op.tip
216 );
217 }
218 if self.base_sync_blocked()
219 && (self.base_sync_recovery.stop.is_none()
220 || self
221 .rebase_fixes
222 .iter()
223 .any(|r| r.stop.is_none() && !r.finished))
224 {
225 let task = self
226 .origin
227 .as_ref()
228 .and_then(|o| o.task.as_deref())
229 .unwrap_or("<task>");
230 return format!(
231 "Inspect legacy or missing structured stop evidence with `magi task inspect-base-stop {task} --run {}`; diagnostic-only textual stops require explicit evidence-bound base-conflict authorization, not movement budget recovery",
232 self.id
233 );
234 }
235 match self.base_sync_recovery.stop.as_ref().map(|s| &s.kind) {
236 Some(StopKind::Exhausted) => {
237 let why = if self.base_sync_recovery.stop.as_ref().is_some_and(|s| s.unverified_sync) {
238 "The synchronized HEAD never reached its gate before the base moved again. "
239 } else {
240 ""
241 };
242 if self.base_recovery_pending() {
243 format!("{why}Approval recorded; resume the retained run to open the approved bounded synchronization interval")
244 } else {
245 format!("{why}Authorize a new bounded synchronization interval with `magi run --resume {} --recover-base`, or, from a standing chat, `magi task control recover-base {} --request <unique-id> --head {}`", self.id, self.id, self.base_sync_recovery.stop.as_ref().unwrap().head)
246 }
247 },
248 Some(StopKind::Conflict) => {
249 let stop = self.base_sync_recovery.stop.as_ref().unwrap();
250 let task = self.origin.as_ref().and_then(|origin| origin.task.as_deref()).unwrap_or("<task>");
251 format!("Authorize a narrow history-preserving repair with `magi task grant-repair {task} --run {} --kind base-conflict --rounds 1 --head {} --tip {} --expect-pass {} --request <unique-id> --reason <reason> --instruction <approved-resolution>`; retain the original review criteria. Base-budget recovery cannot resolve a textual conflict", self.id, stop.head, stop.tip, self.passes.last().map_or(0, |p| p.index))
252 },
253 Some(StopKind::Divergence) => "Reconcile foreign remote branch commits before resuming; do not overwrite them".into(),
254 Some(StopKind::Publication) => "Inspect the retained publication failure and remote lease before resuming".into(),
255 Some(StopKind::ClosingIntegratedPr) => "Resume to retry closing the already integrated pull request".into(),
256 Some(StopKind::Unknown) => "Inspect the retained synchronization state before resuming; unknown evidence grants no new budget".into(),
257 None => "Continue verification on the current synchronized HEAD".into(),
258 }
259 }
260
261 pub(crate) fn ensure_base_episode(&mut self) {
262 if let Some(stop) = self.base_sync_recovery.stop.clone()
263 && !self.base_sync_recovery.stops.contains(&stop)
264 {
265 self.base_sync_recovery.stops.push(stop);
266 }
267 if self.base_sync_recovery.episodes.is_empty() {
268 let used = self.base_sync.as_ref().map_or(0, |s| s.attempts);
269 self.base_sync_recovery.episodes.push(Episode {
270 id: 0,
271 reason: "initial or conservative legacy migration".into(),
272 used,
273 synced_head: None,
274 last_sync_head: None,
275 boundary: None,
276 prior_boundaries: Vec::new(),
277 });
278 }
279 }
280
281 fn open_base_episode(&mut self, reason: &str) {
282 self.ensure_base_episode();
283 let id = self.base_sync_recovery.episodes.len();
284 self.base_sync_recovery.episodes.push(Episode {
285 id,
286 reason: reason.into(),
287 used: 0,
288 synced_head: None,
289 last_sync_head: None,
290 boundary: None,
291 prior_boundaries: Vec::new(),
292 });
293 }
294
295 pub(crate) fn base_episode_used(&self) -> usize {
296 self.base_sync_recovery.episodes.last().map_or_else(
297 || self.base_sync.as_ref().map_or(0, |s| s.attempts),
298 |e| e.used,
299 )
300 }
301
302 pub(crate) fn record_base_progress(&mut self, head: &str) {
303 self.ensure_base_episode();
304 let episode = self.base_sync_recovery.episodes.last_mut().unwrap();
305 if let Some(boundary) = episode.boundary.take() {
306 episode.prior_boundaries.push(boundary);
307 }
308 episode.synced_head = Some(head.into());
309 }
310
311 pub(crate) fn record_base_interruption(&mut self) {
313 let Some(block) = &self.resource_block else {
314 return;
315 };
316 if matches!(block.cause, crate::run::ResourceBlockCause::Unrecorded) {
317 return;
318 }
319 let Some(episode) = self.base_sync_recovery.episodes.last_mut() else {
320 return;
321 };
322 let Some(head) = episode.synced_head.clone() else {
323 return;
324 };
325 if episode.boundary.is_none() {
326 episode.boundary = Some(Boundary {
327 pass: self.passes.last().map_or(0, |p| p.index),
328 at: block.at,
329 head,
330 consumed: false,
331 });
332 }
333 }
334
335 pub(crate) fn renew_base_episode(
336 &mut self,
337 task: &str,
338 approval: &crate::queue::Resumption,
339 head: &str,
340 tip: &str,
341 ) -> bool {
342 if self
343 .base_sync_recovery
344 .renewals
345 .iter()
346 .any(|r| r.task == task && r.generation >= approval.generation)
347 || self
348 .base_sync_recovery
349 .authorizations
350 .iter()
351 .any(|a| a.task == task && a.generation.is_some_and(|g| g >= approval.generation))
352 {
353 return false;
354 }
355 let Some(e) = self.base_sync_recovery.episodes.last_mut() else {
356 return false;
357 };
358 let Some(b) = &mut e.boundary else {
359 return false;
360 };
361 if b.consumed || b.head != head || approval.at <= b.at {
362 return false;
363 }
364 b.consumed = true;
365 self.base_sync_recovery.renewals.push(Renewal {
366 task: task.into(),
367 run: self.id.clone(),
368 generation: approval.generation,
369 instruction: approval.instruction.clone(),
370 episode: e.id,
371 boundary_pass: b.pass,
372 head: head.into(),
373 tip: tip.into(),
374 });
375 self.open_base_episode("owner resumption after recorded infrastructure interruption");
376 true
377 }
378
379 pub(crate) fn reserve_base_operation(&mut self, head: &str, tip: &str) {
381 self.ensure_base_episode();
382 if self
383 .base_sync_recovery
384 .operations
385 .last()
386 .is_some_and(|o| !o.finished)
387 {
388 return;
389 }
390 let e = self.base_sync_recovery.episodes.last_mut().unwrap();
391 e.used += 1;
392 let id = self.base_sync_recovery.operations.len();
393 self.base_sync_recovery.operations.push(Operation {
394 id,
395 episode: e.id,
396 head: head.into(),
397 tip: tip.into(),
398 finished: false,
399 validated_head: None,
400 });
401 if let Some(sync) = &mut self.base_sync {
402 sync.attempts += 1;
403 }
404 }
405
406 pub(crate) fn finish_base_operation(&mut self, head: Option<&str>) {
407 if let Some(op) = self.base_sync_recovery.operations.last_mut() {
408 op.finished = true;
409 }
410 if let Some(head) = head {
411 let episode = self.base_sync_recovery.episodes.last_mut().unwrap();
412 if let Some(boundary) = episode.boundary.take() {
413 episode.prior_boundaries.push(boundary);
414 }
415 episode.synced_head = Some(head.into());
416 episode.last_sync_head = Some(head.into());
417 self.reconcile_verification_head(head);
418 }
419 }
420
421 pub(crate) fn base_sync_blocked(&self) -> bool {
422 self.base_sync
423 .as_ref()
424 .is_some_and(|s| s.conflict.is_some())
425 }
426
427 pub(crate) fn base_sync_closure_pending(&self) -> bool {
430 let Some(detail) = self
431 .base_sync
432 .as_ref()
433 .and_then(|sync| sync.conflict.as_ref())
434 else {
435 return false;
436 };
437 let Some(winner) = self.winner() else {
438 return false;
439 };
440 let prefix = format!(
442 "{} is already in {}/{}, but its pull request could not be closed (",
443 winner.branch, self.config.merge.remote, self.base_branch
444 );
445 detail.starts_with(&prefix) && detail.ends_with("); resume to retry")
446 }
447
448 pub(crate) fn record_base_sync_stop(&mut self, head: &str) {
451 self.record_base_sync_stop_as(head, None);
452 }
453
454 pub(crate) fn record_base_sync_stop_as(&mut self, head: &str, kind: Option<StopKind>) {
455 let Some(sync) = self.base_sync.as_ref() else {
456 return;
457 };
458 let Some(detail) = sync.conflict.clone() else {
459 return;
460 };
461 if self
462 .base_sync_recovery
463 .stop
464 .as_ref()
465 .is_some_and(|s| s.head == head && s.tip == sync.tip && s.detail == detail)
466 {
467 self.status = RunStatus::Blocked;
468 return;
469 }
470 let exhausted = self.winner().is_some_and(|w| {
471 [
472 ("rebase(s)", "rebasing"),
473 ("synchronization(s)", "synchronizing"),
474 ].iter().any(|(noun, verb)| detail == format!(
475 "{} moved {} commit(s) ahead of {} after {} {}; {} again would only race it",
476 self.base_branch, sync.behind, w.branch, crate::graph::BASE_SYNC_ROUNDS, noun, verb
477 ))
478 });
479 let inferred = || {
480 if self.base_sync_closure_pending() {
481 StopKind::ClosingIntegratedPr
482 } else if sync.attempts >= crate::graph::BASE_SYNC_ROUNDS && exhausted {
483 StopKind::Exhausted
484 } else if detail.contains("not rebasing over") {
485 StopKind::Divergence
486 } else if detail.contains("refused the push")
487 || detail.contains("could not push")
488 || detail.contains("could not be pushed")
489 {
490 StopKind::Publication
491 } else if detail.contains("conflicts with") && detail.contains("did not rebase") {
492 StopKind::Conflict
493 } else {
494 StopKind::Unknown
495 }
496 };
497 let kind = kind.unwrap_or_else(inferred);
498 let at = self
499 .events
500 .iter()
501 .rev()
502 .find(|event| event.node == "land" && event.message == detail)
503 .map_or(self.updated_at, |event| event.at);
504 let unverified_sync = sync.behind > 0
505 && self
506 .base_sync_recovery
507 .episodes
508 .last()
509 .is_some_and(|e| e.last_sync_head.as_deref() == Some(head))
510 && !(self.gate_ran && self.gate_head.as_deref() == Some(head));
511 let stop = Stop {
512 unverified_sync,
513 kind,
514 head: head.to_owned(),
515 tip: sync.tip.clone(),
516 attempts: sync.attempts,
517 detail,
518 at,
519 };
520 self.base_sync_recovery.stops.push(stop.clone());
521 self.base_sync_recovery.stop = Some(stop);
522 self.status = RunStatus::Blocked;
523 }
524
525 pub(crate) fn base_recovery_pending(&self) -> bool {
526 self.base_sync_recovery
527 .authorizations
528 .last()
529 .is_some_and(|a| {
530 !a.consumed
531 && a.invalidated.is_none()
532 && a.run == self.id
533 && self.base_sync_recovery.stop.as_ref() == Some(&a.stop)
534 })
535 }
536
537 pub(crate) fn authorize_base_sync(&mut self, task: &str) -> Result<()> {
538 let stop = self
539 .base_sync_recovery
540 .stop
541 .clone()
542 .ok_or_else(|| anyhow::anyhow!("no recorded base-sync stop"))?;
543 ensure!(
544 stop.kind == StopKind::Exhausted,
545 "only exhausted base movement permits recovery"
546 );
547 ensure!(
548 !self
549 .base_sync_recovery
550 .authorizations
551 .last()
552 .is_some_and(|a| !a.consumed && a.stop == stop),
553 "this stop already has a pending authorization; resume the existing request"
554 );
555 self.base_sync_recovery.authorizations.push(Authorization {
556 scope: None,
557 request: None,
558 decision: None,
559 question: None,
560 interval: true,
561 invalidated: None,
562 task: task.to_owned(),
563 run: self.id.clone(),
564 stop,
565 at: Timestamp::now(),
566 consumed: false,
567 generation: None,
568 reviews: Vec::new(),
569 gate: Vec::new(),
570 gate_head: None,
571 gate_ran: false,
572 });
573 Ok(())
574 }
575
576 pub(crate) fn authorize_base_sync_resumption(
577 &mut self,
578 task: &str,
579 approval: &crate::queue::Resumption,
580 ) -> Result<()> {
581 let stop = self
582 .base_sync_recovery
583 .stop
584 .as_ref()
585 .ok_or_else(|| anyhow::anyhow!("no recorded base-sync stop"))?;
586 ensure!(
587 approval.at > stop.at,
588 "approval predates the base-sync stop"
589 );
590 ensure!(
591 !self.base_sync_recovery.authorizations.iter().any(|a| {
592 a.task == task && a.generation.is_some_and(|g| g >= approval.generation)
593 }) && !self
594 .base_sync_recovery
595 .renewals
596 .iter()
597 .any(|r| r.task == task && r.generation >= approval.generation),
598 "operator resumption generation already used"
599 );
600 self.authorize_base_sync(task)?;
601 self.base_sync_recovery
602 .authorizations
603 .last_mut()
604 .unwrap()
605 .generation = Some(approval.generation);
606 Ok(())
607 }
608
609 pub(crate) fn consume_base_recovery(&mut self, head: &str) -> bool {
612 if !self.base_recovery_pending() {
613 return false;
614 }
615 let a = self.base_sync_recovery.authorizations.last_mut().unwrap();
616 a.consumed = true;
617 if a.stop.head != head {
618 a.refuse(format!(
619 "retained HEAD changed from {} to {head}; inspect it and authorize again with --recover-base",
620 a.stop.head
621 ));
622 return false;
623 }
624 a.reviews = self.reviews.clone();
625 a.gate = std::mem::take(&mut self.gate);
626 a.gate_head = self.gate_head.take();
627 a.gate_ran = std::mem::replace(&mut self.gate_ran, false);
628 let interval = a.interval;
629 self.base_sync.as_mut().unwrap().conflict = None;
630 self.open_base_episode("explicit exhausted-base recovery");
631 self.base_sync_recovery.stop = None;
632 if !interval {
633 self.base_sync_recovery.episodes.last_mut().unwrap().used =
634 crate::graph::BASE_SYNC_ROUNDS - 1;
635 }
636 true
637 }
638}
639
640pub(crate) fn held_task(queue: &crate::queue::Queue, task: &crate::queue::Task) -> bool {
643 held_run(queue, task).is_some()
644}
645
646pub(crate) fn held_run(queue: &crate::queue::Queue, task: &crate::queue::Task) -> Option<RunState> {
647 let home = queue.root().parent()?;
648 task.execution_runs()
649 .last()
650 .and_then(|id| RunState::load_under(id, home).ok())
651 .filter(|state| {
652 state.base_sync_blocked()
653 && !state.base_recovery_pending()
654 && !state.base_sync_closure_pending()
655 })
656}
657
658impl crate::queue::Task {
659 pub(crate) fn base_sync_notice(&mut self, state: &RunState) {
661 let Some(sync) = state
662 .base_sync
663 .as_ref()
664 .filter(|_| state.base_sync_blocked())
665 else {
666 return;
667 };
668 let stop = state.base_sync_recovery.stop.as_ref();
669 self.held_at = Some(stop.map_or(state.updated_at, |s| s.at));
670 self.notice_cause("base-sync-stop", Some(&state.id), None);
671 if let Some(cause) = &mut self.hold_notice_cause {
672 cause.target = Some(match stop {
673 Some(s) => format!("{}:{}:{}", s.head, s.tip, s.at),
674 None => format!("legacy:{}:{}", sync.tip, sync.attempts),
675 });
676 }
677 }
678}
679
680pub fn ensure_generic_release(
682 queue: &crate::queue::Queue,
683 task: &crate::queue::Task,
684) -> Result<()> {
685 if let Some(state) = task
686 .execution_runs()
687 .last()
688 .and_then(|id| RunState::load_under(id, queue.root().parent()?).ok())
689 && let Some(reason) = crate::repair_budget::conflict_failure(task, &state)
690 {
691 anyhow::bail!(reason);
692 }
693 if let Some(state) = held_run(queue, task)
694 && state
695 .base_sync_recovery
696 .stop
697 .as_ref()
698 .is_some_and(|s| s.kind == StopKind::Conflict)
699 && !crate::repair_budget::conflict_pending(task, &state)
700 {
701 anyhow::bail!(
702 "generic release cannot authorize textual conflict editing. {}",
703 state.base_sync_next_action()
704 );
705 }
706 Ok(())
707}