Skip to content

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

33.6 KB
Raw Blame
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"
import { createMemorySessionManager } from "../lib/memory-session.mjs"
import { contextMessageEvents } from "../lib/v2-events.mjs"
import { initLogger } from "../lib/utils.mjs"

async function withTempDir(prefix, fn) {
  const dir = await mkdtemp(join(tmpdir(), prefix))
  try {
    return await fn(dir)
  } finally {
    await rm(dir, { recursive: true, force: true })
  }
}

async function withCaptureServer(fn) {
  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 if (req.url?.startsWith("/api/v1/sessions/") && req.url.endsWith("/messages/batch")) {
      res.end(JSON.stringify({ status: "ok", result: { accepted: true } }))
    } else if (req.url?.startsWith("/api/v1/sessions/")) {
      res.end(JSON.stringify({ status: "ok", result: { pending_tokens: 0 } }))
    } else {
      res.statusCode = 404
      res.end(JSON.stringify({ status: "error", error: { message: "not found" } }))
    }
  })

  await new Promise((resolve) => server.listen(0, "127.0.0.1", resolve))
  try {
    const { port } = server.address()
    return await fn({ endpoint: `http://127.0.0.1:${port}`, requests })
  } finally {
    await new Promise((resolve) => server.close(resolve))
  }
}

function baseConfig(endpoint) {
  return {
    endpoint,
    apiKey: "",
    account: "",
    user: "",
    peerId: "",
    timeoutMs: 5000,
    autoCapture: true,
    captureAssistantTurns: true,
    captureToolMaxChars: 2000,
    captureMode: "semantic",
    captureMaxLength: 24000,
    commitTokenThreshold: 20000,
    commitKeepRecentCount: 10,
  }
}

test("deferred startup loads local state without waiting for legacy migration", async () => {
  await withTempDir("ov-oc-deferred-", async (dir) => {
    let releaseCommit
    let markCommitStarted
    const commitStarted = new Promise((resolve) => { markCommitStarted = resolve })
    const commitGate = new Promise((resolve) => { releaseCommit = resolve })
    const server = createServer(async (req, res) => {
      if (req.url?.endsWith("/commit")) {
        markCommitStarted()
        await commitGate
      }
      res.setHeader("Content-Type", "application/json")
      res.end(JSON.stringify({ status: "ok", result: {} }))
    })
    await new Promise((resolve) => server.listen(0, "127.0.0.1", resolve))
    const legacyPath = join(dir, "openviking-session-map.json")
    fs.writeFileSync(legacyPath, JSON.stringify({ sessions: { old: { ovSessionId: "legacy" } } }))
    const manager = createMemorySessionManager({ config: baseConfig(`http://127.0.0.1:${server.address().port}`), pluginRoot: dir })
    let initialized = false
    const init = manager.init({ deferNetwork: true }).then(() => { initialized = true })
    try {
      await commitStarted
      assert.equal(initialized, true, "activation must finish while legacy commit is still pending")
      assert.equal(fs.existsSync(`${legacyPath}.migrated`), false)
    } finally {
      releaseCommit()
      await init
      await manager.waitForBackground()
      await new Promise((resolve) => server.close(resolve))
    }
    assert.equal(fs.existsSync(`${legacyPath}.migrated`), true)
  })
})

test("autoCapture=false prevents OpenCode messages from being captured", async () => {
  await withCaptureServer(async ({ endpoint, requests }) => {
    await withTempDir("ov-oc-session-", async (dir) => {
      const manager = createMemorySessionManager({
        config: { ...baseConfig(endpoint), autoCapture: false },
        pluginRoot: dir,
      })

      await manager.init()
      await manager.handleEvent({ type: "session.created", properties: { info: { id: "oc-session-disabled" } } })
      await manager.handleEvent({
        type: "message.updated",
        properties: {
          info: {
            id: "msg-user-disabled",
            sessionID: "oc-session-disabled",
            role: "user",
          },
        },
      })
      await manager.handleEvent({
        type: "message.part.updated",
        properties: {
          part: {
            id: "part-user-disabled",
            messageID: "msg-user-disabled",
            sessionID: "oc-session-disabled",
            type: "text",
            text: "This message must not be captured.",
          },
        },
      })

      await manager.handleEvent({ type: "session.idle", sessionID: "oc-session-disabled" })
      await manager.flushAll({ commit: false })

      assert.equal(
        requests.some((request) => request.url?.endsWith("/messages/batch")),
        false,
        "disabled automatic capture must never POST session messages",
      )
    })
  })
})

test("autoCapture=false skips lifecycle commits but preserves explicit commits", async () => {
  await withCaptureServer(async ({ endpoint, requests }) => {
    await withTempDir("ov-oc-session-", async (dir) => {
      const manager = createMemorySessionManager({
        config: { ...baseConfig(endpoint), autoCapture: false },
        pluginRoot: dir,
      })

      await manager.init()
      await manager.handleEvent({ type: "session.created", properties: { info: { id: "oc-session-disabled" } } })
      await manager.handleEvent({ type: "session.compacted", sessionID: "oc-session-disabled" })
      await manager.flushAll({ commit: true })
      await manager.commitSession("oc-explicit-manual")

      const commitUrls = requests
        .filter((request) => request.method === "POST" && request.url?.endsWith("/commit"))
        .map((request) => request.url)
      assert.deepEqual(commitUrls, ["/api/v1/sessions/oc-explicit-manual/commit"])
    })
  })
})

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) => {
      const manager = createMemorySessionManager({ config: baseConfig(endpoint), pluginRoot: dir })

      await manager.init()
      await manager.handleEvent({
        type: "message.updated",
        properties: {
          info: {
            id: "msg-first",
            sessionID: "oc-child",
            parentID: "msg-parent",
            role: "user",
          },
        },
      })
      await manager.handleEvent({
        type: "message.part.updated",
        properties: {
          part: {
            id: "part-first",
            messageID: "msg-first",
            sessionID: "oc-child",
            type: "text",
            text: "A message event must not derive a session id from the parent message id.",
          },
        },
      })

      await manager.handleEvent({ type: "session.idle", sessionID: "oc-child" })

      const addMessage = requests.find((request) => request.url?.endsWith("/messages/batch"))
      assert.ok(addMessage, "message.updated should flush the captured message")
      assert.equal(addMessage.url, "/api/v1/sessions/oc-oc-child/messages/batch")
      await manager.flushAll({ commit: false })
    })
  })
})

test("session.idle event flushes pending OpenCode capture", async () => {
  await withCaptureServer(async ({ endpoint, requests }) => {
    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-session-1" } } })
      await manager.handleEvent({
        type: "message.updated",
        properties: {
          info: {
            id: "msg-user-1",
            sessionID: "oc-session-1",
            role: "user",
          },
        },
      })
      await manager.handleEvent({
        type: "message.part.updated",
        properties: {
          part: {
            id: "part-user-1",
            messageID: "msg-user-1",
            sessionID: "oc-session-1",
            type: "text",
            text: "Remember that OpenCode idle events must flush captures.",
          },
        },
      })

      await manager.handleEvent({ type: "session.idle", sessionID: "oc-session-1" })

      const addMessage = requests.find((request) => request.url === "/api/v1/sessions/oc-oc-session-1/messages/batch")
      assert.ok(addMessage, "session.idle should POST pending messages")
      const body = JSON.parse(addMessage.body)
      assert.equal(body.messages[0].role, "user")
      assert.match(body.messages[0].content, /idle events must flush captures/)
      await manager.flushAll({ commit: false })
    })
  })
})

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) => {
      const manager = createMemorySessionManager({ config: baseConfig(endpoint), pluginRoot: dir })

      await manager.init()
      await manager.handleEvent({
        type: "message.updated",
        properties: {
          info: {
            id: "msg-assistant-1",
            sessionID: "oc-session-2",
            role: "assistant",
            finish: "length",
          },
        },
      })
      await manager.handleEvent({
        type: "message.part.updated",
        properties: {
          part: {
            id: "part-assistant-1",
            messageID: "msg-assistant-1",
            sessionID: "oc-session-2",
            type: "text",
            text: "Partial assistant output still belongs in capture.",
          },
        },
      })

      await manager.handleEvent({ type: "session.idle", properties: { sessionID: "oc-session-2" } })

      const addMessage = requests.find((request) => request.url === "/api/v1/sessions/oc-oc-session-2/messages/batch")
      assert.ok(addMessage, "session.idle should capture non-stop assistant messages")
      const body = JSON.parse(addMessage.body)
      assert.equal(body.messages[0].role, "assistant")
      assert.match(body.messages[0].content, /Partial assistant output/)
      await manager.flushAll({ commit: false })
    })
  })
})

test("OpenCode tool parts preserve completed and error state fields", async () => {
  await withCaptureServer(async ({ endpoint, requests }) => {
    await withTempDir("ov-oc-session-", async (dir) => {
      const manager = createMemorySessionManager({ config: baseConfig(endpoint), pluginRoot: dir })
      const sessionID = "oc-tool-parts"
      const uri = "viking://user/test/memories/experiences/exchange.md"

      await manager.init()
      for (const [messageID, part] of [
        [
          "msg-tool-completed",
          {
            id: "part-tool-completed",
            sessionID,
            messageID: "msg-tool-completed",
            type: "tool",
            callID: "call-tool-completed",
            tool: "openviking_read",
            state: {
              status: "completed",
              input: { uris: uri },
              output: "experience contents",
            },
          },
        ],
        [
          "msg-tool-error",
          {
            id: "part-tool-error",
            sessionID,
            messageID: "msg-tool-error",
            type: "tool",
            callID: "call-tool-error",
            tool: "openviking_read",
            state: {
              status: "error",
              input: { uris: uri },
              error: "OpenViking request failed",
            },
          },
        ],
      ]) {
        await manager.handleEvent({
          type: "message.updated",
          properties: { info: { id: messageID, sessionID, role: "assistant" } },
        })
        await manager.handleEvent({
          type: "message.part.updated",
          properties: { part },
        })
      }

      await manager.handleEvent({ type: "session.idle", properties: { sessionID } })

      const addMessage = requests.find((request) => request.url === "/api/v1/sessions/oc-oc-tool-parts/messages/batch")
      assert.ok(addMessage, "session.idle should capture OpenCode tool parts")
      const body = JSON.parse(addMessage.body)
      assert.deepEqual(body.messages, [
        {
          role: "assistant",
          parts: [
            {
              type: "tool",
              tool_id: "call-tool-completed",
              tool_name: "openviking_read",
              tool_status: "completed",
              tool_input: { uris: uri },
              tool_output: "experience contents",
            },
          ],
        },
        {
          role: "assistant",
          parts: [
            {
              type: "tool",
              tool_id: "call-tool-error",
              tool_name: "openviking_read",
              tool_status: "error",
              tool_input: { uris: uri },
              tool_output: "OpenViking request failed",
            },
          ],
        },
      ])
      await manager.flushAll({ commit: false })
    })
  })
})

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("startup prunes stale sessions with nothing pending and keeps the rest", async () => {
  await withTempDir("ov-oc-state-prune-", async (dir) => {
    const statePath = join(dir, "openviking-session-state.json")
    const stale = Date.now() - 30 * 24 * 60 * 60 * 1000
    const recent = Date.now() - 60 * 1000
    const session = (lastActivityAt, messages) => ({
      ovSessionId: "oc-x",
      createdAt: lastActivityAt,
      lastActivityAt,
      messages,
    })
    const captured = ["msg-done", { role: "user", captured: true, parts: [] }]
    const pending = ["msg-pending", { role: "user", captured: false, parts: [["part-1", { type: "text", text: "unsent" }]] }]
    fs.writeFileSync(statePath, JSON.stringify({
      version: 2,
      sessions: {
        "stale-done": session(stale, [captured]),
        "stale-empty": session(stale, []),
        "stale-pending": session(stale, [captured, pending]),
        "recent-done": session(recent, [captured]),
      },
    }))

    const manager = createMemorySessionManager({
      config: { ...baseConfig("http://127.0.0.1:1"), autoCapture: false },
      pluginRoot: dir,
    })
    await manager.init({ deferNetwork: true })

    const data = JSON.parse(await fs.promises.readFile(statePath, "utf8"))
    await manager.waitForBackground()
    assert.deepEqual(Object.keys(data.sessions).sort(), ["recent-done", "stale-pending"])
    assert.equal(new Map(data.sessions["stale-pending"].messages).get("msg-pending").parts.length, 1)
  })
})

test("OpenCode capture filters sanitized text before sending", async () => {
  await withCaptureServer(async ({ endpoint, requests }) => {
    await withTempDir("ov-oc-filters-", async (dir) => {
      const manager = createMemorySessionManager({
        config: { ...baseConfig(endpoint), captureFilters: ["k/approved/", "s/secret/[redacted]/g"] },
        pluginRoot: dir,
      })
      await manager.init()
      for (const [id, text] of [
        ["removed", "<openviking-context>approved</openviking-context>Remember secret details."],
        ["kept", "Remember approved secret details."],
      ]) {
        for (const event of contextMessageEvents("oc-filters", { id, type: "user", text })) {
          await manager.handleEvent(event)
        }
      }
      await manager.handleEvent({ type: "session.idle", sessionID: "oc-filters" })
      const sent = requests.find((request) => request.url === "/api/v1/sessions/oc-oc-filters/messages/batch")
      assert.ok(sent)
      assert.deepEqual(JSON.parse(sent.body).messages, [
        { role: "user", content: "Remember approved [redacted] details." },
      ])
      await manager.flushAll({ commit: false })
    })
  })
})

test("OpenCode caps mixed-message text and omits low-signal text", async () => {
  await withCaptureServer(async ({ endpoint, requests }) => {
    await withTempDir("ov-oc-mixed-", async (dir) => {
      const manager = createMemorySessionManager({ config: baseConfig(endpoint), pluginRoot: dir })
      await manager.init()
      for (const [id, text] of [["long", "x".repeat(40000)], ["ack", "ok"]]) {
        const message = {
          id, type: "assistant", content: [
            { type: "text", text },
            { type: "tool", id: `tool-${id}`, name: "lookup", state: {
              status: "completed", input: { id }, content: [{ type: "text", text: "result" }],
            } },
          ],
        }
        for (const event of contextMessageEvents("oc-mixed", message)) await manager.handleEvent(event)
      }
      await manager.handleEvent({ type: "session.idle", sessionID: "oc-mixed" })
      const sent = requests.find((request) => request.url === "/api/v1/sessions/oc-oc-mixed/messages/batch")
      assert.ok(sent)
      const messages = JSON.parse(sent.body).messages
      assert.equal(messages.length, 2)
      assert.equal(messages[0].parts[0].type, "text")
      assert.ok(messages[0].parts[0].text.length <= 24000)
      assert.match(messages[0].parts[0].text, /\[truncated\]$/)
      assert.equal(messages[0].parts[1].type, "tool")
      assert.deepEqual(messages[1].parts.map((part) => part.type), ["tool"])
      await manager.flushAll({ commit: false })
    })
  })
})

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)
  })
})

test("explicit commit writes the response trace_id to the plugin log", 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")
    res.end(JSON.stringify({
      status: "ok",
      result: {
        status: "accepted",
        task_id: "task-opencode-trace",
        trace_id: "trace-opencode-commit",
      },
    }))
  })
  await new Promise((resolve) => server.listen(0, "127.0.0.1", resolve))
  try {
    await withTempDir("ov-oc-session-trace-", async (dir) => {
      const { port } = server.address()
      initLogger(dir)
      const manager = createMemorySessionManager({
        config: { ...baseConfig(`http://127.0.0.1:${port}`), autoCapture: false },
        pluginRoot: dir,
      })

      const result = await manager.commitSession("oc-explicit-trace")
      assert.equal(result.traceId, "trace-opencode-commit")
      assert.equal(requests[0].url, "/api/v1/sessions/oc-explicit-trace/commit")
      assert.deepEqual(JSON.parse(requests[0].body), { keep_recent_count: 0 })
      const raw = await fs.promises.readFile(join(dir, "openviking-memory.log"), "utf8")
      assert.match(raw, /"trace_id":"trace-opencode-commit"/)
    })
  } finally {
    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))
  }
})