Fix config override bug by recreating LMs after load_state
This commit is contained in:
39
nanocode.py
39
nanocode.py
@@ -2,7 +2,6 @@ import os
|
||||
import glob as globlib
|
||||
from modaic import PrecompiledProgram, PrecompiledConfig
|
||||
import dspy
|
||||
import re
|
||||
import subprocess
|
||||
from dspy.utils.callback import BaseCallback
|
||||
|
||||
@@ -36,13 +35,14 @@ def read_file(path: str, offset: int = 0, limit: int = None) -> str:
|
||||
Returns:
|
||||
File contents with line numbers
|
||||
"""
|
||||
print(f"{MAGENTA}⏺ Reading file: {path}{RESET}")
|
||||
|
||||
lines = open(path).readlines()
|
||||
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:
|
||||
@@ -110,28 +110,33 @@ def glob_files(pattern: str, path: str = ".") -> str:
|
||||
return "\n".join(files) or "no files found"
|
||||
|
||||
|
||||
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:
|
||||
"""Search files 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'
|
||||
"""
|
||||
print(f"{MAGENTA}⏺ Grep: {pattern}{RESET}")
|
||||
|
||||
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"
|
||||
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 ---
|
||||
@@ -363,7 +368,7 @@ class RLMCodingProgram(PrecompiledProgram):
|
||||
if __name__ == "__main__":
|
||||
agent = RLMCodingProgram(RLMCodingConfig())
|
||||
#agent(task="explicity call llm_query(who is the ceo of apple?) to get the answer to 'who is the ceo of apple?'")
|
||||
branches = ["main", "dev", "prod"]
|
||||
branches = ["dev"]
|
||||
for branch in branches:
|
||||
agent.push_to_hub(
|
||||
MODAIC_REPO_PATH,
|
||||
|
||||
Reference in New Issue
Block a user