39 Commits
v0.0.3 ... dev

Author SHA1 Message Date
7016105521 Remove list_files tool 2026-02-01 01:26:07 -08:00
2e4a79e925 Remove list_files tool 2026-02-01 01:12:04 -08:00
14325cc16e Remove list_files tool 2026-01-31 23:42:21 -08:00
d9acd94155 Remove list_files tool 2026-01-31 16:13:45 -08:00
85d5330fc8 Remove list_files tool 2026-01-31 16:09:27 -08:00
9f27228565 Remove list_files tool 2026-01-31 15:14:19 -08:00
c27cdf870b Remove list_files tool 2026-01-31 13:18:44 -08:00
f9b1b0964e Remove list_files tool 2026-01-31 12:41:04 -08:00
fb5aa0070f Add list_files tool 2026-01-31 12:36:10 -08:00
35616d3e3e Migrate to ripgrep for glob_files 2026-01-31 12:30:11 -08:00
64e375e960 Fix config override bug by recreating LMs after load_state 2026-01-31 12:24:18 -08:00
3087006561 Fix config override bug by recreating LMs after load_state 2026-01-31 12:19:30 -08:00
3fa9f925ff Fix config override bug by recreating LMs after load_state 2026-01-24 15:50:24 -08:00
0e27e83b57 Fix config override bug by recreating LMs after load_state 2026-01-24 15:41:33 -08:00
33f34e9615 Fix config override bug by recreating LMs after load_state 2026-01-24 14:57:54 -08:00
3257f4dc01 Fix config override bug by recreating LMs after load_state 2026-01-24 14:45:24 -08:00
e633f96338 Fix config override bug by recreating LMs after load_state 2026-01-24 02:48:58 -08:00
2149e14573 Fix config override bug by recreating LMs after load_state 2026-01-24 02:29:24 -08:00
b8400242e4 Fix config override bug by recreating LMs after load_state 2026-01-24 01:25:11 -08:00
a234b48cda Fix config override bug by recreating LMs after load_state 2026-01-24 01:08:08 -08:00
2e671bd27f Fix config override bug by recreating LMs after load_state 2026-01-24 00:53:08 -08:00
2cd7286320 Add reload_lms method and debug forward() 2026-01-24 00:41:45 -08:00
fa1a5847cf Add reload_lms method and debug forward() 2026-01-24 00:37:42 -08:00
10163277ca Add reload_lms method and debug forward() 2026-01-24 00:34:19 -08:00
1a005b6584 Add reload_lms method and debug forward() 2026-01-24 00:29:10 -08:00
a4ae97ef81 change signature 2026-01-23 20:11:53 -08:00
378a657595 change signature 2026-01-23 20:11:43 -08:00
7f0aba9241 change signature 2026-01-23 19:36:06 -08:00
e69c82dea9 change signature 2026-01-23 19:35:16 -08:00
70597dc453 change signature 2026-01-23 04:19:28 -08:00
af90aeddf9 change signature 2026-01-22 19:47:29 -08:00
175979fb15 change signature 2026-01-22 17:37:10 -08:00
367fad475b change signature 2026-01-22 03:33:16 -08:00
c086336a0f debug 2026-01-22 03:01:32 -08:00
2d252e3221 debug 2026-01-22 02:32:24 -08:00
fdb81ee671 debug 2026-01-22 02:30:55 -08:00
22b7e87aaf debug 2026-01-22 02:26:21 -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 318 additions and 293 deletions

View File

@@ -1,6 +1,6 @@
# 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.
@@ -41,15 +41,14 @@ from modaic import AutoProgram
agent = AutoProgram.from_precompiled(
"farouk1/nanocode",
config={
"lm": "openrouter/anthropic/claude-3.5-sonnet",
"max_iters": 20
"lm": "openrouter/openai/gpt-5.2-codex",
"max_iters": 50
}
)
# Run a coding task
result = agent(task="What Python files are in this directory?")
print(result.answer)
print(result.affected_files)
```
### 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 |
|-----------|------|---------|-------------|
| `lm` | str | `openrouter/anthropic/claude-3.5-sonnet` | Primary language model |
| `sub_lm` | str | `openrouter/openai/gpt-4.1` | Sub-LM for reasoning steps |
| `max_iters` | int | `20` | Maximum agent iterations |
| `lm` | str | `openrouter/openai/gpt-5.2-codex` | Primary language model |
| `sub_lm` | str | `openrouter/openai/gpt-5-mini` | Sub-LM for reasoning steps |
| `max_iters` | int | `50` | Maximum agent iterations |
| `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 |
| `verbose` | bool | `False` | Enable verbose logging |
| `track_usage` | bool | `True` | Track token usage |
Example with custom configuration:
@@ -91,11 +91,12 @@ from modaic import AutoProgram
agent = AutoProgram.from_precompiled(
"farouk1/nanocode",
config={
"lm": "openrouter/openai/gpt-4",
"sub_lm": "openrouter/openai/gpt-3.5-turbo",
"lm": "openrouter/anthropic/claude-sonnet-4",
"sub_lm": "openrouter/openai/gpt-4.1-mini",
"max_iters": 30,
"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:
| Tool | Function | Description |
|------|----------|-------------|
| `readfile` | `read_file(path, offset, limit)` | Read file contents with line numbers |
| `writefile` | `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`) |
| `globfiles` | `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 |
| `runbash` | `run_bash(cmd)` | Run a shell command and return output |
| Tool | Description |
|------|-------------|
| `read_file(path, offset, limit)` | Read file contents with line numbers |
| `write_file(path, content)` | Write content to a file |
| `edit_file(path, old, new, replace_all)` | Replace text in a file (old must be unique unless `replace_all=True`) |
| `glob_files(pattern, path)` | Find files matching a glob pattern, sorted by modification time |
| `grep_files(pattern, path)` | Search files for a regex pattern |
| `run_bash(cmd)` | Run a shell command and return output |
---
@@ -161,7 +162,7 @@ print(result.answer)
# Make edits
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
├── DSPy Components
│ ├── 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
└── RLMCodingConfig (PrecompiledConfig)
```
@@ -195,13 +203,14 @@ Configuration class extending `PrecompiledConfig` for experiment-specific parame
```python
class RLMCodingConfig(PrecompiledConfig):
max_iters: int = 20
lm: str = "openrouter/anthropic/claude-3.5-sonnet"
sub_lm: str = "openrouter/openai/gpt-4.1"
max_iters: int = 50
lm: str = "openrouter/openai/gpt-5.2-codex"
sub_lm: str = "openrouter/openai/gpt-5-mini"
api_base: str = "https://openrouter.ai/api/v1"
max_tokens: int = 16000
max_tokens: int = 50000
max_output_chars: int = 100000
verbose: bool = False
track_usage: bool = True
```
#### `RLMCodingProgram`
@@ -212,8 +221,20 @@ class RLMCodingProgram(PrecompiledProgram):
config: RLMCodingConfig
def forward(self, task: str) -> dspy.Prediction:
# Returns prediction with .answer and .affected_files
# Returns prediction with .answer
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`
@@ -221,9 +242,10 @@ DSPy Signature defining the agent's input/output schema.
```python
class CodingAssistant(dspy.Signature):
task: str = dspy.InputField()
answer: str = dspy.OutputField()
affected_files: list[str] = dspy.OutputField()
"""You are a concise coding assistant with access to sub agents."""
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,12 @@
{
"model": null,
"max_iters": 20,
"lm": "openrouter/anthropic/claude-3.5-sonnet",
"sub_lm": "openrouter/openai/gpt-4.1",
"max_iters": 50,
"lm": "openrouter/anthropic/claude-opus-4.5",
"sub_lm": "openrouter/qwen/qwen-coder",
"api_base": "https://openrouter.ai/api/v1",
"max_tokens": 32000,
"max_tokens": 50000,
"max_output_chars": 100000,
"verbose": false,
"track_usage": true
"verbose": true,
"track_usage": true,
"track_trace": false
}

View File

@@ -1,17 +1,12 @@
import os
import re
import glob as globlib
import subprocess
from modaic import PrecompiledProgram, PrecompiledConfig
import dspy
import weave
import subprocess
from dspy.utils.callback import BaseCallback
# --- Modaic ---
MODAIC_REPO_PATH = "farouk1/nanocode"
# --- ANSI colors ---
RESET = "\033[0m"
BOLD = "\033[1m"
DIM = "\033[2m"
@@ -22,24 +17,11 @@ YELLOW = "\033[33m"
RED = "\033[31m"
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 ---
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:
path: Path to the file to read
@@ -53,26 +35,45 @@ def read_file(path: str, offset: int = 0, limit: int = None) -> str:
if limit is None:
limit = len(lines)
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:
"""Write content to a file.
"""[EXTERNAL FILESYSTEM] Write content to a file on disk (creates or overwrites).
Args:
path: Path to the file to write
content: Content to write to the file
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:
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:
"""Replace text in a file.
"""[EXTERNAL FILESYSTEM] Replace text in a file on disk.
Args:
path: Path to the file to edit
@@ -83,6 +84,8 @@ def edit_file(path: str, old: str, new: str, replace_all: bool = False) -> str:
Returns:
'ok' on success, error message on failure
"""
print(f"{MAGENTA}⏺ Edit({path}){RESET}")
text = open(path).read()
if old not in text:
return "error: old_string not found"
@@ -96,7 +99,9 @@ def edit_file(path: str, old: str, new: str, replace_all: bool = False) -> str:
def glob_files(pattern: str, path: str = ".") -> str:
"""Find files matching a glob pattern, sorted by modification time.
"""[EXTERNAL FILESYSTEM] Do not use for simple file listing, run bash instead. Find files on disk matching a glob pattern.
Respects .gitignore files automatically via ripgrep. Sorted by modification time.
Args:
pattern: Glob pattern to match (e.g., '**/*.py')
@@ -105,43 +110,60 @@ def glob_files(pattern: str, path: str = ".") -> str:
Returns:
Newline-separated list of matching files
"""
full_pattern = (path + "/" + pattern).replace("//", "/")
files = globlib.glob(full_pattern, recursive=True)
files = sorted(
files,
key=lambda f: os.path.getmtime(f) if os.path.isfile(f) else 0,
reverse=True,
)
return "\n".join(files) or "no files found"
print(f"{MAGENTA}⏺ Glob({pattern}): {path}{RESET}")
cmd = ["rg", "--files", "--no-require-git", "-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,
key=lambda f: os.path.getmtime(f) if os.path.isfile(f) else 0,
reverse=True,
)
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:
"""Search files for a regex pattern.
def grep_files(
pattern: str, path: str = ".", glob: str = None, max_results: int = 50
) -> str:
"""[EXTERNAL FILESYSTEM] Search files on disk for a regex pattern using ripgrep.
Args:
pattern: Regular expression pattern to search for
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:
Matching lines in format 'filepath:line_num:content'
"""
regex = re.compile(pattern)
hits = []
for filepath in globlib.glob(path + "/**", recursive=True):
try:
for line_num, line in enumerate(open(filepath), 1):
if regex.search(line):
hits.append(f"{filepath}:{line_num}:{line.rstrip()}")
except Exception:
pass
return "\n".join(hits[:50]) or "no matches found"
print(f"{MAGENTA}⏺ Grep: {pattern}{RESET}")
cmd = ["rg", "-n", "--no-heading", "--color=never", f"-m{max_results}"]
if glob:
cmd.extend(["-g", glob])
cmd.extend([pattern, path])
try:
result = subprocess.run(cmd, capture_output=True, text=True, timeout=30)
output = result.stdout.strip()
return output if output else "no matches found"
except FileNotFoundError:
return "error: ripgrep (rg) not installed - install with 'brew install ripgrep'"
except subprocess.TimeoutExpired:
return "error: search timed out after 30s"
# --- Shell operations ---
def run_bash(cmd: str) -> str:
"""Run a shell command and return output.
"""[EXTERNAL SYSTEM] Run a shell command on the host machine.
Args:
cmd: Shell command to execute
@@ -149,6 +171,8 @@ def run_bash(cmd: str) -> str:
Returns:
Command output (stdout and stderr combined)
"""
print(f"{MAGENTA}⏺ Bash: {cmd}{RESET}")
proc = subprocess.Popen(
cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, text=True
)
@@ -168,118 +192,88 @@ def run_bash(cmd: str) -> str:
return "".join(output_lines).strip() or "(empty output)"
# --- Model selection ---
AVAILABLE_MODELS = {
"1": ("GPT-5.2 Codex", "openai/gpt-5.2-codex"),
"2": ("GPT-5.2", "openai/gpt-5.2"),
"3": ("Claude Opus 4.5", "anthropic/claude-opus-4.5"),
"4": ("Claude Opus 4", "anthropic/claude-opus-4"),
"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:
print(f"{RED}⏺ Invalid model ID{RESET}")
class RLMReasoningCallback(BaseCallback):
def on_module_end(self, call_id, outputs, exception):
if outputs and hasattr(outputs, "reasoning") and hasattr(outputs, "code"):
has_backticks = "```" in outputs.code
print(f"{DIM}⏺ [REASONING STEP]\n{outputs.reasoning}\n{RESET}")
if has_backticks:
print(f"{DIM}⏺ [CODE]\n{outputs.code}\n{RESET}")
else:
print(f"{RED}Invalid choice. Please enter 1-8 or c{RESET}")
except (KeyboardInterrupt, EOFError):
print(f"\n{RED}⏺ Model selection cancelled{RESET}")
exit(1)
print(f"{DIM}[CODE]\n```\n{outputs.code}\n```\n{RESET}")
# -- Program ---
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")
answer: str = dspy.OutputField(
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):
max_iters: int = 20
lm: str = "openrouter/anthropic/claude-3.5-sonnet" # Default fallback
sub_lm: str = "openrouter/openai/gpt-4.1" # Default fallback
max_iters: int = 50
lm: str = "openrouter/anthropic/claude-opus-4.5"
sub_lm: str = "openrouter/qwen/qwen-coder"
api_base: str = "https://openrouter.ai/api/v1"
max_tokens: int = 32000
max_tokens: int = 50000
max_output_chars: int = 100000
verbose: bool = False
verbose: bool = True
track_usage: bool = True
track_trace: bool = False
class RLMCodingProgram(PrecompiledProgram):
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):
self.config = config
super().__init__(config, **kwargs)
if config.track_trace:
project = kwargs.get("project", os.getenv("WANDB_PROJECT"))
if project is None:
raise ValueError("project is required when track_trace is True")
wandb_key = kwargs.get("wandb_key", os.getenv("WANDB_API_KEY"))
if wandb_key is None:
raise ValueError("wandb_key is required when track_trace is True")
os.environ["WANDB_PROJECT"] = project
os.environ["WANDB_API_KEY"] = wandb_key
weave.init(project_name=project)
self.config = config
self.tools = {
"read_file": read_file,
"write_file": write_file,
@@ -289,146 +283,158 @@ class RLMCodingProgram(PrecompiledProgram):
"run_bash": run_bash,
}
# tool logging for introspections on multi-turn conversations
dspy.settings.configure(callbacks=[ToolLoggingCallback()])
lm = dspy.LM(
self.config.lm,
self.lm = dspy.LM(
model=self.config.lm,
api_base=self.config.api_base,
max_tokens=self.config.max_tokens,
track_usage=self.config.track_usage,
)
sub_lm = dspy.LM(
self.config.sub_lm,
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,
)
agent = dspy.RLM(
self.agent = dspy.RLM(
CodingAssistant,
sub_lm=sub_lm,
sub_lm=self.sub_lm,
tools=self.tools,
max_output_chars=self.config.max_output_chars,
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)
self.agent = agent
if self.config.verbose:
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:
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)
def get_tools(self):
"""Get the tools for the agent."""
return self.tools
def set_tool(self, name: str, tool: callable):
"""Set a tool for the agent."""
self.tools[name] = tool
self.reload_repl()
def remove_tool(self, name: str):
del self.tools[name]
"""Remove a tool from the agent."""
if name in self.tools:
del self.tools[name]
self.reload_repl()
def main():
model = os.getenv("MODEL")
if model is None:
model = select_model()
def reload_repl(
self,
): # We need to create a new instance for tool mutations to be passed back into the REPL
"""Reload the REPL with the current tools."""
# Add openrouter/ prefix if not already present
if not model.startswith("openrouter/"):
model = f"openrouter/{model}"
new_instance = dspy.RLM(
CodingAssistant,
sub_lm=self.sub_lm,
tools=self.tools,
max_output_chars=self.config.max_output_chars,
max_iterations=self.config.max_iters,
verbose=False, # We add our own verbose logging
)
new_instance.set_lm(self.lm)
self.agent = new_instance
if self.config.verbose:
self.add_logging_callbacks()
config = RLMCodingConfig()
config.lm = model
def reload_lms(self):
"""Recreate LM objects from current config. Call this after changing config.lm or config.sub_lm."""
agent = RLMCodingProgram(config)
print(
f"{BOLD}NANOCODE DSPY{RESET} | {DIM}{agent.config.lm} | {os.getcwd()}{RESET}\n"
)
self.lm = dspy.LM(
model=self.config.lm,
api_base=self.config.api_base,
max_tokens=self.config.max_tokens,
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}")
# Conversation history for context
history = []
def load_state(self, state):
"""Override to recreate LMs from config after loading state.
while True:
try:
print(separator())
user_input = input(f"{BOLD}{BLUE}{RESET} ").strip()
print(separator())
if not user_input:
continue
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()
if choice == "k":
print(f"{GREEN}⏺ Keeping current model: {agent.config.lm}{RESET}")
continue
elif choice in AVAILABLE_MODELS:
name, model_id = AVAILABLE_MODELS[choice]
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}")
PrecompiledProgram.from_precompiled() calls load_state() AFTER __init__,
which overwrites our LMs with saved state. We fix this by recreating
the LMs from self.config after the parent load_state runs. Modaic will
fix this in a later patch for future devs.
"""
super().load_state(state)
self.reload_lms() # Recreate LMs from config (not from saved state)
if __name__ == "__main__":
agent = RLMCodingProgram(RLMCodingConfig())
agent.push_to_hub(MODAIC_REPO_PATH, commit_message="Switch to RLM instead of ReAct", tag="v0.0.3")
#main()
# agent(task="what's 1 + 1?")
branches = ["dev"]
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": [],
"demos": [],
"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] Do not use for simple file listing, run bash instead. 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": [
{
"prefix": "Variables Info:",
@@ -29,7 +29,7 @@
]
},
"lm": {
"model": "openrouter/anthropic/claude-3.5-sonnet",
"model": "openrouter/anthropic/claude-opus-4.5",
"model_type": "chat",
"cache": true,
"num_retries": 3,
@@ -37,7 +37,7 @@
"launch_kwargs": {},
"train_kwargs": {},
"temperature": null,
"max_tokens": 32000,
"max_tokens": 50000,
"api_base": "https://openrouter.ai/api/v1",
"track_usage": true
}
@@ -47,7 +47,7 @@
"train": [],
"demos": [],
"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": [
{
"prefix": "Variables Info:",
@@ -60,15 +60,11 @@
{
"prefix": "Answer:",
"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": {
"model": "openrouter/anthropic/claude-3.5-sonnet",
"model": "openrouter/anthropic/claude-opus-4.5",
"model_type": "chat",
"cache": true,
"num_retries": 3,
@@ -76,7 +72,7 @@
"launch_kwargs": {},
"train_kwargs": {},
"temperature": null,
"max_tokens": 32000,
"max_tokens": 50000,
"api_base": "https://openrouter.ai/api/v1",
"track_usage": true
}

View File

@@ -4,4 +4,4 @@ version = "0.1.0"
description = "Add your description here"
readme = "README.md"
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", "wandb>=0.24.1", "weave>=0.52.25"]