File size: 3,689 Bytes
a02272f
 
 
 
 
 
 
64b4b5e
a02272f
 
 
 
 
 
 
 
 
 
 
 
 
 
c4fa2b0
64b4b5e
 
37b748e
 
 
 
 
 
 
a02272f
c4fa2b0
 
 
 
 
37b748e
a02272f
 
 
c4fa2b0
a02272f
 
 
 
37b748e
 
 
 
 
 
 
 
 
 
 
a02272f
 
 
 
 
 
 
 
 
 
c4fa2b0
a02272f
64b4b5e
c4fa2b0
 
 
37b748e
 
c4fa2b0
64b4b5e
 
 
 
 
 
 
 
 
 
 
 
 
 
a02272f
64b4b5e
 
37b748e
 
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
"""brain attach β€” Register the current workspace as a project inside the Brain Instance."""

import json
import typer
from pathlib import Path

from brain.config import get_active_brain
from brain.paths import get_workspace_dir, get_project_root
from brain.templates.project_memory import scaffold_project_memory
from brain.templates.agent_protocol import generate_agent_md
from brain.utils.git import detect_project_identity
from brain.utils.display import error, success, info

def attach(
    full: bool = typer.Option(
        False,
        "--full",
        help="Import the full Brain Instance structure into the workspace runtime.",
    ),
) -> None:
    """Attach the current workspace to the active Brain Instance as a project."""
    workspace = get_workspace_dir()
    workspace.mkdir(parents=True, exist_ok=True)
    
    root = get_project_root()
    
    # ── Safety Check ──
    # If root is the filesystem root, someone is trying to attach C:\
    if root.parent == root:
        error(f"Cannot attach system root: {root}")
        info("Please navigate to your project folder and run 'ever-brain init' first.")
        raise typer.Exit(code=1)

    # Get active brain from local config
    brain_path = get_active_brain()
    
    if not brain_path:
        error("No active Brain selected.")
        info("Run: ever-brain use <name>")
        raise typer.Exit(code=1)

    if not brain_path.exists():
        error(f"Active Brain not found at: {brain_path}")
        raise typer.Exit(code=1)

    # Detect project identity
    project_name = detect_project_identity()
    
    # ── Storage Mode Logic ──
    from brain.config import get_storage_mode
    mode = get_storage_mode()
    
    if mode == "local":
        # Project memory is strictly inside the project's designated folder
        project_path = workspace / "memory"
    else:
        # Project memory is stored globally within the Brain Instance
        project_path = brain_path / "projects" / project_name

    # Create or resume project memory
    if project_path.exists():
        info(f"Resuming existing project memory: {project_name}")
    else:
        scaffold_project_memory(project_path)
        success(f"Created project memory: {project_name}")

    # Update workspace runtime metadata
    (workspace / "current_project").write_text(project_name, encoding="utf-8")
    (workspace / "active_brain_name").write_text(brain_path.name, encoding="utf-8")

    # Update BRAIN.json
    brain_json = {
        "project": project_name,
        "brain": brain_path.name,
        "mode": f"ever-brain-{mode}",
        "memory_path": str(project_path.resolve())
    }
    (workspace / "BRAIN.json").write_text(json.dumps(brain_json, indent=2), encoding="utf-8")

    # 1. Generate AGENT.md in project ROOT for visibility
    agent_md = generate_agent_md(brain_path.name, brain_path, project_name)
    (root / "AGENT.md").write_text(agent_md, encoding="utf-8")
    
    # 2. Generate .cursorrules for AUTO-DISCOVERY
    cursor_rules = (
        "### EVER BRAIN AUTO-DISCOVERY\n"
        "At the start of every session, you MUST read **AGENT.md** in the root directory.\n"
        "This file synchronizes you with the project's persistent cognitive state.\n"
        "Always use the tools and memory structures defined therein to maintain operational continuity.\n"
    )
    (root / ".cursorrules").write_text(cursor_rules, encoding="utf-8")

    success(f"Attached project '{project_name}' to Ever Brain '{brain_path.name}'.")
    info("Created AGENT.md and .cursorrules for automatic AI discovery.")
    info(f"Storage Mode:   {mode.upper()}")
    info(f"Memory Path:    {project_path}")