mirror of
https://github.com/instructkr/claw-code.git
synced 2026-11-16 00:00:40 +08:00
fix(runtime): keep a failed turn's tool results, and size guards from the real context window
Five defects found by auditing the agent stack, all of which let the harness act confidently on a wrong picture of its own state. 1. A turn that errored discarded the record of tools that had already run. The CLI hands run_turn a CLONE of the session; the turn appends the user message, each assistant reply, and a tool_result per executed tool to that clone alone. The error path dropped it while the side effects of those tools remained on disk, and the recovery path then replayed the same user input against a session showing no sign they had ever run — inviting a second execution of tools that are not idempotent. The clone is now adopted before teardown, and recovery resumes the turn instead of replaying it (new ConversationRuntime::resume_turn). 2. Every sizing guard was computed against the model NAME's context window. With a local server behind a cloud model alias that is 200k against a much smaller backend, which does not make the preflights inaccurate — it makes them unreachable. CLAW_CONTEXT_WINDOW now declares the real window; model_token_limit honours it and returns a limit for unrecognised models instead of None, max_tokens is capped to a quarter of it, and the auto-compaction threshold derives from it (70%) rather than defaulting to a 200k-window constant. 3. Whether to recover by discarding conversation history was decided by substring matching on rendered error prose, including strings that ordinary transport and decode failures also produce — so a dropped connection cost the session its transcript. Classification now happens in the API layer, where the error still has its type and the request is still in hand: typed overflow errors, plus ambiguous transport failures only when the request actually filled the window. The two copy-pasted detection blocks are gone. 4. The top-level agent loop had no iteration ceiling (usize::MAX) while spawned subagents were bounded at 32. It now gets the same bound, overridable with CLAW_MAX_TURN_ITERATIONS. 5. Auto-continue nudges were injected as USER turns, so harness-authored text was persisted, re-sent on every later request, and folded into compaction summaries as something the user had said. They are recorded as system turns now; the wire payload is unchanged. Also replaces the repository's own self-host section, which described a translator and a serving stack no longer in the request path. That file is folded into the system prompt verbatim, so it was briefing the model with an architecture that does not exist. (cherry picked from commit c8834cb4d1a3372eb70241429f641162a420297e)
This commit is contained in:
1 parent
7c38d78759
commit
6a797de094
7 files changed
+478
-93
No files matched your search
@@ -74,6 +74,22 @@ use tools::{
|
||||
|
||||
const DEFAULT_MODEL: &str = "anthropic/claude-opus-4-7";
|
||||
|
||||
/// Ceiling on model/tool round trips within one turn.
|
||||
///
|
||||
/// `ConversationRuntime` defaults to `usize::MAX`, which for the interactive and `-p` paths means
|
||||
/// no ceiling at all: a model that keeps re-reading the same file loops until the user kills it.
|
||||
/// Spawned subagents have always been bounded; this gives the top-level loop the same treatment.
|
||||
const DEFAULT_MAX_TURN_ITERATIONS: usize = 32;
|
||||
const MAX_TURN_ITERATIONS_ENV_VAR: &str = "CLAW_MAX_TURN_ITERATIONS";
|
||||
|
||||
fn max_turn_iterations_from_env() -> usize {
|
||||
std::env::var(MAX_TURN_ITERATIONS_ENV_VAR)
|
||||
.ok()
|
||||
.and_then(|raw| raw.trim().parse::<usize>().ok())
|
||||
.filter(|iterations| *iterations > 0)
|
||||
.unwrap_or(DEFAULT_MAX_TURN_ITERATIONS)
|
||||
}
|
||||
|
||||
/// #148: Model provenance for `claw status` JSON/text output. Records where
|
||||
/// the resolved model string came from so claws don't have to re-read argv
|
||||
/// to audit whether their `--model` flag was honored vs falling back to env
|
||||
@@ -7854,6 +7870,15 @@ impl LiveCli {
|
||||
Ok(())
|
||||
}
|
||||
Err(error) => {
|
||||
// Salvage the transcript before the failed runtime is torn down.
|
||||
//
|
||||
// `prepare_turn_runtime` handed `run_turn` a CLONE of the session, and the turn
|
||||
// mutated only that clone — appending the user message, each assistant reply, and
|
||||
// a tool_result for every tool that actually executed. Dropping the clone throws
|
||||
// all of that away while the side effects of those tools remain on disk, so the
|
||||
// retry below would re-issue them against a session that shows no sign they ever
|
||||
// ran. Adopting the clone keeps the record and the reality in step.
|
||||
*self.runtime.session_mut() = runtime.session().clone();
|
||||
runtime.shutdown_plugins()?;
|
||||
// Same ownership rule as the success arm: `fail` clears the current
|
||||
// line, so it may only do so when the emitter stopped on a line
|
||||
@@ -7880,37 +7905,17 @@ impl LiveCli {
|
||||
// This eliminates the need for users to manually run /compact when they
|
||||
// hit context limits - the recovery happens automatically.
|
||||
//
|
||||
// Detection: We look for "context_window" or "Context window" in the error
|
||||
// message, which covers error types like:
|
||||
// - "context_window_blocked"
|
||||
// - "Context window blocked"
|
||||
// - "This model's maximum context length is X tokens..."
|
||||
// Detection: the API layer classified this when it still had the typed error and
|
||||
// the outgoing request in hand — see `AnthropicRuntimeClient::runtime_error_from_api`.
|
||||
// It is deliberately NOT re-derived from the rendered message here: compaction
|
||||
// discards conversation history, and the substrings that used to gate it
|
||||
// ("error decoding response body", "Failed to parse input at pos") are emitted by
|
||||
// ordinary transport failures as well as by overflow, so a dropped connection cost
|
||||
// the session its transcript.
|
||||
// ============================================================================
|
||||
|
||||
let error_str = error.to_string();
|
||||
// Detect context window overflow. Some providers (e.g. OpenAI-compat backends)
|
||||
// return 400 with "no parseable body" instead of a proper context_length_exceeded
|
||||
// error when the request is too large to even parse — treat that as context overflow too.
|
||||
// Also detect model-specific context error markers (e.g. llama.cpp returns
|
||||
// "Context size has been exceeded." / "exceed_context_size_error" / "exceeds the available context size").
|
||||
let is_context_window = error_str.contains("context_window")
|
||||
|| error_str.contains("Context window")
|
||||
|| error_str.contains("no parseable body")
|
||||
|| error_str.contains("exceed_context_size")
|
||||
|| error_str.contains("exceeds the available context size")
|
||||
|| error_str
|
||||
.to_ascii_lowercase()
|
||||
.contains("context size has been exceeded");
|
||||
|
||||
// Also treat "assistant stream produced no content" and reqwest decode failures
|
||||
// as recoverable errors that may benefit from auto-compaction. Some backends (e.g.
|
||||
// llama.cpp) return a non-SSE HTTP 500 body when context overflows, causing
|
||||
// reqwest to fail with "error decoding response body" — treat that as context overflow too.
|
||||
let is_no_content = error_str.contains("assistant stream produced no content")
|
||||
|| error_str.contains("Failed to parse input at pos")
|
||||
|| error_str.contains("error decoding response body");
|
||||
|
||||
if is_context_window || is_no_content {
|
||||
if error.is_context_window_failure() {
|
||||
// If the error tells us the server's actual context window, adapt our
|
||||
// auto-compaction threshold so future auto-compact-trigger checks are accurate.
|
||||
if let Some(window) = extract_context_window_tokens_from_error(&error_str) {
|
||||
@@ -7977,7 +7982,11 @@ impl LiveCli {
|
||||
drop(hook_abort_monitor);
|
||||
|
||||
let mut rp = CliPermissionPrompter::new(self.permission_mode);
|
||||
match new_runtime.run_turn(input, Some(&mut rp)) {
|
||||
// Resume, do not replay. The salvaged session already carries this turn's
|
||||
// user message and every tool result that completed before the failure;
|
||||
// re-running `run_turn(input, ..)` would duplicate the prompt and invite a
|
||||
// second execution of tools that are not idempotent.
|
||||
match new_runtime.resume_turn(Some(&mut rp)) {
|
||||
Ok(summary) => {
|
||||
self.replace_runtime(new_runtime)?;
|
||||
if !terminal_tail.at_line_start() {
|
||||
@@ -8004,20 +8013,7 @@ impl LiveCli {
|
||||
}
|
||||
Err(retry_error) => {
|
||||
let retry_str = retry_error.to_string();
|
||||
let still_context_window = retry_str.contains("context_window")
|
||||
|| retry_str.contains("Context window")
|
||||
|| retry_str.contains("no parseable body")
|
||||
|| retry_str.contains("exceed_context_size")
|
||||
|| retry_str.contains("exceeds the available context size")
|
||||
|| retry_str
|
||||
.to_ascii_lowercase()
|
||||
.contains("context size has been exceeded");
|
||||
let still_no_content = retry_str
|
||||
.contains("assistant stream produced no content")
|
||||
|| retry_str.contains("Failed to parse input at pos")
|
||||
|| retry_str.contains("error decoding response body");
|
||||
|
||||
if (still_context_window || still_no_content)
|
||||
if retry_error.is_context_window_failure()
|
||||
&& round + 1 < max_compact_rounds
|
||||
{
|
||||
// If the retry error reveals the context window, adapt threshold.
|
||||
@@ -12613,7 +12609,8 @@ fn build_runtime_with_plugin_state(
|
||||
policy,
|
||||
system_prompt,
|
||||
&feature_config,
|
||||
);
|
||||
)
|
||||
.with_max_iterations(max_turn_iterations_from_env());
|
||||
if emit_output {
|
||||
runtime = runtime.with_hook_progress_reporter(Box::new(CliHookProgressReporter));
|
||||
}
|
||||
@@ -12934,7 +12931,49 @@ impl ApiClient for AnthropicRuntimeClient {
|
||||
}
|
||||
}
|
||||
|
||||
/// How full a request must be, as a percentage of the declared context window, before an
|
||||
/// unreadable failure is taken to mean the window overflowed.
|
||||
const AMBIGUOUS_FAILURE_WINDOW_PERCENT: u64 = 85;
|
||||
|
||||
/// True when a request was close enough to the window that an otherwise unreadable failure is
|
||||
/// most likely an overflow.
|
||||
///
|
||||
/// This is the piece that lets transport errors be classified without matching on their prose.
|
||||
/// A backend can answer an oversized request with a non-SSE 500 that reqwest reports as
|
||||
/// "error decoding response body"; the same string also appears when the connection simply
|
||||
/// dropped. What separates them is not the text, it is whether the request we just sent had any
|
||||
/// room left. Requires a declared window — with none, every request looks small and this
|
||||
/// correctly returns false.
|
||||
fn request_fills_context_window(request: &MessageRequest) -> bool {
|
||||
let Some(limit) = api::model_token_limit(&request.model) else {
|
||||
return false;
|
||||
};
|
||||
let estimated =
|
||||
api::estimate_message_request_input_tokens(request).saturating_add(request.max_tokens);
|
||||
u64::from(estimated) * 100
|
||||
>= u64::from(limit.context_window_tokens) * AMBIGUOUS_FAILURE_WINDOW_PERCENT
|
||||
}
|
||||
|
||||
impl AnthropicRuntimeClient {
|
||||
/// Converts an API failure into a runtime error, carrying the overflow verdict with it.
|
||||
///
|
||||
/// The verdict is made here, where the error still has its type and the request is still in
|
||||
/// hand, rather than reconstructed downstream from the rendered message.
|
||||
fn runtime_error_from_api(
|
||||
&self,
|
||||
error: &api::ApiError,
|
||||
request: &MessageRequest,
|
||||
) -> RuntimeError {
|
||||
let message = format_user_visible_api_error(&self.session_id, error);
|
||||
if error.is_context_window_failure()
|
||||
|| (error.is_ambiguous_transport_failure() && request_fills_context_window(request))
|
||||
{
|
||||
RuntimeError::context_window(message)
|
||||
} else {
|
||||
RuntimeError::new(message)
|
||||
}
|
||||
}
|
||||
|
||||
/// Consume a single streaming response, optionally applying a stall
|
||||
/// timeout on the first event for post-tool continuations.
|
||||
#[allow(clippy::too_many_lines)]
|
||||
@@ -12947,9 +12986,7 @@ impl AnthropicRuntimeClient {
|
||||
.client
|
||||
.stream_message(message_request)
|
||||
.await
|
||||
.map_err(|error| {
|
||||
RuntimeError::new(format_user_visible_api_error(&self.session_id, &error))
|
||||
})?;
|
||||
.map_err(|error| self.runtime_error_from_api(&error, message_request))?;
|
||||
let mut stdout = io::stdout();
|
||||
let mut sink = io::sink();
|
||||
let mut tracked = TailTrackingWriter::new(&mut stdout, self.terminal_tail.clone());
|
||||
@@ -12971,9 +13008,8 @@ impl AnthropicRuntimeClient {
|
||||
loop {
|
||||
let next = if apply_stall_timeout && !received_any_event {
|
||||
match tokio::time::timeout(POST_TOOL_STALL_TIMEOUT, stream.next_event()).await {
|
||||
Ok(inner) => inner.map_err(|error| {
|
||||
RuntimeError::new(format_user_visible_api_error(&self.session_id, &error))
|
||||
})?,
|
||||
Ok(inner) => inner
|
||||
.map_err(|error| self.runtime_error_from_api(&error, message_request))?,
|
||||
Err(_elapsed) => {
|
||||
return Err(RuntimeError::new(
|
||||
"post-tool stall: model did not respond within timeout",
|
||||
@@ -12981,9 +13017,10 @@ impl AnthropicRuntimeClient {
|
||||
}
|
||||
}
|
||||
} else {
|
||||
stream.next_event().await.map_err(|error| {
|
||||
RuntimeError::new(format_user_visible_api_error(&self.session_id, &error))
|
||||
})?
|
||||
stream
|
||||
.next_event()
|
||||
.await
|
||||
.map_err(|error| self.runtime_error_from_api(&error, message_request))?
|
||||
};
|
||||
|
||||
let Some(event) = next else {
|
||||
@@ -13123,9 +13160,7 @@ impl AnthropicRuntimeClient {
|
||||
..message_request.clone()
|
||||
})
|
||||
.await
|
||||
.map_err(|error| {
|
||||
RuntimeError::new(format_user_visible_api_error(&self.session_id, &error))
|
||||
})?;
|
||||
.map_err(|error| self.runtime_error_from_api(&error, message_request))?;
|
||||
let mut events = response_to_events(response, out)?;
|
||||
push_prompt_cache_record(&self.client, &mut events);
|
||||
Ok(events)
|
||||
|
||||
Reference in new issue
Block a user