The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import { describe, expect, it } from "vite-plus/test"; |
| 2 | import * as Schema from "effect/Schema"; |
| 3 | import type * as EffectAcpSchema from "effect-acp/compat"; |
| 4 | import * as AcpWireSchema from "effect-acp/schema-v1"; |
| 5 | |
| 6 | import { |
| 7 | extractAntigravityUserInputQuestion, |
| 8 | antigravityApprovalOptions, |
| 9 | antigravitySubagentOutput, |
| 10 | antigravityToolOutlivesPrompt, |
| 11 | classifyAntigravitySubagentToolCall, |
| 12 | isAntigravityUserInputRequest, |
| 13 | makeAntigravityUserInputResponse, |
| 14 | normalizeAntigravitySessionUpdate, |
| 15 | normalizeAntigravityToolCall, |
| 16 | sanitizeAntigravityToolPayload, |
| 17 | makeAntigravityReadRepair, |
| 18 | } from "./AntigravityProtocol.ts"; |
| 19 | import { |
| 20 | mergeToolCallState, |
| 21 | parseSessionUpdateEvent, |
| 22 | } from "@t3tools/provider-acp/server/runtimeModel"; |
| 23 | import { acpToolCallDiffPatch } from "@t3tools/provider-acp/server/adapter"; |
| 24 | |
| 25 | const isSessionNotification = Schema.is(AcpWireSchema.SessionNotification); |
| 26 | |
| 27 | describe("native Antigravity subagent tools", () => { |
| 28 | it("recognizes only native invocation titles and excludes MCP tools", () => { |
| 29 | const toolCall = { toolCallId: "trajectory:4", kind: "other", data: {} }; |
| 30 | for (const title of [ |
| 31 | "Running start_subagent", |
| 32 | "Run start_subagent?", |
| 33 | "Running invoke_subagent", |
| 34 | "Run invoke_subagent?", |
| 35 | ]) { |
| 36 | expect(classifyAntigravitySubagentToolCall({ ...toolCall, title }, {})).toBe("subagent"); |
| 37 | expect( |
| 38 | classifyAntigravitySubagentToolCall( |
| 39 | { ...toolCall, title }, |
| 40 | { update: { _meta: { is_mcp_tool_call: true } } }, |
| 41 | ), |
| 42 | ).toBe("mcp"); |
| 43 | } |
| 44 | for (const title of [ |
| 45 | "Running subagent", |
| 46 | "start_subagent", |
| 47 | "Run command", |
| 48 | "Running manage_task", |
| 49 | ]) { |
| 50 | expect(classifyAntigravitySubagentToolCall({ ...toolCall, title }, {})).toBeUndefined(); |
| 51 | } |
| 52 | expect( |
| 53 | classifyAntigravitySubagentToolCall( |
| 54 | { ...toolCall, title: "Running start_subagent", kind: "execute" }, |
| 55 | {}, |
| 56 | ), |
| 57 | ).toBeUndefined(); |
| 58 | }); |
| 59 | |
| 60 | it("recognizes history starts and bounds the launch output", () => { |
| 61 | expect( |
| 62 | antigravitySubagentOutput({ |
| 63 | toolCallId: "trajectory:4", |
| 64 | data: { rawOutput: " Finished review. " }, |
| 65 | }), |
| 66 | ).toBe("Finished review."); |
| 67 | const result = antigravitySubagentOutput({ |
| 68 | toolCallId: "trajectory:4", |
| 69 | data: { rawOutput: `${"x".repeat(16_000)}The result.` }, |
| 70 | }); |
| 71 | expect(result?.length).toBeLessThan(8_100); |
| 72 | expect(result?.endsWith("The result.")).toBe(true); |
| 73 | expect( |
| 74 | antigravitySubagentOutput({ toolCallId: "trajectory:4", data: { rawOutput: {} } }), |
| 75 | ).toBeUndefined(); |
| 76 | }); |
| 77 | }); |
| 78 | |
| 79 | const questionRequest = { |
| 80 | sessionId: "session-1", |
| 81 | toolCall: { |
| 82 | toolCallId: "interaction_9960062f", |
| 83 | status: "pending", |
| 84 | title: "Which result label should be used for the verification?", |
| 85 | rawInput: {}, |
| 86 | }, |
| 87 | options: [ |
| 88 | { optionId: "1", name: "Verified", kind: "allow_once" }, |
| 89 | { optionId: "2", name: "Needs review", kind: "allow_once" }, |
| 90 | ], |
| 91 | } satisfies EffectAcpSchema.RequestPermissionRequest; |
| 92 | |
| 93 | const commandStarted = { |
| 94 | sessionId: "session-1", |
| 95 | update: { |
| 96 | sessionUpdate: "tool_call", |
| 97 | toolCallId: "fc28d0af6ad14be8bbba20f4258d4d3e", |
| 98 | title: "run_command", |
| 99 | kind: "execute", |
| 100 | status: "in_progress", |
| 101 | rawInput: { CommandLine: "cat probe.txt", Cwd: "/workspace" }, |
| 102 | }, |
| 103 | } satisfies EffectAcpSchema.SessionNotification; |
| 104 | |
| 105 | const commandCompleted = { |
| 106 | sessionId: "session-1", |
| 107 | update: { |
| 108 | toolCallId: commandStarted.update.toolCallId, |
| 109 | status: "completed", |
| 110 | rawOutput: { |
| 111 | commandLine: "cat probe.txt", |
| 112 | workingDir: "/workspace", |
| 113 | exitCode: 0, |
| 114 | exit_code: 0, |
| 115 | combinedOutput: "after\n", |
| 116 | formatted_output: "after\n", |
| 117 | }, |
| 118 | sessionUpdate: "tool_call_update", |
| 119 | }, |
| 120 | } satisfies EffectAcpSchema.SessionNotification; |
| 121 | |
| 122 | function parseToolUpdate(notification: EffectAcpSchema.SessionNotification) { |
| 123 | const result = parseSessionUpdateEvent(normalizeAntigravitySessionUpdate(notification)); |
| 124 | const event = result.events[0]; |
| 125 | if (event?._tag !== "ToolCallUpdated") { |
| 126 | throw new Error("Expected a tool update."); |
| 127 | } |
| 128 | return event; |
| 129 | } |
| 130 | |
| 131 | describe("Antigravity permissions and questions", () => { |
| 132 | const permissionRequest = { |
| 133 | sessionId: "session-1", |
| 134 | toolCall: { toolCallId: "command-1", kind: "execute", title: "Run command" }, |
| 135 | options: [ |
| 136 | { optionId: "remember-this-command", name: "Allow always", kind: "allow_always" }, |
| 137 | { optionId: "run-this-time", name: "Allow", kind: "allow_once" }, |
| 138 | { optionId: "stop-this-command", name: "Deny", kind: "reject_once" }, |
| 139 | ], |
| 140 | } satisfies EffectAcpSchema.RequestPermissionRequest; |
| 141 | |
| 142 | it("surfaces the agent's prompt injection warning on the remembered approval", () => { |
| 143 | const risky = { |
| 144 | ...permissionRequest, |
| 145 | options: [ |
| 146 | { |
| 147 | optionId: "remember-this-command", |
| 148 | name: "Allow Always (risky)", |
| 149 | kind: "allow_always", |
| 150 | _meta: { |
| 151 | "agy.security.warning": { |
| 152 | severity: "high", |
| 153 | risk: "prompt_injection", |
| 154 | title: "Allowing always can be risky", |
| 155 | message: "Untrusted files could re-run this action without asking.", |
| 156 | }, |
| 157 | }, |
| 158 | }, |
| 159 | { optionId: "run-this-time", name: "Allow", kind: "allow_once" }, |
| 160 | { optionId: "stop-this-command", name: "Deny", kind: "reject_once" }, |
| 161 | ], |
| 162 | } satisfies EffectAcpSchema.RequestPermissionRequest; |
| 163 | expect(antigravityApprovalOptions(risky)).toEqual([ |
| 164 | { decision: "accept", label: "Allow once" }, |
| 165 | { |
| 166 | decision: "acceptForSession", |
| 167 | label: "Allow for this thread", |
| 168 | warning: "Untrusted files could re-run this action without asking.", |
| 169 | }, |
| 170 | { decision: "decline", label: "Deny" }, |
| 171 | { decision: "cancel", label: "Cancel" }, |
| 172 | ]); |
| 173 | expect(antigravityApprovalOptions(permissionRequest)[1]).not.toHaveProperty("warning"); |
| 174 | }); |
| 175 | |
| 176 | it("routes the captured native question to single-choice input, not approval", () => { |
| 177 | expect(isAntigravityUserInputRequest(questionRequest)).toBe(true); |
| 178 | expect(extractAntigravityUserInputQuestion(questionRequest)).toEqual({ |
| 179 | id: "interaction_9960062f", |
| 180 | header: "Question", |
| 181 | question: "Which result label should be used for the verification?", |
| 182 | multiSelect: false, |
| 183 | allowCustomAnswer: false, |
| 184 | options: [ |
| 185 | { value: "1", label: "Verified", description: "Verified" }, |
| 186 | { value: "2", label: "Needs review", description: "Needs review" }, |
| 187 | ], |
| 188 | }); |
| 189 | expect(extractAntigravityUserInputQuestion(permissionRequest)).toBeUndefined(); |
| 190 | }); |
| 191 | |
| 192 | it("returns exact opaque IDs and accepts a unique label from an older client", () => { |
| 193 | for (const answer of ["1", ["1"], "Verified"]) { |
| 194 | expect( |
| 195 | makeAntigravityUserInputResponse(questionRequest, { interaction_9960062f: answer }), |
| 196 | ).toEqual({ outcome: { outcome: "selected", optionId: "1" } }); |
| 197 | } |
| 198 | const request = { |
| 199 | ...questionRequest, |
| 200 | options: [{ optionId: " choice: opaque ", name: "Keep", kind: "allow_once" as const }], |
| 201 | }; |
| 202 | expect( |
| 203 | makeAntigravityUserInputResponse(request, { interaction_9960062f: " choice: opaque " }), |
| 204 | ).toEqual({ outcome: { outcome: "selected", optionId: " choice: opaque " } }); |
| 205 | }); |
| 206 | |
| 207 | it("does not treat a question's reject choice as cancellation", () => { |
| 208 | const request = { |
| 209 | ...questionRequest, |
| 210 | options: [{ optionId: "deny", name: "Do not trust", kind: "reject_once" as const }], |
| 211 | }; |
| 212 | expect(makeAntigravityUserInputResponse(request, { interaction_9960062f: "deny" })).toEqual({ |
| 213 | outcome: { outcome: "selected", optionId: "deny" }, |
| 214 | }); |
| 215 | }); |
| 216 | |
| 217 | it("preserves duplicate labels and rejects ambiguous label answers", () => { |
| 218 | const request = { |
| 219 | ...questionRequest, |
| 220 | options: questionRequest.options.map((option) => ({ ...option, name: "Same label" })), |
| 221 | }; |
| 222 | expect( |
| 223 | extractAntigravityUserInputQuestion(request)?.options.map((option) => option.value), |
| 224 | ).toEqual(["1", "2"]); |
| 225 | expect( |
| 226 | makeAntigravityUserInputResponse(request, { interaction_9960062f: "Same label" }), |
| 227 | ).toBeUndefined(); |
| 228 | expect(makeAntigravityUserInputResponse(request, { interaction_9960062f: "2" })).toEqual({ |
| 229 | outcome: { outcome: "selected", optionId: "2" }, |
| 230 | }); |
| 231 | }); |
| 232 | |
| 233 | it.each([undefined, null, "", "arbitrary answer", [], ["1", "2"], { answer: "1" }, 1])( |
| 234 | "keeps the question open for an unsupported answer: %j", |
| 235 | (answer) => { |
| 236 | expect( |
| 237 | makeAntigravityUserInputResponse(questionRequest, { interaction_9960062f: answer }), |
| 238 | ).toBeUndefined(); |
| 239 | }, |
| 240 | ); |
| 241 | |
| 242 | it("rejects missing or duplicate native option IDs", () => { |
| 243 | for (const optionId of ["", "1"]) { |
| 244 | const request = { |
| 245 | ...questionRequest, |
| 246 | options: [questionRequest.options[0]!, { ...questionRequest.options[1]!, optionId }], |
| 247 | }; |
| 248 | expect(extractAntigravityUserInputQuestion(request)).toBeUndefined(); |
| 249 | expect( |
| 250 | makeAntigravityUserInputResponse(request, { interaction_9960062f: "1" }), |
| 251 | ).toBeUndefined(); |
| 252 | } |
| 253 | }); |
| 254 | |
| 255 | it("bounds question text without changing choice IDs", () => { |
| 256 | const request = { |
| 257 | ...questionRequest, |
| 258 | toolCall: { ...questionRequest.toolCall, title: "Question ".repeat(2_000) }, |
| 259 | options: [ |
| 260 | { optionId: "stable-choice", name: "Choice ".repeat(2_000), kind: "allow_once" as const }, |
| 261 | ], |
| 262 | }; |
| 263 | const question = extractAntigravityUserInputQuestion(request); |
| 264 | expect(question?.question.length).toBeLessThanOrEqual(8_000); |
| 265 | expect(question?.options[0]?.label.length).toBeLessThanOrEqual(512); |
| 266 | expect(question?.options[0]?.value).toBe("stable-choice"); |
| 267 | }); |
| 268 | }); |
| 269 | |
| 270 | describe("Antigravity tool results", () => { |
| 271 | it("normalizes the captured command and completed output for the existing clients", () => { |
| 272 | const initial = parseToolUpdate(commandStarted).toolCall; |
| 273 | const completed = parseToolUpdate(commandCompleted).toolCall; |
| 274 | const toolCall = normalizeAntigravityToolCall(mergeToolCallState(initial, completed)); |
| 275 | |
| 276 | expect(toolCall).toMatchObject({ |
| 277 | kind: "execute", |
| 278 | status: "completed", |
| 279 | command: "cat probe.txt", |
| 280 | detail: "cat probe.txt", |
| 281 | data: { |
| 282 | command: "cat probe.txt", |
| 283 | cwd: "/workspace", |
| 284 | item: { |
| 285 | command: "cat probe.txt", |
| 286 | cwd: "/workspace", |
| 287 | aggregatedOutput: "after\n", |
| 288 | exitCode: 0, |
| 289 | }, |
| 290 | }, |
| 291 | }); |
| 292 | expect(toolCall.data.rawOutput).not.toHaveProperty("formatted_output"); |
| 293 | expect(commandCompleted.update.rawOutput.formatted_output).toBe("after\n"); |
| 294 | }); |
| 295 | |
| 296 | it("keeps the captured edit location through approval and completion", () => { |
| 297 | const path = "/workspace/app.ts"; |
| 298 | const replace = { |
| 299 | TargetContent: "const target = 10;", |
| 300 | ReplacementContent: "const target = 100;", |
| 301 | TargetFile: path, |
| 302 | }; |
| 303 | const frame = (rawInput: Record<string, unknown>, line?: number) => |
| 304 | ({ |
| 305 | sessionId: "session-1", |
| 306 | update: { |
| 307 | sessionUpdate: "tool_call", |
| 308 | toolCallId: "edit-1", |
| 309 | title: "Run edit_file?", |
| 310 | kind: "edit", |
| 311 | status: "pending", |
| 312 | content: [ |
| 313 | { |
| 314 | type: "diff", |
| 315 | path, |
| 316 | oldText: replace.TargetContent, |
| 317 | newText: replace.ReplacementContent, |
| 318 | }, |
| 319 | ], |
| 320 | locations: [line === undefined ? { path } : { path, line }], |
| 321 | rawInput, |
| 322 | }, |
| 323 | }) satisfies EffectAcpSchema.SessionNotification; |
| 324 | const running = parseToolUpdate({ |
| 325 | sessionId: "session-1", |
| 326 | update: { |
| 327 | sessionUpdate: "tool_call_update", |
| 328 | toolCallId: "edit-1", |
| 329 | kind: "edit", |
| 330 | status: "in_progress", |
| 331 | title: "Running edit_file", |
| 332 | locations: [{ path }], |
| 333 | rawInput: { file_path: path }, |
| 334 | }, |
| 335 | }).toolCall; |
| 336 | const completed = parseToolUpdate({ |
| 337 | sessionId: "session-1", |
| 338 | update: { |
| 339 | sessionUpdate: "tool_call_update", |
| 340 | toolCallId: "edit-1", |
| 341 | status: "completed", |
| 342 | rawOutput: "Tool execution", |
| 343 | }, |
| 344 | }).toolCall; |
| 345 | const preEdit = Array.from({ length: 12 }, (_, i) => |
| 346 | i === 9 ? replace.TargetContent : `line ${i + 1}`, |
| 347 | ).join("\n"); |
| 348 | const postEdit = preEdit.replace(replace.TargetContent, replace.ReplacementContent); |
| 349 | for (const [pending, readFile] of [ |
| 350 | [frame({ ...replace, StartLine: 5, EndLine: 12 }, 5), () => preEdit], |
| 351 | [frame(replace), () => preEdit], |
| 352 | ] as const) { |
| 353 | let toolCall = normalizeAntigravityToolCall(parseToolUpdate(pending).toolCall, readFile); |
| 354 | for (const next of [running, completed]) { |
| 355 | toolCall = normalizeAntigravityToolCall(mergeToolCallState(toolCall, next), () => postEdit); |
| 356 | } |
| 357 | expect(toolCall.status).toBe("completed"); |
| 358 | expect(acpToolCallDiffPatch(toolCall.data.content)).toBe( |
| 359 | `--- ${path}\n+++ ${path}\n@@ -10,1 +10,1 @@\n-const target = 10;\n+const target = 100;\n`, |
| 360 | ); |
| 361 | } |
| 362 | // Full-access edits arrive path-only; no hunk is offered instead of the tool's status text. |
| 363 | let pathOnly = normalizeAntigravityToolCall( |
| 364 | parseToolUpdate({ |
| 365 | sessionId: "session-1", |
| 366 | update: { |
| 367 | sessionUpdate: "tool_call", |
| 368 | toolCallId: "edit-2", |
| 369 | kind: "edit", |
| 370 | status: "in_progress", |
| 371 | title: "Running edit_file", |
| 372 | locations: [{ path }], |
| 373 | rawInput: { file_path: path }, |
| 374 | }, |
| 375 | }).toolCall, |
| 376 | ); |
| 377 | pathOnly = normalizeAntigravityToolCall(mergeToolCallState(pathOnly, completed)); |
| 378 | expect(pathOnly.data.content).toEqual([{ type: "diff", patch: { format: "omitted" } }]); |
| 379 | expect(acpToolCallDiffPatch(pathOnly.data.content)).toBeUndefined(); |
| 380 | // A captured patch that a later update's shared text budget cut or dropped is omitted. |
| 381 | for (const patch of [{ text: "[Earlier output truncated]\n\n@@ -80,1 +80,1 @@" }, {}]) { |
| 382 | const kept = normalizeAntigravityToolCall({ |
| 383 | toolCallId: "edit-cut", |
| 384 | kind: "edit", |
| 385 | status: "completed", |
| 386 | data: { content: [{ type: "diff", path, patch }] }, |
| 387 | }); |
| 388 | expect(kept.data.content).toEqual([{ type: "diff", path, patch: { format: "omitted" } }]); |
| 389 | } |
| 390 | // A whole-file write whose old text was cut to its tail must not show the cut as deletions. |
| 391 | const overwrite = normalizeAntigravityToolCall({ |
| 392 | toolCallId: "edit-overwrite", |
| 393 | kind: "edit", |
| 394 | status: "completed", |
| 395 | data: { |
| 396 | rawInput: { TargetFile: path, CodeContent: "one line\n" }, |
| 397 | content: [ |
| 398 | { |
| 399 | type: "diff", |
| 400 | path, |
| 401 | oldText: "[Earlier output truncated]\n\nrow 999\n", |
| 402 | newText: "one line\n", |
| 403 | }, |
| 404 | ], |
| 405 | }, |
| 406 | }); |
| 407 | expect(acpToolCallDiffPatch(overwrite.data.content)).toBeUndefined(); |
| 408 | // A patch too long to keep, or input cut to its tail, must not fall back to line-1 snippets. |
| 409 | for (const width of [60, 120]) { |
| 410 | const block = (value: string) => |
| 411 | Array.from({ length: 120 }, (_, i) => `${value} ${i} `.padEnd(width, "x")).join("\n"); |
| 412 | const long = { |
| 413 | TargetContent: block("old"), |
| 414 | ReplacementContent: block("new"), |
| 415 | TargetFile: path, |
| 416 | }; |
| 417 | const toolCall = normalizeAntigravityToolCall( |
| 418 | parseToolUpdate(frame({ ...long, StartLine: 80, EndLine: 199 }, 80)).toolCall, |
| 419 | ); |
| 420 | expect(toolCall.data.content).toMatchObject([{ patch: { format: "omitted" } }]); |
| 421 | expect(acpToolCallDiffPatch(toolCall.data.content)).toBeUndefined(); |
| 422 | } |
| 423 | }); |
| 424 | |
| 425 | it.each([ |
| 426 | { CommandLine: "pwd", Cwd: "/one" }, |
| 427 | { CommandLine: "pwd", WorkingDirectory: "/one" }, |
| 428 | { command_line: "pwd", working_dir: "/one" }, |
| 429 | { commandLine: "pwd", workingDir: "/one" }, |
| 430 | { command: "pwd", cwd: "/one" }, |
| 431 | ])("handles native command input aliases: %j", (rawInput) => { |
| 432 | const event = parseToolUpdate({ |
| 433 | ...commandStarted, |
| 434 | update: { ...commandStarted.update, rawInput }, |
| 435 | }); |
| 436 | expect(normalizeAntigravityToolCall(event.toolCall)).toMatchObject({ |
| 437 | command: "pwd", |
| 438 | detail: "pwd", |
| 439 | data: { item: { command: "pwd", cwd: "/one" } }, |
| 440 | }); |
| 441 | }); |
| 442 | |
| 443 | it("recovers command fields from history and keeps nonzero exits separate from tool failure", () => { |
| 444 | const event = parseToolUpdate({ |
| 445 | ...commandCompleted, |
| 446 | update: { |
| 447 | ...commandCompleted.update, |
| 448 | rawOutput: { |
| 449 | command_line: "test -f missing.txt", |
| 450 | working_dir: "/workspace", |
| 451 | combined_output: "", |
| 452 | exit_code: 1, |
| 453 | }, |
| 454 | }, |
| 455 | }); |
| 456 | expect(normalizeAntigravityToolCall(event.toolCall)).toMatchObject({ |
| 457 | kind: "execute", |
| 458 | status: "completed", |
| 459 | command: "test -f missing.txt", |
| 460 | data: { item: { cwd: "/workspace", aggregatedOutput: "", exitCode: 1 } }, |
| 461 | }); |
| 462 | }); |
| 463 | |
| 464 | it.each([ |
| 465 | { name: "ASCII", boundary: "x", kept: "x", field: "combinedOutput" }, |
| 466 | { name: "split emoji", boundary: "🚀", kept: "", field: "combinedOutput" }, |
| 467 | { name: "split emoji (snake case)", boundary: "🚀", kept: "", field: "combined_output" }, |
| 468 | ])("bounds both canonical and retained raw output at $name", ({ boundary, kept, field }) => { |
| 469 | const tail = `${"x".repeat(7_989)}last line\n`; |
| 470 | const output = `${"output line\n".repeat(20_000)}${boundary}${tail}`; |
| 471 | const expected = `[Earlier output truncated]\n\n${kept}${tail}`; |
| 472 | const { combinedOutput: _combinedOutput, ...rawOutput } = commandCompleted.update.rawOutput; |
| 473 | const event = parseToolUpdate({ |
| 474 | ...commandCompleted, |
| 475 | update: { |
| 476 | ...commandCompleted.update, |
| 477 | rawOutput: { |
| 478 | ...rawOutput, |
| 479 | [field]: output, |
| 480 | formatted_output: output, |
| 481 | }, |
| 482 | }, |
| 483 | }); |
| 484 | const toolCall = normalizeAntigravityToolCall(event.toolCall); |
| 485 | expect(toolCall.data).toMatchObject({ |
| 486 | item: { aggregatedOutput: expected }, |
| 487 | rawOutput: { [field]: expected }, |
| 488 | }); |
| 489 | expect(event.toolCall.data.rawOutput).toMatchObject({ [field]: expected }); |
| 490 | expect(JSON.stringify(toolCall).length).toBeLessThan(20_000); |
| 491 | expect(JSON.stringify(event.rawPayload).length).toBeLessThan(10_000); |
| 492 | expect(JSON.stringify(event.rawPayload)).not.toContain("formatted_output"); |
| 493 | }); |
| 494 | |
| 495 | it.each([ |
| 496 | { name: "a command", update: commandStarted.update, outlives: true }, |
| 497 | { |
| 498 | name: "a daemon", |
| 499 | update: { ...commandStarted.update, rawInput: { CommandLine: "serve", IsDaemon: true } }, |
| 500 | outlives: false, |
| 501 | }, |
| 502 | // agy 1.5.13 keeps this call in progress until the wake fires. |
| 503 | { |
| 504 | name: "a scheduled wake", |
| 505 | update: { |
| 506 | sessionUpdate: "tool_call", |
| 507 | toolCallId: "call_4902666363527503248", |
| 508 | status: "in_progress", |
| 509 | kind: "other", |
| 510 | title: "Run schedule", |
| 511 | rawInput: { DurationSeconds: 30, Prompt: "Reply only with the word DONE." }, |
| 512 | }, |
| 513 | outlives: true, |
| 514 | }, |
| 515 | { |
| 516 | name: "another native tool", |
| 517 | update: { |
| 518 | sessionUpdate: "tool_call", |
| 519 | toolCallId: "call_other", |
| 520 | status: "in_progress", |
| 521 | kind: "other", |
| 522 | title: "Run list_dir", |
| 523 | rawInput: { DirectoryPath: "/workspace" }, |
| 524 | }, |
| 525 | outlives: false, |
| 526 | }, |
| 527 | ] satisfies ReadonlyArray<{ |
| 528 | name: string; |
| 529 | update: EffectAcpSchema.SessionNotification["update"]; |
| 530 | outlives: boolean; |
| 531 | }>)("knows whether $name outlives the prompt", ({ update, outlives }) => { |
| 532 | const event = parseToolUpdate({ sessionId: "session-1", update }); |
| 533 | expect(antigravityToolOutlivesPrompt(normalizeAntigravityToolCall(event.toolCall))).toBe( |
| 534 | outlives, |
| 535 | ); |
| 536 | }); |
| 537 | |
| 538 | it("keeps 1.5 command input and output valid Unicode when cut", () => { |
| 539 | const tail = `${"x".repeat(7_982)}build completed\r\n`; |
| 540 | const output = `Command output:\nprefix\r\n🚀${tail}`; |
| 541 | const expected = `[Earlier output truncated]\n\n${tail}`; |
| 542 | const toolCall = normalizeAntigravityToolCall( |
| 543 | parseToolUpdate({ |
| 544 | ...commandCompleted, |
| 545 | update: { |
| 546 | ...commandCompleted.update, |
| 547 | rawInput: { CommandLine: output }, |
| 548 | rawOutput: { output, exit_code: 0 }, |
| 549 | content: [{ type: "content", content: { type: "text", text: output } }], |
| 550 | }, |
| 551 | }).toolCall, |
| 552 | ); |
| 553 | expect(toolCall.command).toBe(expected.trimEnd()); |
| 554 | expect(toolCall.data).toMatchObject({ |
| 555 | rawInput: { CommandLine: expected }, |
| 556 | rawOutput: { output: expected }, |
| 557 | content: [{ content: { text: expected.trimEnd() } }], |
| 558 | }); |
| 559 | }); |
| 560 | |
| 561 | it("removes inline images while preserving valid tool content and the local image path", () => { |
| 562 | const inlineImage = "inline-image-bytes".repeat(50_000); |
| 563 | const notification = { |
| 564 | sessionId: "session-1", |
| 565 | update: { |
| 566 | sessionUpdate: "tool_call_update", |
| 567 | toolCallId: "image-1", |
| 568 | status: "completed", |
| 569 | rawOutput: { imageName: "result", imagePath: "file:///workspace/brain/result%20image.png" }, |
| 570 | content: [ |
| 571 | { type: "content", content: { type: "image", mimeType: "image/png", data: inlineImage } }, |
| 572 | { type: "content", content: { type: "text", text: "Saved the image." } }, |
| 573 | { type: "diff", path: "/workspace/note.txt", oldText: "old", newText: "new" }, |
| 574 | ], |
| 575 | }, |
| 576 | } satisfies EffectAcpSchema.SessionNotification; |
| 577 | const normalized = normalizeAntigravitySessionUpdate(notification); |
| 578 | expect(isSessionNotification(normalized)).toBe(true); |
| 579 | expect(JSON.stringify(normalized)).not.toContain("inline-image-bytes"); |
| 580 | expect(normalized.update).toMatchObject({ |
| 581 | content: [ |
| 582 | { type: "content", content: { type: "text", text: "Saved the image." } }, |
| 583 | { type: "diff", path: "/workspace/note.txt", oldText: "old", newText: "new" }, |
| 584 | ], |
| 585 | }); |
| 586 | expect(normalizeAntigravityToolCall(parseToolUpdate(normalized).toolCall).data.imagePath).toBe( |
| 587 | "/workspace/brain/result image.png", |
| 588 | ); |
| 589 | }); |
| 590 | |
| 591 | it.each([ |
| 592 | ["/workspace/result.png", "/workspace/result.png"], |
| 593 | ["C:\\work\\result.png", "C:\\work\\result.png"], |
| 594 | ["file:///C:/work/result.png", "C:/work/result.png"], |
| 595 | ["https://example.com/result.png", undefined], |
| 596 | ["file://another-host/result.png", undefined], |
| 597 | ["data:image/png;base64,AAAA", undefined], |
| 598 | ])("only promotes local image references: %s", (imagePath, expected) => { |
| 599 | const toolCall = normalizeAntigravityToolCall({ |
| 600 | toolCallId: "image-1", |
| 601 | data: { rawOutput: { imagePath } }, |
| 602 | }); |
| 603 | expect(toolCall.data.imagePath).toBe(expected); |
| 604 | }); |
| 605 | |
| 606 | it("bounds nested tool data and drops image blobs and data URLs", () => { |
| 607 | const payload = sanitizeAntigravityToolPayload({ |
| 608 | rawOutput: { |
| 609 | text: "a".repeat(100_000), |
| 610 | result: { mimeType: "image/png", blob: "image-blob".repeat(100_000) }, |
| 611 | image: { type: "image", data: "inline-image".repeat(100_000) }, |
| 612 | uri: "data:image/png;base64,inline-data-url", |
| 613 | }, |
| 614 | }); |
| 615 | const serialized = JSON.stringify(payload); |
| 616 | expect(serialized.length).toBeLessThan(9_000); |
| 617 | expect(serialized).not.toContain("image-blob"); |
| 618 | expect(serialized).not.toContain("inline-image"); |
| 619 | expect(serialized).not.toContain("inline-data-url"); |
| 620 | |
| 621 | // Leave three UTF-16 units for the retained output. |
| 622 | const padding = Object.fromEntries( |
| 623 | Array.from({ length: 7 }, (_, index) => [index, "x".repeat(8_000)]), |
| 624 | ); |
| 625 | expect( |
| 626 | sanitizeAntigravityToolPayload({ |
| 627 | rawOutput: { ...padding, tail: "x".repeat(7_997) }, |
| 628 | item: { aggregatedOutput: "🚀ok" }, |
| 629 | }), |
| 630 | ).toMatchObject({ item: { aggregatedOutput: "[Earlier output truncated]\n\nok" } }); |
| 631 | }); |
| 632 | }); |
| 633 | |
| 634 | describe("Antigravity served reads", () => { |
| 635 | const flush = (reads: ReturnType<typeof makeAntigravityReadRepair>, toolCallId: string) => |
| 636 | reads.repair({ |
| 637 | sessionId: "session-1", |
| 638 | update: { |
| 639 | sessionUpdate: "tool_call_update", |
| 640 | toolCallId, |
| 641 | status: "failed", |
| 642 | rawOutput: "Tool execution failed", |
| 643 | }, |
| 644 | }).update; |
| 645 | |
| 646 | it("credits a read T3 served before the agent announced it", () => { |
| 647 | const reads = makeAntigravityReadRepair(); |
| 648 | reads.attempted("/repo/a.ts", true); |
| 649 | const announced = reads.repair({ |
| 650 | sessionId: "session-1", |
| 651 | update: { |
| 652 | sessionUpdate: "tool_call", |
| 653 | toolCallId: "call_late", |
| 654 | title: "Running client_view_file", |
| 655 | kind: "read", |
| 656 | status: "in_progress", |
| 657 | locations: [{ path: "/repo/a.ts" }], |
| 658 | }, |
| 659 | }); |
| 660 | expect(announced.update).toMatchObject({ status: "completed" }); |
| 661 | expect(flush(reads, "call_late")).toEqual({ |
| 662 | sessionUpdate: "tool_call_update", |
| 663 | toolCallId: "call_late", |
| 664 | status: "completed", |
| 665 | }); |
| 666 | }); |
| 667 | |
| 668 | it("forgets an earlier turn's unannounced served read", () => { |
| 669 | const reads = makeAntigravityReadRepair(); |
| 670 | reads.attempted("/repo/a.ts", true); |
| 671 | reads.reset(); |
| 672 | reads.repair({ |
| 673 | sessionId: "session-1", |
| 674 | update: { |
| 675 | sessionUpdate: "tool_call", |
| 676 | toolCallId: "call_refused", |
| 677 | title: "Running client_view_file", |
| 678 | kind: "read", |
| 679 | status: "in_progress", |
| 680 | locations: [{ path: "/repo/a.ts" }], |
| 681 | }, |
| 682 | }); |
| 683 | reads.attempted("/repo/a.ts", false); |
| 684 | expect(flush(reads, "call_refused")).toMatchObject({ status: "failed" }); |
| 685 | }); |
| 686 | |
| 687 | it("drops an earlier turn's unanswered read so a new turn's served read gets the credit", () => { |
| 688 | const reads = makeAntigravityReadRepair(); |
| 689 | const announce = (toolCallId: string) => |
| 690 | reads.repair({ |
| 691 | sessionId: "session-1", |
| 692 | update: { |
| 693 | sessionUpdate: "tool_call", |
| 694 | toolCallId, |
| 695 | title: "Running client_view_file", |
| 696 | kind: "read", |
| 697 | status: "in_progress", |
| 698 | locations: [{ path: "/repo/a.ts" }], |
| 699 | }, |
| 700 | }); |
| 701 | announce("call_abandoned"); |
| 702 | reads.reset(); |
| 703 | announce("call_current"); |
| 704 | reads.attempted("/repo/a.ts", true); |
| 705 | expect(flush(reads, "call_current")).toEqual({ |
| 706 | sessionUpdate: "tool_call_update", |
| 707 | toolCallId: "call_current", |
| 708 | status: "completed", |
| 709 | }); |
| 710 | }); |
| 711 | }); |