Skip to content

[Bug]: Context Compression Failure #22244

@0xharryriddle

Description

@0xharryriddle

Bug Description

The context compression system fails when attempting to generate summaries, resulting in warnings like:

WARNING: Failed to generate context summary: Expecting value: line 631 column 1 (char 3465)
WARNING: Failed to generate context summary: Expecting value: line 823 column 1 (char 4521)

This issue occurs when the auxiliary LLM provider returns a non-JSON response (plain text, HTML error page, or malformed JSON) instead of a valid JSON response. The OpenAI-compatible client attempts to parse the response as JSON, raising a json.JSONDecodeError that propagates to the _generate_summary method.

Steps to Reproduce

  1. Configure an auxiliary model that may return non-JSON responses (e.g., misconfigured endpoint, rate-limited provider, or provider returning HTML error pages)
  2. Run a conversation long enough to trigger context compression
  3. Observe the warning messages in the logs
  4. Note that the compression fails and middle conversation turns are dropped without a summary

Expected Behavior

  • Context compression should succeed by falling back to the main model when the auxiliary model returns invalid JSON
  • The system should log detailed diagnostics about the error
  • The agent should preserve as much conversational context as possible
  • No data loss should occur due to compression failures

Actual Behavior

  • Context compression fails completely
  • The system enters a 60-second cooldown period
  • Middle conversation turns are dropped without a summary
  • No detailed diagnostics are logged about the provider or error position
  • The agent loses conversational context

Affected Component

Agent Core (conversation loop, context compression, memory)

Messaging Platform (if gateway-related)

No response

Debug Report

Debug report uploaded:
  Report       https://paste.rs/qeLV4
  agent.log    https://dpaste.com/3UXBRZUPF
  gateway.log  https://dpaste.com/2KX4JLVCZ

Operating System

macOs 26.3.1

Python Version

3.14.3

Hermes Version

0.13.0

Additional Logs / Traceback (optional)

Root Cause Analysis (optional)

The root cause is that the _generate_summary method in agent/context_compressor.py did not explicitly handle json.JSONDecodeError exceptions. When the auxiliary LLM provider returns a non-JSON response, the JSON parsing fails, but the existing error handling only covered:

  • Model not found errors (404, 503)
  • Timeout errors
  • Transient errors (rate limits, network issues)

The JSON decode error was caught by the generic except Exception block, which:

  • Did not log provider details or error position
  • Did not attempt fallback to the main model
  • Entered a 60-second cooldown period
  • Returned None, causing the middle turns to be dropped

This resulted in context compression failures and potential data loss when providers returned invalid responses.

Proposed Fix (optional)

No response

Are you willing to submit a PR for this?

  • I'd like to fix this myself and submit a PR

Metadata

Metadata

Assignees

No one assigned

    Labels

    P2Medium — degraded but workaround existscomp/agentCore agent loop, run_agent.py, prompt buildertype/bugSomething isn't working

    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