Skip to content

Commit 6affed6

Browse files
committed
Merge remote-tracking branch 'origin/main' into t3/fix-commit-loader-shift
2 parents 47d02ab + 671f854 commit 6affed6

2 files changed

Lines changed: 258 additions & 14 deletions

File tree

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

Lines changed: 129 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";
@@ -210,6 +212,133 @@ it.effect("--wait follows the sent run to its continuation's terminal status", (
210212
}),
211213
);
212214

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+
213342
it.effect("--wait stops when the thread waits for an answer instead of polling forever", () =>
214343
Effect.gen(function* () {
215344
const { tools } = scriptedTools({

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

Lines changed: 129 additions & 14 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

@@ -498,35 +508,131 @@ type WaitOutcome =
498508
}
499509
| { readonly type: "needs_input" };
500510

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+
501554
/**
502555
* Waits for this exact run, following a restart to its continuation, so a
503556
* 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.
504559
*/
505560
export const waitForRun = Effect.fn("cli.thread.waitForRun")(function* (
506561
tools: ThreadTools,
507562
threadId: ThreadId,
508563
runId: RunId,
564+
followWakes = false,
509565
) {
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+
);
512577
const ended = (waited: OrchestratorMcpThreadWaitResult) =>
513578
({ 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;
514613
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;
519629
const { thread } = yield* tools.call(
520630
"t3_thread_read",
521631
{ threadId, limit: 1, runLimit: 1 },
522632
OrchestratorMcpThreadReadResult,
523633
);
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;
530636
}
531637
});
532638

@@ -540,9 +646,10 @@ const finishRun = Effect.fn("cli.thread.finishRun")(function* (
540646
readonly threadId: ThreadId;
541647
readonly runId: RunId;
542648
readonly json: boolean;
649+
readonly followWakes: boolean;
543650
},
544651
) {
545-
const outcome = yield* waitForRun(tools, input.threadId, input.runId);
652+
const outcome = yield* waitForRun(tools, input.threadId, input.runId, input.followWakes);
546653
const runId = outcome.type === "ended" ? (outcome.runId ?? input.runId) : input.runId;
547654
const status = outcome.type === "ended" ? outcome.status : "waiting";
548655
const reply =
@@ -727,6 +834,7 @@ const threadStartCommand = Command.make("start", {
727834
threadId: launched.threadId,
728835
runId: launched.runId,
729836
json: flags.json,
837+
followWakes: true,
730838
});
731839
}
732840
yield* flags.json ? printJson(launched) : Console.log(launched.threadId);
@@ -753,13 +861,20 @@ const threadSendCommand = Command.make("send", {
753861
yield* withThreadTools(
754862
flags,
755863
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;
756866
const sent = yield* tools.call(
757867
"t3_thread_send",
758868
{ threadId, message, mode: "queue" },
759869
OrchestratorMcpThreadSendResult,
760870
);
761871
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+
});
763878
}
764879
yield* flags.json
765880
? printJson(sent)

0 commit comments

Comments
 (0)