Skip to content

injectSection() overwrites entire Kasper section instead of accumulating improvements #1

@Eric-Spr

Description

@Eric-Spr

Bug

When applying multiple improvements via /kasper apply, each new improvement replaces the entire ## Kasper Inferred Instructions section instead of appending to it. Only the most recently applied improvement survives.

Steps to Reproduce

  1. Run /kasper improve — get multiple pending improvements
  2. Apply improvement injectSection() overwrites entire Kasper section instead of accumulating improvements #1 → AGENTS.md gets ## Kasper Inferred Instructions with improvement 1
  3. Apply improvement AGENTS.md path is hardcoded — no config option to change target file #2 → AGENTS.md now contains only improvement 2 (improvement 1 is gone)
  4. Apply improvement #3 → AGENTS.md now contains only improvement 3

Root Cause

In agents-md.js, injectSection() uses a regex that matches the entire ## Kasper Inferred Instructions section and replaces it wholesale:

injectSection(existing, sectionName, content) {
    const sectionRegex = new RegExp(
        `((?:^|\\n)##\\s*${escapeRegex(sectionName)})[\\s\\S]*?(?=\\r?\\n##|$)`
    );
    if (sectionRegex.test(existing)) {
        return existing.replace(sectionRegex, `$1\n${provenance}${content.trim()}`);
    }
    // append at end if section doesn't exist
}
When the section already exists, $1\n${provenance}${content.trim()} replaces everything between the header and the next ## with only the new content. Previous improvements are lost.
Expected Behavior
New improvements should accumulateeither:
- Append as new bullet points under the existing section, OR
- Merge/replace within the section while preserving previous entries
Workaround
Manually edit AGENTS.md to combine all improvements into the ## Kasper Inferred Instructions section.
Environment
- Plugin version: 1.1.0
- OS: Windows 11

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions