Spaces:
Sleeping
Sleeping
File size: 18,560 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 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 | """Atomic runtime aggregate for one messaging conversation tree."""
from __future__ import annotations
import asyncio
from dataclasses import dataclass
from uuid import uuid4
from loguru import logger
from ..models import MessageScope
from .graph import MessageTreeGraph
from .identity import TreeIdentity
from .node import MessageNode, MessageReferenceKind, MessageState
from .queue import MessageNodeQueue
from .snapshot import TreeSnapshot
from .transitions import (
AdmissionRejection,
CompletionResult,
FailureResult,
MessageSubtreeRemoval,
NodeClaim,
NodeUiTarget,
NodeView,
QueueDecision,
QueueEntry,
ReplyTarget,
TreeCancellation,
)
@dataclass(slots=True)
class _ActiveClaim:
"""Runtime execution identity kept separate from the node's UI state."""
claim: NodeClaim
cancellation_requested: bool = False
class MessageTree:
"""Own graph, queue, claim identity, and every concurrency invariant."""
def __init__(
self,
root_node: MessageNode,
*,
graph: MessageTreeGraph | None = None,
) -> None:
self._graph = graph or MessageTreeGraph(root_node)
self._queue = MessageNodeQueue()
self._lock = asyncio.Lock()
self._active: _ActiveClaim | None = None
self._restored_snapshot: TreeSnapshot | None = None
self._restored_stale_targets: tuple[NodeUiTarget, ...] = ()
logger.debug("Created MessageTree with root {}", self.root_id)
@property
def root_id(self) -> str:
return self._graph.root_id
@property
def identity(self) -> TreeIdentity:
return self._graph.identity
@property
def restored_snapshot(self) -> TreeSnapshot | None:
"""Normalized startup snapshot captured before the tree is published."""
return self._restored_snapshot
@property
def restored_stale_targets(self) -> tuple[NodeUiTarget, ...]:
"""UI targets normalized from runnable to interrupted on restore."""
return self._restored_stale_targets
def _ui_target(self, node: MessageNode) -> NodeUiTarget:
if node.status_message_id is None:
raise ValueError("Runnable node has no status message")
return NodeUiTarget(
scope=node.scope,
node_id=node.node_id,
status_message_id=node.status_message_id,
)
def _queue_entries(self) -> tuple[QueueEntry, ...]:
entries: list[QueueEntry] = []
for node_id in self._queue.items():
node = self._graph.get_node(node_id)
if node is None or node.state is not MessageState.PENDING:
continue
entries.append(
QueueEntry(node=self._ui_target(node), position=len(entries) + 1)
)
return tuple(entries)
def _claim(self, node: MessageNode) -> NodeClaim:
node.update_state(MessageState.IN_PROGRESS)
claim = NodeClaim(
identity=self.identity,
claim_id=uuid4().hex,
node=self._ui_target(node),
prompt=node.prompt,
parent_session_id=self._graph.get_parent_session_id(node.node_id),
)
self._active = _ActiveClaim(claim=claim)
return claim
def _enqueue_or_claim(self, node_id: str) -> QueueDecision:
node = self._graph.get_node(node_id)
if node is None or node.state is not MessageState.PENDING:
return QueueDecision(
claim=None,
position=None,
snapshot=None,
rejection=AdmissionRejection.DUPLICATE,
)
if self._active is None:
claim = self._claim(node)
return QueueDecision(
claim=claim,
position=None,
snapshot=self._graph.snapshot(),
)
if not self._queue.put(node_id):
return QueueDecision(
claim=None,
position=None,
snapshot=None,
rejection=AdmissionRejection.DUPLICATE,
)
position = self._queue.qsize()
logger.info("Queued node {}, position {}", node_id, position)
return QueueDecision(
claim=None,
position=position,
snapshot=self._graph.snapshot(),
)
async def enqueue_or_claim(self, node_id: str) -> QueueDecision:
"""Atomically reject, queue, or exclusively claim an existing node."""
async with self._lock:
return self._enqueue_or_claim(node_id)
async def add_and_enqueue(
self,
node_id: str,
scope: MessageScope,
prompt: str,
status_message_id: str,
parent_id: str,
parent_reference_id: str,
) -> QueueDecision:
"""Atomically add a reply and admit it to this tree."""
async with self._lock:
self._graph.add_node(
node_id=node_id,
scope=scope,
prompt=prompt,
status_message_id=status_message_id,
parent_id=parent_id,
parent_reference_id=parent_reference_id,
)
return self._enqueue_or_claim(node_id)
async def finish_and_claim_next(self, claim_id: str) -> CompletionResult:
"""Release only the matching claim and atomically select its successor."""
async with self._lock:
if self._active is None or self._active.claim.claim_id != claim_id:
return CompletionResult(
next_claim=None,
queue=self._queue_entries(),
)
self._active = None
next_claim: NodeClaim | None = None
while node_id := self._queue.pop():
node = self._graph.get_node(node_id)
if node is not None and node.state is MessageState.PENDING:
next_claim = self._claim(node)
break
return CompletionResult(
next_claim=next_claim,
queue=self._queue_entries(),
)
async def cancel_node(
self,
node_id: str,
) -> TreeCancellation:
"""Atomically cancel one active, queued, or stale runnable node."""
async with self._lock:
node = self._graph.get_node(node_id)
active_claim = (
self._active.claim
if self._active is not None
and self._active.claim.node.node_id == node_id
else None
)
if active_claim is not None:
active = self._active
if active is not None:
active.cancellation_requested = True
if node is None:
return TreeCancellation(
nodes=(),
active_claim=active_claim,
queue_update=None,
)
queue_changed = self._queue.remove(node_id)
cancelled_nodes: tuple[NodeUiTarget, ...] = ()
if node.state in (MessageState.PENDING, MessageState.IN_PROGRESS):
node.mark_error()
cancelled_nodes = (self._ui_target(node),)
elif node.state is MessageState.ERROR and active_claim is not None:
cancelled_nodes = (self._ui_target(node),)
return TreeCancellation(
nodes=cancelled_nodes,
active_claim=active_claim,
queue_update=self._queue_entries() if queue_changed else None,
)
async def cancel_all(
self,
) -> TreeCancellation:
"""Atomically cancel every runnable node present at the transition."""
async with self._lock:
cancelled_nodes: list[NodeUiTarget] = []
seen: set[str] = set()
active_claim: NodeClaim | None = None
if self._active is not None:
active_claim = self._active.claim
self._active.cancellation_requested = True
active_node = self._graph.get_node(active_claim.node.node_id)
if active_node is not None and active_node.state in (
MessageState.PENDING,
MessageState.IN_PROGRESS,
):
active_node.mark_error()
seen.add(active_node.node_id)
cancelled_nodes.append(self._ui_target(active_node))
elif active_node is not None:
seen.add(active_node.node_id)
if active_node.state is MessageState.ERROR:
cancelled_nodes.append(self._ui_target(active_node))
queued_ids = self._queue.drain()
for node_id in queued_ids:
node = self._graph.get_node(node_id)
if node is None or node.state not in (
MessageState.PENDING,
MessageState.IN_PROGRESS,
):
continue
node.mark_error()
seen.add(node_id)
cancelled_nodes.append(self._ui_target(node))
for node in self._graph.all_nodes():
if node.node_id in seen or node.state not in (
MessageState.PENDING,
MessageState.IN_PROGRESS,
):
continue
node.mark_error()
cancelled_nodes.append(self._ui_target(node))
return TreeCancellation(
nodes=tuple(cancelled_nodes),
active_claim=active_claim,
queue_update=() if queued_ids else None,
)
async def remove_message_subtree(
self,
reference_id: str,
) -> MessageSubtreeRemoval:
"""Atomically cancel and detach one literal platform reply subtree."""
async with self._lock:
resolved = self._graph.resolve_reference(reference_id)
reference_ids = tuple(self._graph.get_reference_descendants(reference_id))
if resolved is None or not reference_ids:
empty = TreeCancellation(
nodes=(),
active_claim=None,
queue_update=None,
)
return MessageSubtreeRemoval(
cancellation=empty,
removed_message_ids=frozenset(),
removed_entire_tree=False,
)
owner, reference_kind = resolved
removed_node_ids = {
candidate
for candidate in reference_ids
if self._graph.get_node(candidate) is not None
}
affected_node_ids = set(removed_node_ids)
if reference_kind is MessageReferenceKind.STATUS:
affected_node_ids.add(owner.node_id)
active_claim = (
self._active.claim
if self._active is not None
and self._active.claim.node.node_id in affected_node_ids
else None
)
if active_claim is not None:
active = self._active
if active is not None:
active.cancellation_requested = True
cancelled_nodes: list[NodeUiTarget] = []
queue_changed = False
for node_id in affected_node_ids:
node = self._graph.get_node(node_id)
if node is None:
continue
queue_changed = self._queue.remove(node_id) or queue_changed
if node.state in (MessageState.PENDING, MessageState.IN_PROGRESS):
target = self._ui_target(node)
node.mark_error()
cancelled_nodes.append(target)
elif (
node.state is MessageState.ERROR
and active_claim is not None
and active_claim.node.node_id == node_id
):
cancelled_nodes.append(self._ui_target(node))
if reference_kind is MessageReferenceKind.STATUS:
self._graph.clear_status(owner.node_id)
removed_entire_tree = self.root_id in removed_node_ids
self._graph.remove_nodes(removed_node_ids)
cancellation = TreeCancellation(
nodes=tuple(cancelled_nodes),
active_claim=active_claim,
queue_update=self._queue_entries() if queue_changed else None,
)
return MessageSubtreeRemoval(
cancellation=cancellation,
removed_message_ids=frozenset(reference_ids),
removed_entire_tree=removed_entire_tree,
)
async def record_session(
self, claim_id: str, session_id: str
) -> TreeSnapshot | None:
"""Record a real CLI session only for the currently active claim."""
async with self._lock:
if (
self._active is None
or self._active.claim.claim_id != claim_id
or self._active.cancellation_requested
):
return None
node = self._graph.get_node(self._active.claim.node.node_id)
if node is None or node.state is not MessageState.IN_PROGRESS:
return None
node.update_state(MessageState.IN_PROGRESS, session_id=session_id)
return self._graph.snapshot()
async def complete_claim(
self, claim_id: str, session_id: str | None
) -> TreeSnapshot | None:
"""Mark the currently active claim complete."""
async with self._lock:
if (
self._active is None
or self._active.claim.claim_id != claim_id
or self._active.cancellation_requested
):
return None
node = self._graph.get_node(self._active.claim.node.node_id)
if node is None or node.state not in (
MessageState.IN_PROGRESS,
MessageState.ERROR,
):
return None
node.update_state(MessageState.COMPLETED, session_id=session_id)
return self._graph.snapshot()
async def fail_claim(
self,
claim_id: str,
*,
propagate: bool,
) -> FailureResult:
"""Atomically fail the active claim and its pending descendants."""
async with self._lock:
if (
self._active is None
or self._active.claim.claim_id != claim_id
or self._active.cancellation_requested
):
return FailureResult(affected=(), queue_update=None, snapshot=None)
node = self._graph.get_node(self._active.claim.node.node_id)
if node is None:
return FailureResult(affected=(), queue_update=None, snapshot=None)
affected: list[NodeUiTarget] = []
queue_changed = False
if node.state is not MessageState.COMPLETED:
if node.state is not MessageState.ERROR:
node.mark_error()
affected.append(self._ui_target(node))
if propagate:
for descendant_id in self._graph.get_descendants(node.node_id)[1:]:
child = self._graph.get_node(descendant_id)
if child is None or child.state is not MessageState.PENDING:
continue
child.mark_error()
queue_changed = (
self._queue.remove(child.node_id) or queue_changed
)
affected.append(self._ui_target(child))
return FailureResult(
affected=tuple(affected),
queue_update=self._queue_entries() if queue_changed else None,
snapshot=self._graph.snapshot(),
)
async def resolve_reply(self, reference_id: str) -> ReplyTarget | None:
"""Resolve a node/status reference without exposing the mutable graph."""
async with self._lock:
resolved = self._graph.resolve_reference(reference_id)
if resolved is None:
return None
node, reference_kind = resolved
return ReplyTarget(
node_id=node.node_id,
reference_id=reference_id,
reference_kind=reference_kind,
queue_position=(self._queue.qsize() + 1)
if self._active is not None
else None,
)
async def node_view(self, node_id: str) -> NodeView | None:
"""Return a copied node read model."""
async with self._lock:
node = self._graph.get_node(node_id)
if node is None:
return None
return NodeView(
identity=self.identity,
node_id=node.node_id,
state=node.state,
parent_id=node.parent_id,
session_id=node.session_id,
)
async def snapshot(self) -> TreeSnapshot:
"""Capture a detached persistence snapshot under the aggregate lock."""
async with self._lock:
return self._graph.snapshot()
async def message_ids_for_chat(self, platform: str, chat_id: str) -> set[str]:
"""Copy every prompt and FCC status belonging to one platform chat."""
async with self._lock:
if self.identity.scope.platform != str(platform) or (
self.identity.scope.chat_id != str(chat_id)
):
return set()
return self._graph.all_reference_ids()
@classmethod
def from_snapshot(cls, snapshot: TreeSnapshot) -> "MessageTree":
"""Restore and reconcile interrupted nodes before publishing the tree."""
graph = MessageTreeGraph.from_snapshot(snapshot)
tree = cls(graph.get_root(), graph=graph)
stale_targets: list[NodeUiTarget] = []
for node in graph.all_nodes():
if node.state in (MessageState.PENDING, MessageState.IN_PROGRESS):
stale_targets.append(tree._ui_target(node))
node.mark_error()
tree._restored_stale_targets = tuple(stale_targets)
tree._restored_snapshot = graph.snapshot()
return tree
__all__ = ["MessageTree"]
|