11/**
22 * Queues embedded-agent session compaction onto the correct command lane.
33 */
4+ import { normalizeOptionalString } from "@openclaw/normalization-core/string-coerce" ;
45import { OPENCLAW_EMBEDDED_CONTEXT_ENGINE_HOST } from "../../context-engine/host-compat.js" ;
56import { ensureContextEnginesInitialized } from "../../context-engine/init.js" ;
67import {
@@ -158,6 +159,7 @@ async function disposeContextEngine(contextEngine: ContextEngine): Promise<void>
158159
159160async function deferOwningContextEngineBudgetCompaction ( params : {
160161 compactParams : CompactEmbeddedAgentSessionParams ;
162+ contextEngineSessionKey ?: string ;
161163 contextEngine : ContextEngine ;
162164 contextEngineRuntimeContext : ContextEngineRuntimeContext ;
163165 contextEngineRuntimeSettings : ContextEngineRuntimeSettings ;
@@ -168,13 +170,14 @@ async function deferOwningContextEngineBudgetCompaction(params: {
168170 await runContextEngineMaintenance ( {
169171 contextEngine : params . contextEngine ,
170172 sessionId : params . compactParams . sessionId ,
171- sessionKey : params . compactParams . sessionKey ,
173+ sessionKey : params . contextEngineSessionKey ?? params . compactParams . sessionKey ,
172174 sessionTarget : buildContextEngineCompactionSessionTarget ( params . compactParams ) ,
173175 sessionFile : params . compactParams . sessionFile ,
174176 reason : "turn" ,
175177 runtimeContext : params . contextEngineRuntimeContext ,
176178 runtimeSettings : params . contextEngineRuntimeSettings ,
177179 config : params . compactParams . config ,
180+ contextEngineAgentId : params . compactParams . contextEngineAgentId ,
178181 disposeDeferredContextEngineAfterMaintenance : true ,
179182 onDeferredMaintenance : ( ) => {
180183 deferredScheduled = true ;
@@ -243,6 +246,11 @@ function mergeSecondaryNativeHarnessCompactionDetails(params: {
243246export async function compactEmbeddedAgentSession (
244247 params : CompactEmbeddedAgentSessionParams ,
245248) : Promise < EmbeddedAgentCompactResult > {
249+ const contextEngineAgentId =
250+ normalizeOptionalString ( params . contextEngineAgentId ) ?? normalizeOptionalString ( params . agentId ) ;
251+ const contextEngineSessionKey =
252+ normalizeOptionalString ( params . sessionKey ) ??
253+ normalizeOptionalString ( params . sessionTarget ?. sessionKey ) ;
246254 const runtimeTarget = await resolveAgentRunSessionTarget ( {
247255 ...params ,
248256 missingSessionKey : "resolve-existing" ,
@@ -254,9 +262,10 @@ export async function compactEmbeddedAgentSession(
254262 sessionKey : runtimeTarget . sessionKey ,
255263 sessionTarget : runtimeTarget ,
256264 sessionFile : runtimeTarget . sessionKey ,
265+ contextEngineAgentId,
257266 } ;
258267 if ( resolvedParams . trigger !== "manual" ) {
259- return await compactEmbeddedAgentSessionImpl ( resolvedParams ) ;
268+ return await compactEmbeddedAgentSessionImpl ( resolvedParams , contextEngineSessionKey ) ;
260269 }
261270 // Reply operations and embedded handles are separate lifecycle owners. A
262271 // /compact reply may coexist with this handle, but another embedded writer may not.
@@ -289,10 +298,13 @@ export async function compactEmbeddedAgentSession(
289298 resolvedParams . sessionFile ,
290299 ) ;
291300 try {
292- return await compactEmbeddedAgentSessionImpl ( {
293- ...resolvedParams ,
294- abortSignal,
295- } ) ;
301+ return await compactEmbeddedAgentSessionImpl (
302+ {
303+ ...resolvedParams ,
304+ abortSignal,
305+ } ,
306+ contextEngineSessionKey ,
307+ ) ;
296308 } finally {
297309 clearActiveEmbeddedRun (
298310 resolvedParams . sessionId ,
@@ -305,6 +317,7 @@ export async function compactEmbeddedAgentSession(
305317
306318async function compactEmbeddedAgentSessionImpl (
307319 inputParams : CompactEmbeddedAgentSessionParams ,
320+ contextEngineSessionKey ?: string ,
308321) : Promise < EmbeddedAgentCompactResult > {
309322 if ( inputParams . abortSignal ?. aborted ) {
310323 return createCompactionAbortedResult ( ) ;
@@ -399,6 +412,7 @@ async function compactEmbeddedAgentSessionImpl(
399412 agentDir ,
400413 resolvedWorkspaceDir ,
401414 lease . snapshot ,
415+ contextEngineSessionKey ,
402416 ( ) => {
403417 disposeContextEngineOnExit = false ;
404418 } ,
@@ -422,6 +436,7 @@ async function compactResolvedContextEngine(
422436 agentDir : string ,
423437 resolvedWorkspaceDir : string ,
424438 preparedModelRuntime : PreparedModelRuntimeSnapshot ,
439+ contextEngineSessionKey : string | undefined ,
425440 releaseContextEngineOwnership : ( ) => void ,
426441) : Promise < EmbeddedAgentCompactResult > {
427442 const runtimeTarget = await resolveAgentRunSessionTarget ( {
@@ -576,6 +591,7 @@ async function compactResolvedContextEngine(
576591 params : preparedParams ,
577592 agentDir,
578593 harnessRuntime : preparedHarnessRuntime ,
594+ contextEngineSessionKey,
579595 contextTokenBudget,
580596 contextEnginePluginId : resolveContextEngineOwnerPluginId ( contextEngine ) ,
581597 } ) ;
@@ -620,6 +636,7 @@ async function compactResolvedContextEngine(
620636 ) {
621637 const deferredResult = await deferOwningContextEngineBudgetCompaction ( {
622638 compactParams : preparedParams ,
639+ contextEngineSessionKey,
623640 contextEngine,
624641 contextEngineRuntimeContext,
625642 contextEngineRuntimeSettings,
@@ -768,7 +785,7 @@ async function compactResolvedContextEngine(
768785 await runContextEngineMaintenance ( {
769786 contextEngine,
770787 sessionId : postCompactionSessionId ,
771- sessionKey : params . sessionKey ,
788+ sessionKey : contextEngineSessionKey ?? params . sessionKey ,
772789 sessionTarget : buildContextEngineCompactionSessionTarget ( {
773790 ...params ,
774791 sessionFile : postCompactionSessionFile ,
@@ -780,6 +797,7 @@ async function compactResolvedContextEngine(
780797 runtimeContext,
781798 runtimeSettings : contextEngineRuntimeSettings ,
782799 config : params . config ,
800+ contextEngineAgentId : params . contextEngineAgentId ,
783801 } ) ;
784802 }
785803 if ( engineOwnsCompaction && result . ok && result . compacted ) {
@@ -922,16 +940,12 @@ function shouldAttemptNativeHarnessCompaction(params: {
922940function buildCompactionContextEngineRuntimeContext ( params : {
923941 params : CompactEmbeddedAgentSessionParams ;
924942 agentDir : string ;
943+ contextEngineSessionKey ?: string ;
925944 harnessRuntime ?: string ;
926945 contextEnginePluginId ?: string ;
927946 contextTokenBudget ?: number ;
928947} ) : ContextEngineRuntimeContext {
929- const { sessionAgentId } = resolveSessionAgentIds ( {
930- sessionKey : params . params . sessionKey ,
931- config : params . params . config ,
932- agentId : params . params . agentId ,
933- } ) ;
934- const { sessionFile : _sessionFile , ...runtimeParams } = params . params ;
948+ const { sessionFile : _sessionFile , contextEngineAgentId, ...runtimeParams } = params . params ;
935949 return {
936950 ...runtimeParams ,
937951 sessionTarget : buildContextEngineCompactionSessionTarget ( params . params ) ,
@@ -943,8 +957,8 @@ function buildCompactionContextEngineRuntimeContext(params: {
943957 } ) ,
944958 ...resolveContextEngineCapabilities ( {
945959 config : params . params . config ,
946- sessionKey : params . params . sessionKey ,
947- agentId : sessionAgentId ,
960+ sessionKey : params . contextEngineSessionKey ?? params . params . sessionKey ,
961+ explicitAgentId : contextEngineAgentId ,
948962 authProfileId : params . params . authProfileId ,
949963 contextEnginePluginId : params . contextEnginePluginId ,
950964 purpose : "context-engine.compaction" ,
0 commit comments