Skip to content

Framework for orchestrating role-playing, autonomous AI agents. By fostering collaborative intelligence, CrewAI empowers agents to work together seamlessly, tackling complex tasks.

fix: treat checkpoint paths literally in glob patterns (#8269)

Yingzi Zhang4f4573706ea4
diff --git a/lib/cli/src/crewai_cli/checkpoint_cli.py b/lib/cli/src/crewai_cli/checkpoint_cli.py
index 16a6a3056..a9ce23dec 100644
--- a/lib/cli/src/crewai_cli/checkpoint_cli.py
+++ b/lib/cli/src/crewai_cli/checkpoint_cli.py
@@ -239,7 +239,7 @@ def _entity_summary(entities: list[dict[str, Any]]) -> str:
def _list_json(location: str) -> list[dict[str, Any]]:
- pattern = os.path.join(location, "**", "*.json")
+ pattern = os.path.join(glob.escape(location), "**", "*.json")
results = []
for path in sorted(
glob.glob(pattern, recursive=True), key=os.path.getmtime, reverse=True
@@ -260,7 +260,7 @@ def _list_json(location: str) -> list[dict[str, Any]]:
def _info_json_latest(location: str) -> dict[str, Any] | None:
- pattern = os.path.join(location, "**", "*.json")
+ pattern = os.path.join(glob.escape(location), "**", "*.json")
files = sorted(
glob.glob(pattern, recursive=True), key=os.path.getmtime, reverse=True
)
@@ -456,7 +456,7 @@ def _resolve_checkpoint(
from crewai.state.provider.json_provider import JsonProvider
_json_provider: JsonProvider = JsonProvider()
- pattern: str = os.path.join(location, "**", "*.json")
+ pattern: str = os.path.join(glob.escape(location), "**", "*.json")
all_files: list[str] = glob.glob(pattern, recursive=True)
matches: list[str] = [
f for f in all_files if checkpoint_id in _json_provider.extract_id(f)
@@ -647,7 +647,7 @@ def _parse_duration(value: str) -> timedelta:
def _prune_json(location: str, keep: int | None, older_than: timedelta | None) -> int:
- pattern: str = os.path.join(location, "**", "*.json")
+ pattern: str = os.path.join(glob.escape(location), "**", "*.json")
files: list[str] = sorted(
glob.glob(pattern, recursive=True), key=os.path.getmtime, reverse=True
)
@@ -725,7 +725,7 @@ def prune_checkpoints(
elif os.path.isdir(location):
if dry_run:
files: list[str] = glob.glob(
- os.path.join(location, "**", "*.json"), recursive=True
+ os.path.join(glob.escape(location), "**", "*.json"), recursive=True
)
click.echo(f"Would prune from {len(files)} checkpoint(s) in {location}")
return
diff --git a/lib/crewai/src/crewai/state/provider/json_provider.py b/lib/crewai/src/crewai/state/provider/json_provider.py
index 93556f0e3..c81851fcb 100644
--- a/lib/crewai/src/crewai/state/provider/json_provider.py
+++ b/lib/crewai/src/crewai/state/provider/json_provider.py
@@ -99,7 +99,7 @@ class JsonProvider(BaseProvider):
"""Remove oldest checkpoint files beyond *max_keep* on a branch."""
_safe_branch(location, branch)
branch_dir = os.path.join(location, branch)
- pattern = os.path.join(branch_dir, "*.json")
+ pattern = os.path.join(glob.escape(branch_dir), "*.json")
files = sorted(glob.glob(pattern), key=os.path.getmtime)
removed = 0
for path in files if max_keep == 0 else files[:-max_keep]:
diff --git a/lib/crewai/tests/test_checkpoint.py b/lib/crewai/tests/test_checkpoint.py
index 8bd43e3e7..dae9768c9 100644
--- a/lib/crewai/tests/test_checkpoint.py
+++ b/lib/crewai/tests/test_checkpoint.py
@@ -120,6 +120,34 @@ class TestFindCheckpoint:
class TestPrune:
+ @pytest.mark.parametrize(
+ ("directory", "branch"),
+ [("checkpoints[1]", "main"), ("checkpoints", "run[1]"), ("checkpoints", "fork/run[1]")],
+ )
+ @pytest.mark.parametrize("max_keep", [0, 1])
+ def test_prune_treats_directory_and_branch_as_literal_paths(
+ self, tmp_path: Path, directory: str, branch: str, max_keep: int
+ ) -> None:
+ provider = JsonProvider()
+ location = str(tmp_path / directory)
+ paths = []
+ for i in range(2):
+ path = Path(provider.checkpoint("{}", location, branch=branch))
+ os.utime(path, (i + 1, i + 1))
+ paths.append(path)
+ # As a glob pattern, "[1]" matches "1", so this path must not be pruned
+ other_path = Path(
+ provider.checkpoint(
+ "{}",
+ str(tmp_path / directory.replace("[1]", "1")),
+ branch=branch.replace("[1]", "1"),
+ )
+ )
+
+ assert provider.prune(location, max_keep=max_keep, branch=branch) == 2 - max_keep
+ assert [path for path in paths if path.exists()] == (paths[-1:] if max_keep else [])
+ assert other_path.exists()
+
def test_prune_keeps_newest(self) -> None:
with tempfile.TemporaryDirectory() as d:
branch_dir = os.path.join(d, "main")
diff --git a/lib/crewai/tests/test_checkpoint_cli.py b/lib/crewai/tests/test_checkpoint_cli.py
index 5007ba557..764816362 100644
--- a/lib/crewai/tests/test_checkpoint_cli.py
+++ b/lib/crewai/tests/test_checkpoint_cli.py
@@ -169,6 +169,24 @@ class TestResolveCheckpoint:
_write_json_checkpoint(d)
assert _resolve_checkpoint(d, "nonexistent") is None
+ def test_json_location_with_glob_characters(self) -> None:
+ with tempfile.TemporaryDirectory() as d:
+ location = os.path.join(d, "runs[1]")
+ path = _write_json_checkpoint(
+ location, name="20260101T000000_aaaa1111_p-none.json"
+ )
+ # As a glob pattern, "runs[1]" matches this directory instead
+ _write_json_checkpoint(
+ os.path.join(d, "runs1"), name="20260102T000000_bbbb2222_p-none.json"
+ )
+ assert [item["path"] for item in _list_json(location)] == [path]
+ meta = _resolve_checkpoint(location, None)
+ assert meta is not None
+ assert meta["path"] == path
+ meta = _resolve_checkpoint(location, "aaaa1111")
+ assert meta is not None
+ assert meta["path"] == path
+
def test_sqlite_latest(self) -> None:
with tempfile.TemporaryDirectory() as d:
db_path = os.path.join(d, "test.db")
@@ -355,6 +373,28 @@ class TestPruneJson:
_prune_json(d, keep=None, older_than=timedelta(days=1))
assert not os.path.exists(os.path.join(d, "feature"))
+ def test_location_with_glob_characters(
+ self, capsys: pytest.CaptureFixture[str]
+ ) -> None:
+ with tempfile.TemporaryDirectory() as d:
+ location = os.path.join(d, "runs[1]")
+ path = _write_json_checkpoint(
+ location, name="20260101T000000_aaaa1111_p-none.json"
+ )
+ # As a glob pattern, "runs[1]" matches this directory instead
+ others = [
+ _write_json_checkpoint(
+ os.path.join(d, "runs1"),
+ name=f"2026010{i + 2}T000000_bbbb{i}222_p-none.json",
+ )
+ for i in range(2)
+ ]
+ prune_checkpoints(location, keep=0, older_than=None, dry_run=True)
+ assert "Would prune from 1 checkpoint(s)" in capsys.readouterr().out
+ assert _prune_json(location, keep=0, older_than=None) == 1
+ assert not os.path.exists(path)
+ assert all(os.path.exists(other) for other in others)
+
class TestPruneSqlite:
def test_keep_n(self) -> None: