Skip to content

Let a controller send its agents' operations on its socket, not over HTTP (CHOO-3667) - #1275

Closed
abeldantas wants to merge 2 commits into
mainfrom
controller-socket-calls
Closed

abeldantas wants to merge 2 commits into
mainfrom
controller-socket-calls

Conversation

@abeldantas

Copy link
Copy Markdown
Collaborator

A controller relays every operation its agents' sessions call (post_message, read_context and the rest of /agents/{id}/ops/*), and each one was an HTTP request of its own, authenticated with the controller's token. The controller already holds an authenticated WebSocket to the same server for its agents' events, so in this PR the socket carries the operations too.

We ran the same build both ways on 12 pods (four shared m5.xlarge nodes, Aurora), with 10,000 agents behind 498 controllers posting and reading through operations, as the controller relays them:

msg/s over HTTP: pod CPU, p99 on the socket: pod CPU, p99
83 34%, 977 ms 25%, 589 ms
150 52%, 985 ms 40%, 998 ms
200 68%, 1.8 s 51%, 1.0 s
250 fell behind: 123 msg/s sent, 704 errors 62%, 1.7 s
300 75%, 14.6 s, no errors

In this PR we:

  • take call frames on the controller's socket and answer each with a call.result frame carrying the status and body the HTTP route would have answered with (management/socket_calls.py). A call is authorized as the HTTP request is: the controller is checked against its row, and the agent's binding against presence.
  • offer calls in the socket's first frame (calls.max_in_flight, 64), so a controller from before this keeps using HTTP. A call over the limit is answered 429 capacity_exceeded without running.
  • have the controller's relay send POST /agents/{id}/ops/{operation} as a call while the socket offers them, and over HTTP otherwise (socket-calls.ts, relay-forward.ts). A call the socket closes on is answered 502, as a request whose connection drops.

test_socket_calls.py sends each outcome of an operation through both doors and compares the answers.

…HTTP (CHOO-3667)

A controller relays every operation its agents' sessions call, and each
was an HTTP request of its own: parsed, routed and authenticated with the
controller's token, though the controller already holds an authenticated
WebSocket to the same server. The socket now takes them as `call` frames
and answers each with the status and body the HTTP route would have
(`management/socket_calls.py`). A call is authorized as the HTTP request
is: the controller checked against its row, the agent's binding against
presence. The socket's first frame says it takes calls, so a controller
from before this keeps using HTTP.
…667)

The relay sends `POST /agents/{id}/ops/{operation}` as a call on the
controller's socket while Switch offers calls, and over HTTP otherwise
(`socket-calls.ts`, `relay-forward.ts`). A call the socket closes on is
answered 502, as a request whose connection drops. The server answers
every call, a failure with 500, so a controller is never left waiting
on one until its socket closes.

Copilot AI 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.

🟡 Changes recommended

Critical unresolved issues remain in relay response typing, per-connection answer-queue bounds, and socket authorization after agent removal.

3 open issues
What changed in this PR

This PR adds a WebSocket call channel for controller-relayed agent operations, reducing per-operation HTTP overhead while preserving HTTP-compatible status and response bodies. It also adds authorization parity, capacity negotiation, fallback behavior, and protocol coverage across the Python service and TypeScript controller.

Changes:

  • Adds authenticated call and call.result frames to controller WebSockets, including concurrency limits and disconnect handling.
  • Updates the controller relay to use socket calls for JSON operation requests, falling back to HTTP when unsupported or at capacity.
  • Adds shared refusal-status handling, request-ID sanitization, contract fixtures, and extensive Python/TypeScript tests.
File Description
core/​tests/​switch_core/​management/​test_stream.py Updated as part of this pull request.
core/​tests/​switch_core/​management/​test_socket_calls.py Updated as part of this pull request.
core/​tests/​switch_core/​management/​test_contract_fixtures.py Updated as part of this pull request.
core/​tests/​switch_core/​management/​test_connection_socket.py Updated as part of this pull request.
core/​tests/​switch_core/​fixtures/​agent_controllers/​stream_frames.json Updated as part of this pull request.
core/​switch_core/​request_context.py Updated as part of this pull request.
core/​switch_core/​management/​socket_calls.py Updated as part of this pull request.
core/​switch_core/​management/​controller_routes.py Updated as part of this pull request.
core/​switch_core/​management/​auth.py Updated as part of this pull request.
core/​switch_core/​bridges/​agent/​api/​operations.py Updated as part of this pull request.
console/​packages/​agent-controller/​src/​stream.ts Updated as part of this pull request.
console/​packages/​agent-controller/​src/​stream.test.ts Updated as part of this pull request.
console/​packages/​agent-controller/​src/​socket-calls.ts Updated as part of this pull request.
console/​packages/​agent-controller/​src/​socket-calls.test.ts Updated as part of this pull request.
console/​packages/​agent-controller/​src/​schemas.ts Updated as part of this pull request.
console/​packages/​agent-controller/​src/​schemas.test.ts Updated as part of this pull request.
console/​packages/​agent-controller/​src/​relay.test.ts Updated as part of this pull request.
console/​packages/​agent-controller/​src/​relay-forward.ts Updated as part of this pull request.
console/​packages/​agent-controller/​src/​controller.ts Updated as part of this pull request.
CHANGELOG.md Updated as part of this pull request.

Review effort: Lite

Comment on lines +103 to +106
const text = JSON.stringify(answer.body);
res.writeHead(answer.status, {
'Content-Type': 'application/json',
'Content-Length': Buffer.byteLength(text),
Comment on lines +770 to +775
if len(in_flight) >= socket_calls.MAX_CALLS_IN_FLIGHT:
outbox.put_answer(socket_calls.capacity_exceeded(id_))
return

async def run() -> None:
outbox.put_answer(
Comment on lines +155 to +159
binding = authenticator.presence.binding(call.agent_id)
if (
binding is None
or binding.controller_id != controller.controller_id
or binding.tenant_id != controller.tenant_id
@abeldantas

Copy link
Copy Markdown
Collaborator Author

Combined into #1307 with the other scaling PRs.

@abeldantas abeldantas closed this Nov 2, 2026
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