Self-evolving Context Database for AI Agents. Unify Agent Memory, Knowledge RAG and Skills.
Python agent-contextagent-memoryagent-pluginsagentic-ragcontext-databasedsh-pluginknowledge-basememorymemory-managementself-evolvingvector-database
fix(plugins): share capture filtering across adapters (#5359)
z1gon92dcf00f837f
diff --git a/examples/agent-hook-plugin/hosts/cursor/openviking.integration.json b/examples/agent-hook-plugin/hosts/cursor/openviking.integration.jsonindex 3771a413b..5caf00999 100644--- a/examples/agent-hook-plugin/hosts/cursor/openviking.integration.json+++ b/examples/agent-hook-plugin/hosts/cursor/openviking.integration.json@@ -1,7 +1,7 @@{"schemaVersion": 1,"id": "openviking-memory",- "version": "0.5.1",+ "version": "0.5.2","clients": ["cursor"],diff --git a/examples/agent-hook-plugin/hosts/kimicode/openviking.integration.json b/examples/agent-hook-plugin/hosts/kimicode/openviking.integration.jsonindex 04ff3ac14..5c7daf397 100644--- a/examples/agent-hook-plugin/hosts/kimicode/openviking.integration.json+++ b/examples/agent-hook-plugin/hosts/kimicode/openviking.integration.json@@ -1,7 +1,7 @@{"schemaVersion": 1,"id": "openviking-memory",- "version": "0.5.1",+ "version": "0.5.2","clients": ["kimicode"],diff --git a/examples/agent-hook-plugin/hosts/trae/openviking.integration.json b/examples/agent-hook-plugin/hosts/trae/openviking.integration.jsonindex ff3c4d762..186c7e954 100644--- a/examples/agent-hook-plugin/hosts/trae/openviking.integration.json+++ b/examples/agent-hook-plugin/hosts/trae/openviking.integration.json@@ -1,7 +1,7 @@{"schemaVersion": 1,"id": "openviking-memory",- "version": "0.5.1",+ "version": "0.5.2","clients": ["trae","trae-cn"diff --git a/examples/agent-hook-plugin/hosts/zcode/openviking.integration.json b/examples/agent-hook-plugin/hosts/zcode/openviking.integration.jsonindex 8b43f5570..79e96a2ed 100644--- a/examples/agent-hook-plugin/hosts/zcode/openviking.integration.json+++ b/examples/agent-hook-plugin/hosts/zcode/openviking.integration.json@@ -1,7 +1,7 @@{"schemaVersion": 1,"id": "openviking-memory",- "version": "0.5.1",+ "version": "0.5.2","clients": ["zcode"],diff --git a/examples/agent-hook-plugin/plugin.json b/examples/agent-hook-plugin/plugin.jsonindex 885b00edf..da1c6d776 100644--- a/examples/agent-hook-plugin/plugin.json+++ b/examples/agent-hook-plugin/plugin.json@@ -1,6 +1,6 @@{"name": "agent-hook-plugin",- "version": "0.5.1",+ "version": "0.5.2","description": "Long-term semantic memory for Cursor, TRAE, TRAE CN, ZCode and Kimi Code — powered by OpenViking. Auto-recall relevant memories and capture the conversation as it happens.","author": {"name": "OpenViking",diff --git a/examples/claude-code-memory-plugin/.claude-plugin/plugin.json b/examples/claude-code-memory-plugin/.claude-plugin/plugin.jsonindex ef22bbe69..cb0c4f12e 100644--- a/examples/claude-code-memory-plugin/.claude-plugin/plugin.json+++ b/examples/claude-code-memory-plugin/.claude-plugin/plugin.json@@ -1,6 +1,6 @@{"name": "openviking-memory",- "version": "0.6.2",+ "version": "0.6.3","description": "Long-term semantic memory for Claude Code, powered by OpenViking. Auto-recall relevant memories at session start and capture important information during conversations.","author": {"name": "OpenViking",diff --git a/examples/claude-code-memory-plugin/package.json b/examples/claude-code-memory-plugin/package.jsonindex 91336b804..e05f59619 100644--- a/examples/claude-code-memory-plugin/package.json+++ b/examples/claude-code-memory-plugin/package.json@@ -1,6 +1,6 @@{"name": "claude-code-openviking-memory",- "version": "0.6.2",+ "version": "0.6.3","description": "OpenViking memory plugin for Claude Code — semantic long-term memory","type": "module","scripts": {diff --git a/examples/claude-code-memory-plugin/scripts/auto-capture.mjs b/examples/claude-code-memory-plugin/scripts/auto-capture.mjsindex 7733b1955..183cfd38b 100644--- a/examples/claude-code-memory-plugin/scripts/auto-capture.mjs+++ b/examples/claude-code-memory-plugin/scripts/auto-capture.mjs@@ -38,7 +38,7 @@ import { readJsonState, writeJsonState } from "./lib/state.mjs";import { getEffectivePeerId } from "./lib/workspace-peer.mjs";import { runHookStage } from "./shared/agent-hook-runtime.mjs";import { sendSessionMessages } from "./shared/batch-send.mjs";-import { filterCaptureParts } from "./shared/capture-utils.mjs";+import { shapeCaptureParts } from "./shared/capture-utils.mjs";if (!isPluginEnabled()) {process.stdout.write(JSON.stringify({ decision: "approve" }) + "\n");@@ -160,31 +160,13 @@ function formatTurnsAsText(turns) {// Persistent-session capture// ----------------------------------------------------------------------------// Strip plugin-injected blocks from text parts (tool parts pass through), and-// drop parts that become empty. Mirrors the old content-path stripInjectedBlocks-// + trim, but per text part so tool I/O is never collapsed. The configured-// capture filters run last, here at the send site rather than in the extractor,-// because the cursor CC advances is an index into the extracted turn list.-function sanitizePartsForSend(parts, role = "", cfg = {}) {- const out = [];- for (const p of parts || []) {- if (p.type === "text") {- const t = sanitizeCapturedText(p.text);- if (t) out.push({ type: "text", text: t });- } else {- out.push(p);- }- }- const shaped = filterCaptureParts(out, role, cfg);- return shaped.dropped ? [] : shaped.parts;-}-async function pushTurnsToOv(ovSessionId, turns, peerId = "", cfg = {}) {const payloads = [];for (const turn of turns) {// Send structured parts: tool calls/results are dedicated `tool` parts, not// inlined into content, so the server can process them separately.- const parts = sanitizePartsForSend(turn.parts, turn.role, cfg);+ // Shape at the send site: capturedTurnCount indexes the unfiltered turns.+ const parts = shapeCaptureParts(turn.parts, turn.role, cfg).parts;if (parts.length === 0) continue;const payload = { role: turn.role, parts };@@ -207,7 +189,7 @@ async function enqueueTurnsToPending(ovSessionId, turns, peerId = "", cfg = {})let queued = 0;let failed = 0;for (const turn of turns) {- const parts = sanitizePartsForSend(turn.parts, turn.role, cfg);+ const parts = shapeCaptureParts(turn.parts, turn.role, cfg).parts;if (parts.length === 0) continue;const payload = { role: turn.role, parts };diff --git a/examples/claude-code-memory-plugin/scripts/auto-capture.test.mjs b/examples/claude-code-memory-plugin/scripts/auto-capture.test.mjsindex 9838a3389..786d3a223 100644--- a/examples/claude-code-memory-plugin/scripts/auto-capture.test.mjs+++ b/examples/claude-code-memory-plugin/scripts/auto-capture.test.mjs@@ -10,13 +10,13 @@ import { readRequestBody, withMockOpenViking, writeJson } from "../../memory-pluconst SCRIPT_DIR = dirname(fileURLToPath(import.meta.url));-function runAutoCapture(input, env) {+function runHook(script, input, env) {return new Promise((resolve, reject) => {const cleanEnv = { ...process.env };for (const key of Object.keys(cleanEnv)) {if (key.startsWith("OPENVIKING_")) delete cleanEnv[key];}- const child = spawn(process.execPath, [join(SCRIPT_DIR, "auto-capture.mjs")], {+ const child = spawn(process.execPath, [join(SCRIPT_DIR, script)], {env: { ...cleanEnv, ...env },stdio: ["pipe", "pipe", "pipe"],});@@ -27,7 +27,7 @@ function runAutoCapture(input, env) {child.on("error", reject);child.on("close", (code) => {if (code !== 0) {- reject(new Error(`auto-capture exited ${code}: ${stderr}`));+ reject(new Error(`${script} exited ${code}: ${stderr}`));return;}resolve({ stdout, stderr });@@ -36,6 +36,10 @@ function runAutoCapture(input, env) {});}+function runAutoCapture(input, env) {+ return runHook("auto-capture.mjs", input, env);+}+function hookEnv(root, baseUrl) {return {HOME: root,@@ -402,3 +406,42 @@ test("capture filters rewrite and drop turns at the send site", async () => {await rm(root, { recursive: true, force: true });}});++test("subagent capture sanitizes injected text before filtering and sending", async () => {+ const root = await mkdtemp(join(tmpdir(), "ov-cc-subagent-filters-"));+ const transcriptPath = join(root, "subagent.jsonl");+ const batches = [];+ try {+ await writeFile(transcriptPath, [+ JSON.stringify({ role: "user", content: "<system-reminder>approved</system-reminder>ignore this" }),+ JSON.stringify({ role: "user", content: "<system-reminder>secret</system-reminder>approved SECRET123" }),+ ].join("\n"));+ await withMockOpenViking(async (req, res) => {+ const url = new URL(req.url, "http://127.0.0.1");+ if (req.method === "GET" && url.pathname === "/health") {+ writeJson(res, { status: "ok", result: { healthy: true } });+ } else if (req.method === "POST" && url.pathname.endsWith("/messages/batch")) {+ batches.push(await readRequestBody(req));+ writeJson(res, { status: "ok", result: { added: batches.at(-1).messages.length } });+ } else {+ writeJson(res, { status: "ok", result: {} });+ }+ }, async (baseUrl) => {+ await runHook("subagent-stop.mjs", {+ session_id: "parent-capture-filters",+ agent_id: "child-1",+ agent_transcript_path: transcriptPath,+ cwd: root,+ }, {+ ...hookEnv(root, baseUrl),+ OPENVIKING_CAPTURE_FILTERS: "user:k/approved/,user:d/secret/,user:s/SECRET123/[redacted]/g",+ });+ });+ assert.equal(batches.length, 1);+ assert.deepEqual(batches[0].messages.map((message) => message.parts), [+ [{ type: "text", text: "approved [redacted]" }],+ ]);+ } finally {+ await rm(root, { recursive: true, force: true });+ }+});diff --git a/examples/claude-code-memory-plugin/scripts/shared/capture-utils.mjs b/examples/claude-code-memory-plugin/scripts/shared/capture-utils.mjsindex 777913f74..a606ff44b 100644--- a/examples/claude-code-memory-plugin/scripts/shared/capture-utils.mjs+++ b/examples/claude-code-memory-plugin/scripts/shared/capture-utils.mjs@@ -409,6 +409,61 @@ export function filterCaptureParts(parts, role, cfg = {}) {return { parts: out, dropped: out.length === 0 };}+/** Sanitize already-extracted message parts before applying capture rules. */+export function shapeCaptureParts(parts, role, cfg = {}) {+ const sanitized = (parts || []).map((part) => part?.type === "text"+ ? { ...part, text: sanitizeCapturedText(part.text) }+ : part);+ return filterCaptureParts(sanitized, role, cfg);+}++function faithfulCaptureDecision(text, cfg) {+ const sanitized = sanitizeCapturedText(text);+ if (!sanitized) return { shouldCapture: false, text: "" };+ const capped = truncateCaptureText(sanitized, cfg.captureMaxLength || 24000);+ const compact = oneLine(capped);+ if (/^\[openviking-memory\]/i.test(compact) || SLASH_COMMAND_RE.test(compact)) {+ return { shouldCapture: false, text: "" };+ }+ return { shouldCapture: true, text: capped };+}++/**+ * Shape one host message before an adapter builds its session payload.+ * Sanitize injected context before evaluating keep/drop rules, so text that+ * will not be sent cannot satisfy a keep rule. Tool parts are never filtered.+ */+export function shapeCapturePayload(payload, role, cfg = {}, { toolNameById = {}, faithful = false } = {}) {+ const options = { toolMaxChars: cfg.captureToolMaxChars, toolNameById };+ const rawText = extractTextFromPayload(payload, options);+ const sourceParts = extractPartsFromPayload(payload, options);+ const hasTextPart = sourceParts.some((part) => part?.type === "text");+ const sanitizedText = sanitizeCapturedText(rawText);+ // Some hosts supply an array of plain strings. It has text but no structured+ // parts, so use a temporary part for the same filter verdict.+ const fallback = sourceParts.length === 0 && sanitizedText+ ? [{ type: "text", text: rawText }]+ : [];+ const shaped = shapeCaptureParts(sourceParts.length ? sourceParts : fallback, role, cfg);+ if (shaped.dropped) return { parts: [], text: "", signalText: "", dropped: true };++ const parts = sourceParts.length ? shaped.parts : [];+ const decisionText = hasTextPart || fallback.length+ ? shaped.parts.filter((part) => part.type === "text").map((part) => part.text).join("\n\n")+ : sanitizedText;+ const decision = faithful+ ? faithfulCaptureDecision(decisionText, cfg)+ : shouldCaptureText(decisionText, role, cfg, { filters: false });+ // Codex scans turn.text for explicit-memory keywords; keep that scan based+ // on the user's original words while the filtered parts go on the wire.+ const signalText = decision.shouldCapture && parts.length+ ? (faithful+ ? faithfulCaptureDecision(sanitizedText, cfg)+ : shouldCaptureText(sanitizedText, role, cfg, { filters: false })).text+ : decision.text;+ return { parts, text: decision.shouldCapture ? decision.text : "", signalText: signalText || "", dropped: false };+}+export function extractCaptureTurns(rolloutEntries, cfg = {}) {const toolNameById = collectToolNamesByIdFromEntries(rolloutEntries);const turns = [];@@ -421,20 +476,9 @@ export function extractCaptureTurns(rolloutEntries, cfg = {}) {if (!role) continue;if (isAssistantSideCaptureRole(rawRole) && !cfg.captureAssistantTurns) continue;- const rawText = extractTextFromPayload(payload, { toolMaxChars: cfg.captureToolMaxChars });- const parts = extractPartsFromPayload(payload, {- toolMaxChars: cfg.captureToolMaxChars,- toolNameById,- });- const shaped = filterCaptureParts(parts, role, cfg);- if (shaped.dropped) continue;- // With parts on the wire the drop decision was already taken above, so the- // text path only runs the filters for the `content` fallback. That also- // keeps `turn.text` faithful for callers that scan it for trigger words.- const decision = shouldCaptureText(rawText, role, cfg, { filters: shaped.parts.length === 0 });- if (!decision.shouldCapture && shaped.parts.length === 0) continue;- const text = decision.shouldCapture ? decision.text : "";- turns.push({ role, text, parts: shaped.parts });+ const shaped = shapeCapturePayload(payload, role, cfg, { toolNameById });+ if (shaped.dropped || (!shaped.text && shaped.parts.length === 0)) continue;+ turns.push({ role, text: shaped.parts.length ? shaped.signalText : shaped.text, parts: shaped.parts });}return turns;}@@ -555,7 +599,7 @@ export function sanitizeCapturedText(text) {.replace(/^[ \t]*\[Subagent Context\][^\n]*$/gim, " ").replace(/^\s*Sender\s*\([^)]+\)\s*```[\s\S]*?```\s*/gim, " ").replace(/^\s*Conversation (?:metadata|info):\s*```[\s\S]*?```\s*/gim, " ")- .replace(/^\s*\[?\d{4}-\d{2}-\d{2}[T ][^\]\n]{3,80}\]?\s*/gm, "")+ .replace(/^[ \t]*\[\d{4}-\d{2}-\d{2}[T ][-+0-9:.Z]{5,35}\][ \t]*/gm, "").replace(/^\s*\d{10,13}\s+/gm, "");value = stripMetadataFences(value);value = stripInjectedDigestBlocks(value);diff --git a/examples/claude-code-memory-plugin/scripts/subagent-stop.mjs b/examples/claude-code-memory-plugin/scripts/subagent-stop.mjsindex 50d8e5a40..2a20d8f94 100644--- a/examples/claude-code-memory-plugin/scripts/subagent-stop.mjs+++ b/examples/claude-code-memory-plugin/scripts/subagent-stop.mjs@@ -33,7 +33,7 @@ import { maybeDetach, readHookStdin } from "./lib/async-writer.mjs";import { getEffectivePeerId } from "./lib/workspace-peer.mjs";import { runHookStage } from "./shared/agent-hook-runtime.mjs";import { sendSessionMessages } from "./shared/batch-send.mjs";-import { filterCaptureParts } from "./shared/capture-utils.mjs";+import { shapeCaptureParts } from "./shared/capture-utils.mjs";if (!isPluginEnabled()) {process.stdout.write(JSON.stringify({ decision: "approve" }) + "\n");@@ -81,8 +81,8 @@ async function pushTurns(cfg, ovSessionId, turns, { peerId = null, enqueueOnly =for (const turn of turns) {// Send structured parts: tool calls/results are dedicated `tool` parts, not// inlined into content, so the server can process them separately.- // Blank text parts are pruned and the configured capture filters applied.- const parts = filterCaptureParts(turn.parts, turn.role, cfg).parts;+ // Sanitize injected context before applying the configured filters.+ const parts = shapeCaptureParts(turn.parts, turn.role, cfg).parts;if (parts.length === 0) continue;const payload = { role: turn.role, parts };if (peerId) payload.peer_id = peerId;diff --git a/examples/codex-memory-plugin/.codex-plugin/plugin.json b/examples/codex-memory-plugin/.codex-plugin/plugin.jsonindex dcdf8e13b..7a5c04551 100644--- a/examples/codex-memory-plugin/.codex-plugin/plugin.json+++ b/examples/codex-memory-plugin/.codex-plugin/plugin.json@@ -1,6 +1,6 @@{"name": "openviking-memory",- "version": "0.10.2",+ "version": "0.10.3","description": "Long-term semantic memory for Codex, powered by OpenViking. Recall on UserPromptSubmit, incremental add_message on Stop (per turn), commit on SessionEnd (Codex >= 0.145) and on PreCompact, with an idle/ended sweep on SessionStart as the fallback for signals, crashes and older Codex builds. Includes a local stdio MCP proxy that reads OpenViking credentials from env/ovcli.conf.","author": {"name": "OpenViking",diff --git a/examples/codex-memory-plugin/scripts/capture-utils.test.mjs b/examples/codex-memory-plugin/scripts/capture-utils.test.mjsindex cd09baedb..b95d6bbea 100644--- a/examples/codex-memory-plugin/scripts/capture-utils.test.mjs+++ b/examples/codex-memory-plugin/scripts/capture-utils.test.mjs@@ -15,6 +15,16 @@ function toolParts(turns, toolName) {.filter((part) => part.type === "tool" && part.tool_name === toolName);}+test("keeps timestamped user log lines on the capture wire", () => {+ const log = "2024-01-01 12:00:00 ERROR something broke\n2024-01-01 12:00:01 INFO retry";+ const turns = extractCaptureTurns([{+ type: "response_item",+ payload: { type: "message", role: "user", content: [{ type: "input_text", text: log }] },+ }], CAPTURE_CONFIG);+ assert.equal(turns.length, 1);+ assert.deepEqual(turns[0].parts, [{ type: "text", text: log }]);+});+test("pairs current Codex function_call records by call_id", () => {const turns = extractCaptureTurns([diff --git a/examples/codex-memory-plugin/scripts/shared/capture-utils.mjs b/examples/codex-memory-plugin/scripts/shared/capture-utils.mjsindex 777913f74..a606ff44b 100644--- a/examples/codex-memory-plugin/scripts/shared/capture-utils.mjs+++ b/examples/codex-memory-plugin/scripts/shared/capture-utils.mjs@@ -409,6 +409,61 @@ export function filterCaptureParts(parts, role, cfg = {}) {return { parts: out, dropped: out.length === 0 };}+/** Sanitize already-extracted message parts before applying capture rules. */+export function shapeCaptureParts(parts, role, cfg = {}) {+ const sanitized = (parts || []).map((part) => part?.type === "text"+ ? { ...part, text: sanitizeCapturedText(part.text) }+ : part);+ return filterCaptureParts(sanitized, role, cfg);+}++function faithfulCaptureDecision(text, cfg) {+ const sanitized = sanitizeCapturedText(text);+ if (!sanitized) return { shouldCapture: false, text: "" };+ const capped = truncateCaptureText(sanitized, cfg.captureMaxLength || 24000);+ const compact = oneLine(capped);+ if (/^\[openviking-memory\]/i.test(compact) || SLASH_COMMAND_RE.test(compact)) {+ return { shouldCapture: false, text: "" };+ }+ return { shouldCapture: true, text: capped };+}++/**+ * Shape one host message before an adapter builds its session payload.+ * Sanitize injected context before evaluating keep/drop rules, so text that+ * will not be sent cannot satisfy a keep rule. Tool parts are never filtered.+ */+export function shapeCapturePayload(payload, role, cfg = {}, { toolNameById = {}, faithful = false } = {}) {+ const options = { toolMaxChars: cfg.captureToolMaxChars, toolNameById };+ const rawText = extractTextFromPayload(payload, options);+ const sourceParts = extractPartsFromPayload(payload, options);+ const hasTextPart = sourceParts.some((part) => part?.type === "text");+ const sanitizedText = sanitizeCapturedText(rawText);+ // Some hosts supply an array of plain strings. It has text but no structured+ // parts, so use a temporary part for the same filter verdict.+ const fallback = sourceParts.length === 0 && sanitizedText+ ? [{ type: "text", text: rawText }]+ : [];+ const shaped = shapeCaptureParts(sourceParts.length ? sourceParts : fallback, role, cfg);+ if (shaped.dropped) return { parts: [], text: "", signalText: "", dropped: true };++ const parts = sourceParts.length ? shaped.parts : [];+ const decisionText = hasTextPart || fallback.length+ ? shaped.parts.filter((part) => part.type === "text").map((part) => part.text).join("\n\n")+ : sanitizedText;+ const decision = faithful+ ? faithfulCaptureDecision(decisionText, cfg)+ : shouldCaptureText(decisionText, role, cfg, { filters: false });+ // Codex scans turn.text for explicit-memory keywords; keep that scan based+ // on the user's original words while the filtered parts go on the wire.+ const signalText = decision.shouldCapture && parts.length+ ? (faithful+ ? faithfulCaptureDecision(sanitizedText, cfg)+ : shouldCaptureText(sanitizedText, role, cfg, { filters: false })).text+ : decision.text;+ return { parts, text: decision.shouldCapture ? decision.text : "", signalText: signalText || "", dropped: false };+}+export function extractCaptureTurns(rolloutEntries, cfg = {}) {const toolNameById = collectToolNamesByIdFromEntries(rolloutEntries);const turns = [];@@ -421,20 +476,9 @@ export function extractCaptureTurns(rolloutEntries, cfg = {}) {if (!role) continue;if (isAssistantSideCaptureRole(rawRole) && !cfg.captureAssistantTurns) continue;- const rawText = extractTextFromPayload(payload, { toolMaxChars: cfg.captureToolMaxChars });- const parts = extractPartsFromPayload(payload, {- toolMaxChars: cfg.captureToolMaxChars,- toolNameById,- });- const shaped = filterCaptureParts(parts, role, cfg);- if (shaped.dropped) continue;- // With parts on the wire the drop decision was already taken above, so the- // text path only runs the filters for the `content` fallback. That also- // keeps `turn.text` faithful for callers that scan it for trigger words.- const decision = shouldCaptureText(rawText, role, cfg, { filters: shaped.parts.length === 0 });- if (!decision.shouldCapture && shaped.parts.length === 0) continue;- const text = decision.shouldCapture ? decision.text : "";- turns.push({ role, text, parts: shaped.parts });+ const shaped = shapeCapturePayload(payload, role, cfg, { toolNameById });+ if (shaped.dropped || (!shaped.text && shaped.parts.length === 0)) continue;+ turns.push({ role, text: shaped.parts.length ? shaped.signalText : shaped.text, parts: shaped.parts });}return turns;}@@ -555,7 +599,7 @@ export function sanitizeCapturedText(text) {.replace(/^[ \t]*\[Subagent Context\][^\n]*$/gim, " ").replace(/^\s*Sender\s*\([^)]+\)\s*```[\s\S]*?```\s*/gim, " ").replace(/^\s*Conversation (?:metadata|info):\s*```[\s\S]*?```\s*/gim, " ")- .replace(/^\s*\[?\d{4}-\d{2}-\d{2}[T ][^\]\n]{3,80}\]?\s*/gm, "")+ .replace(/^[ \t]*\[\d{4}-\d{2}-\d{2}[T ][-+0-9:.Z]{5,35}\][ \t]*/gm, "").replace(/^\s*\d{10,13}\s+/gm, "");value = stripMetadataFences(value);value = stripInjectedDigestBlocks(value);diff --git a/examples/dsh-memory-plugin/capture.mjs b/examples/dsh-memory-plugin/capture.mjsindex dd2ddb31a..09b24e71d 100644--- a/examples/dsh-memory-plugin/capture.mjs+++ b/examples/dsh-memory-plugin/capture.mjs@@ -1,8 +1,7 @@import { createUserMessage } from "@deepseek-ai/dsh-llm";import {- extractPartsFromPayload,extractTextFromPayload,- shouldCaptureText,+ shapeCapturePayload,} from "./shared/capture-utils.mjs";export const OPENVIKING_PLUGIN_SOURCE = "openviking-memory";@@ -74,27 +73,23 @@ function captureMessage(event, message, config, toolNames) {const role = message.role === "assistant" ? "assistant" : "user";const toolNameById = Object.fromEntries(toolNames);- const rawText = extractTextFromPayload(message, {- toolMaxChars: config.captureToolMaxChars,- });- const parts = extractPartsFromPayload(message, {- toolMaxChars: config.captureToolMaxChars,+ const shaped = shapeCapturePayload(message, role, config, {toolNameById,});- const decision = shouldCaptureText(rawText, role, config);- const structuredParts = parts.filter(part => part?.type !== "text");- if (!decision.shouldCapture && structuredParts.length === 0) return null;+ if (shaped.dropped) return null;+ const structuredParts = shaped.parts.filter(part => part?.type !== "text");+ if (!shaped.text && structuredParts.length === 0) return null;- const hasTextPart = parts.some(part => part?.type === "text");+ const hasTextPart = shaped.parts.some(part => part?.type === "text");const bodyParts = [- ...(hasTextPart && decision.shouldCapture && decision.text- ? [{ type: "text", text: decision.text }]+ ...(hasTextPart && shaped.text+ ? [{ type: "text", text: shaped.text }]: []),...structuredParts,];const payload = bodyParts.length > 0? { role, parts: bodyParts }- : { role, content: decision.text };+ : { role, content: shaped.text };const createdAt = eventCreatedAt(event);if (createdAt) payload.created_at = createdAt;if (config.peerId) payload.peer_id = config.peerId;diff --git a/examples/dsh-memory-plugin/capture.test.mjs b/examples/dsh-memory-plugin/capture.test.mjsindex f970422de..42c5a3ef1 100644--- a/examples/dsh-memory-plugin/capture.test.mjs+++ b/examples/dsh-memory-plugin/capture.test.mjs@@ -85,6 +85,23 @@ test("captures DSH message events without recapturing injected context", () => {assert.equal(otherPlugin, null);});+test("DSH capture applies rules after removing injected context", () => {+ const event = {+ type: "user/message",+ data: {+ role: "user",+ content: [{ type: "text", text: "<openviking-context>approved</openviking-context>Remember private details." }],+ source: { kind: "user" },+ },+ };+ assert.equal(captureEvent(event, { ...CONFIG, captureFilters: ["k/approved/"] }), null);+ assert.deepEqual(captureEvent(event, { ...CONFIG, captureFilters: ["d/approved/"] }), {+ role: "user",+ parts: [{ type: "text", text: "Remember private details." }],+ peer_id: "workspace-a",+ });+});+test("preserves DSH tool call identity in captured tool results", () => {const names = new Map();assert.equal(captureEvent({diff --git a/examples/dsh-memory-plugin/config.mjs b/examples/dsh-memory-plugin/config.mjsindex 852211c32..43664c6c1 100644--- a/examples/dsh-memory-plugin/config.mjs+++ b/examples/dsh-memory-plugin/config.mjs@@ -1,7 +1,7 @@import { buildPluginConfig } from "./shared/plugin-config.mjs";import { loadCredentialFiles } from "./shared/credentials.mjs";-export const PLUGIN_VERSION = "0.5.3";+export const PLUGIN_VERSION = "0.5.4";/*** Namespace for the bridged OpenViking MCP tools. DSH publishes every MCP tooldiff --git a/examples/dsh-memory-plugin/package-lock.json b/examples/dsh-memory-plugin/package-lock.jsonindex 79d6131ca..9d7494971 100644--- a/examples/dsh-memory-plugin/package-lock.json+++ b/examples/dsh-memory-plugin/package-lock.json@@ -1,12 +1,12 @@{"name": "@openviking/dsh-memory-plugin",- "version": "0.5.3",+ "version": "0.5.4","lockfileVersion": 3,"requires": true,"packages": {"": {"name": "@openviking/dsh-memory-plugin",- "version": "0.5.3",+ "version": "0.5.4","license": "Apache-2.0","devDependencies": {"@deepseek-ai/dsh-llm": "0.1.0-rc.6",diff --git a/examples/dsh-memory-plugin/package.json b/examples/dsh-memory-plugin/package.jsonindex f5ecea7b5..03aa95dc1 100644--- a/examples/dsh-memory-plugin/package.json+++ b/examples/dsh-memory-plugin/package.json@@ -1,6 +1,6 @@{"name": "@openviking/dsh-memory-plugin",- "version": "0.5.3",+ "version": "0.5.4","description": "OpenViking memory and context bundle for DeepSeek Harness","type": "module","main": "index.mjs",diff --git a/examples/memory-plugin-shared/capture-utils.test.mjs b/examples/memory-plugin-shared/capture-utils.test.mjsindex 7a2fd4d2e..fd0c81e07 100644--- a/examples/memory-plugin-shared/capture-utils.test.mjs+++ b/examples/memory-plugin-shared/capture-utils.test.mjs@@ -7,6 +7,8 @@ import {findLastHumanTurnIndex,filterCaptureParts,sanitizeCapturedText,+ shapeCapturePayload,+ shapeCaptureParts,shouldCaptureText,} from "./lib/capture-utils.mjs"@@ -242,6 +244,80 @@ test("filterCaptureParts never judges a turn that carries no text", () => {assert.equal(filterCaptureParts([], "user", { captureFilters: ["k/x/"] }).dropped, false)})+test("already-extracted parts are sanitized before capture rules run", () => {+ const parts = [+ { type: "text", text: "<system-reminder>approved</system-reminder>Remember SECRET123" },+ TOOL_PART,+ ]+ assert.equal(shapeCaptureParts(parts, "user", { captureFilters: ["k/approved/"] }).dropped, true)+ assert.deepEqual(shapeCaptureParts(parts, "user", {+ captureFilters: ["d/approved/", "s/SECRET123/[redacted]/g"],+ }).parts, [{ type: "text", text: "Remember [redacted]" }, TOOL_PART])+ assert.equal(parts[0].text.includes("SECRET123"), true)+})++test("shared capture filters sanitized conversation text before keep and drop", () => {+ const payload = { role: "user", content: "<openviking-context>approved</openviking-context>Remember private details." }+ const kept = shapeCapturePayload(payload, "user", { captureFilters: ["k/approved/"] })+ assert.equal(kept.dropped, true)+ assert.deepEqual(extractCaptureTurns([{ payload }], { captureFilters: ["k/approved/"] }), [])++ const dropped = shapeCapturePayload(payload, "user", { captureFilters: ["d/approved/"] })+ assert.equal(dropped.dropped, false)+ assert.deepEqual(dropped.parts, [{ type: "text", text: "Remember private details." }])+ assert.equal(dropped.text, "Remember private details.")+})++test("shared capture filters aggregate text and leave tool payloads intact", () => {+ const payload = { role: "assistant", content: [+ { type: "text", text: "Remember secret" },+ { type: "toolCall", id: "call-1", name: "lookup", arguments: { secret: true } },+ ] }+ assert.equal(shapeCapturePayload(payload, "assistant", {+ captureFilters: ["d/secret/"],+ }, { faithful: true }).dropped, true)++ const shaped = shapeCapturePayload(payload, "assistant", {+ captureFilters: ["s/secret/[redacted]/g"],+ }, { faithful: true })+ assert.equal(shaped.text, "Remember [redacted]")+ assert.equal(shaped.parts[1].tool_input.secret, true)++ const toolOnly = shapeCapturePayload({ role: "assistant", content: payload.content.slice(1) }, "assistant", {+ captureFilters: ["k/never matches/"],+ }, { faithful: true })+ assert.equal(toolOnly.dropped, false)+ assert.equal(toolOnly.parts[0].type, "tool")+})++test("shared capture uses conversation text for mixed messages regardless of rule presence", () => {+ const payload = { role: "assistant", content: [+ { type: "text", text: "Run this." },+ { type: "toolCall", id: "call-1", name: "lookup", arguments: { q: "x" } },+ ] }+ const baseline = shapeCapturePayload(payload, "assistant", {}, { faithful: true })+ assert.equal(baseline.text, "Run this.")+ assert.deepEqual(shapeCapturePayload(payload, "assistant", {+ captureFilters: ["user:d/.*/"],+ }, { faithful: true }), baseline)+ assert.deepEqual(shapeCapturePayload(payload, "assistant", {+ captureFilters: ["s/zzz/y/"],+ }, { faithful: true }), baseline)+ assert.equal(shapeCapturePayload(payload, "assistant", {+ captureFilters: ["assistant:s/Run/Do/"],+ }, { faithful: true }).text, "Do this.")+})++test("sanitation keeps ordinary timestamped log lines", () => {+ const log = "2024-01-01 12:00:00 ERROR something broke\n2024-01-01 12:00:01 INFO retry"+ assert.equal(sanitizeCapturedText(log), log)+ const turns = extractCaptureTurns([userEntry(log)], {})+ assert.equal(turns.length, 1)+ assert.deepEqual(turns[0].parts, [{ type: "text", text: log }])+ assert.equal(sanitizeCapturedText("[2024-01-01 12:00:00] Context"), "Context")+ assert.equal(sanitizeCapturedText("[2024-01-01 12:00:00 ERROR] failed"), "[2024-01-01 12:00:00 ERROR] failed")+})+test("shouldCaptureText reports a filtered drop and can be asked to skip filters", () => {const cfg = { captureFilters: ["user:d/^\\/compact\\b/", "s/^ultrathink\\s+//"] }assert.deepEqual(shouldCaptureText("/compact the thread", "user", cfg), {diff --git a/examples/memory-plugin-shared/lib/capture-utils.d.mts b/examples/memory-plugin-shared/lib/capture-utils.d.mtsindex 4da3e3306..ee22a39d6 100644--- a/examples/memory-plugin-shared/lib/capture-utils.d.mts+++ b/examples/memory-plugin-shared/lib/capture-utils.d.mts@@ -2,7 +2,12 @@ export function collectToolNamesByIdFromEntries(entries: any[]): Record<string,export function findLastHumanTurnIndex(turns: any[]): number;export function extractPartsFromPayload(payload: any, options?: Record<string, any>): any[];export function extractTextFromPayload(payload: any, options?: Record<string, any>): string;-export function shouldCaptureText(text: string, role: string, cfg?: Record<string, any>): {+export function shapeCaptureParts(parts: any[], role: string, cfg?: Record<string, any>): { parts: any[]; dropped: boolean };+export function shapeCapturePayload(payload: any, role: string, cfg?: Record<string, any>, options?: {+ toolNameById?: Record<string, string>;+ faithful?: boolean;+}): { parts: any[]; text: string; signalText: string; dropped: boolean };+export function shouldCaptureText(text: string, role: string, cfg?: Record<string, any>, options?: { filters?: boolean }): {shouldCapture: boolean;reason: string;text: string;diff --git a/examples/memory-plugin-shared/lib/capture-utils.mjs b/examples/memory-plugin-shared/lib/capture-utils.mjsindex e707b2ec5..2347d0562 100644--- a/examples/memory-plugin-shared/lib/capture-utils.mjs+++ b/examples/memory-plugin-shared/lib/capture-utils.mjs@@ -408,6 +408,61 @@ export function filterCaptureParts(parts, role, cfg = {}) {return { parts: out, dropped: out.length === 0 };}+/** Sanitize already-extracted message parts before applying capture rules. */+export function shapeCaptureParts(parts, role, cfg = {}) {+ const sanitized = (parts || []).map((part) => part?.type === "text"+ ? { ...part, text: sanitizeCapturedText(part.text) }+ : part);+ return filterCaptureParts(sanitized, role, cfg);+}++function faithfulCaptureDecision(text, cfg) {+ const sanitized = sanitizeCapturedText(text);+ if (!sanitized) return { shouldCapture: false, text: "" };+ const capped = truncateCaptureText(sanitized, cfg.captureMaxLength || 24000);+ const compact = oneLine(capped);+ if (/^\[openviking-memory\]/i.test(compact) || SLASH_COMMAND_RE.test(compact)) {+ return { shouldCapture: false, text: "" };+ }+ return { shouldCapture: true, text: capped };+}++/**+ * Shape one host message before an adapter builds its session payload.+ * Sanitize injected context before evaluating keep/drop rules, so text that+ * will not be sent cannot satisfy a keep rule. Tool parts are never filtered.+ */+export function shapeCapturePayload(payload, role, cfg = {}, { toolNameById = {}, faithful = false } = {}) {+ const options = { toolMaxChars: cfg.captureToolMaxChars, toolNameById };+ const rawText = extractTextFromPayload(payload, options);+ const sourceParts = extractPartsFromPayload(payload, options);+ const hasTextPart = sourceParts.some((part) => part?.type === "text");+ const sanitizedText = sanitizeCapturedText(rawText);+ // Some hosts supply an array of plain strings. It has text but no structured+ // parts, so use a temporary part for the same filter verdict.+ const fallback = sourceParts.length === 0 && sanitizedText+ ? [{ type: "text", text: rawText }]+ : [];+ const shaped = shapeCaptureParts(sourceParts.length ? sourceParts : fallback, role, cfg);+ if (shaped.dropped) return { parts: [], text: "", signalText: "", dropped: true };++ const parts = sourceParts.length ? shaped.parts : [];+ const decisionText = hasTextPart || fallback.length+ ? shaped.parts.filter((part) => part.type === "text").map((part) => part.text).join("\n\n")+ : sanitizedText;+ const decision = faithful+ ? faithfulCaptureDecision(decisionText, cfg)+ : shouldCaptureText(decisionText, role, cfg, { filters: false });+ // Codex scans turn.text for explicit-memory keywords; keep that scan based+ // on the user's original words while the filtered parts go on the wire.+ const signalText = decision.shouldCapture && parts.length+ ? (faithful+ ? faithfulCaptureDecision(sanitizedText, cfg)+ : shouldCaptureText(sanitizedText, role, cfg, { filters: false })).text+ : decision.text;+ return { parts, text: decision.shouldCapture ? decision.text : "", signalText: signalText || "", dropped: false };+}+export function extractCaptureTurns(rolloutEntries, cfg = {}) {const toolNameById = collectToolNamesByIdFromEntries(rolloutEntries);const turns = [];@@ -420,20 +475,9 @@ export function extractCaptureTurns(rolloutEntries, cfg = {}) {if (!role) continue;if (isAssistantSideCaptureRole(rawRole) && !cfg.captureAssistantTurns) continue;- const rawText = extractTextFromPayload(payload, { toolMaxChars: cfg.captureToolMaxChars });- const parts = extractPartsFromPayload(payload, {- toolMaxChars: cfg.captureToolMaxChars,- toolNameById,- });- const shaped = filterCaptureParts(parts, role, cfg);- if (shaped.dropped) continue;- // With parts on the wire the drop decision was already taken above, so the- // text path only runs the filters for the `content` fallback. That also- // keeps `turn.text` faithful for callers that scan it for trigger words.- const decision = shouldCaptureText(rawText, role, cfg, { filters: shaped.parts.length === 0 });- if (!decision.shouldCapture && shaped.parts.length === 0) continue;- const text = decision.shouldCapture ? decision.text : "";- turns.push({ role, text, parts: shaped.parts });+ const shaped = shapeCapturePayload(payload, role, cfg, { toolNameById });+ if (shaped.dropped || (!shaped.text && shaped.parts.length === 0)) continue;+ turns.push({ role, text: shaped.parts.length ? shaped.signalText : shaped.text, parts: shaped.parts });}return turns;}@@ -554,7 +598,7 @@ export function sanitizeCapturedText(text) {.replace(/^[ \t]*\[Subagent Context\][^\n]*$/gim, " ").replace(/^\s*Sender\s*\([^)]+\)\s*```[\s\S]*?```\s*/gim, " ").replace(/^\s*Conversation (?:metadata|info):\s*```[\s\S]*?```\s*/gim, " ")- .replace(/^\s*\[?\d{4}-\d{2}-\d{2}[T ][^\]\n]{3,80}\]?\s*/gm, "")+ .replace(/^[ \t]*\[\d{4}-\d{2}-\d{2}[T ][-+0-9:.Z]{5,35}\][ \t]*/gm, "").replace(/^\s*\d{10,13}\s+/gm, "");value = stripMetadataFences(value);value = stripInjectedDigestBlocks(value);diff --git a/examples/opencode-plugin/lib/memory-session.mjs b/examples/opencode-plugin/lib/memory-session.mjsindex 1dff5f55a..0014cc257 100644--- a/examples/opencode-plugin/lib/memory-session.mjs+++ b/examples/opencode-plugin/lib/memory-session.mjs@@ -1,10 +1,9 @@import fs from "fs"import path from "path"import {- extractPartsFromPayload,extractTextFromPayload,isCaptureEnabled,- shouldCaptureText,+ shapeCapturePayload,} from "./shared/capture-utils.mjs"import {deriveHarnessSessionId,@@ -383,18 +382,17 @@ export function createMemorySessionManager({ config, pluginRoot }) {if (!role) return nullif (role === "assistant" && !config.captureAssistantTurns) return null- const rawText = partsRaw- .map((part) => extractTextFromPayload(part, { toolMaxChars: config.captureToolMaxChars }))- .filter(Boolean)- .join("\n\n")- const captureParts = partsRaw.flatMap((part) => extractPartsFromPayload(part, {- toolMaxChars: config.captureToolMaxChars,- }))- const decision = shouldCaptureText(rawText, role, config)- if (!decision.shouldCapture && captureParts.length === 0) return null- const body = captureParts.length > 0- ? { role, parts: captureParts }- : { role, content: decision.text }+ const shaped = shapeCapturePayload({ role, content: partsRaw }, role, config)+ const toolParts = shaped.parts.filter((part) => part.type !== "text")+ if (shaped.dropped || (!shaped.text && toolParts.length === 0)) return null+ const body = toolParts.length > 0+ ? { role, parts: [+ ...(shaped.text && shaped.parts.some((part) => part.type === "text")+ ? [{ type: "text", text: shaped.text }]+ : []),+ ...toolParts,+ ] }+ : { role, content: shaped.text }const peerId = effectivePeerId(config)if (peerId) body.peer_id = peerIdreturn bodydiff --git a/examples/opencode-plugin/package.json b/examples/opencode-plugin/package.jsonindex 70acb7e26..6d4dd1a1f 100644--- a/examples/opencode-plugin/package.json+++ b/examples/opencode-plugin/package.json@@ -1,6 +1,6 @@{"name": "@openviking/opencode-plugin",- "version": "0.5.0",+ "version": "0.5.1","author": "@openviking","description": "Unified OpenCode plugin for OpenViking repository retrieval and long-term memory","type": "module",diff --git a/examples/opencode-plugin/tests/memory-session.test.mjs b/examples/opencode-plugin/tests/memory-session.test.mjsindex 8b22ec28b..414ec5dfc 100644--- a/examples/opencode-plugin/tests/memory-session.test.mjs+++ b/examples/opencode-plugin/tests/memory-session.test.mjs@@ -6,6 +6,7 @@ import { mkdtemp, rm } from "node:fs/promises"import { tmpdir } from "node:os"import { join } from "node:path"import { createMemorySessionManager } from "../lib/memory-session.mjs"+import { contextMessageEvents } from "../lib/v2-events.mjs"import { initLogger } from "../lib/utils.mjs"async function withTempDir(prefix, fn) {@@ -409,6 +410,64 @@ test("captured message parts are not retained in the session state file", async})})+test("OpenCode capture filters sanitized text before sending", async () => {+ await withCaptureServer(async ({ endpoint, requests }) => {+ await withTempDir("ov-oc-filters-", async (dir) => {+ const manager = createMemorySessionManager({+ config: { ...baseConfig(endpoint), captureFilters: ["k/approved/", "s/secret/[redacted]/g"] },+ pluginRoot: dir,+ })+ await manager.init()+ for (const [id, text] of [+ ["removed", "<openviking-context>approved</openviking-context>Remember secret details."],+ ["kept", "Remember approved secret details."],+ ]) {+ for (const event of contextMessageEvents("oc-filters", { id, type: "user", text })) {+ await manager.handleEvent(event)+ }+ }+ await manager.handleEvent({ type: "session.idle", sessionID: "oc-filters" })+ const sent = requests.find((request) => request.url === "/api/v1/sessions/oc-oc-filters/messages/batch")+ assert.ok(sent)+ assert.deepEqual(JSON.parse(sent.body).messages, [+ { role: "user", content: "Remember approved [redacted] details." },+ ])+ await manager.flushAll({ commit: false })+ })+ })+})++test("OpenCode caps mixed-message text and omits low-signal text", async () => {+ await withCaptureServer(async ({ endpoint, requests }) => {+ await withTempDir("ov-oc-mixed-", async (dir) => {+ const manager = createMemorySessionManager({ config: baseConfig(endpoint), pluginRoot: dir })+ await manager.init()+ for (const [id, text] of [["long", "x".repeat(40000)], ["ack", "ok"]]) {+ const message = {+ id, type: "assistant", content: [+ { type: "text", text },+ { type: "tool", id: `tool-${id}`, name: "lookup", state: {+ status: "completed", input: { id }, content: [{ type: "text", text: "result" }],+ } },+ ],+ }+ for (const event of contextMessageEvents("oc-mixed", message)) await manager.handleEvent(event)+ }+ await manager.handleEvent({ type: "session.idle", sessionID: "oc-mixed" })+ const sent = requests.find((request) => request.url === "/api/v1/sessions/oc-oc-mixed/messages/batch")+ assert.ok(sent)+ const messages = JSON.parse(sent.body).messages+ assert.equal(messages.length, 2)+ assert.equal(messages[0].parts[0].type, "text")+ assert.ok(messages[0].parts[0].text.length <= 24000)+ assert.match(messages[0].parts[0].text, /\[truncated\]$/)+ assert.equal(messages[0].parts[1].type, "tool")+ assert.deepEqual(messages[1].parts.map((part) => part.type), ["tool"])+ await manager.flushAll({ commit: false })+ })+ })+})+test("concurrent saves never race the shared state file (#3877)", async (t) => {// Widen the race window: concurrent saveState() calls share the same// `${statePath}.tmp` temp file. A slow writeFile keeps the shared .tmpdiff --git a/examples/pi-coding-agent-extension/lib/capture-adapter.mjs b/examples/pi-coding-agent-extension/lib/capture-adapter.mjsindex de2dc522a..f4096e8fc 100644--- a/examples/pi-coding-agent-extension/lib/capture-adapter.mjs+++ b/examples/pi-coding-agent-extension/lib/capture-adapter.mjs@@ -1,9 +1,5 @@import {- extractPartsFromPayload,- extractTextFromPayload,- sanitizeCapturedText,- shouldCaptureText,- truncateCaptureText,+ shapeCapturePayload,} from "../shared/capture-utils.mjs";function normalizeRole(role) {@@ -24,20 +20,6 @@ function entryPayload(entry) {return entry;}-function faithfulDecision(rawText, cfg) {- const sanitized = sanitizeCapturedText(rawText);- if (!sanitized) return { shouldCapture: false, reason: "empty", text: "" };- const capped = truncateCaptureText(sanitized, cfg.captureMaxLength || 24000);- const compact = String(capped || "").replace(/\s+/g, " ").trim();- if (/^\[openviking-memory\]/i.test(compact)) {- return { shouldCapture: false, reason: "plugin_status", text: "" };- }- if (/^\/[a-z0-9_-]{1,64}\b/i.test(compact)) {- return { shouldCapture: false, reason: "slash_command", text: "" };- }- return { shouldCapture: true, reason: "faithful", text: capped };-}-export function extractBranchCapturePayloads(branch, syncedEntryCount = 0, cfg = {}) {const entries = Array.isArray(branch) ? branch : [];const previousCount = Math.max(0, Number(syncedEntryCount) || 0);@@ -53,28 +35,23 @@ export function extractBranchCapturePayloads(branch, syncedEntryCount = 0, cfg =if (!role) continue;if (role === "assistant" && cfg.captureAssistantTurns === false) continue;- const rawText = extractTextFromPayload(payload, { toolMaxChars: cfg.captureToolMaxChars });- const parts = extractPartsFromPayload(payload, { toolMaxChars: cfg.captureToolMaxChars });- const decision = cfg.faithfulCapture || cfg.takeoverEnabled- ? faithfulDecision(rawText, cfg)- : shouldCaptureText(rawText, role, cfg);- const structuredParts = parts.filter((part) => part?.type !== "text");- if (!decision.shouldCapture && structuredParts.length === 0) continue;+ const shaped = shapeCapturePayload(payload, role, cfg, {+ faithful: cfg.faithfulCapture || cfg.takeoverEnabled,+ });+ if (shaped.dropped) continue;+ const structuredParts = shaped.parts.filter((part) => part?.type !== "text");+ if (!shaped.text && structuredParts.length === 0) continue;- // decision.text is derived from rawText, which renders tool I/O as- // "[tool-result ...]" lines. For a tool-only payload that would resend the- // same output the tool part already carries, so only keep it when the- // payload really had text of its own.- const hasTextPart = parts.some((part) => part?.type === "text");+ // Tool-only payloads must not also send rendered tool output as text.const bodyParts = [- ...(hasTextPart && decision.shouldCapture && decision.text- ? [{ type: "text", text: decision.text }]+ ...(shaped.parts.some((part) => part?.type === "text") && shaped.text+ ? [{ type: "text", text: shaped.text }]: []),...structuredParts,];const body = bodyParts.length > 0? { role, parts: bodyParts }- : { role, content: decision.text };+ : { role, content: shaped.text };if (cfg.peerId) body.peer_id = cfg.peerId;payloads.push(body);}diff --git a/examples/pi-coding-agent-extension/package-lock.json b/examples/pi-coding-agent-extension/package-lock.jsonindex 56f992790..0c70f6b80 100644--- a/examples/pi-coding-agent-extension/package-lock.json+++ b/examples/pi-coding-agent-extension/package-lock.json@@ -1,12 +1,12 @@{"name": "openviking-pi-extension",- "version": "0.4.3",+ "version": "0.4.4","lockfileVersion": 3,"requires": true,"packages": {"": {"name": "openviking-pi-extension",- "version": "0.4.3",+ "version": "0.4.4","dependencies": {"@modelcontextprotocol/client": "2.0.0"},diff --git a/examples/pi-coding-agent-extension/package.json b/examples/pi-coding-agent-extension/package.jsonindex 67cd32a82..c650ae81c 100644--- a/examples/pi-coding-agent-extension/package.json+++ b/examples/pi-coding-agent-extension/package.json@@ -1,6 +1,6 @@{"name": "openviking-pi-extension",- "version": "0.4.3",+ "version": "0.4.4","description": "OpenViking memory and context extension for the pi coding agent","private": true,"type": "module",diff --git a/examples/pi-coding-agent-extension/tests/capture-adapter.test.mjs b/examples/pi-coding-agent-extension/tests/capture-adapter.test.mjsindex 5e41a8da9..b6227b867 100644--- a/examples/pi-coding-agent-extension/tests/capture-adapter.test.mjs+++ b/examples/pi-coding-agent-extension/tests/capture-adapter.test.mjs@@ -122,3 +122,149 @@ test("tool-only payloads carry tool output once, not duplicated as text", () =>assert.equal(toolParts.length, 1);assert.equal(toolParts[0].tool_output, output);});++test("mixed text and tool capture does not duplicate rendered tool calls", () => {+ const { payloads } = extractBranchCapturePayloads([{+ role: "assistant",+ content: [+ { type: "text", text: "Run this." },+ { type: "toolCall", id: "call-1", name: "lookup", arguments: { q: "x" } },+ ],+ }], 0, { faithfulCapture: true });+ assert.equal(payloads[0].parts[0].text, "Run this.");+ assert.equal(payloads[0].parts[1].tool_name, "lookup");+});++for (const [mode, modeConfig] of [+ ["normal", {}],+ ["takeover", { takeoverEnabled: true }],+ ["faithful", { faithfulCapture: true }],+]) {+ for (const [name, rules, text, expected] of [+ ["substitution", ["s/secret/[removed]/g"], "Remember secret for later.", "Remember [removed] for later."],+ ["drop", ["d/secret/"], "Remember secret for later.", null],+ ["keep match", ["k/approved/"], "Remember approved settings.", "Remember approved settings."],+ ["keep miss", ["k/approved/"], "Remember other settings.", null],+ ["empty substitution", ["s/^.*$//"], "Remember secret for later.", null],+ ["single substitution", ["s/secret/secret-safe/"], "Remember secret for later.", "Remember secret-safe for later."],+ ["ordered rules", ["s/secret/blocked/", "d/blocked/"], "Remember secret for later.", null],+ ["filter after sanitation", ["d/secret/"], "<openviking-context>secret</openviking-context>Remember safe settings.", "Remember safe settings."],+ ["filter before truncation", ["d/secret/"], `${"Safe settings. ".repeat(10)}secret`, null],+ ]) {+ for (const fallback of [false, true]) {+ test(`${mode} capture applies ${name} to ${fallback ? "fallback content" : "text parts"}`, () => {+ const branch = [{ role: "user", content: fallback ? [text] : text }];+ const original = structuredClone(branch);+ const result = extractBranchCapturePayloads(branch, 0, {+ ...modeConfig, captureFilters: rules, captureMaxLength: 80, peerId: "peer-filter",+ });+ assert.deepEqual(result.payloads, expected === null ? [] : [{+ role: "user",+ ...(fallback ? { content: expected } : { parts: [{ type: "text", text: expected }] }),+ peer_id: "peer-filter",+ }]);+ assert.equal(result.nextEntryCount, 1);+ assert.deepEqual(branch, original);+ assert.deepEqual(extractBranchCapturePayloads(branch, 1, modeConfig).payloads, []);+ });+ }+ }++ for (const scope of ["user", "assistant"]) {+ test(`${mode} capture applies ${scope}-scoped rules only to that role`, () => {+ const result = extractBranchCapturePayloads(["USER", "ASSISTANT"].map((role) => ({+ role, content: "Remember secret for later.",+ })), 0, { ...modeConfig, captureFilters: [`${scope}:s/secret/[removed]/`] });+ assert.deepEqual(result.payloads, ["user", "assistant"].map((role) => ({+ role,+ parts: [{ type: "text", text: `Remember ${role === scope ? "[removed]" : "secret"} for later.` }],+ })));+ });+ }++ const toolCall = { type: "toolCall", id: "call-filter", name: "lookup", arguments: { value: "secret" } };+ const toolPart = {+ type: "tool", tool_id: "call-filter", tool_name: "lookup",+ tool_status: "running", tool_input: { value: "secret" },+ };+ for (const [name, rules, texts, expected] of [+ ["whole-turn drop", ["d/secret/"], ["Remember secret for later."], null],+ ["whole-turn keep miss", ["k/approved/"], ["Remember other settings."], null],+ ["conversation-only matching", ["d/secret/"], ["Remember safe settings."], "Remember safe settings."],+ ["conversation-only substitution", ["s/secret/secret-safe/"], ["Remember secret for later."], "Remember secret-safe for later."],+ ["aggregate keep", ["k/approved/"], ["Remember approved settings.", "Keep this detail too."], "Remember approved settings.\n\nKeep this detail too."],+ ["empty text removal", ["s/^.*$//s"], ["Remember secret for later."], ""],+ ]) {+ test(`${mode} capture uses ${name} for mixed text and tools`, () => {+ const branch = [{ role: "assistant", content: [+ ...texts.map((text) => ({ type: "text", text })), toolCall,+ ] }];+ const original = structuredClone(branch);+ const { payloads } = extractBranchCapturePayloads(branch, 0, { ...modeConfig, captureFilters: rules });+ assert.deepEqual(payloads, expected === null ? [] : [{+ role: "assistant",+ parts: [...(expected ? [{ type: "text", text: expected }] : []), toolPart],+ }]);+ assert.deepEqual(branch, original);+ });+ }++ test(`${mode} capture preserves tool-only input and output with conversation rules`, () => {+ const branch = [+ { role: "assistant", content: [toolCall] },+ { role: "toolResult", toolCallId: "call-filter", toolName: "lookup", content: "secret" },+ ];+ const { payloads } = extractBranchCapturePayloads(branch, 0, {+ ...modeConfig, captureFilters: ["s/secret/[removed]/", "d/secret/", "k/approved/"],+ });+ assert.deepEqual(payloads, [+ { role: "assistant", parts: [toolPart] },+ { role: "user", parts: [{+ type: "tool", tool_id: "call-filter", tool_name: "lookup",+ tool_status: "completed", tool_output: "secret",+ }] },+ ]);+ });++ for (const role of ["user", "assistant"]) {+ test(`${mode} capture preserves exact ${role} output when no valid filter applies`, () => {+ const branch = [+ { role, content: " Remember these settings. " },+ { role, content: ["Remember this fallback."] },+ { role, content: [{ type: "text", text: "Remember tool settings." }, toolCall] },+ { role, content: [toolCall] },+ ...["ok", "hi", "!!!", "/viking", "[OpenViking-memory] synced"].map((content) => ({ role, content })),+ ];+ const baseline = extractBranchCapturePayloads(branch, 0, modeConfig);+ for (const captureFilters of [undefined, [], ["invalid", "s/[//", 42], [`${role === "user" ? "assistant" : "user"}:d/.*/`]]) {+ assert.deepEqual(extractBranchCapturePayloads(branch, 0, { ...modeConfig, captureFilters }), baseline);+ }+ });+ }++ test(`${mode} capture preserves its short-turn policy after filtering`, () => {+ const { payloads } = extractBranchCapturePayloads(["ok", "hi", "!!!"].map((content) => ({+ role: "user", content: `prefix ${content}`,+ })), 0, { ...modeConfig, captureFilters: ["s/^prefix //"] });+ assert.deepEqual(payloads, mode === "normal" ? [] : ["ok", "hi", "!!!"].map((text) => ({+ role: "user", parts: [{ type: "text", text }],+ })));+ });++ test(`${mode} capture excludes commands and status after filtering`, () => {+ const { payloads } = extractBranchCapturePayloads([+ { role: "user", content: "prefix /viking" },+ { role: "assistant", content: "prefix [OpenViking-memory] synced" },+ ], 0, { ...modeConfig, captureFilters: ["s/^prefix //"] });+ assert.deepEqual(payloads, []);+ });++ test(`${mode} capture cannot satisfy keep with removed context`, () => {+ const branch = [{ role: "user", content: "<openviking-context>approved</openviking-context>Remember private details." }];+ const result = extractBranchCapturePayloads(branch, 0, {+ ...modeConfig, captureFilters: ["k/approved/"],+ });+ assert.deepEqual(result.payloads, []);+ assert.equal(result.nextEntryCount, 1);+ });+}