Spaces:
Sleeping
Sleeping
Upload jarvis_core/audit.py with huggingface_hub
Browse files- jarvis_core/audit.py +55 -0
jarvis_core/audit.py
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""jarvis_core.audit — append-only action audit log (Firebase: jarvis_audit).
|
| 2 |
+
|
| 3 |
+
Never stores secrets (all free text redacted). Answers:
|
| 4 |
+
"What did JARVIS do today?" / "Show my recent actions." / "What failed?"
|
| 5 |
+
"""
|
| 6 |
+
import datetime
|
| 7 |
+
import time
|
| 8 |
+
|
| 9 |
+
from .store import get_store, redact
|
| 10 |
+
|
| 11 |
+
COL = "jarvis_audit"
|
| 12 |
+
|
| 13 |
+
|
| 14 |
+
def record(user, channel, intent, tool="", args="", approval="none",
|
| 15 |
+
approval_status="", status="SUCCESS", result="", error=""):
|
| 16 |
+
return get_store().add(COL, {
|
| 17 |
+
"timestamp": time.time(), "user": user, "channel": channel,
|
| 18 |
+
"intent": redact(intent)[:300], "tool": tool,
|
| 19 |
+
"arguments_summary": redact(args)[:300],
|
| 20 |
+
"approval_required": approval, "approval_status": approval_status,
|
| 21 |
+
"execution_status": status, "result_summary": redact(result)[:300],
|
| 22 |
+
"error": redact(error)[:300],
|
| 23 |
+
})
|
| 24 |
+
|
| 25 |
+
|
| 26 |
+
def recent(limit=10):
|
| 27 |
+
items = get_store().all(COL)
|
| 28 |
+
return sorted(items, key=lambda a: a.get("timestamp", 0), reverse=True)[:limit]
|
| 29 |
+
|
| 30 |
+
|
| 31 |
+
def today():
|
| 32 |
+
day = datetime.datetime.now().strftime("%Y-%m-%d")
|
| 33 |
+
out = []
|
| 34 |
+
for a in recent(200):
|
| 35 |
+
ts = datetime.datetime.fromtimestamp(a.get("timestamp", 0)).strftime("%Y-%m-%d")
|
| 36 |
+
if ts == day:
|
| 37 |
+
out.append(a)
|
| 38 |
+
return out
|
| 39 |
+
|
| 40 |
+
|
| 41 |
+
def failures(limit=10):
|
| 42 |
+
items = [a for a in get_store().all(COL)
|
| 43 |
+
if a.get("execution_status") == "FAILED"]
|
| 44 |
+
return sorted(items, key=lambda a: a.get("timestamp", 0), reverse=True)[:limit]
|
| 45 |
+
|
| 46 |
+
|
| 47 |
+
def format_row(a):
|
| 48 |
+
ts = datetime.datetime.fromtimestamp(a.get("timestamp", 0)).strftime("%H:%M")
|
| 49 |
+
base = (f"{ts} {a.get('channel')}: {a.get('intent')} "
|
| 50 |
+
f"[{a.get('tool')}] {a.get('execution_status')}")
|
| 51 |
+
if a.get("error"):
|
| 52 |
+
base += f" — {a.get('error')}"
|
| 53 |
+
elif a.get("result_summary"):
|
| 54 |
+
base += f" — {a.get('result_summary')}"
|
| 55 |
+
return base
|