Skip to content

fix(kiro): report context pressure for compaction - #439

Closed
coseung2 wants to merge 8 commits into
lidge-jun:devfrom
coseung2:fix/kiro-context-usage-reporting
Closed

fix(kiro): report context pressure for compaction#439
coseung2 wants to merge 8 commits into
lidge-jun:devfrom
coseung2:fix/kiro-context-usage-reporting

Conversation

@coseung2

@coseung2 coseung2 commented Jul 25, 2026

Copy link
Copy Markdown
Contributor

Summary - preserve Kiro current-turn provider usage and cache fields internally - expose active context pressure as an explicit absolute contextTotalTokens checkpoint - serialize Responses usage without double-counting output - propagate rebuilt context estimates through the bounded completion retry - keep Kiro request-log rows estimated and based on the full request context ## Problem Kiro can report current-turn token usage separately from whole-conversation occupancy. OCX also reconstructs the complete Codex history locally, but the Responses usage sent back to Codex previously contained only the current-turn estimate. A conversation with roughly 226k active tokens could therefore report only a few dozen input tokens to Codex, preventing automatic compaction until Kiro rejected the request as over its context limit. ## Approach Kiro now keeps two distinct concepts: - inputTokens / outputTokens: provider per-attempt usage, including any reported cache split - contextTotalTokens: absolute active-context size after the response The absolute checkpoint is the maximum of: - the normalized Kiro payload estimate, including conservative image cost, plus response output - authoritative provider input plus output usage - contextUsagePercentage decoded against the native Kiro model window, including the concrete response model selected by kiro-auto Responses serialization uses: input_tokens = max(0, contextTotalTokens - output_tokens) output_tokens = output_tokens total_tokens = contextTotalTokens This prevents output from being added twice while allowing Codex latest last_token_usage.total_tokens value to reflect active context pressure. Cache-read and cache-write detail fields are clamped to remain subsets of the derived absolute input. Bounded retries sum per-attempt usage, preserve definite growth from visible text carried into the retry, and retain the maximum absolute checkpoint without re-adding dropped private reasoning. Kiro request logs intentionally remain estimated. Existing request-log behavior floors input usage at the adapter full-request estimate; the bridged absolute checkpoint is likewise persisted as estimated request usage. Cache values remain absent when Kiro does not provide them. ## Verification - bun run typecheck - bun test tests/kiro-stream.test.ts tests/bridge.test.ts tests/request-log.test.ts — 141 pass, 0 fail - regressions cover Kiro GPT token ratios, authoritative usage floors, native-window and auto-route percentage decoding, normalized image estimates, cache-detail clamping, fallback context growth, merged usage identity, nonzero-output percentages, consecutive absolute checkpoints, bounded-retry estimate propagation, normalized-payload reasoning exclusion, and deferred Kiro request logging - the Windows full-suite process did not terminate after more than five minutes; no failure summary was produced, so cross-platform CI remains the full-suite authority ## Summary by CodeRabbit * Bug Fixes * Improved token usage reporting for Kiro requests, including absolute context totals and output token accounting. * Prevented token totals from accumulating incorrectly across streaming updates or retries. * Improved cache-related usage details and image token estimation. * Preserved accurate usage information in Responses output and deferred request logs. * Tests * Added coverage for context growth, fallback responses, streaming checkpoints, caching, images, and request-log consistency.

@github-actions github-actions Bot added the bug Something isn't working label Jul 25, 2026
@github-actions

github-actions Bot commented Jul 25, 2026

Copy link
Copy Markdown

Target branch corrected

This pull request now targets dev.

The [WRONG BRANCH] title prefix has been removed. The pull request has been marked ready for review again.

@github-actions github-actions Bot changed the title fix(kiro): report context pressure for compaction [WRONG BRANCH] fix(kiro): report context pressure for compaction Jul 25, 2026
@coderabbitai

coderabbitai Bot commented Jul 25, 2026

Copy link
Copy Markdown

Review Change Stack

Note

Reviews paused

It looks like this branch is under active development. To avoid overwhelming you with review comments due to an influx of new commits, CodeRabbit has automatically paused this review. You can configure this behavior by changing the reviews.auto_review.auto_pause_after_reviewed_commits setting.

Use the following commands to manage reviews:

  • @coderabbitai resume to resume automatic reviews.
  • @coderabbitai review to trigger a single review.

Use the checkboxes below for quick actions:

  • ▶️ Resume reviews
  • 🔍 Trigger review
📝 Walkthrough

Walkthrough

Kiro now estimates and propagates absolute context totals through streaming and bounded fallback parsing. The Responses bridge serializes those totals without double-counting output tokens, with coverage for metadata, history, SSE output, and request logging.

Changes

Context total token accounting

Layer / File(s) Summary
Kiro context propagation
src/adapters/kiro.ts
Kiro estimates request context, carries it through initial and bounded fallback attempts, derives totals from estimates and metadata, and merges checkpoints using the maximum.
Usage contract and Responses serialization
src/types.ts, src/bridge.ts
OcxUsage adds contextTotalTokens; Responses usage derives input and total tokens from the absolute context checkpoint and bounds cache details to the derived input.
Usage regression coverage
tests/kiro-stream.test.ts, tests/bridge.test.ts, tests/request-log.test.ts
Tests validate fallback and provider-derived totals, history and image sensitivity, absolute checkpoint handling, SSE serialization, and deferred request-log usage.

Estimated code review effort: 3 (Moderate) | ~25 minutes

Sequence Diagram(s)

sequenceDiagram
  participant Request as Kiro request
  participant Adapter as createKiroAdapter
  participant Parser as parseKiroStream
  participant Attempt as parseKiroAttempt
  participant Bridge as responsesUsage
  participant Log as RequestLogEntry

  Request->>Adapter: Build request and estimate context input
  Adapter->>Parser: Pass contextInputEstimate
  Parser->>Attempt: Parse initial stream attempt
  Parser->>Attempt: Parse bounded fallback attempt
  Attempt-->>Parser: Return contextTotalTokens
  Parser-->>Bridge: Provide merged usage
  Bridge-->>Request: Emit input_tokens and total_tokens
  Bridge-->>Log: Preserve absolute context checkpoint
Loading

Possibly related PRs

Suggested labels: bug

Suggested reviewers: ingwannu, lidge-jun

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly summarizes the main change: Kiro now reports context pressure to enable compaction.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Comment @coderabbitai help to get the list of available commands.

@coseung2
coseung2 changed the base branch from main to dev July 25, 2026 05:16

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
tests/kiro-stream.test.ts (1)

827-841: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick win

Add a bounded-retry context-pressure regression test.

These tests cover metadata, percentage floors, history, and request-log estimates, but not the fallback request whose context grows after assistant continuation. Add a test that forces the retry and verifies that per-turn usage is summed while contextInputTokens is the maximum across both attempts.

As per path instructions, behavior changes in src/ should have a focused regression test near the existing subsystem tests.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@tests/kiro-stream.test.ts` around lines 827 - 841, Add a focused test near
the existing Kiro stream usage tests that triggers the bounded retry after
assistant continuation, then assert per-turn input/output usage is summed across
both attempts while contextInputTokens equals the maximum context usage
observed. Use the existing adapter, request-building, event-frame, and doneUsage
helpers, and preserve the established estimated-usage assertions where
applicable.

Source: Path instructions

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@src/adapters/kiro.ts`:
- Line 1062: Update the fallback result and second parse flow so the retry’s
computed contextInputTokens is returned from the fallback and passed to the
retry parse instead of the original estimate. Adjust the relevant fallback
result shape and the parse call near contextInputTokens, preserving the existing
retry estimate calculation and ensuring mergeKiroUsage receives the larger retry
context pressure.

---

Outside diff comments:
In `@tests/kiro-stream.test.ts`:
- Around line 827-841: Add a focused test near the existing Kiro stream usage
tests that triggers the bounded retry after assistant continuation, then assert
per-turn input/output usage is summed across both attempts while
contextInputTokens equals the maximum context usage observed. Use the existing
adapter, request-building, event-frame, and doneUsage helpers, and preserve the
established estimated-usage assertions where applicable.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: ASSERTIVE

Plan: Pro Plus

Run ID: 57ab3ac0-50d7-4287-81d9-a57a803927f6

📥 Commits

Reviewing files that changed from the base of the PR and between 357acee and 46e3745.

📒 Files selected for processing (5)
  • src/adapters/kiro.ts
  • src/bridge.ts
  • src/types.ts
  • tests/bridge.test.ts
  • tests/kiro-stream.test.ts

Comment thread src/adapters/kiro.ts Outdated
@github-actions github-actions Bot changed the title [WRONG BRANCH] fix(kiro): report context pressure for compaction fix(kiro): report context pressure for compaction Jul 25, 2026

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 46e3745556

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread src/bridge.ts
Comment thread src/bridge.ts Outdated
Comment thread src/adapters/kiro.ts Outdated

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@tests/request-log.test.ts`:
- Around line 755-782: Update responseWithDeferredRequestLog and its deferred
persistence path to carry raw OcxUsage separately from the Responses compaction
projection. Keep the SSE input_tokens value at 49,900, but persist and bill the
upstream current-turn inputTokens value of 58 instead of the projected
context-pressure value.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: ASSERTIVE

Plan: Pro Plus

Run ID: d2d4eec0-5ead-42ca-ab60-7b32cea2245e

📥 Commits

Reviewing files that changed from the base of the PR and between 46e3745 and 0c0b4f8.

📒 Files selected for processing (6)
  • src/adapters/kiro.ts
  • src/bridge.ts
  • src/types.ts
  • tests/bridge.test.ts
  • tests/kiro-stream.test.ts
  • tests/request-log.test.ts

Comment thread tests/request-log.test.ts

Copy link
Copy Markdown
Contributor Author

@codex review

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 0c0b4f89da

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread src/adapters/kiro.ts Outdated
Comment thread src/adapters/kiro.ts Outdated
Comment thread src/bridge.ts

Copy link
Copy Markdown
Contributor Author

@codex review

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: aec4df8339

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread src/adapters/kiro.ts Outdated
Comment thread src/adapters/kiro.ts Outdated
Comment thread src/adapters/kiro.ts
Comment thread src/adapters/kiro.ts Outdated
@coseung2

Copy link
Copy Markdown
Contributor Author

@codex review

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: f44da01a95

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread src/adapters/kiro.ts Outdated
Comment thread src/adapters/kiro.ts
@coseung2

Copy link
Copy Markdown
Contributor Author

@codex review

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 8f1dd35c87

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread src/adapters/kiro.ts
@coseung2

Copy link
Copy Markdown
Contributor Author

@codex review

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@src/adapters/kiro.ts`:
- Around line 1145-1162: Preserve checkpoint growth for reasoning-only fallback
attempts by deriving one carried-growth flag from firstResult.assistantText or
firstResult.sawReasoning, then passing it to all three mergeKiroUsage calls in
the fallback handling of src/adapters/kiro.ts lines 1145-1162. Add an upstream
context-percentage frame and assert the exact checkpoint equals 50,000 plus
finalOutputTokens in tests/kiro-stream.test.ts lines 391-407.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: ASSERTIVE

Plan: Pro Plus

Run ID: a3b03513-3dc8-4e2b-b707-f8e1d34d0e28

📥 Commits

Reviewing files that changed from the base of the PR and between f44da01 and 7e21e64.

📒 Files selected for processing (2)
  • src/adapters/kiro.ts
  • tests/kiro-stream.test.ts

Comment thread src/adapters/kiro.ts

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 7e21e64016

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread src/adapters/kiro.ts
@coseung2

Copy link
Copy Markdown
Contributor Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Nice work!

Reviewed commit: e78e84636b

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@lidge-jun

Copy link
Copy Markdown
Owner

🔒 Maintainer integration in progress — please hold off on merging

@lidge-jun (maintainer) is actively integrating this PR into dev as part of a coordinated
integration pass. Please do not merge, rebase, force-push, or close this PR until this
marker is removed.

What is happening

This PR is being integrated on the maintainer branch codex/260725-pr-rework, which is
based on dev. The pass applies the contributed change, adds any missing regression
coverage, and repairs review-identified defects before a single verified merge into dev.
Every integrated change goes through bun run typecheck, the full bun run test suite,
bun run privacy:scan, and bun run lint:gui, followed by exact-SHA hosted
Cross-platform CI and Service lifecycle runs.

What this means for you

  • Your authorship and commits are preserved. Nothing is being rewritten under your name.
  • No action is needed from you right now. If a defect repair changes your intended
    behavior, it will be described explicitly in a follow-up comment before the merge.
  • If you have work in flight on this branch, please comment here instead of pushing, so we
    do not race each other.

This PR will be closed with a merge receipt (integration commit SHA, verification output,
and hosted CI links) once the pass lands on dev. Thanks for the contribution.

Integration tracker: devlog/_plan/260725_pr_issue_rework · marker posted by the maintainer integration pass

lidge-jun added a commit that referenced this pull request Jul 25, 2026
PR #439 (head e78e846, author coseung2)를 통합하고 활성화 공백을 메우는
회귀 2건을 추가한다.

Kiro는 attempt별 usage만 보고해서 클라이언트가 실제 컨텍스트 점유를 알 수 없었고
compaction 시점을 판단할 수 없었다. 이제 payload estimate, upstream 사용률,
authoritative turn usage 중 최댓값을 절대 checkpoint로 삼아 streaming, fallback,
bridge 직렬화, cache-detail clamping, deferred logging 전 경로로 전달한다.

추가한 회귀 2건:
- 첫 attempt progress가 클수록 rebuilt fallback checkpoint도 커진다 (기존
  assertion은 mergeKiroUsage의 floor만으로 만족되어 stale estimate 퇴행을
  잡지 못했다)
- non-stream parseResponse가 buildRequest로 설치된 contextInputEstimate
  closure를 실제로 활성화하고 절대 checkpoint를 전달한다

Co-authored-by: coseung2 <coseung2@users.noreply.github.com>
@lidge-jun

Copy link
Copy Markdown
Owner

✅ Integrated into dev

Thank you for this fix. It has been integrated into dev as part of a verified integration pass.

Integration commit: fc517004fix(kiro): report context pressure for compaction
Merged into dev at: ebc62d1f
Your authorship is preserved via Co-authored-by.

What was added on top

The implementation went in unchanged. Two regressions were added because the existing tests could not actually prove the branches they targeted:

  • the fallback test was satisfied by mergeKiroUsage()'s first.contextTotalTokens + second.outputTokens floor alone, so regressing fallback.contextInputEstimate back to the stale initial estimate would still have passed. The new test compares two runs that differ only in first-attempt progress size and asserts the rebuilt checkpoint grows with it.
  • the non-streaming parseResponse test never called buildRequest(), so the contextInputEstimate closure was never installed. The new test builds a long-history request first and asserts the terminal usage carries the absolute checkpoint.

Verification

  • bun test tests/kiro-stream.test.ts tests/bridge.test.ts tests/request-log.test.ts: 143 pass / 0 fail (128 before this change)
  • Full suite at merge: 4222 pass / 0 fail
  • bun run typecheck, bun run privacy:scan, bun run lint:gui: all clean

Note for your other PR: #447 touches the same createKiroAdapter region in src/adapters/kiro.ts and src/types.ts, so it will need a rebase onto current dev. Thanks for separating per-attempt usage from absolute occupancy — the max-merge on checkpoints is the right call.

@lidge-jun

Copy link
Copy Markdown
Owner

Closing: integrated into dev at ebc62d1f with authorship preserved. See the receipt comment above for the integration commit, the changes made on top, and the verification evidence.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants