---
name: CBM Environment Preflight
description: Ensure environment readiness for CBM CLI commands via safe execution planning.
---

# CBM Environment Preflight (Safe Execution Plan)

## Core Constraints (Tool Use Optimization)
Before executing any CBM commands, the Agent MUST follow this detection logic, merging specific Shell scripts with safe execution strategies:

### 1. Command Detection & Path Resolution
- **Detection Script**:
  ```bash
  test -f ./node_modules/.bin/cbm && echo "LOCAL" || (which cbm > /dev/null && echo "GLOBAL" || echo "MISSING")
  ```
- **Execution Standards**:
  - **LOCAL**: Subsequent commands MUST use `./node_modules/.bin/cbm`.
  - **GLOBAL**: Use `cbm` from the system path.
  - **MISSING**: MUST stop and guide the user to install (using private registry):
    `npm install -g @cbm/cbm-cli --registry http://ires.58corp.com/repository/58npm/`

### 2. Deep Port Scanning
- **Monitored Ports**: 5885 (Worker), 3001 (MCP), 3000 (UI).
- **Detection Command**: `lsof -i :5885 -i :3001 -i :3000 2>/dev/null | grep LISTEN`
- **Strategy**: If any output is detected, force mark as `ERROR_PORT_CONFLICT`.
- **Guidance**: Prompt the user to run `/cbm-destroy` or manually kill PIDs.

### 3. Node.js Runtime Alignment
- **Minimum Baseline**: MUST be >= 14.15.3.
- **Modern Recommendation**: ^18.0.0 (Required for modern VitePress doc sync).
- **Validation Logic**:
  - Run `node -v`.
  - If version < 14.15.3, error out and block startup.
  - If 14 <= version < 18, output `WARN_NODE_VERSION` regarding compatibility risks.

### 4. Project Configuration Detection
- **Detection Command**: `test -f cbm.config.js && echo "CONFIG_OK" || echo "MISSING"`
- **Strategy**: If `MISSING`, guide the user to run `cbm init` in the current directory.

## Exception Status Codes
- `ERROR_PORT_CONFLICT`: Port occupied, cleanup recommended.
- `ERROR_CONFIG_MISSING`: Configuration file missing, guide initialization.
- `WARN_NODE_VERSION`: Node version is not the recommended version, notify potential risks.

## Report Summary Template
```text
=== CBM Environment Preflight Report ===
[✓] CBM CLI: Ready (Locality: GLOBAL/LOCAL)
[✓] Project Config: cbm.config.js Located
[!] Port Check: 5885 Occupied (PID: xxx)
[✓] Node Version: v18.x.x (Recommended)

Status Recommendation: [PASS / WARN / ERROR]
```
