File size: 28,077 Bytes
83e1c23
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ab97788
83e1c23
 
 
 
 
 
 
 
6a8a419
285e72f
 
0421ffe
285e72f
 
 
 
6a8a419
53a2e9c
 
6a8a419
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
53a2e9c
 
 
 
 
 
 
 
 
 
 
6a8a419
 
53a2e9c
 
 
 
 
6a8a419
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
53a2e9c
6a8a419
 
 
 
 
f19f993
6a8a419
 
 
 
 
53a2e9c
 
 
 
6a8a419
 
83e1c23
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9483be8
83e1c23
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9ceccf9
83e1c23
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
76405c8
 
83e1c23
76405c8
 
9ceccf9
76405c8
53a2e9c
 
f138863
83e1c23
53a2e9c
83e1c23
 
 
 
 
f138863
 
83e1c23
f138863
83e1c23
 
4d7ac87
 
83e1c23
 
 
 
 
53a2e9c
9ceccf9
 
 
53a2e9c
83e1c23
 
 
 
f138863
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
83e1c23
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4145b27
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
83e1c23
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
53a2e9c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fb4d72b
53a2e9c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0ce755e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
46a15a1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
cd857fb
 
9ceccf9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ab97788
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
68947c8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9483be8
68947c8
 
 
 
9483be8
 
 
 
 
 
68947c8
 
 
764e398
 
 
 
68947c8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9483be8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Behavioral checks for billing, isolation, output preservation and preset recovery."""

import hashlib
import json
import sys
import time
from pathlib import Path
from types import SimpleNamespace
from unittest.mock import Mock

import pyarrow as pa
import pytest

sys.path.insert(0, str(Path(__file__).parent))
import studio  # noqa: E402
from model_catalog import MODELS, model_profile  # noqa: E402
from provider import Budget, BudgetExceededError  # noqa: E402
from worker import FALLBACK, PreservingWriter, TunedPlan  # noqa: E402

from lerobot.annotations.steerable_pipeline.config import PlanConfig
from lerobot.annotations.steerable_pipeline.modules.plan_subtasks_memory import PlanSubtasksMemoryModule
from lerobot.annotations.steerable_pipeline.vlm_client import StubVlmClient


@pytest.mark.parametrize(
    "features",
    [
        [f for f in studio.FEATURES if f not in {studio.HANDS, studio.SLAM}],
        ["Interjections & speech"],
        ["Memory"],
        ["Visual questions"],
    ],
)
@pytest.mark.parametrize("custom", [False, True])
def test_real_video_pipeline_and_checkbox_filtering(tmp_path, features, custom):
    import av
    import numpy as np
    from worker import annotate_episode

    from lerobot.annotations.steerable_pipeline.reader import EpisodeRecord
    from lerobot.annotations.steerable_pipeline.staging import EpisodeStaging

    camera = "observation.images.top"
    video = tmp_path / "videos" / camera / "chunk-000" / "file-000.mp4"
    video.parent.mkdir(parents=True)
    with av.open(str(video), "w") as container:
        stream = container.add_stream("mpeg4", rate=10)
        stream.width, stream.height, stream.pix_fmt = 64, 64, "yuv420p"
        for i in range(40):
            frame = av.VideoFrame.from_ndarray(np.full((64, 64, 3), i * 5, dtype=np.uint8), format="rgb24")
            for packet in stream.encode(frame):
                container.mux(packet)
        for packet in stream.encode():
            container.mux(packet)
    info = {"fps": 10, "video_path": "videos/{video_key}/chunk-{chunk_index:03d}/file-{file_index:03d}.mp4"}
    row = {
        "length": 40,
        f"videos/{camera}/chunk_index": 0,
        f"videos/{camera}/file_index": 0,
        f"videos/{camera}/from_timestamp": 0.0,
    }
    record = EpisodeRecord(
        0,
        "move the cup to the table",
        tuple(i / 10 for i in range(40)),
        tuple(range(40)),
        tmp_path / "unused.parquet",
        0,
        40,
    )

    from prompt_config import TEMPLATES, validate_prompts

    overrides = (
        validate_prompts(
            {key: item["default"] + f"\nCUSTOM_{key}" for key, item in TEMPLATES.items()}, features
        )
        if custom
        else {}
    )
    seen = set()

    def respond(messages):
        text = " ".join(p.get("text", "") for m in messages for p in m["content"] if isinstance(p, dict))
        for key in overrides:
            if f"CUSTOM_{key}" in text:
                seen.add(key)
        if "CUSTOM_plan_render" in text:
            return {"plan": "1. Put the cup on the table."}
        if "meaningful action phases" in text:
            return {
                "subtasks": [
                    {"text": "grasp the cup", "start": 0, "end": 2.5},
                    {"text": "place the cup on table", "start": 2.5, "end": 3.9},
                ]
            }
        if "compressed semantic memory" in text:
            return {"memory": "grasped the cup"}
        if "acknowledgement the robot" in text:
            return {"text": "On it."}
        if "Write ONE compact interjection" in text:
            return {"interjection": "place the cup on the table", "speech": "Okay."}
        if "frame-grounded visual question" in text:
            return {"question": "How many cups?", "answer": {"label": "cup", "count": 1}}
        if "rephras" in text.lower():
            return {"rephrasings": ["put the cup on the table"]}
        raise AssertionError("Unexpected pipeline prompt")

    result = annotate_episode(
        record,
        row,
        info,
        tmp_path,
        {"camera": camera, "features": features, "vqa_hz": 0.1, "prompts": overrides},
        StubVlmClient(respond),
        tmp_path / "staging",
    )
    staged = EpisodeStaging(tmp_path / "staging", 0).read_all()
    actual_styles = {r.get("style") for rows in staged.values() for r in rows}
    expected = {studio.FEATURES[f] for f in features if f != studio.HUMAN_VIDEOS}
    if "Interjections & speech" in features:
        expected.add(None)
    assert actual_styles == expected
    assert result["status"] == "completed"
    assert result["rows"] > 0
    assert set(overrides) == seen
    from worker import CUSTOM_PROMPTS

    assert CUSTOM_PROMPTS.get() is None


def selection():
    return {
        "episodes": [{"episode_index": 0, "length": 300}, {"episode_index": 1, "length": 600}],
        "seconds": 30,
        "info": {"fps": 30},
        "dataset": "source/robot",
        "revision": "a" * 40,
        "start": 0,
        "count": 2,
    }


def plan():
    return {
        "selection": selection(),
        "features": ["Subtasks"],
        "camera": "observation.images.top",
        "vqa_hz": 0.1,
        "mode": studio.COPY,
        "created": time.time(),
        "nonce": "request123",
        "costs": studio.estimate(selection(), ["Subtasks"], 0.1, 4, 0.0005),
    }


def api_mock():
    api = Mock()
    api.whoami.return_value = {"name": "alice"}
    api.list_jobs.return_value = []
    api.repo_exists.return_value = False
    api.dataset_info.return_value = SimpleNamespace(id="source/robot", sha="a" * 40)
    api.space_info.return_value = SimpleNamespace(sha="b" * 40)
    api.list_jobs_hardware.return_value = [
        SimpleNamespace(name="cpu-upgrade", unit_label="minute", unit_cost_usd=0.0005)
    ]
    api.model_info.return_value = SimpleNamespace(
        inference_provider_mapping={studio.PROVIDER: SimpleNamespace(status="live")}
    )
    api.run_job.return_value = SimpleNamespace(id="job123")
    return api


def test_cost_features_and_timeout():
    base = studio.estimate(selection(), ["Subtasks"], 0.1, 1, 0.0005)
    plans = studio.estimate(selection(), ["Subtasks", "Plans"], 0.1, 1, 0.0005)
    memory = studio.estimate(selection(), ["Subtasks", "Memory"], 0.1, 1, 0.0005)
    vqa = studio.estimate(selection(), ["Visual questions"], 1.0, 4, 0.0005)
    assert base["inference"] == plans["inference"] < memory["inference"]
    assert base["cpu_max"] < vqa["cpu_max"]
    assert base["low"] < base["high"]
    assert vqa["calls"] == 32


@pytest.mark.parametrize("value", ["../secret", "/tmp/file", "videos/../../secret", "a\\b"])
def test_reject_metadata_path_traversal(value):
    with pytest.raises(ValueError):
        studio.relative_path(value)


def test_budget_unknown_failure_is_never_refunded():
    budget = Budget(0.09, 0.15, 0.5)
    first = budget.reserve(4096)
    budget.reconcile(first, None)
    second = budget.reserve(4096)
    budget.reconcile(second, {"prompt_tokens": 1000, "completion_tokens": 100})
    assert budget.reserved == pytest.approx(first + 0.0002)
    budget.reserve(4096)
    with pytest.raises(BudgetExceededError):
        budget.reserve(4096)


@pytest.mark.parametrize("mapping_format", ["dict", "list"])
def test_launch_identity_secret_and_immutable_manifest(mapping_format):
    api = api_mock()
    if mapping_format == "list":
        api.model_info.return_value.inference_provider_mapping = [
            SimpleNamespace(provider=studio.PROVIDER, status="live")
        ]
    configured = plan()
    configured["prompts"] = {"plan_subtasks": "custom instructions preserved verbatim"}
    factory = Mock(return_value=api)
    result = studio.launch(
        configured,
        "alice/annotations",
        5,
        True,
        "sensitive-token",
        space="lerobot/annotation-studio",
        api_factory=factory,
        expected_owner="alice",
    )
    factory.assert_called_once_with(token="sensitive-token")
    args = api.run_job.call_args.kwargs
    assert args["namespace"] == "alice"
    assert args["labels"]["annotation_output"] == "annotations"
    assert all("/" not in value for value in args["labels"].values())
    assert args["secrets"] == {"HF_TOKEN": "sensitive-token"}
    assert "sensitive-token" not in json.dumps(args["env"]) + json.dumps(args["command"])
    manifest = json.loads(args["env"]["ANNOTATION_MANIFEST"])
    assert manifest["episode_ids_sha256"] == hashlib.sha256(json.dumps([0, 1]).encode()).hexdigest()
    assert manifest["space_revision"] == "b" * 40
    assert manifest["prompts"] == configured["prompts"]
    assert manifest["model"] == "Qwen/Qwen3.8-27B:novita"
    assert manifest["request_concurrency"] == manifest["episode_parallelism"] == 32
    assert manifest["costs"]["context_length"] == 1_000_000
    assert "prompt-templates.json" in str(args["command"])
    assert manifest["inference_budget"] + plan()["costs"]["cpu_max"] == 5
    assert result["id"] == "job123"


@pytest.mark.parametrize("quote_owner,expected_owner", [("bob", "alice"), ("alice", "bob")])
def test_launch_binds_token_identity_to_session_and_quote(monkeypatch, quote_owner, expected_owner):
    monkeypatch.setenv("HF_TOKEN", "finerobotics-owner-token")
    api = api_mock()
    factory = Mock(return_value=api)
    configured = plan()
    configured["quote_owner"] = quote_owner
    with pytest.raises(ValueError):
        studio.launch(
            configured,
            "alice/output",
            5,
            True,
            "alice-token",
            expected_owner=expected_owner,
            api_factory=factory,
        )
    factory.assert_called_once_with(token="alice-token")
    api.run_job.assert_not_called()


@pytest.mark.parametrize(
    "output,consent,token",
    [("bob/output", True, "token"), ("alice/output", False, "token"), ("alice/output", True, False)],
)
def test_launch_rejects_unauthorized_or_unconfirmed(output, consent, token):
    api = api_mock()
    with pytest.raises(ValueError):
        studio.launch(
            plan(), output, 5, consent, token, space="lerobot/annotation-studio", api_factory=lambda **_: api
        )
    api.run_job.assert_not_called()


def test_duplicate_click_recovers_existing_job():
    api = api_mock()
    api.list_jobs.return_value = [
        SimpleNamespace(id="first-job", labels={"annotation_request": "request123"})
    ]
    assert (
        studio.launch(plan(), "alice/output", 5, True, "token", api_factory=lambda **_: api)["id"]
        == "first-job"
    )
    api.run_job.assert_not_called()


def test_source_changes_block_launch():
    api = api_mock()
    api.dataset_info.return_value.sha = "c" * 40
    with pytest.raises(ValueError, match="source changed"):
        studio.launch(plan(), "alice/output", 5, True, "token", api_factory=lambda **_: api)
    api.run_job.assert_not_called()


def test_compact_quote_preserves_pinned_episode_selection():
    api = api_mock()
    request = plan()
    digest = hashlib.sha256(json.dumps([0, 1]).encode()).hexdigest()
    request["selection"].pop("episodes")
    request["selection"]["episode_ids_sha256"] = digest
    studio.launch(
        request,
        "alice/output",
        5,
        True,
        "token",
        space="lerobot/annotation-studio",
        api_factory=lambda **_: api,
    )
    manifest = json.loads(api.run_job.call_args.kwargs["env"]["ANNOTATION_MANIFEST"])
    assert manifest["episode_ids_sha256"] == digest


def test_preserve_unselected_episodes_and_styles():
    def row(text, style):
        return {
            "role": "assistant",
            "content": text,
            "style": style,
            "timestamp": 0.0,
            "camera": None,
            "tool_calls": None,
        }

    old = [[row("old subtask", "subtask"), row("keep memory", "memory")], [row("untouched", "subtask")]]
    table = pa.table({"episode_index": [0, 1], "timestamp": [0.0, 0.0], "language_persistent": old})
    writer = PreservingWriter([0], {"subtask"})
    result = writer._materialize_table(table, [[row("new subtask", "subtask")], []], [[], []], drop_old=False)
    rows = result.column("language_persistent").to_pylist()
    assert {r["content"] for r in rows[0]} == {"new subtask", "keep memory"}
    assert rows[1] == old[1]


def test_recovery_uses_original_prompt_and_does_not_swallow_budget(monkeypatch):
    calls = []

    def generate(self, record, task=None):
        calls.append(FALLBACK.get())
        if not FALLBACK.get():
            raise ValueError("Invalid model response")
        return [{"text": "grasp cup", "start": 0, "end": 1}]

    monkeypatch.setattr(PlanSubtasksMemoryModule, "_generate_subtasks", generate)
    model = TunedPlan(vlm=StubVlmClient(lambda _: {}), config=PlanConfig())
    assert model._generate_subtasks(None)[0]["text"] == "grasp cup"
    assert calls == [False, True] and not FALLBACK.get()
    monkeypatch.setattr(
        PlanSubtasksMemoryModule, "_generate_subtasks", Mock(side_effect=BudgetExceededError())
    )
    with pytest.raises(BudgetExceededError):
        model._generate_subtasks(None)
    assert not model.used_fallback


def test_prompt_defaults_and_validation():
    from prompt_config import TEMPLATES, validate_prompts
    from worker import ACTION_PROMPT, ORIGINAL_LOAD

    for key, item in TEMPLATES.items():
        default = item["default"]
        assert validate_prompts({key: default}) == {}
        if key == "plan_subtasks":
            assert default == ACTION_PROMPT
        elif key != "plan_render":
            assert default == ORIGINAL_LOAD(key)
    default = TEMPLATES["plan_subtasks"]["default"]
    for bad in [
        "",
        default.replace("{episode_task}", ""),
        default + "{episode_task.__class__}",
        default + "{unknown}",
        default + "{episode_task:999999999}",
        "a" * 20001,
    ]:
        with pytest.raises(ValueError):
            validate_prompts({"plan_subtasks": bad})
    with pytest.raises(ValueError):
        validate_prompts({"unknown": "text"})
    edits = {"plan_subtasks": default + "\nUse concise labels."}
    assert validate_prompts(edits, ["Memory"]) == edits
    assert validate_prompts(edits, ["Visual questions"]) == {}


def test_custom_plan_adds_estimated_calls_and_reset_restores_baseline():
    from prompt_config import TEMPLATES

    features = ["Plans"]
    base = studio.estimate(selection(), features, 0.1, 1, 0.0005)
    template = TEMPLATES["plan_render"]["default"]
    custom = studio.estimate(
        selection(), features, 0.1, 1, 0.0005, {"plan_render": template + "\nBe concise."}
    )
    reset = studio.estimate(selection(), features, 0.1, 1, 0.0005, {"plan_render": template})
    assert custom["calls"] > base["calls"]
    assert custom["inference"] > base["inference"]
    assert custom["custom_prompts"] == 1
    assert reset == base


def test_custom_prompt_context_resets_on_failure_and_is_thread_isolated(monkeypatch):
    from concurrent.futures import ThreadPoolExecutor
    from threading import Barrier

    import worker
    from prompt_config import TEMPLATES

    barrier = Barrier(2)

    def fail(*args):
        barrier.wait(timeout=5)
        text = worker.load_prompt("plan_subtasks")
        fallback = FALLBACK.set(True)
        try:
            assert worker.load_prompt("plan_subtasks") == text
        finally:
            FALLBACK.reset(fallback)
        raise ValueError(text.splitlines()[-1])

    monkeypatch.setattr(worker, "run_runtime_episode", fail)

    def run(name):
        custom = TEMPLATES["plan_subtasks"]["default"] + "\n" + name
        with pytest.raises(ValueError, match=name):
            worker.annotate_episode(
                None,
                None,
                None,
                None,
                {"features": ["Subtasks"], "prompts": {"plan_subtasks": custom}},
                None,
                None,
            )
        assert worker.CUSTOM_PROMPTS.get() is None

    with ThreadPoolExecutor(max_workers=2) as pool:
        list(pool.map(run, ["FIRST_USER", "SECOND_USER"]))


def test_custom_plan_only_uses_remaining_actions_and_default_avoids_model():
    from prompt_config import TEMPLATES
    from worker import CUSTOM_PROMPTS

    record = SimpleNamespace(episode_task="move the cup")
    spans = [{"start": 0, "text": "reach the cup"}, {"start": 1, "text": "grasp the cup"}]
    vlm = Mock()
    module = TunedPlan(vlm=vlm, config=PlanConfig())
    assert module._generate_plan(record, spans, refresh_t=1) == "1. grasp the cup"
    vlm.generate_json.assert_not_called()
    vlm.generate_json.return_value = [{"plan": "1. Grasp the cup."}]
    ctx = CUSTOM_PROMPTS.set({"plan_render": TEMPLATES["plan_render"]["default"] + "\nBe brief."})
    try:
        assert module._generate_plan(record, spans, refresh_t=1) == "1. Grasp the cup."
        sent = str(vlm.generate_json.call_args)
        assert "grasp the cup" in sent and "reach the cup" not in sent
    finally:
        CUSTOM_PROMPTS.reset(ctx)


@pytest.mark.parametrize("token", [False, "private-reader-token"])
def test_camera_discovery_reads_only_pinned_info_and_uses_request_identity(tmp_path, monkeypatch, token):
    import tempfile

    path = tmp_path / "info.json"
    path.write_text(
        json.dumps(
            {
                "codebase_version": "v3.0",
                "fps": 30,
                "features": {
                    "observation.images.top": {"dtype": "video"},
                    "observation.images.left_wrist": {"dtype": "video"},
                    "observation.images.right_wrist": {"dtype": "video"},
                    "observation.images.depth": {"dtype": "video"},
                    "observation.state": {"dtype": "float32"},
                    "observation.images.still": {"dtype": "image"},
                },
            }
        )
    )
    api = Mock()
    api.dataset_info.return_value = SimpleNamespace(sha="c" * 40)
    factory = Mock(return_value=api)
    download = Mock(return_value=str(path))
    monkeypatch.setattr(studio, "HfApi", factory)
    monkeypatch.setattr(studio, "hf_hub_download", download)
    result = studio.read_cameras("owner/dataset", token)
    assert result["cameras"] == [
        "observation.images.top",
        "observation.images.left_wrist",
        "observation.images.right_wrist",
    ]
    factory.assert_called_once_with(token=token)
    api.list_repo_tree.assert_not_called()
    download.assert_called_once()
    args = download.call_args
    assert args.args == ("owner/dataset", "meta/info.json")
    assert args.kwargs["revision"] == "c" * 40
    assert args.kwargs["token"] == token
    assert args.kwargs["cache_dir"].startswith(tempfile.gettempdir())
    assert not Path(args.kwargs["cache_dir"]).exists()


def test_camera_discovery_rejects_unsupported_sources():
    with pytest.raises(ValueError, match="RGB video camera"):
        studio.video_cameras({"codebase_version": "v3.0", "fps": 30, "features": {}})
    with pytest.raises(ValueError, match="v3"):
        studio.video_cameras({"codebase_version": "v2.1"})


def test_usage_costs_use_reported_runtime_and_keep_unknown_charges_separate():
    from reporting import usage_costs

    job = SimpleNamespace(durations=SimpleNamespace(running_secs=61))
    result = usage_costs(
        job,
        {
            "manifest": {"costs": {"cpu_rate": 0.0005}},
            "usage": {"measured_inference_usd": 0.2, "reserved_including_uncertain_usd": 0.24},
        },
    )
    assert result["cpu_usd"] == 0.001
    assert result["total_usd"] == pytest.approx(0.201)
    assert result["uncertain_inference_usd"] == pytest.approx(0.04)
    assert usage_costs(SimpleNamespace(), {})["total_usd"] is None


def test_visualizer_links_only_target_materialized_copy_annotations():
    from reporting import annotation_url

    report = {
        "manifest": {"mode": studio.COPY, "output": "alice/out"},
        "status": "partial",
        "written_episode_ids": [12, 5],
    }
    assert annotation_url(report) == "https://lerobot-visualize-dataset.hf.space/alice/out/5?tab=annotations"
    report["manifest"]["mode"] = studio.EXPORT
    assert annotation_url(report) is None
    report["manifest"]["mode"] = studio.COPY
    report["written_episode_ids"] = []
    assert annotation_url(report) is None


def test_cached_usage_and_deadline_stop_before_a_paid_request(monkeypatch):
    import provider

    budget = Budget(1, 1, 2, cached_input_rate=0.2)
    reserved = budget.reserve(100)
    budget.reconcile(reserved, {"prompt_tokens": 10, "completion_tokens": 20, "cached_tokens": 6})
    assert budget.summary()["measured_inference_usd"] == pytest.approx(0.0000452)
    assert budget.summary()["input_tokens"] == 10
    assert budget.summary()["cached_input_tokens"] == 6
    post = Mock()
    monkeypatch.setattr(provider.requests, "post", post)
    client = provider.Provider("token", studio.MODEL, budget, deadline=time.time() - 1)
    with pytest.raises(provider.DeadlineExceededError):
        client.generate([[{"role": "user", "content": "hello"}]], 100, 0.2)
    post.assert_not_called()


def test_five_minute_run_has_job_and_worker_deadlines():
    api = api_mock()
    studio.launch(
        plan(),
        "alice/output",
        30,
        True,
        "token",
        space="lerobot/annotation-studio",
        api_factory=lambda **_: api,
        timeout_minutes=5,
    )
    kwargs = api.run_job.call_args.kwargs
    assert kwargs["timeout"] == "5m"
    assert "ANNOTATION_DEADLINE_EPOCH" in str(kwargs["command"])
    assert "270" in str(kwargs["command"])


def test_parallel_reservations_respect_hosted_context_and_small_budget():
    from provider import Provider

    budget = Budget(5, 0.42, 3, context_length=1_000_000)
    client = Provider("token", studio.MODEL, budget, concurrency=32)
    assert client.concurrency == 11
    reservations = [budget.reserve(4096) for _ in range(client.concurrency)]
    assert budget.reserved < budget.limit
    with pytest.raises(BudgetExceededError):
        budget.reserve(4096)
    budget.reconcile(reservations[0], {"prompt_tokens": 100, "completion_tokens": 40})
    budget.reserve(4096)
    assert (
        Provider(
            "token", studio.MODEL, Budget(30, 0.42, 3, context_length=1_000_000), concurrency=32
        ).concurrency
        == 32
    )


@pytest.mark.parametrize("key", list(MODELS))
def test_selected_model_pins_route_prices_and_generation_parameters(key, monkeypatch):
    import provider

    profile = model_profile(key)
    configured = plan()
    configured["costs"] = studio.estimate(selection(), ["Subtasks"], 0.1, 1, 0.0005, model_key=key)
    api = api_mock()
    api.model_info.return_value.inference_provider_mapping = {
        profile["provider"]: SimpleNamespace(status="live")
    }
    studio.launch(
        configured,
        "alice/selected",
        30,
        True,
        "secret",
        space="lerobot/annotation-studio",
        api_factory=lambda **_: api,
    )
    manifest = json.loads(api.run_job.call_args.kwargs["env"]["ANNOTATION_MANIFEST"])
    assert manifest["model"] == profile["route"]
    assert manifest["costs"]["input_rate"] == profile["input_rate"]
    assert manifest["request_concurrency"] == profile["concurrency"]
    api.model_info.assert_called_once_with(profile["model_id"], expand=["inferenceProviderMapping"])
    post = Mock(
        return_value=SimpleNamespace(
            status_code=200,
            json=lambda: {
                "choices": [{"message": {"content": "{}"}, "finish_reason": "stop"}],
                "usage": {"prompt_tokens": 100, "completion_tokens": 10},
            },
        )
    )
    monkeypatch.setattr(provider.requests, "post", post)
    client = provider.Provider(
        "secret",
        manifest["model"],
        Budget(10, profile["input_rate"], profile["output_rate"]),
        generation_options=manifest["generation_options"],
    )
    client.generate([[{"role": "user", "content": "JSON please"}]], 100, 0.2)
    body = post.call_args.kwargs["json"]
    assert body["model"] == profile["route"]
    assert {
        k: v for k, v in body.items() if k not in {"model", "messages", "max_tokens", "temperature"}
    } == profile["generation_options"]


def test_tiered_prices_reserve_maximum_and_reconcile_actual_prompt_tier():
    profile = model_profile("minimax-m3")
    budget = Budget(10, 0.3, 1.2, 0.06, 1_000_000, profile["pricing_tiers"])
    assert budget.reservation_cost(1000) == pytest.approx(0.6024)
    first = budget.reserve(1000)
    budget.reconcile(first, {"prompt_tokens": 1000, "completion_tokens": 1000, "cached_tokens": 500})
    assert budget.measured == pytest.approx(0.00138)
    second = budget.reserve(1000)
    budget.reconcile(second, {"prompt_tokens": 524288, "completion_tokens": 1000})
    assert budget.measured == pytest.approx(0.00138 + 524288 * 0.6 / 1e6 + 0.0024)


def test_unknown_model_and_stale_model_prices_cannot_launch():
    with pytest.raises(ValueError, match="supported vision model"):
        studio.prepare(
            "source/robot", 0, 1, ["Subtasks"], None, 0.1, 1, studio.COPY, model_key="invented/model"
        )
    api = api_mock()
    configured = plan()
    configured["costs"]["input_rate"] = 0
    with pytest.raises(ValueError, match="pricing changed"):
        studio.launch(
            configured,
            "alice/output",
            30,
            True,
            "secret",
            space="lerobot/annotation-studio",
            api_factory=lambda **_: api,
        )
    api.run_job.assert_not_called()


@pytest.mark.parametrize("visibility", ["private", "public"])
def test_launch_pins_output_visibility(visibility):
    api = api_mock()
    studio.launch(
        plan(),
        "alice/new-output",
        5,
        True,
        "token",
        space="lerobot/annotation-studio",
        api_factory=lambda **_: api,
        visibility=visibility,
    )
    manifest = json.loads(api.run_job.call_args.kwargs["env"]["ANNOTATION_MANIFEST"])
    assert manifest["visibility"] == visibility


@pytest.mark.parametrize("visibility", [None, "private", "public"])
def test_output_visibility_applies_to_parquet_dataset(visibility):
    import worker

    api = Mock()
    api.dataset_info.return_value.sha = "pinned"
    manifest = {
        "dataset": "alice/source",
        "dataset_revision": "pinned",
        "output": "alice/new",
        "mode": studio.COPY,
    }
    if visibility is not None:
        manifest["visibility"] = visibility
    worker.create_output_repo(api, manifest)
    api.create_repo.assert_called_once_with(
        "alice/new", repo_type="dataset", private=visibility != "public", exist_ok=False
    )
    api.duplicate_repo.assert_not_called()
    api.update_repo_settings.assert_not_called()


def test_invalid_visibility_cannot_launch_or_create_output():
    from worker import create_output_repo

    api = api_mock()
    with pytest.raises(ValueError, match="visibility"):
        studio.launch(
            plan(), "alice/out", 5, True, "token", api_factory=lambda **_: api, visibility="unknown"
        )
    with pytest.raises(ValueError, match="visibility"):
        create_output_repo(api, {"visibility": "unknown"})
    api.run_job.assert_not_called()
    api.create_repo.assert_not_called()
    api.duplicate_repo.assert_not_called()


def test_export_cannot_start_new_jobs_or_create_repositories():
    from worker import create_output_repo

    api = api_mock()
    legacy = plan()
    legacy["mode"] = studio.EXPORT
    with pytest.raises(ValueError, match="Parquet"):
        studio.prepare("alice/source", 0, 1, ["Subtasks"], None, 0.1, 1, studio.EXPORT)
    with pytest.raises(ValueError, match="Parquet"):
        studio.launch(legacy, "alice/out", 5, True, "token", api_factory=lambda **_: api)
    with pytest.raises(ValueError, match="Parquet"):
        create_output_repo(api, {"mode": studio.EXPORT})
    api.run_job.assert_not_called()
    api.create_repo.assert_not_called()