Skip to content

Shared task board where people and AI coding agents work as teammates. Source-available, self-hostable.

Go

MUL-5708 fix(taskfailure): classify response-side context-window overflow (#6366)

Bohan Jiang79c5832e1dbc
diff --git a/server/internal/service/task_complete_race_test.go b/server/internal/service/task_complete_race_test.go
index ef8bde0d79..1b8355145e 100644
--- a/server/internal/service/task_complete_race_test.go
+++ b/server/internal/service/task_complete_race_test.go
@@ -324,3 +324,40 @@ func TestSkillBundleFailureFromLegacyDaemonRetries(t *testing.T) {
t.Errorf("a skill-bundle failure reported by a current daemon must be retried; got reason %q", current)
}
}
+
+// TestContextOverflowFromLegacyDaemonRetiresSession is the mixed-version
+// regression for GH #6360. It walks the chain FailTask runs for a task an
+// un-upgraded daemon just failed on a response-side context overflow, and
+// asserts the user-visible outcome: the conversation is retired, so the next
+// comment on that issue starts from a fresh session.
+//
+// Higher stakes than the skill-bundle case above. There, a stale label costs
+// one missed retry; here the catchall is on no resume blacklist, so the
+// over-full session stays pinned as the (agent, issue) resume pointer and
+// EVERY later comment replays the same overflow — one un-upgraded host means a
+// permanently stuck issue until it updates.
+func TestContextOverflowFromLegacyDaemonRetiresSession(t *testing.T) {
+ // Verbatim from Claude Code 2.1.x: the turn is not rejected with a 400,
+ // the response comes back with stop_reason model_context_window_exceeded.
+ const overflowErr = "API Error: The model has reached its context window limit."
+
+ legacyReason := taskfailure.ReasonAgentUnknown.String()
+ if ResumeUnsafeFailure(legacyReason, overflowErr) {
+ t.Fatal("precondition: the raw catchall must be resume-safe, or this test proves nothing")
+ }
+
+ normalized := taskfailure.NormalizeDaemonReason(legacyReason, overflowErr).String()
+ if normalized != taskfailure.ReasonAgentContextOverflow.String() {
+ t.Fatalf("normalized reason = %q, want %q", normalized, taskfailure.ReasonAgentContextOverflow)
+ }
+ if !ResumeUnsafeFailure(normalized, overflowErr) {
+ t.Errorf("an overflow reported by an old daemon must retire the session; got reason %q", normalized)
+ }
+
+ // A current daemon supplies the reason itself and must reach the same
+ // outcome — the two versions converge rather than diverging by client.
+ current := taskfailure.NormalizeDaemonReason(taskfailure.ReasonAgentContextOverflow.String(), overflowErr).String()
+ if !ResumeUnsafeFailure(current, overflowErr) {
+ t.Errorf("an overflow reported by a current daemon must retire the session; got reason %q", current)
+ }
+}
diff --git a/server/pkg/taskfailure/classify.go b/server/pkg/taskfailure/classify.go
index 62c5e744f1..042bfc8b95 100644
--- a/server/pkg/taskfailure/classify.go
+++ b/server/pkg/taskfailure/classify.go
@@ -81,6 +81,7 @@ func Classify(rawError string) Reason {
"prompt is too long",
"context size has been exceeded",
),
+ containsAny(lower, contextWindowExceededWitnesses...),
// SQL had `%token%limit%` — ILIKE wildcard between tokens. We
// approximate with both substrings present, which catches
// "token limit", "tokens per minute limit", etc., without the
@@ -254,6 +255,27 @@ func Classify(rawError string) Reason {
return ReasonAgentUnknown
}
+// contextWindowExceededWitnesses are the two wordings for an overflow reported
+// on the RESPONSE rather than as a 400 on the request: the provider accepts the
+// call and ends the turn with stop_reason "model_context_window_exceeded", which
+// Claude Code 2.1.x surfaces verbatim as "API Error: The model has reached its
+// context window limit." (GH #6360). Both are matched so a backend forwarding
+// the raw stop reason classifies the same way as one forwarding the CLI's copy.
+//
+// Neither carries "token" nor any of rule 1's other phrases, so before this the
+// failure landed in agent_error.unknown — a reason no resume blacklist covers,
+// which left the over-full session pinned as the resume pointer and made every
+// later comment on that issue replay the same overflow.
+//
+// Each is an unambiguous witness on its own, which is what lets
+// NormalizeDaemonReason reuse them to upgrade an older daemon's catchall
+// server-side. Matched against pre-lowercased text.
+// Mirror these substrings into the MUL-1949 offline backfill SQL.
+var contextWindowExceededWitnesses = []string{
+ "context window limit",
+ "model_context_window_exceeded",
+}
+
// legacySkillBundlePrefix is the exact wrapper a pre-MUL-5370 daemon put on a
// failed skill-bundle download. It is an unambiguous witness: no other code
// path ever produced it, and a current daemon writes "skill bundle
@@ -272,6 +294,22 @@ var legacySkillBundleReasons = map[string]bool{
"agent_error": true,
}
+// legacyContextOverflowReasons are the buckets an older daemon lands the
+// response-side context overflow in: agent_error.unknown from its own
+// classifier (its rule 1 predates contextWindowExceededWitnesses) and the
+// pre-MUL-1949 coarse agent_error.
+//
+// Deliberately narrower than legacySkillBundleReasons. A refined reason means
+// the old daemon matched an earlier rule on the same text — process_failure on
+// a crash marker, provider_network on a stream cut — and that says more about
+// what actually ended the run than a witness appearing somewhere in the same
+// blob does. Upgrading those would discard information; leaving them alone
+// costs at most the pre-existing behaviour.
+var legacyContextOverflowReasons = map[string]bool{
+ string(ReasonAgentUnknown): true,
+ "agent_error": true,
+}
+
// NormalizeDaemonReason upgrades a failure_reason reported by an older daemon
// onto the taxonomy this server understands, using the raw error text as the
// witness. It returns the reason unchanged when nothing applies.
@@ -284,14 +322,26 @@ var legacySkillBundleReasons = map[string]bool{
// generic copy. Recognising the wire shape an old daemon produces closes that
// gap the moment the server deploys.
//
-// This is a boundary compatibility shim, not internal fallback logic: it can
-// be deleted once no daemon old enough to emit legacySkillBundlePrefix is
-// still reporting.
+// This is a boundary compatibility shim, not internal fallback logic: each rule
+// can be deleted once no daemon old enough to produce its wire shape is still
+// reporting.
func NormalizeDaemonReason(reason, rawError string) Reason {
if legacySkillBundleReasons[reason] &&
strings.HasPrefix(strings.TrimSpace(rawError), legacySkillBundlePrefix) {
return ReasonSkillBundleUnavailable
}
+ // GH #6360: the same mixed-version gap, on a failure where waiting for
+ // every host to update is more expensive. A daemon whose rule 1 predates
+ // contextWindowExceededWitnesses reports the catchall, and the catchall is
+ // on no resume blacklist — so the over-full session stays pinned as the
+ // resume pointer and every later comment on that issue replays the same
+ // overflow. One un-upgraded host means a permanently stuck (agent, issue)
+ // pair, not just a missing label; upgrading here retires the session the
+ // moment the server deploys.
+ if legacyContextOverflowReasons[reason] &&
+ containsAny(strings.ToLower(rawError), contextWindowExceededWitnesses...) {
+ return ReasonAgentContextOverflow
+ }
return Reason(reason)
}
diff --git a/server/pkg/taskfailure/classify_test.go b/server/pkg/taskfailure/classify_test.go
index 9cd240b6f1..487f7f5496 100644
--- a/server/pkg/taskfailure/classify_test.go
+++ b/server/pkg/taskfailure/classify_test.go
@@ -40,6 +40,11 @@ func TestClassifyRules(t *testing.T) {
{"prompt is too long", "API Error: prompt is too long: 250000 tokens > 200000 maximum", ReasonAgentContextOverflow},
{"context size has been exceeded", "context size has been exceeded; consider /compact", ReasonAgentContextOverflow},
{"token limit", "Hit the token limit for this conversation", ReasonAgentContextOverflow},
+ // GH #6360, verbatim from Claude Code 2.1.x. The turn is not
+ // rejected with a 400 — the response comes back with stop_reason
+ // "model_context_window_exceeded" and the CLI prints this line.
+ {"claude code context window limit", "API Error: The model has reached its context window limit.", ReasonAgentContextOverflow},
+ {"raw stop reason", `{"stop_reason":"model_context_window_exceeded"}`, ReasonAgentContextOverflow},
// 2. Missing config.
{"missing env var", "Missing environment variable: `MIFY_API_KEY`.", ReasonAgentMissingConfig},
@@ -352,6 +357,58 @@ func TestNormalizeDaemonReason(t *testing.T) {
raw: legacyErr,
want: Reason(""),
},
+
+ // --- GH #6360: response-side context overflow. An un-upgraded daemon
+ // classifies the wordings below as the catchall, which is on no resume
+ // blacklist — so without this the over-full session stays pinned and
+ // every later comment on the issue replays the same overflow.
+ {
+ name: "old daemon catchall on the claude code wording is upgraded",
+ reason: string(ReasonAgentUnknown),
+ raw: "API Error: The model has reached its context window limit.",
+ want: ReasonAgentContextOverflow,
+ },
+ {
+ name: "old daemon catchall on the raw stop reason is upgraded",
+ reason: string(ReasonAgentUnknown),
+ raw: `{"stop_reason":"model_context_window_exceeded"}`,
+ want: ReasonAgentContextOverflow,
+ },
+ {
+ name: "pre-MUL-1949 coarse reason on the overflow is upgraded",
+ reason: "agent_error",
+ raw: "API Error: The model has reached its context window limit.",
+ want: ReasonAgentContextOverflow,
+ },
+ {
+ // The witness is matched case-insensitively, like Classify's.
+ name: "witness casing does not defeat the upgrade",
+ reason: string(ReasonAgentUnknown),
+ raw: "API ERROR: THE MODEL HAS REACHED ITS CONTEXT WINDOW LIMIT.",
+ want: ReasonAgentContextOverflow,
+ },
+ {
+ // A current daemon already classified it; nothing to do.
+ name: "current daemon overflow reason passes through",
+ reason: string(ReasonAgentContextOverflow),
+ raw: "API Error: The model has reached its context window limit.",
+ want: ReasonAgentContextOverflow,
+ },
+ {
+ // A refined reason means the old daemon matched an earlier rule on
+ // this same text. That is a stronger statement about what ended the
+ // run than the witness is, so it is left alone.
+ name: "refined reason with the overflow witness is left alone",
+ reason: string(ReasonAgentProcessFailure),
+ raw: "claude exited with error: exit status 1: The model has reached its context window limit.",
+ want: ReasonAgentProcessFailure,
+ },
+ {
+ name: "catchall without an overflow witness is left alone",
+ reason: string(ReasonAgentUnknown),
+ raw: "API Error: the model is overloaded",
+ want: ReasonAgentUnknown,
+ },
}
for _, tc := range cases {