diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000000000000000000000000000000000000..44efbbc02232d0fa36cc360cf1abf7fcc9141cd9 --- /dev/null +++ b/.dockerignore @@ -0,0 +1,8 @@ +**/__pycache__ +**/*.pyc +.pytest_cache +tests +web/node_modules +web/dist +web/vendor +.env diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000000000000000000000000000000000000..2faba93f183fe4ac26e9e12ba9416a88c070259d --- /dev/null +++ b/.gitignore @@ -0,0 +1,7 @@ +__pycache__/ +*.pyc +.pytest_cache/ +web/node_modules/ +web/dist/ +web/vendor/ +.codex-notes/ diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000000000000000000000000000000000000..134d19ce1c8a0bd134f3ed4ba24f817a87a53933 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,50 @@ +ARG CODEHYGIENE_REF=0221af718b51a56b6ee0fbea52f0bf42afa381bb + +FROM node:22-slim AS web +ARG CODEHYGIENE_REF +WORKDIR /build +COPY web/package.json web/package-lock.json web/ +RUN cd web && npm ci +COPY web/ web/ +ADD https://raw.githubusercontent.com/brianbell-x/codehygiene/${CODEHYGIENE_REF}/docs/engine.js web/vendor/engine.js +ADD https://raw.githubusercontent.com/brianbell-x/codehygiene/${CODEHYGIENE_REF}/docs/presets.json web/vendor/presets.json +ADD https://raw.githubusercontent.com/brianbell-x/codehygiene/${CODEHYGIENE_REF}/docs/weights.npz web/vendor/weights.npz +RUN cd web && npm run build + +FROM python:3.12-slim +ARG CODEHYGIENE_REF +ARG REFINER_REPO=Qwen/Qwen2.5-Coder-0.5B-Instruct-GGUF +ARG REFINER_FILE=qwen2.5-coder-0.5b-instruct-q4_k_m.gguf + +RUN apt-get update \ + && apt-get install -y --no-install-recommends libgomp1 git \ + && rm -rf /var/lib/apt/lists/* + +RUN useradd -m -u 1000 user +USER user +ENV HOME=/home/user \ + PATH=/home/user/.local/bin:$PATH \ + PYTHONUNBUFFERED=1 +WORKDIR /home/user/app + +COPY --chown=user requirements.txt . +RUN pip install --no-cache-dir --user \ + --extra-index-url https://abetlen.github.io/llama-cpp-python/whl/cpu \ + --only-binary=llama-cpp-python \ + -r requirements.txt \ + "codehygiene @ git+https://github.com/brianbell-x/codehygiene@${CODEHYGIENE_REF}" + +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')" \ + "${REFINER_REPO}" "${REFINER_FILE}" + +COPY --chown=user server/ server/ +COPY --from=web --chown=user /build/web/dist web/dist +COPY --from=web --chown=user /build/web/vendor/presets.json web/vendor/presets.json + +ENV REFINER_BACKEND=llama \ + REFINER_REPO=${REFINER_REPO} \ + REFINER_FILE=${REFINER_FILE} \ + REFINER_MODEL_PATH=/home/user/models/${REFINER_FILE} + +EXPOSE 7860 +CMD ["python", "-m", "uvicorn", "server.app:app", "--host", "0.0.0.0", "--port", "7860", "--workers", "1", "--no-access-log"] diff --git a/README.md b/README.md index d3ec649a0f32dceaa191d28e419b52b9aad32d90..fd4f00ca4025be6592cdb14ffd826dbd5346b6eb 100644 --- a/README.md +++ b/README.md @@ -1,11 +1,62 @@ --- -title: Codehygiene -emoji: 📚 -colorFrom: yellow -colorTo: purple +title: codehygiene +emoji: 🧹 +colorFrom: red +colorTo: gray sdk: docker -pinned: false +app_port: 7860 short_description: Paste a GitHub link and watch the code get cleaned up +pinned: false +suggested_hardware: cpu-basic --- -Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference +# codehygiene Space + +Paste a GitHub link. The page finds the hardest-to-read files and a small model rewrites them while you watch. + +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. + +## Settings + +| Variable | Default | What it does | +|---|---|---| +| `REFINER_BACKEND` | `llama` | `llama` runs the GGUF model. `mock` replays recorded patches. | +| `REFINER_REPO` | `Qwen/Qwen2.5-Coder-0.5B-Instruct-GGUF` | Build arg. Hub repo of the GGUF. | +| `REFINER_FILE` | `qwen2.5-coder-0.5b-instruct-q4_k_m.gguf` | Build arg. GGUF file name. | +| `REFINER_MODEL_PATH` | set by the Dockerfile | Path of the GGUF inside the image. | +| `CODEHYGIENE_REF` | pinned commit | Build arg. Version of the scorer and web assets. | +| `MAX_FILE_CHARS` | `12000` | Longer files are not sent to the model. | +| `WARMUP` | on for `llama`, off for `mock` | Fix the examples in the background after start. | +| `MOCK_PROMPT_DELAY` | `1.2` | Seconds before the mock starts streaming. | +| `MOCK_PIECES_PER_SECOND` | `60` | Mock streaming speed. | + +Counters, privacy rules and the GitHub App settings for `Create PR` are in [TELEMETRY.md](TELEMETRY.md). + +## Run locally + +Mock mode needs only FastAPI, uvicorn and numpy. + +```bash +cd space +REFINER_BACKEND=mock python -m uvicorn server.app:app --port 7860 +``` + +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`. + +Run the tests: + +```bash +cd space +python -m pytest tests -q +``` + +## Swap the model + +Pass other build args. The model must follow the ChatML prompt format. + +```bash +docker build \ + --build-arg REFINER_REPO=your-name/your-refiner-GGUF \ + --build-arg REFINER_FILE=refiner-q4_k_m.gguf \ + -t codehygiene-space . +``` diff --git a/TELEMETRY.md b/TELEMETRY.md new file mode 100644 index 0000000000000000000000000000000000000000..7e23cb523eb519563f327a08e43126928dfb45ac --- /dev/null +++ b/TELEMETRY.md @@ -0,0 +1,134 @@ +# Telemetry + +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)). + +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. + +## What is counted + +| Counter | Counted by | When it goes up | +|---|---|---| +| `files_scored` | Browser report, checked by the server | A file from a repo, folder or snippet is scored for the first time. | +| `files_cleaned` | Server only | The refiner finishes an accepted fix of a file for the first time. | +| `fix_runs` | Server only | Every accepted refiner run, repeats included. | +| `avg_score_before`, `avg_score_after` | Server only | Mean score out of 100 of cleaned files, before and after. | +| `issues_fixed` | Derived | Sum of `squiggles_resolved_by_kind`. | +| `median_score` | Browser report | Median score out of 100 of scored files, from a 101-bin histogram. | +| `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. | +| `by_ext` | Both | Files scored and cleaned per extension, and the mean score of scored files. Unknown extensions count as `other`. | +| `by_loc_bucket` | Browser report | Files scored per size band: `0-50`, `50-100`, `100-300`, `300-1000`, `1000+` lines. | +| `squiggles_before_by_kind` | Browser report | Issues found in scored files, per kind. | +| `squiggles_resolved_by_kind` | Server only | Issues removed by accepted fixes, per kind. | +| `prs_opened` | Server only | The GitHub App opens a pull request. | +| `prs_merged`, `prs_closed_unmerged` | Signed GitHub webhook | A pull request opened by the App is merged, or closed without merging. | +| `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. | +| `leaderboard_entries` | Server only | Repos on the leaderboard. | + +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). + +`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: + +| Banner item | Shown when | +|---|---| +| Issues fixed | `files_cleaned` ≥ 1,000 | +| PRs merged | `prs_decided` ≥ 20 | +| Time per file | `score_ms_samples` ≥ 1,000 | +| Leaderboard link | `leaderboard_entries` ≥ 5 | + +The Stats tab of the leaderboard page shows every number from the first count, with its sample size. + +## What the browser sends + +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: + +```json +{ + "source_kind": "repo", + "files": [ + { + "digest16": "9f84c2a10e77b31d", + "ext": "py", + "loc_bucket": "100-300", + "score_before": 29, + "squiggles": { "deep_nesting": 2, "swallowed_error": 1 }, + "score_ms": 14 + } + ] +} +``` + +| Field | Meaning | +|---|---| +| `source_kind` | `repo`, `folder` or `snippet`. Example runs send nothing. | +| `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. | +| `ext` | Lower-case file extension. | +| `loc_bucket` | Size band, not the line count. | +| `score_before` | Score out of 100. | +| `squiggles` | Issue counts per kind. | +| `score_ms` | Milliseconds the browser took to score the file, capped at 60,000. Optional. | + +Any other field is rejected. A batch holds at most 200 files. Each address may send 10 batches a minute. + +## What is never kept + +- **Uploaded folders and pasted snippets** skip the result cache in both directions. Their content exists only in memory for the request that fixes them. +- **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. +- **Logs** carry no content, diffs, file names, folder or repository names, cookies or addresses. The access log is turned off. +- **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. +- **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. + +## Where the counters live + +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. + +`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. + +## Pull requests + +`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. + +1. A pop-up asks the visitor to authorize the App on GitHub. +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. +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. +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. +5. A branch `codehygiene/` is created from that commit, and the pull request lists each file with its score before and after and the issues resolved. +6. The visitor's token is held in memory for at most 3 minutes, used for that one pull request, then revoked. + +`POST /api/github/webhook` accepts only requests signed with `GITHUB_WEBHOOK_SECRET` (`X-Hub-Signature-256`, compared in constant time). Anything else gets `401`. + +## Leaderboard + +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. + +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: + +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. +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. +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. +4. Requires at least 5 such files and 1,000 lines. +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. +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. +7. Refuses a commit that is already ranked under another name, which catches mirrors and forks. + +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. + +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. + +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. + +## Settings + +| Variable | What it does | +|---|---| +| `STATS_PATH` | Local `stats.json` path. | +| `HF_STATS_DATASET_REPO`, `HF_TOKEN` | Dataset repo that keeps `stats.json` across restarts. The repo can be public. | +| `STATS_SYNC_SECONDS` | Sync interval, default `300`. | +| `ALLOW_MOCK_TELEMETRY` | Count fixes from the mock backend. Tests only. | +| `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. | +| `GITHUB_APP_SLUG` | Optional. Read from GitHub when unset. | +| `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. | +| `LEADERBOARD_PATH` | Local `leaderboard.json` path, default `/tmp/codehygiene_leaderboard.json`. | +| `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. | +| `LEADERBOARD_SEED_PAUSE` | Seconds between seed repos, default `20`. | + +The App needs repository permissions **Contents: write** and **Pull requests: write**, the callback URL `https://.hf.space/api/github/callback`, and the webhook URL `https://.hf.space/api/github/webhook` with the **Pull request** event. diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000000000000000000000000000000000000..fd5af1dddf2b80f50f92e656006b079ae3053c3d --- /dev/null +++ b/requirements.txt @@ -0,0 +1,7 @@ +fastapi==0.115.12 +uvicorn==0.34.2 +numpy>=1.26,<3 +llama-cpp-python==0.3.36 +httpx==0.28.1 +PyJWT[crypto]==2.10.1 +huggingface_hub>=0.24 diff --git a/server/__init__.py b/server/__init__.py new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git a/server/app.py b/server/app.py new file mode 100644 index 0000000000000000000000000000000000000000..106087852725a59553218893168042791c1c8dd8 --- /dev/null +++ b/server/app.py @@ -0,0 +1,330 @@ +from __future__ import annotations + +import asyncio +import json +import logging +from collections.abc import AsyncIterator, Awaitable, Callable +from contextlib import asynccontextmanager +from html import escape +from typing import Any + +import httpx +from fastapi import FastAPI, HTTPException, Request +from fastapi.middleware.cors import CORSMiddleware +from fastapi.responses import HTMLResponse, JSONResponse, PlainTextResponse, RedirectResponse, StreamingResponse +from fastapi.staticfiles import StaticFiles +from pydantic import BaseModel, Field + +from .backends import Refiner, make_refiner +from .config import Settings, load_settings +from .fix_loop import Cancelled, Emit, FixRequest, RefinerUnavailable +from .fix_queue import QueueFull +from .github_pr import CreatePrBody, PrError, PullRequests +from .hygiene import SCORER_VERSION, score_dict +from .leaderboard import BOARD_FILE, SEED_REPOS, Leaderboard, RankBody, RankError +from .service import FixService +from .telemetry import RateLimiter, ScanBody, SourceKind, StatsStore, SyncedFile, Telemetry, preset_digests + +log = logging.getLogger("space") + +PING_SECONDS = 10.0 +SSE_HEADERS = {"Cache-Control": "no-cache, no-transform", "X-Accel-Buffering": "no"} +MAX_BODY_CHARS = 200_000 +QUEUE_FULL_MESSAGE = "The refiner queue is full. Try again shortly." +ROOT_NOTE = "codehygiene Space API is running. Build space/web to serve the UI here.\n" +STREAM_ONLY_KEYS = ("tokens", "tokens_per_second") +ERROR_STATUS = {"model_unavailable": 503, "queue_full": 429, "cancelled": 499} +SCAN_BATCHES_PER_MINUTE = 10 +SCAN_BATCHES_PER_MINUTE_ALL = 600 + +RANKS_PER_MINUTE = 2 +RANKS_PER_MINUTE_ALL = 20 + +STATS_HEADERS = {"Cache-Control": "public, max-age=60"} +CALLBACK_PAGE = """ + +CodeHygiene

{message}

+""" +SIGNED_IN = "Signed in. Return to CodeHygiene." +CALLBACK_HEADERS = {"Cache-Control": "no-store", "Referrer-Policy": "no-referrer"} + + +def callback_page(message: str, reply: dict[str, str], status_code: int = 200) -> HTMLResponse: + page = CALLBACK_PAGE.format(message=escape(message), reply=json.dumps(reply)) + return HTMLResponse(page, status_code=status_code, headers=CALLBACK_HEADERS) + + +class ScoreBody(BaseModel): + filename: str = "" + content: str = Field(max_length=MAX_BODY_CHARS) + threshold: float = Field(0.55, ge=0.0, le=1.0) + + +class FixBody(ScoreBody): + max_passes: int = Field(2, ge=1, le=2) + no_cache: bool = False + source_kind: SourceKind = "snippet" + + def to_request(self) -> FixRequest: + return FixRequest(self.filename, self.content, self.threshold, self.max_passes) + + +Producer = Callable[[Emit], Awaitable[None]] + + +def sse_frame(name: str, data: dict[str, Any]) -> str: + return f"event: {name}\ndata: {json.dumps(data)}\n\n" + + +async def guarded(produce: Producer, emit: Emit) -> None: + try: + await produce(emit) + except Cancelled: + log.info("fix cancelled") + except QueueFull: + emit("error", {"code": "queue_full", "message": QUEUE_FULL_MESSAGE}) + except RefinerUnavailable as exc: + log.warning("refiner unavailable: %s", exc) + emit("error", {"code": "model_unavailable", "message": str(exc)}) + except Exception as exc: + log.error("fix failed: %s", type(exc).__name__) + emit("error", {"code": "internal", "message": f"{type(exc).__name__}: {exc}"}) + + +async def sse_events(produce: Producer) -> AsyncIterator[str]: + events: asyncio.Queue[tuple[str, dict[str, Any]] | None] = asyncio.Queue() + task = asyncio.create_task(guarded(produce, lambda name, data: events.put_nowait((name, data)))) + task.add_done_callback(lambda _: events.put_nowait(None)) + try: + while True: + try: + item = await asyncio.wait_for(events.get(), PING_SECONDS) + except TimeoutError: + yield ": ping\n\n" + continue + if item is None: + return + yield sse_frame(*item) + finally: + task.cancel() + + +def caller_of(request: Request) -> str: + """Address used only for the rate-limit bucket check, then discarded.""" + forwarded = request.headers.get("x-forwarded-for", "").split(",")[0].strip() + return forwarded or (request.client.host if request.client else "") + + +def error_response(error: PrError | RankError) -> JSONResponse: + return JSONResponse(error.payload(), status_code=error.status) + + +def create_app( + settings: Settings | None = None, + refiner: Refiner | None = None, + github_transport: httpx.AsyncBaseTransport | None = None, + board: Leaderboard | None = None, +) -> FastAPI: + settings = settings or load_settings() + telemetry = Telemetry(excluded=preset_digests(settings.presets_path)) + service = FixService(settings, refiner or make_refiner(settings), telemetry) + pulls = PullRequests(settings, telemetry, github_transport) + board = board or Leaderboard(settings, github_transport) + dataset = (settings.stats_dataset_repo, settings.hf_token) + synced = ( + SyncedFile(StatsStore(settings.stats_path, *dataset), telemetry), + SyncedFile(StatsStore(settings.leaderboard_path, *dataset, filename=BOARD_FILE), board), + ) + scan_limit = RateLimiter(SCAN_BATCHES_PER_MINUTE) + scan_limit_all = RateLimiter(SCAN_BATCHES_PER_MINUTE_ALL) + rank_limit = RateLimiter(RANKS_PER_MINUTE) + rank_limit_all = RateLimiter(RANKS_PER_MINUTE_ALL) + + async def sync_files() -> None: + while True: + await asyncio.sleep(settings.stats_sync_seconds) + for file in synced: + await file.flush() + + @asynccontextmanager + async def lifespan(_: FastAPI) -> AsyncIterator[None]: + for file in synced: + await file.load() + tasks = [asyncio.create_task(sync_files())] + if settings.backend == "llama" or settings.warmup: + tasks.append(asyncio.create_task(warm_up())) + if settings.leaderboard_seed: + tasks.append(asyncio.create_task(board.maintain(SEED_REPOS, settings.leaderboard_seed_pause))) + yield + for task in tasks: + task.cancel() + service.close() + await pulls.close() + await board.close() + for file in synced: + await file.flush() + + async def warm_up() -> None: + try: + if settings.warmup: + await service.warm_presets(settings.presets_path) + else: + await asyncio.get_running_loop().run_in_executor(None, service.refiner.load) + except Exception: + log.exception("model load or warm-up failed") + + app = FastAPI(title="codehygiene", lifespan=lifespan) + app.state.service = service + app.state.telemetry = telemetry + app.state.pulls = pulls + app.state.board = board + app.add_middleware(CORSMiddleware, allow_origins=["*"], allow_methods=["*"], allow_headers=["*"]) + + def producer_for(body: FixBody, paced: bool) -> Producer: + request = body.to_request() + cached = None if body.no_cache else service.cached(request, body.source_kind) + if cached is not None: + return lambda emit: service.replay(cached, emit, paced=paced) + if not service.queue.has_room: + raise HTTPException(status_code=429, detail=QUEUE_FULL_MESSAGE) + + async def produce(emit: Emit) -> None: + await service.execute(request, service.queue.join(), emit, source_kind=body.source_kind) + + return produce + + @app.get("/api/health") + def health() -> dict[str, Any]: + return { + "status": "ok", + "model_loaded": service.refiner.loaded, + "model_id": service.refiner.model_id, + "backend": settings.backend, + "max_file_chars": settings.max_file_chars, + "github_pr_enabled": pulls.enabled, + } + + @app.get("/api/stats") + def stats() -> JSONResponse: + body = { + **telemetry.public(), + "leaderboard_entries": len(board.current()), + "scorer_version": SCORER_VERSION, + "github_pr_enabled": pulls.enabled, + } + return JSONResponse(body, headers=STATS_HEADERS) + + @app.get("/api/leaderboard") + def leaderboard() -> JSONResponse: + return JSONResponse(board.public(), headers=STATS_HEADERS) + + @app.post("/api/leaderboard/rank", response_model=None) + async def leaderboard_rank(body: RankBody, request: Request) -> dict[str, Any] | JSONResponse: + if not (rank_limit.allow(caller_of(request)) and rank_limit_all.allow("*")): + return error_response(RankError(429, "busy", "Too many rank requests. Try again shortly.")) + try: + return await board.rank(body.owner, body.repo) + except RankError as error: + return error_response(error) + except httpx.HTTPError as error: + log.warning("GitHub archive request failed: %s", type(error).__name__) + return error_response(RankError(502, "github_error", "GitHub could not be reached.")) + + @app.post("/api/telemetry/scan") + def telemetry_scan(body: ScanBody, request: Request) -> dict[str, int]: + allowed = scan_limit.allow(caller_of(request)) and scan_limit_all.allow("*") + if not allowed: + raise HTTPException(status_code=429, detail="Too many scan reports.") + return {"counted": telemetry.record_scan(body)} + + def require_pulls() -> None: + if not pulls.enabled: + raise HTTPException(status_code=404, detail="Pull requests are not enabled.") + + @app.get("/api/github/login", response_model=None) + def github_login() -> RedirectResponse | JSONResponse: + require_pulls() + try: + return RedirectResponse(pulls.login_url(), status_code=302, headers=CALLBACK_HEADERS) + except PrError as error: + return error_response(error) + + @app.get("/api/github/callback") + async def github_callback(code: str = "", state: str = "") -> HTMLResponse: + require_pulls() + try: + ticket = await pulls.complete_login(code, state) + except PrError as error: + return callback_page(error.message, {"error": error.code}, error.status) + except httpx.HTTPError as error: + log.warning("GitHub sign-in failed: %s", type(error).__name__) + return callback_page("GitHub could not be reached.", {"error": "github_error"}, 502) + return callback_page(SIGNED_IN, {"ticket": ticket}) + + @app.post("/api/github/create-pr", response_model=None) + async def github_create_pr(body: CreatePrBody) -> dict[str, Any] | JSONResponse: + require_pulls() + try: + return await pulls.create(body) + except PrError as error: + return error_response(error) + except httpx.HTTPError as error: + log.warning("GitHub request failed: %s", type(error).__name__) + return error_response(PrError(502, "github_error", "GitHub could not be reached.")) + + @app.post("/api/github/webhook", response_model=None) + async def github_webhook(request: Request) -> dict[str, Any] | JSONResponse: + if not settings.github_webhook_secret: + raise HTTPException(status_code=404, detail="Webhooks are not enabled.") + body = await request.body() + try: + return pulls.webhook( + request.headers.get("x-github-event", ""), + body, + request.headers.get("x-hub-signature-256"), + ) + except PrError as error: + return error_response(error) + + @app.post("/api/score") + def score(body: ScoreBody) -> dict[str, Any]: + return score_dict(body.content, body.threshold) + + @app.post("/api/fix") + async def fix(body: FixBody) -> dict[str, Any]: + collected: dict[str, dict[str, Any]] = {} + produce = producer_for(body, paced=False) + await guarded(produce, lambda name, data: collected.__setitem__(name, data)) + if "done" in collected: + return {key: value for key, value in collected["done"].items() if key not in STREAM_ONLY_KEYS} + failure = collected.get("error", {"code": "cancelled", "message": "The fix was cancelled."}) + raise HTTPException(status_code=ERROR_STATUS.get(failure["code"], 500), detail=failure) + + @app.post("/api/fix/stream") + async def fix_stream(body: FixBody) -> StreamingResponse: + produce = producer_for(body, paced=True) + return StreamingResponse(sse_events(produce), media_type="text/event-stream", headers=SSE_HEADERS) + + if (settings.web_dist / "index.html").is_file(): + app.mount("/", StaticFiles(directory=settings.web_dist, html=True), name="web") + else: + + @app.get("/", response_class=PlainTextResponse) + def root_note() -> str: + return ROOT_NOTE + + return app + + +app = create_app() diff --git a/server/backends.py b/server/backends.py new file mode 100644 index 0000000000000000000000000000000000000000..fcf00279d00e069eabe0c044dfc0697fad31d339 --- /dev/null +++ b/server/backends.py @@ -0,0 +1,205 @@ +from __future__ import annotations + +import json +import logging +import re +import threading +import time +from collections.abc import Callable, Iterator, Sequence +from pathlib import Path +from typing import Any, Protocol + +from .config import Settings +from .prompt import content_key + +log = logging.getLogger("space.refiner") + +STOP = "<|im_end|>" +PREFILL_CHUNK = 128 +MOCK_PROGRESS_STEP = 0.2 +_PIECE_RE = re.compile(r" ?\w{1,6}| ?[^\w\s]{1,3}|\s+") + +Progress = Callable[[int, int], None] + + +class Refiner(Protocol): + model_id: str + + @property + def loaded(self) -> bool: ... + + def load(self) -> None: ... + + def prompt_fits(self, prompt: str) -> bool: ... + + def stream( + self, + prompt: str, + content: str, + temperature: float, + top_p: float, + cancel: threading.Event, + on_progress: Progress, + ) -> Iterator[str]: ... + + +def shared_prefix(left: Sequence[int], right: Sequence[int]) -> int: + count = 0 + for left_token, right_token in zip(left, right): + if left_token != right_token: + break + count += 1 + return count + + +class LlamaRefiner: + def __init__(self, settings: Settings) -> None: + self.model_id = settings.model_id + self._model_path = settings.model_path + self._n_ctx = settings.n_ctx + self._n_threads = settings.n_threads + self._max_new_tokens = settings.max_new_tokens + self._llm: Any = None + self._load_lock = threading.Lock() + + @property + def loaded(self) -> bool: + return self._llm is not None + + def load(self) -> None: + with self._load_lock: + if self._llm is not None: + return + if not Path(self._model_path).is_file(): + raise FileNotFoundError(f"GGUF not found at REFINER_MODEL_PATH={self._model_path!r}") + from llama_cpp import Llama + + self._llm = Llama( + model_path=self._model_path, + n_ctx=self._n_ctx, + n_threads=self._n_threads, + verbose=False, + ) + + def _tokenize(self, prompt: str) -> list[int]: + return self._llm.tokenize(prompt.encode("utf-8"), add_bos=False, special=True) + + def prompt_fits(self, prompt: str) -> bool: + return len(self._tokenize(prompt)) <= self._n_ctx - self._max_new_tokens + + def _prefill(self, tokens: list[int], cancel: threading.Event, on_progress: Progress) -> None: + """Evaluate all but the last prompt token so create_completion reuses them by prefix match.""" + llm = self._llm + total = len(tokens) + end = total - 1 + cached = list(llm.input_ids[: llm.n_tokens]) + reuse = min(shared_prefix(cached, tokens), end) + if reuse == 0: + llm.reset() + else: + llm.n_tokens = reuse + on_progress(reuse, total) + for start in range(reuse, end, PREFILL_CHUNK): + if cancel.is_set(): + return + stop = min(start + PREFILL_CHUNK, end) + llm.eval(tokens[start:stop]) + on_progress(stop, total) + on_progress(total, total) + + def stream( + self, + prompt: str, + content: str, + temperature: float, + top_p: float, + cancel: threading.Event, + on_progress: Progress, + ) -> Iterator[str]: + tokens = self._tokenize(prompt) + completion_prompt: str | list[int] = tokens + try: + self._prefill(tokens, cancel, on_progress) + except Exception: + log.exception("chunked prompt evaluation failed; using plain create_completion") + self._llm.reset() + completion_prompt = prompt + if cancel.is_set(): + return + chunks = self._llm.create_completion( + prompt=completion_prompt, + stream=True, + max_tokens=self._max_new_tokens, + temperature=temperature, + top_p=top_p, + stop=[STOP], + ) + try: + for chunk in chunks: + if cancel.is_set(): + return + text = chunk["choices"][0]["text"] + if text: + yield text + finally: + chunks.close() + + +def split_pieces(text: str) -> list[str]: + return _PIECE_RE.findall(text) + + +class MockRefiner: + def __init__(self, settings: Settings) -> None: + self.model_id = settings.model_id or "mock" + self._prompt_delay = settings.mock_prompt_delay + self._interval = 1.0 / settings.mock_pieces_per_second if settings.mock_pieces_per_second > 0 else 0.0 + self._prompt_budget = settings.n_ctx - settings.max_new_tokens + fixtures = json.loads(Path(settings.fixtures_path).read_text(encoding="utf-8"))["fixtures"] + self._replies = {fixture["key"]: fixture["reply"] for fixture in fixtures} + + @property + def loaded(self) -> bool: + return True + + def load(self) -> None: + return None + + def prompt_tokens(self, prompt: str) -> int: + return len(prompt) // 3 + + def prompt_fits(self, prompt: str) -> bool: + return self.prompt_tokens(prompt) <= self._prompt_budget + + def _read_prompt(self, total: int, cancel: threading.Event, on_progress: Progress) -> bool: + started = time.monotonic() + while (elapsed := time.monotonic() - started) < self._prompt_delay: + on_progress(int(total * elapsed / self._prompt_delay), total) + if cancel.wait(min(MOCK_PROGRESS_STEP, self._prompt_delay - elapsed)): + return False + on_progress(total, total) + return True + + def stream( + self, + prompt: str, + content: str, + temperature: float, + top_p: float, + cancel: threading.Event, + on_progress: Progress, + ) -> Iterator[str]: + if not self._read_prompt(self.prompt_tokens(prompt), cancel, on_progress): + return + started = time.monotonic() + for index, piece in enumerate(split_pieces(self._replies.get(content_key(content), ""))): + ahead = started + index * self._interval - time.monotonic() + if cancel.wait(max(0.0, ahead)): + return + yield piece + + +def make_refiner(settings: Settings) -> Refiner: + if settings.backend == "mock": + return MockRefiner(settings) + return LlamaRefiner(settings) diff --git a/server/config.py b/server/config.py new file mode 100644 index 0000000000000000000000000000000000000000..ffa44f106f07e2b1536be55abbbb0d0853c6d703 --- /dev/null +++ b/server/config.py @@ -0,0 +1,140 @@ +from __future__ import annotations + +import os +from dataclasses import dataclass, field +from pathlib import Path + +SPACE_DIR = Path(__file__).resolve().parents[1] +REPO_ROOT = SPACE_DIR.parent + + +def _env_float(env: dict[str, str], name: str, default: float) -> float: + raw = env.get(name, "").strip() + return float(raw) if raw else default + + +def _env_int(env: dict[str, str], name: str, default: int) -> int: + raw = env.get(name, "").strip() + return int(raw) if raw else default + + +def _env_flag(env: dict[str, str], name: str, default: bool) -> bool: + raw = env.get(name, "").strip().lower() + if not raw: + return default + return raw in ("1", "true", "yes", "on") + + +def _first_existing(*candidates: Path) -> Path | None: + return next((path for path in candidates if path.is_file()), None) + + +@dataclass(frozen=True) +class Settings: + backend: str = "llama" + model_path: str = "" + model_id: str = "" + n_ctx: int = 8192 + n_threads: int = 2 + max_new_tokens: int = 1280 + max_file_chars: int = 12_000 + + fixtures_path: Path = SPACE_DIR / "server" / "fixtures" / "mock_patches.json" + mock_prompt_delay: float = 1.2 + mock_pieces_per_second: float = 60.0 + replay_tokens_per_second: float = 120.0 + + warmup: bool = True + presets_path: Path | None = None + web_dist: Path = SPACE_DIR / "web" / "dist" + max_waiters: int = 5 + cache_size: int = 128 + + stats_path: Path = Path("/tmp/codehygiene_stats.json") + stats_dataset_repo: str = "" + hf_token: str = field(default="", repr=False) + stats_sync_seconds: float = 300.0 + allow_mock_telemetry: bool = False + + github_app_id: str = "" + github_app_client_id: str = "" + github_app_slug: str = "" + github_app_client_secret: str = field(default="", repr=False) + github_app_private_key: str = field(default="", repr=False) + github_webhook_secret: str = field(default="", repr=False) + + github_token: str = field(default="", repr=False) + leaderboard_path: Path = Path("/tmp/codehygiene_leaderboard.json") + leaderboard_seed: bool = False + leaderboard_seed_pause: float = 20.0 + + @property + def counts_fixes(self) -> bool: + return self.backend != "mock" or self.allow_mock_telemetry + + @property + def github_pr_enabled(self) -> bool: + credentials = ( + self.github_app_id, + self.github_app_client_id, + self.github_app_client_secret, + self.github_app_private_key, + self.github_webhook_secret, + ) + return all(credentials) and self.backend != "mock" + + +def _env_text(env: dict[str, str], name: str) -> str: + return env.get(name, "").strip() + + +def load_settings(env: dict[str, str] | None = None) -> Settings: + env = dict(os.environ if env is None else env) + backend = env.get("REFINER_BACKEND", "llama").strip().lower() or "llama" + if backend not in ("llama", "mock"): + raise ValueError(f"REFINER_BACKEND must be 'llama' or 'mock', got {backend!r}") + + repo_id = env.get("REFINER_REPO", "").strip() + file_name = env.get("REFINER_FILE", "").strip() + model_path = env.get("REFINER_MODEL_PATH", "").strip() + default_id = f"{repo_id}/{file_name}" if repo_id and file_name else Path(model_path).name + model_id = env.get("REFINER_MODEL_ID", "").strip() or ("mock" if backend == "mock" else default_id) + + presets_env = env.get("PRESETS_PATH", "").strip() + presets_path = _first_existing( + *([Path(presets_env)] if presets_env else []), + SPACE_DIR / "web" / "vendor" / "presets.json", + REPO_ROOT / "docs" / "presets.json", + ) + defaults = Settings() + return Settings( + backend=backend, + model_path=model_path, + model_id=model_id, + n_threads=_env_int(env, "REFINER_THREADS", defaults.n_threads), + max_file_chars=_env_int(env, "MAX_FILE_CHARS", defaults.max_file_chars), + fixtures_path=Path(env.get("MOCK_FIXTURES", "").strip() or defaults.fixtures_path), + mock_prompt_delay=_env_float(env, "MOCK_PROMPT_DELAY", defaults.mock_prompt_delay), + mock_pieces_per_second=_env_float(env, "MOCK_PIECES_PER_SECOND", defaults.mock_pieces_per_second), + replay_tokens_per_second=_env_float( + env, "REPLAY_TOKENS_PER_SECOND", defaults.replay_tokens_per_second + ), + warmup=_env_flag(env, "WARMUP", backend == "llama"), + presets_path=presets_path, + web_dist=Path(env.get("WEB_DIST", "").strip() or defaults.web_dist), + stats_path=Path(_env_text(env, "STATS_PATH") or defaults.stats_path), + stats_dataset_repo=_env_text(env, "HF_STATS_DATASET_REPO"), + hf_token=_env_text(env, "HF_TOKEN"), + stats_sync_seconds=_env_float(env, "STATS_SYNC_SECONDS", defaults.stats_sync_seconds), + allow_mock_telemetry=_env_flag(env, "ALLOW_MOCK_TELEMETRY", False), + github_app_id=_env_text(env, "GITHUB_APP_ID"), + github_app_client_id=_env_text(env, "GITHUB_APP_CLIENT_ID"), + github_app_slug=_env_text(env, "GITHUB_APP_SLUG"), + github_app_client_secret=_env_text(env, "GITHUB_APP_CLIENT_SECRET"), + github_app_private_key=_env_text(env, "GITHUB_APP_PRIVATE_KEY").replace("\\n", "\n"), + github_webhook_secret=_env_text(env, "GITHUB_WEBHOOK_SECRET"), + github_token=_env_text(env, "GITHUB_TOKEN"), + leaderboard_path=Path(_env_text(env, "LEADERBOARD_PATH") or defaults.leaderboard_path), + leaderboard_seed=_env_flag(env, "LEADERBOARD_SEED", backend == "llama"), + leaderboard_seed_pause=_env_float(env, "LEADERBOARD_SEED_PAUSE", defaults.leaderboard_seed_pause), + ) diff --git a/server/fix_loop.py b/server/fix_loop.py new file mode 100644 index 0000000000000000000000000000000000000000..7b81c109ce5aa4fb1160e75afe9c0d5b47c83ae0 --- /dev/null +++ b/server/fix_loop.py @@ -0,0 +1,246 @@ +from __future__ import annotations + +import threading +import time +from collections.abc import Callable +from dataclasses import dataclass +from typing import Any + +from .backends import Refiner +from .guards import Verdict, verify_patch +from .hygiene import score_dict +from .prompt import build_prompt +from .vendored_pipeline import apply_changes + +Emit = Callable[[str, dict[str, Any]], None] + +GREEDY = (0.0, 1.0) +SAMPLED = (0.45, 0.92) +PREVIEW_INTERVAL = 1.0 / 12 +PROGRESS_INTERVAL = 1.0 / 5 +CLOSE_BLOCK = "\n>>>>>>> REPLACE" +_OPEN_MARKER = "<<<<<<< SEARCH" +_PARTIAL_MARKERS = ("=======", ">>>>>>> REPLACE") + + +class Cancelled(Exception): + pass + + +class RefinerUnavailable(Exception): + pass + + +@dataclass(frozen=True) +class FixRequest: + filename: str + content: str + threshold: float = 0.55 + max_passes: int = 2 + + +def preview_patch_text(raw: str) -> str: + open_at = raw.rfind(_OPEN_MARKER) + if open_at == -1 or ">>>>>>> REPLACE" in raw[open_at:]: + return raw + text = raw[:-1] if raw.endswith("\n") else raw + head, newline, tail = text.rpartition("\n") + if newline and tail != "=======" and any(marker.startswith(tail) for marker in _PARTIAL_MARKERS): + text = head + return text + CLOSE_BLOCK + + +def preview_content(base: str, raw: str) -> tuple[str, int] | None: + try: + content, applied = apply_changes(base, preview_patch_text(raw)) + except ValueError: + return None + if applied == 0: + return None + return content, applied + + +class _Attempt: + def __init__(self, emit: Emit, cancel: threading.Event, base: str) -> None: + self.emit = emit + self.cancel = cancel + self.base = base + self.raw = "" + self.tokens = 0 + self.seconds = 0.0 + self._last_preview_at = 0.0 + self._last_preview = base + self._last_progress_at = -PROGRESS_INTERVAL + self._prompt_total: int | None = None + self._read_reported = False + + def progress(self, done: int, total: int) -> None: + if self.tokens or self._read_reported: + return + self._prompt_total = total + finished = done >= total + if not finished and time.monotonic() - self._last_progress_at < PROGRESS_INTERVAL: + return + self._last_progress_at = time.monotonic() + self._read_reported = finished + self.emit("progress", {"phase": "read", "done": min(done, total), "total": total}) + + def add(self, piece: str) -> None: + if not self.tokens and self._prompt_total is not None: + self.progress(self._prompt_total, self._prompt_total) + self.raw += piece + self.tokens += 1 + self.emit("token", {"text": piece}) + if time.monotonic() - self._last_preview_at >= PREVIEW_INTERVAL: + self._preview() + + def flush(self) -> None: + wait = PREVIEW_INTERVAL - (time.monotonic() - self._last_preview_at) + if wait > 0 and self.cancel.wait(wait): + return + self._preview() + + def _preview(self) -> None: + self._last_preview_at = time.monotonic() + found = preview_content(self.base, self.raw) + if found is None or found[0] == self._last_preview: + return + self._last_preview, applied = found + self.emit("preview", {"content": self._last_preview, "applied_blocks": applied}) + + +class FixRun: + def __init__( + self, + refiner: Refiner, + request: FixRequest, + cancel: threading.Event, + emit: Emit, + max_file_chars: int, + ) -> None: + self.refiner = refiner + self.request = request + self.cancel = cancel + self.emit = emit + self.max_file_chars = max_file_chars + self.attempts = 0 + self.tokens = 0 + self.generation_seconds = 0.0 + + def run(self) -> dict[str, Any]: + started = time.monotonic() + request = self.request + before = score_dict(request.content, request.threshold) + self.emit("start", {"before_hygiene": before, "model_id": self.refiner.model_id, "cached": False}) + outcome = self._outcome(before) + done = self._done_payload(before, outcome, started) + self.emit("done", done) + return done + + def _outcome(self, before: dict[str, Any]) -> dict[str, Any]: + request = self.request + outcome: dict[str, Any] = {"accepted": [], "reason": None, "passes": 0} + if not before["squiggles"]: + return outcome + if len(request.content) > self.max_file_chars: + outcome["reason"] = "too_long" + return outcome + self._load_refiner() + + first = self._attempt(1, request.content, before, sampled=False) + outcome["passes"] = 1 + if first.accepted: + outcome["accepted"].append(first) + if request.max_passes < 2: + outcome["reason"] = first.reason + return outcome + + if first.accepted and first.squiggles_after: + outcome["passes"] = 2 + second = self._attempt(2, first.content, first.hygiene, sampled=False) + if second.accepted: + outcome["accepted"].append(second) + elif not first.accepted: + outcome["passes"] = 2 + outcome["reason"], best = self._sampled_retry(before) + if best is not None: + outcome["accepted"].append(best) + if not outcome["accepted"] and outcome["reason"] is None: + outcome["reason"] = first.reason + return outcome + + def _sampled_retry(self, before: dict[str, Any]) -> tuple[str | None, Verdict | None]: + tries = [self._attempt(2, self.request.content, before, sampled=True) for _ in range(2)] + valid = [verdict for verdict in tries if verdict.accepted] + if not valid: + return tries[-1].reason, None + best = min(valid, key=lambda verdict: (verdict.squiggles_after, -(verdict.score_after or 0.0))) + return None, best + + def _load_refiner(self) -> None: + try: + self.refiner.load() + except Exception as exc: + raise RefinerUnavailable(f"{type(exc).__name__}: {exc}") from exc + + def _attempt(self, pass_no: int, base: str, hygiene: dict[str, Any], sampled: bool) -> Verdict: + if self.cancel.is_set(): + raise Cancelled() + self.attempts += 1 + number = self.attempts + self.emit("attempt", {"n": number, "pass": pass_no, "sampled": sampled}) + prompt = build_prompt(base, hygiene, self.request.filename) + if self.refiner.prompt_fits(prompt): + reply = self._generate(prompt, base, sampled) + verdict = verify_patch(base, reply, self.request.filename, self.request.threshold, hygiene) + verdict.reply = reply + else: + verdict = Verdict(False, "too_long", base, squiggles_before=len(hygiene["squiggles"])) + self.emit("verdict", { + "n": number, + "accepted": verdict.accepted, + "reason": verdict.reason, + "squiggles_before": verdict.squiggles_before, + "squiggles_after": verdict.squiggles_after, + "score_after": verdict.score_after, + "applied_blocks": verdict.applied_blocks, + }) + return verdict + + def _generate(self, prompt: str, base: str, sampled: bool) -> str: + temperature, top_p = SAMPLED if sampled else GREEDY + attempt = _Attempt(self.emit, self.cancel, base) + first_token_at: float | None = None + pieces = self.refiner.stream(prompt, base, temperature, top_p, self.cancel, attempt.progress) + for piece in pieces: + first_token_at = first_token_at or time.monotonic() + attempt.add(piece) + if self.cancel.is_set(): + raise Cancelled() + if first_token_at is not None: + self.generation_seconds += time.monotonic() - first_token_at + attempt.flush() + self.tokens += attempt.tokens + return attempt.raw + + def _done_payload( + self, before: dict[str, Any], outcome: dict[str, Any], started: float + ) -> dict[str, Any]: + accepted: list[Verdict] = outcome["accepted"] + final = accepted[-1] if accepted else None + rate = self.tokens / self.generation_seconds if self.generation_seconds > 0 else 0.0 + return { + "filename": self.request.filename, + "changes": "\n\n".join(verdict.reply for verdict in accepted), + "applied_blocks": sum(verdict.applied_blocks for verdict in accepted), + "updated_content": final.content if final else self.request.content, + "before_hygiene": before, + "after_hygiene": final.hygiene if final else before, + "passes_used": outcome["passes"], + "latency_ms": round((time.monotonic() - started) * 1000), + "accepted": final is not None, + "reason": None if final else outcome["reason"], + "cached": False, + "tokens": self.tokens, + "tokens_per_second": round(rate, 1), + } diff --git a/server/fix_queue.py b/server/fix_queue.py new file mode 100644 index 0000000000000000000000000000000000000000..69f42a430d088cc34152b930ba77cf2c3d7609ff --- /dev/null +++ b/server/fix_queue.py @@ -0,0 +1,89 @@ +from __future__ import annotations + +import asyncio +import threading +from collections.abc import Callable +from dataclasses import dataclass, field + + +class QueueFull(Exception): + pass + + +@dataclass(eq=False) +class Ticket: + background: bool = False + cancel: threading.Event = field(default_factory=threading.Event) + + +class FixQueue: + """One running fix at a time; visitors wait in order ahead of background warm-up jobs.""" + + def __init__(self, max_waiters: int) -> None: + self.max_waiters = max_waiters + self._waiting: list[Ticket] = [] + self._busy: Ticket | None = None + self._changed = asyncio.Event() + + @property + def idle(self) -> bool: + return self._busy is None and not self._waiting + + @property + def visitors_waiting(self) -> int: + return sum(1 for ticket in self._waiting if not ticket.background) + + @property + def has_room(self) -> bool: + return self._busy is None or self.visitors_waiting < self.max_waiters + + def join(self, background: bool = False) -> Ticket: + if not background and not self.has_room: + raise QueueFull() + ticket = Ticket(background=background) + if background: + self._waiting.append(ticket) + else: + first_background = next( + (idx for idx, queued in enumerate(self._waiting) if queued.background), + len(self._waiting), + ) + self._waiting.insert(first_background, ticket) + if self._busy is not None and self._busy.background: + self._busy.cancel.set() + self._notify() + return ticket + + async def wait_turn(self, ticket: Ticket, on_position: Callable[[int], None]) -> None: + last_position = -1 + while True: + changed = self._changed + if self._busy is None and self._waiting and self._waiting[0] is ticket: + self._waiting.pop(0) + self._busy = ticket + self._notify() + return + position = self._waiting.index(ticket) + (self._busy is not None) + if position != last_position: + on_position(position) + last_position = position + await changed.wait() + + async def wait_idle(self) -> None: + while not self.idle: + await self._changed.wait() + + def release(self, ticket: Ticket) -> None: + if self._busy is ticket: + self._busy = None + elif ticket in self._waiting: + self._waiting.remove(ticket) + self._notify() + + def cancel_all(self) -> None: + for ticket in [*self._waiting, *([self._busy] if self._busy else [])]: + ticket.cancel.set() + + def _notify(self) -> None: + self._changed.set() + self._changed = asyncio.Event() diff --git a/server/fixtures/mock_patches.json b/server/fixtures/mock_patches.json new file mode 100644 index 0000000000000000000000000000000000000000..db99239870815f3c4887904f79d341dea425916b --- /dev/null +++ b/server/fixtures/mock_patches.json @@ -0,0 +1,103 @@ +{ + "fixtures": [ + { + "key": "603901a66c358a271f8bea8305951afa99fa1fec4581a303fbd3d3cd392e8ab0", + "filename": "send_receipt.py", + "pass": 1, + "source": "handwritten", + "origin": "docs/presets.json before", + "squiggles_before": 3, + "squiggles_after": 0, + "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", + "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" + }, + { + "key": "fb26a694b102dfe43772450032b23dd08ecc96f56672ea4be0f7bad66322a32d", + "filename": "pricing.ts", + "pass": 1, + "source": "handwritten", + "origin": "docs/presets.json before", + "squiggles_before": 4, + "squiggles_after": 2, + "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}", + "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" + }, + { + "key": "1b7c59b3968bb5426ae5916be0eea950aa72d7a36f610efe3d3d555c064c02ff", + "filename": "frame.rs", + "pass": 1, + "source": "handwritten", + "origin": "docs/presets.json before", + "squiggles_before": 12, + "squiggles_after": 10, + "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}", + "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" + }, + { + "key": "694945a5f10bdd90dec848e6a12a5b0e6b97158da7ffcf7f6db8a3b9273bd13a", + "filename": "frame.rs", + "pass": 2, + "source": "handwritten", + "origin": "pass-1 output", + "squiggles_before": 10, + "squiggles_after": 8, + "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}", + "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" + }, + { + "key": "0e8168b89bab1d90bc459c5daaeaffcd14c49fd368aff7099136e17a4d460ad0", + "filename": "webhook.go", + "pass": 1, + "source": "handwritten", + "origin": "docs/presets.json before", + "squiggles_before": 7, + "squiggles_after": 3, + "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}", + "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" + }, + { + "key": "def4e6ca446326c5e5e83b5127daff28b28d01d51ecd3b2576bd4e7b3293e5a4", + "filename": "webhook.go", + "pass": 2, + "source": "handwritten", + "origin": "pass-1 output", + "squiggles_before": 3, + "squiggles_after": 0, + "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}", + "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" + }, + { + "key": "e06eb41fc26eac74527f6145b19c1796c95d167092e4be3202d71ae449601e02", + "filename": "engine.js", + "pass": 1, + "source": "handwritten", + "origin": "docs/presets.json before", + "squiggles_before": 7, + "squiggles_after": 5, + "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}", + "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" + }, + { + "key": "5026dc2109a6c6eeceb284475e808c2dc1ff30327232235e4296a4fe404b5ce8", + "filename": "engine.js", + "pass": 2, + "source": "handwritten", + "origin": "pass-1 output", + "squiggles_before": 5, + "squiggles_after": 0, + "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}", + "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" + }, + { + "key": "924c910f843823ebb2889d773d89212f28bb465f7197f360417cac41e8cd2e8e", + "filename": "index.js", + "pass": 1, + "source": "handwritten", + "origin": "github:lukeed/clsx@925494cf31bcd97d3337aacd34e659e80cae7fe2:src/index.js", + "squiggles_before": 2, + "squiggles_after": 0, + "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", + "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" + } + ] +} diff --git a/server/github_pr.py b/server/github_pr.py new file mode 100644 index 0000000000000000000000000000000000000000..bca3f2c39d36725e29cda234756ebd23fece0297 --- /dev/null +++ b/server/github_pr.py @@ -0,0 +1,375 @@ +"""Single-repo pull requests through a GitHub App, plus signed webhook merge tracking. + +The visitor authorizes the App in a pop-up. The callback page hands a one-time ticket to the +CodeHygiene page that opened it (`postMessage` restricted to this origin), so a sign-in link +sent by someone else can never be redeemed by them. The user token lives only in this +process's memory, is used for one pull request on a repository the visitor can push to, and +is then revoked. +""" + +from __future__ import annotations + +import asyncio +import hashlib +import hmac +import json +import logging +import secrets +import time +from dataclasses import dataclass +from typing import Any +from urllib.parse import quote, urlencode + +import httpx +import jwt +from pydantic import BaseModel, ConfigDict, Field + +from .config import Settings +from .hygiene import score_dict +from .telemetry import Telemetry, pr_key + +log = logging.getLogger("space.github") + +API_URL = "https://api.github.com" +AUTHORIZE_URL = "https://github.com/login/oauth/authorize" +TOKEN_URL = "https://github.com/login/oauth/access_token" +PROJECT_URL = "https://github.com/brianbell-x/codehygiene" +STATE_TTL = 600.0 +TICKET_TTL = 180.0 +MAX_PENDING = 256 +MAX_PR_FILES = 20 +THRESHOLD = 0.55 +NAME_PATTERN = r"^[A-Za-z0-9_.-]{1,100}$" +TICKET_PATTERN = r"^[0-9a-f]{32}$" +DEFAULT_MODE = "100644" +BLOB_MODES = frozenset({DEFAULT_MODE, "100755"}) + + +class PrError(Exception): + def __init__(self, status: int, code: str, message: str, **extra: Any) -> None: + super().__init__(message) + self.status = status + self.code = code + self.message = message + self.extra = extra + + def payload(self) -> dict[str, Any]: + return {"code": self.code, "message": self.message, **self.extra} + + +class PrFile(BaseModel): + model_config = ConfigDict(extra="forbid") + + path: str = Field(min_length=1, max_length=400) + content_after: str = Field(max_length=200_000) + score_before: int = Field(ge=0, le=100) + score_after: int = Field(ge=0, le=100) + squiggles_fixed: int = Field(ge=0, le=10_000) + + +class CreatePrBody(BaseModel): + model_config = ConfigDict(extra="forbid") + + ticket: str = Field(pattern=TICKET_PATTERN) + owner: str = Field(pattern=NAME_PATTERN) + repo: str = Field(pattern=NAME_PATTERN) + base_branch: str = Field("", max_length=255) + base_commit_sha: str = Field(pattern=r"^[0-9a-f]{40}$") + files: list[PrFile] = Field(min_length=1, max_length=MAX_PR_FILES) + + +@dataclass(frozen=True) +class CheckedFile: + path: str + mode: str + content: str + score_before: int + score_after: int + resolved: int + + +def signature_valid(secret: str, body: bytes, header: str | None) -> bool: + if not secret or not header or not header.startswith("sha256="): + return False + expected = "sha256=" + hmac.new(secret.encode(), body, hashlib.sha256).hexdigest() + return hmac.compare_digest(expected, header) + + +def safe_path(path: str) -> bool: + parts = path.split("/") + if path.startswith("/") or "\\" in path or "\0" in path: + return False + return all(part not in ("", ".", "..", ".git") for part in parts) + + +def file_mode(modes: dict[str, str] | None, path: str) -> str | None: + """Git mode of a base file. A truncated tree has no listing; reading the file later proves it exists.""" + if modes is None: + return DEFAULT_MODE + return modes.get(path) + + +def pr_body(files: list[CheckedFile]) -> str: + rows = "\n".join( + f"| `{item.path}` | {item.score_before} → {item.score_after} | {item.resolved} |" for item in files + ) + return ( + f"Readability cleanup suggested by [CodeHygiene]({PROJECT_URL}).\n\n" + "| File | Score (of 100) | Issues resolved |\n" + "| --- | --- | --- |\n" + f"{rows}\n\n" + "Each change was re-scored before this pull request was opened. " + "Behaviour should be unchanged, but run your tests and wait for CI before merging.\n" + ) + + +def pr_title(count: int) -> str: + return "Clean up readability in 1 file" if count == 1 else f"Clean up readability in {count} files" + + +class PullRequests: + def __init__( + self, + settings: Settings, + telemetry: Telemetry, + transport: httpx.AsyncBaseTransport | None = None, + ) -> None: + self.settings = settings + self.telemetry = telemetry + self.http = httpx.AsyncClient( + timeout=20.0, + transport=transport, + headers={ + "Accept": "application/vnd.github+json", + "X-GitHub-Api-Version": "2022-11-28", + "User-Agent": "codehygiene-space", + }, + ) + self._states: dict[str, float] = {} + self._tickets: dict[str, tuple[str, float]] = {} + self._slug = settings.github_app_slug + + @property + def enabled(self) -> bool: + return self.settings.github_pr_enabled + + async def close(self) -> None: + await self.http.aclose() + + def _sweep(self) -> None: + now = time.monotonic() + self._states = {state: expires for state, expires in self._states.items() if expires > now} + self._tickets = {key: entry for key, entry in self._tickets.items() if entry[1] > now} + + def login_url(self) -> str: + self._sweep() + if len(self._states) >= MAX_PENDING: + raise PrError(429, "busy", "Too many sign-ins in progress. Try again shortly.") + state = secrets.token_urlsafe(24) + self._states[state] = time.monotonic() + STATE_TTL + query = urlencode({"client_id": self.settings.github_app_client_id, "state": state}) + return f"{AUTHORIZE_URL}?{query}" + + async def complete_login(self, code: str, state: str) -> str: + """Exchange the code and return a one-time ticket for the page that opened the pop-up.""" + self._sweep() + if self._states.pop(state, None) is None: + raise PrError(400, "expired", "This sign-in expired. Start again from CodeHygiene.") + if len(self._tickets) >= MAX_PENDING: + raise PrError(429, "busy", "Too many sign-ins in progress. Try again shortly.") + return self.issue_ticket(await self._exchange_code(code)) + + def issue_ticket(self, token: str) -> str: + """Hold a user token in memory under a fresh one-time ticket.""" + ticket = secrets.token_hex(16) + self._tickets[ticket] = (token, time.monotonic() + TICKET_TTL) + return ticket + + async def create(self, body: CreatePrBody) -> dict[str, Any]: + self._sweep() + entry = self._tickets.pop(body.ticket, None) + if entry is None: + raise PrError(401, "login_required", "Sign in with GitHub again.") + token = entry[0] + try: + return await self._open(token, body) + finally: + await self._revoke(token) + + async def _exchange_code(self, code: str) -> str: + response = await self.http.post( + TOKEN_URL, + data={ + "client_id": self.settings.github_app_client_id, + "client_secret": self.settings.github_app_client_secret, + "code": code, + }, + headers={"Accept": "application/json"}, + ) + token = response.json().get("access_token") if response.status_code == 200 else None + if not token: + raise PrError(401, "login_failed", "GitHub sign-in failed.") + return token + + async def _revoke(self, token: str) -> None: + """Best effort: the token is dropped from memory either way and expires on GitHub's side.""" + try: + response = await self.http.request( + "DELETE", + f"{API_URL}/applications/{self.settings.github_app_client_id}/token", + auth=(self.settings.github_app_client_id, self.settings.github_app_client_secret), + json={"access_token": token}, + ) + except httpx.HTTPError as error: + log.warning("token revoke failed: %s", type(error).__name__) + return + if response.status_code not in (204, 404): + log.warning("token revoke returned HTTP %d", response.status_code) + + def _app_jwt(self) -> str: + now = int(time.time()) + claims = {"iat": now - 60, "exp": now + 540, "iss": self.settings.github_app_id} + return jwt.encode(claims, self.settings.github_app_private_key, algorithm="RS256") + + async def _call( + self, method: str, path: str, *, token: str | None = None, app: bool = False, **kwargs: Any + ) -> httpx.Response: + bearer = self._app_jwt() if app else token + headers = {**kwargs.pop("headers", {}), "Authorization": f"Bearer {bearer}"} + return await self.http.request(method, f"{API_URL}{path}", headers=headers, **kwargs) + + async def _json(self, method: str, path: str, *, token: str, expect: int = 200, **kwargs: Any) -> Any: + response = await self._call(method, path, token=token, **kwargs) + if response.status_code != expect: + raise PrError(502, "github_error", f"GitHub returned HTTP {response.status_code}.") + return response.json() + + async def _app_slug(self) -> str: + if not self._slug: + response = await self._call("GET", "/app", app=True) + self._slug = response.json().get("slug", "") if response.status_code == 200 else "" + return self._slug + + async def _install_url(self, repo: dict[str, Any] | None) -> str: + url = f"https://github.com/apps/{await self._app_slug()}/installations/new" + if repo is None: + return url + query = urlencode({"suggested_target_id": repo["owner"]["id"], "repository_ids[]": repo["id"]}) + return f"{url}/permissions?{query}" + + async def _installed(self, slug: str) -> bool: + response = await self._call("GET", f"/repos/{slug}/installation", app=True) + return response.status_code == 200 + + async def _open(self, token: str, body: CreatePrBody) -> dict[str, Any]: + slug = f"{body.owner}/{body.repo}" + response = await self._call("GET", f"/repos/{slug}", token=token) + if response.status_code == 404: + if not await self._installed(slug): + raise PrError(409, "not_installed", "Install the app on this repository first.", + install_url=await self._install_url(None)) + raise PrError(404, "not_found", "Repository not found.") + if response.status_code != 200: + raise PrError(502, "github_error", f"GitHub returned HTTP {response.status_code}.") + repo = response.json() + if not repo.get("permissions", {}).get("push"): + raise PrError(403, "no_push", "You need write access to open a pull request here.") + if not await self._installed(slug): + raise PrError(409, "not_installed", "Install the app on this repository first.", + install_url=await self._install_url(repo)) + + base_branch = body.base_branch or repo["default_branch"] + branch_path = f"/repos/{slug}/branches/{quote(base_branch, safe='')}" + branch = await self._call("GET", branch_path, token=token) + if branch.status_code != 200: + raise PrError(422, "bad_base", "The base must be a branch.") + commit = await self._call("GET", f"/repos/{slug}/git/commits/{body.base_commit_sha}", token=token) + if commit.status_code != 200: + raise PrError(422, "bad_base", "The base commit was not found.") + base_tree = commit.json()["tree"]["sha"] + + files = await self._checked_files(token, slug, base_tree, body) + head = await self._commit(token, slug, base_tree, body.base_commit_sha, files) + created = await self._json("POST", f"/repos/{slug}/pulls", token=token, expect=201, json={ + "title": pr_title(len(files)), + "head": head, + "base": base_branch, + "body": pr_body(files), + "maintainer_can_modify": True, + }) + self.telemetry.record_pr_opened(pr_key(repo["id"], created["number"])) + return {"url": created["html_url"], "number": created["number"]} + + async def _modes(self, token: str, slug: str, base_tree: str) -> dict[str, str] | None: + tree = await self._json("GET", f"/repos/{slug}/git/trees/{base_tree}?recursive=1", token=token) + if tree.get("truncated"): + return None + return {node["path"]: node["mode"] for node in tree.get("tree", []) if node.get("type") == "blob"} + + async def _checked_files( + self, token: str, slug: str, base_tree: str, body: CreatePrBody + ) -> list[CheckedFile]: + """Re-read each base file at the commit and re-score both versions; the client's numbers are not used.""" + if len({item.path for item in body.files}) != len(body.files): + raise PrError(422, "bad_path", "Each file may appear once.") + modes = await self._modes(token, slug, base_tree) + checked: list[CheckedFile] = [] + for item in body.files: + mode = file_mode(modes, item.path) + if not safe_path(item.path) or mode not in BLOB_MODES: + raise PrError(422, "bad_path", "A file is not in the base commit.") + raw = await self._call( + "GET", f"/repos/{slug}/contents/{quote(item.path)}?ref={body.base_commit_sha}", + token=token, headers={"Accept": "application/vnd.github.raw"}, + ) + if raw.status_code != 200: + raise PrError(422, "bad_path", "A file is not in the base commit.") + before, after = await asyncio.gather( + asyncio.to_thread(score_dict, raw.text, THRESHOLD), + asyncio.to_thread(score_dict, item.content_after, THRESHOLD), + ) + resolved = len(before["squiggles"]) - len(after["squiggles"]) + if item.content_after == raw.text or resolved <= 0: + raise PrError(422, "not_cleaner", "Every file must have fewer issues than its base.") + checked.append(CheckedFile( + item.path, mode, item.content_after, + round(before["score"] * 100), round(after["score"] * 100), resolved, + )) + return checked + + async def _commit( + self, token: str, slug: str, base_tree: str, base_sha: str, files: list[CheckedFile] + ) -> str: + entries = [] + for item in files: + blob = await self._json("POST", f"/repos/{slug}/git/blobs", token=token, expect=201, + json={"content": item.content, "encoding": "utf-8"}) + entries.append({"path": item.path, "mode": item.mode, "type": "blob", "sha": blob["sha"]}) + tree = await self._json("POST", f"/repos/{slug}/git/trees", token=token, expect=201, + json={"base_tree": base_tree, "tree": entries}) + commit = await self._json("POST", f"/repos/{slug}/git/commits", token=token, expect=201, json={ + "message": pr_title(len(files)), + "tree": tree["sha"], + "parents": [base_sha], + }) + for _ in range(3): + branch = f"codehygiene/{secrets.token_hex(4)}" + ref = await self._call("POST", f"/repos/{slug}/git/refs", token=token, + json={"ref": f"refs/heads/{branch}", "sha": commit["sha"]}) + if ref.status_code == 201: + return branch + if ref.status_code != 422: + break + raise PrError(502, "github_error", "Could not create the branch.") + + def webhook(self, event: str, body: bytes, signature: str | None) -> dict[str, Any]: + if not signature_valid(self.settings.github_webhook_secret, body, signature): + raise PrError(401, "bad_signature", "Invalid signature.") + if event != "pull_request": + return {"counted": False} + payload = json.loads(body) + if payload.get("action") != "closed" or "pull_request" not in payload: + return {"counted": False} + pull = payload["pull_request"] + key = pr_key(payload["repository"]["id"], pull["number"]) + return {"counted": self.telemetry.record_pr_closed(key, merged=bool(pull.get("merged")))} diff --git a/server/guards.py b/server/guards.py new file mode 100644 index 0000000000000000000000000000000000000000..c8a3e66057327ae3b75872c708e2e3169011c6cd --- /dev/null +++ b/server/guards.py @@ -0,0 +1,69 @@ +from __future__ import annotations + +from dataclasses import dataclass, field +from typing import Any + +from .hygiene import score_dict +from .vendored_guards import check_basic_syntax, is_bloated, is_gutted +from .vendored_pipeline import apply_changes + + +@dataclass +class Verdict: + accepted: bool + reason: str | None + content: str + applied_blocks: int = 0 + squiggles_before: int = 0 + squiggles_after: int | None = None + hygiene: dict[str, Any] = field(default_factory=dict) + reply: str = "" + + @property + def score_after(self) -> float | None: + return self.hygiene.get("score") + + +def verify_patch( + content: str, + reply: str, + filename: str, + threshold: float, + before_hygiene: dict[str, Any], +) -> Verdict: + squiggles_before = len(before_hygiene["squiggles"]) + rejected = Verdict(False, None, content, squiggles_before=squiggles_before) + if not reply.strip(): + rejected.reason = "empty_output" + return rejected + try: + updated, applied = apply_changes(content, reply) + except ValueError: + rejected.reason = "search_miss" + return rejected + rejected.applied_blocks = applied + if applied == 0 or updated == content: + rejected.reason = "no_blocks_applied" + return rejected + if filename and not check_basic_syntax(updated, filename): + rejected.reason = "invalid_syntax" + return rejected + if is_gutted(content, updated): + rejected.reason = "gutted_file" + return rejected + if is_bloated(content, updated): + rejected.reason = "line_bloat" + return rejected + + after = score_dict(updated, threshold) + squiggles_after = len(after["squiggles"]) + accepted = squiggles_after < squiggles_before + return Verdict( + accepted=accepted, + reason=None if accepted else "no_improvement", + content=updated if accepted else content, + applied_blocks=applied, + squiggles_before=squiggles_before, + squiggles_after=squiggles_after, + hygiene=after, + ) diff --git a/server/hygiene.py b/server/hygiene.py new file mode 100644 index 0000000000000000000000000000000000000000..f8ea2837cab9680a2667c73b8e268fd27883f7f7 --- /dev/null +++ b/server/hygiene.py @@ -0,0 +1,28 @@ +from __future__ import annotations + +import hashlib +import importlib.util +import sys +from pathlib import Path +from typing import Any + +REPO_ROOT = Path(__file__).resolve().parents[2] +LOCAL_PACKAGE = REPO_ROOT / "codehygiene" / "__init__.py" + +if importlib.util.find_spec("codehygiene") is None and LOCAL_PACKAGE.is_file(): + sys.path.insert(0, str(REPO_ROOT)) + +import codehygiene + +WEIGHTS_PATH = Path(codehygiene.__file__).with_name("weights.npz") +SCORER_VERSION = str(codehygiene.__version__) +# Version plus a weights digest: a recompile that keeps the version string still counts as a new scorer. +SCORER_ID = f"{SCORER_VERSION}+{hashlib.sha256(WEIGHTS_PATH.read_bytes()).hexdigest()[:12]}" + + +def score_dict(content: str, threshold: float) -> dict[str, Any]: + return codehygiene.score(content, threshold=threshold).to_dict() + + +def score_value(content: str) -> float: + return float(codehygiene.score(content).score) diff --git a/server/leaderboard.py b/server/leaderboard.py new file mode 100644 index 0000000000000000000000000000000000000000..164ffbfd12983547dfe3449fbdb44a6c63456033 --- /dev/null +++ b/server/leaderboard.py @@ -0,0 +1,437 @@ +"""Public leaderboard of GitHub repos, each scored on the server. See space/TELEMETRY.md.""" + +from __future__ import annotations + +import asyncio +import hashlib +import io +import json +import logging +import re +import tarfile +import time +from collections.abc import Callable, Iterable +from dataclasses import asdict, dataclass +from datetime import datetime, timezone +from typing import Any +from urllib.parse import quote + +import httpx +from pydantic import BaseModel, ConfigDict, Field, field_validator + +from .config import Settings +from .hygiene import SCORER_ID, SCORER_VERSION, score_value + +log = logging.getLogger("space.leaderboard") + +API = "https://api.github.com" +ARCHIVES = "https://codeload.github.com" +BOARD_FILE = "leaderboard.json" +BOARD_VERSION = 1 +BOARD_LIMIT = 100 + +MIN_FILES = 5 +MIN_LINES = 1_000 +MAX_ARCHIVE_BYTES = 80 * 1024 * 1024 +MAX_FILE_BYTES = 150_000 +MAX_SCORED_FILES = 300 +MAX_SCORED_LINES = 20_000 + +FRESH_SECONDS = 24 * 3600 +SEED_FRESH_SECONDS = 7 * 24 * 3600 +PAST_SECONDS = 365 * 24 * 3600 +MAX_WAITING = 3 +GONE_CODES = frozenset({"not_found", "too_small", "fork", "duplicate"}) + +RANKED_EXTENSIONS = frozenset({"py", "ts", "tsx", "js", "jsx", "mjs", "cjs", "go", "rs"}) +SKIPPED_DIRS = frozenset({ + "test", "tests", "__tests__", "spec", "specs", "e2e", "testdata", "fixtures", "__fixtures__", + "__mocks__", "vendor", "vendored", "third_party", "thirdparty", "node_modules", "dist", + "build", "out", "target", "generated", "__generated__", +}) +SKIPPED_SUFFIXES = (".d.ts", ".pb.go", "_pb2.py", "_test.go", "_test.py", "_generated.go") +SKIPPED_INFIXES = (".test.", ".spec.", ".generated.", ".min.") +GENERATED_MARKS = ( + "code generated", "do not edit", "@generated", "auto-generated", "autogenerated", "file generated", +) +REPO_NAME = re.compile(r"^[A-Za-z0-9][A-Za-z0-9-]{0,38}/[A-Za-z0-9._-]{1,100}$") +COMMIT_SHA = re.compile(r"^[0-9a-f]{40}$") + +SEED_REPOS = ( + "BurntSushi/ripgrep", "honojs/hono", "fastapi/fastapi", "karpathy/nanoGPT", + "openai/codex", "sst/opencode", "pallets/flask", "psf/requests", + "astral-sh/ruff", "junegunn/fzf", "charmbracelet/bubbletea", "gin-gonic/gin", + "colinhacks/zod", "vitejs/vite", "expressjs/express", "tokio-rs/axum", + "tinygrad/tinygrad", "pydantic/pydantic", "sindresorhus/ky", "anthropics/claude-code", +) + +Scorer = Callable[[str], float] + + +class RankError(Exception): + def __init__(self, status: int, code: str, message: str, **extra: Any) -> None: + super().__init__(message) + self.status = status + self.code = code + self.message = message + self.extra = extra + + def payload(self) -> dict[str, Any]: + return {"code": self.code, "message": self.message, **self.extra} + + +class RankBody(BaseModel): + model_config = ConfigDict(extra="forbid") + + owner: str = Field(pattern=r"^[A-Za-z0-9][A-Za-z0-9-]{0,38}$") + repo: str = Field(pattern=r"^[A-Za-z0-9._-]{1,100}$") + + @field_validator("repo") + @classmethod + def not_dots(cls, value: str) -> str: + if not value.strip("."): + raise ValueError("not a repository name") + return value + + +@dataclass(frozen=True) +class Measure: + sha: str + files: int + lines: int + score: float + + +@dataclass(frozen=True) +class Entry: + repo: str + score: float + files: int + lines: int + sha: str + ranked_at: float + past_score: float | None = None + past_sha: str = "" + scorer: str = "" + + @property + def key(self) -> str: + return self.repo.lower() + + @property + def change(self) -> float | None: + return None if self.past_score is None else round(self.score - self.past_score, 1) + + def row(self) -> dict[str, Any]: + return { + "repo": self.repo, + "score": self.score, + "past_score": self.past_score, + "change": self.change, + "files": self.files, + "lines": self.lines, + "sha": self.sha, + } + + +def line_count(text: str) -> int: + return text.count("\n") + (0 if text.endswith("\n") else 1) + + +def ranked_path(path: str) -> bool: + """Source files that count toward a repo's score: no tests, vendored, generated or minified code.""" + parts = path.lower().split("/") + name = parts[-1] + if name.rpartition(".")[2] not in RANKED_EXTENSIONS: + return False + if any(part in SKIPPED_DIRS or part.startswith(".") for part in parts[:-1]): + return False + if name.startswith("test_") or name == "conftest.py" or name.endswith(SKIPPED_SUFFIXES): + return False + return not any(infix in name for infix in SKIPPED_INFIXES) + + +def generated(text: str) -> bool: + head = text[:600].lower() + return any(mark in head for mark in GENERATED_MARKS) + + +def archive_files(archive: bytes) -> tuple[str, list[tuple[str, str]]]: + """Commit SHA and every ranked file of a GitHub tar.gz, read in memory; nothing touches disk.""" + files = [] + with tarfile.open(fileobj=io.BytesIO(archive), mode="r:gz") as tar: + sha = str(tar.pax_headers.get("comment", "")) + for member in tar: + path = member.name.partition("/")[2] + if not member.isfile() or member.size > MAX_FILE_BYTES or not ranked_path(path): + continue + handle = tar.extractfile(member) + text = handle.read().decode("utf-8", errors="replace") if handle else "" + if text.strip() and "�" not in text and not generated(text): + files.append((path, text)) + return (sha if COMMIT_SHA.match(sha) else ""), files + + +def sample(files: list[tuple[str, str]]) -> list[str]: + """A stable subset within the scoring budget, picked by path hash so a rerun scores the same files.""" + ordered = sorted(files, key=lambda item: hashlib.sha256(item[0].encode()).digest()) + picked: list[str] = [] + lines = 0 + for _, text in ordered: + if len(picked) >= MAX_SCORED_FILES or lines >= MAX_SCORED_LINES: + break + picked.append(text) + lines += line_count(text) + return picked + + +def measure(archive: bytes, scorer: Scorer) -> Measure: + """Line-weighted mean file score, 0 to 100, over the sampled files.""" + sha, files = archive_files(archive) + picked = sample(files) + weights = [line_count(text) for text in picked] + weighted = sum(scorer(text) * weight for text, weight in zip(picked, weights)) + score = round(100 * weighted / sum(weights), 1) if picked else 0.0 + total_lines = sum(line_count(text) for _, text in files) + return Measure(sha=sha, files=len(files), lines=total_lines, score=score) + + +def qualifies(files: int, lines: int) -> bool: + return files >= MIN_FILES and lines >= MIN_LINES + + +def iso(stamp: float) -> str: + return datetime.fromtimestamp(stamp, timezone.utc).strftime("%Y-%m-%dT%H:%M:%SZ") + + +def _score(value: Any) -> float | None: + number = value if isinstance(value, (int, float)) and not isinstance(value, bool) else None + return float(number) if number is not None and 0 <= number <= 100 else None + + +def entry_from(raw: Any) -> Entry | None: + """An entry from a saved board, or None when any field is missing or out of range.""" + if not isinstance(raw, dict): + return None + repo, sha, files, lines = raw.get("repo"), raw.get("sha"), raw.get("files"), raw.get("lines") + score, stamp = _score(raw.get("score")), raw.get("ranked_at") + shaped = ( + isinstance(repo, str) and REPO_NAME.match(repo) + and isinstance(sha, str) and COMMIT_SHA.match(sha) + and isinstance(files, int) and isinstance(lines, int) and qualifies(files, lines) + and score is not None and isinstance(stamp, (int, float)) + ) + if not shaped: + return None + past_sha = raw.get("past_sha", "") + past_score = _score(raw.get("past_score")) + has_past = past_score is not None and isinstance(past_sha, str) and COMMIT_SHA.match(past_sha) + scorer = raw.get("scorer") + return Entry( + repo=repo, score=score, files=files, lines=lines, sha=sha, ranked_at=float(stamp), + past_score=past_score if has_past else None, past_sha=past_sha if has_past else "", + scorer=scorer[:64] if isinstance(scorer, str) else "", + ) + + +class Leaderboard: + """Repos ranked by server-side score. Holds only public repo names, commit SHAs and numbers. + + Every entry records the scorer that produced it. Only entries from the running scorer are listed; + older ones stay in the file until `maintain` re-ranks or drops them. + """ + + def __init__( + self, + settings: Settings, + transport: httpx.AsyncBaseTransport | None = None, + scorer: Scorer | None = None, + clock: Callable[[], float] = time.time, + scorer_id: str = SCORER_ID, + ) -> None: + self.entries: dict[str, Entry] = {} + self.dirty = False + self.scorer = scorer or score_value + self.scorer_id = scorer_id + self.clock = clock + self.api_headers = {"Accept": "application/vnd.github+json", "X-GitHub-Api-Version": "2022-11-28"} + if settings.github_token: + self.api_headers["Authorization"] = f"Bearer {settings.github_token}" + self.client = httpx.AsyncClient( + transport=transport, + headers={"User-Agent": "codehygiene-space"}, + timeout=httpx.Timeout(60.0, connect=10.0), + follow_redirects=True, + ) + self._turn = asyncio.Lock() + self._waiting = 0 + + async def close(self) -> None: + await self.client.aclose() + + def merge(self, raw: Any) -> None: + rows = raw.get("entries") if isinstance(raw, dict) else None + for item in rows if isinstance(rows, list) else []: + entry = entry_from(item) + known = self.entries.get(entry.key) if entry else None + if entry and (known is None or entry.ranked_at > known.ranked_at): + self.entries[entry.key] = entry + + def snapshot(self) -> str: + rows = [asdict(entry) for entry in sorted(self.entries.values(), key=lambda entry: entry.key)] + return json.dumps({"version": BOARD_VERSION, "entries": rows}, indent=2, sort_keys=True) + "\n" + + def current(self) -> list[Entry]: + return [entry for entry in self.entries.values() if entry.scorer == self.scorer_id] + + def stale(self) -> list[Entry]: + return [entry for entry in self.entries.values() if entry.scorer != self.scorer_id] + + def cleanest(self) -> list[Entry]: + return sorted(self.current(), key=lambda entry: (-entry.score, entry.key)) + + def improved(self) -> list[Entry]: + rising = [entry for entry in self.current() if (entry.change or 0) >= 1] + return sorted(rising, key=lambda entry: (-(entry.change or 0), entry.key)) + + def public(self) -> dict[str, Any]: + stamps = [entry.ranked_at for entry in self.current()] + return { + "total": len(stamps), + "updated_at": iso(max(stamps)) if stamps else None, + "scorer_version": SCORER_VERSION, + "cleanest": [entry.row() for entry in self.cleanest()[:BOARD_LIMIT]], + "improved": [entry.row() for entry in self.improved()[:BOARD_LIMIT]], + } + + def standing(self, entry: Entry) -> dict[str, Any]: + order = [ranked.key for ranked in self.cleanest()] + return {**entry.row(), "rank": order.index(entry.key) + 1, "total": len(order)} + + def _fresh(self, entry: Entry | None, seconds: float) -> bool: + return bool(entry) and entry.scorer == self.scorer_id and self.clock() - entry.ranked_at < seconds + + async def rank(self, owner: str, repo: str) -> dict[str, Any]: + known = self.entries.get(f"{owner}/{repo}".lower()) + if known and self._fresh(known, FRESH_SECONDS): + return self.standing(known) + if self._waiting >= MAX_WAITING: + raise RankError(429, "busy", "Ranking is busy. Try again shortly.") + self._waiting += 1 + try: + async with self._turn: + entry = await self._build(owner, repo) + finally: + self._waiting -= 1 + self.entries[entry.key] = entry + self.dirty = True + return self.standing(entry) + + async def maintain(self, seeds: Iterable[str], pause: float) -> None: + """Ranks seeds that are missing or old, then re-ranks entries from an older scorer.""" + for slug in seeds: + if not self._fresh(self.entries.get(slug.lower()), SEED_FRESH_SECONDS): + await self._refresh(slug) + await asyncio.sleep(pause) + for entry in self.stale(): + await self._refresh(entry.repo) + await asyncio.sleep(pause) + + async def _refresh(self, slug: str) -> None: + """Re-ranks one repo; an older entry is dropped when the repo can no longer be listed.""" + owner, repo = slug.split("/") + try: + await self.rank(owner, repo) + except RankError as error: + log.info("refresh skipped: %s", error.code) + if error.code in GONE_CODES: + self._drop_stale(slug) + except httpx.HTTPError as error: + log.info("refresh skipped: %s", type(error).__name__) + + def _drop_stale(self, slug: str) -> None: + known = self.entries.get(slug.lower()) + if known and known.scorer != self.scorer_id: + del self.entries[known.key] + self.dirty = True + + async def _build(self, owner: str, repo: str) -> Entry: + meta = await self._meta(owner, repo) + slug = str(meta.get("full_name") or f"{owner}/{repo}") + ref = str(meta.get("default_branch") or "HEAD") + current = await self._measure(slug, ref) + if not qualifies(current.files, current.lines) or not current.sha: + raise RankError( + 422, "too_small", f"Needs {MIN_FILES}+ code files and {MIN_LINES:,}+ lines.", + files=current.files, lines=current.lines, + ) + twin = self._twin(current.sha, slug) + if twin: + raise RankError(409, "duplicate", f"Already ranked as {twin.repo}.", repo=twin.repo) + past = await self._past(slug, ref, current.sha) if meta else None + return Entry( + repo=slug, score=current.score, files=current.files, lines=current.lines, sha=current.sha, + ranked_at=self.clock(), + past_score=past.score if past else None, + past_sha=past.sha if past else "", + scorer=self.scorer_id, + ) + + def _twin(self, sha: str, slug: str) -> Entry | None: + """An entry for the same commit under another name, which is a fork or mirror.""" + others = (entry for entry in self.current() if entry.key != slug.lower()) + return next((entry for entry in others if entry.sha == sha), None) + + async def _meta(self, owner: str, repo: str) -> dict[str, Any]: + """Repo metadata, or {} when the API is rate limited; archives still download without it.""" + response = await self.client.get(f"{API}/repos/{owner}/{repo}", headers=self.api_headers) + if response.status_code in (403, 429): + log.info("GitHub API rate limited; ranking from the archive alone") + return {} + if response.status_code == 404: + raise RankError(404, "not_found", "Repo not found.") + response.raise_for_status() + meta = response.json() + if meta.get("private"): + raise RankError(404, "not_found", "Repo not found.") + if meta.get("fork"): + raise RankError(422, "fork", "Forks are not ranked.") + return meta + + async def _measure(self, slug: str, ref: str) -> Measure: + archive = await self._archive(slug, ref) + return await asyncio.to_thread(measure, archive, self.scorer) + + async def _archive(self, slug: str, ref: str) -> bytes: + chunks: list[bytes] = [] + size = 0 + url = f"{ARCHIVES}/{slug}/tar.gz/{quote(ref, safe='/')}" + async with self.client.stream("GET", url) as response: + if response.status_code == 404: + raise RankError(404, "not_found", "Repo not found.") + response.raise_for_status() + async for chunk in response.aiter_bytes(): + size += len(chunk) + if size > MAX_ARCHIVE_BYTES: + raise RankError(413, "too_large", "Repo is too large to rank.") + chunks.append(chunk) + return b"".join(chunks) + + async def _past(self, slug: str, ref: str, current_sha: str) -> Measure | None: + """The same branch a year earlier, for Most improved. None when it cannot be scored.""" + try: + return await self._past_measure(slug, ref, current_sha) + except (RankError, httpx.HTTPError) as error: + log.info("past score unavailable: %s", type(error).__name__) + return None + + async def _past_measure(self, slug: str, ref: str, current_sha: str) -> Measure | None: + params = {"sha": ref, "until": iso(self.clock() - PAST_SECONDS), "per_page": "1"} + url = f"{API}/repos/{slug}/commits" + response = await self.client.get(url, params=params, headers=self.api_headers) + rows = response.json() if response.status_code == 200 else [] + sha = rows[0].get("sha", "") if rows and isinstance(rows[0], dict) else "" + if not COMMIT_SHA.match(sha) or sha == current_sha: + return None + past = await self._measure(slug, sha) + return past if qualifies(past.files, past.lines) else None diff --git a/server/prompt.py b/server/prompt.py new file mode 100644 index 0000000000000000000000000000000000000000..46f8cac16516a6d66d84242c79d1ddbe7eb5b254 --- /dev/null +++ b/server/prompt.py @@ -0,0 +1,24 @@ +from __future__ import annotations + +import hashlib +from typing import Any + +from .vendored_pipeline import build_inference_messages + + +def render_chatml(messages: list[dict[str, str]]) -> str: + system, user = messages + return ( + f"<|im_start|>system\n{system['content']}<|im_end|>\n" + f"<|im_start|>user\n{user['content']}<|im_end|>\n" + "<|im_start|>assistant\n\n\n\n\n" + ) + + +def build_prompt(content: str, hygiene: dict[str, Any], filename: str) -> str: + return render_chatml(build_inference_messages(content, hygiene, filename)) + + +def content_key(content: str) -> str: + normalized = content.replace("\r\n", "\n").rstrip("\n") + return hashlib.sha256(normalized.encode("utf-8")).hexdigest() diff --git a/server/service.py b/server/service.py new file mode 100644 index 0000000000000000000000000000000000000000..6a3a5376ef6e8f565049a293479a7ecf6156fb75 --- /dev/null +++ b/server/service.py @@ -0,0 +1,150 @@ +from __future__ import annotations + +import asyncio +import json +import logging +import time +from collections import OrderedDict +from concurrent.futures import ThreadPoolExecutor +from pathlib import Path +from typing import Any + +from .backends import Refiner +from .config import Settings +from .fix_loop import PREVIEW_INTERVAL, Cancelled, Emit, FixRequest, FixRun +from .fix_queue import FixQueue, Ticket +from .telemetry import PRIVATE_SOURCES, Telemetry + +log = logging.getLogger("space.fix") + +Event = tuple[str, dict[str, Any]] +CacheKey = tuple[str, str, str, float, int] + + +def final_previews(events: list[Event]) -> set[int]: + """Indices of the last preview before each verdict, which a paced replay must not drop.""" + keep: set[int] = set() + latest = None + for index, (name, _) in enumerate(events): + if name == "preview": + latest = index + elif name == "verdict" and latest is not None: + keep.add(latest) + latest = None + return keep + + +class ResultCache: + def __init__(self, size: int) -> None: + self.size = size + self._items: OrderedDict[CacheKey, list[Event]] = OrderedDict() + + def get(self, key: CacheKey) -> list[Event] | None: + events = self._items.get(key) + if events is not None: + self._items.move_to_end(key) + return events + + def put(self, key: CacheKey, events: list[Event]) -> None: + self._items[key] = events + self._items.move_to_end(key) + while len(self._items) > self.size: + self._items.popitem(last=False) + + +def retains(source_kind: str) -> bool: + """Uploaded folders and pasted snippets live only in request memory, never in the cache.""" + return source_kind not in PRIVATE_SOURCES + + +class FixService: + def __init__(self, settings: Settings, refiner: Refiner, telemetry: Telemetry | None = None) -> None: + self.settings = settings + self.refiner = refiner + self.telemetry = telemetry or Telemetry() + self.queue = FixQueue(settings.max_waiters) + self.cache = ResultCache(settings.cache_size) + self._executor = ThreadPoolExecutor(max_workers=2, thread_name_prefix="refiner") + + def cache_key(self, request: FixRequest) -> CacheKey: + model_id = self.refiner.model_id + return (model_id, request.filename, request.content, request.threshold, request.max_passes) + + def cached(self, request: FixRequest, source_kind: str) -> list[Event] | None: + if not retains(source_kind): + return None + return self.cache.get(self.cache_key(request)) + + async def execute( + self, request: FixRequest, ticket: Ticket, emit: Emit, source_kind: str = "snippet" + ) -> list[Event]: + loop = asyncio.get_running_loop() + events: list[Event] = [] + + def thread_emit(name: str, data: dict[str, Any]) -> None: + events.append((name, data)) + loop.call_soon_threadsafe(emit, name, data) + + future = None + try: + await self.queue.wait_turn(ticket, lambda position: emit("queue", {"position": position})) + run = FixRun(self.refiner, request, ticket.cancel, thread_emit, self.settings.max_file_chars) + future = self._executor.submit(run.run) + future.add_done_callback(lambda _: loop.call_soon_threadsafe(self.queue.release, ticket)) + await asyncio.wrap_future(future) + finally: + if future is None: + self.queue.release(ticket) + elif not future.done(): + ticket.cancel.set() + if retains(source_kind): + self.cache.put(self.cache_key(request), events) + if source_kind != "examples" and self.settings.counts_fixes: + self.telemetry.record_clean(request.filename, request.content, future.result()) + return events + + async def replay(self, events: list[Event], emit: Emit, paced: bool = True) -> None: + rate = self.settings.replay_tokens_per_second if paced else 0.0 + keep = final_previews(events) + started = time.monotonic() + sent = 0 + last_preview_at = -PREVIEW_INTERVAL + for index, (name, data) in enumerate(events): + if name in ("queue", "progress"): + continue + if name == "preview" and rate > 0 and index not in keep: + if time.monotonic() - last_preview_at < PREVIEW_INTERVAL: + continue + last_preview_at = time.monotonic() + emit(name, {**data, "cached": True} if name in ("start", "done") else data) + if name != "token" or rate <= 0: + continue + sent += 1 + ahead = started + sent / rate - time.monotonic() + if ahead > 0: + await asyncio.sleep(ahead) + + async def warm_presets(self, presets_path: Path | None) -> None: + await asyncio.get_running_loop().run_in_executor(self._executor, self.refiner.load) + if presets_path is None: + return + presets = json.loads(presets_path.read_text(encoding="utf-8")) + pending = [FixRequest(preset["filename"], preset["before"]) for preset in presets] + while pending: + request = pending[0] + if self.cache.get(self.cache_key(request)) is not None: + pending.pop(0) + continue + await self.queue.wait_idle() + ticket = self.queue.join(background=True) + try: + await self.execute(request, ticket, lambda name, data: None, source_kind="examples") + except Cancelled: + log.info("warm-up yielded to a visitor") + continue + pending.pop(0) + log.info("warmed %d of %d examples", len(presets) - len(pending), len(presets)) + + def close(self) -> None: + self.queue.cancel_all() + self._executor.shutdown(wait=False, cancel_futures=True) diff --git a/server/telemetry.py b/server/telemetry.py new file mode 100644 index 0000000000000000000000000000000000000000..b66fdd52e89282133376c319488eed9a500e9858 --- /dev/null +++ b/server/telemetry.py @@ -0,0 +1,476 @@ +"""Aggregate, code-free usage counters for the Space. See space/TELEMETRY.md.""" + +from __future__ import annotations + +import asyncio +import hashlib +import hmac +import json +import logging +import secrets +import time +from collections import Counter, OrderedDict +from collections.abc import Iterable, Mapping +from datetime import datetime, timezone +from pathlib import Path +from typing import Annotated, Any, Literal, Protocol + +from pydantic import BaseModel, ConfigDict, Field + +log = logging.getLogger("space.telemetry") + +DIGEST_HEX = 16 +SEEN_LIMIT = 100_000 +OPEN_PR_LIMIT = 10_000 +STATS_VERSION = 1 +STATS_FILE = "stats.json" +PRIVATE_SOURCES = frozenset({"folder", "snippet"}) + +SQUIGGLE_KINDS = ( + "deep_nesting", "poor_naming", "noisy_comment", "swallowed_error", + "dense_line", "special_casing", "duplicate_block", "cramped_layout", +) +LOC_BUCKETS = ("0-50", "50-100", "100-300", "300-1000", "1000+") +CODE_EXTENSIONS = frozenset({ + "py", "pyi", "js", "mjs", "cjs", "ts", "tsx", "jsx", "c", "h", "cc", "cpp", "cxx", + "hpp", "rs", "go", "zig", "java", "cs", "lua", "rb", "php", "swift", "kt", "scala", "sh", +}) +OTHER_EXT = "other" +SCORE_KEYS = frozenset(str(points) for points in range(101)) +MS_STEPS_PER_DECADE = 8 +MS_DECADES = 4 + + +def _two_figures(value: float) -> int: + """Rounds to two significant figures so bucket edges read as plain numbers.""" + digits = len(str(int(value))) + return int(round(value, 2 - digits)) + + +MS_EDGES = tuple(sorted({ + _two_figures(10 ** (step / MS_STEPS_PER_DECADE)) for step in range(MS_STEPS_PER_DECADE * MS_DECADES + 1) +})) +MS_OVERFLOW = f"{MS_EDGES[-1]}+" +MS_KEYS = frozenset(str(edge) for edge in MS_EDGES) | {MS_OVERFLOW} + +SourceKind = Literal["examples", "repo", "folder", "snippet"] +SquiggleKind = Literal[ + "deep_nesting", "poor_naming", "noisy_comment", "swallowed_error", + "dense_line", "special_casing", "duplicate_block", "cramped_layout", +] +Count = Annotated[int, Field(ge=0, le=10_000)] + +COUNTERS = ( + "files_scored", "files_cleaned", "fix_runs", "scan_batches", + "score_scored_sum", "score_before_sum", "score_after_sum", + "prs_opened", "prs_merged", "prs_closed_unmerged", +) +TALLIES = ( + "scored_by_ext", "cleaned_by_ext", "score_sum_by_ext", "scored_by_loc_bucket", + "squiggles_before_by_kind", "squiggles_resolved_by_kind", "score_hist", "score_ms_hist", +) +TALLY_KEYS: dict[str, frozenset[str]] = { + "scored_by_ext": CODE_EXTENSIONS | {OTHER_EXT}, + "cleaned_by_ext": CODE_EXTENSIONS | {OTHER_EXT}, + "score_sum_by_ext": CODE_EXTENSIONS | {OTHER_EXT}, + "scored_by_loc_bucket": frozenset(LOC_BUCKETS), + "squiggles_before_by_kind": frozenset(SQUIGGLE_KINDS), + "squiggles_resolved_by_kind": frozenset(SQUIGGLE_KINDS), + "score_hist": SCORE_KEYS, + "score_ms_hist": MS_KEYS, +} + + +class ScanFile(BaseModel): + model_config = ConfigDict(extra="forbid") + + digest16: str = Field(pattern=r"^[0-9a-f]{16}$") + ext: str = Field("", max_length=8) + loc_bucket: Literal["0-50", "50-100", "100-300", "300-1000", "1000+"] + score_before: int = Field(ge=0, le=100) + squiggles: dict[SquiggleKind, Count] = Field(default_factory=dict) + score_ms: int | None = Field(None, ge=0, le=60_000) + + +class ScanBody(BaseModel): + model_config = ConfigDict(extra="forbid") + + source_kind: SourceKind + files: list[ScanFile] = Field(max_length=200) + + +def extension_of(filename: str) -> str: + """Lower-case extension of the base name, as the browser computes it; empty when there is none.""" + base = filename.rsplit("/", 1)[-1] + stem, dot, ext = base.rpartition(".") + return ext.lower() if dot and stem else "" + + +def known_ext(ext: str) -> str: + """Tally key: an extension from the fixed code set, or `other`, so tallies stay bounded.""" + return ext if ext in CODE_EXTENSIONS else OTHER_EXT + + +def content_digest(ext: str, content: str) -> str: + """First 64 bits of SHA-256(ext + NUL + content), the same digest the browser sends.""" + payload = f"{ext}\0{content}".encode("utf-8", errors="replace") + return hashlib.sha256(payload).hexdigest()[:DIGEST_HEX] + + +def pr_key(repo_id: int, pr_number: int) -> str: + return hashlib.sha256(f"{repo_id}:{pr_number}".encode()).hexdigest()[:DIGEST_HEX] + + +def kind_counts(squiggles: Iterable[Mapping[str, Any]]) -> Counter[str]: + return Counter(item["kind"] for item in squiggles if item.get("kind") in SQUIGGLE_KINDS) + + +def to_points(score: float) -> int: + return round(score * 100) + + +class DigestSet: + """Rolling set of the most recent 64-bit digests, oldest dropped first.""" + + def __init__(self, limit: int = SEEN_LIMIT) -> None: + self.limit = limit + self._items: OrderedDict[int, None] = OrderedDict() + + def add(self, digest: str) -> bool: + key = int(digest, 16) + if key in self._items: + self._items.move_to_end(key) + return False + self._items[key] = None + if len(self._items) > self.limit: + self._items.popitem(last=False) + return True + + def __len__(self) -> int: + return len(self._items) + + +def empty_stats() -> dict[str, Any]: + stats: dict[str, Any] = {"version": STATS_VERSION} + stats.update({name: 0 for name in COUNTERS}) + stats.update({name: {} for name in TALLIES}) + stats["open_pr_keys"] = [] + return stats + + +def _clean_count(value: Any) -> int: + return value if isinstance(value, int) and not isinstance(value, bool) and value >= 0 else 0 + + +def sanitize_stats(raw: Any) -> dict[str, Any]: + """Keep only known counters, bounded tally keys and opaque PR digests from a stats file.""" + stats = empty_stats() + if not isinstance(raw, dict): + return stats + for name in COUNTERS: + stats[name] = _clean_count(raw.get(name)) + for name in TALLIES: + tally = raw.get(name) + if isinstance(tally, dict): + allowed = TALLY_KEYS[name] + stats[name] = {key: _clean_count(value) for key, value in tally.items() if key in allowed} + keys = raw.get("open_pr_keys") + if isinstance(keys, list): + digests = [key for key in keys if isinstance(key, str) and len(key) == DIGEST_HEX] + stats["open_pr_keys"] = [key for key in digests if all(ch in "0123456789abcdef" for ch in key)] + return stats + + +def _bump(tally: dict[str, int], key: str, amount: int = 1) -> None: + if amount > 0: + tally[key] = tally.get(key, 0) + amount + + +def _mean(total: int, count: int) -> float | None: + return round(total / count, 1) if count else None + + +def ms_bucket(ms: int) -> str: + """Histogram key for a scoring time: the smallest edge at or above it.""" + edge = next((edge for edge in MS_EDGES if ms <= edge), None) + return str(edge) if edge is not None else MS_OVERFLOW + + +def _median_key(tally: Mapping[str, int], keys: Iterable[str]) -> str | None: + """Key holding the middle sample when keys are walked in order.""" + total = sum(tally.values()) + seen = 0 + for key in keys: + seen += tally.get(key, 0) + if total and seen * 2 >= total: + return key + return None + + +def median_score(tally: Mapping[str, int]) -> int | None: + key = _median_key(tally, (str(points) for points in range(101))) + return int(key) if key is not None else None + + +def median_ms(tally: Mapping[str, int]) -> int | None: + key = _median_key(tally, [*(str(edge) for edge in MS_EDGES), MS_OVERFLOW]) + return int(key.rstrip("+")) if key is not None else None + + +class Telemetry: + """Aggregate counters. Mutated only on the event loop; no content, names or addresses.""" + + def __init__(self, excluded: Iterable[str] = ()) -> None: + self.stats = empty_stats() + self.dirty = False + self.excluded = frozenset(excluded) + self.seen_scored = DigestSet() + self.seen_cleaned = DigestSet() + self._open_prs: set[str] = set() + + def merge(self, raw: Any) -> None: + loaded = sanitize_stats(raw) + for name in COUNTERS: + self.stats[name] += loaded[name] + for name in TALLIES: + for key, value in loaded[name].items(): + _bump(self.stats[name], key, value) + self._open_prs.update(loaded["open_pr_keys"]) + self._trim_open_prs() + + def record_scan(self, body: ScanBody) -> int: + if body.source_kind == "examples": + return 0 + counted = 0 + for item in body.files: + if item.digest16 in self.excluded or not self.seen_scored.add(item.digest16): + continue + counted += 1 + ext = known_ext(item.ext) + self.stats["score_scored_sum"] += item.score_before + _bump(self.stats["scored_by_ext"], ext) + _bump(self.stats["score_sum_by_ext"], ext, item.score_before) + _bump(self.stats["scored_by_loc_bucket"], item.loc_bucket) + _bump(self.stats["score_hist"], str(item.score_before)) + if item.score_ms is not None: + _bump(self.stats["score_ms_hist"], ms_bucket(item.score_ms)) + for kind, amount in item.squiggles.items(): + _bump(self.stats["squiggles_before_by_kind"], kind, amount) + self.stats["files_scored"] += counted + self.stats["scan_batches"] += 1 + self.dirty = True + return counted + + def record_clean(self, filename: str, content: str, done: Mapping[str, Any]) -> bool: + """Count one accepted refiner run; the file counts once however often it is cleaned.""" + if not done.get("accepted"): + return False + self.stats["fix_runs"] += 1 + self.dirty = True + ext = extension_of(filename) + digest = content_digest(ext, content) + if digest in self.excluded or not self.seen_cleaned.add(digest): + return False + before, after = done["before_hygiene"], done["after_hygiene"] + self.stats["files_cleaned"] += 1 + self.stats["score_before_sum"] += to_points(before["score"]) + self.stats["score_after_sum"] += to_points(after["score"]) + _bump(self.stats["cleaned_by_ext"], known_ext(ext)) + remaining = kind_counts(after["squiggles"]) + for kind, amount in kind_counts(before["squiggles"]).items(): + _bump(self.stats["squiggles_resolved_by_kind"], kind, amount - remaining[kind]) + return True + + def record_pr_opened(self, key: str) -> bool: + if key in self._open_prs: + return False + self._open_prs.add(key) + self._trim_open_prs() + self.stats["prs_opened"] += 1 + self.dirty = True + return True + + def record_pr_closed(self, key: str, merged: bool) -> bool: + if key not in self._open_prs: + return False + self._open_prs.discard(key) + self.stats["prs_merged" if merged else "prs_closed_unmerged"] += 1 + self.dirty = True + return True + + def _trim_open_prs(self) -> None: + while len(self._open_prs) > OPEN_PR_LIMIT: + self._open_prs.pop() + + def snapshot(self) -> str: + stats = {**self.stats, "open_pr_keys": sorted(self._open_prs)} + stats["updated_at"] = datetime.now(timezone.utc).strftime("%Y-%m-%dT%H:%M:%SZ") + return json.dumps(stats, indent=2, sort_keys=True) + "\n" + + def _by_ext(self) -> dict[str, dict[str, Any]]: + stats = self.stats + extensions = sorted(set(stats["scored_by_ext"]) | set(stats["cleaned_by_ext"])) + rows = {} + for ext in extensions: + scored = stats["scored_by_ext"].get(ext, 0) + rows[ext] = { + "scored": scored, + "cleaned": stats["cleaned_by_ext"].get(ext, 0), + "avg_score": _mean(stats["score_sum_by_ext"].get(ext, 0), scored), + } + return rows + + def public(self) -> dict[str, Any]: + stats = self.stats + cleaned = stats["files_cleaned"] + merged = stats["prs_merged"] + decided = merged + stats["prs_closed_unmerged"] + resolved = stats["squiggles_resolved_by_kind"] + return { + "files_scored": stats["files_scored"], + "files_cleaned": cleaned, + "fix_runs": stats["fix_runs"], + "issues_fixed": sum(resolved.values()), + "avg_score_scored": _mean(stats["score_scored_sum"], stats["files_scored"]), + "avg_score_before": _mean(stats["score_before_sum"], cleaned), + "avg_score_after": _mean(stats["score_after_sum"], cleaned), + "median_score": median_score(stats["score_hist"]), + "score_ms_median": median_ms(stats["score_ms_hist"]), + "score_ms_samples": sum(stats["score_ms_hist"].values()), + "by_ext": self._by_ext(), + "by_loc_bucket": dict(stats["scored_by_loc_bucket"]), + "squiggles_before_by_kind": dict(stats["squiggles_before_by_kind"]), + "squiggles_resolved_by_kind": dict(resolved), + "prs_opened": stats["prs_opened"], + "prs_merged": merged, + "prs_closed_unmerged": stats["prs_closed_unmerged"], + "prs_decided": decided, + "pr_merge_rate": round(merged / decided, 3) if decided else None, + } + + +def preset_digests(presets_path: Path | None) -> set[str]: + """Digests of the bundled examples, which never count.""" + if presets_path is None or not presets_path.is_file(): + return set() + rows = json.loads(presets_path.read_text(encoding="utf-8")) + return {content_digest(extension_of(row["filename"]), row["before"]) for row in rows} + + +class RateLimiter: + """Token bucket keyed by a salted hash of the caller; the address itself is never kept.""" + + def __init__(self, per_minute: int, idle_seconds: float = 120.0) -> None: + self.per_minute = per_minute + self.idle_seconds = idle_seconds + self._salt = secrets.token_bytes(16) + self._buckets: dict[bytes, tuple[float, float]] = {} + + def allow(self, caller: str) -> bool: + key = hmac.new(self._salt, caller.encode(), hashlib.sha256).digest()[:8] + now = time.monotonic() + tokens, updated = self._buckets.get(key, (float(self.per_minute), now)) + tokens = min(float(self.per_minute), tokens + (now - updated) * self.per_minute / 60.0) + allowed = tokens >= 1.0 + self._buckets[key] = (tokens - 1.0 if allowed else tokens, now) + if len(self._buckets) > 10_000: + self._prune(now) + return allowed + + def _prune(self, now: float) -> None: + stale = [key for key, (_, updated) in self._buckets.items() if now - updated > self.idle_seconds] + for key in stale: + del self._buckets[key] + + +class StatsStore: + """Reads and writes one JSON file locally and, when configured, in a Hugging Face Dataset repo.""" + + def __init__( + self, local_path: Path, dataset_repo: str = "", token: str = "", filename: str = STATS_FILE, + ) -> None: + self.local_path = local_path + self.dataset_repo = dataset_repo + self.token = token + self.filename = filename + self.remote_loaded = not self.remote + + @property + def remote(self) -> bool: + return bool(self.dataset_repo and self.token) + + def load(self) -> dict[str, Any] | None: + """Blocking. Returns the saved counters, or None when there is nothing to load yet.""" + if self.remote: + loaded = self._load_remote() + self.remote_loaded = True + return loaded + if self.local_path.is_file(): + return json.loads(self.local_path.read_text(encoding="utf-8")) + return None + + def _load_remote(self) -> dict[str, Any] | None: + from huggingface_hub import hf_hub_download + from huggingface_hub.errors import EntryNotFoundError + + try: + path = hf_hub_download( + self.dataset_repo, self.filename, repo_type="dataset", token=self.token, + force_download=True, + ) + except EntryNotFoundError: + return None + return json.loads(Path(path).read_text(encoding="utf-8")) + + def save(self, text: str) -> None: + """Blocking. Never uploads before the remote copy was read, so it cannot be overwritten.""" + self.local_path.parent.mkdir(parents=True, exist_ok=True) + self.local_path.write_text(text, encoding="utf-8") + if not self.remote or not self.remote_loaded: + return + from huggingface_hub import HfApi + + HfApi(token=self.token).upload_file( + path_or_fileobj=text.encode("utf-8"), + path_in_repo=self.filename, + repo_id=self.dataset_repo, + repo_type="dataset", + commit_message=f"Update {self.filename}", + ) + + +class Persisted(Protocol): + dirty: bool + + def merge(self, raw: Any) -> None: ... + + def snapshot(self) -> str: ... + + +class SyncedFile: + """Loads a model from its store once, then writes it back whenever it changed.""" + + def __init__(self, store: StatsStore, model: Persisted) -> None: + self.store = store + self.model = model + + async def load(self) -> None: + try: + loaded = await asyncio.to_thread(self.store.load) + except Exception as error: + log.warning("%s load failed: %s", self.store.filename, type(error).__name__) + return + self.model.merge(loaded) + + async def flush(self) -> None: + if not self.store.remote_loaded: + await self.load() + if not self.model.dirty: + return + text = self.model.snapshot() + self.model.dirty = False + try: + await asyncio.to_thread(self.store.save, text) + except Exception as error: + self.model.dirty = True + log.warning("%s sync failed: %s", self.store.filename, type(error).__name__) diff --git a/server/vendored_guards.py b/server/vendored_guards.py new file mode 100644 index 0000000000000000000000000000000000000000..c51ee58f6ac97c6916a453ba73c44c0179b0d6e1 --- /dev/null +++ b/server/vendored_guards.py @@ -0,0 +1,61 @@ +# Vendored verbatim from prototype/train_browser_model.py (helpers and grade_patch_candidate thresholds). +from __future__ import annotations + +import ast +from pathlib import Path + + +def infer_lang_from_name(filename: str) -> str: + ext = Path(filename.lower()).suffix + mapping = { + ".py": "python", + ".ts": "typescript", + ".mts": "typescript", + ".js": "javascript", + ".mjs": "javascript", + ".rs": "rust", + ".go": "go", + } + return mapping.get(ext, "other") + + +def count_non_comment_lines(code: str) -> int: + total = 0 + for raw in code.splitlines(): + stripped = raw.strip() + if stripped and not stripped.startswith(("#", "//", "/*", "*")): + total += 1 + return total + + +def check_basic_syntax(code: str, filename: str) -> bool: + if not code.strip(): + return False + lang = infer_lang_from_name(filename) + if lang == "python": + try: + ast.parse(code) + return True + except SyntaxError: + return False + if lang in ("typescript", "javascript", "rust", "go"): + return code.count("{") == code.count("}") and code.count("(") == code.count(")") + return True + + +def is_gutted(before_code: str, updated: str) -> bool: + orig_code_n = count_non_comment_lines(before_code) + new_code_n = count_non_comment_lines(updated) + if orig_code_n >= 1 and new_code_n == 0: + return True + if orig_code_n >= 10 and new_code_n < max(5, int(orig_code_n * 0.55)): + return True + return False + + +def is_bloated(before_code: str, updated: str) -> bool: + before_nonempty = sum(1 for ln in before_code.splitlines() if ln.strip()) + after_nonempty = sum(1 for ln in updated.splitlines() if ln.strip()) + if after_nonempty > max(40, int(before_nonempty * 1.60)): + return True + return False diff --git a/server/vendored_pipeline.py b/server/vendored_pipeline.py new file mode 100644 index 0000000000000000000000000000000000000000..bf06e9a8b9401d1c948c37f7218ab92f5e3681d3 --- /dev/null +++ b/server/vendored_pipeline.py @@ -0,0 +1,278 @@ +# Vendored verbatim from prototype/pipeline.py; keep byte-identical (tests/test_vendored.py). +from __future__ import annotations + +import difflib +import json +import re +from typing import Any + +TASK_INSTRUCTIONS = """\ +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. +Keep edits minimal and idiomatic; do not add redundant comments, wrapper ceremony, or line bloat. +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. +Return ONLY one or more SEARCH/REPLACE blocks in this exact format (no prose outside blocks): + +<<<<<<< SEARCH +exact existing lines from the file +======= +replacement lines +>>>>>>> REPLACE""" + +_PATCH_BLOCK_RE = re.compile( + r"<<<<<<< SEARCH\r?\n(.*?)\r?\n=======\r?\n(.*?)>>>>>>> REPLACE", + re.DOTALL, +) + + +def enrich_hygiene_with_source_lines( + content: str, + hygiene_response: dict[str, Any], +) -> dict[str, Any]: + src_lines = content.replace("\r\n", "\n").split("\n") + enriched_sq: list[dict[str, Any]] = [] + for sq in hygiene_response.get("squiggles", []): + line_no = int(sq.get("line", 1)) + src_line = src_lines[line_no - 1] if 1 <= line_no <= len(src_lines) else "" + enriched_sq.append( + { + "line": line_no, + "source_line": src_line, + "col_start": sq.get("col_start", 0), + "col_end": sq.get("col_end", len(src_line)), + "probability": sq.get("probability", 1.0), + "kind": sq.get("kind", ""), + "message": sq.get("message", ""), + } + ) + return { + "score": hygiene_response.get("score", 0.0), + "rl_hygiene": hygiene_response.get("rl_hygiene", 0.0), + "scores": hygiene_response.get("scores", {}), + "archetype": hygiene_response.get("archetype", ""), + "squiggles": enriched_sq, + } + + +def build_user_context( + content: str, + hygiene_response: dict[str, Any], + filename: str = "", +) -> str: + file_line = f"File: {filename}\n\n" if filename else "" + enriched = enrich_hygiene_with_source_lines(content, hygiene_response) + hygiene_text = json.dumps(enriched, indent=2) + return ( + f"{file_line}" + f"\n{content}\n\n\n" + f"\n{hygiene_text}\n" + ) + + +def build_inference_messages( + content: str, + hygiene_response: dict[str, Any], + filename: str = "", +) -> list[dict[str, str]]: + """Build the exact [system, user] message list used for both teacher API and browser LLM.""" + return [ + {"role": "system", "content": TASK_INSTRUCTIONS}, + {"role": "user", "content": build_user_context(content, hygiene_response, filename)}, + ] + + +def _strip_single_trailing_newline(text: str) -> str: + if text.endswith("\r\n"): + return text[:-2] + if text.endswith("\n"): + return text[:-1] + return text + + +def parse_changes(response_text: str) -> list[tuple[str, str]]: + blocks: list[tuple[str, str]] = [] + for match in _PATCH_BLOCK_RE.finditer(response_text): + search_part = match.group(1).replace("\r\n", "\n") + replace_part = _strip_single_trailing_newline(match.group(2)).replace("\r\n", "\n") + blocks.append((search_part, replace_part)) + return blocks + + +def _replace_rstrip_lines(source: str, search: str, replace: str) -> str | None: + src_lines = source.split("\n") + needle = [ln.rstrip() for ln in search.split("\n")] + span = len(needle) + if span == 0 or span > len(src_lines): + return None + + matches = [ + idx + for idx in range(len(src_lines) - span + 1) + if [ln.rstrip() for ln in src_lines[idx : idx + span]] == needle + ] + if len(matches) != 1: + return None + + pos = matches[0] + repl_lines = replace.split("\n") if replace else [] + merged = src_lines[:pos] + repl_lines + src_lines[pos + span :] + return "\n".join(merged) + + +def _leading_spaces(line: str) -> int: + return len(line) - len(line.lstrip(" ")) + + +def _shift_indent(text: str, delta: int) -> str: + if not text or delta == 0: + return text + shifted: list[str] = [] + for raw in text.split("\n"): + if not raw.strip(): + shifted.append("") + elif delta > 0: + shifted.append((" " * delta) + raw) + else: + trim = min(-delta, _leading_spaces(raw)) + shifted.append(raw[trim:]) + return "\n".join(shifted) + + +def _replace_stripped_unique(source: str, search: str, replace: str) -> str | None: + src_lines = source.split("\n") + s_lines = search.split("\n") + needle = [ln.strip() for ln in s_lines] + span = len(needle) + if span == 0 or span > len(src_lines) or not any(needle): + return None + if span == 1 and len(needle[0]) < 14: + return None + + matches = [ + idx + for idx in range(len(src_lines) - span + 1) + if [ln.strip() for ln in src_lines[idx : idx + span]] == needle + ] + if len(matches) != 1: + return None + + pos = matches[0] + delta = 0 + for src_ln, srch_ln in zip(src_lines[pos : pos + span], s_lines): + if src_ln.strip() and srch_ln.strip() and "\t" not in src_ln and "\t" not in srch_ln: + delta = _leading_spaces(src_ln) - _leading_spaces(srch_ln) + break + + adj_replace = _shift_indent(replace, delta) + repl_lines = adj_replace.split("\n") if adj_replace else [] + merged = src_lines[:pos] + repl_lines + src_lines[pos + span :] + return "\n".join(merged) + + +def _score_fuzzy_candidates( + src_norm: list[str], + s_nonempty: list[str], + needle_text: str, + span: int, +) -> tuple[int, float, float]: + best_pos = -1 + best_ratio = 0.0 + runner_up = 0.0 + if span < 1 or span > len(src_norm): + return best_pos, best_ratio, runner_up + + for pos in range(len(src_norm) - span + 1): + window = src_norm[pos : pos + span] + c_nonempty = [ln for ln in window if ln] + if not c_nonempty: + continue + first_ok = difflib.SequenceMatcher(None, s_nonempty[0], c_nonempty[0]).ratio() >= 0.78 + last_ok = difflib.SequenceMatcher(None, s_nonempty[-1], c_nonempty[-1]).ratio() >= 0.78 + if not (first_ok and last_ok): + continue + + cand_text = "\n".join(window) + matcher = difflib.SequenceMatcher(None, needle_text, cand_text) + if matcher.quick_ratio() < 0.88: + continue + ratio = matcher.ratio() + if ratio > best_ratio: + if best_pos >= 0 and abs(pos - best_pos) >= max(2, span // 2): + runner_up = max(runner_up, best_ratio) + best_ratio = ratio + best_pos = pos + elif abs(pos - best_pos) >= max(2, span // 2): + runner_up = max(runner_up, ratio) + return best_pos, best_ratio, runner_up + + +def _replace_fuzzy_unique(source: str, search: str, replace: str) -> str | None: + src_lines = source.split("\n") + s_lines = search.split("\n") + s_norm = [ln.strip() for ln in s_lines] + s_nonempty = [ln for ln in s_norm if ln] + span = len(s_norm) + if span == 0 or span > len(src_lines) or not s_nonempty: + return None + + needle_text = "\n".join(s_norm) + min_chars = 12 if span >= 2 else 24 + if len(needle_text) < min_chars: + return None + + src_norm = [ln.strip() for ln in src_lines] + pos, best_ratio, runner_up = _score_fuzzy_candidates(src_norm, s_nonempty, needle_text, span) + min_ratio = 0.90 if span >= 3 else 0.93 + if pos < 0 or best_ratio < min_ratio or (best_ratio - runner_up) < 0.05: + return None + + delta = 0 + for src_ln, srch_ln in zip(src_lines[pos : pos + span], s_lines): + if src_ln.strip() and srch_ln.strip() and "\t" not in src_ln and "\t" not in srch_ln: + delta = _leading_spaces(src_ln) - _leading_spaces(srch_ln) + break + + adj_replace = _shift_indent(replace, delta) + repl_lines = adj_replace.split("\n") if adj_replace else [] + merged = src_lines[:pos] + repl_lines + src_lines[pos + span :] + return "\n".join(merged) + + +def apply_changes(content: str, response_text: str) -> tuple[str, int]: + blocks = parse_changes(response_text) + if not blocks: + return content, 0 + + newline = "\r\n" if "\r\n" in content else "\n" + working = content.replace("\r\n", "\n") + applied = 0 + first_miss: str | None = None + + for search, replace in blocks: + if not search or search == replace: + continue + if replace == "" and f"{search}\n" in working: + working = working.replace(f"{search}\n", "", 1) + applied += 1 + continue + if search in working: + working = working.replace(search, replace, 1) + applied += 1 + continue + + fallback = _replace_rstrip_lines(working, search, replace) + if fallback is None: + fallback = _replace_stripped_unique(working, search, replace) + if fallback is None: + fallback = _replace_fuzzy_unique(working, search, replace) + if fallback is not None: + working = fallback + applied += 1 + elif first_miss is None: + first_miss = search.splitlines()[0][:60] + + if applied == 0 and first_miss is not None: + raise ValueError(f"SEARCH block did not match file content: {first_miss!r}") + + if newline == "\r\n": + working = working.replace("\n", "\r\n") + return working, applied diff --git a/web/.gitignore b/web/.gitignore new file mode 100644 index 0000000000000000000000000000000000000000..4dcbcf6da2cddcf2c8e93f68ee054eb83e3a33df --- /dev/null +++ b/web/.gitignore @@ -0,0 +1,3 @@ +node_modules/ +dist/ +vendor/ diff --git a/web/build.mjs b/web/build.mjs new file mode 100644 index 0000000000000000000000000000000000000000..f0b82d426b169c5b5ef9cefeece0486639699351 --- /dev/null +++ b/web/build.mjs @@ -0,0 +1,89 @@ +import { copyFile, mkdir, rm } from "node:fs/promises"; +import { existsSync } from "node:fs"; +import path from "node:path"; +import { fileURLToPath } from "node:url"; +import * as esbuild from "esbuild"; + +const webDir = path.dirname(fileURLToPath(import.meta.url)); +const srcDir = path.join(webDir, "src"); +const distDir = path.join(webDir, "dist"); +const vendorDir = path.join(webDir, "vendor"); +const docsDir = path.resolve(webDir, "../../docs"); +const SHARED_FILES = ["engine.js", "presets.json", "weights.npz"]; +const watch = process.argv.includes("--watch"); + +function sharedDir() { + const vendored = SHARED_FILES.every((name) => existsSync(path.join(vendorDir, name))); + return vendored ? vendorDir : docsDir; +} + +function sharedFilesPlugin(sourceDir) { + const targets = { + "shared:engine": path.join(sourceDir, "engine.js"), + "shared:presets": path.join(sourceDir, "presets.json"), + }; + return { + name: "shared-files", + setup(build) { + build.onResolve({ filter: /^shared:/ }, (args) => { + const target = targets[args.path]; + return target ? { path: target } : { errors: [{ text: `Unknown shared file ${args.path}` }] }; + }); + }, + }; +} + +async function copyStatic(sourceDir) { + const boardDir = path.join(distDir, "leaderboard"); + await mkdir(boardDir, { recursive: true }); + await copyFile(path.join(srcDir, "index.html"), path.join(distDir, "index.html")); + await copyFile(path.join(srcDir, "board.html"), path.join(boardDir, "index.html")); + await copyFile(path.join(sourceDir, "weights.npz"), path.join(distDir, "weights.npz")); +} + +const copyStaticPlugin = (sourceDir) => ({ + name: "copy-static", + setup(build) { + build.onEnd(async (result) => { + if (result.errors.length === 0) { + await copyStatic(sourceDir); + } + }); + }, +}); + +async function main() { + const sourceDir = sharedDir(); + await rm(distDir, { recursive: true, force: true }); + await mkdir(distDir, { recursive: true }); + + const context = await esbuild.context({ + entryPoints: { + app: path.join(srcDir, "main.js"), + board: path.join(srcDir, "board.js"), + "score-worker": path.join(srcDir, "score-worker.js"), + styles: path.join(srcDir, "styles.css"), + }, + outdir: distDir, + entryNames: "[name]", + assetNames: "fonts/[name]-[hash]", + bundle: true, + format: "esm", + target: ["es2022", "chrome123", "safari17.5", "firefox120"], + minify: !watch, + sourcemap: watch, + legalComments: "none", + loader: { ".woff2": "file" }, + logLevel: "info", + plugins: [sharedFilesPlugin(sourceDir), copyStaticPlugin(sourceDir)], + }); + + if (watch) { + await context.watch(); + return; + } + await context.rebuild(); + await context.dispose(); +} + +await main(); diff --git a/web/package-lock.json b/web/package-lock.json new file mode 100644 index 0000000000000000000000000000000000000000..fc65677123596e625023e619141623ddaed93577 --- /dev/null +++ b/web/package-lock.json @@ -0,0 +1,592 @@ +{ + "name": "codehygiene-space-web", + "lockfileVersion": 3, + "requires": true, + "packages": { + "": { + "name": "codehygiene-space-web", + "dependencies": { + "@fontsource-variable/google-sans-code": "5.3.2", + "@fontsource-variable/host-grotesk": "5.3.0", + "@lezer/go": "1.0.1", + "@lezer/highlight": "1.2.5", + "@lezer/javascript": "1.5.6", + "@lezer/python": "1.1.19", + "@lezer/rust": "1.0.3" + }, + "devDependencies": { + "esbuild": "0.28.2" + } + }, + "node_modules/@esbuild/aix-ppc64": { + "version": "0.28.2", + "resolved": "https://registry.npmjs.org/@esbuild/aix-ppc64/-/aix-ppc64-0.28.2.tgz", + "integrity": "sha512-XExcO+dvLKvVtNTibSTBej1NCAbaGhWn9Ww1ZPx80qsahhPFe/8jgWP0IchNe0F3HwkU7n8ejhH8bjonqht8mQ==", + "cpu": [ + "ppc64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "aix" + ], + "engines": { + "node": ">=18" + } + }, + "node_modules/@esbuild/android-arm": { + "version": "0.28.2", + "resolved": "https://registry.npmjs.org/@esbuild/android-arm/-/android-arm-0.28.2.tgz", + "integrity": "sha512-kXXoiPVVGQcnIYGOeaovwOURpniDBpSq4A03qkQ+BMQqtGG6HYap3xne9C1O1yo4TR3qxlCX5IqqmX6fFo2Lqg==", + "cpu": [ + "arm" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "android" + ], + "engines": { + "node": ">=18" + } + }, + "node_modules/@esbuild/android-arm64": { + "version": "0.28.2", + "resolved": "https://registry.npmjs.org/@esbuild/android-arm64/-/android-arm64-0.28.2.tgz", + "integrity": "sha512-5YfKeeI8qWfBZIX+u2xZC3Zlb3Os/gLS2sbEKM+I4ZOcsWmHS2WLysCcQZDAFRslDUU5Oiq44gf6PYN1vGwG5A==", + "cpu": [ + "arm64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "android" + ], + "engines": { + "node": ">=18" + } + }, + "node_modules/@esbuild/android-x64": { + "version": "0.28.2", + "resolved": "https://registry.npmjs.org/@esbuild/android-x64/-/android-x64-0.28.2.tgz", + "integrity": "sha512-O387ite7SzUyCcy3JQX4P4bLtEA7bLLkx+esve5JHnyYfNTxcVpXZo9jhdB0lTKN44gztELTdU7nS8Nr16Fs1Q==", + "cpu": [ + "x64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "android" + ], + "engines": { + "node": ">=18" + } + }, + "node_modules/@esbuild/darwin-arm64": { + "version": "0.28.2", + "resolved": "https://registry.npmjs.org/@esbuild/darwin-arm64/-/darwin-arm64-0.28.2.tgz", + "integrity": "sha512-n4KqkOQrraxHJcgjM1RvwbigfQKIKJVpM7xp+KsxiyUSrRdIXnt73VhrPAx0fV44hgfmIVKjxMN9J1t5jySVkw==", + "cpu": [ + "arm64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "darwin" + ], + "engines": { + "node": ">=18" + } + }, + "node_modules/@esbuild/darwin-x64": { + "version": "0.28.2", + "resolved": "https://registry.npmjs.org/@esbuild/darwin-x64/-/darwin-x64-0.28.2.tgz", + "integrity": "sha512-uq6suIWYP37qzGddBKPw5QEQPi6HiLGsO7UmkpfyaYNQ3D+rN6w6WfwH+nuqcGXWvawGwxOEroO4YGnFh95azw==", + "cpu": [ + "x64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "darwin" + ], + "engines": { + "node": ">=18" + } + }, + "node_modules/@esbuild/freebsd-arm64": { + "version": "0.28.2", + "resolved": "https://registry.npmjs.org/@esbuild/freebsd-arm64/-/freebsd-arm64-0.28.2.tgz", + "integrity": "sha512-n+I0BTSRIoy+d6RPKnEVwql5UwBJolytvY4mAOIEJorKlqgPII8ix6slVVrfZ5Tnj7glIZvloylbB/EJPMWEXw==", + "cpu": [ + "arm64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "freebsd" + ], + "engines": { + "node": ">=18" + } + }, + "node_modules/@esbuild/freebsd-x64": { + "version": "0.28.2", + "resolved": "https://registry.npmjs.org/@esbuild/freebsd-x64/-/freebsd-x64-0.28.2.tgz", + "integrity": "sha512-78XJTJkvPs0kz2w61301PJjXl4g7q3JqiYMZ/M/yVI73EHBrCRTgkhu9oqG7vPqq+a/yadEW8aD+agKlk5xrmg==", + "cpu": [ + "x64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "freebsd" + ], + "engines": { + "node": ">=18" + } + }, + "node_modules/@esbuild/linux-arm": { + "version": "0.28.2", + "resolved": "https://registry.npmjs.org/@esbuild/linux-arm/-/linux-arm-0.28.2.tgz", + "integrity": "sha512-XlDnu2q5yoqems+xay6wSAcg9DDD7K9RLKZEBOMZm3ckNpJBvOX20tSfby8KfrrhINDyv9V2YVZKY/SpoGJI8w==", + "cpu": [ + "arm" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">=18" + } + }, + "node_modules/@esbuild/linux-arm64": { + "version": "0.28.2", + "resolved": "https://registry.npmjs.org/@esbuild/linux-arm64/-/linux-arm64-0.28.2.tgz", + "integrity": "sha512-pW4AC0P3it8c7do9MVM4p51FzHzdM/TZrerurgRcHJ2WTa1VQ1CIq18xncfpBJw4ojkiZZrKW2yIBWBP92j6Ug==", + "cpu": [ + "arm64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">=18" + } + }, + "node_modules/@esbuild/linux-ia32": { + "version": "0.28.2", + "resolved": "https://registry.npmjs.org/@esbuild/linux-ia32/-/linux-ia32-0.28.2.tgz", + "integrity": "sha512-CYbnj78HsIeA+DhgUKgFCfvNsTHFhMMrinUrMZpDXJXKN8T3XViTZ/+wtHeVxEWY8ewSzTFN+nRmSwO2tZaLUQ==", + "cpu": [ + "ia32" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">=18" + } + }, + "node_modules/@esbuild/linux-loong64": { + "version": "0.28.2", + "resolved": "https://registry.npmjs.org/@esbuild/linux-loong64/-/linux-loong64-0.28.2.tgz", + "integrity": "sha512-buwkd8nsph4R+ajRvw0qM5Hja/TXQow3ptzWO2EbG/cqcIkHloRrdlBtQlshyYGTNFvfkfJ5tpPLVkY4DtsPfQ==", + "cpu": [ + "loong64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">=18" + } + }, + "node_modules/@esbuild/linux-mips64el": { + "version": "0.28.2", + "resolved": "https://registry.npmjs.org/@esbuild/linux-mips64el/-/linux-mips64el-0.28.2.tgz", + "integrity": "sha512-ZVykbDyk7519VwiNb9Lcj9m8XM6v5V9uKPvrEMkkEedVewf+0itkhahp4HDpgERXhwLRpWFypsGbG/J8s0QjJA==", + "cpu": [ + "mips64el" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">=18" + } + }, + "node_modules/@esbuild/linux-ppc64": { + "version": "0.28.2", + "resolved": "https://registry.npmjs.org/@esbuild/linux-ppc64/-/linux-ppc64-0.28.2.tgz", + "integrity": "sha512-CAXl+Dtd9UUuJd8pKKdwh6MLm3MUMiqMPmhZ3tTSXPqfyQ3vDl6R5hZdZ/kYojK4ofXtdfSv1tFq8XzWx3heNQ==", + "cpu": [ + "ppc64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">=18" + } + }, + "node_modules/@esbuild/linux-riscv64": { + "version": "0.28.2", + "resolved": "https://registry.npmjs.org/@esbuild/linux-riscv64/-/linux-riscv64-0.28.2.tgz", + "integrity": "sha512-GeXCej4IQtU1B+QlDV8W/RRvbzI3O/Stss+/bCXv4lZls5WGRtu2a+3JkA3i4qIUlMXpcHebWpF8AkJhATowuA==", + "cpu": [ + "riscv64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">=18" + } + }, + "node_modules/@esbuild/linux-s390x": { + "version": "0.28.2", + "resolved": "https://registry.npmjs.org/@esbuild/linux-s390x/-/linux-s390x-0.28.2.tgz", + "integrity": "sha512-3H1weTYZPxt/WOhByszQZybS9w5lKzUn1FDMsgEChbHWQwHYQQRfBxgCcZvPhjHfKyJjIievvMmEUawJrdY9Dg==", + "cpu": [ + "s390x" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">=18" + } + }, + "node_modules/@esbuild/linux-x64": { + "version": "0.28.2", + "resolved": "https://registry.npmjs.org/@esbuild/linux-x64/-/linux-x64-0.28.2.tgz", + "integrity": "sha512-4xTZr1FUmSoQW4XIWmit3tzQrUTZM+N3P0XV8xROKYF50XfI7xeO90+1bZvNwxIufQ9hDQVRJH5YhgPVF8A/HQ==", + "cpu": [ + "x64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">=18" + } + }, + "node_modules/@esbuild/netbsd-arm64": { + "version": "0.28.2", + "resolved": "https://registry.npmjs.org/@esbuild/netbsd-arm64/-/netbsd-arm64-0.28.2.tgz", + "integrity": "sha512-sSATRjPeDBg3pdgHoQfoYBob11Kk1FGa9lui5RIHZCoCkJa9QKlvl3/vKz2usCmYYjs7ymJR/2Nnsqe+Hjt5nw==", + "cpu": [ + "arm64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "netbsd" + ], + "engines": { + "node": ">=18" + } + }, + "node_modules/@esbuild/netbsd-x64": { + "version": "0.28.2", + "resolved": "https://registry.npmjs.org/@esbuild/netbsd-x64/-/netbsd-x64-0.28.2.tgz", + "integrity": "sha512-lqnzCV+mM0gIADaKihiCg6ifgfU2L3h5E33rNQBN1Y4MaVGnzryzmvvf7UHxprpQdE8hpqLolJ9Rl+SkIRDpyw==", + "cpu": [ + "x64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "netbsd" + ], + "engines": { + "node": ">=18" + } + }, + "node_modules/@esbuild/openbsd-arm64": { + "version": "0.28.2", + "resolved": "https://registry.npmjs.org/@esbuild/openbsd-arm64/-/openbsd-arm64-0.28.2.tgz", + "integrity": "sha512-AL2qJILH7lNjrDmCQDvdxMfAUIv8KMNZOvrwAQ8i8//ntL9FflhOyMJ8OZSMBb8/AWXe3/5v5S20y3zCoZWKoQ==", + "cpu": [ + "arm64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "openbsd" + ], + "engines": { + "node": ">=18" + } + }, + "node_modules/@esbuild/openbsd-x64": { + "version": "0.28.2", + "resolved": "https://registry.npmjs.org/@esbuild/openbsd-x64/-/openbsd-x64-0.28.2.tgz", + "integrity": "sha512-QtiuPytchRyC4rwUKhexJdQKvDuZ6hWloi3igqPQNUJCS1/v9EiO3UTOXR6A3FoMo4fnAKbWJdqaIwhOzh8qEw==", + "cpu": [ + "x64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "openbsd" + ], + "engines": { + "node": ">=18" + } + }, + "node_modules/@esbuild/openharmony-arm64": { + "version": "0.28.2", + "resolved": "https://registry.npmjs.org/@esbuild/openharmony-arm64/-/openharmony-arm64-0.28.2.tgz", + "integrity": "sha512-WkhYDmpTjLvGlScA1rwjRUmhl4k8oXR3cIbtqWmELgU/dFeHHlEllxDvdWcNJV9rbzCexB5vz8gtNewWLgCT7Q==", + "cpu": [ + "arm64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "openharmony" + ], + "engines": { + "node": ">=18" + } + }, + "node_modules/@esbuild/sunos-x64": { + "version": "0.28.2", + "resolved": "https://registry.npmjs.org/@esbuild/sunos-x64/-/sunos-x64-0.28.2.tgz", + "integrity": "sha512-GPMSkTOtMnv2U2F8gxe4Io6qmVs+YKyp832Etqqxr0hFngmXQ3rzwytelm3GIn7T4VviRUlf3sOgBOiTdvaf7g==", + "cpu": [ + "x64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "sunos" + ], + "engines": { + "node": ">=18" + } + }, + "node_modules/@esbuild/win32-arm64": { + "version": "0.28.2", + "resolved": "https://registry.npmjs.org/@esbuild/win32-arm64/-/win32-arm64-0.28.2.tgz", + "integrity": "sha512-PIhhEkE9uPBleRBrQEJpUn7MBnibZzbGzYWPmY3x+YoVg/95zbjB4CxPPOQ8l5tYYM4mMaCthF8/1DIfBQQyWQ==", + "cpu": [ + "arm64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "win32" + ], + "engines": { + "node": ">=18" + } + }, + "node_modules/@esbuild/win32-ia32": { + "version": "0.28.2", + "resolved": "https://registry.npmjs.org/@esbuild/win32-ia32/-/win32-ia32-0.28.2.tgz", + "integrity": "sha512-YmJbfTlvU7Sdn9BB+4PRES4oB6pxgS37MAONj+hBr/cpXS1aBPKXxNnDbu+QCWPj0o9dgyxeq79g6c5P8KeuYA==", + "cpu": [ + "ia32" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "win32" + ], + "engines": { + "node": ">=18" + } + }, + "node_modules/@esbuild/win32-x64": { + "version": "0.28.2", + "resolved": "https://registry.npmjs.org/@esbuild/win32-x64/-/win32-x64-0.28.2.tgz", + "integrity": "sha512-5ebpxr3nWMzrL/rnUI755Jkuee0bHL/Gq0WTF9lvcpv73wAp5eu8MfBUgWK9bhWvZjj7yX8etf/8tI8Ney695g==", + "cpu": [ + "x64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "win32" + ], + "engines": { + "node": ">=18" + } + }, + "node_modules/@fontsource-variable/google-sans-code": { + "version": "5.3.2", + "resolved": "https://registry.npmjs.org/@fontsource-variable/google-sans-code/-/google-sans-code-5.3.2.tgz", + "integrity": "sha512-kW4RcfoTTObtUWAhlh6hzs8LCDsBNLwVRPqjUIKMkEiabfR3SRGP0Y6erFKd9k1yaRppTSCLSVkxhPKpEW3QPg==", + "license": "OFL-1.1", + "funding": { + "url": "https://github.com/sponsors/ayuhito" + } + }, + "node_modules/@fontsource-variable/host-grotesk": { + "version": "5.3.0", + "resolved": "https://registry.npmjs.org/@fontsource-variable/host-grotesk/-/host-grotesk-5.3.0.tgz", + "integrity": "sha512-WvdqilphrdjYLeCHXny9gkdpIZgzyeHpgRWB8T4LSKs5nVnCs6pYYeCeMiRshxcuB6djqL6CSdF1Hq0BvMNCwA==", + "license": "OFL-1.1", + "funding": { + "url": "https://github.com/sponsors/ayuhito" + } + }, + "node_modules/@lezer/common": { + "version": "1.5.5", + "resolved": "https://registry.npmjs.org/@lezer/common/-/common-1.5.5.tgz", + "integrity": "sha512-Pw7PVCndruF1pb3ebCxpSVpkMHSDjwRCLcTRw+RizUKco5QbWylwq2g4lnG/RYN7yTTQDXE9F77MwtkAyphrjg==", + "license": "MIT" + }, + "node_modules/@lezer/go": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/@lezer/go/-/go-1.0.1.tgz", + "integrity": "sha512-xToRsYxwsgJNHTgNdStpcvmbVuKxTapV0dM0wey1geMMRc9aggoVyKgzYp41D2/vVOx+Ii4hmE206kvxIXBVXQ==", + "license": "MIT", + "dependencies": { + "@lezer/common": "^1.2.0", + "@lezer/highlight": "^1.0.0", + "@lezer/lr": "^1.3.0" + } + }, + "node_modules/@lezer/highlight": { + "version": "1.2.5", + "resolved": "https://registry.npmjs.org/@lezer/highlight/-/highlight-1.2.5.tgz", + "integrity": "sha512-O1GMVKgtf5YspFaRzpmqkVgMtIk2HG9uMAbH6wdAIdNh3TVzCsch/+cRi+6l2//UGDKe6NB9BW3cvuR5ezMQ0w==", + "license": "MIT", + "dependencies": { + "@lezer/common": "^1.3.0" + } + }, + "node_modules/@lezer/javascript": { + "version": "1.5.6", + "resolved": "https://registry.npmjs.org/@lezer/javascript/-/javascript-1.5.6.tgz", + "integrity": "sha512-sEvOqpl4vJQca0NugSQK02vQPbc5KrvoojnYGoqqFgFO0MtcYhwGiHKVEB+LuXYJnfyXo4QPA8ztbrEa4Fw5NA==", + "license": "MIT", + "dependencies": { + "@lezer/common": "^1.2.0", + "@lezer/highlight": "^1.1.3", + "@lezer/lr": "^1.3.0" + } + }, + "node_modules/@lezer/lr": { + "version": "1.4.12", + "resolved": "https://registry.npmjs.org/@lezer/lr/-/lr-1.4.12.tgz", + "integrity": "sha512-On9HU4oMMiHPMkQDgoTnGahjgG5n/MkN38CgG77sMraKLL+3JBbeQNi1FcCXV9QRqwF9/BUXP3caHyJC1fBOTQ==", + "license": "MIT", + "dependencies": { + "@lezer/common": "^1.0.0" + } + }, + "node_modules/@lezer/python": { + "version": "1.1.19", + "resolved": "https://registry.npmjs.org/@lezer/python/-/python-1.1.19.tgz", + "integrity": "sha512-MhQIURHRytsNzP/YXnqpYKW6la6voAH3kyplTOOiCdjyFY6cWWGFVmYVdHIPrElqSDf4iCDktQCockB9FxuhzQ==", + "license": "MIT", + "dependencies": { + "@lezer/common": "^1.2.0", + "@lezer/highlight": "^1.0.0", + "@lezer/lr": "^1.0.0" + } + }, + "node_modules/@lezer/rust": { + "version": "1.0.3", + "resolved": "https://registry.npmjs.org/@lezer/rust/-/rust-1.0.3.tgz", + "integrity": "sha512-XxErOjZzQ7yJt1agUT4fu9qQvESZ3acgoxpPaPTPOiUx+duCjaVAtZGFIgphkHxlN05djdVAIOy/wItShMEjqQ==", + "license": "MIT", + "dependencies": { + "@lezer/common": "^1.2.0", + "@lezer/highlight": "^1.0.0", + "@lezer/lr": "^1.0.0" + } + }, + "node_modules/esbuild": { + "version": "0.28.2", + "resolved": "https://registry.npmjs.org/esbuild/-/esbuild-0.28.2.tgz", + "integrity": "sha512-HKVLS8dvII+xoKW9kmqxbRKrnWEXfJJr/FZhhJmiqIB0e053QNYFqOBouTMO/k5sID4MvCiUCvv8b9M4h32wIA==", + "dev": true, + "hasInstallScript": true, + "license": "MIT", + "bin": { + "esbuild": "bin/esbuild" + }, + "engines": { + "node": ">=18" + }, + "optionalDependencies": { + "@esbuild/aix-ppc64": "0.28.2", + "@esbuild/android-arm": "0.28.2", + "@esbuild/android-arm64": "0.28.2", + "@esbuild/android-x64": "0.28.2", + "@esbuild/darwin-arm64": "0.28.2", + "@esbuild/darwin-x64": "0.28.2", + "@esbuild/freebsd-arm64": "0.28.2", + "@esbuild/freebsd-x64": "0.28.2", + "@esbuild/linux-arm": "0.28.2", + "@esbuild/linux-arm64": "0.28.2", + "@esbuild/linux-ia32": "0.28.2", + "@esbuild/linux-loong64": "0.28.2", + "@esbuild/linux-mips64el": "0.28.2", + "@esbuild/linux-ppc64": "0.28.2", + "@esbuild/linux-riscv64": "0.28.2", + "@esbuild/linux-s390x": "0.28.2", + "@esbuild/linux-x64": "0.28.2", + "@esbuild/netbsd-arm64": "0.28.2", + "@esbuild/netbsd-x64": "0.28.2", + "@esbuild/openbsd-arm64": "0.28.2", + "@esbuild/openbsd-x64": "0.28.2", + "@esbuild/openharmony-arm64": "0.28.2", + "@esbuild/sunos-x64": "0.28.2", + "@esbuild/win32-arm64": "0.28.2", + "@esbuild/win32-ia32": "0.28.2", + "@esbuild/win32-x64": "0.28.2" + } + } + } +} diff --git a/web/package.json b/web/package.json new file mode 100644 index 0000000000000000000000000000000000000000..7ede9788c0969da4c9c25c181f49896ac098f25c --- /dev/null +++ b/web/package.json @@ -0,0 +1,21 @@ +{ + "name": "codehygiene-space-web", + "private": true, + "type": "module", + "scripts": { + "build": "node build.mjs", + "dev": "node build.mjs --watch" + }, + "dependencies": { + "@fontsource-variable/google-sans-code": "5.3.2", + "@fontsource-variable/host-grotesk": "5.3.0", + "@lezer/go": "1.0.1", + "@lezer/highlight": "1.2.5", + "@lezer/javascript": "1.5.6", + "@lezer/python": "1.1.19", + "@lezer/rust": "1.0.3" + }, + "devDependencies": { + "esbuild": "0.28.2" + } +} diff --git a/web/src/board.css b/web/src/board.css new file mode 100644 index 0000000000000000000000000000000000000000..2f813b902e33436ba60093ec14c2de9b56a26dca --- /dev/null +++ b/web/src/board.css @@ -0,0 +1,398 @@ +.board-page { + min-height: 100svh; +} + +.board { + max-width: 800px; + margin: 0 auto; + padding: 0 24px 64px; +} + +.board-head { + display: grid; + grid-template-columns: 1fr auto 1fr; + align-items: center; + gap: 16px; + height: 72px; +} + +.board-home { + display: inline-flex; + align-items: center; + gap: 8px; + justify-self: start; + border-radius: var(--radius-sm); + color: var(--ink-2); + font-weight: 500; + letter-spacing: -0.01em; + transition: color var(--motion) var(--ease); +} + +.board-home .icon { + color: var(--ink-3); + transition: transform 160ms var(--ease-out); +} + +.board-home:hover { + color: var(--ink); +} + +.board-home:hover .icon { + transform: translateX(-2px); +} + +.board-tabs { + display: flex; + gap: 2px; + padding: 3px; + border-radius: var(--radius); + background: var(--fill); +} + +.board-tab { + display: inline-flex; + align-items: center; + height: 28px; + padding: 0 14px; + border-radius: 7px; + color: var(--ink-3); + font-size: 13.5px; + transition: color var(--motion) var(--ease), background-color var(--motion) var(--ease); +} + +.board-tab:hover { + color: var(--ink); +} + +.board-tab[aria-current="page"] { + background: var(--surface); + color: var(--ink); + box-shadow: 0 1px 2px var(--shadow-color), 0 0 0 1px var(--line); +} + +.board-updated { + justify-self: end; + color: var(--ink-3); + font-size: 12.5px; + white-space: nowrap; +} + +.board-view { + padding-top: 8px; +} + +.board-modes { + display: flex; + gap: 20px; + margin-bottom: 12px; +} + +.board-mode { + height: 28px; + border-radius: var(--radius-sm); + color: var(--ink-3); + font-size: 14px; + transition: color var(--motion) var(--ease); +} + +.board-mode:hover, +.board-mode[aria-pressed="true"] { + color: var(--ink); +} + +.board-mode[aria-pressed="true"] { + font-weight: 500; +} + +.board-list { + margin: 0; + padding: 0; + overflow: hidden; + list-style: none; + border: 1px solid var(--line); + border-radius: var(--radius-stage); + background: var(--surface); + box-shadow: var(--shadow-stage); +} + +.board-row { + display: grid; + grid-template-columns: 32px minmax(0, 1fr) 160px 44px; + align-items: center; + gap: 16px; + height: 48px; + padding: 0 20px; + border-top: 1px solid var(--line); + font-size: 14px; +} + +.board-row:first-child { + border-top: 0; +} + +.board-row.is-yours { + background: var(--selected); +} + +.board-row.is-yours .track { + background: var(--surface); +} + +.board-rank { + color: var(--ink-3); + font-variant-numeric: tabular-nums; +} + +.board-repo { + display: inline-flex; + align-items: center; + gap: 10px; + min-width: 0; + border-radius: var(--radius-sm); + color: var(--ink); + font-weight: 500; +} + +.board-repo .icon { + flex: none; + color: var(--ink-3); +} + +.board-repo-name { + overflow: hidden; + text-overflow: ellipsis; + white-space: nowrap; +} + +.board-owner { + color: var(--ink-3); + font-weight: 400; +} + +.board-repo:hover .board-repo-name { + text-decoration: underline 1px var(--line-strong); + text-underline-offset: 4px; +} + +.board-past { + color: var(--ink-3); + font-variant-numeric: tabular-nums; + text-align: right; +} + +.board-score { + font-weight: 500; + font-variant-numeric: tabular-nums; + text-align: right; +} + +.board-score.is-up { + color: var(--green); +} + +.board-empty { + padding: 48px 0; + color: var(--ink-3); + text-align: center; +} + +.track { + display: block; + height: 6px; + overflow: hidden; + border-radius: 3px; + background: var(--fill); +} + +.track i { + display: block; + height: 100%; + border-radius: inherit; + background: var(--ink-2); +} + +.tiles { + display: grid; + grid-template-columns: repeat(4, minmax(0, 1fr)); + gap: 12px; +} + +.tile, +.panel { + border: 1px solid var(--line); + border-radius: var(--radius); + background: var(--surface); +} + +.tile { + padding: 14px 16px; +} + +.tile-label, +.tile-note, +.panel-note, +.reward-fact dt { + color: var(--ink-3); + font-size: 12.5px; + line-height: 18px; +} + +.tile-value { + margin: 4px 0 2px; + font-size: 26px; + font-weight: 500; + line-height: 32px; + letter-spacing: -0.02em; + font-variant-numeric: tabular-nums; +} + +.charts { + display: grid; + grid-template-columns: repeat(2, minmax(0, 1fr)); + gap: 12px; + margin-top: 12px; +} + +.panel { + padding: 16px; +} + +.panel-title { + font-size: 14px; + font-weight: 500; + line-height: 20px; +} + +.bars { + display: grid; + gap: 10px; + margin-top: 14px; +} + +.bar-row { + display: grid; + grid-template-columns: minmax(0, 1fr) minmax(0, 1fr) 48px; + align-items: center; + gap: 12px; + font-size: 13px; + line-height: 18px; +} + +.bar-label { + overflow: hidden; + color: var(--ink-2); + text-overflow: ellipsis; + white-space: nowrap; +} + +.bar-value { + font-variant-numeric: tabular-nums; + text-align: right; +} + +.panel-empty { + color: var(--ink-3); + font-size: 13px; +} + +.reward { + margin-top: 12px; + padding: 0; +} + +.reward-head { + display: flex; + align-items: baseline; + justify-content: space-between; + gap: 16px; + padding: 16px 16px 0; +} + +.reward-source { + display: inline-flex; + align-items: center; + gap: 4px; + border-radius: var(--radius-sm); + color: var(--ink-3); + font-size: 12.5px; + transition: color var(--motion) var(--ease); +} + +.reward-source .icon { + width: 10px; + height: 10px; +} + +.reward-source:hover { + color: var(--ink); +} + +.reward-facts { + display: grid; + grid-template-columns: repeat(4, minmax(0, 1fr)); + margin: 14px 0 0; + border-top: 1px solid var(--line); +} + +.reward-fact { + padding: 12px 16px 14px; + border-left: 1px solid var(--line); +} + +.reward-fact:first-child { + border-left: 0; +} + +.reward-fact dd { + margin: 2px 0 0; + font-size: 15px; + font-weight: 500; + line-height: 22px; + font-variant-numeric: tabular-nums; + white-space: nowrap; +} + +@media (max-width: 640px) { + .board { + padding: 0 16px 48px; + } + + .board-head { + grid-template-columns: 1fr auto; + height: auto; + padding: 16px 0 8px; + } + + .board-home span, + .board-updated { + display: none; + } + + .board-row { + grid-template-columns: 24px minmax(0, 1fr) 44px; + gap: 12px; + padding: 0 14px; + } + + .board-row .track, + .board-row .board-past { + display: none; + } + + .tiles, + .reward-facts { + grid-template-columns: repeat(2, minmax(0, 1fr)); + } + + .charts { + grid-template-columns: minmax(0, 1fr); + } + + .reward-fact:nth-child(3) { + border-left: 0; + } + + .reward-fact:nth-child(n + 3) { + border-top: 1px solid var(--line); + } +} diff --git a/web/src/board.html b/web/src/board.html new file mode 100644 index 0000000000000000000000000000000000000000..fe99fab4295c48ed37eee8937949d17324433b09 --- /dev/null +++ b/web/src/board.html @@ -0,0 +1,70 @@ + + + + + + + Leaderboard · codehygiene + + + + + + + +
+
+ + + CodeHygiene + + +

+
+ +
+
+
+ + +
+
    + +
    + + +
    +
    + + diff --git a/web/src/board.js b/web/src/board.js new file mode 100644 index 0000000000000000000000000000000000000000..67260c29007878b07b526eb559ec1c398e881014 --- /dev/null +++ b/web/src/board.js @@ -0,0 +1,259 @@ +// Leaderboard and stats page. Opens in its own tab from the main page. +import "./board.css"; +import { byId, el } from "./dom.js"; +import { LANGUAGES, NOTES, sentenceCase } from "./labels.js"; +import { fetchLeaderboard, fetchStats } from "./refiner.js"; +import REWARD_FACTS from "./reward-facts.json"; +import { formatCount, formatMs } from "./telemetry.js"; + +const SVG_NS = "http://www.w3.org/2000/svg"; +const MIN_LANGUAGE_FILES = 20; +const MAX_KINDS = 5; +const MAX_LANGUAGES = 6; +const MINUTE = 60_000; +const HOUR = 60 * MINUTE; +const DAY = 24 * HOUR; + +// reward-facts.json holds published benchmark figures per scorer version, copied from the README +// benchmark table at each release. The strip stays hidden when the running scorer has no entry. +const README_AT = "https://github.com/brianbell-x/codehygiene/blob"; + +const state = { mode: "cleanest", board: null, stats: null, yours: null }; + +function icon(name) { + const svg = document.createElementNS(SVG_NS, "svg"); + svg.setAttribute("class", "icon"); + svg.setAttribute("aria-hidden", "true"); + const use = document.createElementNS(SVG_NS, "use"); + use.setAttribute("href", `#${name}`); + svg.append(use); + return svg; +} + +function points(score) { + return String(Math.round(score)); +} + +function track(percent) { + const width = Math.max(0, Math.min(100, percent)); + return el("span", { class: "track", "aria-hidden": "true" }, [el("i", { style: `width: ${width}%` })]); +} + +function since(isoTime) { + const elapsed = Date.now() - Date.parse(isoTime); + if (elapsed < MINUTE) { + return "Updated just now"; + } + if (elapsed < HOUR) { + return `Updated ${Math.floor(elapsed / MINUTE)} min ago`; + } + if (elapsed < DAY) { + return `Updated ${Math.floor(elapsed / HOUR)} h ago`; + } + return `Updated ${Math.floor(elapsed / DAY)} d ago`; +} + +function repoCell(repo) { + const [owner, name] = repo.split("/"); + return el("a", { + class: "board-repo", + href: `https://github.com/${repo}`, + target: "_blank", + rel: "noopener", + }, [ + icon("i-github"), + el("span", { class: "board-repo-name" }, [ + el("span", { class: "board-owner", text: `${owner}/` }), + el("span", { text: name }), + ]), + ]); +} + +function boardRow(row, index) { + const improved = state.mode === "improved"; + const yours = state.yours && row.repo.toLowerCase() === state.yours; + const middle = improved + ? el("span", { class: "board-past", text: `${points(row.past_score)} → ${points(row.score)}` }) + : track(row.score); + const value = improved ? `+${points(row.change)}` : points(row.score); + return el("li", { class: yours ? "board-row is-yours" : "board-row" }, [ + el("span", { class: "board-rank", text: String(index + 1) }), + repoCell(row.repo), + middle, + el("span", { class: improved ? "board-score is-up" : "board-score", text: value }), + ]); +} + +function renderBoard() { + const rows = state.board?.[state.mode] ?? []; + byId("board-list").replaceChildren(...rows.map(boardRow)); + byId("board-list").hidden = rows.length === 0; + byId("board-empty").hidden = rows.length > 0 || !state.board; + for (const button of document.querySelectorAll(".board-mode")) { + button.setAttribute("aria-pressed", String(button.dataset.mode === state.mode)); + } + byId("board-list").querySelector(".is-yours")?.scrollIntoView({ block: "center" }); +} + +function tile(label, value, note) { + return el("div", { class: "tile" }, [ + el("p", { class: "tile-label", text: label }), + el("p", { class: "tile-value", text: value }), + el("p", { class: "tile-note", text: note }), + ]); +} + +function mergeTile(stats) { + const decided = stats.prs_decided; + if (!decided) { + return tile("PRs merged", "—", "No PRs yet"); + } + const rate = `${Math.round(stats.pr_merge_rate * 100)}%`; + return tile("PRs merged", rate, `${formatCount(stats.prs_merged)} of ${formatCount(decided)}`); +} + +function statTiles(stats) { + const timed = stats.score_ms_median === null ? "—" : formatMs(stats.score_ms_median); + const typical = stats.median_score === null ? "—" : String(stats.median_score); + return [ + mergeTile(stats), + tile("Issues fixed", formatCount(stats.issues_fixed), `in ${formatCount(stats.files_cleaned)} files`), + tile("Scoring time", timed, "per file, median"), + tile("Typical file", typical, `of 100, ${formatCount(stats.files_scored)} scored`), + ]; +} + +function barRow(label, percent, value) { + return el("div", { class: "bar-row" }, [ + el("span", { class: "bar-label", text: label }), + track(percent), + el("span", { class: "bar-value", text: value }), + ]); +} + +function emptyChart() { + return el("p", { class: "panel-empty", text: "No data yet" }); +} + +function kindBars(stats) { + const kinds = Object.entries(stats.squiggles_resolved_by_kind) + .filter(([kind, count]) => count > 0 && kind in NOTES) + .sort((left, right) => right[1] - left[1]) + .slice(0, MAX_KINDS); + const top = kinds[0]?.[1] ?? 1; + const rows = kinds.map(([kind, count]) => { + const label = sentenceCase(NOTES[kind]); + return barRow(label, (count / top) * 100, formatCount(count)); + }); + return rows.length ? rows : [emptyChart()]; +} + +function languageAverages(byExt) { + const totals = new Map(); + for (const [ext, row] of Object.entries(byExt)) { + const language = LANGUAGES[ext]; + if (!language || row.avg_score === null) { + continue; + } + const total = totals.get(language) ?? { files: 0, points: 0 }; + total.files += row.scored; + total.points += row.avg_score * row.scored; + totals.set(language, total); + } + return [...totals] + .filter(([, total]) => total.files >= MIN_LANGUAGE_FILES) + .map(([language, total]) => [language, total.points / total.files]) + .sort((left, right) => right[1] - left[1]) + .slice(0, MAX_LANGUAGES); +} + +function languageBars(stats) { + const averages = languageAverages(stats.by_ext); + const rows = averages.map(([language, average]) => barRow(language, average, points(average))); + return rows.length ? rows : [emptyChart()]; +} + +function rewardFact(fact) { + return el("div", { class: "reward-fact", title: fact.title }, [ + el("dt", { text: fact.label }), + el("dd", { text: fact.value }), + ]); +} + +function renderReward(version) { + const reward = REWARD_FACTS[version]; + byId("reward").hidden = !reward; + if (!reward) { + return; + } + byId("reward-version").textContent = `Benchmarks, v${version}`; + byId("reward-source").href = `${README_AT}/${reward.ref}/README.md#benchmarks`; + byId("reward-facts").replaceChildren(...reward.facts.map(rewardFact)); +} + +function renderStats() { + const stats = state.stats; + if (!stats) { + return; + } + byId("tiles").replaceChildren(...statTiles(stats)); + byId("chart-kinds").replaceChildren(...kindBars(stats)); + byId("chart-languages").replaceChildren(...languageBars(stats)); + renderReward(stats.scorer_version); +} + +function currentView() { + return window.location.hash === "#stats" ? "stats" : "board"; +} + +function updatedText() { + if (currentView() === "stats") { + return "Live"; + } + const updated = state.board?.updated_at; + return updated ? since(updated) : ""; +} + +function renderUpdated() { + byId("board-updated").textContent = updatedText(); +} + +function showView() { + const view = currentView(); + byId("view-board").hidden = view !== "board"; + byId("view-stats").hidden = view !== "stats"; + byId("tab-board").setAttribute("aria-current", view === "board" ? "page" : "false"); + byId("tab-stats").setAttribute("aria-current", view === "stats" ? "page" : "false"); + renderUpdated(); +} + +function readYours() { + const match = window.location.hash.match(/^#repo=(.+)$/); + return match ? decodeURIComponent(match[1]).toLowerCase() : null; +} + +function boot() { + state.yours = readYours(); + for (const button of document.querySelectorAll(".board-mode")) { + button.addEventListener("click", () => { + state.mode = button.dataset.mode; + renderBoard(); + }); + } + window.addEventListener("hashchange", showView); + showView(); + fetchLeaderboard().then((board) => { + state.board = board; + renderBoard(); + renderUpdated(); + }, () => { + state.board = { cleanest: [], improved: [] }; + renderBoard(); + }); + fetchStats().then((stats) => { + state.stats = stats; + renderStats(); + }, () => byId("tiles").replaceChildren(emptyChart())); +} + +boot(); diff --git a/web/src/columns.js b/web/src/columns.js new file mode 100644 index 0000000000000000000000000000000000000000..e6192d5e06828503f275f6b94da0ad7e859b9a28 --- /dev/null +++ b/web/src/columns.js @@ -0,0 +1,25 @@ +const TAB_STOP = 2; + +export function scorerSpanToOffsets(lineText, colStart, colEnd) { + const startCol = colStart - 1; + const endCol = colEnd - 1; + let expandedCol = 0; + let from = null; + let index = 0; + + while (index < lineText.length) { + const char = String.fromCodePoint(lineText.codePointAt(index)); + const width = char === "\t" ? TAB_STOP - (expandedCol % TAB_STOP) : 1; + const nextCol = expandedCol + width; + if (from === null && nextCol > startCol) { + from = index; + } + if (nextCol > endCol) { + return { from, to: index + char.length }; + } + expandedCol = nextCol; + index += char.length; + } + const lineEnd = lineText.length; + return { from: from ?? lineEnd, to: lineEnd }; +} diff --git a/web/src/diff.js b/web/src/diff.js new file mode 100644 index 0000000000000000000000000000000000000000..3a8a224512957ed79e51a875803394e757a125f5 --- /dev/null +++ b/web/src/diff.js @@ -0,0 +1,90 @@ +const MAX_CELLS = 4_000_000; + +function trimCommon(before, after) { + let head = 0; + while (head < before.length && head < after.length && before[head] === after[head]) { + head += 1; + } + let tail = 0; + while ( + tail < before.length - head + && tail < after.length - head + && before[before.length - 1 - tail] === after[after.length - 1 - tail] + ) { + tail += 1; + } + return { head, tail }; +} + +function lcsTable(before, after) { + const width = after.length + 1; + const table = new Uint32Array((before.length + 1) * width); + for (let row = before.length - 1; row >= 0; row -= 1) { + for (let col = after.length - 1; col >= 0; col -= 1) { + const cell = row * width + col; + table[cell] = before[row] === after[col] + ? table[cell + width + 1] + 1 + : Math.max(table[cell + width], table[cell + 1]); + } + } + return table; +} + +function walkMiddle(before, after) { + if (before.length * after.length > MAX_CELLS) { + return [ + ...before.map((text, index) => ({ kind: "removed", text, oldIndex: index })), + ...after.map((text, index) => ({ kind: "added", text, newIndex: index })), + ]; + } + const table = lcsTable(before, after); + const width = after.length + 1; + const rows = []; + let row = 0; + let col = 0; + while (row < before.length || col < after.length) { + if (row < before.length && col < after.length && before[row] === after[col]) { + rows.push({ kind: "same", text: after[col], oldIndex: row, newIndex: col }); + row += 1; + col += 1; + continue; + } + const lcsIfRemoved = table[(row + 1) * width + col]; + const lcsIfAdded = table[row * width + col + 1]; + if (row < before.length && (col === after.length || lcsIfRemoved >= lcsIfAdded)) { + rows.push({ kind: "removed", text: before[row], oldIndex: row }); + row += 1; + } else { + rows.push({ kind: "added", text: after[col], newIndex: col }); + col += 1; + } + } + return rows; +} + +function shift(rows, offset) { + return rows.map((entry) => ({ + ...entry, + oldIndex: entry.oldIndex === undefined ? undefined : entry.oldIndex + offset, + newIndex: entry.newIndex === undefined ? undefined : entry.newIndex + offset, + })); +} + +export function diffLines(beforeText, afterText) { + const before = beforeText.split("\n"); + const after = afterText.split("\n"); + const { head, tail } = trimCommon(before, after); + const sameRows = (start, end, oldOffset) => after.slice(start, end).map((text, index) => ({ + kind: "same", + text, + oldIndex: oldOffset + start + index, + newIndex: start + index, + })); + const changedBefore = before.slice(head, before.length - tail); + const changedAfter = after.slice(head, after.length - tail); + return [ + ...sameRows(0, head, 0), + ...shift(walkMiddle(changedBefore, changedAfter), head), + ...sameRows(after.length - tail, after.length, before.length - after.length), + ]; +} diff --git a/web/src/dom.js b/web/src/dom.js new file mode 100644 index 0000000000000000000000000000000000000000..1abdea8a72c1212460b7105483bd86c4808556b4 --- /dev/null +++ b/web/src/dom.js @@ -0,0 +1,80 @@ +export function el(tag, props = {}, children = []) { + const node = document.createElement(tag); + for (const [key, value] of Object.entries(props)) { + if (value === null || value === undefined || value === false) { + continue; + } + if (key === "class") { + node.className = value; + } else if (key === "text") { + node.textContent = value; + } else if (key === "style") { + node.style.cssText = value; + } else if (key.startsWith("on")) { + node.addEventListener(key.slice(2), value); + } else { + node.setAttribute(key, value === true ? "" : value); + } + } + node.append(...children); + return node; +} + +export function byId(id) { + return document.getElementById(id); +} + +export function prefersReducedMotion() { + return window.matchMedia("(prefers-reduced-motion: reduce)").matches; +} + +const COPY_FEEDBACK_MS = 1200; + +function flashIcon(button, icon, label, iconHref) { + const original = { label: button.dataset.label ?? button.getAttribute("aria-label"), href: "#i-copy" }; + button.dataset.label = original.label; + button.setAttribute("aria-label", label); + icon.setAttribute("href", iconHref); + clearTimeout(Number(button.dataset.flashTimer)); + const timer = setTimeout(() => { + button.setAttribute("aria-label", original.label); + icon.setAttribute("href", original.href); + }, COPY_FEEDBACK_MS); + button.dataset.flashTimer = String(timer); +} + +export function copyWithIcon(button, icon, text) { + navigator.clipboard.writeText(text).then( + () => flashIcon(button, icon, "Copied", "#i-check"), + () => flashIcon(button, icon, "Copy failed", "#i-copy"), + ); +} + +let tweenCount = 0; + +export function tweenNumber(node, from, to, durationMs) { + if (from === to && node.dataset.tweenTo === String(to)) { + return; + } + tweenCount += 1; + const id = String(tweenCount); + node.dataset.tween = id; + node.dataset.tweenTo = String(to); + if (from === to || durationMs === 0 || prefersReducedMotion()) { + node.textContent = String(to); + return; + } + const startedAt = performance.now(); + const step = (now) => { + if (node.dataset.tween !== id) { + return; + } + const progress = Math.min(1, (now - startedAt) / durationMs); + const eased = 1 - (1 - progress) ** 3; + node.textContent = String(Math.round(from + (to - from) * eased)); + if (progress < 1) { + requestAnimationFrame(step); + } + }; + requestAnimationFrame(step); +} diff --git a/web/src/fix-run.js b/web/src/fix-run.js new file mode 100644 index 0000000000000000000000000000000000000000..43c6d1ab7f077eda2cffc8dc83badaa5aae5eb3a --- /dev/null +++ b/web/src/fix-run.js @@ -0,0 +1,168 @@ +import { STATUS } from "./labels.js"; +import { streamFix } from "./refiner.js"; +import { THRESHOLD } from "./scoring.js"; + +const SEARCH_OPEN = "<<<<<<< SEARCH\n"; +const DIVIDER = "======="; + +function searchFocusRange(rawText, docText) { + const openAt = rawText.lastIndexOf(SEARCH_OPEN); + if (openAt < 0) { + return null; + } + const completeLines = rawText.slice(openAt + SEARCH_OPEN.length).split("\n").slice(0, -1); + if (completeLines.length === 0 || completeLines.includes(DIVIDER)) { + return null; + } + const docLines = docText.split("\n"); + const lastStart = docLines.length - completeLines.length; + for (let start = 0; start <= lastStart; start += 1) { + if (completeLines.every((line, offset) => docLines[start + offset] === line)) { + return { first: start + 1, last: start + completeLines.length }; + } + } + return null; +} + +function squiggleCount(hygiene) { + return hygiene?.squiggles?.length ?? 0; +} + +export class FixSession { + constructor({ file, sourceKind, noCache, hooks }) { + this.file = file; + this.sourceKind = sourceKind; + this.noCache = noCache; + this.hooks = hooks; + this.baseline = file.content; + this.lastAccepted = this.baseline; + this.attemptBase = this.baseline; + this.preview = this.baseline; + this.raw = ""; + this.phase = "reading"; + this.readPercent = null; + this.outcome = { kind: "running" }; + this.controller = new AbortController(); + } + + get running() { + return this.outcome.kind === "running"; + } + + get statusText() { + if (this.phase === "waiting") { + return STATUS.waiting; + } + if (this.phase === "writing") { + return STATUS.rewriting; + } + return this.readPercent === null ? STATUS.reading : `${STATUS.reading} ${this.readPercent}%`; + } + + get reading() { + return this.phase === "writing" ? searchFocusRange(this.raw, this.preview) : null; + } + + async start() { + const body = { + filename: this.file.path, + content: this.baseline, + threshold: THRESHOLD, + max_passes: 2, + source_kind: this.sourceKind, + }; + if (this.noCache) { + body.no_cache = true; + } + try { + await streamFix(body, { + signal: this.controller.signal, + onEvent: (name, data) => this.handle(name, data), + }); + if (this.running) { + this.settle({ kind: "unreachable" }); + } + } catch (error) { + if (this.running) { + this.settle({ kind: error.name === "AbortError" ? "cancelled" : "unreachable" }); + } + } + } + + cancel() { + if (this.running) { + this.controller.abort(); + this.settle({ kind: "cancelled" }); + } + } + + handle(name, data) { + const handler = this.handlers[name]; + if (this.running && handler) { + handler.call(this, data); + } + } + + settle(outcome) { + this.outcome = outcome; + this.hooks.onFinish(this); + } +} + +FixSession.prototype.handlers = { + queue() { + this.phase = "waiting"; + this.hooks.onStatus(this); + }, + attempt() { + this.attemptBase = this.lastAccepted; + this.preview = this.attemptBase; + this.raw = ""; + this.phase = "reading"; + this.readPercent = null; + this.hooks.onAttempt(this); + }, + progress(data) { + if (data.phase === "read" && data.total > 0) { + this.readPercent = Math.round((data.done / data.total) * 100); + this.hooks.onStatus(this); + } + }, + token(data) { + const firstToken = this.phase !== "writing"; + this.raw += data.text; + this.phase = "writing"; + if (firstToken) { + this.hooks.onStatus(this); + } + this.hooks.onPreview(this); + }, + preview(data) { + this.preview = data.content; + this.hooks.onPreview(this); + }, + verdict(data) { + if (data.accepted) { + this.lastAccepted = this.preview; + } else { + this.preview = this.lastAccepted; + } + this.hooks.onVerdict(this, data); + }, + done(data) { + const changed = data.updated_content !== this.baseline; + if (!data.accepted || !changed) { + this.settle({ kind: "rejected" }); + return; + } + this.settle({ + kind: "kept", + content: data.updated_content, + before: squiggleCount(data.before_hygiene), + after: squiggleCount(data.after_hygiene), + }); + }, + error() { + this.settle({ kind: "rejected" }); + }, +}; diff --git a/web/src/github.js b/web/src/github.js new file mode 100644 index 0000000000000000000000000000000000000000..78ef538ec5997cb8579ae82c7fc054026611d0c7 --- /dev/null +++ b/web/src/github.js @@ -0,0 +1,322 @@ +const CODE_EXTENSIONS = new Set([ + ".py", ".pyi", ".js", ".mjs", ".cjs", ".ts", ".tsx", ".jsx", ".c", ".h", ".cc", + ".cpp", ".cxx", ".hpp", ".rs", ".go", ".zig", ".java", ".cs", ".lua", ".rb", + ".php", ".swift", ".kt", ".scala", ".sh", +]); + +const SKIP_DIRS = new Set([ + ".git", ".hg", ".svn", "__pycache__", "node_modules", "target", "build", "dist", + "venv", ".venv", ".tox", ".mypy_cache", ".pytest_cache", +]); + +const SECONDARY_DIRS = new Set([ + "test", "tests", "__tests__", "spec", "example", "examples", "benchmark", + "benchmarks", "fixtures", +]); + +const TEST_DIRS = new Set(["test", "tests", "__tests__", "spec"]); +const TEST_INFIXES = [".test.", ".spec.", "_test.", "_spec."]; + +export function isTestPath(relPath) { + const parts = relPath.toLowerCase().split("/"); + const baseName = parts[parts.length - 1]; + if (parts.slice(0, -1).some((dir) => TEST_DIRS.has(dir))) { + return true; + } + return /^tests?_/.test(baseName) || TEST_INFIXES.some((infix) => baseName.includes(infix)); +} + +const REPO_HOSTS =new Set(["github.com", "www.github.com", "gitingest.com", "www.gitingest.com"]); +const RAW_HOST = "raw.githubusercontent.com"; +const MINIFIED_SUFFIXES = [".min.js", ".min.mjs", ".min.cjs", ".bundle.js"]; +const SAFE_SEGMENT = /^[A-Za-z0-9_.-]+$/; +const MAX_FILE_BYTES = 131072; +const MAX_SCORED_FILES = 200; +const FETCH_CONCURRENCY = 12; + +export class RepoError extends Error { + constructor(code, message) { + super(message); + this.code = code; + } +} + +function describeRepo(owner, rawRepo, ref = "", subpath = "", blobPath = "") { + const repo = rawRepo.endsWith(".git") ? rawRepo.slice(0, -4) : rawRepo; + if (!SAFE_SEGMENT.test(owner) || !SAFE_SEGMENT.test(repo)) { + return null; + } + return { owner, repo, ref, subpath, blobPath, slug: `${owner}/${repo}` }; +} + +function stripScheme(text) { + return text.replace(/^https?:\/\//i, ""); +} + +function cleanPath(text) { + const cut = text.search(/[?#]/); + const sliced = cut >= 0 ? text.slice(0, cut) : text; + return sliced.replace(/^\/+|\/+$/g, ""); +} + +function describeFromSegments(segments) { + const [owner, repo, kind, ref, ...rest] = segments; + if (kind === "tree" && ref) { + return describeRepo(owner, repo, ref, rest.join("/")); + } + if (kind === "blob" && ref && rest.length > 0) { + return describeRepo(owner, repo, ref, "", rest.join("/")); + } + return describeRepo(owner, repo); +} + +function describeRawSegments(segments) { + const [owner, repo, ref, ...rest] = segments; + if (!ref || rest.length === 0) { + return null; + } + return describeRepo(owner, repo, ref, "", rest.join("/")); +} + +function describeRefSuffix(segments) { + const [owner, repoWithRef] = segments; + const atIndex = repoWithRef.indexOf("@"); + if (atIndex <= 0) { + return describeRepo(owner, repoWithRef); + } + return describeRepo(owner, repoWithRef.slice(0, atIndex), repoWithRef.slice(atIndex + 1)); +} + +export function parseRepoSpec(rawInput) { + const trimmed = rawInput.trim(); + if (!trimmed || /\s/.test(trimmed)) { + return null; + } + const withoutScheme = stripScheme(trimmed); + const firstSlash = withoutScheme.indexOf("/"); + if (firstSlash <= 0) { + return null; + } + const host = withoutScheme.slice(0, firstSlash).toLowerCase(); + const pathText = REPO_HOSTS.has(host) || host === RAW_HOST + ? withoutScheme.slice(firstSlash + 1) + : withoutScheme; + const segments = cleanPath(pathText).split("/").filter(Boolean).map(decodeURIComponent); + if (segments.length < 2) { + return null; + } + if (host === RAW_HOST) { + return describeRawSegments(segments); + } + if (segments.length === 2) { + return describeRefSuffix(segments); + } + return describeFromSegments(segments); +} + +export function isFullRepoUrl(rawText) { + const text = stripScheme(rawText.trim()).toLowerCase(); + const host = text.slice(0, text.indexOf("/")); + return (REPO_HOSTS.has(host) || host === RAW_HOST) && parseRepoSpec(rawText) !== null; +} + +export function specLabel(spec) { + return spec.ref ? `${spec.slug}@${spec.ref}` : spec.slug; +} + +function fileExtension(relPath) { + const baseName = relPath.slice(relPath.lastIndexOf("/") + 1); + const dotIndex = baseName.lastIndexOf("."); + return dotIndex <= 0 ? "" : baseName.slice(dotIndex).toLowerCase(); +} + +function hasSkippedDirectory(segments) { + return segments.slice(0, -1).some( + (dir) => dir.startsWith(".") || dir.endsWith(".egg-info") || SKIP_DIRS.has(dir), + ); +} + +function isScorablePath(relPath, byteSize, subpath) { + const lowerPath = relPath.toLowerCase(); + if (MINIFIED_SUFFIXES.some((suffix) => lowerPath.endsWith(suffix))) { + return false; + } + if (byteSize !== null && (byteSize <= 0 || byteSize > MAX_FILE_BYTES)) { + return false; + } + if (subpath && relPath !== subpath && !relPath.startsWith(`${subpath}/`)) { + return false; + } + const segments = relPath.split("/").filter(Boolean); + if (segments.length === 0 || hasSkippedDirectory(segments)) { + return false; + } + return CODE_EXTENSIONS.has(fileExtension(relPath)); +} + +function pathPriority(relPath, focusPath) { + if (focusPath && relPath === focusPath) { + return -10; + } + const parts = relPath.toLowerCase().split("/"); + const inSecondary = parts.slice(0, -1).some((dir) => SECONDARY_DIRS.has(dir)); + return (inSecondary ? 100 : 0) + parts.length; +} + +function selectEntries(entries, spec, focusPath) { + const scorable = entries.filter((entry) => isScorablePath(entry.path, entry.size, spec.subpath)); + scorable.sort((left, right) => { + const rankDiff = pathPriority(left.path, focusPath) - pathPriority(right.path, focusPath); + return rankDiff !== 0 ? rankDiff : left.path.localeCompare(right.path); + }); + return scorable.slice(0, MAX_SCORED_FILES); +} + +export function selectLocalEntries(entries) { + return selectEntries(entries, { subpath: "" }, ""); +} + +async function guardedFetch(url, signal, headers = {}) { + try { + return await fetch(url, { signal, headers }); + } catch (error) { + if (error.name === "AbortError") { + throw error; + } + throw new RepoError("offline", "Network request failed"); + } +} + +function sizeOf(value) { + return typeof value === "number" ? value : null; +} + +async function fetchApiTree(spec, ref, signal) { + const treeRef = encodeURIComponent(ref); + const url = `https://api.github.com/repos/${spec.slug}/git/trees/${treeRef}?recursive=1`; + const response = await guardedFetch(url, signal); + if (response.status === 404) { + throw new RepoError("not_found", `${spec.slug} not found`); + } + if (response.status === 403 || response.status === 429) { + return { entries: null, rateLimited: true }; + } + if (!response.ok) { + return { entries: null, rateLimited: false }; + } + const body = await response.json(); + if (body.truncated || !Array.isArray(body.tree)) { + return { entries: null, rateLimited: false }; + } + const entries = body.tree + .filter((node) => node.type === "blob" && typeof node.path === "string") + .map((node) => ({ path: node.path, size: sizeOf(node.size) })); + return { entries, rateLimited: false }; +} + +async function fetchJsdelivrTree(spec, ref, signal) { + const url = `https://data.jsdelivr.com/v1/package/gh/${spec.slug}@${encodeURIComponent(ref)}/flat`; + const response = await guardedFetch(url, signal); + if (!response.ok) { + return { entries: null, status: response.status }; + } + const payload = await response.json(); + if (!Array.isArray(payload.files)) { + return { entries: null, status: 502 }; + } + const entries = payload.files.map((item) => ({ + path: item.name.replace(/^\//, ""), + size: sizeOf(item.size), + })); + return { entries, status: 200 }; +} + +async function discoverTree(spec, signal) { + const ref = spec.ref || "HEAD"; + const api = await fetchApiTree(spec, ref, signal); + if (api.entries) { + return api.entries; + } + const cdn = await fetchJsdelivrTree(spec, ref, signal); + if (cdn.entries) { + return cdn.entries; + } + if (cdn.status === 404) { + throw new RepoError("not_found", `${spec.slug} not found`); + } + if (api.rateLimited) { + throw new RepoError("rate_limited", "GitHub rate limit reached"); + } + throw new RepoError("offline", `Tree request failed (${cdn.status})`); +} + +function encodePath(relPath) { + return relPath.split("/").map(encodeURIComponent).join("/"); +} + +async function fetchFileText(spec, relPath, signal) { + const ref = encodeURIComponent(spec.ref || "HEAD"); + const path = encodePath(relPath); + const rawUrl = `https://raw.githubusercontent.com/${spec.slug}/${ref}/${path}`; + const primary = await guardedFetch(rawUrl, signal); + if (primary.ok) { + return primary.text(); + } + const cdnUrl = `https://cdn.jsdelivr.net/gh/${spec.slug}@${ref}/${path}`; + const fallback = await guardedFetch(cdnUrl, signal); + return fallback.ok ? fallback.text() : null; +} + +export function isReadableText(text) { + return Boolean(text) && text.trim().length > 0 && !text.includes("\0"); +} + +const COMMIT_SHA = /^[0-9a-f]{40}$/; + +// The commit a branch or tag points at, so every file (and a pull request) comes from one snapshot. +async function resolveCommit(spec, signal) { + const ref = encodeURIComponent(spec.ref || "HEAD"); + const url = `https://api.github.com/repos/${spec.slug}/commits/${ref}`; + const response = await guardedFetch(url, signal, { Accept: "application/vnd.github.sha" }); + if (!response.ok) { + return null; + } + const sha = (await response.text()).trim(); + return COMMIT_SHA.test(sha) ? sha : null; +} + +export async function fetchRepoFiles(spec, { signal, focusPath, onProgress, pinCommit = false }) { + const commitSha = pinCommit ? await resolveCommit(spec, signal) : null; + const pinned = commitSha ? { ...spec, ref: commitSha } : spec; + const entries = await discoverTree(pinned, signal); + const selected = selectEntries(entries, spec, focusPath || spec.blobPath); + if (selected.length === 0) { + throw new RepoError("no_files", `No code files in ${spec.slug}`); + } + + const contents = new Array(selected.length).fill(null); + let cursor = 0; + let fetched = 0; + + async function drain() { + while (cursor < selected.length) { + const index = cursor; + cursor += 1; + const text = await fetchFileText(pinned, selected[index].path, signal); + contents[index] = isReadableText(text) ? text : null; + fetched += 1; + onProgress(fetched, selected.length); + } + } + + const lanes = Math.min(FETCH_CONCURRENCY, selected.length); + await Promise.all(Array.from({ length: lanes }, drain)); + + const files = selected + .map((entry, index) => ({ path: entry.path, content: contents[index] })) + .filter((file) => file.content !== null); + if (files.length === 0) { + throw new RepoError("no_files", `No readable files in ${spec.slug}`); + } + return { files, commitSha }; +} diff --git a/web/src/highlight.js b/web/src/highlight.js new file mode 100644 index 0000000000000000000000000000000000000000..54b3bd5611a524c320f185348c344d17f391e453 --- /dev/null +++ b/web/src/highlight.js @@ -0,0 +1,55 @@ +import { parser as goParser } from "@lezer/go"; +import { highlightTree, tagHighlighter, tags } from "@lezer/highlight"; +import { parser as jsParser } from "@lezer/javascript"; +import { parser as pythonParser } from "@lezer/python"; +import { parser as rustParser } from "@lezer/rust"; +import { extensionOf } from "./languages.js"; + +const PARSERS = { + py: () => pythonParser, + pyi: () => pythonParser, + js: () => jsParser, + mjs: () => jsParser, + cjs: () => jsParser, + jsx: () => jsParser.configure({ dialect: "jsx" }), + ts: () => jsParser.configure({ dialect: "ts" }), + tsx: () => jsParser.configure({ dialect: "ts jsx" }), + rs: () => rustParser, + go: () => goParser, +}; + +const tokenClasses = tagHighlighter([ + { tag: tags.comment, class: "tok-com" }, + { tag: [tags.string, tags.regexp, tags.character], class: "tok-str" }, + { tag: [tags.keyword, tags.bool, tags.null, tags.atom, tags.self], class: "tok-kw" }, + { tag: tags.number, class: "tok-num" }, +]); + +export function tokenizeLines(text, filename) { + const lines = text.split("\n"); + const spans = lines.map(() => []); + const factory = PARSERS[extensionOf(filename)]; + if (!factory) { + return spans; + } + const lineStarts = []; + let offset = 0; + for (const line of lines) { + lineStarts.push(offset); + offset += line.length + 1; + } + let lineIndex = 0; + highlightTree(factory().parse(text), tokenClasses, (from, to, className) => { + while (lineIndex + 1 < lineStarts.length && lineStarts[lineIndex + 1] <= from) { + lineIndex += 1; + } + for (let index = lineIndex; index < lines.length && lineStarts[index] < to; index += 1) { + const start = Math.max(from, lineStarts[index]) - lineStarts[index]; + const end = Math.min(to - lineStarts[index], lines[index].length); + if (end > start) { + spans[index].push({ from: start, to: end, className }); + } + } + }); + return spans; +} diff --git a/web/src/index.html b/web/src/index.html new file mode 100644 index 0000000000000000000000000000000000000000..cd6de689e5a878bc4997dfeb966314a9961e03b9 --- /dev/null +++ b/web/src/index.html @@ -0,0 +1,160 @@ + + + + + + + codehygiene + + + + + + +
    +
    +
    +

    Try CodeHygiene on your code.

    + +
    +
    + +
    + + + +
    +
    + or + + +
    + + +
    + +
    +
    +
    +

    + Cleaned + /100 +

    + send_receipt.py +
    +
    + +
    + + + +
    +
    + + +
    + + diff --git a/web/src/labels.js b/web/src/labels.js new file mode 100644 index 0000000000000000000000000000000000000000..aef1e0bf45bceebc349f16c61952eb57739969c5 --- /dev/null +++ b/web/src/labels.js @@ -0,0 +1,113 @@ +export const NOTES = { + deep_nesting: "deeply nested", + poor_naming: "unclear name or magic number", + noisy_comment: "redundant comment", + swallowed_error: "error silently ignored", + dense_line: "overlong line", + special_casing: "stacked special cases", + duplicate_block: "duplicated block", + cramped_layout: "dense block", +}; + +export const SOURCE_ERRORS = { + invalid: "Not a GitHub link", + not_found: "Repo not found", + rate_limited: "GitHub rate limit, try later", + no_files: "No code files found", + offline: "Offline", +}; + +export const PR_ERRORS = { + popup_blocked: "Pop-up blocked", + closed: "Sign-in closed", + login_required: "Sign-in expired", + login_failed: "Sign-in failed", + no_push: "No write access", + not_installed: "App not installed", + not_found: "Repo not found", + bad_base: "Base not found", + bad_path: "File not in base", + not_cleaner: "Nothing to commit", + busy: "Busy, try later", + failed: "PR failed", +}; + +export const RANK_ERRORS = { + too_small: "Too small to rank", + too_large: "Too large to rank", + fork: "Forks aren't ranked", + not_found: "Repo not found", + busy: "Busy, try later", + failed: "Rank failed", +}; + +export const LANGUAGES = { + py: "Python", + pyi: "Python", + ts: "TypeScript", + tsx: "TypeScript", + js: "JavaScript", + jsx: "JavaScript", + mjs: "JavaScript", + cjs: "JavaScript", + go: "Go", + rs: "Rust", + c: "C", + h: "C", + cc: "C++", + cpp: "C++", + cxx: "C++", + hpp: "C++", + zig: "Zig", + java: "Java", + cs: "C#", + lua: "Lua", + rb: "Ruby", + php: "PHP", + swift: "Swift", + kt: "Kotlin", + scala: "Scala", + sh: "Shell", +}; + +export function sentenceCase(text) { + return text.charAt(0).toUpperCase() + text.slice(1); +} + +export const STATUS = { + waiting: "Waiting", + queued: "Queued", + reading: "Reading", + rewriting: "Rewriting", + cleaned: "Cleaned", + skipped: "Skipped", + offline: "Offline", + tooLong: "Too long", + noIssues: "No issues found", +}; + +export function plural(count, noun) { + return count === 1 ? `1 ${noun}` : `${count} ${noun}s`; +} + +export function issuesText(count) { + return count === 0 ? "No issues" : plural(count, "issue"); +} + +export function fixedText(before, after) { + return after === 0 ? STATUS.cleaned : `${before - after} of ${before} fixed`; +} + +export function toHundred(score) { + return Math.round(score * 100); +} + +export function scoreWord(points) { + if (points < 50) { + return { word: "poor", className: "is-sloppy" }; + } + if (points < 75) { + return { word: "fair", className: "" }; + } + return { word: "good", className: "is-clean" }; +} diff --git a/web/src/languages.js b/web/src/languages.js new file mode 100644 index 0000000000000000000000000000000000000000..22407629be2fc3a5346466d53c16a37f3f197081 --- /dev/null +++ b/web/src/languages.js @@ -0,0 +1,43 @@ +const SNIFF_RULES = [ + { + extension: "go", + linePrefixes: ["package ", "func "], + fragments: [" := "], + }, + { + extension: "rs", + linePrefixes: ["fn ", "pub fn ", "let mut ", "impl ", "use std::"], + fragments: ["-> Option<", "-> Result<"], + }, + { + extension: "ts", + linePrefixes: ["interface ", "export interface ", "type ", "export type "], + fragments: [": string", ": number", ": boolean"], + }, + { + extension: "js", + linePrefixes: ["const ", "let ", "function ", "export function ", "export default "], + fragments: [" => "], + }, + { + extension: "py", + linePrefixes: ["def ", "class ", "import ", "from ", "elif "], + fragments: ["self."], + }, +]; + +function matchesRule(rule, trimmedLines, text) { + const startsRule = (line) => rule.linePrefixes.some((prefix) => line.startsWith(prefix)); + return trimmedLines.some(startsRule) || rule.fragments.some((fragment) => text.includes(fragment)); +} + +export function extensionOf(filename) { + const dotIndex = filename.lastIndexOf("."); + return dotIndex <= 0 ? "" : filename.slice(dotIndex + 1).toLowerCase(); +} + +export function sniffExtension(text) { + const trimmedLines = text.split("\n").map((line) => line.trimStart()); + const rule = SNIFF_RULES.find((candidate) => matchesRule(candidate, trimmedLines, text)); + return rule ? rule.extension : "py"; +} diff --git a/web/src/main.js b/web/src/main.js new file mode 100644 index 0000000000000000000000000000000000000000..05979886f0203302bd7b7285d748b4b73fd65bb3 --- /dev/null +++ b/web/src/main.js @@ -0,0 +1,687 @@ +import { byId, copyWithIcon, prefersReducedMotion, tweenNumber } from "./dom.js"; +import { FixSession } from "./fix-run.js"; +import { isFullRepoUrl, parseRepoSpec, specLabel } from "./github.js"; +import { PR_ERRORS, SOURCE_ERRORS, STATUS, fixedText, issuesText, toHundred } from "./labels.js"; +import { buildPatch, downloadText } from "./patch.js"; +import { openPullRequest } from "./pull-request.js"; +import { DEFAULT_MAX_FILE_CHARS, fetchHealth, fetchStats } from "./refiner.js"; +import { createScorer } from "./scoring.js"; +import { + RUN_SIZE, + eligibleFiles, + isSloppy, + loadExamples, + loadFolder, + loadRepo, + loadSnippet, +} from "./sources.js"; +import { createStage } from "./stage.js"; +import { createNavigator } from "./navigator.js"; +import { createTabs } from "./tabs.js"; +import { paintSample } from "./sample.js"; +import { boardLink, rankErrorText, rankLabel, requestRank } from "./ranking.js"; +import { hideBanner, renderBanner, reportScan } from "./telemetry.js"; + +const TIMING = { + firstStart: 800, + beforeFix: 1400, + afterFix: 2200, + retry: 10000, + tallyTween: 600, +}; + +const nodes = { + sample: byId("sample"), + stage: byId("stage"), + input: byId("source-input"), + error: byId("source-error"), + progress: byId("source-progress"), + + tally: byId("tally"), + tallyLabel: byId("tally-label"), + tallyFixed: byId("tally-fixed"), + tallyTotal: byId("tally-total"), + tallyFill: byId("tally-fill"), + + copy: byId("copy"), + copyIcon: byId("copy-icon"), + patch: byId("patch"), + pr: byId("pr"), + prOpen: byId("pr-open"), + + rank: byId("rank"), + rankOpen: byId("rank-open"), + rankText: byId("rank-text"), +}; + +const PATCH_SOURCES = new Set(["repo", "folder"]); + +const narrowScreen = window.matchMedia("(max-width: 640px)"); +const scorer = createScorer(); +const stage = createStage(); +const fileNav = createNavigator(selectFile); +const fileTabs = createTabs(selectFile); + +const app = { + repoSpec: null, + sourceKind: "examples", + files: [], + queue: [], + cursor: -1, + shown: null, + following: true, + idle: true, + activeFile: null, + picked: new Set(), + session: null, + timers: new Set(), + initialIssues: 0, + shownFixed: null, + ingest: null, + maxChars: DEFAULT_MAX_FILE_CHARS, + healthReady: Promise.resolve(), + prEnabled: false, + commitSha: null, + prUrl: null, + standing: null, + renderQueued: false, + followChange: false, + ingestController: null, +}; + +function later(ms, action) { + const timer = setTimeout(() => { + app.timers.delete(timer); + action(); + }, ms); + app.timers.add(timer); +} + +function clearTimers() { + app.timers.forEach(clearTimeout); + app.timers.clear(); +} + +function issueCount(files) { + return files.reduce((sum, file) => sum + file.result.squiggles.length, 0); +} + +function renderTally() { + nodes.progress.hidden = !app.ingest; + nodes.progress.textContent = app.ingest ?? ""; + const total = app.initialIssues; + const fixed = Math.max(0, total - issueCount(app.files)); + nodes.tally.classList.toggle("is-empty", total === 0); + nodes.tallyLabel.textContent = total === 0 ? STATUS.noIssues : "Issues fixed"; + nodes.tallyTotal.textContent = String(total); + tweenNumber(nodes.tallyFixed, app.shownFixed ?? fixed, fixed, TIMING.tallyTween); + app.shownFixed = fixed; + nodes.tallyFill.style.width = `${total === 0 ? 0 : (fixed / total) * 100}%`; +} + +function tabFiles(current) { + const working = new Set([...app.queue, ...app.picked]); + if (current) { + working.add(current); + } + return [...working]; +} + +function paintNav(current, { fresh = false } = {}) { + if (fresh) { + fileNav.render(app.files, current); + } else { + fileNav.update(app.files, current); + } + fileTabs.render(tabFiles(current), current, app.session?.file ?? null); +} +function fileStatus(file) { + if (app.session?.file === file) { + return app.session.statusText; + } + if (app.picked.has(file) && !file.attempted && app.activeFile !== file) { + return STATUS.queued; + } + if (file.kept) { + return fixedText(file.originalResult.squiggles.length, file.result.squiggles.length); + } + if (file.lastRejected && file.attempted) { + return STATUS.skipped; + } + if (isSloppy(file) && !file.kept && file.content.length > app.maxChars) { + return STATUS.tooLong; + } + return issuesText(file.result.squiggles.length); +} + +function keptFiles() { + return app.files.filter((file) => file.kept && file.content !== file.original); +} + +function renderActions(file) { + nodes.copy.hidden = !file?.kept || app.session?.file === file; + const anyKept = keptFiles().length > 0; + const canPr = anyKept && app.prEnabled && app.sourceKind === "repo" && Boolean(app.commitSha); + nodes.patch.hidden = !anyKept || !PATCH_SOURCES.has(app.sourceKind); + nodes.pr.hidden = !canPr || Boolean(app.prUrl); + nodes.prOpen.hidden = !app.prUrl; + nodes.prOpen.href = app.prUrl ?? ""; + const rankable = app.sourceKind === "repo" && Boolean(app.repoSpec); + nodes.rank.hidden = !rankable || Boolean(app.standing); + nodes.rankOpen.hidden = !rankable || !app.standing; +} + +function paintStanding(standing) { + nodes.rankText.textContent = rankLabel(standing); + nodes.rankOpen.href = boardLink(standing.repo); + const points = Math.round(standing.score); + nodes.rankOpen.title = `${standing.repo}: ${points}/100, #${standing.rank} of ${standing.total}`; +} + +async function rankRepo() { + const spec = app.repoSpec; + showSourceError(""); + setBusy(nodes.rank, true); + try { + const standing = await requestRank(spec); + if (app.repoSpec === spec) { + app.standing = standing; + paintStanding(standing); + } + } catch (error) { + showSourceError(rankErrorText(error)); + } finally { + setBusy(nodes.rank, false); + renderActions(app.shown); + } +} + +function patchName() { + return app.repoSpec ? `codehygiene-${app.repoSpec.repo}.patch` : "codehygiene.patch"; +} + +function downloadPatch() { + downloadText(buildPatch(keptFiles()), patchName()); +} + +function setBusy(button, busy) { + button.disabled = busy; + button.classList.toggle("is-busy", busy); + button.setAttribute("aria-busy", String(busy)); +} + +async function createPr() { + showSourceError(""); + setBusy(nodes.pr, true); + try { + const opened = await openPullRequest({ + spec: app.repoSpec, + commitSha: app.commitSha, + files: keptFiles(), + }); + app.prUrl = opened.url; + } catch (error) { + showSourceError(PR_ERRORS[error.code] ?? PR_ERRORS.failed); + } finally { + setBusy(nodes.pr, false); + renderActions(app.shown); + } +} + +function renderRestingDiff(file, text, followChange) { + stage.renderDiff({ + base: file.original, + next: text, + filename: file.path, + followChange, + squiggles: file.result.squiggles, + squigglesOn: "next", + }); +} + +function renderShown({ enter = false, animateScore = false } = {}) { + const file = app.shown; + stage.setHead({ + filename: file.path, + statusText: fileStatus(file), + points: toHundred(file.result.score), + animate: animateScore, + }); + if (file.kept) { + renderRestingDiff(file, file.content, enter); + } else { + stage.renderPlain({ text: file.content, filename: file.path, squiggles: file.result.squiggles, enter }); + } + renderActions(file); +} + +function showFile(file, { enter = true } = {}) { + app.shown = file; + stage.resetScore(); + paintNav(file); + if (app.session?.file === file) { + renderSessionDiff(app.session, true); + renderActions(file); + return; + } + renderShown({ enter }); +} + +function renderSessionDiff(session, followChange) { + const scoredBase = session.attemptBase === (session.file.scoredText ?? session.file.content); + stage.setHead({ + filename: session.file.path, + statusText: session.statusText, + points: toHundred(session.file.result.score), + }); + stage.renderDiff({ + base: session.attemptBase, + next: session.preview, + filename: session.file.path, + reading: session.reading, + followChange, + squiggles: scoredBase ? session.file.result.squiggles : [], + }); +} + +function queueSessionRender(session, followChange) { + app.followChange ||= followChange; + if (app.renderQueued || app.shown !== session.file) { + return; + } + app.renderQueued = true; + requestAnimationFrame(() => { + const follow = app.followChange; + Object.assign(app, { renderQueued: false, followChange: false }); + if (app.session === session && app.shown === session.file) { + renderSessionDiff(session, follow); + } + }); +} + +function renderFileText(file, text) { + stage.renderPlain({ text, filename: file.path, squiggles: file.result.squiggles, enter: false }); +} + +function sessionMovedOn(file, acceptedText) { + return app.session?.file === file && app.session.preview !== acceptedText; +} + +async function handleVerdict(session, verdict) { + const file = session.file; + if (!verdict.accepted) { + if (app.shown === file) { + stage.setStatus(STATUS.skipped); + renderFileText(file, session.preview); + } + return; + } + const acceptedText = session.preview; + if (app.shown === file) { + stage.setStatus(fixedText(file.originalResult.squiggles.length, verdict.squiggles_after)); + } + const result = await scorer.scoreOne(acceptedText); + if (session.outcome.kind === "cancelled") { + return; + } + file.result = result; + file.scoredText = acceptedText; + paintNav(app.shown); + renderTally(); + if (app.shown !== file || sessionMovedOn(file, acceptedText)) { + return; + } + file.restingText = acceptedText; + stage.setScore(toHundred(file.result.score), true); + renderRestingDiff(file, acceptedText, true); +} + +async function restoreScore(file) { + file.result = await scorer.scoreOne(file.content); + file.scoredText = file.content; + paintNav(app.shown); + renderTally(); +} + +async function finishSession(session) { + const file = session.file; + const { outcome } = session; + if (app.session === session) { + app.session = null; + } + if (outcome.kind === "cancelled") { + if (file.scoredText !== file.content) { + await restoreScore(file); + } + return; + } + if (outcome.kind === "unreachable") { + if (app.shown === file) { + renderShown(); + stage.setStatus(STATUS.offline); + } + later(TIMING.retry, () => fixFile(file)); + return; + } + app.activeFile = null; + file.attempted = true; + file.lastRejected = outcome.kind === "rejected"; + if (outcome.kind === "kept") { + file.content = outcome.content; + file.result = await scorer.scoreOne(outcome.content); + file.scoredText = outcome.content; + file.kept = true; + file.cleaned = file.result.squiggles.length === 0; + } + paintNav(app.shown); + renderTally(); + renderActions(app.shown); + const resting = file.kept && file.restingText === file.content; + file.restingText = null; + if (app.shown === file && resting) { + stage.setStatus(fileStatus(file)); + renderActions(file); + } else if (app.shown === file) { + renderShown(); + } + later(TIMING.afterFix, advance); +} + +function fixFile(file) { + if (app.following && app.shown !== file) { + showFile(file, { enter: false }); + } + const session = new FixSession({ + file, + sourceKind: app.sourceKind, + noCache: file.lastRejected, + hooks: { + onStatus: (current) => app.shown === file && stage.setStatus(current.statusText), + onAttempt: (current) => queueSessionRender(current, true), + onPreview: (current) => queueSessionRender(current, false), + onVerdict: handleVerdict, + onFinish: finishSession, + }, + }); + app.session = session; + paintNav(app.shown); + renderActions(app.shown); + session.start(); +} + +function openAndFix(index, { reshow = true } = {}) { + app.cursor = index; + const file = app.queue[index]; + app.activeFile = file; + if (app.following && (reshow || app.shown !== file)) { + showFile(file); + } + later(TIMING.beforeFix, () => fixFile(file)); +} + +function advance() { + if (app.cursor + 1 < app.queue.length) { + openAndFix(app.cursor + 1); + return; + } + app.idle = true; +} + +function stopEverything() { + clearTimers(); + app.session?.cancel(); + app.session = null; + app.activeFile = null; +} + +function beginRun(queue, startDelay) { + stopEverything(); + Object.assign(app, { queue, cursor: -1, following: true, idle: queue.length === 0, picked: new Set() }); + renderTally(); + paintNav(queue[0] ?? app.files[0], { fresh: true }); + showFile(queue[0] ?? app.files[0]); + if (queue.length > 0) { + later(startDelay, () => openAndFix(0, { reshow: false })); + } +} + +function bringStageIntoView() { + if (narrowScreen.matches) { + nodes.stage.scrollIntoView({ behavior: prefersReducedMotion() ? "auto" : "smooth", block: "start" }); + } +} + +function revealStage(update) { + if (!nodes.stage.hidden) { + update(); + return; + } + const swap = () => { + nodes.sample.hidden = true; + nodes.stage.hidden = false; + update(); + }; + if (!document.startViewTransition || prefersReducedMotion()) { + swap(); + bringStageIntoView(); + return; + } + document.startViewTransition(swap).finished.then(bringStageIntoView); +} + +function startFiles({ files, sourceKind, repoSpec = null, commitSha = null, queue, startDelay }) { + Object.assign(app, { files, sourceKind, repoSpec, commitSha }); + Object.assign(app, { prUrl: null, standing: null, ingest: null, shownFixed: null }); + app.initialIssues = issueCount(files); + syncUrl(); + reportScan(sourceKind, files); + revealStage(() => beginRun(queue, startDelay)); +} + +async function startExamples() { + showSourceError(""); + stopEverything(); + const files = await loadExamples(scorer.scoreAll); + startFiles({ + files, + sourceKind: "examples", + queue: files.filter(isSloppy), + startDelay: TIMING.firstStart, + }); +} + +function repoQueue(files) { + return eligibleFiles(files, app.maxChars).slice(0, RUN_SIZE); +} + +function syncUrl() { + const url = new URL(window.location.href); + url.searchParams.delete("repo"); + if (app.repoSpec) { + url.searchParams.set("repo", specLabel(app.repoSpec)); + } + url.search = url.searchParams.toString().replaceAll("%2F", "/").replaceAll("%40", "@"); + window.history.replaceState(null, "", url); +} + +function showSourceError(message) { + nodes.error.hidden = !message; + nodes.error.textContent = message; +} + +async function startRepo(spec) { + showSourceError(""); + stopEverything(); + app.ingestController?.abort(); + const controller = new AbortController(); + app.ingest = "Fetching"; + app.ingestController = controller; + renderTally(); + try { + await app.healthReady; + const { files, commitSha } = await loadRepo(spec, { + signal: controller.signal, + scoreAll: scorer.scoreAll, + pinCommit: app.prEnabled, + onProgress: (text) => { + app.ingest = text; + renderTally(); + }, + }); + startFiles({ + files, + sourceKind: "repo", + repoSpec: spec, + commitSha, + queue: repoQueue(files), + startDelay: 0, + }); + } catch (error) { + if (error.name === "AbortError") { + return; + } + app.ingest = null; + renderTally(); + showSourceError(SOURCE_ERRORS[error.code] ?? SOURCE_ERRORS.offline); + } +} + +async function startFolder(fileList) { + showSourceError(""); + stopEverything(); + app.ingestController?.abort(); + app.ingest = "Reading"; + renderTally(); + try { + const files = await loadFolder(fileList, { + scoreAll: scorer.scoreAll, + onProgress: (text) => { + app.ingest = text; + renderTally(); + }, + }); + nodes.input.value = ""; + startFiles({ files, sourceKind: "folder", queue: repoQueue(files), startDelay: 0 }); + } catch (error) { + app.ingest = null; + renderTally(); + showSourceError(SOURCE_ERRORS[error.code] ?? SOURCE_ERRORS.no_files); + } +} + +async function startSnippet(text) { + showSourceError(""); + const files = await loadSnippet(text, scorer.scoreAll); + const queue = files.filter((file) => isSloppy(file) && file.content.length <= app.maxChars); + startFiles({ files, sourceKind: "snippet", queue, startDelay: 0 }); +} + +function submitLink(text) { + const spec = parseRepoSpec(text); + if (!spec) { + showSourceError(SOURCE_ERRORS.invalid); + return; + } + nodes.input.value = spec.slug; + startRepo(spec); +} + +function canClean(file) { + const position = app.queue.indexOf(file); + const started = position !== -1 && position <= app.cursor; + return isSloppy(file) && !file.attempted && !started && file.content.length <= app.maxChars; +} + +function cleanNext(file) { + if (!canClean(file)) { + return; + } + app.picked.add(file); + app.queue = app.queue.filter((queued) => queued !== file); + const autoplayBusy = app.activeFile && !app.picked.has(app.activeFile); + if (autoplayBusy) { + const cursor = app.cursor; + stopEverything(); + app.queue.splice(cursor, 0, file); + openAndFix(cursor); + return; + } + app.queue.splice(app.cursor + 1, 0, file); + stage.setStatus(fileStatus(file)); + if (app.idle) { + app.idle = false; + advance(); + } +} + +function selectFile(file) { + app.following = false; + showFile(file); + cleanNext(file); +} + +function handlePaste(event) { + const text = event.clipboardData?.getData("text/plain") ?? ""; + const multiLine = text.trim().includes("\n"); + if (multiLine) { + event.preventDefault(); + startSnippet(text); + return; + } + if (isFullRepoUrl(text)) { + event.preventDefault(); + nodes.input.value = text.trim(); + submitLink(text); + } +} + +function applyHealth(health) { + app.maxChars = health.max_file_chars || DEFAULT_MAX_FILE_CHARS; + app.prEnabled = health.github_pr_enabled === true; + byId("foot-mode").hidden = health.backend !== "mock"; +} + +function keepDefaultLimits() { + app.maxChars = DEFAULT_MAX_FILE_CHARS; +} + +function bindControls() { + byId("source").addEventListener("submit", (event) => { + event.preventDefault(); + submitLink(nodes.input.value); + }); + nodes.copy.addEventListener("click", () => copyWithIcon(nodes.copy, nodes.copyIcon, app.shown.content)); + nodes.patch.addEventListener("click", downloadPatch); + nodes.pr.addEventListener("click", createPr); + nodes.rank.addEventListener("click", rankRepo); + byId("command-copy").addEventListener("click", () => copyWithIcon( + byId("command-copy"), + byId("command-copy-icon"), + byId("command-text").textContent, + )); + byId("sample-run").addEventListener("click", startExamples); + const uploadInput = byId("upload-input"); + byId("upload").addEventListener("click", () => uploadInput.click()); + uploadInput.addEventListener("change", () => { + if (uploadInput.files.length > 0) { + startFolder([...uploadInput.files]); + } + uploadInput.value = ""; + }); + document.addEventListener("paste", handlePaste); +} + +function boot() { + bindControls(); + app.healthReady = fetchHealth().then(applyHealth, keepDefaultLimits); + fetchStats().then(renderBanner, hideBanner); + paintSample(scorer.scoreAll); + const repoParam = new URLSearchParams(window.location.search).get("repo"); + const spec = repoParam ? parseRepoSpec(repoParam) : null; + if (spec) { + nodes.input.value = spec.slug; + startRepo(spec); + } +} + +boot(); diff --git a/web/src/navigator.js b/web/src/navigator.js new file mode 100644 index 0000000000000000000000000000000000000000..6446b17317b4cf888ace94eec68995a5634db6a0 --- /dev/null +++ b/web/src/navigator.js @@ -0,0 +1,261 @@ +import { byId, el } from "./dom.js"; +import { plural } from "./labels.js"; + +const ROW_INDENT = 12; +const NARROW = "(max-width: 640px)"; + +function insertPath(root, file) { + const parts = file.path.split("/"); + let folder = root; + parts.slice(0, -1).forEach((name, depth) => { + const path = parts.slice(0, depth + 1).join("/"); + if (!folder.folders.has(name)) { + folder.folders.set(name, { name, path, folders: new Map(), files: [] }); + } + folder = folder.folders.get(name); + }); + folder.files.push(file); +} + +function buildTree(files) { + const root = { name: "", path: "", folders: new Map(), files: [] }; + files.forEach((file) => insertPath(root, file)); + return root; +} + +const byName = (left, right) => left.name.localeCompare(right.name); +const byPath = (left, right) => left.path.localeCompare(right.path); + +function sortedFolders(folder) { + return [...folder.folders.values()].sort(byName); +} + +function sortedFiles(folder) { + return [...folder.files].sort(byPath); +} + +function flatten(folder) { + return [...sortedFolders(folder).flatMap(flatten), ...sortedFiles(folder)]; +} + +export function fileState(file) { + if (file.result.squiggles.length > 0) { + return "has-issues"; + } + return file.cleaned ? "is-cleaned" : ""; +} + +function folderState(folder) { + const states = flatten(folder).map(fileState); + if (states.includes("has-issues")) { + return "has-issues"; + } + return states.includes("is-cleaned") ? "is-cleaned" : ""; +} + +function ancestors(path) { + const parts = path.split("/").slice(0, -1); + return parts.map((_, index) => parts.slice(0, index + 1).join("/")); +} + +export function baseName(path) { + return path.slice(path.lastIndexOf("/") + 1); +} + +function fileTitle(file) { + const count = file.result.squiggles.length; + return count > 0 ? `${file.path} · ${plural(count, "issue")}` : file.path; +} + +function icon(href, className = "icon") { + const svg = document.createElementNS("http://www.w3.org/2000/svg", "svg"); + svg.setAttribute("class", className); + svg.setAttribute("aria-hidden", "true"); + const use = document.createElementNS("http://www.w3.org/2000/svg", "use"); + use.setAttribute("href", href); + svg.append(use); + return svg; +} + +function fileBadge(file) { + const count = file.result.squiggles.length; + if (count > 0) { + return el("span", { class: "tree-count", text: String(count) }); + } + return file.cleaned ? el("span", { class: "tree-mark" }) : ""; +} + +function rowClass(state, selected) { + return ["tree-row", state, selected ? "is-current" : ""].filter(Boolean).join(" "); +} + +function keepRowVisible(list, row) { + const rowBox = row.getBoundingClientRect(); + const listBox = list.getBoundingClientRect(); + if (rowBox.top < listBox.top) { + list.scrollTop -= listBox.top - rowBox.top + 8; + } else if (rowBox.bottom > listBox.bottom) { + list.scrollTop += rowBox.bottom - listBox.bottom + 8; + } +} + +export function createNavigator(onSelect) { + const stage = byId("stage"); + const explorer = byId("explorer"); + const toggle = byId("explorer-toggle"); + const filter = byId("tree-filter"); + const list = byId("tree-list"); + const narrow = window.matchMedia(NARROW); + const open = new Set(); + let files = []; + let order = []; + let current = null; + + function setExplorer(visible) { + const label = visible ? "Hide files" : "Show files"; + stage.classList.toggle("is-collapsed", !visible); + toggle.setAttribute("aria-expanded", String(visible)); + toggle.setAttribute("aria-label", label); + toggle.title = label; + } + + function explorerVisible() { + return !stage.classList.contains("is-collapsed"); + } + + function choose(file) { + if (narrow.matches) { + setExplorer(false); + } + onSelect(file); + } + + function folderRow(folder, depth, expanded) { + return el("button", { + class: rowClass(folderState(folder), false), + type: "button", + style: `--depth: ${depth * ROW_INDENT}px`, + "aria-expanded": String(expanded), + onclick: () => { + if (open.has(folder.path)) { + open.delete(folder.path); + } else { + open.add(folder.path); + } + paintList(); + }, + }, [ + icon("#i-chevron", "icon tree-chevron"), + icon("#i-folder"), + el("span", { class: "tree-name", text: folder.name }), + folderState(folder) ? el("span", { class: "tree-mark" }) : "", + ]); + } + + function fileRow(file, depth, label) { + const selected = file === current; + return el("button", { + class: rowClass(fileState(file), selected), + type: "button", + style: `--depth: ${depth * ROW_INDENT}px`, + title: fileTitle(file), + "aria-current": selected ? "true" : null, + onclick: () => choose(file), + }, [ + el("span", { class: "tree-spacer" }), + icon("#i-code"), + el("span", { class: "tree-name", text: label }), + fileBadge(file), + ]); + } + + function folderRows(folder, depth) { + const rows = []; + for (const child of sortedFolders(folder)) { + const expanded = open.has(child.path); + rows.push(folderRow(child, depth, expanded)); + if (expanded) { + rows.push(...folderRows(child, depth + 1)); + } + } + rows.push(...sortedFiles(folder).map((file) => fileRow(file, depth, baseName(file.path)))); + return rows; + } + + function matchingRows(query) { + const matches = order.filter((file) => file.path.toLowerCase().includes(query)); + if (matches.length === 0) { + return [el("p", { class: "tree-empty", text: "No matches" })]; + } + return matches.map((file) => fileRow(file, 0, file.path)); + } + + function paintList() { + const query = filter.value.trim().toLowerCase(); + list.replaceChildren(...(query ? matchingRows(query) : folderRows(buildTree(files), 0))); + } + + function revealCurrent() { + if (current) { + ancestors(current.path).forEach((path) => open.add(path)); + } + } + + function showCurrentRow() { + const row = list.querySelector(".is-current"); + if (row && explorerVisible()) { + keepRowVisible(list, row); + } + } + + function step(offset) { + if (order.length === 0) { + return; + } + const index = Math.max(0, order.indexOf(current)); + onSelect(order[(index + offset + order.length) % order.length]); + } + + setExplorer(!narrow.matches); + toggle.addEventListener("click", () => setExplorer(!explorerVisible())); + byId("nav-prev").addEventListener("click", () => step(-1)); + byId("nav-next").addEventListener("click", () => step(1)); + filter.addEventListener("input", paintList); + filter.addEventListener("keydown", (event) => { + if (event.key === "Enter") { + list.querySelector(".tree-row:not([aria-expanded])")?.click(); + } + }); + document.addEventListener("pointerdown", (event) => { + const outside = !explorer.contains(event.target) && !toggle.contains(event.target); + if (narrow.matches && explorerVisible() && outside) { + setExplorer(false); + } + }); + + return { + render(nextFiles, nextCurrent) { + files = nextFiles; + order = flatten(buildTree(files)); + open.clear(); + current = nextCurrent; + filter.value = ""; + revealCurrent(); + paintList(); + list.scrollTop = 0; + showCurrentRow(); + }, + update(nextFiles, nextCurrent) { + const moved = nextCurrent !== current; + files = nextFiles; + current = nextCurrent; + if (moved) { + revealCurrent(); + } + paintList(); + if (moved) { + showCurrentRow(); + } + }, + }; +} diff --git a/web/src/patch.js b/web/src/patch.js new file mode 100644 index 0000000000000000000000000000000000000000..df0b35f3ee3acdb62cd22d2ab7927fa66800129f --- /dev/null +++ b/web/src/patch.js @@ -0,0 +1,78 @@ +import { diffLines } from "./diff.js"; +import { el } from "./dom.js"; + +const CONTEXT = 3; +const NO_NEWLINE = "\0"; + +// One entry per line; a last line without a trailing newline carries a marker so the diff sees it. +function lineEntries(text) { + const lines = text.split("\n"); + if (lines[lines.length - 1] === "") { + lines.pop(); + } else { + lines[lines.length - 1] += NO_NEWLINE; + } + return lines; +} + +function hunkRanges(rows) { + const ranges = []; + rows.forEach((row, index) => { + if (row.kind === "same") { + return; + } + const start = Math.max(0, index - CONTEXT); + const end = Math.min(rows.length, index + CONTEXT + 1); + const last = ranges[ranges.length - 1]; + if (last && start <= last.end) { + last.end = Math.max(last.end, end); + } else { + ranges.push({ start, end }); + } + }); + return ranges; +} + +function patchLine(prefix, text) { + const missingNewline = text.endsWith(NO_NEWLINE); + const body = missingNewline ? text.slice(0, -1) : text; + return missingNewline ? `${prefix}${body}\n\\ No newline at end of file\n` : `${prefix}${body}\n`; +} + +const PREFIX = { same: " ", removed: "-", added: "+" }; + +function rangeStart(count, linesBefore) { + return count === 0 ? linesBefore : linesBefore + 1; +} + +function renderHunk(rows, { start, end }) { + const slice = rows.slice(start, end); + const oldBefore = rows.slice(0, start).filter((row) => row.kind !== "added").length; + const newBefore = rows.slice(0, start).filter((row) => row.kind !== "removed").length; + const oldCount = slice.filter((row) => row.kind !== "added").length; + const newCount = slice.filter((row) => row.kind !== "removed").length; + const oldRange = `${rangeStart(oldCount, oldBefore)},${oldCount}`; + const newRange = `${rangeStart(newCount, newBefore)},${newCount}`; + const lines = slice.map((row) => patchLine(PREFIX[row.kind], row.text)).join(""); + return `@@ -${oldRange} +${newRange} @@\n${lines}`; +} + +export function filePatch(path, before, after) { + const rows = diffLines(lineEntries(before).join("\n"), lineEntries(after).join("\n")); + const ranges = hunkRanges(rows); + if (ranges.length === 0) { + return ""; + } + const header = `diff --git a/${path} b/${path}\n--- a/${path}\n+++ b/${path}\n`; + return header + ranges.map((range) => renderHunk(rows, range)).join(""); +} + +export function buildPatch(files) { + return files.map((file) => filePatch(file.path, file.original, file.content)).join(""); +} + +export function downloadText(text, filename) { + const url = URL.createObjectURL(new Blob([text], { type: "text/x-diff" })); + el("a", { href: url, download: filename }).click(); + setTimeout(() => URL.revokeObjectURL(url), 0); +} diff --git a/web/src/pull-request.js b/web/src/pull-request.js new file mode 100644 index 0000000000000000000000000000000000000000..b922645a65e2ac4fbaee3750979208667df52312 --- /dev/null +++ b/web/src/pull-request.js @@ -0,0 +1,99 @@ +import { toHundred } from "./labels.js"; +import { apiUrl } from "./refiner.js"; + +const MESSAGE_TYPE = "codehygiene:github"; +const SIGN_IN_TIMEOUT_MS = 5 * 60 * 1000; +const POPUP_CHECK_MS = 500; +const POPUP_FEATURES = "popup,width=720,height=800"; +const MAX_PR_FILES = 20; + +export class PrFailure extends Error { + constructor(code) { + super(code); + this.code = code; + } +} + +// The sign-in pop-up posts a one-time ticket back to this page only (same origin, same window). +function waitForTicket(popup) { + return new Promise((resolve, reject) => { + const startedAt = Date.now(); + let timer = 0; + const finish = (settle, value) => { + clearInterval(timer); + window.removeEventListener("message", onMessage); + settle(value); + }; + function onMessage(event) { + const fromPopup = event.source === popup && event.origin === window.location.origin; + if (!fromPopup || event.data?.type !== MESSAGE_TYPE) { + return; + } + if (event.data.ticket) { + finish(resolve, event.data.ticket); + } else { + finish(reject, new PrFailure(event.data.error ?? "failed")); + } + } + timer = setInterval(() => { + if (popup.closed) { + finish(reject, new PrFailure("closed")); + } else if (Date.now() - startedAt > SIGN_IN_TIMEOUT_MS) { + popup.close(); + finish(reject, new PrFailure("login_required")); + } + }, POPUP_CHECK_MS); + window.addEventListener("message", onMessage); + }); +} + +function prFiles(files) { + return files.slice(0, MAX_PR_FILES).map((file) => ({ + path: file.path, + content_after: file.content, + score_before: toHundred(file.originalResult.score), + score_after: toHundred(file.result.score), + squiggles_fixed: Math.max(0, file.originalResult.squiggles.length - file.result.squiggles.length), + })); +} + +async function readReply(response) { + const text = await response.text(); + try { + return JSON.parse(text); + } catch { + return { code: "failed" }; + } +} + +// Must be called straight from a click so the pop-up is not blocked. +export async function openPullRequest({ spec, commitSha, files }) { + const popup = window.open(apiUrl("github/login"), "codehygiene-github", POPUP_FEATURES); + if (!popup) { + throw new PrFailure("popup_blocked"); + } + const ticket = await waitForTicket(popup); + const response = await fetch(apiUrl("github/create-pr"), { + method: "POST", + headers: { "Content-Type": "application/json" }, + body: JSON.stringify({ + ticket, + owner: spec.owner, + repo: spec.repo, + base_branch: spec.ref, + base_commit_sha: commitSha, + files: prFiles(files), + }), + }); + const reply = await readReply(response); + const next = response.ok ? reply.url : reply.install_url; + if (next) { + popup.location.href = next; + } else { + popup.close(); + } + if (!response.ok) { + throw new PrFailure(reply.code ?? "failed"); + } + return reply; +} diff --git a/web/src/ranking.js b/web/src/ranking.js new file mode 100644 index 0000000000000000000000000000000000000000..d7a582abb69868a7e89fb200332533c26ec6d4cd --- /dev/null +++ b/web/src/ranking.js @@ -0,0 +1,45 @@ +// Ranks the loaded GitHub repo. The server downloads and scores the repo itself; +// the page only sends the owner and name. +import { RANK_ERRORS } from "./labels.js"; +import { apiUrl } from "./refiner.js"; + +const PERCENT_FROM = 100; + +export class RankFailure extends Error { + constructor(code, detail = {}) { + super(code); + this.code = code; + this.detail = detail; + } +} + +export async function requestRank(spec) { + const response = await fetch(apiUrl("leaderboard/rank"), { + method: "POST", + headers: { "Content-Type": "application/json" }, + body: JSON.stringify({ owner: spec.owner, repo: spec.repo }), + }); + const body = await response.json().catch(() => ({})); + if (!response.ok) { + throw new RankFailure(body.code ?? "failed", body); + } + return body; +} + +export function rankErrorText(error) { + if (error.code === "duplicate" && error.detail.repo) { + return `Ranked as ${error.detail.repo}`; + } + return RANK_ERRORS[error.code] ?? RANK_ERRORS.failed; +} + +export function rankLabel({ rank, total }) { + if (total < PERCENT_FROM) { + return `#${rank} of ${total}`; + } + return `Top ${Math.max(1, Math.ceil((rank / total) * 100))}%`; +} + +export function boardLink(repo) { + return `leaderboard/#repo=${encodeURIComponent(repo)}`; +} diff --git a/web/src/refiner.js b/web/src/refiner.js new file mode 100644 index 0000000000000000000000000000000000000000..29104183d73025fe16679f7e83cbbb45169a6c1a --- /dev/null +++ b/web/src/refiner.js @@ -0,0 +1,92 @@ +export const DEFAULT_MAX_FILE_CHARS = 12000; + +// Pages below the site root (the leaderboard) set data-root="../" so API calls still reach /api. +const SITE_ROOT = new URL(document.documentElement.dataset.root ?? "./", document.baseURI); + +export function apiUrl(path) { + return new URL(`api/${path}`, SITE_ROOT).href; +} + +async function getJson(path, label) { + const response = await fetch(apiUrl(path), { cache: "no-store" }); + if (!response.ok) { + throw new Error(`${label} failed (${response.status})`); + } + return response.json(); +} + +export function fetchHealth() { + return getJson("health", "Health check"); +} + +export function fetchStats() { + return getJson("stats", "Stats request"); +} + +export function fetchLeaderboard() { + return getJson("leaderboard", "Leaderboard request"); +} + +function parseFrame(frame) { + let name = "message"; + const dataLines = []; + for (const line of frame.split("\n")) { + if (line.startsWith(":")) { + continue; + } + const colon = line.indexOf(":"); + const field = colon < 0 ? line : line.slice(0, colon); + const value = colon < 0 ? "" : line.slice(colon + 1).replace(/^ /, ""); + if (field === "event") { + name = value; + } else if (field === "data") { + dataLines.push(value); + } + } + if (dataLines.length === 0) { + return null; + } + return { name, data: JSON.parse(dataLines.join("\n")) }; +} + +async function responseError(response) { + const body = await response.text(); + try { + const parsed = JSON.parse(body); + return parsed.detail || parsed.message || `Request failed (${response.status})`; + } catch { + return body || `Request failed (${response.status})`; + } +} + +export async function streamFix(body, { signal, onEvent }) { + const response = await fetch(apiUrl("fix/stream"), { + method: "POST", + headers: { "Content-Type": "application/json", Accept: "text/event-stream" }, + body: JSON.stringify(body), + signal, + }); + if (!response.ok || !response.body) { + onEvent("error", { code: "http", message: await responseError(response) }); + return; + } + + const reader = response.body.pipeThrough(new TextDecoderStream()).getReader(); + let buffer = ""; + for (;;) { + const { value, done } = await reader.read(); + if (done) { + break; + } + buffer = (buffer + value).replace(/\r\n?/g, "\n"); + let boundary = buffer.indexOf("\n\n"); + while (boundary >= 0) { + const event = parseFrame(buffer.slice(0, boundary)); + buffer = buffer.slice(boundary + 2); + if (event) { + onEvent(event.name, event.data); + } + boundary = buffer.indexOf("\n\n"); + } + } +} diff --git a/web/src/reward-facts.json b/web/src/reward-facts.json new file mode 100644 index 0000000000000000000000000000000000000000..0e3e7cdecf9ad4f8779f667e952afb1bd370e621 --- /dev/null +++ b/web/src/reward-facts.json @@ -0,0 +1,20 @@ +{ + "0.5.0": { + "ref": "0221af718b51a56b6ee0fbea52f0bf42afa381bb", + "facts": [ + { "label": "Gaming gain", "value": "+0.007", "title": "Largest rl_reward gain from an adversarial probe (40 padding helpers)" }, + { "label": "Held-out issues", "value": "2.0 / 100 lines", "title": "25 unmodified CPython 3.13 stdlib modules" }, + { "label": "Python vs browser", "value": "Δ 0.0000", "title": "Largest score difference between the Python and JavaScript scorers" }, + { "label": "Model", "value": "14.47 KiB", "title": "1.58-bit weights, NumPy only" } + ] + }, + "0.5.1": { + "ref": "b97871b", + "facts": [ + { "label": "Gaming gain", "value": "+0.007", "title": "Largest rl_reward gain from an adversarial probe (40 padding helpers)" }, + { "label": "Held-out issues", "value": "1.9 / 100 lines", "title": "25 unmodified CPython 3.13 stdlib modules" }, + { "label": "Python vs browser", "value": "Δ 0.0000", "title": "Largest score difference between the Python and JavaScript scorers" }, + { "label": "Model", "value": "14.85 KiB", "title": "1.58-bit weights, NumPy only" } + ] + } +} diff --git a/web/src/sample.js b/web/src/sample.js new file mode 100644 index 0000000000000000000000000000000000000000..534b5c306f2ad34bf057cf05a1906408799257e2 --- /dev/null +++ b/web/src/sample.js @@ -0,0 +1,33 @@ +import presets from "shared:presets"; +import { byId } from "./dom.js"; +import { scoreWord, toHundred } from "./labels.js"; +import { diffNodes } from "./stage.js"; + +const FILENAME = "send_receipt.py"; +const CLEANED = `import logging + +logger = logging.getLogger(__name__) + + +def send_receipt(order: Order) -> None: + if order is None or not order.paid or order.refunded: + return + email = order.email + if not email or not email.strip(): + return + try: + mailer.send(email, order.id) + except Exception: + logger.exception("Could not send receipt for order %s", order.id)`; + +export async function paintSample(scoreAll) { + const original = presets.find((preset) => preset.filename === FILENAME).before; + byId("sample-code").replaceChildren(...diffNodes({ base: original, next: CLEANED, filename: FILENAME })); + const [result] = await scoreAll([CLEANED]); + const points = toHundred(result.score); + const { word, className } = scoreWord(points); + const wordNode = byId("sample-word"); + byId("sample-value").textContent = String(points); + wordNode.textContent = word; + wordNode.className = `stage-score-word ${className}`.trim(); +} diff --git a/web/src/score-worker.js b/web/src/score-worker.js new file mode 100644 index 0000000000000000000000000000000000000000..26a8d9560ce7e9d1f4fa824f40ca2670c2f0c005 --- /dev/null +++ b/web/src/score-worker.js @@ -0,0 +1,21 @@ +import { loadWeights, scoreCode } from "shared:engine"; + +const WARMUP_CODE = ["def warm(items):", " for item in items:", " return item", ""].join("\n"); + +const weightsReady = loadWeights(new URL("./weights.npz", import.meta.url).href).then((weights) => { + scoreCode(WARMUP_CODE, weights); + return weights; +}); + +globalThis.addEventListener("message", async (event) => { + const { id, code, threshold } = event.data; + try { + const weights = await weightsReady; + const started = performance.now(); + const { score, squiggles } = scoreCode(code, weights, threshold); + const ms = performance.now() - started; + globalThis.postMessage({ id, result: { score, squiggles, ms } }); + } catch (error) { + globalThis.postMessage({ id, error: String(error && error.message ? error.message : error) }); + } +}); diff --git a/web/src/scoring.js b/web/src/scoring.js new file mode 100644 index 0000000000000000000000000000000000000000..2b54760d0bc1d592d404661a42a092a81bbcc16d --- /dev/null +++ b/web/src/scoring.js @@ -0,0 +1,63 @@ +export const THRESHOLD = 0.55; + +const WORKER_URL = new URL("./score-worker.js", import.meta.url); + +class ScoreWorker { + constructor() { + this.worker = new Worker(WORKER_URL, { type: "module" }); + this.pending = new Map(); + this.nextId = 1; + this.worker.addEventListener("message", (event) => this.settle(event.data)); + } + + settle({ id, result, error }) { + const job = this.pending.get(id); + this.pending.delete(id); + if (error) { + job.reject(new Error(error)); + } else { + job.resolve(result); + } + } + + score(code) { + const id = this.nextId; + this.nextId += 1; + return new Promise((resolve, reject) => { + this.pending.set(id, { resolve, reject }); + this.worker.postMessage({ id, code, threshold: THRESHOLD }); + }); + } +} + +export function createScorer() { + const cores = navigator.hardwareConcurrency || 4; + const workers = Array.from({ length: Math.min(4, Math.max(2, cores - 1)) }, () => new ScoreWorker()); + let turn = 0; + + function scoreOne(code) { + turn = (turn + 1) % workers.length; + return workers[turn].score(code); + } + + async function scoreAll(codes, onProgress = () => {}) { + const results = new Array(codes.length); + let cursor = 0; + let completed = 0; + + async function drain(worker) { + while (cursor < codes.length) { + const index = cursor; + cursor += 1; + results[index] = await worker.score(codes[index]); + completed += 1; + onProgress(completed, codes.length); + } + } + + await Promise.all(workers.map(drain)); + return results; + } + + return { scoreOne, scoreAll }; +} diff --git a/web/src/sources.js b/web/src/sources.js new file mode 100644 index 0000000000000000000000000000000000000000..de796b8678394f0f88a8f9228a2674381937f205 --- /dev/null +++ b/web/src/sources.js @@ -0,0 +1,89 @@ +import presets from "shared:presets"; +import { RepoError, fetchRepoFiles, isReadableText, isTestPath, selectLocalEntries } from "./github.js"; +import { sniffExtension } from "./languages.js"; + +const EXAMPLE_ORDER = ["send_receipt.py", "webhook.go", "engine.js", "pricing.ts", "frame.rs"]; +export const RUN_SIZE = 5; + +function makeFile(path, content, result) { + return { + path, + original: content, + content, + originalResult: result, + result, + cleaned: false, + kept: false, + lastRejected: false, + attempted: false, + restingText: null, + }; +} + +export function isSloppy(file) { + return file.result.squiggles.length > 0; +} + +function byScore(left, right) { + return left.result.score - right.result.score || left.path.localeCompare(right.path); +} + +export function eligibleFiles(files, maxChars) { + const fits = (file) => !file.attempted && file.content.length <= maxChars; + const sloppy = files.filter((file) => isSloppy(file) && fits(file)); + const nonTest = sloppy.filter((file) => !isTestPath(file.path)); + return (nonTest.length > 0 ? nonTest : sloppy).sort(byScore); +} + +function scoredFiles(entries, results) { + const files = entries.map((entry, index) => makeFile(entry.path, entry.content, results[index])); + return files.sort(byScore); +} + +export async function loadExamples(scoreAll) { + const ordered = EXAMPLE_ORDER.map((name) => presets.find((preset) => preset.filename === name)); + const results = await scoreAll(ordered.map((preset) => preset.before)); + return ordered.map((preset, index) => makeFile(preset.filename, preset.before, results[index])); +} + +export async function loadRepo(spec, { signal, scoreAll, onProgress, pinCommit }) { + const fetched = await fetchRepoFiles(spec, { + signal, + pinCommit, + focusPath: spec.blobPath, + onProgress: (done, total) => onProgress(`Fetching ${done} of ${total}`), + }); + const results = await scoreAll( + fetched.files.map((entry) => entry.content), + (done, total) => onProgress(`Scoring ${done} of ${total}`), + ); + signal.throwIfAborted(); + return { files: scoredFiles(fetched.files, results), commitSha: fetched.commitSha }; +} + +function relativePath(file) { + const parts = (file.webkitRelativePath || file.name).split("/"); + return parts.length > 1 ? parts.slice(1).join("/") : parts[0]; +} + +export async function loadFolder(fileList, { scoreAll, onProgress }) { + const byPath = new Map([...fileList].map((file) => [relativePath(file), file])); + const entries = selectLocalEntries([...byPath].map(([path, file]) => ({ path, size: file.size }))); + const texts = await Promise.all(entries.map((entry) => byPath.get(entry.path).text())); + const readable = entries + .map((entry, index) => ({ path: entry.path, content: texts[index] })) + .filter((entry) => isReadableText(entry.content)); + if (readable.length === 0) { + throw new RepoError("no_files", "No code files found"); + } + const results = await scoreAll( + readable.map((entry) => entry.content), + (done, total) => onProgress(`Scoring ${done} of ${total}`), + ); + return scoredFiles(readable, results); +} + +export async function loadSnippet(text, scoreAll) { + const [result] = await scoreAll([text]); + return [makeFile(`snippet.${sniffExtension(text)}`, text, result)]; +} diff --git a/web/src/stage.js b/web/src/stage.js new file mode 100644 index 0000000000000000000000000000000000000000..f43bbb032f483570faeaaa61064943bb27c7d2b7 --- /dev/null +++ b/web/src/stage.js @@ -0,0 +1,226 @@ +import { scorerSpanToOffsets } from "./columns.js"; +import { diffLines } from "./diff.js"; +import { byId, el, prefersReducedMotion, tweenNumber } from "./dom.js"; +import { tokenizeLines } from "./highlight.js"; +import { NOTES, STATUS, scoreWord } from "./labels.js"; + +const NOTE_STAGGER_MS = 30; +const NOTE_STAGGER_STEPS = 7; +const BUSY_STATUSES = new Set([STATUS.reading, STATUS.rewriting]); +const SCORE_TWEEN_MS = 600; +const TAB_COLUMNS = 4; + +function leadingColumns(text) { + let columns = 0; + for (const char of text) { + if (char === " ") { + columns += 1; + } else if (char === "\t") { + columns += TAB_COLUMNS; + } else { + break; + } + } + return columns; +} + +function textNodes(text, tokens, start, end) { + const nodes = []; + let pos = start; + for (const token of tokens) { + if (token.to <= start || token.from >= end) { + continue; + } + const from = Math.max(token.from, start); + const to = Math.min(token.to, end); + if (from > pos) { + nodes.push(text.slice(pos, from)); + } + nodes.push(el("span", { class: token.className, text: text.slice(from, to) })); + pos = to; + } + if (pos < end) { + nodes.push(text.slice(pos, end)); + } + return nodes; +} + +function leadingLength(text) { + return text.length - text.trimStart().length; +} + +function bodyNodes(text, tokens, start, squiggle) { + const from = squiggle ? Math.max(squiggle.from, start) : start; + if (!squiggle || squiggle.to <= from) { + return textNodes(text, tokens, start, text.length); + } + return [ + ...textNodes(text, tokens, start, from), + el("span", { class: "squiggle" }, textNodes(text, tokens, from, squiggle.to)), + ...textNodes(text, tokens, squiggle.to, text.length), + ]; +} + +function srcNode(text, tokens, squiggle) { + const lead = leadingLength(text); + const children = bodyNodes(text, tokens, lead, squiggle); + if (lead > 0) { + children.unshift(el("span", { class: "lead", text: text.slice(0, lead) })); + } + return el("span", { class: "src", style: `--indent: ${leadingColumns(text)}` }, children); +} + +function lineNode({ number, text, tokens, className = "line", squiggle = null, note = "" }) { + return el("div", { class: className }, [ + el("span", { class: "ln", text: number === null ? "" : String(number) }), + srcNode(text, tokens, squiggle), + note ? el("span", { class: "note", text: note }) : "", + ]); +} + +function flaggedLines(lines, squiggles) { + const byLine = new Map(); + let previous = null; + for (const squiggle of squiggles) { + const text = lines[squiggle.line - 1]; + if (text === undefined) { + continue; + } + const continues = previous && previous.kind === squiggle.kind && previous.line === squiggle.line - 1; + byLine.set(squiggle.line, { + span: scorerSpanToOffsets(text, squiggle.col_start, squiggle.col_end), + note: continues ? "" : NOTES[squiggle.kind], + }); + previous = squiggle; + } + return byLine; +} + +function paintLines(container, { text, filename, squiggles, enter }) { + const lines = text.split("\n"); + const tokens = tokenizeLines(text, filename); + const flagged = flaggedLines(lines, squiggles); + const stagger = enter && !prefersReducedMotion(); + let order = 0; + const nodes = lines.map((lineText, index) => { + const flag = flagged.get(index + 1); + const node = lineNode({ + number: index + 1, + text: lineText, + tokens: tokens[index], + className: flag ? "line is-flagged" : "line", + squiggle: flag?.span, + note: flag?.note, + }); + if (flag && stagger) { + node.style.setProperty("--delay", `${Math.min(order, NOTE_STAGGER_STEPS) * NOTE_STAGGER_MS}ms`); + order += 1; + } + return node; + }); + container.classList.toggle("is-entering", stagger); + container.replaceChildren(...nodes); +} + +function scrollIntoStage(code, line) { + const offset = line.getBoundingClientRect().top - code.getBoundingClientRect().top + code.scrollTop; + code.scrollTop = Math.max(0, offset - code.clientHeight / 3); +} + +function readingClass(row, reading) { + if (!reading || row.kind !== "same") { + return "line"; + } + const lineNumber = row.newIndex + 1; + return lineNumber >= reading.first && lineNumber <= reading.last ? "line is-reading" : "line"; +} + +export function diffNodes(options) { + const { base, next, filename, reading = null } = options; + const { squiggles = [], squigglesOn = "base" } = options; + const onNext = squigglesOn === "next"; + const flagged = flaggedLines((onNext ? next : base).split("\n"), squiggles); + const flagFor = (row) => flagged.get((onNext ? row.newIndex : row.oldIndex) + 1); + const oldTokens = tokenizeLines(base, filename); + const newTokens = tokenizeLines(next, filename); + return diffLines(base, next).map((row) => { + if (row.kind === "removed") { + const tokens = oldTokens[row.oldIndex]; + return lineNode({ number: null, text: row.text, tokens, className: "line is-removed" }); + } + const added = row.kind === "added"; + const flag = added && !onNext ? null : flagFor(row); + const rowClass = added ? "line is-added" : readingClass(row, reading); + return lineNode({ + number: row.newIndex + 1, + text: row.text, + tokens: newTokens[row.newIndex], + className: flag ? `${rowClass} is-flagged` : rowClass, + squiggle: flag?.span, + note: flag?.note, + }); + }); +} + +export function createStage() { + const path = byId("stage-path"); + const status = byId("stage-status"); + const code = byId("stage-code"); + const scoreBox = byId("stage-score"); + let shownPoints = null; + + function setStatus(text) { + status.textContent = text; + status.toggleAttribute("data-busy", BUSY_STATUSES.has(text)); + } + + function setScore(points, animate) { + const value = scoreBox.querySelector(".stage-score-value"); + const word = scoreBox.querySelector(".stage-score-word"); + const { word: label, className } = scoreWord(points); + tweenNumber(value, shownPoints ?? points, points, animate ? SCORE_TWEEN_MS : 0); + shownPoints = points; + word.textContent = label; + word.className = `stage-score-word ${className}`.trim(); + } + + function renderPlain({ text, filename, squiggles, enter }) { + paintLines(code, { text, filename, squiggles, enter }); + const first = code.querySelector(".is-flagged"); + if (enter) { + code.scrollTop = 0; + if (first) { + scrollIntoStage(code, first); + } + } + } + + function renderDiff(options) { + code.classList.remove("is-entering"); + code.replaceChildren(...diffNodes(options)); + const target = options.followChange ? code.querySelector(".is-removed, .is-added, .is-reading") : null; + if (target) { + scrollIntoStage(code, target); + } + } + + return { + setHead({ filename, statusText, points, animate = false }) { + const slash = filename.lastIndexOf("/"); + path.title = filename; + path.replaceChildren( + slash >= 0 ? el("span", { class: "path-dir", text: filename.slice(0, slash + 1) }) : "", + filename.slice(slash + 1), + ); + setStatus(statusText); + setScore(points, animate); + }, + setStatus, + setScore, + resetScore() { + shownPoints = null; + }, + renderPlain, + renderDiff, + }; +} diff --git a/web/src/styles.css b/web/src/styles.css new file mode 100644 index 0000000000000000000000000000000000000000..dd41750cbfca824a729aec00b015adedfc5b93d9 --- /dev/null +++ b/web/src/styles.css @@ -0,0 +1,1662 @@ +@font-face { + font-family: "Host Grotesk"; + font-style: normal; + font-weight: 300 800; + font-display: swap; + src: url("@fontsource-variable/host-grotesk/files/host-grotesk-latin-wght-normal.woff2") format("woff2"); +} + +@font-face { + font-family: "Google Sans Code"; + font-style: normal; + font-weight: 300 800; + font-display: swap; + src: url("@fontsource-variable/google-sans-code/files/google-sans-code-latin-wght-normal.woff2") format("woff2"); +} + +:root { + color-scheme: light dark; + + --page: light-dark(#f7f7f5, #161615); + --surface: light-dark(#ffffff, #1c1c1b); + --sunken: light-dark(#fafaf8, #191918); + --fill: light-dark(#efeeeb, #262625); + --fill-hover: light-dark(#e7e6e2, #2e2e2c); + --hover: light-dark(rgba(28, 28, 26, 0.045), rgba(255, 255, 250, 0.045)); + --selected: light-dark(rgba(28, 28, 26, 0.075), rgba(255, 255, 250, 0.075)); + --line: light-dark(rgba(28, 28, 26, 0.085), rgba(255, 255, 250, 0.075)); + --line-strong: light-dark(rgba(28, 28, 26, 0.15), rgba(255, 255, 250, 0.13)); + + --ink: light-dark(#1c1c1a, #ecebe6); + --ink-2: light-dark(#4d4c48, #b5b3ac); + --ink-3: light-dark(#6e6c66, #918f89); + --ink-faint: light-dark(#adaba5, #5d5c58); + + --red: light-dark(#c8383e, #f2777b); + --green: light-dark(#2c7a4d, #86c69a); + --bg-del: light-dark(rgba(200, 56, 62, 0.065), rgba(242, 119, 123, 0.075)); + --bg-add: light-dark(rgba(44, 122, 77, 0.075), rgba(134, 198, 154, 0.08)); + --bg-flag: light-dark(rgba(200, 56, 62, 0.035), rgba(242, 119, 123, 0.045)); + + --tok-comment: light-dark(#74726b, #8c8a84); + --tok-keyword: light-dark(#6a4fb0, #b9a7ea); + --tok-string: light-dark(#8a5a14, #dbb889); + --tok-number: light-dark(#1f6889, #8cc3da); + + --shadow-color: light-dark(rgba(28, 28, 26, 0.06), rgba(0, 0, 0, 0)); + --shadow-stage: 0 1px 2px var(--shadow-color), 0 12px 32px -16px var(--shadow-color); + --shadow-menu: 0 16px 40px light-dark(rgba(28, 28, 26, 0.12), rgba(0, 0, 0, 0.5)), + 0 2px 6px light-dark(rgba(28, 28, 26, 0.06), rgba(0, 0, 0, 0.3)); + --focus: light-dark(#4d4c48, #b5b3ac); + --focus-moat: var(--page); + --selection: light-dark(rgba(106, 79, 176, 0.16), rgba(185, 167, 234, 0.24)); + + --wave: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='8' height='4' viewBox='0 0 8 4'%3E%3Cpath d='M0 3 Q2 0.8 4 3 T8 3' fill='none' stroke='%23c8383e' stroke-width='1' stroke-linecap='round'/%3E%3C/svg%3E"); + + --font-sans: "Host Grotesk", -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif; + --font-mono: "Google Sans Code", ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; + --radius-sm: 6px; + --radius: 10px; + --radius-stage: 14px; + --gutter: 58px; + --motion: 140ms; + --ease: cubic-bezier(0.2, 0, 0, 1); + --ease-out: cubic-bezier(0.22, 1, 0.36, 1); + --ease-expo: cubic-bezier(0.16, 1, 0.3, 1); +} + +@media (prefers-color-scheme: dark) { + :root { + --wave: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='8' height='4' viewBox='0 0 8 4'%3E%3Cpath d='M0 3 Q2 0.8 4 3 T8 3' fill='none' stroke='%23f2777b' stroke-width='1' stroke-linecap='round'/%3E%3C/svg%3E"); + } +} + +*, +*::before, +*::after { + box-sizing: border-box; +} + +html { + -webkit-text-size-adjust: 100%; + scrollbar-width: thin; + scrollbar-color: var(--line-strong) transparent; +} + +body { + margin: 0; + background: var(--page); + color: var(--ink); + font: 400 15px/22px var(--font-sans); + -webkit-font-smoothing: antialiased; + -moz-osx-font-smoothing: grayscale; +} + +::selection { + background: var(--selection); +} + +h1, +h2, +p { + margin: 0; +} + +button, +input { + font: inherit; + color: inherit; +} + +button { + margin: 0; + padding: 0; + border: 0; + background: none; + cursor: pointer; +} + +a { + color: inherit; + text-decoration: none; +} + +:is(a, button, [tabindex="0"]):focus-visible { + outline: 2px solid transparent; + outline-offset: 2px; + box-shadow: 0 0 0 2px var(--focus-moat), 0 0 0 3.5px var(--focus); +} + +[hidden] { + display: none !important; +} + +.sr-only { + position: absolute; + width: 1px; + height: 1px; + margin: -1px; + overflow: hidden; + clip-path: inset(50%); + white-space: nowrap; +} + +.sprite { + position: absolute; + width: 0; + height: 0; +} + +.icon { + display: block; + width: 16px; + height: 16px; + fill: none; + stroke: currentColor; + stroke-width: 1.5; + stroke-linecap: round; + stroke-linejoin: round; +} + +.icon-button { + display: inline-grid; + flex: none; + place-items: center; + width: 32px; + height: 32px; + border-radius: 8px; + color: var(--ink-3); + transition: + background-color var(--motion) var(--ease), + color var(--motion) var(--ease), + transform 160ms var(--ease-out); +} + +.icon-button:hover { + background: var(--hover); + color: var(--ink); + transition-duration: 0s, 0s, 160ms; +} + +.icon-button:active { + background: var(--selected); + transform: scale(0.94); + transition-duration: 0s, 0s, 100ms; +} + +.icon-button[aria-label="Copied"] { + color: var(--green); +} + +.icon-button[aria-label="Copied"] .icon { + animation: copied-pop 220ms var(--ease-out) both; +} + +.icon-button[aria-label="Copy failed"] { + color: var(--red); +} + +.icon-button.is-done { + color: var(--green); +} + +.icon-button.is-busy { + color: var(--ink); + cursor: progress; + animation: breathe 1.2s ease-in-out infinite; +} + +body:has(> .page) { + display: flex; + flex-direction: column; + height: 100svh; +} + +.page { + display: flex; + flex: 1 1 auto; + flex-direction: column; + width: 100%; + min-height: 600px; + max-width: 1120px; + margin: 0 auto; + padding: 0 24px; +} + +.banner { + flex: none; + border-bottom: 1px solid var(--line); + color: var(--ink-3); + font-size: 12.5px; + line-height: 18px; +} + +.banner-inner { + display: flex; + align-items: center; + justify-content: space-between; + gap: 16px; + max-width: 1120px; + height: 36px; + margin: 0 auto; + padding: 0 24px; +} + +.banner-stats { + display: flex; + align-items: center; + gap: 20px; + min-width: 0; + border-radius: var(--radius-sm); + white-space: nowrap; + transition: color var(--motion) var(--ease); +} + +.banner-stats::before { + content: ""; + flex: none; + width: 6px; + height: 6px; + margin-right: -12px; + border-radius: 50%; + background: var(--green); +} + +.banner-stats:hover { + color: var(--ink-2); +} + +.banner-stat b { + color: var(--ink); + font-weight: 500; + font-variant-numeric: tabular-nums; +} + +.banner-board { + display: inline-flex; + flex: none; + align-items: center; + gap: 6px; + margin-left: auto; + border-radius: var(--radius-sm); + color: var(--ink); +} + +.banner-board .icon { + width: 14px; + height: 14px; +} + +.banner-board .banner-board-arrow { + width: 10px; + height: 10px; + color: var(--ink-3); + transition: color var(--motion) var(--ease), transform 160ms var(--ease-out); +} + +.banner-board:hover .banner-board-arrow { + color: var(--ink); + transform: translate(1px, -1px); +} + +.banner:not([hidden]) + .page .intro { + padding-top: 44px; +} + +.rank-pill { + display: inline-flex; + flex: none; + align-items: center; + gap: 6px; + height: 28px; + margin-right: 2px; + padding: 0 10px; + border-radius: 999px; + background: var(--fill); + color: var(--ink); + font-size: 12.5px; + font-weight: 500; + font-variant-numeric: tabular-nums; + white-space: nowrap; + transition: background-color var(--motion) var(--ease); +} + +.rank-pill:hover { + background: var(--fill-hover); +} + +.rank-pill .icon { + width: 14px; + height: 14px; + color: var(--green); +} + +main { + display: flex; + flex: 1; + flex-direction: column; + min-height: 0; +} + +.intro { + position: relative; + padding-top: 64px; +} + +.headline { + text-align: center; + font-size: 30px; + line-height: 36px; + font-weight: 500; + letter-spacing: -0.025em; + text-wrap: balance; +} + +.headline-link { + border-radius: var(--radius-sm); + text-decoration: underline 1.5px transparent; + text-underline-offset: 5px; + transition: text-decoration-color var(--motion) var(--ease); +} + +.headline-link:hover { + text-decoration-color: var(--line-strong); +} + +.headline-arrow { + display: inline-block; + width: 0.4em; + height: 0.4em; + margin-left: 0.1em; + vertical-align: 0.34em; + overflow: visible; + color: var(--ink-3); + stroke-width: 1.75; + transition: color var(--motion) var(--ease), transform 160ms var(--ease-out); +} + +.headline-link:hover .headline-arrow { + color: var(--ink); + transform: translate(1px, -1px); +} + +.sources { + display: flex; + align-items: center; + justify-content: center; + gap: 16px; + margin-top: 28px; +} + +.source { + width: min(420px, 100%); +} + +.sources-or { + color: var(--ink-3); + font-size: 13px; +} + +.source-field { + --focus-moat: var(--surface); + position: relative; + flex: 1; + min-width: 0; +} + +.source-icon { + position: absolute; + top: 13px; + left: 16px; + width: 18px; + height: 18px; + color: var(--ink-3); + pointer-events: none; + transition: color var(--motion) var(--ease); +} + +.source-field:focus-within .source-icon, +.source-field:has(.source-input:not(:placeholder-shown)) .source-icon { + color: var(--ink); +} + +.intro:has(.source-progress:not([hidden])) .source-icon { + animation: breathe 1.8s ease-in-out infinite; +} + +.source-input { + width: 100%; + height: 44px; + padding: 0 48px 0 46px; + border: 1px solid var(--line-strong); + border-radius: var(--radius); + background: var(--surface); + box-shadow: 0 1px 2px var(--shadow-color); + transition: border-color var(--motion) var(--ease), box-shadow var(--motion) var(--ease); +} + +.source-input::placeholder { + color: var(--ink-3); +} + +.source-input:hover { + border-color: var(--ink-faint); +} + +.source-input:focus-visible { + outline: none; + border-color: var(--ink-3); + box-shadow: 0 0 0 1px var(--ink-3), 0 0 0 5px var(--hover); +} + +.intro:has(.source-error:not([hidden])) .source-input { + border-color: var(--red); + box-shadow: 0 0 0 3px color-mix(in srgb, var(--red) 14%, transparent); +} + +.source-submit { + position: absolute; + top: 6px; + right: 6px; + color: var(--ink); + transition: + opacity 180ms var(--ease-out), + transform 180ms var(--ease-out), + visibility 180ms, + background-color var(--motion) var(--ease), + color var(--motion) var(--ease); +} + +.source-input:placeholder-shown ~ .source-submit { + visibility: hidden; + opacity: 0; + transform: translateX(-6px); + pointer-events: none; +} + +.ghost.source-upload { + display: inline-flex; + flex: none; + align-items: center; + gap: 8px; + height: 44px; + padding: 0 18px 0 16px; + font-weight: 500; +} + +.source-upload .icon { + width: 18px; + height: 18px; + color: var(--ink-2); +} + +.source-progress, +.source-error { + position: absolute; + top: 100%; + right: 0; + left: 0; + margin-top: 12px; + text-align: center; + font-size: 13px; + line-height: 20px; + transition: opacity 180ms var(--ease-out), transform 180ms var(--ease-out); +} + +@starting-style { + .source-progress, + .source-error { + opacity: 0; + transform: translateY(-3px); + } +} + +.source-progress { + color: var(--ink-3); + font-variant-numeric: tabular-nums; +} + +.source-error { + color: var(--red); +} + +.run { + display: flex; + flex: 1; + flex-direction: column; + min-height: 0; + margin-top: 44px; +} + +.sample, +.stage { + view-transition-name: workspace; +} + +.sample { + --focus-moat: var(--page); + position: relative; + display: flex; + flex: 0 1 auto; + flex-direction: column; + min-height: 0; + border: 1px solid var(--line-strong); + border-radius: var(--radius-stage); + background: var(--surface); + box-shadow: var(--shadow-stage); + overflow: hidden; + transition: border-color var(--motion) var(--ease); +} + +.sample:hover { + border-color: var(--ink-faint); +} + +.sample:has(.sample-run:focus-visible) { + box-shadow: 0 0 0 2px var(--page), 0 0 0 3.5px var(--focus); +} + +.sample-head { + display: grid; + flex: none; + grid-template-columns: minmax(0, 1fr) auto minmax(0, 1fr); + align-items: center; + gap: 12px; + height: 52px; + padding: 0 18px; + border-bottom: 1px solid var(--line); +} + +.sample .sample-code { + overflow: hidden; + mask-image: linear-gradient(to bottom, #000 calc(100% - 24px), transparent); +} + +.sample-run { + position: absolute; + z-index: 2; + inset: 0; + border-radius: inherit; +} + +.sample-run:focus-visible { + box-shadow: none; +} + +::view-transition-group(workspace) { + overflow: clip; + border-radius: var(--radius-stage); + background: var(--surface); + box-shadow: inset 0 0 0 1px var(--line-strong), var(--shadow-stage); + animation-duration: 640ms; + animation-timing-function: cubic-bezier(0.32, 0.72, 0, 1); +} + +::view-transition-image-pair(workspace) { + overflow: clip; +} + +::view-transition-old(workspace), +::view-transition-new(workspace) { + width: auto; + height: auto; + object-fit: none; + object-position: top left; +} + +::view-transition-old(workspace) { + animation: workspace-out 200ms var(--ease) both; +} + +::view-transition-new(workspace) { + animation: workspace-in 360ms var(--ease-out) 240ms both; +} + +@media (max-width: 640px) { + ::view-transition-group(workspace) { + border-radius: 0; + } +} + +body:has(#source-progress:not([hidden])) :is(.stage-code, .tree-list, .sample) { + pointer-events: none; + animation: breathe 1.8s ease-in-out infinite; +} + +.stage { + --focus-moat: var(--surface); + display: grid; + flex: 1 1 0; + grid-template-columns: 240px minmax(0, 1fr); + grid-template-rows: minmax(0, 1fr) auto; + min-height: 360px; + border: 1px solid var(--line-strong); + border-radius: var(--radius-stage); + background: var(--surface); + box-shadow: var(--shadow-stage); + overflow: hidden; +} + +.stage.is-collapsed { + grid-template-columns: 0 minmax(0, 1fr); +} + +.stage.is-collapsed :is(.explorer, .tally) { + visibility: hidden; +} + +.explorer { + --focus-moat: var(--sunken); + grid-area: 1 / 1; + display: flex; + flex-direction: column; + min-width: 0; + min-height: 0; + border-right: 1px solid var(--line); + background: var(--sunken); +} + +.editor { + grid-area: 1 / 2 / 3 / 3; + display: flex; + flex-direction: column; + min-width: 0; + min-height: 0; +} + +.stage-head { + display: grid; + grid-template-columns: minmax(0, 1fr) auto minmax(0, 1fr); + align-items: center; + gap: 12px; + height: 52px; + padding: 0 10px; + border-bottom: 1px solid var(--line); +} + +.head-tools { + display: flex; + align-items: center; + justify-self: start; + gap: 8px; + min-width: 0; +} + +.head-actions { + display: flex; + align-items: center; + justify-self: end; + gap: 6px; +} + +#explorer-toggle { + position: relative; +} + +#explorer-toggle::before { + content: ""; + position: absolute; + top: calc(50% - 5.33px); + left: calc(50% - 6px); + width: 4px; + height: 10.67px; + border-radius: 1.33px 0 0 1.33px; + background: currentColor; + opacity: 0; + transition: opacity var(--motion) var(--ease); +} + +#explorer-toggle[aria-expanded="true"]::before { + opacity: 0.45; +} + +.nav { + display: flex; + align-items: center; + gap: 2px; + min-width: 0; +} + +.nav-step { + width: 28px; + height: 28px; + border-radius: var(--radius-sm); +} + +.tabs { + display: none; +} + +.nav-prev-icon { + transform: rotate(180deg); +} + +.stage-path { + min-width: 0; + padding: 0 6px; + overflow: hidden; + font: 500 14px/20px var(--font-mono); + letter-spacing: -0.01em; + text-overflow: ellipsis; + white-space: nowrap; +} + +.path-dir { + color: var(--ink-3); + font-weight: 400; +} + +.stage-meta { + display: flex; + align-items: baseline; + gap: 10px; + min-width: 0; + font-size: 14px; + line-height: 20px; + white-space: nowrap; +} + +.stage-status { + color: var(--ink-3); +} + +.stage-status[data-busy]::before { + content: ""; + display: inline-block; + width: 6px; + height: 6px; + margin-right: 8px; + border-radius: 50%; + background: currentColor; + vertical-align: 1px; + animation: breathe 1.2s ease-in-out infinite; +} + +.stage-score { + font-variant-numeric: tabular-nums; +} + +.stage-score-value { + display: inline-block; + min-width: 2ch; + text-align: right; + font-weight: 500; +} + +.stage-score-scale, +.stage-score-word { + color: var(--ink-3); +} + +.stage-score-word { + margin-left: 4px; + transition: color 600ms var(--ease-out); +} + +.stage-score-word.is-sloppy { + color: var(--red); +} + +.stage-score-word.is-clean { + color: var(--green); +} + +.tree-search { + position: relative; + flex: none; + height: 52px; + padding: 11px 10px; + border-bottom: 1px solid var(--line); +} + +.tree-search-icon { + position: absolute; + top: 19px; + left: 20px; + width: 14px; + height: 14px; + color: var(--ink-3); + pointer-events: none; +} + +.tree-filter { + width: 100%; + height: 29px; + padding: 0 10px 0 30px; + border: 1px solid transparent; + border-radius: 8px; + background: var(--hover); + font-size: 13px; + transition: background-color var(--motion) var(--ease), border-color var(--motion) var(--ease); +} + +.tree-filter::placeholder { + color: var(--ink-3); +} + +.tree-filter:hover { + background: var(--selected); +} + +.tree-filter:focus-visible { + outline: none; + border-color: var(--line-strong); + background: var(--surface); +} + +.tree-list { + flex: 1; + min-height: 0; + padding: 8px; + overflow-y: auto; + scrollbar-width: thin; + scrollbar-color: var(--line-strong) transparent; +} + +.tree-row { + display: flex; + align-items: center; + gap: 8px; + width: 100%; + height: 30px; + padding: 0 10px 0 calc(6px + var(--depth, 0px)); + border-radius: var(--radius-sm); + color: var(--ink-2); + font-size: 13.5px; + text-align: left; + transition: background-color var(--motion) var(--ease); +} + +.tree-row:hover { + background: var(--hover); + transition-duration: 0s; +} + +.tree-row:active, +.tree-row.is-current { + background: var(--selected); +} + +.tree-row.has-issues { + color: var(--red); +} + +.tree-row.is-cleaned { + color: var(--green); +} + +.tree-row .icon { + flex: none; + width: 14px; + height: 14px; + color: var(--ink-3); +} + +.tree-chevron { + transition: transform var(--motion) var(--ease); +} + +.tree-row[aria-expanded="true"] .tree-chevron { + transform: rotate(90deg); +} + +.tree-spacer { + flex: none; + width: 14px; +} + +.tree-name { + flex: 1; + min-width: 0; + overflow: hidden; + text-overflow: ellipsis; + white-space: nowrap; +} + +.tree-count { + font: 500 11.5px/16px var(--font-mono); + font-variant-numeric: tabular-nums; + opacity: 0.85; +} + +.tree-mark { + flex: none; + width: 6px; + height: 6px; + margin-right: 2px; + border-radius: 50%; + background: currentColor; +} + +.tree-empty { + padding: 6px 10px; + color: var(--ink-3); + font-size: 13px; +} + +.tally { + grid-area: 2 / 1; + min-width: 0; + padding: 12px 16px 14px; + border-top: 1px solid var(--line); + border-right: 1px solid var(--line); + background: var(--sunken); +} + +.tally-line { + display: flex; + align-items: baseline; + justify-content: space-between; + gap: 12px; + font-size: 13px; + line-height: 20px; +} + +.tally-label { + color: var(--ink-2); +} + +.tally-count { + color: var(--ink-3); + font-variant-numeric: tabular-nums; +} + +#tally-fixed { + display: inline-block; + min-width: 2ch; + color: var(--ink); + font-weight: 500; + text-align: right; +} + +.tally-bar { + height: 4px; + margin-top: 8px; + border-radius: 2px; + background: var(--selected); + overflow: hidden; +} + +.tally-fill { + display: block; + width: 0; + height: 100%; + border-radius: inherit; + background: var(--green); + transition: width 600ms var(--ease-out); +} + +.tally.is-empty .tally-count, +.tally.is-empty .tally-bar { + display: none; +} + +.stage-code { + flex: 1; + min-height: 0; + padding: 10px 0 20px; + overflow-y: auto; + scrollbar-width: thin; + scrollbar-color: var(--line-strong) transparent; + font: 400 13.5px/24px var(--font-mono); + font-variant-ligatures: none; + tab-size: 4; +} + +.stage-code:focus-visible { + outline: 2px solid transparent; + box-shadow: inset 0 0 0 1.5px var(--focus); +} + +.line { + display: grid; + grid-template-columns: var(--gutter) minmax(0, 1fr) auto; + padding-right: 20px; + transition: + background-color 400ms var(--ease), + box-shadow 240ms var(--ease); +} + +.ln { + position: relative; + padding-right: 22px; + color: color-mix(in srgb, var(--ink-3) 80%, var(--surface)); + text-align: right; + user-select: none; + font-variant-numeric: tabular-nums; +} + +.lead { + white-space: pre; +} + +.src { + padding-left: calc((var(--indent, 0) + 4) * 1ch); + text-indent: calc((var(--indent, 0) + 4) * -1ch); + white-space: pre-wrap; + overflow-wrap: anywhere; +} + +.squiggle { + padding-bottom: 3px; + background: var(--wave) repeat-x left bottom; + box-decoration-break: clone; + -webkit-box-decoration-break: clone; +} + +.note { + padding-left: 24px; + color: var(--red); + font: 400 12.5px/24px var(--font-sans); + white-space: nowrap; +} + +.line.is-flagged { + --row-tint: var(--bg-flag); + background: var(--bg-flag); +} + +.line.is-flagged .ln { + color: var(--red); +} + +.line.is-removed { + --row-tint: var(--bg-del); + --row-bar: var(--red); + background: var(--bg-del); + box-shadow: inset 2px 0 var(--red); +} + +.line.is-removed .src, +.line.is-removed .src span { + color: var(--ink-3); +} + +.line.is-added { + --row-tint: var(--bg-add); + --row-bar: var(--green); + background: var(--bg-add); + box-shadow: inset 2px 0 var(--green); +} + +.line.is-added .ln, +.line.is-reading .ln { + color: var(--ink-2); +} + +.line.is-removed .ln::after, +.line.is-added .ln::after { + position: absolute; + top: 0; + right: 7px; + content: "\2212"; + color: var(--red); + font-weight: 500; +} + +.line.is-added .ln::after { + content: "+"; + color: var(--green); +} + +.line.is-reading { + --row-tint: var(--selected); + --row-bar: var(--ink-3); + background: var(--selected); + box-shadow: inset 2px 0 var(--ink-3); +} + +.tok-com { + color: var(--tok-comment); +} + +.tok-kw { + color: var(--tok-keyword); +} + +.tok-str { + color: var(--tok-string); +} + +.tok-num { + color: var(--tok-number); +} + +.ghost { + height: 32px; + padding: 0 12px; + border-radius: var(--radius); + background: var(--fill); + color: var(--ink); + transition: + background-color var(--motion) var(--ease), + transform 160ms var(--ease-out); +} + +.ghost:hover { + background: var(--fill-hover); +} + +.ghost:active { + background: var(--fill-hover); + transform: scale(0.97); + transition-duration: 0s, 100ms; +} + +.command { + --focus-moat: var(--surface); + display: flex; + align-items: center; + gap: 4px; + min-width: 0; + height: 36px; + padding: 0 4px 0 14px; + border: 1px solid var(--line); + border-radius: var(--radius); + background: var(--surface); + color: var(--ink-2); + transition: border-color var(--motion) var(--ease); +} + +.command:hover, +.command:focus-within { + border-color: var(--line-strong); +} + +.command-text { + min-width: 0; + overflow: hidden; + font: 400 12.5px/16px var(--font-mono); + text-overflow: ellipsis; + white-space: nowrap; +} + +.command-text::before { + content: "$"; + margin-right: 1ch; + color: var(--ink-faint); + user-select: none; +} + +.command .icon-button { + width: 28px; + height: 28px; + border-radius: var(--radius-sm); +} + +.command .icon { + width: 14px; + height: 14px; +} + +.foot { + display: flex; + flex: none; + flex-wrap: wrap; + align-items: center; + justify-content: center; + gap: 6px 20px; + padding: 18px 0; + color: var(--ink-3); + font-size: 12.5px; + line-height: 18px; + text-align: center; +} + +.foot .command { + max-width: 100%; +} + +@media (min-width: 641px) { + .explorer { + transition: + opacity 220ms var(--ease-expo), + transform 220ms var(--ease-expo); + } + + .stage.is-collapsed .explorer { + opacity: 0; + transform: translateX(-8px); + transition-duration: 0s; + } +} + +@media (max-width: 640px) { + :root { + --gutter: 40px; + } + + body:has(> .page) { + height: auto; + min-height: 100svh; + } + + .page { + min-height: 0; + padding: 0 16px; + } + + .intro, + .banner:not([hidden]) + .page .intro { + padding-top: 36px; + } + + .banner-inner { + padding: 0 16px; + } + + .banner-stats { + gap: 16px; + } + + .banner-stats::before { + margin-right: -8px; + } + + .banner-stat.is-extra, + .banner-board-text, + .banner-board-arrow { + display: none; + } + + .headline { + font-size: 24px; + line-height: 30px; + } + + .sources { + flex-direction: column; + align-items: stretch; + gap: 8px; + margin-top: 20px; + } + + .sources-or { + text-align: center; + } + + .ghost.source-upload { + justify-content: center; + } + + .source-progress, + .source-error { + margin-top: 6px; + } + + .run { + flex: none; + margin-top: 32px; + } + + .foot { + flex-direction: column; + gap: 14px; + padding: 32px 0; + } + + .sample-head { + display: flex; + justify-content: space-between; + height: 44px; + padding: 0 16px; + } + + .sample-head .stage-path { + order: -1; + padding: 0; + } + + .sample-head .stage-meta { + padding: 0; + } + + .stage { + position: relative; + flex: none; + grid-template-columns: minmax(0, 1fr); + height: min(calc(100svh - 24px), 780px); + min-height: 420px; + margin: 0 -16px; + scroll-margin-top: 12px; + border-right: 0; + border-left: 0; + border-radius: 0; + box-shadow: none; + } + + .stage.is-collapsed { + grid-template-columns: minmax(0, 1fr); + } + + .editor { + grid-area: 1 / 1; + } + + .tally, + .stage.is-collapsed .tally { + grid-area: 2 / 1; + display: flex; + align-items: center; + gap: 14px; + padding: 10px 16px; + border-right: 0; + background: var(--surface); + visibility: visible; + } + + .tally-line { + flex: none; + gap: 8px; + font-size: 12.5px; + } + + .tally-bar { + flex: 1; + margin-top: 0; + } + + .stage.is-collapsed .explorer { + display: none; + opacity: 0; + transform: translateX(-16px); + transition: + opacity 160ms var(--ease), + transform 160ms var(--ease), + display 160ms allow-discrete; + } + + .explorer { + --focus-moat: var(--surface); + position: absolute; + z-index: 5; + top: 0; + bottom: 0; + left: 0; + width: min(300px, 84vw); + background: var(--surface); + box-shadow: var(--shadow-menu); + transition: + opacity 240ms var(--ease-expo), + transform 240ms var(--ease-expo), + display 240ms allow-discrete; + } + + @starting-style { + .explorer { + opacity: 0; + transform: translateX(-16px); + } + } + + .stage-head { + grid-template-columns: 48px minmax(0, 1fr) auto; + grid-template-areas: + "toggle tabs tabs" + "meta meta actions"; + gap: 0; + height: auto; + padding: 0; + background: linear-gradient(var(--line), var(--line)) 0 44px / 100% 1px no-repeat; + } + + .head-tools { + display: contents; + } + + #explorer-toggle { + grid-area: toggle; + justify-self: center; + } + + .nav { + display: none; + } + + .tabs { + position: relative; + grid-area: tabs; + display: flex; + min-width: 0; + height: 44px; + overflow-x: auto; + overscroll-behavior-x: contain; + scrollbar-width: none; + mask-image: linear-gradient(to right, #000 calc(100% - 28px), transparent); + } + + .tabs::-webkit-scrollbar { + display: none; + } + + .tab { + position: relative; + display: inline-flex; + flex: none; + align-items: center; + gap: 6px; + height: 44px; + padding: 0 12px; + color: var(--ink-3); + font-size: 13.5px; + white-space: nowrap; + } + + .tab:last-child { + margin-right: 28px; + } + + .tab.has-issues { + color: var(--red); + } + + .tab.is-cleaned { + color: var(--green); + } + + .tab.is-current { + font-weight: 500; + } + + .tab.is-current:not(.has-issues, .is-cleaned) { + color: var(--ink); + } + + .tab.is-current::after { + content: ""; + position: absolute; + right: 12px; + bottom: 0; + left: 12px; + height: 2px; + border-radius: 2px 2px 0 0; + background: currentColor; + } + + .tab-count { + font: 500 11px/16px var(--font-mono); + opacity: 0.85; + } + + .tab.is-busy .tab-name::before { + content: ""; + display: inline-block; + width: 6px; + height: 6px; + margin-right: 7px; + border-radius: 50%; + background: currentColor; + vertical-align: 1px; + animation: breathe 1.2s ease-in-out infinite; + } + + .stage-meta { + grid-area: meta; + align-self: center; + padding: 0 16px; + } + + .head-actions { + grid-area: actions; + min-height: 40px; + padding-right: 4px; + } + + .stage-code { + display: grid; + grid-template-columns: minmax(max-content, 1fr); + align-content: start; + padding: 8px 0 16px; + overflow: auto; + overscroll-behavior-x: contain; + font-size: 12.5px; + line-height: 20px; + } + + .stage-code .line { + grid-template-columns: var(--gutter) minmax(max-content, 1fr); + padding-right: 24px; + } + + .stage-code .ln { + position: sticky; + z-index: 1; + left: 0; + padding-right: 12px; + background: linear-gradient(var(--row-tint, transparent), var(--row-tint, transparent)), var(--surface); + box-shadow: inset 2px 0 var(--row-bar, transparent); + } + + .line.is-removed .ln::after { + right: 12px; + } + + .line.is-added .ln { + color: var(--green); + } + + .line.is-added .ln::after { + content: none; + } + + .lead { + font-size: 50%; + } + + .stage-code .src { + padding-left: 0; + text-indent: 0; + white-space: pre; + overflow-wrap: normal; + } + + .stage-code .note { + position: sticky; + left: var(--gutter); + grid-column: 2; + justify-self: start; + padding: 0 0 4px; + font-size: 12px; + line-height: 16px; + } +} + +@media (pointer: coarse) { + .icon-button, + .nav-step { + width: 40px; + height: 40px; + } + + .source-input, + .tree-filter { + font-size: 16px; + } + + .command { + height: 40px; + } + + .command .icon-button { + width: 36px; + height: 36px; + } + + .tree-row { + height: 38px; + } +} + +.is-entering .note { + animation: note-in 320ms var(--ease) var(--delay, 0ms) both; +} + +.is-entering .squiggle { + animation: squiggle-in 320ms var(--ease) var(--delay, 0ms) both; +} + +@keyframes note-in { + from { + opacity: 0; + transform: translateX(4px); + } +} + +@keyframes squiggle-in { + from { + background-position: left calc(100% + 6px); + } +} + +@keyframes workspace-out { + to { + opacity: 0; + } +} + +@keyframes workspace-in { + from { + opacity: 0; + } +} + +@keyframes copied-pop { + from { + opacity: 0; + transform: scale(0.6); + } +} + +@keyframes breathe { + 0%, + 100% { + opacity: 0.44; + } + + 50% { + opacity: 0.78; + } +} + +@media (prefers-reduced-motion: reduce) { + *, + *::before, + *::after { + transition-duration: 0s !important; + animation-duration: 0s !important; + } + + .intro:has(.source-progress:not([hidden])) .source-icon, + body:has(#source-progress:not([hidden])) :is(.stage-code, .tree-list, .sample), + .stage-status[data-busy]::before { + animation: none; + opacity: 0.58; + } + + .icon-button:active, + .ghost:active, + .explorer, + .stage.is-collapsed .explorer { + transform: none; + } +} diff --git a/web/src/tabs.js b/web/src/tabs.js new file mode 100644 index 0000000000000000000000000000000000000000..975fd51e78eed5810875f323d607cc05d9193919 --- /dev/null +++ b/web/src/tabs.js @@ -0,0 +1,53 @@ +import { byId, el } from "./dom.js"; +import { baseName, fileState } from "./navigator.js"; + +const EDGE = 16; + +function badge(file) { + const count = file.result.squiggles.length; + if (count > 0) { + return el("span", { class: "tab-count", text: String(count) }); + } + return file.cleaned ? el("span", { class: "tree-mark" }) : ""; +} + +function keepTabVisible(bar, tab) { + const left = tab.offsetLeft - EDGE; + const right = tab.offsetLeft + tab.offsetWidth + EDGE - bar.clientWidth; + if (bar.scrollLeft > left) { + bar.scrollTo({ left, behavior: "smooth" }); + } else if (bar.scrollLeft < right) { + bar.scrollTo({ left: right, behavior: "smooth" }); + } +} + +export function createTabs(onSelect) { + const bar = byId("tabs"); + + function tab(file, selected, busy) { + const classes = ["tab", fileState(file)]; + if (selected) { + classes.push("is-current"); + } + if (busy) { + classes.push("is-busy"); + } + return el("button", { + class: classes.join(" "), + type: "button", + title: file.path, + "aria-current": selected ? "true" : null, + onclick: () => onSelect(file), + }, [el("span", { class: "tab-name", text: baseName(file.path) }), badge(file)]); + } + + return { + render(files, current, busyFile) { + bar.replaceChildren(...files.map((file) => tab(file, file === current, file === busyFile))); + const active = bar.querySelector(".is-current"); + if (active) { + keepTabVisible(bar, active); + } + }, + }; +} diff --git a/web/src/telemetry.js b/web/src/telemetry.js new file mode 100644 index 0000000000000000000000000000000000000000..7e0333dfd16ee6052850d2c201eca1c35d37efd3 --- /dev/null +++ b/web/src/telemetry.js @@ -0,0 +1,110 @@ +// Aggregate, code-free usage reporting. Every field sent here is listed in space/TELEMETRY.md. +import { byId } from "./dom.js"; +import { NOTES, toHundred } from "./labels.js"; +import { extensionOf } from "./languages.js"; +import { apiUrl } from "./refiner.js"; + +export const MIN_VISIBLE_CLEANED = 1000; +export const MIN_VISIBLE_PRS = 20; +export const MIN_VISIBLE_TIMED = 1000; +export const MIN_BOARD_ENTRIES = 5; +const MAX_SCAN_FILES = 200; +const MAX_SCORE_MS = 60000; +const MAX_EXT_CHARS = 8; +const LOC_BUCKETS = [[50, "0-50"], [100, "50-100"], [300, "100-300"], [1000, "300-1000"]]; + +function baseExtension(path) { + const ext = extensionOf(path.slice(path.lastIndexOf("/") + 1)); + return ext.length <= MAX_EXT_CHARS ? ext : ""; +} + +function locBucket(text) { + const lines = text.split("\n").length; + const bucket = LOC_BUCKETS.find(([limit]) => lines < limit); + return bucket ? bucket[1] : "1000+"; +} + +async function digest16(ext, content) { + const bytes = new TextEncoder().encode(`${ext}\0${content}`); + const hash = new Uint8Array(await crypto.subtle.digest("SHA-256", bytes)); + return Array.from(hash.subarray(0, 8), (byte) => byte.toString(16).padStart(2, "0")).join(""); +} + +function squiggleCounts(squiggles) { + const counts = {}; + for (const { kind } of squiggles) { + if (kind in NOTES) { + counts[kind] = (counts[kind] ?? 0) + 1; + } + } + return counts; +} + +function scoreMs(result) { + return Number.isFinite(result.ms) ? Math.min(MAX_SCORE_MS, Math.round(result.ms)) : null; +} + +async function scanEntry(file) { + const ext = baseExtension(file.path); + return { + digest16: await digest16(ext, file.original), + ext, + loc_bucket: locBucket(file.original), + score_before: toHundred(file.originalResult.score), + squiggles: squiggleCounts(file.originalResult.squiggles), + score_ms: scoreMs(file.originalResult), + }; +} + +export async function reportScan(sourceKind, files) { + if (sourceKind === "examples" || files.length === 0 || !globalThis.crypto?.subtle) { + return false; + } + const entries = await Promise.all(files.slice(0, MAX_SCAN_FILES).map(scanEntry)); + const request = fetch(apiUrl("telemetry/scan"), { + method: "POST", + headers: { "Content-Type": "application/json" }, + body: JSON.stringify({ source_kind: sourceKind, files: entries }), + keepalive: true, + }); + // Reporting is best effort: a failed report must never interrupt the run. + return request.then((response) => response.ok, () => false); +} + +export function formatCount(count) { + return count.toLocaleString("en-US"); +} + +export function formatMs(ms) { + return `${formatCount(ms)} ms`; +} + +const PHONE_BANNER_STATS = 2; + +function bannerStat(id, value) { + byId(id).hidden = value === null; + if (value !== null) { + byId(`${id}-value`).textContent = value; + } + return value !== null; +} + +export function renderBanner(stats) { + const fixed = stats.files_cleaned >= MIN_VISIBLE_CLEANED ? formatCount(stats.issues_fixed) : null; + const merged = stats.prs_decided >= MIN_VISIBLE_PRS ? `${Math.round(stats.pr_merge_rate * 100)}%` : null; + const timed = stats.score_ms_samples >= MIN_VISIBLE_TIMED ? formatMs(stats.score_ms_median) : null; + const ids = ["banner-fixed", "banner-merged", "banner-speed"]; + const shown = [fixed, merged, timed].map((value, index) => bannerStat(ids[index], value)); + const visibleIds = ids.filter((_, index) => shown[index]); + for (const id of ids) { + byId(id).classList.toggle("is-extra", visibleIds.indexOf(id) >= PHONE_BANNER_STATS); + } + const anyStat = visibleIds.length > 0; + byId("banner-stats").hidden = !anyStat; + byId("banner-board").hidden = stats.leaderboard_entries < MIN_BOARD_ENTRIES; + byId("banner").hidden = !anyStat && byId("banner-board").hidden; +} + +export function hideBanner() { + byId("banner").hidden = true; +}