bamboo_engine/session_app/child_session/mod.rs
1//! Child session management use cases.
2//!
3//! Provides the application-layer logic for managing child sessions within
4//! a root session. The server layer implements `ChildSessionPort` to supply
5//! the infrastructure operations (load, save, schedule, cancel).
6
7use async_trait::async_trait;
8use bamboo_domain::session::runtime_state::ChildWaitPolicy;
9use bamboo_domain::Session;
10use std::collections::HashMap;
11
12mod actions;
13mod helpers;
14
15#[cfg(test)]
16mod tests;
17
18pub use actions::{
19 assemble_session_tree, build_session_tree_action, cancel_child_action, create_child_action,
20 delete_child_action, get_child_action, list_children_action, run_child_action,
21 send_message_to_child_action, update_child_action, SessionTreeNode,
22};
23pub use helpers::{
24 compute_status_guidance, format_child_assignment, map_child_entry, metadata_text,
25 normalize_non_empty_optional, normalize_required_text, replace_or_append_last_user_message,
26 truncate_after_index, truncate_after_last_user,
27};
28
29// ---------------------------------------------------------------------------
30// Error type
31// ---------------------------------------------------------------------------
32
33#[derive(Debug, thiserror::Error)]
34pub enum ChildSessionError {
35 #[error("session not found: {0}")]
36 NotFound(String),
37 #[error("session is not a root session: {0}")]
38 NotRootSession(String),
39 #[error("session is not a child session: {0}")]
40 NotChildSession(String),
41 #[error("child session {child_id} does not belong to parent {parent_id}")]
42 NotChildOfParent { child_id: String, parent_id: String },
43 #[error("{0}")]
44 InvalidArguments(String),
45 #[error("{0}")]
46 Execution(String),
47}
48
49// ---------------------------------------------------------------------------
50// Value types
51// ---------------------------------------------------------------------------
52
53/// Summary of a child session for listing.
54#[derive(Debug, Clone)]
55pub struct ChildSessionEntry {
56 pub child_session_id: String,
57 pub title: String,
58 pub pinned: bool,
59 pub message_count: usize,
60 pub updated_at: String,
61 pub last_run_status: Option<String>,
62 pub last_run_error: Option<String>,
63}
64
65/// Result of deleting a child session.
66#[derive(Debug, Clone)]
67pub struct DeleteChildResult {
68 pub deleted: bool,
69 pub cancelled_running_child: bool,
70}
71
72/// Diagnostic snapshot of a running child session runner.
73#[derive(Debug, Clone)]
74pub struct ChildRunnerInfo {
75 pub started_at: Option<chrono::DateTime<chrono::Utc>>,
76 pub completed_at: Option<chrono::DateTime<chrono::Utc>>,
77 pub last_tool_name: Option<String>,
78 pub last_tool_phase: Option<String>,
79 pub last_event_at: Option<chrono::DateTime<chrono::Utc>>,
80 pub round_count: u32,
81}
82
83/// Short delegation note appended to the full root-style base prompt for a
84/// sub-agent. A sub-agent is a first-class agent (same base prompt + context
85/// enhancement as a top-level session); this note only frames the delegation
86/// relationship and the expected concise hand-back to the parent.
87pub const DELEGATION_NOTE: &str = r#"---
88
89You are running as a delegated sub-agent, spawned by a parent agent to handle a focused task. You have the full capabilities of a top-level agent, including the ability to spawn your own sub-agents when that helps. Stay focused on the assigned task, and when you finish, report a concise conclusion first (the parent receives your final message as the task result)."#;
90
91/// Input for creating a child session.
92#[derive(Debug, Clone)]
93pub struct CreateChildInput {
94 pub parent_session: Session,
95 pub child_id: String,
96 pub title: String,
97 pub responsibility: String,
98 pub assignment_prompt: String,
99 pub subagent_type: String,
100 /// Absolute path to the working directory for the child session.
101 pub workspace: String,
102 /// Optional model override resolved from subagent_type routing.
103 /// When `None`, the child inherits the parent session's model.
104 pub model_override: Option<String>,
105 /// Optional provider+model override resolved from subagent routing.
106 /// When present, this preserves cross-provider routing for child execution.
107 pub model_ref_override: Option<bamboo_domain::ProviderModelRef>,
108 /// Runtime metadata resolved from subagent routing (e.g. external agent config).
109 pub runtime_metadata: std::collections::HashMap<String, String>,
110 /// Whether to immediately enqueue the child for execution.
111 /// Defaults to `true`.
112 pub auto_run: bool,
113 /// Optional reasoning effort to apply to the child's own LLM calls.
114 /// `None` (the default) leaves `Session::reasoning_effort` at `None`,
115 /// so the provider falls back to its default. The child does NOT
116 /// inherit the parent's reasoning_effort — fan-out children that
117 /// only need a quick lookup should not pay for `xhigh` reasoning
118 /// just because the orchestrator is running at `xhigh`.
119 pub reasoning_effort: Option<bamboo_domain::ReasoningEffort>,
120 /// Lifecycle of this child: `Some("resident")` marks a reusable resident
121 /// agent (one stable session reused for successive tasks under the same
122 /// root); `None`/`Some("oneshot")` is the default throwaway child.
123 pub lifecycle: Option<String>,
124 /// For a resident agent, the stable reuse key (scoped to the root session).
125 pub resident_name: Option<String>,
126 /// For a resident agent, how successive tasks treat prior context:
127 /// `"reset"` (default — independent tasks) or `"accumulate"` (remember).
128 pub resident_context: Option<String>,
129 /// Tool names to disable for this child (denylist; matched by EXACT
130 /// `ToolSchema.function.name`). `None` (the default) = full toolset. A
131 /// read-only Guardian reviewer sets e.g. {"Edit","Write","SubAgent",...}.
132 /// Carried to the child's `SpawnJob.disabled_tools` via the child session
133 /// metadata (see `create_child_action`) so the worker trims its toolset.
134 pub disabled_tools: Option<std::collections::BTreeSet<String>>,
135 /// Model-controllable context fork (Phase 3): when `Some(n)` with `n > 0`,
136 /// the last `n` non-system parent messages are rendered into a "Forked
137 /// context from parent" block prepended to the child's task brief. `None`
138 /// (the default) keeps the child on a clean, freshly-seeded context.
139 pub context_fork: Option<usize>,
140}
141
142/// Result of creating a child session.
143#[derive(Debug, Clone)]
144pub struct CreateChildResult {
145 pub child_session_id: String,
146 pub model: String,
147}
148
149/// A queued follow-up message stored in session metadata for later injection.
150#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
151pub struct QueuedInjectedMessage {
152 pub content: String,
153 #[serde(default)]
154 pub created_at: Option<chrono::DateTime<chrono::Utc>>,
155}
156
157// ---------------------------------------------------------------------------
158// Port trait
159// ---------------------------------------------------------------------------
160
161#[async_trait]
162pub trait ChildSessionPort: Send + Sync {
163 async fn load_root_session(&self, root_id: &str) -> Result<Session, ChildSessionError>;
164 async fn load_child_for_parent(
165 &self,
166 parent_id: &str,
167 child_id: &str,
168 ) -> Result<Session, ChildSessionError>;
169 async fn save_child_session(&self, child: &mut Session) -> Result<(), ChildSessionError>;
170 async fn is_child_running(&self, child_id: &str) -> bool;
171 async fn list_children(&self, parent_id: &str) -> Vec<ChildSessionEntry>;
172 async fn enqueue_child_run(
173 &self,
174 parent: &Session,
175 child: &Session,
176 ) -> Result<(), ChildSessionError>;
177 async fn cancel_child_run_and_wait(&self, child_id: &str) -> Result<(), ChildSessionError>;
178 async fn delete_child_session(
179 &self,
180 parent_id: &str,
181 child_id: &str,
182 ) -> Result<DeleteChildResult, ChildSessionError>;
183 /// Return live diagnostic info for a running child session, if available.
184 async fn get_child_runner_info(&self, child_id: &str) -> Option<ChildRunnerInfo>;
185
186 /// Register a durable parent wait for a single enqueued child. Idempotent
187 /// and coalesced per parent (concurrent sibling spawns merge into one write).
188 async fn register_parent_wait_for_child(
189 &self,
190 parent_session_id: &str,
191 child_session_id: &str,
192 tool_call_id: Option<&str>,
193 ) -> Result<(), ChildSessionError>;
194
195 /// Register a durable parent wait over an explicit set of children with a
196 /// chosen policy (the `SubAgent.wait` action). Returns the number of
197 /// children the wait now covers (0 = nothing to wait on).
198 async fn register_parent_wait_for_children(
199 &self,
200 parent_session_id: &str,
201 child_session_ids: &[String],
202 policy: ChildWaitPolicy,
203 ) -> Result<usize, ChildSessionError>;
204
205 /// The parent's currently-active (non-terminal) child session ids.
206 async fn active_child_ids(&self, parent_session_id: &str) -> Vec<String>;
207
208 /// Find an existing resident agent in the same root tree by its stable
209 /// `resident_name`, returning its child session id if one exists. Used to
210 /// reuse a resident agent for a new task instead of minting a new child.
211 /// Index-backed (matches `root_session_id` + `metadata["resident_name"]`).
212 async fn find_resident_child(
213 &self,
214 root_session_id: &str,
215 resident_name: &str,
216 ) -> Option<String>;
217
218 /// Best-effort: ensure the child's session-index entry is visible
219 /// immediately after creation (the index is otherwise eventually
220 /// consistent). Failures are ignored by the caller.
221 async fn ensure_child_indexed(&self, child_session_id: &str);
222}
223
224// ---------------------------------------------------------------------------
225// Subagent resolution port
226// ---------------------------------------------------------------------------
227
228/// Resolves subagent-type–specific configuration (model, runtime metadata)
229/// for the `SubAgent` tool.
230///
231/// Kept separate from [`ChildSessionPort`] (session CRUD/lifecycle/state): this
232/// port is pure `subagent_type` → config resolution (cross-provider model
233/// routing + actor/external-agent metadata). The server layer implements it;
234/// the tool depends only on the trait, carrying no `AppState` coupling.
235#[async_trait]
236pub trait SubagentResolutionPort: Send + Sync {
237 /// Provider+model ref for a `subagent_type`, or `None` to use defaults.
238 async fn resolve_subagent_model(
239 &self,
240 subagent_type: &str,
241 ) -> Option<bamboo_domain::ProviderModelRef>;
242
243 /// Runtime metadata (e.g. external-agent routing) for a `subagent_type`.
244 async fn resolve_runtime_metadata(&self, subagent_type: &str) -> HashMap<String, String>;
245}
246
247/// Models available from one configured provider (best-effort listing).
248#[derive(Debug, Clone, serde::Serialize)]
249pub struct ProviderModelList {
250 pub provider: String,
251 pub models: Vec<String>,
252 /// Set when this provider's listing failed (auth missing, network, …);
253 /// the provider is still usable with an explicitly known model id.
254 #[serde(skip_serializing_if = "Option::is_none")]
255 pub error: Option<String>,
256}
257
258/// Lists the models the parent can pin a child session to
259/// (`SubAgent` tool `action=list_models` / `create.model`).
260///
261/// Separate from [`SubagentResolutionPort`] because it is backed by the live
262/// provider registry rather than per-`subagent_type` config resolution.
263#[async_trait]
264pub trait ModelCatalogPort: Send + Sync {
265 /// Best-effort model listing per configured provider. Providers whose
266 /// listing fails are still returned (with `error` set) so the caller can
267 /// see they exist.
268 async fn list_models(&self) -> Vec<ProviderModelList>;
269
270 /// The default provider name (used to resolve a bare model id without a
271 /// `provider:` prefix).
272 fn default_provider(&self) -> String;
273}