Skip to content

Commit b05aea4

Browse files
committed
fix(acp): clear zombie background tasks on cancel and turn settlement (#25599)
1 parent 0770446 commit b05aea4

5 files changed

Lines changed: 424 additions & 3 deletions

File tree

‎apps/server/src/orchestration-v2/Adapters/AntigravityAdapterV2.test.ts‎

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ import { ANTIGRAVITY_SIGN_IN_REQUIRED_MESSAGE } from "../../provider/antigravity
3737
import * as IdAllocator from "@t3tools/provider-core/server/IdAllocator";
3838
import * as ProviderAdapter from "@t3tools/provider-core/server/ProviderAdapter";
3939
import {
40+
acpCompletedTurnShouldTerminalizeTool,
4041
makeAntigravityAcpAdapterFlavor,
4142
makeAntigravityAdapterV2,
4243
} from "./AntigravityAdapterV2.ts";
@@ -133,6 +134,61 @@ describe("AntigravityAdapterV2 flavor", () => {
133134
outcome: { outcome: "selected", optionId: "opt-dev" },
134135
});
135136
});
137+
138+
it("enables background task tracking and completion de-registration", () => {
139+
assert.isTrue(flavor.backgroundTasksIdleOnTurnCompletion);
140+
assert.isDefined(flavor.extractBackgroundTaskId);
141+
assert.isDefined(flavor.extractBackgroundTaskCompletion);
142+
assert.isDefined(flavor.extractBackgroundToolMutation);
143+
144+
const taskId = flavor.extractBackgroundTaskId?.({
145+
toolCallId: "call-bg-1",
146+
title: "run_command",
147+
data: {
148+
rawOutput:
149+
"Tool is running as a background task with task id: 4bb4a8ac-7126-4867-a8d8-b4d62dc6c581/task-999",
150+
},
151+
});
152+
assert.equal(taskId, "4bb4a8ac-7126-4867-a8d8-b4d62dc6c581/task-999");
153+
154+
const completed = flavor.extractBackgroundTaskCompletion?.({
155+
toolCallId: "call-kill-1",
156+
title: "manage_task",
157+
status: "completed",
158+
data: {
159+
rawInput: { Action: "kill", TaskId: "4bb4a8ac-7126-4867-a8d8-b4d62dc6c581/task-999" },
160+
rawOutput: 'Task "4bb4a8ac-7126-4867-a8d8-b4d62dc6c581/task-999" cancelled.',
161+
},
162+
});
163+
assert.deepEqual(completed, [
164+
{
165+
taskId: "4bb4a8ac-7126-4867-a8d8-b4d62dc6c581/task-999",
166+
status: "completed",
167+
appendOutput: 'Task "4bb4a8ac-7126-4867-a8d8-b4d62dc6c581/task-999" cancelled.',
168+
},
169+
]);
170+
});
171+
172+
it("terminalizes background task tools on turn completion when backgroundTasksIdleOnTurnCompletion is enabled", () => {
173+
const bgTool = {
174+
toolCallId: "bg-1",
175+
title: "run_command",
176+
status: "inProgress" as const,
177+
data: {
178+
rawOutput: "Tool is running as a background task with task id: task-1",
179+
},
180+
};
181+
// Under Antigravity flavor (backgroundTasksIdleOnTurnCompletion: true), background tools terminalize on completed turn
182+
assert.isTrue(acpCompletedTurnShouldTerminalizeTool(bgTool, flavor));
183+
184+
// Under a flavor where backgroundTasksIdleOnTurnCompletion is false/undefined, background tools are excluded
185+
assert.isFalse(
186+
acpCompletedTurnShouldTerminalizeTool(bgTool, {
187+
...flavor,
188+
backgroundTasksIdleOnTurnCompletion: false,
189+
}),
190+
);
191+
});
136192
});
137193

138194
const layerSession = Layer.mergeAll(

‎apps/server/src/orchestration-v2/Adapters/AntigravityAdapterV2.ts‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,9 @@ import {
3838
antigravityApprovalOptions,
3939
antigravitySubagentOutput,
4040
classifyAntigravitySubagentToolCall,
41+
extractAntigravityBackgroundTaskId,
42+
extractAntigravityBackgroundTaskCompletion,
43+
extractAntigravityBackgroundToolMutation,
4144
extractAntigravityUserInputQuestion,
4245
makeAntigravityReadRepair,
4346
makeAntigravityUserInputResponse,
@@ -46,11 +49,14 @@ import {
4649
import { isAntigravitySignInRequiredError } from "../../provider/antigravityAuthSupport.ts";
4750
import {
4851
AcpProviderCapabilitiesV2,
52+
acpCompletedTurnShouldTerminalizeTool,
4953
makeAcpAdapterV2,
5054
type AcpAdapterV2Flavor,
5155
type AcpAdapterV2RuntimeInput,
5256
} from "@t3tools/provider-acp/server/adapter";
5357

58+
export { acpCompletedTurnShouldTerminalizeTool };
59+
5460
const ANTIGRAVITY_PROVIDER = ProviderDriverKind.make("antigravity");
5561

5662
const AntigravityProviderCapabilitiesV2 = {
@@ -233,6 +239,10 @@ export function makeAntigravityAcpAdapterFlavor(
233239
),
234240
// Loading history replays tool calls; resume avoids replay.
235241
subagentsIdleOnTurnCompletion: true,
242+
backgroundTasksIdleOnTurnCompletion: true,
243+
extractBackgroundTaskId: extractAntigravityBackgroundTaskId,
244+
extractBackgroundTaskCompletion: extractAntigravityBackgroundTaskCompletion,
245+
extractBackgroundToolMutation: extractAntigravityBackgroundToolMutation,
236246
...(options.onSessionEvent === undefined ? {} : { onSessionEvent: options.onSessionEvent }),
237247
applyModelSelection: Effect.fn("AntigravityAdapterV2.applyModelSelection")(function* ({
238248
runtime,

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

Lines changed: 174 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ import {
1414
normalizeAntigravityToolCall,
1515
sanitizeAntigravityToolPayload,
1616
makeAntigravityReadRepair,
17+
extractAntigravityBackgroundTaskId,
18+
extractAntigravityBackgroundTaskCompletion,
19+
extractAntigravityBackgroundToolMutation,
1720
} from "./AntigravityProtocol.ts";
1821
import {
1922
mergeToolCallState,
@@ -623,3 +626,174 @@ describe("Antigravity served reads", () => {
623626
});
624627
});
625628
});
629+
630+
describe("Antigravity background tasks", () => {
631+
it("extracts background task ID from background run_command and schedule outputs", () => {
632+
expect(
633+
extractAntigravityBackgroundTaskId({
634+
toolCallId: "call-1",
635+
title: "run_command",
636+
data: {
637+
rawOutput:
638+
"Tool is running as a background task with task id: 4bb4a8ac-7126-4867-a8d8-b4d62dc6c581/task-756\nTask Description: ./build.sh",
639+
},
640+
}),
641+
).toBe("4bb4a8ac-7126-4867-a8d8-b4d62dc6c581/task-756");
642+
643+
expect(
644+
extractAntigravityBackgroundTaskId({
645+
toolCallId: "call-2",
646+
title: "schedule",
647+
data: {
648+
rawOutput: "Scheduled task with ID: cron-job-99",
649+
},
650+
}),
651+
).toBe("cron-job-99");
652+
653+
expect(
654+
extractAntigravityBackgroundTaskId({
655+
toolCallId: "call-3",
656+
title: "run_command",
657+
data: {
658+
rawOutput: { taskId: "task-custom-123" },
659+
},
660+
}),
661+
).toBe("task-custom-123");
662+
663+
// Does not identify manage_task as a background task
664+
expect(
665+
extractAntigravityBackgroundTaskId({
666+
toolCallId: "call-4",
667+
title: "manage_task",
668+
data: {
669+
rawInput: { Action: "kill", TaskId: "task-custom-123" },
670+
rawOutput: 'Task "task-custom-123" cancelled.',
671+
},
672+
}),
673+
).toBeUndefined();
674+
});
675+
676+
it("extracts task completion when manage_task kills or inspects a task", () => {
677+
// manage_task kill completed
678+
expect(
679+
extractAntigravityBackgroundTaskCompletion({
680+
toolCallId: "call-kill-1",
681+
title: "manage_task",
682+
status: "completed",
683+
data: {
684+
rawInput: { Action: "kill", TaskId: "task-123" },
685+
rawOutput: 'Task "task-123" cancelled.',
686+
},
687+
}),
688+
).toEqual([
689+
{ taskId: "task-123", status: "completed", appendOutput: 'Task "task-123" cancelled.' },
690+
]);
691+
692+
// manage_task status shows completed
693+
expect(
694+
extractAntigravityBackgroundTaskCompletion({
695+
toolCallId: "call-status-1",
696+
title: "manage_task",
697+
status: "completed",
698+
data: {
699+
rawInput: { Action: "status", TaskId: "task-123" },
700+
rawOutput: "Task: task-123\nStatus: COMPLETED\nThe command exited with code 0.",
701+
},
702+
}),
703+
).toEqual([
704+
{
705+
taskId: "task-123",
706+
status: "completed",
707+
appendOutput: "Task: task-123\nStatus: COMPLETED\nThe command exited with code 0.",
708+
},
709+
]);
710+
711+
// manage_task status shows failed
712+
expect(
713+
extractAntigravityBackgroundTaskCompletion({
714+
toolCallId: "call-status-2",
715+
title: "manage_task",
716+
status: "completed",
717+
data: {
718+
rawInput: { Action: "status", TaskId: "task-123" },
719+
rawOutput: "Task: task-123\nStatus: FAILED\nThe command exited with code 1.",
720+
},
721+
}),
722+
).toEqual([
723+
{
724+
taskId: "task-123",
725+
status: "failed",
726+
appendOutput: "Task: task-123\nStatus: FAILED\nThe command exited with code 1.",
727+
},
728+
]);
729+
730+
// manage_task status shows running
731+
expect(
732+
extractAntigravityBackgroundTaskCompletion({
733+
toolCallId: "call-status-3",
734+
title: "manage_task",
735+
status: "completed",
736+
data: {
737+
rawInput: { Action: "status", TaskId: "task-123" },
738+
rawOutput: "Task: task-123\nStatus: RUNNING",
739+
},
740+
}),
741+
).toEqual([
742+
{
743+
taskId: "task-123",
744+
status: "running",
745+
appendOutput: "Task: task-123\nStatus: RUNNING",
746+
},
747+
]);
748+
});
749+
750+
it("extracts background tool mutations from injected system messages", () => {
751+
// Task finished with exit 0
752+
expect(
753+
extractAntigravityBackgroundToolMutation(
754+
'Task id "4bb4a8ac-7126-4867-a8d8-b4d62dc6c581/task-1265" finished with result:\n\nThe command exited with code 0.',
755+
),
756+
).toEqual([
757+
{
758+
taskId: "4bb4a8ac-7126-4867-a8d8-b4d62dc6c581/task-1265",
759+
status: "completed",
760+
appendOutput: expect.stringContaining("4bb4a8ac-7126-4867-a8d8-b4d62dc6c581/task-1265"),
761+
},
762+
]);
763+
764+
// Task finished with nonzero exit code
765+
expect(
766+
extractAntigravityBackgroundToolMutation(
767+
'Task id "4bb4a8ac-7126-4867-a8d8-b4d62dc6c581/task-1265" finished with result:\n\nThe command exited with code 1.',
768+
),
769+
).toEqual([
770+
{
771+
taskId: "4bb4a8ac-7126-4867-a8d8-b4d62dc6c581/task-1265",
772+
status: "failed",
773+
appendOutput: expect.stringContaining("4bb4a8ac-7126-4867-a8d8-b4d62dc6c581/task-1265"),
774+
},
775+
]);
776+
777+
// Task was canceled
778+
expect(
779+
extractAntigravityBackgroundToolMutation(
780+
'Task id "task-xyz" was canceled with result:\nStep was canceled: context canceled by manage_task',
781+
),
782+
).toEqual([
783+
{
784+
taskId: "task-xyz",
785+
status: "completed",
786+
appendOutput: expect.stringContaining("task-xyz"),
787+
},
788+
]);
789+
790+
// Task cancelled simple string
791+
expect(extractAntigravityBackgroundToolMutation('Task "task-xyz" cancelled.')).toEqual([
792+
{
793+
taskId: "task-xyz",
794+
status: "completed",
795+
appendOutput: 'Task "task-xyz" cancelled.',
796+
},
797+
]);
798+
});
799+
});

0 commit comments

Comments
 (0)