The fastest, cheapest, most efficient open-source agent harness. Run more than 500 agents on a $10 VPS.
Rust agent-orchestrationai-agentsai-assistantdesktopllmlocal-firstmcppersonal-aiprivacyrustsecond-braintauri
chore(scripts): add script to capture first inference
Steven Enamakelc9fbba6da780
diff --git a/scripts/debug/capture-first-inference.mjs b/scripts/debug/capture-first-inference.mjsindex de647c6ef4..93c4425376 100644--- a/scripts/debug/capture-first-inference.mjs+++ b/scripts/debug/capture-first-inference.mjs@@ -1,4 +1,13 @@#!/usr/bin/env node+// capture-first-inference.mjs — loopback proxy that sits between the core and+// its inference backend, records the exact request bodies the harness sends,+// and summarises every inference response (which endpoint served it, time to+// first byte, prompt / cached tokens, status). Everything else on the backend+// (auth, sockets, integrations) is forwarded untouched.+//+// Run `node scripts/debug/capture-first-inference.mjs --help` (or+// `pnpm debug capture --help`) for the knobs; the tail of this file has the+// setup recipe.import fs from 'node:fs';import http from 'node:http';@@ -11,12 +20,47 @@ function isLoopbackHost(host) {return LOOPBACK_HOSTS.has(host.toLowerCase());}+const USAGE = `Usage: node scripts/debug/capture-first-inference.mjs [--help]++Loopback proxy between the OpenHuman core and its inference backend. Records the+exact request bodies the harness sends and prints one summary line per inference+response: serving endpoint, time to first byte, prompt / cached tokens, status.++Configure with environment variables:+ CAPTURE_PORT listen port (default 18765)+ CAPTURE_HOST listen host (default 127.0.0.1; see CAPTURE_ALLOW_REMOTE)+ CAPTURE_UPSTREAM where to forward (default https://api.tinyhumans.ai;+ https://openrouter.ai for a direct BYOK OpenRouter route)+ CAPTURE_OUTPUT file for the first inference body+ (default target/debug-logs/first-inference-request.json)+ CAPTURE_ALL=1 also record every inference request (and every non-2xx+ response body) numbered under CAPTURE_ALL_DIR+ CAPTURE_ALL_DIR (default target/debug-logs/inference-sequence)+ CAPTURE_LOG JSONL file receiving one record per inference response+ (default target/debug-logs/inference-capture.jsonl)+ CAPTURE_ALLOW_REMOTE=1 bind a non-loopback CAPTURE_HOST+ CAPTURE_ALLOW_PLAINTEXT_UPSTREAM=1 forward the bearer to a non-loopback http: upstream++Point the core at it, then drive turns and read the summary lines:+ CAPTURE_ALL=1 pnpm debug capture+ api_url = "http://127.0.0.1:18765" # in the user's config.toml, or+ BACKEND_URL=http://127.0.0.1:18765 ./target/debug/openhuman-core run --port 7799+`;++if (process.argv.includes('--help') || process.argv.includes('-h')) {+ process.stdout.write(USAGE);+ process.exit(0);+}+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');+const summaryLogPath = path.resolve(+ process.env.CAPTURE_LOG || 'target/debug-logs/inference-capture.jsonl'+);// The proxy forwards the inbound `authorization` header verbatim. Binding to a// non-loopback interface would expose that bearer to anything on the network@@ -42,6 +86,7 @@ if ('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",@@ -71,13 +116,96 @@ function upstreamPath(requestUrl) {return `${basePath}${requestUrl || '/'}`;}+/**+ * The request-side facts worth one summary line: which model, how big, and the+ * routing/cache identifiers the backend or OpenRouter will key on.+ * `prompt_cache_key` is the harness's stable-prefix fingerprint and OpenRouter's+ * sticky-routing key — it must be identical across the turns of one thread, so+ * seeing it change call to call is a finding, not noise.+ */+export function summarizeRequestBody(body) {+ try {+ const json = JSON.parse(body.toString('utf8'));+ return {+ model: json.model ?? null,+ messages: Array.isArray(json.messages) ? json.messages.length : null,+ tools: Array.isArray(json.tools) ? json.tools.length : 0,+ stream: json.stream === true,+ prompt_cache_key: json.prompt_cache_key ?? null,+ thread_id: json.thread_id ?? json.session_id ?? null,+ };+ } catch {+ return { model: null, messages: null, tools: 0, stream: false, prompt_cache_key: null };+ }+}++/**+ * Folds an OpenAI-compatible response body (SSE stream or unary JSON) into the+ * response-side facts: the endpoint that served it (OpenRouter's `provider`+ * field), the final usage block, and any error object. Works on a partial body+ * too, so a stream cut off mid-way still reports what arrived.+ */+export function summarizeResponseBody(text) {+ const out = { provider: null, prompt_tokens: null, cached_tokens: null, error: null };+ const absorb = json => {+ if (json && typeof json === 'object') {+ if (typeof json.provider === 'string') out.provider = json.provider;+ if (json.usage && typeof json.usage === 'object') {+ out.prompt_tokens = json.usage.prompt_tokens ?? out.prompt_tokens;+ out.cached_tokens = json.usage.prompt_tokens_details?.cached_tokens ?? out.cached_tokens;+ }+ if (json.error) {+ out.error =+ typeof json.error === 'string'+ ? json.error+ : (json.error.message ?? JSON.stringify(json.error));+ }+ }+ };+ for (const line of text.split('\n')) {+ const trimmed = line.trim();+ if (!trimmed) continue;+ const payload = trimmed.startsWith('data:') ? trimmed.slice(5).trim() : trimmed;+ if (payload === '[DONE]' || !payload.startsWith('{')) continue;+ try {+ absorb(JSON.parse(payload));+ } catch {+ // partial or non-JSON line — keep scanning+ }+ }+ if (out.error === null && !text.trimStart().startsWith('data:') && text.includes('<html')) {+ out.error = text.replace(/<[^>]+>/g, ' ').replace(/\s+/g, ' ').trim().slice(0, 120);+ }+ return out;+}++function formatSummaryLine(record) {+ const ms = value => (value == null ? '-' : `${(value / 1000).toFixed(2)}s`);+ return (+ `[capture] #${String(record.seq).padStart(3, '0')} ${record.status} ` ++ `model=${record.model ?? '?'} msgs=${record.messages ?? '?'} tools=${record.tools} ` ++ `served_by=${record.provider ?? '?'} ttfb=${ms(record.ttfb_ms)} total=${ms(record.total_ms)} ` ++ `prompt=${record.prompt_tokens ?? '?'} cached=${record.cached_tokens ?? '?'} ` ++ `cache_key=${record.prompt_cache_key ?? '-'}` ++ (record.error ? ` error=${JSON.stringify(record.error)}` : '')+ );+}++function appendSummary(record) {+ fs.mkdirSync(path.dirname(summaryLogPath), { recursive: true });+ fs.appendFileSync(summaryLogPath, `${JSON.stringify(record)}\n`);+ process.stdout.write(`${formatSummaryLine(record)}\n`);+}+const server = http.createServer((req, res) => {const chunks = [];req.on('data', chunk => chunks.push(chunk));req.on('end', () => {const body = Buffer.concat(chunks);+ const inference = isInferenceRequest(req);+ const seq = inference ? sequenceIndex++ : null;- if (isInferenceRequest(req)) {+ if (inference) {if (!captured) {fs.mkdirSync(path.dirname(outputPath), { recursive: true });fs.writeFileSync(outputPath, body);@@ -86,9 +214,8 @@ const server = http.createServer((req, res) => {}if (captureAll) {fs.mkdirSync(captureAllDir, { recursive: true });- const name = `req-${String(sequenceIndex).padStart(3, '0')}.json`;+ const name = `req-${String(seq).padStart(3, '0')}.json`;fs.writeFileSync(path.join(captureAllDir, name), body);- sequenceIndex += 1;process.stdout.write(`[capture] wrote ${name} (${body.length} B)\n`);}}@@ -97,6 +224,7 @@ const server = http.createServer((req, res) => {delete headers['content-length'];headers['content-length'] = String(body.length);+ const startedAt = Date.now();const transport = upstream.protocol === 'https:' ? https : http;const upstreamReq = transport.request({@@ -109,7 +237,44 @@ const server = http.createServer((req, res) => {},upstreamRes => {res.writeHead(upstreamRes.statusCode || 502, upstreamRes.headers);- upstreamRes.pipe(res);+ if (!inference) {+ upstreamRes.pipe(res);+ return;+ }+ // Tee the response: stream it to the core untouched while folding a+ // copy into the summary. Inference bodies are small (a few hundred KB+ // at most), so buffering the copy is fine.+ let firstByteAt = null;+ const pieces = [];+ upstreamRes.on('data', chunk => {+ if (firstByteAt === null) firstByteAt = Date.now();+ pieces.push(chunk);+ res.write(chunk);+ });+ upstreamRes.on('end', () => {+ res.end();+ const text = Buffer.concat(pieces).toString('utf8');+ const status = upstreamRes.statusCode || 502;+ const record = {+ seq,+ at: new Date(startedAt).toISOString(),+ status,+ ...summarizeRequestBody(body),+ ...summarizeResponseBody(text),+ ttfb_ms: firstByteAt === null ? null : firstByteAt - startedAt,+ total_ms: Date.now() - startedAt,+ };+ if (captureAll && (status < 200 || status >= 300)) {+ const name = `res-${String(seq).padStart(3, '0')}.txt`;+ fs.writeFileSync(path.join(captureAllDir, name), text);+ record.response_body = path.join(captureAllDir, name);+ }+ appendSummary(record);+ });+ upstreamRes.on('error', error => {+ process.stderr.write(`[capture] upstream stream error: ${error.message}\n`);+ res.end();+ });});@@ -117,6 +282,20 @@ const server = http.createServer((req, res) => {process.stderr.write(`[capture] upstream error: ${error.message}\n`);if (!res.headersSent) res.writeHead(502, { 'content-type': 'text/plain' });res.end('capture proxy upstream error');+ if (inference) {+ appendSummary({+ seq,+ at: new Date(startedAt).toISOString(),+ status: 502,+ ...summarizeRequestBody(body),+ provider: null,+ prompt_tokens: null,+ cached_tokens: null,+ error: `upstream: ${error.message}`,+ ttfb_ms: null,+ total_ms: Date.now() - startedAt,+ });+ }});upstreamReq.end(body);@@ -126,6 +305,7 @@ const server = http.createServer((req, res) => {server.listen(listenPort, listenHost, () => {process.stdout.write(`[capture] listening on http://${listenHost}:${listenPort}; forwarding to ${upstream.origin}` ++ `; summaries → ${summaryLogPath}` +`${captureAll ? `; recording every request under ${captureAllDir}` : ''}\n`);});