Files
zesdex/apps/infrastructure/src/subagent/spawn.rs
T
asepharyana 6a98d52d54 perf(agent): stabilkan async & parallel — satu runtime, bounded concurrency, isolasi error
Seperti Claude Code: satu runtime shared, concurrency dibatasi, error
subagent terisolasi (satu node gagal tidak menggagalkan cycle).

- feat(runtime): global tokio runtime via OnceLock — ganti 9+ titik
  Runtime::new() per tool call (spawn, parallel_delegate, workflow,
  explore, dir_cache, daemon handler). Hemat resource, hilangkan panic
  path Runtime::new().expect() di daemon compaction.
- fix(workflow): execute_cycle ganti try_join_all (fail-fast) →
  buffer_unordered(8) + isolasi error per node; node gagal di-log dan
  diganti [ERROR], hasil node lain tetap dipakai (Claude Code-style).
- fix(parallel_delegate): spawn subagent dibatasi per batch max_parallel
  (tidak unbounded threads).
- perf(subagent): run_agent adaptif max_tokens (800/1600/4096), temp 0.2,
  truncate tool output 12k, error-recovery note utk tool error berulang.
- test: runtime singleton + block_on (2 test).
2026-08-27 23:25:44 +07:00

38 lines
1.2 KiB
Rust

//! Subagent spawning — launch a subagent on a background OS thread.
//!
//! Flow: creates a new tokio runtime on a dedicated OS thread, then
//! `block_on` the engine's `run_agent` future. Returns a
//! `JoinHandle<Result<String>>` the caller can `.join()`.
use std::thread;
use anyhow::Result;
use tracing::{info, instrument};
use crate::subagent::context::SubagentContext;
use crate::subagent::division::AccessTier;
use crate::subagent::engine::run_agent;
use crate::tools::ToolCtx;
/// Spawn a subagent on a background OS thread.
///
/// The subagent runs inside its own tokio runtime so it can make async calls
/// without blocking the calling thread's runtime.
///
/// Flow: `thread::spawn` → create `tokio::runtime::Runtime` →
/// `runtime.block_on(run_agent(...))` → return.
///
/// Returns a `JoinHandle` the caller can `join()` to await the result.
#[instrument(skip(ctx, tool_ctx))]
pub fn spawn_subagent(
ctx: SubagentContext,
directive: String,
access: AccessTier,
tool_ctx: ToolCtx,
) -> thread::JoinHandle<Result<String>> {
info!("Spawning subagent: {directive}");
thread::spawn(move || {
crate::runtime::runtime().block_on(run_agent(ctx, &directive, access, tool_ctx))
})
}