← Back to PRs

#20267: feat: workspace-aware post-compaction context

by nickjlamb open 2026-02-18 18:25 View on GitHub →
size: M
## Problem Fixes #20225 After auto-compaction, agents frequently forget to re-read their workspace bootstrap files. The existing post-compaction context injection only extracts `## Session Startup` and `## Red Lines` sections from AGENTS.md — but most users don't have those exact section names, so the injection returns `null` and agents lose their identity mid-session. This is the single most common cause of "my agent forgot who it is" reports. ## Fix Make the post-compaction context injection **workspace-aware**: 1. **Scan the workspace** for well-known bootstrap files: AGENTS.md, CLAUDE.md, CLAUDE.local.md, SOUL.md, IDENTITY.md, USER.md, MEMORY.md, HEARTBEAT.md, TOOLS.md 2. **Detect recent daily memory files** (`memory/YYYY-MM-DD.md`, most recent 2 days) 3. **Generate a file inventory** telling the agent exactly which files to re-read 4. **Also extract `## Every Session` sections** from AGENTS.md (a common pattern alongside Session Startup and Red Lines) The file inventory and AGENTS.md sections are combined into a single system event, so agents get both a concrete list of files to re-read and any critical inline rules. ## Changes - `src/auto-reply/reply/post-compaction-context.ts`: Add `detectBootstrapFiles()` function, enhance `readPostCompactionContext()` with two-layer approach (file inventory + AGENTS.md sections) - `src/auto-reply/reply/post-compaction-context.test.ts`: 19 tests covering file detection, daily memory files, section extraction, and combined output ## Before / After **Before:** Agent compacts → gets summary → has no idea what files exist → responds without identity/context. **After:** Agent compacts → gets summary + `[Post-compaction context refresh]` listing all bootstrap files → re-reads them → responds with full context. ## Example output ``` [Post-compaction context refresh] Session was just compacted. The conversation summary above is a condensed hint, NOT a substitute for your workspace files. Re-read your bootstrap files before responding to the user. Your workspace contains these bootstrap files — re-read them now before responding: - AGENTS.md - SOUL.md - IDENTITY.md - USER.md - MEMORY.md - HEARTBEAT.md - TOOLS.md - memory/2026-02-18.md - memory/2026-02-17.md Critical rules from AGENTS.md: ## Every Session Before doing anything else: 1. Read SOUL.md — this is who you are 2. Read USER.md — this is who you're helping ... ``` <!-- greptile_comment --> <h3>Greptile Summary</h3> This PR enhances the post-compaction context injection to be workspace-aware, addressing the common issue of agents losing their identity after auto-compaction. Instead of only extracting specific AGENTS.md sections (which many users don't have), the system now scans for well-known bootstrap files (AGENTS.md, CLAUDE.md, SOUL.md, etc.) and recent daily memory files, then generates a file inventory instructing the agent to re-read them. It also adds extraction of `## Every Session` sections from AGENTS.md. - Adds `detectBootstrapFiles()` to scan for well-known workspace files and recent daily memory entries (`memory/YYYY-MM-DD.md`, limited to 2 most recent) - Enhances `readPostCompactionContext()` with a two-layer approach: file inventory listing + inlined critical AGENTS.md sections - Returns `null` only when zero workspace files exist (previously returned `null` when AGENTS.md lacked specific section names) - Adds "Every Session" to the list of extracted section names alongside "Session Startup" and "Red Lines" - 19 comprehensive tests covering detection, filtering, section extraction, truncation, and combined output - **Issue**: `MEMORY.md` and `memory.md` are both in the bootstrap file list — on case-insensitive filesystems (macOS), both will resolve to the same file, producing duplicate entries in the inventory <h3>Confidence Score: 4/5</h3> - This PR is safe to merge with one minor filesystem edge case to consider on macOS. - The changes are well-structured with a clean two-layer design, comprehensive test coverage (19 tests), and graceful error handling (try/catch with null fallback). The only issue found is a duplicate detection bug on case-insensitive filesystems where both MEMORY.md and memory.md entries would resolve to the same file. The feature is best-effort (silent failure on errors) and doesn't affect core agent execution paths. - `src/auto-reply/reply/post-compaction-context.ts` — the `BOOTSTRAP_FILENAMES` array contains both `MEMORY.md` and `memory.md`, which will produce duplicate entries on case-insensitive filesystems (macOS). <sub>Last reviewed commit: ba0af93</sub> <!-- greptile_other_comments_section --> <sub>(4/5) You can add custom instructions or style guidelines for the agent [here](https://app.greptile.com/review/github)!</sub> <!-- /greptile_comment -->

Most Similar PRs