-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstall.sh
More file actions
executable file
·336 lines (303 loc) · 14 KB
/
Copy pathinstall.sh
File metadata and controls
executable file
·336 lines (303 loc) · 14 KB
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
#!/bin/bash
# =============================================================================
# Cortex Install Script
# =============================================================================
# Usage: ./install.sh [--full]
#
# --full Also install optional analytics + orchestration packages
# (xgboost, shap, openai, litellm). Default: core only.
#
# Prerequisites: Python 3.11+, git. Homebrew auto-offered if Python missing.
# =============================================================================
set -euo pipefail
# ─── Flags ────────────────────────────────────────────────────────────────────
FULL_INSTALL=false
for arg in "$@"; do
case "$arg" in
--full) FULL_INSTALL=true ;;
*) echo "Unknown argument: $arg"; exit 1 ;;
esac
done
# ─── Paths ────────────────────────────────────────────────────────────────────
SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
CORTEX_DIR="$SCRIPT_DIR"
DEV_DIR="$(dirname "$CORTEX_DIR")"
HOME_DIR="$HOME"
CORTEX_STATE="$HOME_DIR/.cortex"
VENV="$CORTEX_DIR/.venv"
LOCAL_BIN="$HOME_DIR/.local/bin"
PYTHON_BIN=""
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m'
log() { echo -e "${GREEN}[+]${NC} $1"; }
warn() { echo -e "${YELLOW}[!]${NC} $1"; }
fail() { echo -e "${RED}[x]${NC} $1"; exit 1; }
info() { echo -e "${BLUE}[i]${NC} $1"; }
# ─── Step 0: Prerequisites ────────────────────────────────────────────────────
log "Checking prerequisites..."
# Find Python 3.11+ — accept any version >= 3.11
for py in python3.13 python3.12 python3.11; do
if command -v "$py" >/dev/null 2>&1; then
PYTHON_BIN="$py"
break
fi
done
if [ -z "$PYTHON_BIN" ]; then
warn "Python 3.11+ not found."
if command -v brew >/dev/null 2>&1; then
INSTALL_PY="Y"
[ -t 0 ] && { read -rp " Install Python 3.11 via Homebrew now? [Y/n]: " INSTALL_PY || true; }
if [[ "$INSTALL_PY" =~ ^[Yy] ]]; then
brew install python@3.11
PYTHON_BIN="python3.11"
else
fail "Python 3.11+ required. Run: brew install python@3.11"
fi
else
fail "Python 3.11+ not found and Homebrew is not installed.\nInstall Homebrew first:\n /bin/bash -c \"\$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)\"\nThen re-run this script."
fi
fi
PYTHON_VERSION=$("$PYTHON_BIN" -c "import sys; print(f'{sys.version_info.major}.{sys.version_info.minor}')")
PYTHON_MINOR=$("$PYTHON_BIN" -c "import sys; print(sys.version_info.minor)")
if [[ "$PYTHON_MINOR" -lt 11 ]]; then
fail "Python 3.11+ required, got $PYTHON_VERSION"
fi
log "Python $PYTHON_VERSION OK"
command -v git >/dev/null 2>&1 || fail "git not found. Install via Xcode CLT: xcode-select --install"
command -v node >/dev/null 2>&1 || warn "node not found — site dashboard will not work (optional)"
# ─── Step 1: Virtual environment ─────────────────────────────────────────────
if [ ! -f "$VENV/bin/python" ]; then
log "Creating virtual environment..."
"$PYTHON_BIN" -m venv "$VENV"
else
log "Virtual environment already exists"
fi
log "Installing Python dependencies..."
"$VENV/bin/pip" install --upgrade pip setuptools wheel -q
PIP_LOG=$(mktemp)
if [ "$FULL_INSTALL" = true ]; then
info "Full install: adding analytics + orchestration packages (this takes a few minutes)..."
if ! "$VENV/bin/pip" install -e ".[all,server,dev]" 2>"$PIP_LOG"; then
warn "Some optional packages failed — core install still works:"
grep "^ERROR" "$PIP_LOG" | head -5 || true
fi
else
# Default install includes [server] (FastAPI bridge) and [dev] (pytest)
# so a fresh-clone user can run `cortex briefing`, the bridge, and the
# test suite the README badge advertises without a separate pip step.
if ! "$VENV/bin/pip" install -e ".[server,dev]" 2>"$PIP_LOG"; then
cat "$PIP_LOG"
rm -f "$PIP_LOG"
fail "Core package install failed. See errors above."
fi
fi
rm -f "$PIP_LOG"
"$VENV/bin/pip" install chromadb -q 2>/dev/null || warn "chromadb install failed (optional vector store)"
log "Python packages installed"
# ─── Step 2: State directories ───────────────────────────────────────────────
log "Creating state directories..."
mkdir -p "$CORTEX_STATE"/{logs,metrics,batch,secrets,prompts,flywheel,session_metrics,pid_sessions}
chmod 700 "$CORTEX_STATE"
# ─── Step 3: Environment configuration ───────────────────────────────────────
if [ ! -f "$CORTEX_DIR/.env" ]; then
if [ -f "$CORTEX_DIR/.env.template" ]; then
cp "$CORTEX_DIR/.env.template" "$CORTEX_DIR/.env"
else
cat > "$CORTEX_DIR/.env" <<ENVEOF
# Cortex Environment Configuration
CORTEX_ROOT_DIR=/path/to/your/projects
CORTEX_STATE_DIR=$CORTEX_STATE
# Required: Anthropic API key
# ANTHROPIC_API_KEY=sk-ant-...
# Optional: Multi-provider keys
# OPENAI_API_KEY=
# GROQ_API_KEY=
# DEEPSEEK_API_KEY=
ENVEOF
fi
log ".env created"
else
log ".env already exists"
fi
# Prompt for ANTHROPIC_API_KEY — skip only if a real key (>20 chars) is already present
_key_is_real() {
local key="$1"
[[ "$key" == sk-* ]] && [ "${#key}" -gt 20 ]
}
ENV_FILE_KEY=$(grep -E "^ANTHROPIC_API_KEY=sk-" "$CORTEX_DIR/.env" 2>/dev/null | cut -d= -f2 || true)
if _key_is_real "${ENV_FILE_KEY:-}" || _key_is_real "${ANTHROPIC_API_KEY:-}"; then
log "ANTHROPIC_API_KEY already configured"
elif [ ! -t 0 ]; then
warn "Non-interactive install: set ANTHROPIC_API_KEY in $CORTEX_DIR/.env before running cortex"
else
echo ""
echo " Anthropic API key required for intelligence features."
echo " Get yours at: https://console.anthropic.com/settings/keys"
while true; do
read -rp " Enter ANTHROPIC_API_KEY (sk-ant-...): " INPUT_KEY || true
if _key_is_real "${INPUT_KEY:-}"; then
sed -i '' "s|^# ANTHROPIC_API_KEY=.*|ANTHROPIC_API_KEY=$INPUT_KEY|" "$CORTEX_DIR/.env"
sed -i '' "s|^ANTHROPIC_API_KEY=sk-ant-api03-YOUR_KEY_HERE|ANTHROPIC_API_KEY=$INPUT_KEY|" "$CORTEX_DIR/.env"
export ANTHROPIC_API_KEY="$INPUT_KEY"
log "ANTHROPIC_API_KEY saved"
break
else
warn "Key must start with 'sk-' and be a valid length. Try again (or Ctrl+C to skip)."
fi
done
fi
# Prompt for CORTEX_ROOT_DIR — show actual default so user can just hit Enter
INPUT_ROOT=""
if grep -q "CORTEX_ROOT_DIR=/path/to/your/projects" "$CORTEX_DIR/.env" 2>/dev/null; then
echo ""
INPUT_ROOT=""
[ -t 0 ] && { read -rp " Projects root directory [${DEV_DIR}]: " INPUT_ROOT || true; }
INPUT_ROOT="${INPUT_ROOT/#\~/$HOME}"
INPUT_ROOT="${INPUT_ROOT:-$DEV_DIR}"
sed -i '' "s|CORTEX_ROOT_DIR=.*|CORTEX_ROOT_DIR=$INPUT_ROOT|" "$CORTEX_DIR/.env"
log "CORTEX_ROOT_DIR set to $INPUT_ROOT"
else
INPUT_ROOT=$(grep "^CORTEX_ROOT_DIR=" "$CORTEX_DIR/.env" 2>/dev/null | cut -d= -f2 || echo "$DEV_DIR")
fi
# Store API key for batch daemons
ACTIVE_KEY="${ANTHROPIC_API_KEY:-$ENV_FILE_KEY}"
if [ -n "${ACTIVE_KEY:-}" ]; then
mkdir -p "$CORTEX_STATE/secrets"
echo "$ACTIVE_KEY" > "$CORTEX_STATE/secrets/anthropic_batch_key"
chmod 600 "$CORTEX_STATE/secrets/anthropic_batch_key"
fi
# ─── Step 4: PATH — cortex entrypoint always available ───────────────────────
mkdir -p "$LOCAL_BIN"
ln -sf "$VENV/bin/cortex" "$LOCAL_BIN/cortex"
ln -sf "$VENV/bin/cx" "$LOCAL_BIN/cx" 2>/dev/null || true
PYTHON311_PATH=$(command -v "$PYTHON_BIN" 2>/dev/null || true)
if [ -n "$PYTHON311_PATH" ]; then
ln -sf "$PYTHON311_PATH" "$LOCAL_BIN/python3"
ln -sf "$PYTHON311_PATH" "$LOCAL_BIN/python"
fi
log "cortex linked to ~/.local/bin/cortex"
# Add ~/.local/bin to PATH in shell profile if not already there
PROFILE_UPDATED=false
for profile in "$HOME_DIR/.zprofile" "$HOME_DIR/.zshrc" "$HOME_DIR/.bash_profile" "$HOME_DIR/.bashrc"; do
if [ -f "$profile" ] && ! grep -q '\.local/bin' "$profile" 2>/dev/null; then
echo '' >> "$profile"
echo 'export PATH="$HOME/.local/bin:$PATH"' >> "$profile"
log "Added ~/.local/bin to PATH in $(basename "$profile")"
PROFILE_UPDATED=true
break
fi
done
export PATH="$LOCAL_BIN:$PATH"
# ─── Step 5: Site dashboard ──────────────────────────────────────────────────
if [ -f "$CORTEX_DIR/site/package.json" ] && command -v npm >/dev/null 2>&1; then
if [ ! -d "$CORTEX_DIR/site/node_modules" ]; then
log "Installing site dashboard dependencies..."
cd "$CORTEX_DIR/site" && npm install --silent 2>/dev/null
cd "$CORTEX_DIR"
else
log "Site dashboard node_modules already present"
fi
fi
# ─── Step 6: LaunchAgents (macOS only) ──────────────────────────────────────
if [ "$(uname)" = "Darwin" ]; then
echo ""
info "Background agents run nightly analysis, batch jobs, and session monitoring."
info "They are user-level LaunchAgents — no root access required."
INSTALL_AGENTS="Y"
[ -t 0 ] && { read -rp " Install background agents? [Y/n]: " INSTALL_AGENTS || true; }
if [[ "$INSTALL_AGENTS" =~ ^[Yy] ]]; then
AGENTS_DIR="$HOME_DIR/Library/LaunchAgents"
mkdir -p "$AGENTS_DIR"
install_plist() {
local src="$1"
local name
name=$(basename "$src")
local dest="$AGENTS_DIR/$name"
[ -f "$dest" ] && return 0
cp "$src" "$dest"
sed -i '' \
-e "s|/Users/jesse.kemp/Dev|$DEV_DIR|g" \
-e "s|/Users/jesse/Dev|$DEV_DIR|g" \
-e "s|/Users/jesse.kemp|$HOME_DIR|g" \
-e "s|/Users/jesse/dev/venv/|$VENV/|g" \
-e "s|/Users/jesse/dev/cortex/venv/|$VENV/|g" \
-e "s|alpha_arena/venv/|alpha_arena/.venv/|g" \
-e "s|Vortex/backend/venv/|Vortex/backend/.venv/|g" \
"$dest"
echo " Installed: $name"
}
for plist in "$CORTEX_DIR"/batch/automation/*.plist "$CORTEX_DIR"/*.plist; do
[ -f "$plist" ] && install_plist "$plist"
done
log "LaunchAgents installed"
log "Load now: launchctl load ~/Library/LaunchAgents/com.cortex.*.plist"
else
log "Skipped background agents"
fi
fi
# ─── Step 7: Fix Claude Code hooks REPO_ROOT (if present) ───────────────────
HOOKS_DIR="$DEV_DIR/.claude/hooks"
if [ -d "$HOOKS_DIR" ]; then
for hook in "$HOOKS_DIR"/*.py; do
if grep -q 'REPO_ROOT.*=.*"/Users/' "$hook" 2>/dev/null; then
sed -i '' "s|REPO_ROOT.*=.*/Users/[^\"]*\"|REPO_ROOT = \"$DEV_DIR\"|g" "$hook"
fi
done
log "Claude Code hooks updated with correct REPO_ROOT"
fi
# ─── Step 8: Verify ──────────────────────────────────────────────────────────
echo ""
echo "╔════════════════════════════════════════╗"
echo "║ CORTEX INSTALL VERIFICATION ║"
echo "╚════════════════════════════════════════╝"
echo ""
echo " — Install —"
"$VENV/bin/python" -c "from cli import main" 2>/dev/null \
&& echo " ✅ cortex package importable" \
|| echo " ❌ cortex package import failed"
[ -d "$CORTEX_STATE/logs" ] \
&& echo " ✅ ~/.cortex/ state directory ready" \
|| echo " ❌ ~/.cortex/ state directory missing"
[ -f "$CORTEX_DIR/.env" ] \
&& echo " ✅ .env present" \
|| echo " ❌ .env missing"
[ -L "$LOCAL_BIN/cortex" ] \
&& echo " ✅ cortex on PATH (~/.local/bin/cortex)" \
|| echo " ⚠️ cortex not linked to ~/.local/bin"
echo ""
echo " — Runtime —"
VERIFY_KEY=$(grep -E "^ANTHROPIC_API_KEY=sk-" "$CORTEX_DIR/.env" 2>/dev/null | cut -d= -f2 || true)
if _key_is_real "${VERIFY_KEY:-}" || _key_is_real "${ANTHROPIC_API_KEY:-}"; then
echo " ✅ ANTHROPIC_API_KEY configured"
else
echo " ⚠️ ANTHROPIC_API_KEY not set"
fi
"$VENV/bin/python" -m cli health 2>&1 | grep -q "All Systems Operational" \
&& echo " ✅ cortex health: all systems operational" \
|| echo " ⚠️ cortex health: some systems degraded"
[ -d "$CORTEX_DIR/site/node_modules" ] \
&& echo " ✅ site dashboard ready" \
|| echo " ⚠️ site dashboard not set up (optional)"
echo ""
log "Install complete."
echo ""
if [ "$PROFILE_UPDATED" = true ]; then
echo " PATH updated — open a new terminal or run: source ~/.zprofile"
fi
echo " Run: cortex status"
echo ""
# ─── Step 9: Onboard ─────────────────────────────────────────────────────────
if [ -n "${INPUT_ROOT:-}" ] && [ -d "${INPUT_ROOT:-}" ]; then
echo ""
info "cortex onboard scans $INPUT_ROOT, detects projects, and seeds memory."
RUN_ONBOARD="n"
[ -t 0 ] && { read -rp " Run cortex onboard now? [Y/n]: " RUN_ONBOARD || true; }
if [[ "$RUN_ONBOARD" =~ ^[Yy] ]]; then
log "Running cortex onboard..."
"$VENV/bin/python" -m cli onboard --root "$INPUT_ROOT" --non-interactive 2>&1 \
|| warn "Onboard completed with warnings — run 'cortex onboard' manually to retry"
fi
fi