| 1 | """On "X control my computer", results of the capability-only searches reach the report. |
| 2 | |
| 3 | Posts naming the host match several searches and outrank a general tool such as |
| 4 | an MCP server on fused rank, and the reranker capped anything that never names |
| 5 | the host. Playwright MCP and similar tools were retrieved, then cut. |
| 6 | """ |
| 7 | |
| 8 | from __future__ import annotations |
| 9 | |
| 10 | from lib import fusion, planner, rerank, schema |
| 11 | |
| 12 | TOPIC = "best Grok AI bot to control my computer agent" |
| 13 | SOURCES = ["reddit", "grounding", "github"] |
| 14 | |
| 15 | |
| 16 | def _candidate(cid: str, title: str, labels: list[str], rrf: float) -> schema.Candidate: |
| 17 | item = schema.SourceItem( |
| 18 | item_id=cid, |
| 19 | source="grounding", |
| 20 | title=title, |
| 21 | body=title, |
| 22 | url=f"https://e.com/{cid}", |
| 23 | ) |
| 24 | return schema.Candidate( |
| 25 | candidate_id=cid, |
| 26 | item_id=cid, |
| 27 | source="grounding", |
| 28 | title=title, |
| 29 | url=f"https://e.com/{cid}", |
| 30 | snippet=title, |
| 31 | subquery_labels=labels, |
| 32 | native_ranks={"grounding": 1}, |
| 33 | local_relevance=0.6, |
| 34 | freshness=50, |
| 35 | engagement=None, |
| 36 | source_quality=0.6, |
| 37 | rrf_score=rrf, |
| 38 | sources=["grounding"], |
| 39 | source_items=[item], |
| 40 | ) |
| 41 | |
| 42 | |
| 43 | def _plan(topic: str = TOPIC) -> schema.QueryPlan: |
| 44 | return planner._fallback_plan(topic, SOURCES, None, "default") |
| 45 | |
| 46 | |
| 47 | def test_plan_names_the_host_and_its_capability_searches(): |
| 48 | plan = _plan() |
| 49 | assert "enablement-host:grok" in plan.notes |
| 50 | assert fusion.capability_labels(plan) == {"capability-1", "capability-2"} |
| 51 | |
| 52 | |
| 53 | def test_topic_without_a_named_host_has_no_capability_searches(): |
| 54 | plan = _plan("agent that automates the desktop") |
| 55 | assert "enablement" in plan.notes |
| 56 | assert fusion.capability_labels(plan) == set() |
| 57 | |
| 58 | |
| 59 | def test_ordinary_topic_has_no_capability_searches(): |
| 60 | assert fusion.capability_labels(_plan("best wireless earbuds")) == set() |
| 61 | |
| 62 | |
| 63 | def test_pool_keeps_capability_results_that_rank_last(): |
| 64 | host_posts = [ |
| 65 | _candidate(f"grok-{i}", f"Grok Bot post {i}", ["primary"], 1.0 - i / 100) |
| 66 | for i in range(10) |
| 67 | ] |
| 68 | tools = [ |
| 69 | _candidate(f"tool-{i}", f"MCP server {i}", ["capability-1"], 0.01) |
| 70 | for i in range(6) |
| 71 | ] |
| 72 | pool = fusion._diversify_pool( |
| 73 | host_posts + tools, 8, capability={"capability-1"}, host="grok" |
| 74 | ) |
| 75 | kept = {c.candidate_id for c in pool} |
| 76 | # One source: at most three reserved slots. |
| 77 | assert {"tool-0", "tool-1", "tool-2"} <= kept |
| 78 | assert "tool-5" not in kept |
| 79 | |
| 80 | |
| 81 | def _sourced(cid: str, title: str, source: str, rrf: float) -> schema.Candidate: |
| 82 | candidate = _candidate(cid, title, ["capability-1"], rrf) |
| 83 | candidate.source = source |
| 84 | return candidate |
| 85 | |
| 86 | |
| 87 | def test_capability_slots_are_shared_across_sources(): |
| 88 | # Social posts outrank tool pages on fused rank; the tool pages still get slots. |
| 89 | posts = [ |
| 90 | _sourced(f"post-{i}", f"AI controls your desktop {i}", "reddit", 0.5 - i / 100) |
| 91 | for i in range(6) |
| 92 | ] |
| 93 | pages = [ |
| 94 | _sourced(f"page-{i}", f"MCP server {i}", "grounding", 0.1 - i / 100) |
| 95 | for i in range(3) |
| 96 | ] |
| 97 | reserved = fusion._capability_reservation(posts + pages) |
| 98 | assert [c.candidate_id for c in reserved] == [ |
| 99 | "post-0", |
| 100 | "page-0", |
| 101 | "post-1", |
| 102 | "page-1", |
| 103 | "post-2", |
| 104 | "page-2", |
| 105 | ] |
| 106 | |
| 107 | |
| 108 | def test_pool_reserves_no_slot_for_a_capability_search_post_that_names_the_host(): |
| 109 | # Reddit and other sources search the raw topic whatever the subquery says, |
| 110 | # so a capability label also tags posts about the host. |
| 111 | host_posts = [ |
| 112 | _candidate(f"grok-{i}", f"Grok Bot post {i}", ["primary"], 1.0 - i / 100) |
| 113 | for i in range(10) |
| 114 | ] |
| 115 | tagged = [ |
| 116 | _candidate(f"tagged-{i}", f"Grok Bot setup {i}", ["capability-1"], 0.02) |
| 117 | for i in range(4) |
| 118 | ] |
| 119 | tools = [ |
| 120 | _candidate(f"tool-{i}", f"MCP server {i}", ["capability-1"], 0.01) |
| 121 | for i in range(3) |
| 122 | ] |
| 123 | pool = fusion._diversify_pool( |
| 124 | host_posts + tagged + tools, 8, capability={"capability-1"}, host="grok" |
| 125 | ) |
| 126 | kept = {c.candidate_id for c in pool} |
| 127 | assert {"tool-0", "tool-1", "tool-2"} <= kept |
| 128 | assert not any(cid.startswith("tagged-") for cid in kept) |
| 129 | |
| 130 | |
| 131 | def test_pool_reserves_no_slot_for_out_of_window_capability_results(): |
| 132 | host_posts = [ |
| 133 | _candidate(f"grok-{i}", f"Grok Bot post {i}", ["primary"], 1.0 - i / 100) |
| 134 | for i in range(10) |
| 135 | ] |
| 136 | stale = _candidate("stale", "MCP server stale", ["capability-1"], 0.02) |
| 137 | stale.source_items[0].published_at = "2024-01-01" |
| 138 | stale.metadata = {"range_from": "2026-10-10", "range_to": "2026-11-09"} |
| 139 | fresh = _candidate("fresh", "MCP server fresh", ["capability-1"], 0.01) |
| 140 | pool = fusion._diversify_pool( |
| 141 | host_posts + [stale, fresh], 8, capability={"capability-1"}, host="grok" |
| 142 | ) |
| 143 | kept = {c.candidate_id for c in pool} |
| 144 | assert "fresh" in kept |
| 145 | assert "stale" not in kept |
| 146 | |
| 147 | |
| 148 | def test_pool_without_capability_searches_is_unchanged(): |
| 149 | host_posts = [ |
| 150 | _candidate(f"grok-{i}", f"Grok Bot post {i}", ["primary"], 1.0 - i / 100) |
| 151 | for i in range(10) |
| 152 | ] |
| 153 | tools = [ |
| 154 | _candidate(f"tool-{i}", f"MCP server {i}", ["capability-1"], 0.01) |
| 155 | for i in range(6) |
| 156 | ] |
| 157 | pool = fusion._diversify_pool(host_posts + tools, 8) |
| 158 | assert not any(c.candidate_id.startswith("tool-") for c in pool) |
| 159 | |
| 160 | |
| 161 | def test_capability_result_escapes_the_entity_cap(): |
| 162 | plan = _plan() |
| 163 | tool = _candidate( |
| 164 | "tool", "Playwright MCP browser automation", ["capability-1"], 0.01 |
| 165 | ) |
| 166 | other = _candidate("other", "Playwright MCP browser automation", ["primary"], 0.01) |
| 167 | rerank._mark_capability_results([tool, other], plan) |
| 168 | entity = "Grok" |
| 169 | tool_score, tool_reason = rerank._fallback_tuple(tool, primary_entity=entity) |
| 170 | other_score, other_reason = rerank._fallback_tuple(other, primary_entity=entity) |
| 171 | assert tool_reason == "fallback-local-score (capability search)" |
| 172 | assert other_reason == "fallback-local-score (entity-miss demotion)" |
| 173 | assert tool_score > other_score |
| 174 | |
| 175 | |
| 176 | def test_judge_prompt_exempts_capability_results(): |
| 177 | plan = _plan() |
| 178 | tool = _candidate( |
| 179 | "tool", "Playwright MCP browser automation", ["capability-1"], 0.01 |
| 180 | ) |
| 181 | rerank._mark_capability_results([tool], plan) |
| 182 | prompt = rerank._build_prompt(TOPIC, plan, [tool], "Grok") |
| 183 | assert "capability_search: true" in prompt |
| 184 | assert "EXEMPT from this cap" in prompt |
| 185 | |
| 186 | |
| 187 | def test_judge_prompt_has_no_exception_without_capability_results(): |
| 188 | plan = _plan() |
| 189 | # A capability-search result that names the host is ordinary evidence. |
| 190 | host = _candidate("host", "Grok Bot update", ["capability-1"], 0.5) |
| 191 | rerank._mark_capability_results([host], plan) |
| 192 | prompt = rerank._build_prompt(TOPIC, plan, [host], "Grok") |
| 193 | assert "capability_search" not in prompt |
| 194 |