"""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 ") 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}")