diff --git a/plugins/openclaw/extra-message-policy/README.md b/plugins/openclaw/extra-message-policy/README.md index 205c3e2..2f0e328 100644 --- a/plugins/openclaw/extra-message-policy/README.md +++ b/plugins/openclaw/extra-message-policy/README.md @@ -320,7 +320,10 @@ transcripts or backups, or prove complete deletion from those systems. `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 +model dispatch. The final `before_agent_run` defense targets only explicit +`agentIds`; unrelated agents do not need a guild/cache at that late hook. Positively +identified native DM contexts stay outside guild scopes; missing guild alone does +not identify a DM. 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 diff --git a/plugins/openclaw/extra-message-policy/channel-scope.js b/plugins/openclaw/extra-message-policy/channel-scope.js index 22b86e7..a692f47 100644 --- a/plugins/openclaw/extra-message-policy/channel-scope.js +++ b/plugins/openclaw/extra-message-policy/channel-scope.js @@ -27,8 +27,11 @@ export function scopeRoute(event = {}, ctx = {}) { } export function activeChannelScope(scopes = [], event = {}, ctx = {}) { const route = scopeRoute(event, ctx); + const session = value(ctx.sessionKey, ctx.SessionKey, event.sessionKey, event.SessionKey); + const knownDirect = ctx.ChatType === 'direct' || event.ChatType === 'direct' || + /^agent:[^:]+:discord:direct:[^:]+$/.test(session); return scopes.find(scope => scope.agentIds.includes(route.agentId) || - (route.accountId === scope.accountId && (!route.guildId || route.guildId === scope.guildId))); + (!knownDirect && route.accountId === scope.accountId && (!route.guildId || route.guildId === scope.guildId))); } const externalKeys = new Set([ 'ReplyToId', 'ReplyToIdFull', 'ReplyToBody', 'ReplyToSender', 'ReplyToIsQuote', 'replyToId', 'replyToIdFull', 'replyToBody', 'replyToSender', 'replyToIsQuote', diff --git a/plugins/openclaw/extra-message-policy/package.json b/plugins/openclaw/extra-message-policy/package.json index 72863e8..9637482 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.9", + "version": "0.1.10", "private": false, "type": "module", "description": "OpenClaw plugin for cross-platform message ingest, response policy, and zero-embedding raw recall.", diff --git a/plugins/openclaw/extra-message-policy/plugin-runtime.js b/plugins/openclaw/extra-message-policy/plugin-runtime.js index 2090a8b..44bbc3b 100644 --- a/plugins/openclaw/extra-message-policy/plugin-runtime.js +++ b/plugins/openclaw/extra-message-policy/plugin-runtime.js @@ -973,6 +973,9 @@ export function registerExtraMessagePolicy(api, options = {}) { }, { priority: 1000 }); api.on("before_agent_run", async (event, ctx) => { + // Final-model hooks do not guarantee guild metadata or an earlier route cache. + // Only explicitly isolated agents carry this final fail-closed defense. + if (!cfg.discordChannelScopes.some(scope => scope.agentIds.includes(scopeRoute(event, ctx).agentId))) return; const admission = await checkScope(event, ctx); if (admission.protected && !admission.allowed) return { outcome: "block", reason: "Discord channel scope could not be verified", diff --git a/plugins/openclaw/extra-message-policy/test/channel-scope.test.js b/plugins/openclaw/extra-message-policy/test/channel-scope.test.js index 3d5cfe1..0b731b7 100644 --- a/plugins/openclaw/extra-message-policy/test/channel-scope.test.js +++ b/plugins/openclaw/extra-message-policy/test/channel-scope.test.js @@ -34,3 +34,9 @@ test('invalid mandatory config fails startup rather than silently dropping the s test('later opt-out excludes even previously admitted raw records',()=>{ assert.equal(admittedRecallRecord({...base,channelScopeAdmission:1,senderId:'400'},scopes,{agentId:'isolated'},{public:['400']}),false); }); +test('only positively identified direct contexts bypass guild admission',()=>{ + assert.equal(admitChannelScope(scopes,{accountId:'public',content:'DM'}, {ChatType:'direct'}).protected,false); + assert.equal(admitChannelScope(scopes,{accountId:'public',content:'DM'}, {sessionKey:'agent:legacy:discord:direct:400'}).protected,false); + assert.equal(admitChannelScope(scopes,{accountId:'public',content:'ambiguous',isGroup:false},{}).allowed,false); + assert.equal(admitChannelScope(scopes,{accountId:'public',content:'ambiguous'},{agentId:'isolated',ChatType:'direct'}).allowed,false); +}); 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 847ff2d..24e640b 100644 --- a/plugins/openclaw/extra-message-policy/test/plugin-runtime.test.js +++ b/plugins/openclaw/extra-message-policy/test/plugin-runtime.test.js @@ -2464,3 +2464,12 @@ test('native provider contexts retain numeric sink provenance and support later 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'); }); +test('uncached unrelated agent final hooks pass with SecretRef while isolated agents fail closed',async()=>{ + const accountId='public',channelId='222222222222222222'; + const harness=await createHarness({discordChannelScopes:[{accountId,guildId:'111111111111111111',channelIds:[channelId],agentIds:['isolated']}]},{channels:{discord:{accounts:{public:{token:{source:'env',provider:'default',id:'TEST_TOKEN_REFERENCE'}}}}}}); + const event={accountId,channelId,prompt:'current request'}; + assert.equal(await harness.emit('before_agent_run',event,{accountId,channelId,agentId:'lab',sessionKey:`agent:lab:discord:channel:${channelId}`}),undefined); + assert.equal((await harness.emit('before_agent_run',event,{accountId,channelId,agentId:'isolated',sessionKey:`agent:isolated:discord:channel:${channelId}`})).outcome,'block'); + const outside={accountId,guildId:'111111111111111111',channelId:'333333333333333333',content:'/help'}; + assert.deepEqual(await harness.emit('inbound_claim',outside,{accountId,channelId:outside.channelId}),{handled:true}); +});