chore: sync local workspace state

This commit is contained in:
2026-05-30 00:38:57 +02:00
parent 20098a3253
commit e6e8eba8f6
8 changed files with 5626 additions and 68 deletions

View File

@@ -390,13 +390,19 @@ def api_insights(limit: int = Query(8, ge=1, le=50)):
forgotten: list[dict] = []
for r in rows:
meta = json.loads(r["metadata_json"] or "{}")
try:
meta = json.loads(r["metadata_json"] or "{}")
except Exception:
meta = {}
src = meta.get("source", "unknown")
source_counts[src] = source_counts.get(src, 0) + 1
for t in (meta.get("tags") or []):
if isinstance(t, str):
tag_counts[t] = tag_counts.get(t, 0) + 1
host = _host_from_meta(r["metadata_json"])
try:
host = _host_from_meta(r["metadata_json"])
except Exception:
host = None
if host:
host_counts[host] = host_counts.get(host, 0) + 1
@@ -430,7 +436,8 @@ def api_insights(limit: int = Query(8, ge=1, le=50)):
conn.close()
return {
"total": total,
"confirmed": confirmed,
"confirmed": confirmed_true,
"rejected": confirmed_false,
"pending": pending,
"top_tags": top_k(tag_counts),
"top_sources": top_k(source_counts),
@@ -440,7 +447,10 @@ def api_insights(limit: int = Query(8, ge=1, le=50)):
}
@app.get("/api/graph")
def api_graph(limit_nodes: int = Query(200, ge=50, le=1000)):
def api_graph(
limit_nodes: int = Query(0, ge=0, le=50000),
limit_edges: int = Query(0, ge=0, le=200000),
):
"""
Returns a lightweight graph view:
- Nodes: engrams + tag:<tag> + host:<hostname>
@@ -448,8 +458,34 @@ def api_graph(limit_nodes: int = Query(200, ge=50, le=1000)):
"""
conn = get_db()
c = conn.cursor()
rows = c.execute("SELECT id, metadata_json FROM engrams ORDER BY created_at DESC LIMIT 1000").fetchall()
link_rows = c.execute("SELECT from_id, to_id FROM engrams_links ORDER BY rowid DESC LIMIT 2000").fetchall()
if limit_nodes > 0:
# Fetch a bigger window than the final node cap so trim can keep hubs + neighbors.
engram_fetch = min(50000, max(1000, int(limit_nodes * 3)))
else:
engram_fetch = None
if limit_edges > 0:
link_fetch = limit_edges
elif limit_nodes > 0:
link_fetch = min(200000, max(2000, int(limit_nodes * 10)))
else:
link_fetch = None
if engram_fetch is None:
rows = c.execute("SELECT id, metadata_json, correctness_json, created_at, modified_at FROM engrams ORDER BY created_at DESC").fetchall()
else:
rows = c.execute(
"SELECT id, metadata_json, correctness_json, created_at, modified_at FROM engrams ORDER BY created_at DESC LIMIT ?",
(engram_fetch,),
).fetchall()
if link_fetch is None:
link_rows = c.execute("SELECT from_id, to_id FROM engrams_links ORDER BY rowid DESC").fetchall()
else:
link_rows = c.execute(
"SELECT from_id, to_id FROM engrams_links ORDER BY rowid DESC LIMIT ?",
(link_fetch,),
).fetchall()
conn.close()
nodes: dict[str, dict] = {}
@@ -465,24 +501,60 @@ def api_graph(limit_nodes: int = Query(200, ge=50, le=1000)):
for r in rows:
eid = r["id"]
add_node(eid, "engram", label=eid[:8])
try:
meta = json.loads(r["metadata_json"] or "{}")
except Exception:
meta = {}
try:
corr = json.loads(r["correctness_json"] or "{}")
except Exception:
corr = {}
verdict = corr.get("verdict")
if not isinstance(verdict, str) or not verdict:
if corr.get("confirmed", False):
verdict = "confirmed_true"
elif int(corr.get("rejections", 0) or 0) > 0:
verdict = "confirmed_false"
else:
verdict = "unknown"
add_node(
eid,
"engram",
label=eid[:8],
weight=float(meta.get("access_count", 0) or 0),
)
nodes[eid].update(
{
"source": meta.get("source", "unknown"),
"confidence": float(meta.get("confidence", 0.0) or 0.0),
"created": meta.get("created", r["created_at"]),
"modified": meta.get("modified", r["modified_at"]),
"last_accessed": meta.get("last_accessed"),
"verdict": verdict,
"confirmed": bool(corr.get("confirmed", False)),
"rejections": int(corr.get("rejections", 0) or 0),
"grounding": meta.get("grounding", 0),
"predict_locked": bool(meta.get("predict_locked", False)),
}
)
for t in _safe_json_extract_tags(r["metadata_json"]):
tid = f"tag:{t}"
add_node(tid, "tag", label=t)
edges.append({"from": eid, "to": tid, "kind": "has_tag"})
edges.append({"from": eid, "to": tid, "kind": "has_tag", "weight": 0.35})
host = _host_from_meta(r["metadata_json"])
if host:
hid = f"host:{host}"
add_node(hid, "host", label=host)
edges.append({"from": eid, "to": hid, "kind": "grounded_at"})
edges.append({"from": eid, "to": hid, "kind": "grounded_at", "weight": 0.25})
for fr, to in link_rows:
add_node(fr, "engram")
add_node(to, "engram")
edges.append({"from": fr, "to": to, "kind": "link"})
edges.append({"from": fr, "to": to, "kind": "link", "weight": 1.0})
# Trim nodes to keep payload bounded (prefer engrams and connected tags/hosts)
if len(nodes) > limit_nodes:
if limit_nodes > 0 and len(nodes) > limit_nodes:
# Keep a balanced subset: many engrams plus the most-connected non-engrams.
kept: dict[str, dict] = {}
engram_budget = int(limit_nodes * 0.7)