Skip to content

The agent that grows with you

refactor(kanban-db): lift decompose children validation into _validate_children_graph

Teknium1c281abfd5c1
diff --git a/hermes_cli/kanban_db.py b/hermes_cli/kanban_db.py
index edcd881d0b1..2f6b0575cdc 100644
--- a/hermes_cli/kanban_db.py
+++ b/hermes_cli/kanban_db.py
@@ -4654,36 +4654,14 @@ def specify_triage_task(
return True
-def decompose_triage_task(
- conn: sqlite3.Connection,
- task_id: str,
- *,
- root_assignee: Optional[str],
- children: list[dict],
- author: Optional[str] = None,
- auto_promote: bool = True,
-) -> Optional[list[str]]:
- """Fan a triage task out into child tasks and promote the root to ``todo``.
-
- The root task stays alive and becomes the parent of every child —
- when all children reach ``done``, the root promotes to ``ready`` and
- its assignee (typically the orchestrator profile) wakes back up to
- judge completion or spawn more work.
+def _validate_children_graph(children: list) -> None:
+ """Shape-check ``decompose_triage_task`` children and reject cycles.
- ``children``: dicts of ``title`` (required), ``body``, ``assignee`` (None
- -> default fallback) and ``parents`` (indices into this same list).
- Returns the created child ids in input order, or ``None`` when the root
- is missing / not in ``triage`` / the graph has a cycle. Title/assignee
- validation runs inside the same write_txn as the inserts so a malformed
- entry aborts the whole decomposition (no orphan children).
+ Cheap, DB-free, so bad input aborts before the txn. The sibling parent
+ graph is checked whole (Kahn's sort) rather than edge-by-edge like
+ ``link_tasks``/``_would_cycle``: a cycle silently deadlocks every involved
+ child in ``todo`` because ``recompute_ready`` can never promote them.
"""
- if not children:
- return None
- if root_assignee is not None:
- root_assignee = _canonical_assignee(root_assignee)
-
- # Pre-validate the children list shape outside the txn. Cheap checks
- # that don't need DB access. Bad input aborts before we touch the DB.
for idx, child in enumerate(children):
if not isinstance(child, dict):
raise ValueError(f"child[{idx}] is not a dict")
@@ -4701,11 +4679,6 @@ def decompose_triage_task(
if p == idx:
raise ValueError(f"child[{idx}] cannot list itself as a parent")
- # Detect cycles in the sibling parent graph (Kahn's topological sort).
- # link_tasks() calls _would_cycle() for every new edge; here we check
- # the entire sibling graph before touching the DB. A cycle silently
- # deadlocks every involved child in 'todo' because recompute_ready()
- # can never promote them.
_in_deg = [0] * len(children)
_adj: list[list[int]] = [[] for _ in range(len(children))]
for _i, _c in enumerate(children):
@@ -4724,6 +4697,37 @@ def decompose_triage_task(
if _seen != len(children):
raise ValueError("cyclic dependency detected in decomposed children list")
+
+def decompose_triage_task(
+ conn: sqlite3.Connection,
+ task_id: str,
+ *,
+ root_assignee: Optional[str],
+ children: list[dict],
+ author: Optional[str] = None,
+ auto_promote: bool = True,
+) -> Optional[list[str]]:
+ """Fan a triage task out into child tasks and promote the root to ``todo``.
+
+ The root task stays alive and becomes the parent of every child —
+ when all children reach ``done``, the root promotes to ``ready`` and
+ its assignee (typically the orchestrator profile) wakes back up to
+ judge completion or spawn more work.
+
+ ``children``: dicts of ``title`` (required), ``body``, ``assignee`` (None
+ -> default fallback) and ``parents`` (indices into this same list).
+ Returns the created child ids in input order, or ``None`` when the root
+ is missing / not in ``triage`` / the graph has a cycle. Title/assignee
+ validation runs inside the same write_txn as the inserts so a malformed
+ entry aborts the whole decomposition (no orphan children).
+ """
+ if not children:
+ return None
+ if root_assignee is not None:
+ root_assignee = _canonical_assignee(root_assignee)
+
+ _validate_children_graph(children)
+
# We do the full decomposition in a SINGLE write_txn so it's
# atomic: either every child is created AND the root flips to
# ``todo``, or nothing changes. We deliberately do NOT call any