File size: 3,110 Bytes
2d2c6e4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from typing import Any, List, Optional
from codereview_env.config import CONFIG
from utils.pagination import get_paged_items


class PaginationValidator:
    """
    Validation layer for pagination inputs.
    Strictly handles edge cases and enforced constraints.
    """

    @staticmethod
    def validate_inputs(items: List[Any], page: Any, page_size: Any) -> tuple[int, int]:
        """
        Validates types and ranges for pagination.

        Formula:
        min_page = CONFIG.pagination.MIN_PAGE
        max_size = CONFIG.pagination.MAX_PAGE_SIZE
        """
        # Rule 3.1: Wrong types
        if not isinstance(items, list):
            raise TypeError(CONFIG.pagination.ERROR_NOT_A_LIST)

        try:
            p = int(page)
            ps = int(page_size)
        except (ValueError, TypeError):
            raise TypeError(CONFIG.pagination.ERROR_NOT_NUMERIC)

        # Rule 3.1: Null/Zero/Negative
        conf = CONFIG.pagination
        if p < conf.MIN_PAGE:
            msg = conf.ERROR_INVALID_PAGE.format(min_page=conf.MIN_PAGE)
            raise ValueError(msg)

        if ps < conf.MIN_PAGE or ps > conf.MAX_PAGE_SIZE:
            msg = conf.ERROR_INVALID_SIZE.format(max_size=conf.MAX_PAGE_SIZE)
            raise ValueError(msg)

        return p, ps


class SafePaginationFacade:
    """
    Facade pattern providing a safe entry point to the pagination system.
    Complies with Rule 1: Zero modifications to existing code.
    """

    def __init__(self, items: List[Any]):
        self._items = items
        self._length = len(items)

    def get_page(self, page: int, page_size: Optional[int] = None) -> List[Any]:
        """
        Safely retrieves a page of items.
        Calls the underlying black-box system after thorough validation.
        """
        ps = page_size if page_size is not None else CONFIG.pagination.DEFAULT_PAGE_SIZE

        # Validation Layer
        clean_page, clean_ps = PaginationValidator.validate_inputs(
            self._items, page, ps
        )

        # Rule 3.1: Handle empty list or out-of-bounds gracefully before calling existing
        if not self._items:
            return []

        # Existing System Call
        return get_paged_items(self._items, clean_page, clean_ps)


class SafeRewardCalculator:
    """
    Wrapper for reward calculations to ensure mathematical accuracy.
    """

    @staticmethod
    def calculate_final_reward(score: float, bonus: float) -> float:
        """
        Mathematically correct reward summation with final-step rounding.

        Boundary Math:
        score [0.0, 1.0], bonus [0.0, 0.25]
        Return range: [0.0, 1.0]
        """
        conf = CONFIG.reward

        # Intermediate calculations (full precision)
        # Using raw floats for calculation per Rule 3.5
        total = (score * conf.BONUS_COEFFICIENT) + bonus

        # Clipping to safety boundaries
        safe_total = max(conf.MIN_REWARD, min(conf.MAX_REWARD, total))

        # Rule 3.5: Round floating-point numbers ONLY at the final output step
        return round(safe_total, conf.DECIMAL_PRECISION)