Skip to content

fix: return HTTP 400 Response for overflow to trigger opencode compaction - #114

Merged
tickernelz merged 1 commit into
tickernelz:masterfrom
leecoder:fix/overflow-response-for-compaction
Jul 23, 2026
Merged

tickernelz merged 1 commit into
tickernelz:masterfrom
leecoder:fix/overflow-response-for-compaction

Conversation

@leecoder

Copy link
Copy Markdown
Contributor

Problem

When Kiro API returns "Input is too long" (400), the plugin was throwing a plain Error. This bypassed ai-sdk's APICallError handling, so OpenCode never recognized it as a context overflow and never triggered automatic compaction.

Solution

Return a proper HTTP 400 Response object with:

  • message: "input is too long for requested model" (matches OpenCode's overflow pattern)
  • type: "invalid_request_error"
  • code: "context_length_exceeded"

This allows ai-sdk to convert it into an APICallError, which OpenCode then pattern-matches to trigger compaction retry.

Testing

  • Verified overflow scenario now triggers compaction instead of showing a terminal error.

…tion

Previously threw a plain Error for 'input is too long' which bypassed
ai-sdk's APICallError handling. OpenCode expects a fetch Response so
ai-sdk can convert it to APICallError and match the overflow pattern
to trigger automatic context compaction.

Now returns a proper 400 Response with context_length_exceeded code
that ai-sdk recognizes as an overflow signal.
@tickernelz
tickernelz merged commit 8966554 into tickernelz:master Jul 23, 2026
1 check passed
@tickernelz

Copy link
Copy Markdown
Owner

Merged after validating the exact PR head against the latest master.

The initial integrated suite exposed an unintended regression in the generic non-overflow SDK error contract. I kept the new context-overflow 400 Response behavior, restored the existing error contract for all other HTTP failures, and added a regression test for the overflow response.

Final merged-tree verification:

  • TypeScript typecheck passed
  • Build passed
  • 141 tests passed, 0 failed
  • Prettier check passed

RvVeen added a commit to Servoy/opencode-kiro-auth that referenced this pull request Aug 10, 2026
While rebasing this branch onto upstream/master, three issues surfaced
during review that needed fixing on top of the mechanical conflict
resolution:

1. Test API drift: the tool-name-registry API on upstream
   (createToolNameRegistry/restoreToolName, per-request scoped) diverged
   from this branch's standalone shortenToolName/buildToolNameMaps
   helpers. Updated tests to use the registry API that survived conflict
   resolution, and aligned description/schema length expectations with
   upstream's sanitizeCodeWhispererSchema defaults. Also fixed two tests
   that assumed pre-upstream ErrorHandler/AccountManager behavior
   (expiresAt reset on bearer-invalid, markUnhealthy mock) that changed
   upstream in tickernelz#114.

2. Duplicate/dead code introduced by the rebase:
   - request-handler.ts's uiEffort budget lookup used its own
     THINKING_BUDGETS in constants.ts, a scale that disagreed with
     plugin/effort.ts's version (the one getEffectiveEffort/
     budgetToEffort actually use to map a budget back to an effort level
     for the Kiro wire request). UI 'high' silently became wire 'max'
     instead of 'high'. Now imports THINKING_BUDGETS from plugin/effort.ts
     so there is a single source of truth.
   - withInProcessLock() in locked-operations.ts was exported but never
     called anywhere — dead code left over from a lock-hardening commit
     not carried into this branch's account-write path. Removed.
   - image-cache.ts's defaultCacheDir() duplicated the platform config-dir
     lookup already in plugin/config/loader.ts (getConfigDir, now
     exported). Reuses it instead of a fourth copy of the same win32/XDG
     branching.
   - request.ts had a redundant DEBUG/OPENCODE_LOG_LEVEL guard around a
     logger.debug() call that already gates on those env vars internally.

3. Regression: RequestHandler.kiroRequestQueue serializes all Kiro API
   calls across sessions/workspaces and predates this branch (introduced
   in 'Match Kiro CLI retry pacing', still present on upstream/master).
   The session-id extraction change earlier in this branch replaced the
   enqueueKiroRequest() wrapper in handle() with a direct call to
   handleKiroRequest(), silently dropping the serialization. Restored the
   queue with sessionId extracted before enqueueing, so per-session
   conversation isolation and global request pacing both hold.
RvVeen added a commit to Servoy/opencode-kiro-auth that referenced this pull request Aug 10, 2026
While rebasing this branch onto upstream/master, three issues surfaced
during review that needed fixing on top of the mechanical conflict
resolution:

1. Test API drift: the tool-name-registry API on upstream
   (createToolNameRegistry/restoreToolName, per-request scoped) diverged
   from this branch's standalone shortenToolName/buildToolNameMaps
   helpers. Updated tests to use the registry API that survived conflict
   resolution, and aligned description/schema length expectations with
   upstream's sanitizeCodeWhispererSchema defaults. Also fixed two tests
   that assumed pre-upstream ErrorHandler/AccountManager behavior
   (expiresAt reset on bearer-invalid, markUnhealthy mock) that changed
   upstream in tickernelz#114.

2. Duplicate/dead code introduced by the rebase:
   - request-handler.ts's uiEffort budget lookup used its own
     THINKING_BUDGETS in constants.ts, a scale that disagreed with
     plugin/effort.ts's version (the one getEffectiveEffort/
     budgetToEffort actually use to map a budget back to an effort level
     for the Kiro wire request). UI 'high' silently became wire 'max'
     instead of 'high'. Now imports THINKING_BUDGETS from plugin/effort.ts
     so there is a single source of truth.
   - withInProcessLock() in locked-operations.ts was exported but never
     called anywhere — dead code left over from a lock-hardening commit
     not carried into this branch's account-write path. Removed.
   - image-cache.ts's defaultCacheDir() duplicated the platform config-dir
     lookup already in plugin/config/loader.ts (getConfigDir, now
     exported). Reuses it instead of a fourth copy of the same win32/XDG
     branching.
   - request.ts had a redundant DEBUG/OPENCODE_LOG_LEVEL guard around a
     logger.debug() call that already gates on those env vars internally.

3. Regression: RequestHandler.kiroRequestQueue serializes all Kiro API
   calls across sessions/workspaces and predates this branch (introduced
   in 'Match Kiro CLI retry pacing', still present on upstream/master).
   The session-id extraction change earlier in this branch replaced the
   enqueueKiroRequest() wrapper in handle() with a direct call to
   handleKiroRequest(), silently dropping the serialization. Restored the
   queue with sessionId extracted before enqueueing, so per-session
   conversation isolation and global request pacing both hold.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants