feat(position-roundtable): add delivery rejection workflow
This commit is contained in:
1 parent
2ca8a23102
commit
1cd99a5a90
9 files changed
+1422
-148
No files matched your search
+105
@@ -37,6 +37,10 @@ def _time(value: Any) -> str | Any:
|
||||
return value.isoformat() if isinstance(value, datetime) else value
|
||||
|
||||
|
||||
def _has_material(row: PositionRoundtableNodeRow) -> bool:
|
||||
return row.revision > 0 or bool(row.latest_answer) or bool(_loads(row.artifact_manifest))
|
||||
|
||||
|
||||
class PositionRoundtableRepository:
|
||||
def __init__(self, session_factory: async_sessionmaker[AsyncSession]) -> None:
|
||||
self._sf = session_factory
|
||||
@@ -51,6 +55,8 @@ class PositionRoundtableRepository:
|
||||
"chain_snapshot": _loads(row.chain_snapshot),
|
||||
"intent_thread_id": row.intent_thread_id,
|
||||
"intent_snapshot": _loads(row.intent_snapshot),
|
||||
"summary_snapshot": _loads(row.summary_snapshot),
|
||||
"action_plan_snapshot": _loads(row.action_plan_snapshot),
|
||||
"status": row.status,
|
||||
"created_at": _time(row.created_at),
|
||||
"updated_at": _time(row.updated_at),
|
||||
@@ -72,6 +78,9 @@ class PositionRoundtableRepository:
|
||||
"artifact_manifest": _loads(row.artifact_manifest) or [],
|
||||
"revision": row.revision,
|
||||
"upstream_revision": _loads(row.upstream_revision),
|
||||
"rejection_count": row.rejection_count or 0,
|
||||
"last_rejection": _loads(row.last_rejection),
|
||||
"invalidated_by": row.invalidated_by,
|
||||
"created_at": _time(row.created_at),
|
||||
"updated_at": _time(row.updated_at),
|
||||
}
|
||||
@@ -125,6 +134,8 @@ class PositionRoundtableRepository:
|
||||
task_snapshot: dict[str, Any] | object = _UNSET,
|
||||
intent_thread_id: str | None | object = _UNSET,
|
||||
intent_snapshot: dict[str, Any] | None | object = _UNSET,
|
||||
summary_snapshot: dict[str, Any] | None | object = _UNSET,
|
||||
action_plan_snapshot: dict[str, Any] | None | object = _UNSET,
|
||||
status: str | object = _UNSET,
|
||||
) -> dict[str, Any] | None:
|
||||
async with self._sf() as session:
|
||||
@@ -137,6 +148,10 @@ class PositionRoundtableRepository:
|
||||
row.intent_thread_id = intent_thread_id or None
|
||||
if intent_snapshot is not _UNSET:
|
||||
row.intent_snapshot = _dumps(intent_snapshot)
|
||||
if summary_snapshot is not _UNSET:
|
||||
row.summary_snapshot = _dumps(summary_snapshot)
|
||||
if action_plan_snapshot is not _UNSET:
|
||||
row.action_plan_snapshot = _dumps(action_plan_snapshot)
|
||||
if status is not _UNSET:
|
||||
row.status = str(status)
|
||||
row.updated_at = datetime.now(UTC)
|
||||
@@ -239,7 +254,10 @@ class PositionRoundtableRepository:
|
||||
latest_answer: str | None | object = _UNSET,
|
||||
artifact_manifest: list[dict[str, Any]] | object = _UNSET,
|
||||
upstream_revision: dict[str, Any] | object = _UNSET,
|
||||
last_rejection: dict[str, Any] | None | object = _UNSET,
|
||||
invalidated_by: str | None | object = _UNSET,
|
||||
increment_revision: bool = False,
|
||||
increment_rejection: bool = False,
|
||||
) -> dict[str, Any] | None:
|
||||
async with self._sf() as session:
|
||||
stmt = (
|
||||
@@ -265,13 +283,100 @@ class PositionRoundtableRepository:
|
||||
row.artifact_manifest = _dumps(artifact_manifest)
|
||||
if upstream_revision is not _UNSET:
|
||||
row.upstream_revision = _dumps(upstream_revision)
|
||||
if last_rejection is not _UNSET:
|
||||
row.last_rejection = _dumps(last_rejection)
|
||||
if invalidated_by is not _UNSET:
|
||||
row.invalidated_by = invalidated_by or None
|
||||
if increment_revision:
|
||||
row.revision += 1
|
||||
if increment_rejection:
|
||||
row.rejection_count += 1
|
||||
row.updated_at = datetime.now(UTC)
|
||||
await session.commit()
|
||||
await session.refresh(row)
|
||||
return self._node_dict(row)
|
||||
|
||||
async def reject_node(
|
||||
self,
|
||||
session_id: str,
|
||||
node_key: str,
|
||||
user_id: str,
|
||||
*,
|
||||
title: str,
|
||||
reason: str,
|
||||
rejected_by: str,
|
||||
) -> tuple[dict[str, Any], list[dict[str, Any]]] | None:
|
||||
"""Reject one delivered node and invalidate every later stage.
|
||||
|
||||
Historical answers and artifact manifests are deliberately preserved.
|
||||
``invalidated_by`` gates downstream rework until the rejected upstream
|
||||
stage is completed again.
|
||||
"""
|
||||
now = datetime.now(UTC)
|
||||
async with self._sf() as session:
|
||||
parent = await session.get(PositionRoundtableSessionRow, session_id)
|
||||
if parent is None or parent.user_id != user_id:
|
||||
return None
|
||||
rows = (
|
||||
await session.execute(
|
||||
select(PositionRoundtableNodeRow)
|
||||
.where(PositionRoundtableNodeRow.session_id == session_id)
|
||||
.order_by(PositionRoundtableNodeRow.stage_index, PositionRoundtableNodeRow.seat_index)
|
||||
)
|
||||
).scalars().all()
|
||||
target = next((row for row in rows if row.node_key == node_key), None)
|
||||
if target is None:
|
||||
return None
|
||||
|
||||
rejection = {
|
||||
"title": title,
|
||||
"reason": reason,
|
||||
"rejected_by": rejected_by,
|
||||
"rejected_at": now.isoformat(),
|
||||
}
|
||||
summary_snapshot = _loads(parent.summary_snapshot)
|
||||
if isinstance(summary_snapshot, dict) and summary_snapshot.get("status") == "done":
|
||||
summary_snapshot = {
|
||||
**summary_snapshot,
|
||||
"status": "stale",
|
||||
"invalidated_by": target.node_key,
|
||||
"invalidated_at": now.isoformat(),
|
||||
}
|
||||
parent.summary_snapshot = _dumps(summary_snapshot)
|
||||
action_plan_snapshot = _loads(parent.action_plan_snapshot)
|
||||
if isinstance(action_plan_snapshot, dict) and action_plan_snapshot.get("status") == "done":
|
||||
action_plan_snapshot = {
|
||||
**action_plan_snapshot,
|
||||
"status": "stale",
|
||||
"invalidated_by": target.node_key,
|
||||
"invalidated_at": now.isoformat(),
|
||||
}
|
||||
parent.action_plan_snapshot = _dumps(action_plan_snapshot)
|
||||
if parent.status == "completed":
|
||||
parent.status = "active"
|
||||
target.status = "rejected"
|
||||
target.last_rejection = _dumps(rejection)
|
||||
target.rejection_count += 1
|
||||
target.invalidated_by = None
|
||||
target.updated_at = now
|
||||
|
||||
for row in rows:
|
||||
if row.stage_index <= target.stage_index:
|
||||
continue
|
||||
if _has_material(row) or row.status in {"running", "done", "stale", "rejected", "error"}:
|
||||
row.status = "stale"
|
||||
else:
|
||||
row.status = "locked"
|
||||
row.invalidated_by = target.node_key
|
||||
row.updated_at = now
|
||||
|
||||
parent.updated_at = now
|
||||
await session.commit()
|
||||
await session.refresh(parent)
|
||||
for row in rows:
|
||||
await session.refresh(row)
|
||||
return self._session_dict(parent), [self._node_dict(row) for row in rows]
|
||||
|
||||
async def archive_session(self, session_id: str, user_id: str) -> dict[str, Any] | None:
|
||||
return await self.update_session(session_id, user_id, status="archived")
|
||||
|
||||
|
||||
Reference in new issue
Block a user