Skip to content

Compaction/Safeguard: preserve recent turns verbatim#25554

Merged
jalehman merged 9 commits intoopenclaw:mainfrom
rodrigouroz:codex/pr20038-02
Mar 3, 2026
Merged

Compaction/Safeguard: preserve recent turns verbatim#25554
jalehman merged 9 commits intoopenclaw:mainfrom
rodrigouroz:codex/pr20038-02

Conversation

@rodrigouroz
Copy link
Copy Markdown
Contributor

@rodrigouroz rodrigouroz commented Feb 24, 2026

Summary

Describe the problem and fix in 2–5 bullets:

  • Problem: safeguard compaction could lose exact phrasing from the freshest user/assistant turns.
  • Why it matters: immediate follow-up turns after compaction can miss critical recent context.
  • What changed: safeguard compaction now splits and preserves a configurable-count tail of recent user/assistant turns verbatim; added helper tests and runtime field.
  • What did NOT change (scope boundary): no structured heading enforcement, no quality-audit retry loop, no config-schema exposure yet.

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

  • Safeguard summaries include a "Recent turns preserved verbatim" section with the newest user/assistant turns.

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 tests)
  • Integration/channel (if any): n/a
  • Relevant config (redacted): safeguard runtime defaults

Steps

  1. pnpm vitest run src/agents/pi-extensions/compaction-safeguard.test.ts
  2. pnpm tsgo
  3. Inspect helper behavior for preserve-count clamping and tail extraction.

Expected

  • Safeguard helper tests pass and preserved-turn section is generated correctly.

Actual

  • Local tests passed; typecheck 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: newest user/assistant turns are excluded from summarization input and appended verbatim to output.
  • Edge cases checked: preserve count clamp behavior (<0, unset, and very high values).
  • What you did not verify: provider-specific long-conversation live runs.

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: revert this PR.
  • Files/config to restore: src/agents/pi-extensions/compaction-safeguard.ts, src/agents/pi-extensions/compaction-safeguard-runtime.ts
  • Known bad symptoms reviewers should watch for: larger-than-expected safeguard summaries due to preserved tail.

Risks and Mitigations

List only real risks for this PR. Add/remove entries as needed. If none, write None.

  • Risk: preserving verbatim turns may slightly increase summary size.
    • Mitigation: clamp and truncation bounds are enforced in helpers and covered by tests.

Stack: 2/9, depends on #25553.

@rodrigouroz rodrigouroz force-pushed the codex/pr20038-02 branch 2 times, most recently from e6f5191 to c4188db Compare February 24, 2026 15:38
@openclaw-barnacle openclaw-barnacle bot added the app: web-ui App: web-ui label Feb 24, 2026
@rodrigouroz rodrigouroz force-pushed the codex/pr20038-02 branch 5 times, most recently from 006446c to 5ec8a8a Compare February 24, 2026 16:39
@rodrigouroz rodrigouroz marked this pull request as draft February 24, 2026 16:59
@openclaw-barnacle openclaw-barnacle bot removed the app: web-ui App: web-ui label Feb 26, 2026
@rodrigouroz rodrigouroz marked this pull request as ready for review February 27, 2026 14:46
@rodrigouroz
Copy link
Copy Markdown
Contributor Author

@greptileai please review and update PR description

Copy link
Copy Markdown

@chatgpt-codex-connector chatgpt-codex-connector bot left a comment

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: cc02d4e70d

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@greptile-apps
Copy link
Copy Markdown
Contributor

greptile-apps bot commented Feb 27, 2026

Greptile Summary

This PR adds functionality to preserve recent user/assistant turns verbatim during safeguard compaction, addressing the problem where exact phrasing from the freshest turns could be lost. The implementation correctly splits out a configurable number of recent turns (default 3, max 12) before summarization and appends them to the final summary.

Key changes:

  • Added recentTurnsPreserve runtime configuration field with proper clamping (0-12 turns)
  • Implemented helper functions (splitPreservedRecentTurns, formatPreservedTurnsSection) with defensive type guards and bounds checking
  • Preserved turns are excluded from summarization input and appended verbatim under "Recent turns preserved verbatim" section
  • Added comprehensive tests covering main scenarios and edge cases (clamping, empty messages, split behavior)

Code quality:

  • Proper TypeScript typing with no any usage
  • Defensive programming with fallback values and type guards
  • Clean separation of concerns with focused helper functions
  • Tests verify both happy path and edge cases

Confidence Score: 5/5

  • This PR is safe to merge with minimal risk
  • The implementation is well-tested, handles edge cases properly, follows repository conventions, and has no logical errors or security issues. The changes are backward compatible and isolated to the compaction safeguard extension.
  • No files require special attention

Last reviewed commit: cc02d4e

@openclaw-barnacle openclaw-barnacle bot removed docs Improvements or additions to documentation scripts Repository scripts commands Command implementations labels Feb 27, 2026
@rodrigouroz
Copy link
Copy Markdown
Contributor Author

@jalehman Hey Josh, I have several follow up PRs chained on top of this one. Would you like to review / test them all at once? Or do you prefer this approach?
Thank you

@jalehman jalehman merged commit c8b45a4 into openclaw:main Mar 3, 2026
6 checks passed
@jalehman
Copy link
Copy Markdown
Contributor

jalehman commented Mar 3, 2026

Merged via squash.

Thanks @rodrigouroz!

@jalehman
Copy link
Copy Markdown
Contributor

jalehman commented Mar 3, 2026

@jalehman Hey Josh, I have several follow up PRs chained on top of this one. Would you like to review / test them all at once? Or do you prefer this approach?

Individual PRs as you have them are fine. I'll add your others to my queue. Thank you!

dawi369 pushed a commit to dawi369/davis that referenced this pull request Mar 3, 2026
Merged via squash.

Prepared head SHA: 7fb33c4
Co-authored-by: rodrigouroz <[email protected]>
Co-authored-by: jalehman <[email protected]>
Reviewed-by: @jalehman