Clean up nested duplicate: v2/v2/engine/beliefs.py
Browse files- v2/v2/engine/beliefs.py +0 -178
v2/v2/engine/beliefs.py
DELETED
|
@@ -1,178 +0,0 @@
|
|
| 1 |
-
"""Seed Rivet's BDI state from what it actually knows.
|
| 2 |
-
|
| 3 |
-
Beliefs are not vibes — every belief carries a confidence and an
|
| 4 |
-
evidence list naming the source it came from:
|
| 5 |
-
|
| 6 |
-
1.0 hard constraints declared by the operators (kintsugi_config.yaml)
|
| 7 |
-
0.95 audit findings that survived adversarial red team (Nexus audit)
|
| 8 |
-
0.9 architecture map facts (human-verified system documentation)
|
| 9 |
-
0.7 derived state (schema snapshot on disk, recent git log)
|
| 10 |
-
|
| 11 |
-
The discipline gate later reasons against these beliefs, and the
|
| 12 |
-
confidence it reports to the user is bounded by the confidence of the
|
| 13 |
-
beliefs it relied on.
|
| 14 |
-
"""
|
| 15 |
-
|
| 16 |
-
import subprocess
|
| 17 |
-
from datetime import datetime, timezone
|
| 18 |
-
from pathlib import Path
|
| 19 |
-
|
| 20 |
-
from kintsugi_core import (
|
| 21 |
-
BDIBelief,
|
| 22 |
-
BDIDesire,
|
| 23 |
-
BDIStore,
|
| 24 |
-
BeliefStatus,
|
| 25 |
-
DesireStatus,
|
| 26 |
-
)
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
def _belief(bid: str, content: str, confidence: float, source: str,
|
| 30 |
-
tags: list, evidence: list) -> BDIBelief:
|
| 31 |
-
return BDIBelief(
|
| 32 |
-
id=bid, content=content, confidence=confidence,
|
| 33 |
-
status=BeliefStatus.ACTIVE, source=source, tags=tags,
|
| 34 |
-
created_at=datetime.now(timezone.utc), evidence=evidence,
|
| 35 |
-
)
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
def seed_bdi(store: BDIStore, config: dict, context_dir: Path) -> None:
|
| 39 |
-
"""Populate the BDI store from config constraints + context files."""
|
| 40 |
-
_seed_constraints(store, config)
|
| 41 |
-
_seed_audit_findings(store)
|
| 42 |
-
_seed_architecture(store, context_dir)
|
| 43 |
-
_seed_schema(store, context_dir)
|
| 44 |
-
_seed_recent_git(store, context_dir)
|
| 45 |
-
_seed_desires(store, config)
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
def _seed_constraints(store: BDIStore, config: dict) -> None:
|
| 49 |
-
for entry in config.get("beliefs", {}).get("constraints", []):
|
| 50 |
-
store.add_belief(_belief(
|
| 51 |
-
bid=f"belief_constraint_{entry['id']}",
|
| 52 |
-
content=entry["content"],
|
| 53 |
-
confidence=1.0,
|
| 54 |
-
source="operator_config",
|
| 55 |
-
tags=["constraint"] + str(entry.get("tags", "")).split(),
|
| 56 |
-
evidence=["kintsugi_config.yaml"],
|
| 57 |
-
))
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
def _seed_audit_findings(store: BDIStore) -> None:
|
| 61 |
-
from skills.security_review import AUDIT_FINDINGS
|
| 62 |
-
for f in AUDIT_FINDINGS:
|
| 63 |
-
store.add_belief(_belief(
|
| 64 |
-
bid=f"belief_audit_{f.id.lower()}",
|
| 65 |
-
content=f"[{f.severity}] {f.title} — {f.advice}",
|
| 66 |
-
confidence=0.95,
|
| 67 |
-
source="nexus_audit_2026-07-10",
|
| 68 |
-
tags=["audit", f.area, f.severity.lower()],
|
| 69 |
-
evidence=[f.file_hint] if f.file_hint else [],
|
| 70 |
-
))
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
def _seed_architecture(store: BDIStore, context_dir: Path) -> None:
|
| 74 |
-
arch = context_dir / "architecture_map.md"
|
| 75 |
-
if not arch.exists():
|
| 76 |
-
return
|
| 77 |
-
text = arch.read_text()
|
| 78 |
-
facts = {
|
| 79 |
-
"stack": ("Campus stack: Node.js 20 / TypeScript / Express 4 / "
|
| 80 |
-
"React 18 + Vite / Zustand (49 stores) / PostgreSQL 16 "
|
| 81 |
-
"(120 tables, 377 migrations) / Redis 7 / Socket.IO 4 / "
|
| 82 |
-
"pg-boss 12 / npm workspaces monorepo."),
|
| 83 |
-
"auth_flow": ("Auth: Bearer JWT (15-min access / 7-day refresh) or "
|
| 84 |
-
"session cookie via Redis. Middleware chain: "
|
| 85 |
-
"verifyToken → rejectIfIneligible → "
|
| 86 |
-
"requireAdmin/requireModerator per-route. WebSocket "
|
| 87 |
-
"auth via handshake.auth.token → verifyToken()."),
|
| 88 |
-
"deploy": ("Deploy: push to main → Coolify rebuild (webhook flaky). "
|
| 89 |
-
"PM2 cluster, Docker multi-stage. deploy-safe.sh does "
|
| 90 |
-
"snapshot + smoke test + rollback."),
|
| 91 |
-
"realtime": ("Real-time: 16 Socket.IO handler modules over a Redis "
|
| 92 |
-
"adapter for PM2 horizontal scaling."),
|
| 93 |
-
}
|
| 94 |
-
for key, content in facts.items():
|
| 95 |
-
store.add_belief(_belief(
|
| 96 |
-
bid=f"belief_arch_{key}",
|
| 97 |
-
content=content,
|
| 98 |
-
confidence=0.9,
|
| 99 |
-
source="architecture_map",
|
| 100 |
-
tags=["architecture", key],
|
| 101 |
-
evidence=[str(arch)],
|
| 102 |
-
))
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
def _seed_schema(store: BDIStore, context_dir: Path) -> None:
|
| 106 |
-
snapshot = context_dir / "schema_snapshot.sql"
|
| 107 |
-
if snapshot.exists():
|
| 108 |
-
tables = snapshot.read_text().count("CREATE TABLE")
|
| 109 |
-
content = (f"Schema snapshot on disk with {tables} CREATE TABLE "
|
| 110 |
-
f"statements (refresh with tools/schema_tools.py).")
|
| 111 |
-
confidence = 0.7
|
| 112 |
-
evidence = [str(snapshot)]
|
| 113 |
-
else:
|
| 114 |
-
content = ("No schema snapshot loaded — schema beliefs are "
|
| 115 |
-
"architecture-map-only. Migration advice is LOW "
|
| 116 |
-
"confidence until a snapshot is pulled.")
|
| 117 |
-
confidence = 0.5
|
| 118 |
-
evidence = []
|
| 119 |
-
store.add_belief(_belief(
|
| 120 |
-
bid="belief_schema_snapshot",
|
| 121 |
-
content=content, confidence=confidence,
|
| 122 |
-
source="schema_tools", tags=["schema"], evidence=evidence,
|
| 123 |
-
))
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
def _seed_recent_git(store: BDIStore, context_dir: Path) -> None:
|
| 127 |
-
git_log = context_dir / "recent_git.txt"
|
| 128 |
-
if not git_log.exists():
|
| 129 |
-
return
|
| 130 |
-
text = git_log.read_text().strip()
|
| 131 |
-
if not text:
|
| 132 |
-
return
|
| 133 |
-
store.add_belief(_belief(
|
| 134 |
-
bid="belief_recent_changes",
|
| 135 |
-
content=f"Recent campus commits:\n{text[:2000]}",
|
| 136 |
-
confidence=0.7,
|
| 137 |
-
source="git_log",
|
| 138 |
-
tags=["git", "recent"],
|
| 139 |
-
evidence=[str(git_log)],
|
| 140 |
-
))
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
def _seed_desires(store: BDIStore, config: dict) -> None:
|
| 144 |
-
for entry in config.get("desires", []):
|
| 145 |
-
store.add_desire(BDIDesire(
|
| 146 |
-
id=f"desire_{entry['id']}",
|
| 147 |
-
content=entry["content"],
|
| 148 |
-
priority=float(entry.get("priority", 0.5)),
|
| 149 |
-
status=DesireStatus.ACTIVE,
|
| 150 |
-
related_tags=str(entry.get("tags", "")).split(),
|
| 151 |
-
measurable=bool(entry.get("measurable", False)),
|
| 152 |
-
metric=entry.get("metric"),
|
| 153 |
-
created_at=datetime.now(timezone.utc),
|
| 154 |
-
))
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
def refresh_git_belief(store: BDIStore, repo_path: str,
|
| 158 |
-
context_dir: Path) -> None:
|
| 159 |
-
"""Pull fresh git history from the campus repo and update the belief."""
|
| 160 |
-
try:
|
| 161 |
-
result = subprocess.run(
|
| 162 |
-
["git", "log", "--oneline", "--since=7 days ago", "-20"],
|
| 163 |
-
cwd=repo_path, capture_output=True, text=True, timeout=10,
|
| 164 |
-
)
|
| 165 |
-
except (subprocess.TimeoutExpired, FileNotFoundError, OSError):
|
| 166 |
-
return
|
| 167 |
-
if result.returncode != 0 or not result.stdout.strip():
|
| 168 |
-
return
|
| 169 |
-
(context_dir / "recent_git.txt").write_text(result.stdout)
|
| 170 |
-
content = f"Recent campus commits:\n{result.stdout[:2000]}"
|
| 171 |
-
if store.get_belief("belief_recent_changes"):
|
| 172 |
-
store.update_belief("belief_recent_changes", content=content)
|
| 173 |
-
else:
|
| 174 |
-
store.add_belief(_belief(
|
| 175 |
-
bid="belief_recent_changes", content=content, confidence=0.7,
|
| 176 |
-
source="git_log", tags=["git", "recent"],
|
| 177 |
-
evidence=[str(context_dir / "recent_git.txt")],
|
| 178 |
-
))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|