From 7ab4efab16f3e8361a540fe7411ebf70237c6c09 Mon Sep 17 00:00:00 2001 From: bugkeep <1921817430@qq.com> Date: Fri, 1 May 2026 00:14:01 +0800 Subject: [PATCH] fix(agent): include AGENTS.md for custom prompts --- src/kimi_cli/soul/agent.py | 33 ++++++++++++++++++++++++++++++++- tests/core/test_load_agent.py | 16 ++++++++++++++++ 2 files changed, 48 insertions(+), 1 deletion(-) diff --git a/src/kimi_cli/soul/agent.py b/src/kimi_cli/soul/agent.py index a208e0879..4fafde4cb 100644 --- a/src/kimi_cli/soul/agent.py +++ b/src/kimi_cli/soul/agent.py @@ -512,8 +512,39 @@ def _load_system_prompt( ) try: template = env.from_string(system_prompt) - return template.render(asdict(builtin_args), **args) + rendered = template.render(asdict(builtin_args), **args) + return _ensure_agents_md_in_system_prompt(rendered, builtin_args) except UndefinedError as exc: raise SystemPromptTemplateError(f"Missing system prompt arg in {path}: {exc}") from exc except TemplateError as exc: raise SystemPromptTemplateError(f"Invalid system prompt template: {path}: {exc}") from exc + + +def _ensure_agents_md_in_system_prompt( + system_prompt: str, builtin_args: BuiltinSystemPromptArgs +) -> str: + agents_md = builtin_args.KIMI_AGENTS_MD.strip() + if not agents_md or agents_md in system_prompt: + return system_prompt + + agents_guidance = ( + "`AGENTS.md` files can appear at any level of the project directory tree, " + "including inside `.kimi/` directories. Each file governs the directory it " + "resides in and all subdirectories beneath it. When multiple `AGENTS.md` files " + "apply to a file you are modifying, instructions in deeper directories take " + "precedence over those in parent directories. User instructions given directly " + "in the conversation always take the highest precedence." + ) + + return f"""{system_prompt} + +# Project Information + +The `AGENTS.md` instructions (merged from all applicable directories): + +````````` +{agents_md} +````````` + +{agents_guidance} +""" diff --git a/tests/core/test_load_agent.py b/tests/core/test_load_agent.py index 6e202b529..1a15d153f 100644 --- a/tests/core/test_load_agent.py +++ b/tests/core/test_load_agent.py @@ -48,6 +48,7 @@ def test_system_prompt_contains_platform_info(builtin_args: BuiltinSystemPromptA # System prompt must include OS kind and shell info assert builtin_args.KIMI_OS in prompt assert builtin_args.KIMI_SHELL in prompt + assert prompt.count(builtin_args.KIMI_AGENTS_MD) == 1 @pytest.mark.parametrize( @@ -87,6 +88,21 @@ def test_system_prompt_platform_warning(temp_work_dir, os_kind, shell, expect_wi assert "Many common Unix commands are not available" not in prompt +def test_load_system_prompt_appends_agents_md_for_custom_prompt( + builtin_args: BuiltinSystemPromptArgs, +): + """Custom prompts should still receive project instructions from AGENTS.md.""" + with tempfile.TemporaryDirectory() as tmpdir: + tmpdir = Path(tmpdir) + system_md = tmpdir / "system.md" + system_md.write_text("You are a custom agent.") + prompt = _load_system_prompt(system_md, {}, builtin_args) + + assert "You are a custom agent." in prompt + assert "The `AGENTS.md` instructions" in prompt + assert builtin_args.KIMI_AGENTS_MD in prompt + + def test_load_system_prompt_allows_literal_dollar(builtin_args: BuiltinSystemPromptArgs): """System prompt should allow literal $ without template errors.""" with tempfile.TemporaryDirectory() as tmpdir: