Skip to content

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

chore: files changed scripts/debug/run-multi-turn-capture.mjs

Steven Enamakelaf0089c0cb18
diff --git a/scripts/debug/run-multi-turn-capture.mjs b/scripts/debug/run-multi-turn-capture.mjs
index c864360b1e..25dfb04c55 100644
--- a/scripts/debug/run-multi-turn-capture.mjs
+++ b/scripts/debug/run-multi-turn-capture.mjs
@@ -59,11 +59,19 @@ async function rpc(method, params) {
}
// `channel_web_chat` ACKs immediately and runs the turn asynchronously, streaming
-// over the socket. Firing the next turn on the ACK would put three turns into one
-// thread concurrently — which is not a multi-turn session at all, and would make
-// the capture unreadable. Wait for the capture directory to go quiet instead: it
-// is the same signal the audit reads, so "the turn's requests have all landed" is
-// observed rather than assumed.
+// over the socket. Firing the next turn on the ACK would put several turns into
+// one thread concurrently — which is not a multi-turn session at all, and makes
+// the capture meaningless: each overlapping turn starts from an empty history, so
+// every request looks like turn 1 and the sequence appears never to accumulate.
+//
+// Waiting for the *capture directory* to go quiet is not enough either, and that
+// is a mistake worth recording: the inference request is sent at the START of the
+// turn, so a long streamed reply is still being generated and persisted long
+// after the last request landed. It produced exactly the false reading above.
+//
+// The turn's own transcript is the completion signal — it is written when the
+// turn ends — so wait for this thread's persisted assistant-message count to
+// rise.
const captureDir = path.resolve(
process.env.CAPTURE_ALL_DIR || 'target/debug-logs/inference-sequence'
);
@@ -73,22 +81,49 @@ const capturedCount = () =>
const sleep = ms => new Promise(resolve => setTimeout(resolve, ms));
-/** Wait until at least one new request has landed and none has for `quietMs`. */
-async function waitForTurnToSettle(before, { quietMs = 8_000, timeoutMs = 300_000 } = {}) {
+/**
+ * Assistant messages persisted for this thread, across every transcript file
+ * that belongs to it (a resume mints a new file, so one thread can own several).
+ */
+function persistedReplies() {
+ const roots = fs.existsSync(path.join(os.homedir(), '.openhuman', 'users'))
+ ? fs.readdirSync(path.join(os.homedir(), '.openhuman', 'users'))
+ : [];
+ // The session key truncates the thread id, so match on a prefix rather than
+ // the whole thing.
+ const key = threadId.slice(0, 12);
+ let replies = 0;
+ for (const user of roots) {
+ const dir = path.join(os.homedir(), '.openhuman', 'users', user, 'workspace', 'session_raw');
+ if (!fs.existsSync(dir)) continue;
+ for (const file of fs.readdirSync(dir)) {
+ if (!file.includes(key) || !file.endsWith('.jsonl')) continue;
+ for (const line of fs.readFileSync(path.join(dir, file), 'utf8').split('\n')) {
+ if (!line.trim()) continue;
+ try {
+ if (JSON.parse(line).role === 'assistant') replies += 1;
+ } catch {
+ /* a partially flushed line — it will be counted on the next poll */
+ }
+ }
+ }
+ }
+ return replies;
+}
+
+/** Wait until this thread has persisted one more assistant reply than before. */
+async function waitForTurnToSettle(repliesBefore, { timeoutMs = 300_000 } = {}) {
const deadline = Date.now() + timeoutMs;
- let last = before;
- let lastChange = Date.now();
while (Date.now() < deadline) {
await sleep(1_000);
- const now = capturedCount();
- if (now !== last) {
- last = now;
- lastChange = Date.now();
- } else if (last > before && Date.now() - lastChange >= quietMs) {
- return last;
+ if (persistedReplies() > repliesBefore) {
+ // The transcript is written at the end of the turn; give the rest of the
+ // turn's teardown a beat before the next one starts.
+ await sleep(2_000);
+ return;
}
}
- throw new Error(`turn did not settle within ${timeoutMs}ms (captured ${last}, started at ${before})`);
+ throw new Error(`turn did not persist a reply within ${timeoutMs}ms`);
}
console.log(`[multi-turn] core=${coreUrl} thread_id=${threadId}`);