From 680450b5131a581044a2fe4fbecd0f7f81e455ff Mon Sep 17 00:00:00 2001 From: wzr <2668940489@qq.com> Date: Sat, 4 Apr 2026 09:01:22 +0800 Subject: [PATCH 1/2] fix: reset stale state on plugin re-load to handle double-load by OpenClaw Gateway Closes #1210 When OpenClaw Gateway starts, the openviking plugin is loaded twice with different cacheKey. Module-level Maps (localClientCache, localClientPendingPromises) persist across re-imports, causing the second register() call to see stale state. This patch adds the plugin file with an idempotency guard that detects duplicate registration and clears cached state before proceeding. --- examples/openclaw-plugin/index.ts | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/examples/openclaw-plugin/index.ts b/examples/openclaw-plugin/index.ts index 2bf71daf2..d30a42987 100644 --- a/examples/openclaw-plugin/index.ts +++ b/examples/openclaw-plugin/index.ts @@ -262,6 +262,8 @@ function totalCommitMemories(r: CommitSessionResult): number { return Object.values(m).reduce((sum, n) => sum + (n ?? 0), 0); } +let _registered = false; + const contextEnginePlugin = { id: "openviking", name: "Context Engine (OpenViking)", @@ -270,6 +272,17 @@ const contextEnginePlugin = { configSchema: memoryOpenVikingConfigSchema, register(api: OpenClawPluginApi) { + // Idempotency guard: when OpenClaw reloads the plugin with a different cacheKey, + // module-level state (localClientCache, localClientPendingPromises) persists from + // the previous load. Reset stale state so the second registration works cleanly. + // See: https://github.com/volcengine/OpenViking/issues/1210 + if (_registered) { + api.logger.info("openviking: plugin re-loaded, resetting stale state"); + localClientCache.clear(); + localClientPendingPromises.clear(); + } + _registered = true; + const rawCfg = api.pluginConfig && typeof api.pluginConfig === "object" && !Array.isArray(api.pluginConfig) ? (api.pluginConfig as Record) @@ -1360,4 +1373,4 @@ export async function buildMemoryLinesWithBudget( return { lines, estimatedTokens: totalTokens }; } -export default contextEnginePlugin; +export default contextEnginePlugin; \ No newline at end of file From 0cfb5821565970c2347686b7509c0858fda772d8 Mon Sep 17 00:00:00 2001 From: r266-tech Date: Sun, 5 Apr 2026 04:31:39 +0800 Subject: [PATCH 2/2] fix: add missing client parameters in defensive re-spawn path The defensive re-spawn path was creating OpenVikingClient without tenantAccount, tenantUser, and routingDebugLog parameters. This mismatch could cause incorrect agent routing. --- examples/openclaw-plugin/index.ts | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/examples/openclaw-plugin/index.ts b/examples/openclaw-plugin/index.ts index d30a42987..45ceb83ef 100644 --- a/examples/openclaw-plugin/index.ts +++ b/examples/openclaw-plugin/index.ts @@ -1228,7 +1228,15 @@ const contextEnginePlugin = { }); try { await waitForHealthOrExit(baseUrl, timeoutMs, intervalMs, child); - const client = new OpenVikingClient(baseUrl, cfg.apiKey, cfg.agentId, cfg.timeoutMs); + const client = new OpenVikingClient( + baseUrl, + cfg.apiKey, + cfg.agentId, + cfg.timeoutMs, + tenantAccount, + tenantUser, + routingDebugLog, + ); localClientCache.set(localCacheKey, { client, process: child }); if (resolveLocalClient) { resolveLocalClient(client);