Skip to content

SWF(perf): Batch repo-read gate lookups - #661

Merged
beardthelion merged 1 commit into
mainfrom
perf/combine-repo-read-gate
Dec 2, 2026
Merged

beardthelion merged 1 commit into
mainfrom
perf/combine-repo-read-gate

Conversation

@beardthelion

@beardthelion beardthelion commented Dec 1, 2026 •

Copy link
Copy Markdown
Collaborator

Fetch the canonical repo and its ordered visibility rules in one Postgres statement instead of three. Pick the canonical row before filtering quarantine so a public mirror can't take its place. Rust still decides who can read each path. Missing, withheld, and quarantined repos still return the same 404.

The gate signature stays the same. The separate repo identity check after Git stays in place, including in #648.

Warm repo metadata

Postgres path Gate statements/request, before → after Route statements/request, before → after p50 before p50 after p95 before p95 after
Local TCP 3 → 1 5 → 3 1.754 ms 1.371 ms 2.583 ms 2.649 ms
TCP via Toxiproxy, +20 ms injected RTT 3 → 1 5 → 3 173.618 ms 87.549 ms 184.444 ms 90.945 ms

Method: PostgreSQL 16.16 in Docker. Same database and public repo with two subtree rules for both binaries. Cargo dev builds with debug info off, before at d4cbdb3. Loopback HTTP GET /api/v1/repos/zGateBench/read-gate, one request at a time on a reused connection. 60 warm-ups per run, then 500 samples on local TCP or 250 through Toxiproxy (10 ms each direction, zero configured jitter). Timed through the last response byte and took nearest-rank percentiles. Counted executed route statements with pg_stat_statements, excluding the observer and background jobs. The endpoint still does its separate star-count and homepage lookups.

Local p95 was 0.066 ms higher in this run. With injected delay, p95 fell by 93.499 ms.

Try it

With DATABASE_URL pointing at a PostgreSQL 16+ test server:

cargo test --locked -p twigpine-node --lib repo_read_gate_matches_sequential_lookup

The test compares the old and new gate on the same rows, including rule order, owner and reader access, and byte-identical 404s. It also proves a quarantined canonical row can't fall back to a public mirror.

Strict Clippy hits clippy::double_must_use in blockstore/mod.rs on both this branch and d4cbdb3 with the local Rust 1.99 toolchain.

Summary by CodeRabbit

  • Bug Fixes
    • Repository read access now applies visibility rules from the selected repository consistently.
    • Canonical repository records are preferred when resolving matching repositories, and quarantined repositories remain unavailable for read access.
    • Missing repositories and requests denied by visibility rules continue to be rejected.

@coderabbitai

coderabbitai Bot commented Dec 1, 2026 •

Copy link
Copy Markdown
Contributor

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

The database now shares repository selection and visibility-rule conversion logic. Repository read authorization retrieves the selected repository and its rules through one policy lookup. Tests compare the authorization results with sequential lookups.

Changes: Repository read policy

Layer / File(s) Summary
Repository selection and rule conversion
crates/twigpine-node/src/db/mod.rs
Shared SQL selects a canonical-preferred repository. StoredVisibilityRule conversion is reused by visibility-rule listing.
Read policy authorization
crates/twigpine-node/src/db/mod.rs, crates/twigpine-node/src/api/mod.rs
The database returns the selected non-quarantined repository with path-ordered rules. authorize_repo_read uses this policy, and database tests compare its results with sequential lookups.

Estimated code review effort: 3 (Moderate) | ~20 minutes

Change: Refactor

Possibly related issues

  • Twigpine/node#583 — This change covers canonical selection before quarantine filtering in the read gate. The issue’s purge and sibling-survival cases remain unaddressed.

Possibly related PRs

  • Twigpine/node#125 — Both changes use repository quarantine state; this PR filters the selected canonical repository after selection.

Suggested labels: crate:node

Suggested reviewers: kevincodex1, gravirei

Merge Risk: Minimal · up to 8c303

The change batches repository read-gate lookups into one query while keeping the same access decisions and 404 responses. No concrete merge-blocking risk was found.

Pre-merge checks: Passed 4

Check name Status Explanation
Title check Passed The title "SWF(perf): Batch repo-read gate lookups" matches the main change. The PR replaces three separate repo-read gate queries (repo load, quarantine check, visibility rules list) with one Postgre…
Description check Passed The description follows the template's main sections: Summary, Motivation (no linked issue), What changed is not present, How a reviewer can verify (with a sh block containing the test command), and P…
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Full details: Title check

Explanation

The title "SWF(perf): Batch repo-read gate lookups" matches the main change. The PR replaces three separate repo-read gate queries (repo load, quarantine check, visibility rules list) with one Postgres statement via get_repo_read_policy. The title is short, specific, and uses a Conventional Commits-style scope.

Full details: Description check

Explanation

The description follows the template's main sections: Summary, Motivation (no linked issue), What changed is not present, How a reviewer can verify (with a sh block containing the test command), and Protocol & signing impact is not addressed. It gives a clear summary, a benchmark table, validation steps, and notes on the Clippy issue. Several template sections are missing or incomplete: the 'Closes #' issue link is blank, the 'Kind of change' checkbox is not ticked, the 'What changed' bullets are absent (the crate touched, twigpine-node, is only implied), the 'Before you request review' checklist is not addressed, and the protocol block is not addressed. The description is mostly complete, so the check passes, but these gaps should be filled.

Resolution

Optional improvements: tick a 'Kind of change' box (this looks like Refactor or Performance-related, so the template may need a matching option), fill in or remove the 'Closes #' line, add 'What changed' bullets that name twigpine-node and the touched functions (authorize_repo_read, Db::get_repo_read_policy, StoredVisibilityRule), and state which 'Before you request review' items were run.

Finishing Touches

Generate unit tests (beta)

  • Commit to this branch
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Usage-based review receipt

Note

This review was completed with usage-based billing: files reviewed beyond your plan's included limits are billed at $0.25/file. View usage-based billing.

Recent review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: defaults
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 226e6a1d-6124-40ca-b30a-52689d92bc21
Commits

Reviewing files that changed from the base of the PR and between 69f7f36 and 8c303f7.

Files selected for processing: 2

  • crates/twigpine-node/src/api/mod.rs
  • crates/twigpine-node/src/db/mod.rs

Limit details: You’ve used the included review currently available.

@jatmn jatmn left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Merge readiness

  • GitHub reports mergeable_state: blocked with required CI green on head 8c303f7. That usually means an outstanding approval or branch-protection rule, not a technical merge conflict (mergeable: true).

  • The PR branch is one commit behind its configured base ref (behind_by: 1 on 69f7f36). Rebasing onto current main before merge is cheap insurance even though no main commits since the merge-base touch crates/twigpine-node/src/api/mod.rs or crates/twigpine-node/src/db/mod.rs.

  • PR template gaps CodeRabbit flagged (unchecked "Kind of change", empty Closes #, missing "Before you request review" checklist) are housekeeping for the author, not merge blockers for this perf refactor.

Findings

🟢 Thanks for the contribution. I do not see any actionable issues from my review.

The batched get_repo_read_policy path preserves the security shape that mattered on merge-base: pick canonical before mirror, apply quarantine withhold after LIMIT 1 so a quarantined canonical cannot yield the public mirror, then run the same Rust visibility_check. The new sqlx equivalence test is the right oracle for that contract.

@beardthelion
beardthelion added this pull request to the merge queue Dec 2, 2026
Merged via the queue into main with commit 23635a9 Dec 2, 2026
22 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants