@@ -12,6 +12,7 @@ import type { OpenClawConfig } from "../config/types.openclaw.js";
1212import type { PluginOrigin } from "./plugin-origin.types.js" ;
1313import { markPluginRegistryActive , markPluginRegistryRetired } from "./registry-lifecycle.js" ;
1414import { createPluginRegistry } from "./registry.js" ;
15+ import { createPluginRuntime } from "./runtime/index.js" ;
1516import type { PluginRuntime } from "./runtime/types.js" ;
1617import { createPluginRecord } from "./status.test-fixtures.js" ;
1718
@@ -134,6 +135,91 @@ function inspect(context: object) {
134135}
135136
136137describe ( "bundled channel ingress runtime ownership" , ( ) => {
138+ it ( "binds authenticated owner turns to the exact live trusted channel plugin" , async ( ) => {
139+ const runtime = createPluginRuntime ( ) ;
140+ const command = vi . fn ( async ( ) => ( { payloads : [ ] } ) ) ;
141+ Object . defineProperty ( runtime . agent , "runCommandFromIngress" , {
142+ configurable : true ,
143+ value : command ,
144+ } ) ;
145+ const registryBuilder = createPluginRegistry ( {
146+ logger : { info ( ) { } , warn ( ) { } , error ( ) { } , debug ( ) { } } ,
147+ runtime,
148+ activateGlobalSideEffects : false ,
149+ } ) ;
150+ const owner = createPluginRecord ( { id : "discord" , origin : "bundled" } ) ;
151+ const foreign = createPluginRecord ( { id : "foreign" , origin : "bundled" } ) ;
152+ const untrusted = createPluginRecord ( { id : "impostor" , origin : "workspace" } ) ;
153+ const ownerApi = registryBuilder . createApi ( owner , { config : { } as OpenClawConfig } ) ;
154+ const foreignApi = registryBuilder . createApi ( foreign , { config : { } as OpenClawConfig } ) ;
155+ const untrustedApi = registryBuilder . createApi ( untrusted , { config : { } as OpenClawConfig } ) ;
156+ registryBuilder . registry . plugins . push ( owner , foreign , untrusted ) ;
157+ registryBuilder . registry . channels . push (
158+ {
159+ pluginId : "discord" ,
160+ plugin : { id : "discord" } ,
161+ source : owner . source ,
162+ } as never ,
163+ {
164+ pluginId : "impostor" ,
165+ plugin : { id : "community" } ,
166+ source : untrusted . source ,
167+ } as never ,
168+ ) ;
169+ markPluginRegistryActive ( registryBuilder . registry ) ;
170+ const options = {
171+ message : "owner turn" ,
172+ messageChannel : "discord" as const ,
173+ senderIsOwner : true ,
174+ allowModelOverride : false ,
175+ } ;
176+ const commandRuntime = { log : vi . fn ( ) , error : vi . fn ( ) } as never ;
177+ const retained = ownerApi . runtime . agent . runCommandFromIngress ;
178+
179+ await expect ( retained ( options , commandRuntime ) ) . resolves . toEqual ( { payloads : [ ] } ) ;
180+ expect ( command ) . toHaveBeenCalledWith ( options , commandRuntime ) ;
181+ await expect (
182+ foreignApi . runtime . agent . runCommandFromIngress ( options , commandRuntime ) ,
183+ ) . rejects . toThrow ( 'Plugin "foreign" cannot admit authenticated owner authority' ) ;
184+ const guestOptions = { ...options , messageChannel : "community" , senderIsOwner : false } ;
185+ const retainedGuest = untrustedApi . runtime . agent . runCommandFromIngress ;
186+ await expect ( retainedGuest ( guestOptions , commandRuntime ) ) . resolves . toEqual ( { payloads : [ ] } ) ;
187+ expect ( command ) . toHaveBeenLastCalledWith ( guestOptions , commandRuntime ) ;
188+ let ownerClaimReads = 0 ;
189+ let channelReads = 0 ;
190+ const changingGuestOptions = {
191+ ...guestOptions ,
192+ get senderIsOwner ( ) {
193+ return ownerClaimReads ++ > 0 ;
194+ } ,
195+ get messageChannel ( ) {
196+ return channelReads ++ === 0 ? "community" : "discord" ;
197+ } ,
198+ } ;
199+ await expect ( retainedGuest ( changingGuestOptions , commandRuntime ) ) . resolves . toEqual ( {
200+ payloads : [ ] ,
201+ } ) ;
202+ expect ( command ) . toHaveBeenLastCalledWith (
203+ expect . objectContaining ( { messageChannel : "community" , senderIsOwner : false } ) ,
204+ commandRuntime ,
205+ ) ;
206+ expect ( ownerClaimReads ) . toBe ( 1 ) ;
207+ expect ( channelReads ) . toBe ( 1 ) ;
208+ await expect (
209+ retainedGuest ( { ...guestOptions , senderIsOwner : true } , commandRuntime ) ,
210+ ) . rejects . toThrow ( 'Plugin "impostor" cannot admit authenticated owner authority' ) ;
211+
212+ registryBuilder . rollbackPluginGlobalSideEffects ( owner . id , owner ) ;
213+ await expect ( retained ( options , commandRuntime ) ) . rejects . toThrow (
214+ 'Plugin "discord" cannot admit authenticated owner authority' ,
215+ ) ;
216+ registryBuilder . rollbackPluginGlobalSideEffects ( untrusted . id , untrusted ) ;
217+ await expect ( retainedGuest ( guestOptions , commandRuntime ) ) . rejects . toThrow (
218+ 'Plugin "impostor" cannot admit authenticated owner authority' ,
219+ ) ;
220+ expect ( command ) . toHaveBeenCalledTimes ( 3 ) ;
221+ } ) ;
222+
137223 it ( "defers and preserves the exact active runtime across an inactive prepared load" , async ( ) => {
138224 let channelReads = 0 ;
139225 const channel = { inbound : { buildContext : buildChannelInboundEventContext } } ;
0 commit comments