mirror of
https://github.com/outbackdingo/optimclaw.git
synced 2026-08-25 14:53:34 +00:00
* fix(ci): secrets can't be used in step if conditions [skip-regression-check] (#787) GitHub Actions step-level `if:` doesn't have access to `secrets` context. Replace `if: secrets.X != ''` with `continue-on-error: true` and let the Set token step handle the fallback. Co-authored-by: Claude Sonnet 4.6 <[email protected]> * fix(ci): clean up staging pipeline — remove hacks, skip redundant checks [skip-regression-check] (#794) - Remove continue-on-error from staging-ci.yml app token steps (secrets are configured) - Skip test.yml and code_style.yml on PRs targeting staging (staging-ci.yml already runs tests before promoting, promotion PR gets full CI on main) - Allow ironclaw-ci[bot] in Claude Code review for bot-created promotion PRs Co-authored-by: Claude Opus 4.6 <[email protected]> * fix(ci): run fmt + clippy on staging PRs, skip Windows clippy [skip-regression-check] (#802) - Remove branches:[main] filter from code_style.yml so it runs on all PRs - Gate clippy-windows with `if: github.base_ref == 'main'` (skip on staging PRs) - Update rollup job to allow skipped clippy-windows - Simplify claude-review.yml to only trigger on labeled event (avoids duplicate runs) Co-authored-by: Claude Opus 4.6 <[email protected]> * feat: persist user_id in save_job and expose job_id on routine runs (#709) * feat: persist worker events to DB and fix activity tab rendering In-process Worker (used by Scheduler::dispatch_job) now persists events via save_job_event at key execution points: plan creation, LLM responses, tool_use, tool_result, and job completion/failure/stuck. Event data shapes match the container worker format so the gateway activity tab renders them correctly. Frontend: tool_result errors now show a red X icon with danger styling instead of a silent empty output. The result event falls back to the error field when message is absent. Co-Authored-By: Claude Opus 4.6 <[email protected]> * feat: wire RoutineEngine into gateway for direct manual trigger firing Replace the message-channel hack in routines_trigger_handler with a direct call to RoutineEngine::fire_manual(), ensuring FullJob routines dispatch correctly when triggered from the web UI. Inject the engine into GatewayState from Agent::run after construction. Also persists user_id in save_job for both PG and libSQL backends, removes the source='sandbox' filter so all jobs are visible, and exposes job_id on RoutineRunInfo for the frontend job link. Co-Authored-By: Claude Opus 4.6 <[email protected]> * fix: remove stale gateway_state argument from Agent::new test call sites The gateway_state parameter was removed from Agent::new during rebase (replaced by post-construction set_routine_engine_slot), but three test call sites still passed the extra None argument. Co-Authored-By: Claude Opus 4.6 <[email protected]> * fix: address PR review — restore sandbox source filter, remove blank lines - Revert removal of `source = 'sandbox'` filter in all SandboxStore queries (8 sites across PG and libSQL). Sandbox-specific APIs should stay scoped to sandbox jobs; unified job listing for the Jobs tab should use a separate query path. - Remove extra blank lines in agent_loop.rs and worker.rs that caused formatting CI failure. [skip-regression-check] Co-Authored-By: Claude Opus 4.6 <[email protected]> * fix: address review — regenerate Cargo.lock, add user_id regression test - Regenerate Cargo.lock from main's lockfile to eliminate dependency version downgrades (anyhow, syn, etc.) that were churn from rebase. - Add regression test verifying user_id round-trips through save_job and get_job in the libSQL backend. Co-Authored-By: Claude Opus 4.6 <[email protected]> * style: remove trailing blank line in libsql jobs.rs [skip-regression-check] Co-Authored-By: Claude Opus 4.6 <[email protected]> * test: add Postgres-side regression test for user_id persistence in save_job Mirrors the existing libSQL test (test_save_job_persists_user_id) for the Postgres backend. Gated behind #[cfg(feature = "postgres")] + #[ignore] since it requires a running PostgreSQL instance (integration tier). Co-Authored-By: Claude Opus 4.6 <[email protected]> --------- Co-authored-by: Claude Opus 4.6 <[email protected]> * refactor: unify three agentic loops into single AgenticLoop engine (#654) Replace three independent copy-pasted agentic loops (dispatcher, worker, container runtime) with a single shared engine in `agentic_loop.rs` that all consumers customize via the `LoopDelegate` trait. Phase 1 — Shared engine (`src/agent/agentic_loop.rs`, 205 lines): - `run_agentic_loop()` owns the core LLM → tool exec → repeat cycle - `LoopDelegate` trait (Send + Sync, &dyn dispatch) with 6 hook points - Tool intent nudge logic consolidated (was duplicated in 3 files) - Iteration limit + force-text behavior preserved Phase 2 — Three delegate implementations: - `ChatDelegate` (dispatcher.rs): 3-phase approval flow, hooks, cost guard, context compaction, skill attenuation, interruption - `JobDelegate` (worker/job.rs): planning pre-loop phase, parallel JoinSet exec, mark_completed/stuck/failed, SSE streaming, self-repair - `ContainerDelegate` (worker/container.rs): sequential tool exec, HTTP-proxied LLM, container-safe tools, credential injection Phase 3 — File moves and cleanup: - Delete `src/agent/worker.rs` — job logic moved to `src/worker/job.rs` - Rename `src/worker/runtime.rs` → `src/worker/container.rs` - Re-export `Worker`/`WorkerDeps` from `crate::worker` in `agent/mod.rs` - Update `scheduler.rs` imports to new worker location Shared helpers (`src/tools/execute.rs`): - `execute_tool_with_safety()` replaces 4 copies of validate → timeout → execute → serialize - `process_tool_result()` replaces 3 copies of sanitize → wrap → ChatMessage (also used by thread_ops.rs approval resume paths) Net result: -2,408 lines, zero duplicated loop logic, single code path for tool intent nudge and completion detection. Closes #654 Co-Authored-By: Claude Opus 4.6 <[email protected]> * fix: address review feedback from Copilot 1. scheduler.rs: Replace `unwrap_or` fallback with proper error propagation when parsing tool output JSON — surfaces bugs instead of silently changing the output type. 2. worker/job.rs: Drop MutexGuard before the cancellation `.await` in `check_signals()` to avoid holding a lock across an async I/O call (prevents `await_holding_lock` lint). 3. worker/job.rs: Restore consecutive rate-limit counter (MAX_CONSECUTIVE_RATE_LIMITS = 10) so sustained rate limiting marks the job stuck with "Persistent rate limiting" instead of silently burning through max_iterations. Co-Authored-By: Claude Opus 4.6 <[email protected]> * fix: incorporate staging changes — token budget tracking + mark_failed Merge staging's changes into the refactored JobDelegate: - Add token budget tracking in call_llm (update_context/add_tokens) - mark_stuck → mark_failed for iteration cap and rate-limit exhaustion (aligns with staging's #788 fix) Co-Authored-By: Claude Opus 4.6 <[email protected]> * fix: address zmanian's PR review — eliminate type erasure, clean up Address all 6 review points from zmanian on PR #800: 1. Replace LoopOutcome::Custom(Box<dyn Any>) with typed LoopOutcome::NeedApproval(Box<PendingApproval>) — eliminates type erasure and downcast, resolves clippy large_enum_variant. 2. Remove dead max_tool_iterations field from ChatDelegate struct. 3. Add on_tool_intent_nudge() hook to LoopDelegate trait with implementations in Job and Container delegates for observability. 4. Fix SSE events in job worker to emit raw sanitized content instead of XML-wrapped <tool_output> tags. 5. Remove 4 duplicate completion tests from job.rs that were already covered by the shared util module. 6. Avoid logging full tool results — use result_size_bytes in debug logs (execute.rs, job.rs). Also updates path references in CLAUDE.md, COVERAGE_PLAN.md, and add-sse-event.md command. Co-Authored-By: Claude Opus 4.6 <[email protected]> * feat(doctor): expand diagnostics from 7 to 16 health checks * test: add unit tests for agentic_loop and execute shared modules Add 16 tests covering the two new critical shared modules: agentic_loop.rs (10 tests): - Text response exits loop immediately - Tool call → text response continuation - LoopSignal::Stop exits before LLM call - LoopSignal::InjectMessage adds user message to context - Max iterations terminates with LoopOutcome::MaxIterations - Tool intent nudge fires twice then caps - before_llm_call early exit bypasses LLM - truncate_for_preview: short string, long string, multibyte safety execute.rs (6 tests): - execute_tool_with_safety success path - Missing tool returns ToolError::NotFound - Tool execution failure propagates - Per-tool timeout enforcement (50ms) - process_tool_result XML wrapping on success - process_tool_result error formatting All 2,777 unit tests pass, 0 clippy warnings. Co-Authored-By: Claude Opus 4.6 <[email protected]> * style: cargo fmt Co-Authored-By: Claude Opus 4.6 <[email protected]> * fix: address code review — 9 issues across agentic loop, job worker, container CRITICAL fixes: - Rate-limit exhaustion now returns Err(LlmError::RateLimited) instead of Ok(Text("")), stopping the loop immediately with no ghost iteration. Below-threshold retries still use Text("") with an explicit empty-string guard in handle_text_response to skip injection. - check_signals drains the entire message channel before returning, prioritizing Stop over UserMessage. Previously returned early on first UserMessage, silently dropping any queued Stop or additional messages. - check_signals now detects all non-progressing job states (Cancelled, Failed, Stuck, Completed, Submitted, Accepted) instead of only Cancelled and Failed. HIGH fixes: - Error path in process_tool_result_job applies truncate_for_preview to bound error strings in SSE/DB events (was unbounded). - Document Send+Sync lifetime constraint on LoopDelegate trait. - Test mock before_llm_call refactored from double-lock to single lock acquisition, eliminating deadlock risk on refactor. MEDIUM fixes: - CompletionReport includes actual iteration count via shared Arc<Mutex<u32>> tracker (was hardcoded 0). - process_tool_result_job return type changed from Result<bool> to Result<()> — the bool was always false (dead API). - Deduplicate truncate in container.rs; now uses truncate_for_preview from agentic_loop. Verified: 0 clippy warnings, 2781 tests pass, cargo fmt clean. Co-Authored-By: Claude Opus 4.6 <[email protected]> --------- Co-authored-by: Henry Park <[email protected]> Co-authored-by: Claude Sonnet 4.6 <[email protected]> Co-authored-by: Illia Polosukhin <[email protected]> Co-authored-by: Umesh Kumar Singh <[email protected]> Co-authored-by: reidliu41 <[email protected]>
588 lines
20 KiB
Rust
588 lines
20 KiB
Rust
//! Unified agentic loop engine.
|
|
//!
|
|
//! Provides a single implementation of the core LLM call → tool execution →
|
|
//! result processing → context update → repeat cycle. Three consumers
|
|
//! (chat dispatcher, job worker, container runtime) customize behavior
|
|
//! via the `LoopDelegate` trait.
|
|
|
|
use async_trait::async_trait;
|
|
|
|
use crate::agent::session::PendingApproval;
|
|
use crate::error::Error;
|
|
use crate::llm::{ChatMessage, Reasoning, ReasoningContext, RespondResult};
|
|
|
|
/// Signal from the delegate indicating how the loop should proceed.
|
|
pub enum LoopSignal {
|
|
/// Continue normally.
|
|
Continue,
|
|
/// Stop the loop gracefully.
|
|
Stop,
|
|
/// Inject a user message into context and continue.
|
|
InjectMessage(String),
|
|
}
|
|
|
|
/// Outcome of a text response from the LLM.
|
|
pub enum TextAction {
|
|
/// Return this as the final loop result.
|
|
Return(LoopOutcome),
|
|
/// Continue the loop (text was handled but loop should proceed).
|
|
Continue,
|
|
}
|
|
|
|
/// Final outcome of the agentic loop.
|
|
pub enum LoopOutcome {
|
|
/// Completed with a text response.
|
|
Response(String),
|
|
/// Loop was stopped by a signal.
|
|
Stopped,
|
|
/// Max iterations exceeded.
|
|
MaxIterations,
|
|
/// A tool requires user approval before continuing (chat delegate only).
|
|
NeedApproval(Box<PendingApproval>),
|
|
}
|
|
|
|
/// Configuration for the agentic loop.
|
|
pub struct AgenticLoopConfig {
|
|
pub max_iterations: usize,
|
|
pub enable_tool_intent_nudge: bool,
|
|
pub max_tool_intent_nudges: u32,
|
|
}
|
|
|
|
impl Default for AgenticLoopConfig {
|
|
fn default() -> Self {
|
|
Self {
|
|
max_iterations: 50,
|
|
enable_tool_intent_nudge: true,
|
|
max_tool_intent_nudges: 2,
|
|
}
|
|
}
|
|
}
|
|
|
|
/// Strategy trait — each consumer implements this to customize I/O and lifecycle.
|
|
///
|
|
/// The shared loop calls these methods at well-defined points. Consumers
|
|
/// implement only the behavior that differs between chat, job, and container
|
|
/// contexts. The loop itself handles the common logic: tool intent nudge,
|
|
/// iteration counting, tool definition refresh, and the respond → execute → process cycle.
|
|
///
|
|
/// # `Send + Sync` requirement
|
|
///
|
|
/// This trait requires `Send + Sync` because the loop accepts `&dyn LoopDelegate`.
|
|
/// Delegates using borrowed references (e.g. `ChatDelegate<'a>`) must ensure all
|
|
/// borrowed fields are `Send + Sync`. This is a load-bearing constraint: if a
|
|
/// delegate needs to be spawned into a detached task, it must use `Arc`-based
|
|
/// ownership instead of borrows (as `JobDelegate` and `ContainerDelegate` do).
|
|
#[async_trait]
|
|
pub trait LoopDelegate: Send + Sync {
|
|
/// Called at the start of each iteration. Check for external signals
|
|
/// (cancellation, user messages, stop requests).
|
|
async fn check_signals(&self) -> LoopSignal;
|
|
|
|
/// Called before the LLM call. Allows the delegate to refresh tool
|
|
/// definitions, enforce cost guards, or inject messages.
|
|
/// Return `Some(outcome)` to break the loop early.
|
|
async fn before_llm_call(
|
|
&self,
|
|
reason_ctx: &mut ReasoningContext,
|
|
iteration: usize,
|
|
) -> Option<LoopOutcome>;
|
|
|
|
/// Call the LLM and return the result. Delegates own the LLM call
|
|
/// to handle consumer-specific concerns (rate limiting, auto-compaction,
|
|
/// cost tracking, force_text mode).
|
|
async fn call_llm(
|
|
&self,
|
|
reasoning: &Reasoning,
|
|
reason_ctx: &mut ReasoningContext,
|
|
iteration: usize,
|
|
) -> Result<crate::llm::RespondOutput, Error>;
|
|
|
|
/// Handle a text-only response from the LLM.
|
|
/// Return `TextAction::Return` to exit the loop, `TextAction::Continue` to proceed.
|
|
async fn handle_text_response(
|
|
&self,
|
|
text: &str,
|
|
reason_ctx: &mut ReasoningContext,
|
|
) -> TextAction;
|
|
|
|
/// Execute tool calls and add results to context.
|
|
/// Return `Some(outcome)` to break the loop (e.g. approval needed).
|
|
async fn execute_tool_calls(
|
|
&self,
|
|
tool_calls: Vec<crate::llm::ToolCall>,
|
|
content: Option<String>,
|
|
reason_ctx: &mut ReasoningContext,
|
|
) -> Result<Option<LoopOutcome>, Error>;
|
|
|
|
/// Called when the LLM expresses tool intent without actually calling a tool.
|
|
/// Delegates can use this to emit events or log the nudge for observability.
|
|
async fn on_tool_intent_nudge(&self, _text: &str, _reason_ctx: &mut ReasoningContext) {}
|
|
|
|
/// Called after each successful iteration (no error, no early return).
|
|
async fn after_iteration(&self, _iteration: usize) {}
|
|
}
|
|
|
|
/// Run the unified agentic loop.
|
|
///
|
|
/// This is the single implementation used by all three consumers (chat, job, container).
|
|
/// The `delegate` provides consumer-specific behavior via the `LoopDelegate` trait.
|
|
pub async fn run_agentic_loop(
|
|
delegate: &dyn LoopDelegate,
|
|
reasoning: &Reasoning,
|
|
reason_ctx: &mut ReasoningContext,
|
|
config: &AgenticLoopConfig,
|
|
) -> Result<LoopOutcome, Error> {
|
|
let mut consecutive_tool_intent_nudges: u32 = 0;
|
|
|
|
for iteration in 1..=config.max_iterations {
|
|
// Check for external signals (stop, cancellation, user messages)
|
|
match delegate.check_signals().await {
|
|
LoopSignal::Continue => {}
|
|
LoopSignal::Stop => return Ok(LoopOutcome::Stopped),
|
|
LoopSignal::InjectMessage(msg) => {
|
|
reason_ctx.messages.push(ChatMessage::user(&msg));
|
|
}
|
|
}
|
|
|
|
// Pre-LLM call hook (cost guard, tool refresh, iteration limit nudge)
|
|
if let Some(outcome) = delegate.before_llm_call(reason_ctx, iteration).await {
|
|
return Ok(outcome);
|
|
}
|
|
|
|
// Call LLM
|
|
let output = delegate.call_llm(reasoning, reason_ctx, iteration).await?;
|
|
|
|
match output.result {
|
|
RespondResult::Text(text) => {
|
|
// Tool intent nudge: if the LLM says "let me search..." without
|
|
// actually calling a tool, inject a nudge message.
|
|
if config.enable_tool_intent_nudge
|
|
&& !reason_ctx.available_tools.is_empty()
|
|
&& !reason_ctx.force_text
|
|
&& consecutive_tool_intent_nudges < config.max_tool_intent_nudges
|
|
&& crate::llm::llm_signals_tool_intent(&text)
|
|
{
|
|
consecutive_tool_intent_nudges += 1;
|
|
tracing::info!(
|
|
iteration,
|
|
"LLM expressed tool intent without calling a tool, nudging"
|
|
);
|
|
delegate.on_tool_intent_nudge(&text, reason_ctx).await;
|
|
reason_ctx.messages.push(ChatMessage::assistant(&text));
|
|
reason_ctx
|
|
.messages
|
|
.push(ChatMessage::user(crate::llm::TOOL_INTENT_NUDGE));
|
|
delegate.after_iteration(iteration).await;
|
|
continue;
|
|
}
|
|
|
|
// Reset nudge counter since we got a non-intent text response
|
|
if !crate::llm::llm_signals_tool_intent(&text) {
|
|
consecutive_tool_intent_nudges = 0;
|
|
}
|
|
|
|
match delegate.handle_text_response(&text, reason_ctx).await {
|
|
TextAction::Return(outcome) => return Ok(outcome),
|
|
TextAction::Continue => {}
|
|
}
|
|
}
|
|
RespondResult::ToolCalls {
|
|
tool_calls,
|
|
content,
|
|
} => {
|
|
consecutive_tool_intent_nudges = 0;
|
|
|
|
if let Some(outcome) = delegate
|
|
.execute_tool_calls(tool_calls, content, reason_ctx)
|
|
.await?
|
|
{
|
|
return Ok(outcome);
|
|
}
|
|
}
|
|
}
|
|
|
|
delegate.after_iteration(iteration).await;
|
|
}
|
|
|
|
Ok(LoopOutcome::MaxIterations)
|
|
}
|
|
|
|
/// Truncate a string for log/status previews.
|
|
///
|
|
/// `max` is a byte budget. The result is truncated at the last valid char
|
|
/// boundary at or before `max` bytes, so it is always valid UTF-8.
|
|
pub fn truncate_for_preview(s: &str, max: usize) -> String {
|
|
if s.len() <= max {
|
|
s.to_string()
|
|
} else {
|
|
let end = crate::util::floor_char_boundary(s, max);
|
|
format!("{}...", &s[..end])
|
|
}
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use super::*;
|
|
use crate::llm::{RespondOutput, TokenUsage, ToolCall};
|
|
use crate::testing::StubLlm;
|
|
use std::sync::Arc;
|
|
use std::sync::atomic::{AtomicUsize, Ordering};
|
|
use tokio::sync::Mutex;
|
|
|
|
fn stub_reasoning() -> Reasoning {
|
|
Reasoning::new(Arc::new(StubLlm::default()))
|
|
}
|
|
|
|
fn zero_usage() -> TokenUsage {
|
|
TokenUsage {
|
|
input_tokens: 0,
|
|
output_tokens: 0,
|
|
cache_read_input_tokens: 0,
|
|
cache_creation_input_tokens: 0,
|
|
}
|
|
}
|
|
|
|
fn text_output(text: &str) -> RespondOutput {
|
|
RespondOutput {
|
|
result: RespondResult::Text(text.to_string()),
|
|
usage: zero_usage(),
|
|
}
|
|
}
|
|
|
|
fn tool_calls_output(calls: Vec<ToolCall>) -> RespondOutput {
|
|
RespondOutput {
|
|
result: RespondResult::ToolCalls {
|
|
tool_calls: calls,
|
|
content: None,
|
|
},
|
|
usage: zero_usage(),
|
|
}
|
|
}
|
|
|
|
/// Configurable mock delegate for testing run_agentic_loop.
|
|
struct MockDelegate {
|
|
signal: Mutex<LoopSignal>,
|
|
llm_responses: Mutex<Vec<RespondOutput>>,
|
|
tool_exec_count: AtomicUsize,
|
|
tool_exec_outcome: Mutex<Option<LoopOutcome>>,
|
|
iterations_seen: Mutex<Vec<usize>>,
|
|
early_exit: Mutex<Option<(usize, LoopOutcome)>>,
|
|
nudge_count: AtomicUsize,
|
|
}
|
|
|
|
impl MockDelegate {
|
|
fn new(responses: Vec<RespondOutput>) -> Self {
|
|
Self {
|
|
signal: Mutex::new(LoopSignal::Continue),
|
|
llm_responses: Mutex::new(responses),
|
|
tool_exec_count: AtomicUsize::new(0),
|
|
tool_exec_outcome: Mutex::new(None),
|
|
iterations_seen: Mutex::new(Vec::new()),
|
|
early_exit: Mutex::new(None),
|
|
nudge_count: AtomicUsize::new(0),
|
|
}
|
|
}
|
|
|
|
fn with_signal(mut self, signal: LoopSignal) -> Self {
|
|
self.signal = Mutex::new(signal);
|
|
self
|
|
}
|
|
|
|
fn with_early_exit(mut self, iteration: usize, outcome: LoopOutcome) -> Self {
|
|
self.early_exit = Mutex::new(Some((iteration, outcome)));
|
|
self
|
|
}
|
|
}
|
|
|
|
#[async_trait]
|
|
impl LoopDelegate for MockDelegate {
|
|
async fn check_signals(&self) -> LoopSignal {
|
|
let mut sig = self.signal.lock().await;
|
|
std::mem::replace(&mut *sig, LoopSignal::Continue)
|
|
}
|
|
|
|
async fn before_llm_call(
|
|
&self,
|
|
_reason_ctx: &mut ReasoningContext,
|
|
iteration: usize,
|
|
) -> Option<LoopOutcome> {
|
|
let mut guard = self.early_exit.lock().await;
|
|
let should_take = guard
|
|
.as_ref()
|
|
.is_some_and(|(target, _)| *target == iteration);
|
|
if should_take {
|
|
guard.take().map(|(_, o)| o)
|
|
} else {
|
|
None
|
|
}
|
|
}
|
|
|
|
async fn call_llm(
|
|
&self,
|
|
_reasoning: &Reasoning,
|
|
_reason_ctx: &mut ReasoningContext,
|
|
_iteration: usize,
|
|
) -> Result<crate::llm::RespondOutput, crate::error::Error> {
|
|
let mut responses = self.llm_responses.lock().await;
|
|
if responses.is_empty() {
|
|
panic!("MockDelegate: no more LLM responses queued");
|
|
}
|
|
Ok(responses.remove(0))
|
|
}
|
|
|
|
async fn handle_text_response(
|
|
&self,
|
|
text: &str,
|
|
_reason_ctx: &mut ReasoningContext,
|
|
) -> TextAction {
|
|
TextAction::Return(LoopOutcome::Response(text.to_string()))
|
|
}
|
|
|
|
async fn execute_tool_calls(
|
|
&self,
|
|
_tool_calls: Vec<ToolCall>,
|
|
_content: Option<String>,
|
|
reason_ctx: &mut ReasoningContext,
|
|
) -> Result<Option<LoopOutcome>, crate::error::Error> {
|
|
self.tool_exec_count.fetch_add(1, Ordering::SeqCst);
|
|
reason_ctx
|
|
.messages
|
|
.push(ChatMessage::user("tool result stub"));
|
|
let outcome = self.tool_exec_outcome.lock().await.take();
|
|
Ok(outcome)
|
|
}
|
|
|
|
async fn on_tool_intent_nudge(&self, _text: &str, _reason_ctx: &mut ReasoningContext) {
|
|
self.nudge_count.fetch_add(1, Ordering::SeqCst);
|
|
}
|
|
|
|
async fn after_iteration(&self, iteration: usize) {
|
|
self.iterations_seen.lock().await.push(iteration);
|
|
}
|
|
}
|
|
|
|
// --- Tests ---
|
|
|
|
#[tokio::test]
|
|
async fn test_text_response_returns_immediately() {
|
|
let delegate = MockDelegate::new(vec![text_output("Hello, world!")]);
|
|
let reasoning = stub_reasoning();
|
|
let mut ctx = ReasoningContext::new();
|
|
let config = AgenticLoopConfig::default();
|
|
|
|
let outcome = run_agentic_loop(&delegate, &reasoning, &mut ctx, &config)
|
|
.await
|
|
.unwrap();
|
|
|
|
match outcome {
|
|
LoopOutcome::Response(text) => assert_eq!(text, "Hello, world!"),
|
|
_ => panic!("Expected LoopOutcome::Response"),
|
|
}
|
|
// after_iteration is NOT called when handle_text_response returns Return
|
|
// (the loop exits before reaching after_iteration).
|
|
assert!(delegate.iterations_seen.lock().await.is_empty());
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn test_tool_call_then_text_response() {
|
|
let tool_call = ToolCall {
|
|
id: "call_1".to_string(),
|
|
name: "echo".to_string(),
|
|
arguments: serde_json::json!({}),
|
|
};
|
|
let delegate = MockDelegate::new(vec![
|
|
tool_calls_output(vec![tool_call]),
|
|
text_output("Done!"),
|
|
]);
|
|
let reasoning = stub_reasoning();
|
|
let mut ctx = ReasoningContext::new();
|
|
let config = AgenticLoopConfig::default();
|
|
|
|
let outcome = run_agentic_loop(&delegate, &reasoning, &mut ctx, &config)
|
|
.await
|
|
.unwrap();
|
|
|
|
match outcome {
|
|
LoopOutcome::Response(text) => assert_eq!(text, "Done!"),
|
|
_ => panic!("Expected LoopOutcome::Response"),
|
|
}
|
|
assert_eq!(delegate.tool_exec_count.load(Ordering::SeqCst), 1);
|
|
// after_iteration called for iteration 1 (tool call), but not 2
|
|
// (text response exits before after_iteration).
|
|
assert_eq!(*delegate.iterations_seen.lock().await, vec![1]);
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn test_stop_signal_exits_immediately() {
|
|
let delegate =
|
|
MockDelegate::new(vec![text_output("unreachable")]).with_signal(LoopSignal::Stop);
|
|
let reasoning = stub_reasoning();
|
|
let mut ctx = ReasoningContext::new();
|
|
let config = AgenticLoopConfig::default();
|
|
|
|
let outcome = run_agentic_loop(&delegate, &reasoning, &mut ctx, &config)
|
|
.await
|
|
.unwrap();
|
|
|
|
assert!(matches!(outcome, LoopOutcome::Stopped));
|
|
assert!(delegate.iterations_seen.lock().await.is_empty());
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn test_inject_message_adds_user_message() {
|
|
let delegate = MockDelegate::new(vec![text_output("Got it")])
|
|
.with_signal(LoopSignal::InjectMessage("injected prompt".to_string()));
|
|
let reasoning = stub_reasoning();
|
|
let mut ctx = ReasoningContext::new();
|
|
let config = AgenticLoopConfig::default();
|
|
|
|
let outcome = run_agentic_loop(&delegate, &reasoning, &mut ctx, &config)
|
|
.await
|
|
.unwrap();
|
|
|
|
assert!(matches!(outcome, LoopOutcome::Response(_)));
|
|
assert!(
|
|
ctx.messages
|
|
.iter()
|
|
.any(|m| m.role == crate::llm::Role::User && m.content.contains("injected prompt")),
|
|
"Injected message should appear in context"
|
|
);
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn test_max_iterations_reached() {
|
|
struct ContinueDelegate;
|
|
|
|
#[async_trait]
|
|
impl LoopDelegate for ContinueDelegate {
|
|
async fn check_signals(&self) -> LoopSignal {
|
|
LoopSignal::Continue
|
|
}
|
|
async fn before_llm_call(
|
|
&self,
|
|
_: &mut ReasoningContext,
|
|
_: usize,
|
|
) -> Option<LoopOutcome> {
|
|
None
|
|
}
|
|
async fn call_llm(
|
|
&self,
|
|
_: &Reasoning,
|
|
_: &mut ReasoningContext,
|
|
_: usize,
|
|
) -> Result<crate::llm::RespondOutput, crate::error::Error> {
|
|
Ok(text_output("still working"))
|
|
}
|
|
async fn handle_text_response(
|
|
&self,
|
|
_: &str,
|
|
ctx: &mut ReasoningContext,
|
|
) -> TextAction {
|
|
ctx.messages.push(ChatMessage::assistant("still working"));
|
|
TextAction::Continue
|
|
}
|
|
async fn execute_tool_calls(
|
|
&self,
|
|
_: Vec<ToolCall>,
|
|
_: Option<String>,
|
|
_: &mut ReasoningContext,
|
|
) -> Result<Option<LoopOutcome>, crate::error::Error> {
|
|
Ok(None)
|
|
}
|
|
}
|
|
|
|
let delegate = ContinueDelegate;
|
|
let reasoning = stub_reasoning();
|
|
let mut ctx = ReasoningContext::new();
|
|
let config = AgenticLoopConfig {
|
|
max_iterations: 3,
|
|
..Default::default()
|
|
};
|
|
|
|
let outcome = run_agentic_loop(&delegate, &reasoning, &mut ctx, &config)
|
|
.await
|
|
.unwrap();
|
|
|
|
assert!(matches!(outcome, LoopOutcome::MaxIterations));
|
|
let assistant_count = ctx
|
|
.messages
|
|
.iter()
|
|
.filter(|m| m.role == crate::llm::Role::Assistant)
|
|
.count();
|
|
assert_eq!(assistant_count, 3);
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn test_tool_intent_nudge_fires_and_caps() {
|
|
let delegate = MockDelegate::new(vec![
|
|
text_output("Let me search for that file"),
|
|
text_output("Let me search for that file"),
|
|
text_output("Let me search for that file"),
|
|
]);
|
|
let reasoning = stub_reasoning();
|
|
let mut ctx = ReasoningContext::new();
|
|
ctx.available_tools.push(crate::llm::ToolDefinition {
|
|
name: "search".to_string(),
|
|
description: "Search files".to_string(),
|
|
parameters: serde_json::json!({"type": "object"}),
|
|
});
|
|
let config = AgenticLoopConfig {
|
|
max_iterations: 10,
|
|
enable_tool_intent_nudge: true,
|
|
max_tool_intent_nudges: 2,
|
|
};
|
|
|
|
let outcome = run_agentic_loop(&delegate, &reasoning, &mut ctx, &config)
|
|
.await
|
|
.unwrap();
|
|
|
|
assert!(matches!(outcome, LoopOutcome::Response(_)));
|
|
assert_eq!(delegate.nudge_count.load(Ordering::SeqCst), 2);
|
|
let nudge_messages = ctx
|
|
.messages
|
|
.iter()
|
|
.filter(|m| {
|
|
m.role == crate::llm::Role::User
|
|
&& m.content.contains("you did not include any tool calls")
|
|
})
|
|
.count();
|
|
assert_eq!(
|
|
nudge_messages, 2,
|
|
"Should have exactly 2 nudge messages in context"
|
|
);
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn test_before_llm_call_early_exit() {
|
|
let delegate = MockDelegate::new(vec![text_output("unreachable")])
|
|
.with_early_exit(1, LoopOutcome::Stopped);
|
|
let reasoning = stub_reasoning();
|
|
let mut ctx = ReasoningContext::new();
|
|
let config = AgenticLoopConfig::default();
|
|
|
|
let outcome = run_agentic_loop(&delegate, &reasoning, &mut ctx, &config)
|
|
.await
|
|
.unwrap();
|
|
|
|
assert!(matches!(outcome, LoopOutcome::Stopped));
|
|
assert!(delegate.iterations_seen.lock().await.is_empty());
|
|
}
|
|
|
|
#[test]
|
|
fn test_truncate_short_string_unchanged() {
|
|
assert_eq!(truncate_for_preview("hello", 10), "hello");
|
|
}
|
|
|
|
#[test]
|
|
fn test_truncate_long_string_adds_ellipsis() {
|
|
let result = truncate_for_preview("hello world", 5);
|
|
assert_eq!(result, "hello...");
|
|
}
|
|
|
|
#[test]
|
|
fn test_truncate_multibyte_safe() {
|
|
let result = truncate_for_preview("café", 4);
|
|
assert_eq!(result, "caf...");
|
|
}
|
|
}
|