An assistant answers a question about a contract renewal in eleven seconds, with three citations. All three are real documents. Two of them belong to a different customer. Nothing errored, nothing logged a permission failure, and no test failed, because the permission check that everybody believed was in the pipeline ran on the path nobody used. The retriever searched one index holding every workspace's chunks, took the top forty by cosine distance, and the application removed the ones the caller was not allowed to see just before handing the remainder to the model. The answer was written from the two that survived, and it was fluent, confident and wrong in the way that matters most: it disclosed another customer's contract terms to a person who had no right to read them.
Retrieve broadly, filter afterwards. That is the default shape of a retrieval pipeline, it is what most frameworks make easy, and it is wrong for a reason that has nothing to do with permissions. Filtering after retrieval is not a filter applied to an answer. It is a different query, with different reachability, different recall and a different failure mode — and the failure is silent, because a short result set is a valid result set to every layer below the application.
There are two questions in a retrieval request, and the architecture usually collapses them into one: which documents are closest to this query, and which documents may this reader see. The first is approximate, statistical and tolerant of being a little wrong. The second is exact, non-negotiable, and must hold for every path that can produce text. Everything below is about putting the second question inside the query that answers the first, and about the places that decision still leaks.
Filtering after retrieval is a different query
A vector index does not evaluate your WHERE clause while it searches. It walks a graph or probes a set of lists, collects a bounded number of nearest candidates, and only then does the database apply the predicate. pgvector documents this plainly: with HNSW and the default hnsw.ef_search of 40, a predicate that matches ten percent of rows leaves roughly four, because forty candidates is all the index was asked for. The rows that were filtered out are, by construction, the ones nearest to the query — the most likely to be relevant. Filtering does not prune the tail. It removes the head and keeps whatever distant rows happened to be in the pool.
This is the same mechanism that makes filtered search hard in every engine. Qdrant approaches it by building extra graph edges for indexed payload fields and by estimating filter cardinality so the planner can choose between the graph, a full scan of matching points, and an exact rescore when the filter is very selective. pgvector added iterative index scans in 0.8.0: when the predicate rejects too many candidates, the scan continues through the graph until the limit is satisfied or it hits hnsw.max_scan_tuples, twenty thousand by default. strict_order keeps exact distance ordering across iterations; relaxed_order permits slight reordering in exchange for better recall, at the cost of re-sorting yourself. None of this is exotic. All of it assumes you knew the filter was part of the search.
The number that matters is selectivity. A tenant filter on a single-tenant deployment matches everything. A permission filter matches the fraction of the corpus the reader can see, and for a new user with access to one project out of two hundred, that fraction is small enough that a forty-candidate pool contains none of their documents. The index then returns three rows, or zero, and reports success. The database is not lying. It was asked for the nearest forty and then told to drop most of them.
| Selectivity of the permission filter | What a fixed candidate pool returns | What it looks like |
|---|---|---|
| Most of the corpus | Enough rows, slightly worse ordering | Fine, and hides the problem |
| Ten percent | Roughly four rows for a limit of ten | Answers get thinner, not wrong |
| One percent | Often zero rows | "I could not find anything about that" |
| Empty (reader lost access) | Zero rows, full scan | Indistinguishable from an empty corpus |
Authorisation belongs to the reader, not the document
The instinct is to store a permission list on the document: owner, shared-with, workspace. Real permission models are not that flat. A document inherits from its folder, the folder from its workspace, the workspace from the organisation, and a share to an external guest adds a principal that appears nowhere in the hierarchy. Groups nest. A deny on a subfolder overrides an allow on its parent. Any document may carry a handful of explicit exceptions that make the inherited answer wrong.
So the thing the query needs is not the document's owner. It is the reader's effective principal set — every identity the reader holds, flattened: their user id, every group they are transitively a member of, their role in the tenant, and a marker for genuinely public content. Resolve that set once per session, cache it, and put it in the query.
Where you resolve it decides what breaks.
| Resolution point | Correctness | Failure mode |
|---|---|---|
| At ingest, baked into the chunk | Correct until the first permission change | Revoking access requires re-indexing content that did not change |
| Per query, joined from the ACL tables | Correct | Eight joins on the hot path; the planner cannot estimate the result |
| Precomputed with a version, refreshed on change | Correct within a stated staleness budget | The budget is invisible until an audit asks what it is |
| In application code, after retrieval | Correct only if every path calls it | The path that does not call it is the one that leaks |
The third row is the one that works, provided the staleness budget is written down and the invalidation actually fires. Concretely: a table of effective principals per user, rebuilt when a membership changes, plus an access-control epoch per tenant that increments on any document permission write. The epoch is what makes caching honest — more on that below.
Put the permission in the index
The schema change is small and it is the whole design. Store the flattened principal set on the chunk, alongside the vector, and make it filterable.
CREATE TABLE chunk (
id bigint PRIMARY KEY,
document_id uuid NOT NULL,
tenant_id uuid NOT NULL,
content text NOT NULL,
embedding vector(1024) NOT NULL,
acl_principals text[] NOT NULL,
acl_deny text[] NOT NULL DEFAULT '{}'
);
CREATE INDEX chunk_acl ON chunk USING gin (acl_principals);
CREATE INDEX chunk_deny ON chunk USING gin (acl_deny);
CREATE INDEX chunk_tenant ON chunk (tenant_id);
Tenant is a separate column even though a tenant principal could be folded into the array, because it is the one filter that must never be missing and the one an index can answer cheaply. Deny is a separate array because "allow unless explicitly denied" cannot be expressed by removing a principal from a list — the deny may sit on the parent while the allow sits on the child.
Then the query. Note what is inside the WHERE and what is not.
BEGIN;
SET LOCAL hnsw.iterative_scan = relaxed_order;
SET LOCAL hnsw.max_scan_tuples = 20000;
WITH candidates AS MATERIALIZED (
SELECT c.id,
c.document_id,
c.content,
c.embedding <=> $1 AS distance
FROM chunk c
WHERE c.tenant_id = $2
AND c.acl_principals && $3::text[]
AND NOT (c.acl_deny && $3::text[])
ORDER BY c.embedding <=> $1
LIMIT $4
)
SELECT * FROM candidates ORDER BY distance;
COMMIT;
&& is array overlap, and the GIN index answers it without touching a row. The predicate is inside the scan, so the planner has a selectivity estimate, the iterative scan knows when to keep walking, and the deny check happens in the same pass. The materialised CTE exists only because relaxed_order does not guarantee the final order; re-sorting a few dozen rows costs nothing and removes an ordering bug that is otherwise very hard to see.
SET LOCAL rather than SET matters: the setting lives for the transaction and dies with it, so a pooled connection cannot carry a relaxed scan into the next request, and a query that raises does not leave a session configured differently from its peers.
Two schema-level guards are worth the migration. A CHECK that acl_principals is not empty, because an empty array means "visible to nobody" and is what a bug produces when it resolves a set of zero groups — you want that insert to fail rather than produce an invisible document. And a partial unique index on (document_id, chunk_ordinal), so a partially failed re-ingest cannot double every chunk and quietly halve the effective candidate pool.
The candidate pool is part of the contract
Requesting ten results means the retriever promises to return ten if ten exist. With a permission filter, that promise is broken by default, and the fix is not a bigger ef_search. Over-fetching ten times — ask for a hundred, keep ten — buys headroom proportional to the visible fraction, which is exactly the quantity you do not control. It also multiplies the cost of every stage downstream, including the reranker, for a user whose corpus is large and whose permissions are narrow.
What you need instead is for the query to know it may need to work harder, and for the application to know when it did not get there. Iterative scans give the first part, bounded by max_scan_tuples and the memory multiplier, and the bound exists because walking a large portion of a graph is expensive. The second part is your code: return returned and scanned alongside the rows, and treat returned < k as a signal rather than an outcome. When the count with the same filter is greater than zero and the returned set is short, the filter is too selective for the scan budget — that is a configuration problem you can see, not an absence of information you must explain to a user.
interface RetrievalResult {
chunks: Chunk[];
matchedWithFilter: number;
scanned: number;
truncated: boolean;
}
export async function retrieve(
principal: PrincipalSet,
query: Float32Array,
k: number,
): Promise<RetrievalResult> {
if (!principal.tenantId || principal.principals.length === 0) {
throw new Error('retrieve() requires a resolved principal set');
}
const [rows, count] = await Promise.all([
runFilteredQuery({ tenantId: principal.tenantId, acl: principal.principals, query, k }),
countWithFilter({ tenantId: principal.tenantId, acl: principal.principals }),
]);
return {
chunks: rows.chunks,
matchedWithFilter: count,
scanned: rows.scanned,
truncated: rows.chunks.length < k && count > rows.chunks.length,
};
}
The thrown error is not defensive noise. The most common incarnation of this bug is a service-to-service call with a user id in one field and no tenant in another, where the resolver returns a well-formed empty set and every subsequent layer treats it as "this user can see nothing" instead of "this caller did not say who the user is".
Reranking happens inside the boundary
A cross-encoder over a hundred candidates fixes ordering and costs money per candidate. If the candidates include documents the reader cannot see, you pay for them twice: once in the rerank bill, and once in the latency every user feels because the system is grading text that will be thrown away. If you rerank first and filter after, the loss is worse than cost — the top of a reranked list is dominated by the best-matching documents, so filtering the head of a reranked list is the same over-truncation as filtering the head of a vector scan, with a bill attached.
There is a second reason the order matters, and it is not about efficiency. Sending a chunk to a third-party reranker or a hosted model is a disclosure. It leaves your boundary, it may be retained in a vendor log, and no amount of filtering afterwards un-sends it. The permission predicate has to sit upstream of every hop that leaves your infrastructure, which almost always means: retrieve with the filter, rerank the permitted set, then generate.
Revocation is a cache problem
Once the query is correct, the remaining leaks live in the layers between the query and the reader. A result cache keyed on the query string serves one user's documents to another user who asked the same thing. A prompt cache keyed on a system prompt and a trimmed context is worse, because the answer is already synthesised. An in-flight request started before a revocation completes happily. A provider-side prompt cache has its own lifetime. A conversation that has been "shared with the team" is an export of documents that were permitted when the answer was written.
The cache key has to include the identity that produced the answer, not the text that produced it.
function retrievalCacheKey(input: {
tenantId: string;
principalHash: string;
aclEpoch: number;
queryHash: string;
model: string;
k: number;
}): string {
return [
'ret',
input.tenantId,
input.principalHash,
String(input.aclEpoch),
input.queryHash,
input.model,
String(input.k),
].join(':');
}
principalHash changes when a user joins or leaves a group, because the flattened set changes. aclEpoch covers the case the principal hash cannot see: a document moved into a private folder without any membership changing. Increment it on every document permission write for the tenant and every existing entry for that tenant becomes unreachable — blunt, cheap, and correct, which is the right trade for a security boundary.
For content sensitive enough that a stale cache entry is unacceptable, add a second check at the point of use. Re-resolve the document ids after retrieval against the authorisation service, batched into one call, and drop anything that no longer passes before it reaches the model. This is the same discipline as fencing a queue write: the check lives in the operation that produces the bytes, not in a step that ran earlier and might not have run at all. It costs a round trip. What it must never become is the only check, because a single enforcement point that exists to catch mistakes will eventually be the single point that has the mistake.
Absence is information
"I could not find any documents about the Kestrel renewal." Depending on whether Kestrel exists and is merely restricted, that sentence tells the reader something they were not entitled to learn. Inference from absence is cheap and requires no exploit: ask about a project code, watch the assistant confirm or deny that the corpus contains it. The mitigation is not a smarter answer. It is making the two states indistinguishable — the same empty-state copy, the same number of retrieval round trips, no branch in the model's instructions that explains the difference between "nothing matched" and "you may not see what matched".
The same reasoning applies to numbers. Raw similarity scores in a user-facing API are a probe: query a phrase you know appears in a document you cannot read, and a suspiciously high top score confirms it is indexed. Return the documents, not their distances. Related-document rails, "more like this", and citation chips that resolve document metadata on click are all part of the same surface, and all of them are commonly written months later by somebody who was not in the conversation about the filter.
Every path, not the main one
The predicate above protects one query. A retrieval system has several, and the leaks are almost always in the second one.
- The sparse leg of hybrid search. A BM25 query against the same table, added to improve recall, without the ACL predicate — and it usually returns a different, complementary set, so the leak is content the dense search would never have surfaced.
- Parent-document expansion. Retrieve a child chunk, then fetch its parent for context. The parent can live in a document whose permissions were resolved differently, or in a folder the reader cannot see.
- Summary tables and generated FAQs. A nightly job summarises the corpus into a table keyed by topic with no permission columns at all, and the assistant reads the summary.
- Traces and logs. Chunk text in span attributes, prompt payloads in a debug log, the retrieved-documents panel in an observability UI. Every engineer on the team can read them, which is fine, and every support tool that surfaces them is not.
- The evaluation fixture. A golden set built from real documents, committed to a repository, run in CI, and pasted into a hosted eval dashboard.
- The admin search box. Deliberately unfiltered, authenticated by a session cookie that does not distinguish an operator from a curious employee, and the one path nobody thought to review.
- Exports. Conversation downloads, CSV of "documents used", email digests. Each is a copy that outlives the revocation that prompted the question.
A grep is the cheapest audit: search for the collection or table name and confirm every call site passes a resolved principal. Then search for the raw table name in migrations and notebooks, because that is where the second client lives.
Prove it with a matrix, not a vibe
Retrieval quality is measured with recall and judged subjectively. Permissions cannot be. They are a boolean over a cross product, and the test should look like one: a synthetic corpus where every document is labelled with the principals that may read it, and a set of principals that cover the interesting cases — owner, ordinary member, guest with a single share, outsider in another tenant, a user whose access was revoked one minute ago, and a user whose access arrives only through a nested group.
For each principal, query with text taken from documents they may read and documents they may not, at several values of k, across every retrieval path: dense, sparse, hybrid, rerank, parent expansion, summary lookup. Then assert set inclusion. Not a ratio, not a threshold — retrieved ⊆ permitted, failing the build on one violation. A permission bug that shows up as one chunk in a thousand queries is still a breach, and a metric that averages it into 0.999 is a metric that will be read as passing.
Three mutations belong in the same suite because they are the ones that ship. Revoke a document mid-run and assert the next request does not return it, including from a warm cache. Change a group membership and assert the principal hash changed. Kill the process between retrieval and generation, restart it, and assert no cached answer is served to the revoked principal. The last one is the only test that exercises the layer everybody assumes is stateless.
Finally, a canary: a document containing a unique random token, granted to no principal at all, indexed in the production collection. Then scan responses, traces, logs and exports for that token in CI. It is the retrieval equivalent of a secret scanner, it costs nothing to maintain, and it catches exactly the path that a per-path test matrix forgets — which is always the newest one.
Symptoms and where they actually live
| Symptom | Where to look first |
|---|---|
| An answer cites a document from another workspace | A retrieval path without the tenant and ACL predicate — usually sparse search, parent expansion or a summary table |
| "It cannot find my documents" for one user, fine for everyone else | Post-filter truncation: candidate pool too small for that principal's visible fraction |
| Filtered queries got slower after the ACL rollout | Filter column without a supporting index, or the planner falling back to a full scan on a very broad filter |
| Access was revoked and the assistant still answers | Result or prompt cache without the principal and epoch in the key |
| Answers degrade gradually as the corpus grows | Fixed candidate pool meeting a falling visible fraction; recall loss without an error |
| One tenant reports wildly inconsistent answer quality | Permission model with a deeply nested group structure, producing very wide or very narrow filters |
What to instrument
Four numbers make this class of bug visible before a customer finds it. Filter selectivity per query, as matched rows over collection size — when it drops below a fraction you can name, the retrieval budget is wrong. Candidates scanned per returned row — the ratio tells you how much of the graph is being walked to satisfy a permission predicate. Empty-result rate grouped by principal — a spike for one user or one tenant is a resolution bug, not a content gap. And the count of documents rejected by the post-retrieval re-check, which must be zero; anything else means one of the upstream paths let something through and the second check is doing the work the first one was supposed to do.
None of this is a retrieval-quality problem, which is why it gets neglected. Recall is visible in the answers and gets tuned; authorisation is invisible until it is a disclosure, and then it is not a tuning problem at all. The fix is a filter inside the query, an identity in the cache key, a re-check at the point of use and a canary that fails the build — four mechanisms for one property, because a boundary defended in one place is a boundary defended by whoever last edited that place. If you are adding permissions to a retrieval system that was built without them, send us the shape of your corpus and your permission model; it is ordinary work in our AI engineering practice, and it touches deletion, tenant isolation and tracing before it is finished.