Skip to content

Bug: clean_old_caches(keep_count=0) retains every pricing cache #6235

Open

Description

@elysiopires

Browser Use Version

main at 0.13.14, Python 3.12 on macOS.

Bug Description and Reproduction

TokenCost.clean_old_caches(keep_count=0) leaves every pricing cache file in place. The final removal loop uses own_files[:-keep_count]; for zero that becomes own_files[:0], so it iterates over no files. Asking to keep zero caches therefore keeps all of them.

Reproduce without a browser, model, or network request:

import asyncio
import tempfile
from datetime import datetime
from pathlib import Path
from browser_use.tokens.service import TokenCost
from browser_use.tokens.views import CachedPricingData

async def main():
    with tempfile.TemporaryDirectory() as directory:
        cost = TokenCost(pricing_url="https://example.com/pricing.json")
        cost._cache_dir = Path(directory)
        path = cost._cache_dir / "pricing_test.json"
        path.write_text(CachedPricingData(
            timestamp=datetime.now(), source_url=cost.pricing_url, data={}
        ).model_dump_json())
        await cost.clean_old_caches(keep_count=0)
        assert not path.exists()

asyncio.run(main())

Expected: zero matching-source cache files remain. Actual: the assertion fails.

I have a focused fix that treats zero as removal of all matching-source caches, with regression coverage for retention counts 0, 1, and 3. The tests verify that another pricing source's cache and malformed JSON are untouched. Before the fix, zero fails and the two positive counts pass. Afterward, those cases plus the existing token-cost suite pass (8 tests), and all pre-commit hooks pass.

Duplicate checks: searched issues for clean_old_caches and open PRs for cache/keep_count and pricing cache cleanup; no matching fix found. Prepared with Codex assistance and verified locally.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions