Skip to content

fix(backend): clear the agentVm record when its GCE instance is gone (reaped VM strands agent chat) - #10694

Merged
kodjima33 merged 1 commit into
mainfrom
watchdog/agent-vm-reaped-record-recovery
Jul 27, 2026
Merged

kodjima33 merged 1 commit into
mainfrom
watchdog/agent-vm-reaped-record-recovery

Conversation

@kodjima33

@kodjima33 kodjima33 commented Jul 27, 2026 •

Copy link
Copy Markdown
Collaborator

Bug (live prod)

backend/scripts/agent_vm_reaper.py (CronJob) deletes aged TERMINATED omi-agent-* instances to stop boot-disk billing, but it leaves users/{uid}.agentVm saying status: ready with the deleted VM's IP. Both Python readers folded the GCE 404 into "UNKNOWN":

  • agent-proxy _ensure_vm_running → if gce_status not in ("TERMINATED", "STOPPED"): return vm # STAGING, etc. — let it be → returns the stale record → agent_ws then polls the dead IP for the full _wait_for_vm_healthy 120s window and answers {"type":"error","message":"Agent VM is not responding"}.
  • POST /v1/agent/vm-ensure → falls through and answers {"has_vm": true, "status": "ready"}.

Nothing repairs the record, and POST /v2/agent/provision reports exists while it is present, so agent chat stays broken on every later attempt. Prod evidence (based-hardware, 2026-07-25..26): 20 distinct users whose agentVm points at a GCE instance that returns 404 (e.g. omi-agent-5yc7omh3ffbg — 20 lookups, omi-agent-pkqlgn652iaf — 19), plus the matching unhandled ASGI WebSocketDisconnect when the user gives up before the 120s wait ends (main.py:432, 12:41:10Z).

Fix

Report a 404 as NOT_FOUND — distinct from UNKNOWN ("could not tell") — and, only for that status, delete the agentVm record so a client can provision a replacement. This is the resume contract GET /v2/agent/status already implements on the desktop Rust path (desktop/macos/Backend-Rust/src/routes/agent.rs), now extended to the two Python readers. UNKNOWN keeps the existing leave-it-alone behavior, so a transient GCE error can never delete a live VM's record.

  • agent-proxy/main.py: _check_gce_status returns NOT_FOUND on 404; _ensure_vm_running clears the record and fails fast; _start_vm_and_wait raises VmNotFoundError on 404 so the non-ready restart path clears it too.
  • routers/agent_tools.py: vm-ensure returns {"has_vm": false} and clears the record.
  • database/users.py: delete_agent_vm(uid).

What I tested

Real WS route through the agent-proxy FastAPI app (TestClient.websocket_connect("/v1/agent/ws"), only Firebase/Firestore/GCE-HTTP edges faked, GCE instance GET → 404):

close latency dead-IP health probes last message to client Firestore
before 120.10s 41 Agent VM is not responding record untouched
after 0.02s 1 Failed to start agent VM agentVm deleted

Regression tests — backend/tests/unit/test_agent_vm_reaped_record_recovery.py (6 behavioral tests through the module seams): 4 fail on the pre-fix code (git stash of the three source files), all 6 pass after. Two of them assert the negative case — an UNKNOWN (500) status must leave the record intact.

Suites run (pytest): test_agent_vm_reaped_record_recovery.py, test_agent_proxy_async_boundaries.py, test_agent_vm_async.py, test_agent_vm_reaper.py, test_tools_agent_route_response_shape.py, test_scan_async_blockers.py → 63 passed. black --line-length 120 --skip-string-normalization clean.

Product invariants affected: none (scripts/pr-preflight --suggest).

This is the stated resume contract of FC-agent-vm-stop-retains-disk ("keep the client NOT_FOUND → re-provision path
as the resume contract after delete") — the contract the Python readers never implemented. The new regression test is
recorded as that class's canonical_prevention_artifact.

Failure-Class: FC-agent-vm-stop-retains-disk

🤖 automated by hourly watchdog — tested and merged

Review in cubic

The agent-vm reaper deletes aged TERMINATED omi-agent-* instances but leaves
users/{uid}.agentVm saying "ready" with the deleted VM's IP. Both Python
readers folded the GCE 404 into "UNKNOWN": _ensure_vm_running fell into its
"STAGING, etc. — let it be" branch and returned the stale record, so
agent-proxy dialed a dead address and waited out the full 120s health timeout
before answering "Agent VM is not responding", and POST /v1/agent/vm-ensure
answered "ready". Nothing repaired the record, and the desktop provision
endpoint reports "exists" while it is present, so the user's agent chat stayed
broken on every later attempt. 20 distinct users hit this in prod on
2026-07-25..26 (repeat attempts per user).

Report 404 as NOT_FOUND (distinct from "could not tell") and, only for that
status, delete the record — the same resume contract GET /v2/agent/status
already implements on the desktop Rust path. UNKNOWN still leaves a live VM's
record untouched.

Verified: real WS route through the agent-proxy FastAPI app with GCE returning
404 — before: 120.1s to close, 41 probes of the dead IP, "Agent VM is not
responding", record untouched; after: 0.02s, 1 probe, error + record cleared.
Regression tests fail on the pre-fix code (4/6) and pass after; 63 tests green
across the agent VM / agent-proxy / route-shape suites.

Failure-Class: FC-agent-vm-stop-retains-disk
@kodjima33
kodjima33 merged commit 571c5d8 into main Jul 27, 2026
38 of 41 checks passed
@kodjima33
kodjima33 deleted the watchdog/agent-vm-reaped-record-recovery branch July 27, 2026 01:44
Git-on-my-level added a commit that referenced this pull request Aug 4, 2026
…ks (#11029)

## Summary

- Make the durable `account_deletions/{uid}` marker a single fail-closed
admission policy across Firebase, developer/MCP keys, OAuth issuance,
HTTP/WebSocket entry points, Agent proxy sessions, and Agent VM
provisioning. Completed and unknown markers remain fenced so an old
token cannot revive a deleted UID.
- Make deletion admission atomic at `pending`, revoke UID-bearing
API/OAuth credentials, delete the account's GCE Agent VM before its
Firestore pointer disappears, and transactionally fence late proxy/VM
writes so in-flight work cannot recreate owner data.
- Make accepted macOS cleanup durable across restart, drain owner-bound
VM/sync work before deleting storage, remove the legacy anonymous
fallback, upload a consistent SQLite snapshot with cancellable
compression and bounded retries, and preserve typed startup errors in
Flutter.

Repeated delete requests during `pending`/`running` intentionally
receive typed `403 account_deletion_in_progress` responses without
marker rollback or requeue. The deletion fence is preserved.

Canonical project: [BasedHardware Project
#15](https://github.com/orgs/BasedHardware/projects/15)

## Sanitized incident evidence

- A delete confirmation preceded a later same-provider
sign-in/onboarding sequence by nine days.
- The deleted UID's user document and GCE instance were gone, while its
durable deletion marker completed after retried queue acceleration
failures.
- No verified event proves old server memory reads after deletion. This
PR treats legacy local-database migration, still-valid old credentials,
and late owner-scoped work as identity-isolation risks; typed readiness
failures remain timeout-class evidence rather than proof of server data
disclosure.

Related lineage: #10444, #7696, #9632, #10597, #9821, #6891, #6892,
#10600, and #10694.

## Failure class

Failure-Class: none

This is an audit-driven hardening sweep across durable deletion
admission, credential revocation, local owner-data disposal, and Agent
VM/proxy lifecycle safety. The reusable prevention surfaces are the
shared fail-closed policy, transactional admission/CAS writers, the
durable local cleanup journal, the effective-owner generation fence, and
behavioral regression tests.

## Product invariants affected

- INV-AUTH-1 — preserves the exclusive effective-owner transition,
clears accepted-deletion storage before credential/session publication,
blocks every credential family after durable admission, and rejects late
owner-bound Agent VM continuations.
- INV-CHAT-1 — drains Agent VM/sync work at the existing owner
transition, transactionally fences late proxy message/session writes,
and preserves typed startup failures without changing canonical
transcript ownership.

## Changed paths

-
`.github/scripts/product_file_line_count_ratchet_baseline/backend-database.json`
-
`.github/scripts/product_file_line_count_ratchet_baseline/backend-routers.json`
-
`.github/scripts/product_file_line_count_ratchet_baseline/desktop-swift-root.json`
- `app/lib/services/agent_chat_service.dart`
- `app/test/services/agent_chat_service_test.dart`
- `backend/agent-proxy/Dockerfile`
- `backend/agent-proxy/main.py`
- `backend/database/account_deletion_projection_fence.py`
- `backend/database/mcp_oauth.py`
- `backend/database/users.py`
- `backend/dependencies.py`
- `backend/routers/apps.py`
- `backend/routers/desktop_agent_vm.py`
- `backend/routers/mcp_sse.py`
- `backend/routers/oauth.py`
- `backend/routers/transcribe.py`
- `backend/services/users/account_deletion.py`
- `backend/testing/e2e/test_account_deletion_cloud_tasks.py`
- `backend/tests/services/users/test_account_deletion.py`
- `backend/tests/unit/test_account_deletion_auth_fence.py`
- `backend/tests/unit/test_agent_proxy_async_boundaries.py`
- `backend/tests/unit/test_agent_proxy_startup_client_gone.py`
- `backend/tests/unit/test_byok_security.py`
- `backend/tests/unit/test_dependency_async_boundaries.py`
- `backend/tests/unit/test_desktop_agent_vm.py`
- `backend/tests/unit/test_mcp_api_key_auth_context.py`
- `backend/tests/unit/test_mcp_oauth.py`
- `backend/tests/unit/test_oauth_token_async_boundaries.py`
- `backend/utils/account_deletion_access.py`
- `backend/utils/other/endpoints.py`
- `desktop/macos/Desktop/Sources/AgentSyncService.swift`
- `desktop/macos/Desktop/Sources/AgentVMService.swift`
- `desktop/macos/Desktop/Sources/AuthService.swift`
- `desktop/macos/Desktop/Sources/Chat/RuntimeOwnerIdentity.swift`
- `desktop/macos/Desktop/Sources/DefaultsKey.swift`
- `desktop/macos/Desktop/Sources/DesktopAutomationBridge.swift`
-
`desktop/macos/Desktop/Sources/MainWindow/Pages/Settings/Components/SettingsContentView+SettingsUpdates.swift`
-
`desktop/macos/Desktop/Sources/Rewind/Core/RewindDatabase+AccountDeletion.swift`
- `desktop/macos/Desktop/Tests/AgentSyncBatchQueryTests.swift`
- `desktop/macos/Desktop/Tests/AgentVMLifecycleFenceTests.swift`
- `desktop/macos/Desktop/Tests/AuthSessionCoordinatorTests.swift`
- `desktop/macos/Desktop/Tests/OnboardingPersistenceClearingTests.swift`
- `desktop/macos/Desktop/Tests/RewindDatabaseLifecycleTests.swift`
-
`desktop/macos/changelog/unreleased/20260802-account-deletion-local-data.json`
- `desktop/macos/e2e/flows/logout.yaml`

## Verification

- Focused backend deletion/auth/proxy/VM suite: `194 passed`.
- Hermetic Cloud Tasks deletion suite: `5 passed`; repeated
pending/running requests are asserted as typed `403` without requeue.
- Focused macOS deletion, owner-fence, snapshot, and sync suite: `32
tests`, `0 failures`.
- Focused Flutter Agent Chat suite: `2 passed`.
- Follow-up MCP OAuth consent suite: `37 passed`.
- Follow-up macOS cleanup/bridge compile suite: `17 tests`, `0
failures`.
- Python runtime sources compile cleanly; pinned Python/Dart/Swift
formatters and `git diff --check` are clean.
- Required CI-lane PR-body preflight: `34 checks passed` in `108.01s`.

Backend Hermetic Merge Gate: in scope because backend runtime and
hermetic contract code changed.

## Limitations

- No production Firebase/Auth, Firestore, Cloud Tasks, GCE, deployment,
release, or customer-data mutation was performed.
- Same-provider recreation is covered hermetically at the UID/auth
boundary; no live Google sign-in was attempted.
- Agent readiness and teardown are covered through backend
state-machine, proxy protocol, owner-generation, snapshot/upload,
credential cleanup, and stale-pointer tests; no live GCE VM was
provisioned.

<!-- This is an auto-generated description by cubic. -->
<a
href="https://cubic.dev/pr/BasedHardware/omi/pull/11029?utm_source=github"
target="_blank" rel="noopener noreferrer"
data-no-image-dialog="true"><picture><source
media="(prefers-color-scheme: dark)"
srcset="https://www.cubic.dev/buttons/review-in-cubic-dark.svg"><source
media="(prefers-color-scheme: light)"
srcset="https://www.cubic.dev/buttons/review-in-cubic-light.svg"><img
alt="Review in cubic"
src="https://www.cubic.dev/buttons/review-in-cubic-dark.svg"></picture></a>
<!-- End of auto-generated description by cubic. -->
@undivisible undivisible added the AI label Aug 10, 2026
undivisible pushed a commit to undivisible/omi that referenced this pull request Sep 29, 2026
…asedHardware#10694)

The agent-vm reaper deletes aged TERMINATED omi-agent-* instances but leaves
users/{uid}.agentVm saying "ready" with the deleted VM's IP. Both Python
readers folded the GCE 404 into "UNKNOWN": _ensure_vm_running fell into its
"STAGING, etc. — let it be" branch and returned the stale record, so
agent-proxy dialed a dead address and waited out the full 120s health timeout
before answering "Agent VM is not responding", and POST /v1/agent/vm-ensure
answered "ready". Nothing repaired the record, and the desktop provision
endpoint reports "exists" while it is present, so the user's agent chat stayed
broken on every later attempt. 20 distinct users hit this in prod on
2026-07-25..26 (repeat attempts per user).

Report 404 as NOT_FOUND (distinct from "could not tell") and, only for that
status, delete the record — the same resume contract GET /v2/agent/status
already implements on the desktop Rust path. UNKNOWN still leaves a live VM's
record untouched.

Verified: real WS route through the agent-proxy FastAPI app with GCE returning
404 — before: 120.1s to close, 41 probes of the dead IP, "Agent VM is not
responding", record untouched; after: 0.02s, 1 probe, error + record cleared.
Regression tests fail on the pre-fix code (4/6) and pass after; 63 tests green
across the agent VM / agent-proxy / route-shape suites.

Failure-Class: FC-agent-vm-stop-retains-disk
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants