File size: 7,387 Bytes
ddebc3e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
24822d8
 
 
ddebc3e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2ed8df9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
24822d8
 
 
 
 
2ed8df9
 
24822d8
 
 
ddebc3e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2ed8df9
 
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
"""Apply feedback to the runtime table columns (in-place, lock-free inner ops).

The caller (AccountDirectory) is responsible for holding the state lock before
calling these functions.

Strategy-split functions
------------------------
``apply_success`` / ``apply_rate_limited`` are split into ``*_quota`` and
``*_random`` variants. The caller picks exactly one based on the active
selector strategy. No runtime ``if`` inside the apply helpers themselves.
"""

from app.platform.runtime.clock import now_s
from ..shared.enums import ALL_MODE_IDS, StatusId
from .table import AccountRuntimeTable
from app.control.account.runtime import global_auditor
from app.control.account.models import AuditLogRecord


# Health adjustment constants.
_SUCCESS_STEP        = 0.12
_AUTH_FACTOR         = 0.55
_FORBIDDEN_FACTOR    = 0.25
_RATE_LIMIT_FACTOR   = 0.45
_SERVER_ERROR_FACTOR = 0.75
_MIN_HEALTH = 0.05
_MAX_HEALTH = 1.0


# ---------------------------------------------------------------------------
# apply_success — two independent implementations
# ---------------------------------------------------------------------------


def apply_success_quota(table: AccountRuntimeTable, idx: int, mode_id: int) -> None:
    """Quota strategy: decrement per-mode quota and improve health."""
    quota_col = table._quota_col(mode_id)
    quota_col[idx] = max(0, int(quota_col[idx]) - 1)
    _bump_health(table, idx)


def apply_success_random(table: AccountRuntimeTable, idx: int) -> None:
    """Random strategy: only improve health; quota columns are ignored."""
    _bump_health(table, idx)


# ---------------------------------------------------------------------------
# apply_rate_limited — two independent implementations
# ---------------------------------------------------------------------------


def apply_rate_limited_quota(
    table: AccountRuntimeTable, idx: int, mode_id: int
) -> None:
    """Quota strategy: zero the mode quota and reduce health."""
    table._quota_col(mode_id)[idx] = 0
    _adjust_health(table, idx, _RATE_LIMIT_FACTOR)


def apply_rate_limited_random(
    table: AccountRuntimeTable, idx: int, *, cooling_sec: int
) -> None:
    """Random strategy: set per-account cooldown timestamp; do not touch quota.

    The account stays in ``mode_available``; ``_random_select`` filters it out
    until ``cooling_until_s_by_idx`` has elapsed.
    """
    ts = now_s() + max(0, cooling_sec)
    cooling_col = table.cooling_until_s_by_idx
    cooling_col[idx] = max(int(cooling_col[idx]), ts)
    _adjust_health(table, idx, _RATE_LIMIT_FACTOR)


# ---------------------------------------------------------------------------
# Strategy-agnostic feedback helpers (shared)
# ---------------------------------------------------------------------------


def apply_auth_failure(table: AccountRuntimeTable, idx: int) -> None:
    """Reduce health on 401; caller may mark account expired."""
    _adjust_health(table, idx, _AUTH_FACTOR)


def apply_forbidden(table: AccountRuntimeTable, idx: int) -> None:
    """Reduce health heavily on 403."""
    _adjust_health(table, idx, _FORBIDDEN_FACTOR)


def apply_server_error(table: AccountRuntimeTable, idx: int) -> None:
    """Mildly reduce health on 5xx / transport errors."""
    _adjust_health(table, idx, _SERVER_ERROR_FACTOR)


def apply_status_change(
    table: AccountRuntimeTable, idx: int, new_status_id: int
) -> None:
    """Update status column and refresh availability indexes."""
    pool_id = int(table.pool_by_idx[idx])
    old_status = int(table.status_by_idx[idx])

    if old_status == new_status_id:
        return

    table.status_by_idx[idx] = new_status_id

    if new_status_id != int(StatusId.ACTIVE):
        for mode_id in ALL_MODE_IDS:
            bucket = table.mode_available.get((pool_id, mode_id))
            if bucket:
                bucket.discard(idx)
    else:
        for mode_id in ALL_MODE_IDS:
            if int(table._quota_col(mode_id)[idx]) > 0:
                table.mode_available.setdefault((pool_id, mode_id), set()).add(idx)


def apply_quota_update(
    table: AccountRuntimeTable,
    idx: int,
    mode_id: int,
    remaining: int,
    reset_s: int,
) -> None:
    """Update quota and reset timestamp from upstream API data.

    Used exclusively by the quota strategy (refresh service). The random
    strategy never calls this.
    """
    quota_col = table._quota_col(mode_id)
    reset_col = table._reset_col(mode_id)
    quota_col[idx] = max(0, min(remaining, 32767))
    reset_col[idx] = reset_s

    pool_id = int(table.pool_by_idx[idx])
    if int(table.status_by_idx[idx]) == int(StatusId.ACTIVE):
        bucket = table.mode_available.setdefault((pool_id, mode_id), set())
        if int(table._window_col(mode_id)[idx]) > 0:
            bucket.add(idx)


def increment_inflight(table: AccountRuntimeTable, idx: int) -> None:
    table.inflight_by_idx[idx] = min(int(table.inflight_by_idx[idx]) + 1, 65535)


def decrement_inflight(table: AccountRuntimeTable, idx: int) -> None:
    table.inflight_by_idx[idx] = max(0, int(table.inflight_by_idx[idx]) - 1)


def update_last_use(table: AccountRuntimeTable, idx: int, now_s: int) -> None:
    table.last_use_at_by_idx[idx] = now_s


def update_last_fail(table: AccountRuntimeTable, idx: int, now_s: int) -> None:
    table.last_fail_at_by_idx[idx] = now_s
    table.fail_count_by_idx[idx] = min(int(table.fail_count_by_idx[idx]) + 1, 65535)


# ---------------------------------------------------------------------------
# Internal
# ---------------------------------------------------------------------------


def _bump_health(table: AccountRuntimeTable, idx: int) -> None:
    table.health_by_idx[idx] = min(
        _MAX_HEALTH, float(table.health_by_idx[idx]) + _SUCCESS_STEP
    )


def _adjust_health(table: AccountRuntimeTable, idx: int, factor: float) -> None:
    table.health_by_idx[idx] = max(
        _MIN_HEALTH, float(table.health_by_idx[idx]) * factor
    )


# ---------------------------------------------------------------------------
# Audit Extension Logic (Memory-safe mode)
# ---------------------------------------------------------------------------

_MODE_ID_TO_NAME = {
    0: "auto",
    1: "fast",
    2: "expert",
    3: "heavy",
    4: "grok_4_3",
    5: "console"
}


def trigger_audit_log(token: str, quota_type: str | int, status: str, ctx: dict) -> None:
    """Record a single API call metrics into the in-memory rolling queue."""
    # If a raw integer mode_id is passed, map it automatically to its string name
    if isinstance(quota_type, int):
        quota_type = _MODE_ID_TO_NAME.get(quota_type, "unknown")

    log_rec = AuditLogRecord(
        token=token,
        device_ip=ctx.get("client_ip", "0.0.0.0"),
        user_agent=ctx.get("user_agent", "Unknown"),
        conversation_id=ctx.get("conversation_id"),
        quota_type=str(quota_type),
        status=status  # "SUCCESS" or "FAIL"
    )
    global_auditor.record(log_rec)


__all__ = [
    "apply_success_quota",
    "apply_success_random",
    "apply_rate_limited_quota",
    "apply_rate_limited_random",
    "apply_auth_failure",
    "apply_forbidden",
    "apply_server_error",
    "apply_status_change",
    "apply_quota_update",
    "increment_inflight",
    "decrement_inflight",
    "update_last_use",
    "update_last_fail",
    "trigger_audit_log",  # Exported properly for the upper layers
]