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
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
"""Verified post-blind expert comparison with no model or mutation capability."""

from __future__ import annotations

import hashlib
import json
import logging
from collections import Counter, defaultdict
from typing import Protocol

from gcmd_classifier.datasets.comparison_models import (
    ComparisonCategoryCounts,
    ComparisonComponentHashes,
    ComparisonRelationship,
    ComparisonRunStatus,
    DatasetComparisonItem,
    DatasetComparisonResult,
    ExpertOnlyAssignment,
    HierarchyRelationshipKind,
    HumanReviewRecord,
    PersistedComparisonOutcome,
    ProposalType,
)
from gcmd_classifier.datasets.errors import DatasetComparisonError
from gcmd_classifier.datasets.expert_resolution import resolve_expert_assignments
from gcmd_classifier.datasets.models import (
    ComparisonCategory,
    DatasetClassificationOutcome,
    DatasetClassificationResult,
    DatasetReviewStatus,
)
from gcmd_classifier.datasets.runtime_models import (
    ArtifactType,
    ExpertSourceBinding,
    SealedExpertArtifactEnvelope,
    VerifiedPersistedBlindResultReference,
)
from gcmd_classifier.datasets.runtime_persistence import DatasetArtifactStore
from gcmd_classifier.logging_config import get_logger, log_event
from gcmd_classifier.vocabulary.index import VocabularyIndex

COMPARISON_POLICY = "dataset-comparison-policy-v1"
RESOLUTION_POLICY = "expert-resolution-policy-v1"
NORMALIZATION_POLICY = "case-whitespace-normalization-v1"
REVIEW_POLICY = "dataset-initial-review-v1"


class ExpertUnsealer(Protocol):
    def unseal(self, persisted_bytes: bytes) -> SealedExpertArtifactEnvelope:
        """Open typed expert bytes only after prerequisite verification."""


class TypedExpertUnsealer:
    def unseal(self, persisted_bytes: bytes) -> SealedExpertArtifactEnvelope:
        return SealedExpertArtifactEnvelope.model_validate_json(persisted_bytes)


def _json_default(value: object) -> object:
    if hasattr(value, "model_dump"):
        return value.model_dump(mode="json")
    if hasattr(value, "value"):
        return value.value
    raise TypeError(f"Unsupported canonical comparison value: {type(value).__name__}")


def _canonical(value: object) -> bytes:
    return json.dumps(
        value,
        sort_keys=True,
        separators=(",", ":"),
        ensure_ascii=False,
        default=_json_default,
    ).encode()


def _hash(value: object) -> str:
    return hashlib.sha256(_canonical(value)).hexdigest()


def _distance(vocabulary: VocabularyIndex, ancestor: str, descendant: str) -> int:
    ancestors = vocabulary.ancestors_of(descendant)
    return ancestors.index(ancestor) + 1


def _verified_inputs(
    *,
    store: DatasetArtifactStore,
    session_id: str,
    verified: VerifiedPersistedBlindResultReference,
    vocabulary: VocabularyIndex,
    unsealer: ExpertUnsealer,
) -> tuple[DatasetClassificationResult, SealedExpertArtifactEnvelope]:
    if not isinstance(verified, VerifiedPersistedBlindResultReference):
        raise DatasetComparisonError(
            "COMPARISON_PREREQUISITE_INVALID", "A verified blind reference is required."
        )
    run = verified.run
    blind_bytes = store.load_verified(run, session_id, verified.artifact)
    cmr_bytes = store.load_verified(run, session_id, verified.cmr_source_artifact)
    sealed_bytes = store.load_verified(run, session_id, verified.sealed_expert_artifact)
    binding_bytes = store.load_verified(run, session_id, verified.expert_source_binding_artifact)
    try:
        blind = DatasetClassificationResult.model_validate_json(blind_bytes)
        binding = ExpertSourceBinding.model_validate_json(binding_bytes)
    except Exception as exc:
        raise DatasetComparisonError(
            "COMPARISON_PREREQUISITE_INVALID", "Persisted blind or binding schema is invalid."
        ) from exc
    if (
        verified.blind_result_sha256 != verified.artifact.sha256
        or verified.cmr_source_sha256 != verified.cmr_source_artifact.sha256
        or verified.sealed_expert_artifact_sha256 != verified.sealed_expert_artifact.sha256
        or verified.expert_source_binding_sha256 != verified.expert_source_binding_artifact.sha256
        or blind.identity != verified.identity
        or blind.dataset_key != verified.identity.concept_id
        or blind.evidence_packet_sha256 != verified.evidence_packet_sha256
        or blind.processing_status.value != "completed"
        or blind.classification_outcome is None
        or blind.classification_outcome.value != verified.completed_blind_status
        or binding.identity != verified.identity
        or binding.run != verified.run
        or binding.cmr_source_sha256 != verified.cmr_source_sha256
        or binding.sealed_expert_artifact_sha256 != verified.sealed_expert_artifact_sha256
        or binding.sealed_payload_sha256 != verified.sealed_payload_sha256
        or vocabulary.vocabulary_version != verified.vocabulary_hash
    ):
        raise DatasetComparisonError(
            "COMPARISON_PROVENANCE_INVALID",
            "Blind, expert, or hierarchy provenance does not agree.",
        )
    try:
        source = json.loads(cmr_bytes)
        item = source["items"][0]
        exact = (
            item["meta"]["concept-id"],
            item["meta"]["native-id"],
            item["umm"]["ShortName"],
            item["umm"]["Version"],
            item["meta"]["revision-id"],
        )
    except (ValueError, KeyError, IndexError, TypeError) as exc:
        raise DatasetComparisonError(
            "EXPERT_SOURCE_INVALID", "Persisted CMR source is invalid."
        ) from exc
    identity = verified.identity
    if exact != (
        identity.concept_id,
        identity.native_id,
        identity.short_name,
        identity.version,
        identity.cmr_revision_id,
    ):
        raise DatasetComparisonError("EXPERT_SOURCE_IDENTITY_MISMATCH", "CMR identity differs.")
    # Authorization occurs only after every non-expert prerequisite above succeeds.
    try:
        envelope = unsealer.unseal(sealed_bytes)
    except Exception as exc:
        raise DatasetComparisonError(
            "EXPERT_UNSEAL_FAILED", "Sealed expert artifact is invalid."
        ) from exc
    if (
        envelope.identity != identity
        or envelope.cmr_source_sha256 != verified.cmr_source_sha256
        or envelope.sealed_payload_sha256 != verified.sealed_payload_sha256
        or tuple(item["umm"].get("ScienceKeywords", ())) != envelope.sealed_payload.science_keywords
    ):
        raise DatasetComparisonError(
            "EXPERT_SOURCE_BINDING_INVALID", "Unsealed expert source differs."
        )
    return blind, envelope


def _relationships(blind_uuid, experts, vocabulary):
    by_uuid: dict[str, list[int]] = defaultdict(list)
    for item in experts:
        by_uuid[item.UUID].append(item.source_position)
    relationships = []
    for expert_uuid in sorted(by_uuid):
        positions = tuple(by_uuid[expert_uuid])
        if blind_uuid == expert_uuid:
            kind = HierarchyRelationshipKind.EXACT
            distance = 0
            path = vocabulary.get(blind_uuid).path_components
        elif vocabulary.is_descendant(blind_uuid, expert_uuid):
            kind = HierarchyRelationshipKind.BLIND_DESCENDANT
            distance = _distance(vocabulary, expert_uuid, blind_uuid)
            path = vocabulary.get(blind_uuid).path_components
        elif vocabulary.is_descendant(expert_uuid, blind_uuid):
            kind = HierarchyRelationshipKind.EXPERT_DESCENDANT
            distance = _distance(vocabulary, blind_uuid, expert_uuid)
            path = vocabulary.get(expert_uuid).path_components
        else:
            kind = HierarchyRelationshipKind.INDEPENDENT
            distance = None
            path = ()
        relationships.append(
            ComparisonRelationship(
                relationship=kind,
                expert_UUID=expert_uuid,
                expert_source_positions=positions,
                distance=distance,
                authoritative_path=path,
            )
        )
    return tuple(relationships)


def _item(index, blind, experts, unresolved, vocabulary):
    relationships = list(_relationships(blind.UUID, experts, vocabulary))
    if unresolved:
        relationships.extend(
            ComparisonRelationship(
                relationship=HierarchyRelationshipKind.UNRESOLVED,
                expert_source_positions=(item.source_position,),
            )
            for item in unresolved
        )
        category = ComparisonCategory.REVIEW_REQUIRED
        primary = HierarchyRelationshipKind.UNRESOLVED
        proposal = ProposalType.NONE
        review = True
        reasons = tuple(sorted({item.resolution_status.value for item in unresolved}))
        rationale = "Unresolved expert assignments prevent safe deterministic categorization."
    else:
        kinds = {item.relationship for item in relationships}
        if HierarchyRelationshipKind.EXACT in kinds:
            category = ComparisonCategory.ALREADY_ASSIGNED
            primary = HierarchyRelationshipKind.EXACT
            rationale = "Blind terminal UUID exactly matches an existing expert assignment."
        elif HierarchyRelationshipKind.EXPERT_DESCENDANT in kinds:
            category = ComparisonCategory.REDUNDANT_RESULT
            primary = HierarchyRelationshipKind.EXPERT_DESCENDANT
            rationale = "A more specific expert descendant already exists; no change is proposed."
        elif HierarchyRelationshipKind.BLIND_DESCENDANT in kinds:
            category = ComparisonCategory.PROPOSED_REFINEMENT
            primary = HierarchyRelationshipKind.BLIND_DESCENDANT
            rationale = (
                "Blind assignment is a strict descendant of an existing broader expert assignment."
            )
        else:
            category = ComparisonCategory.PROPOSED_ADDITION
            primary = HierarchyRelationshipKind.INDEPENDENT
            rationale = "Blind assignment is independent of every resolved expert assignment."
        proposal = {
            ComparisonCategory.PROPOSED_ADDITION: ProposalType.ADDITION,
            ComparisonCategory.PROPOSED_REFINEMENT: ProposalType.REFINEMENT,
        }.get(category, ProposalType.NONE)
        review = category in {
            ComparisonCategory.PROPOSED_ADDITION,
            ComparisonCategory.PROPOSED_REFINEMENT,
        }
        reasons = (category.value,) if review else ()
    related = tuple(
        sorted({position for item in relationships for position in item.expert_source_positions})
    )
    material = {
        "blind_index": index,
        "blind_uuid": blind.UUID,
        "category": category.value,
        "relationships": [item.model_dump(mode="json") for item in relationships],
    }
    return DatasetComparisonItem(
        comparison_item_id=_hash(material),
        blind_index=index,
        blind_classification=blind,
        category=category.value,
        primary_relationship=primary,
        relationships=tuple(relationships),
        related_expert_source_positions=related,
        proposal_type=proposal,
        rationale=rationale,
        review_required=review,
        review_reason_codes=reasons,
    )


def _comparison_hash_material(result: DatasetComparisonResult) -> dict:
    return result.model_dump(mode="json", exclude={"comparison_sha256", "comparison_artifact"})


def compare_verified_dataset(
    *,
    store: DatasetArtifactStore,
    session_id: str,
    verified: VerifiedPersistedBlindResultReference,
    vocabulary: VocabularyIndex,
    unsealer: ExpertUnsealer | None = None,
    logger: logging.Logger | None = None,
) -> PersistedComparisonOutcome:
    """Resolve, compare, persist, and verify advisory records with zero model calls."""
    blind, envelope = _verified_inputs(
        store=store,
        session_id=session_id,
        verified=verified,
        vocabulary=vocabulary,
        unsealer=unsealer or TypedExpertUnsealer(),
    )
    resolved, unresolved, duplicates = resolve_expert_assignments(
        envelope.sealed_payload.science_keywords, vocabulary
    )
    items = tuple(
        _item(index, item, resolved, unresolved, vocabulary)
        for index, item in enumerate(blind.classifications)
    )
    counts = Counter(item.category for item in items)
    category_counts = ComparisonCategoryCounts(
        **{name: counts[name] for name in ComparisonCategoryCounts.model_fields}
    )
    related_uuids = {
        relationship.expert_UUID
        for item in items
        for relationship in item.relationships
        if relationship.expert_UUID
        and relationship.relationship is not HierarchyRelationshipKind.INDEPENDENT
    }
    positions: dict[str, list[int]] = defaultdict(list)
    paths = {}
    for item in resolved:
        positions[item.UUID].append(item.source_position)
        paths[item.UUID] = item.canonical_path
    expert_only = tuple(
        ExpertOnlyAssignment(
            UUID=uuid, source_positions=tuple(positions[uuid]), canonical_path=paths[uuid]
        )
        for uuid in sorted(positions)
        if uuid not in related_uuids
    )
    policy_identity = {
        "comparison": COMPARISON_POLICY,
        "resolution": RESOLUTION_POLICY,
        "normalization": NORMALIZATION_POLICY,
        "review": REVIEW_POLICY,
        "schema": "dataset-comparison-v2",
        "vocabulary": vocabulary.vocabulary_version,
    }
    component_hashes = ComparisonComponentHashes(
        blind_input_sha256=verified.blind_result_sha256,
        expert_input_sha256=verified.sealed_payload_sha256,
        resolved_experts_sha256=_hash(
            {"resolved": resolved, "unresolved": unresolved, "duplicates": duplicates}
        ),
        comparison_items_sha256=_hash(items),
        policy_identity_sha256=_hash(policy_identity),
    )
    comparison_id = _hash(
        {
            "blind": verified.blind_result_sha256,
            "expert": verified.sealed_payload_sha256,
            "vocabulary": vocabulary.vocabulary_version,
            **policy_identity,
        }
    )
    review_reasons = tuple(
        sorted(
            {reason for item in items for reason in item.review_reason_codes}
            | {item.resolution_status.value for item in unresolved}
        )
    )
    draft = DatasetComparisonResult(
        comparison_id=comparison_id,
        comparison_sha256="0" * 64,
        comparison_policy_version=COMPARISON_POLICY,
        expert_resolution_policy_version=RESOLUTION_POLICY,
        normalization_policy_version=NORMALIZATION_POLICY,
        review_policy_version=REVIEW_POLICY,
        run_id=verified.run.run_id,
        identity=verified.identity,
        verified_blind_result=verified,
        blind_result_sha256=verified.blind_result_sha256,
        evidence_packet_sha256=verified.evidence_packet_sha256,
        expert_source_artifact=verified.cmr_source_artifact,
        expert_source_sha256=verified.cmr_source_sha256,
        sealed_expert_artifact=verified.sealed_expert_artifact,
        sealed_payload_sha256=verified.sealed_payload_sha256,
        vocabulary_hash=vocabulary.vocabulary_version,
        comparison_status=(
            ComparisonRunStatus.BLIND_NOT_CLASSIFIED
            if blind.classification_outcome is DatasetClassificationOutcome.NOT_CLASSIFIED
            else ComparisonRunStatus.BLIND_CLASSIFIED
        ),
        resolved_expert_assignments=resolved,
        unresolved_expert_assignments=unresolved,
        expert_duplicate_groups=duplicates,
        comparison_items=items,
        expert_only_assignments=expert_only,
        category_counts=category_counts,
        review_status=(
            DatasetReviewStatus.PENDING if review_reasons else DatasetReviewStatus.NOT_REQUIRED
        ),
        review_required_reasons=review_reasons,
        component_hashes=component_hashes,
    )
    comparison = draft.model_copy(
        update={"comparison_sha256": _hash(_comparison_hash_material(draft))}
    )
    reference = store.persist_json(
        run=verified.run,
        session_id=session_id,
        relative_name="comparison.json",
        value=comparison,
        artifact_type=ArtifactType.COMPARISON,
        schema_name="dataset-comparison-v2",
        upstream_sha256=(verified.blind_result_sha256, verified.sealed_payload_sha256),
        validate=lambda data: DatasetComparisonResult.model_validate_json(data),
    )
    persisted = DatasetComparisonResult.model_validate_json(
        store.load_verified(verified.run, session_id, reference)
    )
    if persisted.comparison_sha256 != _hash(_comparison_hash_material(persisted)):
        raise DatasetComparisonError(
            "COMPARISON_HASH_INVALID", "Persisted comparison hash is invalid."
        )
    item_review_records = tuple(
        HumanReviewRecord(
            review_record_id=_hash(
                {"comparison": comparison.comparison_sha256, "item": item.comparison_item_id}
            ),
            comparison_sha256=comparison.comparison_sha256,
            comparison_item_id=item.comparison_item_id,
            identity=comparison.identity,
            blind_classification=item.blind_classification,
            related_expert_source_positions=item.related_expert_source_positions,
            comparison_category=item.category,
            proposal_type=item.proposal_type,
            relationships=item.relationships,
            review_reason_codes=item.review_reason_codes,
            comparison_policy_version=COMPARISON_POLICY,
            review_policy_version=REVIEW_POLICY,
            blind_result_sha256=verified.blind_result_sha256,
            sealed_payload_sha256=verified.sealed_payload_sha256,
        )
        for item in items
        if item.review_required
    )
    unresolved_review_records = tuple(
        HumanReviewRecord(
            review_record_id=_hash(
                {
                    "comparison": comparison.comparison_sha256,
                    "unresolved_source_position": item.source_position,
                }
            ),
            comparison_sha256=comparison.comparison_sha256,
            comparison_item_id=_hash({"unresolved_source_position": item.source_position}),
            identity=comparison.identity,
            related_expert_source_positions=(item.source_position,),
            comparison_category=ComparisonCategory.REVIEW_REQUIRED,
            proposal_type=ProposalType.NONE,
            relationships=(
                ComparisonRelationship(
                    relationship=HierarchyRelationshipKind.UNRESOLVED,
                    expert_source_positions=(item.source_position,),
                ),
            ),
            review_reason_codes=(item.resolution_status.value,),
            comparison_policy_version=COMPARISON_POLICY,
            review_policy_version=REVIEW_POLICY,
            blind_result_sha256=verified.blind_result_sha256,
            sealed_payload_sha256=verified.sealed_payload_sha256,
        )
        for item in unresolved
    )
    review_records = (*item_review_records, *unresolved_review_records)
    review_refs = tuple(
        store.persist_json(
            run=verified.run,
            session_id=session_id,
            relative_name=f"reviews/{record.review_record_id}.json",
            value=record,
            artifact_type=ArtifactType.HUMAN_REVIEW,
            schema_name="dataset-human-review-v1",
            upstream_sha256=(comparison.comparison_sha256,),
            validate=lambda data: HumanReviewRecord.model_validate_json(data),
        )
        for record in review_records
    )
    log_event(
        logger or get_logger("gcmd_classifier.datasets.comparison"),
        "dataset_comparison_completed",
        run_id=verified.run.run_id,
        concept_id=verified.identity.concept_id,
        blind_result_sha256=verified.blind_result_sha256,
        expert_source_sha256=verified.cmr_source_sha256,
        hierarchy_hash=vocabulary.vocabulary_version,
        comparison_sha256=comparison.comparison_sha256,
        resolved_count=len(resolved),
        unresolved_count=len(unresolved),
        duplicate_group_count=len(duplicates),
        category_counts=category_counts.model_dump(),
        review_record_count=len(review_records),
        comparison_reference=reference.relative_path,
    )
    return PersistedComparisonOutcome(
        comparison=comparison,
        comparison_artifact=reference,
        review_records=review_records,
        review_artifacts=review_refs,
    )