@@ -255,6 +255,11 @@ function connector(
255255 maxConcurrentRuns = 4 ,
256256 runTimeoutMs = 1_800_000 ,
257257 stopTimeoutMs = 5_000 ,
258+ policy : {
259+ allowedUserIds ?: string [ ] ;
260+ allowedChannelIds ?: string [ ] ;
261+ signal ?: AbortSignal ;
262+ } = { } ,
258263) {
259264 return new HermesClickClackConnector ( {
260265 clickclack : client ,
@@ -265,6 +270,9 @@ function connector(
265270 maxConcurrentRuns,
266271 runTimeoutMs,
267272 stopTimeoutMs,
273+ allowedUserIds : new Set ( policy . allowedUserIds ?? [ "usr_human" ] ) ,
274+ allowedChannelIds : new Set ( policy . allowedChannelIds ?? [ "chn_1" ] ) ,
275+ signal : policy . signal ,
268276 logger : {
269277 info : ( ) => { } ,
270278 warn : ( ) => { } ,
@@ -273,6 +281,63 @@ function connector(
273281 } ) ;
274282}
275283
284+ test ( "rejects a DM from a sender outside the explicit allowlist" , async ( ) => {
285+ const client = new FakeClickClack ( ) ;
286+ const hermes = new FakeHermes ( ) ;
287+ const current = message ( "msg_denied_dm" , "run a tool" , {
288+ channel_id : undefined ,
289+ direct_conversation_id : "dm_1" ,
290+ channel_seq : 1 ,
291+ } ) ;
292+ client . byId . set ( current . id , current ) ;
293+ client . dmHistory . set ( "dm_1" , [ current ] ) ;
294+
295+ await connector ( client , hermes , [ ] , 4 , 1_800_000 , 5_000 , {
296+ allowedUserIds : [ "usr_someone_else" ] ,
297+ } ) . handleEvent ( eventFor ( current ) ) ;
298+
299+ assert . equal ( hermes . starts . length , 0 ) ;
300+ assert . equal ( client . dmSends . length , 0 ) ;
301+ } ) ;
302+
303+ test ( "rejects a channel mention outside the explicit channel allowlist" , async ( ) => {
304+ const client = new FakeClickClack ( ) ;
305+ const hermes = new FakeHermes ( ) ;
306+ const current = message ( "msg_denied_channel" , "@hermes run a tool" ) ;
307+ client . byId . set ( current . id , current ) ;
308+
309+ await connector ( client , hermes , [ ] , 4 , 1_800_000 , 5_000 , {
310+ allowedChannelIds : [ "chn_somewhere_else" ] ,
311+ } ) . handleEvent ( eventFor ( current ) ) ;
312+
313+ assert . equal ( hermes . starts . length , 0 ) ;
314+ assert . equal ( client . threadSends . length , 0 ) ;
315+ } ) ;
316+
317+ test ( "rejects malformed dual-target and cross-workspace messages" , async ( ) => {
318+ const client = new FakeClickClack ( ) ;
319+ const hermes = new FakeHermes ( ) ;
320+ const dualTarget = message ( "msg_dual_target" , "hello" , {
321+ channel_id : "chn_1" ,
322+ direct_conversation_id : "dm_1" ,
323+ } ) ;
324+ const wrongWorkspace = message ( "msg_wrong_workspace" , "hello" , {
325+ channel_id : undefined ,
326+ direct_conversation_id : "dm_1" ,
327+ workspace_id : "wsp_other" ,
328+ } ) ;
329+ client . byId . set ( dualTarget . id , dualTarget ) ;
330+ client . byId . set ( wrongWorkspace . id , wrongWorkspace ) ;
331+
332+ const instance = connector ( client , hermes ) ;
333+ await instance . handleEvent ( eventFor ( dualTarget ) ) ;
334+ await instance . handleEvent ( eventFor ( wrongWorkspace ) ) ;
335+
336+ assert . equal ( hermes . starts . length , 0 ) ;
337+ assert . equal ( client . dmSends . length , 0 ) ;
338+ assert . equal ( client . threadSends . length , 0 ) ;
339+ } ) ;
340+
276341test ( "ignores self-authored, bot-authored, activity, and unmentioned channel messages" , async ( ) => {
277342 const client = new FakeClickClack ( ) ;
278343 const hermes = new FakeHermes ( ) ;
@@ -587,6 +652,75 @@ test("retries transient final reply delivery with one deterministic nonce", asyn
587652 assert . equal ( lifecycle . line . status , "completed" ) ;
588653} ) ;
589654
655+ test ( "rejects completion when final reply delivery exhausts retries" , async ( ) => {
656+ const client = new FakeClickClack ( ) ;
657+ const hermes = new FakeHermes ( ) ;
658+ const current = message ( "msg_reply_exhausted" , "hello" , {
659+ channel_id : undefined ,
660+ direct_conversation_id : "dm_1" ,
661+ channel_seq : 1 ,
662+ } ) ;
663+ client . byId . set ( current . id , current ) ;
664+ client . dmHistory . set ( "dm_1" , [ current ] ) ;
665+ client . dmFailuresRemaining = 3 ;
666+
667+ const scheduled = await connector ( client , hermes ) . scheduleEvent ( eventFor ( current ) ) ;
668+
669+ await assert . rejects ( scheduled . completion , / r e p l y d e l i v e r y e x h a u s t e d r e t r i e s / i) ;
670+ assert . equal ( client . dmSends . length , 3 ) ;
671+ } ) ;
672+
673+ test ( "rejects completion when generic failure delivery exhausts retries" , async ( ) => {
674+ const client = new FakeClickClack ( ) ;
675+ const hermes = new FakeHermes ( ) ;
676+ const current = message ( "msg_failure_exhausted" , "hello" , {
677+ channel_id : undefined ,
678+ direct_conversation_id : "dm_1" ,
679+ channel_seq : 1 ,
680+ } ) ;
681+ client . byId . set ( current . id , current ) ;
682+ client . dmHistory . set ( "dm_1" , [ current ] ) ;
683+ client . dmFailuresRemaining = 3 ;
684+ hermes . startError = new Error ( "provider failed" ) ;
685+
686+ const scheduled = await connector ( client , hermes ) . scheduleEvent ( eventFor ( current ) ) ;
687+
688+ await assert . rejects ( scheduled . completion , / r e p l y d e l i v e r y e x h a u s t e d r e t r i e s / i) ;
689+ assert . equal ( client . dmSends . length , 3 ) ;
690+ } ) ;
691+
692+ test ( "rejects active and queued completions interrupted by shutdown" , async ( ) => {
693+ const client = new FakeClickClack ( ) ;
694+ const hermes = new TimeoutHermes ( ) ;
695+ const abort = new AbortController ( ) ;
696+ const first = message ( "msg_abort_1" , "first" , {
697+ channel_id : undefined ,
698+ direct_conversation_id : "dm_1" ,
699+ channel_seq : 1 ,
700+ } ) ;
701+ const second = message ( "msg_abort_2" , "second" , {
702+ channel_id : undefined ,
703+ direct_conversation_id : "dm_1" ,
704+ channel_seq : 2 ,
705+ } ) ;
706+ client . byId . set ( first . id , first ) ;
707+ client . byId . set ( second . id , second ) ;
708+ client . dmHistory . set ( "dm_1" , [ first , second ] ) ;
709+ const instance = connector ( client , hermes , [ ] , 4 , 1_800_000 , 5_000 , {
710+ signal : abort . signal ,
711+ } ) ;
712+
713+ const firstTask = await instance . scheduleEvent ( eventFor ( first ) ) ;
714+ const secondTask = await instance . scheduleEvent ( eventFor ( second ) ) ;
715+ await waitForCondition ( ( ) => hermes . starts . length === 1 ) ;
716+ abort . abort ( new Error ( "connector shutdown" ) ) ;
717+
718+ await assert . rejects ( firstTask . completion , / c o n n e c t o r s h u t d o w n / i) ;
719+ await assert . rejects ( secondTask . completion , / c o n n e c t o r s h u t d o w n / i) ;
720+ assert . equal ( hermes . starts . length , 1 ) ;
721+ assert . equal ( client . dmSends . length , 0 ) ;
722+ } ) ;
723+
590724test ( "times out a stalled run, stops it, and reports a generic failure" , async ( ) => {
591725 const client = new FakeClickClack ( ) ;
592726 const hermes = new TimeoutHermes ( ) ;
0 commit comments