Spaces:
Paused
feat(ui): headless UI-verification harness (run-ui skill) + fix combobox JS (#33)
Browse filesAdds a reusable, deterministic UI check for the Clinical Trials tab so UI changes
stop shipping blind. Building it surfaced and fixed a real production bug.
- .claude/skills/run-ui/: launches the app in UI-smoke mode and drives the tab in
headless Chromium (Playwright), asserting placeholder, 3-char gate, clean picked
value, one-line labels, and a search result — plus a screenshot. Exits non-zero on
failure (CI-usable). SKILL.md documents usage + the gotchas learned.
- app.py: CANDLE_UI_SMOKE mode skips the heavy startup loads (cross-encoder, ChromaDB,
graph, Anthropic client) so UI tests boot in ~25s instead of ~60s.
- FIX: the combobox in-box placeholder + 3-char gate never ran — Gradio ignored
`js=`/`demo.load(js=)` in this version. Inject the script via gr.Blocks(head=...)
with a MutationObserver (the tab renders lazily). This was broken on the live Space
too; now verified working end-to-end.
- agents/research_agent.py: skip the cross-encoder load under CANDLE_UI_SMOKE.
- docs/ui-verification-todo.md: the full design→mock→implement→verify plan + status.
Co-authored-by: Claude Opus 4.8 <noreply@anthropic.com>
- .claude/skills/run-ui/SKILL.md +77 -0
- .claude/skills/run-ui/verify_ui.py +170 -0
- .gitignore +3 -0
- agents/research_agent.py +4 -2
- app.py +42 -19
- docs/ui-verification-todo.md +94 -0
|
@@ -0,0 +1,77 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
name: run-ui
|
| 3 |
+
description: Launch the Candle-Fire Gradio app in a headless browser and verify the Clinical Trials tab against deterministic UI checks (plus a screenshot). Use after any change to the Clinical Trials tab UI (app.py widgets, trials_query render, the combobox gate/placeholder JS/CSS) to confirm it renders and behaves correctly — instead of asking the user to restart and eyeball.
|
| 4 |
+
---
|
| 5 |
+
|
| 6 |
+
# run-ui — headless UI verification for the Clinical Trials tab
|
| 7 |
+
|
| 8 |
+
Gradio renders in the browser, so Python tests can't catch layout/JS regressions (wrapped
|
| 9 |
+
labels, a placeholder that never attaches, a broken 3-char gate, an eligibility block that
|
| 10 |
+
doesn't show). This skill drives the real app with Playwright + Chromium and asserts the
|
| 11 |
+
behaviors that have broken before.
|
| 12 |
+
|
| 13 |
+
## Prerequisites (one-time)
|
| 14 |
+
|
| 15 |
+
```bash
|
| 16 |
+
uv pip install playwright
|
| 17 |
+
uv run playwright install chromium # ~95MB; confirmed to work in this environment
|
| 18 |
+
```
|
| 19 |
+
|
| 20 |
+
## Run it
|
| 21 |
+
|
| 22 |
+
```bash
|
| 23 |
+
# Launch a fast UI-smoke instance (no heavy models) + verify + screenshot
|
| 24 |
+
uv run python .claude/skills/run-ui/verify_ui.py
|
| 25 |
+
|
| 26 |
+
# Verify an already-running instance instead of launching one
|
| 27 |
+
uv run python .claude/skills/run-ui/verify_ui.py --url http://127.0.0.1:7860/
|
| 28 |
+
|
| 29 |
+
# Options
|
| 30 |
+
# --port N port for the smoke launch (default 7899)
|
| 31 |
+
# --shot PATH screenshot destination (default docs/ui-mocks/clinical-trials-actual.png)
|
| 32 |
+
# --keep leave the launched app running
|
| 33 |
+
```
|
| 34 |
+
|
| 35 |
+
Exit code is **0 if all checks pass, non-zero otherwise** (CI-usable). Each check prints
|
| 36 |
+
`[PASS]`/`[FAIL]`. Always writes a full-page screenshot — **look at it**; a blank frame is a
|
| 37 |
+
failure to launch.
|
| 38 |
+
|
| 39 |
+
**Run it in the background** (`run_in_background`) and read the output file — the smoke launch
|
| 40 |
+
takes ~25s and Python buffers when piped. Don't foreground it behind a `| grep`.
|
| 41 |
+
|
| 42 |
+
## What it checks (Phase 3)
|
| 43 |
+
|
| 44 |
+
- facility & city comboboxes present
|
| 45 |
+
- in-box placeholder set on both (regressed once — see "Gotchas")
|
| 46 |
+
- 3-char gate: option list hidden at 2 chars, shown at 3
|
| 47 |
+
- picking a suggestion fills the CLEAN value (no "· N" count suffix leaking in)
|
| 48 |
+
- "Recruitment status" label renders on one line
|
| 49 |
+
- a search returns a result panel (eligibility/results/empty-hint)
|
| 50 |
+
|
| 51 |
+
Add a check by extending the `Checks` block in `verify_ui.py`. Target the comboboxes by
|
| 52 |
+
`#facility_combo` / `#city_combo` (their `elem_id`s); other widgets by role/text.
|
| 53 |
+
|
| 54 |
+
## How it works
|
| 55 |
+
|
| 56 |
+
- **UI-smoke mode** (`CANDLE_UI_SMOKE=1`, set by the launcher): `app.py` skips the heavy
|
| 57 |
+
startup loads (cross-encoder, ChromaDB, graph, Anthropic client) and renders the interface
|
| 58 |
+
with just the trials list. Boot ~25s (dominated by torch/chromadb imports) vs ~60s full.
|
| 59 |
+
Query features are inert in this mode; layout/search-over-trials still work.
|
| 60 |
+
- **Readiness = the port accepts a connection**, not a log line (the child's file-redirected
|
| 61 |
+
stdout is block-buffered, so "Running on local URL" can lag serving).
|
| 62 |
+
|
| 63 |
+
## Gotchas learned building this (don't relearn them)
|
| 64 |
+
|
| 65 |
+
- **Gradio `js=` / `demo.load(js=...)` did NOT execute** in this version. Inject browser JS via
|
| 66 |
+
`gr.Blocks(head="<script>…</script>")` instead — a real `<head>` script runs directly.
|
| 67 |
+
- The Clinical Trials tab **renders lazily** (inputs exist only after the tab is opened), so the
|
| 68 |
+
head script uses a **MutationObserver** to wire the combobox once it appears.
|
| 69 |
+
- Don't `stdout=PIPE` a long-running child and stop reading it — the pipe fills (~64KB) and
|
| 70 |
+
deadlocks the app mid-request. Log to a file.
|
| 71 |
+
- Gradio keeps a persistent connection, so `wait_until="networkidle"` may never settle — use
|
| 72 |
+
`"load"` and wait for `#facility_combo input`.
|
| 73 |
+
|
| 74 |
+
## Not yet built (see docs/ui-verification-todo.md)
|
| 75 |
+
|
| 76 |
+
Phase 4–5: describe → HTML mock → reference PNG → a `ui-reviewer` subagent that compares the
|
| 77 |
+
screenshot to the mock for subjective design fidelity. This skill is the deterministic gate.
|
|
@@ -0,0 +1,170 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Launch the app in UI-smoke mode and verify the Clinical Trials tab with a headless browser.
|
| 2 |
+
|
| 3 |
+
Deterministic checks for the things that have regressed before (placeholder, 3-char gate,
|
| 4 |
+
clean picked value, eligibility on recruiting trials, one-line filter labels), plus a
|
| 5 |
+
screenshot for eyeballing / visual-QA. Exits non-zero if any check fails, so it is CI-usable.
|
| 6 |
+
|
| 7 |
+
Usage:
|
| 8 |
+
uv run python .claude/skills/run-ui/verify_ui.py # launch smoke app + verify
|
| 9 |
+
uv run python .claude/skills/run-ui/verify_ui.py --url URL # verify an already-running app
|
| 10 |
+
uv run python .claude/skills/run-ui/verify_ui.py --keep # leave the launched app running
|
| 11 |
+
uv run python .claude/skills/run-ui/verify_ui.py --shot PATH # screenshot destination
|
| 12 |
+
|
| 13 |
+
Requires: playwright + chromium (`uv pip install playwright && uv run playwright install chromium`).
|
| 14 |
+
"""
|
| 15 |
+
from __future__ import annotations
|
| 16 |
+
|
| 17 |
+
import argparse
|
| 18 |
+
import os
|
| 19 |
+
import socket
|
| 20 |
+
import subprocess
|
| 21 |
+
import sys
|
| 22 |
+
import time
|
| 23 |
+
from pathlib import Path
|
| 24 |
+
|
| 25 |
+
ROOT = Path(__file__).resolve().parents[3]
|
| 26 |
+
|
| 27 |
+
|
| 28 |
+
def _launch_smoke(port: int, log_path: Path) -> subprocess.Popen:
|
| 29 |
+
"""Boot app.py in UI-smoke mode (no heavy models) on `port`; return the process.
|
| 30 |
+
|
| 31 |
+
Child stdout/stderr go to a LOG FILE, not a PIPE: once running, the app keeps logging,
|
| 32 |
+
and an unread PIPE fills its ~64KB buffer and deadlocks the app mid-request. Poll the file.
|
| 33 |
+
"""
|
| 34 |
+
subprocess.run(["bash", "-c", f"lsof -ti:{port} | xargs kill 2>/dev/null || true"], check=False)
|
| 35 |
+
env = {**os.environ, "CANDLE_UI_SMOKE": "1", "GRADIO_SERVER_PORT": str(port)}
|
| 36 |
+
log = open(log_path, "w")
|
| 37 |
+
proc = subprocess.Popen(
|
| 38 |
+
["uv", "run", "python", "app.py"], cwd=str(ROOT), env=env,
|
| 39 |
+
stdout=log, stderr=subprocess.STDOUT, text=True,
|
| 40 |
+
)
|
| 41 |
+
# Readiness = the PORT accepts a connection. (Don't grep the log for "Running on local URL":
|
| 42 |
+
# the child's file-redirected stdout is block-buffered, so that line can lag serving.)
|
| 43 |
+
deadline = time.time() + 120
|
| 44 |
+
while time.time() < deadline:
|
| 45 |
+
if proc.poll() is not None:
|
| 46 |
+
raise RuntimeError(f"app exited during startup (see {log_path})")
|
| 47 |
+
try:
|
| 48 |
+
with socket.create_connection(("127.0.0.1", port), timeout=1):
|
| 49 |
+
time.sleep(1.0) # let Gradio finish wiring routes
|
| 50 |
+
return proc
|
| 51 |
+
except OSError:
|
| 52 |
+
time.sleep(1.0)
|
| 53 |
+
raise TimeoutError(f"app did not become ready within 120s (see {log_path})")
|
| 54 |
+
|
| 55 |
+
|
| 56 |
+
class Checks:
|
| 57 |
+
def __init__(self) -> None:
|
| 58 |
+
self.failures: list[str] = []
|
| 59 |
+
|
| 60 |
+
def ok(self, cond: bool, label: str, detail: str = "") -> None:
|
| 61 |
+
mark = "PASS" if cond else "FAIL"
|
| 62 |
+
print(f" [{mark}] {label}" + (f" — {detail}" if detail and not cond else ""))
|
| 63 |
+
if not cond:
|
| 64 |
+
self.failures.append(label)
|
| 65 |
+
|
| 66 |
+
|
| 67 |
+
def verify(url: str, shot: str) -> int:
|
| 68 |
+
from playwright.sync_api import sync_playwright
|
| 69 |
+
|
| 70 |
+
c = Checks()
|
| 71 |
+
with sync_playwright() as p:
|
| 72 |
+
b = p.chromium.launch()
|
| 73 |
+
pg = b.new_page()
|
| 74 |
+
pg.set_viewport_size({"width": 1200, "height": 1000})
|
| 75 |
+
# 'load', not 'networkidle': Gradio keeps a persistent connection, so networkidle
|
| 76 |
+
# may never settle. Wait explicitly for the app shell instead.
|
| 77 |
+
pg.goto(url, wait_until="load", timeout=60000)
|
| 78 |
+
pg.get_by_role("tab", name="Clinical Trials").click()
|
| 79 |
+
pg.wait_for_selector("#facility_combo input", timeout=15000)
|
| 80 |
+
pg.wait_for_timeout(1000) # lazy tab render + MutationObserver wiring
|
| 81 |
+
|
| 82 |
+
print("Clinical Trials tab checks:")
|
| 83 |
+
fac = pg.query_selector("#facility_combo input")
|
| 84 |
+
city = pg.query_selector("#city_combo input")
|
| 85 |
+
c.ok(bool(fac) and bool(city), "facility & city comboboxes present")
|
| 86 |
+
|
| 87 |
+
# In-box placeholder hint (regressed once — demo.load ran before lazy tab render)
|
| 88 |
+
fac_ph = fac.get_attribute("placeholder") if fac else None
|
| 89 |
+
c.ok(bool(fac_ph), "facility placeholder set", f"got {fac_ph!r}")
|
| 90 |
+
c.ok(bool(city.get_attribute("placeholder") if city else None), "city placeholder set")
|
| 91 |
+
|
| 92 |
+
# 3-char gate: <3 chars keeps the option list hidden (.ac-hide), >=3 reveals it
|
| 93 |
+
if fac:
|
| 94 |
+
fac.click()
|
| 95 |
+
fac.fill("ma")
|
| 96 |
+
pg.wait_for_timeout(200)
|
| 97 |
+
hidden2 = pg.eval_on_selector("#facility_combo", "e => e.classList.contains('ac-hide')")
|
| 98 |
+
fac.fill("mas")
|
| 99 |
+
pg.wait_for_timeout(200)
|
| 100 |
+
hidden3 = pg.eval_on_selector("#facility_combo", "e => e.classList.contains('ac-hide')")
|
| 101 |
+
c.ok(hidden2 and not hidden3, "3-char gate (hidden at 2, shown at 3)",
|
| 102 |
+
f"hidden@2={hidden2} hidden@3={hidden3}")
|
| 103 |
+
|
| 104 |
+
# Picking a suggestion fills the CLEAN value (no "·"/count suffix leaking in)
|
| 105 |
+
if fac:
|
| 106 |
+
fac.fill("mass gen")
|
| 107 |
+
pg.wait_for_timeout(400)
|
| 108 |
+
opt = pg.query_selector("#facility_combo li, #facility_combo [role='option']")
|
| 109 |
+
if opt:
|
| 110 |
+
opt.click()
|
| 111 |
+
pg.wait_for_timeout(200)
|
| 112 |
+
val = fac.input_value()
|
| 113 |
+
c.ok("·" not in val and "site" not in val.lower(), "picked value is clean", f"got {val!r}")
|
| 114 |
+
else:
|
| 115 |
+
c.ok(False, "picked value is clean", "no option element found to click")
|
| 116 |
+
|
| 117 |
+
# One-line filter labels (the "Recruitment status" wrap fix)
|
| 118 |
+
rs_h = pg.evaluate(
|
| 119 |
+
"""() => {
|
| 120 |
+
const el = [...document.querySelectorAll('span,label')]
|
| 121 |
+
.find(e => e.textContent.trim() === 'Recruitment status');
|
| 122 |
+
return el ? el.getBoundingClientRect().height : null;
|
| 123 |
+
}"""
|
| 124 |
+
)
|
| 125 |
+
c.ok(rs_h is not None and rs_h < 28, "‘Recruitment status’ label on one line", f"height={rs_h}")
|
| 126 |
+
|
| 127 |
+
# Eligibility renders for recruiting trials after a search
|
| 128 |
+
pg.query_selector("#city_combo input") # ensure tab is settled
|
| 129 |
+
state = pg.query_selector("input[aria-label='State'], #component-state") # best-effort
|
| 130 |
+
# Simpler: search by facility we just picked (recruiting default) via the button
|
| 131 |
+
btn = pg.get_by_role("button", name="Search trials")
|
| 132 |
+
btn.click()
|
| 133 |
+
pg.wait_for_timeout(1500)
|
| 134 |
+
body = pg.inner_text("body")
|
| 135 |
+
c.ok("Eligibility" in body or "trial(s) matched" in body or "No " in body,
|
| 136 |
+
"search returns a result panel")
|
| 137 |
+
|
| 138 |
+
pg.screenshot(path=shot, full_page=True)
|
| 139 |
+
print(f"screenshot: {shot}")
|
| 140 |
+
b.close()
|
| 141 |
+
|
| 142 |
+
print(f"\n{'ALL CHECKS PASSED' if not c.failures else f'{len(c.failures)} CHECK(S) FAILED: ' + ', '.join(c.failures)}")
|
| 143 |
+
return 0 if not c.failures else 1
|
| 144 |
+
|
| 145 |
+
|
| 146 |
+
def main() -> int:
|
| 147 |
+
ap = argparse.ArgumentParser()
|
| 148 |
+
ap.add_argument("--url", default=None, help="Verify an already-running app instead of launching.")
|
| 149 |
+
ap.add_argument("--port", type=int, default=7899, help="Port for the smoke launch.")
|
| 150 |
+
ap.add_argument("--keep", action="store_true", help="Leave the launched app running.")
|
| 151 |
+
ap.add_argument("--shot", default=str(ROOT / "docs" / "ui-mocks" / "clinical-trials-actual.png"))
|
| 152 |
+
args = ap.parse_args()
|
| 153 |
+
|
| 154 |
+
Path(args.shot).parent.mkdir(parents=True, exist_ok=True)
|
| 155 |
+
proc = None
|
| 156 |
+
try:
|
| 157 |
+
url = args.url
|
| 158 |
+
if not url:
|
| 159 |
+
print(f"Launching smoke app on :{args.port} ...", flush=True)
|
| 160 |
+
proc = _launch_smoke(args.port, Path(args.shot).parent / "smoke_app.log")
|
| 161 |
+
url = f"http://127.0.0.1:{args.port}/"
|
| 162 |
+
print(f"ready at {url}", flush=True)
|
| 163 |
+
return verify(url, args.shot)
|
| 164 |
+
finally:
|
| 165 |
+
if proc and not args.keep:
|
| 166 |
+
proc.terminate()
|
| 167 |
+
|
| 168 |
+
|
| 169 |
+
if __name__ == "__main__":
|
| 170 |
+
raise SystemExit(main())
|
|
@@ -29,3 +29,6 @@ logs/
|
|
| 29 |
# IDE
|
| 30 |
.vscode/
|
| 31 |
.idea/
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
# IDE
|
| 30 |
.vscode/
|
| 31 |
.idea/
|
| 32 |
+
|
| 33 |
+
# UI verification artifacts (screenshots, smoke logs)
|
| 34 |
+
docs/ui-mocks/
|
|
@@ -2,6 +2,7 @@
|
|
| 2 |
from __future__ import annotations
|
| 3 |
|
| 4 |
import json
|
|
|
|
| 5 |
import re
|
| 6 |
from collections.abc import Generator
|
| 7 |
|
|
@@ -30,8 +31,9 @@ import trials_query
|
|
| 30 |
|
| 31 |
_logger = get_logger("agents.research_agent")
|
| 32 |
|
| 33 |
-
# Loaded once at startup — ~80MB model, ~80ms/pair on CPU
|
| 34 |
-
|
|
|
|
| 35 |
|
| 36 |
# Drug vocabulary is derived from the (startup-loaded) trials + graph; cache by identity
|
| 37 |
# so it is built once per session rather than on every query.
|
|
|
|
| 2 |
from __future__ import annotations
|
| 3 |
|
| 4 |
import json
|
| 5 |
+
import os
|
| 6 |
import re
|
| 7 |
from collections.abc import Generator
|
| 8 |
|
|
|
|
| 31 |
|
| 32 |
_logger = get_logger("agents.research_agent")
|
| 33 |
|
| 34 |
+
# Loaded once at startup — ~80MB model, ~80ms/pair on CPU. Skipped in UI smoke mode
|
| 35 |
+
# (CANDLE_UI_SMOKE=1), which renders the interface without query features.
|
| 36 |
+
_cross_encoder = None if os.getenv("CANDLE_UI_SMOKE") else CrossEncoder(CROSS_ENCODER_MODEL)
|
| 37 |
|
| 38 |
# Drug vocabulary is derived from the (startup-loaded) trials + graph; cache by identity
|
| 39 |
# so it is built once per session rather than on every query.
|
|
@@ -3,6 +3,7 @@ from __future__ import annotations
|
|
| 3 |
|
| 4 |
import html
|
| 5 |
import json
|
|
|
|
| 6 |
from pathlib import Path
|
| 7 |
|
| 8 |
import anthropic
|
|
@@ -96,12 +97,19 @@ def _ensure_data() -> None:
|
|
| 96 |
_logger.warning(f"Failed to download data from HF dataset: {e}")
|
| 97 |
|
| 98 |
|
| 99 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 100 |
|
| 101 |
-
|
| 102 |
-
|
|
|
|
|
|
|
|
|
|
| 103 |
_trials = _load_trials()
|
| 104 |
-
_client = anthropic.Anthropic()
|
| 105 |
|
| 106 |
_n_chunks = _collection.count() if _collection else 0
|
| 107 |
# Headline trial count = recruiting, interventional trials only (the actionable set), not the
|
|
@@ -226,27 +234,45 @@ _US_STATES = ["All"] + sorted(set(trials_query._STATE_ABBREV.values()))
|
|
| 226 |
_LOC_INDEX = trials_query.build_location_index(_trials)
|
| 227 |
_LOC_CHOICES = trials_query.location_choices(_LOC_INDEX)
|
| 228 |
|
| 229 |
-
#
|
| 230 |
-
#
|
| 231 |
-
#
|
| 232 |
-
#
|
| 233 |
-
|
| 234 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 235 |
const MIN = {trials_query.MIN_AUTOCOMPLETE_CHARS};
|
|
|
|
|
|
|
|
|
|
|
|
|
| 236 |
const gate = (id, hint) => {{
|
| 237 |
const root = document.getElementById(id);
|
| 238 |
if (!root) return;
|
| 239 |
const input = root.querySelector('input');
|
| 240 |
-
if (!input) return;
|
| 241 |
-
|
|
|
|
| 242 |
const apply = () => root.classList.toggle('ac-hide', input.value.trim().length < MIN);
|
| 243 |
input.addEventListener('input', apply);
|
| 244 |
input.addEventListener('focus', apply);
|
| 245 |
apply();
|
| 246 |
}};
|
| 247 |
-
|
| 248 |
-
|
| 249 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 250 |
"""
|
| 251 |
|
| 252 |
|
|
@@ -288,7 +314,7 @@ def _search_trials(facility: str, state: str, city: str, study_type: str, status
|
|
| 288 |
return trials_query.render_trials_html(enriched, len(matches))
|
| 289 |
|
| 290 |
|
| 291 |
-
with gr.Blocks(title="Candle-Fire — ALS Research Intelligence") as demo:
|
| 292 |
|
| 293 |
with gr.Tabs():
|
| 294 |
|
|
@@ -460,9 +486,6 @@ with gr.Blocks(title="Candle-Fire — ALS Research Intelligence") as demo:
|
|
| 460 |
msg_box.submit(**submit_kwargs)
|
| 461 |
send_btn.click(**submit_kwargs)
|
| 462 |
|
| 463 |
-
# Gate the combobox autocomplete lists to open only after MIN_AUTOCOMPLETE_CHARS.
|
| 464 |
-
demo.load(js=_AUTOCOMPLETE_GATE_JS)
|
| 465 |
-
|
| 466 |
|
| 467 |
if __name__ == "__main__":
|
| 468 |
demo.launch(
|
|
|
|
| 3 |
|
| 4 |
import html
|
| 5 |
import json
|
| 6 |
+
import os
|
| 7 |
from pathlib import Path
|
| 8 |
|
| 9 |
import anthropic
|
|
|
|
| 97 |
_logger.warning(f"Failed to download data from HF dataset: {e}")
|
| 98 |
|
| 99 |
|
| 100 |
+
# UI smoke mode (CANDLE_UI_SMOKE=1): render the Blocks WITHOUT the heavy startup loads
|
| 101 |
+
# (cross-encoder, ChromaDB, graph, Anthropic client) so UI/layout tests boot in seconds.
|
| 102 |
+
# The Clinical Trials tab only needs the trials list, which loads fast. Query features are
|
| 103 |
+
# inert in this mode — it exists purely to render and drive the interface.
|
| 104 |
+
_SMOKE = bool(os.getenv("CANDLE_UI_SMOKE"))
|
| 105 |
|
| 106 |
+
if not _SMOKE:
|
| 107 |
+
_ensure_data()
|
| 108 |
+
|
| 109 |
+
_collection = None if _SMOKE else _load_collection()
|
| 110 |
+
_graph = None if _SMOKE else _load_graph()
|
| 111 |
_trials = _load_trials()
|
| 112 |
+
_client = None if _SMOKE else anthropic.Anthropic()
|
| 113 |
|
| 114 |
_n_chunks = _collection.count() if _collection else 0
|
| 115 |
# Headline trial count = recruiting, interventional trials only (the actionable set), not the
|
|
|
|
| 234 |
_LOC_INDEX = trials_query.build_location_index(_trials)
|
| 235 |
_LOC_CHOICES = trials_query.location_choices(_LOC_INDEX)
|
| 236 |
|
| 237 |
+
# Combobox behavior that Gradio can't express natively, wired in JS:
|
| 238 |
+
# - an in-box placeholder hint (gr.Dropdown has no `placeholder` param), and
|
| 239 |
+
# - hiding the attached option list until MIN_AUTOCOMPLETE_CHARS (a `.ac-hide` class the
|
| 240 |
+
# CSS in _CSS acts on).
|
| 241 |
+
# Injected via gr.Blocks(head=...) as a real <script> — Gradio's js=/demo.load(js=) callbacks
|
| 242 |
+
# did not execute in this version, but a <head> script runs directly in the browser. The
|
| 243 |
+
# Clinical Trials tab renders LAZILY (inputs appear only when the tab is first opened), so a
|
| 244 |
+
# MutationObserver re-runs the wiring as the DOM changes and marks each input done, attaching
|
| 245 |
+
# whenever the tab renders. Degrades gracefully — if the dropdown DOM differs, the combobox
|
| 246 |
+
# still filters from the first character.
|
| 247 |
+
_AUTOCOMPLETE_GATE_HEAD = f"""
|
| 248 |
+
<script>
|
| 249 |
+
(function() {{
|
| 250 |
const MIN = {trials_query.MIN_AUTOCOMPLETE_CHARS};
|
| 251 |
+
const targets = {{
|
| 252 |
+
'facility_combo': 'Type \\u22653 letters, e.g. Mass General',
|
| 253 |
+
'city_combo': 'Type \\u22653 letters, busiest cities first',
|
| 254 |
+
}};
|
| 255 |
const gate = (id, hint) => {{
|
| 256 |
const root = document.getElementById(id);
|
| 257 |
if (!root) return;
|
| 258 |
const input = root.querySelector('input');
|
| 259 |
+
if (!input || input.dataset.acReady) return; // not rendered yet, or already wired
|
| 260 |
+
input.dataset.acReady = '1';
|
| 261 |
+
if (hint) input.setAttribute('placeholder', hint);
|
| 262 |
const apply = () => root.classList.toggle('ac-hide', input.value.trim().length < MIN);
|
| 263 |
input.addEventListener('input', apply);
|
| 264 |
input.addEventListener('focus', apply);
|
| 265 |
apply();
|
| 266 |
}};
|
| 267 |
+
const applyAll = () => Object.entries(targets).forEach(([id, h]) => gate(id, h));
|
| 268 |
+
const start = () => {{
|
| 269 |
+
applyAll();
|
| 270 |
+
new MutationObserver(applyAll).observe(document.body, {{childList: true, subtree: true}});
|
| 271 |
+
}};
|
| 272 |
+
if (document.body) start();
|
| 273 |
+
else document.addEventListener('DOMContentLoaded', start);
|
| 274 |
+
}})();
|
| 275 |
+
</script>
|
| 276 |
"""
|
| 277 |
|
| 278 |
|
|
|
|
| 314 |
return trials_query.render_trials_html(enriched, len(matches))
|
| 315 |
|
| 316 |
|
| 317 |
+
with gr.Blocks(title="Candle-Fire — ALS Research Intelligence", head=_AUTOCOMPLETE_GATE_HEAD) as demo:
|
| 318 |
|
| 319 |
with gr.Tabs():
|
| 320 |
|
|
|
|
| 486 |
msg_box.submit(**submit_kwargs)
|
| 487 |
send_btn.click(**submit_kwargs)
|
| 488 |
|
|
|
|
|
|
|
|
|
|
| 489 |
|
| 490 |
if __name__ == "__main__":
|
| 491 |
demo.launch(
|
|
@@ -0,0 +1,94 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# TODO — UI design→mock→implement→verify loop
|
| 2 |
+
|
| 3 |
+
> **Status (in progress):** Phases 0–3 + 6 built and green — `.claude/skills/run-ui/` launches
|
| 4 |
+
> the app in UI-smoke mode, drives the Clinical Trials tab in headless Chromium, and asserts the
|
| 5 |
+
> key behaviors (all PASS). Building it caught a real bug: the combobox placeholder + 3-char gate
|
| 6 |
+
> **never ran** (Gradio ignored `js=`/`demo.load(js=)`) — fixed by injecting the script via
|
| 7 |
+
> `gr.Blocks(head=...)`. Still to do: Phase 1 (<5s smoke boot — currently ~25s), Phases 4–5
|
| 8 |
+
> (design-mock front half + visual-QA agent).
|
| 9 |
+
|
| 10 |
+
**Gap:** UI changes can't be verified from the agent's environment (no browser tooling), so
|
| 11 |
+
fixes ship "blind" and rely on the user restarting the app and eyeballing. We also want the
|
| 12 |
+
front half: describe a change → see a visual mock → iterate → implement → auto-verify against
|
| 13 |
+
that mock.
|
| 14 |
+
|
| 15 |
+
**Two reframes that make this real (read first):**
|
| 16 |
+
1. **The "mock" is HTML, not a hand-painted PNG.** Claude can't render a raster image from a
|
| 17 |
+
description, but it can generate an HTML mock that renders visually and *exports* to PNG.
|
| 18 |
+
The PNG is the **reference snapshot** the automated test diffs the real app against.
|
| 19 |
+
2. **Gradio owns the DOM.** The Clinical Trials tab is `gr.Dropdown`/`gr.Row`/etc.; Gradio
|
| 20 |
+
generates the markup. So "mock → code" here means **mock → Gradio components + targeted
|
| 21 |
+
CSS/JS hooks**, not arbitrary DOM/JS. Fidelity is "close," not pixel-perfect. (Pixel control
|
| 22 |
+
would require moving the UI to a raw-HTML/React custom component — a much bigger change,
|
| 23 |
+
out of scope unless we decide otherwise.)
|
| 24 |
+
|
| 25 |
+
Sequence deliberately: prove the risky/automated half (Phases 0–3) before investing in the
|
| 26 |
+
design front half (Phases 4–5). Stop and report if Phase 0 fails.
|
| 27 |
+
|
| 28 |
+
---
|
| 29 |
+
|
| 30 |
+
## Phase 0 — Feasibility spike (DO FIRST; may kill the automated half)
|
| 31 |
+
- [ ] Confirm Playwright + Chromium can install in the agent environment
|
| 32 |
+
(`uv pip install playwright && playwright install chromium`); note if network-restricted.
|
| 33 |
+
- [ ] Boot the app on a test port and confirm a headless Chromium can reach it
|
| 34 |
+
(navigate to `http://127.0.0.1:<port>`, read the page title).
|
| 35 |
+
- [ ] **If blocked:** stop; switch to the fallback (user runs the browser, agent drives via a
|
| 36 |
+
script the user executes). Record the decision here.
|
| 37 |
+
|
| 38 |
+
## Phase 1 — Fast "UI smoke" launch mode
|
| 39 |
+
- [ ] Add a lightweight launch path that renders the Blocks WITHOUT the heavy startup loads
|
| 40 |
+
(cross-encoder, ~31k-chunk ChromaDB, graph) — layout checks don't need them.
|
| 41 |
+
e.g. `CANDLE_UI_SMOKE=1` stubs `_collection`/`_graph`/`_cross_encoder` and loads a tiny
|
| 42 |
+
trials subset for the combobox vocab.
|
| 43 |
+
- [ ] Target cold-start < ~5s in smoke mode (vs ~60s full).
|
| 44 |
+
- [ ] Verify the Clinical Trials tab renders with the smoke subset.
|
| 45 |
+
|
| 46 |
+
## Phase 2 — Playwright driver
|
| 47 |
+
- [ ] Script: launch app (smoke mode, test port, background) → wait for ready → open the
|
| 48 |
+
🏥 Clinical Trials tab.
|
| 49 |
+
- [ ] Drive the combobox: type into facility/city, read the suggestion list, pick an option,
|
| 50 |
+
run a search.
|
| 51 |
+
- [ ] Capture a screenshot of the tab to a known path.
|
| 52 |
+
|
| 53 |
+
## Phase 3 — Deterministic assertions (the reliable gate; the things that actually broke)
|
| 54 |
+
- [ ] `Recruitment status` label renders on ONE line (measure label box height / line count).
|
| 55 |
+
- [ ] Dropdown chevron does NOT overlap the value text (compare bounding boxes).
|
| 56 |
+
- [ ] 3-char gate: 2 chars → option list hidden; 3 chars → visible.
|
| 57 |
+
- [ ] Placeholder shows when empty and clears on typing.
|
| 58 |
+
- [ ] Picking a suggestion fills the box with the CLEAN value (no count suffix).
|
| 59 |
+
- [ ] Defaults are Interventional + Recruiting; empty-result hint appears when a location has
|
| 60 |
+
only trials outside the active filters.
|
| 61 |
+
- [ ] Eligibility section shows for recruiting trials, hidden for completed.
|
| 62 |
+
- [ ] Headline status-bar counts are present and non-hardcoded (match loaded data).
|
| 63 |
+
- [ ] Assertions exit non-zero on failure (CI-usable).
|
| 64 |
+
|
| 65 |
+
## Phase 4 — Design front half: describe → HTML mock → iterate → reference PNG
|
| 66 |
+
- [ ] Use the `design` skill (multi-artboard canvas) or `frontend-design` to turn a text
|
| 67 |
+
description into an HTML mock of the target tab/component.
|
| 68 |
+
- [ ] Iterate on the HTML mock (edit + re-render) until approved.
|
| 69 |
+
- [ ] Export the approved mock to a **reference PNG** stored alongside the test
|
| 70 |
+
(e.g. `docs/ui-mocks/clinical-trials.png`).
|
| 71 |
+
|
| 72 |
+
## Phase 5 — Close the loop: implement in Gradio + visual-QA agent
|
| 73 |
+
- [ ] Translate the approved mock into Gradio components + CSS/JS hooks.
|
| 74 |
+
- [ ] A `ui-reviewer` subagent takes the Phase-2 screenshot + the Phase-4 reference PNG + a
|
| 75 |
+
requirements checklist and reports pass/fail with reasons (fidelity, spacing, alignment).
|
| 76 |
+
- [ ] Loop: implement → screenshot → compare to reference → adjust, until it matches.
|
| 77 |
+
- [ ] (Optional) pixel-diff the screenshot vs reference for regression alarms.
|
| 78 |
+
|
| 79 |
+
## Phase 6 — Package as a project `run-ui` skill
|
| 80 |
+
- [ ] Commit as `.claude/skills/run-ui/` (SKILL.md + driver + smoke-mode notes) so any agent
|
| 81 |
+
invokes it the same way; document how to add an assertion and where artifacts land.
|
| 82 |
+
- [ ] Wire into the PR/deploy flow: no UI change ships without this passing.
|
| 83 |
+
|
| 84 |
+
## Notes / open questions
|
| 85 |
+
- Deterministic DOM assertions (Phase 3) are the trustworthy regression gate; the visual-QA
|
| 86 |
+
agent (Phase 5) is for subjective design judgment. Use both; don't gate on the fuzzy one.
|
| 87 |
+
- Playwright install feasibility (Phase 0) is the single biggest unknown — everything
|
| 88 |
+
automated depends on it.
|
| 89 |
+
|
| 90 |
+
## Definition of done
|
| 91 |
+
- [ ] An agent can run one command to launch + assert + screenshot the Clinical Trials tab.
|
| 92 |
+
- [ ] The regressions in Phase 3 are covered by assertions that fail loudly.
|
| 93 |
+
- [ ] A described UI change can be mocked, approved as a reference PNG, implemented, and
|
| 94 |
+
auto-checked against that reference.
|