Kairo-Jinkendo/backend/services/operating_transitions.py
Lars e25ff86420
Some checks failed
Deploy Development / deploy (push) Successful in 45s
Test Suite / pytest-backend (push) Failing after 1m10s
Test Suite / k6 /api/health Baseline (push) Has been skipped
Test Suite / playwright-smoke (push) Has been skipped
Test Suite / lint-backend (push) Successful in 1s
Test Suite / compose-smoke (push) Has been skipped
AP0.10a: OM-Integration — Steering-Snapshot, Transitions, Verknüpfungen
Beantwortet die Lücke zwischen parallelen Tabellen und späterem Steering Core: Graph-Read-Model, minimale Flows, sichtbarer Steuerungszustand.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-07-05 15:32:33 +02:00

148 lines
4.4 KiB
Python

"""Minimale Operating-Transitions — Vorstufe zum Steering Orchestrator (AP0.10a).
Side Effects bei Statusänderungen. Später: Hook Registry + Method Strategies.
"""
from __future__ import annotations
from typing import Any, Optional
from psycopg2.extras import RealDictCursor
from db import get_connection
RESOLVED_BLOCKER_STATUSES = frozenset({"resolved", "accepted_risk", "dismissed"})
def after_blocker_status_change(
*,
tenant_id: str,
blocker_id: str,
action_id: Optional[str],
old_status: str,
new_status: str,
user_id: Optional[str] = None,
) -> list[dict[str, Any]]:
"""Blocker geschlossen → verknüpfte blockierte Maßnahme entblocken."""
effects: list[dict[str, Any]] = []
if new_status == old_status:
return effects
if new_status not in RESOLVED_BLOCKER_STATUSES or not action_id:
return effects
conn = get_connection()
try:
with conn.cursor(cursor_factory=RealDictCursor) as cur:
cur.execute(
"""
SELECT id, status FROM actions
WHERE id = %s AND tenant_id = %s
""",
(action_id, tenant_id),
)
action = cur.fetchone()
if not action or action["status"] != "blocked":
return effects
cur.execute(
"""
SELECT COUNT(*) FROM blockers
WHERE tenant_id = %s AND action_id = %s
AND id != %s AND status IN ('open', 'in_progress')
""",
(tenant_id, action_id, blocker_id),
)
remaining = cur.fetchone()[0]
if remaining > 0:
effects.append(
{
"kind": "action_still_blocked",
"action_id": str(action_id),
"reason": "other_open_blockers",
}
)
return effects
cur.execute(
"""
UPDATE actions SET status = 'open', updated_at = NOW()
WHERE id = %s AND tenant_id = %s AND status = 'blocked'
RETURNING id
""",
(action_id, tenant_id),
)
if cur.fetchone():
effects.append(
{
"kind": "action_unblocked",
"action_id": str(action_id),
"trigger": "blocker_resolved",
"blocker_id": blocker_id,
}
)
conn.commit()
finally:
conn.close()
if effects and user_id:
from services.audit import log_audit
log_audit(
"operating_transition.action_unblocked",
user_id=user_id,
tenant_id=tenant_id,
details={"effects": effects, "blocker_id": blocker_id},
)
return effects
def after_review_status_change(
*,
tenant_id: str,
review_id: str,
action_id: Optional[str],
old_status: str,
new_status: str,
user_id: Optional[str] = None,
) -> list[dict[str, Any]]:
"""Review abgeschlossen → Maßnahme aus review_required weiterführen."""
effects: list[dict[str, Any]] = []
if new_status == old_status or new_status != "completed" or not action_id:
return effects
conn = get_connection()
try:
with conn.cursor(cursor_factory=RealDictCursor) as cur:
cur.execute(
"""
UPDATE actions
SET status = 'done', updated_at = NOW()
WHERE id = %s AND tenant_id = %s AND status = 'review_required'
RETURNING id
""",
(action_id, tenant_id),
)
if cur.fetchone():
effects.append(
{
"kind": "action_completed_after_review",
"action_id": str(action_id),
"trigger": "review_completed",
"review_id": review_id,
}
)
conn.commit()
finally:
conn.close()
if effects and user_id:
from services.audit import log_audit
log_audit(
"operating_transition.action_completed_after_review",
user_id=user_id,
tenant_id=tenant_id,
details={"effects": effects, "review_id": review_id},
)
return effects