fix: arm emergency shrink on upstream failure to break relay-5xx deadlock (#11) - #229
Open
ranxianglei wants to merge 1 commit into
Open
fix: arm emergency shrink on upstream failure to break relay-5xx deadlock (#11)#229ranxianglei wants to merge 1 commit into
ranxianglei wants to merge 1 commit into
Conversation
…lock (#11) A failed request (relay 5xx, network error) never reports usage, so lastInputTokens stays frozen and every retry re-sends the same oversized payload — the relay fails it again. On 5xx and network-level failures, arm the emergency shrink with a local estimate of the body just sent: the next turn's processTurn fires the kernel's emergency nudge and tool-result truncate server-side, shrinking the payload without model cooperation. 4xx responses are excluded (auth/quota errors must not distort the usage signal).
📦 Built Package ArtifactBranch: Option A — Install from npm PR tag (recommended)npm install -g billion-context@pr-229Each push to this PR publishes a new version under the Option B — Download artifact
tar xzf billion-context-pr229.tgz
npm install -g packageThis comment is automatically updated on each push. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem (#11)
zcode user on a justwoker relay channel: every request for claude-opus-5 returns 500 after installing bili. Log shows:
{"error":{"type":"new_api_error","message":"upstream error: do request failed"}}— the relay's own error (relay → its upstream failed), fixed ~31s timeout, plus one Cloudflare 522Deadlock: compression triggering depends on the upstream usage report (
lastInputTokens). A failed request never reports usage → the proxy re-sends the same ~250K payload on every retry → the relay fails it again → forever. The session can never recover through bili.Fix
Arm the emergency shrink on failures with a local estimate of the body actually sent:
armFailureShrink()(src/server.ts) —estimateTokensFast(body)raisessession.stats.lastInputTokensif larger, thenmarkDirty(error paths return before forward()'s trailing save). Deliberate exception to the "tokenCount must be real usage" invariant: it only RAISES the value (lower bound → compress earlier, never later), the kernel no-ops below truncate.threshold, and the next successful usage report overwrites it.Next turn,
processTurnsees usage ≥100% → the kernel's emergency nudge +emergency-truncatenode truncate large old tool results server-side (no model cooperation — critical, because the model never responds in this state). The resent payload shrinks below the relay's tolerance and the session recovers.Tests
tests/e2e-relay-5xx-selfheal.test.ts(2 new e2e tests, mirroring the overflow-selfheal harness):[truncated for context space], shrank, upstream 200 → real usage report overwrites the armed valuelastInputTokensuntouchedFull suite: 567 tests pass, typecheck + build clean.