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
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
07fb183
 
 
 
 
c7b4747
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
"""Dashboard adapter for the isolated non-canonical Playground."""

from __future__ import annotations

import csv
import hashlib
import hmac
import io
import json
import os
import re
import shutil
import subprocess
import threading
import time
import uuid
from collections import deque
from collections.abc import Callable, Mapping
from dataclasses import replace
from datetime import datetime, timezone
from http import HTTPStatus
from pathlib import Path
from typing import cast

from src.dashboard.verification import (
    SCIENTIFIC_PATHS,
    TEST_PATHS,
    current_git_head,
    inspect_source_tree,
)
from src.playground import service
from src.playground.cuda import (
    CompileBundle,
    CudaDriver,
    CudaDriverError,
    CudaRuntimeUnavailable,
    GateLaunchInputs,
    compile_mapping,
    cpu_gate_reference,
    execute_gate_bundle,
    gate_execution_parity_summary,
    preflight_bundle,
    validate_cuda_block_size,
)
from src.playground.cuda.builder_parity import run_builder_parity
from src.playground.cuda.recurrent import (
    cpu_recurrent_reference,
    execute_recurrent,
    recurrent_fixture,
    recurrent_parity,
)
from src.playground.cuda.synapses import SynapseConfig
from src.playground.integration import integration_catalog, transfer_element
from src.playground.models import PlaygroundConfig
from src.playground.night_run import NightRunManager
from src.playground.pan import PANEmbodiedSandboxSession, PANSessionDaemon
from src.playground.pan.cue_controls import run_cue_controls
from src.playground.pan.transfer import run_synaptic_transfer

_MAX_CONCURRENT_RUNS = 2
_RUNS_PER_MINUTE = 20
_RUN_WINDOW_SECONDS = 60.0
_CUDA_DIAGNOSTIC_ID = re.compile(r"[0-9a-f]{32}")
_CUDA_DIAGNOSTIC_MAX_BYTES = 8 * 1024 * 1024
_REPO_ROOT = Path(__file__).resolve().parents[2]
_CUDA_DIAGNOSTIC_URL_PREFIX = "/api/playground/cuda/diagnostics/"

_RUN_SEMAPHORE = threading.BoundedSemaphore(_MAX_CONCURRENT_RUNS)
_RATE_LOCK = threading.Lock()
_RECENT_RUNS: deque[float] = deque()


def _canonical_artifact_bytes(artifact: Mapping[str, object]) -> bytes:
    return json.dumps(
        artifact, sort_keys=True, ensure_ascii=True, separators=(",", ":")
    ).encode("utf-8")


def _write_cuda_diagnostic_artifact(
    repo_root: Path, artifact: Mapping[str, object]
) -> tuple[Path, str]:
    artifact_id = artifact.get("artifact_id")
    if not isinstance(artifact_id, str) or not _CUDA_DIAGNOSTIC_ID.fullmatch(
        artifact_id
    ):
        raise ValueError("CUDA diagnostic artifact ID is invalid")

    directory = repo_root / "artifacts" / "cuda_diagnostics"
    directory.mkdir(mode=0o700, parents=True, exist_ok=True)
    if os.name != "nt":
        directory.chmod(0o700)
    record = dict(artifact)
    artifact_digest = hashlib.sha256(_canonical_artifact_bytes(record)).hexdigest()
    record["artifact_sha256"] = artifact_digest
    serialized = json.dumps(record, sort_keys=True, indent=2, ensure_ascii=True) + "\n"
    encoded = serialized.encode("utf-8")
    if len(encoded) > _CUDA_DIAGNOSTIC_MAX_BYTES:
        raise ValueError("CUDA diagnostic artifact exceeds the 8 MiB limit")

    destination = directory / f"{artifact_id}.json"
    temporary = directory / f".{artifact_id}.{uuid.uuid4().hex}.tmp"
    try:
        temporary.write_bytes(encoded)
        if os.name != "nt":
            temporary.chmod(0o600)
        os.replace(temporary, destination)
    finally:
        temporary.unlink(missing_ok=True)
    return destination, artifact_digest


def _load_cuda_diagnostic_artifact(
    repo_root: Path, artifact_id: str
) -> dict[str, object]:
    if not _CUDA_DIAGNOSTIC_ID.fullmatch(artifact_id):
        raise ValueError("CUDA diagnostic artifact ID is invalid")
    path = repo_root / "artifacts" / "cuda_diagnostics" / f"{artifact_id}.json"
    value = json.loads(path.read_text(encoding="utf-8"))
    if not isinstance(value, dict):
        raise ValueError("CUDA diagnostic artifact is malformed")
    record = dict(cast(dict[str, object], value))
    if record.get("artifact_id") != artifact_id:
        raise ValueError("CUDA diagnostic artifact is malformed")
    recorded_digest = record.pop("artifact_sha256", None)
    actual_digest = hashlib.sha256(_canonical_artifact_bytes(record)).hexdigest()
    if not isinstance(recorded_digest, str) or not hmac.compare_digest(
        recorded_digest, actual_digest
    ):
        raise ValueError("CUDA diagnostic artifact digest mismatch")
    record["artifact_sha256"] = recorded_digest
    return record


def _cuda_hardware_identity(device_ordinal: int) -> dict[str, object]:
    executable = shutil.which("nvidia-smi")
    if executable is None:
        return {
            "status": "unavailable",
            "source": "nvidia-smi",
            "device_ordinal": device_ordinal,
            "reason": "nvidia-smi_not_found",
        }
    try:
        completed = subprocess.run(
            [
                executable,
                "--query-gpu=index,name,uuid,pci.bus_id,driver_version",
                "--format=csv,noheader,nounits",
            ],
            capture_output=True,
            check=False,
            text=True,
            timeout=5,
        )
    except (OSError, subprocess.SubprocessError) as exc:
        return {
            "status": "unavailable",
            "source": "nvidia-smi",
            "device_ordinal": device_ordinal,
            "reason": type(exc).__name__,
        }
    if completed.returncode != 0:
        return {
            "status": "unavailable",
            "source": "nvidia-smi",
            "device_ordinal": device_ordinal,
            "reason": "query_failed",
        }

    rows = [
        [value.strip() for value in row]
        for row in csv.reader(io.StringIO(completed.stdout), skipinitialspace=True)
        if row
    ]
    selector = os.environ.get("CUDA_VISIBLE_DEVICES")
    visible = selector.split(",") if selector is not None else None
    if visible is not None:
        if device_ordinal >= len(visible):
            return {
                "status": "unavailable",
                "source": "nvidia-smi",
                "device_ordinal": device_ordinal,
                "reason": "device_not_visible",
            }
        selected = visible[device_ordinal].strip()
        if selected.isdecimal():
            match = next((row for row in rows if row[0] == selected), None)
        elif selected.startswith("GPU-"):
            match = next(
                (row for row in rows if len(row) > 2 and row[2] == selected), None
            )
        else:
            match = None
    else:
        match = next((row for row in rows if row[0] == str(device_ordinal)), None)

    if match is None or len(match) < 5 or match[2] in {"", "N/A"}:
        return {
            "status": "unavailable",
            "source": "nvidia-smi",
            "device_ordinal": device_ordinal,
            "reason": "device_identity_unresolved",
        }
    return {
        "status": "captured",
        "source": "nvidia-smi",
        "device_ordinal": device_ordinal,
        "physical_index": match[0],
        "model": match[1],
        "device_uuid_sha256": hashlib.sha256(match[2].encode("utf-8")).hexdigest(),
        "pci_bus_id": match[3],
        "driver_version": match[4],
    }


def _working_tree_provenance(repo_root: Path) -> dict[str, object]:
    try:
        inspection = inspect_source_tree(repo_root)
    except (OSError, ValueError, subprocess.SubprocessError) as exc:
        return {
            "git_commit": current_git_head(repo_root),
            "working_tree_digest": None,
            "working_tree_digest_status": type(exc).__name__,
            "working_tree_digest_scope": [*SCIENTIFIC_PATHS, *TEST_PATHS],
        }
    dirty = bool(
        inspection.dirty_relevant_paths
        or inspection.untracked_relevant_paths
        or inspection.missing_relevant_paths
    )
    return {
        "git_commit": current_git_head(repo_root),
        "working_tree_digest": inspection.digest,
        "working_tree_digest_status": (
            "captured" if inspection.digest else "unavailable"
        ),
        "working_tree_state": (
            "unknown"
            if not inspection.git_available
            else "modified" if dirty else "clean"
        ),
        "working_tree_digest_scope": [*SCIENTIFIC_PATHS, *TEST_PATHS],
        "dirty_relevant_paths": list(inspection.dirty_relevant_paths),
        "untracked_relevant_paths": list(inspection.untracked_relevant_paths),
        "missing_relevant_paths": list(inspection.missing_relevant_paths),
    }


def _diagnostic_outcome(result: Mapping[str, object]) -> str:
    passed = result.get("passed")
    if isinstance(passed, bool):
        return "passed" if passed else "failed"
    parity = result.get("parity")
    if isinstance(parity, Mapping):
        parity_passed = cast(Mapping[str, object], parity).get("passed")
        if isinstance(parity_passed, bool):
            return "passed" if parity_passed else "failed"
    actions_exact = result.get("actions_exact")
    if isinstance(actions_exact, bool):
        return "passed" if actions_exact else "failed"
    return "not_evaluated"


def _diagnostic_http_status(exc: Exception) -> int:
    if isinstance(exc, (ValueError, TypeError, json.JSONDecodeError)):
        return HTTPStatus.BAD_REQUEST
    if isinstance(exc, FileNotFoundError):
        return HTTPStatus.NOT_FOUND
    if isinstance(exc, TimeoutError):
        return HTTPStatus.GATEWAY_TIMEOUT
    if isinstance(exc, RuntimeError):
        return HTTPStatus.SERVICE_UNAVAILABLE
    return HTTPStatus.INTERNAL_SERVER_ERROR


def _run_cuda_diagnostic(
    route: str,
    payload: Mapping[str, object],
    operation: Callable[[], dict[str, object]],
    *,
    repo_root: Path | None = None,
) -> dict[str, object]:
    root = repo_root or _REPO_ROOT
    started = datetime.now(timezone.utc)
    artifact_id = uuid.uuid4().hex
    ordinal = payload.get("device_ordinal", 0)
    device_ordinal = (
        ordinal
        if isinstance(ordinal, int) and not isinstance(ordinal, bool) and ordinal >= 0
        else 0
    )
    provenance = _working_tree_provenance(root)
    hardware = _cuda_hardware_identity(device_ordinal)
    try:
        result = operation()
    except Exception as exc:
        completed = datetime.now(timezone.utc)
        record: dict[str, object] = {
            "schema_version": "mhrn.cuda-diagnostic.v1",
            "artifact_id": artifact_id,
            "route": route,
            "execution_status": "failed",
            "outcome": "error",
            "started_at": started.isoformat(),
            "completed_at": completed.isoformat(),
            "duration_seconds": (completed - started).total_seconds(),
            "request_sha256": hashlib.sha256(
                _canonical_artifact_bytes(payload)
            ).hexdigest(),
            "provenance": provenance,
            "hardware_identity": hardware,
            "error_type": type(exc).__name__,
            "error": str(exc)[:1000],
            "scientific_evidence": False,
        }
        _attach_cuda_diagnostic_receipt(exc, root, record)
        raise

    completed = datetime.now(timezone.utc)
    record = {
        "schema_version": "mhrn.cuda-diagnostic.v1",
        "artifact_id": artifact_id,
        "route": route,
        "execution_status": "completed",
        "outcome": _diagnostic_outcome(result),
        "started_at": started.isoformat(),
        "completed_at": completed.isoformat(),
        "duration_seconds": (completed - started).total_seconds(),
        "request_sha256": hashlib.sha256(
            _canonical_artifact_bytes(payload)
        ).hexdigest(),
        "provenance": provenance,
        "hardware_identity": hardware,
        "result": result,
        "scientific_evidence": False,
    }
    receipt = _attach_cuda_diagnostic_receipt(None, root, record)
    response = dict(result)
    response["run_evidence"] = receipt
    return response


def _attach_cuda_diagnostic_receipt(
    error: Exception | None,
    repo_root: Path,
    record: dict[str, object],
) -> dict[str, object]:
    artifact_id = cast(str, record["artifact_id"])
    receipt: dict[str, object] = {
        "artifact_id": artifact_id,
        "artifact_url": f"/api/playground/cuda/diagnostics/{artifact_id}",
        "execution_status": record["execution_status"],
        "outcome": record["outcome"],
        "started_at": record["started_at"],
        "completed_at": record["completed_at"],
        "duration_seconds": record["duration_seconds"],
        "provenance": record["provenance"],
        "hardware_identity": record["hardware_identity"],
    }
    record["artifact_url"] = receipt["artifact_url"]
    try:
        _, artifact_digest = _write_cuda_diagnostic_artifact(repo_root, record)
    except (OSError, TypeError, ValueError) as exc:
        receipt["persistence_status"] = "failed"
        receipt["persistence_error"] = type(exc).__name__
        receipt["artifact_url"] = None
    else:
        receipt["persistence_status"] = "persisted"
        receipt["artifact_sha256"] = artifact_digest
    if error is not None:
        setattr(error, "run_evidence", receipt)
        setattr(error, "run_http_status", _diagnostic_http_status(error))
    return receipt


def _bounded_cuda_diagnostic(
    route: str,
    payload: Mapping[str, object],
    operation: Callable[[], dict[str, object]],
) -> dict[str, object]:
    return _bounded_operation(lambda: _run_cuda_diagnostic(route, payload, operation))


def _payload_int(
    payload: Mapping[str, object],
    name: str,
    default: int,
) -> int:
    value = payload.get(name, default)
    if isinstance(value, bool) or not isinstance(value, (int, float)):
        raise ValueError(f"{name} must be numeric")
    converted = int(value)
    if float(value) != float(converted):
        raise ValueError(f"{name} must be an integer")
    return converted


def _payload_float(
    payload: Mapping[str, object],
    name: str,
    default: float,
) -> float:
    value = payload.get(name, default)
    if isinstance(value, bool) or not isinstance(value, (int, float)):
        raise ValueError(f"{name} must be numeric")
    return float(value)


def _payload_text(
    payload: Mapping[str, object],
    name: str,
    default: str,
) -> str:
    value = payload.get(name, default)
    if not isinstance(value, str):
        raise ValueError(f"{name} must be a string")
    return value


def _numeric_list(value: object, name: str) -> list[float]:
    if not isinstance(value, list):
        raise ValueError(f"{name} must be a list")
    items = cast(list[object], value)
    result: list[float] = []
    for item in items:
        if isinstance(item, bool) or not isinstance(item, (int, float)):
            raise ValueError(f"{name} entries must be numeric")
        result.append(float(item))
    return result


_LIVE_DAEMON = PANSessionDaemon()
_LIVE_SANDBOXES: dict[str, PANEmbodiedSandboxSession] = {}
_LIVE_LOCK = threading.RLock()
_NIGHT_RUN = NightRunManager()


class PlaygroundRateLimitError(RuntimeError):
    """Raised when the bounded Playground API rate is exceeded."""


class PlaygroundBusyError(RuntimeError):
    """Raised when all bounded Playground worker slots are occupied."""


def _reserve_rate_slot() -> None:
    now = time.monotonic()
    with _RATE_LOCK:
        cutoff = now - _RUN_WINDOW_SECONDS
        while _RECENT_RUNS and _RECENT_RUNS[0] < cutoff:
            _RECENT_RUNS.popleft()
        if len(_RECENT_RUNS) >= _RUNS_PER_MINUTE:
            raise PlaygroundRateLimitError(
                "Playground run rate exceeded; retry after the current minute window."
            )
        _RECENT_RUNS.append(now)


def _bounded_operation(
    operation: Callable[[], dict[str, object]],
) -> dict[str, object]:
    _reserve_rate_slot()
    if not _RUN_SEMAPHORE.acquire(blocking=False):
        raise PlaygroundBusyError(
            "Playground is busy; at most two simulation requests run concurrently."
        )
    try:
        return operation()
    finally:
        _RUN_SEMAPHORE.release()


def _bounded_run(path: str, payload: Mapping[str, object]) -> dict[str, object]:
    def operation() -> dict[str, object]:
        if path == "/api/playground/run":
            return service.run(payload)
        if path == "/api/playground/robustness":
            return service.robustness(payload)
        raise ValueError(f"unknown Playground POST route: {path}")

    return _bounded_operation(operation)


def _live_parts(path: str) -> list[str]:
    prefix = "/api/playground/live/"
    if not path.startswith(prefix):
        return []
    return [part for part in path[len(prefix) :].split("/") if part]


def _cuda_runtime_status() -> dict[str, object]:
    ptxas_path = shutil.which("ptxas")
    driver_available = False
    driver_error: str | None = None
    try:
        driver = CudaDriver()
        driver.initialize()
        driver_available = True
    except (CudaRuntimeUnavailable, CudaDriverError, OSError) as exc:
        driver_error = str(exc)

    return {
        "classification": "PLAYGROUND_CUDA_DIAGNOSTICS_STATUS",
        "scientific_evidence": False,
        "canonical_cuda_backend": False,
        "provenance": {
            "git_commit": current_git_head(Path(__file__).resolve().parents[2]),
            "source": "runtime_repository_head",
            "working_tree_state": "not_captured",
        },
        "target_default": "sm_86",
        "ptxas_available": ptxas_path is not None,
        "ptxas_path": ptxas_path,
        "cuda_driver_available": driver_available,
        "cuda_driver_error": driver_error,
        "stages": {
            "CUDA-1.0": "PTXAS_LOAD_PREFLIGHT_IMPLEMENTED",
            "CUDA-1.1": "DETERMINISM_FREEZE_PARITY_CONTRACT_IMPLEMENTED",
            "CUDA-1.2": "LAUNCH_ABI_BUFFER_VALIDATION_IMPLEMENTED",
            "CUDA-1.3": "CPU_GATE_REFERENCE_D2_IMPLEMENTED_HARDWARE_VERIFICATION_REQUIRED",
            "CUDA-1.4": "BOUNDED_RECURRENT_FP64_REFERENCE_IMPLEMENTED",
            "CUDA-1.5": "BOUNDED_FROZEN_REWARD_PLASTICITY_REFERENCE_IMPLEMENTED",
            "CUDA-1.6": "HYBRID_BUILDER_MEMBRANE_AND_OPTIONAL_PAN_STATE_GPU_BODY_CPU_IMPLEMENTED",
        },
        "application_cpu": {
            "sandbox_physics": True,
            "sensorics": True,
            "actuation": True,
            "posture_analysis": True,
            "reward_triggers": True,
            "closed_loop": True,
        },
        "gpu_porting": {
            "gate_single_tick": True,
            "membrane_state_100_ticks": True,
            "adaptation_state": True,
            "pan_health_energy_hyperstate": "OPTIONAL_CUDA_PAN_BUILDER",
            "pan_apoptosis_decision": "OPTIONAL_CUDA_PAN_BUILDER",
            "pan_feedback_projection": "OPTIONAL_CUDA_PAN_BUILDER",
            "refractory_state": False,
            "synapses": "BUILDER_GPU_EMISSION_PLASTICITY_REWARD_RESIDENT_QUEUE_HOST_TRACE",
            "builder_rng_semantics": "HOST_TRAVERSAL_STREAM_PRESERVED",
            "builder_inhibitory_emission": True,
            "delays": True,
            "builder_delay_queue": "DEVICE_RESIDENT_RING_65",
            "builder_neuron_traces": "DEVICE_RESIDENT_TWO_PHASE",
            "builder_pan_population": "CUDA_ORDERED_AXIS_REDUCTION",
            "builder_feedback_history": "DEVICE_RING_66_SOURCE_SELECTION",
            "plasticity": "BUILDER_PAIR_TRIPLET_ELIGIBILITY_MODULATION_AND_BOUNDED_REFERENCE",
            "sandbox_physics": False,
        },
        "verification": {
            "cpu_gate_reference": True,
            "single_tick_d2_endpoint": True,
            "rng_action_path_endpoint": True,
            "raw_rng_value_parity": True,
            "memory_leak_instrumentation": True,
        },
    }


def _diagnostic_inputs(
    bundle: CompileBundle,
    *,
    n_neurons: int,
    seed: int,
    epsilon: float,
) -> GateLaunchInputs:
    manifest = bundle.manifest
    raw_abi = manifest.get("kernel_abi")
    if not isinstance(raw_abi, Mapping):
        raise ValueError("compile bundle is missing kernel_abi")
    abi = cast(Mapping[str, object], raw_abi)
    input_channels = _payload_int(abi, "input_channels", 8)
    action_count = _payload_int(abi, "action_space_size", 4)
    pan_dimensions = _payload_int(abi, "pan_dimensions", 5)
    full_mask = (1 << input_channels) - 1 if input_channels < 64 else 0xFFFFFFFFFFFFFFFF
    logits: list[float] = []
    for neuron in range(n_neurons):
        preferred = neuron % action_count
        for action in range(action_count):
            logits.append(1.0 if action == preferred else 0.05 * (action + 1))
    return GateLaunchInputs.from_sequences(
        input_current=[1.0 + 0.125 * (index % 7) for index in range(n_neurons)],
        channel_masks=[full_mask] * n_neurons,
        amplitudes=[1.0 + 0.05 * channel for channel in range(input_channels)],
        reward_ring=[0.5, -0.25, 0.75, 0.0],
        action_map=[
            0.1 * (action + 1) * (channel + 1)
            for action in range(action_count)
            for channel in range(input_channels)
        ],
        feedback_matrix=[
            0.01 * (1 + ((neuron + dimension) % 5))
            for neuron in range(n_neurons)
            for dimension in range(pan_dimensions)
        ],
        population=[0.1 * (dimension + 1) for dimension in range(pan_dimensions)],
        logits=logits,
        tick=3,
        target_index=min(2, action_count - 1),
        previous_action=min(1, action_count - 1),
        seed=seed,
        epsilon=epsilon,
    )


def _cuda_config_payload(payload: Mapping[str, object]) -> dict[str, object]:
    excluded = {
        "target_sm",
        "ptx_version",
        "n_neurons",
        "block_size",
        "device_ordinal",
        "reference_commit",
        "rng_samples",
    }
    return {key: value for key, value in payload.items() if key not in excluded}


def _cuda_preflight(payload: Mapping[str, object]) -> dict[str, object]:
    target_sm = _payload_text(payload, "target_sm", "sm_86")
    ptx_version = _payload_text(payload, "ptx_version", "7.1")
    n_neurons = _payload_int(payload, "n_neurons", 256)
    block_size = validate_cuda_block_size(_payload_int(payload, "block_size", 64))
    device_ordinal = _payload_int(payload, "device_ordinal", 0)
    if not 1 <= n_neurons <= 12_288:
        raise ValueError("n_neurons must be in [1, 12288]")
    bundle = compile_mapping(
        _cuda_config_payload(payload),
        target_sm=target_sm,
        ptx_version=ptx_version,
    )
    return preflight_bundle(
        bundle,
        n_neurons=n_neurons,
        block_size=block_size,
        device_ordinal=device_ordinal,
    )


def _cuda_smoke(payload: Mapping[str, object]) -> dict[str, object]:
    target_sm = _payload_text(payload, "target_sm", "sm_86")
    ptx_version = _payload_text(payload, "ptx_version", "7.1")
    n_neurons = _payload_int(payload, "n_neurons", 64)
    block_size = validate_cuda_block_size(_payload_int(payload, "block_size", 64))
    device_ordinal = _payload_int(payload, "device_ordinal", 0)
    reference_commit = _payload_text(payload, "reference_commit", "")
    if not 1 <= n_neurons <= 4096:
        raise ValueError("n_neurons must be in [1, 4096] for hardware smoke")
    bundle = compile_mapping(
        _cuda_config_payload(payload),
        target_sm=target_sm,
        ptx_version=ptx_version,
    )
    inputs = _diagnostic_inputs(
        bundle,
        n_neurons=n_neurons,
        seed=12345,
        epsilon=0.0,
    )
    reference = cpu_gate_reference(bundle, inputs)
    first = execute_gate_bundle(
        bundle,
        inputs,
        block_size=block_size,
        device_ordinal=device_ordinal,
    )
    second = execute_gate_bundle(
        bundle,
        inputs,
        block_size=block_size,
        device_ordinal=device_ordinal,
    )
    first_parity = gate_execution_parity_summary(
        reference,
        first,
        tolerance=1.0e-5,
        reference_commit=reference_commit,
    )
    second_parity = gate_execution_parity_summary(
        reference,
        second,
        tolerance=1.0e-5,
        reference_commit=reference_commit,
    )
    deterministic = first.get("outputs") == second.get("outputs")
    return {
        "classification": "PLAYGROUND_CUDA1_3_HARDWARE_SMOKE",
        "scientific_evidence": False,
        "canonical_cuda_backend": False,
        "reference": reference,
        "first": first,
        "second": second,
        "parity": first_parity,
        "second_parity": second_parity,
        "gpu_repeat_exact": deterministic,
        "passed": bool(first_parity["passed"])
        and bool(second_parity["passed"])
        and deterministic,
        "cleanup_contract": {
            "device_allocations_released_in_finally": True,
            "driver_module_unloaded": True,
            "memory_leak_instrumented": True,
            "first_memory": first.get("memory"),
            "second_memory": second.get("memory"),
        },
    }


def _cuda_rng_parity(payload: Mapping[str, object]) -> dict[str, object]:
    target_sm = _payload_text(payload, "target_sm", "sm_86")
    ptx_version = _payload_text(payload, "ptx_version", "7.1")
    samples = _payload_int(payload, "rng_samples", 1000)
    block_size = validate_cuda_block_size(_payload_int(payload, "block_size", 64))
    device_ordinal = _payload_int(payload, "device_ordinal", 0)
    if not 1 <= samples <= 4096:
        raise ValueError("rng_samples must be in [1, 4096]")
    bundle = compile_mapping(
        _cuda_config_payload(payload),
        target_sm=target_sm,
        ptx_version=ptx_version,
    )
    inputs = _diagnostic_inputs(
        bundle,
        n_neurons=samples,
        seed=12345,
        epsilon=1.0,
    )
    reference = cpu_gate_reference(bundle, inputs)
    candidate = execute_gate_bundle(
        bundle,
        inputs,
        block_size=block_size,
        device_ordinal=device_ordinal,
    )
    reference_outputs = cast(Mapping[str, object], reference["outputs"])
    candidate_outputs = cast(Mapping[str, object], candidate["outputs"])
    reference_actions = cast(list[int], reference_outputs["action"])
    candidate_actions = cast(list[int], candidate_outputs["action"])
    actions_exact = reference_actions == candidate_actions
    return {
        "classification": "PLAYGROUND_CUDA_RNG_ACTION_PATH_PARITY",
        "scientific_evidence": False,
        "samples": samples,
        "seed": 12345,
        "tick": inputs.tick,
        "epsilon": 1.0,
        "actions_exact": actions_exact,
        "passed": actions_exact,
        "first_reference_actions": reference_actions[:20],
        "first_cuda_actions": candidate_actions[:20],
        "scope": "HASH_EPSILON_GREEDY_ACTION_PATH_UINT24_TO_F32_HALF_OPEN",
    }


def _cuda_recurrent_parity(payload: Mapping[str, object]) -> dict[str, object]:
    block_size = validate_cuda_block_size(_payload_int(payload, "block_size", 64))
    inputs = recurrent_fixture(
        n_neurons=_payload_int(payload, "n_neurons", 129),
        ticks=_payload_int(payload, "ticks", 100),
        model=_payload_text(payload, "model", "pan_adex_5d"),
    )
    plasticity = payload.get("plasticity", False)
    if not isinstance(plasticity, bool):
        raise ValueError("plasticity must be boolean")
    if plasticity:
        inputs = replace(
            inputs,
            synapses=SynapseConfig(),
            rewards=tuple(
                1.0 if tick % 32 == 0 else 0.0 for tick in range(inputs.ticks)
            ),
        )
    reference = cpu_recurrent_reference(inputs)
    candidate = execute_recurrent(
        inputs,
        block_size=block_size,
        target_sm=_payload_text(payload, "target_sm", "sm_86"),
    )
    return {
        "classification": (
            "PLAYGROUND_CUDA15_PLASTICITY_PARITY"
            if plasticity
            else "PLAYGROUND_CUDA14_RECURRENT_PARITY"
        ),
        "scientific_evidence": False,
        "model": inputs.model,
        "ticks": inputs.ticks,
        "state_dtype": candidate["state_dtype"],
        "preflight": candidate["preflight"],
        "parity": recurrent_parity(
            reference, cast(dict[str, object], candidate["outputs"])
        ),
        "scope": candidate["comparison_scope"],
        "full_pan_backend": False,
    }


def get_playground(path: str) -> dict[str, object] | None:
    if path == "/api/playground/catalog":
        return service.catalog()
    if path == "/api/playground/integration":
        return integration_catalog()
    if path == "/api/playground/sessions":
        return service.sessions()
    if path.startswith("/api/playground/sessions/"):
        session_id = path[len("/api/playground/sessions/") :]
        return service.replay(session_id)
    if path.startswith(_CUDA_DIAGNOSTIC_URL_PREFIX):
        artifact_id = path[len(_CUDA_DIAGNOSTIC_URL_PREFIX) :]
        if not _CUDA_DIAGNOSTIC_ID.fullmatch(artifact_id):
            return None
        try:
            return _load_cuda_diagnostic_artifact(_REPO_ROOT, artifact_id)
        except FileNotFoundError:
            return None
    if path == "/api/playground/night":
        return _NIGHT_RUN.status()
    if path == "/api/playground/cuda/status":
        return _cuda_runtime_status()
    if path == "/api/playground/live":
        return {
            "class": "PLAYGROUND_LIVE_SESSIONS",
            "scientific_evidence": False,
            "sessions": _LIVE_DAEMON.list(),
        }
    parts = _live_parts(path)
    if len(parts) == 1:
        return {"session_id": parts[0], **_LIVE_DAEMON.get(parts[0]).snapshot()}
    return None


def post_playground(
    path: str, payload: Mapping[str, object]
) -> dict[str, object] | None:
    if path in {"/api/playground/run", "/api/playground/robustness"}:
        return _bounded_run(path, payload)

    if path == "/api/playground/integration/transfer":
        return transfer_element(payload)

    if path == "/api/playground/research/synaptic-transfer":
        return _bounded_cuda_diagnostic(
            path, payload, lambda: run_synaptic_transfer(payload)
        )

    if path == "/api/playground/research/cue-controls":
        return _bounded_cuda_diagnostic(
            path, payload, lambda: run_cue_controls(payload)
        )

    if path == "/api/playground/determinism":
        return service.determinism(payload)

    if path == "/api/playground/cuda/compile":
        target_sm = _payload_text(payload, "target_sm", "sm_86")
        ptx_version = _payload_text(payload, "ptx_version", "7.1")
        config_payload = dict(payload)
        config_payload.pop("target_sm", None)
        config_payload.pop("ptx_version", None)
        return compile_mapping(
            config_payload,
            target_sm=target_sm,
            ptx_version=ptx_version,
        ).to_mapping()

    if path == "/api/playground/cuda/preflight":
        return _bounded_cuda_diagnostic(path, payload, lambda: _cuda_preflight(payload))

    if path == "/api/playground/cuda/smoke":
        return _bounded_cuda_diagnostic(path, payload, lambda: _cuda_smoke(payload))

    if path == "/api/playground/cuda/rng-parity":
        return _bounded_cuda_diagnostic(
            path, payload, lambda: _cuda_rng_parity(payload)
        )

    if path == "/api/playground/cuda/builder-parity":
        return _bounded_cuda_diagnostic(
            path, payload, lambda: run_builder_parity(payload)
        )

    if path == "/api/playground/cuda/recurrent-parity":
        return _bounded_cuda_diagnostic(
            path, payload, lambda: _cuda_recurrent_parity(payload)
        )

    if path == "/api/playground/night/start":
        return _NIGHT_RUN.start(
            hours=_payload_float(payload, "hours", 8.0),
            max_episodes=_payload_int(payload, "max_episodes", 10_000),
            checkpoint_seconds=_payload_float(payload, "checkpoint_seconds", 600.0),
            seed=_payload_int(payload, "seed", 12345),
        )
    if path == "/api/playground/night/stop":
        return _NIGHT_RUN.stop()

    if path == "/api/playground/live/stop-all":
        with _LIVE_LOCK:
            _LIVE_SANDBOXES.clear()
        return _LIVE_DAEMON.stop_all()

    if path == "/api/playground/live/create":
        config_payload = dict(payload)
        config_payload.setdefault("neuron_model", "pan_adex_5d")
        config_payload.setdefault("pan_enabled", True)
        config_payload.setdefault("thalamic_relay_threshold", 0.0)
        config_payload.setdefault("pan_bias_current", 10.0)
        config_payload.setdefault("behavior_target_mode", "cycle")
        config = PlaygroundConfig.from_mapping(config_payload)
        session_id = _LIVE_DAEMON.create(config)
        with _LIVE_LOCK:
            _LIVE_SANDBOXES[session_id] = PANEmbodiedSandboxSession(
                _LIVE_DAEMON.get(session_id)
            )
        return {
            "class": "PLAYGROUND_LIVE_SESSION",
            "scientific_evidence": False,
            "session_id": session_id,
            "state": _LIVE_DAEMON.get(session_id).snapshot(),
        }

    parts = _live_parts(path)
    if len(parts) >= 2:
        session_id, action = parts[0], parts[1]
        session = _LIVE_DAEMON.get(session_id)
        if action == "step":
            ticks = _payload_int(payload, "ticks", 32)
            include_topology = payload.get("include_topology", True) is not False
            return {
                "session_id": session_id,
                **session.step(ticks, include_topology=include_topology),
            }
        if action == "input":
            values = _numeric_list(payload.get("values", []), "values")
            duration = _payload_int(payload, "duration_ticks", 16)
            gain = _payload_float(payload, "gain", 25.0)
            session.inject_vector(values, duration_ticks=duration, gain=gain)
            return {"session_id": session_id, "accepted": True}
        if action == "strategy":
            context = _payload_text(payload, "context", "default")
            options = _payload_int(payload, "action_count", 4)
            chosen = session.choose_strategy(context, options)
            return {
                "session_id": session_id,
                "classification": "PLAYGROUND_META_STRATEGY",
                "scientific_evidence": False,
                "context": context,
                "action": chosen,
                "action_count": options,
            }
        if action == "reward":
            context = _payload_text(payload, "context", "default")
            chosen = _payload_int(payload, "action", 0)
            action_count = _payload_int(payload, "action_count", 4)
            reward = _payload_float(payload, "reward", 0.0)
            return {
                "session_id": session_id,
                **session.apply_strategy_reward(
                    context=context,
                    action=chosen,
                    reward=reward,
                    action_count=action_count,
                ),
            }
        if action == "sandbox":
            with _LIVE_LOCK:
                sandbox = _LIVE_SANDBOXES.get(session_id)
                if sandbox is None:
                    sandbox = PANEmbodiedSandboxSession(session)
                    _LIVE_SANDBOXES[session_id] = sandbox
            ticks = _payload_int(payload, "ticks", 1)
            return {"session_id": session_id, **sandbox.step(ticks)}
        if action == "stop":
            with _LIVE_LOCK:
                _LIVE_SANDBOXES.pop(session_id, None)
            return {"session_id": session_id, **_LIVE_DAEMON.stop(session_id)}
    return None