Skip to content

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

fix(plugins): align OpenCode and OpenClaw contracts (#3232)

Hao Zheebe7fa2a7d28
diff --git a/examples/openclaw-plugin/openclaw.plugin.json b/examples/openclaw-plugin/openclaw.plugin.json
index b5723f853..40e3ea242 100644
--- a/examples/openclaw-plugin/openclaw.plugin.json
+++ b/examples/openclaw-plugin/openclaw.plugin.json
@@ -12,6 +12,7 @@
},
"contracts": {
"tools": [
+ "add_resource",
"add_skill",
"ov_search",
"ov_read",
diff --git a/examples/openclaw-plugin/tests/ut/manifest-contracts.test.ts b/examples/openclaw-plugin/tests/ut/manifest-contracts.test.ts
index 1b7809a22..b15c5d427 100644
--- a/examples/openclaw-plugin/tests/ut/manifest-contracts.test.ts
+++ b/examples/openclaw-plugin/tests/ut/manifest-contracts.test.ts
@@ -53,6 +53,7 @@ function collectRegisteredToolNames(): string[] {
baseUrl: "http://127.0.0.1:1933",
autoCapture: false,
autoRecall: false,
+ enableAddResourceTool: true,
},
logger: {
info: vi.fn(),
@@ -78,7 +79,7 @@ function collectRegisteredToolNames(): string[] {
}
describe("OpenClaw 5.2 manifest contracts", () => {
- it("declares every runtime tool in contracts.tools", () => {
+ it("declares every registerable runtime tool in contracts.tools", () => {
expect(manifest.contracts?.tools?.toSorted()).toEqual(collectRegisteredToolNames());
});
diff --git a/examples/opencode-plugin/lib/config.mjs b/examples/opencode-plugin/lib/config.mjs
index d253ae3d2..ebdbaa1a8 100644
--- a/examples/opencode-plugin/lib/config.mjs
+++ b/examples/opencode-plugin/lib/config.mjs
@@ -233,7 +233,14 @@ function normalizeConfig(config) {
config.captureMaxLength = Math.max(200, Math.min(100000, Math.round(Number(config.captureMaxLength) || 24000)))
config.captureToolMaxChars = Math.max(200, Math.min(20000, Math.round(Number(config.captureToolMaxChars) || 2000)))
config.commitTokenThreshold = Math.max(1000, Math.round(Number(config.commitTokenThreshold) || 20000))
- config.commitKeepRecentCount = Math.max(0, Math.round(Number(config.commitKeepRecentCount) || 10))
+ const rawCommitKeepRecentCount = config.commitKeepRecentCount
+ const commitKeepRecentCount = rawCommitKeepRecentCount == null ||
+ (typeof rawCommitKeepRecentCount === "string" && rawCommitKeepRecentCount.trim() === "")
+ ? Number.NaN
+ : Number(rawCommitKeepRecentCount)
+ config.commitKeepRecentCount = Number.isFinite(commitKeepRecentCount)
+ ? Math.max(0, Math.round(commitKeepRecentCount))
+ : DEFAULT_CONFIG.commitKeepRecentCount
config.profileTokenBudget = Math.max(500, Math.round(Number(config.profileTokenBudget) || 10000))
config.resumeContextBudget = Math.max(1024, Math.round(Number(config.resumeContextBudget) || 32000))
if (!Array.isArray(config.bypassSessionPatterns)) config.bypassSessionPatterns = []
diff --git a/examples/opencode-plugin/lib/memory-session.mjs b/examples/opencode-plugin/lib/memory-session.mjs
index 22ec44b20..6646a6896 100644
--- a/examples/opencode-plugin/lib/memory-session.mjs
+++ b/examples/opencode-plugin/lib/memory-session.mjs
@@ -29,7 +29,7 @@ export function createMemorySessionManager({ config, pluginRoot }) {
let saveTimer = null
async function init() {
- await migrateLegacySessionMap()
+ if (config.autoCapture) await migrateLegacySessionMap()
await loadState()
const health = await fetchJSON(config, "/health", {}, { timeoutMs: 5000 })
if (health.ok) {
@@ -140,9 +140,9 @@ export function createMemorySessionManager({ config, pluginRoot }) {
await handleSessionCompacted(event)
} else if (event.type === "session.idle") {
await handleSessionIdle(event)
- } else if (event.type === "message.updated") {
+ } else if (event.type === "message.updated" && config.autoCapture) {
await handleMessageUpdated(event)
- } else if (event.type === "message.part.updated") {
+ } else if (event.type === "message.part.updated" && config.autoCapture) {
await handleMessagePartUpdated(event)
}
}
@@ -259,7 +259,7 @@ export function createMemorySessionManager({ config, pluginRoot }) {
if (!state) return false
const added = await flushPendingMessages(opencodeSessionId, state)
- if (commit) {
+ if (commit && config.autoCapture) {
await commitOvSession(state.ovSessionId, { force: true, reason })
} else if (added > 0) {
await maybeCommitByThreshold(state)
@@ -361,6 +361,7 @@ export function createMemorySessionManager({ config, pluginRoot }) {
}
async function flushPendingMessages(opencodeSessionId, state) {
+ if (!config.autoCapture) return 0
const toSend = []
for (const [messageId, message] of state.messages.entries()) {
if (message.captured) continue
diff --git a/examples/opencode-plugin/lib/viking-uri-guard.mjs b/examples/opencode-plugin/lib/viking-uri-guard.mjs
index 3f428df4c..aa242faa1 100644
--- a/examples/opencode-plugin/lib/viking-uri-guard.mjs
+++ b/examples/opencode-plugin/lib/viking-uri-guard.mjs
@@ -4,7 +4,7 @@ import { buildGuardMessage, findVikingUri, normalizeToolName } from "./shared/ur
const FILESYSTEM_TOOL_HINTS = {
read: {
tool: "openviking_read",
- example: (uri) => `openviking_read(uri="${uri}")`,
+ example: (uri) => `openviking_read(uris=["${uri}"])`,
},
glob: {
tool: "openviking_glob",
diff --git a/examples/opencode-plugin/tests/config.test.mjs b/examples/opencode-plugin/tests/config.test.mjs
index 3f2bd690d..2529d53ee 100644
--- a/examples/opencode-plugin/tests/config.test.mjs
+++ b/examples/opencode-plugin/tests/config.test.mjs
@@ -141,3 +141,49 @@ test("loadConfig can disable workspace peer", async () => {
}
})
})
+
+test("loadConfig preserves an explicit zero commit keep recent count", async () => {
+ const snapshot = { ...process.env }
+ await withTempDir("ov-oc-keep-recent-zero-", async (dir) => {
+ try {
+ for (const key of Object.keys(process.env)) {
+ if (key.startsWith("OPENVIKING_")) delete process.env[key]
+ }
+ const project = join(dir, "project")
+ process.env.OPENVIKING_CREDENTIAL_SOURCE = "env"
+ process.env.OPENVIKING_URL = "https://env.example.com"
+ await mkdir(join(project, ".opencode"), { recursive: true })
+ await writeFile(join(project, ".opencode", "openviking-config.json"), JSON.stringify({
+ commitKeepRecentCount: 0,
+ }))
+
+ const cfg = loadConfig(dir, project)
+ assert.equal(cfg.commitKeepRecentCount, 0)
+ } finally {
+ restoreOpenVikingEnv(snapshot)
+ }
+ })
+})
+
+test("loadConfig defaults an invalid commit keep recent count", async () => {
+ const snapshot = { ...process.env }
+ await withTempDir("ov-oc-keep-recent-invalid-", async (dir) => {
+ try {
+ for (const key of Object.keys(process.env)) {
+ if (key.startsWith("OPENVIKING_")) delete process.env[key]
+ }
+ const project = join(dir, "project")
+ process.env.OPENVIKING_CREDENTIAL_SOURCE = "env"
+ process.env.OPENVIKING_URL = "https://env.example.com"
+ await mkdir(join(project, ".opencode"), { recursive: true })
+ await writeFile(join(project, ".opencode", "openviking-config.json"), JSON.stringify({
+ commitKeepRecentCount: null,
+ }))
+
+ const cfg = loadConfig(dir, project)
+ assert.equal(cfg.commitKeepRecentCount, 10)
+ } finally {
+ restoreOpenVikingEnv(snapshot)
+ }
+ })
+})
diff --git a/examples/opencode-plugin/tests/memory-session.test.mjs b/examples/opencode-plugin/tests/memory-session.test.mjs
index d223fdf17..679ef58da 100644
--- a/examples/opencode-plugin/tests/memory-session.test.mjs
+++ b/examples/opencode-plugin/tests/memory-session.test.mjs
@@ -53,6 +53,7 @@ function baseConfig(endpoint) {
user: "",
peerId: "",
timeoutMs: 5000,
+ autoCapture: true,
captureAssistantTurns: true,
captureToolMaxChars: 2000,
captureMode: "semantic",
@@ -62,6 +63,73 @@ function baseConfig(endpoint) {
}
}
+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("session.idle event flushes pending OpenCode capture", async () => {
await withCaptureServer(async ({ endpoint, requests }) => {
await withTempDir("ov-oc-session-", async (dir) => {
diff --git a/examples/opencode-plugin/tests/viking-uri-guard.test.mjs b/examples/opencode-plugin/tests/viking-uri-guard.test.mjs
index d76f00267..41e4cff83 100644
--- a/examples/opencode-plugin/tests/viking-uri-guard.test.mjs
+++ b/examples/opencode-plugin/tests/viking-uri-guard.test.mjs
@@ -10,7 +10,7 @@ test("viking uri guard blocks filesystem tools on virtual URIs", async () => {
await assert.rejects(
() => guard({ tool: "read" }, { args: { filePath: "viking://resources/project/file.md" } }),
- /Use openviking_read instead/,
+ /openviking_read\(uris=\["viking:\/\/resources\/project\/file\.md"\]\)/,
)
await assert.rejects(
() => guard({ tool: "glob" }, { args: { path: "viking://resources/project/" } }),