graff — a fast agentic coding harness in Zig: multi-provider, MCP, workflows, DGM evolution loop, TS/Python SDKs
Python
Merge #679 into release/v0.0.282: reuse warmed TLS on MCP HTTP and WSS
Cursor Agentc1c6522f225f
diff --cc docs/adr/0050-reuse-warmed-tls-on-known-networks.mdindex 00000000,dc3c1473..99591ed7mode 000000,100644..100644--- a/docs/adr/0050-reuse-warmed-tls-on-known-networks.md+++ b/docs/adr/0050-reuse-warmed-tls-on-known-networks.md@@@ -1,0 -1,52 +1,52 @@@-# 0043. Reuse warmed TLS state on the networks we already speak++# 0050. Reuse warmed TLS state on the networks we already speak++ Status: accepted 2026-08-29++ ## Context++ The harness already talks to three networks: provider HTTP/SSE (and WS),+ MCP Streamable HTTP, and the Codegraff gateway. Two patterns were wasting+ a handshake on every first useful call:++ 1. MCP Auto's modern `tools/list` probe and the legacy+ `notifications/initialized` notify each built a throwaway `std.http.Client`,+ so a successful probe's keep-alive died before `tools/call`, and initialized+ paid a second TLS for a fire-and-forget 202.+ 2. Every WSS dial (`ws.WsClient.connect`) rescanned the host CA store from+ disk, even though launch already warms the HTTP client's bundle.++ ADR 0002 (xAI WS full-resend), 0009/0011/0028 (prompt-cache keys), and 0035+ (deferred MCP join) stay untouched: this is transport reuse, not wire shape.++ ## Decision++ - MCP HTTP probe and `notifications/initialized` use `server.transport.http`.+ Do not construct a per-call client for those paths.+ - WSS TLS uses a process-lifetime CA bundle warmed once (`http_warm.ensureProcessCa`).+ A reconnect must not walk the host store again.+ - WS→SSE fallback keeps the Agent's prewarmed HTTP pool (`postStream`). Do not+ introduce a fresh client on that latch: WS never used the HTTP pool, and a+ new TLS would be strictly more expensive.+ - MCP Streamable HTTP advertises the std client's default Accept-Encoding+ (gzip/deflate) and decompresses Content-Encoding. Do not omit it: a tools/list+ catalog is the fat payload on that network, and provider POST already accepts+ compression.++ ## Consequences++ A modern MCP connect plus the next list is one TCP accept on keep-alive.+ WSS reconnects skip the CA disk walk. MCP catalogs can travel gzip-compressed.+ Revisit only if a shared `std.http.Client` is shown unsafe for the concurrent+ initialized+list pair, a host CA rotation must be picked up mid-process without+ restart, or a server is broken by Accept-Encoding.++ ## Measured (2026-08-30, this host)++ | Path | Before | After | Left on the table |+ |---|---|---|---|+ | MCP modern connect + next `tools/list` | 2 TCP accepts (throwaway probe client, then the persistent one) | 1 accept, 2 POSTs | nothing — keep-alive is the rest |+ | WSS CA disk walk | 1 `rescan` per connect, including every reconnect | 1 per process | launch still walks once for the HTTP client (~5–7 ms, 144 certs / 154 KB here). Cloning that bundle into the process one saves one launch scan and risks a double-free; not worth it |+ | MCP `tools/list` catalog (40-tool fixture) | 6110 B raw (`Accept-Encoding` omitted) | 320 B gzip (5% of plaintext) | only if the server ignores gzip — then we still send the header and read identity |+ | WS→SSE latch | prewarmed Agent HTTP pool | unchanged | a fresh client would add a TLS handshake |+ | xAI / Codex WS turn body | full history | unchanged | ADR 0002: no `previous_response_id` chain |+ | MCP OAuth token / login 401 probe | throwaway `std.http.Client`; login probe omits encoding | unchanged | not on the turn path |diff --cc docs/adr/README.mdindex 2ea750be,13abb757..cb7e3da6--- a/docs/adr/README.md+++ b/docs/adr/README.md@@@ -52,14 -52,7 +52,15 @@@ record only when you need the evidence| [0039](0039-local-tools-are-project-scripts.md) | Agent-authored local tools are project scripts under `.graff/tools/`; skills stay instructions. Runtime catalog extras, not `schema.effectiveRootSpecs`. || [0040](0040-codedb-stays-when-licensed.md) | Ordinary reads use native `codedb` / `read_file`; codedb-pro is extra search, not the default reader. || [0041](0041-tui-is-an-acp-client.md) | The fullscreen TUI is an in-process ACP client: session/prompt in, session/update thought/tool/text out. No child `graff acp`. |-| [0043](0043-reuse-warmed-tls-on-known-networks.md) | Reuse warmed TLS: MCP probe/initialized stay on the persistent HTTP client; WSS CA is scanned once per process; WS→SSE keeps the prewarmed pool; MCP HTTP accepts gzip. |+| [0042](0042-tui-claims-screen-before-session.md) | `graff tui` / TTY `graff repl` claim the alt-screen before keys/MCP/prompt; leftover boot happens inside the pager. |+| [0043](0043-pi-swe-same-seat.md) | Pi SWE A/B uses `pi-xai` on the SuperGrok seat; do not steal Pi's catalog or heap from the json-stream pass. |+| [0044](0044-oneshot-skips-learn-auto.md) | `-p` and `--json` skip learn auto-init; the Pi SWE wall gap was a 38s `graff-pinned` copy, not their catalog. |+| [0045](0045-glm-flash-swe-codegraff.md) | Codegraff `glm-5.3-flash` SWE: Pi 5/6 in 758s, graff 3/6 with three 300s timeouts; do not steal Pi's heap. |+| [0046](0046-flash-omits-default-effort.md) | Flash / Gemini send `reasoning_effort=low` (omit still thinks); lean `-p` shortens tool prose; `-p` streams. |+| [0047](0047-codegraff-swe-not-glm-only.md) | Codegraff SWE A/B is not GLM-only: Gemini graff 5/6 in 103s; DeepSeek flash still one-shots; do not steal Pi's catalog. |+| [0048](0048-model-http-client-recovery-uses-generations.md) | Model HTTP calls lease a recoverable client generation; request-construction TLS failure rotates safely without deinitializing in-flight users. |+| [0049](0049-resume-branches-have-independent-durable-identity.md) | `--resume SOURCE --branch DEST` clones provider history and peer cursor state once; every later save belongs only to DEST. |++| [0050](0050-reuse-warmed-tls-on-known-networks.md) | Reuse warmed TLS: MCP probe/initialized stay on the persistent HTTP client; WSS CA is scanned once per process; WS→SSE keeps the prewarmed pool; MCP HTTP accepts gzip. |## When to write onediff --cc src/agent_stream.zigindex 697eab5e,40eb98f5..697dec6f--- a/src/agent_stream.zig+++ b/src/agent_stream.zig@@@ -48,17 -47,11 +48,17 @@@ pub fn postStream(self: *Agent, body: [return postStreamWithClient(self, self.client, body);}- /// SSE stream using an explicit HTTP client. Normal traffic uses the Agent's- /// shared pool; a WebSocket failure supplies a fresh client so a stale pooled- /// keep-alive cannot poison the WS→SSE handoff and every fallback retry dials- /// from a clean pool.+ /// SSE stream using an explicit HTTP client. Normal traffic and the WS→SSE+ /// latch both use the Agent's prewarmed pool (agent_ws.postLive); a test can+ /// pass another client. A failed SEND still poisons that connection so the+ /// next retry dials fresh instead of replaying a dead keep-alive.pub fn postStreamWithClient(self: *Agent, client: *std.http.Client, body: []const u8) ![]u8 {+ http_client.waitForReady(client.io);+ var lease = http_client.acquire(client);+ defer lease.release();+ if (!lease.available) return error.Canceled;+ if (http_client.injectedConstructionTls(&lease)) |err| return err;+ const transport = lease.client;const sink = engine_sink.forAgent(self);sink.emit(self.io, .stream_begin);// Every exit path — success, interrupt, transport error — tears down thediff --cc src/http_warm.zigindex d95d744b,adb889a4..ec2f0119--- a/src/http_warm.zig+++ b/src/http_warm.zig@@@ -3,12 -3,44 +3,44 @@@const std = @import("std");const Io = std.Io;+ /// Process-lifetime bundle. Always `page_allocator` so a test GPA cannot+ /// free it while a later WSS dial still holds the pointer.+ var process_ca: std.crypto.Certificate.Bundle = .empty;+ var process_ca_rw: Io.RwLock = .init;+ var process_ca_init: Io.Mutex = .init;+ var process_ca_ready = std.atomic.Value(bool).init(false);++ /// Test seam: how many times the process bundle actually hit the disk.+ pub var process_ca_rescans: u32 = 0;++ pub fn processCa() *std.crypto.Certificate.Bundle {+ return &process_ca;+ }++ pub fn processCaLock() *Io.RwLock {+ return &process_ca_rw;+ }++ /// Scan the host CA store once. Later WSS reconnects reuse the same bytes.+ pub fn ensureProcessCa(io: Io) !void {+ if (process_ca_ready.load(.acquire)) return;+ process_ca_init.lockUncancelable(io);+ defer process_ca_init.unlock(io);+ if (process_ca_ready.load(.acquire)) return;+ const now = Io.Clock.real.now(io);+ process_ca.rescan(std.heap.page_allocator, io, now) catch return error.HandshakeFailed;+ process_ca_rescans += 1;+ process_ca_ready.store(true, .release);+ }+/// Pre-load the shared HTTP client's CA bundle single-threaded so concurrent- /// agents never race Zig's lazy first-connect rescan.+ /// agents never race Zig's lazy first-connect rescan. Also warms the process+ /// bundle so the first WSS dial does not pay a second disk walk.-pub fn prewarmCaBundle(client: *std.http.Client, gpa: std.mem.Allocator, io: Io) void {+pub fn prewarmCaBundle(client: *std.http.Client, gpa: std.mem.Allocator, io: Io) !void {const now = Io.Clock.real.now(io);- client.ca_bundle.rescan(gpa, io, now) catch return;+ try client.ca_bundle.rescan(gpa, io, now);client.now = now;+ ensureProcessCa(io) catch return;}/// Warm off the launch critical path. Outbound users wait ondiff --cc src/test_hooks.zigindex ff1fe6aa,08fb225e..9dd39eed--- a/src/test_hooks.zig+++ b/src/test_hooks.zig@@@ -345,7 -341,5 +345,8 @@@ test_ = @import("channel_worker.zig");_ = @import("session_wake.zig");_ = @import("tui_acp.zig");+ _ = @import("readline_paste.zig"); // #674: semantic paste spans (not reached via readline.zig's runtime import)+ _ = @import("agent_request_scratch.zig"); // #693: split out of agent_request.zig (600-line ceiling)+ _ = @import("commands_resume.zig"); // #697: --resume SOURCE --branch DEST parser+ _ = @import("net_efficiency_test.zig");}