Skip to content

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

fix(dsh): prevent duplicate profile injection after re-seed (#4231)

Oneflyb02f60252651
diff --git a/examples/dsh-memory-plugin/runtime.mjs b/examples/dsh-memory-plugin/runtime.mjs
index 1061466ec..e6ef7036a 100644
--- a/examples/dsh-memory-plugin/runtime.mjs
+++ b/examples/dsh-memory-plugin/runtime.mjs
@@ -109,6 +109,10 @@ export class OpenVikingRuntime {
async profileMessage(agent) {
const state = await this.initialize(agent);
if (!state.ready || !state.profileBlock || state.profileDelivered) return null;
+ if (hasStartupProfile(agent)) {
+ state.profileDelivered = true;
+ return null;
+ }
state.profileDelivered = true;
return pluginMessage(state.profileBlock, "instructions");
}
@@ -314,3 +318,21 @@ function pluginMessage(content, form) {
},
});
}
+
+function hasStartupProfile(agent) {
+ const session = agent.session;
+ const ownEvents = (session?.events || []).slice(session?.header?.seedLength ?? 0);
+ const inHistory = ownEvents.some(event => (
+ event?.type === "user/message" && isStartupProfile(event.data)
+ ));
+ if (inHistory) return true;
+ return [agent.inbox?.nextTurn, agent.inbox?.nextStep].some(messages => (
+ (messages || []).some(isStartupProfile)
+ ));
+}
+
+function isStartupProfile(message) {
+ return message?.source?.kind === "plugin"
+ && message.source.plugin === OPENVIKING_PLUGIN_SOURCE
+ && message.source.form === "instructions";
+}
diff --git a/examples/dsh-memory-plugin/runtime.test.mjs b/examples/dsh-memory-plugin/runtime.test.mjs
index bbd9e5f78..2439a0c18 100644
--- a/examples/dsh-memory-plugin/runtime.test.mjs
+++ b/examples/dsh-memory-plugin/runtime.test.mjs
@@ -206,6 +206,64 @@ test("dispose waits for the final commit before deleting session state", async (
assert.equal(runtime.states.has(session.id), false);
});
+test("persisted profile delivery survives dispose and re-seed", async () => {
+ const runtime = new OpenVikingRuntime({
+ async commitSession() {
+ return { ok: true };
+ },
+ }, config(), { debug() {} });
+ const session = {
+ id: "profile-resume",
+ header: { cwd: "/workspace" },
+ events: [],
+ };
+ const firstState = runtime.stateFor(session);
+ firstState.ready = true;
+ firstState.profileBlock = "profile v1";
+
+ const profile = await runtime.profileMessage({ session });
+ assert.equal(profile?.source?.form, "instructions");
+ session.events.push({ type: "user/message", data: profile });
+ await runtime.dispose(session);
+
+ const resumedSession = {
+ id: session.id,
+ header: session.header,
+ events: [...session.events],
+ };
+ const resumedState = runtime.stateFor(resumedSession);
+ resumedState.ready = true;
+ resumedState.profileBlock = "profile v2";
+ assert.equal(await runtime.profileMessage({ session: resumedSession }), null);
+ assert.equal(resumedState.profileDelivered, true);
+
+ const pendingSession = {
+ id: "profile-pending",
+ header: { cwd: "/workspace" },
+ events: [],
+ };
+ const pendingState = runtime.stateFor(pendingSession);
+ pendingState.ready = true;
+ pendingState.profileBlock = "pending profile";
+ assert.equal(await runtime.profileMessage({
+ session: pendingSession,
+ inbox: { nextTurn: [], nextStep: [profile] },
+ }), null);
+
+ const otherSession = {
+ id: "profile-other",
+ header: { cwd: "/workspace", seedLength: 1 },
+ events: [{ type: "user/message", data: profile }],
+ };
+ const otherState = runtime.stateFor(otherSession);
+ otherState.ready = true;
+ otherState.profileBlock = "other profile";
+ assert.equal(
+ (await runtime.profileMessage({ session: otherSession }))?.source?.form,
+ "instructions",
+ );
+});
+
test("disposeAll drains every live session", async () => {
const committed = [];
const runtime = new OpenVikingRuntime({