Skip to content

Local-first AI coding agent desktop: Electron + Rust host core + pi Agent Harness + user-installable plugins

fix(agent-runtime): do not treat connection abort wording as user Stop (#1865)

xpeng5278-webb8748f2ebcb1
diff --git a/docs/spec/03-runtime/08-error-codes.md b/docs/spec/03-runtime/08-error-codes.md
index b0d1997ca..d99f8b3a3 100644
--- a/docs/spec/03-runtime/08-error-codes.md
+++ b/docs/spec/03-runtime/08-error-codes.md
@@ -381,7 +381,11 @@ replayed by either retry budget, and a delegate reports it as
classified first and stays `PROVIDER_RATE_LIMITED`.
An exact `terminated` provider message and equivalent premature stream-close
-messages map to `STREAM_FAILED`. A request-setup or post-response
+messages map to `STREAM_FAILED`. A message is `TURN_ABORTED` only for a
+structured abort or the fixed phrases `Request aborted`, `The operation was
+aborted`, and `Turn aborted`; connection-abort wording such as `software
+caused connection abort` stays retriable `NETWORK_ERROR`, and `stream aborted`
+stays retriable `STREAM_FAILED`. A request-setup or post-response
`PROVIDER_RATE_LIMITED` uses the shared runtime budget: ten retries after the
initial attempt, with setup and stream failures counting together. Non-429
transient failures — `STREAM_FAILED`, `NETWORK_ERROR`, `TIMEOUT`, and retryable
diff --git a/packages/agent-runtime/src/agent-errors.test.ts b/packages/agent-runtime/src/agent-errors.test.ts
index 4290ee0d0..01e45fcfa 100644
--- a/packages/agent-runtime/src/agent-errors.test.ts
+++ b/packages/agent-runtime/src/agent-errors.test.ts
@@ -501,6 +501,50 @@ describe("classifyAgentError", () => {
});
});
+ it("does not treat transport abort wording as a user Stop", () => {
+ // Socket and stream failures say "abort" without being a cancellation.
+ // They must stay retriable so provider retry runs and the UI does not
+ // show Stop.
+ expect(classifyAgentError(new Error("software caused connection abort"))).toMatchObject({
+ code: "NETWORK_ERROR",
+ retriable: true,
+ details: { networkCategory: "reset" },
+ });
+ expect(classifyAgentError(new Error("connection aborted"))).toMatchObject({
+ code: "NETWORK_ERROR",
+ retriable: true,
+ details: { networkCategory: "reset" },
+ });
+ expect(classifyAgentError(new Error("stream aborted"))).toMatchObject({
+ code: "STREAM_FAILED",
+ retriable: true,
+ });
+ expect(classifyAgentError(new Error("provider aborted the stream"))).toMatchObject({
+ code: "STREAM_FAILED",
+ retriable: true,
+ });
+
+ expect(classifyAgentError("Request aborted")).toMatchObject({
+ code: "TURN_ABORTED",
+ retriable: false,
+ });
+ expect(classifyAgentError("The operation was aborted")).toMatchObject({
+ code: "TURN_ABORTED",
+ retriable: false,
+ });
+ expect(classifyAgentError("Turn aborted while compacting context")).toMatchObject({
+ code: "TURN_ABORTED",
+ retriable: false,
+ });
+ const abortErr = Object.assign(new Error("The operation was aborted"), {
+ name: "AbortError",
+ });
+ expect(classifyAgentError(abortErr)).toMatchObject({
+ code: "TURN_ABORTED",
+ retriable: false,
+ });
+ });
+
it("classifies a provider termination as a retryable stream failure", () => {
expect(classifyAgentError("terminated")).toMatchObject({
code: "STREAM_FAILED",
diff --git a/packages/agent-runtime/src/agent-errors.ts b/packages/agent-runtime/src/agent-errors.ts
index eb6b4ece1..20a761956 100644
--- a/packages/agent-runtime/src/agent-errors.ts
+++ b/packages/agent-runtime/src/agent-errors.ts
@@ -26,7 +26,7 @@ export type ClassifiedAgentError = {
const MAX_ERROR_MESSAGE_CHARS = 600;
const NETWORK_PATTERN =
- /ECONNREFUSED|ECONNRESET|ENOTFOUND|EAI_AGAIN|ETIMEDOUT|EPIPE|ENETUNREACH|EHOSTUNREACH|UND_ERR|fetch failed|socket hang up|network error|connection error|connection refused|dns/i;
+ /ECONNREFUSED|ECONNRESET|ECONNABORTED|ENOTFOUND|EAI_AGAIN|ETIMEDOUT|EPIPE|ENETUNREACH|EHOSTUNREACH|UND_ERR|fetch failed|socket hang up|network error|connection error|connection refused|connection abort|dns/i;
const CONTEXT_PATTERN =
/context[ _-]?length|maximum context|context window|too many tokens|prompt is too long|input token count|exceeds the (?:maximum|model)|token limit/i;
@@ -63,7 +63,17 @@ function isContextOverflowMessage(message: string): boolean {
}
const STREAM_TERMINATION_PATTERN =
- /\bterminated\b|stream ended without finish_reason|premature(?:ly)?\s+(?:closed|ended)|(?:stream|response).*(?:closed|interrupted)/i;
+ /\bterminated\b|stream ended without finish_reason|premature(?:ly)?\s+(?:closed|ended)|(?:stream|response).*(?:closed|interrupted)|\bstream aborted\b|aborted the stream/i;
+
+/**
+ * User-Stop wording the runtime and AbortError actually emit (`Request
+ * aborted`, `The operation was aborted`, `Turn aborted …`). Do not widen this
+ * to `\babort`: socket and stream failures ("software caused connection
+ * abort", "connection aborted", "stream aborted") contain the same stem and
+ * must stay retriable network or stream errors instead of `TURN_ABORTED`.
+ */
+const TURN_ABORTED_MESSAGE_PATTERN =
+ /\b(?:request aborted|the operation was aborted|turn aborted)\b/i;
/** An adapter refusing a request option, e.g. "Custom fetch is not supported
* by the Google Generative AI adapter" (issue #1072). */
@@ -265,7 +275,7 @@ function networkCategoryFromText(text: string): NetworkFailureCategory {
return "unreachable";
}
if (
- /ECONNRESET|ECONNABORTED|EPIPE|socket hang up|premature close|reset by peer|connection (?:was )?(?:closed|lost|reset)/i.test(
+ /ECONNRESET|ECONNABORTED|EPIPE|socket hang up|premature close|reset by peer|connection (?:was )?(?:closed|lost|reset|abort)/i.test(
text,
)
) {
@@ -462,7 +472,7 @@ export function classifyAgentError(err: unknown): ClassifiedAgentError {
// cause, and that turn must read as stopped, not as a compaction failure.
if (
explicitlyAborted ||
- /\babort/i.test(rawMessage)
+ TURN_ABORTED_MESSAGE_PATTERN.test(rawMessage)
) {
return result("TURN_ABORTED", false);
}