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
feat(chat): a chart view built from a small spec over any result (#3843)
Raghav Chamadiya1d73039dac32
diff --git a/docs/architecture/chat.md b/docs/architecture/chat.mdindex 6778bacde..38c9b4b6b 100644--- a/docs/architecture/chat.md+++ b/docs/architecture/chat.md@@ -278,6 +278,11 @@ saved to the database and a `done` event is emitted.result. Every result the model sees starts with its `artifact_id`, and`show_view` takes any id from the conversation or `latest:<tool_name>` forthat tool's newest result. A chart whose values are all zero is not drawn.+When no named view fits, the `chart` view takes a small spec (rows, x, y,+series, filter, aggregate, sort, top with an Other bucket, kind) and the server+filters, groups and draws it, picking the chart type from the data's shape+unless one is asked for. The same guards apply: no person fields, no risk score+as a plotted value, and attribution figures only through `changes.provenance`.---diff --git a/packages/api-client/src/views/chart.ts b/packages/api-client/src/views/chart.tsindex 32ff43203..0dc27933a 100644--- a/packages/api-client/src/views/chart.ts+++ b/packages/api-client/src/views/chart.ts@@ -3,8 +3,9 @@* check that the chart reads that table honestly: axes name real columns,* plotted values are numbers in one unit, a donut is a whole of two to five* non-negative parts with a positive total, a stacked bar is not all zeros,- * and a stated sort order is the order the rows are in (a sorted chart may- * not hold nulls in its series, since a null has no place in the order).+ * a scatter's x is a number, and a stated sort order is the order the rows+ * are in (a sorted chart may not hold nulls in its series, since a null has+ * no place in the order).*/import {@@ -35,6 +36,7 @@ const SERIES_BOUNDS: { [T in ChartType]: [number, number] } = {line: [1, VIEW_LIMITS.chart_series],stacked_bar: [2, VIEW_LIMITS.chart_series],donut: [1, 1],+ scatter: [1, VIEW_LIMITS.chart_series],};/** Chart types whose values are parts of a whole, so never negative. */const PARTS_OF_A_WHOLE: ReadonlySet<ChartType> = new Set(["stacked_bar", "donut"]);@@ -89,6 +91,9 @@ function chartRules(v: Rec, p: string, e: string[]): void {let total = 0;rows.forEach((r, i) => {if (typeof v.x === "string" && r[v.x] === null) e.push(`${p}.data.rows[${i}].${v.x}: x is null`);+ else if (type === "scatter" && typeof v.x === "string" && !isFiniteNumber(r[v.x])) {+ e.push(`${p}.data.rows[${i}].${v.x}: a scatter x is a number`);+ }for (const s of plotted) {const cell = r[s];if (cell === undefined) continue;diff --git a/packages/server/src/repowise/server/chat_view_spec.py b/packages/server/src/repowise/server/chat_view_spec.pynew file mode 100644index 000000000..a0a4638c4--- /dev/null+++ b/packages/server/src/repowise/server/chat_view_spec.py@@ -0,0 +1,699 @@+"""The ``chart`` view's spec: parse and check it, then turn rows into chart rows.++Pure functions over a list of row dicts; the ``chart`` builder in+:mod:`repowise.server.chat_views` picks the rows, applies the view guards and+makes the part. One pass over the rows filters and groups; sort, top and the+Other bucket then work on the groups. Every refusal is a ``SpecError`` whose+message says what to change, and names the fields the rows hold, so the model+can fix its spec in one retry.+"""++from __future__ import annotations++import difflib+import re+from collections.abc import Mapping, Sequence+from dataclasses import dataclass, field+from typing import Any++from repowise.server.schemas.views import VIEW_LIMITS++AGGREGATES = ("count", "sum", "avg", "min", "max")+OPS = ("eq", "ne", "lt", "lte", "gt", "gte", "in", "contains", "prefix")+KINDS = ("auto", "bar", "stacked_bar", "line", "donut", "scatter")+OPTIONS = frozenset(+ {"rows", "x", "y", "series", "filter", "group_by", "aggregate", "sort", "top", "other"}+ | {"kind", "title", "subtitle"}+)+OTHER_LABEL = "Other"++_ADDITIVE = frozenset({"count", "sum"})+_AGGREGATE_LABEL = {+ "count": "Count",+ "sum": "Total",+ "avg": "Average",+ "min": "Lowest",+ "max": "Highest",+}+_KEY = re.compile(r"^[a-z][a-z0-9_]{0,63}$")+_TIME = re.compile(+ r"^\d{4}-\d{2}(-\d{2}([T ]\d{2}:\d{2}(:\d{2}(\.\d+)?)?)?(Z|[+-]\d{2}:?\d{2})?)?$"+)+# Fields that name a point in a scatter or an unaggregated chart, in preference order.+_LABEL_FIELDS = ("file_path", "path", "name", "title", "id")+_MAX_FILTERS = 10+_MAX_TEXT = 120+_FIELDS_SHOWN = 30+++class SpecError(ValueError):+ """A spec the rows cannot satisfy; the message says what to change."""++ def __init__(+ self, message: str, code: str = "bad_spec", fields: Mapping[str, str] | None = None+ ) -> None:+ super().__init__(message)+ self.code = code+ self.fields = dict(fields) if fields else None+++@dataclass(frozen=True)+class Filter:+ field: str+ op: str+ value: Any+++@dataclass(frozen=True)+class ChartSpec:+ x: str+ y: tuple[str, ...]+ series: str | None = None+ filters: tuple[Filter, ...] = ()+ aggregate: str | None = None+ sort: tuple[str, str] | None = None+ top: int | None = None+ other: bool = False+ kind: str = "auto"+ title: str | None = None+ subtitle: str | None = None++ @property+ def measures(self) -> tuple[str, ...]:+ """Keys of the plotted measures before any series split."""+ return ("count",) if self.aggregate == "count" else self.y++ def fields(self) -> list[str]:+ """Every row field the spec reads."""+ names = [self.x, *self.y, *(f.field for f in self.filters)]+ if self.series:+ names.append(self.series)+ return list(dict.fromkeys(names))+++@dataclass+class ChartRows:+ """Chart rows and what was done to get them."""++ x: str+ x_type: str+ columns: list[tuple[str, str]]+ series: list[str]+ rows: list[dict[str, Any]]+ chart_type: str+ reason: str+ total_rows: int+ matched: int+ source_rows: int+ missing_x: int = 0+ sort: str | None = None+ other: bool = False+++# ---------------------------------------------------------------------------+# Parse+# ---------------------------------------------------------------------------+++def _name(options: Mapping[str, Any], key: str) -> str | None:+ value = options.get(key)+ if value is None:+ return None+ if not isinstance(value, str) or not value:+ raise SpecError(f"{key} must be a field name")+ return value+++def _text(options: Mapping[str, Any], key: str) -> str | None:+ value = options.get(key)+ if value is None:+ return None+ if not isinstance(value, str) or not value.strip() or len(value) > _MAX_TEXT:+ raise SpecError(f"{key} must be text of 1 to {_MAX_TEXT} characters")+ return value.strip()+++def _scalar(value: Any) -> bool:+ return value is None or isinstance(value, (str, int, float, bool))+++def _filter(raw: Any, i: int) -> Filter:+ if not isinstance(raw, Mapping) or not isinstance(raw.get("field"), str):+ raise SpecError(f"filter[{i}] must be {{field, op, value}}")+ op = raw.get("op", "eq")+ if op not in OPS:+ raise SpecError(f"filter[{i}].op {op!r} is not one of {list(OPS)}")+ value = raw.get("value")+ if op == "in":+ if not isinstance(value, list) or not value or not all(_scalar(v) for v in value):+ raise SpecError(f"filter[{i}]: op in takes a non-empty list of values")+ value = tuple(value)+ elif op in ("contains", "prefix"):+ if not isinstance(value, str):+ raise SpecError(f"filter[{i}]: op {op} takes a text value")+ elif op in ("lt", "lte", "gt", "gte"):+ if isinstance(value, bool) or not isinstance(value, (int, float, str)):+ raise SpecError(f"filter[{i}]: op {op} takes a number or text value")+ elif not _scalar(value):+ raise SpecError(f"filter[{i}]: op {op} takes one value")+ return Filter(raw["field"], op, value)+++def parse_spec(options: Mapping[str, Any]) -> ChartSpec:+ """A spec from ``show_view`` options, checked for shape only (not against rows)."""+ x, group_by = _name(options, "x"), _name(options, "group_by")+ if x and group_by and x != group_by:+ raise SpecError("x and group_by name the same axis; pass one of them")+ x = x or group_by+ if x is None:+ raise SpecError("pass x: the field the chart runs along (a category or a date)")++ raw_y = options.get("y")+ if raw_y is None:+ y: tuple[str, ...] = ()+ elif isinstance(raw_y, str):+ y = (raw_y,)+ elif isinstance(raw_y, list) and raw_y and all(isinstance(v, str) and v for v in raw_y):+ y = tuple(dict.fromkeys(raw_y))+ else:+ raise SpecError("y must be a field name or a list of field names")+ if len(y) > VIEW_LIMITS["chart_series"]:+ raise SpecError(f"y takes at most {VIEW_LIMITS['chart_series']} fields")++ aggregate = options.get("aggregate")+ if aggregate is not None and aggregate not in AGGREGATES:+ raise SpecError(f"aggregate {aggregate!r} is not one of {list(AGGREGATES)}")+ if aggregate is None and not y:+ aggregate = "count"+ if aggregate is None and group_by:+ raise SpecError(f"group_by needs aggregate, one of {list(AGGREGATES)}")+ if aggregate == "count" and y:+ raise SpecError("aggregate count counts rows; drop y, or use sum, avg, min or max")+ if aggregate not in (None, "count") and not y:+ raise SpecError(f"aggregate {aggregate} needs y, the numeric field to {aggregate}")++ series = _name(options, "series")+ if series and len(y) > 1:+ raise SpecError("with series, y takes one field, which series splits by value")+ if series and (series == x or series in y):+ raise SpecError("series must be a field other than x and y")+ if x in y:+ raise SpecError("x cannot also be a y field")++ raw_filters = options.get("filter") or []+ if isinstance(raw_filters, Mapping):+ raw_filters = [raw_filters]+ if not isinstance(raw_filters, list) or len(raw_filters) > _MAX_FILTERS:+ raise SpecError(f"filter must be a list of at most {_MAX_FILTERS} {{field, op, value}}")+ filters = tuple(_filter(raw, i) for i, raw in enumerate(raw_filters))++ sort = options.get("sort")+ if sort is not None:+ if isinstance(sort, str):+ sort = {"field": sort}+ if not isinstance(sort, Mapping) or not isinstance(sort.get("field"), str):+ raise SpecError('sort must be {"field": <x or a y field>, "order": "asc" | "desc"}')+ order = sort.get("order", "desc")+ if order not in ("asc", "desc"):+ raise SpecError('sort order must be "asc" or "desc"')+ sort = (sort["field"], order)++ other = options.get("other", False)+ if not isinstance(other, bool):+ raise SpecError("other must be true or false")+ top = options.get("top")+ most = VIEW_LIMITS["chart_points"] - (1 if other else 0)+ whole = isinstance(top, int) and not isinstance(top, bool)+ if top is not None and not (whole and 1 <= top <= most):+ raise SpecError(f"top must be a whole number from 1 to {most}")+ if other and top is None:+ raise SpecError("other needs top: the rows past top go into Other")+ if other and aggregate not in _ADDITIVE:+ raise SpecError("other adds up the rows past top, so it needs aggregate count or sum")++ kind = options.get("kind", "auto")+ if kind not in KINDS:+ raise SpecError(f"kind {kind!r} is not one of {list(KINDS)}")++ return ChartSpec(+ x=x,+ y=y,+ series=series,+ filters=filters,+ aggregate=aggregate,+ sort=sort,+ top=top,+ other=other,+ kind=kind,+ title=_text(options, "title"),+ subtitle=_text(options, "subtitle"),+ )+++# ---------------------------------------------------------------------------+# Fields+# ---------------------------------------------------------------------------+++def _value_type(value: Any) -> str:+ if isinstance(value, bool):+ return "text"+ if isinstance(value, (int, float)):+ return "number"+ if isinstance(value, str):+ return "time" if _TIME.match(value) else "text"+ return "nested"+++def field_types(rows: Sequence[Mapping[str, Any]]) -> dict[str, str]:+ """Chartable fields of *rows* and their type: number, time or text.++ A field mixing numbers and text is text; one holding objects or lists is+ left out, as is a key that cannot be a column key.+ """+ seen: dict[str, str | None] = {}+ for row in rows:+ for key, value in row.items():+ if value is None:+ seen.setdefault(key, None)+ continue+ kind = _value_type(value)+ before = seen.get(key)+ if before is None or before == kind:+ seen[key] = kind+ elif "nested" in (before, kind):+ seen[key] = "nested"+ else:+ seen[key] = "text"+ return {k: t or "text" for k, t in seen.items() if t != "nested" and _KEY.match(k)}+++def _shown(types: Mapping[str, str]) -> dict[str, str]:+ return dict(list(types.items())[:_FIELDS_SHOWN])+++def _unknown(name: str, role: str, types: Mapping[str, str]) -> SpecError:+ close = difflib.get_close_matches(name, list(types), n=3, cutoff=0.6)+ hint = f" Did you mean {', '.join(repr(c) for c in close)}?" if close else ""+ return SpecError(+ f"{role} {name!r} is not a field of these rows.{hint} Fields are listed in `fields`.",+ fields=_shown(types),+ )+++def check_fields(spec: ChartSpec, types: Mapping[str, str]) -> None:+ """Every field the spec reads is in *types*, and measures are numeric."""+ roles = [+ ("x", spec.x),+ *(("y", y) for y in spec.y),+ *(("filter field", f.field) for f in spec.filters),+ ]+ if spec.series:+ roles.append(("series", spec.series))+ for role, name in roles:+ if name not in types:+ raise _unknown(name, role, types)+ numeric = [k for k, t in types.items() if t == "number"]+ for y in spec.y:+ if types[y] != "number":+ raise SpecError(+ f"y {y!r} holds {types[y]}; y fields must be numeric: {numeric[:_FIELDS_SHOWN]}",+ fields=_shown(types),+ )+ if spec.kind == "scatter" and types[spec.x] != "number":+ raise SpecError(+ f"a scatter needs a numeric x and {spec.x!r} holds {types[spec.x]}; "+ f"numeric fields: {numeric[:_FIELDS_SHOWN]}",+ fields=_shown(types),+ )+ if spec.other and types[spec.x] != "text":+ raise SpecError(+ f"other needs a category x and {spec.x!r} holds {types[spec.x]}; "+ "drop other, or use top alone"+ )+ if spec.sort and spec.sort[0] not in (spec.x, *spec.measures):+ choices = [spec.x, *spec.measures]+ raise SpecError(f"sort field {spec.sort[0]!r} must be one of {choices}")+++# ---------------------------------------------------------------------------+# Transform+# ---------------------------------------------------------------------------+++def _passes(f: Filter, value: Any) -> bool:+ op, want = f.op, f.value+ if op == "eq":+ return value == want+ if op == "ne":+ return value != want+ if op == "in":+ return value in want+ if op == "contains":+ return isinstance(value, str) and want in value+ if op == "prefix":+ return isinstance(value, str) and value.startswith(want)+ # Ordering compares like with like; a null or a type mismatch never passes.+ if value is None or isinstance(value, bool) or isinstance(value, str) != isinstance(want, str):+ return False+ if op == "lt":+ return value < want+ if op == "lte":+ return value <= want+ if op == "gt":+ return value > want+ return value >= want+++class _Acc:+ """Running count, sum, min and max of one measure in one group."""++ __slots__ = ("hi", "lo", "n", "rows", "total")++ def __init__(self) -> None:+ self.rows = 0+ self.n = 0+ self.total: float = 0+ self.lo: float | None = None+ self.hi: float | None = None++ def add(self, value: Any) -> None:+ self.rows += 1+ if isinstance(value, (int, float)) and not isinstance(value, bool):+ self.n += 1+ self.total += value+ self.lo = value if self.lo is None or value < self.lo else self.lo+ self.hi = value if self.hi is None or value > self.hi else self.hi++ def value(self, aggregate: str) -> float | None:+ if aggregate == "count":+ return self.rows+ if not self.n:+ return None+ if aggregate == "sum":+ return _round(self.total)+ if aggregate == "avg":+ return round(self.total / self.n, 4)+ return self.lo if aggregate == "min" else self.hi+++def _round(value: float) -> float:+ # Float sums carry noise (0.1 + 0.2); a whole number stays an int.+ return value if isinstance(value, int) else round(value, 6)+++def _x_order(value: Any) -> tuple[bool, Any]:+ return (isinstance(value, str), value)+++def label(key: str) -> str:+ """A field name as a column label: ``line_count`` is "Line count"."""+ return key.replace("_", " ").capitalize()+++@dataclass+class _Pass:+ """What the single pass over the rows found."""++ groups: dict[Any, dict[Any, list[_Acc]]] = field(default_factory=dict)+ points: list[dict[str, Any]] = field(default_factory=list)+ series_values: dict[Any, None] = field(default_factory=dict)+ matched: int = 0+ missing_x: int = 0+++def _collect(+ spec: ChartSpec,+ rows: Sequence[Mapping[str, Any]],+ keep_points: bool,+ label_key: str | None,+ scatter: bool,+) -> _Pass:+ """Filter, then group (aggregate), pivot (series) or keep each row as a point."""+ found = _Pass()+ # Outside a scatter, a kept point is a category or a date, which must not repeat.+ seen_x: set[Any] | None = None if scatter else set()+ for row in rows:+ if not all(_passes(f, row.get(f.field)) for f in spec.filters):+ continue+ found.matched += 1+ x_value = row.get(spec.x)+ if x_value is None:+ found.missing_x += 1+ continue+ s_value = row.get(spec.series) if spec.series else None+ if spec.series:+ found.series_values.setdefault(s_value)+ if keep_points:+ if seen_x is not None:+ if x_value in seen_x:+ raise SpecError(+ f"x {spec.x!r} repeats ({x_value!r}); add aggregate (count or sum) "+ "to combine the rows that share it"+ )+ seen_x.add(x_value)+ point = {spec.x: x_value, **{y: row.get(y) for y in spec.y}, "__series": s_value}+ if label_key:+ point[label_key] = row.get(label_key)+ found.points.append(point)+ continue+ cells = found.groups.setdefault(x_value, {})+ accs = cells.get(s_value)+ if accs is None:+ accs = cells[s_value] = [_Acc() for _ in spec.measures]+ elif spec.aggregate is None:+ raise SpecError(+ f"several rows share {spec.x}={x_value!r} and {spec.series}={s_value!r}; "+ "add aggregate (sum, avg, min or max) to combine them"+ )+ for acc, measure in zip(accs, spec.measures, strict=True):+ acc.add(None if measure == "count" else row.get(measure))++ if found.matched == 0:+ what = "match the filters" if spec.filters else "were selected"+ raise SpecError(f"no rows {what} ({len(rows)} rows before filtering)", code="no_data")+ if found.matched == found.missing_x:+ raise SpecError(f"no matching row has a value for x {spec.x!r}", code="no_data")+ if len(found.series_values) > VIEW_LIMITS["chart_series"]:+ raise SpecError(+ f"series {spec.series!r} has {len(found.series_values)} values and a chart plots "+ f"at most {VIEW_LIMITS['chart_series']}; filter it with op in, or drop series"+ )+ return found+++def _value_label(value: Any) -> str:+ if value is None:+ return "none"+ if isinstance(value, bool):+ return "true" if value else "false"+ return str(value)+++def _measure_columns(spec: ChartSpec, found: _Pass) -> tuple[list[str], list[str]]:+ """Keys and labels of the plotted columns: one per measure, or one per series value."""+ if spec.series:+ keys = [f"s{i + 1}" for i in range(len(found.series_values))]+ return keys, [_value_label(v) for v in found.series_values]+ keys = ["row_count" if m == "count" and spec.x == "count" else m for m in spec.measures]+ labels = [_AGGREGATE_LABEL["count"] if m == "count" else label(m) for m in spec.measures]+ return keys, labels+++def _chart_rows(+ spec: ChartSpec, found: _Pass, keys: list[str], label_key: str | None+) -> list[dict[str, Any]]:+ by_value = dict(zip(found.series_values, keys, strict=True)) if spec.series else {}+ out: list[dict[str, Any]] = []+ for p in found.points:+ if spec.series:+ # One series per point: the other series columns stay null.+ row_out = {spec.x: p[spec.x], **dict.fromkeys(keys)}+ row_out[by_value[p["__series"]]] = p.get(spec.y[0])+ else:+ values = zip(keys, spec.measures, strict=True)+ row_out = {spec.x: p[spec.x], **{k: p.get(m) for k, m in values}}+ if label_key:+ row_out[label_key] = p[label_key]+ out.append(row_out)+ # Without aggregate each cell holds one row's value, read through "sum".+ aggregate = spec.aggregate or "sum"+ empty = 0 if spec.aggregate in _ADDITIVE else None+ for x_value, cells in found.groups.items():+ row_out = {spec.x: x_value}+ if spec.series:+ row_out.update(dict.fromkeys(keys, empty))+ for s_value, accs in cells.items():+ row_out[by_value[s_value]] = accs[0].value(aggregate)+ else:+ row_out.update(+ (k, acc.value(aggregate)) for k, acc in zip(keys, cells[None], strict=True)+ )+ out.append(row_out)+ return out+++def _total(row: Mapping[str, Any], keys: list[str]) -> float:+ return sum(v for k in keys if isinstance(v := row.get(k), (int, float)))+++def _sort(+ spec: ChartSpec, out: list[dict[str, Any]], keys: list[str], x_type: str, scatter: bool+) -> tuple[tuple[str, str] | None, str | None]:+ """Sort *out* in place; returns the sort used and the order a chart may claim.++ As asked; else grouped categories largest first and dates oldest first.+ """+ sort = spec.sort+ if sort is None and x_type == "time":+ sort = (spec.x, "asc")+ elif sort is None and spec.aggregate and not scatter:+ sort = ("__total", "desc")+ if sort is None:+ return None, None+ name, order = sort+ reverse = order == "desc"+ if name == spec.x:+ out.sort(key=lambda r: _x_order(r[spec.x]), reverse=reverse)+ return sort, None+ # Series columns sort by their row total; a measure by its own value.+ key = None if name == "__total" or spec.series else keys[spec.measures.index(name)]++ def by_value(r: Mapping[str, Any]) -> tuple[bool, float]:+ value = _total(r, keys) if key is None else r.get(key)+ if value is None:+ return (True, 0) # nulls last either way+ return (False, -value if reverse else value)++ out.sort(key=by_value)+ # A chart claims an order only by its series total, which a null has no place in.+ sums = key is None or len(keys) == 1+ claim = order if sums and all(r.get(k) is not None for r in out for k in keys) else None+ return sort, claim+++def transform(+ spec: ChartSpec, rows: Sequence[Mapping[str, Any]], types: Mapping[str, str]+) -> ChartRows:+ """Filter, group, sort and cut *rows* by *spec*, and pick the chart type.++ *types* comes from ``field_types`` and must already pass ``check_fields``.+ """+ x_type = types[spec.x]+ scatter = spec.kind == "scatter" or (+ spec.kind == "auto" and spec.aggregate is None and x_type == "number"+ )+ label_key = None+ if spec.aggregate is None and not spec.series:+ label_key = next(+ (k for k in _LABEL_FIELDS if k in types and k != spec.x and k not in spec.y), None+ )+ keep_points = spec.aggregate is None and (scatter or not spec.series)+ found = _collect(spec, rows, keep_points, label_key, scatter)+ keys, labels = _measure_columns(spec, found)+ columns = [(spec.x, label(spec.x)), *zip(keys, labels, strict=True)]+ if label_key:+ columns.insert(0, (label_key, label(label_key)))+ out = _chart_rows(spec, found, keys, label_key)+ sort, claim = _sort(spec, out, keys, x_type, scatter)++ # Top N, with the rest added up as Other; else the chart-point cap.+ total_rows = len(out)+ other = False+ if spec.top is not None and len(out) > spec.top:+ rest, out = out[spec.top :], out[: spec.top]+ if spec.other:+ bucket: dict[str, Any] = {spec.x: f"{OTHER_LABEL} ({len(rest)} more)"}+ for k in keys:+ values = [r[k] for r in rest if isinstance(r.get(k), (int, float))]+ bucket[k] = _round(sum(values)) if values else None+ out.append(bucket)+ # The bucket sits last whatever its size, so no order is claimed.+ other, claim, total_rows = True, None, len(out)+ cap = VIEW_LIMITS["chart_points"]+ if len(out) > cap:+ # A time axis keeps its newest points; anything else keeps its first.+ newest = x_type == "time" and (sort is None or sort[0] == spec.x)+ out = out[-cap:] if newest else out[:cap]++ chart_type, reason = _choose_kind(spec, x_type, keys, out, total_rows > len(out), scatter)+ return ChartRows(+ x=spec.x,+ x_type=x_type,+ columns=columns,+ series=keys,+ rows=out,+ chart_type=chart_type,+ reason=reason,+ total_rows=total_rows,+ matched=found.matched,+ source_rows=len(rows),+ missing_x=found.missing_x,+ sort=claim if chart_type != "scatter" else None,+ other=other,+ )+++# ---------------------------------------------------------------------------+# Chart type+# ---------------------------------------------------------------------------+++def _whole(keys: list[str], rows: list[dict[str, Any]]) -> bool:+ values = [r.get(k) for r in rows for k in keys]+ return all(isinstance(v, (int, float)) and v >= 0 for v in values) and sum(values) > 0+++def _choose_kind(+ spec: ChartSpec,+ x_type: str,+ keys: list[str],+ rows: list[dict[str, Any]],+ truncated: bool,+ scatter: bool,+) -> tuple[str, str]:+ """The chart type and why: the spec's ``kind``, checked, or one picked from the data's shape."""+ additive = spec.aggregate in _ADDITIVE or spec.aggregate is None+ low, high = VIEW_LIMITS["donut_min_parts"], VIEW_LIMITS["donut_max_parts"]+ donut_ok = len(keys) == 1 and low <= len(rows) <= high and not truncated and _whole(keys, rows)+ kind = spec.kind+ if kind == "donut" and not donut_ok:+ raise SpecError(+ f"a donut shows one measure as {low} to {high} non-negative parts of a whole, and this "+ f"chart has {len(rows)} rows and {len(keys)} series"+ f"{' with some cut' if truncated else ''}; "+ f"use top: {high - 1} with other: true, or kind bar"+ )+ if kind == "stacked_bar" and (len(keys) < 2 or not additive or not _whole(keys, rows)):+ raise SpecError(+ "a stacked_bar needs two or more series of non-negative counts or sums; "+ "pass series (a field to split by) or several y fields, or use kind bar"+ )+ if kind != "auto":+ return kind, f"kind {kind} was asked for"+ if scatter:+ return "scatter", f"x {spec.x!r} and the plotted values are both numeric"+ if x_type == "time":+ return "line", f"x {spec.x!r} holds dates"+ if spec.series and len(keys) > 1:+ if additive and _whole(keys, rows):+ return "stacked_bar", f"series {spec.series!r} splits each bar into parts"+ why = f"{spec.aggregate} does not add up"+ return "bar", f"series {spec.series!r} values sit side by side ({why})"+ if len(keys) > 1:+ return "bar", "several measures sit side by side"+ if spec.aggregate in _ADDITIVE and donut_ok:+ return "donut", f"{len(rows)} parts that add up to one whole"+ return "bar", "one measure across categories"+++def default_title(spec: ChartSpec) -> str:+ if spec.aggregate == "count":+ measure = "Count"+ else:+ names = ", ".join(label(y).lower() for y in spec.y)+ measure = (+ f"{_AGGREGATE_LABEL[spec.aggregate]} {names}" if spec.aggregate else names.capitalize()+ )+ by = label(spec.x).lower() + (f" and {label(spec.series).lower()}" if spec.series else "")+ return f"{measure} by {by}"diff --git a/packages/server/src/repowise/server/chat_views.py b/packages/server/src/repowise/server/chat_views.pyindex ecd1a4b34..18ffc5560 100644--- a/packages/server/src/repowise/server/chat_views.py+++ b/packages/server/src/repowise/server/chat_views.py@@ -28,6 +28,7 @@ from uuid import uuid4from pydantic import TypeAdapter, ValidationErrorfrom repowise.core.registry.plugins import SeamRegistry, load_plugins+from repowise.server import chat_view_specfrom repowise.server.chat_artifacts import ARTIFACT_ID_FIELDfrom repowise.server.chat_references import (LATEST_PREFIX,@@ -36,6 +37,8 @@ from repowise.server.chat_references import (ViewArtifact,stamp,)+from repowise.server.chat_view_spec import SpecError+from repowise.server.chat_view_spec import label as _labelfrom repowise.server.schemas.evidence import MAX_WIRE_NUMBERfrom repowise.server.schemas.views import (VIEW_LIMITS,@@ -55,6 +58,8 @@ _PERSON = re.compile(r"|reviewers?|maintainers?|contributors?|person|people|knowledge_map)(_|$)")_RISK_SCORE = re.compile(r"risk|impact_score|hotspot_score")+# Attribution figures are drawn only by views that check their coverage.+_ATTRIBUTION = re.compile(r"(^|_)(agents?|attested|provenance|attribution)(_|$)")_DEFAULT_ROWS = 50@@ -76,7 +81,9 @@ class ViewBuilder:The function returns ``kind``, ``title``, ``basis`` and the kind's fields;``build_view`` adds ``schema_version``, ``id``, ``source``, ``stale`` and- ``as_of``. ``artifact_types`` None means any artifact.+ ``as_of``. A ``receipt`` dict, if returned, goes to the model's+ ``show_view`` result and never to the part. ``artifact_types`` None means+ any artifact."""build: Callable[[ViewArtifact, Mapping[str, Any]], dict[str, Any]]@@ -189,7 +196,10 @@ def build_view(*,part_id: str,) -> dict[str, Any]:- """Build and validate one view part, or raise ``ViewError``."""+ """Build and validate one view part, or raise ``ViewError``.++ A builder's ``receipt`` comes back under ``"receipt"``; ``TurnViews`` pops it.+ """if view not in _view_names():raise ViewError(f"unknown view {view!r}; views for this artifact: {views_for(artifact.artifact_type)}",@@ -209,6 +219,7 @@ def build_view(raise ViewError(f"unknown options {unknown}; {view!r} takes {sorted(spec.options)}")part = spec.build(artifact, options)+ receipt = part.pop("receipt", None)meta = _meta(artifact.data)commit = meta.get("indexed_commit")part.update(@@ -236,7 +247,10 @@ def build_view(f"view {view!r} could not be built from this result: {exc.errors()[0]['msg']}",code="invalid_view",) from None- return _PART.dump_python(model, mode="json", exclude_unset=True)+ built = _PART.dump_python(model, mode="json", exclude_unset=True)+ if receipt:+ built["receipt"] = receipt+ return builtclass TurnViews:@@ -294,7 +308,7 @@ class TurnViews:def show(self, arguments: Mapping[str, Any]) -> dict[str, Any]:"""Run ``show_view``: a short receipt on success, an error payload otherwise."""try:- part = self._build(arguments)+ part, extra = self._build(arguments)except ViewError as exc:return exc.payload()except Exception:@@ -310,9 +324,9 @@ class TurnViews:receipt["source_tool"] = part["source"]["tool_name"]if "summary" in part:receipt["summary"] = part["summary"]- return receipt+ return {**receipt, **extra}- def _build(self, arguments: Mapping[str, Any]) -> dict[str, Any]:+ def _build(self, arguments: Mapping[str, Any]) -> tuple[dict[str, Any], dict[str, Any]]:reference = arguments.get("artifact_id")view = arguments.get("view")options = arguments.get("options") or {}@@ -328,13 +342,14 @@ class TurnViews:if not isinstance(view, str) or not isinstance(options, Mapping):raise ViewError("view must be a string and options an object", code="bad_arguments")part = build_view(artifact, view, options, part_id=f"v{len(self.parts) + 1}")+ extra = part.pop("receipt", None) or {}try:ViewPartList.model_validate([*self.parts, part])except ValidationError:raise ViewError(f"a turn holds at most {VIEW_LIMITS['parts']} views", code="view_limit") from None- return part+ return part, extradef event(self, tool_id: str, result: Mapping[str, Any]) -> dict[str, Any] | None:"""The SSE ``view`` payload for a successful ``show_view`` result."""@@ -359,7 +374,12 @@ def show_view_schema(artifact_types: list[str] | None = None) -> dict[str, Any]:"tool's newest result. Never restate the numbers yourself; the view ""carries them. Views: " + ", ".join(names) + ". ""`table` takes options path (dotted path to a list or map in the result), "- "columns and limit."+ "columns and limit. When no named view fits, `chart` plots any list in the "+ "result from options rows (path), x, y (field or list; omit to count rows), "+ "series, filter [{field, op: eq|ne|lt|lte|gt|gte|in|contains|prefix, value}], "+ "aggregate count|sum|avg|min|max, sort {field, order}, top, other (bucket the "+ "rest), kind auto|bar|stacked_bar|line|donut|scatter, title. Its receipt says "+ "the kind picked and why; an error lists the fields."),"parameters": {"type": "object",@@ -419,10 +439,6 @@ def _cell(key: str, value: Any) -> Any:return _wire(value)-def _label(key: str) -> str:- return key.replace("_", " ").capitalize()--def _node(data: Any, path: str) -> Any:node = datafor segment in path.split("."):@@ -457,27 +473,36 @@ def _table_paths(data: Mapping[str, Any], prefix: str = "", depth: int = 0) -> lreturn paths-@register_view_builder(- "table",- description="Any list or map of objects in a result, as a table.",- options={"path", "columns", "limit"},-)-def table_view(artifact: ViewArtifact, options: Mapping[str, Any]) -> dict[str, Any]:- path = options.get("path")+def _rows_at(+ data: Mapping[str, Any], path: Any, option: str+) -> tuple[str, list[dict[str, Any]], int]:+ """The list or map of objects at *path* (picked when the result holds one),+ its rows, and the row count the tool reports for it (``<leaf>_total``)."""if path is None:- candidates = _table_paths(artifact.data)+ candidates = _table_paths(data)if len(candidates) != 1:raise ViewError(- f"pass options.path; tables in this result: {candidates}", code="bad_path"+ f"pass options.{option}; tables in this result: {candidates}", code="bad_path")path = candidates[0]if not isinstance(path, str) or any(_PERSON.search(seg) for seg in path.split(".")):raise ViewError(f"views never group by person ({path!r})", code="person_grouping")- node = _node(artifact.data, path)- rows = _tabular(node)+ rows = _tabular(_node(data, path))if rows is None:raise ViewError(f"{path!r} is not a list or map of objects", code="bad_path")+ parent_path, _, leaf = path.rpartition(".")+ parent = _node(data, parent_path) if parent_path else data+ total = parent.get(f"{leaf}_total") if isinstance(parent, Mapping) else None+ return path, rows, total if isinstance(total, int) and total >= len(rows) else len(rows)++@register_view_builder(+ "table",+ description="Any list or map of objects in a result, as a table.",+ options={"path", "columns", "limit"},+)+def table_view(artifact: ViewArtifact, options: Mapping[str, Any]) -> dict[str, Any]:+ path, rows, total_rows = _rows_at(artifact.data, options.get("path"), "path")wanted = options.get("columns")if wanted is not None and not (isinstance(wanted, list) and all(isinstance(k, str) for k in wanted)@@ -501,10 +526,6 @@ def table_view(artifact: ViewArtifact, options: Mapping[str, Any]) -> dict[str,raise ViewError(f"{path!r} has no scalar fields to show", code="bad_path")shown = rows[: _limit(options)]- parent_path, _, leaf = path.rpartition(".")- parent = _node(artifact.data, parent_path) if parent_path else artifact.data- total = parent.get(f"{leaf}_total") if isinstance(parent, Mapping) else None- total_rows = total if isinstance(total, int) and total >= len(rows) else len(rows)return {"kind": "table","title": f"{artifact.tool_name}: {path}",@@ -517,6 +538,86 @@ def table_view(artifact: ViewArtifact, options: Mapping[str, Any]) -> dict[str,}+def _spec_error(exc: SpecError) -> ViewError:+ extra = {"fields": exc.fields} if exc.fields else {}+ return ViewError(str(exc), code=exc.code, **extra)+++@register_view_builder(+ "chart",+ description="A chart you specify over any list of objects in a result.",+ options=chat_view_spec.OPTIONS,+)+def chart_view(artifact: ViewArtifact, options: Mapping[str, Any]) -> dict[str, Any]:+ try:+ spec = chat_view_spec.parse_spec(options)+ except SpecError as exc:+ raise _spec_error(exc) from None+ path, rows, source_total = _rows_at(artifact.data, options.get("rows"), "rows")+ for name in spec.fields():+ if _PERSON.search(name):+ raise ViewError(+ f"views never group or filter by person ({name!r})", code="person_grouping"+ )+ for name in [*spec.fields(), *path.split(".")]:+ if _ATTRIBUTION.search(name):+ # Deliberate ceiling: no coverage check per row here, so these are+ # refused; changes.provenance draws them with their coverage.+ raise ViewError(+ f"attribution figures ({name!r}) are shown only by changes.provenance, "+ "which checks their coverage",+ code="attribution_figure",+ )+ types = {k: t for k, t in chat_view_spec.field_types(rows).items() if not _PERSON.search(k)}+ numeric_x = types.get(spec.x) == "number"+ split = [spec.series] if spec.series else []+ for name in [*spec.y, *split, *([spec.x] if numeric_x else [])]:+ if _RISK_SCORE.search(name):+ raise ViewError(+ f"a risk score is never a chart series ({name!r}); show it in a table",+ code="risk_score_series",+ )+ try:+ chat_view_spec.check_fields(spec, types)+ result = chat_view_spec.transform(spec, rows, types)+ except SpecError as exc:+ raise _spec_error(exc) from None++ def cell(key: str, value: Any) -> Any:+ value = _cell(key, value) if key not in result.series else _wire(value)+ return None if value is ... else value++ shown = f"{result.matched} of {result.source_rows}" if spec.filters else f"{result.source_rows}"+ summary = f"{shown} rows of {path}" + (f", grouped by {spec.x}" if spec.aggregate else "") + "."+ if result.missing_x:+ summary += f" {result.missing_x} rows without {spec.x} are left out."+ if result.other:+ summary += f" {chat_view_spec.OTHER_LABEL} adds up the rows past the top {spec.top}."+ if source_total > len(rows):+ summary += (+ f" The tool returned {len(rows)} of {source_total} rows; the chart covers those only."+ )+ part: dict[str, Any] = {+ "kind": "chart",+ "chart_type": result.chart_type,+ "title": spec.title or chat_view_spec.default_title(spec),+ "summary": f"{spec.subtitle} {summary}" if spec.subtitle else summary,+ "basis": _tool_basis(artifact),+ "x": result.x,+ "series": result.series,+ "data": {+ "columns": [{"key": k, "label": label} for k, label in result.columns],+ "rows": [{k: cell(k, v) for k, v in row.items()} for row in result.rows],+ "total_rows": result.total_rows,+ "truncated": result.total_rows > len(result.rows),+ },+ "receipt": {"chart_type": result.chart_type, "reason": result.reason},+ }+ if result.sort:+ part["sort"] = result.sort+ return part++def _chart(*,title: str,diff --git a/packages/server/src/repowise/server/schemas/views.py b/packages/server/src/repowise/server/schemas/views.pyindex f413ea2ce..669048912 100644--- a/packages/server/src/repowise/server/schemas/views.py+++ b/packages/server/src/repowise/server/schemas/views.py@@ -15,6 +15,7 @@ tests and the TypeScript validator's run.from __future__ import annotations+import mathfrom collections import Counterfrom itertools import pairwisefrom typing import Annotated, ClassVar, Literal, get_args@@ -32,7 +33,7 @@ from repowise.server.schemas.evidence import (VIEW_SCHEMA_VERSION = 1-ChartType = Literal["bar", "stacked_bar", "line", "donut"]+ChartType = Literal["bar", "stacked_bar", "line", "donut", "scatter"]CHART_TYPES: tuple[str, ...] = get_args(ChartType)ViewCoverageMetric = Literal["attribution", "test_reach"]@@ -146,6 +147,7 @@ _SERIES_BOUNDS: dict[str, tuple[int, int]] = {"line": (1, VIEW_LIMITS["chart_series"]),"stacked_bar": (2, VIEW_LIMITS["chart_series"]),"donut": (1, 1),+ "scatter": (1, VIEW_LIMITS["chart_series"]),}_PARTS_OF_A_WHOLE = frozenset({"stacked_bar", "donut"})@@ -186,6 +188,8 @@ class ChartView(ViewProvenance, ViewPartBase):for i, row in enumerate(rows):if row[self.x] is None:raise ValueError(f"rows[{i}]: x is null")+ if kind == "scatter" and not _finite_number(row[self.x]):+ raise ValueError(f"rows[{i}]: a scatter x is a number")for s in self.series:cell = row[s]if cell is None:@@ -215,6 +219,11 @@ class ChartView(ViewProvenance, ViewPartBase):return self+def _finite_number(cell: object) -> bool:+ """A number as the TypeScript ``isFiniteNumber`` sees one: no bools, no NaN or infinity."""+ return isinstance(cell, (int, float)) and not isinstance(cell, bool) and math.isfinite(cell)++def _row_total(row: dict[str, ViewCell], series: list[str]) -> float:"""A row's numeric series values summed in series order, as the TypeScript side sums."""total: float = 0diff --git a/packages/types/fixtures/views.conformance.json b/packages/types/fixtures/views.conformance.jsonindex cc2517dfb..5eade432c 100644--- a/packages/types/fixtures/views.conformance.json+++ b/packages/types/fixtures/views.conformance.json@@ -4,7 +4,7 @@"schema_version": 1,"max_wire_number": 9007199254740991,"view_kinds": ["table", "chart", "section"],- "chart_types": ["bar", "stacked_bar", "line", "donut"],+ "chart_types": ["bar", "stacked_bar", "line", "donut", "scatter"],"coverage_metrics": ["attribution", "test_reach"],"sort_orders": ["asc", "desc"],"basis_kinds": ["measured", "inferred", "declared", "unresolved"],@@ -168,6 +168,33 @@"x": "tier","series": ["files"]},+ "c5": {+ "schema_version": 1,+ "kind": "chart",+ "id": "c5",+ "title": "Complexity against churn",+ "summary": "40 rows of files.",+ "source": { "artifact_id": "a6", "tool_name": "get_health", "args_hash": "0123abcd" },+ "basis": { "kind": "measured", "source": "get_health" },+ "stale": false,+ "as_of": null,+ "chart_type": "scatter",+ "data": {+ "columns": [+ { "key": "file_path", "label": "File path" },+ { "key": "max_ccn", "label": "Max ccn" },+ { "key": "commits", "label": "Commits" }+ ],+ "rows": [+ { "file_path": { "type": "file", "id": "src/a.py" }, "max_ccn": 279, "commits": 41 },+ { "file_path": { "type": "file", "id": "src/b.py" }, "max_ccn": 12.5, "commits": null }+ ],+ "total_rows": 2,+ "truncated": false+ },+ "x": "max_ccn",+ "series": ["commits"]+ },"s1": {"schema_version": 1,"kind": "section",@@ -227,6 +254,7 @@{ "name": "chart points over the cap", "from": "c3", "grow": { "data.rows": 101 }, "set": { "data.total_rows": 101 }, "error": "a chart plots at most 100 rows" },{ "name": "donut with six parts", "from": "c4", "grow": { "data.rows": 6 }, "set": { "data.total_rows": 6 }, "error": "a donut has 2 to 5 parts" },{ "name": "donut over truncated data", "from": "c4", "set": { "data.total_rows": 10, "data.truncated": true }, "error": "a donut must show the whole" },+ { "name": "scatter with a text x", "from": "c5", "set": { "data.rows.0.max_ccn": "high" }, "error": "a scatter x is a number" },{ "name": "donut with a null part", "from": "c4", "set": { "data.rows.0.files": null }, "error": "a donut part is null" },{ "name": "empty section", "from": "s1", "set": { "narrative": [], "view_ids": [] }, "error": "a section needs narrative or views" },{ "name": "section repeating a view", "from": "s1", "set": { "view_ids": ["t1", "t1"] }, "error": "view.view_ids: duplicate t1" },@@ -251,7 +279,7 @@{ "name": "line chart with a null and no sort", "from": "c3", "set": { "data.rows.0.commits": null } }],"valid_lists": [- { "name": "every valid part together", "items": [{ "from": "t1" }, { "from": "c1" }, { "from": "c2" }, { "from": "c3" }, { "from": "c4" }, { "from": "s1" }] },+ { "name": "every valid part together", "items": [{ "from": "t1" }, { "from": "c1" }, { "from": "c2" }, { "from": "c3" }, { "from": "c4" }, { "from": "c5" }, { "from": "s1" }] },{ "name": "no parts", "items": [] }],"invalid_lists": [diff --git a/packages/types/src/views.ts b/packages/types/src/views.tsindex f197bfab5..9e48a0a0b 100644--- a/packages/types/src/views.ts+++ b/packages/types/src/views.ts@@ -26,7 +26,7 @@ export const VIEW_SCHEMA_VERSION = 1 as const;export const SHOW_VIEW_TOOL = "show_view";/** Chart forms. A new form is added with its builder, never as a general grammar. */-export const CHART_TYPES = ["bar", "stacked_bar", "line", "donut"] as const;+export const CHART_TYPES = ["bar", "stacked_bar", "line", "donut", "scatter"] as const;export type ChartType = (typeof CHART_TYPES)[number];/** What a coverage ratio measures. */@@ -124,7 +124,8 @@ export interface TableView extends ViewPartBase<"table">, ViewProvenance, ViewTa* A chart over its own table, which renderers also show as the data view.* `x` and every `series` entry are column keys; series values are numbers in* one shared unit. `sort` states the rows' order by the sum of their series- * values (absent means source order) and is checked, not trusted.+ * values (absent means source order) and is checked, not trusted. A scatter's+ * `x` is numeric too: each row is one point per series it has a value in.*/export interface ChartView extends ViewPartBase<"chart">, ViewProvenance {chart_type: ChartType;diff --git a/packages/ui/__tests__/chat/view-part.test.tsx b/packages/ui/__tests__/chat/view-part.test.tsxindex 3d628fe38..9fa4bca15 100644--- a/packages/ui/__tests__/chat/view-part.test.tsx+++ b/packages/ui/__tests__/chat/view-part.test.tsx@@ -24,6 +24,7 @@ const BAR = valid.c1 as ChartView;const STACKED = valid.c2 as ChartView;const LINE = valid.c3 as ChartView;const DONUT = valid.c4 as ChartView;+const SCATTER = valid.c5 as ChartView;const SECTION = valid.s1 as SectionView;const href = (ref: { type: string; id: string }) => `/repos/r1/${ref.type}/${ref.id}`;@@ -153,6 +154,50 @@ describe("ViewPartList", () => {expect(new Set(dashes).size).toBe(2);});+ it("stacks a stacked bar's segments end to end on one row", () => {+ render(<ViewPartList views={[STACKED]} />);++ const [first, second] = [...part("c2").querySelectorAll(".recharts-bar")].map(+ (bar) => bar.querySelector(".recharts-bar-rectangle path") as SVGPathElement,+ );+ const start = (el: SVGPathElement) => Number(/^M\s*([\d.]+)/.exec(el.getAttribute("d") ?? "")?.[1]);+ const width = (el: SVGPathElement) => Number(el.getAttribute("width"));+ expect(start(second!)).toBeCloseTo(start(first!) + width(first!), 1);+ });++ it("plots a scatter's numeric points, skipping nulls, with the table one toggle away", () => {+ render(<ViewPartList views={[SCATTER]} />);+ const figure = part("c5");++ expect(figure).toHaveAttribute("data-chart-type", "scatter");+ // Two rows, one with a null y: one point drawn.+ expect(figure.querySelectorAll(".recharts-scatter-symbol")).toHaveLength(1);+ fireEvent.click(within(figure).getByRole("button", { name: "Data" }));+ expect(within(figure).getAllByRole("row")).toHaveLength(3);+ });++ it("gives each scatter series its own marker shape in the plot and legend", () => {+ const split: ChartView = {+ ...SCATTER,+ id: "sc2",+ data: {+ ...SCATTER.data,+ columns: [...SCATTER.data.columns, { key: "fixes", label: "Fixes" }],+ rows: SCATTER.data.rows.map((row, i) => ({ ...row, commits: i === 0 ? 41 : null, fixes: i === 1 ? 6 : null })),+ },+ series: ["commits", "fixes"],+ };+ render(<ViewPartList views={[split]} />);+ const figure = part("sc2");++ const shapes = [...figure.querySelectorAll(".recharts-scatter-symbol path")].map((el) => el.getAttribute("d"));+ expect(shapes).toHaveLength(2);+ expect(new Set(shapes).size).toBe(2);+ const legend = within(figure).getByRole("list");+ expect(within(legend).getAllByRole("listitem")).toHaveLength(2);+ expect(legend.querySelectorAll("svg path")).toHaveLength(2);+ });+it("draws nothing for a donut with no series instead of failing", () => {render(<ViewPartList views={[{ ...DONUT, id: "d0", series: [] }]} />);diff --git a/packages/ui/src/chat/view-part.tsx b/packages/ui/src/chat/view-part.tsxindex d94c8e893..5418899bf 100644--- a/packages/ui/src/chat/view-part.tsx+++ b/packages/ui/src/chat/view-part.tsx@@ -19,9 +19,13 @@ import {Pie,PieChart,ResponsiveContainer,+ Scatter,+ ScatterChart,+ Symbols,Tooltip,XAxis,YAxis,+ type SymbolType,} from "recharts";import type { CardRef } from "@repowise-dev/types/cards";import type {@@ -52,9 +56,12 @@ const SERIES_FILLS = [] as const;/** Lines differ by dash as well as lightness, one pattern per fill. */const LINE_DASHES = ["", "6 3", "2 3", "8 3 2 3", "1 2"] as const;+/** Scatter series differ by marker shape the same way. */+const POINT_SHAPES: readonly SymbolType[] = ["circle", "square", "triangle", "diamond", "cross"];const seriesFill = (i: number): string => SERIES_FILLS[Math.min(i, SERIES_FILLS.length - 1)]!;const lineDash = (i: number): string => LINE_DASHES[Math.min(i, LINE_DASHES.length - 1)]!;+const pointShape = (i: number): SymbolType => POINT_SHAPES[Math.min(i, POINT_SHAPES.length - 1)]!;/** The series a chart draws; the rest stay in its data table. */const drawnSeries = (view: ChartView): string[] => view.series.slice(0, SERIES_FILLS.length);@@ -221,6 +228,10 @@ function ChartLegend({ view }: { view: ChartView }) {<svg aria-hidden width="16" height="6" className="shrink-0"><line x1="0" y1="3" x2="16" y2="3" stroke={item.fill} strokeWidth="2" strokeDasharray={lineDash(i)} /></svg>+ ) : view.chart_type === "scatter" ? (+ <svg aria-hidden width="10" height="10" className="shrink-0">+ <Symbols cx={5} cy={5} size={48} type={pointShape(i)} fill={item.fill} />+ </svg>) : (<span aria-hidden className="h-2.5 w-2.5 shrink-0 rounded-sm" style={{ background: item.fill }} />)}@@ -285,6 +296,48 @@ function ChartPlot({ view }: { view: ChartView }) {);}+ if (view.chart_type === "scatter") {+ // Both axes are numbers; each series plots the rows it has a value in.+ const plotted = series.map((key) => ({+ key,+ points: view.data.rows.flatMap((row) => {+ const x = row[view.x];+ const y = row[key];+ return typeof x === "number" && typeof y === "number" ? [{ x, y }] : [];+ }),+ }));+ return (+ <div style={{ height: PLOT_PX }}>+ <ResponsiveContainer width="100%" height="100%">+ <ScatterChart margin={{ top: 8, right: 12, bottom: 0, left: -12 }}>+ <CartesianGrid stroke="var(--color-border-default)" />+ <XAxis type="number" dataKey="x" name={labels.get(view.x) ?? view.x} tick={AXIS_TICK} axisLine={false} tickLine={false} />+ <YAxis+ type="number"+ dataKey="y"+ // A split's series are values of one field, so the tooltip calls them "Value".+ name={series.length === 1 ? (labels.get(series[0]!) ?? series[0]!) : "Value"}+ tick={AXIS_TICK}+ axisLine={false}+ tickLine={false}+ />+ <Tooltip cursor={{ strokeDasharray: "3 3" }} contentStyle={TOOLTIP_STYLE} />+ {plotted.map(({ key, points }, i) => (+ <Scatter+ key={key}+ name={labels.get(key) ?? key}+ data={points}+ fill={seriesFill(i)}+ shape={pointShape(i)}+ isAnimationActive={false}+ />+ ))}+ </ScatterChart>+ </ResponsiveContainer>+ </div>+ );+ }+if (view.chart_type === "line") {return (<div style={{ height: PLOT_PX }}>diff --git a/tests/unit/server/test_chat_view_spec.py b/tests/unit/server/test_chat_view_spec.pynew file mode 100644index 000000000..ee0e0f989--- /dev/null+++ b/tests/unit/server/test_chat_view_spec.py@@ -0,0 +1,322 @@+"""The ``chart`` view spec: parsing, field checks, filters, grouping, top + Other and the kind choice."""++from __future__ import annotations++from typing import Any++import pytest++from repowise.server.chat_view_spec import (+ OTHER_LABEL,+ SpecError,+ check_fields,+ default_title,+ field_types,+ parse_spec,+ transform,+)++FINDINGS = [+ {"file_path": "a.py", "kind": "unused_export", "lines": 10, "confidence": 0.9, "seen": "2026-03-02"},+ {"file_path": "b.py", "kind": "unused_export", "lines": 4, "confidence": 0.5, "seen": "2026-03-09"},+ {"file_path": "c.py", "kind": "unreachable_file", "lines": 120, "confidence": 0.7, "seen": "2026-03-02"},+ {"file_path": "d.ts", "kind": "zombie_package", "lines": 0, "confidence": 0.4, "seen": "2026-03-16"},+ {"file_path": "e.ts", "kind": "unused_export", "lines": 7, "confidence": None, "seen": "2026-03-16"},+]+++SIX = [*FINDINGS, {"file_path": "f.ts", "kind": "unused_export", "lines": 1, "confidence": 0.1, "seen": "2026-03-16"}]+++def _run(rows: list[dict[str, Any]] = FINDINGS, **options: Any):+ spec = parse_spec(options)+ types = field_types(rows)+ check_fields(spec, types)+ return transform(spec, rows, types)+++def _column(result, key: str) -> list:+ return [row[key] for row in result.rows]+++# ---------------------------------------------------------------------------+# Fields and validation+# ---------------------------------------------------------------------------+++def test_field_types_infer_number_time_and_text_and_skip_nested():+ rows = [+ {"n": 1, "when": "2026-03-02T09:00:00Z", "name": "x", "flag": True, "deep": {"a": 1}, "Bad": 1},+ {"n": 2.5, "when": "2026-03", "name": 3, "flag": None, "deep": None, "Bad": 2},+ ]+ assert field_types(rows) == {"n": "number", "when": "time", "name": "text", "flag": "text"}+++def test_an_unknown_field_suggests_close_matches_and_lists_the_fields():+ with pytest.raises(SpecError) as exc:+ _run(x="knid")+ assert "'kind'" in str(exc.value)+ assert exc.value.fields == {+ "file_path": "text",+ "kind": "text",+ "lines": "number",+ "confidence": "number",+ "seen": "time",+ }+++ ("options", "message"),+ [+ ({}, "pass x"),+ ({"x": "kind", "group_by": "seen"}, "same axis"),+ ({"x": "kind", "y": "kind"}, "x cannot also be a y field"),+ ({"x": "kind", "y": "lines", "aggregate": "median"}, "not one of"),+ ({"x": "kind", "y": "lines", "aggregate": "count"}, "drop y"),+ ({"x": "kind", "aggregate": "sum"}, "needs y"),+ ({"x": "kind", "y": "lines", "group_by": "kind"}, "group_by needs aggregate"),+ ({"x": "kind", "y": ["lines", "confidence"], "series": "seen"}, "y takes one field"),+ ({"x": "kind", "filter": [{"field": "lines", "op": "between", "value": 1}]}, "op 'between'"),+ ({"x": "kind", "filter": [{"field": "kind", "op": "in", "value": "a"}]}, "non-empty list"),+ ({"x": "kind", "filter": [{"field": "lines", "op": "gt", "value": True}]}, "number or text"),+ ({"x": "kind", "top": 0}, "top must be"),+ ({"x": "kind", "top": 100, "other": True}, "from 1 to 99"),+ ({"x": "kind", "other": True}, "needs top"),+ ({"x": "kind", "y": "lines", "aggregate": "avg", "top": 2, "other": True}, "count or sum"),+ ({"x": "kind", "kind": "pie"}, "kind 'pie'"),+ ({"x": "kind", "sort": {"field": "kind", "order": "up"}}, "asc"),+ ({"x": "kind", "title": ""}, "title must be text"),+ ],+)+def test_a_malformed_spec_says_what_to_change(options, message):+ with pytest.raises(SpecError) as exc:+ parse_spec(options)+ assert message in str(exc.value)+++ ("options", "message"),+ [+ ({"x": "kind", "y": "file_path", "aggregate": "sum"}, "y fields must be numeric"),+ ({"x": "kind", "y": "lines", "kind": "scatter"}, "scatter needs a numeric x"),+ ({"x": "kind", "sort": {"field": "lines"}}, "must be one of ['kind', 'count']"),+ ({"x": "kind", "filter": [{"field": "owner", "value": 1}]}, "filter field 'owner'"),+ ],+)+def test_a_spec_that_does_not_fit_the_rows_is_refused(options, message):+ with pytest.raises(SpecError) as exc:+ _run(**options)+ assert message in str(exc.value)+++# ---------------------------------------------------------------------------+# Filters, grouping and aggregates+# ---------------------------------------------------------------------------+++ ("flt", "kept"),+ [+ ({"field": "kind", "value": "unused_export"}, 3),+ ({"field": "kind", "op": "ne", "value": "unused_export"}, 2),+ ({"field": "lines", "op": "lt", "value": 7}, 2),+ ({"field": "lines", "op": "lte", "value": 7}, 3),+ ({"field": "lines", "op": "gt", "value": 7}, 2),+ ({"field": "lines", "op": "gte", "value": 120}, 1),+ ({"field": "kind", "op": "in", "value": ["zombie_package", "unreachable_file"]}, 2),+ ({"field": "file_path", "op": "contains", "value": ".ts"}, 2),+ ({"field": "file_path", "op": "prefix", "value": "a"}, 1),+ ({"field": "seen", "op": "gte", "value": "2026-03-09"}, 3),+ # A null and a type mismatch never pass an ordering filter.+ ({"field": "confidence", "op": "lt", "value": 1}, 4),+ ({"field": "lines", "op": "gt", "value": "3"}, 0),+ ],+)+def test_each_filter_op(flt, kept):+ if kept == 0:+ with pytest.raises(SpecError) as exc:+ _run(x="kind", filter=[flt])+ assert exc.value.code == "no_data"+ return+ assert _run(x="kind", filter=[flt]).matched == kept+++def test_count_by_category_is_largest_first():+ result = _run(x="kind")+ assert _column(result, "kind") == ["unused_export", "unreachable_file", "zombie_package"]+ assert _column(result, "count") == [3, 1, 1]+ assert result.sort == "desc"+ assert result.columns == [("kind", "Kind"), ("count", "Count")]+++ ("aggregate", "values"),+ [("sum", [21, 120, 0]), ("avg", [7.0, 120.0, 0.0]), ("min", [4, 120, 0]), ("max", [10, 120, 0])],+)+def test_aggregates_by_group(aggregate, values):+ result = _run(group_by="kind", y="lines", aggregate=aggregate, sort={"field": "kind", "order": "desc"})+ assert _column(result, "kind") == ["zombie_package", "unused_export", "unreachable_file"]+ assert _column(result, "lines") == [values[2], values[0], values[1]]+++def test_avg_skips_nulls_and_float_sums_lose_their_noise():+ rows = [{"k": "a", "v": 0.1}, {"k": "a", "v": 0.2}, {"k": "a", "v": None}]+ assert _column(_run(rows, x="k", y="v", aggregate="sum"), "v") == [0.3]+ assert _column(_run(rows, x="k", y="v", aggregate="avg"), "v") == [0.15]+++def test_series_splits_a_count_into_one_column_per_value():+ result = _run(x="seen", series="kind")+ assert result.series == ["s1", "s2", "s3"]+ assert [label for _, label in result.columns[1:]] == [+ "unused_export",+ "unreachable_file",+ "zombie_package",+ ]+ # Dates run oldest first; a missing combination counts zero.+ assert result.rows[0] == {"seen": "2026-03-02", "s1": 1, "s2": 1, "s3": 0}+ assert _column(result, "seen") == ["2026-03-02", "2026-03-09", "2026-03-16"]+++def test_too_many_series_values_is_refused():+ rows = [{"x": "a", "s": f"v{i}"} for i in range(9)]+ with pytest.raises(SpecError, match="at most 8"):+ _run(rows, x="x", series="s")+++def test_unaggregated_series_with_repeats_asks_for_an_aggregate():+ rows = [{"x": "a", "s": "p", "v": 1}, {"x": "a", "s": "p", "v": 2}]+ with pytest.raises(SpecError, match="add aggregate"):+ _run(rows, x="x", y="v", series="s")+++def test_a_repeated_x_without_aggregate_asks_for_one_outside_a_scatter():+ with pytest.raises(SpecError, match="x 'kind' repeats"):+ _run(x="kind", y="lines")+ # Points of a scatter may share an x.+ rows = [{"a": 1, "b": 2}, {"a": 1, "b": 3}]+ assert len(_run(rows, x="a", y="b").rows) == 2+++def test_an_unaggregated_pivot_leaves_missing_combinations_null():+ rows = [{"x": "a", "s": "p", "v": 1}, {"x": "b", "s": "q", "v": 2}]+ result = _run(rows, x="x", y="v", series="s")+ assert result.rows == [{"x": "a", "s1": 1, "s2": None}, {"x": "b", "s1": None, "s2": 2}]+++def test_bool_fields_group_and_split_as_text():+ rows = [{"ok": True, "k": "a"}, {"ok": False, "k": "a"}, {"ok": True, "k": "b"}]+ by_flag = _run(rows, x="ok")+ assert _column(by_flag, "ok") == [True, False] and _column(by_flag, "count") == [2, 1]+ split = _run(rows, x="k", series="ok")+ assert [label for _, label in split.columns[1:]] == ["true", "false"]+++def test_rows_without_x_are_counted_and_left_out():+ rows = [{"k": "a"}, {"k": None}, {"k": "a"}]+ result = _run(rows, x="k")+ assert result.missing_x == 1 and _column(result, "count") == [2]+++# ---------------------------------------------------------------------------+# Top, Other and caps+# ---------------------------------------------------------------------------+++def test_top_with_other_adds_up_the_rest():+ result = _run(x="file_path", y="lines", aggregate="sum", top=2, other=True)+ assert _column(result, "file_path") == ["c.py", "a.py", f"{OTHER_LABEL} (3 more)"]+ assert _column(result, "lines") == [120, 10, 11]+ # The Other row breaks the order, so no sort is claimed, and nothing is cut.+ assert result.sort is None and result.other+ assert result.total_rows == len(result.rows)+++def test_the_other_bucket_never_merges_with_a_real_other_category():+ rows = [{"k": "a"}, {"k": "a"}, {"k": "Other"}, {"k": "b"}, {"k": "c"}]+ result = _run(rows, x="k", top=2, other=True)+ assert _column(result, "k") == ["a", "Other", f"{OTHER_LABEL} (2 more)"]+ assert _column(result, "count") == [2, 1, 2]++[email protected]("x", ["seen", "lines"])+def test_other_needs_a_category_x(x):+ with pytest.raises(SpecError, match="other needs a category x"):+ _run(x=x, top=1, other=True)+++def test_top_without_other_is_truncated():+ result = _run(x="file_path", y="lines", aggregate="sum", top=2)+ assert len(result.rows) == 2 and result.total_rows == 5+++def test_a_chart_keeps_at_most_100_points_and_a_time_axis_its_newest():+ rows = [{"day": f"2026-{1 + i // 28:02d}-{1 + i % 28:02d}", "n": i} for i in range(150)]+ result = _run(rows, x="day", y="n")+ assert len(result.rows) == 100 and result.total_rows == 150+ assert result.rows[-1]["n"] == 149+ bars = _run([{"k": f"k{i}"} for i in range(150)], x="k")+ assert len(bars.rows) == 100 and bars.total_rows == 150+++def test_the_transform_is_one_pass_over_large_inputs():+ rows = [{"k": f"k{i % 7}", "v": i} for i in range(50_000)]+ result = _run(rows, x="k", y="v", aggregate="sum")+ assert sum(_column(result, "v")) == sum(range(50_000))+++# ---------------------------------------------------------------------------+# Kind+# ---------------------------------------------------------------------------+++ ("rows", "options", "kind"),+ [+ (FINDINGS, {"x": "seen"}, "line"),+ (FINDINGS, {"x": "kind"}, "donut"),+ (FINDINGS, {"x": "kind", "y": "lines", "aggregate": "sum"}, "donut"),+ (FINDINGS, {"x": "kind", "y": "lines", "aggregate": "avg"}, "bar"),+ (SIX, {"x": "file_path", "y": "lines", "aggregate": "sum"}, "bar"),+ (FINDINGS, {"x": "kind", "series": "seen"}, "stacked_bar"),+ (FINDINGS, {"x": "kind", "series": "seen", "y": "lines", "aggregate": "avg"}, "bar"),+ (FINDINGS, {"x": "file_path", "y": ["lines", "confidence"]}, "bar"),+ (FINDINGS, {"x": "lines", "y": "confidence"}, "scatter"),+ (FINDINGS, {"x": "kind", "kind": "line"}, "line"),+ ([{"k": f"k{i}"} for i in range(6)], {"x": "k"}, "bar"),+ ],+)+def test_auto_kind_follows_the_data_shape(rows, options, kind):+ result = _run(rows, **options)+ assert result.chart_type == kind+ assert result.reason+++def test_a_scatter_keeps_one_row_per_point_with_its_label():+ result = _run(x="lines", y="confidence")+ assert result.columns[0] == ("file_path", "File path")+ assert result.rows[0] == {"file_path": "a.py", "lines": 10, "confidence": 0.9}+ split = _run(x="lines", y="confidence", series="kind")+ assert len(split.rows) == 5+ assert split.rows[0] == {"lines": 10, "s1": 0.9, "s2": None, "s3": None}+++ ("options", "message"),+ [+ ({"x": "file_path", "kind": "donut"}, "top: 4 with other: true"),+ ({"x": "kind", "kind": "stacked_bar"}, "pass series"),+ ],+)+def test_an_asked_kind_the_data_cannot_take_is_refused_with_a_fix(options, message):+ with pytest.raises(SpecError, match=message):+ _run(SIX, **options)+++def test_default_titles_name_the_measure_and_axes():+ assert default_title(parse_spec({"x": "kind"})) == "Count by kind"+ assert (+ default_title(parse_spec({"x": "kind", "y": "lines", "aggregate": "sum", "series": "seen"}))+ == "Total lines by kind and seen"+ )+ assert default_title(parse_spec({"x": "lines", "y": "confidence"})) == "Confidence by lines"diff --git a/tests/unit/server/test_chat_views.py b/tests/unit/server/test_chat_views.pyindex 70c584027..6f7d6c710 100644--- a/tests/unit/server/test_chat_views.py+++ b/tests/unit/server/test_chat_views.py@@ -733,3 +733,85 @@ def test_query_keys_and_bearer_tokens_are_redacted(text, expected):from repowise.core.redaction import redact_secretsassert redact_secrets(text) == expected+++# ---------------------------------------------------------------------------+# chart: a spec over any list+# ---------------------------------------------------------------------------+++def _show_chart(data: dict, **options: Any) -> tuple[TurnViews, dict]:+ turn = TurnViews()+ stamped = turn.record("get_health", {}, data, artifact_type="health")+ return turn, turn.show(+ {"artifact_id": stamped[ARTIFACT_ID_FIELD], "view": "chart", "options": options}+ )+++def test_chart_draws_a_spec_and_its_receipt_names_the_kind_and_why():+ turn, receipt = _show_chart(DEAD_CODE, rows="tiers.medium.findings", x="file_path", y="lines")+ assert receipt["chart_type"] == "bar" and receipt["reason"]+ part = turn.parts[0]+ assert "receipt" not in part+ ViewPartList.model_validate(turn.parts)+ assert part["data"]["rows"] == [{"file_path": {"type": "file", "id": "x.py"}, "lines": 307}]+ assert part["title"] == "Lines by file path"+ assert part["as_of"] == "4c1e31fe3a9b"+++def test_chart_summary_says_when_the_tool_returned_only_part_of_a_list():+ data = {"files": [{"kind": "py", "n": 1}, {"kind": "ts", "n": 2}], "files_total": 9}+ _, receipt = _show_chart(data, x="kind", subtitle="Files by language.")+ assert receipt["chart_type"] == "donut"+ assert receipt["summary"].startswith("Files by language. 2 rows of files")+ assert "returned 2 of 9 rows" in receipt["summary"]+++def test_chart_errors_list_the_fields_without_people():+ _, result = _show_chart(HEALTH, rows="high_leverage_files", x="max_cnn")+ assert result["error_code"] == "bad_spec"+ assert "'max_ccn'" in result["error"]+ assert result["fields"] == {"file_path": "text", "score": "number", "max_ccn": "number"}+++ ("options", "code"),+ [+ ({"rows": "high_leverage_files", "x": "primary_owner"}, "person_grouping"),+ (+ {"rows": "high_leverage_files", "x": "file_path", "filter": [{"field": "primary_owner", "value": "S"}]},+ "person_grouping",+ ),+ ({"rows": "global_hotspots", "x": "file_path", "y": "hotspot_score"}, "risk_score_series"),+ ({"rows": "global_hotspots", "x": "hotspot_score", "y": "fix_count"}, "risk_score_series"),+ ({"rows": "changes", "x": "category", "series": "agent_label"}, "attribution_figure"),+ ({"rows": "nope", "x": "a"}, "bad_path"),+ ({"x": "a", "colour": "red"}, "view_unavailable"),+ ],+)+def test_chart_keeps_every_view_guard(options, code):+ data = {+ **HEALTH,+ **RISK,+ "changes": [{"category": "fix", "agent_label": "agent"}],+ }+ turn, result = _show_chart(data, **options)+ assert result["error_code"] == code+ assert turn.parts == []+++def test_chart_bool_x_values_are_text_cells():+ turn, _ = _show_chart({"rows": [{"ok": True}, {"ok": False}, {"ok": True}]}, x="ok")+ assert [row["ok"] for row in turn.parts[0]["data"]["rows"]] == ["true", "false"]+++def test_chart_never_splits_series_by_a_risk_score():+ data = {"rows": [{"file_path": "a", "risk_level": "high", "lines": 1}]}+ _, result = _show_chart(data, x="file_path", series="risk_level")+ assert result["error_code"] == "risk_score_series"+++def test_chart_series_split_is_still_risk_guarded_by_its_measure():+ data = {"rows": [{"file_path": "a", "tier": "x", "risk_score": 1}]}+ _, result = _show_chart(data, x="file_path", y="risk_score", series="tier", aggregate="sum")+ assert result["error_code"] == "risk_score_series"diff --git a/tests/unit/server/test_view_parts_contract.py b/tests/unit/server/test_view_parts_contract.pyindex a6b2178e4..4f1b8693e 100644--- a/tests/unit/server/test_view_parts_contract.py+++ b/tests/unit/server/test_view_parts_contract.py@@ -48,6 +48,14 @@ def _derive(case: dict[str, Any]) -> dict[str, Any]:return part[email protected]("x", [True, math.inf, math.nan])+def test_a_scatter_x_is_a_finite_number_as_typescript_checks(x: Any) -> None:+ part = copy.deepcopy(FIXTURE["valid"]["c5"])+ part["data"]["rows"][0]["max_ccn"] = x+ with pytest.raises(ValidationError):+ PART.validate_python(part)++def test_the_vocabulary_and_limits_are_shared() -> None:assert {"schema_version": views.VIEW_SCHEMA_VERSION,