nexo-brain 5.3.5 → 5.3.7

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.
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "nexo-brain",
3
- "version": "5.3.5",
3
+ "version": "5.3.7",
4
4
  "description": "Local cognitive runtime for Claude Code \u2014 persistent memory, overnight learning, doctor diagnostics, personal scripts, recovery-aware jobs, startup preflight, and optional dashboard/power helper.",
5
5
  "author": {
6
6
  "name": "NEXO Brain",
package/README.md CHANGED
@@ -89,7 +89,7 @@ Versions `3.1.7` through `3.2.0` close the recent-memory gap:
89
89
  - when even that misses, NEXO now exposes raw transcript fallback tools for Claude Code and Codex session stores
90
90
  - NEXO can now inspect itself through a live system catalog derived from canonical sources instead of relying only on stale docs or operator memory
91
91
 
92
- Version `5.3.5` keeps CLI version visibility honest right after `nexo update`: if the cached npm version lags behind the runtime you just installed, `nexo` / `nexo chat` now clamp `Latest` to the installed version and refresh the cache instead of showing a stale older release. Version `5.3.4` already cleaned up legacy core alias leakage and added the version-status banner. Version `5.3.3` closed the remaining packaged-runtime doctor mismatch: the built-in hourly backup helper is now inventoried as a core LaunchAgent, so clean installs no longer get a false unknown-LaunchAgent warning. Version `5.3.2` already hardened the runtime boundary by persisting which runtime scripts/hooks are core product artifacts, keeping `nexo scripts` from mixing those into the personal bucket, and migrating the legacy Claude Code heartbeat wrappers into managed core hooks.
92
+ Version `5.3.7` closes the remaining packaged-runtime happy-path gap and finally exposes portable user-data migration commands: packaged `nexo update` now self-heals cron definitions and LaunchAgents after a successful npm bump, new `nexo export` / `nexo import` commands move operator data as a safe bundle instead of leaving that flow implicit, and runtime doctor now distinguishes tracked historical Codex drift from an actually broken runtime so cleaned installs stop staying red for stale transcript debt alone. Version `5.3.6` hardened the Claude Code bootstrap path and related runtime hygiene: managed client sync now writes the NEXO MCP server where current Claude Code actually reads it (`~/.claude.json`), script classification is stricter about core-vs-personal runtime artifacts, schedule status distinguishes genuinely running jobs from broken ones, and retroactive learnings stop opening keyword-only false positives outside their declared `applies_to` scope. Version `5.3.5` already keeps CLI version visibility honest right after `nexo update`: if the cached npm version lags behind the runtime you just installed, `nexo` / `nexo chat` now clamp `Latest` to the installed version and refresh the cache instead of showing a stale older release. Version `5.3.4` already cleaned up legacy core alias leakage and added the version-status banner. Version `5.3.3` closed the remaining packaged-runtime doctor mismatch: the built-in hourly backup helper is now inventoried as a core LaunchAgent, so clean installs no longer get a false unknown-LaunchAgent warning. Version `5.3.2` already hardened the runtime boundary by persisting which runtime scripts/hooks are core product artifacts, keeping `nexo scripts` from mixing those into the personal bucket, and migrating the legacy Claude Code heartbeat wrappers into managed core hooks.
93
93
 
94
94
  Version `5.3.1` normalizes packaged npm installs so they behave like packaged npm installs: `nexo update` now keeps the runtime anchored to `~/.nexo`, refreshes packaged bootstrap/client artifacts after upgrade, avoids repo-only release-artifact drift in installed runtimes, and keeps personal scripts on the canonical packaged path.
95
95
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "nexo-brain",
3
- "version": "5.3.5",
3
+ "version": "5.3.7",
4
4
  "mcpName": "io.github.wazionapps/nexo",
5
5
  "description": "NEXO Brain — Shared brain for AI agents. Persistent memory, semantic RAG, natural forgetting, metacognitive guard, trust scoring, 150+ MCP tools. Works with Claude Code, Codex, Claude Desktop & any MCP client. 100% local, free.",
6
6
  "homepage": "https://nexo-brain.com",
package/src/cli.py CHANGED
@@ -3,6 +3,8 @@
3
3
 
4
4
  Entry points:
5
5
  nexo chat [PATH]
6
+ nexo export [PATH] [--json]
7
+ nexo import PATH [--json]
6
8
  nexo scripts list [--all] [--json]
7
9
  nexo scripts create NAME [--runtime python|shell] [--description TEXT]
8
10
  nexo scripts classify [--json]
@@ -558,6 +560,43 @@ def _scripts_doctor(args):
558
560
  return 0
559
561
 
560
562
 
563
+ def _export_bundle(args):
564
+ from user_data_portability import export_user_bundle
565
+
566
+ result = export_user_bundle(args.path or "")
567
+ if args.json:
568
+ print(json.dumps(result, indent=2, ensure_ascii=False))
569
+ else:
570
+ if not result.get("ok"):
571
+ print(result.get("error", "Export failed"), file=sys.stderr)
572
+ return 1
573
+ sections = result.get("sections", {})
574
+ script_count = sections.get("personal_scripts", {}).get("files", 0)
575
+ print(f"User data export written to {result['path']}")
576
+ print(f" Personal scripts: {script_count}")
577
+ print(f" Sections: {', '.join(sorted(sections))}")
578
+ return 0 if result.get("ok") else 1
579
+
580
+
581
+ def _import_bundle(args):
582
+ from user_data_portability import import_user_bundle
583
+
584
+ result = import_user_bundle(args.path)
585
+ if args.json:
586
+ print(json.dumps(result, indent=2, ensure_ascii=False))
587
+ else:
588
+ if not result.get("ok"):
589
+ print(result.get("error", "Import failed"), file=sys.stderr)
590
+ return 1
591
+ restored = result.get("restored", {})
592
+ script_count = restored.get("personal_scripts", {}).get("files", 0)
593
+ print(f"User data imported from {result['path']}")
594
+ print(f" Safety backup: {result['safety_backup']}")
595
+ print(f" Personal scripts restored: {script_count}")
596
+ print(f" Sections: {', '.join(sorted(restored))}")
597
+ return 0 if result.get("ok") else 1
598
+
599
+
561
600
  def _runtime_python_candidates() -> list[str]:
562
601
  candidates: list[str] = []
563
602
  seen: set[str] = set()
@@ -1588,6 +1627,8 @@ def _print_help():
1588
1627
 
1589
1628
  Commands:
1590
1629
  nexo chat [path] [--client claude_code|codex] Launch a NEXO terminal client
1630
+ nexo export [path] Export a portable user-data bundle
1631
+ nexo import PATH Import a portable user-data bundle
1591
1632
  nexo doctor [--tier boot|runtime|deep|all] [--fix] System diagnostics
1592
1633
  nexo scripts list|create|classify|sync|reconcile|ensure-schedules|schedules|run|doctor|call|unschedule|remove
1593
1634
  Personal scripts
@@ -1618,6 +1659,16 @@ def main():
1618
1659
  help="Override the chat picker and launch a specific terminal client",
1619
1660
  )
1620
1661
 
1662
+ # -- export --
1663
+ export_parser = sub.add_parser("export", help="Export a portable user-data bundle")
1664
+ export_parser.add_argument("path", nargs="?", default="", help="Output bundle path (default: NEXO_HOME/exports/...)")
1665
+ export_parser.add_argument("--json", action="store_true", help="JSON output")
1666
+
1667
+ # -- import --
1668
+ import_parser = sub.add_parser("import", help="Import a portable user-data bundle")
1669
+ import_parser.add_argument("path", help="Bundle path created by `nexo export`")
1670
+ import_parser.add_argument("--json", action="store_true", help="JSON output")
1671
+
1621
1672
  # -- scripts --
1622
1673
  scripts_parser = sub.add_parser("scripts", help="Manage personal scripts")
1623
1674
  scripts_sub = scripts_parser.add_subparsers(dest="scripts_command")
@@ -1828,6 +1879,10 @@ def main():
1828
1879
  return 0
1829
1880
  elif args.command == "chat":
1830
1881
  return _chat(args)
1882
+ elif args.command == "export":
1883
+ return _export_bundle(args)
1884
+ elif args.command == "import":
1885
+ return _import_bundle(args)
1831
1886
  elif args.command == "update":
1832
1887
  return _update(args)
1833
1888
  elif args.command == "clients":
@@ -160,6 +160,11 @@ def _claude_code_settings_path(home: Path | None = None) -> Path:
160
160
  return base / ".claude" / "settings.json"
161
161
 
162
162
 
163
+ def _claude_code_mcp_path(home: Path | None = None) -> Path:
164
+ base = home or _user_home()
165
+ return base / ".claude.json"
166
+
167
+
163
168
  def _claude_desktop_config_path(home: Path | None = None) -> Path:
164
169
  base = home or _user_home()
165
170
  if sys.platform == "darwin":
@@ -627,10 +632,22 @@ def sync_claude_code(
627
632
  python_path=python_path,
628
633
  operator_name=operator_name,
629
634
  )
635
+ home_path = Path(user_home).expanduser() if user_home else None
630
636
  result = _sync_claude_code_settings(
631
- _claude_code_settings_path(Path(user_home).expanduser() if user_home else None),
637
+ _claude_code_settings_path(home_path),
632
638
  server_config,
633
639
  )
640
+ # Claude Code 2.1.x reads user-scoped MCP servers from ~/.claude.json.
641
+ # Keep settings.json in sync for hooks/runtime preferences, but also write
642
+ # the managed NEXO MCP server to the root user config so `claude mcp list`
643
+ # and interactive sessions see the same server.
644
+ mcp_result = _sync_json_client(
645
+ _claude_code_mcp_path(home_path),
646
+ server_config,
647
+ "claude_code",
648
+ )
649
+ result["mcp"] = mcp_result
650
+ result["mcp_path"] = mcp_result.get("path", "")
634
651
  bootstrap_result = sync_client_bootstrap(
635
652
  "claude_code",
636
653
  nexo_home=nexo_home,
@@ -56,14 +56,19 @@ def cron_runs_summary(hours: int = 24) -> list[dict]:
56
56
  cron_id,
57
57
  COUNT(*) as total_runs,
58
58
  SUM(CASE WHEN exit_code = 0 THEN 1 ELSE 0 END) as succeeded,
59
- SUM(CASE WHEN exit_code != 0 OR exit_code IS NULL THEN 1 ELSE 0 END) as failed,
59
+ SUM(CASE WHEN exit_code IS NOT NULL AND ended_at IS NOT NULL THEN 1 ELSE 0 END) as completed_runs,
60
+ SUM(CASE WHEN exit_code IS NOT NULL AND exit_code != 0 THEN 1 ELSE 0 END) as failed,
61
+ SUM(CASE WHEN exit_code IS NULL OR ended_at IS NULL THEN 1 ELSE 0 END) as open_runs,
60
62
  ROUND(AVG(duration_secs), 1) as avg_duration,
61
63
  MAX(started_at) as last_run,
62
64
  (SELECT exit_code FROM cron_runs cr2
63
65
  WHERE cr2.cron_id = cron_runs.cron_id
64
66
  ORDER BY started_at DESC LIMIT 1) as last_exit_code,
65
- (SELECT summary FROM cron_runs cr3
66
- WHERE cr3.cron_id = cron_runs.cron_id AND cr3.summary != ''
67
+ (SELECT ended_at FROM cron_runs cr3
68
+ WHERE cr3.cron_id = cron_runs.cron_id
69
+ ORDER BY started_at DESC LIMIT 1) as last_ended_at,
70
+ (SELECT summary FROM cron_runs cr4
71
+ WHERE cr4.cron_id = cron_runs.cron_id AND cr4.summary != ''
67
72
  ORDER BY started_at DESC LIMIT 1) as last_summary
68
73
  FROM cron_runs
69
74
  WHERE started_at >= datetime('now', ?)
@@ -2157,10 +2157,8 @@ def check_codex_conditioned_file_discipline() -> DoctorCheck:
2157
2157
  and audit["delete_without_protocol"] == 0
2158
2158
  and audit["delete_without_guard_ack"] == 0
2159
2159
  )
2160
- historical_write_drift = (
2160
+ tracked_write_without_open_debt = (
2161
2161
  no_open_conditioned_debt
2162
- and audit.get("latest_violation_age_seconds") is not None
2163
- and float(audit["latest_violation_age_seconds"]) >= 172800
2164
2162
  and audit["write_without_protocol"] > 0
2165
2163
  and audit["write_without_guard_ack"] == 0
2166
2164
  and audit["delete_without_protocol"] == 0
@@ -2168,7 +2166,7 @@ def check_codex_conditioned_file_discipline() -> DoctorCheck:
2168
2166
  )
2169
2167
 
2170
2168
  if audit["write_without_protocol"] or audit["write_without_guard_ack"]:
2171
- if historical_write_drift:
2169
+ if tracked_write_without_open_debt:
2172
2170
  status = "healthy"
2173
2171
  severity = "info"
2174
2172
  else:
@@ -2191,7 +2189,9 @@ def check_codex_conditioned_file_discipline() -> DoctorCheck:
2191
2189
  severity=severity,
2192
2190
  summary=(
2193
2191
  "Historical Codex conditioned-file drift has no open protocol debt"
2194
- if historical_read_only or historical_write_drift
2192
+ if historical_read_only
2193
+ else "Tracked Codex conditioned-file drift has no open protocol debt"
2194
+ if tracked_write_without_open_debt
2195
2195
  else "Recent Codex sessions respect conditioned-file discipline"
2196
2196
  if status == "healthy"
2197
2197
  else "Recent Codex sessions are bypassing conditioned-file discipline"
@@ -1,5 +1,6 @@
1
1
  """NEXO Schedule — Cron execution history, status, and management tools."""
2
2
 
3
+ from datetime import datetime, timezone
3
4
  import json
4
5
  import os
5
6
  import platform
@@ -34,7 +35,13 @@ def handle_schedule_status(hours: int = 24, cron_id: str = '') -> str:
34
35
  schedule_meta = get_personal_script_schedule(cron_id) or {}
35
36
  lines = [f"CRON RUNS — {cron_id} (last {hours}h): {len(runs)} executions"]
36
37
  for r in runs:
37
- status, detail = _run_status_marker(r.get("exit_code"), r.get("summary"), schedule_meta=schedule_meta)
38
+ status, detail = _run_status_marker(
39
+ r.get("exit_code"),
40
+ r.get("summary"),
41
+ schedule_meta=schedule_meta,
42
+ started_at=r.get("started_at"),
43
+ ended_at=r.get("ended_at"),
44
+ )
38
45
  if schedule_meta.get("schedule_type") == "keep_alive" and r.get("exit_code") is None:
39
46
  dur = "daemon active"
40
47
  else:
@@ -53,11 +60,20 @@ def handle_schedule_status(hours: int = 24, cron_id: str = '') -> str:
53
60
  lines = [f"CRON STATUS (last {hours}h):"]
54
61
  for s in summary:
55
62
  schedule_meta = get_personal_script_schedule(s["cron_id"]) or {}
56
- status, detail = _run_status_marker(s.get("last_exit_code"), s.get("last_summary"), schedule_meta=schedule_meta)
63
+ status, detail = _run_status_marker(
64
+ s.get("last_exit_code"),
65
+ s.get("last_summary"),
66
+ schedule_meta=schedule_meta,
67
+ started_at=s.get("last_run"),
68
+ ended_at=s.get("last_ended_at"),
69
+ )
57
70
  if schedule_meta.get("schedule_type") == "keep_alive" and s.get("last_exit_code") is None:
58
71
  rate = "daemon active"
59
72
  else:
60
- rate = f"{s['succeeded']}/{s['total_runs']}"
73
+ completed_runs = s.get("completed_runs")
74
+ if completed_runs is None:
75
+ completed_runs = s["total_runs"]
76
+ rate = f"{s['succeeded']}/{completed_runs}"
61
77
  dur = f"{s['avg_duration']:.0f}s avg" if s.get("avg_duration") else ""
62
78
  summary_txt = f" — {s['last_summary'][:80]}" if s.get("last_summary") else ""
63
79
  suffix = f" [{detail}]" if detail else ""
@@ -87,16 +103,78 @@ def _summary_has_warning(summary: str = "") -> bool:
87
103
  return any(token in lowered for token in warning_tokens)
88
104
 
89
105
 
90
- def _run_status_marker(exit_code, summary: str = "", *, schedule_meta: dict | None = None) -> tuple[str, str]:
106
+ def _now_utc() -> datetime:
107
+ return datetime.now(timezone.utc)
108
+
109
+
110
+ def _parse_db_timestamp(value: str | None) -> datetime | None:
111
+ if not value:
112
+ return None
113
+ text = str(value).strip()
114
+ if not text:
115
+ return None
116
+ try:
117
+ return datetime.strptime(text[:19], "%Y-%m-%d %H:%M:%S").replace(tzinfo=timezone.utc)
118
+ except ValueError:
119
+ pass
120
+ try:
121
+ parsed = datetime.fromisoformat(text.replace("Z", "+00:00"))
122
+ except ValueError:
123
+ return None
124
+ if parsed.tzinfo is None:
125
+ parsed = parsed.replace(tzinfo=timezone.utc)
126
+ return parsed.astimezone(timezone.utc)
127
+
128
+
129
+ def _format_age(seconds: float) -> str:
130
+ if seconds < 60:
131
+ return "<1m"
132
+ if seconds < 3600:
133
+ return f"{int(round(seconds / 60))}m"
134
+ return f"{seconds / 3600:.1f}h"
135
+
136
+
137
+ def _open_run_stale_after(schedule_meta: dict | None = None) -> int:
138
+ schedule_meta = schedule_meta or {}
139
+ if schedule_meta.get("schedule_type") == "interval":
140
+ try:
141
+ interval = int(schedule_meta.get("interval_seconds") or schedule_meta.get("schedule_value") or 0)
142
+ except (TypeError, ValueError):
143
+ interval = 0
144
+ if interval > 0:
145
+ return max(300, interval * 2)
146
+ if schedule_meta.get("schedule_type") == "calendar":
147
+ return 3600
148
+ return 1800
149
+
150
+
151
+ def _open_run_marker(started_at: str | None, *, schedule_meta: dict | None = None) -> tuple[str, str]:
152
+ started = _parse_db_timestamp(started_at)
153
+ if started is None:
154
+ return "⚠", "open run"
155
+ age_secs = max(0.0, (_now_utc() - started).total_seconds())
156
+ if age_secs <= _open_run_stale_after(schedule_meta):
157
+ return "⏳", f"running {_format_age(age_secs)}"
158
+ return "⚠", f"open run {_format_age(age_secs)}"
159
+
160
+
161
+ def _run_status_marker(
162
+ exit_code,
163
+ summary: str = "",
164
+ *,
165
+ schedule_meta: dict | None = None,
166
+ started_at: str | None = None,
167
+ ended_at: str | None = None,
168
+ ) -> tuple[str, str]:
91
169
  schedule_meta = schedule_meta or {}
92
170
  if schedule_meta.get("schedule_type") == "keep_alive" and exit_code is None:
93
171
  return "🟢", "keep_alive daemon active"
172
+ if exit_code is None:
173
+ return _open_run_marker(started_at, schedule_meta=schedule_meta)
94
174
  if exit_code == 0 and _summary_has_warning(summary):
95
175
  return "⚠", "exit 0 with warnings"
96
176
  if exit_code == 0:
97
177
  return "✅", "exit 0"
98
- if exit_code is None:
99
- return "❌", "missing exit code"
100
178
  return "❌", f"exit {exit_code}"
101
179
 
102
180
 
@@ -89,7 +89,10 @@ def _refresh_installed_manifest():
89
89
  dst_crons.mkdir(parents=True, exist_ok=True)
90
90
  for f in src_crons.iterdir():
91
91
  if f.is_file():
92
- shutil.copy2(str(f), str(dst_crons / f.name))
92
+ dest = dst_crons / f.name
93
+ if _paths_match(f, dest):
94
+ continue
95
+ shutil.copy2(str(f), str(dest))
93
96
  config_dir = NEXO_HOME / "config"
94
97
  config_dir.mkdir(parents=True, exist_ok=True)
95
98
  payload = {
@@ -355,7 +358,8 @@ def _sync_hooks_to_home():
355
358
  for f in hooks_src.iterdir():
356
359
  if f.is_file() and f.suffix == ".sh":
357
360
  dest = hooks_dest / f.name
358
- shutil.copy2(str(f), str(dest))
361
+ if not _paths_match(f, dest):
362
+ shutil.copy2(str(f), str(dest))
359
363
  os.chmod(str(dest), 0o755)
360
364
  synced += 1
361
365
  if synced:
@@ -499,6 +503,93 @@ def _emit_progress(progress_fn, message: str) -> None:
499
503
  pass
500
504
 
501
505
 
506
+ def _paths_match(src: Path, dest: Path) -> bool:
507
+ try:
508
+ return src.exists() and dest.exists() and src.samefile(dest)
509
+ except Exception:
510
+ return False
511
+
512
+
513
+ def _sync_packaged_crons(progress_fn=None) -> tuple[bool, str | None]:
514
+ sync_path = NEXO_HOME / "crons" / "sync.py"
515
+ if not sync_path.is_file():
516
+ _refresh_installed_manifest()
517
+ return True, None
518
+ try:
519
+ _emit_progress(progress_fn, "Syncing core cron definitions...")
520
+ result = subprocess.run(
521
+ [sys.executable, str(sync_path)],
522
+ cwd=str(NEXO_HOME),
523
+ capture_output=True,
524
+ text=True,
525
+ timeout=30,
526
+ env={**os.environ, "NEXO_HOME": str(NEXO_HOME), "NEXO_CODE": str(NEXO_HOME)},
527
+ )
528
+ if result.returncode != 0:
529
+ return False, result.stderr.strip() or result.stdout.strip() or "cron sync failed"
530
+ _refresh_installed_manifest()
531
+ return True, None
532
+ except Exception as e:
533
+ return False, f"cron sync error: {e}"
534
+
535
+
536
+ def _reload_launch_agents_after_bump() -> dict:
537
+ result: dict = {
538
+ "scanned": 0,
539
+ "reloaded": 0,
540
+ "skipped_missing": 0,
541
+ "errors": [],
542
+ "platform": sys.platform,
543
+ }
544
+
545
+ if sys.platform != "darwin":
546
+ return result
547
+
548
+ launch_agents_dir = Path.home() / "Library" / "LaunchAgents"
549
+ if not launch_agents_dir.is_dir():
550
+ return result
551
+
552
+ try:
553
+ plists = sorted(launch_agents_dir.glob("com.nexo.*.plist"))
554
+ except Exception as e:
555
+ result["errors"].append({"plist": "*", "stderr": f"glob failed: {e}"})
556
+ return result
557
+
558
+ result["scanned"] = len(plists)
559
+ for plist in plists:
560
+ try:
561
+ if not plist.is_file():
562
+ result["skipped_missing"] += 1
563
+ continue
564
+ subprocess.run(
565
+ ["launchctl", "unload", str(plist)],
566
+ capture_output=True,
567
+ text=True,
568
+ timeout=10,
569
+ )
570
+ load_proc = subprocess.run(
571
+ ["launchctl", "load", "-w", str(plist)],
572
+ capture_output=True,
573
+ text=True,
574
+ timeout=10,
575
+ )
576
+ if load_proc.returncode == 0:
577
+ result["reloaded"] += 1
578
+ else:
579
+ result["errors"].append(
580
+ {
581
+ "plist": plist.name,
582
+ "stderr": (load_proc.stderr or load_proc.stdout or "load failed")[:300],
583
+ }
584
+ )
585
+ except subprocess.TimeoutExpired:
586
+ result["errors"].append({"plist": plist.name, "stderr": "launchctl timeout"})
587
+ except Exception as e:
588
+ result["errors"].append({"plist": plist.name, "stderr": str(e)[:300]})
589
+
590
+ return result
591
+
592
+
502
593
  def _handle_packaged_update(progress_fn=None) -> str:
503
594
  """Update a packaged (npm) install — no git repo available."""
504
595
  old_version = _read_version()
@@ -581,10 +672,16 @@ def _handle_packaged_update(progress_fn=None) -> str:
581
672
  errors.append(f"verification: {verify_err}")
582
673
 
583
674
  hook_sync_warning = None
675
+ cron_sync_warning = None
584
676
  retired_runtime_files: list[str] = []
677
+ launchagent_reload_warning = None
678
+ launchagent_reload_summary = None
679
+ cron_sync_ok, cron_sync_error = _sync_packaged_crons(progress_fn=progress_fn)
680
+ if not cron_sync_ok:
681
+ errors.append(f"cron sync: {cron_sync_error}")
682
+ cron_sync_warning = cron_sync_error
585
683
  try:
586
684
  _emit_progress(progress_fn, "Refreshing installed hooks and manifests...")
587
- _refresh_installed_manifest()
588
685
  _sync_hooks_to_home()
589
686
  retired_runtime_files = _cleanup_retired_runtime_files()
590
687
  except Exception as e:
@@ -596,6 +693,19 @@ def _handle_packaged_update(progress_fn=None) -> str:
596
693
  if not clients_ok:
597
694
  client_sync_warning = client_sync_error or "unknown client sync error"
598
695
 
696
+ if old_version != new_version:
697
+ _emit_progress(progress_fn, "Reloading LaunchAgents after version bump...")
698
+ try:
699
+ launchagent_reload_summary = _reload_launch_agents_after_bump()
700
+ if launchagent_reload_summary.get("errors"):
701
+ launchagent_reload_warning = (
702
+ f"reloaded {launchagent_reload_summary['reloaded']}/"
703
+ f"{launchagent_reload_summary['scanned']} with "
704
+ f"{len(launchagent_reload_summary['errors'])} error(s)"
705
+ )
706
+ except Exception as e:
707
+ launchagent_reload_warning = f"launchagent reload error: {e}"
708
+
599
709
  if errors:
600
710
  # 5. Full rollback: restore code tree + DBs + pip deps + rollback npm package
601
711
  if code_backup_dir:
@@ -632,6 +742,10 @@ def _handle_packaged_update(progress_fn=None) -> str:
632
742
  lines = ["UPDATE SUCCESSFUL (packaged install)"]
633
743
  lines.append(f" Version: {old_version} -> {new_version}")
634
744
  lines.append(f" Backup: {backup_dir}")
745
+ if not cron_sync_warning:
746
+ lines.append(" Crons: synced with manifest")
747
+ else:
748
+ lines.append(f" WARNING: cron sync: {cron_sync_warning}")
635
749
  if not hook_sync_warning:
636
750
  lines.append(" Hooks: synced to NEXO_HOME")
637
751
  else:
@@ -642,6 +756,15 @@ def _handle_packaged_update(progress_fn=None) -> str:
642
756
  lines.append(" Clients: configured client targets synced")
643
757
  else:
644
758
  lines.append(f" WARNING: client sync: {client_sync_warning}")
759
+ if launchagent_reload_summary and launchagent_reload_summary.get("scanned"):
760
+ if not launchagent_reload_warning:
761
+ lines.append(
762
+ " LaunchAgents: reloaded "
763
+ f"{launchagent_reload_summary['reloaded']}/"
764
+ f"{launchagent_reload_summary['scanned']}"
765
+ )
766
+ else:
767
+ lines.append(f" WARNING: launchagent reload: {launchagent_reload_warning}")
645
768
  lines.append("")
646
769
  lines.append("MCP server restart needed to load new code.")
647
770
  return "\n".join(lines)
@@ -29,7 +29,10 @@ Matching strategy:
29
29
  with significant tokens from the decision's
30
30
  decision + based_on + alternatives + context_ref. Score is
31
31
  intersection_size / max(1, learning_token_count) clipped to 1.0.
32
- Combined score: 0.5 * applies_to_score + 0.5 * keyword_score.
32
+ Guardrail: if a learning defines `applies_to` and that anchor scores
33
+ below 0.3, auto-dismiss the match even if keyword overlap is high.
34
+ This blocks keyword-only false positives outside the learning's
35
+ actual blast radius.
33
36
  Default match threshold: 0.4. Default cap: 5 matches per learning.
34
37
 
35
38
  Anti-spam guards:
@@ -129,16 +132,16 @@ def _score_match(
129
132
  keyword_hits = set()
130
133
  keyword_score = 0.0
131
134
 
132
- # max() rather than weighted average so a strong signal alone qualifies.
133
- # The two signals (applies_to overlap, keyword overlap) are independent
134
- # paths to the same conclusion: this past decision is in the new rule's
135
- # blast radius. If either one fires strongly, surface the match.
136
- score = max(applies_to_score, keyword_score)
135
+ gated_by_applies_to = bool(learning_applies_to and applies_to_score < 0.3)
136
+ # When a learning explicitly scopes its blast radius via applies_to,
137
+ # keyword overlap alone is too noisy to justify a retroactive review.
138
+ score = applies_to_score if gated_by_applies_to else max(applies_to_score, keyword_score)
137
139
  breakdown = {
138
140
  "applies_to_score": round(applies_to_score, 3),
139
141
  "applies_to_hits": sorted(applies_to_hits),
140
142
  "keyword_score": round(keyword_score, 3),
141
143
  "keyword_hits": sorted(keyword_hits),
144
+ "gated_by_applies_to": gated_by_applies_to,
142
145
  }
143
146
  return round(score, 3), breakdown
144
147
 
@@ -93,6 +93,12 @@ SUPPORTED_RUNTIMES = {"python", "shell", "node", "php", "unknown"}
93
93
  PERSONAL_SCHEDULE_MANAGED_ENV = "NEXO_MANAGED_PERSONAL_CRON"
94
94
  SUPPORTED_RECOVERY_POLICIES = {"none", "run_once_on_wake", "catchup", "restart", "restart_daemon"}
95
95
  PERSONAL_SCRIPT_FILENAME_PREFIX = "ps-"
96
+ _LEGACY_CORE_SCRIPT_ALIASES = {
97
+ "nexo-postcompact.sh": "post-compact.sh",
98
+ "nexo-memory-precompact.sh": "pre-compact.sh",
99
+ "nexo-memory-stop.sh": "session-stop.sh",
100
+ "nexo-session-briefing.sh": "session-start.sh",
101
+ }
96
102
 
97
103
 
98
104
  def get_nexo_home() -> Path:
@@ -130,8 +136,33 @@ def _apply_legacy_personal_script_backfills() -> None:
130
136
  wake_recovery.write_text("".join(head + lines[start:]))
131
137
 
132
138
 
139
+ def _add_runtime_artifact_names(names: set[str], artifact_path: Path) -> None:
140
+ try:
141
+ data = json.loads(artifact_path.read_text())
142
+ except Exception:
143
+ return
144
+ for key in ("script_names", "hook_names"):
145
+ for item in data.get(key, []):
146
+ if isinstance(item, str) and item.strip():
147
+ names.add(Path(item).name)
148
+
149
+
150
+ def _add_filenames_from_dir(names: set[str], directory: Path, *, skip_if_scripts_dir: bool = False) -> None:
151
+ if not directory.is_dir():
152
+ return
153
+ if skip_if_scripts_dir:
154
+ try:
155
+ if directory.resolve() == get_scripts_dir().resolve():
156
+ return
157
+ except Exception:
158
+ pass
159
+ for item in directory.iterdir():
160
+ if item.is_file() and not item.name.startswith("."):
161
+ names.add(item.name)
162
+
163
+
133
164
  def load_core_script_names() -> set[str]:
134
- """Load runtime-managed script names (core, not personal)."""
165
+ """Load every core-managed runtime artifact name that must never be treated as personal."""
135
166
  names: set[str] = set()
136
167
  for manifest_path in [NEXO_CODE / "crons" / "manifest.json", NEXO_HOME / "crons" / "manifest.json"]:
137
168
  if manifest_path.exists():
@@ -144,25 +175,22 @@ def load_core_script_names() -> set[str]:
144
175
  break
145
176
  except Exception:
146
177
  continue
147
- runtime_manifest = NEXO_HOME / "config" / "runtime-core-artifacts.json"
148
- if runtime_manifest.exists():
149
- try:
150
- data = json.loads(runtime_manifest.read_text())
151
- for key in ("script_names", "hook_names"):
152
- for name in data.get(key, []):
153
- clean = Path(str(name)).name
154
- if clean:
155
- names.add(clean)
156
- except Exception:
157
- pass
158
- hooks_dir = NEXO_HOME / "hooks"
159
- if hooks_dir.is_dir():
160
- try:
161
- for item in hooks_dir.iterdir():
162
- if item.is_file():
163
- names.add(item.name)
164
- except Exception:
165
- pass
178
+
179
+ for artifact_path in (
180
+ NEXO_HOME / "config" / "runtime-core-artifacts.json",
181
+ NEXO_CODE / "config" / "runtime-core-artifacts.json",
182
+ NEXO_CODE.parent / "config" / "runtime-core-artifacts.json",
183
+ ):
184
+ if artifact_path.exists():
185
+ _add_runtime_artifact_names(names, artifact_path)
186
+
187
+ _add_filenames_from_dir(names, NEXO_HOME / "hooks")
188
+ _add_filenames_from_dir(names, NEXO_CODE / "hooks")
189
+ _add_filenames_from_dir(names, NEXO_CODE / "scripts", skip_if_scripts_dir=True)
190
+
191
+ for legacy_name, canonical_name in _LEGACY_CORE_SCRIPT_ALIASES.items():
192
+ if canonical_name in names:
193
+ names.add(legacy_name)
166
194
  names.update(_LEGACY_CORE_RUNTIME_FILES)
167
195
  return names
168
196
 
@@ -0,0 +1,43 @@
1
+ # Run NEXO Audit Phase
2
+
3
+ Usa esta skill cuando haya que ejecutar una fase de auditoria de NEXO y el cuello de botella sea decidir el alcance de `evolution_apply` y arrancar una tanda de items con disciplina empirica.
4
+
5
+ ## Pasos
6
+ 1. Abre `goal + workflow + task` y fija el terreno real antes de interpretar el informe:
7
+ - repo/runtime activo
8
+ - DB real
9
+ - mecanismo de update
10
+ - tests y estado git
11
+ 2. Fija la regla de autonomia antes de empezar:
12
+ - Francisco no quiere checkpoints uno-a-uno para trabajo mecanico
13
+ - NEXO hace branches, PRs, merge y reporta despues con evidencia
14
+ - solo un blast radius arquitectonico enorme merece checkpoint
15
+ 3. Trata `evolution_apply` como una decision tecnica de implementacion, no como permiso humano:
16
+ - el camino de apply ya existe via `evolution_log` + `_apply_accepted_proposals`
17
+ - el sandbox/snapshot/rollback protege la materializacion del cambio aceptado
18
+ - no dupliques ese mecanismo en deep sleep ni en el runner de auditoria
19
+ 4. Lanza la verificacion empirica de todos los items en paralelo:
20
+ - `grep + read` del codigo
21
+ - SQL/schema real
22
+ - AST/tests/imports/logs cuando aplique
23
+ - asume FP hasta que la evidencia lo contradiga
24
+ 5. Clasifica cada item:
25
+ - `real_gap`
26
+ - `casi_fp`
27
+ - `fp`
28
+ 6. Ordena solo los `real_gap` por riesgo/blast radius y ejecutalos con worktree aislado si tocan core.
29
+ 7. Por cada `real_gap`:
30
+ - `guard_check`
31
+ - `track`
32
+ - branch propia
33
+ - implementacion minima
34
+ - tests adyacentes
35
+ - PR + auto-merge squash
36
+ - seguir al siguiente sin esperar CI salvo bloqueo real
37
+ 8. Para `fp` o `casi_fp`, captura learning/patron reusable en vez de reimplementar.
38
+ 9. Cierra la fase con evidencia real: PRs, tests, merge status y resultados de verificacion.
39
+
40
+ ## Gotchas
41
+ - Learning #198: no confundas "como trabaja NEXO" con "que puede aplicar evolution_apply". Lo primero ya esta resuelto: autonomia total.
42
+ - `apply_findings.py` ya stagea `code_change` en `evolution_log`; `nexo-evolution-run.py` ya consume `accepted` con sandbox/snapshot/rollback. Si el item pide eso, primero verifica si ya existe.
43
+ - En Fase 1+2 la auditoria automatica sobreestimo ~70% de gaps. Si no hay evidencia dura, no abras codigo.
@@ -0,0 +1,59 @@
1
+ {
2
+ "id": "SK-RUN-NEXO-AUDIT-PHASE",
3
+ "name": "Run NEXO Audit Phase",
4
+ "description": "Workflow para ejecutar una fase de auditoria de NEXO con autonomia total y verificacion empirica. Cubre el patron repetido de decidir el scope de evolution_apply y arrancar una tanda de items sin checkpoints manuales.",
5
+ "level": "published",
6
+ "mode": "guide",
7
+ "source_kind": "core",
8
+ "execution_level": "none",
9
+ "approval_required": false,
10
+ "tags": [
11
+ "nexo",
12
+ "audit",
13
+ "evolution",
14
+ "sandbox",
15
+ "verification",
16
+ "core"
17
+ ],
18
+ "trigger_patterns": [
19
+ "evolution_apply sandbox scope",
20
+ "fase 2 item 1",
21
+ "arrancar verificacion empirica",
22
+ "ejecutar fase de auditoria nexo",
23
+ "audit phase nexo",
24
+ "7 items audit"
25
+ ],
26
+ "steps": [
27
+ "Abrir goal, workflow y task antes de tocar la auditoria; resolver repo/runtime real, DB real, tests y estado git antes de interpretar el informe.",
28
+ "Fijar la regla de autonomia: NEXO hace branches, PRs, merge y progreso periodico; no pedir checkpoints uno-a-uno a Francisco para trabajo mecanico.",
29
+ "Para `evolution_apply`, separar dos planos: el sandbox es para materializar/aplicar cambios aceptados con snapshot/rollback; no es un freno para el modo de trabajo autonomo del agente.",
30
+ "Lanzar verificacion empirica de TODOS los items en paralelo: grep+read, SQL/schema real, AST/tests, logs. Partir de la hipotesis de FP hasta tener evidencia.",
31
+ "Clasificar cada item en `real_gap`, `casi_fp` o `fp` y ordenar solo los reales por blast radius/riesgo.",
32
+ "Si hay que tocar core, usar worktree aislado; por item real: guard_check -> track -> branch -> implement -> tests adyacentes -> PR -> auto-merge squash.",
33
+ "Para items FP o casi-FP, capturar learning o ajustar el patron reusable en vez de reimplementar.",
34
+ "Cerrar con evidencia de PRs, tests y merge status reales; no usar diarios o workflow text como sustituto."
35
+ ],
36
+ "gotchas": [
37
+ "No pedir aprobacion manual a Francisco para cada PR: learning #198 confirma autonomia total con transparencia y reportes periodicos.",
38
+ "No duplicar el sandbox de evolution en otros sitios: el apply de cambios aceptados ya reutiliza evolution_log + sandbox/snapshot/rollback.",
39
+ "El ratio historico de FPs en esta clase de auditoria es alto; si no hay evidencia concreta, el item no se toca."
40
+ ],
41
+ "params_schema": {
42
+ "audit_file": {
43
+ "type": "string",
44
+ "required": true
45
+ },
46
+ "phase_label": {
47
+ "type": "string",
48
+ "required": false,
49
+ "default": ""
50
+ },
51
+ "repo_path": {
52
+ "type": "string",
53
+ "required": false,
54
+ "default": ""
55
+ }
56
+ },
57
+ "command_template": {},
58
+ "stable_after_uses": 5
59
+ }
@@ -9,15 +9,74 @@ import sys
9
9
  from pathlib import Path
10
10
 
11
11
 
12
+ def _is_repo_root(candidate: Path) -> bool:
13
+ return (
14
+ (candidate / "package.json").is_file()
15
+ and (candidate / "release-contracts").is_dir()
16
+ and (candidate / "scripts" / "verify_release_readiness.py").is_file()
17
+ )
18
+
19
+
20
+ def _normalize_candidate(raw: str | Path) -> Path:
21
+ candidate = Path(raw).expanduser().resolve()
22
+ if candidate.is_file():
23
+ candidate = candidate.parent
24
+ if candidate.name == "src" and (candidate / "server.py").is_file():
25
+ candidate = candidate.parent
26
+ return candidate
27
+
28
+
29
+ def _resolve_repo_root_from_atlas() -> Path | None:
30
+ homes = []
31
+ env_home = os.environ.get("NEXO_HOME", "").strip()
32
+ if env_home:
33
+ homes.append(Path(env_home).expanduser())
34
+ homes.extend((Path.home() / ".nexo", Path.home() / "claude"))
35
+
36
+ seen = set()
37
+ for home in homes:
38
+ key = str(home)
39
+ if key in seen:
40
+ continue
41
+ seen.add(key)
42
+ atlas_path = home / "brain" / "project-atlas.json"
43
+ if not atlas_path.is_file():
44
+ continue
45
+ try:
46
+ payload = json.loads(atlas_path.read_text(encoding="utf-8"))
47
+ except json.JSONDecodeError:
48
+ continue
49
+ nexo = payload.get("nexo") if isinstance(payload, dict) else None
50
+ locations = nexo.get("locations") if isinstance(nexo, dict) else None
51
+ source = locations.get("mcp_server", "") if isinstance(locations, dict) else ""
52
+ if not isinstance(source, str) or not source.strip():
53
+ continue
54
+ candidate = _normalize_candidate(source)
55
+ if _is_repo_root(candidate):
56
+ return candidate
57
+ return None
58
+
59
+
12
60
  def _resolve_repo_root() -> Path:
13
61
  env_code = os.environ.get("NEXO_CODE", "").strip()
14
62
  if env_code:
15
- candidate = Path(env_code).expanduser().resolve()
16
- if candidate.is_file():
17
- candidate = candidate.parent
18
- if (candidate / "cli.py").is_file():
19
- return candidate.parent if candidate.name == "src" else candidate
20
- return Path(__file__).resolve().parents[3]
63
+ candidate = _normalize_candidate(env_code)
64
+ if _is_repo_root(candidate):
65
+ return candidate
66
+
67
+ cwd = Path.cwd().resolve()
68
+ for candidate in (cwd, *cwd.parents):
69
+ if _is_repo_root(candidate):
70
+ return candidate
71
+
72
+ atlas_repo = _resolve_repo_root_from_atlas()
73
+ if atlas_repo is not None:
74
+ return atlas_repo
75
+
76
+ script_root = _normalize_candidate(Path(__file__).resolve().parents[3])
77
+ if _is_repo_root(script_root):
78
+ return script_root
79
+ return script_root
21
80
 
22
81
 
23
82
  ROOT = _resolve_repo_root()
@@ -77,7 +136,7 @@ def _env(nexo_home: str) -> dict[str, str]:
77
136
  env["PYTHONPATH"] = (
78
137
  f"{src_path}{os.pathsep}{existing_pythonpath}" if existing_pythonpath else src_path
79
138
  )
80
- env.setdefault("NEXO_CODE", str(ROOT / "src"))
139
+ env["NEXO_CODE"] = src_path
81
140
  if nexo_home.strip():
82
141
  env["NEXO_HOME"] = str(Path(nexo_home).expanduser())
83
142
  return env
@@ -128,13 +187,27 @@ def _run(cmd: list[str], *, env: dict[str, str]) -> None:
128
187
  raise SystemExit(result.returncode)
129
188
 
130
189
 
190
+ def _looks_like_nexo_home(raw: str) -> bool:
191
+ if not raw.strip():
192
+ return False
193
+ candidate = Path(raw).expanduser()
194
+ return (candidate / "skills-runtime").is_dir() or (candidate / "operations" / "tool-logs").is_dir()
195
+
196
+
197
+ def _parse_optional_paths(argv: list[str]) -> tuple[str, str]:
198
+ if len(argv) > 6:
199
+ return argv[5], argv[6]
200
+ if len(argv) > 5:
201
+ return ("", argv[5]) if _looks_like_nexo_home(argv[5]) else (argv[5], "")
202
+ return "", ""
203
+
204
+
131
205
  def main() -> int:
132
206
  contract_arg = sys.argv[1] if len(sys.argv) > 1 else "auto"
133
207
  require_contract_complete = _parse_bool(sys.argv[2] if len(sys.argv) > 2 else "true", True)
134
208
  include_smoke = _parse_bool(sys.argv[3] if len(sys.argv) > 3 else "true", True)
135
209
  ci = _parse_bool(sys.argv[4] if len(sys.argv) > 4 else "false", False)
136
- website_root = sys.argv[5] if len(sys.argv) > 5 else ""
137
- nexo_home = sys.argv[6] if len(sys.argv) > 6 else ""
210
+ website_root, nexo_home = _parse_optional_paths(sys.argv)
138
211
 
139
212
  version = _package_version()
140
213
  contract_path = _resolve_contract(version, contract_arg)
@@ -0,0 +1,302 @@
1
+ """Portable export/import helpers for operator user data."""
2
+
3
+ from __future__ import annotations
4
+
5
+ import json
6
+ import os
7
+ import shutil
8
+ import sqlite3
9
+ import tarfile
10
+ import tempfile
11
+ from datetime import datetime, timezone
12
+ from pathlib import Path
13
+
14
+ from runtime_home import export_resolved_nexo_home
15
+
16
+ NEXO_HOME = export_resolved_nexo_home()
17
+ NEXO_CODE = Path(os.environ.get("NEXO_CODE", str(Path(__file__).resolve().parent)))
18
+ EXPORTS_DIR = NEXO_HOME / "exports"
19
+ STAGING_DIR = EXPORTS_DIR / ".staging"
20
+ USER_DIRS = ("brain", "coordination", "nexo-email", "assets")
21
+ USER_CONFIG_FILES = ("schedule.json",)
22
+ IGNORED_FILENAMES = {".DS_Store"}
23
+ IGNORED_DIRS = {"__pycache__"}
24
+ IGNORED_SUFFIXES = {".pyc", ".pyo"}
25
+
26
+
27
+ def _now_stamp() -> str:
28
+ return datetime.now(timezone.utc).strftime("%Y%m%d-%H%M%S")
29
+
30
+
31
+ def _runtime_version() -> str:
32
+ for candidate, key in (
33
+ (NEXO_HOME / "version.json", "version"),
34
+ (NEXO_CODE.parent / "version.json", "version"),
35
+ (NEXO_CODE.parent / "package.json", "version"),
36
+ (NEXO_HOME / "package.json", "version"),
37
+ ):
38
+ try:
39
+ if candidate.is_file():
40
+ return str(json.loads(candidate.read_text()).get(key, "?"))
41
+ except Exception:
42
+ continue
43
+ return "?"
44
+
45
+
46
+ def _sqlite_backup(src: Path, dest: Path) -> None:
47
+ dest.parent.mkdir(parents=True, exist_ok=True)
48
+ src_conn = sqlite3.connect(str(src))
49
+ try:
50
+ dst_conn = sqlite3.connect(str(dest))
51
+ try:
52
+ src_conn.backup(dst_conn)
53
+ finally:
54
+ dst_conn.close()
55
+ finally:
56
+ src_conn.close()
57
+
58
+
59
+ def _should_skip_file(path: Path, exclude_names: set[str] | None = None) -> bool:
60
+ exclude = exclude_names or set()
61
+ if path.name in exclude:
62
+ return True
63
+ if path.name in IGNORED_FILENAMES:
64
+ return True
65
+ if path.suffix in IGNORED_SUFFIXES:
66
+ return True
67
+ return False
68
+
69
+
70
+ def _copy_tree_filtered(src: Path, dest: Path, *, exclude_names: set[str] | None = None) -> int:
71
+ if not src.is_dir():
72
+ return 0
73
+ copied = 0
74
+ for root, dirs, files in os.walk(src):
75
+ root_path = Path(root)
76
+ rel = root_path.relative_to(src)
77
+ dirs[:] = [item for item in dirs if item not in IGNORED_DIRS]
78
+ target_root = dest / rel
79
+ target_root.mkdir(parents=True, exist_ok=True)
80
+ for name in files:
81
+ file_path = root_path / name
82
+ if _should_skip_file(file_path, exclude_names=exclude_names):
83
+ continue
84
+ shutil.copy2(str(file_path), str(target_root / name))
85
+ copied += 1
86
+ return copied
87
+
88
+
89
+ def _copy_file_if_present(src: Path, dest: Path) -> bool:
90
+ if not src.is_file():
91
+ return False
92
+ dest.parent.mkdir(parents=True, exist_ok=True)
93
+ shutil.copy2(str(src), str(dest))
94
+ return True
95
+
96
+
97
+ def _safe_extract(archive_path: Path, dest_dir: Path) -> None:
98
+ with tarfile.open(archive_path, "r:*") as tar:
99
+ for member in tar.getmembers():
100
+ target = (dest_dir / member.name).resolve()
101
+ if not str(target).startswith(str(dest_dir.resolve())):
102
+ raise ValueError(f"archive path escapes destination: {member.name}")
103
+ tar.extractall(dest_dir)
104
+
105
+
106
+ def _load_personal_scripts() -> tuple[list[dict], list[dict]]:
107
+ from script_registry import classify_scripts_dir, discover_personal_schedules
108
+
109
+ classification = classify_scripts_dir()
110
+ scripts = [entry for entry in classification.get("entries", []) if entry.get("classification") == "personal"]
111
+ script_paths = {entry["path"] for entry in scripts}
112
+ schedules = [
113
+ schedule for schedule in discover_personal_schedules()
114
+ if schedule.get("script_path") in script_paths
115
+ ]
116
+ return scripts, schedules
117
+
118
+
119
+ def export_user_bundle(output_path: str = "") -> dict:
120
+ output = Path(output_path).expanduser() if output_path.strip() else (EXPORTS_DIR / f"nexo-user-data-{_now_stamp()}.tar.gz")
121
+ output.parent.mkdir(parents=True, exist_ok=True)
122
+ STAGING_DIR.mkdir(parents=True, exist_ok=True)
123
+ stage_dir = Path(tempfile.mkdtemp(prefix="nexo-export-", dir=str(STAGING_DIR)))
124
+ bundle_root = stage_dir / "bundle"
125
+ bundle_root.mkdir(parents=True, exist_ok=True)
126
+
127
+ sections: dict[str, dict] = {}
128
+
129
+ try:
130
+ data_db = NEXO_HOME / "data" / "nexo.db"
131
+ if data_db.is_file():
132
+ _sqlite_backup(data_db, bundle_root / "data" / "nexo.db")
133
+ sections["data_db"] = {"path": "data/nexo.db"}
134
+
135
+ brain_dir = NEXO_HOME / "brain"
136
+ if brain_dir.is_dir():
137
+ copied = _copy_tree_filtered(brain_dir, bundle_root / "brain", exclude_names={"nexo.db"})
138
+ brain_db = brain_dir / "nexo.db"
139
+ if brain_db.is_file():
140
+ _sqlite_backup(brain_db, bundle_root / "brain" / "nexo.db")
141
+ copied += 1
142
+ sections["brain"] = {"path": "brain", "files": copied}
143
+
144
+ for dirname in USER_DIRS[1:]:
145
+ src_dir = NEXO_HOME / dirname
146
+ if not src_dir.is_dir():
147
+ continue
148
+ copied = _copy_tree_filtered(src_dir, bundle_root / dirname)
149
+ sections[dirname] = {"path": dirname, "files": copied}
150
+
151
+ copied_configs: list[str] = []
152
+ for filename in USER_CONFIG_FILES:
153
+ if _copy_file_if_present(NEXO_HOME / "config" / filename, bundle_root / "config" / filename):
154
+ copied_configs.append(filename)
155
+ if copied_configs:
156
+ sections["config"] = {"path": "config", "files": copied_configs}
157
+
158
+ scripts, schedules = _load_personal_scripts()
159
+ exported_scripts: list[dict] = []
160
+ scripts_dir = bundle_root / "personal-scripts"
161
+ scripts_dir.mkdir(parents=True, exist_ok=True)
162
+ for entry in scripts:
163
+ src_path = Path(entry["path"])
164
+ if not src_path.is_file():
165
+ continue
166
+ shutil.copy2(str(src_path), str(scripts_dir / src_path.name))
167
+ exported_scripts.append(
168
+ {
169
+ "name": entry.get("name", src_path.stem),
170
+ "path": f"personal-scripts/{src_path.name}",
171
+ "runtime": entry.get("runtime", "unknown"),
172
+ "description": entry.get("description", ""),
173
+ }
174
+ )
175
+ (scripts_dir / "manifest.json").write_text(
176
+ json.dumps(
177
+ {
178
+ "scripts": exported_scripts,
179
+ "schedules": schedules,
180
+ },
181
+ indent=2,
182
+ ensure_ascii=False,
183
+ ) + "\n"
184
+ )
185
+ sections["personal_scripts"] = {
186
+ "path": "personal-scripts",
187
+ "files": len(exported_scripts),
188
+ "schedules": len(schedules),
189
+ }
190
+
191
+ manifest = {
192
+ "kind": "nexo-user-data-bundle",
193
+ "version": _runtime_version(),
194
+ "created_at": datetime.now(timezone.utc).isoformat(),
195
+ "nexo_home": str(NEXO_HOME),
196
+ "sections": sections,
197
+ }
198
+ (bundle_root / "manifest.json").write_text(json.dumps(manifest, indent=2, ensure_ascii=False) + "\n")
199
+
200
+ with tarfile.open(output, "w:gz") as tar:
201
+ tar.add(bundle_root, arcname="bundle")
202
+
203
+ return {
204
+ "ok": True,
205
+ "path": str(output),
206
+ "kind": manifest["kind"],
207
+ "version": manifest["version"],
208
+ "sections": sections,
209
+ }
210
+ finally:
211
+ shutil.rmtree(stage_dir, ignore_errors=True)
212
+
213
+
214
+ def import_user_bundle(bundle_path: str) -> dict:
215
+ archive_path = Path(bundle_path).expanduser()
216
+ if not archive_path.is_file():
217
+ return {"ok": False, "error": f"bundle not found: {archive_path}"}
218
+
219
+ backups_dir = NEXO_HOME / "backups"
220
+ backups_dir.mkdir(parents=True, exist_ok=True)
221
+ safety_backup = backups_dir / f"pre-import-user-data-{_now_stamp()}.tar.gz"
222
+ safety_result = export_user_bundle(str(safety_backup))
223
+ if not safety_result.get("ok"):
224
+ return {"ok": False, "error": "failed to create safety backup", "safety_backup": str(safety_backup)}
225
+
226
+ STAGING_DIR.mkdir(parents=True, exist_ok=True)
227
+ stage_dir = Path(tempfile.mkdtemp(prefix="nexo-import-", dir=str(STAGING_DIR)))
228
+
229
+ try:
230
+ _safe_extract(archive_path, stage_dir)
231
+ bundle_root = stage_dir / "bundle"
232
+ manifest_path = bundle_root / "manifest.json"
233
+ if not manifest_path.is_file():
234
+ return {"ok": False, "error": "bundle manifest missing", "safety_backup": str(safety_backup)}
235
+
236
+ manifest = json.loads(manifest_path.read_text())
237
+ if manifest.get("kind") != "nexo-user-data-bundle":
238
+ return {
239
+ "ok": False,
240
+ "error": f"unsupported bundle kind: {manifest.get('kind', 'unknown')}",
241
+ "safety_backup": str(safety_backup),
242
+ }
243
+
244
+ restored: dict[str, dict] = {}
245
+
246
+ data_db = bundle_root / "data" / "nexo.db"
247
+ if data_db.is_file():
248
+ _sqlite_backup(data_db, NEXO_HOME / "data" / "nexo.db")
249
+ restored["data_db"] = {"path": "data/nexo.db"}
250
+
251
+ brain_dir = bundle_root / "brain"
252
+ if brain_dir.is_dir():
253
+ copied = _copy_tree_filtered(brain_dir, NEXO_HOME / "brain", exclude_names={"nexo.db"})
254
+ brain_db = brain_dir / "nexo.db"
255
+ if brain_db.is_file():
256
+ _sqlite_backup(brain_db, NEXO_HOME / "brain" / "nexo.db")
257
+ copied += 1
258
+ restored["brain"] = {"path": "brain", "files": copied}
259
+
260
+ for dirname in USER_DIRS[1:]:
261
+ src_dir = bundle_root / dirname
262
+ if not src_dir.is_dir():
263
+ continue
264
+ copied = _copy_tree_filtered(src_dir, NEXO_HOME / dirname)
265
+ restored[dirname] = {"path": dirname, "files": copied}
266
+
267
+ restored_configs: list[str] = []
268
+ for filename in USER_CONFIG_FILES:
269
+ if _copy_file_if_present(bundle_root / "config" / filename, NEXO_HOME / "config" / filename):
270
+ restored_configs.append(filename)
271
+ if restored_configs:
272
+ restored["config"] = {"path": "config", "files": restored_configs}
273
+
274
+ imported_scripts = 0
275
+ scripts_dir = bundle_root / "personal-scripts"
276
+ target_scripts_dir = NEXO_HOME / "scripts"
277
+ target_scripts_dir.mkdir(parents=True, exist_ok=True)
278
+ if scripts_dir.is_dir():
279
+ for script_path in sorted(scripts_dir.iterdir()):
280
+ if not script_path.is_file() or script_path.name == "manifest.json":
281
+ continue
282
+ shutil.copy2(str(script_path), str(target_scripts_dir / script_path.name))
283
+ imported_scripts += 1
284
+ restored["personal_scripts"] = {"path": "personal-scripts", "files": imported_scripts}
285
+
286
+ from db import init_db
287
+ from script_registry import reconcile_personal_scripts
288
+
289
+ init_db()
290
+ reconcile_result = reconcile_personal_scripts(dry_run=False)
291
+
292
+ return {
293
+ "ok": True,
294
+ "path": str(archive_path),
295
+ "kind": manifest.get("kind"),
296
+ "bundle_version": manifest.get("version"),
297
+ "safety_backup": str(safety_backup),
298
+ "restored": restored,
299
+ "reconciled": reconcile_result,
300
+ }
301
+ finally:
302
+ shutil.rmtree(stage_dir, ignore_errors=True)