diff --git a/docs/internals.md b/docs/internals.md index add55476..e9c3d602 100644 --- a/docs/internals.md +++ b/docs/internals.md @@ -29,6 +29,26 @@ first; read this only when changing Heart's own code. the bash equivalent (`heart_write_json` in `_common.sh`). Concurrent ticks must not corrupt `state.json`. +## Dashboard consumer contract + +Readiness emits additive `repository_reasons` with gate keys, plus a `penalties` +breakdown (count, weight, cap and deducted points). The dashboard projects these +into `sections[].entries`; `affects_release` comes from readiness, not a separate +dashboard policy. Legacy verdicts keep their original score and display an +unavailable breakdown until refreshed. + +Brain board consumers forward `blockers[].prompt` and `command` verbatim. +Checkout-behind prompts now request a clean canonical-main fast-forward followed +by a tick; dirty/wrong-branch observations use `fix dirty`, rather than `/bug`. +CI failures retain their existing bug route. No new blocker severity is added. +`fix_plan`, `penalties`, `vantage`, `devbox_observed` and section entries are +additive; existing `stale_plan` and `performance` consumers need no migration. + +Published dev-box entries use the existing path scrub on the complete entry, +including its evidence and prompt. Entries containing private local paths stay +local. Fresh observations retain their timestamp; expired observations remain +unobserved on the cloud board. + ## Repo structure ``` diff --git a/heart/dashboard.py b/heart/dashboard.py index a9d6bdd5..0aaf1efc 100644 --- a/heart/dashboard.py +++ b/heart/dashboard.py @@ -49,6 +49,7 @@ import json import os import pathlib +import shlex import sys from dataclasses import dataclass, field from typing import Any, Iterable, Sequence @@ -258,6 +259,7 @@ class Section: observed_ago: str | None = None # HTML-only projection built from measurement fields, never parsed prose. detail_html: list[str] = field(default_factory=list) + entries: list[dict] = field(default_factory=list) @dataclass @@ -291,6 +293,9 @@ class Board: # N. None when nothing is stale, or when no gap has a known remedy. stale_plan: dict | None = None fix_plan: dict | None = None + penalties: list[dict] | None = None + vantage: str = "local snapshot" + devbox_observed: str | None = None # --- verdict/state → glyph & colour maps ------------------------------------ @@ -583,85 +588,143 @@ def _ci_fragment(ci: dict) -> tuple[str, str] | None: return None -def _lib_row(name: str, body: dict, *, unobserved: Sequence[str]) -> tuple[str, str]: - """(state, one-line label) for a single library/workspace repo row.""" - frags: list[tuple[str, str]] = [] +def _repo_remedy(name: str, key: str) -> dict | None: + """Typed checkout remedies; canonical paths are resolved by the receiving chat.""" + if key == "lib_behind": + return {"label": "update the canonical checkout", "kind": "prompt", + "payload": (f"/health {name} is behind origin. Resolve its canonical main " + "checkout from the workspace repository manifest (not a task worktree). " + "Inspect branch, status and active claims. Only if clean on main, run " + "git -C pull --ff-only, then " + "pyauto-heart tick and pyauto-heart readiness. Preserve edits; " + "stop and diagnose divergence or an unexpected branch.")} + if key in ("lib_dirty", "lib_branch"): + return {"label": "inspect checkout changes", "kind": "command", + "payload": f"pyauto-heart fix dirty {shlex.quote(name)}"} + if key == "open_pr": + return {"label": "review the open PR", "kind": "prompt", + "payload": f"/health Review the aging open PRs for {name}; report CI and next steps. Do not merge or close without approval."} + if key == "ahead": + return {"label": "inspect unpublished commits", "kind": "prompt", + "payload": f"/health Inspect {name}'s ahead-of-origin commits and active task claims. Preserve commits; propose the next step before pushing or resetting anything."} + return None + + +def _repo_entries(name: str, body: dict, *, unobserved: Sequence[str], + repository_reasons: list[dict], ts: str = "") -> list[dict]: + """Observations retain their identity and the impact emitted by readiness.""" + impacts = [r for r in repository_reasons if r.get("repo") == name] + entries = [] + + def add(key, reason, state, source, evidence=None, action=None, impact=None): + matched = impact or next((r for r in impacts if r.get("key") == key), None) + if matched: + state = FAIL if matched["severity"] == "red" else WARN + sidecar = {"CI": "ci_status", "local checkout": "repo_state", "GitHub PRs": "open_prs"}.get(source) + observed_at = (body.get(sidecar) or {}).get("ts") if sidecar else None + entries.append({"id": f"{name}:{key}", "subject": name, "key": key, + "state": state, "reason": reason, "source": source, + "observed_at": observed_at or ts or None, "evidence": evidence, + "affects_release": bool(matched), + "release_severity": matched.get("severity") if matched else None, + "action": action}) + ci = body.get("ci_status") or {} - ci_frag = _ci_fragment(ci) - if ci_frag: - frags.append(ci_frag) + fragment = _ci_fragment(ci) + ci_impacts = [r for r in impacts if r.get("key") in ("lib_ci", "ws_ci", "lib_ci_unavailable")] + if ci_impacts: + for impact in ci_impacts: + workflow = impact.get("workflow") + detail = ((ci.get("workflows") or {}).get(workflow) or {}) if workflow else ci + url = detail.get("url") or ci.get("url") + key = impact["key"] + prompt = (f"/health Refresh CI evidence for {name}" if impact["severity"] == "stale" + else f"/bug Heart board: {name} {workflow or ci.get('workflow') or 'CI'} failing on main") + if url: + prompt += f" — failing run: {url}" + add(key + (":" + workflow if workflow else ""), impact["text"], WARN, + "CI", url, {"label": "inspect CI", "kind": "prompt", "payload": prompt}, impact) + elif fragment: + state, reason = fragment + # Non-gating failures remain advisory, never red release blockers. + add("ci", reason, WARN if state == FAIL else state, "CI", ci.get("url"), + {"label": "inspect CI", "kind": "prompt", + "payload": f"/health Inspect CI for {name}: {reason}"} if state != OK else None) if "repo_state" in unobserved: - frags.append((UNOBS, "repo state n/a here")) + add("checkout_unobserved", "repo state n/a here", UNOBS, "local checkout") else: rs = body.get("repo_state") or {} branch = rs.get("branch") - dirty_real = _as_int(rs.get("dirty_real", rs.get("dirty_files", 0))) + dirty = _as_int(rs.get("dirty_real", rs.get("dirty_files", 0))) + findings = [] if branch and branch != "main": - frags.append((FAIL, f"branch={branch}")) - if dirty_real: - frags.append((FAIL, f"dirty={dirty_real}")) - if _as_int(rs.get("ahead")): - frags.append((WARN, f"ahead={rs['ahead']}")) + findings.append(("lib_branch", f"branch={branch} (not main)")) + if dirty: + findings.append(("lib_dirty", f"dirty={dirty} uncommitted source change(s)")) if _as_int(rs.get("behind")): - frags.append((FAIL, f"behind={rs['behind']}")) + findings.append(("lib_behind", f"behind={rs['behind']} commit(s)")) + if _as_int(rs.get("ahead")): + findings.append(("ahead", f"ahead={rs['ahead']} commit(s)")) + for key, reason in findings: + add(key, reason, WARN, "local checkout", action=_repo_remedy(name, key)) pr = body.get("open_prs") or {} if _as_int(pr.get("open_count")): - n = _as_int(pr.get("open_count")) - age = _as_int(pr.get("max_age_days")) - if age >= 30: - frags.append((FAIL, f"PR×{n} ({age}d)")) - elif age >= 7: - frags.append((WARN, f"PR×{n} ({age}d)")) - else: - frags.append((INFO, f"PR×{n}")) - - if not frags: - return OK, "clean / nominal" - # State reflects the OBSERVED signals; an "n/a here" annotation never drags - # a row with a real green CI down to unobserved. A row that is *only* - # unobserved fragments stays unobserved. - observed = [s for s, _ in frags if s != UNOBS] - state = _worst(observed) if observed else UNOBS - label = " ".join(t for _, t in frags) - return state, label + n, age = _as_int(pr.get("open_count")), _as_int(pr.get("max_age_days")) + add("open_pr", f"PR×{n} ({age}d)", INFO, "GitHub PRs", + action=_repo_remedy(name, "open_pr") if age >= 7 else None) + if not entries: + add("nominal", "clean / nominal", OK, "snapshot") + return entries + + +def _lib_row(name: str, body: dict, *, unobserved: Sequence[str], + repository_reasons: list[dict] | None = None) -> tuple[str, str]: + if repository_reasons is None: + from heart import readiness + repository_reasons = readiness.compute({"repos": {name: body}})["repository_reasons"] + entries = _repo_entries(name, body, unobserved=unobserved, + repository_reasons=repository_reasons) + observed = [e["state"] for e in entries if e["state"] != UNOBS] + return (_worst(observed) if observed else UNOBS, + " ".join(e["reason"] for e in entries)) def _repo_section( - key: str, title: str, repos: dict, want_lib: bool, *, unobserved: Sequence[str] + key: str, title: str, repos: dict, want_lib: bool, *, unobserved: Sequence[str], + repository_reasons: list[dict] | None = None, ts: str = "", ) -> Section | None: - rows: list[tuple[str, str, str]] = [] # (state, name, label) - links: list[dict] = [] + if repository_reasons is None: + from heart import readiness + repository_reasons = readiness.compute({"repos": repos})["repository_reasons"] + rows, entries, links = [], [], [] for name, body in sorted(repos.items()): - if not isinstance(body, dict): - continue - if _is_library(name, body) != want_lib: + if not isinstance(body, dict) or _is_library(name, body) != want_lib: continue - # Workspaces are gated only for a handful of groups; skip ungrouped noise. if not want_lib and _repo_group(body) not in GATED_WORKSPACE_GROUPS: continue - state, label = _lib_row(name, body, unobserved=unobserved) - rows.append((state, name, label)) - # The way OUT of a red row: the failing run itself, plus a ready-made - # /bug prompt (rendered as the link's paired 📋 on the html surface). - ci = body.get("ci_status") or {} - if (state == FAIL and ci.get("url") - and str(ci.get("conclusion") or "") not in ("", "success")): - wf = ci.get("workflow") or "CI" - links.append({ - "label": f"{name} run", "url": str(ci["url"]), - "prompt": (f"/bug Heart board: {name} {wf} failing on main — " - f"failing run: {ci['url']}"), - }) + observations = _repo_entries(name, body, unobserved=unobserved, + repository_reasons=repository_reasons, ts=ts) + entries.extend(observations) + observed = [e["state"] for e in observations if e["state"] != UNOBS] + state = _worst(observed) if observed else UNOBS + rows.append((state, name, " ".join(e["reason"] for e in observations))) + for e in observations: + if e["state"] == FAIL and e.get("evidence"): + links.append({"label": f"{name} run", "url": e["evidence"], + "prompt": (e.get("action") or {}).get("payload")}) if not rows: return None - overall = _worst(s for s, _, _ in rows) - n_bad = sum(1 for s, _, _ in rows if s in (FAIL, WARN)) + entries.sort(key=lambda e: ({FAIL: 0, WARN: 1, INFO: 2, UNOBS: 3, OK: 4}[e["state"]], e["id"])) + n_bad = sum(1 for state, _, _ in rows if state in (FAIL, WARN)) summary = f"{len(rows)} repos" + (f", {n_bad} need attention" if n_bad else " nominal") - details = [f"{name:<26} {label}" for _, name, label in rows] - return Section(key=key, title=title, state=overall, summary=summary, - details=details, links=links[:4]) + if n_bad: + impacted = {e["subject"] for e in entries if e["affects_release"]} + summary += f" · {len(impacted)} affect release" + return Section(key, title, _worst(s for s, _, _ in rows), summary, + [f"{name:<26} {label}" for _, name, label in rows], + links=links[:4], entries=entries) def build_board( @@ -701,11 +764,19 @@ def build_board( stale_keys = [] sections: list[Section] = [] - - lib_sec = _repo_section("libraries", "Libraries", repos, True, unobserved=unobserved) + repository_reasons = verdict.get("repository_reasons") + if repository_reasons is None: + # Legacy snapshots have no typed repository reasons. Ask Heart's own + # gate, never invent a second library/workflow policy in the renderer. + from heart import readiness + repository_reasons = readiness.compute(snapshot)["repository_reasons"] + + lib_sec = _repo_section("libraries", "Libraries", repos, True, unobserved=unobserved, + repository_reasons=repository_reasons, ts=ts) if lib_sec: sections.append(lib_sec) - ws_sec = _repo_section("workspaces", "Workspaces", repos, False, unobserved=unobserved) + ws_sec = _repo_section("workspaces", "Workspaces", repos, False, unobserved=unobserved, + repository_reasons=repository_reasons, ts=ts) if ws_sec: sections.append(ws_sec) @@ -744,8 +815,31 @@ def build_board( ] action = ({"label": "triage the drift", "payload": "pyauto-heart fix drift"} if st in (FAIL, WARN) else None) + entries = [] + for category, findings in (("dirty", dirty), ("missing", missing), + ("orphan", orphans), ("canonical_dirty", canonical)): + for finding in findings: + path = finding.get("path") or "/".join(str(finding[k]) for k in + ("worktree", "repo") if finding.get(k)) or str(finding.get("task") or "unknown") + reason = { + "dirty": f"{finding.get('dirty_files', '?')} uncommitted file(s) in task worktree", + "missing": "claimed by the task ledger but absent on disk", + "orphan": "directory exists without an active task claim; inspect before cleanup", + "canonical_dirty": f"{finding.get('dirty_files', '?')} uncommitted file(s) in canonical checkout", + }[category] + entries.append({"id": f"drift:{category}:{path}", "subject": path, + "key": category, "reason": reason, "state": WARN, + "affects_release": False, "release_severity": None, + "source": "local worktree inventory", "observed_at": ts, + "evidence": path, + "action": {"kind": "prompt", "label": "triage drift", + "payload": f"/health Inspect worktree drift: {path} — {reason}. " + "Run pyauto-heart fix drift for context; reconcile active claims, " + "preserve user edits and obtain approval before cleanup."}}) + # Local details are uncapped; publication still scrubs private paths. + details = [f"{e['subject']}: {e['reason']}" for e in entries] sections.append(Section("worktree_drift", "Worktree drift", st, summary, - details, action=action)) + details, action=action, entries=entries)) # Script timing ---------------------------------------------------------- if "script_timing" in unobserved: @@ -1050,6 +1144,15 @@ def build_board( f"{len(uc['repos'])} repos clean (swept {uc.get('ts', '?')})", [])) sections = _devbox_enrich(sections, devbox, now) + for section in sections: + if section.key == "release_validation" and section.state in (FAIL, WARN): + section.action = {"label": "inspect release validation", "kind": "prompt", + "payload": "/release Review the current release-validation evidence and failing stages; " + "plan any repair or rehearsal through the existing gates. Do not publish a release."} + elif section.key == "test_run" and section.state in (FAIL, WARN): + section.action = {"label": "inspect workspace validation", "kind": "prompt", + "payload": "/health Inspect the latest workspace validation report, identify failures " + "and timeouts, and route confirmed repairs through /bug and start-dev."} board = Board( verdict=v, @@ -1061,9 +1164,14 @@ def build_board( yellow_reasons=yellow, sections=sections, stale_reasons=stale_reasons, - blockers=_structure_reasons(red, yellow, stale_reasons, repos, stale_keys), + blockers=_structure_reasons(red, yellow, stale_reasons, repos, stale_keys, + repository_reasons=repository_reasons), performance=performance, stale_plan=build_stale_plan(stale_reasons, stale_keys), + penalties=verdict.get("penalties"), + vantage="cloud snapshot" if unobserved else "local snapshot", + devbox_observed=(f"dev box last observed {format_age(_age_seconds(devbox.get('ts'), now))}" + if isinstance(devbox, dict) and devbox.get("ts") else None), ) board.fix_plan = build_fix_plan(board, snapshot, devbox=devbox) return board @@ -1113,6 +1221,11 @@ def build_fix_plan(board: Board, snapshot: dict | None = None, *, lines.extend(f" {detail}" for detail in section.details) if section.action: lines.append(f" existing action: {section.action.get('payload', '')}") + for entry in section.entries: + lines.append(f" {entry['subject']}: {entry['reason']} " + f"({'affects release' if entry['affects_release'] else 'advisory'})") + if entry.get("action"): + lines.append(f" next action: {entry['action']['payload']}") for link in section.links: lines.append(f" evidence: {link.get('url', '')}") if link.get("prompt"): @@ -1581,7 +1694,8 @@ def build_stale_plan(stale_reasons: list, stale_keys: list) -> dict | None: return {"count": len(stale_reasons), "command": command, "prompt": prompt} -def _reason_item(text: str, severity: str, repos: dict, key: str = "") -> dict: +def _reason_item(text: str, severity: str, repos: dict, key: str = "", + detail: dict | None = None) -> dict: """Structure one flat reason string into an actionable blocker. Reasons follow the ``": "`` convention (readiness.py), so @@ -1611,18 +1725,29 @@ def _reason_item(text: str, severity: str, repos: dict, key: str = "") -> dict: prompt = f"/bug Heart board: {text}" if run_url: prompt += f" — failing run: {run_url}" + remedy = _repo_remedy(repo or "", (detail or {}).get("key", "")) + if remedy: + # A checkout observation is not a CI failure even while CI runs. + run_url = None + if remedy["kind"] == "command": + command = remedy["payload"] + prompt = f"/health Inspect {repo}'s checkout using `{command}`; preserve user edits and review before changing it." + else: + prompt = remedy["payload"] return {"text": text, "severity": severity, "repo": repo, "repo_url": repo_url, "run_url": run_url, "prompt": prompt, "command": command} def _structure_reasons(red: list, yellow: list, stales: list, repos: dict, - stale_keys: list | None = None) -> list[dict]: + stale_keys: list | None = None, *, + repository_reasons: Sequence[dict] = ()) -> list[dict]: """Every reason as an actionable item. ``stale_keys`` rides index for index with ``stales`` (empty when the verdict predates them).""" keys = list(stale_keys or []) keys += [""] * (len(stales) - len(keys)) - items = [_reason_item(str(t), sev, repos) + typed = {r["text"]: r for r in repository_reasons} + items = [_reason_item(str(t), sev, repos, detail=typed.get(str(t))) for sev, texts in (("red", red), ("yellow", yellow)) for t in texts] items += [_reason_item(str(t), "stale", repos, k) for t, k in zip(stales, keys)] @@ -1659,6 +1784,9 @@ def _devbox_enrich( [str(x) for x in (d.get("details") or [])][:8], links=sec.links, action=sec.action, observed_ago=f"observed {ago} on the dev box", + entries=[{**e, "source": "published dev-box observation", + "observed_at": devbox.get("ts")} for e in d.get("entries", []) + if isinstance(e, dict)], )) else: out.append(Section( @@ -1680,7 +1808,7 @@ def render_readiness_block(verdict: dict[str, Any], *, quiet: bool = False) -> l reds = verdict.get("red_reasons") or [] yellows = verdict.get("yellow_reasons") or [] stales = verdict.get("stale_reasons") or [] - limit = 1 if quiet else 6 + limit = 1 if quiet else len(reds) + len(yellows) + len(stales) shown = 0 for r in reds: lines.append(" " + c_fail(f"✗ {r}")) @@ -1776,7 +1904,7 @@ def _md_prompts_block(items: list[dict], plan: dict | None = None) -> list[str]: lines += ["**Clear every gap in one go:**", "", "```", plan["prompt"], "```", ""] if plan.get("command"): lines += ["```", plan["command"], "```", ""] - for it in items[:6]: + for it in items: if it.get("command"): lines += ["```", it["command"], "```", ""] lines += ["```", it["prompt"], "```", ""] @@ -1795,11 +1923,12 @@ def _render_md(board: Board) -> str: + (" ⚠️ **stale — run `pyauto-heart tick`**" if board.stale else ""), "", ] - label, items = _shown_reasons(board) - if items: - lines.append(f"**{label}:** " + "; ".join(_md_reason(i) for i in items[:6])) - lines.append("") - lines += _md_prompts_block(items, board.stale_plan) + for severity, label in (("red", "Blockers"), ("yellow", "Warnings"), ("stale", "Evidence gaps")): + items = [b for b in board.blockers if b["severity"] == severity] + if items: + lines.append(f"**{label}:** " + "; ".join(_md_reason(i) for i in items)) + lines.append("") + lines += _md_prompts_block(items, board.stale_plan) lines += ["| | Check | Status |", "|--|--|--|"] for sec in board.sections: em = _STATE_MD[sec.state] @@ -1895,11 +2024,11 @@ def _html_reason(item: dict) -> str: table.board tr.ok td.dot::before{background:var(--ok)} table.board tr.warn td.dot::before{background:var(--warn)} table.board tr.fail td.dot::before{background:var(--bad)} -table.board tr.info td.dot::before{background:var(--accent)} +table.board tr.info td.dot::before{background:var(--muted)} table.board td.name{font-weight:600;white-space:nowrap} table.board tr.unobs td.name,table.board tr.unobs td.sum{color:var(--muted)} /* Details use readable prose; numeric fields get structural emphasis. */ -ul.det{margin:.6rem 0 0;padding-left:1.2rem;color:var(--text); +ul.det{margin:.6rem 0 0;padding-left:1.2rem;color:var(--fg); font-size:1rem;white-space:normal;line-height:1.65} ul.det li{margin:.5rem 0;overflow-wrap:anywhere} .ago{color:var(--muted)} @@ -1939,22 +2068,26 @@ def _html_reason(item: dict) -> str: """ _EXTRA_CSS += """ -h2,h3{color:var(--text);border-color:var(--line)} +h2,h3{color:var(--fg);border-color:var(--line)} h2::after{display:none} .lyric{display:inline-block;font-size:1.15rem;margin-bottom:.65rem} table.board td,table.board summary{font-size:1rem;line-height:1.55} table.board td.name{white-space:normal;overflow-wrap:anywhere} summary{cursor:pointer;overflow-wrap:anywhere;white-space:normal;padding:.6rem 0; - color:var(--text)} -summary:focus-visible,button:focus-visible{outline:3px solid var(--text);outline-offset:3px} + color:var(--fg)} +summary:focus-visible,button:focus-visible{outline:3px solid var(--fg);outline-offset:3px} body button.copy,body table button.copy{min-width:44px;min-height:44px; width:auto;height:auto;padding:.55rem .8rem;white-space:normal;font-size:1rem} .actions{display:flex;gap:.65rem;flex-wrap:wrap;margin:1rem 0} .actions button{font-weight:650} .prompt-fallback pre,#copy-fallback{white-space:pre-wrap;overflow-wrap:anywhere; max-height:24rem;overflow:auto;font-size:1rem;line-height:1.5;user-select:text} -.duration{font-weight:750;font-variant-numeric:tabular-nums;color:var(--text)} +.duration{font-weight:750;font-variant-numeric:tabular-nums;color:var(--fg)} .state-label{font-size:1rem;font-weight:600} +.entry.fail > b{color:var(--bad)} +.entry.warn > b{color:var(--warn)} +.entry.ok > b{color:var(--ok)} +.entry{color:var(--fg)} .hint,.ago{font-size:1rem} .reason-counts{font-weight:600} #copy-status{min-height:1.5em} @@ -1982,6 +2115,60 @@ def _html_reason(item: dict) -> str: """ +def _html_entries(entries: list[dict]) -> str: + attention, other = [], [] + for entry in entries: + e = lambda value: _html.escape(str(value or "")) + impact = ("Release blocker" if entry.get("release_severity") == "red" else + "Release evidence needed" if entry.get("release_severity") == "stale" else + "Release warning" if entry.get("affects_release") else "Advisory") + if entry.get("state") == OK: + impact = "Passing" + action = entry.get("action") or {} + button = (_copy_btn(action["payload"], action.get("label", "copy"), + "copy command" if action.get("kind") == "command" else "copy prompt") + if action.get("payload") else "") + evidence = entry.get("evidence") + link = (f' evidence ↗' + if isinstance(evidence, str) and evidence.startswith(("https://", "http://")) else "") + row = (f'
  • {e(entry.get("subject"))}' + f' · {impact}
    {e(entry.get("reason"))}{link} {button}' + f'
    Observation source{e(entry.get("source"))}' + f' · {e(entry.get("observed_at") or "time unknown")}
  • ') + (attention if entry.get("state") in (FAIL, WARN) else other).append(row) + if not attention: + return '
      ' + ''.join(other) + '
    ' + result = '
      ' + ''.join(attention[:3]) + '
    ' + for label, rows in (("more findings", attention[3:]), ("passing / informational observations", other)): + if rows: + result += (f'
    Show {len(rows)} {label}' + '
      ' + ''.join(rows) + '
    ') + return result + + +def _html_score(board: Board) -> str: + if board.penalties is None: + body = "This older verdict did not record a breakdown. Refresh Heart evidence to see it." + else: + labels = {"lib_ci": "Library CI failure", "lib_branch": "Library checkout on another branch", + "lib_dirty": "Uncommitted library source", "lib_behind": "Library checkout behind origin", + "ws_ci": "Required workspace CI failure", "validation_failed": "Release validation failed", + "validation_absent": "Missing release validation", "install_unknown": "Missing install verification", + "test_unknown": "Missing workspace test report", "test_failing": "Workspace validation not passing", + "open_pr": "Aging open PRs"} + rows = [] + for penalty in board.penalties: + key = str(penalty.get("key", "unknown")) + label = _html.escape(labels.get(key, key.replace("_", " "))) + rows.append(f"
  • {label}: −{_as_int(penalty.get('points'))} " + f"({_as_int(penalty.get('count'))} × {_as_int(penalty.get('weight'))}, " + f"cap {_as_int(penalty.get('cap'))})
  • ") + body = ("Start at 100; subtract these capped penalties, with a floor of 0. " + "The verdict is determined by the reasons, not the score.
      " + + ("".join(rows) or "
    • No penalties recorded.
    • ") + "
    ") + return f'
    Why this score: {board.score}/100{body}
    ' + + def _html_actions(board: Board) -> str: plans = [("Fix Heart systematically", board.fix_plan or build_fix_plan(board))] if board.stale_plan: @@ -2019,7 +2206,11 @@ def _render_html(board: Board) -> str: "copy command" if payload.startswith("pyauto-heart ") else "copy prompt") details = "" - if sec.details: + if sec.entries: + opened = " open" if sec.state in (FAIL, WARN) else "" + details = (f'{_html.escape(sec.title)} — ' + f'observations ({len(sec.entries)}){_html_entries(sec.entries)}') + elif sec.details: items = "".join(f"
  • {d}
  • " for d in (sec.detail_html or [_html.escape(d) for d in sec.details])) opened = " open" if sec.state in (FAIL, WARN) else "" @@ -2072,6 +2263,9 @@ def _render_html(board: Board) -> str: {board.score}snapshot {_html.escape(board.ts)} · {age} · markdown version{github_link}

    {stale_html} +

    {_html.escape(board.vantage)} · +{_html.escape(board.devbox_observed or ('no dev-box observation attached' if board.vantage == 'cloud snapshot' else 'observed on this dev box'))}

    +{_html_score(board)}

    {len(board.red_reasons)} release blockers · {len(board.yellow_reasons)} warnings · {len(board.stale_reasons)} evidence gaps

    {_html_actions(board)} @@ -2109,6 +2303,9 @@ def to_dict(board: Board) -> dict[str, Any]: # nothing is stale — a sibling board renders it, never re-derives it. "stale_plan": board.stale_plan, "fix_plan": board.fix_plan or build_fix_plan(board), + "penalties": board.penalties, + "vantage": board.vantage, + "devbox_observed": board.devbox_observed, "pages_url": PAGES_URL, "sections": [ { @@ -2120,6 +2317,7 @@ def to_dict(board: Board) -> dict[str, Any]: "links": s.links, "action": s.action, "observed_ago": s.observed_ago, + "entries": s.entries, } for s in board.sections ], diff --git a/heart/fix.py b/heart/fix.py index 945ef045..58e55ba2 100644 --- a/heart/fix.py +++ b/heart/fix.py @@ -120,7 +120,8 @@ def fix_dirty(repo: str) -> int: real = rs.get("real_files", []) or [] noise = rs.get("noise_files", []) or [] - if not real and not noise: + branch = rs.get("branch") + if not real and not noise and branch in (None, "", "main"): print(c_ok(f"'{repo}' has no dirty files"), file=sys.stderr) return 0 @@ -131,7 +132,11 @@ def fix_dirty(repo: str) -> int: print(c_info("# Paste/run this in a fresh Claude Code session:"), file=sys.stderr) print() print(f"# Clean up the dirty working tree in {repo}") - print(f"# Repo: {_workspace.canonical_root()}/{repo}") + print(f"# Repo: {_workspace.repo_path(_workspace.canonical_root(), repo)}") + if branch and branch != "main": + print(f"# Current branch: {branch} (expected main for the canonical checkout)") + print("Inspect active task claims and preserve this branch and its commits. " + "Propose how to restore the canonical checkout; do not switch or discard work blindly.") print() print("## Generated artifacts (regenerated by build/simulator runs)") print("# These are almost always safe to revert (`git checkout -- `)") diff --git a/heart/publish.py b/heart/publish.py index 90fe2a9b..056471ff 100644 --- a/heart/publish.py +++ b/heart/publish.py @@ -9,8 +9,8 @@ "observed Nh ago on the dev box", falling back to grey once the observation expires (``dashboard.DEVBOX_FRESH_SECONDS``). -Privacy: the distilled file carries section states, summaries, and detail -lines ONLY — and any detail line naming a local filesystem path (``/home/``, +Privacy: the distilled file carries section states, summaries, detail lines +and structured entries — any detail line or entry naming a local filesystem path (``/home/``, ``~``, the expanded home dir) is dropped before it leaves the machine. The repo is public; worktree names and repo names are fine, absolute local paths are not. ``tests/test_publish.py`` pins this. @@ -71,6 +71,13 @@ def build_devbox_board(snapshot: dict | None, verdict: dict | None) -> dict[str, "summary": sec.summary, "details": _scrub(sec.details)[:8], } + if sec.entries: + # Structured observations obey the same public-path boundary as + # plain detail lines, including paths inside prompts/evidence. + sections[sec.key]["entries"] = [ + entry for entry in sec.entries + if _scrub([json.dumps(entry, ensure_ascii=False)]) + ] return { "schema_version": DEVBOX_SCHEMA_VERSION, "ts": (snapshot or {}).get("ts") or "", diff --git a/heart/readiness.py b/heart/readiness.py index 9ca8c19a..2c304c49 100644 --- a/heart/readiness.py +++ b/heart/readiness.py @@ -264,6 +264,7 @@ def compute( # module docstring's "Profiles" section is the rule's home. na: list[str] = [] counts: dict[str, int] = {} + repository_reasons: list[dict[str, str]] = [] def hit(key: str, n: int = 1) -> None: counts[key] = counts.get(key, 0) + n @@ -274,6 +275,12 @@ def add_stale(msg: str, key: str) -> None: stale_keys.append(key) hit(key) + def repo_red(repo: str, key: str, text: str, workflow: str = "") -> None: + red.append(text) + hit(key) + repository_reasons.append({"repo": repo, "key": key, "text": text, + "severity": "red", "workflow": workflow}) + def scope_local(msg: str, key: str) -> None: """File a *gap* in dev-box-local evidence: stale on the default profile; out-of-scope (na, non-gating) under release-ci. Adverse @@ -297,22 +304,20 @@ def scope_local(msg: str, key: str) -> None: # silent unknown here is indistinguishable from a healthy repo. add_stale(f"{lib}: CI status unavailable ({ci['error']})", "lib_ci_unavailable") + repository_reasons.append({"repo": lib, "key": "lib_ci_unavailable", + "text": stale[-1], "severity": "stale"}) elif conclusion not in (None, "", "success"): - red.append(f"{lib}: CI {conclusion}") - hit("lib_ci") + repo_red(lib, "lib_ci", f"{lib}: CI {conclusion}") rs = body.get("repo_state", {}) or {} branch = rs.get("branch") if branch and branch != "main": - red.append(f"{lib}: on branch {branch} (not main)") - hit("lib_branch") + repo_red(lib, "lib_branch", f"{lib}: on branch {branch} (not main)") dirty_real = _as_int(rs.get("dirty_real", rs.get("dirty_files", 0))) if dirty_real > 0: - red.append(f"{lib}: {dirty_real} uncommitted source change(s)") - hit("lib_dirty") + repo_red(lib, "lib_dirty", f"{lib}: {dirty_real} uncommitted source change(s)") behind = _as_int(rs.get("behind", 0)) if behind > 0: - red.append(f"{lib}: {behind} commit(s) behind origin") - hit("lib_behind") + repo_red(lib, "lib_behind", f"{lib}: {behind} commit(s) behind origin") # --- workspace CI gate (RED) --- # Gate each gated workspace/howto repo on the conclusion of its REQUIRED @@ -335,14 +340,12 @@ def scope_local(msg: str, key: str) -> None: for wf in required: concl = (workflows.get(wf) or {}).get("conclusion") if concl in FAILURE_CONCLUSIONS: - red.append(f"{name}: {wf} {concl} on main") - hit("ws_ci") + repo_red(name, "ws_ci", f"{name}: {wf} {concl} on main", wf) else: # Pre-structured sidecar: fall back to the rolled-up conclusion. concl = ci.get("conclusion") if concl in FAILURE_CONCLUSIONS: - red.append(f"{name}: CI {concl} on main") - hit("ws_ci") + repo_red(name, "ws_ci", f"{name}: CI {concl} on main") # --- test-run gate (RED if false, YELLOW if unknown) --- test_run = snapshot.get("test_run") @@ -679,6 +682,8 @@ def scope_local(msg: str, key: str) -> None: if _as_int(pr.get("open_count", 0)) > 0 and _as_int(pr.get("max_age_days", 0)) >= 7: yellow.append(f"{name}: open PR {_as_int(pr.get('max_age_days'))}d old") hit("open_pr") + repository_reasons.append({"repo": name, "key": "open_pr", + "text": yellow[-1], "severity": "yellow"}) # --- release-ci: name the dev-box-local evidence this snapshot never # carried, so "out of scope" is a stated fact, not an assumption. Slices @@ -701,15 +706,21 @@ def scope_local(msg: str, key: str) -> None: # --- score --- score = 100 + penalties = [] for key, n in counts.items(): w, cap = _WEIGHTS.get(key, (0, 0)) - score -= min(n * w, cap) + points = min(n * w, cap) + score -= points + penalties.append({"key": key, "count": n, "weight": w, + "cap": cap, "points": points}) score = max(0, min(100, score)) verdict = "red" if red else ("yellow" if yellow else ("stale" if stale else "green")) return { "verdict": verdict, "score": score, + "penalties": penalties, + "repository_reasons": repository_reasons, "profile": profile, "reasons": red + yellow + stale, "red_reasons": red, diff --git a/tests/test_dashboard_reasons.py b/tests/test_dashboard_reasons.py new file mode 100644 index 00000000..1526eb16 --- /dev/null +++ b/tests/test_dashboard_reasons.py @@ -0,0 +1,159 @@ +"""Release impact comes from readiness; local advice never invents a gate.""" + +import datetime +import json + +from heart import dashboard, fix, publish, readiness + +TS = "2026-06-01T00:00:00+00:00" +NOW = datetime.datetime(2026, 6, 1, 0, 1, tzinfo=datetime.timezone.utc) + + +def repo(group, **checkout): + return {"ci_status": {"group": group, "conclusion": "success", "ts": TS}, + "repo_state": {"group": group, "branch": "main", **checkout}} + + +def board_for(repos, **slices): + snapshot = {"ts": TS, "repos": repos, **slices} + verdict = readiness.compute(snapshot, libraries=["LibA"], + required_workflows={"workspaces": ["Smoke Tests"]}) + return dashboard.build_board(snapshot, verdict, now=NOW), verdict + + +def entries(board, subject): + return [e for section in board.sections for e in section.entries if e["subject"] == subject] + + +def test_only_configured_library_checkout_is_release_red(): + b, v = board_for({"LibA": repo("libraries", behind=2), + "LibB": repo("libraries", behind=2), + "WorkspaceA": repo("workspaces", behind=2)}) + a, other, ws = [next(e for e in entries(b, n) if e["key"] == "lib_behind") + for n in ("LibA", "LibB", "WorkspaceA")] + assert a["state"] == "fail" and a["affects_release"] + assert all(e["state"] == "warn" and not e["affects_release"] for e in (other, ws)) + assert v["red_reasons"] == ["LibA: 2 commit(s) behind origin"] + blocker = next(e for e in b.blockers if e["severity"] == "red") + assert "pull --ff-only" in blocker["prompt"] and "/bug" not in blocker["prompt"] + assert "not a task worktree" in blocker["prompt"] + assert blocker["run_url"] is None + + +def test_workflow_impact_respects_required_list_and_pending_is_advisory(): + ws = repo("workspaces") + ws["ci_status"].update(conclusion="failure", workflows={ + "Smoke Tests": {"conclusion": "success"}, + "Optional": {"conclusion": "failure"}}) + b, _ = board_for({"WorkspaceA": ws}) + assert all(not e["affects_release"] and e["state"] != "fail" for e in entries(b, "WorkspaceA")) + ws["ci_status"]["workflows"]["Smoke Tests"] = { + "conclusion": "failure", "url": "https://example.invalid/smoke"} + b, _ = board_for({"WorkspaceA": ws}) + e = next(e for e in entries(b, "WorkspaceA") if e["state"] == "fail") + assert e["affects_release"] and e["evidence"].endswith("/smoke") + ws["ci_status"] = {"group": "workspaces", "conclusion": "in_progress"} + b, _ = board_for({"WorkspaceA": ws}) + assert all(e["state"] != "fail" and not e["affects_release"] for e in entries(b, "WorkspaceA")) + + +def test_old_pr_is_warning_not_failure_and_recent_pr_is_information(): + body = repo("libraries") + body["open_prs"] = {"open_count": 1, "max_age_days": 40} + b, _ = board_for({"LibA": body}) + e = next(e for e in entries(b, "LibA") if e["key"] == "open_pr") + assert e["state"] == "warn" and e["affects_release"] + body["open_prs"]["max_age_days"] = 2 + b, _ = board_for({"LibA": body}) + e = next(e for e in entries(b, "LibA") if e["key"] == "open_pr") + assert e["state"] == "info" and not e["affects_release"] + + +def test_dirty_and_branch_remedies_are_checkout_diagnostics(): + b, _ = board_for({"LibA": repo("libraries", branch="feature/work", dirty_real=2)}) + reasons = [r for r in b.blockers if r["severity"] == "red"] + assert len(reasons) == 2 + assert all(r["command"] == "pyauto-heart fix dirty LibA" for r in reasons) + assert all(r["prompt"].startswith("/health") for r in reasons) + + +def test_fix_dirty_on_clean_wrong_branch_emits_preserving_plan(monkeypatch, capsys, tmp_path): + monkeypatch.setattr(fix.state, "load", lambda: {"repos": {"LibA": repo("libraries", branch="work")}}) + monkeypatch.setattr(fix._workspace, "canonical_root", lambda: tmp_path) + monkeypatch.setattr(fix._workspace, "repo_path", lambda root, name: root / "group" / name) + assert fix.fix_dirty("LibA") == 0 + out = capsys.readouterr().out + assert "Current branch: work" in out and "preserve this branch" in out + assert str(tmp_path / "group" / "LibA") in out + + +def test_score_breakdown_reproduces_caps_and_floor(): + repos = {f"Lib{i}": repo("libraries", branch="work", dirty_real=3, behind=4) for i in range(5)} + v = readiness.compute({"ts": TS, "repos": repos}, libraries=list(repos)) + penalties = {p["key"]: p for p in v["penalties"]} + assert penalties["lib_behind"] == {"key": "lib_behind", "count": 5, "weight": 20, "cap": 40, "points": 40} + assert v["score"] == max(0, 100-sum(p["points"] for p in v["penalties"])) == 0 + html = dashboard.render({"ts": TS, "repos": repos}, v, fmt="html", now=NOW) + assert "Why this score: 0/100" in html and "floor of 0" in html + assert "Library checkout behind origin" in html + + +def test_old_score_snapshot_is_explicitly_unexplained(): + html = dashboard.render({"ts": TS}, {"verdict": "green", "score": 65}, fmt="html", now=NOW) + assert "older verdict did not record a breakdown" in html + + +def test_drift_categories_paths_and_publication_privacy(): + snapshot = {"ts": TS, "repos": {}, "worktree_drift": { + "dirty": [{"worktree": f"task{i}", "repo": "LibA", "dirty_files": 1} for i in range(12)], + "missing": [{"task": "missing", "path": "/home/user/private/missing"}], + "orphans": [{"path": "orphan-dir"}], + "canonical_dirty": [{"repo": "LibB", "dirty_files": 2}]}} + verdict = {"verdict": "green", "score": 100} + board = dashboard.build_board(snapshot, verdict, now=NOW) + drift = next(s for s in board.sections if s.key == "worktree_drift") + assert len(drift.entries) == 15 + assert {e["key"] for e in drift.entries} == {"dirty", "missing", "orphan", "canonical_dirty"} + assert all(not e["affects_release"] for e in drift.entries) + assert "/home/user/private/missing" in board.fix_plan["prompt"] + public = publish.build_devbox_board(snapshot, verdict) + assert "/home/" not in json.dumps(public) + cloud = dashboard.build_board({"ts": TS}, verdict, unobserved=dashboard.LOCAL_ONLY_FAMILIES, + devbox=public, now=NOW) + assert cloud.vantage == "cloud snapshot" and "dev box" in cloud.devbox_observed + imported = next(s for s in cloud.sections if s.key == "worktree_drift") + assert len(imported.entries) == 14 # private entry excluded; no display cap on others + assert all(e["source"] == "published dev-box observation" for e in imported.entries) + + +def test_full_text_surfaces_keep_all_tiers_but_brief_does_not(): + snapshot = {"ts": TS} + v = {"verdict": "red", "score": 0, "repository_reasons": [], + "red_reasons": [f"failure {i}" for i in range(10)], + "yellow_reasons": ["a warning"], "stale_reasons": ["a gap"]} + for fmt in ("md", "term"): + out = dashboard.render(snapshot, v, fmt=fmt, now=NOW) + assert all(text in out for text in ("failure 9", "a warning", "a gap")) + brief = dashboard.render(snapshot, v, fmt="md-brief", now=NOW) + assert "a warning" not in brief and "a gap" not in brief + + +def test_validation_actions_and_json_contract_remain_separate(): + b, v = board_for({}, test_run={"ready": False, "failed": 1}, + validation_report={"release_ready": False, "stages": {"integrate": {"status": "fail"}}}) + sections = {s.key: s for s in b.sections} + assert sections["test_run"].action["payload"].startswith("/health") + assert sections["release_validation"].action["payload"].startswith("/release") + payload = dashboard.to_dict(b) + assert payload["penalties"] == v["penalties"] + assert all("entries" not in blocker for blocker in payload["blockers"]) + assert {r["severity"] for r in payload["blockers"]} <= {"red", "yellow", "stale"} + + +def test_large_inventory_discloses_rest_without_losing_entries(): + rows = [{"subject": f"Repo{i}", "reason": "behind origin", "state": "warn", + "affects_release": False} for i in range(12)] + html = dashboard._html_entries(rows) + assert "Show 9 more findings" in html + assert html.count('class="entry warn"') == 12 + assert html.index("Repo2") < html.index("Show 9 more findings") < html.index("Repo3")