@@ -16,12 +16,15 @@ import { htmlRenderFromToolItem } from "@t3tools/shared/toolOutput";
1616import * as Crypto from "effect/Crypto" ;
1717import * as DateTime from "effect/DateTime" ;
1818import * as Deferred from "effect/Deferred" ;
19+ import type * as Duration from "effect/Duration" ;
1920import * as Effect from "effect/Effect" ;
2021import * as Exit from "effect/Exit" ;
2122import * as FileSystem from "effect/FileSystem" ;
2223import * as Layer from "effect/Layer" ;
2324import * as Path from "effect/Path" ;
25+ import * as Queue from "effect/Queue" ;
2426import * as Stream from "effect/Stream" ;
27+ import * as TestClock from "effect/testing/TestClock" ;
2528import { ChildProcessSpawner } from "effect/process" ;
2629import type * as EffectAcpSchema from "effect-acp/compat" ;
2730import * as EffectAcpErrors from "effect-acp/errors" ;
@@ -36,10 +39,12 @@ import { normalizeAntigravitySessionUpdate } from "../../provider/acp/Antigravit
3639import { ANTIGRAVITY_SIGN_IN_REQUIRED_MESSAGE } from "../../provider/antigravityAuthSupport.ts" ;
3740import * as IdAllocator from "@t3tools/provider-core/server/IdAllocator" ;
3841import * as ProviderAdapter from "@t3tools/provider-core/server/ProviderAdapter" ;
42+ import type * as ProviderContinuationRequests from "@t3tools/provider-core/server/ProviderContinuationRequests" ;
3943import {
4044 makeAntigravityAcpAdapterFlavor ,
4145 makeAntigravityAdapterV2 ,
4246} from "./AntigravityAdapterV2.ts" ;
47+ import { makeTurnInput } from "./AcpAdapterV2.testkit.ts" ;
4348
4449const flavor = makeAntigravityAcpAdapterFlavor ( {
4550 instanceId : ProviderInstanceId . make ( "antigravity-test" ) ,
@@ -1037,3 +1042,251 @@ describe("AntigravityAdapterV2 subagent reads", () => {
10371042 } ) . pipe ( Effect . provide ( layerSession ) , Effect . scoped ) ,
10381043 ) ;
10391044} ) ;
1045+
1046+ describe ( "AntigravityAdapterV2 commands running past the reply" , ( ) => {
1047+ it . effect ( "holds the turn for a running command and offers a continuation for its wake" , ( ) =>
1048+ Effect . gen ( function * ( ) {
1049+ const childProcessSpawner = yield * ChildProcessSpawner . ChildProcessSpawner ;
1050+ const path = yield * Path . Path ;
1051+ const crypto = yield * Crypto . Crypto ;
1052+ const mockAgentPath = yield * path . fromFileUrl (
1053+ new URL ( "../../../scripts/acp-mock-agent.ts" , import . meta. url ) ,
1054+ ) ;
1055+ type RuntimeService = AcpSessionRuntime . AcpSessionRuntime [ "Service" ] ;
1056+ let sessionUpdate : Parameters < RuntimeService [ "handleSessionUpdate" ] > [ 0 ] | undefined ;
1057+ const promptStarted = yield * Deferred . make < void > ( ) ;
1058+ const promptGate = yield * Deferred . make < EffectAcpSchema . PromptResponse > ( ) ;
1059+ const promptHeld = yield * Deferred . make < void > ( ) ;
1060+ const finalizeScheduled = yield * Queue . unbounded < Duration . Input > ( ) ;
1061+ const continuationRequests : Array < ProviderContinuationRequests . ProviderContinuationRequest > =
1062+ [ ] ;
1063+ const instanceId = ProviderInstanceId . make ( "antigravity-running-command-test" ) ;
1064+ const threadId = ThreadId . make ( "thread-antigravity-running-command" ) ;
1065+ const adapter = yield * makeAntigravityAdapterV2 ( {
1066+ instanceId,
1067+ selfInvocation : yield * resolveSelfInvocation ( ) ,
1068+ makeRuntime : ( input ) =>
1069+ makeAntigravityAcpRuntime ( {
1070+ ...input ,
1071+ childProcessSpawner,
1072+ spawn : {
1073+ command : process . execPath ,
1074+ args : [ mockAgentPath ] ,
1075+ cwd : input . cwd ,
1076+ env : { T3_ACP_ANTIGRAVITY : "1" } ,
1077+ } ,
1078+ } ) . pipe (
1079+ Effect . provideService ( Crypto . Crypto , crypto ) ,
1080+ Effect . map ( ( runtime ) : RuntimeService => ( {
1081+ ...runtime ,
1082+ handleSessionUpdate : ( handler ) =>
1083+ Effect . sync ( ( ) => {
1084+ sessionUpdate = handler ;
1085+ } ) . pipe ( Effect . andThen ( runtime . handleSessionUpdate ( handler ) ) ) ,
1086+ prompt : ( ) =>
1087+ Deferred . succeed ( promptStarted , undefined ) . pipe (
1088+ Effect . andThen ( Deferred . await ( promptGate ) ) ,
1089+ ) ,
1090+ } ) ) ,
1091+ ) ,
1092+ withProcess : ( _stop , task ) => task ,
1093+ defaultModel : Effect . undefined ,
1094+ continuationRequests : {
1095+ offer : ( request ) => Effect . sync ( ( ) => void continuationRequests . push ( request ) ) ,
1096+ } ,
1097+ testHooks : {
1098+ afterPromptSettledWithBackgroundWork : ( ) =>
1099+ Deferred . succeed ( promptHeld , undefined ) . pipe ( Effect . asVoid ) ,
1100+ onDeferredFinalizeScheduled : ( debounce ) =>
1101+ Queue . offer ( finalizeScheduled , debounce ) . pipe ( Effect . asVoid ) ,
1102+ } ,
1103+ } ) ;
1104+ const runtimePolicy = ProviderAdapter . ProviderAdapterV2RuntimePolicy . make ( {
1105+ runtimeMode : "full-access" ,
1106+ interactionMode : "default" ,
1107+ cwd : process . cwd ( ) ,
1108+ } ) ;
1109+ const modelSelection = { instanceId, model : "gemini-test-low" } as const ;
1110+ const session = yield * adapter . openSession ( {
1111+ threadId,
1112+ providerSessionId : ProviderSessionId . make ( "provider-session-antigravity-running-command" ) ,
1113+ modelSelection,
1114+ runtimePolicy,
1115+ } ) ;
1116+ const events = yield * Queue . unbounded < ProviderAdapter . ProviderAdapterV2Event > ( ) ;
1117+ yield * session . events . pipe (
1118+ Stream . runForEach ( ( event ) => Queue . offer ( events , event ) ) ,
1119+ Effect . forkScoped ,
1120+ ) ;
1121+ const providerThread = yield * session . ensureThread ( {
1122+ threadId,
1123+ modelSelection,
1124+ runtimePolicy,
1125+ } ) ;
1126+ const now = yield * DateTime . now ;
1127+ const turnInput = ( ordinal : number , continuation : boolean ) =>
1128+ makeTurnInput ( {
1129+ threadId,
1130+ providerThread,
1131+ instanceId,
1132+ runtimePolicy,
1133+ now,
1134+ ordinal,
1135+ modelSelection,
1136+ ...( continuation
1137+ ? { messageCreatedBy : "agent" as const , messageCreationSource : "provider" as const }
1138+ : { } ) ,
1139+ } ) ;
1140+ const seen : Array < ProviderAdapter . ProviderAdapterV2Event > = [ ] ;
1141+ const takeUntil = ( done : ( event : ProviderAdapter . ProviderAdapterV2Event ) => boolean ) =>
1142+ Effect . gen ( function * ( ) {
1143+ while ( true ) {
1144+ const event = yield * Queue . take ( events ) ;
1145+ seen . push ( event ) ;
1146+ if ( done ( event ) ) return event ;
1147+ }
1148+ } ) ;
1149+ const toolStatuses = ( runId : RunId , nativeId : string ) =>
1150+ seen . flatMap ( ( event ) =>
1151+ event . type === "turn_item.updated" &&
1152+ event . turnItem . runId === runId &&
1153+ event . turnItem . nativeItemRef ?. nativeId === nativeId
1154+ ? [ event . turnItem . status ]
1155+ : [ ] ,
1156+ ) ;
1157+ const send = ( update : EffectAcpSchema . SessionUpdate ) =>
1158+ sessionUpdate === undefined
1159+ ? Effect . die ( "the adapter must handle session updates" )
1160+ : sessionUpdate (
1161+ normalizeAntigravitySessionUpdate ( { sessionId : "mock-session-1" , update } ) ,
1162+ ) ;
1163+ const say = ( text : string ) =>
1164+ send ( { sessionUpdate : "agent_message_chunk" , content : { type : "text" , text } } ) ;
1165+
1166+ const first = turnInput ( 1 , false ) ;
1167+ yield * session . startTurn ( first ) . pipe ( Effect . forkScoped ) ;
1168+ yield * Deferred . await ( promptStarted ) ;
1169+ // A command that finishes inside the prompt is ordinary foreground work.
1170+ yield * send ( {
1171+ sessionUpdate : "tool_call" ,
1172+ toolCallId : "list" ,
1173+ title : "Terminal" ,
1174+ kind : "execute" ,
1175+ status : "in_progress" ,
1176+ rawInput : { CommandLine : "ls" } ,
1177+ } ) ;
1178+ yield * send ( { sessionUpdate : "tool_call_update" , toolCallId : "list" , status : "completed" } ) ;
1179+ yield * send ( {
1180+ sessionUpdate : "tool_call" ,
1181+ toolCallId : "build" ,
1182+ title : "Terminal" ,
1183+ kind : "execute" ,
1184+ status : "in_progress" ,
1185+ rawInput : { CommandLine : "npm run build" } ,
1186+ } ) ;
1187+ // Neither a command awaiting approval nor a subagent batch may hold the turn.
1188+ yield * send ( {
1189+ sessionUpdate : "tool_call" ,
1190+ toolCallId : "approval" ,
1191+ title : "Terminal" ,
1192+ kind : "execute" ,
1193+ status : "pending" ,
1194+ rawInput : { CommandLine : "npm run dev" } ,
1195+ } ) ;
1196+ yield * send ( {
1197+ sessionUpdate : "tool_call" ,
1198+ toolCallId : "trajectory:4" ,
1199+ title : "Running start_subagent" ,
1200+ kind : "other" ,
1201+ status : "completed" ,
1202+ rawOutput : "Started two agents." ,
1203+ } ) ;
1204+ yield * say ( "Waiting for the build to finish." ) ;
1205+ yield * Deferred . succeed ( promptGate , { stopReason : "end_turn" } ) ;
1206+ const held = yield * Deferred . await ( promptHeld ) . pipe (
1207+ Effect . as ( true ) ,
1208+ Effect . raceFirst (
1209+ takeUntil ( ( event ) => event . type === "turn.terminal" ) . pipe ( Effect . as ( false ) ) ,
1210+ ) ,
1211+ ) ;
1212+ assert . isTrue ( held , "the running command holds the turn past end_turn" ) ;
1213+
1214+ // Antigravity reports the command's end after the prompt returned, without its kind.
1215+ yield * send ( { sessionUpdate : "tool_call_update" , toolCallId : "build" , status : "completed" } ) ;
1216+ yield * takeUntil (
1217+ ( event ) =>
1218+ event . type === "turn_item.updated" &&
1219+ event . turnItem . nativeItemRef ?. nativeId === "build" &&
1220+ event . turnItem . status === "completed" ,
1221+ ) ;
1222+ assert . isFalse ( seen . some ( ( event ) => event . type === "turn.terminal" ) ) ;
1223+ yield * TestClock . adjust ( yield * Queue . take ( finalizeScheduled ) ) ;
1224+ const firstTerminal = yield * takeUntil ( ( event ) => event . type === "turn.terminal" ) ;
1225+ assert . equal (
1226+ firstTerminal . type === "turn.terminal" ? firstTerminal . status : null ,
1227+ "completed" ,
1228+ ) ;
1229+ assert . deepEqual ( toolStatuses ( first . runId , "build" ) . slice ( - 1 ) , [ "completed" ] ) ;
1230+ assert . deepEqual ( toolStatuses ( first . runId , "approval" ) . slice ( - 1 ) , [ "completed" ] ) ;
1231+ const batch = seen . findLast ( ( event ) => event . type === "subagent.updated" ) ;
1232+ assert . equal ( batch ?. type === "subagent.updated" ? batch . subagent . status : null , "idle" ) ;
1233+ assert . lengthOf ( continuationRequests , 0 ) ;
1234+ yield * Queue . clear ( finalizeScheduled ) ;
1235+
1236+ // The command's completion wakes the agent, which starts another command before speaking.
1237+ yield * send ( {
1238+ sessionUpdate : "tool_call" ,
1239+ toolCallId : "test" ,
1240+ title : "Terminal" ,
1241+ kind : "execute" ,
1242+ status : "in_progress" ,
1243+ rawInput : { CommandLine : "npm test" } ,
1244+ } ) ;
1245+ yield * say ( "The build passed. Running the tests." ) ;
1246+ assert . lengthOf ( continuationRequests , 1 , "the reply opens a continuation while tests run" ) ;
1247+ assert . equal ( continuationRequests [ 0 ] ?. threadId , threadId ) ;
1248+ yield * send ( {
1249+ sessionUpdate : "tool_call_update" ,
1250+ toolCallId : "test" ,
1251+ status : "in_progress" ,
1252+ content : [ { type : "content" , content : { type : "text" , text : "3 of 6 suites passed" } } ] ,
1253+ } ) ;
1254+ yield * say ( "Halfway through the tests." ) ;
1255+
1256+ const continuation = turnInput ( 2 , true ) ;
1257+ yield * session . startTurn ( continuation ) . pipe ( Effect . forkScoped ) ;
1258+ yield * takeUntil (
1259+ ( event ) =>
1260+ event . type === "message.updated" &&
1261+ event . message . runId === continuation . runId &&
1262+ event . message . text . includes ( "Halfway through the tests." ) ,
1263+ ) ;
1264+ assert . equal ( yield * Queue . size ( finalizeScheduled ) , 0 , "the running command holds the turn" ) ;
1265+ yield * send ( { sessionUpdate : "tool_call_update" , toolCallId : "test" , status : "completed" } ) ;
1266+ yield * say ( "All tests passed." ) ;
1267+ yield * TestClock . adjust ( yield * Queue . take ( finalizeScheduled ) ) ;
1268+ const continuationTerminal = yield * takeUntil (
1269+ ( event ) => event . type === "turn.terminal" && event . runOrdinal === continuation . runOrdinal ,
1270+ ) ;
1271+ assert . equal (
1272+ continuationTerminal . type === "turn.terminal" ? continuationTerminal . status : null ,
1273+ "completed" ,
1274+ ) ;
1275+ const continuationText = seen . flatMap ( ( event ) =>
1276+ event . type === "message.updated" &&
1277+ event . message . role === "assistant" &&
1278+ event . message . runId === continuation . runId
1279+ ? [ event . message . text ]
1280+ : [ ] ,
1281+ ) ;
1282+ assert . isTrue (
1283+ [ "The build passed." , "Halfway through the tests." , "All tests passed." ] . every ( ( reply ) =>
1284+ continuationText . some ( ( text ) => text . includes ( reply ) ) ,
1285+ ) ,
1286+ "the wake's replies stream into the continuation" ,
1287+ ) ;
1288+ assert . deepEqual ( toolStatuses ( continuation . runId , "test" ) . slice ( - 1 ) , [ "completed" ] ) ;
1289+ assert . lengthOf ( continuationRequests , 1 ) ;
1290+ } ) . pipe ( Effect . provide ( layerSession ) , Effect . scoped ) ,
1291+ ) ;
1292+ } ) ;
0 commit comments