Skip to content

fix(backend): enable agent-vm reaper to stop idle disk cost leak - #10390

Merged
Git-on-my-level merged 3 commits into
mainfrom
fix/agent-vm-reaper-cost-leak
Jul 25, 2026
Merged

Git-on-my-level merged 3 commits into
mainfrom
fix/agent-vm-reaper-cost-leak

Conversation

@Git-on-my-level

@Git-on-my-level Git-on-my-level commented Jul 23, 2026 •

Copy link
Copy Markdown
Member

Summary

Fixes the cost half of #7326: stopped omi-agent-* VMs keep billing ~50 GB pd-balanced boot disks because autoDelete: true only fires on instance delete, not stop.

Approach: enable/fix the existing backend/charts/agent-vm-reaper CronJob (it was written but never deployed — no GSA, no CronJob in prod-omi-backend). Prefer reusing that machinery over new systems.

  • Extract selection/delete into backend/scripts/agent_vm_reaper.py (dry-run default; live gated)
  • Add refuse-by-default backend/scripts/apply-agent-vm-reaper.sh (creates GSA + WI + ConfigMap + CronJob)
  • TERMINATED grace: lastStopTimestamp ≥ 12h (preserves same-day start_stopped_vm resume; after delete, status NOT_FOUND → clear Firestore → client re-provisions + DB upload)
  • RUNNING older than 2d still reaped as abandoned
  • Checked-in CronJob default DRY_RUN=true so bare apply is log-only

Not in this PR: public NAT removal / firewall cutover (phase-1 health-auth PR / #9261 lane). No network changes.

Live evidence (2026-07-23, project based-hardware)

Metric Value
Fleet ~183 TERMINATED + ~104 RUNNING omi-agent-*
Disk 50 GB pd-balanced each
Idle disk burn 183 × $5 ≈ **$915/mo**
Prod CronJob missing (kubectl -n prod-omi-backend get cronjob — no reaper)
GSA agent-vm-reaper@… NOT_FOUND
Node default SA roles/compute.viewer only — dedicated WI SA required

Dry-run of this PR's script (python3 backend/scripts/agent_vm_reaper.py --dry-run):

  • 137 reapable now (85 TERMINATED ≥12h + 52 RUNNING ≥2d)
  • Immediate idle-disk savings from those TERMINATED ≈ ~$425/mo
  • Remaining TERMINATED age into the 12h window → full idle-disk fleet bounded toward $0 beyond grace; steady-state savings ≈ **$800–915/mo** once LIVE, plus abandoned RUNNING compute

Manual steps after merge (required — no CD for this chart)

# 1) Install IAM + CronJob in dry-run (log-only)
AGENT_VM_REAPER_APPLY=1 bash backend/scripts/apply-agent-vm-reaper.sh

# 2) Manual job + inspect logs
kubectl -n prod-omi-backend create job --from=cronjob/prod-agent-vm-reaper agent-vm-reaper-manual-$(date +%s)
kubectl -n prod-omi-backend logs job/<job-name>

# 3) Enable deletes
AGENT_VM_REAPER_APPLY=1 AGENT_VM_REAPER_LIVE=1 bash backend/scripts/apply-agent-vm-reaper.sh

Local inventory only (no mutation): python3 backend/scripts/agent_vm_reaper.py --dry-run

Test plan

  • python3 -m pytest backend/tests/unit/test_agent_vm_reaper.py -v → 9 passed (selection, dry-run, refuse gates, manifest defaults)
  • bash backend/scripts/apply-agent-vm-reaper.sh without gate → REFUSED exit 1
  • Live dry-run against GCP inventory → 137 targets logged, zero deletes
  • After merge: apply dry-run CronJob, review logs, then LIVE=1
  • Confirm TERMINATED count drops over ~24h; disks deleted with instances

Relationship

Failure-Class: new

Review in cubic

Git-on-my-level added a commit that referenced this pull request Jul 23, 2026

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 1c32ffbbc0

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread backend/scripts/agent_vm_reaper.py Resolved
Comment thread backend/scripts/agent_vm_reaper.py Resolved
Git-on-my-level added a commit that referenced this pull request Jul 23, 2026
Git-on-my-level added a commit that referenced this pull request Jul 23, 2026
- Remove RUNNING VM reaping entirely: agent-proxy keeps active sessions
  RUNNING via /ping keep-alive, and the reaper has no heartbeat signal
  to safely distinguish a long-lived session from an abandoned one.
  Reaping RUNNING VMs by creation age alone would delete VMs underneath
  active sessions.
- Return exit code 1 when live deletes fail so the CronJob's
  restartPolicy: OnFailure retries instead of silently reporting success.
- Replace module-scope sys.modules mutation in tests with a pytest fixture
  to avoid polluting the shared pytest process.

Verification: BACKEND_UNIT_TEST_FILE_LIST=tests/unit/test_agent_vm_reaper.py
bash test.sh — 10 passed.
@Git-on-my-level
Git-on-my-level force-pushed the fix/agent-vm-reaper-cost-leak branch from 8d16fe7 to 659de24 Compare July 23, 2026 09:19
@Git-on-my-level

Copy link
Copy Markdown
Member Author

Monitor run — review feedback addressed

Addressed all 3 Codex review threads:

  • P2 — Don't reap active RUNNING VMs by age alone: Removed RUNNING reaping entirely. The reaper has no heartbeat signal to distinguish a long-lived agent-proxy /ping session from an abandoned VM. TERMINATED-only is the safe surface; the running_age_days / REAP_RUNNING_AGE_DAYS config is gone from the script, manifest, and CronJob.
  • P2 — Return failure when live deletes fail: main() now tracks failures and exits 1 when any live delete fails, so restartPolicy: OnFailure retries.
  • P2 — Avoid module-scope sys.modules mutation in tests: Replaced top-level _load_reaper() with a pytest fixture that does not mutate sys.modules at collection time.

Verification: BACKEND_UNIT_TEST_FILE_LIST=tests/unit/test_agent_vm_reaper.py bash test.sh — 10 passed.

Note: Backend unit suite failure on this PR is the pre-existing test_desktop_release_scripts.py::test_codemagic_produces_canonical_app_and_strictly_verifiable_dmg flake on main (confirmed failing on main too), unrelated to this PR.

@kodjima33 kodjima33 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

agent-vm reaper — approved; not auto-merged (740 lines exceeds scoped bug-fix gate)

Git-on-my-level added a commit that referenced this pull request Jul 24, 2026
Git-on-my-level added a commit that referenced this pull request Jul 24, 2026
- Remove RUNNING VM reaping entirely: agent-proxy keeps active sessions
  RUNNING via /ping keep-alive, and the reaper has no heartbeat signal
  to safely distinguish a long-lived session from an abandoned one.
  Reaping RUNNING VMs by creation age alone would delete VMs underneath
  active sessions.
- Return exit code 1 when live deletes fail so the CronJob's
  restartPolicy: OnFailure retries instead of silently reporting success.
- Replace module-scope sys.modules mutation in tests with a pytest fixture
  to avoid polluting the shared pytest process.

Verification: BACKEND_UNIT_TEST_FILE_LIST=tests/unit/test_agent_vm_reaper.py
bash test.sh — 10 passed.
@Git-on-my-level
Git-on-my-level force-pushed the fix/agent-vm-reaper-cost-leak branch from 659de24 to f9ec905 Compare July 24, 2026 15:13
@Git-on-my-level

Copy link
Copy Markdown
Member Author

Monitor run — rebased onto current main

Rebased this branch onto the latest origin/main (9067aff). The previous CI run was on a stale base where test_desktop_release_scripts::test_codemagic_produces_canonical_app_and_strictly_verifiable_dmg failed — that failure is gone from main and no longer reproduces.

The remaining CI failures (Backend unit suite, Public Developer API contract) are pre-existing main breakage, not caused by this PR:

  • OpenAPI contract: docs/api-reference/app-client-openapi.json is stale on main (same failure on every backend-touching PR and on main pushes).
  • Backend unit suite: 9 pre-existing main failures (stale generated Swift/TS types, langchain_core import error, canonical_memory_vectors access-denied assertion, rate-limit count mismatch).

The PR's own test (test_agent_vm_reaper.py, 10 tests) passes locally against the rebased branch.

Git-on-my-level added a commit that referenced this pull request Jul 24, 2026
Git-on-my-level added a commit that referenced this pull request Jul 24, 2026
- Remove RUNNING VM reaping entirely: agent-proxy keeps active sessions
  RUNNING via /ping keep-alive, and the reaper has no heartbeat signal
  to safely distinguish a long-lived session from an abandoned one.
  Reaping RUNNING VMs by creation age alone would delete VMs underneath
  active sessions.
- Return exit code 1 when live deletes fail so the CronJob's
  restartPolicy: OnFailure retries instead of silently reporting success.
- Replace module-scope sys.modules mutation in tests with a pytest fixture
  to avoid polluting the shared pytest process.

Verification: BACKEND_UNIT_TEST_FILE_LIST=tests/unit/test_agent_vm_reaper.py
bash test.sh — 10 passed.
@Git-on-my-level
Git-on-my-level force-pushed the fix/agent-vm-reaper-cost-leak branch from f9ec905 to 9bf0c06 Compare July 24, 2026 19:14
@Git-on-my-level

Copy link
Copy Markdown
Member Author

Monitor run — rebased onto current main (09ce8a7)

Rebased this branch onto the latest origin/main and force-pushed. The PR's own tests still pass (10/10 in test_agent_vm_reaper.py).

The two failing CI checks are pre-existing main breakage, not caused by this PR (confirmed identical failures on multiple main Backend Unit Tests / OpenAPI Contract runs):

  • Backend unit suite: 7 pre-existing main failures — stale generated Swift/TS types (app_client_*_generator), langchain_core import error (lock_bypass_fixes, action_item_date_validation), canonical_memory_vectors access-denied assertion, rate-limit count mismatch (memories_create, rate_limiting). test_agent_vm_reaper.py passes.
  • OpenAPI contract: docs/api-reference/app-client-openapi.json is stale on main (same failure on every backend-touching PR and on main pushes).

Git-on-my-level added a commit that referenced this pull request Jul 24, 2026
Git-on-my-level added a commit that referenced this pull request Jul 24, 2026
- Remove RUNNING VM reaping entirely: agent-proxy keeps active sessions
  RUNNING via /ping keep-alive, and the reaper has no heartbeat signal
  to safely distinguish a long-lived session from an abandoned one.
  Reaping RUNNING VMs by creation age alone would delete VMs underneath
  active sessions.
- Return exit code 1 when live deletes fail so the CronJob's
  restartPolicy: OnFailure retries instead of silently reporting success.
- Replace module-scope sys.modules mutation in tests with a pytest fixture
  to avoid polluting the shared pytest process.

Verification: BACKEND_UNIT_TEST_FILE_LIST=tests/unit/test_agent_vm_reaper.py
bash test.sh — 10 passed.
@Git-on-my-level
Git-on-my-level force-pushed the fix/agent-vm-reaper-cost-leak branch from 9bf0c06 to 1d2463d Compare July 24, 2026 21:52
@Git-on-my-level

Copy link
Copy Markdown
Member Author

Monitor run — rebased onto current main; OpenAPI contract now green

Rebased this branch onto the latest origin/main (8f3c0f7) and force-pushed. The rebase cleared the previously-failing Public Developer API contract check — it is now SUCCESS. The PR does not touch any OpenAPI-generating surface; the stale-artifact failure was inherited from the old base.

  • PR's own test (test_agent_vm_reaper.py): 10/10 pass post-rebase.
  • Backend unit suite: still fails the same 5 pre-existing files that also fail on main (test_canonical_memory_vectors, test_lock_bypass_fixes, test_memories_create, test_rate_limiting, test_action_item_date_validation). None are touched by this PR; confirmed via latest main run. The last one is a ModuleNotFoundError: langchain_core.messages dependency issue unrelated to the reaper script.
  • All 3 Codex review threads remain resolved; no new bot review threads.

Git-on-my-level and others added 3 commits July 25, 2026 06:03
The existing CronJob was never deployed (no GSA, no prod CronJob). Stop ≠
delete, so TERMINATED omi-agent VMs keep ~50 GB pd-balanced disks. Add a
dry-run-first apply path, 12h terminated grace, and selection tests.

Failure-Class: new
Co-authored-by: Cursor <[email protected]>
- Remove RUNNING VM reaping entirely: agent-proxy keeps active sessions
  RUNNING via /ping keep-alive, and the reaper has no heartbeat signal
  to safely distinguish a long-lived session from an abandoned one.
  Reaping RUNNING VMs by creation age alone would delete VMs underneath
  active sessions.
- Return exit code 1 when live deletes fail so the CronJob's
  restartPolicy: OnFailure retries instead of silently reporting success.
- Replace module-scope sys.modules mutation in tests with a pytest fixture
  to avoid polluting the shared pytest process.

Verification: BACKEND_UNIT_TEST_FILE_LIST=tests/unit/test_agent_vm_reaper.py
bash test.sh — 10 passed.
@Git-on-my-level
Git-on-my-level force-pushed the fix/agent-vm-reaper-cost-leak branch from 1d2463d to 1b492ed Compare July 24, 2026 23:04
@Git-on-my-level

Copy link
Copy Markdown
Member Author

Rebased onto latest main to resolve stale-branch CI failures (langchain_core.messages import error). The agent-vm reaper tests pass against current main.

All checks green.

@Git-on-my-level
Git-on-my-level merged commit 2fd5dfd into main Jul 25, 2026
36 checks passed
@Git-on-my-level
Git-on-my-level deleted the fix/agent-vm-reaper-cost-leak branch July 25, 2026 05:17
undivisible pushed a commit to undivisible/omi that referenced this pull request Sep 29, 2026
undivisible pushed a commit to undivisible/omi that referenced this pull request Sep 29, 2026
…edHardware#10390)

## Summary

Fixes the **cost half** of BasedHardware#7326: stopped `omi-agent-*` VMs keep billing
~50 GB `pd-balanced` boot disks because `autoDelete: true` only fires on
instance **delete**, not stop.

**Approach:** enable/fix the existing `backend/charts/agent-vm-reaper`
CronJob (it was written but never deployed — no GSA, no CronJob in
`prod-omi-backend`). Prefer reusing that machinery over new systems.

- Extract selection/delete into `backend/scripts/agent_vm_reaper.py`
(dry-run default; live gated)
- Add refuse-by-default `backend/scripts/apply-agent-vm-reaper.sh`
(creates GSA + WI + ConfigMap + CronJob)
- TERMINATED grace: `lastStopTimestamp` ≥ **12h** (preserves same-day
`start_stopped_vm` resume; after delete, status `NOT_FOUND` → clear
Firestore → client re-provisions + DB upload)
- RUNNING older than **2d** still reaped as abandoned
- Checked-in CronJob default `DRY_RUN=true` so bare apply is log-only

**Not in this PR:** public NAT removal / firewall cutover (phase-1
health-auth PR / BasedHardware#9261 lane). No network changes.

## Live evidence (2026-07-23, project `based-hardware`)

| Metric | Value |
|--------|-------|
| Fleet | ~183 TERMINATED + ~104 RUNNING `omi-agent-*` |
| Disk | 50 GB `pd-balanced` each |
| Idle disk burn | ~183 × $5 ≈ **~$915/mo** |
| Prod CronJob | **missing** (`kubectl -n prod-omi-backend get cronjob`
— no reaper) |
| GSA `agent-vm-reaper@…` | **NOT_FOUND** |
| Node default SA | `roles/compute.viewer` only — dedicated WI SA
required |

Dry-run of this PR's script (`python3 backend/scripts/agent_vm_reaper.py
--dry-run`):

- **137** reapable now (85 TERMINATED ≥12h + 52 RUNNING ≥2d)
- Immediate idle-disk savings from those TERMINATED ≈ **~$425/mo**
- Remaining TERMINATED age into the 12h window → full idle-disk fleet
bounded toward ~$0 beyond grace; steady-state savings ≈ **~$800–915/mo**
once LIVE, plus abandoned RUNNING compute

## Manual steps after merge (required — no CD for this chart)

```bash
# 1) Install IAM + CronJob in dry-run (log-only)
AGENT_VM_REAPER_APPLY=1 bash backend/scripts/apply-agent-vm-reaper.sh

# 2) Manual job + inspect logs
kubectl -n prod-omi-backend create job --from=cronjob/prod-agent-vm-reaper agent-vm-reaper-manual-$(date +%s)
kubectl -n prod-omi-backend logs job/<job-name>

# 3) Enable deletes
AGENT_VM_REAPER_APPLY=1 AGENT_VM_REAPER_LIVE=1 bash backend/scripts/apply-agent-vm-reaper.sh
```

Local inventory only (no mutation): `python3
backend/scripts/agent_vm_reaper.py --dry-run`

## Test plan

- [x] `python3 -m pytest backend/tests/unit/test_agent_vm_reaper.py -v`
→ 9 passed (selection, dry-run, refuse gates, manifest defaults)
- [x] `bash backend/scripts/apply-agent-vm-reaper.sh` without gate →
REFUSED exit 1
- [x] Live dry-run against GCP inventory → 137 targets logged, zero
deletes
- [ ] After merge: apply dry-run CronJob, review logs, then LIVE=1
- [ ] Confirm TERMINATED count drops over ~24h; disks deleted with
instances

## Relationship

- **BasedHardware#7326** — progresses **cost** half only (partial). Full close still
needs private networking / remove public NAT (deferred; depends on proxy
routing + peer/move VMs).
- **Health-auth / superseding BasedHardware#9261** — separate PR; this change does
not touch agent-proxy, NAT, or firewall.

Failure-Class: new



<!-- This is an auto-generated description by cubic. -->
<a
href="https://cubic.dev/pr/BasedHardware/omi/pull/10390?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. -->
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants