Skip to content

Bug: compaction/reset paths can break session invariants (messageProvider + token accounting) #69269

Description

@de1tydev

Problem

Compaction/reset paths can break session invariants in two related ways:

  1. hook contexts can lose or mis-normalize messageProvider
  2. successful compaction can leave stale token/cost accounting behind

In practice this causes downstream consumers to see one real conversation as multiple logical sessions, and can also trap a session in repeated compaction safeguard loops after the transcript has already been cleared.

Concrete affected paths

Today this cluster shows up across several connected paths:

  1. auto compaction hook dispatch via handleAutoCompactionStart/End
  2. engine-owned compaction hook dispatch in compact.queued.ts
  3. command reset hooks via emitResetCommandHooks
  4. gateway sessions.reset via emitGatewayBeforeResetPluginHook
  5. post-compaction session accounting when tokensAfter === 0

Reproduction A: provider invariants

  1. Use a channel-scoped session key like agent:main:feishu:default:direct:ou_xxx
  2. Register a plugin that uses ctx.sessionKey + ctx.messageProvider to build stable session identity
  3. Trigger auto compaction, /new, /reset, or sessions.reset
  4. Observe ctx.messageProvider missing, mixed-case, or inferred from a non-channel key shape
  5. Plugin falls back to unknown or treats Telegram / telegram as different providers
  6. One real conversation is split into multiple logical sessions

Reproduction B: token accounting invariants

  1. Start a long-running session that reaches compaction
  2. Let compaction collapse the remaining transcript to an effectively empty/zero-token state
  3. Observe the transcript is cleared but cached accounting still reports old totals
  4. Next turn hits the compaction safeguard again because totalTokens / cost state was not reset consistently

Expected

Compaction/reset code paths should preserve the same session invariants everywhere:

  • hook contexts should expose a normalized deliverable messageProvider when one is actually known
  • non-channel session keys should not manufacture fake providers
  • post-compaction token/cost accounting should be refreshed from the compaction result, including zero-token outcomes

Actual

Different paths currently preserve different subsets of those invariants, so downstream systems must guess or degrade.

Scope

This is broader than the original single-field hook bug. It covers the coordinated compaction/reset invariant cluster now reported separately in:

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: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

    Fields

    Priority

    None yet

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions