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
//! The children an agent of another runtime starts on its own, as Claude
//! Code starts a subagent with its Task tool, recorded as runs a host
//! follows as child jobs (`rho_protocol::ChildRole::Runtime`): a daemon
//! session's, whose job hears of them directly, and a delegated run's,
//! which tells the job following it on its event channel.
//!
//! Each child's run is reserved where the session's subagents' runs go, so
//! it is listed, attached to and deleted as theirs are, and clients follow
//! its session's events as a subagent's. A child's own children are
//! recorded the same way, as children of its job, which hears of them on
//! the child's event channel. Rho did not start the child and its
//! conversation is the agent's, so nobody prompts, cancels or resumes it:
//! its session is not saved and shows only while it runs; its status and
//! attach journal stay.
use std::{path::PathBuf, sync::Arc};
use rho_sdk::SessionId;
use tokio::sync::{mpsc, watch};
use super::{
agent_concurrency::QueueWatch,
session_runtime::SessionRole,
subagent_pool::{
ChildEvent, ChildEventSender, ChildObserver, ExternalRun, RunSession, RunStarter,
StartedSubagent,
},
};
use crate::{
acp_runtime::runtime_children::{ChildRecord, RuntimeChild, RuntimeChildren, StartedChild},
permission::PermissionMode,
run_artifacts::RunArtifactIdentity,
session::SessionPersistence,
subagent::{self, RunPlacement, RESULT_FILE_NAME},
};
/// Where the children of one run are recorded, before the run's agent is
/// known: the session they work for, where their runs go, and who hears of
/// each.
#[derive(Clone)]
pub(crate) struct RecordedChildren {
/// The session the children work for, which deleting deletes them.
pub(crate) works_for: SessionId,
/// Where that session's subagents' runs go.
pub(crate) placement: RunPlacement,
/// Where the run, and its children, work.
pub(crate) workspace: PathBuf,
/// The workspace the run index files the children's runs under
/// ([`subagent::reserve_run_directory`]): the session's own, as for its
/// subagents', even for a run in a worktree.
pub(crate) indexed_under: PathBuf,
/// Who lists each child as a child job.
pub(crate) announce: Announce,
}
/// Who hears of each child, which lists it as a child job.
#[derive(Clone)]
pub(crate) enum Announce {
/// The observer of a session's subagents: the child is the session's.
Observer(Arc<dyn ChildObserver>),
/// The event channel of a run its host follows: the child is the run's.
Events(ChildEventSender),
}
impl RecordedChildren {
/// The host of a run of the agent bound as `identity`, in permission
/// mode `mode`, which each child runs as too.
pub(crate) fn host(
self,
identity: &RunArtifactIdentity,
mode: PermissionMode,
) -> Arc<dyn RuntimeChildren> {
Arc::new(Host {
children: self,
identity: identity.clone(),
mode,
})
}
}
/// [`RecordedChildren`] of a run whose agent is known.
struct Host {
children: RecordedChildren,
identity: RunArtifactIdentity,
mode: PermissionMode,
}
impl RuntimeChildren for Host {
fn started(&self, child: RuntimeChild) -> StartedChild<'_> {
Box::pin(async move {
let placement = self.children.placement.clone();
let indexed_under = self.children.indexed_under.clone();
let (run, directory) = tokio::task::spawn_blocking(move || {
subagent::reserve_run_directory(&placement, &indexed_under)
})
.await
.map_err(|error| error.to_string())?
.map_err(|error| format!("{error:#}"))?;
Ok(self.record(child, run, directory))
})
}
}
impl Host {
/// Child `child`'s run `run`, reserved in `directory`, as its job
/// follows it.
fn record(&self, child: RuntimeChild, run: String, directory: PathBuf) -> ChildRecord {
let RecordedChildren {
works_for,
workspace,
announce,
..
} = &self.children;
let RuntimeChild {
title,
description,
task,
..
} = child;
let output_file = directory.join(RESULT_FILE_NAME);
let shown_as = SessionId::new();
let (events, followed) = mpsc::unbounded_channel();
let prompt = task.text_content();
// The role a saved session would record; a child's is never saved
// (`Ephemeral`), so its provenance is never persisted.
let role = SessionRole::Subagent {
parent: works_for.clone(),
parent_workspace: Some(workspace.clone()),
};
// The child's own children are its job's, which hears of them on
// the child's event channel.
let grandchildren = RecordedChildren {
announce: Announce::Events(events.clone()),
..self.children.clone()
}
.host(&self.identity, self.mode);
let observer = ExternalRun {
events,
session: shown_as.clone(),
cwd: workspace.clone(),
provenance: role.provenance(),
run: run.clone(),
status: output_file.clone(),
prompt: prompt.clone(),
persistence: SessionPersistence::Ephemeral,
}
.observer(&self.identity, self.mode);
let works_for = Some(works_for.to_string());
let mut status = self.identity.starting_status();
status.parent_session_id.clone_from(&works_for);
let (status_tx, status_rx) = watch::channel(status);
// Until the run is titled, it shows as what the agent calls it.
let shown = title
.or(description)
.or_else(|| crate::title::placeholder(&prompt));
let started = StartedSubagent {
run_id: run,
session: Some(RunSession::External(shown_as)),
workspace: workspace.clone(),
status: status_rx,
queue: QueueWatch::default(),
events: Some(followed),
task: shown,
started_by: RunStarter::Runtime,
};
match announce {
Announce::Observer(observer) => observer.subagent_started(started),
// Nobody follows the run any more: its child goes unlisted, as
// a subagent started then would.
Announce::Events(events) => {
let _ = events.send(ChildEvent::RuntimeChild(Box::new(started)));
}
}
ChildRecord {
output_file,
works_for,
identity: self.identity.clone(),
status_tx: Some(status_tx),
observer: Some(observer),
children: Some(grandchildren),
}
}
}