Skip to content

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

fix(opencode-plugin): drop local session mapping when the server no longer has the session (#5935)

t0sakia0c357417199
diff --git a/examples/opencode-plugin/lib/memory-session.mjs b/examples/opencode-plugin/lib/memory-session.mjs
index 9cd924cf0..69f8594c4 100644
--- a/examples/opencode-plugin/lib/memory-session.mjs
+++ b/examples/opencode-plugin/lib/memory-session.mjs
@@ -316,7 +316,11 @@ export function createMemorySessionManager({ config, pluginRoot }) {
const run = (state.flushChain ?? Promise.resolve()).then(async () => {
const added = await flushPendingMessages(opencodeSessionId, state)
if (commit && isCaptureEnabled(config)) {
- await commitOvSession(state.ovSessionId, { force: true, reason })
+ 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) {
+ sessions.delete(opencodeSessionId)
+ }
} else if (added > 0) {
await maybeCommitByThreshold(state)
}
@@ -515,6 +519,14 @@ export function createMemorySessionManager({ config, pluginRoot }) {
})
return { status: "queued" }
}
+ if (res.status === 404) {
+ log("WARN", "session", "OpenViking session not found", {
+ openviking_session: ovSessionId,
+ reason,
+ trace_id: res.traceId,
+ })
+ return { status: "not_found" }
+ }
log("ERROR", "session", "Failed to commit OpenViking session", {
openviking_session: ovSessionId,
reason,
diff --git a/examples/opencode-plugin/package.json b/examples/opencode-plugin/package.json
index 116bb81bc..f71276c08 100644
--- a/examples/opencode-plugin/package.json
+++ b/examples/opencode-plugin/package.json
@@ -1,6 +1,6 @@
{
"name": "@openviking/opencode-plugin",
- "version": "0.5.15",
+ "version": "0.5.16",
"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 294ea1323..d71a04f48 100644
--- a/examples/opencode-plugin/tests/memory-session.test.mjs
+++ b/examples/opencode-plugin/tests/memory-session.test.mjs
@@ -657,3 +657,60 @@ test("explicit commit writes the response trace_id to the plugin log", async ()
await new Promise((resolve) => server.close(resolve))
}
})
+
+test("commit of a session the server no longer has drops the stale local mapping", async () => {
+ const requests = []
+ const server = createServer(async (req, res) => {
+ let body = ""
+ req.setEncoding("utf8")
+ for await (const chunk of req) body += chunk
+ requests.push({ method: req.method, url: req.url, body })
+ res.setHeader("Content-Type", "application/json")
+ if (req.url === "/health") {
+ res.end(JSON.stringify({ status: "ok" }))
+ } else {
+ res.statusCode = 404
+ res.end(JSON.stringify({ status: "error", error: { code: "NOT_FOUND", message: "Session not found" } }))
+ }
+ })
+ await new Promise((resolve) => server.listen(0, "127.0.0.1", resolve))
+ try {
+ await withTempDir("ov-oc-session-stale-", async (dir) => {
+ const statePath = join(dir, "openviking-session-state.json")
+ // Recent activity keeps these out of the startup prune; only the server has lost them.
+ const now = Date.now()
+ const staleSession = (ovSessionId) => ({
+ ovSessionId,
+ createdAt: now,
+ lastActivityAt: now,
+ messages: [["msg-1", { role: "user", captured: true, parts: [] }]],
+ })
+ await fs.promises.writeFile(statePath, JSON.stringify({
+ version: 2,
+ sessions: {
+ "oc-stale-a": staleSession("oc-oc-stale-a"),
+ "oc-stale-b": staleSession("oc-oc-stale-b"),
+ },
+ }), "utf8")
+ const { port } = server.address()
+ const manager = createMemorySessionManager({ config: baseConfig(`http://127.0.0.1:${port}`), pluginRoot: dir })
+
+ await manager.init({ deferNetwork: true })
+ await manager.handleEvent({ type: "session.deleted", properties: { info: { id: "oc-stale-a" } } })
+ await manager.flushAll({ commit: true })
+ await manager.flushAll({ commit: true })
+ assert.deepEqual(await manager.commitSession("oc-oc-stale-c"), { status: "not_found" })
+
+ const commits = requests.filter((request) => request.url?.endsWith("/commit"))
+ assert.deepEqual(commits.map((request) => request.url), [
+ "/api/v1/sessions/oc-oc-stale-a/commit",
+ "/api/v1/sessions/oc-oc-stale-b/commit",
+ "/api/v1/sessions/oc-oc-stale-c/commit",
+ ])
+ const state = JSON.parse(await fs.promises.readFile(statePath, "utf8"))
+ assert.deepEqual(state.sessions, {})
+ })
+ } finally {
+ await new Promise((resolve) => server.close(resolve))
+ }
+})