feat: inject AGENTS.md into system prompt

This commit is contained in:
aran-yogesh 2026-02-22 22:01:21 -08:00
parent 3d9ea5b916
commit 2d5da34b3d
3 changed files with 49 additions and 2 deletions

View file

@ -4,6 +4,7 @@ You are operating in a **remote Linux sandbox** at `{working_dir}`.
All code execution and file operations happen in this sandbox environment.
{agents_md_section}
**Important:**
- Use `{working_dir}` as your working directory for all operations
@ -72,9 +73,20 @@ def construct_system_prompt(
working_dir: str,
linear_project_id: str = "",
linear_issue_number: str = "",
agents_md: str | None = None,
) -> str:
agents_md_section = ""
if agents_md:
agents_md_section = (
"\nThe following text is pulled from the repository's AGENTS.md file. "
"It may contain specific instructions and guidelines for the agent.\n"
"<agents_md>\n"
f"{agents_md}\n"
"</agents_md>\n"
)
return SYSTEM_PROMPT.format(
working_dir=working_dir,
linear_project_id=linear_project_id or "<PROJECT_ID>",
linear_issue_number=linear_issue_number or "<ISSUE_NUMBER>",
agents_md_section=agents_md_section,
)

View file

@ -42,6 +42,7 @@ SANDBOX_CREATING = "__creating__"
SANDBOX_CREATION_TIMEOUT = 180
SANDBOX_POLL_INTERVAL = 1.0
from .utils.agents_md import read_agents_md_in_sandbox
from .utils.github import (
git_has_uncommitted_changes,
is_valid_git_repo,
@ -136,8 +137,10 @@ async def _clone_or_pull_repo_in_sandbox( # noqa: PLR0915
logger.exception("Failed to restore clean remote URL")
raise
logger.info("Repo updated at %s", repo_dir)
return repo_dir
logger.info("Repo updated at %s", repo_dir)
return repo_dir
logger.info("Cloning repo %s/%s to %s", owner, repo, repo_dir)
try:
@ -327,6 +330,7 @@ async def get_agent(config: RunnableConfig) -> Pregel: # noqa: PLR0915
linear_issue = config["configurable"].get("linear_issue", {})
linear_project_id = linear_issue.get("linear_project_id", "")
linear_issue_number = linear_issue.get("linear_issue_number", "")
agents_md = await read_agents_md_in_sandbox(sandbox_backend, repo_dir)
logger.info("Returning agent with sandbox for thread %s", thread_id)
return create_deep_agent(
@ -335,6 +339,7 @@ async def get_agent(config: RunnableConfig) -> Pregel: # noqa: PLR0915
repo_dir,
linear_project_id=linear_project_id,
linear_issue_number=linear_issue_number,
agents_md=agents_md,
),
tools=[http_request, fetch_url, commit_and_open_pr],
backend=sandbox_backend,

View file

@ -0,0 +1,30 @@
"""Helpers for reading agent instructions from AGENTS.md."""
from __future__ import annotations
import asyncio
import shlex
from deepagents.backends.protocol import SandboxBackendProtocol
async def read_agents_md_in_sandbox(
sandbox_backend: SandboxBackendProtocol,
repo_dir: str | None,
) -> str | None:
"""Read AGENTS.md from the repo root if it exists."""
if not repo_dir:
return None
safe_agents_path = shlex.quote(f"{repo_dir}/AGENTS.md")
loop = asyncio.get_event_loop()
result = await loop.run_in_executor(
None,
sandbox_backend.execute,
f"test -f {safe_agents_path} && cat {safe_agents_path}",
)
if result.exit_code != 0:
return None
content = result.output or ""
content = content.strip()
return content or None