ADR 0011doc/{zh,en}/adr/0011-egress-routing.md
ADR 0011: Egress Routing per Instance
Status: accepted, 2026-11-20.
Status: accepted, 2026-11-20.
Context
Admins want outbound traffic routed through proxies by its source (the Codex Runtime, web search, model calls) or by domain, including multi-hop chains, and want to set this in conversation. Today one process-wide proxy comes from the configuration's environment:; Go's ProxyFromEnvironment reads it once, so it can neither differ per instance nor change at run time. Builtin plugins share the Host process, so per-instance environment variables cannot separate them. Only the Host knows which instance makes a request, so the separation has to be a Host mechanism; the proxy itself (mihomo, GPL-3.0) must stay outside the binary.
Decision
- Role
egressinpkg/plugin, needing the operator's grantegress, at most one instance per Host. In v1 it must be builtin, and it loads before every other instance. The Host gives it the targets (instances with their egress support and a per-start token) and takes its routing snapshot: the local proxy address and routes{instance?, domain?, direct?}. It notifies the Host of changes throughEgressHost.Changed, which also keeps instance-scope secrets. The Host caches the last snapshot so routed traffic fails closed when the provider cannot load. - Host evaluator (
internal/host/egress) holds the snapshot and picks the most specific matching route per request: instance and domain, then domain, then instance; an exact domain before a wildcard (*.x.com, which includesx.com). - *
plugin.Envgains `Proxy func(http.Request) (url.URL, error)andProxyAddr`.* Builtin plugins and remote MCP servers use them instead ofProxyFromEnvironment; unrouted traffic keeps the environment's proxy. The Host's own clients (OAuth, install, catalogs) keep the environment's proxy in v1. Every outbound HTTP client is built by one public package,pkg/outbound(proxy, default timeouts, call logging,egress.unavailable); no call site builds its ownhttp.Transportor useshttp.DefaultClient. - Child processes get
HTTP(S)_PROXYandALL_PROXYwith their instance's credentials, a loopbackNO_PROXYandNODE_USE_ENV_PROXY=1, whenever an egress instance is configured; the URL is stable, so route changes need no restart. - Manifests declare
egress: in_process | env. A route for an instance that declares neither is refused (egress.target_unsupported). - Failure is closed: a routed request never falls back to direct (
egress.unavailable). - First provider: the builtin
mihomoplugin, which runs mihomo as a child process and offers admin-only, admin-approved tools. Routes stop at domains: there is no TLS interception, no path matching and no TUN.
Consequences
- ◆Changes the plugin contract after M6: a role, a grant, two
Envfields, a manifest field, the child environment. Plugins that make no outbound calls are unaffected; consumers move fromProxyFromEnvironmentin the same change. - ◆With an egress instance configured, child processes depend on the local mihomo for all their traffic; when it is down they cannot connect until it restarts.
- ◆
envsupport is best effort: a program that ignores proxy variables goes direct, and the tools say so. - ◆The
environment:proxy variables keep working when no egress instance is configured. - ◆This is the eleventh ADR, beyond the v1 guideline of about ten; the owner accepted it as a kernel decision on 2026-11-20.
Design: Egress proxy.