Skip to content

D156 step 2: filter search and search_documents by document tags - #614

Merged
fazpu merged 14 commits into
mainfrom
feat/document-tags-search-filter
Nov 14, 2026
Merged

fazpu merged 14 commits into
mainfrom
feat/document-tags-search-filter

Conversation

@fazpu

@fazpu fazpu commented Nov 14, 2026 •

Copy link
Copy Markdown
Member

Summary

D156 delivery step 2 (plan): filter search (claims, chunks) and search_documents by caller tags. Base: main (step 1, #606, is merged).

What this does

  • DocumentSearchFilters.tags: {all?, any?, none?} takes tag names (any case) or tag_ids. At least one list must be non-empty, no list may repeat a tag, each list holds at most 32, and every reference follows TagRef. Unregistered tags fail with unknown_tag, naming every one, before anything is searched.
  • The shared compiler core/document_filters.py tests tags on the lineage (document_tags.doc_id, one EXISTS per all tag, so the planner starts from ix_document_tags_tag). Claims, chunks, search_documents and P1 fact eligibility all share it. Read-side tag resolution locks nothing.
  • D140 same-occurrence rule, for the whole documents filter. With a time scope, a periodised claim needs one occurrence that is both selected and matching, and a fact needs one supporting occurrence that both passes the evidence gate and matches. Before, the filter and the time scope were tested independently.
  • Eligibility before ranking (§4.6).
    • A selective filter's eligible set is enumerated: chunks from the matching versions, claims from the matching lineages, facts from the matching lineages' claims through the evidence tables' claim indexes.
    • That set is scored on the existing exact tier (≤ 5,000) or pushed down (≤ 20,000, claims and chunks). Above that, ordinary nomination runs.
    • The fact tier counts distinct facts.
  • §4.5. A completed, empty, filtered claim or chunk search says whether no live document matched or the matching documents say nothing relevant. A work-bounded read stays boundary.
  • Surfaces.
    • HTTP/SDK filters.tags / documents.tags; 422 unknown_tag on POST /search/claims, POST /search/chunks and POST /documents/search.
    • MCP search_documents gets a flat tags field (tool_version 4).
    • CLI remember documents search --tag/--any-tag/--not-tag.
    • TypeScript parity, including local validation of tag shape and references.
    • openapi.json, TS contracts, D143 fixtures and the docs (website/) are updated; not-built-yet now says the context operations are not filtered yet.

Deviation from the design, agreed with the supervisor

No deployment surface searches relations or observations with a documents filter. So the §4.4 evidence rule (supporting evidence from matching documents, contradictions and co-members from everywhere marked in_documents_filter, two-sided evidence_totals) moves to step 3, in the facts_context evidence path. Step 2 builds the shared fact eligibility that step 3 consumes: P1 search_facts_scored(documents=…) with tags, the same-occurrence rule and the exact tier. The plan says so. The entity_ids combination is proven at the P1 nomination level, because no public step-2 read takes both.

Qualification

These are synthetic stores on a local PostgreSQL 19 container. What was qualified: chunk and claim search at 300k chunks (D142 sweep, benchmarks/p1_ranked/sweep.py), and fact search at 20k facts (benchmarks/p1_ranked/tag_facts.py). The 1M and 3M chunk stores could not be seeded: the PostgreSQL backend was OOM-killed (signal 9) in the shared ~10 GB Docker VM during the seed's bulk statements. Nothing was qualified at that scale.

Chunks and claims: 300k chunks

6,000 lineages × 5 versions × 10 chunks and 60k claims reused across versions. 40 BM25 queries, k = 20, public search_chunks / search_claims.

Stratum p50 p95 Outcome
chunks, unfiltered 140 ms 381 ms 40/40 k filled
chunks, tag on 1% (600 eligible) 157 ms 228 ms 40/40 exhausted via the exact tier (fetched = 600)
chunks, tag on 90% 2,067 ms 2,475 ms 40/40 k filled
claims, unfiltered 81 ms 135 ms 40/40 k filled
claims, tag on 1% 210 ms 303 ms 40/40 exhausted via the exact tier
claims, tag on 90% 1,224 ms 1,509 ms 40/40 k filled

The eligible-set probe (EXPLAIN ANALYZE) takes 78 ms for the 1% tag (600 rows), starting from ix_document_tags_tag. For the 90% tag it reads its 20,001-row cap in 2.8 s (4.05M buffer hits), then falls back to ordinary nomination. That bounded probe is the broad-filter cost.

Facts: 20k relations

One relation per lineage, random 1,536-dimension vectors, live mentions and evidence. 40 random query vectors, k = 20, P1 search_facts_scored(kind="relation").

Stratum p50 p95 Outcome
unfiltered 343 ms 392 ms 40/40 k filled
tag on 1% (200 eligible facts) 422 ms 464 ms 40/40 complete via the exact keyed branch
tag on 90% (18,000 eligible) 1,777 ms 1,880 ms 40/40 k filled

Fact enumeration probe, EXPLAIN ANALYZE, claim-keyed (review r3). Before this commit the same probe took 1.26 s at 1% and 24 s at 90%. The 63 of 64 identical per-partition index scans are elided below:

== bench:1pct
Limit   loops=1 t~38ms rows=200.0
  Limit   loops=1 t~38ms rows=200.0
    Nested Loop   loops=1 t~38ms rows=200.0
      Nested Loop   loops=1 t~18ms rows=400.0
        Aggregate   loops=1 t~15ms rows=200.0
          Nested Loop   loops=1 t~15ms rows=400.0
            Nested Loop   loops=1 t~13ms rows=400.0
              Hash Join   loops=1 t~11ms rows=400.0
                Seq Scan document_metadata  loops=1 t~7ms rows=40000.0
                Hash   loops=1 t~0ms rows=200.0
                  Index Only Scan document_tags ix_document_tags_tag loops=1 t~0ms rows=200.0
              Index Scan document_versions document_versions_pkey loops=400 t~2ms rows=1.0
            Index Scan documents documents_pkey loops=400 t~2ms rows=1.0
        Index Scan claims ix_claims_doc loops=200 t~2ms rows=2.0
      Memoize   loops=400 t~20ms rows=0.5
        Append   loops=400 t~19ms rows=0.5
          Index Scan relation_evidence_p0 relation_evidence_p0_claim_id_idx loops=400 t~0ms rows=0.01
  Aggregate   loops=1 t~0ms rows=1.0
    CTE Scan   loops=1 t~0ms rows=200.0
  Subquery Scan   loops=1 t~38ms rows=200.0
    Aggregate   loops=1 t~38ms rows=200.0
      CTE Scan   loops=1 t~38ms rows=200.0
38.719
== bench:90pct
Limit   loops=1 t~1720ms rows=5001.0
  Limit   loops=1 t~1698ms rows=18000.0
    Nested Loop   loops=1 t~1696ms rows=18000.0
      Nested Loop   loops=1 t~135ms rows=36000.0
        Aggregate   loops=1 t~62ms rows=18000.0
          Hash Join   loops=1 t~50ms rows=36000.0
            Hash Join   loops=1 t~37ms rows=36000.0
              Hash Join   loops=1 t~24ms rows=40000.0
                Seq Scan document_versions  loops=1 t~4ms rows=40000.0
                Hash   loops=1 t~10ms rows=40000.0
                  Seq Scan document_metadata  loops=1 t~4ms rows=40000.0
              Hash   loops=1 t~4ms rows=18000.0
                Seq Scan document_tags  loops=1 t~2ms rows=18000.0
            Hash   loops=1 t~6ms rows=20000.0
              Seq Scan documents  loops=1 t~3ms rows=20000.0
        Index Scan claims ix_claims_doc loops=18000 t~72ms rows=2.0
      Memoize   loops=36000 t~1548ms rows=0.5
        Append   loops=36000 t~1512ms rows=0.5
          Index Scan relation_evidence_p0 relation_evidence_p0_claim_id_idx loops=36000 t~36ms rows=0.01
  Aggregate   loops=1 t~3ms rows=1.0
    CTE Scan   loops=1 t~1ms rows=18000.0
  Subquery Scan   loops=1 t~1719ms rows=5001.0
    Aggregate   loops=1 t~1716ms rows=5001.0
      CTE Scan   loops=1 t~1704ms rows=18000.0
1721.414

Verification

  • ruff format --check and ruff check (src/, benchmarks/): clean. pyright (src/, benchmarks/): 0 errors.
  • PostgreSQL: test_document_tag_filters_pg.py (14), D134 filter, D140 scoped retrieval, D153 eligibility and storage-capture profile: 85 passed. The full workers, surfaces and adapters lanes run in CI.
  • TypeScript: typecheck, check:generated, check:parity, npm test (607 pass).
  • Review: GPT 6.1 sol (max), four rounds, approved on ac5a39f.
  • GitHub CI on ac5a39f: all jobs green. CLA was failing until this agreement box was ticked.

Contributor agreement

Signing on behalf of a legal entity (leave blank if accepting individually):

🤖 Generated with Claude Code

https://claude.ai/code/session_01VYytLf2PUQ2RVpEZYENzsq

fazpu and others added 9 commits November 14, 2026 01:35
Tags join the D134 documents filter in the shared compiler
core/document_filters.py, tested on the lineage. Names resolve to tag ids
before retrieval and an unregistered tag fails with unknown_tag. With a
D140 time scope the filter and the scope hold on the same occurrence for
claims and for a fact's supporting claims. A completed empty filtered
claim/chunk search says whether any live document matched. MCP
search_documents gains tags (tool_version 4), the CLI gains --tag,
--any-tag and --not-tag; openapi.json and TypeScript contracts regenerated.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01VYytLf2PUQ2RVpEZYENzsq
…ule, empty explanations, surfaces

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01VYytLf2PUQ2RVpEZYENzsq
…filter; qualification strata in the D142 sweep

The D142 sweep gains D156 tag strata (1% and 90% of lineages) for chunk
search and, with --claims, for claim search over one reused claim per served
chunk, plus EXPLAIN ANALYZE of the tag filter's eligible-set probe.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01VYytLf2PUQ2RVpEZYENzsq
…r parity; plan step 2 status

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01VYytLf2PUQ2RVpEZYENzsq
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01VYytLf2PUQ2RVpEZYENzsq
…dence rule

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01VYytLf2PUQ2RVpEZYENzsq
…o feat/document-tags-search-filter

# Conflicts:
#	packages/typescript-client/contracts/metadata.json
#	packages/typescript-client/src/metadata.generated.ts
#	packages/typescript-client/src/version.generated.ts
…cts exactly; TS tag-filter validator checks shapes first

Review r1 (GPT 6.1 sol) on #614:
- Claims now enumerate their eligible set from the matching lineages, as
  chunks do from the matching versions, and reuse the exact / pushdown
  tiers. Unscoped fact search under a document filter enumerates the facts
  supported from matching lineages and, up to the exact tier threshold,
  scores them through the keyed exact branch instead of the HNSW loop.
  Regression: tagged items ranked beyond the work budget are found on every
  grain, and the same read is a boundary with the tiers switched off.
- The TypeScript DocumentTagFilter invariant runs only on well-shaped lists,
  so malformed tags are an InputValidationError; parity fixtures added.
- Filter tag resolution in reads locks nothing (step 1's write-side FOR
  SHARE cannot run in a read-only snapshot).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01VYytLf2PUQ2RVpEZYENzsq
…te from the base evidence tables; facts qualification harness

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01VYytLf2PUQ2RVpEZYENzsq
@fazpu
fazpu changed the base branch from feat/document-tags-registry to main November 14, 2026 05:17
fazpu and others added 5 commits November 14, 2026 06:30
…arch-filter

# Conflicts:
#	packages/typescript-client/contracts/metadata.json
#	packages/typescript-client/src/metadata.generated.ts
#	packages/typescript-client/src/version.generated.ts
…efuses invalid tag references locally

Review r2 (GPT 6.1 sol) on #614:
- _enumerate_supported_facts deduplicates fact keys before applying
  EXACT_TIER_THRESHOLD; the support-row read is bounded at
  FILTER_PUSHDOWN_LIMIT + 1. Regression: two facts with four matching
  supports each take the exact tier behind twelve better-ranked facts.
- The TypeScript DocumentTagFilter invariant applies the TagRef rule
  (512 raw, 1-128 after NFC) through the shared isTagRef; parity fixtures
  for empty, 129- and 513-character references.
- Regenerated the storage-capture live-profile fixture after the main merge.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01VYytLf2PUQ2RVpEZYENzsq
…by claim index

Review r3 (GPT 6.1 sol) on #614: the fact enumeration joined each matching
lineage's claims to a whole partitioned evidence table, which the planner
hash-joined once per lineage (24 s at 90% of 20k facts). Each claim now
probes relation_evidence and observation_evidence through their claim
indexes in an OFFSET 0 LATERAL: 39 ms at 1% and 1.7 s at 90% on the same
store (EXPLAIN ANALYZE in the PR).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01VYytLf2PUQ2RVpEZYENzsq
…arch-filter

# Conflicts:
#	src/tests/fixtures/storage_capture/live_corpus_profile.json
#	src/tests/surfaces/test_d143_defaults_unchanged.py
@fazpu
fazpu marked this pull request as ready for review November 14, 2026 20:55
@fazpu
fazpu merged commit a2508c4 into main Nov 14, 2026
19 checks passed
fazpu added a commit that referenced this pull request Nov 14, 2026
Release coordinates from origin/main (after #614), raised to a minor:
PostgreSQL 18 becomes the reference server (D157); corpus folders and
document tags ship. Changelog records 0.21.0 and labels the 0.20.0 entry.
RELEASING.md: the pypi environment no longer has a required reviewer.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01AXAuJJ6Tswr9Q3Auc1VPKb
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.

1 participant