Skip to content

feat: triple-layer post-compaction context enforcement#18049

Merged
steipete merged 7 commits intoopenclaw:mainfrom
irchelper:feat/post-compaction-context-inject
Feb 16, 2026
Merged

feat: triple-layer post-compaction context enforcement#18049
steipete merged 7 commits intoopenclaw:mainfrom
irchelper:feat/post-compaction-context-inject

Conversation

@irchelper
Copy link

@irchelper irchelper commented Feb 16, 2026

After session compaction, agents often skip their configured startup sequence because the compaction summary creates a false 'I already know everything' illusion.

This PR implements triple-layer enforcement to ensure post-compaction recovery:

Layer 1: Summary Append

Append critical workspace rules (AGENTS.md Session Startup + Red Lines) into the compaction summary via <workspace-critical-rules> tags. 100% reliable — rules are physically in the prompt.

Layer 2: System Event

Inject workspace context as a system event for the next agent turn after compaction. Explicit reminder to execute startup sequence.

Layer 3: Post-Turn Audit

After the agent's first post-compaction turn, audit whether required files were actually read. If not, inject a one-time warning listing missing files. Max 1 retry to prevent loops.

Changes

  • src/agents/pi-extensions/compaction-safeguard.ts — Layer 1: append workspace context to summary
  • src/auto-reply/reply/agent-runner.ts — Layer 2 + 3: system event injection + audit integration
  • src/auto-reply/reply/post-compaction-context.ts — reads and formats AGENTS.md sections
  • src/auto-reply/reply/post-compaction-audit.ts — audits post-compaction file reads
  • src/auto-reply/reply/post-compaction-context.test.ts — tests for context extraction
  • src/auto-reply/reply/post-compaction-audit.test.ts — tests for read audit

Design decisions

  • Reuses existing enqueueSystemEvent() infrastructure
  • Only activates if workspace has AGENTS.md
  • extractSections handles H2/H3, case-insensitive, code-block aware
  • Truncation limits prevent token explosion / Yo-Yo re-compaction
  • Silent failure on errors (best-effort)
  • Audit fires at most once per compaction (no infinite loops)
  • Cross-platform path normalization (forward-slash unification) for Windows compatibility in audit layer

Fixes #18023

康熙 added 3 commits February 16, 2026 20:44
- Add readWorkspaceContextForSummary() to extract Session Startup + Red Lines from AGENTS.md
- Inject workspace context into compaction summary (limited to 2000 chars)
- Export extractSections() from post-compaction-context.ts for reuse
- Ensures compaction summary includes core rules needed for recovery

Part 1 of post-compaction context injection feature.
@openclaw-barnacle openclaw-barnacle bot added the agents Agent runtime and tooling label Feb 16, 2026
@irchelper irchelper changed the title feat: inject post-compaction workspace context as system event feat: dual-layer post-compaction context enforcement (summary append + system event) Feb 16, 2026
@irchelper irchelper changed the title feat: dual-layer post-compaction context enforcement (summary append + system event) feat: triple-layer post-compaction context enforcement Feb 16, 2026
Windows path.relative() produces backslashes (e.g., memory\2026-02-16.md)
which fail to match RegExp patterns using forward slashes.

Normalize relative paths to forward slashes before RegExp matching
using rel.split(path.sep).join('/').

Fixes 4 test failures on Windows CI.
@steipete steipete merged commit 65a1787 into openclaw:main Feb 16, 2026
23 checks passed
@irchelper irchelper deleted the feat/post-compaction-context-inject branch February 17, 2026 00:43
@steipete
Copy link
Contributor

acciedentially merged this but it's problematic:

  1. High – wrong workspace root used for post-compaction context/audit
    • src/auto-reply/reply/agent-runner.ts:564 reads workspaceDir =
      process.cwd() before enqueuing context.
    • src/auto-reply/reply/agent-runner.ts:599 reuses process.cwd()
      for layer-3 audit.
    • followupRun.run.workspaceDir is already passed through (src/
      auto-reply/reply/get-reply-run.ts:413), so this should be
      authoritative.
    • Why this is a problem: process.cwd() is process-global and does
      not follow per-session workspace context. In multi-workspace or
      non-default-cwd scenarios, it can point at the wrong repo. That
      means:
      • layer-1/2 context injection can miss the correct AGENTS.md,
        and
      • layer-3 audit can validate reads against the wrong
        workspace.
    • Impact: enforcement can silently not run for the intended
      workspace.
  2. Medium – post-compaction audit can pass/fail on stale transcript
    data
    • src/auto-reply/reply/post-compaction-audit.ts:50-62 reads the
      last 100 lines of the entire session transcript and scans all
      assistant read tool calls.
    • src/agents/pi-extensions/compaction-safeguard.ts compaction
      occurs by summarization, not by truncating transcript history.
    • Why this is a problem: the “first turn after compaction” check
      is meant to validate startup reads in the turn immediately after
      reset, but current logic can include pre-compaction reads still
      present in transcript history (false pass), and can also miss
      late/older reads if windowing shifts (false fail/noise).
    • Impact: weak enforcement and inconsistent warnings in real
      sessions.

will revert

@irchelper
Copy link
Author

irchelper commented Feb 17, 2026

@steipete Hey, thanks for the detailed review on the process.cwd() and stale transcript issues — you were absolutely right about both problems.

I actually spotted these bugs shortly after submitting the PR, but I was hit by AI model rate limits at the time and couldn't get my agent system to work on fixes (I work on a strict "AI-only, zero manual coding" principle 😅).

When the rate limits finally cleared, I did a full audit:

  • Bug 2 (stale transcript): Already fixed upstream — getPostCompactionContext() now anchors from compaction summary instead of the fixed slice(-100) window. ✅
  • Bug 1 (process.cwd()): Still present in agent-runner.ts lines 564 and 599. The upstream compaction safeguard (compact.ts) uses the correct effectiveWorkspace path, so the main compaction flow is unaffected — but the post-compaction context injection in agent-runner.ts still relies on process.cwd(), which is wrong for multi-workspace / non-default-cwd scenarios.

I'm working on a fix for Bug 1 now and will submit a new PR shortly.

On a personal note — this was my very first time using GitHub to submit issues and PRs, so I'm still learning. It's also Chinese New Year right now, which added to the delay.

Since then I've set up a full HA multi-model setup with four providers (Claude, Gemini, GPT-5.x, MiniMax) with task-type routing, so I should be able to keep contributing going forward. Thank you for open-sourcing OpenClaw — it got me back into programming after 20+ years away from code. Turns out directing AI workers is a pretty fun way to build things!

@irchelper
Copy link
Author

Quick follow-up: I've submitted #18914 to fix Bug 1 (process.cwd()followupRun.run.workspaceDir).

I'm aware that upstream's compaction safeguard in compact.ts already uses the correct effectiveWorkspace path via resolveBootstrapContextForRun(), so the main compaction flow is unaffected by this bug. But the post-compaction context injection code in agent-runner.ts still had the incorrect process.cwd() calls, and I wanted to close the loop properly rather than leave known bugs in the codebase.

irchelper added a commit to irchelper/openclaw that referenced this pull request Feb 19, 2026
Add notifyOnStart and notifyOnStartText fields to AgentCompactionConfig so
users receive a message the moment auto-compaction begins, resolving the UX
problem where the interface appears frozen or shows red errors with no context.

Changes:
- src/config/types.agent-defaults.ts: add notifyOnStart (boolean) and
  notifyOnStartText (string) to AgentCompactionConfig
- src/config/zod-schema.agent-defaults.ts: add corresponding Zod fields
  inside the .strict() compaction object
- src/auto-reply/reply/agent-runner-execution.ts: add onCompactionStart
  optional callback to runAgentTurnWithFallback params; call it when the
  Pi runtime fires compaction:phase=start
- src/auto-reply/reply/agent-runner.ts: build and pass onCompactionStart
  callback that reads cfg.agents.defaults.compaction.notifyOnStart and
  delivers via opts.onBlockReply when enabled
- src/config/config.compaction-settings.test.ts: 5 new tests covering
  true/false/undefined and schema rejection of invalid types
- src/auto-reply/reply/agent-runner.compaction-notify.test.ts: 5 new
  agent-runner integration tests (default text, custom text, false, undefined,
  end-only event)
- docs/concepts/compaction.md: document the new config fields
- docs/reference/session-management-compaction.md: add implementation
  reference section

Extends openclaw#18049 (triple-layer post-compaction context enforcement).

Default: notifyOnStart is false (opt-in, no behaviour change for existing users).
irchelper added a commit to irchelper/openclaw that referenced this pull request Feb 22, 2026
Add notifyOnStart and notifyOnStartText fields to AgentCompactionConfig so
users receive a message the moment auto-compaction begins, resolving the UX
problem where the interface appears frozen or shows red errors with no context.

Changes:
- src/config/types.agent-defaults.ts: add notifyOnStart (boolean) and
  notifyOnStartText (string) to AgentCompactionConfig
- src/config/zod-schema.agent-defaults.ts: add corresponding Zod fields
  inside the .strict() compaction object
- src/auto-reply/reply/agent-runner-execution.ts: add onCompactionStart
  optional callback to runAgentTurnWithFallback params; call it when the
  Pi runtime fires compaction:phase=start
- src/auto-reply/reply/agent-runner.ts: build and pass onCompactionStart
  callback that reads cfg.agents.defaults.compaction.notifyOnStart and
  delivers via opts.onBlockReply when enabled
- src/config/config.compaction-settings.test.ts: 5 new tests covering
  true/false/undefined and schema rejection of invalid types
- src/auto-reply/reply/agent-runner.compaction-notify.test.ts: 5 new
  agent-runner integration tests (default text, custom text, false, undefined,
  end-only event)
- docs/concepts/compaction.md: document the new config fields
- docs/reference/session-management-compaction.md: add implementation
  reference section

Extends openclaw#18049 (triple-layer post-compaction context enforcement).

Default: notifyOnStart is false (opt-in, no behaviour change for existing users).
irchelper added a commit to irchelper/openclaw that referenced this pull request Feb 23, 2026
Add notifyOnStart and notifyOnStartText fields to AgentCompactionConfig so
users receive a message the moment auto-compaction begins, resolving the UX
problem where the interface appears frozen or shows red errors with no context.

Changes:
- src/config/types.agent-defaults.ts: add notifyOnStart (boolean) and
  notifyOnStartText (string) to AgentCompactionConfig
- src/config/zod-schema.agent-defaults.ts: add corresponding Zod fields
  inside the .strict() compaction object
- src/auto-reply/reply/agent-runner-execution.ts: add onCompactionStart
  optional callback to runAgentTurnWithFallback params; call it when the
  Pi runtime fires compaction:phase=start
- src/auto-reply/reply/agent-runner.ts: build and pass onCompactionStart
  callback that reads cfg.agents.defaults.compaction.notifyOnStart and
  delivers via opts.onBlockReply when enabled
- src/config/config.compaction-settings.test.ts: 5 new tests covering
  true/false/undefined and schema rejection of invalid types
- src/auto-reply/reply/agent-runner.compaction-notify.test.ts: 5 new
  agent-runner integration tests (default text, custom text, false, undefined,
  end-only event)
- docs/concepts/compaction.md: document the new config fields
- docs/reference/session-management-compaction.md: add implementation
  reference section

Extends openclaw#18049 (triple-layer post-compaction context enforcement).

Default: notifyOnStart is false (opt-in, no behaviour change for existing users).
irchelper added a commit to irchelper/openclaw that referenced this pull request Feb 23, 2026
Add notifyOnStart and notifyOnStartText fields to AgentCompactionConfig so
users receive a message the moment auto-compaction begins, resolving the UX
problem where the interface appears frozen or shows red errors with no context.

Changes:
- src/config/types.agent-defaults.ts: add notifyOnStart (boolean) and
  notifyOnStartText (string) to AgentCompactionConfig
- src/config/zod-schema.agent-defaults.ts: add corresponding Zod fields
  inside the .strict() compaction object
- src/auto-reply/reply/agent-runner-execution.ts: add onCompactionStart
  optional callback to runAgentTurnWithFallback params; call it when the
  Pi runtime fires compaction:phase=start
- src/auto-reply/reply/agent-runner.ts: build and pass onCompactionStart
  callback that reads cfg.agents.defaults.compaction.notifyOnStart and
  delivers via opts.onBlockReply when enabled
- src/config/config.compaction-settings.test.ts: 5 new tests covering
  true/false/undefined and schema rejection of invalid types
- src/auto-reply/reply/agent-runner.compaction-notify.test.ts: 5 new
  agent-runner integration tests (default text, custom text, false, undefined,
  end-only event)
- docs/concepts/compaction.md: document the new config fields
- docs/reference/session-management-compaction.md: add implementation
  reference section

Extends openclaw#18049 (triple-layer post-compaction context enforcement).

Default: notifyOnStart is false (opt-in, no behaviour change for existing users).
irchelper added a commit to irchelper/openclaw that referenced this pull request Feb 23, 2026
Add notifyOnStart and notifyOnStartText fields to AgentCompactionConfig so
users receive a message the moment auto-compaction begins, resolving the UX
problem where the interface appears frozen or shows red errors with no context.

Changes:
- src/config/types.agent-defaults.ts: add notifyOnStart (boolean) and
  notifyOnStartText (string) to AgentCompactionConfig
- src/config/zod-schema.agent-defaults.ts: add corresponding Zod fields
  inside the .strict() compaction object
- src/auto-reply/reply/agent-runner-execution.ts: add onCompactionStart
  optional callback to runAgentTurnWithFallback params; call it when the
  Pi runtime fires compaction:phase=start
- src/auto-reply/reply/agent-runner.ts: build and pass onCompactionStart
  callback that reads cfg.agents.defaults.compaction.notifyOnStart and
  delivers via opts.onBlockReply when enabled
- src/config/config.compaction-settings.test.ts: 5 new tests covering
  true/false/undefined and schema rejection of invalid types
- src/auto-reply/reply/agent-runner.compaction-notify.test.ts: 5 new
  agent-runner integration tests (default text, custom text, false, undefined,
  end-only event)
- docs/concepts/compaction.md: document the new config fields
- docs/reference/session-management-compaction.md: add implementation
  reference section

Extends openclaw#18049 (triple-layer post-compaction context enforcement).

Default: notifyOnStart is false (opt-in, no behaviour change for existing users).
irchelper added a commit to irchelper/openclaw that referenced this pull request Mar 7, 2026
Add notifyOnStart and notifyOnStartText fields to AgentCompactionConfig so
users receive a message the moment auto-compaction begins, resolving the UX
problem where the interface appears frozen or shows red errors with no context.

Changes:
- src/config/types.agent-defaults.ts: add notifyOnStart (boolean) and
  notifyOnStartText (string) to AgentCompactionConfig
- src/config/zod-schema.agent-defaults.ts: add corresponding Zod fields
  inside the .strict() compaction object
- src/auto-reply/reply/agent-runner-execution.ts: add onCompactionStart
  optional callback to runAgentTurnWithFallback params; call it when the
  Pi runtime fires compaction:phase=start
- src/auto-reply/reply/agent-runner.ts: build and pass onCompactionStart
  callback that reads cfg.agents.defaults.compaction.notifyOnStart and
  delivers via opts.onBlockReply when enabled
- src/config/config.compaction-settings.test.ts: 5 new tests covering
  true/false/undefined and schema rejection of invalid types
- src/auto-reply/reply/agent-runner.compaction-notify.test.ts: 5 new
  agent-runner integration tests (default text, custom text, false, undefined,
  end-only event)
- docs/concepts/compaction.md: document the new config fields
- docs/reference/session-management-compaction.md: add implementation
  reference section

Extends openclaw#18049 (triple-layer post-compaction context enforcement).

Default: notifyOnStart is false (opt-in, no behaviour change for existing users).
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.

feat: triple-layer post-compaction context enforcement

2 participants