Skip to content

Commit faf3773

Browse files
committed
fix(antigravity): keep subagent identity across late updates
1 parent 4d7945e commit faf3773

4 files changed

Lines changed: 165 additions & 22 deletions

File tree

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

Lines changed: 136 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,11 @@ import { ServerConfig } from "../../config.ts";
2626
import { ANTIGRAVITY_SIGN_IN_REQUIRED_MESSAGE } from "../antigravityAuthSupport.ts";
2727
import type { AcpSessionRuntimeEvent } from "../acp/AcpSessionRuntime.ts";
2828
import { makeAntigravityAcpRuntime } from "../acp/AntigravityAcpSupport.ts";
29-
import { parseSessionUpdateEvent } from "../acp/AcpRuntimeModel.ts";
29+
import {
30+
mergeToolCallState,
31+
parseSessionUpdateEvent,
32+
type AcpToolCallState,
33+
} from "../acp/AcpRuntimeModel.ts";
3034
import { makeAntigravityAdapter, type AntigravityAdapterOptions } from "./AntigravityAdapter.ts";
3135

3236
const instanceId = ProviderInstanceId.make("antigravity-test");
@@ -55,12 +59,13 @@ function nativeToolUpdate(
5559
AcpSchema.SessionNotification["update"],
5660
{ sessionUpdate: "tool_call" | "tool_call_update" }
5761
>,
62+
previous?: AcpToolCallState,
5863
) {
5964
const event = parseSessionUpdateEvent({ sessionId: nativeSessionId, update }).events.find(
6065
(event) => event._tag === "ToolCallUpdated",
6166
);
6267
if (!event) throw new Error("Expected a native tool update");
63-
return event;
68+
return { ...event, toolCall: mergeToolCallState(previous, event.toolCall) };
6469
}
6570

6671
const makeHarness = Effect.fn("makeAntigravityAdapterHarness")(function* (options?: {
@@ -903,6 +908,135 @@ it.layer(layer)("AntigravityAdapter", (it) => {
903908
}),
904909
);
905910

911+
for (const settlement of ["cancelled", "interrupted", "completed"] as const) {
912+
it.effect(`does not reopen a ${settlement} subagent on late merged updates`, () =>
913+
Effect.gen(function* () {
914+
const h = yield* makeHarness();
915+
yield* h.adapter.startSession({
916+
threadId,
917+
cwd: process.cwd(),
918+
runtimeMode: "approval-required",
919+
});
920+
const first = yield* h.adapter
921+
.sendTurn({ threadId, input: "Start review" })
922+
.pipe(Effect.forkChild);
923+
const firstPrompt = yield* h.nextPrompt;
924+
const started = nativeToolUpdate({
925+
sessionUpdate: "tool_call",
926+
toolCallId: "old:4",
927+
title: "Running start_subagent",
928+
kind: "other",
929+
status: "in_progress",
930+
rawInput: {},
931+
});
932+
yield* h.emitNative(started);
933+
yield* h.waitForEvent((event) => event.type === "task.progress");
934+
if (settlement === "cancelled") {
935+
yield* h.adapter.interruptTurn(threadId);
936+
} else {
937+
if (settlement === "completed") {
938+
yield* h.emitNative(
939+
nativeToolUpdate(
940+
{
941+
sessionUpdate: "tool_call_update",
942+
toolCallId: "old:4",
943+
status: "completed",
944+
rawOutput: "Original result.",
945+
},
946+
started.toolCall,
947+
),
948+
);
949+
}
950+
yield* Deferred.succeed(firstPrompt.result, { stopReason: "end_turn" });
951+
}
952+
yield* Fiber.join(first);
953+
yield* h.waitForEvent((event) => event.type === "turn.completed");
954+
const second = yield* h.adapter
955+
.sendTurn({ threadId, input: "Next review" })
956+
.pipe(Effect.forkChild);
957+
const secondPrompt = yield* h.nextPrompt;
958+
for (const status of ["in_progress", "completed"] as const) {
959+
yield* h.emitNative(
960+
nativeToolUpdate(
961+
{
962+
sessionUpdate: "tool_call_update",
963+
toolCallId: "old:4",
964+
status,
965+
rawOutput: "Late result.",
966+
},
967+
started.toolCall,
968+
),
969+
);
970+
}
971+
yield* h.emitNative(
972+
nativeToolUpdate({
973+
sessionUpdate: "tool_call",
974+
toolCallId: "new:4",
975+
title: "Running start_subagent",
976+
kind: "other",
977+
status: "completed",
978+
rawOutput: "New result.",
979+
}),
980+
);
981+
const completed = yield* h.waitForEvent((event) => event.type === "task.completed");
982+
expect(completed.payload.taskId).toBe("new:4");
983+
yield* Deferred.succeed(secondPrompt.result, { stopReason: "end_turn" });
984+
yield* Fiber.join(second);
985+
yield* h.waitForEvent((event) => event.type === "turn.completed");
986+
expect(h.seen.filter((event) => event.type === "task.progress")).toHaveLength(1);
987+
expect(
988+
h.seen.filter(
989+
(event) => event.type === "task.completed" && event.payload.taskId === "old:4",
990+
),
991+
).toHaveLength(settlement === "completed" ? 1 : 0);
992+
}),
993+
);
994+
}
995+
996+
it.effect("keeps MCP identity when later updates omit metadata", () =>
997+
Effect.gen(function* () {
998+
const h = yield* makeHarness();
999+
yield* h.adapter.startSession({
1000+
threadId,
1001+
cwd: process.cwd(),
1002+
runtimeMode: "approval-required",
1003+
});
1004+
const sending = yield* h.adapter
1005+
.sendTurn({ threadId, input: "Run an MCP tool" })
1006+
.pipe(Effect.forkChild);
1007+
const prompt = yield* h.nextPrompt;
1008+
const started = nativeToolUpdate({
1009+
sessionUpdate: "tool_call",
1010+
toolCallId: "mcp-4",
1011+
title: "Running start_subagent",
1012+
kind: "other",
1013+
status: "in_progress",
1014+
rawInput: { arguments: {} },
1015+
_meta: { is_mcp_tool_call: true },
1016+
});
1017+
yield* h.emitNative(started);
1018+
for (const status of ["in_progress", "completed"] as const) {
1019+
yield* h.emitNative(
1020+
nativeToolUpdate(
1021+
{
1022+
sessionUpdate: "tool_call_update",
1023+
toolCallId: "mcp-4",
1024+
status,
1025+
rawOutput: "MCP output.",
1026+
},
1027+
started.toolCall,
1028+
),
1029+
);
1030+
}
1031+
yield* Deferred.succeed(prompt.result, { stopReason: "end_turn" });
1032+
yield* Fiber.join(sending);
1033+
yield* h.waitForEvent((event) => event.type === "turn.completed");
1034+
expect(h.seen.filter((event) => event.type.startsWith("task."))).toHaveLength(0);
1035+
expect(h.seen.filter((event) => event.type === "item.updated")).toHaveLength(2);
1036+
expect(h.seen.filter((event) => event.type === "item.completed")).toHaveLength(1);
1037+
}),
1038+
);
1039+
9061040
it.effect("shows pending subagents and closes a denied invocation", () =>
9071041
Effect.gen(function* () {
9081042
const h = yield* makeHarness();

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

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -72,10 +72,10 @@ import {
7272
import {
7373
antigravityApprovalOptions,
7474
antigravitySubagentResult,
75+
classifyAntigravitySubagentToolCall,
7576
extractAntigravityUserInputQuestion,
7677
isAntigravityOpenCommand,
7778
isAntigravitySubagentReplayStart,
78-
isAntigravitySubagentToolCall,
7979
isAntigravityUserInputRequest,
8080
makeAntigravityUserInputResponse,
8181
normalizeAntigravityToolCall,
@@ -196,7 +196,8 @@ interface SessionContext {
196196
readonly approvals: Map<ApprovalRequestId, PendingApproval>;
197197
readonly questions: Map<ApprovalRequestId, PendingQuestion>;
198198
readonly commands: Map<string, OpenCommand>;
199-
readonly subagents: Map<string, OpenSubagent>;
199+
/** Keep only IDs after settlement or MCP exclusion so merged late updates cannot change identity. */
200+
readonly subagents: Map<string, OpenSubagent | "finished" | "mcp">;
200201
readonly turns: Array<{ id: TurnId; items: Array<unknown> }>;
201202
session: ProviderSession;
202203
activeTurnId: TurnId | undefined;
@@ -391,6 +392,7 @@ export const makeAntigravityAdapter = Effect.fn("makeAntigravityAdapter")(functi
391392
context.commandLock.withPermit(
392393
Effect.gen(function* () {
393394
for (const [id, subagent] of context.subagents) {
395+
if (subagent === "finished" || subagent === "mcp") continue;
394396
yield* emit({
395397
type: "task.updated",
396398
...(yield* stamp),
@@ -403,8 +405,8 @@ export const makeAntigravityAdapter = Effect.fn("makeAntigravityAdapter")(functi
403405
...(error ? { error } : {}),
404406
},
405407
});
408+
context.subagents.set(id, "finished");
406409
}
407-
context.subagents.clear();
408410
}),
409411
);
410412

@@ -428,6 +430,7 @@ export const makeAntigravityAdapter = Effect.fn("makeAntigravityAdapter")(functi
428430
context.disconnected ? "failed" : "cancelled",
429431
context.disconnected ? "Antigravity process stopped." : undefined,
430432
);
433+
context.subagents.clear();
431434
yield* emit({
432435
type: "session.exited",
433436
...(yield* stamp),
@@ -602,8 +605,13 @@ export const makeAntigravityAdapter = Effect.fn("makeAntigravityAdapter")(functi
602605
yield* context.commandLock.withPermit(
603606
Effect.gen(function* () {
604607
const toolCall = normalizeAntigravityToolCall(event.toolCall);
605-
const subagent = context.subagents.get(toolCall.toolCallId);
606-
if (subagent || isAntigravitySubagentToolCall(toolCall, event.rawPayload)) {
608+
const tracked = context.subagents.get(toolCall.toolCallId);
609+
if (tracked === "finished") return;
610+
const kind = classifyAntigravitySubagentToolCall(toolCall, event.rawPayload);
611+
const isMcp = tracked === "mcp" || kind === "mcp";
612+
if (isMcp) context.subagents.set(toolCall.toolCallId, "mcp");
613+
const subagent = tracked === "mcp" ? undefined : tracked;
614+
if (!isMcp && (subagent || kind === "subagent")) {
607615
const turnId = subagent?.turnId ?? context.activeTurnId;
608616
const linkage = subagentLinkage(toolCall.toolCallId);
609617
// Replay starts claim completion before the result says whether the call failed.
@@ -628,7 +636,7 @@ export const makeAntigravityAdapter = Effect.fn("makeAntigravityAdapter")(functi
628636
...(summary ? { summary } : {}),
629637
},
630638
});
631-
context.subagents.delete(toolCall.toolCallId);
639+
context.subagents.set(toolCall.toolCallId, "finished");
632640
} else {
633641
const status = toolCall.status === "pending" ? "pending" : "running";
634642
if (subagent?.status !== status) {

‎apps/server/src/provider/acp/AntigravityProtocol.test.ts‎

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import {
88
antigravityApprovalOptions,
99
antigravitySubagentResult,
1010
isAntigravitySubagentReplayStart,
11-
isAntigravitySubagentToolCall,
11+
classifyAntigravitySubagentToolCall,
1212
isAntigravityUserInputRequest,
1313
makeAntigravityUserInputResponse,
1414
normalizeAntigravitySessionUpdate,
@@ -24,28 +24,28 @@ describe("native Antigravity subagent tools", () => {
2424
it("recognizes only native invocation titles and excludes MCP tools", () => {
2525
const toolCall = { toolCallId: "trajectory:4", kind: "other", data: {} };
2626
for (const title of ["Running start_subagent", "Run start_subagent?"]) {
27-
expect(isAntigravitySubagentToolCall({ ...toolCall, title }, {})).toBe(true);
27+
expect(classifyAntigravitySubagentToolCall({ ...toolCall, title }, {})).toBe("subagent");
2828
expect(
29-
isAntigravitySubagentToolCall(
29+
classifyAntigravitySubagentToolCall(
3030
{ ...toolCall, title },
3131
{ update: { _meta: { is_mcp_tool_call: true } } },
3232
),
33-
).toBe(false);
33+
).toBe("mcp");
3434
}
3535
for (const title of [
3636
"Running subagent",
3737
"start_subagent",
3838
"Run command",
3939
"Running manage_task",
4040
]) {
41-
expect(isAntigravitySubagentToolCall({ ...toolCall, title }, {})).toBe(false);
41+
expect(classifyAntigravitySubagentToolCall({ ...toolCall, title }, {})).toBeUndefined();
4242
}
4343
expect(
44-
isAntigravitySubagentToolCall(
44+
classifyAntigravitySubagentToolCall(
4545
{ ...toolCall, title: "Running start_subagent", kind: "execute" },
4646
{},
4747
),
48-
).toBe(false);
48+
).toBeUndefined();
4949
});
5050

5151
it("recognizes history starts and bounds the native result", () => {

‎apps/server/src/provider/acp/AntigravityProtocol.ts‎

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -351,17 +351,18 @@ export function isAntigravityOpenCommand(toolCall: AcpToolCallState): boolean {
351351
}
352352

353353
/** ACP 1.1.1 exposes subagent invocations as ordinary tools, without child IDs or models. */
354-
export function isAntigravitySubagentToolCall(
354+
export function classifyAntigravitySubagentToolCall(
355355
toolCall: AcpToolCallState,
356356
rawPayload: unknown,
357-
): boolean {
357+
): "subagent" | "mcp" | undefined {
358+
if (
359+
(toolCall.kind !== undefined && toolCall.kind !== "other") ||
360+
(toolCall.title !== "Running start_subagent" && toolCall.title !== "Run start_subagent?")
361+
)
362+
return undefined;
358363
const update = Predicate.isObject(rawPayload) ? rawPayload.update : undefined;
359364
const meta = Predicate.isObject(update) ? update._meta : undefined;
360-
if (Predicate.isObject(meta) && meta.is_mcp_tool_call === true) return false;
361-
return (
362-
(toolCall.kind === undefined || toolCall.kind === "other") &&
363-
(toolCall.title === "Running start_subagent" || toolCall.title === "Run start_subagent?")
364-
);
365+
return Predicate.isObject(meta) && meta.is_mcp_tool_call === true ? "mcp" : "subagent";
365366
}
366367

367368
/** History sends a completed start before the separate result and its final status. */

0 commit comments

Comments
 (0)