From 1e63ca07e71a1113e0a3015eada4b65e3c26eb69 Mon Sep 17 00:00:00 2001 From: Yuval Yeret <6516902+yyeret@users.noreply.github.com> Date: Thu, 15 Oct 2026 10:46:38 -0400 Subject: [PATCH 1/3] feat: reopen analyzed repositories from saved GitHub data --- .github/workflows/ci.yml | 9 + README.md | 3 + analyze.py | 24 ++- app.py | 19 +- ...eat-persist-ui-repository-analysis-plan.md | 163 ++++++++++++++++++ index.html | 36 +++- pyproject.toml | 2 +- repository_cache.py | 57 ++++++ tests/frontend/repository_restore.test.cjs | 95 ++++++++++ tests/integration/test_collect_data.py | 24 ++- .../integration/test_repository_api_cache.py | 101 +++++++++++ tests/unit/test_repository_cache.py | 70 ++++++++ 12 files changed, 590 insertions(+), 13 deletions(-) create mode 100644 docs/plans/2026-10-15-1026-feat-persist-ui-repository-analysis-plan.md create mode 100644 repository_cache.py create mode 100644 tests/frontend/repository_restore.test.cjs create mode 100644 tests/integration/test_repository_api_cache.py create mode 100644 tests/unit/test_repository_cache.py diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 9a2ecce..42da58a 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -34,6 +34,15 @@ jobs: - name: Install dependencies run: python -m pip install -r requirements-dev.txt + - uses: actions/setup-node@v4 + if: matrix.python-version == '3.13' + with: + node-version: '22' + + - name: Test UI behavior without a browser + if: matrix.python-version == '3.13' + run: node --test tests/frontend/*.test.cjs + - name: Lint (errors only) run: ruff check . diff --git a/README.md b/README.md index cce99f4..dac31ef 100644 --- a/README.md +++ b/README.md @@ -37,6 +37,8 @@ python analyze.py openclaw/openclaw --refresh To view the generated dashboard, open `dashboard.html` directly in your web browser. Cache files are stored locally in `raw_data.json` to prevent API rate-limiting. +The web UI keeps each analyzed repository's GitHub data under `.cache/repositories/` on the server. Reopening a repository, including after a browser reload or server restart, uses that saved data without querying GitHub again. The UI remembers the last repository in the browser and displays when its data was captured. Use **Refresh data** to fetch the latest GitHub data; saved data otherwise remains until the server cache is removed. Hosts with an ephemeral filesystem need persistent storage for the cache to survive a deployment restart. The CLI's `raw_data.json` cache is separate. + ## Testing @@ -48,6 +50,7 @@ pip install -r requirements-dev.txt ruff check . pytest --cov # fails below the coverage floor in pyproject.toml pytest -m unit # or -m integration / -m e2e +node --test tests/frontend/*.test.cjs # UI reload and refresh logic, no browser ``` Tests never touch the network, the `gh`/`bws` CLIs, or Gemini; `subprocess.run` is blocked by default and each test runs in a temp directory. diff --git a/analyze.py b/analyze.py index b450d0b..81b9c52 100644 --- a/analyze.py +++ b/analyze.py @@ -16,7 +16,7 @@ RAW_DATA_FILE = "raw_data.json" DASHBOARD_FILE = "dashboard.html" -def run_graphql_query(search_query, count=100): +def run_graphql_query(search_query, count=100, strict=False): query = """ query($searchQuery: String!, $first: Int!) { search(query: $searchQuery, type: ISSUE, first: $first) { @@ -93,17 +93,23 @@ def run_graphql_query(search_query, count=100): try: res = subprocess.run(cmd, capture_output=True, text=True, encoding="utf-8", check=True) data = json.loads(res.stdout) + if strict and data.get("errors"): + raise RuntimeError("GraphQL error in pull request search") if "data" in data and "search" in data["data"]: return data["data"]["search"]["nodes"] else: + if strict: + raise RuntimeError("Invalid GraphQL response for pull request search") print("GraphQL Error or empty response:", data) return [] except subprocess.CalledProcessError as e: + if strict: + raise print("Error executing query:", e) print("Stderr:", e.stderr) return [] -def collect_data(repo="sveltejs/svelte"): +def collect_data(repo="sveltejs/svelte", strict=False): print(f"Collecting data for {repo}...") # Issues are independent of the PR queries, so fetch them alongside; fail-soft to None @@ -114,7 +120,7 @@ def collect_data(repo="sveltejs/svelte"): # 1. Pre-AI Era (2021) baseline print("Fetching Pre-AI Era (2021) sample...") pre_ai_query = f"repo:{repo} is:pr is:merged merged:2021-06-01..2021-12-31" - pre_ai_nodes = run_graphql_query(pre_ai_query, 30) + pre_ai_nodes = run_graphql_query(pre_ai_query, 30, strict=strict) if strict else run_graphql_query(pre_ai_query, 30) print(f"Retrieved {len(pre_ai_nodes)} Pre-AI PRs.") # 2. Modern Era: Fetch 600 most recent merged PRs in pages of 100 @@ -206,6 +212,8 @@ def collect_data(repo="sveltejs/svelte"): try: res = subprocess.run(cmd, capture_output=True, text=True, encoding="utf-8", check=True) data = json.loads(res.stdout) + if strict and data.get("errors"): + raise RuntimeError("GraphQL error in merged pull request search") if "data" in data and "search" in data["data"]: page_nodes = data["data"]["search"]["nodes"] recent_nodes.extend(page_nodes) @@ -217,9 +225,13 @@ def collect_data(repo="sveltejs/svelte"): else: break else: + if strict: + raise RuntimeError("Invalid GraphQL response for merged pull requests") print("Error or empty response:", data) break except Exception as e: + if strict: + raise print("Exception during merged PR fetch:", e) break @@ -298,10 +310,16 @@ def collect_data(repo="sveltejs/svelte"): try: res = subprocess.run(cmd, capture_output=True, text=True, encoding="utf-8", check=True) data = json.loads(res.stdout) + if strict and data.get("errors"): + raise RuntimeError("GraphQL error in open pull request search") if "data" in data and "search" in data["data"]: open_nodes = data["data"]["search"]["nodes"] print(f"Retrieved {len(open_nodes)} active open PRs.") + elif strict: + raise RuntimeError("Invalid GraphQL response for open pull requests") except Exception as e: + if strict: + raise print("Error fetching open PRs:", e) # 4. Issues were fetched in parallel above, independent of PR linkage diff --git a/app.py b/app.py index 2d35151..2673750 100644 --- a/app.py +++ b/app.py @@ -33,6 +33,7 @@ import sys sys.path.insert(0, str(Path(__file__).parent)) from analyze import collect_data, compute_flow_metrics +from repository_cache import RepositoryCache # --------------------------------------------------------------------------- # App Setup @@ -76,20 +77,34 @@ def _parse_repo_slug(repo_url: str) -> str: url = url[len("https://github.com/"):] if url.startswith("http://github.com/"): url = url[len("http://github.com/"):] + if url.startswith("github.com/"): + url = url[len("github.com/"):] # Remove trailing .git if present if url.endswith(".git"): url = url[:-4] # Remove any /tree/... or /blob/... path segments parts = url.split("/") if len(parts) >= 2: - return f"{parts[0]}/{parts[1]}" + repo = f"{parts[0].lower()}/{parts[1].lower()}" + RepositoryCache().path_for(repo) + return repo raise ValueError(f"Cannot parse repo slug from: {repo_url!r}") def _run_analysis(repo: str, force: bool) -> dict: """Blocking function that collects raw data and computes metrics.""" - raw = collect_data(repo) + cache = RepositoryCache() + raw = None if force else cache.load(repo) + cache_hit = raw is not None + if raw is None: + raw = collect_data(repo, strict=True) + if raw.get("issues") is not None: + try: + cache.save(repo, raw) + except OSError as exc: + print(f"Could not save repository cache for {repo}: {exc}") metrics = compute_flow_metrics(raw) + metrics["cache_hit"] = cache_hit return metrics diff --git a/docs/plans/2026-10-15-1026-feat-persist-ui-repository-analysis-plan.md b/docs/plans/2026-10-15-1026-feat-persist-ui-repository-analysis-plan.md new file mode 100644 index 0000000..03d084f --- /dev/null +++ b/docs/plans/2026-10-15-1026-feat-persist-ui-repository-analysis-plan.md @@ -0,0 +1,163 @@ +--- +title: "Persist UI Repository Analysis - Plan" +type: feat +date: 2026-10-15 +artifact_contract: ce-unified-plan/v1 +product_contract_source: ce-plan-bootstrap +execution: code +--- + +# Persist UI Repository Analysis - Plan + +## Goal Capsule + +- **Objective:** A returning visitor can reopen a previously analyzed GitHub repository in the UI without waiting for another GitHub scan, including after a page reload or server restart. +- **Means:** Store raw analysis data by repository on disk and let the UI reopen its last repository through the existing analysis API (KTD1, KTD3). +- **Authority:** The user's clarification that this concerns the UI. Existing API and metric behavior remain the compatibility baseline. +- **Execution profile:** Add integration coverage for a cache hit before changing the request path; verify the browser reload flow after the server path works. +- **Stop conditions:** Do not use a cache entry for another repository or serve an incomplete entry as a successful analysis. +- **Who finishes:** The implementer verifies and ships the code change; the user decides when to merge it. + +--- + +## Product Contract + +### Summary + +Keep repository analysis available between UI visits. The next visit reuses saved GitHub data, while a deliberate refresh fetches new data. + +### Problem Frame + +The UI always posts `force_refresh: false` to `/api/analyze`, but `app.py` calls `collect_data` for every request and ignores that flag. Its in-memory metrics map supports chat during the current server process but cannot reopen analysis after a restart. The existing CLI cache is one `raw_data.json` file, which is not keyed by repository and is unsuitable for the UI's multi-repository history. + +### Requirements + +**Returning to a repository** + +- R1. Reanalyzing a repository already viewed through the UI reuses its saved GitHub data without calling GitHub again, including after the server restarts. +- R2. Reloading the browser page reopens the last viewed repository and displays its dashboard without a fresh GitHub scan when saved data exists. +- R3. Repository A's saved data is never returned for repository B, regardless of how the repository was entered as a URL or slug. + +**Freshness and recovery** + +- R4. The UI offers a clear action to fetch fresh GitHub data for the displayed repository. A successful refresh replaces that repository's saved data without changing other repositories' entries. +- R5. A missing, unreadable, corrupt, or mismatched saved entry causes a normal collection for the requested repository. A failed collection must not replace a previously valid entry. +- R6. The UI distinguishes data loaded from saved collection from newly collected data and shows when GitHub data was captured. + +### Key Decisions + +- **UI repository revisit is the target** (session-settled: user-directed β chosen over a general CLI cache change: the user clarified that repeated scans in the UI are the problem). Governs R1, R2, R4, R6. + +### Scope Boundaries + +- The existing CLI `raw_data.json` workflow is outside this change. The UI's new repository cache must not read that file as a cross-repository cache. +- Saved GitHub data has no automatic expiration in this plan. The user controls freshness through R4; the capture time in R6 makes age visible. +- Previously cached data from the single `raw_data.json` file is not migrated because its repository association cannot be trusted without checking its contents. +- Account-wide history, cloud storage, and cache management screens are outside this change. + +### Acceptance Examples + +- AE1. Given the UI has analyzed `owner/alpha`, when the browser reloads and the last repository reopens, the dashboard appears and no GitHub query runs. Covers R1, R2. +- AE2. Given `owner/alpha` is cached, when the visitor opens `owner/beta`, only beta's matching cache entry may be used; a missing beta entry triggers collection. Covers R3. +- AE3. Given a saved analysis, when the visitor requests fresh data, GitHub is queried and the next ordinary visit uses the new capture. Covers R4, R6. +- AE4. Given an invalid saved entry, when the repository is opened, the API collects it and returns its metrics. Covers R5. + +--- + +## Planning Contract + +### Key Technical Decisions + +- KTD1. **Persist raw data per normalized repository slug.** Put API cache files under an ignored local cache directory and derive their paths only from validated owner/repo components. Raw data preserves `captured_at`; metrics are computed on each request so age-dependent metrics use the current clock. This extends `compute_flow_metrics` without persisting its time-sensitive output. +- KTD2. **Keep the cache boundary in the API path.** `app._run_analysis` checks the repository cache unless `force_refresh` is true, then passes raw data to `compute_flow_metrics`. Leave `analyze.load_or_collect` and the CLI's legacy file behavior alone, per the UI scope decision governing R1 and R2. +- KTD3. **Remember only the last repository in the browser.** Store its normalized slug locally and request it through `/api/analyze` on page load. The server remains the source of analysis data; the browser does not duplicate the metrics payload. +- KTD4. **Use complete, atomic cache replacement.** Validate stored repo identity and raw-data shape before use. Distinguish a successful empty GitHub result from a transport failure before replacing a cache entry: current collection helpers swallow some query failures and return empty lists. A failed cache read is a miss; a failed cache write does not discard a successful response. +- KTD5. **Expose cache provenance in the API response.** Add a small additive field that tells the UI whether the request reused saved data. Keep existing metric fields and chat context intact; display `captured_at` already carried by the metrics payload. + +### High-Level Technical Design + +```mermaid +flowchart TB + UI[UI: last repo or Analyze] --> API[POST /api/analyze] + API --> Gate{Force refresh?} + Gate -->|yes| GH[Collect from GitHub] + Gate -->|no| Cache{Matching valid cache?} + Cache -->|yes| Metrics[Compute current metrics] + Cache -->|no| GH + GH --> Save[Atomically save raw data for this repo] + Save --> Metrics + Metrics --> Render[Render dashboard and prime chat] +``` + +### System-Wide Impact + +- The API retains its in-memory metrics map for `/api/chat`; every cache-backed `/api/analyze` request must repopulate that map so chat works after a server restart. +- The browser's saved last-repository slug is local to that browser. The repository data cache is local to the server filesystem; ephemeral hosting will lose it on deployment restart unless that filesystem is mounted persistently. +- `collect_data` currently also writes `raw_data.json`. The new API cache is independent of that side effect, and the plan does not make `raw_data.json` a source for UI cache hits. + +--- + +## Implementation Units + +### U1. Repository-scoped raw cache + +- **Goal:** Reuse valid saved GitHub data for the requested repository and replace it safely on refresh. +- **Requirements:** R1, R3, R5. +- **Dependencies:** None. +- **Files:** `repository_cache.py` (new), `tests/unit/test_repository_cache.py` (new), `tests/integration/test_collect_data.py` (existing cache contract reference). +- **Approach:** Implement KTD1 and KTD4 as a small file cache around the existing raw bundle shape. Reject slugs that cannot safely form a repository identity or cache path. Keep the API cache separate from `raw_data.json`. +- **Patterns to follow:** `analyze.py` raw bundle keys and `tests/factories.py`'s `small_raw_data`; test isolation in `tests/conftest.py`. +- **Test scenarios:** + - Save data for `owner/alpha`, load it from a new cache instance, and get the same raw bundle with no collection. + - Save alpha and beta; loading either returns only its own bundle, including when input case and URL normalization are considered at the API boundary. + - A cache file with a different `repo`, missing required collection fields, or invalid JSON is treated as a miss. + - A failed replacement leaves the prior complete entry readable; path traversal shaped input cannot address files outside the cache directory. +- **Verification:** Repository identity and `captured_at` survive a process-level cache reopen; bad entries never produce a hit. + +### U2. API cache use and explicit refresh + +- **Goal:** Make repeated `/api/analyze` calls reuse saved data and honor `force_refresh`. +- **Requirements:** R1, R3, R4, R5, R6. +- **Dependencies:** U1. +- **Files:** `app.py`, `analyze.py`, `tests/integration/test_api.py`, `tests/integration/test_collect_data.py`, `tests/e2e/test_pipeline.py`. +- **Approach:** Route `_run_analysis` through U1, preserving current metric computation and `_metrics_cache` population. Expose enough collection failure status from `analyze.py` for KTD4 without changing the CLI's cache behavior. Normalize and validate repository identity before cache lookup. Return the KTD5 provenance field with the existing response shape. +- **Patterns to follow:** `_parse_repo_slug`, `compute_flow_metrics`, `tests/factories.py`'s `FakeGh`, and the existing `TestClient` route tests. +- **Test scenarios:** + - Covers AE1. First request collects alpha; a later request after clearing in-memory metrics returns alpha from disk without invoking `gh`, and chat can use those metrics. + - Covers AE2. A cached alpha never satisfies beta's request; beta collects and gets beta metrics. + - Covers AE3. A request with `force_refresh: true` calls collection even on a cache hit, updates only alpha's cache, and the next ordinary request sees the new capture. + - Covers AE4. A corrupt alpha entry triggers collection; a collection failure returns the current error response and does not publish partial data. + - A GitHub query failure that currently yields an empty list does not overwrite an older valid cache entry; a genuinely empty result may be saved. + - The response reports cache hit versus fresh collection without removing existing metric fields. +- **Verification:** The API makes no GitHub call for a valid cache hit and honors the refresh flag across server restarts. + +### U3. UI reopen and freshness controls + +- **Goal:** Reopen the last repository after page reload and let the visitor request current GitHub data. +- **Requirements:** R2, R4, R6. +- **Dependencies:** U2. +- **Files:** `index.html`, `tests/frontend/repository_restore.test.cjs` (new), `.github/workflows/ci.yml`, `README.md`. +- **Approach:** Remember the normalized slug after successful analysis. On page initialization, put it back in the input and request analysis; use the API's cache provenance and `captured_at` in status text. Add a visible refresh action that sends `force_refresh: true` for the current repository. Reuse the existing loading, error, and render paths so failed restores remain recoverable through the form. +- **Patterns to follow:** `analyzeRepo`, `showLoading`, `showStatus`, `renderDashboard`, and the existing API request shape in `index.html`. Use Node's built-in test and VM APIs to exercise the page script without launching Chromium; API integration tests prove the real cache boundary. +- **Test scenarios:** + - Covers AE1. After a successful analysis, reloading the page restores the same repository and renders the dashboard from a cache-backed API response. + - Covers AE3. The refresh control sends `force_refresh: true`, shows a fresh capture, and an ordinary subsequent load reports a cache hit. + - Missing or unavailable browser storage leaves manual analysis usable; a failed automatic restore shows an error and does not trap the visitor in loading state. +- **Verification:** The page script restores the previous repository and sends a cache-backed request on simulated reload; the UI code makes the data's age and refresh option clear. + +--- + +## Verification Contract + +- Run the relevant unit and integration tests for the cache and API, then the full `pytest --cov` gate and `ruff check .` used by CI. Run the Node test for last-repository restore and refresh controls. +- Verify the combined page-script and server behavior: the Node test simulates reload and refresh; API integration tests reopen persisted data after clearing memory and confirm no new GitHub scan. +- Keep automated tests network-free using the existing `subprocess.run` guard and temporary working directory. The Node test intercepts API responses and does not launch Chromium. + +--- + +## Definition of Done + +- R1βR6 and AE1βAE4 are demonstrated by the unit, API, and Node UI checks above. +- A cache hit leaves GitHub untouched; force refresh collects and replaces one repository's entry; a bad entry cannot leak another repository's data. +- The README explains where the UI cache lives, how long data remains saved, how to refresh it, and the limitation of ephemeral server storage. +- The final diff contains no abandoned cache formats or experimental paths. diff --git a/index.html b/index.html index 89adcd7..6deeb9f 100644 --- a/index.html +++ b/index.html @@ -119,6 +119,8 @@ color:var(--text-dim); } .btn-ghost:hover{border-color:var(--violet);color:var(--text);} + #refresh-btn:disabled{opacity:.5;cursor:not-allowed;} + #capture-note{color:var(--text-muted);font-size:.82rem;margin:.35rem 0 1rem;} /* Spinner */ .spinner{ @@ -436,7 +438,8 @@ @media(max-width:640px){ #app{padding:1rem;} header{flex-direction:column;align-items:flex-start;} - .repo-bar{max-width:100%;} + .repo-bar{max-width:100%;flex-wrap:wrap;} + .repo-bar input{flex-basis:100%;} .kpi-grid{grid-template-columns:1fr 1fr;} #chat-drawer{width:100vw;} #chat-toggle.open{right:1rem;} @@ -471,6 +474,7 @@