Skip to content

The fastest, cheapest, most efficient open-source agent harness. Run more than 500 agents on a $10 VPS.

chore(scripts): add first inference capture debug script

Steven Enamakel67bdf26e5895
diff --git a/scripts/debug/capture-first-inference.mjs b/scripts/debug/capture-first-inference.mjs
index 53c7ba624a..de647c6ef4 100644
--- a/scripts/debug/capture-first-inference.mjs
+++ b/scripts/debug/capture-first-inference.mjs
@@ -5,12 +5,43 @@ import http from 'node:http';
import https from 'node:https';
import path from 'node:path';
+const LOOPBACK_HOSTS = new Set(['127.0.0.1', '::1', 'localhost']);
+
+function isLoopbackHost(host) {
+ return LOOPBACK_HOSTS.has(host.toLowerCase());
+}
+
const listenHost = process.env.CAPTURE_HOST || '127.0.0.1';
const listenPort = Number.parseInt(process.env.CAPTURE_PORT || '18765', 10);
const upstream = new URL(process.env.CAPTURE_UPSTREAM || 'https://api.tinyhumans.ai');
const outputPath = path.resolve(
process.env.CAPTURE_OUTPUT || 'target/debug-logs/first-inference-request.json'
);
+
+// The proxy forwards the inbound `authorization` header verbatim. Binding to a
+// non-loopback interface would expose that bearer to anything on the network
+// that can reach this port with no auth of its own; forwarding it over a
+// plaintext (`http:`) upstream that isn't itself loopback would expose it in
+// transit. Both are opt-in escape hatches for a deliberate reason (a
+// non-loopback CAPTURE_UPSTREAM pointed at a local mock server on `http:` is a
+// normal debugging setup), gated by explicit env vars rather than silently
+// allowed.
+if (!isLoopbackHost(listenHost) && process.env.CAPTURE_ALLOW_REMOTE !== '1') {
+ throw new Error(
+ `refusing to bind CAPTURE_HOST=${listenHost}: not loopback. ` +
+ 'Set CAPTURE_ALLOW_REMOTE=1 to bind a non-loopback interface anyway.'
+ );
+}
+if (
+ upstream.protocol === 'http:' &&
+ !isLoopbackHost(upstream.hostname) &&
+ process.env.CAPTURE_ALLOW_PLAINTEXT_UPSTREAM !== '1'
+) {
+ throw new Error(
+ `refusing to forward the authorization header to CAPTURE_UPSTREAM=${upstream.origin} over plaintext http:. ` +
+ 'Use an https: upstream, point at a loopback host, or set CAPTURE_ALLOW_PLAINTEXT_UPSTREAM=1 to override.'
+ );
+}
// `CAPTURE_ALL=1` records *every* inference request of the session, numbered, into
// `CAPTURE_ALL_DIR`. The single-shot default answers "what does the first turn
// cost"; only the sequence answers "does the cacheable prefix survive turn 2",