Skip to content

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

feat: capture successful inference response streams for diagnosis

Steven Enamakelb16961960910
diff --git a/scripts/__tests__/capture-first-inference.test.mjs b/scripts/__tests__/capture-first-inference.test.mjs
index fa27792270..4131477e92 100644
--- a/scripts/__tests__/capture-first-inference.test.mjs
+++ b/scripts/__tests__/capture-first-inference.test.mjs
@@ -153,6 +153,7 @@ before(async () => {
CAPTURE_PORT: '0',
CAPTURE_UPSTREAM: `http://127.0.0.1:${upstream.port}`,
CAPTURE_ALL: '1',
+ CAPTURE_RESPONSES: '1',
CAPTURE_ALL_DIR: path.join(workDir, 'seq'),
CAPTURE_OUTPUT: path.join(workDir, 'first.json'),
CAPTURE_LOG: path.join(workDir, 'capture.jsonl'),
@@ -182,6 +183,14 @@ test('capture proxy forwards an inference call, dumps the body, and summarises t
assert.equal(reply.status, 200);
assert.match(reply.text, /"content":"Hel"/, 'the stream reaches the client untouched');
assert.match(reply.text, /\[DONE\]/);
+ assert.equal(
+ fs.readFileSync(path.join(workDir, 'seq', 'res-000.txt'), 'utf8'),
+ reply.text,
+ 'the saved successful response must match the stream forwarded to the core'
+ );
+ for (const file of ['first.json', 'seq/req-000.json', 'seq/res-000.txt', 'capture.jsonl']) {
+ assert.equal(fs.statSync(path.join(workDir, file)).mode & 0o777, 0o600, file);
+ }
// Forwarded verbatim, bearer included, to the upstream path.
const seen = upstream.requests.find(r => r.url === '/openai/v1/chat/completions');
diff --git a/scripts/debug/capture-first-inference.mjs b/scripts/debug/capture-first-inference.mjs
index 52212b1ba4..81d8585d26 100644
--- a/scripts/debug/capture-first-inference.mjs
+++ b/scripts/debug/capture-first-inference.mjs
@@ -35,6 +35,7 @@ Configure with environment variables:
(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_RESPONSES=1 record every inference response body 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)
@@ -92,6 +93,7 @@ if (
// cost"; only the sequence answers "does the cacheable prefix survive turn 2",
// which is a different question and the one a prefix cache is graded on.
const captureAll = process.env.CAPTURE_ALL === '1';
+const captureResponses = process.env.CAPTURE_RESPONSES === '1';
const captureAllDir = path.resolve(
process.env.CAPTURE_ALL_DIR || 'target/debug-logs/inference-sequence'
);
@@ -193,10 +195,16 @@ function formatSummaryLine(record) {
function appendSummary(record) {
fs.mkdirSync(path.dirname(summaryLogPath), { recursive: true });
- fs.appendFileSync(summaryLogPath, `${JSON.stringify(record)}\n`);
+ fs.appendFileSync(summaryLogPath, `${JSON.stringify(record)}\n`, { mode: 0o600 });
+ fs.chmodSync(summaryLogPath, 0o600);
process.stdout.write(`${formatSummaryLine(record)}\n`);
}
+function writePrivateCapture(file, data) {
+ fs.writeFileSync(file, data, { mode: 0o600 });
+ fs.chmodSync(file, 0o600);
+}
+
const server = http.createServer((req, res) => {
const chunks = [];
req.on('data', chunk => chunks.push(chunk));
@@ -208,14 +216,14 @@ const server = http.createServer((req, res) => {
if (inference) {
if (!captured) {
fs.mkdirSync(path.dirname(outputPath), { recursive: true });
- fs.writeFileSync(outputPath, body);
+ writePrivateCapture(outputPath, body);
captured = true;
process.stdout.write(`[capture] wrote first inference body to ${outputPath}\n`);
}
if (captureAll) {
fs.mkdirSync(captureAllDir, { recursive: true });
const name = `req-${String(seq).padStart(3, '0')}.json`;
- fs.writeFileSync(path.join(captureAllDir, name), body);
+ writePrivateCapture(path.join(captureAllDir, name), body);
process.stdout.write(`[capture] wrote ${name} (${body.length} B)\n`);
}
}
@@ -266,9 +274,10 @@ const server = http.createServer((req, res) => {
ttfb_ms: firstByteAt === null ? null : firstByteAt - startedAt,
total_ms: Date.now() - startedAt,
};
- if (captureAll && (status < 200 || status >= 300)) {
+ if (captureResponses || (captureAll && (status < 200 || status >= 300))) {
const name = `res-${String(seq).padStart(3, '0')}.txt`;
- fs.writeFileSync(path.join(captureAllDir, name), text);
+ fs.mkdirSync(captureAllDir, { recursive: true });
+ writePrivateCapture(path.join(captureAllDir, name), text);
record.response_body = path.join(captureAllDir, name);
}
appendSummary(record);
@@ -312,7 +321,8 @@ server.listen(listenPort, listenHost, () => {
process.stdout.write(
`[capture] listening on http://${listenHost}:${boundPort}; forwarding to ${upstream.origin}` +
`; summaries → ${summaryLogPath}` +
- `${captureAll ? `; recording every request under ${captureAllDir}` : ''}\n`
+ `${captureAll ? `; recording every request under ${captureAllDir}` : ''}` +
+ `${captureResponses ? `; recording every response under ${captureAllDir}` : ''}\n`
);
});