File size: 10,825 Bytes
3fbbaab
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
21ff762
 
 
 
 
 
 
 
 
 
 
3fbbaab
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
21ff762
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3fbbaab
 
 
 
 
 
 
 
 
 
 
 
21ff762
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3fbbaab
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
21ff762
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3fbbaab
 
 
 
 
 
 
 
 
 
 
 
21ff762
 
 
 
 
 
 
 
 
 
3fbbaab
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
21ff762
3fbbaab
 
 
 
 
 
 
 
21ff762
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3fbbaab
 
 
 
 
 
 
 
21ff762
 
 
 
3fbbaab
 
 
 
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
#!/usr/bin/env python3
"""

agent_add.py -- Add new agents with intake gate + wiki ingestion.



Mirror of ``skill_add`` for the agent subject type. Agents live as flat

``.md`` files under ``~/.claude/agents/`` (unlike skills, which each get

their own directory). The flow is therefore shorter:



    validate name -> read content -> intake gate -> copy file ->

    record embedding -> write wiki entity page -> log



Usage

-----

    # Single agent

    python agent_add.py --agent-path /path/to/agent.md --name my-agent \

        --wiki ~/.claude/skill-wiki --agents-dir ~/.claude/agents



    # Batch from directory (every *.md at depth 1 is treated as an agent)

    python agent_add.py --scan-dir /path/to/new-agents/ \

        --wiki ~/.claude/skill-wiki --agents-dir ~/.claude/agents

"""

import argparse
import os
import sys
from datetime import datetime, timezone
from pathlib import Path

from ctx.core.entity_update import build_update_review, render_update_review
from ctx_config import cfg
from ctx.adapters.claude_code.install.install_utils import safe_copy_file
from intake_pipeline import IntakeRejected, check_intake, record_embedding
from wiki_batch_entities import generate_agent_page
from ctx.core.wiki.wiki_queue import enqueue_entity_upsert
from ctx.core.wiki.wiki_sync import append_log, ensure_wiki, update_index
from ctx.core.wiki.wiki_utils import validate_skill_name
from ctx.utils._fs_utils import reject_symlink_path, safe_atomic_write_text

TODAY = datetime.now(timezone.utc).strftime("%Y-%m-%d")

# Match skill_add's ceiling — agents are prose, 1 MB is absurdly generous.
_MAX_AGENT_BYTES = 1_048_576


def install_agent(source: Path, agents_dir: Path, name: str) -> Path:
    """Copy agent file into ``agents_dir/<name>.md``.



    Unlike :func:`skill_add.install_skill`, agents are flat single-file

    entries — no per-agent subdirectory.

    """
    dest = agents_dir / f"{name}.md"
    safe_copy_file(source, dest, dest_root=agents_dir)
    return dest


def write_entity_page(wiki_path: Path, name: str, content: str) -> bool:
    """Write agent entity page. Returns True if newly created."""
    page = wiki_path / "entities" / "agents" / f"{name}.md"
    reject_symlink_path(page)
    is_new = not page.exists()
    safe_atomic_write_text(page, content, encoding="utf-8")
    return is_new


def add_agent(
    *,
    source_path: Path,
    name: str,
    wiki_path: Path,
    agents_dir: Path,
    review_existing: bool = False,
    update_existing: bool = False,
) -> dict:
    """Add a single agent: validate, gate, install, ingest, log.



    Returns a result dict with keys: name, installed, is_new_page.

    """
    validate_skill_name(name)

    file_size = source_path.stat().st_size
    if file_size > _MAX_AGENT_BYTES:
        raise ValueError(
            f"agent file too large ({file_size:,} bytes). Max "
            f"{_MAX_AGENT_BYTES:,}. Trim before ingestion."
        )

    content = source_path.read_text(encoding="utf-8", errors="replace")
    line_count = len(content.splitlines())

    installed_path = agents_dir / f"{name}.md"
    entity_page = wiki_path / "entities" / "agents" / f"{name}.md"
    existing_path = (
        installed_path
        if installed_path.exists()
        else entity_page if entity_page.exists() else None
    )
    has_existing = existing_path is not None

    if review_existing and has_existing and not update_existing:
        assert existing_path is not None
        existing_text = existing_path.read_text(encoding="utf-8", errors="replace")
        review = build_update_review(
            entity_type="agent",
            slug=name,
            existing_text=existing_text,
            proposed_text=content,
        )
        return {
            "name": name,
            "installed": str(installed_path),
            "is_new_page": False,
            "skipped": True,
            "update_required": True,
            "update_review": render_update_review(review),
        }

    decision = None
    if not has_existing:
        # Intake gate: reject broken/duplicate agents before we install.
        # Existing updates bypass similarity intake because they compare
        # against their own cached embedding.
        decision = check_intake(content, "agents")
        if not decision.allow:
            raise IntakeRejected(decision)

    # 1. Install into agents-dir.
    installed_path = install_agent(source_path, agents_dir, name)

    # 2. Record embedding. Non-fatal on failure — install already
    # succeeded and a missing vector only weakens the next check.
    try:
        record_embedding(subject_id=name, raw_md=content, subject_type="agents")
    except Exception as exc:  # noqa: BLE001 — cache failure must not break install
        print(
            f"Warning: failed to record intake embedding for {name}: {exc}",
            file=sys.stderr,
        )

    # 3. Write wiki entity page via the shared generator so the agent
    # page layout stays consistent with wiki_batch_entities output.
    page_content = generate_agent_page(name, installed_path)
    is_new = write_entity_page(wiki_path, name, page_content)

    # 4. Index + log.
    if is_new:
        update_index(str(wiki_path), [name], subject_type="agents")

    log_details = [
        f"Source: {source_path}",
        f"Installed: {installed_path}",
        f"Lines: {line_count}",
    ]
    warnings = decision.warnings if decision is not None else ()
    if warnings:
        log_details.append(
            "Warnings: " + "; ".join(f"{w.code}:{w.message}" for w in warnings)
        )
    append_log(str(wiki_path), "add-agent", name, log_details)
    queue_job = enqueue_entity_upsert(
        wiki_path,
        entity_type="agent",
        slug=name,
        entity_path=wiki_path / "entities" / "agents" / f"{name}.md",
        content=page_content,
        action="created" if is_new else "updated",
        source="agent_add",
    )

    return {
        "name": name,
        "installed": str(installed_path),
        "is_new_page": is_new,
        "skipped": False,
        "update_required": False,
        "queued_job_id": queue_job.id,
    }


# ── CLI ───────────────────────────────────────────────────────────────

def main() -> None:
    parser = argparse.ArgumentParser(description="Add new agents with wiki ingestion")
    parser.add_argument("--agent-path", help="Path to a single agent .md to add")
    parser.add_argument("--name", help="Agent name (required with --agent-path)")
    parser.add_argument(
        "--scan-dir",
        help="Directory of agent .md files to batch-add (non-recursive)",
    )
    parser.add_argument(
        "--skip-existing",
        action="store_true",
        help="Skip agents already installed (prevents overwrites)",
    )
    parser.add_argument(
        "--update-existing",
        action="store_true",
        help="Apply the reviewed replacement when an agent already exists",
    )
    parser.add_argument("--wiki", default=str(cfg.wiki_dir), help="Wiki path")
    parser.add_argument(
        "--agents-dir",
        default=str(cfg.agents_dir),
        help="Agents install path",
    )
    args = parser.parse_args()

    wiki_path = Path(os.path.expanduser(args.wiki))
    agents_dir = Path(os.path.expanduser(args.agents_dir))

    ensure_wiki(str(wiki_path))
    agents_dir.mkdir(parents=True, exist_ok=True)

    if args.agent_path and args.scan_dir:
        print("Error: use --agent-path or --scan-dir, not both.", file=sys.stderr)
        sys.exit(1)

    if not args.agent_path and not args.scan_dir:
        print("Error: --agent-path or --scan-dir is required.", file=sys.stderr)
        sys.exit(1)

    candidates: list[tuple[Path, str]] = []

    if args.agent_path:
        if not args.name:
            print("Error: --name is required with --agent-path.", file=sys.stderr)
            sys.exit(1)
        source = Path(os.path.expanduser(args.agent_path))
        if not source.exists():
            print(f"Error: {source} does not exist.", file=sys.stderr)
            sys.exit(1)
        candidates.append((source, args.name))

    if args.scan_dir:
        scan_root = Path(os.path.expanduser(args.scan_dir))
        if not scan_root.exists():
            print(f"Error: {scan_root} does not exist.", file=sys.stderr)
            sys.exit(1)
        # Non-recursive: only top-level *.md files. Nested agents are
        # rare enough that forcing an explicit path is safer than
        # surprise recursion sweeping up unrelated markdown.
        for agent_md in sorted(scan_root.glob("*.md")):
            candidates.append((agent_md, agent_md.stem))

        if not candidates:
            print(f"No agent .md files found under {scan_root}.", file=sys.stderr)
            sys.exit(0)

    added = updated = skipped = rejected = errors = 0
    total = len(candidates)
    for i, (source_path, name) in enumerate(candidates, 1):
        if args.skip_existing and (agents_dir / f"{name}.md").exists():
            skipped += 1
            if skipped <= 5 or skipped % 100 == 0:
                print(f"  [{i}/{total}] [skipped] {name}")
            continue
        try:
            result = add_agent(
                source_path=source_path,
                name=name,
                wiki_path=wiki_path,
                agents_dir=agents_dir,
                review_existing=True,
                update_existing=args.update_existing,
            )
            if result.get("skipped"):
                skipped += 1
                if result.get("update_review"):
                    print(result["update_review"])
                print(f"  [{i}/{total}] [update-review] {name}")
                continue
            if result["is_new_page"]:
                added += 1
                status = "installed"
            else:
                updated += 1
                status = "updated"
            print(f"  [{i}/{total}] [{status}] {name}")
        except IntakeRejected as exc:
            rejected += 1
            codes = ", ".join(f.code for f in exc.decision.failures) or "unknown"
            print(f"  [{i}/{total}] [rejected] {name}: {codes}", file=sys.stderr)
        except Exception as exc:  # noqa: BLE001 — batch CLI must continue past one failure
            errors += 1
            print(f"  [{i}/{total}] ERROR: {name}: {exc}", file=sys.stderr)

    print(
        f"\nDone: {added} added, {updated} updated, {skipped} skipped, "
        f"{rejected} rejected, {errors} errors"
    )


if __name__ == "__main__":
    main()