Skip to content

Heuristic Code Safety

The runpy and bash tools are protected by a 3-layer heuristic safety system that prevents dangerous operations. The safety system is implemented in backend/tools/lib/heuristic_safety.py and is applied before any command is executed.

The first layer blocks dangerous patterns in the command string using regex matching.

PatternExampleReason
rm -rf / or rm -rf *rm -rf /Mass file deletion
dd if=dd if=/dev/zeroRaw disk writing
mkfsmkfs.ext4Filesystem formatting
>/dev/sd> /dev/sdaDirect disk writing
wget + `bash``wget …
curl + `bash``curl …
chmod 777chmod 777 /World-writable permissions
chown rootchown root /Privilege escalation
iptablesiptables -FFirewall manipulation
shutdownshutdown -h nowSystem shutdown
rebootrebootSystem reboot
kill -9kill -9 1Force kill processes

The second layer ensures file operations stay within the agent’s workspace directory.

  • All file paths must be relative to the workspace directory
  • ../ traversal is rejected
  • Absolute paths outside the workspace are rejected
  • Symlinks pointing outside the workspace are rejected
Workspace: /workspace/agents/my_agent/workspace/
✓ Allowed: "write_file('notes.md', 'content')"
✗ Blocked: "write_file('../../etc/passwd', 'hacked')"
✗ Blocked: "write_file('/etc/shadow', 'hacked')"

The third layer restricts allowed commands and flags.

CategoryCommands
File operationscat, ls, find, grep, wc, head, tail, sort, uniq, diff, patch
Text processingsed, awk, tr, cut, tee
System infouname, df, du, free, whoami, id
Pythonpython3, pip, pip3
Gitgit status, git diff, git log (read-only)
Package managersapt-get update, apt-get install (with restrictions)
FlagReason
-rf (rm)Recursive force deletion
--force (many commands)Bypass safety checks
-i (rm)Interactive mode (can be abused)
--all (ls with dangerous commands)Access restricted files

If a command is blocked by any layer, the tool returns an error:

{
"error": "Command blocked by safety rule: Pattern 'rm -rf /' matches dangerous pattern 'mass_deletion'",
"rule": "mass_deletion",
"layer": 1
}

The error message includes:

  • The rule that was triggered
  • Which safety layer blocked it
  • A human-readable explanation

The safety system is configured in backend/tools/lib/heuristic_safety.py. To add new rules:

  1. Add the pattern to the appropriate layer’s rule list
  2. Provide a descriptive name for the rule
  3. Test with the heuristic safety test suite

Run the heuristic safety tests:

Terminal window
python3 tests/test_heuristic_safety.py

This verifies that all safety rules are working correctly and that legitimate commands are not blocked.