Gemini Loop Prevention
Hypothesis
How can redundant loops in Gemini Code CLI be prevented through configuration, environment variables, and doctrine?
Approach
- Identified common loop patterns in Gemini CLI
- Analyzed configuration options (GEMINI.md, SYSTEM.md)
- Implemented Kill Switch environment variables
- Established stand-down order protocols
Findings
Key Observations
Common Loop Patterns:
| Pattern | Cause | Prevention |
|---|---|---|
| Retry Loop | Error → Retry → Same Error | Max retry limit |
| Approval Loop | Request → Deny → Request | Stand-down order |
| Search Loop | Search → No Result → Search | Scope limitation |
| Edit Loop | Edit → Fail → Edit | Verification gate |
GEMINI.md Configuration:
Project-level configuration to constrain behavior:
# GEMINI.md
## Loop Prevention Rules
1. Maximum 3 retries per operation
2. Stand down after 2 denied approvals
3. Report blockers instead of retrying
4. Request human guidance on repeated failures
## Kill Conditions
- GEMINI_MAX_RETRIES=3
- GEMINI_STANDOWN_ON_DENY=true
- GEMINI_SCOPE_CHECK=trueSYSTEM.md Doctrine:
System-level rules that override project config:
# SYSTEM.md
## Absolute Rules
1. Never modify files outside project scope
2. Never bypass approval requirements
3. Never retry indefinitely
4. Always report state after operationsEnvironment Variables (Kill Switch):
export GEMINI_MAX_RETRIES=3
export GEMINI_APPROVAL_REQUIRED=true
export GEMINI_SCOPE_LOCK=/path/to/project
export GEMINI_EMERGENCY_STOP=false # Set true to haltStand-Down Order Protocol
When to issue stand-down:
- After 2 denied approvals: Stop requesting same action
- After 3 failed attempts: Report blocker, wait for guidance
- On scope violation: Immediately halt, report violation
- On anomalous behavior: Emergency stop
Stand-Down Response:
"Standing down as requested. Current state:
- Completed: [list]
- Pending: [list]
- Blocker: [description]
Awaiting further instructions."
Outcome
Status: Success
Loop prevention patterns fully documented and ready for implementation.
Next Steps
- Add Kill Switch env vars to shell profile
- Create GEMINI.md and SYSTEM.md templates
- Test loop scenarios in controlled environment
- Document recovery procedures
Related
- Gemini CLI Improvement - Optimization patterns
- MCP Security - Security patterns
- Systematic Debugging - Error handling