Gemini Loop Prevention

Hypothesis

How can redundant loops in Gemini Code CLI be prevented through configuration, environment variables, and doctrine?

Approach

  1. Identified common loop patterns in Gemini CLI
  2. Analyzed configuration options (GEMINI.md, SYSTEM.md)
  3. Implemented Kill Switch environment variables
  4. Established stand-down order protocols

Findings

Key Observations

Common Loop Patterns:

PatternCausePrevention
Retry LoopError → Retry → Same ErrorMax retry limit
Approval LoopRequest → Deny → RequestStand-down order
Search LoopSearch → No Result → SearchScope limitation
Edit LoopEdit → Fail → EditVerification 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=true

SYSTEM.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 operations

Environment 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 halt

Stand-Down Order Protocol

When to issue stand-down:

  1. After 2 denied approvals: Stop requesting same action
  2. After 3 failed attempts: Report blocker, wait for guidance
  3. On scope violation: Immediately halt, report violation
  4. 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