fix(cli): thread --wait returns once background work the agent waits on has ended #34696
Mergedjuliusmarmingemerged 0 commits into
main fromrefs/pull/34696/headmerged by juliusmarminge View on GitHubgithub-actions[bot](GitHub profile, opens in a new tab) added labelsvouch:trusted
github-actions[bot](GitHub profile, opens in a new tab) added labelssize:M
- @@ -210,3 +210,18 @@210210 }),211211 );212212213+/** The reads that ask whether a run after `runId` holds a message someone sent. */214+const laterRunReads = (215+ laterRun: RunId,216+ items: ReadonlyArray<{ readonly runId: RunId; readonly type: string }>,217+) => [218+ {219+ recentRuns: [220+ { runId, ordinal: 1 },221+ { runId: laterRun, ordinal: 2 },222+ ],223+ },224+ { thread: {}, items, nextPosition: null, hasMore: false },225+];226+227+it.effect("--wait lasts until background work the agent waits on wakes it and that run ends", () =>@@ -500,0 +503,21 @@503+ * or background work. Only then is the work a later wait settles on this504+ * send's own.505+ */506+const threadIsIdle = Effect.fn("cli.thread.threadIsIdle")(function* (507+ tools: ThreadTools,508+ threadId: ThreadId,509+) {510+ const waited = yield* tools.call(511+ "t3_thread_wait",512+ { threadId, timeoutMs: 1, untilDelegatedWorkSettled: true },513+ OrchestratorMcpThreadWaitResult,514+ );515+ return !waited.timedOut;516+});517+518+/**519+ * The runs after `runId`, and whether one holds a message someone sent.520+ * Background work wakes a run with a notification, never a user message, so521+ * such a run is another request and its reply is not this one's.522+ */523+const runsAfter = Effect.fn("cli.thread.runsAfter")(function* (@@ -500,0 +514,21 @@514+ );515+ return !waited.timedOut;516+});517+518+/**519+ * The runs after `runId`, and whether one holds a message someone sent.520+ * Background work wakes a run with a notification, never a user message, so521+ * such a run is another request and its reply is not this one's.522+ */523+const runsAfter = Effect.fn("cli.thread.runsAfter")(function* (524+ tools: ThreadTools,525+ threadId: ThreadId,526+ runId: RunId,527+) {528+ const { recentRuns } = yield* tools.call(529+ "t3_thread_read",530+ { threadId, limit: 1, runLimit: 50 },531+ OrchestratorMcpThreadReadResult,532+ );533+ const own = recentRuns.find((run) => run.runId === runId);534+ if (own === undefined) return { later: new Set<RunId>(), sent: true };@@ -500,0 +519,21 @@519+ * The runs after `runId`, and whether one holds a message someone sent.520+ * Background work wakes a run with a notification, never a user message, so521+ * such a run is another request and its reply is not this one's.522+ */523+const runsAfter = Effect.fn("cli.thread.runsAfter")(function* (524+ tools: ThreadTools,525+ threadId: ThreadId,526+ runId: RunId,527+) {528+ const { recentRuns } = yield* tools.call(529+ "t3_thread_read",530+ { threadId, limit: 1, runLimit: 50 },531+ OrchestratorMcpThreadReadResult,532+ );533+ 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 };539+ const { items } = yield* readTimeline(tools, threadId, "messages");@@ -753,3 +836,5 @@753836 yield* withThreadTools(754837 flags,755838 Effect.fn("cli.thread.send")(function* (tools) {839+ // Work already running when this is sent is not this message's to wait for.840+ const followWakes = flags.wait ? yield* threadIsIdle(tools, threadId) : false;
github-actions[bot](GitHub profile, opens in a new tab) removed labelssize:M
github-actions[bot](GitHub profile, opens in a new tab) added labelssize:L
- @@ -514,5 +607,17 @@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 queued518- // 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;
- @@ -514,5 +607,17 @@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 queued518- // 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;
juliusmarminge merged this671f854
juliusmarminge closed this
Sign in to join this conversation.


Thread transfer impact#
✅ Thread transfer remains within every enforced ceiling.
Baseline:
4ae66e2· PR result:5819dda· Source CI: successScenario and decoded snapshot size
10 historical turns, 5 command tools per turn, 878.9 KiB retained MCP result per historical turn, and a 1.05 MiB retained result in the measured turn.
Updated in place by a trusted workflow. PR artifacts are strictly validated and never executed.