docs/internal/agenthub-application.md
AgentHub endpoint application v1
The application codec is the boundary between the gateway's endpoint tunnel and ordinary EzAgent JSONL. It carries no invitation, TLS, authorization, or store state.
The direct-handshake review places this handoff after the separate admission and endpoint approval ceremonies.
Frames
An application frame is a U32BE body length followed by a closed JSON object.
The body uses v: 1. open contains an endpoint ID and the fixed application
name ez-agent-jsonl/v1; open_ok acknowledges it. data carries an opaque
JSON value. informational currently reports queue_pressure, and error
uses one of the bounded endpoint/application error codes.
Wrapper strings parse strictly — including UTF-8 and surrogate handling — and wrapper and payload bounds are charged before allocation. Duplicate or escaped duplicate field names, unknown fields, invalid numeric spellings, invalid nesting, and trailing bytes are rejected. The codec preserves payload bytes and does not impose a second JSON grammar on the enclosed data.
Connection ownership
The gateway checks endpoint publication and selection policy before handing off the tunnel. Inside endpoint TLS, the endpoint engine receives one open frame, checks the exact identity and ACL, dials the configured AF_UNIX service once, sends open_ok, and passes the same decoder and encoder to the active proxy. A denied, retired, unpublished, unavailable, or mismatched endpoint never reaches the application dial.
Public entry and prepared direct endpoint handlers use the same hoptls
transport/capacity lifecycle. Cancellation aborts blocked I/O; the serving worker
joins before releasing its authenticated reservation. The endpoint invitation
scheduler lease is not the generic TLS reservation and survives the transfer
through the ceremony. Ordinary application work still requires the inner
endpoint identity and the exact endpoint authorization epoch, never the outer
entry admission. The application codec never changes authorization epochs or
gateway listing state.
Fixture and tests
application.json contains current version-1 open, data, informational, error,
Unicode, bounds, and negative wrapper literals. The Go generator and
check-application.rb are independent from the runtime codec. Application and
endpoint package tests cover exact framing, malformed JSON, bounds, dial order,
redaction, and decoder handoff.
Direct and routed paths must use the same endpoint TLS, ACL implementation, application codec and one-open-one-dial handoff. The preparation contract uses Engine.PrepareDirect and a routed wrapper over a shared private handler, not a second application engine. Production installs those consumers; remote availability is composed on them, and routed endpoint authorization uses the same engine. AgentHub is unreleased. The existing TUI enters through this application handoff; pairing never enters it.
Origin engine boundary
A production origin Consumer passes its stable, reserved Session net.Conn adapter to the prepared endpoint handler once. Only inner endpoint TLS supplies AuthenticatedEndpoint.Peer. The outer admitted client, Hub bootstrap peer and route metadata cannot supply endpoint ACL identity. Preserve the current RunApplication order: exact open endpoint/profile, local published identity, Registry.RegisterEndpoint, BeginWork, socket reservation, one AF_UNIX dial, open_ok, MarkFirstUse and RunActive with the same decoder/encoder.
Publication/root/generation rechecks before ready and delayed TLS prevent a pending open from using stale selection. BeginWork remains the acceptance barrier for the actual application; an established open survives publication withdrawal, while revocation and exact epoch changes retain their terminal policy. Route recovery alone repeats none of these steps. Its replay is opaque transport ciphertext, not application requests. A missing service socket is still an open-time application error, not permission to withdraw publication, fall back to entry's local socket or try another endpoint. Terminal cancellation joins the engine, adapter and proxy before releasing TLS/authenticated/socket charges; origin relay settlement remains a separate later boundary.
The engine separates normal application completion from terminal failure before closing the raw carrier. RunApplication uses the proxy's joined normal TLS close; RunActive seals and drains accepted output before close_notify and carrier FIN. Failure still invokes narrow Abort before joined raw Close. Revocation, malformed TLS, abnormal proxy termination, caller loss and close expiry still abort immediately and join. A nil application result is only a request for graceful shutdown, not proof of a clean circuit. Circuit observation publishes TerminalClean only after authenticated FIN and nil joined settlement; normal completion emits no gateway circuit_failed. The existing application codec, ACL/epoch order and one-dial rule remain unchanged.
Recovery continuity
The production recovery checkpoint preserves the original endpoint TLS, application decoder/encoder, exact ACL epoch and AF_UNIX connection across alternate and restored paths. Ciphertext replay does not replay open, hello or application requests. A partially read JSONL line may already be decoded and buffered downstream; application continuity does not require a ciphertext charge to remain at either Hub after the circuit has acknowledged those bytes. Publication withdrawal permits the established retained session to recover but grants no fresh opening authority. Explicit revocation, client-entry loss, application/TLS failure and grace expiry remain terminal.
The same origin queue retains required FIFO output and its existing replacement and 15-second freeze policy during pause. Pending service EOF and authenticated TLS close_notify drain through successful replacement; physical loss alone does not become application EOF. At fixed grace expiry both application edges close abortively and join. The service checkpoint and service tests separate real production TLS/socket evidence from scripted local application behavior. Gateway lifecycle status stays outside this codec and ordinary EzAgent JSONL.
Cross-owner epoch evidence
TestProductionLifecycleRevokedEpochRegrantDoesNotReviveApplication composes
live routed application revocation with fresh admission or endpoint consent.
The affected local application joins before re-grant. The old owner stays
terminal; a distinct fresh application is required. Endpoint re-grant changes
the exact ACL epoch and secret, not endpoint identity. Admission re-grant does
not replace or grant the independent endpoint ACL. The remote retained owner
may outlive a best-effort RESET until its existing failure/grace bound, without
reviving the old entry application.
The diamond peer-removal production regression and the earlier withdrawal, pressure, partial-read and restored-path tests keep one application engine and socket through authenticated circuit replacement. This does not replay an application operation or make withdrawal an ACL revocation. See service application regressions. These are service fixtures, not shipped TUI or live EzAgent acceptance.
Maximum-value headroom
The complete raw JSON value ceiling is 67,108,864 bytes. The application wrapper has an independent 1,024-byte allowance; escaped payload text is not counted again as wrapper text or decoded into a maximum-size Go string. Grammar accepts fractional/exponent numbers and the existing encoding/json nesting limit of 10,000. Parser scratch is a separate 262,144-byte admission charge.
Reserved decoding admits declared body capacity plus parser scratch after the four-byte prefix and before body allocation/read. Reserved encoding admits wrapper construction plus scratch and the simultaneous complete encoded write copy before validation or marshaling. Charges survive blocked/partial writes until retirement; malformed input, admission failure and write failure release exactly once. Encoder reuse after a terminal failure does not reacquire storage.
The service defaults now give application and parser resources 256 MiB each, within the unchanged 512 MiB DATA aggregate. This covers maximum queue payload, line/compact overlap, incoming body/compact storage and active outgoing wrapper and encoding copies. It does not promise admission under unrelated DATA pressure or at every simultaneous configured count. Resource byte sums deliberately need not fit the aggregate; finite exhaustion rejects work rather than dropping required values or silently raising limits.