1use std::path::{Path, PathBuf};
4
5use anyhow::{Context, Result, ensure};
6
7use super::{Controller, now};
8use mj_core::config::HarnessKind;
9use mj_core::state::{SessionRecord, SessionState, new_session_id};
10use mj_core::subagent::SubagentRecord;
11
12#[derive(Debug, Clone)]
13pub struct RegisterSubagentRequest {
14 pub parent_session_id: String,
15 pub task_name: String,
16 pub profile_id: String,
17 pub model: Option<String>,
18 pub effort: Option<String>,
19 pub working_directory: PathBuf,
24 pub initial_prompt: String,
25 pub request_key: String,
26 pub report_root: Option<String>,
30}
31
32const PREPARE_REPORT_DIR_SCRIPT: &str = r#"set -eu
41cd
42mkdir -p -- "$1"
43if [ -n "$2" ] && exclude=$(git -C "$2" rev-parse --git-path info/exclude 2>/dev/null); then
44 case "$exclude" in /*) ;; *) exclude="$2/$exclude" ;; esac
45 line="/$3/"
46 if ! grep -qxF -- "$line" "$exclude" 2>/dev/null; then
47 mkdir -p -- "$(dirname -- "$exclude")"
48 if [ -s "$exclude" ] && [ -n "$(tail -c 1 -- "$exclude")" ]; then
49 printf '
50' >> "$exclude"
51 fi
52 printf '%s
53' "$line" >> "$exclude"
54 fi
55fi
56cd -- "$1"
57pwd -P
58"#;
59
60fn prepare_report_dir_argv(directory: &str, exclude_in: Option<&str>) -> Vec<String> {
63 vec![
64 "sh".into(),
65 "-c".into(),
66 PREPARE_REPORT_DIR_SCRIPT.into(),
67 "sh".into(),
68 directory.into(),
69 exclude_in.unwrap_or_default().into(),
70 mj_core::subagent::PROJECT_REPORT_ROOT_DIR.into(),
71 ]
72}
73
74fn prepare_report_dir(
77 executor: &impl mj_core::targets::CommandExecutor,
78 backend: &mj_core::targets::TargetLocator,
79 session_id: &str,
80 directory: &str,
81 exclude_in: Option<&str>,
82) -> Result<String> {
83 let command = mj_core::targets::command_on_locator(
84 backend,
85 session_id,
86 prepare_report_dir_argv(directory, exclude_in),
87 "create the sub-agent report directory",
88 )?;
89 let output = super::execute_checked(executor, command)?;
90 let absolute = String::from_utf8(output.stdout)
91 .context("the sub-agent report directory is not UTF-8")?
92 .trim_end_matches('\n')
93 .to_owned();
94 ensure!(
95 absolute.starts_with('/'),
96 "the target did not report an absolute sub-agent report directory: {absolute:?}"
97 );
98 Ok(absolute)
99}
100
101impl Controller {
102 pub fn prepare_subagent_report_root(
110 &self,
111 parent_session_id: &str,
112 executor: &impl mj_core::targets::CommandExecutor,
113 ) -> Result<String> {
114 let parent = self
115 .state
116 .sessions
117 .get(parent_session_id)
118 .with_context(|| format!("unknown parent session {parent_session_id}"))?;
119 let locator = parent
120 .target
121 .as_ref()
122 .context("parent session has no live target")?;
123 let backend = super::backend::backend_locator(locator, parent, &self.config)?;
124 let checkout = self.state.checkout(parent_session_id)?;
125 let (root, exclude_in) = subagent_report_root(parent, &backend, &checkout);
126 prepare_report_dir(
127 executor,
128 &backend,
129 parent_session_id,
130 &root,
131 exclude_in.as_deref(),
132 )
133 }
134
135 pub(super) fn prepare_subagent_report_dir(
138 &self,
139 session_id: &str,
140 backend: &mj_core::targets::TargetLocator,
141 executor: &impl mj_core::targets::CommandExecutor,
142 ) -> Result<()> {
143 if crate::database::load_subagent(session_id)?.is_none() {
144 return Ok(());
145 }
146 let Some(directory) = crate::database::load_subagent_report(session_id)?.report_dir else {
147 return Ok(());
148 };
149 prepare_report_dir(executor, backend, session_id, &directory, None)?;
150 Ok(())
151 }
152
153 pub fn register_subagent(
155 &mut self,
156 request: RegisterSubagentRequest,
157 ) -> Result<SubagentRecord> {
158 if let Some(existing) = crate::database::lookup_subagent_request(
159 &request.parent_session_id,
160 &request.request_key,
161 )? {
162 return Ok(existing);
163 }
164 ensure!(
165 !request.request_key.trim().is_empty(),
166 "sub-agent request key cannot be empty"
167 );
168 ensure!(
169 !request.task_name.trim().is_empty(),
170 "sub-agent task name cannot be empty"
171 );
172 ensure!(
173 !request.initial_prompt.trim().is_empty(),
174 "sub-agent instructions cannot be empty"
175 );
176 let parent = self
177 .state
178 .sessions
179 .get(&request.parent_session_id)
180 .with_context(|| format!("unknown parent session {}", request.parent_session_id))?
181 .clone();
182 ensure!(
183 matches!(
184 parent.harness_kind,
185 HarnessKind::Claude | HarnessKind::Codex
186 ),
187 "only Claude and Codex sessions can spawn sub-agents"
188 );
189 ensure_parent_may_delegate(&parent)?;
190 ensure!(
191 parent.state != SessionState::StartupCleanup,
192 "parent startup cleanup is pending"
193 );
194 if let Some(mj_core::subagent::SubagentPolicy::SingleModel { model, effort }) =
195 &parent.subagents
196 {
197 ensure!(
198 request.model.as_ref() == Some(model) && &request.effort == effort,
199 "child selectors must match the parent's fixed model and effort"
200 );
201 }
202 ensure!(parent.state.is_active(), "parent session is not active");
203 ensure!(parent.target.is_some(), "parent session has no live target");
204 ensure!(
205 crate::database::load_subagent(&parent.id)?.is_none(),
206 "sub-agents cannot spawn other sub-agents"
207 );
208 ensure!(
209 self.config
210 .subagents
211 .profile_is_eligible(&parent.last_profile, &request.profile_id),
212 "profile {:?} is not eligible for sub-agent use",
213 request.profile_id
214 );
215 let profile = self
216 .config
217 .enabled_profile(&request.profile_id)
218 .with_context(|| {
219 format!("sub-agent profile {:?} is unavailable", request.profile_id)
220 })?;
221 profile.ensure_ready(&request.profile_id)?;
222 if profile.kind == HarnessKind::Muse {
223 let multiple_roots = !parent.additional_mounts.is_empty()
224 || (self
225 .state
226 .checkout(&parent.id)?
227 .project_directory()
228 .is_none()
229 && parent
230 .project_bundle(&self.config)
231 .is_some_and(|bundle| bundle.repositories.len() > 1));
232 ensure!(
233 !multiple_roots,
234 "{} ACP supports one workspace root; this parent exposes multiple roots",
235 profile.kind.display_name()
236 );
237 }
238 self.ensure_subagent_slot_available(&parent.id, None)?;
239
240 let child_id = new_session_id()?;
241 let target = borrowed_locator(
242 parent.target.as_ref().expect("live target checked above"),
243 &parent.id,
244 &child_id,
245 )?;
246 let created_at = now();
247 let session = SessionRecord {
248 project: parent.project.clone(),
249 target_runtime: Some(parent.target_runtime_settings(&self.config)?.into_owned()),
250 launch_base: None,
251 launch_branch: None,
252 checkout: None,
253 publication: None,
254 build_cache: parent.build_cache.clone(),
257 subagents: Some(mj_core::subagent::SubagentPolicy::None),
260 create_managed_worktree: Some(false),
261 archived: false,
262 container_cpus: None,
263 container_memory: None,
264 container_workspace: parent.container_workspace.clone(),
267 id: child_id.clone(),
268 workspace_id: parent.workspace_id.clone(),
269 title: request.task_name.clone(),
270 harness_kind: profile.kind,
271 last_profile: request.profile_id.clone(),
272 bundle_id: parent.bundle_id.clone(),
273 project_directory: parent.project_directory.clone(),
274 managed_worktree: None,
275 review: None,
276 target_template_id: parent.target_template_id.clone(),
277 resource_allocation: parent.resource_allocation.clone(),
278 additional_mounts: parent.additional_mounts.clone(),
279 state: SessionState::Provisioning,
280 target: Some(target),
281 native_session_id: None,
282 acp_session_title: None,
283 session_title_override: Some(request.task_name.clone()),
284 created_at: created_at.clone(),
285 updated_at: created_at.clone(),
286 viewed_through_event_ordinal: 0,
287 draft_input: String::new(),
288 last_error: None,
289 last_checkpoint_error: None,
290 checkpoint: None,
291 };
292 let handback_tool = child_gets_handback_tool(profile.kind);
293 let report_dir = request
294 .report_root
295 .as_deref()
296 .map(|root| format!("{}/{child_id}", root.trim_end_matches('/')));
297 let initial_prompt = match (handback_tool, &report_dir) {
299 (true, Some(report_dir)) => format!(
300 "{}\n\n{}",
301 mj_core::subagent::handback_prompt_note(report_dir),
302 request.initial_prompt
303 ),
304 _ => request.initial_prompt,
305 };
306 let relation = SubagentRecord {
307 child_session_id: child_id.clone(),
308 parent_session_id: parent.id,
309 task_name: request.task_name,
310 profile_id: request.profile_id,
311 model: request.model,
312 effort: request.effort,
313 working_directory: request.working_directory,
314 initial_prompt,
315 request_key: request.request_key,
316 created_at,
317 noticed_turn: None,
318 reported_finish: None,
319 handback_tool,
320 };
321 crate::database::save_subagent_session(&session, &relation)?;
322 if let Some(report_dir) = &report_dir {
323 crate::database::record_subagent_report_dir(&child_id, report_dir)?;
324 }
325 self.state.sessions.insert(child_id, session);
326 self.state
327 .subagents
328 .insert(relation.child_session_id.clone(), relation.clone());
329 Ok(relation)
330 }
331
332 pub fn ensure_subagent_slot_available(
342 &self,
343 parent_session_id: &str,
344 starting: Option<&str>,
345 ) -> Result<()> {
346 let live = crate::database::list_subagents(parent_session_id)?
347 .into_iter()
348 .filter(|child| Some(child.child_session_id.as_str()) != starting)
349 .filter_map(|child| {
350 let session = self.state.sessions.get(&child.child_session_id)?;
351 session.state.has_live_worker().then(|| LiveSubagent {
352 child_session_id: child.child_session_id.clone(),
353 title: session.listed_title().to_owned(),
354 state: live_subagent_state(session),
355 })
356 })
357 .collect::<Vec<_>>();
358 let maximum = self.config.subagents.max_concurrent;
359 ensure!(live.len() < maximum, "{}", slot_refusal(&live, maximum));
360 Ok(())
361 }
362}
363
364struct LiveSubagent {
367 child_session_id: String,
368 title: String,
369 state: &'static str,
370}
371
372fn live_subagent_state(session: &SessionRecord) -> &'static str {
376 match session.state {
377 SessionState::Provisioning => "starting",
378 SessionState::Checkpointing => "checkpointing",
379 SessionState::Closing | SessionState::Destroying | SessionState::StartupCleanup => {
380 "stopping"
381 }
382 SessionState::Disconnected => "disconnected",
383 _ => match crate::database::load_materialized_session_summary(&session.id) {
384 Ok(Some(summary))
385 if matches!(
386 summary.execution,
387 mj_core::state::MaterializedExecutionState::Idle
388 ) =>
389 {
390 "idle"
391 }
392 _ => "running",
393 },
394 }
395}
396
397fn slot_refusal(live: &[LiveSubagent], maximum: usize) -> String {
400 let listed = live
401 .iter()
402 .map(|child| {
403 format!(
404 "{} \"{}\" ({})",
405 mj_core::state::short_id(&child.child_session_id),
406 child.title,
407 child.state
408 )
409 })
410 .collect::<Vec<_>>()
411 .join(", ");
412 format!(
413 "this session already has {} live sub-agents and the maximum is {maximum}. \
414 Live sub-agents: {listed}. A sub-agent frees its slot when it hands back its \
415 report (it is then parked and holds no processes until you send it input) or \
416 when you close it.",
417 live.len()
418 )
419}
420
421pub fn subagent_has_handed_back(child_session_id: &str) -> Result<bool> {
426 let Some(relation) = crate::database::load_subagent(child_session_id)? else {
427 return Ok(false);
428 };
429 if crate::database::load_session_state(child_session_id)? == Some(SessionState::Parked) {
432 return Ok(true);
433 }
434 let Some((execution, active_turn, last_turn)) =
435 crate::database::load_materialized_turn_outcome(child_session_id)?
436 else {
437 return Ok(false);
438 };
439 let report = crate::database::load_subagent_report(child_session_id)?;
440 let working = active_turn.is_some()
441 || !matches!(execution, mj_core::state::MaterializedExecutionState::Idle);
442 Ok(mj_core::subagent::has_handed_back(
443 relation.handback_tool,
444 &report,
445 working,
446 last_turn.as_ref(),
447 mj_core::clock::epoch_millis(),
448 ))
449}
450
451pub fn stopped_subagent(
454 state: &mj_core::state::State,
455 child_session_id: &str,
456) -> Result<mj_core::subagent::StoppedSubagent> {
457 let relation = state
458 .subagents
459 .get(child_session_id)
460 .with_context(|| format!("unknown sub-agent session {child_session_id}"))?;
461 let title = state
462 .sessions
463 .get(child_session_id)
464 .map_or(relation.task_name.as_str(), SessionRecord::listed_title)
465 .to_owned();
466 let report_dir = crate::database::load_subagent_report(child_session_id)?.report_dir;
467 Ok(mj_core::subagent::StoppedSubagent {
468 child_session_id: child_session_id.to_owned(),
469 title,
470 task: mj_core::subagent::task_summary(&relation.initial_prompt, report_dir.as_deref()),
471 handed_back: subagent_has_handed_back(child_session_id)?,
472 })
473}
474
475fn sibling_path(path: &Path, parent_id: &str, child_id: &str) -> Result<PathBuf> {
476 ensure!(
477 path.ends_with(parent_id),
478 "parent target path does not end in its session id"
479 );
480 Ok(path
481 .parent()
482 .context("parent target path has no parent")?
483 .join(child_id))
484}
485
486fn borrowed_locator(
487 target: &mj_core::state::TargetLocator,
488 parent_id: &str,
489 child_id: &str,
490) -> Result<mj_core::state::TargetLocator> {
491 use mj_core::state::TargetLocator;
492 Ok(match target {
493 TargetLocator::LocalBare { worker_root } => TargetLocator::LocalBare {
494 worker_root: sibling_path(worker_root, parent_id, child_id)?,
495 },
496 TargetLocator::SshBare {
497 host,
498 workspace,
499 worker_id: _,
500 } => TargetLocator::SshBare {
501 host: host.clone(),
502 workspace: workspace.clone(),
503 worker_id: Some(child_id.to_owned()),
504 },
505 TargetLocator::LocalPodman {
509 container_id,
510 workspace_storage,
511 ..
512 } => TargetLocator::LocalPodman {
513 container_id: container_id.clone(),
514 workspace_storage: workspace_storage.clone(),
515 borrowed_from: Some(parent_id.to_owned()),
516 },
517 TargetLocator::LocalDocker { container_id, .. } => TargetLocator::LocalDocker {
518 container_id: container_id.clone(),
519 borrowed_from: Some(parent_id.to_owned()),
520 },
521 TargetLocator::AppleContainer { container_id, .. } => TargetLocator::AppleContainer {
522 container_id: container_id.clone(),
523 borrowed_from: Some(parent_id.to_owned()),
524 },
525 TargetLocator::SshPodman {
526 host,
527 container_id,
528 workspace_storage,
529 ..
530 } => TargetLocator::SshPodman {
531 host: host.clone(),
532 container_id: container_id.clone(),
533 workspace_storage: workspace_storage.clone(),
534 borrowed_from: Some(parent_id.to_owned()),
535 },
536 TargetLocator::SshDocker {
537 host, container_id, ..
538 } => TargetLocator::SshDocker {
539 host: host.clone(),
540 container_id: container_id.clone(),
541 borrowed_from: Some(parent_id.to_owned()),
542 },
543 other @ TargetLocator::AwsEc2 { .. } => other.clone(),
546 })
547}
548
549fn child_gets_handback_tool(harness: HarnessKind) -> bool {
553 matches!(harness, HarnessKind::Codex | HarnessKind::Claude)
554}
555
556fn ensure_parent_may_delegate(parent: &SessionRecord) -> Result<()> {
561 ensure!(
562 parent
563 .subagents
564 .as_ref()
565 .is_some_and(mj_core::subagent::SubagentPolicy::uses_mjolnir),
566 "this session does not allow Mjolnir sub-agents"
567 );
568 Ok(())
569}
570
571fn subagent_report_root(
574 parent: &SessionRecord,
575 backend: &mj_core::targets::TargetLocator,
576 checkout: &mj_core::state::Checkout<'_>,
577) -> (String, Option<String>) {
578 match checkout.project_directory() {
579 Some(project) => {
580 let project = project.to_string_lossy().trim_end_matches('/').to_owned();
581 (
582 format!("{project}/{}", mj_core::subagent::PROJECT_REPORT_ROOT_DIR),
583 Some(project),
584 )
585 }
586 None => {
587 let workspace =
588 super::network_git::workspace_root(backend, parent.container_workspace.as_deref());
589 (
590 format!(
591 "{}/{}",
592 workspace.trim_end_matches('/'),
593 mj_core::subagent::REPORT_ROOT_DIR
594 ),
595 None,
596 )
597 }
598 }
599}
600
601#[cfg(test)]
602mod tests {
603 use super::*;
604
605 fn run_prepare_script(directory: &Path, exclude_in: Option<&Path>) -> String {
606 let argv = prepare_report_dir_argv(
607 &directory.to_string_lossy(),
608 exclude_in
609 .map(|path| path.to_string_lossy().into_owned())
610 .as_deref(),
611 );
612 let output = std::process::Command::new(&argv[0])
613 .args(&argv[1..])
614 .output()
615 .expect("run the report directory script");
616 assert!(
617 output.status.success(),
618 "{}",
619 String::from_utf8_lossy(&output.stderr)
620 );
621 String::from_utf8(output.stdout)
622 .unwrap()
623 .trim_end()
624 .to_owned()
625 }
626
627 #[test]
631 fn the_report_directory_script_creates_the_directory_and_keeps_git_status_clean() {
632 let temp = tempfile::tempdir().unwrap();
633 let project = temp.path().join("project");
634 std::fs::create_dir_all(&project).unwrap();
635 let git = |args: &[&str]| {
636 let output = std::process::Command::new("git")
637 .arg("-C")
638 .arg(&project)
639 .args(args)
640 .output()
641 .unwrap();
642 assert!(output.status.success(), "{output:?}");
643 String::from_utf8(output.stdout).unwrap()
644 };
645 git(&["init", "-q"]);
646 std::fs::write(project.join(".git/info/exclude"), "*.tmp").unwrap();
649 let root = project.join(mj_core::subagent::PROJECT_REPORT_ROOT_DIR);
650 let printed = run_prepare_script(&root, Some(&project));
651 assert_eq!(
652 Path::new(&printed),
653 root.canonicalize().unwrap(),
654 "the script prints the absolute directory"
655 );
656 run_prepare_script(&root, Some(&project));
657 std::fs::write(root.join("report.md"), "details").unwrap();
658 let exclude = std::fs::read_to_string(project.join(".git/info/exclude")).unwrap();
659 assert_eq!(exclude, "*.tmp\n/.mj/agents/\n");
660 assert_eq!(git(&["status", "--porcelain", "--ignored=no"]), "");
661
662 let plain = temp.path().join("plain");
664 std::fs::create_dir_all(&plain).unwrap();
665 let reports = plain.join(".mj/agents/child");
666 run_prepare_script(&reports, Some(&plain));
667 assert!(reports.is_dir());
668 }
669
670 #[test]
671 fn a_report_root_is_under_the_workspace_or_inside_a_bare_project() {
672 let mut parent = super::super::test_support::checkpoint_test_session("parent-1");
673 let backend = mj_core::targets::TargetLocator::LocalBare {
674 worker_root: "/var/lib/hel/workers/parent-1".into(),
675 };
676 parent.project_directory = None;
677 let checkout = parent.checkout();
678 assert_eq!(
679 subagent_report_root(&parent, &backend, &checkout),
680 ("/var/lib/hel/workers/parent-1/.mj-agents".to_owned(), None)
681 );
682 parent.project_directory = Some("/home/dev/project/".into());
683 let checkout = parent.checkout();
684 assert_eq!(
685 subagent_report_root(&parent, &backend, &checkout),
686 (
687 "/home/dev/project/.mj/agents".to_owned(),
688 Some("/home/dev/project".to_owned())
689 )
690 );
691 }
692
693 #[test]
695 fn a_container_child_borrows_its_parents_container() {
696 use mj_core::state::{PodmanWorkspaceLocator, TargetLocator};
697
698 let parent_id = "0123456789abcdef0123456789abcdef";
699 let child_id = "fedcba9876543210fedcba9876543210";
700 let container = mj_core::targets::resource_name(parent_id).unwrap();
701
702 let local = borrowed_locator(
703 &TargetLocator::LocalPodman {
704 container_id: container.clone(),
705 workspace_storage: PodmanWorkspaceLocator::Volume {
706 name: "parent-volume".to_owned(),
707 },
708 borrowed_from: None,
709 },
710 parent_id,
711 child_id,
712 )
713 .unwrap();
714 assert_eq!(
715 local,
716 TargetLocator::LocalPodman {
717 container_id: container.clone(),
718 workspace_storage: PodmanWorkspaceLocator::Volume {
719 name: "parent-volume".to_owned(),
720 },
721 borrowed_from: Some(parent_id.to_owned()),
722 }
723 );
724
725 let remote = borrowed_locator(
726 &TargetLocator::SshPodman {
727 host: "builder".to_owned(),
728 container_id: container.clone(),
729 workspace_storage: PodmanWorkspaceLocator::ContainerLayer,
730 borrowed_from: None,
731 },
732 parent_id,
733 child_id,
734 )
735 .unwrap();
736 assert_eq!(
737 remote,
738 TargetLocator::SshPodman {
739 host: "builder".to_owned(),
740 container_id: container,
741 workspace_storage: PodmanWorkspaceLocator::ContainerLayer,
742 borrowed_from: Some(parent_id.to_owned()),
743 }
744 );
745 }
746
747 #[test]
748 fn a_parent_using_native_delegation_cannot_spawn_mjolnir_children() {
749 let parent = |choice: Option<bool>| {
750 let mut session = crate::controller::test_support::checkpoint_test_session("parent");
751 session.subagents = choice.map(|enabled| {
752 if enabled {
753 mj_core::subagent::SubagentPolicy::AllModels
754 } else {
755 mj_core::subagent::SubagentPolicy::Native
756 }
757 });
758 session
759 };
760
761 assert_eq!(
762 ensure_parent_may_delegate(&parent(Some(false)))
763 .unwrap_err()
764 .to_string(),
765 "this session does not allow Mjolnir sub-agents"
766 );
767 assert_eq!(
768 ensure_parent_may_delegate(&parent(None))
769 .unwrap_err()
770 .to_string(),
771 "this session does not allow Mjolnir sub-agents"
772 );
773 assert!(ensure_parent_may_delegate(&parent(Some(true))).is_ok());
774 }
775
776 #[test]
783 fn the_cap_counts_every_child_holding_processes_and_names_them() {
784 const MARKER: &str = "MJ_TEST_SUBAGENT_CAP_CHILD";
785 if std::env::var_os(MARKER).is_none() {
786 let directory = tempfile::tempdir().unwrap();
787 super::super::test_support::IsolatedTest::new(super::super::test_support::test_name(
788 module_path!(),
789 "the_cap_counts_every_child_holding_processes_and_names_them",
790 ))
791 .env(MARKER, "1")
792 .isolated_store(directory.path())
793 .run();
794 return;
795 }
796 let _writer = crate::database::install_isolated_test_writer();
797 let parent = super::super::test_support::checkpoint_test_session("parent-1");
798 crate::database::save_session(&parent).unwrap();
799 let children = [
800 ("aaaaaaaa-idle", "Audit the lockfile", SessionState::Running),
801 ("bbbbbbbb-busy", "Run the suite", SessionState::Running),
802 ("cccccccc-park", "Map the parser", SessionState::Parked),
803 ("dddddddd-done", "Old task", SessionState::Stopped),
804 ];
805 for (id, title, state) in children {
806 let mut child = super::super::test_support::checkpoint_test_session(id);
807 child.state = state;
808 child.session_title_override = Some(title.into());
809 crate::database::save_subagent_session(
810 &child,
811 &SubagentRecord {
812 child_session_id: id.into(),
813 parent_session_id: "parent-1".into(),
814 task_name: title.into(),
815 profile_id: "codex".into(),
816 model: None,
817 effort: None,
818 working_directory: PathBuf::new(),
819 initial_prompt: "do it".into(),
820 request_key: format!("request-{id}"),
821 created_at: "2026-09-25T00:00:00Z".into(),
822 noticed_turn: None,
823 reported_finish: None,
824 handback_tool: true,
825 },
826 )
827 .unwrap();
828 }
829 let mut controller = Controller {
830 config: mj_core::config::Config::default(),
831 state: crate::database::load_state().unwrap(),
832 };
833 controller.config.subagents.max_concurrent = 2;
834
835 let refusal = controller
837 .ensure_subagent_slot_available("parent-1", None)
838 .unwrap_err()
839 .to_string();
840 assert!(
841 refusal.contains("already has 2 live sub-agents and the maximum is 2"),
842 "{refusal}"
843 );
844 assert!(
845 refusal.contains("aaaaaaaa \"Audit the lockfile\"")
846 && refusal.contains("bbbbbbbb \"Run the suite\""),
847 "the refusal names every live child: {refusal}"
848 );
849 assert!(
850 !refusal.contains("Map the parser") && !refusal.contains("Old task"),
851 "parked and stopped children hold no processes: {refusal}"
852 );
853 assert!(
854 refusal.contains("hands back")
855 && refusal.contains("parked")
856 && refusal.contains("close it"),
857 "the refusal says how a slot frees up: {refusal}"
858 );
859 assert!(
861 controller
862 .ensure_subagent_slot_available("parent-1", Some("cccccccc-park"))
863 .is_err()
864 );
865 controller
867 .ensure_subagent_slot_available("parent-1", Some("aaaaaaaa-idle"))
868 .unwrap();
869
870 controller
872 .state
873 .sessions
874 .get_mut("aaaaaaaa-idle")
875 .unwrap()
876 .state = SessionState::Parked;
877 controller
878 .ensure_subagent_slot_available("parent-1", None)
879 .unwrap();
880 controller
881 .ensure_subagent_slot_available("parent-1", Some("cccccccc-park"))
882 .unwrap();
883 }
884}