nexo-brain 2.5.1 → 2.6.1
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 +33 -0
- package/.mcp.json +12 -0
- package/README.md +38 -26
- package/bin/nexo-brain.js +35 -32
- package/hooks/hooks.json +14 -0
- package/package.json +11 -4
- package/src/auto_update.py +44 -1
- package/src/cli.py +388 -23
- package/src/cron_recovery.py +283 -0
- package/src/crons/manifest.json +79 -21
- package/src/crons/sync.py +136 -31
- package/src/db/__init__.py +11 -0
- package/src/db/_personal_scripts.py +548 -0
- package/src/db/_schema.py +44 -1
- package/src/doctor/providers/runtime.py +272 -75
- package/src/evolution_cycle.py +4 -1
- package/src/nexo.db +0 -0
- package/src/plugins/personal_scripts.py +117 -0
- package/src/plugins/schedule.py +116 -27
- package/src/script_registry.py +877 -28
- package/src/scripts/nexo-catchup.py +74 -109
- package/src/scripts/nexo-evolution-run.py +37 -12
- package/src/scripts/nexo-watchdog.sh +242 -54
- package/src/tools_learnings.py +8 -0
- package/templates/launchagents/com.nexo.catchup.plist +7 -6
- package/templates/script-template.py +3 -0
- package/templates/script-template.sh +13 -0
- package/src/scripts/nexo-day-orchestrator.sh +0 -139
|
@@ -0,0 +1,548 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
"""NEXO DB — Personal scripts registry.
|
|
3
|
+
|
|
4
|
+
Filesystem remains the source of truth for personal scripts in NEXO_HOME/scripts/.
|
|
5
|
+
SQLite stores operational metadata so NEXO can reason about what scripts exist,
|
|
6
|
+
what they do, and which schedules/plists are attached to them.
|
|
7
|
+
"""
|
|
8
|
+
|
|
9
|
+
import datetime
|
|
10
|
+
import json
|
|
11
|
+
import os
|
|
12
|
+
from pathlib import Path
|
|
13
|
+
|
|
14
|
+
from db._core import get_db
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
NEXO_HOME = Path(os.environ.get("NEXO_HOME", str(Path.home() / ".nexo")))
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
def _now_text() -> str:
|
|
21
|
+
return datetime.datetime.now().isoformat(timespec="seconds")
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
def _row_to_dict(row) -> dict:
|
|
25
|
+
return dict(row) if row is not None else {}
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
def _json_load(value, default):
|
|
29
|
+
if value in ("", None):
|
|
30
|
+
return default
|
|
31
|
+
if isinstance(value, (dict, list)):
|
|
32
|
+
return value
|
|
33
|
+
try:
|
|
34
|
+
parsed = json.loads(value)
|
|
35
|
+
except Exception:
|
|
36
|
+
return default
|
|
37
|
+
return parsed if isinstance(parsed, type(default)) else default
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
def _json_dump(value, default):
|
|
41
|
+
if value in ("", None):
|
|
42
|
+
value = default
|
|
43
|
+
if isinstance(value, str):
|
|
44
|
+
try:
|
|
45
|
+
parsed = json.loads(value)
|
|
46
|
+
value = parsed
|
|
47
|
+
except Exception:
|
|
48
|
+
return json.dumps(default, ensure_ascii=False)
|
|
49
|
+
return json.dumps(value, ensure_ascii=False)
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
def _safe_slug(value: str) -> str:
|
|
53
|
+
chars: list[str] = []
|
|
54
|
+
for ch in value.lower():
|
|
55
|
+
if ch.isalnum():
|
|
56
|
+
chars.append(ch)
|
|
57
|
+
elif ch in {"-", "_"}:
|
|
58
|
+
chars.append("-")
|
|
59
|
+
slug = "".join(chars).strip("-")
|
|
60
|
+
return slug or "script"
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+
def _ensure_script_id(conn, name: str, path: str) -> str:
|
|
64
|
+
existing = conn.execute(
|
|
65
|
+
"SELECT id FROM personal_scripts WHERE path = ? OR name = ? ORDER BY path = ? DESC LIMIT 1",
|
|
66
|
+
(path, name, path),
|
|
67
|
+
).fetchone()
|
|
68
|
+
if existing:
|
|
69
|
+
return existing["id"]
|
|
70
|
+
|
|
71
|
+
base = f"ps-{_safe_slug(name)}"
|
|
72
|
+
candidate = base
|
|
73
|
+
suffix = 2
|
|
74
|
+
while conn.execute("SELECT 1 FROM personal_scripts WHERE id = ?", (candidate,)).fetchone():
|
|
75
|
+
candidate = f"{base}-{suffix}"
|
|
76
|
+
suffix += 1
|
|
77
|
+
return candidate
|
|
78
|
+
|
|
79
|
+
|
|
80
|
+
def upsert_personal_script(
|
|
81
|
+
*,
|
|
82
|
+
name: str,
|
|
83
|
+
path: str,
|
|
84
|
+
description: str = "",
|
|
85
|
+
runtime: str = "unknown",
|
|
86
|
+
metadata: dict | None = None,
|
|
87
|
+
created_by: str = "manual",
|
|
88
|
+
source: str = "filesystem",
|
|
89
|
+
enabled: bool = True,
|
|
90
|
+
has_inline_metadata: bool = False,
|
|
91
|
+
) -> dict:
|
|
92
|
+
conn = get_db()
|
|
93
|
+
script_id = _ensure_script_id(conn, name, path)
|
|
94
|
+
now = _now_text()
|
|
95
|
+
conn.execute(
|
|
96
|
+
"""
|
|
97
|
+
INSERT INTO personal_scripts (
|
|
98
|
+
id, name, path, description, runtime, metadata_json, created_by, source,
|
|
99
|
+
enabled, has_inline_metadata, last_synced_at, created_at, updated_at
|
|
100
|
+
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
|
|
101
|
+
ON CONFLICT(path) DO UPDATE SET
|
|
102
|
+
name = excluded.name,
|
|
103
|
+
description = excluded.description,
|
|
104
|
+
runtime = excluded.runtime,
|
|
105
|
+
metadata_json = excluded.metadata_json,
|
|
106
|
+
created_by = COALESCE(NULLIF(personal_scripts.created_by, ''), excluded.created_by),
|
|
107
|
+
source = excluded.source,
|
|
108
|
+
enabled = excluded.enabled,
|
|
109
|
+
has_inline_metadata = excluded.has_inline_metadata,
|
|
110
|
+
last_synced_at = excluded.last_synced_at,
|
|
111
|
+
updated_at = excluded.updated_at
|
|
112
|
+
""",
|
|
113
|
+
(
|
|
114
|
+
script_id,
|
|
115
|
+
name,
|
|
116
|
+
path,
|
|
117
|
+
description,
|
|
118
|
+
runtime,
|
|
119
|
+
_json_dump(metadata or {}, {}),
|
|
120
|
+
created_by,
|
|
121
|
+
source,
|
|
122
|
+
1 if enabled else 0,
|
|
123
|
+
1 if has_inline_metadata else 0,
|
|
124
|
+
now,
|
|
125
|
+
now,
|
|
126
|
+
now,
|
|
127
|
+
),
|
|
128
|
+
)
|
|
129
|
+
row = conn.execute("SELECT * FROM personal_scripts WHERE path = ?", (path,)).fetchone()
|
|
130
|
+
return hydrate_personal_script(_row_to_dict(row))
|
|
131
|
+
|
|
132
|
+
|
|
133
|
+
def delete_missing_personal_scripts(active_paths: list[str]) -> int:
|
|
134
|
+
conn = get_db()
|
|
135
|
+
if active_paths:
|
|
136
|
+
placeholders = ",".join("?" for _ in active_paths)
|
|
137
|
+
rows = conn.execute(
|
|
138
|
+
f"SELECT id FROM personal_scripts WHERE path NOT IN ({placeholders})",
|
|
139
|
+
tuple(active_paths),
|
|
140
|
+
).fetchall()
|
|
141
|
+
else:
|
|
142
|
+
rows = conn.execute("SELECT id FROM personal_scripts").fetchall()
|
|
143
|
+
|
|
144
|
+
count = len(rows)
|
|
145
|
+
for row in rows:
|
|
146
|
+
conn.execute("DELETE FROM personal_scripts WHERE id = ?", (row["id"],))
|
|
147
|
+
return count
|
|
148
|
+
|
|
149
|
+
|
|
150
|
+
def register_personal_script_schedule(
|
|
151
|
+
*,
|
|
152
|
+
script_path: str,
|
|
153
|
+
cron_id: str,
|
|
154
|
+
schedule_type: str,
|
|
155
|
+
schedule_value: str,
|
|
156
|
+
schedule_label: str = "",
|
|
157
|
+
launchd_label: str = "",
|
|
158
|
+
plist_path: str = "",
|
|
159
|
+
description: str = "",
|
|
160
|
+
enabled: bool = True,
|
|
161
|
+
) -> dict | None:
|
|
162
|
+
conn = get_db()
|
|
163
|
+
script = conn.execute(
|
|
164
|
+
"SELECT id FROM personal_scripts WHERE path = ?",
|
|
165
|
+
(script_path,),
|
|
166
|
+
).fetchone()
|
|
167
|
+
if not script:
|
|
168
|
+
return None
|
|
169
|
+
|
|
170
|
+
now = _now_text()
|
|
171
|
+
conn.execute(
|
|
172
|
+
"""
|
|
173
|
+
INSERT INTO personal_script_schedules (
|
|
174
|
+
script_id, cron_id, schedule_type, schedule_value, schedule_label,
|
|
175
|
+
launchd_label, plist_path, description, enabled, last_synced_at, created_at, updated_at
|
|
176
|
+
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
|
|
177
|
+
ON CONFLICT(cron_id) DO UPDATE SET
|
|
178
|
+
script_id = excluded.script_id,
|
|
179
|
+
schedule_type = excluded.schedule_type,
|
|
180
|
+
schedule_value = excluded.schedule_value,
|
|
181
|
+
schedule_label = excluded.schedule_label,
|
|
182
|
+
launchd_label = excluded.launchd_label,
|
|
183
|
+
plist_path = excluded.plist_path,
|
|
184
|
+
description = excluded.description,
|
|
185
|
+
enabled = excluded.enabled,
|
|
186
|
+
last_synced_at = excluded.last_synced_at,
|
|
187
|
+
updated_at = excluded.updated_at
|
|
188
|
+
""",
|
|
189
|
+
(
|
|
190
|
+
script["id"],
|
|
191
|
+
cron_id,
|
|
192
|
+
schedule_type,
|
|
193
|
+
schedule_value,
|
|
194
|
+
schedule_label,
|
|
195
|
+
launchd_label,
|
|
196
|
+
plist_path,
|
|
197
|
+
description,
|
|
198
|
+
1 if enabled else 0,
|
|
199
|
+
now,
|
|
200
|
+
now,
|
|
201
|
+
now,
|
|
202
|
+
),
|
|
203
|
+
)
|
|
204
|
+
row = conn.execute(
|
|
205
|
+
"SELECT * FROM personal_script_schedules WHERE cron_id = ?",
|
|
206
|
+
(cron_id,),
|
|
207
|
+
).fetchone()
|
|
208
|
+
return hydrate_personal_schedule(_row_to_dict(row))
|
|
209
|
+
|
|
210
|
+
|
|
211
|
+
def delete_missing_personal_schedules(active_cron_ids: list[str]) -> int:
|
|
212
|
+
conn = get_db()
|
|
213
|
+
if active_cron_ids:
|
|
214
|
+
placeholders = ",".join("?" for _ in active_cron_ids)
|
|
215
|
+
rows = conn.execute(
|
|
216
|
+
f"SELECT cron_id FROM personal_script_schedules WHERE cron_id NOT IN ({placeholders})",
|
|
217
|
+
tuple(active_cron_ids),
|
|
218
|
+
).fetchall()
|
|
219
|
+
else:
|
|
220
|
+
rows = conn.execute("SELECT cron_id FROM personal_script_schedules").fetchall()
|
|
221
|
+
|
|
222
|
+
count = len(rows)
|
|
223
|
+
for row in rows:
|
|
224
|
+
conn.execute("DELETE FROM personal_script_schedules WHERE cron_id = ?", (row["cron_id"],))
|
|
225
|
+
return count
|
|
226
|
+
|
|
227
|
+
|
|
228
|
+
def list_personal_script_schedules(script_id: str = "", include_disabled: bool = True) -> list[dict]:
|
|
229
|
+
conn = get_db()
|
|
230
|
+
clauses = []
|
|
231
|
+
params: list = []
|
|
232
|
+
if script_id:
|
|
233
|
+
clauses.append("script_id = ?")
|
|
234
|
+
params.append(script_id)
|
|
235
|
+
if not include_disabled:
|
|
236
|
+
clauses.append("enabled = 1")
|
|
237
|
+
where = f"WHERE {' AND '.join(clauses)}" if clauses else ""
|
|
238
|
+
rows = conn.execute(
|
|
239
|
+
f"SELECT * FROM personal_script_schedules {where} ORDER BY cron_id ASC",
|
|
240
|
+
tuple(params),
|
|
241
|
+
).fetchall()
|
|
242
|
+
return [hydrate_personal_schedule(_row_to_dict(row)) for row in rows]
|
|
243
|
+
|
|
244
|
+
|
|
245
|
+
def get_personal_script_schedule(cron_id: str) -> dict | None:
|
|
246
|
+
conn = get_db()
|
|
247
|
+
row = conn.execute(
|
|
248
|
+
"SELECT * FROM personal_script_schedules WHERE cron_id = ?",
|
|
249
|
+
(cron_id,),
|
|
250
|
+
).fetchone()
|
|
251
|
+
return hydrate_personal_schedule(_row_to_dict(row)) if row else None
|
|
252
|
+
|
|
253
|
+
|
|
254
|
+
def delete_personal_script_schedule(cron_id: str) -> int:
|
|
255
|
+
conn = get_db()
|
|
256
|
+
result = conn.execute(
|
|
257
|
+
"DELETE FROM personal_script_schedules WHERE cron_id = ?",
|
|
258
|
+
(cron_id,),
|
|
259
|
+
)
|
|
260
|
+
return int(result.rowcount or 0)
|
|
261
|
+
|
|
262
|
+
|
|
263
|
+
def hydrate_personal_schedule(row: dict) -> dict:
|
|
264
|
+
if not row:
|
|
265
|
+
return {}
|
|
266
|
+
row["enabled"] = bool(row.get("enabled", 1))
|
|
267
|
+
return row
|
|
268
|
+
|
|
269
|
+
|
|
270
|
+
def _latest_cron_runs_by_id(cron_ids: list[str]) -> dict[str, dict]:
|
|
271
|
+
conn = get_db()
|
|
272
|
+
if not cron_ids:
|
|
273
|
+
return {}
|
|
274
|
+
placeholders = ",".join("?" for _ in cron_ids)
|
|
275
|
+
rows = conn.execute(
|
|
276
|
+
f"""
|
|
277
|
+
SELECT c1.cron_id, c1.started_at, c1.exit_code
|
|
278
|
+
FROM cron_runs c1
|
|
279
|
+
JOIN (
|
|
280
|
+
SELECT cron_id, MAX(id) AS max_id
|
|
281
|
+
FROM cron_runs
|
|
282
|
+
WHERE cron_id IN ({placeholders})
|
|
283
|
+
GROUP BY cron_id
|
|
284
|
+
) latest ON latest.max_id = c1.id
|
|
285
|
+
""",
|
|
286
|
+
tuple(cron_ids),
|
|
287
|
+
).fetchall()
|
|
288
|
+
return {
|
|
289
|
+
row["cron_id"]: {
|
|
290
|
+
"started_at": row["started_at"],
|
|
291
|
+
"exit_code": row["exit_code"],
|
|
292
|
+
}
|
|
293
|
+
for row in rows
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
|
|
297
|
+
def hydrate_personal_script(row: dict) -> dict:
|
|
298
|
+
if not row:
|
|
299
|
+
return {}
|
|
300
|
+
row["enabled"] = bool(row.get("enabled", 1))
|
|
301
|
+
row["has_inline_metadata"] = bool(row.get("has_inline_metadata", 0))
|
|
302
|
+
row["metadata"] = _json_load(row.pop("metadata_json", "{}"), {})
|
|
303
|
+
return row
|
|
304
|
+
|
|
305
|
+
|
|
306
|
+
def list_personal_scripts(include_disabled: bool = True) -> list[dict]:
|
|
307
|
+
conn = get_db()
|
|
308
|
+
where = "" if include_disabled else "WHERE enabled = 1"
|
|
309
|
+
rows = conn.execute(
|
|
310
|
+
f"SELECT * FROM personal_scripts {where} ORDER BY name COLLATE NOCASE ASC"
|
|
311
|
+
).fetchall()
|
|
312
|
+
scripts = [hydrate_personal_script(_row_to_dict(row)) for row in rows]
|
|
313
|
+
if not scripts:
|
|
314
|
+
return []
|
|
315
|
+
|
|
316
|
+
schedules_by_script: dict[str, list[dict]] = {}
|
|
317
|
+
cron_ids: list[str] = []
|
|
318
|
+
for schedule in list_personal_script_schedules(include_disabled=include_disabled):
|
|
319
|
+
schedules_by_script.setdefault(schedule["script_id"], []).append(schedule)
|
|
320
|
+
cron_ids.append(schedule["cron_id"])
|
|
321
|
+
|
|
322
|
+
latest_runs = _latest_cron_runs_by_id(cron_ids)
|
|
323
|
+
for script in scripts:
|
|
324
|
+
script_schedules = schedules_by_script.get(script["id"], [])
|
|
325
|
+
for schedule in script_schedules:
|
|
326
|
+
latest = latest_runs.get(schedule["cron_id"])
|
|
327
|
+
if latest:
|
|
328
|
+
schedule["last_run_at"] = latest["started_at"]
|
|
329
|
+
schedule["last_exit_code"] = latest["exit_code"]
|
|
330
|
+
script["schedules"] = script_schedules
|
|
331
|
+
script["has_schedule"] = bool(script_schedules)
|
|
332
|
+
latest = None
|
|
333
|
+
for schedule in script_schedules:
|
|
334
|
+
started_at = schedule.get("last_run_at")
|
|
335
|
+
if started_at and (latest is None or started_at > latest.get("started_at", "")):
|
|
336
|
+
latest = {"started_at": started_at, "exit_code": schedule.get("last_exit_code")}
|
|
337
|
+
if latest:
|
|
338
|
+
script["last_run_at"] = latest["started_at"]
|
|
339
|
+
script["last_exit_code"] = latest["exit_code"]
|
|
340
|
+
return scripts
|
|
341
|
+
|
|
342
|
+
|
|
343
|
+
def get_personal_script(name_or_path: str) -> dict | None:
|
|
344
|
+
conn = get_db()
|
|
345
|
+
row = conn.execute(
|
|
346
|
+
"""
|
|
347
|
+
SELECT * FROM personal_scripts
|
|
348
|
+
WHERE path = ? OR name = ?
|
|
349
|
+
ORDER BY path = ? DESC
|
|
350
|
+
LIMIT 1
|
|
351
|
+
""",
|
|
352
|
+
(name_or_path, name_or_path, name_or_path),
|
|
353
|
+
).fetchone()
|
|
354
|
+
if not row:
|
|
355
|
+
return None
|
|
356
|
+
script = hydrate_personal_script(_row_to_dict(row))
|
|
357
|
+
script["schedules"] = list_personal_script_schedules(script["id"])
|
|
358
|
+
script["has_schedule"] = bool(script["schedules"])
|
|
359
|
+
return script
|
|
360
|
+
|
|
361
|
+
|
|
362
|
+
def delete_personal_script(name_or_path: str) -> int:
|
|
363
|
+
conn = get_db()
|
|
364
|
+
result = conn.execute(
|
|
365
|
+
"DELETE FROM personal_scripts WHERE path = ? OR name = ? OR id = ?",
|
|
366
|
+
(name_or_path, name_or_path, name_or_path),
|
|
367
|
+
)
|
|
368
|
+
return int(result.rowcount or 0)
|
|
369
|
+
|
|
370
|
+
|
|
371
|
+
def record_personal_script_run(name_or_path: str, exit_code: int, run_at: str | None = None) -> None:
|
|
372
|
+
conn = get_db()
|
|
373
|
+
run_at = run_at or _now_text()
|
|
374
|
+
conn.execute(
|
|
375
|
+
"""
|
|
376
|
+
UPDATE personal_scripts
|
|
377
|
+
SET last_run_at = ?, last_exit_code = ?, updated_at = ?
|
|
378
|
+
WHERE path = ? OR name = ?
|
|
379
|
+
""",
|
|
380
|
+
(run_at, exit_code, _now_text(), name_or_path, name_or_path),
|
|
381
|
+
)
|
|
382
|
+
|
|
383
|
+
|
|
384
|
+
def sync_personal_scripts_registry(
|
|
385
|
+
script_records: list[dict],
|
|
386
|
+
schedule_records: list[dict] | None = None,
|
|
387
|
+
*,
|
|
388
|
+
prune_missing: bool = True,
|
|
389
|
+
) -> dict:
|
|
390
|
+
schedule_records = schedule_records or []
|
|
391
|
+
active_paths: list[str] = []
|
|
392
|
+
upserted = 0
|
|
393
|
+
scheduled = 0
|
|
394
|
+
|
|
395
|
+
for record in script_records:
|
|
396
|
+
path = str(record["path"])
|
|
397
|
+
active_paths.append(path)
|
|
398
|
+
upsert_personal_script(
|
|
399
|
+
name=record.get("name") or Path(path).stem,
|
|
400
|
+
path=path,
|
|
401
|
+
description=record.get("description", ""),
|
|
402
|
+
runtime=record.get("runtime", "unknown"),
|
|
403
|
+
metadata=record.get("metadata", {}),
|
|
404
|
+
created_by=record.get("created_by", "manual"),
|
|
405
|
+
source=record.get("source", "filesystem"),
|
|
406
|
+
enabled=record.get("enabled", True),
|
|
407
|
+
has_inline_metadata=bool(record.get("metadata")),
|
|
408
|
+
)
|
|
409
|
+
upserted += 1
|
|
410
|
+
|
|
411
|
+
pruned_scripts = delete_missing_personal_scripts(active_paths) if prune_missing else 0
|
|
412
|
+
|
|
413
|
+
active_cron_ids: list[str] = []
|
|
414
|
+
for schedule in schedule_records:
|
|
415
|
+
cron_id = schedule.get("cron_id")
|
|
416
|
+
script_path = schedule.get("script_path")
|
|
417
|
+
if not cron_id or not script_path:
|
|
418
|
+
continue
|
|
419
|
+
active_cron_ids.append(cron_id)
|
|
420
|
+
registered = register_personal_script_schedule(
|
|
421
|
+
script_path=script_path,
|
|
422
|
+
cron_id=cron_id,
|
|
423
|
+
schedule_type=schedule.get("schedule_type", ""),
|
|
424
|
+
schedule_value=schedule.get("schedule_value", ""),
|
|
425
|
+
schedule_label=schedule.get("schedule_label", ""),
|
|
426
|
+
launchd_label=schedule.get("launchd_label", ""),
|
|
427
|
+
plist_path=schedule.get("plist_path", ""),
|
|
428
|
+
description=schedule.get("description", ""),
|
|
429
|
+
enabled=schedule.get("enabled", True),
|
|
430
|
+
)
|
|
431
|
+
if registered:
|
|
432
|
+
scheduled += 1
|
|
433
|
+
|
|
434
|
+
pruned_schedules = delete_missing_personal_schedules(active_cron_ids) if prune_missing else 0
|
|
435
|
+
return {
|
|
436
|
+
"ok": True,
|
|
437
|
+
"scripts_upserted": upserted,
|
|
438
|
+
"schedules_upserted": scheduled,
|
|
439
|
+
"scripts_pruned": pruned_scripts,
|
|
440
|
+
"schedules_pruned": pruned_schedules,
|
|
441
|
+
"registered_scripts": len(list_personal_scripts()),
|
|
442
|
+
}
|
|
443
|
+
|
|
444
|
+
|
|
445
|
+
def get_personal_script_health_report(*, fix: bool = False) -> dict:
|
|
446
|
+
if fix:
|
|
447
|
+
from script_registry import reconcile_personal_scripts
|
|
448
|
+
|
|
449
|
+
reconcile_personal_scripts(dry_run=False)
|
|
450
|
+
|
|
451
|
+
from script_registry import classify_scripts_dir, audit_personal_schedules
|
|
452
|
+
|
|
453
|
+
issues: list[dict] = []
|
|
454
|
+
scripts = list_personal_scripts()
|
|
455
|
+
checked = 0
|
|
456
|
+
audit = audit_personal_schedules()
|
|
457
|
+
audit_by_path: dict[str, list[dict]] = {}
|
|
458
|
+
for schedule in audit.get("schedules", []):
|
|
459
|
+
audit_by_path.setdefault(schedule.get("script_path", ""), []).append(schedule)
|
|
460
|
+
|
|
461
|
+
classified = classify_scripts_dir()
|
|
462
|
+
personal_entries = [entry for entry in classified.get("entries", []) if entry.get("classification") == "personal"]
|
|
463
|
+
|
|
464
|
+
for script in scripts:
|
|
465
|
+
checked += 1
|
|
466
|
+
path = Path(script["path"])
|
|
467
|
+
if not path.is_file():
|
|
468
|
+
issues.append({
|
|
469
|
+
"script_id": script["id"],
|
|
470
|
+
"severity": "error",
|
|
471
|
+
"message": f"missing file: {path}",
|
|
472
|
+
})
|
|
473
|
+
for schedule in script.get("schedules", []):
|
|
474
|
+
checked += 1
|
|
475
|
+
plist_path = schedule.get("plist_path", "")
|
|
476
|
+
if plist_path and not Path(plist_path).is_file():
|
|
477
|
+
issues.append({
|
|
478
|
+
"script_id": script["id"],
|
|
479
|
+
"severity": "warn",
|
|
480
|
+
"message": f"missing managed plist for cron {schedule['cron_id']}: {plist_path}",
|
|
481
|
+
})
|
|
482
|
+
|
|
483
|
+
for entry in personal_entries:
|
|
484
|
+
checked += 1
|
|
485
|
+
declared = entry.get("declared_schedule", {})
|
|
486
|
+
if declared.get("required") and not declared.get("valid"):
|
|
487
|
+
issues.append({
|
|
488
|
+
"script_id": f"declared:{entry['name']}",
|
|
489
|
+
"severity": "error",
|
|
490
|
+
"message": f"invalid declared schedule for {entry['name']}: {declared.get('error', 'invalid metadata')}",
|
|
491
|
+
})
|
|
492
|
+
continue
|
|
493
|
+
|
|
494
|
+
if not declared.get("required"):
|
|
495
|
+
continue
|
|
496
|
+
|
|
497
|
+
managed = [
|
|
498
|
+
item for item in audit_by_path.get(entry["path"], [])
|
|
499
|
+
if item.get("schedule_managed")
|
|
500
|
+
]
|
|
501
|
+
if managed:
|
|
502
|
+
continue
|
|
503
|
+
|
|
504
|
+
related = audit_by_path.get(entry["path"], [])
|
|
505
|
+
reason = "no schedule discovered"
|
|
506
|
+
if related:
|
|
507
|
+
states = ", ".join(item.get("schedule_state", item.get("schedule_origin", "unknown")) for item in related)
|
|
508
|
+
reason = f"discovered but not managed ({states})"
|
|
509
|
+
issues.append({
|
|
510
|
+
"script_id": f"declared:{entry['name']}",
|
|
511
|
+
"severity": "warn",
|
|
512
|
+
"message": (
|
|
513
|
+
f"missing declared managed schedule for {entry['name']}: "
|
|
514
|
+
f"{declared.get('schedule_label', declared.get('cron_id', ''))} [{reason}]"
|
|
515
|
+
),
|
|
516
|
+
})
|
|
517
|
+
|
|
518
|
+
for schedule in audit.get("schedules", []):
|
|
519
|
+
checked += 1
|
|
520
|
+
if schedule.get("schedule_managed"):
|
|
521
|
+
continue
|
|
522
|
+
|
|
523
|
+
severity = "warn"
|
|
524
|
+
if schedule.get("schedule_origin") == "orphan_schedule":
|
|
525
|
+
severity = "error"
|
|
526
|
+
elif schedule.get("schedule_declared") and schedule.get("schedule_matches_declared") is False:
|
|
527
|
+
severity = "error"
|
|
528
|
+
|
|
529
|
+
label = schedule.get("schedule_label") or schedule.get("schedule_value") or schedule.get("schedule_type")
|
|
530
|
+
problems = "; ".join(schedule.get("problems", [])) or schedule.get("schedule_state", "schedule issue")
|
|
531
|
+
target = schedule.get("script_name") or schedule.get("script_path") or schedule.get("cron_id")
|
|
532
|
+
issues.append({
|
|
533
|
+
"script_id": target,
|
|
534
|
+
"severity": severity,
|
|
535
|
+
"message": (
|
|
536
|
+
f"{schedule.get('schedule_origin', 'schedule')} {schedule['cron_id']} "
|
|
537
|
+
f"({label}) for {target}: {problems}"
|
|
538
|
+
),
|
|
539
|
+
})
|
|
540
|
+
|
|
541
|
+
return {
|
|
542
|
+
"checked": checked,
|
|
543
|
+
"scripts": len(scripts),
|
|
544
|
+
"schedules": sum(len(script.get("schedules", [])) for script in scripts),
|
|
545
|
+
"issues": issues,
|
|
546
|
+
"fixed": bool(fix),
|
|
547
|
+
"schedule_audit": audit,
|
|
548
|
+
}
|
package/src/db/_schema.py
CHANGED
|
@@ -381,6 +381,49 @@ def _m19_skills_v2(conn):
|
|
|
381
381
|
_migrate_add_column(conn, "skills", "last_reviewed_at", "TEXT DEFAULT NULL")
|
|
382
382
|
|
|
383
383
|
|
|
384
|
+
def _m20_personal_scripts_registry(conn):
|
|
385
|
+
conn.execute("""
|
|
386
|
+
CREATE TABLE IF NOT EXISTS personal_scripts (
|
|
387
|
+
id TEXT PRIMARY KEY,
|
|
388
|
+
name TEXT NOT NULL,
|
|
389
|
+
path TEXT NOT NULL UNIQUE,
|
|
390
|
+
description TEXT DEFAULT '',
|
|
391
|
+
runtime TEXT DEFAULT 'unknown',
|
|
392
|
+
metadata_json TEXT DEFAULT '{}',
|
|
393
|
+
created_by TEXT DEFAULT 'manual',
|
|
394
|
+
source TEXT DEFAULT 'filesystem',
|
|
395
|
+
enabled INTEGER NOT NULL DEFAULT 1,
|
|
396
|
+
has_inline_metadata INTEGER NOT NULL DEFAULT 0,
|
|
397
|
+
last_run_at TEXT DEFAULT NULL,
|
|
398
|
+
last_exit_code INTEGER DEFAULT NULL,
|
|
399
|
+
last_synced_at TEXT DEFAULT (datetime('now')),
|
|
400
|
+
created_at TEXT DEFAULT (datetime('now')),
|
|
401
|
+
updated_at TEXT DEFAULT (datetime('now'))
|
|
402
|
+
)
|
|
403
|
+
""")
|
|
404
|
+
conn.execute("""
|
|
405
|
+
CREATE TABLE IF NOT EXISTS personal_script_schedules (
|
|
406
|
+
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
407
|
+
script_id TEXT NOT NULL REFERENCES personal_scripts(id) ON DELETE CASCADE,
|
|
408
|
+
cron_id TEXT NOT NULL UNIQUE,
|
|
409
|
+
schedule_type TEXT DEFAULT '',
|
|
410
|
+
schedule_value TEXT DEFAULT '',
|
|
411
|
+
schedule_label TEXT DEFAULT '',
|
|
412
|
+
launchd_label TEXT DEFAULT '',
|
|
413
|
+
plist_path TEXT DEFAULT '',
|
|
414
|
+
description TEXT DEFAULT '',
|
|
415
|
+
enabled INTEGER NOT NULL DEFAULT 1,
|
|
416
|
+
last_synced_at TEXT DEFAULT (datetime('now')),
|
|
417
|
+
created_at TEXT DEFAULT (datetime('now')),
|
|
418
|
+
updated_at TEXT DEFAULT (datetime('now'))
|
|
419
|
+
)
|
|
420
|
+
""")
|
|
421
|
+
_migrate_add_index(conn, "idx_personal_scripts_name", "personal_scripts", "name")
|
|
422
|
+
_migrate_add_index(conn, "idx_personal_scripts_enabled", "personal_scripts", "enabled")
|
|
423
|
+
_migrate_add_index(conn, "idx_personal_script_schedules_script", "personal_script_schedules", "script_id")
|
|
424
|
+
_migrate_add_index(conn, "idx_personal_script_schedules_enabled", "personal_script_schedules", "enabled")
|
|
425
|
+
|
|
426
|
+
|
|
384
427
|
MIGRATIONS = [
|
|
385
428
|
(1, "learnings_columns", _m1_learnings_columns),
|
|
386
429
|
(2, "followups_reasoning", _m2_followups_reasoning),
|
|
@@ -401,6 +444,7 @@ MIGRATIONS = [
|
|
|
401
444
|
(17, "cron_runs", _m17_cron_runs),
|
|
402
445
|
(18, "skills_steps_column", _m18_skills_steps),
|
|
403
446
|
(19, "skills_v2", _m19_skills_v2),
|
|
447
|
+
(20, "personal_scripts_registry", _m20_personal_scripts_registry),
|
|
404
448
|
]
|
|
405
449
|
|
|
406
450
|
|
|
@@ -459,4 +503,3 @@ def get_schema_version() -> int:
|
|
|
459
503
|
return row[0] or 0
|
|
460
504
|
except Exception:
|
|
461
505
|
return 0
|
|
462
|
-
|