Skip to content

Commit cdb26fe

Browse files
fix(antigravity): let Stop end commands that outlived their turn (#13388)
Co-authored-by: Claude Opus 5.5 (1M context) <[email protected]>
1 parent 87d8428 commit cdb26fe

2 files changed

Lines changed: 61 additions & 1 deletion

File tree

‎apps/server/src/provider/Layers/AntigravityAdapter.test.ts‎

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -775,6 +775,44 @@ it.layer(layer)("AntigravityAdapter", (it) => {
775775
}),
776776
);
777777

778+
it.effect("stops commands left running after a turn when the idle turn is stopped", () =>
779+
Effect.gen(function* () {
780+
const h = yield* makeHarness();
781+
yield* h.adapter.startSession({
782+
threadId,
783+
cwd: process.cwd(),
784+
runtimeMode: "approval-required",
785+
});
786+
const sending = yield* h.adapter
787+
.sendTurn({ threadId, input: "Start a watcher" })
788+
.pipe(Effect.forkChild);
789+
const prompt = yield* h.nextPrompt;
790+
yield* h.emitNative({
791+
_tag: "ToolCallUpdated",
792+
toolCall: {
793+
toolCallId: "watcher-1",
794+
kind: "execute",
795+
status: "inProgress",
796+
command: "tail -f log",
797+
data: {},
798+
},
799+
rawPayload: {},
800+
});
801+
yield* Deferred.succeed(prompt.result, { stopReason: "end_turn" });
802+
yield* Fiber.join(sending);
803+
const started = yield* h.waitForEvent((event) => event.type === "task.started");
804+
805+
// Monitoring's Stop reaches the adapter as a turn interrupt. With no
806+
// prompt to cancel, it has to end the session to stop the command.
807+
yield* h.adapter.interruptTurn(threadId);
808+
const stopped = yield* h.waitForEvent((event) => event.type === "task.completed");
809+
expect(stopped.payload).toMatchObject({ taskId: started.payload.taskId, status: "stopped" });
810+
yield* h.waitForEvent((event) => event.type === "session.exited");
811+
expect(yield* h.adapter.hasSession(threadId)).toBe(false);
812+
expect(h.controls.closed).toBe(1);
813+
}),
814+
);
815+
778816
it.effect("keeps a launched batch active while child tools continue", () =>
779817
Effect.gen(function* () {
780818
const h = yield* makeHarness();

‎apps/server/src/provider/Layers/AntigravityAdapter.ts‎

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1165,14 +1165,36 @@ export const makeAntigravityAdapter = Effect.fn("makeAntigravityAdapter")(functi
11651165
const interruptTurn: Adapter["interruptTurn"] = (threadId) =>
11661166
Effect.gen(function* () {
11671167
const context = yield* requireSession(threadId);
1168+
// A command that outlived its turn keeps running in the agent, and
1169+
// session/cancel only stops a prompt. The agent kills its background
1170+
// commands when its session closes, so Stop with nothing else running
1171+
// ends the session, as Claude's does. The next turn resumes it.
1172+
let idleWithCommands = false;
11681173
yield* context.promptLock
11691174
.withPermit(
11701175
Effect.gen(function* () {
1176+
// Decided under the prompt lock so a turn cannot start in between.
1177+
if (!context.promptFiber && [...context.commands.values()].some((c) => c.promoted)) {
1178+
context.stopped = true;
1179+
idleWithCommands = true;
1180+
return;
1181+
}
11711182
yield* cancelRequests(context);
11721183
yield* context.runtime.cancel;
11731184
}),
11741185
)
1175-
.pipe(Effect.mapError((cause) => mapAntigravityError(threadId, "session/cancel", cause)));
1186+
.pipe(
1187+
Effect.mapError((cause) => mapAntigravityError(threadId, "session/cancel", cause)),
1188+
// Once marked stopped the session must close, even if this call is
1189+
// interrupted, or it is left unreachable with its commands running.
1190+
Effect.ensuring(
1191+
Effect.suspend(() =>
1192+
idleWithCommands
1193+
? withThreadLock(threadId, stopContext(context)).pipe(Effect.ignore)
1194+
: Effect.void,
1195+
),
1196+
),
1197+
);
11761198
});
11771199

11781200
const respondToRequest: Adapter["respondToRequest"] = (threadId, requestId, decision) =>

0 commit comments

Comments
 (0)