Skip to content

fix(daemon): bound daemon ingress before it reaches the terminal host - #10837

Draft
nwparker wants to merge 3 commits into
oom-slice-3-daemon-wirefrom
oom-slice-4-daemon-admission
Draft

nwparker wants to merge 3 commits into
oom-slice-3-daemon-wirefrom
oom-slice-4-daemon-admission

Conversation

@nwparker

@nwparker nwparker commented Jul 27, 2026 •

Copy link
Copy Markdown
Contributor

Slice 4 of the re-landed OOM work. Stacked on #10874 (slice 3) — review the diff against oom-slice-3-daemon-wire, not main.

ELI5

The daemon is the background process that owns your terminals. Anything connected to it could previously say "here, hold this" and the daemon would hold it — as many connections as it liked, as many requests in flight as it liked, each one as large as it liked. Nobody was counting.

This adds a bouncer at the door. There's a limit on how many can come in, how many requests each can have going at once, and how big any one piece of a request is allowed to be. Over the limit gets a polite "no" instead of being let in and remembered.

What this fixes

Every retained field on the daemon's ingress path was previously unbounded:

Surface Before After
Pre-auth transport sockets unbounded 128, + 10s handshake deadline
Control clients / stream attachments unbounded 32 / 24
Active requests (per client / process) unbounded 128 / 256
Retained request bytes (per client / process) unbounded 16 MiB / 32 MiB
Reserved response bytes (per client / process) unbounded 64 MiB / 128 MiB
cwd, command, historySeed, ids unbounded 256 KiB / 4 MiB / 12 MiB / 1–4 KiB
env map unbounded 4096 entries, 1 MiB per value, 4 MiB aggregate
PTY cols/rows unvalidated rejected before reaching the native allocator
Handshake line 16 MiB (session-data ceiling) 64 KiB
Client outbound queue unbounded pending-request + control-buffer caps
Daemon pid/token file reads unbounded readFileSync bounded reader (64 KiB)

Strings are measured in UTF-8 bytes, not code units, so a multi-byte payload can't slip past a length check.

Proof the bound works

daemon-server-admission.test.ts drives a real DaemonServer over a real socket and asserts each ceiling holds, then that capacity is released and reusable after disconnect — a limiter that leaks its counter would pass a naive cap test and fail these.

Proof of no regression

  • 1,748 tests pass across src/main/daemon + src/main/providers; node typecheck and lint clean.
  • Rejections don't drop the connection. A malformed request is answered with an error on the same socket; a following well-formed request still succeeds. One bad caller can't disturb a live session.
  • Notifications keep their delivery signal. notify() returns false when it can't buffer, so the caller's existing write-failure recovery runs immediately rather than waiting for a close event — a silent drop would desync the terminal invisibly. Both the false and true paths are asserted.
  • Limits are sized against real usage, not guessed: the history-seed cap is deliberately under the per-client byte budget because the seed rides inside one createOrAttach line, and it's clamped at the producer rather than raised here.

Mutation-tested

A passing test proves nothing if it also passes against a broken implementation. Each of these was deliberately broken and the suite confirmed to fail:

Mutation Result
Unwire the request-admission call 3 tests fail
Answer errors for notifications 1 test fails
notify() always reports delivered 1 test fails
Remove the checkpoint timer's dirty-gate 1 test fails

The first two initially survived — the limit table was unit-tested directly, but nothing asserted the server actually called it, so removing the wiring left the suite green. Tests were added to close that gap.

One test changed, and why

daemon-pty-adapter.test.ts identifies the checkpoint timer by its delay, and had chosen 10s — which now collides with the handshake deadline the live server arms during spawn. It uses a distinct delay instead. Verified against a mutant that removes the dirty-gate: the test still fails, so it continues to prove what it claims.

User-facing behavior changes

None expected in normal use — every ceiling sits well above real usage. Under genuine abuse or a wedged peer, the daemon now answers an error or closes one connection instead of growing main-process memory.

Made with Orca 🐋

The daemon control socket accepted whatever a peer sent: unbounded connection
counts, unbounded concurrent requests, and request payloads whose strings,
environment maps, and PTY dimensions were retained without a size check. A
single misbehaving or wedged client could hold the main process's memory
proportional to what it chose to send.

This adds an admission layer in front of the terminal host:

- Connection ceilings for pre-auth transports, control clients, and stream
  attachments, plus a handshake deadline so a socket that never says hello is
  dropped instead of parked.
- Per-client and process-wide caps on active request count, retained request
  bytes, and reserved response bytes, released when the request settles.
- A field-level limit table for retained request strings (ids, cwd, command,
  history seed, env names/values) measured in UTF-8 bytes, with entry and
  aggregate caps on env maps, and a PTY dimension check.
- A tighter line ceiling for handshake framing than for session data, since a
  hello is a fixed small record from an unauthenticated peer.
- Outbound symmetry on the client: pending-request and control-buffer caps so a
  stalled daemon can't grow the app's own send queue without bound.
- Daemon control files (pid, token) read through a bounded reader.

Rejections answer the request with an error rather than closing the connection,
so a malformed request from one caller does not disturb the session.

notify() keeps its delivered/undelivered return: an over-buffer notify now
reports false and destroys, so the caller's existing write-failure recovery runs
immediately instead of waiting for the close event.

Tests: the limit table is unit-tested directly, and the server suite asserts the
live socket actually consults it — a malformed request is answered with an error
and never reaches spawn. Verified by mutation: unwiring the admission call, or
answering notify errors, each fail the suite.

The checkpoint-timer test picked 10s as its timer's identity, which now collides
with the handshake deadline the server arms during spawn; it uses a distinct
delay so the assertion still proves the timer is dirty-gated (verified against a
mutant that removes the gate).
@nwparker
nwparker force-pushed the oom-slice-4-daemon-admission branch from 87097b4 to 97c07e6 Compare July 27, 2026 06:20
@nwparker
nwparker force-pushed the oom-slice-3-daemon-wire branch from f5a64ff to 2fd224d Compare July 27, 2026 06:40
@OrcaWin
OrcaWin marked this pull request as draft October 27, 2026 09:44
@OrcaWin

OrcaWin commented Oct 27, 2026

Copy link
Copy Markdown
Collaborator

Moving this PR to draft because it has been untouched for more than 80 days (latest recorded PR activity or commit timestamp: 2026-07-28T03:18:08Z; checked on 2026-10-27). This keeps the review queue focused on active work while preserving the proposal. Please mark it ready for review when work resumes.

This branch has not been deployed

No deployments
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.

2 participants