-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbootstrap.py
More file actions
368 lines (320 loc) · 14.2 KB
/
Copy pathbootstrap.py
File metadata and controls
368 lines (320 loc) · 14.2 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
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
"""
MetaOps — one-command bootstrap
curl -sSL https://raw.githubusercontent.com/Metrium987/MetaOps/main/bootstrap.py | python -
"""
import sys
import shutil
import subprocess
import sqlite3
from pathlib import Path
REPO_URL = "https://github.com/Metrium987/MetaOps.git"
REPO_DIR = "MetaOps"
if sys.platform == "win32":
try:
sys.stdout.reconfigure(encoding="utf-8", errors="replace")
except Exception:
pass
BOLD = "\033[1m"
OK = "\033[92m[OK]\033[0m"
FAIL = "\033[91m[FAIL]\033[0m"
WARN = "\033[93m[WARN]\033[0m"
INFO = "\033[94m[-->]\033[0m"
RESET = "\033[0m"
def ok(msg):
print(f" {OK} {msg}")
def fail(msg):
print(f" {FAIL} {msg}")
sys.exit(1)
def warn(msg):
print(f" {WARN} {msg}")
def info(msg):
print(f" {INFO} {msg}")
def header(msg):
print(f"\n{BOLD}{msg}{RESET}")
PROJECT_ROOT = Path.cwd() / REPO_DIR
DATA_DIR = PROJECT_ROOT / ".data"
VENV_DIR = PROJECT_ROOT / ".venv"
ENV_FILE = PROJECT_ROOT / ".env"
ENV_EXAMPLE = PROJECT_ROOT / ".env.example"
if sys.platform == "win32":
VENV_PYTHON = VENV_DIR / "Scripts" / "python.exe"
VENV_BIN = VENV_DIR / "Scripts"
else:
VENV_PYTHON = VENV_DIR / "bin" / "python"
VENV_BIN = VENV_DIR / "bin"
header("MetaOps — Setup")
print(f" {REPO_URL}\n")
# ── 1. Python 3.10+ ───────────────────────────────────────────────────────────
header("1 / 5 Python version")
major, minor = sys.version_info[:2]
if (major, minor) < (3, 10):
fail(f"Python 3.10+ required — found {major}.{minor}")
ok(f"Python {major}.{minor} ({sys.executable})")
# ── 2. System dependencies ────────────────────────────────────────────────────
header("2 / 5 System dependencies")
git = shutil.which("git")
if not git:
info("git not found — installing...")
for pkg_mgr in ["apt-get", "apt", "dnf", "yum", "brew", "pacman", "zypper"]:
if shutil.which(pkg_mgr):
cmd = (
["brew", "install", "git"]
if pkg_mgr == "brew"
else ["sudo", pkg_mgr, "install", "-y", "git"]
)
subprocess.run(cmd)
git = shutil.which("git")
break
if not git:
fail("git not found — install manually: sudo apt-get install -y git")
ok(f"git: {git}")
npx = shutil.which("npx")
if not npx:
info("npx not found — installing Node.js...")
if sys.platform == "win32":
for mgr, cmd in [
(
"winget",
["winget", "install", "--id", "OpenJS.NodeJS", "-e", "--silent"],
),
("choco", ["choco", "install", "nodejs", "-y"]),
]:
if shutil.which(mgr):
subprocess.run(cmd)
break
else:
for pkg_mgr in ["apt-get", "apt", "dnf", "yum", "brew", "pacman", "zypper"]:
if shutil.which(pkg_mgr):
cmd = (
["brew", "install", "node"]
if pkg_mgr == "brew"
else ["sudo", pkg_mgr, "install", "-y", "nodejs", "npm"]
)
subprocess.run(cmd)
break
npx = shutil.which("npx")
if npx:
ok(f"npx: {npx}")
else:
warn("npx not found — MCP servers will not work")
uv = shutil.which("uv")
if not uv:
info("uv not found — installing...")
if sys.platform == "win32":
subprocess.run(
[sys.executable, "-m", "pip", "install", "uv", "--quiet"],
capture_output=True,
)
uv = shutil.which("uv")
else:
subprocess.run("curl -LsSf https://astral.sh/uv/install.sh | sh", shell=True)
for candidate in [
Path.home() / ".local" / "bin" / "uv",
Path.home() / ".cargo" / "bin" / "uv",
]:
if candidate.exists():
uv = str(candidate)
break
if not uv:
uv = shutil.which("uv")
if uv and Path(uv).exists():
ok(f"uv: {uv}")
else:
fail("Could not install uv")
# ── 3. Clone or update ────────────────────────────────────────────────────────
header("3 / 5 Repository")
if PROJECT_ROOT.exists():
info(f"{REPO_DIR} exists — pulling latest...")
r = subprocess.run(
["git", "-C", str(PROJECT_ROOT), "pull", "--ff-only"],
capture_output=True,
text=True,
)
if r.returncode != 0:
warn(f"git pull failed — using existing\n{r.stderr.strip()}")
else:
ok("Repository updated")
else:
info(f"Cloning into {PROJECT_ROOT} ...")
r = subprocess.run(
["git", "clone", REPO_URL, str(PROJECT_ROOT)], capture_output=True, text=True
)
if r.returncode != 0:
print(r.stderr)
fail("git clone failed")
ok("Repository cloned")
# ── 4. Virtual environment + install ──────────────────────────────────────────
header("4 / 5 Installing MetaOps + dependencies")
if not VENV_DIR.exists():
info("Creating venv ...")
subprocess.run([uv, "venv", str(VENV_DIR), "--python", sys.executable], check=True)
ok("Virtual environment created")
else:
ok("Venv exists")
info("Installing MetaOps ...")
print()
r = subprocess.run(
[uv, "pip", "install", "-e", ".", "--reinstall", "--python", str(VENV_PYTHON)],
cwd=str(PROJECT_ROOT),
)
print()
if r.returncode != 0:
fail("Installation failed")
ok("MetaOps installed")
# MCP servers — install locally so npx finds them without downloading each time
npm = shutil.which("npm")
if npm:
info("Installing MCP servers via npm ...")
r = subprocess.run(
[
npm,
"install",
"@modelcontextprotocol/server-filesystem",
"@modelcontextprotocol/server-memory",
"--save",
],
cwd=str(PROJECT_ROOT),
)
if r.returncode == 0:
ok("MCP servers installed locally")
else:
warn("npm install failed — npx fallback at runtime")
# Audit tools (optional)
for tool_bin, package in [("bandit", "bandit"), ("pip-audit", "pip-audit")]:
if not ((VENV_BIN / tool_bin).exists() or (VENV_BIN / f"{tool_bin}.exe").exists()):
info(f"Installing {package} (optional)...")
subprocess.run(
[uv, "pip", "install", package, "--python", str(VENV_PYTHON)],
capture_output=True,
)
# ── 5. First-run setup ────────────────────────────────────────────────────────
header("5 / 5 First-run setup")
DATA_DIR.mkdir(parents=True, exist_ok=True)
(DATA_DIR / "artifacts").mkdir(parents=True, exist_ok=True)
(DATA_DIR / "metaops_vector_db").mkdir(parents=True, exist_ok=True)
ok(f"{DATA_DIR} ready")
# Create unified SQLite database — must match database.py exactly
for db_name, schemas in [
(
"metaops.db",
[
# Skills (L1/L2/L3 structure)
"CREATE TABLE IF NOT EXISTS skills (id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT UNIQUE NOT NULL, description TEXT NOT NULL, instructions TEXT NOT NULL, status TEXT NOT NULL DEFAULT 'pending_review', version INTEGER NOT NULL DEFAULT 1, created_at DATETIME DEFAULT CURRENT_TIMESTAMP, updated_at DATETIME DEFAULT CURRENT_TIMESTAMP)",
"CREATE TABLE IF NOT EXISTS skill_resources (id INTEGER PRIMARY KEY AUTOINCREMENT, skill_name TEXT NOT NULL, path TEXT NOT NULL, content TEXT NOT NULL, FOREIGN KEY (skill_name) REFERENCES skills(name) ON DELETE CASCADE, UNIQUE(skill_name, path))",
# RAG sources
"CREATE TABLE IF NOT EXISTS rag_sources (file_path TEXT PRIMARY KEY, filename TEXT NOT NULL, description TEXT, global_context TEXT, file_size INTEGER NOT NULL, chunk_count INTEGER NOT NULL, timestamp DATETIME DEFAULT CURRENT_TIMESTAMP)",
# Observability logs
"CREATE TABLE IF NOT EXISTS llm_calls (id TEXT PRIMARY KEY, timestamp DATETIME DEFAULT CURRENT_TIMESTAMP, session_id TEXT, role TEXT, provider TEXT NOT NULL, model TEXT NOT NULL, prompt TEXT, completion TEXT, prompt_tokens INTEGER, completion_tokens INTEGER, total_tokens INTEGER, latency_ms INTEGER, status_code INTEGER, error_message TEXT)",
"CREATE TABLE IF NOT EXISTS subagent_logs (id TEXT PRIMARY KEY, timestamp DATETIME DEFAULT CURRENT_TIMESTAMP, session_id TEXT, parent_agent TEXT NOT NULL, subagent_name TEXT NOT NULL, query TEXT, response TEXT, prompt_tokens INTEGER, completion_tokens INTEGER, total_tokens INTEGER, latency_ms INTEGER, status TEXT)",
# Autonomous loop context (must match database.py exactly)
"CREATE TABLE IF NOT EXISTS loop_context (id TEXT PRIMARY KEY, timestamp DATETIME DEFAULT CURRENT_TIMESTAMP, session_id TEXT, loop_type TEXT NOT NULL, query TEXT NOT NULL, result TEXT, iterations INTEGER DEFAULT 0, approved INTEGER DEFAULT 0, prompt_tokens INTEGER DEFAULT 0, completion_tokens INTEGER DEFAULT 0, total_tokens INTEGER DEFAULT 0, latency_ms INTEGER DEFAULT 0, status TEXT DEFAULT 'running', app_name TEXT, user_id TEXT, plan_json TEXT, todo_list TEXT, todo_done INTEGER DEFAULT 0, todo_total INTEGER DEFAULT 0, started_at DATETIME, completed_at DATETIME, error_message TEXT, assigned_to TEXT)",
# Checkpoints (crash recovery)
"CREATE TABLE IF NOT EXISTS checkpoints (session_key TEXT PRIMARY KEY, data TEXT NOT NULL, updated_at DATETIME DEFAULT CURRENT_TIMESTAMP)",
# Free models catalog
"CREATE TABLE IF NOT EXISTS free_models (id TEXT NOT NULL, provider TEXT NOT NULL, source TEXT NOT NULL, description TEXT, context_window INTEGER, max_completion_tokens INTEGER, pricing_prompt REAL, pricing_completion REAL, last_seen DATETIME DEFAULT CURRENT_TIMESTAMP, PRIMARY KEY (id, provider, source))",
# Memory stores (persistent user preferences)
"CREATE TABLE IF NOT EXISTS memory_stores (id TEXT PRIMARY KEY, user_id TEXT NOT NULL, store_name TEXT NOT NULL, key TEXT NOT NULL, value TEXT NOT NULL, created_at DATETIME DEFAULT CURRENT_TIMESTAMP, updated_at DATETIME DEFAULT CURRENT_TIMESTAMP, UNIQUE(user_id, store_name, key))",
"CREATE INDEX IF NOT EXISTS idx_memory_stores_user ON memory_stores(user_id, store_name)",
"CREATE INDEX IF NOT EXISTS idx_free_models_provider ON free_models(provider)",
"CREATE INDEX IF NOT EXISTS idx_llm_calls_session_id ON llm_calls(session_id)",
"CREATE INDEX IF NOT EXISTS idx_llm_calls_role ON llm_calls(role)",
"CREATE INDEX IF NOT EXISTS idx_llm_calls_timestamp ON llm_calls(timestamp)",
"CREATE INDEX IF NOT EXISTS idx_subagent_logs_session_id ON subagent_logs(session_id)",
"CREATE INDEX IF NOT EXISTS idx_subagent_logs_subagent ON subagent_logs(subagent_name)",
"CREATE INDEX IF NOT EXISTS idx_subagent_logs_timestamp ON subagent_logs(timestamp)",
"CREATE INDEX IF NOT EXISTS idx_loop_context_loop_type ON loop_context(loop_type)",
"CREATE INDEX IF NOT EXISTS idx_loop_context_session_id ON loop_context(session_id)",
"CREATE INDEX IF NOT EXISTS idx_loop_context_timestamp ON loop_context(timestamp)",
],
),
]:
db_path = DATA_DIR / db_name
if not db_path.exists():
try:
conn = sqlite3.connect(str(db_path))
for stmt in schemas:
conn.execute(stmt)
conn.commit()
conn.close()
ok(f"{db_name} created")
except Exception as e:
warn(f"Failed to create {db_name}: {e}")
else:
ok(f"{db_name} exists")
# ChromaDB — create via venv Python (chromadb is installed there)
chroma_dir = DATA_DIR / "metaops_vector_db"
chroma_dir.mkdir(parents=True, exist_ok=True)
r = subprocess.run(
[
str(VENV_PYTHON),
"-c",
f"""
import chromadb
from pathlib import Path
db = Path('{chroma_dir}')
db.mkdir(parents=True, exist_ok=True)
c = chromadb.PersistentClient(path=str(db))
for n in ["episodic_memory", "semantic_memory", "procedural_memory"]:
c.get_or_create_collection(n)
print("OK")
""",
],
capture_output=True,
text=True,
)
if r.returncode == 0 and "OK" in r.stdout:
ok("ChromaDB vector collections created")
else:
warn(f"ChromaDB init: {r.stderr.strip()[:200]}")
# .env
if ENV_FILE.exists():
ok(".env exists")
elif ENV_EXAMPLE.exists():
shutil.copy(ENV_EXAMPLE, ENV_FILE)
warn(".env created from .env.example — add your API keys")
# Shell
shell = shutil.which("bash") or shutil.which("powershell") or shutil.which("cmd")
ok(f"Shell: {shell}") if shell else warn("No shell found")
# Global launcher (Linux/macOS)
launcher_installed = False
if sys.platform != "win32":
venv_metaops = VENV_BIN / "metaops"
if venv_metaops.exists():
for launcher_dir in [Path.home() / ".local" / "bin", Path("/usr/local/bin")]:
try:
launcher_dir.mkdir(parents=True, exist_ok=True)
launcher = launcher_dir / "metaops"
launcher.write_text(f'#!/bin/sh\nexec {venv_metaops.resolve()} "$@"\n')
launcher.chmod(0o755)
ok(f"Global launcher: {launcher}")
launcher_installed = True
break
except PermissionError:
continue
# Smoke test
info("Verifying install...")
r = subprocess.run(
[
str(VENV_PYTHON),
"-c",
"from metaops.config import MetaOpsConfig; MetaOpsConfig()",
],
capture_output=True,
text=True,
)
if r.returncode != 0:
warn(f"Import check failed:\n{r.stderr.strip()}")
else:
ok("MetaOps imports correctly")
metaops_cmd = "metaops" if launcher_installed else str(VENV_BIN / "metaops")
print(f"""
{BOLD}━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
MetaOps is ready.
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━{RESET}
Location : {PROJECT_ROOT}
Venv : {VENV_DIR}
Next steps:
1. cd {PROJECT_ROOT}
2. Edit .env — add your API keys
3. {metaops_cmd}
""")