fix(shared): inverted terminal ranges emit no fake lines#27843

merged1
main
GitHub fork: fix/terminal-context-inverted-range
opened13d agobySsamkelly880
View on GitHubTwo-way sync enabledPR last imported 11d ago

Problem

A terminal context record with lineEnd < lineStart computed slice(0, negative), which takes from the end of the buffer and invents line numbers for the agent payload.

Change

Clamp the line count with Math.max(0, …) so an inverted range emits the terminal label and no lines.

Scope and approval

Obvious shared-util boundary bug. Only affects malformed or swapped terminal capture ranges.

Verification

  • Regression test: inverted lineStart/lineEnd emits no | lines.
  • vp test run packages/shared/src/composerContextReferences.test.ts — 33 passed.

Attribution

Written by grok-4.5 in T3 Code (Grok harness).

Checks

25 successful · 15 skipped
All checks passed
4a2b895

Added 1 commit

  • fix(shared): inverted terminal ranges emit no fake lines4a2b895
Synced from GitHub

Approvability

Verdict: Approved at 4a2b895

Macroscope's review found this PR approvable — Inverted terminal ranges now emit no numbered context lines, while valid ranges retain their existing formatting. The change is narrowly scoped and introduces no material operational or product-policy risk.

You can add or adjust custom eligibility rules. Learn more.

Synced from GitHub

Review in Change Stack →

No actionable comments were generated in the recent review.

External image Recent review info
External image Run configuration
External image Commits

Reviewing files that changed from the base of the PR and between c6ef3b46ebed34fbefa1706674fe5748254cee18 and 4a2b895bcfb217e49f19dd3b20f5c3cc636958f2.

External image Files selected for processing: 2
  • packages/shared/src/composerContextReferences.test.ts
  • packages/shared/src/composerContextReferences.ts

Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 7 remain after this review.

Walkthrough

Terminal context formatting now emits no text lines when lineEnd is before lineStart. A provider-projection test verifies that the context envelope and terminal label remain.

Changes: Terminal context formatting

Layer / File(s)Summary
Clamp terminal lines and test projection
packages/shared/src/composerContextReferences.ts, packages/shared/src/composerContextReferences.test.ts
The formatter clamps the line count to zero or greater. The test checks that an inverted range produces no numbered terminal lines while retaining the context envelope and terminal label.

Estimated code review effort: 2 (Simple) | ~10 minutes

Change: Bug fix

Possibly related issues

  • Issue 27844 reports the same inverted terminal range behavior. This change clamps the line count to zero.

Suggested labels: bugfix, size:XS

Suggested reviewers: maria-rcks

Merge Risk: External image Minimal · up to 4a2b8

The change is mergeable with normal checks.

Pre-merge checks: Passed 4
Check nameStatusExplanation
Title checkPassedThe title names the main change: inverted terminal ranges (lineEnd < lineStart) no longer emit fake lines. It follows the conventional commit form and matches the raw summary and objectives, which des…
Description checkPassedThe description follows the template's Problem, Change, Scope and approval, and Verification sections. The Problem and Change sections clearly state the bug and the clamp fix. Scope is stated as an ob…
Linked Issues checkPassedCheck skipped because no linked issues were found for this pull request.
Out of Scope Changes checkPassedCheck skipped because no linked issues were found for this pull request.
Full details: Title check

Explanation

The title names the main change: inverted terminal ranges (lineEnd < lineStart) no longer emit fake lines. It follows the conventional commit form and matches the raw summary and objectives, which describe clamping the line count to zero.

Full details: Description check

Explanation

The description follows the template's Problem, Change, Scope and approval, and Verification sections. The Problem and Change sections clearly state the bug and the clamp fix. Scope is stated as an obvious shared-util boundary bug, which fits the template's exemption for very small, focused fixes of obvious bugs, though it does not link an issue. Verification lists a regression test and a passing test command with a count. The Attribution section follows the template's agent note. The template's leading comment asks for a conventional commit title, which the author provided. The template asks for explicit links to triage or approval only for larger changes, and this one is small and focused, so the description is mostly complete.

Finishing Touches
🧪 Generate unit tests (beta)
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

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

Note

Written by gpt-6.1-sol on behalf of Maria

Verified in a real dev environment

checked exact head 4a2b895bcfb217e49f19dd3b20f5c3cc636958f2 against github/main at 955e2bfbb5 on linux. installed dependencies in both checkouts and ran one background dev server at a time. created the project and thread through the web app, generated real terminal output, and sent real Codex gpt-6.1-sol turns through the local proxy. browser automation used the repo's Playwright and /usr/bin/google-chrome.

  • backward terminal selection, add to chat, and submit: both revisions sent exactly lines 3-8 and all six selected values. confirmed the outgoing Codex turn/start payload and rendered response.
  • single-line selection on the pr: sent exactly 5 | CTX_CHARLIE.
  • malformed client-boundary probe: changed only the captured range to 10-5 in the outgoing websocket request. both revisions discarded the malformed record and sent unavailable="true" with no numbered lines. this probe deliberately bypassed normal selection normalization; it was not a natural UI reproduction.
  • direct formatter reproduction: main emitted fake lines 10 and 11 from six text lines with range 10-5. this head emitted only terminal: Terminal 1. this helper call bypasses the server contract.
  • blacksmith-testbox: vp test run packages/shared/src/composerContextReferences.test.ts packages/contracts/src/composerContext.test.ts, 41 tests passed. existing head CI reports 33 successful checks. no source changes or pushes.

short transcript with extracted real provider inputs and direct formatter output

backward terminal selection through add to chat and real Codex response

single-line context response and unavailable malformed range on the pr head

Not verified

a user-visible before/after for the inverted-range fix could not be reproduced. both revisions already normalize terminal selections in ThreadTerminalDrawer.tsx and reject inverted records in TerminalContextRecord; forward-compatible decoding drops those records before provider projection. the changed formatter branch is therefore unreachable through the current client/server path tested here. this verifies defensive helper behavior and nearby integration, not an end-to-end reproduction of the original bug.

native desktop, ios/android, other providers, and macos/windows were not exercised on this linux vm.