Skip to content

feat(gateway): session-level recall dedup with change detection (fixes #120) - #653

Open
heguang-mercy wants to merge 1 commit into
TencentCloud:mainfrom
heguang-mercy:feat/gateway-recall-dedup
Open

feat(gateway): session-level recall dedup with change detection (fixes #120)#653
heguang-mercy wants to merge 1 commit into
TencentCloud:mainfrom
heguang-mercy:feat/gateway-recall-dedup

Conversation

@heguang-mercy

Copy link
Copy Markdown

Background

Issue #120 and #523 observed that on the Gateway/Hermes path, every turn calls POST /recall (prefetch) and receives a byte-identical appendSystemContext (stable persona / scene / tools blocks) ??87.5% of injections within a session were exact duplicates. The redundant text is re-sent to the LLM every turn, wasting tokens.

The OpenClaw path is already covered by #188 / #335 / #375 / #514 (showInjected, injectionMode, prependSystemContext), but those PRs explicitly kept the Gateway /recall response compatible. This PR fills that gap.

Approach

Adds opt-in session-level dedup to the Gateway via a new SessionRecallDedup component (src/gateway/recall-dedup.ts):

  • Change detection, not "seen-before": a context is skipped only when it is byte-identical to the session's most recent /recall response. If the context changes (e.g. scene switch), it is served again ??safe even when the consumer rebuilds its prompt from scratch each turn.
  • When deduped, /recall returns context: "" + deduplicated: true (new optional field, fully backward compatible).
  • Keyed by session_key; entries expire after ttlMs (default 1h), capped at maxEntries (default 1000, LRU eviction), and cleared on /session/end.
  • Disabled by default ??existing deployments are unaffected until they explicitly opt in.

Configuration

recallDedup:
  enabled: false   # default; set true to activate
  ttlMs: 3600000
  maxEntries: 1000

Env vars: TDAI_GATEWAY_RECALL_DEDUP_ENABLED, TDAI_GATEWAY_RECALL_DEDUP_TTL_MS, TDAI_GATEWAY_RECALL_DEDUP_MAX_ENTRIES.

Changes

  • src/gateway/recall-dedup.ts ??new SessionRecallDedup + fingerprintOf
  • src/gateway/recall-dedup.test.ts ??13 unit tests
  • src/gateway/types.ts ??RecallResponse.deduplicated?: boolean
  • src/gateway/config.ts ??recallDedup config section (env + yaml)
  • src/gateway/server.ts ??wire dedup into /recall and /session/end
  • CHANGELOG.md ??entry under Unreleased

Testing

npx vitest run ??80/80 passing (5 files), including 13 new dedup tests covering: disabled passthrough, identical-repeat skip, session isolation, change-detection re-serve, empty-context passthrough, TTL expiry, LRU eviction, clearSession, and hit/miss counters.

Closes #120

Adds opt-in SessionRecallDedup to the Gateway /recall endpoint: within a
session, a byte-identical appendSystemContext is skipped (empty context +
deduplicated flag) until the content changes. Disabled by default; config
recallDedup.{enabled,ttlMs,maxEntries} + TDAI_GATEWAY_RECALL_DEDUP_* env vars.
13 new unit tests; full suite 80/80.

Closes TencentCloud#120
@Maxwell-Code07

Copy link
Copy Markdown
Collaborator

Thank you for submitting this PR and participating in Tencent Rhino-bird Open-source Training Program!
We have successfully received your submission. The program is currently in full swing, and we will complete the Code Review for you as soon as possible. Please keep an eye on the status notifications for this PR so you can follow up promptly once the review feedback is provided.
Thanks again for your contribution and open-source spirit! 🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants