fix(harness): reuse one upstream connection in the image gate, keep Content-Encoding

AR
16h ago
+622-119

The image gate is the component that stops a coding session from failing when it resends pictures from earlier turns. The gateway takes 8 images per request, so a later turn that resends the conversation can carry more than that and gets refused. The gate strips the oldest earlier images before forwarding.

It was paying for that in the most expensive way available.

forward built a fresh ureq::Agent per request. An agent owns its connection pool, so every turn opened a new socket and ran a full TCP plus TLS handshake to the hosted endpoint. Nothing was reused between turns, or even between requests in the same turn.

codex::start names this as its reason for skipping the gate entirely on a hosted session:

// A hosted endpoint is reached directly, not through `harness::image_gate`:
// the gate opens a new TLS connection for every request (measured at
// ~200 ms of added time to first token)

That comment is the clearest evidence the cost is real. A guard that protects against a gateway 400 was expensive enough that one caller opted out of the guard rather than pay it.

What the handshake costs against the real endpoint

$ curl -s -o /dev/null -w "%{time_appconnect}s\n" https://inference.runanywhere.ai/v1/models
0.166616
0.070439
0.073149

70 to 170ms per turn, before the model has done anything. The ~200 ms in the comment was in the right range.

Fix

One agent is built in start and moved into the handler, so its pool is shared across every request the gate serves.

max_idle_age goes to 120s. ureq's default is 15s, and a coding session sits idle far longer than that between prompts, so the default would expire the connection before the next turn and hand back the handshake this change exists to remove.

Two other things on the same path

Content-Encoding is now forwarded. The request's Accept-Encoding was already being copied to the upstream call. ureq is built here with default-features = false, features = ["native-tls", "json"], so it has no gzip or brotli decoder and passes the body back untouched with the header still on it. This crate's own Client decodes whatever the reply declares. Dropping the header meant the gate advertised an encoding to the editor and then hid the fact that the body carried it.

Forwarding is correct in both directions: ureq strips Content-Encoding itself whenever it does decode (run.rs, "If the body will be decompressed, strip Content-Encoding"), so the header only survives when the body really is still encoded.

The reader is released before the client write on the non-streaming path, so an editor that stopped reading cannot hold a pooled connection open.

Tests

Four cases in image_gate's own module, because that is where cargo test runs them:

TestWhat it pins
turns_reuse_one_upstream_connectionfour turns ride one accepted connection; a fresh agent per request accepts four
the_shared_connection_still_trims_resent_imagesthe shared path still trims nine resent images to eight
other_paths_are_forwarded_untouched/v1/models and cancel routes are not rewritten
an_encoded_reply_keeps_its_content_encodingthe reply still declares gzip and still carries the gzip bytes

The encoding case reads the gate over a raw socket on purpose. This crate's Client decodes gzip, so going through it would answer for the decoder instead of for the header under test.

Both new behaviours were checked against their own absence. Reverting the pooling makes turns_reuse_one_upstream_connection report left: 2, right: 1. Reverting the header forward makes the encoding case fail with the header missing.

Verified

$ cargo test --lib image_gate
test result: ok. 4 passed; 0 failed

$ cargo test --lib
test result: ok. 634 passed; 0 failed

$ cargo test
every suite, 0 failed

$ cargo clippy --all-targets -- -D warnings
no warnings

$ python3 scripts/test/test-ledger.py
147/149 C++ cases ported, 2 excepted, 0 missing

Scope

src/harness/image_gate.rs only.

No change to what the gate rewrites, to which harnesses route through it, or to any translator. harness.rs:536 still starts it for every hosted session exactly as before, cmd_openwork.rs still starts it for OpenWork.

I tried routing the Codex translator through the gate as a follow-up and dropped it: codex/server.rs:271 already calls within_openai_image_limit unconditionally, so the gate adds nothing there and the test passed with the gate bypassed. That work does not belong in this PR.

Worth a follow-up

The gate forwards the client's Accept-Encoding to the upstream call but only ever copies Content-Type and Content-Encoding back. A Retry-After on a 429, for instance, is dropped on the floor. The translators already carry Retry-After through, so the gate should too.


Summary by cubic

The image gate now shares one upstream connection across all turns instead of opening a fresh TCP + TLS handshake per request, removing the per-turn delay that made codex::start bypass the gate. It also forwards Content-Encoding so encoded replies are labeled correctly. The same PR fixes the build link filter (a compile-only flag's operand could leak to the linker) and makes Codex sessions declare X-RA-Harness: codex.

Image gate

  • One ureq::Agent is built in start and moved into the handler; idle timeout rises from 15s to 120s so the keep-alive connection survives gaps between prompts.
  • Content-Encoding is forwarded because the editor's Accept-Encoding is already passed through and ureq (built without gzip/brotli features) delivers the body still encoded.
  • The non-streaming reader is dropped before the client write so a stopped editor cannot hold the pooled connection open.

Other fixes

  • The link-line filter moved to build_link_args.rs and now drops operand-taking compile-only flags with their operand, including the --sysroot=<dir> and -nostdinc spellings that previously survived.
  • The Codex translator now sends X-RA-Harness: codex; the contract has had a Codex harness member since release 2, so sessions were previously attributed by the server's User-Agent fallback.

Summary by CodeRabbit

  • Bug Fixes
    • Image responses now retain supported content-encoding information, helping ensure compressed content is handled correctly when forwarded.
  • New Features
    • Codex requests now identify the Codex harness to upstream services.
  • Performance
    • Forwarded requests reuse an upstream connection, reducing the need to establish a new connection for each request.