Skip to content

fix(antigravity): keep turns open when a background wake is still pending - #39989

Merged
juliusmarminge merged 3 commits into
pingdotgg:mainfrom
mkantautas:t3/antigravity-background-wake-race
Nov 21, 2026
Merged

juliusmarminge merged 3 commits into
pingdotgg:mainfrom
mkantautas:t3/antigravity-background-wake-race

Conversation

@mkantautas

Copy link
Copy Markdown
Contributor

Problem

When an Antigravity (agy) turn has a background task complete while a model reply is already in flight, T3 Code can finalize the turn early and (for delegated tasks) send session/close, terminating agy before it streams the follow-up wake reply.

Observed live on 0.1.12-nightly.20261120.4994 (gemini-4-neon, delegated task):

  1. Two parallel background commands (bin/gates back and EmailBodyAsDocumentTest.php) stayed running past session/prompt (end_turn at 16:46:10.537Z).
  2. Command oxc stack #1 finished at 16:46:21.356Z and started wake reply oxc stack #1 (~9 s TTFB on 357k tokens).
  3. Command Add open-in-editor feature with Cmd+O shortcut #2 finished 503 ms later (16:46:21.859Z) while reply oxc stack #1 was still in flight; agy queued its MESSAGE_PRIORITY_HIGH completion in .system_generated/messages/.
  4. Reply oxc stack #1 streamed (16:46:30.295Z) and ended with usage_update at ~16:46:30.739Z. agy immediately dequeued command Add open-in-editor feature with Cmd+O shortcut #2 into transcript.jsonl (step_index: 163, SYSTEM_MESSAGE, 16:46:30Z) and started wake reply Add open-in-editor feature with Cmd+O shortcut #2.
  5. On usage_update oxc stack #1, T3 saw no running tools left, set context.replyOpen = false, armed the 3 s ACP_DEFERRED_FINALIZE_DEBOUNCE, finalized the turn at 16:46:33.739Z before reply Add open-in-editor feature with Cmd+O shortcut #2's first token could arrive, and sent session/close at 16:46:34.201Z.

The same race also occurs when a single background command finishes while the initial prompt's final PLANNER_RESPONSE is still in flight, so the tool is already completed when session/prompt returns end_turn while its wake sits unread in the inbox.

Fix

  • Add an optional hasPendingReply(sessionId) hook on AcpAdapterV2Flavor in packages/provider-acp/src/server/adapter.ts:
    • In scheduleDeferredFinalize, when the 3 s post-usage_update debounce expires (!replyOpen), check hasPendingReply. If true, re-open context.replyOpen = true and extend into a single ACP_OPEN_REPLY_QUIET_LIMIT (2 min) wait instead of finalizing early; serialize the final check and finalizeTurn under runtimeCallbackPermit. Because it only extends from !replyOpen, a stalled wake still finishes after the 2-minute quiet limit and cannot loop.
    • On session/prompt completion (status === "completed"), check hasPendingReply when no background tool or subagent is still running so a pre-end_turn background completion holds the turn open until its wake reply finishes.
  • Implement readAntigravityPendingReply in apps/server/src/provider/acp/AntigravityChildWork.ts and wire it in apps/server/src/orchestration-v2/Adapters/AntigravityAdapterV2.ts when profileDirectory is set:
    • Checks .system_generated/messages/ against read.json for unread MESSAGE_PRIORITY_HIGH messages (ignoring MESSAGE_PRIORITY_LOW notices such as cancelled tasks or restart notices).
    • Checks the tail of .system_generated/logs/transcript.jsonl (64 KiB fast path, 256 KiB cap) for a trailing waking SYSTEM_MESSAGE step with no subsequent PLANNER_RESPONSE.
    • Uses antigravityManagedPath and O_NOFOLLOW and fails closed on missing, linked, or corrupt records.
    • Avoids false hangs when agy batches multiple completions into a single reply (all receipts read and transcript ends in PLANNER_RESPONSE), finalizing in 3 s as before.

Tests

  • apps/server/src/orchestration-v2/Adapters/AntigravityAdapterV2.test.ts:
    • Two parallel background commands finishing 500 ms apart: separate second wake (waits >3 s for reply 2 in the same run), batched into one reply (finalizes at 3 s without extending), and stalled second wake (bounded by the 2-minute quiet limit).
    • Background command finishing before session/prompt returns end_turn with an unread high-priority wake.
  • apps/server/src/provider/acp/AntigravityChildWork.test.ts:
    • Unit tests for readAntigravityPendingReply covering high vs. low priority unread messages, read.json receipts, trailing SYSTEM_MESSAGE vs. PLANNER_RESPONSE, >64 KiB transcript tails, corrupt read.json, invalid session IDs, and symlinks.
  • Ran vp run check:changed (format, lint, knip, graphql, typecheck across 14 packages, 47 tests) and vp test run apps/server/src/orchestration-v2/Adapters/AcpAdapterV2.test.ts -t "defer|background|usage_update|wake" (14 passed).

Written by Gemini 4 Neon, in Antigravity.

Loading
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

size:L 100-499 changed lines (additions + deletions). vouch:unvouched Author is not yet trusted in the VOUCHED list.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants