From 5e3bc2ca0bccf3847386df5478eb9e914c860eb5 Mon Sep 17 00:00:00 2001 From: Byron Wall Date: Sat, 21 Nov 2026 13:32:16 -0500 Subject: [PATCH 01/16] docs: Change Lens contract and shared client types (#397) Co-Authored-By: Claude Fable 5.2 --- client/src/changelens/types.ts | 190 ++++++++++++++++++++++++++++ docs/intent/change-lens/contract.md | 93 ++++++++++++++ 2 files changed, 283 insertions(+) create mode 100644 client/src/changelens/types.ts create mode 100644 docs/intent/change-lens/contract.md diff --git a/client/src/changelens/types.ts b/client/src/changelens/types.ts new file mode 100644 index 00000000..c848e48e --- /dev/null +++ b/client/src/changelens/types.ts @@ -0,0 +1,190 @@ +// Change Lens payload contract. The server builds it in +// server/app/services/changelens/; docs/intent/change-lens/contract.md explains each field. +// Field names are camelCase on the wire. + +export type LensWindow = "branch" | "24h" | "7d" | "custom"; +/** touched: union of chunks changed by the window's commits. net: base snapshot against head snapshot. */ +export type LensMode = "touched" | "net"; + +/** The literal head and commit id of uncommitted work. */ +export const WORKTREE = "WORKTREE"; + +export interface LensEndpoint { + /** What the user or preset named: a branch, `merge-base(main, HEAD)`, a sha, `WORKTREE`. */ + ref: string; + /** Null only for the working tree. */ + sha: string | null; + /** Short text for the UI, e.g. `main @ e17633f` or `Working tree`. */ + label: string; + /** ISO 8601 committer time; null for the working tree. */ + date: string | null; +} + +export interface LensSelection { + window: LensWindow; + mode: LensMode; + /** The branch or revision whose first-parent history was read. */ + ref: string; + base: LensEndpoint; + head: LensEndpoint; + /** Time limits of a time window (ISO 8601); null for `branch` and for ref-only custom ranges. */ + since: string | null; + until: string | null; + /** IANA name the server used to print times. */ + timezone: string; + /** True when staged and unstaged edits are part of the result. */ + includesWorktree: boolean; + /** Plain sentences the UI shows as is: shallow history, clipped commit list, how merges were walked. */ + notes: string[]; +} + +export interface LensCommit { + /** Full sha, or `WORKTREE` for the uncommitted entry (always last, when present). */ + sha: string; + short: string; + subject: string; + author: string; + /** ISO 8601 committer time; null for `WORKTREE`. */ + date: string | null; + files: number; + added: number; + deleted: number; + /** Chunks this commit touched. */ + chunkIds: string[]; +} + +export type ChunkKind = + | "function" // an executable scope the analyzer named (function, method, component, arrow const) + | "class" // a display scope: class, type, object literal + | "heading" // a Markdown heading section + | "file" // hunks outside any symbol, or a file with no symbols (config, JSON) + | "range"; // nearby hunks in a file the analyzers can't read, grouped by line range + +export type ChunkStatus = + | "modified" + | "added" // not in base + | "deleted" // not in head; lines are base lines + | "moved" // same symbol, other file or renamed file, body unchanged or changed + | "reverted"; // touched in the window, identical at base and head (touched mode only) + +/** How the chunk at head was paired with base. */ +export type ChunkMatch = + | "exact" // same path, same qualified name + | "renamed-file" // git reports the file as renamed; same qualified name + | "moved" // same name and near-identical body in another file + | "uncertain" // best guess; the UI labels it + | "none"; // added or deleted: nothing to pair + +export interface LensHunk { + /** Commit that made the hunk; `WORKTREE` for uncommitted; null in net mode (base → head). */ + sha: string | null; + /** Line ranges as git prints them (`-oldStart,oldLines +newStart,newLines`). */ + oldStart: number; + oldLines: number; + newStart: number; + newLines: number; +} + +export interface LensTouch { + sha: string; + added: number; + deleted: number; +} + +/** null on a side means that side has no value: absent, or not measured. */ +export interface BeforeAfter { + before: number | null; + after: number | null; +} + +export type ReviewBucket = "complex-and-changed" | "new-hotspot"; + +export interface LensChunk { + /** Stable for one selection: `::` or `::L-` or ``. */ + id: string; + /** Repo-relative path at head (at base for deleted chunks). */ + path: string; + /** Path at base when it differs. */ + oldPath?: string; + kind: ChunkKind; + /** Display name: `retryJob`, `## Install`, `lines 40–62`, or the file name. */ + name: string; + /** Lines at head (at base for deleted chunks), 1-based, inclusive. */ + startLine: number; + endLine: number; + /** Lines at base when the chunk exists there. */ + baseStartLine?: number; + baseEndLine?: number; + status: ChunkStatus; + match: ChunkMatch; + /** Sum over every touch (churn = added + deleted). */ + added: number; + deleted: number; + /** Lines that differ between base and head for this chunk. 0 for reverted. */ + netAdded: number; + netDeleted: number; + /** One per commit that touched the chunk, oldest first. Empty in net mode. */ + touches: LensTouch[]; + /** ISO 8601 times of the first and last touch; null when only the working tree touched it. */ + firstTouched: string | null; + lastTouched: string | null; + /** Catalog metric id → before/after. Only metrics the chunk's language measures at its scope. */ + metrics: Record; + /** False when no analyzer reads this chunk: the UI says "not measured", never 0. */ + measured: boolean; + /** Why not, when `measured` is false or a side failed. */ + naReason?: string; + buckets: ReviewBucket[]; + /** code | test | docs | style | config | fixture | generated | vendor … from file_classes.py */ + fileClass: string; + hunks: LensHunk[]; +} + +export interface LensFile { + path: string; + oldPath?: string; + status: "added" | "deleted" | "modified" | "renamed"; + added: number; + deleted: number; + /** Lines of code at head; null for deleted files. Area of the file in Show in repo. */ + loc: number | null; + fileClass: string; + chunkIds: string[]; + /** Generated or vendored: listed, drawn muted, left out of buckets. */ + outOfScope: boolean; +} + +export interface LensSummary { + filesEdited: number; + /** Files in the repo at head (tracked, after srcly's ignore rules). */ + filesTotal: number; + chunks: number; + churnLines: number; + netLines: number; + reverted: number; +} + +export interface ChangeLensPayload { + selection: LensSelection; + /** First-parent commits in the window, oldest first; capped (see `selection.notes`). */ + commits: LensCommit[]; + chunks: LensChunk[]; + files: LensFile[]; + summary: LensSummary; + /** Catalog ids of the metrics that have a before/after on at least one chunk. */ + metricIds: string[]; +} + +/** `GET /api/change-lens/hunk`: the diff text behind one chunk. */ +export interface LensHunkDiff { + chunkId: string; + path: string; + oldPath?: string; + /** Revisions the diff runs between, as labels. */ + from: string; + to: string; + /** Unified diff (3 lines of context) limited to the chunk's lines; "" when the sides are equal. */ + diff: string; + /** Set when the diff was clipped. */ + truncated: boolean; +} diff --git a/docs/intent/change-lens/contract.md b/docs/intent/change-lens/contract.md new file mode 100644 index 00000000..91bf378d --- /dev/null +++ b/docs/intent/change-lens/contract.md @@ -0,0 +1,93 @@ +# Change Lens: contract + +Source: [#397](https://github.com/byronwall/srcly/issues/397). This file is temporary. The last PR of the stack moves its lasting facts into `docs/CODE_MAP.md` and the site docs, and removes this folder. + +Change Lens answers three questions after a coding session: where the work happened, what changed in the shape of the code, and what to review first. A window changes which code is visible. + +The payload types are in `client/src/changelens/types.ts`. That file is the contract. The server's Pydantic models mirror it with camelCase aliases. + +## Endpoints + +### `GET /api/change-lens` + +| Param | Values | Default | +| --- | --- | --- | +| `path` | Absolute path of the analyzed folder (the scan root) | required | +| `window` | `branch`, `24h`, `7d`, `custom` | `branch` | +| `mode` | `touched`, `net` | `touched` | +| `ref` | Branch or revision whose history is read | `HEAD` | +| `base`, `head` | Revisions, for `custom` | none | +| `since`, `until` | ISO 8601 times, for `custom` | none | +| `worktree` | `1` or `0`: include staged and unstaged edits | `1` when `ref` is `HEAD` | + +Returns `ChangeLensPayload`. A folder that is not a git repo returns 409 with a reason. An empty window returns 200 with empty lists and a note. + +### `GET /api/change-lens/hunk` + +`path`, the same selection params, `chunk` (a chunk id), and an optional `commit` (a sha or `WORKTREE`). Returns `LensHunkDiff`. Without `commit`, the diff runs from the selection's base to its head. With `commit`, it runs from that commit's first parent to the commit. + +### `srcly changes [path]` + +Prints the same result as Markdown: the resolved selection, the review buckets, then the chunks grouped by folder, with `before → after` complexity. Flags: `--window`, `--mode`, `--ref`, `--base`, `--head`, `--since`, `--until`, `--no-worktree`, `--json`. + +## Time rules + +1. **One history.** Read `git log --first-parent `. A merge commit counts once, as its diff against its first parent. Activity from other branches never mixes in. Say this in `selection.notes` when the window holds a merge. +2. **Branch window.** Base is the merge-base of `ref` and the default branch, found the way `compare/git_refs.py` (`resolve_auto_base`) finds it. Head is `ref`. On the default branch itself, the base is the upstream tip (`origin/`); when that equals head, the window is empty and a note says so. +3. **Time windows.** Commits whose committer date is inside `[since, until]`. Base is the first parent of the oldest commit in the window. Head is the newest. `24h` and `7d` count back from now. +4. **Working tree.** Staged and unstaged edits are one entry, `WORKTREE`, after the last commit. It has no time. Untracked files count as added, unless srcly's ignore rules skip them. +5. **Touched against net.** Touched is the union of chunks that any commit in the window changed. Net compares the base snapshot with the head snapshot (the working tree, when included). A chunk that was changed and then put back has status `reverted` in touched mode and is absent in net mode. +6. **Shallow or incomplete history.** State it in `selection.notes`. Never guess a base. + +## Chunk rules + +1. Parse each changed file once per side (base and head) with the analyzer the scan uses for it. Parse the blob (`git show :`), not a checkout. Do not scan whole snapshots. +2. A hunk belongs to the innermost named scope that holds its lines: on the new side for added and changed lines, on the old side for deleted lines. Executable scopes win over display scopes. +3. Hunks outside every scope form one `file` chunk for that file. In Markdown, a hunk belongs to its nearest heading section (`heading`). In a file no analyzer reads, hunks within 10 lines of each other form one `range` chunk. +4. Never invent a symbol. A chunk with no analyzer has `measured: false`, an empty `metrics`, and a `naReason`. +5. Pair head and base chunks by path and qualified name (`exact`). When git reports a rename, pair by qualified name across the two paths (`renamed-file`). A symbol that left one file and appeared in another with a near-identical body is `moved`. A line-number overlap alone is never a match. A weaker guess is `uncertain`. Unpaired chunks are `added` or `deleted`. +6. A pure rename with an unchanged body has equal `before` and `after`. It is not an improvement or a regression. +7. Deleted chunks stay in touched mode, with their base lines. +8. Touches come from mapping each commit's hunks to the chunk through the file's symbols at that commit. `touches.length` is the number of commits that changed the chunk. +9. Use one analysis version for both sides. + +## Review buckets + +Buckets are plain rules. There is no single score. + +- `complex-and-changed`: the chunk existed at base with cyclomatic complexity of 10 or more, and its body changed. +- `new-hotspot`: the chunk is `added`, or its complexity rose, and its complexity at head is 10 or more. + +Test, docs, generated and vendor files get no bucket. The thresholds are constants in one place. + +## Client view + +Change Lens is the fourth view: `?view=changes`, beside Treemap, Graph and 2x2. Its code is in `client/src/changelens/`. URL params: `cw` (window), `cm` (mode), `cl` (layout: `edits` or `repo`), `cc` (selected commit), `cs` (selected chunk id), `cbase`, `chead`, `csince`, `cuntil`. + +- **Window bar** (in the view toolbar slot): the four presets, the Touched/Net switch, the Only edits/Show in repo switch. Below it, one status line shows the resolved base and head, the time limits and time zone, and each note. +- **Only edits:** a packed treemap of chunks grouped by folder, then file. Area is changed lines (`added + deleted`), with a floor so each chunk can be clicked. Folder labels read like `5 edited / 43 files · mostly tests`. Breadcrumbs and collapsible groups stay. +- **Show in repo:** the file map with area by file LOC. Untouched files are muted. Edited chunks keep an outline. +- **Lenses:** the color picker gains "Complexity delta" (a diverging scale) beside the current metrics. Added, deleted, moved, reverted and not-measured each have their own state, never a zero. A color change never changes the layout or the selection. +- **Panel** (`client/src/changelens/panel/`): `ReviewBuckets`, `ChunkInspector` (before → after for each metric, the touch strip, churn against net), `HunkDiff`, `CommitRail`. + +Panel components take data through props only. They do not fetch, and they do not read the navigation store. The view owns state and passes it in: + +```ts +// client/src/changelens/panel/index.ts +ReviewBuckets(props: { payload: ChangeLensPayload; selected: string | null; onSelect: (chunkId: string) => void }) +ChunkInspector(props: { payload: ChangeLensPayload; chunk: LensChunk | null; onOpenHunk: (chunkId: string, commit?: string) => void; onOpenCode: (chunk: LensChunk) => void }) +HunkDiff(props: { diff: LensHunkDiff | null; loading: boolean; error?: string; onClose: () => void }) +CommitRail(props: { commits: LensCommit[]; selected: string | null; onSelect: (sha: string | null) => void; highlightChunk?: string | null }) +``` + +## Acceptance fixture + +`server/tests/fixtures/change_lens_repo.py` builds a small git repo in a temp folder, with fixed commit dates. Its branch holds: one edit to one function in a large file; three edits to the same function; a change that a later commit reverts; a pure file rename; a deleted function; a config hunk and a Markdown hunk; a new function with high complexity; and an uncommitted edit. + +The tests and the browser proof check that: + +- Only edits hides untouched chunks, keeps breadcrumbs, and opens the right diff. +- Reverted work shows in Touched and not in Net. The pure rename reports no complexity change. +- A color change keeps layout and selection. A switch to Show in repo keeps the selection. +- Before → after values come from the same analysis inputs. Unsupported metrics and unresolved matches say so. +- The UI never names an agent as the author of a read or an edit. From e1df04c4ad8f0a34072d5c842d1df0f5725c1719 Mon Sep 17 00:00:00 2001 From: Byron Wall Date: Sat, 21 Nov 2026 13:56:01 -0500 Subject: [PATCH 02/16] feat(server): Change Lens engine, API and srcly changes (#397) The engine reads one first-parent history, follows each changed file through the window, and gives every changed line to the scope that holds it in that revision. It pairs base and head chunks by name, never by line number. - GET /api/change-lens and /api/change-lens/hunk - srcly changes [path], Markdown by default and --json - An acceptance repo builder with fixed dates, and tests on it Co-Authored-By: Claude Fable 5.2 --- server/app/main.py | 3 +- server/app/routers/changelens.py | 79 +++ server/app/run.py | 53 +- server/app/services/changelens/__init__.py | 1 + server/app/services/changelens/blobs.py | 341 ++++++++++ server/app/services/changelens/chunks.py | 727 +++++++++++++++++++++ server/app/services/changelens/hunks.py | 195 ++++++ server/app/services/changelens/models.py | 162 +++++ server/app/services/changelens/service.py | 326 +++++++++ server/app/services/changelens/textdiff.py | 139 ++++ server/app/services/changelens/window.py | 352 ++++++++++ server/tests/fixtures/change_lens_repo.py | 347 ++++++++++ server/tests/test_change_lens.py | 175 +++++ 13 files changed, 2898 insertions(+), 2 deletions(-) create mode 100644 server/app/routers/changelens.py create mode 100644 server/app/services/changelens/__init__.py create mode 100644 server/app/services/changelens/blobs.py create mode 100644 server/app/services/changelens/chunks.py create mode 100644 server/app/services/changelens/hunks.py create mode 100644 server/app/services/changelens/models.py create mode 100644 server/app/services/changelens/service.py create mode 100644 server/app/services/changelens/textdiff.py create mode 100644 server/app/services/changelens/window.py create mode 100644 server/tests/fixtures/change_lens_repo.py create mode 100644 server/tests/test_change_lens.py diff --git a/server/app/main.py b/server/app/main.py index 7030e7d9..99b6fcc9 100644 --- a/server/app/main.py +++ b/server/app/main.py @@ -8,7 +8,7 @@ import signal import threading -from app.routers import analysis, compare, dataflow, files, metrics, scans, symbols +from app.routers import analysis, changelens, compare, dataflow, files, metrics, scans, symbols from app.services.dataflow import engine as dataflow_engine from app.services.scan_workers import cancel_all_scans @@ -82,6 +82,7 @@ async def lifespan(_app: FastAPI): app.include_router(files.router) app.include_router(scans.router) app.include_router(compare.router) +app.include_router(changelens.router) app.include_router(metrics.router) app.include_router(dataflow.router) app.include_router(symbols.router) diff --git a/server/app/routers/changelens.py b/server/app/routers/changelens.py new file mode 100644 index 00000000..8706f0ae --- /dev/null +++ b/server/app/routers/changelens.py @@ -0,0 +1,79 @@ +""" +Change Lens: the code chunks a window of git history edited. + + GET /api/change-lens the window's commits, chunks and files (ChangeLensPayload) + GET /api/change-lens/hunk the diff text behind one chunk (LensHunkDiff) + +The payload types are in client/src/changelens/types.ts. +""" + +from pathlib import Path +from typing import Literal, Optional + +from fastapi import APIRouter, HTTPException, Query + +from app.services.changelens import service +from app.services.changelens.window import LensError, Params + +router = APIRouter(prefix="/api/change-lens", tags=["change-lens"]) + + +def _params( + window: Literal["branch", "24h", "7d", "custom"] = "branch", + mode: Literal["touched", "net"] = "touched", + ref: str = "HEAD", + base: Optional[str] = None, + head: Optional[str] = None, + since: Optional[str] = None, + until: Optional[str] = None, + worktree: Optional[bool] = None, +) -> Params: + return Params(window, mode, ref, base or None, head or None, since or None, until or None, worktree) + + +def _folder(path: str) -> Path: + folder = Path(path).expanduser() + if not folder.exists(): + raise HTTPException(status_code=404, detail=f"Path not found: {path}") + return folder + + +# Plain `def` handlers: FastAPI runs them in its thread pool, so git and the analyzers never block the event loop. +@router.get("") +def get_change_lens( + path: str = Query(..., description="Absolute path of the analyzed folder"), + window: Literal["branch", "24h", "7d", "custom"] = "branch", + mode: Literal["touched", "net"] = "touched", + ref: str = "HEAD", + base: Optional[str] = None, + head: Optional[str] = None, + since: Optional[str] = None, + until: Optional[str] = None, + worktree: Optional[bool] = None, +): + try: + return service.build_payload(_folder(path), _params(window, mode, ref, base, head, since, until, worktree)) + except LensError as exc: + raise HTTPException(status_code=exc.status, detail=str(exc)) from exc + + +@router.get("/hunk") +def get_change_lens_hunk( + path: str = Query(..., description="Absolute path of the analyzed folder"), + chunk: str = Query(..., description="A chunk id from the payload"), + commit: Optional[str] = Query(None, description="A commit sha or WORKTREE; omit for base to head"), + window: Literal["branch", "24h", "7d", "custom"] = "branch", + mode: Literal["touched", "net"] = "touched", + ref: str = "HEAD", + base: Optional[str] = None, + head: Optional[str] = None, + since: Optional[str] = None, + until: Optional[str] = None, + worktree: Optional[bool] = None, +): + try: + return service.hunk_diff( + _folder(path), _params(window, mode, ref, base, head, since, until, worktree), chunk, commit or None + ) + except LensError as exc: + raise HTTPException(status_code=exc.status, detail=str(exc)) from exc diff --git a/server/app/run.py b/server/app/run.py index 79456df8..213802e7 100644 --- a/server/app/run.py +++ b/server/app/run.py @@ -80,7 +80,7 @@ def _main(argv: list[str] | None = None) -> None: - Opens the default browser to the app URL. """ argv = list(argv) if argv is not None else sys.argv[1:] - if argv and argv[0] in {"scan", "report", "hotspots", "explain", "compare", "fix", "check", "trace", "impact"}: + if argv and argv[0] in {"scan", "report", "hotspots", "explain", "compare", "changes", "fix", "check", "trace", "impact"}: # stderr, so JSON on stdout stays clean. print(startup_info.version_line(), file=sys.stderr, flush=True) _run_headless(argv) @@ -99,6 +99,7 @@ def _main(argv: list[str] | None = None) -> None: " explain drill into the repo, a folder, a file, or file::symbol\n" " hotspots rank files or functions by one metric\n" " compare metric and dependency changes between two git refs\n" + " changes the code chunks a branch or time window edited, and what to review first\n" " fix show, then apply (--write), safe fixes for srcly's own findings\n" " check --ratchet: fail when a file over the size budget grows\n" " trace where a value comes from and goes (data flow), or --effects\n" @@ -455,6 +456,31 @@ def _run_headless(argv: list[str]) -> None: ) compare_parser.add_argument("--limit", type=_positive_int, default=40, help="Maximum files in the markdown summary.") + changes_parser = subparsers.add_parser( + "changes", + help="Change Lens: the code chunks a branch or time window edited, with before → after complexity.", + ) + changes_parser.add_argument("path", nargs="?", default=None, help="Folder to read. Defaults to the enclosing Git repo root.") + changes_parser.add_argument( + "--window", + choices=["branch", "24h", "7d", "custom"], + default=None, + help="branch (default): this branch against its base. 24h, 7d: commits in that time. custom: use --base/--head or --since/--until.", + ) + changes_parser.add_argument( + "--mode", + choices=["touched", "net"], + default="touched", + help="touched (default): every chunk a commit in the window changed. net: the base snapshot against the head.", + ) + changes_parser.add_argument("--ref", default="HEAD", help="Branch or revision whose first-parent history is read. Defaults to HEAD.") + changes_parser.add_argument("--base", default=None, help="Base revision of a custom window.") + changes_parser.add_argument("--head", default=None, help="Head revision of a custom window. Defaults to --ref.") + changes_parser.add_argument("--since", default=None, help="Start of a custom time window (ISO 8601).") + changes_parser.add_argument("--until", default=None, help="End of a custom time window (ISO 8601). Defaults to now.") + changes_parser.add_argument("--no-worktree", action="store_true", help="Leave staged and unstaged edits out.") + changes_parser.add_argument("--json", action="store_true", help="Print the Change Lens payload as JSON.") + fix_parser = subparsers.add_parser( "fix", help="Show, then apply, the safe mechanical fixes for srcly's own findings." ) @@ -509,6 +535,9 @@ def _dispatch(args) -> None: if args.command == "compare": _run_compare(args) return + if args.command == "changes": + _run_changes(args) + return if args.command == "scan": if args.format != "json": @@ -697,6 +726,28 @@ def _run_compare(args) -> None: print(render_markdown(payload, limit=args.limit), end="") +def _run_changes(args) -> None: + from app.services.changelens import service + from app.services.changelens.window import LensError, Params + + custom = any((args.base, args.head, args.since, args.until)) + params = Params( + window=args.window or ("custom" if custom else "branch"), + mode=args.mode, + ref=args.ref, + base=args.base, + head=args.head, + since=args.since, + until=args.until, + worktree=False if args.no_worktree else None, + ) + try: + payload = service.build_payload(_resolve_target_path(args.path), params) + except LensError as exc: + raise SystemExit(str(exc)) from exc + print(reporting._json_dumps(payload) if args.json else service.render_markdown(payload), end="" if args.json else "\n") + + def _deprioritize_paths(args) -> tuple[str, ...]: if args.no_default_deprioritize: return tuple(args.deprioritize or ()) diff --git a/server/app/services/changelens/__init__.py b/server/app/services/changelens/__init__.py new file mode 100644 index 00000000..b5927ea0 --- /dev/null +++ b/server/app/services/changelens/__init__.py @@ -0,0 +1 @@ +"""Change Lens: the code chunks a window of git history edited (``docs/intent/change-lens/contract.md``).""" diff --git a/server/app/services/changelens/blobs.py b/server/app/services/changelens/blobs.py new file mode 100644 index 00000000..c5187857 --- /dev/null +++ b/server/app/services/changelens/blobs.py @@ -0,0 +1,341 @@ +"""File contents at a revision, and the named scopes the analyzers find in them. + +Only changed files are read. A parsed blob is cached by its blob sha, so a file +is analyzed once however many commits, windows or requests see it. +""" + +from __future__ import annotations + +import multiprocessing +import os +import re +import subprocess +import tempfile +from collections import OrderedDict +from concurrent.futures import ProcessPoolExecutor +from dataclasses import dataclass, field +from pathlib import Path +from typing import Iterable, Optional + +from app.config import MAX_ANALYZED_FILE_BYTES +from app.services.analysis import _scope_value, analyze_single_file, language_for_path +from app.services.analysis_types import MEASURED_SCOPE_METRICS, iter_executable_scopes, scope_kind +from app.services.compare.git_refs import GitError + +SYMBOL_LANGUAGES = frozenset({"ts", "tsx", "js", "jsx", "python", "code", "markdown"}) +"""Languages whose analyzer names scopes. Every other file is grouped by line range.""" +PARSE_CACHE_SIZE = 4000 +POOL_AT = 150 +"""Uncached blobs from which parsing moves to worker processes.""" +MAX_WORKERS = 8 +GIT_TIMEOUT = 60.0 + +_NAME = re.compile(r"^[A-Za-z_$#][\w$#.]*$") +_DECORATION = re.compile(r"^\(class\) | \((?:class|object|type)\)$") +_LEAD_IN = ("//", "/*", "*", "@") +_LEAD_IN_PYTHON = ("#", "@") +"""A comment or decorator line right above a scope belongs to that scope.""" + +_UNREAD_REASONS = { + "text": "No analyzer reads this file type", + "css": "Stylesheets have no named code scopes", + "notebook": "Notebooks have no named code scopes", +} + + +@dataclass(frozen=True) +class BlobRef: + """One side of a change: a blob in the object store, or a file on disk (the working tree).""" + + path: str + sha: Optional[str] = None + disk: Optional[str] = None + + @property + def key(self) -> str: + return self.sha or f"disk:{self.disk}" + + +@dataclass +class Scope: + key: str + """Qualified name, unique in its file: ``Foo.bar``, ``## Install``.""" + name: str + kind: str + """function, class or heading.""" + start: int + end: int + metrics: dict[str, Optional[float]] + body: tuple[str, ...] + """Stripped, non-blank lines: what move detection compares.""" + + +@dataclass +class Parsed: + language: str + loc: int + n_lines: int + scopes: dict[str, Scope] = field(default_factory=dict) + owner: list[str] = field(default_factory=list) + """Scope key for each line (index = line number); ``""`` is the file itself.""" + blank: bytearray = field(default_factory=bytearray) + reason: Optional[str] = None + """Why no scopes were read, when none were.""" + file_metrics: dict[str, Optional[float]] = field(default_factory=dict) + + def owners(self, start: int, count: int) -> list[str]: + """Scope key of each line in a hunk side. + + A blank line outside every scope goes with its neighbour in the same + hunk: the separator added with a new function is part of that edit. + """ + keys = [self.owner[n] if 0 < n < len(self.owner) else "" for n in range(start, start + count)] + loose = [i for i, key in enumerate(keys) if not key and 0 < start + i < len(self.blank) and self.blank[start + i]] + for i in loose: + near = next((keys[j] for j in range(i - 1, -1, -1) if keys[j]), "") or next((k for k in keys[i + 1 :] if k), "") + keys[i] = near + return keys + + +_cache: OrderedDict[tuple, Parsed] = OrderedDict() + + +def clear_cache() -> None: + _cache.clear() + + +def _clean(name: str) -> str: + return _DECORATION.sub("", name).replace("::", ".") + + +def _unique(key: str, taken: dict[str, Scope]) -> str: + if key not in taken: + return key + n = 2 + while f"{key}#{n}" in taken: + n += 1 + return f"{key}#{n}" + + +def parse_text(text: str, path: str) -> Parsed: + """Run the scan's analyzer for ``path`` on ``text`` and index its named scopes by line.""" + language = language_for_path(path) + lines = text.splitlines() + n = len(lines) + blank = bytearray(n + 2) + for i, line in enumerate(lines, start=1): + if not line.strip(): + blank[i] = 1 + parsed = Parsed(language, sum(1 for line in lines if line.strip()), n, owner=[""] * (n + 2), blank=blank) + if language not in SYMBOL_LANGUAGES: + parsed.reason = _UNREAD_REASONS.get(language, _UNREAD_REASONS["text"]) + return parsed + if len(text) > MAX_ANALYZED_FILE_BYTES: + parsed.reason = "The file is too large to analyze" + return parsed + + with tempfile.TemporaryDirectory(prefix="srcly-lens-") as tmp: + target = os.path.join(tmp, os.path.basename(path) or "file") + with open(target, "w", encoding="utf-8", newline="") as f: + f.write(text) + info = analyze_single_file(target) + if isinstance(info, dict): + parsed.reason = f"Analysis failed: {info.get('error', 'unknown error')}" + return parsed + parsed.loc = int(getattr(info, "nloc", parsed.loc) or 0) + + measured = MEASURED_SCOPE_METRICS[language] + display: list[Scope] = [] + executable: list[Scope] = [] + + def body(start: int, end: int) -> tuple[str, ...]: + return tuple(s for s in (line.strip() for line in lines[start - 1 : end]) if s) + + def walk(funcs, prefix: str) -> None: + for func in funcs or []: + start, end = int(getattr(func, "start_line", 0) or 0), int(getattr(func, "end_line", 0) or 0) + children = getattr(func, "children", None) or [] + if language == "markdown": + if getattr(func, "origin_type", "") == "markdown_section" and 0 < start <= end: + key = _unique(func.name, parsed.scopes) + scope = Scope(key, func.name, "heading", start, min(end, n), {}, body(start, end)) + parsed.scopes[key] = scope + display.append(scope) + walk(children, prefix) + continue + kind = scope_kind(func) + name = _clean(func.name or "") + if kind not in ("function", "class", "type", "object") or not _NAME.match(name) or not 0 < start <= end: + walk(children, prefix) + continue + key = _unique(prefix + name, parsed.scopes) + if kind == "function": + metrics = {m: _scope_value(func, m) for m in measured} + metrics["loc"] = func.nloc + scope = Scope(key, name, "function", start, min(end, n), metrics, body(start, end)) + executable.append(scope) + else: + scope = Scope(key, name, "class", start, min(end, n), {"loc": func.nloc}, body(start, end)) + display.append(scope) + parsed.scopes[key] = scope + walk(children, key + ".") + + walk(info.function_list, "") + if language == "markdown": + # The grammar ends a section on the next heading's line; that line is the next section's. + starts = {scope.start for scope in display} + for scope in display: + if scope.end > scope.start and scope.end in starts: + scope.end -= 1 + + # The innermost executable scope owns a line; a display scope owns it only + # outside every function. Wider scopes are written first. + for group in (display, executable): + for scope in sorted(group, key=lambda s: s.start - s.end): + for line in range(scope.start, scope.end + 1): + parsed.owner[line] = scope.key + if language != "markdown": + lead_in = _LEAD_IN_PYTHON if language == "python" else _LEAD_IN + for scope in sorted(parsed.scopes.values(), key=lambda s: s.start): + line = scope.start - 1 + while line >= 1 and not parsed.owner[line] and lines[line - 1].lstrip().startswith(lead_in): + parsed.owner[line] = scope.key + line -= 1 + scope.start = line + 1 + + functions = list(iter_executable_scopes(info.function_list)) + parsed.file_metrics = {"loc": parsed.loc} + for metric in ("complexity", "cognitive_complexity"): + if metric in measured: + parsed.file_metrics[metric] = max((_scope_value(f, metric) for f in functions), default=0) + return parsed + + +def _parse_job(job: tuple[str, str]) -> Parsed: + return parse_text(*job) + + +class BlobStore: + """Reads blobs for one build through a single ``git cat-file --batch`` call per batch.""" + + def __init__(self, repo: Path) -> None: + self.repo = repo + self._texts: dict[str, Optional[str]] = {} + + def _batch(self, names: list[str]) -> list[Optional[tuple[str, bytes]]]: + """``(object sha, content)`` per name, None for a missing object or a non-blob.""" + if not names: + return [] + try: + proc = subprocess.run( + ["git", "cat-file", "--batch"], + cwd=self.repo, + input="".join(name + "\n" for name in names).encode("utf-8"), + capture_output=True, + check=False, + timeout=GIT_TIMEOUT, + ) + except (OSError, subprocess.TimeoutExpired) as exc: + raise GitError(f"git cat-file failed: {exc}") from exc + data, pos, out = proc.stdout, 0, [] + for _ in names: + end = data.find(b"\n", pos) + if end < 0: + out.append(None) + continue + header = data[pos:end].decode("utf-8", "replace").split() + pos = end + 1 + if len(header) == 3 and header[2].isdigit() and header[0] != "missing": + size = int(header[2]) + content = data[pos : pos + size] + pos += size + 1 + out.append((header[0], content) if header[1] == "blob" else None) + else: + out.append(None) + return out + + @staticmethod + def _decode(content: bytes) -> Optional[str]: + if b"\0" in content[:8192]: + return None + return content.decode("utf-8", "replace") + + def lookup(self, tree: str, paths: Iterable[str]) -> dict[str, BlobRef]: + """The blob at each path in ``tree`` (a commit or tree sha). Missing paths are left out.""" + wanted = [p for p in dict.fromkeys(paths) if "\n" not in p] + found = {} + for path, hit in zip(wanted, self._batch([f"{tree}:{path}" for path in wanted])): + if hit is not None: + sha, content = hit + self._texts.setdefault(sha, self._decode(content)) + found[path] = BlobRef(path, sha=sha) + return found + + def load(self, refs: Iterable[BlobRef]) -> None: + shas = list(dict.fromkeys(r.sha for r in refs if r.sha and r.sha not in self._texts)) + for sha, hit in zip(shas, self._batch(shas)): + self._texts[sha] = self._decode(hit[1]) if hit else None + + def text(self, ref: Optional[BlobRef]) -> Optional[str]: + """The text of one side; None when the side is absent, unreadable or binary.""" + if ref is None: + return None + if ref.sha: + if ref.sha not in self._texts: + self.load([ref]) + return self._texts[ref.sha] + if ref.key not in self._texts: + try: + with open(ref.disk or "", "rb") as f: + self._texts[ref.key] = self._decode(f.read()) + except OSError: + self._texts[ref.key] = None + return self._texts[ref.key] + + def _cache_key(self, ref: BlobRef, text: str) -> tuple: + # The analyzer is picked by extension, so a renamed file reuses its parse. + name = os.path.basename(ref.path).lower() + suffix = os.path.splitext(name)[1] or name + if ref.sha: + return (ref.sha, suffix) + try: + stat = os.stat(ref.disk or "") + return (ref.disk, stat.st_mtime_ns, stat.st_size) + except OSError: + return (ref.disk, len(text), hash(text)) + + def warm(self, refs: Iterable[BlobRef]) -> None: + """Parse many blobs at once. A long window has over a thousand; worker processes cut the wait.""" + jobs: dict[tuple, tuple[str, str]] = {} + for ref in refs: + text = self.text(ref) + if text is None: + continue + key = self._cache_key(ref, text) + if key not in _cache and key not in jobs: + jobs[key] = (text, ref.path) + if len(jobs) < POOL_AT: + return + workers = max(1, min(MAX_WORKERS, (os.cpu_count() or 2) - 1)) + try: + with ProcessPoolExecutor(max_workers=workers, mp_context=multiprocessing.get_context("spawn")) as pool: + for key, parsed in zip(jobs, pool.map(_parse_job, jobs.values(), chunksize=8)): + _cache[key] = parsed + except Exception: + # No pool (a sandbox, a dying worker): `parsed` reads each blob in this process instead. + return + + def parsed(self, ref: Optional[BlobRef]) -> Optional[Parsed]: + text = self.text(ref) + if ref is None or text is None: + return None + key = self._cache_key(ref, text) + hit = _cache.get(key) + if hit is None: + hit = parse_text(text, ref.path) + _cache[key] = hit + while len(_cache) > PARSE_CACHE_SIZE: + _cache.popitem(last=False) + else: + _cache.move_to_end(key) + return hit diff --git a/server/app/services/changelens/chunks.py b/server/app/services/changelens/chunks.py new file mode 100644 index 00000000..d18ef655 --- /dev/null +++ b/server/app/services/changelens/chunks.py @@ -0,0 +1,727 @@ +"""Hunks to chunks: follow each file through the window, give every changed line to a scope, pair base with head. + +A line belongs to the scope that holds it in the revision the line is in: the +commit's own blob for added lines, its parent's blob for deleted lines. Line +numbers from one revision are never looked up in another. +""" + +from __future__ import annotations + +import re +from collections import defaultdict +from dataclasses import dataclass, field +from difflib import SequenceMatcher +from typing import Optional + +from app.services import file_classes, metric_catalog +from app.services.analysis import language_for_path +from app.services.changelens import textdiff +from app.services.changelens.blobs import SYMBOL_LANGUAGES, BlobRef, BlobStore, Parsed, Scope +from app.services.changelens.hunks import FileDiff, Hunk, commit_diffs, kept, tree_diff +from app.services.changelens.models import WORKTREE, BeforeAfter, LensChunk, LensCommit, LensFile, LensHunk, LensTouch +from app.services.changelens.window import Window, iso + +# Review bucket and matching thresholds, all in one place. +COMPLEX_AT = 10 +"""Cyclomatic complexity from which a chunk is a hotspot.""" +RANGE_GAP = 10 +"""Hunks this many lines apart or closer form one ``range`` chunk.""" +MOVED_AT = 0.9 +"""Body similarity from which a same-named symbol in another place is ``moved``.""" +UNCERTAIN_AT = 0.6 +"""Body similarity from which it is still offered, as ``uncertain``.""" +BUCKET_CLASSES = frozenset({"code"}) +OUT_OF_SCOPE_CLASSES = frozenset({"generated", "vendored"}) + +OUTSIDE_SCOPES = "Lines outside every named scope" + +_ORDINAL = re.compile(r"#\d+$") + + +@dataclass +class Tally: + added: int = 0 + deleted: int = 0 + hunks: list[Hunk] = field(default_factory=list) + + def add(self, other: "Tally") -> None: + self.added += other.added + self.deleted += other.deleted + self.hunks.extend(other.hunks) + + @property + def lines(self) -> int: + return self.added + self.deleted + + +@dataclass +class Change: + """One commit's (or the working tree's) edit to a file.""" + + sha: str + old: Optional[BlobRef] + new: Optional[BlobRef] + hunks: list[Hunk] + renamed: bool + + +@dataclass +class Lineage: + """One file followed through the window, across renames.""" + + first_path: str + path: str + changes: list[Change] = field(default_factory=list) + base: Optional[BlobRef] = None + head: Optional[BlobRef] = None + net: list[Hunk] = field(default_factory=list) + file_class: str = "code" + gave_chunk: bool = False + """A chunk of this file was paired with one in another file (a move).""" + + @property + def renamed(self) -> bool: + return self.base is not None and self.head is not None and self.base.path != self.head.path + + @property + def shown_path(self) -> str: + return self.head.path if self.head else self.base.path if self.base else self.path + + +@dataclass +class Draft: + """A chunk being built. Kept after the build: the hunk endpoint reads it.""" + + lineage: Lineage + key: str + kind: str + name: str + base: Optional[Scope] = None + head: Optional[Scope] = None + seen: Optional[Scope] = None + """The scope in the last revision that held it, for a symbol absent at both ends.""" + span: tuple[int, int] = (1, 1) + touches: dict[str, Tally] = field(default_factory=dict) + net: Tally = field(default_factory=Tally) + base_lineage: Optional[Lineage] = None + base_key: str = "" + match: Optional[str] = None + measured: bool = True + reason: Optional[str] = None + sources: dict[str, list[tuple[Lineage, str]]] = field(default_factory=dict) + chunk: Optional[LensChunk] = None + + @property + def is_symbol(self) -> bool: + return self.kind in ("function", "class", "heading") + + @property + def base_file(self) -> Lineage: + return self.base_lineage or self.lineage + + +@dataclass +class Built: + window: Window + mode: str + store: BlobStore + commits: list[LensCommit] + chunks: list[LensChunk] + files: list[LensFile] + drafts: dict[str, Draft] + has_worktree: bool + left_out: int + """Changed files not read: binary, or skipped by srcly's ignore rules.""" + + +# -- lineages ----------------------------------------------------------------- + + +def _follow(window: Window, steps: list[tuple[str, list[FileDiff]]]) -> list[Lineage]: + current: dict[str, Lineage] = {} + lineages: list[Lineage] = [] + for sha, diffs in steps: + on_disk = sha == WORKTREE + after = dict(current) + for diff in diffs: + old = BlobRef(diff.old_path, sha=diff.old_sha) if diff.old_path and diff.old_sha else None + new: Optional[BlobRef] = None + if diff.new_path: + if on_disk: + new = BlobRef(diff.new_path, disk=str(window.repo / diff.new_path)) + elif diff.new_sha: + new = BlobRef(diff.new_path, sha=diff.new_sha) + source = diff.old_path or diff.new_path or "" + line = current.get(source) + if line is None: + line = Lineage(first_path=source, path=source) + lineages.append(line) + line.changes.append(Change(sha, old, new, diff.hunks, renamed=diff.status == "R")) + if diff.status == "R" and diff.new_path: + if after.get(source) is line: + del after[source] + line.path = diff.new_path + after[line.path] = line + current = after + return lineages + + +def _resolve_ends(window: Window, store: BlobStore, lineages: list[Lineage], net: list[FileDiff], dirty: set[str], has_worktree: bool) -> None: + """Read each file's base and head from the two snapshots, and its net hunks.""" + repo = window.repo + at_base = store.lookup(window.base_tree, [line.first_path for line in lineages]) + at_tip = store.lookup(window.tip.sha, [line.path for line in lineages if line.path not in dirty]) + for line in lineages: + line.base = at_base.get(line.first_path) + if has_worktree and line.path in dirty: + full = repo / line.path + line.head = BlobRef(line.path, disk=str(full)) if full.is_file() else None + else: + line.head = at_tip.get(line.path) + + known_base = {line.base.path for line in lineages if line.base} + known_head = {line.head.path for line in lineages if line.head} + by_new = {d.new_path: d for d in net if d.new_path} + by_old = {d.old_path: d for d in net if d.old_path and not d.new_path} + for line in lineages: + base, head = line.base, line.head + diff = by_new.get(head.path) if head else by_old.get(base.path) if base else None + if diff is not None and diff.old_path == (base.path if base else None) and diff.new_path == (head.path if head else None): + line.net = diff.hunks + elif (base is None and head is None) or (base and head and base.sha and base.sha == head.sha): + line.net = [] + else: + # Git paired the two snapshots another way (a rename it did not follow). + line.net = textdiff.hunks(_lines(store, base), _lines(store, head)) + + # Files that differ between the snapshots though no commit in the window touched them. + for diff in net: + if (diff.new_path and diff.new_path in known_head) or (diff.old_path and diff.old_path in known_base): + continue + line = Lineage(first_path=diff.old_path or diff.new_path or "", path=diff.new_path or diff.old_path or "") + if diff.old_path and diff.old_sha: + line.base = BlobRef(diff.old_path, sha=diff.old_sha) + if diff.new_path: + if diff.new_sha: + line.head = BlobRef(diff.new_path, sha=diff.new_sha) + elif has_worktree: + line.head = BlobRef(diff.new_path, disk=str(repo / diff.new_path)) + line.net = diff.hunks + lineages.append(line) + + +def _lines(store: BlobStore, ref: Optional[BlobRef]) -> list[str]: + return (store.text(ref) or "").splitlines() + + +# -- attribution --------------------------------------------------------------- + + +def _attribute(old: Optional[Parsed], new: Optional[Parsed], hunks: list[Hunk]) -> dict[str, Tally]: + out: dict[str, Tally] = defaultdict(Tally) + for hunk in hunks: + new_keys = new.owners(hunk.new_start, hunk.new_lines) if new else [""] * hunk.new_lines + old_keys = old.owners(hunk.old_start, hunk.old_lines) if old else [""] * hunk.old_lines + for key in new_keys: + out[key].added += 1 + for key in old_keys: + out[key].deleted += 1 + for key in dict.fromkeys(new_keys + old_keys): + out[key].hunks.append(hunk) + return out + + +def _whole(hunks: list[Hunk]) -> Tally: + return Tally(sum(h.new_lines for h in hunks), sum(h.old_lines for h in hunks), list(hunks)) + + +def _symbol_drafts(store: BlobStore, line: Lineage) -> list[Draft]: + touches: dict[str, dict[str, Tally]] = defaultdict(dict) + seen: dict[str, Scope] = {} + for change in line.changes: + old, new = store.parsed(change.old), store.parsed(change.new) + for key, tally in _attribute(old, new, change.hunks).items(): + touches[key][change.sha] = tally + scope = (new and new.scopes.get(key)) or (old and old.scopes.get(key)) + if scope: + seen[key] = scope + base, head = store.parsed(line.base), store.parsed(line.head) + net = _attribute(base, head, line.net) + + drafts = [] + for key in dict.fromkeys([*touches, *net]): + base_scope = base.scopes.get(key) if base else None + head_scope = head.scopes.get(key) if head else None + scope = head_scope or base_scope or seen.get(key) + if key and scope is None: + continue + draft = Draft(line, key, scope.kind if scope else "file", key or line.shown_path.rsplit("/", 1)[-1]) + draft.base, draft.head, draft.seen = base_scope, head_scope, scope + draft.touches, draft.net = touches.get(key, {}), net.get(key, Tally()) + if not key: + draft.measured, draft.reason = False, OUTSIDE_SCOPES + parsed = head or base + if parsed and parsed.reason: + draft.reason = parsed.reason + elif scope.kind == "heading": + draft.measured, draft.reason = False, "Markdown sections have no code metrics" + drafts.append(draft) + return drafts + + +def _shift(line: int, hunks: list[Hunk]) -> int: + """Where ``line`` of the old side is on the new side of one change.""" + delta = 0 + for hunk in hunks: + if hunk.old_lines == 0: + if line <= hunk.old_start: + break + delta += hunk.new_lines + elif line >= hunk.old_start + hunk.old_lines: + delta += hunk.new_lines - hunk.old_lines + elif line >= hunk.old_start: + return max(1, hunk.new_start + min(line - hunk.old_start, max(hunk.new_lines - 1, 0))) + else: + break + return line + delta + + +def _range_drafts(store: BlobStore, line: Lineage, mode: str, reason: str) -> list[Draft]: + """A file with no named scopes: nearby hunks, placed in head line numbers, form one chunk.""" + total = max(1, len(_lines(store, line.head))) + items: list[tuple[int, int, Optional[str], Hunk]] = [] + + def place(hunk: Hunk, later: list[Change], sha: Optional[str]) -> None: + lo = max(1, hunk.new_start) + hi = lo + max(hunk.new_lines, 1) - 1 + for change in later: + lo, hi = _shift(lo, change.hunks), _shift(hi, change.hunks) + lo = min(max(1, lo), total) + items.append((lo, min(max(lo, hi), total), sha, hunk)) + + if mode == "touched": + for i, change in enumerate(line.changes): + for hunk in change.hunks: + place(hunk, line.changes[i + 1 :], change.sha) + for hunk in line.net: + place(hunk, [], None) + + drafts: list[Draft] = [] + for lo, hi, sha, hunk in sorted(items, key=lambda item: (item[0], item[1])): + draft = drafts[-1] if drafts and lo <= drafts[-1].span[1] + RANGE_GAP else None + if draft is None: + draft = Draft(line, "", "range", "", measured=False, reason=reason) + draft.span = (lo, hi) + drafts.append(draft) + draft.span = (draft.span[0], max(draft.span[1], hi)) + tally = draft.net if sha is None else draft.touches.setdefault(sha, Tally()) + tally.add(_whole([hunk])) + for draft in drafts: + lo, hi = draft.span + draft.key = f"L{lo}-{hi}" + draft.name = f"lines {lo}–{hi}" if hi > lo else f"line {lo}" + return drafts + + +def _file_draft(line: Lineage, reason: str) -> Draft: + """The whole file as one chunk.""" + draft = Draft(line, "", "file", line.shown_path.rsplit("/", 1)[-1], measured=False, reason=reason) + for change in line.changes: + if change.hunks or change.renamed: + draft.touches.setdefault(change.sha, Tally()).add(_whole(change.hunks)) + draft.net = _whole(line.net) + return draft + + +def _drafts_for(store: BlobStore, line: Lineage, mode: str) -> list[Draft]: + line.file_class, class_reason = file_classes.classify(line.shown_path) + if line.file_class in OUT_OF_SCOPE_CLASSES: + return [_file_draft(line, f"Not analyzed: {line.file_class} file ({class_reason})")] + if store.text(line.head) is None and store.text(line.base) is None and (line.head or line.base): + return [_file_draft(line, "Not analyzed: binary content")] + if language_for_path(line.shown_path) in SYMBOL_LANGUAGES: + return _symbol_drafts(store, line) + parsed = store.parsed(line.head) or store.parsed(line.base) + reason = (parsed.reason if parsed else None) or "No analyzer reads this file type" + if line.base is None or line.head is None: + return [_file_draft(line, reason)] + return _range_drafts(store, line, mode, reason) + + +# -- pairing ------------------------------------------------------------------- + + +def _leaf(key: str) -> str: + return _ORDINAL.sub("", key).rsplit(".", 1)[-1] + + +def _similar(a: tuple[str, ...], b: tuple[str, ...]) -> float: + if a == b: + return 1.0 + matcher = SequenceMatcher(None, a, b, autojunk=False) + if matcher.real_quick_ratio() < UNCERTAIN_AT or matcher.quick_ratio() < UNCERTAIN_AT: + return 0.0 + return matcher.ratio() + + +def _pair_moves(drafts: list[Draft]) -> list[Draft]: + """Pair a symbol that left one place with the same body arriving in another. + + Same name and a near-identical body is ``moved``. A looser body match, or a + new name on an unchanged body in the same file, is ``uncertain``. + """ + gone = [d for d in drafts if d.kind in ("function", "class") and d.base and not d.head] + new = [d for d in drafts if d.kind in ("function", "class") and d.head and not d.base] + if not gone or not new: + return drafts + by_leaf: dict[str, list[Draft]] = defaultdict(list) + by_file: dict[int, list[Draft]] = defaultdict(list) + for draft in gone: + by_leaf[_leaf(draft.key)].append(draft) + by_file[id(draft.lineage)].append(draft) + taken: set[int] = set() + for arrival in new: + body = arrival.head.body + best: Optional[tuple[float, Draft, str]] = None + if len(body) >= 2: + for source in by_leaf[_leaf(arrival.key)]: + if id(source) in taken or source.kind != arrival.kind: + continue + ratio = _similar(source.base.body, body) + if ratio >= UNCERTAIN_AT and (best is None or ratio > best[0]): + best = (ratio, source, "moved" if ratio >= MOVED_AT else "uncertain") + if best is None and len(body) >= 4: + # A renamed symbol: compare the bodies without their first lines, which hold the names. + for source in by_file[id(arrival.lineage)]: + if id(source) in taken or source.kind != arrival.kind: + continue + ratio = _similar(source.base.body[1:], body[1:]) + if ratio >= MOVED_AT and (best is None or ratio > best[0]): + best = (ratio, source, "uncertain") + if best is None: + continue + _, source, match = best + taken.add(id(source)) + arrival.base, arrival.base_lineage, arrival.base_key, arrival.match = source.base, source.lineage, source.key, match + for sha, tally in source.touches.items(): + arrival.touches.setdefault(sha, Tally()).add(tally) + arrival.sources.setdefault(sha, []).append((source.lineage, source.key)) + arrival.net.hunks.extend(source.net.hunks) + if source.lineage is not arrival.lineage: + source.lineage.gave_chunk = True + return [d for d in drafts if id(d) not in taken] + + +# -- finishing ----------------------------------------------------------------- + + +def _slice(store: BlobStore, ref: Optional[BlobRef], scope: Optional[Scope]) -> list[str]: + if ref is None or scope is None: + return [] + return _lines(store, ref)[scope.start - 1 : scope.end] + + +def _metrics(before: Optional[dict], after: Optional[dict]) -> dict[str, BeforeAfter]: + out = {} + for name in dict.fromkeys([*(before or {}), *(after or {})]): + pair = BeforeAfter(before=(before or {}).get(name), after=(after or {}).get(name)) + if pair.before is not None or pair.after is not None: + out[name] = pair + return out + + +def _buckets(chunk: LensChunk) -> list[str]: + complexity = chunk.metrics.get("complexity") + if chunk.kind != "function" or chunk.file_class not in BUCKET_CLASSES or complexity is None: + return [] + before, after = complexity.before, complexity.after + out = [] + changed = chunk.net_added + chunk.net_deleted > 0 + if before is not None and after is not None and before >= COMPLEX_AT and changed: + out.append("complex-and-changed") + if after is not None and after >= COMPLEX_AT and (chunk.status == "added" or (before is not None and after > before)): + out.append("new-hotspot") + return out + + +def _finish(store: BlobStore, draft: Draft, mode: str, order: dict[str, int], times: dict[str, Optional[int]]) -> LensChunk: + line = draft.lineage + base_file = draft.base_file + path = line.shown_path + if draft.is_symbol: + has_base, has_head = draft.base is not None, draft.head is not None + else: + has_base, has_head = line.base is not None, line.head is not None + + net = draft.net + metrics: dict[str, BeforeAfter] = {} + if draft.is_symbol and has_base and has_head: + old, new = _slice(store, base_file.base, draft.base), _slice(store, line.head, draft.head) + if draft.base_lineage is not None or (net.lines and old == new): + # A move: the lines that differ are the body's own, not the whole relocated block. + body = textdiff.hunks(old, new) + net = Tally(sum(h.new_lines for h in body), sum(h.old_lines for h in body), net.hunks) + if draft.kind in ("function", "class"): + metrics = _metrics(draft.base.metrics if draft.base else None, draft.head.metrics if draft.head else None) + elif draft.kind == "file" and draft.measured: + base, head = store.parsed(line.base), store.parsed(line.head) + metrics = _metrics(base.file_metrics if base else None, head.file_metrics if head else None) + + touches = sorted(draft.touches.items(), key=lambda item: order.get(item[0], 0)) + churn = sum(t.lines for _, t in touches) + crossed = base_file.shown_path != path if draft.base_lineage else line.renamed + reason = draft.reason + if has_base and has_head: + match = draft.match or ("renamed-file" if line.renamed else "exact") + if draft.base_lineage is not None: + status = "moved" if match == "moved" or crossed else "modified" + elif net.lines == 0: + # Touched and back where it started, or carried along unchanged (a rename, a move in its file). + status = "reverted" if draft.net.lines == 0 and churn else "moved" + else: + status = "moved" if line.renamed else "modified" + elif has_head: + match, status = "none", "added" + elif has_base: + match, status = "none", "deleted" + else: + match, status = "none", "reverted" + if draft.is_symbol: + reason = "Added and removed inside the window: nothing to measure at base or head" + + scope = draft.head or draft.base or draft.seen + if draft.kind == "range": + start, end = draft.span + elif scope is not None: + start, end = scope.start, scope.end + else: + start, end = 1, max(1, len(_lines(store, line.head or line.base))) + base_span: tuple[Optional[int], Optional[int]] = (None, None) + if draft.base is not None: + base_span = (draft.base.start, draft.base.end) + elif draft.kind == "file" and line.base is not None: + base_span = (1, max(1, len(_lines(store, line.base)))) + + old_path = base_file.base.path if has_base and base_file.base and base_file.base.path != path else None + stamps = [times.get(sha) for sha, _ in touches] + stamps = [t for t in stamps if t is not None] + if mode == "net": + hunks = [LensHunk(sha=None, old_start=h.old_start, old_lines=h.old_lines, new_start=h.new_start, new_lines=h.new_lines) for h in draft.net.hunks] + else: + hunks = [ + LensHunk(sha=sha, old_start=h.old_start, old_lines=h.old_lines, new_start=h.new_start, new_lines=h.new_lines) + for sha, tally in touches + for h in tally.hunks + ] + chunk = LensChunk( + id=f"{path}::{draft.key}" if draft.key else path, + path=path, + old_path=old_path, + kind=draft.kind, + name=draft.name, + start_line=start, + end_line=end, + base_start_line=base_span[0], + base_end_line=base_span[1], + status=status, + match=match, + added=net.added if mode == "net" else sum(t.added for _, t in touches), + deleted=net.deleted if mode == "net" else sum(t.deleted for _, t in touches), + net_added=net.added, + net_deleted=net.deleted, + touches=[] if mode == "net" else [LensTouch(sha=sha, added=t.added, deleted=t.deleted) for sha, t in touches], + first_touched=iso(stamps[0]) if stamps else None, + last_touched=iso(stamps[-1]) if stamps else None, + metrics=metrics, + measured=draft.measured and not (status == "reverted" and not has_base), + na_reason=reason, + buckets=[], + file_class=line.file_class, + hunks=hunks, + ) + chunk.buckets = _buckets(chunk) + return chunk + + +# -- build --------------------------------------------------------------------- + + +def build(window: Window, mode: str) -> Built: + repo, scope = window.repo, window.scope + store = BlobStore(repo) + left_out: set[str] = set() + + def keep(diffs: list[FileDiff]) -> list[FileDiff]: + keeping = kept(diffs) + ids = {id(d) for d in keeping} + left_out.update(d.path for d in diffs if id(d) not in ids and d.path) + return keeping + + read = [c for c in window.commits if not c.upstream_merge] + per_commit = commit_diffs(repo, [c.sha for c in read], scope) + steps = [(c.sha, keep(per_commit.get(c.sha, []))) for c in read] + dirty: set[str] = set() + has_worktree = False + if window.wants_worktree: + diffs = keep(tree_diff(repo, window.tip.sha, None, scope)) + if diffs: + has_worktree = True + steps.append((WORKTREE, diffs)) + dirty = {p for d in diffs for p in (d.old_path, d.new_path) if p} + net: list[FileDiff] = [] + if has_worktree or window.base_tree != window.tip.sha: + net = keep(tree_diff(repo, window.base_tree, None if has_worktree else window.tip.sha, scope)) + + lineages = _follow(window, steps) + _resolve_ends(window, store, lineages, net, dirty, has_worktree) + store.load( + [ref for line in lineages for change in line.changes for ref in (change.old, change.new) if ref] + + [ref for line in lineages for ref in (line.base, line.head) if ref] + ) + + store.warm( + ref + for line in lineages + if language_for_path(line.path) in SYMBOL_LANGUAGES and file_classes.classify(line.path)[0] not in OUT_OF_SCOPE_CLASSES + for ref in (line.base, line.head, *(side for change in line.changes for side in (change.old, change.new))) + if ref + ) + drafts: list[Draft] = [] + for line in lineages: + drafts.extend(_drafts_for(store, line, mode)) + drafts = _pair_moves(drafts) + + order = {sha: i for i, (sha, _) in enumerate(steps)} + times: dict[str, Optional[int]] = {c.sha: c.ts for c in window.commits} + finished: list[Draft] = [] + for draft in drafts: + if mode == "touched" and not draft.touches: + continue + draft.chunk = _finish(store, draft, mode, order, times) + if mode == "net" and draft.chunk.status == "reverted": + continue + finished.append(draft) + + # A renamed file with no edited chunk still shows, as one unchanged chunk. + with_chunks = {id(d.lineage) for d in finished} + for line in lineages: + if not line.renamed or id(line) in with_chunks: + continue + if mode == "touched" and not line.changes: + continue + draft = _file_draft(line, "") + draft.net = Tally() + parsed = store.parsed(line.head) + draft.measured = bool(parsed and not parsed.reason and parsed.language != "markdown" and line.file_class not in OUT_OF_SCOPE_CLASSES) + draft.reason = None if draft.measured else (parsed.reason if parsed and parsed.reason else "No code metrics for this file") + draft.touches = {c.sha: Tally() for c in line.changes if c.renamed} + draft.chunk = _finish(store, draft, mode, order, times) + finished.append(draft) + + finished.sort(key=lambda d: (d.chunk.path, d.chunk.start_line, d.chunk.id)) + by_id: dict[str, Draft] = {} + for draft in finished: + # Two scopes can share an id only through odd names; keep ids unique. + chunk_id, n = draft.chunk.id, 2 + while chunk_id in by_id: + chunk_id = f"{draft.chunk.id}~{n}" + n += 1 + draft.chunk.id = chunk_id + by_id[chunk_id] = draft + + files = _files(store, lineages, finished, mode) + commits = _commits(window, steps, finished, has_worktree) + return Built(window, mode, store, commits, [d.chunk for d in finished], files, by_id, has_worktree, len(left_out)) + + +def _files(store: BlobStore, lineages: list[Lineage], drafts: list[Draft], mode: str) -> list[LensFile]: + chunk_ids: dict[int, list[str]] = defaultdict(list) + for draft in drafts: + chunk_ids[id(draft.lineage)].append(draft.chunk.id) + files = [] + for line in lineages: + ids = chunk_ids.get(id(line), []) + if not ids and not line.gave_chunk: + continue + if line.base is None and line.head is None: + status = "deleted" + elif line.base is None: + status = "added" + elif line.head is None: + status = "deleted" + else: + status = "renamed" if line.renamed else "modified" + if mode == "net": + added, deleted = sum(h.new_lines for h in line.net), sum(h.old_lines for h in line.net) + else: + added = sum(h.new_lines for c in line.changes for h in c.hunks) + deleted = sum(h.old_lines for c in line.changes for h in c.hunks) + loc: Optional[int] = None + if line.head is not None: + if line.file_class in OUT_OF_SCOPE_CLASSES: + loc = sum(1 for text in _lines(store, line.head) if text.strip()) + else: + parsed = store.parsed(line.head) + loc = parsed.loc if parsed else 0 + files.append( + LensFile( + path=line.shown_path, + old_path=line.base.path if line.renamed else None, + status=status, + added=added, + deleted=deleted, + loc=loc, + file_class=line.file_class, + chunk_ids=ids, + out_of_scope=line.file_class in OUT_OF_SCOPE_CLASSES, + ) + ) + files.sort(key=lambda f: f.path) + return files + + +def _commits(window: Window, steps: list[tuple[str, list[FileDiff]]], drafts: list[Draft], has_worktree: bool) -> list[LensCommit]: + touched: dict[str, list[str]] = defaultdict(list) + for draft in drafts: + for sha in draft.touches: + touched[sha].append(draft.chunk.id) + diffs = dict(steps) + out = [] + for commit in window.commits: + own = diffs.get(commit.sha, []) + out.append( + LensCommit( + sha=commit.sha, + short=commit.short, + subject=commit.subject, + author=commit.author, + date=iso(commit.ts), + files=len(own), + added=sum(d.added for d in own), + deleted=sum(d.deleted for d in own), + chunk_ids=touched.get(commit.sha, []), + ) + ) + if has_worktree: + own = diffs.get(WORKTREE, []) + out.append( + LensCommit( + sha=WORKTREE, + short="worktree", + subject="Uncommitted changes", + author="", + date=None, + files=len(own), + added=sum(d.added for d in own), + deleted=sum(d.deleted for d in own), + chunk_ids=touched.get(WORKTREE, []), + ) + ) + return out + + +def metric_ids(chunks: list[LensChunk]) -> list[str]: + """Metrics with a before/after on at least one chunk, in catalog order.""" + used = {name for chunk in chunks for name in chunk.metrics} + ordered = [spec.id for spec in metric_catalog.METRICS if spec.id in used] + return ordered + sorted(used - set(ordered)) diff --git a/server/app/services/changelens/hunks.py b/server/app/services/changelens/hunks.py new file mode 100644 index 00000000..03759700 --- /dev/null +++ b/server/app/services/changelens/hunks.py @@ -0,0 +1,195 @@ +"""Git diffs as per-file hunks: one ``git log -p -U0`` pass for the commits, one diff for the working tree, one for net.""" + +from __future__ import annotations + +import re +from dataclasses import dataclass, field +from pathlib import Path +from typing import Optional + +from app.config import BINARY_EXTENSIONS, IGNORE_FILES +from app.services.analysis import _has_ignored_extension, _is_pruned_dir_name, classify_candidate +from app.services.compare.git_refs import run_git + +GIT_TIMEOUT = 60.0 +RECORD = "\x1e" +_LOG_FORMAT = f"--format={RECORD}%H" +_DIFF_FLAGS = ("--raw", "--no-abbrev", "-p", "-U0", "-M", "--no-color", "--no-ext-diff", "--no-textconv") +_HUNK = re.compile(r"^@@ -(\d+)(?:,(\d+))? \+(\d+)(?:,(\d+))? @@") +_NULL_SHA = "0" * 40 +_FILE_MODES = {"100644", "100755"} + + +@dataclass(frozen=True) +class Hunk: + """Line ranges as git prints them with ``-U0``: ``old_lines`` deleted, ``new_lines`` added.""" + + old_start: int + old_lines: int + new_start: int + new_lines: int + + +@dataclass +class FileDiff: + status: str + """A, M, D or R.""" + old_path: Optional[str] + new_path: Optional[str] + old_sha: Optional[str] + new_sha: Optional[str] + """None when the side is absent, or when the new side is a file on disk.""" + hunks: list[Hunk] = field(default_factory=list) + binary: bool = False + + @property + def path(self) -> str: + return self.new_path or self.old_path or "" + + @property + def added(self) -> int: + return sum(h.new_lines for h in self.hunks) + + @property + def deleted(self) -> int: + return sum(h.old_lines for h in self.hunks) + + +def is_skipped(path: str) -> bool: + """True for a path srcly's scan never lists: ignored names and extensions, binary types, pruned folders.""" + parts = path.split("/") + name = parts[-1] + if any(_is_pruned_dir_name(part) for part in parts[:-1]): + return True + if name in IGNORE_FILES or _has_ignored_extension(name): + return True + return Path(name).suffix.lower() in BINARY_EXTENSIONS + + +def _unquote(path: str) -> str: + """Git quotes a path with a tab, a newline or a quote in it, C style.""" + if len(path) >= 2 and path.startswith('"') and path.endswith('"'): + try: + return path[1:-1].encode("latin-1", "backslashreplace").decode("unicode_escape").encode("latin-1").decode("utf-8") + except (UnicodeError, ValueError): + return path[1:-1] + return path + + +def _raw_entry(line: str) -> Optional[FileDiff]: + meta, _, rest = line.partition("\t") + fields = meta.split() + if len(fields) != 5: + return None + old_mode, new_mode, old_sha, new_sha, status = fields[0].lstrip(":"), fields[1], fields[2], fields[3], fields[4][0] + paths = [_unquote(p) for p in rest.split("\t")] + if status in ("R", "C") and len(paths) == 2: + old_path, new_path = paths + if status == "C": + status, old_path, old_sha = "A", None, _NULL_SHA + else: + old_path = new_path = paths[0] + if status == "T": + status = "M" + if status not in ("A", "M", "D", "R"): + return None + diff = FileDiff( + status, + None if status == "A" else old_path, + None if status == "D" else new_path, + None if status == "A" or old_sha == _NULL_SHA else old_sha, + None if status == "D" or new_sha == _NULL_SHA else new_sha, + ) + # Symlinks and submodules are not files to read. Marked like binaries, so they are left out. + live_modes = [m for m in (old_mode, new_mode) if m != "000000"] + if any(m not in _FILE_MODES for m in live_modes): + diff.binary = True + return diff + + +def parse(text: str) -> dict[str, list[FileDiff]]: + """Parse ``--raw -p -U0`` output. Keys are commit shas; a plain ``git diff`` lands under ``""``. + + Git prints one raw line and then one ``diff --git`` section per file, in the same order. + """ + out: dict[str, list[FileDiff]] = {} + entries: list[FileDiff] = out.setdefault("", []) + section = -1 + for line in text.split("\n"): + if not line: + continue + first = line[0] + if first in "+- \\": + continue + if first == RECORD: + entries = out.setdefault(line[1:].strip(), []) + section = -1 + elif first == ":": + entry = _raw_entry(line) + # A placeholder keeps sections and entries in step. + entries.append(entry or FileDiff("M", None, None, None, None, binary=True)) + elif line.startswith("diff --git "): + section += 1 + elif first == "@": + match = _HUNK.match(line) + if match and 0 <= section < len(entries): + old_start, old_lines, new_start, new_lines = match.groups() + entries[section].hunks.append( + Hunk(int(old_start), int(old_lines or 1), int(new_start), int(new_lines or 1)) + ) + elif line.startswith(("Binary files ", "GIT binary patch")) and 0 <= section < len(entries): + entries[section].binary = True + if not out[""]: + del out[""] + return out + + +def _pathspec(scope: str) -> list[str]: + return ["--", scope] if scope else [] + + +def commit_diffs(repo: Path, shas: list[str], scope: str) -> dict[str, list[FileDiff]]: + """Each commit's diff against its first parent.""" + if not shas: + return {} + text = run_git( + repo, "log", "--no-walk=unsorted", "--diff-merges=first-parent", _LOG_FORMAT, *_DIFF_FLAGS, *shas, *_pathspec(scope), + timeout=GIT_TIMEOUT, + ) + return parse(text) + + +def tree_diff(repo: Path, base: str, head: Optional[str], scope: str) -> list[FileDiff]: + """``base`` against ``head``, or against the files on disk (untracked files count as added) when ``head`` is None.""" + revs = [base] if head is None else [base, head] + text = run_git(repo, "diff", *_DIFF_FLAGS, *revs, *_pathspec(scope), timeout=GIT_TIMEOUT) + diffs = parse(text).get("", []) + if head is None: + diffs.extend(_untracked(repo, scope)) + return diffs + + +def _untracked(repo: Path, scope: str) -> list[FileDiff]: + raw = run_git(repo, "ls-files", "-z", "--others", "--exclude-standard", *_pathspec(scope), timeout=GIT_TIMEOUT) + out = [] + for path in filter(None, raw.split("\0")): + if is_skipped(path): + continue + full = repo / path + if not full.is_file() or full.is_symlink() or classify_candidate(full) != "ok": + continue + try: + with open(full, "rb") as f: + lines = len(f.read().splitlines()) + except OSError: + continue + diff = FileDiff("A", None, path, None, None) + if lines: + diff.hunks.append(Hunk(0, 0, 1, lines)) + out.append(diff) + return out + + +def kept(diffs: list[FileDiff]) -> list[FileDiff]: + """The diffs Change Lens reads: not binary, and not skipped by srcly's ignore rules.""" + return [d for d in diffs if not d.binary and not is_skipped(d.path)] diff --git a/server/app/services/changelens/models.py b/server/app/services/changelens/models.py new file mode 100644 index 00000000..a2110dee --- /dev/null +++ b/server/app/services/changelens/models.py @@ -0,0 +1,162 @@ +"""Change Lens payload models. They mirror ``client/src/changelens/types.ts`` field for field.""" + +from __future__ import annotations + +from typing import Literal, Optional + +from pydantic import BaseModel, ConfigDict +from pydantic.alias_generators import to_camel + +WORKTREE = "WORKTREE" + +LensWindow = Literal["branch", "24h", "7d", "custom"] +LensMode = Literal["touched", "net"] +ChunkKind = Literal["function", "class", "heading", "file", "range"] +ChunkStatus = Literal["modified", "added", "deleted", "moved", "reverted"] +ChunkMatch = Literal["exact", "renamed-file", "moved", "uncertain", "none"] +ReviewBucket = Literal["complex-and-changed", "new-hotspot"] +FileStatus = Literal["added", "deleted", "modified", "renamed"] + +# Optional in the TypeScript types (`field?:`): left out of the JSON when unset. +# Every other null is a real value ("no sha", "not measured") and stays. +_OMIT_WHEN_NONE = frozenset({"oldPath", "baseStartLine", "baseEndLine", "naReason"}) + + +class LensModel(BaseModel): + model_config = ConfigDict(alias_generator=to_camel, populate_by_name=True) + + def wire(self) -> dict: + """The JSON shape the client reads.""" + return _strip(self.model_dump(by_alias=True)) + + +def _strip(value): + if isinstance(value, dict): + return {k: _strip(v) for k, v in value.items() if not (v is None and k in _OMIT_WHEN_NONE)} + if isinstance(value, list): + return [_strip(v) for v in value] + return value + + +class LensEndpoint(LensModel): + ref: str + sha: Optional[str] + label: str + date: Optional[str] + + +class LensSelection(LensModel): + window: LensWindow + mode: LensMode + ref: str + base: LensEndpoint + head: LensEndpoint + since: Optional[str] + until: Optional[str] + timezone: str + includes_worktree: bool + notes: list[str] + repo_root: str + + +class LensCommit(LensModel): + sha: str + short: str + subject: str + author: str + date: Optional[str] + files: int + added: int + deleted: int + chunk_ids: list[str] + + +class LensHunk(LensModel): + sha: Optional[str] + old_start: int + old_lines: int + new_start: int + new_lines: int + + +class LensTouch(LensModel): + sha: str + added: int + deleted: int + + +class BeforeAfter(LensModel): + before: Optional[int | float] + after: Optional[int | float] + + +class LensChunk(LensModel): + id: str + path: str + old_path: Optional[str] = None + kind: ChunkKind + name: str + start_line: int + end_line: int + base_start_line: Optional[int] = None + base_end_line: Optional[int] = None + status: ChunkStatus + match: ChunkMatch + added: int + deleted: int + net_added: int + net_deleted: int + touches: list[LensTouch] + first_touched: Optional[str] + last_touched: Optional[str] + metrics: dict[str, BeforeAfter] + measured: bool + na_reason: Optional[str] = None + buckets: list[ReviewBucket] + file_class: str + hunks: list[LensHunk] + + +class LensFile(LensModel): + path: str + old_path: Optional[str] = None + status: FileStatus + added: int + deleted: int + loc: Optional[int] + file_class: str + chunk_ids: list[str] + out_of_scope: bool + + +class LensSummary(LensModel): + files_edited: int + files_total: int + chunks: int + churn_lines: int + net_lines: int + reverted: int + + +class ChangeLensPayload(LensModel): + selection: LensSelection + commits: list[LensCommit] + chunks: list[LensChunk] + files: list[LensFile] + summary: LensSummary + metric_ids: list[str] + + +class LensHunkDiff(LensModel): + chunk_id: str + path: str + old_path: Optional[str] = None + # `from` is a Python keyword. + from_: str + to: str + diff: str + truncated: bool + + model_config = ConfigDict( + alias_generator=lambda name: "from" if name == "from_" else to_camel(name), populate_by_name=True + ) diff --git a/server/app/services/changelens/service.py b/server/app/services/changelens/service.py new file mode 100644 index 00000000..7493a0a4 --- /dev/null +++ b/server/app/services/changelens/service.py @@ -0,0 +1,326 @@ +"""Change Lens payload, hunk diff text, and the Markdown summary the CLI prints.""" + +from __future__ import annotations + +import os +from collections import OrderedDict, defaultdict +from pathlib import Path +from typing import Optional + +from app.services.changelens import chunks as engine +from app.services.changelens import textdiff +from app.services.changelens.blobs import BlobRef, Scope +from app.services.changelens.chunks import Built, Draft, Lineage +from app.services.changelens.hunks import GIT_TIMEOUT, Hunk, is_skipped +from app.services.changelens.models import ( + WORKTREE, + ChangeLensPayload, + LensEndpoint, + LensHunkDiff, + LensSelection, + LensSummary, +) +from app.services.changelens.window import LensError, Params, Window, iso, local_timezone, resolve +from app.services.compare.git_refs import GitError, run_git + +MAX_DIFF_LINES = 2000 +CACHE_SIZE = 8 + +_cache: OrderedDict[tuple, tuple[Built, int]] = OrderedDict() + + +def clear_cache() -> None: + _cache.clear() + + +def _pathspec(scope: str) -> list[str]: + return ["--", scope] if scope else [] + + +def _worktree_print(window: Window) -> tuple: + """Changes when any uncommitted edit does: the status lines, and each listed file's size and mtime.""" + status = run_git( + window.repo, "status", "--porcelain=v1", "-z", "--untracked-files=all", *_pathspec(window.scope), timeout=GIT_TIMEOUT + ) + stats = [] + for entry in status.split("\0"): + try: + stat = os.stat(window.repo / entry[3:]) + stats.append((stat.st_mtime_ns, stat.st_size)) + except OSError: + stats.append(None) + return (status, tuple(stats)) + + +def _files_total(window: Window, has_worktree: bool) -> int: + if has_worktree: + raw = run_git( + window.repo, "ls-files", "-z", "--cached", "--others", "--exclude-standard", *_pathspec(window.scope), timeout=GIT_TIMEOUT + ) + paths = {p for p in raw.split("\0") if p and (window.repo / p).is_file()} + else: + raw = run_git(window.repo, "ls-tree", "-r", "-z", "--name-only", window.tip.sha, *_pathspec(window.scope), timeout=GIT_TIMEOUT) + paths = {p for p in raw.split("\0") if p} + return sum(1 for p in paths if not is_skipped(p)) + + +def build_lens(path: Path, params: Params) -> tuple[Built, int]: + """The built window and the count of files at its head. Cached until a commit or an uncommitted edit changes.""" + try: + window = resolve(Path(path), params) + key = ( + str(window.repo), + window.scope, + params.mode, + window.base_tree, + window.tip.sha, + tuple((c.sha, c.upstream_merge) for c in window.commits), + _worktree_print(window) if window.wants_worktree else None, + ) + hit = _cache.get(key) + if hit is not None: + _cache.move_to_end(key) + hit[0].window = window + return hit + built = engine.build(window, params.mode) + result = (built, _files_total(window, built.has_worktree)) + except GitError as exc: + raise LensError(str(exc)) from exc + _cache[key] = result + while len(_cache) > CACHE_SIZE: + _cache.popitem(last=False) + return result + + +def _head_endpoint(built: Built) -> LensEndpoint: + tip = built.window.tip + if built.has_worktree: + return LensEndpoint(ref=WORKTREE, sha=None, label=f"Working tree on {tip.label}", date=None) + return LensEndpoint(ref=tip.ref, sha=tip.sha, label=tip.label, date=iso(tip.ts)) + + +def build_payload(path: Path, params: Params) -> dict: + if params.mode not in ("touched", "net"): + raise LensError(f"Unknown mode: {params.mode}") + built, files_total = build_lens(path, params) + window = built.window + notes = list(window.notes) + if built.left_out: + plural = "s" if built.left_out != 1 else "" + notes.append(f"{built.left_out} changed file{plural} left out: binary, or skipped by srcly's ignore rules (lockfiles, .json, images).") + if not built.chunks and not any("no commits" in n.lower() or "empty" in n.lower() or "no base" in n.lower() for n in notes): + notes.append("Nothing changed in this window.") + base = window.base + selection = LensSelection( + window=params.window, + mode=params.mode, + ref=params.ref, + base=LensEndpoint(ref=base.ref, sha=base.sha, label=base.label, date=iso(base.ts)), + head=_head_endpoint(built), + since=iso(window.since), + until=iso(window.until), + timezone=local_timezone()[0], + includes_worktree=window.wants_worktree, + notes=notes, + repo_root=str(window.repo), + ) + chunks = built.chunks + payload = ChangeLensPayload( + selection=selection, + commits=built.commits, + chunks=chunks, + files=built.files, + summary=LensSummary( + files_edited=len(built.files), + files_total=files_total, + chunks=len(chunks), + churn_lines=sum(c.added + c.deleted for c in chunks), + net_lines=sum(c.net_added + c.net_deleted for c in chunks), + reverted=sum(1 for c in chunks if c.status == "reverted"), + ), + metric_ids=engine.metric_ids(chunks), + ) + return payload.wire() + + +# -- hunk diff ----------------------------------------------------------------- + + +def _near(hunks: list[Hunk]): + """A filter for diff hunks: keep those that meet one of the chunk's recorded git hunks.""" + + def meets(lo: int, hi: int, start: int, count: int) -> bool: + if hi < lo: + lo, hi = hi, lo + other_hi = start + max(count, 1) - 1 + return lo <= other_hi + 1 and start <= hi + 1 + + def keep(old_lo: int, old_hi: int, new_lo: int, new_hi: int) -> bool: + return any(meets(old_lo, old_hi, h.old_start, h.old_lines) or meets(new_lo, new_hi, h.new_start, h.new_lines) for h in hunks) + + return keep + + +def _change_at(line: Lineage, sha: str): + return next((c for c in line.changes if c.sha == sha), None) + + +def hunk_diff(path: Path, params: Params, chunk_id: str, commit: Optional[str] = None) -> dict: + built, _ = build_lens(path, params) + draft = built.drafts.get(chunk_id) + if draft is None: + raise LensError(f"No chunk {chunk_id} in this selection", status=404) + store, window, line = built.store, built.window, draft.lineage + chunk = draft.chunk + + old_ref: Optional[BlobRef] = None + new_ref: Optional[BlobRef] = None + old_scope: Optional[Scope] = None + new_scope: Optional[Scope] = None + recorded: list[Hunk] = [] + if commit is None: + old_ref, new_ref = draft.base_file.base, line.head + old_scope, new_scope = draft.base, draft.head + recorded = draft.net.hunks + label_from, label_to = window.base.label, _head_endpoint(built).label + else: + sha = next((s for s in draft.touches if s == commit or (len(commit) >= 7 and s.startswith(commit))), None) + if commit == WORKTREE: + label_from, label_to = window.tip.label, "Working tree" + else: + label_from, label_to = f"{commit[:7]}^", commit[:7] + if sha is not None: + recorded = draft.touches[sha].hunks + places: list[tuple[Lineage, str]] = [(line, draft.key), *draft.sources.get(sha, [])] + for place, key in places: + change = _change_at(place, sha) + if change is None: + continue + if not draft.is_symbol: + old_ref, new_ref = change.old, change.new + break + before, after = store.parsed(change.old), store.parsed(change.new) + if old_scope is None and before and key in before.scopes: + old_ref, old_scope = change.old, before.scopes[key] + if new_scope is None and after and key in after.scopes: + new_ref, new_scope = change.new, after.scopes[key] + + old_lines = (store.text(old_ref) or "").splitlines() + new_lines = (store.text(new_ref) or "").splitlines() + if draft.is_symbol: + old_at = old_scope.start - 1 if old_scope else 0 + new_at = new_scope.start - 1 if new_scope else 0 + body = textdiff.unified( + old_lines[old_at : old_scope.end] if old_scope else [], + new_lines[new_at : new_scope.end] if new_scope else [], + old_at, + new_at, + ) + elif draft.kind == "file" and draft.reason == engine.OUTSIDE_SCOPES: + # Lines outside every named scope: the edits inside functions belong to other chunks. + sides = {"old": store.parsed(old_ref), "new": store.parsed(new_ref)} + + def owned(side: str, start: int, count: int) -> list[bool]: + parsed = sides[side] + return [not key for key in parsed.owners(start, count)] if parsed else [True] * count + + body = textdiff.unified(old_lines, new_lines, owned=owned) if recorded else [] + else: + body = textdiff.unified(old_lines, new_lines, keep=_near(recorded)) if recorded else [] + + truncated = len(body) > MAX_DIFF_LINES + text = "" + if body: + old_name = f"a/{old_ref.path}" if old_ref and (old_scope or not draft.is_symbol) else "/dev/null" + new_name = f"b/{new_ref.path}" if new_ref and (new_scope or not draft.is_symbol) else "/dev/null" + text = "\n".join([f"--- {old_name}", f"+++ {new_name}", *body[:MAX_DIFF_LINES]]) + "\n" + return LensHunkDiff( + chunk_id=chunk_id, + path=chunk.path, + old_path=chunk.old_path, + from_=label_from, + to=label_to, + diff=text, + truncated=truncated, + ).wire() + + +# -- markdown ------------------------------------------------------------------ + +_WINDOW_NAMES = {"branch": "This branch", "24h": "Last 24 hours", "7d": "Last 7 days", "custom": "Custom"} +_BUCKET_NAMES = {"complex-and-changed": "Already complex and changed", "new-hotspot": "New hotspot"} + + +def _number(value) -> str: + if value is None: + return "–" + return str(int(value)) if float(value).is_integer() else f"{value:.1f}" + + +def _chunk_line(chunk: dict, with_path: bool) -> str: + name = f"`{chunk['id']}`" if with_path else f"`{chunk['name']}`" + parts = [f"{name} {chunk['status']}"] + if chunk["match"] in ("moved", "uncertain", "renamed-file") and chunk.get("oldPath"): + parts.append(f"from `{chunk['oldPath']}`" + (" (uncertain match)" if chunk["match"] == "uncertain" else "")) + elif chunk["match"] == "uncertain": + parts.append("uncertain match") + complexity = chunk["metrics"].get("complexity") + if not chunk["measured"]: + parts.append("not measured") + elif complexity: + parts.append(f"complexity {_number(complexity['before'])} → {_number(complexity['after'])}") + parts.append(f"+{chunk['added']} −{chunk['deleted']}") + touches = len(chunk["touches"]) + if touches > 1: + parts.append(f"{touches} touches") + return "- " + " · ".join(parts) + + +def render_markdown(payload: dict) -> str: + sel, summary, chunks = payload["selection"], payload["summary"], payload["chunks"] + mode = "touched (every chunk a commit changed)" if sel["mode"] == "touched" else "net (base against head)" + out = [ + f"# Change Lens: {Path(sel['repoRoot']).name}", + "", + f"- Window: {_WINDOW_NAMES.get(sel['window'], sel['window'])}, {mode}, first-parent history of `{sel['ref']}`", + f"- Base: {sel['base']['label']}" + (f" ({sel['base']['date']})" if sel["base"]["date"] else ""), + f"- Head: {sel['head']['label']}" + (f" ({sel['head']['date']})" if sel["head"]["date"] else ""), + ] + if sel["since"] or sel["until"]: + out.append(f"- Time: {sel['since'] or 'start'} to {sel['until'] or 'now'} ({sel['timezone']})") + out.append( + f"- {len(payload['commits'])} commits · {summary['filesEdited']} of {summary['filesTotal']} files edited · " + f"{summary['chunks']} chunks · {summary['churnLines']} lines churned · {summary['netLines']} net · {summary['reverted']} reverted" + ) + out.extend(f"- Note: {note}" for note in sel["notes"]) + + out.extend(["", "## Review first", ""]) + any_bucket = False + for bucket, title in _BUCKET_NAMES.items(): + hits = [c for c in chunks if bucket in c["buckets"]] + if not hits: + continue + any_bucket = True + out.extend([f"### {title}", ""]) + hits.sort(key=lambda c: -((c["metrics"].get("complexity") or {}).get("after") or 0)) + out.extend(_chunk_line(c, with_path=True) for c in hits) + out.append("") + if not any_bucket: + out.extend(["No chunk matches a review bucket.", ""]) + + out.extend(["## Edited chunks", ""]) + by_folder: dict[str, dict[str, list[dict]]] = defaultdict(lambda: defaultdict(list)) + for chunk in chunks: + folder = chunk["path"].rsplit("/", 1)[0] if "/" in chunk["path"] else "." + by_folder[folder][chunk["path"]].append(chunk) + for folder in sorted(by_folder): + files = by_folder[folder] + count = sum(len(v) for v in files.values()) + out.extend([f"### {folder}/ ({count} chunk{'s' if count != 1 else ''} in {len(files)} file{'s' if len(files) != 1 else ''})", ""]) + for file_path in sorted(files): + out.append(f"- `{file_path.rsplit('/', 1)[-1]}`") + out.extend(" " + _chunk_line(c, with_path=False) for c in files[file_path]) + out.append("") + if not chunks: + out.extend(["No edited chunks.", ""]) + return "\n".join(out) diff --git a/server/app/services/changelens/textdiff.py b/server/app/services/changelens/textdiff.py new file mode 100644 index 00000000..4a7eecf1 --- /dev/null +++ b/server/app/services/changelens/textdiff.py @@ -0,0 +1,139 @@ +"""Line diffs of two texts: zero-context hunks, and unified text with context.""" + +from __future__ import annotations + +from difflib import SequenceMatcher +from typing import Callable, Optional + +from app.services.changelens.hunks import Hunk + +Op = tuple[str, int, int, int, int] + + +def opcodes(a: list[str], b: list[str]) -> list[Op]: + """``SequenceMatcher`` opcodes. The common head and tail are cut first, so a small edit in a big file is cheap.""" + head = 0 + limit = min(len(a), len(b)) + while head < limit and a[head] == b[head]: + head += 1 + tail = 0 + while tail < limit - head and a[len(a) - 1 - tail] == b[len(b) - 1 - tail]: + tail += 1 + ops: list[Op] = [] + if head: + ops.append(("equal", 0, head, 0, head)) + middle = SequenceMatcher(None, a[head : len(a) - tail], b[head : len(b) - tail], autojunk=False) + for tag, i1, i2, j1, j2 in middle.get_opcodes(): + ops.append((tag, i1 + head, i2 + head, j1 + head, j2 + head)) + if tail: + ops.append(("equal", len(a) - tail, len(a), len(b) - tail, len(b))) + return ops + + +def hunks(a: list[str], b: list[str]) -> list[Hunk]: + """The changes as git's ``-U0`` would number them.""" + out = [] + for tag, i1, i2, j1, j2 in opcodes(a, b): + if tag == "equal": + continue + old_lines, new_lines = i2 - i1, j2 - j1 + out.append(Hunk(i1 + 1 if old_lines else i1, old_lines, j1 + 1 if new_lines else j1, new_lines)) + return out + + +def _groups(ops: list[Op], context: int) -> list[list[Op]]: + """``difflib.SequenceMatcher.get_grouped_opcodes`` for a plain opcode list.""" + ops = list(ops) or [("equal", 0, 1, 0, 1)] + if ops[0][0] == "equal": + tag, i1, i2, j1, j2 = ops[0] + ops[0] = (tag, max(i1, i2 - context), i2, max(j1, j2 - context), j2) + if ops[-1][0] == "equal": + tag, i1, i2, j1, j2 = ops[-1] + ops[-1] = (tag, i1, min(i2, i1 + context), j1, min(j2, j1 + context)) + groups: list[list[Op]] = [] + group: list[Op] = [] + for tag, i1, i2, j1, j2 in ops: + if tag == "skip": + # A change that belongs to another chunk: it ends the hunk and is never printed. + groups.append(group) + group = [] + continue + if tag == "equal" and i2 - i1 > 2 * context: + group.append((tag, i1, min(i2, i1 + context), j1, min(j2, j1 + context))) + groups.append(group) + group = [] + i1, j1 = max(i1, i2 - context), max(j1, j2 - context) + group.append((tag, i1, i2, j1, j2)) + if group and not (len(group) == 1 and group[0][0] == "equal"): + groups.append(group) + return [g for g in groups if any(op[0] != "equal" for op in g)] + + +def _only_owned(ops: list[Op], owned: Callable[[str, int, int], list[bool]]) -> list[Op]: + """Cut each change into runs of lines the chunk owns and runs it does not (tagged ``skip``).""" + out: list[Op] = [] + for tag, i1, i2, j1, j2 in ops: + if tag == "equal": + out.append((tag, i1, i2, j1, j2)) + continue + for side, lo, hi in (("old", i1, i2), ("new", j1, j2)): + flags = owned(side, lo + 1, hi - lo) if hi > lo else [] + start = 0 + for n in range(1, len(flags) + 1): + if n == len(flags) or flags[n] != flags[start]: + a1, a2 = lo + start, lo + n + kind = "skip" if not flags[start] else "delete" if side == "old" else "insert" + out.append((kind, a1, a2, j1, j1) if side == "old" else (kind, i2, i2, a1, a2)) + start = n + return out + + +def _range(start: int, length: int) -> str: + if length == 1: + return str(start + 1) + return f"{start + 1 if length else start},{length}" + + +def unified( + a: list[str], + b: list[str], + a_offset: int = 0, + b_offset: int = 0, + keep: Optional[Callable[[int, int, int, int], bool]] = None, + owned: Optional[Callable[[str, int, int], list[bool]]] = None, + context: int = 3, +) -> list[str]: + """Unified diff lines (no file header) of two line lists. + + The offsets shift the printed line numbers when ``a`` and ``b`` are slices + of files. ``keep(old_start, old_end, new_start, new_end)`` filters hunks by + their 1-based changed line ranges in file coordinates. ``owned(side, start, + count)`` says, line by line, which changed lines belong to the chunk; the + rest are left out. + """ + out: list[str] = [] + ops = opcodes(a, b) + if owned is not None: + ops = _only_owned(ops, owned) + for group in _groups(ops, context): + if keep is not None: + wanted = False + for tag, i1, i2, j1, j2 in group: + if tag != "equal" and keep(a_offset + i1 + 1, a_offset + i2, b_offset + j1 + 1, b_offset + j2): + wanted = True + break + if not wanted: + continue + first, last = group[0], group[-1] + out.append( + f"@@ -{_range(a_offset + first[1], last[2] - first[1])} +{_range(b_offset + first[3], last[4] - first[3])} @@" + ) + for tag, i1, i2, j1, j2 in group: + if tag == "equal": + out.extend(" " + line for line in a[i1:i2]) + continue + if tag in ("replace", "delete"): + out.extend("-" + line for line in a[i1:i2]) + if tag in ("replace", "insert"): + out.extend("+" + line for line in b[j1:j2]) + return out diff --git a/server/app/services/changelens/window.py b/server/app/services/changelens/window.py new file mode 100644 index 00000000..fb5e19d5 --- /dev/null +++ b/server/app/services/changelens/window.py @@ -0,0 +1,352 @@ +"""A selection (window, ref, times) resolved to a base, a head and one first-parent commit list.""" + +from __future__ import annotations + +import os +import time +from dataclasses import dataclass, field +from datetime import datetime, timedelta, timezone +from pathlib import Path +from typing import Optional +from zoneinfo import ZoneInfo + +from app.services.compare.git_refs import ( + GitError, + ResolvedRef, + branch_of, + default_branch, + repo_root, + resolve_auto_base, + run_git, +) + +EMPTY_TREE = "4b825dc642cb6eb9a060e54bf8d69288fbee4904" +"""Git's empty tree: the base of a window that reaches the first commit.""" +MAX_COMMITS = 200 +"""Newest commits kept in one window. A longer window says so in its notes.""" +MAX_LISTED = 5000 +"""First-parent commits read when a time window is resolved.""" +GIT_TIMEOUT = 30.0 +WINDOW_SPANS = {"24h": timedelta(hours=24), "7d": timedelta(days=7)} + +_FORMAT = "%H%x1f%h%x1f%s%x1f%an%x1f%ct%x1f%P" + + +class LensError(Exception): + """The selection cannot be read. ``status`` is the HTTP status the router answers with.""" + + def __init__(self, message: str, status: int = 400) -> None: + super().__init__(message) + self.status = status + + +@dataclass(frozen=True) +class Params: + window: str = "branch" + mode: str = "touched" + ref: str = "HEAD" + base: Optional[str] = None + head: Optional[str] = None + since: Optional[str] = None + until: Optional[str] = None + worktree: Optional[bool] = None + + +@dataclass +class Commit: + sha: str + short: str + subject: str + author: str + ts: int + parents: list[str] + upstream_merge: bool = False + """A merge of the default branch into this branch: its diff is other people's work, so it is not read.""" + + +@dataclass +class Endpoint: + ref: str + sha: Optional[str] + label: str + ts: Optional[int] + + +@dataclass +class Window: + repo: Path + scope: str + """The scan root inside the repo (``""`` for the repo root): only files below it are read.""" + params: Params + base: Endpoint + tip: Endpoint + """The head commit. The payload's head is the working tree instead when it holds edits.""" + commits: list[Commit] + wants_worktree: bool + since: Optional[int] = None + until: Optional[int] = None + notes: list[str] = field(default_factory=list) + + @property + def base_tree(self) -> str: + return self.base.sha or EMPTY_TREE + + +def local_timezone() -> tuple[str, timezone | ZoneInfo]: + """The server's IANA zone name and tzinfo; UTC when the name cannot be found.""" + name = os.environ.get("TZ", "") + if not name: + try: + link = os.path.realpath("/etc/localtime") + if "zoneinfo/" in link: + name = link.split("zoneinfo/", 1)[1] + except OSError: + name = "" + try: + return name, ZoneInfo(name) + except Exception: + return "UTC", timezone.utc + + +def iso(ts: Optional[int]) -> Optional[str]: + if ts is None: + return None + return datetime.fromtimestamp(ts, local_timezone()[1]).isoformat() + + +def _parse_time(text: str, what: str) -> int: + try: + when = datetime.fromisoformat(text.strip().replace("Z", "+00:00")) + except ValueError: + raise LensError(f"{what} is not an ISO 8601 time: {text}") from None + if when.tzinfo is None: + when = when.replace(tzinfo=local_timezone()[1]) + return int(when.timestamp()) + + +def _git(repo: Path, *args: str) -> str: + return run_git(repo, *args, timeout=GIT_TIMEOUT) + + +def _sha(repo: Path, ref: str) -> str: + try: + return _git(repo, "rev-parse", "--verify", "--quiet", f"{ref}^{{commit}}").strip() + except GitError: + raise LensError(f"{ref} is not a branch, tag or commit in {repo.name}") from None + + +def _try_sha(repo: Path, ref: str) -> Optional[str]: + try: + return _git(repo, "rev-parse", "--verify", "--quiet", f"{ref}^{{commit}}").strip() or None + except GitError: + return None + + +def _commit_ts(repo: Path, sha: str) -> int: + return int(_git(repo, "log", "-1", "--format=%ct", sha).strip()) + + +def _log(repo: Path, *revs: str) -> list[Commit]: + """First-parent commits, newest first.""" + raw = _git(repo, "log", "--first-parent", f"--format={_FORMAT}", *revs) + commits = [] + for line in raw.splitlines(): + sha, short, subject, author, ts, parents = (line.split("\x1f") + [""] * 6)[:6] + if sha: + commits.append(Commit(sha, short, subject, author, int(ts or 0), parents.split())) + return commits + + +def _is_ancestor(repo: Path, older: str, newer: str) -> bool: + try: + _git(repo, "merge-base", "--is-ancestor", older, newer) + return True + except GitError: + return False + + +def _shallow_boundary(repo: Path) -> set[str]: + try: + if _git(repo, "rev-parse", "--is-shallow-repository").strip() != "true": + return set() + path = Path(_git(repo, "rev-parse", "--path-format=absolute", "--git-path", "shallow").strip()) + return set(path.read_text(encoding="utf-8").split()) + except (GitError, OSError): + return set() + + +def resolve(path: Path, params: Params) -> Window: + try: + repo = repo_root(path) + except GitError as exc: + raise LensError(str(exc), status=409) from None + head_commit = _try_sha(repo, "HEAD") + if head_commit is None: + raise LensError(f"{repo.name} has no commits yet", status=409) + start = (path if path.is_dir() else path.parent).resolve() + scope = "" if start == repo else start.relative_to(repo).as_posix() + + notes: list[str] = [] + tip_ref = params.head if params.window == "custom" and params.head else params.ref + tip_sha = _sha(repo, tip_ref) + tip_name = branch_of(repo, tip_ref) or tip_ref + tip = Endpoint(tip_ref, tip_sha, f"{tip_name} @ {tip_sha[:7]}", _commit_ts(repo, tip_sha)) + + wants_worktree = params.worktree if params.worktree is not None else tip_sha == head_commit + if wants_worktree and tip_sha != head_commit: + wants_worktree = False + notes.append(f"The working tree is left out: {tip_ref} is not the checked-out commit.") + + window = Window(repo, scope, params, base=tip, tip=tip, commits=[], wants_worktree=wants_worktree, notes=notes) + if scope: + notes.append(f"Only files under {scope}/ are read.") + boundary = _shallow_boundary(repo) + if boundary: + notes.append("This clone is shallow: history before its oldest commits is not available.") + + try: + default = default_branch(repo) + except GitError: + default = None + + if params.window == "branch": + _branch_window(window, default) + elif params.window in WINDOW_SPANS: + now = int(time.time()) + _time_window(window, now - int(WINDOW_SPANS[params.window].total_seconds()), now) + elif params.window == "custom": + if params.base: + base_sha = _sha(repo, params.base) + window.base = Endpoint(params.base, base_sha, f"{branch_of(repo, params.base) or params.base} @ {base_sha[:7]}", _commit_ts(repo, base_sha)) + window.commits = _log(repo, f"{base_sha}..{tip_sha}")[::-1] + if not _is_ancestor(repo, base_sha, tip_sha): + notes.append( + f"{params.base} is not an ancestor of {tip_ref}. Net compares the two snapshots; touched reads only {tip_ref}'s own commits." + ) + if params.since or params.until: + notes.append("The base revision sets this window; since and until are not used.") + elif params.since or params.until: + since = _parse_time(params.since, "since") if params.since else 0 + until = _parse_time(params.until, "until") if params.until else int(time.time()) + _time_window(window, since, until) + else: + raise LensError("A custom window needs base (and head), or since (and until)") + else: + raise LensError(f"Unknown window: {params.window}") + + _trim(window, boundary) + _mark_upstream_merges(window, default) + return window + + +def _branch_window(window: Window, default: Optional[str]) -> None: + repo, tip, notes = window.repo, window.tip, window.notes + if default is None: + notes.append("No default branch was found (origin/HEAD, main or master), so the branch window has no base.") + return + default_name = default.removeprefix("origin/") + branch = branch_of(repo, tip.ref) + if branch == default_name: + # On the default branch itself the window is the work not yet upstream. + upstream = f"origin/{default_name}" + if _try_sha(repo, upstream) is None: + notes.append(f"{default_name} is the default branch and has no upstream: the branch window is empty. Pick a time window.") + return + base_sha = _git(repo, "merge-base", upstream, tip.sha).strip() + base_ref = upstream + label = f"{upstream} @ {base_sha[:7]}" + else: + try: + resolved = resolve_auto_base(repo, tip.ref, ResolvedRef(ref=tip.ref, sha=tip.sha, label=tip.sha[:7])) + except GitError as exc: + notes.append(f"No base was found for this branch: {exc}") + return + base_sha = resolved.sha + if resolved.pr: + target = resolved.pr.get("baseRefName") or default_name + base_ref = f"merge-base({target}, {branch or tip.ref})" + notes.append(f"The base comes from open PR #{resolved.pr['number']}, which targets {target}.") + else: + target = default_name + base_ref = f"merge-base({default}, {branch or tip.ref})" + label = f"{target} @ {base_sha[:7]}" + window.base = Endpoint(base_ref, base_sha, label, _commit_ts(repo, base_sha)) + window.commits = _log(repo, f"{base_sha}..{tip.sha}")[::-1] + if not window.commits: + notes.append(f"{branch or tip.ref} has no commits after {label}.") + + +def _time_window(window: Window, since: int, until: int) -> None: + """Commits on the ref whose committer time is in [since, until]: one unbroken first-parent run.""" + window.since, window.until = since, until + listed = _log(window.repo, "-n", str(MAX_LISTED), window.tip.sha) + run: list[Commit] = [] + for commit in listed: + if commit.ts > until: + if run: + break + continue + if commit.ts < since: + break + run.append(commit) + if not run: + window.notes.append(f"No commits on {window.tip.ref} between {iso(since)} and {iso(until)}.") + return + window.commits = run[::-1] + newest = run[0] + if newest.sha != window.tip.sha: + window.tip = Endpoint(newest.sha, newest.sha, newest.short, newest.ts) + if window.wants_worktree: + window.wants_worktree = False + window.notes.append("The working tree is left out: the window ends before the checked-out commit.") + _base_from_oldest(window) + + +def _base_from_oldest(window: Window) -> None: + oldest = window.commits[0] + if oldest.parents: + parent = oldest.parents[0] + window.base = Endpoint(f"{oldest.short}^", parent, parent[:7], _commit_ts(window.repo, parent)) + else: + window.base = Endpoint("empty tree", None, "before the first commit", None) + window.notes.append("The window reaches the first commit of the repository: its base is the empty tree.") + + +def _trim(window: Window, boundary: set[str]) -> None: + commits = window.commits + # A shallow clone's oldest commits have no parents on disk. Git would diff + # them against nothing and report the whole repository as added. + cut = max((i for i, c in enumerate(commits) if c.sha in boundary), default=-1) + if cut >= 0: + edge = commits[cut] + window.commits = commits = commits[cut + 1 :] + window.base = Endpoint(edge.sha, edge.sha, edge.short, edge.ts) + window.notes.append(f"The window starts at {edge.short}, the oldest commit this shallow clone holds.") + if len(commits) > MAX_COMMITS: + total = len(commits) + window.commits = commits[-MAX_COMMITS:] + _base_from_oldest(window) + window.notes.append( + f"The window holds {total} commits. Only the newest {MAX_COMMITS} are read, so the base moved to {window.base.label}." + ) + + +def _mark_upstream_merges(window: Window, default: Optional[str]) -> None: + merges = [c for c in window.commits if len(c.parents) > 1] + if not merges: + return + repo = window.repo + if default and not _is_ancestor(repo, window.tip.sha, default): + for commit in merges: + commit.upstream_merge = all(_is_ancestor(repo, parent, default) for parent in commit.parents[1:]) + upstream = [c for c in merges if c.upstream_merge] + counted = len(merges) - len(upstream) + if counted: + window.notes.append( + f"{counted} merge commit{'s' if counted != 1 else ''} in the window: each counts once, as its diff against its first parent." + ) + if upstream: + names = ", ".join(c.short for c in upstream) + window.notes.append( + f"{names} merged {default} into this branch. Those upstream changes are not counted as work on this branch." + ) diff --git a/server/tests/fixtures/change_lens_repo.py b/server/tests/fixtures/change_lens_repo.py new file mode 100644 index 00000000..66c27d10 --- /dev/null +++ b/server/tests/fixtures/change_lens_repo.py @@ -0,0 +1,347 @@ +"""The Change Lens acceptance repo (docs/intent/change-lens/contract.md, "Acceptance fixture"). + +``build(folder)`` makes a git repo with a ``main`` commit and a ``feature`` +branch whose commits hold each case the contract names. Dates, names and +contents are fixed, so the commit shas are the same on every machine. + + uv run python -m tests.fixtures.change_lens_repo + uv run python -m tests.fixtures.change_lens_repo --write-payloads ../client/src/changelens + +The second form also writes ``fixture.json`` (touched) and ``fixture.net.json`` +(net): the payloads the engine returns for this repo, which the client tests read. +""" + +from __future__ import annotations + +import json +import os +import subprocess +import sys +from pathlib import Path + +AUTHOR = "Fixture Author" +EMAIL = "fixture@example.com" + +FILLERS = "".join( + f""" +export function step{n:02d}(input: number) {{ + const scaled = input * {n}; + if (scaled > 100) {{ + return scaled - 100; + }} + return scaled; +}} +""" + for n in range(1, 15) +) + +PARSE_ARGS_V0 = """export function parseArgs(argv: string[]) { + const args = { verbose: false, limit: 10 }; + for (const arg of argv) { + if (arg === "--verbose") args.verbose = true; + else if (arg.startsWith("--limit=")) args.limit = Number(arg.slice(8)); + } + return args; +} +""" + +PARSE_ARGS_V1 = """export function parseArgs(argv: string[]) { + const args = { verbose: false, limit: 10, dryRun: false }; + for (const arg of argv) { + if (arg === "--verbose") args.verbose = true; + else if (arg === "--dry-run") args.dryRun = true; + else if (arg.startsWith("--limit=")) args.limit = Number(arg.slice(8)); + } + return args; +} +""" + +RETRY_V0 = """export async function retryJob(job: Job, max: number) { + while (job.attempts < max) { + try { + await job.run(); + return true; + } catch (err) { + job.attempts += 1; + if (err instanceof TypeError) throw err; + if (job.attempts >= max) break; + } + } + return false; +} +""" + +RETRY_V1 = """export async function retryJob(job: Job, max: number) { + while (job.attempts < max) { + try { + await job.run(); + return true; + } catch (err) { + job.attempts += 1; + if (err instanceof TypeError) throw err; + if (job.attempts >= max) break; + const delay = job.attempts > 3 ? 400 : 100; + await sleep(delay); + } + } + return false; +} +""" + +RETRY_V2 = RETRY_V1.replace( + " while (job.attempts < max) {\n", + " if (max > 20) max = 20;\n while (job.attempts < max) {\n", +) + +RETRY_V3 = RETRY_V2.replace( + " await sleep(delay);\n", + " const jitter = job.id.length % 2 === 0 ? 10 : job.id.length % 3 === 0 ? 20 : 0;\n" + " if (delay + jitter > 400 && job.attempts < 10) continue;\n" + " await sleep(delay + jitter);\n", +) + +WORKER_HEAD = """export interface Job { + id: string; + attempts: number; + run: () => Promise; +} + +const sleep = (ms: number) => new Promise((resolve) => setTimeout(resolve, ms)); + +""" + +FORMAT_LOG_V0 = """export function formatLog(job: Job, ok: boolean) { + return `${job.id}: ${ok ? "done" : "failed"}`; +} +""" + +FORMAT_LOG_WORKTREE = """export function formatLog(job: Job, ok: boolean) { + const attempts = job.attempts === 1 ? "1 attempt" : `${job.attempts} attempts`; + return `${job.id}: ${ok ? "done" : "failed"} after ${attempts}`; +} +""" + +LEGACY = """export function legacyCleanup(jobs: Job[]) { + for (const job of jobs) { + if (job.attempts > 100) job.attempts = 0; + } + return jobs.length; +} + +""" + + +def worker(parse_args: str, retry: str, format_log: str = FORMAT_LOG_V0, legacy: str = LEGACY) -> str: + return WORKER_HEAD + parse_args + "\n" + retry + "\n" + legacy + format_log + FILLERS + + +STRINGS = """export function titleCase(text: string) { + return text + .split(" ") + .map((word) => (word ? word[0].toUpperCase() + word.slice(1) : word)) + .join(" "); +} + +export function truncate(text: string, max: number) { + if (text.length <= max) return text; + return text.slice(0, max - 1) + "…"; +} +""" + +SCHEDULER_V0 = """import type { Job } from "./worker"; + +export function pickNext(queue: Job[]) { + for (const job of queue) { + if (job.attempts === 0) return job; + } + return queue[0]; +} + +export function isIdle(queue: Job[]) { + return queue.length === 0; +} +""" + +SCHEDULER_V1 = SCHEDULER_V0.replace( + """ for (const job of queue) { + if (job.attempts === 0) return job; + } + return queue[0]; +""", + """ const sorted = [...queue].sort((a, b) => a.attempts - b.attempts); + if (sorted.length > 1 && sorted[0].attempts === sorted[1].attempts) { + return sorted[0].id < sorted[1].id ? sorted[0] : sorted[1]; + } + return sorted[0]; +""", +) + +ROUTER_V0 = """export function dispatch(kind: string, size: number) { + if (kind === "email") return size > 10 ? "bulk-mail" : "mail"; + if (kind === "sms") return size > 5 ? "bulk-sms" : "sms"; + if (kind === "push") return "push"; + if (kind === "webhook") { + if (size > 100) return "webhook-batch"; + return "webhook"; + } + if (kind === "export" || kind === "report") return "slow"; + if (kind === "cleanup") return "idle"; + return "default"; +} +""" + +ROUTER_V1 = ROUTER_V0.replace( + ' if (kind === "cleanup") return "idle";\n', + ' if (kind === "cleanup") return "idle";\n if (kind === "audit") return size > 50 ? "slow" : "default";\n', +) + +PLANNER = """export interface Stop { + id: string; + open: boolean; + load: number; + urgent: boolean; +} + +export function planRoute(stops: Stop[], capacity: number) { + const route: string[] = []; + let load = 0; + for (const stop of stops) { + if (!stop.open) continue; + if (stop.urgent && load + stop.load > capacity) { + route.unshift(stop.id); + continue; + } + if (load + stop.load > capacity) { + if (route.length === 0) break; + load = 0; + } + if (stop.load > 0 || stop.urgent) { + route.push(stop.id); + load += stop.load; + } else if (route.length > 3 && !stop.urgent) { + route.pop(); + } + } + return route.length > 0 ? route : null; +} +""" + +README_V0 = """# Worker + +Runs queued jobs. + +## Install + +Run `pnpm install`. + +## Usage + +Run `pnpm start` with a queue file. +""" + +README_V1 = README_V0.replace("Run `pnpm install`.", "Run `pnpm install`, then `pnpm build`.") + +SETTINGS_V0 = """queue: + limit: 10 + retries: 3 +log: + level: info +""" + +SETTINGS_V1 = SETTINGS_V0.replace("retries: 3", "retries: 5") + +# (subject, date, {path: content or None to delete}, [(old, new) renames]) +COMMITS: list[tuple[str, str, dict, list]] = [ + ("retry: back off between attempts", "2026-01-06T10:00:00+00:00", {"src/worker.ts": worker(PARSE_ARGS_V0, RETRY_V1)}, []), + ("retry: cap the attempt limit", "2026-01-06T11:00:00+00:00", {"src/worker.ts": worker(PARSE_ARGS_V0, RETRY_V2)}, []), + ("scheduler: try a sorted pick", "2026-01-06T12:00:00+00:00", {"src/scheduler.ts": SCHEDULER_V1}, []), + ("rename strings to text", "2026-01-06T13:00:00+00:00", {}, [("src/util/strings.ts", "src/util/text.ts")]), + ("worker: remove legacyCleanup", "2026-01-06T14:00:00+00:00", {"src/worker.ts": worker(PARSE_ARGS_V0, RETRY_V2, legacy="")}, []), + ("docs: build step, more retries", "2026-01-06T15:00:00+00:00", {"README.md": README_V1, "config/settings.yaml": SETTINGS_V1}, []), + ("scheduler: back to the first idle job", "2026-01-06T16:00:00+00:00", {"src/scheduler.ts": SCHEDULER_V0}, []), + ("args: accept --dry-run", "2026-01-06T17:00:00+00:00", {"src/worker.ts": worker(PARSE_ARGS_V1, RETRY_V2, legacy="")}, []), + ("retry: add jitter", "2026-01-06T18:00:00+00:00", {"src/worker.ts": worker(PARSE_ARGS_V1, RETRY_V3, legacy="")}, []), + ("planner: route stops; router: audit jobs", "2026-01-06T19:00:00+00:00", {"src/planner.ts": PLANNER, "src/router.ts": ROUTER_V1}, []), +] + +BASE_FILES = { + "src/worker.ts": worker(PARSE_ARGS_V0, RETRY_V0), + "src/util/strings.ts": STRINGS, + "src/scheduler.ts": SCHEDULER_V0, + "src/router.ts": ROUTER_V0, + "README.md": README_V0, + "config/settings.yaml": SETTINGS_V0, +} +BASE_DATE = "2026-01-05T09:00:00+00:00" + +# Left uncommitted on the feature branch. +WORKTREE_FILES = {"src/worker.ts": worker(PARSE_ARGS_V1, RETRY_V3, format_log=FORMAT_LOG_WORKTREE, legacy="")} + + +def _git(repo: Path, *args: str, date: str | None = None) -> str: + # A clean environment: no GIT_DIR from a hook, no user or system config. + env = {k: v for k, v in os.environ.items() if not k.startswith("GIT_")} + env.update(GIT_CONFIG_GLOBAL=os.devnull, GIT_CONFIG_NOSYSTEM="1", GIT_AUTHOR_NAME=AUTHOR, GIT_AUTHOR_EMAIL=EMAIL) + env.update(GIT_COMMITTER_NAME=AUTHOR, GIT_COMMITTER_EMAIL=EMAIL) + if date: + env.update(GIT_AUTHOR_DATE=date, GIT_COMMITTER_DATE=date) + proc = subprocess.run(["git", *args], cwd=repo, env=env, capture_output=True, text=True, check=False) + if proc.returncode != 0: + raise RuntimeError(f"git {' '.join(args)} failed: {proc.stderr.strip()}") + return proc.stdout.strip() + + +def _write(repo: Path, files: dict) -> None: + for rel, content in files.items(): + target = repo / rel + if content is None: + target.unlink() + continue + target.parent.mkdir(parents=True, exist_ok=True) + target.write_text(content, encoding="utf-8") + + +def build(folder: Path, *, worktree_edit: bool = True) -> Path: + """Build the repo in ``folder`` (created; must be empty) and return it, on ``feature``.""" + repo = Path(folder) + repo.mkdir(parents=True, exist_ok=True) + if any(repo.iterdir()): + raise RuntimeError(f"{repo} is not empty") + repo = repo.resolve() + _git(repo, "init", "--quiet", "--initial-branch=main") + _write(repo, BASE_FILES) + _git(repo, "add", "-A") + _git(repo, "commit", "--quiet", "-m", "worker, scheduler and router", date=BASE_DATE) + _git(repo, "checkout", "--quiet", "-b", "feature") + for subject, date, files, renames in COMMITS: + for old, new in renames: + _git(repo, "mv", old, new) + _write(repo, files) + _git(repo, "add", "-A") + _git(repo, "commit", "--quiet", "-m", subject, date=date) + if worktree_edit: + _write(repo, WORKTREE_FILES) + return repo + + +def write_payloads(repo: Path, out_dir: Path) -> None: + """Write the engine's touched and net payloads for the fixture repo, with a fixed repo root and time zone.""" + os.environ["TZ"] = "UTC" + from app.services.changelens import service + from app.services.changelens.window import Params + + for mode, name in (("touched", "fixture.json"), ("net", "fixture.net.json")): + payload = service.build_payload(repo, Params(window="branch", mode=mode)) + payload["selection"]["repoRoot"] = "/repo/change-lens-fixture" + (out_dir / name).write_text(json.dumps(payload, indent=2) + "\n", encoding="utf-8") + + +if __name__ == "__main__": + args = sys.argv[1:] + if not args: + raise SystemExit(__doc__) + built = build(Path(args[0])) + print(built) + if len(args) == 3 and args[1] == "--write-payloads": + write_payloads(built, Path(args[2])) + print(f"Wrote fixture.json and fixture.net.json to {args[2]}") diff --git a/server/tests/test_change_lens.py b/server/tests/test_change_lens.py new file mode 100644 index 00000000..4d4a2ad1 --- /dev/null +++ b/server/tests/test_change_lens.py @@ -0,0 +1,175 @@ +"""Change Lens on the acceptance repo (docs/intent/change-lens/contract.md).""" + +from __future__ import annotations + +import subprocess + +import pytest +from fastapi.testclient import TestClient + +from app.main import app +from app.services.changelens import service +from app.services.changelens.models import WORKTREE +from app.services.changelens.window import LensError, Params +from tests.fixtures.change_lens_repo import BASE_DATE, COMMITS, build + + +@pytest.fixture(scope="module") +def repo(tmp_path_factory): + return build(tmp_path_factory.mktemp("change-lens") / "repo") + + +@pytest.fixture(scope="module") +def touched(repo): + return service.build_payload(repo, Params(mode="touched")) + + +@pytest.fixture(scope="module") +def net(repo): + return service.build_payload(repo, Params(mode="net")) + + +def by_id(payload: dict) -> dict[str, dict]: + return {chunk["id"]: chunk for chunk in payload["chunks"]} + + +def _rev(repo, ref: str) -> str: + return subprocess.run(["git", "rev-parse", ref], cwd=repo, capture_output=True, text=True, check=True).stdout.strip() + + +def test_branch_window_resolves_base_head_and_commits(repo, touched): + selection = touched["selection"] + assert selection["base"]["sha"] == _rev(repo, "main") + assert selection["base"]["date"].startswith(BASE_DATE[:4]) + # The head is the working tree, because it holds an uncommitted edit. + assert selection["head"] == {"ref": WORKTREE, "sha": None, "label": selection["head"]["label"], "date": None} + assert "feature" in selection["head"]["label"] + assert [c["subject"] for c in touched["commits"][:-1]] == [subject for subject, *_ in COMMITS] + assert touched["commits"][-1]["sha"] == WORKTREE and touched["commits"][-1]["date"] is None + + +def test_only_edited_chunks_are_listed(touched): + ids = set(by_id(touched)) + # worker.ts holds 18 functions; four were edited. + assert {i for i in ids if i.startswith("src/worker.ts")} == { + "src/worker.ts::parseArgs", + "src/worker.ts::retryJob", + "src/worker.ts::legacyCleanup", + "src/worker.ts::formatLog", + } + assert "src/scheduler.ts::isIdle" not in ids + assert touched["summary"]["filesEdited"] == len(touched["files"]) == 7 + + +def test_repeated_edits_count_touches_and_separate_churn_from_net(touched): + chunks = by_id(touched) + retry, parse = chunks["src/worker.ts::retryJob"], chunks["src/worker.ts::parseArgs"] + assert len(retry["touches"]) == 3 and len(parse["touches"]) == 1 + assert (retry["added"], retry["deleted"]) == (6, 1) + assert (retry["netAdded"], retry["netDeleted"]) == (5, 0) + complexity = retry["metrics"]["complexity"] + assert complexity["before"] < 10 <= complexity["after"] + assert retry["firstTouched"] < retry["lastTouched"] + + +def test_reverted_work_shows_in_touched_and_not_in_net(touched, net): + pick = by_id(touched)["src/scheduler.ts::pickNext"] + assert pick["status"] == "reverted" and len(pick["touches"]) == 2 + assert pick["netAdded"] == pick["netDeleted"] == 0 and pick["added"] > 0 + assert pick["metrics"]["complexity"]["before"] == pick["metrics"]["complexity"]["after"] + assert touched["summary"]["reverted"] == 1 + + assert "src/scheduler.ts::pickNext" not in by_id(net) + assert net["summary"]["reverted"] == 0 + assert all(chunk["touches"] == [] and all(h["sha"] is None for h in chunk["hunks"]) for chunk in net["chunks"]) + # Everything else is a net difference too. + assert set(by_id(net)) == set(by_id(touched)) - {"src/scheduler.ts::pickNext"} + + +def test_pure_rename_reports_no_complexity_change(touched, net): + for payload in (touched, net): + moved = by_id(payload)["src/util/text.ts"] + assert (moved["status"], moved["match"], moved["oldPath"]) == ("moved", "renamed-file", "src/util/strings.ts") + assert moved["netAdded"] == moved["netDeleted"] == 0 + assert moved["metrics"]["complexity"]["before"] == moved["metrics"]["complexity"]["after"] + assert moved["buckets"] == [] + renamed = next(f for f in touched["files"] if f["path"] == "src/util/text.ts") + assert renamed["status"] == "renamed" and renamed["oldPath"] == "src/util/strings.ts" + + +def test_deleted_function_stays_with_its_base_lines(touched): + gone = by_id(touched)["src/worker.ts::legacyCleanup"] + assert (gone["status"], gone["match"]) == ("deleted", "none") + assert gone["baseEndLine"] - gone["baseStartLine"] == 5 + assert gone["metrics"]["complexity"]["before"] == 3 and gone["metrics"]["complexity"]["after"] is None + + +def test_markdown_and_config_are_not_measured_and_get_no_symbol(touched): + chunks = by_id(touched) + heading = chunks["README.md::## Install"] + config = next(c for c in touched["chunks"] if c["path"] == "config/settings.yaml") + assert (heading["kind"], heading["name"]) == ("heading", "## Install") + assert (config["kind"], config["name"], config["startLine"]) == ("range", "line 3", 3) + for chunk in (heading, config): + assert chunk["measured"] is False and chunk["metrics"] == {} and chunk["naReason"] and chunk["buckets"] == [] + + +def test_review_buckets(touched): + chunks = by_id(touched) + plan = chunks["src/planner.ts::planRoute"] + assert plan["status"] == "added" and plan["buckets"] == ["new-hotspot"] + assert plan["metrics"]["complexity"] == {"before": None, "after": plan["metrics"]["complexity"]["after"]} + assert "complex-and-changed" in chunks["src/router.ts::dispatch"]["buckets"] + assert chunks["src/worker.ts::retryJob"]["buckets"] == ["new-hotspot"] + assert chunks["src/worker.ts::parseArgs"]["buckets"] == [] + + +def test_working_tree_is_one_optional_entry(repo, touched): + log = by_id(touched)["src/worker.ts::formatLog"] + assert [t["sha"] for t in log["touches"]] == [WORKTREE] + assert log["firstTouched"] is None and log["lastTouched"] is None + assert touched["commits"][-1]["chunkIds"] == ["src/worker.ts::formatLog"] + + committed = service.build_payload(repo, Params(worktree=False)) + assert "src/worker.ts::formatLog" not in by_id(committed) + assert committed["selection"]["head"]["sha"] == _rev(repo, "feature") + assert all(c["sha"] != WORKTREE for c in committed["commits"]) + + +def test_time_window_base_is_the_parent_of_its_oldest_commit(repo): + payload = service.build_payload( + repo, Params(window="custom", since="2026-01-06T17:00:00+00:00", until="2026-01-06T18:30:00+00:00") + ) + assert [c["subject"] for c in payload["commits"]] == ["args: accept --dry-run", "retry: add jitter"] + assert payload["selection"]["base"]["sha"] == _rev(repo, "feature~3") + assert payload["selection"]["head"]["sha"] == _rev(repo, "feature~1") + assert set(by_id(payload)) == {"src/worker.ts::parseArgs", "src/worker.ts::retryJob"} + + +def test_hunk_diff_for_a_chunk_and_for_one_commit(repo, touched): + whole = service.hunk_diff(repo, Params(), "src/worker.ts::retryJob") + assert "+ if (max > 20) max = 20;" in whole["diff"] and "jitter" in whole["diff"] + # Only this chunk's lines: no neighbour, though parseArgs and formatLog changed in the same file. + assert "parseArgs" not in whole["diff"] and "formatLog" not in whole["diff"] and "dryRun" not in whole["diff"] + + cap = next(c for c in touched["commits"] if c["subject"] == "retry: cap the attempt limit") + one = service.hunk_diff(repo, Params(), "src/worker.ts::retryJob", cap["sha"]) + added = [line for line in one["diff"].splitlines() if line.startswith("+") and not line.startswith("+++")] + assert added == ["+ if (max > 20) max = 20;"] + assert one["to"] == cap["short"] + + assert service.hunk_diff(repo, Params(), "src/scheduler.ts::pickNext")["diff"] == "" + with pytest.raises(LensError): + service.hunk_diff(repo, Params(), "src/worker.ts::nope") + + +def test_api_serves_the_payload_and_refuses_a_folder_outside_git(repo, tmp_path): + client = TestClient(app) + ok = client.get("/api/change-lens", params={"path": str(repo), "mode": "net", "worktree": "0"}) + assert ok.status_code == 200 + assert ok.json()["selection"]["mode"] == "net" and ok.json()["selection"]["includesWorktree"] is False + + hunk = client.get("/api/change-lens/hunk", params={"path": str(repo), "chunk": "src/router.ts::dispatch"}) + assert hunk.status_code == 200 and "audit" in hunk.json()["diff"] and hunk.json()["from"].startswith("main") + + assert client.get("/api/change-lens", params={"path": str(tmp_path)}).status_code == 409 From 044585e3d357297a3cccb5b5860472885a0b47cf Mon Sep 17 00:00:00 2001 From: Byron Wall Date: Sat, 21 Nov 2026 13:57:47 -0500 Subject: [PATCH 03/16] fix(server): Change Lens shallow-window note and worktree flag Co-Authored-By: Claude Fable 5.2 --- server/app/services/changelens/service.py | 2 +- server/app/services/changelens/window.py | 5 ++++- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/server/app/services/changelens/service.py b/server/app/services/changelens/service.py index 7493a0a4..190df9a4 100644 --- a/server/app/services/changelens/service.py +++ b/server/app/services/changelens/service.py @@ -120,7 +120,7 @@ def build_payload(path: Path, params: Params) -> dict: since=iso(window.since), until=iso(window.until), timezone=local_timezone()[0], - includes_worktree=window.wants_worktree, + includes_worktree=built.has_worktree, notes=notes, repo_root=str(window.repo), ) diff --git a/server/app/services/changelens/window.py b/server/app/services/changelens/window.py index fb5e19d5..5db73ca9 100644 --- a/server/app/services/changelens/window.py +++ b/server/app/services/changelens/window.py @@ -29,6 +29,7 @@ GIT_TIMEOUT = 30.0 WINDOW_SPANS = {"24h": timedelta(hours=24), "7d": timedelta(days=7)} +_ROOT_NOTE = "The window reaches the first commit of the repository: its base is the empty tree." _FORMAT = "%H%x1f%h%x1f%s%x1f%an%x1f%ct%x1f%P" @@ -309,7 +310,7 @@ def _base_from_oldest(window: Window) -> None: window.base = Endpoint(f"{oldest.short}^", parent, parent[:7], _commit_ts(window.repo, parent)) else: window.base = Endpoint("empty tree", None, "before the first commit", None) - window.notes.append("The window reaches the first commit of the repository: its base is the empty tree.") + window.notes.append(_ROOT_NOTE) def _trim(window: Window, boundary: set[str]) -> None: @@ -321,6 +322,8 @@ def _trim(window: Window, boundary: set[str]) -> None: edge = commits[cut] window.commits = commits = commits[cut + 1 :] window.base = Endpoint(edge.sha, edge.sha, edge.short, edge.ts) + if _ROOT_NOTE in window.notes: + window.notes.remove(_ROOT_NOTE) window.notes.append(f"The window starts at {edge.short}, the oldest commit this shallow clone holds.") if len(commits) > MAX_COMMITS: total = len(commits) From 437ef77c87563d84b29025a599ba205af4c374ba Mon Sep 17 00:00:00 2001 From: Byron Wall Date: Sat, 21 Nov 2026 13:57:47 -0500 Subject: [PATCH 04/16] docs: Change Lens contract follows the engine (#397) - selection.repoRoot: payload paths are relative to it. - A file no analyzer reads gives range chunks. A whole file (added, deleted, renamed with no edit, generated) is one file chunk. - Upstream merges, the 200-commit cap and left-out files are noted. - Hunk diffs hold only the chunk's own lines. Co-Authored-By: Claude Fable 5.2 --- client/src/changelens/types.ts | 15 +++++++++------ docs/intent/change-lens/contract.md | 24 +++++++++++++++--------- 2 files changed, 24 insertions(+), 15 deletions(-) diff --git a/client/src/changelens/types.ts b/client/src/changelens/types.ts index c848e48e..70dc20fc 100644 --- a/client/src/changelens/types.ts +++ b/client/src/changelens/types.ts @@ -32,10 +32,12 @@ export interface LensSelection { until: string | null; /** IANA name the server used to print times. */ timezone: string; - /** True when staged and unstaged edits are part of the result. */ + /** True when staged and unstaged edits are part of the result. Then `head` is the working tree. */ includesWorktree: boolean; /** Plain sentences the UI shows as is: shallow history, clipped commit list, how merges were walked. */ notes: string[]; + /** Absolute path of the git repo. Every `path` in the payload is relative to it. */ + repoRoot: string; } export interface LensCommit { @@ -57,21 +59,21 @@ export type ChunkKind = | "function" // an executable scope the analyzer named (function, method, component, arrow const) | "class" // a display scope: class, type, object literal | "heading" // a Markdown heading section - | "file" // hunks outside any symbol, or a file with no symbols (config, JSON) - | "range"; // nearby hunks in a file the analyzers can't read, grouped by line range + | "file" // hunks outside any symbol; or a whole file: added, deleted, renamed with no edit, generated + | "range"; // nearby hunks in a file the analyzers can't read (YAML, CSS), grouped by line range export type ChunkStatus = | "modified" | "added" // not in base | "deleted" // not in head; lines are base lines - | "moved" // same symbol, other file or renamed file, body unchanged or changed + | "moved" // same symbol in another file, a renamed file, or another place in its file; body unchanged or changed | "reverted"; // touched in the window, identical at base and head (touched mode only) /** How the chunk at head was paired with base. */ export type ChunkMatch = | "exact" // same path, same qualified name | "renamed-file" // git reports the file as renamed; same qualified name - | "moved" // same name and near-identical body in another file + | "moved" // same name and near-identical body in another file or scope | "uncertain" // best guess; the UI labels it | "none"; // added or deleted: nothing to pair @@ -117,7 +119,7 @@ export interface LensChunk { baseEndLine?: number; status: ChunkStatus; match: ChunkMatch; - /** Sum over every touch (churn = added + deleted). */ + /** Sum over every touch (churn = added + deleted). In net mode, the same as netAdded and netDeleted. */ added: number; deleted: number; /** Lines that differ between base and head for this chunk. 0 for reverted. */ @@ -144,6 +146,7 @@ export interface LensFile { path: string; oldPath?: string; status: "added" | "deleted" | "modified" | "renamed"; + /** Touched mode: lines summed over the window's commits. Net mode: base against head. */ added: number; deleted: number; /** Lines of code at head; null for deleted files. Area of the file in Show in repo. */ diff --git a/docs/intent/change-lens/contract.md b/docs/intent/change-lens/contract.md index 91bf378d..725f28dc 100644 --- a/docs/intent/change-lens/contract.md +++ b/docs/intent/change-lens/contract.md @@ -22,32 +22,38 @@ The payload types are in `client/src/changelens/types.ts`. That file is the cont Returns `ChangeLensPayload`. A folder that is not a git repo returns 409 with a reason. An empty window returns 200 with empty lists and a note. +Every `path` in the payload is relative to `selection.repoRoot`. When `path` is a folder inside the repo, only files below it are read, and a note says so. + ### `GET /api/change-lens/hunk` `path`, the same selection params, `chunk` (a chunk id), and an optional `commit` (a sha or `WORKTREE`). Returns `LensHunkDiff`. Without `commit`, the diff runs from the selection's base to its head. With `commit`, it runs from that commit's first parent to the commit. +The diff holds only the chunk's own lines. A symbol's diff compares its body on the two sides, so a moved symbol shows only what changed in it. A `file` chunk's diff leaves out the lines inside named scopes. An unknown chunk id returns 404. + ### `srcly changes [path]` Prints the same result as Markdown: the resolved selection, the review buckets, then the chunks grouped by folder, with `before → after` complexity. Flags: `--window`, `--mode`, `--ref`, `--base`, `--head`, `--since`, `--until`, `--no-worktree`, `--json`. ## Time rules -1. **One history.** Read `git log --first-parent `. A merge commit counts once, as its diff against its first parent. Activity from other branches never mixes in. Say this in `selection.notes` when the window holds a merge. +1. **One history.** Read `git log --first-parent `. A merge commit counts once, as its diff against its first parent. Activity from other branches never mixes in. Say this in `selection.notes` when the window holds a merge. A merge of the default branch into a feature branch is listed, but its diff is not read: those are upstream changes. A note names it. 2. **Branch window.** Base is the merge-base of `ref` and the default branch, found the way `compare/git_refs.py` (`resolve_auto_base`) finds it. Head is `ref`. On the default branch itself, the base is the upstream tip (`origin/`); when that equals head, the window is empty and a note says so. 3. **Time windows.** Commits whose committer date is inside `[since, until]`. Base is the first parent of the oldest commit in the window. Head is the newest. `24h` and `7d` count back from now. -4. **Working tree.** Staged and unstaged edits are one entry, `WORKTREE`, after the last commit. It has no time. Untracked files count as added, unless srcly's ignore rules skip them. +4. **Working tree.** Staged and unstaged edits are one entry, `WORKTREE`, after the last commit. It has no time. Untracked files count as added, unless srcly's ignore rules skip them. The entry exists only when the tree holds edits; then `selection.head` is the working tree. 5. **Touched against net.** Touched is the union of chunks that any commit in the window changed. Net compares the base snapshot with the head snapshot (the working tree, when included). A chunk that was changed and then put back has status `reverted` in touched mode and is absent in net mode. -6. **Shallow or incomplete history.** State it in `selection.notes`. Never guess a base. +6. **Shallow or incomplete history.** State it in `selection.notes`. Never guess a base. A window never reads past the oldest commit a shallow clone holds. +7. **Long windows.** A window keeps its newest 200 commits. The base moves to the parent of the oldest one kept, and a note says so. +8. **Files left out.** Binary files and files that srcly's ignore rules skip (lockfiles, `.json`, images) are not in the payload. A note gives their count. ## Chunk rules 1. Parse each changed file once per side (base and head) with the analyzer the scan uses for it. Parse the blob (`git show :`), not a checkout. Do not scan whole snapshots. -2. A hunk belongs to the innermost named scope that holds its lines: on the new side for added and changed lines, on the old side for deleted lines. Executable scopes win over display scopes. -3. Hunks outside every scope form one `file` chunk for that file. In Markdown, a hunk belongs to its nearest heading section (`heading`). In a file no analyzer reads, hunks within 10 lines of each other form one `range` chunk. -4. Never invent a symbol. A chunk with no analyzer has `measured: false`, an empty `metrics`, and a `naReason`. -5. Pair head and base chunks by path and qualified name (`exact`). When git reports a rename, pair by qualified name across the two paths (`renamed-file`). A symbol that left one file and appeared in another with a near-identical body is `moved`. A line-number overlap alone is never a match. A weaker guess is `uncertain`. Unpaired chunks are `added` or `deleted`. -6. A pure rename with an unchanged body has equal `before` and `after`. It is not an improvement or a regression. -7. Deleted chunks stay in touched mode, with their base lines. +2. A hunk belongs to the innermost named scope that holds its lines: on the new side for added and changed lines, on the old side for deleted lines. Executable scopes win over display scopes. A callback with no name belongs to the named scope around it. A nested named scope has a qualified name (`Outer.inner`, `Class.method`). Comment and decorator lines right above a scope belong to it. +3. Hunks outside every scope form one `file` chunk for that file. In Markdown, a hunk belongs to its nearest heading section (`heading`). In a file no analyzer reads (YAML, CSS, shell), hunks within 10 lines of each other form one `range` chunk; when such a file is added or deleted, it is one `file` chunk. A generated or vendored file is one `file` chunk and is not analyzed. +4. Never invent a symbol. A chunk with no analyzer has `measured: false`, an empty `metrics`, and a `naReason`. So do `heading` chunks and `file` chunks: no code metric applies to them. +5. Pair head and base chunks by path and qualified name (`exact`). When git reports a rename, pair by qualified name across the two paths (`renamed-file`). A symbol that left one file and appeared in another with a near-identical body is `moved` (same name, 90% of body lines equal). A line-number overlap alone is never a match. A weaker guess is `uncertain`: the same name with 60% to 90% equal, or a new name on an unchanged body in the same file. Unpaired chunks are `added` or `deleted`. +6. A pure rename with an unchanged body has equal `before` and `after`. It is not an improvement or a regression. A file renamed with no edit is one `file` chunk with status `moved`, no changed lines, and file-level metrics (`loc`, max `complexity`) that are equal on both sides. +7. Deleted chunks stay in touched mode, with their base lines. A symbol added and removed inside the window is `reverted`, with no metrics. 8. Touches come from mapping each commit's hunks to the chunk through the file's symbols at that commit. `touches.length` is the number of commits that changed the chunk. 9. Use one analysis version for both sides. From 511795084eb2a80667a518b47daa14e776505bbe Mon Sep 17 00:00:00 2001 From: Byron Wall Date: Sat, 21 Nov 2026 14:01:01 -0500 Subject: [PATCH 05/16] test: Change Lens fixture payloads and model parity check (#397) fixture.json and fixture.net.json are the payloads the engine returns for the acceptance repo. The ranking parity snapshot scans tests/fixtures, so it gains the new fixture builder. Co-Authored-By: Claude Fable 5.2 --- client/src/changelens/fixture.json | 1116 ++++++++++++++++++++ client/src/changelens/fixture.net.json | 909 ++++++++++++++++ server/app/services/changelens/service.py | 2 +- server/tests/snapshots/ranking-parity.json | 264 ++++- server/tests/test_change_lens.py | 16 + 5 files changed, 2249 insertions(+), 58 deletions(-) create mode 100644 client/src/changelens/fixture.json create mode 100644 client/src/changelens/fixture.net.json diff --git a/client/src/changelens/fixture.json b/client/src/changelens/fixture.json new file mode 100644 index 00000000..54f15c95 --- /dev/null +++ b/client/src/changelens/fixture.json @@ -0,0 +1,1116 @@ +{ + "selection": { + "window": "branch", + "mode": "touched", + "ref": "HEAD", + "base": { + "ref": "merge-base(main, feature)", + "sha": "97e5cf7df943e4232f3d2efcdd499dd7d0a70d63", + "label": "main @ 97e5cf7", + "date": "2026-01-05T09:00:00+00:00" + }, + "head": { + "ref": "WORKTREE", + "sha": null, + "label": "Working tree on feature @ 0c1943e", + "date": null + }, + "since": null, + "until": null, + "timezone": "UTC", + "includesWorktree": true, + "notes": [], + "repoRoot": "/repo/change-lens-fixture" + }, + "commits": [ + { + "sha": "2036666a05e59375b6a19dff1e41f8a914133451", + "short": "2036666", + "subject": "retry: back off between attempts", + "author": "Fixture Author", + "date": "2026-01-06T10:00:00+00:00", + "files": 1, + "added": 2, + "deleted": 0, + "chunkIds": [ + "src/worker.ts::retryJob" + ] + }, + { + "sha": "519844cbebcde3860bd926666ecd36517e396092", + "short": "519844c", + "subject": "retry: cap the attempt limit", + "author": "Fixture Author", + "date": "2026-01-06T11:00:00+00:00", + "files": 1, + "added": 1, + "deleted": 0, + "chunkIds": [ + "src/worker.ts::retryJob" + ] + }, + { + "sha": "8b98925caa5c902b877b0fa56a81ea9064306d46", + "short": "8b98925", + "subject": "scheduler: try a sorted pick", + "author": "Fixture Author", + "date": "2026-01-06T12:00:00+00:00", + "files": 1, + "added": 4, + "deleted": 3, + "chunkIds": [ + "src/scheduler.ts::pickNext" + ] + }, + { + "sha": "c19696660996461f9922f262e0c3a30245f143c9", + "short": "c196966", + "subject": "rename strings to text", + "author": "Fixture Author", + "date": "2026-01-06T13:00:00+00:00", + "files": 1, + "added": 0, + "deleted": 0, + "chunkIds": [ + "src/util/text.ts" + ] + }, + { + "sha": "2a74e4ce39f075a472e3c3c13b372d9beaa4b298", + "short": "2a74e4c", + "subject": "worker: remove legacyCleanup", + "author": "Fixture Author", + "date": "2026-01-06T14:00:00+00:00", + "files": 1, + "added": 0, + "deleted": 7, + "chunkIds": [ + "src/worker.ts::legacyCleanup" + ] + }, + { + "sha": "c9382753c54b7845c5b3aacef78a523c46ebcb4b", + "short": "c938275", + "subject": "docs: build step, more retries", + "author": "Fixture Author", + "date": "2026-01-06T15:00:00+00:00", + "files": 2, + "added": 2, + "deleted": 2, + "chunkIds": [ + "README.md::## Install", + "config/settings.yaml::L3-3" + ] + }, + { + "sha": "4dd738aba8d2e93cf29326ff849ca1e531182a4f", + "short": "4dd738a", + "subject": "scheduler: back to the first idle job", + "author": "Fixture Author", + "date": "2026-01-06T16:00:00+00:00", + "files": 1, + "added": 3, + "deleted": 4, + "chunkIds": [ + "src/scheduler.ts::pickNext" + ] + }, + { + "sha": "fa015dd3440f3cbacd27376749f5cd0187dd6b73", + "short": "fa015dd", + "subject": "args: accept --dry-run", + "author": "Fixture Author", + "date": "2026-01-06T17:00:00+00:00", + "files": 1, + "added": 2, + "deleted": 1, + "chunkIds": [ + "src/worker.ts::parseArgs" + ] + }, + { + "sha": "cd07328565c1c06f90eb5a440bbfaf1647c00c58", + "short": "cd07328", + "subject": "retry: add jitter", + "author": "Fixture Author", + "date": "2026-01-06T18:00:00+00:00", + "files": 1, + "added": 3, + "deleted": 1, + "chunkIds": [ + "src/worker.ts::retryJob" + ] + }, + { + "sha": "0c1943eb48825c52ff0ff143f5dbf8c95a029b60", + "short": "0c1943e", + "subject": "planner: route stops; router: audit jobs", + "author": "Fixture Author", + "date": "2026-01-06T19:00:00+00:00", + "files": 2, + "added": 30, + "deleted": 0, + "chunkIds": [ + "src/planner.ts", + "src/planner.ts::planRoute", + "src/router.ts::dispatch" + ] + }, + { + "sha": "WORKTREE", + "short": "worktree", + "subject": "Uncommitted changes", + "author": "", + "date": null, + "files": 1, + "added": 2, + "deleted": 1, + "chunkIds": [ + "src/worker.ts::formatLog" + ] + } + ], + "chunks": [ + { + "id": "README.md::## Install", + "path": "README.md", + "kind": "heading", + "name": "## Install", + "startLine": 5, + "endLine": 8, + "baseStartLine": 5, + "baseEndLine": 8, + "status": "modified", + "match": "exact", + "added": 1, + "deleted": 1, + "netAdded": 1, + "netDeleted": 1, + "touches": [ + { + "sha": "c9382753c54b7845c5b3aacef78a523c46ebcb4b", + "added": 1, + "deleted": 1 + } + ], + "firstTouched": "2026-01-06T15:00:00+00:00", + "lastTouched": "2026-01-06T15:00:00+00:00", + "metrics": {}, + "measured": false, + "naReason": "Markdown sections have no code metrics", + "buckets": [], + "fileClass": "docs", + "hunks": [ + { + "sha": "c9382753c54b7845c5b3aacef78a523c46ebcb4b", + "oldStart": 7, + "oldLines": 1, + "newStart": 7, + "newLines": 1 + } + ] + }, + { + "id": "config/settings.yaml::L3-3", + "path": "config/settings.yaml", + "kind": "range", + "name": "line 3", + "startLine": 3, + "endLine": 3, + "status": "modified", + "match": "exact", + "added": 1, + "deleted": 1, + "netAdded": 1, + "netDeleted": 1, + "touches": [ + { + "sha": "c9382753c54b7845c5b3aacef78a523c46ebcb4b", + "added": 1, + "deleted": 1 + } + ], + "firstTouched": "2026-01-06T15:00:00+00:00", + "lastTouched": "2026-01-06T15:00:00+00:00", + "metrics": {}, + "measured": false, + "naReason": "No analyzer reads this file type", + "buckets": [], + "fileClass": "config", + "hunks": [ + { + "sha": "c9382753c54b7845c5b3aacef78a523c46ebcb4b", + "oldStart": 3, + "oldLines": 1, + "newStart": 3, + "newLines": 1 + } + ] + }, + { + "id": "src/planner.ts", + "path": "src/planner.ts", + "kind": "file", + "name": "planner.ts", + "startLine": 1, + "endLine": 29, + "status": "added", + "match": "none", + "added": 6, + "deleted": 0, + "netAdded": 6, + "netDeleted": 0, + "touches": [ + { + "sha": "0c1943eb48825c52ff0ff143f5dbf8c95a029b60", + "added": 6, + "deleted": 0 + } + ], + "firstTouched": "2026-01-06T19:00:00+00:00", + "lastTouched": "2026-01-06T19:00:00+00:00", + "metrics": {}, + "measured": false, + "naReason": "Lines outside every named scope", + "buckets": [], + "fileClass": "code", + "hunks": [ + { + "sha": "0c1943eb48825c52ff0ff143f5dbf8c95a029b60", + "oldStart": 0, + "oldLines": 0, + "newStart": 1, + "newLines": 29 + } + ] + }, + { + "id": "src/planner.ts::planRoute", + "path": "src/planner.ts", + "kind": "function", + "name": "planRoute", + "startLine": 8, + "endLine": 29, + "status": "added", + "match": "none", + "added": 23, + "deleted": 0, + "netAdded": 23, + "netDeleted": 0, + "touches": [ + { + "sha": "0c1943eb48825c52ff0ff143f5dbf8c95a029b60", + "added": 23, + "deleted": 0 + } + ], + "firstTouched": "2026-01-06T19:00:00+00:00", + "lastTouched": "2026-01-06T19:00:00+00:00", + "metrics": { + "parameter_count": { + "before": null, + "after": 2 + }, + "ts_type_interface_count": { + "before": null, + "after": 0 + }, + "comment_noise_docblock": { + "before": null, + "after": 0 + }, + "comment_noise": { + "before": null, + "after": 0 + }, + "comment_noise_ratio": { + "before": null, + "after": 0 + }, + "cognitive_complexity": { + "before": null, + "after": 17 + }, + "max_nesting_depth": { + "before": null, + "after": 3 + }, + "complexity": { + "before": null, + "after": 12 + }, + "comment_noise_commented_code": { + "before": null, + "after": 0 + }, + "todo_count": { + "before": null, + "after": 0 + }, + "comment_lines": { + "before": null, + "after": 0 + }, + "loc": { + "before": null, + "after": 22 + } + }, + "measured": true, + "buckets": [ + "new-hotspot" + ], + "fileClass": "code", + "hunks": [ + { + "sha": "0c1943eb48825c52ff0ff143f5dbf8c95a029b60", + "oldStart": 0, + "oldLines": 0, + "newStart": 1, + "newLines": 29 + } + ] + }, + { + "id": "src/router.ts::dispatch", + "path": "src/router.ts", + "kind": "function", + "name": "dispatch", + "startLine": 1, + "endLine": 13, + "baseStartLine": 1, + "baseEndLine": 12, + "status": "modified", + "match": "exact", + "added": 1, + "deleted": 0, + "netAdded": 1, + "netDeleted": 0, + "touches": [ + { + "sha": "0c1943eb48825c52ff0ff143f5dbf8c95a029b60", + "added": 1, + "deleted": 0 + } + ], + "firstTouched": "2026-01-06T19:00:00+00:00", + "lastTouched": "2026-01-06T19:00:00+00:00", + "metrics": { + "parameter_count": { + "before": 2, + "after": 2 + }, + "ts_type_interface_count": { + "before": 0, + "after": 0 + }, + "comment_noise_docblock": { + "before": 0, + "after": 0 + }, + "comment_noise": { + "before": 0, + "after": 0 + }, + "comment_noise_ratio": { + "before": 0, + "after": 0 + }, + "cognitive_complexity": { + "before": 13, + "after": 16 + }, + "max_nesting_depth": { + "before": 2, + "after": 2 + }, + "complexity": { + "before": 11, + "after": 13 + }, + "comment_noise_commented_code": { + "before": 0, + "after": 0 + }, + "todo_count": { + "before": 0, + "after": 0 + }, + "comment_lines": { + "before": 0, + "after": 0 + }, + "loc": { + "before": 12, + "after": 13 + } + }, + "measured": true, + "buckets": [ + "complex-and-changed", + "new-hotspot" + ], + "fileClass": "code", + "hunks": [ + { + "sha": "0c1943eb48825c52ff0ff143f5dbf8c95a029b60", + "oldStart": 10, + "oldLines": 0, + "newStart": 11, + "newLines": 1 + } + ] + }, + { + "id": "src/scheduler.ts::pickNext", + "path": "src/scheduler.ts", + "kind": "function", + "name": "pickNext", + "startLine": 3, + "endLine": 8, + "baseStartLine": 3, + "baseEndLine": 8, + "status": "reverted", + "match": "exact", + "added": 7, + "deleted": 7, + "netAdded": 0, + "netDeleted": 0, + "touches": [ + { + "sha": "8b98925caa5c902b877b0fa56a81ea9064306d46", + "added": 4, + "deleted": 3 + }, + { + "sha": "4dd738aba8d2e93cf29326ff849ca1e531182a4f", + "added": 3, + "deleted": 4 + } + ], + "firstTouched": "2026-01-06T12:00:00+00:00", + "lastTouched": "2026-01-06T16:00:00+00:00", + "metrics": { + "parameter_count": { + "before": 1, + "after": 1 + }, + "ts_type_interface_count": { + "before": 0, + "after": 0 + }, + "comment_noise_docblock": { + "before": 0, + "after": 0 + }, + "comment_noise": { + "before": 0, + "after": 0 + }, + "comment_noise_ratio": { + "before": 0, + "after": 0 + }, + "cognitive_complexity": { + "before": 3, + "after": 3 + }, + "max_nesting_depth": { + "before": 2, + "after": 2 + }, + "complexity": { + "before": 3, + "after": 3 + }, + "comment_noise_commented_code": { + "before": 0, + "after": 0 + }, + "todo_count": { + "before": 0, + "after": 0 + }, + "comment_lines": { + "before": 0, + "after": 0 + }, + "loc": { + "before": 6, + "after": 6 + } + }, + "measured": true, + "buckets": [], + "fileClass": "code", + "hunks": [ + { + "sha": "8b98925caa5c902b877b0fa56a81ea9064306d46", + "oldStart": 4, + "oldLines": 2, + "newStart": 4, + "newLines": 3 + }, + { + "sha": "8b98925caa5c902b877b0fa56a81ea9064306d46", + "oldStart": 7, + "oldLines": 1, + "newStart": 8, + "newLines": 1 + }, + { + "sha": "4dd738aba8d2e93cf29326ff849ca1e531182a4f", + "oldStart": 4, + "oldLines": 3, + "newStart": 4, + "newLines": 2 + }, + { + "sha": "4dd738aba8d2e93cf29326ff849ca1e531182a4f", + "oldStart": 8, + "oldLines": 1, + "newStart": 7, + "newLines": 1 + } + ] + }, + { + "id": "src/util/text.ts", + "path": "src/util/text.ts", + "oldPath": "src/util/strings.ts", + "kind": "file", + "name": "text.ts", + "startLine": 1, + "endLine": 11, + "baseStartLine": 1, + "baseEndLine": 11, + "status": "moved", + "match": "renamed-file", + "added": 0, + "deleted": 0, + "netAdded": 0, + "netDeleted": 0, + "touches": [ + { + "sha": "c19696660996461f9922f262e0c3a30245f143c9", + "added": 0, + "deleted": 0 + } + ], + "firstTouched": "2026-01-06T13:00:00+00:00", + "lastTouched": "2026-01-06T13:00:00+00:00", + "metrics": { + "loc": { + "before": 10, + "after": 10 + }, + "complexity": { + "before": 2, + "after": 2 + }, + "cognitive_complexity": { + "before": 1, + "after": 1 + } + }, + "measured": true, + "buckets": [], + "fileClass": "code", + "hunks": [] + }, + { + "id": "src/worker.ts::parseArgs", + "path": "src/worker.ts", + "kind": "function", + "name": "parseArgs", + "startLine": 9, + "endLine": 17, + "baseStartLine": 9, + "baseEndLine": 16, + "status": "modified", + "match": "exact", + "added": 2, + "deleted": 1, + "netAdded": 2, + "netDeleted": 1, + "touches": [ + { + "sha": "fa015dd3440f3cbacd27376749f5cd0187dd6b73", + "added": 2, + "deleted": 1 + } + ], + "firstTouched": "2026-01-06T17:00:00+00:00", + "lastTouched": "2026-01-06T17:00:00+00:00", + "metrics": { + "parameter_count": { + "before": 1, + "after": 1 + }, + "ts_type_interface_count": { + "before": 0, + "after": 0 + }, + "comment_noise_docblock": { + "before": 0, + "after": 0 + }, + "comment_noise": { + "before": 0, + "after": 0 + }, + "comment_noise_ratio": { + "before": 0, + "after": 0 + }, + "cognitive_complexity": { + "before": 4, + "after": 5 + }, + "max_nesting_depth": { + "before": 2, + "after": 2 + }, + "complexity": { + "before": 4, + "after": 5 + }, + "comment_noise_commented_code": { + "before": 0, + "after": 0 + }, + "todo_count": { + "before": 0, + "after": 0 + }, + "comment_lines": { + "before": 0, + "after": 0 + }, + "loc": { + "before": 8, + "after": 9 + } + }, + "measured": true, + "buckets": [], + "fileClass": "code", + "hunks": [ + { + "sha": "fa015dd3440f3cbacd27376749f5cd0187dd6b73", + "oldStart": 10, + "oldLines": 1, + "newStart": 10, + "newLines": 1 + }, + { + "sha": "fa015dd3440f3cbacd27376749f5cd0187dd6b73", + "oldStart": 12, + "oldLines": 0, + "newStart": 13, + "newLines": 1 + } + ] + }, + { + "id": "src/worker.ts::retryJob", + "path": "src/worker.ts", + "kind": "function", + "name": "retryJob", + "startLine": 19, + "endLine": 36, + "baseStartLine": 18, + "baseEndLine": 30, + "status": "modified", + "match": "exact", + "added": 6, + "deleted": 1, + "netAdded": 5, + "netDeleted": 0, + "touches": [ + { + "sha": "2036666a05e59375b6a19dff1e41f8a914133451", + "added": 2, + "deleted": 0 + }, + { + "sha": "519844cbebcde3860bd926666ecd36517e396092", + "added": 1, + "deleted": 0 + }, + { + "sha": "cd07328565c1c06f90eb5a440bbfaf1647c00c58", + "added": 3, + "deleted": 1 + } + ], + "firstTouched": "2026-01-06T10:00:00+00:00", + "lastTouched": "2026-01-06T18:00:00+00:00", + "metrics": { + "parameter_count": { + "before": 2, + "after": 2 + }, + "ts_type_interface_count": { + "before": 0, + "after": 0 + }, + "comment_noise_docblock": { + "before": 0, + "after": 0 + }, + "comment_noise": { + "before": 0, + "after": 0 + }, + "comment_noise_ratio": { + "before": 0, + "after": 0 + }, + "cognitive_complexity": { + "before": 9, + "after": 24 + }, + "max_nesting_depth": { + "before": 4, + "after": 4 + }, + "complexity": { + "before": 5, + "after": 11 + }, + "comment_noise_commented_code": { + "before": 0, + "after": 0 + }, + "todo_count": { + "before": 0, + "after": 0 + }, + "comment_lines": { + "before": 0, + "after": 0 + }, + "loc": { + "before": 13, + "after": 18 + } + }, + "measured": true, + "buckets": [ + "new-hotspot" + ], + "fileClass": "code", + "hunks": [ + { + "sha": "2036666a05e59375b6a19dff1e41f8a914133451", + "oldStart": 26, + "oldLines": 0, + "newStart": 27, + "newLines": 2 + }, + { + "sha": "519844cbebcde3860bd926666ecd36517e396092", + "oldStart": 18, + "oldLines": 0, + "newStart": 19, + "newLines": 1 + }, + { + "sha": "cd07328565c1c06f90eb5a440bbfaf1647c00c58", + "oldStart": 30, + "oldLines": 1, + "newStart": 30, + "newLines": 3 + } + ] + }, + { + "id": "src/worker.ts::legacyCleanup", + "path": "src/worker.ts", + "kind": "function", + "name": "legacyCleanup", + "startLine": 32, + "endLine": 37, + "baseStartLine": 32, + "baseEndLine": 37, + "status": "deleted", + "match": "none", + "added": 0, + "deleted": 7, + "netAdded": 0, + "netDeleted": 7, + "touches": [ + { + "sha": "2a74e4ce39f075a472e3c3c13b372d9beaa4b298", + "added": 0, + "deleted": 7 + } + ], + "firstTouched": "2026-01-06T14:00:00+00:00", + "lastTouched": "2026-01-06T14:00:00+00:00", + "metrics": { + "parameter_count": { + "before": 1, + "after": null + }, + "ts_type_interface_count": { + "before": 0, + "after": null + }, + "comment_noise_docblock": { + "before": 0, + "after": null + }, + "comment_noise": { + "before": 0, + "after": null + }, + "comment_noise_ratio": { + "before": 0, + "after": null + }, + "cognitive_complexity": { + "before": 3, + "after": null + }, + "max_nesting_depth": { + "before": 2, + "after": null + }, + "complexity": { + "before": 3, + "after": null + }, + "comment_noise_commented_code": { + "before": 0, + "after": null + }, + "todo_count": { + "before": 0, + "after": null + }, + "comment_lines": { + "before": 0, + "after": null + }, + "loc": { + "before": 6, + "after": null + } + }, + "measured": true, + "buckets": [], + "fileClass": "code", + "hunks": [ + { + "sha": "2a74e4ce39f075a472e3c3c13b372d9beaa4b298", + "oldStart": 35, + "oldLines": 7, + "newStart": 34, + "newLines": 0 + } + ] + }, + { + "id": "src/worker.ts::formatLog", + "path": "src/worker.ts", + "kind": "function", + "name": "formatLog", + "startLine": 38, + "endLine": 41, + "baseStartLine": 39, + "baseEndLine": 41, + "status": "modified", + "match": "exact", + "added": 2, + "deleted": 1, + "netAdded": 2, + "netDeleted": 1, + "touches": [ + { + "sha": "WORKTREE", + "added": 2, + "deleted": 1 + } + ], + "firstTouched": null, + "lastTouched": null, + "metrics": { + "parameter_count": { + "before": 2, + "after": 2 + }, + "ts_type_interface_count": { + "before": 0, + "after": 0 + }, + "comment_noise_docblock": { + "before": 0, + "after": 0 + }, + "comment_noise": { + "before": 0, + "after": 0 + }, + "comment_noise_ratio": { + "before": 0, + "after": 0 + }, + "cognitive_complexity": { + "before": 1, + "after": 2 + }, + "max_nesting_depth": { + "before": 0, + "after": 0 + }, + "complexity": { + "before": 2, + "after": 3 + }, + "comment_noise_commented_code": { + "before": 0, + "after": 0 + }, + "todo_count": { + "before": 0, + "after": 0 + }, + "comment_lines": { + "before": 0, + "after": 0 + }, + "loc": { + "before": 3, + "after": 4 + } + }, + "measured": true, + "buckets": [], + "fileClass": "code", + "hunks": [ + { + "sha": "WORKTREE", + "oldStart": 39, + "oldLines": 1, + "newStart": 39, + "newLines": 2 + } + ] + } + ], + "files": [ + { + "path": "README.md", + "status": "modified", + "added": 1, + "deleted": 1, + "loc": 6, + "fileClass": "docs", + "chunkIds": [ + "README.md::## Install" + ], + "outOfScope": false + }, + { + "path": "config/settings.yaml", + "status": "modified", + "added": 1, + "deleted": 1, + "loc": 5, + "fileClass": "config", + "chunkIds": [ + "config/settings.yaml::L3-3" + ], + "outOfScope": false + }, + { + "path": "src/planner.ts", + "status": "added", + "added": 29, + "deleted": 0, + "loc": 28, + "fileClass": "code", + "chunkIds": [ + "src/planner.ts", + "src/planner.ts::planRoute" + ], + "outOfScope": false + }, + { + "path": "src/router.ts", + "status": "modified", + "added": 1, + "deleted": 0, + "loc": 13, + "fileClass": "code", + "chunkIds": [ + "src/router.ts::dispatch" + ], + "outOfScope": false + }, + { + "path": "src/scheduler.ts", + "status": "modified", + "added": 7, + "deleted": 7, + "loc": 10, + "fileClass": "code", + "chunkIds": [ + "src/scheduler.ts::pickNext" + ], + "outOfScope": false + }, + { + "path": "src/util/text.ts", + "oldPath": "src/util/strings.ts", + "status": "renamed", + "added": 0, + "deleted": 0, + "loc": 10, + "fileClass": "code", + "chunkIds": [ + "src/util/text.ts" + ], + "outOfScope": false + }, + { + "path": "src/worker.ts", + "status": "modified", + "added": 10, + "deleted": 10, + "loc": 135, + "fileClass": "code", + "chunkIds": [ + "src/worker.ts::parseArgs", + "src/worker.ts::retryJob", + "src/worker.ts::legacyCleanup", + "src/worker.ts::formatLog" + ], + "outOfScope": false + } + ], + "summary": { + "filesEdited": 7, + "filesTotal": 7, + "chunks": 11, + "churnLines": 68, + "netLines": 52, + "reverted": 1 + }, + "metricIds": [ + "complexity", + "cognitive_complexity", + "loc", + "comment_noise", + "max_nesting_depth", + "parameter_count", + "todo_count", + "comment_noise_ratio", + "comment_noise_docblock", + "comment_noise_commented_code", + "comment_lines", + "ts_type_interface_count" + ] +} diff --git a/client/src/changelens/fixture.net.json b/client/src/changelens/fixture.net.json new file mode 100644 index 00000000..e5790b13 --- /dev/null +++ b/client/src/changelens/fixture.net.json @@ -0,0 +1,909 @@ +{ + "selection": { + "window": "branch", + "mode": "net", + "ref": "HEAD", + "base": { + "ref": "merge-base(main, feature)", + "sha": "97e5cf7df943e4232f3d2efcdd499dd7d0a70d63", + "label": "main @ 97e5cf7", + "date": "2026-01-05T09:00:00+00:00" + }, + "head": { + "ref": "WORKTREE", + "sha": null, + "label": "Working tree on feature @ 0c1943e", + "date": null + }, + "since": null, + "until": null, + "timezone": "UTC", + "includesWorktree": true, + "notes": [], + "repoRoot": "/repo/change-lens-fixture" + }, + "commits": [ + { + "sha": "2036666a05e59375b6a19dff1e41f8a914133451", + "short": "2036666", + "subject": "retry: back off between attempts", + "author": "Fixture Author", + "date": "2026-01-06T10:00:00+00:00", + "files": 1, + "added": 2, + "deleted": 0, + "chunkIds": [ + "src/worker.ts::retryJob" + ] + }, + { + "sha": "519844cbebcde3860bd926666ecd36517e396092", + "short": "519844c", + "subject": "retry: cap the attempt limit", + "author": "Fixture Author", + "date": "2026-01-06T11:00:00+00:00", + "files": 1, + "added": 1, + "deleted": 0, + "chunkIds": [ + "src/worker.ts::retryJob" + ] + }, + { + "sha": "8b98925caa5c902b877b0fa56a81ea9064306d46", + "short": "8b98925", + "subject": "scheduler: try a sorted pick", + "author": "Fixture Author", + "date": "2026-01-06T12:00:00+00:00", + "files": 1, + "added": 4, + "deleted": 3, + "chunkIds": [] + }, + { + "sha": "c19696660996461f9922f262e0c3a30245f143c9", + "short": "c196966", + "subject": "rename strings to text", + "author": "Fixture Author", + "date": "2026-01-06T13:00:00+00:00", + "files": 1, + "added": 0, + "deleted": 0, + "chunkIds": [ + "src/util/text.ts" + ] + }, + { + "sha": "2a74e4ce39f075a472e3c3c13b372d9beaa4b298", + "short": "2a74e4c", + "subject": "worker: remove legacyCleanup", + "author": "Fixture Author", + "date": "2026-01-06T14:00:00+00:00", + "files": 1, + "added": 0, + "deleted": 7, + "chunkIds": [ + "src/worker.ts::legacyCleanup" + ] + }, + { + "sha": "c9382753c54b7845c5b3aacef78a523c46ebcb4b", + "short": "c938275", + "subject": "docs: build step, more retries", + "author": "Fixture Author", + "date": "2026-01-06T15:00:00+00:00", + "files": 2, + "added": 2, + "deleted": 2, + "chunkIds": [ + "README.md::## Install" + ] + }, + { + "sha": "4dd738aba8d2e93cf29326ff849ca1e531182a4f", + "short": "4dd738a", + "subject": "scheduler: back to the first idle job", + "author": "Fixture Author", + "date": "2026-01-06T16:00:00+00:00", + "files": 1, + "added": 3, + "deleted": 4, + "chunkIds": [] + }, + { + "sha": "fa015dd3440f3cbacd27376749f5cd0187dd6b73", + "short": "fa015dd", + "subject": "args: accept --dry-run", + "author": "Fixture Author", + "date": "2026-01-06T17:00:00+00:00", + "files": 1, + "added": 2, + "deleted": 1, + "chunkIds": [ + "src/worker.ts::parseArgs" + ] + }, + { + "sha": "cd07328565c1c06f90eb5a440bbfaf1647c00c58", + "short": "cd07328", + "subject": "retry: add jitter", + "author": "Fixture Author", + "date": "2026-01-06T18:00:00+00:00", + "files": 1, + "added": 3, + "deleted": 1, + "chunkIds": [ + "src/worker.ts::retryJob" + ] + }, + { + "sha": "0c1943eb48825c52ff0ff143f5dbf8c95a029b60", + "short": "0c1943e", + "subject": "planner: route stops; router: audit jobs", + "author": "Fixture Author", + "date": "2026-01-06T19:00:00+00:00", + "files": 2, + "added": 30, + "deleted": 0, + "chunkIds": [ + "src/planner.ts", + "src/planner.ts::planRoute", + "src/router.ts::dispatch" + ] + }, + { + "sha": "WORKTREE", + "short": "worktree", + "subject": "Uncommitted changes", + "author": "", + "date": null, + "files": 1, + "added": 2, + "deleted": 1, + "chunkIds": [ + "src/worker.ts::formatLog" + ] + } + ], + "chunks": [ + { + "id": "README.md::## Install", + "path": "README.md", + "kind": "heading", + "name": "## Install", + "startLine": 5, + "endLine": 8, + "baseStartLine": 5, + "baseEndLine": 8, + "status": "modified", + "match": "exact", + "added": 1, + "deleted": 1, + "netAdded": 1, + "netDeleted": 1, + "touches": [], + "firstTouched": "2026-01-06T15:00:00+00:00", + "lastTouched": "2026-01-06T15:00:00+00:00", + "metrics": {}, + "measured": false, + "naReason": "Markdown sections have no code metrics", + "buckets": [], + "fileClass": "docs", + "hunks": [ + { + "sha": null, + "oldStart": 7, + "oldLines": 1, + "newStart": 7, + "newLines": 1 + } + ] + }, + { + "id": "config/settings.yaml::L3-3", + "path": "config/settings.yaml", + "kind": "range", + "name": "line 3", + "startLine": 3, + "endLine": 3, + "status": "modified", + "match": "exact", + "added": 1, + "deleted": 1, + "netAdded": 1, + "netDeleted": 1, + "touches": [], + "firstTouched": null, + "lastTouched": null, + "metrics": {}, + "measured": false, + "naReason": "No analyzer reads this file type", + "buckets": [], + "fileClass": "config", + "hunks": [ + { + "sha": null, + "oldStart": 3, + "oldLines": 1, + "newStart": 3, + "newLines": 1 + } + ] + }, + { + "id": "src/planner.ts", + "path": "src/planner.ts", + "kind": "file", + "name": "planner.ts", + "startLine": 1, + "endLine": 29, + "status": "added", + "match": "none", + "added": 6, + "deleted": 0, + "netAdded": 6, + "netDeleted": 0, + "touches": [], + "firstTouched": "2026-01-06T19:00:00+00:00", + "lastTouched": "2026-01-06T19:00:00+00:00", + "metrics": {}, + "measured": false, + "naReason": "Lines outside every named scope", + "buckets": [], + "fileClass": "code", + "hunks": [ + { + "sha": null, + "oldStart": 0, + "oldLines": 0, + "newStart": 1, + "newLines": 29 + } + ] + }, + { + "id": "src/planner.ts::planRoute", + "path": "src/planner.ts", + "kind": "function", + "name": "planRoute", + "startLine": 8, + "endLine": 29, + "status": "added", + "match": "none", + "added": 23, + "deleted": 0, + "netAdded": 23, + "netDeleted": 0, + "touches": [], + "firstTouched": "2026-01-06T19:00:00+00:00", + "lastTouched": "2026-01-06T19:00:00+00:00", + "metrics": { + "parameter_count": { + "before": null, + "after": 2 + }, + "ts_type_interface_count": { + "before": null, + "after": 0 + }, + "comment_noise_docblock": { + "before": null, + "after": 0 + }, + "comment_noise": { + "before": null, + "after": 0 + }, + "comment_noise_ratio": { + "before": null, + "after": 0 + }, + "cognitive_complexity": { + "before": null, + "after": 17 + }, + "max_nesting_depth": { + "before": null, + "after": 3 + }, + "complexity": { + "before": null, + "after": 12 + }, + "comment_noise_commented_code": { + "before": null, + "after": 0 + }, + "todo_count": { + "before": null, + "after": 0 + }, + "comment_lines": { + "before": null, + "after": 0 + }, + "loc": { + "before": null, + "after": 22 + } + }, + "measured": true, + "buckets": [ + "new-hotspot" + ], + "fileClass": "code", + "hunks": [ + { + "sha": null, + "oldStart": 0, + "oldLines": 0, + "newStart": 1, + "newLines": 29 + } + ] + }, + { + "id": "src/router.ts::dispatch", + "path": "src/router.ts", + "kind": "function", + "name": "dispatch", + "startLine": 1, + "endLine": 13, + "baseStartLine": 1, + "baseEndLine": 12, + "status": "modified", + "match": "exact", + "added": 1, + "deleted": 0, + "netAdded": 1, + "netDeleted": 0, + "touches": [], + "firstTouched": "2026-01-06T19:00:00+00:00", + "lastTouched": "2026-01-06T19:00:00+00:00", + "metrics": { + "parameter_count": { + "before": 2, + "after": 2 + }, + "ts_type_interface_count": { + "before": 0, + "after": 0 + }, + "comment_noise_docblock": { + "before": 0, + "after": 0 + }, + "comment_noise": { + "before": 0, + "after": 0 + }, + "comment_noise_ratio": { + "before": 0, + "after": 0 + }, + "cognitive_complexity": { + "before": 13, + "after": 16 + }, + "max_nesting_depth": { + "before": 2, + "after": 2 + }, + "complexity": { + "before": 11, + "after": 13 + }, + "comment_noise_commented_code": { + "before": 0, + "after": 0 + }, + "todo_count": { + "before": 0, + "after": 0 + }, + "comment_lines": { + "before": 0, + "after": 0 + }, + "loc": { + "before": 12, + "after": 13 + } + }, + "measured": true, + "buckets": [ + "complex-and-changed", + "new-hotspot" + ], + "fileClass": "code", + "hunks": [ + { + "sha": null, + "oldStart": 10, + "oldLines": 0, + "newStart": 11, + "newLines": 1 + } + ] + }, + { + "id": "src/util/text.ts", + "path": "src/util/text.ts", + "oldPath": "src/util/strings.ts", + "kind": "file", + "name": "text.ts", + "startLine": 1, + "endLine": 11, + "baseStartLine": 1, + "baseEndLine": 11, + "status": "moved", + "match": "renamed-file", + "added": 0, + "deleted": 0, + "netAdded": 0, + "netDeleted": 0, + "touches": [], + "firstTouched": "2026-01-06T13:00:00+00:00", + "lastTouched": "2026-01-06T13:00:00+00:00", + "metrics": { + "loc": { + "before": 10, + "after": 10 + }, + "complexity": { + "before": 2, + "after": 2 + }, + "cognitive_complexity": { + "before": 1, + "after": 1 + } + }, + "measured": true, + "buckets": [], + "fileClass": "code", + "hunks": [] + }, + { + "id": "src/worker.ts::parseArgs", + "path": "src/worker.ts", + "kind": "function", + "name": "parseArgs", + "startLine": 9, + "endLine": 17, + "baseStartLine": 9, + "baseEndLine": 16, + "status": "modified", + "match": "exact", + "added": 2, + "deleted": 1, + "netAdded": 2, + "netDeleted": 1, + "touches": [], + "firstTouched": "2026-01-06T17:00:00+00:00", + "lastTouched": "2026-01-06T17:00:00+00:00", + "metrics": { + "parameter_count": { + "before": 1, + "after": 1 + }, + "ts_type_interface_count": { + "before": 0, + "after": 0 + }, + "comment_noise_docblock": { + "before": 0, + "after": 0 + }, + "comment_noise": { + "before": 0, + "after": 0 + }, + "comment_noise_ratio": { + "before": 0, + "after": 0 + }, + "cognitive_complexity": { + "before": 4, + "after": 5 + }, + "max_nesting_depth": { + "before": 2, + "after": 2 + }, + "complexity": { + "before": 4, + "after": 5 + }, + "comment_noise_commented_code": { + "before": 0, + "after": 0 + }, + "todo_count": { + "before": 0, + "after": 0 + }, + "comment_lines": { + "before": 0, + "after": 0 + }, + "loc": { + "before": 8, + "after": 9 + } + }, + "measured": true, + "buckets": [], + "fileClass": "code", + "hunks": [ + { + "sha": null, + "oldStart": 10, + "oldLines": 1, + "newStart": 10, + "newLines": 1 + }, + { + "sha": null, + "oldStart": 12, + "oldLines": 0, + "newStart": 13, + "newLines": 1 + } + ] + }, + { + "id": "src/worker.ts::retryJob", + "path": "src/worker.ts", + "kind": "function", + "name": "retryJob", + "startLine": 19, + "endLine": 36, + "baseStartLine": 18, + "baseEndLine": 30, + "status": "modified", + "match": "exact", + "added": 5, + "deleted": 0, + "netAdded": 5, + "netDeleted": 0, + "touches": [], + "firstTouched": "2026-01-06T10:00:00+00:00", + "lastTouched": "2026-01-06T18:00:00+00:00", + "metrics": { + "parameter_count": { + "before": 2, + "after": 2 + }, + "ts_type_interface_count": { + "before": 0, + "after": 0 + }, + "comment_noise_docblock": { + "before": 0, + "after": 0 + }, + "comment_noise": { + "before": 0, + "after": 0 + }, + "comment_noise_ratio": { + "before": 0, + "after": 0 + }, + "cognitive_complexity": { + "before": 9, + "after": 24 + }, + "max_nesting_depth": { + "before": 4, + "after": 4 + }, + "complexity": { + "before": 5, + "after": 11 + }, + "comment_noise_commented_code": { + "before": 0, + "after": 0 + }, + "todo_count": { + "before": 0, + "after": 0 + }, + "comment_lines": { + "before": 0, + "after": 0 + }, + "loc": { + "before": 13, + "after": 18 + } + }, + "measured": true, + "buckets": [ + "new-hotspot" + ], + "fileClass": "code", + "hunks": [ + { + "sha": null, + "oldStart": 18, + "oldLines": 0, + "newStart": 20, + "newLines": 1 + }, + { + "sha": null, + "oldStart": 26, + "oldLines": 0, + "newStart": 29, + "newLines": 4 + } + ] + }, + { + "id": "src/worker.ts::legacyCleanup", + "path": "src/worker.ts", + "kind": "function", + "name": "legacyCleanup", + "startLine": 32, + "endLine": 37, + "baseStartLine": 32, + "baseEndLine": 37, + "status": "deleted", + "match": "none", + "added": 0, + "deleted": 7, + "netAdded": 0, + "netDeleted": 7, + "touches": [], + "firstTouched": "2026-01-06T14:00:00+00:00", + "lastTouched": "2026-01-06T14:00:00+00:00", + "metrics": { + "parameter_count": { + "before": 1, + "after": null + }, + "ts_type_interface_count": { + "before": 0, + "after": null + }, + "comment_noise_docblock": { + "before": 0, + "after": null + }, + "comment_noise": { + "before": 0, + "after": null + }, + "comment_noise_ratio": { + "before": 0, + "after": null + }, + "cognitive_complexity": { + "before": 3, + "after": null + }, + "max_nesting_depth": { + "before": 2, + "after": null + }, + "complexity": { + "before": 3, + "after": null + }, + "comment_noise_commented_code": { + "before": 0, + "after": null + }, + "todo_count": { + "before": 0, + "after": null + }, + "comment_lines": { + "before": 0, + "after": null + }, + "loc": { + "before": 6, + "after": null + } + }, + "measured": true, + "buckets": [], + "fileClass": "code", + "hunks": [ + { + "sha": null, + "oldStart": 32, + "oldLines": 7, + "newStart": 37, + "newLines": 0 + } + ] + }, + { + "id": "src/worker.ts::formatLog", + "path": "src/worker.ts", + "kind": "function", + "name": "formatLog", + "startLine": 38, + "endLine": 41, + "baseStartLine": 39, + "baseEndLine": 41, + "status": "modified", + "match": "exact", + "added": 2, + "deleted": 1, + "netAdded": 2, + "netDeleted": 1, + "touches": [], + "firstTouched": null, + "lastTouched": null, + "metrics": { + "parameter_count": { + "before": 2, + "after": 2 + }, + "ts_type_interface_count": { + "before": 0, + "after": 0 + }, + "comment_noise_docblock": { + "before": 0, + "after": 0 + }, + "comment_noise": { + "before": 0, + "after": 0 + }, + "comment_noise_ratio": { + "before": 0, + "after": 0 + }, + "cognitive_complexity": { + "before": 1, + "after": 2 + }, + "max_nesting_depth": { + "before": 0, + "after": 0 + }, + "complexity": { + "before": 2, + "after": 3 + }, + "comment_noise_commented_code": { + "before": 0, + "after": 0 + }, + "todo_count": { + "before": 0, + "after": 0 + }, + "comment_lines": { + "before": 0, + "after": 0 + }, + "loc": { + "before": 3, + "after": 4 + } + }, + "measured": true, + "buckets": [], + "fileClass": "code", + "hunks": [ + { + "sha": null, + "oldStart": 40, + "oldLines": 1, + "newStart": 39, + "newLines": 2 + } + ] + } + ], + "files": [ + { + "path": "README.md", + "status": "modified", + "added": 1, + "deleted": 1, + "loc": 6, + "fileClass": "docs", + "chunkIds": [ + "README.md::## Install" + ], + "outOfScope": false + }, + { + "path": "config/settings.yaml", + "status": "modified", + "added": 1, + "deleted": 1, + "loc": 5, + "fileClass": "config", + "chunkIds": [ + "config/settings.yaml::L3-3" + ], + "outOfScope": false + }, + { + "path": "src/planner.ts", + "status": "added", + "added": 29, + "deleted": 0, + "loc": 28, + "fileClass": "code", + "chunkIds": [ + "src/planner.ts", + "src/planner.ts::planRoute" + ], + "outOfScope": false + }, + { + "path": "src/router.ts", + "status": "modified", + "added": 1, + "deleted": 0, + "loc": 13, + "fileClass": "code", + "chunkIds": [ + "src/router.ts::dispatch" + ], + "outOfScope": false + }, + { + "path": "src/util/text.ts", + "oldPath": "src/util/strings.ts", + "status": "renamed", + "added": 0, + "deleted": 0, + "loc": 10, + "fileClass": "code", + "chunkIds": [ + "src/util/text.ts" + ], + "outOfScope": false + }, + { + "path": "src/worker.ts", + "status": "modified", + "added": 9, + "deleted": 9, + "loc": 135, + "fileClass": "code", + "chunkIds": [ + "src/worker.ts::parseArgs", + "src/worker.ts::retryJob", + "src/worker.ts::legacyCleanup", + "src/worker.ts::formatLog" + ], + "outOfScope": false + } + ], + "summary": { + "filesEdited": 6, + "filesTotal": 7, + "chunks": 10, + "churnLines": 52, + "netLines": 52, + "reverted": 0 + }, + "metricIds": [ + "complexity", + "cognitive_complexity", + "loc", + "comment_noise", + "max_nesting_depth", + "parameter_count", + "todo_count", + "comment_noise_ratio", + "comment_noise_docblock", + "comment_noise_commented_code", + "comment_lines", + "ts_type_interface_count" + ] +} diff --git a/server/app/services/changelens/service.py b/server/app/services/changelens/service.py index 190df9a4..02a2e812 100644 --- a/server/app/services/changelens/service.py +++ b/server/app/services/changelens/service.py @@ -10,7 +10,7 @@ from app.services.changelens import chunks as engine from app.services.changelens import textdiff from app.services.changelens.blobs import BlobRef, Scope -from app.services.changelens.chunks import Built, Draft, Lineage +from app.services.changelens.chunks import Built, Lineage from app.services.changelens.hunks import GIT_TIMEOUT, Hunk, is_skipped from app.services.changelens.models import ( WORKTREE, diff --git a/server/tests/snapshots/ranking-parity.json b/server/tests/snapshots/ranking-parity.json index c5ad99b9..7505a129 100644 --- a/server/tests/snapshots/ranking-parity.json +++ b/server/tests/snapshots/ranking-parity.json @@ -13,6 +13,10 @@ "comment_noise/commented_out.ts", 1 ], + [ + "change_lens_repo.py", + 0 + ], [ "components/react/OrderDashboard.tsx", 0 @@ -64,23 +68,27 @@ 1 ], [ - "comment_noise/commented_out.ts::loadUser::then(\u0192)", + "change_lens_repo.py::_git", 0 ], [ - "comment_noise/commented_out.ts::saveUser", + "change_lens_repo.py::_write", 0 ], [ - "comment_noise/narrated.ts::tidy", + "change_lens_repo.py::build", 0 ], [ - "comment_noise/narrated.ts::tidy::map(\u0192)", + "change_lens_repo.py::worker", 0 ] ], "complexity/file": [ + [ + "change_lens_repo.py", + 5 + ], [ "components/react/OrderDashboard.tsx", 5 @@ -119,6 +127,10 @@ ] ], "complexity/function": [ + [ + "change_lens_repo.py::build", + 5 + ], [ "components/react/OrderDashboard.tsx::OrderDashboard", 5 @@ -132,32 +144,28 @@ 4 ], [ - "comment_noise/narrated.py::explained", + "change_lens_repo.py::_git", 3 ], [ - "components/react/OrderDashboard.tsx::OrderDashboard::
::
::::map(\u0192)", + "change_lens_repo.py::_write", 3 ], [ - "metric_trust/src/Panel.tsx::Panel", + "comment_noise/narrated.py::explained", 3 ], [ - "metric_trust/src/Panel.tsx::Panel::label", + "components/react/OrderDashboard.tsx::OrderDashboard::
::
::::map(\u0192)", 3 ], [ - "metric_trust/src/jobs.py::schedule", + "metric_trust/src/Panel.tsx::Panel", 3 ], [ - "comment_noise/narrated.py::total", - 2 - ], - [ - "components/solid/Counter.tsx::Counter::
::