The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(prompt): keep memory guidance within available tools
Teknium9d661c2c924d
diff --git a/agent/prompt_builder.py b/agent/prompt_builder.pyindex f7f6956f344..aedf7f43484 100644--- a/agent/prompt_builder.py+++ b/agent/prompt_builder.py@@ -158,15 +158,11 @@ HERMES_AGENT_HELP_GUIDANCE_NO_SKILLS = ()-# Memory guidance (#95681, consolidated): ONE block from ONE builder. The opening frame adapts to which-# stores config enables; everything else is written exactly once. Leads with the positive posture (save-# proactively, replace when full) — the routing rules come after, as refinements, not as the headline. WHAT-# belongs in memory is the memory tool schema's job and is never re-taught here.-def build_memory_guidance(memory_enabled: bool = True, profile_enabled: bool = True) -> str:- """ONE memory-guidance block whose opening frame adapts to the enabled store(s); "" when both are off.-- Positive posture first, routing rules as refinements. WHAT belongs in memory is the tool schema's job.- """+# Keep the every-session memory scope even when task knowledge cannot be saved as a skill.+def build_memory_guidance(+ memory_enabled: bool = True, profile_enabled: bool = True, *, skill_manage_available: bool = True,+) -> str:+ """Adapt store and skill-write guidance without widening what belongs in memory."""if not memory_enabled and not profile_enabled:return ""if memory_enabled:@@ -180,11 +176,17 @@ def build_memory_guidance(memory_enabled: bool = True, profile_enabled: bool = T"loaded into each new session's context; save durable facts about the user with the ""memory tool (target='user') — the built-in notes store is disabled, so never target='memory'. ")- return frame + (+ skill_routing = ("Skills come first: when you learn something while doing a task — a ""procedure, a pitfall, and the user's preferences and corrections ""for that kind of work — record it in the skill you used or built ""for the task (skill_manage), where it loads only when relevant. "+ if skill_manage_available else+ "Task-specific knowledge — procedures, pitfalls, and the user's preferences "+ "and corrections for that kind of work — belongs in skills, not in memory, "+ "even when skill writing is unavailable. "+ )+ return frame + skill_routing + ("Memory is the narrow exception for facts that apply to EVERY ""session regardless of task (who the user is, environment facts, ""standing conventions with no task home); it has a hard character "diff --git a/agent/system_prompt.py b/agent/system_prompt.pyindex e7f04271d05..34338b16d54 100644--- a/agent/system_prompt.py+++ b/agent/system_prompt.py@@ -19,8 +19,8 @@ from typing import Any, Dict, List, Optional, Tuplefrom agent.prompt_builder import (DEFAULT_AGENT_IDENTITY, EXECUTION_GUIDANCE_MODELS, GOOGLE_MODEL_OPERATIONAL_GUIDANCE,- HERMES_AGENT_HELP_GUIDANCE, HERMES_AGENT_HELP_GUIDANCE_NO_SKILLS, KANBAN_GUIDANCE, MEMORY_GUIDANCE,- USER_PROFILE_GUIDANCE, PARALLEL_TOOL_CALL_GUIDANCE, PLATFORM_HINTS, SESSION_SEARCH_GUIDANCE,+ HERMES_AGENT_HELP_GUIDANCE, HERMES_AGENT_HELP_GUIDANCE_NO_SKILLS, KANBAN_GUIDANCE,+ PARALLEL_TOOL_CALL_GUIDANCE, PLATFORM_HINTS, SESSION_SEARCH_GUIDANCE,SKILLS_GUIDANCE, STEER_CHANNEL_NOTE, TASK_COMPLETION_GUIDANCE, TELEGRAM_RICH_MESSAGES_HINT,TOOL_USE_ENFORCEMENT_GUIDANCE, TOOL_USE_ENFORCEMENT_MODELS, drain_truncation_warnings,)@@ -277,10 +277,11 @@ def _tool_guidance_block(agent: Any) -> Optional[str]:# available"; with only USER.md enabled the narrower block is used.memory_guidance = Noneif "memory" in names:- if getattr(agent, "_memory_enabled", True):- memory_guidance = MEMORY_GUIDANCE- elif getattr(agent, "_user_profile_enabled", True):- memory_guidance = USER_PROFILE_GUIDANCE+ memory_guidance = _pb.build_memory_guidance(+ getattr(agent, "_memory_enabled", True),+ getattr(agent, "_user_profile_enabled", True),+ skill_manage_available="skill_manage" in names,+ )# Kanban lifecycle: resolved once at __init__ (_kanban_worker_guidance);# the kanban_show fallback covers code paths that bypass agent_init._kanban_guidance = getattr(agent, "_kanban_worker_guidance", None)diff --git a/tests/agent/test_system_prompt.py b/tests/agent/test_system_prompt.pyindex 7f0862cd49e..0618ddf1cad 100644--- a/tests/agent/test_system_prompt.py+++ b/tests/agent/test_system_prompt.py@@ -6,6 +6,8 @@ from types import SimpleNamespacefrom unittest.mock import patchfrom zoneinfo import ZoneInfo+import pytest+from agent.system_prompt import build_system_prompt, build_system_prompt_parts@@ -55,6 +57,28 @@ def _captured_context_cwd(agent):return captured["cwd"][email protected]("stores", [(True, True), (False, True), (True, False), (False, False)])[email protected]("names", [+ set(), {"memory"}, {"memory", "skill_view", "skills_list"},+ {"memory", "skill_view", "skills_list", "skill_manage"},+])+def test_memory_guidance_respects_available_writes(stores, names, monkeypatch, tmp_path):+ monkeypatch.chdir(tmp_path)+ agent = _make_agent(valid_tool_names=names, skip_context_files=True,+ _memory_enabled=stores[0], _user_profile_enabled=stores[1])+ prompt = build_system_prompt(agent)+ enabled = "memory" in names and any(stores)+ assert ("Memory is the narrow exception" in prompt) == enabled+ assert ("(skill_manage)" in prompt) == (enabled and "skill_manage" in names)+ if enabled:+ assert "EVERY session regardless of task" in prompt+ assert "procedures and workflows belong in skills" in prompt+ if "skill_manage" not in names:+ assert "not in memory" in prompt+ if enabled and not stores[0]:+ assert "never target='memory'" in prompt++class TestContextFileCwd:def test_none_when_terminal_cwd_unset(self, monkeypatch):# Unset → None, so discovery falls back to the launch dir insidediff --git a/tests/run_agent/test_run_agent.py b/tests/run_agent/test_run_agent.pyindex 40bc52a06f7..a807fb7a6a5 100644--- a/tests/run_agent/test_run_agent.py+++ b/tests/run_agent/test_run_agent.py@@ -862,11 +862,10 @@ class TestBuildSystemPrompt:def test_memory_guidance_when_memory_tool_loaded(self, agent_with_memory_tool):- from agent.prompt_builder import MEMORY_GUIDANCE-agent_with_memory_tool._memory_enabled = Trueprompt = agent_with_memory_tool._build_system_prompt()- assert MEMORY_GUIDANCE in prompt+ assert "Memory is the narrow exception" in prompt+ assert "(skill_manage)" not in promptdef test_no_memory_guidance_when_both_builtin_stores_disabled(self, agent_with_memory_tool@@ -895,13 +894,15 @@ class TestBuildSystemPrompt:MEMORY.md store that does not exist in this configuration, so theprofile-specific block is injected instead."""- from agent.prompt_builder import MEMORY_GUIDANCE, USER_PROFILE_GUIDANCE+ from agent.prompt_builder import MEMORY_GUIDANCEagent_with_memory_tool._memory_enabled = Falseagent_with_memory_tool._user_profile_enabled = Trueprompt = agent_with_memory_tool._build_system_prompt()assert MEMORY_GUIDANCE not in prompt- assert USER_PROFILE_GUIDANCE in prompt+ assert "memory tool (target='user')" in prompt+ assert "never target='memory'" in prompt+ assert "(skill_manage)" not in promptdiff --git a/website/docs/developer-guide/prompt-assembly.md b/website/docs/developer-guide/prompt-assembly.mdindex aeae73d6a02..41ababfdb1c 100644--- a/website/docs/developer-guide/prompt-assembly.md+++ b/website/docs/developer-guide/prompt-assembly.md@@ -67,10 +67,10 @@ You value correctness, clarity, and efficiency....# Layer 2: Tool-aware behavior guidance-You have persistent memory across sessions. Save durable facts using-the memory tool: user preferences, environment details, tool quirks,-and stable conventions. Memory is injected into every turn, so keep-it compact and focused on facts that will still matter later.+Task-learned procedures, pitfalls, and task-specific preferences belong+in skills. Memory is the narrow exception for facts that apply to EVERY+session regardless of task. Skill-writing instructions appear here only+when skill_manage is available; its absence does not widen memory's scope....When the user references something from a past conversation or yoususpect relevant cross-session context exists, use session_search