Public source file

backend/app/services/trust_residuals_decision.py

Documentation home
476 lines18,471 bytesread-only generated view
  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
"""
Shared trust-residuals decision core (the paper's Δ = decide(R⃗, P)).

This module is the single source of truth for the residual vocabulary, the
positive-eligibility sets, the attention lattice, and the decision function
shared by the offline corpus evaluation (scripts/trust_residuals_evaluation.py)
and the runtime scanner decision path. It is deliberately stdlib-only so both
callers can import it without pulling backend service dependencies.
"""

from __future__ import annotations

from dataclasses import dataclass
from typing import Any, Iterable, Literal, Mapping


PRIMARY_STATES = {
    "unreadable",
    "unverified",
    "signed-unaccepted-issuer",
    "verified-issuer",
    "verified-issuer-destination-risky",
    "blocked",
}

ANNOTATIONS = {
    "artifact-warning",
    "stale-offline-warning",
    "limited-runtime-safety-visibility",
    "redirect-variation-warning",
    "invalid-trust-claim-warning",
    "policy-profile-warning",
    "incomplete-verification-warning",
}

# The decision context P is drawn from a closed profile enum. Every profile
# comparison in decide() is an equality test against one of these values, so
# an unrecognized string would otherwise silently select the most permissive
# arm of every profile-conditional rule; decide() rejects it instead.
KNOWN_PROFILES: frozenset[str] = frozenset(
    {
        "strict-online",
        "bounded-online",
        "bounded-offline",
        "production-trusted",
        "reference-testing",
    }
)

# The policy profile is decision context (the P in decide(R⃗, P)), not a member
# of the residual vector: it selects how strict each rule is, it is not itself
# verification evidence. It therefore has no entry here, no residual tier, and
# no ablation — "ablating" it would swap policies, not remove evidence.
RESIDUAL_FAMILIES: tuple[str, ...] = (
    "issuer_chain",
    "destination_policy",
    "redirect_flow",
    "runtime_safety",
    "freshness",
    "artifact_integrity",
)

# D15 totality rule: the positive terminal is only reachable when every residual
# sits in an explicitly positive-eligible tier. Any tier outside these sets —
# including tiers added to compute_residuals() later — falls through to a
# never-positive caution default instead of implicit trust.
POSITIVE_ELIGIBLE_RESIDUALS: dict[str, frozenset[str]] = {
    "issuer_chain": frozenset({"pass"}),
    "destination_policy": frozenset({"pass", "not-applicable"}),
    "redirect_flow": frozenset({"pass", "warn", "not-applicable"}),
    "runtime_safety": frozenset({"pass", "unavailable"}),
    "freshness": frozenset({"pass", "warn", "not-applicable"}),
    "artifact_integrity": frozenset({"pass", "warn"}),
}

# Tiers that report the absence of decision-grade evidence rather than a
# verdict about it. A profile may tolerate insufficiency for an unrequired
# family (runtime "unavailable" stays positive-eligible and is annotated), but
# a family P declares mandatory can never satisfy its evidence requirement
# with one of these tiers, even where the tier is positive-eligible. The
# "stale" and "not-checked" tiers are listed for completeness; their dedicated
# D14 arms in decide() return before the general mandatory rule is reached.
INSUFFICIENCY_TIERS: dict[str, frozenset[str]] = {
    "runtime_safety": frozenset({"unavailable", "not-checked", "stale"}),
}

AttentionLevel = Literal["positive", "neutral", "warning", "block"]


@dataclass(frozen=True)
class Decision:
    primary_state: str
    annotations: tuple[str, ...] = ()
    reason_codes: tuple[str, ...] = ()

    def as_dict(self) -> dict[str, Any]:
        return {
            "primary_state": self.primary_state,
            "annotations": list(self.annotations),
            "reason_codes": list(self.reason_codes),
            "attention_level": attention_level(self),
        }


class ResidualEvidenceError(ValueError):
    """Raised when evidence cannot be mapped into the residual vocabulary."""


class UnknownProfileError(ValueError):
    """Raised when decide() receives a profile outside KNOWN_PROFILES."""


class UnknownResidualFamilyError(ValueError):
    """Raised when decide() receives a mandatory name outside RESIDUAL_FAMILIES."""


def compute_residuals(case: dict[str, Any]) -> dict[str, str]:
    evidence = case["evidence"]
    managed_claim = evidence["managed_trust_claim"]
    issuer = evidence["issuer"]

    if managed_claim == "invalid":
        issuer_chain = "invalid-managed-claim"
    elif issuer == "accepted":
        issuer_chain = "pass"
    elif issuer == "revoked":
        issuer_chain = "revoked-issuer"
    elif issuer == "unaccepted":
        issuer_chain = "unaccepted-issuer"
    elif issuer == "contradictory":
        issuer_chain = "cross-root-contradiction"
    elif issuer == "none":
        issuer_chain = "no-issuer"
    else:
        raise ResidualEvidenceError(
            f"{case['case_id']} has unsupported issuer evidence: {issuer}"
        )

    return {
        "issuer_chain": issuer_chain,
        "destination_policy": map_status(
            evidence["destination_policy"],
            {
                "bound": "pass",
                "mismatch": "fail",
                "not_applicable": "not-applicable",
            },
            case,
        ),
        "redirect_flow": map_status(
            evidence["redirect_flow"],
            {
                "pass": "pass",
                "fail": "fail",
                "warn": "warn",
                "not_applicable": "not-applicable",
            },
            case,
        ),
        "runtime_safety": map_status(
            evidence["runtime_safety"],
            {
                "clear": "pass",
                "risky": "warn",
                "blocked": "block",
                "provider_disagreement_block": "block",
                "stale": "stale",
                "unavailable": "unavailable",
                "provider_disagreement_unavailable": "unavailable",
                "not_checked": "not-checked",
            },
            case,
        ),
        "freshness": map_status(
            evidence["freshness"],
            {
                "fresh": "pass",
                "stale_warn": "warn",
                "expired_block": "block",
                "replay_block": "block",
                "not_applicable": "not-applicable",
            },
            case,
        ),
        "artifact_integrity": map_status(
            evidence["artifact_integrity"],
            {
                "pass": "pass",
                "warn": "warn",
                "fail": "fail",
                "block": "block",
            },
            case,
        ),
    }


def map_status(
    value: str,
    mapping: dict[str, str],
    case: dict[str, Any],
) -> str:
    try:
        return mapping[value]
    except KeyError as exc:
        raise ResidualEvidenceError(
            f"{case['case_id']} has unsupported evidence value: {value}"
        ) from exc


def decide(
    residuals: Mapping[str, str],
    *,
    profile: str,
    mandatory_residuals: Iterable[str] = (),
    qr_decodable: bool,
) -> Decision:
    if profile not in KNOWN_PROFILES:
        raise UnknownProfileError(
            f"unknown policy profile {profile!r}; expected one of "
            f"{sorted(KNOWN_PROFILES)}"
        )

    # A misspelled mandatory name would otherwise never match its equality
    # test in D11, silently demoting a policy-mandated block to a warning.
    mandatory = set(mandatory_residuals)
    unknown_families = mandatory - set(RESIDUAL_FAMILIES)
    if unknown_families:
        raise UnknownResidualFamilyError(
            f"unknown mandatory residual families {sorted(unknown_families)}; "
            f"expected members of {sorted(RESIDUAL_FAMILIES)}"
        )
    annotations: list[str] = []
    reasons: list[str] = []

    # Evaluation order follows the paper's rule classes: capture (D0), the
    # mandatory block class (D3, D6-D8, D10, strict-D9, mandatory-D11), the
    # downgrade/annotation class (D11, D13, then the neutral capture D1/D2,
    # then D14 and D5 with annotations), the totality default (D15), and the
    # positive terminal (D4). Adverse tiers (fail/block) preempt neutral
    # capture; insufficiency tiers (stale, unavailable, not-checked) gate only
    # paths that could otherwise turn positive, because requiredness under D14
    # protects positive trust and a neutral-capped path grants none.
    if not qr_decodable:
        return Decision("unreadable", reason_codes=("qr-undecodable",))

    if residuals["issuer_chain"] == "invalid-managed-claim":
        if profile == "reference-testing":
            return Decision(
                "unverified",
                ("invalid-trust-claim-warning", "policy-profile-warning"),
                ("invalid-managed-trust-claim", "reference-testing-profile"),
            )
        return Decision("blocked", reason_codes=("invalid-managed-trust-claim",))

    if residuals["issuer_chain"] == "revoked-issuer":
        return Decision("blocked", reason_codes=("issuer-revoked",))

    # D6/D7/D8 and the freshness block tier of D9: adverse evidence in any
    # non-issuer family blocks before the neutral issuer rules can capture the
    # case, so an unsigned or unaccepted-issuer payload with a blocking
    # runtime verdict is still blocked.
    for family, reason in (
        ("destination_policy", "destination-policy-failed"),
        ("redirect_flow", "redirect-flow-failed"),
        ("runtime_safety", "runtime-safety-block"),
        ("freshness", "freshness-block"),
    ):
        if residuals[family] in {"fail", "block"}:
            return Decision("blocked", reason_codes=(reason,))

    # D9, strict arm: strict online profiles refuse stale freshness evidence
    # outright; other profiles carry it as the stale-offline annotation below.
    if profile == "strict-online" and residuals["freshness"] == "warn":
        return Decision("blocked", reason_codes=("freshness-stale",))

    if residuals["artifact_integrity"] == "block":
        return Decision("blocked", reason_codes=("artifact-integrity-block",))

    # D11: an artifact-integrity failure is policy-gated. If P marks the
    # artifact residual mandatory, the failure blocks; otherwise it removes
    # positive trust and is preserved as evidence, never a positive state.
    if residuals["artifact_integrity"] == "fail":
        if "artifact_integrity" in mandatory:
            return Decision("blocked", reason_codes=("artifact-integrity-failed",))
        return Decision(
            "unverified",
            ("artifact-warning",),
            ("artifact-integrity-failed",),
        )

    # D13: contradictory verdicts across accepted roots for the same issuer.
    # The paper delegates resolution to P: strict online profiles refuse to
    # arbitrate and block; bounded profiles surface an explicitly labeled
    # caution. Neither profile may resolve the contradiction into trust.
    if residuals["issuer_chain"] == "cross-root-contradiction":
        if profile == "strict-online":
            return Decision("blocked", reason_codes=("cross-root-contradiction",))
        return Decision(
            "unverified",
            ("incomplete-verification-warning",),
            ("cross-root-contradiction",),
        )

    # D1/D2 neutral capture: after every adverse rule, before the
    # missing-evidence rules. A missing or stale runtime verdict adds nothing
    # to a path already capped at neutral, so it neither blocks these states
    # nor annotates them.
    if residuals["issuer_chain"] == "no-issuer":
        return Decision("unverified", reason_codes=("no-issuer-path",))

    if residuals["issuer_chain"] == "unaccepted-issuer":
        return Decision(
            "signed-unaccepted-issuer",
            reason_codes=("signed-by-unaccepted-issuer",),
        )

    # D14: an expired runtime verdict is stale runtime-safety evidence owned by
    # R_S. Strict online profiles block; other profiles fall to an explicitly
    # labeled caution and never a positive state.
    if residuals["runtime_safety"] == "stale":
        if profile == "strict-online":
            return Decision("blocked", reason_codes=("runtime-safety-stale",))
        return Decision(
            "unverified",
            ("incomplete-verification-warning",),
            ("runtime-safety-stale",),
        )

    # D14: the strict online profile marks the runtime-safety residual as
    # required evidence, so a verdict that was never obtained ("not-checked")
    # or could not be obtained ("unavailable") blocks outright.
    if profile == "strict-online" and residuals["runtime_safety"] in {
        "unavailable",
        "not-checked",
    }:
        return Decision(
            "blocked",
            reason_codes=(
                "runtime-safety-unavailable"
                if residuals["runtime_safety"] == "unavailable"
                else "runtime-safety-not-checked",
            ),
        )

    # D14, non-strict arm for a skipped check: bounded profiles treat an
    # unavailable provider as best-effort evidence (annotated below), but a
    # verdict that was never requested is a verifier omission rather than a
    # provider failure, so it caps the outcome at a labeled caution — never a
    # positive state.
    if residuals["runtime_safety"] == "not-checked":
        return Decision(
            "unverified",
            ("incomplete-verification-warning",),
            ("runtime-safety-not-checked",),
        )

    if residuals["freshness"] == "warn":
        annotations.append("stale-offline-warning")
        reasons.append("freshness-warning")
    if residuals["runtime_safety"] == "unavailable":
        annotations.append("limited-runtime-safety-visibility")
        reasons.append("runtime-safety-unavailable")
    if residuals["artifact_integrity"] == "warn":
        annotations.append("artifact-warning")
        reasons.append("artifact-integrity-warning")
    if residuals["redirect_flow"] == "warn":
        annotations.append("redirect-variation-warning")
        reasons.append("redirect-variation-warning")
    if profile == "reference-testing":
        annotations.append("policy-profile-warning")
        reasons.append("reference-testing-profile")

    # D5: a risky runtime verdict downgrades the positive terminal to the
    # explicitly risky state. Evaluated after annotation accumulation so
    # co-occurring cautions (stale-offline, artifact, redirect variation)
    # are carried on the risky state instead of being dropped. The risky
    # state still asserts issuer verification, so it demands the same
    # positive-eligibility of every non-runtime family as the positive
    # terminal; an unmodeled tier anywhere falls through to the D15
    # default instead of reaching this early return.
    if residuals["runtime_safety"] == "warn" and all(
        residuals[family] in POSITIVE_ELIGIBLE_RESIDUALS[family]
        for family in RESIDUAL_FAMILIES
        if family != "runtime_safety"
    ):
        reasons.append("runtime-safety-warning")
        return Decision(
            "verified-issuer-destination-risky",
            tuple(annotations),
            tuple(reasons),
        )

    # D14, general arm: requiredness under P is not limited to runtime safety.
    # A family P declares mandatory must present decision-grade evidence: any
    # tier that survived the adverse rules yet is either an insufficiency tier
    # or outside the positive-eligible set means the required evidence could
    # not be established. Positive-eligibility does not excuse a required
    # family — runtime "unavailable" is tolerated and annotated only when the
    # family is unrequired. Strict online profiles block; every other profile
    # caps the outcome at an explicitly labeled caution, never a positive
    # state and never the generic D15 default reserved for unrequired
    # families.
    for family in RESIDUAL_FAMILIES:
        if family not in mandatory:
            continue
        if (
            residuals[family] not in INSUFFICIENCY_TIERS.get(family, frozenset())
            and residuals[family] in POSITIVE_ELIGIBLE_RESIDUALS[family]
        ):
            continue
        if family == "runtime_safety" and residuals[family] == "warn":
            # A risky runtime verdict is real evidence, not missing evidence;
            # D5 above and the D15 default below own that tier.
            continue
        reason = f"{family.replace('_', '-')}-required-evidence-missing"
        if profile == "strict-online":
            return Decision("blocked", reason_codes=(reason,))
        return Decision(
            "unverified",
            ("incomplete-verification-warning",),
            (reason,),
        )

    if any(
        residuals[family] not in POSITIVE_ELIGIBLE_RESIDUALS[family]
        for family in RESIDUAL_FAMILIES
    ):
        return Decision(
            "unverified",
            ("incomplete-verification-warning",),
            ("unmodeled-residual-combination",),
        )

    return Decision("verified-issuer", tuple(annotations), tuple(reasons))


def attention_level(decision: Decision) -> AttentionLevel:
    if decision.primary_state == "blocked":
        return "block"
    if (
        decision.primary_state in {"signed-unaccepted-issuer", "verified-issuer-destination-risky"}
        or decision.annotations
    ):
        return "warning"
    if decision.primary_state in {"unreadable", "unverified"}:
        return "neutral"
    return "positive"


def attention_rank(decision: Decision) -> int:
    return {
        "positive": 0,
        "neutral": 1,
        "warning": 2,
        "block": 3,
    }[attention_level(decision)]


def is_attention_undercut(expected: Decision, actual: Decision) -> bool:
    """True when ``actual`` demands strictly less user attention than ``expected``.

    This is an ordering comparison over attention levels, not a claim of
    positive trust: a neutral ``unverified`` rendering of a case that demands
    a warning undercuts the required attention without asserting anything.
    """
    return attention_rank(actual) < attention_rank(expected)


def is_unsafe_positive(expected: Decision, actual: Decision) -> bool:
    """True when ``actual`` asserts positive trust where ``expected`` does not."""
    return (
        attention_level(actual) == "positive"
        and attention_level(expected) != "positive"
    )