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
//! A child as the registry holds it (ยง40.50 items 2, 6): its harness, mode
//! and directory, its harness session, its turns, the private home a Vibe
//! or Muse Code child keeps for its life, removed when the child is
//! forgotten (X6a), and the session directory made for a Muse child
//! (X6c U5).
use serde_json::Value;
use super::adapter::{Agent, Mode};
use super::turn::{End, Turn, lock};
/// A child as the registry holds it.
pub struct Child {
pub id: String,
pub agent: Agent,
pub mode: Mode,
pub model: Option<String>,
pub directory: std::path::PathBuf,
pub parent: Option<String>,
pub session: Option<String>,
/// The session the harness announced already existed in this store.
pub existed: bool,
/// One of its turns was filed by its own watcher (X4f Q3).
pub claimed: bool,
pub turns: u32,
pub stopped: bool,
pub current: Option<Turn>,
pub limit_seconds: u64,
/// The private home made at its first turn (X6a, X6c U10).
pub home: Option<std::path::PathBuf>,
/// A Muse child's session directory, made at its first turn (X6c U5).
pub session_dir: Option<std::path::PathBuf>,
}
impl Child {
pub fn running(&self) -> bool {
self.current.as_ref().is_some_and(|turn| !turn.ended())
}
/// The harness session, learned from the current turn when it is new.
pub fn learn(&mut self) {
if self.session.is_none()
&& let Some(turn) = &self.current
{
let shared = lock(&turn.shared);
self.session = shared.session.clone();
self.existed = shared.existed;
}
}
/// The session already had turns here when the announce check or the
/// watcher's filing looked: it is never continued (X4e P2).
pub fn collided(&self) -> bool {
self.existed || self.receipt(&["existing"])
}
/// Whether the current turn's receipt says one of `dispositions`.
fn receipt(&self, dispositions: &[&str]) -> bool {
self.current
.as_ref()
.is_some_and(|turn| filed_as(lock(&turn.shared).filed.as_ref(), dispositions))
}
/// The child's own watcher filed a turn of this session: the session is
/// this child's, whoever writes it later (X4f Q3).
pub fn claim(&mut self) -> bool {
self.claimed = self.claimed || self.receipt(&["recorded", "pending"]);
self.claimed
}
pub(super) fn state(&self) -> &'static str {
match (self.stopped, self.outcome()) {
(true, _) => "stopped",
(false, outcome) => outcome,
}
}
/// How the current turn itself went, whatever became of the child.
pub(super) fn outcome(&self) -> &'static str {
let Some(turn) = &self.current else {
return "starting";
};
let shared = lock(&turn.shared);
if !shared.ended() {
return "running";
}
match shared.end {
Some(End::Exited(0)) if shared.failure.is_none() => "finished",
Some(End::TimedOut) => "timed_out",
Some(End::Stopped) => "stopped",
_ => "failed",
}
}
}
pub(super) fn filed_as(filed: Option<&Value>, dispositions: &[&str]) -> bool {
filed.is_some_and(|filed| dispositions.iter().any(|d| filed["disposition"] == *d))
}
impl Drop for Child {
/// A forgotten child takes its private home with it (X6a, X6b), and a
/// Muse session directory no turn wrote (X6c U5).
fn drop(&mut self) {
if let Some(home) = &self.home {
super::home::forget(home);
}
if let Some(dir) = &self.session_dir {
let _ = std::fs::remove_dir(dir);
}
}
}