WRITE · TRACE · UNDERSTAND

Filter retrieval permissions before selecting context

Implement authorized_topk(docs,tenant,roles,k).

Read lesson: Filter retrieval permissions before selecting context →

Your experiment

Implement authorized_topk(docs,tenant,roles,k). Each doc has id,tenant,roles (empty means tenant-public),score,text. Keep same-tenant docs whose roles intersect caller roles or are empty; rank by descending score then id; return at most k ids. Reject k<0. Do not mutate docs.

Python runs in your browser. Your draft stays on this device.

What to look for

Authorization must precede data disclosure. This local policy tests access filtering; it does not claim that role checks detect prompt injection or that a model will obey instructions once untrusted text enters its context.

Watch your code work

Ready to experiment

Write a solution or load the worked answer, then choose Trace & check.

Step through arrays, variables and training curves produced by your own Python.

About this algorithm

How Filter retrieval permissions before selecting context works

The idea

In a shared search index the documents of several customers sit side by side, and inside one customer some documents are limited to certain roles. authorized_topk first removes everything the caller may not see, and only then ranks what is left and takes the top k.

The order of those two operations is what matters. If you rank first and filter afterwards, forbidden documents take places in the top k, and the caller is left with fewer results or none. Filtering first also means a forbidden document can never reach the prompt, however high it scores. A document passes when its tenant equals the caller's and its roles list is either empty, meaning visible to the whole tenant, or shares at least one role with the caller.

How it works

  1. Raise ValueError when k is negative.
  2. Keep only the documents whose tenant equals the caller's tenant.
  3. Of those, keep a document when its roles list is empty or shares at least one role with the caller's roles.
  4. Sort the survivors by descending score, with equal scores ordered by id.
  5. Return the ids of the first k, building new lists so that docs is never mutated.

What it costs

Time
O(n log n)
Space
O(n)

Filtering is one pass over the n documents and the sort dominates; production systems push the same filter into the index query so forbidden rows are never scored.

Where it is used

  • Pinecone separates tenants with namespaces and applies metadata filters during the vector search itself.
  • PostgreSQL row-level security policies restrict which rows a query may return for the current user, including pgvector similarity searches.
  • Elasticsearch document-level security attaches a query to a role, so users holding that role only see the documents it matches.

Watch out for

  • Taking the top k first and filtering afterwards, which returns fewer than k results and lets hidden documents shape what is selected.
  • Trusting a tenant or role named in the query or in a document's text: only the tenant and roles arguments identify the caller.
  • Treating an empty roles list as visible to nobody, or as visible across tenants. Here it means public inside its own tenant only.