nexo-brain 5.3.19 → 5.3.21
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.claude-plugin/plugin.json +1 -1
- package/bin/nexo-brain.js +52 -10
- package/package.json +1 -1
- package/src/auto_update.py +11 -8
- package/src/dashboard/static/favicon 2.svg +32 -0
- package/src/dashboard/static/nexo-logo 2.png +0 -0
- package/src/dashboard/static/nexo-logo 2.svg +40 -0
- package/src/dashboard/static/style 2.css +2458 -0
- package/src/dashboard/templates/adaptive 2.html +118 -0
- package/src/dashboard/templates/artifacts 2.html +133 -0
- package/src/dashboard/templates/backups 2.html +136 -0
- package/src/dashboard/templates/base 2.html +417 -0
- package/src/dashboard/templates/calendar 2.html +591 -0
- package/src/dashboard/templates/chat 2.html +356 -0
- package/src/dashboard/templates/claims 2.html +259 -0
- package/src/dashboard/templates/cortex 2.html +321 -0
- package/src/dashboard/templates/credentials 2.html +128 -0
- package/src/dashboard/templates/crons 2.html +370 -0
- package/src/dashboard/templates/dashboard 2.html +494 -0
- package/src/dashboard/templates/dreams 2.html +252 -0
- package/src/dashboard/templates/email 2.html +160 -0
- package/src/dashboard/templates/evolution 2.html +189 -0
- package/src/dashboard/templates/feed 2.html +249 -0
- package/src/dashboard/templates/followup_health 2.html +170 -0
- package/src/dashboard/templates/graph 2.html +201 -0
- package/src/dashboard/templates/guard 2.html +259 -0
- package/src/dashboard/templates/inbox 2.html +251 -0
- package/src/dashboard/templates/memory 2.html +420 -0
- package/src/dashboard/templates/operations 2.html +608 -0
- package/src/dashboard/templates/plugins 2.html +185 -0
- package/src/dashboard/templates/protocol 2.html +199 -0
- package/src/dashboard/templates/rules 2.html +246 -0
- package/src/dashboard/templates/sentiment 2.html +247 -0
- package/src/dashboard/templates/sessions 2.html +218 -0
- package/src/dashboard/templates/skills 2.html +329 -0
- package/src/dashboard/templates/somatic 2.html +73 -0
- package/src/dashboard/templates/triggers 2.html +133 -0
- package/src/dashboard/templates/trust 2.html +360 -0
- package/src/db/__init__ 2.py +259 -0
- package/src/db/_core 2.py +437 -0
- package/src/db/_credentials 2.py +124 -0
- package/src/db/_episodic 2.py +762 -0
- package/src/db/_evolution 2.py +54 -0
- package/src/db/_fts 2.py +406 -0
- package/src/db/_goal_profiles 2.py +376 -0
- package/src/db/_hot_context 2.py +660 -0
- package/src/db/_outcomes 2.py +800 -0
- package/src/db/_personal_scripts 2.py +582 -0
- package/src/db/_sessions 2.py +330 -0
- package/src/db/_tasks 2.py +91 -0
- package/src/db/_watchers 2.py +173 -0
- package/src/doctor/formatters 2.py +52 -0
- package/src/doctor/models 2.py +69 -0
- package/src/doctor/planes 2.py +87 -0
- package/src/doctor/providers/__init__ 2.py +1 -0
- package/src/doctor/providers/deep 2.py +367 -0
- package/src/evolution_cycle 2.py +519 -0
- package/src/hooks/auto_capture 2.py +208 -0
- package/src/hooks/caffeinate-guard 2.sh +8 -0
- package/src/hooks/capture-session 2.sh +21 -0
- package/src/hooks/capture-tool-logs 2.sh +158 -0
- package/src/hooks/daily-briefing-check 2.sh +33 -0
- package/src/hooks/heartbeat-enforcement 2.py +90 -0
- package/src/hooks/heartbeat-posttool 2.sh +18 -0
- package/src/hooks/inbox-hook 2.sh +76 -0
- package/src/hooks/post-compact 2.sh +152 -0
- package/src/hooks/pre-compact 2.sh +169 -0
- package/src/hooks/protocol-guardrail 2.sh +10 -0
- package/src/hooks/protocol-pretool-guardrail 2.sh +9 -0
- package/src/hooks/session-stop 2.sh +52 -0
- package/src/kg_populate 2.py +292 -0
- package/src/maintenance 2.py +53 -0
- package/src/memory_backends 2.py +71 -0
- package/src/migrate_embeddings 2.py +124 -0
- package/src/nexo_sdk 2.py +103 -0
- package/src/observability 2.py +199 -0
- package/src/plugin_loader 2.py +217 -0
- package/src/plugins/__init__ 2.py +0 -0
- package/src/plugins/artifact_registry 2.py +450 -0
- package/src/plugins/backup 2.py +127 -0
- package/src/plugins/claims_tools 2.py +119 -0
- package/src/plugins/cognitive_memory 2.py +609 -0
- package/src/plugins/core_rules 2.py +252 -0
- package/src/plugins/cortex 2.py +1155 -0
- package/src/plugins/entities 2.py +67 -0
- package/src/plugins/episodic_memory 2.py +560 -0
- package/src/plugins/evolution 2.py +167 -0
- package/src/plugins/goal_engine 2.py +142 -0
- package/src/plugins/guard 2.py +862 -0
- package/src/plugins/impact 2.py +29 -0
- package/src/plugins/knowledge_graph_tools 2.py +137 -0
- package/src/plugins/media_memory_tools 2.py +98 -0
- package/src/plugins/memory_export 2.py +196 -0
- package/src/plugins/outcomes 2.py +130 -0
- package/src/plugins/personal_scripts 2.py +117 -0
- package/src/plugins/preferences 2.py +47 -0
- package/src/plugins/protocol 2.py +1449 -0
- package/src/plugins/simple_api 2.py +106 -0
- package/src/plugins/skills 2.py +341 -0
- package/src/plugins/state_watchers 2.py +79 -0
- package/src/plugins/update 2.py +986 -0
- package/src/plugins/user_state_tools 2.py +43 -0
- package/src/plugins/workflow 2.py +588 -0
- package/src/protocol_settings 2.py +59 -0
- package/src/public_contribution 2.py +466 -0
- package/src/public_evolution_queue 2.py +241 -0
- package/src/requirements 2.txt +14 -0
- package/src/retroactive_learnings 2.py +373 -0
- package/src/rules/__init__ 2.py +0 -0
- package/src/rules/core-rules 2.json +331 -0
- package/src/rules/migrate 2.py +207 -0
- package/src/runtime_power 2.py +874 -0
- package/src/script_registry 2.py +1559 -0
- package/src/scripts/check-context 2.py +272 -0
- package/src/scripts/deep-sleep/apply_findings 2.py +2327 -0
- package/src/scripts/deep-sleep/collect 2.py +928 -0
- package/src/scripts/deep-sleep/extract 2.py +330 -0
- package/src/scripts/deep-sleep/extract-prompt 2.md +285 -0
- package/src/scripts/deep-sleep/synthesize 2.py +312 -0
- package/src/scripts/deep-sleep/synthesize-prompt 2.md +336 -0
- package/src/scripts/nexo-agent-run 2.py +75 -0
- package/src/scripts/nexo-auto-update 2.py +6 -0
- package/src/scripts/nexo-backup 2.sh +25 -0
- package/src/scripts/nexo-brain-activation 2.sh +140 -0
- package/src/scripts/nexo-catchup 2.py +300 -0
- package/src/scripts/nexo-cognitive-decay 2.py +257 -0
- package/src/scripts/nexo-cortex-cycle 2.py +293 -0
- package/src/scripts/nexo-cron-wrapper 2.sh +53 -0
- package/src/scripts/nexo-daily-self-audit 2.py +2161 -0
- package/src/scripts/nexo-dashboard 2.sh +29 -0
- package/src/scripts/nexo-deep-sleep 2.sh +86 -0
- package/src/scripts/nexo-evolution-run 2.py +1664 -0
- package/src/scripts/nexo-followup-hygiene 2.py +139 -0
- package/src/scripts/nexo-hook-record 2.py +42 -0
- package/src/scripts/nexo-immune 2.py +936 -0
- package/src/scripts/nexo-impact-scorer 2.py +117 -0
- package/src/scripts/nexo-inbox-hook 2.sh +74 -0
- package/src/scripts/nexo-install 2.py +6 -0
- package/src/scripts/nexo-learning-housekeep 2.py +401 -0
- package/src/scripts/nexo-learning-validator 2.py +266 -0
- package/src/scripts/nexo-migrate 2.py +260 -0
- package/src/scripts/nexo-outcome-checker 2.py +127 -0
- package/src/scripts/nexo-postmortem-consolidator 2.py +456 -0
- package/src/scripts/nexo-pre-commit 2.py +120 -0
- package/src/scripts/nexo-prevent-sleep 2.sh +35 -0
- package/src/scripts/nexo-proactive-dashboard 2.py +354 -0
- package/src/scripts/nexo-reflection 2.py +256 -0
- package/src/scripts/nexo-runtime-preflight 2.py +274 -0
- package/src/scripts/nexo-sleep 2.py +631 -0
- package/src/scripts/nexo-snapshot-restore 2.sh +35 -0
- package/src/scripts/nexo-sync-clients 2.py +16 -0
- package/src/scripts/nexo-synthesis 2.py +475 -0
- package/src/scripts/nexo-tcc-approve 2.sh +79 -0
- package/src/scripts/nexo-update 2.sh +306 -0
- package/src/scripts/nexo-watchdog 2.sh +1207 -0
- package/src/scripts/nexo-watchdog-smoke 2.py +119 -0
- package/src/scripts/rehydrate_learnings_from_archive 2.py +245 -0
- package/src/server 2.py +1296 -0
- package/src/skills/run-nexo-audit-phase/guide 2.md +43 -0
- package/src/skills/run-nexo-audit-phase/skill 2.json +59 -0
- package/src/skills/run-nexo-core-fix-cycle/guide 2.md +17 -0
- package/src/skills/run-nexo-core-fix-cycle/script 2.py +276 -0
- package/src/skills/run-nexo-core-fix-cycle/skill 2.json +58 -0
- package/src/skills/run-release-final-audit/guide 2.md +16 -0
- package/src/skills/run-release-final-audit/script 2.py +259 -0
- package/src/skills/run-release-final-audit/skill 2.json +77 -0
- package/src/skills/run-runtime-doctor/guide 2.md +12 -0
- package/src/skills/run-runtime-doctor/script 2.py +21 -0
- package/src/skills/run-runtime-doctor/skill 2.json +25 -0
- package/src/skills_runtime 2.py +932 -0
- package/src/state_watchers_runtime 2.py +475 -0
- package/src/storage_router 2.py +32 -0
- package/src/system_catalog 2.py +786 -0
- package/src/tools_coordination 2.py +103 -0
- package/src/tools_credentials 2.py +68 -0
- package/src/tools_drive 2.py +487 -0
- package/src/tools_hot_context 2.py +163 -0
- package/src/tools_learnings 2.py +612 -0
- package/src/tools_menu 2.py +229 -0
- package/src/tools_reminders 2.py +88 -0
- package/src/tools_reminders_crud 2.py +363 -0
- package/src/tools_sessions 2.py +1054 -0
- package/src/tools_system_catalog 2.py +19 -0
- package/src/tools_task_history 2.py +57 -0
- package/src/tools_transcripts 2.py +98 -0
- package/src/transcript_utils 2.py +412 -0
- package/src/user_context 2.py +46 -0
- package/src/user_data_portability 2.py +328 -0
- package/src/user_state_model 2.py +170 -0
- package/templates/CLAUDE.md 2.template +108 -0
- package/templates/CODEX.AGENTS.md 2.template +66 -0
- package/templates/launchagents/README 2.md +132 -0
- package/templates/launchagents/com.nexo.auto-close-sessions 2.plist +39 -0
- package/templates/launchagents/com.nexo.catchup 2.plist +39 -0
- package/templates/launchagents/com.nexo.cognitive-decay 2.plist +40 -0
- package/templates/launchagents/com.nexo.dashboard 2.plist +43 -0
- package/templates/launchagents/com.nexo.deep-sleep 2.plist +43 -0
- package/templates/launchagents/com.nexo.evolution 2.plist +44 -0
- package/templates/launchagents/com.nexo.followup-hygiene 2.plist +45 -0
- package/templates/launchagents/com.nexo.immune 2.plist +41 -0
- package/templates/launchagents/com.nexo.postmortem 2.plist +45 -0
- package/templates/launchagents/com.nexo.self-audit 2.plist +47 -0
- package/templates/launchagents/com.nexo.synthesis 2.plist +45 -0
- package/templates/launchagents/com.nexo.watchdog 2.plist +37 -0
- package/templates/nexo_helper 2.py +301 -0
- package/templates/openclaw 2.json +13 -0
- package/templates/plugin-template 2.py +40 -0
- package/templates/script-template 2.py +59 -0
- package/templates/script-template 2.sh +13 -0
- package/templates/skill-script-template 2.py +48 -0
- package/templates/skill-template 2.md +33 -0
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
"""Doctor data models — check results and report structure."""
|
|
2
|
+
from __future__ import annotations
|
|
3
|
+
|
|
4
|
+
import traceback
|
|
5
|
+
from dataclasses import dataclass, field
|
|
6
|
+
from typing import Callable
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
@dataclass
|
|
10
|
+
class DoctorCheck:
|
|
11
|
+
id: str
|
|
12
|
+
tier: str
|
|
13
|
+
status: str # healthy, degraded, critical
|
|
14
|
+
severity: str # info, warn, error
|
|
15
|
+
summary: str
|
|
16
|
+
evidence: list[str] = field(default_factory=list)
|
|
17
|
+
repair_plan: list[str] = field(default_factory=list)
|
|
18
|
+
escalation_prompt: str = ""
|
|
19
|
+
fixed: bool = False
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
@dataclass
|
|
23
|
+
class DoctorReport:
|
|
24
|
+
overall_status: str # healthy, degraded, critical
|
|
25
|
+
counts: dict = field(default_factory=dict)
|
|
26
|
+
checks: list[DoctorCheck] = field(default_factory=list)
|
|
27
|
+
duration_ms: int = 0
|
|
28
|
+
|
|
29
|
+
def add(self, check: DoctorCheck):
|
|
30
|
+
self.checks.append(check)
|
|
31
|
+
|
|
32
|
+
def compute_status(self):
|
|
33
|
+
"""Compute overall status from individual checks."""
|
|
34
|
+
statuses = [c.status for c in self.checks]
|
|
35
|
+
if "critical" in statuses:
|
|
36
|
+
self.overall_status = "critical"
|
|
37
|
+
elif "degraded" in statuses:
|
|
38
|
+
self.overall_status = "degraded"
|
|
39
|
+
else:
|
|
40
|
+
self.overall_status = "healthy"
|
|
41
|
+
self.counts = {
|
|
42
|
+
"healthy": statuses.count("healthy"),
|
|
43
|
+
"degraded": statuses.count("degraded"),
|
|
44
|
+
"critical": statuses.count("critical"),
|
|
45
|
+
"total": len(statuses),
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
def safe_check(fn: Callable[..., DoctorCheck], *args, **kwargs) -> DoctorCheck:
|
|
50
|
+
"""Run a single check function, returning a crash DoctorCheck on exception.
|
|
51
|
+
|
|
52
|
+
This isolates individual checks so one failure doesn't take down
|
|
53
|
+
all sibling checks within a tier.
|
|
54
|
+
"""
|
|
55
|
+
try:
|
|
56
|
+
return fn(*args, **kwargs)
|
|
57
|
+
except Exception as exc:
|
|
58
|
+
tb = traceback.format_exception(type(exc), exc, exc.__traceback__)
|
|
59
|
+
last_frame = tb[-1].strip() if tb else str(exc)
|
|
60
|
+
check_name = getattr(fn, "__name__", "unknown")
|
|
61
|
+
return DoctorCheck(
|
|
62
|
+
id=f"check.{check_name}_crashed",
|
|
63
|
+
tier="unknown",
|
|
64
|
+
status="critical",
|
|
65
|
+
severity="error",
|
|
66
|
+
summary=f"Check {check_name} crashed: {type(exc).__name__}: {exc}",
|
|
67
|
+
evidence=[last_frame],
|
|
68
|
+
repair_plan=[f"Investigate {check_name} — exception during check execution"],
|
|
69
|
+
)
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
"""Diagnostic plane preflight for NEXO Doctor."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from doctor.models import DoctorCheck
|
|
6
|
+
|
|
7
|
+
VALID_DIAGNOSTIC_PLANES = {
|
|
8
|
+
"product_public": {
|
|
9
|
+
"label": "producto público",
|
|
10
|
+
"use": "release contracts, artefactos publicados, compare/, docs y surfaces públicas del repo",
|
|
11
|
+
},
|
|
12
|
+
"runtime_personal": {
|
|
13
|
+
"label": "runtime personal",
|
|
14
|
+
"use": "~/.nexo, scripts personales, followups, reminders y hábitos operativos del operador",
|
|
15
|
+
},
|
|
16
|
+
"installation_live": {
|
|
17
|
+
"label": "instalación viva",
|
|
18
|
+
"use": "runtime instalado, hooks activos, clientes conectados, cron sync y parity de la instalación local",
|
|
19
|
+
},
|
|
20
|
+
"database_real": {
|
|
21
|
+
"label": "BD real",
|
|
22
|
+
"use": "SQLite/MySQL reales, filas, schema, deudas, sesiones y evidencia persistida",
|
|
23
|
+
},
|
|
24
|
+
"cooperator": {
|
|
25
|
+
"label": "co-operador",
|
|
26
|
+
"use": "comportamiento del agente, protocolo, comunicación y decisiones del asistente",
|
|
27
|
+
},
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
DOCTOR_COMPATIBLE_PLANES = {"runtime_personal", "installation_live", "database_real"}
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
def normalize_diagnostic_plane(plane: str = "") -> str:
|
|
34
|
+
clean = (plane or "").strip().lower().replace("-", "_").replace(" ", "_")
|
|
35
|
+
return clean if clean in VALID_DIAGNOSTIC_PLANES else ""
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
def diagnostic_plane_choices() -> list[str]:
|
|
39
|
+
return sorted(VALID_DIAGNOSTIC_PLANES)
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
def diagnostic_plane_preflight(plane: str = "") -> tuple[str, DoctorCheck | None]:
|
|
43
|
+
clean_plane = normalize_diagnostic_plane(plane)
|
|
44
|
+
if not clean_plane:
|
|
45
|
+
options = ", ".join(diagnostic_plane_choices())
|
|
46
|
+
return "", DoctorCheck(
|
|
47
|
+
id="orchestrator.diagnostic_plane_required",
|
|
48
|
+
tier="orchestrator",
|
|
49
|
+
status="critical",
|
|
50
|
+
severity="error",
|
|
51
|
+
summary="El diagnóstico está bloqueado hasta fijar explícitamente el plano",
|
|
52
|
+
evidence=[
|
|
53
|
+
f"planes válidos: {options}",
|
|
54
|
+
"Usa `runtime_personal` para ~/.nexo y hábitos del runtime; `installation_live` para hooks/clientes/instalación; `database_real` para filas y schema reales.",
|
|
55
|
+
],
|
|
56
|
+
repair_plan=[
|
|
57
|
+
"Repite `nexo_doctor` o `nexo doctor` con `plane='runtime_personal'`, `plane='installation_live'` o `plane='database_real'`.",
|
|
58
|
+
"Si el problema pertenece a producto público o al co-operador, usa el surface correcto en vez de NEXO Doctor.",
|
|
59
|
+
],
|
|
60
|
+
escalation_prompt=(
|
|
61
|
+
"NEXO mezcló planos en diagnósticos anteriores. El doctor no debe correr hasta que se elija "
|
|
62
|
+
"explícitamente si el problema está en producto público, runtime personal, instalación viva, BD real o co-operador."
|
|
63
|
+
),
|
|
64
|
+
)
|
|
65
|
+
|
|
66
|
+
if clean_plane not in DOCTOR_COMPATIBLE_PLANES:
|
|
67
|
+
plane_info = VALID_DIAGNOSTIC_PLANES[clean_plane]
|
|
68
|
+
return clean_plane, DoctorCheck(
|
|
69
|
+
id="orchestrator.diagnostic_plane_mismatch",
|
|
70
|
+
tier="orchestrator",
|
|
71
|
+
status="degraded",
|
|
72
|
+
severity="warn",
|
|
73
|
+
summary=f"NEXO Doctor no es la superficie correcta para el plano {plane_info['label']}",
|
|
74
|
+
evidence=[
|
|
75
|
+
f"plane: {clean_plane}",
|
|
76
|
+
f"este plano se diagnostica mejor desde: {plane_info['use']}",
|
|
77
|
+
],
|
|
78
|
+
repair_plan=[
|
|
79
|
+
"Si quieres diagnosticar runtime/instalación/BD, vuelve a lanzar el doctor con el plano correcto.",
|
|
80
|
+
"Si el problema es del producto público o del co-operador, usa release checks, repo checks o herramientas de protocolo/sesión en vez de Doctor.",
|
|
81
|
+
],
|
|
82
|
+
escalation_prompt=(
|
|
83
|
+
"El plano elegido no corresponde al runtime doctor. Cambia de plano o de herramienta antes de seguir para no mezclar diagnóstico técnico con producto o comportamiento del agente."
|
|
84
|
+
),
|
|
85
|
+
)
|
|
86
|
+
|
|
87
|
+
return clean_plane, None
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"""Doctor check providers — boot, runtime, deep."""
|
|
@@ -0,0 +1,367 @@
|
|
|
1
|
+
"""Deep tier checks — read existing artifacts for richer validation. Target <60s."""
|
|
2
|
+
from __future__ import annotations
|
|
3
|
+
|
|
4
|
+
import datetime as dt
|
|
5
|
+
import json
|
|
6
|
+
import os
|
|
7
|
+
import time
|
|
8
|
+
from pathlib import Path
|
|
9
|
+
|
|
10
|
+
from cron_recovery import load_enabled_crons
|
|
11
|
+
from doctor.models import DoctorCheck, safe_check
|
|
12
|
+
|
|
13
|
+
NEXO_HOME = Path(os.environ.get("NEXO_HOME", str(Path.home() / ".nexo")))
|
|
14
|
+
|
|
15
|
+
# Freshness thresholds
|
|
16
|
+
SELF_AUDIT_FRESHNESS = 86400 * 2 # 2 days (runs daily)
|
|
17
|
+
SELF_AUDIT_BOOTSTRAP_GRACE = 86400 # 1 day grace after install/update before the first summary exists
|
|
18
|
+
PREFLIGHT_FRESHNESS = 86400 # 1 day
|
|
19
|
+
WATCHDOG_SMOKE_FRESHNESS = 86400 # 1 day
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
def _file_age_seconds(path: Path) -> float | None:
|
|
23
|
+
try:
|
|
24
|
+
if path.is_file():
|
|
25
|
+
return time.time() - path.stat().st_mtime
|
|
26
|
+
except Exception:
|
|
27
|
+
pass
|
|
28
|
+
return None
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
def _load_json(path: Path) -> dict:
|
|
32
|
+
return json.loads(path.read_text())
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
def _timestamp_age_seconds(value: str) -> float | None:
|
|
36
|
+
raw = str(value or "").strip()
|
|
37
|
+
if not raw:
|
|
38
|
+
return None
|
|
39
|
+
try:
|
|
40
|
+
parsed = dt.datetime.fromisoformat(raw.replace("Z", "+00:00"))
|
|
41
|
+
except Exception:
|
|
42
|
+
return None
|
|
43
|
+
if parsed.tzinfo is None:
|
|
44
|
+
parsed = parsed.replace(tzinfo=dt.timezone.utc)
|
|
45
|
+
return max(0.0, time.time() - parsed.timestamp())
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
def _runtime_bootstrap_age_seconds() -> float | None:
|
|
49
|
+
version_file = NEXO_HOME / "version.json"
|
|
50
|
+
try:
|
|
51
|
+
payload = _load_json(version_file)
|
|
52
|
+
except Exception:
|
|
53
|
+
payload = {}
|
|
54
|
+
for key in ("updated_at", "installed_at"):
|
|
55
|
+
age = _timestamp_age_seconds(str(payload.get(key, "") or ""))
|
|
56
|
+
if age is not None:
|
|
57
|
+
return age
|
|
58
|
+
return _file_age_seconds(version_file)
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
def _self_audit_enabled() -> bool | None:
|
|
62
|
+
try:
|
|
63
|
+
return any(str(cron.get("id") or "").strip() == "self-audit" for cron in load_enabled_crons())
|
|
64
|
+
except Exception:
|
|
65
|
+
return None
|
|
66
|
+
|
|
67
|
+
|
|
68
|
+
def check_self_audit_summary() -> DoctorCheck:
|
|
69
|
+
"""Check latest self-audit summary exists and is recent."""
|
|
70
|
+
summary_file = NEXO_HOME / "logs" / "self-audit-summary.json"
|
|
71
|
+
age = _file_age_seconds(summary_file)
|
|
72
|
+
|
|
73
|
+
if age is None:
|
|
74
|
+
enabled = _self_audit_enabled()
|
|
75
|
+
if enabled is False:
|
|
76
|
+
return DoctorCheck(
|
|
77
|
+
id="deep.self_audit",
|
|
78
|
+
tier="deep",
|
|
79
|
+
status="healthy",
|
|
80
|
+
severity="info",
|
|
81
|
+
summary="Self-audit automation disabled or not installed",
|
|
82
|
+
)
|
|
83
|
+
|
|
84
|
+
bootstrap_age = _runtime_bootstrap_age_seconds()
|
|
85
|
+
if enabled and bootstrap_age is not None and bootstrap_age <= SELF_AUDIT_BOOTSTRAP_GRACE:
|
|
86
|
+
bootstrap_hours = bootstrap_age / 3600
|
|
87
|
+
return DoctorCheck(
|
|
88
|
+
id="deep.self_audit",
|
|
89
|
+
tier="deep",
|
|
90
|
+
status="healthy",
|
|
91
|
+
severity="info",
|
|
92
|
+
summary="Self-audit scheduled but no summary yet",
|
|
93
|
+
evidence=[
|
|
94
|
+
f"Runtime install/update {bootstrap_hours:.0f} hours ago",
|
|
95
|
+
f"Expected later at: {summary_file}",
|
|
96
|
+
],
|
|
97
|
+
)
|
|
98
|
+
|
|
99
|
+
return DoctorCheck(
|
|
100
|
+
id="deep.self_audit",
|
|
101
|
+
tier="deep",
|
|
102
|
+
status="degraded",
|
|
103
|
+
severity="warn",
|
|
104
|
+
summary="Self-audit summary not found",
|
|
105
|
+
evidence=[f"Expected: {summary_file}"],
|
|
106
|
+
repair_plan=["Check if daily self-audit cron is installed"],
|
|
107
|
+
)
|
|
108
|
+
|
|
109
|
+
age_hours = age / 3600
|
|
110
|
+
if age > SELF_AUDIT_FRESHNESS:
|
|
111
|
+
return DoctorCheck(
|
|
112
|
+
id="deep.self_audit",
|
|
113
|
+
tier="deep",
|
|
114
|
+
status="degraded",
|
|
115
|
+
severity="warn",
|
|
116
|
+
summary=f"Self-audit summary stale ({age_hours:.0f}h old)",
|
|
117
|
+
evidence=[f"Last modified {age_hours:.0f} hours ago, threshold {SELF_AUDIT_FRESHNESS // 3600}h"],
|
|
118
|
+
)
|
|
119
|
+
|
|
120
|
+
try:
|
|
121
|
+
data = _load_json(summary_file)
|
|
122
|
+
counts = data.get("counts") or {}
|
|
123
|
+
error_count = int(counts.get("error", 0) or 0)
|
|
124
|
+
warn_count = int(counts.get("warn", 0) or 0)
|
|
125
|
+
findings = data.get("findings") or []
|
|
126
|
+
if error_count > 0:
|
|
127
|
+
status = "critical"
|
|
128
|
+
severity = "error"
|
|
129
|
+
else:
|
|
130
|
+
status = "healthy"
|
|
131
|
+
severity = "info"
|
|
132
|
+
return DoctorCheck(
|
|
133
|
+
id="deep.self_audit",
|
|
134
|
+
tier="deep",
|
|
135
|
+
status=status,
|
|
136
|
+
severity=severity,
|
|
137
|
+
summary=(
|
|
138
|
+
f"Self-audit: {len(findings)} findings "
|
|
139
|
+
f"({error_count} error, {warn_count} warn; {age_hours:.0f}h ago)"
|
|
140
|
+
),
|
|
141
|
+
)
|
|
142
|
+
except Exception as e:
|
|
143
|
+
return DoctorCheck(
|
|
144
|
+
id="deep.self_audit",
|
|
145
|
+
tier="deep",
|
|
146
|
+
status="degraded",
|
|
147
|
+
severity="warn",
|
|
148
|
+
summary=f"Self-audit summary unreadable ({age_hours:.0f}h ago)",
|
|
149
|
+
evidence=[str(e)],
|
|
150
|
+
)
|
|
151
|
+
|
|
152
|
+
|
|
153
|
+
def check_schema_version() -> DoctorCheck:
|
|
154
|
+
"""Check DB schema version is present and reasonable."""
|
|
155
|
+
try:
|
|
156
|
+
import sqlite3
|
|
157
|
+
db_path = NEXO_HOME / "data" / "nexo.db"
|
|
158
|
+
if not db_path.is_file():
|
|
159
|
+
return DoctorCheck(
|
|
160
|
+
id="deep.schema_version",
|
|
161
|
+
tier="deep",
|
|
162
|
+
status="degraded",
|
|
163
|
+
severity="warn",
|
|
164
|
+
summary="No database to check schema",
|
|
165
|
+
)
|
|
166
|
+
conn = sqlite3.connect(str(db_path), timeout=2)
|
|
167
|
+
try:
|
|
168
|
+
version = conn.execute("PRAGMA user_version").fetchone()[0]
|
|
169
|
+
finally:
|
|
170
|
+
conn.close()
|
|
171
|
+
return DoctorCheck(
|
|
172
|
+
id="deep.schema_version",
|
|
173
|
+
tier="deep",
|
|
174
|
+
status="healthy",
|
|
175
|
+
severity="info",
|
|
176
|
+
summary=f"DB schema version: {version}",
|
|
177
|
+
)
|
|
178
|
+
except Exception as e:
|
|
179
|
+
return DoctorCheck(
|
|
180
|
+
id="deep.schema_version",
|
|
181
|
+
tier="deep",
|
|
182
|
+
status="degraded",
|
|
183
|
+
severity="warn",
|
|
184
|
+
summary=f"Schema check failed: {e}",
|
|
185
|
+
)
|
|
186
|
+
|
|
187
|
+
|
|
188
|
+
def check_preflight_summary() -> DoctorCheck:
|
|
189
|
+
"""Check runtime preflight summary."""
|
|
190
|
+
summary_file = NEXO_HOME / "logs" / "runtime-preflight-summary.json"
|
|
191
|
+
age = _file_age_seconds(summary_file)
|
|
192
|
+
|
|
193
|
+
if age is None:
|
|
194
|
+
return DoctorCheck(
|
|
195
|
+
id="deep.preflight",
|
|
196
|
+
tier="deep",
|
|
197
|
+
status="healthy",
|
|
198
|
+
severity="info",
|
|
199
|
+
summary="No preflight summary (optional)",
|
|
200
|
+
)
|
|
201
|
+
|
|
202
|
+
age_hours = age / 3600
|
|
203
|
+
if age > PREFLIGHT_FRESHNESS:
|
|
204
|
+
return DoctorCheck(
|
|
205
|
+
id="deep.preflight",
|
|
206
|
+
tier="deep",
|
|
207
|
+
status="degraded",
|
|
208
|
+
severity="warn",
|
|
209
|
+
summary=f"Preflight summary stale ({age_hours:.0f}h old)",
|
|
210
|
+
)
|
|
211
|
+
try:
|
|
212
|
+
data = _load_json(summary_file)
|
|
213
|
+
ok = data.get("ok")
|
|
214
|
+
checks = data.get("checks") or {}
|
|
215
|
+
errors = data.get("errors") or []
|
|
216
|
+
if ok is True:
|
|
217
|
+
return DoctorCheck(
|
|
218
|
+
id="deep.preflight",
|
|
219
|
+
tier="deep",
|
|
220
|
+
status="healthy",
|
|
221
|
+
severity="info",
|
|
222
|
+
summary=f"Runtime preflight OK ({len(checks)} checks, {age_hours:.0f}h ago)",
|
|
223
|
+
)
|
|
224
|
+
return DoctorCheck(
|
|
225
|
+
id="deep.preflight",
|
|
226
|
+
tier="deep",
|
|
227
|
+
status="critical",
|
|
228
|
+
severity="error",
|
|
229
|
+
summary=f"Runtime preflight failed ({len(errors)} errors, {age_hours:.0f}h ago)",
|
|
230
|
+
evidence=errors[:5],
|
|
231
|
+
)
|
|
232
|
+
except Exception as e:
|
|
233
|
+
return DoctorCheck(
|
|
234
|
+
id="deep.preflight",
|
|
235
|
+
tier="deep",
|
|
236
|
+
status="degraded",
|
|
237
|
+
severity="warn",
|
|
238
|
+
summary=f"Preflight summary unreadable ({age_hours:.0f}h ago)",
|
|
239
|
+
evidence=[str(e)],
|
|
240
|
+
)
|
|
241
|
+
|
|
242
|
+
|
|
243
|
+
def check_watchdog_smoke() -> DoctorCheck:
|
|
244
|
+
"""Check watchdog smoke summary."""
|
|
245
|
+
summary_file = NEXO_HOME / "logs" / "watchdog-smoke-summary.json"
|
|
246
|
+
age = _file_age_seconds(summary_file)
|
|
247
|
+
|
|
248
|
+
if age is None:
|
|
249
|
+
return DoctorCheck(
|
|
250
|
+
id="deep.watchdog_smoke",
|
|
251
|
+
tier="deep",
|
|
252
|
+
status="healthy",
|
|
253
|
+
severity="info",
|
|
254
|
+
summary="No watchdog smoke summary (optional)",
|
|
255
|
+
)
|
|
256
|
+
|
|
257
|
+
age_hours = age / 3600
|
|
258
|
+
if age > WATCHDOG_SMOKE_FRESHNESS:
|
|
259
|
+
return DoctorCheck(
|
|
260
|
+
id="deep.watchdog_smoke",
|
|
261
|
+
tier="deep",
|
|
262
|
+
status="degraded",
|
|
263
|
+
severity="warn",
|
|
264
|
+
summary=f"Watchdog smoke summary stale ({age_hours:.0f}h old)",
|
|
265
|
+
)
|
|
266
|
+
|
|
267
|
+
try:
|
|
268
|
+
data = _load_json(summary_file)
|
|
269
|
+
ok = data.get("ok")
|
|
270
|
+
findings = data.get("findings") or []
|
|
271
|
+
error_count = sum(1 for finding in findings if finding.get("severity") == "ERROR")
|
|
272
|
+
if ok is True:
|
|
273
|
+
return DoctorCheck(
|
|
274
|
+
id="deep.watchdog_smoke",
|
|
275
|
+
tier="deep",
|
|
276
|
+
status="healthy",
|
|
277
|
+
severity="info",
|
|
278
|
+
summary=f"Watchdog smoke OK ({len(findings)} findings, {age_hours:.0f}h ago)",
|
|
279
|
+
)
|
|
280
|
+
return DoctorCheck(
|
|
281
|
+
id="deep.watchdog_smoke",
|
|
282
|
+
tier="deep",
|
|
283
|
+
status="critical",
|
|
284
|
+
severity="error",
|
|
285
|
+
summary=f"Watchdog smoke failed ({error_count} errors, {age_hours:.0f}h ago)",
|
|
286
|
+
evidence=[finding.get("msg", "") for finding in findings[:5]],
|
|
287
|
+
)
|
|
288
|
+
except Exception as e:
|
|
289
|
+
return DoctorCheck(
|
|
290
|
+
id="deep.watchdog_smoke",
|
|
291
|
+
tier="deep",
|
|
292
|
+
status="degraded",
|
|
293
|
+
severity="warn",
|
|
294
|
+
summary=f"Watchdog smoke summary unreadable ({age_hours:.0f}h ago)",
|
|
295
|
+
evidence=[str(e)],
|
|
296
|
+
)
|
|
297
|
+
|
|
298
|
+
|
|
299
|
+
def check_learning_count() -> DoctorCheck:
|
|
300
|
+
"""Check learning count as a health proxy."""
|
|
301
|
+
try:
|
|
302
|
+
import sqlite3
|
|
303
|
+
db_path = NEXO_HOME / "data" / "nexo.db"
|
|
304
|
+
if not db_path.is_file():
|
|
305
|
+
return DoctorCheck(
|
|
306
|
+
id="deep.learning_count",
|
|
307
|
+
tier="deep",
|
|
308
|
+
status="healthy",
|
|
309
|
+
severity="info",
|
|
310
|
+
summary="No DB to check learnings",
|
|
311
|
+
)
|
|
312
|
+
conn = sqlite3.connect(str(db_path), timeout=2)
|
|
313
|
+
try:
|
|
314
|
+
tables = conn.execute(
|
|
315
|
+
"SELECT name FROM sqlite_master WHERE type='table' AND name='learnings'"
|
|
316
|
+
).fetchone()
|
|
317
|
+
if not tables:
|
|
318
|
+
return DoctorCheck(
|
|
319
|
+
id="deep.learning_count",
|
|
320
|
+
tier="deep",
|
|
321
|
+
status="healthy",
|
|
322
|
+
severity="info",
|
|
323
|
+
summary="No learnings table yet",
|
|
324
|
+
)
|
|
325
|
+
columns = {
|
|
326
|
+
row[1]
|
|
327
|
+
for row in conn.execute("PRAGMA table_info(learnings)").fetchall()
|
|
328
|
+
}
|
|
329
|
+
if "status" in columns:
|
|
330
|
+
count = conn.execute(
|
|
331
|
+
"SELECT COUNT(*) FROM learnings WHERE COALESCE(status, 'active') != 'archived'"
|
|
332
|
+
).fetchone()[0]
|
|
333
|
+
elif "archived" in columns:
|
|
334
|
+
count = conn.execute(
|
|
335
|
+
"SELECT COUNT(*) FROM learnings WHERE archived=0"
|
|
336
|
+
).fetchone()[0]
|
|
337
|
+
else:
|
|
338
|
+
count = conn.execute("SELECT COUNT(*) FROM learnings").fetchone()[0]
|
|
339
|
+
finally:
|
|
340
|
+
conn.close()
|
|
341
|
+
return DoctorCheck(
|
|
342
|
+
id="deep.learning_count",
|
|
343
|
+
tier="deep",
|
|
344
|
+
status="healthy",
|
|
345
|
+
severity="info",
|
|
346
|
+
summary=f"{count} non-archived learnings in memory",
|
|
347
|
+
)
|
|
348
|
+
except Exception as e:
|
|
349
|
+
return DoctorCheck(
|
|
350
|
+
id="deep.learning_count",
|
|
351
|
+
tier="deep",
|
|
352
|
+
status="degraded",
|
|
353
|
+
severity="warn",
|
|
354
|
+
summary=f"Learning check unreadable: {e}",
|
|
355
|
+
evidence=[str(e)],
|
|
356
|
+
)
|
|
357
|
+
|
|
358
|
+
|
|
359
|
+
def run_deep_checks(fix: bool = False) -> list[DoctorCheck]:
|
|
360
|
+
"""Run all deep-tier checks. Read-only."""
|
|
361
|
+
return [
|
|
362
|
+
safe_check(check_self_audit_summary),
|
|
363
|
+
safe_check(check_schema_version),
|
|
364
|
+
safe_check(check_preflight_summary),
|
|
365
|
+
safe_check(check_watchdog_smoke),
|
|
366
|
+
safe_check(check_learning_count),
|
|
367
|
+
]
|