Skip to content

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

@irchelper

Description

@irchelper

Problem

After session compaction, agents receive a <summary> block with prior context. This creates a false sense of "already knowing everything", causing the agent to skip its configured Session Startup sequence (reading AGENTS.md-specified files like WORKFLOW_AUTO.md, memory files, etc.).

The compaction summary is a hint, not a substitute for startup reads. But LLMs have a completion bias — seeing a large context summary triggers "I already have context" reasoning, bypassing explicit file-read rules.

Solution: Triple-Layer Enforcement

Three independent layers ensure post-compaction recovery, with progressive reliability:

Layer 1: Summary Append (100% reliable)

Append critical workspace rules (Session Startup + Red Lines from AGENTS.md) directly into the compaction summary via <workspace-critical-rules> tags. Since the summary is part of the prompt, the agent physically sees the rules — zero latency, zero extra cost.

Modified: compaction-safeguard.ts

Layer 2: System Event (high reliability)

After compaction completes, inject the full workspace context as a system event for the next agent turn via enqueueSystemEvent(). This provides a second copy with explicit instructions to execute the startup sequence.

Modified: agent-runner.ts, new post-compaction-context.ts

Layer 3: Post-Turn Audit (closed loop)

After the agent's first turn post-compaction, audit whether it actually read the required files. If not, inject a one-time warning system event listing the missing files. Max 1 retry to prevent Yo-Yo loops. Includes cross-platform path normalization (forward-slash unification) to ensure consistent audit behavior on Windows.

Modified: agent-runner.ts, new post-compaction-audit.ts

Why Three Layers

Layer Mechanism Reliability Failure Mode
Summary Append Rules in prompt 100% Agent may not follow rules despite seeing them
System Event Explicit reminder High Event may not fire (edge cases)
Post-Turn Audit Closed-loop check Very High Only fires once (max 1 retry)

Each layer covers the failure mode of the previous one. Together they form a defense-in-depth strategy.

Additional Improvements

  • extractSections handles H2/H3 headings case-insensitively, skips code blocks, includes sub-headings

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions