graff — a fast agentic coding harness in Zig: multi-provider, MCP, workflows, DGM evolution loop, TS/Python SDKs
Python
remote-control: security audit fixes
Rach Pradhanbe53e19968e3
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex 5cfa3644..f2e52662 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -24,9 +24,11 @@ current is part of cutting a release.their sessions; `graff remote new` / `send` / `answer` / `tail` / `cancel`/ `close` drive them, with the same NDJSON events a serve client gets. Themachine keeps the complete tape; the relay keeps a recent window and a- viewer that falls behind gets a replay from the machine. Gated on the- `sessions` key scope, so a plain `api` key can never reach a machine.- `docs/remote-control.md`.+ viewer that falls behind gets a replay from the machine. Gated on the new+ `remote` key scope, so neither a plain `api` key nor a history-only+ `sessions` key can reach a machine, and a viewer cannot start an+ unattended session unless the machine was started with `--yolo`.+ `docs/remote-control.md`, `docs/remote-control-security.md`.## v0.0.295diff --git a/docs/remote-control-security.md b/docs/remote-control-security.mdnew file mode 100644index 00000000..cf3fd507--- /dev/null+++ b/docs/remote-control-security.md@@ -0,0 +1,62 @@+# Remote control: security model++What remote control can and cannot do, who can make it do it, and the+bounds each side enforces. Companion to [remote-control.md](remote-control.md).++## What is at stake++A machine running `graff remote-control` will run turns on request, and a+turn can run tools: read and edit files, run commands. Whoever can reach the+machine through the relay can do what a person at its keyboard could do,+within what that person allowed. So the question is who can reach it.++## Who can reach a machine++- Only a Codegraff key with the **`remote` scope**, on the **same account**+ the machine signed in with. `graff login` keys carry it; dashboard keys+ get it only when the person issuing the key turns it on. A plain `api`+ key (the kind that ships inside a product) can never reach a machine, and+ neither can a `sessions` key (synced history is a different grant).+- Revoking the key ends the pipe: the machine's next poll is refused and it+ exits, telling you to sign in again.+- The relay is per account. A machine cannot be listed, created on, or+ driven from another account, and the relay never accepts an inbound+ connection to the machine; the machine only executes what arrives on the+ connection it opened.++## What the machine itself enforces++The relay carries requests; the machine is the last word.++- **Unattended sessions are the machine's call.** A viewer cannot start a+ `yolo` session unless the machine was started with `graff remote-control+ --yolo`. Without it, such a request is refused with an explanation, and+ sessions run with the normal tool approvals.+- The same flags that shape `graff serve` sessions (`--model`,+ `--system-prompt`, `--max-tool-calls`, ...) set the defaults for remote+ sessions and are chosen on the machine, not by the viewer.+- Command and session ids are validated before they touch the filesystem:+ a session name is one path segment under `.graff/`, never a walk out of it.+- Nothing is bound. There is no port, no LAN exposure, no pairing secret to+ type; the only credential is the account key already on the machine.++## What the relay enforces++- Request bodies and event lines are capped; an oversized line reaches+ viewers as an envelope-only stub (the machine's tape keeps the full line).+- Each session's recent window is bounded in bytes and count, the number of+ windows is bounded, and windows are freed when a session closes.+- A machine's command queue is bounded; past it a viewer gets 429.+- Only the machine that owns a session may push its events or answer its+ commands, even within one account.+- Results nobody is waiting for are not kept.++## Residual risks and follow-ups++- A `remote` key is as powerful as the machines it can reach. Treat it like+ an SSH key: never embed it, revoke it when a device is lost.+- Per-session, short-lived viewer tokens (shareable links) are not built+ yet; today every viewer holds the account key.+- The relay and the machine trust the gateway's TLS; `GRAFF_REMOTE_BASE` is+ a development knob and should never point at a plain-HTTP host you do not+ control.diff --git a/docs/remote-control.md b/docs/remote-control.mdindex 7676adb8..b3801f8e 100644--- a/docs/remote-control.md+++ b/docs/remote-control.md@@ -59,10 +59,16 @@ the connection it opened, and revoking the key ends the pipe.## Auth- Machine → gateway: the `graff login` device key. Remote control requires- the `sessions` scope (device keys have it; dashboard keys opt in), so a- plain `api` key embedded in a product can never drive anyone's machine.+ the `remote` scope (device keys have it; dashboard keys opt in), so a+ plain `api` key embedded in a product, or a `sessions` key that only reads+ history, can never drive anyone's machine. A refused key stops the machine.- Viewer → gateway: the same account login.- Gateway → machine: nothing inbound.+- Unattended (`yolo`) sessions are the machine's decision: a viewer can+ request one only if the machine was started with `--yolo`.++The full model, including what the relay bounds, is in+[remote-control-security.md](remote-control-security.md).`CODEGRAFF_API_KEY` overrides the login file on either side.`GRAFF_REMOTE_BASE` points both at a different gateway (a local one whilediff --git a/src/remote_control.zig b/src/remote_control.zigindex 2f11dd9f..adcb61d4 100644--- a/src/remote_control.zig+++ b/src/remote_control.zig@@ -10,9 +10,14 @@//!//! Sessions are serve's ServeSession (durable `--resume <id>` children with//! the seq-stamped tape under .graff/serve/); serve_create spawns them and the-//! drain here mirrors serveMessage with the socket replaced by coalesced-//! uploads. The relay keeps a bounded recent window; the tape here is complete,-//! so a viewer whose cursor fell behind asks for `reattach` and gets a replay.+//! drain here mirrors serveMessage with the socket replaced by the coalesced+//! uploads in remote_upload.zig. The relay keeps a bounded recent window; the+//! tape here is complete, so a viewer whose cursor fell behind asks for+//! `reattach` and gets a replay.+//!+//! Trust: the relay only carries what an account key with the `remote` scope+//! sent, and this process is the last word — a viewer cannot make it run an+//! unattended (`yolo`) session unless it was started with --yolo itself.const std = @import("std");const builtin = @import("builtin");@@ -24,6 +29,7 @@ const root = @import("main.zig");const serve = @import("serve.zig");const serve_create = @import("serve_create.zig");const events = @import("serve_events.zig");+const upload = @import("remote_upload.zig");const util = @import("util.zig");const ServeState = serve.ServeState;@@ -43,11 +49,6 @@ pub const Config = struct {pub const device_file = ".graff/remote-device.json";/// Longest one poll is held server-side before an empty answer.const poll_wait_ms: u64 = 25_000;-/// Event lines above this go upstream as a stub; the local tape stays complete.-pub const upload_line_cap: usize = 256 * 1024;-/// Coalesce uploads: a burst of text deltas becomes one POST per window.-const flush_window_ms: i64 = 60;-const flush_bytes: usize = 32 * 1024;const backoff_min_ms: i64 = 1_000;const backoff_max_ms: i64 = 30_000;@@ -75,34 +76,8 @@ fn hostName(buf: *[256]u8, from_env: ?[]const u8) []const u8 {return from_env orelse "unknown";}-const Reply = struct { code: u16, body: []const u8 };--/// `client` is per task: the poll loop owns one, every streamed request-/// another, so a turn's uploads never queue behind a 25s poll (or vice versa).-fn post(self: *State, client: *std.http.Client, arena: Allocator, path: []const u8, payload: []const u8) !Reply {- const url = try std.fmt.allocPrint(arena, "{s}{s}", .{ self.cfg.base, path });- var aw: Io.Writer.Allocating = .init(arena);- const res = try client.fetch(.{- .location = .{ .url = url },- .method = .POST,- .payload = payload,- .response_writer = &aw.writer,- .headers = .{- .content_type = .{ .override = "application/json" },- .authorization = .{ .override = try std.fmt.allocPrint(arena, "Bearer {s}", .{self.cfg.key}) },- .user_agent = .{ .override = "simple-harness/" ++ harness_version },- },- });- return .{ .code = @intFromEnum(res.status), .body = aw.writer.buffered() };-}--/// The gateway's error message out of a non-2xx body, or the raw body.-fn errorMessage(arena: Allocator, body: []const u8) []const u8 {- if (std.json.parseFromSliceLeaky(Value, arena, body, .{ .allocate = .alloc_always })) |v| {- if (v == .object) if (v.object.get("error")) |e| if (e == .object)- if (util.strFieldObj(e.object, "message")) |m| return m;- } else |_| {}- return body[0..@min(body.len, 200)];+fn link(self: *State, client: *std.http.Client) upload.Link {+ return .{ .io = self.st.io, .base = self.cfg.base, .key = self.cfg.key, .agent_id = self.agent_id, .client = client };}pub fn remoteControlMain(gpa: Allocator, io: Io, cfg: Config, exe: []const u8) !void {@@ -132,8 +107,8 @@ pub fn remoteControlMain(gpa: Allocator, io: Io, cfg: Config, exe: []const u8) !continue;};registered = true;- serve.serveLog(io, "graff remote-control · {s} ({s}) · device {s} · relay {s} · sessions are `{s} --json` children · Ctrl-C disconnects", .{- label, self.hostname, &self.agent_id, cfg.base, exe,+ serve.serveLog(io, "graff remote-control · {s} ({s}) · device {s} · relay {s} · unattended sessions: {s} · sessions are `{s} --json` children · Ctrl-C disconnects", .{+ label, self.hostname, &self.agent_id, cfg.base, if (cfg.serve.yolo) "allowed (--yolo)" else "refused (start with --yolo to allow)", exe,});}const cmds = poll(&self, arena) catch |err| {@@ -178,6 +153,13 @@ pub fn parseDeviceId(data: []const u8) ?[16]u8 {return out;}+/// The key was refused: revoked, or missing the `remote` scope. Nothing this+/// process can do fixes that, and retrying forever would just hammer the+/// gateway with a dead credential — exit and say what to run.+fn refused(arena: Allocator, code: u16, body: []const u8) noreturn {+ std.process.fatal("remote-control: gateway refused the key (HTTP {d}: {s}) — run `graff login` again (keys need the `remote` scope)", .{ code, upload.errorMessage(arena, body) });+}+fn register(self: *State, arena: Allocator, label: []const u8) !void {// AT_FDCWD has no path of its own; resolve "." through it instead.const cwd: []const u8 = Io.Dir.cwd().realPathFileAlloc(self.st.io, ".", arena) catch "";@@ -185,10 +167,10 @@ fn register(self: *State, arena: Allocator, label: []const u8) !void {var s: std.json.Stringify = .{ .writer = &aw.writer };try s.write(.{ .hostname = self.hostname, .name = label, .cwd = cwd, .version = harness_version });const path = try std.fmt.allocPrint(arena, "/v1/remote/agents/{s}/register", .{&self.agent_id});- const r = try post(self, &self.client, arena, path, aw.writer.buffered());- if (r.code == 401 or r.code == 403) std.process.fatal("remote-control: gateway refused the key (HTTP {d}: {s}) — run `graff login`", .{ r.code, errorMessage(arena, r.body) });+ const r = try upload.post(link(self, &self.client), arena, path, aw.writer.buffered());+ if (r.code == 401 or r.code == 403) refused(arena, r.code, r.body);if (r.code < 200 or r.code >= 300) {- serve.serveLog(self.st.io, "remote-control: HTTP {d}: {s}", .{ r.code, errorMessage(arena, r.body) });+ serve.serveLog(self.st.io, "remote-control: HTTP {d}: {s}", .{ r.code, upload.errorMessage(arena, r.body) });return error.RegisterFailed;}}@@ -205,10 +187,11 @@ pub const Command = struct {fn poll(self: *State, arena: Allocator) ![]const Command {const payload = try pollPayload(self, arena);const path = try std.fmt.allocPrint(arena, "/v1/remote/agents/{s}/poll", .{&self.agent_id});- const r = try post(self, &self.client, arena, path, payload);+ const r = try upload.post(link(self, &self.client), arena, path, payload);+ if (r.code == 401 or r.code == 403) refused(arena, r.code, r.body);if (r.code == 404) return error.UnknownDevice;if (r.code < 200 or r.code >= 300) {- serve.serveLog(self.st.io, "remote-control: HTTP {d}: {s}", .{ r.code, errorMessage(arena, r.body) });+ serve.serveLog(self.st.io, "remote-control: HTTP {d}: {s}", .{ r.code, upload.errorMessage(arena, r.body) });return error.PollFailed;}return parseCommands(arena, r.body);@@ -263,10 +246,23 @@ pub fn parseCommands(arena: Allocator, text: []const u8) ![]const Command {return out.items;}+/// Does a create request ask for an unattended session? Only a real JSON+/// `true` counts; anything unparseable reads as "no".+pub fn yoloRequested(arena: Allocator, body: []const u8) bool {+ const v = std.json.parseFromSliceLeaky(Value, arena, body, .{ .allocate = .alloc_always }) catch return false;+ if (v != .object) return false;+ const y = v.object.get("yolo") orelse return false;+ return y == .bool and y.bool;+}+fn runCommand(self: *State, arena: Allocator, cmd: Command) void {const io = self.st.io;const client = &self.client;if (std.mem.eql(u8, cmd.kind, "create")) {+ // The machine's own flag is the ceiling: a viewer cannot escalate a+ // session past what whoever started this process allowed.+ if (yoloRequested(arena, cmd.body) and !self.cfg.serve.yolo)+ return sendResult(self, client, arena, cmd.id, null, 403, "{\"error\":\"this machine refuses unattended sessions — start `graff remote-control --yolo` there to allow them\"}");const made = serve_create.createFromBody(&self.st, arena, cmd.body) catch |err| {serve.serveLog(io, "remote-control: create failed ({t})", .{err});return sendResult(self, client, arena, cmd.id, null, 500, "{\"error\":\"failed to create session\"}");@@ -287,7 +283,7 @@ fn runCommand(self: *State, arena: Allocator, cmd: Command) void {// A pure reconnect replays the tape and sends nothing to the child — and// works for a session this supervisor never ran, straight off the disk.if (std.mem.eql(u8, rtype, "reattach")) {- var up = Uploader.init(self, client, arena, cmd.id, sid);+ var up = upload.Uploader.init(link(self, client), arena, cmd.id, sid);replayTape(self, arena, &up, sid, from orelse 1);return up.finish(200, "{\"ok\":true,\"type\":\"reattach\"}");}@@ -372,7 +368,7 @@ fn drain(self: *State, client: *std.http.Client, arena: Allocator, s: *ServeSess// Our own copy of the name: `s` may be closed and freed by another task// the moment busy is released below, and finish() still names it.const sid = arena.dupe(u8, s.name) catch s.name;- var up = Uploader.init(self, client, arena, cmd_id, sid);+ var up = upload.Uploader.init(link(self, client), arena, cmd_id, sid);{s.busy.lockUncancelable(io); // serialize requests per sessiondefer s.busy.unlock(io);@@ -404,7 +400,7 @@ fn drain(self: *State, client: *std.http.Client, arena: Allocator, s: *ServeSess/// The child died mid-request: a terminal error with the next seq goes on the/// tape and upstream, so a viewer sees the failure, not a hole.-fn abort(s: *ServeSession, up: *Uploader, message: []const u8, dead: *bool) void {+fn abort(s: *ServeSession, up: *upload.Uploader, message: []const u8, dead: *bool) void {s.last_seq += 1;var buf: [256]u8 = undefined;const line = std.fmt.bufPrint(&buf, "{{\"seq\":{d},\"type\":\"error\",\"message\":\"{s}\"}}", .{ s.last_seq, message }) catch@@ -415,7 +411,7 @@ fn abort(s: *ServeSession, up: *Uploader, message: []const u8, dead: *bool) void}/// Every complete tape line with seq >= from, in order.-fn replayTape(self: *State, arena: Allocator, up: *Uploader, sid: []const u8, from: u64) void {+fn replayTape(self: *State, arena: Allocator, up: *upload.Uploader, sid: []const u8, from: u64) void {const path = events.logPath(arena, sid) catch return;const data = Io.Dir.cwd().readFileAlloc(self.st.io, path, arena, .limited(events.max_log_bytes)) catch return;var rest = data;@@ -456,84 +452,10 @@ fn closeSession(self: *State, client: *std.http.Client, arena: Allocator, cmd_id}fn sendResult(self: *State, client: *std.http.Client, arena: Allocator, cmd_id: []const u8, sid: ?[]const u8, status: u16, body: []const u8) void {- var up = Uploader.init(self, client, arena, cmd_id, sid orelse "");+ var up = upload.Uploader.init(link(self, client), arena, cmd_id, sid orelse "");up.finish(status, body);}-/// Event types a viewer must see NOW rather than at the end of a window.-pub fn urgent(line: []const u8) bool {- const t = events.stringField(line, "type") orelse return false;- return std.mem.eql(u8, t, "tool_call") or std.mem.eql(u8, t, "ask_user") or events.terminalEvent(line);-}--/// A line too big for the relay window becomes an envelope-only stub. Only the-/// upload shrinks: the tape and a later `reattach` still carry the full line.-pub fn uploadLine(buf: []u8, line: []const u8) []const u8 {- if (line.len <= upload_line_cap) return line;- const seq = events.seqOf(line) orelse 0;- const t = events.stringField(line, "type") orelse "event";- return std.fmt.bufPrint(buf, "{{\"seq\":{d},\"type\":\"{s}\",\"truncated\":true,\"bytes\":{d}}}", .{ seq, t[0..@min(t.len, 64)], line.len }) catch line[0..0];-}--/// Batches event lines for one command into `POST …/events` bodies:-/// `{"command_id","session_id","events":[<line>,…][,"result":{status,body}]}`.-/// Lines are JSON objects already, so they ride as values, never re-escaped.-const Uploader = struct {- self: *State,- client: *std.http.Client,- arena: Allocator,- cmd_id: []const u8,- sid: []const u8,- aw: Io.Writer.Allocating,- count: usize = 0,- last_flush_ms: i64,-- fn init(self: *State, client: *std.http.Client, arena: Allocator, cmd_id: []const u8, sid: []const u8) Uploader {- return .{ .self = self, .client = client, .arena = arena, .cmd_id = cmd_id, .sid = sid, .aw = .init(arena), .last_flush_ms = util.unixMs(self.st.io) };- }-- fn open(u: *Uploader) void {- u.aw.writer.print("{{\"command_id\":\"{s}\",\"session_id\":\"{s}\",\"events\":[", .{ u.cmd_id, u.sid }) catch {};- }-- fn push(u: *Uploader, line: []const u8) void {- if (u.count == 0) u.open();- if (u.count > 0) u.aw.writer.writeByte(',') catch {};- var stub: [256]u8 = undefined;- u.aw.writer.writeAll(uploadLine(&stub, line)) catch {};- u.count += 1;- const now = util.unixMs(u.self.st.io);- if (u.aw.writer.buffered().len >= flush_bytes or now - u.last_flush_ms >= flush_window_ms or urgent(line)) u.flush(null);- }-- /// One POST. A failed upload is logged and dropped: the tape is complete- /// and the viewer's next `reattach` fills the hole from it.- fn flush(u: *Uploader, result: ?struct { status: u16, body: []const u8 }) void {- if (u.count == 0 and result == null) return;- if (u.count == 0) u.open();- u.aw.writer.writeByte(']') catch {};- if (result) |r| u.aw.writer.print(",\"result\":{{\"status\":{d},\"body\":{s}}}", .{ r.status, r.body }) catch {};- u.aw.writer.writeByte('}') catch {};- const path = std.fmt.allocPrint(u.arena, "/v1/remote/agents/{s}/events", .{&u.self.agent_id}) catch return;- var attempt: usize = 0;- while (attempt < 3) : (attempt += 1) {- if (post(u.self, u.client, u.arena, path, u.aw.writer.buffered())) |r| {- if (r.code >= 200 and r.code < 300) break;- serve.serveLog(u.self.st.io, "remote-control: upload HTTP {d}: {s}", .{ r.code, errorMessage(u.arena, r.body) });- if (r.code < 500) break;- } else |err| serve.serveLog(u.self.st.io, "remote-control: upload failed ({t})", .{err});- u.self.st.io.sleep(.fromMilliseconds(250 * @as(i64, @intCast(attempt + 1))), .awake) catch break;- }- u.aw = .init(u.arena);- u.count = 0;- u.last_flush_ms = util.unixMs(u.self.st.io);- }-- fn finish(u: *Uploader, status: u16, body: []const u8) void {- u.flush(.{ .status = status, .body = body });- }-};-test "parseDeviceId accepts exactly 16 lowercase hex, nothing else" {try std.testing.expectEqualStrings("00ff00ff00ff00ff", &(parseDeviceId("{\"device_id\":\"00ff00ff00ff00ff\"}\n").?));try std.testing.expect(parseDeviceId("{\"device_id\":\"00FF00FF00FF00FF\"}") == null);@@ -565,28 +487,15 @@ test "parseCommands: ids validated, session names validated, bodies re-lined" {try std.testing.expectError(error.BadReply, parseCommands(arena, "[]"));}-test "urgent: tool calls, prompts and terminals flush immediately; deltas batch" {- try std.testing.expect(urgent("{\"seq\":1,\"type\":\"tool_call\",\"name\":\"bash\"}"));- try std.testing.expect(urgent("{\"seq\":2,\"type\":\"ask_user\",\"call_id\":\"q\"}"));- try std.testing.expect(urgent("{\"seq\":3,\"type\":\"turn\"}"));- try std.testing.expect(!urgent("{\"seq\":4,\"type\":\"text\",\"text\":\"turn\"}"));- try std.testing.expect(!urgent("{\"seq\":5,\"type\":\"reasoning\",\"text\":\"…\"}"));-}--test "uploadLine: small lines pass through, oversized ones become an envelope stub" {- var buf: [256]u8 = undefined;- const small = "{\"seq\":7,\"type\":\"text\",\"text\":\"hi\"}";- try std.testing.expectEqualStrings(small, uploadLine(&buf, small));- const big = try std.testing.allocator.alloc(u8, upload_line_cap + 100);- defer std.testing.allocator.free(big);- const head = "{\"seq\":9,\"type\":\"tool_result\",\"output\":\"";- @memset(big, 'x');- @memcpy(big[0..head.len], head);- big[big.len - 2] = '"';- big[big.len - 1] = '}';- const stub = uploadLine(&buf, big);- try std.testing.expect(std.mem.startsWith(u8, stub, "{\"seq\":9,\"type\":\"tool_result\",\"truncated\":true,\"bytes\":"));- try std.testing.expect(events.seqOf(stub).? == 9);+test "yoloRequested: only a JSON true asks for an unattended session" {+ var arena_state = std.heap.ArenaAllocator.init(std.testing.allocator);+ defer arena_state.deinit();+ const arena = arena_state.allocator();+ try std.testing.expect(yoloRequested(arena, "{\"yolo\":true}"));+ try std.testing.expect(!yoloRequested(arena, "{\"yolo\":false}"));+ try std.testing.expect(!yoloRequested(arena, "{\"yolo\":\"true\"}"));+ try std.testing.expect(!yoloRequested(arena, "{\"model\":\"m\"}"));+ try std.testing.expect(!yoloRequested(arena, "not json"));}test "validCommandId keeps relay ids inside a JSON string" {diff --git a/src/remote_upload.zig b/src/remote_upload.zignew file mode 100644index 00000000..5737e892--- /dev/null+++ b/src/remote_upload.zig@@ -0,0 +1,185 @@+//! The device→relay upload half of remote control (#722): the one HTTP call+//! shape, the coalescing event batcher, the oversized-line stub, and the+//! gateway's error text. Split out of remote_control.zig (600-line cap).+//!+//! Lines are JSON objects already, so they ride as values inside the batch+//! body and are never re-escaped. A failed upload is logged and dropped: the+//! tape on disk is complete and a viewer's next `reattach` fills the hole.++const std = @import("std");+const Io = std.Io;+const Value = std.json.Value;+const Allocator = std.mem.Allocator;++const root = @import("main.zig");+const serve = @import("serve.zig");+const events = @import("serve_events.zig");+const util = @import("util.zig");+const harness_version = root.harness_version;++/// Event lines above this go upstream as a stub; the local tape stays complete.+pub const upload_line_cap: usize = 256 * 1024;+/// Coalesce uploads: a burst of text deltas becomes one POST per window.+const flush_window_ms: i64 = 60;+const flush_bytes: usize = 32 * 1024;++/// Everything one HTTP call to the relay needs. `client` is per task: the+/// poll loop owns one, every streamed request another, so a turn's uploads+/// never queue behind a 25s poll (or vice versa).+pub const Link = struct {+ io: Io,+ base: []const u8,+ key: []const u8,+ agent_id: [16]u8,+ client: *std.http.Client,+};++pub const Reply = struct { code: u16, body: []const u8 };++pub fn post(link: Link, arena: Allocator, path: []const u8, payload: []const u8) !Reply {+ const url = try std.fmt.allocPrint(arena, "{s}{s}", .{ link.base, path });+ var aw: Io.Writer.Allocating = .init(arena);+ const res = try link.client.fetch(.{+ .location = .{ .url = url },+ .method = .POST,+ .payload = payload,+ .response_writer = &aw.writer,+ .headers = .{+ .content_type = .{ .override = "application/json" },+ .authorization = .{ .override = try std.fmt.allocPrint(arena, "Bearer {s}", .{link.key}) },+ .user_agent = .{ .override = "simple-harness/" ++ harness_version },+ },+ });+ return .{ .code = @intFromEnum(res.status), .body = aw.writer.buffered() };+}++/// The gateway's error message out of a non-2xx body, or the raw body.+pub fn errorMessage(arena: Allocator, body: []const u8) []const u8 {+ if (std.json.parseFromSliceLeaky(Value, arena, body, .{ .allocate = .alloc_always })) |v| {+ if (v == .object) if (v.object.get("error")) |e| if (e == .object)+ if (util.strFieldObj(e.object, "message")) |m| return m;+ } else |_| {}+ return body[0..@min(body.len, 200)];+}++/// Event types a viewer must see NOW rather than at the end of a window.+pub fn urgent(line: []const u8) bool {+ const t = events.stringField(line, "type") orelse return false;+ return std.mem.eql(u8, t, "tool_call") or std.mem.eql(u8, t, "ask_user") or events.terminalEvent(line);+}++/// A line too big for the relay window becomes an envelope-only stub. Only the+/// upload shrinks: the tape and a later `reattach` still carry the full line.+pub fn uploadLine(buf: []u8, line: []const u8) []const u8 {+ if (line.len <= upload_line_cap) return line;+ const seq = events.seqOf(line) orelse 0;+ const t = events.stringField(line, "type") orelse "event";+ return std.fmt.bufPrint(buf, "{{\"seq\":{d},\"type\":\"{s}\",\"truncated\":true,\"bytes\":{d}}}", .{ seq, t[0..@min(t.len, 64)], line.len }) catch line[0..0];+}++/// Batches event lines for one command into `POST …/events` bodies:+/// `{"command_id","session_id","events":[<line>,…][,"result":{status,body}]}`.+pub const Uploader = struct {+ link: Link,+ arena: Allocator,+ cmd_id: []const u8,+ sid: []const u8,+ aw: Io.Writer.Allocating,+ count: usize = 0,+ last_flush_ms: i64,++ pub fn init(link: Link, arena: Allocator, cmd_id: []const u8, sid: []const u8) Uploader {+ return .{ .link = link, .arena = arena, .cmd_id = cmd_id, .sid = sid, .aw = .init(arena), .last_flush_ms = util.unixMs(link.io) };+ }++ fn open(u: *Uploader) void {+ u.aw.writer.print("{{\"command_id\":\"{s}\",\"session_id\":\"{s}\",\"events\":[", .{ u.cmd_id, u.sid }) catch {};+ }++ pub fn push(u: *Uploader, line: []const u8) void {+ if (u.count == 0) u.open();+ if (u.count > 0) u.aw.writer.writeByte(',') catch {};+ var stub: [256]u8 = undefined;+ u.aw.writer.writeAll(uploadLine(&stub, line)) catch {};+ u.count += 1;+ const now = util.unixMs(u.link.io);+ if (u.aw.writer.buffered().len >= flush_bytes or now - u.last_flush_ms >= flush_window_ms or urgent(line)) u.flush(null);+ }++ /// One POST, retried briefly on 5xx. A failed upload is logged and+ /// dropped: the tape is complete and the viewer's next `reattach` fills it.+ fn flush(u: *Uploader, result: ?struct { status: u16, body: []const u8 }) void {+ if (u.count == 0 and result == null) return;+ if (u.count == 0) u.open();+ u.aw.writer.writeByte(']') catch {};+ if (result) |r| u.aw.writer.print(",\"result\":{{\"status\":{d},\"body\":{s}}}", .{ r.status, r.body }) catch {};+ u.aw.writer.writeByte('}') catch {};+ const path = std.fmt.allocPrint(u.arena, "/v1/remote/agents/{s}/events", .{&u.link.agent_id}) catch return;+ var attempt: usize = 0;+ while (attempt < 3) : (attempt += 1) {+ if (post(u.link, u.arena, path, u.aw.writer.buffered())) |r| {+ if (r.code >= 200 and r.code < 300) break;+ serve.serveLog(u.link.io, "remote-control: upload HTTP {d}: {s}", .{ r.code, errorMessage(u.arena, r.body) });+ if (r.code < 500) break;+ } else |err| serve.serveLog(u.link.io, "remote-control: upload failed ({t})", .{err});+ u.link.io.sleep(.fromMilliseconds(250 * @as(i64, @intCast(attempt + 1))), .awake) catch break;+ }+ u.aw = .init(u.arena);+ u.count = 0;+ u.last_flush_ms = util.unixMs(u.link.io);+ }++ pub fn finish(u: *Uploader, status: u16, body: []const u8) void {+ u.flush(.{ .status = status, .body = body });+ }+};++test "urgent: tool calls, prompts and terminals flush immediately; deltas batch" {+ try std.testing.expect(urgent("{\"seq\":1,\"type\":\"tool_call\",\"name\":\"bash\"}"));+ try std.testing.expect(urgent("{\"seq\":2,\"type\":\"ask_user\",\"call_id\":\"q\"}"));+ try std.testing.expect(urgent("{\"seq\":3,\"type\":\"turn\"}"));+ try std.testing.expect(!urgent("{\"seq\":4,\"type\":\"text\",\"text\":\"turn\"}"));+ try std.testing.expect(!urgent("{\"seq\":5,\"type\":\"reasoning\",\"text\":\"…\"}"));+}++test "uploadLine: small lines pass through, oversized ones become an envelope stub" {+ var buf: [256]u8 = undefined;+ const small = "{\"seq\":7,\"type\":\"text\",\"text\":\"hi\"}";+ try std.testing.expectEqualStrings(small, uploadLine(&buf, small));+ const big = try std.testing.allocator.alloc(u8, upload_line_cap + 100);+ defer std.testing.allocator.free(big);+ const head = "{\"seq\":9,\"type\":\"tool_result\",\"output\":\"";+ @memset(big, 'x');+ @memcpy(big[0..head.len], head);+ big[big.len - 2] = '"';+ big[big.len - 1] = '}';+ const stub = uploadLine(&buf, big);+ try std.testing.expect(std.mem.startsWith(u8, stub, "{\"seq\":9,\"type\":\"tool_result\",\"truncated\":true,\"bytes\":"));+ try std.testing.expect(events.seqOf(stub).? == 9);+}++test "Uploader.push builds one batch body with the lines as JSON values, never re-escaped" {+ var arena_state = std.heap.ArenaAllocator.init(std.testing.allocator);+ defer arena_state.deinit();+ const arena = arena_state.allocator();+ var client: std.http.Client = .{ .allocator = std.testing.allocator, .io = std.testing.io };+ defer client.deinit();+ const link: Link = .{ .io = std.testing.io, .base = "http://relay.invalid", .key = "k", .agent_id = "00ff00ff00ff00ff".*, .client = &client };+ var up = Uploader.init(link, arena, "c1", "nightly");+ up.push("{\"seq\":1,\"type\":\"text\",\"text\":\"a \\\"quoted\\\" word\"}");+ up.push("{\"seq\":2,\"type\":\"reasoning\",\"text\":\"…\"}");+ // Two deltas inside the flush window stay buffered (nothing was posted).+ try std.testing.expectEqual(@as(usize, 2), up.count);+ try std.testing.expectEqualStrings(+ "{\"command_id\":\"c1\",\"session_id\":\"nightly\",\"events\":[{\"seq\":1,\"type\":\"text\",\"text\":\"a \\\"quoted\\\" word\"},{\"seq\":2,\"type\":\"reasoning\",\"text\":\"…\"}",+ up.aw.writer.buffered(),+ );+}++test "errorMessage prefers the gateway's structured message" {+ var arena_state = std.heap.ArenaAllocator.init(std.testing.allocator);+ defer arena_state.deinit();+ const arena = arena_state.allocator();+ try std.testing.expectEqualStrings("device is offline", errorMessage(arena, "{\"error\":{\"message\":\"device is offline\",\"type\":\"device_offline\"}}"));+ try std.testing.expectEqualStrings("<html>", errorMessage(arena, "<html>"));+}diff --git a/src/test_hooks.zig b/src/test_hooks.zigindex e522735f..d7184979 100644--- a/src/test_hooks.zig+++ b/src/test_hooks.zig@@ -360,7 +360,8 @@ test {_ = @import("codex_tool_search.zig"); // hosted tool_search on gpt-5.4+ Codex_ = @import("tool_call_args.zig"); // #752: truncated tool-call arguments must not poison history_ = @import("cache_affinity.zig"); // ADR 0069: git-root / scratch cache seed- _ = @import("remote_control.zig"); // #722: relay command parsing, upload stubs, device id+ _ = @import("remote_control.zig"); // #722: relay command parsing, yolo ceiling, device id+ _ = @import("remote_upload.zig"); // #722: batch body shape, upload stubs, urgent flushes_ = @import("remote_client.zig"); // #722: event painting, gateway error shapes_ = @import("meta_wire.zig"); // #751 / ADR 0083: Meta tool_choice is auto-only_ = @import("codedb_local.zig"); // #765: local-only codedb context (no remote rerank)