fix(session): avoid O(n²) semantic re-processing on memory commit#809
Open
sliverp wants to merge 1 commit intovolcengine:mainfrom
Open
fix(session): avoid O(n²) semantic re-processing on memory commit#809sliverp wants to merge 1 commit intovolcengine:mainfrom
sliverp wants to merge 1 commit intovolcengine:mainfrom
Conversation
When SessionCompressor successfully extracts memories it already enqueues incremental SemanticMsg(s) with per-file change sets via _flush_semantic_operations(). The session.commit_async() fallback was unconditionally enqueueing a *second* SemanticMsg without any change info, causing the semantic processor to re-summarise and re-vectorise every file in the memory directory on every commit. Cost impact: cumulative token usage grew as O(n²) with memory count — 500 memories produced ~250K embedding tokens/day with 2000+ rate-limit retries. Changes: 1. session.py: Only enqueue fallback SemanticMsg when compressor is absent or extracted 0 memories. 2. semantic_processor.py: Always try to load existing overview.md summaries regardless of whether msg.changes is set, so even the fallback path can skip unchanged files. 3. Add 4 unit tests covering both paths. Closes volcengine#505 Ref volcengine#744
Collaborator
|
Thanks for the PR. The final semantic step in commit_async targets the session path, which is different from the memory path flushed by the compressor, so it can’t be removed or skipped. We’ll revisit and adjust the session-path semantic logic later. |
|
Failed to generate code suggestions for PR |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
When a new memory is extracted via
session.commit(), the session unconditionally enqueues aSemanticMsgwithout anychangesinformation (line 315 ofsession.py). This causes_process_memory_directory()to re-summarise and re-vectorise every file in the memory directory, regardless of whether it changed.The cumulative cost grows as O(n²) with memory count:
Real-world impact: ~500 memories consumed hundreds of thousands of embedding tokens per day on Volcengine, with 2000+ rate-limit (429) retries.
Root Cause
Two interacting issues:
session.py:315—commit_async()always enqueues a full-directorySemanticMsgeven whenSessionCompressor._flush_semantic_operations()has already enqueued incremental messages with per-file change sets.semantic_processor.py:369—_process_memory_directory()only attempts to reuse existing summaries from.overview.mdwhenmsg.changesis notNone. When the fallback path sends a message without changes, every file gets re-summarised.Fix
session.py: Only enqueue the fallbackSemanticMsgwhen the compressor is absent or extracted 0 memories. When the compressor runs successfully, it already handles incremental semantic processing.semantic_processor.py: Always try to load existing summaries from.overview.md, regardless of whethermsg.changesis set. This ensures even the fallback/redo path can skip unchanged files.Tests
Added 4 unit tests in
tests/unit/session/test_incremental_semantic.py:test_commit_skips_fallback_semantic_when_compressor_flushed— verifies no duplicate SemanticMsgtest_commit_enqueues_fallback_semantic_when_no_compressor— verifies fallback still workstest_commit_enqueues_fallback_when_compressor_extracts_zero— edge casetest_semantic_msg_changes_none_by_default— documents the defaultImpact
For a workspace with N memories, this reduces per-commit semantic processing from O(N) file summaries down to O(changed_files), and eliminates the O(N²) cumulative cost growth.
Closes #505
Ref #744