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
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8514223
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
"""X04 - Configuration.

Spec: docs/X04-config.md
Impl-ref: impl_ref.md §1

All config in typed frozen dataclasses.
Config file: ~/.hearthnet/config.toml

Example config.toml:
  [transport]
  host = "0.0.0.0"
  port = 7080

  [[llm.backends]]
  name = "ollama"
  url  = "http://localhost:11434"

  [[llm.backends]]
  name  = "openbmb"
  url   = "http://localhost:8000"
  model = "openbmb/MiniCPM4-8B"
"""
from __future__ import annotations

import os
import tomllib  # stdlib ≥ 3.11; fallback below
from dataclasses import dataclass, field
from pathlib import Path
from typing import Optional

from hearthnet.constants import (
    CHUNK_SIZE_BYTES,
    EMBED_DEFAULT_MODEL,
    HTTP_PORT,
    MARKET_DEFAULT_TTL_SECONDS,
    MARKET_MAX_TTL_SECONDS,
    UI_PORT,
)

# ── Fall back to tomli for Python < 3.11 ────────────────────────────────────
try:
    import tomllib
except ImportError:
    try:
        import tomli as tomllib  # type: ignore[no-redef]
    except ImportError:
        tomllib = None  # type: ignore[assignment]


# ── Sub-config dataclasses ───────────────────────────────────────────────────

@dataclass(frozen=True)
class IdentityConfig:
    keys_dir: Path = field(default_factory=lambda: Path())
    auto_generate: bool = True


@dataclass(frozen=True)
class CommunityConfig:
    community_id: str | None = None
    state_dir: Path = field(default_factory=lambda: Path())


@dataclass(frozen=True)
class TransportConfig:
    host: str = "0.0.0.0"
    port: int = HTTP_PORT
    tls_cert: Path | None = None
    tls_key: Path | None = None


@dataclass(frozen=True)
class DiscoveryConfig:
    mdns_enabled: bool = True
    udp_enabled: bool = True
    udp_port: int = 7079
    relay_urls: tuple[str, ...] = field(default_factory=tuple)


@dataclass(frozen=True)
class BusConfig:
    prefer_local: bool = True
    local_load_threshold: float = 0.80


@dataclass(frozen=True)
class LlmBackendConfig:
    name: str
    model: str = ""
    base_url: str = ""
    api_key_env: str | None = None
    extra: dict = field(default_factory=dict)


@dataclass(frozen=True)
class LlmConfig:
    backends: tuple[LlmBackendConfig, ...] = field(default_factory=tuple)


@dataclass(frozen=True)
class EmbeddingConfig:
    model: str = EMBED_DEFAULT_MODEL
    device: str = "auto"


@dataclass(frozen=True)
class RagConfig:
    enabled: bool = True
    corpora_dir: Path = field(default_factory=lambda: Path())


@dataclass(frozen=True)
class FileConfig:
    blobs_dir: Path = field(default_factory=lambda: Path())
    chunk_size_bytes: int = CHUNK_SIZE_BYTES
    gc_threshold: float = 0.80


@dataclass(frozen=True)
class MarketConfig:
    enabled: bool = True
    default_ttl_seconds: int = MARKET_DEFAULT_TTL_SECONDS
    max_ttl_seconds: int = MARKET_MAX_TTL_SECONDS


@dataclass(frozen=True)
class ChatConfig:
    enabled: bool = True
    store_and_forward: bool = True
    read_receipts_enabled: bool = True


@dataclass(frozen=True)
class EmergencyConfig:
    probe_targets: tuple[str, ...] = field(
        default_factory=lambda: (
            "1.1.1.1",
            "8.8.8.8",
            "https://cloudflare.com",
            "https://quad9.net",
        )
    )


@dataclass(frozen=True)
class UiConfig:
    host: str = "127.0.0.1"
    port: int = UI_PORT
    launch_browser: bool = True


@dataclass(frozen=True)
class ObservabilityConfig:
    log_level: str = "info"
    log_dir: Path | None = None
    metrics_enabled: bool = True
    otlp_endpoint: str | None = None


@dataclass(frozen=True)
class ResearchConfig:
    """Phase 3 experimental feature flags. All default False."""
    enable: bool = False
    distributed_inference: bool = False
    moe_routing: bool = False
    federated_learning: bool = False
    lora_beacons: bool = False
    evidence_graph: bool = False
    civil_defense: bool = False
    conformance_suite: bool = False


@dataclass(frozen=True)
class Config:
    identity: IdentityConfig = field(default_factory=IdentityConfig)
    community: CommunityConfig = field(default_factory=CommunityConfig)
    transport: TransportConfig = field(default_factory=TransportConfig)
    discovery: DiscoveryConfig = field(default_factory=DiscoveryConfig)
    bus: BusConfig = field(default_factory=BusConfig)
    llm: LlmConfig = field(default_factory=LlmConfig)
    embedding: EmbeddingConfig = field(default_factory=EmbeddingConfig)
    rag: RagConfig = field(default_factory=RagConfig)
    file: FileConfig = field(default_factory=FileConfig)
    market: MarketConfig = field(default_factory=MarketConfig)
    chat: ChatConfig = field(default_factory=ChatConfig)
    emergency: EmergencyConfig = field(default_factory=EmergencyConfig)
    ui: UiConfig = field(default_factory=UiConfig)
    observability: ObservabilityConfig = field(default_factory=ObservabilityConfig)
    research: ResearchConfig = field(default_factory=ResearchConfig)


# ── ConfigError ───────────────────────────────────────────────────────────────

class ConfigError(Exception):
    def __init__(self, code: str, **kwargs: object) -> None:
        super().__init__(code)
        self.code = code
        self.context = kwargs


# ── XDG path resolution ───────────────────────────────────────────────────────

def _xdg_data() -> Path:
    raw = os.environ.get("XDG_DATA_HOME") or os.path.expanduser("~/.local/share")
    return Path(raw) / "hearthnet"


def _xdg_config() -> Path:
    raw = os.environ.get("XDG_CONFIG_HOME") or os.path.expanduser("~/.config")
    return Path(raw) / "hearthnet"


def _xdg_cache() -> Path:
    raw = os.environ.get("XDG_CACHE_HOME") or os.path.expanduser("~/.cache")
    return Path(raw) / "hearthnet"


def _default_config_path() -> Path:
    return _xdg_config() / "config.toml"


# ── Path resolution ───────────────────────────────────────────────────────────

def resolve_paths(config: Config) -> Config:
    """Fill empty Path() fields with XDG-standard locations. Idempotent."""
    data = _xdg_data()
    cache = _xdg_cache()
    cfg = _xdg_config()

    identity = config.identity
    if identity.keys_dir == Path():
        identity = IdentityConfig(
            keys_dir=data / "keys",
            auto_generate=identity.auto_generate,
        )

    community = config.community
    if community.state_dir == Path():
        cid = community.community_id or "default"
        community = CommunityConfig(
            community_id=community.community_id,
            state_dir=data / "communities" / cid,
        )

    transport = config.transport
    tls_cert = transport.tls_cert or data / "tls" / "server.crt"
    tls_key = transport.tls_key or data / "tls" / "server.key"
    transport = TransportConfig(
        host=transport.host,
        port=transport.port,
        tls_cert=tls_cert,
        tls_key=tls_key,
    )

    rag = config.rag
    if rag.corpora_dir == Path():
        rag = RagConfig(enabled=rag.enabled, corpora_dir=cache / "embeddings")

    file_cfg = config.file
    if file_cfg.blobs_dir == Path():
        file_cfg = FileConfig(
            blobs_dir=data / "blobs",
            chunk_size_bytes=file_cfg.chunk_size_bytes,
            gc_threshold=file_cfg.gc_threshold,
        )

    obs = config.observability
    if obs.log_dir is None:
        obs = ObservabilityConfig(
            log_level=obs.log_level,
            log_dir=data / "logs",
            metrics_enabled=obs.metrics_enabled,
            otlp_endpoint=obs.otlp_endpoint,
        )

    return Config(
        identity=identity,
        community=community,
        transport=transport,
        discovery=config.discovery,
        bus=config.bus,
        llm=config.llm,
        embedding=config.embedding,
        rag=rag,
        file=file_cfg,
        market=config.market,
        chat=config.chat,
        emergency=config.emergency,
        ui=config.ui,
        observability=obs,
    )


# ── Validation ────────────────────────────────────────────────────────────────

def validate(config: Config) -> None:
    """Cross-field validation. Raises ConfigError on failure."""
    t = config.transport
    d = config.discovery
    if t.port == d.udp_port:
        raise ConfigError("invalid_field", field="transport.port/discovery.udp_port",
                          reason="transport port and UDP discovery port must differ")
    if not (1 <= t.port <= 65535):
        raise ConfigError("invalid_field", field="transport.port", reason="port out of range")
    if config.bus.local_load_threshold <= 0 or config.bus.local_load_threshold > 1:
        raise ConfigError("invalid_field", field="bus.local_load_threshold",
                          reason="must be in (0, 1]")


# ── TOML parsing helpers ──────────────────────────────────────────────────────

def _parse_toml(text: str) -> dict:
    if tomllib is None:
        raise ConfigError("invalid_toml", reason="no TOML parser available (install tomli)")
    try:
        return tomllib.loads(text)
    except Exception as exc:
        raise ConfigError("invalid_toml", reason=str(exc)) from exc


def _from_dict(raw: dict) -> Config:
    def _path(v: object) -> Path:
        return Path(v) if v else Path()

    identity_raw = raw.get("identity", {})
    identity = IdentityConfig(
        keys_dir=_path(identity_raw.get("keys_dir")),
        auto_generate=bool(identity_raw.get("auto_generate", True)),
    )

    community_raw = raw.get("community", {})
    community = CommunityConfig(
        community_id=community_raw.get("community_id") or None,
        state_dir=_path(community_raw.get("state_dir")),
    )

    transport_raw = raw.get("transport", {})
    transport = TransportConfig(
        host=str(transport_raw.get("host", "0.0.0.0")),
        port=int(transport_raw.get("port", HTTP_PORT)),
        tls_cert=_path(transport_raw.get("tls_cert")) or None,
        tls_key=_path(transport_raw.get("tls_key")) or None,
    )

    discovery_raw = raw.get("discovery", {})
    discovery = DiscoveryConfig(
        mdns_enabled=bool(discovery_raw.get("mdns_enabled", True)),
        udp_enabled=bool(discovery_raw.get("udp_enabled", True)),
        udp_port=int(discovery_raw.get("udp_port", 7079)),
        relay_urls=tuple(discovery_raw.get("relay_urls", [])),
    )

    bus_raw = raw.get("bus", {})
    bus = BusConfig(
        prefer_local=bool(bus_raw.get("prefer_local", True)),
        local_load_threshold=float(bus_raw.get("local_load_threshold", 0.80)),
    )

    llm_raw = raw.get("llm", {})
    backends = []
    for b in llm_raw.get("backends", []):
        backends.append(LlmBackendConfig(
            name=str(b["name"]),
            model=str(b.get("model", "")),
            base_url=str(b.get("base_url", "")),
            api_key_env=b.get("api_key_env") or None,
        ))
    llm = LlmConfig(backends=tuple(backends))

    embedding_raw = raw.get("embedding", {})
    embedding = EmbeddingConfig(
        model=str(embedding_raw.get("model", EMBED_DEFAULT_MODEL)),
        device=str(embedding_raw.get("device", "auto")),
    )

    rag_raw = raw.get("rag", {})
    rag = RagConfig(
        enabled=bool(rag_raw.get("enabled", True)),
        corpora_dir=_path(rag_raw.get("corpora_dir")),
    )

    file_raw = raw.get("file", {})
    file_cfg = FileConfig(
        blobs_dir=_path(file_raw.get("blobs_dir")),
        chunk_size_bytes=int(file_raw.get("chunk_size_bytes", CHUNK_SIZE_BYTES)),
        gc_threshold=float(file_raw.get("gc_threshold", 0.80)),
    )

    market_raw = raw.get("market", {})
    market = MarketConfig(
        enabled=bool(market_raw.get("enabled", True)),
        default_ttl_seconds=int(market_raw.get("default_ttl_seconds", MARKET_DEFAULT_TTL_SECONDS)),
        max_ttl_seconds=int(market_raw.get("max_ttl_seconds", MARKET_MAX_TTL_SECONDS)),
    )

    chat_raw = raw.get("chat", {})
    chat = ChatConfig(
        enabled=bool(chat_raw.get("enabled", True)),
        store_and_forward=bool(chat_raw.get("store_and_forward", True)),
        read_receipts_enabled=bool(chat_raw.get("read_receipts_enabled", True)),
    )

    emergency_raw = raw.get("emergency", {})
    emergency = EmergencyConfig(
        probe_targets=tuple(emergency_raw.get("probe_targets", [
            "1.1.1.1", "8.8.8.8", "https://cloudflare.com", "https://quad9.net",
        ])),
    )

    ui_raw = raw.get("ui", {})
    ui = UiConfig(
        host=str(ui_raw.get("host", "127.0.0.1")),
        port=int(ui_raw.get("port", UI_PORT)),
        launch_browser=bool(ui_raw.get("launch_browser", True)),
    )

    obs_raw = raw.get("observability", {})
    obs = ObservabilityConfig(
        log_level=str(obs_raw.get("log_level", "info")),
        log_dir=_path(obs_raw.get("log_dir")) or None,
        metrics_enabled=bool(obs_raw.get("metrics_enabled", True)),
        otlp_endpoint=obs_raw.get("otlp_endpoint") or None,
    )

    return Config(
        identity=identity,
        community=community,
        transport=transport,
        discovery=discovery,
        bus=bus,
        llm=llm,
        embedding=embedding,
        rag=rag,
        file=file_cfg,
        market=market,
        chat=chat,
        emergency=emergency,
        ui=ui,
        observability=obs,
    )


# ── Public API ────────────────────────────────────────────────────────────────

def default_config() -> Config:
    """Return a Config populated entirely from defaults."""
    return resolve_paths(Config())


def load(path: Path | None = None) -> Config:
    """Load from TOML file; apply defaults for omitted sections; validate."""
    cfg_path = path or _default_config_path()
    if not cfg_path.exists():
        cfg = default_config()
        validate(cfg)
        return cfg
    try:
        text = cfg_path.read_text(encoding="utf-8")
    except OSError as exc:
        raise ConfigError("path_resolution", reason=str(exc)) from exc
    raw = _parse_toml(text)
    cfg = resolve_paths(_from_dict(raw))
    validate(cfg)
    return cfg


def save(config: Config, path: Path | None = None) -> None:
    """Serialise config to TOML atomically."""
    import tempfile

    cfg_path = path or _default_config_path()
    cfg_path.parent.mkdir(parents=True, exist_ok=True)

    lines: list[str] = []
    lines.append("[identity]")
    lines.append(f'keys_dir = "{config.identity.keys_dir}"')
    lines.append(f"auto_generate = {str(config.identity.auto_generate).lower()}")
    lines.append("")
    lines.append("[community]")
    if config.community.community_id:
        lines.append(f'community_id = "{config.community.community_id}"')
    lines.append(f'state_dir = "{config.community.state_dir}"')
    lines.append("")
    lines.append("[transport]")
    lines.append(f'host = "{config.transport.host}"')
    lines.append(f"port = {config.transport.port}")
    if config.transport.tls_cert:
        lines.append(f'tls_cert = "{config.transport.tls_cert}"')
    if config.transport.tls_key:
        lines.append(f'tls_key = "{config.transport.tls_key}"')
    lines.append("")
    lines.append("[discovery]")
    lines.append(f"mdns_enabled = {str(config.discovery.mdns_enabled).lower()}")
    lines.append(f"udp_enabled = {str(config.discovery.udp_enabled).lower()}")
    lines.append(f"udp_port = {config.discovery.udp_port}")
    if config.discovery.relay_urls:
        urls = ", ".join(f'"{u}"' for u in config.discovery.relay_urls)
        lines.append(f"relay_urls = [{urls}]")
    lines.append("")
    lines.append("[bus]")
    lines.append(f"prefer_local = {str(config.bus.prefer_local).lower()}")
    lines.append(f"local_load_threshold = {config.bus.local_load_threshold}")
    lines.append("")
    lines.append("[embedding]")
    lines.append(f'model = "{config.embedding.model}"')
    lines.append(f'device = "{config.embedding.device}"')
    lines.append("")
    lines.append("[rag]")
    lines.append(f"enabled = {str(config.rag.enabled).lower()}")
    lines.append(f'corpora_dir = "{config.rag.corpora_dir}"')
    lines.append("")
    lines.append("[observability]")
    lines.append(f'log_level = "{config.observability.log_level}"')
    lines.append(f"metrics_enabled = {str(config.observability.metrics_enabled).lower()}")
    if config.observability.log_dir:
        lines.append(f'log_dir = "{config.observability.log_dir}"')

    content = "\n".join(lines) + "\n"
    fd, tmp = tempfile.mkstemp(dir=cfg_path.parent, prefix=".config_tmp")
    try:
        with os.fdopen(fd, "w", encoding="utf-8") as fh:
            fh.write(content)
        os.replace(tmp, cfg_path)
    except Exception:
        try:
            os.unlink(tmp)
        except OSError:
            pass
        raise