Skip to content

Auto-prune stale state on startup#20

Merged
jackparnell merged 1 commit intomainfrom
feature/stale-state-cleanup
Apr 7, 2026
Merged

Auto-prune stale state on startup#20
jackparnell merged 1 commit intomainfrom
feature/stale-state-cleanup

Conversation

@ColonistOne
Copy link
Copy Markdown
Collaborator

Summary

When an agent is stopped for a week or more and restarted, it now automatically cleans up stale entries from its state file on load. Previously the state would accumulate thousands of old post IDs that would never be seen again, wasting memory and slowing down lookups.

How it works

  • On load, checks last_heartbeat timestamp
  • If the agent has been offline for 7+ days, triggers a prune
  • Prune age matches the offline duration (not a fixed 30 days)
  • Logs the cleanup: "Agent was offline for 14 days — pruned 847 stale entries (23 remaining)."
  • Recent entries are preserved, only truly stale ones are removed

No change for active agents

If last_heartbeat is within the last 7 days, no auto-prune happens. The existing monthly prune in the heartbeat loop continues to handle gradual cleanup.

Test plan

  • Long offline period (14 days) → stale entries pruned, recent kept
  • Recently active (2 days) → no auto-prune
  • Fresh state (no heartbeat) → no crash
  • total_tracked property correct
  • 159 tests pass, ruff + mypy clean

🤖 Generated with Claude Code

If the agent has been offline for 7+ days, it now prunes old
entries from seen_posts, commented_on, voted_on, and
replied_comments on load. Previously this only happened during
the monthly prune in the heartbeat, so an agent stopped for
weeks would restart carrying thousands of stale post IDs.

- New _prune_if_stale() called during _load()
- Threshold: 7 days offline triggers cleanup
- Prune age matches offline duration (not fixed 30 days)
- New total_tracked property for diagnostics
- Logs count of removed entries on startup

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@jackparnell jackparnell merged commit b50780e into main Apr 7, 2026
4 checks passed
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