Skip to content

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

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 fix: injectSection accumulates improvements instead of replacing #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

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions