Skip to content

[openclaw]: Context Engine's dispose function called after every turn causes premature ingestion and clears deduplication state #110

@DaevMithran

Description

@DaevMithran

The OpenClaw runtime is calling the context engine's dispose() method after every conversation turn, rather than only at session end.

Expected behavior

dispose() should be called once when the session ends, as a final opportunity to flush any remaining buffered episodes to the backend. The lifecycle should be:

  1. assemble() — inject context before inference
  2. afterTurn() — buffer the new episode
  3. compact() — called when context window fills up (ingests buffered episodes)
  4. dispose() — called once at session end to flush remaining buffer

Actual behavior

dispose() is being called after every turn. This means:

  • Buffered episodes are ingested immediately instead of being batched — each turn triggers backend.ingest() for just 1 episode, defeating the buffering strategy
  • injectedItemIds is cleared every turn — the deduplication set that prevents re-injecting already-seen context items is wiped, so the same context can be injected repeatedly in subsequent assemble() calls
  • Compaction never sees buffered messages — by the time compact() runs, the buffer is always empty because dispose() already flushed it

Impact

  • Excessive API calls to the backend (1 ingest per turn instead of batched)
  • Context deduplication is broken — previously injected items may be re-injected
  • SlidingWindowEngine's compaction strategy is effectively bypassed since it never has buffered messages to work with

Relevant code

  • dispose() implementation: src/engine/default.ts:42-49, src/engine/sliding-window.ts:84-91
  • Engine lifecycle base class: src/engine/base.ts
  • Plugin registration: src/index.ts:53

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