fix(net): buffer chunked framing instead of reading it a byte at a time

AR
12h ago
+908-134

read_chunked passed read_line the bare socket, and read_line asks for one byte per read call:

fn read_line<R: Read>(reader: &mut R) -> Result<Vec<u8>, Error> {
    let mut line = Vec::new();
    let mut byte = [0u8; 1];
    loop {
        let n = reader.read(&mut byte).map_err(|_| Error::Read)?;

Every byte of every chunk-size line, trailer line and chunk CRLF was therefore its own read syscall. Chunked framing appears twice per chunk — the size line and the trailing CRLF — so a streamed reply pays that on every event, interleaved with the tokens a person is waiting for.

This is local to the chunked path, not the shape of the transport: read_head already reads in 4 KiB chunks (let mut chunk = [0u8; 4096]), and read_exact_len reads in bulk. read_line has exactly three call sites, all in read_chunked.

Change

Wrap the reader in a small Buffered for the framing reads only. Body bytes still go to sink straight from the existing 8 KiB stack buffer, so nothing is copied twice, and a read as large as the buffer bypasses it entirely so nothing is staged unnecessarily.

Test

chunked_framing_reads_more_than_one_byte_at_a_time counts read calls rather than asserting on structure: 200 chunks of two bytes each, so 800 framing bytes and 400 body bytes.

chunked framing took 1205 reads for 400 body bytes; framing must not cost a read per byte

On main that is 1205 reads; with the buffer it is 206. Reverting only Buffered reintroduces 1205.

Verification

  • cargo test --lib: 635 passed, 0 failed (three consecutive runs)
  • cargo clippy --all-targets -- -D warnings: clean
  • cargo fmt --all: clean

Summary by cubic

Buffers HTTP/1.1 chunked framing reads so chunk-size lines, trailer lines, and chunk CRLFs are no longer read one byte at a time. A streamed reply emits framing on every event; 200 two-byte chunks went from 1205 reads down to 206 for 400 body bytes. Body bytes still go straight to the sink from the existing 8 KiB buffer, and reads as large as the buffer bypass it, so nothing is copied twice.

Also in this PR:

  • Keeps console and image-gate requests on shared keep-alive connections via a pooled ureq::Agent per key, instead of a TCP+TLS handshake per request.
  • Keeps compile-only flags and their separate operands off the kit's link line, fixing a CXXFLAGS -isystem <dir> leak that broke linking.
  • Forwards upstream Content-Encoding through the image gate, and Codex upstream requests now declare X-RA-Harness: codex.

Summary by CodeRabbit

  • Performance

    • Reuses connections for console requests and image-gate forwarding, reducing repeated connection setup.
    • Handles small HTTP chunked transfers with fewer underlying reads.
  • Bug Fixes

    • Preserves non-identity content encoding when forwarding image-gate responses.
    • Improves filtering of build link arguments, including sysroot and quoted arguments.