Skip to content

(feat) Enhanced Memory Management: (1) maintain a rolling cross-sessi…#96

Merged
linfangw merged 2 commits into
mainfrom
memory_enhancement
Mar 7, 2026
Merged

(feat) Enhanced Memory Management: (1) maintain a rolling cross-sessi…#96
linfangw merged 2 commits into
mainfrom
memory_enhancement

Conversation

@linfangw

@linfangw linfangw commented Mar 7, 2026

Copy link
Copy Markdown
Member

…on digest; (2)classify conversations by importance using a two-stage pipeline

Summary

  • Problem: OpenClaw lacks cross-session context continuity and proactive importance classification. The existing session-memory hook saves isolated per-session snapshots with no consolidated "recent context" view, and memory-flush only saves memory reactively near compaction time — meaning routine chit-chat and critical architecture decisions are treated identically.
  • Why it matters: Without a cross-session digest, the bot cannot reliably answer continuity questions ("what were we working on yesterday?"). Without proactive importance classification, high-value technical decisions, project milestones, and action items are never durably archived and can be lost after context window rotation.
  • What changed: Added two new bundled hooks (context-digest, session-importance), shared utilities (transcript-reader.ts, llm-memory-helpers.ts), a System Prompt Anchor (context-digest-anchor.ts), and a hotfix for PluginRuntime missing its channel.x binding (which caused a crash in qverisbot onboard after configuring Qveris).
  • What did NOT change: All existing mechanisms (session-memory, memory-flush, memory_search, memory_get) are completely unchanged. New hooks complement rather than replace existing behavior.

Change Type (select all)

  • Bug fix ← (the channel.x missing fix is a bug fix)
  • Feature
  • Refactor
  • Docs
  • Security hardening
  • Chore/infra

Scope (select all touched areas)

  • Gateway / orchestration
  • Skills / tool execution
  • Auth / tokens
  • Memory / storage
  • Integrations
  • API / contracts
  • UI / DX
  • CI/CD / infra

Linked Issue/PR

  • Closes #
  • Related #

User-visible / Behavior Changes

  1. context-digest hook (new, enabled by default): On every /new or /reset, a rolling 7-day cross-session digest is written to memory/context-digest.md. The digest is topic-organized with sections: Topics Discussed, Key Decisions, Open Items / Action Items, Important Context.
  2. session-importance hook (new, enabled by default): On every /new or /reset, the previous session is evaluated by a multi-dimensional scoring engine (code density, message depth, collaboration intensity, keyword signals). Sessions that pass the threshold are classified by LLM (or keyword fallback) and persisted to memory/important/YYYY-MM-DD-<category>-<slug>.md. Duplicate topic sessions are smart-appended rather than creating new files.
  3. System Prompt Anchor: The ## Open Items / Action Items section from context-digest.md (≤800 chars) is automatically injected into every session's system prompt, giving the model subconscious awareness of pending tasks without requiring an explicit memory_search call.
  4. qverisbot onboard crash fix: Fixed TypeError: Cannot read properties of undefined (reading 'listXAccountIds') that occurred after configuring Qveris during onboarding. The PluginRuntimeChannel type and createRuntimeChannel() were missing the x property entirely.
  5. Config knobs (all optional, under hooks.internal.entries):
    • context-digest.days (default: 7), context-digest.llmDigest (default: true)
    • session-importance.messages (default: 30), session-importance.llmClassify (default: true)

Security Impact (required)

  • New permissions/capabilities? No
  • Secrets/tokens handling changed? No
  • New/changed network calls? Yes — two new embedded LLM calls per /new//reset event (context-digest and session-importance). Both use the existing runEmbeddedPiAgent pattern with the user's already-configured model/provider. Both have 20s timeouts and full no-LLM fallback paths, so they degrade gracefully if the model is unavailable.
  • Command/tool execution surface changed? No
  • Data access scope changed? Yes — hooks read session transcripts from ~/.openclaw/agents/main/sessions/*.jsonl (already accessible to the gateway process). They write to memory/context-digest.md and memory/important/*.md (within the existing workspace memory directory). No new file system paths outside existing scope.
  • If any Yes, explain risk + mitigation:
    • LLM cost: Two additional LLM calls per session boundary. Mitigated by: (1) keyword pre-filter eliminates calls for routine sessions; (2) llmDigest: false / llmClassify: false config disables LLM entirely; (3) 20s hard timeout; (4) all calls are fire-and-forget (do not block reply).
    • File writes: New files written to workspace memory dir. Mitigated by writeFileWithinRoot (atomic rename, alias-safe) and a Promise-based Mutex preventing concurrent writes to the same digest file.

Repro + Verification

Environment

  • OS: macOS 25.3.0 (darwin)
  • Runtime/container: Node 22 / Bun
  • Model/provider: (any configured model)
  • Integration/channel: Feishu / Telegram (tested)
  • Relevant config: hooks.internal.entries.context-digest.enabled: true, hooks.internal.entries.session-importance.enabled: true

Steps

  1. Start the gateway (pnpm qverisbot gateway run or via macOS app)
  2. Have a conversation with the bot containing technical content (code blocks, project discussion, or say "记住这个:...")
  3. Send /new to the bot

Expected

  • ~/.openclaw/workspace/memory/context-digest.md is created/updated with a structured topic digest
  • ~/.openclaw/workspace/memory/important/ contains a new .md file for important sessions (or nothing for routine ones)
  • Gateway log shows [hooks/context-digest] Context digest updated: ...
  • Evidence

  • 44 unit tests passing across 4 test files:
  • src/hooks/llm-memory-helpers.test.ts (evaluateSessionImportance — all 8 probes)
  • src/hooks/bundled/context-digest/handler.test.ts
  • src/hooks/bundled/session-importance/handler.test.ts
  • src/auto-reply/reply/context-digest-anchor.test.ts
  • pnpm build passes with 0 errors
  • pnpm check (lint) passes with 0 errors after fixing 3 unused-variable lint issues in test files
  • Live gateway log evidence above confirms context-digest running on real sessions

Human Verification (required)

  • Verified scenarios:
  • /new triggers context-digest update (confirmed via gateway.log)
  • context-digest.md file written with correct freshness header and section structure
  • session-importance correctly skips routine conversations (no files written for short/trivial sessions)
  • qverisbot onboard no longer crashes after Qveris config step
  • pnpm qverisbot hooks list shows both new hooks as ✓ ready
  • Edge cases checked:
  • No sessions in window → writes minimal "No recent conversations" digest
  • Concurrent /new from two channels → Mutex debounce prevents race condition (unit tested)
  • Same sessionId triggered by both command:new and session:end → dedup filter skips second (unit tested)
  • LLM timeout/unavailable → both hooks fall back to rule-based output without crashing
  • Duplicate slug in memory/important/ → smart append with timestamp divider (unit tested)
  • What you did NOT verify:
  • Full LLM classification path (all tests run with LLM mocked via VITEST=true)
  • session:end safety net path from genuine idle timeout (only command:new tested live)
  • System Prompt Anchor injection at session start (file injection confirmed; model behavior with anchor not evaluated)

Compatibility / Migration

  • Backward compatible? Yes — both hooks are additive. No existing behavior changed.
  • Config/env changes? No — hooks are enabled by default but produce no output until sessions exist. Can be disabled via openclaw hooks disable context-digest / openclaw hooks disable session-importance.
  • Migration needed? No

Failure Recovery (if this breaks)

  • How to disable quickly:
pnpm qverisbot hooks disable context-digest
pnpm qverisbot hooks disable session-importance
Files to restore (if needed): src/auto-reply/reply/get-reply-run.ts — remove the buildContextDigestAnchorPrompt call and import.
Known bad symptoms to watch:
LLM costs unexpectedly high → check if hook pre-filter is working; set llmClassify: false / llmDigest: false
memory/context-digest.md not updating → check gateway.log for [hooks/context-digest] entries; verify workspace.dir is set or check default ~/.openclaw/workspace/memory/
Risks and Mitigations
Risk: LLM cost increase from two additional calls per session boundary.

Mitigation: Multi-dimensional pre-filter eliminates LLM calls for routine sessions (short messages, no code, no keywords). Config flags llmDigest: false / llmClassify: false for zero-cost mode. Both calls are async fire-and-forget and never block the bot reply.
Risk: Context-digest injection slightly increases system prompt length for every session.

Mitigation: Hard cap of 800 chars on injected content. Only injects when memory/context-digest.md exists and has non-empty Open Items. Returns undefined (no injection) gracefully on any I/O error.
Risk: The channel.x fix (PluginRuntime) could break other X extension functionality if the added functions have subtle behavioral differences from what the extension expects.

Mitigation: Functions added (listXAccountIds, resolveXAccount, isXAccountConfigured, resolveDefaultXAccountId, chunkTextForX, sendMessageX, probeX, removeClientManager) are the exact same functions already used in src/x/. No wrapper logic added. Existing extensions/x/src/onboarding.test.ts mock shape matches exactly.

…on digest; (2)classify conversations by importance using a two-stage pipeline
@gemini-code-assist

Copy link
Copy Markdown

Summary of Changes

Hello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request significantly enhances the bot's memory management capabilities by introducing mechanisms for maintaining long-term context and intelligently archiving important conversations. It moves beyond isolated session snapshots to provide a consolidated view of ongoing work and proactively identifies high-value interactions, ensuring critical information is retained and accessible across sessions. This improves the bot's ability to maintain continuity and focus on relevant tasks without explicit prompting.

Highlights

  • Cross-Session Context Digest: Introduced a new context-digest hook that automatically maintains a rolling 7-day cross-session digest in memory/context-digest.md. This digest is topic-organized, including sections for discussed topics, key decisions, open items, and important context, providing the bot with continuous awareness.
  • Proactive Session Importance Classification: Added a session-importance hook that classifies conversations by importance using a two-stage pipeline (keyword pre-filter + LLM classification). Important sessions are persisted to memory/important/ with slug-based deduplication and smart appending, ensuring valuable information is durably archived.
  • System Prompt Anchor for Open Items: Implemented a System Prompt Anchor that automatically injects the 'Open Items / Action Items' section from context-digest.md (up to 800 characters) into every session's system prompt, giving the model subconscious awareness of pending tasks.
  • Bug Fix: PluginRuntime Channel 'x' Binding: Resolved a TypeError crash in qverisbot onboard by adding the missing x property and its associated functions to the PluginRuntimeChannel type and createRuntimeChannel() function, ensuring proper integration with X extensions.

🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console.

Changelog
  • src/auto-reply/reply/context-digest-anchor.test.ts
    • Added unit tests for extractOpenItemsSection to verify correct extraction of content from context digest Markdown.
    • Added unit tests for buildContextDigestAnchorPrompt to ensure it correctly generates prompts, handles missing files, empty sections, and truncates content.
  • src/auto-reply/reply/context-digest-anchor.ts
    • Added a new module to extract 'Open Items / Action Items' from the context digest and format it as a system prompt fragment.
    • Implemented logic to handle placeholder texts and truncate content to a maximum character limit.
  • src/auto-reply/reply/get-reply-run.ts
    • Imported buildContextDigestAnchorPrompt to enable its functionality.
    • Integrated the context digest anchor prompt into the extraSystemPromptParts array, ensuring it is included in the system prompt for new sessions.
  • src/hooks/bundled/context-digest/HOOK.md
    • Added documentation for the new context-digest hook, detailing its purpose, functionality, output format, and configuration options.
    • Included metadata for OpenClaw, specifying events, requirements, and installation details.
  • src/hooks/bundled/context-digest/handler.test.ts
    • Added comprehensive unit tests for the context-digest hook handler, covering scenarios like empty stores, fallback digests, session filtering, output capping, and event deduplication.
  • src/hooks/bundled/context-digest/handler.ts
    • Implemented the context-digest hook handler, responsible for collecting recent session transcripts, generating a structured digest (using LLM or fallback), and writing it to memory/context-digest.md.
    • Incorporated mutex-based debouncing and session deduplication to prevent redundant processing.
  • src/hooks/bundled/session-importance/HOOK.md
    • Added documentation for the new session-importance hook, outlining its two-stage classification pipeline, categories, output format, and configuration.
    • Provided details on how the hook identifies and persists important conversations to durable memory.
  • src/hooks/bundled/session-importance/handler.test.ts
    • Added unit tests for the session-importance hook handler, verifying its ability to classify sessions based on keywords, code density, collaboration intensity, and handle deduplication.
  • src/hooks/bundled/session-importance/handler.ts
    • Implemented the session-importance hook handler, which evaluates session importance using a multi-dimensional scoring engine and an LLM (or fallback) to classify and persist important conversations.
    • Included logic for slug-based deduplication and smart appending to existing memory files.
  • src/hooks/llm-memory-helpers.test.ts
    • Added unit tests for evaluateSessionImportance to validate its multi-dimensional scoring logic, including explicit intent, code block density, user message depth, collaboration intensity, and keyword scoring.
  • src/hooks/llm-memory-helpers.ts
    • Added shared utilities for LLM orchestration in memory management hooks, including mutex/debounce for digest generation, session deduplication, and token boundary guards.
    • Implemented generateDigestViaLLM and generateDigestFallback for context digest creation.
    • Introduced evaluateSessionImportance for multi-dimensional scoring and classifyImportanceViaLLM and classifyImportanceFallback for session importance classification.
  • src/hooks/transcript-reader.ts
    • Added shared utilities for parsing JSONL session transcripts, including filtering system messages, commands, and inter-session provenance.
    • Implemented collectRecentSessionTranscripts to gather transcripts within a specified time window, respecting message and character limits.
  • src/plugins/runtime/runtime-channel.ts
    • Imported various x related functions and constants from ../../x/ modules.
    • Extended the createRuntimeChannel function to include an x property with methods for X account management, message chunking, sending, probing, and client manager removal.
  • src/plugins/runtime/types-channel.ts
    • Updated the PluginRuntimeChannel type definition to include a new x property.
    • Defined the types for x related functions and constants, such as listXAccountIds, resolveXAccount, sendMessageX, and probeX.
Activity
  • 44 unit tests passed across 4 test files: src/hooks/llm-memory-helpers.test.ts, src/hooks/bundled/context-digest/handler.test.ts, src/hooks/bundled/session-importance/handler.test.ts, and src/auto-reply/reply/context-digest-anchor.test.ts.
  • pnpm build passed with 0 errors.
  • pnpm check (lint) passed with 0 errors after fixing 3 unused-variable lint issues in test files.
  • Live gateway logs confirmed context-digest running on real sessions.
  • Verified that /new triggers context-digest updates and context-digest.md is written with correct structure.
  • Confirmed session-importance correctly skips routine conversations.
  • Verified that qverisbot onboard no longer crashes after the Qveris configuration step.
  • Confirmed pnpm qverisbot hooks list shows both new hooks as ✓ ready.
  • Edge cases checked include: no sessions in window, concurrent /new from two channels (mutex debounce), duplicate sessionId triggering (dedup filter), LLM timeout/unavailable (graceful fallback), and duplicate slugs in memory/important/ (smart append).
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces two significant new features for memory management: a context-digest hook to create a rolling cross-session summary, and a session-importance hook to classify and archive important conversations. It also fixes a bug that caused a crash during Qveris onboarding due to a missing channel.x binding in the PluginRuntime. However, these new memory management features introduce security risks related to cross-session prompt injection and potential denial of service. Specifically, the automatic injection of summarized conversation history into the system prompt can be exploited to persist malicious instructions across sessions, and reading entire session transcript files into memory without pre-checking their size could lead to memory exhaustion and process crashes. My review also focuses on improving the robustness of configuration handling in the new hook handlers.

Comment thread src/auto-reply/reply/get-reply-run.ts
Comment thread src/hooks/transcript-reader.ts Outdated
Comment thread src/hooks/bundled/context-digest/handler.ts Outdated
Comment thread src/hooks/bundled/context-digest/handler.ts Outdated
@linfangw
linfangw requested review from ax2, chris7iu and vxwork March 7, 2026 05:55
@linfangw
linfangw merged commit 65722fe into main Mar 7, 2026
2 of 9 checks passed
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