fix(orchestrator/grok): Prevent spurious wake run after in-turn monitors #4218
Mergedmwolson(GitHub profile, opens in a new tab)merged 2 commits into
t3code/codex-turn-mapping fromrefs/pull/4218/headmerged by juliusmarminge View on GitHubgithub-actions[bot](GitHub profile, opens in a new tab) added labelsvouch:unvouched
github-actions[bot](GitHub profile, opens in a new tab) added labelssize:L
- apps/server/src/orchestration-v2/Adapters/AcpAdapterV2.ts line 2944OutdatedResolvedby cursor[bot](GitHub profile, opens in a new tab)@@ -2849,3 +2926,4 @@28492926 // completed." spam).28502927 if (28512928 !alreadyHandledToolUpdate &&2929+ !isInTurnHandledAgentChatter &&apps/server/src/orchestration-v2/Adapters/AcpAdapterV2.ts line 2818OutdatedResolvedby cursor[bot](GitHub profile, opens in a new tab)@@ -2764,9 +2790,14 @@2790+ if (mutation.status === "running") {2791+ // Straggler running notice after a genuine end: suppress residual2792+ // monitor-prompt agent chatter so it cannot open a synthetic wake.2793+ if (taskAlreadyEnded && acpPostSettleMonitorPromptShouldSuppress(mutation)) {2794+ yield* Ref.set(suppressPostSettleMonitorPrompt, true);2795+ }2796+ return;27642797 }2765- if (mutation.status !== "running") {2798+ // First genuine terminal: allow subsequent agent output. Re-delivery2799+ // of an already-ended terminal must not un-suppress residual ack2800+ // chatter from the injected monitor-event turn.2801+ if (!taskAlreadyEnded) {27662802 yield* Ref.set(suppressPostSettleMonitorPrompt, false);2767- yield* Ref.update(handledBackgroundTaskIdsInActiveTurn, (current) => {2768- if (!current.has(mutation.taskId)) return current;2769- const next = new Set(current);2770- next.delete(mutation.taskId);2771- return next;2772- });2803+ }apps/server/src/orchestration-v2/Adapters/AcpAdapterV2.ts line 4269OutdatedResolvedby cursor[bot](GitHub profile, opens in a new tab)@@ -4152 +4222,21 @@41524222 yield* Ref.set(activeTurn, null);4223+ // A mid-turn background completion may have deferred its offer while4224+ // this root turn was still streaming. Once the turn leaves the active4225+ // slot, re-check: empty running set + residual wake evidence (or a4226+ // mid-turn unreported completion) means a legitimate unhandled4227+ // completion can open exactly one continuation. Sticky4228+ // continuationRequested prevents double-offer if a later frame also4229+ // races into offerContinuationRun.4230+ if (4231+ postSettleContinuationEnabled &&4232+ settledStatus === "completed" &&4233+ !directStopQuarantine &&4234+ (yield* Ref.get(runningBackgroundTaskIds)).size === 0 &&4235+ ((yield* Ref.get(wakeBuffer)).length > 0 ||4236+ (yield* Ref.get(midTurnUnreportedCompletedTaskIds)).size > 0)4237+ ) {4238+ const sessionId = yield* Ref.get(activeSessionId);4239+ if (sessionId !== null) {4240+ yield* offerContinuationRun(sessionId);4241+ }4242+ }
- apps/server/src/orchestration-v2/Adapters/AcpAdapterV2.ts line 2860OutdatedResolvedby cursor[bot](GitHub profile, opens in a new tab)@@ -2776,5 +2828,18 @@2776- (yield* Ref.get(runningBackgroundTaskIds)).size === 0 &&2777- (yield* Ref.get(wakeBuffer)).length > 02828+ !activeContext.promptSettled &&2829+ !handled.has(mutation.taskId) &&2830+ activeContext.toolCallIdsByBackgroundTaskId.has(mutation.taskId)27782831 ) {2779- yield* offerContinuationRun(sessionId);2832+ yield* Ref.update(midTurnUnreportedCompletedTaskIds, (current) =>2833+ new Set(current).add(mutation.taskId),2834+ );27802835 }2836+ return;2837+ }2838+ if (2839+ postSettleContinuationEnabled &&2840+ (yield* Ref.get(activeSessionId)) === sessionId &&2841+ (yield* Ref.get(runningBackgroundTaskIds)).size === 0 &&2842+ ((yield* Ref.get(wakeBuffer)).length > 0 ||2843+ (yield* Ref.get(midTurnUnreportedCompletedTaskIds)).size > 0)2844+ ) {2845+ yield* offerContinuationRun(sessionId);
- apps/server/src/orchestration-v2/Adapters/AcpAdapterV2.ts line 4256OutdatedResolvedby cursor[bot](GitHub profile, opens in a new tab)@@ -4152,0 +4236,21 @@4236+ // this root turn was still streaming. Once the turn leaves the active4237+ // slot, re-check: empty running set + residual wake evidence (or a4238+ // mid-turn unreported completion) means a legitimate unhandled4239+ // completion can open exactly one continuation. Sticky4240+ // continuationRequested prevents double-offer if a later frame also4241+ // races into offerContinuationRun.4242+ if (4243+ postSettleContinuationEnabled &&4244+ settledStatus === "completed" &&4245+ !directStopQuarantine &&4246+ (yield* Ref.get(runningBackgroundTaskIds)).size === 0 &&4247+ ((yield* Ref.get(wakeBuffer)).length > 0 ||4248+ (yield* Ref.get(midTurnUnreportedCompletedTaskIds)).size > 0)4249+ ) {4250+ const sessionId = yield* Ref.get(activeSessionId);4251+ if (sessionId !== null) {4252+ yield* offerContinuationRun(sessionId);4253+ }4254+ }4255+ // Turn boundary owns these marks; do not let them arm a later turn.4256+ yield* Ref.set(midTurnUnreportedCompletedTaskIds, new Set());apps/server/src/orchestration-v2/Adapters/AcpAdapterV2.ts line 4513OutdatedResolvedby cursor[bot](GitHub profile, opens in a new tab)@@ -4351,9 +4455,21 @@43514455 });43524456 yield* Ref.set(suppressPostSettleMonitorPrompt, false);43534457 yield* Ref.set(handledBackgroundTaskIdsInActiveTurn, new Set());4458+ yield* Ref.set(midTurnUnreportedCompletedTaskIds, new Set());43544459 // Continuation turns attach to wake traffic the agent already produced43554460 // after the prior root turn settled; do not re-prompt the ACP session.43564461 const isContinuationTurn =43574462 postSettleContinuationEnabled &&43584463 turnInput.message.createdBy === "agent" &&43594464 turnInput.message.creationSource === "provider";4465+ // User turns must not inherit prior-turn wake residue. Stale injected-4466+ // turn ack chatter buffered for in-turn-handled work can otherwise4467+ // arm a mid-turn offer when a later monitor completes (multiturn4468+ // live repro). Continuation turns keep the buffer so attach mode can4469+ // drain it. Still-running tasks remain in runningBackgroundTaskIds4470+ // and re-buffer evidence when they complete; a user message means4471+ // this turn owns the conversation, so prior wake frames cannot be4472+ // legitimate for a synthetic continuation of the previous turn.4473+ if (!isContinuationTurn) {4474+ yield* Ref.set(wakeBuffer, []);4475+ }apps/server/src/orchestration-v2/Adapters/AcpAdapterV2.ts line 3104OutdatedResolvedby cursor[bot](GitHub profile, opens in a new tab)@@ -3003,6 +3072,18 @@30033072 case "agent_message_chunk":30043073 if (update.content.type === "text") {30053074 // The injected-turn report is streaming; the normal debounce3006- // after the last chunk takes over from here.3075+ // after the last chunk takes over from here. Drop matching3076+ // mid-turn armed ids so finalize does not open a duplicate3077+ // wake after the report already projected into this turn.30073078 if (context.pendingInjectedReport.size > 0) {3079+ const reportedTaskIds = [...context.pendingInjectedReport];30083080 context.pendingInjectedReport.clear();3081+ yield* Ref.update(midTurnUnreportedCompletedTaskIds, (current) => {3082+ let next: Set<string> | null = null;3083+ for (const taskId of reportedTaskIds) {3084+ if (!current.has(taskId)) continue;3085+ if (next === null) next = new Set(current);3086+ next.delete(taskId);3087+ }3088+ return next ?? current;3089+ });
- apps/server/src/orchestration-v2/Adapters/AcpAdapterV2.ts line 4631OutdatedResolvedby cursor[bot](GitHub profile, opens in a new tab)@@ -4460,6 +4585,18 @@44604585 context.promptSettled = true;44614586 context.promptSettledStatus = "completed";44624587 if (drained.length === 0) {4463- yield* finalizeTurn(context, "completed", undefined, {4464- drainTrailingChunks: true,4465- });4588+ // Empty wakeBuffer (midTurn-only offer): wait the quiet window4589+ // so late CLI frames can attach. scheduleDeferredFinalize4590+ // no-ops without deferFinalizeForBackgroundWork; fall back to4591+ // immediate finalize so the turn cannot wedge.4592+ if (flavor.deferFinalizeForBackgroundWork === true) {4593+ if (hasDeferredBackgroundWork(context)) {4594+ yield* rearmDeferredFinalize(context);4595+ } else {4596+ yield* scheduleDeferredFinalize(context);4597+ }4598+ } else {4599+ yield* finalizeTurn(context, "completed", undefined, {4600+ drainTrailingChunks: true,4601+ });4602+ }
- apps/server/src/orchestration-v2/Adapters/AcpAdapterV2.ts line 4280OutdatedResolvedby cursor[bot](GitHub profile, opens in a new tab)@@ -4153,0 +4246,21 @@4246+ !directStopQuarantine &&4247+ (yield* Ref.get(runningBackgroundTaskIds)).size === 0 &&4248+ ((yield* Ref.get(wakeBuffer)).length > 0 ||4249+ (yield* Ref.get(midTurnUnreportedCompletedTaskIds)).size > 0)4250+ ) {4251+ const sessionId = yield* Ref.get(activeSessionId);4252+ if (sessionId !== null) {4253+ yield* offerContinuationRun(sessionId);4254+ }4255+ }4256+ // Clear mid-turn unreported marks unless a completed turn still has4257+ // running background work: keep them so the post-finalize gate can4258+ // offer once when the last task ends. Interrupted/failed turns must4259+ // not leave marks that open a wake after interrupt (quarantine owns4260+ // that path). Non-continuation turn start also clears (~user turn).4261+ if (4262+ settledStatus !== "completed" ||4263+ (yield* Ref.get(runningBackgroundTaskIds)).size === 04264+ ) {4265+ yield* Ref.set(midTurnUnreportedCompletedTaskIds, new Set());4266+ }
mwolson(GitHub profile, opens in a new tab) changed the title
- apps/server/src/orchestration-v2/Adapters/AcpAdapterV2.ts line 4513Resolvedby cursor[bot](GitHub profile, opens in a new tab)@@ -4358,8 +4485,21 @@43584485 yield* Ref.set(suppressPostSettleMonitorPrompt, false);43594486 yield* Ref.set(handledBackgroundTaskIdsInActiveTurn, new Set());4487+ yield* Ref.set(midTurnUnreportedCompletedTaskIds, new Set());43604488 // Continuation turns attach to wake traffic the agent already produced43614489 // after the prior root turn settled; do not re-prompt the ACP session.43624490 const isContinuationTurn =43634491 postSettleContinuationEnabled &&43644492 turnInput.message.createdBy === "agent" &&43654493 turnInput.message.creationSource === "provider";4494+ const isAppOwnedWakeTurn = acpIsAppOwnedWakeTurn(turnInput.message);4495+ // User turns must not inherit prior-turn wake residue. Stale injected-4496+ // turn ack chatter buffered for in-turn-handled work can otherwise4497+ // arm a mid-turn offer when a later monitor completes (multiturn4498+ // live repro). Continuation turns keep the buffer so attach mode can4499+ // drain it. Still-running tasks remain in runningBackgroundTaskIds4500+ // and re-buffer evidence when they complete; a user message means4501+ // this turn owns the conversation, so prior wake frames cannot be4502+ // legitimate for a synthetic continuation of the previous turn.4503+ if (!isContinuationTurn && !isAppOwnedWakeTurn) {4504+ yield* Ref.set(wakeBuffer, []);4505+ }
juliusmarminge merged this82d7d40
juliusmarminge closed this
Sign in to join this conversation.


Important
Review skipped#
Auto reviews are disabled on this repository. Please check the settings in the CodeRabbit UI or the
.coderabbit.yamlfile in this repository. To trigger a single review, invoke the@coderabbitai reviewcommand.⚙️ Run configuration
Configuration used: Repository UI
Review profile: CHILL
Plan: Pro Plus
Run ID:
bd8e4dda-0bd6-43ee-b141-173861396170You can disable this status message by setting the
reviews.review_statustofalsein the CodeRabbit configuration file.Use the checkbox below for a quick retry:
✨ Finishing Touches
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.
❤️ Share