88} from "@openclaw/normalization-core/string-coerce" ;
99import { truncateUtf16Safe } from "@openclaw/normalization-core/utf16-slice" ;
1010import { resolveBootstrapWarningSignaturesSeen } from "../../agents/bootstrap-budget.js" ;
11+ import { resolveCliBackendConfig } from "../../agents/cli-backends.js" ;
1112import { estimateMessagesTokens } from "../../agents/compaction.js" ;
1213import { isBenignCompactionSkipResult } from "../../agents/embedded-agent-runner/compact-reasons.js" ;
1314import { runEmbeddedAgentEntry } from "../../agents/embedded-agent-runner/run-entry.js" ;
@@ -255,32 +256,28 @@ function resolveMemoryFlushModelFallbackOptions(
255256 } ;
256257}
257258
258- function followupUsesCliRuntime ( params : {
259+ type FollowupRuntimeParams = {
259260 cfg : OpenClawConfig ;
260261 followupRun : FollowupRun ;
261262 sessionEntry ?: Pick <
262263 SessionEntry ,
263- "agentHarnessId" | "agentRuntimeOverride" | "modelSelectionLocked"
264+ "agentHarnessId" | "agentRuntimeOverride" | "modelSelectionLocked" | "sessionId"
264265 > ;
265- } ) : boolean {
266+ sessionKey ?: string ;
267+ runtimePolicySessionKey ?: string ;
268+ } ;
269+
270+ function followupUsesCliRuntime ( params : FollowupRuntimeParams , runtimeId : string ) : boolean {
266271 const provider = params . followupRun . run . provider ;
267272 if ( isCliProvider ( provider , params . cfg ) ) {
268273 return true ;
269274 }
270- return isCliRuntimeAliasForProvider ( {
271- provider,
272- runtime : resolvePersistedSessionRuntimeId ( params . sessionEntry ) ,
273- cfg : params . cfg ,
274- } ) ;
275+ return [ resolvePersistedSessionRuntimeId ( params . sessionEntry ) , runtimeId ] . some ( ( runtime ) =>
276+ isCliRuntimeAliasForProvider ( { provider, runtime, cfg : params . cfg } ) ,
277+ ) ;
275278}
276279
277- function resolveFollowupContextConfigProvider ( params : {
278- cfg : OpenClawConfig ;
279- followupRun : FollowupRun ;
280- sessionEntry ?: SessionEntry ;
281- sessionKey ?: string ;
282- runtimePolicySessionKey ?: string ;
283- } ) : string {
280+ function resolveFollowupContextConfigProvider ( params : FollowupRuntimeParams ) : string {
284281 const provider = params . followupRun . run . provider ;
285282 return resolveContextConfigProviderForRuntime ( {
286283 provider,
@@ -289,13 +286,7 @@ function resolveFollowupContextConfigProvider(params: {
289286 } ) ;
290287}
291288
292- function resolveFollowupAgentRuntimeId ( params : {
293- cfg : OpenClawConfig ;
294- followupRun : FollowupRun ;
295- sessionEntry ?: SessionEntry ;
296- sessionKey ?: string ;
297- runtimePolicySessionKey ?: string ;
298- } ) : string {
289+ function resolveFollowupAgentRuntimeId ( params : FollowupRuntimeParams ) : string {
299290 const matchingSessionEntry =
300291 params . sessionEntry ?. sessionId === params . followupRun . run . sessionId
301292 ? params . sessionEntry
@@ -314,14 +305,14 @@ function resolveFollowupAgentRuntimeId(params: {
314305 } ) ;
315306}
316307
317- function followupUsesCodexRuntime ( params : {
318- cfg : OpenClawConfig ;
319- followupRun : FollowupRun ;
320- sessionEntry ?: SessionEntry ;
321- sessionKey ?: string ;
322- runtimePolicySessionKey ?: string ;
323- } ) : boolean {
324- return normalizeLowercaseStringOrEmpty ( resolveFollowupAgentRuntimeId ( params ) ) === "codex" ;
308+ function followupOwnsNativeCompaction ( params : FollowupRuntimeParams , runtimeId : string ) : boolean {
309+ // Backends that persist resumable native transcripts must remain the sole
310+ // compaction owner; OpenClaw maintenance would corrupt that runtime state.
311+ return (
312+ resolveCliBackendConfig ( runtimeId , params . cfg , {
313+ agentId : params . followupRun . run . agentId ,
314+ } ) ?. ownsNativeCompaction === true
315+ ) ;
325316}
326317
327318function resolveVisibleMemoryFlushErrorPayloads ( payloads ?: ReplyPayload [ ] ) : ReplyPayload [ ] {
@@ -792,23 +783,20 @@ export async function runPreflightCompactionIfNeeded(params: {
792783 return entry ?? params . sessionEntry ;
793784 }
794785
795- const isCli = followupUsesCliRuntime ( {
786+ const runtimeParams = {
796787 cfg : params . cfg ,
797788 followupRun : params . followupRun ,
798789 sessionEntry : entry ,
799- } ) ;
800- if ( params . isHeartbeat || isCli ) {
790+ sessionKey : params . sessionKey ,
791+ runtimePolicySessionKey : params . runtimePolicySessionKey ,
792+ } ;
793+ const runtimeId = resolveFollowupAgentRuntimeId ( runtimeParams ) ;
794+ const isCli = followupUsesCliRuntime ( runtimeParams , runtimeId ) ;
795+ const ownsNativeCompaction = followupOwnsNativeCompaction ( runtimeParams , runtimeId ) ;
796+ if ( params . isHeartbeat || isCli || ownsNativeCompaction ) {
801797 return entry ?? params . sessionEntry ;
802798 }
803- if (
804- followupUsesCodexRuntime ( {
805- cfg : params . cfg ,
806- followupRun : params . followupRun ,
807- sessionEntry : entry ,
808- sessionKey : params . sessionKey ,
809- runtimePolicySessionKey : params . runtimePolicySessionKey ,
810- } )
811- ) {
799+ if ( normalizeLowercaseStringOrEmpty ( runtimeId ) === "codex" ) {
812800 // Codex runtime sessions should reach Codex with their real thread state.
813801 // Its harness owns automatic compaction; OpenClaw preflight compaction is
814802 // only for non-Codex embedded runtimes.
@@ -1170,11 +1158,17 @@ export async function runMemoryFlushIfNeeded(params: {
11701158 if ( entry ?. incognito === true || isIncognitoSessionKey ( params . sessionKey ) ) {
11711159 return { sessionEntry : entry , outcome : "skipped" } ;
11721160 }
1173- const isCli = followupUsesCliRuntime ( {
1161+ const runtimeParams = {
11741162 cfg : params . cfg ,
11751163 followupRun : params . followupRun ,
11761164 sessionEntry : entry ,
1177- } ) ;
1165+ sessionKey : params . sessionKey ,
1166+ runtimePolicySessionKey : params . runtimePolicySessionKey ,
1167+ } ;
1168+ const runtimeId = resolveFollowupAgentRuntimeId ( runtimeParams ) ;
1169+ const isCli =
1170+ followupUsesCliRuntime ( runtimeParams , runtimeId ) ||
1171+ followupOwnsNativeCompaction ( runtimeParams , runtimeId ) ;
11781172 const canAttemptFlush = memoryFlushWritable && ! params . isHeartbeat && ! isCli ;
11791173 const contextWindowTokens = resolveMemoryFlushContextWindowTokens ( {
11801174 cfg : params . cfg ,
0 commit comments