Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 18 additions & 4 deletions examples/opencode-plugin/lib/memory-session.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,20 @@ export function createMemorySessionManager({ config, pluginRoot }) {
const statePath = path.join(pluginRoot, "openviking-session-state.json")
const oldSessionMapPath = path.join(pluginRoot, "openviking-session-map.json")
let saveTimer = null
// Serialize saves: concurrent saveState() calls (a debounced save racing
// with flushAll / flushSession / session deletion) all share the same
// `${statePath}.tmp` temp file, so one rename can fail with ENOENT after
// another already moved it. Chaining through a promise queue keeps at most
// one write+rename in flight. Each queued save re-serializes the in-memory
// `sessions` map at execution time, so the last save always persists the
// latest state.
let saveChain = Promise.resolve()

function enqueueSave() {
const run = saveChain.then(() => saveState())
saveChain = run.catch(() => {})
return run
}

async function init() {
if (config.autoCapture) await migrateLegacySessionMap()
Expand Down Expand Up @@ -84,7 +98,7 @@ export function createMemorySessionManager({ config, pluginRoot }) {
function debouncedSaveState() {
if (saveTimer) clearTimeout(saveTimer)
saveTimer = setTimeout(() => {
saveState().catch((error) => {
enqueueSave().catch((error) => {
log("ERROR", "persistence", "Debounced save failed", { error: error?.message })
})
}, 300)
Expand Down Expand Up @@ -176,7 +190,7 @@ export function createMemorySessionManager({ config, pluginRoot }) {
if (!sessionId) return
await flushSession(sessionId, { commit: true, reason: event.type })
sessions.delete(sessionId)
await saveState()
await enqueueSave()
}

async function handleSessionError(event) {
Expand Down Expand Up @@ -253,7 +267,7 @@ export function createMemorySessionManager({ config, pluginRoot }) {
for (const sessionId of sessions.keys()) {
await flushSession(sessionId, { commit, reason: "flushAll" })
}
await saveState()
await enqueueSave()
}

async function flushSession(opencodeSessionId, { commit = false, reason = "manual" } = {}) {
Expand All @@ -267,7 +281,7 @@ export function createMemorySessionManager({ config, pluginRoot }) {
} else if (added > 0) {
await maybeCommitByThreshold(state)
}
await saveState()
await enqueueSave()
return true
}

Expand Down
71 changes: 71 additions & 0 deletions examples/opencode-plugin/tests/memory-session.test.mjs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import test from "node:test"
import assert from "node:assert/strict"
import { createServer } from "node:http"
import fs from "node:fs"
import { mkdtemp, rm } from "node:fs/promises"
import { tmpdir } from "node:os"
import { join } from "node:path"
Expand Down Expand Up @@ -213,3 +214,73 @@ test("assistant messages are captured even when finish is not stop", async () =>
})
})
})

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
// alive while a second writer arrives, and a slow rename lets a second
// rename observe the file already moved — the exact ENOENT from #3877.
const delay = (ms) => new Promise((resolve) => setTimeout(resolve, ms))
const origWriteFile = fs.promises.writeFile
const origRename = fs.promises.rename
let raceDetected = false

t.mock.method(fs.promises, "writeFile", async (...args) => {
await delay(20)
return origWriteFile.call(fs.promises, ...args)
})
t.mock.method(fs.promises, "rename", async (from, to) => {
await delay(5)
if (!fs.existsSync(from)) {
// The shared .tmp was already moved by another concurrent save.
raceDetected = true
const err = new Error(
`ENOENT: no such file or directory, rename '${from}' -> '${to}'`,
)
err.code = "ENOENT"
throw err
}
return origRename.call(fs.promises, from, to)
})

await withTempDir("ov-oc-session-", async (dir) => {
const manager = createMemorySessionManager({
// autoCapture=false keeps the test fully offline (flushSession skips
// the network); the endpoint is unreachable so health probes fail fast.
config: { ...baseConfig("http://127.0.0.1:1"), autoCapture: false },
pluginRoot: dir,
})

await manager.handleEvent({ type: "session.created", properties: { info: { id: "sess-a" } } })
await manager.handleEvent({ type: "session.created", properties: { info: { id: "sess-b" } } })
await manager.handleEvent({ type: "session.created", properties: { info: { id: "sess-c" } } })

// Fire several flushAll() calls concurrently — each one calls saveState()
// against the same shared `.tmp` file, racing the debounced saves above.
const racing = Promise.all([
manager.flushAll(),
manager.flushAll(),
manager.flushAll(),
])

// Mutate in-memory state while the racing saves are in flight: with the
// race, whichever rename runs first moves the shared .tmp away and the
// others fail with ENOENT, leaving a stale snapshot on disk. With
// serialized saves the final save is the newest snapshot.
await delay(30)
await manager.handleEvent({ type: "session.created", properties: { info: { id: "sess-d" } } })
await racing
await manager.flushAll() // final serialized save, includes sess-d

const statePath = join(dir, "openviking-session-state.json")
const data = JSON.parse(await fs.promises.readFile(statePath, "utf8"))
assert.equal(data.version, 2)
assert.deepEqual(
Object.keys(data.sessions).sort(),
["sess-a", "sess-b", "sess-c", "sess-d"],
)
// Core assertion: concurrent saves must never race each other off the
// shared `.tmp` file. With serialized saves no ENOENT can occur.
assert.equal(raceDetected, false)
})
})