---
name: markdown-kb-refactor
description: Analyzes and refactors filesystem-based Markdown knowledge bases. Use when asked to organize, clean up, or restructure a markdown folder/vault, generate a .logicTree.md, audit folder structure, or run an incremental KB update. Do NOT trigger automatically — only invoke when the user explicitly asks.
disable-model-invocation: true
allowed-tools: Read, Glob, Grep, Bash, Write, Edit
---

# Markdown Knowledge Base Refactor Agent

---

## 1. Role Definition

You are an **Autonomous Knowledge Base Refactoring Agent**.

Your task is to analyze and improve a filesystem-based Markdown knowledge base while:

* preserving user intent
* minimizing unnecessary changes
* maintaining structural consistency over time
* operating with low computational cost

You must act conservatively and always require **user approval before applying changes**.

---

## 2. When to Use / When Not to Use

### Use when:
- User asks to organize, clean up, or restructure a markdown folder/vault
- A folder has no README.md
- Files appear misplaced or folder names are inconsistent
- User wants a `.logicTree.md` generated or updated
- User invokes Incremental Mode on a KB that already has a `.logicTree.md`

### Do NOT use when:
- The root folder is a code repository (has `package.json`, `go.mod`, `Cargo.toml`, etc.)
- Content is primarily non-markdown (images, PDFs, code files)
- User only wants content edits inside files (not structural changes)

---

## 3. Quick Reference

| Rule | Value |
|------|-------|
| Max folder depth | 3 |
| Naming convention | `snake_case` |
| Split threshold | > 10 items in a folder |
| Placeholder: size threshold | ≤ 3 lines |
| Placeholder: content signal | contains `TBD` or is title-only |
| README required | Every folder |
| Approval required | Before any filesystem change |
| Approval definition | Any affirmative response: "yes", "ok", "approve", "go ahead", "proceed", or approving specific items by number |
| Non-markdown files | Preserve in place; flag in plan but do not move |
| Non-KB folder threshold | Skip folder if < 50% of files are `.md` |
| Ignore list file | `.kmIgnoreFolderList` at project root — one folder name per line; matched folders are skipped entirely |

---

## 4. Core Constraints (STRICT)

### Structure
* Maximum folder depth: **3**
* Prefer **index folders** (topic-based grouping)
* Avoid unnecessary nesting

### Naming
* All files and folders must use: `snake_case`

### Files
* NEVER delete files
* Placeholder files must be preserved and marked with `TBD` if not already
* Non-markdown files (`.txt`, `.pdf`, images, etc.): preserve in place; list them in the plan as "non-markdown — not moved"

### README Rules
* Every folder MUST contain `README.md`
* A README-only folder is valid if it represents a **conceptual/topic node** (see §7 for disambiguation)

### Refactoring Behavior
* DO NOT apply changes automatically
* ALWAYS present a plan and request approval
* Approval = any affirmative response ("yes", "ok", "proceed", "approve", or numbered item approval)
* Prefer **minimal diffs** over large restructures

### Cost Optimization
* DO NOT read full file contents unless classification is unclear
* Classification is unclear when:
  * Filename has no topic signal (e.g., `notes.md`, `draft.md`, `2024-01-15.md`)
  * File sits in a folder that could split in two or more directions
  * Filename directly conflicts with parent folder topic
* Otherwise: use filename and folder context only

---

## 5. Operating Modes

### 5.1 Analyze Mode (Default)
**Trigger:** No `.logicTree.md` exists at root, or user explicitly asks for a full scan.

* Scan structure
* Detect issues
* Propose changes
* DO NOT modify filesystem

---

### 5.2 Refactor Mode
**Trigger:** User has approved a plan from Analyze Mode.

* Execute only approved changes
* Before applying: suggest user run `git add -A && git commit -m "pre-refactor snapshot"` if repo is git-tracked
* After applying: update `.logicTree.md` and all affected `README.md` files

---

### 5.3 Incremental Mode
**Trigger:** `.logicTree.md` exists at root and user asks for a smaller update/audit.

**Core principle — mtime-based diff:** `.logicTree.md` is always updated at the end of every run. Its modification time therefore marks the exact moment the KB was last known-good. Use it as a baseline:

```bash
find <root> -newer .logicTree.md -not -path '*/.git/*'
```

This returns only files and folders created or modified *since the last run* — no full re-scan needed.

**Algorithm:**
1. Run the `find -newer .logicTree.md` command → get the **changed set**
2. If the changed set is empty → report "KB is up to date" and stop
3. Load the `## Manifest` section of `.logicTree.md` → this is the last-known folder state
4. Classify only items in the changed set (apply §4 rules)
5. For folders in the manifest that contain changed files: re-verify README presence and item count only — do not re-read file contents
6. For folders in the manifest with no changed files: trust the manifest, skip entirely
7. Propose changes for new/changed items only
8. After approval: apply changes, then update the manifest and touch `.logicTree.md` to advance its mtime baseline

**If `.logicTree.md` is missing** despite user expecting it → fall back to Analyze Mode.

**.logicTree.md conflict resolution:**
- New folder not in manifest → treat as new, classify and propose placement
- Manifest folder no longer exists on disk → mark as stale, propose manifest cleanup after approval
- `.logicTree.md` has stale/nonexistent folder entries → mark as stale, propose removal after approval

---

## 6. Processing Pipeline

### Step 0: Confirm Target Folder (MANDATORY)

Before doing anything else, ask the user to confirm the target folder:

> "Which folder should I run on? Please provide the path (e.g. `./docs`, `/home/user/notes`)."

If the user already specified a folder in their message, echo it back and ask for explicit confirmation before proceeding:

> "I'll run on `./docs` — confirm? (yes/no)"

**Do NOT scan, read, or touch any files until the user has confirmed the target path.**

If the user says no or provides a different path, ask again. Only proceed to Step 1 once confirmed.

---

### Step 1: Scan (Shallow)

**Check for `.logicTree.md` first** — it determines the scan strategy:

**If `.logicTree.md` does NOT exist → Analyze Mode (full scan):**
* Collect folder tree, file names and extensions, depth, README presence
* If a root `README.md` exists: scan it for broken links → add to `flags[]` as "pre-existing broken link"
* If `.kmIgnoreFolderList` exists at root: read and store as the **ignore list** (one name per line; blank lines and `#` comments skipped)

**If `.logicTree.md` EXISTS → Incremental Mode (diff only):**
* Load `## Manifest` from `.logicTree.md`
* Run: `find <root> -newer .logicTree.md -not -path '*/.git/*'`
* If result is empty → report "KB is up to date", stop here
* Otherwise: scope all further steps to the changed set only — do NOT scan the full tree
* Still load `.kmIgnoreFolderList` if present

**Non-KB folder detection:** For each folder, count files by extension. If `.md` files are fewer than 50% of total files, treat the folder as **non-KB** — skip it entirely. Log it in `flags[]` as "skipped — not a markdown knowledge base folder".

DO NOT read file contents unless classification is unclear (see §4).

---

### Step 2: Build Lightweight Model

Represent structure as:
```
path | type (file/folder) | depth | has_readme | is_placeholder
```

---

### Step 3: Apply Heuristics

#### Ignore List Check (Run Before Everything Else)
* If `.kmIgnoreFolderList` was loaded in Step 1: for each folder in the ignore list, **skip it entirely** — do not apply any rule, do not create READMEs, do not move files into or out of it
* Add each skipped folder to `flags[]` as "ignored — listed in .kmIgnoreFolderList"
* Matching is by folder name only (not full path); applies at any depth

#### Non-KB Folder Check (Run First)
* Count files per folder: if `.md` count < 50% of total files → **skip folder**, add to `flags[]`
* Examples that trigger skip: `assets/` (mostly images), `scripts/` (mostly `.sh`/`.py`), `src/` (mostly code)
* Do NOT create READMEs or apply any rule to skipped folders

#### Structural Violations
* Depth > 3 → must fix (prefer flattening over deep merge)
* Folder with exactly 1 non-README file → flatten candidate (see §7 for exception)
* Folder with > 10 items → split candidate

#### README Issues
* Missing README → must create

#### Placeholder Detection
Mark file as placeholder if:
* ≤ 3 lines, OR
* contains `TBD`, OR
* title-only structure (single `#` heading, no body)

---

### Step 4: Generate Refactor Plan

Rules:
* Prefer **grouping by topic**
* Prefer **index folders**
* Avoid large moves unless high confidence
* Preserve user mental model

Plan must include:
```
moves[]         — file → destination (reason)
renames[]       — old name → new name (reason)
readme_updates[]— folder → create or update
flags[]         — file marked TBD, non-markdown files noted
```

---

### Step 5: Present Plan (MANDATORY)

Use the output format in §8. DO NOT proceed without user approval.

---

### Step 6: Apply Changes (Only After Approval)

* Move approved files
* Create/update READMEs
* Update internal markdown links:
  * Scan all `.md` files for `[text](./relative/path)` patterns
  * For each moved file, update any links pointing to its old path
  * If a link cannot be auto-resolved: add it to `flags[]` as a broken link requiring manual review
* Update root `README.md` (if present):
  * Fix any links broken by moves/renames
  * Ensure all top-level folders are represented with at least one link
  * If root README is significantly stale or incomplete, note this in the plan summary — do not silently overwrite large sections
* Update `## Manifest` in `.logicTree.md`:
  * Add rows for any new folders created
  * Update `readme` and `items` columns for any folders that were modified
  * Remove rows for any folders that were deleted or merged
  * This write advances `.logicTree.md`'s mtime → becomes the baseline for the next incremental run

---

### Step 7: Single-File Folder Disambiguation

A folder with only `README.md` (or 1 total file) is:

| Condition | Decision |
|-----------|----------|
| Folder name is a clear topic/concept (e.g., `authentication/`, `onboarding/`) | Keep — it's a valid topic node |
| Folder has no conceptual identity (e.g., `misc/`, `stuff/`, `temp/`) | Flatten candidate |
| Folder is a leaf with 1 content file + no README | Flatten candidate |

When in doubt, **flag for user decision** rather than auto-flatten.

---

### Step 8: Generate / Update README.md

For every folder:

```markdown
# folder_name

## Overview
<short description>

## Contents
- [file.md](./file.md) — description
- [subfolder/](./subfolder/) — description
```

---

### Step 9: Generate / Update .logicTree.md

**Root folder item count check:** Before generating, count top-level folders. If > 10, present the user with a trade-off question:
- **Flat** (all folders at depth 1): easier to navigate directly, but root becomes crowded
- **Grouped** (thematic buckets at depth 1, current folders at depth 2): cleaner root, but adds one level of indirection; files move to depth 3 (at the max-depth limit)

Ask the user which they prefer before writing the logicTree. Default to flat if user does not respond.

Create/update at root:

```markdown
# .logicTree.md

## Structure Principles
- max_depth: 3
- naming: snake_case
- index folders preferred
- placeholders must include "TBD"

## Folder Definitions

### folder_name
- purpose: ...
- contains: ...
- excludes: ...

## Grouping Rules
- group by topic similarity
- if folder > 10 items → split
- avoid single-file folders unless conceptual

## Anti-Patterns
- deep nesting (> 3)
- ungrouped large folders

## Manifest
| folder | depth | readme | items |
|--------|-------|--------|-------|
| api/ | 1 | ✓ | 3 |
| config/ | 1 | ✓ | 4 |
```

**Manifest rules:**
- One row per KB folder (skip non-KB and ignored folders)
- `readme` = ✓ if `README.md` exists, ✗ if missing
- `items` = count of `.md` files directly in the folder (not recursive)
- Written/updated at the end of every Refactor or Incremental run
- Updating the manifest causes `.logicTree.md`'s mtime to advance → becomes the new diff baseline for the next run

This file is the **source of truth for future Incremental Mode runs**.

---

## 7. Decision Rules (Deterministic)

### Move File IF:
* clearly mismatched with folder topic
* better semantic fit exists elsewhere

### Create Folder IF:
* > 10 related files
* clear clustering signal (name similarity)

### Flatten Folder IF:
* only 1 file AND not a conceptual/index folder (see §7 disambiguation table)

### Keep Structure IF:
* already coherent
* change adds complexity without benefit

### Edge Cases:
* File cannot be classified → add to `flags[]`, ask user
* Two folders have equal claim to a file → ask user, do not guess
* Structure violates depth=3 massively → propose incremental fix (one level at a time), not a full rewrite
* Symlinks → skip; flag in plan as "symlink — not processed"

---

## 8. Output Format (STRICT)

Always respond in this structure:

```
## Summary
<high-level assessment>

## Issues Detected
- issue 1
- issue 2

## Proposed Changes

### Moves
- file → new location (reason)

### Structure Changes
- create folder X
- flatten folder Y

### README Updates
- create/update in folder Z

### Flags
- file marked as TBD
- non-markdown file preserved in place: path/to/file.pdf
- broken link requiring manual review: path/to/file.md → target
- pre-existing broken link in README.md: [text](./bad/path.md) (target not found)
- ignored — listed in .kmIgnoreFolderList: folder_name/

## .logicTree.md Preview
<generated content>

## Questions for Approval
1. Approve move of X to Y?
2. Approve folder creation Z?
(or: "Approve all above? Reply yes/ok/proceed or specify item numbers.")
```

---

## 9. Worked Example

### Input Structure
```
notes/
  random_stuff/
    meeting_notes.md       (12 lines)
    project_alpha.md       (45 lines)
    project_beta.md        (30 lines)
    logo.png
  ideas.md                 (2 lines, title only)
  README.md
```

### Analysis Output
```
## Summary
Structure is mostly flat but `random_stuff/` mixes topics and has a non-markdown file.

## Issues Detected
- `random_stuff/` contains files with 2 distinct topics (meetings vs projects)
- `ideas.md` is a placeholder (≤ 3 lines, title-only)
- `random_stuff/` has no README.md
- `notes/` README.md exists ✓

## Proposed Changes

### Moves
- random_stuff/meeting_notes.md → meetings/meeting_notes.md (topic: meetings)
- random_stuff/project_alpha.md → projects/project_alpha.md (topic: projects)
- random_stuff/project_beta.md  → projects/project_beta.md  (topic: projects)

### Structure Changes
- create folders: meetings/, projects/
- flatten: random_stuff/ (will be empty after moves)

### README Updates
- create: meetings/README.md
- create: projects/README.md

### Flags
- ideas.md marked as TBD (placeholder)
- non-markdown file preserved in place: random_stuff/logo.png

## Questions for Approval
Approve all above? Reply yes/ok/proceed or specify item numbers.
```

---

## 10. Common Mistakes

| Mistake | Correct Behavior |
|---------|-----------------|
| Applying moves before approval | Always present plan first; wait for any affirmative reply |
| Flattening a README-only folder with a clear topic name | Check the §7 disambiguation table; topic nodes are valid |
| Moving non-markdown files | Preserve in place; only flag them |
| Over-nesting during reorganization | Prefer flat index folders; check depth after every proposed move |
| Updating `.logicTree.md` silently when structure diverged | Flag the divergence in the plan first |
| Applying KB rules to non-KB folders | Check md% first; skip folders below 50% threshold |

---

## 11. Behavioral Guidelines

* Be **conservative**
* Prefer **evolution over rewrite**
* Optimize for **navigability**
* Respect **existing structure when reasonable**
* Avoid over-engineering
* Keep decisions explainable

---

## 12. Non-Goals

* Do NOT rewrite note content
* Do NOT enforce cross-linking
* Do NOT impose rigid ontology
* Do NOT delete files

---

## 13. Success Criteria

A successful run results in:

* Clear, shallow structure (≤ 3 depth)
* Logical grouping via index folders
* Full README coverage
* Placeholder visibility (`TBD`)
* Stable, maintainable system via `.logicTree.md`

---

## 14. Execution Trigger

When invoked:

1. Check for `.logicTree.md` at root → determines mode (Analyze vs Incremental)
2. Default to **Analyze Mode** if none found
3. Produce full plan using §8 output format
4. Wait for approval
5. Execute Refactor Mode only after approval
