diff --git a/apps/server/src/orchestration-v2/Adapters/AntigravityAdapterV2.test.ts b/apps/server/src/orchestration-v2/Adapters/AntigravityAdapterV2.test.ts index fca472c6c433e..77a282c415beb 100644 --- a/apps/server/src/orchestration-v2/Adapters/AntigravityAdapterV2.test.ts +++ b/apps/server/src/orchestration-v2/Adapters/AntigravityAdapterV2.test.ts @@ -1319,6 +1319,34 @@ describe("AntigravityAdapterV2 work after the prompt ends", () => { }).pipe(Effect.provide(layerSession)), ); + it.effect("leaves no background work when the continuation run ends without starting", () => + Effect.gen(function* () { + const { session, command, offers, send, startTurn, throughTerminal } = + yield* openSessionWithRunningCommand({ IsDaemon: true, WaitMsBeforeAsync: 500 }); + yield* startTurn(1, false); + yield* throughTerminal; + yield* send({ + sessionUpdate: "tool_call_update", + ...command, + status: "completed", + rawOutput: { output: "Command exited with code 0.", exit_code: 0 }, + }); + assert.lengthOf(offers, 1, "the finished daemon must wake the thread"); + yield* offers[0]!.dispatchIfCurrent!(Effect.void); + assert.isTrue(yield* session.hasPendingBackgroundWork!, "the queued continuation is owed"); + + assert.isDefined( + offers[0]!.onRunEndedWithoutStarting, + "a cancelled queued continuation must release its wake", + ); + yield* offers[0]!.onRunEndedWithoutStarting!(); + assert.isFalse( + yield* session.hasPendingBackgroundWork!, + "Stop on the queued continuation must let the thread switch providers", + ); + }).pipe(Effect.provide(layerSession)), + ); + // Frames as agy 1.5.11 sent them for `run_command` with `WaitMsBeforeAsync`: the prompt returns // end_turn while the command runs, and the agent replies when the command finishes. it.effect("keeps the run open while a command the agent waits for still runs", () => diff --git a/packages/provider-acp/src/server/adapter.ts b/packages/provider-acp/src/server/adapter.ts index e214a51586f4c..0574939e5feda 100644 --- a/packages/provider-acp/src/server/adapter.ts +++ b/packages/provider-acp/src/server/adapter.ts @@ -5234,6 +5234,20 @@ export const makeAcpAdapterV2 = Effect.fn("makeAcpAdapterV2")(function* ( } }), ), + // Drops the wake traffic the unstarted run would have drained, as its start does. + onRunEndedWithoutStarting: () => + continuationPermit.withPermit( + Effect.gen(function* () { + if ((yield* Ref.get(continuationGeneration)) !== generation) return; + yield* Ref.update(continuationGeneration, (value) => value + 1); + yield* Ref.set(continuationRequested, false); + yield* Ref.set(wakeBuffer, []); + yield* Ref.update(wakeReports, ({ reports, offered }) => ({ + reports: new Map([...reports].filter(([key]) => !offered.has(key))), + offered: noWakeReports.offered, + })); + }), + ), dispatchIfCurrent: (effect) => continuationPermit.withPermit( Effect.gen(function* () {