Skip to content

[FileStore]: fix Use caching on demand to reduce memory usage - #1850

Closed
CLFutureX wants to merge 13 commits into
OpenHands:mainfrom
CLFutureX:feat_cache
Closed

CLFutureX wants to merge 13 commits into
OpenHands:mainfrom
CLFutureX:feat_cache

Conversation

@CLFutureX

@CLFutureX CLFutureX commented Jan 29, 2026 •

Copy link
Copy Markdown
Contributor

Background:

Some FileStore usage scenarios actually don’t need caching.For example, _save_base_state() and _save_processing_history() cause extra memory usage.

Change:

Add direct write and direct read methods to enable on-demand caching.

Signed-off-by: CLFutureX <chenyongqyl@163.com>

@all-hands-bot all-hands-bot left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Code Review Summary

Found several issues with the caching implementation that need to be addressed:

🔴 Critical Issues

1. Cache behavior doesn't respect cache=False in LocalFileStore.read() (line 76-80)

The cache is still consulted even when cache=False. Current flow:

  1. Check cache → return if found (ignores cache param)
  2. Read from disk
  3. Only cache if cache=True

This means if a file was previously cached, it will return stale data even when caching is disabled.

Fix: Move the cache check inside the conditional:

if cache and full_path in self.cache:
    return self.cache[full_path]

🟠 Important Issues

2. Breaking API change (LocalFileStore line 62)

The previous behavior was to always cache (implicit cache=True). Now the default is cache=False, which could cause performance regressions for existing code that hasn't been updated.

Options:

  • Change default to cache=True to maintain backward compatibility
  • Document this as a breaking change and audit all call sites
  • Verify there are no other callers besides EventStore

3. Missing documentation (base.py lines 17, 26)

The cache parameter needs docstring explaining:

  • When to use True vs False
  • That True caches in memory for faster subsequent reads
  • Use cases: True for frequently-read data (events), False for one-time reads (state snapshots)

🟡 Suggestions

4. Inconsistent default values

  • LocalFileStore: defaults to cache=False
  • MemoryFileStore: defaults to cache=True

This inconsistency could cause confusion. Consider standardizing.

5. MemoryFileStore ignores the cache parameter (lines 26, 31)

The parameter is added but not used since MemoryFileStore stores everything in memory by nature. Consider adding a comment explaining why it's ignored.

6. Verify the change achieves the stated goal

The PR description mentions reducing memory usage in _save_base_state() and _save_processing_history(). However, EventStore now passes cache=True for all writes. If those methods write events, this change won't help. Verify:

  • Do those methods use EventStore or FileStore directly?
  • Should event writes default to cache=False and only cache on read?

Recommendations

  1. Must fix: Issue #1 (critical cache bug)
  2. Should fix: Issues #2 and #3 (breaking change and docs)
  3. Nice to have: Issues #4, #5, #6 (consistency and verification)

Signed-off-by: CLFutureX <chenyongqyl@163.com>
Signed-off-by: CLFutureX <chenyongqyl@163.com>
This reverts commit b17bb5a.
Signed-off-by: CLFutureX <chenyongqyl@163.com>
history_file = f"{get_usermodeling_dir(self.user_id)}/processed_sessions_timestamps.json" # noqa: E501

self.file_store.write(history_file, json.dumps(history, indent=2))
self.file_store.directWrite(history_file, json.dumps(history, indent=2))

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Sorry I'm dense, if read/write get cache argument, do we need directRead/directWrite, isn't it with cache false?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Sorry I'm dense, if read/write get cache argument, do we need directRead/directWrite, isn't it with cache false?

Yes, i initially planned to add the cache parameter. However, i found it would result in redundant parameters in InMemoryFileStore and cause validation failures. Also, to make the interface clearer and easier to use, i decided to add new methods instead.

Signed-off-by: CLFutureX <chenyongqyl@163.com>
@all-hands-bot

Copy link
Copy Markdown
Collaborator

[Automatic Post]: It has been a while since there was any activity on this PR. @CLFutureX, are you still working on it? If so, please go ahead, if not then please request review, close it, or request that someone else follow up.

1 similar comment
@all-hands-bot

Copy link
Copy Markdown
Collaborator

[Automatic Post]: It has been a while since there was any activity on this PR. @CLFutureX, are you still working on it? If so, please go ahead, if not then please request review, close it, or request that someone else follow up.

@neubig
neubig self-requested a review February 12, 2026 00:18

neubig commented Feb 12, 2026

Copy link
Copy Markdown
Member

[automated message] @neubig assigned for review according to git blame

@all-hands-bot

Copy link
Copy Markdown
Collaborator

[Automatic Post]: This PR seems to be currently waiting for review. @neubig, could you please take a look when you have a chance?

@neubig neubig left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Hi @CLFutureX , thank you for opening the PR. Before I fully assess the PR, could you please open an issue with a minimal reproducible example of the problem that you're trying to solve? For instance, a command line argument or script that runs the agent where you monitor the agent's memory and find that it is very large. You could then run the same command with this fix implemented and demonstrate that it significantly reduces memory.

That would help me (1) understand the scope of the problem, and (2) be able to confirm the error on my end as well.

Thank you! And once you are done with that, please click the "re-request review" button next to my name on the reviewers panel.

@CLFutureX

Copy link
Copy Markdown
Contributor Author

Hi @CLFutureX , thank you for opening the PR. Before I fully assess the PR, could you please open an issue with a minimal reproducible example of the problem that you're trying to solve? For instance, a command line argument or script that runs the agent where you monitor the agent's memory and find that it is very large. You could then run the same command with this fix implemented and demonstrate that it significantly reduces memory.

That would help me (1) understand the scope of the problem, and (2) be able to confirm the error on my end as well.

Thank you! And once you are done with that, please click the "re-request review" button next to my name on the reviewers panel.

Thanks for the review. The reason I opened this PR is exactly as described.
Right now, all read and write operations in LocalFileStore use caching by default. I originally added this cache layer to avoid reading from the local file on every event iteration.
However, LocalFileStore is not only used for event storage — it’s also used for other things like reading and writing base_state. In these scenarios, we only need to write to the file and read it once, so caching is unnecessary.
That’s why this PR adds a flag to control whether caching is enabled for read/write operations.

Also, @enyst please take a look at the latest changes: I think adding a parameter is indeed better than adding a new method.

@neubig neubig left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Hi @CLFutureX , we still need a minimal reproducible example for this bug so we can confirm the problem and that it is fixed after the PR. Thank you! If you are able to provide this we can review it, otherwise we will have to close it, because we won't be able to confirm the issue.

Relatedly, please add a test that fails before the PR and passes after the PR demonstrating the problem.

@CLFutureX

Copy link
Copy Markdown
Contributor Author

Hi @CLFutureX , we still need a minimal reproducible example for this bug so we can confirm the problem and that it is fixed after the PR. Thank you! If you are able to provide this we can review it, otherwise we will have to close it, because we won't be able to confirm the issue.

Relatedly, please add a test that fails before the PR and passes after the PR demonstrating the problem.

Thanks for your review. I'll find some time to add the relevant examples.

Signed-off-by: CLFutureX <chenyongqyl@163.com>
@CLFutureX

Copy link
Copy Markdown
Contributor Author

@neubig

Description

This change is an enhancement to the existing functionality, so i have added functional correctness tests:

  • tests/sdk/io/test_filestore_cache.py#test_cache_enable_and_disable

I also added tests for the usage scenario of this feature:

  • tests/sdk/conversation/local/test_state_serialization.py#test_conversation_state_fs_cache

Key Test Detail (test_conversation_state_fs_cache)

Before this change, the cache count was 1 for this test case. After adding the "cache on demand" feature (allowing cache to be enabled/disabled as needed), the conversation state is no longer cached in memory. This is verified by the assertion:

assert len(loaded_state._fs.cache) == 0

@CLFutureX
CLFutureX requested a review from neubig March 8, 2026 09:05

@neubig neubig left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks for adding the tests. However, this still is not the MRE I requested.

The new tests check the internal caching behavior after your change, but they do not minimally reproduce the original problem on main (i.e., excessive memory usage), nor do they show that the memory issue is significant in a real runnable scenario.

Please provide a true minimal reproducible example that includes:

  1. an exact script or command I can run,
  2. the minimal setup/input needed,
  3. the expected vs actual behavior,
  4. a concrete way to measure memory usage, and
  5. ideally, numbers showing the behavior on main and on this branch.

For example, this could be a small script that runs the agent or conversation state persistence in a loop, measures RSS / memory usage, and demonstrates that memory grows much larger than expected on main.

The assertion assert len(loaded_state._fs.cache) == 0 is not sufficient for this, because it only checks an internal implementation detail of your fix, not the original bug itself.

Once there is a runnable MRE, I’ll be happy to review again.

@CLFutureX

Copy link
Copy Markdown
Contributor Author

Thanks for adding the tests. However, this still is not the MRE I requested.

The new tests check the internal caching behavior after your change, but they do not minimally reproduce the original problem on main (i.e., excessive memory usage), nor do they show that the memory issue is significant in a real runnable scenario.

Please provide a true minimal reproducible example that includes:

  1. an exact script or command I can run,
  2. the minimal setup/input needed,
  3. the expected vs actual behavior,
  4. a concrete way to measure memory usage, and
  5. ideally, numbers showing the behavior on main and on this branch.

For example, this could be a small script that runs the agent or conversation state persistence in a loop, measures RSS / memory usage, and demonstrates that memory grows much larger than expected on main.

The assertion assert len(loaded_state._fs.cache) == 0 is not sufficient for this, because it only checks an internal implementation detail of your fix, not the original bug itself.

Once there is a runnable MRE, I’ll be happy to review again.

thanks for the review, I think we might have a slight misunderstanding about this PR

This is actually a feature enhancement. Its main goal is to implement on-demand caching to reduce unnecessary memory usage, rather than fixing the current high memory consumption issue.

For example, this check:
assert len(loaded_state._fs.cache) == 0

What it really shows is:
Previously, we cached session states unconditionally for all read and write operations in LocalFileStore. But in fact, caching should only be used in high-frequency reading scenarios.
With on-demand caching now supported, session states will no longer be cached by default.

@all-hands-bot

Copy link
Copy Markdown
Collaborator

[Automatic Post]: It has been a while since there was any activity on this PR. @CLFutureX, are you still working on it? If so, please go ahead, if not then please request review, close it, or request that someone else follow up.

@neubig

neubig commented Mar 15, 2026

Copy link
Copy Markdown
Member

I see your argument, but the thing is we don't add features unless they are solving a problem.

At the moment, you have not yet demonstrated that there is a problem. What I am asking for is that you demonstrate that there is a problem, for instance demonstrating that OpenHands is using more memory than it should. Once you demonstrate that there is a problem by providing the commands that you used to run the program and monitor memory, you could demonstrate that you ran the same commands after the fix and the memory usage went down.

@all-hands-bot

Copy link
Copy Markdown
Collaborator

[Automatic Post]: It has been a while since there was any activity on this PR. @CLFutureX, are you still working on it? If so, please go ahead, if not then please request review, close it, or request that someone else follow up.

5 similar comments
@all-hands-bot

Copy link
Copy Markdown
Collaborator

[Automatic Post]: It has been a while since there was any activity on this PR. @CLFutureX, are you still working on it? If so, please go ahead, if not then please request review, close it, or request that someone else follow up.

@all-hands-bot

Copy link
Copy Markdown
Collaborator

[Automatic Post]: It has been a while since there was any activity on this PR. @CLFutureX, are you still working on it? If so, please go ahead, if not then please request review, close it, or request that someone else follow up.

@all-hands-bot

Copy link
Copy Markdown
Collaborator

[Automatic Post]: It has been a while since there was any activity on this PR. @CLFutureX, are you still working on it? If so, please go ahead, if not then please request review, close it, or request that someone else follow up.

@all-hands-bot

Copy link
Copy Markdown
Collaborator

[Automatic Post]: It has been a while since there was any activity on this PR. @CLFutureX, are you still working on it? If so, please go ahead, if not then please request review, close it, or request that someone else follow up.

@all-hands-bot

Copy link
Copy Markdown
Collaborator

[Automatic Post]: It has been a while since there was any activity on this PR. @CLFutureX, are you still working on it? If so, please go ahead, if not then please request review, close it, or request that someone else follow up.

@neubig

neubig commented Apr 13, 2026

Copy link
Copy Markdown
Member

Closing due to lack of activity!

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.

4 participants