graff — a fast agentic coding harness in Zig: multi-provider, MCP, workflows, DGM evolution loop, TS/Python SDKs
Python
Use bounded HTTP/2 for model catalog requests
Rach Pradhan83525f3918fc
diff --git a/docs/adr/0190-catalog-gets-share-bounded-http2-transport.md b/docs/adr/0190-catalog-gets-share-bounded-http2-transport.mdnew file mode 100644index 00000000..f893c0f7--- /dev/null+++ b/docs/adr/0190-catalog-gets-share-bounded-http2-transport.md@@ -0,0 +1,35 @@+# 0190. Catalog GETs share bounded HTTP/2 transport++Status: accepted 2026-09-23++## Context++Dynamic model catalogs are startup inputs. Some providers paginate them, and a+later page can fail after earlier pages were valid. The old HTTP/1.1 fetch+followed redirects and accepted unbounded response bodies. The small response+limit for optional JSON POSTs in [0189](0189-bounded-buffered-http2-posts.md)+cannot accommodate complete catalogs with verbose model metadata.++## Decision++Use the same exclusive HTTP/2 pool and joined deadline for catalog GETs. Keep+the page walk and its valid-prefix/cache fallback in the catalog layer. Bound+each page at 16 MiB and reject larger responses before parsing. A page larger+than the picker response is valid; the transport limit must not silently turn+it into a missing catalog. Read the status before the body on both HTTP/2 and+HTTP/1.1, including failed responses whose bodies stall or exceed the cap.++Follow up to three GET redirects within the same deadline on either protocol.+Keep catalog headers on same-origin redirects; send only non-credential Accept+on a scheme, host, or port change. HTTP/1.1 fallback is allowed only before an+HTTP/2 request is sent. The transport allocator owns pooled sessions until+shutdown; the completed page is copied to the result allocator.++## Consequences++Catalog discovery can reuse an HTTP/2 connection across pages without making+startup depend on a partial response or unbounded body. Each concurrent fetch+may temporarily reserve its page limit. A response beyond 16 MiB falls back+to the existing cached or baked catalog rather than activating a truncated+list. The offline TLS fixture covers redirects, credentials, pagination,+large valid pages, limits, status-first failures, and cancellation.diff --git a/docs/adr/README.md b/docs/adr/README.mdindex 41da4957..8aa01b17 100644--- a/docs/adr/README.md+++ b/docs/adr/README.md@@ -215,6 +215,7 @@ record only when you need the evidence or the edge cases.| [0187](0187-optional-judgments-preserve-usage-uncertainty.md) | Optional judgments use an independent gateway login; tokens and unsettled cost remain distinct in CLI and ACP usage. || [0188](0188-streamed-tool-calls-retain-identity.md) | Streamed tool calls use explicit IDs; reused indexes keep incomplete predecessors invalid, and prose is not execution evidence. || [0189](0189-bounded-buffered-http2-posts.md) | Buffered HTTP/2 posts cap body growth, use exclusive pooled leases with durable transport allocation, join deadlines, and never replay ambiguous sends. |+| [0190](0190-catalog-gets-share-bounded-http2-transport.md) | Catalog GETs use bounded HTTP/2 pages with joined deadlines, shared redirect limits, and credentials scoped to the original origin. |## When to write onediff --git a/scripts/eval/catalog-http2.cjs b/scripts/eval/catalog-http2.cjsnew file mode 100644index 00000000..5d35fb78--- /dev/null+++ b/scripts/eval/catalog-http2.cjs@@ -0,0 +1,35 @@+// Local TLS model-list fixture with cursor pages and bounded-body failures.+const fs = require('node:fs');+const http2 = require('node:http2');+const https = require('node:https');+const [key, cert, log] = process.argv.slice(2);+const server = http2.createSecureServer({ key: fs.readFileSync(key), cert: fs.readFileSync(cert), allowHTTP1: true });+const sessions = new WeakMap(); let nextSession = 1;+function record(row) { fs.appendFileSync(log, JSON.stringify(row) + '\n'); }+server.on('session', session => { const id = nextSession++; sessions.set(session, id); record({ event: 'session', id, alpn: session.socket.alpnProtocol }); });+server.on('stream', (stream, headers) => {+ stream.on('error', () => {});+ const path = headers[':path'];+ record({ event: 'request', path, method: headers[':method'], key: headers['x-api-key'], version: headers['anthropic-version'], accept: headers.accept, session: sessions.get(stream.session), stream: stream.id });+ if (path === '/stall') { stream.respond({ ':status': 200 }); stream.write('{'); return; }+ if (path === '/deny') { stream.respond({ ':status': 401 }); stream.end('private error'); return; }+ if (path === '/redirect') { stream.respond({ ':status': 302, location: '/v1/models?limit=1000' }); stream.end(); return; }+ if (path === '/cross-redirect') { stream.respond({ ':status': 302, location: `https://localhost:${h1.address().port}/v1/models?limit=1000` }); stream.end(); return; }+ if (path === '/cross-loop') { stream.respond({ ':status': 302, location: `https://localhost:${h1.address().port}/cross-loop` }); stream.end(); return; }+ stream.respond({ ':status': 200, 'content-type': 'application/json' });+ if (path === '/oversize') { stream.end('x'.repeat(2048)); return; }+ if (path === '/large-valid') { stream.end(JSON.stringify({ data: [{ id: 'large', description: 'x'.repeat(3 * 1024 * 1024) }] })); return; }+ if (path.includes('after_id=first')) { stream.end('{"data":[{"id":"second"}],"has_more":false}'); return; }+ stream.end('{"data":[{"id":"first"}],"has_more":true,"last_id":"first"}');+});+const h1 = https.createServer({ key: fs.readFileSync(key), cert: fs.readFileSync(cert) }, (req, res) => {+ record({ event: 'h1_request', path: req.url, method: req.method, key: req.headers['x-api-key'], version: req.headers['anthropic-version'], accept: req.headers.accept });+ if (req.url === '/redirect') { res.writeHead(302, { location: '/v1/models?limit=1000' }); res.end(); return; }+ if (req.url === '/cross-redirect') { res.writeHead(302, { location: `https://localhost:${server.address().port}/v1/models?limit=1000` }); res.end(); return; }+ if (req.url === '/cross-loop') { res.writeHead(302, { location: `https://localhost:${server.address().port}/cross-loop` }); res.end(); return; }+ if (req.url === '/deny-stall') { res.writeHead(401); res.write('private error'); return; }+ if (req.url === '/oversize') { res.end('x'.repeat(2048)); return; }+ res.setHeader('content-type', 'application/json');+ res.end('{"data":[{"id":"first"}],"has_more":false}');+});+server.listen(0, () => h1.listen(0, () => process.stdout.write(`${server.address().port} ${h1.address().port}\n`)));diff --git a/scripts/test-catalog-http2.py b/scripts/test-catalog-http2.pynew file mode 100644index 00000000..b645e421--- /dev/null+++ b/scripts/test-catalog-http2.py@@ -0,0 +1,61 @@+#!/usr/bin/env python3+"""Offline authenticated catalog GET over TLS/ALPN h2 with a pinned test CA."""+import importlib.util+import json+import os+from pathlib import Path+import queue+import subprocess+import tempfile+import threading++REPO = Path(__file__).resolve().parents[1]+spec = importlib.util.spec_from_file_location('h2_fixture', REPO/'scripts/test-acp-http2.py')+fixture = importlib.util.module_from_spec(spec)+spec.loader.exec_module(fixture)+++def exercise(root):+ fixture.certificates(root)+ fixture.build(root) # verifies the pin and injects only the fixture CA+ log = root/'server.jsonl'+ log.touch()+ with (root/'server-stderr').open('w') as stderr:+ server = subprocess.Popen(['node', str(REPO/'scripts/eval/catalog-http2.cjs'), str(root/'server.key'), str(root/'server.pem'), str(log)],+ stdout=subprocess.PIPE, stderr=stderr, text=True, start_new_session=True)+ try:+ ports = queue.Queue()+ threading.Thread(target=lambda: ports.put(server.stdout.readline()), daemon=True).start()+ h2_port, h1_port = map(int, ports.get(timeout=10).split())+ env = dict(os.environ, GRAFF_CATALOG_HTTP2_URL=f'https://localhost:{h2_port}',+ GRAFF_CATALOG_H1_URL=f'https://localhost:{h1_port}',+ GRAFF_CATALOG_CA_CERT=str(root/'ca.pem'))+ try:+ result = fixture.run(['zig', 'build', 'test', '--system', str(root/'packages'), '--cache-dir', str(root/'test-cache'),+ '-Dtest-filter=loopback catalog HTTP2', '--summary', 'all'], cwd=REPO, env=env, timeout=240)+ except RuntimeError:+ print('server log:', log.read_text(), flush=True)+ print('server stderr:', (root/'server-stderr').read_text(), flush=True)+ raise+ assert '1 skipped' not in result.stderr, result.stderr+ rows = [json.loads(line) for line in log.read_text().splitlines()]+ sessions = [row for row in rows if row['event'] == 'session']+ requests = [row for row in rows if row['event'] == 'request']+ h1_requests = [row for row in rows if row['event'] == 'h1_request']+ assert sessions and all(row['alpn'] == 'h2' for row in sessions), rows+ assert [row['path'] for row in requests] == ['/v1/models?limit=1000', '/v1/models?limit=1000&after_id=first', '/redirect', '/v1/models?limit=1000', '/cross-redirect', '/cross-loop', '/cross-loop', '/deny', '/oversize', '/large-valid', '/stall', '/v1/models?limit=1000'], rows+ assert all(row['method'] == 'GET' and row['key'] == 'fixture-key' and row['version'] == '2023-06-01' and row['accept'] == 'application/json' for row in requests[:6]), rows+ assert all(row.get('key') is None and row.get('version') is None and row['accept'] == 'application/json' for row in (requests[6], requests[-1])), rows+ assert all(row['key'] == 'fixture-key' and row['version'] == '2023-06-01' for row in requests[7:-1]), rows+ assert requests[0]['session'] == requests[1]['session'] and [row['stream'] for row in requests[:2]] == [1, 3], rows+ assert [row['path'] for row in h1_requests] == ['/v1/models?limit=1000', '/cross-loop', '/cross-loop', '/v1/models?limit=1000', '/redirect', '/v1/models?limit=1000', '/cross-redirect', '/deny-stall', '/oversize'], rows+ assert all(row['method'] == 'GET' and row['key'] == 'fixture-key' and row['version'] == '2023-06-01' for row in h1_requests[3:]), rows+ assert all(row.get('key') is None and row.get('version') is None and row['accept'] == 'application/json' for row in h1_requests[:3]), rows+ print('PASS catalog HTTPS: auth, cursor, redirects, pooled h2, TLS h1 fallback, status-first errors, large page, deadline', flush=True)+ finally:+ fixture.terminate(server)+++if __name__ == '__main__':+ with tempfile.TemporaryDirectory(prefix='catalog-http2-') as name:+ exercise(Path(name))diff --git a/src/http2_buffered.zig b/src/http2_buffered.zigindex 8c53f7c9..28284f3e 100644--- a/src/http2_buffered.zig+++ b/src/http2_buffered.zig@@ -1,4 +1,4 @@-//! Bounded HTTPS JSON POST. HTTP/2 streams hold an exclusive pool lease;+//! Bounded HTTPS JSON requests. HTTP/2 streams hold an exclusive pool lease;//! HTTP/1.1 is used only before an HTTP/2 request was sent (dial/ALPN).const std = @import("std");const Io = std.Io;@@ -8,6 +8,11 @@ const pool = @import("http2_pool.zig");pub const max_body = 64 * 1024;pub const Response = struct { status: u16, body: []u8 };+const Method = enum { post, get };++fn redirects(status: u16) bool {+ return status == 301 or status == 302 or status == 303 or status == 307 or status == 308;+}fn authority(url: []const u8) []const u8 {const rest = url["https://".len..]; // caller already passed parseHttpsUrl@@ -22,30 +27,64 @@ const Exchange = struct {bearer: []const u8,body: []const u8,limit: usize,+ method: Method = .post,+ headers: []const std.http.Header = &.{},+ redirected_headers: [4]std.http.Header = undefined,+ large_bytes: ?[]u8 = null,+ redirect_location: ?[]u8 = null,status: u16 = 0,len: usize = 0,bytes: [max_body]u8 = undefined,test_task: ?*const fn (*Exchange) anyerror!void = null,+ fn buffer(self: *Exchange) []u8 {+ return self.large_bytes orelse &self.bytes;+ }+fn append(self: *Exchange, chunk: []const u8) !void {if (chunk.len > self.limit - self.len) return error.ResponseTooLarge;- @memcpy(self.bytes[self.len..][0..chunk.len], chunk);+ @memcpy(self.buffer()[self.len..][0..chunk.len], chunk);self.len += chunk.len;}fn run(self: *Exchange) !void {if (self.test_task) |task| return task(self);- if (pool.want(self.url)) {- const origin = try pool.parseOrigin(self.url);- if (!pool.knownH1(self.io, origin.host, origin.port)) {- if (try self.tryH2(origin)) return;+ var owned_url: ?[]u8 = null;+ defer if (owned_url) |url| self.gpa.free(url);+ defer if (self.redirect_location) |location| self.gpa.free(location);+ for (0..4) |hops| {+ self.status = 0;+ self.len = 0;+ var sent_h2 = false;+ if (pool.want(self.url)) {+ const origin = try pool.parseOrigin(self.url);+ if (!pool.knownH1(self.io, origin.host, origin.port)) {+ sent_h2 = try self.tryH2(origin);+ }+ }+ if (!sent_h2) try self.runH1();+ if (self.method != .get or !redirects(self.status) or self.redirect_location == null) return;+ if (hops == 3) return error.TooManyHttpRedirects;+ const next = try resolveLocation(self.gpa, self.url, self.redirect_location.?);+ self.gpa.free(self.redirect_location.?);+ self.redirect_location = null;+ if (!sameOrigin(self.url, next)) {+ var count: usize = 0;+ for (self.headers) |header| {+ if (!std.ascii.eqlIgnoreCase(header.name, "accept")) continue;+ self.redirected_headers[count] = header;+ count += 1;+ }+ self.headers = self.redirected_headers[0..count];}+ if (owned_url) |old| self.gpa.free(old);+ owned_url = next;+ self.url = next;}- try self.runH1();}- /// Returns false only before startLines has been called. Errors after it- /// may follow a complete upload and must never silently resend on H1.+ /// Returns false only before the request is sent. http-zig's H1NoStream+ /// exits before Conn.startLines; every other startLines error is final.fn tryH2(self: *Exchange, origin: pool.Origin) !bool {const lease = pool.acquire(self.gpa, self.io, origin.host, origin.port) catch |err| {if (h2.session.handshakeFallback(err)) return false; // pre-send TLS failure@@ -57,19 +96,31 @@ const Exchange = struct {pool.noteH1(self.io, origin.host, origin.port);return false; // ALPN did not select h2}- const headers = [_]h2.Header{+ const post_headers = [_]h2.Header{.{ .name = "content-type", .value = "application/json" },.{ .name = "accept", .value = "application/json" },.{ .name = "authorization", .value = self.bearer },};- var stream = try lease.session.startLines(.{- .method = "POST",+ var catalog_headers: [4]h2.Header = undefined;+ var names: [4][64]u8 = undefined;+ for (self.headers, 0..) |header, i| {+ if (i >= catalog_headers.len or header.name.len > names[i].len) return error.InvalidHeader;+ catalog_headers[i] = .{ .name = std.ascii.lowerString(&names[i], header.name), .value = header.value };+ }+ var stream = lease.session.startLines(.{+ .method = if (self.method == .post) "POST" else "GET",.scheme = "https",.authority = authority(self.url),.path = origin.path,- .extra = &headers,+ .extra = if (self.method == .post) &post_headers else catalog_headers[0..self.headers.len],.body = self.body,- });+ }) catch |err| {+ if (err == error.H1NoStream) {+ pool.noteH1(self.io, origin.host, origin.port);+ return false;+ }+ return err;+ };defer stream.deinit();try self.consume(&stream);keep = stream.ended and lease.session.reusable();@@ -78,6 +129,9 @@ const Exchange = struct {fn consume(self: *Exchange, stream: *h2.LineStream) !void {self.status = try stream.waitStatus();+ if (self.method == .get and redirects(self.status)) {+ if (stream.header("location")) |location| self.redirect_location = try self.gpa.dupe(u8, location);+ }if (self.status != 200) return; // no error body crosses this boundaryvar chunk: [4096]u8 = undefined;while (true) {@@ -88,6 +142,29 @@ const Exchange = struct {}fn runH1(self: *Exchange) !void {+ if (self.method == .get) {+ var req = try self.client.request(.GET, try std.Uri.parse(self.url), .{+ .redirect_behavior = .unhandled,+ .extra_headers = self.headers,+ });+ defer req.deinit();+ errdefer if (req.connection) |conn| {+ conn.closing = true;+ };+ try req.sendBodiless();+ var response = try req.receiveHead(&.{});+ self.status = @intFromEnum(response.head.status);+ if (self.status != 200) {+ if (redirects(self.status)) {+ if (response.head.location) |location|+ self.redirect_location = try self.gpa.dupe(u8, location);+ }+ if (req.connection) |conn| conn.closing = true;+ return;+ }+ try self.readH1Body(&response);+ return;+ }var req = try self.client.request(.POST, try std.Uri.parse(self.url), .{.redirect_behavior = .unhandled,.headers = .{@@ -107,6 +184,10 @@ const Exchange = struct {if (req.connection) |conn| conn.closing = true;return;}+ try self.readH1Body(&response);+ }++ fn readH1Body(self: *Exchange, response: *std.http.Client.Response) !void {const dbuf: []u8 = switch (response.head.content_encoding) {.identity => &.{},.zstd => try self.gpa.alloc(u8, std.compress.zstd.default_window_len),@@ -117,12 +198,41 @@ const Exchange = struct {var tbuf: [64]u8 = undefined;var dec: std.http.Decompress = undefined;const reader = response.readerDecompressing(&tbuf, &dec, dbuf);- var writer = Io.Writer.fixed(self.bytes[0..self.limit]);- _ = try reader.streamRemaining(&writer);+ var writer = Io.Writer.fixed(self.buffer()[0..self.limit]);+ _ = reader.streamRemaining(&writer) catch |err| {+ if (err == error.WriteFailed) return error.ResponseTooLarge;+ return err;+ };self.len = writer.buffered().len;}};+fn sameOrigin(a_url: []const u8, b_url: []const u8) bool {+ const a = std.Uri.parse(a_url) catch return false;+ const b = std.Uri.parse(b_url) catch return false;+ if (!std.ascii.eqlIgnoreCase(a.scheme, b.scheme)) return false;+ var a_buf: [std.Io.net.HostName.max_len]u8 = undefined;+ var b_buf: [std.Io.net.HostName.max_len]u8 = undefined;+ const a_host = std.Io.net.HostName.fromUri(a, &a_buf) catch return false;+ const b_host = std.Io.net.HostName.fromUri(b, &b_buf) catch return false;+ const default_port: u16 = if (std.ascii.eqlIgnoreCase(a.scheme, "https")) 443 else 80;+ return a_host.eql(b_host) and (a.port orelse default_port) == (b.port orelse default_port);+}++fn resolveLocation(gpa: Allocator, current: []const u8, location: []const u8) ![]u8 {+ const base = try std.Uri.parse(current);+ const capacity = current.len + location.len * 2 + 16;+ var buffer = try gpa.alloc(u8, capacity);+ defer gpa.free(buffer);+ @memcpy(buffer[0..location.len], location);+ var spare = buffer;+ const resolved = try base.resolveInPlace(location.len, &spare);+ var out: Io.Writer.Allocating = .init(gpa);+ errdefer out.deinit();+ try resolved.format(&out.writer);+ return out.toOwnedSlice();+}+fn deadline(io: Io, ms: u64) void {io.sleep(.fromMilliseconds(@intCast(@min(ms, std.math.maxInt(i64)))), .awake) catch {};}@@ -158,6 +268,99 @@ pub fn post(transport_gpa: Allocator, result_allocator: Allocator, io: Io, clienreturn .{ .status = ex.status, .body = try result_allocator.dupe(u8, ex.bytes[0..ex.len]) };}+/// `transport_gpa` owns pooled connections. `result_allocator` may be a+/// request arena; the bounded body is copied into it after the worker joins.+pub fn get(transport_gpa: Allocator, result_allocator: Allocator, io: Io, client: *std.http.Client, url: []const u8, headers: []const std.http.Header, limit: usize, deadline_ms: u64) !Response {+ if (limit == 0) return error.EmptyBuffer;+ const buffer = try transport_gpa.alloc(u8, limit);+ defer transport_gpa.free(buffer);+ var ex: Exchange = .{ .gpa = transport_gpa, .io = io, .client = client, .url = url, .bearer = "", .body = "", .limit = limit, .method = .get, .headers = headers, .large_bytes = buffer };+ try watched(&ex, deadline_ms);+ return .{ .status = ex.status, .body = try result_allocator.dupe(u8, buffer[0..ex.len]) };+}++test "loopback catalog HTTP2 GET preserves headers, pages, status, size, and deadline" {+ const value = std.c.getenv("GRAFF_CATALOG_HTTP2_URL") orelse return error.SkipZigTest;+ const base = std.mem.span(value);+ const io = std.testing.io;+ const saved = @import("main.zig").g_http2;+ defer @import("main.zig").g_http2 = saved;+ @import("main.zig").g_http2 = true;+ defer pool.shutdown(io);+ var client: std.http.Client = .{ .allocator = std.testing.allocator, .io = io };+ defer client.deinit();+ const ca_value = std.c.getenv("GRAFF_CATALOG_CA_CERT") orelse return error.SkipZigTest;+ const now = Io.Clock.real.now(io);+ try client.ca_bundle.rescan(std.testing.allocator, io, now);+ try client.ca_bundle.addCertsFromFilePathAbsolute(std.testing.allocator, io, now, std.mem.span(ca_value));+ client.now = now;+ const headers = [_]std.http.Header{+ .{ .name = "Accept", .value = "application/json" },+ .{ .name = "x-api-key", .value = "fixture-key" },+ .{ .name = "anthropic-version", .value = "2023-06-01" },+ };+ var arena_state = std.heap.ArenaAllocator.init(std.testing.allocator);+ defer arena_state.deinit();+ const arena = arena_state.allocator();+ const first_url = try std.fmt.allocPrint(arena, "{s}/v1/models?limit=1000", .{base});+ const first = try get(std.testing.allocator, arena, io, &client, first_url, &headers, 2 * 1024 * 1024, 2000);+ try std.testing.expectEqual(@as(u16, 200), first.status);+ try std.testing.expect(std.mem.indexOf(u8, first.body, "\"first\"") != null);+ const second_url = try std.fmt.allocPrint(arena, "{s}/v1/models?limit=1000&after_id=first", .{base});+ const second = try get(std.testing.allocator, arena, io, &client, second_url, &headers, 2 * 1024 * 1024, 2000);+ try std.testing.expectEqual(@as(u16, 200), second.status);+ try std.testing.expect(std.mem.indexOf(u8, second.body, "\"second\"") != null);+ const redirect_url = try std.fmt.allocPrint(arena, "{s}/redirect", .{base});+ const redirected = try get(std.testing.allocator, arena, io, &client, redirect_url, &headers, 512, 2000);+ try std.testing.expectEqual(@as(u16, 200), redirected.status);+ try std.testing.expect(std.mem.indexOf(u8, redirected.body, "\"first\"") != null);+ const cross_url = try std.fmt.allocPrint(arena, "{s}/cross-redirect", .{base});+ const crossed = try get(std.testing.allocator, arena, io, &client, cross_url, &headers, 512, 2000);+ try std.testing.expectEqual(@as(u16, 200), crossed.status);+ const loop_url = try std.fmt.allocPrint(arena, "{s}/cross-loop", .{base});+ try std.testing.expectError(error.TooManyHttpRedirects, get(std.testing.allocator, arena, io, &client, loop_url, &headers, 512, 2000));+ const denied_url = try std.fmt.allocPrint(arena, "{s}/deny", .{base});+ const denied = try get(std.testing.allocator, arena, io, &client, denied_url, &headers, 512, 2000);+ try std.testing.expectEqual(@as(u16, 401), denied.status);+ try std.testing.expectEqual(@as(usize, 0), denied.body.len);+ const large_url = try std.fmt.allocPrint(arena, "{s}/oversize", .{base});+ try std.testing.expectError(error.ResponseTooLarge, get(std.testing.allocator, arena, io, &client, large_url, &headers, 512, 2000));+ const valid_url = try std.fmt.allocPrint(arena, "{s}/large-valid", .{base});+ const valid = try get(std.testing.allocator, arena, io, &client, valid_url, &headers, 16 * 1024 * 1024, 15_000);+ try std.testing.expect(valid.body.len > 2 * 1024 * 1024);+ const parsed = @import("router_catalog.zig").parseModels(arena, "anthropic", valid.body) orelse return error.ExpectedValidCatalog;+ try std.testing.expectEqual(@as(usize, 1), parsed.models.len);+ try std.testing.expectEqualStrings("large", parsed.models[0].name);+ const stall_url = try std.fmt.allocPrint(arena, "{s}/stall", .{base});+ try std.testing.expectError(error.DeadlineExceeded, get(std.testing.allocator, arena, io, &client, stall_url, &headers, 512, 100));+ const h1_value = std.c.getenv("GRAFF_CATALOG_H1_URL") orelse return error.SkipZigTest;+ const h1_url = try std.fmt.allocPrint(arena, "{s}/v1/models?limit=1000", .{std.mem.span(h1_value)});+ const h1_response = try get(std.testing.allocator, arena, io, &client, h1_url, &headers, 512, 2000);+ try std.testing.expectEqual(@as(u16, 200), h1_response.status);+ try std.testing.expect(std.mem.indexOf(u8, h1_response.body, "\"first\"") != null);+ const h1_redirect_url = try std.fmt.allocPrint(arena, "{s}/redirect", .{std.mem.span(h1_value)});+ const h1_redirected = try get(std.testing.allocator, arena, io, &client, h1_redirect_url, &headers, 512, 2000);+ try std.testing.expectEqual(@as(u16, 200), h1_redirected.status);+ const h1_cross_url = try std.fmt.allocPrint(arena, "{s}/cross-redirect", .{std.mem.span(h1_value)});+ const h1_crossed = try get(std.testing.allocator, arena, io, &client, h1_cross_url, &headers, 512, 2000);+ try std.testing.expectEqual(@as(u16, 200), h1_crossed.status);+ const h1_denied_url = try std.fmt.allocPrint(arena, "{s}/deny-stall", .{std.mem.span(h1_value)});+ const h1_denied = try get(std.testing.allocator, arena, io, &client, h1_denied_url, &headers, 512, 500);+ try std.testing.expectEqual(@as(u16, 401), h1_denied.status);+ try std.testing.expectEqual(@as(usize, 0), h1_denied.body.len);+ const h1_oversize_url = try std.fmt.allocPrint(arena, "{s}/oversize", .{std.mem.span(h1_value)});+ try std.testing.expectError(error.ResponseTooLarge, get(std.testing.allocator, arena, io, &client, h1_oversize_url, &headers, 512, 2000));+}++test "catalog redirect origin includes scheme, host, and port" {+ try std.testing.expect(sameOrigin("https://example.test/models", "https://EXAMPLE.test:443/next"));+ try std.testing.expect(!sameOrigin("https://example.test/models", "https://example.test:444/next"));+ try std.testing.expect(!sameOrigin("https://example.test/models", "http://example.test/next"));+ try std.testing.expect(!sameOrigin("https://example.test/models", "https://other.test/next"));+ try std.testing.expect(!redirects(304));+ try std.testing.expect(redirects(308));+}+test "buffered response rejects growth past limit before copying" {var client: std.http.Client = undefined;var ex: Exchange = .{ .gpa = std.testing.allocator, .io = std.testing.io, .client = &client, .url = "", .bearer = "", .body = "", .limit = 4 };diff --git a/src/router_catalog.zig b/src/router_catalog.zigindex b89f3e3a..d3735a79 100644--- a/src/router_catalog.zig+++ b/src/router_catalog.zig@@ -19,9 +19,11 @@ const pricing_db = @import("pricing_db.zig"); // #557: LiteLLM price/context oveconst provider = @import("provider.zig");const serde = @import("serde.zig");const util = @import("util.zig");+const buffered_http = @import("http2_buffered.zig");/// Router catalogs change on the order of weeks; favour instant startup.const cache_ttl_ms: i64 = 6 * 60 * 60 * 1000;+const catalog_page_limit = 16 * 1024 * 1024; // Full provider lists may be much larger than the picker response.var attempted: [provider.provider_specs.len]bool = @splat(false);var additional_attempted = false;@@ -264,17 +266,13 @@ fn containsName(rows: []const pricing.ModelInfo, name: []const u8) bool {fn fetchPage(io: Io, gpa: Allocator, arena: Allocator, spec: provider.ProviderSpec, key: []const u8, source: provider.Keys.CredentialSource, url: []const u8) ?Snapshot {var client: std.http.Client = .{ .allocator = gpa, .io = io };defer client.deinit();- var aw: Io.Writer.Allocating = .init(arena);var headers_buf: [4]std.http.Header = undefined;const headers = catalogHeaders(arena, spec, key, source, &headers_buf) orelse return null;- const res = client.fetch(.{- .location = .{ .url = url },- .method = .GET,- .response_writer = &aw.writer,- .extra_headers = headers,- }) catch return null;- if (@intFromEnum(res.status) != 200) return null;- return parseModels(arena, spec.id, aw.writer.buffered());+ // Each page is independently bounded; a later failure leaves any valid+ // earlier pages in fetch()'s prefix, and cached rows remain offline fallback.+ const res = buffered_http.get(gpa, arena, io, &client, url, headers, catalog_page_limit, 15_000) catch return null;+ if (res.status != 200) return null;+ return parseModels(arena, spec.id, res.body);}/// Fetch the COMPLETE model list. The Models API pages with after_id +@@ -283,7 +281,7 @@ fn fetchPage(io: Io, gpa: Allocator, arena: Allocator, spec: provider.ProviderSp/// A failed later page keeps the rows already gathered rather than/// discarding a valid prefix; the page cap only guards against a server/// that always answers has_more.-fn fetch(io: Io, gpa: Allocator, arena: Allocator, spec: provider.ProviderSpec, key: []const u8, source: provider.Keys.CredentialSource) ?Snapshot {+pub fn fetch(io: Io, gpa: Allocator, arena: Allocator, spec: provider.ProviderSpec, key: []const u8, source: provider.Keys.CredentialSource) ?Snapshot {const base = modelsUrl(spec);if (base.len == 0) return null;var rows: std.ArrayList(pricing.ModelInfo) = .empty;diff --git a/src/router_catalog_tests.zig b/src/router_catalog_tests.zigindex 201d73d5..163e6113 100644--- a/src/router_catalog_tests.zig+++ b/src/router_catalog_tests.zig@@ -339,6 +339,57 @@ test "Models-API cursor pagination is followed 1:1 with the docs" {try std.testing.expectEqualStrings("base", pageUrl(arena, "base", null) orelse return error.TestUnexpectedResult);}+test "catalog HTTP/1.1 keeps auth, cursor pagination, and a valid prefix after a failed page" {+ const io = std.testing.io;+ var address = try std.Io.net.IpAddress.parseLiteral("127.0.0.1:0");+ var server = try std.Io.net.IpAddress.listen(&address, io, .{});+ defer server.deinit(io);+ var group: std.Io.Group = .init;+ defer group.cancel(io);+ try group.concurrent(io, serveCatalogPages, .{ io, &server });+ var state = std.heap.ArenaAllocator.init(std.testing.allocator);+ defer state.deinit();+ var spec = provider.specFor("anthropic") orelse return error.TestUnexpectedResult;+ spec.models_url = try std.fmt.allocPrint(state.allocator(), "http://127.0.0.1:{d}/v1/models?limit=1000", .{server.socket.address.getPort()});+ const complete = rc.fetch(io, std.testing.allocator, state.allocator(), spec, "test-key", .environment) orelse return error.TestUnexpectedResult;+ try std.testing.expectEqual(@as(usize, 2), complete.models.len);+ try std.testing.expectEqualStrings("first", complete.models[0].name);+ try std.testing.expectEqualStrings("second", complete.models[1].name);+ const prefix = rc.fetch(io, std.testing.allocator, state.allocator(), spec, "test-key", .environment) orelse return error.TestUnexpectedResult;+ try std.testing.expectEqual(@as(usize, 1), prefix.models.len);+ try std.testing.expectEqualStrings("first", prefix.models[0].name);+ const retried = rc.fetch(io, std.testing.allocator, state.allocator(), spec, "test-key", .environment) orelse return error.TestUnexpectedResult;+ try std.testing.expectEqual(@as(usize, 2), retried.models.len);+ try std.testing.expectEqualStrings("second", retried.models[1].name);+}++fn serveCatalogPages(io: std.Io, server: *std.Io.net.Server) void {+ const first = "{\"data\":[{\"id\":\"first\"}],\"has_more\":true,\"last_id\":\"first\"}";+ const second = "{\"data\":[{\"id\":\"second\"}],\"has_more\":false}";+ for (0..6) |i| {+ const stream = server.accept(io) catch return;+ defer stream.close(io);+ var read_buffer: [4096]u8 = undefined;+ var reader = std.Io.net.Stream.Reader.init(stream, io, &read_buffer);+ const line = reader.interface.takeDelimiter('\n') catch return;+ const expected = if (i % 2 == 0) "GET /v1/models?limit=1000 HTTP/1.1\r" else "GET /v1/models?limit=1000&after_id=first HTTP/1.1\r";+ if (!std.mem.eql(u8, line orelse return, expected)) return;+ var key = false;+ var version = false;+ while (reader.interface.takeDelimiter('\n') catch null) |header| {+ if (std.ascii.eqlIgnoreCase(header, "x-api-key: test-key\r")) key = true;+ if (std.ascii.eqlIgnoreCase(header, "anthropic-version: " ++ @import("main.zig").anthropic_version ++ "\r")) version = true;+ if (std.mem.eql(u8, header, "\r") or header.len == 0) break;+ }+ if (!key or !version) return;+ const body = if (i % 2 == 1) second else first;+ var write_buffer: [4096]u8 = undefined;+ var writer = std.Io.net.Stream.Writer.init(stream, io, &write_buffer);+ writer.interface.print("HTTP/1.1 {s}\r\nContent-Length: {d}\r\nConnection: close\r\n\r\n{s}", .{ if (i == 3) "500 Error" else "200 OK", body.len, body }) catch return;+ writer.interface.flush() catch return;+ }+}+test "router discovery replaces only its provider slice" {const saved = pricing.active_model_table;defer pricing.active_model_table = saved;diff --git a/src/startup_tests.zig b/src/startup_tests.zigindex c9c1eb7e..d3398a87 100644--- a/src/startup_tests.zig+++ b/src/startup_tests.zig@@ -38,6 +38,19 @@ test "resolveKeys does not load Kimi/xAI OAuth credentials for an explicit unrelvar real_buf: [std.fs.max_path_bytes]u8 = undefined;const real_len = try tmp.dir.realPath(io, &real_buf);const home = try arena.dupe(u8, real_buf[0..real_len]);+ const pricing = @import("pricing.zig");+ const saved_models = pricing.active_model_table;+ defer pricing.active_model_table = saved_models;+ defer @import("http2_pool.zig").shutdown(io);+ defer @import("router_catalog.zig").shutdown(io);++ // This test checks credential scope, so seed a fresh local catalog. It+ // must not turn a synthetic key into a real provider GET during tier 1.+ const catalog = @import("router_catalog.zig");+ const spec = @import("provider.zig").specFor("codegraff") orelse return error.TestUnexpectedResult;+ const cached_rows = [_]pricing.ModelInfo{.{ .provider = spec.id, .name = spec.default_model, .context = 128_000 }};+ const cached = catalog.cacheDocument(io, arena, spec, &cached_rows) orelse return error.TestUnexpectedResult;+ try tmp.dir.writeFile(io, .{ .sub_path = ".codegraff-codegraff-models.json", .data = cached });try tmp.dir.createDirPath(io, ".kimi/credentials");const now_s = @divTrunc(Io.Timestamp.now(io, .real).nanoseconds, 1_000_000_000);