docs/internal/agenthub-jsonl-proxy.md
AgentHub JSONL proxy
The JSONL proxy connects an external EzAgent JSONL edge to the current application codec. It is a bounded origin queue, not a second AgentHub transport. Direct and relayed production paths use this same proxy behind identical inner endpoint TLS. Relaying changes the route, not the JSONL grammar, supported provider frames or origin queue contract.
Queue ownership
Each origin has a finite queue. A replacement snapshot removes only an older queued snapshot for that origin; active writes are never silently replaced. Required values remain FIFO. The sole writer retains its payload charge until send returns. Reservations cover line construction, compact copies, queued payloads, classification/metadata, application wrappers and encoded writes. Replacement-adjusted queue pressure closes the application transport and returns a resource-limit failure. Informational application frames remain transport-local and never enter client EzAgent JSONL.
The queue counts retained compact payload bytes, including its popped writer item, and permits one complete 64 MiB value as the sole item. Line construction, old/new growth, compact destination and parser scratch remain separate charges. The escaped maximum is tested through real direct and one-relay endpoint TLS under production defaults. This does not establish an allocator or RSS ceiling, or complete the broader concurrent-capacity matrix.
Known local resource pressure is published before partial-write interruption.
The queue's first terminal cause remains authoritative for the origin's joined
return, even when that interruption makes the direct gateway cancel its
invocation context. This local cause does not guarantee remote terminal delivery.
Existing authenticated circuit/mux resource RESETs preserve broad diagnosis
inside local owners. The user has now explicitly approved Hub-to-client exit
feedback. The client-entry gateway sends terminal/resource_limit when its owner
has causal pressure evidence, otherwise generic terminal/circuit_failed or
transport failure. See the exact approved contract.
The client retains a received pressure terminal as a native error. No status
enters EzAgent JSONL. Received RESETs are not echoed.
The terminal attempt is bounded and best effort. A busy terminal slot, lost
attachment or failed outer status write can yield generic circuit_failed or
transport failure, never clean completion or invented pressure. Existing reason
codes cannot identify origin queue overflow separately from other resource
failures. An exact subtype or guaranteed remote diagnosis requires an approved
inter-Hub semantics change; neither is claimed by this implementation.
The proxy preserves complete JSONL lines and classifies only supported origin output for queue replacement. Client input and client-side output conversion remain strict pass-through. The application codec wraps payloads without imposing a second JSON grammar on the enclosed data.
EOF, drain and joined shutdown
Clean local edge EOF seals admission without discarding accepted output. The origin queue drains its accepted values, including the active writer, before closing the application write direction. Clean application EOF also seals admission and stops the owned local edge, while preserving already accepted output for drain. EOF is directional: production TLS CloseWrite sends close_notify but keeps reads alive until reciprocal authenticated EOF. The final ordinary application Close and every proxy worker join precede return. Legacy transports without CloseWrite use ordinary Close to retire their reader; that fallback does not hide production TLS transport errors.
One fixed 30-second bound starts at the first seal. It covers accepted-output drain, transport close and worker joins; later EOF or close work cannot restart it, and expiry wins at equality. Cancellation, malformed/truncated input, resource pressure and abnormal edge/application errors abort blocked I/O and join workers before releasing reservations. Missing TLS close_notify is not clean EOF.
This close bound is separate from the fixed 65-second transport recovery grace and from security time, certificate validity and authorization epochs. Attachment loss alone pauses the retained stream, does not manufacture EOF and does not reopen the application. Recovery cannot extend an earlier close bound or revive revoked security state. See application ownership and production service recovery.
Handoff and reuse
Proxy tests cover line bounds, classification, queue replacement, origin isolation, reservation release, partial writes, clean directional EOF, accepted-output drain, cancellation and joined shutdown. The application fixture contains the current outer handoff literals.
The direct and relayed transports yield an owned ordinary JSONL stream for
pkg/ezagentapi.ConnectClientRW. Handoffs use non-buffering readers so a control
decoder cannot consume bytes from the next protocol phase. There is no second
TUI request map or remote-only capability allowlist. The bootstrap flow hands
this client directly to the existing TUI after JSONL startup and adds no
pre-TUI session protocol. Production reuses this client for direct and routed
paths.
The AgentHub EOF, half-close, drain and joined-shutdown behavior was approved as part of endpoint application relaying. No public agent JSONL fields or RPC semantics changed.