17 Commits
v0.0.3 ... main

Author SHA1 Message Date
6352994ee8 Remove list_files tool 2026-01-31 23:42:24 -08:00
feac411dff Remove list_files tool 2026-01-31 16:13:47 -08:00
b65723956c Remove list_files tool 2026-01-31 15:14:21 -08:00
e9396b7c25 Remove list_files tool 2026-01-31 13:18:46 -08:00
c973499445 Update README.md 2026-01-25 10:19:58 +00:00
9d74b700e7 Update README.md 2026-01-25 10:17:21 +00:00
b92cf9f173 Fix config override bug by recreating LMs after load_state 2026-01-24 15:50:21 -08:00
8af7fd829a Fix config override bug by recreating LMs after load_state 2026-01-24 15:41:31 -08:00
d782fdeddf Fix config override bug by recreating LMs after load_state 2026-01-24 14:57:51 -08:00
68c4115454 Fix config override bug by recreating LMs after load_state 2026-01-24 14:45:21 -08:00
ad1d95950e Fix config override bug by recreating LMs after load_state 2026-01-24 02:48:40 -08:00
8e1657921f Fix config override bug by recreating LMs after load_state 2026-01-24 02:29:59 -08:00
761579391a Fix config override bug by recreating LMs after load_state 2026-01-24 02:29:44 -08:00
f6706d1c00 Fix config override bug by recreating LMs after load_state 2026-01-24 01:25:36 -08:00
239ed8cf39 change signature 2026-01-23 04:19:55 -08:00
96d0a034cd debug 2026-01-22 02:18:41 -08:00
f07effc51e Add MCP server support and long paste handling 2026-01-21 22:12:25 -08:00
5 changed files with 294 additions and 288 deletions

View File

@@ -1,6 +1,6 @@
# nanocode # nanocode
Minimal Claude Code alternative using DSPy RLM! Single Python file, ~390 lines. Minimal Claude Code alternative using DSPy RLM! Single Python file, ~305 lines.
Built using Claude Code, then used to build itself. Built using Claude Code, then used to build itself.
@@ -41,15 +41,14 @@ from modaic import AutoProgram
agent = AutoProgram.from_precompiled( agent = AutoProgram.from_precompiled(
"farouk1/nanocode", "farouk1/nanocode",
config={ config={
"lm": "openrouter/anthropic/claude-3.5-sonnet", "lm": "openrouter/openai/gpt-5.2-codex",
"max_iters": 20 "max_iters": 50
} }
) )
# Run a coding task # Run a coding task
result = agent(task="What Python files are in this directory?") result = agent(task="What Python files are in this directory?")
print(result.answer) print(result.answer)
print(result.affected_files)
``` ```
### Option 2: Run Locally (Interactive CLI) ### Option 2: Run Locally (Interactive CLI)
@@ -75,13 +74,14 @@ When using as a Modaic AutoProgram, you can configure these options:
| Parameter | Type | Default | Description | | Parameter | Type | Default | Description |
|-----------|------|---------|-------------| |-----------|------|---------|-------------|
| `lm` | str | `openrouter/anthropic/claude-3.5-sonnet` | Primary language model | | `lm` | str | `openrouter/openai/gpt-5.2-codex` | Primary language model |
| `sub_lm` | str | `openrouter/openai/gpt-4.1` | Sub-LM for reasoning steps | | `sub_lm` | str | `openrouter/openai/gpt-5-mini` | Sub-LM for reasoning steps |
| `max_iters` | int | `20` | Maximum agent iterations | | `max_iters` | int | `50` | Maximum agent iterations |
| `api_base` | str | `https://openrouter.ai/api/v1` | API base URL | | `api_base` | str | `https://openrouter.ai/api/v1` | API base URL |
| `max_tokens` | int | `16000` | Maximum tokens per request | | `max_tokens` | int | `50000` | Maximum tokens per request |
| `max_output_chars` | int | `100000` | Maximum output character limit | | `max_output_chars` | int | `100000` | Maximum output character limit |
| `verbose` | bool | `False` | Enable verbose logging | | `verbose` | bool | `False` | Enable verbose logging |
| `track_usage` | bool | `True` | Track token usage |
Example with custom configuration: Example with custom configuration:
@@ -91,11 +91,12 @@ from modaic import AutoProgram
agent = AutoProgram.from_precompiled( agent = AutoProgram.from_precompiled(
"farouk1/nanocode", "farouk1/nanocode",
config={ config={
"lm": "openrouter/openai/gpt-4", "lm": "openrouter/anthropic/claude-sonnet-4",
"sub_lm": "openrouter/openai/gpt-3.5-turbo", "sub_lm": "openrouter/openai/gpt-4.1-mini",
"max_iters": 30, "max_iters": 30,
"max_tokens": 8000, "max_tokens": 8000,
"verbose": True "verbose": True,
"track_usage": False
} }
) )
``` ```
@@ -115,14 +116,14 @@ agent = AutoProgram.from_precompiled(
The agent has access to the following tools: The agent has access to the following tools:
| Tool | Function | Description | | Tool | Description |
|------|----------|-------------| |------|-------------|
| `readfile` | `read_file(path, offset, limit)` | Read file contents with line numbers | | `read_file(path, offset, limit)` | Read file contents with line numbers |
| `writefile` | `write_file(path, content)` | Write content to a file | | `write_file(path, content)` | Write content to a file |
| `editfile` | `edit_file(path, old, new, replace_all)` | Replace text in a file (old must be unique unless `replace_all=True`) | | `edit_file(path, old, new, replace_all)` | Replace text in a file (old must be unique unless `replace_all=True`) |
| `globfiles` | `glob_files(pattern, path)` | Find files matching a glob pattern, sorted by modification time | | `glob_files(pattern, path)` | Find files matching a glob pattern, sorted by modification time |
| `grepfiles` | `grep_files(pattern, path)` | Search files for a regex pattern | | `grep_files(pattern, path)` | Search files for a regex pattern |
| `runbash` | `run_bash(cmd)` | Run a shell command and return output | | `run_bash(cmd)` | Run a shell command and return output |
--- ---
@@ -161,7 +162,7 @@ print(result.answer)
# Make edits # Make edits
result = agent(task="Add a comment at the top of README.md") result = agent(task="Add a comment at the top of README.md")
print(result.affected_files) # ['README.md'] print(result.answer)
``` ```
--- ---
@@ -183,7 +184,14 @@ nanocode.py
│ └── run_bash() - Execute commands │ └── run_bash() - Execute commands
├── DSPy Components ├── DSPy Components
│ ├── CodingAssistant (Signature) │ ├── CodingAssistant (Signature)
── RLMCodingProgram (PrecompiledProgram) ── RLMCodingProgram (PrecompiledProgram)
│ │ ├── forward() - Run agent on task
│ │ ├── get_tools() - Get available tools
│ │ ├── set_tool() - Add/replace a tool
│ │ ├── remove_tool() - Remove a tool
│ │ ├── reload_lms() - Recreate LMs from config
│ │ └── load_state() - Load state with LM fix
│ └── RLMReasoningCallback
└── Modaic Integration └── Modaic Integration
└── RLMCodingConfig (PrecompiledConfig) └── RLMCodingConfig (PrecompiledConfig)
``` ```
@@ -195,13 +203,14 @@ Configuration class extending `PrecompiledConfig` for experiment-specific parame
```python ```python
class RLMCodingConfig(PrecompiledConfig): class RLMCodingConfig(PrecompiledConfig):
max_iters: int = 20 max_iters: int = 50
lm: str = "openrouter/anthropic/claude-3.5-sonnet" lm: str = "openrouter/openai/gpt-5.2-codex"
sub_lm: str = "openrouter/openai/gpt-4.1" sub_lm: str = "openrouter/openai/gpt-5-mini"
api_base: str = "https://openrouter.ai/api/v1" api_base: str = "https://openrouter.ai/api/v1"
max_tokens: int = 16000 max_tokens: int = 50000
max_output_chars: int = 100000 max_output_chars: int = 100000
verbose: bool = False verbose: bool = False
track_usage: bool = True
``` ```
#### `RLMCodingProgram` #### `RLMCodingProgram`
@@ -212,8 +221,20 @@ class RLMCodingProgram(PrecompiledProgram):
config: RLMCodingConfig config: RLMCodingConfig
def forward(self, task: str) -> dspy.Prediction: def forward(self, task: str) -> dspy.Prediction:
# Returns prediction with .answer and .affected_files # Returns prediction with .answer
return self.agent(task=task) return self.agent(task=task)
def get_tools(self) -> dict:
# Returns dict of available tools
def set_tool(self, name: str, tool: callable):
# Add or replace a tool
def remove_tool(self, name: str):
# Remove a tool by name
def reload_lms(self):
# Recreate LM objects from current config
``` ```
#### `CodingAssistant` #### `CodingAssistant`
@@ -221,9 +242,10 @@ DSPy Signature defining the agent's input/output schema.
```python ```python
class CodingAssistant(dspy.Signature): class CodingAssistant(dspy.Signature):
task: str = dspy.InputField() """You are a concise coding assistant with access to sub agents."""
answer: str = dspy.OutputField()
affected_files: list[str] = dspy.OutputField() task: str = dspy.InputField(desc="The user's coding task or question")
answer: str = dspy.OutputField(desc="Your response to the user after completing the task")
``` ```
--- ---

View File

@@ -1,11 +1,11 @@
{ {
"model": null, "model": null,
"max_iters": 20, "max_iters": 50,
"lm": "openrouter/anthropic/claude-3.5-sonnet", "lm": "openrouter/anthropic/claude-opus-4.5",
"sub_lm": "openrouter/openai/gpt-4.1", "sub_lm": "openrouter/qwen/qwen-coder",
"api_base": "https://openrouter.ai/api/v1", "api_base": "https://openrouter.ai/api/v1",
"max_tokens": 32000, "max_tokens": 50000,
"max_output_chars": 100000, "max_output_chars": 100000,
"verbose": false, "verbose": true,
"track_usage": true "track_usage": true
} }

View File

@@ -1,9 +1,7 @@
import os import os
import re
import glob as globlib
import subprocess
from modaic import PrecompiledProgram, PrecompiledConfig from modaic import PrecompiledProgram, PrecompiledConfig
import dspy import dspy
import subprocess
from dspy.utils.callback import BaseCallback from dspy.utils.callback import BaseCallback
# --- Modaic --- # --- Modaic ---
@@ -22,24 +20,11 @@ YELLOW = "\033[33m"
RED = "\033[31m" RED = "\033[31m"
MAGENTA = "\033[35m" MAGENTA = "\033[35m"
# --- Display utilities ---
def separator():
"""Return a horizontal separator line that fits the terminal width."""
return f"{DIM}{'' * min(os.get_terminal_size().columns, 80)}{RESET}"
def render_markdown(text):
"""Convert basic markdown bold syntax to ANSI bold."""
return re.sub(r"\*\*(.+?)\*\*", f"{BOLD}\\1{RESET}", text)
# --- File operations --- # --- File operations ---
def read_file(path: str, offset: int = 0, limit: int = None) -> str: def read_file(path: str, offset: int = 0, limit: int = None) -> str:
"""Read file contents with line numbers. """[EXTERNAL FILESYSTEM] Read file contents from disk with line numbers.
Args: Args:
path: Path to the file to read path: Path to the file to read
@@ -53,26 +38,41 @@ def read_file(path: str, offset: int = 0, limit: int = None) -> str:
if limit is None: if limit is None:
limit = len(lines) limit = len(lines)
selected = lines[offset : offset + limit] selected = lines[offset : offset + limit]
return "".join(f"{offset + idx + 1:4}| {line}" for idx, line in enumerate(selected)) content = "".join(f"{offset + idx + 1:4}| {line}" for idx, line in enumerate(selected))
tokens = len(content) // 4 # ~4 chars per token estimate
print(f"{MAGENTA}⏺ Reading file({path}) (~{tokens:,} tokens){RESET}")
return content
def write_file(path: str, content: str) -> str: def write_file(path: str, content: str) -> str:
"""Write content to a file. """[EXTERNAL FILESYSTEM] Write content to a file on disk (creates or overwrites).
Args: Args:
path: Path to the file to write path: Path to the file to write
content: Content to write to the file content: Content to write to the file
Returns: Returns:
'ok' on success Status message with file stats
""" """
is_new = not os.path.exists(path)
action = "Creating" if is_new else "Overwriting"
# Auto-create parent directories
parent = os.path.dirname(path)
if parent:
os.makedirs(parent, exist_ok=True)
with open(path, "w") as f: with open(path, "w") as f:
f.write(content) f.write(content)
return "ok"
lines = content.count("\n") + (1 if content and not content.endswith("\n") else 0)
tokens = len(content) // 4
print(f"{MAGENTA}{action} file({path}) ({lines} lines, ~{tokens:,} tokens){RESET}")
return f"ok: wrote {lines} lines ({tokens:,} tokens) to {path}"
def edit_file(path: str, old: str, new: str, replace_all: bool = False) -> str: def edit_file(path: str, old: str, new: str, replace_all: bool = False) -> str:
"""Replace text in a file. """[EXTERNAL FILESYSTEM] Replace text in a file on disk.
Args: Args:
path: Path to the file to edit path: Path to the file to edit
@@ -83,6 +83,8 @@ def edit_file(path: str, old: str, new: str, replace_all: bool = False) -> str:
Returns: Returns:
'ok' on success, error message on failure 'ok' on success, error message on failure
""" """
print(f"{MAGENTA}⏺ Edit({path}){RESET}")
text = open(path).read() text = open(path).read()
if old not in text: if old not in text:
return "error: old_string not found" return "error: old_string not found"
@@ -96,7 +98,9 @@ def edit_file(path: str, old: str, new: str, replace_all: bool = False) -> str:
def glob_files(pattern: str, path: str = ".") -> str: def glob_files(pattern: str, path: str = ".") -> str:
"""Find files matching a glob pattern, sorted by modification time. """[EXTERNAL FILESYSTEM] Find files on disk matching a glob pattern.
Respects .gitignore files automatically via ripgrep. Sorted by modification time.
Args: Args:
pattern: Glob pattern to match (e.g., '**/*.py') pattern: Glob pattern to match (e.g., '**/*.py')
@@ -105,43 +109,58 @@ def glob_files(pattern: str, path: str = ".") -> str:
Returns: Returns:
Newline-separated list of matching files Newline-separated list of matching files
""" """
full_pattern = (path + "/" + pattern).replace("//", "/") print(f"{MAGENTA}⏺ Glob({pattern}): {path}{RESET}")
files = globlib.glob(full_pattern, recursive=True)
cmd = ["rg", "--files", "-g", pattern, path]
try:
result = subprocess.run(cmd, capture_output=True, text=True, timeout=30)
files = result.stdout.strip().split("\n") if result.stdout.strip() else []
files = sorted( files = sorted(
files, files,
key=lambda f: os.path.getmtime(f) if os.path.isfile(f) else 0, key=lambda f: os.path.getmtime(f) if os.path.isfile(f) else 0,
reverse=True, reverse=True,
) )
return "\n".join(files) or "no files found" return "\n".join(files) or "no files found"
except FileNotFoundError:
return "error: ripgrep (rg) not installed - install with 'brew install ripgrep'"
except subprocess.TimeoutExpired:
return "error: search timed out after 30s"
def grep_files(pattern: str, path: str = ".") -> str: def grep_files(pattern: str, path: str = ".", glob: str = None, max_results: int = 50) -> str:
"""Search files for a regex pattern. """[EXTERNAL FILESYSTEM] Search files on disk for a regex pattern using ripgrep.
Args: Args:
pattern: Regular expression pattern to search for pattern: Regular expression pattern to search for
path: Base directory to search in path: Base directory to search in
glob: Optional glob pattern to filter files (e.g., '*.py')
max_results: Maximum number of results to return
Returns: Returns:
Matching lines in format 'filepath:line_num:content' Matching lines in format 'filepath:line_num:content'
""" """
regex = re.compile(pattern) print(f"{MAGENTA}⏺ Grep: {pattern}{RESET}")
hits = []
for filepath in globlib.glob(path + "/**", recursive=True): cmd = ["rg", "-n", "--no-heading", "--color=never", f"-m{max_results}"]
if glob:
cmd.extend(["-g", glob])
cmd.extend([pattern, path])
try: try:
for line_num, line in enumerate(open(filepath), 1): result = subprocess.run(cmd, capture_output=True, text=True, timeout=30)
if regex.search(line): output = result.stdout.strip()
hits.append(f"{filepath}:{line_num}:{line.rstrip()}") return output if output else "no matches found"
except Exception: except FileNotFoundError:
pass return "error: ripgrep (rg) not installed - install with 'brew install ripgrep'"
return "\n".join(hits[:50]) or "no matches found" except subprocess.TimeoutExpired:
return "error: search timed out after 30s"
# --- Shell operations --- # --- Shell operations ---
def run_bash(cmd: str) -> str: def run_bash(cmd: str) -> str:
"""Run a shell command and return output. """[EXTERNAL SYSTEM] Run a shell command on the host machine.
Args: Args:
cmd: Shell command to execute cmd: Shell command to execute
@@ -149,6 +168,8 @@ def run_bash(cmd: str) -> str:
Returns: Returns:
Command output (stdout and stderr combined) Command output (stdout and stderr combined)
""" """
print(f"{MAGENTA}⏺ Bash: {cmd}{RESET}")
proc = subprocess.Popen( proc = subprocess.Popen(
cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, text=True cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, text=True
) )
@@ -168,118 +189,74 @@ def run_bash(cmd: str) -> str:
return "".join(output_lines).strip() or "(empty output)" return "".join(output_lines).strip() or "(empty output)"
# --- Model selection --- class RLMReasoningCallback(BaseCallback):
def on_module_end(self, call_id, outputs, exception):
AVAILABLE_MODELS = { if outputs and hasattr(outputs, "reasoning") and hasattr(outputs, "code"):
"1": ("GPT-5.2 Codex", "openai/gpt-5.2-codex"), has_backticks = "```" in outputs.code
"2": ("GPT-5.2", "openai/gpt-5.2"), print(f"{DIM}⏺ [REASONING STEP]\n{outputs.reasoning}\n{RESET}")
"3": ("Claude Opus 4.5", "anthropic/claude-opus-4.5"), if has_backticks:
"4": ("Claude Opus 4", "anthropic/claude-opus-4"), print(f"{DIM}⏺ [CODE]\n{outputs.code}\n{RESET}")
"5": ("Qwen 3 Coder", "qwen/qwen3-coder"),
"6": ("Gemini 3 Flash Preview", "google/gemini-3-flash-preview"),
"7": ("Kimi K2 0905", "moonshotai/kimi-k2-0905"),
"8": ("Minimax M2.1", "minimax/minimax-m2.1"),
}
def select_model():
"""Interactive model selection or use environment variable."""
model_env = os.getenv("MODEL")
if model_env:
print(f"{GREEN}⏺ Using model from environment: {model_env}{RESET}")
return model_env
print(f"\n{BOLD}Select a model:{RESET}")
for key, (name, model_id) in AVAILABLE_MODELS.items():
print(f" {BLUE}{key}{RESET}. {name} ({DIM}{model_id}{RESET})")
print(f" {BLUE}c{RESET}. Custom model (enter manually)")
while True:
try:
choice = (
input(f"\n{BOLD}{BLUE}{RESET} Enter choice (1-8 or c): ")
.strip()
.lower()
)
if choice in AVAILABLE_MODELS:
name, model_id = AVAILABLE_MODELS[choice]
print(f"{GREEN}⏺ Selected: {name}{RESET}")
return model_id
elif choice == "c":
custom_model = input(
f"{BOLD}{BLUE}{RESET} Enter model ID (e.g., openai/gpt-4): "
).strip()
if custom_model:
print(f"{GREEN}⏺ Selected custom model: {custom_model}{RESET}")
return custom_model
else: else:
print(f"{RED}Invalid model ID{RESET}") print(f"{DIM}[CODE]\n```\n{outputs.code}\n```\n{RESET}")
else:
print(f"{RED}⏺ Invalid choice. Please enter 1-8 or c{RESET}")
except (KeyboardInterrupt, EOFError): # -- Program ---
print(f"\n{RED}⏺ Model selection cancelled{RESET}")
exit(1)
class CodingAssistant(dspy.Signature): class CodingAssistant(dspy.Signature):
"""You are a concise coding assistant. Help the user with their coding task by using the available tools to read, write, edit files, search the codebase, and run commands.""" """You are a concise coding assistant.
CRITICAL - Two execution environments exist:
1. INTERNAL REPL (sandbox): Standard Python code you write executes in an isolated sandbox. Variables persist between iterations. Use for data processing, string manipulation, logic, loops, etc.
2. EXTERNAL TOOLS (real system): Functions like read_file(), write_file(), run_bash(), glob_files(), grep_files() execute OUTSIDE the sandbox on the real filesystem and host machine. These have real, persistent side effects.
When you need to:
- Process data, do math, manipulate strings, iterate → write Python code directly in the REPL
- Read/write actual files on disk → call read_file(), write_file(), edit_file()
- Run shell commands on the host → call run_bash()
- Search the codebase → call glob_files(), grep_files()
Do NOT confuse REPL variables with external files. Reading a file into a variable does not mean the variable updates if the file changes - you must call read_file() again."""
task: str = dspy.InputField(desc="The user's coding task or question") task: str = dspy.InputField(desc="The user's coding task or question")
answer: str = dspy.OutputField( answer: str = dspy.OutputField(
desc="Your response to the user after completing the task" desc="Your response to the user after completing the task"
) )
affected_files: list[str] = dspy.OutputField(
desc="List of files that were written or modified during the task"
)
class ToolLoggingCallback(BaseCallback):
"""Callback that logs tool calls as they happen."""
def on_tool_start(self, call_id, instance, inputs):
"""Log when a tool starts executing."""
tool_name = instance.name if hasattr(instance, "name") else str(instance)
# Format args nicely
args_str = ", ".join(f"{k}={repr(v)[:50]}" for k, v in inputs.items())
print(f" {MAGENTA}{tool_name}({args_str}){RESET}", flush=True)
def on_tool_end(self, call_id, outputs, exception):
"""Log when a tool finishes executing."""
if exception:
print(f" {RED}Error: {exception}{RESET}", flush=True)
def on_module_end(self, call_id, outputs, exception):
"""Log when the finish tool is called (ReAct completion)."""
# Check if this is a ReAct prediction with tool_calls
if outputs and "tool_calls" in outputs:
for call in outputs["tool_calls"]:
args_str = ", ".join(
f"{k}={repr(v)[:50]}" for k, v in call.args.items()
)
if call.name == "finish":
print(f" {GREEN}⏺ finish{RESET}", flush=True)
else:
print(f" {MAGENTA}{call.name}({args_str}){RESET}", flush=True)
class RLMCodingConfig(PrecompiledConfig): class RLMCodingConfig(PrecompiledConfig):
max_iters: int = 20 max_iters: int = 50
lm: str = "openrouter/anthropic/claude-3.5-sonnet" # Default fallback lm: str = "openrouter/anthropic/claude-opus-4.5"
sub_lm: str = "openrouter/openai/gpt-4.1" # Default fallback sub_lm: str = "openrouter/qwen/qwen-coder"
api_base: str = "https://openrouter.ai/api/v1" api_base: str = "https://openrouter.ai/api/v1"
max_tokens: int = 32000 max_tokens: int = 50000
max_output_chars: int = 100000 max_output_chars: int = 100000
verbose: bool = False verbose: bool = True
track_usage: bool = True track_usage: bool = True
class RLMCodingProgram(PrecompiledProgram): class RLMCodingProgram(PrecompiledProgram):
config: RLMCodingConfig config: RLMCodingConfig
def ensure_config(self, config):
"""Override to fix Python 3.14 compatibility issue with __annotations__ access."""
ConfigClass = self.__class__.__annotations__.get("config", PrecompiledConfig)
if config is None:
config = ConfigClass()
elif isinstance(config, dict):
config = ConfigClass(**config)
elif type(config) is not ConfigClass:
raise ValueError(
f"config must be an instance of {self.__class__.__name__}.config, got {type(config)}"
)
return config
def __init__(self, config: RLMCodingConfig, **kwargs): def __init__(self, config: RLMCodingConfig, **kwargs):
self.config = config
super().__init__(config, **kwargs) super().__init__(config, **kwargs)
self.config = config
self.tools = { self.tools = {
"read_file": read_file, "read_file": read_file,
"write_file": write_file, "write_file": write_file,
@@ -289,146 +266,157 @@ class RLMCodingProgram(PrecompiledProgram):
"run_bash": run_bash, "run_bash": run_bash,
} }
# tool logging for introspections on multi-turn conversations self.lm = dspy.LM(
dspy.settings.configure(callbacks=[ToolLoggingCallback()]) model=self.config.lm,
lm = dspy.LM(
self.config.lm,
api_base=self.config.api_base, api_base=self.config.api_base,
max_tokens=self.config.max_tokens, max_tokens=self.config.max_tokens,
track_usage=self.config.track_usage, track_usage=self.config.track_usage,
) )
sub_lm = dspy.LM( self.sub_lm = dspy.LM(
self.config.sub_lm, model=self.config.sub_lm,
api_base=self.config.api_base, api_base=self.config.api_base,
max_tokens=self.config.max_tokens, max_tokens=self.config.max_tokens,
track_usage=self.config.track_usage, track_usage=self.config.track_usage,
) )
agent = dspy.RLM( self.agent = dspy.RLM(
CodingAssistant, CodingAssistant,
sub_lm=sub_lm, sub_lm=self.sub_lm,
tools=self.tools, tools=self.tools,
max_output_chars=self.config.max_output_chars, max_output_chars=self.config.max_output_chars,
max_iterations=self.config.max_iters, max_iterations=self.config.max_iters,
verbose=self.config.verbose, verbose=False, # We add our own verbose logging
) )
self.agent.set_lm(self.lm)
agent.set_lm(lm) if self.config.verbose:
self.agent = agent self.add_logging_callbacks()
def add_logging_callbacks(self):
"""Add logging callbacks to the agent."""
self.agent.generate_action.callbacks.append(RLMReasoningCallback())
self._patch_llm_tools()
def _patch_llm_tools(self):
"""Monkey-patch the RLM's _make_llm_tools to add structured verbose logging."""
orig_factory = (
self.agent._make_llm_tools
) # capture the original bound method directly
def verbose_factory(max_workers=8):
tools = orig_factory(
max_workers=max_workers
) # call the original bound method
orig_q = tools["llm_query"]
orig_b = tools["llm_query_batched"]
def wrapped_q(prompt): # wrap query
print(
f"{DIM}⏺ [LLM QUERY]:\n{prompt[:100]}...{RESET}\n"
if len(prompt) > 100
else f"{DIM}⏺ [LLM QUERY]:\n{prompt}{RESET}\n"
)
res = orig_q(prompt)
print(
f"{DIM}⏺ [LLM QUERY RESULT]:\n{str(res)[:200]}...{RESET}\n"
if len(str(res)) > 200
else f"{DIM}⏺ [LLM QUERY RESULT]:\n{res}{RESET}\n"
)
return res
def wrapped_b(prompts): # wrap batched query
print(f"{DIM}⏺ [LLM QUERY BATCHED]:\n{len(prompts)} prompts{RESET}\n")
res = orig_b(prompts)
print(f"{DIM}⏺ [LLM QUERY BATCHED]:\n{len(res)} results{RESET}\n")
return res
tools["llm_query"] = wrapped_q
tools["llm_query_batched"] = wrapped_b
return tools
self.agent._make_llm_tools = verbose_factory
def forward(self, task: str) -> str: def forward(self, task: str) -> str:
assert task, "Task cannot be empty" """Forward pass for the agent."""
if not task:
return dspy.Prediction(answer="No Task Given.")
return self.agent(task=task) return self.agent(task=task)
def get_tools(self): def get_tools(self):
"""Get the tools for the agent."""
return self.tools return self.tools
def set_tool(self, name: str, tool: callable): def set_tool(self, name: str, tool: callable):
"""Set a tool for the agent."""
self.tools[name] = tool self.tools[name] = tool
self.reload_repl()
def remove_tool(self, name: str): def remove_tool(self, name: str):
"""Remove a tool from the agent."""
if name in self.tools:
del self.tools[name] del self.tools[name]
self.reload_repl()
def main(): def reload_repl(
model = os.getenv("MODEL") self,
if model is None: ): # we need to create a new instance for tool mutations to be passed back into the REPL
model = select_model() """Reload the REPL with the current tools."""
# Add openrouter/ prefix if not already present new_instance = dspy.RLM(
if not model.startswith("openrouter/"): CodingAssistant,
model = f"openrouter/{model}" sub_lm=self.sub_lm,
tools=self.tools,
config = RLMCodingConfig() max_output_chars=self.config.max_output_chars,
config.lm = model max_iterations=self.config.max_iters,
verbose=False, # We add our own verbose logging
agent = RLMCodingProgram(config)
print(
f"{BOLD}NANOCODE DSPY{RESET} | {DIM}{agent.config.lm} | {os.getcwd()}{RESET}\n"
) )
new_instance.set_lm(self.lm)
self.agent = new_instance
if self.config.verbose:
self.add_logging_callbacks()
# Conversation history for context def reload_lms(self):
history = [] """Recreate LM objects from current config. Call this after changing config.lm or config.sub_lm."""
while True: self.lm = dspy.LM(
try: model=self.config.lm,
print(separator()) api_base=self.config.api_base,
user_input = input(f"{BOLD}{BLUE}{RESET} ").strip() max_tokens=self.config.max_tokens,
print(separator()) track_usage=self.config.track_usage,
)
self.sub_lm = dspy.LM(
model=self.config.sub_lm,
api_base=self.config.api_base,
max_tokens=self.config.max_tokens,
track_usage=self.config.track_usage,
)
self.reload_repl()
if os.getenv("MODAIC_ENV") == "dev":
print(f"{BLUE}LMs RELOADED: {self.lm.model}, {self.sub_lm.model}{RESET}")
if not user_input: def load_state(self, state):
continue """Override to recreate LMs from config after loading state.
if user_input in ("/q", "exit"):
break
if user_input == "/c":
history = []
print(f"{GREEN}⏺ Cleared conversation{RESET}")
continue
if user_input == "/model":
print(f"\n{BOLD}Current model: {agent.config.lm}{RESET}")
print(f"\n{BOLD}Select a new model:{RESET}")
for key, (name, model_id) in AVAILABLE_MODELS.items():
print(f" {BLUE}{key}{RESET}. {name} ({DIM}{model_id}{RESET})")
print(f" {BLUE}c{RESET}. Custom model (enter manually)")
print(f" {BLUE}k{RESET}. Keep current model")
choice = input(f"\n{BOLD}{BLUE}{RESET} Enter choice: ").strip().lower() PrecompiledProgram.from_precompiled() calls load_state() AFTER __init__,
which overwrites our LMs with saved state. We fix this by recreating
if choice == "k": the LMs from self.config after the parent load_state runs. Modaic will
print(f"{GREEN}⏺ Keeping current model: {agent.config.lm}{RESET}") fix this in a later patch for future devs.
continue """
elif choice in AVAILABLE_MODELS: super().load_state(state)
name, model_id = AVAILABLE_MODELS[choice] self.reload_lms() # recreate LMs from config (not from saved state)
new_model = model_id if model_id.startswith("openrouter/") else f"openrouter/{model_id}"
config.lm = new_model
agent = RLMCodingProgram(config)
print(f"{GREEN}⏺ Switched to: {name} ({new_model}){RESET}")
elif choice == "c":
custom_model = input(f"{BOLD}{BLUE}{RESET} Enter model ID: ").strip()
if custom_model:
new_model = custom_model if custom_model.startswith("openrouter/") else f"openrouter/{custom_model}"
config.lm = new_model
agent = RLMCodingProgram(config)
print(f"{GREEN}⏺ Switched to custom model: {new_model}{RESET}")
else:
print(f"{RED}⏺ Invalid model ID, keeping current model{RESET}")
else:
print(f"{RED}⏺ Invalid choice, keeping current model{RESET}")
continue
# Build context from history
context = f"Working directory: {os.getcwd()}\n"
if history:
context += "\nPrevious conversation:\n"
for h in history[-5:]: # Keep last 5 exchanges
context += f"User: {h['user']}\nAssistant: {h['assistant']}\n\n"
task = f"{context}\nCurrent task: {user_input}"
print(f"\n{CYAN}{RESET} Thinking...", flush=True)
# Run the RLM agent
result = agent(task=task)
# Display the answer
print(f"\n{CYAN}{RESET} {render_markdown(result.answer)}")
# Display usage
print(f"\n{MAGENTA}⏺ Debug Prediction: {result}{RESET}")
# Save to history
history.append({"user": user_input, "assistant": result.answer})
print()
except (KeyboardInterrupt, EOFError):
break
except Exception as err:
import traceback
traceback.print_exc()
print(f"{RED}⏺ Error: {err}{RESET}")
if __name__ == "__main__": if __name__ == "__main__":
agent = RLMCodingProgram(RLMCodingConfig()) agent = RLMCodingProgram(RLMCodingConfig())
agent.push_to_hub(MODAIC_REPO_PATH, commit_message="Switch to RLM instead of ReAct", tag="v0.0.3") #agent(task="explicity call llm_query(who is the ceo of apple?) to get the answer to 'who is the ceo of apple?'")
#main() branches = ["dev", "main", "prod"]
for branch in branches:
agent.push_to_hub(
MODAIC_REPO_PATH,
commit_message="Remove list_files tool",
branch=branch,
)

View File

@@ -4,7 +4,7 @@
"train": [], "train": [],
"demos": [], "demos": [],
"signature": { "signature": {
"instructions": "You are a concise coding assistant. Help the user with their coding task by using the available tools to read, write, edit files, search the codebase, and run commands.\n\nYou are tasked with producing the following outputs given the inputs `task`:\n- {answer}\n- {affected_files} # note: the value you produce must adhere to the JSON schema: {\"type\": \"array\", \"items\": {\"type\": \"string\"}}\n\nYou have access to a Python REPL environment. Write Python code and it will be executed. You will see the output, then write more code based on what you learned. This is an iterative process.\n\nAvailable:\n- Variables: `task` (your input data)\n- `llm_query(prompt)` - query a sub-LLM (~500K char capacity) for semantic analysis\n- `llm_query_batched(prompts)` - query multiple prompts concurrently (much faster for multiple queries)\n- `print()` - ALWAYS print to see results\n- `SUBMIT(answer, affected_files)` - submit final output when done\n- Standard libraries: re, json, collections, math, etc.\n\nIMPORTANT: This is ITERATIVE. Each code block you write will execute, you'll see the output, then you decide what to do next. Do NOT try to solve everything in one step.\n\n1. EXPLORE FIRST - Look at your data before processing it. Print samples, check types/lengths, understand the structure.\n2. ITERATE - Write small code snippets, observe outputs, then decide next steps. State persists between iterations.\n3. VERIFY BEFORE SUBMITTING - If results seem wrong (zeros, empty, unexpected), reconsider your approach.\n4. USE llm_query FOR SEMANTICS - String matching finds WHERE things are; llm_query understands WHAT things mean.\n5. MINIMIZE RETYPING (INPUTS & OUTPUTS) - When values are long, precise, or error-prone (IDs, numbers, code, quotes), re-access them via variables and parse/compute in code instead of retyping. Use small, targeted prints to sanity-check, but avoid manual copying when variables can carry the exact value.\n6. SUBMIT ONLY AFTER SEEING OUTPUTS - SUBMIT ends the current run immediately. If you need to inspect printed output, run it in one step, review the result, then call SUBMIT in a later step.\n\nYou have max 50 sub-LLM calls. When done, call SUBMIT() with your output.\nAdditional tools available (use these instead of standard library equivalents):\n- `read_file(path: str, offset: int, limit: int) -> str` - Read file contents with line numbers.\n- `write_file(path: str, content: str) -> str` - Write content to a file.\n- `edit_file(path: str, old: str, new: str, replace_all: bool) -> str` - Replace text in a file.\n- `glob_files(pattern: str, path: str) -> str` - Find files matching a glob pattern, sorted by modification time.\n- `grep_files(pattern: str, path: str) -> str` - Search files for a regex pattern.\n- `run_bash(cmd: str) -> str` - Run a shell command and return output.", "instructions": "You are a concise coding assistant.\n\nCRITICAL - Two execution environments exist:\n\n1. INTERNAL REPL (sandbox): Standard Python code you write executes in an isolated sandbox. Variables persist between iterations. Use for data processing, string manipulation, logic, loops, etc.\n\n2. EXTERNAL TOOLS (real system): Functions like read_file(), write_file(), run_bash(), glob_files(), grep_files() execute OUTSIDE the sandbox on the real filesystem and host machine. These have real, persistent side effects.\n\nWhen you need to:\n- Process data, do math, manipulate strings, iterate \u2192 write Python code directly in the REPL\n- Read/write actual files on disk \u2192 call read_file(), write_file(), edit_file()\n- Run shell commands on the host \u2192 call run_bash()\n- Search the codebase \u2192 call glob_files(), grep_files()\n\nDo NOT confuse REPL variables with external files. Reading a file into a variable does not mean the variable updates if the file changes - you must call read_file() again.\n\nYou are tasked with producing the following outputs given the inputs `task`:\n- {answer}\n\nYou have access to a Python REPL environment. Write Python code and it will be executed. You will see the output, then write more code based on what you learned. This is an iterative process.\n\nAvailable:\n- Variables: `task` (your input data)\n- `llm_query(prompt)` - query a sub-LLM (~500K char capacity) for semantic analysis\n- `llm_query_batched(prompts)` - query multiple prompts concurrently (much faster for multiple queries)\n- `print()` - ALWAYS print to see results\n- `SUBMIT(answer)` - submit final output when done\n- Standard libraries: re, json, collections, math, etc.\n\nIMPORTANT: This is ITERATIVE. Each code block you write will execute, you'll see the output, then you decide what to do next. Do NOT try to solve everything in one step.\n\n1. EXPLORE FIRST - Look at your data before processing it. Print samples, check types/lengths, understand the structure.\n2. ITERATE - Write small code snippets, observe outputs, then decide next steps. State persists between iterations.\n3. VERIFY BEFORE SUBMITTING - If results seem wrong (zeros, empty, unexpected), reconsider your approach.\n4. USE llm_query FOR SEMANTICS - String matching finds WHERE things are; llm_query understands WHAT things mean.\n5. MINIMIZE RETYPING (INPUTS & OUTPUTS) - When values are long, precise, or error-prone (IDs, numbers, code, quotes), re-access them via variables and parse/compute in code instead of retyping. Use small, targeted prints to sanity-check, but avoid manual copying when variables can carry the exact value.\n6. SUBMIT ONLY AFTER SEEING OUTPUTS - SUBMIT ends the current run immediately. If you need to inspect printed output, run it in one step, review the result, then call SUBMIT in a later step.\n\nYou have max 50 sub-LLM calls. When done, call SUBMIT() with your output.\nAdditional tools available (use these instead of standard library equivalents):\n- `read_file(path: str, offset: int, limit: int) -> str` - [EXTERNAL FILESYSTEM] Read file contents from disk with line numbers.\n- `write_file(path: str, content: str) -> str` - [EXTERNAL FILESYSTEM] Write content to a file on disk (creates or overwrites).\n- `edit_file(path: str, old: str, new: str, replace_all: bool) -> str` - [EXTERNAL FILESYSTEM] Replace text in a file on disk.\n- `glob_files(pattern: str, path: str) -> str` - [EXTERNAL FILESYSTEM] Find files on disk matching a glob pattern.\n- `grep_files(pattern: str, path: str, glob: str, max_results: int) -> str` - [EXTERNAL FILESYSTEM] Search files on disk for a regex pattern using ripgrep.\n- `run_bash(cmd: str) -> str` - [EXTERNAL SYSTEM] Run a shell command on the host machine.",
"fields": [ "fields": [
{ {
"prefix": "Variables Info:", "prefix": "Variables Info:",
@@ -29,7 +29,7 @@
] ]
}, },
"lm": { "lm": {
"model": "openrouter/anthropic/claude-3.5-sonnet", "model": "openrouter/anthropic/claude-opus-4.5",
"model_type": "chat", "model_type": "chat",
"cache": true, "cache": true,
"num_retries": 3, "num_retries": 3,
@@ -37,7 +37,7 @@
"launch_kwargs": {}, "launch_kwargs": {},
"train_kwargs": {}, "train_kwargs": {},
"temperature": null, "temperature": null,
"max_tokens": 32000, "max_tokens": 50000,
"api_base": "https://openrouter.ai/api/v1", "api_base": "https://openrouter.ai/api/v1",
"track_usage": true "track_usage": true
} }
@@ -47,7 +47,7 @@
"train": [], "train": [],
"demos": [], "demos": [],
"signature": { "signature": {
"instructions": "The trajectory was generated with the following objective: \nYou are a concise coding assistant. Help the user with their coding task by using the available tools to read, write, edit files, search the codebase, and run commands.\n\n\nBased on the REPL trajectory, extract the final outputs now.\n\n Review your trajectory to see what information you gathered and what values you computed, then provide the final outputs.", "instructions": "The trajectory was generated with the following objective: \nYou are a concise coding assistant.\n\nCRITICAL - Two execution environments exist:\n\n1. INTERNAL REPL (sandbox): Standard Python code you write executes in an isolated sandbox. Variables persist between iterations. Use for data processing, string manipulation, logic, loops, etc.\n\n2. EXTERNAL TOOLS (real system): Functions like read_file(), write_file(), run_bash(), glob_files(), grep_files() execute OUTSIDE the sandbox on the real filesystem and host machine. These have real, persistent side effects.\n\nWhen you need to:\n- Process data, do math, manipulate strings, iterate \u2192 write Python code directly in the REPL\n- Read/write actual files on disk \u2192 call read_file(), write_file(), edit_file()\n- Run shell commands on the host \u2192 call run_bash()\n- Search the codebase \u2192 call glob_files(), grep_files()\n\nDo NOT confuse REPL variables with external files. Reading a file into a variable does not mean the variable updates if the file changes - you must call read_file() again.\n\n\nBased on the REPL trajectory, extract the final outputs now.\n\n Review your trajectory to see what information you gathered and what values you computed, then provide the final outputs.",
"fields": [ "fields": [
{ {
"prefix": "Variables Info:", "prefix": "Variables Info:",
@@ -60,15 +60,11 @@
{ {
"prefix": "Answer:", "prefix": "Answer:",
"description": "Your response to the user after completing the task" "description": "Your response to the user after completing the task"
},
{
"prefix": "Affected Files:",
"description": "List of files that were written or modified during the task"
} }
] ]
}, },
"lm": { "lm": {
"model": "openrouter/anthropic/claude-3.5-sonnet", "model": "openrouter/anthropic/claude-opus-4.5",
"model_type": "chat", "model_type": "chat",
"cache": true, "cache": true,
"num_retries": 3, "num_retries": 3,
@@ -76,7 +72,7 @@
"launch_kwargs": {}, "launch_kwargs": {},
"train_kwargs": {}, "train_kwargs": {},
"temperature": null, "temperature": null,
"max_tokens": 32000, "max_tokens": 50000,
"api_base": "https://openrouter.ai/api/v1", "api_base": "https://openrouter.ai/api/v1",
"track_usage": true "track_usage": true
} }

View File

@@ -4,4 +4,4 @@ version = "0.1.0"
description = "Add your description here" description = "Add your description here"
readme = "README.md" readme = "README.md"
requires-python = ">=3.13" requires-python = ">=3.13"
dependencies = ["dspy>=3.1.2", "modaic>=0.10.4"] dependencies = ["dspy>=3.1.2", "fastmcp>=2.14.3", "mcp2py>=0.6.0", "modaic>=0.10.4", "weave>=0.52.25"]