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).
This commit is contained in:
@@ -23,6 +23,37 @@ use zesdex_domain::subagent_directive;
|
||||
/// Maximum number of tool-call iterations before the engine gives up.
|
||||
const MAX_ITERATIONS: u32 = 25;
|
||||
|
||||
/// A single tool-result message is truncated before entering the subagent's
|
||||
/// context so it cannot blow the window (matches the main turn service).
|
||||
const TOOL_OUTPUT_MAX_CHARS: usize = 12_000;
|
||||
|
||||
/// Maximum consecutive identical tool errors before the engine injects a
|
||||
/// recovery note steering the model to a different approach.
|
||||
const MAX_CONSECUTIVE_TOOL_ERRORS: usize = 3;
|
||||
|
||||
/// Pick a `max_tokens` budget proportional to the directive's length.
|
||||
fn adaptive_max_tokens(directive_len: usize) -> u32 {
|
||||
if directive_len <= 80 {
|
||||
800
|
||||
} else if directive_len <= 400 {
|
||||
1600
|
||||
} else {
|
||||
4096
|
||||
}
|
||||
}
|
||||
|
||||
fn truncate_tool_output(output: String) -> String {
|
||||
if output.len() <= TOOL_OUTPUT_MAX_CHARS {
|
||||
return output;
|
||||
}
|
||||
let mut result: String = output.chars().take(TOOL_OUTPUT_MAX_CHARS).collect();
|
||||
result.push_str(&format!(
|
||||
"\n...[truncated {} chars]",
|
||||
output.len() - TOOL_OUTPUT_MAX_CHARS
|
||||
));
|
||||
result
|
||||
}
|
||||
|
||||
/// Emit an `AgentProgress` event onto the turn-event queue, if one is
|
||||
/// configured in the `ToolCtx`.
|
||||
fn report_progress(tool_ctx: &ToolCtx, progress: AgentProgress) {
|
||||
@@ -85,11 +116,17 @@ pub async fn run_agent(
|
||||
Some(ctx.base_url.clone()),
|
||||
);
|
||||
|
||||
let max_tokens = adaptive_max_tokens(directive.len());
|
||||
|
||||
// Track repeated tool errors so the agent can recover from a dead end.
|
||||
let mut consecutive_errors = 0usize;
|
||||
let mut last_tool = String::new();
|
||||
|
||||
// Limited iteration loop so we don't run forever
|
||||
for iteration in 0..MAX_ITERATIONS {
|
||||
use zesdex_application::ports::ProviderService;
|
||||
let (response_msg, _usage) = client
|
||||
.chat(&messages, Some(defs.clone()), Some(4096), None)
|
||||
.chat(&messages, Some(defs.clone()), Some(max_tokens), Some(0.2))
|
||||
.await?;
|
||||
|
||||
let content = response_msg.content.clone().unwrap_or_default();
|
||||
@@ -113,7 +150,7 @@ pub async fn run_agent(
|
||||
&tool_ctx,
|
||||
AgentProgress::running(
|
||||
"subagent",
|
||||
format!("{}:{}", directive, tool_name),
|
||||
format!("{}:{tool_name}", directive),
|
||||
Some(tool_name.clone()),
|
||||
),
|
||||
);
|
||||
@@ -127,7 +164,29 @@ pub async fn run_agent(
|
||||
format!("Unknown tool: {tool_name}")
|
||||
};
|
||||
|
||||
messages.push(ChatMessage::tool(tc.id.clone(), result));
|
||||
// Error-recovery: if the same tool keeps failing, inject a
|
||||
// system note steering the model to a different approach.
|
||||
if result.starts_with("Error:") {
|
||||
if last_tool.as_str() == tool_name.as_str() {
|
||||
consecutive_errors += 1;
|
||||
} else {
|
||||
consecutive_errors = 1;
|
||||
last_tool = tool_name.to_string();
|
||||
}
|
||||
if consecutive_errors >= MAX_CONSECUTIVE_TOOL_ERRORS {
|
||||
messages.push(ChatMessage::system(
|
||||
zesdex_domain::agent::prompt::error_recovery_note(tool_name, &result),
|
||||
));
|
||||
consecutive_errors = 0;
|
||||
}
|
||||
} else {
|
||||
consecutive_errors = 0;
|
||||
}
|
||||
|
||||
messages.push(ChatMessage::tool(
|
||||
tc.id.clone(),
|
||||
truncate_tool_output(result),
|
||||
));
|
||||
}
|
||||
|
||||
// Add assistant response if there was text content
|
||||
|
||||
@@ -32,7 +32,6 @@ pub fn spawn_subagent(
|
||||
) -> thread::JoinHandle<Result<String>> {
|
||||
info!("Spawning subagent: {directive}");
|
||||
thread::spawn(move || {
|
||||
let rt = tokio::runtime::Runtime::new()?;
|
||||
rt.block_on(run_agent(ctx, &directive, access, tool_ctx))
|
||||
crate::runtime::runtime().block_on(run_agent(ctx, &directive, access, tool_ctx))
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user