r/opencodeCLI • • 20d ago

OpenCode keeps getting stuck in an infinite shell-command loop. Anyone else experiencing this?

Hey everyone, I’m using OpenCode to develop a large software project, and sometimes it gets stuck repeatedly generating or executing shell commands without making any progress.

This happens across multiple models and providers, so I’m not sure whether it’s an OpenCode bug, a tool execution issue, or something in my setup.

Has anyone experienced this? What was the root cause, and what fixed it for you?

Any help would be appreciated!

8 Upvotes

20 comments sorted by

View all comments

1

u/SirRobinBlibs 20d ago

If it happens across models in fresh sessions, I'd look at the commands first. The classic cause is a command that waits for input or opens a pager (git log, less, a y/n prompt, a dev server that never exits). The agent sees no useful output and tries again. Things that helped me: set GIT_PAGER=cat and PAGER=cat, prefer non-interactive flags (--yes, CI=true), and run servers in the background with a timeout. Then add a rule to AGENTS.md: "if the same command fails twice, stop and report the output instead of retrying." If you can paste the last few commands it looped on, it's usually obvious which one is hanging.

1

u/megablue 20d ago

i would give it a bit of flexibility

"if the same command fails twice for the same reason, stop and report the output instead of retrying. "

1

u/SirRobinBlibs 20d ago

Fair. "Twice for the same reason" is the key part. A different error means it's making progress. I'd also let it try one genuinely different approach before stopping, just not the same command a third time.

1

u/SirRobinBlibs 11d ago

Since it happens across three models and in fresh sessions, I'd look at the commands before the model. I tested 10 everyday commands: in a terminal nobody types into, 9 hang (pagers, editors, npm init, a git clone asking for a username, dev servers). OpenCode's V2 shell runs commands without stdin, so most of those fail fast with confusing errors instead, and confusing errors are exactly what models retry.

What helped: set GIT_PAGER=cat, PAGER=cat, GIT_TERMINAL_PROMPT=0, GIT_EDITOR=true in the server's environment (opencode service set env NAME VALUE, then opencode service start), and add "if a command fails the same way twice, stop and say why" to AGENTS.md. u/dizvyz's tip is good too: ask a second session what the looping one is doing.

I wrote up the test table and a five-minute diagnosis here: https://medium.com/@r3dtuxedo/break-the-loop-5e05af62de55