@@ -1787,6 +1787,138 @@ describe("gateway server sessions", () => {
17871787 ws . close ( ) ;
17881788 } ) ;
17891789
1790+ test ( "session:patch hook fires with correct context" , async ( ) => {
1791+ const dir = await fs . mkdtemp ( path . join ( os . tmpdir ( ) , "openclaw-sessions-patch-hook-" ) ) ;
1792+ const storePath = path . join ( dir , "sessions.json" ) ;
1793+ testState . sessionStorePath = storePath ;
1794+
1795+ await writeSessionStore ( {
1796+ entries : {
1797+ main : {
1798+ sessionId : "sess-hook-test" ,
1799+ updatedAt : Date . now ( ) ,
1800+ label : "original-label" ,
1801+ } ,
1802+ } ,
1803+ } ) ;
1804+
1805+ sessionHookMocks . triggerInternalHook . mockClear ( ) ;
1806+
1807+ const { ws } = await openClient ( ) ;
1808+
1809+ const patched = await rpcReq ( ws , "sessions.patch" , {
1810+ key : "agent:main:main" ,
1811+ label : "updated-label" ,
1812+ } ) ;
1813+
1814+ expect ( patched . ok ) . toBe ( true ) ;
1815+ expect ( sessionHookMocks . triggerInternalHook ) . toHaveBeenCalledWith (
1816+ expect . objectContaining ( {
1817+ type : "session" ,
1818+ action : "patch" ,
1819+ sessionKey : expect . stringMatching ( / a g e n t : m a i n : m a i n / ) ,
1820+ context : expect . objectContaining ( {
1821+ sessionEntry : expect . objectContaining ( {
1822+ sessionId : "sess-hook-test" ,
1823+ label : "updated-label" ,
1824+ } ) ,
1825+ patch : expect . objectContaining ( {
1826+ label : "updated-label" ,
1827+ } ) ,
1828+ cfg : expect . any ( Object ) ,
1829+ } ) ,
1830+ } ) ,
1831+ ) ;
1832+
1833+ ws . close ( ) ;
1834+ } ) ;
1835+
1836+ test ( "session:patch hook does not fire for webchat clients" , async ( ) => {
1837+ const dir = await fs . mkdtemp ( path . join ( os . tmpdir ( ) , "openclaw-sessions-webchat-hook-" ) ) ;
1838+ const storePath = path . join ( dir , "sessions.json" ) ;
1839+ testState . sessionStorePath = storePath ;
1840+
1841+ await writeSessionStore ( {
1842+ entries : {
1843+ main : {
1844+ sessionId : "sess-webchat-test" ,
1845+ updatedAt : Date . now ( ) ,
1846+ } ,
1847+ } ,
1848+ } ) ;
1849+
1850+ sessionHookMocks . triggerInternalHook . mockClear ( ) ;
1851+
1852+ const ws = new WebSocket ( `ws://127.0.0.1:${ harness . port } ` , {
1853+ headers : { origin : `http://127.0.0.1:${ harness . port } ` } ,
1854+ } ) ;
1855+ trackConnectChallengeNonce ( ws ) ;
1856+ await new Promise < void > ( ( resolve ) => ws . once ( "open" , resolve ) ) ;
1857+ await connectOk ( ws , {
1858+ client : {
1859+ id : GATEWAY_CLIENT_IDS . WEBCHAT_UI ,
1860+ version : "1.0.0" ,
1861+ platform : "test" ,
1862+ mode : GATEWAY_CLIENT_MODES . UI ,
1863+ } ,
1864+ scopes : [ "operator.admin" ] ,
1865+ } ) ;
1866+
1867+ const patched = await rpcReq ( ws , "sessions.patch" , {
1868+ key : "agent:main:main" ,
1869+ label : "should-not-trigger-hook" ,
1870+ } ) ;
1871+
1872+ expect ( patched . ok ) . toBe ( false ) ;
1873+ expect ( sessionHookMocks . triggerInternalHook ) . not . toHaveBeenCalled ( ) ;
1874+
1875+ ws . close ( ) ;
1876+ } ) ;
1877+
1878+ test ( "session:patch hook only fires after successful patch" , async ( ) => {
1879+ const dir = await fs . mkdtemp ( path . join ( os . tmpdir ( ) , "openclaw-sessions-success-hook-" ) ) ;
1880+ const storePath = path . join ( dir , "sessions.json" ) ;
1881+ testState . sessionStorePath = storePath ;
1882+
1883+ await writeSessionStore ( {
1884+ entries : {
1885+ main : {
1886+ sessionId : "sess-success-test" ,
1887+ updatedAt : Date . now ( ) ,
1888+ } ,
1889+ } ,
1890+ } ) ;
1891+
1892+ const { ws } = await openClient ( ) ;
1893+
1894+ sessionHookMocks . triggerInternalHook . mockClear ( ) ;
1895+
1896+ // Test 1: Invalid patch (missing key) - hook should not fire
1897+ const invalidPatch = await rpcReq ( ws , "sessions.patch" , {
1898+ // Missing required 'key' parameter
1899+ label : "should-fail" ,
1900+ } ) ;
1901+
1902+ expect ( invalidPatch . ok ) . toBe ( false ) ;
1903+ expect ( sessionHookMocks . triggerInternalHook ) . not . toHaveBeenCalled ( ) ;
1904+
1905+ // Test 2: Valid patch - hook should fire
1906+ const validPatch = await rpcReq ( ws , "sessions.patch" , {
1907+ key : "agent:main:main" ,
1908+ label : "should-succeed" ,
1909+ } ) ;
1910+
1911+ expect ( validPatch . ok ) . toBe ( true ) ;
1912+ expect ( sessionHookMocks . triggerInternalHook ) . toHaveBeenCalledWith (
1913+ expect . objectContaining ( {
1914+ type : "session" ,
1915+ action : "patch" ,
1916+ } ) ,
1917+ ) ;
1918+
1919+ ws . close ( ) ;
1920+ } ) ;
1921+
17901922 test ( "control-ui client can delete sessions even in webchat mode" , async ( ) => {
17911923 const dir = await fs . mkdtemp ( path . join ( os . tmpdir ( ) , "openclaw-sessions-control-ui-delete-" ) ) ;
17921924 const storePath = path . join ( dir , "sessions.json" ) ;
0 commit comments