feat(memory): post-final-answer memory extraction via standalone LLM call - #3682
Open
JasonW404 wants to merge 2 commits into
Open
feat(memory): post-final-answer memory extraction via standalone LLM call#3682JasonW404 wants to merge 2 commits into
JasonW404 wants to merge 2 commits into
Conversation
…call
Decouple memory extraction from the react loop. Previously, memories were
only stored when the LLM called StoreMemoryTool inline — unreliable and
often missed. This refactor introduces two complementary mechanisms:
1. Restrict StoreMemoryTool to action steps only
- Updated system prompt (zh/en) to prohibit store_memory during the
final answer step
- Updated ToolConfig description to match the new policy
2. Post-final-answer extraction pipeline
- New FaMemoryExtractor module: validates, truncates, calls tenant
LLM, parses <memory-item> tags, stores via MemoryService
- New extraction prompt YAML with structured output format
- Wired into agent_service.py finally block as fire-and-forget
asyncio.create_task (non-blocking, never delays user response)
- New build_memory_service_for_fa_extraction() factory in adapter
3. Bug fix: memory_type enum normalization in _backend_store_hook used
isinstance(x, MemoryLayer) instead of hasattr(x, 'value')
4. Test coverage: 29 new FaMemoryExtractor tests + 9 new adapter tests
(enum normalization, embedding skip, conversation_id coercion,
search defaults, string layer serialization, minimal payload)
…only policy
The existing tests asserted on old prompt text ('Before every final answer',
'must call store_memory', '用户提问') that was deliberately removed in the
refactor. Updated assertions to match the new action-steps-only policy.
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.
Summary
Refactors the memory service execution logic to decouple memory extraction from the react loop.
Problem
Previously, memories were only stored when the LLM called
StoreMemoryToolinline during the agent loop — a model-directed approach that was unreliable. The LLM often forgot or skipped the call at the final answer step.Solution
Two complementary mechanisms:
1. Restrict StoreMemoryTool to action steps only
store_memoryduring the final answer stepToolConfigdescription to match the new policy2. Post-final-answer extraction pipeline
FaMemoryExtractormodule (backend/services/fa_memory_extractor.py)backend/prompts/fa_memory_extraction_en.yaml)agent_service.pyfinally block as fire-and-forgetasyncio.create_task<memory-item>tags from the final answerMemoryServicewithlayer=AGENT, memory_type=SHORT_TERM3. Bug fix
memory_typeenum normalization in_backend_store_hookusedisinstance(x, MemoryLayer)instead ofhasattr(x, 'value')Files Changed
backend/services/fa_memory_extractor.pyFaMemoryExtractorclass +ExtractionResultdataclassbackend/prompts/fa_memory_extraction_en.yaml<memory-item>output formattest/backend/services/test_fa_memory_extractor.pybackend/utils/memory_tool_prompt.pystore_memoryto action stepsbackend/agents/create_agent_info.pybackend/services/memory_backend_adapter.pybuild_memory_service_for_fa_extraction()+ fix enum bugbackend/services/agent_service.pytest/backend/services/test_memory_backend_adapter.pyTest Results