Skip to content

Commit 8305b04

Browse files
t3dotggclaude
andcommitted
fix(cli): --wait reports its own run when a later run is waiting for an answer
Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent c55f822 commit 8305b04

2 files changed

Lines changed: 34 additions & 10 deletions

File tree

‎apps/server/src/cli/thread.test.ts‎

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,31 @@ it.effect("--wait follows the sent run to its continuation's terminal status", (
4545
it.effect("--wait stops when the thread waits for an answer instead of polling forever", () =>
4646
Effect.gen(function* () {
4747
const { tools } = scriptedTools({
48-
t3_thread_wait: [{ threadId, runId, status: "queued", timedOut: true }],
48+
t3_thread_wait: [
49+
{ threadId, runId, status: "queued", timedOut: true },
50+
{ threadId, runId, status: "queued", timedOut: true },
51+
],
4952
t3_thread_read: [{ thread: { pendingRequestCount: 1 } }],
5053
});
5154

5255
assert.deepStrictEqual(yield* waitForRun(tools, threadId, runId), { type: "needs_input" });
5356
}),
5457
);
58+
59+
it.effect("--wait reports its run's result when a later run is the one waiting", () =>
60+
Effect.gen(function* () {
61+
const { tools } = scriptedTools({
62+
t3_thread_wait: [
63+
{ threadId, runId, status: "running", timedOut: true },
64+
{ threadId, runId, status: "completed", timedOut: false },
65+
],
66+
t3_thread_read: [{ thread: { pendingRequestCount: 1 } }],
67+
});
68+
69+
assert.deepStrictEqual(yield* waitForRun(tools, threadId, runId), {
70+
type: "ended",
71+
runId,
72+
status: "completed",
73+
});
74+
}),
75+
);

‎apps/server/src/cli/thread.ts‎

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -387,23 +387,26 @@ export const waitForRun = Effect.fn("cli.thread.waitForRun")(function* (
387387
threadId: ThreadId,
388388
runId: RunId,
389389
) {
390+
const waitSlice = (timeoutMs: number) =>
391+
tools.call("t3_thread_wait", { threadId, runId, timeoutMs }, OrchestratorMcpThreadWaitResult);
392+
const ended = (waited: OrchestratorMcpThreadWaitResult) =>
393+
({ type: "ended", runId: waited.runId, status: waited.status }) satisfies WaitOutcome;
390394
while (true) {
391-
const waited = yield* tools.call(
392-
"t3_thread_wait",
393-
{ threadId, runId, timeoutMs: WAIT_SLICE_MS },
394-
OrchestratorMcpThreadWaitResult,
395-
);
396-
if (!waited.timedOut) {
397-
return { type: "ended", runId: waited.runId, status: waited.status } satisfies WaitOutcome;
398-
}
395+
const waited = yield* waitSlice(WAIT_SLICE_MS);
396+
if (!waited.timedOut) return ended(waited);
399397
// A pending request on this run, or on an earlier run this one is queued
400398
// behind, blocks it until someone answers.
401399
const { thread } = yield* tools.call(
402400
"t3_thread_read",
403401
{ threadId, limit: 1, runLimit: 1 },
404402
OrchestratorMcpThreadReadResult,
405403
);
406-
if (thread.pendingRequestCount > 0) return { type: "needs_input" } satisfies WaitOutcome;
404+
if (thread.pendingRequestCount > 0) {
405+
// The count is thread-wide: if this run ended since the slice, the
406+
// request belongs to a later run.
407+
const recheck = yield* waitSlice(1);
408+
return recheck.timedOut ? ({ type: "needs_input" } satisfies WaitOutcome) : ended(recheck);
409+
}
407410
}
408411
});
409412

0 commit comments

Comments
 (0)