Skip to content

fix(cli): strip removed library from AGENTS.md on ask remove#98

Merged
amondnet merged 3 commits intomainfrom
amondnet/fix-remove-agents-md
Apr 17, 2026
Merged

fix(cli): strip removed library from AGENTS.md on ask remove#98
amondnet merged 3 commits intomainfrom
amondnet/fix-remove-agents-md

Conversation

@amondnet
Copy link
Copy Markdown
Contributor

@amondnet amondnet commented Apr 17, 2026

Summary

  • Fix ask remove leaving the removed library's section (or the whole auto-generated block) in AGENTS.md when the last entry is removed from ask.json
  • runInstall now regenerates AGENTS.md even when the full install has zero targets (previously short-circuited without touching the file)
  • generateAgentsMd now strips the existing <!-- BEGIN:ask-docs-auto-generated --> … <!-- END --> block on empty input: deletes AGENTS.md when only the block remained, preserves user-authored content otherwise
  • Added 3 CLI-level tests covering non-last removal, last-library removal (file deleted), and user content preservation

Root cause

Two cooperating early returns skipped regeneration when askJson.libraries became empty:

  1. runInstall short-circuited on targets.length === 0 before calling generateAgentsMd
  2. generateAgentsMd returned '' on libraries.length === 0 without touching AGENTS.md

The happy path (remove a non-last library) worked because runInstall still hit the regeneration call with the remaining libraries.

Test plan

  • bun run --cwd packages/cli test test/install/remove.test.ts — 4 pass
  • bun run --cwd packages/cli test — 473 pass / 0 fail
  • bun run --cwd packages/cli lint — clean
  • bun run --cwd packages/cli build — typecheck clean

Follow-up (not in this PR)

ask remove still does not call removeFromIntentSkillsBlock, so intent-format packages leave their <!-- intent-skills:start --> entries behind. Tracked for a separate PR.


Summary by cubic

Fixes ask remove leaving stale entries in AGENTS.md, ensures the file is cleaned when the last library is removed, and warns on orphaned markers to prevent silent corruption.

  • Bug Fixes
    • generateAgentsMd: on empty input, strips the auto-generated block; deletes AGENTS.md if only the block remained; preserves user content; logs a consola.warn and leaves the file untouched when markers are mismatched.
    • runInstall: when not scoped with --only-specs and targets are empty, regenerates AGENTS.md and updates ignore files; when scoped and no specs match, skips regeneration.
    • Added CLI tests for removing a single library, removing the last library (file deleted), preserving user content before/after the block, orphan marker warning (BEGIN or END), and the --only-specs no-match case.

Written for commit 1675e4e. Summary will update on new commits.

When the last library was removed from ask.json, the auto-generated
block in AGENTS.md lingered because both runInstall (short-circuit
on empty targets) and generateAgentsMd (early return on empty input)
skipped regeneration. Now runInstall explicitly regenerates AGENTS.md
when the full install has zero targets, and generateAgentsMd strips
the existing marker block (deleting AGENTS.md when only the block
remains, preserving any user-authored content otherwise).
@dosubot dosubot bot added the size:M This PR changes 30-99 lines, ignoring generated files. label Apr 17, 2026
@codecov
Copy link
Copy Markdown

codecov bot commented Apr 17, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

@dosubot dosubot bot added type:bug Something isn't working type:test Testing improvements or additions labels Apr 17, 2026
@cloudflare-workers-and-pages
Copy link
Copy Markdown

cloudflare-workers-and-pages bot commented Apr 17, 2026

Deploying ask-registry with  Cloudflare Pages  Cloudflare Pages

Latest commit: 1675e4e
Status: ✅  Deploy successful!
Preview URL: https://1d4de86c.ask-registry.pages.dev
Branch Preview URL: https://amondnet-fix-remove-agents-m.ask-registry.pages.dev

View logs

Copy link
Copy Markdown
Contributor

@cubic-dev-ai cubic-dev-ai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No issues found across 3 files

Requires human review: Modifies core CLI logic and handles file deletions based on manual string parsing of project files. This logic change in a core path warrants human review to ensure no data loss edge cases.

Architecture diagram
sequenceDiagram
    participant User
    participant CLI as CLI Engine
    participant Install as runInstall()
    participant Agents as generateAgentsMd()
    participant FS as File System (AGENTS.md)
    participant Ignore as manageIgnoreFiles()

    User->>CLI: ask remove [library]
    CLI->>FS: Update ask.json
    CLI->>Install: sync state

    Note over Install,FS: Handling Last Library Removal

    alt targets.length == 0
        Install->>Install: Detect empty targets
        
        rect rgb(240, 240, 240)
            Note right of Install: CHANGED: Previously short-circuited here
            Install->>Agents: NEW: generateAgentsMd(projectDir, [])
            
            Agents->>FS: Check if AGENTS.md exists
            opt File exists
                Agents->>FS: Read AGENTS.md content
                Agents->>Agents: NEW: Strip <!-- BEGIN/END --> block
                
                alt Only auto-generated block existed
                    Agents->>FS: NEW: Delete AGENTS.md (fs.rmSync)
                else User-authored content remains
                    Agents->>FS: NEW: Overwrite AGENTS.md (fs.writeFileSync)
                end
            end
            Agents-->>Install: return empty string
        end

        Install->>Ignore: NEW: manageIgnoreFiles(projectDir, 'remove')
    else targets.length > 0
        Install->>Install: Resolve remaining libraries
        Install->>Agents: generateAgentsMd(projectDir, libraries)
        Agents->>FS: Update AGENTS.md block
        Install->>Ignore: manageIgnoreFiles(projectDir, 'add')
    end

    Install-->>CLI: Return summary
    CLI-->>User: Operation complete
Loading

…markers

Iteration 1 review surfaced four concerns:
- `--only-specs` guard in runInstall was untested
- Malformed marker pairs (only BEGIN or only END) in AGENTS.md were
  silently skipped in the strip branch, and a follow-up install would
  splice new content into the orphan, compounding corruption
- Block-at-top layout (user content after) was not exercised
- The strip branch had no diagnostic when it encountered mismatched markers

This commit adds a `consola.warn` for the mismatched-marker case so
operators can repair AGENTS.md by hand instead of watching further
corruption stack up, and adds three regression tests covering each
gap.
Iteration 2 review flagged that the orphan-marker regression only
exercised the BEGIN-only branch, leaving the END-only side of the OR
guard (agents.ts:44) uncovered. Parametrize the case table so both
halves of the guard are locked in.
Copy link
Copy Markdown
Contributor

@cubic-dev-ai cubic-dev-ai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

0 issues found across 2 files (changes from recent commits).

Auto-approved: Safe bug fix for CLI documentation maintenance; correctly handles AGENTS.md cleanup and adds comprehensive tests.

Copy link
Copy Markdown
Contributor

@cubic-dev-ai cubic-dev-ai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

0 issues found across 1 file (changes from recent commits).

Auto-approved: Fixes cleanup logic in AGENTS.md when libraries are removed. Includes comprehensive tests and defensive handling of user-authored content.

@amondnet amondnet merged commit 58660e4 into main Apr 17, 2026
5 checks passed
@amondnet amondnet deleted the amondnet/fix-remove-agents-md branch April 17, 2026 01:53
@github-actions github-actions bot mentioned this pull request Apr 17, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

size:M This PR changes 30-99 lines, ignoring generated files. type:bug Something isn't working type:test Testing improvements or additions

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant