Skip to content

OpenConceptLab/ocl_online#747 OpenConceptLab/ocl_online#546 | Version lists, counts and latest are of the versions the requester can view - #1119

Open
paynejd wants to merge 4 commits into
masterfrom
ocl_online-747-viewable-versions
Open

paynejd wants to merge 4 commits into
masterfrom
ocl_online-747-viewable-versions

Conversation

@paynejd

@paynejd paynejd commented Dec 3, 2026

Copy link
Copy Markdown
Member

What the API answers now

For a source or a collection alike:

Read Answer
…/versions/, GET and HEAD, with any parameter the versions the requester can view, and num_found counts those
latest in a URL: …/latest/, its summary and export, and everything read under it the newest released version the requester can view. With none, it answers as a repository without releases does. A repository the requester can't view is refused, as before
versions in a repository's summary, latest_released_version, previous_version_url of the versions the requester can view
/sources/, /collections/ and /repos/, an owner's or a member's lists, with or without ?version= a version is listed only if its HEAD is viewable too
the same with allVersions=true hits, counts and facets follow the same rule

Staff, a repository's owner and its organization's members see what they saw. Writes under latest go to the same version as before, or are refused: under a collection's latest with no release, 404.

Where a repository's versions are embedded (pins, an expansion's value set versions, a reference's), the count and the previous version are the requester's too.

How

  • One rule. ConceptContainerModel.get_viewable() (get_viewable_versions_criteria) already says which versions a user can view. These reads now use it. find_latest_release_for(filters, user) is what latest means to a user.
  • The rule is cheaper. Its HEAD check is one condition for organizations and one for users, where it was an OR, which Postgres couldn't join on. On production's largest versions list (678 versions) that's 3 ms, down from 290 ms, and the reads that used the rule already gain too.
  • RepoLatestVersionMixin and RepoExportVersionMixin each replace copies of one lookup. RepoVersionListMixin reads a page's previous versions in one query, where each row had its own.

Cost, on production data

Signed out: the count of /sources/ goes from 2 to 10 ms, that of /collections/ from 11 to 86 ms (11,798 rows). A versions list is unchanged.

For the reviewer

  • latest used to be refused when the newest release was private. It's now the newest one the requester can view. The tests that said otherwise are updated (tests_access_control.py, tests_release_map_codes.py).
  • An export file names the newest public version before it, whatever its own version's access is: the file is written once and stays when that access changes.
  • A collection's $cascade starts from a concept of the version the URL names, HEAD's without one: any of its expansions', or the one the URL names.
  • /user/collections/<c>/latest/… now reads the signed-in user's own collection's release.
  • HEAD on a /repos/ search with no hits answers 0, where it failed.

Tests

New: core/integration_tests/tests_viewable_versions.py: signed out, another signed-in user, a member, an admin, an owner and staff, for sources and collections, HEAD and releases apart.

Refs OpenConceptLab/ocl_online#747, OpenConceptLab/ocl_online#546, OpenConceptLab/ocl_online#735.

🤖 Generated with Claude Code

https://claude.ai/code/session_01AaZ4qUWvM4HRKZktm5NSVk

paynejd and others added 4 commits December 3, 2026 01:24
…ptLab/ocl_online#735 | Version lists, counts and latest are of the versions the requester can view

A source's or collection's versions list holds the versions the requester can
view, and so do the version count in its summary, latest_released_version and
previous_version_url. latest in a URL names the newest released version they
can view, for the version and for everything read under it. Lists and searches
of sources and collections show a version only with its HEAD, whichever
versions they ask for.

The rule's HEAD check is one condition per kind of owner, which the database
can join on, so it costs a list of versions next to nothing.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01AaZ4qUWvM4HRKZktm5NSVk
…rt of the latest version is written only where it was before

A read of latest/export/ by someone who can't view the latest version gets the
newest one they can view. A write is to the latest version, or refused.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01AaZ4qUWvM4HRKZktm5NSVk
… under latest needs a release, and versions embedded elsewhere follow the reader

A write under a collection's latest is refused when it has no release. A
collection's $cascade starts from a concept of the version its URL names, HEAD's
without one. A pinned repository's version count, and the previous version of
one an expansion or a reference lists, are the requester's. An export of a
private version names the one before it, whoever can view the export being able
to view both.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01AaZ4qUWvM4HRKZktm5NSVk
… concept of the version it names, and an export names the previous public version

Without a version it starts from HEAD's, the one it walks: from any expansion, or
the one the URL names. `source` picks among concepts that share a code.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01AaZ4qUWvM4HRKZktm5NSVk
@paynejd

paynejd commented Dec 3, 2026

Copy link
Copy Markdown
Member Author

Reviews: two full Codex passes and checks of the commits after them, a simplification review, and an access-control pass that compared signed-out, member and staff answers with master. The findings and answers are on OpenConceptLab/ocl_online#747. No P0 or P1 is open. Two P2s are accepted: odd query strings on a collection's $cascade, which its sibling reads treat the same way.

Lint and the full suite pass on 855949b (4,308 tests, and 818 with search).

After the deploy, one clean-up of export files is on OpenConceptLab/ocl_online#747.

paynejd added a commit that referenced this pull request Dec 3, 2026

@snyaggarwal snyaggarwal left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Approve. Mergeable as is. The two notes below are about performance and can be follow-ups.

Merge order: #1119, then #1120, then #1128, as in the hand-back. All three merge cleanly onto today's master (3c2e9c1) and onto each other in either order. #1119 and #1120 carry the same get_viewable_versions_criteria hunk, and #1119 and #1128 the same HEAD num_found line, so whichever goes second has nothing to resolve. CI ran on 7ab829e. Since then master has gained #1123, #1127 and #1124, which touch core/concepts/models.py and core/mappings/models.py in other hunks only.

What I checked, beyond the Codex passes and the adversarial pass on OpenConceptLab/ocl_online#747, which I don't repeat here:

  • Leaks, by requester. I checked the versions list (GET and HEAD), latest (reads, summary, export and the reads under it), the summary's versions, and previous_version_url, including where it's embedded in expansions, references and pins. I also checked the ?version= and allVersions lists. tests_viewable_versions.py covers signed out, outsider, member, admin, owner and staff. I found no path left that names a hidden version. The create serializers still return versions = num_versions, but only to the creator, and no detail serializer has that field.
  • DB and ES agree. The DB lists go through get_viewable. allVersions searches exclude by id the versions that only their HEAD hides. #1120 applies the same rule to the facet.
  • Cache. The only cached lists here are a version's concepts and mappings. Their key comes from the resolved version's URI (get_concepts_cache_keys), not from the request URL. So when latest resolves differently for different users, each version gets its own key. Nothing is shared across users that shouldn't be.
  • Queries. load_previous_version_uris makes one query per page, where there used to be one per row. Summary counts and latest_released_version stay at one query per row, as before.

Non-blocking, inline

  1. HEAD-only lists don't need the HEAD check. Skipping it there would likely win back most of the signed-out /collections/ count going from 11 to 86 ms.
  2. The allVersions exclusion negates the criteria, which brings back the OR of two EXISTS this PR removed elsewhere. Please measure it on production.

Comment thread core/common/views.py

def filter_queryset_by_public_access(self, queryset):
user = self.request.user
if issubclass(queryset.model, ConceptContainerModel): # a version shows only with its HEAD

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Non-blocking, performance. The default /sources/, /collections/ and /repos/ DB lists are filtered to version=HEAD. For a HEAD row, "its HEAD is private" is the row's own public_access, which criteria already checks, and own is already left out of private_heads. So in those lists the two NOT EXISTS checks can never remove a row. That's likely most of the signed-out /collections/ count going from 11 to 86 ms. One fix is to skip the HEAD check when the list is HEAD-only, meaning no ?version= and no allVersions. A heads_only argument to get_viewable_versions_criteria would do it.

Comment thread core/common/views.py
viewable = model.get_viewable_versions_criteria(self.request.user)
if viewable is None:
return results
ids = list(model.objects.filter(

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Non-blocking, performance. ~viewable negates the criteria, which turns the two NOT EXISTS back into EXISTS(private org HEAD) OR EXISTS(private user HEAD). That's the OR the PR body says Postgres can't join on. It runs on every allVersions search, over every active public version. It also runs for both models on /sources/ and /collections/, where the repo_type term means only one can match. Could you time it on production? If it's slow, two changes would help: query only the model the search covers, and build the ids from two positive semi-joins, public active versions with Exists(private_heads.filter(mnemonic=..., organization_id=...)) and then the same for user_id, reusing the private_heads queryset from get_viewable_versions_criteria.

This branch has not been deployed

No deployments
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