This commit is contained in:
2024-12-04 17:19:46 +01:00
parent af390dd779
commit da8583ab28
36 changed files with 712 additions and 69 deletions

View File

@@ -73,13 +73,13 @@ class Main:
metrics=SystemMetrics(),
llms=self._llms,
validator=XMLValidator(self._action_schema),
parser=ResponseParser(self._io_buffer),
parser=ResponseParser(config.work_dir, self._io_buffer),
iteration_logger=IterationLogger(self._config.iterations_dir, self._system_prompt, self._action_schema),
)
self._auto_approver = AutoApprover(self._agent)
self._app = web.Application()
self._api = Api(self._app, self._agent, self._io_buffer, self._working_memory, self._auto_approver)
self._api = Api(config.work_dir, self._app, self._agent, self._io_buffer, self._working_memory, self._auto_approver)
self._websockets = Websockets(self._app, self._agent, self._io_buffer, self._auto_approver, self._working_memory)
self._static = Static(self._app, self._config)

View File

@@ -30,6 +30,12 @@ class Config:
default=os.getenv('SIA_ITERATIONS_DIR', 'iterations'),
help='Path to the directory for storing iterations (default: iterations, env: SIA_ITERATIONS_DIR)'
)
parser.add_argument(
'--work-dir',
type=Path,
default=os.getenv('SIA_WORK_DIR', '/root'),
help='Path to the working directory (default: /root, env: SIA_WORK_DIR)'
)
# Web server configuration
parser.add_argument(
@@ -205,6 +211,10 @@ class Config:
@property
def iterations_dir(self) -> Path:
return self.args.iterations_dir
@property
def work_dir(self) -> Path:
return self.args.work_dir
# Server properties
@property

View File

@@ -19,6 +19,7 @@ class BackgroundEntry(Entry):
def __init__(
self,
id: str,
work_dir: str,
script: str,
):
"""
@@ -26,9 +27,11 @@ class BackgroundEntry(Entry):
Args:
id: Unique identifier for this entry
work_dir: Working directory for the process
script: The script/command to execute
"""
super().__init__(id)
self.work_dir = work_dir
self.script = script
self._process: Optional[subprocess.Popen] = None
self.stdout = ""
@@ -81,6 +84,7 @@ class BackgroundEntry(Entry):
if self._process is None and self.exit_code is None:
self._process = subprocess.Popen(
self.script,
cwd=self.work_dir,
shell=True,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
@@ -155,6 +159,7 @@ class BackgroundEntry(Entry):
return {
"type": "background",
"id": self.id,
"work_dir": str(self.work_dir),
"script": self.script,
"stdout": self.stdout,
"stderr": self.stderr,

View File

@@ -1,4 +1,4 @@
from typing import Optional
from pathlib import Path
from . import Entry
from ..io_buffer import IOBuffer
@@ -11,12 +11,12 @@ from .single_entry import SingleEntry
from .write_entry import WriteEntry
class EntryFactory:
def create_entry(data: dict, io_buffer: IOBuffer) -> Entry:
def create_entry(data: dict, work_dir: Path, io_buffer: IOBuffer) -> Entry:
entry_type = data.get("type")
entry_id = data.get("id")
if entry_type == "background":
return BackgroundEntry(entry_id, data["script"])
return BackgroundEntry(entry_id, work_dir, data["script"])
elif entry_type == "read_stdin":
return ReadEntry(entry_id, io_buffer)
@@ -27,6 +27,7 @@ class EntryFactory:
elif entry_type == "repeat":
return RepeatEntry(
entry_id,
work_dir,
data["script"],
data.get("timeout"),
data.get("limit")
@@ -35,6 +36,7 @@ class EntryFactory:
elif entry_type == "single":
return SingleEntry(
entry_id,
work_dir,
data["script"],
data.get("timeout"),
data.get("limit")
@@ -51,6 +53,8 @@ class EntryFactory:
entry.id = data["id"]
if isinstance(entry, SingleEntry):
if "work_dir" in data:
entry.work_dir = Path(data["work_dir"])
if "script" in data:
entry.script = data["script"]
if "timeout" in data:
@@ -69,6 +73,8 @@ class EntryFactory:
entry.timed_out = bool(data["timed_out"])
if isinstance(entry, RepeatEntry):
if "work_dir" in data:
entry.work_dir = Path(data["work_dir"])
if "script" in data:
entry.script = data["script"]
if "timeout" in data:
@@ -87,6 +93,8 @@ class EntryFactory:
entry.timed_out = bool(data["timed_out"])
elif isinstance(entry, BackgroundEntry):
if "work_dir" in data:
entry.work_dir = Path(data["work_dir"])
if "script" in data:
entry.script = data["script"]
if "stdout" in data:

View File

@@ -1,3 +1,4 @@
from pathlib import Path
import subprocess
import xml.etree.ElementTree as ET
from typing import Optional
@@ -15,6 +16,7 @@ class RepeatEntry(Entry):
def __init__(
self,
id: str,
work_dir: Path,
script: str,
timeout: Optional[float] = None,
limit: Optional[int] = None,
@@ -29,6 +31,7 @@ class RepeatEntry(Entry):
limit: Maximum number of characters to capture from stdout/stderr
"""
super().__init__(id)
self.work_dir = work_dir
self.script = script
self.timeout = timeout
self.limit = limit
@@ -45,6 +48,7 @@ class RepeatEntry(Entry):
try:
process = subprocess.run(
self.script,
cwd=self.work_dir,
timeout=(self.timeout or self.default_timeout),
shell=True,
capture_output=True,
@@ -95,6 +99,7 @@ class RepeatEntry(Entry):
return {
"type": "repeat",
"id": self.id,
"work_dir": str(self.work_dir),
"script": self.script,
"timeout": self.timeout,
"limit": self.limit,

View File

@@ -1,3 +1,4 @@
from pathlib import Path
import subprocess
import xml.etree.ElementTree as ET
from typing import Optional
@@ -15,6 +16,7 @@ class SingleEntry(Entry):
def __init__(
self,
id: str,
work_dir: Path,
script: str,
timeout: Optional[float] = None,
limit: Optional[int] = None,
@@ -29,6 +31,7 @@ class SingleEntry(Entry):
limit: Maximum number of characters to capture from stdout/stderr
"""
super().__init__(id)
self.work_dir = work_dir
self.script = script
self.timeout = timeout
self.limit = limit
@@ -58,6 +61,7 @@ class SingleEntry(Entry):
try:
process = subprocess.run(
self.script,
cwd=self.work_dir,
timeout=(self.timeout or self.default_timeout),
shell=True,
capture_output=True,
@@ -106,6 +110,7 @@ class SingleEntry(Entry):
return {
"type": "single",
"id": self.id,
"work_dir": str(self.work_dir),
"script": self.script,
"timeout": self.timeout,
"limit": self.limit,

View File

@@ -1,6 +1,6 @@
from datetime import datetime
import xml.etree.ElementTree as ET
from pathlib import Path
from typing import List
import xml.etree.ElementTree as ET
from .entry import Entry
from .entry.background_entry import BackgroundEntry
@@ -15,7 +15,7 @@ class IterationParser:
"""Parses iteration XML files into entries"""
@staticmethod
def parse_iteration(content: str, io_buffer) -> tuple[str, str, List[Entry]]:
def parse_iteration(content: str, work_dir: Path, io_buffer) -> tuple[str, str, List[Entry]]:
"""
Parse iteration XML content into context, response and entries
@@ -37,7 +37,7 @@ class IterationParser:
entries = []
for elem in context:
if elem.tag == "background":
entries.append(IterationParser._parse_background(elem))
entries.append(IterationParser._parse_background(elem, work_dir))
elif elem.tag == "parse_error":
entries.append(IterationParser._parse_parse_error(elem))
elif elem.tag == "read_stdin":
@@ -45,19 +45,20 @@ class IterationParser:
elif elem.tag == "reasoning":
entries.append(IterationParser._parse_reasoning(elem))
elif elem.tag == "repeat":
entries.append(IterationParser._parse_repeat(elem))
entries.append(IterationParser._parse_repeat(elem, work_dir))
elif elem.tag == "single":
entries.append(IterationParser._parse_single(elem))
entries.append(IterationParser._parse_single(elem, work_dir))
elif elem.tag == "write_stdout":
entries.append(IterationParser._parse_write(elem, io_buffer))
return entries
@staticmethod
def _parse_background(elem: ET.Element) -> BackgroundEntry:
def _parse_background(elem: ET.Element, work_dir: Path) -> BackgroundEntry:
entry = BackgroundEntry(
id=elem.get("id"),
script=elem.text
script=elem.text,
work_dir=work_dir
)
if elem.get("exit_code"):
entry.exit_code = int(elem.get("exit_code"))
@@ -97,9 +98,10 @@ class IterationParser:
)
@staticmethod
def _parse_repeat(elem: ET.Element) -> RepeatEntry:
def _parse_repeat(elem: ET.Element, work_dir: Path) -> RepeatEntry:
entry = RepeatEntry(
id=elem.get("id"),
work_dir=work_dir,
script=elem.text,
timeout=float(elem.get("timeout")) if elem.get("timeout") else None,
limit=int(elem.get("limit")) if elem.get("limit") else None
@@ -116,9 +118,10 @@ class IterationParser:
return entry
@staticmethod
def _parse_single(elem: ET.Element) -> SingleEntry:
def _parse_single(elem: ET.Element, work_dir: Path) -> SingleEntry:
entry = SingleEntry(
id=elem.get("id"),
work_dir=work_dir,
script=elem.text,
timeout=float(elem.get("timeout")) if elem.get("timeout") else None,
limit=int(elem.get("limit")) if elem.get("limit") else None

View File

@@ -1,4 +1,5 @@
from datetime import datetime
from pathlib import Path
import xml.etree.ElementTree as ET
from typing import Union
@@ -30,13 +31,15 @@ class ResponseParser:
io_buffer: Buffer to use for IO operations
"""
def __init__(self, io_buffer: IOBuffer):
def __init__(self, work_dir: Path, io_buffer: IOBuffer):
"""
Initialize parser with IO buffer.
Args:
work_dir: Workdir for the scripts
io_buffer: Buffer to use for IO operations
"""
self._work_dir = work_dir
self._io_buffer = io_buffer
@property
@@ -87,7 +90,7 @@ class ResponseParser:
elif root.tag == 'background':
if len(root) != 0 or root.attrib or root.text is None or root.text.strip() == '':
return ParseErrorEntry(entry_id, xml, "Background entry requires (only) script content")
return BackgroundEntry(entry_id, root.text)
return BackgroundEntry(entry_id, self._work_dir, root.text)
elif root.tag == 'repeat':
if len(root) != 0 or root.text is None or root.text.strip() == '':
@@ -98,7 +101,7 @@ class ResponseParser:
limit = root.get('limit')
timeout = float(timeout) if timeout is not None else None
limit = int(limit) if limit is not None else None
return RepeatEntry(entry_id, root.text, timeout, limit)
return RepeatEntry(entry_id, self._work_dir, root.text, timeout, limit)
elif root.tag == 'single':
if len(root) != 0 or root.text is None or root.text.strip() == '':
@@ -109,7 +112,7 @@ class ResponseParser:
limit = root.get('limit')
timeout = float(timeout) if timeout is not None else None
limit = int(limit) if limit is not None else None
return SingleEntry(entry_id, root.text, timeout, limit)
return SingleEntry(entry_id, self._work_dir, root.text, timeout, limit)
elif root.tag == 'reasoning':
if len(root) != 0 or root.attrib or root.text is None or root.text.strip() == '':

View File

@@ -1,3 +1,4 @@
from pathlib import Path
from aiohttp import web
import json
import asyncio
@@ -13,12 +14,14 @@ from ..working_memory import WorkingMemory
class Api:
def __init__(
self,
work_dir: Path,
app: web.Application,
agent: WebAgent,
io_buffer: WebIOBuffer,
working_memory: WorkingMemory,
auto_approver: AutoApprover
):
self._work_dir = work_dir
self._app = app
self._agent = agent
self._working_memory = working_memory
@@ -275,7 +278,7 @@ class Api:
if not content:
return web.Response(status=400, text="Missing content in request body")
entries = IterationParser.parse_iteration(content, self._io_buffer)
entries = IterationParser.parse_iteration(content, self._work_dir, self._io_buffer)
for entry in entries:
self._working_memory.add_entry(entry)