Skip to content

Compaction fails with 'Cannot read properties of undefined (reading length)' #3225

@senseji

Description

@senseji

Summary

When /compact runs or auto-compaction triggers, a TypeError: Cannot read properties of undefined (reading 'length') occurs.

Stack Trace

The error occurs in limitHistoryTurns (history.js) when messages is undefined:

if (!limit || limit <= 0 || messages.length === 0) return messages;

Root Cause

In compact.js, session.messages can be undefined in edge cases (e.g., corrupted session file, SDK edge case). This undefined value flows through sanitizeSessionHistory to limitHistoryTurns, which doesn't guard against it.

Suggested Fix

Add a guard in limitHistoryTurns:

export function limitHistoryTurns(messages, limit) {
    if (!messages || !limit || limit <= 0 || messages.length === 0) return messages ?? [];
    // ...
}

Or guard upstream in compact.js:

const prior = await sanitizeSessionHistory({ messages: session.messages ?? [], ... });

Environment

  • Clawdbot version: 2026.1.24-3
  • OS: macOS (arm64)
  • Node: v22.11.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions