Skip to content

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

@de1tydev

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

    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