File size: 29,040 Bytes
275d9c9
 
 
f08047d
275d9c9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4aaae80
e2fd8ae
 
27be63b
e2fd8ae
9fb722e
275d9c9
 
 
7063d0f
275d9c9
4aaae80
275d9c9
7063d0f
275d9c9
7063d0f
275d9c9
 
 
9fb722e
495b06b
 
9fb722e
275d9c9
e2fd8ae
275d9c9
e2fd8ae
 
275d9c9
 
4aaae80
 
e2fd8ae
 
 
275d9c9
e2fd8ae
 
4aaae80
 
 
 
e2fd8ae
 
 
275d9c9
e2fd8ae
 
275d9c9
 
4aaae80
 
e2fd8ae
 
 
275d9c9
4aaae80
e2fd8ae
4aaae80
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e2fd8ae
 
275d9c9
 
4aaae80
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
275d9c9
4aaae80
 
 
 
 
 
 
 
 
 
 
 
 
 
 
275d9c9
e2fd8ae
9fb722e
 
e2fd8ae
275d9c9
 
e2fd8ae
275d9c9
 
7573216
29530f8
 
 
 
275d9c9
 
4aaae80
 
 
 
275d9c9
cbad453
 
 
 
 
29530f8
cbad453
 
 
 
 
 
29530f8
275d9c9
29530f8
 
 
e2fd8ae
9fb722e
275d9c9
 
 
6db565a
 
 
 
 
4aaae80
6db565a
78cc96f
6db565a
 
4aaae80
 
 
6db565a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
78cc96f
8ee8138
6db565a
 
4aaae80
 
 
6db565a
78cc96f
 
6db565a
 
 
 
 
 
 
 
 
 
 
27be63b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1d068f1
 
 
 
27be63b
 
 
 
 
1d068f1
 
 
 
 
 
 
 
27be63b
 
 
 
 
 
275d9c9
 
9fb722e
275d9c9
9fb722e
27be63b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e2fd8ae
27be63b
 
 
 
 
 
 
 
 
 
 
 
29530f8
27be63b
 
29530f8
f08047d
 
8753188
 
 
 
 
 
 
 
 
 
 
27be63b
 
f08047d
27be63b
 
 
29530f8
27be63b
 
 
 
 
f08047d
 
 
27be63b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
29530f8
f08047d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
27be63b
f08047d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
27be63b
146edc4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
27be63b
8231854
fa5b4a9
4aaae80
9fb722e
275d9c9
9fb722e
e2fd8ae
275d9c9
 
e2fd8ae
f08047d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8f53c4c
 
 
 
 
 
f08047d
 
 
 
 
 
 
 
 
 
8f53c4c
275d9c9
9fb722e
275d9c9
 
 
481b78e
275d9c9
 
 
 
9fb722e
c70fa57
f620ad7
0572318
 
 
 
694ac0a
8231854
0572318
694ac0a
 
 
0572318
 
 
 
 
 
8231854
 
 
 
 
 
 
 
 
 
 
428efdf
8231854
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
428efdf
8231854
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3f78ea8
0572318
b8dcc7e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0572318
8231854
8f53c4c
ff86593
 
 
bc0c2ea
 
 
 
 
 
 
 
 
 
 
 
 
a4fb1d3
bc0c2ea
 
 
b8dcc7e
 
 
 
 
694ac0a
275d9c9
c2fa541
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
"""HearthNet β€” Hugging Face Space entry point.

This Space runs a **real** HearthNet node using HuggingFace Transformers as the
LLM backend. All 8 tabs are live:

  Ask        β€” LLM + RAG queries routed via capability bus
  Chat       β€” Event-sourced direct messages between nodes
  Mesh       β€” Live topology graph of discovered peers
  Marketplace β€” Community offers / requests / emergency posts
  Files      β€” BLAKE3 content-addressed blob store
  Emergency  β€” Offline-mode probe and connectivity status
  Settings   β€” Node identity, peer list, QR invite, RAG ingest

Difference between this Space and a local install
──────────────────────────────────────────────────
  HF Space     β†’ single node, no real peer mesh, SmolLM2-135M for LLM
  Local node   β†’ full peer mesh, any LLM backend (Ollama / llama.cpp / HF),
                 file sharing, multi-node chat, hardware acceleration

Quick start (local, full features):
  git clone https://huggingface.co/spaces/build-small-hackathon/HearthNet
  cd HearthNet
  pip install -e .
  python -m hearthnet.cli run
  # Open http://localhost:7860 in your browser

See docs/HOWTO.md for Raspberry Pi, Docker, and multi-node mesh setup.
"""

from __future__ import annotations

import contextlib
import os

# ─────────────────────────────────────────────────────────────────────────────
# Optional HF Spaces GPU decorator
# ─────────────────────────────────────────────────────────────────────────────
try:
    import spaces as _spaces  # type: ignore[import]

    HF_SPACES = True
except ImportError:
    HF_SPACES = False

# ─────────────────────────────────────────────────────────────────────────────
# Bootstrap a real HearthNet node
# ─────────────────────────────────────────────────────────────────────────────

MODEL_ID = os.getenv("MODEL_ID", "openbmb/MiniCPM3-4B")
MODEL_REVISION = os.getenv("MODEL_REVISION") or None

SEED_CORPUS = [
    {
        "id": "water.001",
        "title": "Water Safety",
        "text": (
            "If the mains supply is disrupted, use stored clean water first. "
            "Rainwater should be filtered through clean cloth, brought to a rolling "
            "boil for at least one minute, and stored in a clean covered container. "
            "Adult daily minimum: 3 litres for drinking and sanitation."
        ),
    },
    {
        "id": "power.001",
        "title": "Power Outage",
        "text": (
            "Keep refrigerators closed to preserve food up to 4 hours. "
            "Disconnect sensitive electronics. Reserve battery banks for communication. "
            "Share verified charging points through the local marketplace. "
            "Candles are a fire risk β€” use battery or wind-up torches."
        ),
    },
    {
        "id": "mesh.001",
        "title": "HearthNet Routing",
        "text": (
            "A HearthNet UI sends requests to a capability bus. The bus scores local "
            "capabilities higher than remote ones and routes to the best available "
            "provider. If a node is quarantined the bus fails over automatically. "
            "RAG corpus routing uses the 'corpus' parameter to match the right node."
        ),
    },
    {
        "id": "firstaid.001",
        "title": "First Aid β€” Bleeding",
        "text": (
            "Apply direct firm pressure to the wound with a clean cloth. "
            "Maintain pressure for at least 10 minutes. Do not remove the cloth β€” "
            "add more on top if it soaks through. Elevate the limb above heart level "
            "if possible. Seek emergency care if bleeding is severe or arterial."
        ),
    },
    {
        "id": "firstaid.002",
        "title": "CPR Basics",
        "text": (
            "If a person is unresponsive and not breathing normally: call emergency services, "
            "then give 30 chest compressions (hard, fast, centre of chest) followed by "
            "2 rescue breaths. Continue the 30:2 cycle until help arrives or the person "
            "recovers. Hands-only CPR (compressions without rescue breaths) is acceptable "
            "for untrained bystanders."
        ),
    },
    {
        "id": "setup.001",
        "title": "Node Setup β€” Quick Start",
        "text": (
            "Install HearthNet with: pip install hearthnet. "
            "Run: python -m hearthnet.cli run "
            "to start a node. Open http://localhost:7860 in your browser. "
            "Other devices on the same LAN discover your node automatically via mDNS. "
            "Use the Settings tab to generate an invite QR for devices on other networks."
        ),
    },
    {
        "id": "setup.002",
        "title": "Node Setup β€” Specialized Nodes",
        "text": (
            "Register only the capabilities your hardware supports. "
            "An OCR Raspberry Pi: register OcrService. "
            "A medical knowledge node: register RagService with a medical corpus. "
            "A thin client (phone): register no services β€” all bus calls route to peers. "
            "The bus auto-discovers and routes to the best provider in the mesh."
        ),
    },
    {
        "id": "emergency.001",
        "title": "Emergency Communication Plan",
        "text": (
            "Before a disaster: exchange node IDs with neighbours. "
            "During internet outage: HearthNet switches to offline mode automatically. "
            "All routing stays local. Use the mesh to share offers and requests. "
            "For emergency alerts, post to the Marketplace with category=emergency. "
            "Battery-powered device with HearthNet can serve the whole neighbourhood."
        ),
    },
    {
        "id": "food.001",
        "title": "Emergency Food Safety",
        "text": (
            "In a power outage, refrigerated food is safe for up to 4 hours. "
            "Frozen food stays safe for 24-48 hours if the freezer stays closed. "
            "Discard meat, poultry, seafood, dairy, or cooked food left above 4Β°C "
            "for more than 2 hours. When in doubt, throw it out."
        ),
    },
    {
        "id": "shelter.001",
        "title": "Shelter in Place",
        "text": (
            "During chemical or biological hazards, stay indoors. "
            "Close all windows and doors. Turn off HVAC. "
            "Seal gaps with wet towels or tape. "
            "Monitor emergency broadcasts on battery radio. "
            "Do not leave until authorities give the all-clear."
        ),
    },
]


def _build_node():
    """Bootstrap the HearthNet node for this Space.

    Uses HfLocalBackend (SmolLM2-135M) so inference works without Ollama.
    Falls back to _UnavailableBackend if transformers is not installed.
    """
    import hashlib
    import os
    import socket

    from hearthnet.node import HearthNode
    from hearthnet.services.chat.service import ChatService
    from hearthnet.services.files.service import FileService
    from hearthnet.services.llm.backends.hf_local import HfLocalBackend
    from hearthnet.services.llm.service import LlmService
    from hearthnet.services.marketplace.service import MarketplaceService

    # Generate a stable node_id from the HF Space hostname (so it doesn't change on restart).
    # Use SPACE_HOST env var (set only on HF Spaces) to differentiate: local nodes get
    # "local-*" prefix so they never collide with live "hf-space-*" peers in the relay.
    _space_host = os.getenv("SPACE_HOST", "")
    _host = _space_host or socket.gethostname()
    _suffix = hashlib.sha256(_host.encode()).hexdigest()[:8]
    if _space_host:
        _node_id = f"hf-space-{_suffix}"
        _display = os.getenv("SPACE_TITLE", f"HearthNet Space ({_suffix})")
    else:
        _node_id = f"local-{_suffix}"
        _display = os.getenv("SPACE_TITLE", f"HearthNet Local ({_suffix})")

    node = HearthNode(
        node_id=_node_id,
        display_name=_display,
        community_id="ed25519:hf-space-community",
    )

    # LLM β€” HF Transformers backend (SmolLM2 by default)
    try:
        backend = HfLocalBackend(model=MODEL_ID)
        # On ZeroGPU Spaces, patch the backend to use the @spaces.GPU wrapper so
        # GPU memory is properly allocated per inference call.
        if HF_SPACES:
            import asyncio
            import time as _time

            from hearthnet.services.llm.backends.base import ChatResult
            from hearthnet.services.llm.backends.hf_local import _trim_generated

            @_spaces.GPU(duration=120)
            def _gpu_pipeline_call(
                pipeline, prompt: str, max_new_tokens: int, temperature: float
            ) -> list:
                """GPU-wrapped pipeline call. ZeroGPU allocates GPU for this function."""
                return pipeline(
                    prompt,
                    max_new_tokens=max_new_tokens,
                    temperature=temperature,
                    do_sample=True,
                    return_full_text=False,
                )

            # Store the GPU wrapper on the backend so it can be replaced without
            # changing the public API.
            backend._gpu_pipeline_call = _gpu_pipeline_call  # type: ignore[attr-defined]

            async def _patched_chat(
                self,
                messages: list[dict],
                *,
                model: str = "",
                stream: bool = False,
                temperature: float = 0.7,
                max_tokens: int = 256,
                **kwargs,
            ):
                if self._pipeline is None:
                    await self.warm()
                if self._pipeline is None:
                    raise RuntimeError("HF model not loaded")
                t0 = _time.monotonic()
                prompt = self._build_prompt(messages)
                loop = asyncio.get_running_loop()
                result = await loop.run_in_executor(
                    None,
                    lambda: self._gpu_pipeline_call(
                        self._pipeline, prompt, max_tokens, temperature
                    ),
                )
                raw = result[0]["generated_text"] if result else ""
                text = _trim_generated(raw)
                ms = int((_time.monotonic() - t0) * 1000)
                return ChatResult(
                    text=text,
                    tokens_in=len(prompt.split()),
                    tokens_out=len(text.split()),
                    model=self._model_name,
                    ms=ms,
                )

            HfLocalBackend.chat = _patched_chat  # type: ignore[method-assign]

        backends: list = [backend]
        # ── Sponsor cloud backends (opt-in via env) ───────────────────────
        # NVIDIA Nemotron (prize track) β€” cloud NIM, no local availability check.
        if os.getenv("NVIDIA_API_KEY"):
            try:
                from hearthnet.services.llm.backends.nemotron import NemotronBackend

                backends.append(NemotronBackend(api_key_env="NVIDIA_API_KEY"))
            except Exception:
                pass
        # Modal serverless GPU (prize track).
        if os.getenv("MODAL_ENDPOINT"):
            try:
                from hearthnet.services.llm.backends.modal_backend import ModalBackend

                modal_b = ModalBackend()
                if modal_b.is_available():
                    backends.append(modal_b)
            except Exception:
                pass
        # MiniCPM local server (OpenBMB prize track).
        # MINICPM_URL          β†’ OpenAI-compatible vLLM/SGLang/llama.cpp endpoint
        # MINICPM_MODELS       β†’ comma-separated model ids to advertise (multi-model
        #                        serving from one server). Omit β†’ full MiniCPM catalogue.
        # MINICPM_LIGHTWEIGHT  β†’ "1" to also advertise Pi-friendly small models.
        _minicpm_url = os.getenv("MINICPM_URL")
        if _minicpm_url:
            try:
                from hearthnet.services.llm.backends.openbmb import OpenBmbBackend

                _models_env = os.getenv("MINICPM_MODELS", "")
                _models = [m.strip() for m in _models_env.split(",") if m.strip()] or None
                _lightweight = os.getenv("MINICPM_LIGHTWEIGHT", "") in ("1", "true", "yes")
                minicpm = OpenBmbBackend(
                    base_url=_minicpm_url,
                    models=_models,
                    include_lightweight=_lightweight,
                )
                if minicpm.is_available():
                    backends.append(minicpm)
            except Exception:
                pass

        llm = LlmService(backends=backends)
    except Exception:
        llm = LlmService()  # _UnavailableBackend β€” shows clear error

    node.bus.register_service(llm)

    # ── Durable event log (ZeroGPU-safe; no mDNS/transport on a single Space) ──
    event_log = None
    try:
        import tempfile
        from pathlib import Path

        from hearthnet.events import EventLog

        _data_dir = Path(os.getenv("HEARTHNET_DATA_DIR", tempfile.gettempdir())) / "hearthnet-space"
        _data_dir.mkdir(parents=True, exist_ok=True)
        event_log = EventLog(_data_dir / "events.db", node.community_id, node.node_id)
        node._event_log = event_log
    except Exception:
        event_log = None

    # ── Blob store for content-addressed RAG documents ────────────────────
    blob_store = None
    try:
        import tempfile
        from pathlib import Path

        from hearthnet.blobs.store import BlobStore

        blob_store = BlobStore(
            Path(os.getenv("HEARTHNET_DATA_DIR", tempfile.gettempdir()))
            / "hearthnet-space"
            / "blobs"
        )
    except Exception:
        blob_store = None

    # ── Real semantic RAG (replaces the in-memory demo corpus) ────────────
    from hearthnet.bus.capability import RouteRequest
    from hearthnet.services.rag.federated import FederatedRagService
    from hearthnet.services.rag.service import RagService

    # Register the embedding backend first so rag.query routes through embed.text.
    node.install_extended_services(research=True)

    import tempfile

    _data_env = os.getenv("HEARTHNET_DATA_DIR", "")
    _data_base = Path(_data_env) if _data_env else Path(tempfile.gettempdir())
    # Verify the base path (or its first existing ancestor) is writable.
    # Falls back to tempdir if e.g. /data persistent storage isn't mounted.
    _check = _data_base
    while not _check.exists():
        _check = _check.parent
    if not os.access(_check, os.W_OK):
        _data_base = Path(tempfile.gettempdir())
        print(f"[hearthnet] HEARTHNET_DATA_DIR {_data_env!r} not writable, using tmpdir")
    _corpora_dir = _data_base / "hearthnet-space" / "corpora"
    rag = RagService(
        corpus="community",
        corpora_dir=_corpora_dir,
        bus=node.bus,
        event_log=event_log,
        blob_store=blob_store,
    )
    node.bus.register_service(rag)
    node.bus.register_service(FederatedRagService(node.bus, corpus="community"))

    # Seed the corpus through the real ingest path (content-addressed + logged).
    async def _seed_corpus() -> None:
        import pathlib

        # 1. Fixed emergency seed documents (water, first aid, CPR, etc.)
        for doc in SEED_CORPUS:
            with contextlib.suppress(Exception):
                await rag.handle_ingest(
                    RouteRequest(
                        capability="rag.ingest",
                        version_req=(1, 0),
                        body={
                            "input": {
                                "corpus": "community",
                                "documents": [
                                    {
                                        "id": doc["id"],
                                        "title": doc["title"],
                                        "text": doc["text"],
                                    }
                                ],
                            }
                        },
                        caller=node.node_id,
                        trace_id="seed",
                        deadline_ms=0,
                    )
                )

        # 2. Ingest all .md / .txt files from docs/ (main), docs/guides/, assets/initial_docs/.
        # Files are content-addressed (BLAKE3), so re-ingesting the same file is a no-op.
        _app_root = pathlib.Path(__file__).parent
        _doc_dirs = [
            _app_root / "docs",  # Main docs: CAPABILITY_CONTRACT, GLOSSARY, M01-M13, X01-X04, etc.
            _app_root / "docs" / "guides",
            _app_root / "assets" / "initial_docs",
        ]
        _text_suffixes = {".md", ".txt", ".rst"}
        for _doc_dir in _doc_dirs:
            if not _doc_dir.exists():
                continue
            for _doc_file in sorted(_doc_dir.rglob("*")):
                if _doc_file.suffix.lower() not in _text_suffixes:
                    continue
                with contextlib.suppress(Exception):
                    _text = _doc_file.read_text(encoding="utf-8", errors="replace")
                    if len(_text.strip()) < 80:
                        continue  # skip near-empty or placeholder files
                    _title = _doc_file.stem.replace("-", " ").replace("_", " ").title()
                    _doc_id = f"file:{_doc_file.relative_to(_app_root).as_posix()}"
                    await rag.handle_ingest(
                        RouteRequest(
                            capability="rag.ingest",
                            version_req=(1, 0),
                            body={
                                "input": {
                                    "text": _text,
                                    "title": _title,
                                    "doc_cid": _doc_id,
                                }
                            },
                            caller=node.node_id,
                            trace_id="seed-docs",
                            deadline_ms=0,
                        )
                    )

    # Run seed corpus in a dedicated thread with its own event loop to avoid
    # conflicts with any loop already running (e.g. Gradio's internal loop).
    import asyncio
    import threading

    def _seed_in_thread() -> None:
        loop = asyncio.new_event_loop()
        asyncio.set_event_loop(loop)
        try:
            loop.run_until_complete(_seed_corpus())
        except Exception:
            pass
        finally:
            loop.close()

    _seed_thread = threading.Thread(target=_seed_in_thread, daemon=True, name="hearthnet-seed")
    _seed_thread.start()
    _seed_thread.join(timeout=60)  # wait up to 60 s; don't block Space startup indefinitely

    # Register this node's LLM model as an expert in the MoE registry so
    # route_expert tool calls return meaningful results instead of an empty list.
    try:
        _moe_tags = list({
            doc.get("id", "").split(".")[0]
            for doc in SEED_CORPUS
            if doc.get("id")
        } | {"emergency", "mesh", "community"})
        loop_moe = asyncio.new_event_loop()
        loop_moe.run_until_complete(
            node.bus.call(
                "moe.register",
                (1, 0),
                {
                    "input": {
                        "expert_id": f"model:{MODEL_ID}",
                        "expert_type": "model",
                        "topic_tags": _moe_tags,
                        "confidence_score": 0.6,
                        "community_id": node.community_id,
                        "name": MODEL_ID.split("/")[-1],
                        "ttl_seconds": 0,
                    }
                },
            )
        )
        loop_moe.close()
    except Exception:
        pass

    # Marketplace, Chat, Files β€” now durably event-sourced where supported.
    node.bus.register_service(MarketplaceService(event_log=event_log, node_id=node.node_id))
    node.bus.register_service(ChatService(node.node_id, event_log=event_log, bus=node.bus))
    node.bus.register_service(FileService())

    return node


# Build node and Gradio app at import time (HF Spaces requires module-level `demo`)
_node = _build_node()

# ── Local-only: start mDNS peer discovery + HTTP bus transport ────────────────
# On HF Space (SPACE_HOST set): port 7080 is not exposed to the internet and mDNS
# doesn't cross network boundaries β€” the relay hub handles internet peering instead.
# Locally: node.start() activates zero-config LAN discovery and makes this node's
# bus callable by other nodes over HTTP so RAG, chat, and LLM route across devices.
if not os.getenv("SPACE_HOST"):
    import asyncio as _asyncio
    import threading as _threading

    def _run_local_networking() -> None:
        _loop = _asyncio.new_event_loop()
        _asyncio.set_event_loop(_loop)
        try:
            # node._event_log is already set by _build_node(); start() reuses it
            # (see the "already set" guard added to node.start()).
            _loop.run_until_complete(_node.start(port=7080))
            _loop.run_forever()
        except Exception as _exc:
            print(f"[hearthnet] local networking start failed: {_exc}")

    _threading.Thread(
        target=_run_local_networking, daemon=True, name="hearthnet-local-node"
    ).start()

# Relay hub: pull-based mailbox router so NAT-bound nodes mesh all-to-all through
# this public Space (see hearthnet/transport/relay_hub.py). Members poll their
# mailbox over HTTPS; the Space never needs to reach back into a home network.
from hearthnet.transport.relay_hub import RelayHub as _RelayHub  # noqa: E402
from hearthnet.transport.relay_hub import mount_relay_endpoints as _mount_relay_endpoints  # noqa: E402

import tempfile as _tempfile
from pathlib import Path as _Path2

_relay_db_path = (
    _Path2(os.getenv("HEARTHNET_DATA_DIR", _tempfile.gettempdir()))
    / "hearthnet-space"
    / "relay.db"
)
_relay_db_path.parent.mkdir(parents=True, exist_ok=True)
_relay_hub = _RelayHub(db_path=_relay_db_path)

from hearthnet.ui.app import build_ui as _build_ui  # noqa: E402

_ui = _build_ui(
    bus=_node.bus,
    state_bus=_node.state_bus,
    node=_node,
    display_name=_node.display_name,
    node_id=_node.node_id,
    community_id=_node.community_id,
)

demo = _ui.build()

# ── Serve webagent at /webagent/ ──────────────────────────────────────────────
# HF Space enables Gradio SSR mode (GRADIO_SSR_MODE=true), where a Node.js layer
# intercepts ALL requests before Python/FastAPI sees them, making StaticFiles
# mounts invisible. Fix: force SSR off so Python handles all requests directly.
from pathlib import Path as _Path

import gradio as _gr

_webagent_dir = _Path(__file__).parent / "webagent"

# 1) Override the env var that launch() reads when ssr_mode param is None
os.environ["GRADIO_SSR_MODE"] = "false"

# 2) Also patch _resolve_ssr_mode in case HF passes ssr_mode=True explicitly
_gr.Blocks._resolve_ssr_mode = lambda self, ssr_mode=None, **kw: False


def _mount_bus_endpoints(app) -> None:
    """Expose the node's capability bus on the Space's public port.

    On HF Spaces only the Gradio port is reachable from the internet β€” the
    node's internal HttpServer (port 7080) is not. Mounting the bus RPC
    endpoints directly into the Gradio FastAPI app lets a remote/local node
    peer with this Space via ``discovery.peer.add`` and route real
    ``llm.chat`` / ``rag.query`` / ``moe.*`` calls to it over HTTPS.
    """
    try:
        from fastapi import Body
        from fastapi.responses import JSONResponse
    except Exception as exc:  # pragma: no cover
        print(f"[hearthnet] bus endpoint mount skipped: {exc}")
        return

    if any(getattr(r, "path", "") == "/bus/v1/call" for r in app.routes):
        return

    def _parse_version(v) -> tuple[int, int]:
        parts = str(v).split(".")
        if len(parts) < 2:
            parts.append("0")
        return (int(parts[0]), int(parts[1]))

    @app.get("/manifest")
    async def _hn_manifest():
        return JSONResponse(_node.manifest().as_dict())

    @app.get("/health")
    async def _hn_health():
        return JSONResponse({"status": "ok", "node_id": _node.node_id})

    @app.get("/bus/v1/capabilities")
    async def _hn_capabilities():
        return JSONResponse([e.descriptor.name for e in _node.bus.registry.all_local()])

    @app.post("/bus/v1/call")
    async def _hn_bus_call(payload: dict = Body(...)):
        capability = payload.get("capability")
        if not capability:
            return JSONResponse(
                {"error": "bad_request", "message": "capability required"}, status_code=400
            )
        version = _parse_version(payload.get("version", "1.0"))
        call_body = {
            "params": payload.get("params", {}),
            "input": payload.get("input", {}),
        }
        try:
            result = await _node.bus.call(capability, version, call_body)
            return JSONResponse(result)
        except Exception as exc:
            code = getattr(exc, "code", "call_error")
            return JSONResponse({"error": code, "message": str(exc)}, status_code=500)

    # New routes are appended last; move them ahead of Gradio's SPA catch-all.
    for _path in ("/bus/v1/call", "/bus/v1/capabilities", "/manifest", "/health"):
        for _i in range(len(app.routes) - 1, -1, -1):
            if getattr(app.routes[_i], "path", "") == _path:
                app.routes.insert(0, app.routes.pop(_i))
                break


# 3) Patch App.create_app to inject the StaticFiles mount after Gradio routes
if _webagent_dir.exists():
    try:
        import gradio.routes as _gr_routes
        from fastapi.staticfiles import StaticFiles as _SF

        _orig_create_app = _gr_routes.App.__dict__["create_app"].__func__

        def _patched_create_app(blocks, app=None, **kwargs):
            result = _orig_create_app(blocks, app=app, **kwargs)
            try:
                if not any(getattr(r, "name", "") == "webagent" for r in result.routes):
                    result.mount("/webagent", _SF(directory=str(_webagent_dir)), name="webagent")
                    _wrt = result.routes.pop()
                    result.routes.insert(0, _wrt)
            except Exception as _me:
                print(f"[hearthnet] webagent mount: {_me}")
            _mount_bus_endpoints(result)
            _mount_relay_endpoints(result, _relay_hub)

            # Auto-join: the Space node registers itself in its own relay hub
            # so remote nodes that connect see it in the roster immediately.
            try:
                _caps = [
                    f"{e.descriptor.name}@{e.descriptor.version[0]}.{e.descriptor.version[1]}"
                    for e in _node.bus.registry.all_local()
                ]
                _nid = getattr(_node, "node_id_full", _node.node_id)
                _relay_hub.join(
                    _nid,
                    display_name=_node.display_name,
                    community_id=_node.community_id,
                    capabilities=_caps,
                    endpoint="",
                )
                _relay_hub.set_local_handler(_nid, _node.bus)
                print(f"[hearthnet] Space node '{_node.display_name}' joined local relay hub")
            except Exception as _je:
                print(f"[hearthnet] self-join relay failed: {_je}")
            return result

        _gr_routes.App.create_app = staticmethod(_patched_create_app)
    except Exception as _pe:
        print(f"[hearthnet] create_app patch failed: {_pe}")

if __name__ == "__main__":
    import os

    # HF Spaces health-checks port 7860. Bind explicitly and disable Gradio
    # SSR mode (Node proxy on a different port crashes on HF and the health
    # check on :7860 then times out -> "workload was not healthy").
    _port = int(os.environ.get("GRADIO_SERVER_PORT", "7860"))
    demo.launch(
        server_name="0.0.0.0",
        server_port=_port,
        ssr_mode=False,
    )