Repository navigation
OpenConceptLab/ocl_online#739 | The collection facets list only collection versions the requester can view - #1044
Conversation
…ction versions the requester can view A concept or mapping search's collection, collection_version and expansion facets named every collection version and expansion its matches belong to, as the index lists them: versions the requester can't view and versions since deleted too, with their member counts. For a requester who isn't staff, each name now counts only the matches in the versions they can view, by the rule the collection filter uses (Collection.get_viewable_versions_criteria), and a name with none is left out. A bucket's count is what its collection filter finds for them. The facet request also asks for the collection version URLs (and, outside global search, expansion URLs) among the matches, up to max_viewable_collection_urls; one query keeps the ones the requester can view. A name with one URL takes that URL's count; names with several get exact counts from one more size-0 request (a filters aggregation). Past the URL cap a name can undercount, but a version the requester can't view never shows. Staff are unchanged. A facetsOnly answer on a source version's list is cached for a day; its cache key now names the requester, so one requester's facets never reach another. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01AaZ4qTpN4J5JFjNSmhYF49
…ctions matched, and a cached facets answer is filtered for each requester Review pass 1 on oclapi2#1044: - The facets no longer list the version URLs among the matches, which stopped at 1,000: on a broad search, versions the requester can't view could crowd out the ones they can. The search now lists the top 300 collection names matched (`_collection_names`), and one query reads the viewable versions' expansions of those collections. A filters aggregation counts each collection, version name and expansion over the URLs those expansions give their members' documents, so the names read as the index has them (a version name with a space stays encoded). - A facetsOnly answer on a source version's list is cached again as anyone who can run the search sees it, and each answer, cached or not, is then filtered for its requester (get_requester_facets). A collection made private, or a member who leaves, no longer leaves its name in a cached answer. The cache key is the shared one again, and the updatedBy facet is dropped for signed-out requesters on cached answers too. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01AaZ4qTpN4J5JFjNSmhYF49
Codex review, pass 1 (codex-cli 0.169.0, commit 313c498)Verdict: findings: 2 P1, 3 P2. Two P1s fixed and one P2 fixed in ad283a2; two P2s accepted. Detailed scenarios are on the tracking ticket.
|
|
Test runs on ad283a2 (local compose stack, oclapi2-local image with pylint 4.0.9): |
…ction filter matches them; shared facet entries get their own cache key Review pass 2 on oclapi2#1044: - The collection and collection_version facets count over the URLs of the collection versions the requester can view, as the collection filter matches them, rather than over their expansions. A version whose only expansion was deleted is still counted while its members' documents still name it. The expansion facet counts over expansion URLs. A version or expansion name is counted only if its facet lists it, so the count request stays the size of the facets. - A facetsOnly entry is cached as anyone sees it, under a key ending in `#for-anyone` (FACETS_CACHE_SUFFIX). So an answer cached before as one requester's, under the old key, is never read as anyone's, and code from before this change never reads the new entries. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01AaZ4qTpN4J5JFjNSmhYF49
snyaggarwal
left a comment
There was a problem hiding this comment.
Reviewed at ad283a2. Two findings inline, plus two notes.
- Stale description. The PR description still describes the first pass: facets built from a list of up to 1,000 URLs (
max_viewable_collection_urls), and a cache key that names the requester. Since ad283a2 the facets work from the top 300 collection names, the cache key is unchanged, and each answer is filtered for its requester. Please update it so the merged PR text matches the code. - Cost of a cache hit. A cached
facetsOnlyanswer still costs a DB query on every hit for non-staff. When any collection names are present it also costs a size-0 ES request. So the day-long cache now saves only the main aggregation. That's fine by me, but the "Cost" section should say so.
Codex pass 1 (F1.1–F1.5, in the author's comment above) is already covered there, so I haven't repeated it.
| collection_url_facets = {'collection': 'collection_url', 'collection_version': 'collection_url', | ||
| 'expansion': 'expansion_url'} |
There was a problem hiding this comment.
The recount makes the collection_version and expansion facets count only versions the requester can view, but their filters still read the raw index. They aren't in collection_facets, so get_faceted_filters turns ?collectionVersion= and ?expansion= into a plain term query, and get_collection_facet_criterion never sees them. In a source scope:
- An outsider sees
v1: 1, clicks it, and gets the members of every collection'sv1, private ones included. That's more rows than the count, and it shows which concepts are in a version they can't view. - A name left out of the facet still lists its members when typed in. In the new test,
?collectionVersion=hidden…works for an outsider.
So the description's "a bucket's count is what clicking it finds" holds for collection only.
Fix options:
- In this PR: route these two filters through the same viewable criterion.
collection_version=<name>becomesterms collection_urlover the viewable versions with that name, andexpansion=<name>becomesterms expansion_urlover the viewable expansions. - Or open a follow-up ticket and narrow the description's claim to
collection.
Either way, a check like listed(f'{url}?collectionVersion=v1', self.outsider, 'concepts') next to the facet assertions in test_collection_version_and_expansion_facets_name_only_versions_the_requester_can_view would pin it.
There was a problem hiding this comment.
Thanks. I checked this on b73149a before changing anything: collection_version and expansion aren't query-string filters. Neither is in Concept.es_fields or Mapping.es_fields, so get_faceted_fields never picks them up. In a source scope, an outsider's and a member's ?collectionVersion=v1, ?collectionVersion=hidden…, ?expansion=hidden… and ?collectionVersion=nope all list exactly the unfiltered scope, on both the DB path and the search path (with q). So clicking a bucket doesn't narrow anything and can't show a version's members.
I pinned it as you suggested, next to the facet assertions in that test (b73149a): each of those queries lists what the unfiltered scope lists for an outsider. The description now claims "count = what clicking finds" for collection only, and says the other two aren't filters. If they're ever made filterable, they should go through get_collection_facet_criterion like collection.
There was a problem hiding this comment.
Confirmed, my mistake. get_faceted_fields builds the filter list from es_fields, and neither Concept.es_fields nor Mapping.es_fields has collection_version or expansion. So ?collectionVersion= and ?expansion= never reach the query, and nothing leaks. The new assertions in b73149a pin that, and the description now scopes its claim to collection. Resolved.
| if data and self.only_facets(): # cached as anyone sees them, so as this requester sees them | ||
| return Response({'facets': {'fields': self.get_requester_facets(data['facets']['fields'])}}) |
There was a problem hiding this comment.
Entries cached before this deploys (they live for a day) were stored by the old get_facets(), so they have no _collection_names. On a hit, keep_collection_facets_viewable gets collections == [] and finds no URLs. It then sets collection, collection_version and expansion to [] for every requester who isn't staff, until the entry expires or the repo's cache is cleared. So for up to a day after release, every source version's cached facets would show no collections.
_collection_names is always in the new entries (the faceted response has a key for every facet, even an empty one), so an entry without it is from before the change. Treating it as a miss recomputes the entry and caches it in the new shape:
| if data and self.only_facets(): # cached as anyone sees them, so as this requester sees them | |
| return Response({'facets': {'fields': self.get_requester_facets(data['facets']['fields'])}}) | |
| if data and self.only_facets(): # cached as anyone sees them, so as this requester sees them | |
| if '_collection_names' in get(data, 'facets.fields', {}): | |
| return Response({'facets': {'fields': self.get_requester_facets(data['facets']['fields'])}}) | |
| data = None # cached before collection facets were recounted |
There was a problem hiding this comment.
Agreed. 0290592 handles it with a separate key rather than a shape check: shared entries are stored under a key ending #for-anyone (FACETS_CACHE_SUFFIX), so entries from before the change sit under the old key and are never read. Non-staff get a fresh computation on the first hit, which is then cached in the new shape. It also covers a rollback (Codex pass 2, F2.1): old code never reads the new entries, which hold _collection_names. test_repo_version_concepts_facets_ignore_answers_cached_before checks that an answer under the old key isn't read.
There was a problem hiding this comment.
The #for-anyone key works better than my shape check: it also covers a rollback, and clear_cache still clears it because delete_pattern matches by prefix. Resolved.
…nsion facets aren't query-string filters Naming one of their buckets in the query string lists the scope as it is (review on oclapi2#1044). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01AaZ4qTpN4J5JFjNSmhYF49
Codex review, pass 2 (codex-cli 0.169.0, commit ad283a2)Verdict: findings: 1 P1, 4 P2. The P1 and two P2s are fixed in 0290592 (and b73149a adds a test); two P2s accepted. From pass 1, Codex reads F1.1 and F1.2 as followed up by F2.1 to F2.4 below; F1.5 resolved; F1.3 and F1.4 accepted as before (F1.4 is now OpenConceptLab/ocl_online#753). Detailed scenarios are on the tracking ticket.
|
|
Thanks, Sunny. Both notes are done in the description: it now describes the top-300 names, the |
Codex review, pass 3 (codex-cli 0.169.0, commit b73149a)Verdict: findings: 2 P2, no P0 or P1. Both accepted; this was the last Codex pass. Codex reads F1.1, F1.2, F1.5, F2.1 and F2.5 as resolved. F1.3, F1.4 (OpenConceptLab/ocl_online#753), F2.2 and F2.3 stay accepted. It reads F2.4's fix as narrowing the version and expansion names (F3.2 below).
Simplification review and the adversarial access review follow. |
Simplification review (Claude subagent, commit b73149a)Verdict: 4 suggested cuts, all low confidence. None applied, so the head stays b73149a and no Codex check of later commits is needed.
|
Adversarial access pass (Claude subagent, commit b73149a)Verdict: no way in found. A requester who isn't staff can't learn the name of a collection, version or expansion they can't view, or how many concepts or mappings it holds, through the
No change follows from it. |
snyaggarwal
left a comment
There was a problem hiding this comment.
Re-reviewed at b73149a (0290592 and b73149a). Both threads are resolved, and my first finding was wrong: collection_version and expansion were never filters.
get_viewable_collection_facet_urls now reads version URLs and names from Collection.uri, the same URLs the collection filter matches through. A HEAD version's URI has no version segment and reads as HEAD, which matches how documents store a HEAD version's collection_url. So a collection whose expansion was deleted on its own still counts what its filter lists, which the new deleted-expansion assertion covers. Version and expansion names are now limited to what the raw facet lists, so the recount stays the facet's size. The new description is accurate, including the cost of a cached hit.
The two accepted gaps, F3.1 and F3.2, can only leave a name missing. They never show a wrong count or a private name. That's fine by me.
Optional nit: test_repo_version_concepts_facets_ignore_answers_cached_before has no mappings twin, unlike the other cache tests. The code path is shared, so this isn't blocking.
No new findings from me.
snyaggarwal
left a comment
There was a problem hiding this comment.
Approving at b73149a. Both earlier threads are resolved; the one remaining nit (a mappings twin for the old-cache-key test) is optional.
Codex check of commits b73149a..e3dd745 (codex-cli 0.169.0)Verdict: clean, no findings. e3dd745 adds only the mappings twin of the old-cache-key test that the review asked for: @snyaggarwal: your optional nit is done in e3dd745. |
Closes OpenConceptLab/ocl_online#739.
What changes
A concept or mapping search's
collectionfacet lists only collections the requester can view, and each count covers only the matches in the versions they can view. The rule is the one thecollectionfilter already uses (Collection.get_viewable_versions_criteria, from #1027), so acollectionbucket's count is what that name's filter finds. Thecollection_versionandexpansionfacets (in org, user, source and collection scopes) follow the same rule. They aren't query-string filters: naming one of their buckets lists the scope as it is. A name the requester can't view in any version is left out. Versions since deleted, which the index keeps until their members are indexed again, drop out too. Staff see no change.How
_collection_names,max_collection_facet_names). It's removed before the facets are returned.keep_collection_facets_viewablereads, in one DB query, the URLs of those collections' versions the requester can view, and for theexpansionfacet their expansions' URLs. It counts eachcollectionname, and eachcollection_versionandexpansionname the facet lists, in one size-0 Elasticsearch request: afiltersaggregation with atermsfilter oncollection_urlorexpansion_urlper name. Names read as the documents read them.get_facetssplits in two.get_facets(for_requester=False)gives the facets as anyone who can run the search sees them, andget_requester_facetsapplies what depends on the requester:updatedByonly for the signed in, and the collection facets above.facetsOnlyanswer on a source version's list is still cached for a day, now asget_facets(for_requester=False), under a key ending#for-anyone. Every answer, cached or not, goes throughget_requester_facets, so a cached entry never carries one requester's view to another, and losing access takes effect at once. Entries cached before this change sit under the old key and aren't read. The repository's cache clearing matches by prefix, so it still clears them.CustomESFacetedSearchtakesextra_facets.Cost
For requesters who aren't staff, a facets request costs one more terms aggregation (the 300 names, in the same request), one DB query, and one size-0 Elasticsearch request when any collection facet has buckets. A cached
facetsOnlyhit saves the main aggregation only: the DB query and the size-0 request still run on every hit. Staff skip the DB query and the extra request.Tests
QueryCollectionAccessTest(ES-tagged), for signed out, an outsider, a member and staff, concepts and mappings:collectionfacet: a private collection, a public one with a private version, and a public one with a public version. Each count equals what that name'scollectionfilter lists;collection_versionandexpansionfacets leave out a private version's name, and a version name with a space keeps the index's form for everyone. Neither facet is a query-string filter;facetsOnlyanswer on a source version: a member's view doesn't reach an outsider or a signed-out requester, nor outlast the member's access.ConceptListCacheTest/MappingListCacheTest: thefacetsOnlycache key ends#for-anyone, and an answer cached under the old key isn't read.Local runs on 0290592:
pylint -j0 core/clean; ES-tagged suite 701 OK; non-ES suite 3,647, one failure incore.capacity's renewer-thread timing test, which passes alone (80 OK) and is a known flake under load.QueryCollectionAccessTeston b73149a: 13 OK.🤖 Generated with Claude Code
https://claude.ai/code/session_01AaZ4qTpN4J5JFjNSmhYF49