The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 21:00 UTC
fix(cli): thread --wait returns once background work the agent waits on has ended (#34696)
Julius Marminge committed
Nov 11, 2026 at 19:56 UTC
671f85417c6633f6cbb2965a196c266463392e7f
2 files changed
+258
-14
apps/server/src/cli/thread.test.ts
+129
@@ -7,10 +7,12 @@ import * as NetService from "@t3tools/shared/Net";
7
import * as ConfigProvider from "effect/ConfigProvider";
8
import * as DateTime from "effect/DateTime";
9
import * as Effect from "effect/Effect";
10
+import * as Fiber from "effect/Fiber";
11
import * as FileSystem from "effect/FileSystem";
12
import * as Layer from "effect/Layer";
13
import * as Path from "effect/Path";
14
import { ChildProcess, ChildProcessSpawner } from "effect/process";
15
+import * as TestClock from "effect/testing/TestClock";
16
import * as TestConsole from "effect/testing/TestConsole";
17
import { Command } from "effect/cli";
18
import { vi } from "vite-plus/test";
@@ -210,6 +212,133 @@ it.effect("--wait follows the sent run to its continuation's terminal status", (
212
}),
213
);
214
215
+/** The reads that ask whether a run after `runId` holds a message someone sent. */
216
+const laterRunReads = (
217
+ laterRun: RunId,
218
+ items: ReadonlyArray<{ readonly runId: RunId; readonly type: string }>,
219
+) => [
220
+ {
221
+ recentRuns: [
222
+ { runId, ordinal: 1 },
223
+ { runId: laterRun, ordinal: 2 },
224
+ ],
225
+ },
226
+ { thread: {}, items, nextPosition: null, hasMore: false },
227
+];
228
+
229
+it.effect("--wait lasts until background work the agent waits on wakes it and that run ends", () =>
230
+ Effect.gen(function* () {
231
+ const wakeRun = RunId.make("run-2");
232
+ const { tools, calls } = scriptedTools({
233
+ t3_thread_wait: [
234
+ { threadId, runId, status: "completed", timedOut: false },
235
+ {
236
+ threadId,
237
+ runId,
238
+ status: "completed",
239
+ timedOut: false,
240
+ resultRun: { runId: wakeRun, status: "completed" },
241
+ },
242
+ ],
243
+ // The wake run starts from a notification, which the messages view leaves out.
244
+ t3_thread_read: laterRunReads(wakeRun, []),
245
+ });
246
+
247
+ const outcome = yield* waitForRun(tools, threadId, runId, true);
248
+
249
+ assert.deepStrictEqual(
250
+ calls.filter((entry) => entry.tool === "t3_thread_wait").map((entry) => entry.args),
251
+ [
252
+ { threadId, runId, timeoutMs: 15_000 },
253
+ { threadId, runId, timeoutMs: 15_000, untilDelegatedWorkSettled: true },
254
+ ],
255
+ );
256
+ assert.deepStrictEqual(outcome, { type: "ended", runId: wakeRun, status: "completed" });
257
+ }),
258
+);
259
+
260
+it.effect(
261
+ "--wait waits out the moment between background work ending and its wake run starting",
262
+ () =>
263
+ Effect.gen(function* () {
264
+ const wakeRun = RunId.make("run-2");
265
+ const ownOnly = { recentRuns: [{ runId, ordinal: 1, status: "completed" }] };
266
+ const withWake = (status: string) => [
267
+ {
268
+ recentRuns: [
269
+ { runId, ordinal: 1, status: "completed" },
270
+ { runId: wakeRun, ordinal: 2, status },
271
+ ],
272
+ },
273
+ { thread: {}, items: [], nextPosition: null, hasMore: false },
274
+ ];
275
+ const { tools } = scriptedTools({
276
+ t3_thread_wait: [
277
+ { threadId, runId, status: "completed", timedOut: false },
278
+ { threadId, runId, status: "completed", timedOut: true },
279
+ // The work ended, and the run it wakes is not recorded yet.
280
+ {
281
+ threadId,
282
+ runId,
283
+ status: "completed",
284
+ timedOut: false,
285
+ resultRun: { runId, status: "completed" },
286
+ },
287
+ {
288
+ threadId,
289
+ runId,
290
+ status: "completed",
291
+ timedOut: false,
292
+ resultRun: { runId: wakeRun, status: "completed" },
293
+ },
294
+ ],
295
+ t3_thread_read: [
296
+ ownOnly,
297
+ { thread: { pendingRequestCount: 0 } },
298
+ ownOnly,
299
+ ...withWake("running"),
300
+ ...withWake("completed"),
301
+ ],
302
+ });
303
+
304
+ const fiber = yield* Effect.forkChild(waitForRun(tools, threadId, runId, true));
305
+ yield* TestClock.adjust("500 millis");
306
+
307
+ assert.deepStrictEqual(yield* Fiber.join(fiber), {
308
+ type: "ended",
309
+ runId: wakeRun,
310
+ status: "completed",
311
+ });
312
+ }),
313
+);
314
+
315
+it.effect(
316
+ "--wait keeps its own run's result when another client's later run settles the thread",
317
+ () =>
318
+ Effect.gen(function* () {
319
+ const sentRun = RunId.make("run-2");
320
+ const { tools } = scriptedTools({
321
+ t3_thread_wait: [
322
+ { threadId, runId, status: "completed", timedOut: false },
323
+ {
324
+ threadId,
325
+ runId,
326
+ status: "completed",
327
+ timedOut: false,
328
+ resultRun: { runId: sentRun, status: "failed" },
329
+ },
330
+ ],
331
+ t3_thread_read: laterRunReads(sentRun, [{ runId: sentRun, type: "user_message" }]),
332
+ });
333
+
334
+ assert.deepStrictEqual(yield* waitForRun(tools, threadId, runId, true), {
335
+ type: "ended",
336
+ runId,
337
+ status: "completed",
338
+ });
339
+ }),
340
+);
341
+
342
it.effect("--wait stops when the thread waits for an answer instead of polling forever", () =>
343
Effect.gen(function* () {
344
const { tools } = scriptedTools({
apps/server/src/cli/thread.ts
+129
-14
@@ -9,6 +9,7 @@ import {
9
ThreadId,
10
TrimmedNonEmptyString,
11
type OrchestratorMcpThreadListItem,
12
+ type OrchestratorMcpThreadRun,
13
type OrchestratorMcpThreadStatus,
14
type OrchestratorMcpThreadTimelineItem,
15
} from "@t3tools/contracts";
@@ -39,6 +40,15 @@ import { projectLocationFlags, resolveCliAuthConfig } from "./config.ts";
40
* soon, since `t3_thread_wait` only returns on a terminal run.
41
*/
42
const WAIT_SLICE_MS = 15_000;
43
+const WAKE_GRACE_MS = 10_000;
44
+const WAKE_POLL_MS = 500;
45
+const TERMINAL_RUN_STATUSES = new Set<OrchestratorMcpThreadRun["status"]>([
46
+ "completed",
47
+ "failed",
48
+ "cancelled",
49
+ "interrupted",
50
+ "rolled_back",
51
+]);
52
const READ_PAGE_LIMIT = 100;
53
const TITLE_MAX_CHARS = 80;
54
@@ -498,35 +508,131 @@ type WaitOutcome =
508
}
509
| { readonly type: "needs_input" };
510
511
+/**
512
+ * Whether the thread has nothing left to do: no active run and no delegated
513
+ * or background work. Only then is the work a later wait settles on this
514
+ * send's own.
515
+ */
516
+const threadIsIdle = Effect.fn("cli.thread.threadIsIdle")(function* (
517
+ tools: ThreadTools,
518
+ threadId: ThreadId,
519
+) {
520
+ const waited = yield* tools.call(
521
+ "t3_thread_wait",
522
+ { threadId, timeoutMs: 1, untilDelegatedWorkSettled: true },
523
+ OrchestratorMcpThreadWaitResult,
524
+ );
525
+ return !waited.timedOut;
526
+});
527
+
528
+/**
529
+ * The runs after `runId`, and whether one holds a message someone sent.
530
+ * Background work wakes a run with a notification, never a user message, so
531
+ * such a run is another request and its reply is not this one's.
532
+ */
533
+const runsAfter = Effect.fn("cli.thread.runsAfter")(function* (
534
+ tools: ThreadTools,
535
+ threadId: ThreadId,
536
+ runId: RunId,
537
+) {
538
+ const { recentRuns } = yield* tools.call(
539
+ "t3_thread_read",
540
+ { threadId, limit: 1, runLimit: 50 },
541
+ OrchestratorMcpThreadReadResult,
542
+ );
543
+ const own = recentRuns.find((run) => run.runId === runId);
544
+ if (own === undefined) return { later: [], sent: true };
545
+ const later = recentRuns.filter((run) => run.ordinal > own.ordinal);
546
+ if (later.length === 0) return { later, sent: false };
547
+ const { items } = yield* readTimeline(tools, threadId, "messages");
548
+ const sent = items.some(
549
+ (item) => item.type === "user_message" && later.some((run) => run.runId === item.runId),
550
+ );
551
+ return { later, sent };
552
+});
553
+
554
/**
555
* Waits for this exact run, following a restart to its continuation, so a
556
* message another client sends meanwhile cannot stand in for its result.
557
+ * With `followWakes`, a completed run is followed further to the runs its
558
+ * background work wakes, as long as no one else sends to the thread.
559
*/
560
export const waitForRun = Effect.fn("cli.thread.waitForRun")(function* (
561
tools: ThreadTools,
562
threadId: ThreadId,
563
runId: RunId,
564
+ followWakes = false,
565
) {
510
- const waitSlice = (timeoutMs: number) =>
511
- tools.call("t3_thread_wait", { threadId, runId, timeoutMs }, OrchestratorMcpThreadWaitResult);
566
+ const waitSlice = (timeoutMs: number, untilDelegatedWorkSettled?: true) =>
567
+ tools.call(
568
+ "t3_thread_wait",
569
+ {
570
+ threadId,
571
+ runId,
572
+ timeoutMs,
573
+ ...(untilDelegatedWorkSettled ? { untilDelegatedWorkSettled } : {}),
574
+ },
575
+ OrchestratorMcpThreadWaitResult,
576
+ );
577
const ended = (waited: OrchestratorMcpThreadWaitResult) =>
578
({ type: "ended", runId: waited.runId, status: waited.status }) satisfies WaitOutcome;
579
+ const ownRun = Effect.gen(function* () {
580
+ while (true) {
581
+ const waited = yield* waitSlice(WAIT_SLICE_MS);
582
+ if (!waited.timedOut) return ended(waited);
583
+ // A pending request on this run, or on an earlier run this one is queued
584
+ // behind, blocks it until someone answers.
585
+ const { thread } = yield* tools.call(
586
+ "t3_thread_read",
587
+ { threadId, limit: 1, runLimit: 1 },
588
+ OrchestratorMcpThreadReadResult,
589
+ );
590
+ if (thread.pendingRequestCount > 0) {
591
+ // The count is thread-wide: if this run ended since the slice, the
592
+ // request belongs to a later run.
593
+ const recheck = yield* waitSlice(1);
594
+ return recheck.timedOut ? ({ type: "needs_input" } satisfies WaitOutcome) : ended(recheck);
595
+ }
596
+ }
597
+ });
598
+ const outcome = yield* ownRun;
599
+ if (!followWakes || outcome.type !== "ended" || outcome.status !== "completed") return outcome;
600
+ const finalRunId = outcome.runId ?? runId;
601
+ // A provider records the run that background work wakes only after that
602
+ // work has ended, so the thread briefly looks settled in between.
603
+ const wakeRunRecorded = Effect.gen(function* () {
604
+ for (let waited = 0; waited < WAKE_GRACE_MS; waited += WAKE_POLL_MS) {
605
+ yield* Effect.sleep(WAKE_POLL_MS);
606
+ const after = yield* runsAfter(tools, threadId, finalRunId);
607
+ if (after.sent) return false;
608
+ if (after.later.length > 0) return true;
609
+ }
610
+ return false;
611
+ });
612
+ let workOutlivedRun = false;
613
while (true) {
515
- const waited = yield* waitSlice(WAIT_SLICE_MS);
516
- if (!waited.timedOut) return ended(waited);
517
- // A pending request on this run, or on an earlier run this one is queued
518
- // behind, blocks it until someone answers.
614
+ const waited = yield* waitSlice(WAIT_SLICE_MS, true);
615
+ // `resultRun` is the thread's latest finished run, whoever sent it.
616
+ const result = waited.timedOut ? undefined : waited.resultRun;
617
+ const after = yield* runsAfter(tools, threadId, finalRunId);
618
+ if (after.sent) return outcome;
619
+ if (!waited.timedOut) {
620
+ if (result !== undefined && after.later.some((run) => run.runId === result.runId)) {
621
+ return { type: "ended", runId: result.runId, status: result.status } satisfies WaitOutcome;
622
+ }
623
+ if (!workOutlivedRun) return outcome;
624
+ if (after.later.some((run) => !TERMINAL_RUN_STATUSES.has(run.status))) continue;
625
+ if (after.later.length === 0 && (yield* wakeRunRecorded)) continue;
626
+ return outcome;
627
+ }
628
+ workOutlivedRun = true;
629
const { thread } = yield* tools.call(
630
"t3_thread_read",
631
{ threadId, limit: 1, runLimit: 1 },
632
OrchestratorMcpThreadReadResult,
633
);
524
- if (thread.pendingRequestCount > 0) {
525
- // The count is thread-wide: if this run ended since the slice, the
526
- // request belongs to a later run.
527
- const recheck = yield* waitSlice(1);
528
- return recheck.timedOut ? ({ type: "needs_input" } satisfies WaitOutcome) : ended(recheck);
529
- }
634
+ // Earlier runs have ended, so the request is on a run this one's work woke.
635
+ if (thread.pendingRequestCount > 0) return { type: "needs_input" } satisfies WaitOutcome;
636
}
637
});
638
@@ -540,9 +646,10 @@ const finishRun = Effect.fn("cli.thread.finishRun")(function* (
646
readonly threadId: ThreadId;
647
readonly runId: RunId;
648
readonly json: boolean;
649
+ readonly followWakes: boolean;
650
},
651
) {
545
- const outcome = yield* waitForRun(tools, input.threadId, input.runId);
652
+ const outcome = yield* waitForRun(tools, input.threadId, input.runId, input.followWakes);
653
const runId = outcome.type === "ended" ? (outcome.runId ?? input.runId) : input.runId;
654
const status = outcome.type === "ended" ? outcome.status : "waiting";
655
const reply =
@@ -727,6 +834,7 @@ const threadStartCommand = Command.make("start", {
834
threadId: launched.threadId,
835
runId: launched.runId,
836
json: flags.json,
837
+ followWakes: true,
838
});
839
}
840
yield* flags.json ? printJson(launched) : Console.log(launched.threadId);
@@ -753,13 +861,20 @@ const threadSendCommand = Command.make("send", {
861
yield* withThreadTools(
862
flags,
863
Effect.fn("cli.thread.send")(function* (tools) {
864
+ // Work already running when this is sent is not this message's to wait for.
865
+ const followWakes = flags.wait ? yield* threadIsIdle(tools, threadId) : false;
866
const sent = yield* tools.call(
867
"t3_thread_send",
868
{ threadId, message, mode: "queue" },
869
OrchestratorMcpThreadSendResult,
870
);
871
if (flags.wait) {
762
- return yield* finishRun(tools, { threadId, runId: sent.runId, json: flags.json });
872
+ return yield* finishRun(tools, {
873
+ threadId,
874
+ runId: sent.runId,
875
+ json: flags.json,
876
+ followWakes,
877
+ });
878
}
879
yield* flags.json
880
? printJson(sent)