nexo-brain 7.12.0 → 7.12.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 +1 -1
- package/README.md +2 -2
- package/package.json +1 -1
- package/src/hook_guardrails.py +177 -4
- package/src/scripts/nexo-agent-run.py +12 -0
- package/src/scripts/nexo_personal_automation.py +142 -10
- package/templates/nexo_helper.py +10 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "nexo-brain",
|
|
3
|
-
"version": "7.12.
|
|
3
|
+
"version": "7.12.1",
|
|
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
|
@@ -18,9 +18,9 @@
|
|
|
18
18
|
|
|
19
19
|
[Watch the overview video](https://nexo-brain.com/watch/) · [Watch on YouTube](https://www.youtube.com/watch?v=i2lkGhKyVqI) · [Open the infographic](https://nexo-brain.com/assets/nexo-brain-infographic-v5.png)
|
|
20
20
|
|
|
21
|
-
Version `7.12.
|
|
21
|
+
Version `7.12.1` is the current packaged-runtime line. Patch release — Guardian G3 now sees SSH remote-write intent even when the shell payload arrives through pipe, heredoc, or stdin redirect, recent same-task Cortex decisions unlock the next matching G3 retry for a short TTL, and personal text automations now run as bare one-shots with short timeouts and overlap locks instead of spawning full agent sessions. Result: remote deploy/recovery flows stop dead-ending after `nexo_cortex_decide`, and short cron-owned text jobs stop hanging for hours or stacking parallel subprocesses.
|
|
22
22
|
|
|
23
|
-
Previously in `7.
|
|
23
|
+
Previously in `7.12.0`: minor release — adds `nexo support-snapshot` for generic local runtime diagnostics and completes the silent-reminder hardening on the live Protocol Enforcer path. The support collector emits one JSON bundle with version/platform metadata, runtime path presence, health-check output, and recent event/operation tails, while map-driven reminders (`nexo_startup`, `nexo_smart_startup`, `nexo_heartbeat`, `nexo_reminders`, `nexo_session_diary_*`, `nexo_stop`, `nexo_task_close`, compaction checkpoint prompts) now say explicitly that silence owns the entire reminder turn.
|
|
24
24
|
|
|
25
25
|
Previously in `7.11.5`: patch release — Desktop-managed installs now block the standalone dashboard at the same product-mode layer as evolution, so `installation_live`, cron sync, and watchdog no longer disagree about whether `com.nexo.dashboard` should exist. Validation: `125` targeted tests across product-mode, cron sync, and doctor, plus a full pre-release wrapper (`2321 passed, 2 skipped, 1 xfailed, 4 xpassed`).
|
|
26
26
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "nexo-brain",
|
|
3
|
-
"version": "7.12.
|
|
3
|
+
"version": "7.12.1",
|
|
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/hook_guardrails.py
CHANGED
|
@@ -191,6 +191,14 @@ _SFTP_BATCH_RE = re.compile(
|
|
|
191
191
|
r"\bsftp\b(?:[^|&;]*\s)?-b\s+\S+",
|
|
192
192
|
re.IGNORECASE,
|
|
193
193
|
)
|
|
194
|
+
_SSH_REMOTE_PIPE_RE = re.compile(
|
|
195
|
+
r"\|\s*ssh\b",
|
|
196
|
+
re.IGNORECASE,
|
|
197
|
+
)
|
|
198
|
+
_SSH_REMOTE_STDIN_RE = re.compile(
|
|
199
|
+
r"\bssh\b[^\n|&;]*(?:<\s*\S+|<<-?\s*(?:['\"]?[A-Za-z0-9_]+['\"]?))",
|
|
200
|
+
re.IGNORECASE,
|
|
201
|
+
)
|
|
194
202
|
|
|
195
203
|
|
|
196
204
|
def _classify_ssh_remote_write(command: str) -> str | None:
|
|
@@ -231,6 +239,10 @@ def _classify_ssh_remote_write(command: str) -> str | None:
|
|
|
231
239
|
for pattern in _SSH_REMOTE_WRITE_VERBS:
|
|
232
240
|
if pattern.search(trimmed):
|
|
233
241
|
return "ssh_remote_shell_write"
|
|
242
|
+
if _SSH_REMOTE_PIPE_RE.search(cmd):
|
|
243
|
+
return "ssh_remote_shell_write"
|
|
244
|
+
if _SSH_REMOTE_STDIN_RE.search(cmd):
|
|
245
|
+
return "ssh_remote_shell_write"
|
|
234
246
|
return None
|
|
235
247
|
|
|
236
248
|
|
|
@@ -270,6 +282,64 @@ _PATH_ARTIFACT_RE = re.compile(
|
|
|
270
282
|
)
|
|
271
283
|
_DATE_LIKE_PATH_RE = re.compile(r"^/\d{1,4}/\d{1,4}(?:/\d{1,4})?$")
|
|
272
284
|
_STRICT_WRITE_HEARTBEAT_WINDOW_SECONDS = 300
|
|
285
|
+
_G3_CORTEX_AUTH_WINDOW_SECONDS = max(
|
|
286
|
+
60,
|
|
287
|
+
int(os.environ.get("NEXO_G3_CORTEX_AUTH_WINDOW_SECONDS", "900")),
|
|
288
|
+
)
|
|
289
|
+
_CORTEX_NEGATIVE_TOKENS = (
|
|
290
|
+
"abort",
|
|
291
|
+
"avoid",
|
|
292
|
+
"block",
|
|
293
|
+
"cancel",
|
|
294
|
+
"decline",
|
|
295
|
+
"defer",
|
|
296
|
+
"deny",
|
|
297
|
+
"do_not",
|
|
298
|
+
"dont",
|
|
299
|
+
"no_",
|
|
300
|
+
"not_now",
|
|
301
|
+
"reject",
|
|
302
|
+
"skip",
|
|
303
|
+
"wait",
|
|
304
|
+
)
|
|
305
|
+
_G3_CORTEX_GENERIC_APPROVAL_TOKENS = (
|
|
306
|
+
"allow",
|
|
307
|
+
"apply",
|
|
308
|
+
"approve",
|
|
309
|
+
"continue",
|
|
310
|
+
"deploy",
|
|
311
|
+
"execute",
|
|
312
|
+
"go_ahead",
|
|
313
|
+
"proceed",
|
|
314
|
+
"publish",
|
|
315
|
+
"retry",
|
|
316
|
+
"run",
|
|
317
|
+
)
|
|
318
|
+
_G3_CORTEX_FAMILY_TOKENS = {
|
|
319
|
+
"destructive": (
|
|
320
|
+
"chmod",
|
|
321
|
+
"cleanup",
|
|
322
|
+
"delete",
|
|
323
|
+
"drop",
|
|
324
|
+
"force",
|
|
325
|
+
"git_push",
|
|
326
|
+
"purge",
|
|
327
|
+
"remove",
|
|
328
|
+
"rm",
|
|
329
|
+
"truncate",
|
|
330
|
+
"wipe",
|
|
331
|
+
),
|
|
332
|
+
"ssh": (
|
|
333
|
+
"deploy",
|
|
334
|
+
"remote",
|
|
335
|
+
"rsync",
|
|
336
|
+
"scp",
|
|
337
|
+
"sftp",
|
|
338
|
+
"ssh",
|
|
339
|
+
"sync",
|
|
340
|
+
"upload",
|
|
341
|
+
),
|
|
342
|
+
}
|
|
273
343
|
|
|
274
344
|
# Single-segment ``/word`` candidates that match a small dictionary block-list
|
|
275
345
|
# of confirmed false positives observed in the live debt log.
|
|
@@ -655,6 +725,7 @@ def _find_open_task_for_file(conn, sid: str, filepath: str) -> dict | None:
|
|
|
655
725
|
target = _normalize_file_path(filepath)
|
|
656
726
|
rows = conn.execute(
|
|
657
727
|
"""SELECT task_id, files, guard_has_blocking, guard_acknowledged, task_type, plan, unknowns,
|
|
728
|
+
opened_at,
|
|
658
729
|
verification_step, opened_with_guard, must_change_log, must_verify
|
|
659
730
|
FROM protocol_tasks
|
|
660
731
|
WHERE session_id = ? AND status = 'open'
|
|
@@ -675,6 +746,7 @@ def _find_open_task_for_file(conn, sid: str, filepath: str) -> dict | None:
|
|
|
675
746
|
def _find_any_open_task(conn, sid: str) -> dict | None:
|
|
676
747
|
row = conn.execute(
|
|
677
748
|
"""SELECT task_id, files, guard_has_blocking, guard_acknowledged, task_type, plan, unknowns,
|
|
749
|
+
opened_at,
|
|
678
750
|
verification_step, opened_with_guard, must_change_log, must_verify
|
|
679
751
|
FROM protocol_tasks
|
|
680
752
|
WHERE session_id = ? AND status = 'open'
|
|
@@ -685,6 +757,86 @@ def _find_any_open_task(conn, sid: str) -> dict | None:
|
|
|
685
757
|
return dict(row) if row else None
|
|
686
758
|
|
|
687
759
|
|
|
760
|
+
def _normalize_cortex_tokens(value: str) -> str:
|
|
761
|
+
return re.sub(r"[^a-z0-9]+", "_", str(value or "").lower()).strip("_")
|
|
762
|
+
|
|
763
|
+
|
|
764
|
+
def _text_has_any_token(value: str, tokens: tuple[str, ...]) -> bool:
|
|
765
|
+
normalized = _normalize_cortex_tokens(value)
|
|
766
|
+
if not normalized:
|
|
767
|
+
return False
|
|
768
|
+
return any(token in normalized for token in tokens)
|
|
769
|
+
|
|
770
|
+
|
|
771
|
+
def _cortex_choice_is_negative(value: str) -> bool:
|
|
772
|
+
return _text_has_any_token(value, _CORTEX_NEGATIVE_TOKENS)
|
|
773
|
+
|
|
774
|
+
|
|
775
|
+
def _find_recent_cortex_authorization(
|
|
776
|
+
conn,
|
|
777
|
+
*,
|
|
778
|
+
sid: str,
|
|
779
|
+
task: dict | None,
|
|
780
|
+
gate_family: str,
|
|
781
|
+
pattern_name: str = "",
|
|
782
|
+
) -> dict | None:
|
|
783
|
+
if not sid or not task:
|
|
784
|
+
return None
|
|
785
|
+
task_id = str(task.get("task_id") or "").strip()
|
|
786
|
+
if not task_id:
|
|
787
|
+
return None
|
|
788
|
+
params: list[object] = [
|
|
789
|
+
sid,
|
|
790
|
+
task_id,
|
|
791
|
+
f"-{_G3_CORTEX_AUTH_WINDOW_SECONDS} seconds",
|
|
792
|
+
]
|
|
793
|
+
sql = (
|
|
794
|
+
"""SELECT id, task_id, recommended_choice, selected_choice,
|
|
795
|
+
recommended_reasoning, selection_reason, context_hint, created_at
|
|
796
|
+
FROM cortex_evaluations
|
|
797
|
+
WHERE session_id = ?
|
|
798
|
+
AND task_id = ?
|
|
799
|
+
AND created_at >= datetime('now', ?)"""
|
|
800
|
+
)
|
|
801
|
+
opened_at = str(task.get("opened_at") or "").strip()
|
|
802
|
+
if opened_at:
|
|
803
|
+
sql += " AND created_at >= ?"
|
|
804
|
+
params.append(opened_at)
|
|
805
|
+
sql += " ORDER BY created_at DESC, id DESC LIMIT 5"
|
|
806
|
+
try:
|
|
807
|
+
rows = conn.execute(sql, params).fetchall()
|
|
808
|
+
except sqlite3.OperationalError:
|
|
809
|
+
return None
|
|
810
|
+
family_tokens = _G3_CORTEX_FAMILY_TOKENS.get(gate_family, ())
|
|
811
|
+
pattern_tokens = tuple(
|
|
812
|
+
token for token in _normalize_cortex_tokens(pattern_name).split("_") if token
|
|
813
|
+
)
|
|
814
|
+
fallback_candidates: list[dict] = []
|
|
815
|
+
for row in rows:
|
|
816
|
+
item = dict(row)
|
|
817
|
+
choice = str(item.get("selected_choice") or item.get("recommended_choice") or "").strip()
|
|
818
|
+
if not choice or _cortex_choice_is_negative(choice):
|
|
819
|
+
continue
|
|
820
|
+
combined = " ".join(
|
|
821
|
+
[
|
|
822
|
+
choice,
|
|
823
|
+
str(item.get("selection_reason") or ""),
|
|
824
|
+
str(item.get("recommended_reasoning") or ""),
|
|
825
|
+
str(item.get("context_hint") or ""),
|
|
826
|
+
]
|
|
827
|
+
)
|
|
828
|
+
if (
|
|
829
|
+
_text_has_any_token(choice, _G3_CORTEX_GENERIC_APPROVAL_TOKENS)
|
|
830
|
+
or _text_has_any_token(combined, family_tokens)
|
|
831
|
+
or _text_has_any_token(combined, pattern_tokens)
|
|
832
|
+
):
|
|
833
|
+
return item
|
|
834
|
+
fallback_candidates.append(item)
|
|
835
|
+
if len(fallback_candidates) == 1:
|
|
836
|
+
return fallback_candidates[0]
|
|
837
|
+
return None
|
|
838
|
+
|
|
839
|
+
|
|
688
840
|
def _find_any_open_workflow(conn, sid: str) -> dict | None:
|
|
689
841
|
row = conn.execute(
|
|
690
842
|
"""SELECT run_id, protocol_task_id, current_step_key
|
|
@@ -1164,6 +1316,7 @@ def process_pre_tool_event(payload: dict) -> dict:
|
|
|
1164
1316
|
if not claude_sid:
|
|
1165
1317
|
claude_sid = _read_claude_session_id_from_coordination()
|
|
1166
1318
|
sid = _resolve_nexo_sid(conn, claude_sid)
|
|
1319
|
+
open_task = _find_any_open_task(conn, sid) if sid else None
|
|
1167
1320
|
automation_blocks = _collect_automation_live_repo_blocks(
|
|
1168
1321
|
conn,
|
|
1169
1322
|
sid=sid,
|
|
@@ -1228,12 +1381,22 @@ def process_pre_tool_event(payload: dict) -> dict:
|
|
|
1228
1381
|
if g3_mode in {"shadow", "hard"} and tool_name == "Bash":
|
|
1229
1382
|
shell_command = _extract_bash_command(tool_input)
|
|
1230
1383
|
destructive_pattern = _classify_destructive_intent(shell_command)
|
|
1384
|
+
if destructive_pattern:
|
|
1385
|
+
if _find_recent_cortex_authorization(
|
|
1386
|
+
conn,
|
|
1387
|
+
sid=sid,
|
|
1388
|
+
task=open_task,
|
|
1389
|
+
gate_family="destructive",
|
|
1390
|
+
pattern_name=destructive_pattern,
|
|
1391
|
+
):
|
|
1392
|
+
destructive_pattern = None
|
|
1231
1393
|
if destructive_pattern:
|
|
1232
1394
|
severity = "error" if g3_mode == "hard" else "warn"
|
|
1395
|
+
task_id = str((open_task or {}).get("task_id") or "").strip()
|
|
1233
1396
|
debt = _ensure_protocol_debt(
|
|
1234
1397
|
conn,
|
|
1235
1398
|
session_id=sid,
|
|
1236
|
-
task_id=
|
|
1399
|
+
task_id=task_id,
|
|
1237
1400
|
debt_type="g3_destructive_command_requires_cortex",
|
|
1238
1401
|
severity=severity,
|
|
1239
1402
|
evidence=(
|
|
@@ -1253,7 +1416,7 @@ def process_pre_tool_event(payload: dict) -> dict:
|
|
|
1253
1416
|
"blocks": [
|
|
1254
1417
|
{
|
|
1255
1418
|
"file": "",
|
|
1256
|
-
"task_id":
|
|
1419
|
+
"task_id": task_id,
|
|
1257
1420
|
"debt_id": debt.get("id"),
|
|
1258
1421
|
"debt_type": "g3_destructive_command_requires_cortex",
|
|
1259
1422
|
"reason_code": "g3_destructive_blocked",
|
|
@@ -1283,12 +1446,22 @@ def process_pre_tool_event(payload: dict) -> dict:
|
|
|
1283
1446
|
if g3_ssh_mode in {"shadow", "hard"} and tool_name == "Bash":
|
|
1284
1447
|
shell_command = _extract_bash_command(tool_input)
|
|
1285
1448
|
ssh_pattern = _classify_ssh_remote_write(shell_command)
|
|
1449
|
+
if ssh_pattern:
|
|
1450
|
+
if _find_recent_cortex_authorization(
|
|
1451
|
+
conn,
|
|
1452
|
+
sid=sid,
|
|
1453
|
+
task=open_task,
|
|
1454
|
+
gate_family="ssh",
|
|
1455
|
+
pattern_name=ssh_pattern,
|
|
1456
|
+
):
|
|
1457
|
+
ssh_pattern = None
|
|
1286
1458
|
if ssh_pattern:
|
|
1287
1459
|
severity = "error" if g3_ssh_mode == "hard" else "warn"
|
|
1460
|
+
task_id = str((open_task or {}).get("task_id") or "").strip()
|
|
1288
1461
|
debt = _ensure_protocol_debt(
|
|
1289
1462
|
conn,
|
|
1290
1463
|
session_id=sid,
|
|
1291
|
-
task_id=
|
|
1464
|
+
task_id=task_id,
|
|
1292
1465
|
debt_type="g3_ssh_remote_write_requires_cortex",
|
|
1293
1466
|
severity=severity,
|
|
1294
1467
|
evidence=(
|
|
@@ -1309,7 +1482,7 @@ def process_pre_tool_event(payload: dict) -> dict:
|
|
|
1309
1482
|
"blocks": [
|
|
1310
1483
|
{
|
|
1311
1484
|
"file": "",
|
|
1312
|
-
"task_id":
|
|
1485
|
+
"task_id": task_id,
|
|
1313
1486
|
"debt_id": debt.get("id"),
|
|
1314
1487
|
"debt_type": "g3_ssh_remote_write_requires_cortex",
|
|
1315
1488
|
"reason_code": "g3_ssh_remote_write_blocked",
|
|
@@ -50,6 +50,12 @@ def main(argv: list[str] | None = None) -> int:
|
|
|
50
50
|
parser.add_argument("--timeout", type=int, default=AUTOMATION_SUBPROCESS_TIMEOUT, help="Timeout in seconds")
|
|
51
51
|
parser.add_argument("--output-format", default="text", help="Requested output format")
|
|
52
52
|
parser.add_argument("--allowed-tools", default="", help="Claude-style allowed tools contract")
|
|
53
|
+
parser.add_argument(
|
|
54
|
+
"--bare-mode",
|
|
55
|
+
choices=("auto", "on", "off"),
|
|
56
|
+
default="auto",
|
|
57
|
+
help="Bare mode for one-shot runs: auto|on|off.",
|
|
58
|
+
)
|
|
53
59
|
parser.add_argument("--append-system-prompt", default="", help="Extra system prompt text")
|
|
54
60
|
parser.add_argument("--append-system-prompt-file", default="", help="Read extra system prompt from a file")
|
|
55
61
|
args = parser.parse_args(argv)
|
|
@@ -62,6 +68,11 @@ def main(argv: list[str] | None = None) -> int:
|
|
|
62
68
|
return 1
|
|
63
69
|
|
|
64
70
|
append_system_prompt = args.append_system_prompt or _read_text(args.append_system_prompt_file)
|
|
71
|
+
bare_mode = None
|
|
72
|
+
if args.bare_mode == "on":
|
|
73
|
+
bare_mode = True
|
|
74
|
+
elif args.bare_mode == "off":
|
|
75
|
+
bare_mode = False
|
|
65
76
|
|
|
66
77
|
try:
|
|
67
78
|
result = run_automation_prompt(
|
|
@@ -76,6 +87,7 @@ def main(argv: list[str] | None = None) -> int:
|
|
|
76
87
|
output_format=args.output_format,
|
|
77
88
|
append_system_prompt=append_system_prompt,
|
|
78
89
|
allowed_tools=args.allowed_tools,
|
|
90
|
+
bare_mode=bare_mode,
|
|
79
91
|
)
|
|
80
92
|
except AutomationBackendUnavailableError as exc:
|
|
81
93
|
print(str(exc), file=sys.stderr)
|
|
@@ -17,8 +17,11 @@ Both paths are probed so dev and live operators get identical behaviour.
|
|
|
17
17
|
"""
|
|
18
18
|
from __future__ import annotations
|
|
19
19
|
|
|
20
|
+
import inspect
|
|
20
21
|
import os
|
|
22
|
+
import re
|
|
21
23
|
import sys
|
|
24
|
+
import time
|
|
22
25
|
from pathlib import Path
|
|
23
26
|
|
|
24
27
|
|
|
@@ -31,6 +34,12 @@ if str(_repo_src) not in sys.path:
|
|
|
31
34
|
|
|
32
35
|
NEXO_HOME = Path(os.environ.get("NEXO_HOME", str(Path.home() / ".nexo")))
|
|
33
36
|
DEFAULT_ALLOWED_TOOLS = "Read,Write,Edit,Glob,Grep,Bash,mcp__nexo__*"
|
|
37
|
+
DEFAULT_SHORT_TEXT_ALLOWED_TOOLS = ""
|
|
38
|
+
DEFAULT_SHORT_TEXT_TIMEOUT = max(
|
|
39
|
+
30,
|
|
40
|
+
int(os.environ.get("NEXO_PERSONAL_AUTOMATION_TIMEOUT", "180")),
|
|
41
|
+
)
|
|
42
|
+
_PROCESS_LOCK_COUNTS: dict[str, int] = {}
|
|
34
43
|
|
|
35
44
|
# Templates live next to the code at repo time and at ``~/.nexo/templates``
|
|
36
45
|
# once installed. Probe both and surface whichever exists first so the
|
|
@@ -50,14 +59,126 @@ except Exception:
|
|
|
50
59
|
from nexo_helper import run_automation_text as _run_automation_text
|
|
51
60
|
|
|
52
61
|
|
|
62
|
+
def _infer_personal_caller() -> str:
|
|
63
|
+
env_caller = str(os.environ.get("NEXO_AUTOMATION_CALLER") or "").strip()
|
|
64
|
+
if env_caller:
|
|
65
|
+
return env_caller
|
|
66
|
+
candidates: list[Path] = []
|
|
67
|
+
argv0 = str(sys.argv[0] or "").strip()
|
|
68
|
+
if argv0:
|
|
69
|
+
candidates.append(Path(argv0).expanduser())
|
|
70
|
+
current = Path(__file__).resolve()
|
|
71
|
+
for frame in inspect.stack()[1:]:
|
|
72
|
+
try:
|
|
73
|
+
path = Path(frame.filename).resolve()
|
|
74
|
+
except Exception:
|
|
75
|
+
continue
|
|
76
|
+
if path != current:
|
|
77
|
+
candidates.append(path)
|
|
78
|
+
for candidate in candidates:
|
|
79
|
+
parts = candidate.parts
|
|
80
|
+
if "personal" in parts and "scripts" in parts:
|
|
81
|
+
stem = candidate.stem.strip()
|
|
82
|
+
if stem:
|
|
83
|
+
return f"personal/{stem}"
|
|
84
|
+
if argv0:
|
|
85
|
+
stem = Path(argv0).stem.strip()
|
|
86
|
+
if stem and stem not in {"python", "python3", "-m"}:
|
|
87
|
+
return f"personal/{stem}"
|
|
88
|
+
return "agent_run/generic"
|
|
89
|
+
|
|
90
|
+
|
|
91
|
+
def _caller_lock_path(caller: str) -> Path:
|
|
92
|
+
slug = re.sub(r"[^A-Za-z0-9_.-]+", "-", caller).strip("-") or "generic"
|
|
93
|
+
return NEXO_HOME / "runtime" / "locks" / "personal-automation" / f"{slug}.lock"
|
|
94
|
+
|
|
95
|
+
|
|
96
|
+
def _read_lock_pid(path: Path) -> int:
|
|
97
|
+
try:
|
|
98
|
+
raw = path.read_text().splitlines()
|
|
99
|
+
except Exception:
|
|
100
|
+
return 0
|
|
101
|
+
if not raw:
|
|
102
|
+
return 0
|
|
103
|
+
try:
|
|
104
|
+
return int(raw[0].strip())
|
|
105
|
+
except Exception:
|
|
106
|
+
return 0
|
|
107
|
+
|
|
108
|
+
|
|
109
|
+
def _pid_is_alive(pid: int) -> bool:
|
|
110
|
+
if pid <= 0:
|
|
111
|
+
return False
|
|
112
|
+
try:
|
|
113
|
+
os.kill(pid, 0)
|
|
114
|
+
except ProcessLookupError:
|
|
115
|
+
return False
|
|
116
|
+
except PermissionError:
|
|
117
|
+
return True
|
|
118
|
+
return True
|
|
119
|
+
|
|
120
|
+
|
|
121
|
+
def _acquire_personal_caller_lock(caller: str) -> str:
|
|
122
|
+
clean = str(caller or "").strip()
|
|
123
|
+
if not clean.startswith("personal/"):
|
|
124
|
+
return ""
|
|
125
|
+
if _PROCESS_LOCK_COUNTS.get(clean, 0) > 0:
|
|
126
|
+
_PROCESS_LOCK_COUNTS[clean] += 1
|
|
127
|
+
return clean
|
|
128
|
+
pid = os.getpid()
|
|
129
|
+
lock_path = _caller_lock_path(clean)
|
|
130
|
+
lock_path.parent.mkdir(parents=True, exist_ok=True)
|
|
131
|
+
while True:
|
|
132
|
+
try:
|
|
133
|
+
fd = os.open(str(lock_path), os.O_CREAT | os.O_EXCL | os.O_WRONLY)
|
|
134
|
+
except FileExistsError:
|
|
135
|
+
existing_pid = _read_lock_pid(lock_path)
|
|
136
|
+
if existing_pid == pid:
|
|
137
|
+
_PROCESS_LOCK_COUNTS[clean] = 1
|
|
138
|
+
return clean
|
|
139
|
+
if existing_pid and _pid_is_alive(existing_pid):
|
|
140
|
+
raise RuntimeError(
|
|
141
|
+
f"Automation caller {clean} already has a live run (pid {existing_pid})."
|
|
142
|
+
)
|
|
143
|
+
try:
|
|
144
|
+
lock_path.unlink()
|
|
145
|
+
except FileNotFoundError:
|
|
146
|
+
pass
|
|
147
|
+
continue
|
|
148
|
+
with os.fdopen(fd, "w", encoding="ascii") as handle:
|
|
149
|
+
handle.write(f"{pid}\n{int(time.time())}\n{clean}\n")
|
|
150
|
+
_PROCESS_LOCK_COUNTS[clean] = 1
|
|
151
|
+
return clean
|
|
152
|
+
|
|
153
|
+
|
|
154
|
+
def _release_personal_caller_lock(caller: str) -> None:
|
|
155
|
+
clean = str(caller or "").strip()
|
|
156
|
+
if not clean.startswith("personal/"):
|
|
157
|
+
return
|
|
158
|
+
count = _PROCESS_LOCK_COUNTS.get(clean, 0)
|
|
159
|
+
if count > 1:
|
|
160
|
+
_PROCESS_LOCK_COUNTS[clean] = count - 1
|
|
161
|
+
return
|
|
162
|
+
_PROCESS_LOCK_COUNTS.pop(clean, None)
|
|
163
|
+
lock_path = _caller_lock_path(clean)
|
|
164
|
+
if _read_lock_pid(lock_path) == os.getpid():
|
|
165
|
+
try:
|
|
166
|
+
lock_path.unlink()
|
|
167
|
+
except FileNotFoundError:
|
|
168
|
+
pass
|
|
169
|
+
|
|
170
|
+
|
|
53
171
|
def run_personal_automation_text(
|
|
54
172
|
prompt: str,
|
|
55
173
|
*,
|
|
56
174
|
model: str = "",
|
|
57
175
|
cwd: str = "",
|
|
58
|
-
timeout: int =
|
|
59
|
-
allowed_tools: str =
|
|
176
|
+
timeout: int = DEFAULT_SHORT_TEXT_TIMEOUT,
|
|
177
|
+
allowed_tools: str = DEFAULT_SHORT_TEXT_ALLOWED_TOOLS,
|
|
60
178
|
append_system_prompt: str = "",
|
|
179
|
+
caller: str = "",
|
|
180
|
+
tier: str = "",
|
|
181
|
+
bare_mode: bool | None = True,
|
|
61
182
|
) -> str:
|
|
62
183
|
"""Run ``prompt`` through the configured NEXO automation backend.
|
|
63
184
|
|
|
@@ -68,18 +189,29 @@ def run_personal_automation_text(
|
|
|
68
189
|
Every other kwarg passes through verbatim.
|
|
69
190
|
"""
|
|
70
191
|
effective_model = model or _USER_MODEL or "opus"
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
192
|
+
effective_caller = caller or _infer_personal_caller()
|
|
193
|
+
lock_token = _acquire_personal_caller_lock(effective_caller)
|
|
194
|
+
try:
|
|
195
|
+
return _run_automation_text(
|
|
196
|
+
prompt,
|
|
197
|
+
model=effective_model,
|
|
198
|
+
cwd=cwd or "",
|
|
199
|
+
timeout=timeout,
|
|
200
|
+
allowed_tools=allowed_tools,
|
|
201
|
+
append_system_prompt=append_system_prompt,
|
|
202
|
+
include_bootstrap=False,
|
|
203
|
+
caller=effective_caller,
|
|
204
|
+
tier=tier,
|
|
205
|
+
bare_mode=bare_mode,
|
|
206
|
+
)
|
|
207
|
+
finally:
|
|
208
|
+
_release_personal_caller_lock(lock_token)
|
|
79
209
|
|
|
80
210
|
|
|
81
211
|
__all__ = [
|
|
82
212
|
"DEFAULT_ALLOWED_TOOLS",
|
|
213
|
+
"DEFAULT_SHORT_TEXT_ALLOWED_TOOLS",
|
|
214
|
+
"DEFAULT_SHORT_TEXT_TIMEOUT",
|
|
83
215
|
"NEXO_HOME",
|
|
84
216
|
"run_personal_automation_text",
|
|
85
217
|
]
|
package/templates/nexo_helper.py
CHANGED
|
@@ -226,6 +226,7 @@ def run_automation_text(
|
|
|
226
226
|
include_bootstrap: bool = True,
|
|
227
227
|
caller: str = "",
|
|
228
228
|
tier: str = "",
|
|
229
|
+
bare_mode: bool | None = None,
|
|
229
230
|
) -> str:
|
|
230
231
|
"""Run the configured NEXO automation backend and return text output.
|
|
231
232
|
|
|
@@ -264,6 +265,10 @@ def run_automation_text(
|
|
|
264
265
|
cmd.extend(["--append-system-prompt", "\n\n".join(merged_system_prompt)])
|
|
265
266
|
if allowed_tools:
|
|
266
267
|
cmd.extend(["--allowed-tools", allowed_tools])
|
|
268
|
+
if bare_mode is True:
|
|
269
|
+
cmd.extend(["--bare-mode", "on"])
|
|
270
|
+
elif bare_mode is False:
|
|
271
|
+
cmd.extend(["--bare-mode", "off"])
|
|
267
272
|
|
|
268
273
|
env = os.environ.copy()
|
|
269
274
|
env.setdefault("NEXO_HOME", str(NEXO_HOME))
|
|
@@ -292,6 +297,7 @@ def run_automation_json(
|
|
|
292
297
|
include_bootstrap: bool = True,
|
|
293
298
|
caller: str = "",
|
|
294
299
|
tier: str = "",
|
|
300
|
+
bare_mode: bool | None = None,
|
|
295
301
|
) -> dict:
|
|
296
302
|
"""Run the configured backend and return a parsed JSON object.
|
|
297
303
|
|
|
@@ -326,6 +332,10 @@ def run_automation_json(
|
|
|
326
332
|
cmd.extend(["--append-system-prompt", "\n\n".join(merged_system_prompt)])
|
|
327
333
|
if allowed_tools:
|
|
328
334
|
cmd.extend(["--allowed-tools", allowed_tools])
|
|
335
|
+
if bare_mode is True:
|
|
336
|
+
cmd.extend(["--bare-mode", "on"])
|
|
337
|
+
elif bare_mode is False:
|
|
338
|
+
cmd.extend(["--bare-mode", "off"])
|
|
329
339
|
|
|
330
340
|
env = os.environ.copy()
|
|
331
341
|
env.setdefault("NEXO_HOME", str(NEXO_HOME))
|