|
9 | 9 | ThreadId, |
10 | 10 | TrimmedNonEmptyString, |
11 | 11 | type OrchestratorMcpThreadListItem, |
| 12 | + type OrchestratorMcpThreadRun, |
12 | 13 | type OrchestratorMcpThreadStatus, |
13 | 14 | type OrchestratorMcpThreadTimelineItem, |
14 | 15 | } from "@t3tools/contracts"; |
@@ -39,6 +40,15 @@ import { projectLocationFlags, resolveCliAuthConfig } from "./config.ts"; |
39 | 40 | * soon, since `t3_thread_wait` only returns on a terminal run. |
40 | 41 | */ |
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 | +]); |
42 | 52 | const READ_PAGE_LIMIT = 100; |
43 | 53 | const TITLE_MAX_CHARS = 80; |
44 | 54 |
|
@@ -531,14 +541,12 @@ const runsAfter = Effect.fn("cli.thread.runsAfter")(function* ( |
531 | 541 | OrchestratorMcpThreadReadResult, |
532 | 542 | ); |
533 | 543 | 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 }; |
539 | 547 | const { items } = yield* readTimeline(tools, threadId, "messages"); |
540 | 548 | 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), |
542 | 550 | ); |
543 | 551 | return { later, sent }; |
544 | 552 | }); |
@@ -590,17 +598,34 @@ export const waitForRun = Effect.fn("cli.thread.waitForRun")(function* ( |
590 | 598 | const outcome = yield* ownRun; |
591 | 599 | if (!followWakes || outcome.type !== "ended" || outcome.status !== "completed") return outcome; |
592 | 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; |
593 | 613 | while (true) { |
594 | 614 | const waited = yield* waitSlice(WAIT_SLICE_MS, true); |
595 | 615 | // `resultRun` is the thread's latest finished run, whoever sent it. |
596 | 616 | const result = waited.timedOut ? undefined : waited.resultRun; |
597 | 617 | const after = yield* runsAfter(tools, threadId, finalRunId); |
598 | 618 | if (after.sent) return outcome; |
599 | 619 | 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; |
603 | 627 | } |
| 628 | + workOutlivedRun = true; |
604 | 629 | const { thread } = yield* tools.call( |
605 | 630 | "t3_thread_read", |
606 | 631 | { threadId, limit: 1, runLimit: 1 }, |
|
0 commit comments