Skip to content

Self-evolving Context Database for AI Agents. Unify Agent Memory, Knowledge RAG and Skills.

fix(cc-memory-plugin): default-capture assistant turns + inline tool I/O (#1849)

t0saki5d83e668558c
diff --git a/examples/claude-code-memory-plugin/README.md b/examples/claude-code-memory-plugin/README.md
index 38260e9a6..5b9f35c78 100644
--- a/examples/claude-code-memory-plugin/README.md
+++ b/examples/claude-code-memory-plugin/README.md
@@ -160,7 +160,7 @@ All plugin behavior can be set via env vars. Connection / identity vars affect b
| `OPENVIKING_AUTO_CAPTURE` | `true` | Enable auto-capture; also gates write hooks (PreCompact / SessionEnd / SubagentStop) |
| `OPENVIKING_CAPTURE_MODE` | `semantic` | `semantic` (always capture) or `keyword` (trigger-based) |
| `OPENVIKING_CAPTURE_MAX_LENGTH` | `24000` | Max sanitized text length for the capture decision |
-| `OPENVIKING_CAPTURE_ASSISTANT_TURNS` | `false` | Include assistant turns; default is user-only |
+| `OPENVIKING_CAPTURE_ASSISTANT_TURNS` | `true` | Include assistant turns (text + tool I/O). Set to `0` for user-only. |
| `OPENVIKING_COMMIT_TOKEN_THRESHOLD` | `20000` | Pending-token threshold for client-driven commit |
| `OPENVIKING_RESUME_CONTEXT_BUDGET` | `32000` | Token budget when fetching archive overview on session resume |
diff --git a/examples/claude-code-memory-plugin/README_CN.md b/examples/claude-code-memory-plugin/README_CN.md
index a86565260..2864e2e6e 100644
--- a/examples/claude-code-memory-plugin/README_CN.md
+++ b/examples/claude-code-memory-plugin/README_CN.md
@@ -160,7 +160,7 @@ claude() {
| `OPENVIKING_AUTO_CAPTURE` | `true` | 启用自动捕获;同时 gate 写 hook(PreCompact / SessionEnd / SubagentStop) |
| `OPENVIKING_CAPTURE_MODE` | `semantic` | `semantic`(总是捕获)或 `keyword`(基于触发词) |
| `OPENVIKING_CAPTURE_MAX_LENGTH` | `24000` | 捕获判定时 sanitized 文本的长度上限 |
-| `OPENVIKING_CAPTURE_ASSISTANT_TURNS` | `false` | 是否捕获 assistant turn;默认仅用户回合 |
+| `OPENVIKING_CAPTURE_ASSISTANT_TURNS` | `true` | 捕获 assistant 回合(文本 + tool 输入/输出)。设为 `0` 可退回仅用户 |
| `OPENVIKING_COMMIT_TOKEN_THRESHOLD` | `20000` | client-driven commit 的 pending-token 阈值 |
| `OPENVIKING_RESUME_CONTEXT_BUDGET` | `32000` | resume 时拉取 archive overview 的 token 预算 |
diff --git a/examples/claude-code-memory-plugin/scripts/auto-capture.mjs b/examples/claude-code-memory-plugin/scripts/auto-capture.mjs
index 8c2aedbd1..408f3e2c9 100644
--- a/examples/claude-code-memory-plugin/scripts/auto-capture.mjs
+++ b/examples/claude-code-memory-plugin/scripts/auto-capture.mjs
@@ -175,10 +175,44 @@ function parseTranscript(content) {
return messages;
}
+// Per-block cap for tool input / tool result snippets. Sized to keep most invocations
+// (URLs, file paths, search queries, short results) verbatim while bounding worst-case
+// blowup when an agent reads a large file or fetches a long page.
+const TOOL_BLOCK_MAX_CHARS = 4096;
+
+function truncateForLog(value) {
+ let s;
+ if (typeof value === "string") {
+ s = value;
+ } else {
+ try {
+ s = JSON.stringify(value, null, 2);
+ } catch {
+ s = String(value);
+ }
+ }
+ if (typeof s !== "string") s = "";
+ if (s.length <= TOOL_BLOCK_MAX_CHARS) return s;
+ return (
+ s.slice(0, TOOL_BLOCK_MAX_CHARS) +
+ `\n... [truncated, ${s.length - TOOL_BLOCK_MAX_CHARS} more chars]`
+ );
+}
+
+function extractToolResultText(content) {
+ if (typeof content === "string") return content;
+ if (!Array.isArray(content)) return "";
+ return content
+ .filter((b) => b && b.type === "text" && typeof b.text === "string")
+ .map((b) => b.text)
+ .join("\n");
+}
+
/**
- * Extract user/assistant turns. Tier-1 parts: plain text + tool-use name list.
- * Tool-use blocks are aggregated into a tool-name summary per assistant message
- * so the OV memory extractor sees "what happened" without raw tool_result JSON.
+ * Extract user/assistant turns. Captures plain text, tool_use input (server-side args),
+ * and tool_result content (tool output). Tool blocks are inlined into the per-turn text
+ * so the OV memory extractor sees "what happened" with substance, not just tool names.
+ * Each tool block is truncated to TOOL_BLOCK_MAX_CHARS to bound size.
*/
function extractAllTurns(messages) {
const turns = [];
@@ -193,16 +227,22 @@ function extractAllTurns(messages) {
if (typeof content === "string") {
text = content;
} else if (Array.isArray(content)) {
- const texts = [];
+ const parts = [];
for (const block of content) {
if (!block || typeof block !== "object") continue;
if (block.type === "text" && typeof block.text === "string") {
- texts.push(block.text);
+ parts.push(block.text);
} else if (block.type === "tool_use" && typeof block.name === "string") {
toolNames.push(block.name);
+ parts.push(`[tool: ${block.name}]\n${truncateForLog(block.input)}`);
+ } else if (block.type === "tool_result") {
+ const resultText = extractToolResultText(block.content);
+ if (resultText) {
+ parts.push(`[tool result]\n${truncateForLog(resultText)}`);
+ }
}
}
- text = texts.join("\n");
+ text = parts.join("\n\n");
}
};
@@ -242,13 +282,9 @@ async function pushTurnsToOv(ovSessionId, turns) {
let ok = 0;
let failed = 0;
for (const turn of turns) {
- let content = stripInjectedBlocks(turn.text).trim();
- if (turn.role === "assistant" && turn.toolNames.length > 0) {
- const uniq = Array.from(new Set(turn.toolNames)).join(", ");
- content = content
- ? `${content}\n\n[tools used: ${uniq}]`
- : `[tools used: ${uniq}]`;
- }
+ // Tool input + tool_result are already inlined as `[tool: NAME]` / `[tool result]`
+ // blocks during harvesting, so no separate suffix is needed here.
+ const content = stripInjectedBlocks(turn.text).trim();
if (!content) continue;
const res = await addMessage(fetchJSON, ovSessionId, { role: turn.role, content });
diff --git a/examples/claude-code-memory-plugin/scripts/config.mjs b/examples/claude-code-memory-plugin/scripts/config.mjs
index 9e52bae61..f21ac038c 100644
--- a/examples/claude-code-memory-plugin/scripts/config.mjs
+++ b/examples/claude-code-memory-plugin/scripts/config.mjs
@@ -242,7 +242,12 @@ export function loadConfig() {
num(cc.captureMaxLength, 24000),
))),
captureTimeoutMs,
- captureAssistantTurns: envBool("OPENVIKING_CAPTURE_ASSISTANT_TURNS") ?? (cc.captureAssistantTurns === true),
+ // Default true: a "memory plugin" without assistant-side capture only sees half the
+ // conversation, which makes extraction noticeably worse. Subagent capture has always
+ // pushed both sides (subagent-stop.mjs); this aligns the main-session path with that
+ // behavior. Operators who want the old user-only behavior can still set
+ // OPENVIKING_CAPTURE_ASSISTANT_TURNS=0 or claude_code.captureAssistantTurns=false.
+ captureAssistantTurns: envBool("OPENVIKING_CAPTURE_ASSISTANT_TURNS") ?? (cc.captureAssistantTurns !== false),
// P0-2: client-driven commit threshold (ported from openclaw afterTurn).
// Default 20000 aligns with openclaw; lower values produce archives faster.
commitTokenThreshold: Math.max(1000, Math.floor(num(
diff --git a/examples/claude-code-memory-plugin/scripts/subagent-stop.mjs b/examples/claude-code-memory-plugin/scripts/subagent-stop.mjs
index 527b79519..a2b239122 100644
--- a/examples/claude-code-memory-plugin/scripts/subagent-stop.mjs
+++ b/examples/claude-code-memory-plugin/scripts/subagent-stop.mjs
@@ -68,9 +68,43 @@ function parseTranscript(content) {
return out;
}
+// Per-block cap for tool input / tool result snippets. Sized to keep most invocations
+// verbatim while bounding worst-case blowup. Mirrors auto-capture.mjs.
+const TOOL_BLOCK_MAX_CHARS = 4096;
+
+function truncateForLog(value) {
+ let s;
+ if (typeof value === "string") {
+ s = value;
+ } else {
+ try {
+ s = JSON.stringify(value, null, 2);
+ } catch {
+ s = String(value);
+ }
+ }
+ if (typeof s !== "string") s = "";
+ if (s.length <= TOOL_BLOCK_MAX_CHARS) return s;
+ return (
+ s.slice(0, TOOL_BLOCK_MAX_CHARS) +
+ `\n... [truncated, ${s.length - TOOL_BLOCK_MAX_CHARS} more chars]`
+ );
+}
+
+function extractToolResultText(content) {
+ if (typeof content === "string") return content;
+ if (!Array.isArray(content)) return "";
+ return content
+ .filter((b) => b && b.type === "text" && typeof b.text === "string")
+ .map((b) => b.text)
+ .join("\n");
+}
+
/**
* Tier-1 parts extraction — shared shape with auto-capture.mjs.
* Kept inline here so SubagentStop does not import auto-capture's globals.
+ * Inlines tool_use input + tool_result content (truncated) so the memory extractor
+ * sees what the subagent actually fetched/read/searched, not just tool names.
*/
function extractTurns(messages) {
const turns = [];
@@ -84,16 +118,22 @@ function extractTurns(messages) {
if (typeof content === "string") {
text = content;
} else if (Array.isArray(content)) {
- const texts = [];
+ const parts = [];
for (const block of content) {
if (!block || typeof block !== "object") continue;
if (block.type === "text" && typeof block.text === "string") {
- texts.push(block.text);
+ parts.push(block.text);
} else if (block.type === "tool_use" && typeof block.name === "string") {
toolNames.push(block.name);
+ parts.push(`[tool: ${block.name}]\n${truncateForLog(block.input)}`);
+ } else if (block.type === "tool_result") {
+ const resultText = extractToolResultText(block.content);
+ if (resultText) {
+ parts.push(`[tool result]\n${truncateForLog(resultText)}`);
+ }
}
}
- text = texts.join("\n");
+ text = parts.join("\n\n");
}
};
@@ -119,11 +159,8 @@ async function pushTurns(ovSessionId, ovAgentId, turns) {
let ok = 0;
let failed = 0;
for (const turn of turns) {
- let content = turn.text;
- if (turn.role === "assistant" && turn.toolNames.length > 0) {
- const uniq = Array.from(new Set(turn.toolNames)).join(", ");
- content = content ? `${content}\n\n[tools used: ${uniq}]` : `[tools used: ${uniq}]`;
- }
+ // Tool input / result already inlined as `[tool: NAME]` / `[tool result]` during harvest.
+ const content = turn.text;
if (!content) continue;
const res = await addMessage(fetchJSON, ovSessionId, { role: turn.role, content });
if (res.ok) ok++;