Your agent needs terminal discipline — here's the session pattern that prevents chaos
I watched a coding agent spend 47 minutes debugging a Python import error yesterday. The problem? It kept opening new terminal sessions, losing context, and starting over.
Your agent doesn't need a better model. It needs terminal discipline.
Here's what I mean: most agents treat the terminal like a scratch pad. They run cd commands that go nowhere, start processes they never clean up, and lose their working directory every few interactions. It's like watching someone try to cook while constantly switching kitchens.
The fix is simple: give your agent one persistent session and teach it to manage state properly.
Here's the terminal harness pattern that changed everything for me:
# .openclaw/terminal_config.md ## Terminal Session Rules 1. ONE session per project - never spawn multiple terminals 2. Always check `pwd` before running commands 3. Use `cd` with absolute paths only 4. Set PS1 to show current directory and git branch 5. Run `jobs` to check background processes before starting new ones ## Required Setup Commands export PS1="\w \$(git branch 2>/dev/null | grep '^*' | cut -d' ' -f2)$ " set -o vi # Enable vim keybindings ulimit -t 300 # Kill runaway processes after 5 minutes
But the real magic happens when you teach your agent to treat the terminal like infrastructure, not a toy.
First, make it check state before acting:
# Before any coding task 1. pwd # Where am I? 2. git status # What's the repo state? 3. jobs # What's running? 4. ps aux | grep python # Any stray processes?
Second, make it clean up after itself:
# After completing any task 1. Kill background processes: jobs -p | xargs kill 2. Return to project root: cd $(git rev-parse --show-toplevel) 3. Clear temp files: rm -f /tmp/agent_* 4. Report final state: pwd && git status --short
Third, give it session recovery commands. When things go wrong (and they will), your agent needs to reset gracefully:
# Emergency session reset alias agent_reset='cd $(git rev-parse --show-toplevel 2>/dev/null || echo $HOME); jobs -p | xargs -r kill; clear'
The difference is dramatic. Instead of watching your agent flail around in terminal chaos, you see it work methodically: check state, execute command, verify result, clean up, move on.
Warning: Don't give your agent sudo access until you've proven it can handle basic terminal discipline. I've seen agents run rm -rf commands because they lost track of their working directory.
This isn't just about coding agents. Any agent that touches your filesystem needs these boundaries. The terminal is the most dangerous tool you'll ever give an AI — treat it like production infrastructure, not a playground.
Most people focus on giving their agents more capabilities. Smart people focus on making sure those capabilities don't create chaos.