Skip to content

Session hangs when context reaches ~198k tokens — no user reply, infinite tool loop; file-based reset doesn't clear in-memory state #51154

@IIIyban

Description

@IIIyban

Bug Report

Summary

When a session approaches the context limit (~198k/200k tokens), the agent enters an infinite tool-call loop and never sends a final reply to the user. The chat appears dead — the user sees no response. Additionally, resetting the session by deleting/renaming the .jsonl file does not clear the in-memory session state in the gateway.

Environment

  • OpenClaw version: 2026.3.13 (61d171a)
  • OS: Ubuntu Linux 6.8.0-106-generic (x64)
  • Node: v22.22.0
  • Model: anthropic/claude-sonnet-4-6 (OAuth)
  • Channel: Telegram (group/topic)

Steps to Reproduce

Bug 1: Agent hangs at ~198k tokens

  1. Have a long conversation in a Telegram group topic until context reaches ~198k tokens
  2. Send a message to the agent
  3. Agent starts executing tool calls (exec, process.poll, etc.) but never emits a stop with a text reply
  4. User sees no response — chat appears dead
  5. sessions_list shows stopReason: "toolUse" or stopReason: "aborted" as the last state
  6. abortedLastRun: false — the session is not marked as broken

Bug 2: File-based session reset does not work

  1. Rename/delete the session .jsonl file to reset the session
  2. Gateway creates a new (small) .jsonl file on next message
  3. But sessions_list still reports the OLD totalTokens: 198706 — the in-memory state was not cleared
  4. The session ID remains the same (5f53d330-...)
  5. Agent continues from the old bloated state despite the file being replaced

Expected Behavior

Bug 1:

  • When context approaches the limit, the agent should either:
    • Auto-compact (trigger compaction before hitting the wall)
    • Or gracefully stop tool calls and send a text reply like "Context limit reached, please start a new thread"
  • The agent should NEVER silently hang with no user-visible output

Bug 2:

  • Deleting/renaming the .jsonl file should trigger the gateway to re-read from disk and reset the in-memory session state
  • Or there should be an API/CLI command like openclaw session reset <key> to properly reset a session

Actual Behavior

  • Agent enters infinite tool loop, user gets no reply
  • File deletion does not reset in-memory state
  • Only openclaw gateway restart clears the state (but kills ALL sessions)

Impact

  • Critical for production use: users in Telegram/Discord groups see the bot go silent with no explanation
  • No self-healing: the session stays broken until manual intervention
  • Data loss risk: the only workaround is to delete the session file and lose all conversation history, or restart the entire gateway

Workaround

  1. Backup the session .jsonl file
  2. Restart the gateway (openclaw gateway restart)
  3. Inject a summary of the previous conversation to restore context

Session Details (for debugging)

  • Session key: agent:main:telegram:group:-1003829863617:topic:7524
  • Session ID: 5f53d330-4186-4831-ac2d-be94c4685eb1
  • Transcript: 5f53d330-4186-4831-ac2d-be94c4685eb1-topic-7524.jsonl
  • Total tokens at hang: 198,706 / 200,000
  • Last stopReason: "aborted" with errorMessage: "Request was aborted."

Suggested Improvements

  1. Proactive compaction — trigger compaction at ~90% context usage, not at 100%
  2. Tool loop breaker — if agent does N consecutive tool calls without a text reply, force a text response
  3. Graceful degradation — send "context full" message to user instead of silent hang
  4. Session reset APIopenclaw session reset <key> that properly clears both disk and memory
  5. File watch — gateway should detect .jsonl file changes and reload session state

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions