Skip to content

bug(sync-health): list_git_enabled_agents() does not exclude soft-deleted agents — the 60s poller hammers dead agents and poisons their circuit breakers #1561

Closed

Description

@webmixgamer

Summary

SyncHealthService polls every git-sync-enabled agent once a minute via db.list_git_enabled_agents() (src/backend/services/sync_health_service.py:91). That accessor (src/backend/db/schedules.py:3103) selects from agent_git_config filtered only on sync_enabled == 1 — it does not join agent_ownership and does not exclude deleted_at IS NOT NULL.

Soft delete preserves agent_git_config rows by design, so the poller keeps calling agents whose container was removed — indefinitely. Each attempt raises httpx.ConnectError, which is exactly the exception class that increments the transport circuit breaker.

Context

The sibling accessor was already hardened for this. list_all_enabled_schedules() (src/backend/db/schedules.py:437) carries a docstring stating it filters agent_ownership.deleted_at "otherwise the scheduler fires" for soft-deleted agents. The git-sync poller was simply missed by that pass.

Observed on a dev instance after an agent was soft-deleted at 11:42:52 (container removed):

Error getting git status for <name>: All connection attempts failed     # once a minute, forever
11:45:14  Circuit OPENED for agent <name> after 3 failures
12:18:19  Circuit DORMANT for agent <name> after 10 consecutive open-probe failures — switching to 3600s cooldown
12:18:19  [CB] circuit_breaker_dormant operator-queue entry emitted for <name>

Three consequences, all real:

  1. The breaker for a deleted agent is driven to DORMANT, which then poisons any future agent created under the same name — see the companion issue bug(lifecycle): stale transport circuit-breaker Redis key survives agent delete/recreate — a fresh healthy agent fast-fails as "unhealthy" #1560. The arithmetic corroborates the poller as the driver: with CIRCUIT_FAILURE_THRESHOLD = 3 and cooldowns of 30 + 60 + 120 + 240 + 300×6 s ≈ 36 min against a 60s poll interval, 11:42 + ~36 min = 12:18, matching the DORMANT log to the minute.
  2. A bogus operator-queue alert (circuit_breaker_dormant) is raised naming an agent that no longer exists, and it sits unresolved in the Operations queue.
  3. Unbounded error churn — one failed HTTP call per deleted agent per minute, forever, plus the operator-queue sync loop retrying delivery of that alert every 5s once any container reappears under the name.

fleet_audit_service.py:28 consumes the same accessor for GET /api/fleet/sync-audit, so the audit surface also reports soft-deleted agents. Both consumers want the filter; nothing depends on deleted agents being included.

Reproduction

  1. Create an agent with git sync enabled.
  2. DELETE /api/agents/<name> (soft delete — container removed, agent_git_config row retained).
  3. Tail the backend log: Error getting git status for <name>: All connection attempts failed recurs every 60s indefinitely.
  4. After ~3 min the transport breaker opens; after ~36 min it goes DORMANT and emits an operator-queue alert for the deleted agent.
  5. GET /api/fleet/sync-audit still lists the deleted agent.

Acceptance Criteria

  • list_git_enabled_agents() joins agent_ownership and excludes rows where deleted_at IS NOT NULL
  • SyncHealthService no longer issues any HTTP call to a soft-deleted agent
  • GET /api/fleet/sync-audit no longer reports soft-deleted agents
  • No circuit-breaker failure is ever recorded against a soft-deleted agent, and no circuit_breaker_dormant operator-queue entry is emitted for one
  • Audit sweep: every DB accessor consumed by a background loop is checked for the same missing deleted_at filter, and the result is recorded in the issue (the scheduler's accessor was hardened; this one was not — there may be others)
  • Regression test: soft-delete a git-sync-enabled agent, run one SyncHealthService cycle, assert zero agent HTTP calls and zero breaker failures recorded

Technical Notes

Activity

  1. added
    priority-p2
    type-bug
    theme-reliability
    complexity-low
    status-ready
    on Jul 9, 2026
  2. added
    status-in-progress
    and removed
    status-ready
    on Jul 10, 2026
  3. self-assigned this
    on Jul 10, 2026
  4. dolho commented on Jul 10, 2026

    @dolho
    Contributor

    Fix in #1565.

    Audit-sweep result (AC: "every DB accessor consumed by a background loop is checked for the same missing deleted_at filter"):

    list_git_enabled_agents was the sole offender. Findings:

    Loop Enumeration source Soft-deleted excluded?
    sync_health (60s) list_git_enabled_agents (DB) NO — this bug, fixed in #1565
    monitoring (30s) list_all_agents_fast() (Docker) ✓ removed container not listed
    operator_queue (5s) list_all_agents_fast() + status==running (Docker) ✓
    scheduler list_all_enabled_schedules (DB) ✓ already hardened (#834)
    capacity run_maintenance (60s) list_agents_with_queued (DB) ✓ DB-only backlog drain, no agent HTTP
    heartbeat watch (5s) Redis seen keys, cleared on delete ✓

    Conclusion: every other per-agent HTTP poller enumerates from Docker (a removed soft-deleted container is inherently absent), and the remaining DB-driven loops either already filter deleted_at (scheduler) or issue no agent HTTP call (capacity drain is DB-only). No additional missing-filter accessor found on a breaker-affecting path.

    AC coverage:

    • accessor joins agent_ownership + excludes deleted_at IS NOT NULL
    • SyncHealthService issues no HTTP to a soft-deleted agent
    • /api/fleet/sync-audit no longer reports soft-deleted agents (same accessor)
    • no breaker failure / no circuit_breaker_dormant entry for a soft-deleted agent (no HTTP → no ConnectError)
    • audit sweep recorded (above)
    • regression test (TestSoftDeletedExcluded: excluded from accessor + zero HTTP in one poll cycle)

    Also converted git_service.get_git_status's bare print() → logger.warning (the "minor, same code path" note). Inheritance side (agent:circuit:{name} surviving delete/recreate) stays with the companion #1560.

  5. added
    status-in-dev
    and removed
    status-in-progress
    on Jul 10, 2026
  6. vybe commented on Jul 10, 2026

    @vybe
    Contributor

    Fixed by #1565 (merged to dev). Promoting to status-in-dev — the bare (#1561) reference didn't trip the closing-keyword auto-promotion.

  7. added a commit that references this issue on Jul 26, 2026
  8. removed
    status-in-dev
    on Aug 7, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

complexity-lowpriority-p2theme-reliabilitytype-bug

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions