Skip to content

Persist content strategy runtime state in DB-backed SharedStateBackend#433

Open
AJaySi wants to merge 1 commit intomainfrom
codex/refactor-state-management-using-shared-backend
Open

Persist content strategy runtime state in DB-backed SharedStateBackend#433
AJaySi wants to merge 1 commit intomainfrom
codex/refactor-state-management-using-shared-backend

Conversation

@AJaySi
Copy link
Owner

@AJaySi AJaySi commented Mar 12, 2026

Motivation

  • Remove fragile in-memory state patterns used for AI polling/streaming and make runtime state robust across workers.
  • Ensure tenant isolation by keying runtime records by authenticated user_id plus task/resource/cache identifiers.
  • Add TTL/cleanup and clear not-found semantics so expired tasks/cache entries are handled consistently.

Description

  • Introduces a DB-backed SharedStateBackend service (backend/services/shared_state_backend.py) implementing task lifecycle, latest-strategy refs, and short-lived streaming cache with TTL and cleanup.
  • Adds SQLAlchemy models StrategyGenerationTaskState, LatestGeneratedStrategyState, and StreamingCacheState in backend/models/content_strategy_state_models.py (tenant-scoped keys + unique indexes).
  • Replaces in-memory holders in endpoints: polling flow in ai_generation_endpoints.py now uses authenticated current_user, writes/reads task status via SharedStateBackend, persists latest strategy refs, and returns robust 404s for expired/missing tasks.
  • Replaces streaming_cache in streaming_endpoints.py with the shared DB cache (SharedStateBackend) for strategic intelligence and keyword research flows, and registers the new models in DB init (backend/services/database.py) so tables are created.

Testing

  • Static checks: python -m py_compile backend/services/shared_state_backend.py backend/api/content_planning/api/content_strategy/endpoints/ai_generation_endpoints.py backend/api/content_planning/api/content_strategy/endpoints/streaming_endpoints.py backend/models/content_strategy_state_models.py backend/tests/integration/test_shared_state_backend.py succeeded.
  • Integration: ran cd backend && PYTHONPATH=/workspace/ALwrity/backend pytest -q tests/integration/test_shared_state_backend.py which exercises multi-worker semantics (separate SQLAlchemy sessions), TTL expiry and not-found behavior; all tests passed (4 passed).
  • Notes: test run emits existing environment/dependency and SQLAlchemy deprecation warnings but the new integration tests passed.

Codex Task

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant