@@ -4,7 +4,7 @@ import type { RelayEvent } from "../relay/events";
44import type { LiveBatch , LiveListener } from "../relay/incoming" ;
55import type { RelayData , RelaySnapshot } from "../relay/service" ;
66import type { RelaySession } from "../relay/session" ;
7- import type { AgentIdentity , AgentsNative } from "./native" ;
7+ import type { AgentEventTemplate , AgentIdentity , AgentsNative } from "./native" ;
88import {
99 Agents2Service ,
1010 type AgentCommunities ,
@@ -231,8 +231,13 @@ function fakeNative(identities: AgentIdentity[] = []) {
231231 steps . push ( "forget" ) ;
232232 identities . splice ( at ( pubkey ) , 1 ) ;
233233 } ) ,
234- publish : vi . fn ( async ( pubkey : string , template : { kind : number } ) =>
235- event ( `p${ ++ published } ` , { pubkey, kind : template . kind } ) ,
234+ publish : vi . fn ( async ( pubkey : string , template : AgentEventTemplate ) =>
235+ event ( `p${ ++ published } ` , {
236+ pubkey,
237+ kind : template . kind ,
238+ content : template . content ,
239+ tags : ( template . tags ?? [ ] ) . map ( ( tag ) => [ ...tag ] ) ,
240+ } ) ,
236241 ) ,
237242 publishProfile : vi . fn ( async ( _ : string ) => { } ) ,
238243 query : vi . fn ( async ( ) => [ ] ) ,
@@ -325,6 +330,14 @@ async function setup({
325330 * microtask queue is empty, so this does not depend on how fast runners are. */
326331const settle = ( ) => new Promise ( ( resolve ) => setTimeout ( resolve , 0 ) ) ;
327332
333+ /** The events native published as an agent, of `kind`, in order. */
334+ async function published ( native : ReturnType < typeof fakeNative > , kind : number ) {
335+ const events = await Promise . all (
336+ native . publish . mock . results . map ( ( { value } ) => value ) ,
337+ ) ;
338+ return events . filter ( ( event ) => event . kind === kind ) ;
339+ }
340+
328341beforeEach ( ( ) => {
329342 vi . useRealTimers ( ) ;
330343 steps . length = 0 ;
@@ -415,6 +428,55 @@ it("delivers mentions and matching watches once, and never the agent's own event
415428 expect ( first ?. config ) . toEqual ( { reply : "ok" } ) ;
416429} ) ;
417430
431+ it ( "reacts π and π¬ to a mention until the work its run handed off settles" , async ( ) => {
432+ const { service, run, emit, native } = await setup ( ) ;
433+ await service . create ( { type : "example/echo" , name : "Echo" } ) ;
434+ let finish = ( ) => { } ;
435+ const turn = new Promise < void > ( ( resolve ) => {
436+ finish = resolve ;
437+ } ) ;
438+ run . mockImplementationOnce ( async ( { workingUntil } ) => workingUntil ( turn ) ) ;
439+ const mention = event ( "m" , {
440+ tags : [
441+ [ "h" , channel ] ,
442+ [ "p" , bot ] ,
443+ ] ,
444+ } ) ;
445+ emit ( { events : [ mention ] , channelId : channel } ) ;
446+ await vi . waitFor ( ( ) => expect ( run ) . toHaveBeenCalledTimes ( 1 ) ) ;
447+ await settle ( ) ;
448+ const reactions = await published ( native , 7 ) ;
449+ expect ( reactions . map ( ( { content, tags } ) => [ content , tags ] ) ) . toEqual (
450+ [ "π" , "π¬" ] . map ( ( emoji ) => [
451+ emoji ,
452+ [
453+ [ "h" , channel ] ,
454+ [ "e" , mention . id ] ,
455+ ] ,
456+ ] ) ,
457+ ) ;
458+ // `run` has returned, but the turn it handed off is still going.
459+ expect ( await published ( native , 5 ) ) . toEqual ( [ ] ) ;
460+ finish ( ) ;
461+ await vi . waitFor ( async ( ) =>
462+ expect ( await published ( native , 5 ) ) . toHaveLength ( 1 ) ,
463+ ) ;
464+ expect ( ( await published ( native , 5 ) ) [ 0 ] ?. tags ) . toEqual ( [
465+ [ "h" , channel ] ,
466+ ...reactions . map ( ( { id } ) => [ "e" , id ] ) ,
467+ [ "k" , "7" ] ,
468+ ] ) ;
469+ // Only mentions are reacted to, and a failed run takes its reactions back.
470+ run . mockRejectedValueOnce ( new Error ( "boom" ) ) ;
471+ emit ( { events : [ event ( "m2" , { tags : mention . tags } ) ] , channelId : channel } ) ;
472+ emit ( { events : [ event ( "w" , { content : "deploy" } ) ] , channelId : channel } ) ;
473+ await vi . waitFor ( ( ) => expect ( run ) . toHaveBeenCalledTimes ( 3 ) ) ;
474+ await vi . waitFor ( async ( ) =>
475+ expect ( await published ( native , 5 ) ) . toHaveLength ( 2 ) ,
476+ ) ;
477+ expect ( await published ( native , 7 ) ) . toHaveLength ( 4 ) ;
478+ } ) ;
479+
418480it ( "treats a reply to something the agent published as addressed" , async ( ) => {
419481 const { service, run, emit } = await setup ( ) ;
420482 await service . create ( { type : "example/echo" , name : "Echo" } ) ;
@@ -736,7 +798,7 @@ it("is not woken by reactions, deletions or DMs", async () => {
736798} ) ;
737799
738800it ( "does not let one owner's agents wake each other by replying" , async ( ) => {
739- const { service, run, emit } = await setup ( ) ;
801+ const { service, run, emit, native } = await setup ( ) ;
740802 await service . create ( { type : "example/echo" , name : "Echo" } ) ;
741803 await service . create ( { type : "example/echo" , name : "Sibling" } ) ;
742804 run . mockImplementation ( async ( { agent, trigger } ) => {
@@ -753,7 +815,7 @@ it("does not let one owner's agents wake each other by replying", async () => {
753815 await vi . waitFor ( ( ) => expect ( run ) . toHaveBeenCalledTimes ( 1 ) ) ;
754816 // The sibling replies to Echo's message, naming it, and also says "deploy",
755817 // which Echo's watch matches. Neither wakes Echo.
756- const echoMessage = "p1" . padEnd ( 64 , "0" ) ;
818+ const echoMessage = ( await published ( native , 9 ) ) [ 0 ] ?. id ;
757819 emit ( {
758820 events : [
759821 event ( "reply" , {
0 commit comments