feat(gateway): add pinned-session mode and relay client requests in both modes

DA
2d ago
+12625-734

Summary

Adds kit gateway --pinned-session <id>: one gateway process serves one session to many ACP-over-HTTP clients at the same time. The gateway now also relays agent-to-client requests and supports replay cursors in both modes, so pinned mode differs from the default mode only in what many attachments require.

Motivation

A gateway session has one controller: a second attach replaces the first and withdraws its held prompts, and updates reach one client. Hosting one Kit process per conversation needs several clients on one session: a UI, a short-lived deliverer that submits a prompt and leaves, and an operator, with an authenticating proxy in front.

The default mode also refused agent-to-client requests such as ask_user and offered replay only from the start. Neither limit had a protocol reason. Lifting them for both modes keeps the pinned-only surface small.

Impact

Both modes:

  • Agent-to-client requests are relayed. Ask-user (elicitation/create) is offered to attached clients whose own initialize declared capabilities.elicitation.form; permission requests go to every attached client. The first well-formed answer wins and the other clients receive a cancellation. With no eligible client attached, the child gets -32601 at once, as before.
  • A pending request survives a reconnect. It is re-offered to a client that attaches later, and expires after --client-request-timeout (default 5m). Form content is validated by the child, not the gateway.
  • Capabilities are forwarded, not blanked. Default mode forwards the connecting client's declared elicitation.form and nothing else. A client that declares {} sees no change and is never sent a request.
  • Replay cursors. {"type":"_kit_current_turn"} and {"type":"_kit_message", ...} join {"type":"start"}. A current-turn replay that no longer fits is trimmed and flagged with _meta["kit/gateway"].replayTruncated.
  • kit/subagent/steer is accepted as a session-scoped passthrough.
  • SSE keep-alive. Streams carry a comment every 15 seconds while idle, so a proxy that closes silent streams does not cut a quiet turn.

Pinned mode only:

  • One session. session/new and session/resume both attach to the pinned session; any other session id is rejected. session/close detaches the caller and does not end the session. The session is bound to the approved project that already holds its transcript.
  • Many attachments. A new attachment never replaces another. Every attachment receives every session/update through its own bounded queue; one that overflows is dropped alone. Replies go to the sender.
  • Held prompts survive a detach. A deliverer may disconnect before insertion. A retry with the same idempotencyKey takes over the orphaned entry and is not forwarded to the child again. An explicit $/cancel_request withdraws every held attempt that shares the key.
  • Defaults and limits. No replayFrom means no history. elicitation.form is always advertised to the child, because the first client to attach may be a headless one. 32 pending requests per attachment, 128 shared.

Technical details

  • Both modes use one attachment map, one send path and one relay implementation. Default mode holds at most one attachment and keeps its replace, lease and withdrawal policy.
  • Withdrawing one waiter in agentkit-acp cancels its whole idempotency group, so pinned mode keeps orphaned prompts and re-parents them on retry instead of withdrawing them.
  • All attachments in pinned mode share the session, including its configuration and MCP servers. The proxy in front decides who may attach. A client or the proxy must send DELETE when a client disconnects; the gateway does not detect a dropped connection.
  • Two default-mode error details changed: an unsupported replayFrom type now returns -32602, and a response-shaped frame from a client is no longer answered with an error.

Not covered

  • session/fork and session/delete through the gateway. They need coordination with the gateway's session registry.
  • The TUI through the gateway with a relayed ask_user, and the TUI against a pinned gateway, were not exercised end to end.
  • Transport-level resume with Last-Event-ID. It needs per-stream event ids and a bounded replay buffer in agentkit-acp-http.
  • After a truncated replay the first assistant message may be a partial tail; the result flags truncation but not which message.