I need to understand how clusters are built and collapsed in the work queue, and how they render, so I can apply the same pattern to collapse initial subjective review items into a single cluster.
Key areas to explore:
desloppify/engine/_work_queue/plan_order.py — especially collapse_clusters() and _build_cluster_item() to understand the cluster item structure
desloppify/app/commands/next/render_support.py — render_cluster_item() to understand how clusters render in terminal
desloppify/app/commands/next/output.py — how clusters serialize for JSON output
desloppify/engine/_work_queue/core.py — the full build_work_queue pipeline, especially _apply_lifecycle_filter and _gather_subjective_items and the _has_initial_reviews / _has_objective_items helpers
desloppify/engine/_work_queue/ranking.py — _natural_sort_key to see how clusters and initial reviews rank
For each, give me the full function implementations so I can understand the exact data shapes and flow.
Assistant
I'll explore these areas to understand the cluster building, collapsing, and rendering patterns. Let me gather the key files in parallel.
Assistant
Perfect! I now have all the key files. Let me provide you with a comprehensive analysis of the cluster building, collapsing, and rendering patterns.
Cluster System Architecture
1. Cluster Meta-Item Building (plan_order.py, lines 144-189)
The _build_cluster_meta() function creates a single synthetic "cluster" item from multiple member issues:
python
def _build_cluster_meta(
cluster_name: str, members: list[WorkQueueItem], cluster_data: dict[str, Any]
) -> WorkQueueItem:
"""Build a cluster meta-item from its member items."""
detector = members[0].get("detector", "") if members else ""
action = cluster_data.get("action") or ""
if "desloppify autofix" in action:
action_type = "auto_fix"
elif "desloppify move" in action:
action_type = "reorganize"
else:
action_type = action_type_for_detector(detector)
if action_type == "auto_fix" and "desloppify autofix" not in action:
action_type = "refactor"
stored_desc = cluster_data.get("description") or ""
total_in_cluster = len(cluster_data.get("issue_ids", []))
if stored_desc and total_in_cluster != len(members):
summary = stored_desc.replace(str(total_in_cluster), str(len(members)))
else:
summary = stored_desc or f"{len(members)} issues"
primary_command = cluster_data.get("action")
if not primary_command:
primary_command = f"desloppify next --cluster {cluster_name} --count 10"
estimated_impact = max(
(m.get("estimated_impact", 0.0) for m in members), default=0.0
)
return {
"id": cluster_name,
"kind": "cluster",
"action_type": action_type,
"summary": summary,
"members": members, # Full member items nested
"member_count": len(members),
"primary_command": primary_command,
"cluster_name": cluster_name,
"cluster_auto": True,
"cluster_optional": bool(cluster_data.get("optional")),
"confidence": "high",
"detector": detector,
"file": "",
"estimated_impact": estimated_impact,
}
Key data shape:
kind: "cluster" marks it as synthetic
members: list of full WorkQueueItem objects (stored whole, not just IDs)
def collapse_clusters(items: list[WorkQueueItem], plan: dict) -> list[WorkQueueItem]:
"""Replace cluster member items with single cluster meta-items.
Walks the list in order: the first member of each collapsed cluster is
replaced with its meta-item, subsequent members are skipped. This
preserves the ordering established by sort + plan-order.
"""
clusters = plan.get("clusters", {})
if not clusters:
return items
fid_to_cluster: dict[str, str] = {}
for name, cluster in clusters.items():
if not cluster.get("auto"):
continue
for issue_id in cluster.get("issue_ids", []):
fid_to_cluster[issue_id] = name
if not fid_to_cluster:
return items
# Collect members per cluster (preserving encounter order)
cluster_members: dict[str, list[WorkQueueItem]] = {}
for item in items:
cname = fid_to_cluster.get(item.get("id", ""))
if cname:
cluster_members.setdefault(cname, []).append(item)
# Build meta-items only for clusters with 2+ members in the queue
meta_items: dict[str, WorkQueueItem] = {}
for cname, members in cluster_members.items():
if len(members) < 2:
continue
meta_items[cname] = _build_cluster_meta(
cname, members, clusters.get(cname, {})
)
# Walk in order: replace first member of each collapsed cluster
# with its meta-item, skip subsequent members
seen_clusters: set[str] = set()
result: list[WorkQueueItem] = []
for item in items:
cname = fid_to_cluster.get(item.get("id", ""))
if cname and cname in meta_items:
if cname not in seen_clusters:
seen_clusters.add(cname)
result.append(meta_items[cname])
# skip individual member
else:
result.append(item)
return result
Key algorithm points:
Reads from plan["clusters"][cluster_name]["auto"] — only auto-clusters (not manual) are collapsed
So clusters rank between triage stages and individual issues. Within clusters, ordering is by:
Action type priority
Member count (more members sort first)
Cluster ID (alphabetic tie-breaker)
6. Lifecycle Integration (core.py, lines 273-319)
Initial reviews are handled specially in lifecycle:
python
def _has_initial_reviews(items: list[WorkQueueItem]) -> bool:
"""True if any unassessed subjective dimensions need initial review."""
return any(
i.get("kind") == "subjective_dimension"
and i.get("initial_review")
for i in items
)
def _apply_lifecycle_filter(items: list[WorkQueueItem]) -> list[WorkQueueItem]:
"""Enforce lifecycle visibility rules."""
if _has_initial_reviews(items):
# Phase 1: only initial reviews visible
return [
i for i in items
if i.get("kind") == "subjective_dimension" and i.get("initial_review")
]
# ... other phases ...
Subjective items (synthetic.py via build_subjective_items()) are individual kind: "subjective_dimension" items before clustering. Each has:
initial_review: bool — whether this is an unassessed dimension
is_subjective: bool
detail.dimension_name: the dimension being reviewed
To Apply This Pattern for Initial Subjective Reviews
You'd need to:
Create a synthetic cluster item at the point where subjective dimensions are built (in build_subjective_items() or post-ranking)
Mark it properly:
kind: "cluster"
cluster_name: "auto/initial-review"
action_type: "refactor" or appropriate type
members: list of all initial-review subjective dimension items
member_count: count of members
Update lifecycle filtering to recognize this cluster (or treat initial-review as a special case that still shows when initial reviews are pending)
Modify collapse_clusters() to handle both auto-clusters from plan AND synthetic "auto/initial-review" clusters
Use existing render/serialize infrastructure — no changes needed there
The key distinction: plan-based clusters come from plan["clusters"][name] with auto: true, but an initial-review cluster could be synthetic (always auto-generated if 2+ initial reviews exist).