feat(openclaw-plugin): add SCCS tool-output compression integration#1547
feat(openclaw-plugin): add SCCS tool-output compression integration#1547HaotianChen616 wants to merge 1 commit intovolcengine:mainfrom
Conversation
Add Shared Context Caching System (SCCS) to the OpenViking context engine plugin. SCCS compresses large tool outputs into compact summaries with REF_ID placeholders, allowing agents to fetch original data on demand via the fetch_original_data tool. Key components: - compressor: detects and compresses oversized tool outputs - summarizer: smart content-type-aware summary extraction (JSON, markdown, tables, logs, code, plain text) - storage: disk-backed ref store with TTL expiration - integration: wraps context engine assemble() with compression layer - ref-tool: fetch_original_data tool for retrieving original outputs Configuration: - sccsEnabled (default: false), sccsCompressThreshold (default: 3000) - sccsSummaryMaxChars (default: 300), sccsEnableSmartSummary (default: true) - sccsStorageTtlSeconds (default: 86400), sccsStorageDir, sccsMaxEntries Also includes: - Whitelist for OpenClaw config files (SOUL.md, MEMORY.md, etc.) to prevent compression of critical agent context
PR Reviewer Guide 🔍Here are some key observations to aid the review process:
|
PR Code Suggestions ✨No code suggestions found for the PR. |
|
I found two functional issues in the SCCS integration.
I reproduced this locally with a temporary store directory and a sibling Impact: the new tool can read (and, on expired entries, delete) JSON files outside the intended SCCS storage area.
I reproduced this locally by delaying Impact: SCCS can hand the model a REF_ID that is not actually recoverable yet. I did not find any SCCS-specific tests in this PR, so both issues appear to be uncovered right now. I’d recommend:
|
Description
为 OpenViking OpenClaw 插件新增 SCCS(Shared Context Caching System),在 context engine
assemble阶段自动压缩过长的 tool output,替换为紧凑摘要 + REF_ID 占位符,Agent 可通过fetch_original_data工具按需取回原始数据。Related Issue
Resolves #1548
Type of Change
Changes Made
新增 SCCS 模块(
examples/openclaw-plugin/sccs/)MemoryStore(内存 FIFO 淘汰)+DiskBackedStore(磁盘持久化 + 内存缓存,支持 TTL 过期)fetch_original_data工具定义,Agent 通过 REF_ID 取回原始数据createSccsIntegration()返回wrapContextEngine装饰器和 tool 定义集成到 OpenViking 插件
createSccsIntegration,通过sccs.wrapContextEngine(baseEngine)接入 context engine;注册fetch_original_data工具设计概要
核心思路
Agent 与工具交互时,tool output 往往远超 Agent 实际需要的信息量(如完整文件内容、大量日志、长 JSON 数组等),这些冗余内容占用大量上下文窗口 token。SCCS 的核心思路是:通过引用替换,将大工具输出卸载到上下文窗口之外,同时保留完整按需取回的能力 — 将长输出替换为 REF_ID 引用 + 紧凑摘要,原始数据持久化存储,Agent 需要细节时可随时通过
fetch_original_data取回。架构:Decorator 模式接入 Context Engine
SCCS 通过 Decorator 模式包装 OpenViking context engine,仅拦截
assemble()方法:graph TB subgraph OpenClaw Framework A[Agent Loop] B[Tool Execution] end subgraph OpenViking Plugin C[Context Engine] D[SCCS Layer] E[fetch_original_data Tool] end A -->|assemble| C C -->|wrap| D D -->|compressed messages| A A -->|tool call| B B -->|tool output| C A -->|need detail| E E -->|REF_ID lookup| D style D fill:#e1f5fe,stroke:#0288d1 style E fill:#e1f5fe,stroke:#0288d1其他方法(
ingest、compact、afterTurn)均透传,零侵入。sccsEnabled=false时wrapContextEngine为恒等函数,完全无开销。压缩流程
assemble()返回原始 messages 后,SCCS 逐条扫描SummaryExtractor生成智能摘要DiskBackedStore[REF_ID: xxx] (Summary: ...)占位符REF_ID_INSTRUCTION到systemPromptAddition,指导 Agent 何时应 fetch 原始数据flowchart LR A[assemble 返回<br>原始 messages] --> B{遍历每条 message} B --> C{是 tool role?} C -->|No| B C -->|Yes| D{长度 > 阈值?<br>非配置文件?<br>无 REF_ID?} D -->|No| B D -->|Yes| E[智能摘要] E --> F[原文存入 RefStore] F --> G[替换为<br>REF_ID + Summary] G --> B B -->|遍历结束| H{有压缩?} H -->|Yes| I[注入 REF_ID_INSTRUCTION<br>到 systemPromptAddition] H -->|No| J[原样返回]智能摘要(6 种内容类型)
SummaryExtractor自动检测内容类型并针对性摘要:JSON.parse成功所有摘要最终截断到
summaryMaxChars(默认 300 字符)。引用存储:内存 + 磁盘双层
maxEntries限制(默认 10000)set()先写内存再异步写磁盘get()时检查expiresAt,过期则返回 null 并删除磁盘文件防护机制
hasRefId()检测已压缩消息,防止重复压缩Math.max下限保护,防止不合理配置配置项
sccsEnabledfalsesccsCompressThreshold30002000sccsSummaryMaxChars30050sccsEnableSmartSummarytruesccsStorageTtlSeconds86400600sccsStorageDir~/.openclaw/sccssccsMaxEntries100001000Testing
Token 节省效果(GLM-4.7 模型)
使用不同场景测试 SCCS 对 assemble 返回 token 数的影响:
结论:SCCS 实现显著的 token 节省,平均节省 42.06%。对于 tool output 较多的大型任务(test1/test2/test4),节省比例可达 50%+;对于触发了解引用
fetch_original_data的任务(test5),仍有 16% 的优化。Checklist
Additional Notes
默认关闭
SCCS 默认关闭(
sccsEnabled: false),不影响现有用户。需在插件配置中显式开启。配置示例
{ "sccsEnabled": true, "sccsCompressThreshold": 3000, "sccsSummaryMaxChars": 300, "sccsEnableSmartSummary": true, "sccsStorageTtlSeconds": 86400, "sccsStorageDir": "~/.openclaw/sccs", "sccsMaxEntries": 10000 }