Skip to content

Agent loops after providing answer in non-interactive modes (interactive: false or direct CLI prompt) #403

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
BearThreat opened this issue May 28, 2025 · 1 comment
Labels
bug Something isn't working

Comments

@BearThreat
Copy link

When MyCoder is run with interactive: false in the configuration, or when a prompt is provided directly via the CLI (e.g., mycoder "some prompt"), the agent correctly processes the request and provides an initial answer. However, instead of exiting cleanly, it enters an indefinite loop, repeatedly generating follow-up messages asking for confirmation or if further assistance is needed. The process does not terminate on its own and requires manual intervention (Ctrl+C or kill when using mycoder -i).

This behavior is observed even for simple, non-tool-intensive tasks (e.g., "What is the capital of France?") and persists whether profile is true or false.

Environment:

  • MyCoder Version: Installed via npm install -g mycoder
  • Node.js Version: v22.16.0 (npm v10.9.2) (Managed via NVM for Linux)
  • Operating System: Windows 10 with WSL2
  • WSL Distro: Ubuntu
  • Terminal: VSCode Integrated Terminal
  • Git Version: git version 2.34.1
  • GitHub CLI Version: gh version 2.4.0+dfsg1

mycoder.config.js:

// mycoder.config.js
export default {
  githubMode: false,

  // Browser settings (loop observed even without browser tasks)
  headless: true,
  userSession: false,
  browser: {
    useSystemBrowsers: false,
    preferredType: 'chromium',
    executablePath: null,
  },

  // Model settings (Ollama proxy to OpenRouter)
  provider: 'ollama',
  model: 'openai/gpt-4o', // Also observed with 'anthropic/claude-sonnet-4' via proxy
  baseUrl: 'http://localhost:11434', // Ollama-compatible OpenRouter proxy

  // Core issue observed with these settings
  interactive: false,
  profile: false, // Also observed with profile: true

  customPrompt: [],
  mcp: { servers: [] },
};

Steps to Reproduce:

  1. Set up mycoder.config.js as shown above.
  2. Ensure an Ollama-compatible proxy (e.g., to OpenRouter) is running and accessible at baseUrl.
  3. Run MyCoder with a simple prompt directly on the command line:
    mycoder "What is the capital of France?"
  4. Alternatively, run mycoder -i and enter the same prompt. Ensure interactive: false is in the config.

Expected Behavior:
The agent should provide the answer (e.g., "The capital of France is Paris.") and then terminate the process cleanly without further interaction, especially when a prompt is given directly on the CLI.

Actual Behavior:
The agent provides the correct answer, then enters a loop. Example output snippet:

The capital of France is Paris.
Please let me know if this answer is acceptable, or if you need any changes or additional information.
I understand this is the correct information for your inquiry. If you have more questions, need further assistance, or would like further clarifications, feel free to let me know!
I hope this answer meets your expectations. If there is anything else you need or any follow-up questions you might have, feel free to ask!
... (continues until manually terminated with Ctrl+C or kill) ...

The process consumes resources and generates extensive token usage due to the loop.

Additional Notes:

  • Ctrl+C works to terminate the process when run as mycoder "prompt".
  • Ctrl+C does not reliably work when run in mycoder -i shell mode (requires kill from another terminal). This might be a separate TTY/signal handling issue within the -i mode.
  • The Ollama OpenRouter proxy backend (using https://github.com/xsharov/enchanted-ollama-openrouter-proxy) appears to be functioning correctly as the initial answer is accurate. The issue seems to be in MyCoder's agent logic for task completion in non-interactive scenarios.
@bhouston bhouston added the bug Something isn't working label May 28, 2025
@bhouston
Copy link
Member

Issue Triage Analysis

Issue Classification

  • Type: Bug
  • Labels: bug

Root Cause Analysis

After investigating the issue, I've identified the likely cause of the agent looping in non-interactive mode:

  1. In the toolAgent function (packages/agent/src/core/toolAgent/toolAgentCore.ts), the agent continues running in a loop until one of two conditions is met:

    • The maximum number of iterations is reached (config.maxIterations)
    • The agentDone tool is called, which sets agentDoned to true
  2. In non-interactive mode, after providing an answer, the agent doesn't receive any feedback from the user. It then continues to generate follow-up messages asking for confirmation, but since there's no user interaction, it never receives a signal to call the agentDone tool.

  3. The CLI sets up a force exit mechanism (setupForceExit) with a timeout of 5000ms in packages/cli/src/index.ts, but this only happens after the toolAgent function returns. Since the agent is stuck in a loop, the force exit mechanism is never triggered.

Possible Solutions

  1. Add a Non-Interactive Mode Termination Signal:

    • Modify the toolAgent function to detect when it's running in non-interactive mode and add logic to automatically call agentDone after providing an initial answer
    • This could be done by adding a new configuration option like autoTerminate: true for non-interactive mode
  2. Add a Maximum Response Count:

    • Add a configuration option to limit the number of responses the agent can generate in non-interactive mode
    • After reaching this limit, automatically call agentDone
  3. Detect Response Patterns:

    • Add logic to detect when the agent is generating repetitive follow-up messages without receiving user input
    • If this pattern is detected, automatically terminate the agent
  4. Explicit CLI Handling:

    • Modify the CLI to handle non-interactive mode differently, wrapping the toolAgent call with additional logic to terminate it after receiving the initial response

Recommendation

The most straightforward solution would be to add a new configuration option (e.g., autoTerminate) that is automatically set to true when interactive: false is set or when a prompt is provided directly via the CLI. This option would signal the toolAgent function to automatically call agentDone after providing an initial answer in non-interactive mode.

This change would need to be implemented in:

  1. The toolAgent function in packages/agent/src/core/toolAgent/toolAgentCore.ts
  2. The CLI configuration handling in packages/cli/src/commands/$default.ts

Would you like me to provide more specific implementation details or explore any of these solutions further?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants