Skip to content

test(runtime): enforce ANSI preview-final pre-filter invariant - #27376

Merged
nwparker merged 1 commit into
mainfrom
nwparker/perf-ansi-final-invariant
Oct 11, 2026
Merged

nwparker merged 1 commit into
mainfrom
nwparker/perf-ansi-final-invariant

Conversation

@nwparker

@nwparker nwparker commented Oct 11, 2026 •

Copy link
Copy Markdown
Contributor
Files Added Deleted Net
Test 1 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​37 $\color{#cf222e}{\Huge{\mathbf{−}}}$​26 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​11
Prod 1 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​7 $\color{#cf222e}{\Huge{\mathbf{−}}}$​3 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​4

ELI5

Follow-up to #27344, which sped up how Orca cleans terminal output for previews. That change was correct, but one of its shortcuts depended on two lists staying in sync by hand, and one test counted internal steps instead of checking results. This makes the shortcut safe by construction and makes the tests check what users actually see.

What Changed

No user-visible change; terminal previews keep exactly the same text.

  • normalizeTerminalChunk only parses a CSI escape when isTerminalPreviewLineControlFinal(final) is true. Before, isTerminalPreviewLineControl decided retention on its own, so a final byte added there but not to the pre-filter would have been silently dropped. Now isTerminalPreviewLineControl returns false for any final the pre-filter rejects, so isTerminalPreviewLineControlFinal is the single source of truth for which finals can be kept.
  • New test walks every CSI final byte 0x40–0x7e (with and without a parameter) through normalizeTerminalChunk and asserts exactly `A C D G K `` are retained and everything else is stripped. Removing a final from the pre-filter fails this test (checked by mutation).
  • terminal-vertical-control-scan.test.ts no longer spies on the global String.prototype.charCodeAt to count inspections, which tied the test to how the code is written. The same inputs are still asserted for output; the separate equivalence and fuzz work from perf(runtime): reduce terminal ANSI scanning work #27344 covers correctness.

Why

A structural gate is cheaper to keep correct than a comment asking people to update two functions, and the exhaustive final-byte test pins the retained set so any future change is deliberate. Removing the spy keeps the tests stable under refactors that do not change behavior.

Linked Issue

Follow-up to #27344 (part of #27023).

Visual Proof

N/A — no UI or output change; the pre-filter and predicate accept the same finals as before.

Testing

  • macOS: terminal-vertical-control-scan, terminal-ansi-pending-retention, all terminal-tail*, terminal-command-paint and orca-runtime-on-pty-data-redraw suites pass (12 files / 115 tests).

  • Mutation check: removing G from isTerminalPreviewLineControlFinal makes the new final-byte test fail.

  • oxlint and oxfmt clean on changed files.

  • I manually tested these changes locally

  • Automated tests added/updated, or explained why not below

AI Disclosure

Review

Check that the reordered guard in isTerminalPreviewLineControl returns the same result for every input (the final check moved first; K mode handling is unchanged).

Agent skill upstream boundary

  • Not applicable, or this change copies or mechanically translates no upstream skill-installer source, tests, fixtures, registry entries, path tables, comments, or documentation.

Notes

Pure-function change; platform-independent and applies equally to local, SSH and folder-workspace terminals. No wire, state or allocation changes.

Checklist

  • This PR is small and focused
  • I explained what changed and why (ELI5, the user-facing before/after, the mechanism, and why over the alternatives)
  • Before/after screenshots or videos attached for UI changes, or N/A with reason
  • Self-reviewed for correctness, security, and performance
  • Cross-platform, SSH/remote, and path/shortcut impact considered (or N/A)
  • pnpm lint, pnpm typecheck, pnpm test, and pnpm build pass (or CI will cover; local preferred)

Follow-up to #27344. isTerminalPreviewLineControl now gates on
isTerminalPreviewLineControlFinal, so the normalizeTerminalChunk
pre-filter cannot drop a final the predicate accepts. Add a test over
every CSI final 0x40-0x7e and replace String.prototype.charCodeAt spies
with behavior assertions.
@coderabbitai

coderabbitai Bot commented Oct 11, 2026 •

Copy link
Copy Markdown
Contributor

Review in Change Stack →

📝 Walkthrough

Walkthrough

The CSI control predicate now rejects unsupported finals before validating canonical numeric parameters. The accepted finals and K mode limits remain unchanged. Tests now check normalized output and expected detection results instead of counting charCodeAt calls.


Priority: ⬇️ Low

Merge Risk: 🔵 Low · up to d9366

Terminal detection still returns the expected results, but a future broad scan of large output could pass these tests unnoticed. Restoring the small inspection bound keeps that performance regression detectable.

Pre-merge checks | Passed 4 | Failed 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage Warning Docstring coverage is 33.33% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 3 functions across 2 files. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check Passed The title clearly identifies the test change and the ANSI preview-final invariant it enforces.
Description check Passed The description is complete and focused. It explains the change, purpose, testing, visual impact, compatibility considerations, and checklist status. It references issue #27344 but does not provide an…
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.

  • Fix all pre-merge checks with AI
✨ Finishing Touches 💡 1
📝 Generate docstrings 💡
  • Commit to this branch
  • Create a new PR

  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

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

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🧹 Nitpick comments (1)
src/main/runtime/terminal-vertical-control-scan.test.ts (1)

56-62: 🚀 Performance & Scalability | 🔵 Trivial | ⚡ Quick win

Restore the code-unit bound for the large-input detector test.

The boolean assertions do not detect broader scanning when output remains unchanged. No other runtime test bounds charCodeAt calls for containsTerminalVerticalLineControl. Restore the previous bound in this test.

Suggested fix
-import { describe, expect, it } from 'vitest'
+import { describe, expect, it, vi } from 'vitest'
...
-  ] as const)('detects vertical controls in %s output', (_name, input, expected) => {
-    expect(containsTerminalVerticalLineControl(input)).toBe(expected)
+  ] as const)('bounds code-unit inspections on %s output', (_name, input, expected) => {
+    const charCodeAt = vi.spyOn(String.prototype, 'charCodeAt')
+    let actual: boolean
+    let inspections: number
+    try {
+      actual = containsTerminalVerticalLineControl(input)
+      inspections = charCodeAt.mock.calls.length
+    } finally {
+      charCodeAt.mockRestore()
+    }
+
+    expect(actual).toBe(expected)
+    expect(inspections).toBeLessThan(16)
   })

ℹ️ Review info
⚙️ Run configuration
  • Configuration used: Repository UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 1450071a-2b18-4a79-b0ea-c72405b97689
📥 Commits

Reviewing files that changed from the base of the PR and between 41de89a and d93666f.

📒 Files selected for processing (2)
  • src/main/runtime/terminal-ansi-normalization.ts
  • src/main/runtime/terminal-vertical-control-scan.test.ts

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

@nwparker
nwparker merged commit dae2cc9 into main Oct 11, 2026
29 checks passed
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