diffstalkerd 0.13.13 → 0.14.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -5,6 +5,79 @@ All notable changes to this project will be documented in this file.
5
5
  The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),
6
6
  and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
7
7
 
8
+ ## [0.14.0] - 2026-11-23
9
+
10
+ ### Added
11
+
12
+ - **A Work page for all your unfinished work.** Click the diffstalker
13
+ wordmark. It lists your open PRs, the PRs you are asked to review, and
14
+ the worktrees and branches with work in them, across every watched repo,
15
+ found without bookkeeping. Each row is one line of columns: how far along
16
+ it is (with a colour), its layer in a PR stack, the PR link, review
17
+ state, CI, merge conflicts, local changes, the draft, whose move it is,
18
+ and how long ago something last happened. Group the rows by repository
19
+ and by time, keep only the last day, week or month, and sort by any
20
+ column. A red alert marks what waits on you: changes requested and not
21
+ asked to review again yet, or a review you are asked for. **Track**
22
+ keeps a candidate in view; **Dismiss** hides an item until something
23
+ about it changes. The page catches up when you come back to it and says
24
+ how old each part is. PR state comes from `gh`; without it the page
25
+ shows what git knows.
26
+ - **PR body drafts.** Write the top zone of a PR description per branch,
27
+ in a Work row's fold-out or in Compare. Drafts live in the repo's git
28
+ dir (`diffstalker/pr/<branch>/`), where agents read them and leave a
29
+ suggestion or the AI zone in files of their own. For an open PR the
30
+ editor starts from its text on GitHub. When your words differ from the
31
+ PR, **Take GitHub's text** copies them over, and **Update GitHub**
32
+ replaces only the PR's top zone after asking once more. That button is
33
+ the only change diffstalker makes on GitHub.
34
+ - **Copy for a session** puts a PR's link, branch, head commit and local
35
+ clone on the clipboard, to paste into an agent session.
36
+ - The daemon serves the Work page and the drafts: `/work/*`,
37
+ `/repos/:id/draft`, `/repos/:id/pr`, and `gh` in `GET /health`. See the
38
+ daemon README.
39
+
40
+ ### Changed
41
+
42
+ - **Repo ages in the picker follow commits and checkouts,** not git's
43
+ index file, which a plain `git status` rewrites.
44
+ - **Compare takes any branch as `?head=`.** A branch that is no layer of
45
+ the stack is compared against the trunk.
46
+
47
+ ### Fixed
48
+
49
+ - **A huge change no longer mounts every diff at once.** A repo with
50
+ 15,000 changed files made one Compare tab use 10 to 14 GiB. Files now
51
+ mount while their rows fit in 20,000; the rest wait behind the **Load
52
+ diff** gate, and `e` still loads everything. A compare that refreshes
53
+ keeps every file whose diff did not change.
54
+
55
+ ## [0.13.14] - 2026-11-11
56
+
57
+ ### Fixed
58
+
59
+ - **The daemon no longer works for repos nobody is looking at.** A repo
60
+ kept open by follow mode, or kept for ten minutes after its last tab
61
+ closed, refreshed on every file change with no one to see it. With a
62
+ large uncommitted diff and a process writing into the repo, that cost
63
+ about a third of a CPU core around the clock. Such a repo now only notes
64
+ that it changed, and catches up when a client opens it or asks for it.
65
+ - **Watching a repo costs one watch per folder, not per file.** The
66
+ watcher put an inotify watch on every file and re-read a whole folder,
67
+ every entry, on each change in it: a folder of 7,500 files written to
68
+ every few seconds cost over a second of CPU per write. Folders git
69
+ ignores are no longer watched at all, now including `build/`-style
70
+ rules, the global ignore file and a worktree's `info/exclude`.
71
+ - **Git's own writes are watched, not polled.** The index, HEAD and refs
72
+ of every open repo were checked ten times a second. A background status
73
+ also no longer rewrites the index, so it cannot make a git command in
74
+ your terminal fail on `index.lock`.
75
+ - **Refreshes are spaced out under constant writes.** A repo that keeps
76
+ changing refreshes at most once a second, and spends at most a fifth of
77
+ its time refreshing however large its diff.
78
+ - **A large diff is read once per refresh, not twice,** when nothing is
79
+ staged.
80
+
8
81
  ## [0.13.13] - 2026-11-02
9
82
 
10
83
  ### Fixed
package/README.md CHANGED
@@ -83,7 +83,8 @@ daemon itself. `GET /` and any unmatched non-API GET path return the SPA's
83
83
  immutable cache. The REST/SSE API always takes precedence over static files,
84
84
  and an unknown path under an API prefix (`/health`, `/version`, `/repos`,
85
85
  `/resolve`, `/worktrees`, `/events`, `/follow`, `/settings`, `/discovered`,
86
- `/browse`) is a JSON 404, never the SPA page.
86
+ `/browse`, `/work`) is a JSON 404, never the SPA page. `/work` alone is the
87
+ exception: it is the web UI's Work page, while `/work/...` is the API.
87
88
 
88
89
  Without `--socket`, the daemon binds a unix socket at
89
90
  `$XDG_RUNTIME_DIR/diffstalker/diffstalkerd.sock`. The directory is created
@@ -173,6 +174,29 @@ An always-warm service answers the probe immediately. The daemon also has no
173
174
  idle timeout — it holds watchers on open repos and is meant to stay warm for
174
175
  the session — so lazy activation buys little here.
175
176
 
177
+ ### Background work
178
+
179
+ Staying warm must not mean staying busy. An open repo costs one inotify
180
+ watch per directory git does not ignore (`.gitignore` files, the repo's
181
+ `info/exclude` and the user's `core.excludesFile` all count), and none per
182
+ file. Git's own writes (index, HEAD, refs, packed-refs) are seen through
183
+ watches on the git dirs, not by polling, and the daemon's git runs with
184
+ `GIT_OPTIONAL_LOCKS=0`, so a refresh never rewrites the index behind the
185
+ user's back.
186
+
187
+ A file change refreshes a repo only while a client **views** it: while it
188
+ has at least one SSE subscriber (`GET /repos/:id/events` or
189
+ `GET /events?repo=`). A repo nobody views — one kept open by follow mode
190
+ with no client attached, or one lingering after its release — only marks
191
+ itself stale. Its first new subscriber, and any read that serves its state
192
+ (`GET /status`, the whole-tree `GET /diff`, `GET /journal`), refreshes it
193
+ first; `GET /repos` reads its branch from the HEAD file instead. Change-driven refreshes of a viewed repo are spaced out: a burst
194
+ settles for 100 ms, and the next one starts no sooner than 1 s, or four
195
+ times the last refresh's duration, after the last one ended. A repo with a
196
+ huge diff and a process writing into it all day therefore spends at most a
197
+ fifth of its time refreshing. Explicit actions (stage, commit, fetch, …)
198
+ refresh at once.
199
+
176
200
  ## API
177
201
 
178
202
  Current surface. All bodies are JSON; errors are non-2xx with `{"error": "..."}`.
@@ -194,18 +218,18 @@ when the repo is open but its directory is gone or is no longer a git repo
194
218
  for real failures.
195
219
 
196
220
  Ref-like values (the body fields `name`, `hash`, `branch` and the `base`
197
- query param) must not be empty or start with `-`; such values are rejected
221
+ and `branch` query params) must not be empty or start with `-`; such values are rejected
198
222
  with a 400 so they can never be parsed as git flags.
199
223
 
200
224
  ### Reads
201
225
 
202
226
  | Method | Path | What |
203
227
  | ------ | -------------------------- | ------------------------------------------------ |
204
- | GET | `/health` | `{ok, ready, home, http: {port}, symbols}` — `http.port` is the loopback port a browser reaches the web UI on, or null when only a socket is bound (what `diffstalker link` needs to build a URL at all) |
228
+ | GET | `/health` | `{ok, ready, home, http: {port}, symbols, gh}` — `http.port` is the loopback port a browser reaches the web UI on, or null when only a socket is bound (what `diffstalker link` needs to build a URL at all). `gh` says whether the `gh` binary is on PATH (a PATH lookup, no spawn); see [Work page and PR drafts](#work-page-and-pr-drafts) |
205
229
  | GET | `/version` | `{current, latest, status, install, restart}` — the running version against npm's `latest` dist-tag (`current` \| `outdated` \| `ahead` \| `unknown`). The npm lookup is cached (6h, 5min after a failure) and only ever runs when this endpoint is called; `--no-update-check` skips it, leaving `latest: null`. `install` is `{method, package, command}` — how this daemon was installed (`npm` \| `bun` \| `pnpm` \| `yarn` \| `pacman` \| `unknown`) and the one command that updates it, detected once from where the daemon's own files live. An install nothing owns reports `unknown` with a null command rather than a guess. `restart` is the command that restarts this daemon (`systemctl --user restart diffstalkerd`, or `sudo systemctl restart …` for a system unit), set only when systemd started this very process (`$SYSTEMD_EXEC_PID` is its pid) and `/proc/self/cgroup` names a plain `.service` unit; otherwise null |
206
230
  | GET | `/repos` | List open repos (`id`, `path`, `branch`) |
207
231
  | POST | `/repos` | Open a repo: `{"path": "/abs/path"}` → `{id, path}` (201 created, 200 already open or still kept warm after its release). 400 for a relative path or a directory that is not a git repository, 404 when the path does not exist. One missing last segment under a repo still opens the repo above it (`<repo>/nope` opens `<repo>`); a path missing more than one level deep (`<repo>/nope/deeper`) is a 404 |
208
- | DELETE | `/repos/:id` | Close a repo (refcounted per open). At zero it is closed for every client at once (`repo-closed`, gone from `GET /repos`), but the daemon keeps its git state live for 10 minutes (up to 4 such repos) so a reopen in that window is warm |
232
+ | DELETE | `/repos/:id` | Close a repo (refcounted per open). At zero it is closed for every client at once (`repo-closed`, gone from `GET /repos`), but the daemon keeps its watchers and last state for 10 minutes (up to 4 such repos) so a reopen in that window is warm; it refreshes only once a client views it again |
209
233
  | GET | `/repos/:id/worktrees` | Registered worktrees (`path`, `branch`, `head`, `isBare`, `lastActivity`, `aheadOfBase`): main worktree, linked worktrees, and the bare entry in a bare-worktree layout |
210
234
  | GET | `/worktrees?path=` | Same as above, but for a raw filesystem path instead of an already-opened repo id (e.g. a recently-visited repo a client hasn't opened on this daemon) |
211
235
  | GET | `/resolve?path=` | `{openable, root}` — can this exact path be opened, and as which worktree, without opening it. Same resolver as `POST /repos`, but the path must EXIST (git resolves a missing path to its parent worktree, which would make a typo look openable). A relative path is a 400; not-a-repo is a 200 answer |
@@ -230,12 +254,17 @@ with a 400 so they can never be parsed as git flags.
230
254
  | GET | `/repos/:id/blob?path=&side=&v=` | Raw image bytes for `<img src>`; `side` is `worktree` \| `index` \| `head`, `v` is an ignored cache key. Only PNG/JPEG/GIF, typed from magic bytes. Status-independent (no `git status`), so a clean committed image is reachable. See [Image bytes](#image-bytes) |
231
255
  | GET | `/repos/:id/files` | All tracked + untracked (not ignored) paths: the fuzzy-finder source |
232
256
  | GET | `/repos/:id/journal?since=` | Append-only edit journal: `{epoch, prunedBefore, entries}`; `since=<seq>` returns only entries with a higher seq (all when omitted) |
233
- | GET | `/repos/:id/events` | SSE: `snapshot` on connect, then `state-change` events from the file watcher and `journal-append {entries}` per journal observation |
234
- | GET | `/events?repo=` | Daemon-scope SSE: `snapshot` (open repos) on connect, then `repo-opened` / `repo-closed` / `follow-change` / `settings-change` / `discovery-change`. With `repo=<id>` the same stream also carries that repo's events — `repo-snapshot` right after the daemon `snapshot`, then `state-change` / `journal-append` as on `/repos/:id/events`. An id that is not open is still a 200: one `repo-missing {id}` follows the snapshot; an empty `repo=` is the same as none. See [One stream per tab](#one-stream-per-tab) |
257
+ | GET | `/repos/:id/draft?branch=` | The branch's PR draft: `{branch, top, suggested, ai, adoptedUnedited, version}` (`PrDraft` in `@diffstalker/core/types/work`), each text null when its file is missing. `version` is the sha256 of `top.md`, or `absent`. 400 for a missing or refused branch name, 422 when a draft file is a link, not a regular file, or over 256 KB |
258
+ | GET | `/repos/:id/pr?branch=` | The cached PR for one branch: `{pr, status, message, checkedAt, viewer}`, matched by the repo's GitHub remotes and the branch name. Answers from the cache and never runs `gh`; `pr` is null before the first refresh |
259
+ | GET | `/repos/:id/events` | SSE: `snapshot` on connect, then `state-change` events from the file watcher, `journal-append {entries}` per journal observation, and `draft-change {branch, prefix}` when a PR draft file changes (no git refresh comes with it) |
260
+ | GET | `/events?repo=` | Daemon-scope SSE: `snapshot` (open repos) on connect, then `repo-opened` / `repo-closed` / `follow-change` / `settings-change` / `discovery-change`. With `repo=<id>` the same stream also carries that repo's events — `repo-snapshot` right after the daemon `snapshot`, then `state-change` / `journal-append` / `draft-change` as on `/repos/:id/events`. An id that is not open is still a 200: one `repo-missing {id}` follows the snapshot; an empty `repo=` is the same as none. See [One stream per tab](#one-stream-per-tab) |
235
261
  | GET | `/follow` | Follow state: `{targetFile, enabled, followedRepoId, followedPath}` |
236
262
  | GET | `/settings` | Daemon settings: `{watchRoots, persisted}`. `persisted: false` means this daemon holds them in memory only (no settings file) |
237
263
  | GET | `/discovered` | Repos found under the watch directories: `{roots: [{path, repos: [{path, name, branch, lastActivity}], error, capped}]}`. See [Watch directories](#watch-directories) |
238
264
  | GET | `/browse?path=` | One directory level of the daemon's filesystem, for picking a watch directory: `{path, parent, home, entries: [{name, path, isRepo}]}`. Directory names only — no files, no dot directories. No `path` starts at the daemon's home; 400 for a relative path, 404 for one it cannot read |
265
+ | POST | `/work/repo` | `{"path": "/abs/path"}` → one repo's facts for the Work page: `{path, root, name, worktrees, branches, branchesCapped, identities, drafts}` (`WorkRepoFacts`). A read, but POST: it runs a full git scan, and only a non-GET request gets the CSRF check, so a GET would let any website start scans and time them. The path must be absolute (400) and be a discovered or open repo (404 otherwise); 404 when it is not a git repository, 500 when the scan fails. One scan per path runs at a time; a request while it runs joins it |
266
+ | GET | `/work/prs` | The cached PR list: `{status, message, checkedAt, viewer, prs, capped}` (`PrSnapshot`). Never runs `gh`; `status` is `never` until the first refresh |
267
+ | GET | `/work/marks` | The Work page's marks: `{tracked, dismissed}` |
239
268
 
240
269
  ### Mutations (all respond `{state, result?}`)
241
270
 
@@ -243,6 +272,12 @@ with a 400 so they can never be parsed as git flags.
243
272
  | ------ | -------------------------- | ------------------------------------------------ |
244
273
  | PUT | `/settings` | Replace the settings: `{"watchRoots": ["/abs/dir", "~/projects"]}` → `{watchRoots, persisted}`. Whole document, not a patch. Each path is expanded, made absolute and checked to be an existing directory; a refusal is a 400 naming the path, and nothing is saved |
245
274
  | POST | `/discovered/rescan` | Re-walk every watch directory now and return the new `{roots}` (filesystem only, no git processes) |
275
+ | POST | `/work/prs/refresh` | Run the `gh` query now and return the new `PrSnapshot`. The body is ignored. A refresh while one runs joins it. A `gh` failure is a 200 with its `status`; the last good list and its `checkedAt` stay |
276
+ | PUT | `/work/marks` | `{"key": "...", "action": "track" \| "dismiss" \| "drop" \| "undismiss", "fingerprint"?: "...", "prune"?: ["..."]}` → `{tracked, dismissed}`. `dismiss` needs a `fingerprint`; `prune` keys are dropped from both kinds first. 400 for a bad request or more than 2000 marks of a kind, 500 when the file cannot be written |
277
+ | PUT | `/repos/:id/draft` | `{"branch": "...", "top": "...", "version": "..."}` → `PrDraft`. Saves `top.md` when `version` still matches (409 `Draft changed elsewhere` otherwise). An empty `top` deletes `top.md` and `adopted`, and the branch's directory when nothing else is in it. 413 over 256 KB; 422 when any draft file cannot be read, and then nothing is written |
278
+ | POST | `/repos/:id/draft/adopt` | `{"branch": "...", "version": "..."}` → `PrDraft`. "Start from this": copies `top.suggested.md` into `top.md` and writes its sha256 to `adopted`. 409 when the version moved, `top.md` is not empty, or there is no suggestion; 422 when any draft file cannot be read, and then nothing is written |
279
+ | POST | `/repos/:id/pr/check` | `{"branch": "..."}` → `{pr, status, message, checkedAt, viewer}`. Reads that branch's PR fresh from GitHub (one `gh` call) and puts it in the cached list. 404 when the list has no PR for the branch; 502/503 when `gh` fails |
280
+ | POST | `/repos/:id/pr/top` | `{"branch": "...", "version": "...", "expectedTop": "..."}` → same shape. "Update GitHub": replaces the open PR's top zone with `top.md` and keeps the body from the `---` line on, as GitHub has it. Refuses, writing nothing: 409 when `top.md`'s version moved, the PR is not open, or GitHub's top zone is not `expectedTop`; 422 when `top.md` is empty or has a `{{...}}`; 404 when no PR is known; 502/503 when `gh` fails |
246
281
  | POST | `/repos/:id/stage` | `{"path": "file.txt"}` — stage one file (404 when not in status) |
247
282
  | POST | `/repos/:id/unstage` | `{"path": "file.txt"}` — unstage one file |
248
283
  | POST | `/repos/:id/stage-all` | Stage everything |
@@ -270,9 +305,11 @@ returns the repo to its pre-operation state — no shell required. The
270
305
  failing mutation refreshes the shared state before it answers, so the
271
306
  `state-change` event carries the wedge too, and `/status` serves that same
272
307
  cached state rather than re-reading the git dir. For an operation started
273
- from a shell the value lags by the watcher's debounce plus one refresh
274
- (roughly 150–250 ms after HEAD or the index changes): a `/status` read in
275
- that window still shows the previous value. A failed git operation's
308
+ from a shell the value of a viewed repo lags by the watcher's settle time
309
+ plus one refresh (roughly 150–250 ms after HEAD or the index changes, up to
310
+ about a second when another refresh just ended): a `/status` read in that
311
+ window still shows the previous value. An unviewed repo refreshes for the
312
+ read itself. A failed git operation's
276
313
  `{error}` is git's own message (a rejected push, a patch that does not
277
314
  apply); the daemon log records the same text.
278
315
 
@@ -318,7 +355,7 @@ hung. `GET /events?repo=<id>` folds both into ONE stream:
318
355
 
319
356
  - On connect: the daemon `snapshot` first, then the repo's `repo-snapshot`
320
357
  (renamed only because `snapshot` is already taken on this stream). After
321
- that, `state-change` and `journal-append` exactly as on
358
+ that, `state-change`, `journal-append` and `draft-change` exactly as on
322
359
  `/repos/:id/events`, with the same payloads and the same dedup.
323
360
  - An id that is not open (the daemon restarted and the browser's
324
361
  EventSource reconnected before the page re-POSTed `/repos`) is still a
@@ -361,7 +398,9 @@ clients apply them atomically.
361
398
  The journal store lives above the repo's manager lifecycle: closing a
362
399
  repo's last client (a browser F5) keeps the store, so a reopen resumes the
363
400
  same chronology under the same epoch, even after the lingering manager is
364
- disposed. While the manager lingers it keeps journaling edits as usual.
401
+ disposed. A repo journals while a client views it. Unviewed (lingering, or
402
+ followed with no client attached) it observes nothing, and the next refresh
403
+ records everything that changed meanwhile in one observation.
365
404
  Stores are LRU-capped daemon-wide; the store of an open or lingering repo
366
405
  is never evicted.
367
406
 
@@ -513,6 +552,61 @@ in the settings and is reported with an `error` — it is never quietly
513
552
  dropped. Both `/settings` and `/discovered` are served on every transport,
514
553
  including a `--port` listener: the settings UI lives in the web client.
515
554
 
555
+ ## Work page and PR drafts
556
+
557
+ See `docs/work-page.md` for the design.
558
+
559
+ PR drafts live in the repo's common git dir, shared by every worktree and
560
+ never committed: `<common>/diffstalker/pr/<branch>/`, with `feature/x`
561
+ nesting as `pr/feature/x/`. The daemon writes only `top.md` (the author's
562
+ words, from the draft panel) and `adopted` (the sha256 of a suggestion the
563
+ author started from). `top.suggested.md` and `ai.md` belong to agents: the
564
+ daemon reads them and never writes or removes them. A branch name is
565
+ checked as a path before any read or write: no empty, `.` or `..` part, no
566
+ part starting with `.` or ending in `.lock`, no part named like a draft
567
+ file, no backslash or control character, and no symlinked directory on the
568
+ way down from the common dir (`diffstalker` and `pr` included). A save
569
+ reads every draft file first, so one it cannot read fails the save before
570
+ anything is written. The Work scan leaves out a draft or directory it
571
+ cannot read, with a warning, and does not fail. Draft files are opened
572
+ without following a link and must be regular files of at most 256 KB. An
573
+ open repo's common-dir watcher also watches `diffstalker/pr/` and sends
574
+ `draft-change {branch, prefix}` on the repo stream, without a git refresh.
575
+ `prefix: true` means a directory changed, so every draft at or below
576
+ `branch` may have changed (`""` is all of them).
577
+
578
+ PR state comes from `gh`: every run is `gh api graphql --input -` with a
579
+ fixed query and its variables as JSON on stdin, so no value, from a request
580
+ or from GitHub, is ever in the argv. A refresh reads, side by side, light
581
+ lists of the author's open PRs and of the open PRs the author is asked to
582
+ review (what shows a change, plus checks, conflicts, the review decision
583
+ and the draft flag, about 1 point each), and the author's closed PRs. Only
584
+ a PR that is new, or whose `updatedAt` or head commit moved, is then read
585
+ whole (body, requested reviewers, threads and recent events), ten per
586
+ request; the others keep what was read before. The closed list is the
587
+ newest 300 on the first refresh, then only the PRs updated since the last
588
+ good refresh (less 10 minutes) are asked for and merged in; it has no date
589
+ window, since an old closed PR is what tells the page a branch is finished.
590
+ At most 300 of each list. The one-PR read (`/pr/check`) and the one write
591
+ (`/pr/top`, "Update GitHub": a PR's top zone, nothing else) go the same
592
+ way, and the write's text is `top.md` from disk, never the request's. It
593
+ runs only on `POST /work/prs/refresh` (POST, so the CSRF guard covers it:
594
+ `gh` sends the author's token), never in the background, with one 20 s
595
+ deadline, and a failed refresh is not retried. The answer is kept in
596
+ memory; `GET /work/prs` and `GET /repos/:id/pr` read it and never run
597
+ `gh`. When `gh` is missing or logged out the status says so, and
598
+ everything from git keeps working.
599
+ `POST /work/repo` is POST too: it runs a full git scan, and the CSRF guard
600
+ covers only non-GET requests.
601
+
602
+ The Work page's marks (track, dismiss) are kept at
603
+ `$XDG_STATE_HOME/diffstalker/work.json`, written atomically like
604
+ `daemon.json`. Every daemon of the user shares the file (the dev daemon of
605
+ `bun run serve` too), so `GET /work/marks` reads it and `PUT` applies its
606
+ change to what is on disk, not to what this daemon remembers. The page
607
+ names the marks whose item is gone (`prune`), since only it knows which
608
+ items exist.
609
+
516
610
  ## Follow mode
517
611
 
518
612
  The daemon owns the truth, clients decide policy. It watches ONE hook file