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
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
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
from __future__ import annotations

import json
import hashlib
import re
from dataclasses import asdict, dataclass
from pathlib import Path
from typing import Any

from .cache_store import JsonObjectCache
from .progress import progress
from .prompt_templates import PromptTemplate


TASK_INTERFACE_DIR = Path(__file__).resolve().parent / "task_interface"
DEFAULT_STRUCTURING_TEMPLATE = TASK_INTERFACE_DIR / "structuring_prompt_default.json"

FORBIDDEN_VISIBLE_KEYS = (
    "task_name",
    "task_id",
    "task_type",
    "task_index",
    "source_task_id",
    "dataset",
    "dataset_index",
    "split",
)
FORBIDDEN_ASSERT_KEYS = tuple(
    key for key in FORBIDDEN_VISIBLE_KEYS if key != "dataset"
)
FORBIDDEN_TASK_LABELS = ("gsm8k", "mathverse", "arxivqa", "mathvista", "chartqa")
EVIDENCE_MODALITIES = {
    "text",
    "image",
    "document",
    "tool_output",
    "environment_observation",
    "user_feedback",
    "action_trace",
    "other",
}
EVIDENCE_SOURCES = {"user", "environment", "tool", "file", "prior_interaction", "unknown"}
GOAL_STATUSES = {"explicit", "inferred", "under_specified"}
CONSTRAINT_TYPES = {
    "output_format",
    "tool_condition",
    "environment_condition",
    "action_condition",
    "success_criterion",
    "resource_limit",
    "safety_condition",
    "user_preference",
    "other",
}
_ATTACHMENT_HASH_CACHE: dict[tuple[str, int, int], str] = {}


@dataclass
class TaskInterfaceConfig:
    mode: str = "auto"
    structuring_template_path: str = str(DEFAULT_STRUCTURING_TEMPLATE)
    cache_path: str | None = None
    max_new_tokens: int = 512
    invalid_output_retries: int = 1
    retry_max_new_tokens: int = 512
    fallback_to_rules_on_invalid: bool = False
    long_context_rule_threshold_chars: int = 0
    visible_context_max_chars: int = 0
    visible_context_max_items: int = 0

    def to_dict(self) -> dict[str, object]:
        return asdict(self)


class TaskInterface:
    """Build the minimal HCL interface I_t=(X_t, G_hat_t, K_hat_t).

    Dataset identity, gold labels, paths, and evaluator metadata remain in the
    internal runtime envelope. Only ``hcl_interface`` is model-visible.
    """

    def __init__(
        self,
        config: TaskInterfaceConfig | None = None,
        *,
        model: Any | None = None,
        structuring_template_content: dict[str, Any] | None = None,
    ) -> None:
        self.config = config or TaskInterfaceConfig()
        if structuring_template_content is None:
            self.structuring_template = PromptTemplate.from_path(self.config.structuring_template_path)
        else:
            self.structuring_template = PromptTemplate.from_dict(
                structuring_template_content,
                template_id_fallback="candidate_task_interface_structuring",
            )
        self.model = model
        self._cache = _TaskInterfaceCache(self.config.cache_path)

    def build_taskinterfacechunk(self, example: dict[str, Any], stream_position: int) -> dict[str, Any]:
        mode = self.config.mode
        if mode not in {"rule", "llm", "auto"}:
            raise ValueError("TaskInterfaceConfig.mode must be one of: rule, llm, auto")
        visible_chars = self._raw_visible_input_chars(example)
        long_context_threshold = max(int(self.config.long_context_rule_threshold_chars), 0)
        if mode != "rule" and long_context_threshold and visible_chars > long_context_threshold:
            progress(
                f"task interface bypass mode=rule_long_context "
                f"task_id={example.get('task_id', '')} visible_chars={visible_chars} "
                f"threshold={long_context_threshold}"
            )
            return self._build_runtime_envelope(
                example,
                stream_position,
                hcl_interface=self._rule_hcl_interface(example),
                interface_mode="rule_long_context",
            )
        if mode == "llm":
            return self._build_chunk_with_llm(example, stream_position)
        if mode == "auto" and self.model is not None:
            return self._try_build_chunk_with_llm(example, stream_position)
        return self._build_chunk_with_rules(example, stream_position)

    def uses_llm_structuring(self, example: dict[str, Any]) -> bool:
        """Return whether this example can exercise the learnable structuring artifact."""
        mode = self.config.mode
        if mode == "rule":
            return False
        long_context_threshold = max(int(self.config.long_context_rule_threshold_chars), 0)
        if long_context_threshold and self._raw_visible_input_chars(example) > long_context_threshold:
            return False
        return mode == "llm" or (mode == "auto" and self.model is not None)

    def _build_chunk_with_rules(self, example: dict[str, Any], stream_position: int) -> dict[str, Any]:
        return self._build_runtime_envelope(
            example,
            stream_position,
            hcl_interface=self._rule_hcl_interface(example),
            interface_mode="rule",
        )

    def _try_build_chunk_with_llm(self, example: dict[str, Any], stream_position: int) -> dict[str, Any]:
        try:
            return self._build_chunk_with_llm(example, stream_position)
        except Exception as exc:
            progress(
                f"task interface fallback mode=rule task_id={example.get('task_id', '')} "
                f"reason={type(exc).__name__}:{exc}"
            )
            return self._build_runtime_envelope(
                example,
                stream_position,
                hcl_interface=self._rule_hcl_interface(example),
                interface_mode="rule_fallback",
            )

    def _build_chunk_with_llm(self, example: dict[str, Any], stream_position: int) -> dict[str, Any]:
        if self.model is None:
            raise RuntimeError("TaskInterface mode llm requires a model.")
        cache_key = self._cache_key(example)
        cached = self._cache.get(cache_key)
        if cached is not None:
            assert_no_label_leakage(json.dumps(cached, ensure_ascii=False), phase="task_interface_cache")
            progress(
                f"task interface cache hit task_id={example.get('task_id', '')} "
                f"key={cache_key[:12]}"
            )
            return self._build_runtime_envelope(
                example,
                stream_position,
                hcl_interface=cached,
                interface_mode="llm_cache",
            )
        attempts = max(int(self.config.invalid_output_retries), 0) + 1
        diagnostics: list[str] = []
        hcl_interface: dict[str, Any] | None = None
        for attempt in range(attempts):
            is_retry = attempt > 0
            prompt = self._render_structuring_prompt(example, compact_retry=is_retry)
            token_limit = (
                self.config.retry_max_new_tokens if is_retry else self.config.max_new_tokens
            )
            raw_output = self.model.generate(
                prompt,
                state={
                    "task_interface_phase": "structure_task",
                    "task_interface_attempt": attempt + 1,
                    "internal_sample_id": example.get("task_id"),
                    "max_new_tokens": max(int(token_limit), 1),
                },
            )
            decoded, parse_error = _extract_json_with_diagnostics(raw_output)
            if parse_error is not None:
                diagnostics.append(f"attempt_{attempt + 1}:{parse_error}")
                continue
            try:
                hcl_interface = self._normalize_hcl_interface(decoded, example)
            except ValueError as exc:
                diagnostics.append(f"attempt_{attempt + 1}:schema_error:{exc}")
                continue
            break
        if hcl_interface is None:
            if self.config.fallback_to_rules_on_invalid:
                progress(
                    f"task interface fallback mode=rule task_id={example.get('task_id', '')} "
                    f"reason=invalid_llm_output attempts={attempts} "
                    f"diagnostics={'; '.join(diagnostics)}"
                )
                return self._build_runtime_envelope(
                    example,
                    stream_position,
                    hcl_interface=self._rule_hcl_interface(example),
                    interface_mode="rule_fallback",
                )
            raise ValueError(
                "Task Interface LLM output remained invalid after "
                f"{attempts} attempt(s): {'; '.join(diagnostics)}"
            )
        assert_no_label_leakage(
            json.dumps(hcl_interface, ensure_ascii=False),
            phase="task_interface_output",
        )
        self._cache.put(cache_key, hcl_interface)
        progress(
            f"task interface cache store task_id={example.get('task_id', '')} "
            f"key={cache_key[:12]}"
        )
        return self._build_runtime_envelope(
            example,
            stream_position,
            hcl_interface=hcl_interface,
            interface_mode="llm",
        )

    def _cache_key(self, example: dict[str, Any]) -> str:
        payload = {
            "schema": "hcl_task_interface_cache_v1",
            "visible_input": self._raw_visible_input(example),
            "attachments": _attachment_content_hashes(self._files(example)),
            "structuring_artifact": {
                "template_id": self.structuring_template.template_id,
                "description": self.structuring_template.description,
                "sections": self.structuring_template.sections,
            },
        }
        canonical = json.dumps(payload, ensure_ascii=False, sort_keys=True, separators=(",", ":"))
        return hashlib.sha256(canonical.encode("utf-8")).hexdigest()

    def _build_runtime_envelope(
        self,
        example: dict[str, Any],
        stream_position: int,
        *,
        hcl_interface: dict[str, Any],
        interface_mode: str,
    ) -> dict[str, Any]:
        question = str(example.get("question", ""))
        task_id = str(example.get("task_id") or f"internal_sample_{stream_position}")
        return {
            "hcl_interface": hcl_interface,
            "task_context": {
                "raw_question": question,
                "files": self._files(example),
                "capability_inputs": {
                    "text_candidates": _capability_text_candidates(example),
                },
            },
            "task_id": task_id,
            "question": question,
            "task_name": str(example.get("task_name", "")),
            "task_type": str(example.get("task_type", "")),
            "stream_position": stream_position,
            "metadata": {
                **dict(example.get("metadata", {})),
                "answer": example.get("answer"),
                "split": example.get("split", ""),
                "task_interface_mode": interface_mode,
            },
        }

    def _render_structuring_prompt(
        self,
        example: dict[str, Any],
        *,
        compact_retry: bool = False,
    ) -> str:
        prompt = self.structuring_template.render(
            {
                "raw_visible_input": json.dumps(
                    self._raw_visible_input(example),
                    ensure_ascii=False,
                    indent=2,
                )
            }
        )
        if compact_retry:
            prompt = "\n".join(
                (
                    prompt,
                    "[RETRY_AFTER_INVALID_OUTPUT]",
                    "The previous response was invalid or truncated. Return a much more compact object now.",
                    "Use one evidence item per raw visible observation, a summary of at most 12 words, "
                    "a goal of at most 20 words, and at most 4 genuine interaction constraints.",
                    "Do not explain, solve, repeat the schema, or enumerate problem facts as constraints.",
                    "Close every JSON string, array, and object.",
                )
            )
        assert_no_label_leakage(prompt, phase="task_interface_structuring")
        return prompt

    def _raw_visible_input(self, example: dict[str, Any]) -> dict[str, Any]:
        visible_context = self._bounded_visible_context(example)
        extra_example = dict(example)
        # visible_context has its own schema field; do not duplicate it as an
        # additional observation in structuring and cache-key payloads.
        extra_example["visible_context"] = ""
        return {
            "text": str(example.get("question", "")),
            "options": example.get("options", []),
            "visible_context": visible_context,
            "additional_observations": [
                {"modality": modality, "content": content, "source": source}
                for modality, content, source in _additional_visible_observations(extra_example)
            ],
            "attachments": [
                {
                    "modality": str(item.get("type", "file")),
                    "content": f"A {item.get('type', 'file')} is available as part of the current observation.",
                    "source": "file",
                }
                for item in self._files(example)
            ],
        }

    def _raw_visible_input_chars(self, example: dict[str, Any]) -> int:
        return len(
            json.dumps(
                self._raw_visible_input(example),
                ensure_ascii=False,
                separators=(",", ":"),
            )
        )

    def _rule_hcl_interface(self, example: dict[str, Any]) -> dict[str, Any]:
        bounded_example = dict(example)
        bounded_example["visible_context"] = self._bounded_visible_context(example)
        evidence = _evidence_from_example(bounded_example)
        goal = _recognizable_goal(str(example.get("question", "")), evidence)
        constraints = _recognizable_constraints(str(example.get("question", "")), evidence)
        return {
            "evidence": evidence,
            "recognizable_goal": goal,
            "recognizable_constraints": constraints,
        }

    def _bounded_visible_context(self, example: dict[str, Any]) -> object:
        return _prefilter_visible_context(
            example.get("visible_context", ""),
            question=str(example.get("question", "")),
            max_chars=max(int(self.config.visible_context_max_chars), 0),
            max_items=max(int(self.config.visible_context_max_items), 0),
        )

    def _normalize_hcl_interface(
        self,
        decoded: dict[str, Any],
        example: dict[str, Any],
    ) -> dict[str, Any]:
        required_keys = {"evidence", "recognizable_goal", "recognizable_constraints"}
        if set(decoded) != required_keys:
            raise ValueError(
                "Task Interface output must contain exactly evidence, recognizable_goal, "
                "and recognizable_constraints."
            )
        fallback = self._rule_hcl_interface(example)
        raw_evidence = decoded.get("evidence")
        evidence = _normalize_evidence(raw_evidence, fallback["evidence"])
        evidence = _preserve_observed_evidence(evidence, fallback["evidence"])
        valid_ids = {
            str(item.get("id", ""))
            for item in evidence.get("items", [])
            if isinstance(item, dict)
        }
        fallback_goal = _remap_supporting_evidence(
            fallback["recognizable_goal"],
            fallback["evidence"],
            evidence,
        )
        fallback_constraints = [
            _remap_supporting_evidence(item, fallback["evidence"], evidence)
            for item in fallback["recognizable_constraints"]
        ]
        goal = _normalize_goal(decoded.get("recognizable_goal"), fallback_goal, valid_ids)
        constraints = _normalize_constraints(
            decoded.get("recognizable_constraints"),
            fallback_constraints,
            valid_ids,
        )
        return {
            "evidence": evidence,
            "recognizable_goal": goal,
            "recognizable_constraints": constraints,
        }

    @staticmethod
    def to_model_visible(taskinterfacechunk: dict[str, Any]) -> dict[str, Any]:
        """Return exactly the minimal HCL interface, with no runtime metadata."""
        hcl_interface = taskinterfacechunk.get("hcl_interface")
        if not isinstance(hcl_interface, dict):
            return _empty_hcl_interface()
        return {
            "evidence": dict(hcl_interface.get("evidence", {})),
            "recognizable_goal": dict(hcl_interface.get("recognizable_goal", {})),
            "recognizable_constraints": [
                dict(item)
                for item in hcl_interface.get("recognizable_constraints", [])
                if isinstance(item, dict)
            ],
        }

    @staticmethod
    def visible_question(taskinterfacechunk: dict[str, Any]) -> str:
        visible = TaskInterface.to_model_visible(taskinterfacechunk)
        evidence = visible.get("evidence", {})
        items = evidence.get("items", []) if isinstance(evidence, dict) else []
        for item in items:
            if isinstance(item, dict) and item.get("modality") == "text":
                return str(item.get("content", ""))
        return ""

    def _files(self, example: dict[str, Any]) -> list[dict[str, str]]:
        files: list[dict[str, str]] = []
        image = example.get("image") or example.get("image_path")
        if image:
            files.append({"type": "image", "path": str(image)})
        file_path = example.get("file") or example.get("file_path")
        if file_path:
            files.append({"type": "document", "path": str(file_path)})
        return files


def _capability_text_candidates(example: dict[str, Any]) -> list[dict[str, str]]:
    """Preserve visible answer options as task-local matcher inputs."""
    options = example.get("options")
    candidates: list[dict[str, str]] = []
    if isinstance(options, dict):
        for key, value in options.items():
            text = str(value).strip()
            if text:
                candidates.append({"id": str(key), "text": text})
    elif isinstance(options, list):
        for index, value in enumerate(options):
            if isinstance(value, dict):
                candidate_id = str(value.get("id") or value.get("label") or index)
                text = str(value.get("text") or value.get("content") or value.get("value") or "").strip()
            else:
                candidate_id = str(index)
                text = str(value).strip()
            if text:
                candidates.append({"id": candidate_id, "text": text})
    # COCO's public category ontology is part of the task definition, not a
    # sample label.  Supplying it makes image/text capability matching usable
    # without exposing which categories or boxes are present in this example.
    if not candidates and str(example.get("task_type", "")).lower() == "coco_detection":
        categories = (
            "person", "bicycle", "car", "motorcycle", "airplane", "bus", "train", "truck",
            "boat", "traffic light", "fire hydrant", "stop sign", "parking meter", "bench",
            "bird", "cat", "dog", "horse", "sheep", "cow", "elephant", "bear", "zebra",
            "giraffe", "backpack", "umbrella", "handbag", "tie", "suitcase", "frisbee",
            "skis", "snowboard", "sports ball", "kite", "baseball bat", "baseball glove",
            "skateboard", "surfboard", "tennis racket", "bottle", "wine glass", "cup", "fork",
            "knife", "spoon", "bowl", "banana", "apple", "sandwich", "orange", "broccoli",
            "carrot", "hot dog", "pizza", "donut", "cake", "chair", "couch", "potted plant",
            "bed", "dining table", "toilet", "tv", "laptop", "mouse", "remote", "keyboard",
            "cell phone", "microwave", "oven", "toaster", "sink", "refrigerator", "book",
            "clock", "vase", "scissors", "teddy bear", "hair drier", "toothbrush",
        )
        candidates = [{"id": category, "text": category} for category in categories]
    return candidates


class _TaskInterfaceCache(JsonObjectCache):
    """Append-only cache of successful, normalized Task Interface outputs."""

    def __init__(self, path: str | None) -> None:
        super().__init__(path, value_field="hcl_interface")


def _attachment_content_hashes(files: list[dict[str, str]]) -> list[dict[str, str]]:
    fingerprints: list[dict[str, str]] = []
    for item in files:
        path = Path(str(item.get("path", "")))
        digest = hashlib.sha256()
        status = "unavailable"
        try:
            stat = path.stat()
            memo_key = (str(path.resolve()), stat.st_mtime_ns, stat.st_size)
            status = _ATTACHMENT_HASH_CACHE.get(memo_key, "")
            if not status:
                with path.open("rb") as f:
                    for block in iter(lambda: f.read(1024 * 1024), b""):
                        digest.update(block)
                status = digest.hexdigest()
                _ATTACHMENT_HASH_CACHE[memo_key] = status
        except OSError:
            pass
        fingerprints.append({"type": str(item.get("type", "file")), "content_sha256": status})
    return fingerprints


def infer_output_form(question: str) -> dict[str, Any]:
    """Infer a visible output constraint; this is not part of the interface schema."""
    text = str(question)
    option_letters: list[str] = []
    for match in re.finditer(r"(?:^|\n)\s*\(?([A-Ea-e])\)?\s*[\.:\)]\s+", text):
        letter = match.group(1).upper()
        if letter not in option_letters:
            option_letters.append(letter)
    lower = text.lower()
    if "json" in lower and "bbox" in lower:
        coordinate_text = (
            " using the 0-1000 image coordinate system"
            if "0-1000" in lower or "0 to 1000" in lower
            else ""
        )
        return {
            "kind": "json_bbox" if "object" not in lower and "label" not in lower else "json_objects",
            "valid_outputs": [],
            "constraint": (
                "The response should be valid JSON only, using bbox coordinates in "
                f"[x1, y1, x2, y2] format{coordinate_text}."
            ),
        }
    if "caption" in lower:
        return {
            "kind": "caption",
            "valid_outputs": [],
            "constraint": "The response should be one short natural-language image caption, without explanation.",
        }
    explicitly_requests_option = "option letter" in lower or "选项字母" in lower
    if len(option_letters) >= 2 or explicitly_requests_option:
        if len(option_letters) < 2:
            example_match = re.search(
                r"(?:e\.g\.|例如)[,,:]?\s*([A-E](?:\s*[,,/]\s*[A-E]){1,4})",
                text,
                flags=re.IGNORECASE,
            )
            if example_match:
                option_letters = re.findall(r"[A-E]", example_match.group(1).upper())
        if len(option_letters) < 2:
            option_letters = ["A", "B", "C", "D"]
        return {
            "kind": "single_option_label",
            "valid_outputs": option_letters,
            "constraint": f"The response should be exactly one visible option label from {option_letters}, without explanation.",
        }
    if "single word or phrase" in lower or "short answer" in lower:
        return {
            "kind": "short_text",
            "valid_outputs": [],
            "constraint": "The response should contain only the requested short word, phrase, or value, without explanation.",
        }
    numeric_cues = ("how many", "what number", "calculate", "compute", "find the value", "多少", "几个")
    if any(cue in lower for cue in numeric_cues):
        return {
            "kind": "numeric",
            "valid_outputs": [],
            "constraint": "The response should contain only the final plain numeric value, without explanation.",
        }
    return {"kind": "unspecified", "valid_outputs": [], "constraint": ""}


def _evidence_from_example(example: dict[str, Any]) -> dict[str, Any]:
    items: list[dict[str, str]] = []
    question = str(example.get("question", "")).strip()
    if question:
        items.append({"id": "x1", "modality": "text", "content": question, "source": "user"})
    for modality, content, source in _additional_visible_observations(example):
        items.append(
            {
                "id": f"x{len(items) + 1}",
                "modality": modality,
                "content": content,
                "source": source,
            }
        )
    if example.get("image") or example.get("image_path"):
        items.append(
            {
                "id": f"x{len(items) + 1}",
                "modality": "image",
                "content": "An image is available as part of the current observation.",
                "source": "file",
            }
        )
    if example.get("file") or example.get("file_path"):
        items.append(
            {
                "id": f"x{len(items) + 1}",
                "modality": "document",
                "content": "A document is available as part of the current observation.",
                "source": "file",
            }
        )
    modalities = [item["modality"] for item in items]
    summary = "No visible evidence is available."
    if modalities:
        summary = f"The current observation contains visible evidence with modalities: {', '.join(modalities)}."
    return {"items": items, "summary": summary}


def _additional_visible_observations(example: dict[str, Any]) -> list[tuple[str, str, str]]:
    observations: list[tuple[str, str, str]] = []
    options = example.get("options")
    if options not in (None, [], {}):
        observations.append(("text", f"Visible answer options: {_visible_value(options)}", "user"))
    visible_context = example.get("visible_context")
    if visible_context not in (None, "", [], {}):
        observations.append(("environment_observation", _visible_value(visible_context), "environment"))
    for key, modality, source in (
        ("tool_output", "tool_output", "tool"),
        ("environment_observation", "environment_observation", "environment"),
        ("user_feedback", "user_feedback", "user"),
        ("action_trace", "action_trace", "prior_interaction"),
    ):
        value = example.get(key)
        if value is not None and str(value).strip():
            observations.append((modality, str(value), source))
    return observations


def _visible_value(value: object) -> str:
    if isinstance(value, (dict, list)):
        return json.dumps(value, ensure_ascii=False, sort_keys=True)
    return str(value)


def _prefilter_visible_context(
    value: object,
    *,
    question: str,
    max_chars: int,
    max_items: int,
) -> object:
    """Select a bounded, query-connected subset of long paragraph lists."""
    if not isinstance(value, list) or (max_chars <= 0 and max_items <= 0):
        return value
    serialized = json.dumps(value, ensure_ascii=False, sort_keys=True)
    item_limit = max_items if max_items > 0 else len(value)
    if len(serialized) <= max_chars or max_chars <= 0:
        return value[:item_limit]
    candidates = [
        {
            "index": index,
            "item": item,
            "text": _visible_value(item),
            "tokens": _context_tokens(_visible_value(item)),
            "title_tokens": _context_title_tokens(item),
        }
        for index, item in enumerate(value)
    ]
    question_tokens = _context_tokens(question)
    selected: list[dict[str, Any]] = []
    selected_chars = 2
    while candidates and len(selected) < item_limit:
        selected_body_tokens = set().union(*(item["tokens"] for item in selected)) if selected else set()

        def score(candidate: dict[str, Any]) -> tuple[float, float, int]:
            query_overlap = _token_overlap(question_tokens, candidate["tokens"])
            bridge_overlap = _token_overlap(candidate["title_tokens"], selected_body_tokens)
            return query_overlap + 1.5 * bridge_overlap, bridge_overlap, -int(candidate["index"])

        best = max(candidates, key=score)
        item_chars = len(json.dumps(best["item"], ensure_ascii=False, sort_keys=True)) + 1
        candidates.remove(best)
        if selected and selected_chars + item_chars > max_chars:
            continue
        selected.append(best)
        selected_chars += item_chars
        if selected_chars >= max_chars:
            break
    return [item["item"] for item in selected]


def _context_tokens(text: str) -> set[str]:
    stopwords = {
        "about",
        "after",
        "answer",
        "before",
        "context",
        "did",
        "does",
        "from",
        "only",
        "provided",
        "return",
        "shortest",
        "that",
        "the",
        "this",
        "using",
        "what",
        "when",
        "where",
        "which",
        "who",
        "with",
    }
    return {
        token
        for token in re.findall(r"[a-z0-9]+|[\u4e00-\u9fff]", str(text).lower())
        if len(token) > 1 and token not in stopwords
    }


def _context_title_tokens(item: object) -> set[str]:
    if not isinstance(item, dict):
        return set()
    title = item.get("title") or item.get("name") or ""
    return _context_tokens(str(title))


def _token_overlap(left: set[str], right: set[str]) -> float:
    if not left or not right:
        return 0.0
    return len(left & right) / max(len(left), 1)


def _recognizable_goal(question: str, evidence: dict[str, Any]) -> dict[str, Any]:
    text = str(question).strip()
    text_id = _first_evidence_id(evidence, "text")
    if not text:
        return {
            "status": "under_specified",
            "content": "The current evidence does not provide a complete executable goal.",
            "supporting_evidence": [],
        }
    output_form = infer_output_form(text)
    if output_form["kind"] == "single_option_label":
        content = "Select the option supported by the visible evidence."
    elif output_form["kind"] in {"json_bbox", "json_objects"}:
        content = "Return the requested visual localization result in valid JSON."
    elif output_form["kind"] == "caption":
        content = "Write a concise caption grounded in the visible image."
    elif output_form["kind"] == "numeric":
        content = "Compute the requested final numeric value from the visible evidence."
    elif output_form["kind"] == "short_text":
        content = "Provide the short answer requested by the visible evidence."
    else:
        content = "Answer the request expressed in the visible text evidence."
    explicit_cues = ("?", "please", "answer", "calculate", "compute", "find", "what", "which", "how", "请", "求", "多少", "哪个")
    status = "explicit" if any(cue in text.lower() for cue in explicit_cues) else "inferred"
    return {"status": status, "content": content, "supporting_evidence": [text_id] if text_id else []}


def _recognizable_constraints(question: str, evidence: dict[str, Any]) -> list[dict[str, Any]]:
    output_form = infer_output_form(question)
    if output_form["kind"] == "unspecified":
        return []
    text_id = _first_evidence_id(evidence, "text")
    return [
        {
            "id": "k1",
            "content": output_form["constraint"],
            "constraint_type": "output_format",
            "supporting_evidence": [text_id] if text_id else [],
        }
    ]


def _normalize_evidence(raw: Any, fallback: dict[str, Any]) -> dict[str, Any]:
    if not isinstance(raw, dict) or not isinstance(raw.get("items"), list):
        return {"items": [dict(item) for item in fallback["items"]], "summary": fallback["summary"]}
    items: list[dict[str, str]] = []
    used_ids: set[str] = set()
    for index, item in enumerate(raw["items"]):
        if not isinstance(item, dict) or not str(item.get("content", "")).strip():
            continue
        item_id = str(item.get("id") or f"x{index + 1}")
        if item_id in used_ids:
            item_id = f"x{len(items) + 1}"
        used_ids.add(item_id)
        modality = str(item.get("modality", "other"))
        source = str(item.get("source", "unknown"))
        items.append(
            {
                "id": item_id,
                "modality": modality if modality in EVIDENCE_MODALITIES else "other",
                "content": str(item.get("content", "")),
                "source": source if source in EVIDENCE_SOURCES else "unknown",
            }
        )
    return {"items": items, "summary": str(raw.get("summary") or fallback["summary"])}


def _preserve_observed_evidence(evidence: dict[str, Any], fallback: dict[str, Any]) -> dict[str, Any]:
    items = [dict(item) for item in evidence.get("items", []) if isinstance(item, dict)]
    signatures = {(item.get("modality"), item.get("content")) for item in items}
    for observed in fallback.get("items", []):
        signature = (observed.get("modality"), observed.get("content"))
        if signature in signatures:
            continue
        item = dict(observed)
        item["id"] = _next_id("x", {str(current.get("id", "")) for current in items})
        items.append(item)
        signatures.add(signature)
    return {"items": items, "summary": str(evidence.get("summary") or fallback["summary"])}


def _normalize_goal(raw: Any, fallback: dict[str, Any], valid_ids: set[str]) -> dict[str, Any]:
    if not isinstance(raw, dict):
        return dict(fallback)
    status = str(raw.get("status", "under_specified"))
    if status not in GOAL_STATUSES:
        status = "under_specified"
    supporting = _valid_supporting_ids(raw.get("supporting_evidence"), valid_ids)
    content = str(raw.get("content", "")).strip()
    if not content:
        return dict(fallback)
    if status == "under_specified" and fallback.get("status") == "explicit":
        return dict(fallback)
    return {"status": status, "content": content, "supporting_evidence": supporting}


def _normalize_constraints(raw: Any, fallback: list[dict[str, Any]], valid_ids: set[str]) -> list[dict[str, Any]]:
    if not isinstance(raw, list):
        return [dict(item) for item in fallback]
    constraints: list[dict[str, Any]] = []
    used_ids: set[str] = set()
    for index, item in enumerate(raw):
        if not isinstance(item, dict) or not str(item.get("content", "")).strip():
            continue
        constraint_id = str(item.get("id") or f"k{index + 1}")
        if constraint_id in used_ids:
            constraint_id = _next_id("k", used_ids)
        used_ids.add(constraint_id)
        constraint_type = str(item.get("constraint_type", "other"))
        constraints.append(
            {
                "id": constraint_id,
                "content": str(item.get("content", "")),
                "constraint_type": constraint_type if constraint_type in CONSTRAINT_TYPES else "other",
                "supporting_evidence": _valid_supporting_ids(item.get("supporting_evidence"), valid_ids),
            }
        )
    existing = {
        (item.get("constraint_type"), str(item.get("content", "")).strip().lower())
        for item in constraints
    }
    for fallback_item in fallback:
        if fallback_item.get("constraint_type") == "output_format":
            constraints = [
                item
                for item in constraints
                if item.get("constraint_type") != "output_format"
            ]
            used_ids = {str(item.get("id", "")) for item in constraints}
            existing = {
                (item.get("constraint_type"), str(item.get("content", "")).strip().lower())
                for item in constraints
            }
        signature = (
            fallback_item.get("constraint_type"),
            str(fallback_item.get("content", "")).strip().lower(),
        )
        if signature in existing:
            continue
        preserved = dict(fallback_item)
        preserved["id"] = _next_id("k", used_ids)
        used_ids.add(str(preserved["id"]))
        constraints.append(preserved)
        existing.add(signature)
    return constraints


def _valid_supporting_ids(raw: Any, valid_ids: set[str]) -> list[str]:
    if not isinstance(raw, list):
        return []
    return [str(item) for item in raw if str(item) in valid_ids]


def _remap_supporting_evidence(
    item: dict[str, Any],
    source_evidence: dict[str, Any],
    target_evidence: dict[str, Any],
) -> dict[str, Any]:
    source_by_id = {
        str(source.get("id", "")): (source.get("modality"), source.get("content"))
        for source in source_evidence.get("items", [])
        if isinstance(source, dict)
    }
    target_by_signature = {
        (target.get("modality"), target.get("content")): str(target.get("id", ""))
        for target in target_evidence.get("items", [])
        if isinstance(target, dict)
    }
    remapped = dict(item)
    remapped["supporting_evidence"] = [
        target_by_signature[source_by_id[source_id]]
        for source_id in item.get("supporting_evidence", [])
        if source_id in source_by_id and source_by_id[source_id] in target_by_signature
    ]
    return remapped


def _first_evidence_id(evidence: dict[str, Any], modality: str) -> str:
    for item in evidence.get("items", []):
        if isinstance(item, dict) and item.get("modality") == modality:
            return str(item.get("id", ""))
    return ""


def _next_id(prefix: str, used_ids: set[str]) -> str:
    index = 1
    while f"{prefix}{index}" in used_ids:
        index += 1
    return f"{prefix}{index}"


def _empty_hcl_interface() -> dict[str, Any]:
    return {
        "evidence": {"items": [], "summary": "No visible evidence is available."},
        "recognizable_goal": {
            "status": "under_specified",
            "content": "The current evidence does not provide a complete executable goal.",
            "supporting_evidence": [],
        },
        "recognizable_constraints": [],
    }


def assert_no_label_leakage(prompt: str, *, phase: str) -> None:
    text = str(prompt)
    leaked: list[str] = []
    for key in FORBIDDEN_ASSERT_KEYS:
        # Runtime metadata is serialized by json.dumps as an unescaped quoted
        # object key (for example, `"split": "train"`).  Restrict the guard to
        # that representation so ordinary model-visible prose such as
        # `split: 12 and 8` and escaped JSON snippets inside evidence strings do
        # not trigger a false-positive leakage failure.
        key_pattern = rf"(?<!\\)[\"']{re.escape(key)}[\"']\s*:"
        if re.search(key_pattern, text, flags=re.IGNORECASE):
            leaked.append(key)
    for label in FORBIDDEN_TASK_LABELS:
        label_pattern = rf"(?<![a-z0-9_]){re.escape(label)}(?:[_-]\d+)?(?![a-z0-9_])"
        if re.search(label_pattern, text, flags=re.IGNORECASE):
            leaked.append(label)
    if leaked:
        raise ValueError(f"Label leakage in {phase} prompt: {leaked}")


def sanitize_label_leakage_for_visible_prompt(value: Any) -> Any:
    """Return a model-visible copy with internal labels and metadata removed."""
    if isinstance(value, dict):
        cleaned: dict[str, Any] = {}
        for raw_key, raw_child in value.items():
            key = str(raw_key)
            if key in FORBIDDEN_VISIBLE_KEYS or key in {"internal_source", "metadata", "_anchor_meta"}:
                continue
            cleaned[raw_key] = sanitize_label_leakage_for_visible_prompt(raw_child)
        return cleaned
    if isinstance(value, list):
        return [sanitize_label_leakage_for_visible_prompt(item) for item in value]
    if isinstance(value, tuple):
        return tuple(sanitize_label_leakage_for_visible_prompt(item) for item in value)
    if isinstance(value, str):
        text = value
        for label in FORBIDDEN_TASK_LABELS:
            label_pattern = rf"(?<![a-z0-9_]){re.escape(label)}(?:[_-]\d+)?(?![a-z0-9_])"
            text = re.sub(label_pattern, "benchmark", text, flags=re.IGNORECASE)
        return text
    return value


def _extract_json_with_diagnostics(text: str) -> tuple[dict[str, Any], str | None]:
    cleaned = str(text).strip()
    fenced = re.fullmatch(r"```(?:json)?\s*(.*?)\s*```", cleaned, flags=re.IGNORECASE | re.DOTALL)
    if fenced:
        cleaned = fenced.group(1).strip()
    try:
        data = json.loads(cleaned)
    except json.JSONDecodeError as direct_error:
        start = cleaned.find("{")
        if start < 0:
            return {}, "json_decode_error:no_object_start"
        try:
            data, _ = json.JSONDecoder().raw_decode(cleaned[start:])
        except json.JSONDecodeError as extracted_error:
            likely_truncated = _looks_truncated(cleaned)
            kind = "likely_truncated_json" if likely_truncated else "json_decode_error"
            return {}, (
                f"{kind}:{extracted_error.msg} at line "
                f"{extracted_error.lineno} column {extracted_error.colno}; "
                f"direct_error={direct_error.msg}"
            )
    if not isinstance(data, dict):
        return {}, f"top_level_not_object:{type(data).__name__}"
    return data, None


def _looks_truncated(text: str) -> bool:
    stripped = str(text).rstrip()
    if not stripped:
        return True
    return not stripped.endswith("}") or stripped.count("{") > stripped.count("}")