Skip to content

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

fix(opencode): skip child-session capture and commit under skipSubagentSessions (#6027) (#6032)

akushonkamene5aae34111e3
diff --git a/examples/opencode-plugin/lib/memory-session.mjs b/examples/opencode-plugin/lib/memory-session.mjs
index 05b318225..a187c9d99 100644
--- a/examples/opencode-plugin/lib/memory-session.mjs
+++ b/examples/opencode-plugin/lib/memory-session.mjs
@@ -125,6 +125,7 @@ export function createMemorySessionManager({ config, pluginRoot }) {
function serializeSessionState(state) {
return {
ovSessionId: state.ovSessionId,
+ parentId: state.parentId ?? "",
createdAt: state.createdAt,
lastActivityAt: state.lastActivityAt,
lastCommitTime: state.lastCommitTime,
@@ -151,6 +152,7 @@ export function createMemorySessionManager({ config, pluginRoot }) {
function deserializeSessionState(persisted) {
return {
ovSessionId: persisted.ovSessionId,
+ parentId: persisted.parentId ?? "",
createdAt: persisted.createdAt,
lastActivityAt: persisted.lastActivityAt,
lastCommitTime: persisted.lastCommitTime,
@@ -209,6 +211,13 @@ export function createMemorySessionManager({ config, pluginRoot }) {
opencode_session: sessionId,
openviking_session: state.ovSessionId,
})
+ if (shouldSkipSubagentSession(state)) {
+ log("INFO", "capture", "Skipping capture for subagent session", {
+ opencode_session: sessionId,
+ openviking_session: state.ovSessionId,
+ parent_session: state.parentId,
+ })
+ }
}
async function handleSessionDeleted(event) {
@@ -255,6 +264,7 @@ export function createMemorySessionManager({ config, pluginRoot }) {
if (!sessionId || !messageId) return
const state = getOrCreateSession(sessionId, event)
+ if (shouldSkipSubagentSession(state)) return
const captured = state.messages.get(messageId)
const next = captured ?? createMessageState()
if (role === "user") {
@@ -276,6 +286,7 @@ export function createMemorySessionManager({ config, pluginRoot }) {
if (!sessionId || !messageId) return
const state = getOrCreateSession(sessionId, event)
+ if (shouldSkipSubagentSession(state)) return
const message = state.messages.get(messageId) ?? createMessageState()
if (message.captured) return
const partId = part.id ?? `${messageId}:${message.parts.size}`
@@ -315,7 +326,7 @@ export function createMemorySessionManager({ config, pluginRoot }) {
// not send the same pending messages twice.
const run = (state.flushChain ?? Promise.resolve()).then(async () => {
const added = await flushPendingMessages(opencodeSessionId, state)
- if (commit && isCaptureEnabled(config)) {
+ if (commit && isCaptureEnabled(config) && !shouldSkipSubagentSession(state)) {
const result = await commitOvSession(state.ovSessionId, { force: true, reason })
// Server lost the session (reset/reinstall); drop the stale mapping.
if (result.status === "not_found" && sessions.get(opencodeSessionId) === state) {
@@ -359,6 +370,10 @@ export function createMemorySessionManager({ config, pluginRoot }) {
: deriveHarnessSessionId("oc-", opencodeSessionId)
return {
ovSessionId,
+ // Kept on the state (and persisted) so capture, commit, and the
+ // replay/recovery paths can all tell a spawned child session from a
+ // primary one — #6027.
+ parentId,
createdAt: Date.now(),
lastActivityAt: Date.now(),
lastCommitTime: undefined,
@@ -367,6 +382,10 @@ export function createMemorySessionManager({ config, pluginRoot }) {
}
}
+ function shouldSkipSubagentSession(state) {
+ return Boolean(config.skipSubagentSessions) && Boolean(state?.parentId)
+ }
+
function createMessageState() {
return {
role: "",
@@ -428,6 +447,11 @@ export function createMemorySessionManager({ config, pluginRoot }) {
async function flushPendingMessages(opencodeSessionId, state) {
if (!isCaptureEnabled(config)) return 0
+ // Recovery/replay paths (idle flush, dispose flushAll, tool commit, a
+ // state file reloaded after a restart) come through here too, so the
+ // child-session gate has to hold at this boundary — not just at the
+ // message events. See #6027.
+ if (shouldSkipSubagentSession(state)) return 0
const toSend = []
for (const [messageId, message] of state.messages.entries()) {
if (message.captured) continue
diff --git a/examples/opencode-plugin/package.json b/examples/opencode-plugin/package.json
index 0d4878a5b..7663ee3f1 100644
--- a/examples/opencode-plugin/package.json
+++ b/examples/opencode-plugin/package.json
@@ -1,6 +1,6 @@
{
"name": "@openviking/opencode-plugin",
- "version": "0.5.21",
+ "version": "0.5.22",
"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.mjs
index 1f32f296e..d24240603 100644
--- a/examples/opencode-plugin/tests/memory-session.test.mjs
+++ b/examples/opencode-plugin/tests/memory-session.test.mjs
@@ -167,6 +167,128 @@ test("autoCapture=false skips lifecycle commits but preserves explicit commits",
})
})
+test("skipSubagentSessions=true keeps child-session prompts out of capture and commit (#6027)", async () => {
+ await withCaptureServer(async ({ endpoint, requests }) => {
+ await withTempDir("ov-oc-subagent-", async (dir) => {
+ const statePath = join(dir, "openviking-session-state.json")
+ const manager = createMemorySessionManager({
+ config: { ...baseConfig(endpoint), skipSubagentSessions: true },
+ pluginRoot: dir,
+ })
+
+ await manager.init()
+
+ // The child prompt and the primary prompt carry the same text: only the
+ // session's parentID distinguishes a spawned agent task from a human one.
+ const text = "You are a subagent spawned by another session. Classify the review items."
+ await manager.handleEvent({ type: "session.created", properties: { info: { id: "oc-child-1", parentID: "oc-parent-1" } } })
+ await manager.handleEvent({
+ type: "message.updated",
+ properties: { info: { id: "msg-child-1", sessionID: "oc-child-1", role: "user" } },
+ })
+ await manager.handleEvent({
+ type: "message.part.updated",
+ properties: { part: { id: "part-child-1", messageID: "msg-child-1", sessionID: "oc-child-1", type: "text", text } },
+ })
+
+ await manager.handleEvent({ type: "session.created", properties: { info: { id: "oc-primary-1" } } })
+ await manager.handleEvent({
+ type: "message.updated",
+ properties: { info: { id: "msg-primary-1", sessionID: "oc-primary-1", role: "user" } },
+ })
+ await manager.handleEvent({
+ type: "message.part.updated",
+ properties: { part: { id: "part-primary-1", messageID: "msg-primary-1", sessionID: "oc-primary-1", type: "text", text } },
+ })
+
+ await manager.handleEvent({ type: "session.idle", sessionID: "oc-child-1" })
+ await manager.handleEvent({ type: "session.idle", sessionID: "oc-primary-1" })
+ await manager.flushAll({ commit: true })
+
+ const childUrls = requests.filter((request) => request.url?.includes("__subagent-")).map((request) => request.url)
+ assert.deepEqual(childUrls, [], "child-session capture and commit must never reach OpenViking")
+ assert.equal(
+ requests.some((request) =>
+ request.method === "POST" &&
+ request.url?.startsWith("/api/v1/sessions/oc-oc-primary-1/") &&
+ request.url?.endsWith("/messages/batch") &&
+ request.body?.includes("Classify the review items."),
+ ),
+ true,
+ "the same text in a primary session must still be captured",
+ )
+
+ // Lifecycle commit for the child session is part of the same ownership gate.
+ const persisted = JSON.parse(fs.readFileSync(statePath, "utf8"))
+ assert.equal(persisted.sessions["oc-child-1"].parentId, "oc-parent-1")
+
+ // Recovery path: the persisted parentID must survive a restart so a
+ // reloaded manager keeps skipping the child session without relying on
+ // a fresh session.created event.
+ const reloaded = createMemorySessionManager({
+ config: { ...baseConfig(endpoint), skipSubagentSessions: true },
+ pluginRoot: dir,
+ })
+ await reloaded.init()
+ await reloaded.handleEvent({
+ type: "message.updated",
+ properties: { info: { id: "msg-child-2", sessionID: "oc-child-1", role: "user" } },
+ })
+ await reloaded.handleEvent({
+ type: "message.part.updated",
+ properties: { part: { id: "part-child-2", messageID: "msg-child-2", sessionID: "oc-child-1", type: "text", text: "More child-agent output." } },
+ })
+ await reloaded.flushAll({ commit: true })
+ assert.deepEqual(
+ requests.filter((request) => request.url?.includes("__subagent-")).map((request) => request.url),
+ [],
+ "recovered child session must stay excluded from capture",
+ )
+
+ requests.splice(0, requests.length)
+ await manager.handleEvent({ type: "session.deleted", properties: { info: { id: "oc-child-1" } } })
+ assert.deepEqual(
+ requests.filter((request) => request.url?.includes("__subagent-")).map((request) => request.url),
+ [],
+ "child-session deletion must not commit the child OpenViking session",
+ )
+ })
+ })
+})
+
+test("skipSubagentSessions=false preserves child-session capture", async () => {
+ await withCaptureServer(async ({ endpoint, requests }) => {
+ await withTempDir("ov-oc-subagent-off-", async (dir) => {
+ const manager = createMemorySessionManager({
+ config: { ...baseConfig(endpoint), skipSubagentSessions: false },
+ pluginRoot: dir,
+ })
+
+ await manager.init()
+ await manager.handleEvent({ type: "session.created", properties: { info: { id: "oc-child-off", parentID: "oc-parent-off" } } })
+ await manager.handleEvent({
+ type: "message.updated",
+ properties: { info: { id: "msg-child-off", sessionID: "oc-child-off", role: "user" } },
+ })
+ await manager.handleEvent({
+ type: "message.part.updated",
+ properties: { part: { id: "part-child-off", messageID: "msg-child-off", sessionID: "oc-child-off", type: "text", text: "Child capture stays on by default." } },
+ })
+ await manager.flushAll({ commit: true })
+
+ assert.equal(
+ requests.some((request) =>
+ request.url?.includes("__subagent-") &&
+ request.url?.endsWith("/messages/batch") &&
+ request.body?.includes("Child capture stays on by default."),
+ ),
+ true,
+ "the default must keep capturing child sessions",
+ )
+ })
+ })
+})
+
test("a session first seen via message.updated keeps its own session id (#6222)", async () => {
await withCaptureServer(async ({ endpoint, requests }) => {
await withTempDir("ov-oc-session-", async (dir) => {