@@ -14,6 +14,9 @@ import {
1414 normalizeAntigravityToolCall ,
1515 sanitizeAntigravityToolPayload ,
1616 makeAntigravityReadRepair ,
17+ extractAntigravityBackgroundTaskId ,
18+ extractAntigravityBackgroundTaskCompletion ,
19+ extractAntigravityBackgroundToolMutation ,
1720} from "./AntigravityProtocol.ts" ;
1821import {
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