File size: 21,663 Bytes
919fd68
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Portable, inference-only Release 188 model assembly.

The public repository is one standard safetensors weight map.  This module
combines its four runtime roles without consulting a training checkpoint,
private manifest, optimizer, or host-specific path:

* the immutable lexical/projection parent;
* the dense ResynthesisRBO graph;
* the resident NoNE routers (including their learned trauma state); and
* lazily materialized page tensors selected by those routers.

Generation remains uncapped at the host boundary.  The trained additive
completion surfaces inside :func:`resynthesis_rbo_generate` own stopping.
"""

from __future__ import annotations

import contextlib
import json
from collections.abc import Callable, Mapping
from dataclasses import dataclass
from pathlib import Path, PurePosixPath
from typing import Final, cast

import torch
from safetensors import safe_open

from resynthesis.config import ResynthesisConfig
from resynthesis.none_migration import (
    NONE_V2_RESIDENT_RUNTIME_SCHEMA,
    build_checkpoint_direct_science_stack_boundary,
)
from resynthesis.none_paging import (
    NoNEImmutablePageStore,
    NoNEPagedExpertRuntime,
)
from resynthesis.rbo import (
    COMPOSED_ADDITIVE_LINEAGE_SCHEMA,
    RBOEmissionTracePacket,
    RBOGenerationResult,
    ResynthesisRBO,
    ResynthesisRBOConfig,
    resynthesis_rbo_generate,
)
from resynthesis.release_page_store import (
    RELEASE_188_GENERATION,
    Release188SafetensorsPageStore,
)
from resynthesis.release_parent import (
    RELEASE_188_LEXICAL_ROWS,
    Release188ParentAdapter,
)
from resynthesis.science_layers import ResynthesisScienceLayerConfig

RELEASE_188_RUNTIME_SCHEMA: Final[str] = (
    "nnf.resynthesis.release_runtime.v1"
)
RELEASE_188_SCIENCE_LAYERS: Final[int] = 18
RELEASE_188_SCIENCE_EXPERTS: Final[int] = 8
RELEASE_188_DEVICE_POLICY: Final[str] = (
    "single_best_free_gpu_else_cpu"
)
RELEASE_188_DTYPE_POLICY: Final[str] = "auto"
RELEASE_188_MINIMUM_GPU_HEADROOM_BYTES: Final[int] = 48 * 1024**3
_DENSE_FILENAME: Final[str] = (
    "weights/safetensors/resynthesis.safetensors"
)
_RESIDENT_FILENAME: Final[str] = (
    "weights/safetensors/resident-runtime.safetensors"
)
_SPEC_FIELDS: Final[frozenset[str]] = frozenset(
    (
        "schema",
        "release",
        "model_index",
        "dtype",
        "device_policy",
        "science_layers",
        "science_experts",
    )
)


@dataclass(frozen=True)
class Release188RuntimeSpec:
    """Minimal public configuration resolved from ``runtime/model.json``."""

    source_path: Path
    model_index_path: Path
    dtype_policy: str
    device_policy: str
    science_layers: int
    science_experts: int

    @property
    def release_root(self) -> Path:
        """Return the public repository root containing the weight map."""

        return self.model_index_path.parent


def release_188_runtime_payload() -> dict[str, object]:
    """Return the complete portable ``runtime/model.json`` payload."""

    return {
        "schema": RELEASE_188_RUNTIME_SCHEMA,
        "release": RELEASE_188_GENERATION,
        "model_index": "../model.safetensors.index.json",
        "dtype": RELEASE_188_DTYPE_POLICY,
        "device_policy": RELEASE_188_DEVICE_POLICY,
        "science_layers": RELEASE_188_SCIENCE_LAYERS,
        "science_experts": RELEASE_188_SCIENCE_EXPERTS,
    }


def load_release_188_runtime_spec(
    spec_path: str | Path,
) -> Release188RuntimeSpec:
    """Load the small public runtime contract without accepting local paths."""

    resolved_spec = Path(spec_path).expanduser().resolve()
    if not resolved_spec.is_file():
        raise FileNotFoundError(
            f"Release 188 runtime specification is absent: {resolved_spec}"
        )
    with resolved_spec.open(encoding="utf-8") as handle:
        payload: object = json.load(handle)
    if not isinstance(payload, dict) or set(payload) != _SPEC_FIELDS:
        raise RuntimeError("Release 188 runtime specification fields differ")
    if (
        payload.get("schema") != RELEASE_188_RUNTIME_SCHEMA
        or payload.get("release") != RELEASE_188_GENERATION
        or payload.get("dtype") != RELEASE_188_DTYPE_POLICY
        or payload.get("device_policy") != RELEASE_188_DEVICE_POLICY
        or payload.get("science_layers") != RELEASE_188_SCIENCE_LAYERS
        or payload.get("science_experts") != RELEASE_188_SCIENCE_EXPERTS
    ):
        raise RuntimeError("Release 188 runtime specification differs")
    model_index_value = payload.get("model_index")
    if not isinstance(model_index_value, str):
        raise RuntimeError("Release 188 model index path is malformed")
    relative_index = PurePosixPath(model_index_value)
    if relative_index.is_absolute():
        raise RuntimeError("Release 188 model index must be repository-relative")
    model_index_path = resolved_spec.parent.joinpath(
        *relative_index.parts
    ).resolve()
    expected_index_path = (
        resolved_spec.parent.parent / "model.safetensors.index.json"
    ).resolve()
    if (
        model_index_path != expected_index_path
        or model_index_path.name != "model.safetensors.index.json"
        or not model_index_path.is_file()
    ):
        raise RuntimeError(
            "Release 188 runtime does not point to the repository weight map"
        )
    return Release188RuntimeSpec(
        source_path=resolved_spec,
        model_index_path=model_index_path,
        dtype_policy=cast(str, payload["dtype"]),
        device_policy=cast(str, payload["device_policy"]),
        science_layers=cast(int, payload["science_layers"]),
        science_experts=cast(int, payload["science_experts"]),
    )


def select_release_188_device_boundary(
    *,
    minimum_free_bytes: int = RELEASE_188_MINIMUM_GPU_HEADROOM_BYTES,
) -> torch.device:
    """Choose one clean accelerator for placement, otherwise use CPU.

    This external resource boundary does not select model routes, experts, or
    pages.  It only chooses where the complete model executes.
    """

    if (
        isinstance(minimum_free_bytes, bool)
        or minimum_free_bytes < 1
    ):
        raise ValueError("Release 188 minimum GPU headroom must be positive")
    if not torch.cuda.is_available():
        return torch.device("cpu")
    candidates: list[tuple[int, int]] = []
    for index in range(torch.cuda.device_count()):
        free_bytes, _total_bytes = torch.cuda.mem_get_info(index)
        candidates.append((free_bytes, index))
    if not candidates:
        return torch.device("cpu")
    free_bytes, index = max(candidates)
    if free_bytes < minimum_free_bytes:
        return torch.device("cpu")
    return torch.device("cuda", index)


def _release_dtype_boundary(
    device: torch.device,
    dtype_policy: str,
) -> torch.dtype:
    if dtype_policy != RELEASE_188_DTYPE_POLICY:
        raise RuntimeError("Release 188 dtype policy differs")
    return torch.bfloat16 if device.type == "cuda" else torch.float32


def _science_config(
    cfg: ResynthesisConfig,
) -> ResynthesisScienceLayerConfig:
    return ResynthesisScienceLayerConfig(
        hidden_size=cfg.hidden_size,
        knowledge_transfer_dim=cfg.knowledge_transfer_dim,
        num_layers=cfg.num_layers,
        num_experts=cfg.num_experts,
        expert_hidden_size=cfg.expert_hidden_size,
        memory_slots=cfg.memory_slots,
        attention_heads=cfg.attention_heads,
        mhc_heads=cfg.mhc_heads,
        recursive_steps=cfg.recursive_steps,
        residual_init=cfg.residual_init,
        logit_residual_init=cfg.logit_residual_init,
        kl_anchor_weight=cfg.kl_anchor_weight,
        kl_anchor_warmup_steps=cfg.kl_anchor_warmup_steps,
        glyph_input_dim=cfg.glyph_input_dim,
    )


def _load_safetensors_boundary(
    path: Path,
    *,
    role: str,
    schema: str,
) -> dict[str, torch.Tensor]:
    """Read one public safetensors role at the external load boundary."""

    if not path.is_file():
        raise FileNotFoundError(f"Release 188 safetensors is absent: {path}")
    with safe_open(  # type: ignore[no-untyped-call]
        str(path),
        framework="pt",
        device="cpu",
    ) as handle:
        metadata = handle.metadata()
        if (
            metadata is None
            or metadata.get("release") != str(RELEASE_188_GENERATION)
            or metadata.get("role") != role
            or metadata.get("schema") != schema
            or metadata.get("optimizer_state") != "excluded"
        ):
            raise RuntimeError(
                f"Release 188 {role} safetensors metadata differs"
            )
        state = {
            name: handle.get_tensor(name)
            for name in handle.keys()
        }
    if not state:
        raise RuntimeError(f"Release 188 {role} safetensors is empty")
    return state


def _resident_layer_states_boundary(
    state: Mapping[str, torch.Tensor],
    *,
    layer_count: int,
) -> tuple[dict[str, torch.Tensor], ...]:
    layers: list[dict[str, torch.Tensor]] = [
        {} for _ in range(layer_count)
    ]
    for name, value in state.items():
        layer_stem, separator, local_name = name.partition(".")
        layer_digits = layer_stem.removeprefix("layer_")
        if (
            not separator
            or not layer_stem.startswith("layer_")
            or not layer_digits.isdigit()
            or not local_name
        ):
            raise RuntimeError(
                "Release 188 resident runtime tensor name differs"
            )
        layer_id = int(layer_digits)
        if layer_id < 0 or layer_id >= layer_count:
            raise RuntimeError(
                "Release 188 resident runtime layer is outside the graph"
            )
        layers[layer_id][local_name] = value
    if any(not layer for layer in layers):
        raise RuntimeError(
            "Release 188 resident runtime omits a science layer"
        )
    return tuple(layers)


def _resident_geometry_boundary(
    state: Mapping[str, torch.Tensor],
    *,
    expected_hidden_size: int,
) -> tuple[int, int, torch.Tensor]:
    route_keys_t = state.get("router.page_route_keys")
    hidden_projection_t = state.get("router.hidden_projection.weight")
    action_projection_t = state.get("router.action_projection.weight")
    page_catalog_ids_t = state.get("router.page_catalog_ids_t")
    if (
        not isinstance(route_keys_t, torch.Tensor)
        or route_keys_t.ndim != 2
        or not isinstance(hidden_projection_t, torch.Tensor)
        or hidden_projection_t.ndim != 2
        or hidden_projection_t.shape[1] != expected_hidden_size
        or not isinstance(action_projection_t, torch.Tensor)
        or action_projection_t.ndim != 2
        or not isinstance(page_catalog_ids_t, torch.Tensor)
        or page_catalog_ids_t.ndim != 1
        or page_catalog_ids_t.dtype != torch.long
        or route_keys_t.shape[0] != page_catalog_ids_t.shape[0]
        or route_keys_t.shape[1] != hidden_projection_t.shape[0]
        or route_keys_t.shape[1] != action_projection_t.shape[0]
        or page_catalog_ids_t.numel() < 1
    ):
        raise RuntimeError(
            "Release 188 resident runtime geometry differs"
        )
    return (
        int(action_projection_t.shape[1]),
        int(route_keys_t.shape[1]),
        page_catalog_ids_t.detach().cpu().long(),
    )


def _construct_release_188_model(
    spec: Release188RuntimeSpec,
    *,
    device: torch.device,
    dtype: torch.dtype,
    session_id_t: torch.Tensor,
) -> Release188Model:
    release_root = spec.release_root
    dense_state = _load_safetensors_boundary(
        release_root / _DENSE_FILENAME,
        role="resynthesis-additive-graph",
        schema=COMPOSED_ADDITIVE_LINEAGE_SCHEMA,
    )
    resident_state = _load_safetensors_boundary(
        release_root / _RESIDENT_FILENAME,
        role="paged-resident-runtime",
        schema=NONE_V2_RESIDENT_RUNTIME_SCHEMA,
    )
    page_store = Release188SafetensorsPageStore(spec.model_index_path)
    parent = Release188ParentAdapter(
        release_root,
        device_map={"": device},
        dtype=dtype,
    )
    parent.load_weights()

    cfg = ResynthesisConfig(
        num_layers=spec.science_layers,
        num_experts=spec.science_experts,
    )
    construction_context = (
        torch.device(device)
        if device.type != "cpu"
        else contextlib.nullcontext()
    )
    with construction_context:
        science_stack = build_checkpoint_direct_science_stack_boundary(
            _science_config(cfg),
            dense_state,
            device=device,
        )
        rbo = ResynthesisRBO(
            base=parent,
            science_stack=science_stack,
            cfg=ResynthesisRBOConfig(),
            model_cfg=cfg,
        )
    rbo.ensure_parent_capability_attachment()

    # The direct CUDA builder has already assigned every science tensor in its
    # exact first-forward BF16 representation.  Reuse those assigned tensors
    # for the full-graph strict load rather than widening them back through a
    # redundant host round trip.
    active_dense_state = dict(dense_state)
    if device.type == "cuda":
        active_dense_state.update(
            {
                f"science_stack.{name}": value
                for name, value in science_stack.state_dict().items()
            }
        )
    rbo.load_trainable_state_dict(active_dense_state)
    rbo.to(device=device, dtype=dtype)
    rbo.preserve_trainable_control_precision()

    layer_states = _resident_layer_states_boundary(
        resident_state,
        layer_count=spec.science_layers,
    )
    runtimes: list[NoNEPagedExpertRuntime] = []
    expected_catalogs: list[torch.Tensor] = []
    for layer_id, layer_state in enumerate(layer_states):
        action_size, router_size, page_catalog_ids_t = (
            _resident_geometry_boundary(
                layer_state,
                expected_hidden_size=cfg.hidden_size,
            )
        )
        with construction_context:
            runtime = NoNEPagedExpertRuntime(
                hidden_size=cfg.hidden_size,
                action_size=action_size,
                router_size=router_size,
                page_count=int(page_catalog_ids_t.shape[0]),
                layer_id=layer_id,
                page_catalog_ids_t=page_catalog_ids_t,
            )
        runtime.load_seed_resident_state_boundary(layer_state)
        runtime.to(device=device, dtype=dtype)
        runtime.preserve_training_proof_precision()
        generation_t = runtime.bind_store_boundary(
            cast(NoNEImmutablePageStore, page_store),
            session_id_t,
        )
        if not torch.equal(
            generation_t.detach().cpu().long().reshape(()),
            torch.tensor(RELEASE_188_GENERATION, dtype=torch.long),
        ):
            raise RuntimeError(
                "Release 188 resident runtime generation differs"
            )
        science_stack.attach_paged_expert_runtime(layer_id, runtime)
        runtimes.append(runtime)
        expected_catalogs.append(page_catalog_ids_t)

    # This is the manifest-free equivalent of the internal composition
    # boundary: the router tensors themselves are the layer/page authority.
    rbo._paged_none_store_boundary = page_store
    rbo._paged_none_expected_layer_ids_boundary = tuple(
        range(spec.science_layers)
    )
    rbo._paged_none_expected_layer_catalog_ids_t_boundary = tuple(
        expected_catalogs
    )
    rbo._paged_none_reasoning_source_layer_count = spec.science_layers
    rbo._apply_paged_none_model_wide_residency_budget_boundary(
        tuple(runtimes)
    )
    validated_generation_t = (
        rbo.validate_paged_none_graph_frontier_boundary()
    )
    if not torch.equal(
        validated_generation_t.detach().cpu().long().reshape(()),
        torch.tensor(RELEASE_188_GENERATION, dtype=torch.long),
    ):
        raise RuntimeError("Release 188 page frontier generation differs")

    rbo.requires_grad_(False)
    rbo.eval()
    return Release188Model(
        spec=spec,
        parent=parent,
        rbo=rbo,
        page_store=page_store,
        device=device,
        dtype=dtype,
        session_id_t=session_id_t,
    )


class Release188Model:
    """Loaded Release 188 graph with tensor-native and text generation seams."""

    def __init__(
        self,
        *,
        spec: Release188RuntimeSpec,
        parent: Release188ParentAdapter,
        rbo: ResynthesisRBO,
        page_store: Release188SafetensorsPageStore,
        device: torch.device,
        dtype: torch.dtype,
        session_id_t: torch.Tensor,
    ) -> None:
        self.spec = spec
        self.parent = parent
        self.rbo = rbo
        self.page_store = page_store
        self.device = device
        self.dtype = dtype
        self.session_id_t = (
            session_id_t.detach().cpu().long().reshape(-1).clone()
        )

    @classmethod
    def load(
        cls,
        spec_path: str | Path,
        *,
        device: str | torch.device | None = None,
        session_id_t: torch.Tensor | None = None,
    ) -> Release188Model:
        """Load one inference-only Release 188 runtime."""

        spec = load_release_188_runtime_spec(spec_path)
        resolved_device = (
            select_release_188_device_boundary()
            if device is None
            else torch.device(device)
        )
        dtype = _release_dtype_boundary(
            resolved_device,
            spec.dtype_policy,
        )
        resolved_session_id_t = (
            torch.randint(
                0,
                torch.iinfo(torch.int32).max,
                (4,),
                dtype=torch.long,
            )
            if session_id_t is None
            else session_id_t.detach().cpu().long().reshape(-1).clone()
        )
        if (
            resolved_session_id_t.shape != (4,)
            or torch.unique(resolved_session_id_t).numel() < 2
        ):
            raise ValueError(
                "Release 188 inference session identity is malformed"
            )
        return _construct_release_188_model(
            spec,
            device=resolved_device,
            dtype=dtype,
            session_id_t=resolved_session_id_t,
        )

    def generate_token_ids_boundary(
        self,
        prompt_ids_t: torch.Tensor,
        *,
        emission_observer: (
            Callable[[RBOEmissionTracePacket], object] | None
        ) = None,
    ) -> RBOGenerationResult:
        """Generate through the uncapped model-owned additive stop boundary."""

        if (
            prompt_ids_t.dtype != torch.long
            or prompt_ids_t.ndim != 2
            or prompt_ids_t.shape[0] != 1
            or prompt_ids_t.shape[1] < 1
        ):
            raise ValueError(
                "Release 188 prompt IDs must be one nonempty int64 row"
            )
        if bool(
            torch.logical_or(
                prompt_ids_t.lt(0),
                prompt_ids_t.ge(RELEASE_188_LEXICAL_ROWS),
            ).any()
        ):
            raise ValueError(
                "Release 188 prompt contains a projection-only token ID"
            )
        self.rbo.begin_session()
        return resynthesis_rbo_generate(
            self.rbo,
            prompt_ids_t.to(device=self.device),
            emission_observer=emission_observer,
        )

    def _encode_prompt_boundary(self, prompt: str) -> torch.Tensor:
        if not isinstance(prompt, str) or not prompt.strip():
            raise ValueError("Release 188 prompt must be nonempty text")
        tokenizer = self.parent.tokenizer
        backend = tokenizer.backend
        if backend.chat_template:
            encoded = backend.apply_chat_template(
                [{"role": "user", "content": prompt}],
                tokenize=True,
                add_generation_prompt=True,
                return_tensors="pt",
            )
            if not isinstance(encoded, torch.Tensor):
                raise RuntimeError(
                    "Release 188 chat template returned no tensor"
                )
            return encoded.long()
        encoded_batch = tokenizer(
            prompt,
            return_tensors="pt",
            add_special_tokens=True,
        )
        encoded_ids = encoded_batch.get("input_ids")
        if not isinstance(encoded_ids, torch.Tensor):
            raise RuntimeError("Release 188 tokenizer returned no input IDs")
        return encoded_ids.long()

    def solve(
        self,
        prompt: str,
        *,
        emission_observer: (
            Callable[[RBOEmissionTracePacket], object] | None
        ) = None,
    ) -> str:
        """Encode one prompt, traverse Release 188, and decode its own surface."""

        prompt_ids_t = self._encode_prompt_boundary(prompt)
        generated = self.generate_token_ids_boundary(
            prompt_ids_t,
            emission_observer=emission_observer,
        )
        completed_row = generated.select_batch_row_boundary(
            0,
            prompt_width=prompt_ids_t.shape[1],
        )
        generated_ids_t = completed_row.token_ids[
            0,
            prompt_ids_t.shape[1] :,
        ]
        return self.parent.tokenizer.decode(
            generated_ids_t,
            skip_special_tokens=True,
        )


def load_release_188_model(
    spec_path: str | Path,
    *,
    device: str | torch.device | None = None,
    session_id_t: torch.Tensor | None = None,
) -> Release188Model:
    """Public convenience loader for ``runtime/model.json``."""

    return Release188Model.load(
        spec_path,
        device=device,
        session_id_t=session_id_t,
    )