Repository navigation
feat(retention): execution_log + health_check retention sweeps (#772) - #776
Conversation
Bounded growth for schedule_executions (driven by per-run JSONL transcripts
in execution_log, ~150-190KB/row) and agent_health_checks so active fleets
don't hit disk pressure within weeks. Production observation pre-fix:
~3.3GB / ~9k rows on schedule_executions, ~200MB / ~750k rows on
agent_health_checks.
- Two-stage retention on schedule_executions: null execution_log past
execution_log_retention_days (default 30, preserves row + metadata),
DELETE row past execution_row_retention_days (default 90).
- Reuse cleanup_old_records on agent_health_checks
(health_check_retention_days, default 7). Was orphaned; now wired.
- All sweeps chunked (5000 rows/cycle budget) so the first post-deploy
backfill spans hours rather than holding a multi-minute write lock.
- iso_cutoff() cutoffs per Architectural Invariant #16 — prior issue spec
used datetime('now', '-N days') which silently breaks lex comparison
against ISO-Z TEXT columns (#476).
- Partial index idx_executions_completed_terminal on (completed_at)
WHERE status IN ('completed','failed','terminated').
- PRAGMA wal_checkpoint(TRUNCATE) per cycle that reclaims rows.
- New db_vacuum_service.py runs full VACUUM daily at 04:30 UTC
(autocommit connection, accepts BUSY rather than retry).
- Admin-tunable via GET/PUT /api/settings/ops/config — 0 disables a sweep.
Verified on local stack: startup sweep deleted 2820 stale health_check
rows; retention=1 + trigger nulled the test execution_log payload while
preserving row metadata; row-retention=1 deleted 1510 stale terminal
rows in one tick within the budget cap.
Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
8ec569d to
eddeaa2
Compare
Adds steps 7 (retention sweeps) and 8 (WAL checkpoint) to the cleanup cycle in docs/memory/feature-flows/cleanup-service.md, matching the behavior added by #772. Updates the section header from "Seven" to "Nine sequential operations". Related to #772. Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
PR Validation Report (
|
| Category | Status | Notes |
|---|---|---|
| Issue link | ✅ | "Related to #772" — non-closing per new policy (issues close on merge to main) |
| Commit Messages | ✅ | 2 commits, conventional format (feat(retention):, docs(cleanup-service):) |
| Base Branch | ✅ | Targets dev (after retarget — initially missed and rebased onto origin/dev) |
| PR Size | ✅ | 14 files, +850/-32, well under 50 |
| Issue priority | ✅ | P1 / type-feature — appropriate scrutiny applied |
| Requirements | ✅ | New §12.10 RETENTION-001 in docs/memory/requirements.md |
| Architecture | ✅ | Cleanup Service row updated + new DB Vacuum row in Background Services table |
| Feature Flow | ✅ | cleanup-service.md updated with new steps 7 + 8 (after warning surfaced + fixed) |
| Security Check | ✅ | No API keys, real emails, public IPs, .env files, hardcoded secrets, or credential files |
| Code Quality | ✅ | Mirrors audit_retention_service.py for VACUUM job; follows §4/§4b cleanup-tick shape; chunked SQL pattern identical to other prune sites |
| Requirements Trace | ✅ | Issue #772 explicitly referenced; acceptance criteria covered |
| Unit tests | ✅ | 9/9 pass in tests/unit/test_execution_retention_prune.py |
| Live smoke | ✅ | Backend boots; startup tick deleted 2820 stale agent_health_checks rows; PUT + trigger end-to-end works |
Issues Found
Critical: none.
Warnings (resolved during validation):
Targeted— rebased ontomaininstead ofdevorigin/dev, force-pushed, retargeted. Migration conflict resolved (kept dev'sslack_bot_token_encryption+canary_violations_tableplus ours).— added steps 7 (retention sweeps) + 8 (WAL checkpoint), bumped count from "Seven" to "Nine sequential operations".cleanup-service.mdfeature flow not updated
Suggestions (non-blocking):
EXPLAIN QUERY PLANon a real DB showed the planner picked the existingidx_executions_statusover our new partial indexidx_executions_completed_terminal. Not a correctness issue (smoke pruned 1510 rows fast).ANALYZE schedule_executionsmay improve the plan if perf becomes a problem on much larger tables — worth a follow-up issue, not a blocker.
Recommendation
APPROVE — ready for human review + merge to dev.
PR adds bounded growth for schedule_executions (production-observed at 3.3 GB) and agent_health_checks (~750k rows). Two-stage retention (null → delete), per-cycle row budget (5000) caps backfill blast radius, partial index, WAL checkpoint per tick, daily VACUUM service. Admin-tunable via existing /api/settings/ops/config; 0 disables any sweep. All Architectural Invariants respected (esp. #16 iso_cutoff()); reuses three-layer router → service → DB pattern. Reuses orphaned cleanup_old_records() (was dead code; now wired). Security clean, unit tests pass, live smoke confirms end-to-end behavior. Issue #772 stays open per new policy.
🤖 Generated with Claude Code
vybe
left a comment
There was a problem hiding this comment.
LGTM — solid P1 implementation. All architectural invariants respected (iso_cutoff, schema+migration, service lifecycle). Chunked-commit approach is correct; minor doc framing on 'per-cycle budget' is non-blocking.
Summary
schedule_executions: nullexecution_logpastexecution_log_retention_days(default 30) preserves row + metadata; DELETE row pastexecution_row_retention_days(default 90) for deeper retention.agent_health_checkscleanup (previously orphaned) into the 5-min cleanup tick, configurable viahealth_check_retention_days(default 7).iso_cutoff()cutoffs per Architectural Invariant #16 (issue spec'sdatetime('now', '-N days')would have silently broken lex compare on ISO-Z columns, bug(SUB-003): rate-limit events never age out due to SQLite string-compare bug; retries amplify outages #476).idx_executions_completed_terminal+PRAGMA wal_checkpoint(TRUNCATE)after sweeps that reclaim rows.db_vacuum_service.pyruns fullVACUUMdaily at 04:30 UTC (autocommit connection, accepts BUSY rather than retry).GET/PUT /api/settings/ops/config;0disables a sweep.Related to #772.
Production motivation
Pre-fix observation:
schedule_executions~3.3 GB / ~9k rows (driven by 150–190 KB/row JSONL transcripts),agent_health_checks~200 MB / ~750k rows. Growth was unbounded — no TTL, no pruning hook incleanup_service.Test plan
tests/unit/test_execution_retention_prune.py(cutoff correctness, non-terminal not touched, chunked drain,retention_days=0disables, empty-table 0, health-check delete) — all passtests/test_cleanup_service.pyupdated for new report fields + dynamictotalsumDB vacuum scheduler started: daily at 04:30 UTCagent_health_checksrowsGET /api/settings/ops/configPOST /api/monitoring/cleanup-triggerreturns report withexecution_logs_pruned/execution_rows_pruned/health_checks_prunedexecution_log_retention_days=1→ inject old terminal row with 200KB log → trigger → row preserved withexecution_log IS NULLexecution_row_retention_days=1→ trigger → 1510 stale terminal rows deleted in one tick (budget cap held)idx_executions_completed_terminalpresent on real DB after migration/validate-prFiles
services/cleanup_service.pyservices/db_vacuum_service.py(new)db/schedules.pyprune_execution_logs()+prune_execution_rows()— chunked SELECT-id → UPDATE/DELETE-id-indb/monitoring.pycleanup_old_records()upgraded to chunked +iso_cutoff(),chunk_sizeparamdb/schema.py+db/migrations.pyidx_executions_completed_terminal ON (completed_at) WHERE status IN (...)services/settings_service.pydatabase.pymain.pydb_vacuum_service.start()/.stop()into lifespandocs/memory/architecture.md+requirements.md🤖 Generated with Claude Code