Skip to content

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

fix(dsh): use producer-owned source kind (#5318)

dvd233914078f7cc4f
diff --git a/examples/dsh-memory-plugin/README.md b/examples/dsh-memory-plugin/README.md
index b7738aa20..5523e1813 100644
--- a/examples/dsh-memory-plugin/README.md
+++ b/examples/dsh-memory-plugin/README.md
@@ -103,7 +103,10 @@ the stock `web` profile, not a subcommand to append after `--profile`.
### Why injection uses pre-step user messages, not the system prompt
Recall and profile context enter through the `agent/pre-step` waterfall as
-durable, source-attributed user messages (`source: { kind: 'plugin', … }`).
+durable, source-attributed user messages
+(`source: { kind: 'plugin:openviking-memory', … }`). The producer-owned kind is
+required by DSH session format v4; the plugin still recognizes legacy
+`kind: 'plugin'` messages while older sessions are replayed.
They are deliberately **not** added to the system prompt: a DSH preset whose
persona declares `complete: true` (the stock `minimal` preset does) restores
that persona as the sole prompt section after assembly, silently discarding
diff --git a/examples/dsh-memory-plugin/capture.mjs b/examples/dsh-memory-plugin/capture.mjs
index 445ccbde6..dd2ddb31a 100644
--- a/examples/dsh-memory-plugin/capture.mjs
+++ b/examples/dsh-memory-plugin/capture.mjs
@@ -6,6 +6,7 @@ import {
} from "./shared/capture-utils.mjs";
export const OPENVIKING_PLUGIN_SOURCE = "openviking-memory";
+export const OPENVIKING_PLUGIN_KIND = `plugin:${OPENVIKING_PLUGIN_SOURCE}`;
export function pluginMessage(content, source) {
// dsh's own constructor: identity, normalization, and any future Message
@@ -13,13 +14,31 @@ export function pluginMessage(content, source) {
return createUserMessage({
content: [{ type: "text", text: content }],
source: {
- kind: "plugin",
+ kind: OPENVIKING_PLUGIN_KIND,
plugin: OPENVIKING_PLUGIN_SOURCE,
...source,
},
});
}
+export function isOpenVikingPluginMessage(message) {
+ const source = message?.source;
+ return source?.kind === OPENVIKING_PLUGIN_KIND
+ || (source?.kind === "plugin" && source.plugin === OPENVIKING_PLUGIN_SOURCE);
+}
+
+function isSyntheticUserMessage(message) {
+ const kind = message?.source?.kind;
+ if (kind === "plugin") return true;
+ if (typeof kind === "string" && kind.startsWith("plugin:")) return true;
+ // DSH's v4 migration gives first-party context producers their own kinds
+ // (for example, "time-context") instead of the retired plugin wrapper.
+ return message?.role === "user"
+ && typeof kind === "string"
+ && kind !== "user"
+ && kind !== "tool";
+}
+
export function captureEvent(event, config, toolNames = new Map()) {
if (!event || typeof event !== "object") return null;
if (event.type === "tool/call") {
@@ -42,11 +61,10 @@ export function captureEvent(event, config, toolNames = new Map()) {
}
function captureMessage(event, message, config, toolNames) {
- // Whitelist by source: plugin-injected user messages (this plugin's recall
- // blocks, time-context snapshots, any other plugin's context) are model
- // input, not human input — mirroring them would launder synthetic text
- // into memory as if a person said it.
- if (message.source?.kind === "plugin") return null;
+ // DSH v4 gives producer-owned context messages their own kind. Treat
+ // user-role messages with an explicit non-user/tool kind as synthetic;
+ // untagged legacy messages stay capturable for backward compatibility.
+ if (isSyntheticUserMessage(message)) return null;
if (message.role === "assistant" && config.captureAssistantTurns === false) {
return null;
}
@@ -85,10 +103,7 @@ function captureMessage(event, message, config, toolNames) {
export function promptText(messages) {
return (messages || [])
- .filter(message => !(
- message?.source?.kind === "plugin"
- && message.source.plugin === OPENVIKING_PLUGIN_SOURCE
- ))
+ .filter(message => !isOpenVikingPluginMessage(message))
.map(message => extractTextFromPayload(message))
.filter(Boolean)
.join("\n\n")
diff --git a/examples/dsh-memory-plugin/capture.test.mjs b/examples/dsh-memory-plugin/capture.test.mjs
index 7a72c76e8..f970422de 100644
--- a/examples/dsh-memory-plugin/capture.test.mjs
+++ b/examples/dsh-memory-plugin/capture.test.mjs
@@ -1,6 +1,12 @@
import assert from "node:assert/strict";
import test from "node:test";
-import { captureEvent, promptText } from "./capture.mjs";
+import {
+ captureEvent,
+ isOpenVikingPluginMessage,
+ OPENVIKING_PLUGIN_KIND,
+ pluginMessage,
+ promptText,
+} from "./capture.mjs";
const CONFIG = {
captureAssistantTurns: true,
@@ -11,6 +17,27 @@ const CONFIG = {
peerId: "workspace-a",
};
+test("emits producer-owned kinds and filters namespaced plugin injections", () => {
+ const injected = pluginMessage("recalled context", { form: "recall" });
+ assert.equal(injected.source.kind, OPENVIKING_PLUGIN_KIND);
+ assert.equal(isOpenVikingPluginMessage(injected), true);
+
+ assert.equal(captureEvent({
+ type: "user/message",
+ data: injected,
+ }, CONFIG), null);
+ for (const kind of ["plugin:time-context", "time-context"]) {
+ assert.equal(captureEvent({
+ type: "user/message",
+ data: {
+ role: "user",
+ content: [{ type: "text", text: "Time sampled while preparing turn 3" }],
+ source: { kind, form: "snapshot" },
+ },
+ }, CONFIG), null, kind);
+ }
+});
+
test("captures DSH message events without recapturing injected context", () => {
const user = captureEvent({
type: "user/message",
@@ -26,6 +53,15 @@ test("captures DSH message events without recapturing injected context", () => {
peer_id: "workspace-a",
});
+ const legacyUser = captureEvent({
+ type: "user/message",
+ data: {
+ role: "user",
+ content: [{ type: "text", text: "Legacy hosts may omit source metadata." }],
+ },
+ }, CONFIG);
+ assert.equal(legacyUser?.parts?.[0]?.text, "Legacy hosts may omit source metadata.");
+
const injected = captureEvent({
type: "user/message",
data: {
@@ -163,7 +199,20 @@ test("builds recall queries from current input while excluding its own context",
{
role: "user",
content: [{ type: "text", text: "background job completed" }],
- source: { kind: "plugin", plugin: "job-controller", form: "notice", summary: "done" },
+ source: { kind: "plugin:job-controller", form: "notice", summary: "done" },
},
]), "Current question\n\nbackground job completed");
+
+ assert.equal(promptText([
+ {
+ role: "user",
+ content: [{ type: "text", text: "Current question" }],
+ source: { kind: "user" },
+ },
+ {
+ role: "user",
+ content: [{ type: "text", text: "new recall" }],
+ source: { kind: OPENVIKING_PLUGIN_KIND },
+ },
+ ]), "Current question");
});
diff --git a/examples/dsh-memory-plugin/runtime.mjs b/examples/dsh-memory-plugin/runtime.mjs
index 892264f25..c0a8c1926 100644
--- a/examples/dsh-memory-plugin/runtime.mjs
+++ b/examples/dsh-memory-plugin/runtime.mjs
@@ -12,7 +12,7 @@ import { isRetryableFailure } from "./shared/retryable.mjs";
import { resolveEffectivePeerId } from "./shared/workspace-peer.mjs";
import {
captureEvent,
- OPENVIKING_PLUGIN_SOURCE,
+ isOpenVikingPluginMessage,
pluginMessage,
promptText,
} from "./capture.mjs";
@@ -420,7 +420,6 @@ function hasStartupProfile(agent) {
}
function isStartupProfile(message) {
- return message?.source?.kind === "plugin"
- && message.source.plugin === OPENVIKING_PLUGIN_SOURCE
+ return isOpenVikingPluginMessage(message)
&& message.source.form === "instructions";
}
diff --git a/examples/dsh-memory-plugin/runtime.test.mjs b/examples/dsh-memory-plugin/runtime.test.mjs
index 6d3726f8c..629a6c51e 100644
--- a/examples/dsh-memory-plugin/runtime.test.mjs
+++ b/examples/dsh-memory-plugin/runtime.test.mjs
@@ -6,6 +6,7 @@ import { join } from "node:path";
import { afterEach, test } from "node:test";
import { enqueue, listPending } from "./shared/pending-queue.mjs";
import { deriveWorkspacePeerId } from "./shared/workspace-peer.mjs";
+import { OPENVIKING_PLUGIN_KIND } from "./capture.mjs";
import { OpenVikingRuntime } from "./runtime.mjs";
const originalPendingDir = process.env.OPENVIKING_PENDING_DIR;
@@ -256,6 +257,7 @@ test("persisted profile delivery survives dispose and re-seed", async () => {
firstState.profileBlock = "profile v1";
const profile = await runtime.profileMessage({ session });
+ assert.equal(profile?.source?.kind, OPENVIKING_PLUGIN_KIND);
assert.equal(profile?.source?.form, "instructions");
session.events.push({ type: "user/message", data: profile });
await runtime.dispose(session);
diff --git a/examples/dsh-memory-plugin/uri-guard.test.mjs b/examples/dsh-memory-plugin/uri-guard.test.mjs
index 41494afa6..f87c74eab 100644
--- a/examples/dsh-memory-plugin/uri-guard.test.mjs
+++ b/examples/dsh-memory-plugin/uri-guard.test.mjs
@@ -1,5 +1,6 @@
import assert from "node:assert/strict";
import test from "node:test";
+import { OPENVIKING_PLUGIN_KIND } from "./capture.mjs";
import { guardVikingUri, noticeVikingUri } from "./uri-guard.mjs";
test("uri guard blocks every DSH filesystem tool that accepts paths", async () => {
@@ -130,7 +131,7 @@ test("shell commands carrying a viking URI run and get a notice", async () => {
assert.equal(decision.additionalContexts.length, 1);
const [context] = decision.additionalContexts;
assert.equal(context.role, "user");
- assert.equal(context.source.kind, "plugin");
+ assert.equal(context.source.kind, OPENVIKING_PLUGIN_KIND);
assert.equal(context.source.plugin, "openviking-memory");
assert.equal(context.source.form, "notice");
assert.match(context.source.summary, /viking:\/\/user\/default\/memories\/profile\.md/);