agent-orchestrator icon indicating copy to clipboard operation
agent-orchestrator copied to clipboard

[Bug]: agy session gets trapped in Terminal mode on tui -> chat switch due to checkpoint mismatches [ao_high_water] when turn 1 contains handoff or settings wrappers

Open fewensa opened this issue 2 weeks ago • 2 comments

What went wrong?

In Agent Orchestrator v0.15.3, switching an agy (Antigravity) session from Chat UI to Terminal (chat -> tui) and then switching back to Chat UI (tui -> chat) deterministically fails with TARGET_HISTORY_UNSETTLED (checkpoint mismatches [ao_high_water]: context deadline exceeded), even when agy ACP session/load completes in ~1.3s.

Unlike slow-provider timeout cases (such as #7697), session_load succeeds immediately here (stage=session_load outcome=ok), and every poll iteration during the 45s settle window deterministically rejects the replayed history due to a text normalization mismatch:

  1. Wrapper injection on the initial agy turn:

    • When a session switches in-place to agy via POST /api/v1/sessions/{id}/provider-switch (or starts a new agy session with initial model configuration), the first prompt stored in agy's native conversation database (antigravity-acp/conversations/<uuid>.db) wraps the user prompt with:
      • <USER_REQUEST> ... </USER_REQUEST>
      • Standing instructions from Agent Orchestrator... <ao_standing_instructions>...</ao_standing_instructions>
      • <ao-handoff-request switch-id="...">...</ao-handoff-request> (on provider-switch)
      • Queued message JSON envelope (when delivered via ao send / automation)
      • <USER_SETTINGS_CHANGE> The user has changed the model to ... </USER_SETTINGS_CHANGE>
    • Meanwhile, Orchestrator's conversation_messages table stores only the bare user message text with an empty provider_item_id ("").
  2. Incomplete wrapper stripping during tui -> chat replay (acp.historicalUserContent):

    • On tui -> chat, BridgeConversationToACP bridges the conversation back and session/load replays the native history.
    • captureAOHighWater selects the highest-sequence user message (highWater["message.user.completed"]). Because provider_item_id is empty for that user message, nativeHistoryCheckpoint.mismatches falls back to domain.NativeCheckpointTextMatches(aoText, replayedText).
    • acp.historicalUserContent only strips the standing-instructions block; it does not strip <ao-handoff-request>...</ao-handoff-request>, queued batch envelopes, or <USER_REQUEST> / <USER_SETTINGS_CHANGE> tags persisted by agy.
    • NativeCheckpointTextMatches therefore returns false on every poll until the deadline expires with: switch interface <session>: resume chat: wait for settled native conversation history: chat conversation history is not settled: checkpoint mismatches [ao_high_water]: context deadline exceeded
  3. Session is permanently trapped in tui mode with no official API escape hatch:

    • Sending additional turns inside Terminal (tui) does not advance conversation_messages in Orchestrator's DB, so highWater["message.user.completed"] remains pinned to the wrapped turn.
    • Retrying POST /api/v1/sessions/{id}/interface-transition with "historyPolicy": "provider_history" is rejected with HTTP 409 (PROVIDER_HISTORY_RECOVERY_UNAVAILABLE) because ChatHistoryMismatchOnlyUntrustedText only admits untrusted_user_text and untrusted_assistant_text, not ao_high_water.
    • POST /api/v1/sessions/{id}/provider-switch requires mode == "chat" (requireChatSession), so it cannot be used from tui mode either.

Steps to reproduce

  1. Start a Chat UI session (for example on codex), run a few turns, and switch it in-place to agy via provider-switch (or start an agy Chat session with model configuration changes on turn 1), then complete 1 turn on agy.
  2. Switch the session from Chat UI to Terminal (chat -> tui).
  3. Switch the session back from Terminal to Chat UI (tui -> chat).
  4. Observe ACP start stage ... stage=session_load outcome=ok in ~1.3s, followed ~45s later by Interface switch failed: ... checkpoint mismatches [ao_high_water]: context deadline exceeded.

Expected behavior

  • acp.historicalUserContent (or agyacp history normalization / NativeCheckpointTextMatches) should strip <ao-handoff-request> blocks, queued delivery envelopes, and agy-injected <USER_REQUEST> / <USER_SETTINGS_CHANGE> wrappers when matching replayed user messages against ao_high_water (or persist a stable provider_item_id for user turns).
  • If an ao_high_water text mismatch still occurs due to provider-side prompt wrapping, allow explicit user-confirmed recovery back to Chat UI instead of leaving the session permanently stuck in tui mode.

Version

0.15.3

fewensa avatar Nov 19 '26 10:11 fewensa

This is a bug report. Check that nobody has claimed it, comment to claim it, and open a PR with Fixes #8677.

See the contributing guide. Think the classification is wrong? Tell us on Discord.

OrcInc avatar Nov 19 '26 11:11 OrcInc

I'd like to work on this. historicalUserContent only strips the <ao-standing-instructions> form, but agyacp sends its standing instructions as the <ao_standing_instructions> lead-sentence block, which Antigravity replays joined to the first prompt. So that message never matches AO's record and the switch times out on ao_high_water. I have a small fix with a unit test that strips that form too; I'll leave the <USER_REQUEST> wrapper part out unless someone can confirm agy replays it.

utsxvrai avatar Nov 21 '26 11:11 utsxvrai