Spaces:
Running
Running
Download guide.html from pgg-engineering/rankfence: direct link, hf CLI and curl.
- Browser
- Download file 19.3 kB
-
https://huggingface.co/spaces/pgg-engineering/rankfence/resolve/main/guide.html
- Command line
-
hf download hf://spaces/pgg-engineering/rankfence/guide.html
-
curl -L -o guide.html https://huggingface.co/spaces/pgg-engineering/rankfence/resolve/main/guide.html
19.3 kB
| <html lang="en"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1"><link rel="stylesheet" href="docs.css"><title>RankFence guide</title></head><body><header><a class="brand" href="index.html">RankFence</a><nav><a href="workbench.html">Try your rankings</a><a href="https://huggingface.co/spaces/pgg-engineering/rankfence/resolve/main/rankfence-0.1.0.zip">Source .zip</a></nav></header><article><h1 id="rankfence">RankFence</h1> | |
| <p><strong>Stop fetching from search shards once the exact winners are settled.</strong> RankFence coordinates paged lexical, vector or agent retrievers. Tell it which sources can return the same document. It uses that information to rule out candidates, return the global top k and close unfinished streams. Python 3.10+, MIT, no runtime dependencies.</p> | |
| <p><a href="https://github.com/pgg898200-ship-it/streamsieve/tree/rankfence-v0.1.0">GitHub source</a> · <a href="https://pgg-engineering-rankfence.static.hf.space">Explore the replay</a> · <a href="https://pgg-engineering-rankfence.static.hf.space/workbench.html">Test your own shard map locally in a browser</a> · <a href="https://huggingface.co/spaces/pgg-engineering/rankfence/resolve/main/rankfence-0.1.0-py3-none-any.whl">Download the wheel</a> · <a href="benchmarks.html">Benchmark methodology and raw results</a></p> | |
| <h2 id="why-give-the-coordinator-a-shard-map">Why give the coordinator a shard map?</h2> | |
| <p>Consider eight indexes, each with four ranked retrievers. An item in shard A can collect four reciprocal-rank contributions. A coordinator that only knows there are 32 open streams must allow that item to appear in the other 28. Declaring the disjoint ID namespaces tightens the highest score every challenger could still reach. A complete ranking from any individual shard can also require more pages than the global consumer needs.</p> | |
| <p>On 100 TREC Robust03 topics, we hash-partitioned each original ranking into eight shards and recomputed ranks within each shard. All methods solved the same resulting 32-stream queries and produced the same ordered top five. This is a simulation of a sharded service on historical retrieval results. The ranking contract uses the position in each component cursor. Repartitioning can change those positions and the answer. Systems that require invariance under resharding need globally comparable scores or global per-retriever ranks. <a href="https://github.com/ai-pipestream/protomolt-search">Example of a global-rank contract</a></p> | |
| <table> | |
| <thead> | |
| <tr class="header"> | |
| <th>Method</th> | |
| <th style="text-align: right;">Ranked IDs consumed across 100 topics</th> | |
| </tr> | |
| </thead> | |
| <tbody> | |
| <tr class="odd"> | |
| <td>Full-window fusion</td> | |
| <td style="text-align: right;">400,000</td> | |
| </tr> | |
| <tr class="even"> | |
| <td>RankSeal on all 32 streams</td> | |
| <td style="text-align: right;">400,000</td> | |
| </tr> | |
| <tr class="odd"> | |
| <td>RankSeal in each shard, then merge resolved scores</td> | |
| <td style="text-align: right;">206,934</td> | |
| </tr> | |
| <tr class="even"> | |
| <td>RankFence with the shard map</td> | |
| <td style="text-align: right;">82,637</td> | |
| </tr> | |
| <tr class="odd"> | |
| <td><strong>RankFence with the shard map and individual shard retirement</strong></td> | |
| <td style="text-align: right;"><strong>78,956</strong></td> | |
| </tr> | |
| <tr class="even"> | |
| <td>Independent integer no-random-access algorithm with the same shard map</td> | |
| <td style="text-align: right;">82,637</td> | |
| </tr> | |
| </tbody> | |
| </table> | |
| <p>With shard retirement enabled, the coordinator read <strong>61.84% fewer IDs</strong> than the local RankSeal merge. All 100 answers matched exhaustive fusion. The lower bound comes from known document ownership. A sufficiently capable competing implementation can use the same information.</p> | |
| <p>The RankFence implementation also supports unpartitioned streams, membership-only proofs and an independent receipt verifier. RankSeal supports weighted inputs and a two-source hindsight benchmark. For mutable rankings with replacements and withdrawals, see the PGG RankPatch project. Stable ranked pages are required here.</p> | |
| <h2 id="install-and-try">Install and try</h2> | |
| <div class="sourceCode" id="cb1"><pre class="sourceCode sh"><code class="sourceCode bash"><span id="cb1-1"><a href="#cb1-1" aria-hidden="true" tabindex="-1"></a><span class="ex">python</span> <span class="at">-m</span> pip install https://huggingface.co/spaces/pgg-engineering/rankfence/resolve/main/rankfence-0.1.0-py3-none-any.whl</span></code></pre></div> | |
| <div class="sourceCode" id="cb2"><pre class="sourceCode python"><code class="sourceCode python"><span id="cb2-1"><a href="#cb2-1" aria-hidden="true" tabindex="-1"></a><span class="im">from</span> rankfence.partition <span class="im">import</span> PartitionFence</span> | |
| <span id="cb2-2"><a href="#cb2-2" aria-hidden="true" tabindex="-1"></a></span> | |
| <span id="cb2-3"><a href="#cb2-3" aria-hidden="true" tabindex="-1"></a>fence <span class="op">=</span> PartitionFence([<span class="st">"west"</span>, <span class="st">"west"</span>, <span class="st">"east"</span>, <span class="st">"east"</span>], k<span class="op">=</span><span class="dv">1</span>)</span> | |
| <span id="cb2-4"><a href="#cb2-4" aria-hidden="true" tabindex="-1"></a>fence.push(<span class="dv">0</span>, [<span class="st">"west/alpha"</span>, <span class="st">"west/beta"</span>])</span> | |
| <span id="cb2-5"><a href="#cb2-5" aria-hidden="true" tabindex="-1"></a>fence.push(<span class="dv">1</span>, [<span class="st">"west/alpha"</span>, <span class="st">"west/gamma"</span>])</span> | |
| <span id="cb2-6"><a href="#cb2-6" aria-hidden="true" tabindex="-1"></a>fence.push(<span class="dv">2</span>, [<span class="st">"east/delta"</span>, <span class="st">"east/epsilon"</span>])</span> | |
| <span id="cb2-7"><a href="#cb2-7" aria-hidden="true" tabindex="-1"></a>fence.push(<span class="dv">3</span>, [<span class="st">"east/epsilon"</span>, <span class="st">"east/delta"</span>])</span> | |
| <span id="cb2-8"><a href="#cb2-8" aria-hidden="true" tabindex="-1"></a><span class="cf">assert</span> fence.ordered() <span class="op">==</span> (<span class="st">"west/alpha"</span>,)</span> | |
| <span id="cb2-9"><a href="#cb2-9" aria-hidden="true" tabindex="-1"></a><span class="cf">assert</span> fence.done <span class="op">==</span> (<span class="va">False</span>, <span class="va">False</span>, <span class="va">False</span>, <span class="va">False</span>)</span></code></pre></div> | |
| <p>For flat fusion use <code>Fence(sources=4, k=1)</code>. Both classes accept IDs in one-based rank order. Use <code>members()</code> if a downstream reranker just needs the winning set. Either call returns <code>None</code> while its requested property is uncertain. A certified ID can still gain score.</p> | |
| <h2 id="use-live-retrieval-cursors">Use live retrieval cursors</h2> | |
| <div class="sourceCode" id="cb3"><pre class="sourceCode python"><code class="sourceCode python"><span id="cb3-1"><a href="#cb3-1" aria-hidden="true" tabindex="-1"></a><span class="im">from</span> rankfence <span class="im">import</span> Page, fuse_partitioned, verify</span> | |
| <span id="cb3-2"><a href="#cb3-2" aria-hidden="true" tabindex="-1"></a></span> | |
| <span id="cb3-3"><a href="#cb3-3" aria-hidden="true" tabindex="-1"></a><span class="cf">async</span> <span class="kw">def</span> ranked_pages(cursor):</span> | |
| <span id="cb3-4"><a href="#cb3-4" aria-hidden="true" tabindex="-1"></a> <span class="cf">try</span>:</span> | |
| <span id="cb3-5"><a href="#cb3-5" aria-hidden="true" tabindex="-1"></a> <span class="cf">async</span> <span class="cf">for</span> ids, is_last <span class="kw">in</span> cursor:</span> | |
| <span id="cb3-6"><a href="#cb3-6" aria-hidden="true" tabindex="-1"></a> <span class="cf">yield</span> Page(ids, done<span class="op">=</span>is_last)</span> | |
| <span id="cb3-7"><a href="#cb3-7" aria-hidden="true" tabindex="-1"></a> <span class="cf">finally</span>:</span> | |
| <span id="cb3-8"><a href="#cb3-8" aria-hidden="true" tabindex="-1"></a> <span class="cf">await</span> cursor.aclose()</span> | |
| <span id="cb3-9"><a href="#cb3-9" aria-hidden="true" tabindex="-1"></a></span> | |
| <span id="cb3-10"><a href="#cb3-10" aria-hidden="true" tabindex="-1"></a><span class="cf">async</span> <span class="kw">def</span> top_documents(cursors, shard_names):</span> | |
| <span id="cb3-11"><a href="#cb3-11" aria-hidden="true" tabindex="-1"></a> result <span class="op">=</span> <span class="cf">await</span> fuse_partitioned(</span> | |
| <span id="cb3-12"><a href="#cb3-12" aria-hidden="true" tabindex="-1"></a> [ranked_pages(cur) <span class="cf">for</span> cur <span class="kw">in</span> cursors], shard_names, k<span class="op">=</span><span class="dv">5</span>, receipt<span class="op">=</span><span class="va">True</span></span> | |
| <span id="cb3-13"><a href="#cb3-13" aria-hidden="true" tabindex="-1"></a> )</span> | |
| <span id="cb3-14"><a href="#cb3-14" aria-hidden="true" tabindex="-1"></a> <span class="cf">assert</span> verify(result.receipt, source_groups<span class="op">=</span>shard_names)</span> | |
| <span id="cb3-15"><a href="#cb3-15" aria-hidden="true" tabindex="-1"></a> <span class="cf">return</span> result.ids</span></code></pre></div> | |
| <p>Open one consistent query snapshot per producer. The sources may finish at different times. The adapter owns each iterator, keeps at most one page request per source in flight, propagates source failures, and closes iterators when done. The partitioned adapter retires losing shard cursors as soon as their strongest possible candidate falls below the current global cutoff. Retired cursors keep their true EOF flags. Pass <code>prune=False</code> to disable the extra checks. Remote work stops only if the provider supports cancellation or lazy production. For flat rankings, <code>await fuse(sources, k=5, receipt=True)</code> uses the same page interface.</p> | |
| <p>The <a href="https://github.com/pgg898200-ship-it/streamsieve/blob/rankfence-v0.1.0/examples/sqlite_fts.py">SQLite FTS5 example</a> builds a real full-text index of the installed Python standard library, ranks matches with three BM25 configurations, closes its cursors at certification, and checks the answer against exhaustive RRF. On our Python 3.12 corpus, the default query read 72 of 1,605 result rows. Run <code>python examples/sqlite_fts.py --shards 4</code> for four actual FTS5 tables and twelve ranked cursors. The query <code>file</code> read 96 of 1,605 rows and returned the same top three as exhaustive shard-local-rank fusion. Across seven illustrative searches, the partitioned coordinator consumed 612 rows against 900 for independently solving each shard and merging its exact scores. These counts measure rows fetched from component retrievers. A backend that natively returns fused, scored shard winners has a different interface. SQLite can score and sort matches before the client reads its first page.</p> | |
| <h2 id="let-an-agent-measure-its-own-workload">Let an agent measure its own workload</h2> | |
| <p>After installing the wheel, configure a local MCP server using the Python interpreter that contains RankFence. A typical stdio client accepts this entry.</p> | |
| <div class="sourceCode" id="cb4"><pre class="sourceCode json"><code class="sourceCode json"><span id="cb4-1"><a href="#cb4-1" aria-hidden="true" tabindex="-1"></a><span class="fu">{</span><span class="dt">"mcpServers"</span><span class="fu">:{</span><span class="dt">"rankfence"</span><span class="fu">:{</span><span class="dt">"command"</span><span class="fu">:</span><span class="st">"python"</span><span class="fu">,</span><span class="dt">"args"</span><span class="fu">:</span><span class="ot">[</span><span class="st">"-m"</span><span class="ot">,</span><span class="st">"rankfence.mcp"</span><span class="ot">]</span><span class="fu">}}}</span></span></code></pre></div> | |
| <p>The server exposes two local tools. <code>rankfence_analyze</code> replays a small complete query and checks the answer against exhaustive fusion. <code>rankfence_verify</code> checks a receipt against the supplied trusted topology. A retrieval agent can use them to decide whether early stopping pays on its own ranked lists. <a href="https://pgg-engineering-rankfence.static.hf.space/workbench.html">Browser workbench</a></p> | |
| <h2 id="verify-a-receipt-or-explain-a-wait">Verify a receipt or explain a wait</h2> | |
| <p>A receipt contains the actual prefixes, EOF assertions, ranking parameters and returned IDs. The verifier rebuilds exact rational score intervals independently. For a partitioned receipt, compare its source groups to your own trusted index topology. An upstream source must vouch for rank order, EOF and namespace isolation.</p> | |
| <p>The command line reads a stream of newline-delimited JSON pages. This example produces a flat receipt and checks it in a second process.</p> | |
| <div class="sourceCode" id="cb5"><pre class="sourceCode sh"><code class="sourceCode bash"><span id="cb5-1"><a href="#cb5-1" aria-hidden="true" tabindex="-1"></a><span class="bu">printf</span> <span class="st">'%s\n'</span> <span class="st">'{"source":0,"ids":["alpha"]}'</span> <span class="st">'{"source":1,"ids":["alpha"]}'</span> <span class="kw">|</span></span> | |
| <span id="cb5-2"><a href="#cb5-2" aria-hidden="true" tabindex="-1"></a> <span class="ex">python</span> <span class="at">-m</span> rankfence fuse <span class="at">--sources</span> 2 <span class="at">-k</span> 1 <span class="at">--receipt</span> <span class="kw">|</span></span> | |
| <span id="cb5-3"><a href="#cb5-3" aria-hidden="true" tabindex="-1"></a> <span class="ex">python</span> <span class="at">-m</span> rankfence verify</span></code></pre></div> | |
| <p>For a flat stream that has not settled, <code>counterexample(prefixes, done, k)</code> can construct two valid completions that change the winners or their order. It returns the hypothetical suffixes and exact answer for each. A missing counterexample is inconclusive.</p> | |
| <p>In a 32-source synthetic asyncio example, the coordinator closed all 29 losing cursors before the deciding page arrived. Median aggregate time that losing cursors stayed open fell from 1,193 to 8.85 cursor-ms across seven alternating runs. Median answer latency stayed near 41 ms. <a href="https://github.com/pgg898200-ship-it/streamsieve/blob/rankfence-v0.1.0/examples/retirement.py">Run the cancellation example</a> · <a href="results/retirement.json">Raw samples</a></p> | |
| <h2 id="input-contract">Input contract</h2> | |
| <table> | |
| <thead> | |
| <tr class="header"> | |
| <th>Field</th> | |
| <th>Requirement</th> | |
| </tr> | |
| </thead> | |
| <tbody> | |
| <tr class="odd"> | |
| <td>Rankings</td> | |
| <td>Immutable, append-only, unique string IDs within each source. IDs can repeat across retrievers in the same shard.</td> | |
| </tr> | |
| <tr class="even"> | |
| <td>Source groups</td> | |
| <td>Every occurrence of a document, present and future, belongs to one group. Use one group when uncertain. Qualifying IDs by shard is correct only for genuinely distinct documents.</td> | |
| </tr> | |
| <tr class="odd"> | |
| <td>Scores</td> | |
| <td>Standard unweighted RRF, sum of <code>1/(c + rank)</code> for each observed rank. Rank starts at 1, <code>c</code> defaults to 60, absent IDs contribute zero.</td> | |
| </tr> | |
| <tr class="even"> | |
| <td>Ties</td> | |
| <td>Ascending Python string order, including exact rational ties.</td> | |
| </tr> | |
| <tr class="odd"> | |
| <td>End of stream</td> | |
| <td>A true final page or natural exhaustion. A timeout, dropped packet or partial error does not establish EOF.</td> | |
| </tr> | |
| <tr class="even"> | |
| <td>Output</td> | |
| <td>Ordered tuple or winner set, possibly shorter than k once the available documents are exhausted.</td> | |
| </tr> | |
| <tr class="odd"> | |
| <td>Execution</td> | |
| <td>Serialize updates to a single query coordinator. Memory grows with the prefixes retained. Duplicate IDs in a page or the same source are rejected.</td> | |
| </tr> | |
| </tbody> | |
| </table> | |
| <p>For an unobserved term from an open stream of depth d, the maximum contribution is <code>1/(c+d+1)</code>. For a seen document, sum only possible missing terms in its shard. For a document not yet seen anywhere, take the largest possible score across shards. Compare these upper bounds against the known scores of the winners. The implementation groups candidates by the sources that have reported them, uses integer intervals for fast separation and resolves close score comparisons using exact rationals. These are applications of established top-k aggregation methods. <a href="https://www.cs.ubc.ca/~laks/cpsc504/fagin-jcss03.pdf">Fagin, Lotem and Naor</a> · <a href="https://www.cs.uoi.gr/~nikos/lara_tods.pdf">Mamoulis et al.</a></p> | |
| <h2 id="when-do-fewer-pages-improve-latency">When do fewer pages improve latency?</h2> | |
| <p>Measure the price of fetching the next page in your own stack. In our four-producer TCP replay, RankFence reached the median answer in 45.5 ms against 106.4 ms for one-shot retrieval when the producers incurred an imposed 100 μs per returned hit and 1 ms per request. Without added delays one-shot won, and one-shot retained the best p95 in all three tested scenarios. Flat fusion skipped 79.21% of entries across all 100 Robust03 queries and 73.70% on 102 NFCorpus queries. An independent exact streaming algorithm stopped at the same page on every query.</p> | |
| <p>A second test deliberately made the rankers disagree. On three synthetic 32-source queries the unpartitioned coordinator had to read all 65,536 entries and a warm exact integer batch was faster. Use full-window fusion for cheap materialized results when your end-to-end measurements favor it. Growing a top-k ANN request can change earlier IDs; supply a stable cursor or a materialized snapshot.</p> | |
| <h2 id="reproduce">Reproduce</h2> | |
| <p>From an unpacked source release:</p> | |
| <div class="sourceCode" id="cb6"><pre class="sourceCode sh"><code class="sourceCode bash"><span id="cb6-1"><a href="#cb6-1" aria-hidden="true" tabindex="-1"></a><span class="ex">python</span> <span class="at">-m</span> unittest discover <span class="at">-v</span></span> | |
| <span id="cb6-2"><a href="#cb6-2" aria-hidden="true" tabindex="-1"></a><span class="ex">python</span> examples/sqlite_fts.py</span> | |
| <span id="cb6-3"><a href="#cb6-3" aria-hidden="true" tabindex="-1"></a><span class="ex">python</span> bench.py</span> | |
| <span id="cb6-4"><a href="#cb6-4" aria-hidden="true" tabindex="-1"></a><span class="ex">python</span> benchmarks/fetch_replay.py robust03 /tmp/robust03.json</span> | |
| <span id="cb6-5"><a href="#cb6-5" aria-hidden="true" tabindex="-1"></a><span class="ex">python</span> bench.py <span class="at">--trace</span> /tmp/robust03.json <span class="at">--page</span> 32 <span class="at">--repeats</span> 3</span> | |
| <span id="cb6-6"><a href="#cb6-6" aria-hidden="true" tabindex="-1"></a><span class="ex">python</span> examples/partition_replay.py <span class="at">--help</span></span></code></pre></div> | |
| <p>The raw reports record input counts, environment and timings, including the methods that won. There is no hosted ingestion service or API key. Receipts can contain private document IDs; the verifier runs locally. <a href="benchmarks.html">Full benchmark report</a></p> | |
| </article></body></html> |