Skip to content

Agent Zero AI framework

CWD in projects code exec

frdela7f74f907819
diff --git a/prompts/agent.system.main.tips.md b/prompts/agent.system.main.tips.md
index d1818c1a8..1940d912f 100644
--- a/prompts/agent.system.main.tips.md
+++ b/prompts/agent.system.main.tips.md
@@ -7,7 +7,7 @@ never assume success
memory refers memory tools not own knowledge
## Files
-save files in /root
+when not in project save files in /root
don't use spaces in file names
## Instruments
diff --git a/prompts/agent.system.projects.active.md b/prompts/agent.system.projects.active.md
index fa7433905..3d2ae63a9 100644
--- a/prompts/agent.system.projects.active.md
+++ b/prompts/agent.system.projects.active.md
@@ -6,6 +6,7 @@ Description: {{project_description}}
### Important project instructions MUST follow
- always work inside {{project_path}} directory
-- do not rename project directory, do no change meta files in .a0proj folder
+- do not rename project directory do not change meta files in .a0proj folder
+- cleanup when code accidentaly creates files outside move them
{{project_instructions}}
\ No newline at end of file
diff --git a/python/helpers/files.py b/python/helpers/files.py
index 3bcd93a04..ef8587ec6 100644
--- a/python/helpers/files.py
+++ b/python/helpers/files.py
@@ -423,6 +423,14 @@ def fix_dev_path(path: str):
return get_abs_path(path)
+def normalize_a0_path(path: str):
+ "Convert absolute paths into /a0/... paths"
+ if is_in_base_dir(path):
+ deabs = deabsolute_path(path)
+ return "/a0/" + deabs
+ return path
+
+
def exists(*relative_paths):
path = get_abs_path(*relative_paths)
return os.path.exists(path)
diff --git a/python/helpers/projects.py b/python/helpers/projects.py
index 39549b1bc..943db5385 100644
--- a/python/helpers/projects.py
+++ b/python/helpers/projects.py
@@ -244,6 +244,7 @@ def build_system_prompt_vars(name: str):
"project_name": project_data.get("title", ""),
"project_description": project_data.get("description", ""),
"project_instructions": complete_instructions or "",
+ "project_path": files.normalize_a0_path(get_project_folder(name)),
}
diff --git a/python/helpers/shell_local.py b/python/helpers/shell_local.py
index cc0815f10..226d78021 100644
--- a/python/helpers/shell_local.py
+++ b/python/helpers/shell_local.py
@@ -7,12 +7,13 @@ from python.helpers import tty_session
from python.helpers.shell_ssh import clean_string
class LocalInteractiveSession:
- def __init__(self):
+ def __init__(self, cwd: str|None = None):
self.session: tty_session.TTYSession|None = None
self.full_output = ''
+ self.cwd = cwd
async def connect(self):
- self.session = tty_session.TTYSession("/bin/bash")
+ self.session = tty_session.TTYSession("/bin/bash", cwd=self.cwd)
await self.session.start()
await self.session.read_full_until_idle(idle_timeout=1, total_timeout=1)
diff --git a/python/helpers/shell_ssh.py b/python/helpers/shell_ssh.py
index 9b12a8bd8..77ca5341d 100644
--- a/python/helpers/shell_ssh.py
+++ b/python/helpers/shell_ssh.py
@@ -14,7 +14,7 @@ class SSHInteractiveSession:
# ps1_label = "SSHInteractiveSession CLI>"
def __init__(
- self, logger: Log, hostname: str, port: int, username: str, password: str
+ self, logger: Log, hostname: str, port: int, username: str, password: str, cwd: str|None = None
):
self.logger = logger
self.hostname = hostname
@@ -27,6 +27,7 @@ class SSHInteractiveSession:
self.full_output = b""
self.last_command = b""
self.trimmed_command_length = 0 # Initialize trimmed_command_length
+ self.cwd = cwd
async def connect(self, keepalive_interval: int = 5):
"""
@@ -60,7 +61,11 @@ class SSHInteractiveSession:
# invoke interactive shell
self.shell = self.client.invoke_shell(width=100, height=50)
- self.shell.send("stty -echo\n".encode()) # disable local echo
+
+ initial_command = "stty -echo"
+ if self.cwd:
+ initial_command = f"cd {self.cwd}; {initial_command}"
+ self.shell.send(f"{initial_command}\n".encode())
# wait for initial prompt/output to settle
while True:
diff --git a/python/tools/code_execution_tool.py b/python/tools/code_execution_tool.py
index bff6147e4..6e993e522 100644
--- a/python/tools/code_execution_tool.py
+++ b/python/tools/code_execution_tool.py
@@ -3,7 +3,7 @@ from dataclasses import dataclass
import shlex
import time
from python.helpers.tool import Tool, Response
-from python.helpers import files, rfc_exchange
+from python.helpers import files, rfc_exchange, projects
from python.helpers.print_style import PrintStyle
from python.helpers.shell_local import LocalInteractiveSession
from python.helpers.shell_ssh import SSHInteractiveSession
@@ -146,9 +146,10 @@ class CodeExecution(Tool):
self.agent.config.code_exec_ssh_port,
self.agent.config.code_exec_ssh_user,
pswd,
+ cwd=self.get_cwd(),
)
else:
- shell = LocalInteractiveSession()
+ shell = LocalInteractiveSession(cwd=self.get_cwd())
shells[session] = ShellWrap(id=session, session=shell, running=False)
await shell.connect()
@@ -469,3 +470,14 @@ class CodeExecution(Tool):
output = "\n".join(line.strip() for line in output.splitlines())
output = truncate_text_agent(agent=self.agent, output=output, threshold=1000000) # ~1MB, larger outputs should be dumped to file, not read from terminal
return output
+
+ def get_cwd(self):
+ project_name = projects.get_context_project_name(self.agent.context)
+ if not project_name:
+ return None
+ project_path = projects.get_project_folder(project_name)
+ normalized = files.normalize_a0_path(project_path)
+ return normalized
+
+
+
\ No newline at end of file
diff --git a/usr/.gitkeep b/usr/.gitkeep
new file mode 100644
index 000000000..e69de29bb