File size: 2,816 Bytes
ddebc3e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Account repository protocol — the contract every backend must satisfy."""

from typing import Protocol, runtime_checkable

from .commands import AccountPatch, AccountUpsert, BulkReplacePoolCommand, ListAccountsQuery
from .models import (
    AccountChangeSet,
    AccountMutationResult,
    AccountPage,
    AccountRecord,
    RuntimeSnapshot,
)


@runtime_checkable
class AccountRepository(Protocol):
    """Storage contract shared by all account backends."""

    async def initialize(self) -> None:
        """Create schema / tables / indices if they do not exist."""
        ...

    async def get_revision(self) -> int:
        """Return the current global revision counter."""
        ...

    async def runtime_snapshot(self) -> RuntimeSnapshot:
        """Return all non-deleted accounts for hot-path bootstrap."""
        ...

    async def scan_changes(
        self,
        since_revision: int,
        *,
        limit: int = 5000,
    ) -> AccountChangeSet:
        """Return records modified after *since_revision* (inclusive)."""
        ...

    async def upsert_accounts(
        self,
        items: list[AccountUpsert],
    ) -> AccountMutationResult:
        """Insert or replace accounts.  Pushes revision."""
        ...

    async def patch_accounts(
        self,
        patches: list[AccountPatch],
    ) -> AccountMutationResult:
        """Apply partial updates to existing accounts.  Pushes revision."""
        ...

    async def delete_accounts(
        self,
        tokens: list[str],
    ) -> AccountMutationResult:
        """Soft-delete accounts (set deleted_at).  Pushes revision."""
        ...

    async def get_accounts(
        self,
        tokens: list[str],
    ) -> list[AccountRecord]:
        """Fetch accounts by token list."""
        ...

    async def list_accounts(
        self,
        query: ListAccountsQuery,
    ) -> AccountPage:
        """Return a paginated, filtered, sorted account list."""
        ...

    async def replace_pool(
        self,
        command: BulkReplacePoolCommand,
    ) -> AccountMutationResult:
        """Atomically replace all accounts in a pool."""
        ...

    async def reset_expired_console_windows(self) -> int:
        """Batch-reset console quotas that are exhausted and expired.  Pushes revision."""
        ...

    async def recover_console_expired_accounts(self) -> int:
        """Auto-recover console 429 EXPIRED accounts with successful history.

        Recovery conditions (AND):
        - status = EXPIRED
        - state_reason = 'console_429_threshold_exceeded'
        - usage_use_count > 5
        - ext.expired_at <= now - 1 hour
        """
        ...

    async def close(self) -> None:
        """Release database connections / file handles."""
        ...


__all__ = ["AccountRepository"]