Skip to content

perf(daemon): send PTY output to main as raw bytes, not NDJSON - #27006

Merged
nwparker merged 2 commits into
stablyai:mainfrom
outof-place:perf/daemon-binary-pty-frames
Oct 10, 2026
Merged

nwparker merged 2 commits into
stablyai:mainfrom
outof-place:perf/daemon-binary-pty-frames

Conversation

@outof-place

@outof-place outof-place commented Oct 9, 2026 •

Copy link
Copy Markdown
Contributor

ELI5

Busy terminals spend less time encoding and decoding their output between Orca and its terminal daemon.

What Changed

Terminal data uses a length-prefixed byte stream only when both peers negotiate support. Older peers keep their existing JSON stream. The reader pauses during the hello handoff, installs the next reader with the exact remaining bytes, and resumes without dropping buffered output. Valid Unicode uses raw UTF-8; malformed UTF-16 uses the existing JSON event frame so the original string survives. Frame splitting preserves the existing sequence and size rules.

Why

This removes repeated escaping and parsing from the busiest path. Capability negotiation preserves independently updated peers. Existing event frames handle exceptional strings without another protocol feature, and normal writes encode metadata once.

Linked Issue

Fixes #27003

Visual Proof

N/A — backend performance changes; no layout or controls change.

Performance measurements

A codec microbenchmark compared NDJSON, the original PR (80d2cc7), and the corrected implementation (b1442e4) on macOS arm64 with Node 24.20.0. Each iteration ran the actual writer directly into the actual reader, including NDJSON UTF-8 encoding and StringDecoder, and checked decoded character counts. Writes included sequence metadata.

The table shows median elapsed time per write across nine interleaved rounds, with GC before each timed sample. Small cases used 50,000 writes per round; bulk used 128.

Output NDJSON baseline (µs/write) Original PR (µs/write) Corrected (µs/write)
Single ASCII character 3.584 4.777 3.334
Short log, 13 UTF-16 units 1.264 1.365 0.937
Short colored line, 12 UTF-16 units 2.059 1.543 1.044
Colored line repeated 4,096 times, 49,152 UTF-16 units 1,460.884 140.956 131.110

The corrected writer encodes fitting frames once and calculates splitting budgets only for oversized frames. A deterministic regression test verifies 100 small writes require 100 metadata serializations; the original path required 200.

The shared host was busy, so absolute timings varied. These measurements cover codec work, excluding real sockets, PTY production, batching delays, IPC, and rendering. They do not establish application throughput or interactive latency.

Testing

Final focused run: 114 tests across 10 files, including real socket negotiation, handoff, Unicode, byte limits, stream replacement, and backpressure. Wider compatibility tests cover older daemon versions. Independent fuzzing covered 1,200 payloads and 41,524 frames. Real Linux and native Windows sockets reproduce the original output loss and pass the correction. Node typecheck and changed-code quality pass; the combined branch also passes all typechecks, the production build, and focused tests.

  • I manually tested these changes locally
  • Automated tests added/updated, or explained why not below

AI Disclosure

Implemented with Claude Opus 5.5 via Claude Code: design, code, tests and measurements. A human (outof-place) directed the work.

Review

The review corrected output loss between readers, malformed Unicode replacement, quadratic hello buffering, and duplicate metadata encoding. Start with the hello handoff and binary frame writer.

Agent skill upstream boundary

  • Not applicable: no upstream skill-installer material copied.

Notes

No negative user-facing trade-off identified in the corrected code. Authentication and the 16 MiB bound remain. Remote hosts negotiate between their own daemon and client; consumer event shapes remain compatible. Full packaged cross-platform journeys still require CI. ORCA_DAEMON_BINARY_STREAM=0 preserves the older transport.

Checklist

  • Scope is focused on the performance change and necessary correctness repairs
  • Before/after, mechanism, and alternatives explained
  • Visual proof is N/A with reason
  • Adversarial correctness, security, and performance review completed
  • Cross-platform, SSH/remote, and compatibility impact considered
  • Full CI passes; local focused tests, typechecks, quality, and combined production build pass

The stream socket's hello can now negotiate binary-v1 framing:
length-prefixed frames carrying PTY data as raw UTF-8 next to a small
JSON header, so neither the daemon nor main escapes or parses the
payload. Older daemons ignore the hello field and stay on NDJSON; older
mains never ask. The hello reader now hands the stream reader the exact
bytes that followed its line, which an event sharing the hello's read
used to lose. ORCA_DAEMON_BINARY_STREAM=0 keeps NDJSON.

Fixes #27003

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@coderabbitai

coderabbitai Bot commented Oct 9, 2026 •

Copy link
Copy Markdown
Contributor

Review in Change Stack →

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration
  • Configuration used: Repository UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 620a58b2-13cb-4a9e-82af-61010d4260f7

📥 Commits

Reviewing files that changed from the base of the PR and between 80d2cc7 and b1442e4.


📒 Files selected for processing (9)
  • src/main/daemon/client.ts
  • src/main/daemon/daemon-client-hello-handshake.ts
  • src/main/daemon/daemon-client-ndjson-readers.ts
  • src/main/daemon/daemon-client-reader-handoff.test.ts
  • src/main/daemon/daemon-stream-binary-framing.test.ts
  • src/main/daemon/daemon-stream-binary-framing.ts
  • src/main/daemon/daemon-stream-binary-unicode.test.ts
  • src/main/daemon/daemon-stream-data-split.ts
  • src/main/daemon/daemon-stream-framing-replacement.test.ts

🚧 Files skipped from review as they are similar to previous changes (2)
  • src/main/daemon/client.ts
  • src/main/daemon/daemon-client-ndjson-readers.ts

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



📝 Walkthrough

Walkthrough

Daemon stream sockets can negotiate binary-v1; clients use NDJSON when binary framing is not selected. Binary frames carry JSON event or metadata records and raw UTF-8 data. The hello handling preserves bytes received after the hello line for the selected reader. Stream batching, session flushing, held-refill writes, and backpressure accounting now support both Buffer and string output.


Priority: ⬇️ Low

Merge Risk: ⚪ Minimal · up to b1442

The stream framing change is ready to merge after normal checks; no actionable compatibility or output-loss issue remains identified.

Pre-merge checks | Passed 4 | Failed 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage Warning Docstring coverage is 13.79% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 29 functions across 17 files. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Linked Issues check Passed The PR meets the coding requirements in [#27003]. daemon-stream-binary-framing.ts implements length-prefixed event and raw UTF-8 data frames. daemon-client-hello-handshake.ts negotiates `binary-v1…
Out of Scope Changes check Passed The changes remain within [#27003]. The splitter, batcher, backpressure, handshake, reader, connection, and held-refill changes support binary framing, per-socket negotiation, compatibility, safe buff…
Title check Passed The title clearly identifies the main change: sending daemon PTY output as raw bytes instead of NDJSON. It is concise and directly related to the pull request.
Description check Passed The description includes the required sections, linked issue, user-facing behavior, rationale, testing details, compatibility notes, AI disclosure, review guidance, and checklist status. It also expla…


  • Fix all pre-merge checks with AI
  • 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.

Actionable comments posted: 1


ℹ️ Review info
⚙️ Run configuration
  • Configuration used: Repository UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 4c1ab0e6-46c4-4453-9f8b-7747d8dff7a0
📥 Commits

Reviewing files that changed from the base of the PR and between e2234a3 and 80d2cc7.

📒 Files selected for processing (14)
  • src/main/daemon/client.ts
  • src/main/daemon/daemon-client-connections.ts
  • src/main/daemon/daemon-client-hello-handshake.ts
  • src/main/daemon/daemon-client-ndjson-readers.ts
  • src/main/daemon/daemon-hello-protocol.ts
  • src/main/daemon/daemon-stream-backpressure.test.ts
  • src/main/daemon/daemon-stream-backpressure.ts
  • src/main/daemon/daemon-stream-binary-framing.test.ts
  • src/main/daemon/daemon-stream-binary-framing.ts
  • src/main/daemon/daemon-stream-binary-negotiation.test.ts
  • src/main/daemon/daemon-stream-data-batcher.ts
  • src/main/daemon/daemon-stream-data-entry.ts
  • src/main/daemon/daemon-stream-data-split.ts
  • src/main/daemon/daemon-stream-held-refill.ts

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

@nwparker
nwparker merged commit 618dc62 into stablyai:main Oct 10, 2026
43 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.

[Feature]: Send daemon PTY output to main as raw bytes instead of NDJSON

2 participants