-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathmain.py
More file actions
1317 lines (1115 loc) · 59.9 KB
/
main.py
File metadata and controls
1317 lines (1115 loc) · 59.9 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
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""main.py — FastAPI server for the Antigravity Advanced Kit.
Provides HTTP endpoints for agent interaction. Zero business logic here —
everything routes to the memory API and onboarding assembler.
Endpoints:
GET /health — System status + daemon connectivity
GET /context — Onboarding spawn context (what the agent sees at T=0)
GET /events — Recent event ledger entries
POST /invoke — Send a directive to the agent engine
POST /lesson — Record a lesson
POST /event — Record an event to the ledger
"""
from __future__ import annotations
import os
import sys
import time
import json
import hashlib
import urllib.request
import urllib.parse
import platform
import datetime
from pathlib import Path
import uvicorn
from dotenv import load_dotenv
from fastapi import FastAPI, HTTPException
from fastapi.responses import FileResponse
from pydantic import BaseModel, Field
# Ensure project root is on path
sys.path.insert(0, str(Path(__file__).parent))
from config import ensure_dirs
from memory_api import MemoryAPI
from onboarding import build_spawn_context
from event_ledger import count_lines
from autonomic_core.organs.cortex_callosum import CortexCallosum
from autonomic_core.organs.semantic_chunker import SemanticChunker, handle_index, handle_read_chunk
from autonomic_core.organs.command_sanitizer import CommandSanitizer
from autonomic_core.organs.coherence_monitor import CoherenceMonitor, handle_coherence_check
from autonomic_core.organs.hebbian_reflection import HebbianReflection, handle_reflection_block
# Load .env variables
load_dotenv()
# Global Chunking Organism
semantic_chunker = SemanticChunker()
command_sanitizer = CommandSanitizer()
coherence_monitor = CoherenceMonitor()
# Load Static Ground Truth File (Option C)
static_gt_path = os.path.join(str(Path(__file__).parent), "baselines", "sovereign_ground_truth.md")
if os.path.exists(static_gt_path):
with open(static_gt_path, "r", encoding="utf-8") as f:
coherence_monitor.inject_ground_truth(f.read())
# Ensure directories exist
ensure_dirs()
app = FastAPI(
title="Antigravity Advanced Kit",
description="A production-grade organism for autonomous agents.",
version="2.0.0",
)
api = MemoryAPI()
hebbian_reflection = HebbianReflection(
on_success_callback=lambda msg: api.emit_event("architecture", msg, project="Sovereign Engine Core"),
on_failure_callback=lambda msg: api.emit_event("lesson", msg, project="Sovereign Engine Core")
)
# Load CortexDB Active Memory (Option A)
try:
# Attempt to pull recently verified lessons/memories directly from the API endpoint
# memory_api get_hot() usually returns a list or raw string depending on version
hot = api.get_hot()
if isinstance(hot, list):
# Taking last 5 validated ledger blocks
lessons_text = "\\n".join([str(item.get("content", item)) if isinstance(item, dict) else str(item) for item in hot[-5:]])
if lessons_text.strip():
coherence_monitor.inject_ground_truth(lessons_text)
elif isinstance(hot, str) and hot.strip():
coherence_monitor.inject_ground_truth(hot.strip()[-1000:])
except Exception as e:
print(f"[Coherence Monitor] Warning: CortexDB Bootstrap Failed - {e}. Relying solely on static baseline.")
# ── Inference Engine ──────────────────────────────────────
_CODE_SIGNALS = {
'code', 'function', 'script', 'debug', 'implement', 'refactor', 'class',
'error', 'bug', 'fix', 'write a', 'python', 'javascript', 'typescript',
'sql', 'api', 'test', 'compile', 'syntax', 'bash', 'shell', 'dockerfile',
'regex', 'algorithm', 'parse', 'import', 'export', 'build',
}
_HEAVY_SIGNALS = {
'analyze', 'analyse', 'explain in detail', 'comprehensive', 'essay',
'research', 'compare', 'architecture', 'design', 'strategy', 'review',
'audit', 'plan', 'reason', 'summarize entire', 'deep dive', 'thesis',
'evaluate', 'assessment', 'critique', 'in depth',
}
# EVOLVE-BLOCK-ROUTING-START
def _classify_task(prompt: str) -> str:
"""Returns 'simple' | 'code' | 'heavy' based on prompt signals."""
p = prompt.lower()
if any(s in p for s in _CODE_SIGNALS):
return 'code'
if len(prompt.split()) > 60 or any(s in p for s in _HEAVY_SIGNALS):
return 'heavy'
return 'simple'
def _probe_ollama(host: str) -> list[str]:
"""Return list of installed Ollama model names, empty if unreachable."""
try:
req = urllib.request.Request(f"{host.rstrip('/')}/api/tags", headers={"User-Agent": "sov/1.0"})
with urllib.request.urlopen(req, timeout=3) as resp:
return [m["name"] for m in json.loads(resp.read()).get("models", [])]
except Exception:
return []
def _pick_model_auto(prompt: str) -> str | None:
"""Route to best available model based on task complexity."""
task = _classify_task(prompt)
def _key(env_var, placeholder=''):
v = os.getenv(env_var, '').strip().strip('"').strip("'")
return v if v and v != placeholder else ''
gemini_key = _key('GEMINI_API_KEY', 'AIzaSy...')
openai_key = _key('OPENAI_API_KEY', 'sk-...')
anthropic_key= _key('ANTHROPIC_API_KEY', 'sk-ant-...')
ollama_host = os.getenv('OLLAMA_HOST', 'http://127.0.0.1:11434').strip().rstrip('/')
ollama_mods = _probe_ollama(ollama_host)
def _first_ollama(*keywords):
"""Return first Ollama model matching any keyword, or None."""
for kw in keywords:
for m in ollama_mods:
if kw in m.lower():
return m
return None
if task == 'simple':
# Cloud first for reliability — local 8B models confabulate on the tool system prompt.
# Ollama is the offline-only fallback for users with no cloud keys.
if gemini_key: return 'gemini-2.0-flash'
if openai_key: return 'gpt-4o-mini'
if anthropic_key: return 'claude-3-5-haiku-20241022'
# Offline fallback: prefer 8B+ capable models
local = _first_ollama('llama3.1', 'mistral-nemo', 'qwen3:8b', 'gemma3:12b', 'nemotron')
if local: return local
if ollama_mods: return ollama_mods[0]
elif task == 'code':
# Best code model wins; prefer deepseek locally, then GPT-4o / Gemini Pro
code_local = _first_ollama('deepseek-coder', 'codellama', 'qwen', 'starcoder')
if code_local: return code_local
if openai_key: return 'gpt-4o'
if gemini_key: return 'gemini-2.5-pro-preview-03-25'
if anthropic_key: return 'claude-sonnet-4-5'
if ollama_mods: return ollama_mods[0]
else: # heavy
# Best reasoning: Gemini Pro > GPT-4o > Claude Opus > large local
if gemini_key: return 'gemini-2.5-pro-preview-03-25'
if openai_key: return 'gpt-4o'
if anthropic_key: return 'claude-opus-4-5'
large = _first_ollama('llama3.1', 'mistral-nemo', 'qwen3:8b', 'gemma3:12b', 'deepseek')
if large: return large
if ollama_mods: return ollama_mods[0]
return None # nothing available
# EVOLVE-BLOCK-ROUTING-END
def llm_inference(prompt: str, context: str, model_override: str | None = None) -> str:
"""Dependency-free HTTP caller for the active LLM."""
active_model = (model_override or "").strip() or os.getenv("ACTIVE_MODEL", "").strip().strip('"').strip("'")
# 'auto' or empty → smart task-based routing
if not active_model or active_model == 'auto':
active_model = _pick_model_auto(prompt)
if not active_model:
return "No LLM configured. Open Settings → add an API key, or ensure Ollama is running locally."
sys_temp = float(os.getenv("AGENT_TEMPERATURE", "0.7"))
route = ('openai' if active_model.startswith(('gpt', 'o1', 'o3'))
else 'anthropic' if active_model.startswith('claude')
else 'gemini' if active_model.startswith('gemini')
else 'nim' if active_model.startswith(('meta/', 'deepseek-ai/', 'nvidia/'))
else 'ollama')
print(f"[LLM] task={_classify_task(prompt)} model='{active_model}' route={route} temp={sys_temp}")
try:
if route == 'openai':
api_key = os.getenv("OPENAI_API_KEY", "").strip().strip('"').strip("'")
if not api_key: return "ERROR: OPENAI_API_KEY is not configured."
url = "https://api.openai.com/v1/chat/completions"
headers = {"Content-Type": "application/json", "Authorization": f"Bearer {api_key}"}
data = {
"model": active_model, "temperature": sys_temp,
"messages": [{"role": "system", "content": context}, {"role": "user", "content": prompt}]
}
req = urllib.request.Request(url, data=json.dumps(data).encode("utf-8"), headers=headers, method="POST")
with urllib.request.urlopen(req, timeout=30) as response:
return json.loads(response.read())["choices"][0]["message"]["content"]
elif route == 'anthropic':
api_key = os.getenv("ANTHROPIC_API_KEY", "").strip().strip('"').strip("'")
if not api_key: return "ERROR: ANTHROPIC_API_KEY is not configured."
url = "https://api.anthropic.com/v1/messages"
headers = {"Content-Type": "application/json", "x-api-key": api_key, "anthropic-version": "2023-06-01"}
data = {
"model": active_model, "system": context, "temperature": sys_temp,
"messages": [{"role": "user", "content": prompt}], "max_tokens": 4096
}
req = urllib.request.Request(url, data=json.dumps(data).encode("utf-8"), headers=headers, method="POST")
with urllib.request.urlopen(req, timeout=60) as response:
return json.loads(response.read())["content"][0]["text"]
elif route == 'gemini':
api_key = os.getenv("GEMINI_API_KEY", "").strip().strip('"').strip("'")
if not api_key or api_key.startswith("AIzaSy"): return "ERROR: GEMINI_API_KEY is not configured or uses a placeholder."
url = f"https://generativelanguage.googleapis.com/v1beta/models/{active_model}:generateContent?key={api_key}"
data = {
"system_instruction": {"parts": [{"text": context}]},
"contents": [{"parts": [{"text": prompt}]}],
"generationConfig": {"temperature": sys_temp}
}
req = urllib.request.Request(url, data=json.dumps(data).encode("utf-8"),
headers={"Content-Type": "application/json"}, method="POST")
with urllib.request.urlopen(req, timeout=60) as response:
return json.loads(response.read())["candidates"][0]["content"]["parts"][0]["text"]
elif route == 'nim':
keys_str = os.getenv("SOVEREIGN_NIM_API_KEYS", "")
keypool = [k.strip() for k in keys_str.split(",") if k.strip()]
if not keypool: return "ERROR: SOVEREIGN_NIM_API_KEYS is not configured."
import random
api_key = random.choice(keypool)
url = "https://integrate.api.nvidia.com/v1/chat/completions"
headers = {"Content-Type": "application/json", "Authorization": f"Bearer {api_key}"}
data = {
"model": active_model, "temperature": sys_temp,
"messages": [{"role": "system", "content": context}, {"role": "user", "content": prompt}]
}
req = urllib.request.Request(url, data=json.dumps(data).encode("utf-8"), headers=headers, method="POST")
with urllib.request.urlopen(req, timeout=120) as response:
return json.loads(response.read())["choices"][0]["message"]["content"]
else: # ollama
host = os.getenv("OLLAMA_HOST", "http://127.0.0.1:11434").strip().rstrip("/")
url = f"{host}/api/chat"
data = {
"model": active_model, "stream": False, "options": {"temperature": sys_temp},
"messages": [{"role": "system", "content": context}, {"role": "user", "content": prompt}]
}
req = urllib.request.Request(url, data=json.dumps(data).encode("utf-8"),
headers={"Content-Type": "application/json"}, method="POST")
with urllib.request.urlopen(req, timeout=120) as response:
return json.loads(response.read())["message"]["content"]
except Exception as e:
return f"ERROR: LLM Engine Failed - {str(e)}"
# ── Models ────────────────────────────────────────────────
class InvokeRequest(BaseModel):
prompt: str
context_override: dict | None = None
model_override: str | None = None
history: list[dict] = []
class PendingApproval(BaseModel):
tool: str
payload: str
fpath: str | None = None
class InvokeResponse(BaseModel):
text: str
pending_approval: PendingApproval | None = None
model: str = ""
traces_emitted: int
execution_time_ms: float
class ExecuteRawRequest(BaseModel):
tool: str
payload: str
fpath: str | None = None
class FileWriteRequest(BaseModel):
path: str
content: str
class WorkspaceJailRequest(BaseModel):
location: str
name: str | None = None
class LessonRequest(BaseModel):
text: str
class EventRequest(BaseModel):
event_type: str
content: str
project: str = ""
class SettingsPayload(BaseModel):
"""Visual UI settings dynamically synced to the .env file."""
AGENT_NAME: str | None = None
ENVIRONMENT: str | None = None
ACTIVE_MODEL: str | None = None
OPENAI_API_KEY: str | None = None
ANTHROPIC_API_KEY: str | None = None
GEMINI_API_KEY: str | None = None
OLLAMA_HOST: str | None = None
POSTGRES_DSN: str | None = None
WORKSPACE_JAIL: str | None = None
MAX_AGENT_CYCLES: str | None = None
STRICT_QUARANTINE: str | None = None
CONTEXT_MEMORY_LIMIT: str | None = None
AGENT_TEMPERATURE: str | None = None
LOG_LEVEL: str | None = None
STREAM_RESPONSES: str | None = None
UI_THEME: str | None = None
# ── Endpoints ─────────────────────────────────────────────
@app.on_event("startup")
async def wake_up():
agent_name = os.getenv("AGENT_NAME", "Agent")
print(f"[*] Booting {agent_name}...")
print("[*] Enforcing Protocol: Logic → Proof → Harden → Ship")
daemon_ok = api.ping()
if daemon_ok:
print("[*] Daemon system: ONLINE")
else:
print("[!] Daemon system: OFFLINE (falling back to disk reads)")
print("[!] Start daemon with: python daemon.py")
@app.on_event("shutdown")
async def sleep_now():
"""Cleanup processes on shutdown."""
print("[*] Gracefully shutting down Antigravity APIs...")
try:
from store import close_pool
close_pool()
print("[*] Postgres connection pool closed.")
except Exception as e:
print(f"[!] Warning on shutdown: {e}")
@app.get("/")
def serve_ui():
"""Auto-route the root specifically to the Sovereign Desktop UI."""
return FileResponse(os.path.join(os.path.dirname(__file__), "ui", "index.html"))
@app.get("/api/models")
def list_models():
"""Return all available models grouped by provider, including live Ollama models."""
import urllib.request, json as _json
static = {
"gemini": [
"gemini-2.5-pro-preview-03-25",
"gemini-2.0-flash",
"gemini-2.0-flash-lite",
"gemini-1.5-pro",
"gemini-1.5-flash",
],
"openai": [
"gpt-4o",
"gpt-4o-mini",
"gpt-4-turbo",
"o1",
"o1-mini",
"o3-mini",
],
"anthropic": [
"claude-opus-4-5",
"claude-sonnet-4-5",
"claude-3-5-haiku-20241022",
"claude-3-opus-20240229",
],
}
ollama_models = []
ollama_host = os.getenv("OLLAMA_HOST", "http://localhost:11434").rstrip("/")
try:
req = urllib.request.Request(f"{ollama_host}/api/tags", headers={"User-Agent": "sovereign/1.0"})
with urllib.request.urlopen(req, timeout=3) as resp:
data = _json.loads(resp.read())
ollama_models = [m["name"] for m in data.get("models", [])]
except Exception:
pass # Ollama offline — return empty list, not an error
return {**static, "ollama": ollama_models}
@app.get("/api/settings")
def get_settings():
env_path = os.path.join(os.path.dirname(__file__), ".env")
settings = {}
if os.path.exists(env_path):
with open(env_path, "r") as f:
for line in f:
if "=" in line and not line.strip().startswith("#"):
k, v = line.split("=", 1)
settings[k.strip()] = v.strip().strip('"').strip("'")
return settings
@app.post("/api/settings")
def update_settings(payload: SettingsPayload):
updates = {k: v for k, v in payload.dict(exclude_none=True).items()}
if not updates:
return {"status": "no_changes"}
env_path = os.path.join(os.path.dirname(__file__), ".env")
if not os.path.exists(env_path):
open(env_path, "w").close()
with open(env_path, "r") as f:
lines = f.readlines()
out_lines = []
updated_keys = list(updates.keys())
for line in lines:
if "=" in line and not line.strip().startswith("#"):
k = line.split("=", 1)[0].strip()
if k in updates:
out_lines.append(f'{k}="{updates[k]}"\n')
os.environ[k] = updates[k]
del updates[k]
continue
out_lines.append(line)
for k, v in updates.items():
out_lines.append(f'{k}="{v}"\n')
os.environ[k] = v
with open(env_path, "w") as f:
f.writelines(out_lines)
load_dotenv(override=True)
return {"status": "success", "updated_keys": updated_keys}
@app.get("/api/health")
def health_check():
"""System health including daemon connectivity."""
daemon_online = api.ping()
# Count warm project files (pathways)
projects_dir = Path(__file__).parent / "memory" / "projects"
pathway_count = len(list(projects_dir.glob("*.md"))) if projects_dir.exists() else 0
# Active model
active_model = os.getenv("ACTIVE_MODEL", "auto").strip().strip('"').strip("'")
return {
"status": "alive",
"daemon_online": daemon_online,
"memories": count_lines(),
"pathways": pathway_count,
"active_model": active_model,
"events_in_ledger": count_lines(),
"agent_name": os.getenv("AGENT_NAME", "Agent"),
}
@app.get("/api/context")
def get_context():
"""Return the full spawn context (what the agent sees at T=0)."""
try:
context = build_spawn_context()
return {"ok": True, "context": context, "lines": context.count("\n") + 1}
except Exception as e:
raise HTTPException(status_code=500, detail=str(e))
@app.get("/api/events")
def get_events(limit: int = 20):
"""Return recent event ledger entries."""
events = api.get_ledger_events(limit=limit)
return {"ok": True, "events": events, "count": len(events)}
@app.get("/api/workspace")
def get_workspace():
"""Stream a bounded structural file tree resolving the active Workspace Jail cleanly."""
jail = os.getenv("WORKSPACE_JAIL", "").strip()
if not jail or not os.path.exists(jail):
return {"ok": False, "error": "WORKSPACE_JAIL not mounted"}
base = Path(jail)
try:
def walk_dir(current_dir, depth=0):
if depth > 5: return []
nodes = []
for item in sorted(current_dir.iterdir(), key=lambda x: (not x.is_dir(), x.name.lower())):
if item.name.startswith(".") and item.name not in [".github", ".vscode"]: continue
if item.name in ["node_modules", "__pycache__", "venv", ".venv", "dist", "build"]: continue
node = {
"name": item.name,
"path": str(item.relative_to(base)),
"is_dir": item.is_dir(),
"children": []
}
if item.is_dir():
node["children"] = walk_dir(item, depth + 1)
nodes.append(node)
return nodes
tree = walk_dir(base)
except Exception as e:
return {"ok": False, "error": str(e)}
return {"ok": True, "files": tree}
@app.post("/api/workspace/jail")
def set_workspace_jail(req: WorkspaceJailRequest):
"""Dynamically swap tracking boundaries parsing raw MarkDown payload locations into active Jails natively."""
import re
# Extract inside backticks if present: "`sovereign/` (root)" -> "sovereign/"
match = re.search(r'`([^`]+)`', req.location)
loc = match.group(1).strip() if match else req.location.strip()
if loc == "" and (req.name is None or req.name.strip() == ""):
os.environ["WORKSPACE_JAIL"] = ""
return {"ok": True, "message": "Workspace Jail cleared cleanly.", "path": ""}
if loc.startswith("~/"):
loc = os.path.expanduser(loc)
target = Path(loc)
found = None
if target.is_absolute() and target.is_dir():
found = target
else:
# Fuzzy scan roots:
r1 = (Path("/home/frost/Desktop/Agent_System") / loc).resolve()
r2 = (Path("/home/frost/Desktop") / loc).resolve()
r3 = (Path.cwd() / loc).resolve()
if r1.is_dir(): found = r1
elif r2.is_dir(): found = r2
elif r3.is_dir(): found = r3
elif req.name:
# Fallback to absolute strict literal Name extraction mapping
r_name = req.name.strip().replace(" ", "_")
r4 = (Path.cwd() / r_name).resolve()
r5 = (Path("/home/frost/Desktop/Agent_System") / r_name).resolve()
if r4.is_dir(): found = r4
elif r5.is_dir(): found = r5
if not found or not found.is_dir():
return {"ok": False, "error": f"Resolution failed. Physical directory '{loc}' (or name '{req.name}') not found."}
os.environ["WORKSPACE_JAIL"] = str(found)
return {"ok": True, "message": f"Workspace Jail shifted to: {found}", "path": str(found)}
@app.get("/api/file")
def read_file(path: str):
"""Retrieve arbitrary safe text blobs cleanly bound structurally to the Workspace Jail."""
jail = os.getenv("WORKSPACE_JAIL", "").strip()
if not jail or not os.path.exists(jail):
return {"ok": False, "error": "WORKSPACE_JAIL not mounted"}
try:
target = (Path(jail) / path).resolve(strict=True)
except Exception:
target = (Path(jail) / path).resolve()
if not str(target).startswith(str(Path(jail).resolve())):
return {"ok": False, "error": "[SECURITY] Traversal attempt blocked natively."}
if not target.exists() or target.is_dir():
return {"ok": False, "error": "File not found or is a directory."}
try:
content = target.read_text(encoding="utf-8")
return {"ok": True, "content": content}
except Exception as e:
return {"ok": False, "error": str(e)}
@app.post("/api/file")
def write_file(req: FileWriteRequest):
"""Physically write IDE Editor payload strings securely mapping back into the Sandbox."""
jail = os.getenv("WORKSPACE_JAIL", "").strip()
if not jail or not os.path.exists(jail):
return {"ok": False, "error": "WORKSPACE_JAIL not mounted"}
try:
target = (Path(jail) / req.path).resolve(strict=False)
except Exception:
target = (Path(jail) / req.path).resolve()
if not str(target).startswith(str(Path(jail).resolve())):
return {"ok": False, "error": "[SECURITY] Traversal attempt blocked natively."}
try:
target.parent.mkdir(parents=True, exist_ok=True)
target.write_text(req.content, encoding="utf-8")
return {"ok": True, "message": "File written safely to disk."}
except Exception as e:
return {"ok": False, "error": str(e)}
@app.get("/api/projects")
def get_projects():
"""Extract and array-map the hot.md Active Projects table."""
hot_text = api.get_hot()
rows = []
in_table = False
for line in hot_text.splitlines():
if "| Project" in line:
in_table = True
continue
if in_table and line.strip().startswith("|---"):
continue
if in_table and line.strip().startswith("|"):
cols = [c.strip() for c in line.split("|") if c.strip()]
if len(cols) >= 3:
name_clean = cols[0].replace("**", "")
if name_clean == "Project" or "----" in name_clean:
continue
rows.append({"name": name_clean, "location": cols[1], "status": cols[2]})
elif in_table and not line.strip().startswith("|"):
break
return {"ok": True, "projects": rows}
class ActiveProjectRequest(BaseModel):
project_name: str
@app.post("/api/projects/active")
def set_active_project(req: ActiveProjectRequest):
"""Force context overwrite via the session.md marker."""
ok = api.update_session(current_work=f"Working on {req.project_name}")
if not ok:
from config import SESSION_MD
try:
content = SESSION_MD.read_text(encoding="utf-8")
lines = content.splitlines()
out = []
capturing = False
for line in lines:
if line.startswith("## Current Work"):
out.append(line)
out.append(f"Working on {req.project_name}")
capturing = True
elif capturing and line.startswith("## "):
capturing = False
out.append("")
out.append(line)
elif not capturing:
out.append(line)
SESSION_MD.write_text("\n".join(out), encoding="utf-8")
ok = True
except Exception:
pass
# We explicitly emit an event so it shows up in history/telemetry
api.emit_event("decision", f"Operator manually locked context to project: {req.project_name}", project=req.project_name)
return {"ok": ok, "project": req.project_name}
import re
import shlex
import subprocess
TOOL_PROMPT = """
You are currently connected to a live machine execution loop. You are NO LONGER A CHATBOT.
You have the physical ability to execute bash commands, read files, and write files on the host system.
When you want to execute an action, output an XML block. The orchestration loop will run it, stall, and append the output below your response so you can read what happened.
DO NOT roleplay execution. ACTUALLY output the XML tags to physically run the tool.
TOOLS:
1. To run a command (no interactive loops, no blocking commands):
<execute>
ls -la /
</execute>
CRITICAL EXECUTION RESTRICTION: You are executing commands via `subprocess` with `shell=False` for security. You MAY NOT use shell pipes (`|`), stdout/stderr redirects (`> /dev/null`, `2>&1`), aliases, or command chaining (`&&`, `;`). If you need to filter, use separate commands or write a python script to disk and execute that instead. Every argument you pass is passed literally.
2. To index a large file structurally (Returns a semantic map of functions/headings, bypassing context limits):
<index path="/absolute/path.py"></index>
3. To read a specific semantic chunk (obtained from the index map):
<read_chunk path="/absolute/path.py" chunk="function_name"></read_chunk>
3. To write to a file:
<write path="/path/to/output.txt">
content here
</write>
4. To search the web (DuckDuckGo):
<search>
your query here
</search>
5. To read a webpage (strips HTML/JS to clean text):
<fetch>
https://example.com/docs
</fetch>
6. To list directory contents natively:
<list_dir>
/path/to/folder
</list_dir>
7. To search for files by pattern natively:
<search_dir path="/path/to/folder" pattern="*.py" />
8. To grep/scan inside text files natively:
<grep path="/path/to/folder" query="def main" />
9. To view local system datetime and OS telemetry:
<system></system>
10. To organically mutate your own source code (EVOLVE-BLOCK permitted files ONLY):
<mutate target="/absolute/path/to/file.py" block="BLOCK_NAME">
new python code replacing the block
</mutate>
11. To read explicitly marked physical EVOLVE-BLOCK zones surgically from a file (Bypasses 4kb limits):
<read_block target="/absolute/path/to/file.py" block="BLOCK_NAME">
</read_block>
12. To burn a memory of a mutation outcome into your active memory (Hebbian reinforcement):
<reflect block="BLOCK_NAME" outcome="IMPROVED|DEGRADED">
One concise sentence explaining exactly WHY the mutation succeeded or why it degraded performance.
</reflect>
CRITICAL BEHAVIORAL RULE: If a script or command throws an error or Python traceback, DO NOT just explain the bug to the operator and wait. You MUST autonomously rewrite the code, fix the logic, `<write>` the patched script to disk, and `<execute>` it again immediately. Iterate autonomously until the task succeeds.
CRITICAL ANTI-HALLUCINATION RULE: If the operator provides casual greetings like "hey" or "hello", reply normally (in plain text, NO XML tags) and ask how you can assist. However, if the operator provides literal garbage text, random strings, or nonsensical directives (e.g., "asdf", "jkofjm 9w0920btjww", "yoyo"), DO NOT attempt to execute tools to find meaning. Immediately reject the input and ask for clarification WITHOUT emitting any XML.
CRITICAL: If you receive tool output, analyze it and provide your final response to the user. Stop emitting XML when the task is cleanly complete.
CRITICAL PERSONALITY RULE: You are highly accurate, but you are also dryly sarcastic and unapologetically blunt. Treat the operator like a colleague who constantly tests your patience. Keep answers concise, drop the sycophancy completely, and feel free to end your responses with a sharp, sarcastic observation.
"""
@app.post("/api/execute_raw", response_model=InvokeResponse)
def execute_raw(req: ExecuteRawRequest):
"""Bypass execute sequence used explicitly after UI Quarantine Gate approval."""
out = ""
try:
if req.tool == "execute":
# ==== COMMAND SANITIZER INTERCEPT ====
san_res = command_sanitizer.check(req.payload)
if not san_res.safe:
api.emit_event("lesson", san_res.cortexdb_lesson(), project="Sovereign Engine Core")
return InvokeResponse(text=san_res.rejection_message(), traces_emitted=0, execution_time_ms=0.0)
# =====================================
res = subprocess.run(shlex.split(req.payload), capture_output=True, text=True, timeout=15)
stdout = res.stdout[-4000:]
stderr = res.stderr[-4000:]
out_str = stdout if stdout else (stderr if stderr else "Command completed with no output.")
out = f"[EXECUTE: {req.payload}]\n{out_str}"
elif req.tool == "write":
Path(req.fpath).parent.mkdir(parents=True, exist_ok=True)
Path(req.fpath).write_text(req.payload, encoding="utf-8")
out = f"[WRITE SUCCESS: {req.fpath}]\nFile saved securely."
except Exception as e:
out = f"[{req.tool.upper()} ERROR]: {str(e)}"
return InvokeResponse(text=out, traces_emitted=0, execution_time_ms=0.0)
@app.post("/api/invoke", response_model=InvokeResponse)
def invoke_agent(req: InvokeRequest):
"""Send a directive to the agent engine with autonomous ReAct looping."""
start = time.perf_counter()
api.emit_event("context", f"Directive received: {req.prompt[:100]}")
try:
system_context = req.context_override if req.context_override else build_spawn_context()
except Exception:
system_context = "You are the Antigravity Intelligence Node."
system_context += "\n\n" + TOOL_PROMPT
# EVOLVE-BLOCK-MEMORY_HEURISTICS-START
if req.history:
mem_limit = int(os.getenv("CONTEXT_MEMORY_LIMIT", "6"))
recent = req.history[-mem_limit:] if mem_limit > 0 else req.history
history_str = "\n".join([f"[{msg['role'].upper()}] {msg['content']}" for msg in recent])
system_context += f"\n\n--- RECENT CONVERSATION (Your immediate memory) ---\n{history_str}\n-----------------------------------------------------\n"
# EVOLVE-BLOCK-MEMORY_HEURISTICS-END
current_prompt = req.prompt
# ==== CORTEX CALLOSUM INTERCEPT ====
# Inject Command Sanitizer into Cortex Callosum instance
callosum = CortexCallosum(llm_inference, command_sanitizer)
routing_tier = callosum.classify_complexity(current_prompt, req.history)
print(f"[CORTEX CALLOSUM] Task complexity evaluated as: {routing_tier}")
if routing_tier == "ANCHORED":
print("[IMMUNE SYSTEM] Override trigger detected. Routing exclusively to PyTorch local anchor.")
import autonomic_core.inference.anchored_inference as a_inf
py_script = a_inf.__file__
cmd = [sys.executable, py_script, "--prompt", current_prompt, "--system", system_context]
res = subprocess.run(cmd, capture_output=True, text=True)
immune_output = res.stdout.strip() if res.stdout.strip() else f"Process failed: {res.stderr.strip()}"
# Completely bypass the ReAct loop and return the neutralized output instantly.
return InvokeResponse(text=f"[IMMUNITY ENFORCED]\n{immune_output}", traces_emitted=0, execution_time_ms=0.0)
elif routing_tier == "FRONTIER":
req.model_override = "deepseek-ai/deepseek-v3.1"
print("[CORTEX CALLOSUM] Routing to remote FRONTIER model...")
elif routing_tier == "HYBRID":
shards = callosum.decompose(current_prompt)
if shards:
synthesized_intel = callosum.synthesize(shards)
system_context += f"\n\n{synthesized_intel}"
print("[CORTEX CALLOSUM] HYBRID Synthesis injected into Agent Context.")
# ===================================
loops = 0
final_output = ""
used_model = (req.model_override or "").strip() or os.getenv("ACTIVE_MODEL", "auto").strip().strip('"').strip("'")
sys_temp = float(os.getenv("AGENT_TEMPERATURE", "0.7"))
max_loops = int(os.getenv("MAX_AGENT_CYCLES", "15"))
while loops < max_loops:
llm_output = llm_inference(current_prompt, system_context, model_override=used_model)
final_output += llm_output + "\n"
# Check for executable XML tools
action_found = False
tool_outputs = []
DANGEROUS_BINARIES = ["rm", "mv", "cp", "wget", "curl", "chmod", "chown", "sudo", "apt", "npm", "pip", "kill", "mkfs", "dd", "mkdir", "rmdir", "touch", "zip", "unzip", "tar"]
WORKSPACE_JAIL = os.getenv("WORKSPACE_JAIL", "").strip()
def is_in_jail(target_path: str) -> bool:
if not WORKSPACE_JAIL:
return False
try:
abs_p = os.path.abspath(os.path.expanduser(target_path))
return abs_p.startswith(os.path.abspath(WORKSPACE_JAIL)) and ".." not in target_path
except:
return False
# 1. Execute Block
exec_matches = re.finditer(r'<execute>\s*((?:(?!</?execute>).)*?)\s*</execute>', llm_output, re.DOTALL)
for match in exec_matches:
action_found = True
cmd = match.group(1).strip()
base_cmd = cmd.split()[0] if cmd else ""
if base_cmd in DANGEROUS_BINARIES:
requires_approval = True
strict_quarantine = os.getenv("STRICT_QUARANTINE", "false").lower() == "true"
if not strict_quarantine and WORKSPACE_JAIL and base_cmd in ["rm", "mv", "cp", "chmod", "chown", "mkdir", "rmdir", "touch", "zip", "unzip", "tar"]:
try:
args = shlex.split(cmd)
all_safe = True
has_path_args = False
for arg in args[1:]:
if arg.startswith("-"): continue
has_path_args = True
if not is_in_jail(arg):
all_safe = False
break
if has_path_args and all_safe:
requires_approval = False
except:
pass
if requires_approval:
final_output += f"\n[SYSTEM INTERCEPT]: Command `{base_cmd}` targets outside Workspace Jail. Operator approval required.\n"
return InvokeResponse(text=final_output, pending_approval=PendingApproval(tool="execute", payload=cmd), traces_emitted=0, execution_time_ms=0.0)
# ==== COMMAND SANITIZER INTERCEPT ====
san_res = command_sanitizer.check(cmd)
if not san_res.safe:
final_output += f"\n[BLOCKED]: `{cmd}` (Hanging command detected)\n"
api.emit_event("lesson", san_res.cortexdb_lesson(), project="Sovereign Engine Core")
tool_outputs.append(san_res.rejection_message())
continue
# =====================================
final_output += f"\n[EXECUTING]: `{cmd}`\n"
try:
# Security Constraint: Never use shell=True
res = subprocess.run(shlex.split(cmd), capture_output=True, text=True, timeout=15)
stdout = res.stdout[-4000:]
stderr = res.stderr[-4000:]
out = stdout if stdout else (stderr if stderr else "Command completed with no output.")
tool_outputs.append(f"[EXECUTE: {cmd}]\n{out}")
except Exception as e:
tool_outputs.append(f"[EXECUTE ERROR: {cmd}]\n{str(e)}")
# 2a. Index Map via Semantic AST Chunking
index_matches = re.finditer(r'<index\s+path="([^"]+)"></index>', llm_output)
for match in index_matches:
action_found = True
fpath = match.group(1).strip()
final_output += f"\n[INDEXING]: `{fpath}`\n"
if not (WORKSPACE_JAIL and is_in_jail(fpath)):
final_output += f"\n[SYSTEM INTERCEPT]: File indexing to `{fpath}` requires operator approval.\n"
return InvokeResponse(text=final_output, pending_approval=PendingApproval(tool="read", fpath=fpath, payload=""), traces_emitted=0, execution_time_ms=0.0)
try:
tgt = Path(fpath)
if not tgt.exists() or not tgt.is_file():
raise ValueError("Target does not exist or is not a file.")
out = handle_index(semantic_chunker, str(tgt.resolve()))
tool_outputs.append(out)
except Exception as e:
tool_outputs.append(f"[INDEX ERROR: {fpath}]\n{str(e)}")
# 2b. Semantic Read Chunk
read_chunk_matches = re.finditer(r'<read_chunk\s+path="([^"]+)"\s+chunk="([^"]+)"></read_chunk>', llm_output)
for match in read_chunk_matches:
action_found = True
fpath = match.group(1).strip()
chunk_name = match.group(2).strip()
final_output += f"\n[READING CHUNK]: {chunk_name} from `{fpath}`\n"
if not (WORKSPACE_JAIL and is_in_jail(fpath)):
final_output += f"\n[SYSTEM INTERCEPT]: File read to `{fpath}` requires operator approval.\n"
return InvokeResponse(text=final_output, pending_approval=PendingApproval(tool="read", fpath=fpath, payload=""), traces_emitted=0, execution_time_ms=0.0)
try:
tgt = Path(fpath)
if not tgt.exists() or not tgt.is_file():
raise ValueError("Target does not exist or is not a file.")
out = handle_read_chunk(semantic_chunker, str(tgt.resolve()), chunk_name)
tool_outputs.append(out)
except Exception as e:
tool_outputs.append(f"[READ CHUNK ERROR: {fpath} - {chunk_name}]\n{str(e)}")
# 2c. Legacy Read (Redirected to Index Map)
read_matches = re.findall(r'<read>\s*((?:(?!</?read>).)*?)\s*</read>', llm_output, re.DOTALL)
for fpath in read_matches:
action_found = True
fpath = fpath.strip()
final_output += f"\n[READING (LEGACY INTERCEPT)]: `{fpath}`\n"
if not (WORKSPACE_JAIL and is_in_jail(fpath)):
final_output += f"\n[SYSTEM INTERCEPT]: File read to `{fpath}` requires operator approval.\n"
return InvokeResponse(text=final_output, pending_approval=PendingApproval(tool="read", fpath=fpath, payload=""), traces_emitted=0, execution_time_ms=0.0)
try:
tgt = Path(fpath)
if not tgt.exists() or not tgt.is_file():
raise ValueError("Target does not exist or is not a file.")
intercept_warning = f"[SemanticChunker] Legacy <read> intercepted. Returning index map. Use <read_chunk path='{fpath}' chunk='[NAME]'> to access specific content."
content = handle_index(semantic_chunker, str(tgt.resolve()))
tool_outputs.append(f"[READ: {fpath}]\n{intercept_warning}\n{content}")
except Exception as e:
tool_outputs.append(f"[READ ERROR: {fpath}]\n{str(e)}")
# 2d. Read Block (Surgical EVOLVE-BLOCK Extraction)
read_block_matches = re.finditer(r'<read_block\s+target="([^"]+)"\s+block="([^"]+)">\s*</read_block>', llm_output, re.DOTALL)
for match in read_block_matches:
action_found = True
fpath = match.group(1).strip()
block_name = match.group(2).strip()
final_output += f"\n[READING BLOCK]: {block_name} from `{fpath}`\n"
if not (WORKSPACE_JAIL and is_in_jail(fpath)):
final_output += f"\n[SYSTEM INTERCEPT]: File read to `{fpath}` requires operator approval.\n"
return InvokeResponse(text=final_output, pending_approval=PendingApproval(tool="read", fpath=fpath, payload=""), traces_emitted=0, execution_time_ms=0.0)
try:
tgt = Path(fpath)
if not tgt.exists() or not tgt.is_file():
raise ValueError("Target does not exist or is not a file.")
content = tgt.read_text(encoding="utf-8", errors="replace")
start_marker = f"# EVOLVE-BLOCK-{block_name}-START"
end_marker = f"# EVOLVE-BLOCK-{block_name}-END"
if start_marker not in content or end_marker not in content:
raise ValueError(f"EVOLVE-BLOCK markers for '{block_name}' not found.")
try:
extracted = content.split(start_marker, 1)[1].split(end_marker, 1)[0].strip()
tool_outputs.append(f"[READ BLOCK: {fpath} | {block_name}]\n{extracted}")
except Exception:
raise ValueError("Orphaned EVOLVE-BLOCK markers detected.")
except Exception as e:
tool_outputs.append(f"[READ BLOCK ERROR: {fpath} | {block_name}]\n{str(e)}")
# 3. Write Block
write_matches = re.finditer(r'<write\s+path="([^"]+)">\s*((?:(?!</?write>).)*?)\s*</write>', llm_output, re.DOTALL)
for match in write_matches:
action_found = True
fpath = match.group(1).strip()
content = match.group(2)