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]>
This commit is contained in:
Henry Park
2026-03-07 09:15:00 +00:00
committed by GitHub
co-authored by Claude Opus 4.6 [email protected] <[email protected]>
parent 424a0366a9
commit 30790439ee
14 changed files with 400 additions and 321 deletions
+17 -21
View File
@@ -96,46 +96,42 @@ mod cleanup_tests {
#[test]
fn cleanup_guard_removes_file() {
let tmp = tempfile::tempdir().unwrap();
let path = tmp.path().join("cleanup_guard_test.txt");
std::fs::write(&path, "test").unwrap();
let path_str = path.to_str().unwrap().to_string();
let path = "/tmp/ironclaw_cleanup_guard_test.txt";
std::fs::write(path, "test").unwrap();
{
let _guard = CleanupGuard::new().file(path_str);
assert!(path.exists());
let _guard = CleanupGuard::new().file(path);
assert!(std::path::Path::new(path).exists());
}
assert!(!path.exists());
assert!(!std::path::Path::new(path).exists());
}
#[test]
fn cleanup_guard_removes_dir() {
let tmp = tempfile::tempdir().unwrap();
let dir = tmp.path().join("cleanup_guard_test_dir");
std::fs::create_dir_all(&dir).unwrap();
std::fs::write(dir.join("file.txt"), "test").unwrap();
let dir_str = dir.to_str().unwrap().to_string();
let dir = "/tmp/ironclaw_cleanup_guard_test_dir";
std::fs::create_dir_all(dir).unwrap();
std::fs::write(format!("{dir}/file.txt"), "test").unwrap();
{
let _guard = CleanupGuard::new().dir(dir_str);
assert!(dir.exists());
let _guard = CleanupGuard::new().dir(dir);
assert!(std::path::Path::new(dir).exists());
}
assert!(!dir.exists());
assert!(!std::path::Path::new(dir).exists());
}
#[test]
fn cleanup_guard_file_does_not_remove_dir() {
let tmp = tempfile::tempdir().unwrap();
let dir = tmp.path().join("cleanup_guard_file_not_dir");
std::fs::create_dir_all(&dir).unwrap();
let dir_str = dir.to_str().unwrap().to_string();
let dir = "/tmp/ironclaw_cleanup_guard_file_not_dir";
std::fs::create_dir_all(dir).unwrap();
{
// Registering a directory path as .file() should not remove it
// (remove_file fails on directories).
let _guard = CleanupGuard::new().file(dir_str);
let _guard = CleanupGuard::new().file(dir);
}
assert!(
dir.exists(),
std::path::Path::new(dir).exists(),
"dir should still exist when registered as file"
);
// Clean up manually.
let _ = std::fs::remove_dir_all(dir);
}
}