File size: 27,848 Bytes
784f572
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import re
import json
import os
from pathlib import Path
import subprocess

import pytest
import yaml

from dlb.registry import load_registry


ENVIRONMENT_DIR = Path("envs")
EXPECTED_ENVIRONMENTS = {
    "dlb-flm",
    "dlb-langflow",
    "dlb-duo",
    "dlb-mdlm",
    "dlb-candi",
    "dlb-rdlm",
    "dlb-sdtt",
    "dlb-di4c",
    "dlb-eval",
}


@pytest.fixture
def registry():
    return load_registry(Path("configs/experiments.yaml"))


def load_environments():
    return {
        path.stem: yaml.safe_load(path.read_text())
        for path in sorted(ENVIRONMENT_DIR.glob("*.yml"))
        if not path.name.startswith(".")
    }


def pip_dependencies(environment):
    return next(
        dependency["pip"]
        for dependency in environment["dependencies"]
        if isinstance(dependency, dict) and "pip" in dependency
    )


def conda_dependencies(environment):
    return [
        dependency
        for dependency in environment["dependencies"]
        if isinstance(dependency, str)
    ]


def pinned_pip_dependencies(environment):
    return {
        package.split("==", 1)[0]: package.split("==", 1)[1]
        for package in pip_dependencies(environment)
        if "==" in package
    }


def test_all_declared_environments_exist(registry):
    environments = load_environments()
    names = {environment["name"] for environment in environments.values()}

    assert {model.environment for model in registry.models.values()} <= names
    assert names == EXPECTED_ENVIRONMENTS


def test_gpu_environments_pin_python_torch_and_cuda_strategy():
    for environment in load_environments().values():
        dependencies = environment["dependencies"]
        rendered_dependencies = "\n".join(
            dependency if isinstance(dependency, str) else ""
            for dependency in dependencies
        )
        pip_packages = "\n".join(pip_dependencies(environment))

        assert re.search(r"^python=", rendered_dependencies, re.MULTILINE)
        assert "torch==" in pip_packages or re.search(
            r"^pytorch=", rendered_dependencies, re.MULTILINE
        )
        assert re.search(r"^pytorch-cuda=", rendered_dependencies, re.MULTILINE)
        assert environment["channels"]


def test_special_upstream_constraints_are_explicit():
    environments = load_environments()

    assert "flash-attn==2.8.3" not in pip_dependencies(environments["flm"])
    assert "flash-attn==2.7.4.post1" not in pip_dependencies(environments["duo"])
    assert "python=3.9" in environments["rdlm"]["dependencies"]
    assert "pytorch=2.3.1" in environments["rdlm"]["dependencies"]
    assert all("nightly" not in package for package in pip_dependencies(environments["sdtt"]))
    assert "torchdata==0.8.0" in pip_dependencies(environments["sdtt"])


def test_conda_pytorch_environments_do_not_override_its_torch_stack_with_pip():
    for environment in load_environments().values():
        assert all(
            not package.startswith(("torch==", "torchvision==", "triton=="))
            for package in pip_dependencies(environment)
        )

    for name in {"flm", "duo", "mdlm", "sdtt", "di4c"}:
        assert "torchvision=0.20.1" in load_environments()[name]["dependencies"]


def test_conda_pytorch_environments_pin_mkl_before_ijit_regression():
    for name, environment in load_environments().items():
        dependencies = conda_dependencies(environment)
        if any(dependency.startswith("pytorch=") for dependency in dependencies):
            assert "mkl<2024.1" in dependencies, name
        assert not any(
            dependency.startswith("intel-openmp=2024.0") for dependency in dependencies
        )


def test_upstream_entrypoint_dependencies_are_explicit():
    environments = load_environments()

    assert "rich==14.2.0" in pip_dependencies(environments["flm"])
    assert "rich==13.7.1" in pip_dependencies(environments["duo"])
    assert "rich==13.7.1" in pip_dependencies(environments["mdlm"])
    assert "rich==14.2.0" in pip_dependencies(environments["candi"])


def test_project_runtime_dependencies_are_available_in_every_environment():
    for name, environment in load_environments().items():
        packages = pinned_pip_dependencies(environment)
        assert packages["pydantic"] == "2.12.4", name
        assert packages["pyyaml"] == "6.0.2", name


def test_langflow_environment_includes_conditional_prompt_data_dependencies():
    """Catch conditional runs failing before LangFlow loads because datasets is absent."""

    packages = pinned_pip_dependencies(load_environments()["langflow"])

    assert packages["datasets"] == "3.6.0"
    assert packages["fsspec"] == "2025.3.0"
    assert packages["pyarrow"] == "20.0.0"


def test_python39_pydantic_environments_include_annotation_backport():
    environments = load_environments()

    for name in {"mdlm", "rdlm"}:
        packages = pinned_pip_dependencies(environments[name])
        assert packages["eval-type-backport"] == "0.2.2"


def test_sdtt_family_includes_pkg_resources_provider_for_legacy_imports():
    environments = load_environments()

    for name in {"sdtt", "di4c"}:
        assert "setuptools==80.9.0" in pip_dependencies(environments[name])
        assert "nltk==3.9.1" in pip_dependencies(environments[name])


def test_known_pinned_dependency_pairs_use_compatible_versions():
    environments = load_environments()
    compatibility_matrix = {
        ("duo", "datasets", "2.15.0", "fsspec"): {"2023.10.0"},
        ("candi", "numpy", "1.24.3", "scipy"): {"1.11.4"},
    }

    for (environment, left, left_version, right), compatible_versions in (
        compatibility_matrix.items()
    ):
        packages = pinned_pip_dependencies(environments[environment])
        assert packages[left] == left_version
        assert packages[right] in compatible_versions


def test_mdlm_defers_source_only_cuda_extensions_until_torch_is_available():
    dependencies = pip_dependencies(load_environments()["mdlm"])

    assert not any(package.startswith("causal-conv1d==") for package in dependencies)
    assert not any(package.startswith("mamba-ssm==") for package in dependencies)


@pytest.fixture
def fake_conda(tmp_path):
    command = tmp_path / "fake-conda"
    command.write_text(
        """#!/usr/bin/env python3
import json
import os
from pathlib import Path
import sys

arguments = sys.argv[1:]
with Path(os.environ["FAKE_CONDA_LOG"]).open("a") as log:
    log.write(json.dumps(arguments) + "\\n")

if arguments == ["env", "list", "--json"]:
    mode = os.environ.get("FAKE_ENV_LIST_MODE", "")
    if mode == "failure":
        raise SystemExit(23)
    if mode == "malformed":
        print("not-json")
        raise SystemExit(0)
    if mode == "wrong_type":
        print(json.dumps([]))
        raise SystemExit(0)
    if mode == "foreign":
        print(json.dumps({"envs": ["/foreign/dlb-langflow"]}))
        raise SystemExit(0)
    print(json.dumps({"envs": os.environ.get("FAKE_ENVS", "").split(os.pathsep)}))
    raise SystemExit(0)

if arguments == ["info", "--json"]:
    mode = os.environ.get("FAKE_INFO_MODE", "")
    if mode == "failure":
        raise SystemExit(23)
    if mode == "malformed":
        print("not-json")
        raise SystemExit(0)
    print(json.dumps({"envs_dirs": ["/opt/conda/envs"]}))
    raise SystemExit(0)

failure = os.environ.get("FAKE_FAIL_SUBSTRING", "")
if failure and any(failure in argument for argument in arguments):
    failure_stderr = os.environ.get("FAKE_FAIL_STDERR")
    if failure_stderr:
        print(failure_stderr, file=sys.stderr)
    raise SystemExit(23)

if arguments[:1] == ["run"]:
    if arguments[3:5] == ["python", "-c"]:
        print("2.5.1 12.4")
        raise SystemExit(0)
    if arguments[3:6] == ["python", "-m", "pip"]:
        raise SystemExit(0)
    if os.environ.get("FAKE_DROP_RUN_STDIN") and arguments[4] == "-":
        sys.stdin.read()
        raise SystemExit(0)
    if arguments[4] == "-":
        probe_source = sys.stdin.read()
    else:
        probe_source = Path(arguments[4]).read_text()
    output = os.environ.get("FAKE_PROBE_OUTPUT")
    if output is not None:
        print(output)
    else:
        modules = arguments[6:]
        payload = {
            "environment": arguments[5],
            "python": "3.11",
            "torch": "2.5.1",
            "torch_cuda": "12.4",
            "cuda_available": True,
            "imports": {module: True for module in modules},
        }
        if os.environ.get("FAKE_UNHEALTHY_PROBE"):
            payload["imports"][modules[0]] = False
            payload["import_errors"] = {modules[0]: "simulated import failure"}
        print("DLB_ENV_PROBE_V1:" + json.dumps(payload, sort_keys=True))
        if (
            os.environ.get("FAKE_APPEND_CONDA_DIAGNOSTIC")
            and "raise SystemExit(0)" not in probe_source
        ):
            print("CondaError: child process exited with status 1", file=sys.stderr)
            raise SystemExit(23)
"""
    )
    command.chmod(0o755)
    return command


def run_script(script_name, fake_conda, tmp_path, names, **extra_environment):
    log = tmp_path / "conda.log"
    environment = {
        **os.environ,
        "DLB_CONDA": str(fake_conda),
        "DLB_ENV_NAMES": ",".join(names),
        "FAKE_CONDA_LOG": str(log),
        **extra_environment,
    }
    completed = subprocess.run(
        ["bash", str(ENVIRONMENT_DIR / script_name)],
        cwd=Path.cwd(),
        env=environment,
        text=True,
        capture_output=True,
    )
    calls = [json.loads(line) for line in log.read_text().splitlines()] if log.exists() else []
    return completed, calls


def test_create_all_creates_absent_environment_then_installs_flash_attention(
    fake_conda, tmp_path
):
    completed, calls = run_script("create_all.sh", fake_conda, tmp_path, ["dlb-flm"])

    assert completed.returncode == 0, completed.stderr
    create_call = next(call for call in calls if call[:3] == ["env", "create", "--file"])
    assert Path(create_call[3]).name == "flm.yml"
    assert [
        "run",
        "-n",
        "dlb-flm",
        "python",
        "-m",
        "pip",
        "install",
        "flash-attn==2.7.4.post1",
        "--no-build-isolation",
    ] in calls


def test_create_all_checks_torch_import_before_flash_attention(fake_conda, tmp_path):
    completed, calls = run_script("create_all.sh", fake_conda, tmp_path, ["dlb-duo"])

    assert completed.returncode == 0, completed.stderr
    torch_import_call = next(
        call for call in calls
        if call[:5] == ["run", "-n", "dlb-duo", "python", "-c"]
        and "import torch" in call[5]
    )
    flash_install_call = [
        "run",
        "-n",
        "dlb-duo",
        "python",
        "-m",
        "pip",
        "install",
        "flash-attn==2.7.4.post1",
        "--no-build-isolation",
    ]
    assert calls.index(torch_import_call) < calls.index(flash_install_call)


def test_create_all_installs_mdlm_cuda_extensions_from_git_after_torch(
    fake_conda, tmp_path
):
    completed, calls = run_script("create_all.sh", fake_conda, tmp_path, ["dlb-mdlm"])

    assert completed.returncode == 0, completed.stderr
    torch_import_call = next(
        call for call in calls
        if call[:5] == ["run", "-n", "dlb-mdlm", "python", "-c"]
        and "import torch" in call[5]
    )
    extension_install_call = [
        "run",
        "-n",
        "dlb-mdlm",
        "python",
        "-m",
        "pip",
        "install",
        "--no-build-isolation",
        "git+https://github.com/Dao-AILab/causal-conv1d.git@v1.1.3.post1",
        "git+https://github.com/state-spaces/mamba.git@v1.1.4",
    ]
    assert calls.index(torch_import_call) < calls.index(extension_install_call)


def test_create_all_updates_existing_environment_without_pruning(fake_conda, tmp_path):
    completed, calls = run_script(
        "create_all.sh",
        fake_conda,
        tmp_path,
        ["dlb-langflow"],
        FAKE_ENVS="/opt/conda/envs/dlb-langflow",
    )

    assert completed.returncode == 0, completed.stderr
    update_call = next(call for call in calls if call[:3] == ["env", "update", "--file"])
    assert Path(update_call[3]).name == "langflow.yml"
    assert update_call == [
        "env",
        "update",
        "--file",
        update_call[3],
    ]


def test_create_all_reports_failures_but_continues_other_environments(fake_conda, tmp_path):
    completed, calls = run_script(
        "create_all.sh",
        fake_conda,
        tmp_path,
        ["dlb-langflow", "dlb-eval"],
        FAKE_FAIL_SUBSTRING="langflow.yml",
    )

    assert completed.returncode != 0
    assert "dlb-langflow" in completed.stderr
    assert any(
        call[:3] == ["env", "create", "--file"] and Path(call[3]).name == "eval.yml"
        for call in calls
    )


@pytest.mark.parametrize(
    ("environment_variable", "value"),
    [
        ("FAKE_ENV_LIST_MODE", "malformed"),
        ("FAKE_ENV_LIST_MODE", "wrong_type"),
        ("FAKE_ENV_LIST_MODE", "failure"),
        ("FAKE_INFO_MODE", "malformed"),
        ("FAKE_INFO_MODE", "failure"),
        ("FAKE_ENV_LIST_MODE", "foreign"),
    ],
)
def test_create_all_refuses_unreliable_environment_discovery(
    fake_conda, tmp_path, environment_variable, value
):
    completed, calls = run_script(
        "create_all.sh",
        fake_conda,
        tmp_path,
        ["dlb-langflow"],
        **{environment_variable: value},
    )

    assert completed.returncode != 0
    assert "discovery" in completed.stderr
    assert not any(call[:2] == ["env", "create"] for call in calls)


def test_create_all_distinguishes_exact_existing_name_from_absence(fake_conda, tmp_path):
    existing, existing_calls = run_script(
        "create_all.sh",
        fake_conda,
        tmp_path,
        ["dlb-langflow"],
        FAKE_ENVS="/opt/conda/envs/dlb-langflow",
    )
    absent, absent_calls = run_script(
        "create_all.sh", fake_conda, tmp_path, ["dlb-langflow"]
    )

    assert existing.returncode == 0
    assert any(call[:2] == ["env", "update"] for call in existing_calls)
    assert absent.returncode == 0
    assert any(call[:2] == ["env", "create"] for call in absent_calls)


def test_verify_all_emits_json_for_each_environment_and_fails_in_aggregate(
    fake_conda, tmp_path
):
    completed, calls = run_script(
        "verify_all.sh",
        fake_conda,
        tmp_path,
        ["dlb-langflow", "dlb-duo"],
        FAKE_FAIL_SUBSTRING="dlb-duo",
    )

    records = [json.loads(line) for line in completed.stdout.splitlines()]
    assert completed.returncode != 0
    assert records[0]["environment"] == "dlb-langflow"
    assert records[0]["cuda_available"] is True
    assert "torchvision" in calls[-1]
    assert "flash_attn" in calls[-1]
    assert "rich" in calls[-1]
    assert records[1] == {
        "environment": "dlb-duo",
        "python": None,
        "torch": None,
        "torch_cuda": None,
        "cuda_available": False,
        "imports": {},
        "error": "verification probe failed",
    }


def test_verify_all_debug_mode_reports_probe_manager_stderr(fake_conda, tmp_path):
    completed, _ = run_script(
        "verify_all.sh",
        fake_conda,
        tmp_path,
        ["dlb-duo"],
        DLB_VERIFY_DEBUG="1",
        FAKE_FAIL_SUBSTRING="dlb-duo",
        FAKE_FAIL_STDERR="simulated native loader failure",
    )

    records = [json.loads(line) for line in completed.stdout.splitlines()]
    assert completed.returncode != 0
    assert records[0]["error"] == "verification probe failed"
    assert "conda run exited with status 23" in records[0]["diagnostic"]
    assert "simulated native loader failure" in records[0]["diagnostic"]


def test_verify_all_does_not_depend_on_manager_forwarding_stdin(fake_conda, tmp_path):
    completed, calls = run_script(
        "verify_all.sh",
        fake_conda,
        tmp_path,
        ["dlb-duo"],
        FAKE_DROP_RUN_STDIN="1",
    )

    assert completed.returncode == 0, completed.stderr
    run_call = next(call for call in calls if call[:3] == ["run", "-n", "dlb-duo"])
    assert run_call[3] == "python"
    assert run_call[4] != "-"


@pytest.mark.parametrize(
    "probe_output",
    [
        "run -n dlb-langflow python",
        "noise\nDLB_ENV_PROBE_V1:{}",
        "DLB_ENV_PROBE_V1:not-json",
    ],
)
def test_verify_all_rejects_untrusted_manager_stdout(fake_conda, tmp_path, probe_output):
    completed, _ = run_script(
        "verify_all.sh",
        fake_conda,
        tmp_path,
        ["dlb-langflow"],
        FAKE_PROBE_OUTPUT=probe_output,
    )

    records = [json.loads(line) for line in completed.stdout.splitlines()]
    assert completed.returncode != 0
    assert records == [
        {
            "environment": "dlb-langflow",
            "python": None,
            "torch": None,
            "torch_cuda": None,
            "cuda_available": False,
            "imports": {},
            "error": "verification probe failed",
        }
    ]


@pytest.mark.parametrize(
    "payload",
    [
        '{"environment":"dlb-langflow","python":"3.11","torch":NaN,"torch_cuda":"12.4","cuda_available":true,"imports":{"einops":true,"huggingface_hub":true,"safetensors":true,"transformers":true}}',
        '{"environment":"dlb-langflow","python":"3.11","torch":"2.5.1","torch_cuda":Infinity,"cuda_available":true,"imports":{"einops":true,"huggingface_hub":true,"safetensors":true,"transformers":true}}',
        '{"environment":"dlb-langflow","python":"3.11","torch":"2.5.1","torch_cuda":"12.4","cuda_available":true,"imports":{"einops":true,"huggingface_hub":true,"safetensors":true,"transformers":true},"unexpected":true}',
        '{"environment":"dlb-langflow","python":"3.11","torch":"2.5.1","torch_cuda":"12.4","cuda_available":true,"imports":{"einops":true,"huggingface_hub":true,"safetensors":true,"transformers":true,"unexpected":true}}',
        '{"environment":"dlb-langflow","python":"3.11","torch":"2.5.1","torch_cuda":"12.4","cuda_available":1,"imports":{"einops":true,"huggingface_hub":true,"safetensors":true,"transformers":true}}',
        '{"environment":"dlb-langflow","python":"3.11","torch":"2.5.1","torch_cuda":"12.4","cuda_available":true,"imports":{"einops":true,"einops":false,"huggingface_hub":true,"safetensors":true,"transformers":true}}',
        '{"environment":"dlb-langflow","python":"3.11","torch":"2.5.1","torch_cuda":"12.4","cuda_available":true,"imports":{"einops":true,"huggingface_hub":true,"safetensors":true}}',
    ],
    ids=[
        "nan",
        "infinity",
        "unknown-top-level-field",
        "unknown-import-field",
        "bool-as-int",
        "duplicate-requested-import",
        "missing-requested-import",
    ],
)
def test_verify_all_rejects_invalid_probe_schema(fake_conda, tmp_path, payload):
    completed, _ = run_script(
        "verify_all.sh",
        fake_conda,
        tmp_path,
        ["dlb-langflow"],
        FAKE_PROBE_OUTPUT="DLB_ENV_PROBE_V1:" + payload,
    )

    records = [json.loads(line) for line in completed.stdout.splitlines()]
    assert completed.returncode != 0
    assert records == [
        {
            "environment": "dlb-langflow",
            "python": None,
            "torch": None,
            "torch_cuda": None,
            "cuda_available": False,
            "imports": {},
            "error": "verification probe failed",
        }
    ]
    assert "NaN" not in completed.stdout
    assert "Infinity" not in completed.stdout


def test_verify_all_preserves_unhealthy_probe_when_manager_adds_diagnostic(
    fake_conda, tmp_path
):
    completed, _ = run_script(
        "verify_all.sh",
        fake_conda,
        tmp_path,
        ["dlb-langflow"],
        FAKE_UNHEALTHY_PROBE="1",
        FAKE_APPEND_CONDA_DIAGNOSTIC="1",
    )

    records = [json.loads(line) for line in completed.stdout.splitlines()]
    assert completed.returncode != 0
    assert records[0]["imports"]["datasets"] is False
    assert records[0]["import_errors"] == {"datasets": "simulated import failure"}
    assert "CondaError" not in completed.stdout
    assert "CondaError" not in completed.stderr


def test_verify_all_rejects_echo_manager_output(fake_conda, tmp_path):
    completed, _ = run_script(
        "verify_all.sh", fake_conda, tmp_path, ["dlb-langflow"], DLB_CONDA="/bin/echo"
    )

    records = [json.loads(line) for line in completed.stdout.splitlines()]
    assert completed.returncode != 0
    assert records[0]["error"] == "verification probe failed"


def test_verify_all_rejects_flash_attention_without_required_torch_triton_api(
    fake_conda, tmp_path
):
    payload = {
        "environment": "dlb-flm",
        "python": "3.11",
        "torch": "2.5.1",
        "torch_cuda": "12.4",
        "cuda_available": True,
        "imports": {
            module: True
            for module in [
                "datasets",
                "einops",
                "entmax",
                "flash_attn",
                "fsspec",
                "huggingface_hub",
                "hydra",
                "lightning",
                "numpy",
                "omegaconf",
                "pydantic",
                "requests",
                "rich",
                "scipy",
                "timm",
                "tokenizers",
                "torchmetrics",
                "tqdm",
                "transformers",
                "triton",
                "wandb",
                "yaml",
            ]
        },
        "runtime_checks": {"flash_attn_torch_wrap_triton": False},
        "runtime_errors": {
            "flash_attn_torch_wrap_triton": (
                "flash-attn 2.8.3 requires torch.library.wrap_triton"
            )
        },
    }
    completed, _ = run_script(
        "verify_all.sh",
        fake_conda,
        tmp_path,
        ["dlb-flm"],
        FAKE_PROBE_OUTPUT="DLB_ENV_PROBE_V1:" + json.dumps(payload, sort_keys=True),
    )

    records = [json.loads(line) for line in completed.stdout.splitlines()]
    assert completed.returncode != 0
    assert records[0]["runtime_checks"]["flash_attn_torch_wrap_triton"] is False
    assert "wrap_triton" in records[0]["runtime_errors"]["flash_attn_torch_wrap_triton"]


def test_verify_all_escapes_unknown_environment_names(fake_conda, tmp_path):
    completed, _ = run_script(
        "verify_all.sh", fake_conda, tmp_path, ['bad"name']
    )

    records = [json.loads(line) for line in completed.stdout.splitlines()]
    assert completed.returncode != 0
    assert records[0]["environment"] == 'bad"name'
    assert records[0]["error"] == "unknown environment"


def test_verify_all_checks_the_complete_method_import_mapping(fake_conda, tmp_path):
    project_runtime_imports = {"pydantic", "yaml"}
    expected_imports = {
        "dlb-flm": {
            "datasets", "einops", "entmax", "flash_attn", "fsspec", "huggingface_hub",
            "hydra", "lightning", "numpy", "omegaconf", "requests", "rich", "scipy",
            "timm", "tokenizers", "torchmetrics", "tqdm", "transformers", "triton", "wandb",
        },
        "dlb-langflow": {
            "datasets", "einops", "fsspec", "huggingface_hub", "pyarrow",
            "safetensors", "transformers",
        },
        "dlb-duo": {
            "datasets", "einops", "flash_attn", "fsspec", "h5py", "huggingface_hub",
            "hydra", "lightning", "numpy", "omegaconf", "requests", "rich", "scipy", "timm",
            "tokenizers", "torchmetrics", "torchvision", "tqdm", "transformers", "triton", "wandb",
        },
        "dlb-mdlm": {
            "causal_conv1d", "datasets", "einops", "eval_type_backport", "flash_attn",
            "fsspec", "huggingface_hub", "hydra", "lightning", "mamba_ssm", "numpy",
            "omegaconf", "requests", "rich", "timm", "tokenizers", "torchmetrics",
            "transformers", "wandb",
        },
        "dlb-candi": {
            "datasets", "einops", "evaluate", "flash_attn", "fsspec", "huggingface_hub", "hydra",
            "lightning", "numpy", "omegaconf", "requests", "rich", "scipy", "tokenizers", "torchmetrics",
            "tqdm", "transformers",
        },
        "dlb-rdlm": {
            "accelerate", "datasets", "einops", "eval_type_backport", "fsspec",
            "huggingface_hub", "hydra", "numpy", "omegaconf", "requests", "scipy",
            "tokenizers", "tqdm", "transformers", "wandb",
        },
        "dlb-sdtt": {
            "datasets", "einops", "flash_attn", "fsspec", "huggingface_hub", "hydra", "lightning",
            "loguru", "mauve", "nltk", "numpy", "omegaconf", "pandas", "requests", "safetensors",
            "tensorboard", "timm", "tokenizers", "torchdata", "tqdm", "transformers", "wandb",
        },
        "dlb-di4c": {
            "datasets", "einops", "flash_attn", "fsspec", "huggingface_hub", "hydra", "lightning",
            "loguru", "mauve", "nltk", "numpy", "omegaconf", "pandas", "requests", "safetensors",
            "tensorboard", "timm", "tokenizers", "torchdata", "tqdm", "transformers", "wandb",
        },
        "dlb-eval": {"accelerate", "datasets", "evaluate", "fsspec", "mauve", "sacrebleu", "scipy", "tokenizers", "transformers"},
    }
    expected_imports = {
        environment: modules | project_runtime_imports
        for environment, modules in expected_imports.items()
    }
    completed, calls = run_script(
        "verify_all.sh", fake_conda, tmp_path, list(expected_imports)
    )

    assert completed.returncode == 0, completed.stderr
    requested_imports = {}
    for call in calls:
        if call[:1] != ["run"]:
            continue
        assert call[:2] == ["run", "-n"]
        assert call[3] == "python"
        assert call[4] != "-"
        manager_environment = call[2]
        probe_environment = call[5]
        assert manager_environment == probe_environment
        assert manager_environment in expected_imports
        assert manager_environment not in requested_imports
        raw_modules = call[6:]
        assert len(raw_modules) == len(set(raw_modules))
        requested_imports[manager_environment] = set(raw_modules)

    assert set(requested_imports) == set(expected_imports)
    for environment, expected_modules in expected_imports.items():
        assert requested_imports[environment] == expected_modules


def test_pack_all_writes_archives_without_removing_environment(fake_conda, tmp_path):
    packer = tmp_path / "fake-conda-pack"
    packer.write_text(
        """#!/usr/bin/env python3
import pathlib
import sys
output = pathlib.Path(sys.argv[sys.argv.index("-o") + 1])
output.write_bytes(b"archive")
"""
    )
    packer.chmod(0o755)
    artifact_dir = tmp_path / "artifacts"

    completed, calls = run_script(
        "pack_all.sh",
        fake_conda,
        tmp_path,
        ["dlb-langflow"],
        DLB_CONDA_PACK=str(packer),
        DLB_ARTIFACT_DIR=str(artifact_dir),
    )

    assert completed.returncode == 0, completed.stderr
    assert (artifact_dir / "dlb-langflow.tar.gz").read_bytes() == b"archive"
    assert all("remove" not in call for call in calls)


def test_pack_all_rejects_a_non_file_destination_before_atomic_publication(
    fake_conda, tmp_path
):
    packer = tmp_path / "fake-conda-pack"
    packer.write_text(
        """#!/usr/bin/env python3
import pathlib
import sys
output = pathlib.Path(sys.argv[sys.argv.index("-o") + 1])
output.write_bytes(b"archive")
"""
    )
    packer.chmod(0o755)
    artifact_dir = tmp_path / "artifacts"
    (artifact_dir / "dlb-langflow.tar.gz").mkdir(parents=True)

    completed, _ = run_script(
        "pack_all.sh",
        fake_conda,
        tmp_path,
        ["dlb-langflow"],
        DLB_CONDA_PACK=str(packer),
        DLB_ARTIFACT_DIR=str(artifact_dir),
    )

    assert completed.returncode != 0
    assert not (artifact_dir / "dlb-langflow.tar.gz" / "dlb-langflow.tar.gz").exists()