fix(server): Stop also stops delegated tasks and pull request watches #16002
Mergedt3dotgg(GitHub profile, opens in a new tab)merged 10 commits into
main fromrefs/pull/16002/headmerged by t3dotgg(GitHub profile, opens in a new tab) 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:L
juliusmarminge added labelsmacroscope-review
- apps/server/src/orchestration-v2/EffectOutbox.ts line 121Resolvedby t3dotgg(GitHub profile, opens in a new tab)@@ -113,3 +118,4 @@113118 "terminal.cleanup",114119 "attachment.cleanup",115120 "thread-title.generate",121+ "delegated-tasks.stop",
- @@ -2368,3 +2370,14 @@23682370 threadId: command.threadId,23692371 });23702372 }2373+ // Only the agent's watch_pull_request links as "agent". A call that raced a Stop may land2374+ // after its run ended, so the latest run decides. A user can still watch after a Stop.2375+ if (2376+ command.type === "thread.pull-request.watch" &&2377+ command.watching &&2378+ command.link?.source === "agent"2379+ ) {2380+ const { runs } = yield* projectionStore2381+ .getThreadRecords(command.threadId, ["runs"])2382+ .pipe(mapDispatchError(command));2383+ const latest = latestDequeuedRun(runs);apps/server/src/orchestration-v2/Orchestrator.ts line 8596OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -8401,0 +8576,21 @@8576+ // A failed interrupt may have written part of its events, so they are kept apart.8577+ if (Exit.isSuccess(interrupted)) {8578+ const written = yield* Ref.get(interruptEvents);8579+ const enqueued = yield* Ref.get(interruptEffects);8580+ yield* Ref.update(events, (existing) => [...existing, ...written]);8581+ yield* Ref.update(effects, (existing) => [...existing, ...enqueued]);8582+ return interrupted.value;8583+ }8584+ yield* Effect.logWarning("thread.stop could not interrupt the running turn", {8585+ threadId: command.threadId,8586+ runId: target.id,8587+ cause: interrupted.cause,8588+ });8589+ }8590+ const now = yield* DateTime.now;8591+ // Record that Stop reached the thread's finished run too. A late tool call from its8592+ // agent then starts nothing, and a restart that cut it does not continue it.8593+ const latest = latestDequeuedRun(projection.runs);8594+ if (8595+ latest !== undefined &&8596+ !hasLiveRun({ runs: [latest] }) &&
- apps/server/src/orchestration-v2/Orchestrator.ts lines 8647 to 8652Resolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -8401,0 +8553,21 @@8553+ const interrupted = yield* Effect.exit(8554+ dispatchRunInterrupt(8555+ {8556+ type: "run.interrupt",8557+ commandId: command.commandId,8558+ threadId: command.threadId,8559+ runId: target.id,8560+ holdQueue: true,8561+ ...(command.reason === undefined ? {} : { reason: command.reason }),8562+ },8563+ interruptEvents,8564+ interruptEffects,8565+ ),8566+ );8567+ // A failed interrupt may have written part of its events, so they are kept apart.8568+ if (Exit.isSuccess(interrupted)) {8569+ const written = yield* Ref.get(interruptEvents);8570+ const enqueued = yield* Ref.get(interruptEffects);8571+ yield* Ref.update(events, (existing) => [...existing, ...written]);8572+ yield* Ref.update(effects, (existing) => [...existing, ...enqueued]);8573+ return interrupted.value;
github-actions[bot](GitHub profile, opens in a new tab) removed labelssize:L
github-actions[bot](GitHub profile, opens in a new tab) added labelssize:XL
- apps/server/src/orchestration-v2/Orchestrator.ts lines 8647 to 8653Resolvedby t3dotgg(GitHub profile, opens in a new tab)@@ -8413,0 +8566,21 @@8566+ dispatchRunInterrupt(8567+ {8568+ type: "run.interrupt",8569+ commandId: command.commandId,8570+ threadId: command.threadId,8571+ runId: target.id,8572+ holdQueue: true,8573+ ...(command.reason === undefined ? {} : { reason: command.reason }),8574+ },8575+ interruptEvents,8576+ interruptEffects,8577+ ),8578+ );8579+ // A failed interrupt may have written part of its events, so they are kept apart.8580+ if (Exit.isSuccess(interrupted)) {8581+ const written = yield* Ref.get(interruptEvents);8582+ const enqueued = yield* Ref.get(interruptEffects);8583+ yield* Ref.update(events, (existing) => [...existing, ...written]);8584+ yield* Ref.update(effects, (existing) => [...existing, ...enqueued]);8585+ return interrupted.value;8586+ }
- apps/server/src/orchestration-v2/Orchestrator.ts line 8620Resolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -8413,0 +8597,21 @@8597+ effects: Ref.Ref<Array<PendingOrchestrationEffectV2>>,8598+ ) =>8599+ Effect.gen(function* () {8600+ const projection = yield* projectionStore8601+ .getThreadRecords(command.threadId, ["runs", "providerThreads", "turnItems", "subagents"], {8602+ turnItemTypes: ["command_execution", "dynamic_tool", "subagent"],8603+ turnItemStatuses: ["pending", "running", "waiting"],8604+ })8605+ .pipe(8606+ Effect.mapError(8607+ (cause) => new OrchestratorProjectionError({ threadId: command.threadId, cause }),8608+ ),8609+ );8610+ if (projection.thread.deletedAt !== null) return undefined;8611+ const latestRun = projection.runs.at(-1);8612+ const target =8613+ projection.runs.findLast(8614+ (run) =>8615+ run.status === "preparing" || run.status === "starting" || run.status === "running",8616+ ) ??8617+ (derivePendingBackgroundWork({
- apps/server/src/orchestration-v2/Orchestrator.ts line 8230Resolvedby t3dotgg(GitHub profile, opens in a new tab)@@ -8045,4 +8227,4 @@80458227 ? undefined80468228 : projection.providerThreads.find((candidate) => candidate.id === run?.providerThreadId);80478229 const hasBackgroundWork =8048- run?.id === projection.runs.at(-1)?.id &&8230+ run?.id === projection.runs.findLast((candidate) => candidate.status !== "queued")?.id &&
t3dotgg(GitHub profile, opens in a new tab) closed this
Sign in to join this conversation.
Navigate logical layers of code changes, visualize relationships, and explore their blast radius.
Note
Reviews paused#
It looks like this branch is under active development. To avoid overwhelming you with review comments due to an influx of new commits, CodeRabbit has automatically paused this review. You can configure this behavior by changing the
reviews.auto_review.auto_pause_after_reviewed_commitssetting.Use the following commands to manage reviews:
@coderabbitai resumeto resume automatic reviews.@coderabbitai reviewto trigger a single review.Use the checkboxes below for quick actions:
📝 Walkthrough
Walkthrough#
The change adds
thread.stophandling for thread work, pull-request watches, and delegated tasks. MCPtask_cancelnow stops child threads, including after terminal task status, while preserving the published status.Changes#
Thread Stop and Delegated Task Cancellation
packages/contracts/src/orchestrationV2.ts,apps/server/src/orchestration-v2/Orchestrator.ts,apps/server/src/orchestration-v2/ThreadStop.test.ts,apps/server/src/orchestration-v2/BackgroundWorkStop.integration.test.ts,docs/orchestration-v2/feature-lifecycles.md,docs/user/*,apps/server/src/mcp/toolkits/pullRequests/tools.tsthread.stop, interrupts a selected run when possible, holds queued work, clears pull-request watches, records stop markers, and rejects applicable late requests and continuations. Tests cover stop behavior and queued work.apps/server/src/orchestration-v2/EffectOutbox.ts,apps/server/src/orchestration-v2/EffectWorker.ts,apps/server/src/orchestration-v2/ThreadManagementService.ts,apps/server/src/relay/AgentAwarenessRelay.test.tsthread.stopto app-owned child threads, continues after individual failures, and reports the first failure.apps/server/src/mcp/OrchestratorMcpService.ts,apps/server/src/mcp/OrchestratorMcpService.test.ts,apps/server/src/mcp/OrchestratorMcpToolkit.integration.test.ts,apps/server/src/mcp/toolkits/orchestrator/*,docs/orchestration-v2/orchestrator-mcp-server.mdtask_cancelstops the child thread and delegated tasks before disposing completion delivery. Terminal status remains unchanged. Nonterminal tasks without an active run are rejected unless they are waiting for children.Priority: ⬆️ High
Estimated code review effort: 4 (Complex) | ~50 minutes
Change: Bug fix
Sequence Diagram(s)#
Suggested reviewers:
eimexdevMerge Risk: 🟡 Moderate · up to
ba7d4Stop can leave delegated provider work running in this reachable case. Fix the cross-provider interruption gap before merging unless that behavior is explicitly accepted.
🚥 Pre-merge checks | ✅ 3 | ❌ 2
❌ Failed checks (2 warnings)#
✅ Passed checks (3 passed)
Full details: Description check
Resolution
Add a link to the triaged issue or discussion and the maintainer’s explicit approval comment. If approval is not required, explain why this change qualifies as a small, focused fix for an obvious bug; the described scope appears broader than that exception allows.
✨ Finishing Touches 💡 1
📝 Generate docstrings 💡
🧪 Generate unit tests (beta)
Comment `@coderabbitai help` to get the list of available commands.