Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 18 additions & 18 deletions lib/cli/src/crewai_cli/checkpoint_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@ def _record_checkpoint_usage(
)
"""

_COUNT_CHECKPOINTS = "SELECT COUNT(*) FROM checkpoints"

_SELECT_LIKE = """
SELECT id, created_at, json(data)
Expand Down Expand Up @@ -646,7 +645,9 @@ def _parse_duration(value: str) -> timedelta:
return timedelta(minutes=amount)


def _prune_json(location: str, keep: int | None, older_than: timedelta | None) -> int:
def _prune_json(
location: str, keep: int | None, older_than: timedelta | None, dry_run: bool = False
) -> int:
pattern: str = os.path.join(glob.escape(location), "**", "*.json")
files: list[str] = sorted(
glob.glob(pattern, recursive=True), key=os.path.getmtime, reverse=True
Expand All @@ -668,6 +669,9 @@ def _prune_json(location: str, keep: int | None, older_than: timedelta | None) -
if mtime < cutoff:
to_delete.add(path)

if dry_run:
return len(to_delete)

deleted: int = 0
for path in to_delete:
try:
Expand All @@ -686,7 +690,12 @@ def _prune_json(location: str, keep: int | None, older_than: timedelta | None) -
return deleted


def _prune_sqlite(db_path: str, keep: int | None, older_than: timedelta | None) -> int:
def _prune_sqlite(
db_path: str,
keep: int | None,
older_than: timedelta | None,
dry_run: bool = False,
) -> int:
deleted: int = 0
with sqlite3.connect(db_path) as conn:
if older_than is not None:
Expand All @@ -700,7 +709,8 @@ def _prune_sqlite(db_path: str, keep: int | None, older_than: timedelta | None)
cursor = conn.execute(_DELETE_KEEP_N, (keep,))
deleted += cursor.rowcount

conn.commit()
if not dry_run:
conn.commit()
Comment on lines +712 to +713

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🔵 LOW: Prune dry run still commits its DELETEs through the connection context manager

TL;DR: crewai checkpoint prune --dry-run really deletes rows from the SQLite checkpoint store, including checkpoints from other branches and flows. It runs the same DELETE statements as a real prune and relies on the missing conn.commit() to keep them uncommitted, but with sqlite3.connect(...) commits the open transaction when the block exits, so the preview persists. Rolling back when dry_run is set keeps the preview read-only.

Cause, impact and fix

Cause: _prune_sqlite executes the _DELETE_OLDER_THAN and _DELETE_KEEP_N statements unconditionally and guards only the explicit conn.commit(). The with sqlite3.connect(db_path) as conn block commits any pending transaction on normal exit, so withholding the commit call does not stop the deletes from being written.

Impact: An operator running --dry-run to preview a cleanup permanently loses the oldest checkpoints in that store, not only the rows they meant to drop, and is told those rows only "would" be pruned. The DELETEs carry no branch or flow predicate, so the loss spans every branch sharing the default ./.checkpoints.db.

Fix: Make the dry-run path explicitly non-destructive: roll back the transaction when dry_run is true and commit only otherwise, or count the matching rows with SELECT COUNT(*) and skip the DELETEs entirely. Add a test that runs a dry run against a populated store and asserts the row count is unchanged.

🔵 `crewai checkpoint prune --dry-run` still deletes SQLite checkpoints: `with sqlite3.connect(...)` commits on exit

🔵 crewai checkpoint prune --dry-run still deletes SQLite checkpoints: with sqlite3.connect(...) commits on exit

Severity: LOW

TL;DR: _prune_sqlite in lib/cli/src/crewai_cli/checkpoint_cli.py only skips conn.commit() for --dry-run, but the with sqlite3.connect(...) block commits the pending DELETEs on exit, so the "preview" deletes them.

Impact: Anyone running crewai checkpoint prune --keep N --dry-run to preview a cleanup silently loses the oldest N checkpoints in that store - including rows belonging to other branches and flows - and is told only how many "would" be removed.

Root cause:

  • The PR implements dry-run by withholding the explicit conn.commit(), assuming that no commit call means no write, but Python's connection context manager commits any open transaction when the with block exits normally.
  • with sqlite3.connect(db_path) as conn therefore persists the DELETE statements executed at lines 705 and 709 even when dry_run is true; reproduced with the same code shape: 10 rows, dry run reported 7 deleted and 3 rows remained.
  • The statements carry no branch predicate, so the preview-time deletion spans every branch and crew sharing the default ./.checkpoints.db, not just the rows the operator intended to prune.
Prompt to fix with AI
This is a security vulnerability found during a code review.

Vulnerability: `crewai checkpoint prune --dry-run` still deletes SQLite checkpoints: `with sqlite3.connect(...)` commits on exit
Severity: LOW
CWE: CWE-693

**TL;DR:** `_prune_sqlite` in `lib/cli/src/crewai_cli/checkpoint_cli.py` only skips `conn.commit()` for `--dry-run`, but the `with sqlite3.connect(...)` block commits the pending DELETEs on exit, so the "preview" deletes them.

**Impact:** Anyone running `crewai checkpoint prune --keep N --dry-run` to preview a cleanup silently loses the oldest N checkpoints in that store - including rows belonging to other branches and flows - and is told only how many "would" be removed.

**Root cause:**
- The PR implements dry-run by withholding the explicit `conn.commit()`, assuming that no commit call means no write, but Python's connection context manager commits any open transaction when the `with` block exits normally.
- `with sqlite3.connect(db_path) as conn` therefore persists the `DELETE` statements executed at lines 705 and 709 even when `dry_run` is true; reproduced with the same code shape: 10 rows, dry run reported 7 deleted and 3 rows remained.
- The statements carry no branch predicate, so the preview-time deletion spans every branch and crew sharing the default `./.checkpoints.db`, not just the rows the operator intended to prune.

Location: lib/cli/src/crewai_cli/checkpoint_cli.py:712-713
Context: dry-run branch that leaves the transaction open for the context manager to commit
```
// Before:
        if not dry_run:
            conn.commit()
// After:
        if dry_run:
            conn.rollback()
        else:
            conn.commit()
```

Location: lib/cli/src/crewai_cli/checkpoint_cli.py:700-700
Context: connection context manager that commits the pending DELETEs on exit
```
    with sqlite3.connect(db_path) as conn:
```

How to fix:
Make the non-destructive path explicitly non-destructive: when `dry_run` is true, roll the transaction back (or count the matching rows with `SELECT COUNT(*)` instead of executing the DELETEs) rather than relying on the absence of an explicit commit. Add a regression test that runs a dry run against a populated SQLite store and asserts the row count is unchanged.

Please fix this vulnerability. If you propose a fix, make it concise and minimal.

React 👍 / 👎 to tune Strix for this repo. A repo collaborator (or the PR author) can resolve this thread to dismiss these findings.

return deleted


Expand All @@ -716,21 +726,11 @@ def prune_checkpoints(

deleted: int
if _is_sqlite(location):
if dry_run:
with sqlite3.connect(location) as conn:
total: int = conn.execute(_COUNT_CHECKPOINTS).fetchone()[0]
click.echo(f"Would prune from {total} checkpoint(s) in {location}")
return
deleted = _prune_sqlite(location, keep, duration)
deleted = _prune_sqlite(location, keep, duration, dry_run=dry_run)
elif os.path.isdir(location):
if dry_run:
files: list[str] = glob.glob(
os.path.join(glob.escape(location), "**", "*.json"), recursive=True
)
click.echo(f"Would prune from {len(files)} checkpoint(s) in {location}")
return
deleted = _prune_json(location, keep, duration)
deleted = _prune_json(location, keep, duration, dry_run=dry_run)
else:
click.echo(f"Not a directory or SQLite database: {location}")
return
click.echo(f"Pruned {deleted} checkpoint(s) from {location}")
verb = "Would prune" if dry_run else "Pruned"
click.echo(f"{verb} {deleted} checkpoint(s) from {location}")