Skip to content

fix(coding-agent): bound btw side-query context - #826

Merged
code-yeongyu merged 6 commits into
mainfrom
fix/btw-context-budget-20260812
Aug 12, 2026
Merged

fix(coding-agent): bound btw side-query context#826
code-yeongyu merged 6 commits into
mainfrom
fix/btw-context-budget-20260812

Conversation

@code-yeongyu

@code-yeongyu code-yeongyu commented Aug 12, 2026

Copy link
Copy Markdown
Owner

Summary

  • budget /btw side-query prompts against the selected model's effective prompt window
  • reuse Senpi's deterministic reducer, structural tool-pair repair, and oldest-first pruning
  • preserve the newest usable context and final question while leaving main-session history untouched
  • fail locally with an actionable /compact message when mandatory prompt content cannot fit

Root cause

/btw captured and converted the current branch, then called the provider runtime directly without passing through the main-turn context pipeline. A large session could therefore send a raw side-query snapshot larger than the selected model accepted, even while the main turn's reduced context remained healthy.

The reported screenshot reproduced the user-visible symptom:

/btw 이거 draft PR은 이미 만들어져있는건가?
Codex error: Your input exceeds the context window of this model.

Design

  • Derive the side-query prompt window from the shared compaction getPromptContextWindow() policy.
  • Include system prompt, BTW instruction, history, and the final question in token accounting.
  • Preserve small snapshots unchanged.
  • For oversized snapshots: deterministic reduction -> tool-pair repair -> oldest-first pruning -> final repair and budget re-check.
  • Keep dynamic modelRuntime.streamSimple, credential/header routing, no-tools mode, cancellation, synchronous snapshotting, and ephemeral history behavior unchanged.

The refreshed gajae-code implementation corroborates newest-first retention and oldest-context removal, but uses fixed 12-turn/64 KiB bounds. This PR intentionally uses Senpi's model-aware token machinery instead of copying those fixed limits.

RED -> GREEN evidence

  • Overflow RED: red-overflow.log — pre-fix context estimated 8,018 tokens against a 1,744-token budget.
  • Boundary RED: red-boundary.log — pre-fix context estimated 4,027 tokens against a 900-token budget.
  • Exact-metric mutation RED after review: disabling the budget guard failed both tests at 8,083 > 4,500 and 4,890 > 900.
  • System-prompt mutation RED: omitting system-prompt subtraction failed at 16,073 > 14,000.
  • Focused GREEN: btw-side-query.test.ts plus regression /btw: No API provider registered for api: claude-agent-sdk #488, 16/16 passing.

Evidence is preserved under:

local-ignore/qa-evidence/20260812-btw-context-budget/
local-ignore/qa-evidence/20260812-btw-rpc-baseline/

Real CLI QA

Source-built Senpi RPC mode ran against an isolated local fake provider with a persisted oversized session.

Observed:

  • /btw 이거 draft PR은 이미 만들어져있는건가? returned an info notification
  • one side provider request
  • newest large context preserved
  • oldest overflow context removed
  • empty /btw returned Usage: /btw <question> with no extra provider request
  • main history remained at four messages
  • fake server, RPC process, and sandbox were removed

Independent hands-on review also exercised a 1.08 MB / 49-entry real session: the unbounded request was approximately 293,511 tokens against a 56,000-token prompt window; the bounded request was 50,038 tokens and completed successfully.

Validation

  • npm run check
  • npm run build
  • root npx tsc --noEmit
  • focused BTW tests: 16/16
  • changelog gate against origin/main
  • Senpi QA helper self-tests: RPC, mock-loop, TUI, CLI
  • real seeded-session RPC BTW scenario

The full coding-agent suite completed with 7,228 passed / 35 skipped and four load-sensitive failures outside this diff. Each failed test passed on isolated rerun:

  • footer reftable debounce: 1/1
  • MCP connection races: 2/2
  • app-server goal cleanup: 1/1

Known conflict: PR #746

Open PR #746 changes the same BTW context builder to add prior BTW continuity. The features are complementary, not duplicates.

When resolving the landing order, priorBtw messages from #746 must be included in the message list before the final question and then flow through boundSideQueryMessages(). Taking either PR's buildSideQueryContext() body wholesale would silently drop the other feature or reintroduce unbounded BTW growth.

Reviewer results

  • goal/constraint review: approved after type/test blocker fixes
  • code-quality review: approved after exact-metric assertions and shared prompt-window policy
  • security review: pass
  • independent hands-on QA: pass
  • repository/GitHub context review: changelog placement fixed; PR-number citation will be corrected to this PR number immediately after creation

Summary by cubic

Bounds /btw side-query prompts to the selected model’s effective prompt window to prevent context-window overflows on large sessions. Preserves newest relevant context and the final question while leaving the main session history unchanged.

  • Bug Fixes
    • Budgets system prompt, BTW instruction, history, and question against the model’s prompt window with output reserve via getPromptContextWindow.
    • For oversized snapshots: deterministic reduction → tool-pair repair → oldest-first pruning → final repair.
    • Fails locally with an actionable /compact message when mandatory content cannot fit.
    • Keeps provider dispatch, no-tools mode, cancellation, and ephemeral history behavior unchanged.

Written for commit 86b719d. Summary will update on new commits.

Review in cubic

@code-yeongyu
code-yeongyu merged commit e147609 into main Aug 12, 2026
15 checks passed
@code-yeongyu
code-yeongyu deleted the fix/btw-context-budget-20260812 branch August 12, 2026 05:40
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.

1 participant