fix(account): reuse one console connection instead of a handshake per request
real_transport built a new ureq::Agent inside send() (src/account/console.rs:924), so every console request paid its own TCP+TLS handshake. Measured from this machine against inference.runanywhere.ai, time_appconnect is 0.067–0.189 s:
appconnect=0.189095s total=0.230221s
appconnect=0.074550s total=0.115610s
appconnect=0.066903s total=0.105992s
That cost lands on every repeated call to one origin:
- sign-in polling (
/auth/cli/poll) runs on the authorization's own interval while the person approves in the browser, so each poll paid a handshake on top of the wait; - usage listings fetch pages sequentially behind a cursor (
usage_requests.rs, up toUSAGE_REQUESTS_MAX_PAGES), so an N-page listing paid N handshakes; - catalog/model refreshes;
- the cancel worker, a long-lived thread whose whole job is stopping paid decode quickly, built a fresh agent per queued cancel.
Change
One agent per (proxy, connect timeout, total timeout), cloned per request. Cloning a ureq::Agent shares its connection pool, which is what makes the reuse real.
The key stays narrow because this config is per request, not static: total_timeout_ms follows the request's own timeout_ms, and on Windows the proxy comes from discovery for that URL. Sharing across differing budgets would let one caller's shorter total timeout shorten another's connection lifetime, so those remain separate agents.
ureq's PoolKey is (Scheme, Authority, Option<Proxy>), so a shared agent cannot leak a connection across origins or past a proxy boundary.
Idle connections are now held 120 s instead of ureq's 15 s default — a person routinely outlasts 15 s while approving a sign-in or reading a listing.
Test
tests/test_wally_console_connection_reuse.rs counts accepted TCP connections at a listener rather than asserting on structure:
three console requests should reuse one connection, not open one each
left: 3
right: 1
Fails on main with left: 3; passes here with 1. Reverting only the cache reintroduces left: 3.
Also
src/harness/image_gate.rs's counting origin treated a read timeout as a disconnect (Ok(0) | Err(_) => return). Under a busy test binary that closed the keep-alive connection between turns and made turns_reuse_one_upstream_connection fail on a second accept. It now distinguishes WouldBlock/TimedOut from a real disconnect. This flakes on main too — I hit it with my change stashed.
Verification
cargo test: 634 lib tests pass, 0 failedcargo clippy --all-targets -- -D warnings: cleancargo fmt --all: clean
Summary by cubic
Fixes repeated console requests to reuse one pooled connection instead of opening a fresh TCP+TLS handshake per call, removing 67–189ms of latency from every catalog refresh, usage page, sign-in poll, and queued cancel.
Bug Fixes
- One agent per (proxy, connect timeout, total timeout) pair is cloned per request, sharing the connection pool so calls to one origin ride a single connection.
- Idle pooled connections now live 120 seconds instead of ureq's 15-second default, so a pause while approving a sign-in or reading a listing still reuses the connection.
- The image gate now shares one upstream connection across turns and forwards the upstream
Content-Encodingheader. - Codex sessions now declare the
codexharness viaX-RA-Harnessinstead of relying on a User-Agent substring match. - Build-script link filtering now drops operand-taking flags like
-isystemtogether with their operand, fixing a linker failure caused by a bare include directory; the filter moved to a shared file so tests could exercise it.
Summary by CodeRabbit
- Improvements
- Reuse upstream connections for console and image requests, reducing repeated connection setup.
- Preserve supported content-encoding headers when forwarding image responses.
- Include the declared harness identifier in Codex requests.