Skip to content

Conversation

@im47cn
Copy link
Contributor

@im47cn im47cn commented Mar 16, 2025

Context

History message storage:

There are two core history stores in Cline.ts: apiConversationHistory and clineMessages
These histories grow over time and consume a lot of memory
Existing truncation mechanisms:

The MAX_HISTORY_MESSAGES = 150 constant is defined in sliding-window/index.ts
Long history detection and truncation logic in Cline.ts (lines 324-343 and 395-426)
But the strategy may not be aggressive enough or, in some cases, executed in a timely manner
Anti-shake delay problem:

Save history using anti-shake processing (300ms delay)
High frequency operations may cause the memory usage to increase until a crash occurs

Implementation

Lower the MAX_HISTORY_MESSAGES threshold to make truncation more aggressive
Improved truncation strategy of truncateConversation function
Optimized history saving mechanism in Cline.ts
Example Add memory usage monitoring logs

Screenshots

before after

How to Test

Look test file

Get in Touch

dreambt@Discord


Important

Enhance conversation truncation logic in Cline.ts and sliding-window/index.ts for better memory management and dialog integrity, with added tests and logging.

  • Behavior:
    • Lowered MAX_HISTORY_MESSAGES threshold in sliding-window/index.ts for more aggressive truncation.
    • Enhanced truncateConversation() in sliding-window/index.ts to retain first, last, and some middle messages.
    • Added memory usage logging before and after truncation in Cline.ts.
  • Functions:
    • Updated saveApiConversationHistory() and saveClineMessages() in Cline.ts to include anti-shake logic and aggressive truncation.
    • Modified truncateConversationIfNeeded() in sliding-window/index.ts to handle message count and token thresholds.
  • Tests:
    • Added tests for truncateConversationIfNeeded() in sliding-window.test.ts to verify truncation logic under different conditions.

This description was created by Ellipsis for 6556d0638bfcd5a14dd3423a249a49aaa77068b0. It will automatically update as commits are pushed.

@changeset-bot
Copy link

changeset-bot bot commented Mar 16, 2025

⚠️ No Changeset found

Latest commit: 002355f

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

@dosubot dosubot bot added the size:L This PR changes 100-499 lines, ignoring generated files. label Mar 16, 2025
@im47cn
Copy link
Contributor Author

im47cn commented Mar 16, 2025

fixed #1485

@dosubot dosubot bot added the enhancement New feature or request label Mar 16, 2025
Copy link
Contributor

Choose a reason for hiding this comment

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

Consider replacing direct calls to console.log with a structured logging mechanism (or a logger utility) to ensure consistent and configurable log output in production environments.

@cte
Copy link
Collaborator

cte commented Mar 17, 2025

Thanks for the contribution! I think there's some interesting stuff in here that we should consider. @mrubens has the most context on the sliding window and context management.

@hannesrudolph hannesrudolph moved this from New to PR [Pre Approval Review] in Roo Code Roadmap Mar 17, 2025
@im47cn im47cn force-pushed the feature/truncation branch from 835cdce to b1ca962 Compare March 18, 2025 02:12
@im47cn im47cn force-pushed the feature/truncation branch from b1ca962 to 002355f Compare March 18, 2025 03:00
@mrubens
Copy link
Collaborator

mrubens commented Mar 18, 2025

I agree, this is interesting! I would like to set up a structure for these context management strategies though, ideally outside of Cline.ts (e.g. src/core/sliding-window/index.ts). Let me think a little more about the best path forward with this stuff.

@hannesrudolph hannesrudolph moved this from New to PR [Pre Approval Review] in Roo Code Roadmap May 20, 2025
@hannesrudolph hannesrudolph moved this from PR [Needs Review] to TEMP in Roo Code Roadmap May 26, 2025
@daniel-lxs daniel-lxs moved this from TEMP to PR [Needs Review] in Roo Code Roadmap May 26, 2025
@daniel-lxs
Copy link
Member

Hey @im47cn, Thank you for your contribution.

You mentioned that this issue #1485 is solved on so we are closing this PR.

We've now shifted to a clearer issue-first workflow to avoid this kind of situation going forward. Please create an issue first for any future contributions, as outlined there.

@daniel-lxs daniel-lxs closed this May 27, 2025
@github-project-automation github-project-automation bot moved this from Needs Preliminary Review to Done in Roo Code Roadmap May 27, 2025
@github-project-automation github-project-automation bot moved this from PR [Pre Approval Review] to Done in Roo Code Roadmap May 27, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request size:L This PR changes 100-499 lines, ignoring generated files.

Projects

Archived in project

Development

Successfully merging this pull request may close these issues.

4 participants