Skip to content

[Bug]: 🚨sessions_history tool returns unbounded results when limit is not specified, causing context overflow #12432

@Baoxd123

Description

@Baoxd123

Summary

sessions_history tool has no default limit on the number of messages returned. When an agent calls it without specifying limit, all messages from the target session are returned, which can easily cause context overflow in the calling session.

Steps to Reproduce

  1. Have a long-running session with many tool calls (large history)
  2. From another session (or the same session after reset), call sessions_history without a limit parameter
  3. The tool returns the entire session history, potentially hundreds of messages with full tool outputs
  4. The calling session context overflows immediately

Root Cause

In src/agents/tools/sessions-history-tool.ts (lines 251-254), when limit is not provided, it resolves to undefined, which is passed directly to the gateway chat.history method — resulting in all messages being returned:

const limit =
  typeof params.limit === "number" && Number.isFinite(params.limit)
    ? Math.max(1, Math.floor(params.limit))
    : undefined;  // <-- no default, returns everything

Impact

  • Agent calls sessions_history to investigate an issue → gets entire session dump → instant context overflow
  • This is especially dangerous after a context overflow + reset, where the agent naturally wants to check what happened in the previous session
  • In our case, this caused two consecutive context overflows within minutes

Proposed Fix

Set a reasonable default limit (e.g., 20 messages) when the caller does not specify one:

const limit =
  typeof params.limit === "number" && Number.isFinite(params.limit)
    ? Math.max(1, Math.floor(params.limit))
    : 20;  // safe default

Agents that need more history can explicitly pass a higher limit.

Environment

  • OpenClaw 2026.2.6-3 (85ed6c7)
  • macOS Darwin 24.6.0 (arm64)
  • Model: anthropic/claude-sonnet-4-5 (200k context)

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions