Skip to content

fix(tokens): handle keep_count=0 in clean_old_caches to remove all matching caches - #6239

Open
user-Param wants to merge 2 commits into
browser-use:mainfrom
user-Param:fix/6235-clean-caches-zero-keep
Open

user-Param wants to merge 2 commits into
browser-use:mainfrom
user-Param:fix/6235-clean-caches-zero-keep

Conversation

@user-Param

@user-Param user-Param commented Oct 30, 2026 •

Copy link
Copy Markdown

Fixes #6235 - when is passed to , it should remove all matching cache files. Previously resulted in an empty slice, keeping all files. The fix treats as 'remove all'.


Summary by cubic

Fixes #6235 so passing keep_count=0 to clean_old_caches removes all matching cache files. Previously, keep_count=0 produced an empty slice, which kept every cache file instead of deleting them.

  • Adds 4 tests covering keep_count values of 0, 1, N, and values larger than the file count.

Written for commit 83e08e9. Summary will update on new commits.

View guided diff

@CLAassistant

CLAassistant commented Oct 30, 2026 •

Copy link
Copy Markdown

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.


def-Param seems not to be a GitHub user. You need a GitHub account to be able to sign the CLA. If you have already a GitHub account, please add the email address used for this commit to your account.
You have signed the CLA already but the status is still pending? Let us recheck it.

@cubic-dev-ai cubic-dev-ai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

1 issue found across 1 file

Prompt for AI agents (unresolved issues)

Check if these issues are valid — if so, understand the root cause of each and fix them. When an issue isn't valid or won't be fixed in this PR, reply in its thread with the reason and then resolve the thread. If appropriate, use sub-agents to investigate and fix each issue separately.


<file name="browser_use/tokens/service.py">

<violation number="1" location="browser_use/tokens/service.py:638">
P2: `<= 0` also treats accidental negative `keep_count` values as “delete every cache,” expanding the destructive behavior beyond the requested `0` case. Use `== 0` here, or reject negative counts before cleanup, so a negative retention value cannot wipe all matching caches.</violation>
</file>

Heads up: you’re close to your flex budget. Increase your flex budget so reviews don’t pause.

View guided diff | Re-trigger cubic


# Remove all but the most recent files
for cache_file in own_files[:-keep_count]:
if keep_count <= 0:

@cubic-dev-ai cubic-dev-ai Bot Oct 30, 2026 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

P2: <= 0 also treats accidental negative keep_count values as “delete every cache,” expanding the destructive behavior beyond the requested 0 case. Use == 0 here, or reject negative counts before cleanup, so a negative retention value cannot wipe all matching caches.

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. When an issue isn't valid or won't be fixed in this PR, reply in its thread with the reason and then resolve the thread. At browser_use/tokens/service.py, line 638:

<comment>`<= 0` also treats accidental negative `keep_count` values as “delete every cache,” expanding the destructive behavior beyond the requested `0` case. Use `== 0` here, or reject negative counts before cleanup, so a negative retention value cannot wipe all matching caches.</comment>

<file context>
@@ -635,7 +635,11 @@ async def clean_old_caches(self, keep_count: int = 3) -> None:
 
 			# Remove all but the most recent files
-			for cache_file in own_files[:-keep_count]:
+			if keep_count <= 0:
+				files_to_remove = own_files
+			else:
</file context>
Suggested change
if keep_count <= 0:
if keep_count == 0:
Fix with cubic

@mintlify

mintlify Bot commented Oct 30, 2026 •

Copy link
Copy Markdown

Preview approval required

This pull request comes from a fork, so Mintlify won't build a preview until someone with write access approves commit 83e08e9.

Approve preview

@user-Param

Copy link
Copy Markdown
Author

@gregpr07 Could you please review this PR? All tests pass and the fix addresses a real issue. Thank you!

@cubic-dev-ai cubic-dev-ai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

1 issue found across 1 file (changes from recent commits).

Prompt for AI agents (unresolved issues)

Check if these issues are valid — if so, understand the root cause of each and fix them. When an issue isn't valid or won't be fixed in this PR, reply in its thread with the reason and then resolve the thread. If appropriate, use sub-agents to investigate and fix each issue separately.


<file name="browser_use/tokens/tests/test_clean_old_caches.py">

<violation number="1" location="browser_use/tokens/tests/test_clean_old_caches.py:41">
P2: These tests never verify the “matching” part of the contract, so they would pass if `keep_count=0` deleted caches belonging to another pricing URL. Add a differently sourced cache and assert that it remains after cleanup.</violation>
</file>

Heads up: you’re close to your flex budget. Increase your flex budget so reviews don’t pause.

View guided diff | Re-trigger cubic

tc = token_cost_with_cache
cache_dir = tc._cache_dir

files = [_make_cache_file(cache_dir, i) for i in range(5)]

@cubic-dev-ai cubic-dev-ai Bot Nov 1, 2026 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

P2: These tests never verify the “matching” part of the contract, so they would pass if keep_count=0 deleted caches belonging to another pricing URL. Add a differently sourced cache and assert that it remains after cleanup.

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. When an issue isn't valid or won't be fixed in this PR, reply in its thread with the reason and then resolve the thread. At browser_use/tokens/tests/test_clean_old_caches.py, line 41:

<comment>These tests never verify the “matching” part of the contract, so they would pass if `keep_count=0` deleted caches belonging to another pricing URL. Add a differently sourced cache and assert that it remains after cleanup.</comment>

<file context>
@@ -0,0 +1,87 @@
+	tc = token_cost_with_cache
+	cache_dir = tc._cache_dir
+
+	files = [_make_cache_file(cache_dir, i) for i in range(5)]
+	assert all(f.exists() for f in files)
+
</file context>
Fix with cubic

…ntics (browser-use#6235)

Adds 4 tests covering:
- keep_count=0 removes all matching cache files
- keep_count=N keeps only the N most recent files
- keep_count > file count removes nothing
- keep_count=1 keeps only the single most recent file

Addresses review feedback from r266-tech.
@user-Param
user-Param force-pushed the fix/6235-clean-caches-zero-keep branch from 45d3bcb to 83e08e9 Compare November 1, 2026 22:04

This branch has not been deployed

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

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

2 participants