From 32f5cc76a43da85d7a3da261fe353da3750e8a12 Mon Sep 17 00:00:00 2001 From: chengyongru <2755839590@qq.com> Date: Thu, 15 Oct 2026 23:50:53 +0800 Subject: [PATCH] fix(agent): trim redundant system prompt context --- nanobot/agent/context.py | 2 +- nanobot/agent/skills.py | 12 +++--------- nanobot/templates/agent/identity.md | 2 +- nanobot/templates/agent/platform_policy.md | 6 ++---- tests/agent/test_context_prompt_cache.py | 4 ++-- tests/agent/test_skills_loader.py | 4 ++-- 6 files changed, 11 insertions(+), 19 deletions(-) diff --git a/nanobot/agent/context.py b/nanobot/agent/context.py index 7277705b9..825999fd8 100644 --- a/nanobot/agent/context.py +++ b/nanobot/agent/context.py @@ -113,7 +113,7 @@ class ContextBuilder: if include_memory: memory = self.memory.read_memory() if memory and not self._is_template_content(memory, "memory/MEMORY.md"): - parts.append(f"# Memory\n\n## Long-term Memory\n{memory}") + parts.append(memory) with self.skills.snapshot(): active_skills = self.skills.get_always_skills() diff --git a/nanobot/agent/skills.py b/nanobot/agent/skills.py index cc19ae238..5c010680a 100644 --- a/nanobot/agent/skills.py +++ b/nanobot/agent/skills.py @@ -252,7 +252,7 @@ class SkillsLoader: workspace: Path | None = None, ) -> str: """ - Build a summary of all skills (name, description, path, availability). + Build a summary of available skills (name, description, path). This is used for progressive loading - the agent can read the full skill content using read_file when needed. @@ -268,7 +268,7 @@ class SkillsLoader: return self._build_skills_summary(exclude, workspace=workspace) def _build_skills_summary(self, exclude: set[str] | None, *, workspace: Path | None) -> str: - all_skills = self.list_skills(filter_unavailable=False) + all_skills = self.list_skills(filter_unavailable=True) if not all_skills: return "" @@ -300,15 +300,9 @@ class SkillsLoader: lines = [f"### {label} (`{display_root}`)"] for entry in entries: skill_name = entry["name"] - meta = self._get_skill_meta(skill_name) - available = self._check_requirements(meta) desc = self.get_skill_description(skill_name) - suffix = "" - if not available: - missing = self._get_missing_requirements(meta) - suffix = f" (unavailable: {missing})" if missing else " (unavailable)" relative_path = Path(entry["path"]).relative_to(root).as_posix() - lines.append(f"- **{skill_name}** — {desc}{suffix} `{relative_path}`") + lines.append(f"- **{skill_name}** — {desc} `{relative_path}`") sections.append("\n".join(lines)) return "\n\n".join(sections) diff --git a/nanobot/templates/agent/identity.md b/nanobot/templates/agent/identity.md index 8ca90c663..8c05799b5 100644 --- a/nanobot/templates/agent/identity.md +++ b/nanobot/templates/agent/identity.md @@ -1,5 +1,6 @@ ## Runtime {{ runtime }} +{{ platform_policy }} ## Workspace {% if agent_workspace_path != workspace_path %} @@ -17,7 +18,6 @@ Nanobot's agent workspace is at: {{ agent_workspace_path }} Only Dream memory-consolidation tasks may edit the profile and long-term memory files listed above. -{{ platform_policy }} {% if channel == 'telegram' or channel == 'qq' or channel == 'discord' %} ## Format Hint This conversation is on a messaging app. Use short paragraphs. Avoid large headings (#, ##). Use **bold** sparingly. No tables — use plain lists. diff --git a/nanobot/templates/agent/platform_policy.md b/nanobot/templates/agent/platform_policy.md index a47e104e4..ac4d8fc54 100644 --- a/nanobot/templates/agent/platform_policy.md +++ b/nanobot/templates/agent/platform_policy.md @@ -1,10 +1,8 @@ {% if system == 'Windows' %} -## Platform Policy (Windows) -- You are running on Windows. Do not assume GNU tools like `grep`, `sed`, or `awk` exist. +- Do not assume GNU tools like `grep`, `sed`, or `awk` exist. - Prefer Windows-native commands or file tools when they are more reliable. - If terminal output is garbled, retry with UTF-8 output enabled. {% else %} -## Platform Policy (POSIX) -- You are running on a POSIX system. Prefer UTF-8 and standard shell tools. +- Prefer UTF-8 and standard shell tools. - Use file tools when they are simpler or more reliable than shell commands. {% endif %} diff --git a/tests/agent/test_context_prompt_cache.py b/tests/agent/test_context_prompt_cache.py index c582f03b4..95950bc7c 100644 --- a/tests/agent/test_context_prompt_cache.py +++ b/tests/agent/test_context_prompt_cache.py @@ -270,7 +270,7 @@ def test_template_memory_md_is_skipped(tmp_path) -> None: builder = ContextBuilder(workspace) prompt = builder.build_system_prompt() - assert "## Long-term Memory" not in prompt + assert "# Long-term Memory" not in prompt assert "History log: memory/history.jsonl" in prompt assert "This file is automatically updated by nanobot" not in prompt @@ -297,6 +297,6 @@ def test_customized_memory_md_is_injected(tmp_path, monkeypatch) -> None: monkeypatch.setattr(builder.memory, "read_memory", tracked_read_memory) prompt = builder.build_system_prompt() - assert "# Memory\n\n## Long-term Memory" in prompt + assert prompt.count("# Long-term Memory") == 1 assert "User prefers dark mode" in prompt assert calls == 1 diff --git a/tests/agent/test_skills_loader.py b/tests/agent/test_skills_loader.py index 477ba9531..986be14f4 100644 --- a/tests/agent/test_skills_loader.py +++ b/tests/agent/test_skills_loader.py @@ -530,7 +530,7 @@ def test_prompt_reads_skills_once_and_refreshes_on_next_build( monkeypatch.setattr("nanobot.agent.skills.shutil.which", which) first = build_prompt() assert "First instructions" in first - assert "unavailable: CLI: prompt-test-cli" in first + assert "**beta**" not in first assert discoveries == 1 assert all(reads.count(path) == 1 for path in documents) assert len(parses) == len(documents) @@ -546,7 +546,7 @@ def test_prompt_reads_skills_once_and_refreshes_on_next_build( second = build_prompt() assert "Updated instructions" in second and "First instructions" not in second assert "Updated description" in second - assert "unavailable: CLI: prompt-test-cli" not in second + assert "**beta**" in second assert "**gamma**" in second assert discoveries == 2 assert all(reads.count(path) == 1 for path in documents)