Skip to content

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

fix(opencode-plugin): discard captured message parts from state snapshots (#5179)

dvd233c63785b60a56
diff --git a/examples/opencode-plugin/lib/memory-session.mjs b/examples/opencode-plugin/lib/memory-session.mjs
index 8286af734..1dff5f55a 100644
--- a/examples/opencode-plugin/lib/memory-session.mjs
+++ b/examples/opencode-plugin/lib/memory-session.mjs
@@ -126,7 +126,9 @@ export function createMemorySessionManager({ config, pluginRoot }) {
{
role: message.role,
captured: message.captured,
- parts: Array.from(message.parts.entries()),
+ // Captured messages are never read by flushPendingMessages again; retain
+ // only their metadata so completed payloads cannot grow the state file.
+ parts: message.captured ? [] : Array.from(message.parts.entries()),
},
])),
}
diff --git a/examples/opencode-plugin/tests/memory-session.test.mjs b/examples/opencode-plugin/tests/memory-session.test.mjs
index e33bf14bf..8b22ec28b 100644
--- a/examples/opencode-plugin/tests/memory-session.test.mjs
+++ b/examples/opencode-plugin/tests/memory-session.test.mjs
@@ -340,6 +340,75 @@ test("OpenCode tool parts preserve completed and error state fields", async () =
})
})
+test("captured message parts are not retained in the session state file", async () => {
+ await withCaptureServer(async ({ endpoint }) => {
+ await withTempDir("ov-oc-session-", async (dir) => {
+ const manager = createMemorySessionManager({ config: baseConfig(endpoint), pluginRoot: dir })
+
+ await manager.init()
+ await manager.handleEvent({ type: "session.created", properties: { info: { id: "oc-state-retention" } } })
+ await manager.handleEvent({
+ type: "message.updated",
+ properties: {
+ info: {
+ id: "msg-captured",
+ sessionID: "oc-state-retention",
+ role: "user",
+ },
+ },
+ })
+ await manager.handleEvent({
+ type: "message.part.updated",
+ properties: {
+ part: {
+ id: "part-captured",
+ messageID: "msg-captured",
+ sessionID: "oc-state-retention",
+ type: "text",
+ text: "This payload was already sent and is not needed for a future flush.".repeat(20),
+ },
+ },
+ })
+
+ await manager.handleEvent({ type: "session.idle", sessionID: "oc-state-retention" })
+ await manager.flushAll({ commit: false })
+
+ await manager.handleEvent({
+ type: "message.updated",
+ properties: {
+ info: {
+ id: "msg-pending",
+ sessionID: "oc-state-retention",
+ role: "user",
+ },
+ },
+ })
+ await manager.handleEvent({
+ type: "message.part.updated",
+ properties: {
+ part: {
+ id: "part-pending",
+ messageID: "msg-pending",
+ sessionID: "oc-state-retention",
+ type: "text",
+ text: "This payload is still pending and must remain recoverable.",
+ },
+ },
+ })
+ await new Promise((resolve) => setTimeout(resolve, 350))
+
+ const state = JSON.parse(await fs.promises.readFile(join(dir, "openviking-session-state.json"), "utf8"))
+ const persistedSession = Object.values(state.sessions)[0]
+ const persistedMessages = new Map(persistedSession.messages)
+ const capturedMessage = persistedMessages.get("msg-captured")
+ const pendingMessage = persistedMessages.get("msg-pending")
+ assert.equal(capturedMessage.captured, true)
+ assert.deepEqual(capturedMessage.parts, [])
+ assert.equal(pendingMessage.parts.length, 1)
+ })
+ })
+})
+
test("concurrent saves never race the shared state file (#3877)", async (t) => {
// Widen the race window: concurrent saveState() calls share the same
// `${statePath}.tmp` temp file. A slow writeFile keeps the shared .tmp