Skip to content

Codebase intelligence for AI and humans: code health scores, auto-generated docs, git analytics, dead code detection, and architectural decisions via MCP.

perf(server): workspace API reads member stats from workspace.db (#4651)

Raghav Chamadiya8239ecebd565
diff --git a/docs/architecture/workspace.md b/docs/architecture/workspace.md
index 0ff7a347b..bb8ebfe33 100644
--- a/docs/architecture/workspace.md
+++ b/docs/architecture/workspace.md
@@ -239,7 +239,7 @@ Everything lives in `.repowise-workspace/`:
| `conformance.json` | Violations, cycles, `evidence` | |
| `analysis_config.json` | The `contracts:` and `conformance:` settings the last analysis ran with | `WorkspaceConfig.analysis_fingerprint()` |
| `member_refresh.json` | One digest per member repo | `update_cmd/member_refresh.py::member_fingerprint` |
-| `workspace.db` | Path and symbol lookup, components, recent changes, health, freshness | Each repo's `state.json`; safe to delete |
+| `workspace.db` | Path and symbol lookup, components, recent changes, health, freshness, the per-repo figures `GET /api/workspace` and `/graph` serve | Each repo's `state.json`; the figures also by `wiki.db` stat, read live when it moved; safe to delete |
`member_refresh.py` lives in `packages/cli/src/repowise/cli/commands/update_cmd/`.
Each repo's own `wiki.db` also carries a schema stamp in `PRAGMA user_version`
diff --git a/packages/core/src/repowise/core/workspace/repo_stats.py b/packages/core/src/repowise/core/workspace/repo_stats.py
new file mode 100644
index 000000000..4c2f55b65
--- /dev/null
+++ b/packages/core/src/repowise/core/workspace/repo_stats.py
@@ -0,0 +1,193 @@
+"""One member's headline figures, read straight from its ``wiki.db``.
+
+The workspace store keeps these per repo (see :mod:`.store`), so the
+workspace listing and graph read one row per repo instead of opening every
+member index per request. Raw ``sqlite3`` without a session: one connection,
+a few aggregates.
+"""
+
+from __future__ import annotations
+
+import json
+import logging
+import sqlite3
+from contextlib import closing
+from pathlib import Path
+from typing import Any
+
+from repowise.core.analysis.external_systems.links import IMPORT_TARGET_LANGUAGES
+from repowise.core.docs_mode import resolve_docs_mode
+
+_log = logging.getLogger("repowise.workspace.repo_stats")
+
+# The raw-sqlite form of ``crud.indexed_file``.
+_INDEXED_FILE_SQL = "node_type = 'file' AND language NOT IN ({})".format(
+ ", ".join(f"'{lang}'" for lang in IMPORT_TARGET_LANGUAGES)
+)
+
+
+def query_top_language(db_path: Path) -> str:
+ """Return the most common language across graph_nodes in a repo's wiki.db."""
+ if not db_path.exists():
+ return "unknown"
+ try:
+ with closing(sqlite3.connect(str(db_path))) as conn:
+ row = conn.execute(
+ "SELECT language, COUNT(*) AS cnt FROM graph_nodes "
+ f"WHERE {_INDEXED_FILE_SQL} AND language IS NOT NULL AND language != '' "
+ "GROUP BY language ORDER BY cnt DESC LIMIT 1"
+ ).fetchone()
+ return row[0] if row else "unknown"
+ except Exception:
+ return "unknown"
+
+
+def query_repo_stats(db_path: Path) -> dict[str, Any]:
+ """Query basic stats from a repo's wiki.db using raw sqlite3.
+
+ Returns a dict with repo_id, file_count, symbol_count, page_count,
+ fresh_page_count, doc_coverage_pct, hotspot_count, health_score, status,
+ docs_enabled, docs_skip_reason, and (when the row is read) indexed_at.
+ All values default to sensible neutrals on error.
+
+ ``health_score`` is None when the repo carries no health metrics, and
+ stays None: no other figure here measures code health.
+ """
+ result: dict[str, Any] = {
+ "repo_id": None,
+ "file_count": 0,
+ "symbol_count": 0,
+ "page_count": 0,
+ "fresh_page_count": 0,
+ "doc_coverage_pct": 0.0,
+ "hotspot_count": 0,
+ "health_score": None,
+ "status": "needs_index",
+ "docs_enabled": False,
+ "docs_skip_reason": None,
+ }
+ _read_docs_state(db_path, result)
+ if not db_path.exists():
+ if not db_path.parent.parent.is_dir():
+ result["status"] = "missing_dir"
+ return result
+ result["status"] = "indexed"
+ try:
+ with closing(sqlite3.connect(str(db_path))) as conn:
+ _read_counts(conn.cursor(), result)
+ except Exception:
+ _log.debug("Failed to query stats from %s", db_path, exc_info=True)
+
+ result["health_score"] = read_repo_health_score(db_path)
+ return result
+
+
+def _read_docs_state(db_path: Path, result: dict[str, Any]) -> None:
+ """Surface docs lifecycle from state.json so the UI shows a coherent
+ picture even when only indexing (no docs) ran."""
+ state_path = db_path.parent / "state.json"
+ if not state_path.is_file():
+ return
+ try:
+ state = json.loads(state_path.read_text(encoding="utf-8"))
+ result["docs_enabled"] = resolve_docs_mode(state) != "none"
+ result["docs_skip_reason"] = state.get("docs_skip_reason")
+ except Exception:
+ _log.debug("Unreadable %s", state_path, exc_info=True)
+
+
+def _read_counts(c: sqlite3.Cursor, result: dict[str, Any]) -> None:
+ from .update import synced_at_iso
+
+ # repo id, and the sync time every other surface reports
+ row = c.execute("SELECT id, updated_at FROM repositories LIMIT 1").fetchone()
+ if row:
+ result["repo_id"] = row[0]
+ result["indexed_at"] = synced_at_iso(row[1])
+
+ # The raw-sqlite form of crud.count_files.
+ row = c.execute(f"SELECT COUNT(*) FROM graph_nodes WHERE {_INDEXED_FILE_SQL}").fetchone()
+ result["file_count"] = row[0] if row else 0
+
+ # The raw-sqlite form of crud.count_symbols: one store per repo here.
+ row = c.execute("SELECT COUNT(*) FROM wiki_symbols").fetchone()
+ result["symbol_count"] = int(row[0]) if row else 0
+
+ # Pages, fresh pages, and mean page confidence * 100. The last is named
+ # doc_coverage_pct on the wire; it is not a share of files documented.
+ pages, fresh, confidence = c.execute(
+ "SELECT COUNT(*), SUM(freshness_status = 'fresh'), AVG(confidence) FROM wiki_pages"
+ ).fetchone()
+ result["page_count"] = pages
+ result["fresh_page_count"] = int(fresh or 0)
+ result["doc_coverage_pct"] = round(float(confidence or 0.0) * 100, 1)
+
+ # hotspot count — use the canonical is_hotspot flag, matching the rest
+ # of the codebase (module_health, extract-demo-data). The earlier
+ # churn_percentile >= 90 predicate never matched: churn_percentile is
+ # stored on a 0.0-1.0 scale and only scaled to 0-100 at the API layer.
+ try:
+ row = c.execute("SELECT COUNT(*) FROM git_metadata WHERE is_hotspot = 1").fetchone()
+ result["hotspot_count"] = row[0] if row else 0
+ except sqlite3.OperationalError:
+ _log.debug("No hotspot flags in this index") # table or column may not exist
+
+
+def read_repo_health_score(db_path: Path) -> float | None:
+ """Return the canonical repo health score from a repo-local wiki.db."""
+ if not db_path.exists():
+ return None
+ # Aggregate in SQL rather than reading every row. A workspace calls this once
+ # per repo, and the fetchall() this replaces read ~6,000 rows per repo to
+ # produce one number: 61.6ms against 33.2ms across six large repos, and the
+ # row count grows with the repo. MAX(CAST(..), 1) reproduces the previous
+ # `max(int(nloc or 0), 1)` weight, truncation included; verified identical on
+ # 109 real databases.
+ try:
+ with closing(sqlite3.connect(str(db_path))) as conn:
+ row = conn.execute(
+ "SELECT SUM(score * w), SUM(w) FROM ("
+ " SELECT score, MAX(CAST(COALESCE(nloc, 0) AS INTEGER), 1) AS w"
+ " FROM health_file_metrics WHERE score IS NOT NULL"
+ ")"
+ ).fetchone()
+ except Exception:
+ return None
+
+ if not row or not row[1]:
+ return None
+
+ weighted_sum, total_weight = row
+ average = float(weighted_sum) / float(total_weight)
+ return max(0.0, min(100.0, round(average * 10.0, 1)))
+
+
+def member_stats(db_path: Path) -> dict[str, Any]:
+ """:func:`query_repo_stats` plus ``top_language``: what the store keeps per repo."""
+ return {**query_repo_stats(db_path), "top_language": query_top_language(db_path)}
+
+
+def db_stamp(db_path: Path) -> str:
+ """What moves when anything commits to *db_path*: the main file, plus the
+ WAL while it holds frames. An empty WAL (a reader opened the file) is
+ ignored, so reads never move it."""
+ parts = []
+ for suffix in ("", "-wal"):
+ try:
+ st = Path(f"{db_path}{suffix}").stat()
+ except OSError:
+ parts.append("-")
+ continue
+ parts.append(f"{st.st_mtime_ns}:{st.st_size}" if suffix == "" or st.st_size else "-")
+ return "|".join(parts)
+
+
+def stamped_member_stats(db_path: Path) -> tuple[str, dict[str, Any]] | None:
+ """``(db stamp, member stats)``, or None when a write landed mid-read twice
+ running (the repo is then read live until its next refresh)."""
+ for _ in range(2):
+ before = db_stamp(db_path)
+ stats = member_stats(db_path)
+ if db_stamp(db_path) == before:
+ return before, stats
+ return None
diff --git a/packages/core/src/repowise/core/workspace/store.py b/packages/core/src/repowise/core/workspace/store.py
index 157096e35..fe7cad6c9 100644
--- a/packages/core/src/repowise/core/workspace/store.py
+++ b/packages/core/src/repowise/core/workspace/store.py
@@ -2,7 +2,8 @@
A derived SQLite file holding the cross-repo rows a workspace query needs in
one lookup: which repo has a path or a symbol, each repo's components, recent
-change summaries, health and index freshness. Each repo's own ``wiki.db`` stays
+change summaries, health, index freshness and the headline figures the
+workspace listing shows (:mod:`.repo_stats`). Each repo's own ``wiki.db`` stays
the source of truth; every row here is rebuilt from it, so the file can be
deleted at any time. A missing, corrupt or older-schema file is rebuilt.
@@ -34,7 +35,7 @@ _log = logging.getLogger("repowise.workspace.store")
STORE_FILENAME = "workspace.db"
#: Bump on any table change; a file with another version is rebuilt.
-SCHEMA_VERSION = 1
+SCHEMA_VERSION = 2
_BUSY_TIMEOUT_MS = 5000
#: Change summaries kept per repo, newest first. Ceiling: older changes stay
#: in the repo's own ``changes`` table; raise this when a reader needs more.
@@ -54,7 +55,9 @@ CREATE TABLE repos (
index_updated_at TEXT,
refreshed_at TEXT NOT NULL,
health_json TEXT,
- error TEXT
+ error TEXT,
+ stats_json TEXT,
+ stats_stamp TEXT
);
CREATE TABLE files (
alias TEXT NOT NULL,
@@ -124,6 +127,10 @@ class RepoRows:
indexed_commit: str | None = None
index_updated_at: str | None = None
health: dict[str, Any] | None = None
+ #: :func:`.repo_stats.member_stats` of the repo and the ``wiki.db`` stamp
+ #: it was read at; None when it has no ``wiki.db`` or was written mid-read.
+ stats: dict[str, Any] | None = None
+ stats_stamp: str | None = None
files: list[str] = field(default_factory=list)
symbols: list[tuple[str, str, str, str, str]] = field(default_factory=list)
components: list[tuple[str, str, str | None, str | None, str | None, str, int]] = field(
@@ -187,7 +194,7 @@ class WorkspaceStore:
with _transaction(conn):
self._delete(alias)
conn.execute(
- "INSERT INTO repos VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)",
+ "INSERT INTO repos VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)",
(
alias,
path,
@@ -198,6 +205,8 @@ class WorkspaceStore:
datetime.now(UTC).isoformat(),
json.dumps(rows.health) if rows.health is not None else None,
rows.error,
+ json.dumps(rows.stats) if rows.stats is not None else None,
+ rows.stats_stamp,
),
)
for table, items in (
@@ -257,10 +266,22 @@ def discard(path: Path) -> None:
async def read_repo_rows(alias: str, repo_path: Path, services: list[tuple[str, str]]) -> RepoRows:
"""Read one repo's store rows from its ``wiki.db``; never raises."""
- from .registry import open_repo_db, repo_db_path
+ from .registry import repo_db_path
+ from .repo_stats import stamped_member_stats
- if not repo_db_path(repo_path).is_file():
+ db_path = repo_db_path(repo_path)
+ if not db_path.is_file():
return RepoRows()
+ rows = await _read_index(alias, repo_path, services)
+ stamped = await asyncio.to_thread(stamped_member_stats, db_path)
+ if stamped is not None:
+ rows.stats_stamp, rows.stats = stamped
+ return rows
+
+
+async def _read_index(alias: str, repo_path: Path, services: list[tuple[str, str]]) -> RepoRows:
+ from .registry import open_repo_db
+
try:
engine, session_factory = await open_repo_db(repo_path)
except Exception as exc:
@@ -547,3 +568,39 @@ def read_repo_health(path: Path) -> dict[str, dict[str, Any]] | None:
}
for alias, indexed, commit, health, error in rows
}
+
+
+def read_member_stats(path: Path, wanted: dict[str, Path]) -> dict[str, dict[str, Any]]:
+ """``alias -> stored member stats`` for the repos of *wanted* (``alias ->
+ repo path``) whose stored figures are still current: same path, same
+ ``state.json`` stamp and same ``wiki.db`` stamp. A repo missing here is
+ read from its own ``wiki.db`` by the caller, so a write the store has not
+ seen (``repowise health`` persisting scores, say) is never served stale.
+ Empty when the store is absent, unreadable or of another schema version;
+ read only, so a reader never rebuilds it.
+ """
+ from .registry import repo_db_path
+ from .repo_stats import db_stamp
+
+ if not Path(path).is_file():
+ return {}
+ try:
+ uri = f"{Path(path).resolve().as_uri()}?mode=ro"
+ with closing(sqlite3.connect(uri, uri=True)) as conn:
+ conn.execute(f"PRAGMA busy_timeout={_BUSY_TIMEOUT_MS}")
+ if conn.execute("PRAGMA user_version").fetchone()[0] != SCHEMA_VERSION:
+ return {}
+ rows = conn.execute(
+ "SELECT alias, path, stamp, stats_json, stats_stamp FROM repos "
+ "WHERE stats_json IS NOT NULL"
+ ).fetchall()
+ except sqlite3.Error:
+ return {}
+ out: dict[str, dict[str, Any]] = {}
+ for alias, key, stamp, stats, stats_stamp in rows:
+ repo = wanted.get(alias)
+ if repo is None or key != str(repo) or stamp != repo_stamp(repo):
+ continue
+ if stats_stamp == db_stamp(repo_db_path(repo)):
+ out[alias] = json.loads(stats)
+ return out
diff --git a/packages/server/src/repowise/server/routers/workspace.py b/packages/server/src/repowise/server/routers/workspace.py
index eb4e5036e..2806fdaca 100644
--- a/packages/server/src/repowise/server/routers/workspace.py
+++ b/packages/server/src/repowise/server/routers/workspace.py
@@ -7,6 +7,7 @@ startup from ``.repowise-workspace/`` JSON files) — no DB access needed.
from __future__ import annotations
+import asyncio
import logging
import sqlite3
from pathlib import Path
@@ -14,10 +15,10 @@ from typing import Literal
from fastapi import APIRouter, Depends, HTTPException, Query, Request
-from repowise.core.analysis.external_systems.links import IMPORT_TARGET_LANGUAGES
-from repowise.core.docs_mode import resolve_docs_mode
from repowise.core.workspace import reads
-from repowise.core.workspace.update import synced_at_iso
+from repowise.core.workspace.registry import repo_db_path
+from repowise.core.workspace.repo_stats import member_stats, query_repo_stats
+from repowise.core.workspace.store import read_member_stats, store_path
from repowise.server.deps import (
get_cross_repo_enricher,
get_workspace_config,
@@ -54,7 +55,6 @@ from repowise.server.schemas.actions import (
WorkspaceRepoActions,
)
from repowise.server.schemas.workspace import WorkspaceLinksBetweenResponse
-from repowise.server.services.module_health import read_repo_health_score
router = APIRouter(
prefix="/api/workspace",
@@ -72,115 +72,16 @@ def _require_workspace(ws_config: object) -> None:
raise HTTPException(status_code=404, detail="Not running in workspace mode")
-# The raw-sqlite form of ``crud.indexed_file``: these readers open each repo's
-# store directly, without a session.
-_INDEXED_FILE_SQL = "node_type = 'file' AND language NOT IN ({})".format(
- ", ".join(f"'{lang}'" for lang in IMPORT_TARGET_LANGUAGES)
-)
-
-
-def _query_top_language(db_path: Path) -> str:
- """Return the most common language across graph_nodes in a repo's wiki.db."""
- if not db_path.exists():
- return "unknown"
- try:
- with sqlite3.connect(str(db_path)) as conn:
- row = conn.execute(
- "SELECT language, COUNT(*) AS cnt FROM graph_nodes "
- f"WHERE {_INDEXED_FILE_SQL} AND language IS NOT NULL AND language != '' "
- "GROUP BY language ORDER BY cnt DESC LIMIT 1"
- ).fetchone()
- return row[0] if row else "unknown"
- except Exception:
- return "unknown"
-
-
-def _query_repo_stats(db_path: Path) -> dict:
- """Query basic stats from a repo's wiki.db using raw sqlite3.
-
- Returns a dict with repo_id, file_count, symbol_count, page_count,
- fresh_page_count, doc_coverage_pct, hotspot_count, health_score, status,
- docs_enabled, docs_skip_reason, and (when the row is read) indexed_at.
- All values default to sensible neutrals on error.
-
- ``health_score`` is None when the repo carries no health metrics, and
- stays None: no other figure here measures code health.
- """
- result: dict = {
- "repo_id": None,
- "file_count": 0,
- "symbol_count": 0,
- "page_count": 0,
- "fresh_page_count": 0,
- "doc_coverage_pct": 0.0,
- "hotspot_count": 0,
- "health_score": None,
- "status": "needs_index",
- "docs_enabled": False,
- "docs_skip_reason": None,
+def _member_stats(ws_config, ws_root: Path, read=query_repo_stats) -> dict[str, dict]:
+ """``alias -> member stats`` for every configured repo: the workspace
+ store's row when it is current, else *read* of the repo's own ``wiki.db``.
+ Blocking (stats and SQLite), so callers run it in a worker thread."""
+ wanted = {r.alias: (ws_root / r.path).resolve() for r in ws_config.repos}
+ stored = read_member_stats(store_path(ws_root), wanted)
+ return {
+ alias: stored[alias] if alias in stored else read(repo_db_path(repo))
+ for alias, repo in wanted.items()
}
- # Surface docs lifecycle from state.json so the UI shows a coherent
- # picture even when only indexing (no docs) ran.
- try:
- import json as _json
-
- state_path = db_path.parent / "state.json"
- if state_path.is_file():
- state = _json.loads(state_path.read_text(encoding="utf-8"))
- result["docs_enabled"] = resolve_docs_mode(state) != "none"
- result["docs_skip_reason"] = state.get("docs_skip_reason")
- except Exception:
- pass
- if not db_path.exists():
- if not db_path.parent.parent.is_dir():
- result["status"] = "missing_dir"
- return result
- result["status"] = "indexed"
- try:
- conn = sqlite3.connect(str(db_path))
- c = conn.cursor()
-
- # repo id, and the sync time every other surface reports
- row = c.execute("SELECT id, updated_at FROM repositories LIMIT 1").fetchone()
- if row:
- result["repo_id"] = row[0]
- result["indexed_at"] = synced_at_iso(row[1])
-
- # The raw-sqlite form of crud.count_files.
- row = c.execute(f"SELECT COUNT(*) FROM graph_nodes WHERE {_INDEXED_FILE_SQL}").fetchone()
- result["file_count"] = row[0] if row else 0
-
- # The raw-sqlite form of crud.count_symbols: one store per repo here.
- row = c.execute("SELECT COUNT(*) FROM wiki_symbols").fetchone()
- result["symbol_count"] = int(row[0]) if row else 0
-
- # Pages, fresh pages, and mean page confidence * 100. The last is named
- # doc_coverage_pct on the wire; it is not a share of files documented.
- pages, fresh, confidence = c.execute(
- "SELECT COUNT(*), SUM(freshness_status = 'fresh'), AVG(confidence) FROM wiki_pages"
- ).fetchone()
- result["page_count"] = pages
- result["fresh_page_count"] = int(fresh or 0)
- result["doc_coverage_pct"] = round(float(confidence or 0.0) * 100, 1)
-
- # hotspot count — use the canonical is_hotspot flag, matching the rest
- # of the codebase (module_health, extract-demo-data). The earlier
- # churn_percentile >= 90 predicate never matched: churn_percentile is
- # stored on a 0.0-1.0 scale and only scaled to 0-100 at the API layer.
- try:
- row = c.execute("SELECT COUNT(*) FROM git_metadata WHERE is_hotspot = 1").fetchone()
- result["hotspot_count"] = row[0] if row else 0
- except sqlite3.OperationalError:
- pass # table or column may not exist
-
- conn.close()
- except Exception:
- _log.debug("Failed to query stats from %s", db_path, exc_info=True)
-
- # Read once here rather than per-endpoint: both the workspace listing and
- # the graph need the canonical score, and this used to be a second sweep.
- result["health_score"] = read_repo_health_score(db_path)
- return result
# ---------------------------------------------------------------------------
@@ -205,13 +106,13 @@ async def get_workspace(
ws_root = getattr(request.app.state, "workspace_root", None)
ws_root_path = Path(ws_root) if ws_root else None
+ all_stats = (
+ await asyncio.to_thread(_member_stats, ws_config, ws_root_path) if ws_root_path else {}
+ )
repo_entries = []
for r in ws_config.repos:
- stats: dict = {}
- if ws_root_path:
- repo_path = (ws_root_path / r.path).resolve()
- db_path = repo_path / ".repowise" / "wiki.db"
- stats = _query_repo_stats(db_path)
+ stats = dict(all_stats.get(r.alias, {}))
+ stats.pop("top_language", None)
# The member's own store first: a repo updated on its own leaves the
# config's time behind.
indexed_at = stats.pop("indexed_at", r.indexed_at)
@@ -431,16 +332,14 @@ async def get_workspace_graph(
ws_root_path = Path(ws_root) if ws_root else None
# Build nodes from repo metadata
+ all_stats = (
+ await asyncio.to_thread(_member_stats, ws_config, ws_root_path, member_stats) if ws_root_path else {}
+ )
repo_id_map: dict[str, str] = {} # alias → repo_id
nodes: list[WorkspaceGraphNode] = []
for r in ws_config.repos:
- stats: dict = {}
- top_language = "unknown"
- if ws_root_path:
- repo_path = (ws_root_path / r.path).resolve()
- db_path = repo_path / ".repowise" / "wiki.db"
- stats = _query_repo_stats(db_path)
- top_language = _query_top_language(db_path)
+ stats = all_stats.get(r.alias, {})
+ top_language = stats.get("top_language", "unknown")
rid = stats.get("repo_id") or r.alias
repo_id_map[r.alias] = rid
nodes.append(
@@ -735,12 +634,12 @@ async def get_workspace_actions(
ws_root = getattr(request.app.state, "workspace_root", None)
ws_root_path = Path(ws_root) if ws_root else None
+ all_stats = (
+ await asyncio.to_thread(_member_stats, ws_config, ws_root_path) if ws_root_path else {}
+ )
repos: list[WorkspaceRepoActions] = []
for r in ws_config.repos:
- repo_id = None
- if ws_root_path:
- db_path = (ws_root_path / r.path).resolve() / ".repowise" / "wiki.db"
- repo_id = _query_repo_stats(db_path).get("repo_id")
+ repo_id = all_stats.get(r.alias, {}).get("repo_id")
if not repo_id:
repos.append(
WorkspaceRepoActions(
diff --git a/packages/server/src/repowise/server/services/module_health.py b/packages/server/src/repowise/server/services/module_health.py
index 139feb489..a400b3f72 100644
--- a/packages/server/src/repowise/server/services/module_health.py
+++ b/packages/server/src/repowise/server/services/module_health.py
@@ -9,8 +9,6 @@ views ``module_path`` is the path prefix the caller passed in.
from __future__ import annotations
import json
-import sqlite3
-from pathlib import Path
from sqlalchemy import func as sa_func
from sqlalchemy import select
@@ -74,37 +72,6 @@ async def aggregate_modules(session: AsyncSession, repo_id: str) -> dict[str, Mo
return _fold.aggregate_modules(files, sym_rows, dead_rows, decisions)
-def read_repo_health_score(db_path: Path) -> float | None:
- """Return the canonical repo health score from a repo-local wiki.db."""
- if not db_path.exists():
- return None
- # Aggregate in SQL rather than reading every row. A workspace calls this once
- # per repo, and the fetchall() this replaces read ~6,000 rows per repo to
- # produce one number: 61.6ms against 33.2ms across six large repos, and the
- # row count grows with the repo. MAX(CAST(..), 1) reproduces the previous
- # `max(int(nloc or 0), 1)` weight, truncation included; verified identical on
- # 109 real databases.
- try:
- with sqlite3.connect(str(db_path)) as conn:
- row = conn.execute(
- "SELECT SUM(score * w), SUM(w) FROM ("
- " SELECT score, MAX(CAST(COALESCE(nloc, 0) AS INTEGER), 1) AS w"
- " FROM health_file_metrics WHERE score IS NOT NULL"
- ")"
- ).fetchone()
- except sqlite3.OperationalError:
- return None
- except Exception:
- return None
-
- if not row or not row[1]:
- return None
-
- weighted_sum, total_weight = row
- average = float(weighted_sum) / float(total_weight)
- return max(0.0, min(100.0, round(average * 10.0, 1)))
-
-
async def build_single_file_health(
session: AsyncSession, repo_id: str, file_path: str
) -> dict | None:
@@ -251,7 +218,6 @@ __all__ = [
"aggregate_modules",
"build_single_file_health",
"detail_extras",
- "read_repo_health_score",
"summarize",
"top_level_module",
]
diff --git a/tests/unit/server/test_workspace_router.py b/tests/unit/server/test_workspace_router.py
index 2e574bf74..91737586d 100644
--- a/tests/unit/server/test_workspace_router.py
+++ b/tests/unit/server/test_workspace_router.py
@@ -17,6 +17,7 @@ from httpx import ASGITransport, AsyncClient
from repowise.core.workspace.cross_repo import MAX_EDGES, MAX_EDGES_PER_REPO_PAIR
from repowise.core.workspace.diagnostics import ExtractionDiagnostics
+from repowise.core.workspace.repo_stats import query_repo_stats, query_top_language
from repowise.core.workspace.test_impact import (
UnresolvedLink,
WorkspaceTestImpactResult,
@@ -926,7 +927,7 @@ class TestGetWorkspaceGraph:
# ---------------------------------------------------------------------------
-# Tests — _query_repo_stats
+# Tests — query_repo_stats
# ---------------------------------------------------------------------------
@@ -949,7 +950,7 @@ class TestQueryRepoStats:
"""
db_path.parent.mkdir(parents=True, exist_ok=True)
with sqlite3.connect(str(db_path)) as conn:
- # _query_repo_stats reads several tables before the hotspot count;
+ # query_repo_stats reads several tables before the hotspot count;
# a missing table aborts the whole block, so create the minimal set.
conn.executescript(
"""
@@ -997,7 +998,7 @@ class TestQueryRepoStats:
rows=[(1, 0.99), (1, 0.95), (0, 0.10)],
)
- stats = workspace._query_repo_stats(db_path)
+ stats = query_repo_stats(db_path)
assert stats["hotspot_count"] == 2
@@ -1006,7 +1007,7 @@ class TestQueryRepoStats:
the per-file emission counts (5 here)."""
_create_workspace_repo_db(tmp_path, "backend")
- stats = workspace._query_repo_stats(tmp_path / "backend" / ".repowise" / "wiki.db")
+ stats = query_repo_stats(tmp_path / "backend" / ".repowise" / "wiki.db")
assert stats["symbol_count"] == 3
@@ -1014,7 +1015,7 @@ class TestQueryRepoStats:
db_path = tmp_path / ".repowise" / "wiki.db"
self._make_wiki_db(db_path, rows=[(0, 0.99), (0, 0.80)])
- stats = workspace._query_repo_stats(db_path)
+ stats = query_repo_stats(db_path)
assert stats["hotspot_count"] == 0
@@ -1046,7 +1047,7 @@ class TestQueryRepoStats:
],
)
- stats = workspace._query_repo_stats(db_path)
+ stats = query_repo_stats(db_path)
assert stats["file_count"] == 3
@@ -1076,7 +1077,7 @@ class TestQueryRepoStats:
],
)
- top_language = workspace._query_top_language(db_path)
+ top_language = query_top_language(db_path)
assert top_language == "python"
@@ -1093,7 +1094,7 @@ class TestQueryRepoStats:
],
)
- assert workspace._query_top_language(db_path) == "python"
+ assert query_top_language(db_path) == "python"
# ---------------------------------------------------------------------------
@@ -1719,6 +1720,63 @@ class TestRepoQueryBudget:
assert connections == self.CONNECTIONS_PER_REPO * 3
+def _store_member_stats(root: Path, aliases: list[str]) -> None:
+ """What a workspace refresh writes for *aliases*: their current stats rows."""
+ from repowise.core.workspace.repo_stats import stamped_member_stats
+ from repowise.core.workspace.store import (
+ RepoRows,
+ WorkspaceStore,
+ repo_stamp,
+ store_path,
+ )
+
+ store = WorkspaceStore(store_path(root))
+ for alias in aliases:
+ repo = (root / alias).resolve()
+ stamp, stats = stamped_member_stats(repo / ".repowise" / "wiki.db")
+ rows = RepoRows(indexed=True, stats=stats, stats_stamp=stamp)
+ store.replace_repo(alias, str(repo), repo_stamp(repo), rows)
+ store.close()
+
+
+class TestMemberStatsFromTheStore:
+ """The listing and graph read a current workspace store instead of every index."""
+
+ async def _get(self, root: Path, url: str) -> dict:
+ app = _make_workspace_app(ws_config=_make_ws_config(), workspace_root=str(root))
+ async with AsyncClient(transport=ASGITransport(app=app), base_url="http://test") as c:
+ resp = await c.get(url)
+ assert resp.status_code == 200
+ return resp.json()
+
+ @pytest.mark.asyncio
+ @pytest.mark.parametrize("url", ["/api/workspace", "/api/workspace/graph"])
+ async def test_store_response_equals_the_per_repo_reads(self, tmp_path: Path, url) -> None:
+ _create_workspace_repo_db(tmp_path, "backend", health_rows=[(2.0, 10), (9.0, 30)])
+ live = await self._get(tmp_path, url)
+ _store_member_stats(tmp_path, ["backend"])
+
+ with patch.object(sqlite3, "connect", wraps=sqlite3.connect) as connect:
+ stored = await self._get(tmp_path, url)
+
+ assert stored == live
+ # One connection, the store's; no member index is opened.
+ assert connect.call_count == 1
+ assert "workspace.db" in str(connect.call_args.args[0])
+
+ @pytest.mark.asyncio
+ async def test_a_write_after_the_refresh_is_read_live(self, tmp_path: Path) -> None:
+ _create_workspace_repo_db(tmp_path, "backend")
+ _store_member_stats(tmp_path, ["backend"])
+ with sqlite3.connect(str(tmp_path / "backend" / ".repowise" / "wiki.db")) as conn:
+ conn.execute("INSERT INTO wiki_symbols (id) VALUES ('sym-late')")
+
+ data = await self._get(tmp_path, "/api/workspace")
+
+ backend = next(r for r in data["repos"] if r["alias"] == "backend")
+ assert backend["symbol_count"] == 4
+
+
class TestGetTestImpact:
"""`/api/workspace/test-impact`: consumer tests for a provider change."""
diff --git a/tests/unit/workspace/test_store.py b/tests/unit/workspace/test_store.py
index a50856f04..2cc346021 100644
--- a/tests/unit/workspace/test_store.py
+++ b/tests/unit/workspace/test_store.py
@@ -16,10 +16,12 @@ from repowise.core.persistence.models import (
WikiSymbol,
)
from repowise.core.workspace.config import RepoEntry, WorkspaceConfig
-from repowise.core.workspace.registry import open_repo_db
+from repowise.core.workspace.registry import open_repo_db, repo_db_path
+from repowise.core.workspace.repo_stats import db_stamp, member_stats
from repowise.core.workspace.store import (
SCHEMA_VERSION,
lookup_symbols,
+ read_member_stats,
refresh_workspace_store,
store_path,
)
@@ -260,3 +262,73 @@ async def test_an_unmoved_workspace_refreshes_without_opening_the_store(tmp_path
monkeypatch.setattr(store_mod, "WorkspaceStore", refuse)
assert await refresh_workspace_store(config, root) == path
+
+
+def resolved(config: WorkspaceConfig, root: Path) -> dict[str, Path]:
+ return {e.alias: (root / e.path).resolve() for e in config.repos}
+
+
+async def test_stored_member_stats_equal_a_read_of_each_index(tmp_path):
+ config, root = await make_workspace(tmp_path)
+ path = await refresh_workspace_store(config, root)
+ wanted = resolved(config, root)
+
+ stored = read_member_stats(path, wanted)
+
+ assert stored == {a: member_stats(repo_db_path(r)) for a, r in wanted.items()}
+ assert stored["api"]["symbol_count"] == 2 and stored["api"]["status"] == "indexed"
+
+
+async def test_member_stats_written_after_the_refresh_are_not_served(tmp_path):
+ config, root = await make_workspace(tmp_path)
+ path = await refresh_workspace_store(config, root)
+ wanted = resolved(config, root)
+
+ # A write that leaves state.json alone (``repowise health`` persisting scores).
+ await add_symbol(root / "api", "api", "src/app.py", "late")
+ assert set(read_member_stats(path, wanted)) == {"web"}
+
+ touch_state(root / "web")
+ assert read_member_stats(path, wanted) == {}
+
+ # The refresh re-reads repos whose state.json moved; api stays read live
+ # until its next index run.
+ await refresh_workspace_store(config, root)
+ assert set(read_member_stats(path, wanted)) == {"web"}
+ touch_state(root / "api")
+ await refresh_workspace_store(config, root)
+ assert read_member_stats(path, wanted)["api"]["symbol_count"] == 3
+
+
+async def test_member_stats_of_an_unindexed_or_absent_store_are_empty(tmp_path):
+ config, root = await make_workspace(tmp_path)
+ wanted = resolved(config, root)
+ assert read_member_stats(store_path(root), wanted) == {}
+
+ (root / "docs").mkdir()
+ config = make_config("api", "web", "docs")
+ path = await refresh_workspace_store(config, root)
+ assert set(read_member_stats(path, resolved(config, root))) == {"api", "web"}
+
+
+def test_db_stamp_moves_on_a_wal_commit_and_never_on_a_read(tmp_path):
+ db = tmp_path / "wiki.db"
+ holder = sqlite3.connect(db) # held open, as a running server holds its pool
+ try:
+ holder.execute("PRAGMA journal_mode=WAL")
+ holder.execute("CREATE TABLE t (x)")
+ holder.commit()
+ stamp = db_stamp(db)
+
+ reader = sqlite3.connect(db)
+ reader.execute("SELECT COUNT(*) FROM t").fetchone()
+ reader.close()
+ assert db_stamp(db) == stamp
+
+ writer = sqlite3.connect(db)
+ writer.execute("INSERT INTO t VALUES (1)")
+ writer.commit()
+ writer.close()
+ assert db_stamp(db) != stamp
+ finally:
+ holder.close()