forked from DigammaF/AxiomEngine
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathview_ledger.py
More file actions
258 lines (218 loc) · 7.5 KB
/
view_ledger.py
File metadata and controls
258 lines (218 loc) · 7.5 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
"""PATHING CONFIG FOR INSPECTION UTILITY."""
import argparse
import os
import sqlite3
import zlib
from src.ledger import initialize_database
CYAN = "\033[96m"
GREEN = "\033[92m"
RED = "\033[91m"
PINK = "\033[95m"
GRAY = "\033[90m"
RESET = "\033[0m"
def print_header(text):
"""Show the header for this log stream."""
print(f"\n{CYAN}=== {text} ==={RESET}")
def ensure_ledger_schema(db_path: str) -> None:
"""Initialize schema for empty or missing."""
conn = sqlite3.connect(db_path)
cur = conn.cursor()
try:
cur.execute(
"SELECT name FROM sqlite_master WHERE type='table' AND name='facts'"
)
needs_init = cur.fetchone() is None
finally:
conn.close()
if needs_init:
initialize_database(db_path)
def print_stats(db_path: str):
"""Show stats as status from synapses and lexicon."""
conn = sqlite3.connect(db_path)
cur = conn.cursor()
try:
cur.execute("SELECT status, COUNT(*) FROM facts GROUP BY status")
stats = cur.fetchall()
cur.execute("SELECT COUNT(*) FROM facts")
total_facts = cur.fetchone()[0]
try:
cur.execute("SELECT COUNT(*) FROM fact_relationships")
rels = cur.fetchone()[0]
except sqlite3.Error:
rels = 0
try:
cur.execute("SELECT COUNT(*) FROM lexicon")
atoms = cur.fetchone()[0]
cur.execute("SELECT COUNT(*) FROM synapses")
synapses = cur.fetchone()[0]
except Exception as e:
atoms, synapses = 0, 0
print(f"Error: {e}")
print_header("LEDGER & MESH STATISTICS")
print(f"Total Facts: {total_facts}")
print(f"Fact Relationships: {rels}")
print(f"Linguistic Atoms: {atoms}")
print(f"Neural Synapses: {synapses}")
print("-" * 30)
status_counts = {"trusted": 0, "disputed": 0, "uncorroborated": 0}
for status, count in stats:
if status in status_counts:
status_counts[status] = count
for status in ("trusted", "disputed", "uncorroborated"):
count = status_counts[status]
label = "trusted (verified)" if status == "trusted" else status
color = (
GREEN
if status == "trusted"
else (RED if status == "disputed" else GRAY)
)
print(f"{color}{label.ljust(20)}: {count}{RESET}")
try:
cur.execute(
"SELECT fragment_state, COUNT(*) FROM facts GROUP BY fragment_state"
)
frag_rows = cur.fetchall()
if frag_rows:
frag_counts = {
"unknown": 0,
"suspected_fragment": 0,
"confirmed_fragment": 0,
"rejected_fragment": 0,
}
for state, count in frag_rows:
if state in frag_counts:
frag_counts[state] = count
print()
print(
f"{PINK}{'fragments (suspect)'.ljust(20)}: {frag_counts['suspected_fragment']}{RESET}"
)
print(
f"{PINK}{'fragments (confirmed)'.ljust(20)}: {frag_counts['confirmed_fragment']}{RESET}"
)
except Exception as e:
print(f"Error: {e}")
except Exception as e:
print(f"Error reading stats: {e}")
finally:
conn.close()
def print_brain(db_path: str, limit=15):
"""Specify and display db_path of brain for nodes and peers."""
conn = sqlite3.connect(db_path)
conn.row_factory = sqlite3.Row
cur = conn.cursor()
try:
print_header(f"TOP NEURAL SYNAPSES (Limit: {limit})")
cur.execute(
"""
SELECT word_a, word_b, relation_type, strength
FROM synapses
ORDER BY strength DESC LIMIT ?
""",
(limit,),
)
rows = cur.fetchall()
if not rows:
print(
f"{GRAY}Brain is currently vacant. Run a reflection cycle.{RESET}",
)
return
for row in rows:
print(
f" {PINK}{row['word_a'].ljust(12)}{RESET} ←({row['relation_type']})→ {PINK}{row['word_b'].ljust(12)}{RESET} [Strength: {row['strength']}]",
)
print_header("HEAVIEST CONCEPTS")
cur.execute(
"SELECT word, occurrence_count FROM lexicon ORDER BY occurrence_count DESC LIMIT 10",
)
for row in cur.fetchall():
print(
f" {row['word'].ljust(15)} : {row['occurrence_count']} occurrences",
)
except Exception as e:
print(f"Error reading brain: {e}")
finally:
conn.close()
def print_facts(db_path: str, limit=20):
"""Process facts to print as decompressed content."""
conn = sqlite3.connect(db_path)
conn.row_factory = sqlite3.Row
cur = conn.cursor()
print_header(f"RECENT RECORDS (Limit: {limit})")
cur.execute(
"SELECT * FROM facts ORDER BY ingest_timestamp_utc DESC LIMIT ?",
(limit,),
)
rows = cur.fetchall()
for row in rows:
r = dict(row)
status = r["status"]
color = (
GREEN
if status == "trusted"
else (RED if status == "disputed" else GRAY)
)
try:
fact_content = zlib.decompress(r["fact_content"]).decode("utf-8")
except (TypeError, zlib.error):
fact_content = f"ERROR: Could not decompress fact content (ID: {r['fact_id'][:8]})."
processed = (
f"{PINK}◈{RESET}"
if r.get("lexically_processed")
else f"{GRAY}◇{RESET}"
)
word_count = len(fact_content.split())
frag_state = r.get("fragment_state", "unknown")
frag_score = r.get("fragment_score", 0.0) or 0.0
if frag_state == "confirmed_fragment":
integrity = f"{RED}FRAGMENT!{RESET}"
elif frag_state == "suspected_fragment" or frag_score >= 0.5:
integrity = f"{RED}FRAGMENT?{RESET}"
else:
integrity = (
f"{GREEN}COMPLETE{RESET}"
if word_count > 8
else f"{RED}FRAGMENT?{RESET}"
)
print(
f"{color}[{status.upper()}]{RESET} {processed} Trust: {r['trust_score']} | Words: {word_count} | {integrity}"
)
print(f" {fact_content}")
print(f" {GRAY}Source: {r['source_url']}{RESET}")
print()
conn.close()
if __name__ == "__main__":
parser = argparse.ArgumentParser(
description="Axiom Ledger & Brain Inspector",
)
parser.add_argument(
"--stats",
action="store_true",
help="Show summary statistics only",
)
parser.add_argument(
"--brain",
action="store_true",
help="Inspect the Lexical Mesh neural pathways",
)
parser.add_argument(
"--limit",
type=int,
default=10,
help="Number of items to show",
)
parser.add_argument(
"--db",
type=str,
default=os.environ.get("AXIOM_DB_PATH", "axiom_ledger.db"),
help="Specify the path to the ledger.db file to inspect.",
)
args = parser.parse_args()
ensure_ledger_schema(args.db)
if args.stats:
print_stats(args.db)
elif args.brain:
print_stats(args.db)
print_brain(args.db, args.limit)
else:
print_stats(args.db)
print_facts(args.db, args.limit)