From 8aa1cf4e6df2ebc5aabc479138e1287108b671f1 Mon Sep 17 00:00:00 2001 From: elroy-bot Date: Sun, 25 Jan 2026 17:27:19 -0800 Subject: [PATCH 1/2] add tools for self improvement --- CLAUDE.md | 92 ++++++++ README.md | 2 + ROADMAP.md | 33 ++- claude-skills/README.md | 24 ++ claude-skills/install-skills.sh | 16 +- claude-skills/introspect/SKILL.md | 80 +++++++ claude-skills/make-improvement/SKILL.md | 251 +++++++++++++++++++++ docs/tools_guide.md | 15 +- elroy/cli/main.py | 16 +- elroy/messenger/claude_code_integration.py | 163 +++++++++++++ elroy/tools/self_improvement.py | 124 ++++++++++ elroy/tools/tools_and_commands.py | 4 + 12 files changed, 797 insertions(+), 23 deletions(-) create mode 100644 claude-skills/introspect/SKILL.md create mode 100644 claude-skills/make-improvement/SKILL.md create mode 100644 elroy/messenger/claude_code_integration.py create mode 100644 elroy/tools/self_improvement.py diff --git a/CLAUDE.md b/CLAUDE.md index 9b76fa28..35d9bf43 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -41,3 +41,95 @@ The project roadmap is maintained in `ROADMAP.md`. When working on features or d - Update the roadmap when completing items (move to "Completed" section) - Add new items as they are identified or requested - Keep items well-organized by category (Performance, Features, etc.) + +## Elroy Agent Tools (Also Available as Claude Code Skills) + +Elroy provides tools that are available both to the Elroy agent directly AND as Claude Code skills (via `/` commands). + +### Memory Management Tools + +These tools allow creating and managing long-term memories: + +- **create_memory(name, text)** - Create a new long-term memory + - Claude Code: `/remember "content"` + - Example: `create_memory("Project preferences", "User prefers TypeScript")` + +- **examine_memories(query)** - Search through memories + - Claude Code: `/recall "query"` + - Example: `examine_memories("authentication method")` + +- **print_memories()** - List all active memories + - Claude Code: `/list-memories` + +- **create_reminder(name, text, trigger_datetime, reminder_context)** - Create a reminder + - Claude Code: `/remind "content"` + - Example: `create_reminder("Review PR", "Check the authentication PR", trigger_datetime="2025-01-26 14:00")` + +- **print_active_reminders()** - List active reminders + - Claude Code: `/list-reminders` + +- **ingest_doc(path)** - Ingest documents into memory + - Claude Code: `/ingest path` + - Example: `ingest_doc("docs/architecture.md")` + +### Self-Improvement Tools + +These tools enable Elroy to understand and improve itself: + +- **introspect(query)** - Ask questions about Elroy's implementation + - Claude Code: `/introspect "query"` + - Example: `introspect("How does memory consolidation work?")` + - Returns: Detailed explanation with file paths and implementation details + +- **make_improvement(description, create_branch, run_tests, submit_pr)** - Implement an improvement + - Claude Code: `/make-improvement "description"` + - Example: `make_improvement("Add date-aware search to examine_memories")` + - Workflow: Plan → Implement → Test → Document → Submit PR + - Returns: PR URL when submitted + +- **review_roadmap()** - Review roadmap, issues, and recent commits + - Example: `review_roadmap()` + - Returns: Current project status and priorities + +### Usage in Elroy Agent + +When working as the Elroy agent (not in Claude Code), you can call these tools directly: + +```python +# Understand implementation before making changes +result = introspect("How does the tool registry work?") + +# Check project priorities +status = review_roadmap() + +# Implement an improvement +pr_url = make_improvement( + "Add better error messages for tool failures", + create_branch=True, + run_tests=True, + submit_pr=True +) +``` + +### Usage in Claude Code + +When working in Claude Code, use the slash command versions: + +```bash +/introspect "How does memory consolidation work?" +/make-improvement "Add date-aware search" +/recall "project architecture decisions" +/remember "User prefers functional components" +``` + +### Development Workflow + +The self-improvement tools create a complete feedback loop: + +1. **Identify improvement** - Through usage patterns, user request, or review_roadmap() +2. **Understand current state** - Use introspect() to explore implementation +3. **Plan changes** - Review ROADMAP.md and issues with review_roadmap() +4. **Implement** - Use make_improvement() for structured implementation +5. **Submit for review** - PR created automatically with tests and documentation + +This enables Elroy to actively participate in its own development. diff --git a/README.md b/README.md index 7e8fcb50..4f341dfe 100644 --- a/README.md +++ b/README.md @@ -134,6 +134,8 @@ Elroy provides skills for [Claude Code](https://github.com/anthropics/claude-cod - `/remind` - Create a reminder - `/list-reminders` - List active reminders - `/ingest` - Ingest documents into memory +- `/introspect` - Ask questions about Elroy's implementation (for development) +- `/make-improvement` - Implement a feature or improvement and submit a PR (for development) ### Installation diff --git a/ROADMAP.md b/ROADMAP.md index 8c4fbb14..29e3f7ad 100644 --- a/ROADMAP.md +++ b/ROADMAP.md @@ -8,7 +8,16 @@ This document tracks planned improvements and features for Elroy. ## Future Items -(Items will be added here as they are identified) +### Self-Improvement Framework (Partial) +Enable Elroy to actively participate in its own development: +- **Usage analytics tracking**: Capture interaction outcomes (success/failure/retry patterns) +- **Analysis tools**: Tools to query usage patterns, review roadmap +- **Improvement proposals**: System for agent to suggest and prioritize improvements based on observed data +- **Development session mode**: Special mode where agent works on implementing improvements + +**Completed foundation**: +- ✅ `/introspect` skill - Agent can understand its own implementation +- ✅ `/make-improvement` skill - Agent can implement changes and submit PRs ## Completed @@ -22,12 +31,16 @@ This document tracks planned improvements and features for Elroy. - Configured automatic logging for slow operations (>100ms) ### Developer Experience -- **Create Claude Code skills for memory tools** (Completed: 2025-01) - - Built complete set of 6 Claude Code skills in `claude-skills/` directory - - `/remember` - Create long-term memories - - `/recall` - Search through memories - - `/list-memories` - List all active memories - - `/remind` - Create reminders - - `/list-reminders` - List active reminders - - `/ingest` - Ingest documents into memory - - Includes installation script with help documentation +- **Create Claude Code skills for memory and development tools** (Completed: 2025-01) + - Built complete set of 8 Claude Code skills in `claude-skills/` directory + - Memory management: + - `/remember` - Create long-term memories + - `/recall` - Search through memories + - `/list-memories` - List all active memories + - `/remind` - Create reminders + - `/list-reminders` - List active reminders + - `/ingest` - Ingest documents into memory + - Self-improvement capabilities: + - `/introspect` - Ask questions about Elroy's implementation (enables self-awareness) + - `/make-improvement` - Implement features/improvements and submit PRs (enables self-modification) + - Includes installation script and comprehensive documentation diff --git a/claude-skills/README.md b/claude-skills/README.md index 8ca21bb8..83fafa6d 100644 --- a/claude-skills/README.md +++ b/claude-skills/README.md @@ -6,6 +6,8 @@ This directory contains Claude Code skills that expose Elroy's memory management Elroy skills allow you to use Elroy's powerful memory management features directly from within Claude Code sessions. This creates a seamless integration between Claude Code and Elroy's long-term memory capabilities. +Additionally, development skills (`/introspect` and `/make-improvement`) enable Elroy to understand and improve itself - creating a self-improvement feedback loop. + ## Available Skills | Skill | Description | Usage | @@ -16,6 +18,8 @@ Elroy skills allow you to use Elroy's powerful memory management features direct | `/remind` | Create a reminder | `/remind "Review PR tomorrow"` | | `/list-reminders` | List active reminders | `/list-reminders` | | `/ingest` | Ingest documents | `/ingest docs/` | +| `/introspect` | Ask about Elroy's implementation | `/introspect "How does memory consolidation work?"` | +| `/make-improvement` | Implement and PR a feature | `/make-improvement "Add date-aware search"` | ## Installation @@ -111,6 +115,26 @@ Make documentation available to Elroy for context: /ingest specs/api-documentation.pdf ``` +### Self-Improvement Workflow + +Use the development skills to understand and improve Elroy: + +```bash +# Understand how something works +/introspect "How does memory consolidation work?" + +# Implement an improvement +/make-improvement "Add date-aware search to examine_memories" + +# The agent will: +# 1. Use /introspect to understand current implementation +# 2. Plan the change and ask for approval +# 3. Implement, test, and document the change +# 4. Submit a pull request +``` + +This creates a complete feedback loop where Elroy can understand and improve itself. + ## How It Works Each skill is a Claude Code skill (SKILL.md file with YAML frontmatter) that: diff --git a/claude-skills/install-skills.sh b/claude-skills/install-skills.sh index 48b8221f..0269edce 100755 --- a/claude-skills/install-skills.sh +++ b/claude-skills/install-skills.sh @@ -35,12 +35,14 @@ OPTIONS: --help, -h Show this help message INSTALLED SKILLS: - /remember - Create a long-term memory - /recall - Search through memories - /list-memories - List all memories - /remind - Create a reminder - /list-reminders - List active reminders - /ingest - Ingest documents into memory + /remember - Create a long-term memory + /recall - Search through memories + /list-memories - List all memories + /remind - Create a reminder + /list-reminders - List active reminders + /ingest - Ingest documents into memory + /introspect - Ask questions about Elroy's implementation + /make-improvement - Implement a feature or improvement and submit a PR REQUIREMENTS: - Elroy must be installed and available in PATH @@ -101,6 +103,8 @@ SKILLS=( "remind" "list-reminders" "ingest" + "introspect" + "make-improvement" ) # Uninstall if requested diff --git a/claude-skills/introspect/SKILL.md b/claude-skills/introspect/SKILL.md new file mode 100644 index 00000000..8e5562fb --- /dev/null +++ b/claude-skills/introspect/SKILL.md @@ -0,0 +1,80 @@ +--- +name: introspect +description: Ask questions about Elroy's implementation and codebase +disable-model-invocation: false +--- + +Ask questions about how Elroy is implemented. This skill helps you understand Elroy's codebase, architecture, and implementation details. + +## Usage + +When the user invokes this skill with `/introspect [QUERY]`, you should use the Task tool with the Explore agent to search through the Elroy codebase. + +**IMPORTANT**: You are currently working in the Elroy repository at `/Users/tombedor/development/elroy`. Use the Task tool to explore the codebase. + +### Example Task Tool Usage + +``` +Task tool with: +- subagent_type: "Explore" +- description: "Explore Elroy codebase" +- prompt: "" +- model: "sonnet" (for speed) +``` + +### What You Can Ask About + +- **Architecture**: "How does memory consolidation work?" +- **Implementation**: "Where is the memory recall classifier implemented?" +- **Data models**: "What database tables exist?" +- **Tools**: "How are tools registered and executed?" +- **Configuration**: "What configuration options are available?" +- **Features**: "How do reminders work?" +- **Performance**: "What latency tracking exists?" + +### Examples + +User query: `/introspect "How does memory consolidation work?"` + +You should: +1. Use the Task tool with Explore agent to search the codebase +2. Look for files related to consolidation (e.g., consolidation.py) +3. Examine relevant code +4. Provide a clear explanation of how it works + +User query: `/introspect "Where is the latency tracker used?"` + +You should: +1. Use Task/Explore to find latency.py and usage sites +2. Show where it's initialized and tracked +3. Explain the tracking mechanism + +### Response Format + +Provide: +1. **Summary**: Brief answer to the question +2. **Key Files**: List relevant file paths with line numbers +3. **How It Works**: Explanation of the implementation +4. **Related Code**: Any connected systems or patterns + +### Repository Structure + +The Elroy codebase is organized as: +- `elroy/core/` - Core infrastructure (logging, latency, context, tracing) +- `elroy/db/` - Database models and operations +- `elroy/repository/` - Data access layer (memories, reminders, messages) +- `elroy/tools/` - Agent tools and commands +- `elroy/messenger/` - Message processing and agent loop +- `elroy/cli/` - CLI interface +- `elroy/config/` - Configuration management +- `elroy/io/` - Input/output handling +- `claude-skills/` - Claude Code integration skills + +### Notes + +- Use the Explore agent for thorough searches across the codebase +- Include file paths with line numbers (e.g., `elroy/core/latency.py:45`) +- Explain not just "what" but "why" when possible +- Reference related systems and how they connect + +This skill enables self-awareness - helping the agent understand its own implementation. diff --git a/claude-skills/make-improvement/SKILL.md b/claude-skills/make-improvement/SKILL.md new file mode 100644 index 00000000..1dd1b020 --- /dev/null +++ b/claude-skills/make-improvement/SKILL.md @@ -0,0 +1,251 @@ +--- +name: make-improvement +description: Implement a feature or improvement to Elroy and submit a PR +disable-model-invocation: false +--- + +Implement an improvement to Elroy following a structured workflow. This skill guides you through understanding, implementing, testing, and submitting changes as a pull request. + +## Usage + +When the user invokes this skill with `/make-improvement [DESCRIPTION]`, follow this comprehensive workflow to implement and submit the change. + +**IMPORTANT**: You are working in the Elroy repository at `/Users/tombedor/development/elroy`. All commands should be run from this directory. + +## Workflow + +### Phase 1: Understanding & Planning + +#### 1.1 Understand Current Implementation +Use `/introspect` to understand relevant code: +``` +/introspect "How does [relevant system] work?" +``` + +#### 1.2 Review Context +- Check `ROADMAP.md` for alignment with project direction +- Check open issues: `gh issue list` +- Review recent commits: `git log --oneline -10` + +#### 1.3 Plan the Implementation +Create a clear implementation plan: +- What files will change? +- What new files are needed? +- What's the approach? +- Are there edge cases to consider? +- What tests are needed? + +**Ask the user for approval of the plan before proceeding.** + +### Phase 2: Implementation + +#### 2.1 Create Feature Branch +```bash +git checkout -b feature/[descriptive-name] +``` + +#### 2.2 Implement the Change +- Write clean, focused code +- Follow existing code style and patterns +- Add docstrings for new functions/classes +- Consider error handling and edge cases + +#### 2.3 Update Configuration (if needed) +If adding new config options: +- Add to relevant config dataclass in `elroy/core/configs.py` +- Document in `docs/configuration.md` + +### Phase 3: Testing + +#### 3.1 Write Tests +Add tests in `tests/` directory: +- Unit tests for new functionality +- Integration tests if needed +- Follow existing test patterns + +#### 3.2 Run Tests +```bash +just test +``` + +If tests fail, fix issues and re-run. + +#### 3.3 Run Type Checking +```bash +just typecheck +``` + +#### 3.4 Run Linting +```bash +just lint +``` + +Fix any issues found. + +### Phase 4: Documentation + +#### 4.1 Update Documentation +As appropriate: +- Update `README.md` if user-facing feature +- Update relevant docs in `docs/` +- Add examples if helpful +- Update `ROADMAP.md` (move item to Completed section) + +#### 4.2 Write Clear Commit Message +Follow the repository's commit message style (check recent commits): +- Brief summary (50 chars or less) +- Blank line +- Detailed explanation of what and why +- Reference any related issues + +### Phase 5: Submit PR + +#### 5.1 Commit Changes +```bash +git add [files] +git commit -m "$(cat <<'EOF' +[Imperative summary of change] + +[Detailed explanation of what was changed and why] + +[Any relevant notes, breaking changes, or migration steps] + +Co-Authored-By: Claude Sonnet 4.5 +EOF +)" +``` + +#### 5.2 Push Branch +```bash +git push -u origin feature/[descriptive-name] +``` + +#### 5.3 Create Pull Request +```bash +gh pr create --title "[PR Title]" --body "$(cat <<'EOF' +## Summary +[Brief description of what this PR does] + +## Changes +- [List of key changes] +- [Another change] + +## Testing +- [How this was tested] +- [What tests were added] + +## Documentation +- [Documentation updates made] + +## Related Issues +Closes #[issue-number] (if applicable) + +🤖 Generated with [Claude Code](https://claude.com/claude-code) +EOF +)" +``` + +#### 5.4 Return PR URL +Show the user the PR URL so they can review and merge. + +## Best Practices + +### Code Quality +- Follow existing patterns in the codebase +- Keep changes focused and minimal +- Don't refactor unrelated code +- Write self-documenting code with clear names + +### Testing +- Test both success and failure cases +- Test edge cases +- Ensure tests are reliable (not flaky) +- Use existing test fixtures and utilities + +### Documentation +- Document new configuration options +- Update user-facing docs for new features +- Include usage examples +- Keep documentation concise and clear + +### Git Hygiene +- One feature per PR +- Atomic commits (each commit should work) +- Clear commit messages +- Don't commit debug code or unnecessary changes + +## Example Workflow + +User: `/make-improvement "Add date-aware search to examine_memories"` + +You should: + +1. **Understand**: `/introspect "How does examine_memories work?"` +2. **Plan**: + - Modify `elroy/tools/memories/tools.py` to add date parsing + - Use existing `dateparser` library (already in dependencies for reminders) + - Add date range filter to vector search + - Fall back to text search if no dates detected +3. **Get approval**: "Here's my plan: [explain]. Does this look good?" +4. **Implement**: + - Create branch: `feature/date-aware-memory-search` + - Modify `examine_memories()` function + - Add helper function `extract_date_range()` +5. **Test**: + - Add tests in `tests/tools/test_memory_tools.py` + - Test various date formats + - Test fallback to text search + - Run `just test` +6. **Document**: + - Update tool docstring + - Add example to `docs/tools_guide.md` +7. **Submit**: + - Commit with clear message + - Push to origin + - Create PR with `gh pr create` + - Return PR URL + +## Troubleshooting + +### Tests Failing +- Read the test output carefully +- Fix the issue +- Re-run tests +- Don't skip failing tests + +### Type Errors +- Run `just typecheck` to see issues +- Fix type annotations +- Use `Optional[]` for nullable values +- Add type hints to new functions + +### Lint Errors +- Run `just lint` to see issues +- Run `just format` to auto-fix formatting +- Fix remaining issues manually + +### Merge Conflicts +- Pull latest main: `git pull origin main` +- Resolve conflicts +- Re-run tests +- Push updated branch + +## Configuration Files + +- **Tests**: Use `just test` (NOT direct pytest) +- **Linting**: Use `just lint` +- **Formatting**: Use `just format` +- **Type checking**: Use `just typecheck` + +See `CLAUDE.md` in the repository for more details on the build system. + +## Notes + +- Always use `just` commands, not direct tool invocation +- Check `justfile` with `just --list` for available commands +- Follow the repository's conventions (check existing code) +- Ask for user approval before major changes +- Keep the user informed of progress +- If stuck, use `/introspect` to understand more + +This skill completes the self-improvement loop - enabling Elroy to implement improvements to itself. diff --git a/docs/tools_guide.md b/docs/tools_guide.md index 9362af81..94415bd1 100644 --- a/docs/tools_guide.md +++ b/docs/tools_guide.md @@ -1,6 +1,8 @@ # Tools Guide -Elroy provides a set of tools that can be used by typing a forward slash (/) followed by the command name. These tools are organized into the following categories: +Elroy provides a set of tools that can be used by typing a forward slash (/) followed by the command name. These tools are organized into the following categories. + +**Note:** Many of these tools are also available as [Claude Code skills](../claude-skills/README.md) via slash commands like `/remember`, `/recall`, `/introspect`, etc. ## Memory Management @@ -32,6 +34,16 @@ Elroy provides a set of tools that can be used by typing a forward slash (/) fol | `/get_user_preferred_name` | Returns the user's preferred name. | | `/set_user_preferred_name` | Set the user's preferred name. Should predominantly be used relatively early in first conversations, and relatively rarely afterward. | +## Self-Improvement Tools + +| Tool/Command | Description | +|-------------|-------------| +| `/introspect` | Ask questions about Elroy's implementation and codebase. Returns detailed explanations with file paths and implementation details. Use this to understand how Elroy works before making changes. | +| `/make_improvement` | Implement a feature or improvement to Elroy following a structured workflow. Handles planning, implementation, testing, documentation, and PR submission. | +| `/review_roadmap` | Review the current roadmap, open GitHub issues, and recent commits. Use this to understand current priorities and what to work on next. | + +These tools enable Elroy to understand and improve itself. See [CLAUDE.md](../CLAUDE.md) for detailed usage. + ## Utility Tools | Tool/Command | Description | @@ -40,6 +52,7 @@ Elroy provides a set of tools that can be used by typing a forward slash (/) fol | `/tail_elroy_logs` | Returns the last `lines` of the Elroy logs. | | `/run_shell_command` | Run a shell command and return the output. | | `/make_coding_edit` | Makes an edit to code using a delegated coding LLM. Requires complete context in the instruction. | +| `/get_current_date` | Get the current date and time in ISO 8601 format. | ## Adding Custom Tools diff --git a/elroy/cli/main.py b/elroy/cli/main.py index 3c6bd047..f038ccb9 100644 --- a/elroy/cli/main.py +++ b/elroy/cli/main.py @@ -829,12 +829,14 @@ def install_skills( commands like /remember and /recall. Available skills: - - /remember - Create a long-term memory - - /recall - Search through memories - - /list-memories - List all memories - - /remind - Create a reminder - - /list-reminders - List active reminders - - /ingest - Ingest documents into memory + - /remember - Create a long-term memory + - /recall - Search through memories + - /list-memories - List all memories + - /remind - Create a reminder + - /list-reminders - List active reminders + - /ingest - Ingest documents into memory + - /introspect - Ask questions about Elroy's implementation + - /make-improvement - Implement a feature or improvement and submit a PR """ # Determine skills directory @@ -861,6 +863,8 @@ def install_skills( "remind", "list-reminders", "ingest", + "introspect", + "make-improvement", ] # Handle uninstall diff --git a/elroy/messenger/claude_code_integration.py b/elroy/messenger/claude_code_integration.py new file mode 100644 index 00000000..8c95e4d5 --- /dev/null +++ b/elroy/messenger/claude_code_integration.py @@ -0,0 +1,163 @@ +"""Integration layer for Claude Code skills accessible as Elroy tools. + +This module provides implementations for self-improvement tools that bridge +Elroy's native tool system with Claude Code-style workflows. +""" + +import subprocess +from pathlib import Path + +from ..core.ctx import ElroyContext +from ..core.logging import get_logger + +logger = get_logger(__name__) + + +def introspect_implementation(ctx: ElroyContext, query: str) -> str: + """Explore Elroy's codebase to answer implementation questions. + + This is the implementation for the introspect() tool. It uses similar + logic to the /introspect Claude Code skill. + + Args: + ctx: The Elroy context + query: Question about implementation + + Returns: + Detailed explanation of the implementation + """ + # Get the repository root (parent of elroy package) + repo_root = Path(__file__).parent.parent.parent + + # Build a prompt for code exploration + exploration_prompt = f"""Explore the Elroy codebase to answer this question: {query} + +Repository structure: +- elroy/core/ - Core infrastructure (logging, latency, context, tracing) +- elroy/db/ - Database models and operations +- elroy/repository/ - Data access layer (memories, reminders, messages) +- elroy/tools/ - Agent tools and commands +- elroy/messenger/ - Message processing and agent loop +- elroy/cli/ - CLI interface +- elroy/config/ - Configuration management + +Provide: +1. Summary - Brief answer to the question +2. Key Files - Relevant file paths with line numbers +3. How It Works - Explanation of the implementation +4. Related Code - Connected systems or patterns + +Focus on being thorough and accurate. Include specific file paths and line numbers. +""" + + # For now, return a structured response that guides the agent + # In a full implementation, this would use the Task/Explore agent + return f"""To answer "{query}", I need to explore the codebase. + +[Note: In production, this would use the Task/Explore agent to search the codebase. +For now, I can examine files directly using read/grep tools.] + +Starting exploration at: {repo_root} + +{exploration_prompt} + +I'll now use file reading and search tools to investigate this question. +""" + + +def implement_improvement( + ctx: ElroyContext, + description: str, + create_branch: bool = True, + run_tests: bool = True, + submit_pr: bool = True, +) -> str: + """Implement an improvement following the make-improvement workflow. + + This is the implementation for the make_improvement() tool. + + Args: + ctx: The Elroy context + description: Description of improvement + create_branch: Whether to create feature branch + run_tests: Whether to run tests + submit_pr: Whether to create PR + + Returns: + Summary of actions taken + """ + repo_root = Path(__file__).parent.parent.parent + + workflow_guide = f"""Implementing improvement: {description} + +Workflow: +1. ✓ Understanding current implementation (use introspect() tool) +2. ✓ Review roadmap and issues (use review_roadmap() tool) +3. Plan the implementation +4. Get user approval +5. {"Create feature branch" if create_branch else "Work on current branch"} +6. Implement changes +7. {"Run tests" if run_tests else "Skip tests"} +8. Update documentation +9. {"Submit PR" if submit_pr else "Commit locally"} + +Repository: {repo_root} + +I'll now begin the workflow. First, let me understand the current implementation +by using the introspect() tool to investigate related code. +""" + + return workflow_guide + + +def review_project_status(ctx: ElroyContext) -> str: + """Review roadmap, issues, and recent commits. + + Args: + ctx: The Elroy context + + Returns: + Summary of project status + """ + repo_root = Path(__file__).parent.parent.parent + roadmap_path = repo_root / "ROADMAP.md" + + sections = [] + + # Read roadmap + if roadmap_path.exists(): + with open(roadmap_path, "r") as f: + sections.append("=== ROADMAP ===") + sections.append(f.read()) + + # Get recent commits + try: + result = subprocess.run( + ["git", "log", "--oneline", "-10"], + cwd=repo_root, + capture_output=True, + text=True, + timeout=5, + ) + if result.returncode == 0: + sections.append("\n=== RECENT COMMITS ===") + sections.append(result.stdout) + except Exception as e: + logger.debug(f"Could not get git log: {e}") + + # Get open issues + try: + result = subprocess.run( + ["gh", "issue", "list", "--limit", "10"], + cwd=repo_root, + capture_output=True, + text=True, + timeout=5, + ) + if result.returncode == 0: + sections.append("\n=== OPEN ISSUES ===") + sections.append(result.stdout) + except Exception as e: + logger.debug(f"Could not get GitHub issues: {e}") + + return "\n".join(sections) diff --git a/elroy/tools/self_improvement.py b/elroy/tools/self_improvement.py new file mode 100644 index 00000000..6c32f459 --- /dev/null +++ b/elroy/tools/self_improvement.py @@ -0,0 +1,124 @@ +"""Tools for Elroy to understand and improve itself.""" + +from ..core.constants import tool +from ..core.ctx import ElroyContext + + +@tool +def introspect(ctx: ElroyContext, query: str) -> str: + """Ask questions about Elroy's implementation and codebase. + + Use this tool to understand how Elroy works internally. This helps when: + - Planning improvements or new features + - Understanding existing systems before modifying them + - Debugging issues or unexpected behavior + - Learning about architecture and design patterns + + The tool will explore the codebase and return a comprehensive explanation including: + - Summary of how the queried feature works + - Key file paths with line numbers + - Related systems and connections + - Implementation details and patterns + + Examples: + - "How does memory consolidation work?" + - "Where is the latency tracker used?" + - "What database tables exist?" + - "How are tools registered?" + - "How does the memory recall classifier work?" + + Args: + ctx: The Elroy context + query: Question about Elroy's implementation + + Returns: + Detailed explanation of the queried functionality + """ + from ..messenger.claude_code_integration import introspect_implementation + + return introspect_implementation(ctx, query) + + +@tool +def make_improvement( + ctx: ElroyContext, + description: str, + create_branch: bool = True, + run_tests: bool = True, + submit_pr: bool = True, +) -> str: + """Implement a feature or improvement to Elroy and optionally submit a PR. + + Use this tool to make improvements to Elroy following a structured workflow: + + 1. **Understanding & Planning** + - Uses introspect() to understand current implementation + - Reviews ROADMAP.md and GitHub issues + - Creates implementation plan + + 2. **Implementation** + - Creates feature branch (if create_branch=True) + - Implements the change + - Follows existing code patterns + + 3. **Testing** + - Writes tests for new functionality + - Runs `just test`, `just typecheck`, `just lint` (if run_tests=True) + + 4. **Documentation** + - Updates relevant documentation + - Updates ROADMAP.md + + 5. **Submission** + - Commits changes with clear message + - Pushes to remote + - Creates pull request (if submit_pr=True) + + Examples: + - "Add date-aware search to examine_memories" + - "Improve error messages for tool failures" + - "Add caching for embeddings responses" + + Args: + ctx: The Elroy context + description: Description of the improvement to make + create_branch: Whether to create a feature branch (default: True) + run_tests: Whether to run tests before submitting (default: True) + submit_pr: Whether to create a pull request (default: True) + + Returns: + Summary of what was done, including PR URL if submitted + """ + from ..messenger.claude_code_integration import implement_improvement + + return implement_improvement( + ctx, + description, + create_branch=create_branch, + run_tests=run_tests, + submit_pr=submit_pr, + ) + + +@tool +def review_roadmap(ctx: ElroyContext) -> str: + """Review the current roadmap, open issues, and recent commits. + + Use this tool to understand: + - What features are currently prioritized + - What improvements are planned + - What has been recently completed + - What GitHub issues are open + + This helps identify what to work on next and ensures improvements + align with project direction. + + Args: + ctx: The Elroy context + + Returns: + Summary of roadmap, issues, and recent work + """ + from ..messenger.claude_code_integration import review_project_status + + return review_project_status(ctx) diff --git a/elroy/tools/tools_and_commands.py b/elroy/tools/tools_and_commands.py index cc5086a1..0cb044c2 100644 --- a/elroy/tools/tools_and_commands.py +++ b/elroy/tools/tools_and_commands.py @@ -62,6 +62,7 @@ set_user_preferred_name, ) from .developer import create_bug_report, print_config, tail_elroy_logs +from .self_improvement import introspect, make_improvement, review_roadmap from .time import get_current_date IN_CONTEXT_MEMORY_COMMANDS: Set[Callable] = { @@ -96,6 +97,9 @@ set_user_preferred_name, get_current_date, get_fast_recall, + introspect, + make_improvement, + review_roadmap, } USER_ONLY_COMMANDS = { tail_elroy_logs, From 1acaa4fad56e57ad003b86d6e46437d6b9b3e140 Mon Sep 17 00:00:00 2001 From: elroy-bot Date: Fri, 30 Jan 2026 16:26:54 -0800 Subject: [PATCH 2/2] wip --- elroy/messenger/claude_code_integration.py | 119 +++++++++++---------- 1 file changed, 62 insertions(+), 57 deletions(-) diff --git a/elroy/messenger/claude_code_integration.py b/elroy/messenger/claude_code_integration.py index 8c95e4d5..215a800b 100644 --- a/elroy/messenger/claude_code_integration.py +++ b/elroy/messenger/claude_code_integration.py @@ -13,59 +13,40 @@ logger = get_logger(__name__) -def introspect_implementation(ctx: ElroyContext, query: str) -> str: +def introspect_implementation(ctx: ElroyContext, query: str) -> str: # noqa: ARG001 """Explore Elroy's codebase to answer implementation questions. - This is the implementation for the introspect() tool. It uses similar - logic to the /introspect Claude Code skill. + This tool is designed to work when Elroy is running inside a Claude Code session. + It returns a formatted request that Claude will see and act on by invoking + the /introspect skill. Args: ctx: The Elroy context query: Question about implementation Returns: - Detailed explanation of the implementation + Formatted request for Claude to invoke /introspect skill """ - # Get the repository root (parent of elroy package) - repo_root = Path(__file__).parent.parent.parent - - # Build a prompt for code exploration - exploration_prompt = f"""Explore the Elroy codebase to answer this question: {query} - -Repository structure: -- elroy/core/ - Core infrastructure (logging, latency, context, tracing) -- elroy/db/ - Database models and operations -- elroy/repository/ - Data access layer (memories, reminders, messages) -- elroy/tools/ - Agent tools and commands -- elroy/messenger/ - Message processing and agent loop -- elroy/cli/ - CLI interface -- elroy/config/ - Configuration management - -Provide: -1. Summary - Brief answer to the question -2. Key Files - Relevant file paths with line numbers -3. How It Works - Explanation of the implementation -4. Related Code - Connected systems or patterns - -Focus on being thorough and accurate. Include specific file paths and line numbers. -""" + # Find where the elroy package is located + elroy_path = Path(__file__).parent.parent.parent.resolve() - # For now, return a structured response that guides the agent - # In a full implementation, this would use the Task/Explore agent - return f"""To answer "{query}", I need to explore the codebase. + # Check if it's a git repository (helpful context) + is_git_repo = (elroy_path / ".git").exists() + location_type = "git repository" if is_git_repo else "installed package" -[Note: In production, this would use the Task/Explore agent to search the codebase. -For now, I can examine files directly using read/grep tools.] + # Return a formatted request that Claude will recognize + return f"""[INTROSPECT REQUEST] -Starting exploration at: {repo_root} +Location: {elroy_path} ({location_type}) +Query: {query} -{exploration_prompt} +Please invoke the /introspect skill to explore the Elroy codebase and answer this question. +The codebase is located at: {elroy_path} -I'll now use file reading and search tools to investigate this question. -""" +If the working directory is not already {elroy_path}, you may need to reference that path when exploring.""" -def implement_improvement( +def implement_improvement( # noqa: ARG001 ctx: ElroyContext, description: str, create_branch: bool = True, @@ -74,7 +55,9 @@ def implement_improvement( ) -> str: """Implement an improvement following the make-improvement workflow. - This is the implementation for the make_improvement() tool. + This tool is designed to work when Elroy is running inside a Claude Code session. + It returns a formatted request that Claude will see and act on by invoking + the /make-improvement skill. Args: ctx: The Elroy context @@ -84,33 +67,55 @@ def implement_improvement( submit_pr: Whether to create PR Returns: - Summary of actions taken + Formatted request for Claude to invoke /make-improvement skill """ - repo_root = Path(__file__).parent.parent.parent + repo_root = Path(__file__).parent.parent.parent.resolve() - workflow_guide = f"""Implementing improvement: {description} + # Verify it's a git repository (required for making improvements) + if not (repo_root / ".git").exists(): + return f"""[ERROR] Cannot make improvements to installed package -Workflow: -1. ✓ Understanding current implementation (use introspect() tool) -2. ✓ Review roadmap and issues (use review_roadmap() tool) -3. Plan the implementation -4. Get user approval -5. {"Create feature branch" if create_branch else "Work on current branch"} -6. Implement changes -7. {"Run tests" if run_tests else "Skip tests"} -8. Update documentation -9. {"Submit PR" if submit_pr else "Commit locally"} +The Elroy code is located at: {repo_root} -Repository: {repo_root} +This appears to be an installed package, not a git repository. +The make_improvement tool requires a git repository to: +- Create feature branches +- Commit changes +- Submit pull requests -I'll now begin the workflow. First, let me understand the current implementation -by using the introspect() tool to investigate related code. -""" +Please run Elroy from a development checkout of the repository.""" - return workflow_guide + workflow_options = [] + if create_branch: + workflow_options.append("create feature branch") + if run_tests: + workflow_options.append("run tests") + if submit_pr: + workflow_options.append("submit PR") + + options_str = ", ".join(workflow_options) if workflow_options else "commit locally only" + + return f"""[MAKE IMPROVEMENT REQUEST] + +Location: {repo_root} (git repository) +Description: {description} +Options: {options_str} + +Please invoke the /make-improvement skill with these parameters: +- create_branch={create_branch} +- run_tests={run_tests} +- submit_pr={submit_pr} + +The /make-improvement skill will: +1. Use /introspect to understand current implementation +2. Use /review-roadmap to check priorities +3. Plan the implementation +4. Get your approval +5. Implement the changes +6. {'Create feature branch, ' if create_branch else ''}implement, {'run tests, ' if run_tests else ''}and {'submit PR' if submit_pr else 'commit locally'}""" -def review_project_status(ctx: ElroyContext) -> str: +def review_project_status(ctx: ElroyContext) -> str: # noqa: ARG001 """Review roadmap, issues, and recent commits. Args: