Repository navigation
perf(runtime): remove timer clamps from cooperative yields - #10908
Conversation
📝 WalkthroughWalkthroughAdds a shared 🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✨ Finishing Touches📝 Generate docstrings
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. Comment |
There was a problem hiding this comment.
🧹 Nitpick comments (1)
src/main/opencode-usage/scanner.ts (1)
147-149: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick winUse the shared
yieldToEventLoophelper here.This migration only changes the local implementation to
setImmediate, so the scanner remains outside the shared runtime/fake-timer contract. Import../../shared/event-loop-yieldand remove this local helper to keep all cooperative yields consistent.
ℹ️ Review info
⚙️ Run configuration
Configuration used: Repository UI
Review profile: CHILL
Plan: Pro Plus
Run ID: 00052801-dc06-4273-8828-55338df81924
📒 Files selected for processing (15)
src/main/browser/browser-text-insertion.tssrc/main/opencode-usage/scanner.tssrc/renderer/src/components/dictation/dictation-insertion-target.tssrc/renderer/src/components/editor/monaco-large-text-paste.tssrc/renderer/src/components/editor/rich-markdown-large-text-paste.tssrc/renderer/src/components/terminal-pane/pty-input-write-queue.tssrc/renderer/src/components/terminal-pane/pty-transport.tssrc/renderer/src/components/terminal-pane/terminal-paste-executor.tssrc/renderer/src/lib/agent-draft-paste-content.tssrc/renderer/src/lib/paste-payload-metadata.tssrc/renderer/src/lib/primary-selection-paste.tssrc/renderer/src/lib/text-control-paste.tssrc/shared/clipboard-text.tssrc/shared/event-loop-yield.test.tssrc/shared/event-loop-yield.ts
Renderer paste and input loops can schedule more than a thousand zero-delay timer yields for a maximum-size payload. Chromium clamps nested timers to 4ms, adding seconds of idle wall time. Use MessageChannel tasks in renderer runtimes and setImmediate in Node while retaining a timer fallback for tests and unsupported environments.
Adversarial review found that concurrent producers could retain resolved callbacks until global quiescence. Route renderer yields by token and delete each resolver before resuming its producer. Keep timer pacing in terminal paste and accepted-write loops where SSH and local PTYs do not provide drain acknowledgement. Use the shared scheduler for the OpenCode scanner.
1148cf5 to
239d89b
Compare
…10908) * perf(runtime): remove timer clamps from cooperative yields Renderer paste and input loops can schedule more than a thousand zero-delay timer yields for a maximum-size payload. Chromium clamps nested timers to 4ms, adding seconds of idle wall time. Use MessageChannel tasks in renderer runtimes and setImmediate in Node while retaining a timer fallback for tests and unsupported environments. * fix(runtime): preserve pacing and release yield callbacks Adversarial review found that concurrent producers could retain resolved callbacks until global quiescence. Route renderer yields by token and delete each resolver before resuming its producer. Keep timer pacing in terminal paste and accepted-write loops where SSH and local PTYs do not provide drain acknowledgement. Use the shared scheduler for the OpenCode scanner.
Summary
Follow-up audit to #10892 found the same timer-clamp issue in 11 cooperative-yield implementations across clipboard validation, large editor pastes, terminal input queues, browser text insertion, and the OpenCode usage scanner.
setImmediatein Node andMessageChannelin renderer/web runtimes, with a timer fallback for Vitest and unsupported environments.setTimeout(0)and 1.7 ms forMessageChannel: about 4.57 seconds of artificial wait removed at the limit. With 0.1 ms of simulated work per batch, concurrent timer latency improved slightly (5.1 ms to 4.4 ms), so the faster path does not achieve its gain by starving other timer work.Screenshots
No visual change.
Testing
pnpm lint— changed-file lint, switch exhaustiveness, reliability gates, and max-lines ratchet pass; the full command fails only on six pre-existing unlocalizedGhosttysearch keywords in unrelated settings files.pnpm typecheckpnpm test— focused post-rebase suite run instead: 14 files, 205 tests passing.pnpm buildFocused coverage includes the shared scheduler, clipboard bounds, browser insertion, OpenCode scanning, text controls, primary selection, dictation, Monaco, rich Markdown, terminal paste planning, PTY input queueing, and PTY transport.
AI Review Report
Three independent adversarial agents reviewed scheduler correctness, all migrated call sites, performance claims, tests, security boundaries, and cross-platform behavior. They found and verified fixes for two material issues:
Mapentries deleted before resolution, with a two-producer replenishment regression test.MessageChannel, where existing code explicitly identifies the nested timer clamp as harmful.CodeRabbit's consistency nit was also addressed by routing OpenCode scanning through the shared scheduler. All agents re-reviewed the fixes and reported no remaining findings.
Cross-platform review covered macOS, Linux, Windows, WSL, SSH, remote-runtime, and folder-workspace behavior. The scheduling APIs are runtime-based rather than OS-based, no shortcuts/labels/paths/shell commands changed, and existing remote acknowledgements plus intentional PTY pacing remain intact.
Security Audit
No dependencies, auth, secrets, commands, or path handling changed. Existing input size limits, control-sequence sanitization, target ownership checks, IPC chunk bounds, PTY backpressure/pacing, timeouts, and bracketed-paste cleanup remain in place. The change only replaces the scheduling mechanism between existing bounded operations where timer duration has no behavioral role.
Notes
The full-lint localization failure is unrelated to this diff and is reproducible on
main. Intentional zero-delay timer sites remain in place after the adversarial audit.ELI5
Some zero-delay timers still wait about four milliseconds in browsers. Large operations that pause hundreds of times therefore spend seconds waiting for clocks, not doing work. The shared yield uses the event loop mechanism designed for immediate cooperation, while the few PTY sites where delay is intentional keep their timers.
Proof of performance improvement
A maximum 16 MiB editor payload performs 1,023 between-chunk yields. In three headless Chromium rounds, the median scheduler time fell from 4,567.2 ms with
setTimeout(0)to 1.7 ms withMessageChannel. With 0.1 ms of simulated work per batch, concurrent timer latency improved from 5.1 ms to 4.4 ms, so the gain did not come from starving timer work.Proof of zero regression
Only true cooperative yields moved. Adversarial review restored terminal paste and accepted-write timers because those sites use delay as pacing; bounded clipboard, paste, queue, sanitizer, timeout, and remote acknowledgement behavior remains unchanged. Evidence: 14 focused files with 205 tests, full typecheck, green PR verify, packaged Windows crash-survival, Windows terminal-restart E2E, and explicit macOS, Linux, Windows, WSL, SSH, remote-runtime, and folder-workspace review.
Review loop count + verdict
2 loops with 3 independent adversarial reviewers. Round 1 found a retained-callback queue and two incorrectly migrated pacing sites. Round 2 verified the token-keyed callback cleanup, restored PTY timers, shared OpenCode scheduler use, and reported no remaining findings.
Verdict: ship.