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
//! The background work a session waits on, whose results the daemon (see
//! `idle_wake`) or the session's host (see `t3_wake`) starts a turn for once
//! they finish while the session is idle.
//!
//! Tasks count; services never do: a background process started as a
//! service neither wakes the session nor keeps it from settling (see
//! `settle`). A session whose prompts hold until their work finishes
//! (`BackgroundWait::HoldPrompt`) waits on nothing between turns.
use std::future::Future;
use crate::{
app::{session_runtime::BackgroundWait, subagent_manager::SubagentManager},
tools::{
process::{LiveProcessSummary, ProcessManager},
ui::SurfaceHost,
workflow_tracker::{WorkflowRailSummary, WorkflowRunTracker},
},
};
/// The work one session waits on.
#[derive(Clone)]
pub(super) struct AwaitedWork {
session_id: String,
processes: Option<ProcessManager>,
workflows: WorkflowRunTracker,
/// The session's subagents, when the daemon's own turns deliver them
/// (`BackgroundWait::HostTurns`): one that finished, and a child's
/// request that the parent act, are results.
subagents: Option<SubagentManager>,
/// The session's A2UI surfaces, where the daemon starts a turn for what
/// the user does on one.
surfaces: Option<SurfaceHost>,
}
impl AwaitedWork {
/// The work a session that waits as `wait` is woken for, from its
/// tools' managers; `None` for a session nothing wakes.
pub(super) fn new(
wait: BackgroundWait,
session_id: String,
processes: Option<ProcessManager>,
workflows: WorkflowRunTracker,
subagents: Option<SubagentManager>,
surfaces: Option<SurfaceHost>,
) -> Option<Self> {
let (subagents, surfaces) = match wait {
BackgroundWait::HoldPrompt => return None,
// The host runs the session's children as its own threads and
// reports them itself, and its client draws no surfaces.
BackgroundWait::HostWakes => (None, None),
BackgroundWait::HostTurns => (subagents, surfaces),
};
Some(Self {
session_id,
processes,
workflows,
subagents,
surfaces,
})
}
/// Resolves once a result may have arrived from now on, whatever waits
/// already. A service's exit, or a child's informational notice,
/// resolves it too, though neither is a result: callers look again with
/// [`Self::has_result`].
pub(super) fn next_result(&self) -> impl Future<Output = ()> {
// Subscribed here, not at the first poll.
let process_exit = self.processes.as_ref().map(ProcessManager::notified_owned);
let workflow_finish = self.workflows.notified_owned();
let subagent = self.subagents.as_ref().map(SubagentManager::parent_wake);
let surface_action = self.surfaces.as_ref().map(SurfaceHost::acted_owned);
async move {
let surface_action = async move {
match surface_action {
Some(acted) => acted.await,
None => std::future::pending().await,
}
};
let process_exit = async move {
match process_exit {
Some(exit) => exit.await,
None => std::future::pending().await,
}
};
let subagent = async move {
match subagent {
Some(wake) => wake.await,
None => std::future::pending().await,
}
};
tokio::select! {
() = process_exit => {}
() = workflow_finish => {}
() = subagent => {}
() = surface_action => {}
}
}
}
/// Whether a result is waiting or could still arrive. A workflow counts
/// until its terminal snapshot is published, which follows its last
/// runtime event; a subagent from its start until a turn takes its
/// result, under one lock, so one that just finished is never missed.
pub(super) fn outstanding(&self) -> bool {
// Live first, results last: a task only goes from live to finished
// to delivered, so one that finishes between the two looks is still
// counted by `has_result`.
self.processes
.as_ref()
.is_some_and(ProcessManager::has_live_task)
|| self
.workflows
.has_active_or_pending_notification(&self.session_id)
|| self.subagents.as_ref().is_some_and(|subagents| {
subagents.has_active_or_pending_notification(&self.session_id)
})
|| self.has_result()
}
/// Whether work that may still give a result runs: the complement, among
/// the work not yet delivered, of [`Self::has_result`].
pub(super) fn running(&self) -> bool {
self.processes
.as_ref()
.is_some_and(ProcessManager::has_live_task)
|| self.workflows.has_live(&self.session_id)
|| self
.subagents
.as_ref()
.is_some_and(|subagents| subagents.has_running(&self.session_id))
}
/// The session's tasks still running or holding a result not yet
/// delivered, oldest first; never its services.
pub(super) fn tasks(&self) -> Vec<LiveProcessSummary> {
self.processes
.iter()
.flat_map(ProcessManager::task_summaries)
.collect()
}
/// The session's workflow runs still running or holding a result not
/// yet delivered, oldest first.
pub(super) fn workflows(&self) -> Vec<WorkflowRailSummary> {
self.workflows.rail_summaries(&self.session_id)
}
/// Whether a finished result is waiting for the model.
pub(super) fn has_result(&self) -> bool {
self.processes
.as_ref()
.is_some_and(ProcessManager::has_task_result)
|| self.workflows.has_pending_notification(&self.session_id)
|| self.subagents.as_ref().is_some_and(|subagents| {
subagents.has_pending_notification(&self.session_id)
|| subagents
.undelivered_notices(&self.session_id)
.iter()
.any(|notice| notice.delivery.requires_parent_action())
})
|| self.surfaces.as_ref().is_some_and(SurfaceHost::has_action)
}
/// Resolves once a result is waiting.
pub(super) async fn result_ready(self) {
loop {
// Subscribe before checking so a finish in between still wakes.
let another = self.next_result();
if self.has_result() {
return;
}
#[cfg(debug_assertions)]
if let Some(subagents) = &self.subagents {
crate::app::quiet_notice_signal::parent_looked(
subagents.undelivered_notices(&self.session_id).len(),
);
}
another.await;
}
}
}
#[cfg(test)]
#[path = "awaited_tests.rs"]
mod tests;