Skip to content

feat(sessions): automatic retention policy and cleanup for session metadata on disk #971

Closed

Description

@euxarist

Summary

Zero currently persists session metadata and event logs to disk (~/.local/share/zero/sessions/ on Unix, %LOCALAPPDATA%\zero\sessions\ on Windows), but never prunes or deletes old session directories. Over extended usage, session logs and checkpoint blobs accumulate indefinitely without an automatic retention policy or CLI cleanup mechanism.

Motivation

While in-memory prompt compaction (internal/agent/compaction.go) and tool body pruning (internal/agent/prune.go) manage context token pressure, on-disk storage grows monotonically:

  • Every session generates a separate directory containing metadata.json, events.jsonl, and optional checkpoint blobs.
  • internal/sessions/store.go provides listing and appending, but no cleanup lifecycle or deletion API.
  • Users currently have to manually inspect and delete old session directories.

Proposed Solution

  1. Configurable retention policy: Introduce configuration settings in config.json (e.g. sessions.retention_days or sessions.max_count) to automatically prune stale, non-bookmarked session directories.
  2. CLI cleanup command: Add zero sessions clean (or zero sessions prune --older-than <duration>) to allow users to trigger metadata sweeps manually.
  3. Blob & checkpoint garbage collection: Prune unreferenced rewind blobs when sessions are removed.
  4. Safety guards: Ensure active sessions (holding session.lock) and explicitly saved/named sessions are protected from automated pruning.

Activity

  1. added
    enhancement
    issue-approved
    on Sep 8, 2026
  2. Vasanthdev2004 commented on Sep 8, 2026

    @Vasanthdev2004
    Collaborator

    Approved as opt-in only. Session logs are the user's data and nothing should delete them by default.

    Shape that would be accepted: an explicit zero sessions prune with a dry run that lists what would go and why, a config retention setting that is off unless the user sets it, and a hard rule that the session currently being resumed or forked is never a candidate. A background sweep that runs without being asked is not.

  3. Vasanthdev2004 commented on Sep 12, 2026

    @Vasanthdev2004
    Collaborator

    Back in the pool, and still valid on current main.

    This came up in an audit of open issues whose only linked pull request had closed, which made them look covered to a query while nobody was actually working them. The earlier attempt closed without landing, so this is open for a fresh one.

    I verified the problem still exists rather than assuming it, and the audit verdict was independently re-checked before I posted this. The label is already on, so a PR implementing it can merge on review.

  4. Vasanthdev2004 commented on Sep 26, 2026

    @Vasanthdev2004
    Collaborator

    Notes for whoever picks this up, from reading main at 99721c7. Two of them are traps the approved shape doesn't mention.

    • There is no lifetime lock. session.lock is taken by lockSession around each write and released straight after, so nothing holds it while a session is merely open in a TUI. Skipping sessions whose lock is busy only protects a write in flight, and zero sessions prune in one terminal can't see that another terminal has the session open. The protection has to be something real: a floor on the cutoff, so nothing touched in the last day is ever a candidate, or a lease that the TUI and exec hold for the session's lifetime. The floor is enough for a first version if it's stated in the help text.
    • Deleting a parent breaks its descendants' lineage. Store.Lineage walks ParentSessionID and returns "zero session not found" as soon as an ancestor is missing, so pruning a parent turns every fork or child below it into an error wherever lineage is shown. Either a session that is still the parent of a kept session is not a candidate, or Lineage and Tree stop at a missing ancestor instead of failing. The history itself is safe: Store.Fork copies the parent's events into the fork.
    • Checkpoint blobs live inside the session directory (<session>/checkpoints/blobs), so removing the directory removes them. There is no shared blob store to collect.

    The rest of the approved shape stands: an explicit command, a dry run first, a retention setting that is off unless the user sets it, and nothing that runs on its own.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

enhancementissue-approved

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions