docs/internal/remote-destinations.md

Remote destination ownership

internal/remotedestination owns reusable outbound EzAgent client connections. It consumes config.ProviderConfig remote descriptors, but does not resolve LLM credentials, discover provider exports, cache catalogs, or change saved models. The agent's consumer runtime owns discovery, caching, and adapter resolution. Configurator integration is separate from the daemon's lifetime. Creating a manager performs no network I/O.

Lifetime and API

  • New(ctx, Options) creates a lazy manager. Daemon runtime owns its manager until shutdown. Setup can create a separate temporary manager and close it after a bounded probe. Runtime must not borrow setup's process lifetime.
  • Get(ctx, host, descriptor) establishes or reuses one hello-complete Connection. Concurrent callers and aliases of the same endpoint share a single startup and connection. Each startup attempt publishes its readiness and immutable result together, so delayed waiters retain their attempt's result when another caller reconnects. Canceling one waiter does not cancel other callers or the manager-owned connection.
  • Retain(providers) retires removed or changed routes and joins their current owners. Model-selection-only changes do not interrupt transport. Call this when publishing configuration. After the first publication, Get rejects descriptors that do not match that publication, including stale run snapshots of removed destinations. Peek(host, descriptor) borrows a matching live connection without I/O for nonblocking metadata projections. Close cancels and joins every owner, including generations replaced by Get.
  • Connection.ListMacros(ctx) requests the unfiltered inventory with empty leaf and filter. Connection.MacroCall(ctx, "", name, row) uses the exact connection, never a local registry. Export-name, IFO, and catalog validation belong to discovery, not this transport package.
  • Connection.Dial(ctx, macro, init) opens a shared tunnelstream net.Conn. init starts with the tunnel type, followed by application target fields. The context owns the stream lifetime. Close each result after use. Opening and active streams share a 128-stream destination limit. Retiring a destination interrupts and joins its stream owners.

The private connection sends an ordinary hello named ez-agent-service, with empty leaf and cwd, no handlers, and no announce. Socket connection and hello have a shared startup deadline, ten seconds by default. Output is bounded to 1024 frames and 4 MiB. Disconnection fails current work rather than replaying it. Subsequent access lazily reconnects. Failed starts use exponential backoff from 100 ms to five seconds, returning unavailable during the backoff instead of blocking unrelated leaves. Startup errors distinguish ErrAccess from ErrHello; both wrap ErrUnavailable. Startup errors include bounded connection/hello details and the last 4 KiB of SSH stderr, without logging command lines or environment. Discovery must distinguish these from missing exports.

Automatic SSH

Direct descriptors dial AF_UNIX. SSH descriptors automatically launch the installed ssh client with structured argv, no shell, no inherited stdin, and no detached or multiplexed process. The manager retains the exact child handle, waits for its exit, and kills and reaps only that child on retirement. It never runs an EzAgent helper against user state and never stops another daemon.

Each forward has a fresh 0700 directory beneath Options.RuntimeDir, defaulting to XDG_RUNTIME_DIR or the system temporary directory. Only that private directory and socket are cleaned up. OpenSSH forwarding-field escaping preserves literal colons, backslashes, and brackets in socket paths. Socket readiness is probed only during bounded startup; tunnel I/O is event driven.

SSH authentication, keys, ssh-agent and host trust belong to the administrator's normal SSH configuration. EzAgent uses BatchMode for noninteractive startup, ExitOnForwardFailure and a bounded connect timeout. It disables multiplexing and authentication-time forking only to keep this forward in the exact child it owns. It does not override password methods, host-key policy, local commands or keepalive settings. EzAgent does not prompt or unlock identities. The process inherits the admin's SSH configuration and agent environment. EzAgent adds no LLM credentials to the SSH command or environment. SSH exit closes the associated client connection. Unexpected socket closure also joins the owned forward and retains its bounded failure details during backoff, even when the socket closes before the SSH child exits. Intentional retirement does not report an SSH failure. Next access can start another forward after backoff.

The forwarded socket grants full host daemon access. It is not a provider-only restricted socket. Forward it only into environments trusted with that access.