Skip to content

fix: disable Pi auto-compaction when safeguard mode is active#73839

Merged
jalehman merged 2 commits into
openclaw:mainfrom
bradhallett:fix/safeguard-pi-compaction-guard
May 6, 2026
Merged

fix: disable Pi auto-compaction when safeguard mode is active#73839
jalehman merged 2 commits into
openclaw:mainfrom
bradhallett:fix/safeguard-pi-compaction-guard

Conversation

@bradhallett

@bradhallett bradhallett commented Apr 28, 2026

Copy link
Copy Markdown
Contributor

Summary

Fixes #73003

When compaction.mode = "safeguard", OpenClaw's safeguard compaction is permanently blocked by Pi SDK's internal auto-compaction. The two systems operate on different thresholds with no coordination, causing consecutive already_compacted_recently failures and uncontrolled session growth.

Root Cause

shouldDisablePiAutoCompaction() only disables Pi auto-compaction when a context engine declares ownsCompaction === true. Safeguard mode has no context engine, so Pi auto-compaction remains active and conflicts with OpenClaw's own compaction lifecycle.

Changes

src/agents/pi-settings.ts — Extend shouldDisablePiAutoCompaction() to accept compactionMode and return true when "safeguard". Thread the new param through applyPiAutoCompactionGuard().

src/agents/pi-embedded-runner/run/attempt.ts — Pass compactionMode from config to the guard.

src/agents/command/cli-compaction.ts — Same for the CLI compaction path.

src/agents/pi-settings.test.ts — Add 13 new tests:

  • shouldDisablePiAutoCompaction: 6 tests (false by default, true for safeguard, true for ownsCompaction, both, false for other modes)
  • applyPiAutoCompactionGuard: 3 tests (disables for safeguard, no-op for default, unsupported without setCompactionEnabled)
  • Existing 16 tests unchanged and still passing

Test plan

Real behavior proof

  • Behavior or issue addressed: In safeguard compaction mode, OpenClaw owns the compaction lifecycle. Pi's internal auto-compaction must be disabled so it cannot compact agent.state.messages underneath OpenClaw and trigger the already_compacted_recently cascade from [Bug]: Safeguard compaction permanently blocked by Pi SDK auto-compaction (consecutive failures) #73003.
  • Real environment tested: Clean Blacksmith Testbox tbx_01kqxfh7kn5qnzcx5q623k9hkm on .github/workflows/ci-check-testbox.yml, with a fresh checkout and a temp config using agents.defaults.compaction.mode = "safeguard" and plugins.load.paths = [] so the local Lossless Claw/context-engine setup is not involved.
  • Exact steps or command run after this patch: Pushed PR head a2e2cb7204709973c90455ce5a59a7d4143deea7 to jalehman/clawdbot:test/pr-73839-real-proof, then ran a Testbox script that fetched both origin/main and that PR head and executed the same probe against the real OpenClaw/Pi createPreparedEmbeddedPiSettingsManager, applyPiAutoCompactionGuard, and DefaultResourceLoader.reload() path.
  • Evidence after fix: Copied live Testbox terminal output from /tmp/pr73839-real-proof-testbox.log:
TESTBOX=ip-172-31-64-123
START=2026-05-06T01:51:00Z
PWD=/home/runner/_work/openclaw/openclaw

CHECKOUT main origin/main
--- main ---
head=82942295921e
plugins.load.paths=[]
effectiveCompactionMode=safeguard
hasApplyPiAutoCompactionGuard=true
shouldDisablePiAutoCompaction=function-missing-on-this-branch
beforeGuard.compaction.enabled=true
afterGuard.result={"supported":true,"disabled":false}
afterGuard.compaction.enabled=true
afterResourceLoaderReload.compaction.enabled=true
afterReapplyGuard.result={"supported":true,"disabled":false}
afterReapplyGuard.compaction.enabled=true

CHECKOUT pr FETCH_HEAD
--- pr ---
head=a2e2cb720470
plugins.load.paths=[]
effectiveCompactionMode=safeguard
hasApplyPiAutoCompactionGuard=true
shouldDisablePiAutoCompaction=true
beforeGuard.compaction.enabled=true
afterGuard.result={"supported":true,"disabled":true}
afterGuard.compaction.enabled=false
afterResourceLoaderReload.compaction.enabled=false
afterReapplyGuard.result={"supported":true,"disabled":true}
afterReapplyGuard.compaction.enabled=false

LOG=/tmp/pr73839-real-proof-testbox.log
END=2026-05-06T01:51:07Z
  • Observed result after fix: On current main, safeguard mode is active but Pi auto-compaction remains enabled through the guard/reload path. On the PR head, the same clean Testbox setup resolves safeguard mode, disables Pi auto-compaction, and keeps it disabled after DefaultResourceLoader.reload() plus guard re-application. That verifies the patch changes the real runtime behavior that caused OpenClaw safeguard compaction to fight Pi's auto-compaction.
  • What was not tested: Did not run a provider-backed multi-turn session long enough to reproduce the full historical already_compacted_recently cascade end-to-end; the Testbox proof targets the direct real runtime switch that prevents Pi's competing compaction path from being armed in safeguard mode.

@openclaw-barnacle openclaw-barnacle Bot added agents Agent runtime and tooling size: S labels Apr 28, 2026
@greptile-apps

greptile-apps Bot commented Apr 28, 2026

Copy link
Copy Markdown
Contributor

Greptile Summary

This PR extends shouldDisablePiAutoCompaction and applyPiAutoCompactionGuard to accept a compactionMode parameter and return true when "safeguard" is active, preventing Pi's internal auto-compaction from conflicting with OpenClaw's safeguard compaction lifecycle.

  • The CliCompactionDeps interface's applyPiAutoCompactionGuard type was not updated to include compactionMode, so the call site at cli-compaction.ts:206 passes an undeclared property — this will fail TypeScript's excess-property check and break the build on that path.

Confidence Score: 3/5

Not safe to merge until the CliCompactionDeps interface is updated; the missing compactionMode field causes a TypeScript compile error on the CLI path.

One P1 finding: the CliCompactionDeps type was not updated to reflect the new compactionMode parameter, which will produce a TypeScript excess-property error at the cli-compaction call site. The fix is a one-line change but blocks the build.

src/agents/command/cli-compaction.ts — CliCompactionDeps.applyPiAutoCompactionGuard type is missing the compactionMode field

Comments Outside Diff (1)

  1. src/agents/command/cli-compaction.ts, line 38-41 (link)

    P1 CliCompactionDeps type not updated for compactionMode

    The applyPiAutoCompactionGuard entry in CliCompactionDeps still only declares settingsManager and contextEngineInfo. When the call site at line 206 passes compactionMode, TypeScript's excess-property check will raise a compile error (Object literal may only specify known properties, and 'compactionMode' does not exist in type '...'). The new parameter needs to be added to the interface.

    Prompt To Fix With AI
    This is a comment left during a code review.
    Path: src/agents/command/cli-compaction.ts
    Line: 38-41
    
    Comment:
    **`CliCompactionDeps` type not updated for `compactionMode`**
    
    The `applyPiAutoCompactionGuard` entry in `CliCompactionDeps` still only declares `settingsManager` and `contextEngineInfo`. When the call site at line 206 passes `compactionMode`, TypeScript's excess-property check will raise a compile error (`Object literal may only specify known properties, and 'compactionMode' does not exist in type '...'`). The new parameter needs to be added to the interface.
    
    
    
    How can I resolve this? If you propose a fix, please make it concise.
Prompt To Fix All With AI
This is a comment left during a code review.
Path: src/agents/command/cli-compaction.ts
Line: 38-41

Comment:
**`CliCompactionDeps` type not updated for `compactionMode`**

The `applyPiAutoCompactionGuard` entry in `CliCompactionDeps` still only declares `settingsManager` and `contextEngineInfo`. When the call site at line 206 passes `compactionMode`, TypeScript's excess-property check will raise a compile error (`Object literal may only specify known properties, and 'compactionMode' does not exist in type '...'`). The new parameter needs to be added to the interface.

```suggestion
  applyPiAutoCompactionGuard: (params: {
    settingsManager: SettingsManagerLike;
    contextEngineInfo?: ContextEngine["info"];
    compactionMode?: string;
  }) => unknown;
```

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

---

This is a comment left during a code review.
Path: src/agents/pi-settings.ts
Line: 128-130

Comment:
**Use the existing `AgentCompactionMode` type instead of `string`**

`src/config/types.agent-defaults.ts` already exports `AgentCompactionMode = "default" | "safeguard"`. Using the narrow union type here (and in `applyPiAutoCompactionGuard`'s signature) would catch invalid mode strings at compile time and make it obvious that `"off"` is not a real compaction mode value.

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

Reviews (1): Last reviewed commit: "fix: disable Pi auto-compaction when saf..." | Re-trigger Greptile

Comment thread src/agents/pi-settings.ts Outdated
Comment on lines +128 to +130
/** When "safeguard", OpenClaw owns the compaction lifecycle and Pi auto-compaction
* must be disabled to prevent threshold conflicts (see #73003). */
compactionMode?: string;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

P2 Use the existing AgentCompactionMode type instead of string

src/config/types.agent-defaults.ts already exports AgentCompactionMode = "default" | "safeguard". Using the narrow union type here (and in applyPiAutoCompactionGuard's signature) would catch invalid mode strings at compile time and make it obvious that "off" is not a real compaction mode value.

Prompt To Fix With AI
This is a comment left during a code review.
Path: src/agents/pi-settings.ts
Line: 128-130

Comment:
**Use the existing `AgentCompactionMode` type instead of `string`**

`src/config/types.agent-defaults.ts` already exports `AgentCompactionMode = "default" | "safeguard"`. Using the narrow union type here (and in `applyPiAutoCompactionGuard`'s signature) would catch invalid mode strings at compile time and make it obvious that `"off"` is not a real compaction mode value.

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

@clawsweeper

clawsweeper Bot commented Apr 28, 2026

Copy link
Copy Markdown
Contributor

Codex review: needs changes before merge.

Summary
The PR adds effective safeguard-mode detection to Pi auto-compaction guard wiring for embedded and CLI compaction paths, adds focused tests, and updates the changelog.

Reproducibility: yes. Current-main source shows safeguard mode is not passed into the Pi auto-compaction guard, and the PR body supplies a Testbox probe showing the enabled-versus-disabled runtime switch.

Real behavior proof
Sufficient (terminal): The PR body includes after-fix Testbox terminal output showing the real OpenClaw/Pi settings and resource-loader path disables Pi compaction in safeguard mode and keeps it disabled after reload.

Next step before merge
A single mechanical changelog correction is needed before normal PR merge review can continue.

Security
Cleared: The diff is limited to TypeScript compaction guard wiring, focused tests, and changelog text with no dependency, workflow, lockfile, script, secret-handling, or publishing changes.

Review findings

  • [P2] Restore the escaped dot in the existing changelog example — CHANGELOG.md:394
Review details

Best possible solution:

Restore the unrelated changelog escape change, keep the narrow safeguard guard implementation, rerun the affected checks, and land it as the fix for the linked safeguard compaction bug.

Do we have a high-confidence way to reproduce the issue?

Yes. Current-main source shows safeguard mode is not passed into the Pi auto-compaction guard, and the PR body supplies a Testbox probe showing the enabled-versus-disabled runtime switch.

Is this the best way to solve the issue?

Yes for the functional bug: extending the existing guard preserves default-mode Pi behavior while disabling the competing Pi lifecycle when OpenClaw owns safeguard compaction. The only safer adjustment is to revert the unrelated changelog escape change.

Full review comments:

  • [P2] Restore the escaped dot in the existing changelog example — CHANGELOG.md:394
    This changes the unrelated Exec approvals entry from ^safe\\.py$ to ^safe\.py$. In a JSON/config string, \. is not a valid escape and no longer represents the original literal-dot regex example; keep the existing double backslash while adding this PR's compaction note.
    Confidence: 0.88

Overall correctness: patch is incorrect
Overall confidence: 0.88

Acceptance criteria:

  • git diff --check

What I checked:

  • Current main guard gap: Current main only disables Pi auto-compaction when a context engine owns compaction or the provider is silent-overflow-prone; safeguard compaction mode is not part of the predicate. (src/agents/pi-settings.ts:178, 8b9b849b19c4)
  • Current main runtime wiring: The embedded runner builds Pi auto-compaction guard args with settingsManager, contextEngineInfo, and silentOverflowProneProvider, but no compaction mode, then reapplies the same guard after DefaultResourceLoader.reload(). (src/agents/pi-embedded-runner/run/attempt.ts:1453, 8b9b849b19c4)
  • Documented safeguard ownership: The compaction docs state that setting a compaction provider forces mode "safeguard", making provider-backed safeguard part of the owned compaction lifecycle this guard should protect. Public docs: docs/reference/session-management-compaction.md. (docs/reference/session-management-compaction.md:355, 8b9b849b19c4)
  • PR implementation path: The PR adds resolveEffectiveCompactionMode(), makes shouldDisablePiAutoCompaction true for effective safeguard mode, and passes that mode from embedded and CLI guard call sites. (src/agents/pi-settings.ts:125, 460615fe7026)
  • Real behavior proof: The PR body includes copied Testbox terminal output showing current main keeps Pi compaction enabled in safeguard mode, while the PR head disables it and keeps it disabled after DefaultResourceLoader.reload() plus guard reapplication. (a2e2cb720470)
  • Introduced changelog regression: The latest commit is changelog-only and changes an existing Exec approvals example from the valid displayed JSON regex string ^safe\.py$ to ^safe.py$, while adding the new compaction entry. (CHANGELOG.md:394, 460615fe7026)

Likely related people:

  • openperf: Introduced the current Pi auto-compaction guard surface for z.ai-style provider runs in pi-settings and embedded-runner code. (role: introduced behavior; confidence: high; commits: cc8a8f1df1cd; files: src/agents/pi-settings.ts, src/agents/pi-embedded-runner/run/attempt.ts, src/agents/pi-embedded-runner/compact.ts)
  • jalehman: Introduced custom context-engine and ownsCompaction plumbing that the existing guard keys off, and later committed the current PR's latest changelog-only head. (role: introduced behavior and adjacent maintainer; confidence: high; commits: fee91fefceb4, 460615fe7026; files: src/context-engine/types.ts, src/agents/pi-settings.ts, src/agents/pi-embedded-runner/run/attempt.ts)
  • DhruvBhatia0: Added the pluggable compaction provider path whose configured provider forces safeguard mode, which this PR includes in effective-mode handling. (role: adjacent owner; confidence: medium; commits: 12331f04631f; files: src/plugins/compaction-provider.ts, src/agents/pi-embedded-runner/extensions.ts, docs/reference/session-management-compaction.md)
  • obviyus: Introduced the persisted CLI compaction lifecycle file touched by the PR's CLI guard wiring. (role: adjacent owner; confidence: medium; commits: dfd5940c3491; files: src/agents/command/cli-compaction.ts, src/agents/agent-command.ts)

Codex review notes: model gpt-5.5, reasoning high; reviewed against 8b9b849b19c4.

Re-review progress:

@jalehman jalehman force-pushed the fix/safeguard-pi-compaction-guard branch from d8792c9 to a2e2cb7 Compare May 5, 2026 23:47
@openclaw-barnacle openclaw-barnacle Bot added triage: needs-real-behavior-proof Candidate: external PR needs after-fix proof from a real setup. proof: supplied External PR includes structured after-fix real behavior proof. and removed triage: needs-real-behavior-proof Candidate: external PR needs after-fix proof from a real setup. labels May 5, 2026
@clawsweeper clawsweeper Bot added the proof: sufficient ClawSweeper judged the real behavior proof convincing. label May 5, 2026
@openclaw-barnacle openclaw-barnacle Bot removed the proof: sufficient ClawSweeper judged the real behavior proof convincing. label May 6, 2026
@clawsweeper clawsweeper Bot added the proof: sufficient ClawSweeper judged the real behavior proof convincing. label May 6, 2026
@jalehman jalehman force-pushed the fix/safeguard-pi-compaction-guard branch from a2e2cb7 to 460615f Compare May 6, 2026 02:03
@openclaw-barnacle openclaw-barnacle Bot removed the proof: sufficient ClawSweeper judged the real behavior proof convincing. label May 6, 2026
@clawsweeper clawsweeper Bot added the proof: sufficient ClawSweeper judged the real behavior proof convincing. label May 6, 2026
Brad Hallett and others added 2 commits May 5, 2026 19:31
…aw#73003)

When compaction.mode = "safeguard", OpenClaw's compaction is permanently
blocked by Pi SDK's internal auto-compaction. The two systems operate on
different thresholds and do not coordinate, causing consecutive
already_compacted_recently failures and uncontrolled session growth.

Extend shouldDisablePiAutoCompaction() to recognize safeguard mode in
addition to context engines that own compaction. Pass compactionMode from
both call sites (attempt.ts, cli-compaction.ts).

Fixes openclaw#73003
@jalehman jalehman force-pushed the fix/safeguard-pi-compaction-guard branch from 460615f to d554201 Compare May 6, 2026 02:32
@openclaw-barnacle openclaw-barnacle Bot removed the proof: sufficient ClawSweeper judged the real behavior proof convincing. label May 6, 2026
@jalehman jalehman merged commit 0bdba47 into openclaw:main May 6, 2026
108 of 110 checks passed
@jalehman

jalehman commented May 6, 2026

Copy link
Copy Markdown
Contributor

Merged via squash.

Thanks @bradhallett!

github-actions Bot pushed a commit to Desicool/openclaw that referenced this pull request May 9, 2026
…aw#73839)

Merged via squash.

Prepared head SHA: d554201
Co-authored-by: bradhallett <53977268+bradhallett@users.noreply.github.com>
Co-authored-by: jalehman <550978+jalehman@users.noreply.github.com>
Reviewed-by: @jalehman
github-actions Bot pushed a commit to Desicool/openclaw that referenced this pull request May 24, 2026
…aw#73839)

Merged via squash.

Prepared head SHA: d554201
Co-authored-by: bradhallett <53977268+bradhallett@users.noreply.github.com>
Co-authored-by: jalehman <550978+jalehman@users.noreply.github.com>
Reviewed-by: @jalehman
jameslcowan pushed a commit to jameslcowan/openclaw that referenced this pull request Jun 2, 2026
…aw#73839)

Merged via squash.

Prepared head SHA: d554201
Co-authored-by: bradhallett <53977268+bradhallett@users.noreply.github.com>
Co-authored-by: jalehman <550978+jalehman@users.noreply.github.com>
Reviewed-by: @jalehman
sablehead pushed a commit to sablehead/openclaw that referenced this pull request Jun 10, 2026
…aw#73839)

Merged via squash.

Prepared head SHA: d554201
Co-authored-by: bradhallett <53977268+bradhallett@users.noreply.github.com>
Co-authored-by: jalehman <550978+jalehman@users.noreply.github.com>
Reviewed-by: @jalehman
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

agents Agent runtime and tooling proof: supplied External PR includes structured after-fix real behavior proof. size: S

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: Safeguard compaction permanently blocked by Pi SDK auto-compaction (consecutive failures)

2 participants