Files
OmniRoute/open-sse/executors/opencodeResponseBody.ts
Dizzle e325888d78 fix(sse): fail over opencode request on refused-route 403 (#13498)
Behind the new `OPENCODE_USER_BLOCKED_ROTATION` flag (default off), a 403 or 451 carrying `user_blocked` on a proxied opencode account rotates at most once to the next account instead of being returned as-is.

Maintainer rework before merge (kept the idea, no default behavior change):
- 403 and 451 are handled by one predicate (the original returned 451 without rotation), the refused account gets a cooldown and joins the tried-set, and the response body of the attempt rotated away from is cancelled.

Validated first on the combined board of all 38 PRs of this batch (10 merged as-is, 28 after the maintainer rework) on top of release/v3.8.51 c0f92ec: typecheck:core, check:open-sse-typecheck and check:dashboard-typecheck clean; ESLint clean on every changed file; file-size (rebaselined for the combined growth), complexity, cognitive-complexity, changelog-integrity, docs-counts, docs-sync, migration-numbering and i18n new-key gates green; 735 focused node:test cases with the only batch-caused failure (a flag-count assertion) fixed. Then re-validated alone on the fresh release tip right before this merge: ESLint on the changed files, typecheck:core, check:open-sse-typecheck, the file-size/complexity/changelog gates and this PR's own tests.

Thanks @maxmad64bis!
2026-09-15 20:39:39 -03:00

19 lines
743 B
TypeScript

/**
* opencodeResponseBody.ts — body hygiene for the opencode rotation loop.
*
* Leaf module: zero imports. An upstream response the loop decides not to
* return (a refusal it rotates away from, a failure it pauses after) keeps its
* connection busy until the body is consumed or cancelled. Cancelling releases
* the socket right away instead of whenever the Response is garbage-collected.
*/
/**
* Cancel a response body nobody will read. Never throws. A body that is already
* locked (a reader holds it) is left to that reader.
*/
export function discardResponseBody(response: Response | null | undefined): void {
const body = response?.body;
if (!body || body.locked) return;
void body.cancel().catch(() => undefined);
}