|
| 1 | +# Hermes Context Engine Integration Plan |
| 2 | + |
| 3 | +This document outlines a staged plan for adding TencentDB Agent Memory short-term |
| 4 | +context offload to Hermes as a `ContextEngine` plugin. |
| 5 | + |
| 6 | +It is a design document, not a completed adapter. The goal is to make the |
| 7 | +integration boundary explicit before wiring runtime behavior into Hermes's |
| 8 | +compression lifecycle. |
| 9 | + |
| 10 | +## Background |
| 11 | + |
| 12 | +TencentDB Agent Memory currently has two integration surfaces: |
| 13 | + |
| 14 | +- **OpenClaw**: long-term memory plus short-term context offload through the |
| 15 | + `contextEngine` slot. |
| 16 | +- **Hermes**: long-term memory through `memory.provider: memory_tencentdb`. |
| 17 | + |
| 18 | +Hermes also supports pluggable context engines selected by: |
| 19 | + |
| 20 | +```yaml |
| 21 | +context: |
| 22 | + engine: "memory_tencentdb" |
| 23 | +``` |
| 24 | +
|
| 25 | +The official Hermes `ContextEngine` interface requires an engine to: |
| 26 | + |
| 27 | +- track token usage via `update_from_response(usage)`, |
| 28 | +- decide when compaction should run via `should_compress(prompt_tokens)`, |
| 29 | +- return a valid OpenAI-style message list from `compress(messages, ...)`, |
| 30 | +- optionally expose tools through `get_tool_schemas()` and `handle_tool_call()`, |
| 31 | +- handle session lifecycle hooks such as `on_session_start()` and |
| 32 | + `on_session_end()`. |
| 33 | + |
| 34 | +## Target Behavior |
| 35 | + |
| 36 | +The Hermes context engine should bring the OpenClaw short-term memory benefits |
| 37 | +to Hermes users: |
| 38 | + |
| 39 | +1. Persist large tool outputs outside the active prompt. |
| 40 | +2. Convert tool-heavy task history into compact Mermaid task canvases. |
| 41 | +3. Preserve drill-down recovery through `node_id` and raw reference files. |
| 42 | +4. Keep returned messages valid for Hermes's model provider path. |
| 43 | +5. Avoid replacing the existing long-term memory provider; users may enable the |
| 44 | + memory provider, context engine, or both. |
| 45 | + |
| 46 | +## Proposed Architecture |
| 47 | + |
| 48 | +```text |
| 49 | +Hermes Agent |
| 50 | + ├─ memory.provider: memory_tencentdb |
| 51 | + │ └─ existing Python MemoryProvider -> Node Gateway /recall + /capture |
| 52 | + └─ context.engine: memory_tencentdb |
| 53 | + └─ new Python ContextEngine adapter |
| 54 | + ├─ session lifecycle and token threshold tracking |
| 55 | + ├─ local/offload sidecar client |
| 56 | + ├─ Mermaid injection into compressed messages |
| 57 | + └─ optional lookup tools for node_id/raw refs |
| 58 | +``` |
| 59 | + |
| 60 | +The first implementation should avoid duplicating the whole TypeScript offload |
| 61 | +pipeline in Python. Prefer one of these reuse paths: |
| 62 | + |
| 63 | +- expose a narrow Gateway endpoint for offload operations, then have the Hermes |
| 64 | + engine call the Gateway; |
| 65 | +- or run a small Node helper next to the existing Gateway and keep Python as the |
| 66 | + lifecycle adapter. |
| 67 | + |
| 68 | +## Lifecycle Mapping |
| 69 | + |
| 70 | +| Hermes `ContextEngine` method | TencentDB responsibility | |
| 71 | +| :--- | :--- | |
| 72 | +| `on_session_start(session_id, **kwargs)` | Resolve session key, data directory, Gateway/helper availability, and load existing offload state. | |
| 73 | +| `update_from_response(usage)` | Update prompt/completion/total token counters and maintain `threshold_tokens`. | |
| 74 | +| `should_compress(prompt_tokens)` | Trigger when prompt usage crosses configured mild or aggressive offload ratios. | |
| 75 | +| `compress(messages, current_tokens, focus_topic)` | Persist eligible tool outputs, run or request L1/L1.5/L2 offload processing, inject active Mermaid context, and return valid messages. | |
| 76 | +| `get_tool_schemas()` | Expose optional lookup tools for `node_id`, Mermaid files, and raw refs. | |
| 77 | +| `handle_tool_call(name, args, **kwargs)` | Dispatch lookup calls to the offload store/helper. | |
| 78 | +| `on_session_end(session_id, messages)` | Flush pending offload work and persist final session state. | |
| 79 | +| `on_session_reset()` | Reset per-session state without deleting persisted offload artifacts. | |
| 80 | + |
| 81 | +## Phased Implementation |
| 82 | + |
| 83 | +### Phase 1: No-op Engine Skeleton |
| 84 | + |
| 85 | +- Add `hermes-plugin/context_engine/memory_tencentdb/`. |
| 86 | +- Implement a minimal `ContextEngine` subclass that: |
| 87 | + - satisfies the Hermes ABC, |
| 88 | + - tracks token usage, |
| 89 | + - reports status, |
| 90 | + - returns messages unchanged from `compress()`. |
| 91 | +- Add Python tests that instantiate the engine and verify the ABC contract. |
| 92 | + |
| 93 | +This phase proves discovery, configuration, and lifecycle compatibility without |
| 94 | +risking message corruption. |
| 95 | + |
| 96 | +### Phase 2: Read-only Mermaid Injection |
| 97 | + |
| 98 | +- Reuse existing offload artifacts if present under the configured data |
| 99 | + directory. |
| 100 | +- Inject the active Mermaid task canvas into the returned messages within a |
| 101 | + configurable token/character budget. |
| 102 | +- Add tests for message validity, budget enforcement, and no-op behavior when |
| 103 | + no MMD exists. |
| 104 | + |
| 105 | +This gives Hermes users read-only task canvas context before enabling mutation. |
| 106 | + |
| 107 | +### Phase 3: Offload Write Path |
| 108 | + |
| 109 | +- Persist large tool outputs from Hermes messages into refs/JSONL. |
| 110 | +- Generate or request L1/L1.5/L2 summaries through the shared Gateway/helper. |
| 111 | +- Replace eligible historical tool results with compact summaries. |
| 112 | +- Preserve raw refs and `node_id` drill-down. |
| 113 | + |
| 114 | +### Phase 4: Tools And Recovery |
| 115 | + |
| 116 | +- Expose `tdai_offload_lookup` or equivalent through `get_tool_schemas()`. |
| 117 | +- Support exact raw ref lookup only through explicit tool calls, not automatic |
| 118 | + prompt injection. |
| 119 | +- Add tests for unknown tool names, missing refs, and successful node recovery. |
| 120 | + |
| 121 | +## Configuration |
| 122 | + |
| 123 | +Recommended initial config shape: |
| 124 | + |
| 125 | +```yaml |
| 126 | +context: |
| 127 | + engine: memory_tencentdb |
| 128 | +
|
| 129 | +memory_tencentdb: |
| 130 | + context_engine: |
| 131 | + enabled: true |
| 132 | + data_dir: ~/.hermes/context-offload |
| 133 | + mild_offload_ratio: 0.5 |
| 134 | + aggressive_offload_ratio: 0.85 |
| 135 | + mmd_max_token_ratio: 0.2 |
| 136 | +``` |
| 137 | + |
| 138 | +Keep this separate from `memory.provider` settings so users can enable |
| 139 | +long-term memory and short-term context management independently. |
| 140 | + |
| 141 | +## Validation Plan |
| 142 | + |
| 143 | +Minimum tests before enabling write behavior: |
| 144 | + |
| 145 | +- Engine can be discovered and instantiated by Hermes. |
| 146 | +- `compress()` always returns a list of OpenAI-style message dicts. |
| 147 | +- System, first user messages, and recent tail messages are preserved. |
| 148 | +- Tool call/tool result groups are not split into invalid sequences. |
| 149 | +- Mermaid injection obeys its budget and is absent when no active MMD exists. |
| 150 | +- Session reset clears counters but not persisted artifacts. |
| 151 | +- Unknown tools return structured JSON errors. |
| 152 | + |
| 153 | +Runtime validation: |
| 154 | + |
| 155 | +- Run a long tool-heavy Hermes session and compare prompt tokens before/after. |
| 156 | +- Confirm raw tool output is recoverable by `node_id`. |
| 157 | +- Confirm the existing `memory_tencentdb` MemoryProvider still works when the |
| 158 | + context engine is disabled. |
| 159 | +- Confirm enabling both memory provider and context engine does not duplicate |
| 160 | + recall/context injection. |
| 161 | + |
| 162 | +## Out Of Scope For The First Runtime PR |
| 163 | + |
| 164 | +- Rewriting the TypeScript offload pipeline in Python. |
| 165 | +- Changing the existing Hermes MemoryProvider behavior. |
| 166 | +- Auto-enabling the context engine when the memory provider is selected. |
| 167 | +- Claiming WideSearch or SWE-bench improvements without a reproducible Hermes |
| 168 | + evaluation run. |
0 commit comments