Files
optimclaw/tests/e2e_spot_checks.rs
T
30790439ee perf: build system prompt once per turn, skip tools on force-text (#583)
* perf: build system prompt once per turn, skip tools on force-text, fix nudge role (#565)

Three fixes to agentic loop prompt handling:

1. Build system prompt once per turn instead of every tool iteration.
   `build_system_prompt_with_tools` is now pub; callers pass the result
   via `ReasoningContext::system_prompt` to avoid rebuilding ~1,500 tokens
   per iteration.

2. Skip `## Available Tools` section when `force_text = true`. The
   dispatcher passes a no-tools prompt variant on the final iteration,
   saving ~460 tokens and removing misleading instructions.

3. Change nudge message from `Role::System` to `Role::User`. A second
   system message mid-conversation is unsupported by most providers.

Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>

* fix: revert nudge role change to keep ChatMessage::system

Copilot review correctly identified that using Role::User for the nudge
breaks compact_messages_for_retry, which uses rposition for Role::User
to find the last real user message. Role::Assistant would cause
back-to-back assistant messages. Since no production issues were reported
with the original system role, revert to ChatMessage::system.

[skip-regression-check]

Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>

* fix: address PR review — omit tool guidance when tools empty, rename shadowed var

- Conditionalize "Call tools…" guidelines and "## Tool Call Style" section
  in the system prompt so they are only included when tools are non-empty.
  Previously the force-text (no-tools) prompt still contained misleading
  tool-calling instructions. (Copilot review comment)

- Rename `system_prompt` → `cached_prompt` in dispatcher to avoid shadowing
  the earlier workspace identity `system_prompt` variable. (Copilot review)

- Add regression tests: `test_system_prompt_with_tools_contains_tool_guidance`
  and extended assertions in `test_system_prompt_without_tools_omits_tools_section`.

Co-Authored-By: Claude Opus 4.6 <[email protected]>

---------

Co-authored-by: Claude Opus 4.6 (1M context) <[email protected]>
Co-authored-by: [email protected] <[email protected]>
2026-03-07 09:15:00 +00:00

192 lines
6.7 KiB
Rust

//! E2E spot-check tests adapted from nearai/benchmarks SpotSuite tasks.jsonl.
//!
//! Each test replays an LLM trace through the real agent loop and validates
//! the result using declarative `expects` from the fixture JSON plus any
//! additional assertions that can't be expressed declaratively.
#[cfg(feature = "libsql")]
mod support;
#[cfg(feature = "libsql")]
mod spot_tests {
use std::time::Duration;
use crate::support::cleanup::CleanupGuard;
use crate::support::test_rig::TestRigBuilder;
use crate::support::trace_llm::LlmTrace;
const FIXTURES: &str = concat!(
env!("CARGO_MANIFEST_DIR"),
"/tests/fixtures/llm_traces/spot"
);
const TIMEOUT: Duration = Duration::from_secs(15);
// -----------------------------------------------------------------------
// Smoke tests -- no tools expected
// -----------------------------------------------------------------------
#[tokio::test]
async fn spot_smoke_greeting() {
let trace = LlmTrace::from_file(format!("{FIXTURES}/smoke_greeting.json")).unwrap();
let rig = TestRigBuilder::new()
.with_trace(trace.clone())
.build()
.await;
rig.send_message("Hello! Introduce yourself briefly.").await;
let responses = rig.wait_for_responses(1, TIMEOUT).await;
rig.verify_trace_expects(&trace, &responses);
rig.shutdown();
}
#[tokio::test]
async fn spot_smoke_math() {
let trace = LlmTrace::from_file(format!("{FIXTURES}/smoke_math.json")).unwrap();
let rig = TestRigBuilder::new()
.with_trace(trace.clone())
.build()
.await;
rig.send_message("What is 47 * 23? Reply with just the number.")
.await;
let responses = rig.wait_for_responses(1, TIMEOUT).await;
rig.verify_trace_expects(&trace, &responses);
rig.shutdown();
}
// -----------------------------------------------------------------------
// Tool tests -- verify correct tool selection
// -----------------------------------------------------------------------
#[tokio::test]
async fn spot_tool_echo() {
let trace = LlmTrace::from_file(format!("{FIXTURES}/tool_echo.json")).unwrap();
let rig = TestRigBuilder::new()
.with_trace(trace.clone())
.build()
.await;
rig.send_message("Use the echo tool to repeat the message: 'Spot check passed'")
.await;
let responses = rig.wait_for_responses(1, TIMEOUT).await;
rig.verify_trace_expects(&trace, &responses);
rig.shutdown();
}
#[tokio::test]
async fn spot_tool_json() {
let trace = LlmTrace::from_file(format!("{FIXTURES}/tool_json.json")).unwrap();
let rig = TestRigBuilder::new()
.with_trace(trace.clone())
.build()
.await;
rig.send_message("Parse this json for me: {\"key\": \"value\"}")
.await;
let responses = rig.wait_for_responses(1, TIMEOUT).await;
rig.verify_trace_expects(&trace, &responses);
rig.shutdown();
}
// -----------------------------------------------------------------------
// Chain tests -- multi-tool sequences
// -----------------------------------------------------------------------
#[tokio::test]
async fn spot_chain_write_read() {
let _cleanup = CleanupGuard::new().file("/tmp/ironclaw_spot_test.txt");
let _ = std::fs::remove_file("/tmp/ironclaw_spot_test.txt");
let trace = LlmTrace::from_file(format!("{FIXTURES}/chain_write_read.json")).unwrap();
let rig = TestRigBuilder::new()
.with_trace(trace.clone())
.build()
.await;
rig.send_message(
"Write the text 'ironclaw spot check' to /tmp/ironclaw_spot_test.txt \
using the write_file tool, then read it back using read_file.",
)
.await;
let responses = rig.wait_for_responses(1, TIMEOUT).await;
rig.verify_trace_expects(&trace, &responses);
// Extra: verify file on disk (can't express in expects).
let content =
std::fs::read_to_string("/tmp/ironclaw_spot_test.txt").expect("file should exist");
assert_eq!(content, "ironclaw spot check");
rig.shutdown();
}
// -----------------------------------------------------------------------
// Robustness tests -- correct behavior under constraints
// -----------------------------------------------------------------------
#[tokio::test]
async fn spot_robust_no_tool() {
let trace = LlmTrace::from_file(format!("{FIXTURES}/robust_no_tool.json")).unwrap();
let rig = TestRigBuilder::new()
.with_trace(trace.clone())
.build()
.await;
rig.send_message("What is the capital of France? Answer directly without using any tools.")
.await;
let responses = rig.wait_for_responses(1, TIMEOUT).await;
rig.verify_trace_expects(&trace, &responses);
rig.shutdown();
}
#[tokio::test]
async fn spot_robust_correct_tool() {
let trace = LlmTrace::from_file(format!("{FIXTURES}/robust_correct_tool.json")).unwrap();
let rig = TestRigBuilder::new()
.with_trace(trace.clone())
.build()
.await;
rig.send_message("Please echo the word 'deterministic output'")
.await;
let responses = rig.wait_for_responses(1, TIMEOUT).await;
rig.verify_trace_expects(&trace, &responses);
rig.shutdown();
}
// -----------------------------------------------------------------------
// Memory tests -- save and recall via file tools
// -----------------------------------------------------------------------
#[tokio::test]
async fn spot_memory_save_recall() {
let _cleanup = CleanupGuard::new().file("/tmp/bench-meeting.md");
let _ = std::fs::remove_file("/tmp/bench-meeting.md");
let trace = LlmTrace::from_file(format!("{FIXTURES}/memory_save_recall.json")).unwrap();
let rig = TestRigBuilder::new()
.with_trace(trace.clone())
.build()
.await;
rig.send_message(
"Save these meeting notes to /tmp/bench-meeting.md:\n\
Meeting: Project Phoenix sync\nAttendees: Alice, Bob, Carol\n\
Decisions:\n- Launch date: April 15th\n- Budget: $50k approved\n\
- Bob owns frontend, Carol owns backend\n\
Then read it back and tell me who owns the frontend and what the launch date is.",
)
.await;
let responses = rig.wait_for_responses(1, TIMEOUT).await;
rig.verify_trace_expects(&trace, &responses);
rig.shutdown();
}
}