Skip to content

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

fix(scripts): fail fast on non-2xx RPC responses and stale transcripts

Steven Enamakel530d6da02276
diff --git a/scripts/debug/run-multi-turn-capture.mjs b/scripts/debug/run-multi-turn-capture.mjs
index ecf732edb1..8f15652c2a 100644
--- a/scripts/debug/run-multi-turn-capture.mjs
+++ b/scripts/debug/run-multi-turn-capture.mjs
@@ -52,6 +52,13 @@ async function rpc(method, params) {
signal: AbortSignal.timeout(300_000),
});
const text = await response.text();
+ if (!response.ok) {
+ // A non-2xx status may not even be a JSON-RPC envelope (a proxy error
+ // page, a 401 from an expired token, …), so `body.error` can be absent
+ // and `body.result` silently `undefined` if we fell through to the
+ // normal parse path below.
+ throw new Error(`RPC ${method} failed with HTTP ${response.status}: ${text.slice(0, 300)}`);
+ }
let body;
try {
body = JSON.parse(text);
@@ -130,6 +137,20 @@ async function waitForTurnToSettle(repliesBefore, { timeoutMs = 300_000 } = {})
throw new Error(`turn did not persist a reply within ${timeoutMs}ms`);
}
+// `waitForTurnToSettle` treats "one more persisted reply than before" as the
+// completion signal, so a leftover transcript file matching this thread id's
+// truncated session-key prefix (a stale run, or an unlucky collision) would
+// make turn 1 look complete the instant it is dispatched. The random segment
+// in the default `--thread-id` (see above) makes a collision unlikely, but
+// "unlikely" is not "impossible" — fail fast and loud rather than silently
+// racing ahead on a false completion signal.
+if (persistedReplies() > 0) {
+ throw new Error(
+ `thread_id=${threadId} already has persisted replies before turn 1 was dispatched — ` +
+ 'pick a different --thread-id or clear the stale transcript.'
+ );
+}
+
console.log(`[multi-turn] core=${coreUrl} thread_id=${threadId}`);
for (const [index, message] of TURNS.entries()) {