Agent Zero AI framework
Improve auto-memory extraction quality
Alessandroa0a815943a3b
diff --git a/plugins/_memory/README.md b/plugins/_memory/README.mdindex 9b6d9617c..2f33a0b0b 100644--- a/plugins/_memory/README.md+++ b/plugins/_memory/README.md@@ -15,6 +15,8 @@ This plugin stores memories and knowledge embeddings in a FAISS-backed vector da- Loads configured knowledge directories into memory when a database is initialized.- **Memory tools**- Includes tools for saving, loading, deleting, forgetting, and behavior adjustment workflows.+- **Automatic conversation memory**+ - Stores durable preferences, project facts, and recurring constraints while filtering transient action-history fragments before insertion.- **Dashboard APIs**- Exposes search, delete, bulk delete, update, and subdirectory listing endpoints for the memory dashboard.- **Scoped storage**diff --git a/plugins/_memory/extensions/python/monologue_end/_50_memorize_fragments.py b/plugins/_memory/extensions/python/monologue_end/_50_memorize_fragments.pyindex 2ab9fbf31..59bd71227 100644--- a/plugins/_memory/extensions/python/monologue_end/_50_memorize_fragments.py+++ b/plugins/_memory/extensions/python/monologue_end/_50_memorize_fragments.py@@ -8,6 +8,7 @@ from helpers.defer import DeferredTask, THREAD_BACKGROUND# Direct import - this extension lives inside the memory pluginfrom plugins._memory.helpers.memory import Memory+from plugins._memory.helpers.memory_quality import filter_auto_memory_fragmentsfrom plugins._memory.tools.memory_load import DEFAULT_THRESHOLD as DEFAULT_MEMORY_THRESHOLD@@ -104,9 +105,24 @@ class MemorizeMemories(Extension):if not isinstance(memories, list) or len(memories) == 0:log_item.update(heading="No useful information to memorize.")return- else:- memories_txt = "\n\n".join([str(memory) for memory in memories]).strip()- log_item.update(heading=f"{len(memories)} entries to memorize.", memories=memories_txt)++ raw_memories_count = len(memories)+ memories = filter_auto_memory_fragments(memories)+ filtered_memories_count = raw_memories_count - len(memories)++ if not memories:+ log_item.update(+ heading="No durable information to memorize.",+ filtered_memories_count=filtered_memories_count,+ )+ return++ memories_txt = "\n\n".join(memories).strip()+ log_item.update(+ heading=f"{len(memories)} durable entries to memorize.",+ memories=memories_txt,+ filtered_memories_count=filtered_memories_count,+ )# Process memories with intelligent consolidationtotal_processed = 0diff --git a/plugins/_memory/helpers/memory_quality.py b/plugins/_memory/helpers/memory_quality.pynew file mode 100644index 000000000..82139dd1d--- /dev/null+++ b/plugins/_memory/helpers/memory_quality.py@@ -0,0 +1,88 @@+from __future__ import annotations++import re+from typing import Iterable+++_DURABLE_SUBJECT_RE = re.compile(+ r"\b(user|project|repo|repository|workspace|runtime|service|server|"+ r"plugin|agent zero|a0|profile|team|organization|company)\b",+ re.IGNORECASE,+)+_DURABLE_RELATION_RE = re.compile(+ r"\b(prefers?|requires?|uses?|runs?|lives?|located|configured|default|"+ r"current(?:ly)?|must|should|always|never|constraint|path|endpoint|port)\b",+ re.IGNORECASE,+)+_TRANSIENT_RE = re.compile(+ r"(/tmp\b|\btmp/|\btemporary\b|\btemp\b|\bworkdir\b|"+ r"\bcontainer-local\b|\bmachine-local\b|\bpersonal absolute path\b|"+ r"\blocal endpoint\b|\blocal port\b|"+ r"\btest marker\b|\bmemory test\b|\bcleanup token\b|\bthrowaway\b|"+ r"\bsample file\b|\bdemo file\b|\bvalidation directory\b|\blive-test\b|"+ r"\bone-off\b|\bsession-only\b)",+ re.IGNORECASE,+)+_LOCAL_COORDINATE_RE = re.compile(+ r"\b(localhost|127\.0\.0\.1|0\.0\.0\.0|::1)\b|"+ r"https?://(?:localhost|127\.0\.0\.1|0\.0\.0\.0|\[::1\])(?::\d+)?|"+ r"/(?:home|Users)/[^/\s]+/|"+ r"\b[A-Za-z]:\\Users\\",+ re.IGNORECASE,+)+_AGENT_ACTION_RE = re.compile(+ r"\b(agent|assistant|codex|a0)\b.{0,80}\b("+ r"created|implemented|built|ran|tested|verified|fixed|wrote|generated|"+ r"saved|cleaned|inspected|synced|restarted|committed|staged|opened|"+ r"reported|showed)\b",+ re.IGNORECASE,+)+_USER_REQUEST_RE = re.compile(+ r"\b(user|the user)\s+(asked|requested|wanted|told|prompted|instructed)\b",+ re.IGNORECASE,+)+_COMMAND_HISTORY_RE = re.compile(+ r"\b(ran|executed)\s+[`'\"]?[\w./-]+|\bterminal command\b|"+ r"\bcommand output\b|\bexit code\b|\bstdout\b|\bstderr\b",+ re.IGNORECASE,+)+_LOW_VALUE_RE = re.compile(+ r"^(hello|hi|greeting|conversation start|task completed|file created|"+ r"implementation completed|test passed|memory search)$",+ re.IGNORECASE,+)+++def normalize_memory_candidate(value: object) -> str:+ return str(value).strip()+++def is_auto_fragment_worth_saving(text: str) -> bool:+ """Return True only for durable facts suitable for automatic fragments."""++ text = normalize_memory_candidate(text)+ has_durable_relation = bool(_DURABLE_RELATION_RE.search(text))+ if len(text) < 24:+ return False+ if _LOW_VALUE_RE.match(text):+ return False+ if _TRANSIENT_RE.search(text):+ return False+ if _LOCAL_COORDINATE_RE.search(text):+ return False+ if _COMMAND_HISTORY_RE.search(text):+ return False+ if _AGENT_ACTION_RE.search(text) and not has_durable_relation:+ return False+ if _USER_REQUEST_RE.search(text) and not has_durable_relation:+ return False++ return bool(_DURABLE_SUBJECT_RE.search(text) and has_durable_relation)+++def filter_auto_memory_fragments(memories: Iterable[object]) -> list[str]:+ return [+ text+ for text in (normalize_memory_candidate(memory) for memory in memories)+ if is_auto_fragment_worth_saving(text)+ ]diff --git a/plugins/_memory/prompts/fw.memory.hist_suc.sys.md b/plugins/_memory/prompts/fw.memory.hist_suc.sys.mdindex 8739785e7..ddac2274b 100644--- a/plugins/_memory/prompts/fw.memory.hist_suc.sys.md+++ b/plugins/_memory/prompts/fw.memory.hist_suc.sys.md@@ -20,4 +20,7 @@# Rules- Focus on important details like libraries used, code, encountered issues, error fixing etc.-- Do not include simple solutions that don't require instructions to reproduce like file handling, web search etc.\ No newline at end of file+- Do not include simple solutions that don't require instructions to reproduce like file handling, web search etc.+- Do not save one-off completed work, temporary file paths, exact session artifacts, benchmark/task IDs, or command transcripts+- Save a solution only when it is a reusable technical procedure likely to help with future similar work+- Prefer concise reproduction steps over a diary of what the agent diddiff --git a/plugins/_memory/prompts/memory.keyword_extraction.sys.md b/plugins/_memory/prompts/memory.keyword_extraction.sys.mdindex a0ae25194..33cc8b230 100644--- a/plugins/_memory/prompts/memory.keyword_extraction.sys.md+++ b/plugins/_memory/prompts/memory.keyword_extraction.sys.md@@ -31,23 +31,23 @@ Return ONLY a JSON array of strings containing the extracted keywords/phrases:## Examples-**Memory Content**: "Successfully implemented OAuth authentication using JWT tokens for the user login system. The solution handles token refresh and validation properly."+**Memory Content**: "User prefers Linux shell examples and ./.venv/bin/python paths instead of Windows-only virtualenv commands."**Output**:```json-["OAuth authentication", "JWT tokens", "user login", "token refresh", "authentication implementation"]+["Linux shell examples", "virtualenv paths", "user preference", "Python commands"]```-**Memory Content**: "Fixed the database connection timeout issue by increasing the connection pool size and optimizing slow queries with proper indexing."+**Memory Content**: "Project uses a configured Dockerized live runtime for smoke checks."**Output**:```json-["database connection", "timeout issue", "connection pool", "query optimization", "indexing"]+["Dockerized runtime", "live runtime", "smoke checks", "project runtime"]```-**Memory Content**: "Learned that Alpine.js x-data components should use camelCase for method names and snake_case for data properties to follow best practices."+**Memory Content**: "Agent Zero WebUI stores use Alpine.js createStore from /js/AlpineStore.js."**Output**:```json-["Alpine.js", "x-data components", "camelCase methods", "naming conventions"]+["Agent Zero WebUI", "Alpine.js", "createStore", "AlpineStore.js"]```diff --git a/plugins/_memory/prompts/memory.memories_sum.sys.md b/plugins/_memory/prompts/memory.memories_sum.sys.mdindex 07ad1abae..82615333b 100644--- a/plugins/_memory/prompts/memory.memories_sum.sys.md+++ b/plugins/_memory/prompts/memory.memories_sum.sys.md@@ -1,10 +1,10 @@# Assistant's job1. The assistant receives a HISTORY of conversation between USER and AGENT-2. Assistant searches for relevant information from the HISTORY worth memorizing-3. Assistant writes notes about information worth memorizing for further use+2. Assistant searches for durable information from the HISTORY worth memorizing+3. Assistant writes notes about stable information worth recalling in future work# Format-- The response format is a JSON array of text notes containing facts to memorize+- The response format is a JSON array of text notes containing durable facts to memorize- If the history does not contain any useful information, the response will be an empty JSON array.# Output example@@ -16,14 +16,18 @@~~~# Rules-- Only memorize complete information that is helpful in the future+- Only memorize complete information that is likely to remain helpful across future conversations- Never memorize vague or incomplete information- Never memorize keywords or titles only-- Focus only on relevant details and facts like names, IDs, events, opinions etc.+- Focus on durable user preferences, stable project facts, recurring collaboration constraints, important identities, configured services, and long-lived requirements+- Do not memorize machine-specific local endpoints, personal absolute paths, container work directories, or ephemeral runtime coordinates as fragment memories; convert to a generic durable fact only if the stable project relationship matters- Do not include irrelevant details that are of no use in the future- Do not memorize facts that change like time, date etc.- Do not add your own details that are not specifically mentioned in the history- Do not memorize AI's instructions or thoughts+- Do not memorize what the agent did in this session, commands it ran, files it created, test output, temporary paths, implementation minutiae, or cleanup-only facts+- Do not memorize a user's one-off request unless it states a durable preference, stable project fact, or recurring constraint+- If the only notable information is task progress or a completed implementation, return an empty array; reusable procedures belong to successful-solution memory, not fragments# Merging and cleaning- The goal is to keep the number of new memories low while making memories more complete and detailed@@ -36,8 +40,9 @@# Correct examples of data worth memorizing with (explanation)> User's name is John Doe (name is important)-> AsyncRaceError in primary_modules.py was fixed by adding a thread lock on line 123 (important event with details for context)-> Local SQL database was created, server is running on port 3306 (important event with details for context)+> User prefers Linux shell commands and relative virtualenv paths over Windows-only examples (stable user preference)+> Project currently uses a configured live runtime for smoke checks (stable project fact without local coordinates)+> Runtime-impacting plugin changes must be synced into the configured live environment before testing (recurring project constraint)# WRONG examples with (explanation of error), never output memories like these> Dog Information (no useful facts)@@ -50,6 +55,11 @@> RAM Status (just a topic without detail)> User used to prefer X before changing to Y (historical preference is usually not useful; memorize the current preference only)> Temporary marker ABC123 was used in a memory test (test residue, not useful)+> Agent created a temporary CLI demo file and ran a shell test (agent action history, not a durable fact)+> The live UI was reachable at a machine-local endpoint during this session (local runtime detail, not a durable memory)+> User asked to build a tiny CLI todo app (one-off request, not a recurring preference)+> The markdown-to-HTML script generated sample.html with 181 bytes (task output, not useful later)+> AsyncRaceError in primary_modules.py was fixed by adding a thread lock on line 123 (belongs in successful solutions if reusable, not fragments)# Further WRONG examplesdiff --git a/tests/test_memory_quality.py b/tests/test_memory_quality.pynew file mode 100644index 000000000..d20322ca7--- /dev/null+++ b/tests/test_memory_quality.py@@ -0,0 +1,53 @@+from __future__ import annotations++import sys+from pathlib import Path++PROJECT_ROOT = Path(__file__).resolve().parents[1]+if str(PROJECT_ROOT) not in sys.path:+ sys.path.insert(0, str(PROJECT_ROOT))++from plugins._memory.helpers.memory_quality import (+ filter_auto_memory_fragments,+ is_auto_fragment_worth_saving,+)+++def test_auto_fragment_quality_keeps_durable_preferences_and_project_facts():+ assert is_auto_fragment_worth_saving(+ "User currently prefers concise technical answers with verification."+ )+ assert is_auto_fragment_worth_saving(+ "Project currently uses a configured live runtime for smoke checks."+ )+ assert is_auto_fragment_worth_saving(+ "Runtime-impacting plugin changes must be synced into the configured live environment before testing."+ )+++def test_auto_fragment_quality_rejects_action_history_and_transient_artifacts():+ rejected = [+ "Agent created a temporary CLI demo file and ran a shell test.",+ "The user asked to build a tiny CLI todo app in Python.",+ "Temporary marker ABC123 was used in a memory test.",+ "The markdown-to-HTML script generated sample.html with 181 bytes.",+ "Fixed AsyncRaceError in primary_modules.py by adding a thread lock on line 123.",+ "The live UI was reachable at a machine-local endpoint during this session.",+ "Project repository path is a personal absolute path on this machine.",+ ]++ for memory in rejected:+ assert not is_auto_fragment_worth_saving(memory)+++def test_auto_fragment_filter_normalizes_and_preserves_kept_order():+ memories = [+ "User currently prefers Linux paths in examples.",+ "Agent created a demo file and reported success.",+ "Project repository uses a configured source workspace.",+ ]++ assert filter_auto_memory_fragments(memories) == [+ "User currently prefers Linux paths in examples.",+ "Project repository uses a configured source workspace.",+ ]