File size: 13,198 Bytes
2415446
a1bab2d
 
2415446
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a1bab2d
2415446
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
05e7f80
2415446
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Platform-neutral voice note helpers."""
from __future__ import annotations


import asyncio
from collections.abc import Awaitable, Callable
from contextvars import ContextVar
from dataclasses import dataclass
from enum import Enum
from pathlib import Path
from typing import Protocol
from uuid import uuid4

from .models import MessageScope


async def _await_owned_task[T](
    task: asyncio.Task[T],
) -> tuple[T, asyncio.CancelledError | None]:
    """Finish an owned task before returning any caller cancellation."""

    cancellation: asyncio.CancelledError | None = None
    current = asyncio.current_task()
    while True:
        cancelling_before = current.cancelling() if current is not None else 0
        try:
            return await asyncio.shield(task), cancellation
        except asyncio.CancelledError as exc:
            if current is None or (
                current.cancelling() <= cancelling_before and task.done()
            ):
                raise
            cancellation = cancellation or exc


class Transcriber(Protocol):
    """Consumer-owned voice transcription boundary."""

    async def transcribe(self, file_path: Path) -> str: ...

    async def close(self) -> None: ...


@dataclass(frozen=True, slots=True)
class PendingVoiceClaim:
    """Opaque ownership token for one pending voice-note generation."""

    scope: MessageScope
    voice_message_id: str
    claim_id: str


_current_voice_claim: ContextVar[PendingVoiceClaim | None] = ContextVar(
    "current_voice_claim",
    default=None,
)


class VoiceHandoffOutcome(Enum):
    """Exclusive outcome of publishing one transcribed voice message."""

    REJECTED = "rejected"
    COMPLETED = "completed"
    CANCELLED = "cancelled"


@dataclass(frozen=True, slots=True)
class VoiceCancellationResult:
    """Released ownership for one successfully cancelled user voice note."""

    scope: MessageScope
    voice_message_id: str
    status_message_id: str | None
    delete_message_ids: frozenset[str]


@dataclass(slots=True)
class _PendingVoice:
    claim: PendingVoiceClaim
    status_message_id: str | None = None
    handoff_task: asyncio.Task[None] | None = None


class PendingVoiceRegistry:
    """Own atomic reservation, cancellation, and handoff of voice notes."""

    def __init__(self) -> None:
        self._pending: dict[tuple[MessageScope, str], _PendingVoice] = {}
        self._lock = asyncio.Lock()
        self._active_cancellations: dict[PendingVoiceClaim, int] = {}

    async def reserve(
        self,
        scope: MessageScope,
        voice_message_id: str,
    ) -> PendingVoiceClaim | None:
        async with self._lock:
            key = (scope, voice_message_id)
            if key in self._pending:
                return None
            claim = PendingVoiceClaim(
                scope=scope,
                voice_message_id=voice_message_id,
                claim_id=uuid4().hex,
            )
            self._pending[key] = _PendingVoice(claim=claim)
            return claim

    async def bind_status(
        self,
        claim: PendingVoiceClaim,
        status_message_id: str,
    ) -> bool:
        async with self._lock:
            entry = self._entry_for_claim(claim)
            if entry is None:
                return False
            if entry.status_message_id is not None:
                return entry.status_message_id == status_message_id
            status_key = (claim.scope, status_message_id)
            existing = self._pending.get(status_key)
            if existing is not None and existing is not entry:
                return False
            entry.status_message_id = status_message_id
            self._pending[status_key] = entry
            return True

    async def handoff(
        self,
        claim: PendingVoiceClaim,
        callback: Callable[[], Awaitable[None]],
    ) -> VoiceHandoffOutcome:
        """Run a published handoff while retaining its cancellable ownership."""
        async with self._lock:
            entry = self._entry_for_claim(claim)
            if (
                entry is None
                or entry.status_message_id is None
                or entry.handoff_task is not None
            ):
                return VoiceHandoffOutcome.REJECTED
            task = asyncio.create_task(
                self._run_callback(claim, callback),
                name=f"voice-handoff-{claim.claim_id}",
            )
            entry.handoff_task = task

        current = asyncio.current_task()
        cancelling_before = current.cancelling() if current is not None else 0
        try:
            await asyncio.shield(task)
        except BaseException as error:
            caller_cancelled = (
                isinstance(error, asyncio.CancelledError)
                and current is not None
                and (current.cancelling() > cancelling_before or not task.done())
            )
            if caller_cancelled:
                task.cancel()
                child_error: BaseException | None = None
                try:
                    await self._drain((task,))
                except BaseException as drained_error:
                    child_error = drained_error
                await self._finish_ownership(entry)
                if child_error is not None:
                    raise child_error from None
                raise error from None

            completed, cancellation = await self._finish_ownership(entry)
            if cancellation is not None and not self._is_fatal(error):
                raise cancellation from None
            if completed or self._is_fatal(error):
                raise error
            return VoiceHandoffOutcome.CANCELLED

        completed, cancellation = await self._finish_ownership(entry)
        if cancellation is not None:
            raise cancellation
        if completed:
            return VoiceHandoffOutcome.COMPLETED
        return VoiceHandoffOutcome.CANCELLED

    async def discard(self, claim: PendingVoiceClaim) -> bool:
        async with self._lock:
            entry = self._entry_for_claim(claim)
            if entry is None:
                return False
            self._remove(entry)
            task = entry.handoff_task
        cancellation = await self._cancel_and_drain(task)
        if cancellation is not None:
            raise cancellation
        return True

    async def cancel(
        self, scope: MessageScope, reply_id: str
    ) -> VoiceCancellationResult | None:
        current_claim = _current_voice_claim.get()
        self._protect_claim(current_claim)
        try:
            async with self._lock:
                entry = self._pending.get((scope, reply_id))
                if entry is None or self._is_excluded(entry, current_claim):
                    return None
                self._remove(entry)
                task = entry.handoff_task
                result = self._cancellation_result(entry, reply_id)
            cancellation = await self._cancel_and_drain(task)
            if cancellation is not None:
                raise cancellation
            return result
        finally:
            self._unprotect_claim(current_claim)

    async def cancel_all(self) -> tuple[VoiceCancellationResult, ...]:
        """Cancel every unique pending voice note and drain published handoffs."""
        return await self._cancel_matching_scope(None)

    async def cancel_scope(
        self, scope: MessageScope
    ) -> tuple[VoiceCancellationResult, ...]:
        """Cancel every unique pending voice note in one platform chat."""
        return await self._cancel_matching_scope(scope)

    async def _cancel_matching_scope(
        self,
        scope: MessageScope | None,
    ) -> tuple[VoiceCancellationResult, ...]:
        current_claim = _current_voice_claim.get()
        self._protect_claim(current_claim)
        try:
            async with self._lock:
                entries = tuple(
                    {
                        entry.claim: entry
                        for (entry_scope, _reference_id), entry in self._pending.items()
                        if (scope is None or entry_scope == scope)
                        and not self._is_excluded(entry, current_claim)
                    }.values()
                )
                for entry in entries:
                    self._remove(entry)

            tasks = tuple(
                task for entry in entries if (task := entry.handoff_task) is not None
            )
            for task in tasks:
                task.cancel()
            cancellation = await self._drain(tasks)
            if cancellation is not None:
                raise cancellation
            return tuple(self._cancellation_result(entry) for entry in entries)
        finally:
            self._unprotect_claim(current_claim)

    async def _finish_ownership(
        self,
        entry: _PendingVoice,
    ) -> tuple[bool, asyncio.CancelledError | None]:
        finish_task = asyncio.create_task(
            self._complete_if_owned(entry),
            name=f"voice-handoff-finish-{entry.claim.claim_id}",
        )
        return await _await_owned_task(finish_task)

    async def _complete_if_owned(self, entry: _PendingVoice) -> bool:
        async with self._lock:
            if self._entry_for_claim(entry.claim) is not entry:
                return False
            self._remove(entry)
            return True

    @staticmethod
    async def _run_callback(
        claim: PendingVoiceClaim,
        callback: Callable[[], Awaitable[None]],
    ) -> None:
        token = _current_voice_claim.set(claim)
        try:
            await callback()
        finally:
            _current_voice_claim.reset(token)

    @staticmethod
    async def _cancel_and_drain(
        task: asyncio.Task[None] | None,
    ) -> asyncio.CancelledError | None:
        if task is None or task is asyncio.current_task():
            return None
        task.cancel()
        return await PendingVoiceRegistry._drain((task,))

    @staticmethod
    async def _drain(
        tasks: tuple[asyncio.Task[None], ...],
    ) -> asyncio.CancelledError | None:
        if not tasks:
            return None
        drain_task = asyncio.create_task(
            PendingVoiceRegistry._consume_results(tasks),
            name="voice-handoff-drain",
        )
        _, cancellation = await _await_owned_task(drain_task)
        return cancellation

    @staticmethod
    async def _consume_results(tasks: tuple[asyncio.Task[None], ...]) -> None:
        fatal_error: BaseException | None = None
        for task in tasks:
            try:
                await task
            except (asyncio.CancelledError, Exception):
                continue
            except BaseException as error:
                fatal_error = fatal_error or error
        if fatal_error is not None:
            raise fatal_error

    @staticmethod
    def _is_fatal(error: BaseException) -> bool:
        return not isinstance(error, (asyncio.CancelledError, Exception))

    @staticmethod
    def _cancellation_result(
        entry: _PendingVoice,
        reference_id: str | None = None,
    ) -> VoiceCancellationResult:
        delete_message_ids = {entry.claim.voice_message_id}
        if reference_id is not None and reference_id != entry.claim.voice_message_id:
            delete_message_ids.clear()
        if entry.status_message_id is not None:
            delete_message_ids.add(entry.status_message_id)
        return VoiceCancellationResult(
            scope=entry.claim.scope,
            voice_message_id=entry.claim.voice_message_id,
            status_message_id=entry.status_message_id,
            delete_message_ids=frozenset(delete_message_ids),
        )

    def _entry_for_claim(self, claim: PendingVoiceClaim) -> _PendingVoice | None:
        entry = self._pending.get((claim.scope, claim.voice_message_id))
        if entry is None or entry.claim != claim:
            return None
        return entry

    def _is_excluded(
        self,
        entry: _PendingVoice,
        current_claim: PendingVoiceClaim | None,
    ) -> bool:
        return (
            entry.claim == current_claim
            or self._active_cancellations.get(entry.claim, 0) > 0
        )

    def _protect_claim(self, claim: PendingVoiceClaim | None) -> None:
        if claim is None:
            return
        self._active_cancellations[claim] = self._active_cancellations.get(claim, 0) + 1

    def _unprotect_claim(self, claim: PendingVoiceClaim | None) -> None:
        if claim is None:
            return
        remaining = self._active_cancellations[claim] - 1
        if remaining:
            self._active_cancellations[claim] = remaining
        else:
            self._active_cancellations.pop(claim)

    def _remove(self, entry: _PendingVoice) -> None:
        voice_key = (entry.claim.scope, entry.claim.voice_message_id)
        if self._pending.get(voice_key) is entry:
            self._pending.pop(voice_key)
        if entry.status_message_id is None:
            return
        status_key = (entry.claim.scope, entry.status_message_id)
        if self._pending.get(status_key) is entry:
            self._pending.pop(status_key)