AP1.0: Steering Foundation — Lifecycle, Context, Signal Engine
Deploy Development / deploy (push) Successful in 44s
Test Suite / pytest-backend (push) Successful in 1m19s
Test Suite / lint-backend (push) Successful in 2s
Test Suite / compose-smoke (push) Has been skipped
Test Suite / k6 /api/health Baseline (push) Successful in 18s
Test Suite / playwright-smoke (push) Successful in 11s
Deploy Development / deploy (push) Successful in 44s
Test Suite / pytest-backend (push) Successful in 1m19s
Test Suite / lint-backend (push) Successful in 2s
Test Suite / compose-smoke (push) Has been skipped
Test Suite / k6 /api/health Baseline (push) Successful in 18s
Test Suite / playwright-smoke (push) Successful in 11s
Persistierter Standard Lifecycle pro Initiative, backend/steering/ Skeleton, Attention-Refactor und Hook-Dispatch als Basis für Method Registry. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
1 parent
451d62ed46
commit
95feba6204
36 files changed
+2006
-724
No files matched your search
@@ -270,6 +270,7 @@ def update_blocker(
|
||||
tenant_id=tenant_id,
|
||||
blocker_id=blocker_id,
|
||||
action_id=result.get("action_id"),
|
||||
initiative_id=result.get("initiative_id"),
|
||||
old_status=old_status,
|
||||
new_status=status,
|
||||
user_id=user_id,
|
||||
|
||||
@@ -94,6 +94,13 @@ def create_initiative(
|
||||
tenant_id=tenant_id,
|
||||
details={"initiative_id": row["id"], "title": title, "status": status},
|
||||
)
|
||||
from services.steering_context import create_context_for_new_initiative
|
||||
|
||||
create_context_for_new_initiative(
|
||||
tenant_id=tenant_id,
|
||||
initiative_id=row["id"],
|
||||
user_id=user_id,
|
||||
)
|
||||
return row
|
||||
|
||||
|
||||
|
||||
@@ -1,7 +1,4 @@
|
||||
"""Minimale Operating-Transitions — Vorstufe zum Steering Orchestrator (AP0.10a).
|
||||
|
||||
Side Effects bei Statusänderungen. Später: Hook Registry + Method Strategies.
|
||||
"""
|
||||
"""Minimale Operating-Transitions — AP1.0 mit Hook Dispatch."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
@@ -10,6 +7,7 @@ from typing import Any, Optional
|
||||
from psycopg2.extras import RealDictCursor
|
||||
|
||||
from db import get_connection
|
||||
from steering.hooks.dispatch import dispatch_hook
|
||||
|
||||
RESOLVED_BLOCKER_STATUSES = frozenset({"resolved", "accepted_risk", "dismissed"})
|
||||
|
||||
@@ -19,6 +17,7 @@ def after_blocker_status_change(
|
||||
tenant_id: str,
|
||||
blocker_id: str,
|
||||
action_id: Optional[str],
|
||||
initiative_id: Optional[str] = None,
|
||||
old_status: str,
|
||||
new_status: str,
|
||||
user_id: Optional[str] = None,
|
||||
@@ -86,6 +85,31 @@ def after_blocker_status_change(
|
||||
finally:
|
||||
conn.close()
|
||||
|
||||
if effects:
|
||||
dispatch_hook(
|
||||
"on_blocker_resolved",
|
||||
None,
|
||||
{
|
||||
"blocker_id": blocker_id,
|
||||
"action_id": action_id,
|
||||
"initiative_id": initiative_id,
|
||||
"effects": effects,
|
||||
},
|
||||
tenant_id=tenant_id,
|
||||
user_id=user_id,
|
||||
)
|
||||
if initiative_id and user_id:
|
||||
from steering.lifecycle.orchestrator import transition
|
||||
|
||||
transition(
|
||||
None,
|
||||
initiative_id,
|
||||
"action_selection",
|
||||
reason="blocker_resolved",
|
||||
tenant_id=tenant_id,
|
||||
user_id=user_id,
|
||||
)
|
||||
|
||||
if effects and user_id:
|
||||
from services.audit import log_audit
|
||||
|
||||
@@ -103,6 +127,7 @@ def after_review_status_change(
|
||||
tenant_id: str,
|
||||
review_id: str,
|
||||
action_id: Optional[str],
|
||||
initiative_id: Optional[str] = None,
|
||||
old_status: str,
|
||||
new_status: str,
|
||||
user_id: Optional[str] = None,
|
||||
@@ -137,6 +162,20 @@ def after_review_status_change(
|
||||
finally:
|
||||
conn.close()
|
||||
|
||||
if effects:
|
||||
dispatch_hook(
|
||||
"on_review_completed",
|
||||
None,
|
||||
{
|
||||
"review_id": review_id,
|
||||
"action_id": action_id,
|
||||
"initiative_id": initiative_id,
|
||||
"effects": effects,
|
||||
},
|
||||
tenant_id=tenant_id,
|
||||
user_id=user_id,
|
||||
)
|
||||
|
||||
if effects and user_id:
|
||||
from services.audit import log_audit
|
||||
|
||||
|
||||
@@ -315,6 +315,7 @@ def update_review(
|
||||
tenant_id=tenant_id,
|
||||
review_id=review_id,
|
||||
action_id=result.get("action_id"),
|
||||
initiative_id=result.get("initiative_id"),
|
||||
old_status=old_status,
|
||||
new_status=status,
|
||||
user_id=user_id,
|
||||
|
||||
@@ -0,0 +1,208 @@
|
||||
"""SteeringContext write service — AP1.0."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import json
|
||||
from typing import Any, Optional
|
||||
|
||||
from psycopg2.extras import RealDictCursor, Json
|
||||
|
||||
from db import get_connection
|
||||
from services.audit import log_audit
|
||||
from services.initiatives import get_initiative
|
||||
from steering.lifecycle.states import validate_lifecycle_state
|
||||
|
||||
DEFAULT_METHOD_KEY = "generic_operating"
|
||||
DEFAULT_METHOD_VERSION = "0.1.0"
|
||||
|
||||
|
||||
def _serialize_row(row: dict[str, Any]) -> dict[str, Any]:
|
||||
result = dict(row)
|
||||
for key in ("id", "tenant_id", "initiative_id"):
|
||||
if result.get(key):
|
||||
result[key] = str(result[key])
|
||||
if result.get("created_at"):
|
||||
result["created_at"] = result["created_at"].isoformat()
|
||||
if result.get("updated_at"):
|
||||
result["updated_at"] = result["updated_at"].isoformat()
|
||||
if isinstance(result.get("lifecycle_metadata"), dict):
|
||||
pass
|
||||
elif result.get("lifecycle_metadata") is not None:
|
||||
result["lifecycle_metadata"] = dict(result["lifecycle_metadata"])
|
||||
return result
|
||||
|
||||
|
||||
def _infer_initial_lifecycle_state(*, tenant_id: str, initiative_id: str) -> str:
|
||||
initiative = get_initiative(tenant_id=tenant_id, initiative_id=initiative_id)
|
||||
if not initiative:
|
||||
return "intake"
|
||||
if initiative["status"] in ("completed", "archived"):
|
||||
return "closure"
|
||||
|
||||
conn = get_connection()
|
||||
try:
|
||||
with conn.cursor(cursor_factory=RealDictCursor) as cur:
|
||||
cur.execute(
|
||||
"""
|
||||
SELECT 1 FROM actions
|
||||
WHERE tenant_id = %s AND initiative_id = %s
|
||||
AND status IN (
|
||||
'open', 'ready', 'in_progress', 'blocked', 'review_required'
|
||||
)
|
||||
LIMIT 1
|
||||
""",
|
||||
(tenant_id, initiative_id),
|
||||
)
|
||||
if cur.fetchone():
|
||||
return "action_selection"
|
||||
|
||||
cur.execute(
|
||||
"""
|
||||
SELECT 1 FROM backlog_items
|
||||
WHERE tenant_id = %s AND initiative_id = %s
|
||||
AND status IN ('new', 'triaged', 'accepted')
|
||||
LIMIT 1
|
||||
""",
|
||||
(tenant_id, initiative_id),
|
||||
)
|
||||
if cur.fetchone():
|
||||
return "structure_setup"
|
||||
finally:
|
||||
conn.close()
|
||||
|
||||
return "planning"
|
||||
|
||||
|
||||
def get_steering_context(
|
||||
*, tenant_id: str, initiative_id: str
|
||||
) -> Optional[dict[str, Any]]:
|
||||
conn = get_connection()
|
||||
try:
|
||||
with conn.cursor(cursor_factory=RealDictCursor) as cur:
|
||||
cur.execute(
|
||||
"""
|
||||
SELECT id, tenant_id, initiative_id, method_key, method_version,
|
||||
lifecycle_state, lifecycle_metadata, created_at, updated_at
|
||||
FROM steering_contexts
|
||||
WHERE tenant_id = %s AND initiative_id = %s
|
||||
""",
|
||||
(tenant_id, initiative_id),
|
||||
)
|
||||
row = cur.fetchone()
|
||||
return _serialize_row(dict(row)) if row else None
|
||||
finally:
|
||||
conn.close()
|
||||
|
||||
|
||||
def ensure_steering_context(
|
||||
*,
|
||||
tenant_id: str,
|
||||
initiative_id: str,
|
||||
initial_state: Optional[str] = None,
|
||||
user_id: Optional[str] = None,
|
||||
) -> dict[str, Any]:
|
||||
existing = get_steering_context(tenant_id=tenant_id, initiative_id=initiative_id)
|
||||
if existing:
|
||||
return existing
|
||||
|
||||
if not get_initiative(tenant_id=tenant_id, initiative_id=initiative_id):
|
||||
raise ValueError("Vorhaben nicht gefunden")
|
||||
|
||||
state = initial_state or _infer_initial_lifecycle_state(
|
||||
tenant_id=tenant_id, initiative_id=initiative_id
|
||||
)
|
||||
validate_lifecycle_state(state)
|
||||
|
||||
conn = get_connection()
|
||||
try:
|
||||
with conn.cursor(cursor_factory=RealDictCursor) as cur:
|
||||
cur.execute(
|
||||
"""
|
||||
INSERT INTO steering_contexts (
|
||||
tenant_id, initiative_id, method_key, method_version,
|
||||
lifecycle_state, lifecycle_metadata
|
||||
)
|
||||
VALUES (%s, %s, %s, %s, %s, %s)
|
||||
RETURNING id, tenant_id, initiative_id, method_key, method_version,
|
||||
lifecycle_state, lifecycle_metadata, created_at, updated_at
|
||||
""",
|
||||
(
|
||||
tenant_id,
|
||||
initiative_id,
|
||||
DEFAULT_METHOD_KEY,
|
||||
DEFAULT_METHOD_VERSION,
|
||||
state,
|
||||
Json({}),
|
||||
),
|
||||
)
|
||||
row = _serialize_row(dict(cur.fetchone()))
|
||||
conn.commit()
|
||||
finally:
|
||||
conn.close()
|
||||
|
||||
log_audit(
|
||||
"steering_context.created",
|
||||
user_id=user_id,
|
||||
tenant_id=tenant_id,
|
||||
details={"initiative_id": initiative_id, "lifecycle_state": state},
|
||||
)
|
||||
return row
|
||||
|
||||
|
||||
def update_lifecycle_state(
|
||||
*,
|
||||
tenant_id: str,
|
||||
initiative_id: str,
|
||||
lifecycle_state: str,
|
||||
user_id: Optional[str] = None,
|
||||
reason: str = "",
|
||||
from_state: Optional[str] = None,
|
||||
) -> dict[str, Any]:
|
||||
validate_lifecycle_state(lifecycle_state)
|
||||
conn = get_connection()
|
||||
try:
|
||||
with conn.cursor(cursor_factory=RealDictCursor) as cur:
|
||||
cur.execute(
|
||||
"""
|
||||
UPDATE steering_contexts
|
||||
SET lifecycle_state = %s, updated_at = NOW()
|
||||
WHERE tenant_id = %s AND initiative_id = %s
|
||||
RETURNING id, tenant_id, initiative_id, method_key, method_version,
|
||||
lifecycle_state, lifecycle_metadata, created_at, updated_at
|
||||
""",
|
||||
(lifecycle_state, tenant_id, initiative_id),
|
||||
)
|
||||
row = cur.fetchone()
|
||||
if not row:
|
||||
raise ValueError("SteeringContext nicht gefunden")
|
||||
result = _serialize_row(dict(row))
|
||||
conn.commit()
|
||||
finally:
|
||||
conn.close()
|
||||
|
||||
log_audit(
|
||||
"steering_context.lifecycle_transition",
|
||||
user_id=user_id,
|
||||
tenant_id=tenant_id,
|
||||
details={
|
||||
"initiative_id": initiative_id,
|
||||
"from_state": from_state,
|
||||
"to_state": lifecycle_state,
|
||||
"reason": reason,
|
||||
},
|
||||
)
|
||||
return result
|
||||
|
||||
|
||||
def create_context_for_new_initiative(
|
||||
*,
|
||||
tenant_id: str,
|
||||
initiative_id: str,
|
||||
user_id: Optional[str] = None,
|
||||
) -> dict[str, Any]:
|
||||
return ensure_steering_context(
|
||||
tenant_id=tenant_id,
|
||||
initiative_id=initiative_id,
|
||||
initial_state="intake",
|
||||
user_id=user_id,
|
||||
)
|
||||
Reference in new issue
Block a user