A self-improving RLM agent for coding workflows and long-running autonomous tasks.
Rust
[RSI, bug] ACP: classify transport failures in prompt error responses (#3679)
Seth Karten07ae06883e6c
diff --git a/crates/pa-cli/tests/acp_mode_e2e.rs b/crates/pa-cli/tests/acp_mode_e2e.rsindex a08dadff65..7bc9a6109e 100644--- a/crates/pa-cli/tests/acp_mode_e2e.rs+++ b/crates/pa-cli/tests/acp_mode_e2e.rs@@ -1375,6 +1375,14 @@ fn acp_daemon_attached_failed_turn_publishes_only_the_error_boundary() {.contains("Prompt cannot be empty"),"the worker's failure is the error: {prompt_response}");+ assert_eq!(+ prompt_response["error"]["data"]["kind"], "other",+ "the harness failure is classified as the generic kind: {prompt_response}"+ );+ assert_eq!(+ prompt_response["error"]["data"]["retryable"], false,+ "a harness failure is never boundary-retryable: {prompt_response}"+ );let mut boundaries = 0;for update in &updates {let body = &update["params"]["update"];@@ -1398,6 +1406,119 @@ fn acp_daemon_attached_failed_turn_publishes_only_the_error_boundary() {);}+#[test]+fn acp_failed_prompt_error_data_classifies_the_transport_failure() {+ // A provider connection failure fails the turn: the ACP error data+ // carries the nano-rlm #200 contract — `kind: "model_transport"` (the+ // one retryable class), the fine #2117 `category` alongside — so a+ // host separates retryable model transport from harness bugs without+ // parsing the details text. `repeatLastResponse` keeps every+ // auto-retry attempt failing with the same connection-reset+ // disclosure, so the settled turn error is the scripted transport+ // failure.+ let mut client = AcpChild::spawn(+ &["--mode", "acp", "--no-session"],+ &json!({+ "engine": "faux",+ "repeatLastResponse": true,+ "responses": [+ { "stopReason": "error", "errorMessage": "ECONNRESET: model connection reset mid-stream" }+ ]+ }),+ );+ let init = client.request("initialize", &initialize_params());+ let _ = client.wait_response(init, TIMEOUT);+ let new = client.request("session/new", &json!({ "mcpServers": [] }));+ let (new_response, _) = client.wait_response(new, TIMEOUT);+ let session_id = new_response["result"]["sessionId"]+ .as_str()+ .unwrap()+ .to_string();+ let prompt = client.request(+ "session/prompt",+ &json!({ "sessionId": session_id, "prompt": [{ "type": "text", "text": "a question" }] }),+ );+ let (prompt_response, _) = client.wait_response(prompt, TIMEOUT);+ assert_eq!(+ prompt_response["error"]["code"], -32603,+ "the failed turn errors the request: {prompt_response}"+ );+ assert_eq!(+ prompt_response["error"]["data"]["kind"], "model_transport",+ "the connection reset reports the #200 model_transport kind: {prompt_response}"+ );+ assert_eq!(+ prompt_response["error"]["data"]["category"], "network",+ "the fine #2117 category rides separately: {prompt_response}"+ );+ assert_eq!(+ prompt_response["error"]["data"]["retryable"], true,+ "the exhausted connection family is the one retryable class: {prompt_response}"+ );+ assert!(+ prompt_response["error"]["data"]["details"]+ .as_str()+ .unwrap_or_default()+ .contains("ECONNRESET"),+ "the raw failure still rides the details text: {prompt_response}"+ );+}++#[test]+fn acp_failed_prompt_error_data_marks_provider_failures_not_retryable() {+ // The other half of the #200 contract: a 401 classifies as+ // `authentication` and is never marked retryable — the boundary's+ // `retryable` covers only the model connection family, not the+ // auto-retry policy's verdicts (a host retry is a fresh prompt, not+ // an idempotent replay).+ let mut client = AcpChild::spawn(+ &["--mode", "acp", "--no-session"],+ &json!({+ "engine": "faux",+ "repeatLastResponse": true,+ "responses": [+ { "stopReason": "error", "errorMessage": "401 unauthorized" }+ ]+ }),+ );+ let init = client.request("initialize", &initialize_params());+ let _ = client.wait_response(init, TIMEOUT);+ let new = client.request("session/new", &json!({ "mcpServers": [] }));+ let (new_response, _) = client.wait_response(new, TIMEOUT);+ let session_id = new_response["result"]["sessionId"]+ .as_str()+ .unwrap()+ .to_string();+ let prompt = client.request(+ "session/prompt",+ &json!({ "sessionId": session_id, "prompt": [{ "type": "text", "text": "a question" }] }),+ );+ let (prompt_response, _) = client.wait_response(prompt, TIMEOUT);+ assert_eq!(+ prompt_response["error"]["code"], -32603,+ "the failed turn errors the request: {prompt_response}"+ );+ assert_eq!(+ prompt_response["error"]["data"]["kind"], "authentication",+ "the 401 carries its provider category as the kind: {prompt_response}"+ );+ assert_eq!(+ prompt_response["error"]["data"]["category"], "authentication",+ "the fine #2117 category matches the kind off the transport family: {prompt_response}"+ );+ assert_eq!(+ prompt_response["error"]["data"]["retryable"], false,+ "an authentication failure is never boundary-retryable: {prompt_response}"+ );+ assert!(+ prompt_response["error"]["data"]["details"]+ .as_str()+ .unwrap_or_default()+ .contains("401"),+ "the raw failure still rides the details text: {prompt_response}"+ );+}+#[cfg(unix)]#[test]fn acp_daemon_attached_supervisor_loss_fails_the_prompt() {@@ -1453,7 +1574,17 @@ fn acp_daemon_attached_supervisor_loss_fails_the_prompt() {json!({"code": -32603,"message": "Internal error",- "data": { "details": "the daemon connection closed mid-request" }+ "data": {+ "details": "the daemon connection closed mid-request",+ // The lost supervisor link is classified like every other+ // failed-prompt arm: typed data a host can distinguish+ // from an untyped error, and never retryable — the turn's+ // outcome is unknown, so a host retry may re-run+ // side-effecting tools.+ "kind": "other",+ "category": "other",+ "retryable": false,+ }}),"the in-flight prompt fails fast on supervisor loss: {prompt_response}");diff --git a/crates/pa-core/src/session_engine/error_classify.rs b/crates/pa-core/src/session_engine/error_classify.rsindex a2e5c9df7a..633b39503e 100644--- a/crates/pa-core/src/session_engine/error_classify.rs+++ b/crates/pa-core/src/session_engine/error_classify.rs@@ -71,6 +71,30 @@ pub const ERROR_DIAGNOSTICS: &[(&str, &str)] = &[),];+/// The fixed provider transport disclosures pa-ai emits when the turn's+/// auto-retries exhaust (`ProviderConnectionError::message` and the+/// AWS h2 failure texts, `crates/pa-ai/src/utils_inner/stream_failure.rs`):+/// a recognized disclosure prefix is typed evidence, unlike a bare+/// keyword. Prefix-matched because some families append a cause+/// (`Request timed out: <cause>`) or the SDK's wrapper+/// (`Unexpected HTTP client error: TypeError: <raw>`).+const TRANSPORT_DISCLOSURES: &[(&str, &str)] = &[+ ("Connection error.", "network_error"),+ ("Request timed out", "timeout"),+ (+ "Unable to connect. Is the computer able to access the url?",+ "network_error",+ ),+ (+ "Unexpected HTTP client error: TypeError: Unable to connect",+ "network_error",+ ),+ ("Protocol error", "network_error"),+ ("Stream closed with error code", "network_error"),+ ("Session closed with error code", "network_error"),+ ("The pending stream has been canceled", "network_error"),+];+/// The reviewed fixed-string error messages (#2117/// `TELEMETRY_SAFE_ERROR_MESSAGES`): an exact match may ride as `error_message`.const REVIEWED_MESSAGES: &[(&str, &str)] = &[@@ -212,6 +236,24 @@ pub struct ErrorClassification {pub retryable: bool,}+impl ErrorClassification {+ /// The nano-rlm #200 ACP-boundary view: only the exhausted model+ /// connection family is `model_transport` (the one class a host may+ /// re-prompt on). HTTP status errors keep their provider categories —+ /// a host retry is a fresh prompt, not an idempotent replay, so+ /// `retryable` verdicts from the auto-retry policy (rate limits,+ /// quota, balance, malformed responses) do not transfer to the+ /// boundary.+ #[must_use]+ pub fn is_model_transport(&self) -> bool {+ // An HTTP-status classification (a 408 reads as `timeout`) is a+ // provider answer over a live connection, not a model-connection+ // failure: the status verdicts keep their provider categories.+ matches!(self.subtype, "network_error" | "stream_drop" | "timeout")+ && self.http_status.is_none()+ }+}+/// Classify one failed call's error message. The message itself never/// uploads: only the fixed tuples, the diagnostic, and a reviewed string.#[must_use]@@ -267,7 +309,26 @@ pub fn classify_error_message(message: &str) -> ErrorClassification {retryable: subtype_retryable(subtype),};}- // 3. A reviewed fixed-string message (exact match, the only text that+ // 3. A fixed provider transport disclosure (prefix match): the+ // exhausted-connection family pa-ai itself discloses, so the most+ // common real connection failures classify instead of reading as+ // `unknown`.+ if let Some((_, subtype)) = TRANSPORT_DISCLOSURES+ .iter()+ .find(|(disclosure, _)| message.starts_with(disclosure))+ {+ return ErrorClassification {+ diagnostic: diagnostic(subtype),+ safe_message: None,+ category: subtype_category(subtype),+ subtype,+ code: None,+ http_status: None,+ classification_source: "transport_disclosure",+ retryable: subtype_retryable(subtype),+ };+ }+ // 4. A reviewed fixed-string message (exact match, the only text that// may upload).if let Some((subtype, reviewed)) = REVIEWED_MESSAGES.iter()@@ -284,7 +345,7 @@ pub fn classify_error_message(message: &str) -> ErrorClassification {retryable: subtype_retryable(subtype),};}- // 4. Unmatched: the generic diagnostic; the raw text never uploads.+ // 5. Unmatched: the generic diagnostic; the raw text never uploads.ErrorClassification {diagnostic: diagnostic("unknown"),safe_message: None,@@ -445,6 +506,60 @@ mod tests {assert_eq!(billing.http_status, Some(402));}+ #[test]+ fn transport_disclosures_classify_the_exhausted_connection_family() {+ // The fixed pa-ai disclosures (`ProviderConnectionError::message`):+ // the most common real connection failures, all typed transport.+ let cases = [+ ("Connection error.", "network_error"),+ ("Request timed out.", "timeout"),+ ("Request timed out: fetch timed out", "timeout"),+ ("Unable to connect. Is the computer able to access the url?", "network_error"),+ (+ "Unexpected HTTP client error: TypeError: Unable to connect. Is the computer able to access the url?",+ "network_error",+ ),+ ("Protocol error", "network_error"),+ ("Stream closed with error code 0x2", "network_error"),+ ("The pending stream has been canceled", "network_error"),+ ];+ for (message, subtype) in cases {+ let classification = classify(message);+ assert_eq!(classification.subtype, subtype, "{message}");+ assert_eq!(classification.classification_source, "transport_disclosure");+ assert!(classification.is_model_transport(), "{message}");+ }+ // A disclosure embedded mid-message is not evidence: only the+ // provider layer's own prefix form is a typed disclosure.+ let embedded = classify("tool output: Request timed out mid-run");+ assert_eq!(embedded.subtype, "unknown");+ assert!(!embedded.is_model_transport());+ }++ #[test]+ fn model_transport_covers_only_the_connection_family() {+ for message in ["ECONNRESET mid-stream", "Provider dropped the response stream (stream_drop): the stream ended inside a thinking block before the stop signal"] {+ assert!(classify(message).is_model_transport(), "{message}");+ }+ // HTTP status errors keep their provider categories (#200: they+ // retain the existing behavior, so none is boundary-retryable) —+ // including the 408, whose `timeout` subtype is a provider+ // rejection over a live connection, not an exhausted connect.+ for message in [+ "API Error: 429 rate limit exceeded",+ "401 unauthorized",+ "402 insufficient credits",+ "500 Internal Server Error",+ "408 Request Timeout",+ "context_length_exceeded",+ ] {+ let classification = classify(message);+ assert!(!classification.is_model_transport(), "{message}");+ }+ // The SDK timeout disclosure (no status) is the transport family.+ assert!(classify("Request timed out.").is_model_transport());+ }+#[test]fn every_subtype_has_a_diagnostic() {for (subtype, diagnostic) in ERROR_DIAGNOSTICS {diff --git a/crates/pa-daemon/src/acp/daemon.rs b/crates/pa-daemon/src/acp/daemon.rsindex f64bc450bd..107fdf0d00 100644--- a/crates/pa-daemon/src/acp/daemon.rs+++ b/crates/pa-daemon/src/acp/daemon.rs@@ -1480,7 +1480,11 @@ async fn prompt_turn(Err(error) => {publish_error_boundary(&producer, turn_id).await;producer.finish_prompt(turn_id).await;- return super::internal_error(&id, &error.to_string());+ // The lost supervisor link (route timeout, worker loss) is+ // classified like every other failed-prompt arm: typed data,+ // never retryable at the boundary — the turn's outcome is+ // unknown, so a host retry may re-run side-effecting tools.+ return super::turn_failure_error(&id, &error.to_string(), &error.to_string());}};// TS `abort.signal.aborted` after `promptAndWait`: every turn frame@@ -1507,7 +1511,14 @@ async fn prompt_turn(eprintln!("pa-daemon: acp turn failed: {failure}");publish_error_boundary(&producer, turn_id).await;producer.finish_prompt(turn_id).await;- return super::internal_error(&id, &format!("prime-agent turn failed: {failure}"));+ // The worker's failure text classifies into the #200 host+ // contract (the raw text still never rides past `details`), so+ // the reply data is machine-readable at the transport boundary.+ return super::turn_failure_error(+ &id,+ &failure,+ &format!("prime-agent turn failed: {failure}"),+ );}// The autonomous accounting for the completion envelope: the daemon's// headless-completion status (TS `waitForHeadlessCompletion`), fetched@@ -1525,13 +1536,14 @@ async fn prompt_turn(}// The completion observation's roster read (TS `getRlmChildSnapshots`// throws): a failed read errors the prompt before the boundary, never- // publishes a wrong count.+ // publishes a wrong count — with the same typed data as every other+ // failed-prompt arm.let children = match fetch_rlm_children(link, &hosted_daemon_session_id).await {Ok(children) => children,Err(error) => {publish_error_boundary(&producer, turn_id).await;producer.finish_prompt(turn_id).await;- return super::internal_error(&id, &error.to_string());+ return super::turn_failure_error(&id, &error.to_string(), &error.to_string());}};if turn_cancelled(&*state.lock().await, admission_id) {@@ -1581,8 +1593,9 @@ async fn prompt_turn(Ok(status) => status,Err(error) => {producer.finish_prompt(turn_id).await;- return super::internal_error(+ return super::turn_failure_error(&id,+ &error.to_string(),&format!("ACP lifecycle reconciliation failed: {error}"),);}@@ -1595,8 +1608,9 @@ async fn prompt_turn(Ok(children) => children,Err(error) => {producer.finish_prompt(turn_id).await;- return super::internal_error(+ return super::turn_failure_error(&id,+ &error.to_string(),&format!("ACP lifecycle reconciliation failed: {error}"),);}@@ -1628,7 +1642,13 @@ async fn prompt_turn().await;if !published || !completion_published || !terminal_published {- return super::internal_error(&id, "Failed to publish ACP completion");+ // The typed shape even for a publish failure: a host can tell+ // "classified, not retryable" from an untyped error here too.+ return super::turn_failure_error(+ &id,+ "Failed to publish ACP completion",+ "Failed to publish ACP completion",+ );}break status;};diff --git a/crates/pa-daemon/src/acp/mod.rs b/crates/pa-daemon/src/acp/mod.rsindex db93a84cda..b76bd0a80f 100644--- a/crates/pa-daemon/src/acp/mod.rs+++ b/crates/pa-daemon/src/acp/mod.rs@@ -23,6 +23,46 @@ fn internal_error(id: &Value, details: &str) -> Value {)}+/// The failed-prompt reply: the nano-rlm #200 `kind`/`retryable` contract.+/// `kind` is `"model_transport"` for the exhausted model connection family+/// (`network_error`/`stream_drop`/`timeout` — the one class a host may+/// re-prompt on) or a context overflow (pa-ai's provider pattern table);+/// every other failure carries the #2117 classifier's category. The fine+/// #2117 category rides separately in `category`. `retryable` is true only+/// for `model_transport`: the harness auto-retry is already exhausted by+/// the time this reply exists, and a host retry is a fresh prompt, not an+/// idempotent replay — `session/prompt` persists the user row and+/// side-effecting tools may already have run, so a rate limit, an empty+/// balance, or a lost supervisor link must not be marked retryable. The+/// flag is advisory: the host's own retry policy decides.+fn turn_failure_error(id: &Value, failure: &str, details: &str) -> Value {+ let classification = pa_core::session_engine::error_classify::classify_error_message(failure);+ let context_overflow = pa_ai::utils::overflow::error_message_has_overflow(failure);+ let kind = if context_overflow {+ "context_limit"+ } else if classification.is_model_transport() {+ "model_transport"+ } else {+ classification.category+ };+ let category = if context_overflow {+ kind+ } else {+ classification.category+ };+ jsonrpc::error_response(+ id,+ jsonrpc::INTERNAL_ERROR,+ "Internal error",+ Some(&json!({+ "details": details,+ "kind": kind,+ "category": category,+ "retryable": kind == "model_transport",+ })),+ )+}+/// Two paths are the same cwd when their canonical forms match, or when they/// are the same directory on disk (dev/inode) — the bind-mount and/// case-normalized-FS cases a lexical comparison misses.@@ -51,3 +91,99 @@ fn same_cwd(requested: &Path, actual: &Path) -> bool {}false}++#[cfg(test)]+mod tests {+ use super::*;++ /// The error-data view of one failed prompt arm, as the #200 host+ /// contract reads it.+ fn error_data(failure: &str) -> Value {+ turn_failure_error(&json!(1), failure, "prime-agent turn failed")["error"]["data"].clone()+ }++ #[test]+ fn transport_failures_carry_the_model_transport_contract() {+ for failure in [+ "ECONNRESET: model connection reset mid-stream",+ "Connection error.",+ "Request timed out.",+ ] {+ let data = error_data(failure);+ assert_eq!(data["kind"], "model_transport", "{failure}: {data}");+ assert_eq!(data["retryable"], true, "{failure}: {data}");+ assert_eq!(data["details"], "prime-agent turn failed");+ }+ // The fine #2117 category rides separately.+ let network = error_data("ECONNRESET: model connection reset mid-stream");+ assert_eq!(network["category"], "network");+ let timeout = error_data("Request timed out.");+ assert_eq!(timeout["category"], "timeout");+ }++ #[test]+ fn only_model_transport_is_retryable() {+ // The auto-retry verdicts (rate limit, balance) do not transfer+ // to the boundary: a host retry is a fresh prompt, not an+ // idempotent replay (#200: 400/401 are never marked retryable).+ for failure in [+ "API Error: 429 rate limit exceeded",+ "402 insufficient credits",+ "401 unauthorized",+ "Provider server error",+ ] {+ let data = error_data(failure);+ assert_eq!(data["retryable"], false, "{failure}: {data}");+ assert_ne!(data["kind"], "model_transport", "{failure}: {data}");+ }+ let rate_limited = error_data("API Error: 429 rate limit exceeded");+ assert_eq!(rate_limited["kind"], "rate_limit");+ assert_eq!(rate_limited["category"], "rate_limit");+ let auth = error_data("401 unauthorized");+ assert_eq!(auth["kind"], "authentication");+ assert_eq!(auth["category"], "authentication");+ }++ #[test]+ fn context_overflow_keeps_the_context_limit_kind() {+ // The structured provider pattern table catches the Anthropic+ // overflow the #2117 keyword set misses.+ let data = error_data("prompt is too long: 213462 tokens > 200000 maximum");+ assert_eq!(data["kind"], "context_limit");+ assert_eq!(data["category"], "context_limit");+ assert_eq!(data["retryable"], false);+ // The OpenAI wire `code` reaches the same kind through the+ // classifier.+ let data = error_data("context_length_exceeded");+ assert_eq!(data["kind"], "context_limit");+ assert_eq!(data["retryable"], false);+ }++ #[test]+ fn an_http_408_is_a_provider_category_not_transport() {+ // A 408 is an HTTP status answer over a live connection (#200:+ // HTTP status errors retain the existing behavior): the `timeout`+ // subtype must not leak into the transport family.+ let data = error_data("408 Request Timeout");+ assert_eq!(data["kind"], "timeout");+ assert_eq!(data["category"], "timeout");+ assert_eq!(data["retryable"], false);+ }++ #[test]+ fn unclassified_and_link_failures_are_classified_not_retryable() {+ // A host can tell "classified, not retryable" from an untyped+ // error: every failed-prompt arm carries the typed data.+ for failure in [+ "Prompt cannot be empty",+ "the daemon connection closed mid-request",+ "an opaque harness crash",+ "Failed to publish ACP completion",+ ] {+ let data = error_data(failure);+ assert_eq!(data["kind"], "other", "{failure}: {data}");+ assert_eq!(data["category"], "other", "{failure}: {data}");+ assert_eq!(data["retryable"], false, "{failure}: {data}");+ }+ }+}