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
+13
-694
@@ -1,706 +1,25 @@
|
||||
"""Attention and NextAction read-models — regelbasiert, tenant-scoped (AP0.8a)."""
|
||||
"""Attention and NextAction — thin Data Layer wrapper (AP1.0)."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from typing import Any, Literal, Optional
|
||||
|
||||
from psycopg2.extras import RealDictCursor
|
||||
|
||||
from db import get_connection
|
||||
from steering.signals.engine import evaluate
|
||||
from tenant_context import TenantContext
|
||||
|
||||
AttentionKind = Literal[
|
||||
"blocked_action",
|
||||
"open_blocker",
|
||||
"high_priority_action",
|
||||
"unassigned_action",
|
||||
"initiative_without_next_action",
|
||||
"stale_initiative",
|
||||
"milestone_at_risk",
|
||||
"overdue_action",
|
||||
"review_due",
|
||||
"recurring_due",
|
||||
"action_review_required",
|
||||
]
|
||||
|
||||
NextActionKind = Literal[
|
||||
"assign_action",
|
||||
"resolve_blocker",
|
||||
"create_action",
|
||||
"convert_backlog",
|
||||
"review_milestone",
|
||||
]
|
||||
|
||||
Severity = Literal["info", "warning", "critical"]
|
||||
|
||||
_SEVERITY_ORDER = {"critical": 0, "warning": 1, "info": 2}
|
||||
|
||||
OPEN_BLOCKER_STATUSES = ("open", "in_progress")
|
||||
OPEN_ACTION_STATUSES = ("open", "ready", "in_progress", "blocked", "review_required")
|
||||
ACTIVE_INITIATIVE_STATUSES = ("active", "paused")
|
||||
def get_attention_items(ctx: TenantContext):
|
||||
return evaluate(ctx, kind="attention")
|
||||
|
||||
|
||||
def _serialize_attention(row: dict[str, Any]) -> dict[str, Any]:
|
||||
item = dict(row)
|
||||
for key in (
|
||||
"scope_id",
|
||||
"initiative_id",
|
||||
"action_id",
|
||||
"blocker_id",
|
||||
"milestone_id",
|
||||
"review_id",
|
||||
"recurring_element_id",
|
||||
):
|
||||
if item.get(key):
|
||||
item[key] = str(item[key])
|
||||
return item
|
||||
|
||||
|
||||
def _blocked_actions(cur, ctx: TenantContext) -> list[dict[str, Any]]:
|
||||
cur.execute(
|
||||
"""
|
||||
SELECT
|
||||
'blocked_action' AS kind,
|
||||
'critical' AS severity,
|
||||
a.title AS title,
|
||||
'Maßnahme ist blockiert' AS summary,
|
||||
'action' AS scope_type,
|
||||
a.id AS scope_id,
|
||||
a.initiative_id,
|
||||
a.id AS action_id,
|
||||
NULL::uuid AS blocker_id,
|
||||
NULL::uuid AS milestone_id,
|
||||
'action_blocked' AS reason_code,
|
||||
'actions' AS data_source
|
||||
FROM actions a
|
||||
WHERE a.tenant_id = %s AND a.status = 'blocked'
|
||||
ORDER BY a.updated_at DESC
|
||||
LIMIT 50
|
||||
""",
|
||||
(ctx.tenant_id,),
|
||||
)
|
||||
return [_serialize_attention(dict(r)) for r in cur.fetchall()]
|
||||
|
||||
|
||||
def _open_blockers(cur, ctx: TenantContext) -> list[dict[str, Any]]:
|
||||
cur.execute(
|
||||
"""
|
||||
SELECT
|
||||
'open_blocker' AS kind,
|
||||
'warning' AS severity,
|
||||
b.title AS title,
|
||||
'Offener Blocker im Vorhaben' AS summary,
|
||||
'blocker' AS scope_type,
|
||||
b.id AS scope_id,
|
||||
b.initiative_id,
|
||||
b.action_id,
|
||||
b.id AS blocker_id,
|
||||
NULL::uuid AS milestone_id,
|
||||
'blocker_open' AS reason_code,
|
||||
'blockers' AS data_source
|
||||
FROM blockers b
|
||||
WHERE b.tenant_id = %s AND b.status IN ('open', 'in_progress')
|
||||
ORDER BY b.updated_at DESC
|
||||
LIMIT 50
|
||||
""",
|
||||
(ctx.tenant_id,),
|
||||
)
|
||||
items = []
|
||||
for row in cur.fetchall():
|
||||
item = _serialize_attention(dict(row))
|
||||
if item.get("action_id"):
|
||||
item["action_id"] = str(item["action_id"])
|
||||
items.append(item)
|
||||
return items
|
||||
|
||||
|
||||
def _high_priority_actions(cur, ctx: TenantContext) -> list[dict[str, Any]]:
|
||||
actor_filter = ""
|
||||
params: list[Any] = [ctx.tenant_id]
|
||||
if ctx.actor_id:
|
||||
actor_filter = """
|
||||
AND EXISTS (
|
||||
SELECT 1 FROM action_assignments aa
|
||||
WHERE aa.action_id = a.id
|
||||
AND aa.tenant_id = a.tenant_id
|
||||
AND aa.actor_id = %s
|
||||
)
|
||||
"""
|
||||
params.append(ctx.actor_id)
|
||||
else:
|
||||
return []
|
||||
|
||||
cur.execute(
|
||||
f"""
|
||||
SELECT
|
||||
'high_priority_action' AS kind,
|
||||
'warning' AS severity,
|
||||
a.title AS title,
|
||||
'High-Priority Maßnahme offen' AS summary,
|
||||
'action' AS scope_type,
|
||||
a.id AS scope_id,
|
||||
a.initiative_id,
|
||||
a.id AS action_id,
|
||||
NULL::uuid AS blocker_id,
|
||||
NULL::uuid AS milestone_id,
|
||||
'action_high_priority_open' AS reason_code,
|
||||
'actions' AS data_source
|
||||
FROM actions a
|
||||
WHERE a.tenant_id = %s
|
||||
AND a.priority = 'high'
|
||||
AND a.status IN ('open', 'ready', 'in_progress')
|
||||
{actor_filter}
|
||||
ORDER BY a.updated_at DESC
|
||||
LIMIT 30
|
||||
""",
|
||||
tuple(params),
|
||||
)
|
||||
return [_serialize_attention(dict(r)) for r in cur.fetchall()]
|
||||
|
||||
|
||||
def _unassigned_actions(cur, ctx: TenantContext) -> list[dict[str, Any]]:
|
||||
cur.execute(
|
||||
"""
|
||||
SELECT
|
||||
'unassigned_action' AS kind,
|
||||
'warning' AS severity,
|
||||
a.title AS title,
|
||||
'Maßnahme ohne Zuweisung' AS summary,
|
||||
'action' AS scope_type,
|
||||
a.id AS scope_id,
|
||||
a.initiative_id,
|
||||
a.id AS action_id,
|
||||
NULL::uuid AS blocker_id,
|
||||
NULL::uuid AS milestone_id,
|
||||
'action_unassigned' AS reason_code,
|
||||
'actions' AS data_source
|
||||
FROM actions a
|
||||
WHERE a.tenant_id = %s
|
||||
AND a.status IN ('open', 'ready', 'in_progress')
|
||||
AND NOT EXISTS (
|
||||
SELECT 1 FROM action_assignments aa
|
||||
WHERE aa.action_id = a.id AND aa.tenant_id = a.tenant_id
|
||||
)
|
||||
ORDER BY a.updated_at DESC
|
||||
LIMIT 30
|
||||
""",
|
||||
(ctx.tenant_id,),
|
||||
)
|
||||
return [_serialize_attention(dict(r)) for r in cur.fetchall()]
|
||||
|
||||
|
||||
def _initiatives_without_next_action(cur, ctx: TenantContext) -> list[dict[str, Any]]:
|
||||
cur.execute(
|
||||
"""
|
||||
SELECT
|
||||
'initiative_without_next_action' AS kind,
|
||||
'info' AS severity,
|
||||
i.title AS title,
|
||||
'Keine offene nächste Maßnahme' AS summary,
|
||||
'initiative' AS scope_type,
|
||||
i.id AS scope_id,
|
||||
i.id AS initiative_id,
|
||||
NULL::uuid AS action_id,
|
||||
NULL::uuid AS blocker_id,
|
||||
NULL::uuid AS milestone_id,
|
||||
'initiative_no_open_action' AS reason_code,
|
||||
'initiatives' AS data_source
|
||||
FROM initiatives i
|
||||
WHERE i.tenant_id = %s
|
||||
AND i.status IN ('active', 'paused')
|
||||
AND NOT EXISTS (
|
||||
SELECT 1 FROM actions a
|
||||
WHERE a.initiative_id = i.id
|
||||
AND a.tenant_id = i.tenant_id
|
||||
AND a.status IN ('open', 'ready', 'in_progress', 'blocked', 'review_required')
|
||||
)
|
||||
ORDER BY i.updated_at DESC
|
||||
LIMIT 30
|
||||
""",
|
||||
(ctx.tenant_id,),
|
||||
)
|
||||
return [_serialize_attention(dict(r)) for r in cur.fetchall()]
|
||||
|
||||
|
||||
def _stale_initiatives(cur, ctx: TenantContext) -> list[dict[str, Any]]:
|
||||
cur.execute(
|
||||
"""
|
||||
SELECT
|
||||
'stale_initiative' AS kind,
|
||||
'info' AS severity,
|
||||
i.title AS title,
|
||||
'Vorhaben seit über 14 Tagen unverändert' AS summary,
|
||||
'initiative' AS scope_type,
|
||||
i.id AS scope_id,
|
||||
i.id AS initiative_id,
|
||||
NULL::uuid AS action_id,
|
||||
NULL::uuid AS blocker_id,
|
||||
NULL::uuid AS milestone_id,
|
||||
'initiative_stale' AS reason_code,
|
||||
'initiatives' AS data_source
|
||||
FROM initiatives i
|
||||
WHERE i.tenant_id = %s
|
||||
AND i.status = 'active'
|
||||
AND i.updated_at < NOW() - INTERVAL '14 days'
|
||||
ORDER BY i.updated_at ASC
|
||||
LIMIT 20
|
||||
""",
|
||||
(ctx.tenant_id,),
|
||||
)
|
||||
return [_serialize_attention(dict(r)) for r in cur.fetchall()]
|
||||
|
||||
|
||||
def _milestones_at_risk(cur, ctx: TenantContext) -> list[dict[str, Any]]:
|
||||
cur.execute(
|
||||
"""
|
||||
SELECT
|
||||
'milestone_at_risk' AS kind,
|
||||
'warning' AS severity,
|
||||
m.title AS title,
|
||||
'Meilenstein als gefährdet markiert' AS summary,
|
||||
'milestone' AS scope_type,
|
||||
m.id AS scope_id,
|
||||
m.initiative_id,
|
||||
NULL::uuid AS action_id,
|
||||
NULL::uuid AS blocker_id,
|
||||
m.id AS milestone_id,
|
||||
'milestone_at_risk' AS reason_code,
|
||||
'milestones' AS data_source
|
||||
FROM milestones m
|
||||
WHERE m.tenant_id = %s AND m.status = 'at_risk'
|
||||
ORDER BY m.updated_at DESC
|
||||
LIMIT 20
|
||||
""",
|
||||
(ctx.tenant_id,),
|
||||
)
|
||||
return [_serialize_attention(dict(r)) for r in cur.fetchall()]
|
||||
|
||||
|
||||
def _actions_review_required(cur, ctx: TenantContext) -> list[dict[str, Any]]:
|
||||
cur.execute(
|
||||
"""
|
||||
SELECT
|
||||
'action_review_required' AS kind,
|
||||
'warning' AS severity,
|
||||
a.title AS title,
|
||||
'Maßnahme wartet auf Review — kein geplantes Review verknüpft' AS summary,
|
||||
'action' AS scope_type,
|
||||
a.id AS scope_id,
|
||||
a.initiative_id,
|
||||
a.id AS action_id,
|
||||
NULL::uuid AS blocker_id,
|
||||
NULL::uuid AS milestone_id,
|
||||
NULL::uuid AS review_id,
|
||||
NULL::uuid AS recurring_element_id,
|
||||
'action_review_required_no_review' AS reason_code,
|
||||
'actions' AS data_source
|
||||
FROM actions a
|
||||
WHERE a.tenant_id = %s
|
||||
AND a.status = 'review_required'
|
||||
AND NOT EXISTS (
|
||||
SELECT 1 FROM reviews r
|
||||
WHERE r.tenant_id = a.tenant_id
|
||||
AND r.action_id = a.id
|
||||
AND r.status = 'planned'
|
||||
)
|
||||
ORDER BY a.updated_at DESC
|
||||
LIMIT 20
|
||||
""",
|
||||
(ctx.tenant_id,),
|
||||
)
|
||||
return [_serialize_attention(dict(r)) for r in cur.fetchall()]
|
||||
|
||||
|
||||
def _overdue_actions(cur, ctx: TenantContext) -> list[dict[str, Any]]:
|
||||
cur.execute(
|
||||
"""
|
||||
SELECT
|
||||
'overdue_action' AS kind,
|
||||
'warning' AS severity,
|
||||
a.title AS title,
|
||||
'Maßnahme überfällig' AS summary,
|
||||
'action' AS scope_type,
|
||||
a.id AS scope_id,
|
||||
a.initiative_id,
|
||||
a.id AS action_id,
|
||||
NULL::uuid AS blocker_id,
|
||||
NULL::uuid AS milestone_id,
|
||||
NULL::uuid AS review_id,
|
||||
NULL::uuid AS recurring_element_id,
|
||||
'action_overdue' AS reason_code,
|
||||
'actions' AS data_source
|
||||
FROM actions a
|
||||
WHERE a.tenant_id = %s
|
||||
AND a.due_at IS NOT NULL
|
||||
AND a.due_at < NOW()
|
||||
AND a.status IN ('open', 'ready', 'in_progress', 'blocked', 'review_required')
|
||||
ORDER BY a.due_at ASC
|
||||
LIMIT 30
|
||||
""",
|
||||
(ctx.tenant_id,),
|
||||
)
|
||||
return [_serialize_attention(dict(r)) for r in cur.fetchall()]
|
||||
|
||||
|
||||
def _reviews_due(cur, ctx: TenantContext) -> list[dict[str, Any]]:
|
||||
cur.execute(
|
||||
"""
|
||||
SELECT
|
||||
'review_due' AS kind,
|
||||
'warning' AS severity,
|
||||
r.title AS title,
|
||||
'Review fällig' AS summary,
|
||||
'review' AS scope_type,
|
||||
r.id AS scope_id,
|
||||
r.initiative_id,
|
||||
r.action_id,
|
||||
NULL::uuid AS blocker_id,
|
||||
r.milestone_id,
|
||||
r.id AS review_id,
|
||||
NULL::uuid AS recurring_element_id,
|
||||
'review_due' AS reason_code,
|
||||
'reviews' AS data_source
|
||||
FROM reviews r
|
||||
WHERE r.tenant_id = %s
|
||||
AND r.status = 'planned'
|
||||
AND r.due_at IS NOT NULL
|
||||
AND r.due_at <= NOW()
|
||||
ORDER BY r.due_at ASC
|
||||
LIMIT 20
|
||||
""",
|
||||
(ctx.tenant_id,),
|
||||
)
|
||||
items = []
|
||||
for row in cur.fetchall():
|
||||
item = _serialize_attention(dict(row))
|
||||
for fk in ("action_id", "milestone_id"):
|
||||
if item.get(fk):
|
||||
item[fk] = str(item[fk])
|
||||
items.append(item)
|
||||
return items
|
||||
|
||||
|
||||
def _recurring_due(cur, ctx: TenantContext) -> list[dict[str, Any]]:
|
||||
cur.execute(
|
||||
"""
|
||||
SELECT
|
||||
'recurring_due' AS kind,
|
||||
'info' AS severity,
|
||||
re.title AS title,
|
||||
'Wiederkehrendes Element fällig' AS summary,
|
||||
'recurring_element' AS scope_type,
|
||||
re.id AS scope_id,
|
||||
re.initiative_id,
|
||||
NULL::uuid AS action_id,
|
||||
NULL::uuid AS blocker_id,
|
||||
NULL::uuid AS milestone_id,
|
||||
NULL::uuid AS review_id,
|
||||
re.id AS recurring_element_id,
|
||||
'recurring_due' AS reason_code,
|
||||
'recurring_elements' AS data_source
|
||||
FROM recurring_elements re
|
||||
WHERE re.tenant_id = %s
|
||||
AND re.status = 'active'
|
||||
AND re.next_due_at IS NOT NULL
|
||||
AND re.next_due_at <= NOW()
|
||||
ORDER BY re.next_due_at ASC
|
||||
LIMIT 20
|
||||
""",
|
||||
(ctx.tenant_id,),
|
||||
)
|
||||
return [_serialize_attention(dict(r)) for r in cur.fetchall()]
|
||||
|
||||
|
||||
def get_attention_items(ctx: TenantContext) -> list[dict[str, Any]]:
|
||||
"""Regelbasierte Attention Items — tenant-scoped, erklärbar."""
|
||||
conn = get_connection()
|
||||
try:
|
||||
with conn.cursor(cursor_factory=RealDictCursor) as cur:
|
||||
items: list[dict[str, Any]] = []
|
||||
items.extend(_blocked_actions(cur, ctx))
|
||||
items.extend(_open_blockers(cur, ctx))
|
||||
items.extend(_high_priority_actions(cur, ctx))
|
||||
items.extend(_unassigned_actions(cur, ctx))
|
||||
items.extend(_initiatives_without_next_action(cur, ctx))
|
||||
items.extend(_stale_initiatives(cur, ctx))
|
||||
items.extend(_milestones_at_risk(cur, ctx))
|
||||
items.extend(_overdue_actions(cur, ctx))
|
||||
items.extend(_actions_review_required(cur, ctx))
|
||||
items.extend(_reviews_due(cur, ctx))
|
||||
items.extend(_recurring_due(cur, ctx))
|
||||
|
||||
items.sort(key=lambda x: _SEVERITY_ORDER.get(x["severity"], 99))
|
||||
return items
|
||||
finally:
|
||||
conn.close()
|
||||
|
||||
|
||||
def get_next_action_candidates(
|
||||
ctx: TenantContext, *, limit: int = 10
|
||||
) -> list[dict[str, Any]]:
|
||||
"""Regelbasierte NextActionCandidates — limitiert, tenant-scoped."""
|
||||
if limit < 1:
|
||||
limit = 1
|
||||
if limit > 50:
|
||||
limit = 50
|
||||
|
||||
conn = get_connection()
|
||||
candidates: list[dict[str, Any]] = []
|
||||
try:
|
||||
with conn.cursor(cursor_factory=RealDictCursor) as cur:
|
||||
cur.execute(
|
||||
"""
|
||||
SELECT
|
||||
'resolve_blocker' AS kind,
|
||||
b.title AS title,
|
||||
'Blocker klären oder Status aktualisieren' AS summary,
|
||||
b.initiative_id,
|
||||
b.action_id,
|
||||
NULL::uuid AS backlog_item_id,
|
||||
'blocker_open' AS reason_code,
|
||||
'Blocker bearbeiten' AS recommended_action
|
||||
FROM blockers b
|
||||
WHERE b.tenant_id = %s AND b.status IN ('open', 'in_progress')
|
||||
ORDER BY b.updated_at DESC
|
||||
LIMIT %s
|
||||
""",
|
||||
(ctx.tenant_id, limit),
|
||||
)
|
||||
for row in cur.fetchall():
|
||||
item = dict(row)
|
||||
item["initiative_id"] = str(item["initiative_id"])
|
||||
if item.get("action_id"):
|
||||
item["action_id"] = str(item["action_id"])
|
||||
candidates.append(item)
|
||||
|
||||
remaining = limit - len(candidates)
|
||||
if remaining > 0:
|
||||
cur.execute(
|
||||
"""
|
||||
SELECT
|
||||
'assign_action' AS kind,
|
||||
a.title AS title,
|
||||
'Actor zuweisen' AS summary,
|
||||
a.initiative_id,
|
||||
a.id AS action_id,
|
||||
NULL::uuid AS backlog_item_id,
|
||||
'action_unassigned' AS reason_code,
|
||||
'Maßnahme zuweisen' AS recommended_action
|
||||
FROM actions a
|
||||
WHERE a.tenant_id = %s
|
||||
AND a.status IN ('open', 'ready', 'in_progress')
|
||||
AND NOT EXISTS (
|
||||
SELECT 1 FROM action_assignments aa
|
||||
WHERE aa.action_id = a.id AND aa.tenant_id = a.tenant_id
|
||||
)
|
||||
ORDER BY a.updated_at DESC
|
||||
LIMIT %s
|
||||
""",
|
||||
(ctx.tenant_id, remaining),
|
||||
)
|
||||
for row in cur.fetchall():
|
||||
item = dict(row)
|
||||
item["initiative_id"] = str(item["initiative_id"])
|
||||
item["action_id"] = str(item["action_id"])
|
||||
candidates.append(item)
|
||||
|
||||
remaining = limit - len(candidates)
|
||||
if remaining > 0:
|
||||
cur.execute(
|
||||
"""
|
||||
SELECT
|
||||
'convert_backlog' AS kind,
|
||||
bi.title AS title,
|
||||
'Freigegebenes Backlog-Item in Maßnahme umwandeln' AS summary,
|
||||
bi.initiative_id,
|
||||
NULL::uuid AS action_id,
|
||||
bi.id AS backlog_item_id,
|
||||
'backlog_accepted_not_converted' AS reason_code,
|
||||
'In Maßnahme umwandeln' AS recommended_action
|
||||
FROM backlog_items bi
|
||||
WHERE bi.tenant_id = %s
|
||||
AND bi.status = 'accepted'
|
||||
AND bi.converted_action_id IS NULL
|
||||
ORDER BY bi.updated_at DESC
|
||||
LIMIT %s
|
||||
""",
|
||||
(ctx.tenant_id, remaining),
|
||||
)
|
||||
for row in cur.fetchall():
|
||||
item = dict(row)
|
||||
item["initiative_id"] = str(item["initiative_id"])
|
||||
item["backlog_item_id"] = str(item["backlog_item_id"])
|
||||
candidates.append(item)
|
||||
|
||||
remaining = limit - len(candidates)
|
||||
if remaining > 0:
|
||||
cur.execute(
|
||||
"""
|
||||
SELECT
|
||||
'create_action' AS kind,
|
||||
i.title AS title,
|
||||
'Nächste Maßnahme für Vorhaben anlegen' AS summary,
|
||||
i.id AS initiative_id,
|
||||
NULL::uuid AS action_id,
|
||||
NULL::uuid AS backlog_item_id,
|
||||
'initiative_no_open_action' AS reason_code,
|
||||
'Maßnahme anlegen' AS recommended_action
|
||||
FROM initiatives i
|
||||
WHERE i.tenant_id = %s
|
||||
AND i.status IN ('active', 'paused')
|
||||
AND NOT EXISTS (
|
||||
SELECT 1 FROM actions a
|
||||
WHERE a.initiative_id = i.id
|
||||
AND a.tenant_id = i.tenant_id
|
||||
AND a.status IN ('open', 'ready', 'in_progress', 'blocked', 'review_required')
|
||||
)
|
||||
ORDER BY i.updated_at DESC
|
||||
LIMIT %s
|
||||
""",
|
||||
(ctx.tenant_id, remaining),
|
||||
)
|
||||
for row in cur.fetchall():
|
||||
item = dict(row)
|
||||
item["initiative_id"] = str(item["initiative_id"])
|
||||
candidates.append(item)
|
||||
|
||||
return candidates[:limit]
|
||||
finally:
|
||||
conn.close()
|
||||
def get_next_action_candidates(ctx: TenantContext, *, limit: int = 10):
|
||||
return evaluate(ctx, kind="next_action", limit=limit)
|
||||
|
||||
|
||||
def get_next_action_candidates_for_initiative(
|
||||
ctx: TenantContext, *, initiative_id: str, limit: int = 5
|
||||
) -> list[dict[str, Any]]:
|
||||
"""NextActionCandidates für ein Vorhaben — tenant-scoped."""
|
||||
if limit < 1:
|
||||
limit = 1
|
||||
if limit > 20:
|
||||
limit = 20
|
||||
|
||||
conn = get_connection()
|
||||
candidates: list[dict[str, Any]] = []
|
||||
try:
|
||||
with conn.cursor(cursor_factory=RealDictCursor) as cur:
|
||||
cur.execute(
|
||||
"""
|
||||
SELECT
|
||||
'resolve_blocker' AS kind,
|
||||
b.title AS title,
|
||||
'Blocker klären oder Status aktualisieren' AS summary,
|
||||
b.initiative_id,
|
||||
b.action_id,
|
||||
NULL::uuid AS backlog_item_id,
|
||||
'blocker_open' AS reason_code,
|
||||
'Blocker bearbeiten' AS recommended_action
|
||||
FROM blockers b
|
||||
WHERE b.tenant_id = %s AND b.initiative_id = %s
|
||||
AND b.status IN ('open', 'in_progress')
|
||||
ORDER BY b.updated_at DESC
|
||||
LIMIT %s
|
||||
""",
|
||||
(ctx.tenant_id, initiative_id, limit),
|
||||
)
|
||||
for row in cur.fetchall():
|
||||
item = dict(row)
|
||||
item["initiative_id"] = str(item["initiative_id"])
|
||||
if item.get("action_id"):
|
||||
item["action_id"] = str(item["action_id"])
|
||||
candidates.append(item)
|
||||
|
||||
remaining = limit - len(candidates)
|
||||
if remaining > 0:
|
||||
cur.execute(
|
||||
"""
|
||||
SELECT
|
||||
'assign_action' AS kind,
|
||||
a.title AS title,
|
||||
'Actor zuweisen' AS summary,
|
||||
a.initiative_id,
|
||||
a.id AS action_id,
|
||||
NULL::uuid AS backlog_item_id,
|
||||
'action_unassigned' AS reason_code,
|
||||
'Maßnahme zuweisen' AS recommended_action
|
||||
FROM actions a
|
||||
WHERE a.tenant_id = %s AND a.initiative_id = %s
|
||||
AND a.status IN ('open', 'ready', 'in_progress')
|
||||
AND NOT EXISTS (
|
||||
SELECT 1 FROM action_assignments aa
|
||||
WHERE aa.action_id = a.id AND aa.tenant_id = a.tenant_id
|
||||
)
|
||||
ORDER BY a.updated_at DESC
|
||||
LIMIT %s
|
||||
""",
|
||||
(ctx.tenant_id, initiative_id, remaining),
|
||||
)
|
||||
for row in cur.fetchall():
|
||||
item = dict(row)
|
||||
item["initiative_id"] = str(item["initiative_id"])
|
||||
item["action_id"] = str(item["action_id"])
|
||||
candidates.append(item)
|
||||
|
||||
remaining = limit - len(candidates)
|
||||
if remaining > 0:
|
||||
cur.execute(
|
||||
"""
|
||||
SELECT
|
||||
'convert_backlog' AS kind,
|
||||
bi.title AS title,
|
||||
'Freigegebenes Backlog-Item in Maßnahme umwandeln' AS summary,
|
||||
bi.initiative_id,
|
||||
NULL::uuid AS action_id,
|
||||
bi.id AS backlog_item_id,
|
||||
'backlog_accepted_not_converted' AS reason_code,
|
||||
'In Maßnahme umwandeln' AS recommended_action
|
||||
FROM backlog_items bi
|
||||
WHERE bi.tenant_id = %s AND bi.initiative_id = %s
|
||||
AND bi.status = 'accepted'
|
||||
AND bi.converted_action_id IS NULL
|
||||
ORDER BY bi.updated_at DESC
|
||||
LIMIT %s
|
||||
""",
|
||||
(ctx.tenant_id, initiative_id, remaining),
|
||||
)
|
||||
for row in cur.fetchall():
|
||||
item = dict(row)
|
||||
item["initiative_id"] = str(item["initiative_id"])
|
||||
item["backlog_item_id"] = str(item["backlog_item_id"])
|
||||
candidates.append(item)
|
||||
|
||||
remaining = limit - len(candidates)
|
||||
if remaining > 0:
|
||||
cur.execute(
|
||||
"""
|
||||
SELECT 1 FROM actions a
|
||||
WHERE a.initiative_id = %s AND a.tenant_id = %s
|
||||
AND a.status IN ('open', 'ready', 'in_progress', 'blocked', 'review_required')
|
||||
LIMIT 1
|
||||
""",
|
||||
(initiative_id, ctx.tenant_id),
|
||||
)
|
||||
if not cur.fetchone():
|
||||
cur.execute(
|
||||
"""
|
||||
SELECT title FROM initiatives
|
||||
WHERE id = %s AND tenant_id = %s
|
||||
""",
|
||||
(initiative_id, ctx.tenant_id),
|
||||
)
|
||||
init_row = cur.fetchone()
|
||||
if init_row:
|
||||
candidates.append(
|
||||
{
|
||||
"kind": "create_action",
|
||||
"title": init_row["title"],
|
||||
"summary": "Nächste Maßnahme für Vorhaben anlegen",
|
||||
"initiative_id": initiative_id,
|
||||
"action_id": None,
|
||||
"backlog_item_id": None,
|
||||
"reason_code": "initiative_no_open_action",
|
||||
"recommended_action": "Maßnahme anlegen",
|
||||
}
|
||||
)
|
||||
|
||||
return candidates[:limit]
|
||||
finally:
|
||||
conn.close()
|
||||
):
|
||||
return evaluate(
|
||||
ctx,
|
||||
kind="next_action",
|
||||
limit=limit,
|
||||
initiative_id=initiative_id,
|
||||
)
|
||||
@@ -11,6 +11,7 @@ from psycopg2.extras import RealDictCursor
|
||||
|
||||
from db import get_connection
|
||||
from data_layer.attention import get_next_action_candidates_for_initiative
|
||||
from steering.context import get_steering_context_dto
|
||||
from services.initiatives import get_initiative
|
||||
from tenant_context import TenantContext
|
||||
|
||||
@@ -310,11 +311,18 @@ def get_initiative_steering_snapshot(
|
||||
ctx, initiative_id=initiative_id, limit=5
|
||||
)
|
||||
|
||||
steering = get_steering_context_dto(ctx, initiative_id=initiative_id)
|
||||
|
||||
return {
|
||||
"initiative_id": initiative_id,
|
||||
"initiative_title": initiative["title"],
|
||||
"initiative_status": initiative["status"],
|
||||
"lifecycle_state": steering["lifecycle_state"],
|
||||
"lifecycle_label": steering["lifecycle_label"],
|
||||
"method_key": steering["method_key"],
|
||||
"method_version": steering["method_version"],
|
||||
"operating_phase": phase,
|
||||
"operating_phase_deprecated": True,
|
||||
"phase_signals": phase_signals,
|
||||
"upcoming_milestones": upcoming_milestones,
|
||||
"next_actions": next_actions,
|
||||
|
||||
@@ -11,6 +11,8 @@ from fastapi.middleware.cors import CORSMiddleware
|
||||
from db import check_db
|
||||
from version import APP_NAME, APP_VERSION, DB_SCHEMA_VERSION
|
||||
|
||||
import steering # noqa: F401 — bootstrap hooks & methods
|
||||
|
||||
if os.getenv("KAIRO_DB_READY") != "1":
|
||||
if os.getenv("SKIP_DB_MIGRATE", "").strip().lower() not in ("1", "true", "yes"):
|
||||
import run_migrations
|
||||
|
||||
@@ -0,0 +1,41 @@
|
||||
-- AP1.0: Steering Foundation — steering_contexts
|
||||
|
||||
CREATE TABLE steering_contexts (
|
||||
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
|
||||
tenant_id UUID NOT NULL REFERENCES tenants(id) ON DELETE CASCADE,
|
||||
initiative_id UUID NOT NULL REFERENCES initiatives(id) ON DELETE CASCADE,
|
||||
method_key VARCHAR(64) NOT NULL DEFAULT 'generic_operating',
|
||||
method_version VARCHAR(32) NOT NULL DEFAULT '0.1.0',
|
||||
lifecycle_state VARCHAR(64) NOT NULL DEFAULT 'intake',
|
||||
lifecycle_metadata JSONB NOT NULL DEFAULT '{}',
|
||||
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
|
||||
updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
|
||||
UNIQUE (tenant_id, initiative_id)
|
||||
);
|
||||
|
||||
CREATE INDEX idx_steering_contexts_tenant ON steering_contexts(tenant_id);
|
||||
CREATE INDEX idx_steering_contexts_initiative ON steering_contexts(tenant_id, initiative_id);
|
||||
|
||||
INSERT INTO steering_contexts (tenant_id, initiative_id, lifecycle_state)
|
||||
SELECT
|
||||
i.tenant_id,
|
||||
i.id,
|
||||
CASE
|
||||
WHEN i.status IN ('completed', 'archived') THEN 'closure'
|
||||
WHEN EXISTS (
|
||||
SELECT 1 FROM actions a
|
||||
WHERE a.initiative_id = i.id
|
||||
AND a.tenant_id = i.tenant_id
|
||||
AND a.status IN (
|
||||
'open', 'ready', 'in_progress', 'blocked', 'review_required'
|
||||
)
|
||||
) THEN 'action_selection'
|
||||
WHEN EXISTS (
|
||||
SELECT 1 FROM backlog_items bi
|
||||
WHERE bi.initiative_id = i.id
|
||||
AND bi.tenant_id = i.tenant_id
|
||||
AND bi.status IN ('new', 'triaged', 'accepted')
|
||||
) THEN 'structure_setup'
|
||||
ELSE 'planning'
|
||||
END
|
||||
FROM initiatives i;
|
||||
@@ -9,6 +9,7 @@ from capabilities import require_capability
|
||||
from data_layer import initiative_snapshot as dl_initiative_snapshot
|
||||
from fastapi import APIRouter, Depends, HTTPException
|
||||
from pydantic import BaseModel, Field
|
||||
from steering.context import get_steering_context_dto
|
||||
from services import actions as action_service
|
||||
from services import backlog as backlog_service
|
||||
from services import blockers as blocker_service
|
||||
@@ -168,6 +169,18 @@ def get_initiative_steering_snapshot(
|
||||
return snapshot
|
||||
|
||||
|
||||
@router.get("/{initiative_id}/steering-context")
|
||||
def get_initiative_steering_context(
|
||||
initiative_id: str,
|
||||
ctx: TenantContext = Depends(require_capability("kairo.initiative.read")),
|
||||
):
|
||||
if not initiative_service.get_initiative(
|
||||
tenant_id=ctx.tenant_id, initiative_id=initiative_id
|
||||
):
|
||||
raise HTTPException(status_code=404, detail="Vorhaben nicht gefunden")
|
||||
return get_steering_context_dto(ctx, initiative_id=initiative_id)
|
||||
|
||||
|
||||
@router.patch("/{initiative_id}")
|
||||
def update_initiative(
|
||||
initiative_id: str,
|
||||
|
||||
@@ -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,
|
||||
)
|
||||
@@ -0,0 +1,15 @@
|
||||
"""Adaptive Steering Core — AP1.0 Foundation."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from steering.hooks.registry import register_builtin_hooks
|
||||
from steering.methods.registrations import generic_operating
|
||||
|
||||
|
||||
def bootstrap_steering() -> None:
|
||||
"""Register built-in hooks and methods (idempotent)."""
|
||||
register_builtin_hooks()
|
||||
generic_operating.register()
|
||||
|
||||
|
||||
bootstrap_steering()
|
||||
@@ -0,0 +1,35 @@
|
||||
"""SteeringContext read helpers — AP1.0."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from typing import Any, Optional
|
||||
|
||||
from services import steering_context as sc_service
|
||||
from steering.lifecycle.states import lifecycle_label
|
||||
from tenant_context import TenantContext
|
||||
|
||||
|
||||
def get_steering_context_dto(
|
||||
ctx: TenantContext, *, initiative_id: str
|
||||
) -> Optional[dict[str, Any]]:
|
||||
row = sc_service.get_steering_context(
|
||||
tenant_id=ctx.tenant_id, initiative_id=initiative_id
|
||||
)
|
||||
if not row:
|
||||
row = sc_service.ensure_steering_context(
|
||||
tenant_id=ctx.tenant_id,
|
||||
initiative_id=initiative_id,
|
||||
)
|
||||
return {
|
||||
**row,
|
||||
"lifecycle_label": lifecycle_label(row["lifecycle_state"]),
|
||||
}
|
||||
|
||||
|
||||
def get_or_create_for_initiative(
|
||||
ctx: TenantContext, *, initiative_id: str
|
||||
) -> dict[str, Any]:
|
||||
dto = get_steering_context_dto(ctx, initiative_id=initiative_id)
|
||||
if not dto:
|
||||
raise ValueError("SteeringContext nicht verfügbar")
|
||||
return dto
|
||||
Whitespace-only changes.
@@ -0,0 +1,35 @@
|
||||
"""Hook dispatch — AP1.0 stub with audit trail."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from typing import Any, Optional
|
||||
|
||||
from steering.hooks.registry import get_hook
|
||||
from tenant_context import TenantContext
|
||||
|
||||
|
||||
def dispatch_hook(
|
||||
slug: str,
|
||||
ctx: TenantContext | None,
|
||||
payload: dict[str, Any],
|
||||
*,
|
||||
tenant_id: Optional[str] = None,
|
||||
user_id: Optional[str] = None,
|
||||
) -> list[dict[str, Any]]:
|
||||
"""Dispatch hook — AP1.0: audit only, no handlers yet."""
|
||||
hook = get_hook(slug)
|
||||
if not hook:
|
||||
return [{"kind": "hook_unknown", "slug": slug}]
|
||||
|
||||
tid = tenant_id or (ctx.tenant_id if ctx else None)
|
||||
if tid and user_id:
|
||||
from services.audit import log_audit
|
||||
|
||||
log_audit(
|
||||
f"steering.hook.{slug}",
|
||||
user_id=user_id,
|
||||
tenant_id=tid,
|
||||
details={"slug": slug, "payload": payload},
|
||||
)
|
||||
|
||||
return [{"kind": "hook_dispatched", "slug": slug, "lifecycle_step": hook.lifecycle_step}]
|
||||
@@ -0,0 +1,63 @@
|
||||
"""Hook Registry — stabile Einhängepunkte (AP1.0 definitions only)."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from dataclasses import dataclass
|
||||
|
||||
_HOOKS: dict[str, "HookDefinition"] = {}
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class HookDefinition:
|
||||
slug: str
|
||||
lifecycle_step: str
|
||||
description: str
|
||||
since_version: str = "0.11.0"
|
||||
|
||||
|
||||
def register_hook(defn: HookDefinition) -> None:
|
||||
if defn.slug in _HOOKS:
|
||||
raise ValueError(f"Hook already registered: {defn.slug}")
|
||||
_HOOKS[defn.slug] = defn
|
||||
|
||||
|
||||
def get_hook(slug: str) -> HookDefinition | None:
|
||||
return _HOOKS.get(slug)
|
||||
|
||||
|
||||
def list_hooks() -> tuple[HookDefinition, ...]:
|
||||
return tuple(_HOOKS.values())
|
||||
|
||||
|
||||
def clear_hooks_for_tests() -> None:
|
||||
_HOOKS.clear()
|
||||
|
||||
|
||||
def register_builtin_hooks() -> None:
|
||||
"""Slugs aus Target Architecture §7.3 — Handler folgen in AP1.3."""
|
||||
builtins = (
|
||||
("on_goal_captured", "intake", "Ziel erfasst"),
|
||||
("on_method_selected", "method_selection", "Methode gewählt"),
|
||||
("on_structure_required", "structure_setup", "Struktur erforderlich"),
|
||||
("on_backlog_required", "structure_setup", "Backlog erforderlich"),
|
||||
("on_next_action_requested", "action_selection", "Nächste Aktion angefragt"),
|
||||
("on_assignment_required", "assignment", "Zuweisung erforderlich"),
|
||||
("on_blocker_open", "waiting", "Blocker gemeldet"),
|
||||
("on_blocker_resolved", "action_selection", "Blocker geschlossen"),
|
||||
("on_wait_started", "waiting", "Warten begonnen"),
|
||||
("on_result_received", "result_intake", "Ergebnis eingegangen"),
|
||||
("on_evidence_required", "validation", "Nachweis erforderlich"),
|
||||
("on_review_due", "review", "Review fällig"),
|
||||
("on_review_completed", "review", "Review abgeschlossen"),
|
||||
("on_replan_required", "adaptation", "Anpassung nötig"),
|
||||
("on_closure_requested", "closure", "Abschluss angefragt"),
|
||||
)
|
||||
for slug, step, desc in builtins:
|
||||
if slug not in _HOOKS:
|
||||
register_hook(
|
||||
HookDefinition(
|
||||
slug=slug,
|
||||
lifecycle_step=step,
|
||||
description=desc,
|
||||
)
|
||||
)
|
||||
Whitespace-only changes.
@@ -0,0 +1,78 @@
|
||||
"""Lifecycle orchestrator — persistierter Standard Lifecycle (AP1.0)."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from typing import Any, Optional
|
||||
|
||||
from services import steering_context as sc_service
|
||||
from steering.lifecycle.states import validate_lifecycle_state
|
||||
from tenant_context import TenantContext
|
||||
|
||||
|
||||
def get_lifecycle_state(ctx: TenantContext, initiative_id: str) -> str:
|
||||
row = sc_service.get_steering_context(
|
||||
tenant_id=ctx.tenant_id, initiative_id=initiative_id
|
||||
)
|
||||
if row:
|
||||
return row["lifecycle_state"]
|
||||
created = sc_service.ensure_steering_context(
|
||||
tenant_id=ctx.tenant_id,
|
||||
initiative_id=initiative_id,
|
||||
initial_state="intake",
|
||||
)
|
||||
return created["lifecycle_state"]
|
||||
|
||||
|
||||
def transition(
|
||||
ctx: TenantContext | None,
|
||||
initiative_id: str,
|
||||
to_state: str,
|
||||
*,
|
||||
reason: str = "",
|
||||
tenant_id: Optional[str] = None,
|
||||
user_id: Optional[str] = None,
|
||||
) -> dict[str, Any]:
|
||||
"""Lifecycle-Transition — AP1.0: freier Übergang (Guards in AP1.1)."""
|
||||
validate_lifecycle_state(to_state)
|
||||
tid = tenant_id or (ctx.tenant_id if ctx else None)
|
||||
if not tid:
|
||||
raise ValueError("tenant_id erforderlich")
|
||||
|
||||
existing = sc_service.get_steering_context(tenant_id=tid, initiative_id=initiative_id)
|
||||
if not existing:
|
||||
existing = sc_service.ensure_steering_context(
|
||||
tenant_id=tid,
|
||||
initiative_id=initiative_id,
|
||||
initial_state=to_state,
|
||||
)
|
||||
return {
|
||||
"initiative_id": initiative_id,
|
||||
"from_state": None,
|
||||
"to_state": to_state,
|
||||
"reason": reason,
|
||||
}
|
||||
|
||||
from_state = existing["lifecycle_state"]
|
||||
if from_state == to_state:
|
||||
return {
|
||||
"initiative_id": initiative_id,
|
||||
"from_state": from_state,
|
||||
"to_state": to_state,
|
||||
"reason": reason,
|
||||
"unchanged": True,
|
||||
}
|
||||
|
||||
updated = sc_service.update_lifecycle_state(
|
||||
tenant_id=tid,
|
||||
initiative_id=initiative_id,
|
||||
lifecycle_state=to_state,
|
||||
user_id=user_id,
|
||||
reason=reason,
|
||||
from_state=from_state,
|
||||
)
|
||||
return {
|
||||
"initiative_id": initiative_id,
|
||||
"from_state": from_state,
|
||||
"to_state": updated["lifecycle_state"],
|
||||
"reason": reason,
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
"""Standard Lifecycle — gemeinsame Basis (AP1.0)."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
STANDARD_LIFECYCLE_STEPS: tuple[str, ...] = (
|
||||
"intake",
|
||||
"method_selection",
|
||||
"structure_setup",
|
||||
"planning",
|
||||
"action_selection",
|
||||
"assignment",
|
||||
"waiting",
|
||||
"result_intake",
|
||||
"validation",
|
||||
"review",
|
||||
"adaptation",
|
||||
"closure",
|
||||
)
|
||||
|
||||
LIFECYCLE_LABELS: dict[str, str] = {
|
||||
"intake": "Aufnahme",
|
||||
"method_selection": "Methode wählen",
|
||||
"structure_setup": "Struktur aufbauen",
|
||||
"planning": "Planung",
|
||||
"action_selection": "Maßnahmen auswählen",
|
||||
"assignment": "Zuweisen",
|
||||
"waiting": "Warten",
|
||||
"result_intake": "Ergebnis aufnehmen",
|
||||
"validation": "Validierung",
|
||||
"review": "Review",
|
||||
"adaptation": "Anpassung",
|
||||
"closure": "Abschluss",
|
||||
}
|
||||
|
||||
|
||||
def lifecycle_label(state: str) -> str:
|
||||
return LIFECYCLE_LABELS.get(state, state)
|
||||
|
||||
|
||||
def validate_lifecycle_state(state: str) -> None:
|
||||
if state not in STANDARD_LIFECYCLE_STEPS:
|
||||
raise ValueError(f"Ungültiger Lifecycle-State: {state}")
|
||||
Whitespace-only changes.
Whitespace-only changes.
@@ -0,0 +1,19 @@
|
||||
"""Built-in method: generic_operating — AP1.0."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from steering.lifecycle.states import STANDARD_LIFECYCLE_STEPS
|
||||
from steering.methods.registry import MethodDefinition, get_method, register_method
|
||||
|
||||
|
||||
def register() -> None:
|
||||
if get_method("generic_operating"):
|
||||
return
|
||||
register_method(
|
||||
MethodDefinition(
|
||||
key="generic_operating",
|
||||
version="0.1.0",
|
||||
description="Standard-Lifecycle ohne methodenspezifische Spezialisierung",
|
||||
default_lifecycle_steps=STANDARD_LIFECYCLE_STEPS,
|
||||
)
|
||||
)
|
||||
@@ -0,0 +1,33 @@
|
||||
"""Method Registry — built-in steering methods (AP1.0)."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from dataclasses import dataclass
|
||||
|
||||
_METHODS: dict[str, "MethodDefinition"] = {}
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class MethodDefinition:
|
||||
key: str
|
||||
version: str
|
||||
description: str
|
||||
default_lifecycle_steps: tuple[str, ...]
|
||||
|
||||
|
||||
def register_method(defn: MethodDefinition) -> None:
|
||||
if defn.key in _METHODS:
|
||||
raise ValueError(f"Method already registered: {defn.key}")
|
||||
_METHODS[defn.key] = defn
|
||||
|
||||
|
||||
def get_method(key: str) -> MethodDefinition | None:
|
||||
return _METHODS.get(key)
|
||||
|
||||
|
||||
def list_methods() -> tuple[MethodDefinition, ...]:
|
||||
return tuple(_METHODS.values())
|
||||
|
||||
|
||||
def clear_methods_for_tests() -> None:
|
||||
_METHODS.clear()
|
||||
Whitespace-only changes.
@@ -0,0 +1,706 @@
|
||||
"""Default Signal Rule Provider — regelbasiert, tenant-scoped (AP0.8a, AP1.0)."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from typing import Any, Literal, Optional
|
||||
|
||||
from psycopg2.extras import RealDictCursor
|
||||
|
||||
from db import get_connection
|
||||
from tenant_context import TenantContext
|
||||
|
||||
AttentionKind = Literal[
|
||||
"blocked_action",
|
||||
"open_blocker",
|
||||
"high_priority_action",
|
||||
"unassigned_action",
|
||||
"initiative_without_next_action",
|
||||
"stale_initiative",
|
||||
"milestone_at_risk",
|
||||
"overdue_action",
|
||||
"review_due",
|
||||
"recurring_due",
|
||||
"action_review_required",
|
||||
]
|
||||
|
||||
NextActionKind = Literal[
|
||||
"assign_action",
|
||||
"resolve_blocker",
|
||||
"create_action",
|
||||
"convert_backlog",
|
||||
"review_milestone",
|
||||
]
|
||||
|
||||
Severity = Literal["info", "warning", "critical"]
|
||||
|
||||
_SEVERITY_ORDER = {"critical": 0, "warning": 1, "info": 2}
|
||||
|
||||
OPEN_BLOCKER_STATUSES = ("open", "in_progress")
|
||||
OPEN_ACTION_STATUSES = ("open", "ready", "in_progress", "blocked", "review_required")
|
||||
ACTIVE_INITIATIVE_STATUSES = ("active", "paused")
|
||||
|
||||
|
||||
def _serialize_attention(row: dict[str, Any]) -> dict[str, Any]:
|
||||
item = dict(row)
|
||||
for key in (
|
||||
"scope_id",
|
||||
"initiative_id",
|
||||
"action_id",
|
||||
"blocker_id",
|
||||
"milestone_id",
|
||||
"review_id",
|
||||
"recurring_element_id",
|
||||
):
|
||||
if item.get(key):
|
||||
item[key] = str(item[key])
|
||||
return item
|
||||
|
||||
|
||||
def _blocked_actions(cur, ctx: TenantContext) -> list[dict[str, Any]]:
|
||||
cur.execute(
|
||||
"""
|
||||
SELECT
|
||||
'blocked_action' AS kind,
|
||||
'critical' AS severity,
|
||||
a.title AS title,
|
||||
'Maßnahme ist blockiert' AS summary,
|
||||
'action' AS scope_type,
|
||||
a.id AS scope_id,
|
||||
a.initiative_id,
|
||||
a.id AS action_id,
|
||||
NULL::uuid AS blocker_id,
|
||||
NULL::uuid AS milestone_id,
|
||||
'action_blocked' AS reason_code,
|
||||
'actions' AS data_source
|
||||
FROM actions a
|
||||
WHERE a.tenant_id = %s AND a.status = 'blocked'
|
||||
ORDER BY a.updated_at DESC
|
||||
LIMIT 50
|
||||
""",
|
||||
(ctx.tenant_id,),
|
||||
)
|
||||
return [_serialize_attention(dict(r)) for r in cur.fetchall()]
|
||||
|
||||
|
||||
def _open_blockers(cur, ctx: TenantContext) -> list[dict[str, Any]]:
|
||||
cur.execute(
|
||||
"""
|
||||
SELECT
|
||||
'open_blocker' AS kind,
|
||||
'warning' AS severity,
|
||||
b.title AS title,
|
||||
'Offener Blocker im Vorhaben' AS summary,
|
||||
'blocker' AS scope_type,
|
||||
b.id AS scope_id,
|
||||
b.initiative_id,
|
||||
b.action_id,
|
||||
b.id AS blocker_id,
|
||||
NULL::uuid AS milestone_id,
|
||||
'blocker_open' AS reason_code,
|
||||
'blockers' AS data_source
|
||||
FROM blockers b
|
||||
WHERE b.tenant_id = %s AND b.status IN ('open', 'in_progress')
|
||||
ORDER BY b.updated_at DESC
|
||||
LIMIT 50
|
||||
""",
|
||||
(ctx.tenant_id,),
|
||||
)
|
||||
items = []
|
||||
for row in cur.fetchall():
|
||||
item = _serialize_attention(dict(row))
|
||||
if item.get("action_id"):
|
||||
item["action_id"] = str(item["action_id"])
|
||||
items.append(item)
|
||||
return items
|
||||
|
||||
|
||||
def _high_priority_actions(cur, ctx: TenantContext) -> list[dict[str, Any]]:
|
||||
actor_filter = ""
|
||||
params: list[Any] = [ctx.tenant_id]
|
||||
if ctx.actor_id:
|
||||
actor_filter = """
|
||||
AND EXISTS (
|
||||
SELECT 1 FROM action_assignments aa
|
||||
WHERE aa.action_id = a.id
|
||||
AND aa.tenant_id = a.tenant_id
|
||||
AND aa.actor_id = %s
|
||||
)
|
||||
"""
|
||||
params.append(ctx.actor_id)
|
||||
else:
|
||||
return []
|
||||
|
||||
cur.execute(
|
||||
f"""
|
||||
SELECT
|
||||
'high_priority_action' AS kind,
|
||||
'warning' AS severity,
|
||||
a.title AS title,
|
||||
'High-Priority Maßnahme offen' AS summary,
|
||||
'action' AS scope_type,
|
||||
a.id AS scope_id,
|
||||
a.initiative_id,
|
||||
a.id AS action_id,
|
||||
NULL::uuid AS blocker_id,
|
||||
NULL::uuid AS milestone_id,
|
||||
'action_high_priority_open' AS reason_code,
|
||||
'actions' AS data_source
|
||||
FROM actions a
|
||||
WHERE a.tenant_id = %s
|
||||
AND a.priority = 'high'
|
||||
AND a.status IN ('open', 'ready', 'in_progress')
|
||||
{actor_filter}
|
||||
ORDER BY a.updated_at DESC
|
||||
LIMIT 30
|
||||
""",
|
||||
tuple(params),
|
||||
)
|
||||
return [_serialize_attention(dict(r)) for r in cur.fetchall()]
|
||||
|
||||
|
||||
def _unassigned_actions(cur, ctx: TenantContext) -> list[dict[str, Any]]:
|
||||
cur.execute(
|
||||
"""
|
||||
SELECT
|
||||
'unassigned_action' AS kind,
|
||||
'warning' AS severity,
|
||||
a.title AS title,
|
||||
'Maßnahme ohne Zuweisung' AS summary,
|
||||
'action' AS scope_type,
|
||||
a.id AS scope_id,
|
||||
a.initiative_id,
|
||||
a.id AS action_id,
|
||||
NULL::uuid AS blocker_id,
|
||||
NULL::uuid AS milestone_id,
|
||||
'action_unassigned' AS reason_code,
|
||||
'actions' AS data_source
|
||||
FROM actions a
|
||||
WHERE a.tenant_id = %s
|
||||
AND a.status IN ('open', 'ready', 'in_progress')
|
||||
AND NOT EXISTS (
|
||||
SELECT 1 FROM action_assignments aa
|
||||
WHERE aa.action_id = a.id AND aa.tenant_id = a.tenant_id
|
||||
)
|
||||
ORDER BY a.updated_at DESC
|
||||
LIMIT 30
|
||||
""",
|
||||
(ctx.tenant_id,),
|
||||
)
|
||||
return [_serialize_attention(dict(r)) for r in cur.fetchall()]
|
||||
|
||||
|
||||
def _initiatives_without_next_action(cur, ctx: TenantContext) -> list[dict[str, Any]]:
|
||||
cur.execute(
|
||||
"""
|
||||
SELECT
|
||||
'initiative_without_next_action' AS kind,
|
||||
'info' AS severity,
|
||||
i.title AS title,
|
||||
'Keine offene nächste Maßnahme' AS summary,
|
||||
'initiative' AS scope_type,
|
||||
i.id AS scope_id,
|
||||
i.id AS initiative_id,
|
||||
NULL::uuid AS action_id,
|
||||
NULL::uuid AS blocker_id,
|
||||
NULL::uuid AS milestone_id,
|
||||
'initiative_no_open_action' AS reason_code,
|
||||
'initiatives' AS data_source
|
||||
FROM initiatives i
|
||||
WHERE i.tenant_id = %s
|
||||
AND i.status IN ('active', 'paused')
|
||||
AND NOT EXISTS (
|
||||
SELECT 1 FROM actions a
|
||||
WHERE a.initiative_id = i.id
|
||||
AND a.tenant_id = i.tenant_id
|
||||
AND a.status IN ('open', 'ready', 'in_progress', 'blocked', 'review_required')
|
||||
)
|
||||
ORDER BY i.updated_at DESC
|
||||
LIMIT 30
|
||||
""",
|
||||
(ctx.tenant_id,),
|
||||
)
|
||||
return [_serialize_attention(dict(r)) for r in cur.fetchall()]
|
||||
|
||||
|
||||
def _stale_initiatives(cur, ctx: TenantContext) -> list[dict[str, Any]]:
|
||||
cur.execute(
|
||||
"""
|
||||
SELECT
|
||||
'stale_initiative' AS kind,
|
||||
'info' AS severity,
|
||||
i.title AS title,
|
||||
'Vorhaben seit über 14 Tagen unverändert' AS summary,
|
||||
'initiative' AS scope_type,
|
||||
i.id AS scope_id,
|
||||
i.id AS initiative_id,
|
||||
NULL::uuid AS action_id,
|
||||
NULL::uuid AS blocker_id,
|
||||
NULL::uuid AS milestone_id,
|
||||
'initiative_stale' AS reason_code,
|
||||
'initiatives' AS data_source
|
||||
FROM initiatives i
|
||||
WHERE i.tenant_id = %s
|
||||
AND i.status = 'active'
|
||||
AND i.updated_at < NOW() - INTERVAL '14 days'
|
||||
ORDER BY i.updated_at ASC
|
||||
LIMIT 20
|
||||
""",
|
||||
(ctx.tenant_id,),
|
||||
)
|
||||
return [_serialize_attention(dict(r)) for r in cur.fetchall()]
|
||||
|
||||
|
||||
def _milestones_at_risk(cur, ctx: TenantContext) -> list[dict[str, Any]]:
|
||||
cur.execute(
|
||||
"""
|
||||
SELECT
|
||||
'milestone_at_risk' AS kind,
|
||||
'warning' AS severity,
|
||||
m.title AS title,
|
||||
'Meilenstein als gefährdet markiert' AS summary,
|
||||
'milestone' AS scope_type,
|
||||
m.id AS scope_id,
|
||||
m.initiative_id,
|
||||
NULL::uuid AS action_id,
|
||||
NULL::uuid AS blocker_id,
|
||||
m.id AS milestone_id,
|
||||
'milestone_at_risk' AS reason_code,
|
||||
'milestones' AS data_source
|
||||
FROM milestones m
|
||||
WHERE m.tenant_id = %s AND m.status = 'at_risk'
|
||||
ORDER BY m.updated_at DESC
|
||||
LIMIT 20
|
||||
""",
|
||||
(ctx.tenant_id,),
|
||||
)
|
||||
return [_serialize_attention(dict(r)) for r in cur.fetchall()]
|
||||
|
||||
|
||||
def _actions_review_required(cur, ctx: TenantContext) -> list[dict[str, Any]]:
|
||||
cur.execute(
|
||||
"""
|
||||
SELECT
|
||||
'action_review_required' AS kind,
|
||||
'warning' AS severity,
|
||||
a.title AS title,
|
||||
'Ma├ƒnahme wartet auf Review ÔÇö kein geplantes Review verkn├╝pft' AS summary,
|
||||
'action' AS scope_type,
|
||||
a.id AS scope_id,
|
||||
a.initiative_id,
|
||||
a.id AS action_id,
|
||||
NULL::uuid AS blocker_id,
|
||||
NULL::uuid AS milestone_id,
|
||||
NULL::uuid AS review_id,
|
||||
NULL::uuid AS recurring_element_id,
|
||||
'action_review_required_no_review' AS reason_code,
|
||||
'actions' AS data_source
|
||||
FROM actions a
|
||||
WHERE a.tenant_id = %s
|
||||
AND a.status = 'review_required'
|
||||
AND NOT EXISTS (
|
||||
SELECT 1 FROM reviews r
|
||||
WHERE r.tenant_id = a.tenant_id
|
||||
AND r.action_id = a.id
|
||||
AND r.status = 'planned'
|
||||
)
|
||||
ORDER BY a.updated_at DESC
|
||||
LIMIT 20
|
||||
""",
|
||||
(ctx.tenant_id,),
|
||||
)
|
||||
return [_serialize_attention(dict(r)) for r in cur.fetchall()]
|
||||
|
||||
|
||||
def _overdue_actions(cur, ctx: TenantContext) -> list[dict[str, Any]]:
|
||||
cur.execute(
|
||||
"""
|
||||
SELECT
|
||||
'overdue_action' AS kind,
|
||||
'warning' AS severity,
|
||||
a.title AS title,
|
||||
'Maßnahme überfällig' AS summary,
|
||||
'action' AS scope_type,
|
||||
a.id AS scope_id,
|
||||
a.initiative_id,
|
||||
a.id AS action_id,
|
||||
NULL::uuid AS blocker_id,
|
||||
NULL::uuid AS milestone_id,
|
||||
NULL::uuid AS review_id,
|
||||
NULL::uuid AS recurring_element_id,
|
||||
'action_overdue' AS reason_code,
|
||||
'actions' AS data_source
|
||||
FROM actions a
|
||||
WHERE a.tenant_id = %s
|
||||
AND a.due_at IS NOT NULL
|
||||
AND a.due_at < NOW()
|
||||
AND a.status IN ('open', 'ready', 'in_progress', 'blocked', 'review_required')
|
||||
ORDER BY a.due_at ASC
|
||||
LIMIT 30
|
||||
""",
|
||||
(ctx.tenant_id,),
|
||||
)
|
||||
return [_serialize_attention(dict(r)) for r in cur.fetchall()]
|
||||
|
||||
|
||||
def _reviews_due(cur, ctx: TenantContext) -> list[dict[str, Any]]:
|
||||
cur.execute(
|
||||
"""
|
||||
SELECT
|
||||
'review_due' AS kind,
|
||||
'warning' AS severity,
|
||||
r.title AS title,
|
||||
'Review fällig' AS summary,
|
||||
'review' AS scope_type,
|
||||
r.id AS scope_id,
|
||||
r.initiative_id,
|
||||
r.action_id,
|
||||
NULL::uuid AS blocker_id,
|
||||
r.milestone_id,
|
||||
r.id AS review_id,
|
||||
NULL::uuid AS recurring_element_id,
|
||||
'review_due' AS reason_code,
|
||||
'reviews' AS data_source
|
||||
FROM reviews r
|
||||
WHERE r.tenant_id = %s
|
||||
AND r.status = 'planned'
|
||||
AND r.due_at IS NOT NULL
|
||||
AND r.due_at <= NOW()
|
||||
ORDER BY r.due_at ASC
|
||||
LIMIT 20
|
||||
""",
|
||||
(ctx.tenant_id,),
|
||||
)
|
||||
items = []
|
||||
for row in cur.fetchall():
|
||||
item = _serialize_attention(dict(row))
|
||||
for fk in ("action_id", "milestone_id"):
|
||||
if item.get(fk):
|
||||
item[fk] = str(item[fk])
|
||||
items.append(item)
|
||||
return items
|
||||
|
||||
|
||||
def _recurring_due(cur, ctx: TenantContext) -> list[dict[str, Any]]:
|
||||
cur.execute(
|
||||
"""
|
||||
SELECT
|
||||
'recurring_due' AS kind,
|
||||
'info' AS severity,
|
||||
re.title AS title,
|
||||
'Wiederkehrendes Element fällig' AS summary,
|
||||
'recurring_element' AS scope_type,
|
||||
re.id AS scope_id,
|
||||
re.initiative_id,
|
||||
NULL::uuid AS action_id,
|
||||
NULL::uuid AS blocker_id,
|
||||
NULL::uuid AS milestone_id,
|
||||
NULL::uuid AS review_id,
|
||||
re.id AS recurring_element_id,
|
||||
'recurring_due' AS reason_code,
|
||||
'recurring_elements' AS data_source
|
||||
FROM recurring_elements re
|
||||
WHERE re.tenant_id = %s
|
||||
AND re.status = 'active'
|
||||
AND re.next_due_at IS NOT NULL
|
||||
AND re.next_due_at <= NOW()
|
||||
ORDER BY re.next_due_at ASC
|
||||
LIMIT 20
|
||||
""",
|
||||
(ctx.tenant_id,),
|
||||
)
|
||||
return [_serialize_attention(dict(r)) for r in cur.fetchall()]
|
||||
|
||||
|
||||
def get_attention_items(ctx: TenantContext) -> list[dict[str, Any]]:
|
||||
"""Regelbasierte Attention Items ÔÇö tenant-scoped, erkl├ñrbar."""
|
||||
conn = get_connection()
|
||||
try:
|
||||
with conn.cursor(cursor_factory=RealDictCursor) as cur:
|
||||
items: list[dict[str, Any]] = []
|
||||
items.extend(_blocked_actions(cur, ctx))
|
||||
items.extend(_open_blockers(cur, ctx))
|
||||
items.extend(_high_priority_actions(cur, ctx))
|
||||
items.extend(_unassigned_actions(cur, ctx))
|
||||
items.extend(_initiatives_without_next_action(cur, ctx))
|
||||
items.extend(_stale_initiatives(cur, ctx))
|
||||
items.extend(_milestones_at_risk(cur, ctx))
|
||||
items.extend(_overdue_actions(cur, ctx))
|
||||
items.extend(_actions_review_required(cur, ctx))
|
||||
items.extend(_reviews_due(cur, ctx))
|
||||
items.extend(_recurring_due(cur, ctx))
|
||||
|
||||
items.sort(key=lambda x: _SEVERITY_ORDER.get(x["severity"], 99))
|
||||
return items
|
||||
finally:
|
||||
conn.close()
|
||||
|
||||
|
||||
def get_next_action_candidates(
|
||||
ctx: TenantContext, *, limit: int = 10
|
||||
) -> list[dict[str, Any]]:
|
||||
"""Regelbasierte NextActionCandidates ÔÇö limitiert, tenant-scoped."""
|
||||
if limit < 1:
|
||||
limit = 1
|
||||
if limit > 50:
|
||||
limit = 50
|
||||
|
||||
conn = get_connection()
|
||||
candidates: list[dict[str, Any]] = []
|
||||
try:
|
||||
with conn.cursor(cursor_factory=RealDictCursor) as cur:
|
||||
cur.execute(
|
||||
"""
|
||||
SELECT
|
||||
'resolve_blocker' AS kind,
|
||||
b.title AS title,
|
||||
'Blocker klären oder Status aktualisieren' AS summary,
|
||||
b.initiative_id,
|
||||
b.action_id,
|
||||
NULL::uuid AS backlog_item_id,
|
||||
'blocker_open' AS reason_code,
|
||||
'Blocker bearbeiten' AS recommended_action
|
||||
FROM blockers b
|
||||
WHERE b.tenant_id = %s AND b.status IN ('open', 'in_progress')
|
||||
ORDER BY b.updated_at DESC
|
||||
LIMIT %s
|
||||
""",
|
||||
(ctx.tenant_id, limit),
|
||||
)
|
||||
for row in cur.fetchall():
|
||||
item = dict(row)
|
||||
item["initiative_id"] = str(item["initiative_id"])
|
||||
if item.get("action_id"):
|
||||
item["action_id"] = str(item["action_id"])
|
||||
candidates.append(item)
|
||||
|
||||
remaining = limit - len(candidates)
|
||||
if remaining > 0:
|
||||
cur.execute(
|
||||
"""
|
||||
SELECT
|
||||
'assign_action' AS kind,
|
||||
a.title AS title,
|
||||
'Actor zuweisen' AS summary,
|
||||
a.initiative_id,
|
||||
a.id AS action_id,
|
||||
NULL::uuid AS backlog_item_id,
|
||||
'action_unassigned' AS reason_code,
|
||||
'Maßnahme zuweisen' AS recommended_action
|
||||
FROM actions a
|
||||
WHERE a.tenant_id = %s
|
||||
AND a.status IN ('open', 'ready', 'in_progress')
|
||||
AND NOT EXISTS (
|
||||
SELECT 1 FROM action_assignments aa
|
||||
WHERE aa.action_id = a.id AND aa.tenant_id = a.tenant_id
|
||||
)
|
||||
ORDER BY a.updated_at DESC
|
||||
LIMIT %s
|
||||
""",
|
||||
(ctx.tenant_id, remaining),
|
||||
)
|
||||
for row in cur.fetchall():
|
||||
item = dict(row)
|
||||
item["initiative_id"] = str(item["initiative_id"])
|
||||
item["action_id"] = str(item["action_id"])
|
||||
candidates.append(item)
|
||||
|
||||
remaining = limit - len(candidates)
|
||||
if remaining > 0:
|
||||
cur.execute(
|
||||
"""
|
||||
SELECT
|
||||
'convert_backlog' AS kind,
|
||||
bi.title AS title,
|
||||
'Freigegebenes Backlog-Item in Maßnahme umwandeln' AS summary,
|
||||
bi.initiative_id,
|
||||
NULL::uuid AS action_id,
|
||||
bi.id AS backlog_item_id,
|
||||
'backlog_accepted_not_converted' AS reason_code,
|
||||
'In Maßnahme umwandeln' AS recommended_action
|
||||
FROM backlog_items bi
|
||||
WHERE bi.tenant_id = %s
|
||||
AND bi.status = 'accepted'
|
||||
AND bi.converted_action_id IS NULL
|
||||
ORDER BY bi.updated_at DESC
|
||||
LIMIT %s
|
||||
""",
|
||||
(ctx.tenant_id, remaining),
|
||||
)
|
||||
for row in cur.fetchall():
|
||||
item = dict(row)
|
||||
item["initiative_id"] = str(item["initiative_id"])
|
||||
item["backlog_item_id"] = str(item["backlog_item_id"])
|
||||
candidates.append(item)
|
||||
|
||||
remaining = limit - len(candidates)
|
||||
if remaining > 0:
|
||||
cur.execute(
|
||||
"""
|
||||
SELECT
|
||||
'create_action' AS kind,
|
||||
i.title AS title,
|
||||
'Nächste Maßnahme für Vorhaben anlegen' AS summary,
|
||||
i.id AS initiative_id,
|
||||
NULL::uuid AS action_id,
|
||||
NULL::uuid AS backlog_item_id,
|
||||
'initiative_no_open_action' AS reason_code,
|
||||
'Maßnahme anlegen' AS recommended_action
|
||||
FROM initiatives i
|
||||
WHERE i.tenant_id = %s
|
||||
AND i.status IN ('active', 'paused')
|
||||
AND NOT EXISTS (
|
||||
SELECT 1 FROM actions a
|
||||
WHERE a.initiative_id = i.id
|
||||
AND a.tenant_id = i.tenant_id
|
||||
AND a.status IN ('open', 'ready', 'in_progress', 'blocked', 'review_required')
|
||||
)
|
||||
ORDER BY i.updated_at DESC
|
||||
LIMIT %s
|
||||
""",
|
||||
(ctx.tenant_id, remaining),
|
||||
)
|
||||
for row in cur.fetchall():
|
||||
item = dict(row)
|
||||
item["initiative_id"] = str(item["initiative_id"])
|
||||
candidates.append(item)
|
||||
|
||||
return candidates[:limit]
|
||||
finally:
|
||||
conn.close()
|
||||
|
||||
|
||||
def get_next_action_candidates_for_initiative(
|
||||
ctx: TenantContext, *, initiative_id: str, limit: int = 5
|
||||
) -> list[dict[str, Any]]:
|
||||
"""NextActionCandidates f├╝r ein Vorhaben ÔÇö tenant-scoped."""
|
||||
if limit < 1:
|
||||
limit = 1
|
||||
if limit > 20:
|
||||
limit = 20
|
||||
|
||||
conn = get_connection()
|
||||
candidates: list[dict[str, Any]] = []
|
||||
try:
|
||||
with conn.cursor(cursor_factory=RealDictCursor) as cur:
|
||||
cur.execute(
|
||||
"""
|
||||
SELECT
|
||||
'resolve_blocker' AS kind,
|
||||
b.title AS title,
|
||||
'Blocker klären oder Status aktualisieren' AS summary,
|
||||
b.initiative_id,
|
||||
b.action_id,
|
||||
NULL::uuid AS backlog_item_id,
|
||||
'blocker_open' AS reason_code,
|
||||
'Blocker bearbeiten' AS recommended_action
|
||||
FROM blockers b
|
||||
WHERE b.tenant_id = %s AND b.initiative_id = %s
|
||||
AND b.status IN ('open', 'in_progress')
|
||||
ORDER BY b.updated_at DESC
|
||||
LIMIT %s
|
||||
""",
|
||||
(ctx.tenant_id, initiative_id, limit),
|
||||
)
|
||||
for row in cur.fetchall():
|
||||
item = dict(row)
|
||||
item["initiative_id"] = str(item["initiative_id"])
|
||||
if item.get("action_id"):
|
||||
item["action_id"] = str(item["action_id"])
|
||||
candidates.append(item)
|
||||
|
||||
remaining = limit - len(candidates)
|
||||
if remaining > 0:
|
||||
cur.execute(
|
||||
"""
|
||||
SELECT
|
||||
'assign_action' AS kind,
|
||||
a.title AS title,
|
||||
'Actor zuweisen' AS summary,
|
||||
a.initiative_id,
|
||||
a.id AS action_id,
|
||||
NULL::uuid AS backlog_item_id,
|
||||
'action_unassigned' AS reason_code,
|
||||
'Maßnahme zuweisen' AS recommended_action
|
||||
FROM actions a
|
||||
WHERE a.tenant_id = %s AND a.initiative_id = %s
|
||||
AND a.status IN ('open', 'ready', 'in_progress')
|
||||
AND NOT EXISTS (
|
||||
SELECT 1 FROM action_assignments aa
|
||||
WHERE aa.action_id = a.id AND aa.tenant_id = a.tenant_id
|
||||
)
|
||||
ORDER BY a.updated_at DESC
|
||||
LIMIT %s
|
||||
""",
|
||||
(ctx.tenant_id, initiative_id, remaining),
|
||||
)
|
||||
for row in cur.fetchall():
|
||||
item = dict(row)
|
||||
item["initiative_id"] = str(item["initiative_id"])
|
||||
item["action_id"] = str(item["action_id"])
|
||||
candidates.append(item)
|
||||
|
||||
remaining = limit - len(candidates)
|
||||
if remaining > 0:
|
||||
cur.execute(
|
||||
"""
|
||||
SELECT
|
||||
'convert_backlog' AS kind,
|
||||
bi.title AS title,
|
||||
'Freigegebenes Backlog-Item in Maßnahme umwandeln' AS summary,
|
||||
bi.initiative_id,
|
||||
NULL::uuid AS action_id,
|
||||
bi.id AS backlog_item_id,
|
||||
'backlog_accepted_not_converted' AS reason_code,
|
||||
'In Maßnahme umwandeln' AS recommended_action
|
||||
FROM backlog_items bi
|
||||
WHERE bi.tenant_id = %s AND bi.initiative_id = %s
|
||||
AND bi.status = 'accepted'
|
||||
AND bi.converted_action_id IS NULL
|
||||
ORDER BY bi.updated_at DESC
|
||||
LIMIT %s
|
||||
""",
|
||||
(ctx.tenant_id, initiative_id, remaining),
|
||||
)
|
||||
for row in cur.fetchall():
|
||||
item = dict(row)
|
||||
item["initiative_id"] = str(item["initiative_id"])
|
||||
item["backlog_item_id"] = str(item["backlog_item_id"])
|
||||
candidates.append(item)
|
||||
|
||||
remaining = limit - len(candidates)
|
||||
if remaining > 0:
|
||||
cur.execute(
|
||||
"""
|
||||
SELECT 1 FROM actions a
|
||||
WHERE a.initiative_id = %s AND a.tenant_id = %s
|
||||
AND a.status IN ('open', 'ready', 'in_progress', 'blocked', 'review_required')
|
||||
LIMIT 1
|
||||
""",
|
||||
(initiative_id, ctx.tenant_id),
|
||||
)
|
||||
if not cur.fetchone():
|
||||
cur.execute(
|
||||
"""
|
||||
SELECT title FROM initiatives
|
||||
WHERE id = %s AND tenant_id = %s
|
||||
""",
|
||||
(initiative_id, ctx.tenant_id),
|
||||
)
|
||||
init_row = cur.fetchone()
|
||||
if init_row:
|
||||
candidates.append(
|
||||
{
|
||||
"kind": "create_action",
|
||||
"title": init_row["title"],
|
||||
"summary": "Nächste Maßnahme für Vorhaben anlegen",
|
||||
"initiative_id": initiative_id,
|
||||
"action_id": None,
|
||||
"backlog_item_id": None,
|
||||
"reason_code": "initiative_no_open_action",
|
||||
"recommended_action": "Maßnahme anlegen",
|
||||
}
|
||||
)
|
||||
|
||||
return candidates[:limit]
|
||||
finally:
|
||||
conn.close()
|
||||
@@ -0,0 +1,26 @@
|
||||
"""Signal Engine — delegates to default rule provider (AP1.0)."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from typing import Any, Literal
|
||||
|
||||
from steering.signals import default_rules
|
||||
from tenant_context import TenantContext
|
||||
|
||||
SignalKind = Literal["attention", "next_action"]
|
||||
|
||||
|
||||
def evaluate(
|
||||
ctx: TenantContext,
|
||||
kind: SignalKind = "attention",
|
||||
*,
|
||||
limit: int = 10,
|
||||
initiative_id: str | None = None,
|
||||
) -> list[dict[str, Any]]:
|
||||
if kind == "attention":
|
||||
return default_rules.get_attention_items(ctx)
|
||||
if initiative_id:
|
||||
return default_rules.get_next_action_candidates_for_initiative(
|
||||
ctx, initiative_id=initiative_id, limit=limit
|
||||
)
|
||||
return default_rules.get_next_action_candidates(ctx, limit=limit)
|
||||
@@ -69,6 +69,7 @@ def _seed_hygiene():
|
||||
@pytest.fixture(scope="session", autouse=True)
|
||||
def _sync_rights_registry():
|
||||
import rights_registrations # noqa: F401
|
||||
import steering # noqa: F401 — bootstrap hooks & methods
|
||||
from rights_registry import sync_rights_registry_to_db
|
||||
|
||||
assert sync_rights_registry_to_db() == 0
|
||||
|
||||
@@ -0,0 +1,91 @@
|
||||
"""AP1.0 — Steering Foundation tests."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from tests.factories import provision_user_in_tenant
|
||||
from tests.test_initiatives_actions import (
|
||||
_auth,
|
||||
_create_action,
|
||||
_create_initiative,
|
||||
_login,
|
||||
)
|
||||
|
||||
|
||||
def test_steering_context_on_initiative_create(client):
|
||||
user = provision_user_in_tenant(tenant_role="member")
|
||||
token = _login(client, user)
|
||||
initiative_id = _create_initiative(client, token).json()["id"]
|
||||
|
||||
res = client.get(
|
||||
f"/api/initiatives/{initiative_id}/steering-context",
|
||||
headers=_auth(token),
|
||||
)
|
||||
assert res.status_code == 200
|
||||
data = res.json()
|
||||
assert data["initiative_id"] == initiative_id
|
||||
assert data["lifecycle_state"] == "intake"
|
||||
assert data["method_key"] == "generic_operating"
|
||||
assert data["lifecycle_label"]
|
||||
|
||||
|
||||
def test_steering_snapshot_has_lifecycle(client):
|
||||
user = provision_user_in_tenant(tenant_role="member")
|
||||
token = _login(client, user)
|
||||
initiative_id = _create_initiative(client, token).json()["id"]
|
||||
_create_action(client, token, initiative_id)
|
||||
|
||||
snap = client.get(
|
||||
f"/api/initiatives/{initiative_id}/steering-snapshot",
|
||||
headers=_auth(token),
|
||||
)
|
||||
assert snap.status_code == 200
|
||||
data = snap.json()
|
||||
assert "lifecycle_state" in data
|
||||
assert data.get("operating_phase_deprecated") is True
|
||||
assert data["lifecycle_state"] in (
|
||||
"intake",
|
||||
"planning",
|
||||
"action_selection",
|
||||
"structure_setup",
|
||||
)
|
||||
|
||||
|
||||
def test_steering_context_tenant_isolation(client):
|
||||
user_a = provision_user_in_tenant(tenant_role="member")
|
||||
token_a = _login(client, user_a)
|
||||
initiative_id = _create_initiative(client, token_a).json()["id"]
|
||||
|
||||
user_b = provision_user_in_tenant(tenant_role="member")
|
||||
token_b = _login(client, user_b)
|
||||
|
||||
res = client.get(
|
||||
f"/api/initiatives/{initiative_id}/steering-context",
|
||||
headers=_auth(token_b),
|
||||
)
|
||||
assert res.status_code == 404
|
||||
|
||||
|
||||
def test_lifecycle_transition_on_blocker_resolve(client):
|
||||
user = provision_user_in_tenant(tenant_role="member")
|
||||
token = _login(client, user)
|
||||
initiative_id = _create_initiative(client, token).json()["id"]
|
||||
action = _create_action(
|
||||
client, token, initiative_id, assigned_actor_ids=[user["actor_id"]]
|
||||
).json()
|
||||
blocker = client.post(
|
||||
f"/api/initiatives/{initiative_id}/blockers",
|
||||
json={"title": "B", "action_id": action["id"], "set_action_blocked": True},
|
||||
headers=_auth(token),
|
||||
).json()
|
||||
|
||||
client.patch(
|
||||
f"/api/blockers/{blocker['id']}",
|
||||
json={"status": "resolved"},
|
||||
headers=_auth(token),
|
||||
)
|
||||
|
||||
ctx = client.get(
|
||||
f"/api/initiatives/{initiative_id}/steering-context",
|
||||
headers=_auth(token),
|
||||
).json()
|
||||
assert ctx["lifecycle_state"] == "action_selection"
|
||||
+2
-2
@@ -1,3 +1,3 @@
|
||||
APP_VERSION = "0.10.1-ap0.10b"
|
||||
DB_SCHEMA_VERSION = "008"
|
||||
APP_VERSION = "0.11.0-ap1.0"
|
||||
DB_SCHEMA_VERSION = "009"
|
||||
APP_NAME = "jinkendo-kairo"
|
||||
Reference in new issue
Block a user