ltcai 3.6.0 → 4.0.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.
Files changed (238) hide show
  1. package/README.md +39 -31
  2. package/docs/CHANGELOG.md +64 -0
  3. package/docs/REALTIME_COLLABORATION.md +3 -3
  4. package/docs/V3_FRONTEND.md +9 -8
  5. package/docs/V4_BRAIN_ARCHITECTURE.md +322 -0
  6. package/docs/V4_DIGITAL_BRAIN_RECOVERY.md +552 -0
  7. package/docs/V4_IMPLEMENTATION_PLAN.md +470 -0
  8. package/docs/kg-schema.md +51 -53
  9. package/docs/spec-vs-impl.md +10 -10
  10. package/kg_schema.py +2 -520
  11. package/knowledge_graph.py +37 -4629
  12. package/knowledge_graph_api.py +11 -127
  13. package/latticeai/__init__.py +1 -1
  14. package/latticeai/api/admin.py +16 -17
  15. package/latticeai/api/agents.py +20 -7
  16. package/latticeai/api/auth.py +46 -15
  17. package/latticeai/api/chat.py +112 -76
  18. package/latticeai/api/health.py +1 -1
  19. package/latticeai/api/hooks.py +1 -1
  20. package/latticeai/api/invitations.py +100 -0
  21. package/latticeai/api/knowledge_graph.py +139 -0
  22. package/latticeai/api/local_files.py +1 -1
  23. package/latticeai/api/mcp.py +23 -11
  24. package/latticeai/api/memory.py +1 -1
  25. package/latticeai/api/models.py +1 -1
  26. package/latticeai/api/network.py +81 -0
  27. package/latticeai/api/plugins.py +3 -6
  28. package/latticeai/api/realtime.py +5 -8
  29. package/latticeai/api/search.py +26 -2
  30. package/latticeai/api/security_dashboard.py +2 -3
  31. package/latticeai/api/setup.py +2 -2
  32. package/latticeai/api/static_routes.py +11 -16
  33. package/latticeai/api/tools.py +3 -0
  34. package/latticeai/api/ui_redirects.py +26 -0
  35. package/latticeai/api/workflow_designer.py +85 -6
  36. package/latticeai/api/workspace.py +93 -57
  37. package/latticeai/app_factory.py +1781 -0
  38. package/latticeai/brain/__init__.py +18 -0
  39. package/latticeai/brain/_kg_common.py +1123 -0
  40. package/latticeai/brain/context.py +213 -0
  41. package/latticeai/brain/conversations.py +236 -0
  42. package/latticeai/brain/discovery.py +1455 -0
  43. package/latticeai/brain/documents.py +218 -0
  44. package/latticeai/brain/identity.py +175 -0
  45. package/latticeai/brain/ingest.py +644 -0
  46. package/latticeai/brain/memory.py +102 -0
  47. package/latticeai/brain/network.py +205 -0
  48. package/latticeai/brain/projection.py +561 -0
  49. package/latticeai/brain/provenance.py +401 -0
  50. package/latticeai/brain/retrieval.py +1316 -0
  51. package/latticeai/brain/schema.py +640 -0
  52. package/latticeai/brain/store.py +216 -0
  53. package/latticeai/brain/write_master.py +225 -0
  54. package/latticeai/core/agent.py +31 -7
  55. package/latticeai/core/audit.py +0 -7
  56. package/latticeai/core/config.py +1 -1
  57. package/latticeai/core/context_builder.py +1 -2
  58. package/latticeai/core/enterprise.py +1 -1
  59. package/latticeai/core/graph_curator.py +2 -2
  60. package/latticeai/core/invitations.py +131 -0
  61. package/latticeai/core/marketplace.py +1 -1
  62. package/latticeai/core/mcp_registry.py +791 -0
  63. package/latticeai/core/model_compat.py +1 -1
  64. package/latticeai/core/model_resolution.py +0 -1
  65. package/latticeai/core/multi_agent.py +238 -4
  66. package/latticeai/core/policy.py +54 -0
  67. package/latticeai/core/realtime.py +65 -44
  68. package/latticeai/core/security.py +1 -1
  69. package/latticeai/core/sessions.py +66 -10
  70. package/latticeai/core/users.py +147 -0
  71. package/latticeai/core/workflow_engine.py +114 -2
  72. package/latticeai/core/workspace_os.py +477 -29
  73. package/latticeai/models/__init__.py +7 -0
  74. package/latticeai/models/router.py +779 -0
  75. package/latticeai/server_app.py +29 -1536
  76. package/latticeai/services/agent_runtime.py +243 -4
  77. package/latticeai/services/app_context.py +75 -14
  78. package/latticeai/services/ingestion.py +47 -0
  79. package/latticeai/services/kg_portability.py +33 -3
  80. package/latticeai/services/memory_service.py +39 -11
  81. package/latticeai/services/model_runtime.py +2 -5
  82. package/latticeai/services/platform_runtime.py +100 -23
  83. package/latticeai/services/run_executor.py +328 -0
  84. package/latticeai/services/search_service.py +17 -8
  85. package/latticeai/services/tool_dispatch.py +12 -2
  86. package/latticeai/services/triggers.py +241 -0
  87. package/latticeai/services/upload_service.py +37 -12
  88. package/latticeai/services/workspace_service.py +55 -16
  89. package/llm_router.py +29 -772
  90. package/ltcai_cli.py +1 -2
  91. package/mcp_registry.py +25 -788
  92. package/p_reinforce.py +124 -14
  93. package/package.json +10 -20
  94. package/scripts/bump_version.py +99 -0
  95. package/scripts/generate_diagrams.py +0 -1
  96. package/scripts/lint_v3.mjs +105 -18
  97. package/scripts/validate_release_artifacts.py +0 -1
  98. package/scripts/wheel_smoke.py +142 -0
  99. package/server.py +11 -7
  100. package/setup_wizard.py +1142 -0
  101. package/static/sw.js +81 -52
  102. package/static/v3/asset-manifest.json +33 -25
  103. package/static/v3/css/{lattice.base.e4cdd05d.css → lattice.base.49deefb5.css} +1 -1
  104. package/static/v3/css/lattice.base.css +1 -1
  105. package/static/v3/css/{lattice.components.9b49d614.css → lattice.components.cde18231.css} +1 -1
  106. package/static/v3/css/lattice.components.css +1 -1
  107. package/static/v3/css/{lattice.shell.8fcc9d33.css → lattice.shell.29d36d85.css} +1 -1
  108. package/static/v3/css/lattice.shell.css +1 -1
  109. package/static/v3/css/{lattice.tokens.e7018963.css → lattice.tokens.304cbc40.css} +3 -0
  110. package/static/v3/css/lattice.tokens.css +3 -0
  111. package/static/v3/css/{lattice.views.22f69117.css → lattice.views.0a18b6c5.css} +2 -2
  112. package/static/v3/css/lattice.views.css +2 -2
  113. package/static/v3/index.html +3 -4
  114. package/static/v3/js/{app.c541f955.js → app.c5c80c46.js} +1 -1
  115. package/static/v3/js/core/{api.33d6320e.js → api.ba0fbf14.js} +58 -1
  116. package/static/v3/js/core/api.js +57 -0
  117. package/static/v3/js/core/i18n.880e1fec.js +575 -0
  118. package/static/v3/js/core/i18n.js +575 -0
  119. package/static/v3/js/core/routes.37522821.js +101 -0
  120. package/static/v3/js/core/routes.js +71 -63
  121. package/static/v3/js/core/{shell.8c163e0e.js → shell.e3f6bbfa.js} +68 -39
  122. package/static/v3/js/core/shell.js +66 -37
  123. package/static/v3/js/core/{store.34ebd5e6.js → store.7b2aa044.js} +11 -1
  124. package/static/v3/js/core/store.js +11 -1
  125. package/static/v3/js/views/account.eff40715.js +143 -0
  126. package/static/v3/js/views/account.js +143 -0
  127. package/static/v3/js/views/activity.0d271ef9.js +67 -0
  128. package/static/v3/js/views/activity.js +67 -0
  129. package/static/v3/js/views/{admin-users.03bac88c.js → admin-users.f7ac7b43.js} +4 -6
  130. package/static/v3/js/views/admin-users.js +4 -6
  131. package/static/v3/js/views/{agents.014d0b74.js → agents.17c5288d.js} +35 -12
  132. package/static/v3/js/views/agents.js +35 -12
  133. package/static/v3/js/views/{chat.e6dd7dd0.js → chat.e250e2cc.js} +23 -0
  134. package/static/v3/js/views/chat.js +23 -0
  135. package/static/v3/js/views/graph-canvas.17c15d65.js +509 -0
  136. package/static/v3/js/views/graph-canvas.js +509 -0
  137. package/static/v3/js/views/{hybrid-search.b22b97e0.js → hybrid-search.2fb63ed9.js} +1 -2
  138. package/static/v3/js/views/hybrid-search.js +1 -2
  139. package/static/v3/js/views/{knowledge-graph.a96040a5.js → knowledge-graph.4d09c537.js} +60 -44
  140. package/static/v3/js/views/knowledge-graph.js +60 -44
  141. package/static/v3/js/views/network.52a4f181.js +97 -0
  142. package/static/v3/js/views/network.js +97 -0
  143. package/static/v3/js/views/{planning.9ac3e313.js → planning.4876fd77.js} +26 -5
  144. package/static/v3/js/views/planning.js +26 -5
  145. package/static/v3/js/views/runs.b63b2afa.js +144 -0
  146. package/static/v3/js/views/runs.js +144 -0
  147. package/static/v3/js/views/{settings.8631fa5e.js → settings.b7140634.js} +7 -8
  148. package/static/v3/js/views/settings.js +7 -8
  149. package/static/v3/js/views/snapshots.6f5db095.js +135 -0
  150. package/static/v3/js/views/snapshots.js +135 -0
  151. package/static/v3/js/views/{workflows.26c57290.js → workflows.7752225a.js} +87 -2
  152. package/static/v3/js/views/workflows.js +87 -2
  153. package/static/v3/js/views/workspace-admin.c466029b.js +156 -0
  154. package/static/v3/js/views/workspace-admin.js +156 -0
  155. package/static/vendor/chart.umd.min.js +20 -0
  156. package/static/vendor/fonts/inter-latin-300-normal.woff2 +0 -0
  157. package/static/vendor/fonts/inter-latin-400-normal.woff2 +0 -0
  158. package/static/vendor/fonts/inter-latin-500-normal.woff2 +0 -0
  159. package/static/vendor/fonts/inter-latin-600-normal.woff2 +0 -0
  160. package/static/vendor/fonts/inter-latin-700-normal.woff2 +0 -0
  161. package/static/vendor/fonts/inter-latin-800-normal.woff2 +0 -0
  162. package/static/vendor/fonts/inter.css +44 -0
  163. package/static/vendor/icons/tabler-icons.min.css +4 -0
  164. package/static/vendor/icons/tabler-icons.woff2 +0 -0
  165. package/static/vendor/marked.min.js +69 -0
  166. package/telegram_bot.py +1 -2
  167. package/tools/commands.py +4 -2
  168. package/tools/computer.py +1 -1
  169. package/tools/documents.py +1 -3
  170. package/tools/filesystem.py +0 -4
  171. package/tools/knowledge.py +1 -3
  172. package/tools/network.py +1 -3
  173. package/codex_telegram_bot.py +0 -195
  174. package/docs/assets/v3.4.0/agent-run.png +0 -0
  175. package/docs/assets/v3.4.0/agents.png +0 -0
  176. package/docs/assets/v3.4.0/before/chat-before.png +0 -0
  177. package/docs/assets/v3.4.0/before/files-before.png +0 -0
  178. package/docs/assets/v3.4.0/chat.png +0 -0
  179. package/docs/assets/v3.4.0/connect-folder.png +0 -0
  180. package/docs/assets/v3.4.0/files.png +0 -0
  181. package/docs/assets/v3.4.0/home.png +0 -0
  182. package/docs/assets/v3.4.0/hooks-dispatch.png +0 -0
  183. package/docs/assets/v3.4.0/knowledge-graph.png +0 -0
  184. package/docs/assets/v3.4.0/local-agent.png +0 -0
  185. package/docs/assets/v3.4.0/memory.png +0 -0
  186. package/docs/assets/v3.4.0/settings.png +0 -0
  187. package/docs/assets/v3.4.0/vision-input.png +0 -0
  188. package/docs/assets/v3.4.0/workflows.png +0 -0
  189. package/docs/assets/v3.4.1/e2e_runtime_log.txt +0 -42
  190. package/docs/assets/v3.4.1/hooks-dispatch.png +0 -0
  191. package/docs/assets/v3.4.1/local-agent.png +0 -0
  192. package/docs/images/admin-dashboard.png +0 -0
  193. package/docs/images/architecture.png +0 -0
  194. package/docs/images/enterprise.png +0 -0
  195. package/docs/images/graph.png +0 -0
  196. package/docs/images/hero.gif +0 -0
  197. package/docs/images/knowledge-graph.png +0 -0
  198. package/docs/images/lattice-ai-demo.gif +0 -0
  199. package/docs/images/lattice-ai-hero.png +0 -0
  200. package/docs/images/logo.svg +0 -33
  201. package/docs/images/mobile-responsive.png +0 -0
  202. package/docs/images/model-recommendation.png +0 -0
  203. package/docs/images/onboarding.png +0 -0
  204. package/docs/images/organization.png +0 -0
  205. package/docs/images/pipeline.png +0 -0
  206. package/docs/images/screenshot-admin.png +0 -0
  207. package/docs/images/screenshot-chat.png +0 -0
  208. package/docs/images/screenshot-graph.png +0 -0
  209. package/docs/images/skills.png +0 -0
  210. package/docs/images/workspace-dark.png +0 -0
  211. package/docs/images/workspace-light.png +0 -0
  212. package/docs/images/workspace.png +0 -0
  213. package/requirements.txt +0 -16
  214. package/static/account.html +0 -115
  215. package/static/activity.html +0 -73
  216. package/static/admin.html +0 -488
  217. package/static/agents.html +0 -139
  218. package/static/chat.html +0 -844
  219. package/static/css/reference/account.css +0 -439
  220. package/static/css/reference/admin.css +0 -610
  221. package/static/css/reference/base.css +0 -1661
  222. package/static/css/reference/chat.css +0 -4623
  223. package/static/css/reference/graph.css +0 -1016
  224. package/static/css/responsive.css +0 -861
  225. package/static/graph.html +0 -124
  226. package/static/platform.css +0 -104
  227. package/static/plugins.html +0 -136
  228. package/static/scripts/account.js +0 -238
  229. package/static/scripts/admin.js +0 -1614
  230. package/static/scripts/chat.js +0 -5081
  231. package/static/scripts/graph.js +0 -1804
  232. package/static/scripts/platform.js +0 -64
  233. package/static/scripts/ux.js +0 -167
  234. package/static/scripts/workspace.js +0 -948
  235. package/static/v3/js/core/routes.2ce3815a.js +0 -93
  236. package/static/workflows.html +0 -146
  237. package/static/workspace.css +0 -1121
  238. package/static/workspace.html +0 -357
@@ -0,0 +1,102 @@
1
+ """Memory System — typed, durable memory records on the brain substrate.
2
+
3
+ Decision and Experience records become first-class graph nodes through the
4
+ unified ingestion pipeline (provenance + hooks), instead of markdown dumps
5
+ with swallowed errors. Episodic memory is the conversation store; semantic
6
+ memory is the workspace MEMORY_KINDS records — this module adds the typed
7
+ record kinds the schema always had but never populated.
8
+
9
+ Only REAL events become memories: simulation runs are rejected at this
10
+ boundary (the run record's own mode field is checked — fabricated artifacts
11
+ must never enter the brain as experience).
12
+ """
13
+
14
+ from __future__ import annotations
15
+
16
+ from typing import Any, Dict, Optional
17
+
18
+ from latticeai.services.ingestion import IngestionItem
19
+
20
+
21
+ class BrainMemory:
22
+ """Writes Decision / Experience records through the ingestion pipeline."""
23
+
24
+ def __init__(self, ingestion_pipeline: Any):
25
+ self._pipeline = ingestion_pipeline
26
+
27
+ def available(self) -> bool:
28
+ return self._pipeline is not None and self._pipeline.available()
29
+
30
+ def record_decision(
31
+ self,
32
+ title: str,
33
+ detail: str = "",
34
+ *,
35
+ user_email: Optional[str] = None,
36
+ workspace_id: Optional[str] = None,
37
+ conversation_id: Optional[str] = None,
38
+ decided_by: Optional[str] = None,
39
+ metadata: Optional[Dict[str, Any]] = None,
40
+ ) -> Dict[str, Any]:
41
+ if not str(title or "").strip():
42
+ raise ValueError("a decision needs a title")
43
+ result = self._pipeline.ingest(
44
+ IngestionItem(
45
+ source_type="decision",
46
+ title=title.strip(),
47
+ text=detail,
48
+ owner=user_email,
49
+ workspace_id=workspace_id,
50
+ conversation_id=conversation_id,
51
+ metadata={"decided_by": decided_by or user_email, **(metadata or {})},
52
+ ),
53
+ user_email=user_email,
54
+ )
55
+ return result.as_dict()
56
+
57
+ def record_experience(
58
+ self,
59
+ title: str,
60
+ detail: str = "",
61
+ *,
62
+ run: Optional[Dict[str, Any]] = None,
63
+ user_email: Optional[str] = None,
64
+ workspace_id: Optional[str] = None,
65
+ metadata: Optional[Dict[str, Any]] = None,
66
+ ) -> Dict[str, Any]:
67
+ """Persist a completed run/action as an Experience node.
68
+
69
+ ``run`` is the persisted run record; simulated runs are refused —
70
+ a simulation is replay scaffolding, not something that happened.
71
+ """
72
+ if run is not None and run.get("mode", "simulation") == "simulation":
73
+ return {
74
+ "status": "rejected",
75
+ "detail": "simulation runs are not experiences and never enter the brain",
76
+ }
77
+ if not str(title or "").strip():
78
+ raise ValueError("an experience needs a title")
79
+ run_meta = {}
80
+ if run is not None:
81
+ run_meta = {
82
+ "run_id": run.get("id"),
83
+ "agent_id": run.get("agent_id"),
84
+ "run_status": run.get("status"),
85
+ "mode": run.get("mode"),
86
+ "retries": run.get("retries"),
87
+ }
88
+ result = self._pipeline.ingest(
89
+ IngestionItem(
90
+ source_type="experience",
91
+ title=title.strip(),
92
+ text=detail,
93
+ owner=user_email,
94
+ workspace_id=workspace_id,
95
+ metadata={**run_meta, **(metadata or {})},
96
+ ),
97
+ user_email=user_email,
98
+ )
99
+ return result.as_dict()
100
+
101
+
102
+ __all__ = ["BrainMemory"]
@@ -0,0 +1,205 @@
1
+ """Brain Network v1 — knowledge exchange between paired Lattice instances.
2
+
3
+ Local-first federation: no cloud rendezvous, no relay. A peer is another
4
+ Lattice installation you deliberately paired with by exchanging device
5
+ public keys (LAN/tailnet HTTP). Exchange is per-workspace, per-request,
6
+ owner-initiated: a signed export bundle is pushed to (or received from) a
7
+ paired peer, verified against the *paired* key, imported through the normal
8
+ import path, and stamped with origin-device provenance.
9
+
10
+ Peer requests authenticate independently of user sessions: each carries an
11
+ Ed25519 signature over (body sha256 + timestamp + nonce), with a freshness
12
+ window and a seen-nonce set for replay protection.
13
+ """
14
+
15
+ from __future__ import annotations
16
+
17
+ import hashlib
18
+ import json
19
+ import logging
20
+ import threading
21
+ import time
22
+ import uuid
23
+ from pathlib import Path
24
+ from typing import Any, Dict, List, Optional
25
+
26
+ from latticeai.brain.identity import DeviceIdentity, fingerprint_of, verify_signature
27
+
28
+ PEER_AUTH_WINDOW_SECONDS = 300
29
+ _NONCE_CACHE_MAX = 4096
30
+
31
+ HEADER_DEVICE = "x-lattice-device"
32
+ HEADER_TIMESTAMP = "x-lattice-timestamp"
33
+ HEADER_NONCE = "x-lattice-nonce"
34
+ HEADER_SIGNATURE = "x-lattice-signature"
35
+
36
+
37
+ def _signing_payload(body: bytes, timestamp: str, nonce: str) -> bytes:
38
+ body_digest = hashlib.sha256(body or b"").hexdigest()
39
+ return f"{body_digest}|{timestamp}|{nonce}".encode("ascii")
40
+
41
+
42
+ class BrainNetwork:
43
+ """Peer registry + signed bundle exchange."""
44
+
45
+ def __init__(
46
+ self,
47
+ *,
48
+ identity: DeviceIdentity,
49
+ portability: Any,
50
+ data_dir: Path,
51
+ http_client_factory: Any = None,
52
+ ) -> None:
53
+ self._identity = identity
54
+ self._portability = portability
55
+ self._peers_file = Path(data_dir) / "brain_peers.json"
56
+ self._lock = threading.Lock()
57
+ self._seen_nonces: Dict[str, float] = {}
58
+ # injectable for tests; default builds an httpx client per call
59
+ self._http_client_factory = http_client_factory
60
+
61
+ # ── peer registry (deliberate pairing) ─────────────────────────────────
62
+ def _load_peers(self) -> List[Dict[str, Any]]:
63
+ if not self._peers_file.exists():
64
+ return []
65
+ try:
66
+ return json.loads(self._peers_file.read_text(encoding="utf-8"))
67
+ except Exception as exc:
68
+ logging.warning("brain network: peer registry unreadable: %s", exc)
69
+ return []
70
+
71
+ def _save_peers(self, peers: List[Dict[str, Any]]) -> None:
72
+ self._peers_file.parent.mkdir(parents=True, exist_ok=True)
73
+ tmp = self._peers_file.with_suffix(".tmp")
74
+ tmp.write_text(json.dumps(peers, ensure_ascii=False, indent=2), encoding="utf-8")
75
+ tmp.replace(self._peers_file)
76
+
77
+ def list_peers(self) -> List[Dict[str, Any]]:
78
+ return self._load_peers()
79
+
80
+ def add_peer(self, *, name: str, base_url: str, public_key: str) -> Dict[str, Any]:
81
+ name = str(name or "").strip()
82
+ base_url = str(base_url or "").strip().rstrip("/")
83
+ public_key = str(public_key or "").strip()
84
+ if not name or not base_url or not public_key:
85
+ raise ValueError("pairing requires name, base_url, and the peer's public key")
86
+ if not base_url.startswith(("http://", "https://")):
87
+ raise ValueError("base_url must be an http(s) URL")
88
+ try:
89
+ fingerprint = fingerprint_of(public_key)
90
+ except Exception as exc:
91
+ raise ValueError(f"public_key is not a valid Ed25519 key: {exc}") from exc
92
+ with self._lock:
93
+ peers = self._load_peers()
94
+ if any(p.get("public_key") == public_key for p in peers):
95
+ raise ValueError("this device is already paired")
96
+ peer = {
97
+ "id": f"peer-{uuid.uuid4().hex[:12]}",
98
+ "name": name,
99
+ "base_url": base_url,
100
+ "public_key": public_key,
101
+ "fingerprint": fingerprint,
102
+ "added_at": time.strftime("%Y-%m-%dT%H:%M:%S"),
103
+ }
104
+ peers.append(peer)
105
+ self._save_peers(peers)
106
+ return peer
107
+
108
+ def remove_peer(self, peer_id: str) -> Dict[str, Any]:
109
+ with self._lock:
110
+ peers = self._load_peers()
111
+ kept = [p for p in peers if p.get("id") != peer_id]
112
+ if len(kept) == len(peers):
113
+ raise FileNotFoundError(peer_id)
114
+ self._save_peers(kept)
115
+ return {"status": "removed", "peer_id": peer_id}
116
+
117
+ def _peer_by_id(self, peer_id: str) -> Dict[str, Any]:
118
+ peer = next((p for p in self._load_peers() if p.get("id") == peer_id), None)
119
+ if peer is None:
120
+ raise FileNotFoundError(peer_id)
121
+ return peer
122
+
123
+ # ── request authentication (peer → this brain) ────────────────────────
124
+ def auth_headers(self, body: bytes) -> Dict[str, str]:
125
+ """Headers this device attaches when pushing to a peer."""
126
+ timestamp = str(int(time.time()))
127
+ nonce = uuid.uuid4().hex
128
+ return {
129
+ HEADER_DEVICE: self._identity.public_key_b64,
130
+ HEADER_TIMESTAMP: timestamp,
131
+ HEADER_NONCE: nonce,
132
+ HEADER_SIGNATURE: self._identity.sign(_signing_payload(body, timestamp, nonce)),
133
+ }
134
+
135
+ def verify_peer_request(self, headers: Dict[str, str], body: bytes) -> Dict[str, Any]:
136
+ """Authenticate an inbound peer request. Raises PermissionError."""
137
+ lowered = {str(k).lower(): v for k, v in headers.items()}
138
+ device = lowered.get(HEADER_DEVICE) or ""
139
+ timestamp = lowered.get(HEADER_TIMESTAMP) or ""
140
+ nonce = lowered.get(HEADER_NONCE) or ""
141
+ signature = lowered.get(HEADER_SIGNATURE) or ""
142
+ if not device or not timestamp or not nonce or not signature:
143
+ raise PermissionError("missing peer authentication headers")
144
+ peer = next((p for p in self._load_peers() if p.get("public_key") == device), None)
145
+ if peer is None:
146
+ raise PermissionError("device is not a paired peer")
147
+ try:
148
+ age = abs(time.time() - int(timestamp))
149
+ except ValueError:
150
+ raise PermissionError("invalid timestamp")
151
+ if age > PEER_AUTH_WINDOW_SECONDS:
152
+ raise PermissionError("request outside the freshness window")
153
+ with self._lock:
154
+ if nonce in self._seen_nonces:
155
+ raise PermissionError("replayed nonce")
156
+ self._seen_nonces[nonce] = time.time()
157
+ if len(self._seen_nonces) > _NONCE_CACHE_MAX:
158
+ cutoff = time.time() - PEER_AUTH_WINDOW_SECONDS * 2
159
+ self._seen_nonces = {n: t for n, t in self._seen_nonces.items() if t > cutoff}
160
+ if not verify_signature(device, _signing_payload(body, timestamp, nonce), signature):
161
+ raise PermissionError("peer request signature invalid")
162
+ return peer
163
+
164
+ # ── exchange ────────────────────────────────────────────────────────────
165
+ def push_to_peer(self, peer_id: str, *, workspace_id: Optional[str] = None, timeout: float = 30.0) -> Dict[str, Any]:
166
+ """Owner-initiated: export (signed) and push to one paired peer."""
167
+ peer = self._peer_by_id(peer_id)
168
+ artifact = self._portability.export(workspace_id=workspace_id)
169
+ body = json.dumps(artifact, ensure_ascii=False).encode("utf-8")
170
+ headers = {**self.auth_headers(body), "Content-Type": "application/json"}
171
+ url = f"{peer['base_url']}/network/receive"
172
+ if self._http_client_factory is not None:
173
+ response = self._http_client_factory().post(url, content=body, headers=headers, timeout=timeout)
174
+ else:
175
+ import httpx
176
+
177
+ with httpx.Client() as client:
178
+ response = client.post(url, content=body, headers=headers, timeout=timeout)
179
+ payload = response.json() if response.headers.get("content-type", "").startswith("application/json") else {}
180
+ return {
181
+ "status": "ok" if response.status_code == 200 else "failed",
182
+ "http_status": response.status_code,
183
+ "peer": {"id": peer["id"], "name": peer["name"], "fingerprint": peer["fingerprint"]},
184
+ "peer_result": payload,
185
+ "counts": (artifact.get("header") or {}).get("counts"),
186
+ }
187
+
188
+ def receive(self, headers: Dict[str, str], body: bytes) -> Dict[str, Any]:
189
+ """Inbound: authenticate the peer, verify the bundle, import."""
190
+ peer = self.verify_peer_request(headers, body)
191
+ try:
192
+ artifact = json.loads(body.decode("utf-8"))
193
+ except Exception:
194
+ raise ValueError("body is not a JSON bundle")
195
+ signature = artifact.get("signature") or {}
196
+ # On the network path the bundle itself MUST be signed by the paired
197
+ # peer too (unsigned-legacy applies to local file imports only).
198
+ if signature.get("public_key") != peer.get("public_key"):
199
+ raise PermissionError("bundle signer does not match the paired peer")
200
+ result = self._portability.import_data(artifact, mode="merge")
201
+ result["peer"] = {"id": peer["id"], "name": peer["name"], "fingerprint": peer["fingerprint"]}
202
+ return result
203
+
204
+
205
+ __all__ = ["BrainNetwork", "PEER_AUTH_WINDOW_SECONDS"]