brianbellx commited on
Commit
161cb23
·
verified ·
1 Parent(s): 80666bc

Deploy validated CodeHygiene app, stats and leaderboard

Browse files

Published from approved source snapshot ebd9ac8b5cce5817b29c72eef3b2e964bc0aa4d7. Validation: 100 tests passed; frontend build and hygiene gate passed. Scorer pin retained.

This view is limited to 50 files because it contains too many changes.   See raw diff
.dockerignore ADDED
@@ -0,0 +1,8 @@
 
 
 
 
 
 
 
 
 
1
+ **/__pycache__
2
+ **/*.pyc
3
+ .pytest_cache
4
+ tests
5
+ web/node_modules
6
+ web/dist
7
+ web/vendor
8
+ .env
.gitignore ADDED
@@ -0,0 +1,7 @@
 
 
 
 
 
 
 
 
1
+ __pycache__/
2
+ *.pyc
3
+ .pytest_cache/
4
+ web/node_modules/
5
+ web/dist/
6
+ web/vendor/
7
+ .codex-notes/
Dockerfile ADDED
@@ -0,0 +1,50 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ARG CODEHYGIENE_REF=0221af718b51a56b6ee0fbea52f0bf42afa381bb
2
+
3
+ FROM node:22-slim AS web
4
+ ARG CODEHYGIENE_REF
5
+ WORKDIR /build
6
+ COPY web/package.json web/package-lock.json web/
7
+ RUN cd web && npm ci
8
+ COPY web/ web/
9
+ ADD https://raw.githubusercontent.com/brianbell-x/codehygiene/${CODEHYGIENE_REF}/docs/engine.js web/vendor/engine.js
10
+ ADD https://raw.githubusercontent.com/brianbell-x/codehygiene/${CODEHYGIENE_REF}/docs/presets.json web/vendor/presets.json
11
+ ADD https://raw.githubusercontent.com/brianbell-x/codehygiene/${CODEHYGIENE_REF}/docs/weights.npz web/vendor/weights.npz
12
+ RUN cd web && npm run build
13
+
14
+ FROM python:3.12-slim
15
+ ARG CODEHYGIENE_REF
16
+ ARG REFINER_REPO=Qwen/Qwen2.5-Coder-0.5B-Instruct-GGUF
17
+ ARG REFINER_FILE=qwen2.5-coder-0.5b-instruct-q4_k_m.gguf
18
+
19
+ RUN apt-get update \
20
+ && apt-get install -y --no-install-recommends libgomp1 git \
21
+ && rm -rf /var/lib/apt/lists/*
22
+
23
+ RUN useradd -m -u 1000 user
24
+ USER user
25
+ ENV HOME=/home/user \
26
+ PATH=/home/user/.local/bin:$PATH \
27
+ PYTHONUNBUFFERED=1
28
+ WORKDIR /home/user/app
29
+
30
+ COPY --chown=user requirements.txt .
31
+ RUN pip install --no-cache-dir --user \
32
+ --extra-index-url https://abetlen.github.io/llama-cpp-python/whl/cpu \
33
+ --only-binary=llama-cpp-python \
34
+ -r requirements.txt \
35
+ "codehygiene @ git+https://github.com/brianbell-x/codehygiene@${CODEHYGIENE_REF}"
36
+
37
+ RUN python -c "import sys; from huggingface_hub import hf_hub_download; hf_hub_download(sys.argv[1], sys.argv[2], local_dir='/home/user/models')" \
38
+ "${REFINER_REPO}" "${REFINER_FILE}"
39
+
40
+ COPY --chown=user server/ server/
41
+ COPY --from=web --chown=user /build/web/dist web/dist
42
+ COPY --from=web --chown=user /build/web/vendor/presets.json web/vendor/presets.json
43
+
44
+ ENV REFINER_BACKEND=llama \
45
+ REFINER_REPO=${REFINER_REPO} \
46
+ REFINER_FILE=${REFINER_FILE} \
47
+ REFINER_MODEL_PATH=/home/user/models/${REFINER_FILE}
48
+
49
+ EXPOSE 7860
50
+ CMD ["python", "-m", "uvicorn", "server.app:app", "--host", "0.0.0.0", "--port", "7860", "--workers", "1", "--no-access-log"]
README.md CHANGED
@@ -1,11 +1,62 @@
1
  ---
2
- title: Codehygiene
3
- emoji: 📚
4
- colorFrom: yellow
5
- colorTo: purple
6
  sdk: docker
7
- pinned: false
8
  short_description: Paste a GitHub link and watch the code get cleaned up
 
 
9
  ---
10
 
11
- Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  ---
2
+ title: codehygiene
3
+ emoji: 🧹
4
+ colorFrom: red
5
+ colorTo: gray
6
  sdk: docker
7
+ app_port: 7860
8
  short_description: Paste a GitHub link and watch the code get cleaned up
9
+ pinned: false
10
+ suggested_hardware: cpu-basic
11
  ---
12
 
13
+ # codehygiene Space
14
+
15
+ Paste a GitHub link. The page finds the hardest-to-read files and a small model rewrites them while you watch.
16
+
17
+ The scorer runs in the browser. The refiner runs here on the CPU. A patch is kept only if it parses, keeps the code, and leaves fewer issues.
18
+
19
+ ## Settings
20
+
21
+ | Variable | Default | What it does |
22
+ |---|---|---|
23
+ | `REFINER_BACKEND` | `llama` | `llama` runs the GGUF model. `mock` replays recorded patches. |
24
+ | `REFINER_REPO` | `Qwen/Qwen2.5-Coder-0.5B-Instruct-GGUF` | Build arg. Hub repo of the GGUF. |
25
+ | `REFINER_FILE` | `qwen2.5-coder-0.5b-instruct-q4_k_m.gguf` | Build arg. GGUF file name. |
26
+ | `REFINER_MODEL_PATH` | set by the Dockerfile | Path of the GGUF inside the image. |
27
+ | `CODEHYGIENE_REF` | pinned commit | Build arg. Version of the scorer and web assets. |
28
+ | `MAX_FILE_CHARS` | `12000` | Longer files are not sent to the model. |
29
+ | `WARMUP` | on for `llama`, off for `mock` | Fix the examples in the background after start. |
30
+ | `MOCK_PROMPT_DELAY` | `1.2` | Seconds before the mock starts streaming. |
31
+ | `MOCK_PIECES_PER_SECOND` | `60` | Mock streaming speed. |
32
+
33
+ Counters, privacy rules and the GitHub App settings for `Create PR` are in [TELEMETRY.md](TELEMETRY.md).
34
+
35
+ ## Run locally
36
+
37
+ Mock mode needs only FastAPI, uvicorn and numpy.
38
+
39
+ ```bash
40
+ cd space
41
+ REFINER_BACKEND=mock python -m uvicorn server.app:app --port 7860
42
+ ```
43
+
44
+ Build the UI first (`cd web && npm ci && npm run build`) to serve it at `/`. Without it, `/` returns a short note and the API still works under `/api`.
45
+
46
+ Run the tests:
47
+
48
+ ```bash
49
+ cd space
50
+ python -m pytest tests -q
51
+ ```
52
+
53
+ ## Swap the model
54
+
55
+ Pass other build args. The model must follow the ChatML prompt format.
56
+
57
+ ```bash
58
+ docker build \
59
+ --build-arg REFINER_REPO=your-name/your-refiner-GGUF \
60
+ --build-arg REFINER_FILE=refiner-q4_k_m.gguf \
61
+ -t codehygiene-space .
62
+ ```
TELEMETRY.md ADDED
@@ -0,0 +1,134 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Telemetry
2
+
3
+ The Space keeps a handful of aggregate counters. Source code, file names, paths, user names and IP addresses are never stored, logged or published. Repository names are stored only for public repos that someone asks to rank (see [Leaderboard](#leaderboard)).
4
+
5
+ Every number is public at the Space's `/api/stats` route, and the leaderboard at `/api/leaderboard`. The code that counts is [`server/telemetry.py`](server/telemetry.py) on the server and [`web/src/telemetry.js`](web/src/telemetry.js) in the browser.
6
+
7
+ ## What is counted
8
+
9
+ | Counter | Counted by | When it goes up |
10
+ |---|---|---|
11
+ | `files_scored` | Browser report, checked by the server | A file from a repo, folder or snippet is scored for the first time. |
12
+ | `files_cleaned` | Server only | The refiner finishes an accepted fix of a file for the first time. |
13
+ | `fix_runs` | Server only | Every accepted refiner run, repeats included. |
14
+ | `avg_score_before`, `avg_score_after` | Server only | Mean score out of 100 of cleaned files, before and after. |
15
+ | `issues_fixed` | Derived | Sum of `squiggles_resolved_by_kind`. |
16
+ | `median_score` | Browser report | Median score out of 100 of scored files, from a 101-bin histogram. |
17
+ | `score_ms_median`, `score_ms_samples` | Browser report | Median time the browser took to score one file, from a histogram with about eight bins per factor of ten, and how many files were timed. |
18
+ | `by_ext` | Both | Files scored and cleaned per extension, and the mean score of scored files. Unknown extensions count as `other`. |
19
+ | `by_loc_bucket` | Browser report | Files scored per size band: `0-50`, `50-100`, `100-300`, `300-1000`, `1000+` lines. |
20
+ | `squiggles_before_by_kind` | Browser report | Issues found in scored files, per kind. |
21
+ | `squiggles_resolved_by_kind` | Server only | Issues removed by accepted fixes, per kind. |
22
+ | `prs_opened` | Server only | The GitHub App opens a pull request. |
23
+ | `prs_merged`, `prs_closed_unmerged` | Signed GitHub webhook | A pull request opened by the App is merged, or closed without merging. |
24
+ | `prs_decided`, `pr_merge_rate` | Derived | `prs_merged + prs_closed_unmerged`, and `prs_merged / prs_decided`. Pull requests still open are left out of the rate. |
25
+ | `leaderboard_entries` | Server only | Repos on the leaderboard. |
26
+
27
+ Nothing is counted for the bundled examples, on the page or on the server. The server also ignores the examples' exact contents under any other source. `files_cleaned` cannot be raised by a direct request: it only moves when the refiner itself accepts a fix. The mock backend counts nothing unless `ALLOW_MOCK_TELEMETRY=1` (tests only).
28
+
29
+ `files_scored` and the scoring times come from the browser, so they are best effort. The banner at the top of the page shows each number only once its sample is large enough:
30
+
31
+ | Banner item | Shown when |
32
+ |---|---|
33
+ | Issues fixed | `files_cleaned` ≥ 1,000 |
34
+ | PRs merged | `prs_decided` ≥ 20 |
35
+ | Time per file | `score_ms_samples` ≥ 1,000 |
36
+ | Leaderboard link | `leaderboard_entries` ≥ 5 |
37
+
38
+ The Stats tab of the leaderboard page shows every number from the first count, with its sample size.
39
+
40
+ ## What the browser sends
41
+
42
+ After a repo, folder or snippet is scored, one request goes to `POST /api/telemetry/scan`. It can be read in the browser's network tab:
43
+
44
+ ```json
45
+ {
46
+ "source_kind": "repo",
47
+ "files": [
48
+ {
49
+ "digest16": "9f84c2a10e77b31d",
50
+ "ext": "py",
51
+ "loc_bucket": "100-300",
52
+ "score_before": 29,
53
+ "squiggles": { "deep_nesting": 2, "swallowed_error": 1 },
54
+ "score_ms": 14
55
+ }
56
+ ]
57
+ }
58
+ ```
59
+
60
+ | Field | Meaning |
61
+ |---|---|
62
+ | `source_kind` | `repo`, `folder` or `snippet`. Example runs send nothing. |
63
+ | `digest16` | First 16 hex characters (64 bits) of `SHA-256(ext + "\0" + content)`, computed in the browser with Web Crypto. It is used only to count a file once. |
64
+ | `ext` | Lower-case file extension. |
65
+ | `loc_bucket` | Size band, not the line count. |
66
+ | `score_before` | Score out of 100. |
67
+ | `squiggles` | Issue counts per kind. |
68
+ | `score_ms` | Milliseconds the browser took to score the file, capped at 60,000. Optional. |
69
+
70
+ Any other field is rejected. A batch holds at most 200 files. Each address may send 10 batches a minute.
71
+
72
+ ## What is never kept
73
+
74
+ - **Uploaded folders and pasted snippets** skip the result cache in both directions. Their content exists only in memory for the request that fixes them.
75
+ - **Repository files** may be cached in memory (128 results, cleared on restart) so the same fix is not computed twice. The cache is never written to disk.
76
+ - **Logs** carry no content, diffs, file names, folder or repository names, cookies or addresses. The access log is turned off.
77
+ - **Rate limiting** keys each bucket by a salted hash of the caller's address. The salt is random per process and never stored, and the address itself is dropped after the check.
78
+ - **Digests** for counting repeats live in two in-memory sets of at most 100,000 entries each (scored and cleaned). They are never written anywhere, so a restart forgets them.
79
+
80
+ ## Where the counters live
81
+
82
+ Counters are written to `stats.json` at `STATS_PATH` (default `/tmp/codehygiene_stats.json`). When `HF_STATS_DATASET_REPO` and `HF_TOKEN` are set, `stats.json` is also read from that Hugging Face Dataset repo on start and written back every 5 minutes and on shutdown. Nothing is uploaded until the existing copy has been read, so a failed read cannot reset the public numbers.
83
+
84
+ `stats.json` holds only the counters above, a version and a timestamp, plus `open_pr_keys`. Each key is `SHA-256(repo_id + ":" + pr_number)[:16]` for a pull request the App opened that has not closed yet. It lets merges be counted after a restart, and it is dropped once the pull request closes. There are no titles, branch names, repository names or user names in the file.
85
+
86
+ ## Pull requests
87
+
88
+ `Create PR` appears only when the Space has a GitHub App configured, the run came from a GitHub link, and at least one fix was accepted. It is off in mock mode. `Download patch` needs no account and works for repos and folders.
89
+
90
+ 1. A pop-up asks the visitor to authorize the App on GitHub.
91
+ 2. The callback page hands a one-time ticket to the CodeHygiene page that opened it (`postMessage`, this origin only). A sign-in link sent by someone else cannot be redeemed by them.
92
+ 3. The server checks that the visitor has push access (`permissions.push`) and that the App is installed on that one repository. Without push access the request fails with `403`; no fork is ever made. Without the App, the pop-up goes to its install page.
93
+ 4. Each file is re-read at the exact commit that was scored and re-scored on the server. A file must have fewer issues than its base, or nothing is committed.
94
+ 5. A branch `codehygiene/<id>` is created from that commit, and the pull request lists each file with its score before and after and the issues resolved.
95
+ 6. The visitor's token is held in memory for at most 3 minutes, used for that one pull request, then revoked.
96
+
97
+ `POST /api/github/webhook` accepts only requests signed with `GITHUB_WEBHOOK_SECRET` (`X-Hub-Signature-256`, compared in constant time). Anything else gets `401`.
98
+
99
+ ## Leaderboard
100
+
101
+ The leaderboard page (`/leaderboard/`, opened in a new tab from the banner) ranks public GitHub repos by score. A repo is listed only when someone presses the trophy button after loading it, or when it is one of the seed repos in `SEED_REPOS` in [`server/leaderboard.py`](server/leaderboard.py). Scanning a repo on the page does not list it.
102
+
103
+ The browser sends only the owner and repository name to `POST /api/leaderboard/rank`. Any other field, a score included, is rejected. The server then:
104
+
105
+ 1. Reads the repo's metadata from the GitHub API. Private repos and forks are refused. If the API is rate limited, the repo is ranked from its archive alone, without a past score.
106
+ 2. Downloads the default branch as a `tar.gz` archive from `codeload.github.com` (at most 80 MB) and reads it in memory. Nothing is written to disk. The commit SHA comes from the archive header.
107
+ 3. Keeps hand-written Python, TypeScript, JavaScript, Go and Rust files. Tests, vendored and build folders, minified, generated and type-declaration files, files over 150 KB and binary files are skipped.
108
+ 4. Requires at least 5 such files and 1,000 lines.
109
+ 5. Scores a stable sample, chosen by path hash, of at most 300 files or 20,000 lines with the same Python scorer the Space uses for fixes. The repo score is the line-weighted mean, out of 100.
110
+ 6. Scores the same branch as it was a year earlier, when that commit can be found. **Most improved** ranks repos by the change between the two.
111
+ 7. Refuses a commit that is already ranked under another name, which catches mirrors and forks.
112
+
113
+ A ranked repo is reused for 24 hours. Each address may ask for 2 ranks a minute, the whole Space 20, and at most 3 wait at once.
114
+
115
+ Each entry records the scorer that produced it: the `codehygiene` version plus the first 12 hex characters of the SHA-256 of `weights.npz`. Only entries from the running scorer are listed or counted, so a scorer release never mixes two scales on one board. Older entries stay in `leaderboard.json` until the background task re-ranks them, or drops them when the repo is gone, private, a fork, a duplicate, or too small. The Stats tab's "As a reward" figures come from [`web/src/reward-facts.json`](web/src/reward-facts.json), keyed by version; the strip is hidden when the running version has no entry.
116
+
117
+ The public board shows the top 100 of each list. There is no search and no bottom of the list; a rank below 100 appears only in the reply to a rank request. `leaderboard.json` holds, per repo: the name, score, file and line counts, commit SHA, time ranked, and the past score and SHA. It is stored like `stats.json`, in the same dataset repo when one is set.
118
+
119
+ ## Settings
120
+
121
+ | Variable | What it does |
122
+ |---|---|
123
+ | `STATS_PATH` | Local `stats.json` path. |
124
+ | `HF_STATS_DATASET_REPO`, `HF_TOKEN` | Dataset repo that keeps `stats.json` across restarts. The repo can be public. |
125
+ | `STATS_SYNC_SECONDS` | Sync interval, default `300`. |
126
+ | `ALLOW_MOCK_TELEMETRY` | Count fixes from the mock backend. Tests only. |
127
+ | `GITHUB_APP_ID`, `GITHUB_APP_CLIENT_ID`, `GITHUB_APP_CLIENT_SECRET`, `GITHUB_APP_PRIVATE_KEY`, `GITHUB_WEBHOOK_SECRET` | All five turn on `Create PR`. The private key may use `\n` for line breaks. |
128
+ | `GITHUB_APP_SLUG` | Optional. Read from GitHub when unset. |
129
+ | `GITHUB_TOKEN` | Optional. A fine-grained token with no permissions, used only for the leaderboard's GitHub API reads. It raises the API limit from 60 to 5,000 requests an hour; the Space's address is shared, so the anonymous limit may already be spent. |
130
+ | `LEADERBOARD_PATH` | Local `leaderboard.json` path, default `/tmp/codehygiene_leaderboard.json`. |
131
+ | `LEADERBOARD_SEED` | After start, rank the seed repos in the background, then re-rank entries from an older scorer. On by default with the real backend; seeds ranked by the running scorer in the last 7 days are skipped. |
132
+ | `LEADERBOARD_SEED_PAUSE` | Seconds between seed repos, default `20`. |
133
+
134
+ The App needs repository permissions **Contents: write** and **Pull requests: write**, the callback URL `https://<space>.hf.space/api/github/callback`, and the webhook URL `https://<space>.hf.space/api/github/webhook` with the **Pull request** event.
requirements.txt ADDED
@@ -0,0 +1,7 @@
 
 
 
 
 
 
 
 
1
+ fastapi==0.115.12
2
+ uvicorn==0.34.2
3
+ numpy>=1.26,<3
4
+ llama-cpp-python==0.3.36
5
+ httpx==0.28.1
6
+ PyJWT[crypto]==2.10.1
7
+ huggingface_hub>=0.24
server/__init__.py ADDED
File without changes
server/app.py ADDED
@@ -0,0 +1,330 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from __future__ import annotations
2
+
3
+ import asyncio
4
+ import json
5
+ import logging
6
+ from collections.abc import AsyncIterator, Awaitable, Callable
7
+ from contextlib import asynccontextmanager
8
+ from html import escape
9
+ from typing import Any
10
+
11
+ import httpx
12
+ from fastapi import FastAPI, HTTPException, Request
13
+ from fastapi.middleware.cors import CORSMiddleware
14
+ from fastapi.responses import HTMLResponse, JSONResponse, PlainTextResponse, RedirectResponse, StreamingResponse
15
+ from fastapi.staticfiles import StaticFiles
16
+ from pydantic import BaseModel, Field
17
+
18
+ from .backends import Refiner, make_refiner
19
+ from .config import Settings, load_settings
20
+ from .fix_loop import Cancelled, Emit, FixRequest, RefinerUnavailable
21
+ from .fix_queue import QueueFull
22
+ from .github_pr import CreatePrBody, PrError, PullRequests
23
+ from .hygiene import SCORER_VERSION, score_dict
24
+ from .leaderboard import BOARD_FILE, SEED_REPOS, Leaderboard, RankBody, RankError
25
+ from .service import FixService
26
+ from .telemetry import RateLimiter, ScanBody, SourceKind, StatsStore, SyncedFile, Telemetry, preset_digests
27
+
28
+ log = logging.getLogger("space")
29
+
30
+ PING_SECONDS = 10.0
31
+ SSE_HEADERS = {"Cache-Control": "no-cache, no-transform", "X-Accel-Buffering": "no"}
32
+ MAX_BODY_CHARS = 200_000
33
+ QUEUE_FULL_MESSAGE = "The refiner queue is full. Try again shortly."
34
+ ROOT_NOTE = "codehygiene Space API is running. Build space/web to serve the UI here.\n"
35
+ STREAM_ONLY_KEYS = ("tokens", "tokens_per_second")
36
+ ERROR_STATUS = {"model_unavailable": 503, "queue_full": 429, "cancelled": 499}
37
+ SCAN_BATCHES_PER_MINUTE = 10
38
+ SCAN_BATCHES_PER_MINUTE_ALL = 600
39
+
40
+ RANKS_PER_MINUTE = 2
41
+ RANKS_PER_MINUTE_ALL = 20
42
+
43
+ STATS_HEADERS = {"Cache-Control": "public, max-age=60"}
44
+ CALLBACK_PAGE = """<!doctype html>
45
+ <html lang="en"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width, initial-scale=1">
46
+ <meta name="referrer" content="no-referrer"><title>CodeHygiene</title><style>
47
+ :root{{color-scheme:light dark}}body{{margin:0;display:grid;place-items:center;min-height:100svh;
48
+ font:15px/22px -apple-system,BlinkMacSystemFont,"Segoe UI",sans-serif;
49
+ background:light-dark(#f7f7f5,#161615);color:light-dark(#4d4c48,#b5b3ac)}}
50
+ </style></head><body><p id="message">{message}</p><script>
51
+ const reply = {reply};
52
+ if (window.opener) {{
53
+ window.opener.postMessage({{ type: "codehygiene:github", ...reply }}, window.location.origin);
54
+ }} else if (reply.ticket) {{
55
+ document.getElementById("message").textContent = "Start from the CodeHygiene page.";
56
+ }}
57
+ </script></body></html>
58
+ """
59
+ SIGNED_IN = "Signed in. Return to CodeHygiene."
60
+ CALLBACK_HEADERS = {"Cache-Control": "no-store", "Referrer-Policy": "no-referrer"}
61
+
62
+
63
+ def callback_page(message: str, reply: dict[str, str], status_code: int = 200) -> HTMLResponse:
64
+ page = CALLBACK_PAGE.format(message=escape(message), reply=json.dumps(reply))
65
+ return HTMLResponse(page, status_code=status_code, headers=CALLBACK_HEADERS)
66
+
67
+
68
+ class ScoreBody(BaseModel):
69
+ filename: str = ""
70
+ content: str = Field(max_length=MAX_BODY_CHARS)
71
+ threshold: float = Field(0.55, ge=0.0, le=1.0)
72
+
73
+
74
+ class FixBody(ScoreBody):
75
+ max_passes: int = Field(2, ge=1, le=2)
76
+ no_cache: bool = False
77
+ source_kind: SourceKind = "snippet"
78
+
79
+ def to_request(self) -> FixRequest:
80
+ return FixRequest(self.filename, self.content, self.threshold, self.max_passes)
81
+
82
+
83
+ Producer = Callable[[Emit], Awaitable[None]]
84
+
85
+
86
+ def sse_frame(name: str, data: dict[str, Any]) -> str:
87
+ return f"event: {name}\ndata: {json.dumps(data)}\n\n"
88
+
89
+
90
+ async def guarded(produce: Producer, emit: Emit) -> None:
91
+ try:
92
+ await produce(emit)
93
+ except Cancelled:
94
+ log.info("fix cancelled")
95
+ except QueueFull:
96
+ emit("error", {"code": "queue_full", "message": QUEUE_FULL_MESSAGE})
97
+ except RefinerUnavailable as exc:
98
+ log.warning("refiner unavailable: %s", exc)
99
+ emit("error", {"code": "model_unavailable", "message": str(exc)})
100
+ except Exception as exc:
101
+ log.error("fix failed: %s", type(exc).__name__)
102
+ emit("error", {"code": "internal", "message": f"{type(exc).__name__}: {exc}"})
103
+
104
+
105
+ async def sse_events(produce: Producer) -> AsyncIterator[str]:
106
+ events: asyncio.Queue[tuple[str, dict[str, Any]] | None] = asyncio.Queue()
107
+ task = asyncio.create_task(guarded(produce, lambda name, data: events.put_nowait((name, data))))
108
+ task.add_done_callback(lambda _: events.put_nowait(None))
109
+ try:
110
+ while True:
111
+ try:
112
+ item = await asyncio.wait_for(events.get(), PING_SECONDS)
113
+ except TimeoutError:
114
+ yield ": ping\n\n"
115
+ continue
116
+ if item is None:
117
+ return
118
+ yield sse_frame(*item)
119
+ finally:
120
+ task.cancel()
121
+
122
+
123
+ def caller_of(request: Request) -> str:
124
+ """Address used only for the rate-limit bucket check, then discarded."""
125
+ forwarded = request.headers.get("x-forwarded-for", "").split(",")[0].strip()
126
+ return forwarded or (request.client.host if request.client else "")
127
+
128
+
129
+ def error_response(error: PrError | RankError) -> JSONResponse:
130
+ return JSONResponse(error.payload(), status_code=error.status)
131
+
132
+
133
+ def create_app(
134
+ settings: Settings | None = None,
135
+ refiner: Refiner | None = None,
136
+ github_transport: httpx.AsyncBaseTransport | None = None,
137
+ board: Leaderboard | None = None,
138
+ ) -> FastAPI:
139
+ settings = settings or load_settings()
140
+ telemetry = Telemetry(excluded=preset_digests(settings.presets_path))
141
+ service = FixService(settings, refiner or make_refiner(settings), telemetry)
142
+ pulls = PullRequests(settings, telemetry, github_transport)
143
+ board = board or Leaderboard(settings, github_transport)
144
+ dataset = (settings.stats_dataset_repo, settings.hf_token)
145
+ synced = (
146
+ SyncedFile(StatsStore(settings.stats_path, *dataset), telemetry),
147
+ SyncedFile(StatsStore(settings.leaderboard_path, *dataset, filename=BOARD_FILE), board),
148
+ )
149
+ scan_limit = RateLimiter(SCAN_BATCHES_PER_MINUTE)
150
+ scan_limit_all = RateLimiter(SCAN_BATCHES_PER_MINUTE_ALL)
151
+ rank_limit = RateLimiter(RANKS_PER_MINUTE)
152
+ rank_limit_all = RateLimiter(RANKS_PER_MINUTE_ALL)
153
+
154
+ async def sync_files() -> None:
155
+ while True:
156
+ await asyncio.sleep(settings.stats_sync_seconds)
157
+ for file in synced:
158
+ await file.flush()
159
+
160
+ @asynccontextmanager
161
+ async def lifespan(_: FastAPI) -> AsyncIterator[None]:
162
+ for file in synced:
163
+ await file.load()
164
+ tasks = [asyncio.create_task(sync_files())]
165
+ if settings.backend == "llama" or settings.warmup:
166
+ tasks.append(asyncio.create_task(warm_up()))
167
+ if settings.leaderboard_seed:
168
+ tasks.append(asyncio.create_task(board.maintain(SEED_REPOS, settings.leaderboard_seed_pause)))
169
+ yield
170
+ for task in tasks:
171
+ task.cancel()
172
+ service.close()
173
+ await pulls.close()
174
+ await board.close()
175
+ for file in synced:
176
+ await file.flush()
177
+
178
+ async def warm_up() -> None:
179
+ try:
180
+ if settings.warmup:
181
+ await service.warm_presets(settings.presets_path)
182
+ else:
183
+ await asyncio.get_running_loop().run_in_executor(None, service.refiner.load)
184
+ except Exception:
185
+ log.exception("model load or warm-up failed")
186
+
187
+ app = FastAPI(title="codehygiene", lifespan=lifespan)
188
+ app.state.service = service
189
+ app.state.telemetry = telemetry
190
+ app.state.pulls = pulls
191
+ app.state.board = board
192
+ app.add_middleware(CORSMiddleware, allow_origins=["*"], allow_methods=["*"], allow_headers=["*"])
193
+
194
+ def producer_for(body: FixBody, paced: bool) -> Producer:
195
+ request = body.to_request()
196
+ cached = None if body.no_cache else service.cached(request, body.source_kind)
197
+ if cached is not None:
198
+ return lambda emit: service.replay(cached, emit, paced=paced)
199
+ if not service.queue.has_room:
200
+ raise HTTPException(status_code=429, detail=QUEUE_FULL_MESSAGE)
201
+
202
+ async def produce(emit: Emit) -> None:
203
+ await service.execute(request, service.queue.join(), emit, source_kind=body.source_kind)
204
+
205
+ return produce
206
+
207
+ @app.get("/api/health")
208
+ def health() -> dict[str, Any]:
209
+ return {
210
+ "status": "ok",
211
+ "model_loaded": service.refiner.loaded,
212
+ "model_id": service.refiner.model_id,
213
+ "backend": settings.backend,
214
+ "max_file_chars": settings.max_file_chars,
215
+ "github_pr_enabled": pulls.enabled,
216
+ }
217
+
218
+ @app.get("/api/stats")
219
+ def stats() -> JSONResponse:
220
+ body = {
221
+ **telemetry.public(),
222
+ "leaderboard_entries": len(board.current()),
223
+ "scorer_version": SCORER_VERSION,
224
+ "github_pr_enabled": pulls.enabled,
225
+ }
226
+ return JSONResponse(body, headers=STATS_HEADERS)
227
+
228
+ @app.get("/api/leaderboard")
229
+ def leaderboard() -> JSONResponse:
230
+ return JSONResponse(board.public(), headers=STATS_HEADERS)
231
+
232
+ @app.post("/api/leaderboard/rank", response_model=None)
233
+ async def leaderboard_rank(body: RankBody, request: Request) -> dict[str, Any] | JSONResponse:
234
+ if not (rank_limit.allow(caller_of(request)) and rank_limit_all.allow("*")):
235
+ return error_response(RankError(429, "busy", "Too many rank requests. Try again shortly."))
236
+ try:
237
+ return await board.rank(body.owner, body.repo)
238
+ except RankError as error:
239
+ return error_response(error)
240
+ except httpx.HTTPError as error:
241
+ log.warning("GitHub archive request failed: %s", type(error).__name__)
242
+ return error_response(RankError(502, "github_error", "GitHub could not be reached."))
243
+
244
+ @app.post("/api/telemetry/scan")
245
+ def telemetry_scan(body: ScanBody, request: Request) -> dict[str, int]:
246
+ allowed = scan_limit.allow(caller_of(request)) and scan_limit_all.allow("*")
247
+ if not allowed:
248
+ raise HTTPException(status_code=429, detail="Too many scan reports.")
249
+ return {"counted": telemetry.record_scan(body)}
250
+
251
+ def require_pulls() -> None:
252
+ if not pulls.enabled:
253
+ raise HTTPException(status_code=404, detail="Pull requests are not enabled.")
254
+
255
+ @app.get("/api/github/login", response_model=None)
256
+ def github_login() -> RedirectResponse | JSONResponse:
257
+ require_pulls()
258
+ try:
259
+ return RedirectResponse(pulls.login_url(), status_code=302, headers=CALLBACK_HEADERS)
260
+ except PrError as error:
261
+ return error_response(error)
262
+
263
+ @app.get("/api/github/callback")
264
+ async def github_callback(code: str = "", state: str = "") -> HTMLResponse:
265
+ require_pulls()
266
+ try:
267
+ ticket = await pulls.complete_login(code, state)
268
+ except PrError as error:
269
+ return callback_page(error.message, {"error": error.code}, error.status)
270
+ except httpx.HTTPError as error:
271
+ log.warning("GitHub sign-in failed: %s", type(error).__name__)
272
+ return callback_page("GitHub could not be reached.", {"error": "github_error"}, 502)
273
+ return callback_page(SIGNED_IN, {"ticket": ticket})
274
+
275
+ @app.post("/api/github/create-pr", response_model=None)
276
+ async def github_create_pr(body: CreatePrBody) -> dict[str, Any] | JSONResponse:
277
+ require_pulls()
278
+ try:
279
+ return await pulls.create(body)
280
+ except PrError as error:
281
+ return error_response(error)
282
+ except httpx.HTTPError as error:
283
+ log.warning("GitHub request failed: %s", type(error).__name__)
284
+ return error_response(PrError(502, "github_error", "GitHub could not be reached."))
285
+
286
+ @app.post("/api/github/webhook", response_model=None)
287
+ async def github_webhook(request: Request) -> dict[str, Any] | JSONResponse:
288
+ if not settings.github_webhook_secret:
289
+ raise HTTPException(status_code=404, detail="Webhooks are not enabled.")
290
+ body = await request.body()
291
+ try:
292
+ return pulls.webhook(
293
+ request.headers.get("x-github-event", ""),
294
+ body,
295
+ request.headers.get("x-hub-signature-256"),
296
+ )
297
+ except PrError as error:
298
+ return error_response(error)
299
+
300
+ @app.post("/api/score")
301
+ def score(body: ScoreBody) -> dict[str, Any]:
302
+ return score_dict(body.content, body.threshold)
303
+
304
+ @app.post("/api/fix")
305
+ async def fix(body: FixBody) -> dict[str, Any]:
306
+ collected: dict[str, dict[str, Any]] = {}
307
+ produce = producer_for(body, paced=False)
308
+ await guarded(produce, lambda name, data: collected.__setitem__(name, data))
309
+ if "done" in collected:
310
+ return {key: value for key, value in collected["done"].items() if key not in STREAM_ONLY_KEYS}
311
+ failure = collected.get("error", {"code": "cancelled", "message": "The fix was cancelled."})
312
+ raise HTTPException(status_code=ERROR_STATUS.get(failure["code"], 500), detail=failure)
313
+
314
+ @app.post("/api/fix/stream")
315
+ async def fix_stream(body: FixBody) -> StreamingResponse:
316
+ produce = producer_for(body, paced=True)
317
+ return StreamingResponse(sse_events(produce), media_type="text/event-stream", headers=SSE_HEADERS)
318
+
319
+ if (settings.web_dist / "index.html").is_file():
320
+ app.mount("/", StaticFiles(directory=settings.web_dist, html=True), name="web")
321
+ else:
322
+
323
+ @app.get("/", response_class=PlainTextResponse)
324
+ def root_note() -> str:
325
+ return ROOT_NOTE
326
+
327
+ return app
328
+
329
+
330
+ app = create_app()
server/backends.py ADDED
@@ -0,0 +1,205 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from __future__ import annotations
2
+
3
+ import json
4
+ import logging
5
+ import re
6
+ import threading
7
+ import time
8
+ from collections.abc import Callable, Iterator, Sequence
9
+ from pathlib import Path
10
+ from typing import Any, Protocol
11
+
12
+ from .config import Settings
13
+ from .prompt import content_key
14
+
15
+ log = logging.getLogger("space.refiner")
16
+
17
+ STOP = "<|im_end|>"
18
+ PREFILL_CHUNK = 128
19
+ MOCK_PROGRESS_STEP = 0.2
20
+ _PIECE_RE = re.compile(r" ?\w{1,6}| ?[^\w\s]{1,3}|\s+")
21
+
22
+ Progress = Callable[[int, int], None]
23
+
24
+
25
+ class Refiner(Protocol):
26
+ model_id: str
27
+
28
+ @property
29
+ def loaded(self) -> bool: ...
30
+
31
+ def load(self) -> None: ...
32
+
33
+ def prompt_fits(self, prompt: str) -> bool: ...
34
+
35
+ def stream(
36
+ self,
37
+ prompt: str,
38
+ content: str,
39
+ temperature: float,
40
+ top_p: float,
41
+ cancel: threading.Event,
42
+ on_progress: Progress,
43
+ ) -> Iterator[str]: ...
44
+
45
+
46
+ def shared_prefix(left: Sequence[int], right: Sequence[int]) -> int:
47
+ count = 0
48
+ for left_token, right_token in zip(left, right):
49
+ if left_token != right_token:
50
+ break
51
+ count += 1
52
+ return count
53
+
54
+
55
+ class LlamaRefiner:
56
+ def __init__(self, settings: Settings) -> None:
57
+ self.model_id = settings.model_id
58
+ self._model_path = settings.model_path
59
+ self._n_ctx = settings.n_ctx
60
+ self._n_threads = settings.n_threads
61
+ self._max_new_tokens = settings.max_new_tokens
62
+ self._llm: Any = None
63
+ self._load_lock = threading.Lock()
64
+
65
+ @property
66
+ def loaded(self) -> bool:
67
+ return self._llm is not None
68
+
69
+ def load(self) -> None:
70
+ with self._load_lock:
71
+ if self._llm is not None:
72
+ return
73
+ if not Path(self._model_path).is_file():
74
+ raise FileNotFoundError(f"GGUF not found at REFINER_MODEL_PATH={self._model_path!r}")
75
+ from llama_cpp import Llama
76
+
77
+ self._llm = Llama(
78
+ model_path=self._model_path,
79
+ n_ctx=self._n_ctx,
80
+ n_threads=self._n_threads,
81
+ verbose=False,
82
+ )
83
+
84
+ def _tokenize(self, prompt: str) -> list[int]:
85
+ return self._llm.tokenize(prompt.encode("utf-8"), add_bos=False, special=True)
86
+
87
+ def prompt_fits(self, prompt: str) -> bool:
88
+ return len(self._tokenize(prompt)) <= self._n_ctx - self._max_new_tokens
89
+
90
+ def _prefill(self, tokens: list[int], cancel: threading.Event, on_progress: Progress) -> None:
91
+ """Evaluate all but the last prompt token so create_completion reuses them by prefix match."""
92
+ llm = self._llm
93
+ total = len(tokens)
94
+ end = total - 1
95
+ cached = list(llm.input_ids[: llm.n_tokens])
96
+ reuse = min(shared_prefix(cached, tokens), end)
97
+ if reuse == 0:
98
+ llm.reset()
99
+ else:
100
+ llm.n_tokens = reuse
101
+ on_progress(reuse, total)
102
+ for start in range(reuse, end, PREFILL_CHUNK):
103
+ if cancel.is_set():
104
+ return
105
+ stop = min(start + PREFILL_CHUNK, end)
106
+ llm.eval(tokens[start:stop])
107
+ on_progress(stop, total)
108
+ on_progress(total, total)
109
+
110
+ def stream(
111
+ self,
112
+ prompt: str,
113
+ content: str,
114
+ temperature: float,
115
+ top_p: float,
116
+ cancel: threading.Event,
117
+ on_progress: Progress,
118
+ ) -> Iterator[str]:
119
+ tokens = self._tokenize(prompt)
120
+ completion_prompt: str | list[int] = tokens
121
+ try:
122
+ self._prefill(tokens, cancel, on_progress)
123
+ except Exception:
124
+ log.exception("chunked prompt evaluation failed; using plain create_completion")
125
+ self._llm.reset()
126
+ completion_prompt = prompt
127
+ if cancel.is_set():
128
+ return
129
+ chunks = self._llm.create_completion(
130
+ prompt=completion_prompt,
131
+ stream=True,
132
+ max_tokens=self._max_new_tokens,
133
+ temperature=temperature,
134
+ top_p=top_p,
135
+ stop=[STOP],
136
+ )
137
+ try:
138
+ for chunk in chunks:
139
+ if cancel.is_set():
140
+ return
141
+ text = chunk["choices"][0]["text"]
142
+ if text:
143
+ yield text
144
+ finally:
145
+ chunks.close()
146
+
147
+
148
+ def split_pieces(text: str) -> list[str]:
149
+ return _PIECE_RE.findall(text)
150
+
151
+
152
+ class MockRefiner:
153
+ def __init__(self, settings: Settings) -> None:
154
+ self.model_id = settings.model_id or "mock"
155
+ self._prompt_delay = settings.mock_prompt_delay
156
+ self._interval = 1.0 / settings.mock_pieces_per_second if settings.mock_pieces_per_second > 0 else 0.0
157
+ self._prompt_budget = settings.n_ctx - settings.max_new_tokens
158
+ fixtures = json.loads(Path(settings.fixtures_path).read_text(encoding="utf-8"))["fixtures"]
159
+ self._replies = {fixture["key"]: fixture["reply"] for fixture in fixtures}
160
+
161
+ @property
162
+ def loaded(self) -> bool:
163
+ return True
164
+
165
+ def load(self) -> None:
166
+ return None
167
+
168
+ def prompt_tokens(self, prompt: str) -> int:
169
+ return len(prompt) // 3
170
+
171
+ def prompt_fits(self, prompt: str) -> bool:
172
+ return self.prompt_tokens(prompt) <= self._prompt_budget
173
+
174
+ def _read_prompt(self, total: int, cancel: threading.Event, on_progress: Progress) -> bool:
175
+ started = time.monotonic()
176
+ while (elapsed := time.monotonic() - started) < self._prompt_delay:
177
+ on_progress(int(total * elapsed / self._prompt_delay), total)
178
+ if cancel.wait(min(MOCK_PROGRESS_STEP, self._prompt_delay - elapsed)):
179
+ return False
180
+ on_progress(total, total)
181
+ return True
182
+
183
+ def stream(
184
+ self,
185
+ prompt: str,
186
+ content: str,
187
+ temperature: float,
188
+ top_p: float,
189
+ cancel: threading.Event,
190
+ on_progress: Progress,
191
+ ) -> Iterator[str]:
192
+ if not self._read_prompt(self.prompt_tokens(prompt), cancel, on_progress):
193
+ return
194
+ started = time.monotonic()
195
+ for index, piece in enumerate(split_pieces(self._replies.get(content_key(content), ""))):
196
+ ahead = started + index * self._interval - time.monotonic()
197
+ if cancel.wait(max(0.0, ahead)):
198
+ return
199
+ yield piece
200
+
201
+
202
+ def make_refiner(settings: Settings) -> Refiner:
203
+ if settings.backend == "mock":
204
+ return MockRefiner(settings)
205
+ return LlamaRefiner(settings)
server/config.py ADDED
@@ -0,0 +1,140 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from __future__ import annotations
2
+
3
+ import os
4
+ from dataclasses import dataclass, field
5
+ from pathlib import Path
6
+
7
+ SPACE_DIR = Path(__file__).resolve().parents[1]
8
+ REPO_ROOT = SPACE_DIR.parent
9
+
10
+
11
+ def _env_float(env: dict[str, str], name: str, default: float) -> float:
12
+ raw = env.get(name, "").strip()
13
+ return float(raw) if raw else default
14
+
15
+
16
+ def _env_int(env: dict[str, str], name: str, default: int) -> int:
17
+ raw = env.get(name, "").strip()
18
+ return int(raw) if raw else default
19
+
20
+
21
+ def _env_flag(env: dict[str, str], name: str, default: bool) -> bool:
22
+ raw = env.get(name, "").strip().lower()
23
+ if not raw:
24
+ return default
25
+ return raw in ("1", "true", "yes", "on")
26
+
27
+
28
+ def _first_existing(*candidates: Path) -> Path | None:
29
+ return next((path for path in candidates if path.is_file()), None)
30
+
31
+
32
+ @dataclass(frozen=True)
33
+ class Settings:
34
+ backend: str = "llama"
35
+ model_path: str = ""
36
+ model_id: str = ""
37
+ n_ctx: int = 8192
38
+ n_threads: int = 2
39
+ max_new_tokens: int = 1280
40
+ max_file_chars: int = 12_000
41
+
42
+ fixtures_path: Path = SPACE_DIR / "server" / "fixtures" / "mock_patches.json"
43
+ mock_prompt_delay: float = 1.2
44
+ mock_pieces_per_second: float = 60.0
45
+ replay_tokens_per_second: float = 120.0
46
+
47
+ warmup: bool = True
48
+ presets_path: Path | None = None
49
+ web_dist: Path = SPACE_DIR / "web" / "dist"
50
+ max_waiters: int = 5
51
+ cache_size: int = 128
52
+
53
+ stats_path: Path = Path("/tmp/codehygiene_stats.json")
54
+ stats_dataset_repo: str = ""
55
+ hf_token: str = field(default="", repr=False)
56
+ stats_sync_seconds: float = 300.0
57
+ allow_mock_telemetry: bool = False
58
+
59
+ github_app_id: str = ""
60
+ github_app_client_id: str = ""
61
+ github_app_slug: str = ""
62
+ github_app_client_secret: str = field(default="", repr=False)
63
+ github_app_private_key: str = field(default="", repr=False)
64
+ github_webhook_secret: str = field(default="", repr=False)
65
+
66
+ github_token: str = field(default="", repr=False)
67
+ leaderboard_path: Path = Path("/tmp/codehygiene_leaderboard.json")
68
+ leaderboard_seed: bool = False
69
+ leaderboard_seed_pause: float = 20.0
70
+
71
+ @property
72
+ def counts_fixes(self) -> bool:
73
+ return self.backend != "mock" or self.allow_mock_telemetry
74
+
75
+ @property
76
+ def github_pr_enabled(self) -> bool:
77
+ credentials = (
78
+ self.github_app_id,
79
+ self.github_app_client_id,
80
+ self.github_app_client_secret,
81
+ self.github_app_private_key,
82
+ self.github_webhook_secret,
83
+ )
84
+ return all(credentials) and self.backend != "mock"
85
+
86
+
87
+ def _env_text(env: dict[str, str], name: str) -> str:
88
+ return env.get(name, "").strip()
89
+
90
+
91
+ def load_settings(env: dict[str, str] | None = None) -> Settings:
92
+ env = dict(os.environ if env is None else env)
93
+ backend = env.get("REFINER_BACKEND", "llama").strip().lower() or "llama"
94
+ if backend not in ("llama", "mock"):
95
+ raise ValueError(f"REFINER_BACKEND must be 'llama' or 'mock', got {backend!r}")
96
+
97
+ repo_id = env.get("REFINER_REPO", "").strip()
98
+ file_name = env.get("REFINER_FILE", "").strip()
99
+ model_path = env.get("REFINER_MODEL_PATH", "").strip()
100
+ default_id = f"{repo_id}/{file_name}" if repo_id and file_name else Path(model_path).name
101
+ model_id = env.get("REFINER_MODEL_ID", "").strip() or ("mock" if backend == "mock" else default_id)
102
+
103
+ presets_env = env.get("PRESETS_PATH", "").strip()
104
+ presets_path = _first_existing(
105
+ *([Path(presets_env)] if presets_env else []),
106
+ SPACE_DIR / "web" / "vendor" / "presets.json",
107
+ REPO_ROOT / "docs" / "presets.json",
108
+ )
109
+ defaults = Settings()
110
+ return Settings(
111
+ backend=backend,
112
+ model_path=model_path,
113
+ model_id=model_id,
114
+ n_threads=_env_int(env, "REFINER_THREADS", defaults.n_threads),
115
+ max_file_chars=_env_int(env, "MAX_FILE_CHARS", defaults.max_file_chars),
116
+ fixtures_path=Path(env.get("MOCK_FIXTURES", "").strip() or defaults.fixtures_path),
117
+ mock_prompt_delay=_env_float(env, "MOCK_PROMPT_DELAY", defaults.mock_prompt_delay),
118
+ mock_pieces_per_second=_env_float(env, "MOCK_PIECES_PER_SECOND", defaults.mock_pieces_per_second),
119
+ replay_tokens_per_second=_env_float(
120
+ env, "REPLAY_TOKENS_PER_SECOND", defaults.replay_tokens_per_second
121
+ ),
122
+ warmup=_env_flag(env, "WARMUP", backend == "llama"),
123
+ presets_path=presets_path,
124
+ web_dist=Path(env.get("WEB_DIST", "").strip() or defaults.web_dist),
125
+ stats_path=Path(_env_text(env, "STATS_PATH") or defaults.stats_path),
126
+ stats_dataset_repo=_env_text(env, "HF_STATS_DATASET_REPO"),
127
+ hf_token=_env_text(env, "HF_TOKEN"),
128
+ stats_sync_seconds=_env_float(env, "STATS_SYNC_SECONDS", defaults.stats_sync_seconds),
129
+ allow_mock_telemetry=_env_flag(env, "ALLOW_MOCK_TELEMETRY", False),
130
+ github_app_id=_env_text(env, "GITHUB_APP_ID"),
131
+ github_app_client_id=_env_text(env, "GITHUB_APP_CLIENT_ID"),
132
+ github_app_slug=_env_text(env, "GITHUB_APP_SLUG"),
133
+ github_app_client_secret=_env_text(env, "GITHUB_APP_CLIENT_SECRET"),
134
+ github_app_private_key=_env_text(env, "GITHUB_APP_PRIVATE_KEY").replace("\\n", "\n"),
135
+ github_webhook_secret=_env_text(env, "GITHUB_WEBHOOK_SECRET"),
136
+ github_token=_env_text(env, "GITHUB_TOKEN"),
137
+ leaderboard_path=Path(_env_text(env, "LEADERBOARD_PATH") or defaults.leaderboard_path),
138
+ leaderboard_seed=_env_flag(env, "LEADERBOARD_SEED", backend == "llama"),
139
+ leaderboard_seed_pause=_env_float(env, "LEADERBOARD_SEED_PAUSE", defaults.leaderboard_seed_pause),
140
+ )
server/fix_loop.py ADDED
@@ -0,0 +1,246 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from __future__ import annotations
2
+
3
+ import threading
4
+ import time
5
+ from collections.abc import Callable
6
+ from dataclasses import dataclass
7
+ from typing import Any
8
+
9
+ from .backends import Refiner
10
+ from .guards import Verdict, verify_patch
11
+ from .hygiene import score_dict
12
+ from .prompt import build_prompt
13
+ from .vendored_pipeline import apply_changes
14
+
15
+ Emit = Callable[[str, dict[str, Any]], None]
16
+
17
+ GREEDY = (0.0, 1.0)
18
+ SAMPLED = (0.45, 0.92)
19
+ PREVIEW_INTERVAL = 1.0 / 12
20
+ PROGRESS_INTERVAL = 1.0 / 5
21
+ CLOSE_BLOCK = "\n>>>>>>> REPLACE"
22
+ _OPEN_MARKER = "<<<<<<< SEARCH"
23
+ _PARTIAL_MARKERS = ("=======", ">>>>>>> REPLACE")
24
+
25
+
26
+ class Cancelled(Exception):
27
+ pass
28
+
29
+
30
+ class RefinerUnavailable(Exception):
31
+ pass
32
+
33
+
34
+ @dataclass(frozen=True)
35
+ class FixRequest:
36
+ filename: str
37
+ content: str
38
+ threshold: float = 0.55
39
+ max_passes: int = 2
40
+
41
+
42
+ def preview_patch_text(raw: str) -> str:
43
+ open_at = raw.rfind(_OPEN_MARKER)
44
+ if open_at == -1 or ">>>>>>> REPLACE" in raw[open_at:]:
45
+ return raw
46
+ text = raw[:-1] if raw.endswith("\n") else raw
47
+ head, newline, tail = text.rpartition("\n")
48
+ if newline and tail != "=======" and any(marker.startswith(tail) for marker in _PARTIAL_MARKERS):
49
+ text = head
50
+ return text + CLOSE_BLOCK
51
+
52
+
53
+ def preview_content(base: str, raw: str) -> tuple[str, int] | None:
54
+ try:
55
+ content, applied = apply_changes(base, preview_patch_text(raw))
56
+ except ValueError:
57
+ return None
58
+ if applied == 0:
59
+ return None
60
+ return content, applied
61
+
62
+
63
+ class _Attempt:
64
+ def __init__(self, emit: Emit, cancel: threading.Event, base: str) -> None:
65
+ self.emit = emit
66
+ self.cancel = cancel
67
+ self.base = base
68
+ self.raw = ""
69
+ self.tokens = 0
70
+ self.seconds = 0.0
71
+ self._last_preview_at = 0.0
72
+ self._last_preview = base
73
+ self._last_progress_at = -PROGRESS_INTERVAL
74
+ self._prompt_total: int | None = None
75
+ self._read_reported = False
76
+
77
+ def progress(self, done: int, total: int) -> None:
78
+ if self.tokens or self._read_reported:
79
+ return
80
+ self._prompt_total = total
81
+ finished = done >= total
82
+ if not finished and time.monotonic() - self._last_progress_at < PROGRESS_INTERVAL:
83
+ return
84
+ self._last_progress_at = time.monotonic()
85
+ self._read_reported = finished
86
+ self.emit("progress", {"phase": "read", "done": min(done, total), "total": total})
87
+
88
+ def add(self, piece: str) -> None:
89
+ if not self.tokens and self._prompt_total is not None:
90
+ self.progress(self._prompt_total, self._prompt_total)
91
+ self.raw += piece
92
+ self.tokens += 1
93
+ self.emit("token", {"text": piece})
94
+ if time.monotonic() - self._last_preview_at >= PREVIEW_INTERVAL:
95
+ self._preview()
96
+
97
+ def flush(self) -> None:
98
+ wait = PREVIEW_INTERVAL - (time.monotonic() - self._last_preview_at)
99
+ if wait > 0 and self.cancel.wait(wait):
100
+ return
101
+ self._preview()
102
+
103
+ def _preview(self) -> None:
104
+ self._last_preview_at = time.monotonic()
105
+ found = preview_content(self.base, self.raw)
106
+ if found is None or found[0] == self._last_preview:
107
+ return
108
+ self._last_preview, applied = found
109
+ self.emit("preview", {"content": self._last_preview, "applied_blocks": applied})
110
+
111
+
112
+ class FixRun:
113
+ def __init__(
114
+ self,
115
+ refiner: Refiner,
116
+ request: FixRequest,
117
+ cancel: threading.Event,
118
+ emit: Emit,
119
+ max_file_chars: int,
120
+ ) -> None:
121
+ self.refiner = refiner
122
+ self.request = request
123
+ self.cancel = cancel
124
+ self.emit = emit
125
+ self.max_file_chars = max_file_chars
126
+ self.attempts = 0
127
+ self.tokens = 0
128
+ self.generation_seconds = 0.0
129
+
130
+ def run(self) -> dict[str, Any]:
131
+ started = time.monotonic()
132
+ request = self.request
133
+ before = score_dict(request.content, request.threshold)
134
+ self.emit("start", {"before_hygiene": before, "model_id": self.refiner.model_id, "cached": False})
135
+ outcome = self._outcome(before)
136
+ done = self._done_payload(before, outcome, started)
137
+ self.emit("done", done)
138
+ return done
139
+
140
+ def _outcome(self, before: dict[str, Any]) -> dict[str, Any]:
141
+ request = self.request
142
+ outcome: dict[str, Any] = {"accepted": [], "reason": None, "passes": 0}
143
+ if not before["squiggles"]:
144
+ return outcome
145
+ if len(request.content) > self.max_file_chars:
146
+ outcome["reason"] = "too_long"
147
+ return outcome
148
+ self._load_refiner()
149
+
150
+ first = self._attempt(1, request.content, before, sampled=False)
151
+ outcome["passes"] = 1
152
+ if first.accepted:
153
+ outcome["accepted"].append(first)
154
+ if request.max_passes < 2:
155
+ outcome["reason"] = first.reason
156
+ return outcome
157
+
158
+ if first.accepted and first.squiggles_after:
159
+ outcome["passes"] = 2
160
+ second = self._attempt(2, first.content, first.hygiene, sampled=False)
161
+ if second.accepted:
162
+ outcome["accepted"].append(second)
163
+ elif not first.accepted:
164
+ outcome["passes"] = 2
165
+ outcome["reason"], best = self._sampled_retry(before)
166
+ if best is not None:
167
+ outcome["accepted"].append(best)
168
+ if not outcome["accepted"] and outcome["reason"] is None:
169
+ outcome["reason"] = first.reason
170
+ return outcome
171
+
172
+ def _sampled_retry(self, before: dict[str, Any]) -> tuple[str | None, Verdict | None]:
173
+ tries = [self._attempt(2, self.request.content, before, sampled=True) for _ in range(2)]
174
+ valid = [verdict for verdict in tries if verdict.accepted]
175
+ if not valid:
176
+ return tries[-1].reason, None
177
+ best = min(valid, key=lambda verdict: (verdict.squiggles_after, -(verdict.score_after or 0.0)))
178
+ return None, best
179
+
180
+ def _load_refiner(self) -> None:
181
+ try:
182
+ self.refiner.load()
183
+ except Exception as exc:
184
+ raise RefinerUnavailable(f"{type(exc).__name__}: {exc}") from exc
185
+
186
+ def _attempt(self, pass_no: int, base: str, hygiene: dict[str, Any], sampled: bool) -> Verdict:
187
+ if self.cancel.is_set():
188
+ raise Cancelled()
189
+ self.attempts += 1
190
+ number = self.attempts
191
+ self.emit("attempt", {"n": number, "pass": pass_no, "sampled": sampled})
192
+ prompt = build_prompt(base, hygiene, self.request.filename)
193
+ if self.refiner.prompt_fits(prompt):
194
+ reply = self._generate(prompt, base, sampled)
195
+ verdict = verify_patch(base, reply, self.request.filename, self.request.threshold, hygiene)
196
+ verdict.reply = reply
197
+ else:
198
+ verdict = Verdict(False, "too_long", base, squiggles_before=len(hygiene["squiggles"]))
199
+ self.emit("verdict", {
200
+ "n": number,
201
+ "accepted": verdict.accepted,
202
+ "reason": verdict.reason,
203
+ "squiggles_before": verdict.squiggles_before,
204
+ "squiggles_after": verdict.squiggles_after,
205
+ "score_after": verdict.score_after,
206
+ "applied_blocks": verdict.applied_blocks,
207
+ })
208
+ return verdict
209
+
210
+ def _generate(self, prompt: str, base: str, sampled: bool) -> str:
211
+ temperature, top_p = SAMPLED if sampled else GREEDY
212
+ attempt = _Attempt(self.emit, self.cancel, base)
213
+ first_token_at: float | None = None
214
+ pieces = self.refiner.stream(prompt, base, temperature, top_p, self.cancel, attempt.progress)
215
+ for piece in pieces:
216
+ first_token_at = first_token_at or time.monotonic()
217
+ attempt.add(piece)
218
+ if self.cancel.is_set():
219
+ raise Cancelled()
220
+ if first_token_at is not None:
221
+ self.generation_seconds += time.monotonic() - first_token_at
222
+ attempt.flush()
223
+ self.tokens += attempt.tokens
224
+ return attempt.raw
225
+
226
+ def _done_payload(
227
+ self, before: dict[str, Any], outcome: dict[str, Any], started: float
228
+ ) -> dict[str, Any]:
229
+ accepted: list[Verdict] = outcome["accepted"]
230
+ final = accepted[-1] if accepted else None
231
+ rate = self.tokens / self.generation_seconds if self.generation_seconds > 0 else 0.0
232
+ return {
233
+ "filename": self.request.filename,
234
+ "changes": "\n\n".join(verdict.reply for verdict in accepted),
235
+ "applied_blocks": sum(verdict.applied_blocks for verdict in accepted),
236
+ "updated_content": final.content if final else self.request.content,
237
+ "before_hygiene": before,
238
+ "after_hygiene": final.hygiene if final else before,
239
+ "passes_used": outcome["passes"],
240
+ "latency_ms": round((time.monotonic() - started) * 1000),
241
+ "accepted": final is not None,
242
+ "reason": None if final else outcome["reason"],
243
+ "cached": False,
244
+ "tokens": self.tokens,
245
+ "tokens_per_second": round(rate, 1),
246
+ }
server/fix_queue.py ADDED
@@ -0,0 +1,89 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from __future__ import annotations
2
+
3
+ import asyncio
4
+ import threading
5
+ from collections.abc import Callable
6
+ from dataclasses import dataclass, field
7
+
8
+
9
+ class QueueFull(Exception):
10
+ pass
11
+
12
+
13
+ @dataclass(eq=False)
14
+ class Ticket:
15
+ background: bool = False
16
+ cancel: threading.Event = field(default_factory=threading.Event)
17
+
18
+
19
+ class FixQueue:
20
+ """One running fix at a time; visitors wait in order ahead of background warm-up jobs."""
21
+
22
+ def __init__(self, max_waiters: int) -> None:
23
+ self.max_waiters = max_waiters
24
+ self._waiting: list[Ticket] = []
25
+ self._busy: Ticket | None = None
26
+ self._changed = asyncio.Event()
27
+
28
+ @property
29
+ def idle(self) -> bool:
30
+ return self._busy is None and not self._waiting
31
+
32
+ @property
33
+ def visitors_waiting(self) -> int:
34
+ return sum(1 for ticket in self._waiting if not ticket.background)
35
+
36
+ @property
37
+ def has_room(self) -> bool:
38
+ return self._busy is None or self.visitors_waiting < self.max_waiters
39
+
40
+ def join(self, background: bool = False) -> Ticket:
41
+ if not background and not self.has_room:
42
+ raise QueueFull()
43
+ ticket = Ticket(background=background)
44
+ if background:
45
+ self._waiting.append(ticket)
46
+ else:
47
+ first_background = next(
48
+ (idx for idx, queued in enumerate(self._waiting) if queued.background),
49
+ len(self._waiting),
50
+ )
51
+ self._waiting.insert(first_background, ticket)
52
+ if self._busy is not None and self._busy.background:
53
+ self._busy.cancel.set()
54
+ self._notify()
55
+ return ticket
56
+
57
+ async def wait_turn(self, ticket: Ticket, on_position: Callable[[int], None]) -> None:
58
+ last_position = -1
59
+ while True:
60
+ changed = self._changed
61
+ if self._busy is None and self._waiting and self._waiting[0] is ticket:
62
+ self._waiting.pop(0)
63
+ self._busy = ticket
64
+ self._notify()
65
+ return
66
+ position = self._waiting.index(ticket) + (self._busy is not None)
67
+ if position != last_position:
68
+ on_position(position)
69
+ last_position = position
70
+ await changed.wait()
71
+
72
+ async def wait_idle(self) -> None:
73
+ while not self.idle:
74
+ await self._changed.wait()
75
+
76
+ def release(self, ticket: Ticket) -> None:
77
+ if self._busy is ticket:
78
+ self._busy = None
79
+ elif ticket in self._waiting:
80
+ self._waiting.remove(ticket)
81
+ self._notify()
82
+
83
+ def cancel_all(self) -> None:
84
+ for ticket in [*self._waiting, *([self._busy] if self._busy else [])]:
85
+ ticket.cancel.set()
86
+
87
+ def _notify(self) -> None:
88
+ self._changed.set()
89
+ self._changed = asyncio.Event()
server/fixtures/mock_patches.json ADDED
@@ -0,0 +1,103 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "fixtures": [
3
+ {
4
+ "key": "603901a66c358a271f8bea8305951afa99fa1fec4581a303fbd3d3cd392e8ab0",
5
+ "filename": "send_receipt.py",
6
+ "pass": 1,
7
+ "source": "handwritten",
8
+ "origin": "docs/presets.json before",
9
+ "squiggles_before": 3,
10
+ "squiggles_after": 0,
11
+ "content": "def send_receipt(order: Order) -> None:\n try:\n if order is not None:\n if order.paid and not order.refunded:\n # Extract email address from order\n email = order.email\n if email is not None:\n if len(email.strip()) > 0:\n mailer.send(email, order.id)\n except Exception:\n pass",
12
+ "reply": "<<<<<<< SEARCH\ndef send_receipt(order: Order) -> None:\n try:\n if order is not None:\n if order.paid and not order.refunded:\n # Extract email address from order\n email = order.email\n if email is not None:\n if len(email.strip()) > 0:\n mailer.send(email, order.id)\n except Exception:\n pass\n=======\nimport logging\n\nlogger = logging.getLogger(__name__)\n\n\ndef send_receipt(order: Order) -> None:\n if order is None or not order.paid or order.refunded:\n return\n email = order.email\n if not email or not email.strip():\n return\n try:\n mailer.send(email, order.id)\n except Exception:\n logger.exception(\"Could not send receipt for order %s\", order.id)\n>>>>>>> REPLACE\n"
13
+ },
14
+ {
15
+ "key": "fb26a694b102dfe43772450032b23dd08ecc96f56672ea4be0f7bad66322a32d",
16
+ "filename": "pricing.ts",
17
+ "pass": 1,
18
+ "source": "handwritten",
19
+ "origin": "docs/presets.json before",
20
+ "squiggles_before": 4,
21
+ "squiggles_after": 2,
22
+ "content": "export class DiscountStrategyFactoryManager {\n public computeTotalAmount(cartItems: CartItem[], promoRate: number): number {\n try {\n // Step 1: Compute discounted subtotal from cart items\n const total = cartItems.filter((item) => item.active && item.qty > 0).map((item) => item.price * item.qty * (promoRate > 0 ? (promoRate > 0.5 ? 0.5 : 1 - promoRate) : 1)).reduce((acc, val) => acc + val, 0);\n return total;\n } catch (err) {\n console.log(err);\n return 0.0;\n }\n }\n}",
23
+ "reply": "<<<<<<< SEARCH\n // Step 1: Compute discounted subtotal from cart items\n const total = cartItems.filter((item) => item.active && item.qty > 0).map((item) => item.price * item.qty * (promoRate > 0 ? (promoRate > 0.5 ? 0.5 : 1 - promoRate) : 1)).reduce((acc, val) => acc + val, 0);\n return total;\n=======\n const multiplier = promoRate > 0 ? 1 - Math.min(promoRate, 0.5) : 1;\n const activeItems = cartItems.filter((item) => item.active && item.qty > 0);\n return activeItems.reduce((acc, item) => acc + item.price * item.qty * multiplier, 0);\n>>>>>>> REPLACE\n"
24
+ },
25
+ {
26
+ "key": "1b7c59b3968bb5426ae5916be0eea950aa72d7a36f610efe3d3d555c064c02ff",
27
+ "filename": "frame.rs",
28
+ "pass": 1,
29
+ "source": "handwritten",
30
+ "origin": "docs/presets.json before",
31
+ "squiggles_before": 12,
32
+ "squiggles_after": 10,
33
+ "content": "#[allow(clippy::many_single_char_names)]\npub fn decode_frame(buf: &[u8]) -> Option<(u32, u16, u8)> {\n let mut hd = [0u8; 16];\n hd[0] = buf[12] ^ buf[28];\n hd[1] = buf[14] ^ buf[30];\n hd[2] = buf[18] ^ buf[34];\n hd[3] = buf[22] ^ buf[38];\n let mg = u16::from_be_bytes([buf[10], buf[11]]);\n let sq = u32::from_be_bytes([buf[16], buf[17], buf[18], buf[19]]);\n let ln = u16::from_be_bytes([buf[24], buf[25]]);\n let fl = buf[36] ^ buf[42];\n let ck = buf[44] ^ buf[47];\n let vr = hd[0].wrapping_add(hd[1]).wrapping_add(hd[2]);\n let ok = mg == 23112 && vr > 0 && (fl & 4) != 0 && ck != 0;\n if !ok { return None; }\n Some((sq, ln, ck))\n}",
34
+ "reply": "<<<<<<< SEARCH\n let mg = u16::from_be_bytes([buf[10], buf[11]]);\n let sq = u32::from_be_bytes([buf[16], buf[17], buf[18], buf[19]]);\n let ln = u16::from_be_bytes([buf[24], buf[25]]);\n let fl = buf[36] ^ buf[42];\n let ck = buf[44] ^ buf[47];\n let vr = hd[0].wrapping_add(hd[1]).wrapping_add(hd[2]);\n let ok = mg == 23112 && vr > 0 && (fl & 4) != 0 && ck != 0;\n if !ok { return None; }\n Some((sq, ln, ck))\n=======\n let magic = u16::from_be_bytes([buf[10], buf[11]]);\n let sequence = u32::from_be_bytes([buf[16], buf[17], buf[18], buf[19]]);\n let length = u16::from_be_bytes([buf[24], buf[25]]);\n let flags = buf[36] ^ buf[42];\n let checksum = buf[44] ^ buf[47];\n let header_sum = hd[0].wrapping_add(hd[1]).wrapping_add(hd[2]);\n if magic != 23112 || header_sum == 0 || (flags & 4) == 0 || checksum == 0 {\n return None;\n }\n Some((sequence, length, checksum))\n>>>>>>> REPLACE\n"
35
+ },
36
+ {
37
+ "key": "694945a5f10bdd90dec848e6a12a5b0e6b97158da7ffcf7f6db8a3b9273bd13a",
38
+ "filename": "frame.rs",
39
+ "pass": 2,
40
+ "source": "handwritten",
41
+ "origin": "pass-1 output",
42
+ "squiggles_before": 10,
43
+ "squiggles_after": 8,
44
+ "content": "#[allow(clippy::many_single_char_names)]\npub fn decode_frame(buf: &[u8]) -> Option<(u32, u16, u8)> {\n let mut hd = [0u8; 16];\n hd[0] = buf[12] ^ buf[28];\n hd[1] = buf[14] ^ buf[30];\n hd[2] = buf[18] ^ buf[34];\n hd[3] = buf[22] ^ buf[38];\n let magic = u16::from_be_bytes([buf[10], buf[11]]);\n let sequence = u32::from_be_bytes([buf[16], buf[17], buf[18], buf[19]]);\n let length = u16::from_be_bytes([buf[24], buf[25]]);\n let flags = buf[36] ^ buf[42];\n let checksum = buf[44] ^ buf[47];\n let header_sum = hd[0].wrapping_add(hd[1]).wrapping_add(hd[2]);\n if magic != 23112 || header_sum == 0 || (flags & 4) == 0 || checksum == 0 {\n return None;\n }\n Some((sequence, length, checksum))\n}",
45
+ "reply": "<<<<<<< SEARCH\n let mut hd = [0u8; 16];\n hd[0] = buf[12] ^ buf[28];\n hd[1] = buf[14] ^ buf[30];\n hd[2] = buf[18] ^ buf[34];\n hd[3] = buf[22] ^ buf[38];\n=======\n let header = [buf[12] ^ buf[28], buf[14] ^ buf[30], buf[18] ^ buf[34], buf[22] ^ buf[38]];\n>>>>>>> REPLACE\n<<<<<<< SEARCH\n let header_sum = hd[0].wrapping_add(hd[1]).wrapping_add(hd[2]);\n=======\n let header_sum = header[0].wrapping_add(header[1]).wrapping_add(header[2]);\n>>>>>>> REPLACE\n"
46
+ },
47
+ {
48
+ "key": "0e8168b89bab1d90bc459c5daaeaffcd14c49fd368aff7099136e17a4d460ad0",
49
+ "filename": "webhook.go",
50
+ "pass": 1,
51
+ "source": "handwritten",
52
+ "origin": "docs/presets.json before",
53
+ "squiggles_before": 7,
54
+ "squiggles_after": 3,
55
+ "content": "func resolveEndpoint(region string) string {\n if region == \"us-east-1\" { return \"https://east.hooks.internal\" }\n if region == \"us-west-2\" { return \"https://west.hooks.internal\" }\n if region == \"eu-central-1\" { return \"https://eu.hooks.internal\" }\n if region == \"ap-northeast-1\" { return \"https://ap.hooks.internal\" }\n if region == \"sa-east-1\" { return \"https://sa.hooks.internal\" }\n return \"https://default.hooks.internal\"\n}\n\nfunc publishAuditEvent(httpClient *Client, region string, auditItem Event) {\n targetUrl := resolveEndpoint(region)\n bodyBytes := serializeJsonBody(auditItem)\n authToken := computeHmacSignature(bodyBytes, httpClient.signingSecret)\n statusCode := transmitHttpPost(httpClient, targetUrl, bodyBytes, authToken)\n recordTelemetryCounter(statusCode, region)\n}\n\nfunc publishBillingEvent(httpClient *Client, region string, billingItem Event) {\n targetUrl := resolveEndpoint(region)\n bodyBytes := serializeJsonBody(billingItem)\n authToken := computeHmacSignature(bodyBytes, httpClient.signingSecret)\n statusCode := transmitHttpPost(httpClient, targetUrl, bodyBytes, authToken)\n recordTelemetryCounter(statusCode, region)\n}",
56
+ "reply": "<<<<<<< SEARCH\nfunc resolveEndpoint(region string) string {\n if region == \"us-east-1\" { return \"https://east.hooks.internal\" }\n if region == \"us-west-2\" { return \"https://west.hooks.internal\" }\n if region == \"eu-central-1\" { return \"https://eu.hooks.internal\" }\n if region == \"ap-northeast-1\" { return \"https://ap.hooks.internal\" }\n if region == \"sa-east-1\" { return \"https://sa.hooks.internal\" }\n return \"https://default.hooks.internal\"\n}\n=======\nvar regionEndpoints = map[string]string{\n \"us-east-1\": \"https://east.hooks.internal\",\n \"us-west-2\": \"https://west.hooks.internal\",\n \"eu-central-1\": \"https://eu.hooks.internal\",\n \"ap-northeast-1\": \"https://ap.hooks.internal\",\n \"sa-east-1\": \"https://sa.hooks.internal\",\n}\n\nfunc resolveEndpoint(region string) string {\n if endpoint, ok := regionEndpoints[region]; ok {\n return endpoint\n }\n return \"https://default.hooks.internal\"\n}\n>>>>>>> REPLACE\n"
57
+ },
58
+ {
59
+ "key": "def4e6ca446326c5e5e83b5127daff28b28d01d51ecd3b2576bd4e7b3293e5a4",
60
+ "filename": "webhook.go",
61
+ "pass": 2,
62
+ "source": "handwritten",
63
+ "origin": "pass-1 output",
64
+ "squiggles_before": 3,
65
+ "squiggles_after": 0,
66
+ "content": "var regionEndpoints = map[string]string{\n \"us-east-1\": \"https://east.hooks.internal\",\n \"us-west-2\": \"https://west.hooks.internal\",\n \"eu-central-1\": \"https://eu.hooks.internal\",\n \"ap-northeast-1\": \"https://ap.hooks.internal\",\n \"sa-east-1\": \"https://sa.hooks.internal\",\n}\n\nfunc resolveEndpoint(region string) string {\n if endpoint, ok := regionEndpoints[region]; ok {\n return endpoint\n }\n return \"https://default.hooks.internal\"\n}\n\nfunc publishAuditEvent(httpClient *Client, region string, auditItem Event) {\n targetUrl := resolveEndpoint(region)\n bodyBytes := serializeJsonBody(auditItem)\n authToken := computeHmacSignature(bodyBytes, httpClient.signingSecret)\n statusCode := transmitHttpPost(httpClient, targetUrl, bodyBytes, authToken)\n recordTelemetryCounter(statusCode, region)\n}\n\nfunc publishBillingEvent(httpClient *Client, region string, billingItem Event) {\n targetUrl := resolveEndpoint(region)\n bodyBytes := serializeJsonBody(billingItem)\n authToken := computeHmacSignature(bodyBytes, httpClient.signingSecret)\n statusCode := transmitHttpPost(httpClient, targetUrl, bodyBytes, authToken)\n recordTelemetryCounter(statusCode, region)\n}",
67
+ "reply": "<<<<<<< SEARCH\nfunc publishAuditEvent(httpClient *Client, region string, auditItem Event) {\n targetUrl := resolveEndpoint(region)\n bodyBytes := serializeJsonBody(auditItem)\n authToken := computeHmacSignature(bodyBytes, httpClient.signingSecret)\n statusCode := transmitHttpPost(httpClient, targetUrl, bodyBytes, authToken)\n recordTelemetryCounter(statusCode, region)\n}\n\nfunc publishBillingEvent(httpClient *Client, region string, billingItem Event) {\n targetUrl := resolveEndpoint(region)\n bodyBytes := serializeJsonBody(billingItem)\n authToken := computeHmacSignature(bodyBytes, httpClient.signingSecret)\n statusCode := transmitHttpPost(httpClient, targetUrl, bodyBytes, authToken)\n recordTelemetryCounter(statusCode, region)\n}\n=======\nfunc publishEvent(httpClient *Client, region string, item Event) {\n endpoint := resolveEndpoint(region)\n body := serializeJsonBody(item)\n signature := computeHmacSignature(body, httpClient.signingSecret)\n status := transmitHttpPost(httpClient, endpoint, body, signature)\n recordTelemetryCounter(status, region)\n}\n\nfunc publishAuditEvent(httpClient *Client, region string, auditItem Event) {\n publishEvent(httpClient, region, auditItem)\n}\n\nfunc publishBillingEvent(httpClient *Client, region string, billingItem Event) {\n publishEvent(httpClient, region, billingItem)\n}\n>>>>>>> REPLACE\n"
68
+ },
69
+ {
70
+ "key": "e06eb41fc26eac74527f6145b19c1796c95d167092e4be3202d71ae449601e02",
71
+ "filename": "engine.js",
72
+ "pass": 1,
73
+ "source": "handwritten",
74
+ "origin": "docs/presets.json before",
75
+ "squiggles_before": 7,
76
+ "squiggles_after": 5,
77
+ "content": "export function unpack(tb, vb, ms) {\n let sp, ft, bo, vo, bv, ds;\n // TODO: clean up golfed trit unpacking\n sp = ms.split(\"|\");\n ft = new Int8Array(tb.length * 5);\n bo = 0;\n vo = 0;\n for (let i = 0; i < tb.length; i++) {\n bv = tb[i];\n ds = i * 5;\n ft[ds] = (((bv % 3) + 1) % 3) - 1;\n ft[ds + 1] = (((Math.floor(bv / 3) % 3) + 1) % 3) - 1;\n ft[ds + 2] = (((Math.floor(bv / 9) % 3) + 1) % 3) - 1;\n ft[ds + 3] = (((Math.floor(bv / 27) % 3) + 1) % 3) - 1;\n ft[ds + 4] = (((Math.floor(bv / 81) % 3) + 1) % 3) - 1;\n }\n try {\n return sp[0].length > 0 && bo >= vo ? ft : (vb || []);\n } catch (e) {}\n}",
78
+ "reply": "<<<<<<< SEARCH\n try {\n return sp[0].length > 0 && bo >= vo ? ft : (vb || []);\n } catch (e) {}\n=======\n if (sp[0].length > 0 && bo >= vo) {\n return ft;\n }\n return vb ? vb : [];\n>>>>>>> REPLACE\n"
79
+ },
80
+ {
81
+ "key": "5026dc2109a6c6eeceb284475e808c2dc1ff30327232235e4296a4fe404b5ce8",
82
+ "filename": "engine.js",
83
+ "pass": 2,
84
+ "source": "handwritten",
85
+ "origin": "pass-1 output",
86
+ "squiggles_before": 5,
87
+ "squiggles_after": 0,
88
+ "content": "export function unpack(tb, vb, ms) {\n let sp, ft, bo, vo, bv, ds;\n // TODO: clean up golfed trit unpacking\n sp = ms.split(\"|\");\n ft = new Int8Array(tb.length * 5);\n bo = 0;\n vo = 0;\n for (let i = 0; i < tb.length; i++) {\n bv = tb[i];\n ds = i * 5;\n ft[ds] = (((bv % 3) + 1) % 3) - 1;\n ft[ds + 1] = (((Math.floor(bv / 3) % 3) + 1) % 3) - 1;\n ft[ds + 2] = (((Math.floor(bv / 9) % 3) + 1) % 3) - 1;\n ft[ds + 3] = (((Math.floor(bv / 27) % 3) + 1) % 3) - 1;\n ft[ds + 4] = (((Math.floor(bv / 81) % 3) + 1) % 3) - 1;\n }\n if (sp[0].length > 0 && bo >= vo) {\n return ft;\n }\n return vb ? vb : [];\n}",
89
+ "reply": "<<<<<<< SEARCH\nexport function unpack(tb, vb, ms) {\n let sp, ft, bo, vo, bv, ds;\n // TODO: clean up golfed trit unpacking\n sp = ms.split(\"|\");\n ft = new Int8Array(tb.length * 5);\n bo = 0;\n vo = 0;\n for (let i = 0; i < tb.length; i++) {\n bv = tb[i];\n ds = i * 5;\n ft[ds] = (((bv % 3) + 1) % 3) - 1;\n ft[ds + 1] = (((Math.floor(bv / 3) % 3) + 1) % 3) - 1;\n ft[ds + 2] = (((Math.floor(bv / 9) % 3) + 1) % 3) - 1;\n ft[ds + 3] = (((Math.floor(bv / 27) % 3) + 1) % 3) - 1;\n ft[ds + 4] = (((Math.floor(bv / 81) % 3) + 1) % 3) - 1;\n }\n if (sp[0].length > 0 && bo >= vo) {\n return ft;\n }\n return vb ? vb : [];\n=======\nexport function unpack(tritBytes, fallback, manifest) {\n const sections = manifest.split(\"|\");\n const trits = new Int8Array(tritBytes.length * 5);\n for (let i = 0; i < tritBytes.length; i++) {\n const packed = tritBytes[i];\n const base = i * 5;\n trits[base] = (((packed % 3) + 1) % 3) - 1;\n trits[base + 1] = (((Math.floor(packed / 3) % 3) + 1) % 3) - 1;\n trits[base + 2] = (((Math.floor(packed / 9) % 3) + 1) % 3) - 1;\n trits[base + 3] = (((Math.floor(packed / 27) % 3) + 1) % 3) - 1;\n trits[base + 4] = (((Math.floor(packed / 81) % 3) + 1) % 3) - 1;\n }\n if (sections[0].length > 0) {\n return trits;\n }\n return fallback ? fallback : [];\n>>>>>>> REPLACE\n"
90
+ },
91
+ {
92
+ "key": "924c910f843823ebb2889d773d89212f28bb465f7197f360417cac41e8cd2e8e",
93
+ "filename": "index.js",
94
+ "pass": 1,
95
+ "source": "handwritten",
96
+ "origin": "github:lukeed/clsx@925494cf31bcd97d3337aacd34e659e80cae7fe2:src/index.js",
97
+ "squiggles_before": 2,
98
+ "squiggles_after": 0,
99
+ "content": "function toVal(mix) {\n\tvar k, y, str='';\n\n\tif (typeof mix === 'string' || typeof mix === 'number') {\n\t\tstr += mix;\n\t} else if (typeof mix === 'object') {\n\t\tif (Array.isArray(mix)) {\n\t\t\tvar len=mix.length;\n\t\t\tfor (k=0; k < len; k++) {\n\t\t\t\tif (mix[k]) {\n\t\t\t\t\tif (y = toVal(mix[k])) {\n\t\t\t\t\t\tstr && (str += ' ');\n\t\t\t\t\t\tstr += y;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t} else {\n\t\t\tfor (y in mix) {\n\t\t\t\tif (mix[y]) {\n\t\t\t\t\tstr && (str += ' ');\n\t\t\t\t\tstr += y;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\n\treturn str;\n}\n\nexport function clsx() {\n\tvar i=0, tmp, x, str='', len=arguments.length;\n\tfor (; i < len; i++) {\n\t\tif (tmp = arguments[i]) {\n\t\t\tif (x = toVal(tmp)) {\n\t\t\t\tstr && (str += ' ');\n\t\t\t\tstr += x\n\t\t\t}\n\t\t}\n\t}\n\treturn str;\n}\n\nexport default clsx;\n",
100
+ "reply": "<<<<<<< SEARCH\n\t\t\t\tif (mix[k]) {\n\t\t\t\t\tif (y = toVal(mix[k])) {\n\t\t\t\t\t\tstr && (str += ' ');\n\t\t\t\t\t\tstr += y;\n\t\t\t\t\t}\n\t\t\t\t}\n=======\n\t\t\t\tif (mix[k] && (y = toVal(mix[k]))) {\n\t\t\t\t\tstr && (str += ' ');\n\t\t\t\t\tstr += y;\n\t\t\t\t}\n>>>>>>> REPLACE"
101
+ }
102
+ ]
103
+ }
server/github_pr.py ADDED
@@ -0,0 +1,375 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """Single-repo pull requests through a GitHub App, plus signed webhook merge tracking.
2
+
3
+ The visitor authorizes the App in a pop-up. The callback page hands a one-time ticket to the
4
+ CodeHygiene page that opened it (`postMessage` restricted to this origin), so a sign-in link
5
+ sent by someone else can never be redeemed by them. The user token lives only in this
6
+ process's memory, is used for one pull request on a repository the visitor can push to, and
7
+ is then revoked.
8
+ """
9
+
10
+ from __future__ import annotations
11
+
12
+ import asyncio
13
+ import hashlib
14
+ import hmac
15
+ import json
16
+ import logging
17
+ import secrets
18
+ import time
19
+ from dataclasses import dataclass
20
+ from typing import Any
21
+ from urllib.parse import quote, urlencode
22
+
23
+ import httpx
24
+ import jwt
25
+ from pydantic import BaseModel, ConfigDict, Field
26
+
27
+ from .config import Settings
28
+ from .hygiene import score_dict
29
+ from .telemetry import Telemetry, pr_key
30
+
31
+ log = logging.getLogger("space.github")
32
+
33
+ API_URL = "https://api.github.com"
34
+ AUTHORIZE_URL = "https://github.com/login/oauth/authorize"
35
+ TOKEN_URL = "https://github.com/login/oauth/access_token"
36
+ PROJECT_URL = "https://github.com/brianbell-x/codehygiene"
37
+ STATE_TTL = 600.0
38
+ TICKET_TTL = 180.0
39
+ MAX_PENDING = 256
40
+ MAX_PR_FILES = 20
41
+ THRESHOLD = 0.55
42
+ NAME_PATTERN = r"^[A-Za-z0-9_.-]{1,100}$"
43
+ TICKET_PATTERN = r"^[0-9a-f]{32}$"
44
+ DEFAULT_MODE = "100644"
45
+ BLOB_MODES = frozenset({DEFAULT_MODE, "100755"})
46
+
47
+
48
+ class PrError(Exception):
49
+ def __init__(self, status: int, code: str, message: str, **extra: Any) -> None:
50
+ super().__init__(message)
51
+ self.status = status
52
+ self.code = code
53
+ self.message = message
54
+ self.extra = extra
55
+
56
+ def payload(self) -> dict[str, Any]:
57
+ return {"code": self.code, "message": self.message, **self.extra}
58
+
59
+
60
+ class PrFile(BaseModel):
61
+ model_config = ConfigDict(extra="forbid")
62
+
63
+ path: str = Field(min_length=1, max_length=400)
64
+ content_after: str = Field(max_length=200_000)
65
+ score_before: int = Field(ge=0, le=100)
66
+ score_after: int = Field(ge=0, le=100)
67
+ squiggles_fixed: int = Field(ge=0, le=10_000)
68
+
69
+
70
+ class CreatePrBody(BaseModel):
71
+ model_config = ConfigDict(extra="forbid")
72
+
73
+ ticket: str = Field(pattern=TICKET_PATTERN)
74
+ owner: str = Field(pattern=NAME_PATTERN)
75
+ repo: str = Field(pattern=NAME_PATTERN)
76
+ base_branch: str = Field("", max_length=255)
77
+ base_commit_sha: str = Field(pattern=r"^[0-9a-f]{40}$")
78
+ files: list[PrFile] = Field(min_length=1, max_length=MAX_PR_FILES)
79
+
80
+
81
+ @dataclass(frozen=True)
82
+ class CheckedFile:
83
+ path: str
84
+ mode: str
85
+ content: str
86
+ score_before: int
87
+ score_after: int
88
+ resolved: int
89
+
90
+
91
+ def signature_valid(secret: str, body: bytes, header: str | None) -> bool:
92
+ if not secret or not header or not header.startswith("sha256="):
93
+ return False
94
+ expected = "sha256=" + hmac.new(secret.encode(), body, hashlib.sha256).hexdigest()
95
+ return hmac.compare_digest(expected, header)
96
+
97
+
98
+ def safe_path(path: str) -> bool:
99
+ parts = path.split("/")
100
+ if path.startswith("/") or "\\" in path or "\0" in path:
101
+ return False
102
+ return all(part not in ("", ".", "..", ".git") for part in parts)
103
+
104
+
105
+ def file_mode(modes: dict[str, str] | None, path: str) -> str | None:
106
+ """Git mode of a base file. A truncated tree has no listing; reading the file later proves it exists."""
107
+ if modes is None:
108
+ return DEFAULT_MODE
109
+ return modes.get(path)
110
+
111
+
112
+ def pr_body(files: list[CheckedFile]) -> str:
113
+ rows = "\n".join(
114
+ f"| `{item.path}` | {item.score_before} → {item.score_after} | {item.resolved} |" for item in files
115
+ )
116
+ return (
117
+ f"Readability cleanup suggested by [CodeHygiene]({PROJECT_URL}).\n\n"
118
+ "| File | Score (of 100) | Issues resolved |\n"
119
+ "| --- | --- | --- |\n"
120
+ f"{rows}\n\n"
121
+ "Each change was re-scored before this pull request was opened. "
122
+ "Behaviour should be unchanged, but run your tests and wait for CI before merging.\n"
123
+ )
124
+
125
+
126
+ def pr_title(count: int) -> str:
127
+ return "Clean up readability in 1 file" if count == 1 else f"Clean up readability in {count} files"
128
+
129
+
130
+ class PullRequests:
131
+ def __init__(
132
+ self,
133
+ settings: Settings,
134
+ telemetry: Telemetry,
135
+ transport: httpx.AsyncBaseTransport | None = None,
136
+ ) -> None:
137
+ self.settings = settings
138
+ self.telemetry = telemetry
139
+ self.http = httpx.AsyncClient(
140
+ timeout=20.0,
141
+ transport=transport,
142
+ headers={
143
+ "Accept": "application/vnd.github+json",
144
+ "X-GitHub-Api-Version": "2022-11-28",
145
+ "User-Agent": "codehygiene-space",
146
+ },
147
+ )
148
+ self._states: dict[str, float] = {}
149
+ self._tickets: dict[str, tuple[str, float]] = {}
150
+ self._slug = settings.github_app_slug
151
+
152
+ @property
153
+ def enabled(self) -> bool:
154
+ return self.settings.github_pr_enabled
155
+
156
+ async def close(self) -> None:
157
+ await self.http.aclose()
158
+
159
+ def _sweep(self) -> None:
160
+ now = time.monotonic()
161
+ self._states = {state: expires for state, expires in self._states.items() if expires > now}
162
+ self._tickets = {key: entry for key, entry in self._tickets.items() if entry[1] > now}
163
+
164
+ def login_url(self) -> str:
165
+ self._sweep()
166
+ if len(self._states) >= MAX_PENDING:
167
+ raise PrError(429, "busy", "Too many sign-ins in progress. Try again shortly.")
168
+ state = secrets.token_urlsafe(24)
169
+ self._states[state] = time.monotonic() + STATE_TTL
170
+ query = urlencode({"client_id": self.settings.github_app_client_id, "state": state})
171
+ return f"{AUTHORIZE_URL}?{query}"
172
+
173
+ async def complete_login(self, code: str, state: str) -> str:
174
+ """Exchange the code and return a one-time ticket for the page that opened the pop-up."""
175
+ self._sweep()
176
+ if self._states.pop(state, None) is None:
177
+ raise PrError(400, "expired", "This sign-in expired. Start again from CodeHygiene.")
178
+ if len(self._tickets) >= MAX_PENDING:
179
+ raise PrError(429, "busy", "Too many sign-ins in progress. Try again shortly.")
180
+ return self.issue_ticket(await self._exchange_code(code))
181
+
182
+ def issue_ticket(self, token: str) -> str:
183
+ """Hold a user token in memory under a fresh one-time ticket."""
184
+ ticket = secrets.token_hex(16)
185
+ self._tickets[ticket] = (token, time.monotonic() + TICKET_TTL)
186
+ return ticket
187
+
188
+ async def create(self, body: CreatePrBody) -> dict[str, Any]:
189
+ self._sweep()
190
+ entry = self._tickets.pop(body.ticket, None)
191
+ if entry is None:
192
+ raise PrError(401, "login_required", "Sign in with GitHub again.")
193
+ token = entry[0]
194
+ try:
195
+ return await self._open(token, body)
196
+ finally:
197
+ await self._revoke(token)
198
+
199
+ async def _exchange_code(self, code: str) -> str:
200
+ response = await self.http.post(
201
+ TOKEN_URL,
202
+ data={
203
+ "client_id": self.settings.github_app_client_id,
204
+ "client_secret": self.settings.github_app_client_secret,
205
+ "code": code,
206
+ },
207
+ headers={"Accept": "application/json"},
208
+ )
209
+ token = response.json().get("access_token") if response.status_code == 200 else None
210
+ if not token:
211
+ raise PrError(401, "login_failed", "GitHub sign-in failed.")
212
+ return token
213
+
214
+ async def _revoke(self, token: str) -> None:
215
+ """Best effort: the token is dropped from memory either way and expires on GitHub's side."""
216
+ try:
217
+ response = await self.http.request(
218
+ "DELETE",
219
+ f"{API_URL}/applications/{self.settings.github_app_client_id}/token",
220
+ auth=(self.settings.github_app_client_id, self.settings.github_app_client_secret),
221
+ json={"access_token": token},
222
+ )
223
+ except httpx.HTTPError as error:
224
+ log.warning("token revoke failed: %s", type(error).__name__)
225
+ return
226
+ if response.status_code not in (204, 404):
227
+ log.warning("token revoke returned HTTP %d", response.status_code)
228
+
229
+ def _app_jwt(self) -> str:
230
+ now = int(time.time())
231
+ claims = {"iat": now - 60, "exp": now + 540, "iss": self.settings.github_app_id}
232
+ return jwt.encode(claims, self.settings.github_app_private_key, algorithm="RS256")
233
+
234
+ async def _call(
235
+ self, method: str, path: str, *, token: str | None = None, app: bool = False, **kwargs: Any
236
+ ) -> httpx.Response:
237
+ bearer = self._app_jwt() if app else token
238
+ headers = {**kwargs.pop("headers", {}), "Authorization": f"Bearer {bearer}"}
239
+ return await self.http.request(method, f"{API_URL}{path}", headers=headers, **kwargs)
240
+
241
+ async def _json(self, method: str, path: str, *, token: str, expect: int = 200, **kwargs: Any) -> Any:
242
+ response = await self._call(method, path, token=token, **kwargs)
243
+ if response.status_code != expect:
244
+ raise PrError(502, "github_error", f"GitHub returned HTTP {response.status_code}.")
245
+ return response.json()
246
+
247
+ async def _app_slug(self) -> str:
248
+ if not self._slug:
249
+ response = await self._call("GET", "/app", app=True)
250
+ self._slug = response.json().get("slug", "") if response.status_code == 200 else ""
251
+ return self._slug
252
+
253
+ async def _install_url(self, repo: dict[str, Any] | None) -> str:
254
+ url = f"https://github.com/apps/{await self._app_slug()}/installations/new"
255
+ if repo is None:
256
+ return url
257
+ query = urlencode({"suggested_target_id": repo["owner"]["id"], "repository_ids[]": repo["id"]})
258
+ return f"{url}/permissions?{query}"
259
+
260
+ async def _installed(self, slug: str) -> bool:
261
+ response = await self._call("GET", f"/repos/{slug}/installation", app=True)
262
+ return response.status_code == 200
263
+
264
+ async def _open(self, token: str, body: CreatePrBody) -> dict[str, Any]:
265
+ slug = f"{body.owner}/{body.repo}"
266
+ response = await self._call("GET", f"/repos/{slug}", token=token)
267
+ if response.status_code == 404:
268
+ if not await self._installed(slug):
269
+ raise PrError(409, "not_installed", "Install the app on this repository first.",
270
+ install_url=await self._install_url(None))
271
+ raise PrError(404, "not_found", "Repository not found.")
272
+ if response.status_code != 200:
273
+ raise PrError(502, "github_error", f"GitHub returned HTTP {response.status_code}.")
274
+ repo = response.json()
275
+ if not repo.get("permissions", {}).get("push"):
276
+ raise PrError(403, "no_push", "You need write access to open a pull request here.")
277
+ if not await self._installed(slug):
278
+ raise PrError(409, "not_installed", "Install the app on this repository first.",
279
+ install_url=await self._install_url(repo))
280
+
281
+ base_branch = body.base_branch or repo["default_branch"]
282
+ branch_path = f"/repos/{slug}/branches/{quote(base_branch, safe='')}"
283
+ branch = await self._call("GET", branch_path, token=token)
284
+ if branch.status_code != 200:
285
+ raise PrError(422, "bad_base", "The base must be a branch.")
286
+ commit = await self._call("GET", f"/repos/{slug}/git/commits/{body.base_commit_sha}", token=token)
287
+ if commit.status_code != 200:
288
+ raise PrError(422, "bad_base", "The base commit was not found.")
289
+ base_tree = commit.json()["tree"]["sha"]
290
+
291
+ files = await self._checked_files(token, slug, base_tree, body)
292
+ head = await self._commit(token, slug, base_tree, body.base_commit_sha, files)
293
+ created = await self._json("POST", f"/repos/{slug}/pulls", token=token, expect=201, json={
294
+ "title": pr_title(len(files)),
295
+ "head": head,
296
+ "base": base_branch,
297
+ "body": pr_body(files),
298
+ "maintainer_can_modify": True,
299
+ })
300
+ self.telemetry.record_pr_opened(pr_key(repo["id"], created["number"]))
301
+ return {"url": created["html_url"], "number": created["number"]}
302
+
303
+ async def _modes(self, token: str, slug: str, base_tree: str) -> dict[str, str] | None:
304
+ tree = await self._json("GET", f"/repos/{slug}/git/trees/{base_tree}?recursive=1", token=token)
305
+ if tree.get("truncated"):
306
+ return None
307
+ return {node["path"]: node["mode"] for node in tree.get("tree", []) if node.get("type") == "blob"}
308
+
309
+ async def _checked_files(
310
+ self, token: str, slug: str, base_tree: str, body: CreatePrBody
311
+ ) -> list[CheckedFile]:
312
+ """Re-read each base file at the commit and re-score both versions; the client's numbers are not used."""
313
+ if len({item.path for item in body.files}) != len(body.files):
314
+ raise PrError(422, "bad_path", "Each file may appear once.")
315
+ modes = await self._modes(token, slug, base_tree)
316
+ checked: list[CheckedFile] = []
317
+ for item in body.files:
318
+ mode = file_mode(modes, item.path)
319
+ if not safe_path(item.path) or mode not in BLOB_MODES:
320
+ raise PrError(422, "bad_path", "A file is not in the base commit.")
321
+ raw = await self._call(
322
+ "GET", f"/repos/{slug}/contents/{quote(item.path)}?ref={body.base_commit_sha}",
323
+ token=token, headers={"Accept": "application/vnd.github.raw"},
324
+ )
325
+ if raw.status_code != 200:
326
+ raise PrError(422, "bad_path", "A file is not in the base commit.")
327
+ before, after = await asyncio.gather(
328
+ asyncio.to_thread(score_dict, raw.text, THRESHOLD),
329
+ asyncio.to_thread(score_dict, item.content_after, THRESHOLD),
330
+ )
331
+ resolved = len(before["squiggles"]) - len(after["squiggles"])
332
+ if item.content_after == raw.text or resolved <= 0:
333
+ raise PrError(422, "not_cleaner", "Every file must have fewer issues than its base.")
334
+ checked.append(CheckedFile(
335
+ item.path, mode, item.content_after,
336
+ round(before["score"] * 100), round(after["score"] * 100), resolved,
337
+ ))
338
+ return checked
339
+
340
+ async def _commit(
341
+ self, token: str, slug: str, base_tree: str, base_sha: str, files: list[CheckedFile]
342
+ ) -> str:
343
+ entries = []
344
+ for item in files:
345
+ blob = await self._json("POST", f"/repos/{slug}/git/blobs", token=token, expect=201,
346
+ json={"content": item.content, "encoding": "utf-8"})
347
+ entries.append({"path": item.path, "mode": item.mode, "type": "blob", "sha": blob["sha"]})
348
+ tree = await self._json("POST", f"/repos/{slug}/git/trees", token=token, expect=201,
349
+ json={"base_tree": base_tree, "tree": entries})
350
+ commit = await self._json("POST", f"/repos/{slug}/git/commits", token=token, expect=201, json={
351
+ "message": pr_title(len(files)),
352
+ "tree": tree["sha"],
353
+ "parents": [base_sha],
354
+ })
355
+ for _ in range(3):
356
+ branch = f"codehygiene/{secrets.token_hex(4)}"
357
+ ref = await self._call("POST", f"/repos/{slug}/git/refs", token=token,
358
+ json={"ref": f"refs/heads/{branch}", "sha": commit["sha"]})
359
+ if ref.status_code == 201:
360
+ return branch
361
+ if ref.status_code != 422:
362
+ break
363
+ raise PrError(502, "github_error", "Could not create the branch.")
364
+
365
+ def webhook(self, event: str, body: bytes, signature: str | None) -> dict[str, Any]:
366
+ if not signature_valid(self.settings.github_webhook_secret, body, signature):
367
+ raise PrError(401, "bad_signature", "Invalid signature.")
368
+ if event != "pull_request":
369
+ return {"counted": False}
370
+ payload = json.loads(body)
371
+ if payload.get("action") != "closed" or "pull_request" not in payload:
372
+ return {"counted": False}
373
+ pull = payload["pull_request"]
374
+ key = pr_key(payload["repository"]["id"], pull["number"])
375
+ return {"counted": self.telemetry.record_pr_closed(key, merged=bool(pull.get("merged")))}
server/guards.py ADDED
@@ -0,0 +1,69 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from __future__ import annotations
2
+
3
+ from dataclasses import dataclass, field
4
+ from typing import Any
5
+
6
+ from .hygiene import score_dict
7
+ from .vendored_guards import check_basic_syntax, is_bloated, is_gutted
8
+ from .vendored_pipeline import apply_changes
9
+
10
+
11
+ @dataclass
12
+ class Verdict:
13
+ accepted: bool
14
+ reason: str | None
15
+ content: str
16
+ applied_blocks: int = 0
17
+ squiggles_before: int = 0
18
+ squiggles_after: int | None = None
19
+ hygiene: dict[str, Any] = field(default_factory=dict)
20
+ reply: str = ""
21
+
22
+ @property
23
+ def score_after(self) -> float | None:
24
+ return self.hygiene.get("score")
25
+
26
+
27
+ def verify_patch(
28
+ content: str,
29
+ reply: str,
30
+ filename: str,
31
+ threshold: float,
32
+ before_hygiene: dict[str, Any],
33
+ ) -> Verdict:
34
+ squiggles_before = len(before_hygiene["squiggles"])
35
+ rejected = Verdict(False, None, content, squiggles_before=squiggles_before)
36
+ if not reply.strip():
37
+ rejected.reason = "empty_output"
38
+ return rejected
39
+ try:
40
+ updated, applied = apply_changes(content, reply)
41
+ except ValueError:
42
+ rejected.reason = "search_miss"
43
+ return rejected
44
+ rejected.applied_blocks = applied
45
+ if applied == 0 or updated == content:
46
+ rejected.reason = "no_blocks_applied"
47
+ return rejected
48
+ if filename and not check_basic_syntax(updated, filename):
49
+ rejected.reason = "invalid_syntax"
50
+ return rejected
51
+ if is_gutted(content, updated):
52
+ rejected.reason = "gutted_file"
53
+ return rejected
54
+ if is_bloated(content, updated):
55
+ rejected.reason = "line_bloat"
56
+ return rejected
57
+
58
+ after = score_dict(updated, threshold)
59
+ squiggles_after = len(after["squiggles"])
60
+ accepted = squiggles_after < squiggles_before
61
+ return Verdict(
62
+ accepted=accepted,
63
+ reason=None if accepted else "no_improvement",
64
+ content=updated if accepted else content,
65
+ applied_blocks=applied,
66
+ squiggles_before=squiggles_before,
67
+ squiggles_after=squiggles_after,
68
+ hygiene=after,
69
+ )
server/hygiene.py ADDED
@@ -0,0 +1,28 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from __future__ import annotations
2
+
3
+ import hashlib
4
+ import importlib.util
5
+ import sys
6
+ from pathlib import Path
7
+ from typing import Any
8
+
9
+ REPO_ROOT = Path(__file__).resolve().parents[2]
10
+ LOCAL_PACKAGE = REPO_ROOT / "codehygiene" / "__init__.py"
11
+
12
+ if importlib.util.find_spec("codehygiene") is None and LOCAL_PACKAGE.is_file():
13
+ sys.path.insert(0, str(REPO_ROOT))
14
+
15
+ import codehygiene
16
+
17
+ WEIGHTS_PATH = Path(codehygiene.__file__).with_name("weights.npz")
18
+ SCORER_VERSION = str(codehygiene.__version__)
19
+ # Version plus a weights digest: a recompile that keeps the version string still counts as a new scorer.
20
+ SCORER_ID = f"{SCORER_VERSION}+{hashlib.sha256(WEIGHTS_PATH.read_bytes()).hexdigest()[:12]}"
21
+
22
+
23
+ def score_dict(content: str, threshold: float) -> dict[str, Any]:
24
+ return codehygiene.score(content, threshold=threshold).to_dict()
25
+
26
+
27
+ def score_value(content: str) -> float:
28
+ return float(codehygiene.score(content).score)
server/leaderboard.py ADDED
@@ -0,0 +1,437 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """Public leaderboard of GitHub repos, each scored on the server. See space/TELEMETRY.md."""
2
+
3
+ from __future__ import annotations
4
+
5
+ import asyncio
6
+ import hashlib
7
+ import io
8
+ import json
9
+ import logging
10
+ import re
11
+ import tarfile
12
+ import time
13
+ from collections.abc import Callable, Iterable
14
+ from dataclasses import asdict, dataclass
15
+ from datetime import datetime, timezone
16
+ from typing import Any
17
+ from urllib.parse import quote
18
+
19
+ import httpx
20
+ from pydantic import BaseModel, ConfigDict, Field, field_validator
21
+
22
+ from .config import Settings
23
+ from .hygiene import SCORER_ID, SCORER_VERSION, score_value
24
+
25
+ log = logging.getLogger("space.leaderboard")
26
+
27
+ API = "https://api.github.com"
28
+ ARCHIVES = "https://codeload.github.com"
29
+ BOARD_FILE = "leaderboard.json"
30
+ BOARD_VERSION = 1
31
+ BOARD_LIMIT = 100
32
+
33
+ MIN_FILES = 5
34
+ MIN_LINES = 1_000
35
+ MAX_ARCHIVE_BYTES = 80 * 1024 * 1024
36
+ MAX_FILE_BYTES = 150_000
37
+ MAX_SCORED_FILES = 300
38
+ MAX_SCORED_LINES = 20_000
39
+
40
+ FRESH_SECONDS = 24 * 3600
41
+ SEED_FRESH_SECONDS = 7 * 24 * 3600
42
+ PAST_SECONDS = 365 * 24 * 3600
43
+ MAX_WAITING = 3
44
+ GONE_CODES = frozenset({"not_found", "too_small", "fork", "duplicate"})
45
+
46
+ RANKED_EXTENSIONS = frozenset({"py", "ts", "tsx", "js", "jsx", "mjs", "cjs", "go", "rs"})
47
+ SKIPPED_DIRS = frozenset({
48
+ "test", "tests", "__tests__", "spec", "specs", "e2e", "testdata", "fixtures", "__fixtures__",
49
+ "__mocks__", "vendor", "vendored", "third_party", "thirdparty", "node_modules", "dist",
50
+ "build", "out", "target", "generated", "__generated__",
51
+ })
52
+ SKIPPED_SUFFIXES = (".d.ts", ".pb.go", "_pb2.py", "_test.go", "_test.py", "_generated.go")
53
+ SKIPPED_INFIXES = (".test.", ".spec.", ".generated.", ".min.")
54
+ GENERATED_MARKS = (
55
+ "code generated", "do not edit", "@generated", "auto-generated", "autogenerated", "file generated",
56
+ )
57
+ REPO_NAME = re.compile(r"^[A-Za-z0-9][A-Za-z0-9-]{0,38}/[A-Za-z0-9._-]{1,100}$")
58
+ COMMIT_SHA = re.compile(r"^[0-9a-f]{40}$")
59
+
60
+ SEED_REPOS = (
61
+ "BurntSushi/ripgrep", "honojs/hono", "fastapi/fastapi", "karpathy/nanoGPT",
62
+ "openai/codex", "sst/opencode", "pallets/flask", "psf/requests",
63
+ "astral-sh/ruff", "junegunn/fzf", "charmbracelet/bubbletea", "gin-gonic/gin",
64
+ "colinhacks/zod", "vitejs/vite", "expressjs/express", "tokio-rs/axum",
65
+ "tinygrad/tinygrad", "pydantic/pydantic", "sindresorhus/ky", "anthropics/claude-code",
66
+ )
67
+
68
+ Scorer = Callable[[str], float]
69
+
70
+
71
+ class RankError(Exception):
72
+ def __init__(self, status: int, code: str, message: str, **extra: Any) -> None:
73
+ super().__init__(message)
74
+ self.status = status
75
+ self.code = code
76
+ self.message = message
77
+ self.extra = extra
78
+
79
+ def payload(self) -> dict[str, Any]:
80
+ return {"code": self.code, "message": self.message, **self.extra}
81
+
82
+
83
+ class RankBody(BaseModel):
84
+ model_config = ConfigDict(extra="forbid")
85
+
86
+ owner: str = Field(pattern=r"^[A-Za-z0-9][A-Za-z0-9-]{0,38}$")
87
+ repo: str = Field(pattern=r"^[A-Za-z0-9._-]{1,100}$")
88
+
89
+ @field_validator("repo")
90
+ @classmethod
91
+ def not_dots(cls, value: str) -> str:
92
+ if not value.strip("."):
93
+ raise ValueError("not a repository name")
94
+ return value
95
+
96
+
97
+ @dataclass(frozen=True)
98
+ class Measure:
99
+ sha: str
100
+ files: int
101
+ lines: int
102
+ score: float
103
+
104
+
105
+ @dataclass(frozen=True)
106
+ class Entry:
107
+ repo: str
108
+ score: float
109
+ files: int
110
+ lines: int
111
+ sha: str
112
+ ranked_at: float
113
+ past_score: float | None = None
114
+ past_sha: str = ""
115
+ scorer: str = ""
116
+
117
+ @property
118
+ def key(self) -> str:
119
+ return self.repo.lower()
120
+
121
+ @property
122
+ def change(self) -> float | None:
123
+ return None if self.past_score is None else round(self.score - self.past_score, 1)
124
+
125
+ def row(self) -> dict[str, Any]:
126
+ return {
127
+ "repo": self.repo,
128
+ "score": self.score,
129
+ "past_score": self.past_score,
130
+ "change": self.change,
131
+ "files": self.files,
132
+ "lines": self.lines,
133
+ "sha": self.sha,
134
+ }
135
+
136
+
137
+ def line_count(text: str) -> int:
138
+ return text.count("\n") + (0 if text.endswith("\n") else 1)
139
+
140
+
141
+ def ranked_path(path: str) -> bool:
142
+ """Source files that count toward a repo's score: no tests, vendored, generated or minified code."""
143
+ parts = path.lower().split("/")
144
+ name = parts[-1]
145
+ if name.rpartition(".")[2] not in RANKED_EXTENSIONS:
146
+ return False
147
+ if any(part in SKIPPED_DIRS or part.startswith(".") for part in parts[:-1]):
148
+ return False
149
+ if name.startswith("test_") or name == "conftest.py" or name.endswith(SKIPPED_SUFFIXES):
150
+ return False
151
+ return not any(infix in name for infix in SKIPPED_INFIXES)
152
+
153
+
154
+ def generated(text: str) -> bool:
155
+ head = text[:600].lower()
156
+ return any(mark in head for mark in GENERATED_MARKS)
157
+
158
+
159
+ def archive_files(archive: bytes) -> tuple[str, list[tuple[str, str]]]:
160
+ """Commit SHA and every ranked file of a GitHub tar.gz, read in memory; nothing touches disk."""
161
+ files = []
162
+ with tarfile.open(fileobj=io.BytesIO(archive), mode="r:gz") as tar:
163
+ sha = str(tar.pax_headers.get("comment", ""))
164
+ for member in tar:
165
+ path = member.name.partition("/")[2]
166
+ if not member.isfile() or member.size > MAX_FILE_BYTES or not ranked_path(path):
167
+ continue
168
+ handle = tar.extractfile(member)
169
+ text = handle.read().decode("utf-8", errors="replace") if handle else ""
170
+ if text.strip() and "�" not in text and not generated(text):
171
+ files.append((path, text))
172
+ return (sha if COMMIT_SHA.match(sha) else ""), files
173
+
174
+
175
+ def sample(files: list[tuple[str, str]]) -> list[str]:
176
+ """A stable subset within the scoring budget, picked by path hash so a rerun scores the same files."""
177
+ ordered = sorted(files, key=lambda item: hashlib.sha256(item[0].encode()).digest())
178
+ picked: list[str] = []
179
+ lines = 0
180
+ for _, text in ordered:
181
+ if len(picked) >= MAX_SCORED_FILES or lines >= MAX_SCORED_LINES:
182
+ break
183
+ picked.append(text)
184
+ lines += line_count(text)
185
+ return picked
186
+
187
+
188
+ def measure(archive: bytes, scorer: Scorer) -> Measure:
189
+ """Line-weighted mean file score, 0 to 100, over the sampled files."""
190
+ sha, files = archive_files(archive)
191
+ picked = sample(files)
192
+ weights = [line_count(text) for text in picked]
193
+ weighted = sum(scorer(text) * weight for text, weight in zip(picked, weights))
194
+ score = round(100 * weighted / sum(weights), 1) if picked else 0.0
195
+ total_lines = sum(line_count(text) for _, text in files)
196
+ return Measure(sha=sha, files=len(files), lines=total_lines, score=score)
197
+
198
+
199
+ def qualifies(files: int, lines: int) -> bool:
200
+ return files >= MIN_FILES and lines >= MIN_LINES
201
+
202
+
203
+ def iso(stamp: float) -> str:
204
+ return datetime.fromtimestamp(stamp, timezone.utc).strftime("%Y-%m-%dT%H:%M:%SZ")
205
+
206
+
207
+ def _score(value: Any) -> float | None:
208
+ number = value if isinstance(value, (int, float)) and not isinstance(value, bool) else None
209
+ return float(number) if number is not None and 0 <= number <= 100 else None
210
+
211
+
212
+ def entry_from(raw: Any) -> Entry | None:
213
+ """An entry from a saved board, or None when any field is missing or out of range."""
214
+ if not isinstance(raw, dict):
215
+ return None
216
+ repo, sha, files, lines = raw.get("repo"), raw.get("sha"), raw.get("files"), raw.get("lines")
217
+ score, stamp = _score(raw.get("score")), raw.get("ranked_at")
218
+ shaped = (
219
+ isinstance(repo, str) and REPO_NAME.match(repo)
220
+ and isinstance(sha, str) and COMMIT_SHA.match(sha)
221
+ and isinstance(files, int) and isinstance(lines, int) and qualifies(files, lines)
222
+ and score is not None and isinstance(stamp, (int, float))
223
+ )
224
+ if not shaped:
225
+ return None
226
+ past_sha = raw.get("past_sha", "")
227
+ past_score = _score(raw.get("past_score"))
228
+ has_past = past_score is not None and isinstance(past_sha, str) and COMMIT_SHA.match(past_sha)
229
+ scorer = raw.get("scorer")
230
+ return Entry(
231
+ repo=repo, score=score, files=files, lines=lines, sha=sha, ranked_at=float(stamp),
232
+ past_score=past_score if has_past else None, past_sha=past_sha if has_past else "",
233
+ scorer=scorer[:64] if isinstance(scorer, str) else "",
234
+ )
235
+
236
+
237
+ class Leaderboard:
238
+ """Repos ranked by server-side score. Holds only public repo names, commit SHAs and numbers.
239
+
240
+ Every entry records the scorer that produced it. Only entries from the running scorer are listed;
241
+ older ones stay in the file until `maintain` re-ranks or drops them.
242
+ """
243
+
244
+ def __init__(
245
+ self,
246
+ settings: Settings,
247
+ transport: httpx.AsyncBaseTransport | None = None,
248
+ scorer: Scorer | None = None,
249
+ clock: Callable[[], float] = time.time,
250
+ scorer_id: str = SCORER_ID,
251
+ ) -> None:
252
+ self.entries: dict[str, Entry] = {}
253
+ self.dirty = False
254
+ self.scorer = scorer or score_value
255
+ self.scorer_id = scorer_id
256
+ self.clock = clock
257
+ self.api_headers = {"Accept": "application/vnd.github+json", "X-GitHub-Api-Version": "2022-11-28"}
258
+ if settings.github_token:
259
+ self.api_headers["Authorization"] = f"Bearer {settings.github_token}"
260
+ self.client = httpx.AsyncClient(
261
+ transport=transport,
262
+ headers={"User-Agent": "codehygiene-space"},
263
+ timeout=httpx.Timeout(60.0, connect=10.0),
264
+ follow_redirects=True,
265
+ )
266
+ self._turn = asyncio.Lock()
267
+ self._waiting = 0
268
+
269
+ async def close(self) -> None:
270
+ await self.client.aclose()
271
+
272
+ def merge(self, raw: Any) -> None:
273
+ rows = raw.get("entries") if isinstance(raw, dict) else None
274
+ for item in rows if isinstance(rows, list) else []:
275
+ entry = entry_from(item)
276
+ known = self.entries.get(entry.key) if entry else None
277
+ if entry and (known is None or entry.ranked_at > known.ranked_at):
278
+ self.entries[entry.key] = entry
279
+
280
+ def snapshot(self) -> str:
281
+ rows = [asdict(entry) for entry in sorted(self.entries.values(), key=lambda entry: entry.key)]
282
+ return json.dumps({"version": BOARD_VERSION, "entries": rows}, indent=2, sort_keys=True) + "\n"
283
+
284
+ def current(self) -> list[Entry]:
285
+ return [entry for entry in self.entries.values() if entry.scorer == self.scorer_id]
286
+
287
+ def stale(self) -> list[Entry]:
288
+ return [entry for entry in self.entries.values() if entry.scorer != self.scorer_id]
289
+
290
+ def cleanest(self) -> list[Entry]:
291
+ return sorted(self.current(), key=lambda entry: (-entry.score, entry.key))
292
+
293
+ def improved(self) -> list[Entry]:
294
+ rising = [entry for entry in self.current() if (entry.change or 0) >= 1]
295
+ return sorted(rising, key=lambda entry: (-(entry.change or 0), entry.key))
296
+
297
+ def public(self) -> dict[str, Any]:
298
+ stamps = [entry.ranked_at for entry in self.current()]
299
+ return {
300
+ "total": len(stamps),
301
+ "updated_at": iso(max(stamps)) if stamps else None,
302
+ "scorer_version": SCORER_VERSION,
303
+ "cleanest": [entry.row() for entry in self.cleanest()[:BOARD_LIMIT]],
304
+ "improved": [entry.row() for entry in self.improved()[:BOARD_LIMIT]],
305
+ }
306
+
307
+ def standing(self, entry: Entry) -> dict[str, Any]:
308
+ order = [ranked.key for ranked in self.cleanest()]
309
+ return {**entry.row(), "rank": order.index(entry.key) + 1, "total": len(order)}
310
+
311
+ def _fresh(self, entry: Entry | None, seconds: float) -> bool:
312
+ return bool(entry) and entry.scorer == self.scorer_id and self.clock() - entry.ranked_at < seconds
313
+
314
+ async def rank(self, owner: str, repo: str) -> dict[str, Any]:
315
+ known = self.entries.get(f"{owner}/{repo}".lower())
316
+ if known and self._fresh(known, FRESH_SECONDS):
317
+ return self.standing(known)
318
+ if self._waiting >= MAX_WAITING:
319
+ raise RankError(429, "busy", "Ranking is busy. Try again shortly.")
320
+ self._waiting += 1
321
+ try:
322
+ async with self._turn:
323
+ entry = await self._build(owner, repo)
324
+ finally:
325
+ self._waiting -= 1
326
+ self.entries[entry.key] = entry
327
+ self.dirty = True
328
+ return self.standing(entry)
329
+
330
+ async def maintain(self, seeds: Iterable[str], pause: float) -> None:
331
+ """Ranks seeds that are missing or old, then re-ranks entries from an older scorer."""
332
+ for slug in seeds:
333
+ if not self._fresh(self.entries.get(slug.lower()), SEED_FRESH_SECONDS):
334
+ await self._refresh(slug)
335
+ await asyncio.sleep(pause)
336
+ for entry in self.stale():
337
+ await self._refresh(entry.repo)
338
+ await asyncio.sleep(pause)
339
+
340
+ async def _refresh(self, slug: str) -> None:
341
+ """Re-ranks one repo; an older entry is dropped when the repo can no longer be listed."""
342
+ owner, repo = slug.split("/")
343
+ try:
344
+ await self.rank(owner, repo)
345
+ except RankError as error:
346
+ log.info("refresh skipped: %s", error.code)
347
+ if error.code in GONE_CODES:
348
+ self._drop_stale(slug)
349
+ except httpx.HTTPError as error:
350
+ log.info("refresh skipped: %s", type(error).__name__)
351
+
352
+ def _drop_stale(self, slug: str) -> None:
353
+ known = self.entries.get(slug.lower())
354
+ if known and known.scorer != self.scorer_id:
355
+ del self.entries[known.key]
356
+ self.dirty = True
357
+
358
+ async def _build(self, owner: str, repo: str) -> Entry:
359
+ meta = await self._meta(owner, repo)
360
+ slug = str(meta.get("full_name") or f"{owner}/{repo}")
361
+ ref = str(meta.get("default_branch") or "HEAD")
362
+ current = await self._measure(slug, ref)
363
+ if not qualifies(current.files, current.lines) or not current.sha:
364
+ raise RankError(
365
+ 422, "too_small", f"Needs {MIN_FILES}+ code files and {MIN_LINES:,}+ lines.",
366
+ files=current.files, lines=current.lines,
367
+ )
368
+ twin = self._twin(current.sha, slug)
369
+ if twin:
370
+ raise RankError(409, "duplicate", f"Already ranked as {twin.repo}.", repo=twin.repo)
371
+ past = await self._past(slug, ref, current.sha) if meta else None
372
+ return Entry(
373
+ repo=slug, score=current.score, files=current.files, lines=current.lines, sha=current.sha,
374
+ ranked_at=self.clock(),
375
+ past_score=past.score if past else None,
376
+ past_sha=past.sha if past else "",
377
+ scorer=self.scorer_id,
378
+ )
379
+
380
+ def _twin(self, sha: str, slug: str) -> Entry | None:
381
+ """An entry for the same commit under another name, which is a fork or mirror."""
382
+ others = (entry for entry in self.current() if entry.key != slug.lower())
383
+ return next((entry for entry in others if entry.sha == sha), None)
384
+
385
+ async def _meta(self, owner: str, repo: str) -> dict[str, Any]:
386
+ """Repo metadata, or {} when the API is rate limited; archives still download without it."""
387
+ response = await self.client.get(f"{API}/repos/{owner}/{repo}", headers=self.api_headers)
388
+ if response.status_code in (403, 429):
389
+ log.info("GitHub API rate limited; ranking from the archive alone")
390
+ return {}
391
+ if response.status_code == 404:
392
+ raise RankError(404, "not_found", "Repo not found.")
393
+ response.raise_for_status()
394
+ meta = response.json()
395
+ if meta.get("private"):
396
+ raise RankError(404, "not_found", "Repo not found.")
397
+ if meta.get("fork"):
398
+ raise RankError(422, "fork", "Forks are not ranked.")
399
+ return meta
400
+
401
+ async def _measure(self, slug: str, ref: str) -> Measure:
402
+ archive = await self._archive(slug, ref)
403
+ return await asyncio.to_thread(measure, archive, self.scorer)
404
+
405
+ async def _archive(self, slug: str, ref: str) -> bytes:
406
+ chunks: list[bytes] = []
407
+ size = 0
408
+ url = f"{ARCHIVES}/{slug}/tar.gz/{quote(ref, safe='/')}"
409
+ async with self.client.stream("GET", url) as response:
410
+ if response.status_code == 404:
411
+ raise RankError(404, "not_found", "Repo not found.")
412
+ response.raise_for_status()
413
+ async for chunk in response.aiter_bytes():
414
+ size += len(chunk)
415
+ if size > MAX_ARCHIVE_BYTES:
416
+ raise RankError(413, "too_large", "Repo is too large to rank.")
417
+ chunks.append(chunk)
418
+ return b"".join(chunks)
419
+
420
+ async def _past(self, slug: str, ref: str, current_sha: str) -> Measure | None:
421
+ """The same branch a year earlier, for Most improved. None when it cannot be scored."""
422
+ try:
423
+ return await self._past_measure(slug, ref, current_sha)
424
+ except (RankError, httpx.HTTPError) as error:
425
+ log.info("past score unavailable: %s", type(error).__name__)
426
+ return None
427
+
428
+ async def _past_measure(self, slug: str, ref: str, current_sha: str) -> Measure | None:
429
+ params = {"sha": ref, "until": iso(self.clock() - PAST_SECONDS), "per_page": "1"}
430
+ url = f"{API}/repos/{slug}/commits"
431
+ response = await self.client.get(url, params=params, headers=self.api_headers)
432
+ rows = response.json() if response.status_code == 200 else []
433
+ sha = rows[0].get("sha", "") if rows and isinstance(rows[0], dict) else ""
434
+ if not COMMIT_SHA.match(sha) or sha == current_sha:
435
+ return None
436
+ past = await self._measure(slug, sha)
437
+ return past if qualifies(past.files, past.lines) else None
server/prompt.py ADDED
@@ -0,0 +1,24 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from __future__ import annotations
2
+
3
+ import hashlib
4
+ from typing import Any
5
+
6
+ from .vendored_pipeline import build_inference_messages
7
+
8
+
9
+ def render_chatml(messages: list[dict[str, str]]) -> str:
10
+ system, user = messages
11
+ return (
12
+ f"<|im_start|>system\n{system['content']}<|im_end|>\n"
13
+ f"<|im_start|>user\n{user['content']}<|im_end|>\n"
14
+ "<|im_start|>assistant\n<think>\n\n</think>\n\n"
15
+ )
16
+
17
+
18
+ def build_prompt(content: str, hygiene: dict[str, Any], filename: str) -> str:
19
+ return render_chatml(build_inference_messages(content, hygiene, filename))
20
+
21
+
22
+ def content_key(content: str) -> str:
23
+ normalized = content.replace("\r\n", "\n").rstrip("\n")
24
+ return hashlib.sha256(normalized.encode("utf-8")).hexdigest()
server/service.py ADDED
@@ -0,0 +1,150 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from __future__ import annotations
2
+
3
+ import asyncio
4
+ import json
5
+ import logging
6
+ import time
7
+ from collections import OrderedDict
8
+ from concurrent.futures import ThreadPoolExecutor
9
+ from pathlib import Path
10
+ from typing import Any
11
+
12
+ from .backends import Refiner
13
+ from .config import Settings
14
+ from .fix_loop import PREVIEW_INTERVAL, Cancelled, Emit, FixRequest, FixRun
15
+ from .fix_queue import FixQueue, Ticket
16
+ from .telemetry import PRIVATE_SOURCES, Telemetry
17
+
18
+ log = logging.getLogger("space.fix")
19
+
20
+ Event = tuple[str, dict[str, Any]]
21
+ CacheKey = tuple[str, str, str, float, int]
22
+
23
+
24
+ def final_previews(events: list[Event]) -> set[int]:
25
+ """Indices of the last preview before each verdict, which a paced replay must not drop."""
26
+ keep: set[int] = set()
27
+ latest = None
28
+ for index, (name, _) in enumerate(events):
29
+ if name == "preview":
30
+ latest = index
31
+ elif name == "verdict" and latest is not None:
32
+ keep.add(latest)
33
+ latest = None
34
+ return keep
35
+
36
+
37
+ class ResultCache:
38
+ def __init__(self, size: int) -> None:
39
+ self.size = size
40
+ self._items: OrderedDict[CacheKey, list[Event]] = OrderedDict()
41
+
42
+ def get(self, key: CacheKey) -> list[Event] | None:
43
+ events = self._items.get(key)
44
+ if events is not None:
45
+ self._items.move_to_end(key)
46
+ return events
47
+
48
+ def put(self, key: CacheKey, events: list[Event]) -> None:
49
+ self._items[key] = events
50
+ self._items.move_to_end(key)
51
+ while len(self._items) > self.size:
52
+ self._items.popitem(last=False)
53
+
54
+
55
+ def retains(source_kind: str) -> bool:
56
+ """Uploaded folders and pasted snippets live only in request memory, never in the cache."""
57
+ return source_kind not in PRIVATE_SOURCES
58
+
59
+
60
+ class FixService:
61
+ def __init__(self, settings: Settings, refiner: Refiner, telemetry: Telemetry | None = None) -> None:
62
+ self.settings = settings
63
+ self.refiner = refiner
64
+ self.telemetry = telemetry or Telemetry()
65
+ self.queue = FixQueue(settings.max_waiters)
66
+ self.cache = ResultCache(settings.cache_size)
67
+ self._executor = ThreadPoolExecutor(max_workers=2, thread_name_prefix="refiner")
68
+
69
+ def cache_key(self, request: FixRequest) -> CacheKey:
70
+ model_id = self.refiner.model_id
71
+ return (model_id, request.filename, request.content, request.threshold, request.max_passes)
72
+
73
+ def cached(self, request: FixRequest, source_kind: str) -> list[Event] | None:
74
+ if not retains(source_kind):
75
+ return None
76
+ return self.cache.get(self.cache_key(request))
77
+
78
+ async def execute(
79
+ self, request: FixRequest, ticket: Ticket, emit: Emit, source_kind: str = "snippet"
80
+ ) -> list[Event]:
81
+ loop = asyncio.get_running_loop()
82
+ events: list[Event] = []
83
+
84
+ def thread_emit(name: str, data: dict[str, Any]) -> None:
85
+ events.append((name, data))
86
+ loop.call_soon_threadsafe(emit, name, data)
87
+
88
+ future = None
89
+ try:
90
+ await self.queue.wait_turn(ticket, lambda position: emit("queue", {"position": position}))
91
+ run = FixRun(self.refiner, request, ticket.cancel, thread_emit, self.settings.max_file_chars)
92
+ future = self._executor.submit(run.run)
93
+ future.add_done_callback(lambda _: loop.call_soon_threadsafe(self.queue.release, ticket))
94
+ await asyncio.wrap_future(future)
95
+ finally:
96
+ if future is None:
97
+ self.queue.release(ticket)
98
+ elif not future.done():
99
+ ticket.cancel.set()
100
+ if retains(source_kind):
101
+ self.cache.put(self.cache_key(request), events)
102
+ if source_kind != "examples" and self.settings.counts_fixes:
103
+ self.telemetry.record_clean(request.filename, request.content, future.result())
104
+ return events
105
+
106
+ async def replay(self, events: list[Event], emit: Emit, paced: bool = True) -> None:
107
+ rate = self.settings.replay_tokens_per_second if paced else 0.0
108
+ keep = final_previews(events)
109
+ started = time.monotonic()
110
+ sent = 0
111
+ last_preview_at = -PREVIEW_INTERVAL
112
+ for index, (name, data) in enumerate(events):
113
+ if name in ("queue", "progress"):
114
+ continue
115
+ if name == "preview" and rate > 0 and index not in keep:
116
+ if time.monotonic() - last_preview_at < PREVIEW_INTERVAL:
117
+ continue
118
+ last_preview_at = time.monotonic()
119
+ emit(name, {**data, "cached": True} if name in ("start", "done") else data)
120
+ if name != "token" or rate <= 0:
121
+ continue
122
+ sent += 1
123
+ ahead = started + sent / rate - time.monotonic()
124
+ if ahead > 0:
125
+ await asyncio.sleep(ahead)
126
+
127
+ async def warm_presets(self, presets_path: Path | None) -> None:
128
+ await asyncio.get_running_loop().run_in_executor(self._executor, self.refiner.load)
129
+ if presets_path is None:
130
+ return
131
+ presets = json.loads(presets_path.read_text(encoding="utf-8"))
132
+ pending = [FixRequest(preset["filename"], preset["before"]) for preset in presets]
133
+ while pending:
134
+ request = pending[0]
135
+ if self.cache.get(self.cache_key(request)) is not None:
136
+ pending.pop(0)
137
+ continue
138
+ await self.queue.wait_idle()
139
+ ticket = self.queue.join(background=True)
140
+ try:
141
+ await self.execute(request, ticket, lambda name, data: None, source_kind="examples")
142
+ except Cancelled:
143
+ log.info("warm-up yielded to a visitor")
144
+ continue
145
+ pending.pop(0)
146
+ log.info("warmed %d of %d examples", len(presets) - len(pending), len(presets))
147
+
148
+ def close(self) -> None:
149
+ self.queue.cancel_all()
150
+ self._executor.shutdown(wait=False, cancel_futures=True)
server/telemetry.py ADDED
@@ -0,0 +1,476 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """Aggregate, code-free usage counters for the Space. See space/TELEMETRY.md."""
2
+
3
+ from __future__ import annotations
4
+
5
+ import asyncio
6
+ import hashlib
7
+ import hmac
8
+ import json
9
+ import logging
10
+ import secrets
11
+ import time
12
+ from collections import Counter, OrderedDict
13
+ from collections.abc import Iterable, Mapping
14
+ from datetime import datetime, timezone
15
+ from pathlib import Path
16
+ from typing import Annotated, Any, Literal, Protocol
17
+
18
+ from pydantic import BaseModel, ConfigDict, Field
19
+
20
+ log = logging.getLogger("space.telemetry")
21
+
22
+ DIGEST_HEX = 16
23
+ SEEN_LIMIT = 100_000
24
+ OPEN_PR_LIMIT = 10_000
25
+ STATS_VERSION = 1
26
+ STATS_FILE = "stats.json"
27
+ PRIVATE_SOURCES = frozenset({"folder", "snippet"})
28
+
29
+ SQUIGGLE_KINDS = (
30
+ "deep_nesting", "poor_naming", "noisy_comment", "swallowed_error",
31
+ "dense_line", "special_casing", "duplicate_block", "cramped_layout",
32
+ )
33
+ LOC_BUCKETS = ("0-50", "50-100", "100-300", "300-1000", "1000+")
34
+ CODE_EXTENSIONS = frozenset({
35
+ "py", "pyi", "js", "mjs", "cjs", "ts", "tsx", "jsx", "c", "h", "cc", "cpp", "cxx",
36
+ "hpp", "rs", "go", "zig", "java", "cs", "lua", "rb", "php", "swift", "kt", "scala", "sh",
37
+ })
38
+ OTHER_EXT = "other"
39
+ SCORE_KEYS = frozenset(str(points) for points in range(101))
40
+ MS_STEPS_PER_DECADE = 8
41
+ MS_DECADES = 4
42
+
43
+
44
+ def _two_figures(value: float) -> int:
45
+ """Rounds to two significant figures so bucket edges read as plain numbers."""
46
+ digits = len(str(int(value)))
47
+ return int(round(value, 2 - digits))
48
+
49
+
50
+ MS_EDGES = tuple(sorted({
51
+ _two_figures(10 ** (step / MS_STEPS_PER_DECADE)) for step in range(MS_STEPS_PER_DECADE * MS_DECADES + 1)
52
+ }))
53
+ MS_OVERFLOW = f"{MS_EDGES[-1]}+"
54
+ MS_KEYS = frozenset(str(edge) for edge in MS_EDGES) | {MS_OVERFLOW}
55
+
56
+ SourceKind = Literal["examples", "repo", "folder", "snippet"]
57
+ SquiggleKind = Literal[
58
+ "deep_nesting", "poor_naming", "noisy_comment", "swallowed_error",
59
+ "dense_line", "special_casing", "duplicate_block", "cramped_layout",
60
+ ]
61
+ Count = Annotated[int, Field(ge=0, le=10_000)]
62
+
63
+ COUNTERS = (
64
+ "files_scored", "files_cleaned", "fix_runs", "scan_batches",
65
+ "score_scored_sum", "score_before_sum", "score_after_sum",
66
+ "prs_opened", "prs_merged", "prs_closed_unmerged",
67
+ )
68
+ TALLIES = (
69
+ "scored_by_ext", "cleaned_by_ext", "score_sum_by_ext", "scored_by_loc_bucket",
70
+ "squiggles_before_by_kind", "squiggles_resolved_by_kind", "score_hist", "score_ms_hist",
71
+ )
72
+ TALLY_KEYS: dict[str, frozenset[str]] = {
73
+ "scored_by_ext": CODE_EXTENSIONS | {OTHER_EXT},
74
+ "cleaned_by_ext": CODE_EXTENSIONS | {OTHER_EXT},
75
+ "score_sum_by_ext": CODE_EXTENSIONS | {OTHER_EXT},
76
+ "scored_by_loc_bucket": frozenset(LOC_BUCKETS),
77
+ "squiggles_before_by_kind": frozenset(SQUIGGLE_KINDS),
78
+ "squiggles_resolved_by_kind": frozenset(SQUIGGLE_KINDS),
79
+ "score_hist": SCORE_KEYS,
80
+ "score_ms_hist": MS_KEYS,
81
+ }
82
+
83
+
84
+ class ScanFile(BaseModel):
85
+ model_config = ConfigDict(extra="forbid")
86
+
87
+ digest16: str = Field(pattern=r"^[0-9a-f]{16}$")
88
+ ext: str = Field("", max_length=8)
89
+ loc_bucket: Literal["0-50", "50-100", "100-300", "300-1000", "1000+"]
90
+ score_before: int = Field(ge=0, le=100)
91
+ squiggles: dict[SquiggleKind, Count] = Field(default_factory=dict)
92
+ score_ms: int | None = Field(None, ge=0, le=60_000)
93
+
94
+
95
+ class ScanBody(BaseModel):
96
+ model_config = ConfigDict(extra="forbid")
97
+
98
+ source_kind: SourceKind
99
+ files: list[ScanFile] = Field(max_length=200)
100
+
101
+
102
+ def extension_of(filename: str) -> str:
103
+ """Lower-case extension of the base name, as the browser computes it; empty when there is none."""
104
+ base = filename.rsplit("/", 1)[-1]
105
+ stem, dot, ext = base.rpartition(".")
106
+ return ext.lower() if dot and stem else ""
107
+
108
+
109
+ def known_ext(ext: str) -> str:
110
+ """Tally key: an extension from the fixed code set, or `other`, so tallies stay bounded."""
111
+ return ext if ext in CODE_EXTENSIONS else OTHER_EXT
112
+
113
+
114
+ def content_digest(ext: str, content: str) -> str:
115
+ """First 64 bits of SHA-256(ext + NUL + content), the same digest the browser sends."""
116
+ payload = f"{ext}\0{content}".encode("utf-8", errors="replace")
117
+ return hashlib.sha256(payload).hexdigest()[:DIGEST_HEX]
118
+
119
+
120
+ def pr_key(repo_id: int, pr_number: int) -> str:
121
+ return hashlib.sha256(f"{repo_id}:{pr_number}".encode()).hexdigest()[:DIGEST_HEX]
122
+
123
+
124
+ def kind_counts(squiggles: Iterable[Mapping[str, Any]]) -> Counter[str]:
125
+ return Counter(item["kind"] for item in squiggles if item.get("kind") in SQUIGGLE_KINDS)
126
+
127
+
128
+ def to_points(score: float) -> int:
129
+ return round(score * 100)
130
+
131
+
132
+ class DigestSet:
133
+ """Rolling set of the most recent 64-bit digests, oldest dropped first."""
134
+
135
+ def __init__(self, limit: int = SEEN_LIMIT) -> None:
136
+ self.limit = limit
137
+ self._items: OrderedDict[int, None] = OrderedDict()
138
+
139
+ def add(self, digest: str) -> bool:
140
+ key = int(digest, 16)
141
+ if key in self._items:
142
+ self._items.move_to_end(key)
143
+ return False
144
+ self._items[key] = None
145
+ if len(self._items) > self.limit:
146
+ self._items.popitem(last=False)
147
+ return True
148
+
149
+ def __len__(self) -> int:
150
+ return len(self._items)
151
+
152
+
153
+ def empty_stats() -> dict[str, Any]:
154
+ stats: dict[str, Any] = {"version": STATS_VERSION}
155
+ stats.update({name: 0 for name in COUNTERS})
156
+ stats.update({name: {} for name in TALLIES})
157
+ stats["open_pr_keys"] = []
158
+ return stats
159
+
160
+
161
+ def _clean_count(value: Any) -> int:
162
+ return value if isinstance(value, int) and not isinstance(value, bool) and value >= 0 else 0
163
+
164
+
165
+ def sanitize_stats(raw: Any) -> dict[str, Any]:
166
+ """Keep only known counters, bounded tally keys and opaque PR digests from a stats file."""
167
+ stats = empty_stats()
168
+ if not isinstance(raw, dict):
169
+ return stats
170
+ for name in COUNTERS:
171
+ stats[name] = _clean_count(raw.get(name))
172
+ for name in TALLIES:
173
+ tally = raw.get(name)
174
+ if isinstance(tally, dict):
175
+ allowed = TALLY_KEYS[name]
176
+ stats[name] = {key: _clean_count(value) for key, value in tally.items() if key in allowed}
177
+ keys = raw.get("open_pr_keys")
178
+ if isinstance(keys, list):
179
+ digests = [key for key in keys if isinstance(key, str) and len(key) == DIGEST_HEX]
180
+ stats["open_pr_keys"] = [key for key in digests if all(ch in "0123456789abcdef" for ch in key)]
181
+ return stats
182
+
183
+
184
+ def _bump(tally: dict[str, int], key: str, amount: int = 1) -> None:
185
+ if amount > 0:
186
+ tally[key] = tally.get(key, 0) + amount
187
+
188
+
189
+ def _mean(total: int, count: int) -> float | None:
190
+ return round(total / count, 1) if count else None
191
+
192
+
193
+ def ms_bucket(ms: int) -> str:
194
+ """Histogram key for a scoring time: the smallest edge at or above it."""
195
+ edge = next((edge for edge in MS_EDGES if ms <= edge), None)
196
+ return str(edge) if edge is not None else MS_OVERFLOW
197
+
198
+
199
+ def _median_key(tally: Mapping[str, int], keys: Iterable[str]) -> str | None:
200
+ """Key holding the middle sample when keys are walked in order."""
201
+ total = sum(tally.values())
202
+ seen = 0
203
+ for key in keys:
204
+ seen += tally.get(key, 0)
205
+ if total and seen * 2 >= total:
206
+ return key
207
+ return None
208
+
209
+
210
+ def median_score(tally: Mapping[str, int]) -> int | None:
211
+ key = _median_key(tally, (str(points) for points in range(101)))
212
+ return int(key) if key is not None else None
213
+
214
+
215
+ def median_ms(tally: Mapping[str, int]) -> int | None:
216
+ key = _median_key(tally, [*(str(edge) for edge in MS_EDGES), MS_OVERFLOW])
217
+ return int(key.rstrip("+")) if key is not None else None
218
+
219
+
220
+ class Telemetry:
221
+ """Aggregate counters. Mutated only on the event loop; no content, names or addresses."""
222
+
223
+ def __init__(self, excluded: Iterable[str] = ()) -> None:
224
+ self.stats = empty_stats()
225
+ self.dirty = False
226
+ self.excluded = frozenset(excluded)
227
+ self.seen_scored = DigestSet()
228
+ self.seen_cleaned = DigestSet()
229
+ self._open_prs: set[str] = set()
230
+
231
+ def merge(self, raw: Any) -> None:
232
+ loaded = sanitize_stats(raw)
233
+ for name in COUNTERS:
234
+ self.stats[name] += loaded[name]
235
+ for name in TALLIES:
236
+ for key, value in loaded[name].items():
237
+ _bump(self.stats[name], key, value)
238
+ self._open_prs.update(loaded["open_pr_keys"])
239
+ self._trim_open_prs()
240
+
241
+ def record_scan(self, body: ScanBody) -> int:
242
+ if body.source_kind == "examples":
243
+ return 0
244
+ counted = 0
245
+ for item in body.files:
246
+ if item.digest16 in self.excluded or not self.seen_scored.add(item.digest16):
247
+ continue
248
+ counted += 1
249
+ ext = known_ext(item.ext)
250
+ self.stats["score_scored_sum"] += item.score_before
251
+ _bump(self.stats["scored_by_ext"], ext)
252
+ _bump(self.stats["score_sum_by_ext"], ext, item.score_before)
253
+ _bump(self.stats["scored_by_loc_bucket"], item.loc_bucket)
254
+ _bump(self.stats["score_hist"], str(item.score_before))
255
+ if item.score_ms is not None:
256
+ _bump(self.stats["score_ms_hist"], ms_bucket(item.score_ms))
257
+ for kind, amount in item.squiggles.items():
258
+ _bump(self.stats["squiggles_before_by_kind"], kind, amount)
259
+ self.stats["files_scored"] += counted
260
+ self.stats["scan_batches"] += 1
261
+ self.dirty = True
262
+ return counted
263
+
264
+ def record_clean(self, filename: str, content: str, done: Mapping[str, Any]) -> bool:
265
+ """Count one accepted refiner run; the file counts once however often it is cleaned."""
266
+ if not done.get("accepted"):
267
+ return False
268
+ self.stats["fix_runs"] += 1
269
+ self.dirty = True
270
+ ext = extension_of(filename)
271
+ digest = content_digest(ext, content)
272
+ if digest in self.excluded or not self.seen_cleaned.add(digest):
273
+ return False
274
+ before, after = done["before_hygiene"], done["after_hygiene"]
275
+ self.stats["files_cleaned"] += 1
276
+ self.stats["score_before_sum"] += to_points(before["score"])
277
+ self.stats["score_after_sum"] += to_points(after["score"])
278
+ _bump(self.stats["cleaned_by_ext"], known_ext(ext))
279
+ remaining = kind_counts(after["squiggles"])
280
+ for kind, amount in kind_counts(before["squiggles"]).items():
281
+ _bump(self.stats["squiggles_resolved_by_kind"], kind, amount - remaining[kind])
282
+ return True
283
+
284
+ def record_pr_opened(self, key: str) -> bool:
285
+ if key in self._open_prs:
286
+ return False
287
+ self._open_prs.add(key)
288
+ self._trim_open_prs()
289
+ self.stats["prs_opened"] += 1
290
+ self.dirty = True
291
+ return True
292
+
293
+ def record_pr_closed(self, key: str, merged: bool) -> bool:
294
+ if key not in self._open_prs:
295
+ return False
296
+ self._open_prs.discard(key)
297
+ self.stats["prs_merged" if merged else "prs_closed_unmerged"] += 1
298
+ self.dirty = True
299
+ return True
300
+
301
+ def _trim_open_prs(self) -> None:
302
+ while len(self._open_prs) > OPEN_PR_LIMIT:
303
+ self._open_prs.pop()
304
+
305
+ def snapshot(self) -> str:
306
+ stats = {**self.stats, "open_pr_keys": sorted(self._open_prs)}
307
+ stats["updated_at"] = datetime.now(timezone.utc).strftime("%Y-%m-%dT%H:%M:%SZ")
308
+ return json.dumps(stats, indent=2, sort_keys=True) + "\n"
309
+
310
+ def _by_ext(self) -> dict[str, dict[str, Any]]:
311
+ stats = self.stats
312
+ extensions = sorted(set(stats["scored_by_ext"]) | set(stats["cleaned_by_ext"]))
313
+ rows = {}
314
+ for ext in extensions:
315
+ scored = stats["scored_by_ext"].get(ext, 0)
316
+ rows[ext] = {
317
+ "scored": scored,
318
+ "cleaned": stats["cleaned_by_ext"].get(ext, 0),
319
+ "avg_score": _mean(stats["score_sum_by_ext"].get(ext, 0), scored),
320
+ }
321
+ return rows
322
+
323
+ def public(self) -> dict[str, Any]:
324
+ stats = self.stats
325
+ cleaned = stats["files_cleaned"]
326
+ merged = stats["prs_merged"]
327
+ decided = merged + stats["prs_closed_unmerged"]
328
+ resolved = stats["squiggles_resolved_by_kind"]
329
+ return {
330
+ "files_scored": stats["files_scored"],
331
+ "files_cleaned": cleaned,
332
+ "fix_runs": stats["fix_runs"],
333
+ "issues_fixed": sum(resolved.values()),
334
+ "avg_score_scored": _mean(stats["score_scored_sum"], stats["files_scored"]),
335
+ "avg_score_before": _mean(stats["score_before_sum"], cleaned),
336
+ "avg_score_after": _mean(stats["score_after_sum"], cleaned),
337
+ "median_score": median_score(stats["score_hist"]),
338
+ "score_ms_median": median_ms(stats["score_ms_hist"]),
339
+ "score_ms_samples": sum(stats["score_ms_hist"].values()),
340
+ "by_ext": self._by_ext(),
341
+ "by_loc_bucket": dict(stats["scored_by_loc_bucket"]),
342
+ "squiggles_before_by_kind": dict(stats["squiggles_before_by_kind"]),
343
+ "squiggles_resolved_by_kind": dict(resolved),
344
+ "prs_opened": stats["prs_opened"],
345
+ "prs_merged": merged,
346
+ "prs_closed_unmerged": stats["prs_closed_unmerged"],
347
+ "prs_decided": decided,
348
+ "pr_merge_rate": round(merged / decided, 3) if decided else None,
349
+ }
350
+
351
+
352
+ def preset_digests(presets_path: Path | None) -> set[str]:
353
+ """Digests of the bundled examples, which never count."""
354
+ if presets_path is None or not presets_path.is_file():
355
+ return set()
356
+ rows = json.loads(presets_path.read_text(encoding="utf-8"))
357
+ return {content_digest(extension_of(row["filename"]), row["before"]) for row in rows}
358
+
359
+
360
+ class RateLimiter:
361
+ """Token bucket keyed by a salted hash of the caller; the address itself is never kept."""
362
+
363
+ def __init__(self, per_minute: int, idle_seconds: float = 120.0) -> None:
364
+ self.per_minute = per_minute
365
+ self.idle_seconds = idle_seconds
366
+ self._salt = secrets.token_bytes(16)
367
+ self._buckets: dict[bytes, tuple[float, float]] = {}
368
+
369
+ def allow(self, caller: str) -> bool:
370
+ key = hmac.new(self._salt, caller.encode(), hashlib.sha256).digest()[:8]
371
+ now = time.monotonic()
372
+ tokens, updated = self._buckets.get(key, (float(self.per_minute), now))
373
+ tokens = min(float(self.per_minute), tokens + (now - updated) * self.per_minute / 60.0)
374
+ allowed = tokens >= 1.0
375
+ self._buckets[key] = (tokens - 1.0 if allowed else tokens, now)
376
+ if len(self._buckets) > 10_000:
377
+ self._prune(now)
378
+ return allowed
379
+
380
+ def _prune(self, now: float) -> None:
381
+ stale = [key for key, (_, updated) in self._buckets.items() if now - updated > self.idle_seconds]
382
+ for key in stale:
383
+ del self._buckets[key]
384
+
385
+
386
+ class StatsStore:
387
+ """Reads and writes one JSON file locally and, when configured, in a Hugging Face Dataset repo."""
388
+
389
+ def __init__(
390
+ self, local_path: Path, dataset_repo: str = "", token: str = "", filename: str = STATS_FILE,
391
+ ) -> None:
392
+ self.local_path = local_path
393
+ self.dataset_repo = dataset_repo
394
+ self.token = token
395
+ self.filename = filename
396
+ self.remote_loaded = not self.remote
397
+
398
+ @property
399
+ def remote(self) -> bool:
400
+ return bool(self.dataset_repo and self.token)
401
+
402
+ def load(self) -> dict[str, Any] | None:
403
+ """Blocking. Returns the saved counters, or None when there is nothing to load yet."""
404
+ if self.remote:
405
+ loaded = self._load_remote()
406
+ self.remote_loaded = True
407
+ return loaded
408
+ if self.local_path.is_file():
409
+ return json.loads(self.local_path.read_text(encoding="utf-8"))
410
+ return None
411
+
412
+ def _load_remote(self) -> dict[str, Any] | None:
413
+ from huggingface_hub import hf_hub_download
414
+ from huggingface_hub.errors import EntryNotFoundError
415
+
416
+ try:
417
+ path = hf_hub_download(
418
+ self.dataset_repo, self.filename, repo_type="dataset", token=self.token,
419
+ force_download=True,
420
+ )
421
+ except EntryNotFoundError:
422
+ return None
423
+ return json.loads(Path(path).read_text(encoding="utf-8"))
424
+
425
+ def save(self, text: str) -> None:
426
+ """Blocking. Never uploads before the remote copy was read, so it cannot be overwritten."""
427
+ self.local_path.parent.mkdir(parents=True, exist_ok=True)
428
+ self.local_path.write_text(text, encoding="utf-8")
429
+ if not self.remote or not self.remote_loaded:
430
+ return
431
+ from huggingface_hub import HfApi
432
+
433
+ HfApi(token=self.token).upload_file(
434
+ path_or_fileobj=text.encode("utf-8"),
435
+ path_in_repo=self.filename,
436
+ repo_id=self.dataset_repo,
437
+ repo_type="dataset",
438
+ commit_message=f"Update {self.filename}",
439
+ )
440
+
441
+
442
+ class Persisted(Protocol):
443
+ dirty: bool
444
+
445
+ def merge(self, raw: Any) -> None: ...
446
+
447
+ def snapshot(self) -> str: ...
448
+
449
+
450
+ class SyncedFile:
451
+ """Loads a model from its store once, then writes it back whenever it changed."""
452
+
453
+ def __init__(self, store: StatsStore, model: Persisted) -> None:
454
+ self.store = store
455
+ self.model = model
456
+
457
+ async def load(self) -> None:
458
+ try:
459
+ loaded = await asyncio.to_thread(self.store.load)
460
+ except Exception as error:
461
+ log.warning("%s load failed: %s", self.store.filename, type(error).__name__)
462
+ return
463
+ self.model.merge(loaded)
464
+
465
+ async def flush(self) -> None:
466
+ if not self.store.remote_loaded:
467
+ await self.load()
468
+ if not self.model.dirty:
469
+ return
470
+ text = self.model.snapshot()
471
+ self.model.dirty = False
472
+ try:
473
+ await asyncio.to_thread(self.store.save, text)
474
+ except Exception as error:
475
+ self.model.dirty = True
476
+ log.warning("%s sync failed: %s", self.store.filename, type(error).__name__)
server/vendored_guards.py ADDED
@@ -0,0 +1,61 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Vendored verbatim from prototype/train_browser_model.py (helpers and grade_patch_candidate thresholds).
2
+ from __future__ import annotations
3
+
4
+ import ast
5
+ from pathlib import Path
6
+
7
+
8
+ def infer_lang_from_name(filename: str) -> str:
9
+ ext = Path(filename.lower()).suffix
10
+ mapping = {
11
+ ".py": "python",
12
+ ".ts": "typescript",
13
+ ".mts": "typescript",
14
+ ".js": "javascript",
15
+ ".mjs": "javascript",
16
+ ".rs": "rust",
17
+ ".go": "go",
18
+ }
19
+ return mapping.get(ext, "other")
20
+
21
+
22
+ def count_non_comment_lines(code: str) -> int:
23
+ total = 0
24
+ for raw in code.splitlines():
25
+ stripped = raw.strip()
26
+ if stripped and not stripped.startswith(("#", "//", "/*", "*")):
27
+ total += 1
28
+ return total
29
+
30
+
31
+ def check_basic_syntax(code: str, filename: str) -> bool:
32
+ if not code.strip():
33
+ return False
34
+ lang = infer_lang_from_name(filename)
35
+ if lang == "python":
36
+ try:
37
+ ast.parse(code)
38
+ return True
39
+ except SyntaxError:
40
+ return False
41
+ if lang in ("typescript", "javascript", "rust", "go"):
42
+ return code.count("{") == code.count("}") and code.count("(") == code.count(")")
43
+ return True
44
+
45
+
46
+ def is_gutted(before_code: str, updated: str) -> bool:
47
+ orig_code_n = count_non_comment_lines(before_code)
48
+ new_code_n = count_non_comment_lines(updated)
49
+ if orig_code_n >= 1 and new_code_n == 0:
50
+ return True
51
+ if orig_code_n >= 10 and new_code_n < max(5, int(orig_code_n * 0.55)):
52
+ return True
53
+ return False
54
+
55
+
56
+ def is_bloated(before_code: str, updated: str) -> bool:
57
+ before_nonempty = sum(1 for ln in before_code.splitlines() if ln.strip())
58
+ after_nonempty = sum(1 for ln in updated.splitlines() if ln.strip())
59
+ if after_nonempty > max(40, int(before_nonempty * 1.60)):
60
+ return True
61
+ return False
server/vendored_pipeline.py ADDED
@@ -0,0 +1,278 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Vendored verbatim from prototype/pipeline.py; keep byte-identical (tests/test_vendored.py).
2
+ from __future__ import annotations
3
+
4
+ import difflib
5
+ import json
6
+ import re
7
+ from typing import Any
8
+
9
+ TASK_INSTRUCTIONS = """\
10
+ Refactor the file to resolve every squiggle in the codehygiene response while preserving exact runtime behavior and non-squiggled public/exported function, class, and method names.
11
+ Keep edits minimal and idiomatic; do not add redundant comments, wrapper ceremony, or line bloat.
12
+ Wrap dense lines under 88 columns and rename cryptic 1-2 character identifiers, ceremonial *Helper/*Manager/*Factory names, or magic-index expressions flagged by poor_naming into clear domain names across their scope.
13
+ Return ONLY one or more SEARCH/REPLACE blocks in this exact format (no prose outside blocks):
14
+
15
+ <<<<<<< SEARCH
16
+ exact existing lines from the file
17
+ =======
18
+ replacement lines
19
+ >>>>>>> REPLACE"""
20
+
21
+ _PATCH_BLOCK_RE = re.compile(
22
+ r"<<<<<<< SEARCH\r?\n(.*?)\r?\n=======\r?\n(.*?)>>>>>>> REPLACE",
23
+ re.DOTALL,
24
+ )
25
+
26
+
27
+ def enrich_hygiene_with_source_lines(
28
+ content: str,
29
+ hygiene_response: dict[str, Any],
30
+ ) -> dict[str, Any]:
31
+ src_lines = content.replace("\r\n", "\n").split("\n")
32
+ enriched_sq: list[dict[str, Any]] = []
33
+ for sq in hygiene_response.get("squiggles", []):
34
+ line_no = int(sq.get("line", 1))
35
+ src_line = src_lines[line_no - 1] if 1 <= line_no <= len(src_lines) else ""
36
+ enriched_sq.append(
37
+ {
38
+ "line": line_no,
39
+ "source_line": src_line,
40
+ "col_start": sq.get("col_start", 0),
41
+ "col_end": sq.get("col_end", len(src_line)),
42
+ "probability": sq.get("probability", 1.0),
43
+ "kind": sq.get("kind", ""),
44
+ "message": sq.get("message", ""),
45
+ }
46
+ )
47
+ return {
48
+ "score": hygiene_response.get("score", 0.0),
49
+ "rl_hygiene": hygiene_response.get("rl_hygiene", 0.0),
50
+ "scores": hygiene_response.get("scores", {}),
51
+ "archetype": hygiene_response.get("archetype", ""),
52
+ "squiggles": enriched_sq,
53
+ }
54
+
55
+
56
+ def build_user_context(
57
+ content: str,
58
+ hygiene_response: dict[str, Any],
59
+ filename: str = "",
60
+ ) -> str:
61
+ file_line = f"File: {filename}\n\n" if filename else ""
62
+ enriched = enrich_hygiene_with_source_lines(content, hygiene_response)
63
+ hygiene_text = json.dumps(enriched, indent=2)
64
+ return (
65
+ f"{file_line}"
66
+ f"<file_content>\n{content}\n</file_content>\n\n"
67
+ f"<codehygiene_response>\n{hygiene_text}\n</codehygiene_response>"
68
+ )
69
+
70
+
71
+ def build_inference_messages(
72
+ content: str,
73
+ hygiene_response: dict[str, Any],
74
+ filename: str = "",
75
+ ) -> list[dict[str, str]]:
76
+ """Build the exact [system, user] message list used for both teacher API and browser LLM."""
77
+ return [
78
+ {"role": "system", "content": TASK_INSTRUCTIONS},
79
+ {"role": "user", "content": build_user_context(content, hygiene_response, filename)},
80
+ ]
81
+
82
+
83
+ def _strip_single_trailing_newline(text: str) -> str:
84
+ if text.endswith("\r\n"):
85
+ return text[:-2]
86
+ if text.endswith("\n"):
87
+ return text[:-1]
88
+ return text
89
+
90
+
91
+ def parse_changes(response_text: str) -> list[tuple[str, str]]:
92
+ blocks: list[tuple[str, str]] = []
93
+ for match in _PATCH_BLOCK_RE.finditer(response_text):
94
+ search_part = match.group(1).replace("\r\n", "\n")
95
+ replace_part = _strip_single_trailing_newline(match.group(2)).replace("\r\n", "\n")
96
+ blocks.append((search_part, replace_part))
97
+ return blocks
98
+
99
+
100
+ def _replace_rstrip_lines(source: str, search: str, replace: str) -> str | None:
101
+ src_lines = source.split("\n")
102
+ needle = [ln.rstrip() for ln in search.split("\n")]
103
+ span = len(needle)
104
+ if span == 0 or span > len(src_lines):
105
+ return None
106
+
107
+ matches = [
108
+ idx
109
+ for idx in range(len(src_lines) - span + 1)
110
+ if [ln.rstrip() for ln in src_lines[idx : idx + span]] == needle
111
+ ]
112
+ if len(matches) != 1:
113
+ return None
114
+
115
+ pos = matches[0]
116
+ repl_lines = replace.split("\n") if replace else []
117
+ merged = src_lines[:pos] + repl_lines + src_lines[pos + span :]
118
+ return "\n".join(merged)
119
+
120
+
121
+ def _leading_spaces(line: str) -> int:
122
+ return len(line) - len(line.lstrip(" "))
123
+
124
+
125
+ def _shift_indent(text: str, delta: int) -> str:
126
+ if not text or delta == 0:
127
+ return text
128
+ shifted: list[str] = []
129
+ for raw in text.split("\n"):
130
+ if not raw.strip():
131
+ shifted.append("")
132
+ elif delta > 0:
133
+ shifted.append((" " * delta) + raw)
134
+ else:
135
+ trim = min(-delta, _leading_spaces(raw))
136
+ shifted.append(raw[trim:])
137
+ return "\n".join(shifted)
138
+
139
+
140
+ def _replace_stripped_unique(source: str, search: str, replace: str) -> str | None:
141
+ src_lines = source.split("\n")
142
+ s_lines = search.split("\n")
143
+ needle = [ln.strip() for ln in s_lines]
144
+ span = len(needle)
145
+ if span == 0 or span > len(src_lines) or not any(needle):
146
+ return None
147
+ if span == 1 and len(needle[0]) < 14:
148
+ return None
149
+
150
+ matches = [
151
+ idx
152
+ for idx in range(len(src_lines) - span + 1)
153
+ if [ln.strip() for ln in src_lines[idx : idx + span]] == needle
154
+ ]
155
+ if len(matches) != 1:
156
+ return None
157
+
158
+ pos = matches[0]
159
+ delta = 0
160
+ for src_ln, srch_ln in zip(src_lines[pos : pos + span], s_lines):
161
+ if src_ln.strip() and srch_ln.strip() and "\t" not in src_ln and "\t" not in srch_ln:
162
+ delta = _leading_spaces(src_ln) - _leading_spaces(srch_ln)
163
+ break
164
+
165
+ adj_replace = _shift_indent(replace, delta)
166
+ repl_lines = adj_replace.split("\n") if adj_replace else []
167
+ merged = src_lines[:pos] + repl_lines + src_lines[pos + span :]
168
+ return "\n".join(merged)
169
+
170
+
171
+ def _score_fuzzy_candidates(
172
+ src_norm: list[str],
173
+ s_nonempty: list[str],
174
+ needle_text: str,
175
+ span: int,
176
+ ) -> tuple[int, float, float]:
177
+ best_pos = -1
178
+ best_ratio = 0.0
179
+ runner_up = 0.0
180
+ if span < 1 or span > len(src_norm):
181
+ return best_pos, best_ratio, runner_up
182
+
183
+ for pos in range(len(src_norm) - span + 1):
184
+ window = src_norm[pos : pos + span]
185
+ c_nonempty = [ln for ln in window if ln]
186
+ if not c_nonempty:
187
+ continue
188
+ first_ok = difflib.SequenceMatcher(None, s_nonempty[0], c_nonempty[0]).ratio() >= 0.78
189
+ last_ok = difflib.SequenceMatcher(None, s_nonempty[-1], c_nonempty[-1]).ratio() >= 0.78
190
+ if not (first_ok and last_ok):
191
+ continue
192
+
193
+ cand_text = "\n".join(window)
194
+ matcher = difflib.SequenceMatcher(None, needle_text, cand_text)
195
+ if matcher.quick_ratio() < 0.88:
196
+ continue
197
+ ratio = matcher.ratio()
198
+ if ratio > best_ratio:
199
+ if best_pos >= 0 and abs(pos - best_pos) >= max(2, span // 2):
200
+ runner_up = max(runner_up, best_ratio)
201
+ best_ratio = ratio
202
+ best_pos = pos
203
+ elif abs(pos - best_pos) >= max(2, span // 2):
204
+ runner_up = max(runner_up, ratio)
205
+ return best_pos, best_ratio, runner_up
206
+
207
+
208
+ def _replace_fuzzy_unique(source: str, search: str, replace: str) -> str | None:
209
+ src_lines = source.split("\n")
210
+ s_lines = search.split("\n")
211
+ s_norm = [ln.strip() for ln in s_lines]
212
+ s_nonempty = [ln for ln in s_norm if ln]
213
+ span = len(s_norm)
214
+ if span == 0 or span > len(src_lines) or not s_nonempty:
215
+ return None
216
+
217
+ needle_text = "\n".join(s_norm)
218
+ min_chars = 12 if span >= 2 else 24
219
+ if len(needle_text) < min_chars:
220
+ return None
221
+
222
+ src_norm = [ln.strip() for ln in src_lines]
223
+ pos, best_ratio, runner_up = _score_fuzzy_candidates(src_norm, s_nonempty, needle_text, span)
224
+ min_ratio = 0.90 if span >= 3 else 0.93
225
+ if pos < 0 or best_ratio < min_ratio or (best_ratio - runner_up) < 0.05:
226
+ return None
227
+
228
+ delta = 0
229
+ for src_ln, srch_ln in zip(src_lines[pos : pos + span], s_lines):
230
+ if src_ln.strip() and srch_ln.strip() and "\t" not in src_ln and "\t" not in srch_ln:
231
+ delta = _leading_spaces(src_ln) - _leading_spaces(srch_ln)
232
+ break
233
+
234
+ adj_replace = _shift_indent(replace, delta)
235
+ repl_lines = adj_replace.split("\n") if adj_replace else []
236
+ merged = src_lines[:pos] + repl_lines + src_lines[pos + span :]
237
+ return "\n".join(merged)
238
+
239
+
240
+ def apply_changes(content: str, response_text: str) -> tuple[str, int]:
241
+ blocks = parse_changes(response_text)
242
+ if not blocks:
243
+ return content, 0
244
+
245
+ newline = "\r\n" if "\r\n" in content else "\n"
246
+ working = content.replace("\r\n", "\n")
247
+ applied = 0
248
+ first_miss: str | None = None
249
+
250
+ for search, replace in blocks:
251
+ if not search or search == replace:
252
+ continue
253
+ if replace == "" and f"{search}\n" in working:
254
+ working = working.replace(f"{search}\n", "", 1)
255
+ applied += 1
256
+ continue
257
+ if search in working:
258
+ working = working.replace(search, replace, 1)
259
+ applied += 1
260
+ continue
261
+
262
+ fallback = _replace_rstrip_lines(working, search, replace)
263
+ if fallback is None:
264
+ fallback = _replace_stripped_unique(working, search, replace)
265
+ if fallback is None:
266
+ fallback = _replace_fuzzy_unique(working, search, replace)
267
+ if fallback is not None:
268
+ working = fallback
269
+ applied += 1
270
+ elif first_miss is None:
271
+ first_miss = search.splitlines()[0][:60]
272
+
273
+ if applied == 0 and first_miss is not None:
274
+ raise ValueError(f"SEARCH block did not match file content: {first_miss!r}")
275
+
276
+ if newline == "\r\n":
277
+ working = working.replace("\n", "\r\n")
278
+ return working, applied
web/.gitignore ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ node_modules/
2
+ dist/
3
+ vendor/
web/build.mjs ADDED
@@ -0,0 +1,89 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import { copyFile, mkdir, rm } from "node:fs/promises";
2
+ import { existsSync } from "node:fs";
3
+ import path from "node:path";
4
+ import { fileURLToPath } from "node:url";
5
+ import * as esbuild from "esbuild";
6
+
7
+ const webDir = path.dirname(fileURLToPath(import.meta.url));
8
+ const srcDir = path.join(webDir, "src");
9
+ const distDir = path.join(webDir, "dist");
10
+ const vendorDir = path.join(webDir, "vendor");
11
+ const docsDir = path.resolve(webDir, "../../docs");
12
+ const SHARED_FILES = ["engine.js", "presets.json", "weights.npz"];
13
+ const watch = process.argv.includes("--watch");
14
+
15
+ function sharedDir() {
16
+ const vendored = SHARED_FILES.every((name) => existsSync(path.join(vendorDir, name)));
17
+ return vendored ? vendorDir : docsDir;
18
+ }
19
+
20
+ function sharedFilesPlugin(sourceDir) {
21
+ const targets = {
22
+ "shared:engine": path.join(sourceDir, "engine.js"),
23
+ "shared:presets": path.join(sourceDir, "presets.json"),
24
+ };
25
+ return {
26
+ name: "shared-files",
27
+ setup(build) {
28
+ build.onResolve({ filter: /^shared:/ }, (args) => {
29
+ const target = targets[args.path];
30
+ return target ? { path: target } : { errors: [{ text: `Unknown shared file ${args.path}` }] };
31
+ });
32
+ },
33
+ };
34
+ }
35
+
36
+ async function copyStatic(sourceDir) {
37
+ const boardDir = path.join(distDir, "leaderboard");
38
+ await mkdir(boardDir, { recursive: true });
39
+ await copyFile(path.join(srcDir, "index.html"), path.join(distDir, "index.html"));
40
+ await copyFile(path.join(srcDir, "board.html"), path.join(boardDir, "index.html"));
41
+ await copyFile(path.join(sourceDir, "weights.npz"), path.join(distDir, "weights.npz"));
42
+ }
43
+
44
+ const copyStaticPlugin = (sourceDir) => ({
45
+ name: "copy-static",
46
+ setup(build) {
47
+ build.onEnd(async (result) => {
48
+ if (result.errors.length === 0) {
49
+ await copyStatic(sourceDir);
50
+ }
51
+ });
52
+ },
53
+ });
54
+
55
+ async function main() {
56
+ const sourceDir = sharedDir();
57
+ await rm(distDir, { recursive: true, force: true });
58
+ await mkdir(distDir, { recursive: true });
59
+
60
+ const context = await esbuild.context({
61
+ entryPoints: {
62
+ app: path.join(srcDir, "main.js"),
63
+ board: path.join(srcDir, "board.js"),
64
+ "score-worker": path.join(srcDir, "score-worker.js"),
65
+ styles: path.join(srcDir, "styles.css"),
66
+ },
67
+ outdir: distDir,
68
+ entryNames: "[name]",
69
+ assetNames: "fonts/[name]-[hash]",
70
+ bundle: true,
71
+ format: "esm",
72
+ target: ["es2022", "chrome123", "safari17.5", "firefox120"],
73
+ minify: !watch,
74
+ sourcemap: watch,
75
+ legalComments: "none",
76
+ loader: { ".woff2": "file" },
77
+ logLevel: "info",
78
+ plugins: [sharedFilesPlugin(sourceDir), copyStaticPlugin(sourceDir)],
79
+ });
80
+
81
+ if (watch) {
82
+ await context.watch();
83
+ return;
84
+ }
85
+ await context.rebuild();
86
+ await context.dispose();
87
+ }
88
+
89
+ await main();
web/package-lock.json ADDED
@@ -0,0 +1,592 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "name": "codehygiene-space-web",
3
+ "lockfileVersion": 3,
4
+ "requires": true,
5
+ "packages": {
6
+ "": {
7
+ "name": "codehygiene-space-web",
8
+ "dependencies": {
9
+ "@fontsource-variable/google-sans-code": "5.3.2",
10
+ "@fontsource-variable/host-grotesk": "5.3.0",
11
+ "@lezer/go": "1.0.1",
12
+ "@lezer/highlight": "1.2.5",
13
+ "@lezer/javascript": "1.5.6",
14
+ "@lezer/python": "1.1.19",
15
+ "@lezer/rust": "1.0.3"
16
+ },
17
+ "devDependencies": {
18
+ "esbuild": "0.28.2"
19
+ }
20
+ },
21
+ "node_modules/@esbuild/aix-ppc64": {
22
+ "version": "0.28.2",
23
+ "resolved": "https://registry.npmjs.org/@esbuild/aix-ppc64/-/aix-ppc64-0.28.2.tgz",
24
+ "integrity": "sha512-XExcO+dvLKvVtNTibSTBej1NCAbaGhWn9Ww1ZPx80qsahhPFe/8jgWP0IchNe0F3HwkU7n8ejhH8bjonqht8mQ==",
25
+ "cpu": [
26
+ "ppc64"
27
+ ],
28
+ "dev": true,
29
+ "license": "MIT",
30
+ "optional": true,
31
+ "os": [
32
+ "aix"
33
+ ],
34
+ "engines": {
35
+ "node": ">=18"
36
+ }
37
+ },
38
+ "node_modules/@esbuild/android-arm": {
39
+ "version": "0.28.2",
40
+ "resolved": "https://registry.npmjs.org/@esbuild/android-arm/-/android-arm-0.28.2.tgz",
41
+ "integrity": "sha512-kXXoiPVVGQcnIYGOeaovwOURpniDBpSq4A03qkQ+BMQqtGG6HYap3xne9C1O1yo4TR3qxlCX5IqqmX6fFo2Lqg==",
42
+ "cpu": [
43
+ "arm"
44
+ ],
45
+ "dev": true,
46
+ "license": "MIT",
47
+ "optional": true,
48
+ "os": [
49
+ "android"
50
+ ],
51
+ "engines": {
52
+ "node": ">=18"
53
+ }
54
+ },
55
+ "node_modules/@esbuild/android-arm64": {
56
+ "version": "0.28.2",
57
+ "resolved": "https://registry.npmjs.org/@esbuild/android-arm64/-/android-arm64-0.28.2.tgz",
58
+ "integrity": "sha512-5YfKeeI8qWfBZIX+u2xZC3Zlb3Os/gLS2sbEKM+I4ZOcsWmHS2WLysCcQZDAFRslDUU5Oiq44gf6PYN1vGwG5A==",
59
+ "cpu": [
60
+ "arm64"
61
+ ],
62
+ "dev": true,
63
+ "license": "MIT",
64
+ "optional": true,
65
+ "os": [
66
+ "android"
67
+ ],
68
+ "engines": {
69
+ "node": ">=18"
70
+ }
71
+ },
72
+ "node_modules/@esbuild/android-x64": {
73
+ "version": "0.28.2",
74
+ "resolved": "https://registry.npmjs.org/@esbuild/android-x64/-/android-x64-0.28.2.tgz",
75
+ "integrity": "sha512-O387ite7SzUyCcy3JQX4P4bLtEA7bLLkx+esve5JHnyYfNTxcVpXZo9jhdB0lTKN44gztELTdU7nS8Nr16Fs1Q==",
76
+ "cpu": [
77
+ "x64"
78
+ ],
79
+ "dev": true,
80
+ "license": "MIT",
81
+ "optional": true,
82
+ "os": [
83
+ "android"
84
+ ],
85
+ "engines": {
86
+ "node": ">=18"
87
+ }
88
+ },
89
+ "node_modules/@esbuild/darwin-arm64": {
90
+ "version": "0.28.2",
91
+ "resolved": "https://registry.npmjs.org/@esbuild/darwin-arm64/-/darwin-arm64-0.28.2.tgz",
92
+ "integrity": "sha512-n4KqkOQrraxHJcgjM1RvwbigfQKIKJVpM7xp+KsxiyUSrRdIXnt73VhrPAx0fV44hgfmIVKjxMN9J1t5jySVkw==",
93
+ "cpu": [
94
+ "arm64"
95
+ ],
96
+ "dev": true,
97
+ "license": "MIT",
98
+ "optional": true,
99
+ "os": [
100
+ "darwin"
101
+ ],
102
+ "engines": {
103
+ "node": ">=18"
104
+ }
105
+ },
106
+ "node_modules/@esbuild/darwin-x64": {
107
+ "version": "0.28.2",
108
+ "resolved": "https://registry.npmjs.org/@esbuild/darwin-x64/-/darwin-x64-0.28.2.tgz",
109
+ "integrity": "sha512-uq6suIWYP37qzGddBKPw5QEQPi6HiLGsO7UmkpfyaYNQ3D+rN6w6WfwH+nuqcGXWvawGwxOEroO4YGnFh95azw==",
110
+ "cpu": [
111
+ "x64"
112
+ ],
113
+ "dev": true,
114
+ "license": "MIT",
115
+ "optional": true,
116
+ "os": [
117
+ "darwin"
118
+ ],
119
+ "engines": {
120
+ "node": ">=18"
121
+ }
122
+ },
123
+ "node_modules/@esbuild/freebsd-arm64": {
124
+ "version": "0.28.2",
125
+ "resolved": "https://registry.npmjs.org/@esbuild/freebsd-arm64/-/freebsd-arm64-0.28.2.tgz",
126
+ "integrity": "sha512-n+I0BTSRIoy+d6RPKnEVwql5UwBJolytvY4mAOIEJorKlqgPII8ix6slVVrfZ5Tnj7glIZvloylbB/EJPMWEXw==",
127
+ "cpu": [
128
+ "arm64"
129
+ ],
130
+ "dev": true,
131
+ "license": "MIT",
132
+ "optional": true,
133
+ "os": [
134
+ "freebsd"
135
+ ],
136
+ "engines": {
137
+ "node": ">=18"
138
+ }
139
+ },
140
+ "node_modules/@esbuild/freebsd-x64": {
141
+ "version": "0.28.2",
142
+ "resolved": "https://registry.npmjs.org/@esbuild/freebsd-x64/-/freebsd-x64-0.28.2.tgz",
143
+ "integrity": "sha512-78XJTJkvPs0kz2w61301PJjXl4g7q3JqiYMZ/M/yVI73EHBrCRTgkhu9oqG7vPqq+a/yadEW8aD+agKlk5xrmg==",
144
+ "cpu": [
145
+ "x64"
146
+ ],
147
+ "dev": true,
148
+ "license": "MIT",
149
+ "optional": true,
150
+ "os": [
151
+ "freebsd"
152
+ ],
153
+ "engines": {
154
+ "node": ">=18"
155
+ }
156
+ },
157
+ "node_modules/@esbuild/linux-arm": {
158
+ "version": "0.28.2",
159
+ "resolved": "https://registry.npmjs.org/@esbuild/linux-arm/-/linux-arm-0.28.2.tgz",
160
+ "integrity": "sha512-XlDnu2q5yoqems+xay6wSAcg9DDD7K9RLKZEBOMZm3ckNpJBvOX20tSfby8KfrrhINDyv9V2YVZKY/SpoGJI8w==",
161
+ "cpu": [
162
+ "arm"
163
+ ],
164
+ "dev": true,
165
+ "license": "MIT",
166
+ "optional": true,
167
+ "os": [
168
+ "linux"
169
+ ],
170
+ "engines": {
171
+ "node": ">=18"
172
+ }
173
+ },
174
+ "node_modules/@esbuild/linux-arm64": {
175
+ "version": "0.28.2",
176
+ "resolved": "https://registry.npmjs.org/@esbuild/linux-arm64/-/linux-arm64-0.28.2.tgz",
177
+ "integrity": "sha512-pW4AC0P3it8c7do9MVM4p51FzHzdM/TZrerurgRcHJ2WTa1VQ1CIq18xncfpBJw4ojkiZZrKW2yIBWBP92j6Ug==",
178
+ "cpu": [
179
+ "arm64"
180
+ ],
181
+ "dev": true,
182
+ "license": "MIT",
183
+ "optional": true,
184
+ "os": [
185
+ "linux"
186
+ ],
187
+ "engines": {
188
+ "node": ">=18"
189
+ }
190
+ },
191
+ "node_modules/@esbuild/linux-ia32": {
192
+ "version": "0.28.2",
193
+ "resolved": "https://registry.npmjs.org/@esbuild/linux-ia32/-/linux-ia32-0.28.2.tgz",
194
+ "integrity": "sha512-CYbnj78HsIeA+DhgUKgFCfvNsTHFhMMrinUrMZpDXJXKN8T3XViTZ/+wtHeVxEWY8ewSzTFN+nRmSwO2tZaLUQ==",
195
+ "cpu": [
196
+ "ia32"
197
+ ],
198
+ "dev": true,
199
+ "license": "MIT",
200
+ "optional": true,
201
+ "os": [
202
+ "linux"
203
+ ],
204
+ "engines": {
205
+ "node": ">=18"
206
+ }
207
+ },
208
+ "node_modules/@esbuild/linux-loong64": {
209
+ "version": "0.28.2",
210
+ "resolved": "https://registry.npmjs.org/@esbuild/linux-loong64/-/linux-loong64-0.28.2.tgz",
211
+ "integrity": "sha512-buwkd8nsph4R+ajRvw0qM5Hja/TXQow3ptzWO2EbG/cqcIkHloRrdlBtQlshyYGTNFvfkfJ5tpPLVkY4DtsPfQ==",
212
+ "cpu": [
213
+ "loong64"
214
+ ],
215
+ "dev": true,
216
+ "license": "MIT",
217
+ "optional": true,
218
+ "os": [
219
+ "linux"
220
+ ],
221
+ "engines": {
222
+ "node": ">=18"
223
+ }
224
+ },
225
+ "node_modules/@esbuild/linux-mips64el": {
226
+ "version": "0.28.2",
227
+ "resolved": "https://registry.npmjs.org/@esbuild/linux-mips64el/-/linux-mips64el-0.28.2.tgz",
228
+ "integrity": "sha512-ZVykbDyk7519VwiNb9Lcj9m8XM6v5V9uKPvrEMkkEedVewf+0itkhahp4HDpgERXhwLRpWFypsGbG/J8s0QjJA==",
229
+ "cpu": [
230
+ "mips64el"
231
+ ],
232
+ "dev": true,
233
+ "license": "MIT",
234
+ "optional": true,
235
+ "os": [
236
+ "linux"
237
+ ],
238
+ "engines": {
239
+ "node": ">=18"
240
+ }
241
+ },
242
+ "node_modules/@esbuild/linux-ppc64": {
243
+ "version": "0.28.2",
244
+ "resolved": "https://registry.npmjs.org/@esbuild/linux-ppc64/-/linux-ppc64-0.28.2.tgz",
245
+ "integrity": "sha512-CAXl+Dtd9UUuJd8pKKdwh6MLm3MUMiqMPmhZ3tTSXPqfyQ3vDl6R5hZdZ/kYojK4ofXtdfSv1tFq8XzWx3heNQ==",
246
+ "cpu": [
247
+ "ppc64"
248
+ ],
249
+ "dev": true,
250
+ "license": "MIT",
251
+ "optional": true,
252
+ "os": [
253
+ "linux"
254
+ ],
255
+ "engines": {
256
+ "node": ">=18"
257
+ }
258
+ },
259
+ "node_modules/@esbuild/linux-riscv64": {
260
+ "version": "0.28.2",
261
+ "resolved": "https://registry.npmjs.org/@esbuild/linux-riscv64/-/linux-riscv64-0.28.2.tgz",
262
+ "integrity": "sha512-GeXCej4IQtU1B+QlDV8W/RRvbzI3O/Stss+/bCXv4lZls5WGRtu2a+3JkA3i4qIUlMXpcHebWpF8AkJhATowuA==",
263
+ "cpu": [
264
+ "riscv64"
265
+ ],
266
+ "dev": true,
267
+ "license": "MIT",
268
+ "optional": true,
269
+ "os": [
270
+ "linux"
271
+ ],
272
+ "engines": {
273
+ "node": ">=18"
274
+ }
275
+ },
276
+ "node_modules/@esbuild/linux-s390x": {
277
+ "version": "0.28.2",
278
+ "resolved": "https://registry.npmjs.org/@esbuild/linux-s390x/-/linux-s390x-0.28.2.tgz",
279
+ "integrity": "sha512-3H1weTYZPxt/WOhByszQZybS9w5lKzUn1FDMsgEChbHWQwHYQQRfBxgCcZvPhjHfKyJjIievvMmEUawJrdY9Dg==",
280
+ "cpu": [
281
+ "s390x"
282
+ ],
283
+ "dev": true,
284
+ "license": "MIT",
285
+ "optional": true,
286
+ "os": [
287
+ "linux"
288
+ ],
289
+ "engines": {
290
+ "node": ">=18"
291
+ }
292
+ },
293
+ "node_modules/@esbuild/linux-x64": {
294
+ "version": "0.28.2",
295
+ "resolved": "https://registry.npmjs.org/@esbuild/linux-x64/-/linux-x64-0.28.2.tgz",
296
+ "integrity": "sha512-4xTZr1FUmSoQW4XIWmit3tzQrUTZM+N3P0XV8xROKYF50XfI7xeO90+1bZvNwxIufQ9hDQVRJH5YhgPVF8A/HQ==",
297
+ "cpu": [
298
+ "x64"
299
+ ],
300
+ "dev": true,
301
+ "license": "MIT",
302
+ "optional": true,
303
+ "os": [
304
+ "linux"
305
+ ],
306
+ "engines": {
307
+ "node": ">=18"
308
+ }
309
+ },
310
+ "node_modules/@esbuild/netbsd-arm64": {
311
+ "version": "0.28.2",
312
+ "resolved": "https://registry.npmjs.org/@esbuild/netbsd-arm64/-/netbsd-arm64-0.28.2.tgz",
313
+ "integrity": "sha512-sSATRjPeDBg3pdgHoQfoYBob11Kk1FGa9lui5RIHZCoCkJa9QKlvl3/vKz2usCmYYjs7ymJR/2Nnsqe+Hjt5nw==",
314
+ "cpu": [
315
+ "arm64"
316
+ ],
317
+ "dev": true,
318
+ "license": "MIT",
319
+ "optional": true,
320
+ "os": [
321
+ "netbsd"
322
+ ],
323
+ "engines": {
324
+ "node": ">=18"
325
+ }
326
+ },
327
+ "node_modules/@esbuild/netbsd-x64": {
328
+ "version": "0.28.2",
329
+ "resolved": "https://registry.npmjs.org/@esbuild/netbsd-x64/-/netbsd-x64-0.28.2.tgz",
330
+ "integrity": "sha512-lqnzCV+mM0gIADaKihiCg6ifgfU2L3h5E33rNQBN1Y4MaVGnzryzmvvf7UHxprpQdE8hpqLolJ9Rl+SkIRDpyw==",
331
+ "cpu": [
332
+ "x64"
333
+ ],
334
+ "dev": true,
335
+ "license": "MIT",
336
+ "optional": true,
337
+ "os": [
338
+ "netbsd"
339
+ ],
340
+ "engines": {
341
+ "node": ">=18"
342
+ }
343
+ },
344
+ "node_modules/@esbuild/openbsd-arm64": {
345
+ "version": "0.28.2",
346
+ "resolved": "https://registry.npmjs.org/@esbuild/openbsd-arm64/-/openbsd-arm64-0.28.2.tgz",
347
+ "integrity": "sha512-AL2qJILH7lNjrDmCQDvdxMfAUIv8KMNZOvrwAQ8i8//ntL9FflhOyMJ8OZSMBb8/AWXe3/5v5S20y3zCoZWKoQ==",
348
+ "cpu": [
349
+ "arm64"
350
+ ],
351
+ "dev": true,
352
+ "license": "MIT",
353
+ "optional": true,
354
+ "os": [
355
+ "openbsd"
356
+ ],
357
+ "engines": {
358
+ "node": ">=18"
359
+ }
360
+ },
361
+ "node_modules/@esbuild/openbsd-x64": {
362
+ "version": "0.28.2",
363
+ "resolved": "https://registry.npmjs.org/@esbuild/openbsd-x64/-/openbsd-x64-0.28.2.tgz",
364
+ "integrity": "sha512-QtiuPytchRyC4rwUKhexJdQKvDuZ6hWloi3igqPQNUJCS1/v9EiO3UTOXR6A3FoMo4fnAKbWJdqaIwhOzh8qEw==",
365
+ "cpu": [
366
+ "x64"
367
+ ],
368
+ "dev": true,
369
+ "license": "MIT",
370
+ "optional": true,
371
+ "os": [
372
+ "openbsd"
373
+ ],
374
+ "engines": {
375
+ "node": ">=18"
376
+ }
377
+ },
378
+ "node_modules/@esbuild/openharmony-arm64": {
379
+ "version": "0.28.2",
380
+ "resolved": "https://registry.npmjs.org/@esbuild/openharmony-arm64/-/openharmony-arm64-0.28.2.tgz",
381
+ "integrity": "sha512-WkhYDmpTjLvGlScA1rwjRUmhl4k8oXR3cIbtqWmELgU/dFeHHlEllxDvdWcNJV9rbzCexB5vz8gtNewWLgCT7Q==",
382
+ "cpu": [
383
+ "arm64"
384
+ ],
385
+ "dev": true,
386
+ "license": "MIT",
387
+ "optional": true,
388
+ "os": [
389
+ "openharmony"
390
+ ],
391
+ "engines": {
392
+ "node": ">=18"
393
+ }
394
+ },
395
+ "node_modules/@esbuild/sunos-x64": {
396
+ "version": "0.28.2",
397
+ "resolved": "https://registry.npmjs.org/@esbuild/sunos-x64/-/sunos-x64-0.28.2.tgz",
398
+ "integrity": "sha512-GPMSkTOtMnv2U2F8gxe4Io6qmVs+YKyp832Etqqxr0hFngmXQ3rzwytelm3GIn7T4VviRUlf3sOgBOiTdvaf7g==",
399
+ "cpu": [
400
+ "x64"
401
+ ],
402
+ "dev": true,
403
+ "license": "MIT",
404
+ "optional": true,
405
+ "os": [
406
+ "sunos"
407
+ ],
408
+ "engines": {
409
+ "node": ">=18"
410
+ }
411
+ },
412
+ "node_modules/@esbuild/win32-arm64": {
413
+ "version": "0.28.2",
414
+ "resolved": "https://registry.npmjs.org/@esbuild/win32-arm64/-/win32-arm64-0.28.2.tgz",
415
+ "integrity": "sha512-PIhhEkE9uPBleRBrQEJpUn7MBnibZzbGzYWPmY3x+YoVg/95zbjB4CxPPOQ8l5tYYM4mMaCthF8/1DIfBQQyWQ==",
416
+ "cpu": [
417
+ "arm64"
418
+ ],
419
+ "dev": true,
420
+ "license": "MIT",
421
+ "optional": true,
422
+ "os": [
423
+ "win32"
424
+ ],
425
+ "engines": {
426
+ "node": ">=18"
427
+ }
428
+ },
429
+ "node_modules/@esbuild/win32-ia32": {
430
+ "version": "0.28.2",
431
+ "resolved": "https://registry.npmjs.org/@esbuild/win32-ia32/-/win32-ia32-0.28.2.tgz",
432
+ "integrity": "sha512-YmJbfTlvU7Sdn9BB+4PRES4oB6pxgS37MAONj+hBr/cpXS1aBPKXxNnDbu+QCWPj0o9dgyxeq79g6c5P8KeuYA==",
433
+ "cpu": [
434
+ "ia32"
435
+ ],
436
+ "dev": true,
437
+ "license": "MIT",
438
+ "optional": true,
439
+ "os": [
440
+ "win32"
441
+ ],
442
+ "engines": {
443
+ "node": ">=18"
444
+ }
445
+ },
446
+ "node_modules/@esbuild/win32-x64": {
447
+ "version": "0.28.2",
448
+ "resolved": "https://registry.npmjs.org/@esbuild/win32-x64/-/win32-x64-0.28.2.tgz",
449
+ "integrity": "sha512-5ebpxr3nWMzrL/rnUI755Jkuee0bHL/Gq0WTF9lvcpv73wAp5eu8MfBUgWK9bhWvZjj7yX8etf/8tI8Ney695g==",
450
+ "cpu": [
451
+ "x64"
452
+ ],
453
+ "dev": true,
454
+ "license": "MIT",
455
+ "optional": true,
456
+ "os": [
457
+ "win32"
458
+ ],
459
+ "engines": {
460
+ "node": ">=18"
461
+ }
462
+ },
463
+ "node_modules/@fontsource-variable/google-sans-code": {
464
+ "version": "5.3.2",
465
+ "resolved": "https://registry.npmjs.org/@fontsource-variable/google-sans-code/-/google-sans-code-5.3.2.tgz",
466
+ "integrity": "sha512-kW4RcfoTTObtUWAhlh6hzs8LCDsBNLwVRPqjUIKMkEiabfR3SRGP0Y6erFKd9k1yaRppTSCLSVkxhPKpEW3QPg==",
467
+ "license": "OFL-1.1",
468
+ "funding": {
469
+ "url": "https://github.com/sponsors/ayuhito"
470
+ }
471
+ },
472
+ "node_modules/@fontsource-variable/host-grotesk": {
473
+ "version": "5.3.0",
474
+ "resolved": "https://registry.npmjs.org/@fontsource-variable/host-grotesk/-/host-grotesk-5.3.0.tgz",
475
+ "integrity": "sha512-WvdqilphrdjYLeCHXny9gkdpIZgzyeHpgRWB8T4LSKs5nVnCs6pYYeCeMiRshxcuB6djqL6CSdF1Hq0BvMNCwA==",
476
+ "license": "OFL-1.1",
477
+ "funding": {
478
+ "url": "https://github.com/sponsors/ayuhito"
479
+ }
480
+ },
481
+ "node_modules/@lezer/common": {
482
+ "version": "1.5.5",
483
+ "resolved": "https://registry.npmjs.org/@lezer/common/-/common-1.5.5.tgz",
484
+ "integrity": "sha512-Pw7PVCndruF1pb3ebCxpSVpkMHSDjwRCLcTRw+RizUKco5QbWylwq2g4lnG/RYN7yTTQDXE9F77MwtkAyphrjg==",
485
+ "license": "MIT"
486
+ },
487
+ "node_modules/@lezer/go": {
488
+ "version": "1.0.1",
489
+ "resolved": "https://registry.npmjs.org/@lezer/go/-/go-1.0.1.tgz",
490
+ "integrity": "sha512-xToRsYxwsgJNHTgNdStpcvmbVuKxTapV0dM0wey1geMMRc9aggoVyKgzYp41D2/vVOx+Ii4hmE206kvxIXBVXQ==",
491
+ "license": "MIT",
492
+ "dependencies": {
493
+ "@lezer/common": "^1.2.0",
494
+ "@lezer/highlight": "^1.0.0",
495
+ "@lezer/lr": "^1.3.0"
496
+ }
497
+ },
498
+ "node_modules/@lezer/highlight": {
499
+ "version": "1.2.5",
500
+ "resolved": "https://registry.npmjs.org/@lezer/highlight/-/highlight-1.2.5.tgz",
501
+ "integrity": "sha512-O1GMVKgtf5YspFaRzpmqkVgMtIk2HG9uMAbH6wdAIdNh3TVzCsch/+cRi+6l2//UGDKe6NB9BW3cvuR5ezMQ0w==",
502
+ "license": "MIT",
503
+ "dependencies": {
504
+ "@lezer/common": "^1.3.0"
505
+ }
506
+ },
507
+ "node_modules/@lezer/javascript": {
508
+ "version": "1.5.6",
509
+ "resolved": "https://registry.npmjs.org/@lezer/javascript/-/javascript-1.5.6.tgz",
510
+ "integrity": "sha512-sEvOqpl4vJQca0NugSQK02vQPbc5KrvoojnYGoqqFgFO0MtcYhwGiHKVEB+LuXYJnfyXo4QPA8ztbrEa4Fw5NA==",
511
+ "license": "MIT",
512
+ "dependencies": {
513
+ "@lezer/common": "^1.2.0",
514
+ "@lezer/highlight": "^1.1.3",
515
+ "@lezer/lr": "^1.3.0"
516
+ }
517
+ },
518
+ "node_modules/@lezer/lr": {
519
+ "version": "1.4.12",
520
+ "resolved": "https://registry.npmjs.org/@lezer/lr/-/lr-1.4.12.tgz",
521
+ "integrity": "sha512-On9HU4oMMiHPMkQDgoTnGahjgG5n/MkN38CgG77sMraKLL+3JBbeQNi1FcCXV9QRqwF9/BUXP3caHyJC1fBOTQ==",
522
+ "license": "MIT",
523
+ "dependencies": {
524
+ "@lezer/common": "^1.0.0"
525
+ }
526
+ },
527
+ "node_modules/@lezer/python": {
528
+ "version": "1.1.19",
529
+ "resolved": "https://registry.npmjs.org/@lezer/python/-/python-1.1.19.tgz",
530
+ "integrity": "sha512-MhQIURHRytsNzP/YXnqpYKW6la6voAH3kyplTOOiCdjyFY6cWWGFVmYVdHIPrElqSDf4iCDktQCockB9FxuhzQ==",
531
+ "license": "MIT",
532
+ "dependencies": {
533
+ "@lezer/common": "^1.2.0",
534
+ "@lezer/highlight": "^1.0.0",
535
+ "@lezer/lr": "^1.0.0"
536
+ }
537
+ },
538
+ "node_modules/@lezer/rust": {
539
+ "version": "1.0.3",
540
+ "resolved": "https://registry.npmjs.org/@lezer/rust/-/rust-1.0.3.tgz",
541
+ "integrity": "sha512-XxErOjZzQ7yJt1agUT4fu9qQvESZ3acgoxpPaPTPOiUx+duCjaVAtZGFIgphkHxlN05djdVAIOy/wItShMEjqQ==",
542
+ "license": "MIT",
543
+ "dependencies": {
544
+ "@lezer/common": "^1.2.0",
545
+ "@lezer/highlight": "^1.0.0",
546
+ "@lezer/lr": "^1.0.0"
547
+ }
548
+ },
549
+ "node_modules/esbuild": {
550
+ "version": "0.28.2",
551
+ "resolved": "https://registry.npmjs.org/esbuild/-/esbuild-0.28.2.tgz",
552
+ "integrity": "sha512-HKVLS8dvII+xoKW9kmqxbRKrnWEXfJJr/FZhhJmiqIB0e053QNYFqOBouTMO/k5sID4MvCiUCvv8b9M4h32wIA==",
553
+ "dev": true,
554
+ "hasInstallScript": true,
555
+ "license": "MIT",
556
+ "bin": {
557
+ "esbuild": "bin/esbuild"
558
+ },
559
+ "engines": {
560
+ "node": ">=18"
561
+ },
562
+ "optionalDependencies": {
563
+ "@esbuild/aix-ppc64": "0.28.2",
564
+ "@esbuild/android-arm": "0.28.2",
565
+ "@esbuild/android-arm64": "0.28.2",
566
+ "@esbuild/android-x64": "0.28.2",
567
+ "@esbuild/darwin-arm64": "0.28.2",
568
+ "@esbuild/darwin-x64": "0.28.2",
569
+ "@esbuild/freebsd-arm64": "0.28.2",
570
+ "@esbuild/freebsd-x64": "0.28.2",
571
+ "@esbuild/linux-arm": "0.28.2",
572
+ "@esbuild/linux-arm64": "0.28.2",
573
+ "@esbuild/linux-ia32": "0.28.2",
574
+ "@esbuild/linux-loong64": "0.28.2",
575
+ "@esbuild/linux-mips64el": "0.28.2",
576
+ "@esbuild/linux-ppc64": "0.28.2",
577
+ "@esbuild/linux-riscv64": "0.28.2",
578
+ "@esbuild/linux-s390x": "0.28.2",
579
+ "@esbuild/linux-x64": "0.28.2",
580
+ "@esbuild/netbsd-arm64": "0.28.2",
581
+ "@esbuild/netbsd-x64": "0.28.2",
582
+ "@esbuild/openbsd-arm64": "0.28.2",
583
+ "@esbuild/openbsd-x64": "0.28.2",
584
+ "@esbuild/openharmony-arm64": "0.28.2",
585
+ "@esbuild/sunos-x64": "0.28.2",
586
+ "@esbuild/win32-arm64": "0.28.2",
587
+ "@esbuild/win32-ia32": "0.28.2",
588
+ "@esbuild/win32-x64": "0.28.2"
589
+ }
590
+ }
591
+ }
592
+ }
web/package.json ADDED
@@ -0,0 +1,21 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "name": "codehygiene-space-web",
3
+ "private": true,
4
+ "type": "module",
5
+ "scripts": {
6
+ "build": "node build.mjs",
7
+ "dev": "node build.mjs --watch"
8
+ },
9
+ "dependencies": {
10
+ "@fontsource-variable/google-sans-code": "5.3.2",
11
+ "@fontsource-variable/host-grotesk": "5.3.0",
12
+ "@lezer/go": "1.0.1",
13
+ "@lezer/highlight": "1.2.5",
14
+ "@lezer/javascript": "1.5.6",
15
+ "@lezer/python": "1.1.19",
16
+ "@lezer/rust": "1.0.3"
17
+ },
18
+ "devDependencies": {
19
+ "esbuild": "0.28.2"
20
+ }
21
+ }
web/src/board.css ADDED
@@ -0,0 +1,398 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ .board-page {
2
+ min-height: 100svh;
3
+ }
4
+
5
+ .board {
6
+ max-width: 800px;
7
+ margin: 0 auto;
8
+ padding: 0 24px 64px;
9
+ }
10
+
11
+ .board-head {
12
+ display: grid;
13
+ grid-template-columns: 1fr auto 1fr;
14
+ align-items: center;
15
+ gap: 16px;
16
+ height: 72px;
17
+ }
18
+
19
+ .board-home {
20
+ display: inline-flex;
21
+ align-items: center;
22
+ gap: 8px;
23
+ justify-self: start;
24
+ border-radius: var(--radius-sm);
25
+ color: var(--ink-2);
26
+ font-weight: 500;
27
+ letter-spacing: -0.01em;
28
+ transition: color var(--motion) var(--ease);
29
+ }
30
+
31
+ .board-home .icon {
32
+ color: var(--ink-3);
33
+ transition: transform 160ms var(--ease-out);
34
+ }
35
+
36
+ .board-home:hover {
37
+ color: var(--ink);
38
+ }
39
+
40
+ .board-home:hover .icon {
41
+ transform: translateX(-2px);
42
+ }
43
+
44
+ .board-tabs {
45
+ display: flex;
46
+ gap: 2px;
47
+ padding: 3px;
48
+ border-radius: var(--radius);
49
+ background: var(--fill);
50
+ }
51
+
52
+ .board-tab {
53
+ display: inline-flex;
54
+ align-items: center;
55
+ height: 28px;
56
+ padding: 0 14px;
57
+ border-radius: 7px;
58
+ color: var(--ink-3);
59
+ font-size: 13.5px;
60
+ transition: color var(--motion) var(--ease), background-color var(--motion) var(--ease);
61
+ }
62
+
63
+ .board-tab:hover {
64
+ color: var(--ink);
65
+ }
66
+
67
+ .board-tab[aria-current="page"] {
68
+ background: var(--surface);
69
+ color: var(--ink);
70
+ box-shadow: 0 1px 2px var(--shadow-color), 0 0 0 1px var(--line);
71
+ }
72
+
73
+ .board-updated {
74
+ justify-self: end;
75
+ color: var(--ink-3);
76
+ font-size: 12.5px;
77
+ white-space: nowrap;
78
+ }
79
+
80
+ .board-view {
81
+ padding-top: 8px;
82
+ }
83
+
84
+ .board-modes {
85
+ display: flex;
86
+ gap: 20px;
87
+ margin-bottom: 12px;
88
+ }
89
+
90
+ .board-mode {
91
+ height: 28px;
92
+ border-radius: var(--radius-sm);
93
+ color: var(--ink-3);
94
+ font-size: 14px;
95
+ transition: color var(--motion) var(--ease);
96
+ }
97
+
98
+ .board-mode:hover,
99
+ .board-mode[aria-pressed="true"] {
100
+ color: var(--ink);
101
+ }
102
+
103
+ .board-mode[aria-pressed="true"] {
104
+ font-weight: 500;
105
+ }
106
+
107
+ .board-list {
108
+ margin: 0;
109
+ padding: 0;
110
+ overflow: hidden;
111
+ list-style: none;
112
+ border: 1px solid var(--line);
113
+ border-radius: var(--radius-stage);
114
+ background: var(--surface);
115
+ box-shadow: var(--shadow-stage);
116
+ }
117
+
118
+ .board-row {
119
+ display: grid;
120
+ grid-template-columns: 32px minmax(0, 1fr) 160px 44px;
121
+ align-items: center;
122
+ gap: 16px;
123
+ height: 48px;
124
+ padding: 0 20px;
125
+ border-top: 1px solid var(--line);
126
+ font-size: 14px;
127
+ }
128
+
129
+ .board-row:first-child {
130
+ border-top: 0;
131
+ }
132
+
133
+ .board-row.is-yours {
134
+ background: var(--selected);
135
+ }
136
+
137
+ .board-row.is-yours .track {
138
+ background: var(--surface);
139
+ }
140
+
141
+ .board-rank {
142
+ color: var(--ink-3);
143
+ font-variant-numeric: tabular-nums;
144
+ }
145
+
146
+ .board-repo {
147
+ display: inline-flex;
148
+ align-items: center;
149
+ gap: 10px;
150
+ min-width: 0;
151
+ border-radius: var(--radius-sm);
152
+ color: var(--ink);
153
+ font-weight: 500;
154
+ }
155
+
156
+ .board-repo .icon {
157
+ flex: none;
158
+ color: var(--ink-3);
159
+ }
160
+
161
+ .board-repo-name {
162
+ overflow: hidden;
163
+ text-overflow: ellipsis;
164
+ white-space: nowrap;
165
+ }
166
+
167
+ .board-owner {
168
+ color: var(--ink-3);
169
+ font-weight: 400;
170
+ }
171
+
172
+ .board-repo:hover .board-repo-name {
173
+ text-decoration: underline 1px var(--line-strong);
174
+ text-underline-offset: 4px;
175
+ }
176
+
177
+ .board-past {
178
+ color: var(--ink-3);
179
+ font-variant-numeric: tabular-nums;
180
+ text-align: right;
181
+ }
182
+
183
+ .board-score {
184
+ font-weight: 500;
185
+ font-variant-numeric: tabular-nums;
186
+ text-align: right;
187
+ }
188
+
189
+ .board-score.is-up {
190
+ color: var(--green);
191
+ }
192
+
193
+ .board-empty {
194
+ padding: 48px 0;
195
+ color: var(--ink-3);
196
+ text-align: center;
197
+ }
198
+
199
+ .track {
200
+ display: block;
201
+ height: 6px;
202
+ overflow: hidden;
203
+ border-radius: 3px;
204
+ background: var(--fill);
205
+ }
206
+
207
+ .track i {
208
+ display: block;
209
+ height: 100%;
210
+ border-radius: inherit;
211
+ background: var(--ink-2);
212
+ }
213
+
214
+ .tiles {
215
+ display: grid;
216
+ grid-template-columns: repeat(4, minmax(0, 1fr));
217
+ gap: 12px;
218
+ }
219
+
220
+ .tile,
221
+ .panel {
222
+ border: 1px solid var(--line);
223
+ border-radius: var(--radius);
224
+ background: var(--surface);
225
+ }
226
+
227
+ .tile {
228
+ padding: 14px 16px;
229
+ }
230
+
231
+ .tile-label,
232
+ .tile-note,
233
+ .panel-note,
234
+ .reward-fact dt {
235
+ color: var(--ink-3);
236
+ font-size: 12.5px;
237
+ line-height: 18px;
238
+ }
239
+
240
+ .tile-value {
241
+ margin: 4px 0 2px;
242
+ font-size: 26px;
243
+ font-weight: 500;
244
+ line-height: 32px;
245
+ letter-spacing: -0.02em;
246
+ font-variant-numeric: tabular-nums;
247
+ }
248
+
249
+ .charts {
250
+ display: grid;
251
+ grid-template-columns: repeat(2, minmax(0, 1fr));
252
+ gap: 12px;
253
+ margin-top: 12px;
254
+ }
255
+
256
+ .panel {
257
+ padding: 16px;
258
+ }
259
+
260
+ .panel-title {
261
+ font-size: 14px;
262
+ font-weight: 500;
263
+ line-height: 20px;
264
+ }
265
+
266
+ .bars {
267
+ display: grid;
268
+ gap: 10px;
269
+ margin-top: 14px;
270
+ }
271
+
272
+ .bar-row {
273
+ display: grid;
274
+ grid-template-columns: minmax(0, 1fr) minmax(0, 1fr) 48px;
275
+ align-items: center;
276
+ gap: 12px;
277
+ font-size: 13px;
278
+ line-height: 18px;
279
+ }
280
+
281
+ .bar-label {
282
+ overflow: hidden;
283
+ color: var(--ink-2);
284
+ text-overflow: ellipsis;
285
+ white-space: nowrap;
286
+ }
287
+
288
+ .bar-value {
289
+ font-variant-numeric: tabular-nums;
290
+ text-align: right;
291
+ }
292
+
293
+ .panel-empty {
294
+ color: var(--ink-3);
295
+ font-size: 13px;
296
+ }
297
+
298
+ .reward {
299
+ margin-top: 12px;
300
+ padding: 0;
301
+ }
302
+
303
+ .reward-head {
304
+ display: flex;
305
+ align-items: baseline;
306
+ justify-content: space-between;
307
+ gap: 16px;
308
+ padding: 16px 16px 0;
309
+ }
310
+
311
+ .reward-source {
312
+ display: inline-flex;
313
+ align-items: center;
314
+ gap: 4px;
315
+ border-radius: var(--radius-sm);
316
+ color: var(--ink-3);
317
+ font-size: 12.5px;
318
+ transition: color var(--motion) var(--ease);
319
+ }
320
+
321
+ .reward-source .icon {
322
+ width: 10px;
323
+ height: 10px;
324
+ }
325
+
326
+ .reward-source:hover {
327
+ color: var(--ink);
328
+ }
329
+
330
+ .reward-facts {
331
+ display: grid;
332
+ grid-template-columns: repeat(4, minmax(0, 1fr));
333
+ margin: 14px 0 0;
334
+ border-top: 1px solid var(--line);
335
+ }
336
+
337
+ .reward-fact {
338
+ padding: 12px 16px 14px;
339
+ border-left: 1px solid var(--line);
340
+ }
341
+
342
+ .reward-fact:first-child {
343
+ border-left: 0;
344
+ }
345
+
346
+ .reward-fact dd {
347
+ margin: 2px 0 0;
348
+ font-size: 15px;
349
+ font-weight: 500;
350
+ line-height: 22px;
351
+ font-variant-numeric: tabular-nums;
352
+ white-space: nowrap;
353
+ }
354
+
355
+ @media (max-width: 640px) {
356
+ .board {
357
+ padding: 0 16px 48px;
358
+ }
359
+
360
+ .board-head {
361
+ grid-template-columns: 1fr auto;
362
+ height: auto;
363
+ padding: 16px 0 8px;
364
+ }
365
+
366
+ .board-home span,
367
+ .board-updated {
368
+ display: none;
369
+ }
370
+
371
+ .board-row {
372
+ grid-template-columns: 24px minmax(0, 1fr) 44px;
373
+ gap: 12px;
374
+ padding: 0 14px;
375
+ }
376
+
377
+ .board-row .track,
378
+ .board-row .board-past {
379
+ display: none;
380
+ }
381
+
382
+ .tiles,
383
+ .reward-facts {
384
+ grid-template-columns: repeat(2, minmax(0, 1fr));
385
+ }
386
+
387
+ .charts {
388
+ grid-template-columns: minmax(0, 1fr);
389
+ }
390
+
391
+ .reward-fact:nth-child(3) {
392
+ border-left: 0;
393
+ }
394
+
395
+ .reward-fact:nth-child(n + 3) {
396
+ border-top: 1px solid var(--line);
397
+ }
398
+ }
web/src/board.html ADDED
@@ -0,0 +1,70 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <!doctype html>
2
+ <html lang="en" data-root="../">
3
+ <head>
4
+ <meta charset="utf-8">
5
+ <meta name="viewport" content="width=device-width, initial-scale=1">
6
+ <meta name="description" content="GitHub repos ranked by how readable their code is, plus live stats.">
7
+ <title>Leaderboard · codehygiene</title>
8
+ <link rel="stylesheet" href="../styles.css">
9
+ <link rel="stylesheet" href="../board.css">
10
+ <script type="module" src="../board.js"></script>
11
+ </head>
12
+ <body class="board-page">
13
+ <svg class="sprite" aria-hidden="true">
14
+ <symbol id="i-github" viewBox="0 0 16 16"><path fill="currentColor" stroke="none" d="M8 0c4.42 0 8 3.58 8 8a8.013 8.013 0 0 1-5.45 7.59c-.4.08-.55-.17-.55-.38 0-.27.01-1.13.01-2.2 0-.75-.25-1.23-.54-1.48 1.78-.2 3.65-.88 3.65-3.95 0-.88-.31-1.59-.82-2.15.08-.2.36-1.02-.08-2.12 0 0-.67-.22-2.2.82-.64-.18-1.32-.27-2-.27-.68 0-1.36.09-2 .27-1.53-1.03-2.2-.82-2.2-.82-.44 1.1-.16 1.92-.08 2.12-.51.56-.82 1.28-.82 2.15 0 3.06 1.86 3.75 3.64 3.95-.23.2-.44.55-.51 1.07-.46.21-1.61.55-2.33-.66-.15-.24-.6-.83-1.23-.82-.67.01-.27.38.01.53.34.19.73.9.82 1.13.16.45.68 1.31 2.69.94 0 .67.01 1.3.01 1.49 0 .21-.15.45-.55.38A7.995 7.995 0 0 1 0 8c0-4.42 3.58-8 8-8Z"/></symbol>
15
+ <symbol id="i-back" viewBox="0 0 24 24"><path d="M19 12H5"/><path d="m11 18-6-6 6-6"/></symbol>
16
+ <symbol id="i-external" viewBox="5 5 14 14"><path d="M7 17 17 7"/><path d="M8 7h9v9"/></symbol>
17
+ </svg>
18
+
19
+ <div class="board">
20
+ <header class="board-head">
21
+ <a class="board-home" href="../" title="Back to CodeHygiene">
22
+ <svg class="icon" aria-hidden="true"><use href="#i-back"/></svg>
23
+ <span>CodeHygiene</span>
24
+ </a>
25
+ <nav class="board-tabs" aria-label="Views">
26
+ <a class="board-tab" id="tab-board" href="#board">Leaderboard</a>
27
+ <a class="board-tab" id="tab-stats" href="#stats">Stats</a>
28
+ </nav>
29
+ <p class="board-updated" id="board-updated"></p>
30
+ </header>
31
+
32
+ <main>
33
+ <section class="board-view" id="view-board" aria-labelledby="tab-board">
34
+ <div class="board-modes" role="group" aria-label="Order">
35
+ <button class="board-mode" type="button" data-mode="cleanest" aria-pressed="true">Cleanest</button>
36
+ <button class="board-mode" type="button" data-mode="improved" aria-pressed="false" title="Score a year ago → now">Most improved</button>
37
+ </div>
38
+ <ol class="board-list" id="board-list"></ol>
39
+ <p class="board-empty" id="board-empty" hidden>No repos yet</p>
40
+ </section>
41
+
42
+ <section class="board-view" id="view-stats" aria-labelledby="tab-stats" hidden>
43
+ <div class="tiles" id="tiles"></div>
44
+ <div class="charts">
45
+ <section class="panel">
46
+ <h2 class="panel-title">Most fixed</h2>
47
+ <p class="panel-note">Issues removed</p>
48
+ <div class="bars" id="chart-kinds"></div>
49
+ </section>
50
+ <section class="panel">
51
+ <h2 class="panel-title">Cleanest language</h2>
52
+ <p class="panel-note">Average score, higher is better</p>
53
+ <div class="bars" id="chart-languages"></div>
54
+ </section>
55
+ </div>
56
+ <section class="panel reward" id="reward" hidden>
57
+ <header class="reward-head">
58
+ <h2 class="panel-title">As a reward</h2>
59
+ <a class="reward-source" id="reward-source" target="_blank" rel="noopener">
60
+ <span id="reward-version"></span>
61
+ <svg class="icon" aria-hidden="true"><use href="#i-external"/></svg>
62
+ </a>
63
+ </header>
64
+ <dl class="reward-facts" id="reward-facts"></dl>
65
+ </section>
66
+ </section>
67
+ </main>
68
+ </div>
69
+ </body>
70
+ </html>
web/src/board.js ADDED
@@ -0,0 +1,259 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ // Leaderboard and stats page. Opens in its own tab from the main page.
2
+ import "./board.css";
3
+ import { byId, el } from "./dom.js";
4
+ import { LANGUAGES, NOTES, sentenceCase } from "./labels.js";
5
+ import { fetchLeaderboard, fetchStats } from "./refiner.js";
6
+ import REWARD_FACTS from "./reward-facts.json";
7
+ import { formatCount, formatMs } from "./telemetry.js";
8
+
9
+ const SVG_NS = "http://www.w3.org/2000/svg";
10
+ const MIN_LANGUAGE_FILES = 20;
11
+ const MAX_KINDS = 5;
12
+ const MAX_LANGUAGES = 6;
13
+ const MINUTE = 60_000;
14
+ const HOUR = 60 * MINUTE;
15
+ const DAY = 24 * HOUR;
16
+
17
+ // reward-facts.json holds published benchmark figures per scorer version, copied from the README
18
+ // benchmark table at each release. The strip stays hidden when the running scorer has no entry.
19
+ const README_AT = "https://github.com/brianbell-x/codehygiene/blob";
20
+
21
+ const state = { mode: "cleanest", board: null, stats: null, yours: null };
22
+
23
+ function icon(name) {
24
+ const svg = document.createElementNS(SVG_NS, "svg");
25
+ svg.setAttribute("class", "icon");
26
+ svg.setAttribute("aria-hidden", "true");
27
+ const use = document.createElementNS(SVG_NS, "use");
28
+ use.setAttribute("href", `#${name}`);
29
+ svg.append(use);
30
+ return svg;
31
+ }
32
+
33
+ function points(score) {
34
+ return String(Math.round(score));
35
+ }
36
+
37
+ function track(percent) {
38
+ const width = Math.max(0, Math.min(100, percent));
39
+ return el("span", { class: "track", "aria-hidden": "true" }, [el("i", { style: `width: ${width}%` })]);
40
+ }
41
+
42
+ function since(isoTime) {
43
+ const elapsed = Date.now() - Date.parse(isoTime);
44
+ if (elapsed < MINUTE) {
45
+ return "Updated just now";
46
+ }
47
+ if (elapsed < HOUR) {
48
+ return `Updated ${Math.floor(elapsed / MINUTE)} min ago`;
49
+ }
50
+ if (elapsed < DAY) {
51
+ return `Updated ${Math.floor(elapsed / HOUR)} h ago`;
52
+ }
53
+ return `Updated ${Math.floor(elapsed / DAY)} d ago`;
54
+ }
55
+
56
+ function repoCell(repo) {
57
+ const [owner, name] = repo.split("/");
58
+ return el("a", {
59
+ class: "board-repo",
60
+ href: `https://github.com/${repo}`,
61
+ target: "_blank",
62
+ rel: "noopener",
63
+ }, [
64
+ icon("i-github"),
65
+ el("span", { class: "board-repo-name" }, [
66
+ el("span", { class: "board-owner", text: `${owner}/` }),
67
+ el("span", { text: name }),
68
+ ]),
69
+ ]);
70
+ }
71
+
72
+ function boardRow(row, index) {
73
+ const improved = state.mode === "improved";
74
+ const yours = state.yours && row.repo.toLowerCase() === state.yours;
75
+ const middle = improved
76
+ ? el("span", { class: "board-past", text: `${points(row.past_score)} → ${points(row.score)}` })
77
+ : track(row.score);
78
+ const value = improved ? `+${points(row.change)}` : points(row.score);
79
+ return el("li", { class: yours ? "board-row is-yours" : "board-row" }, [
80
+ el("span", { class: "board-rank", text: String(index + 1) }),
81
+ repoCell(row.repo),
82
+ middle,
83
+ el("span", { class: improved ? "board-score is-up" : "board-score", text: value }),
84
+ ]);
85
+ }
86
+
87
+ function renderBoard() {
88
+ const rows = state.board?.[state.mode] ?? [];
89
+ byId("board-list").replaceChildren(...rows.map(boardRow));
90
+ byId("board-list").hidden = rows.length === 0;
91
+ byId("board-empty").hidden = rows.length > 0 || !state.board;
92
+ for (const button of document.querySelectorAll(".board-mode")) {
93
+ button.setAttribute("aria-pressed", String(button.dataset.mode === state.mode));
94
+ }
95
+ byId("board-list").querySelector(".is-yours")?.scrollIntoView({ block: "center" });
96
+ }
97
+
98
+ function tile(label, value, note) {
99
+ return el("div", { class: "tile" }, [
100
+ el("p", { class: "tile-label", text: label }),
101
+ el("p", { class: "tile-value", text: value }),
102
+ el("p", { class: "tile-note", text: note }),
103
+ ]);
104
+ }
105
+
106
+ function mergeTile(stats) {
107
+ const decided = stats.prs_decided;
108
+ if (!decided) {
109
+ return tile("PRs merged", "—", "No PRs yet");
110
+ }
111
+ const rate = `${Math.round(stats.pr_merge_rate * 100)}%`;
112
+ return tile("PRs merged", rate, `${formatCount(stats.prs_merged)} of ${formatCount(decided)}`);
113
+ }
114
+
115
+ function statTiles(stats) {
116
+ const timed = stats.score_ms_median === null ? "—" : formatMs(stats.score_ms_median);
117
+ const typical = stats.median_score === null ? "—" : String(stats.median_score);
118
+ return [
119
+ mergeTile(stats),
120
+ tile("Issues fixed", formatCount(stats.issues_fixed), `in ${formatCount(stats.files_cleaned)} files`),
121
+ tile("Scoring time", timed, "per file, median"),
122
+ tile("Typical file", typical, `of 100, ${formatCount(stats.files_scored)} scored`),
123
+ ];
124
+ }
125
+
126
+ function barRow(label, percent, value) {
127
+ return el("div", { class: "bar-row" }, [
128
+ el("span", { class: "bar-label", text: label }),
129
+ track(percent),
130
+ el("span", { class: "bar-value", text: value }),
131
+ ]);
132
+ }
133
+
134
+ function emptyChart() {
135
+ return el("p", { class: "panel-empty", text: "No data yet" });
136
+ }
137
+
138
+ function kindBars(stats) {
139
+ const kinds = Object.entries(stats.squiggles_resolved_by_kind)
140
+ .filter(([kind, count]) => count > 0 && kind in NOTES)
141
+ .sort((left, right) => right[1] - left[1])
142
+ .slice(0, MAX_KINDS);
143
+ const top = kinds[0]?.[1] ?? 1;
144
+ const rows = kinds.map(([kind, count]) => {
145
+ const label = sentenceCase(NOTES[kind]);
146
+ return barRow(label, (count / top) * 100, formatCount(count));
147
+ });
148
+ return rows.length ? rows : [emptyChart()];
149
+ }
150
+
151
+ function languageAverages(byExt) {
152
+ const totals = new Map();
153
+ for (const [ext, row] of Object.entries(byExt)) {
154
+ const language = LANGUAGES[ext];
155
+ if (!language || row.avg_score === null) {
156
+ continue;
157
+ }
158
+ const total = totals.get(language) ?? { files: 0, points: 0 };
159
+ total.files += row.scored;
160
+ total.points += row.avg_score * row.scored;
161
+ totals.set(language, total);
162
+ }
163
+ return [...totals]
164
+ .filter(([, total]) => total.files >= MIN_LANGUAGE_FILES)
165
+ .map(([language, total]) => [language, total.points / total.files])
166
+ .sort((left, right) => right[1] - left[1])
167
+ .slice(0, MAX_LANGUAGES);
168
+ }
169
+
170
+ function languageBars(stats) {
171
+ const averages = languageAverages(stats.by_ext);
172
+ const rows = averages.map(([language, average]) => barRow(language, average, points(average)));
173
+ return rows.length ? rows : [emptyChart()];
174
+ }
175
+
176
+ function rewardFact(fact) {
177
+ return el("div", { class: "reward-fact", title: fact.title }, [
178
+ el("dt", { text: fact.label }),
179
+ el("dd", { text: fact.value }),
180
+ ]);
181
+ }
182
+
183
+ function renderReward(version) {
184
+ const reward = REWARD_FACTS[version];
185
+ byId("reward").hidden = !reward;
186
+ if (!reward) {
187
+ return;
188
+ }
189
+ byId("reward-version").textContent = `Benchmarks, v${version}`;
190
+ byId("reward-source").href = `${README_AT}/${reward.ref}/README.md#benchmarks`;
191
+ byId("reward-facts").replaceChildren(...reward.facts.map(rewardFact));
192
+ }
193
+
194
+ function renderStats() {
195
+ const stats = state.stats;
196
+ if (!stats) {
197
+ return;
198
+ }
199
+ byId("tiles").replaceChildren(...statTiles(stats));
200
+ byId("chart-kinds").replaceChildren(...kindBars(stats));
201
+ byId("chart-languages").replaceChildren(...languageBars(stats));
202
+ renderReward(stats.scorer_version);
203
+ }
204
+
205
+ function currentView() {
206
+ return window.location.hash === "#stats" ? "stats" : "board";
207
+ }
208
+
209
+ function updatedText() {
210
+ if (currentView() === "stats") {
211
+ return "Live";
212
+ }
213
+ const updated = state.board?.updated_at;
214
+ return updated ? since(updated) : "";
215
+ }
216
+
217
+ function renderUpdated() {
218
+ byId("board-updated").textContent = updatedText();
219
+ }
220
+
221
+ function showView() {
222
+ const view = currentView();
223
+ byId("view-board").hidden = view !== "board";
224
+ byId("view-stats").hidden = view !== "stats";
225
+ byId("tab-board").setAttribute("aria-current", view === "board" ? "page" : "false");
226
+ byId("tab-stats").setAttribute("aria-current", view === "stats" ? "page" : "false");
227
+ renderUpdated();
228
+ }
229
+
230
+ function readYours() {
231
+ const match = window.location.hash.match(/^#repo=(.+)$/);
232
+ return match ? decodeURIComponent(match[1]).toLowerCase() : null;
233
+ }
234
+
235
+ function boot() {
236
+ state.yours = readYours();
237
+ for (const button of document.querySelectorAll(".board-mode")) {
238
+ button.addEventListener("click", () => {
239
+ state.mode = button.dataset.mode;
240
+ renderBoard();
241
+ });
242
+ }
243
+ window.addEventListener("hashchange", showView);
244
+ showView();
245
+ fetchLeaderboard().then((board) => {
246
+ state.board = board;
247
+ renderBoard();
248
+ renderUpdated();
249
+ }, () => {
250
+ state.board = { cleanest: [], improved: [] };
251
+ renderBoard();
252
+ });
253
+ fetchStats().then((stats) => {
254
+ state.stats = stats;
255
+ renderStats();
256
+ }, () => byId("tiles").replaceChildren(emptyChart()));
257
+ }
258
+
259
+ boot();
web/src/columns.js ADDED
@@ -0,0 +1,25 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ const TAB_STOP = 2;
2
+
3
+ export function scorerSpanToOffsets(lineText, colStart, colEnd) {
4
+ const startCol = colStart - 1;
5
+ const endCol = colEnd - 1;
6
+ let expandedCol = 0;
7
+ let from = null;
8
+ let index = 0;
9
+
10
+ while (index < lineText.length) {
11
+ const char = String.fromCodePoint(lineText.codePointAt(index));
12
+ const width = char === "\t" ? TAB_STOP - (expandedCol % TAB_STOP) : 1;
13
+ const nextCol = expandedCol + width;
14
+ if (from === null && nextCol > startCol) {
15
+ from = index;
16
+ }
17
+ if (nextCol > endCol) {
18
+ return { from, to: index + char.length };
19
+ }
20
+ expandedCol = nextCol;
21
+ index += char.length;
22
+ }
23
+ const lineEnd = lineText.length;
24
+ return { from: from ?? lineEnd, to: lineEnd };
25
+ }
web/src/diff.js ADDED
@@ -0,0 +1,90 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ const MAX_CELLS = 4_000_000;
2
+
3
+ function trimCommon(before, after) {
4
+ let head = 0;
5
+ while (head < before.length && head < after.length && before[head] === after[head]) {
6
+ head += 1;
7
+ }
8
+ let tail = 0;
9
+ while (
10
+ tail < before.length - head
11
+ && tail < after.length - head
12
+ && before[before.length - 1 - tail] === after[after.length - 1 - tail]
13
+ ) {
14
+ tail += 1;
15
+ }
16
+ return { head, tail };
17
+ }
18
+
19
+ function lcsTable(before, after) {
20
+ const width = after.length + 1;
21
+ const table = new Uint32Array((before.length + 1) * width);
22
+ for (let row = before.length - 1; row >= 0; row -= 1) {
23
+ for (let col = after.length - 1; col >= 0; col -= 1) {
24
+ const cell = row * width + col;
25
+ table[cell] = before[row] === after[col]
26
+ ? table[cell + width + 1] + 1
27
+ : Math.max(table[cell + width], table[cell + 1]);
28
+ }
29
+ }
30
+ return table;
31
+ }
32
+
33
+ function walkMiddle(before, after) {
34
+ if (before.length * after.length > MAX_CELLS) {
35
+ return [
36
+ ...before.map((text, index) => ({ kind: "removed", text, oldIndex: index })),
37
+ ...after.map((text, index) => ({ kind: "added", text, newIndex: index })),
38
+ ];
39
+ }
40
+ const table = lcsTable(before, after);
41
+ const width = after.length + 1;
42
+ const rows = [];
43
+ let row = 0;
44
+ let col = 0;
45
+ while (row < before.length || col < after.length) {
46
+ if (row < before.length && col < after.length && before[row] === after[col]) {
47
+ rows.push({ kind: "same", text: after[col], oldIndex: row, newIndex: col });
48
+ row += 1;
49
+ col += 1;
50
+ continue;
51
+ }
52
+ const lcsIfRemoved = table[(row + 1) * width + col];
53
+ const lcsIfAdded = table[row * width + col + 1];
54
+ if (row < before.length && (col === after.length || lcsIfRemoved >= lcsIfAdded)) {
55
+ rows.push({ kind: "removed", text: before[row], oldIndex: row });
56
+ row += 1;
57
+ } else {
58
+ rows.push({ kind: "added", text: after[col], newIndex: col });
59
+ col += 1;
60
+ }
61
+ }
62
+ return rows;
63
+ }
64
+
65
+ function shift(rows, offset) {
66
+ return rows.map((entry) => ({
67
+ ...entry,
68
+ oldIndex: entry.oldIndex === undefined ? undefined : entry.oldIndex + offset,
69
+ newIndex: entry.newIndex === undefined ? undefined : entry.newIndex + offset,
70
+ }));
71
+ }
72
+
73
+ export function diffLines(beforeText, afterText) {
74
+ const before = beforeText.split("\n");
75
+ const after = afterText.split("\n");
76
+ const { head, tail } = trimCommon(before, after);
77
+ const sameRows = (start, end, oldOffset) => after.slice(start, end).map((text, index) => ({
78
+ kind: "same",
79
+ text,
80
+ oldIndex: oldOffset + start + index,
81
+ newIndex: start + index,
82
+ }));
83
+ const changedBefore = before.slice(head, before.length - tail);
84
+ const changedAfter = after.slice(head, after.length - tail);
85
+ return [
86
+ ...sameRows(0, head, 0),
87
+ ...shift(walkMiddle(changedBefore, changedAfter), head),
88
+ ...sameRows(after.length - tail, after.length, before.length - after.length),
89
+ ];
90
+ }
web/src/dom.js ADDED
@@ -0,0 +1,80 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ export function el(tag, props = {}, children = []) {
2
+ const node = document.createElement(tag);
3
+ for (const [key, value] of Object.entries(props)) {
4
+ if (value === null || value === undefined || value === false) {
5
+ continue;
6
+ }
7
+ if (key === "class") {
8
+ node.className = value;
9
+ } else if (key === "text") {
10
+ node.textContent = value;
11
+ } else if (key === "style") {
12
+ node.style.cssText = value;
13
+ } else if (key.startsWith("on")) {
14
+ node.addEventListener(key.slice(2), value);
15
+ } else {
16
+ node.setAttribute(key, value === true ? "" : value);
17
+ }
18
+ }
19
+ node.append(...children);
20
+ return node;
21
+ }
22
+
23
+ export function byId(id) {
24
+ return document.getElementById(id);
25
+ }
26
+
27
+ export function prefersReducedMotion() {
28
+ return window.matchMedia("(prefers-reduced-motion: reduce)").matches;
29
+ }
30
+
31
+ const COPY_FEEDBACK_MS = 1200;
32
+
33
+ function flashIcon(button, icon, label, iconHref) {
34
+ const original = { label: button.dataset.label ?? button.getAttribute("aria-label"), href: "#i-copy" };
35
+ button.dataset.label = original.label;
36
+ button.setAttribute("aria-label", label);
37
+ icon.setAttribute("href", iconHref);
38
+ clearTimeout(Number(button.dataset.flashTimer));
39
+ const timer = setTimeout(() => {
40
+ button.setAttribute("aria-label", original.label);
41
+ icon.setAttribute("href", original.href);
42
+ }, COPY_FEEDBACK_MS);
43
+ button.dataset.flashTimer = String(timer);
44
+ }
45
+
46
+ export function copyWithIcon(button, icon, text) {
47
+ navigator.clipboard.writeText(text).then(
48
+ () => flashIcon(button, icon, "Copied", "#i-check"),
49
+ () => flashIcon(button, icon, "Copy failed", "#i-copy"),
50
+ );
51
+ }
52
+
53
+ let tweenCount = 0;
54
+
55
+ export function tweenNumber(node, from, to, durationMs) {
56
+ if (from === to && node.dataset.tweenTo === String(to)) {
57
+ return;
58
+ }
59
+ tweenCount += 1;
60
+ const id = String(tweenCount);
61
+ node.dataset.tween = id;
62
+ node.dataset.tweenTo = String(to);
63
+ if (from === to || durationMs === 0 || prefersReducedMotion()) {
64
+ node.textContent = String(to);
65
+ return;
66
+ }
67
+ const startedAt = performance.now();
68
+ const step = (now) => {
69
+ if (node.dataset.tween !== id) {
70
+ return;
71
+ }
72
+ const progress = Math.min(1, (now - startedAt) / durationMs);
73
+ const eased = 1 - (1 - progress) ** 3;
74
+ node.textContent = String(Math.round(from + (to - from) * eased));
75
+ if (progress < 1) {
76
+ requestAnimationFrame(step);
77
+ }
78
+ };
79
+ requestAnimationFrame(step);
80
+ }
web/src/fix-run.js ADDED
@@ -0,0 +1,168 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import { STATUS } from "./labels.js";
2
+ import { streamFix } from "./refiner.js";
3
+ import { THRESHOLD } from "./scoring.js";
4
+
5
+ const SEARCH_OPEN = "<<<<<<< SEARCH\n";
6
+ const DIVIDER = "=======";
7
+
8
+ function searchFocusRange(rawText, docText) {
9
+ const openAt = rawText.lastIndexOf(SEARCH_OPEN);
10
+ if (openAt < 0) {
11
+ return null;
12
+ }
13
+ const completeLines = rawText.slice(openAt + SEARCH_OPEN.length).split("\n").slice(0, -1);
14
+ if (completeLines.length === 0 || completeLines.includes(DIVIDER)) {
15
+ return null;
16
+ }
17
+ const docLines = docText.split("\n");
18
+ const lastStart = docLines.length - completeLines.length;
19
+ for (let start = 0; start <= lastStart; start += 1) {
20
+ if (completeLines.every((line, offset) => docLines[start + offset] === line)) {
21
+ return { first: start + 1, last: start + completeLines.length };
22
+ }
23
+ }
24
+ return null;
25
+ }
26
+
27
+ function squiggleCount(hygiene) {
28
+ return hygiene?.squiggles?.length ?? 0;
29
+ }
30
+
31
+ export class FixSession {
32
+ constructor({ file, sourceKind, noCache, hooks }) {
33
+ this.file = file;
34
+ this.sourceKind = sourceKind;
35
+ this.noCache = noCache;
36
+ this.hooks = hooks;
37
+ this.baseline = file.content;
38
+ this.lastAccepted = this.baseline;
39
+ this.attemptBase = this.baseline;
40
+ this.preview = this.baseline;
41
+ this.raw = "";
42
+ this.phase = "reading";
43
+ this.readPercent = null;
44
+ this.outcome = { kind: "running" };
45
+ this.controller = new AbortController();
46
+ }
47
+
48
+ get running() {
49
+ return this.outcome.kind === "running";
50
+ }
51
+
52
+ get statusText() {
53
+ if (this.phase === "waiting") {
54
+ return STATUS.waiting;
55
+ }
56
+ if (this.phase === "writing") {
57
+ return STATUS.rewriting;
58
+ }
59
+ return this.readPercent === null ? STATUS.reading : `${STATUS.reading} ${this.readPercent}%`;
60
+ }
61
+
62
+ get reading() {
63
+ return this.phase === "writing" ? searchFocusRange(this.raw, this.preview) : null;
64
+ }
65
+
66
+ async start() {
67
+ const body = {
68
+ filename: this.file.path,
69
+ content: this.baseline,
70
+ threshold: THRESHOLD,
71
+ max_passes: 2,
72
+ source_kind: this.sourceKind,
73
+ };
74
+ if (this.noCache) {
75
+ body.no_cache = true;
76
+ }
77
+ try {
78
+ await streamFix(body, {
79
+ signal: this.controller.signal,
80
+ onEvent: (name, data) => this.handle(name, data),
81
+ });
82
+ if (this.running) {
83
+ this.settle({ kind: "unreachable" });
84
+ }
85
+ } catch (error) {
86
+ if (this.running) {
87
+ this.settle({ kind: error.name === "AbortError" ? "cancelled" : "unreachable" });
88
+ }
89
+ }
90
+ }
91
+
92
+ cancel() {
93
+ if (this.running) {
94
+ this.controller.abort();
95
+ this.settle({ kind: "cancelled" });
96
+ }
97
+ }
98
+
99
+ handle(name, data) {
100
+ const handler = this.handlers[name];
101
+ if (this.running && handler) {
102
+ handler.call(this, data);
103
+ }
104
+ }
105
+
106
+ settle(outcome) {
107
+ this.outcome = outcome;
108
+ this.hooks.onFinish(this);
109
+ }
110
+ }
111
+
112
+ FixSession.prototype.handlers = {
113
+ queue() {
114
+ this.phase = "waiting";
115
+ this.hooks.onStatus(this);
116
+ },
117
+ attempt() {
118
+ this.attemptBase = this.lastAccepted;
119
+ this.preview = this.attemptBase;
120
+ this.raw = "";
121
+ this.phase = "reading";
122
+ this.readPercent = null;
123
+ this.hooks.onAttempt(this);
124
+ },
125
+ progress(data) {
126
+ if (data.phase === "read" && data.total > 0) {
127
+ this.readPercent = Math.round((data.done / data.total) * 100);
128
+ this.hooks.onStatus(this);
129
+ }
130
+ },
131
+ token(data) {
132
+ const firstToken = this.phase !== "writing";
133
+ this.raw += data.text;
134
+ this.phase = "writing";
135
+ if (firstToken) {
136
+ this.hooks.onStatus(this);
137
+ }
138
+ this.hooks.onPreview(this);
139
+ },
140
+ preview(data) {
141
+ this.preview = data.content;
142
+ this.hooks.onPreview(this);
143
+ },
144
+ verdict(data) {
145
+ if (data.accepted) {
146
+ this.lastAccepted = this.preview;
147
+ } else {
148
+ this.preview = this.lastAccepted;
149
+ }
150
+ this.hooks.onVerdict(this, data);
151
+ },
152
+ done(data) {
153
+ const changed = data.updated_content !== this.baseline;
154
+ if (!data.accepted || !changed) {
155
+ this.settle({ kind: "rejected" });
156
+ return;
157
+ }
158
+ this.settle({
159
+ kind: "kept",
160
+ content: data.updated_content,
161
+ before: squiggleCount(data.before_hygiene),
162
+ after: squiggleCount(data.after_hygiene),
163
+ });
164
+ },
165
+ error() {
166
+ this.settle({ kind: "rejected" });
167
+ },
168
+ };
web/src/github.js ADDED
@@ -0,0 +1,322 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ const CODE_EXTENSIONS = new Set([
2
+ ".py", ".pyi", ".js", ".mjs", ".cjs", ".ts", ".tsx", ".jsx", ".c", ".h", ".cc",
3
+ ".cpp", ".cxx", ".hpp", ".rs", ".go", ".zig", ".java", ".cs", ".lua", ".rb",
4
+ ".php", ".swift", ".kt", ".scala", ".sh",
5
+ ]);
6
+
7
+ const SKIP_DIRS = new Set([
8
+ ".git", ".hg", ".svn", "__pycache__", "node_modules", "target", "build", "dist",
9
+ "venv", ".venv", ".tox", ".mypy_cache", ".pytest_cache",
10
+ ]);
11
+
12
+ const SECONDARY_DIRS = new Set([
13
+ "test", "tests", "__tests__", "spec", "example", "examples", "benchmark",
14
+ "benchmarks", "fixtures",
15
+ ]);
16
+
17
+ const TEST_DIRS = new Set(["test", "tests", "__tests__", "spec"]);
18
+ const TEST_INFIXES = [".test.", ".spec.", "_test.", "_spec."];
19
+
20
+ export function isTestPath(relPath) {
21
+ const parts = relPath.toLowerCase().split("/");
22
+ const baseName = parts[parts.length - 1];
23
+ if (parts.slice(0, -1).some((dir) => TEST_DIRS.has(dir))) {
24
+ return true;
25
+ }
26
+ return /^tests?_/.test(baseName) || TEST_INFIXES.some((infix) => baseName.includes(infix));
27
+ }
28
+
29
+ const REPO_HOSTS =new Set(["github.com", "www.github.com", "gitingest.com", "www.gitingest.com"]);
30
+ const RAW_HOST = "raw.githubusercontent.com";
31
+ const MINIFIED_SUFFIXES = [".min.js", ".min.mjs", ".min.cjs", ".bundle.js"];
32
+ const SAFE_SEGMENT = /^[A-Za-z0-9_.-]+$/;
33
+ const MAX_FILE_BYTES = 131072;
34
+ const MAX_SCORED_FILES = 200;
35
+ const FETCH_CONCURRENCY = 12;
36
+
37
+ export class RepoError extends Error {
38
+ constructor(code, message) {
39
+ super(message);
40
+ this.code = code;
41
+ }
42
+ }
43
+
44
+ function describeRepo(owner, rawRepo, ref = "", subpath = "", blobPath = "") {
45
+ const repo = rawRepo.endsWith(".git") ? rawRepo.slice(0, -4) : rawRepo;
46
+ if (!SAFE_SEGMENT.test(owner) || !SAFE_SEGMENT.test(repo)) {
47
+ return null;
48
+ }
49
+ return { owner, repo, ref, subpath, blobPath, slug: `${owner}/${repo}` };
50
+ }
51
+
52
+ function stripScheme(text) {
53
+ return text.replace(/^https?:\/\//i, "");
54
+ }
55
+
56
+ function cleanPath(text) {
57
+ const cut = text.search(/[?#]/);
58
+ const sliced = cut >= 0 ? text.slice(0, cut) : text;
59
+ return sliced.replace(/^\/+|\/+$/g, "");
60
+ }
61
+
62
+ function describeFromSegments(segments) {
63
+ const [owner, repo, kind, ref, ...rest] = segments;
64
+ if (kind === "tree" && ref) {
65
+ return describeRepo(owner, repo, ref, rest.join("/"));
66
+ }
67
+ if (kind === "blob" && ref && rest.length > 0) {
68
+ return describeRepo(owner, repo, ref, "", rest.join("/"));
69
+ }
70
+ return describeRepo(owner, repo);
71
+ }
72
+
73
+ function describeRawSegments(segments) {
74
+ const [owner, repo, ref, ...rest] = segments;
75
+ if (!ref || rest.length === 0) {
76
+ return null;
77
+ }
78
+ return describeRepo(owner, repo, ref, "", rest.join("/"));
79
+ }
80
+
81
+ function describeRefSuffix(segments) {
82
+ const [owner, repoWithRef] = segments;
83
+ const atIndex = repoWithRef.indexOf("@");
84
+ if (atIndex <= 0) {
85
+ return describeRepo(owner, repoWithRef);
86
+ }
87
+ return describeRepo(owner, repoWithRef.slice(0, atIndex), repoWithRef.slice(atIndex + 1));
88
+ }
89
+
90
+ export function parseRepoSpec(rawInput) {
91
+ const trimmed = rawInput.trim();
92
+ if (!trimmed || /\s/.test(trimmed)) {
93
+ return null;
94
+ }
95
+ const withoutScheme = stripScheme(trimmed);
96
+ const firstSlash = withoutScheme.indexOf("/");
97
+ if (firstSlash <= 0) {
98
+ return null;
99
+ }
100
+ const host = withoutScheme.slice(0, firstSlash).toLowerCase();
101
+ const pathText = REPO_HOSTS.has(host) || host === RAW_HOST
102
+ ? withoutScheme.slice(firstSlash + 1)
103
+ : withoutScheme;
104
+ const segments = cleanPath(pathText).split("/").filter(Boolean).map(decodeURIComponent);
105
+ if (segments.length < 2) {
106
+ return null;
107
+ }
108
+ if (host === RAW_HOST) {
109
+ return describeRawSegments(segments);
110
+ }
111
+ if (segments.length === 2) {
112
+ return describeRefSuffix(segments);
113
+ }
114
+ return describeFromSegments(segments);
115
+ }
116
+
117
+ export function isFullRepoUrl(rawText) {
118
+ const text = stripScheme(rawText.trim()).toLowerCase();
119
+ const host = text.slice(0, text.indexOf("/"));
120
+ return (REPO_HOSTS.has(host) || host === RAW_HOST) && parseRepoSpec(rawText) !== null;
121
+ }
122
+
123
+ export function specLabel(spec) {
124
+ return spec.ref ? `${spec.slug}@${spec.ref}` : spec.slug;
125
+ }
126
+
127
+ function fileExtension(relPath) {
128
+ const baseName = relPath.slice(relPath.lastIndexOf("/") + 1);
129
+ const dotIndex = baseName.lastIndexOf(".");
130
+ return dotIndex <= 0 ? "" : baseName.slice(dotIndex).toLowerCase();
131
+ }
132
+
133
+ function hasSkippedDirectory(segments) {
134
+ return segments.slice(0, -1).some(
135
+ (dir) => dir.startsWith(".") || dir.endsWith(".egg-info") || SKIP_DIRS.has(dir),
136
+ );
137
+ }
138
+
139
+ function isScorablePath(relPath, byteSize, subpath) {
140
+ const lowerPath = relPath.toLowerCase();
141
+ if (MINIFIED_SUFFIXES.some((suffix) => lowerPath.endsWith(suffix))) {
142
+ return false;
143
+ }
144
+ if (byteSize !== null && (byteSize <= 0 || byteSize > MAX_FILE_BYTES)) {
145
+ return false;
146
+ }
147
+ if (subpath && relPath !== subpath && !relPath.startsWith(`${subpath}/`)) {
148
+ return false;
149
+ }
150
+ const segments = relPath.split("/").filter(Boolean);
151
+ if (segments.length === 0 || hasSkippedDirectory(segments)) {
152
+ return false;
153
+ }
154
+ return CODE_EXTENSIONS.has(fileExtension(relPath));
155
+ }
156
+
157
+ function pathPriority(relPath, focusPath) {
158
+ if (focusPath && relPath === focusPath) {
159
+ return -10;
160
+ }
161
+ const parts = relPath.toLowerCase().split("/");
162
+ const inSecondary = parts.slice(0, -1).some((dir) => SECONDARY_DIRS.has(dir));
163
+ return (inSecondary ? 100 : 0) + parts.length;
164
+ }
165
+
166
+ function selectEntries(entries, spec, focusPath) {
167
+ const scorable = entries.filter((entry) => isScorablePath(entry.path, entry.size, spec.subpath));
168
+ scorable.sort((left, right) => {
169
+ const rankDiff = pathPriority(left.path, focusPath) - pathPriority(right.path, focusPath);
170
+ return rankDiff !== 0 ? rankDiff : left.path.localeCompare(right.path);
171
+ });
172
+ return scorable.slice(0, MAX_SCORED_FILES);
173
+ }
174
+
175
+ export function selectLocalEntries(entries) {
176
+ return selectEntries(entries, { subpath: "" }, "");
177
+ }
178
+
179
+ async function guardedFetch(url, signal, headers = {}) {
180
+ try {
181
+ return await fetch(url, { signal, headers });
182
+ } catch (error) {
183
+ if (error.name === "AbortError") {
184
+ throw error;
185
+ }
186
+ throw new RepoError("offline", "Network request failed");
187
+ }
188
+ }
189
+
190
+ function sizeOf(value) {
191
+ return typeof value === "number" ? value : null;
192
+ }
193
+
194
+ async function fetchApiTree(spec, ref, signal) {
195
+ const treeRef = encodeURIComponent(ref);
196
+ const url = `https://api.github.com/repos/${spec.slug}/git/trees/${treeRef}?recursive=1`;
197
+ const response = await guardedFetch(url, signal);
198
+ if (response.status === 404) {
199
+ throw new RepoError("not_found", `${spec.slug} not found`);
200
+ }
201
+ if (response.status === 403 || response.status === 429) {
202
+ return { entries: null, rateLimited: true };
203
+ }
204
+ if (!response.ok) {
205
+ return { entries: null, rateLimited: false };
206
+ }
207
+ const body = await response.json();
208
+ if (body.truncated || !Array.isArray(body.tree)) {
209
+ return { entries: null, rateLimited: false };
210
+ }
211
+ const entries = body.tree
212
+ .filter((node) => node.type === "blob" && typeof node.path === "string")
213
+ .map((node) => ({ path: node.path, size: sizeOf(node.size) }));
214
+ return { entries, rateLimited: false };
215
+ }
216
+
217
+ async function fetchJsdelivrTree(spec, ref, signal) {
218
+ const url = `https://data.jsdelivr.com/v1/package/gh/${spec.slug}@${encodeURIComponent(ref)}/flat`;
219
+ const response = await guardedFetch(url, signal);
220
+ if (!response.ok) {
221
+ return { entries: null, status: response.status };
222
+ }
223
+ const payload = await response.json();
224
+ if (!Array.isArray(payload.files)) {
225
+ return { entries: null, status: 502 };
226
+ }
227
+ const entries = payload.files.map((item) => ({
228
+ path: item.name.replace(/^\//, ""),
229
+ size: sizeOf(item.size),
230
+ }));
231
+ return { entries, status: 200 };
232
+ }
233
+
234
+ async function discoverTree(spec, signal) {
235
+ const ref = spec.ref || "HEAD";
236
+ const api = await fetchApiTree(spec, ref, signal);
237
+ if (api.entries) {
238
+ return api.entries;
239
+ }
240
+ const cdn = await fetchJsdelivrTree(spec, ref, signal);
241
+ if (cdn.entries) {
242
+ return cdn.entries;
243
+ }
244
+ if (cdn.status === 404) {
245
+ throw new RepoError("not_found", `${spec.slug} not found`);
246
+ }
247
+ if (api.rateLimited) {
248
+ throw new RepoError("rate_limited", "GitHub rate limit reached");
249
+ }
250
+ throw new RepoError("offline", `Tree request failed (${cdn.status})`);
251
+ }
252
+
253
+ function encodePath(relPath) {
254
+ return relPath.split("/").map(encodeURIComponent).join("/");
255
+ }
256
+
257
+ async function fetchFileText(spec, relPath, signal) {
258
+ const ref = encodeURIComponent(spec.ref || "HEAD");
259
+ const path = encodePath(relPath);
260
+ const rawUrl = `https://raw.githubusercontent.com/${spec.slug}/${ref}/${path}`;
261
+ const primary = await guardedFetch(rawUrl, signal);
262
+ if (primary.ok) {
263
+ return primary.text();
264
+ }
265
+ const cdnUrl = `https://cdn.jsdelivr.net/gh/${spec.slug}@${ref}/${path}`;
266
+ const fallback = await guardedFetch(cdnUrl, signal);
267
+ return fallback.ok ? fallback.text() : null;
268
+ }
269
+
270
+ export function isReadableText(text) {
271
+ return Boolean(text) && text.trim().length > 0 && !text.includes("\0");
272
+ }
273
+
274
+ const COMMIT_SHA = /^[0-9a-f]{40}$/;
275
+
276
+ // The commit a branch or tag points at, so every file (and a pull request) comes from one snapshot.
277
+ async function resolveCommit(spec, signal) {
278
+ const ref = encodeURIComponent(spec.ref || "HEAD");
279
+ const url = `https://api.github.com/repos/${spec.slug}/commits/${ref}`;
280
+ const response = await guardedFetch(url, signal, { Accept: "application/vnd.github.sha" });
281
+ if (!response.ok) {
282
+ return null;
283
+ }
284
+ const sha = (await response.text()).trim();
285
+ return COMMIT_SHA.test(sha) ? sha : null;
286
+ }
287
+
288
+ export async function fetchRepoFiles(spec, { signal, focusPath, onProgress, pinCommit = false }) {
289
+ const commitSha = pinCommit ? await resolveCommit(spec, signal) : null;
290
+ const pinned = commitSha ? { ...spec, ref: commitSha } : spec;
291
+ const entries = await discoverTree(pinned, signal);
292
+ const selected = selectEntries(entries, spec, focusPath || spec.blobPath);
293
+ if (selected.length === 0) {
294
+ throw new RepoError("no_files", `No code files in ${spec.slug}`);
295
+ }
296
+
297
+ const contents = new Array(selected.length).fill(null);
298
+ let cursor = 0;
299
+ let fetched = 0;
300
+
301
+ async function drain() {
302
+ while (cursor < selected.length) {
303
+ const index = cursor;
304
+ cursor += 1;
305
+ const text = await fetchFileText(pinned, selected[index].path, signal);
306
+ contents[index] = isReadableText(text) ? text : null;
307
+ fetched += 1;
308
+ onProgress(fetched, selected.length);
309
+ }
310
+ }
311
+
312
+ const lanes = Math.min(FETCH_CONCURRENCY, selected.length);
313
+ await Promise.all(Array.from({ length: lanes }, drain));
314
+
315
+ const files = selected
316
+ .map((entry, index) => ({ path: entry.path, content: contents[index] }))
317
+ .filter((file) => file.content !== null);
318
+ if (files.length === 0) {
319
+ throw new RepoError("no_files", `No readable files in ${spec.slug}`);
320
+ }
321
+ return { files, commitSha };
322
+ }
web/src/highlight.js ADDED
@@ -0,0 +1,55 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import { parser as goParser } from "@lezer/go";
2
+ import { highlightTree, tagHighlighter, tags } from "@lezer/highlight";
3
+ import { parser as jsParser } from "@lezer/javascript";
4
+ import { parser as pythonParser } from "@lezer/python";
5
+ import { parser as rustParser } from "@lezer/rust";
6
+ import { extensionOf } from "./languages.js";
7
+
8
+ const PARSERS = {
9
+ py: () => pythonParser,
10
+ pyi: () => pythonParser,
11
+ js: () => jsParser,
12
+ mjs: () => jsParser,
13
+ cjs: () => jsParser,
14
+ jsx: () => jsParser.configure({ dialect: "jsx" }),
15
+ ts: () => jsParser.configure({ dialect: "ts" }),
16
+ tsx: () => jsParser.configure({ dialect: "ts jsx" }),
17
+ rs: () => rustParser,
18
+ go: () => goParser,
19
+ };
20
+
21
+ const tokenClasses = tagHighlighter([
22
+ { tag: tags.comment, class: "tok-com" },
23
+ { tag: [tags.string, tags.regexp, tags.character], class: "tok-str" },
24
+ { tag: [tags.keyword, tags.bool, tags.null, tags.atom, tags.self], class: "tok-kw" },
25
+ { tag: tags.number, class: "tok-num" },
26
+ ]);
27
+
28
+ export function tokenizeLines(text, filename) {
29
+ const lines = text.split("\n");
30
+ const spans = lines.map(() => []);
31
+ const factory = PARSERS[extensionOf(filename)];
32
+ if (!factory) {
33
+ return spans;
34
+ }
35
+ const lineStarts = [];
36
+ let offset = 0;
37
+ for (const line of lines) {
38
+ lineStarts.push(offset);
39
+ offset += line.length + 1;
40
+ }
41
+ let lineIndex = 0;
42
+ highlightTree(factory().parse(text), tokenClasses, (from, to, className) => {
43
+ while (lineIndex + 1 < lineStarts.length && lineStarts[lineIndex + 1] <= from) {
44
+ lineIndex += 1;
45
+ }
46
+ for (let index = lineIndex; index < lines.length && lineStarts[index] < to; index += 1) {
47
+ const start = Math.max(from, lineStarts[index]) - lineStarts[index];
48
+ const end = Math.min(to - lineStarts[index], lines[index].length);
49
+ if (end > start) {
50
+ spans[index].push({ from: start, to: end, className });
51
+ }
52
+ }
53
+ });
54
+ return spans;
55
+ }
web/src/index.html ADDED
@@ -0,0 +1,160 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <!doctype html>
2
+ <html lang="en">
3
+ <head>
4
+ <meta charset="utf-8">
5
+ <meta name="viewport" content="width=device-width, initial-scale=1">
6
+ <meta name="description" content="Paste a GitHub link to find hard-to-read code and watch it get cleaned up.">
7
+ <title>codehygiene</title>
8
+ <link rel="stylesheet" href="styles.css">
9
+ <script type="module" src="app.js"></script>
10
+ </head>
11
+ <body>
12
+ <svg class="sprite" aria-hidden="true">
13
+ <symbol id="i-github" viewBox="0 0 16 16"><path fill="currentColor" stroke="none" d="M8 0c4.42 0 8 3.58 8 8a8.013 8.013 0 0 1-5.45 7.59c-.4.08-.55-.17-.55-.38 0-.27.01-1.13.01-2.2 0-.75-.25-1.23-.54-1.48 1.78-.2 3.65-.88 3.65-3.95 0-.88-.31-1.59-.82-2.15.08-.2.36-1.02-.08-2.12 0 0-.67-.22-2.2.82-.64-.18-1.32-.27-2-.27-.68 0-1.36.09-2 .27-1.53-1.03-2.2-.82-2.2-.82-.44 1.1-.16 1.92-.08 2.12-.51.56-.82 1.28-.82 2.15 0 3.06 1.86 3.75 3.64 3.95-.23.2-.44.55-.51 1.07-.46.21-1.61.55-2.33-.66-.15-.24-.6-.83-1.23-.82-.67.01-.27.38.01.53.34.19.73.9.82 1.13.16.45.68 1.31 2.69.94 0 .67.01 1.3.01 1.49 0 .21-.15.45-.55.38A7.995 7.995 0 0 1 0 8c0-4.42 3.58-8 8-8Z"/></symbol>
14
+ <symbol id="i-copy" viewBox="0 0 24 24"><rect x="8" y="8" width="14" height="14" rx="2"/><path d="M4 16c-1.1 0-2-.9-2-2V4c0-1.1.9-2 2-2h10c1.1 0 2 .9 2 2"/></symbol>
15
+ <symbol id="i-check" viewBox="0 0 24 24"><path d="M20 6 9 17l-5-5"/></symbol>
16
+ <symbol id="i-arrow" viewBox="0 0 24 24"><path d="M5 12h14"/><path d="m13 6 6 6-6 6"/></symbol>
17
+ <symbol id="i-external" viewBox="5 5 14 14"><path d="M7 17 17 7"/><path d="M8 7h9v9"/></symbol>
18
+ <symbol id="i-chevron" viewBox="0 0 24 24"><path d="m9 6 6 6-6 6"/></symbol>
19
+ <symbol id="i-code" viewBox="0 0 24 24"><path d="m16 18 6-6-6-6"/><path d="m8 6-6 6 6 6"/></symbol>
20
+ <symbol id="i-folder" viewBox="0 0 24 24"><path fill="currentColor" stroke="none" d="M3 6.5A2.5 2.5 0 0 1 5.5 4h3.59a2 2 0 0 1 1.41.59L12 6h6.5A2.5 2.5 0 0 1 21 8.5v9a2.5 2.5 0 0 1-2.5 2.5h-13A2.5 2.5 0 0 1 3 17.5Z"/></symbol>
21
+ <symbol id="i-upload" viewBox="0 0 24 24"><path d="M20 20a2 2 0 0 0 2-2V8a2 2 0 0 0-2-2h-7.9a2 2 0 0 1-1.69-.9L9.6 3.9A2 2 0 0 0 7.93 3H4a2 2 0 0 0-2 2v13a2 2 0 0 0 2 2Z"/><path d="M12 10v6"/><path d="m9 13 3-3 3 3"/></symbol>
22
+ <symbol id="i-sidebar" viewBox="0 0 24 24"><rect x="3" y="4" width="18" height="16" rx="2"/><path d="M9 4v16"/></symbol>
23
+ <symbol id="i-search" viewBox="0 0 24 24"><circle cx="11" cy="11" r="7"/><path d="m20 20-3.5-3.5"/></symbol>
24
+ <symbol id="i-download" viewBox="0 0 24 24"><path d="M12 4v11"/><path d="m7 10 5 5 5-5"/><path d="M5 20h14"/></symbol>
25
+ <symbol id="i-pr" viewBox="0 0 24 24"><circle cx="6" cy="6" r="2.5"/><circle cx="18" cy="18" r="2.5"/><path d="M6 8.5V21"/><path d="M13 6h3a2 2 0 0 1 2 2v7.5"/><path d="m15.5 3.5-2.5 2.5 2.5 2.5"/></symbol>
26
+ <symbol id="i-trophy" viewBox="0 0 24 24"><path d="M6 9H4.5a2.5 2.5 0 0 1 0-5H6"/><path d="M18 9h1.5a2.5 2.5 0 0 0 0-5H18"/><path d="M4 22h16"/><path d="M10 14.66V17c0 .55-.47.98-.97 1.21C7.85 18.75 7 20.24 7 22"/><path d="M14 14.66V17c0 .55.47.98.97 1.21C16.15 18.75 17 20.24 17 22"/><path d="M18 2H6v7a6 6 0 0 0 12 0V2Z"/></symbol>
27
+ </svg>
28
+ <aside class="banner" id="banner" aria-label="Live stats" hidden>
29
+ <div class="banner-inner">
30
+ <a class="banner-stats" id="banner-stats" href="leaderboard/#stats" target="_blank" rel="noopener" title="Stats">
31
+ <span class="banner-stat" id="banner-fixed" hidden><b id="banner-fixed-value"></b> issues fixed</span>
32
+ <span class="banner-stat" id="banner-merged" hidden><b id="banner-merged-value"></b> of PRs merged</span>
33
+ <span class="banner-stat" id="banner-speed" hidden><b id="banner-speed-value"></b> per file</span>
34
+ </a>
35
+ <a class="banner-board" id="banner-board" href="leaderboard/" target="_blank" rel="noopener" aria-label="Leaderboard" title="Leaderboard" hidden>
36
+ <svg class="icon" aria-hidden="true"><use href="#i-trophy"/></svg>
37
+ <span class="banner-board-text">Leaderboard</span>
38
+ <svg class="icon banner-board-arrow" aria-hidden="true"><use href="#i-external"/></svg>
39
+ </a>
40
+ </div>
41
+ </aside>
42
+ <div class="page">
43
+ <main>
44
+ <section class="intro">
45
+ <h1 class="headline">Try <a class="headline-link" href="https://github.com/brianbell-x/codehygiene" target="_blank" rel="noopener" title="CodeHygiene on GitHub">CodeHygiene<svg class="icon headline-arrow" aria-hidden="true"><use href="#i-external"/></svg></a> on your code.</h1>
46
+
47
+ <div class="sources">
48
+ <form class="source" id="source">
49
+ <label class="sr-only" for="source-input">GitHub link</label>
50
+ <div class="source-field">
51
+ <svg class="icon source-icon" aria-hidden="true"><use href="#i-github"/></svg>
52
+ <input class="source-input" id="source-input" type="text" inputmode="url" placeholder="github.com/owner/repo"
53
+ autocomplete="off" autocapitalize="off" spellcheck="false" enterkeyhint="go">
54
+ <button class="icon-button is-plain source-submit" type="submit" aria-label="Start" title="Start">
55
+ <svg class="icon" aria-hidden="true"><use href="#i-arrow"/></svg>
56
+ </button>
57
+ </div>
58
+ </form>
59
+ <span class="sources-or">or</span>
60
+ <button class="ghost source-upload" id="upload" type="button">
61
+ <svg class="icon" aria-hidden="true"><use href="#i-upload"/></svg>
62
+ Upload folder
63
+ </button>
64
+ <input class="sr-only" id="upload-input" type="file" webkitdirectory multiple tabindex="-1" aria-hidden="true" aria-label="Upload folder">
65
+ </div>
66
+ <p class="source-progress" id="source-progress" role="status" hidden></p>
67
+ <p class="source-error" id="source-error" role="alert" hidden></p>
68
+ </section>
69
+
70
+ <section class="run" id="run">
71
+ <div class="sample" id="sample">
72
+ <header class="sample-head">
73
+ <p class="stage-meta">
74
+ <span class="stage-status">Cleaned</span>
75
+ <span class="stage-score"><span class="stage-score-value" id="sample-value"></span><span class="stage-score-scale">/100</span> <span class="stage-score-word" id="sample-word"></span></span>
76
+ </p>
77
+ <span class="stage-path">send_receipt.py</span>
78
+ </header>
79
+ <div class="stage-code sample-code" id="sample-code"></div>
80
+ <button class="sample-run" id="sample-run" type="button" aria-label="Run this example" title="Run this example"></button>
81
+ </div>
82
+
83
+ <article class="stage" id="stage" hidden>
84
+ <aside class="explorer" id="explorer" aria-label="Files">
85
+ <div class="tree-search">
86
+ <svg class="icon tree-search-icon" aria-hidden="true"><use href="#i-search"/></svg>
87
+ <label class="sr-only" for="tree-filter">Filter files</label>
88
+ <input class="tree-filter" id="tree-filter" type="text" placeholder="Filter files" autocomplete="off" spellcheck="false">
89
+ </div>
90
+ <nav class="tree-list" id="tree-list" aria-label="File tree"></nav>
91
+ </aside>
92
+ <div class="editor">
93
+ <header class="stage-head">
94
+ <div class="head-tools">
95
+ <button class="icon-button is-plain" id="explorer-toggle" type="button" aria-controls="explorer" aria-expanded="true" aria-label="Hide files" title="Hide files">
96
+ <svg class="icon" aria-hidden="true"><use href="#i-sidebar"/></svg>
97
+ </button>
98
+ <p class="stage-meta">
99
+ <span class="stage-status" id="stage-status"></span>
100
+ <span class="stage-score" id="stage-score"><span class="stage-score-value"></span><span class="stage-score-scale">/100</span> <span class="stage-score-word"></span></span>
101
+ </p>
102
+ </div>
103
+ <div class="nav">
104
+ <button class="icon-button is-plain nav-step" id="nav-prev" type="button" aria-label="Previous file" title="Previous file">
105
+ <svg class="icon nav-prev-icon" aria-hidden="true"><use href="#i-chevron"/></svg>
106
+ </button>
107
+ <span class="stage-path" id="stage-path"></span>
108
+ <button class="icon-button is-plain nav-step" id="nav-next" type="button" aria-label="Next file" title="Next file">
109
+ <svg class="icon" aria-hidden="true"><use href="#i-chevron"/></svg>
110
+ </button>
111
+ </div>
112
+ <nav class="tabs" id="tabs" aria-label="Files being cleaned"></nav>
113
+ <div class="head-actions">
114
+ <button class="icon-button is-plain" id="rank" type="button" aria-label="Rank this repo" title="Rank this repo" hidden>
115
+ <svg class="icon" aria-hidden="true"><use href="#i-trophy"/></svg>
116
+ </button>
117
+ <a class="rank-pill" id="rank-open" target="_blank" rel="noopener" hidden>
118
+ <svg class="icon" aria-hidden="true"><use href="#i-trophy"/></svg>
119
+ <span id="rank-text"></span>
120
+ </a>
121
+ <button class="icon-button is-plain" id="copy" type="button" aria-label="Copy file" title="Copy file" hidden>
122
+ <svg class="icon" aria-hidden="true"><use id="copy-icon" href="#i-copy"/></svg>
123
+ </button>
124
+ <button class="icon-button is-plain" id="patch" type="button" aria-label="Download patch" title="Download patch" hidden>
125
+ <svg class="icon" aria-hidden="true"><use href="#i-download"/></svg>
126
+ </button>
127
+ <button class="icon-button is-plain" id="pr" type="button" aria-label="Create PR" title="Create PR" hidden>
128
+ <svg class="icon" aria-hidden="true"><use href="#i-pr"/></svg>
129
+ </button>
130
+ <a class="icon-button is-plain is-done" id="pr-open" target="_blank" rel="noopener" aria-label="Open PR" title="Open PR" hidden>
131
+ <svg class="icon" aria-hidden="true"><use href="#i-pr"/></svg>
132
+ </a>
133
+ </div>
134
+ </header>
135
+ <div class="stage-code" id="stage-code" role="region" tabindex="0" aria-label="File contents"></div>
136
+ </div>
137
+ <footer class="tally" id="tally">
138
+ <p class="tally-line">
139
+ <span class="tally-label" id="tally-label">Issues fixed</span>
140
+ <span class="tally-count"><span id="tally-fixed">0</span> / <span id="tally-total">0</span></span>
141
+ </p>
142
+ <div class="tally-bar" aria-hidden="true"><span class="tally-fill" id="tally-fill"></span></div>
143
+ </footer>
144
+ </article>
145
+
146
+ </section>
147
+ </main>
148
+
149
+ <footer class="foot">
150
+ <div class="command">
151
+ <code class="command-text" id="command-text">npx skills add brianbell-x/codehygiene -g</code>
152
+ <button class="icon-button is-plain" id="command-copy" type="button" aria-label="Copy install command" title="Copy">
153
+ <svg class="icon" aria-hidden="true"><use id="command-copy-icon" href="#i-copy"/></svg>
154
+ </button>
155
+ </div>
156
+ <p><span id="foot-mode" hidden>Demo mode: fixes are pre-written. </span>codehygiene can make mistakes. Check fixes before merging.</p>
157
+ </footer>
158
+ </div>
159
+ </body>
160
+ </html>
web/src/labels.js ADDED
@@ -0,0 +1,113 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ export const NOTES = {
2
+ deep_nesting: "deeply nested",
3
+ poor_naming: "unclear name or magic number",
4
+ noisy_comment: "redundant comment",
5
+ swallowed_error: "error silently ignored",
6
+ dense_line: "overlong line",
7
+ special_casing: "stacked special cases",
8
+ duplicate_block: "duplicated block",
9
+ cramped_layout: "dense block",
10
+ };
11
+
12
+ export const SOURCE_ERRORS = {
13
+ invalid: "Not a GitHub link",
14
+ not_found: "Repo not found",
15
+ rate_limited: "GitHub rate limit, try later",
16
+ no_files: "No code files found",
17
+ offline: "Offline",
18
+ };
19
+
20
+ export const PR_ERRORS = {
21
+ popup_blocked: "Pop-up blocked",
22
+ closed: "Sign-in closed",
23
+ login_required: "Sign-in expired",
24
+ login_failed: "Sign-in failed",
25
+ no_push: "No write access",
26
+ not_installed: "App not installed",
27
+ not_found: "Repo not found",
28
+ bad_base: "Base not found",
29
+ bad_path: "File not in base",
30
+ not_cleaner: "Nothing to commit",
31
+ busy: "Busy, try later",
32
+ failed: "PR failed",
33
+ };
34
+
35
+ export const RANK_ERRORS = {
36
+ too_small: "Too small to rank",
37
+ too_large: "Too large to rank",
38
+ fork: "Forks aren't ranked",
39
+ not_found: "Repo not found",
40
+ busy: "Busy, try later",
41
+ failed: "Rank failed",
42
+ };
43
+
44
+ export const LANGUAGES = {
45
+ py: "Python",
46
+ pyi: "Python",
47
+ ts: "TypeScript",
48
+ tsx: "TypeScript",
49
+ js: "JavaScript",
50
+ jsx: "JavaScript",
51
+ mjs: "JavaScript",
52
+ cjs: "JavaScript",
53
+ go: "Go",
54
+ rs: "Rust",
55
+ c: "C",
56
+ h: "C",
57
+ cc: "C++",
58
+ cpp: "C++",
59
+ cxx: "C++",
60
+ hpp: "C++",
61
+ zig: "Zig",
62
+ java: "Java",
63
+ cs: "C#",
64
+ lua: "Lua",
65
+ rb: "Ruby",
66
+ php: "PHP",
67
+ swift: "Swift",
68
+ kt: "Kotlin",
69
+ scala: "Scala",
70
+ sh: "Shell",
71
+ };
72
+
73
+ export function sentenceCase(text) {
74
+ return text.charAt(0).toUpperCase() + text.slice(1);
75
+ }
76
+
77
+ export const STATUS = {
78
+ waiting: "Waiting",
79
+ queued: "Queued",
80
+ reading: "Reading",
81
+ rewriting: "Rewriting",
82
+ cleaned: "Cleaned",
83
+ skipped: "Skipped",
84
+ offline: "Offline",
85
+ tooLong: "Too long",
86
+ noIssues: "No issues found",
87
+ };
88
+
89
+ export function plural(count, noun) {
90
+ return count === 1 ? `1 ${noun}` : `${count} ${noun}s`;
91
+ }
92
+
93
+ export function issuesText(count) {
94
+ return count === 0 ? "No issues" : plural(count, "issue");
95
+ }
96
+
97
+ export function fixedText(before, after) {
98
+ return after === 0 ? STATUS.cleaned : `${before - after} of ${before} fixed`;
99
+ }
100
+
101
+ export function toHundred(score) {
102
+ return Math.round(score * 100);
103
+ }
104
+
105
+ export function scoreWord(points) {
106
+ if (points < 50) {
107
+ return { word: "poor", className: "is-sloppy" };
108
+ }
109
+ if (points < 75) {
110
+ return { word: "fair", className: "" };
111
+ }
112
+ return { word: "good", className: "is-clean" };
113
+ }
web/src/languages.js ADDED
@@ -0,0 +1,43 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ const SNIFF_RULES = [
2
+ {
3
+ extension: "go",
4
+ linePrefixes: ["package ", "func "],
5
+ fragments: [" := "],
6
+ },
7
+ {
8
+ extension: "rs",
9
+ linePrefixes: ["fn ", "pub fn ", "let mut ", "impl ", "use std::"],
10
+ fragments: ["-> Option<", "-> Result<"],
11
+ },
12
+ {
13
+ extension: "ts",
14
+ linePrefixes: ["interface ", "export interface ", "type ", "export type "],
15
+ fragments: [": string", ": number", ": boolean"],
16
+ },
17
+ {
18
+ extension: "js",
19
+ linePrefixes: ["const ", "let ", "function ", "export function ", "export default "],
20
+ fragments: [" => "],
21
+ },
22
+ {
23
+ extension: "py",
24
+ linePrefixes: ["def ", "class ", "import ", "from ", "elif "],
25
+ fragments: ["self."],
26
+ },
27
+ ];
28
+
29
+ function matchesRule(rule, trimmedLines, text) {
30
+ const startsRule = (line) => rule.linePrefixes.some((prefix) => line.startsWith(prefix));
31
+ return trimmedLines.some(startsRule) || rule.fragments.some((fragment) => text.includes(fragment));
32
+ }
33
+
34
+ export function extensionOf(filename) {
35
+ const dotIndex = filename.lastIndexOf(".");
36
+ return dotIndex <= 0 ? "" : filename.slice(dotIndex + 1).toLowerCase();
37
+ }
38
+
39
+ export function sniffExtension(text) {
40
+ const trimmedLines = text.split("\n").map((line) => line.trimStart());
41
+ const rule = SNIFF_RULES.find((candidate) => matchesRule(candidate, trimmedLines, text));
42
+ return rule ? rule.extension : "py";
43
+ }
web/src/main.js ADDED
@@ -0,0 +1,687 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import { byId, copyWithIcon, prefersReducedMotion, tweenNumber } from "./dom.js";
2
+ import { FixSession } from "./fix-run.js";
3
+ import { isFullRepoUrl, parseRepoSpec, specLabel } from "./github.js";
4
+ import { PR_ERRORS, SOURCE_ERRORS, STATUS, fixedText, issuesText, toHundred } from "./labels.js";
5
+ import { buildPatch, downloadText } from "./patch.js";
6
+ import { openPullRequest } from "./pull-request.js";
7
+ import { DEFAULT_MAX_FILE_CHARS, fetchHealth, fetchStats } from "./refiner.js";
8
+ import { createScorer } from "./scoring.js";
9
+ import {
10
+ RUN_SIZE,
11
+ eligibleFiles,
12
+ isSloppy,
13
+ loadExamples,
14
+ loadFolder,
15
+ loadRepo,
16
+ loadSnippet,
17
+ } from "./sources.js";
18
+ import { createStage } from "./stage.js";
19
+ import { createNavigator } from "./navigator.js";
20
+ import { createTabs } from "./tabs.js";
21
+ import { paintSample } from "./sample.js";
22
+ import { boardLink, rankErrorText, rankLabel, requestRank } from "./ranking.js";
23
+ import { hideBanner, renderBanner, reportScan } from "./telemetry.js";
24
+
25
+ const TIMING = {
26
+ firstStart: 800,
27
+ beforeFix: 1400,
28
+ afterFix: 2200,
29
+ retry: 10000,
30
+ tallyTween: 600,
31
+ };
32
+
33
+ const nodes = {
34
+ sample: byId("sample"),
35
+ stage: byId("stage"),
36
+ input: byId("source-input"),
37
+ error: byId("source-error"),
38
+ progress: byId("source-progress"),
39
+
40
+ tally: byId("tally"),
41
+ tallyLabel: byId("tally-label"),
42
+ tallyFixed: byId("tally-fixed"),
43
+ tallyTotal: byId("tally-total"),
44
+ tallyFill: byId("tally-fill"),
45
+
46
+ copy: byId("copy"),
47
+ copyIcon: byId("copy-icon"),
48
+ patch: byId("patch"),
49
+ pr: byId("pr"),
50
+ prOpen: byId("pr-open"),
51
+
52
+ rank: byId("rank"),
53
+ rankOpen: byId("rank-open"),
54
+ rankText: byId("rank-text"),
55
+ };
56
+
57
+ const PATCH_SOURCES = new Set(["repo", "folder"]);
58
+
59
+ const narrowScreen = window.matchMedia("(max-width: 640px)");
60
+ const scorer = createScorer();
61
+ const stage = createStage();
62
+ const fileNav = createNavigator(selectFile);
63
+ const fileTabs = createTabs(selectFile);
64
+
65
+ const app = {
66
+ repoSpec: null,
67
+ sourceKind: "examples",
68
+ files: [],
69
+ queue: [],
70
+ cursor: -1,
71
+ shown: null,
72
+ following: true,
73
+ idle: true,
74
+ activeFile: null,
75
+ picked: new Set(),
76
+ session: null,
77
+ timers: new Set(),
78
+ initialIssues: 0,
79
+ shownFixed: null,
80
+ ingest: null,
81
+ maxChars: DEFAULT_MAX_FILE_CHARS,
82
+ healthReady: Promise.resolve(),
83
+ prEnabled: false,
84
+ commitSha: null,
85
+ prUrl: null,
86
+ standing: null,
87
+ renderQueued: false,
88
+ followChange: false,
89
+ ingestController: null,
90
+ };
91
+
92
+ function later(ms, action) {
93
+ const timer = setTimeout(() => {
94
+ app.timers.delete(timer);
95
+ action();
96
+ }, ms);
97
+ app.timers.add(timer);
98
+ }
99
+
100
+ function clearTimers() {
101
+ app.timers.forEach(clearTimeout);
102
+ app.timers.clear();
103
+ }
104
+
105
+ function issueCount(files) {
106
+ return files.reduce((sum, file) => sum + file.result.squiggles.length, 0);
107
+ }
108
+
109
+ function renderTally() {
110
+ nodes.progress.hidden = !app.ingest;
111
+ nodes.progress.textContent = app.ingest ?? "";
112
+ const total = app.initialIssues;
113
+ const fixed = Math.max(0, total - issueCount(app.files));
114
+ nodes.tally.classList.toggle("is-empty", total === 0);
115
+ nodes.tallyLabel.textContent = total === 0 ? STATUS.noIssues : "Issues fixed";
116
+ nodes.tallyTotal.textContent = String(total);
117
+ tweenNumber(nodes.tallyFixed, app.shownFixed ?? fixed, fixed, TIMING.tallyTween);
118
+ app.shownFixed = fixed;
119
+ nodes.tallyFill.style.width = `${total === 0 ? 0 : (fixed / total) * 100}%`;
120
+ }
121
+
122
+ function tabFiles(current) {
123
+ const working = new Set([...app.queue, ...app.picked]);
124
+ if (current) {
125
+ working.add(current);
126
+ }
127
+ return [...working];
128
+ }
129
+
130
+ function paintNav(current, { fresh = false } = {}) {
131
+ if (fresh) {
132
+ fileNav.render(app.files, current);
133
+ } else {
134
+ fileNav.update(app.files, current);
135
+ }
136
+ fileTabs.render(tabFiles(current), current, app.session?.file ?? null);
137
+ }
138
+ function fileStatus(file) {
139
+ if (app.session?.file === file) {
140
+ return app.session.statusText;
141
+ }
142
+ if (app.picked.has(file) && !file.attempted && app.activeFile !== file) {
143
+ return STATUS.queued;
144
+ }
145
+ if (file.kept) {
146
+ return fixedText(file.originalResult.squiggles.length, file.result.squiggles.length);
147
+ }
148
+ if (file.lastRejected && file.attempted) {
149
+ return STATUS.skipped;
150
+ }
151
+ if (isSloppy(file) && !file.kept && file.content.length > app.maxChars) {
152
+ return STATUS.tooLong;
153
+ }
154
+ return issuesText(file.result.squiggles.length);
155
+ }
156
+
157
+ function keptFiles() {
158
+ return app.files.filter((file) => file.kept && file.content !== file.original);
159
+ }
160
+
161
+ function renderActions(file) {
162
+ nodes.copy.hidden = !file?.kept || app.session?.file === file;
163
+ const anyKept = keptFiles().length > 0;
164
+ const canPr = anyKept && app.prEnabled && app.sourceKind === "repo" && Boolean(app.commitSha);
165
+ nodes.patch.hidden = !anyKept || !PATCH_SOURCES.has(app.sourceKind);
166
+ nodes.pr.hidden = !canPr || Boolean(app.prUrl);
167
+ nodes.prOpen.hidden = !app.prUrl;
168
+ nodes.prOpen.href = app.prUrl ?? "";
169
+ const rankable = app.sourceKind === "repo" && Boolean(app.repoSpec);
170
+ nodes.rank.hidden = !rankable || Boolean(app.standing);
171
+ nodes.rankOpen.hidden = !rankable || !app.standing;
172
+ }
173
+
174
+ function paintStanding(standing) {
175
+ nodes.rankText.textContent = rankLabel(standing);
176
+ nodes.rankOpen.href = boardLink(standing.repo);
177
+ const points = Math.round(standing.score);
178
+ nodes.rankOpen.title = `${standing.repo}: ${points}/100, #${standing.rank} of ${standing.total}`;
179
+ }
180
+
181
+ async function rankRepo() {
182
+ const spec = app.repoSpec;
183
+ showSourceError("");
184
+ setBusy(nodes.rank, true);
185
+ try {
186
+ const standing = await requestRank(spec);
187
+ if (app.repoSpec === spec) {
188
+ app.standing = standing;
189
+ paintStanding(standing);
190
+ }
191
+ } catch (error) {
192
+ showSourceError(rankErrorText(error));
193
+ } finally {
194
+ setBusy(nodes.rank, false);
195
+ renderActions(app.shown);
196
+ }
197
+ }
198
+
199
+ function patchName() {
200
+ return app.repoSpec ? `codehygiene-${app.repoSpec.repo}.patch` : "codehygiene.patch";
201
+ }
202
+
203
+ function downloadPatch() {
204
+ downloadText(buildPatch(keptFiles()), patchName());
205
+ }
206
+
207
+ function setBusy(button, busy) {
208
+ button.disabled = busy;
209
+ button.classList.toggle("is-busy", busy);
210
+ button.setAttribute("aria-busy", String(busy));
211
+ }
212
+
213
+ async function createPr() {
214
+ showSourceError("");
215
+ setBusy(nodes.pr, true);
216
+ try {
217
+ const opened = await openPullRequest({
218
+ spec: app.repoSpec,
219
+ commitSha: app.commitSha,
220
+ files: keptFiles(),
221
+ });
222
+ app.prUrl = opened.url;
223
+ } catch (error) {
224
+ showSourceError(PR_ERRORS[error.code] ?? PR_ERRORS.failed);
225
+ } finally {
226
+ setBusy(nodes.pr, false);
227
+ renderActions(app.shown);
228
+ }
229
+ }
230
+
231
+ function renderRestingDiff(file, text, followChange) {
232
+ stage.renderDiff({
233
+ base: file.original,
234
+ next: text,
235
+ filename: file.path,
236
+ followChange,
237
+ squiggles: file.result.squiggles,
238
+ squigglesOn: "next",
239
+ });
240
+ }
241
+
242
+ function renderShown({ enter = false, animateScore = false } = {}) {
243
+ const file = app.shown;
244
+ stage.setHead({
245
+ filename: file.path,
246
+ statusText: fileStatus(file),
247
+ points: toHundred(file.result.score),
248
+ animate: animateScore,
249
+ });
250
+ if (file.kept) {
251
+ renderRestingDiff(file, file.content, enter);
252
+ } else {
253
+ stage.renderPlain({ text: file.content, filename: file.path, squiggles: file.result.squiggles, enter });
254
+ }
255
+ renderActions(file);
256
+ }
257
+
258
+ function showFile(file, { enter = true } = {}) {
259
+ app.shown = file;
260
+ stage.resetScore();
261
+ paintNav(file);
262
+ if (app.session?.file === file) {
263
+ renderSessionDiff(app.session, true);
264
+ renderActions(file);
265
+ return;
266
+ }
267
+ renderShown({ enter });
268
+ }
269
+
270
+ function renderSessionDiff(session, followChange) {
271
+ const scoredBase = session.attemptBase === (session.file.scoredText ?? session.file.content);
272
+ stage.setHead({
273
+ filename: session.file.path,
274
+ statusText: session.statusText,
275
+ points: toHundred(session.file.result.score),
276
+ });
277
+ stage.renderDiff({
278
+ base: session.attemptBase,
279
+ next: session.preview,
280
+ filename: session.file.path,
281
+ reading: session.reading,
282
+ followChange,
283
+ squiggles: scoredBase ? session.file.result.squiggles : [],
284
+ });
285
+ }
286
+
287
+ function queueSessionRender(session, followChange) {
288
+ app.followChange ||= followChange;
289
+ if (app.renderQueued || app.shown !== session.file) {
290
+ return;
291
+ }
292
+ app.renderQueued = true;
293
+ requestAnimationFrame(() => {
294
+ const follow = app.followChange;
295
+ Object.assign(app, { renderQueued: false, followChange: false });
296
+ if (app.session === session && app.shown === session.file) {
297
+ renderSessionDiff(session, follow);
298
+ }
299
+ });
300
+ }
301
+
302
+ function renderFileText(file, text) {
303
+ stage.renderPlain({ text, filename: file.path, squiggles: file.result.squiggles, enter: false });
304
+ }
305
+
306
+ function sessionMovedOn(file, acceptedText) {
307
+ return app.session?.file === file && app.session.preview !== acceptedText;
308
+ }
309
+
310
+ async function handleVerdict(session, verdict) {
311
+ const file = session.file;
312
+ if (!verdict.accepted) {
313
+ if (app.shown === file) {
314
+ stage.setStatus(STATUS.skipped);
315
+ renderFileText(file, session.preview);
316
+ }
317
+ return;
318
+ }
319
+ const acceptedText = session.preview;
320
+ if (app.shown === file) {
321
+ stage.setStatus(fixedText(file.originalResult.squiggles.length, verdict.squiggles_after));
322
+ }
323
+ const result = await scorer.scoreOne(acceptedText);
324
+ if (session.outcome.kind === "cancelled") {
325
+ return;
326
+ }
327
+ file.result = result;
328
+ file.scoredText = acceptedText;
329
+ paintNav(app.shown);
330
+ renderTally();
331
+ if (app.shown !== file || sessionMovedOn(file, acceptedText)) {
332
+ return;
333
+ }
334
+ file.restingText = acceptedText;
335
+ stage.setScore(toHundred(file.result.score), true);
336
+ renderRestingDiff(file, acceptedText, true);
337
+ }
338
+
339
+ async function restoreScore(file) {
340
+ file.result = await scorer.scoreOne(file.content);
341
+ file.scoredText = file.content;
342
+ paintNav(app.shown);
343
+ renderTally();
344
+ }
345
+
346
+ async function finishSession(session) {
347
+ const file = session.file;
348
+ const { outcome } = session;
349
+ if (app.session === session) {
350
+ app.session = null;
351
+ }
352
+ if (outcome.kind === "cancelled") {
353
+ if (file.scoredText !== file.content) {
354
+ await restoreScore(file);
355
+ }
356
+ return;
357
+ }
358
+ if (outcome.kind === "unreachable") {
359
+ if (app.shown === file) {
360
+ renderShown();
361
+ stage.setStatus(STATUS.offline);
362
+ }
363
+ later(TIMING.retry, () => fixFile(file));
364
+ return;
365
+ }
366
+ app.activeFile = null;
367
+ file.attempted = true;
368
+ file.lastRejected = outcome.kind === "rejected";
369
+ if (outcome.kind === "kept") {
370
+ file.content = outcome.content;
371
+ file.result = await scorer.scoreOne(outcome.content);
372
+ file.scoredText = outcome.content;
373
+ file.kept = true;
374
+ file.cleaned = file.result.squiggles.length === 0;
375
+ }
376
+ paintNav(app.shown);
377
+ renderTally();
378
+ renderActions(app.shown);
379
+ const resting = file.kept && file.restingText === file.content;
380
+ file.restingText = null;
381
+ if (app.shown === file && resting) {
382
+ stage.setStatus(fileStatus(file));
383
+ renderActions(file);
384
+ } else if (app.shown === file) {
385
+ renderShown();
386
+ }
387
+ later(TIMING.afterFix, advance);
388
+ }
389
+
390
+ function fixFile(file) {
391
+ if (app.following && app.shown !== file) {
392
+ showFile(file, { enter: false });
393
+ }
394
+ const session = new FixSession({
395
+ file,
396
+ sourceKind: app.sourceKind,
397
+ noCache: file.lastRejected,
398
+ hooks: {
399
+ onStatus: (current) => app.shown === file && stage.setStatus(current.statusText),
400
+ onAttempt: (current) => queueSessionRender(current, true),
401
+ onPreview: (current) => queueSessionRender(current, false),
402
+ onVerdict: handleVerdict,
403
+ onFinish: finishSession,
404
+ },
405
+ });
406
+ app.session = session;
407
+ paintNav(app.shown);
408
+ renderActions(app.shown);
409
+ session.start();
410
+ }
411
+
412
+ function openAndFix(index, { reshow = true } = {}) {
413
+ app.cursor = index;
414
+ const file = app.queue[index];
415
+ app.activeFile = file;
416
+ if (app.following && (reshow || app.shown !== file)) {
417
+ showFile(file);
418
+ }
419
+ later(TIMING.beforeFix, () => fixFile(file));
420
+ }
421
+
422
+ function advance() {
423
+ if (app.cursor + 1 < app.queue.length) {
424
+ openAndFix(app.cursor + 1);
425
+ return;
426
+ }
427
+ app.idle = true;
428
+ }
429
+
430
+ function stopEverything() {
431
+ clearTimers();
432
+ app.session?.cancel();
433
+ app.session = null;
434
+ app.activeFile = null;
435
+ }
436
+
437
+ function beginRun(queue, startDelay) {
438
+ stopEverything();
439
+ Object.assign(app, { queue, cursor: -1, following: true, idle: queue.length === 0, picked: new Set() });
440
+ renderTally();
441
+ paintNav(queue[0] ?? app.files[0], { fresh: true });
442
+ showFile(queue[0] ?? app.files[0]);
443
+ if (queue.length > 0) {
444
+ later(startDelay, () => openAndFix(0, { reshow: false }));
445
+ }
446
+ }
447
+
448
+ function bringStageIntoView() {
449
+ if (narrowScreen.matches) {
450
+ nodes.stage.scrollIntoView({ behavior: prefersReducedMotion() ? "auto" : "smooth", block: "start" });
451
+ }
452
+ }
453
+
454
+ function revealStage(update) {
455
+ if (!nodes.stage.hidden) {
456
+ update();
457
+ return;
458
+ }
459
+ const swap = () => {
460
+ nodes.sample.hidden = true;
461
+ nodes.stage.hidden = false;
462
+ update();
463
+ };
464
+ if (!document.startViewTransition || prefersReducedMotion()) {
465
+ swap();
466
+ bringStageIntoView();
467
+ return;
468
+ }
469
+ document.startViewTransition(swap).finished.then(bringStageIntoView);
470
+ }
471
+
472
+ function startFiles({ files, sourceKind, repoSpec = null, commitSha = null, queue, startDelay }) {
473
+ Object.assign(app, { files, sourceKind, repoSpec, commitSha });
474
+ Object.assign(app, { prUrl: null, standing: null, ingest: null, shownFixed: null });
475
+ app.initialIssues = issueCount(files);
476
+ syncUrl();
477
+ reportScan(sourceKind, files);
478
+ revealStage(() => beginRun(queue, startDelay));
479
+ }
480
+
481
+ async function startExamples() {
482
+ showSourceError("");
483
+ stopEverything();
484
+ const files = await loadExamples(scorer.scoreAll);
485
+ startFiles({
486
+ files,
487
+ sourceKind: "examples",
488
+ queue: files.filter(isSloppy),
489
+ startDelay: TIMING.firstStart,
490
+ });
491
+ }
492
+
493
+ function repoQueue(files) {
494
+ return eligibleFiles(files, app.maxChars).slice(0, RUN_SIZE);
495
+ }
496
+
497
+ function syncUrl() {
498
+ const url = new URL(window.location.href);
499
+ url.searchParams.delete("repo");
500
+ if (app.repoSpec) {
501
+ url.searchParams.set("repo", specLabel(app.repoSpec));
502
+ }
503
+ url.search = url.searchParams.toString().replaceAll("%2F", "/").replaceAll("%40", "@");
504
+ window.history.replaceState(null, "", url);
505
+ }
506
+
507
+ function showSourceError(message) {
508
+ nodes.error.hidden = !message;
509
+ nodes.error.textContent = message;
510
+ }
511
+
512
+ async function startRepo(spec) {
513
+ showSourceError("");
514
+ stopEverything();
515
+ app.ingestController?.abort();
516
+ const controller = new AbortController();
517
+ app.ingest = "Fetching";
518
+ app.ingestController = controller;
519
+ renderTally();
520
+ try {
521
+ await app.healthReady;
522
+ const { files, commitSha } = await loadRepo(spec, {
523
+ signal: controller.signal,
524
+ scoreAll: scorer.scoreAll,
525
+ pinCommit: app.prEnabled,
526
+ onProgress: (text) => {
527
+ app.ingest = text;
528
+ renderTally();
529
+ },
530
+ });
531
+ startFiles({
532
+ files,
533
+ sourceKind: "repo",
534
+ repoSpec: spec,
535
+ commitSha,
536
+ queue: repoQueue(files),
537
+ startDelay: 0,
538
+ });
539
+ } catch (error) {
540
+ if (error.name === "AbortError") {
541
+ return;
542
+ }
543
+ app.ingest = null;
544
+ renderTally();
545
+ showSourceError(SOURCE_ERRORS[error.code] ?? SOURCE_ERRORS.offline);
546
+ }
547
+ }
548
+
549
+ async function startFolder(fileList) {
550
+ showSourceError("");
551
+ stopEverything();
552
+ app.ingestController?.abort();
553
+ app.ingest = "Reading";
554
+ renderTally();
555
+ try {
556
+ const files = await loadFolder(fileList, {
557
+ scoreAll: scorer.scoreAll,
558
+ onProgress: (text) => {
559
+ app.ingest = text;
560
+ renderTally();
561
+ },
562
+ });
563
+ nodes.input.value = "";
564
+ startFiles({ files, sourceKind: "folder", queue: repoQueue(files), startDelay: 0 });
565
+ } catch (error) {
566
+ app.ingest = null;
567
+ renderTally();
568
+ showSourceError(SOURCE_ERRORS[error.code] ?? SOURCE_ERRORS.no_files);
569
+ }
570
+ }
571
+
572
+ async function startSnippet(text) {
573
+ showSourceError("");
574
+ const files = await loadSnippet(text, scorer.scoreAll);
575
+ const queue = files.filter((file) => isSloppy(file) && file.content.length <= app.maxChars);
576
+ startFiles({ files, sourceKind: "snippet", queue, startDelay: 0 });
577
+ }
578
+
579
+ function submitLink(text) {
580
+ const spec = parseRepoSpec(text);
581
+ if (!spec) {
582
+ showSourceError(SOURCE_ERRORS.invalid);
583
+ return;
584
+ }
585
+ nodes.input.value = spec.slug;
586
+ startRepo(spec);
587
+ }
588
+
589
+ function canClean(file) {
590
+ const position = app.queue.indexOf(file);
591
+ const started = position !== -1 && position <= app.cursor;
592
+ return isSloppy(file) && !file.attempted && !started && file.content.length <= app.maxChars;
593
+ }
594
+
595
+ function cleanNext(file) {
596
+ if (!canClean(file)) {
597
+ return;
598
+ }
599
+ app.picked.add(file);
600
+ app.queue = app.queue.filter((queued) => queued !== file);
601
+ const autoplayBusy = app.activeFile && !app.picked.has(app.activeFile);
602
+ if (autoplayBusy) {
603
+ const cursor = app.cursor;
604
+ stopEverything();
605
+ app.queue.splice(cursor, 0, file);
606
+ openAndFix(cursor);
607
+ return;
608
+ }
609
+ app.queue.splice(app.cursor + 1, 0, file);
610
+ stage.setStatus(fileStatus(file));
611
+ if (app.idle) {
612
+ app.idle = false;
613
+ advance();
614
+ }
615
+ }
616
+
617
+ function selectFile(file) {
618
+ app.following = false;
619
+ showFile(file);
620
+ cleanNext(file);
621
+ }
622
+
623
+ function handlePaste(event) {
624
+ const text = event.clipboardData?.getData("text/plain") ?? "";
625
+ const multiLine = text.trim().includes("\n");
626
+ if (multiLine) {
627
+ event.preventDefault();
628
+ startSnippet(text);
629
+ return;
630
+ }
631
+ if (isFullRepoUrl(text)) {
632
+ event.preventDefault();
633
+ nodes.input.value = text.trim();
634
+ submitLink(text);
635
+ }
636
+ }
637
+
638
+ function applyHealth(health) {
639
+ app.maxChars = health.max_file_chars || DEFAULT_MAX_FILE_CHARS;
640
+ app.prEnabled = health.github_pr_enabled === true;
641
+ byId("foot-mode").hidden = health.backend !== "mock";
642
+ }
643
+
644
+ function keepDefaultLimits() {
645
+ app.maxChars = DEFAULT_MAX_FILE_CHARS;
646
+ }
647
+
648
+ function bindControls() {
649
+ byId("source").addEventListener("submit", (event) => {
650
+ event.preventDefault();
651
+ submitLink(nodes.input.value);
652
+ });
653
+ nodes.copy.addEventListener("click", () => copyWithIcon(nodes.copy, nodes.copyIcon, app.shown.content));
654
+ nodes.patch.addEventListener("click", downloadPatch);
655
+ nodes.pr.addEventListener("click", createPr);
656
+ nodes.rank.addEventListener("click", rankRepo);
657
+ byId("command-copy").addEventListener("click", () => copyWithIcon(
658
+ byId("command-copy"),
659
+ byId("command-copy-icon"),
660
+ byId("command-text").textContent,
661
+ ));
662
+ byId("sample-run").addEventListener("click", startExamples);
663
+ const uploadInput = byId("upload-input");
664
+ byId("upload").addEventListener("click", () => uploadInput.click());
665
+ uploadInput.addEventListener("change", () => {
666
+ if (uploadInput.files.length > 0) {
667
+ startFolder([...uploadInput.files]);
668
+ }
669
+ uploadInput.value = "";
670
+ });
671
+ document.addEventListener("paste", handlePaste);
672
+ }
673
+
674
+ function boot() {
675
+ bindControls();
676
+ app.healthReady = fetchHealth().then(applyHealth, keepDefaultLimits);
677
+ fetchStats().then(renderBanner, hideBanner);
678
+ paintSample(scorer.scoreAll);
679
+ const repoParam = new URLSearchParams(window.location.search).get("repo");
680
+ const spec = repoParam ? parseRepoSpec(repoParam) : null;
681
+ if (spec) {
682
+ nodes.input.value = spec.slug;
683
+ startRepo(spec);
684
+ }
685
+ }
686
+
687
+ boot();
web/src/navigator.js ADDED
@@ -0,0 +1,261 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import { byId, el } from "./dom.js";
2
+ import { plural } from "./labels.js";
3
+
4
+ const ROW_INDENT = 12;
5
+ const NARROW = "(max-width: 640px)";
6
+
7
+ function insertPath(root, file) {
8
+ const parts = file.path.split("/");
9
+ let folder = root;
10
+ parts.slice(0, -1).forEach((name, depth) => {
11
+ const path = parts.slice(0, depth + 1).join("/");
12
+ if (!folder.folders.has(name)) {
13
+ folder.folders.set(name, { name, path, folders: new Map(), files: [] });
14
+ }
15
+ folder = folder.folders.get(name);
16
+ });
17
+ folder.files.push(file);
18
+ }
19
+
20
+ function buildTree(files) {
21
+ const root = { name: "", path: "", folders: new Map(), files: [] };
22
+ files.forEach((file) => insertPath(root, file));
23
+ return root;
24
+ }
25
+
26
+ const byName = (left, right) => left.name.localeCompare(right.name);
27
+ const byPath = (left, right) => left.path.localeCompare(right.path);
28
+
29
+ function sortedFolders(folder) {
30
+ return [...folder.folders.values()].sort(byName);
31
+ }
32
+
33
+ function sortedFiles(folder) {
34
+ return [...folder.files].sort(byPath);
35
+ }
36
+
37
+ function flatten(folder) {
38
+ return [...sortedFolders(folder).flatMap(flatten), ...sortedFiles(folder)];
39
+ }
40
+
41
+ export function fileState(file) {
42
+ if (file.result.squiggles.length > 0) {
43
+ return "has-issues";
44
+ }
45
+ return file.cleaned ? "is-cleaned" : "";
46
+ }
47
+
48
+ function folderState(folder) {
49
+ const states = flatten(folder).map(fileState);
50
+ if (states.includes("has-issues")) {
51
+ return "has-issues";
52
+ }
53
+ return states.includes("is-cleaned") ? "is-cleaned" : "";
54
+ }
55
+
56
+ function ancestors(path) {
57
+ const parts = path.split("/").slice(0, -1);
58
+ return parts.map((_, index) => parts.slice(0, index + 1).join("/"));
59
+ }
60
+
61
+ export function baseName(path) {
62
+ return path.slice(path.lastIndexOf("/") + 1);
63
+ }
64
+
65
+ function fileTitle(file) {
66
+ const count = file.result.squiggles.length;
67
+ return count > 0 ? `${file.path} · ${plural(count, "issue")}` : file.path;
68
+ }
69
+
70
+ function icon(href, className = "icon") {
71
+ const svg = document.createElementNS("http://www.w3.org/2000/svg", "svg");
72
+ svg.setAttribute("class", className);
73
+ svg.setAttribute("aria-hidden", "true");
74
+ const use = document.createElementNS("http://www.w3.org/2000/svg", "use");
75
+ use.setAttribute("href", href);
76
+ svg.append(use);
77
+ return svg;
78
+ }
79
+
80
+ function fileBadge(file) {
81
+ const count = file.result.squiggles.length;
82
+ if (count > 0) {
83
+ return el("span", { class: "tree-count", text: String(count) });
84
+ }
85
+ return file.cleaned ? el("span", { class: "tree-mark" }) : "";
86
+ }
87
+
88
+ function rowClass(state, selected) {
89
+ return ["tree-row", state, selected ? "is-current" : ""].filter(Boolean).join(" ");
90
+ }
91
+
92
+ function keepRowVisible(list, row) {
93
+ const rowBox = row.getBoundingClientRect();
94
+ const listBox = list.getBoundingClientRect();
95
+ if (rowBox.top < listBox.top) {
96
+ list.scrollTop -= listBox.top - rowBox.top + 8;
97
+ } else if (rowBox.bottom > listBox.bottom) {
98
+ list.scrollTop += rowBox.bottom - listBox.bottom + 8;
99
+ }
100
+ }
101
+
102
+ export function createNavigator(onSelect) {
103
+ const stage = byId("stage");
104
+ const explorer = byId("explorer");
105
+ const toggle = byId("explorer-toggle");
106
+ const filter = byId("tree-filter");
107
+ const list = byId("tree-list");
108
+ const narrow = window.matchMedia(NARROW);
109
+ const open = new Set();
110
+ let files = [];
111
+ let order = [];
112
+ let current = null;
113
+
114
+ function setExplorer(visible) {
115
+ const label = visible ? "Hide files" : "Show files";
116
+ stage.classList.toggle("is-collapsed", !visible);
117
+ toggle.setAttribute("aria-expanded", String(visible));
118
+ toggle.setAttribute("aria-label", label);
119
+ toggle.title = label;
120
+ }
121
+
122
+ function explorerVisible() {
123
+ return !stage.classList.contains("is-collapsed");
124
+ }
125
+
126
+ function choose(file) {
127
+ if (narrow.matches) {
128
+ setExplorer(false);
129
+ }
130
+ onSelect(file);
131
+ }
132
+
133
+ function folderRow(folder, depth, expanded) {
134
+ return el("button", {
135
+ class: rowClass(folderState(folder), false),
136
+ type: "button",
137
+ style: `--depth: ${depth * ROW_INDENT}px`,
138
+ "aria-expanded": String(expanded),
139
+ onclick: () => {
140
+ if (open.has(folder.path)) {
141
+ open.delete(folder.path);
142
+ } else {
143
+ open.add(folder.path);
144
+ }
145
+ paintList();
146
+ },
147
+ }, [
148
+ icon("#i-chevron", "icon tree-chevron"),
149
+ icon("#i-folder"),
150
+ el("span", { class: "tree-name", text: folder.name }),
151
+ folderState(folder) ? el("span", { class: "tree-mark" }) : "",
152
+ ]);
153
+ }
154
+
155
+ function fileRow(file, depth, label) {
156
+ const selected = file === current;
157
+ return el("button", {
158
+ class: rowClass(fileState(file), selected),
159
+ type: "button",
160
+ style: `--depth: ${depth * ROW_INDENT}px`,
161
+ title: fileTitle(file),
162
+ "aria-current": selected ? "true" : null,
163
+ onclick: () => choose(file),
164
+ }, [
165
+ el("span", { class: "tree-spacer" }),
166
+ icon("#i-code"),
167
+ el("span", { class: "tree-name", text: label }),
168
+ fileBadge(file),
169
+ ]);
170
+ }
171
+
172
+ function folderRows(folder, depth) {
173
+ const rows = [];
174
+ for (const child of sortedFolders(folder)) {
175
+ const expanded = open.has(child.path);
176
+ rows.push(folderRow(child, depth, expanded));
177
+ if (expanded) {
178
+ rows.push(...folderRows(child, depth + 1));
179
+ }
180
+ }
181
+ rows.push(...sortedFiles(folder).map((file) => fileRow(file, depth, baseName(file.path))));
182
+ return rows;
183
+ }
184
+
185
+ function matchingRows(query) {
186
+ const matches = order.filter((file) => file.path.toLowerCase().includes(query));
187
+ if (matches.length === 0) {
188
+ return [el("p", { class: "tree-empty", text: "No matches" })];
189
+ }
190
+ return matches.map((file) => fileRow(file, 0, file.path));
191
+ }
192
+
193
+ function paintList() {
194
+ const query = filter.value.trim().toLowerCase();
195
+ list.replaceChildren(...(query ? matchingRows(query) : folderRows(buildTree(files), 0)));
196
+ }
197
+
198
+ function revealCurrent() {
199
+ if (current) {
200
+ ancestors(current.path).forEach((path) => open.add(path));
201
+ }
202
+ }
203
+
204
+ function showCurrentRow() {
205
+ const row = list.querySelector(".is-current");
206
+ if (row && explorerVisible()) {
207
+ keepRowVisible(list, row);
208
+ }
209
+ }
210
+
211
+ function step(offset) {
212
+ if (order.length === 0) {
213
+ return;
214
+ }
215
+ const index = Math.max(0, order.indexOf(current));
216
+ onSelect(order[(index + offset + order.length) % order.length]);
217
+ }
218
+
219
+ setExplorer(!narrow.matches);
220
+ toggle.addEventListener("click", () => setExplorer(!explorerVisible()));
221
+ byId("nav-prev").addEventListener("click", () => step(-1));
222
+ byId("nav-next").addEventListener("click", () => step(1));
223
+ filter.addEventListener("input", paintList);
224
+ filter.addEventListener("keydown", (event) => {
225
+ if (event.key === "Enter") {
226
+ list.querySelector(".tree-row:not([aria-expanded])")?.click();
227
+ }
228
+ });
229
+ document.addEventListener("pointerdown", (event) => {
230
+ const outside = !explorer.contains(event.target) && !toggle.contains(event.target);
231
+ if (narrow.matches && explorerVisible() && outside) {
232
+ setExplorer(false);
233
+ }
234
+ });
235
+
236
+ return {
237
+ render(nextFiles, nextCurrent) {
238
+ files = nextFiles;
239
+ order = flatten(buildTree(files));
240
+ open.clear();
241
+ current = nextCurrent;
242
+ filter.value = "";
243
+ revealCurrent();
244
+ paintList();
245
+ list.scrollTop = 0;
246
+ showCurrentRow();
247
+ },
248
+ update(nextFiles, nextCurrent) {
249
+ const moved = nextCurrent !== current;
250
+ files = nextFiles;
251
+ current = nextCurrent;
252
+ if (moved) {
253
+ revealCurrent();
254
+ }
255
+ paintList();
256
+ if (moved) {
257
+ showCurrentRow();
258
+ }
259
+ },
260
+ };
261
+ }
web/src/patch.js ADDED
@@ -0,0 +1,78 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import { diffLines } from "./diff.js";
2
+ import { el } from "./dom.js";
3
+
4
+ const CONTEXT = 3;
5
+ const NO_NEWLINE = "\0";
6
+
7
+ // One entry per line; a last line without a trailing newline carries a marker so the diff sees it.
8
+ function lineEntries(text) {
9
+ const lines = text.split("\n");
10
+ if (lines[lines.length - 1] === "") {
11
+ lines.pop();
12
+ } else {
13
+ lines[lines.length - 1] += NO_NEWLINE;
14
+ }
15
+ return lines;
16
+ }
17
+
18
+ function hunkRanges(rows) {
19
+ const ranges = [];
20
+ rows.forEach((row, index) => {
21
+ if (row.kind === "same") {
22
+ return;
23
+ }
24
+ const start = Math.max(0, index - CONTEXT);
25
+ const end = Math.min(rows.length, index + CONTEXT + 1);
26
+ const last = ranges[ranges.length - 1];
27
+ if (last && start <= last.end) {
28
+ last.end = Math.max(last.end, end);
29
+ } else {
30
+ ranges.push({ start, end });
31
+ }
32
+ });
33
+ return ranges;
34
+ }
35
+
36
+ function patchLine(prefix, text) {
37
+ const missingNewline = text.endsWith(NO_NEWLINE);
38
+ const body = missingNewline ? text.slice(0, -1) : text;
39
+ return missingNewline ? `${prefix}${body}\n\\n` : `${prefix}${body}\n`;
40
+ }
41
+
42
+ const PREFIX = { same: " ", removed: "-", added: "+" };
43
+
44
+ function rangeStart(count, linesBefore) {
45
+ return count === 0 ? linesBefore : linesBefore + 1;
46
+ }
47
+
48
+ function renderHunk(rows, { start, end }) {
49
+ const slice = rows.slice(start, end);
50
+ const oldBefore = rows.slice(0, start).filter((row) => row.kind !== "added").length;
51
+ const newBefore = rows.slice(0, start).filter((row) => row.kind !== "removed").length;
52
+ const oldCount = slice.filter((row) => row.kind !== "added").length;
53
+ const newCount = slice.filter((row) => row.kind !== "removed").length;
54
+ const oldRange = `${rangeStart(oldCount, oldBefore)},${oldCount}`;
55
+ const newRange = `${rangeStart(newCount, newBefore)},${newCount}`;
56
+ const lines = slice.map((row) => patchLine(PREFIX[row.kind], row.text)).join("");
57
+ return `@@ -${oldRange} +${newRange} @@\n${lines}`;
58
+ }
59
+
60
+ export function filePatch(path, before, after) {
61
+ const rows = diffLines(lineEntries(before).join("\n"), lineEntries(after).join("\n"));
62
+ const ranges = hunkRanges(rows);
63
+ if (ranges.length === 0) {
64
+ return "";
65
+ }
66
+ const header = `diff --git a/${path} b/${path}\n--- a/${path}\n+++ b/${path}\n`;
67
+ return header + ranges.map((range) => renderHunk(rows, range)).join("");
68
+ }
69
+
70
+ export function buildPatch(files) {
71
+ return files.map((file) => filePatch(file.path, file.original, file.content)).join("");
72
+ }
73
+
74
+ export function downloadText(text, filename) {
75
+ const url = URL.createObjectURL(new Blob([text], { type: "text/x-diff" }));
76
+ el("a", { href: url, download: filename }).click();
77
+ setTimeout(() => URL.revokeObjectURL(url), 0);
78
+ }
web/src/pull-request.js ADDED
@@ -0,0 +1,99 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import { toHundred } from "./labels.js";
2
+ import { apiUrl } from "./refiner.js";
3
+
4
+ const MESSAGE_TYPE = "codehygiene:github";
5
+ const SIGN_IN_TIMEOUT_MS = 5 * 60 * 1000;
6
+ const POPUP_CHECK_MS = 500;
7
+ const POPUP_FEATURES = "popup,width=720,height=800";
8
+ const MAX_PR_FILES = 20;
9
+
10
+ export class PrFailure extends Error {
11
+ constructor(code) {
12
+ super(code);
13
+ this.code = code;
14
+ }
15
+ }
16
+
17
+ // The sign-in pop-up posts a one-time ticket back to this page only (same origin, same window).
18
+ function waitForTicket(popup) {
19
+ return new Promise((resolve, reject) => {
20
+ const startedAt = Date.now();
21
+ let timer = 0;
22
+ const finish = (settle, value) => {
23
+ clearInterval(timer);
24
+ window.removeEventListener("message", onMessage);
25
+ settle(value);
26
+ };
27
+ function onMessage(event) {
28
+ const fromPopup = event.source === popup && event.origin === window.location.origin;
29
+ if (!fromPopup || event.data?.type !== MESSAGE_TYPE) {
30
+ return;
31
+ }
32
+ if (event.data.ticket) {
33
+ finish(resolve, event.data.ticket);
34
+ } else {
35
+ finish(reject, new PrFailure(event.data.error ?? "failed"));
36
+ }
37
+ }
38
+ timer = setInterval(() => {
39
+ if (popup.closed) {
40
+ finish(reject, new PrFailure("closed"));
41
+ } else if (Date.now() - startedAt > SIGN_IN_TIMEOUT_MS) {
42
+ popup.close();
43
+ finish(reject, new PrFailure("login_required"));
44
+ }
45
+ }, POPUP_CHECK_MS);
46
+ window.addEventListener("message", onMessage);
47
+ });
48
+ }
49
+
50
+ function prFiles(files) {
51
+ return files.slice(0, MAX_PR_FILES).map((file) => ({
52
+ path: file.path,
53
+ content_after: file.content,
54
+ score_before: toHundred(file.originalResult.score),
55
+ score_after: toHundred(file.result.score),
56
+ squiggles_fixed: Math.max(0, file.originalResult.squiggles.length - file.result.squiggles.length),
57
+ }));
58
+ }
59
+
60
+ async function readReply(response) {
61
+ const text = await response.text();
62
+ try {
63
+ return JSON.parse(text);
64
+ } catch {
65
+ return { code: "failed" };
66
+ }
67
+ }
68
+
69
+ // Must be called straight from a click so the pop-up is not blocked.
70
+ export async function openPullRequest({ spec, commitSha, files }) {
71
+ const popup = window.open(apiUrl("github/login"), "codehygiene-github", POPUP_FEATURES);
72
+ if (!popup) {
73
+ throw new PrFailure("popup_blocked");
74
+ }
75
+ const ticket = await waitForTicket(popup);
76
+ const response = await fetch(apiUrl("github/create-pr"), {
77
+ method: "POST",
78
+ headers: { "Content-Type": "application/json" },
79
+ body: JSON.stringify({
80
+ ticket,
81
+ owner: spec.owner,
82
+ repo: spec.repo,
83
+ base_branch: spec.ref,
84
+ base_commit_sha: commitSha,
85
+ files: prFiles(files),
86
+ }),
87
+ });
88
+ const reply = await readReply(response);
89
+ const next = response.ok ? reply.url : reply.install_url;
90
+ if (next) {
91
+ popup.location.href = next;
92
+ } else {
93
+ popup.close();
94
+ }
95
+ if (!response.ok) {
96
+ throw new PrFailure(reply.code ?? "failed");
97
+ }
98
+ return reply;
99
+ }
web/src/ranking.js ADDED
@@ -0,0 +1,45 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ // Ranks the loaded GitHub repo. The server downloads and scores the repo itself;
2
+ // the page only sends the owner and name.
3
+ import { RANK_ERRORS } from "./labels.js";
4
+ import { apiUrl } from "./refiner.js";
5
+
6
+ const PERCENT_FROM = 100;
7
+
8
+ export class RankFailure extends Error {
9
+ constructor(code, detail = {}) {
10
+ super(code);
11
+ this.code = code;
12
+ this.detail = detail;
13
+ }
14
+ }
15
+
16
+ export async function requestRank(spec) {
17
+ const response = await fetch(apiUrl("leaderboard/rank"), {
18
+ method: "POST",
19
+ headers: { "Content-Type": "application/json" },
20
+ body: JSON.stringify({ owner: spec.owner, repo: spec.repo }),
21
+ });
22
+ const body = await response.json().catch(() => ({}));
23
+ if (!response.ok) {
24
+ throw new RankFailure(body.code ?? "failed", body);
25
+ }
26
+ return body;
27
+ }
28
+
29
+ export function rankErrorText(error) {
30
+ if (error.code === "duplicate" && error.detail.repo) {
31
+ return `Ranked as ${error.detail.repo}`;
32
+ }
33
+ return RANK_ERRORS[error.code] ?? RANK_ERRORS.failed;
34
+ }
35
+
36
+ export function rankLabel({ rank, total }) {
37
+ if (total < PERCENT_FROM) {
38
+ return `#${rank} of ${total}`;
39
+ }
40
+ return `Top ${Math.max(1, Math.ceil((rank / total) * 100))}%`;
41
+ }
42
+
43
+ export function boardLink(repo) {
44
+ return `leaderboard/#repo=${encodeURIComponent(repo)}`;
45
+ }
web/src/refiner.js ADDED
@@ -0,0 +1,92 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ export const DEFAULT_MAX_FILE_CHARS = 12000;
2
+
3
+ // Pages below the site root (the leaderboard) set data-root="../" so API calls still reach /api.
4
+ const SITE_ROOT = new URL(document.documentElement.dataset.root ?? "./", document.baseURI);
5
+
6
+ export function apiUrl(path) {
7
+ return new URL(`api/${path}`, SITE_ROOT).href;
8
+ }
9
+
10
+ async function getJson(path, label) {
11
+ const response = await fetch(apiUrl(path), { cache: "no-store" });
12
+ if (!response.ok) {
13
+ throw new Error(`${label} failed (${response.status})`);
14
+ }
15
+ return response.json();
16
+ }
17
+
18
+ export function fetchHealth() {
19
+ return getJson("health", "Health check");
20
+ }
21
+
22
+ export function fetchStats() {
23
+ return getJson("stats", "Stats request");
24
+ }
25
+
26
+ export function fetchLeaderboard() {
27
+ return getJson("leaderboard", "Leaderboard request");
28
+ }
29
+
30
+ function parseFrame(frame) {
31
+ let name = "message";
32
+ const dataLines = [];
33
+ for (const line of frame.split("\n")) {
34
+ if (line.startsWith(":")) {
35
+ continue;
36
+ }
37
+ const colon = line.indexOf(":");
38
+ const field = colon < 0 ? line : line.slice(0, colon);
39
+ const value = colon < 0 ? "" : line.slice(colon + 1).replace(/^ /, "");
40
+ if (field === "event") {
41
+ name = value;
42
+ } else if (field === "data") {
43
+ dataLines.push(value);
44
+ }
45
+ }
46
+ if (dataLines.length === 0) {
47
+ return null;
48
+ }
49
+ return { name, data: JSON.parse(dataLines.join("\n")) };
50
+ }
51
+
52
+ async function responseError(response) {
53
+ const body = await response.text();
54
+ try {
55
+ const parsed = JSON.parse(body);
56
+ return parsed.detail || parsed.message || `Request failed (${response.status})`;
57
+ } catch {
58
+ return body || `Request failed (${response.status})`;
59
+ }
60
+ }
61
+
62
+ export async function streamFix(body, { signal, onEvent }) {
63
+ const response = await fetch(apiUrl("fix/stream"), {
64
+ method: "POST",
65
+ headers: { "Content-Type": "application/json", Accept: "text/event-stream" },
66
+ body: JSON.stringify(body),
67
+ signal,
68
+ });
69
+ if (!response.ok || !response.body) {
70
+ onEvent("error", { code: "http", message: await responseError(response) });
71
+ return;
72
+ }
73
+
74
+ const reader = response.body.pipeThrough(new TextDecoderStream()).getReader();
75
+ let buffer = "";
76
+ for (;;) {
77
+ const { value, done } = await reader.read();
78
+ if (done) {
79
+ break;
80
+ }
81
+ buffer = (buffer + value).replace(/\r\n?/g, "\n");
82
+ let boundary = buffer.indexOf("\n\n");
83
+ while (boundary >= 0) {
84
+ const event = parseFrame(buffer.slice(0, boundary));
85
+ buffer = buffer.slice(boundary + 2);
86
+ if (event) {
87
+ onEvent(event.name, event.data);
88
+ }
89
+ boundary = buffer.indexOf("\n\n");
90
+ }
91
+ }
92
+ }
web/src/reward-facts.json ADDED
@@ -0,0 +1,20 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "0.5.0": {
3
+ "ref": "0221af718b51a56b6ee0fbea52f0bf42afa381bb",
4
+ "facts": [
5
+ { "label": "Gaming gain", "value": "+0.007", "title": "Largest rl_reward gain from an adversarial probe (40 padding helpers)" },
6
+ { "label": "Held-out issues", "value": "2.0 / 100 lines", "title": "25 unmodified CPython 3.13 stdlib modules" },
7
+ { "label": "Python vs browser", "value": "Δ 0.0000", "title": "Largest score difference between the Python and JavaScript scorers" },
8
+ { "label": "Model", "value": "14.47 KiB", "title": "1.58-bit weights, NumPy only" }
9
+ ]
10
+ },
11
+ "0.5.1": {
12
+ "ref": "b97871b",
13
+ "facts": [
14
+ { "label": "Gaming gain", "value": "+0.007", "title": "Largest rl_reward gain from an adversarial probe (40 padding helpers)" },
15
+ { "label": "Held-out issues", "value": "1.9 / 100 lines", "title": "25 unmodified CPython 3.13 stdlib modules" },
16
+ { "label": "Python vs browser", "value": "Δ 0.0000", "title": "Largest score difference between the Python and JavaScript scorers" },
17
+ { "label": "Model", "value": "14.85 KiB", "title": "1.58-bit weights, NumPy only" }
18
+ ]
19
+ }
20
+ }
web/src/sample.js ADDED
@@ -0,0 +1,33 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import presets from "shared:presets";
2
+ import { byId } from "./dom.js";
3
+ import { scoreWord, toHundred } from "./labels.js";
4
+ import { diffNodes } from "./stage.js";
5
+
6
+ const FILENAME = "send_receipt.py";
7
+ const CLEANED = `import logging
8
+
9
+ logger = logging.getLogger(__name__)
10
+
11
+
12
+ def send_receipt(order: Order) -> None:
13
+ if order is None or not order.paid or order.refunded:
14
+ return
15
+ email = order.email
16
+ if not email or not email.strip():
17
+ return
18
+ try:
19
+ mailer.send(email, order.id)
20
+ except Exception:
21
+ logger.exception("Could not send receipt for order %s", order.id)`;
22
+
23
+ export async function paintSample(scoreAll) {
24
+ const original = presets.find((preset) => preset.filename === FILENAME).before;
25
+ byId("sample-code").replaceChildren(...diffNodes({ base: original, next: CLEANED, filename: FILENAME }));
26
+ const [result] = await scoreAll([CLEANED]);
27
+ const points = toHundred(result.score);
28
+ const { word, className } = scoreWord(points);
29
+ const wordNode = byId("sample-word");
30
+ byId("sample-value").textContent = String(points);
31
+ wordNode.textContent = word;
32
+ wordNode.className = `stage-score-word ${className}`.trim();
33
+ }
web/src/score-worker.js ADDED
@@ -0,0 +1,21 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import { loadWeights, scoreCode } from "shared:engine";
2
+
3
+ const WARMUP_CODE = ["def warm(items):", " for item in items:", " return item", ""].join("\n");
4
+
5
+ const weightsReady = loadWeights(new URL("./weights.npz", import.meta.url).href).then((weights) => {
6
+ scoreCode(WARMUP_CODE, weights);
7
+ return weights;
8
+ });
9
+
10
+ globalThis.addEventListener("message", async (event) => {
11
+ const { id, code, threshold } = event.data;
12
+ try {
13
+ const weights = await weightsReady;
14
+ const started = performance.now();
15
+ const { score, squiggles } = scoreCode(code, weights, threshold);
16
+ const ms = performance.now() - started;
17
+ globalThis.postMessage({ id, result: { score, squiggles, ms } });
18
+ } catch (error) {
19
+ globalThis.postMessage({ id, error: String(error && error.message ? error.message : error) });
20
+ }
21
+ });
web/src/scoring.js ADDED
@@ -0,0 +1,63 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ export const THRESHOLD = 0.55;
2
+
3
+ const WORKER_URL = new URL("./score-worker.js", import.meta.url);
4
+
5
+ class ScoreWorker {
6
+ constructor() {
7
+ this.worker = new Worker(WORKER_URL, { type: "module" });
8
+ this.pending = new Map();
9
+ this.nextId = 1;
10
+ this.worker.addEventListener("message", (event) => this.settle(event.data));
11
+ }
12
+
13
+ settle({ id, result, error }) {
14
+ const job = this.pending.get(id);
15
+ this.pending.delete(id);
16
+ if (error) {
17
+ job.reject(new Error(error));
18
+ } else {
19
+ job.resolve(result);
20
+ }
21
+ }
22
+
23
+ score(code) {
24
+ const id = this.nextId;
25
+ this.nextId += 1;
26
+ return new Promise((resolve, reject) => {
27
+ this.pending.set(id, { resolve, reject });
28
+ this.worker.postMessage({ id, code, threshold: THRESHOLD });
29
+ });
30
+ }
31
+ }
32
+
33
+ export function createScorer() {
34
+ const cores = navigator.hardwareConcurrency || 4;
35
+ const workers = Array.from({ length: Math.min(4, Math.max(2, cores - 1)) }, () => new ScoreWorker());
36
+ let turn = 0;
37
+
38
+ function scoreOne(code) {
39
+ turn = (turn + 1) % workers.length;
40
+ return workers[turn].score(code);
41
+ }
42
+
43
+ async function scoreAll(codes, onProgress = () => {}) {
44
+ const results = new Array(codes.length);
45
+ let cursor = 0;
46
+ let completed = 0;
47
+
48
+ async function drain(worker) {
49
+ while (cursor < codes.length) {
50
+ const index = cursor;
51
+ cursor += 1;
52
+ results[index] = await worker.score(codes[index]);
53
+ completed += 1;
54
+ onProgress(completed, codes.length);
55
+ }
56
+ }
57
+
58
+ await Promise.all(workers.map(drain));
59
+ return results;
60
+ }
61
+
62
+ return { scoreOne, scoreAll };
63
+ }
web/src/sources.js ADDED
@@ -0,0 +1,89 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import presets from "shared:presets";
2
+ import { RepoError, fetchRepoFiles, isReadableText, isTestPath, selectLocalEntries } from "./github.js";
3
+ import { sniffExtension } from "./languages.js";
4
+
5
+ const EXAMPLE_ORDER = ["send_receipt.py", "webhook.go", "engine.js", "pricing.ts", "frame.rs"];
6
+ export const RUN_SIZE = 5;
7
+
8
+ function makeFile(path, content, result) {
9
+ return {
10
+ path,
11
+ original: content,
12
+ content,
13
+ originalResult: result,
14
+ result,
15
+ cleaned: false,
16
+ kept: false,
17
+ lastRejected: false,
18
+ attempted: false,
19
+ restingText: null,
20
+ };
21
+ }
22
+
23
+ export function isSloppy(file) {
24
+ return file.result.squiggles.length > 0;
25
+ }
26
+
27
+ function byScore(left, right) {
28
+ return left.result.score - right.result.score || left.path.localeCompare(right.path);
29
+ }
30
+
31
+ export function eligibleFiles(files, maxChars) {
32
+ const fits = (file) => !file.attempted && file.content.length <= maxChars;
33
+ const sloppy = files.filter((file) => isSloppy(file) && fits(file));
34
+ const nonTest = sloppy.filter((file) => !isTestPath(file.path));
35
+ return (nonTest.length > 0 ? nonTest : sloppy).sort(byScore);
36
+ }
37
+
38
+ function scoredFiles(entries, results) {
39
+ const files = entries.map((entry, index) => makeFile(entry.path, entry.content, results[index]));
40
+ return files.sort(byScore);
41
+ }
42
+
43
+ export async function loadExamples(scoreAll) {
44
+ const ordered = EXAMPLE_ORDER.map((name) => presets.find((preset) => preset.filename === name));
45
+ const results = await scoreAll(ordered.map((preset) => preset.before));
46
+ return ordered.map((preset, index) => makeFile(preset.filename, preset.before, results[index]));
47
+ }
48
+
49
+ export async function loadRepo(spec, { signal, scoreAll, onProgress, pinCommit }) {
50
+ const fetched = await fetchRepoFiles(spec, {
51
+ signal,
52
+ pinCommit,
53
+ focusPath: spec.blobPath,
54
+ onProgress: (done, total) => onProgress(`Fetching ${done} of ${total}`),
55
+ });
56
+ const results = await scoreAll(
57
+ fetched.files.map((entry) => entry.content),
58
+ (done, total) => onProgress(`Scoring ${done} of ${total}`),
59
+ );
60
+ signal.throwIfAborted();
61
+ return { files: scoredFiles(fetched.files, results), commitSha: fetched.commitSha };
62
+ }
63
+
64
+ function relativePath(file) {
65
+ const parts = (file.webkitRelativePath || file.name).split("/");
66
+ return parts.length > 1 ? parts.slice(1).join("/") : parts[0];
67
+ }
68
+
69
+ export async function loadFolder(fileList, { scoreAll, onProgress }) {
70
+ const byPath = new Map([...fileList].map((file) => [relativePath(file), file]));
71
+ const entries = selectLocalEntries([...byPath].map(([path, file]) => ({ path, size: file.size })));
72
+ const texts = await Promise.all(entries.map((entry) => byPath.get(entry.path).text()));
73
+ const readable = entries
74
+ .map((entry, index) => ({ path: entry.path, content: texts[index] }))
75
+ .filter((entry) => isReadableText(entry.content));
76
+ if (readable.length === 0) {
77
+ throw new RepoError("no_files", "No code files found");
78
+ }
79
+ const results = await scoreAll(
80
+ readable.map((entry) => entry.content),
81
+ (done, total) => onProgress(`Scoring ${done} of ${total}`),
82
+ );
83
+ return scoredFiles(readable, results);
84
+ }
85
+
86
+ export async function loadSnippet(text, scoreAll) {
87
+ const [result] = await scoreAll([text]);
88
+ return [makeFile(`snippet.${sniffExtension(text)}`, text, result)];
89
+ }
web/src/stage.js ADDED
@@ -0,0 +1,226 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import { scorerSpanToOffsets } from "./columns.js";
2
+ import { diffLines } from "./diff.js";
3
+ import { byId, el, prefersReducedMotion, tweenNumber } from "./dom.js";
4
+ import { tokenizeLines } from "./highlight.js";
5
+ import { NOTES, STATUS, scoreWord } from "./labels.js";
6
+
7
+ const NOTE_STAGGER_MS = 30;
8
+ const NOTE_STAGGER_STEPS = 7;
9
+ const BUSY_STATUSES = new Set([STATUS.reading, STATUS.rewriting]);
10
+ const SCORE_TWEEN_MS = 600;
11
+ const TAB_COLUMNS = 4;
12
+
13
+ function leadingColumns(text) {
14
+ let columns = 0;
15
+ for (const char of text) {
16
+ if (char === " ") {
17
+ columns += 1;
18
+ } else if (char === "\t") {
19
+ columns += TAB_COLUMNS;
20
+ } else {
21
+ break;
22
+ }
23
+ }
24
+ return columns;
25
+ }
26
+
27
+ function textNodes(text, tokens, start, end) {
28
+ const nodes = [];
29
+ let pos = start;
30
+ for (const token of tokens) {
31
+ if (token.to <= start || token.from >= end) {
32
+ continue;
33
+ }
34
+ const from = Math.max(token.from, start);
35
+ const to = Math.min(token.to, end);
36
+ if (from > pos) {
37
+ nodes.push(text.slice(pos, from));
38
+ }
39
+ nodes.push(el("span", { class: token.className, text: text.slice(from, to) }));
40
+ pos = to;
41
+ }
42
+ if (pos < end) {
43
+ nodes.push(text.slice(pos, end));
44
+ }
45
+ return nodes;
46
+ }
47
+
48
+ function leadingLength(text) {
49
+ return text.length - text.trimStart().length;
50
+ }
51
+
52
+ function bodyNodes(text, tokens, start, squiggle) {
53
+ const from = squiggle ? Math.max(squiggle.from, start) : start;
54
+ if (!squiggle || squiggle.to <= from) {
55
+ return textNodes(text, tokens, start, text.length);
56
+ }
57
+ return [
58
+ ...textNodes(text, tokens, start, from),
59
+ el("span", { class: "squiggle" }, textNodes(text, tokens, from, squiggle.to)),
60
+ ...textNodes(text, tokens, squiggle.to, text.length),
61
+ ];
62
+ }
63
+
64
+ function srcNode(text, tokens, squiggle) {
65
+ const lead = leadingLength(text);
66
+ const children = bodyNodes(text, tokens, lead, squiggle);
67
+ if (lead > 0) {
68
+ children.unshift(el("span", { class: "lead", text: text.slice(0, lead) }));
69
+ }
70
+ return el("span", { class: "src", style: `--indent: ${leadingColumns(text)}` }, children);
71
+ }
72
+
73
+ function lineNode({ number, text, tokens, className = "line", squiggle = null, note = "" }) {
74
+ return el("div", { class: className }, [
75
+ el("span", { class: "ln", text: number === null ? "" : String(number) }),
76
+ srcNode(text, tokens, squiggle),
77
+ note ? el("span", { class: "note", text: note }) : "",
78
+ ]);
79
+ }
80
+
81
+ function flaggedLines(lines, squiggles) {
82
+ const byLine = new Map();
83
+ let previous = null;
84
+ for (const squiggle of squiggles) {
85
+ const text = lines[squiggle.line - 1];
86
+ if (text === undefined) {
87
+ continue;
88
+ }
89
+ const continues = previous && previous.kind === squiggle.kind && previous.line === squiggle.line - 1;
90
+ byLine.set(squiggle.line, {
91
+ span: scorerSpanToOffsets(text, squiggle.col_start, squiggle.col_end),
92
+ note: continues ? "" : NOTES[squiggle.kind],
93
+ });
94
+ previous = squiggle;
95
+ }
96
+ return byLine;
97
+ }
98
+
99
+ function paintLines(container, { text, filename, squiggles, enter }) {
100
+ const lines = text.split("\n");
101
+ const tokens = tokenizeLines(text, filename);
102
+ const flagged = flaggedLines(lines, squiggles);
103
+ const stagger = enter && !prefersReducedMotion();
104
+ let order = 0;
105
+ const nodes = lines.map((lineText, index) => {
106
+ const flag = flagged.get(index + 1);
107
+ const node = lineNode({
108
+ number: index + 1,
109
+ text: lineText,
110
+ tokens: tokens[index],
111
+ className: flag ? "line is-flagged" : "line",
112
+ squiggle: flag?.span,
113
+ note: flag?.note,
114
+ });
115
+ if (flag && stagger) {
116
+ node.style.setProperty("--delay", `${Math.min(order, NOTE_STAGGER_STEPS) * NOTE_STAGGER_MS}ms`);
117
+ order += 1;
118
+ }
119
+ return node;
120
+ });
121
+ container.classList.toggle("is-entering", stagger);
122
+ container.replaceChildren(...nodes);
123
+ }
124
+
125
+ function scrollIntoStage(code, line) {
126
+ const offset = line.getBoundingClientRect().top - code.getBoundingClientRect().top + code.scrollTop;
127
+ code.scrollTop = Math.max(0, offset - code.clientHeight / 3);
128
+ }
129
+
130
+ function readingClass(row, reading) {
131
+ if (!reading || row.kind !== "same") {
132
+ return "line";
133
+ }
134
+ const lineNumber = row.newIndex + 1;
135
+ return lineNumber >= reading.first && lineNumber <= reading.last ? "line is-reading" : "line";
136
+ }
137
+
138
+ export function diffNodes(options) {
139
+ const { base, next, filename, reading = null } = options;
140
+ const { squiggles = [], squigglesOn = "base" } = options;
141
+ const onNext = squigglesOn === "next";
142
+ const flagged = flaggedLines((onNext ? next : base).split("\n"), squiggles);
143
+ const flagFor = (row) => flagged.get((onNext ? row.newIndex : row.oldIndex) + 1);
144
+ const oldTokens = tokenizeLines(base, filename);
145
+ const newTokens = tokenizeLines(next, filename);
146
+ return diffLines(base, next).map((row) => {
147
+ if (row.kind === "removed") {
148
+ const tokens = oldTokens[row.oldIndex];
149
+ return lineNode({ number: null, text: row.text, tokens, className: "line is-removed" });
150
+ }
151
+ const added = row.kind === "added";
152
+ const flag = added && !onNext ? null : flagFor(row);
153
+ const rowClass = added ? "line is-added" : readingClass(row, reading);
154
+ return lineNode({
155
+ number: row.newIndex + 1,
156
+ text: row.text,
157
+ tokens: newTokens[row.newIndex],
158
+ className: flag ? `${rowClass} is-flagged` : rowClass,
159
+ squiggle: flag?.span,
160
+ note: flag?.note,
161
+ });
162
+ });
163
+ }
164
+
165
+ export function createStage() {
166
+ const path = byId("stage-path");
167
+ const status = byId("stage-status");
168
+ const code = byId("stage-code");
169
+ const scoreBox = byId("stage-score");
170
+ let shownPoints = null;
171
+
172
+ function setStatus(text) {
173
+ status.textContent = text;
174
+ status.toggleAttribute("data-busy", BUSY_STATUSES.has(text));
175
+ }
176
+
177
+ function setScore(points, animate) {
178
+ const value = scoreBox.querySelector(".stage-score-value");
179
+ const word = scoreBox.querySelector(".stage-score-word");
180
+ const { word: label, className } = scoreWord(points);
181
+ tweenNumber(value, shownPoints ?? points, points, animate ? SCORE_TWEEN_MS : 0);
182
+ shownPoints = points;
183
+ word.textContent = label;
184
+ word.className = `stage-score-word ${className}`.trim();
185
+ }
186
+
187
+ function renderPlain({ text, filename, squiggles, enter }) {
188
+ paintLines(code, { text, filename, squiggles, enter });
189
+ const first = code.querySelector(".is-flagged");
190
+ if (enter) {
191
+ code.scrollTop = 0;
192
+ if (first) {
193
+ scrollIntoStage(code, first);
194
+ }
195
+ }
196
+ }
197
+
198
+ function renderDiff(options) {
199
+ code.classList.remove("is-entering");
200
+ code.replaceChildren(...diffNodes(options));
201
+ const target = options.followChange ? code.querySelector(".is-removed, .is-added, .is-reading") : null;
202
+ if (target) {
203
+ scrollIntoStage(code, target);
204
+ }
205
+ }
206
+
207
+ return {
208
+ setHead({ filename, statusText, points, animate = false }) {
209
+ const slash = filename.lastIndexOf("/");
210
+ path.title = filename;
211
+ path.replaceChildren(
212
+ slash >= 0 ? el("span", { class: "path-dir", text: filename.slice(0, slash + 1) }) : "",
213
+ filename.slice(slash + 1),
214
+ );
215
+ setStatus(statusText);
216
+ setScore(points, animate);
217
+ },
218
+ setStatus,
219
+ setScore,
220
+ resetScore() {
221
+ shownPoints = null;
222
+ },
223
+ renderPlain,
224
+ renderDiff,
225
+ };
226
+ }