diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml new file mode 100644 index 0000000..67be98f --- /dev/null +++ b/.github/workflows/test.yml @@ -0,0 +1,19 @@ +name: Test +on: + pull_request: + push: + branches: [main] +jobs: + test: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + - uses: actions/setup-node@v4 + with: + node-version: '22' + - uses: actions/setup-python@v5 + with: + python-version: '3.12' + - run: python -m pip install PyYAML==6.0.1 + - run: npm run build + - run: npm test diff --git a/plugins/openclaw/extra-message-policy/README.md b/plugins/openclaw/extra-message-policy/README.md index bdad178..205c3e2 100644 --- a/plugins/openclaw/extra-message-policy/README.md +++ b/plugins/openclaw/extra-message-policy/README.md @@ -314,3 +314,27 @@ It rechecks every file hash, takes an exclusive lock and atomically rewrites changed JSONL files. A fresh manifest is required if ingestion changed any file. This utility does not schedule retention, encrypt storage, process other transcripts or backups, or prove complete deletion from those systems. + + +### Mandatory Discord channel scopes + +`discordChannelScopes` contains `{accountId, guildId, channelIds, agentIds?}` entries. +It is an admission boundary before mutable policy rules, commands, raw sinks and +model dispatch. Exact roots and native parent metadata for their threads are +admitted. Missing provenance fails closed for protected agents/accounts. Keep +`includeThreadStarter:false`, `historyLimit:0` and thread parent-session inheritance +disabled in native configuration. Automatic quote/starter/forward contexts without +source-channel provenance reject the entire turn conservatively, including reply +attachments. Media provenance is also flattened, so protected media turns (including +ordinary attachments) are conservatively rejected. Native Discord preparation can +already have fetched supplemental media onto the host before these hooks; this +boundary protects plugin sinks and model dispatch, not that earlier transport fetch. +Plain user-written text cannot establish transport provenance. + +Deploy protected guilds onto a dedicated agent, session store and controlled +workspace. Disable indexed memory/wiki, filesystem, execution and delegation tools; +permit only `search_raw_context` for archive access. Both automatic recall and that +tool enforce mandatory admission independently of caller search filters. Protected +recall accepts only new records stamped after scope admission; historical mixed +records are preserved but excluded. This does not delete historical data, provide +complete sender opt-out, or enforce restrictions on unrelated agents/guilds. diff --git a/plugins/openclaw/extra-message-policy/channel-scope.js b/plugins/openclaw/extra-message-policy/channel-scope.js new file mode 100644 index 0000000..22b86e7 --- /dev/null +++ b/plugins/openclaw/extra-message-policy/channel-scope.js @@ -0,0 +1,73 @@ +// Mandatory admission is independent of mutable response/ingestion policy rules. +const value = (...items) => items.find(x => typeof x === 'string' && x.trim())?.trim() || ''; +const bare = x => x.replace(/^(?:channel|chat):/, ''); +const snowflake = (...items) => items.map(x => bare(String(x || ''))).find(x => /^\d+$/.test(x)) || ''; +export function normalizeChannelScopes(raw) { + if (raw === undefined) return []; + if (!Array.isArray(raw)) throw new Error('discordChannelScopes must be an array'); + return raw.map(scope => { + if (!scope || !scope.accountId || !scope.guildId || !Array.isArray(scope.channelIds) || !scope.channelIds.length || + !scope.channelIds.every(id => typeof id === 'string' && /^\d+$/.test(id)) || + (scope.agentIds !== undefined && (!Array.isArray(scope.agentIds) || !scope.agentIds.every(id => typeof id === 'string' && id)))) { + throw new Error('Invalid mandatory Discord channel scope'); + } + return { accountId: String(scope.accountId), guildId: String(scope.guildId), channelIds: [...new Set(scope.channelIds)], agentIds: scope.agentIds || [] }; + }); +} +export function scopeRoute(event = {}, ctx = {}) { + const meta = { ...event.metadata, ...ctx.metadata }; + const session = value(ctx.sessionKey, ctx.SessionKey, event.sessionKey, event.SessionKey); + return { + accountId: value(ctx.accountId, ctx.AccountId, event.accountId, event.AccountId, meta.accountId), + guildId: value(ctx.rawGuildId, ctx.GroupSpace, ctx.guildId, event.rawGuildId, event.guildId, meta.rawGuildId, meta.guildId, meta.guild_id), + channelId: snowflake(ctx.NativeChannelId, ctx.ChannelId, ctx.channelId, event.channelId, event.ChannelId, meta.channelId, meta.channel_id, ctx.conversationId, event.conversationId, session.match(/:channel:([^:]+)/)?.[1]), + parentChannelId: value(ctx.parentChannelId, ctx.ParentChannelId, ctx.ThreadParentId, event.parentChannelId, meta.parentChannelId, meta.threadParentId, meta.thread_parent_id), + agentId: value(ctx.agentId, ctx.AgentId, event.agentId, session.match(/^agent:([^:]+):/)?.[1]) + }; +} +export function activeChannelScope(scopes = [], event = {}, ctx = {}) { + const route = scopeRoute(event, ctx); + return scopes.find(scope => scope.agentIds.includes(route.agentId) || + (route.accountId === scope.accountId && (!route.guildId || route.guildId === scope.guildId))); +} +const externalKeys = new Set([ + 'ReplyToId', 'ReplyToIdFull', 'ReplyToBody', 'ReplyToSender', 'ReplyToIsQuote', 'replyToId', 'replyToIdFull', 'replyToBody', 'replyToSender', 'replyToIsQuote', + 'ThreadStarterBody', 'threadStarterBody', 'starterBody', 'quote', 'quotedMessage', + 'ThreadHistoryBody', 'threadHistoryBody', 'media', 'originalMedia', 'mediaStagingPending', 'MediaPath', 'MediaPaths', 'MediaUrl', 'MediaUrls', 'MediaType', 'MediaTypes', 'referenced_message', 'referencedMessage', 'message_reference', 'messageReference', + 'message_snapshots', 'messageSnapshots', 'ForwardedFrom', 'ForwardedFromId', 'forwarded', + 'forwardedFrom', 'replyContext', 'replyTo', 'quotedAttachments' +]); +export function hasUnverifiedSupplementalContext(...objects) { + const seen = new Set(); + const visit = object => { + if (!object || typeof object !== 'object' || seen.has(object)) return false; + seen.add(object); + for (const [key, item] of Object.entries(object)) { + if (externalKeys.has(key) && item !== undefined && item !== null && item !== '' && item !== false && (!Array.isArray(item) || item.length)) return true; + if (typeof item === 'object' && visit(item)) return true; + // Discord's native forwarded envelope may already be flattened at hooks. + if (typeof item === 'string' && /\[Forwarded (?:message|content)/i.test(item)) return true; + } + return false; + }; + return objects.some(visit); +} +export function admitChannelScope(scopes, event = {}, ctx = {}) { + const scope = activeChannelScope(scopes, event, ctx); + if (!scope) return { allowed: true, protected: false }; + const route = scopeRoute(event, ctx); + const allowed = route.accountId === scope.accountId && route.guildId === scope.guildId && + (scope.channelIds.includes(route.channelId) || (route.channelId && scope.channelIds.includes(route.parentChannelId))) && + !hasUnverifiedSupplementalContext(event, ctx); + return { allowed: Boolean(allowed), protected: true, scope, route }; +} +export function admittedRecallRecord(record, scopes, ctx = {}, discordOptOut = {}) { + const route = scopeRoute({}, ctx); + const scope = (scopes || []).find(s => s.agentIds.includes(route.agentId) || (route.accountId === s.accountId && route.guildId === s.guildId)); + if (!scope) return true; // Preserve unrelated guild/agent recall behavior. + // Old mixed records may already contain flattened quotes. Only post-boundary + // records have the sink admission stamp; no migration invents provenance. + if (record?.channelScopeAdmission !== 1 || record.accountId !== scope.accountId || record.guildId !== scope.guildId) return false; + if (discordOptOut[scope.accountId]?.includes(record.senderId)) return false; + return admitChannelScope([scope], record, {}).allowed; +} diff --git a/plugins/openclaw/extra-message-policy/native-optout-guard.js b/plugins/openclaw/extra-message-policy/native-optout-guard.js new file mode 100644 index 0000000..025df49 --- /dev/null +++ b/plugins/openclaw/extra-message-policy/native-optout-guard.js @@ -0,0 +1,55 @@ +import { createHash } from "node:crypto"; + +function discordSessionRoute(sessionKey, agentId) { + if (typeof sessionKey !== "string" || typeof agentId !== "string") return; + const match = /^agent:([^:]+):discord:(direct|channel|group):([^:]+)$/.exec(sessionKey); + if (!match || match[1] !== agentId) return; + return { kind: match[2], peerId: match[3] }; +} + +function accountCandidates(value) { + return (Array.isArray(value) ? value : [value]).filter((account) => typeof account === "string" && account.length > 0); +} + +function nativeSourceTurnId(account, target, messageId) { + if (!/^[a-z0-9][a-z0-9_-]{0,63}$/i.test(account) || !target || !messageId) return; + const digest = createHash("sha256").update(JSON.stringify([ + "discord", account.trim().toLowerCase(), target, messageId + ])).digest("hex"); + return `channel-user:v1:${digest}`; +} + +export function createNativeOptOutGuard(discordOptOut, agentAccountMap) { + const optedOut = new Map(Object.entries(discordOptOut || {}).map(([account, senders]) => [account, new Set(senders)])); + return (event = {}, ctx = {}) => { + const message = event.message; + if (message?.role !== "user") return; + const route = discordSessionRoute(ctx.sessionKey, ctx.agentId); + const channel = message.sourceChannel ?? message.provenance?.sourceChannel ?? message.__openclaw?.transport?.channel; + if (channel !== undefined ? channel !== "discord" : !route) return; + const candidates = accountCandidates(agentAccountMap?.[ctx.agentId]); + if (candidates.length === 0) return; + const senderId = message.senderId ?? message.__openclaw?.senderId ?? + (route?.kind === "direct" ? route.peerId : undefined); + const sourceId = message.idempotencyKey; + const messageId = message.__openclaw?.transport?.messageId; + if (typeof sourceId === "string" && typeof messageId === "string") { + for (const account of candidates) { + const senders = optedOut.get(account); + if (!senders?.size) continue; + const target = route && route.kind !== "direct" ? `${route.kind}:${route.peerId}` : undefined; + if (target && senderId && senders.has(senderId) && + sourceId === nativeSourceTurnId(account, target, messageId)) return { block: true }; + // Main-scoped direct turns omit sender metadata. The native source ID + // still binds the bot account, direct sender, and provider message ID. + if (!route || route.kind === "direct") { + for (const sender of senders) if ((!senderId || senderId === sender) && + sourceId === nativeSourceTurnId(account, `user:${sender}`, messageId)) return { block: true }; + } + } + } + // Only a single-account agent can fall back to sender metadata. A shared + // agent needs an exact native source ID to avoid blocking the other bot. + if (candidates.length === 1 && senderId && optedOut.get(candidates[0])?.has(senderId)) return { block: true }; + }; +} diff --git a/plugins/openclaw/extra-message-policy/openclaw.plugin.json b/plugins/openclaw/extra-message-policy/openclaw.plugin.json index 0c259ad..f65187c 100644 --- a/plugins/openclaw/extra-message-policy/openclaw.plugin.json +++ b/plugins/openclaw/extra-message-policy/openclaw.plugin.json @@ -32,9 +32,28 @@ "description": "Discord sender snowflake IDs excluded per bot account from this plugin ingestion and dispatch.", "additionalProperties": { "type": "array", - "items": { "type": "string", "pattern": "^[0-9]{16,22}$" }, + "items": { + "type": "string", + "pattern": "^[0-9]{16,22}$" + }, "uniqueItems": true } + }, + "discordAgentAccounts": { + "type": "object", + "additionalProperties": { + "oneOf": [ + { + "type": "string" + }, + { + "type": "array", + "items": { + "type": "string" + } + } + ] + } } } }, @@ -291,6 +310,43 @@ "$ref": "#/definitions/runtimePolicy" } } + }, + "discordChannelScopes": { + "type": "array", + "items": { + "type": "object", + "additionalProperties": false, + "required": [ + "accountId", + "guildId", + "channelIds" + ], + "properties": { + "accountId": { + "type": "string", + "minLength": 1 + }, + "guildId": { + "type": "string", + "pattern": "^[0-9]+$" + }, + "channelIds": { + "type": "array", + "minItems": 1, + "items": { + "type": "string", + "pattern": "^[0-9]+$" + } + }, + "agentIds": { + "type": "array", + "items": { + "type": "string", + "minLength": 1 + } + } + } + } } }, "definitions": { diff --git a/plugins/openclaw/extra-message-policy/package.json b/plugins/openclaw/extra-message-policy/package.json index 6fb8dcd..72863e8 100644 --- a/plugins/openclaw/extra-message-policy/package.json +++ b/plugins/openclaw/extra-message-policy/package.json @@ -1,6 +1,6 @@ { "name": "@nestdevlab/openclaw-plugin-extra-message-policy", - "version": "0.1.7", + "version": "0.1.9", "private": false, "type": "module", "description": "OpenClaw plugin for cross-platform message ingest, response policy, and zero-embedding raw recall.", @@ -10,7 +10,7 @@ ] }, "scripts": { - "build": "node --check index.js && node --check discord-sdk-compat.js && node --check plugin-runtime.js && node --check policy.js && node --check policy-audit.js && node --check policy-command.js && node --check native-reply.js && node --check raw-recall.js && node --check retention.js && node --check retention-cli.js && node --check runtime-context.js", + "build": "node --check index.js && node --check discord-sdk-compat.js && node --check plugin-runtime.js && node --check policy.js && node --check policy-audit.js && node --check policy-command.js && node --check native-reply.js && node --check raw-recall.js && node --check retention.js && node --check retention-cli.js && node --check runtime-context.js && node --check channel-scope.js && node --check native-optout-guard.js", "test": "node --test", "test:coverage": "node --test --experimental-test-coverage", "lint": "npm run build" @@ -43,7 +43,9 @@ "README.md", "ROLLOUT.md", "examples", - "LICENSE" + "LICENSE", + "channel-scope.js", + "native-optout-guard.js" ], "keywords": [ "openclaw", diff --git a/plugins/openclaw/extra-message-policy/plugin-runtime.js b/plugins/openclaw/extra-message-policy/plugin-runtime.js index 33660d6..2090a8b 100644 --- a/plugins/openclaw/extra-message-policy/plugin-runtime.js +++ b/plugins/openclaw/extra-message-policy/plugin-runtime.js @@ -1,3 +1,5 @@ +import { admitChannelScope, activeChannelScope, scopeRoute } from "./channel-scope.js"; +import { createNativeOptOutGuard } from "./native-optout-guard.js"; import { mkdir, appendFile } from "node:fs/promises"; import path from "node:path"; import { @@ -385,7 +387,7 @@ function lookupDiscordRoute(state, event = {}, ctx = {}) { routeChannelId(event, ctx) ? `channel:${routeChannelId(event, ctx)}` : "" ].filter(Boolean)) { const route = state.discordRoutes.get(key); - if (route) return route; + if (route && (!routeAccountId(event, ctx) || !route.accountId || route.accountId === routeAccountId(event, ctx))) return route; } return null; } @@ -449,8 +451,9 @@ function rememberDedupe(state, key, windowSize) { function toRecord(source, event = {}, ctx = {}, policy = {}) { return { source, + ...(policy.channelScopeAdmission ? { channelScopeAdmission: 1, guildId: routeGuildId(event, ctx), parentChannelId: routeParentChannelId(event, ctx) } : {}), observedAt: new Date().toISOString(), - channelId: ctx.channelId ?? ctx.ChannelId ?? ctx.NativeChannelId ?? event.channelId ?? event.ChannelId ?? event.NativeChannelId ?? event.channel ?? event.from, + channelId: policy.channelScopeAdmission ? scopeRoute(event, ctx).channelId : (ctx.channelId ?? ctx.ChannelId ?? ctx.NativeChannelId ?? event.channelId ?? event.ChannelId ?? event.NativeChannelId ?? event.channel ?? event.from), accountId: ctx.accountId ?? ctx.AccountId ?? event.accountId ?? event.AccountId, conversationId: ctx.conversationId ?? ctx.OriginatingTo ?? ctx.To ?? event.conversationId ?? event.OriginatingTo ?? event.To, sessionKey: ctx.sessionKey ?? ctx.SessionKey ?? event.sessionKey ?? event.SessionKey, @@ -949,8 +952,42 @@ export function registerExtraMessagePolicy(api, options = {}) { return isDiscordRoute(event, ctx) && Boolean(accountId && senderId && cfg.privacy.discordOptOut[accountId]?.includes(senderId)); }; + api.on("before_message_write", createNativeOptOutGuard(cfg.privacy.discordOptOut, cfg.privacy.discordAgentAccounts), { priority: 1000 }); + + const checkScope = async (event = {}, ctx = {}) => { + const routed = withRememberedDiscordRoute(state, event, ctx); + if (!activeChannelScope(cfg.discordChannelScopes, routed.event, routed.ctx)) return { allowed: true, protected: false }; + const currentConfig = api.runtime?.config?.current?.() || api.config || {}; + const hydrated = await withHydratedDiscordParent(state, routed.event, routed.ctx, currentConfig, api.logger); + const admission = admitChannelScope(cfg.discordChannelScopes, hydrated.event, hydrated.ctx); + // Only admitted provenance is retained; a rejected quote must never poison a + // later turn's route cache or authorize recall/model execution. + if (admission.allowed) rememberDiscordRoute(state, hydrated.event, hydrated.ctx); + return admission; + }; + + api.on("before_tool_call", (event, ctx) => { + if (cfg.discordChannelScopes.some(scope => scope.agentIds.includes(scopeRoute({}, ctx).agentId)) && event?.toolName !== "search_raw_context") { + return { block: true, blockReason: "Tool is outside the mandatory Discord recall boundary" }; + } + }, { priority: 1000 }); + + api.on("before_agent_run", async (event, ctx) => { + const admission = await checkScope(event, ctx); + if (admission.protected && !admission.allowed) return { + outcome: "block", reason: "Discord channel scope could not be verified", + message: "Context unavailable for this request." + }; + }, { priority: 1000 }); + const resolveEffectiveDecision = async (event = {}, ctx = {}, hook = "unknown") => { const routed = withRememberedDiscordRoute(state, event, ctx); + const admission = await checkScope(routed.event, routed.ctx); + if (!admission.allowed) return { + hook, event: routed.event, ctx: routed.ctx, + policy: { respond: false, ingestMode: "none", mentionRecall: false, matched: "mandatory:discord-channel-scope" }, + mentionEvidence: {}, parentLookup: {} + }; if (isDiscordOptedOut(routed.event, routed.ctx)) { return { hook, event: routed.event, ctx: routed.ctx, @@ -1020,6 +1057,7 @@ export function registerExtraMessagePolicy(api, options = {}) { nativeStatus = null; } const policy = applyNativeMentionGatePolicy(effectivePolicy, nativeStatus, enriched.event, enriched.ctx); + if (admission.protected && admission.allowed) policy.channelScopeAdmission = 1; const mentionEvidence = describeMentionEvidence( hydrated.event, hydrated.ctx, @@ -1213,6 +1251,7 @@ export function registerExtraMessagePolicy(api, options = {}) { }); api.on("inbound_claim", async (event, ctx) => { + if (!(await checkScope(event, ctx)).allowed) return { handled: true }; // inbound_claim runs before command routing and Codex/App Server dispatch. if (isPolicyCommand(commandConfig, event, ctx) && !isDiscordOptedOut(event, ctx)) return; rememberDiscordRoute(state, event, ctx); @@ -1230,6 +1269,7 @@ export function registerExtraMessagePolicy(api, options = {}) { }, { priority: 1000 }); api.on("message_received", async (event, ctx) => { + if (!(await checkScope(event, ctx)).allowed) return; if (isPolicyCommand(commandConfig, event, ctx) && !isDiscordOptedOut(event, ctx)) return; rememberDiscordRoute(state, event, ctx); const decision = await resolveEffectiveDecision(event, ctx, "message_received"); @@ -1238,6 +1278,7 @@ export function registerExtraMessagePolicy(api, options = {}) { }); api.on("before_dispatch", async (event, ctx) => { + if (!(await checkScope(event, ctx)).allowed) return { handled: true }; if (isPolicyCommand(commandConfig, event, ctx) && !isDiscordOptedOut(event, ctx)) return; rememberDiscordRoute(state, event, ctx); const decision = await resolveEffectiveDecision(event, ctx, "before_dispatch"); @@ -1254,6 +1295,9 @@ export function registerExtraMessagePolicy(api, options = {}) { api.on("reply_dispatch", async (event) => { const dispatchCtx = event?.ctx || {}; + if (!(await checkScope(dispatchCtx, dispatchCtx)).allowed) return { + handled: true, queuedFinal: false, counts: { tool: 0, block: 0, final: 0 } + }; if (isPolicyCommand(commandConfig, dispatchPolicyEvent(dispatchCtx), dispatchPolicyContext(dispatchCtx))) return; const decision = await resolveEffectiveDecision(dispatchPolicyEvent(dispatchCtx), dispatchPolicyContext(dispatchCtx), "reply_dispatch"); if (!shouldForceReplyContext(decision.policy)) { diff --git a/plugins/openclaw/extra-message-policy/policy.js b/plugins/openclaw/extra-message-policy/policy.js index dde81e4..9589662 100644 --- a/plugins/openclaw/extra-message-policy/policy.js +++ b/plugins/openclaw/extra-message-policy/policy.js @@ -1,3 +1,4 @@ +import { normalizeChannelScopes } from "./channel-scope.js"; import { normalizeRawRecallConfig } from "./raw-recall.js"; export const INGEST_MODES = Object.freeze(["none", "passive", "all", "responseCandidates"]); @@ -95,7 +96,9 @@ export function normalizeConfig(raw = {}) { }; return { enabled: raw.enabled !== false, + discordChannelScopes: normalizeChannelScopes(raw.discordChannelScopes), privacy: { + discordAgentAccounts: raw.privacy?.discordAgentAccounts || {}, discordOptOut: Object.fromEntries( Object.entries(raw.privacy?.discordOptOut || {}) .filter(([accountId, ids]) => accountId && Array.isArray(ids)) @@ -107,7 +110,7 @@ export function normalizeConfig(raw = {}) { ? raw.policies.filter(Boolean).map((rule) => normalizePolicyRule(rule, defaultPolicy)) : [], jsonlSink, - rawRecall: normalizeRawRecallConfig(raw.rawRecall || {}, jsonlSink), + rawRecall: { ...normalizeRawRecallConfig(raw.rawRecall || {}, jsonlSink), discordChannelScopes: normalizeChannelScopes(raw.discordChannelScopes), discordOptOut: raw.privacy?.discordOptOut || {} }, httpSink: { enabled: raw.httpSink?.enabled === true, url: asString(raw.httpSink?.url), diff --git a/plugins/openclaw/extra-message-policy/raw-recall.js b/plugins/openclaw/extra-message-policy/raw-recall.js index 754ee88..031c349 100644 --- a/plugins/openclaw/extra-message-policy/raw-recall.js +++ b/plugins/openclaw/extra-message-policy/raw-recall.js @@ -1,3 +1,4 @@ +import { admittedRecallRecord } from "./channel-scope.js"; import { readdir, readFile, stat } from "node:fs/promises"; import path from "node:path"; @@ -38,7 +39,9 @@ export function normalizeRawRecallConfig(raw = {}, jsonlSink = {}) { export function buildRawRecallGuidance(cfg) { if (!cfg?.enabled || !cfg?.appendGuidance) return ""; const root = cfg.rootPath || "memory/discord/raw"; - const toolLine = cfg.tool?.enabled === false + const toolLine = cfg.discordChannelScopes?.length + ? "Use only search_raw_context for archive recall; direct file access and other memory sources are not authorized by this scope." + : cfg.tool?.enabled === false ? "If more detail is needed and file tools are available, inspect the relevant JSONL shards directly before answering." : "If more detail is needed, use the search_raw_context tool to query the archive on demand before answering."; return [ @@ -233,7 +236,7 @@ export async function searchRawContext(options, cfg, ctx = {}) { const lines = text.split("\n").filter(Boolean); for (const line of lines) { const record = parseJsonLine(line); - if (!record || !recordMatchesFilters(record, filters)) continue; + if (!record || !admittedRecallRecord(record, cfg.discordChannelScopes, ctx, cfg.discordOptOut) || !recordMatchesFilters(record, filters)) continue; const score = scoreRecord(record, terms, currentConversationId); if (score <= 0) continue; matches.push({ score, file, record }); diff --git a/plugins/openclaw/extra-message-policy/test/channel-scope.test.js b/plugins/openclaw/extra-message-policy/test/channel-scope.test.js new file mode 100644 index 0000000..3d5cfe1 --- /dev/null +++ b/plugins/openclaw/extra-message-policy/test/channel-scope.test.js @@ -0,0 +1,36 @@ +import assert from 'node:assert/strict'; +import test from 'node:test'; +import { admitChannelScope, admittedRecallRecord, normalizeChannelScopes } from '../channel-scope.js'; +const scopes = normalizeChannelScopes([{accountId:'public',guildId:'100',channelIds:['200','201'],agentIds:['isolated']}]); +const base = {accountId:'public',guildId:'100',channelId:'200',content:'local text'}; +test('mandatory roots, native thread parents, excluded channels and unknown provenance', () => { + assert.equal(admitChannelScope(scopes,base).allowed,true); + assert.equal(admitChannelScope(scopes,{...base,channelId:'201'}).allowed,true); + assert.equal(admitChannelScope(scopes,{...base,channelId:'300',parentChannelId:'201'}).allowed,true); + for (const change of [{channelId:'999'},{channelId:'300',parentChannelId:'999'},{guildId:''},{accountId:'',agentId:'isolated'}]) { + assert.equal(admitChannelScope(scopes,{...base,...change}).allowed,false,JSON.stringify(change)); + } + assert.equal(admitChannelScope(scopes,{...base,guildId:'101'}).protected,false); + assert.equal(admitChannelScope(scopes,{...base,accountId:'other'}).protected,false); +}); +test('quote IDs, forwarded text/media and thread starter/history fail closed', () => { + for (const change of [{replyToId:'1'},{replyToIdFull:'1'},{replyToIsQuote:true},{ReplyToBody:'quote'},{ThreadStarterBody:'starter'},{ThreadHistoryBody:'old'}, {content:'[Forwarded message from @x]\nsecret'}, {media:[{path:'/tmp/a'}]}, {originalMedia:[{}]}, {mediaStagingPending:true}, {MediaPaths:['/tmp/a']}]) { + assert.equal(admitChannelScope(scopes,{...base,...change}).allowed,false,JSON.stringify(change)); + } + assert.equal(admitChannelScope(scopes,{...base,media:[],originalMedia:[],mediaStagingPending:false,replyToIsQuote:false}).allowed,true); +}); +test('provider channelId cannot hide native numeric transport conversation', () => { + assert.equal(admitChannelScope(scopes,{content:'text',metadata:{guildId:'100'}},{accountId:'public',channelId:'discord',conversationId:'channel:200'}).allowed,true); +}); +test('protected recall requires post-admission stamp and record provenance', () => { + const ctx = {agentId:'isolated'}; + assert.equal(admittedRecallRecord({...base,channelScopeAdmission:1},scopes,ctx),true); + for(const record of [base,{...base,channelScopeAdmission:1,channelId:'999'},{...base,channelScopeAdmission:1,accountId:'other'},{...base,channelScopeAdmission:1,ReplyToId:'x'}]) assert.equal(admittedRecallRecord(record,scopes,ctx),false); + assert.equal(admittedRecallRecord(base,scopes,{accountId:'public',guildId:'101'}),true); +}); +test('invalid mandatory config fails startup rather than silently dropping the scope', () => { + for(const raw of [{},[{}],[{accountId:'x',guildId:'100',channelIds:[]}], [{accountId:'x',guildId:'100',channelIds:['200'],agentIds:'oops'}]]) assert.throws(()=>normalizeChannelScopes(raw)); +}); +test('later opt-out excludes even previously admitted raw records',()=>{ + assert.equal(admittedRecallRecord({...base,channelScopeAdmission:1,senderId:'400'},scopes,{agentId:'isolated'},{public:['400']}),false); +}); diff --git a/plugins/openclaw/extra-message-policy/test/native-optout-guard.test.js b/plugins/openclaw/extra-message-policy/test/native-optout-guard.test.js new file mode 100644 index 0000000..ecd2c68 --- /dev/null +++ b/plugins/openclaw/extra-message-policy/test/native-optout-guard.test.js @@ -0,0 +1,12 @@ +import test from 'node:test'; +import assert from 'node:assert/strict'; +import {createHash} from 'node:crypto'; +import {createNativeOptOutGuard} from '../native-optout-guard.js'; +test('native opt-out guard preserves exact account binding for shared agents',()=>{ + const guard=createNativeOptOutGuard({public:['400'],other:[]},{shared:['public','other']}); + const ctx={agentId:'shared',sessionKey:'agent:shared:discord:channel:200'}; + const message=account=>({role:'user',senderId:'400',idempotencyKey:'channel-user:v1:'+createHash('sha256').update(JSON.stringify(['discord',account,'channel:200','500'])).digest('hex'),__openclaw:{transport:{messageId:'500'}}}); + assert.deepEqual(guard({message:message('public')},ctx),{block:true}); + assert.equal(guard({message:message('other')},ctx),undefined); + assert.equal(guard({message:{...message('public'),role:'assistant'}},ctx),undefined); +}); diff --git a/plugins/openclaw/extra-message-policy/test/plugin-runtime.test.js b/plugins/openclaw/extra-message-policy/test/plugin-runtime.test.js index f958694..847ff2d 100644 --- a/plugins/openclaw/extra-message-policy/test/plugin-runtime.test.js +++ b/plugins/openclaw/extra-message-policy/test/plugin-runtime.test.js @@ -2411,3 +2411,56 @@ test("Discord opt-out overrides channel and runtime ingestion for its account on }); assert.equal((await readJsonl(jsonlPath)).length, 1); }); + +test('mandatory scope runs before command bypasses, sinks and model/reply dispatch', async () => { + const accountId='public', guildId='111111111111111111', rootId='222222222222222222'; + const sink = path.join(await mkdtemp(path.join(os.tmpdir(),'scope-sink-')),'messages.jsonl'); + const harness = await createHarness({ + defaultPolicy:{respond:true,ingestMode:'all'}, + discordChannelScopes:[{accountId,guildId,channelIds:[rootId],agentIds:['isolated']}], + jsonlSink:{enabled:true,path:sink} + }); + const route = {accountId,guildId,channelId:rootId,conversationId:`channel:${rootId}`,sessionKey:`agent:isolated:discord:channel:${rootId}`,agentId:'isolated'}; + const allowed = {messageId:'allow',content:'local text',metadata:{guildId}}; + assert.equal(await harness.emit('inbound_claim',allowed,route),undefined); + await harness.emit('message_received',allowed,route); + const rows = await readJsonl(sink); + assert.equal(rows.length,1);assert.equal(rows[0].channelScopeAdmission,1);assert.equal(rows[0].guildId,guildId); + for(const change of [{channelId:'333333333333333333'},{content:'/policy status',replyToId:'x'},{content:'/help',media:[{}]},{content:'[Forwarded message]\nsecret'},{ThreadStarterBody:'secret'}]) { + const event={...allowed,...change,messageId:JSON.stringify(change)}; + const ctx={...route,...('channelId' in change?{channelId:change.channelId,conversationId:`channel:${change.channelId}`}:{})}; + for(const hook of ['inbound_claim','before_dispatch']) assert.deepEqual(await harness.emit(hook,event,ctx),{handled:true},hook+JSON.stringify(change)); + await harness.emit('message_received',event,ctx); + } + assert.equal((await readJsonl(sink)).length,1); + const dispatched=await harness.emit('reply_dispatch',{ctx:{AccountId:accountId,GroupSpace:guildId,NativeChannelId:rootId,ReplyToId:'x'}}); + assert.equal(dispatched.handled,true); + assert.equal((await harness.emit('before_agent_run',{accountId,channelId:'333333333333333333'},{agentId:'isolated'})).outcome,'block'); + // Same account in a different guild remains governed by its previous rules. + assert.equal(await harness.emit('inbound_claim',{content:'normal'}, {...route,guildId:'444444444444444444',agentId:'cclab',sessionKey:'unrelated'}),undefined); +}); +test('protected agent tool hook blocks alternate memory and file paths',async()=>{ + const harness=await createHarness({discordChannelScopes:[{accountId:'public',guildId:'111111111111111111',channelIds:['222222222222222222'],agentIds:['isolated']}]}); + for(const toolName of ['read','exec','memory_search','wiki_get','sessions_send','future_unsafe_tool']) assert.equal((await harness.emit('before_tool_call',{toolName},{agentId:'isolated'})).block,true); + assert.equal(await harness.emit('before_tool_call',{toolName:'search_raw_context'},{agentId:'isolated'}),undefined); + assert.equal(await harness.emit('before_tool_call',{toolName:'read'},{agentId:'cclab'}),undefined); +}); +test('native provider contexts retain numeric sink provenance and support later model routing',async()=>{ + const accountId='public',guildId='111111111111111111',channelId='222222222222222222',threadId='333333333333333333'; + const sink=path.join(await mkdtemp(path.join(os.tmpdir(),'native-scope-')),'messages.jsonl'); + const harness=await createHarness({defaultPolicy:{respond:true,ingestMode:'all'},jsonlSink:{enabled:true,path:sink},discordChannelScopes:[{accountId,guildId,channelIds:[channelId],agentIds:['isolated']}]}); + for(const [id,parent] of [[channelId,undefined],[threadId,channelId]]) { + const sessionKey=`agent:isolated:discord:channel:${id}`; + const event={messageId:id,content:'admitted local message',metadata:{guildId,parentChannelId:parent}}; + const ctx={accountId,channelId:'discord',conversationId:`channel:${id}`,sessionKey,agentId:'isolated'}; + assert.equal(await harness.emit('inbound_claim',event,ctx),undefined); + await harness.emit('message_received',event,ctx); + assert.equal(await harness.emit('before_agent_run',{accountId,channelId:id,prompt:'local'}, {agentId:'isolated',accountId,channelId:id,sessionKey}),undefined); + } + const rows=await readJsonl(sink);assert.deepEqual(rows.map(x=>x.channelId),[channelId,threadId]); + assert.equal(rows[1].parentChannelId,channelId); + const lab={accountId,guildId:'444444444444444444',channelId:'555555555555555555',agentId:'lab',sessionKey:'agent:lab:discord:channel:555555555555555555'}; + assert.equal(await harness.emit('inbound_claim',{content:'normal'},lab),undefined); + assert.equal(await harness.emit('before_agent_run',{accountId,channelId:lab.channelId},{...lab,guildId:undefined}),undefined); + assert.equal((await harness.emit('before_agent_run',{accountId:'other',channelId},{agentId:'isolated',sessionKey:`agent:isolated:discord:channel:${channelId}`})).outcome,'block'); +}); diff --git a/plugins/openclaw/extra-message-policy/test/raw-recall.test.js b/plugins/openclaw/extra-message-policy/test/raw-recall.test.js index 9c7a571..3225ffc 100644 --- a/plugins/openclaw/extra-message-policy/test/raw-recall.test.js +++ b/plugins/openclaw/extra-message-policy/test/raw-recall.test.js @@ -189,3 +189,27 @@ test("raw recall file listing covers direct files, legacy files, walking, caps, await rm(tmp, { recursive: true, force: true }); } }); + +test('protected automatic and tool recall cannot broaden mandatory admission via search options', async () => { + const tmp = await mkdtemp(path.join(os.tmpdir(),'scoped-recall-')); + try { + const base = {accountId:'public',guildId:'100',channelId:'200',content:'Phoenix verified context',channelScopeAdmission:1}; + await writeFile(path.join(tmp,'messages.jsonl'),[ + base, {...base,channelId:'999',content:'Phoenix outside secret'}, + {...base,channelScopeAdmission:undefined,content:'Phoenix legacy mixed'}, + {...base,accountId:'other',content:'Phoenix another account'}, + {...base,channelId:'300',parentChannelId:'201',content:'Phoenix admitted post'}, + {...base,ReplyToId:'1',content:'Phoenix quote secret'} + ].map(x=>JSON.stringify(x)).join('\n')); + const cfg = {...normalizeRawRecallConfig({rootPath:tmp,searchOnTriggerOnly:false}),discordChannelScopes:[{accountId:'public',guildId:'100',channelIds:['200','201'],agentIds:['isolated']}]}; + const ctx = {agentId:'isolated'}; + const automatic = await searchRawRecall('Phoenix context',cfg,ctx); + assert.match(automatic,/verified context/);assert.match(automatic,/admitted post/); + assert.doesNotMatch(automatic,/secret|legacy|another account/); + const tool = createRawContextSearchTool(cfg,ctx); + const broadened = await tool.execute('1',{query:'Phoenix',daysBack:0,channelId:'999'}); + assert.equal(broadened.details.matches.length,0); + const normal = await tool.execute('2',{query:'Phoenix',daysBack:0}); + assert.equal(normal.details.matches.length,2); + } finally {await rm(tmp,{recursive:true,force:true});} +});