Design, implementation and evaluation
Autonomous Docker Terminal Agent
A ReAct agent that drives a persistent PTY inside a hardened Docker sandbox, answers its own interactive prompts, and cannot finish until the tests pass.
- Tasks solved on a fixed 10-task suite
- 9/10
- Average steps per task
- 7.9
- Average model cost per task
- $0.006
The problem
Most terminal agents pipe strings into a normal child process. That works until a script asks a question: read -p, a [y/N] prompt, a password. The process blocks, the agent waits forever, and the run dies with no useful signal. The second problem is worse than the hang: an LLM with shell access will eventually try to write outside its workspace, and a naive path check does not stop a symlink.
So the real problem was not "let a model run bash". It was building an execution environment where a model can run bash, hang, fail and misbehave without taking the host with it — and still be provably finished when it claims to be.
Approach
- Persistent PTY, not spawned commands. A
node-ptyshell keepscd, environment variables and history across turns. Large generated scripts are written to host-side files and sourced into the session instead of being pasted through the 4 KB TTY buffer. - A sentinel-based exit code. Every command is followed by a randomised
__DONE_UUID__:$?marker, so the agent reads the real exit code of the command and not of its wrapper. - Hang detection. A background heartbeat notices an idle prompt and hands the model a
send_inputtool, so it can type an answer and press enter without standard IO piping.interruptsends Ctrl-C when a loop will not end. - A symlink-safe path jail. Iterative
lstatandrealpathresolution runs on every file operation, so dangling symlinks and../..traversal resolve to a rejection rather than to/etc. - Context that survives noise. Up to 220k characters are buffered and aggressively truncated before the API call; old tool outputs are replaced with lightweight stubs so a noisy install cannot erase the objective.
- A finish gate. The
finishtool stays locked until a test command returns exit code 0. The agent cannot declare success on vibes. - Seven tools and a 25-step cap inside ephemeral
node:20-bookworm-slimcontainers started with--cap-drop=ALLand restricted PIDs.
- 01Objective
- 02ReAct loop: reason, then act
- 03PTY shell inside a Docker sandbox
- 04Sentinel exit codes and hang detection
- 05Context compaction with stubbed history
- 06finish, gated on a passing test run
Results
Evaluated on a fixed suite of 10 tasks that includes interactive prompts, syntax errors, infinite loops and a deliberate symlink escape attempt, using gpt-oss-120b via Groq.
| Metric | Value | Note |
|---|---|---|
| Tasks solved | 9/10 | 90% pass rate on the suite |
| Average steps | 7.9 | Per task |
| Average cost | $0.006 | Per task |
| Tools exposed | 7 | send_input, interrupt, finish among them |
| Step cap | 25 | Hard limit per task |
| Sandbox | --cap-drop=ALL | Plus PID, CPU and memory limits |
Three edge cases were handled by the general mechanism rather than by special-casing:
| Case in the suite | What breaks a naive agent | What handles it here |
|---|---|---|
A hanging read -p prompt | The process blocks and the run dies on timeout | send_input types the answer and presses enter |
An infinite while true loop | The agent waits until the step cap with nothing to show | interrupt sends Ctrl-C, then the script is rewritten |
A write to /etc/hacked.txt | A sibling prefix or symlink escapes the workspace | realpath jail rejects it and the agent recovers to /workspace |
The one failure is reported as a failure. A 9/10 with the failing case documented is more useful than a claimed 10/10 that quietly dropped the hard task.
What went wrong
The PTY sentinel bug. Exit codes were occasionally read from the wrong command because the sentinel matched the shell's own echoed command line, so a failing run could look clean. The fix was a no-echo shell plus a randomised, digit-anchored sentinel, with a Vitest regression test that reproduces the interleaving.
Three path-jail escapes. A sibling-prefix path, a write through a symlinked directory, and a dangling symlink could each resolve outside /workspace. Each was closed with realpath-based resolution and a regression test, so the next refactor cannot quietly reopen them.
Limits. Docker Desktop is required, the suite is only ten tasks, and the budget is one model on one provider. The cost figure is model spend, not the machine time the sandbox consumes.