@@ -1760,49 +1760,113 @@ describe("runPreparedReply media-only handling", () => {
17601760 } ) ;
17611761
17621762 it . each ( [
1763- [ "group" , true ] ,
1764- [ "channel" , true ] ,
1765- [ "direct" , false ] ,
1766- ] as const ) ( "persists sender attribution for %s turns only" , async ( chatType , shouldPersist ) => {
1763+ [ "group" , false ] ,
1764+ [ "channel" , false ] ,
1765+ [ "direct" , true ] ,
1766+ ] as const ) (
1767+ "persists sender attribution for %s turns from external contacts" ,
1768+ async ( chatType , requiresChannelAdmission ) => {
1769+ await runPrepared ( {
1770+ ctx : {
1771+ ...createInboundBody ( "hello" ) ,
1772+ OriginatingChannel : "telegram" ,
1773+ OriginatingTo : "chat-1" ,
1774+ ChatType : chatType ,
1775+ ...( requiresChannelAdmission ? { InboundAccessAuthorized : true } : { } ) ,
1776+ } ,
1777+ sessionCtx : {
1778+ ...createSessionBody ( "hello" ) ,
1779+ Provider : "telegram" ,
1780+ OriginatingChannel : "telegram" ,
1781+ OriginatingTo : "chat-1" ,
1782+ ChatType : chatType ,
1783+ SenderId : "user-42" ,
1784+ SenderName : "Ada" ,
1785+ SenderUsername : "ada" ,
1786+ } ,
1787+ sessionEntry : {
1788+ sessionId : "session-1" ,
1789+ updatedAt : 1 ,
1790+ chatType,
1791+ channel : "telegram" ,
1792+ } as SessionEntry ,
1793+ } ) ;
1794+
1795+ const message = requireRunReplyAgentCall ( ) . followupRun . userTurnTranscriptRecorder ?. message ;
1796+ expect ( message ) . toMatchObject ( {
1797+ __openclaw : {
1798+ senderId : "user-42" ,
1799+ senderName : "Ada" ,
1800+ senderUsername : "ada" ,
1801+ } ,
1802+ } ) ;
1803+ } ,
1804+ ) ;
1805+
1806+ it ( "does not persist sender attribution for operator-authored direct turns" , async ( ) => {
17671807 await runPrepared ( {
17681808 ctx : {
17691809 ...createInboundBody ( "hello" ) ,
17701810 OriginatingChannel : "telegram" ,
17711811 OriginatingTo : "chat-1" ,
1772- ChatType : chatType ,
1812+ ChatType : "direct" ,
1813+ InboundAccessAuthorized : true ,
1814+ SenderIsSelf : true ,
17731815 } ,
17741816 sessionCtx : {
17751817 ...createSessionBody ( "hello" ) ,
17761818 Provider : "telegram" ,
17771819 OriginatingChannel : "telegram" ,
17781820 OriginatingTo : "chat-1" ,
1779- ChatType : chatType ,
1821+ ChatType : "direct" ,
17801822 SenderId : "user-42" ,
17811823 SenderName : "Ada" ,
17821824 SenderUsername : "ada" ,
17831825 } ,
17841826 sessionEntry : {
17851827 sessionId : "session-1" ,
17861828 updatedAt : 1 ,
1787- chatType,
1829+ chatType : "direct" ,
17881830 channel : "telegram" ,
17891831 } as SessionEntry ,
17901832 } ) ;
17911833
17921834 const message = requireRunReplyAgentCall ( ) . followupRun . userTurnTranscriptRecorder ?. message ;
1793- if ( shouldPersist ) {
1794- expect ( message ) . toMatchObject ( {
1795- __openclaw : {
1796- senderId : "user-42" ,
1797- senderName : "Ada" ,
1798- senderUsername : "ada" ,
1799- } ,
1800- } ) ;
1801- } else {
1802- expect ( message ) . not . toHaveProperty ( "__openclaw.senderId" ) ;
1803- expect ( message ) . not . toHaveProperty ( "__openclaw.senderName" ) ;
1804- expect ( message ) . not . toHaveProperty ( "__openclaw.senderUsername" ) ;
1805- }
1835+ expect ( message ) . not . toHaveProperty ( "__openclaw.senderId" ) ;
1836+ expect ( message ) . not . toHaveProperty ( "__openclaw.senderName" ) ;
1837+ expect ( message ) . not . toHaveProperty ( "__openclaw.senderUsername" ) ;
1838+ } ) ;
1839+
1840+ it ( "does not persist sender attribution for gateway-local direct turns without channel admission" , async ( ) => {
1841+ await runPrepared ( {
1842+ ctx : {
1843+ ...createInboundBody ( "hello" ) ,
1844+ OriginatingChannel : "webchat" ,
1845+ OriginatingTo : "chat-1" ,
1846+ ChatType : "direct" ,
1847+ } ,
1848+ sessionCtx : {
1849+ ...createSessionBody ( "hello" ) ,
1850+ Provider : "webchat" ,
1851+ OriginatingChannel : "webchat" ,
1852+ OriginatingTo : "chat-1" ,
1853+ ChatType : "direct" ,
1854+ SenderId : "gateway-cli" ,
1855+ SenderName : "Gateway CLI" ,
1856+ SenderUsername : "cli" ,
1857+ } ,
1858+ sessionEntry : {
1859+ sessionId : "session-1" ,
1860+ updatedAt : 1 ,
1861+ chatType : "direct" ,
1862+ channel : "webchat" ,
1863+ } as SessionEntry ,
1864+ } ) ;
1865+
1866+ const message = requireRunReplyAgentCall ( ) . followupRun . userTurnTranscriptRecorder ?. message ;
1867+ expect ( message ) . not . toHaveProperty ( "__openclaw.senderId" ) ;
1868+ expect ( message ) . not . toHaveProperty ( "__openclaw.senderName" ) ;
1869+ expect ( message ) . not . toHaveProperty ( "__openclaw.senderUsername" ) ;
18061870 } ) ;
18071871
18081872 it ( "normalizes second-based inbound timestamps before preparing user turns" , async ( ) => {
0 commit comments