Skip to content

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

fix(claude-plugin): replay pending writes on Stop (#6394)

t0sakid7b32ac5b413
diff --git a/examples/claude-code-memory-plugin/.claude-plugin/plugin.json b/examples/claude-code-memory-plugin/.claude-plugin/plugin.json
index 71ef00641..bcf61fa9a 100644
--- a/examples/claude-code-memory-plugin/.claude-plugin/plugin.json
+++ b/examples/claude-code-memory-plugin/.claude-plugin/plugin.json
@@ -1,6 +1,6 @@
{
"name": "openviking-memory",
- "version": "0.7.16",
+ "version": "0.7.17",
"description": "Long-term semantic memory for Claude Code, powered by OpenViking. Auto-recall relevant memories at session start and capture important information during conversations.",
"author": {
"name": "OpenViking",
diff --git a/examples/claude-code-memory-plugin/package.json b/examples/claude-code-memory-plugin/package.json
index 3d8b22125..2557ab684 100644
--- a/examples/claude-code-memory-plugin/package.json
+++ b/examples/claude-code-memory-plugin/package.json
@@ -1,6 +1,6 @@
{
"name": "claude-code-openviking-memory",
- "version": "0.7.16",
+ "version": "0.7.17",
"description": "OpenViking memory plugin for Claude Code — semantic long-term memory",
"type": "module",
"scripts": {
diff --git a/examples/claude-code-memory-plugin/scripts/auto-capture.mjs b/examples/claude-code-memory-plugin/scripts/auto-capture.mjs
index 7bb81b504..8be33ec00 100644
--- a/examples/claude-code-memory-plugin/scripts/auto-capture.mjs
+++ b/examples/claude-code-memory-plugin/scripts/auto-capture.mjs
@@ -34,6 +34,7 @@ import {
makeFetchJSON,
} from "./lib/ov-session.mjs";
import { maybeDetach, readHookStdin } from "./lib/async-writer.mjs";
+import { replayPending } from "./lib/pending-queue.mjs";
import { readJsonState, writeJsonState } from "./lib/state.mjs";
import { getEffectivePeerId } from "./lib/workspace-peer.mjs";
import { runHookStage } from "./shared/agent-hook-runtime.mjs";
@@ -317,6 +318,13 @@ async function main({ cfg, input, cwd }) {
const health = await fetchJSON("/health");
let result;
if (health.ok) {
+ // SessionStart replays at most one capped batch, so drain more of the
+ // queue here; older entries go out before this turn's messages.
+ try {
+ await replayPending(fetchJSON, log, { consumeRetries: false });
+ } catch (err) {
+ logError("pending-replay", err);
+ }
result = await pushTurnsToOv(ovSessionId, captureTurns, effectivePeer.peerId, cfg);
} else if (isRetryableFailure(health)) {
logError("health_check", "server unreachable or unhealthy; enqueuing capture");
diff --git a/examples/claude-code-memory-plugin/scripts/auto-capture.test.mjs b/examples/claude-code-memory-plugin/scripts/auto-capture.test.mjs
index df43e8ff0..24fee44cc 100644
--- a/examples/claude-code-memory-plugin/scripts/auto-capture.test.mjs
+++ b/examples/claude-code-memory-plugin/scripts/auto-capture.test.mjs
@@ -6,8 +6,9 @@ import { dirname, join } from "node:path";
import test from "node:test";
import { fileURLToPath } from "node:url";
-import { readRequestBody, withMockOpenViking, writeJson } from "../../memory-plugin-shared/testing/support.mjs";
+import { readRequestBody, withMockOpenViking, withPendingDir, writeJson } from "../../memory-plugin-shared/testing/support.mjs";
import { extractCaptureTurns } from "./cc-transcript.mjs";
+import { enqueue, listPending } from "./lib/pending-queue.mjs";
const SCRIPT_DIR = dirname(fileURLToPath(import.meta.url));
@@ -127,6 +128,62 @@ test("failed non-retryable capture keeps the cursor for a later retry", async ()
}
});
+test("Stop replays queued writes before sending the new turns", async () => {
+ const root = await mkdtemp(join(tmpdir(), "ov-cc-capture-replay-"));
+ const transcriptPath = join(root, "transcript.jsonl");
+ const requests = [];
+
+ try {
+ await writeTranscript(transcriptPath);
+ await withPendingDir(async (pendingDir) => {
+ for (let i = 0; i < 20; i++) {
+ await enqueue("addMessage", "cc-old-session", {
+ role: "assistant",
+ parts: [{ type: "text", text: `old ${i}` }],
+ }, { createdAt: 1000 + i });
+ }
+
+ await withMockOpenViking(async (req, res) => {
+ const url = new URL(req.url, "http://127.0.0.1");
+ if (req.method === "GET" && url.pathname === "/health") {
+ writeJson(res, { status: "ok", result: { healthy: true } });
+ return;
+ }
+ if (req.method === "POST" && url.pathname.endsWith("/messages/batch")) {
+ const body = await readRequestBody(req);
+ requests.push({ path: url.pathname, count: body.messages.length });
+ writeJson(res, { status: "ok", result: { added: body.messages.length } });
+ return;
+ }
+ if (req.method === "POST" && url.pathname.endsWith("/messages")) {
+ const body = await readRequestBody(req);
+ requests.push({ path: url.pathname, text: body.parts[0].text });
+ writeJson(res, { status: "ok", result: {} });
+ return;
+ }
+ writeJson(res, { status: "ok", result: {} });
+ }, async (baseUrl) => {
+ await runAutoCapture(
+ { session_id: "capture-replay", transcript_path: transcriptPath, cwd: root },
+ { ...hookEnv(root, baseUrl), OPENVIKING_PENDING_DIR: pendingDir },
+ );
+ });
+
+ assert.deepEqual(await listPending(), []);
+ });
+
+ assert.deepEqual(requests, [
+ ...Array.from({ length: 20 }, (_, i) => ({
+ path: "/api/v1/sessions/cc-old-session/messages",
+ text: `old ${i}`,
+ })),
+ { path: "/api/v1/sessions/cc-capture-replay/messages/batch", count: 2 },
+ ]);
+ } finally {
+ await rm(root, { recursive: true, force: true });
+ }
+});
+
test("threshold commit archives every captured message", async () => {
const root = await mkdtemp(join(tmpdir(), "ov-cc-capture-commit-"));
const transcriptPath = join(root, "transcript.jsonl");