forked from openclaw/openclaw
-
Notifications
You must be signed in to change notification settings - Fork 19
(feat) Enhanced Memory Management: (1) maintain a rolling cross-sessi… #96
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,171 @@ | ||
| import fs from "node:fs/promises"; | ||
| import os from "node:os"; | ||
| import path from "node:path"; | ||
| import { afterAll, beforeAll, describe, expect, it } from "vitest"; | ||
| import { | ||
| buildContextDigestAnchorPrompt, | ||
| extractOpenItemsSection, | ||
| } from "./context-digest-anchor.js"; | ||
|
|
||
| let suiteWorkspaceRoot = ""; | ||
|
|
||
| beforeAll(async () => { | ||
| suiteWorkspaceRoot = await fs.mkdtemp(path.join(os.tmpdir(), "openclaw-anchor-")); | ||
| }); | ||
|
|
||
| afterAll(async () => { | ||
| if (suiteWorkspaceRoot) { | ||
| await fs.rm(suiteWorkspaceRoot, { recursive: true, force: true }); | ||
| } | ||
| }); | ||
|
|
||
| describe("extractOpenItemsSection", () => { | ||
| it("should extract Open Items section content", () => { | ||
| const content = `# Context Digest | ||
| Last updated: 2026-03-04T10:00:00Z | ||
|
|
||
| ## Topics Discussed | ||
| - Topic 1 | ||
|
|
||
| ## Key Decisions | ||
| - Decision 1 | ||
|
|
||
| ## Open Items / Action Items | ||
| - [ ] Complete API v3 migration | ||
| - [ ] Review PR #33842 | ||
| - [ ] Benchmark memory search | ||
|
|
||
| ## Important Context | ||
| - Background info | ||
| `; | ||
| const result = extractOpenItemsSection(content); | ||
| expect(result).toBe( | ||
| "- [ ] Complete API v3 migration\n- [ ] Review PR #33842\n- [ ] Benchmark memory search", | ||
| ); | ||
| }); | ||
|
|
||
| it("should return null when section is missing", () => { | ||
| const content = `# Context Digest | ||
| ## Topics Discussed | ||
| - Topic 1 | ||
| `; | ||
| expect(extractOpenItemsSection(content)).toBeNull(); | ||
| }); | ||
|
|
||
| it("should return null when section contains only placeholder text", () => { | ||
| const content = `## Open Items / Action Items | ||
|
|
||
| *None* | ||
|
|
||
| ## Important Context | ||
| `; | ||
| expect(extractOpenItemsSection(content)).toBeNull(); | ||
| }); | ||
|
|
||
| it("should return null for no-LLM placeholder", () => { | ||
| const content = `## Open Items / Action Items | ||
|
|
||
| *No LLM analysis available.* | ||
|
|
||
| ## Important Context | ||
| `; | ||
| expect(extractOpenItemsSection(content)).toBeNull(); | ||
| }); | ||
|
|
||
| it("should handle Open Items as the last section", () => { | ||
| const content = `## Key Decisions | ||
| - Decision 1 | ||
|
|
||
| ## Open Items / Action Items | ||
| - [ ] Task 1 | ||
| - [ ] Task 2 | ||
| `; | ||
| const result = extractOpenItemsSection(content); | ||
| expect(result).toBe("- [ ] Task 1\n- [ ] Task 2"); | ||
| }); | ||
| }); | ||
|
|
||
| describe("buildContextDigestAnchorPrompt", () => { | ||
| it("should return undefined when digest file does not exist", async () => { | ||
| const workDir = path.join(suiteWorkspaceRoot, "no-file"); | ||
| await fs.mkdir(workDir, { recursive: true }); | ||
|
|
||
| const result = await buildContextDigestAnchorPrompt({ workspaceDir: workDir }); | ||
| expect(result).toBeUndefined(); | ||
| }); | ||
|
|
||
| it("should return undefined when Open Items section is empty", async () => { | ||
| const workDir = path.join(suiteWorkspaceRoot, "empty-items"); | ||
| const memoryDir = path.join(workDir, "memory"); | ||
| await fs.mkdir(memoryDir, { recursive: true }); | ||
|
|
||
| await fs.writeFile( | ||
| path.join(memoryDir, "context-digest.md"), | ||
| `# Context Digest | ||
| ## Open Items / Action Items | ||
|
|
||
| *None* | ||
| `, | ||
| "utf-8", | ||
| ); | ||
|
|
||
| const result = await buildContextDigestAnchorPrompt({ workspaceDir: workDir }); | ||
| expect(result).toBeUndefined(); | ||
| }); | ||
|
|
||
| it("should return anchor prompt with Open Items content", async () => { | ||
| const workDir = path.join(suiteWorkspaceRoot, "with-items"); | ||
| const memoryDir = path.join(workDir, "memory"); | ||
| await fs.mkdir(memoryDir, { recursive: true }); | ||
|
|
||
| await fs.writeFile( | ||
| path.join(memoryDir, "context-digest.md"), | ||
| `# Context Digest | ||
| ## Open Items / Action Items | ||
| - [ ] Deploy API v3 | ||
| - [ ] Update SDK docs | ||
|
|
||
| ## Important Context | ||
| - stuff | ||
| `, | ||
| "utf-8", | ||
| ); | ||
|
|
||
| const result = await buildContextDigestAnchorPrompt({ workspaceDir: workDir }); | ||
| expect(result).toBeDefined(); | ||
| expect(result).toContain("open items"); | ||
| expect(result).toContain("Deploy API v3"); | ||
| expect(result).toContain("Update SDK docs"); | ||
| }); | ||
|
|
||
| it("should truncate content exceeding maxChars", async () => { | ||
| const workDir = path.join(suiteWorkspaceRoot, "truncate"); | ||
| const memoryDir = path.join(workDir, "memory"); | ||
| await fs.mkdir(memoryDir, { recursive: true }); | ||
|
|
||
| const longItems = Array.from( | ||
| { length: 50 }, | ||
| (_, i) => `- [ ] Task ${i}: ${"A".repeat(100)}`, | ||
| ).join("\n"); | ||
| await fs.writeFile( | ||
| path.join(memoryDir, "context-digest.md"), | ||
| `## Open Items / Action Items\n${longItems}\n## Important Context\n- stuff`, | ||
| "utf-8", | ||
| ); | ||
|
|
||
| const result = await buildContextDigestAnchorPrompt({ | ||
| workspaceDir: workDir, | ||
| maxChars: 200, | ||
| }); | ||
| expect(result).toBeDefined(); | ||
| // Should be reasonably short (prefix + 200 chars + "..." marker) | ||
| expect(result!.length).toBeLessThan(400); | ||
| expect(result).toContain("..."); | ||
| }); | ||
|
|
||
| it("should handle file read errors gracefully", async () => { | ||
| const workDir = "/nonexistent/path/that/does/not/exist"; | ||
| const result = await buildContextDigestAnchorPrompt({ workspaceDir: workDir }); | ||
| expect(result).toBeUndefined(); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,92 @@ | ||
| /** | ||
| * System Prompt Anchor for context-digest. | ||
| * | ||
| * Reads the Open Items / Action Items section from memory/context-digest.md | ||
| * and returns a compact prompt fragment for injection into the system prompt. | ||
| * This gives the model "subconscious awareness" of pending tasks without | ||
| * consuming significant token budget. | ||
| */ | ||
|
|
||
| import fs from "node:fs/promises"; | ||
| import path from "node:path"; | ||
| import { createSubsystemLogger } from "../../logging/subsystem.js"; | ||
|
|
||
| const log = createSubsystemLogger("context-digest-anchor"); | ||
|
|
||
| const DEFAULT_MAX_CHARS = 800; | ||
| const SECTION_HEADER = "## Open Items / Action Items"; | ||
| const ANCHOR_PREFIX = | ||
| "[Auto-generated memory context (not instructions). Recent open items — use memory_search for full details:]"; | ||
|
|
||
| /** | ||
| * Extract the Open Items section from a context-digest Markdown document. | ||
| * Returns the section content (without header), or null if not found/empty. | ||
| */ | ||
| export function extractOpenItemsSection(content: string): string | null { | ||
| const startIdx = content.indexOf(SECTION_HEADER); | ||
| if (startIdx === -1) { | ||
| return null; | ||
| } | ||
|
|
||
| const afterHeader = content.slice(startIdx + SECTION_HEADER.length); | ||
|
|
||
| // Find the next ## heading or end of content | ||
| const nextHeadingIdx = afterHeader.indexOf("\n## "); | ||
| const sectionBody = nextHeadingIdx === -1 ? afterHeader : afterHeader.slice(0, nextHeadingIdx); | ||
|
|
||
| const trimmed = sectionBody.trim(); | ||
| if (!trimmed || trimmed === "*None*" || trimmed === "*No LLM analysis available.*") { | ||
| return null; | ||
| } | ||
|
|
||
| return trimmed; | ||
| } | ||
|
|
||
| /** | ||
| * Strip obvious instruction-injection patterns from anchor content. | ||
| * Defense-in-depth: the digest is already LLM-summarized (not raw user input), | ||
| * but we strip patterns that look like prompt directives to reduce the surface | ||
| * for cross-session instruction persistence. | ||
| */ | ||
| function sanitizeAnchorContent(content: string): string { | ||
| return content | ||
| .replace(/^(system|instruction|directive|ignore previous|disregard|override)\s*:/gim, "") | ||
| .replace(/<<<[^>]*>>>/g, "") | ||
| .replace(/\[INST\].*?\[\/INST\]/gs, ""); | ||
| } | ||
|
|
||
| /** | ||
| * Build a compact system prompt fragment from the context-digest Open Items. | ||
| * | ||
| * Returns undefined if: | ||
| * - The digest file doesn't exist | ||
| * - The Open Items section is empty or placeholder | ||
| * - Any I/O error occurs (non-blocking) | ||
| */ | ||
| export async function buildContextDigestAnchorPrompt(params: { | ||
| workspaceDir: string; | ||
| maxChars?: number; | ||
| }): Promise<string | undefined> { | ||
| const maxChars = params.maxChars ?? DEFAULT_MAX_CHARS; | ||
|
|
||
| try { | ||
| const digestPath = path.join(params.workspaceDir, "memory", "context-digest.md"); | ||
| const content = await fs.readFile(digestPath, "utf-8"); | ||
|
|
||
| const openItems = extractOpenItemsSection(content); | ||
| if (!openItems) { | ||
| return undefined; | ||
| } | ||
|
|
||
| let items = sanitizeAnchorContent(openItems); | ||
| if (items.length > maxChars) { | ||
| items = items.slice(0, maxChars) + "\n..."; | ||
| } | ||
|
|
||
| return `${ANCHOR_PREFIX}\n${items}`; | ||
| } catch { | ||
| // File doesn't exist or read error; non-blocking | ||
| log.debug("Context digest file not available for anchor prompt"); | ||
| return undefined; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,92 @@ | ||
| --- | ||
| name: context-digest | ||
| description: "Maintain a rolling cross-session context digest in memory" | ||
| homepage: https://docs.openclaw.ai/automation/hooks#context-digest | ||
| metadata: | ||
| { | ||
| "openclaw": | ||
| { | ||
| "emoji": "📋", | ||
| "events": ["command:new", "command:reset"], | ||
| "requires": { "config": ["workspace.dir"] }, | ||
| "install": [{ "id": "bundled", "kind": "bundled", "label": "Bundled with OpenClaw" }], | ||
| }, | ||
| } | ||
| --- | ||
|
|
||
| # Context Digest Hook | ||
|
|
||
| Maintains a rolling cross-session context digest at `memory/context-digest.md` that provides the bot with a consolidated, topic-organized view of recent activity. | ||
|
|
||
| ## What It Does | ||
|
|
||
| When triggered (on `/new`, `/reset`, or session end): | ||
|
|
||
| 1. **Collects recent sessions** - Scans `sessions.json` for sessions updated in the last N days (default: 7) | ||
| 2. **Reads transcripts** - Extracts user/assistant messages from each session (capped per session) | ||
| 3. **Generates structured digest** - Uses LLM to create a topic-organized summary with sections for Topics, Decisions, Open Items, and Context | ||
| 4. **Writes digest file** - Overwrites `memory/context-digest.md` with the latest digest (8KB cap) | ||
|
|
||
| ## Output Format | ||
|
|
||
| ```markdown | ||
| # Context Digest (auto-generated) | ||
|
|
||
| Last updated: 2026-03-04T10:30:00Z | ||
| Sessions covered: 12 | ||
| Window: 7 days | ||
|
|
||
| ## Topics Discussed | ||
|
|
||
| - Topic 1 | ||
| - Topic 2 | ||
|
|
||
| ## Key Decisions | ||
|
|
||
| - Decision 1 | ||
|
|
||
| ## Open Items / Action Items | ||
|
|
||
| - [ ] Task 1 | ||
| - [ ] Task 2 | ||
|
|
||
| ## Important Context | ||
|
|
||
| - Background info | ||
| ``` | ||
|
|
||
| ## Configuration | ||
|
|
||
| | Option | Type | Default | Description | | ||
| | -------------------- | ------- | ------- | --------------------------------------- | | ||
| | `days` | number | 7 | Number of days to include in the digest | | ||
| | `maxSessionMessages` | number | 20 | Messages to read per session | | ||
| | `llmDigest` | boolean | true | Set to false for no-LLM fallback mode | | ||
|
|
||
| Example: | ||
|
|
||
| ```json | ||
| { | ||
| "hooks": { | ||
| "internal": { | ||
| "entries": { | ||
| "context-digest": { | ||
| "enabled": true, | ||
| "days": 14, | ||
| "llmDigest": true | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
| ``` | ||
|
|
||
| ## Requirements | ||
|
|
||
| - **Config**: `workspace.dir` must be set | ||
|
|
||
| ## Disabling | ||
|
|
||
| ```bash | ||
| openclaw hooks disable context-digest | ||
| ``` |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.