magi-cli 0.240.0

Blind multi-agent implementation competition: N agents implement, M judges rank blind, deliberate, vote privately, winner survives double review + E2E gate
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
//! Finite, evidence-triggered continuation of accepted conversation goals.
use std::path::Path;

use anyhow::{Context, Result};
use serde_json::{Value, json};

/// A current observation is data, never a new owner instruction.
#[derive(Debug)]
pub(crate) struct Observation {
    pub facts: Value,
    pub fingerprint: String,
}

fn read_json(path: &Path) -> Result<Value> {
    serde_json::from_slice(
        &std::fs::read(path).with_context(|| format!("read {}", path.display()))?,
    )
    .context("decode goal evidence")
}

fn valid_id(id: &str) -> bool {
    !id.is_empty() && id.bytes().all(|b| b.is_ascii_alphanumeric() || b == b'-')
}

fn unavailable(error: &anyhow::Error) -> Value {
    json!({"unavailable": format!("{error:#}")})
}

trait PrObserver: Sync {
    fn current_pr(&self, repo: &Path, url: &str) -> impl Future<Output = Result<Value>> + Send;
}

struct CurrentPr;

impl PrObserver for CurrentPr {
    async fn current_pr(&self, repo: &Path, url: &str) -> Result<Value> {
        crate::chat_notify::current_pr(repo, url).await
    }
}

/// Local task/run evidence plus one timed forge lookup per distinct associated PR.
/// Timestamps and transcript prose are excluded from the wake fingerprint.
pub(crate) async fn observe(
    home: &Path,
    repo: &Path,
    tasks: &[String],
    runs: &[String],
    watches: &[String],
) -> Observation {
    observe_with_pr(home, repo, tasks, runs, watches, &CurrentPr).await
}

async fn observe_with_pr(
    home: &Path,
    repo: &Path,
    tasks: &[String],
    runs: &[String],
    watches: &[String],
    observer: &impl PrObserver,
) -> Observation {
    let mut task_facts = Vec::new();
    let mut run_ids = Vec::new();
    let mut historical_runs = std::collections::BTreeSet::new();
    let queue = crate::queue::Queue::at(home.join("queue"));
    for id in tasks {
        match queue.get(id) {
            Ok(task) => {
                if task.repo != repo {
                    task_facts.push(json!({"id":id,"evidence":{"unavailable":"task repository changed; goal scope requires review"}}));
                    continue;
                }
                historical_runs.extend(task.runs.iter().cloned());
                if let Some(run) = task.execution_runs().last()
                    && !run_ids.contains(run)
                {
                    run_ids.push(run.clone());
                }
                task_facts.push(json!({"id": task.id, "status":task.status,
                    "reason":task.hold_reason, "runs":task.execution_runs(),"current_run":task.execution_runs().last()}));
            }
            Err(error) => task_facts.push(json!({"id":id,"evidence":unavailable(&error)})),
        }
    }
    for run in runs {
        if !historical_runs.contains(run) && !run_ids.contains(run) {
            run_ids.push(run.clone());
        }
    }
    let mut run_facts = Vec::new();
    let mut pr_observations = std::collections::BTreeMap::new();
    let mut prs = Vec::new();
    for id in &run_ids {
        if !valid_id(id) {
            run_facts.push(json!({"id":id,"evidence":{"unavailable":"invalid run identity"}}));
            continue;
        }
        match read_json(&home.join("runs").join(id).join("run.json")) {
            Ok(run) => {
                let winner = run["candidates"].as_array().and_then(|candidates| {
                    candidates
                        .iter()
                        .find(|candidate| candidate["label"] == run["tally"]["winner"])
                });
                let branch = winner.and_then(|candidate| candidate["branch"].as_str());
                let artifact_head = if let Some(branch) = branch {
                    let revision = format!("{branch}^{{commit}}");
                    tokio::time::timeout(
                        std::time::Duration::from_secs(5),
                        crate::git::git_raw(
                            repo,
                            &["rev-parse", "--verify", "--end-of-options", &revision],
                        ),
                    )
                    .await
                    .ok()
                    .and_then(Result::ok)
                    .filter(|output| output.ok())
                    .map(|output| output.stdout)
                } else {
                    None
                };
                if let Some(url) = run.pointer("/pr/url").and_then(Value::as_str) {
                    if !pr_observations.contains_key(url) {
                        let observed = observer
                            .current_pr(repo, url)
                            .await
                            .unwrap_or_else(|error| unavailable(&error));
                        pr_observations.insert(url.to_owned(), observed);
                    }
                    prs.push(json!({"run":id,"observation":pr_observations[url]}));
                }
                run_facts.push(json!({"id":id,"status":run["status"],
                    "pr":run["pr"],"head":run["pr_pushed_head"],"merge":run["merge"],
                    "resource_block":run["resource_block"],"base_sync":run["base_sync"],
                    "gate_head":run["gate_head"],"gate":run["gate"],
                    "gate_ran":run["gate_ran"], "artifact_head":artifact_head,
                    "github_text":run["github_text"],"land_approval":run["land_approval"]}));
            }
            Err(error) => run_facts.push(json!({"id":id,"evidence":unavailable(&error)})),
        }
    }
    let mut watch_facts = Vec::new();
    for id in watches {
        if !valid_id(id) {
            watch_facts
                .push(json!({"id":id,"observation":{"unavailable":"invalid watch identity"}}));
            continue;
        }
        let value = read_json(&home.join("chat-watches").join(id).join("job.json"))
            .map(|job| json!({"result":job["result"]}))
            .unwrap_or_else(|error| unavailable(&error));
        watch_facts.push(json!({"id":id,"observation":value}));
    }
    let decisions = match crate::task_inquiry::Outbox::at(home).list() {
        Ok(inquiries) => Value::Array(
            inquiries
                .into_iter()
                .filter(|inquiry| {
                    tasks.contains(&inquiry.task)
                        || inquiry.run.as_ref().is_some_and(|id| run_ids.contains(id))
                })
                .filter_map(|inquiry| {
                    inquiry.decision.map(|decision| json!({
                "id":inquiry.id,"task":inquiry.task,"run":inquiry.run,
                "status":decision.status,"request":decision.request,"error":decision.error,
                "retry_safe":decision.retry_safe
            }))
                })
                .collect(),
        ),
        Err(error) => unavailable(&error),
    };
    let facts = json!({"tasks":task_facts,"runs":run_facts,"prs":prs,"watches":watch_facts,"decisions":decisions});
    let fingerprint = facts.to_string();
    Observation { facts, fingerprint }
}

fn satisfied(goal: &crate::chat_goal::Goal, facts: &Value) -> bool {
    use crate::chat_goal::Completion;
    let runs = facts["runs"].as_array().map(Vec::as_slice).unwrap_or(&[]);
    if matches!(
        goal.condition.kind,
        Completion::TasksDone | Completion::RunsMerged
    ) && facts["tasks"].as_array().is_some_and(|tasks| {
        tasks.iter().any(|task| {
            task["current_run"].is_null()
                || !runs.iter().any(|run| run["id"] == task["current_run"])
        })
    }) {
        return false;
    }
    match goal.condition.kind {
        Completion::OwnerReview => false,
        Completion::RunsMerged => {
            !runs.is_empty()
                && runs.iter().all(|run| {
                    let Some(head) = run["head"].as_str().filter(|head| !head.is_empty()) else {
                        return false;
                    };
                    facts["prs"].as_array().is_some_and(|prs| {
                        prs.iter().any(|pr| {
                            pr["run"] == run["id"]
                                && pr["observation"]["state"] == "MERGED"
                                && pr["observation"]["head"] == head
                                && !pr["observation"]["merged_at"].is_null()
                        })
                    })
                })
        }
        Completion::TasksDone => {
            !runs.is_empty()
                && runs.iter().all(|run| {
                    let gate = run["gate"].as_array();
                    run["gate_ran"] == true
                        && run["gate_head"]
                            .as_str()
                            .is_some_and(|head| !head.is_empty())
                        && gate.is_some_and(|commands| {
                            !commands.is_empty()
                                && commands
                                    .iter()
                                    .all(|c| c["code"] == 0 && c["resource_blocked"] != true)
                        })
                        && run["artifact_head"] == run["gate_head"]
                })
        }
        Completion::WatchesSatisfied => facts["watches"].as_array().is_some_and(|watches| {
            !watches.is_empty()
                && watches.iter().all(|watch| {
                    watch["observation"]["result"]
                        .as_str()
                        .is_some_and(|result| result == "exited with status 0")
                })
        }),
    }
}

/// Bounded restart reconciliation. Unchanged evidence never spends another turn.
pub(crate) async fn sweep(talks: &crate::talk::Talks, parking: bool) {
    if parking {
        return;
    }
    use crate::chat_goal::State;
    let mut pending = Vec::new();
    for talk in talks.list() {
        if !talk.status.open() || !talk.pending.is_empty() || !talk.pending_attachments.is_empty() {
            continue;
        }
        if talks.turn_held(&talk.id) || crate::chat_worker::turn_worker_active(talks, &talk.id) {
            continue;
        }
        for goal in &talk.goals {
            if matches!(goal.state, State::Runnable | State::Waiting) {
                pending.push((goal.last_checked, talk.id.clone(), goal.id.clone()));
            }
        }
    }
    pending.sort_by_key(|entry| entry.0);
    for (_, chat, id) in pending.into_iter().take(8) {
        if let Err(error) = reconcile(talks, &chat, &id).await {
            tracing::warn!("chat goal {id}: reconciliation deferred: {error:#}");
        }
    }
}

async fn reconcile(talks: &crate::talk::Talks, chat: &str, id: &str) -> Result<()> {
    use crate::chat_goal::State;
    let talk = talks.get(chat)?;
    let Some(goal) = talk.goals.iter().find(|goal| goal.id == id) else {
        return Ok(());
    };
    if crate::chat_worker::turn_worker_active(talks, chat) || talks.turn_held(chat) {
        return Ok(());
    }
    if goal.started.is_some() {
        talks.mutate(chat, |talk| {
            if let Some(goal) = talk.goals.iter_mut().find(|goal| goal.id == id) {
                goal.state = State::Paused;
                goal.step =
                    "Interrupted continuation; reconcile accepted effects before retrying".into();
            }
            Ok(())
        })?;
        return Ok(());
    }
    if goal.rechecks >= 240 {
        talks.mutate(chat, |talk| {
            if let Some(goal) = talk.goals.iter_mut().find(|goal| goal.id == id)
                && goal.rechecks >= 240 && goal.active() {
                goal.state = State::Paused;
                goal.step = "Evidence recheck budget exhausted; owner input is required to continue observing".into();
            }
            Ok(())
        })?;
        return Ok(());
    }
    let now = jiff::Timestamp::now();
    if goal
        .last_checked
        .is_some_and(|last| now.as_second() - last.as_second() < 60)
    {
        return Ok(());
    }
    let observation = observe(
        talks.home(),
        &talk.repo,
        &goal.tasks,
        &goal.runs,
        &goal.watches,
    )
    .await;
    let complete = satisfied(goal, &observation.facts);
    let previous = goal.fingerprint.clone();
    let goal_snapshot = goal.clone();
    let generation = talks.mutate(chat, |fresh| {
        let Some(goal) = fresh.goals.iter_mut().find(|goal| goal.id == id) else {
            return Ok(None);
        };
        if !fresh.status.open()
            || goal.fingerprint != previous
            || goal.tasks != goal_snapshot.tasks
            || goal.runs != goal_snapshot.runs
            || goal.watches != goal_snapshot.watches
            || goal.wake != goal_snapshot.wake
            || !matches!(goal.state, State::Runnable | State::Waiting)
        {
            return Ok(None);
        }
        goal.last_checked = Some(now);
        goal.rechecks += 1;
        goal.observe(
            observation.fingerprint,
            observation.facts.to_string(),
            complete,
        );
        if goal.wake > goal.handled && goal.started.is_none() && goal.state == State::Runnable {
            Ok(Some(goal.wake))
        } else {
            Ok(None)
        }
    })?;
    if let Some(generation) = generation {
        crate::worker::spawn(
            talks.home(),
            "chat",
            &format!("goal:{chat}:{id}:{generation}"),
            json!({"talk":chat,"goal_wake":{"id":id,"generation":generation}}),
        )
        .await?;
    }
    Ok(())
}

/// Separate reserved input: no draft is promoted, cleared, or implicitly sent.
pub(crate) async fn run_wake(
    talks: &crate::talk::Talks,
    chat: &str,
    id: &str,
    generation: u64,
) -> Result<()> {
    let talk = talks.get(chat)?;
    if !talk.status.open()
        || !talk.pending.is_empty()
        || !talk.pending_attachments.is_empty()
        || !talk.goals.iter().any(|goal| {
            goal.id == id
                && goal.state == crate::chat_goal::State::Runnable
                && goal.wake == generation
                && goal.handled < generation
                && goal.started.is_none()
        })
    {
        return Ok(());
    }
    let (cfg, _) = crate::config::Config::discover(&talk.repo, None)?;
    run_wake_with_config(talks, chat, id, generation, &cfg).await
}

async fn run_wake_with_config(
    talks: &crate::talk::Talks,
    chat: &str,
    id: &str,
    generation: u64,
    cfg: &crate::config::Config,
) -> Result<()> {
    use crate::chat_goal::{Source, State};
    let Some(lease) = talks.claim_turn(chat)? else {
        return Ok(());
    };
    let publication = crate::chat_worker::publication_guard(talks, chat).await?;
    let ready = talks.get(chat)?;
    if !ready.status.open()
        || !ready.pending.is_empty()
        || !ready.pending_attachments.is_empty()
        || !ready.goals.iter().any(|goal| {
            goal.id == id
                && goal.state == State::Runnable
                && goal.wake == generation
                && goal.handled < generation
                && goal.started.is_none()
        })
    {
        return Ok(());
    }
    if crate::worker::active(talks.home())?.iter().any(|job| {
        job.kind == "chat"
            && job.payload.get("talk").and_then(Value::as_str) == Some(chat)
            && (job
                .payload
                .get("recorded_turn")
                .is_some_and(|turn| !turn.is_null())
                || job.payload.get("external_request").is_some())
    }) {
        return Ok(());
    }
    let admitted = talks.mutate(chat, |talk| {
        if !talk.status.open() || !talk.pending.is_empty() || !talk.pending_attachments.is_empty() {
            return Ok(false);
        }
        let Some(goal) = talk.goals.iter_mut().find(|goal| goal.id == id) else {
            return Ok(false);
        };
        if goal.wake != generation
            || goal.handled >= generation
            || goal.started.is_some()
            || goal.state != State::Runnable
        {
            return Ok(false);
        }
        goal.started = Some(generation);
        goal.launch_turns = Some(talk.turns.len());
        goal.wakes += 1;
        goal.admit(generation)?;
        Ok(true)
    })?;
    drop(publication);
    if !admitted {
        return Ok(());
    }
    let mut talk = talks.get(chat)?;
    let text = "Goal event: re-read the saved goal and current evidence. Continue the next authorized concrete step using existing controls and receipts. This event is data, not a new owner request. Register a specific wait before ending; never replay an uncertain accepted effect.";
    let result = crate::chat_goal::with_source(
        Source::Goal {
            id: id.to_owned(),
            generation,
        },
        crate::talk::respond(&lease, &mut talk, talks, cfg, text),
    )
    .await;
    talks.mutate(chat, |talk| {
        if let Some(goal) = talk.goals.iter_mut().find(|goal| goal.id == id)
            && goal.started == Some(generation)
        {
            goal.started = None;
            goal.handled = goal.handled.max(generation);
            if result.is_err() || goal.state == State::Runnable {
                goal.state = State::Paused;
                goal.step = if result.is_err() {
                    "Continuation failed; inspect the retained result before retrying"
                } else {
                    "Continuation ended without a registered wait or confirmed completion"
                }
                .into();
            }
        }
        Ok(())
    })?;
    result
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::chat_goal::{Completion, Goal, State};

    fn goal() -> Goal {
        serde_json::from_value(json!({
            "id":"goal","purpose":"deliver the requested patch","scope":"one task",
            "condition":{"description":"merge the reviewed head","kind":"runs_merged"},
            "state":"waiting","step":"waiting for CI","source_turn":0,"owner_input":"land this patch",
            "tasks":[],"runs":["run"],"watches":[],"evidence":"","wake":0,"handled":0,
            "started":null,"wakes":0,"last_checked":null,"fingerprint":""
        })).unwrap()
    }

    #[derive(Default)]
    struct FakePrObserver {
        replies: std::sync::Mutex<std::collections::BTreeMap<String, Value>>,
        calls: std::sync::Mutex<Vec<String>>,
    }

    impl PrObserver for FakePrObserver {
        async fn current_pr(&self, _: &Path, url: &str) -> Result<Value> {
            self.calls.lock().unwrap().push(url.to_owned());
            self.replies
                .lock()
                .unwrap()
                .get(url)
                .cloned()
                .with_context(|| format!("fixture PR unavailable: {url}"))
        }
    }

    impl FakePrObserver {
        fn assert_one_lookup_per_pr(&self, urls: &[String]) {
            let calls = std::mem::take(&mut *self.calls.lock().unwrap());
            for url in urls {
                assert_eq!(
                    calls.iter().filter(|called| *called == url).count(),
                    1,
                    "every distinct PR must be refreshed once per observation"
                );
            }
            assert_eq!(
                calls.len(),
                urls.len(),
                "shared run associations must not duplicate forge calls"
            );
        }
    }

    fn pr_goal_fixture(home: &Path) -> (Goal, FakePrObserver, Vec<String>) {
        let mut goal = goal();
        goal.runs.clear();
        let observer = FakePrObserver::default();
        let urls: Vec<_> = (1..=5)
            .map(|index| format!("https://example.invalid/pull/{index}"))
            .collect();
        for (index, url) in urls.iter().enumerate() {
            let run = format!("run-{index}");
            let head = format!("{:040x}", index + 1);
            let path = home.join("runs").join(&run);
            std::fs::create_dir_all(&path).unwrap();
            std::fs::write(
                path.join("run.json"),
                json!({"pr":{"url":url},"pr_pushed_head":head}).to_string(),
            )
            .unwrap();
            goal.runs.push(run);
            observer.replies.lock().unwrap().insert(
                url.clone(),
                json!({"state":"MERGED","head":head,"merged_at":"2026-01-01T00:00:00Z"}),
            );
        }
        // A second run sharing the fifth PR still needs a corresponding fact.
        let alias = home.join("runs").join("run-shared");
        std::fs::create_dir_all(&alias).unwrap();
        std::fs::write(
            alias.join("run.json"),
            json!({"pr":{"url":urls[4]},"pr_pushed_head":format!("{:040x}",5)}).to_string(),
        )
        .unwrap();
        goal.runs.push("run-shared".into());
        (goal, observer, urls)
    }

    #[tokio::test]
    async fn fifth_pull_request_changes_wake_and_complete_all_related_runs() {
        let temp = tempfile::tempdir().unwrap();
        let (mut goal, observer, urls) = pr_goal_fixture(temp.path());
        let head = format!("{:040x}", 5);
        observer.replies.lock().unwrap().insert(
            urls[4].clone(),
            json!({"state":"OPEN","head":head,"merged_at":null,"checks":[{"verdict":"pending"}]}),
        );
        let pending =
            observe_with_pr(temp.path(), temp.path(), &[], &goal.runs, &[], &observer).await;
        assert!(!satisfied(&goal, &pending.facts));
        goal.observe(pending.fingerprint, pending.facts.to_string(), false);
        observer.assert_one_lookup_per_pr(&urls);

        observer.replies.lock().unwrap().insert(
            urls[4].clone(),
            json!({"state":"OPEN","head":head,"merged_at":null,"checks":[{"verdict":"failed"}]}),
        );
        let failed =
            observe_with_pr(temp.path(), temp.path(), &[], &goal.runs, &[], &observer).await;
        assert_ne!(
            failed.fingerprint, goal.fingerprint,
            "CI changes beyond the first four PRs must wake the goal"
        );
        assert!(!satisfied(&goal, &failed.facts));
        goal.observe(failed.fingerprint, failed.facts.to_string(), false);
        assert_eq!(goal.wake, 2);
        observer.assert_one_lookup_per_pr(&urls);

        observer.replies.lock().unwrap().insert(urls[4].clone(),
            json!({"state":"MERGED","head":format!("{:040x}",99),"merged_at":"2026-01-01T00:00:00Z"}));
        let moved =
            observe_with_pr(temp.path(), temp.path(), &[], &goal.runs, &[], &observer).await;
        assert!(
            !satisfied(&goal, &moved.facts),
            "the fifth PR must match its saved head too"
        );
        observer.assert_one_lookup_per_pr(&urls);

        observer.replies.lock().unwrap().insert(
            urls[4].clone(),
            json!({"state":"MERGED","head":head,"merged_at":"2026-01-01T00:00:00Z"}),
        );
        let merged =
            observe_with_pr(temp.path(), temp.path(), &[], &goal.runs, &[], &observer).await;
        assert!(
            satisfied(&goal, &merged.facts),
            "all six run associations must have exact-head merge evidence"
        );
        goal.observe(merged.fingerprint, merged.facts.to_string(), true);
        assert_eq!(goal.state, State::Completed);
        observer.assert_one_lookup_per_pr(&urls);
    }

    #[tokio::test]
    async fn unavailable_pull_request_does_not_hide_later_ci_or_reuse_old_merge_evidence() {
        let temp = tempfile::tempdir().unwrap();
        let (goal, observer, urls) = pr_goal_fixture(temp.path());
        let merged =
            observe_with_pr(temp.path(), temp.path(), &[], &goal.runs, &[], &observer).await;
        assert!(satisfied(&goal, &merged.facts));
        observer.assert_one_lookup_per_pr(&urls);
        observer.replies.lock().unwrap().remove(&urls[0]);
        observer.replies.lock().unwrap().insert(urls[4].clone(),
            json!({"state":"OPEN","head":format!("{:040x}",5),"merged_at":null,"checks":[{"verdict":"failed"}]}));
        let current =
            observe_with_pr(temp.path(), temp.path(), &[], &goal.runs, &[], &observer).await;
        assert!(
            !satisfied(&goal, &current.facts),
            "previously merged evidence cannot replace a failed fresh lookup"
        );
        assert_ne!(merged.fingerprint, current.fingerprint);
        let prs = current.facts["prs"].as_array().unwrap();
        assert!(
            prs.iter()
                .any(|pr| pr["run"] == "run-0" && pr["observation"]["unavailable"].is_string())
        );
        assert!(prs.iter().any(
            |pr| pr["run"] == "run-4" && pr["observation"]["checks"][0]["verdict"] == "failed"
        ));
        observer.assert_one_lookup_per_pr(&urls);
    }

    #[test]
    fn merge_completion_requires_observed_exact_head_not_task_done_or_pr_creation() {
        let goal = goal();
        let mut facts = json!({"tasks":[{"status":"done","current_run":"run"}],
            "runs":[{"id":"run","head":"abc","pr":{"url":"https://example.invalid/pull/1"}}],
            "prs":[{"run":"run","observation":{"state":"OPEN","head":"abc","merged_at":null}}]});
        assert!(!satisfied(&goal, &facts));
        facts["prs"][0]["observation"] = json!({"state":"MERGED","head":"other","merged_at":"now"});
        assert!(!satisfied(&goal, &facts));
        facts["prs"][0]["observation"]["head"] = json!("abc");
        assert!(satisfied(&goal, &facts));
        facts["prs"][0]["observation"] = json!({"unavailable":"forge offline"});
        assert!(!satisfied(&goal, &facts));
    }

    #[test]
    fn implementation_completion_requires_a_verified_delivered_commit() {
        let mut goal = goal();
        goal.condition.kind = Completion::TasksDone;
        let mut facts = json!({"runs":[{"id":"run","gate_ran":true,"gate_head":"abc",
            "gate":[{"code":0}], "artifact_head":"old"}]});
        assert!(!satisfied(&goal, &facts));
        facts["runs"][0]["artifact_head"] = json!("abc");
        assert!(satisfied(&goal, &facts));
        facts["runs"][0]["gate"][0]["resource_blocked"] = json!(true);
        assert!(!satisfied(&goal, &facts));
    }

    #[tokio::test]
    async fn restart_retains_uncertain_started_work_without_replaying_it() {
        let temp = tempfile::tempdir().unwrap();
        let talks = crate::talk::Talks::at(temp.path().join("talks"));
        let mut talk = crate::talk::seeded_for_test("chat", temp.path().into());
        let mut goal = goal();
        goal.state = State::Runnable;
        goal.wake = 1;
        goal.started = Some(1);
        talk.goals.push(goal);
        talks.put(&mut talk).unwrap();
        reconcile(&talks, "chat", "goal").await.unwrap();
        let saved = talks.get("chat").unwrap();
        assert_eq!(saved.goals[0].state, State::Paused);
        assert_eq!(saved.goals[0].started, Some(1));
        assert!(saved.turns.is_empty());
        assert!(crate::worker::active(temp.path()).unwrap().is_empty());
    }

    #[tokio::test]
    async fn owner_draft_and_stopped_goal_are_never_consumed_by_a_wake() {
        let temp = tempfile::tempdir().unwrap();
        let talks = crate::talk::Talks::at(temp.path().join("talks"));
        let mut talk = crate::talk::seeded_for_test("chat", temp.path().into());
        let mut goal = goal();
        goal.state = State::Stopped;
        goal.wake = 1;
        talk.goals.push(goal);
        talk.pending = "keep my unsent question".into();
        talks.put(&mut talk).unwrap();
        run_wake(&talks, "chat", "goal", 1).await.unwrap();
        let saved = talks.get("chat").unwrap();
        assert_eq!(saved.pending, "keep my unsent question");
        assert_eq!(saved.goals[0].state, State::Stopped);
        assert!(saved.goals[0].started.is_none());
        assert!(saved.turns.is_empty());
    }

    #[tokio::test]
    async fn completion_checks_the_entire_task_and_run_scope() {
        let temp = tempfile::tempdir().unwrap();
        let home = temp.path().join("home");
        let repo = temp.path().join("repo");
        std::fs::create_dir(&repo).unwrap();
        for args in [
            vec!["init"],
            vec![
                "-c",
                "user.name=Fixture",
                "-c",
                "user.email=fixture@example.invalid",
                "-c",
                "commit.gpgsign=false",
                "commit",
                "--allow-empty",
                "-m",
                "Fixture artifact",
            ],
        ] {
            let result = crate::git::git_raw(&repo, &args).await.unwrap();
            assert!(result.ok(), "{}", result.stderr);
        }
        let head = crate::git::git_raw(&repo, &["rev-parse", "HEAD"])
            .await
            .unwrap()
            .stdout;
        let queue = crate::queue::Queue::at(home.join("queue"));
        let mut goal = goal();
        goal.condition.kind = Completion::TasksDone;
        goal.tasks.clear();
        goal.runs.clear();
        for index in 0..32 {
            let run = format!("run-{index}");
            let path = home.join("runs").join(&run);
            std::fs::create_dir_all(&path).unwrap();
            std::fs::write(
                path.join("run.json"),
                json!({
                    "gate_ran":true,"gate_head":head,"gate":[{"code":0}],
                    "tally":{"winner":"A"},"candidates":[{"label":"A","branch":"HEAD"}]
                })
                .to_string(),
            )
            .unwrap();
            let mut task = crate::queue::Task::new(
                "Verified patch".into(),
                "deliver the patch".into(),
                repo.clone(),
                crate::queue::Source::Human,
            );
            task.runs.push(run.clone());
            queue.put(&mut task).unwrap();
            goal.tasks.push(task.id);
            goal.runs.push(run);
        }
        let mut unstarted = crate::queue::Task::new(
            "Unstarted patch".into(),
            "deliver this patch too".into(),
            repo.clone(),
            crate::queue::Source::Human,
        );
        queue.put(&mut unstarted).unwrap();
        goal.tasks.push(unstarted.id.clone());
        goal.runs.push("missing-run".into());
        let observation = observe(&home, &repo, &goal.tasks, &goal.runs, &[]).await;
        let mut facts = observation.facts;
        assert!(
            !satisfied(&goal, &facts),
            "unobserved work must never be declared complete"
        );
        assert!(
            facts["tasks"]
                .as_array()
                .unwrap()
                .iter()
                .any(|task| task["id"] == unstarted.id)
        );
        assert!(
            facts["runs"]
                .as_array()
                .unwrap()
                .iter()
                .any(|run| run["id"] == "missing-run")
        );

        // Each tail independently prevents completion; the fully verified
        // prefix is a delivered artifact rather than a vacuous failure.
        facts["runs"].as_array_mut().unwrap().pop();
        assert!(
            !satisfied(&goal, &facts),
            "the thirty-third task is still unstarted"
        );
        facts["tasks"].as_array_mut().unwrap().pop();
        assert!(satisfied(&goal, &facts));
        facts["runs"].as_array_mut().unwrap().push(json!({
            "id":"missing-run","evidence":{"unavailable":"not started"}
        }));
        assert!(
            !satisfied(&goal, &facts),
            "the thirty-third direct run is still unavailable"
        );
    }

    #[tokio::test]
    async fn watch_completion_requires_the_tail_and_invalid_identities() {
        let temp = tempfile::tempdir().unwrap();
        let mut goal = goal();
        goal.condition.kind = Completion::WatchesSatisfied;
        goal.watches = (0..33).map(|index| format!("watch-{index}")).collect();
        for id in &goal.watches[..32] {
            let path = temp.path().join("chat-watches").join(id);
            std::fs::create_dir_all(&path).unwrap();
            std::fs::write(
                path.join("job.json"),
                r#"{"result":"exited with status 0"}"#,
            )
            .unwrap();
        }
        let observation = observe(temp.path(), temp.path(), &[], &[], &goal.watches).await;
        assert!(
            !satisfied(&goal, &observation.facts),
            "the thirty-third watch has no result"
        );
        let path = temp.path().join("chat-watches").join(&goal.watches[32]);
        std::fs::create_dir_all(&path).unwrap();
        std::fs::write(
            path.join("job.json"),
            r#"{"result":"exited with status 0"}"#,
        )
        .unwrap();
        let observation = observe(temp.path(), temp.path(), &[], &[], &goal.watches).await;
        assert!(satisfied(&goal, &observation.facts));
        goal.watches.push("../invalid".into());
        let observation = observe(temp.path(), temp.path(), &[], &[], &goal.watches).await;
        assert!(
            !satisfied(&goal, &observation.facts),
            "invalid identities are unknown, not omitted"
        );
    }
}

#[cfg(test)]
mod worker_tests {
    use super::*;
    use crate::config::{AgentKind, AgentSpec, Config};

    #[tokio::test]
    async fn changed_event_resumes_same_goal_once_across_reopened_store_and_concurrent_starters() {
        let temp = tempfile::tempdir().unwrap();
        let talks = crate::talk::Talks::at(temp.path().join("talks"));
        let script = temp.path().join("finite-agent.sh");
        std::fs::write(&script,
            "cat > \"$GOAL_PROMPT\"\nprintf 'step\\n' >> \"$GOAL_CALLS\"\nprintf 'Inspected the retained work; waiting for the registered event.\\n'\n"
        ).unwrap();
        let calls = temp.path().join("calls");
        let prompt = temp.path().join("prompt");
        let cfg = Config {
            agents: vec![AgentSpec {
                id: "worker".into(),
                kind: AgentKind::Command,
                model: None,
                command: vec!["sh".into(), script.to_string_lossy().into_owned()],
                extra_args: Vec::new(),
                env: std::collections::BTreeMap::from([
                    ("GOAL_CALLS".into(), calls.to_string_lossy().into_owned()),
                    ("GOAL_PROMPT".into(), prompt.to_string_lossy().into_owned()),
                ]),
                prompt_delivery: None,
                max_turn_secs: None,
            }],
            ..Config::default()
        };
        let mut talk = crate::talk::seeded_for_test("finite-goal", temp.path().into());
        talk.agent = "worker".into();
        talk.persona = "asuka".into();
        talk.title = Some(crate::talk::ChatTitle {
            text: "Retained work".into(),
            source: crate::talk::TitleSource::Manual,
        });
        talks.put(&mut talk).unwrap();
        crate::talk::record(&mut talk, &talks, "repair and land this task", Vec::new()).unwrap();
        let queue = crate::queue::Queue::at(temp.path().join("queue"));
        let mut task = crate::queue::Task::new(
            "Retained patch".into(),
            "repair the existing patch".into(),
            talk.repo.clone(),
            crate::queue::Source::Agent {
                run: talk.id.clone(),
                node: crate::queue::CHAT_NODE.into(),
            },
        );
        task.status = crate::queue::TaskStatus::Held;
        task.hold_reason = Some("existing verification failed".into());
        queue.put(&mut task).unwrap();
        let mut goal: crate::chat_goal::Goal = serde_json::from_value(json!({
            "id":"deliver","purpose":"Repair the existing patch without a second owner message",
            "scope":"the retained task only",
            "condition":{"description":"merge the approved head","kind":"runs_merged"},
            "state":"waiting","step":"waiting for task result","source_turn":0,
            "owner_input":"repair and land this task","tasks":[task.id],"runs":[],"watches":[],
            "evidence":"","wake":0,"handled":0,"started":null,"wakes":0,
            "last_checked":null,"fingerprint":""
        }))
        .unwrap();
        let event = observe(
            temp.path(),
            &talk.repo,
            &goal.tasks,
            &goal.runs,
            &goal.watches,
        )
        .await;
        goal.observe(event.fingerprint, event.facts.to_string(), false);
        talk.goals.push(goal);
        talks.put(&mut talk).unwrap();
        let (first, duplicate) = tokio::join!(
            Box::pin(run_wake_with_config(&talks, &talk.id, "deliver", 1, &cfg)),
            Box::pin(run_wake_with_config(&talks, &talk.id, "deliver", 1, &cfg))
        );
        first.unwrap();
        duplicate.unwrap();
        assert_eq!(std::fs::read_to_string(&calls).unwrap().lines().count(), 1);
        let saved = talks.get(&talk.id).unwrap();
        assert_eq!(saved.goals[0].handled, 1);
        assert!(saved.goals[0].started.is_none());
        assert!(!talks.turn_held(&talk.id));
        // A process/store restart retains receipts. Replaying the old wake
        // cannot invoke the backend again, even with no live in-memory owner.
        let reopened = crate::talk::Talks::at(temp.path().join("talks"));
        run_wake_with_config(&reopened, &talk.id, "deliver", 1, &cfg)
            .await
            .unwrap();
        assert_eq!(std::fs::read_to_string(&calls).unwrap().lines().count(), 1);
        task.hold_reason = Some("current head checks failed".into());
        queue.put(&mut task).unwrap();
        let event = observe(
            temp.path(),
            &talk.repo,
            std::slice::from_ref(&task.id),
            &[],
            &[],
        )
        .await;
        reopened
            .mutate(&talk.id, |talk| {
                let goal = &mut talk.goals[0];
                goal.state = crate::chat_goal::State::Waiting;
                goal.observe(event.fingerprint, event.facts.to_string(), false);
                Ok(())
            })
            .unwrap();
        run_wake_with_config(&reopened, &talk.id, "deliver", 2, &cfg)
            .await
            .unwrap();
        assert_eq!(std::fs::read_to_string(&calls).unwrap().lines().count(), 2);
        let input = std::fs::read_to_string(prompt).unwrap();
        assert!(input.contains("Repair the existing patch without a second owner message"));
        assert!(input.contains("current head checks failed"));
        assert_eq!(reopened.get(&talk.id).unwrap().persona, "asuka");
        assert_eq!(reopened.get(&talk.id).unwrap().goals[0].handled, 2);
    }
}

#[cfg(test)]
mod budget_tests {
    use super::*;

    #[tokio::test]
    async fn unchanged_wait_has_a_finite_probe_budget_without_agent_calls() {
        let temp = tempfile::tempdir().unwrap();
        let talks = crate::talk::Talks::at(temp.path().join("talks"));
        let mut talk = crate::talk::seeded_for_test("bounded-wait", temp.path().into());
        let goal = serde_json::from_value(json!({
            "id":"wait","purpose":"observe the existing job","scope":"one job",
            "condition":{"description":"owner reviews result","kind":"owner_review"},
            "state":"waiting","step":"waiting for evidence","source_turn":0,"owner_input":"follow this job",
            "tasks":[],"runs":[],"watches":[],"evidence":"","wake":0,"handled":0,
            "started":null,"wakes":0,"rechecks":240,"last_checked":null,"fingerprint":""
        })).unwrap();
        talk.goals.push(goal);
        talks.put(&mut talk).unwrap();
        reconcile(&talks, &talk.id, "wait").await.unwrap();
        let saved = talks.get(&talk.id).unwrap();
        assert_eq!(saved.goals[0].state, crate::chat_goal::State::Paused);
        assert_eq!(saved.goals[0].rechecks, 240);
        assert_eq!(saved.goals[0].wakes, 0);
        assert!(saved.turns.is_empty());
        assert!(crate::worker::active(temp.path()).unwrap().is_empty());
    }
}