File size: 4,305 Bytes
21ff762
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#!/usr/bin/env python3
"""
flatten_agents.py -- Promote every nested agent to a top-level sibling.

Claude Code's /agents Library tab auto-discovers ONLY top-level .md files in
~/.claude/agents/. Agents living in category subdirs (design/, engineering/,
game-development/, etc.) are invisible to the Library — there are 169 such
orphans across 15 folders.

This script walks ~/.claude/agents/ and for every nested .md file with YAML
frontmatter containing a `name:` field (i.e. a real agent, not a reference
note), it writes a sibling copy at the top level. The original nested file is
left untouched so existing references still resolve.

Safety:
- Skips files with no frontmatter (reference notes, includes).
- Skips if a sibling with the same basename already exists (no collision risk
  — verified at time of writing, but re-checked here).
- Dry-run by default; pass --apply to actually write.

Usage:
  python src/flatten_agents.py              # dry run, print plan
  python src/flatten_agents.py --apply      # perform the flatten
  python src/flatten_agents.py --apply -v   # verbose
"""

from __future__ import annotations

import argparse
import shutil
import sys
from pathlib import Path

AGENTS_DIR = Path.home() / ".claude" / "agents"


def has_name_frontmatter(path: Path) -> bool:
    """Return True if the file opens with YAML frontmatter containing `name:`."""
    try:
        with path.open(encoding="utf-8", errors="replace") as fh:
            first = fh.readline().rstrip("\r\n")
            if first != "---":
                return False
            for line in fh:
                stripped = line.rstrip("\r\n")
                if stripped == "---":
                    return False
                if stripped.startswith("name:"):
                    return True
        return False
    except OSError:
        return False


def plan_flatten(agents_dir: Path) -> tuple[list[tuple[Path, Path]], list[str]]:
    """Return (copy_plan, warnings). copy_plan is list of (src, dst) pairs."""
    copy_plan: list[tuple[Path, Path]] = []
    warnings: list[str] = []

    if not agents_dir.exists():
        warnings.append(f"agents_dir does not exist: {agents_dir}")
        return copy_plan, warnings

    for md in agents_dir.rglob("*.md"):
        # Top-level files are already discoverable.
        if md.parent == agents_dir:
            continue
        if not has_name_frontmatter(md):
            continue
        dst = agents_dir / md.name
        if dst.exists():
            # Don't clobber — flag so humans can resolve.
            if dst.read_bytes() != md.read_bytes():
                warnings.append(f"collision (different content): {md} -> {dst}")
            continue
        copy_plan.append((md, dst))

    return copy_plan, warnings


def apply_plan(plan: list[tuple[Path, Path]], verbose: bool) -> int:
    copied = 0
    for src, dst in plan:
        try:
            shutil.copy2(src, dst)
            copied += 1
            if verbose:
                print(f"  copied {src.relative_to(AGENTS_DIR)} -> {dst.name}")
        except OSError as e:
            print(f"  FAILED {src}: {e}", file=sys.stderr)
    return copied


def main() -> None:
    parser = argparse.ArgumentParser(description=__doc__)
    parser.add_argument("--apply", action="store_true", help="actually copy files (default: dry run)")
    parser.add_argument("-v", "--verbose", action="store_true", help="print each action")
    parser.add_argument(
        "--agents-dir",
        type=Path,
        default=AGENTS_DIR,
        help="override agents dir (default: ~/.claude/agents)",
    )
    args = parser.parse_args()

    plan, warnings = plan_flatten(args.agents_dir)

    print(f"flatten_agents: {len(plan)} files to promote, {len(warnings)} warnings")
    for w in warnings:
        print(f"  warn: {w}", file=sys.stderr)

    if not args.apply:
        if args.verbose:
            for src, dst in plan:
                print(f"  would copy {src.relative_to(args.agents_dir)} -> {dst.name}")
        print("dry run — pass --apply to perform the flatten")
        sys.exit(0)

    copied = apply_plan(plan, args.verbose)
    print(f"done: {copied} agents promoted to top-level siblings")
    sys.exit(0 if copied == len(plan) else 1)


if __name__ == "__main__":
    main()