nexo-brain 2.6.11 → 2.6.13

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.
@@ -11,6 +11,11 @@ import sys
11
11
  import time
12
12
  from pathlib import Path
13
13
 
14
+ from client_preferences import (
15
+ detect_installed_clients,
16
+ normalize_client_preferences,
17
+ resolve_client_runtime_profile,
18
+ )
14
19
  from cron_recovery import should_run_at_load
15
20
  from doctor.models import DoctorCheck
16
21
 
@@ -31,6 +36,7 @@ DEFAULT_CRON_THRESHOLD = 7200 # Fallback when manifest data is unavailable
31
36
  SPECIAL_LAUNCHAGENT_IDS = {"prevent-sleep", "tcc-approve"}
32
37
  SPECIAL_ENV_NORMALIZE_IDS = SPECIAL_LAUNCHAGENT_IDS
33
38
  OPTIONALS_FILE = NEXO_HOME / "config" / "optionals.json"
39
+ SCHEDULE_FILE = NEXO_HOME / "config" / "schedule.json"
34
40
 
35
41
 
36
42
  def _file_age_seconds(path: Path) -> float | None:
@@ -90,6 +96,14 @@ def _enabled_manifest_crons() -> list[dict]:
90
96
  NEXO_CODE / "crons" / "manifest.json",
91
97
  ]
92
98
  optionals = _enabled_optionals()
99
+ automation_default = True
100
+ try:
101
+ if SCHEDULE_FILE.is_file():
102
+ schedule = _load_json(SCHEDULE_FILE)
103
+ if isinstance(schedule, dict):
104
+ automation_default = bool(schedule.get("automation_enabled", True))
105
+ except Exception:
106
+ pass
93
107
  for manifest_path in manifest_candidates:
94
108
  if not manifest_path.is_file():
95
109
  continue
@@ -104,7 +118,11 @@ def _enabled_manifest_crons() -> list[dict]:
104
118
  if not cron_id:
105
119
  continue
106
120
  optional_key = cron.get("optional")
107
- if optional_key and not optionals.get(optional_key, False):
121
+ if optional_key == "automation":
122
+ optional_enabled = optionals.get(optional_key, automation_default)
123
+ else:
124
+ optional_enabled = optionals.get(optional_key, False)
125
+ if optional_key and not optional_enabled:
108
126
  continue
109
127
  enabled.append(cron)
110
128
  return enabled
@@ -875,6 +893,95 @@ def check_personal_script_registry(fix: bool = False) -> DoctorCheck:
875
893
  )
876
894
 
877
895
 
896
+ def check_client_backend_preferences() -> DoctorCheck:
897
+ schedule = {}
898
+ try:
899
+ if SCHEDULE_FILE.is_file():
900
+ schedule = _load_json(SCHEDULE_FILE)
901
+ except Exception:
902
+ schedule = {}
903
+
904
+ prefs = normalize_client_preferences(schedule)
905
+ detected = detect_installed_clients()
906
+
907
+ default_terminal = prefs["default_terminal_client"]
908
+ automation_enabled = bool(prefs["automation_enabled"])
909
+ automation_backend = prefs["automation_backend"]
910
+ default_profile = resolve_client_runtime_profile(default_terminal, preferences=prefs)
911
+ automation_profile = (
912
+ resolve_client_runtime_profile(automation_backend, preferences=prefs)
913
+ if automation_enabled and automation_backend != "none"
914
+ else {"model": "", "reasoning_effort": ""}
915
+ )
916
+
917
+ evidence: list[str] = []
918
+ repair_plan: list[str] = []
919
+ severity = "info"
920
+ status = "healthy"
921
+
922
+ default_info = detected.get(default_terminal, {})
923
+ if not default_info.get("installed"):
924
+ status = "degraded"
925
+ severity = "warn"
926
+ evidence.append(f"default terminal client `{default_terminal}` is selected but not installed")
927
+ repair_plan.append(f"Install {default_terminal} or switch the default terminal client in schedule.json")
928
+
929
+ for client_key, enabled in prefs.get("interactive_clients", {}).items():
930
+ if not enabled:
931
+ continue
932
+ info = detected.get(client_key, {})
933
+ if not info.get("installed"):
934
+ status = "degraded"
935
+ severity = "warn"
936
+ evidence.append(f"interactive client `{client_key}` is enabled but not installed")
937
+
938
+ if automation_enabled:
939
+ backend_info = detected.get(automation_backend, {})
940
+ if automation_backend == "none":
941
+ status = "degraded"
942
+ severity = "warn"
943
+ evidence.append("automation is enabled but no automation backend is configured")
944
+ elif not backend_info.get("installed"):
945
+ status = "degraded"
946
+ severity = "warn"
947
+ evidence.append(f"automation backend `{automation_backend}` is enabled but not installed")
948
+ repair_plan.append(f"Install {automation_backend} or disable automation in schedule.json")
949
+
950
+ if not repair_plan and status != "healthy":
951
+ repair_plan.append("Run `nexo update` or `nexo clients sync` after installing the selected client/backend")
952
+
953
+ def _profile_label(client_key: str, profile: dict[str, str]) -> str:
954
+ bits = [client_key]
955
+ if profile.get("model"):
956
+ bits.append(profile["model"])
957
+ if profile.get("reasoning_effort"):
958
+ bits.append(profile["reasoning_effort"])
959
+ return "/".join(bits)
960
+
961
+ terminal_label = f"chat={_profile_label(default_terminal, default_profile)}"
962
+ automation_label = (
963
+ f"automation={_profile_label(automation_backend, automation_profile)}"
964
+ if automation_enabled and automation_backend != "none"
965
+ else "automation=none"
966
+ )
967
+ return DoctorCheck(
968
+ id="runtime.clients",
969
+ tier="runtime",
970
+ status=status,
971
+ severity=severity,
972
+ summary=f"Client/backend preferences OK ({terminal_label}, {automation_label})" if status == "healthy" else f"Client/backend preferences need attention ({terminal_label}, {automation_label})",
973
+ evidence=evidence or [
974
+ f"default terminal client: {_profile_label(default_terminal, default_profile)}",
975
+ f"automation backend: {_profile_label(automation_backend, automation_profile) if automation_enabled and automation_backend != 'none' else 'none'}",
976
+ ],
977
+ repair_plan=repair_plan,
978
+ escalation_prompt=(
979
+ "The configured interactive client or automation backend is missing. "
980
+ "Align installed clients with schedule.json so `nexo chat` and background automation use the intended tools."
981
+ ) if status != "healthy" else "",
982
+ )
983
+
984
+
878
985
  def run_runtime_checks(fix: bool = False) -> list[DoctorCheck]:
879
986
  """Run all runtime-tier checks. Read-only by default."""
880
987
  return [
@@ -882,6 +989,7 @@ def run_runtime_checks(fix: bool = False) -> list[DoctorCheck]:
882
989
  check_watchdog_status(),
883
990
  check_stale_sessions(),
884
991
  check_cron_freshness(),
992
+ check_client_backend_preferences(),
885
993
  check_launchagent_integrity(fix=fix),
886
994
  check_personal_script_registry(fix=fix),
887
995
  check_skill_health(fix=fix),
@@ -56,21 +56,20 @@ def handle_schedule_status(hours: int = 24, cron_id: str = '') -> str:
56
56
 
57
57
  def handle_schedule_add(cron_id: str, script: str, schedule: str = '',
58
58
  interval_seconds: int = 0, description: str = '',
59
- script_type: str = 'auto') -> str:
59
+ script_type: str = 'auto', keep_alive: bool = False) -> str:
60
60
  """Add a new personal cron job. Generates and installs the LaunchAgent (macOS) or systemd timer (Linux).
61
61
 
62
62
  Args:
63
63
  cron_id: Unique ID for this cron (e.g. 'my-backup', 'report-daily'). Must be lowercase with hyphens.
64
64
  script: Path to the script to run (absolute or relative to NEXO_HOME/scripts/).
65
65
  schedule: Time-based schedule as 'HH:MM' (daily) or 'HH:MM:weekday' (e.g. '08:00:1' for Monday 8AM). Mutually exclusive with interval_seconds.
66
- interval_seconds: Run every N seconds (e.g. 300 for every 5 min). Mutually exclusive with schedule.
66
+ interval_seconds: Run every N seconds (e.g. 300 for every 5 min). Mutually exclusive with schedule/keep_alive.
67
67
  description: What this cron does (for logs and status).
68
68
  script_type: 'auto' (default), 'python', 'shell', 'node', or 'php'.
69
+ keep_alive: Run as a daemon/keep-alive service instead of a timer.
69
70
  """
70
71
  if not cron_id or not script:
71
72
  return "ERROR: cron_id and script are required."
72
- if not schedule and not interval_seconds:
73
- return "ERROR: either schedule (e.g. '08:00') or interval_seconds (e.g. 300) is required."
74
73
 
75
74
  nexo_home = Path(os.environ.get("NEXO_HOME", str(Path.home() / ".nexo")))
76
75
  script_path = Path(script)
@@ -82,6 +81,11 @@ def handle_schedule_add(cron_id: str, script: str, schedule: str = '',
82
81
  script_meta = parse_inline_metadata(script_path)
83
82
  detected_runtime = classify_runtime(script_path, script_meta)
84
83
  declared = get_declared_schedule(script_meta, script_meta.get("name", script_path.stem))
84
+ keep_alive = bool(keep_alive or declared.get("schedule_type") == "keep_alive")
85
+
86
+ if sum(bool(value) for value in [schedule, interval_seconds, keep_alive]) != 1:
87
+ return "ERROR: choose exactly one schedule mode: schedule, interval_seconds, or keep_alive."
88
+
85
89
  script_type = (script_type or "auto").strip().lower()
86
90
  if script_type == "auto":
87
91
  script_type = detected_runtime if detected_runtime != "unknown" else "python"
@@ -102,6 +106,7 @@ def handle_schedule_add(cron_id: str, script: str, schedule: str = '',
102
106
  description or script_meta.get("description", ""),
103
107
  script_type,
104
108
  nexo_home,
109
+ keep_alive=keep_alive,
105
110
  declared=declared,
106
111
  )
107
112
  elif system == "Linux":
@@ -114,6 +119,7 @@ def handle_schedule_add(cron_id: str, script: str, schedule: str = '',
114
119
  description or script_meta.get("description", ""),
115
120
  script_type,
116
121
  nexo_home,
122
+ keep_alive=keep_alive,
117
123
  declared=declared,
118
124
  )
119
125
  else:
@@ -134,7 +140,7 @@ def _runtime_command(script_type: str) -> str:
134
140
  return "python3"
135
141
 
136
142
 
137
- def _register_schedule_metadata(cron_id, script_path, schedule, interval_seconds, description, script_type, label="", plist_path=""):
143
+ def _register_schedule_metadata(cron_id, script_path, schedule, interval_seconds, description, script_type, label="", plist_path="", keep_alive: bool = False):
138
144
  init_db()
139
145
  script_meta = parse_inline_metadata(Path(script_path))
140
146
  runtime = classify_runtime(Path(script_path), script_meta)
@@ -148,7 +154,11 @@ def _register_schedule_metadata(cron_id, script_path, schedule, interval_seconds
148
154
  source="filesystem",
149
155
  has_inline_metadata=bool(script_meta),
150
156
  )
151
- if interval_seconds:
157
+ if keep_alive:
158
+ schedule_type = "keep_alive"
159
+ schedule_value = "true"
160
+ schedule_label = "keep alive"
161
+ elif interval_seconds:
152
162
  schedule_type = "interval"
153
163
  schedule_value = str(interval_seconds)
154
164
  schedule_label = f"every {interval_seconds}s"
@@ -174,7 +184,7 @@ def _register_schedule_metadata(cron_id, script_path, schedule, interval_seconds
174
184
 
175
185
 
176
186
  def _add_launchagent(cron_id, script_path, wrapper_path, schedule, interval_seconds,
177
- description, script_type, nexo_home, *, declared: dict | None = None):
187
+ description, script_type, nexo_home, *, keep_alive: bool = False, declared: dict | None = None):
178
188
  """Create and load a macOS LaunchAgent."""
179
189
  import plistlib
180
190
 
@@ -201,7 +211,9 @@ def _add_launchagent(cron_id, script_path, wrapper_path, schedule, interval_seco
201
211
  },
202
212
  }
203
213
 
204
- if interval_seconds:
214
+ if keep_alive:
215
+ plist["KeepAlive"] = True
216
+ elif interval_seconds:
205
217
  plist["StartInterval"] = interval_seconds
206
218
  elif schedule:
207
219
  parts = schedule.split(":")
@@ -211,7 +223,7 @@ def _add_launchagent(cron_id, script_path, wrapper_path, schedule, interval_seco
211
223
  plist["StartCalendarInterval"] = cal
212
224
 
213
225
  declared = declared or {}
214
- if declared.get("run_on_boot"):
226
+ if declared.get("run_on_boot") or (keep_alive and "run_on_boot" not in declared):
215
227
  plist["RunAtLoad"] = True
216
228
 
217
229
  with open(plist_path, "wb") as f:
@@ -228,13 +240,20 @@ def _add_launchagent(cron_id, script_path, wrapper_path, schedule, interval_seco
228
240
  script_type,
229
241
  label=label,
230
242
  plist_path=str(plist_path),
243
+ keep_alive=keep_alive,
231
244
  )
232
245
 
233
- return f"Cron '{cron_id}' installed at {plist_path} and loaded.{' Schedule: ' + schedule if schedule else f' Interval: {interval_seconds}s'}"
246
+ if keep_alive:
247
+ detail = " KeepAlive daemon"
248
+ elif schedule:
249
+ detail = f" Schedule: {schedule}"
250
+ else:
251
+ detail = f" Interval: {interval_seconds}s"
252
+ return f"Cron '{cron_id}' installed at {plist_path} and loaded.{detail}"
234
253
 
235
254
 
236
255
  def _add_systemd_timer(cron_id, script_path, wrapper_path, schedule, interval_seconds,
237
- description, script_type, nexo_home, *, declared: dict | None = None):
256
+ description, script_type, nexo_home, *, keep_alive: bool = False, declared: dict | None = None):
238
257
  """Create and enable a systemd user timer (Linux)."""
239
258
  unit_dir = Path.home() / ".config" / "systemd" / "user"
240
259
  unit_dir.mkdir(parents=True, exist_ok=True)
@@ -257,9 +276,46 @@ Environment=NEXO_PERSONAL_CRON_ID={cron_id}
257
276
  service_path = unit_dir / f"nexo-{cron_id}.service"
258
277
  service_path.write_text(service_content)
259
278
 
260
- # Timer unit
261
279
  declared = declared or {}
262
280
 
281
+ if keep_alive:
282
+ service_content = f"""[Unit]
283
+ Description=NEXO daemon: {description or cron_id}
284
+
285
+ [Service]
286
+ Type=simple
287
+ ExecStart={exec_cmd}
288
+ Restart=always
289
+ RestartSec=10
290
+ Environment=NEXO_HOME={nexo_home}
291
+ Environment=HOME={Path.home()}
292
+ Environment={PERSONAL_SCHEDULE_MANAGED_ENV}=1
293
+ Environment=NEXO_PERSONAL_CRON_ID={cron_id}
294
+
295
+ [Install]
296
+ WantedBy=default.target
297
+ """
298
+ service_path = unit_dir / f"nexo-{cron_id}.service"
299
+ service_path.write_text(service_content)
300
+
301
+ subprocess.run(["systemctl", "--user", "daemon-reload"], capture_output=True)
302
+ subprocess.run(["systemctl", "--user", "enable", "--now", f"nexo-{cron_id}.service"], capture_output=True)
303
+
304
+ _register_schedule_metadata(
305
+ cron_id,
306
+ script_path,
307
+ schedule,
308
+ interval_seconds,
309
+ description,
310
+ script_type,
311
+ label=f"nexo-{cron_id}",
312
+ plist_path="",
313
+ keep_alive=True,
314
+ )
315
+
316
+ return f"Cron '{cron_id}' installed as KeepAlive systemd service and enabled. Service: {service_path}"
317
+
318
+ # Timer unit
263
319
  if interval_seconds:
264
320
  timer_spec = f"OnUnitActiveSec={interval_seconds}s"
265
321
  if declared.get("run_on_boot") or not declared.get("required"):
@@ -301,6 +357,7 @@ WantedBy=timers.target
301
357
  script_type,
302
358
  label=f"nexo-{cron_id}",
303
359
  plist_path="",
360
+ keep_alive=False,
304
361
  )
305
362
 
306
363
  return f"Cron '{cron_id}' installed as systemd timer and enabled. Service: {service_path}, Timer: {timer_path}"
@@ -588,11 +588,15 @@ def handle_update(remote: str = "origin", branch: str = "main", progress_fn=None
588
588
  try:
589
589
  _emit_progress(progress_fn, "Refreshing shared client configs...")
590
590
  from client_sync import sync_all_clients
591
+ from client_preferences import normalize_client_preferences
591
592
 
593
+ schedule_path = NEXO_HOME / "config" / "schedule.json"
594
+ schedule_payload = json.loads(schedule_path.read_text()) if schedule_path.exists() else {}
592
595
  client_sync_result = sync_all_clients(
593
596
  nexo_home=NEXO_HOME,
594
597
  runtime_root=SRC_DIR,
595
598
  operator_name=os.environ.get("NEXO_NAME", ""),
599
+ preferences=normalize_client_preferences(schedule_payload),
596
600
  )
597
601
  if client_sync_result.get("ok"):
598
602
  steps_done.append("client-sync")
@@ -619,7 +623,7 @@ def handle_update(remote: str = "origin", branch: str = "main", progress_fn=None
619
623
  if "hook-sync" in steps_done:
620
624
  lines.append(" Hooks: synced to NEXO_HOME")
621
625
  if "client-sync" in steps_done:
622
- lines.append(" Clients: Claude Code/Desktop/Codex synced")
626
+ lines.append(" Clients: configured client targets synced")
623
627
  lines.append("")
624
628
  lines.append("MCP server restart needed to load new code.")
625
629
  return "\n".join(lines)
@@ -71,6 +71,29 @@ def _schedule_defaults() -> dict:
71
71
  return {
72
72
  "timezone": "UTC",
73
73
  "auto_update": True,
74
+ "interactive_clients": {
75
+ "claude_code": True,
76
+ "codex": False,
77
+ "claude_desktop": False,
78
+ },
79
+ "default_terminal_client": "claude_code",
80
+ "automation_enabled": True,
81
+ "automation_backend": "claude_code",
82
+ "client_runtime_profiles": {
83
+ "claude_code": {
84
+ "model": "opus",
85
+ "reasoning_effort": "",
86
+ },
87
+ "codex": {
88
+ "model": "gpt-5.4",
89
+ "reasoning_effort": "xhigh",
90
+ },
91
+ },
92
+ "client_install_preferences": {
93
+ "claude_code": "ask",
94
+ "codex": "ask",
95
+ "claude_desktop": "manual",
96
+ },
74
97
  POWER_POLICY_KEY: POWER_POLICY_UNSET,
75
98
  POWER_POLICY_VERSION_KEY: POWER_POLICY_VERSION,
76
99
  FULL_DISK_ACCESS_STATUS_KEY: FULL_DISK_ACCESS_UNSET,
@@ -32,6 +32,16 @@ _IGNORED_FILES = {
32
32
  }
33
33
  _IGNORED_DIRS = {"deep-sleep", "__pycache__"}
34
34
 
35
+ _LEGACY_WAKE_RECOVERY_METADATA = [
36
+ "# nexo: name=nexo-wake-recovery",
37
+ "# nexo: description=Recover interval LaunchAgents after macOS sleep/wake gaps",
38
+ "# nexo: runtime=shell",
39
+ "# nexo: cron_id=wake-recovery",
40
+ "# nexo: schedule_required=true",
41
+ "# nexo: recovery_policy=restart_daemon",
42
+ "# nexo: run_on_boot=true",
43
+ ]
44
+
35
45
  # Forbidden patterns — direct DB access from personal scripts
36
46
  _FORBIDDEN_PATTERNS = [
37
47
  re.compile(r"\bsqlite3\b"),
@@ -77,6 +87,33 @@ def get_scripts_dir() -> Path:
77
87
  return NEXO_HOME / "scripts"
78
88
 
79
89
 
90
+ def _apply_legacy_personal_script_backfills() -> None:
91
+ """Backfill metadata for known legacy personal scripts shipped before the registry existed."""
92
+ scripts_dir = get_scripts_dir()
93
+ wake_recovery = scripts_dir / "nexo-wake-recovery.sh"
94
+ if not wake_recovery.is_file():
95
+ return
96
+
97
+ try:
98
+ text = wake_recovery.read_text()
99
+ except Exception:
100
+ return
101
+
102
+ if "# nexo:" in "\n".join(text.splitlines()[:25]):
103
+ return
104
+ if "Wake Recovery" not in text:
105
+ return
106
+
107
+ lines = text.splitlines(keepends=True)
108
+ head: list[str] = []
109
+ start = 0
110
+ if lines and lines[0].startswith("#!"):
111
+ head.append(lines[0])
112
+ start = 1
113
+ head.extend([line + "\n" for line in _LEGACY_WAKE_RECOVERY_METADATA])
114
+ wake_recovery.write_text("".join(head + lines[start:]))
115
+
116
+
80
117
  def load_core_script_names() -> set[str]:
81
118
  """Load script names from crons/manifest.json (these are core, not personal)."""
82
119
  names: set[str] = set()
@@ -279,6 +316,11 @@ def get_declared_schedule(metadata: dict, default_name: str = "") -> dict:
279
316
  "cron_id": cron_id,
280
317
  }
281
318
 
319
+ def _effective_run_on_boot(policy: str) -> bool:
320
+ if "run_on_boot" in metadata:
321
+ return run_on_boot
322
+ return policy == "restart_daemon"
323
+
282
324
  def _effective_run_on_wake(policy: str) -> bool:
283
325
  if "run_on_wake" in metadata:
284
326
  return run_on_wake
@@ -379,12 +421,29 @@ def get_declared_schedule(metadata: dict, default_name: str = "") -> dict:
379
421
  "schedule": schedule_raw,
380
422
  "interval_seconds": 0,
381
423
  "recovery_policy": recovery_policy_raw or "catchup",
382
- "run_on_boot": run_on_boot,
424
+ "run_on_boot": _effective_run_on_boot(recovery_policy_raw or "catchup"),
383
425
  "run_on_wake": _effective_run_on_wake(recovery_policy_raw or "catchup"),
384
426
  "idempotent": _effective_idempotent(recovery_policy_raw or "catchup"),
385
427
  "max_catchup_age": max_catchup_age or (14 * 86400 if weekday is not None else 48 * 3600),
386
428
  }
387
429
 
430
+ if required and recovery_policy_raw == "restart_daemon":
431
+ return {
432
+ "required": required,
433
+ "valid": True,
434
+ "cron_id": cron_id,
435
+ "schedule_type": "keep_alive",
436
+ "schedule_value": "true",
437
+ "schedule_label": "keep alive",
438
+ "schedule": "",
439
+ "interval_seconds": 0,
440
+ "recovery_policy": "restart_daemon",
441
+ "run_on_boot": _effective_run_on_boot("restart_daemon"),
442
+ "run_on_wake": _effective_run_on_wake("restart_daemon"),
443
+ "idempotent": _effective_idempotent("restart_daemon"),
444
+ "max_catchup_age": max_catchup_age,
445
+ }
446
+
388
447
  return {
389
448
  "required": required,
390
449
  "valid": not required,
@@ -416,6 +475,7 @@ def _script_entry(path: Path, meta: dict, *, is_core: bool, classification: str,
416
475
 
417
476
  def classify_scripts_dir() -> dict:
418
477
  """Classify every file in NEXO_HOME/scripts into personal/core/ignored/non-script buckets."""
478
+ _apply_legacy_personal_script_backfills()
419
479
  scripts_dir = get_scripts_dir()
420
480
  if not scripts_dir.is_dir():
421
481
  return {"scripts_dir": str(scripts_dir), "entries": [], "summary": {}}
@@ -927,6 +987,7 @@ def ensure_personal_schedules(*, dry_run: bool = False) -> dict:
927
987
  interval_seconds=declared.get("interval_seconds", 0),
928
988
  description=script.get("description", ""),
929
989
  script_type=script.get("runtime", "auto"),
990
+ keep_alive=declared.get("schedule_type") == "keep_alive",
930
991
  )
931
992
  target = report["repaired" if existing else "created"]
932
993
  target.append({