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
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
"""Public facade for atomic messaging tree aggregates."""

import asyncio
from collections.abc import Callable, Coroutine
from typing import Any

from loguru import logger

from ..models import IncomingMessage, MessageScope
from .identity import TreeIdentity
from .node import MessageNode, MessageState
from .processor import (
    CancelledTask,
    NodeProcessor,
    NodeStartedCallback,
    QueueUpdateCallback,
    TreeQueueProcessor,
)
from .repository import TreeRepository
from .runtime import MessageTree
from .snapshot import ConversationSnapshot, TreeSnapshot
from .transitions import (
    AdmissionRejection,
    CancellationEffect,
    CancellationReason,
    CancellationResult,
    CancellationUiOwner,
    FailureResult,
    MessageSubtreeRemovalResult,
    NodeClaim,
    NodeUiTarget,
    NodeView,
    QueueDecision,
    ReplyTarget,
    TreeCancellation,
)

CANCEL_TASK_DRAIN_TIMEOUT_S = 5.0


async def _finish_transition[T](awaitable: Coroutine[Any, Any, T]) -> T:
    """Deliver a transition result even if its caller is cancelled mid-commit."""
    task = asyncio.create_task(awaitable)
    current = asyncio.current_task()
    while True:
        try:
            return await asyncio.shield(task)
        except asyncio.CancelledError:
            if task.done():
                return task.result()
            if current is not None:
                while current.cancelling():
                    current.uncancel()


async def _drain_cancelled_tasks(tasks: list[asyncio.Task[None]]) -> None:
    """Wait briefly for cancelled claims to finish aggregate cleanup."""
    if not tasks:
        return
    done, pending = await asyncio.wait(
        set(tasks),
        timeout=CANCEL_TASK_DRAIN_TIMEOUT_S,
    )
    if pending:
        logger.warning(
            "Timed out waiting for {} cancelled messaging task(s) to finish cleanup",
            len(pending),
        )
    for task in done:
        if task.cancelled():
            continue
        try:
            task.result()
        except Exception as exc:
            logger.debug(
                "Cancelled messaging task finished with {}", type(exc).__name__
            )


class TreeQueueManager:
    """Locate aggregates and coordinate tasks without exposing mutable trees."""

    def __init__(
        self,
        node_processor: NodeProcessor,
        *,
        queue_update_callback: QueueUpdateCallback | None = None,
        node_started_callback: NodeStartedCallback | None = None,
        unexpected_failure_callback: Callable[[FailureResult], None] | None = None,
        log_messaging_error_details: bool = False,
        _repository: TreeRepository | None = None,
        _restored_snapshot: ConversationSnapshot | None = None,
        _restored_stale_targets: tuple[NodeUiTarget, ...] = (),
    ) -> None:
        self._repository = _repository or TreeRepository()
        self._lock = asyncio.Lock()
        self._processor = TreeQueueProcessor(
            node_processor,
            claim_failure_callback=self._handle_processor_failure,
            claim_finished_callback=self._finish_claim,
            queue_update_callback=queue_update_callback,
            node_started_callback=node_started_callback,
            log_messaging_error_details=log_messaging_error_details,
        )
        self._restored_snapshot = _restored_snapshot
        self._restored_stale_targets = _restored_stale_targets
        self._unexpected_failure_callback = unexpected_failure_callback
        logger.info("TreeQueueManager initialized")

    @property
    def restored_snapshot(self) -> ConversationSnapshot | None:
        return self._restored_snapshot

    @property
    def restored_stale_targets(self) -> tuple[NodeUiTarget, ...]:
        return self._restored_stale_targets

    async def admit(
        self,
        incoming: IncomingMessage,
        status_message_id: str,
        *,
        parent_reference_id: str | None = None,
    ) -> QueueDecision:
        """Publish one admission before its processor can begin."""
        node_id = str(incoming.message_id)
        scope = incoming.scope
        prompt = incoming.text or ""
        async with self._lock:
            duplicate_tree = self._repository.get_tree_for_reference(scope, node_id)
            if duplicate_tree is None:
                duplicate_tree = self._repository.get_tree_for_reference(
                    scope,
                    status_message_id,
                )
            if duplicate_tree is not None:
                return await duplicate_tree.enqueue_or_claim(node_id)

            tree: MessageTree | None = None
            resolved_parent: ReplyTarget | None = None
            if parent_reference_id is not None:
                tree = self._repository.get_tree_for_reference(
                    scope, parent_reference_id
                )
                if tree is not None:
                    resolved_parent = await tree.resolve_reply(parent_reference_id)

                if tree is None or resolved_parent is None:
                    return QueueDecision(
                        claim=None,
                        position=None,
                        snapshot=None,
                        rejection=AdmissionRejection.PARENT_REMOVED,
                    )

            if tree is not None and resolved_parent is not None:
                decision = await tree.add_and_enqueue(
                    node_id,
                    scope,
                    prompt,
                    status_message_id,
                    resolved_parent.node_id,
                    resolved_parent.reference_id,
                )
                self._repository.register_node(
                    identity=tree.identity,
                    node_id=node_id,
                    status_message_id=status_message_id,
                )
                logger.info("Added node {} to tree {}", node_id, tree.identity)
            else:
                root = MessageNode(
                    node_id=node_id,
                    scope=scope,
                    prompt=prompt,
                    status_message_id=status_message_id,
                    state=MessageState.PENDING,
                )
                tree = MessageTree(root)
                decision = await tree.enqueue_or_claim(node_id)
                self._repository.add_tree(
                    tree,
                    node_id=node_id,
                    status_message_id=status_message_id,
                )
                logger.info("Created new tree {}", tree.identity)

        if decision.claim is not None:
            self._processor.launch(tree, decision.claim)
        return decision

    async def resolve_reply(
        self,
        scope: MessageScope,
        reference_id: str,
    ) -> ReplyTarget | None:
        """Resolve a scoped node or status-message reference."""
        async with self._lock:
            tree = self._repository.get_tree_for_reference(scope, reference_id)
            return await tree.resolve_reply(reference_id) if tree is not None else None

    async def resolve_node_id(
        self,
        scope: MessageScope,
        reference_id: str,
    ) -> str | None:
        target = await self.resolve_reply(scope, reference_id)
        return target.node_id if target is not None else None

    async def get_node(
        self,
        scope: MessageScope,
        reference_id: str,
    ) -> NodeView | None:
        """Return an immutable node read model."""
        async with self._lock:
            tree = self._repository.get_tree_for_reference(scope, reference_id)
            if tree is None:
                return None
            target = await tree.resolve_reply(reference_id)
            return await tree.node_view(target.node_id) if target is not None else None

    async def record_session(
        self,
        claim: NodeClaim,
        session_id: str,
    ) -> TreeSnapshot | None:
        async with self._lock:
            tree = self._repository.get_tree(claim.identity)
            return (
                await tree.record_session(claim.claim_id, session_id)
                if tree is not None
                else None
            )

    async def complete_claim(
        self,
        claim: NodeClaim,
        session_id: str | None,
    ) -> TreeSnapshot | None:
        async with self._lock:
            tree = self._repository.get_tree(claim.identity)
            return (
                await tree.complete_claim(claim.claim_id, session_id)
                if tree is not None
                else None
            )

    async def fail_claim(
        self,
        claim: NodeClaim,
        *,
        propagate: bool = True,
    ) -> FailureResult:
        return await self._fail_claim(claim, propagate=propagate)

    async def _fail_claim(
        self,
        claim: NodeClaim,
        *,
        propagate: bool,
    ) -> FailureResult:
        async with self._lock:
            tree = self._repository.get_tree(claim.identity)
            result = (
                await tree.fail_claim(
                    claim.claim_id,
                    propagate=propagate,
                )
                if tree is not None
                else FailureResult(affected=(), queue_update=None, snapshot=None)
            )
        if result.queue_update is not None:
            await self._processor.notify_queue_updated(result.queue_update)
        return result

    async def _handle_processor_failure(
        self,
        claim: NodeClaim,
    ) -> None:
        """Route an escaped runner failure through manager-owned effects."""
        result = await self._fail_claim(claim, propagate=True)
        if self._unexpected_failure_callback is None:
            return
        try:
            self._unexpected_failure_callback(result)
        except Exception as exc:
            logger.warning(
                "Unexpected messaging failure callback failed: {}",
                type(exc).__name__,
            )

    async def _finish_claim(self, tree: MessageTree, claim: NodeClaim) -> None:
        """Serialize successor task publication with aggregate detachment."""
        async with self._lock:
            tree_is_published = self._repository.get_tree(claim.identity) is tree
            completion = await tree.finish_and_claim_next(claim.claim_id)
            if tree_is_published and completion.next_claim is not None:
                self._processor.launch(
                    tree,
                    completion.next_claim,
                    announce_started=True,
                    queue=completion.queue,
                )

    @staticmethod
    def _external_effects(
        transition: TreeCancellation,
        cancelled_task: CancelledTask | None,
    ) -> tuple[CancellationEffect, ...]:
        active_node_id = (
            transition.active_claim.node.node_id
            if transition.active_claim is not None
            else None
        )
        return tuple(
            CancellationEffect(
                node=node,
                ui_owner=(
                    CancellationUiOwner.RUNNER
                    if node.node_id == active_node_id
                    and cancelled_task is not None
                    and cancelled_task.runner_started
                    else CancellationUiOwner.WORKFLOW
                ),
            )
            for node in transition.nodes
        )

    async def _current_snapshot(
        self,
        identity: TreeIdentity,
        expected_tree: MessageTree,
    ) -> TreeSnapshot | None:
        async with self._lock:
            tree = self._repository.get_tree(identity)
            if tree is not expected_tree:
                return None
            return await tree.snapshot()

    async def cancel_node(
        self,
        scope: MessageScope,
        node_id: str,
        *,
        reason: CancellationReason | None = None,
    ) -> CancellationResult:
        return await _finish_transition(self._cancel_node(scope, node_id, reason))

    async def _cancel_node(
        self,
        scope: MessageScope,
        node_id: str,
        reason: CancellationReason | None,
    ) -> CancellationResult:
        async with self._lock:
            tree = self._repository.get_tree_for_reference(scope, node_id)
            if tree is None:
                return CancellationResult()
            target = await tree.resolve_reply(node_id)
            if target is None:
                return CancellationResult()
            transition = await tree.cancel_node(target.node_id)
            cancelled_task = (
                self._processor.cancel(transition.active_claim, reason)
                if transition.active_claim is not None
                else None
            )

        if transition.queue_update is not None:
            await self._processor.notify_queue_updated(transition.queue_update)
        if cancelled_task is not None:
            await _drain_cancelled_tasks([cancelled_task.task])
        snapshot = await self._current_snapshot(tree.identity, tree)
        return CancellationResult(
            effects=self._external_effects(transition, cancelled_task),
            snapshots=(snapshot,) if snapshot is not None else (),
        )

    async def cancel_all(
        self,
        *,
        reason: CancellationReason | None = None,
    ) -> CancellationResult:
        return await _finish_transition(self._cancel_all(reason))

    async def _cancel_all(
        self,
        reason: CancellationReason | None,
    ) -> CancellationResult:
        transitions: list[
            tuple[MessageTree, TreeCancellation, CancelledTask | None]
        ] = []
        async with self._lock:
            for tree in self._repository.trees():
                transition = await tree.cancel_all()
                cancelled_task = (
                    self._processor.cancel(transition.active_claim, reason)
                    if transition.active_claim is not None
                    else None
                )
                transitions.append((tree, transition, cancelled_task))

        for _tree, transition, _task in transitions:
            if transition.queue_update is not None:
                await self._processor.notify_queue_updated(transition.queue_update)
        await _drain_cancelled_tasks(
            [
                cancelled.task
                for _tree, _transition, cancelled in transitions
                if cancelled is not None
            ]
        )

        effects: list[CancellationEffect] = []
        snapshots: list[TreeSnapshot] = []
        for tree, transition, cancelled_task in transitions:
            effects.extend(self._external_effects(transition, cancelled_task))
            snapshot = await self._current_snapshot(tree.identity, tree)
            if snapshot is not None:
                snapshots.append(snapshot)
        return CancellationResult(effects=tuple(effects), snapshots=tuple(snapshots))

    async def clear_scope(
        self,
        scope: MessageScope,
        *,
        reason: CancellationReason | None = None,
    ) -> CancellationResult:
        """Atomically detach one chat's trees, then drain their active tasks."""
        return await _finish_transition(self._clear_scope(scope, reason))

    async def _clear_scope(
        self,
        scope: MessageScope,
        reason: CancellationReason | None,
    ) -> CancellationResult:
        transitions: list[tuple[TreeCancellation, CancelledTask | None]] = []
        async with self._lock:
            for tree in self._repository.trees():
                if tree.identity.scope != scope:
                    continue
                transition = await tree.cancel_all()
                cancelled_task = (
                    self._processor.cancel(transition.active_claim, reason)
                    if transition.active_claim is not None
                    else None
                )
                transitions.append((transition, cancelled_task))
                self._repository.remove_tree(tree.identity)

        await _drain_cancelled_tasks(
            [
                cancelled.task
                for _transition, cancelled in transitions
                if cancelled is not None
            ]
        )
        effects: list[CancellationEffect] = []
        for transition, cancelled_task in transitions:
            effects.extend(self._external_effects(transition, cancelled_task))
        return CancellationResult(effects=tuple(effects))

    async def remove_message_subtree(
        self,
        scope: MessageScope,
        reference_id: str,
        *,
        reason: CancellationReason | None = None,
    ) -> MessageSubtreeRemovalResult:
        """Atomically cancel, detach, and unindex one literal reply subtree."""
        return await _finish_transition(
            self._remove_message_subtree(scope, reference_id, reason)
        )

    async def _remove_message_subtree(
        self,
        scope: MessageScope,
        reference_id: str,
        reason: CancellationReason | None,
    ) -> MessageSubtreeRemovalResult:
        async with self._lock:
            tree = self._repository.get_tree_for_reference(scope, reference_id)
            if tree is None:
                return MessageSubtreeRemovalResult(
                    cancellation=CancellationResult(),
                    removed_tree_identity=None,
                    delete_message_ids=frozenset(),
                    tree_matched=False,
                )
            target = await tree.resolve_reply(reference_id)
            if target is None:
                return MessageSubtreeRemovalResult(
                    cancellation=CancellationResult(),
                    removed_tree_identity=None,
                    delete_message_ids=frozenset(),
                    tree_matched=False,
                )
            identity = tree.identity
            transition = await tree.remove_message_subtree(target.reference_id)
            lookup_ids = set(transition.removed_message_ids)
            if transition.removed_entire_tree:
                self._repository.remove_tree(identity)
            else:
                self._repository.unregister_references(identity, lookup_ids)
            cancelled_task = (
                self._processor.cancel(transition.cancellation.active_claim, reason)
                if transition.cancellation.active_claim is not None
                else None
            )

        if transition.cancellation.queue_update is not None:
            await self._processor.notify_queue_updated(
                transition.cancellation.queue_update
            )
        if cancelled_task is not None:
            await _drain_cancelled_tasks([cancelled_task.task])
        snapshot = (
            None
            if transition.removed_entire_tree
            else await self._current_snapshot(identity, tree)
        )
        cancellation = CancellationResult(
            effects=self._external_effects(
                transition.cancellation,
                cancelled_task,
            ),
            snapshots=(snapshot,) if snapshot is not None else (),
        )
        return MessageSubtreeRemovalResult(
            cancellation=cancellation,
            removed_tree_identity=(
                identity if transition.removed_entire_tree else None
            ),
            delete_message_ids=transition.removed_message_ids,
            tree_matched=True,
        )

    def get_tree_count(self) -> int:
        return self._repository.tree_count()

    def task_count(self) -> int:
        return self._processor.task_count()

    async def wait_idle(self) -> None:
        """Wait until every processor-owned claim has finished cleanup."""
        await self._processor.wait_idle()

    async def get_message_ids_for_chat(self, platform: str, chat_id: str) -> set[str]:
        async with self._lock:
            message_ids: set[str] = set()
            for tree in self._repository.trees():
                message_ids.update(await tree.message_ids_for_chat(platform, chat_id))
            return message_ids

    async def snapshot(self) -> ConversationSnapshot:
        async with self._lock:
            trees: dict[TreeIdentity, TreeSnapshot] = {}
            for tree in self._repository.trees():
                trees[tree.identity] = await tree.snapshot()
            return ConversationSnapshot(trees=trees)

    @classmethod
    def from_snapshot(
        cls,
        snapshot: ConversationSnapshot,
        node_processor: NodeProcessor,
        *,
        queue_update_callback: QueueUpdateCallback | None = None,
        node_started_callback: NodeStartedCallback | None = None,
        unexpected_failure_callback: Callable[[FailureResult], None] | None = None,
        log_messaging_error_details: bool = False,
    ) -> "TreeQueueManager":
        repository, normalized, stale_targets = TreeRepository.from_snapshot(snapshot)
        return cls(
            node_processor,
            queue_update_callback=queue_update_callback,
            node_started_callback=node_started_callback,
            unexpected_failure_callback=unexpected_failure_callback,
            log_messaging_error_details=log_messaging_error_details,
            _repository=repository,
            _restored_snapshot=normalized,
            _restored_stale_targets=stale_targets,
        )


__all__ = ["TreeQueueManager"]