Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
102 changes: 102 additions & 0 deletions apps/server/src/provider/Layers/AntigravityAdapter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1435,6 +1435,108 @@ it.layer(layer)("AntigravityAdapter", (it) => {
}),
);

it.effect("Stop closes an idle session with a promoted native command", () =>
Effect.gen(function* () {
const h = yield* makeHarness();
yield* h.adapter.startSession({ threadId, cwd: process.cwd(), runtimeMode: "full-access" });
const sending = yield* h.adapter
.sendTurn({ threadId, input: "Start a watcher" })
.pipe(Effect.forkChild);
const prompt = yield* h.nextPrompt;
yield* h.emitNative({
_tag: "ToolCallUpdated",
toolCall: {
toolCallId: "watcher-stop",
kind: "execute",
status: "inProgress",
command: "tail -f log",
data: {},
},
rawPayload: {},
});
yield* Deferred.succeed(prompt.result, { stopReason: "end_turn" });
yield* Fiber.join(sending);
const started = yield* h.waitForEvent((event) => event.type === "task.started");
yield* h.adapter.interruptTurn(threadId);
const stopped = yield* h.waitForEvent((event) => event.type === "task.completed");
expect(stopped.payload).toMatchObject({ taskId: started.payload.taskId, status: "stopped" });
yield* h.waitForEvent((event) => event.type === "session.exited");
expect(yield* h.adapter.hasSession(threadId)).toBe(false);
expect(h.controls.closed).toBe(1);
}),
);

it.effect("Stop keeps the session during an active prompt with an older promoted command", () =>
Effect.gen(function* () {
const h = yield* makeHarness();
yield* h.adapter.startSession({ threadId, cwd: process.cwd(), runtimeMode: "full-access" });
const first = yield* h.adapter.sendTurn({ threadId, input: "Start a watcher" });
const firstPrompt = yield* h.nextPrompt;
yield* h.emitNative({
_tag: "ToolCallUpdated",
toolCall: {
toolCallId: "watcher-active-stop",
kind: "execute",
status: "inProgress",
command: "tail -f log",
data: {},
},
rawPayload: {},
});
yield* Deferred.succeed(firstPrompt.result, { stopReason: "end_turn" });
const started = yield* h.waitForEvent((event) => event.type === "task.started");
yield* h.waitForEvent(
(event): event is Extract<ProviderRuntimeEvent, { type: "turn.completed" }> =>
event.type === "turn.completed" && event.turnId === first.turnId,
);
yield* h.adapter.sendTurn({ threadId, input: "Continue" });
yield* h.nextPrompt;
yield* h.adapter.interruptTurn(threadId);
yield* h.waitForEvent((event) => event.type === "turn.completed");
expect(yield* h.adapter.hasSession(threadId)).toBe(true);
expect(h.controls.closed).toBe(0);
expect(
h.seen.some(
(event) =>
event.type === "task.completed" && event.payload.taskId === started.payload.taskId,
),
).toBe(false);
}),
);

it.effect("rejects a new turn while Stop closes an idle promoted command", () =>
Effect.gen(function* () {
const h = yield* makeHarness({ holdClose: true });
yield* h.adapter.startSession({ threadId, cwd: process.cwd(), runtimeMode: "full-access" });
yield* h.adapter.sendTurn({ threadId, input: "Start a watcher" });
const prompt = yield* h.nextPrompt;
yield* h.emitNative({
_tag: "ToolCallUpdated",
toolCall: {
toolCallId: "watcher-close-race",
kind: "execute",
status: "inProgress",
command: "tail -f log",
data: {},
},
rawPayload: {},
});
yield* Deferred.succeed(prompt.result, { stopReason: "end_turn" });
yield* h.waitForEvent((event) => event.type === "task.started");
yield* h.waitForEvent((event) => event.type === "turn.completed");

const stopping = yield* h.adapter.interruptTurn(threadId).pipe(Effect.forkChild);
yield* Deferred.await(h.closeStarted);
const next = yield* h.adapter.sendTurn({ threadId, input: "New turn" }).pipe(Effect.result);
expect(next._tag).toBe("Failure");
expect(h.hasActivePrompt()).toBe(false);
yield* Deferred.succeed(h.closeRelease, undefined);
yield* Fiber.join(stopping);
yield* h.waitForEvent((event) => event.type === "session.exited");
expect(yield* h.adapter.hasSession(threadId)).toBe(false);
}),
);

it.effect("keeps a launched batch active while child tools continue", () =>
Effect.gen(function* () {
const h = yield* makeHarness();
Expand Down
24 changes: 23 additions & 1 deletion apps/server/src/provider/Layers/AntigravityAdapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2024,6 +2024,7 @@ export const makeAntigravityAdapter = Effect.fn("makeAntigravityAdapter")(functi
const interruptTurn: Adapter["interruptTurn"] = (threadId) =>
Effect.gen(function* () {
const context = yield* requireSession(threadId);
let stopIdleCommands = false;
if (context.promptFiber !== undefined || context.activeTurnId !== undefined) {
context.userCancelRequested = true;
context.explicitStopRequested = true;
Expand All @@ -2034,6 +2035,18 @@ export const makeAntigravityAdapter = Effect.fn("makeAntigravityAdapter")(functi
yield* context.promptLock
.withPermit(
Effect.gen(function* () {
// Native commands can outlive their turn. Cancellation only stops a
// prompt, so close the session to end promoted commands on Stop.
// Fence a new turn while still holding the prompt lock.
if (
!context.promptFiber &&
!context.activeTurnId &&
[...context.commands.values()].some((command) => command.promoted)
) {
context.stopped = true;
stopIdleCommands = true;
return;
}
yield* cancelRequests(context);
if (!context.disconnected) {
yield* Effect.ignore(context.runtime.cancel);
Expand All @@ -2043,7 +2056,16 @@ export const makeAntigravityAdapter = Effect.fn("makeAntigravityAdapter")(functi
}
}),
)
.pipe(Effect.mapError((cause) => mapAntigravityError(threadId, "session/cancel", cause)));
.pipe(
Effect.mapError((cause) => mapAntigravityError(threadId, "session/cancel", cause)),
Effect.ensuring(
Effect.suspend(() =>
stopIdleCommands
? withThreadLock(threadId, stopContext(context)).pipe(Effect.ignore)
: Effect.void,
),
),
);
});

const respondToRequest: Adapter["respondToRequest"] = (threadId, requestId, decision) =>
Expand Down
Loading
Loading