File size: 1,841 Bytes
a02272f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""brain sync — Create a versioned snapshot of the active Brain Instance."""

import shutil
import typer

from brain.config import get_active_brain
from brain.paths import get_workspace_dir
from brain.utils.display import error, success, info

import json


def sync() -> None:
    """Snapshot the active Brain Instance into versions/vN/."""
    active = get_active_brain()

    if not active or not active.exists():
        error("No active Brain selected.")
        error("Run: brain use <brain-name>")
        raise typer.Exit(code=1)

    versions_dir = active / "versions"
    versions_dir.mkdir(exist_ok=True)

    # Determine next version number
    existing = [
        d for d in versions_dir.iterdir()
        if d.is_dir() and d.name.startswith("v") and d.name[1:].isdigit()
    ]
    if existing:
        latest_num = max(int(d.name[1:]) for d in existing)
        next_num = latest_num + 1
    else:
        next_num = 1

    version_name = f"v{next_num}"
    target = versions_dir / version_name

    # Full copy, EXCLUDING versions/ to avoid recursive growth
    def _ignore_versions(directory: str, contents: list[str]) -> list[str]:
        if directory == str(active):
            return ["versions"]
        return []

    shutil.copytree(str(active), str(target), ignore=_ignore_versions)

    # Update workspace BRAIN.json if connected
    workspace = get_workspace_dir()
    if workspace.exists():
        brain_json_path = workspace / "BRAIN.json"
        if brain_json_path.exists():
            brain_json = json.loads(brain_json_path.read_text(encoding="utf-8"))
            brain_json["version"] = version_name
            brain_json_path.write_text(
                json.dumps(brain_json, indent=2), encoding="utf-8"
            )

    success(f"Brain synced -> {version_name}")
    info(f"Snapshot: {target}")