openclaw - ✅(Solved) Fix Turn-triggered context-engine maintenance is awaited inline and can stall the session lane [2 pull requests, 1 participants]

Official PRs (…)
ON THIS PAGE

Recommended Tools

×6

Utilities matched from this issue’s tags and category — try them while you read without losing context.

GitHub issue graph ai analysis

Paste a GitHub issue URL. We fetch that issue, discover linked issues from bodies/comments/timeline, collect linked pull requests, and produce a structured English report.

The report is written in English Markdown for sharing and archival.

Helpful · Quick feedback

Loading…
GitHub stats
openclaw/openclaw#65220Fetched 2026-04-12 13:25:02
View on GitHub
Comments
0
Participants
1
Timeline
2
Reactions
0
Participants
Timeline (top)
cross-referenced ×2

Fix Action

Fixed

PR fix notes

PR #408: Defer proactive compaction debt and surface LCM maintenance state

Description (problem / solution / changelog)

Summary

Closes #407.

This changes proactive compaction from inline turn work into deferred maintenance debt by default, then makes that deferred path cache-safe for Anthropic. The result is a hybrid model: proactive compaction no longer blocks the foreground afterTurn() path, and prompt-mutating deferred compaction no longer rewrites a still-hot Anthropic cache.

Why

  • Inline proactive compaction can keep the main session lane busy after the assistant reply is already visible.
  • That starves immediate follow-up turns and can push subagent completion announce traffic into timeouts.
  • Anthropic prompt caching is exact-prefix, so rewriting the active prompt too soon can burn a freshly written cache and drive up cost.
  • Orchestrator-heavy sessions make the pain worse: one main agent plus up to 4 subagents may all need fast LCM reads while one hot session is still writing.

What Changed

  • Added proactiveThresholdCompactionMode: "deferred" | "inline"; default is "deferred".
  • afterTurn() now records coalesced proactive compaction debt per conversation/session instead of running proactive threshold or leaf compaction inline.
  • maintain() now consumes that debt only when runtime context explicitly allows deferred execution, and it skips prompt-mutating deferred compaction while Anthropic cache is still hot.
  • assemble() now consumes deferred prompt-mutating compaction pre-assembly when the cache is cold or the prompt is approaching overflow.
  • Added persistent maintenance state for pending/running/last success/last failure metadata.
  • Added cacheAwareCompaction.cacheTTLSeconds (default 300) and telemetry for lastApiCallAt, lastCacheTouchAt, provider, and model.
  • Extended LCM status/command output and startup banners to surface maintenance state and cache-aware compaction context.
  • Preserved the local same-turn safety guard in legacy inline mode so leaf and threshold compaction do not both fire on the same turn.
  • Kept manual, overflow, and timeout compaction synchronous.
  • Advertised turnMaintenanceMode: "background" for the companion OpenClaw host change.

Safety

  • Legacy inline mode remains available as a rollback/debug escape hatch.
  • Read-only LCM tools continue to work while compaction debt is pending.
  • Public tool inputs stay stable apart from the new config option.
  • Anthropic-active sessions keep their hot cache unless compaction is needed for correctness or the cache has already gone cold.

Validation

  • npm test -> 40 files / 709 tests passed
  • npm run build passed

Companion Host Work

  • Companion OpenClaw issue: openclaw/openclaw#65220
  • Companion OpenClaw PR: openclaw/openclaw#65233
  • Anthropic-native companion issue: openclaw/openclaw#65287
  • Anthropic-native companion PR: openclaw/openclaw#65288

Changed files

  • .changeset/deferred-compaction-maintenance.md (added, +5/-0)
  • README.md (modified, +9/-0)
  • docs/configuration.md (modified, +15/-0)
  • openclaw.plugin.json (modified, +19/-0)
  • skills/lossless-claw/references/config.md (modified, +25/-0)
  • src/db/config.ts (modified, +23/-0)
  • src/db/migration.ts (modified, +34/-0)
  • src/engine.ts (modified, +774/-375)
  • src/plugin/index.ts (modified, +6/-1)
  • src/plugin/lcm-command.ts (modified, +75/-3)
  • src/startup-banner-log.ts (modified, +1/-0)
  • src/store/compaction-maintenance-store.ts (added, +219/-0)
  • src/store/compaction-telemetry-store.ts (modified, +33/-1)
  • src/store/index.ts (modified, +5/-0)
  • test/bootstrap-flood-regression.test.ts (modified, +1/-0)
  • test/circuit-breaker.test.ts (modified, +1/-0)
  • test/config.test.ts (modified, +25/-0)
  • test/engine.test.ts (modified, +391/-27)
  • test/expansion.test.ts (modified, +1/-0)
  • test/lcm-command.test.ts (modified, +52/-0)
  • test/lcm-expand-query-tool.test.ts (modified, +1/-0)
  • test/lcm-expand-tool.test.ts (modified, +1/-0)
  • test/lcm-tools.test.ts (modified, +1/-0)
  • test/plugin-config-registration.test.ts (modified, +17/-3)
  • test/session-operation-queues.test.ts (modified, +1/-0)

PR #65233: Run context-engine turn maintenance as idle-aware background work

Description (problem / solution / changelog)

Summary

Closes #65220.

This teaches context engines to opt into background turn maintenance and moves turn-triggered maintenance off the foreground session lane when turnMaintenanceMode === "background". For lossless-claw, that means proactive post-turn maintenance can be queued as a hidden task instead of blocking the next user turn.

Why

  • Today the host awaits both afterTurn() and maintain(... reason: "turn") inline on the session path.
  • Even if a plugin removes heavy foreground work, the host still controls whether turn maintenance blocks the lane.
  • The result is visible assistant output followed by dead time before the next command can start.
  • Subagent completion announce traffic can time out behind a stalled main lane.

What Changed

  • Added turnMaintenanceMode?: "foreground" | "background" to context-engine info.
  • Added allowDeferredCompactionExecution?: boolean to runtime context.
  • When a context engine opts into background mode, runContextEngineMaintenance(... reason: "turn") now:
    • queues one hidden maintenance task per session key
    • coalesces repeated requests
    • waits for the session lane to go idle before executing
    • runs maintenance with allowDeferredCompactionExecution: true
    • uses task delivery/status infrastructure instead of a visible subagent or announce flow
  • Long-running or failed deferred maintenance can surface subtle task updates to the session.
  • Fast deferred maintenance stays silent.

Guarantees

  • Proactive turn maintenance no longer blocks the immediate next foreground turn for engines that opt in.
  • Overflow/timeout/manual compaction remains synchronous in the plugin/runtime paths that require correctness.
  • Hidden maintenance is not a child agent run and does not create synthetic subagent sessions.

Validation

  • pnpm test -- --run src/agents/pi-embedded-runner/context-engine-maintenance.test.ts src/infra/backoff.test.ts passed
  • NODE_OPTIONS=--max-old-space-size=8192 pnpm exec tsc -p tsconfig.json --noEmit still reports preexisting unrelated AbortSignal type errors in unchanged extensions/telegram/src/bot.ts

Companion Plugin Work

  • Companion lossless-claw issue: Martian-Engineering/lossless-claw#407
  • Companion lossless-claw PR: Martian-Engineering/lossless-claw#408

Changed files

  • src/agents/pi-embedded-runner/context-engine-maintenance.test.ts (modified, +1013/-3)
  • src/agents/pi-embedded-runner/context-engine-maintenance.ts (modified, +566/-21)
  • src/context-engine/types.ts (modified, +12/-0)
  • src/infra/backoff.test.ts (modified, +35/-0)
  • src/infra/backoff.ts (modified, +40/-9)
RAW_BUFFERClick to expand / collapse

Problem

OpenClaw currently awaits context-engine turn maintenance inline. That means even when the assistant has already produced visible output, the session lane can remain occupied by maintenance work long enough to stall the next command, delay subagent completion announce, or make the system appear frozen.

This is especially painful in:

  • orchestrated runs with up to 4 subagents in write mode
  • read-heavy runs with up to 4 agents querying LCM concurrently
  • sessions where proactive maintenance and normal user traffic overlap

Why Plugin-Only Fixes Are Not Enough

lossless-claw can remove heavy foreground work, but the host still owns when turn maintenance runs. As long as the host awaits maintenance inline, the foreground lane can still be blocked.

We need a host-level primitive for hidden, idle-aware background maintenance that can coalesce per session and avoid any visible subagent/announce path.

Goal

Add a host-side background maintenance path for context engines so proactive turn maintenance never blocks the next foreground user turn.

Acceptance Criteria

  • Proactive turn maintenance no longer blocks the next foreground user turn.
  • There is one pending maintenance task per session key, coalesced if more requests arrive.
  • Maintenance waits for the session lane to be idle or yields to active user work.
  • The maintenance path is hidden:
    • no visible subagent session
    • no announce flow
  • Overflow/timeout recovery compaction remains synchronous.
  • User-visible status remains subtle and limited to long-running or failed maintenance.

Validation Notes

  • Reproduce with one orchestrator plus multiple subagents and confirm the session lane stays responsive.
  • Reproduce with read-heavy LCM traffic and confirm background maintenance does not block reads.
  • Verify background maintenance state is visible in task/status tooling without creating user-facing subagent noise.

extent analysis

TL;DR

Implement a host-side background maintenance path for context engines to prevent proactive turn maintenance from blocking the next foreground user turn.

Guidance

  • Introduce a new host-level primitive for idle-aware background maintenance that can coalesce per session, ensuring that maintenance tasks do not block the foreground lane.
  • Modify the maintenance workflow to wait for the session lane to be idle or yield to active user work, preventing delays or stalls.
  • Implement a mechanism to coalesce multiple maintenance requests into a single pending task per session key, avoiding unnecessary overhead.
  • Verify that the new maintenance path is hidden from users, with no visible subagent sessions or announce flows, and that user-visible status updates are subtle and limited.

Example

No explicit code example is provided due to the lack of specific implementation details in the issue.

Notes

The solution requires careful consideration of the trade-offs between maintenance overhead, user responsiveness, and system complexity. The implementation should ensure that background maintenance does not interfere with foreground user activity.

Recommendation

Apply a workaround by introducing a host-side background maintenance path, as this approach addresses the root cause of the issue and provides a more scalable and responsive solution.

Vote matrix · Quick signals

Works
Did the solution work? Tap to confirm.
Easy Fix
Was it a quick fix?
Time Saver
Did it save you time?
Blocking
Was it severely blocking?
Common Issue
Are others likely hitting this too?
Flaky / Intermittent
Is it intermittent?
Verified / Reproducible
Can you reproduce it reliably?
Loading…

Still need to ship something?

×6

Another batch ranked right after the header list — different links, same matching logic.

Back to top recommendations

TRENDING