Repository navigation
fix(tools): give the agent its own scratch directory instead of the user's repo - #3907
Conversation
Verdict: Approve with suggestionsCoding tasks used to leave scratch files in the user's repo because the agent had nowhere else to write. Each agent now gets its own private scratch folder, the prompt tells it to use that folder, the folder is deleted when the agent closes, and a refused temp path now says where to write instead. The design is tight: the rest of the system temp directory stays off-limits, the grant is never written to the machine-wide approvals file, and secret/blocked-file protection still applies inside the scratch folder. One thing to land before merge: this changes the system prompt, and the project rule is that a prompt change needs an agent eval run compared against the baseline. The PR itself marks that as pending, and every test-plan box is still unchecked — worth ticking off what has actually been run. The suggestions below are all small: the "write here instead" hint only reaches some of the refusal messages (file edits and shell working-directory refusals still give the bare "access denied"); the new prompt line is added even for the lean chat mode, which has no file-writing tools at all; and the cleanup step is less forgiving of unexpected errors than every other step around it. Real-world evidenceNo Test suites from the PR's test plan: 🔍 Technical details🟡 ImportantPrompt change has no eval run yet. 🟢 Minor1. The scratch hint reaches only two of the refusal paths. One example, applied at 2. The prompt line goes to profiles with no write tools. Confirmed: 3. Two spellings of the same directory ( 4. Cleanup is narrower than its neighbours ( Strengths
|
|
Verdict: Approve with suggestions The scratch-directory feature is a clean, well-scoped fix: throwaway scripts no longer land in the user's project, secrets and blocked-file checks still apply inside the scratch dir, the LLM gets a useful hint on denials, and the scratch dir is excluded from search roots. Tests cover grants, denials, hints, secret protection, and cleanup. Two small things worth tidying before the next push: 🟢 The 🟢 The 🔍 Technical detailsFixture teardown ( # current — fragile against subdirectories
for child in path.iterdir():
child.unlink()
path.rmdir()
# suggested
import shutil
shutil.rmtree(path)Docs import order ( |
|
Every tool that refuses a temp path now points the model at the scratch directory, and an agent that can't write files (the Skipped: the startup sweep of leftover 🔍 Technical detailsCommits:
|
|
Added a commit: the scratch directory now has the same path every session for a given project. The random per-process name was written into the system prompt, so no two sessions ever shared a cached prompt prefix; measured on the benchmark, the only difference between two processes' system prompts was that line. The path is derived from user and project, created private, and a planted symlink or a directory owned by someone else is refused for a fresh private one (test included). |
|
🟡 The The example shows 🔍 Technical details
# current (teaches random-per-session pattern):
session.set_scratch_dir(tempfile.mkdtemp(prefix="gaia-scratch-"))
# should be (matches the implementation and the fix rationale):
from gaia.security import stable_scratch_dir
session.set_scratch_dir(str(stable_scratch_dir(Path.cwd())))
|
|
Refreshed this branch against current One thing for the author: the new 🔍 Technical detailsOnly Existing assertions match on
Merged, not rebased — no commit on this branch was rewritten. |
…ser's repo Coding tasks left throwaway test runners and intermediate files in the user's repository: the system temp dir is outside the allowed paths, so write_file and execute_python_file refused it and the project root was the only writable place the agent knew. Each ChatAgent (and so GaiaAgent) now creates a private mkdtemp scratch directory, grants exactly that directory in its PathValidator, names it in one system-prompt line, and removes it in the existing cleanup path. The rest of the temp dir stays denied; refusals for temp paths now name the scratch directory. Secret and blocked-file checks still apply inside it, and file search skips it so it never outranks the project as the primary search root. Fixes amd#3734
…y refusal The "write here instead" hint now reaches edit_file, edit_python_file, replace_function, search_code, update_gaia_md and the shell working-directory check, not just write/read and execute_python_file. An agent with no file-creating tool (the chat profile) no longer gets a scratch directory or the prompt line pointing at one. The prompt and the refusal hint now spell the directory the same way, and cleanup logs any failure instead of letting it escape close().
The session scratch dir lives in the system temp dir by design, so an empty temp dir is no longer the invariant; no gaia-run-* snippet left behind is.
…session The scratch directory was created with a random name per process, and its path is written into the system prompt. So every session's system prompt differed by that one line, and a backend's cached prompt prefix never matched across sessions - on a local model, across every TUI or sidecar restart. The name is now derived from the user and the project. A predictable name under a shared temp directory is created private, and one that exists but is not a directory this user owns is refused, loudly, for a fresh private one.
The SHA-1 derives a stable directory name from owner + project path; it guards nothing, and ownership is enforced by the lstat check below. Bandit B324 is HIGH-severity and blocks the gate. Matches context7_cache.py.
2787c0b to
c8fcfd2
Compare
Coding tasks left the user's repository dirty: the agent had nowhere else to put a quick test runner or an intermediate file, because the system temp dir is outside its allowed paths (30 of 30
write_fileerrors in a 168-run benchmark were/tmprefusals). Each agent that can write files now gets its own scratch directory, named in its prompt and deleted when the agent closes. The rest of the temp dir stays denied, and any tool that refuses a temp path now says where to write instead. Secret-file and blocked-file checks still apply inside the scratch directory.Fixes #3734
python -m pytest tests/unit/test_scratch_dir.py tests/unit/test_chat_agent_integration.py -k scratch: the scratch dir is writable, readable and runnable;/tmp/otheris denied and the refusal names the scratch dir, including fromedit_file,search_codeand the shell working-directory check;.env/id_rsa/*.pemare still refused inside it; thechatprofile gets no scratch dir; the dir is removed on cleanuppython -m pytest tests/unit/test_file_access_scope.py tests/unit/test_file_write_guardrails.py tests/unit/test_security_edge_cases.py tests/unit/test_file_search_scope.py tests/unit/test_file_tools.py: existing guardrails unchangedtest_profilespec_characterization) and budget tests unchanged. The prompt assertions match; the tool-set assertions fail the same way on an unmodifiedorigin/maincheckout (pre-existing), so this stays unticked🔍 Technical details
PathValidator.set_scratch_dir()grants exactly that directory.scratch_hint()adds the scratch dir to every "not in allowed paths" refusal for a path under the system temp dir:validate_read/validate_write,execute_python_file, the five file-IO tools that build their own refusal, andrun_shell_command's working-directory check.tempfile.mkdtemp(prefix="gaia-scratch-")(mode 0700) after tool registration, and only when a file-creating tool (write_file,write_python_file,write_markdown_file) is registered. It removes the dir in its existing__del__, whichGaiaAgent.close()also calls. There is no new lifecycle hook.search_roots()skips the scratch dir. On macOS it nests deeper than most repos, so it would otherwise outrank the project as the primary file-search root.