Skip to content

(fix): Compaction: preserve recent context and sync session memory post-compact#20038

Closed
rodrigouroz wants to merge 2 commits intoopenclaw:mainfrom
rodrigouroz:fix/compaction-safeguard-single-commit
Closed

(fix): Compaction: preserve recent context and sync session memory post-compact#20038
rodrigouroz wants to merge 2 commits intoopenclaw:mainfrom
rodrigouroz:fix/compaction-safeguard-single-commit

Conversation

@rodrigouroz
Copy link
Contributor

@rodrigouroz rodrigouroz commented Feb 18, 2026

Summary

  • Problem: post-compaction sessions could drift because summaries were under-structured, recent turns were easy to lose, and session-memory indexing could stay stale after compaction.
  • Why it matters: users see "forgotten context" right after compaction, especially when relying on memory search over session transcripts.
  • What changed: added post-compaction transcript update + configurable index sync, safeguard summary structure/quality checks with retry, and preserved recent verbatim turns in safeguard compaction.
  • What did NOT change (scope boundary): no model/provider changes, no messaging/channel routing changes, no non-compaction memory backend redesign.

Change Type (select all)

  • Bug fix
  • Feature
  • Refactor
  • Docs
  • Security hardening
  • Chore/infra

Scope (select all touched areas)

  • Gateway / orchestration
  • Skills / tool execution
  • Auth / tokens
  • Memory / storage
  • Integrations
  • API / contracts
  • UI / DX
  • CI/CD / infra

Linked Issue/PR

User-visible / Behavior Changes

  • New compaction settings:
    • agents.defaults.compaction.recentTurnsPreserve
    • agents.defaults.compaction.postIndexSync (off|async|await)
    • agents.defaults.compaction.qualityGuard.enabled
    • agents.defaults.compaction.qualityGuard.maxRetries
  • New memory-search setting:
    • agents.defaults.memorySearch.sync.sessions.postCompactionForce
  • After successful compaction, session transcript updates are emitted and session-memory indexing can be synced immediately depending on config.
  • Safeguard compaction now preserves recent turns, enforces structured summary sections, and retries once (configurable) when summary quality checks fail.

Security Impact (required)

  • New permissions/capabilities? (No)
  • Secrets/tokens handling changed? (No)
  • New/changed network calls? (No)
  • Command/tool execution surface changed? (No)
  • Data access scope changed? (No)
  • If any Yes, explain risk + mitigation:

Repro + Verification

Environment

  • OS: macOS
  • Runtime/container: Node 22 + pnpm
  • Model/provider: n/a (unit/integration tests)
  • Integration/channel (if any): n/a
  • Relevant config (redacted): default + compaction/memory overrides in tests

Steps

  1. Run typecheck: pnpm tsgo.
  2. Run compaction/memory/reply targeted suites:
    • pnpm vitest run src/agents/compaction.retry.test.ts src/agents/compaction.identifier-preservation.test.ts src/agents/pi-embedded-runner/run.overflow-compaction.test.ts src/auto-reply/reply/post-compaction-context.test.ts src/auto-reply/reply/post-compaction-audit.test.ts src/memory/manager.async-search.test.ts src/memory/manager.watcher-config.test.ts src/memory/index.test.ts src/memory/manager.atomic-reindex.test.ts
  3. Run reply-runner suites:
    • pnpm vitest run src/auto-reply/reply/followup-runner.test.ts src/auto-reply/reply/agent-runner.runreplyagent.test.ts src/auto-reply/reply/commands.test.ts src/auto-reply/reply/agent-runner.misc.runreplyagent.test.ts

Expected

  • Typecheck passes.
  • All targeted compaction/memory/reply tests pass.

Actual

  • pnpm tsgo passed.
  • First vitest batch: 9 files / 47 tests passed.
  • Second vitest batch: 4 files / 110 tests passed.

Evidence

Attach at least one:

  • Failing test/log before + passing after
  • Trace/log snippets
  • Screenshot/recording
  • Perf numbers (if relevant)

Human Verification (required)

What you personally verified (not just CI), and how:

  • Verified scenarios:
    • Post-compaction transcript update is emitted and post-sync logic runs from compaction path.
    • Safeguard compaction includes structured sections + preserved recent turns + quality audit retry path.
    • Config/schema/type surfaces validate and compile.
  • Edge cases checked:
    • Post-compaction memory sync can be disabled (off) and supports async/await modes.
    • Missing/unavailable memory manager is handled safely (best effort, warning only).
  • What you did not verify:
    • End-to-end live provider behavior under real long-running conversations.
    • Full pnpm test matrix.

Compatibility / Migration

  • Backward compatible? (Yes)
  • Config/env changes? (No)
  • Migration needed? (No)
  • If yes, exact upgrade steps:

Failure Recovery (if this breaks)

  • How to disable/revert this change quickly:
    • Set agents.defaults.compaction.postIndexSync: "off" to disable post-compaction index sync.
    • Set agents.defaults.compaction.qualityGuard.enabled: false to disable quality-guard retries.
    • Set agents.defaults.compaction.recentTurnsPreserve: 0 to disable preserved-tail behavior.
  • Files/config to restore:
    • Revert the compaction + memory config changes in openclaw.json.
  • Known bad symptoms reviewers should watch for:
    • Slower compaction in await mode due to blocking index sync.
    • Unexpected summary verbosity if recent-turn preservation is set too high.

Risks and Mitigations

  • Risk: Post-compaction await mode may increase latency for replies right after compaction.
    • Mitigation: default is async; off and async remain available.
  • Risk: Quality-guard retries could increase compaction token/cost usage.
    • Mitigation: default retry cap is low and configurable; guard can be disabled.

Greptile Summary

This PR enhances compaction reliability by preserving recent context and syncing session memory post-compaction. The implementation adds structured summary quality checks with retry logic, verbatim preservation of recent turns, and configurable memory index syncing.

Key changes:

  • Added quality guard with configurable retries to enforce structured summary sections and identifier preservation
  • Preserved recent user/assistant turns verbatim in compaction output (configurable, default 3 turns)
  • Added post-compaction transcript update emission and memory index sync with three modes: off, async (default), and await
  • Enhanced identifier preservation instructions for all compaction paths
  • Comprehensive test coverage for new features

The code follows project conventions, includes proper error handling with best-effort fallbacks, and provides escape hatches via configuration for all new behaviors.

Confidence Score: 4/5

  • This PR is safe to merge with low risk - well-tested changes that enhance compaction reliability with proper fallbacks
  • Score reflects comprehensive test coverage (47 compaction/memory tests + 110 reply tests passing), proper error handling with best-effort fallbacks for missing managers, backward-compatible config with sensible defaults, and configurable escape hatches for all new behaviors. The redundant call issue mentioned in previous comments has been addressed. Minor deduction for the increased complexity of the retry loop and potential latency impact in await mode, though both are mitigated by defaults (async mode, low retry cap, configurable).
  • No files require special attention - the implementation is well-structured with appropriate error handling throughout

Last reviewed commit: f972d80

Copy link
Contributor

@greptile-apps greptile-apps bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

16 files reviewed, 2 comments

Edit Code Review Agent Settings | Greptile

Comment on lines +298 to +302
function extractOpaqueIdentifiers(text: string): string[] {
const matches =
text.match(
/([a-f0-9]{8,}|https?:\/\/\S+|\/[\w./-]+|[A-Za-z]:\\[\w\\.-]+|[A-Za-z0-9._-]+\.[A-Za-z0-9._/-]+:\d{1,5}|\b\d{6,}\b)/g,
) ?? [];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hex pattern only matches lowercase

The regex [a-f0-9]{8,} only matches lowercase hex characters. Identifiers containing uppercase hex digits (e.g., A1B2C3D4E5F6 or mixed-case UUIDs like A1b2C3d4E5f6G7h8) will not be extracted by this pattern. If the codebase deals with identifiers in any casing, consider using [a-fA-F0-9]{8,} or adding the case-insensitive flag to this alternative.

This is mitigated by the fact that the LLM is separately instructed to preserve identifiers verbatim, so this regex serves as a quality audit rather than the sole mechanism. Still worth noting since it could cause false-negative audit failures (missing identifiers → unnecessary retry).

Prompt To Fix With AI
This is a comment left during a code review.
Path: src/agents/pi-extensions/compaction-safeguard.ts
Line: 298-302

Comment:
**Hex pattern only matches lowercase**

The regex `[a-f0-9]{8,}` only matches lowercase hex characters. Identifiers containing uppercase hex digits (e.g., `A1B2C3D4E5F6` or mixed-case UUIDs like `A1b2C3d4E5f6G7h8`) will not be extracted by this pattern. If the codebase deals with identifiers in any casing, consider using `[a-fA-F0-9]{8,}` or adding the case-insensitive flag to this alternative.

This is mitigated by the fact that the LLM is separately instructed to preserve identifiers verbatim, so this regex serves as a quality audit rather than the sole mechanism. Still worth noting since it could cause false-negative audit failures (missing identifiers → unnecessary retry).

How can I resolve this? If you propose a fix, please make it concise.

@rodrigouroz rodrigouroz force-pushed the fix/compaction-safeguard-single-commit branch from 8f870ee to 9a0520a Compare February 18, 2026 13:21
@rodrigouroz rodrigouroz force-pushed the fix/compaction-safeguard-single-commit branch from 77e9ad6 to f972d80 Compare February 23, 2026 16:14
@rodrigouroz
Copy link
Contributor Author

@greptileai can you update your review on this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

agents Agent runtime and tooling size: L

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: Compaction summarization does not preserve exact identifiers

1 participant