Skip to content

fix(git-sync): bound agent .git bloat — maintenance repack + ignore conventions + size observability (#1596, PR-1) - #1597

Merged
vybe merged 2 commits into
devfrom
feature/1596-git-sync-bloat
Jul 14, 2026
Merged

vybe merged 2 commits into
devfrom
feature/1596-git-sync-bloat

Conversation

@dolho

@dolho dolho commented Jul 13, 2026 •

Copy link
Copy Markdown
Contributor

Problem (P1 — disk exhaustion)

Git-sync auto-commit (agent_server/routers/git.py::_run_auto_sync_once) does git add -A → commit → push every 15-min heartbeat. Under skills that write churny data files, .git grew unboundedly: one agent's .git reached 44 GB / 2,267 packs / 4.26M objects; the fleet hit ~110 GB and the host 90%.

⚠️ Root cause update (see #1595) — scope narrowed; destructive prong dropped

Further investigation (credit: parallel work on #1595 / #1505) found the true root cause is not reachable history retention — it's that git maintenance can never complete inside agent containers: git's detached gc --auto reparents to PID 1, the #817 orphan sweeper SIGKILLs it (a bare SIGKILL, so it leaves stale .lock/gc.pid litter), and every subsequent gc then aborts on the stale lock — permanently disabling maintenance for that repo. ~97% of the bloat is unreachable garbage, not committed history (a plain non-destructive git gc --prune=now took one agent 44 GB → 1.6 GB, fsck clean, HEAD unchanged).

Consequence for this PR:

This PR — the non-destructive prongs that stand on their own

1. Maintenance repack (agent side)

After a successful push, _maybe_run_git_maintenance runs git repack -adl + git gc --prune=now once the pack count crosses GIT_MAINTENANCE_PACK_THRESHOLD (default 20). Self-throttling, best-effort, non-destructive. Note: its reliability on large repos depends on the #1595 sweeper fix (an in-container git process is currently sweep-exposed) — it fully self-heals small/medium repos now and all repos once #1595 lands.

2. Ignore conventions

git_service._GITIGNORE_PATTERNS now also excludes node_modules/, .venv/, venv/, __pycache__/, *.pyc/*.pyo, .pytest_cache/, .mypy_cache/, .ruff_cache/, .ipynb_checkpoints/, *.sqlite/*.sqlite3/*.db. Merged into each agent on sync (also untracks already-committed matches). Datasets/indexes belong in data_paths (#1169).

3. Observability

  • Agent measures .git size (du -sb) into sync-state.json.git_dir_bytes, returned via GET /api/git/status.
  • New agent_sync_state.git_dir_bytes column (dual migration: SQLite + Alembic 0019); SyncHealthService persists it; surfaced on GET /api/agents/sync-health, GET /api/fleet/sync-audit, and the per-agent sync-state endpoint.

Acceptance criteria

Tests

tests/unit/test_1596_git_sync_observability.py — git_dir_bytes round-trips + a partial (failed) update preserves the last size; the default .gitignore includes the bulk-data set and keeps the pre-existing credential/content/ safety rules. Schema + Alembic parity green.

Deploy note

Agent-side maintenance + .git-size measurement land on a base-image rebuild + agent recreate. The DB/backend surfacing works immediately. Full de-bloat of the existing 44 GB agents comes from #1595 (fix the sweeper so gc --prune=now can run to completion); this PR bounds future growth and gives operators the size curve.

Related to #1596 · root cause #1595 · #1505

🤖 Generated with Claude Code

dolho and others added 2 commits July 13, 2026 17:46
…nventions, size observability (#1596, PR-1)

Git-sync auto-commit snapshots the whole workspace every 15-min heartbeat and
NEVER ran git maintenance, so data-churn + `git gc --auto`'s never-consolidating
packs grew one agent's `.git` to 44GB / 2,267 packs and the fleet to ~110GB.
This is the non-destructive first slice (the safe trio); an opt-in history-squash
policy for existing bloat is a deferred follow-up.

Maintenance (agent side, docker/base-image/agent_server/routers/git.py):
- after a successful push, `_maybe_run_git_maintenance` runs a consolidating
  `git repack -adl` + `git gc --prune=now` once packs cross
  GIT_MAINTENANCE_PACK_THRESHOLD (default 20). Self-throttling, best-effort,
  never fails the sync. Non-destructive — does NOT rewrite reachable history.

Ignore conventions (git_service._GITIGNORE_PATTERNS):
- add node_modules/, .venv/, venv/, __pycache__/, *.pyc/*.pyo, .pytest_cache/,
  .mypy_cache/, .ruff_cache/, .ipynb_checkpoints/, *.sqlite/*.sqlite3/*.db so
  churny non-source files stop being auto-committed (merged into agents on sync,
  which also untracks already-committed matches; agents can negate per-repo).

Observability (.git size curve):
- agent measures `.git` size (du -sb) into sync-state.json.git_dir_bytes and
  returns it via GET /api/git/status.
- new agent_sync_state.git_dir_bytes column (dual migration: SQLite
  agent_sync_state_git_dir_bytes + Alembic 0019); SyncHealthService persists it;
  surfaced on GET /api/agents/sync-health, /fleet/sync-audit, and the per-agent
  sync-state endpoint.

Tests: test_1596_git_sync_observability.py — git_dir_bytes round-trips +
partial-update-preserves; default .gitignore includes the bulk-data set and
keeps the credential/content safety rules. Schema + Alembic parity green.

Deferred follow-ups (documented): opt-in history squash/rotation (destructive,
needs owner consent) + a warn-on-large-file guardrail.

Note: the agent-side maintenance + size measurement need a base-image rebuild to
reach existing agents; the DB/backend surfacing works immediately.

Related to #1596

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
…erns

The regression-diff CI job flagged test_doc_and_constant_in_sync: PR-1 added
13 bulk-data/cache/dep/index patterns to _GITIGNORE_PATTERNS (git_service.py)
but the mirrored .gitignore block in TRINITY_COMPATIBLE_AGENT_GUIDE.md was not
updated. Add the matching entries so the doc-drift guard passes.

Related to #1596

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
@github-actions

Copy link
Copy Markdown

⚠️ Nightly unit-suite check skipped — merge conflict against dev.

Resolve by running git merge dev locally and pushing the result. The next nightly run will re-test once the conflict is gone.

@dolho dolho changed the title fix(git-sync): bound agent .git bloat — maintenance repack + ignore conventions + size observability (#1596, PR-1) fix(git-sync): bound + reclaim agent .git bloat — repack + ignore conventions + observability (PR-1) & destructive history compaction (PR-2) (#1596) Jul 14, 2026
@dolho
dolho force-pushed the feature/1596-git-sync-bloat branch from 500a9dd to c515b02 Compare July 14, 2026 09:15
@dolho dolho changed the title fix(git-sync): bound + reclaim agent .git bloat — repack + ignore conventions + observability (PR-1) & destructive history compaction (PR-2) (#1596) fix(git-sync): bound agent .git bloat — maintenance repack + ignore conventions + size observability (#1596, PR-1) Jul 14, 2026
@dolho

dolho commented Jul 14, 2026

Copy link
Copy Markdown
Contributor Author

Heads-up: I force-pushed this branch back to the non-destructive PR-1 scope and dropped the destructive history-compaction commits I'd added.

Reason: the parallel investigation in #1595 / #1505 identified the true root cause — git maintenance can never complete inside agent containers (detached gc --auto reparents to PID 1 → #817 orphan sweeper SIGKILLs it → stale .lock/gc.pid litter → every later gc aborts). Confirmed in code: resolve_allowlist/_hard_protected_pids protect only agent-server + its parent chain, so an in-container git process is sweep-exposed. The bloat is ~97% unreachable garbage, not committed history — a plain non-destructive gc --prune=now reclaims it (44 GB → 1.6 GB, HEAD unchanged).

So rewriting history was the wrong fix (and unsafe — a long rebase would be SIGKILLed mid-operation by the same sweeper). The load-bearing change (sweeper SIGTERM-then-SIGKILL + git-maintenance allowlist + stale-lock cleanup) belongs in #1595. This PR stays scoped to the two prongs that stand on their own — ignore conventions + observability — plus the non-destructive repack (whose large-repo reliability depends on #1595).

@dolho
dolho requested a review from vybe July 14, 2026 09:30

@vybe vybe left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Validated via /validate-pr: non-destructive self-throttling repack (pack-count gated, in-process child so the #817 sweeper doesn't apply), git_dir_bytes observability wired agent→sync-state→dashboard/audit, dual-track migration correct (SQLite agent_sync_state_git_dir_bytes + Alembic 0019 revising 0018), schema.py/tables.py/architecture.md synced, named regression tests. Security scan clean; CI green. Heads-up applied at merge time: #1550 must re-chain its 0019/0020 Alembic revisions onto this one. ✅

@vybe
vybe merged commit ade7db4 into dev Jul 14, 2026
43 checks passed
vybe pushed a commit that referenced this pull request Jul 14, 2026
…nto 0019 (#1597)

dev's #1597 landed 0019_agent_sync_state_git_dir_bytes (also revising 0018),
colliding with this branch's 0019/0020. Re-chained: pull_claim_lease -> 0020
(down: 0019_agent_sync_state_git_dir_bytes), redelivery_count -> 0021. SQLite
MIGRATIONS list keeps both tracks (git_dir_bytes entry first, matching dev
apply order). Doc references updated.

Co-Authored-By: Claude Fable 5 <[email protected]>
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