Skip to content

Main session prompt crash: Cannot read properties of undefined (reading 'length') in compaction token estimation #63612

@DSVinC

Description

@DSVinC

Main session prompt crash: Cannot read properties of undefined (reading 'length')

Date: 2026-04-09
Host: macOS
OpenClaw version: 2026.4.8

Summary

A long-lived main session can become unrecoverable with the error:

Agent failed before reply: Cannot read properties of undefined (reading 'length')

This is not a provider/model error. The crash happens before prompt submission, during pre-prompt compaction token estimation.

Exact stack

Observed via added local diagnostic logging:

TypeError: Cannot read properties of undefined (reading 'length')
    at estimateTokens (file:///Users/vvc/.local/lib/node_modules/openclaw/node_modules/@mariozechner/pi-coding-agent/src/core/compaction/compaction.ts:257:26)
    at file:///Users/vvc/.local/lib/node_modules/openclaw/dist/pi-embedded-CNTNdlGw.js:20243:73
    at Array.reduce (<anonymous>)
    at estimateMessagesTokens (file:///Users/vvc/.local/lib/node_modules/openclaw/dist/pi-embedded-CNTNdlGw.js:20243:42)
    at estimatePrePromptTokens (file:///Users/vvc/.local/lib/node_modules/openclaw/dist/pi-embedded-CNTNdlGw.js:31147:20)
    at shouldPreemptivelyCompactBeforePrompt (file:///Users/vvc/.local/lib/node_modules/openclaw/dist/pi-embedded-CNTNdlGw.js:31151:32)
    at runEmbeddedAttempt (file:///Users/vvc/.local/lib/node_modules/openclaw/dist/pi-embedded-CNTNdlGw.js:32286:35)

Impact

  • main session cannot reply at all.
  • All fallback models fail the same way because the error occurs before the request reaches the provider.
  • Long-lived sessions are affected most often because they pass through preemptive compaction/token estimation on every new turn.

What seems to trigger it

  • A long-lived main session with mixed message history:
    • user/assistant messages
    • toolResult messages
    • compaction/summary-related messages
  • At least one replayed message block appears to have a shape that is valid enough to survive history loading, but invalid for compaction token estimation.

Root cause

The crash point is in pi-coding-agent compaction token estimation logic. estimateTokens() assumes message block fields always exist:

  • assistant.content is iterable
  • block.text.length is always safe
  • block.thinking.length is always safe
  • block.name.length is always safe for tool calls
  • toolResult/custom.content is always an array or string

That assumption breaks on malformed or partially-normalized history blocks.

Local evidence from runtime logs

Representative runtime log lines:

embedded_prompt_error_stack
embedded run failover decision: stage=prompt decision=surface_error
lane task error: lane=session:agent:main:main error="TypeError: Cannot read properties of undefined (reading 'length')"
Embedded agent failed before reply: Cannot read properties of undefined (reading 'length')

Local mitigation that restored the session

Two layers were patched locally:

  1. Normalize replayed message content before replay sanitization/validation in:
    • dist/pi-embedded-CNTNdlGw.js
  2. Add null guards in compaction token estimation in:
    • node_modules/@mariozechner/pi-coding-agent/dist/core/compaction/compaction.js

Specifically, local guards were added for:

  • missing/non-array assistant.content
  • missing/non-array toolResult/custom.content
  • missing block.text
  • missing block.thinking
  • missing block.name

After patching the compaction estimateTokens() path, the main session recovered.

Suggested upstream fix

In pi-coding-agent compaction token estimation, treat malformed blocks as zero-length instead of throwing.

Minimal expectation:

  • return 0 for invalid message objects
  • treat missing content as empty
  • gate all .length access behind type checks

Why this matters

This is a high-impact session-killer bug because it affects the recovery path itself:

  • once a main session contains one malformed block
  • every subsequent prompt attempt can fail before provider dispatch
  • model fallback cannot help

Notes

This issue was observed on a heavily-used main session, but the underlying bug looks general and could affect any long-running session that accumulates malformed history blocks.

Metadata

Metadata

Assignees

No one assigned

    Labels

    P1High-priority user-facing bug, regression, or broken workflow.clawsweeper:linked-pr-openClawSweeper found an open linked pull request for this issue.clawsweeper:no-new-fix-prClawSweeper does not recommend queueing a new automated fix PR for this issue.clawsweeper:source-reproClawSweeper found a high-confidence source-level issue reproduction.impact:crash-loopCrash, hang, restart loop, or process-level availability failure.impact:session-stateSession, memory, transcript, context, or agent state can drift or corrupt.issue-rating: 🦞 diamond lobsterVery strong issue quality with high-confidence source-level or clear reproduction.staleMarked as stale due to inactivity

    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