π OpenHands: AI-Driven Development
[Feature] Support .cursorrules (#9327)
AutoLTX7abad5844a85
diff --git a/openhands/microagent/microagent.py b/openhands/microagent/microagent.pyindex 7a3e299a60..767d60a477 100644--- a/openhands/microagent/microagent.py+++ b/openhands/microagent/microagent.py@@ -1,5 +1,6 @@import ioimport re+from itertools import chainfrom pathlib import Pathfrom typing import Union@@ -39,7 +40,11 @@ class BaseMicroagent(BaseModel):# Otherwise, we will rely on the name from metadata laterderived_name = Noneif microagent_dir is not None:- derived_name = str(path.relative_to(microagent_dir).with_suffix(''))+ # Special handling for .cursorrules files which are not in microagent_dir+ if path.name == '.cursorrules':+ derived_name = 'cursorrules'+ else:+ derived_name = str(path.relative_to(microagent_dir).with_suffix(''))# Only load directly from path if file_content is not providedif file_content is None:@@ -56,6 +61,16 @@ class BaseMicroagent(BaseModel):type=MicroagentType.REPO_KNOWLEDGE,)+ # Handle .cursorrules files+ if path.name == '.cursorrules':+ return RepoMicroagent(+ name='cursorrules',+ content=file_content,+ metadata=MicroagentMetadata(name='cursorrules'),+ source=str(path),+ type=MicroagentType.REPO_KNOWLEDGE,+ )+file_io = io.StringIO(file_content)loaded = frontmatter.load(file_io)content = loaded.content@@ -258,10 +273,15 @@ def load_microagents_from_dir(# Load all agents from microagents directorylogger.debug(f'Loading agents from {microagent_dir}')if microagent_dir.exists():- for file in microagent_dir.rglob('*.md'):- # skip README.md- if file.name == 'README.md':- continue+ # Collect .cursorrules file from repo root and .md files from microagents dir+ cursorrules_files = []+ if (microagent_dir.parent.parent / '.cursorrules').exists():+ cursorrules_files = [microagent_dir.parent.parent / '.cursorrules']++ md_files = [f for f in microagent_dir.rglob('*.md') if f.name != 'README.md']++ # Process all files in one loop+ for file in chain(cursorrules_files, md_files):try:agent = BaseMicroagent.load(file, microagent_dir)if isinstance(agent, RepoMicroagent):diff --git a/tests/unit/test_microagent_utils.py b/tests/unit/test_microagent_utils.pyindex 15d2459685..92a1d0ce4f 100644--- a/tests/unit/test_microagent_utils.py+++ b/tests/unit/test_microagent_utils.py@@ -201,3 +201,75 @@ This microagent has an invalid type.assert '"knowledge"' in error_msgassert '"repo"' in error_msgassert '"task"' in error_msg+++def test_cursorrules_file_load():+ """Test loading .cursorrules file as a RepoMicroagent."""+ cursorrules_content = """Always use Python for new files.+Follow the existing code style.+Add proper error handling."""++ cursorrules_path = Path('.cursorrules')++ # Test loading .cursorrules file directly+ agent = BaseMicroagent.load(cursorrules_path, file_content=cursorrules_content)++ # Verify it's loaded as a RepoMicroagent+ assert isinstance(agent, RepoMicroagent)+ assert agent.name == 'cursorrules'+ assert agent.content == cursorrules_content+ assert agent.type == MicroagentType.REPO_KNOWLEDGE+ assert agent.metadata.name == 'cursorrules'+ assert agent.source == str(cursorrules_path)+++def temp_microagents_dir_with_cursorrules():+ """Create a temporary directory with test microagents and .cursorrules file."""+ with tempfile.TemporaryDirectory() as temp_dir:+ root = Path(temp_dir)++ # Create .openhands/microagents directory structure+ microagents_dir = root / '.openhands' / 'microagents'+ microagents_dir.mkdir(parents=True, exist_ok=True)++ # Create .cursorrules file in repository root+ cursorrules_content = """Always use TypeScript for new files.+Follow the existing code style."""+ (root / '.cursorrules').write_text(cursorrules_content)++ # Create test repo agent+ repo_agent = """---+# type: repo+version: 1.0.0+agent: CodeActAgent+---++# Test Repository Agent++Repository-specific test instructions.+"""+ (microagents_dir / 'repo.md').write_text(repo_agent)++ yield root+++def test_load_microagents_with_cursorrules(temp_microagents_dir_with_cursorrules):+ """Test loading microagents when .cursorrules file exists."""+ microagents_dir = (+ temp_microagents_dir_with_cursorrules / '.openhands' / 'microagents'+ )++ repo_agents, knowledge_agents = load_microagents_from_dir(microagents_dir)++ # Verify that .cursorrules file was loaded as a RepoMicroagent+ assert len(repo_agents) == 2 # repo.md + .cursorrules+ assert 'repo' in repo_agents+ assert 'cursorrules' in repo_agents++ # Check .cursorrules agent+ cursorrules_agent = repo_agents['cursorrules']+ assert isinstance(cursorrules_agent, RepoMicroagent)+ assert cursorrules_agent.name == 'cursorrules'+ assert 'Always use TypeScript for new files' in cursorrules_agent.content+ assert cursorrules_agent.type == MicroagentType.REPO_KNOWLEDGE