Codebase intelligence for AI and humans: code health scores, auto-generated docs, git analytics, dead code detection, and architectural decisions via MCP.
Python aicode-complexitycode-healthcode-intelligencecode-qualitydead-codedeveloper-toolsdocumentationgit-analyticsmcpopen-sourcepythonrefactoringstatic-analysistechnical-debt
refactor(server): decompose the decisions router's long handlers (#2602)
Raghav Chamadiyaef3163b84a43
diff --git a/packages/server/src/repowise/server/routers/decisions.py b/packages/server/src/repowise/server/routers/decisions.pyindex e4dcbf90b..2871639c9 100644--- a/packages/server/src/repowise/server/routers/decisions.py+++ b/packages/server/src/repowise/server/routers/decisions.py@@ -92,6 +92,51 @@ def _in_lane(currency: str | None, lane: str) -> bool:_LANE_SCAN_CAP = 500+def _page_of_lane(decisions: list, currencies: dict, lane: str, page: slice) -> list:+ """The records of *decisions* in *lane*, cut to *page*.++ ``candidates`` was already paged in SQL. Every other lane was over-fetched+ because its currency is derived, so its page is cut here.+ """+ in_lane = [d for d in decisions if _in_lane(currencies.get(d.id), lane)]+ if lane == "candidates":+ return in_lane+ return in_lane[page]+++async def _attach_evidence(session: AsyncSession, items: list[DecisionRecordResponse]) -> None:+ """Give each row its evidence count and its top-ranked quote as a preview."""+ if not items:+ return+ rows = (+ await session.execute(+ select(DecisionEvidence)+ .where(DecisionEvidence.decision_id.in_([item.id for item in items]))+ .order_by(+ DecisionEvidence.source_rank.desc(),+ DecisionEvidence.confidence.desc(),+ )+ )+ ).scalars()+ counts: dict[str, int] = {}+ best: dict[str, DecisionEvidence] = {}+ for ev in rows:+ counts[ev.decision_id] = counts.get(ev.decision_id, 0) + 1+ # Rows arrive best-first, so the first row per decision wins.+ best.setdefault(ev.decision_id, ev)+ for item in items:+ item.evidence_count = counts.get(item.id, 0)+ top = best.get(item.id)+ if top is not None and top.source_quote:+ item.evidence_preview = EvidencePreview(+ source=top.source,+ source_quote=top.source_quote,+ verification=top.verification,+ evidence_file=top.evidence_file,+ evidence_line=top.evidence_line,+ )++@router.get("/api/repos/{repo_id}/decisions",response_model=list[DecisionRecordResponse],@@ -133,17 +178,9 @@ async def list_decisions(decision under the unreviewed proposals the indexer had just mined, sopage one was entirely machine guesses."""- # The acceptance half of the lane is a SQL predicate, so a page of a lane- # is a page of that lane. The currency half cannot be: ``needs_review`` and- # ``uncheckable`` are derived from the record's scope and staleness. Those- # lanes therefore over-fetch the accepted set and cut the page afterwards,- # which is affordable because an accepted decision requires a human action- # and the set stays small by construction.- # Every lane but ``candidates`` filters the accepted set by currency, and a- # currency is derived from the record rather than stored, so the page has- # to be cut after the derivation. ``governing`` is in here for the same- # reason as the rest: cutting first returned an empty tab on a repository- # whose newest accepted records had all been superseded.+ # Acceptance is a SQL predicate, but every lane except ``candidates`` also+ # filters on a derived currency, so those over-fetch the accepted set (small,+ # since each acceptance is a human action) and are paged after derivation.derived = lane is not None and lane != "candidates"decisions = await crud.list_decisions(session,@@ -163,44 +200,13 @@ async def list_decisions()currencies = await crud.decision_currencies(session, repo_id, decisions)if lane is not None:- decisions = [d for d in decisions if _in_lane(currencies.get(d.id), lane)]- if derived:- decisions = decisions[offset : offset + limit]+ decisions = _page_of_lane(decisions, currencies, lane, slice(offset, offset + limit))signatures = await crud.decision_signatures(session, repo_id, decisions)items = [DecisionRecordResponse.from_orm(d) for d in decisions]for item in items:item.currency = currencies.get(item.id)_attach_signature(item, signatures.get(item.id))-- ids = [d.id for d in decisions]- if ids:- rows = (- await session.execute(- select(DecisionEvidence)- .where(DecisionEvidence.decision_id.in_(ids))- .order_by(- DecisionEvidence.source_rank.desc(),- DecisionEvidence.confidence.desc(),- )- )- ).scalars()- counts: dict[str, int] = {}- best: dict[str, DecisionEvidence] = {}- for ev in rows:- counts[ev.decision_id] = counts.get(ev.decision_id, 0) + 1- # Rows arrive best-first, so the first row per decision wins.- best.setdefault(ev.decision_id, ev)- for item in items:- item.evidence_count = counts.get(item.id, 0)- top = best.get(item.id)- if top is not None and top.source_quote:- item.evidence_preview = EvidencePreview(- source=top.source,- source_quote=top.source_quote,- verification=top.verification,- evidence_file=top.evidence_file,- evidence_line=top.evidence_line,- )+ await _attach_evidence(session, items)return items@@ -298,9 +304,7 @@ async def get_decision_graph(proposed statuses. Decision→decision typed edges and decision→code links arereturned without an additional cap (they scale with the node set)."""- # Fetch decisions ordered by staleness (most relevant first): active, then- # superseded/proposed, then deprecated. Use list_decisions without status- # filter so we get all statuses, capped.+ # No status filter: every status is a node, capped in priority order.all_decisions = await crud.list_decisions(session,repo_id,@@ -401,6 +405,50 @@ def _provider_available(repo_path: Path) -> bool:return provider_available_for_repo(repo_path)+def _value_error_as_400():+ """Report a policy the caller asked for but cannot have as a 400."""+ try:+ yield+ except ValueError as exc:+ raise HTTPException(status_code=400, detail=str(exc)) from exc+++def _apply_settings_update(policy, body: DecisionSettingsUpdate):+ """*policy* with the fields *body* sent applied, preset first."""+ from repowise.core.analysis.decisions.policy import preset_policy++ if body.preset is not None:+ with _value_error_as_400():+ # A preset names source membership, not a budget and not which+ # harnesses are read; what the caller did not send is theirs and+ # survives.+ policy = replace(+ preset_policy(body.preset),+ discovery=policy.discovery,+ harnesses=policy.harnesses,+ agent_acceptance=policy.agent_acceptance,+ capture_prompt=policy.capture_prompt,+ )+ if body.enabled is not None:+ policy = policy.with_enabled(body.enabled)+ if body.llm is not None:+ policy = policy.with_llm(body.llm)+ if body.agent_acceptance is not None:+ policy = policy.with_agent_acceptance(body.agent_acceptance)+ return _apply_source_and_discovery_patches(policy, body)+++def _apply_source_and_discovery_patches(policy, body: DecisionSettingsUpdate):+ with _value_error_as_400():+ for key, patch in (body.sources or {}).items():+ policy = policy.with_source(key, enabled=patch.enabled, llm=patch.llm)+ discovery = body.discovery.model_dump(exclude_none=True) if body.discovery else {}+ if discovery:+ policy = policy.with_discovery(**discovery)+ return policy++@router.get("/api/repos/{repo_id}/decisions/settings",response_model=DecisionSettings,@@ -428,44 +476,10 @@ async def update_decision_settings(Omitted fields keep their current value, so a UI can send one switch.``preset`` is applied before the per-source overrides."""- from repowise.core.analysis.decisions.policy import preset_policyfrom repowise.core.analysis.decisions.policy_store import PolicyConflictError, write_policyrepo_path = await _local_repo_path(session, repo_id)- policy = _load_policy_or_400(repo_path).policy-- if body.preset is not None:- try:- # A preset names source membership, not a budget and not which- # harnesses are read; what the caller did not send is theirs and- # survives.- policy = replace(- preset_policy(body.preset),- discovery=policy.discovery,- harnesses=policy.harnesses,- agent_acceptance=policy.agent_acceptance,- capture_prompt=policy.capture_prompt,- )- except ValueError as exc:- raise HTTPException(status_code=400, detail=str(exc)) from exc- if body.enabled is not None:- policy = policy.with_enabled(body.enabled)- if body.llm is not None:- policy = policy.with_llm(body.llm)- if body.agent_acceptance is not None:- policy = policy.with_agent_acceptance(body.agent_acceptance)- for key, patch in (body.sources or {}).items():- try:- policy = policy.with_source(key, enabled=patch.enabled, llm=patch.llm)- except ValueError as exc:- raise HTTPException(status_code=400, detail=str(exc)) from exc- patch = body.discovery.model_dump(exclude_none=True) if body.discovery else {}- if patch:- try:- policy = policy.with_discovery(**patch)- except ValueError as exc:- raise HTTPException(status_code=400, detail=str(exc)) from exc-+ policy = _apply_settings_update(_load_policy_or_400(repo_path).policy, body)try:resolution = write_policy(repo_path, policy, expected_etag=body.etag)except PolicyConflictError as exc:@@ -476,23 +490,24 @@ async def update_decision_settings(async def _live_decision_id(session: AsyncSession, decision_id: str) -> str:"""The id a caller-supplied decision id names today.- Only for an id that no longer names a record. Ids get retired underneath- the places they were written down when a decision moves onto a derived id,- and following the alias is what keeps those working instead of reading as- deleted.-- A live record always wins, so this cannot redirect one request to a- different decision. ``resolve_decision_id`` alone would: it follows a merge- even when the merged record still exists, which is right where the caller- is asking about the constraint, and wrong here, where the caller named a- row it is looking at in the candidates lane. An id with neither record nor- alias resolves to itself, so the handler still raises its own 404.+ A retired id follows its alias, so ids written down before a decision moved+ onto a derived id keep working. A live record always wins: unlike+ ``resolve_decision_id`` alone, this never redirects to a merge target. An+ id with neither record nor alias resolves to itself, so the caller 404s."""if await crud.get_decision(session, decision_id) is not None:return decision_idreturn await crud.resolve_decision_id(session, decision_id) or decision_id+async def _decision_in_repo(session: AsyncSession, repo_id: str, decision_id: str):+ """The record *decision_id* names today, or a 404 unless it is in *repo_id*."""+ rec = await crud.get_decision(session, await _live_decision_id(session, decision_id))+ if rec is None or rec.repository_id != repo_id:+ raise HTTPException(status_code=404, detail="Decision not found")+ return rec++@router.get("/api/repos/{repo_id}/decisions/{decision_id}",response_model=DecisionRecordResponse,@@ -503,10 +518,7 @@ async def get_decision(session: AsyncSession = Depends(get_db_session),) -> DecisionRecordResponse:"""Get a single decision record by ID."""- decision_id = await _live_decision_id(session, decision_id)- rec = await crud.get_decision(session, decision_id)- if rec is None or rec.repository_id != repo_id:- raise HTTPException(status_code=404, detail="Decision not found")+ rec = await _decision_in_repo(session, repo_id, decision_id)return await _one_with_signature(session, repo_id, rec)@@ -526,11 +538,8 @@ async def list_decision_evidence(badge (``exact`` | ``fuzzy`` | ``unverified``). 404 if the decision does notexist or belongs to a different repository."""- decision_id = await _live_decision_id(session, decision_id)- rec = await crud.get_decision(session, decision_id)- if rec is None or rec.repository_id != repo_id:- raise HTTPException(status_code=404, detail="Decision not found")- rows = await crud.list_decision_evidence(session, decision_id)+ rec = await _decision_in_repo(session, repo_id, decision_id)+ rows = await crud.list_decision_evidence(session, rec.id)return {"evidence": [DecisionEvidenceResponse.from_orm(r) for r in rows]}@@ -549,14 +558,22 @@ async def get_decision_lineage(UI can render a timeline. An isolated decision returns a single-entry chain.404 if the decision does not exist or belongs to a different repository."""- decision_id = await _live_decision_id(session, decision_id)- rec = await crud.get_decision(session, decision_id)- if rec is None or rec.repository_id != repo_id:- raise HTTPException(status_code=404, detail="Decision not found")- chain = await decision_graph.build_lineage_chain(session, decision_id)+ rec = await _decision_in_repo(session, repo_id, decision_id)+ chain = await decision_graph.build_lineage_chain(session, rec.id)return {"lineage": [DecisionLineageEntry(**entry) for entry in chain]}+async def _is_accepted_with_scope(session: AsyncSession, existing) -> bool:+ """Whether *existing* is an accepted record that governs a scope.++ Asks what the stored record would lose, not what either side calls it: an+ agreement can be given a real scope.+ """+ if existing is None or not crud.names_a_scope(existing):+ return False+ return await crud.is_accepted(session, existing.id)++@router.post("/api/repos/{repo_id}/decisions",response_model=DecisionRecordResponse,@@ -582,12 +599,9 @@ async def create_decision(unaccepted. The response's ``status`` says which of the two happened, and aform can predict it from the same one field."""- # ``upsert_decision`` dedups on the title and overwrites the scope with- # whatever the body carries, so a second post of an accepted decision's- # title with no files would clear the scope it governs and leave its- # acceptance row pointing at a record that no longer binds. Refuse, and- # name the record, rather than quietly retiring somebody's decision from a- # call that says "create".+ # ``upsert_decision`` dedups on the title and overwrites the scope, so a+ # scope-less re-post of an accepted decision would silently withdraw what+ # it governs. Refuse and name the record instead.existing = await crud.find_decision_by_title(session, repo_id, body.title, source="cli")@@ -598,14 +612,7 @@ async def create_decision(# An agreement names no file because its scope is the repository.# Requiring one would leave the noun permanently unacceptable.scoped = named or kind == AGREEMENT_KIND- # What the stored record would lose, not what either side calls it: an- # agreement can be given a real scope.- if (- existing is not None- and not named- and crud.names_a_scope(existing)- and await crud.is_accepted(session, existing.id)- ):+ if not named and await _is_accepted_with_scope(session, existing):raise HTTPException(status_code=409,detail=(@@ -636,16 +643,36 @@ async def create_decision(# No confidence: upsert_decision scores a manual entry.)if scoped:- # Same rule as the scope-less case above: what the contract will not- # accept is kept as a candidate, not refused. An entry stating no- # reason reaches here, and discarding everything the author typed over- # a missing rationale would be the worse answer. The response's- # ``status`` reports which of the two happened.+ # A refused acceptance (e.g. no rationale) keeps the record as a candidate.with contextlib.suppress(crud.AcceptanceRefusedError):await crud.accept_decision(session, rec, accepter="web", kind="person")return await _one_with_signature(session, repo_id, rec)+async def _transition_status(+ session: AsyncSession, decision_id: str, status: str, superseded_by: str | None+):+ """Move a decision to *status* as a person on the web, or 400/404."""+ # The successor is a caller-supplied id too, and storing a retired one+ # would record a pointer that no longer resolves.+ if superseded_by is not None:+ superseded_by = await _live_decision_id(session, superseded_by)+ try:+ rec = await crud.update_decision_status(+ session,+ decision_id,+ status,+ superseded_by=superseded_by,+ accepter="web",+ kind="person",+ )+ except ValueError as exc:+ raise HTTPException(status_code=400, detail=str(exc)) from exc+ if rec is None:+ raise HTTPException(status_code=404, detail="Decision not found")+ return rec++@router.patch("/api/repos/{repo_id}/decisions/{decision_id}",response_model=DecisionRecordResponse,@@ -664,30 +691,11 @@ async def patch_decision(``affected_files`` without ``affected_modules`` re-derives the modulesfrom those files so the two halves of the scope cannot disagree."""- decision_id = await _live_decision_id(session, decision_id)- rec = await crud.get_decision(session, decision_id)- if rec is None or rec.repository_id != repo_id:- raise HTTPException(status_code=404, detail="Decision not found")+ rec = await _decision_in_repo(session, repo_id, decision_id)+ decision_id = rec.idif body.status is not None:- # The successor is a caller-supplied id too, and storing a retired one- # would record a pointer that no longer resolves.- superseded_by = body.superseded_by- if superseded_by is not None:- superseded_by = await _live_decision_id(session, superseded_by)- try:- rec = await crud.update_decision_status(- session,- decision_id,- body.status,- superseded_by=superseded_by,- accepter="web",- kind="person",- )- except ValueError as exc:- raise HTTPException(status_code=400, detail=str(exc)) from exc- if rec is None:- raise HTTPException(status_code=404, detail="Decision not found")+ rec = await _transition_status(session, decision_id, body.status, body.superseded_by)elif body.superseded_by is not None:raise HTTPException(status_code=400,