@@ -278,7 +278,7 @@ const sessionHook = (action: string): SessionHookEvent | undefined =>
278278 return event ?. type === "session" && event . action === action ;
279279 } ) ?. [ 0 ] as SessionHookEvent | undefined ;
280280
281- async function runCompactionHooks ( params : { sessionKey ? : string ; messageProvider ?: string } ) {
281+ async function runCompactionHooks ( params : { sessionKey : string ; messageProvider ?: string } ) {
282282 // Build metrics through the production helper so hook payload assertions stay
283283 // aligned with compaction token accounting.
284284 const originalMessages = sessionMessages . slice ( 1 ) as AgentMessage [ ] ;
@@ -1903,26 +1903,6 @@ describe("compactEmbeddedAgentSessionDirect hooks", () => {
19031903 ) ;
19041904 } ) ;
19051905
1906- it ( "uses sessionId as hook session key fallback when sessionKey is missing" , async ( ) => {
1907- hookRunner . hasHooks . mockReturnValue ( true ) ;
1908- await runCompactionHooks ( { } ) ;
1909-
1910- expect ( sessionHook ( "compact:before" ) ?. sessionKey ) . toBe ( "session-1" ) ;
1911- expect ( sessionHook ( "compact:after" ) ?. sessionKey ) . toBe ( "session-1" ) ;
1912- expect ( hookRunner . runBeforeCompaction ) . toHaveBeenCalledWith (
1913- mockCallArg ( hookRunner . runBeforeCompaction ) ,
1914- expectRecordFields ( mockCallArg ( hookRunner . runBeforeCompaction , 0 , 1 ) , {
1915- sessionKey : "session-1" ,
1916- } ) ,
1917- ) ;
1918- expect ( hookRunner . runAfterCompaction ) . toHaveBeenCalledWith (
1919- mockCallArg ( hookRunner . runAfterCompaction ) ,
1920- expectRecordFields ( mockCallArg ( hookRunner . runAfterCompaction , 0 , 1 ) , {
1921- sessionKey : "session-1" ,
1922- } ) ,
1923- ) ;
1924- } ) ;
1925-
19261906 it ( "applies validated transcript before hooks even when it becomes empty" , async ( ) => {
19271907 hookRunner . hasHooks . mockReturnValue ( true ) ;
19281908 const beforeMetrics = compactTesting . buildBeforeCompactionHookMetrics ( {
@@ -2454,6 +2434,46 @@ describe("compactEmbeddedAgentSession hooks (ownsCompaction engine)", () => {
24542434 mockQueuedRouteAwareModel ( ) ;
24552435 } ) ;
24562436
2437+ it ( "resolves the durable session key before invoking an owning context engine" , async ( ) => {
2438+ const dir = await mkdtemp ( join ( tmpdir ( ) , "openclaw-compaction-session-key-" ) ) ;
2439+ const storePath = join ( dir , "sessions.json" ) ;
2440+ const sessionId = "9d6c8436-7cb2-4bd5-a302-e33305bfc8c4" ;
2441+ const sessionKey = "agent:main:telegram:direct:reporter" ;
2442+ try {
2443+ await upsertSessionEntry (
2444+ { agentId : "main" , sessionKey, storePath } ,
2445+ { sessionId, updatedAt : 1 } ,
2446+ ) ;
2447+ hookRunner . hasHooks . mockReturnValue ( true ) ;
2448+
2449+ const result = await compactEmbeddedAgentSession (
2450+ wrappedCompactionArgs ( {
2451+ agentId : "main" ,
2452+ config : { session : { store : storePath } } ,
2453+ sessionFile : "" ,
2454+ sessionId,
2455+ sessionKey : undefined ,
2456+ sessionTarget : undefined ,
2457+ } ) ,
2458+ ) ;
2459+
2460+ expect ( result . ok ) . toBe ( true ) ;
2461+ expectRecordFields ( mockCallArg ( contextEngineCompactMock ) , {
2462+ sessionId,
2463+ sessionKey,
2464+ } ) ;
2465+ expectRecordFields (
2466+ ( mockCallArg ( contextEngineCompactMock ) as { sessionTarget ?: unknown } ) . sessionTarget ,
2467+ { agentId : "main" , sessionId, sessionKey, storePath } ,
2468+ ) ;
2469+ expectRecordFields ( mockCallArg ( hookRunner . runBeforeCompaction , 0 , 1 ) , { sessionKey } ) ;
2470+ expectRecordFields ( mockCallArg ( hookRunner . runAfterCompaction , 0 , 1 ) , { sessionKey } ) ;
2471+ } finally {
2472+ closeOpenClawAgentDatabasesForTest ( ) ;
2473+ await rm ( dir , { force : true , recursive : true } ) ;
2474+ }
2475+ } ) ;
2476+
24572477 it ( "uses the acquired gateway runtime generation for queued model resolution" , async ( ) => {
24582478 await compactEmbeddedAgentSession (
24592479 wrappedCompactionArgs ( {
0 commit comments