Skip to content

Commit 5819dda

Browse files
fix(cli): thread --wait waits for the wake run that background work starts
A provider records the run that background work wakes a moment after that work ends, and the thread-wide wait settles in between, so --wait still reported the run that called t3_background_wait. When work outlived the CLI's run and the wait settles with no run after it, --wait now looks for the wake run for up to 10 seconds and keeps waiting once it appears; a later run that is still active is waited for too. AI-assisted; verified with the tests and tools listed above.
1 parent 67c5386 commit 5819dda

2 files changed

Lines changed: 91 additions & 9 deletions

File tree

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

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,12 @@ import * as NetService from "@t3tools/shared/Net";
77
import * as ConfigProvider from "effect/ConfigProvider";
88
import * as DateTime from "effect/DateTime";
99
import * as Effect from "effect/Effect";
10+
import * as Fiber from "effect/Fiber";
1011
import * as FileSystem from "effect/FileSystem";
1112
import * as Layer from "effect/Layer";
1213
import * as Path from "effect/Path";
1314
import { ChildProcess, ChildProcessSpawner } from "effect/process";
15+
import * as TestClock from "effect/testing/TestClock";
1416
import * as TestConsole from "effect/testing/TestConsole";
1517
import { Command } from "effect/cli";
1618
import { vi } from "vite-plus/test";
@@ -255,6 +257,61 @@ it.effect("--wait lasts until background work the agent waits on wakes it and th
255257
}),
256258
);
257259

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+
258315
it.effect(
259316
"--wait keeps its own run's result when another client's later run settles the thread",
260317
() =>

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

Lines changed: 34 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import {
99
ThreadId,
1010
TrimmedNonEmptyString,
1111
type OrchestratorMcpThreadListItem,
12+
type OrchestratorMcpThreadRun,
1213
type OrchestratorMcpThreadStatus,
1314
type OrchestratorMcpThreadTimelineItem,
1415
} from "@t3tools/contracts";
@@ -39,6 +40,15 @@ import { projectLocationFlags, resolveCliAuthConfig } from "./config.ts";
3940
* soon, since `t3_thread_wait` only returns on a terminal run.
4041
*/
4142
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+
]);
4252
const READ_PAGE_LIMIT = 100;
4353
const TITLE_MAX_CHARS = 80;
4454

@@ -531,14 +541,12 @@ const runsAfter = Effect.fn("cli.thread.runsAfter")(function* (
531541
OrchestratorMcpThreadReadResult,
532542
);
533543
const own = recentRuns.find((run) => run.runId === runId);
534-
if (own === undefined) return { later: new Set<RunId>(), sent: true };
535-
const later = new Set(
536-
recentRuns.filter((run) => run.ordinal > own.ordinal).map((run) => run.runId),
537-
);
538-
if (later.size === 0) return { later, sent: false };
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 };
539547
const { items } = yield* readTimeline(tools, threadId, "messages");
540548
const sent = items.some(
541-
(item) => item.type === "user_message" && item.runId !== null && later.has(item.runId),
549+
(item) => item.type === "user_message" && later.some((run) => run.runId === item.runId),
542550
);
543551
return { later, sent };
544552
});
@@ -590,17 +598,34 @@ export const waitForRun = Effect.fn("cli.thread.waitForRun")(function* (
590598
const outcome = yield* ownRun;
591599
if (!followWakes || outcome.type !== "ended" || outcome.status !== "completed") return outcome;
592600
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;
593613
while (true) {
594614
const waited = yield* waitSlice(WAIT_SLICE_MS, true);
595615
// `resultRun` is the thread's latest finished run, whoever sent it.
596616
const result = waited.timedOut ? undefined : waited.resultRun;
597617
const after = yield* runsAfter(tools, threadId, finalRunId);
598618
if (after.sent) return outcome;
599619
if (!waited.timedOut) {
600-
return result !== undefined && after.later.has(result.runId)
601-
? ({ type: "ended", runId: result.runId, status: result.status } satisfies WaitOutcome)
602-
: outcome;
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;
603627
}
628+
workOutlivedRun = true;
604629
const { thread } = yield* tools.call(
605630
"t3_thread_read",
606631
{ threadId, limit: 1, runLimit: 1 },

0 commit comments

Comments
 (0)