Skip to content
113 changes: 113 additions & 0 deletions apps/server/src/orchestration-v2/Adapters/AntigravityAdapterV2.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
// @effect-diagnostics nodeBuiltinImport:off -- fixtures write a native session database.
import * as NodeSqlite from "node:sqlite";

import * as NodeCrypto from "@effect/platform-node/NodeCrypto";
import * as NodeServices from "@effect/platform-node/NodeServices";
import { assert, describe, it } from "@effect/vitest";
Expand Down Expand Up @@ -2129,6 +2132,116 @@ describe("AntigravityAdapterV2 work after the prompt ends", () => {
assert.equal(opened.cancels(), cancels);
}).pipe(Effect.provide(layerSession)),
);

// When manage_task(kill) cancels a background run_command, agy transitions its RUN_COMMAND step
// to CANCELED (6) in conversations/<sessionId>.db without emitting a terminal tool_call_update.
it.effect(
"finalizes the turn when a background command was canceled in the session database without a tool_call_update",
() =>
Effect.gen(function* () {
const fileSystem = yield* FileSystem.FileSystem;
const path = yield* Path.Path;
const profile = yield* fileSystem.makeTempDirectoryScoped({ prefix: "t3-agy-cmd-cancel-" });
const conversations = path.join(profile, "antigravity-acp", "conversations");
yield* fileSystem.makeDirectory(conversations, { recursive: true });
const db = new NodeSqlite.DatabaseSync(path.join(conversations, "mock-session-1.db"));
try {
db.exec(
"CREATE TABLE steps (idx INTEGER PRIMARY KEY, step_type INTEGER NOT NULL, status INTEGER NOT NULL, metadata BLOB, step_payload BLOB)",
);
db.prepare("INSERT INTO steps VALUES (14, 21, 6, NULL, NULL)").run();
} finally {
db.close();
}
const { command, startTurn, throughTerminal } = yield* openSessionWithRunningCommand(
{ WaitMsBeforeAsync: 10000 },
undefined,
undefined,
{ profileDirectory: profile },
);
yield* startTurn(1, false);
const run = yield* throughTerminal;
const terminal = run.at(-1);
assert.equal(terminal?.type === "turn.terminal" && terminal.status, "completed");
const reconciled = run.findLast(
(event) =>
event.type === "turn_item.updated" &&
event.turnItem.nativeItemRef?.nativeId === command.toolCallId,
);
assert.equal(
reconciled?.type === "turn_item.updated" && reconciled.turnItem.status,
"failed",
);
}).pipe(Effect.provide(layerSession)),
);

it.effect(
"keeps the run open when the session database confirms the background command is still running",
() =>
Effect.gen(function* () {
const fileSystem = yield* FileSystem.FileSystem;
const path = yield* Path.Path;
const profile = yield* fileSystem.makeTempDirectoryScoped({ prefix: "t3-agy-cmd-live-" });
const conversations = path.join(profile, "antigravity-acp", "conversations");
yield* fileSystem.makeDirectory(conversations, { recursive: true });
const encoder = new TextEncoder();
const varint = (value: number): number[] => {
const bytes: number[] = [];
while (value >= 0x80) {
bytes.push((value & 0x7f) | 0x80);
value = Math.floor(value / 0x80);
}
return [...bytes, value];
};
const field = (number: number, value: string | Uint8Array) => {
const bytes = typeof value === "string" ? encoder.encode(value) : value;
return [...varint(number * 8 + 2), ...varint(bytes.length), ...bytes];
};
const proto = (...parts: number[][]) => Uint8Array.from(parts.flat());
const metadata = proto(
field(
4,
proto(
field(1, "call_native_999"),
field(2, "run_command"),
field(3, JSON.stringify({ CommandLine: "node fetch_throughput.mjs" })),
),
),
);
const db = new NodeSqlite.DatabaseSync(path.join(conversations, "mock-session-1.db"));
try {
db.exec(
"CREATE TABLE steps (idx INTEGER PRIMARY KEY, step_type INTEGER NOT NULL, status INTEGER NOT NULL, metadata BLOB, step_payload BLOB)",
);
db.prepare("INSERT INTO steps VALUES (14, 21, 2, ?, NULL)").run(metadata);
} finally {
db.close();
}
const { command, usage, send, startTurn, until, throughPromptSettled } =
yield* openSessionWithRunningCommand({ WaitMsBeforeAsync: 10000 }, undefined, undefined, {
profileDirectory: profile,
});
yield* startTurn(1, false);
yield* throughPromptSettled;
yield* TestClock.adjust("10 minutes");
yield* send({
sessionUpdate: "tool_call_update",
...command,
status: "completed",
rawOutput: { output: "Command exited with code 0.", exit_code: 0 },
});
yield* send({
sessionUpdate: "agent_message_chunk",
content: { type: "text", text: "Command finished." },
});
yield* send(usage);
yield* TestClock.adjust("3 seconds");
const run = yield* until((event) => event.type === "turn.terminal");
const terminal = run.at(-1);
assert.equal(terminal?.type === "turn.terminal" && terminal.status, "completed");
assert.include(JSON.stringify(run), "Command finished.");
}).pipe(Effect.provide(layerSession)),
);
});

describe("AntigravityAdapterV2 subagent reads", () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import {
readAntigravityChildrenAtWork,
readAntigravityPendingReply,
} from "../../provider/acp/AntigravityChildWork.ts";
import { readAntigravityRunningCommands } from "../../provider/acp/AntigravityBackgroundCommands.ts";
import { readAntigravityClientTextFile } from "../../provider/acp/AntigravityClientFiles.ts";
import { watchAntigravityPromptOutcome } from "../../provider/acp/AntigravityTurnOutcome.ts";
import {
Expand Down Expand Up @@ -312,6 +313,12 @@ export function makeAntigravityAcpAdapterFlavor(
rootSessionId: sessionId,
toolCallIds: nativeTaskIds,
}),
toolsOutlivingPrompt: ({ sessionId, tools }) =>
readAntigravityRunningCommands({
profileDirectory,
sessionId,
tools,
}),
hasPendingReply: (sessionId: string) =>
readAntigravityPendingReply({
profileDirectory,
Expand Down
Loading
Loading