Skip to content

Commit 92e21bc

Browse files
authored
fix(devin): map Connect trailer codes onto the status core acts on (#4423)
* fix(devin): map Connect trailer codes onto the status core acts on Only the HTTP status line carried a status, so a cap or an expired credential delivered as a Connect EOS trailer fell through to inferHttpStatusFromAdapterMessage and became a generic 502 — not an auth prompt, not a backoff, and nothing core's failover acts on. connectTrailerHttpStatus maps the Connect codes Cognition actually sends, and treats permission_denied carrying "your limit will reset" or "reached overall message rate limit" as the quota refusal it is rather than an authorization failure. It reads the raw trailer message, not the enriched text, so the tool-blocklist wrapper cannot trip the quota regex. An unrecognised code returns undefined and keeps the older inference path. Review follow-up in the same change: unimplemented maps to 501, and the blanket "5xx is retryable" rule was putting retryable: true on the SSE failure a client reads for a call the service will never implement. * docs(devlog): record the trailer-status mapping as closed 020 listed the Connect trailer mapping as the deferred half of the cloud-direct work. It landed, along with the 501 retryability fix the review caught, so the doc records the outcome and the two accepted residuals.
1 parent 1e28e62 commit 92e21bc

4 files changed

Lines changed: 125 additions & 7 deletions

File tree

‎devlog/_plan/260912_devin_hardening/020_cloud_direct_hardening.md‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,3 +76,18 @@ already knows, with status 499.
7676
## Verification
7777

7878
bun test tests/providers/devin-adapter.test.ts tests/providers/devin-hardening.test.ts
79+
80+
## 5. A Connect trailer carries no status — closed
81+
82+
Landed in `connectTrailerHttpStatus`. The three EOS trailer throw sites now pass a status,
83+
so a cap delivered as `permission_denied` with "your limit will reset" reads as 429 rather
84+
than 403, an `unauthenticated` trailer reaches the auth path, and an unrecognised code still
85+
falls back to message inference. `unimplemented` maps to 501 and is explicitly non-retryable,
86+
because the blanket 5xx rule was telling clients to retry a call the service does not
87+
implement.
88+
89+
Accepted residuals: `internal`, `unknown` and `data_loss` map to 502 rather than Connect's
90+
500 — both are transient here and 502 is what this adapter already reported — and a genuine
91+
ACL denial whose text happens to contain the words "rate limit" would be read as a cap. The
92+
regex reads the raw trailer message, never the enriched text, so the tool-description
93+
blocklist wrapper cannot trip it.

‎src/adapters/devin.ts‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,10 @@ export function devinErrorClassification(error: unknown): { status?: number; err
5656
if (status === 401) return { status, errorType: "authentication_error", retryable: false };
5757
if (status === 403) return { status, errorType: "permission_error", retryable: false };
5858
if (status === 429) return { status, errorType: "rate_limit_error", retryable: true };
59+
// 501 is the one 5xx that will never succeed on a second attempt: the service
60+
// does not implement the call. Marking it retryable put `retryable: true` on
61+
// the SSE failure a client reads, inviting a retry that cannot change.
62+
if (status === 501) return { status, retryable: false };
5963
if (status >= 500) return { status, retryable: true };
6064
return { status, retryable: false };
6165
}

‎src/adapters/devin/cloud-direct/chat.ts‎

Lines changed: 60 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -969,6 +969,45 @@ export class CloudChatError extends Error {
969969

970970
const TRACE_ID_RE = /\(trace ID: ([0-9a-f]+)\)/i;
971971

972+
/**
973+
* A quota refusal Cognition delivers as `permission_denied`.
974+
*
975+
* "Your limit will reset in 13 minutes" and "Reached overall message rate
976+
* limit" are caps, not authorization failures. Classified as 403 they invite
977+
* the client to retry straight into a live cap; as 429 the proxy backs off and
978+
* can rotate.
979+
*/
980+
const TRAILER_QUOTA_RE = /\b(?:limit will reset|rate limit|quota exceeded|out of credits)\b/i;
981+
982+
/**
983+
* Connect error code to HTTP status.
984+
*
985+
* Without this only the HTTP status line reached the adapter, so a cap or an
986+
* expired credential delivered as an EOS trailer fell through to
987+
* `inferHttpStatusFromAdapterMessage` and became a generic 502 — which is not
988+
* retryable-with-backoff, not an auth prompt, and not something core's failover
989+
* acts on.
990+
*/
991+
export function connectTrailerHttpStatus(code: string | undefined, message: string): number | undefined {
992+
if (code === 'permission_denied' && TRAILER_QUOTA_RE.test(message)) return 429;
993+
switch (code) {
994+
case 'unauthenticated': return 401;
995+
case 'permission_denied': return 403;
996+
case 'resource_exhausted': return 429;
997+
case 'not_found': return 404;
998+
case 'unavailable': return 503;
999+
case 'deadline_exceeded': return 504;
1000+
case 'unimplemented': return 501;
1001+
case 'invalid_argument':
1002+
case 'failed_precondition':
1003+
case 'out_of_range': return 400;
1004+
case 'internal':
1005+
case 'unknown':
1006+
case 'data_loss': return 502;
1007+
default: return undefined;
1008+
}
1009+
}
1010+
9721011
/**
9731012
* Stream chat events from the cloud. Yields CloudChatEvent (text deltas, tool
9741013
* call deltas, finish reason). Use `streamChatText` for legacy text-only iteration.
@@ -1085,10 +1124,9 @@ export async function* streamChatEvents(req: CloudChatRequest): AsyncGenerator<C
10851124
// error event and /api/logs, and a Connect error can quote the request that
10861125
// produced it - which is the request holding the api_key.
10871126
//
1088-
// Only the HTTP status line is carried here. A Connect EOS trailer that
1089-
// reports resource_exhausted or unavailable still arrives without a status,
1090-
// so a cap delivered that way keeps the older message-inference path.
1091-
// Mapping trailer codes onto HTTP statuses is deliberately a follow-up.
1127+
// The status line is carried on the error. A cap or an expired credential
1128+
// delivered instead as a Connect EOS trailer is mapped by
1129+
// connectTrailerHttpStatus at the trailer sites below.
10921130
throw new CloudChatError(`GetChatMessage failed (HTTP ${resp.status})`, undefined, undefined, resp.status);
10931131
}
10941132
if (!resp.body) {
@@ -1316,7 +1354,12 @@ export async function* streamChatEvents(req: CloudChatRequest): AsyncGenerator<C
13161354
`service accepts. If the request is unchanged and this is new, the ` +
13171355
`account's model access is the next thing to check. ` +
13181356
`(cloud trace ID: ${trailerError.traceId ?? 'n/a'})`;
1319-
throw new CloudChatError(enriched, trailerError.code, trailerError.traceId);
1357+
throw new CloudChatError(
1358+
enriched,
1359+
trailerError.code,
1360+
trailerError.traceId,
1361+
connectTrailerHttpStatus(trailerError.code, trailerError.message),
1362+
);
13201363
}
13211364
// Cognition also returns `permission_denied` when a tool description
13221365
// contains a blocklisted phrase that the sanitizer above did not catch
@@ -1336,9 +1379,19 @@ export async function* streamChatEvents(req: CloudChatRequest): AsyncGenerator<C
13361379
// was a phrase match at all.
13371380
`(cloud message: ${trailerError.message}) ` +
13381381
`(cloud trace ID: ${trailerError.traceId ?? 'n/a'})`;
1339-
throw new CloudChatError(enriched, trailerError.code, trailerError.traceId);
1382+
throw new CloudChatError(
1383+
enriched,
1384+
trailerError.code,
1385+
trailerError.traceId,
1386+
connectTrailerHttpStatus(trailerError.code, trailerError.message),
1387+
);
13401388
}
1341-
throw new CloudChatError(trailerError.message, trailerError.code, trailerError.traceId);
1389+
throw new CloudChatError(
1390+
trailerError.message,
1391+
trailerError.code,
1392+
trailerError.traceId,
1393+
connectTrailerHttpStatus(trailerError.code, trailerError.message),
1394+
);
13421395
}
13431396
// Truncation detection: the cloud always terminates a successful stream
13441397
// with an EOS trailer. If we hit `done` from the body reader without one,

‎tests/providers/devin-hardening.test.ts‎

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { anySignal } from "../../src/lib/abort";
77
import { buildGetChatMessageRequestForTests } from "../../src/adapters/devin/cloud-direct/chat";
88
import { decodeModelUsageStats } from "../../src/adapters/devin/cloud-direct/chat";
99
import { CloudChatError, decodeChatFrame } from "../../src/adapters/devin/cloud-direct/chat";
10+
import { connectTrailerHttpStatus } from "../../src/adapters/devin/cloud-direct/chat";
1011
import { devinErrorClassification, mergeDevinUsage } from "../../src/adapters/devin";
1112
import { iterFields } from "../../src/adapters/devin/cloud-direct/wire";
1213
import { buildMetadata, normalizeDevinSessionToken } from "../../src/adapters/devin/cloud-direct/metadata";
@@ -398,3 +399,48 @@ describe("devin usage merging and error classification", () => {
398399
expect(devinErrorClassification(new CloudChatError("x", "resource_exhausted"))).toEqual({});
399400
});
400401
});
402+
403+
describe("connect trailer to HTTP status", () => {
404+
test("a cap delivered as permission_denied is a 429, not a 403", () => {
405+
// Cognition sends the account cap through the same code as an ACL denial.
406+
// Classified 403 the client retries straight into a live cap.
407+
expect(connectTrailerHttpStatus("permission_denied", "Your limit will reset in 13 minutes")).toBe(429);
408+
expect(connectTrailerHttpStatus("permission_denied", "Reached overall message rate limit")).toBe(429);
409+
// An ordinary denial stays a denial.
410+
expect(connectTrailerHttpStatus("permission_denied", "an internal error occurred")).toBe(403);
411+
});
412+
413+
test("the remaining Connect codes map to the status core acts on", () => {
414+
expect(connectTrailerHttpStatus("unauthenticated", "")).toBe(401);
415+
expect(connectTrailerHttpStatus("resource_exhausted", "")).toBe(429);
416+
expect(connectTrailerHttpStatus("unavailable", "")).toBe(503);
417+
expect(connectTrailerHttpStatus("deadline_exceeded", "")).toBe(504);
418+
expect(connectTrailerHttpStatus("invalid_argument", "")).toBe(400);
419+
expect(connectTrailerHttpStatus("internal", "")).toBe(502);
420+
// An unknown code keeps the older message-inference path rather than
421+
// asserting a status nobody measured.
422+
expect(connectTrailerHttpStatus("some_new_code", "")).toBeUndefined();
423+
expect(connectTrailerHttpStatus(undefined, "")).toBeUndefined();
424+
});
425+
426+
test("a trailer status reaches the adapter's structured classification", () => {
427+
const err = new CloudChatError("capped", "permission_denied", "abc", connectTrailerHttpStatus("permission_denied", "Your limit will reset in 3 minutes"));
428+
expect(devinErrorClassification(err)).toEqual({ status: 429, errorType: "rate_limit_error", retryable: true });
429+
});
430+
});
431+
432+
describe("devin status classification across the newly reachable trailer codes", () => {
433+
const cls = (status: number) => devinErrorClassification(new CloudChatError("x", undefined, undefined, status));
434+
435+
test("a request the service will not accept is never retried", () => {
436+
expect(cls(400)).toEqual({ status: 400, retryable: false });
437+
expect(cls(404)).toEqual({ status: 404, retryable: false });
438+
// 501 is the one 5xx a second attempt cannot change.
439+
expect(cls(501)).toEqual({ status: 501, retryable: false });
440+
});
441+
442+
test("a timeout or an unavailable service is retryable", () => {
443+
expect(cls(503)).toEqual({ status: 503, retryable: true });
444+
expect(cls(504)).toEqual({ status: 504, retryable: true });
445+
});
446+
});

0 commit comments

Comments
 (0)