[PR #433] Persist content strategy runtime state in DB-backed SharedStateBackend #740

Open
opened 2026-03-13 21:06:36 +03:00 by kerem · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/AJaySi/ALwrity/pull/433
Author: @AJaySi
Created: 3/12/2026
Status: 🔄 Open

Base: mainHead: codex/refactor-state-management-using-shared-backend


📝 Commits (1)

  • f63afe8 Persist strategy runtime state in shared DB backend

📊 Changes

6 files changed (+423 additions, -410 deletions)

View changed files

📝 backend/api/content_planning/api/content_strategy/endpoints/ai_generation_endpoints.py (+126 -381)
📝 backend/api/content_planning/api/content_strategy/endpoints/streaming_endpoints.py (+13 -29)
backend/models/content_strategy_state_models.py (+57 -0)
📝 backend/services/database.py (+1 -0)
backend/services/shared_state_backend.py (+148 -0)
backend/tests/integration/test_shared_state_backend.py (+78 -0)

📄 Description

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


🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/AJaySi/ALwrity/pull/433 **Author:** [@AJaySi](https://github.com/AJaySi) **Created:** 3/12/2026 **Status:** 🔄 Open **Base:** `main` ← **Head:** `codex/refactor-state-management-using-shared-backend` --- ### 📝 Commits (1) - [`f63afe8`](https://github.com/AJaySi/ALwrity/commit/f63afe85719de8244367db6cfa9cc3d3507fa543) Persist strategy runtime state in shared DB backend ### 📊 Changes **6 files changed** (+423 additions, -410 deletions) <details> <summary>View changed files</summary> 📝 `backend/api/content_planning/api/content_strategy/endpoints/ai_generation_endpoints.py` (+126 -381) 📝 `backend/api/content_planning/api/content_strategy/endpoints/streaming_endpoints.py` (+13 -29) ➕ `backend/models/content_strategy_state_models.py` (+57 -0) 📝 `backend/services/database.py` (+1 -0) ➕ `backend/services/shared_state_backend.py` (+148 -0) ➕ `backend/tests/integration/test_shared_state_backend.py` (+78 -0) </details> ### 📄 Description ### 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](https://chatgpt.com/codex/tasks/task_e_69b288216e788328ad4a8ff1a620949f) --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
starred/ALwrity#740
No description provided.