All checks were successful
Deploy Development / deploy (push) Successful in 48s
Test Suite / pytest-backend (push) Successful in 2m20s
Test Suite / lint-backend (push) Successful in 3s
Test Suite / compose-smoke (push) Has been skipped
Test Suite / k6 /api/health Baseline (push) Successful in 19s
Test Suite / playwright-smoke (push) Successful in 14s
Migration 023, Engine, API und ADP für Durchführungsplan getrennt vom Gate-Graph; Docs und Sprint-Assignment synchronisiert. Co-authored-by: Cursor <cursoragent@cursor.com>
226 lines
6.9 KiB
Python
226 lines
6.9 KiB
Python
"""Execution plan — Action dependencies CRUD (AP1.16a/b)."""
|
|
|
|
from __future__ import annotations
|
|
|
|
from typing import Any, Literal, Optional
|
|
|
|
from psycopg2.extras import RealDictCursor
|
|
|
|
from db import get_connection
|
|
from services.audit import log_audit
|
|
from services.actions import get_action
|
|
|
|
DependencyKind = Literal["requires", "blocks", "relates"]
|
|
|
|
DEPENDENCY_KINDS = frozenset({"requires", "blocks", "relates"})
|
|
|
|
|
|
def _validate_dependency_kind(kind: str) -> None:
|
|
if kind not in DEPENDENCY_KINDS:
|
|
raise ValueError(f"Ungültiger dependency_kind: {kind}")
|
|
|
|
|
|
def _serialize_dependency(row: dict[str, Any]) -> dict[str, Any]:
|
|
result = dict(row)
|
|
for key in (
|
|
"id",
|
|
"tenant_id",
|
|
"initiative_id",
|
|
"predecessor_action_id",
|
|
"successor_action_id",
|
|
):
|
|
if result.get(key):
|
|
result[key] = str(result[key])
|
|
if result.get("created_at"):
|
|
result["created_at"] = result["created_at"].isoformat()
|
|
return result
|
|
|
|
|
|
def _would_create_cycle(
|
|
*,
|
|
dependencies: list[dict[str, Any]],
|
|
predecessor_id: str,
|
|
successor_id: str,
|
|
) -> bool:
|
|
"""Prüft Zyklus wenn successor transitiv predecessor requires."""
|
|
graph: dict[str, list[str]] = {}
|
|
for dep in dependencies:
|
|
if dep.get("dependency_kind", "requires") != "requires":
|
|
continue
|
|
pred = str(dep["predecessor_action_id"])
|
|
succ = str(dep["successor_action_id"])
|
|
graph.setdefault(succ, []).append(pred)
|
|
|
|
graph.setdefault(successor_id, []).append(predecessor_id)
|
|
|
|
visiting: set[str] = set()
|
|
visited: set[str] = set()
|
|
|
|
def dfs(node: str) -> bool:
|
|
if node in visiting:
|
|
return True
|
|
if node in visited:
|
|
return False
|
|
visiting.add(node)
|
|
for upstream in graph.get(node, []):
|
|
if dfs(upstream):
|
|
return True
|
|
visiting.remove(node)
|
|
visited.add(node)
|
|
return False
|
|
|
|
return dfs(successor_id)
|
|
|
|
|
|
def list_dependencies_for_initiative(
|
|
*, tenant_id: str, initiative_id: str
|
|
) -> list[dict[str, Any]]:
|
|
conn = get_connection()
|
|
try:
|
|
with conn.cursor(cursor_factory=RealDictCursor) as cur:
|
|
cur.execute(
|
|
"""
|
|
SELECT id, tenant_id, initiative_id,
|
|
predecessor_action_id, successor_action_id,
|
|
dependency_kind, created_at
|
|
FROM action_dependencies
|
|
WHERE tenant_id = %s AND initiative_id = %s
|
|
ORDER BY created_at
|
|
""",
|
|
(tenant_id, initiative_id),
|
|
)
|
|
return [_serialize_dependency(dict(row)) for row in cur.fetchall()]
|
|
finally:
|
|
conn.close()
|
|
|
|
|
|
def add_dependency(
|
|
*,
|
|
tenant_id: str,
|
|
predecessor_action_id: str,
|
|
successor_action_id: str,
|
|
dependency_kind: DependencyKind = "requires",
|
|
user_id: Optional[str] = None,
|
|
) -> dict[str, Any]:
|
|
_validate_dependency_kind(dependency_kind)
|
|
if predecessor_action_id == successor_action_id:
|
|
raise ValueError("Abhängigkeit auf sich selbst nicht erlaubt")
|
|
|
|
predecessor = get_action(tenant_id=tenant_id, action_id=predecessor_action_id)
|
|
successor = get_action(tenant_id=tenant_id, action_id=successor_action_id)
|
|
if not predecessor or not successor:
|
|
raise ValueError("Action nicht gefunden")
|
|
if predecessor["initiative_id"] != successor["initiative_id"]:
|
|
raise ValueError("Abhängigkeiten nur innerhalb eines Vorhabens")
|
|
|
|
initiative_id = predecessor["initiative_id"]
|
|
existing = list_dependencies_for_initiative(
|
|
tenant_id=tenant_id, initiative_id=initiative_id
|
|
)
|
|
if _would_create_cycle(
|
|
dependencies=existing,
|
|
predecessor_id=predecessor_action_id,
|
|
successor_id=successor_action_id,
|
|
):
|
|
raise ValueError("Abhängigkeit würde Zyklus erzeugen")
|
|
|
|
conn = get_connection()
|
|
try:
|
|
with conn.cursor(cursor_factory=RealDictCursor) as cur:
|
|
cur.execute(
|
|
"""
|
|
SELECT 1 FROM action_dependencies
|
|
WHERE tenant_id = %s
|
|
AND predecessor_action_id = %s
|
|
AND successor_action_id = %s
|
|
AND dependency_kind = %s
|
|
""",
|
|
(
|
|
tenant_id,
|
|
predecessor_action_id,
|
|
successor_action_id,
|
|
dependency_kind,
|
|
),
|
|
)
|
|
if cur.fetchone():
|
|
raise ValueError("Abhängigkeit existiert bereits")
|
|
|
|
cur.execute(
|
|
"""
|
|
INSERT INTO action_dependencies (
|
|
tenant_id, initiative_id,
|
|
predecessor_action_id, successor_action_id,
|
|
dependency_kind
|
|
)
|
|
VALUES (%s, %s, %s, %s, %s)
|
|
RETURNING id, tenant_id, initiative_id,
|
|
predecessor_action_id, successor_action_id,
|
|
dependency_kind, created_at
|
|
""",
|
|
(
|
|
tenant_id,
|
|
initiative_id,
|
|
predecessor_action_id,
|
|
successor_action_id,
|
|
dependency_kind,
|
|
),
|
|
)
|
|
row = _serialize_dependency(dict(cur.fetchone()))
|
|
conn.commit()
|
|
finally:
|
|
conn.close()
|
|
|
|
log_audit(
|
|
"action.dependency_added",
|
|
user_id=user_id,
|
|
tenant_id=tenant_id,
|
|
details={
|
|
"predecessor_action_id": predecessor_action_id,
|
|
"successor_action_id": successor_action_id,
|
|
"dependency_kind": dependency_kind,
|
|
},
|
|
)
|
|
return row
|
|
|
|
|
|
def delete_dependency(
|
|
*, tenant_id: str, dependency_id: str, user_id: Optional[str] = None
|
|
) -> bool:
|
|
conn = get_connection()
|
|
try:
|
|
with conn.cursor(cursor_factory=RealDictCursor) as cur:
|
|
cur.execute(
|
|
"""
|
|
SELECT id, predecessor_action_id, successor_action_id, dependency_kind
|
|
FROM action_dependencies
|
|
WHERE id = %s AND tenant_id = %s
|
|
""",
|
|
(dependency_id, tenant_id),
|
|
)
|
|
row = cur.fetchone()
|
|
if not row:
|
|
raise ValueError("Abhängigkeit nicht gefunden")
|
|
|
|
cur.execute(
|
|
"""
|
|
DELETE FROM action_dependencies
|
|
WHERE id = %s AND tenant_id = %s
|
|
""",
|
|
(dependency_id, tenant_id),
|
|
)
|
|
conn.commit()
|
|
finally:
|
|
conn.close()
|
|
|
|
log_audit(
|
|
"action.dependency_removed",
|
|
user_id=user_id,
|
|
tenant_id=tenant_id,
|
|
details={
|
|
"dependency_id": dependency_id,
|
|
"predecessor_action_id": str(row["predecessor_action_id"]),
|
|
"successor_action_id": str(row["successor_action_id"]),
|
|
},
|
|
)
|
|
return True
|