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
//! What a child session is to its parent, and the policy that follows:
//! what titles it and what tells the host it stopped.
use agent_client_protocol::v2::schema as v2;
use rho_protocol::{BackgroundStatus, JobState};
use super::{background::subagent_idle, child_jobs::job_ended};
use crate::app::acp::Foreground;
/// What a child is to its parent, which says what titles it and what
/// tells the host it stopped.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(super) enum ChildKind {
/// A subagent: the parent's background list titles it and says how
/// its run ended.
Subagent,
/// A session the parent started with the `sessions` tool, titled as
/// its own session is. It outlives its turns, so it is idle when its
/// session is.
Started,
}
impl ChildKind {
/// Whether the daemon's job list says how its work ended once its
/// relay stops: a subagent's run ends, a started session does not.
pub(super) fn learns_end_from_jobs(self) -> bool {
match self {
Self::Subagent => true,
Self::Started => false,
}
}
/// Whether a link to it follows it again once its relay stopped: a
/// started session that unloaded may be prompted again, while a
/// subagent's run is over.
pub(super) fn follows_again(self) -> bool {
match self {
Self::Subagent => false,
Self::Started => true,
}
}
/// Whether the host is asked its approvals and questions under the
/// child: a started session waits for a person, while a subagent's
/// questions are its parent's requests, put to the host under the
/// child from there.
pub(super) fn asks_host(self) -> bool {
match self {
Self::Subagent => false,
Self::Started => true,
}
}
/// Whether its own session's title is the parent's title for it: a
/// subagent's is its run's, which the background list carries.
pub(super) fn mirrors_title(self) -> bool {
match self {
Self::Subagent => false,
Self::Started => true,
}
}
/// The `idle` the host is told when an observe finds the child in
/// `state`, which no turn's end will tell: a started session that is
/// idle, with how its last turn ended, or no stop reason when it ran
/// none (`JobState::Idle`). A subagent's turn end tells it.
pub(super) fn idle_at_observe(self, state: JobState) -> Option<v2::StateUpdate> {
match (self, Foreground::of(state)) {
(Self::Started, Foreground::Idle) => Some(subagent_idle(
job_ended(state).unwrap_or(BackgroundStatus::Unknown),
)),
(Self::Started, Foreground::Running | Foreground::RequiresAction)
| (Self::Subagent, _) => None,
}
}
}