Skip to content

Clean stale session files#438

Open
toothbrush wants to merge 8 commits intomainfrom
20260220-clean-stale-entire-sessions-json
Open

Clean stale session files#438
toothbrush wants to merge 8 commits intomainfrom
20260220-clean-stale-entire-sessions-json

Conversation

@toothbrush
Copy link
Contributor

@toothbrush toothbrush commented Feb 20, 2026

Summary

Session state files (.git/entire-sessions/) accumulate over time. Sessions that ended more than 24 hours ago are no longer useful. This PR transparently deletes them during load and list operations so stale sessions are invisible to all callers.

As a side effect, this should fix #437 — we already aren't creating unnecessarily huge session files anymore (thanks to #426) but users with polluted .git/entire-sessions/ folders would still suffer from the issue. This will clean things up on an ongoing basis.

What changed

  • State.IsStale() — new method on the State struct centralising the staleness check (ended > 24h ago; active sessions are never stale)
  • StateStore.Load() — now checks IsStale() after loading; stale sessions are deleted from disk and (nil, nil) is returned. This is the core fix: ManualCommitStrategy.loadSessionState() calls StateStore.Load() directly, so hooks, condensation, rewind, and git operations now all benefit from stale session cleanup automatically.
  • StateStore.List() — removed the duplicate stale-session check; Load() already handles it, so List() simply skips nil returns.
  • strategy.LoadSessionState() — replaced duplicated file I/O, unmarshalling, normalisation, and stale-checking logic with a single call to session.NewStateStore() + store.Load(). This centralises all session loading through StateStore, eliminating the parallel code path that existed in the strategy package.

The net effect is that all session loading/listing goes through StateStore and every caller — whether it's the package-level LoadSessionState(), ManualCommitStrategy.loadSessionState(), or StateStore.List() — gets consistent stale-session cleanup.

Test plan

  • New TestStateStore_Load_DeletesStaleSession — verifies Load() returns nil and deletes the file for stale sessions, while still returning active sessions normally
  • Existing TestStateStore_List_DeletesStaleSession — continues to pass (List delegates to Load)
  • Existing TestLoadSessionState_DeletesStaleSession — continues to pass via the simplified delegation
  • Existing TestState_IsStale — unit tests for the staleness predicate
  • Full strategy test suite passes

🤖 Generated with Claude Code

toothbrush and others added 2 commits February 20, 2026 13:26
Session state files (.git/entire-sessions/) accumulate over time. Sessions
that ended more than 24 hours ago are no longer useful. This transparently
deletes them during StateStore.List() and LoadSessionState() so stale
sessions are invisible to all callers.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Entire-Checkpoint: 126847879a7e
Copilot AI review requested due to automatic review settings February 20, 2026 04:06
@toothbrush toothbrush requested a review from a team as a code owner February 20, 2026 04:06
@cursor
Copy link

cursor bot commented Feb 20, 2026

PR Summary

Low Risk
Scoped to session-state housekeeping with best-effort deletion and added tests; main risk is inadvertently removing sessions if EndedAt is set incorrectly.

Overview
Ended session state files older than 24h are now treated as stale and automatically deleted during StateStore.List() and LoadSessionState(), making them effectively invisible to callers.

This introduces StaleSessionThreshold and State.IsStale(), adds debug logging for best-effort cleanup, and includes tests verifying staleness detection plus on-disk deletion behavior for both list and load paths.

Written by Cursor Bugbot for commit 37ea966. Configure here.

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR adds automatic cleanup of stale session state files under .git/entire-sessions/ so that ended sessions older than a threshold are deleted and become invisible to callers, addressing long-term accumulation and improving hook performance (Issue #437).

Changes:

  • Add IsStale() logic and a StaleSessionThreshold constant to determine when sessions should be deleted.
  • Delete stale session state files during StateStore.List() and strategy.LoadSessionState().
  • Add unit tests covering staleness detection and deletion behavior during load/list.

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 5 comments.

File Description
cmd/entire/cli/session/state.go Introduces staleness threshold + deletion of stale session files during StateStore.List()
cmd/entire/cli/session/state_test.go Adds tests for State.IsStale() and stale deletion during StateStore.List()
cmd/entire/cli/strategy/session_state.go Deletes stale session state files during LoadSessionState()
cmd/entire/cli/strategy/session_state_test.go Adds test ensuring stale session files are deleted on LoadSessionState()

toothbrush and others added 5 commits February 20, 2026 13:41
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
This reverts commit 738a41c.
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
@toothbrush
Copy link
Contributor Author

bugbot run

Copy link

@cursor cursor bot left a comment

Choose a reason for hiding this comment

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

✅ Bugbot reviewed your changes and found no new issues!

Comment @cursor review or bugbot run to trigger another review on this PR

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 4 out of 4 changed files in this pull request and generated 1 comment.

Previously, stale session detection was inconsistent: the package-level
LoadSessionState() in strategy/ had its own stale check + file deletion,
and StateStore.List() had a separate stale check, but StateStore.Load()
did not. This meant ManualCommitStrategy.loadSessionState() — which calls
StateStore.Load() directly and is used extensively in hooks, condensation,
rewind, and git operations — never cleaned up stale sessions.

Fix by adding the stale session check to StateStore.Load() itself, so all
callers automatically benefit. Then:

- Remove the duplicate stale check from StateStore.List() (it calls Load,
  which now handles it)
- Simplify strategy.LoadSessionState() to delegate to StateStore instead
  of duplicating file I/O, unmarshaling, normalization, and stale checks

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Entire-Checkpoint: aebb716f655b
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

Git hooks are slow when .git/entire-sessions fills up

1 participant

Comments