Skip to content

Open-source Revit MCP server: let Claude and other AI assistants read and edit BIM models, with human approval on every change.

8.9 KB
Raw Blame
import uuid
import json
import logging
from datetime import datetime, timezone
from pathlib import Path
from typing import Dict, Any, List, Optional
from ..config import config
from ..errors import BridgeError
from . import proof as proof_mod

logger = logging.getLogger(__name__)

class ApprovalGate:
    def __init__(self, workspace_dir: Path, approval_mode: str = config.approval_mode) -> None:
        self.workspace_dir = workspace_dir
        self.approval_mode = approval_mode
        self.plans_dir = workspace_dir / "plans"
        self.plans_dir.mkdir(parents=True, exist_ok=True)

    def _get_plan_path(self, plan_id: str) -> Path:
        return self.plans_dir / f"{plan_id}.json"

    def load_plan(self, plan_id: str) -> Optional[Dict[str, Any]]:
        path = self._get_plan_path(plan_id)
        if not path.exists():
            return None
        try:
            with open(path, "r", encoding="utf-8") as f:
                return json.load(f)
        except Exception as e:
            logger.error("Failed to load plan %s: %s", plan_id, e)
            return None

    def save_plan(self, plan: Dict[str, Any]) -> None:
        plan_id = plan["plan_id"]
        path = self._get_plan_path(plan_id)
        try:
            with open(path, "w", encoding="utf-8") as f:
                json.dump(plan, f, indent=2)
        except Exception as e:
            logger.error("Failed to save plan %s: %s", plan_id, e)

    def create_plan(
        self,
        actions: List[Dict[str, Any]],
        before_states: List[Dict[str, Any]],
        snapshot_id: Optional[str] = None,
        skipped: Optional[List[Dict[str, Any]]] = None,
        extra: Optional[Dict[str, Any]] = None,
        before_storage_types: Optional[List[Optional[str]]] = None,
    ) -> Dict[str, Any]:
        plan_id = f"plan_{uuid.uuid4().hex[:12]}"
        plan_actions = []
        for i, action in enumerate(actions):
            act_id = f"act_{uuid.uuid4().hex[:12]}"
            before = before_states[i] if i < len(before_states) else {}
            
            arguments = action.get("arguments", {})
            diff = {
                "type": "parameter_change" if "parameter_name" in arguments else "model_modification",
                "before": before,
                "after": arguments,
                "element_count": 1
            }
            if before_storage_types and i < len(before_storage_types) and before_storage_types[i]:
                # Revit StorageType of the parameter when the before value was captured, so a
                # revert can rebuild a correctly typed value (the live read returns strings).
                diff["before_storage_type"] = before_storage_types[i]
            plan_actions.append({
                "action_id": act_id,
                "tool": action.get("tool"),
                "arguments": arguments,
                "diff": diff,
                "state": "pending"
            })

        plan = {
            "plan_id": plan_id,
            "state": "pending",
            "created_at": datetime.now(timezone.utc).isoformat(),
            "actions": plan_actions,
            "is_reversible": True,
            "reversible_strategy": "inverse"
        }
        if snapshot_id:
            plan["snapshot_id"] = snapshot_id
        if skipped:
            plan["skipped"] = skipped
        if extra:
            plan.update(extra)
        self.save_plan(plan)
        return plan

    def list_pending_plans(self) -> List[Dict[str, Any]]:
        plans = []
        for path in self.plans_dir.glob("*.json"):
            try:
                with open(path, "r", encoding="utf-8") as f:
                    plan = json.load(f)
                    if plan.get("state") == "pending":
                        plans.append(plan)
            except Exception:
                continue
        return plans

    def update_plan_state(self, plan_id: str, state: str, approver: Optional[str] = None) -> Dict[str, Any]:
        plan = self.load_plan(plan_id)
        if not plan:
            raise ValueError(f"Plan {plan_id} not found")
        plan["state"] = state
        now = datetime.now(timezone.utc).isoformat()
        if state == "approved":
            plan["approved_at"] = now
            if approver:
                plan["approved_by"] = str(approver)
        fill_proof = False
        if state == "executed":
            plan["executed_at"] = now
            # execute_plan writes a richer proof itself; only fill in for direct tool calls.
            try:
                fill_proof = proof_mod.read_proof(self.workspace_dir, plan_id) is None
            except ValueError:
                fill_proof = False
        self.save_plan(plan)
        if fill_proof:
            try:
                self.record_proof(plan)
            except Exception:
                logger.exception("Failed to write proof bundle for plan %s", plan_id)
        return plan

    def record_proof(self, plan: Dict[str, Any], results: Optional[List[Dict[str, Any]]] = None,
                     outcome: Optional[str] = None) -> Dict[str, Any]:
        """Write proofs/<plan_id>.json for a finished (success / partial / failed) plan."""
        bundle = proof_mod.build_proof(plan, self.workspace_dir, results=results, outcome=outcome)
        proof_mod.write_proof(self.workspace_dir, bundle)
        return bundle

    def load_proof(self, plan_id: str) -> Optional[Dict[str, Any]]:
        return proof_mod.read_proof(self.workspace_dir, plan_id)

    def check_tool_execution(self, tool_name: str, arguments: Dict[str, Any]) -> None:
        """
        Interceptors check before execution.
        """
        if self.approval_mode != "required":
            return

        # Check if plan_id is provided
        plan_id = arguments.get("plan_id")
        if not plan_id:
            raise BridgeError(f"Approval mode is enabled. Mutating tool '{tool_name}' requires a valid 'plan_id' parameter.")

        plan = self.load_plan(plan_id)
        if not plan:
            raise BridgeError(f"Plan '{plan_id}' does not exist.")

        if plan.get("state") != "approved":
            raise BridgeError(f"Plan '{plan_id}' is in state '{plan.get('state')}', not 'approved'. Execution blocked.")

    async def rollback_plan(self, plan_id: str, execute_fn) -> Dict[str, Any]:
        """Roll back an executed plan.

        `execute_fn` is an async callable `(tool_name, arguments) -> dict`; it MUST be
        awaited here rather than merely invoked, otherwise the inverse tool call never
        actually runs (a bare call just constructs and discards a coroutine object).

        Tools without a registered rollback handler are recorded as warnings (not
        errors) so that a mixed plan (some rollback-able, some not) still rolls back
        what it can and reports clearly what was skipped.
        """
        plan = self.load_plan(plan_id)
        if not plan:
            raise ValueError(f"Plan {plan_id} not found")
        if plan.get("state") != "executed":
            raise ValueError(f"Plan {plan_id} is in state '{plan.get('state')}', cannot rollback. Only executed plans can be rolled back.")

        errors = []
        warnings = []
        for action in reversed(plan["actions"]):
            tool = action["tool"]
            before = action["diff"]["before"]
            if tool == "revit_set_parameter_value":
                elem_id = action["arguments"].get("element_id")
                param_name = action["arguments"].get("parameter_name")
                old_val = before.get(str(elem_id), {}).get(param_name)
                if old_val is not None:
                    try:
                        # Make sure to bypass or satisfy the approved state check
                        # We temporarily set the plan state to 'approved' for the rollback calls
                        plan["state"] = "approved"
                        self.save_plan(plan)
                        await execute_fn("revit_set_parameter_value", {
                            "element_id": elem_id,
                            "parameter_name": param_name,
                            "value": old_val,
                            "plan_id": plan_id
                        })
                    except Exception as e:
                        errors.append(f"Failed to rollback action {action.get('action_id')}: {e}")
                    finally:
                        plan["state"] = "executed"
                        self.save_plan(plan)
                else:
                    warnings.append(f"Skipped rollback for action {action.get('action_id')}: no before-value recorded")
            else:
                warnings.append(f"No rollback handler for tool '{tool}' (action {action.get('action_id')}) — skipped")

        if errors:
            plan["state"] = "partial_rollback"
            plan["rollback_warnings"] = warnings
            self.save_plan(plan)
            raise BridgeError(f"Rollback encountered errors: {'; '.join(errors)}")

        plan["state"] = "rolled_back"
        plan["rollback_warnings"] = warnings
        self.save_plan(plan)
        return plan