Skip to content
Open

9-15 #575

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 22 additions & 5 deletions agents/s01_agent_loop.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,12 @@
"""

import os
import platform
import subprocess

try:
import readline

# #143 UTF-8 backspace fix for macOS libedit
readline.parse_and_bind('set bind-tty-special-chars off')
readline.parse_and_bind('set input-meta on')
Expand All @@ -49,10 +51,14 @@
client = Anthropic(base_url=os.getenv("ANTHROPIC_BASE_URL"))
MODEL = os.environ["MODEL_ID"]

SYSTEM = f"You are a coding agent at {os.getcwd()}. Use bash to solve tasks. Act, don't explain."
SYSTEM = f"""You are a coding agent running on {platform.system()} at {os.getcwd()}.
You MUST use dos commands on Windows. NEVER use Linux/Unix commands like "ls", "cat", or "grep" on Windows.
On Linux/macOS, use bash commands.
Act and respond with commands only; do not explain or comment.
"""

TOOLS = [{
"name": "bash",
"name": "run_command",
"description": "Run a shell command.",
"input_schema": {
"type": "object",
Expand All @@ -62,8 +68,19 @@
}]


def run_bash(command: str) -> str:
dangerous = ["rm -rf /", "sudo", "shutdown", "reboot", "> /dev/"]
def run_command(command: str) -> str:
dangerous = [
# Linux / macOS
"rm -rf /", "rm -rf /*", "shutdown", "reboot", "halt", "poweroff",
":(){:|:&};:", "> /dev/", "mkfs", "dd if=",

# Windows
"format", "del /f", "rd /s", "shutdown", "taskkill",
"powershell -command", "Remove-Item", "Stop-Computer",

# 通用危险符号
"sudo"
]
if any(d in command for d in dangerous):
return "Error: Dangerous command blocked"
try:
Expand Down Expand Up @@ -94,7 +111,7 @@ def agent_loop(messages: list):
for block in response.content:
if block.type == "tool_use":
print(f"\033[33m$ {block.input['command']}\033[0m")
output = run_bash(block.input["command"])
output = run_command(block.input["command"])
print(output[:200])
results.append({"type": "tool_result", "tool_use_id": block.id,
"content": output})
Expand Down
40 changes: 30 additions & 10 deletions agents/s02_tool_use.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
"""

import os
import platform
import subprocess
from pathlib import Path

Expand All @@ -41,7 +42,11 @@
client = Anthropic(base_url=os.getenv("ANTHROPIC_BASE_URL"))
MODEL = os.environ["MODEL_ID"]

SYSTEM = f"You are a coding agent at {WORKDIR}. Use tools to solve tasks. Act, don't explain."
SYSTEM = f"""You are a coding agent running on {platform.system()} at {os.getcwd()}.
You MUST use dos commands on Windows. NEVER use Linux/Unix commands like "ls", "cat", or "grep" on Windows.
On Linux/macOS, use bash commands.
Act and respond with commands only; do not explain or comment.
"""


def safe_path(p: str) -> Path:
Expand All @@ -51,8 +56,19 @@ def safe_path(p: str) -> Path:
return path


def run_bash(command: str) -> str:
dangerous = ["rm -rf /", "sudo", "shutdown", "reboot", "> /dev/"]
def run_command(command: str) -> str:
dangerous = [
# Linux / macOS
"rm -rf /", "rm -rf /*", "shutdown", "reboot", "halt", "poweroff",
":(){:|:&};:", "> /dev/", "mkfs", "dd if=",

# Windows
"format", "del /f", "rd /s", "shutdown", "taskkill",
"powershell -command", "Remove-Item", "Stop-Computer",

# 通用危险符号
"sudo"
]
if any(d in command for d in dangerous):
return "Error: Dangerous command blocked"
try:
Expand Down Expand Up @@ -100,21 +116,25 @@ def run_edit(path: str, old_text: str, new_text: str) -> str:

# -- The dispatch map: {tool_name: handler} --
TOOL_HANDLERS = {
"bash": lambda **kw: run_bash(kw["command"]),
"read_file": lambda **kw: run_read(kw["path"], kw.get("limit")),
"run_command": lambda **kw: run_command(kw["command"]),
"read_file": lambda **kw: run_read(kw["path"], kw.get("limit")),
"write_file": lambda **kw: run_write(kw["path"], kw["content"]),
"edit_file": lambda **kw: run_edit(kw["path"], kw["old_text"], kw["new_text"]),
"edit_file": lambda **kw: run_edit(kw["path"], kw["old_text"], kw["new_text"]),
}

TOOLS = [
{"name": "bash", "description": "Run a shell command.",
{"name": "run_command", "description": "Run a command.",
"input_schema": {"type": "object", "properties": {"command": {"type": "string"}}, "required": ["command"]}},
{"name": "read_file", "description": "Read file contents.",
"input_schema": {"type": "object", "properties": {"path": {"type": "string"}, "limit": {"type": "integer"}}, "required": ["path"]}},
"input_schema": {"type": "object", "properties": {"path": {"type": "string"}, "limit": {"type": "integer"}},
"required": ["path"]}},
{"name": "write_file", "description": "Write content to file.",
"input_schema": {"type": "object", "properties": {"path": {"type": "string"}, "content": {"type": "string"}}, "required": ["path", "content"]}},
"input_schema": {"type": "object", "properties": {"path": {"type": "string"}, "content": {"type": "string"}},
"required": ["path", "content"]}},
{"name": "edit_file", "description": "Replace exact text in file.",
"input_schema": {"type": "object", "properties": {"path": {"type": "string"}, "old_text": {"type": "string"}, "new_text": {"type": "string"}}, "required": ["path", "old_text", "new_text"]}},
"input_schema": {"type": "object", "properties": {"path": {"type": "string"}, "old_text": {"type": "string"},
"new_text": {"type": "string"}},
"required": ["path", "old_text", "new_text"]}},
]


Expand Down
69 changes: 57 additions & 12 deletions agents/s03_todo_write.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,20 @@ def safe_path(p: str) -> Path:
raise ValueError(f"Path escapes workspace: {p}")
return path

def run_bash(command: str) -> str:
dangerous = ["rm -rf /", "sudo", "shutdown", "reboot", "> /dev/"]

def run_command(command: str) -> str:
dangerous = [
# Linux / macOS
"rm -rf /", "rm -rf /*", "shutdown", "reboot", "halt", "poweroff",
":(){:|:&};:", "> /dev/", "mkfs", "dd if=",

# Windows
"format", "del /f", "rd /s", "shutdown", "taskkill",
"powershell -command", "Remove-Item", "Stop-Computer",

# 通用危险符号
"sudo"
]
if any(d in command for d in dangerous):
return "Error: Dangerous command blocked"
try:
Expand All @@ -114,6 +126,7 @@ def run_bash(command: str) -> str:
except subprocess.TimeoutExpired:
return "Error: Timeout (120s)"


def run_read(path: str, limit: int = None) -> str:
try:
lines = safe_path(path).read_text(encoding="utf-8").splitlines()
Expand All @@ -123,6 +136,7 @@ def run_read(path: str, limit: int = None) -> str:
except Exception as e:
return f"Error: {e}"


def run_write(path: str, content: str) -> str:
try:
fp = safe_path(path)
Expand All @@ -132,6 +146,7 @@ def run_write(path: str, content: str) -> str:
except Exception as e:
return f"Error: {e}"


def run_edit(path: str, old_text: str, new_text: str) -> str:
try:
fp = safe_path(path)
Expand All @@ -145,24 +160,54 @@ def run_edit(path: str, old_text: str, new_text: str) -> str:


TOOL_HANDLERS = {
"bash": lambda **kw: run_bash(kw["command"]),
"read_file": lambda **kw: run_read(kw["path"], kw.get("limit")),
"run_command": lambda **kw: run_command(kw["command"]),
"read_file": lambda **kw: run_read(kw["path"], kw.get("limit")),
"write_file": lambda **kw: run_write(kw["path"], kw["content"]),
"edit_file": lambda **kw: run_edit(kw["path"], kw["old_text"], kw["new_text"]),
"todo": lambda **kw: TODO.update(kw["items"]),
"edit_file": lambda **kw: run_edit(kw["path"], kw["old_text"], kw["new_text"]),
"todo": lambda **kw: TODO.update(kw["items"]),
}

TOOLS = [
{"name": "bash", "description": "Run a shell command.",
{"name": "run_command", "description": "Run a command.",
"input_schema": {"type": "object", "properties": {"command": {"type": "string"}}, "required": ["command"]}},
{"name": "read_file", "description": "Read file contents.",
"input_schema": {"type": "object", "properties": {"path": {"type": "string"}, "limit": {"type": "integer"}}, "required": ["path"]}},
"input_schema": {"type": "object", "properties": {"path": {"type": "string"}, "limit": {"type": "integer"}},
"required": ["path"]}},
{"name": "write_file", "description": "Write content to file.",
"input_schema": {"type": "object", "properties": {"path": {"type": "string"}, "content": {"type": "string"}}, "required": ["path", "content"]}},
"input_schema": {"type": "object", "properties": {"path": {"type": "string"}, "content": {"type": "string"}},
"required": ["path", "content"]}},
{"name": "edit_file", "description": "Replace exact text in file.",
"input_schema": {"type": "object", "properties": {"path": {"type": "string"}, "old_text": {"type": "string"}, "new_text": {"type": "string"}}, "required": ["path", "old_text", "new_text"]}},
{"name": "todo", "description": "Update task list. Track progress on multi-step tasks.",
"input_schema": {"type": "object", "properties": {"items": {"type": "array", "items": {"type": "object", "properties": {"id": {"type": "string"}, "text": {"type": "string"}, "status": {"type": "string", "enum": ["pending", "in_progress", "completed"]}}, "required": ["id", "text", "status"]}}}, "required": ["items"]}},
"input_schema": {"type": "object", "properties": {"path": {"type": "string"}, "old_text": {"type": "string"},
"new_text": {"type": "string"}},
"required": ["path", "old_text", "new_text"]}},
{
"name": "todo", "description": "Update task list. Track progress on multi-step tasks.",
"input_schema": {
"type": "object",
"properties": {
"items": {
"type": "array",
"items": {
"type": "object",
"properties": {
"id": {
"type": "string"
},
"text": {
"type": "string"
},
"status": {
"type": "string",
"enum": ["pending", "in_progress", "completed"]
}
},
"required": ["id", "text", "status"]
}
}
},
"required": ["items"]
}
},
]


Expand Down
44 changes: 33 additions & 11 deletions agents/s04_subagent.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,20 @@ def safe_path(p: str) -> Path:
raise ValueError(f"Path escapes workspace: {p}")
return path

def run_bash(command: str) -> str:
dangerous = ["rm -rf /", "sudo", "shutdown", "reboot", "> /dev/"]

def run_command(command: str) -> str:
dangerous = [
# Linux / macOS
"rm -rf /", "rm -rf /*", "shutdown", "reboot", "halt", "poweroff",
":(){:|:&};:", "> /dev/", "mkfs", "dd if=",

# Windows
"format", "del /f", "rd /s", "shutdown", "taskkill",
"powershell -command", "Remove-Item", "Stop-Computer",

# 通用危险符号
"sudo"
]
if any(d in command for d in dangerous):
return "Error: Dangerous command blocked"
try:
Expand All @@ -70,6 +82,7 @@ def run_bash(command: str) -> str:
except (FileNotFoundError, OSError) as e:
return f"Error: {e}"


def run_read(path: str, limit: int = None) -> str:
try:
lines = safe_path(path).read_text(encoding="utf-8").splitlines()
Expand All @@ -79,6 +92,7 @@ def run_read(path: str, limit: int = None) -> str:
except Exception as e:
return f"Error: {e}"


def run_write(path: str, content: str) -> str:
try:
fp = safe_path(path)
Expand All @@ -88,6 +102,7 @@ def run_write(path: str, content: str) -> str:
except Exception as e:
return f"Error: {e}"


def run_edit(path: str, old_text: str, new_text: str) -> str:
try:
fp = safe_path(path)
Expand All @@ -101,22 +116,26 @@ def run_edit(path: str, old_text: str, new_text: str) -> str:


TOOL_HANDLERS = {
"bash": lambda **kw: run_bash(kw["command"]),
"read_file": lambda **kw: run_read(kw["path"], kw.get("limit")),
"run_command": lambda **kw: run_command(kw["command"]),
"read_file": lambda **kw: run_read(kw["path"], kw.get("limit")),
"write_file": lambda **kw: run_write(kw["path"], kw["content"]),
"edit_file": lambda **kw: run_edit(kw["path"], kw["old_text"], kw["new_text"]),
"edit_file": lambda **kw: run_edit(kw["path"], kw["old_text"], kw["new_text"]),
}

# Child gets all base tools except task (no recursive spawning)
CHILD_TOOLS = [
{"name": "bash", "description": "Run a shell command.",
{"name": "run_command", "description": "Run a shell command.",
"input_schema": {"type": "object", "properties": {"command": {"type": "string"}}, "required": ["command"]}},
{"name": "read_file", "description": "Read file contents.",
"input_schema": {"type": "object", "properties": {"path": {"type": "string"}, "limit": {"type": "integer"}}, "required": ["path"]}},
"input_schema": {"type": "object", "properties": {"path": {"type": "string"}, "limit": {"type": "integer"}},
"required": ["path"]}},
{"name": "write_file", "description": "Write content to file.",
"input_schema": {"type": "object", "properties": {"path": {"type": "string"}, "content": {"type": "string"}}, "required": ["path", "content"]}},
"input_schema": {"type": "object", "properties": {"path": {"type": "string"}, "content": {"type": "string"}},
"required": ["path", "content"]}},
{"name": "edit_file", "description": "Replace exact text in file.",
"input_schema": {"type": "object", "properties": {"path": {"type": "string"}, "old_text": {"type": "string"}, "new_text": {"type": "string"}}, "required": ["path", "old_text", "new_text"]}},
"input_schema": {"type": "object", "properties": {"path": {"type": "string"}, "old_text": {"type": "string"},
"new_text": {"type": "string"}},
"required": ["path", "old_text", "new_text"]}},
]


Expand Down Expand Up @@ -144,8 +163,11 @@ def run_subagent(prompt: str) -> str:

# -- Parent tools: base tools + task dispatcher --
PARENT_TOOLS = CHILD_TOOLS + [
{"name": "task", "description": "Spawn a subagent with fresh context. It shares the filesystem but not conversation history.",
"input_schema": {"type": "object", "properties": {"prompt": {"type": "string"}, "description": {"type": "string", "description": "Short description of the task"}}, "required": ["prompt"]}},
{"name": "task",
"description": "Spawn a subagent with fresh context. It shares the filesystem but not conversation history.",
"input_schema": {"type": "object", "properties": {"prompt": {"type": "string"}, "description": {"type": "string",
"description": "Short description of the task"}},
"required": ["prompt"]}},
]


Expand Down