nexo-brain 7.12.14 → 7.12.15

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": "7.12.14",
3
+ "version": "7.12.15",
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,7 +18,7 @@
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.14` is the current packaged-runtime line. Patch release — dashboard followup execution now respects the selected terminal client across macOS, Windows via WSL, and Linux instead of returning a macOS-only dead end. Result: operators can launch followups from the dashboard on every supported Brain host without hardcoding Claude-only or Mac-only assumptions.
21
+ Version `7.12.15` is the current packaged-runtime line. Patch release — same-version packaged updates now still run the safe maintenance path, Deep Sleep clears process locks on shutdown, sent replies are recorded in durable continuity, and personal script schedule-marker drift is surfaced during reconcile. Result: coordinated Desktop bundles can refresh Brain safely without breaking install/update parity on macOS, Windows via WSL, or Linux.
22
22
 
23
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
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "nexo-brain",
3
- "version": "7.12.14",
3
+ "version": "7.12.15",
4
4
  "mcpName": "io.github.wazionapps/nexo",
5
5
  "description": "NEXO Brain \u2014 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",
@@ -0,0 +1,222 @@
1
+ """Durable sent-email continuity for NEXO automations.
2
+
3
+ The email monitor tracks inbound lifecycle rows. This module tracks outbound
4
+ messages so startup, duplicate checks, and briefings can see what NEXO already
5
+ sent even when the send path did not originate from an inbound email row.
6
+ """
7
+
8
+ from __future__ import annotations
9
+
10
+ import json
11
+ import sqlite3
12
+ from datetime import datetime, timedelta
13
+ from pathlib import Path
14
+ from typing import Any
15
+
16
+ import paths
17
+
18
+
19
+ EMAIL_SENT_TABLE_SQL = """
20
+ CREATE TABLE IF NOT EXISTS sent_email_events (
21
+ id INTEGER PRIMARY KEY AUTOINCREMENT,
22
+ message_id TEXT,
23
+ sender TEXT,
24
+ to_addrs TEXT NOT NULL DEFAULT '',
25
+ cc_addrs TEXT NOT NULL DEFAULT '',
26
+ subject TEXT NOT NULL DEFAULT '',
27
+ in_reply_to TEXT NOT NULL DEFAULT '',
28
+ references_header TEXT NOT NULL DEFAULT '',
29
+ source TEXT NOT NULL DEFAULT '',
30
+ status TEXT NOT NULL DEFAULT 'sent',
31
+ sent_at TEXT NOT NULL DEFAULT (datetime('now','localtime')),
32
+ meta TEXT NOT NULL DEFAULT '{}'
33
+ );
34
+ CREATE UNIQUE INDEX IF NOT EXISTS idx_sent_email_message_id
35
+ ON sent_email_events(message_id)
36
+ WHERE message_id IS NOT NULL AND message_id != '';
37
+ CREATE INDEX IF NOT EXISTS idx_sent_email_sent_at ON sent_email_events(sent_at);
38
+ CREATE INDEX IF NOT EXISTS idx_sent_email_subject ON sent_email_events(subject);
39
+ """
40
+
41
+ RECENT_SENT_EMAILS_TITLE = "EMAILS ENVIADOS ULTIMAS 24H POR LA OPERATIVA"
42
+
43
+
44
+ def sent_email_db_path() -> Path:
45
+ return paths.nexo_email_dir() / "nexo-email.db"
46
+
47
+
48
+ def ensure_sent_email_table(conn: sqlite3.Connection) -> None:
49
+ conn.executescript(EMAIL_SENT_TABLE_SQL)
50
+
51
+
52
+ def _connect(db_path: str | Path | None = None) -> sqlite3.Connection:
53
+ path = Path(db_path) if db_path else sent_email_db_path()
54
+ path.parent.mkdir(parents=True, exist_ok=True)
55
+ conn = sqlite3.connect(path)
56
+ conn.row_factory = sqlite3.Row
57
+ ensure_sent_email_table(conn)
58
+ return conn
59
+
60
+
61
+ def _clean(value: object, limit: int = 500) -> str:
62
+ text = " ".join(str(value or "").split())
63
+ if len(text) <= limit:
64
+ return text
65
+ return text[: limit - 3].rstrip() + "..."
66
+
67
+
68
+ def _safe_meta(meta: dict[str, Any] | None) -> str:
69
+ try:
70
+ return json.dumps(meta or {}, ensure_ascii=True, sort_keys=True)
71
+ except Exception:
72
+ return "{}"
73
+
74
+
75
+ def _record_cognitive_memory(event: dict[str, str]) -> None:
76
+ try:
77
+ import cognitive
78
+
79
+ to_value = event.get("to_addrs", "")
80
+ subject = event.get("subject", "")
81
+ message_id = event.get("message_id", "")
82
+ content = (
83
+ "Sent email recorded by NEXO. "
84
+ f"To: {to_value}. Subject: {subject}. Message-ID: {message_id}."
85
+ )
86
+ cognitive.ingest_to_ltm(
87
+ content,
88
+ source_type="email_sent",
89
+ source_id=message_id or f"{to_value}:{subject}",
90
+ source_title=subject,
91
+ domain="email",
92
+ tags="email,sent,continuity",
93
+ bypass_gate=True,
94
+ )
95
+ except Exception:
96
+ pass
97
+
98
+
99
+ def record_sent_email(
100
+ *,
101
+ message_id: str = "",
102
+ sender: str = "",
103
+ to_addrs: str = "",
104
+ cc_addrs: str = "",
105
+ subject: str = "",
106
+ in_reply_to: str = "",
107
+ references_header: str = "",
108
+ source: str = "",
109
+ status: str = "sent",
110
+ meta: dict[str, Any] | None = None,
111
+ db_path: str | Path | None = None,
112
+ record_memory: bool = True,
113
+ ) -> dict[str, str]:
114
+ event = {
115
+ "message_id": _clean(message_id, 300),
116
+ "sender": _clean(sender, 300),
117
+ "to_addrs": _clean(to_addrs, 800),
118
+ "cc_addrs": _clean(cc_addrs, 800),
119
+ "subject": _clean(subject, 500),
120
+ "in_reply_to": _clean(in_reply_to, 300),
121
+ "references_header": _clean(references_header, 1000),
122
+ "source": _clean(source or "unknown", 120),
123
+ "status": _clean(status or "sent", 80),
124
+ "meta": _safe_meta(meta),
125
+ }
126
+ conn = _connect(db_path)
127
+ try:
128
+ conn.execute(
129
+ """
130
+ INSERT OR REPLACE INTO sent_email_events (
131
+ message_id, sender, to_addrs, cc_addrs, subject, in_reply_to,
132
+ references_header, source, status, meta
133
+ )
134
+ VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
135
+ """,
136
+ (
137
+ event["message_id"],
138
+ event["sender"],
139
+ event["to_addrs"],
140
+ event["cc_addrs"],
141
+ event["subject"],
142
+ event["in_reply_to"],
143
+ event["references_header"],
144
+ event["source"],
145
+ event["status"],
146
+ event["meta"],
147
+ ),
148
+ )
149
+ conn.commit()
150
+ finally:
151
+ conn.close()
152
+
153
+ if record_memory:
154
+ _record_cognitive_memory(event)
155
+ return event
156
+
157
+
158
+ def recent_sent_emails(
159
+ *,
160
+ hours: int = 24,
161
+ limit: int = 10,
162
+ db_path: str | Path | None = None,
163
+ ) -> list[dict[str, str]]:
164
+ cutoff = (datetime.now() - timedelta(hours=max(1, int(hours)))).strftime("%Y-%m-%d %H:%M:%S")
165
+ conn = _connect(db_path)
166
+ try:
167
+ rows = conn.execute(
168
+ """
169
+ SELECT message_id, sender, to_addrs, cc_addrs, subject, in_reply_to,
170
+ references_header, source, status, sent_at, meta
171
+ FROM sent_email_events
172
+ WHERE sent_at >= ?
173
+ ORDER BY sent_at DESC
174
+ LIMIT ?
175
+ """,
176
+ (cutoff, max(1, int(limit))),
177
+ ).fetchall()
178
+ return [dict(row) for row in rows]
179
+ finally:
180
+ conn.close()
181
+
182
+
183
+ def find_sent_email(
184
+ *,
185
+ to_addr: str = "",
186
+ subject: str = "",
187
+ since_hours: int = 72,
188
+ db_path: str | Path | None = None,
189
+ ) -> dict[str, str] | None:
190
+ target_to = _clean(to_addr).lower()
191
+ target_subject = _clean(subject).lower()
192
+ if not target_to or not target_subject:
193
+ return None
194
+ for event in recent_sent_emails(hours=since_hours, limit=100, db_path=db_path):
195
+ if target_to in str(event.get("to_addrs") or "").lower() and target_subject in str(event.get("subject") or "").lower():
196
+ return event
197
+ return None
198
+
199
+
200
+ def format_recent_sent_email_block(*, hours: int = 24, limit: int = 8) -> str:
201
+ rows = recent_sent_emails(hours=hours, limit=limit)
202
+ if not rows:
203
+ return ""
204
+ lines = [f"== {RECENT_SENT_EMAILS_TITLE} =="]
205
+ for row in rows:
206
+ sent_at = str(row.get("sent_at") or "")
207
+ to_value = _clean(row.get("to_addrs"), 120)
208
+ subject = _clean(row.get("subject"), 160)
209
+ source = _clean(row.get("source"), 80)
210
+ lines.append(f"- {sent_at} | to: {to_value} | subject: {subject} | source: {source}")
211
+ return "\n".join(lines)
212
+
213
+
214
+ __all__ = [
215
+ "RECENT_SENT_EMAILS_TITLE",
216
+ "ensure_sent_email_table",
217
+ "find_sent_email",
218
+ "format_recent_sent_email_block",
219
+ "recent_sent_emails",
220
+ "record_sent_email",
221
+ "sent_email_db_path",
222
+ ]
@@ -1137,19 +1137,23 @@ def _handle_packaged_update(progress_fn=None, *, include_clis: bool = True) -> s
1137
1137
  return msg
1138
1138
 
1139
1139
  new_version = _read_version()
1140
- if old_version == new_version:
1141
- return f"Already up to date (v{old_version}). No changes."
1140
+ version_changed = old_version != new_version
1141
+ if not version_changed:
1142
+ _emit_progress(progress_fn, "Package version unchanged; running idempotent maintenance...")
1142
1143
 
1143
1144
  # 4. Post-npm verification steps
1144
1145
  errors = []
1145
1146
 
1146
1147
  # Reinstall pip deps for new version
1147
- _emit_progress(progress_fn, "Reconciling Python dependencies...")
1148
- pip_err = _reinstall_pip_deps()
1149
- if pip_err:
1150
- errors.append(f"pip deps: {pip_err}")
1151
-
1152
- # Run migrations
1148
+ if version_changed:
1149
+ _emit_progress(progress_fn, "Reconciling Python dependencies...")
1150
+ pip_err = _reinstall_pip_deps()
1151
+ if pip_err:
1152
+ errors.append(f"pip deps: {pip_err}")
1153
+
1154
+ # Run migrations even when npm reports the same package version. Several
1155
+ # cleanup hooks are idempotent and may need to repair an install after a
1156
+ # previous interrupted update without waiting for the next version bump.
1153
1157
  _emit_progress(progress_fn, "Running runtime migrations...")
1154
1158
  mig_err = _run_migrations()
1155
1159
  if mig_err:
@@ -1217,7 +1221,7 @@ def _handle_packaged_update(progress_fn=None, *, include_clis: bool = True) -> s
1217
1221
  if not clients_ok:
1218
1222
  client_sync_warning = client_sync_error or "unknown client sync error"
1219
1223
 
1220
- if old_version != new_version:
1224
+ if version_changed:
1221
1225
  _emit_progress(progress_fn, "Reloading LaunchAgents after version bump...")
1222
1226
  try:
1223
1227
  launchagent_reload_summary = _reload_launch_agents_after_bump()
@@ -1235,7 +1239,7 @@ def _handle_packaged_update(progress_fn=None, *, include_clis: bool = True) -> s
1235
1239
  mcp_code_changed = False
1236
1240
  force_restart = False
1237
1241
  new_fingerprint = ""
1238
- if old_version != new_version:
1242
+ if version_changed:
1239
1243
  try:
1240
1244
  _emit_progress(progress_fn, "Activating versioned runtime snapshot...")
1241
1245
  versioned_runtime_summary = activate_versioned_runtime_snapshot(
@@ -1308,8 +1312,13 @@ def _handle_packaged_update(progress_fn=None, *, include_clis: bool = True) -> s
1308
1312
  return "\n".join(lines)
1309
1313
 
1310
1314
  lines = ["UPDATE SUCCESSFUL (packaged install)"]
1311
- lines.append(f" Version: {old_version} -> {new_version}")
1315
+ if version_changed:
1316
+ lines.append(f" Version: {old_version} -> {new_version}")
1317
+ else:
1318
+ lines.append(f" Version: {old_version} (unchanged; idempotent maintenance)")
1312
1319
  lines.append(f" Backup: {backup_dir}")
1320
+ if not version_changed:
1321
+ lines.append(" Python deps: unchanged")
1313
1322
  if not cron_sync_warning:
1314
1323
  lines.append(" Crons: synced with manifest")
1315
1324
  else:
@@ -1340,7 +1349,11 @@ def _handle_packaged_update(progress_fn=None, *, include_clis: bool = True) -> s
1340
1349
  if restart_marker_summary:
1341
1350
  lines.append(f" Restart marker: {restart_marker_summary.get('path')}")
1342
1351
  lines.append("")
1343
- if old_version != new_version and not mcp_code_changed and not force_restart:
1352
+ if not version_changed:
1353
+ lines.append(
1354
+ "Package version unchanged; maintenance completed without an MCP restart."
1355
+ )
1356
+ elif not mcp_code_changed and not force_restart:
1344
1357
  # Doc-only / blog-only / changelog-only release. The bytes the running
1345
1358
  # MCP imports are byte-identical to what's now on disk, so existing
1346
1359
  # MCP clients keep working without a forced restart.
@@ -1432,16 +1445,20 @@ def handle_update(
1432
1445
  new_req_hash = _requirements_hash()
1433
1446
  deps_changed = old_req_hash != new_req_hash
1434
1447
 
1448
+ no_changes_pulled = pull_out == "Already up to date."
1449
+
1435
1450
  # Step 5: Reinstall pip dependencies if requirements.txt changed
1436
- if deps_changed or version_changed:
1451
+ if deps_changed:
1437
1452
  _emit_progress(progress_fn, "Reconciling Python dependencies...")
1438
1453
  pip_err = _reinstall_pip_deps()
1439
1454
  if pip_err:
1440
1455
  raise RuntimeError(f"Pip install failed: {pip_err}")
1441
1456
  steps_done.append("pip-deps")
1442
1457
 
1443
- # Step 6: Run migrations if version changed
1444
- if version_changed:
1458
+ # Step 6: Run idempotent migrations/cleanup even on same-version
1459
+ # updates. This repairs interrupted installs and pending cleanup
1460
+ # without waiting for a future version bump.
1461
+ if version_changed or no_changes_pulled:
1445
1462
  _emit_progress(progress_fn, "Running runtime migrations...")
1446
1463
  mig_err = _run_migrations()
1447
1464
  if mig_err:
@@ -1607,9 +1624,17 @@ def handle_update(
1607
1624
 
1608
1625
  # Build result
1609
1626
  dep_summary_lines = _format_dep_results(dep_results)
1610
- if pull_out == "Already up to date.":
1611
- msg = f"Already up to date (v{old_version}). No changes pulled."
1627
+ if no_changes_pulled:
1628
+ msg = f"Already up to date (v{old_version}). Idempotent maintenance completed."
1612
1629
  trailing = [*dep_summary_lines, *external_cli_lines]
1630
+ if "migrations" in steps_done:
1631
+ trailing.insert(0, " Migrations: checked/applied")
1632
+ if "hook-sync" in steps_done:
1633
+ trailing.insert(1 if trailing else 0, " Hooks: synced to NEXO_HOME")
1634
+ if "cron-sync" in steps_done:
1635
+ trailing.insert(2 if len(trailing) >= 2 else len(trailing), " Crons: synced with manifest")
1636
+ if "client-sync" in steps_done:
1637
+ trailing.append(" Clients: configured client targets synced")
1613
1638
  if trailing:
1614
1639
  msg += "\n" + "\n".join(trailing)
1615
1640
  return msg
@@ -1623,7 +1648,7 @@ def handle_update(
1623
1648
  lines.append(f" Backup: {backup_dir}")
1624
1649
  if "pip-deps" in steps_done:
1625
1650
  lines.append(" Python deps: reinstalled")
1626
- if version_changed:
1651
+ if "migrations" in steps_done:
1627
1652
  lines.append(" Migrations: applied")
1628
1653
  if "cron-sync" in steps_done:
1629
1654
  lines.append(" Crons: synced with manifest")
@@ -1424,9 +1424,38 @@ def sync_personal_scripts(prune_missing: bool = True) -> dict:
1424
1424
  })
1425
1425
  result["schedule_audit"] = schedule_audit
1426
1426
  result["missing_declared_schedules"] = missing_declared
1427
+ result["marker_warnings"] = _schedule_marker_warnings(schedule_audit)
1427
1428
  return result
1428
1429
 
1429
1430
 
1431
+ def _schedule_marker_warnings(schedule_audit: dict) -> list[dict]:
1432
+ """Report LaunchAgent marker drift without silently blessing it.
1433
+
1434
+ Managed personal schedules must be both declared in inline metadata and
1435
+ carry the managed marker written by the official schedule flow.
1436
+ """
1437
+ warnings: list[dict] = []
1438
+ for record in (schedule_audit or {}).get("schedules", []) or []:
1439
+ marker = bool(record.get("managed_marker"))
1440
+ declared = bool(record.get("schedule_declared"))
1441
+ matches = bool(record.get("schedule_matches_declared"))
1442
+ if marker and not declared:
1443
+ reason = "managed marker present but no valid declared schedule"
1444
+ elif marker and declared and not matches:
1445
+ reason = "managed marker present but schedule drifts from declaration"
1446
+ elif not marker and declared:
1447
+ reason = "declared schedule discovered without managed marker"
1448
+ else:
1449
+ continue
1450
+ warnings.append({
1451
+ "cron_id": str(record.get("cron_id") or ""),
1452
+ "script_path": str(record.get("script_path") or ""),
1453
+ "plist_path": str(record.get("plist_path") or ""),
1454
+ "reason": reason,
1455
+ })
1456
+ return warnings
1457
+
1458
+
1430
1459
  def _schedule_matches(existing: dict, declared: dict) -> bool:
1431
1460
  if not existing or not declared.get("valid"):
1432
1461
  return False
@@ -1583,6 +1612,7 @@ def reconcile_personal_scripts(*, dry_run: bool = False) -> dict:
1583
1612
  "dry_run": dry_run,
1584
1613
  "renamed_legacy_filenames": renamed_result,
1585
1614
  "sync": sync_result,
1615
+ "marker_warnings": sync_result.get("marker_warnings", []),
1586
1616
  "ensure_schedules": ensure_result,
1587
1617
  "classification": ensure_result.get("classification", sync_result.get("classification", {})),
1588
1618
  }
@@ -21,6 +21,7 @@ if str(NEXO_CODE) not in sys.path:
21
21
  sys.path.insert(0, str(NEXO_CODE))
22
22
 
23
23
  import paths
24
+ from email_sent_events import find_sent_email
24
25
  from agent_runner import AutomationBackendUnavailableError, run_automation_prompt
25
26
  from core_prompts import render_core_prompt
26
27
  try:
@@ -38,6 +39,9 @@ class ContextChecker:
38
39
 
39
40
  def check_email_sent(self, to_addr, subject, since_hours=72):
40
41
  """Check if email was already sent to address with subject."""
42
+ if find_sent_email(to_addr=to_addr, subject=subject, since_hours=since_hours):
43
+ return True
44
+
41
45
  sent_path = Path.home() / "mail" / ".nexo-sent" / ".Sent"
42
46
  if not sent_path.exists():
43
47
  return False
@@ -55,6 +55,7 @@ from automation_controls import (
55
55
  )
56
56
  from client_preferences import resolve_automation_backend, resolve_client_runtime_profile
57
57
  from core_prompts import render_core_prompt
58
+ from email_sent_events import format_recent_sent_email_block, recent_sent_emails
58
59
  import db as nexo_db
59
60
  from paths import data_dir, logs_dir, operations_dir
60
61
  from runtime_home import export_resolved_nexo_home
@@ -188,6 +189,23 @@ def _serialize_diaries(*, limit: int) -> list[dict]:
188
189
  return result
189
190
 
190
191
 
192
+ def _serialize_recent_sent_emails(*, limit: int = 8) -> list[dict]:
193
+ result: list[dict] = []
194
+ try:
195
+ rows = recent_sent_emails(hours=24, limit=limit)
196
+ except Exception:
197
+ return result
198
+ for row in rows:
199
+ result.append({
200
+ "sent_at": str(row.get("sent_at") or ""),
201
+ "to": _clean_text(row.get("to_addrs"), limit=180),
202
+ "subject": _clean_text(row.get("subject"), limit=220),
203
+ "source": str(row.get("source") or ""),
204
+ "message_id": str(row.get("message_id") or ""),
205
+ })
206
+ return result
207
+
208
+
191
209
  def collect_context(profile: dict) -> dict:
192
210
  nexo_db.init_db()
193
211
  due_followups = _serialize_followups("due", limit=MAX_DUE_ITEMS)
@@ -204,6 +222,7 @@ def collect_context(profile: dict) -> dict:
204
222
  for row in _serialize_reminders("active", limit=MAX_ACTIVE_ITEMS + MAX_DUE_ITEMS)
205
223
  if row["id"] not in due_reminder_ids
206
224
  ][:MAX_ACTIVE_ITEMS]
225
+ recent_sent = _serialize_recent_sent_emails()
207
226
  return {
208
227
  "generated_at": datetime.now().astimezone().isoformat(),
209
228
  "today": date.today().isoformat(),
@@ -220,15 +239,27 @@ def collect_context(profile: dict) -> dict:
220
239
  "due_followups": due_followups,
221
240
  "active_followups": active_followups,
222
241
  "recent_diaries": _serialize_diaries(limit=MAX_DIARY_ITEMS),
242
+ "recent_sent_emails_24h": recent_sent,
223
243
  "counts": {
224
244
  "due_reminders": len(due_reminders),
225
245
  "active_reminders": len(active_reminders),
226
246
  "due_followups": len(due_followups),
227
247
  "active_followups": len(active_followups),
248
+ "recent_sent_emails_24h": len(recent_sent),
228
249
  },
229
250
  }
230
251
 
231
252
 
253
+ def append_recent_sent_email_block(body: str) -> str:
254
+ try:
255
+ block = format_recent_sent_email_block(hours=24, limit=8)
256
+ except Exception:
257
+ block = ""
258
+ if not block or "EMAILS ENVIADOS ULTIMAS 24H" in body:
259
+ return body
260
+ return body.rstrip() + "\n\n" + block + "\n"
261
+
262
+
232
263
  def build_prompt(context: dict, *, extra_instructions_block: str = "") -> str:
233
264
  operator = context.get("operator") if isinstance(context.get("operator"), dict) else {}
234
265
  assistant = context.get("assistant") if isinstance(context.get("assistant"), dict) else {}
@@ -390,6 +421,7 @@ def main(argv: list[str] | None = None) -> int:
390
421
  extra_instructions_block=format_operator_extra_instructions_block("morning-agent"),
391
422
  )
392
423
  subject, body = generate_briefing(prompt)
424
+ body = append_recent_sent_email_block(body)
393
425
  write_latest_briefing(recipient=recipient or "[dry-run]", subject=subject, body=body)
394
426
 
395
427
  if args.dry_run:
@@ -46,6 +46,7 @@ if str(_repo_src) not in sys.path:
46
46
 
47
47
  from paths import nexo_email_dir
48
48
  from runtime_home import export_resolved_nexo_home
49
+ from email_sent_events import record_sent_email
49
50
 
50
51
  NEXO_HOME = export_resolved_nexo_home()
51
52
  EMAIL_BASE_DIR = nexo_email_dir()
@@ -501,11 +502,12 @@ def main(argv=None):
501
502
  args.in_reply_to, args.references,
502
503
  attachments=args.attach
503
504
  )
505
+ sent_copy_saved = False
504
506
  try:
505
- save_to_sent(config, raw_message)
507
+ sent_copy_saved = bool(save_to_sent(config, raw_message))
506
508
  except Exception as sent_exc:
507
509
  print(f"WARN: sent copy not saved to IMAP Sent: {sent_exc}", file=sys.stderr)
508
- record_reply_lifecycle(
510
+ lifecycle_event = record_reply_lifecycle(
509
511
  args.in_reply_to,
510
512
  args.references,
511
513
  reply_body,
@@ -514,6 +516,24 @@ def main(argv=None):
514
516
  cc=args.cc,
515
517
  message_id=msg_id,
516
518
  )
519
+ try:
520
+ record_sent_email(
521
+ message_id=msg_id,
522
+ sender=str(config.get("email") or ""),
523
+ to_addrs=args.to,
524
+ cc_addrs=args.cc,
525
+ subject=args.subject,
526
+ in_reply_to=args.in_reply_to,
527
+ references_header=args.references,
528
+ source="nexo-send-reply",
529
+ meta={
530
+ "sent_copy_saved": sent_copy_saved,
531
+ "lifecycle_event": lifecycle_event,
532
+ "account_label": (args.account_label or "").strip(),
533
+ },
534
+ )
535
+ except Exception as sent_event_exc:
536
+ print(f"WARN: sent email continuity tracking failed: {sent_event_exc}", file=sys.stderr)
517
537
  print(f"OK:{msg_id}")
518
538
  except Exception as e:
519
539
  print(f"FAIL:{e}", file=sys.stderr)
@@ -19,10 +19,12 @@ Stage B — Dreaming (automation backend):
19
19
  """
20
20
 
21
21
  import fcntl
22
+ import atexit
22
23
  import json
23
24
  import os
24
25
  import re
25
26
  import shutil
27
+ import signal
26
28
  import sqlite3
27
29
  import subprocess
28
30
  import sys
@@ -73,6 +75,8 @@ PROCESS_LOCK = COORD_DIR / "sleep-process.lock"
73
75
  TODAY = date.today()
74
76
  NOW = datetime.now()
75
77
  TIMESTAMP = NOW.strftime("%Y-%m-%d %H:%M")
78
+ _PROCESS_LOCK_FD = None
79
+ _PROCESS_LOCK_CLEANED = False
76
80
 
77
81
 
78
82
  # ─── Run-once & resume logic (unchanged from v1) ──────────────────────────────
@@ -128,6 +132,42 @@ def mark_complete():
128
132
  LOCK_FILE.unlink(missing_ok=True)
129
133
 
130
134
 
135
+ def _cleanup_process_lock():
136
+ global _PROCESS_LOCK_FD, _PROCESS_LOCK_CLEANED
137
+
138
+ if _PROCESS_LOCK_CLEANED:
139
+ return
140
+ _PROCESS_LOCK_CLEANED = True
141
+ try:
142
+ if _PROCESS_LOCK_FD is not None:
143
+ fcntl.flock(_PROCESS_LOCK_FD, fcntl.LOCK_UN)
144
+ _PROCESS_LOCK_FD.close()
145
+ except Exception:
146
+ pass
147
+ finally:
148
+ _PROCESS_LOCK_FD = None
149
+ try:
150
+ PROCESS_LOCK.unlink(missing_ok=True)
151
+ except Exception:
152
+ pass
153
+
154
+
155
+ def _handle_shutdown_signal(signum, _frame):
156
+ log(f"Received shutdown signal {signum}; cleaning sleep process lock.")
157
+ _cleanup_process_lock()
158
+ raise SystemExit(128 + signum)
159
+
160
+
161
+ def _register_process_lock_cleanup(lock_fd):
162
+ global _PROCESS_LOCK_FD, _PROCESS_LOCK_CLEANED
163
+
164
+ _PROCESS_LOCK_FD = lock_fd
165
+ _PROCESS_LOCK_CLEANED = False
166
+ atexit.register(_cleanup_process_lock)
167
+ signal.signal(signal.SIGINT, _handle_shutdown_signal)
168
+ signal.signal(signal.SIGTERM, _handle_shutdown_signal)
169
+
170
+
131
171
  # ─── Helpers ──────────────────────────────────────────────────────────────────
132
172
 
133
173
  def log(msg: str):
@@ -518,6 +558,7 @@ def main():
518
558
  fcntl.flock(lock_fd, fcntl.LOCK_EX | fcntl.LOCK_NB)
519
559
  lock_fd.write(str(os.getpid()))
520
560
  lock_fd.flush()
561
+ _register_process_lock_cleanup(lock_fd)
521
562
  except (IOError, OSError):
522
563
  log("Another sleep instance running. Exiting.")
523
564
  sys.exit(0)
@@ -587,12 +628,7 @@ def main():
587
628
  pass
588
629
 
589
630
  finally:
590
- try:
591
- fcntl.flock(lock_fd, fcntl.LOCK_UN)
592
- lock_fd.close()
593
- PROCESS_LOCK.unlink(missing_ok=True)
594
- except Exception:
595
- pass
631
+ _cleanup_process_lock()
596
632
 
597
633
 
598
634
  if __name__ == "__main__":
@@ -1073,6 +1073,13 @@ def handle_smart_startup_query() -> str:
1073
1073
  from db import get_db
1074
1074
  conn = get_db()
1075
1075
  query_parts = []
1076
+ sent_email_block = ""
1077
+ try:
1078
+ from email_sent_events import format_recent_sent_email_block
1079
+
1080
+ sent_email_block = format_recent_sent_email_block(hours=24, limit=8)
1081
+ except Exception:
1082
+ sent_email_block = ""
1076
1083
 
1077
1084
  # 1. Pending followups (what NEXO needs to do)
1078
1085
  followups = conn.execute(
@@ -1099,6 +1106,8 @@ def handle_smart_startup_query() -> str:
1099
1106
  pass
1100
1107
 
1101
1108
  if not query_parts:
1109
+ if sent_email_block:
1110
+ return sent_email_block
1102
1111
  return "No pending context to pre-load."
1103
1112
 
1104
1113
  # Search per-part to avoid diffuse centroid that matches everything
@@ -1123,6 +1132,8 @@ def handle_smart_startup_query() -> str:
1123
1132
  results = sorted(all_results, key=lambda x: x["score"], reverse=True)[:10]
1124
1133
  composite_query = " | ".join(query_parts[:6])
1125
1134
  if not results:
1135
+ if sent_email_block:
1136
+ return "Smart startup query: no relevant memories found.\n\n" + sent_email_block
1126
1137
  return "Smart startup query: no relevant memories found."
1127
1138
 
1128
1139
  lines = [f"SMART STARTUP — {len(results)} memories pre-loaded from composite query:"]
@@ -1138,6 +1149,10 @@ def handle_smart_startup_query() -> str:
1138
1149
  except Exception:
1139
1150
  pass
1140
1151
 
1152
+ if sent_email_block:
1153
+ lines.append("")
1154
+ lines.append(sent_email_block)
1155
+
1141
1156
  # Session tone from Deep Sleep (emotional intelligence layer)
1142
1157
  tone = _load_session_tone()
1143
1158
  if tone: