✨ Limit heavy branch review reads and count a branch's tasks

Give concurrency limits to the branch review and task commands that
decode a document or a review's history: the review bundle, the
activity, the branch info and review listings, moving a review thread,
and marking the tasks of deleted layers. The numbers are generous for
a tech preview with few users.

Add get-file-branch-counts, the threads and the open and closed tasks
of a branch file in a review or not, so the branch delete confirmation
can say what goes with the branch.

AI-assisted-by: mixed models
This commit is contained in:
Álvaro Tejero Cantero 2026-11-23 20:56:40 +01:00
parent ca6cc3c2b2
commit 125c367e07
No known key found for this signature in database
5 changed files with 199 additions and 10 deletions

View File

@ -121,8 +121,8 @@
;; warning (`climit.clj::wrap`). The write-side global queues
;; follow the anchor's ratio of five waiters to three permits, rounded
;; up. The per-profile entry of one permit and a queue of two keeps a
;; single designer to one operation at a time. The two read-side
;; entries are generous by design, with the ratio of
;; single designer to one operation at a time. The read-side entries
;; are generous by design, with the ratio of
;; `:update-file/global` (fifty waiters to twenty permits), so a panel
;; open waits for its turn instead of being refused: six
;; three-document comparisons hold eighteen realized documents, fewer
@ -205,4 +205,60 @@
;; bounded wait sits on the global entry here rather than on a
;; per-profile entry. Provisional.
:update-branch-review-revision/global
{:permits 3 :queue 4 :timeout 60000}}
{:permits 3 :queue 4 :timeout 60000}
;; The review sandbox decodes the pinned snapshot and serves it whole:
;; one realized document and one whole-file payload in memory, the
;; anchor's shape with no snapshot write behind it. Three at once is the
;; anchor's count, and eight waiters give the reviewers of one review
;; room to open their sandboxes together. Provisional.
:get-branch-review-bundle/global
{:permits 3 :queue 8 :timeout 60000}
;; The activity decodes one blob per row of the review, folds the rows
;; as they stream, and serves the folded history. No document is
;; realized, but the work grows with the number of saves rather than
;; with the size of the file, so it sits between the anchor's three and
;; the listing's six, with the read-side ratio for its waiters.
;; Provisional.
:get-branch-review-activity/global
{:permits 4 :queue 10 :timeout 60000}
;; Moving a thread left behind realizes the target document, then moves
;; the thread, its tasks and their events: the one document
;; `create-branch-review` realizes, so it takes the anchor's numbers,
;; and the per-profile entry keeps one designer to one move at a time.
;; Provisional.
:move-branch-review-thread/global
{:permits 3 :queue 5 :timeout 60000}
:move-branch-review-thread/by-profile
{:permits 1 :queue 2 :timeout 60000}
;; Marking the layers of deleted tasks carries their subtrees in,
;; validates up to `max-restore-objects` shapes per layer
;; (`files_task.clj::restore-objects`), and writes them into the event
;; of every task of the layer, so one call can write a document-sized
;; payload. The anchor's numbers bound those copies in flight, and the
;; per-profile entry keeps one designer's bulk deletions from piling
;; up. Provisional.
:mark-tasks-layer-deleted/global
{:permits 3 :queue 5 :timeout 60000}
:mark-tasks-layer-deleted/by-profile
{:permits 1 :queue 2 :timeout 60000}
;; A file open asks for the branch of the file, and a cold miss on a
;; diverged branch runs the compare dialog's own three-document
;; comparison, so the open path shares the compare's numbers instead of
;; opening a way around them. The shared summary cache answers a warm
;; open without realizing anything. Provisional.
:get-file-branch-info/global
{:permits 6 :queue 15 :timeout 60000}
;; A cold review listing realizes the target once and compares each
;; uncached diverged review against it, the branches listing's shape
;; with main shared across rows, so it takes the listing's numbers.
;; Provisional.
:get-file-branch-reviews/global
{:permits 6 :queue 15 :timeout 60000}}

View File

@ -2067,9 +2067,15 @@
open branch whose revn deltas are not both zero runs the three-way diff
(`branch-diff-counts!`), which loads main, the branch and its base.
Returns nil when branching is disabled, so it is safe to call on every
file open."
file open.
The command declares the compare dialog's limit: a warm open answers
from the summary cache without realizing anything, while a cold one on
a diverged branch runs the same three-document comparison as
`get-file-branch-diff`."
{::doc/added "2.16"
::sm/params schema:get-file-branch-info}
::sm/params schema:get-file-branch-info
::climit/id [[:get-file-branch-info/global]]}
[cfg {:keys [::rpc/profile-id file-id]}]
(when (contains? cf/flags :branching)
(db/run! cfg

View File

@ -484,9 +484,14 @@
Those counts come from the branch summary cache that the branches
listing fills, keyed on the same tuple: a repeated listing with no
intervening save performs no comparison work and realizes no file, and
ten open reviews on one branch pay for one computation."
ten open reviews on one branch pay for one computation.
The listing declares the branches listing's limit, because a cold one
holds the same documents: the target realized once and reused across
rows, and each diverged review compared against it."
{::doc/added "2.16"
::sm/params schema:get-file-branch-reviews}
::sm/params schema:get-file-branch-reviews
::climit/id [[:get-file-branch-reviews/global]]}
[cfg {:keys [::rpc/profile-id file-id include-closed]}]
(check-branch-reviews-enabled!)
(db/run! cfg
@ -649,7 +654,8 @@
and `:last-save` the newest save. Every row of the review is read, so
the oldest change still counts in what a layer ends up as."
{::doc/added "2.16"
::sm/params schema:get-branch-review-activity}
::sm/params schema:get-branch-review-activity
::climit/id [[:get-branch-review-activity/global]]}
[cfg {:keys [::rpc/profile-id id]}]
(check-branch-reviews-enabled!)
(db/tx-run! (assoc cfg ::db/read-only true)
@ -675,6 +681,7 @@
{::doc/added "2.16"
::sm/params schema:get-branch-review-bundle
::sm/result files/schema:file-with-permissions
::climit/id [[:get-branch-review-bundle/global]]
::db/transaction true}
[cfg {:keys [::rpc/profile-id id]}]
(check-branch-reviews-enabled!)

View File

@ -43,6 +43,7 @@
[app.loggers.webhooks :as-alias webhooks]
[app.msgbus :as mbus]
[app.rpc :as-alias rpc]
[app.rpc.climit :as-alias climit]
[app.rpc.commands.comments :as comments]
[app.rpc.commands.files :as files]
[app.rpc.commands.profile :as profile]
@ -731,7 +732,9 @@
the deleted layer, so it can be restored later, when the layer is small
and valid enough (`restore-objects`). Returns the tasks it moved."
{::doc/added "2.16"
::sm/params schema:mark-tasks-layer-deleted}
::sm/params schema:mark-tasks-layer-deleted
::climit/id [[:mark-tasks-layer-deleted/by-profile ::rpc/profile-id]
[:mark-tasks-layer-deleted/global]]}
[cfg {:keys [::rpc/profile-id ::rpc/session-id file-id layers]}]
(check-tasks-enabled)
(files/check-read-permissions! cfg profile-id file-id)
@ -872,6 +875,81 @@
(update-vals by-tasks #(merge empty %))
(db/exec! conn [sql:get-review-comment-counts ids])))))
;; --- COMMAND QUERY: get-file-branch-counts
;;
;; The delete confirmation of a branch says how many threads and tasks go
;; with it. `get-review-counts` answers per review, which leaves out what
;; was written on the branch outside any review, so these count by the
;; branch file, where the deletion lands: the file row goes, and the
;; threads and tasks that named it lose their file with it.
(def ^:private sql:get-branch-file-id
"SELECT fb.branch_file_id
FROM file_branch AS fb
WHERE fb.id = ?
AND fb.deleted_at IS NULL")
(def ^:private sql:get-thread-count
"SELECT count(*) AS total
FROM comment_thread
WHERE file_id = ?")
(def ^:private sql:get-task-counts
"SELECT status, count(*) AS total
FROM file_task
WHERE file_id = ?
AND deleted_at IS NULL
GROUP BY status")
(defn- branch-counts
"The live threads and tasks written on the branch file `file-id`:
`{:threads n :open-tasks n :closed-tasks n}`, tasks split as
`closed-statuses` splits them."
[conn file-id]
(let [threads (-> (db/exec-one! conn [sql:get-thread-count file-id]) :total)]
(reduce (fn [counts {:keys [status total]}]
(let [k (if (contains? closed-statuses status) :closed-tasks :open-tasks)]
(update counts k + total)))
{:threads threads :open-tasks 0 :closed-tasks 0}
(db/exec! conn [sql:get-task-counts file-id]))))
(def ^:private schema:get-file-branch-counts
[:map {:title "get-file-branch-counts"}
[:branch-id ::sm/uuid]])
(sv/defmethod ::get-file-branch-counts
"What deleting the branch takes with it, for the confirmation the client
shows before it deletes: how many live threads and tasks were written
on the branch file, tasks split into open and closed, where closed
means verified or dropped as won't do, as the merge reads a task. A
thread has no deleted marker (a deleted thread is gone) and a
logically deleted task is not counted. The dialog asks just before the
delete, so the numbers are the ones the deletion lands on. Needs read
rights on the branch file, and takes no climit: two indexed counts,
nothing realized."
{::doc/added "2.16"
::sm/params schema:get-file-branch-counts}
[cfg {:keys [::rpc/profile-id branch-id]}]
(check-tasks-enabled)
(db/run! cfg
(fn [{:keys [::db/conn] :as cfg}]
(let [row (db/exec-one! conn [sql:get-branch-file-id branch-id])
branch-file-id (:branch-file-id row)]
(when (nil? row)
(ex/raise :type :not-found
:code :branch-not-found
:hint "unable to find branch with the provided id"
:branch-id branch-id))
;; a kept branch whose file the GC took away carries no
;; threads or tasks any more, and cannot be deleted either
(when (nil? branch-file-id)
(ex/raise :type :not-found
:code :object-not-found
:hint "the branch has no file left to count"
:branch-id branch-id))
(files/check-read-permissions! cfg profile-id branch-file-id)
(branch-counts conn branch-file-id)))))
;; --- Merge: move the threads and tasks of a branch into its target
;;
;; The thread is the unit: a merge moves a thread whole, with every task
@ -1131,7 +1209,9 @@
and a thread already on the target."
{::doc/added "2.16"
::webhooks/event? true
::sm/params schema:move-branch-review-thread}
::sm/params schema:move-branch-review-thread
::climit/id [[:move-branch-review-thread/by-profile ::rpc/profile-id]
[:move-branch-review-thread/global]]}
[cfg {:keys [::rpc/profile-id ::rpc/session-id thread-id]}]
(check-tasks-enabled)
(let [{:keys [target-id source-file-id]}

View File

@ -1213,3 +1213,43 @@
{:id (:id author)})
(let [task (new-task)]
(t/is (= [] (move reviewer task "done"))))))))))
;; --- Counts of what a branch deletion takes with it
(defn- branch-counts*
[profile branch-id]
(th/command! {::th/type :get-file-branch-counts
::rpc/profile-id (:id profile)
:branch-id branch-id}))
(t/deftest branch-counts-say-what-a-delete-takes-with-it
(with-redefs [cf/flags (task-flags)
eml/send-template (fn [_ _] nil)]
(let [{:keys [author reviewer branch quiet closed]} (setup-review-threads)]
(t/testing "threads and tasks are counted by the branch file"
(let [out (branch-counts* author (:id branch))]
(t/is (nil? (:error out)))
;; `mixed` holds the open task and the closed one, the closed
;; task outside it got a thread of its own, and `quiet` and
;; `loose` are the threads with no task
(t/is (= {:threads 4 :open-tasks 1 :closed-tasks 2} (:result out)))))
(t/testing "a deleted thread and a deleted task are not counted"
(t/is (nil? (:error (th/command! {::th/type :delete-comment-thread
::rpc/profile-id (:id reviewer)
:id (:id quiet)}))))
(t/is (nil? (:error (th/command! {::th/type :delete-task
::rpc/profile-id (:id author)
:id (:id closed)}))))
(t/is (= {:threads 3 :open-tasks 1 :closed-tasks 1}
(:result (branch-counts* author (:id branch))))))
(t/testing "a profile that cannot read the branch file is refused"
(let [stranger (th/create-profile* 3 {:is-active true})]
(t/is (= :not-found (-> (branch-counts* stranger (:id branch))
:error ex-data :type)))))
(t/testing "a branch that does not exist is not found"
(t/is (= :branch-not-found (-> (branch-counts* author (uuid/random))
:error ex-data :code)))))))