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(opencode-plugin): serialize overlapping flushes of one session (#5674)
t0saki9b9ac101f1c4
diff --git a/examples/opencode-plugin/lib/memory-session.mjs b/examples/opencode-plugin/lib/memory-session.mjsindex 0014cc257..f025ed5dc 100644--- a/examples/opencode-plugin/lib/memory-session.mjs+++ b/examples/opencode-plugin/lib/memory-session.mjs@@ -296,14 +296,20 @@ export function createMemorySessionManager({ config, pluginRoot }) {const state = sessions.get(opencodeSessionId)if (!state) return false- const added = await flushPendingMessages(opencodeSessionId, state)- if (commit && isCaptureEnabled(config)) {- await commitOvSession(state.ovSessionId, { force: true, reason })- } else if (added > 0) {- await maybeCommitByThreshold(state)- }- await enqueueSave()- return true+ // Overlapping flushes of one session (e.g. session.idle and dispose) must+ // 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)) {+ await commitOvSession(state.ovSessionId, { force: true, reason })+ } else if (added > 0) {+ await maybeCommitByThreshold(state)+ }+ await enqueueSave()+ return true+ })+ state.flushChain = run.catch(() => {})+ return run}async function commitSession(sessionId, opencodeSessionId, abortSignal) {diff --git a/examples/opencode-plugin/tests/memory-session.test.mjs b/examples/opencode-plugin/tests/memory-session.test.mjsindex 414ec5dfc..c784a8ade 100644--- a/examples/opencode-plugin/tests/memory-session.test.mjs+++ b/examples/opencode-plugin/tests/memory-session.test.mjs@@ -209,6 +209,51 @@ test("session.idle event flushes pending OpenCode capture", async () => {})})+test("overlapping session.idle and dispose flushAll send each message once and commit after the batch", async () => {+ await withCaptureServer(async ({ endpoint, requests }) => {+ await withTempDir("ov-oc-session-", async (dir) => {+ const manager = createMemorySessionManager({ config: baseConfig(endpoint), pluginRoot: dir })+ const sessionID = "oc-session-race"++ await manager.init()+ await manager.handleEvent({ type: "session.created", properties: { info: { id: sessionID } } })+ for (const [index, role] of ["user", "assistant"].entries()) {+ await manager.handleEvent({+ type: "message.updated",+ properties: { info: { id: `msg-race-${index}`, sessionID, role, finish: "stop" } },+ })+ await manager.handleEvent({+ type: "message.part.updated",+ properties: {+ part: {+ id: `part-race-${index}`,+ messageID: `msg-race-${index}`,+ sessionID,+ type: "text",+ text: `Race message ${index} must be stored exactly once.`,+ },+ },+ })+ }+ requests.length = 0++ await Promise.all([+ manager.handleEvent({ type: "session.idle", sessionID }),+ manager.flushAll({ commit: true }),+ ])++ const urls = requests.map((request) => `${request.method} ${request.url}`)+ const batchIndexes = urls.flatMap((url, index) => (url.endsWith("/messages/batch") ? [index] : []))+ assert.equal(batchIndexes.length, 1, `expected one batch, got: ${urls.join(", ")}`)+ const body = JSON.parse(requests[batchIndexes[0]].body)+ assert.equal(body.messages.length, 2)+ const commitIndex = urls.findIndex((url) => url.endsWith("/commit"))+ assert.ok(commitIndex > batchIndexes[0], `commit must follow the batch: ${urls.join(", ")}`)+ await manager.flushAll({ commit: false })+ })+ })+})+test("assistant messages are captured even when finish is not stop", async () => {await withCaptureServer(async ({ endpoint, requests }) => {await withTempDir("ov-oc-session-", async (dir) => {