Skip to content

[Performance] sessions.list slow - N+1 transcript fallback + full row build before limit #57715

@loyur

Description

@loyur

Problem

sessions.list RPC takes 5-8s per call in our environment, causing Gateway backpressure under normal polling load.

Root Cause

  1. Full row build before filter/limit: listSessionsFromStore() builds the full gateway session row for all 253 sessions before applying limit. Even limit=20 doesn't reduce work.

  2. N+1 transcript fallback: buildGatewaySessionRow() calls resolveTranscriptUsageFallback() for ~122/253 sessions that lack totalTokens/contextTokens/estimatedCostUsd fields. Each fallback reads the transcript file from disk.

  3. O(N²) child session resolution: resolveChildSessionKeys() scans the full store for every row.

Evidence

  • sessions.json deserialization is fast (~15ms total)
  • 44% of sessions.list calls exceed 8s under concurrent polling
  • Peak: 234 slow calls at 18:00 when multiple cron jobs trigger
  • limit parameter has near-zero effect on latency

Suggested Fix

In src/gateway/session-utils.ts - listSessionsFromStore():

  1. Reorder: filter/sort/limit using cheap entry fields BEFORE buildGatewaySessionRow()
  2. Pre-compute child index at list level instead of per-row scan
  3. Consider TTL cache for repeated calls with no store changes

Environment

  • OpenClaw: v2026.3.x
  • Sessions: ~253 total across 5 agents
  • OS: Darwin/arm64

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