Bin
2025-12-17 1d710f844b65d9bfdf986a71a3b924cd70598a41
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
"""
Core state management functionality for Label Studio.
 
Provides high-performance state management with caching and batch operations
that can be extended by Label Studio Enterprise with additional features.
"""
 
import logging
from datetime import datetime
from typing import Any, Dict, List, Optional, Type
 
from core.current_request import CurrentContext
from core.feature_flags import flag_set
from django.conf import settings
from django.core.cache import cache, caches
from django.db.models import Model, QuerySet
from fsm.registry import get_state_model_for_entity
from fsm.state_models import BaseState
from fsm.transition_executor import execute_transition_with_state_manager
 
logger = logging.getLogger(__name__)
 
 
_fsm_cache = None
 
 
def get_fsm_cache():
    """
    Get the cache backend for FSM operations.
 
    Uses REDIS_CACHE_ALIAS when available (LSE) to ensure FSM caching works
    even when DUMMY_CACHE is enabled. Falls back to default cache for LSO.
 
    The result is memoized so the cache lookup only happens once per process.
 
    Returns:
        Cache backend instance
    """
    global _fsm_cache
    if _fsm_cache is not None:
        return _fsm_cache
 
    redis_cache_alias = getattr(settings, 'REDIS_CACHE_ALIAS', None)
    if redis_cache_alias and redis_cache_alias in settings.CACHES:
        _fsm_cache = caches[redis_cache_alias]
    else:
        _fsm_cache = cache
    return _fsm_cache
 
 
class StateManagerError(Exception):
    """Base exception for StateManager operations"""
 
    pass
 
 
class InvalidTransitionError(StateManagerError):
    """Raised when an invalid state transition is attempted"""
 
    pass
 
 
class StateManager:
    """
    Core state management system for Label Studio.
 
    Provides the foundation for state management that can be extended
    by Label Studio Enterprise with additional features like:
    - Advanced caching strategies
    - Bulk operations optimization
    - Complex transition validation
    - Enterprise-specific state models
 
    Features:
    - INSERT-only architecture with UUID7 for maximum performance
    - Basic caching for current state lookups
    - Simple state transitions with audit trails
    - Extensible design for enterprise features
    """
 
    CACHE_TTL = getattr(settings, 'FSM_CACHE_TTL', 300)  # 5 minutes default
    CACHE_PREFIX = 'fsm:current'
 
    @classmethod
    def clear_fsm_cache(cls):
        """
        Clear all FSM-related cache keys.
 
        Uses delete_pattern if available (django-redis), otherwise logs a warning.
        This is primarily used for test isolation.
        """
        fsm_cache = get_fsm_cache()
        pattern = f'{cls.CACHE_PREFIX}:*'
        if hasattr(fsm_cache, 'delete_pattern'):
            fsm_cache.delete_pattern(pattern)
        else:
            logger.warning(
                'FSM cache clear requested but cache backend does not support delete_pattern. '
                'FSM cache keys may persist.'
            )
 
    @classmethod
    def _is_fsm_enabled(cls, user='auto') -> bool:
        if user == 'auto':
            user = CurrentContext.get_user()
        """Check if FSM feature is enabled via feature flag."""
        return flag_set('fflag_feat_fit_568_finite_state_management', user=user)
 
    @classmethod
    def get_cache_key(cls, entity: Model) -> str:
        """Generate cache key for entity's current state"""
        return f'{cls.CACHE_PREFIX}:{entity._meta.label_lower}:{entity.pk}'
 
    @classmethod
    def get_current_state_value(cls, entity: Model) -> Optional[str]:
        """
        Get current state with basic caching.
 
        Args:
            entity: The entity to get current state for
 
        Returns:
            Current state string
        Raises:
            StateManagerError: If no state model found
 
        Example:
            task = Task.objects.get(id=123)
            current_state = StateManager.get_current_state_value(task)
            if current_state == 'COMPLETED':
                # Task is finished
                pass
        """
        if not cls._is_fsm_enabled():
            return None  # Feature disabled, return no state
 
        cache_key = cls.get_cache_key(entity)
        fsm_cache = get_fsm_cache()
 
        # Try cache first
        cached_state = fsm_cache.get(cache_key)
        if cached_state is not None:
            logger.info(
                'FSM: Cache hit',
                extra={
                    'event': 'fsm.cache_hit',
                    'entity_type': entity._meta.label_lower,
                    'entity_id': entity.pk,
                    'organization_id': CurrentContext.get_organization_id(),
                    'state': cached_state,
                },
            )
            return cached_state
 
        # Query database using state model registry
        state_model = get_state_model_for_entity(entity)
        if not state_model:
            raise StateManagerError(f'No state model found for {entity._meta.model_name} when getting current state')
 
        try:
            current_state = state_model.get_current_state_value(entity)
 
            # Cache result
            if current_state is not None:
                fsm_cache.set(cache_key, current_state, cls.CACHE_TTL)
                logger.info(
                    'FSM: Cache miss',
                    extra={
                        'event': 'fsm.cache_miss',
                        'entity_type': entity._meta.label_lower,
                        'entity_id': entity.pk,
                        'organization_id': CurrentContext.get_organization_id(),
                    },
                )
 
            return current_state
 
        except Exception as e:
            logger.error(
                'FSM: Error getting current state',
                extra={
                    'event': 'fsm.get_state_error',
                    'entity_type': entity._meta.label_lower,
                    'entity_id': entity.pk,
                    'organization_id': CurrentContext.get_organization_id(),
                    'error': str(e),
                },
                exc_info=True,
            )
            raise StateManagerError(f'Error getting current state: {e}') from e
 
    @classmethod
    def get_current_state_object(cls, entity: Model) -> BaseState:
        """
        Get current state object with full audit information.
 
        Args:
            entity: The entity to get current state object for
 
        Returns:
            Latest BaseState instance
 
        Raises:
            StateManagerError: If no state model found
        """
        state_model = get_state_model_for_entity(entity)
        if not state_model:
            raise StateManagerError(
                f'No state model found for {entity._meta.model_name} when getting current state object'
            )
 
        return state_model.get_current_state(entity)
 
    @classmethod
    def transition_state(
        cls,
        entity: Model,
        new_state: str,
        transition_name: str = None,
        user=None,
        organization_id=None,
        context: Dict[str, Any] = None,
        reason: str = '',
        force_state_record: bool = False,
    ) -> bool:
        """
        Perform state transition with audit trail.
 
        Uses INSERT-only approach for maximum performance:
        - No UPDATE operations or row locks
        - Complete audit trail by design
        - Basic cache update for consistency
 
        Args:
            entity: The entity to transition
            new_state: Target state
            transition_name: Name of transition method (for audit)
            user: User triggering the transition
            organization_id: Organization ID
            context: Additional context data
            reason: Human-readable reason for transition
            force_state_record: If True, creates state record even if state doesn't change (for audit trails)
 
        Returns:
            True if transition succeeded, False otherwise
 
        Raises:
            InvalidTransitionError: If transition is not valid
            StateManagerError: If transition fails
 
        Example:
            success = StateManager.transition_state(
                entity=task,
                new_state='IN_PROGRESS',
                transition_name='start_annotation',
                user=request.user,
                organization_id=request.user.active_organization_id,
                context={'assignment_id': assignment.id},
                reason='User started annotation work'
            )
        """
        if not cls._is_fsm_enabled(user=user):
            return True  # Feature disabled, silently succeed
 
        # Skip if FSM is temporarily disabled (e.g., during cleanup or bulk operations)
        if CurrentContext.is_fsm_disabled():
            return True  # FSM disabled, silently succeed
 
        state_model = get_state_model_for_entity(entity)
        if not state_model:
            raise StateManagerError(f'No state model found for {entity._meta.model_name} when transitioning state')
 
        current_state = cls.get_current_state_value(entity)
 
        # Prevent same-state transitions - only create state records for actual state changes
        # This avoids creating redundant data when the effective state doesn't change
        # However, allow forced state records for audit trails (e.g., annotation updates)
        # IMPORTANT: Also check if a state record exists in DB - if not, we must create one
        # even if inferred state matches target state (to persist the inferred state)
        if current_state == new_state and not force_state_record:
            # Verify a state record actually exists in DB (not just inferred)
            state_record_exists = state_model.objects.filter(**{entity._meta.model_name: entity}).exists()
            if state_record_exists:
                return True  # Skip transition - record exists and state unchanged
            # else: No record exists (state was inferred), continue to create record
 
        # Optimistic concurrency control using cache-based locking
        cache_key = cls.get_cache_key(entity)
        lock_key = f'{cache_key}:lock'
        fsm_cache = get_fsm_cache()
 
        if organization_id is None:
            organization_id = CurrentContext.get_organization_id()
 
        try:
            # Try to acquire an optimistic lock using cache add (atomic operation)
            # add() only succeeds if the key doesn't exist
            lock_acquired = fsm_cache.add(lock_key, 'locked', timeout=5)  # 5 second timeout
 
            if not lock_acquired:
                # Another process is currently transitioning this entity
                logger.info(
                    'FSM: Concurrent transition detected, skipping',
                    extra={
                        'event': 'fsm.concurrent_transition_skipped',
                        'entity_type': entity._meta.label_lower,
                        'entity_id': entity.pk,
                        'target_state': new_state,
                        'organization_id': organization_id,
                    },
                )
                return True
 
            try:
                # INSERT-only approach - no UPDATE operations needed
                # Get denormalized fields from the state model class
                denormalized_fields = state_model.get_denormalized_fields(entity)
 
                # Get organization from entity or denormalized fields, or user's active organization
                if organization_id is None:
                    organization_id = getattr(
                        entity, 'organization_id', getattr(denormalized_fields, 'organization_id', None)
                    )
                    if organization_id is not None:
                        CurrentContext.set_organization_id(organization_id)
 
                if not organization_id and user and hasattr(user, 'active_organization') and user.active_organization:
                    organization_id = user.active_organization.id
                    if organization_id is not None:
                        CurrentContext.set_organization_id(organization_id)
 
                logger.info(
                    'FSM: State transition starting',
                    extra={
                        'event': 'fsm.transition_state_start',
                        'entity_type': entity._meta.label_lower,
                        'entity_id': entity.pk,
                        'from_state': current_state,
                        'to_state': new_state,
                        'transition_name': transition_name,
                        **{
                            'user_id': user.id if user else None,
                            'organization_id': organization_id if organization_id else None,
                        },
                    },
                )
 
                # CRITICAL FIX: Use state model's correct field name instead of entity._meta.model_name
                # This fixes the architectural entity field mapping issue where entity._meta.model_name
                # doesn't always match the actual field name defined in FSM state models
                entity_field_name = state_model._get_entity_field_name()
 
                new_state_record = state_model.objects.create(
                    **{entity_field_name: entity},
                    state=new_state,
                    previous_state=current_state,
                    transition_name=transition_name,
                    triggered_by=user,
                    context_data=context or {},
                    reason=reason,
                    organization_id=organization_id,
                    **denormalized_fields,
                )
 
                # Write-through cache: Update immediately
                # This ensures the cache is updated atomically with the database
                fsm_cache.set(cache_key, new_state, cls.CACHE_TTL)
 
                logger.info(
                    'FSM: Cache updated for transition state',
                    extra={
                        'event': 'fsm.transition_state_cache_updated',
                        'entity_type': entity._meta.label_lower,
                        'entity_id': entity.pk,
                        'state': new_state,
                        **{
                            'user_id': user.id if user else None,
                            'organization_id': organization_id if organization_id else None,
                        },
                    },
                )
 
                logger.info(
                    'FSM: State transition successful',
                    extra={
                        'event': 'fsm.transition_state_success',
                        'entity_type': entity._meta.label_lower,
                        'entity_id': entity.pk,
                        'state': new_state,
                        'state_record_id': str(new_state_record.id),
                        **{
                            'user_id': user.id if user else None,
                            'organization_id': organization_id if organization_id else None,
                        },
                    },
                )
                return True
 
            finally:
                # Always release the lock, regardless of success or failure
                fsm_cache.delete(lock_key)
 
        except Exception as e:
            # On failure, clean up lock and invalidate potentially stale cache
            fsm_cache.delete(lock_key)
            fsm_cache.delete(cache_key)
 
            # Get organization_id for error logging if it wasn't set earlier
            organization_id = CurrentContext.get_organization_id()
 
            logger.error(
                'FSM: State transition failed',
                extra={
                    'event': 'fsm.transition_state_failed',
                    'entity_type': entity._meta.label_lower,
                    'entity_id': entity.pk,
                    'from_state': current_state,
                    'to_state': new_state,
                    'error': str(e),
                    **{
                        'user_id': user.id if user else None,
                        'organization_id': organization_id if organization_id else None,
                    },
                },
                exc_info=True,
            )
            raise StateManagerError(f'Failed to transition state: {e}') from e
 
    @classmethod
    def get_state_history(cls, entity: Model) -> QuerySet[BaseState]:
        """
        Get complete state history for an entity.
 
        Args:
            entity: Entity to get history for
 
        Returns:
            QuerySet of state records ordered by most recent first
        """
        state_model = get_state_model_for_entity(entity)
        if not state_model:
            raise StateManagerError(
                f'No state model registered for {entity._meta.model_name} when getting state history'
            )
 
        return state_model.get_state_history(entity)
 
    @classmethod
    def get_states_in_time_range(
        cls, entity: Model, start_time: datetime, end_time: Optional[datetime] = None
    ) -> List[BaseState]:
        """
        Get states within a time range using UUID7 time-based queries.
 
        Args:
            entity: Entity to get states for
            start_time: Start of time range
            end_time: End of time range (defaults to now)
 
        Returns:
            List of states within the time range
        """
        state_model = get_state_model_for_entity(entity)
        if not state_model:
            raise StateManagerError(
                f'No state model registered for {entity._meta.model_name} when getting states in time range'
            )
 
        return state_model.get_states_in_range(entity, start_time, end_time or datetime.now())
 
    @classmethod
    def invalidate_cache(cls, entity: Model):
        """Invalidate cached state for an entity"""
        cache_key = cls.get_cache_key(entity)
        fsm_cache = get_fsm_cache()
        fsm_cache.delete(cache_key)
        organization_id = CurrentContext.get_organization_id()
        logger.info(
            'FSM: Cache invalidated',
            extra={
                'event': 'fsm.cache_invalidated',
                'entity_type': entity._meta.label_lower,
                'entity_id': entity.pk,
                **{'organization_id': organization_id if organization_id else None},
            },
        )
 
    @classmethod
    def warm_cache(cls, entities: List[Model]):
        """
        Warm cache with current states for a list of entities.
 
        Basic implementation that can be optimized by Enterprise with
        bulk queries and advanced caching strategies.
        """
        cache_updates = {}
        organization_id = CurrentContext.get_organization_id()
        for entity in entities:
            current_state = cls.get_current_state_value(entity)
            if current_state:
                cache_key = cls.get_cache_key(entity)
                cache_updates[cache_key] = current_state
 
        if cache_updates:
            fsm_cache = get_fsm_cache()
            fsm_cache.set_many(cache_updates, cls.CACHE_TTL)
            logger.info(
                'FSM: Cache warmed',
                extra={
                    'event': 'fsm.cache_warmed',
                    'entity_count': len(cache_updates),
                    **{'organization_id': organization_id if organization_id else None},
                },
            )
 
    @classmethod
    def execute_transition(
        cls, entity: Model, transition_name: str, transition_data: Dict[str, Any] = None, user=None, **context_kwargs
    ) -> BaseState:
        """
        Execute a registered transition by name.
 
        This is the main entry point for all state transitions using the declarative system.
        Enterprise implementations can override this method to add additional behavior.
 
        Args:
            entity: The entity to transition
            transition_name: Name of the registered transition
            transition_data: Data for the transition (validated by Pydantic)
            user: User executing the transition
            **context_kwargs: Additional context data
 
        Returns:
            The newly created state record
 
        Raises:
            ValueError: If transition is not found
            TransitionValidationError: If transition validation fails
        """
        # Delegate to transition executor, passing StateManager methods as parameters
        return execute_transition_with_state_manager(
            entity=entity,
            transition_name=transition_name,
            transition_data=transition_data,
            user=user,
            state_manager_class=cls,
            **context_kwargs,
        )
 
 
# Allow runtime configuration of which StateManager to use
# Enterprise can set this to their extended implementation
DEFAULT_STATE_MANAGER = StateManager
RESOLVED_STATE_MANAGER = None
 
 
def get_state_manager() -> Type[StateManager]:
    """
    Get the configured state manager class.
 
    Returns the StateManager class to use. Enterprise can override
    this by setting a different class in their configuration.
    """
    # Resolve once
    if RESOLVED_STATE_MANAGER is not None:
        return RESOLVED_STATE_MANAGER
 
    # Check if enterprise has configured a custom state manager
    if hasattr(settings, 'FSM_STATE_MANAGER_CLASS'):
        manager_path = settings.FSM_STATE_MANAGER_CLASS
        module_name, class_name = manager_path.rsplit('.', 1)
        module = __import__(module_name, fromlist=[class_name])
        return getattr(module, class_name)
 
    return DEFAULT_STATE_MANAGER