File size: 11,802 Bytes
40b1357
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2f95512
40b1357
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2f95512
 
 
 
 
 
 
 
40b1357
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2f95512
 
 
 
 
 
40b1357
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2f95512
 
40b1357
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from __future__ import annotations

import hashlib
import json
from collections.abc import Callable
from enum import Enum
from pathlib import Path
from typing import Any, Literal

from pydantic import Field, model_validator

from .acoustic_dynamics import derive_acoustic_dynamics
from .adapters import build_signal_bundle
from .decisions import DECISION_POLICY_VERSION, build_call_decision
from .domain_profiles import (
    DomainProfile,
    ProfileSelection,
    load_general_service_profile,
    resolve_domain_plan,
)
from .findings import (
    RequirementAssessmentBatch,
    derive_findings,
)
from .presentation import CallEvaluationView, project_call_evaluation
from .schemas import (
    CallDecision,
    ContractModel,
    DecisionStatus,
    SourceProvenance,
)
from .semantic_assessment import assess_requirements
from .validation import validate_decision_references

RUNTIME_VERSION = "0.1.0"
HERE = Path(__file__).resolve().parent
PROFILE_PATH = HERE / "profiles" / "banking_v1.json"
SELECTIONS_PATH = HERE / "profiles" / "banking_call_selections.json"


class ShadowRunStatus(str, Enum):
    SUCCEEDED = "succeeded"
    UNSUPPORTED_DOMAIN = "unsupported_domain"
    PROFILE_SELECTION_UNAVAILABLE = "profile_selection_unavailable"
    FAILED = "failed"


class LegacyAttentionProxy(ContractModel):
    evaluator_version: str = Field(min_length=1)
    attention_required: bool
    basis: Literal["failed_compliance_or_escalation"]
    failed_compliance_count: int = Field(ge=0)
    escalation_tier: str = Field(min_length=1)


class DecisionComparison(ContractModel):
    comparable: bool
    reason: str = Field(min_length=1)
    attention_agreement: bool | None = None

    @model_validator(mode="after")
    def validate_agreement(self):
        if self.comparable != (self.attention_agreement is not None):
            raise ValueError(
                "attention agreement is present only when runs are comparable"
            )
        return self


class EvaluationV2Run(ContractModel):
    schema_version: Literal["1.0"] = "1.0"
    runtime_version: str = Field(min_length=1)
    run_id: str = Field(min_length=1)
    call_id: str = Field(min_length=1)
    mode: Literal["shadow"] = "shadow"
    status: ShadowRunStatus
    evaluator_version: str = Field(min_length=1)
    profile_id: str | None = None
    decision_sha256: str | None = Field(
        default=None,
        pattern=r"^[0-9a-f]{64}$",
    )
    decision: CallDecision | None = None
    presentation: CallEvaluationView | None = None
    legacy_proxy: LegacyAttentionProxy | None = None
    comparison: DecisionComparison | None = None
    limitations: list[str] = Field(default_factory=list)
    failure_reason: str | None = None
    provenance: SourceProvenance

    @model_validator(mode="after")
    def validate_run(self):
        succeeded = self.status == ShadowRunStatus.SUCCEEDED
        if succeeded != (
            self.decision is not None and self.presentation is not None
        ):
            raise ValueError(
                "successful shadow runs require decision and presentation"
            )
        if succeeded != (self.decision_sha256 is not None):
            raise ValueError(
                "successful shadow runs require a decision hash"
            )
        if self.status == ShadowRunStatus.FAILED:
            if not self.failure_reason:
                raise ValueError("failed shadow runs require a reason")
        elif self.failure_reason is not None:
            raise ValueError(
                "only failed shadow runs may include a failure reason"
            )
        if self.comparison is not None and self.legacy_proxy is None:
            raise ValueError("comparison requires a legacy proxy")
        return self


def _source(method: str) -> SourceProvenance:
    return SourceProvenance(
        producer="evaluator_v2.runtime",
        producer_version=RUNTIME_VERSION,
        method=method,
    )


def _sha256(payload: Any) -> str:
    encoded = json.dumps(
        payload,
        sort_keys=True,
        separators=(",", ":"),
    ).encode("utf-8")
    return hashlib.sha256(encoded).hexdigest()


def _run_id(call_id: str, marker: str) -> str:
    digest = hashlib.sha256(
        f"{call_id}:{RUNTIME_VERSION}:{marker}".encode()
    ).hexdigest()[:16]
    return f"{call_id}:v2:{digest}"


def _profile() -> DomainProfile:
    return DomainProfile.model_validate_json(
        PROFILE_PATH.read_text(encoding="utf-8")
    )


def _general_selection(call_id: str) -> ProfileSelection:
    return ProfileSelection(
        call_id=call_id,
        intent_ids=["service.general_request"],
        selection_method="general_service_fallback",
    )


def _known_selections() -> dict[str, ProfileSelection]:
    payload = json.loads(SELECTIONS_PATH.read_text(encoding="utf-8"))
    return {
        item["call_id"]: ProfileSelection.model_validate(item)
        for item in payload["calls"]
    }


def _selection(
    call_id: str,
    explicit: ProfileSelection | dict[str, Any] | None,
) -> ProfileSelection | None:
    if explicit is not None:
        selection = (
            explicit
            if isinstance(explicit, ProfileSelection)
            else ProfileSelection.model_validate(explicit)
        )
        if selection.call_id != call_id:
            raise ValueError("profile selection call_id does not match")
        return selection
    return _known_selections().get(call_id)


def legacy_attention_proxy(
    evaluation: dict[str, Any] | None,
) -> LegacyAttentionProxy | None:
    if not evaluation:
        return None
    compliance = evaluation.get("compliance") or {}
    failed = sum(
        isinstance(item, dict) and item.get("passed") is False
        for item in compliance.values()
    )
    escalation = str(
        (evaluation.get("escalation") or {}).get("risk_level")
        or "none"
    ).lower()
    return LegacyAttentionProxy(
        evaluator_version=str(
            (evaluation.get("_evaluator") or {}).get("version")
            or evaluation.get("rubric_version")
            or "v1"
        ),
        attention_required=failed > 0 or escalation in {
            "review",
            "escalate",
        },
        basis="failed_compliance_or_escalation",
        failed_compliance_count=failed,
        escalation_tier=escalation,
    )


def _comparison(
    decision: CallDecision,
    proxy: LegacyAttentionProxy | None,
) -> DecisionComparison | None:
    if proxy is None:
        return None
    if decision.decision_status != DecisionStatus.COMPLETE:
        return DecisionComparison(
            comparable=False,
            reason=(
                "v2 requirement coverage is incomplete; attention values "
                "must not be treated as equivalent."
            ),
        )
    return DecisionComparison(
        comparable=True,
        reason=(
            "Both evaluators produced a complete attention value under "
            "their named policies."
        ),
        attention_agreement=(
            proxy.attention_required == decision.attention_required
        ),
    )


def _unavailable(
    *,
    call_id: str,
    status: ShadowRunStatus,
    limitation: str,
    legacy_proxy: LegacyAttentionProxy | None,
) -> EvaluationV2Run:
    return EvaluationV2Run(
        runtime_version=RUNTIME_VERSION,
        run_id=_run_id(call_id, status.value),
        call_id=call_id,
        status=status,
        evaluator_version=f"v2-policy-{DECISION_POLICY_VERSION}",
        legacy_proxy=legacy_proxy,
        limitations=[limitation],
        provenance=_source("feature_flagged_shadow_unavailable"),
    )


def run_shadow_evaluation(
    *,
    transcript: dict[str, Any],
    sentiment: dict[str, Any] | None = None,
    legacy_evaluation: dict[str, Any] | None = None,
    profile_selection: ProfileSelection | dict[str, Any] | None = None,
    assessments: RequirementAssessmentBatch | dict[str, Any] | None = None,
    run_semantic_assessor: bool = True,
    transcript_source: str | None = None,
    sentiment_source: str | None = None,
    progress: Callable[[str], None] | None = None,
) -> EvaluationV2Run:
    report = progress or (lambda _stage: None)
    report("evaluating_v2_prepare")
    call_id = str(transcript.get("call_id") or "").strip()
    if not call_id:
        raise ValueError("transcript call_id is required")
    domain = str(transcript.get("domain") or "").strip().lower()
    proxy = legacy_attention_proxy(legacy_evaluation)
    selection = _selection(call_id, profile_selection)
    uses_general_profile = domain != "banking" or selection is None
    if uses_general_profile:
        profile = load_general_service_profile()
        selection = _general_selection(call_id)
    else:
        profile = _profile()
    plan = resolve_domain_plan(profile, selection)
    report("evaluating_v2_signals")
    bundle = build_signal_bundle(
        transcript,
        sentiment,
        transcript_source=transcript_source,
        sentiment_source=sentiment_source,
    )
    if sentiment:
        bundle = derive_acoustic_dynamics(bundle)
    assessment_batch = None
    if assessments is not None:
        assessment_batch = (
            assessments
            if isinstance(assessments, RequirementAssessmentBatch)
            else RequirementAssessmentBatch.model_validate(assessments)
        )
    elif run_semantic_assessor:
        report("evaluating_v2_requirements")
        assessment_batch = assess_requirements(bundle, plan)
    report("evaluating_v2_findings")
    derivation = derive_findings(bundle, plan, assessment_batch)
    report("evaluating_v2_decision")
    decision = build_call_decision(bundle, derivation)
    validate_decision_references(decision, bundle)
    report("evaluating_v2_presentation")
    presentation = project_call_evaluation(decision, bundle=bundle)
    limitations = [
        "research_domain_profile",
        "bounded_supervisor_not_run",
    ]
    if uses_general_profile:
        limitations.append("general_service_profile_not_domain_compliance")
    if assessment_batch is None:
        limitations.append("semantic_requirement_assessments_missing")
    if not sentiment:
        limitations.append("acoustic_input_unavailable")
    marker = presentation.decision_sha256
    return EvaluationV2Run(
        runtime_version=RUNTIME_VERSION,
        run_id=_run_id(call_id, marker),
        call_id=call_id,
        status=ShadowRunStatus.SUCCEEDED,
        evaluator_version=decision.evaluator_version,
        profile_id=plan.profile_id,
        decision_sha256=presentation.decision_sha256,
        decision=decision,
        presentation=presentation,
        legacy_proxy=proxy,
        comparison=_comparison(decision, proxy),
        limitations=limitations,
        provenance=_source("feature_flagged_shadow_evaluation"),
    )


def safe_run_shadow_evaluation(**kwargs) -> EvaluationV2Run:
    try:
        return run_shadow_evaluation(**kwargs)
    # Shadow execution must record any failure without failing the v1 job.
    except Exception as exc:  # noqa: BLE001
        transcript = kwargs.get("transcript") or {}
        call_id = str(transcript.get("call_id") or "unknown-call")
        proxy = legacy_attention_proxy(kwargs.get("legacy_evaluation"))
        reason = f"{type(exc).__name__}: {exc}"
        return EvaluationV2Run(
            runtime_version=RUNTIME_VERSION,
            run_id=_run_id(call_id, _sha256(reason)),
            call_id=call_id,
            status=ShadowRunStatus.FAILED,
            evaluator_version=f"v2-policy-{DECISION_POLICY_VERSION}",
            legacy_proxy=proxy,
            limitations=["shadow_runtime_failure"],
            failure_reason=reason[:500],
            provenance=_source("feature_flagged_shadow_failure"),
        )