Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 23 additions & 13 deletions src/tools/subagent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,22 +41,32 @@ pub(crate) struct Permit {
_slot: std::fs::File,
}

type TreeSlots = Arc<std::sync::OnceLock<Result<PathBuf, String>>>;
#[derive(Debug, Default)]
struct TreeSlotDirectory {
path: std::sync::OnceLock<PathBuf>,
initialized: std::sync::OnceLock<()>,
}

type TreeSlots = Arc<TreeSlotDirectory>;

/// Slot directory shared by every Kit process in one delegation tree.
fn tree_slot_directory(slots: &TreeSlots) -> Result<&PathBuf, ChildError> {
slots
.get_or_init(|| {
let directory = match std::env::var_os(TREE_SLOTS_ENV) {
Some(directory) => PathBuf::from(directory),
None => std::env::temp_dir().join(format!("kit-subagents-{}", session::new_id())),
};
std::fs::create_dir_all(&directory)
.map_err(|error| format!("could not create {}: {error}", directory.display()))?;
Ok(directory)
})
.as_ref()
.map_err(|error| ChildError::Failed(error.clone()))
let directory = slots
.path
.get_or_init(|| match std::env::var_os(TREE_SLOTS_ENV) {
Some(directory) => PathBuf::from(directory),
None => std::env::temp_dir().join(format!("kit-subagents-{}", session::new_id())),
});
// Cache only success: failed attempts retry the same path. Once initialized,
// callers can reuse the path without new filesystem errors after reserving a slot.
if slots.initialized.get().is_none() {
std::fs::create_dir_all(directory).map_err(|error| {
ChildError::Failed(format!("could not create {}: {error}", directory.display()))
})?;
// Concurrent successful creators publish the same fact; neither owns cleanup.
let _ = slots.initialized.set(());
}
Ok(directory)
}

/// Holds one of the tree-wide slots until dropped or the process exits.
Expand Down
59 changes: 59 additions & 0 deletions src/tools/subagent/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,65 @@ use std::path::{Path, PathBuf};

use super::*;

#[test]
fn tree_slot_directory_failure_is_retryable() {
let root = tempfile::tempdir().unwrap();
let blocker = root.path().join("blocked");
std::fs::write(&blocker, b"not a directory").unwrap();
let directory = blocker.join("slots");
let manager = manager_with_generic_harness(root.path(), Vec::new());
manager.tree_slots.path.set(directory.clone()).unwrap();

for _ in 0..2 {
let error = manager.acquire_permit().unwrap_err();
assert!(error.to_string().contains("could not create"));
assert_eq!(manager.capacity.available_permits(), MAX_LIVE_SUBAGENTS);
}

std::fs::remove_file(&blocker).unwrap();
let permit = manager.acquire_permit().unwrap();
assert!(directory.is_dir());
assert_eq!(
tree_slot_directory(&manager.tree_slots).unwrap(),
&directory
);
drop(permit);
assert_eq!(manager.capacity.available_permits(), MAX_LIVE_SUBAGENTS);
}

#[test]
fn tree_slot_directory_is_shared_across_concurrent_callers() {
let root = tempfile::tempdir().unwrap();
let directory = root.path().join("slots");
let slots = TreeSlots::default();
slots.path.set(directory.clone()).unwrap();

std::thread::scope(|scope| {
for _ in 0..8 {
let slots = Arc::clone(&slots);
let expected = &directory;
scope.spawn(move || {
assert_eq!(tree_slot_directory(&slots).unwrap(), expected);
let _permit = tree_slot(expected).unwrap();
});
}
});
assert!(directory.is_dir());
}

#[test]
fn tree_slot_directory_success_does_not_recheck_filesystem() {
let root = tempfile::tempdir().unwrap();
let directory = root.path().join("slots");
let slots = TreeSlots::default();
slots.path.set(directory.clone()).unwrap();
assert_eq!(tree_slot_directory(&slots).unwrap(), &directory);

std::fs::remove_dir(&directory).unwrap();
std::fs::write(&directory, b"not a directory").unwrap();
assert_eq!(tree_slot_directory(&slots).unwrap(), &directory);
}

fn listing_id(value: &SubagentListing) -> &str {
&value.id
}
Expand Down