Skip to main content

0013: Bounded transports for language servers and AI

  • Status: Proposed (implemented in Phase 2, task 004; corrected after review)
  • Date: 2026-12-05

Context​

The LSP client used unbounded channels in both directions and an unbounded pending-request map, and poll drained every event in one frame. The lsp-server reader trusts Content-Length. AI jobs used an unbounded channel, and the SSE reader read lines of any length. One misbehaving server or provider could exhaust memory or freeze the UI.

Decision​

  • A shared, dependency-free framing module in ide-process bounds header bytes, header count and body size before allocating, and rejects duplicate, invalid or truncated frames. A framing error closes the connection.
  • Every queue has a limit with an explicit default (TransportLimits, ResourcePolicy, AiLimits). Inbound queues apply back-pressure per server. Outbound overflow restarts and resynchronises the server rather than dropping an edit.
  • The manager polls servers round-robin with a count and time budget and requests a repaint when work remains.
  • Lifecycle control stays out of band through the process supervisor. The AI outcome is kept in a separate slot, so a full queue never hides it.

Corrections (review of task 004)​

  • Inbound bytes are budgeted, not just counted. Each connection reserves body × 3 + 512 bytes from its own budget and from one shared application budget before reading a body. The multiplier covers the decoded payload. A reservation travels with its queued event and is released when the event is handled, or when the connection is cancelled, retired or dropped.
  • Pending request deadlines are part of next_deadline. A request expires at the scheduled wakeup without continuous polling.
  • Publish, then wake. An event is in the queue before the waker runs, so a wakeup can never arrive before the event it announces. A reader blocked on a full queue or budget also wakes on cancellation, so it can never deadlock.
  • Terminal state is out of band. Exit, start failure and protocol errors go to a single terminal slot on the connection inbox, not into the event queue, so they are seen while the queue is full.
  • Exactly-once completion. Every accepted definition request completes once, with a result or with an error for timeout, cancellation, disconnection, restart, stop or shutdown. Completions produced outside poll wait in a manager outbox. Overloaded requests are refused at submission and never complete. Each service generation has its own inbox, so a late answer from a replaced server cannot be read.
  • Decoding happens on the reader thread. Definition and diagnostics payloads are decoded there, so poll only moves typed values. The poll_budget is cooperative. It caps the work started per call, but it is not a frame-time guarantee. Applying large results to the UI incrementally is the async core's job (ADR 0014).

Alternatives considered​

  • Dropping events when full: loses document sync or diagnostics silently.
  • Unbounded queues with monitoring: detects the problem only after memory is gone.
  • A third-party framing crate: none audited for these limits; the format is small.

Consequences​

  • A flooding server slows only itself. The work per poll is capped cooperatively, and inbound memory is capped per connection and across the application.
  • A server that stops reading is restarted, which costs a re-initialisation.
  • Limits are functional safety bounds, not performance gates.

Validation​

Fake-server tests for malformed and oversized frames, floods, full outbound queues, timeouts and unknown IDs; shared byte exhaustion across services, terminal delivery under a full queue, timeouts without polling, exactly-once completion and resynchronised contents; local-HTTP tests for long SSE lines and endless streams; AI slow-consumer and cancellation tests. See Phase 2 validation and 004 corrections.