| 1 | """Host-contract tests for non-modal agent runtimes.""" |
| 2 | |
| 3 | from __future__ import annotations |
| 4 | |
| 5 | import re |
| 6 | from pathlib import Path |
| 7 | |
| 8 | from tests.skill_contract import contract_documents, reference_text |
| 9 | |
| 10 | ROOT = Path(__file__).resolve().parents[1] |
| 11 | SKILL_MD = ROOT / "skills" / "last30days" / "SKILL.md" |
| 12 | |
| 13 | |
| 14 | def _prose_flow() -> str: |
| 15 | text = reference_text("setup-wizard") |
| 16 | start_marker = "### Non-Modal Prose Flow" |
| 17 | # The Grok Bot Prose Flow (third Step 0 branch) sits between the prose |
| 18 | # flow and the Manual Setup Guide; slice the prose flow alone. |
| 19 | end_marker = "### Grok Bot Prose Flow" |
| 20 | start = text.find(start_marker) |
| 21 | assert start != -1, f"missing section marker: {start_marker}" |
| 22 | end = text.find(end_marker, start) |
| 23 | assert end != -1, f"missing section marker: {end_marker}" |
| 24 | return text[start:end] |
| 25 | |
| 26 | |
| 27 | def test_non_modal_hosts_are_named(): |
| 28 | prose = _prose_flow() |
| 29 | for host in ("Codex", "Cursor", "Gemini CLI", "raw CLI"): |
| 30 | assert host in prose |
| 31 | |
| 32 | |
| 33 | def test_non_modal_cookie_consent_uses_engine_allow_flag(): |
| 34 | prose = _prose_flow() |
| 35 | consent = prose.index("Cookie consent") |
| 36 | allow = prose.index("setup --allow-browser-cookies") |
| 37 | decline = prose.index("FROM_BROWSER=off") |
| 38 | assert consent < allow |
| 39 | assert consent < decline |
| 40 | |
| 41 | |
| 42 | def test_non_modal_does_not_require_preflight_before_cookie_consent(): |
| 43 | prose = _prose_flow() |
| 44 | consent = prose.index("Cookie consent") |
| 45 | assert "--preflight" not in prose |
| 46 | assert consent > prose.index("Welcome") |
| 47 | |
| 48 | |
| 49 | def test_non_modal_completion_mentions_project_trust(): |
| 50 | prose = _prose_flow() |
| 51 | assert "LAST30DAYS_TRUST_PROJECT_CONFIG=1" in prose |
| 52 | assert "Codex desktop" in prose |
| 53 | |
| 54 | |
| 55 | def _step0_search_contract() -> str: |
| 56 | text = SKILL_MD.read_text(encoding="utf-8") |
| 57 | start_marker = "For fresh research or discovery, resolve" |
| 58 | end_marker = "**GROK BOT HOST RULE" |
| 59 | start = text.find(start_marker) |
| 60 | assert start != -1, f"missing section marker: {start_marker}" |
| 61 | end = text.find(end_marker, start) |
| 62 | assert end != -1, f"missing section marker: {end_marker}" |
| 63 | return text[start:end] |
| 64 | |
| 65 | |
| 66 | def test_host_web_search_uses_available_capability_not_specific_tool_name(): |
| 67 | step0 = _step0_search_contract() |
| 68 | assert "usable web-search tool" in step0 |
| 69 | assert "including a deferred or connector-provided tool" in step0 |
| 70 | assert "loading, selecting, or enabling that tool" in step0 |
| 71 | assert "do so through the host's mechanism before use" in step0 |
| 72 | assert "available capability rather than requiring one particular tool name or schema" in step0 |
| 73 | |
| 74 | |
| 75 | def test_no_host_search_uses_auto_resolve_and_leaves_native_signal_unset(): |
| 76 | step0 = _step0_search_contract() |
| 77 | assert "When no host web search is available" in step0 |
| 78 | assert "--auto-resolve" in step0 |
| 79 | assert "LAST30DAYS_NATIVE_SEARCH=1" in step0 |
| 80 | assert "leave that signal unset" in step0 |
| 81 | |
| 82 | |
| 83 | def test_hosted_named_entity_uses_server_planning(): |
| 84 | root = SKILL_MD.read_text(encoding="utf-8") |
| 85 | law7 = root.split("**LAW 7 -", 1)[1].split("**LAW 8 -", 1)[0] |
| 86 | assert "LOCAL NAMED-ENTITY TOPICS" in law7 |
| 87 | assert "`research_backend=hosted`" in law7 |
| 88 | assert "the API plans server-side" in law7 |
| 89 | assert "On local engine paths with host WebSearch" in root |
| 90 | assert "For person topics on local engine paths" in root |
| 91 | |
| 92 | runbook = reference_text("research-runbook") |
| 93 | assert "--diagnose --include-research-backend" in runbook.split("### Hosted API route", 1)[0] |
| 94 | hosted = runbook.split("### Hosted API route", 1)[1].split("### Local engine route", 1)[0] |
| 95 | assert "skip Steps 0.5 through 0.75" in hosted |
| 96 | assert "without `--plan`, `--auto-resolve`" in hosted |
| 97 | execution = runbook.split("### PRECONDITION GATE", 1)[1].split("**Step 1:", 1)[0] |
| 98 | assert "For local engine research" in execution |
| 99 | |
| 100 | |
| 101 | def test_hosted_route_classifies_natural_language_html_before_submission(): |
| 102 | root = SKILL_MD.read_text(encoding="utf-8") |
| 103 | html = root.split("## HTML and follow-up handoff", 1)[1].split("## Security", 1)[0] |
| 104 | assert "Classify this intent before submitting fresh research" in html |
| 105 | assert "a natural-language request for an HTML brief" in html |
| 106 | |
| 107 | runbook = reference_text("research-runbook") |
| 108 | hosted = runbook.split("### Hosted API route", 1)[1].split("### Local engine route", 1)[0] |
| 109 | command = hosted.index("first Step 1 command") |
| 110 | assert hosted.index("Stop before submitting a hosted search") < command |
| 111 | assert hosted.index("HTML/export intent") < command |
| 112 | assert hosted.index("give me a shareable HTML brief") < command |
| 113 | assert hosted.index("Ask whether to run locally") < command |
| 114 | |
| 115 | |
| 116 | def test_comparison_modes_route_locally_before_planning_or_submission(): |
| 117 | root = SKILL_MD.read_text(encoding="utf-8") |
| 118 | comparison_route = next( |
| 119 | line for line in root.splitlines() if line.startswith("| Explicit comparison intent |") |
| 120 | ) |
| 121 | competitors_route = next( |
| 122 | line for line in root.splitlines() if line.startswith("| `--competitors` mode |") |
| 123 | ) |
| 124 | assert "local" in comparison_route |
| 125 | assert "local" in competitors_route |
| 126 | |
| 127 | runbook = reference_text("research-runbook") |
| 128 | hosted = runbook.split("### Hosted API route", 1)[1].split("### Local engine route", 1)[0] |
| 129 | choice = "Ask whether to run locally for an explicit comparison or competitor request" |
| 130 | assert hosted.index(choice) < hosted.index("first Step 1 command") |
| 131 | assert "before peer discovery or submitting a hosted search" in hosted |
| 132 | |
| 133 | comparison = reference_text("comparison") |
| 134 | gate = comparison.split("**When host web search is available, per-entity resolution", 1)[0] |
| 135 | assert "local engine only" in gate |
| 136 | assert "wait for the user's choice" in gate |
| 137 | |
| 138 | competitors = reference_text("competitors") |
| 139 | gate = competitors.split("**With host web search, use the four-step protocol:", 1)[0] |
| 140 | assert "local engine only" in gate |
| 141 | assert "before discovering peers" in gate |
| 142 | assert "wait for the user's choice" in gate |
| 143 | |
| 144 | |
| 145 | def _law8_block() -> str: |
| 146 | text = SKILL_MD.read_text(encoding="utf-8") |
| 147 | start = text.find("**LAW 8 -") |
| 148 | assert start != -1, "missing LAW 8 marker" |
| 149 | end = text.find("**LAW 9 -", start) |
| 150 | assert end != -1, "missing LAW 9 marker (LAW 8 block end)" |
| 151 | return text[start:end] |
| 152 | |
| 153 | |
| 154 | def test_law8_is_renderer_aware_with_both_regimes(): |
| 155 | # LAW 8 must keep the inline-link default for hidden-link hosts AND carry a |
| 156 | # plain-label branch for visible-URL hosts. Codex rendered every inline link |
| 157 | # as `label (https://...)`, so a single-renderer LAW 8 produced URL soup. |
| 158 | law8 = _law8_block() |
| 159 | # Hidden-link hosts are Claude Code AND Grok Bot / Cursor agent chat: the |
| 160 | # 2026-09-01 Grok Bot brief showed Cursor agent chat hides markdown URLs |
| 161 | # like Claude Code, so lumping it with Codex produced unclickable cites. |
| 162 | assert "Hidden-link default: inline-link" in law8 |
| 163 | assert "Visible-URL default: prefer plain source labels only when links are optional" in law8 |
| 164 | assert "Preserve required links even when URLs display inline" in law8 |
| 165 | # Hidden-link default must remain inline `[name](url)` (no Claude Code regression). |
| 166 | assert "`[name](url)`" in law8 |
| 167 | |
| 168 | |
| 169 | def test_law8_host_detection_is_deterministic_via_claudecode(): |
| 170 | law8 = _law8_block() |
| 171 | assert "CLAUDECODE" in law8 |
| 172 | # CURSOR_AGENT is the second deterministic hidden-link signal (Grok Bot / |
| 173 | # Cursor agent chat). Dropping it regresses those chats to plain labels. |
| 174 | assert "CURSOR_AGENT" in law8 |
| 175 | # The detection must be stated as deterministic, not left to the model guessing. |
| 176 | assert "Detection is deterministic" in law8 |
| 177 | |
| 178 | |
| 179 | def test_law8_visible_url_hosts_exclude_cursor_and_split_from_step0(): |
| 180 | # Grok Bot / Cursor agent chat hides markdown URLs, so Cursor must not be |
| 181 | # named a visible-URL citation host anywhere. Cursor stays a NON-MODAL |
| 182 | # SETUP host (see test_non_modal_hosts_are_named) - the citation renderer |
| 183 | # is a different axis, and LAW 8 must not claim it is the Step 0 split. |
| 184 | text = "\n".join(contract_documents().values()) |
| 185 | visible_lists = re.findall(r"[Vv]isible-URL hosts? \(([^)]*)\)", text) |
| 186 | assert visible_lists, "no visible-URL host list found" |
| 187 | assert any("Codex" in hosts for hosts in visible_lists) |
| 188 | for hosts in visible_lists: |
| 189 | assert "Cursor" not in hosts, f"Cursor named as visible-URL host: {hosts!r}" |
| 190 | law8 = _law8_block() |
| 191 | assert "both unset means visible-URL default" in law8 |
| 192 | assert "renderer split is separate from onboarding; Cursor remains non-modal" in law8 |
| 193 | |
| 194 | |
| 195 | def test_law8_keeps_claude_code_hidden_link_despite_reattach_preview(): |
| 196 | # Claude Code's background-session reattach preview prints every markdown |
| 197 | # link as `label (url)`; the live view below it renders clickable labels. |
| 198 | # Moving Claude Code to plain labels would hide those URLs but strip every |
| 199 | # clickable source link. See docs/solutions/integration-issues/ |
| 200 | # claude-code-reattach-preview-flattens-links.md. |
| 201 | text = "\n".join(contract_documents().values()) |
| 202 | visible_lists = re.findall(r"[Vv]isible-URL hosts? \(([^)]*)\)", text) |
| 203 | assert visible_lists, "no visible-URL host list found" |
| 204 | for hosts in visible_lists: |
| 205 | assert "Claude Code" not in hosts, f"Claude Code named as visible-URL host: {hosts!r}" |
| 206 | hidden_lists = re.findall(r"[Hh]idden-link hosts? \(([^)]*)\)", text) |
| 207 | assert hidden_lists, "no hidden-link host list found" |
| 208 | assert all("Claude Code" in hosts for hosts in hidden_lists) |
| 209 | law8 = _law8_block() |
| 210 | assert "`CLAUDECODE` or `CURSOR_AGENT` set means hidden-link default" in law8 |
| 211 | |
| 212 | |
| 213 | def test_law8_wrap_list_includes_u_name_and_github_repo_first_mentions(): |
| 214 | # u/name comment authors and GitHub repos must be in the wrap-every-citation |
| 215 | # list, with URLs copied from the comment row / engine evidence block. |
| 216 | law8 = _law8_block() |
| 217 | assert "comment author" in law8 |
| 218 | assert "repository" in law8 |
| 219 | assert "never guess" in law8.lower() |
| 220 | # GitHub evidence can carry an issue/PR URL, not the repo root: the label |
| 221 | # must match what the URL opens - never `[owner/repo]` over an item URL, |
| 222 | # and never an item URL trimmed to a guessed repo root. |
| 223 | assert "an issue/PR/release URL must have a matching item label" in law8 |
| 224 | assert "Never trim an item URL to a guessed root" in law8 |
| 225 | |
| 226 | |
| 227 | def test_law8_post_synthesis_self_check_branches_on_both_env_signals(): |
| 228 | # The post-synthesis self-check is the env-branching gate; it must branch |
| 229 | # on CLAUDECODE or CURSOR_AGENT, and PRE-PRESENT is a supplemental sweep. |
| 230 | law8 = _law8_block() |
| 231 | start = law8.index("post-synthesis self-check") |
| 232 | self_check = law8[start:] |
| 233 | assert "`CLAUDECODE` or `CURSOR_AGENT` set" in law8 |
| 234 | assert "both unset means visible-URL default" in law8 |
| 235 | assert "On hidden-link hosts, add missing known" in self_check |
| 236 | assert "On visible-URL hosts, replace links with plain labels only when optional" in self_check |
| 237 | assert "final checklist supplements this check; it never replaces it" in self_check |
| 238 | |
| 239 | |
| 240 | def test_citation_renderer_host_list_is_mirrored_outside_law8(): |
| 241 | # LAW 9, FUN CONTENT, CITATION PRIORITY, and the PRE-PRESENT sweep must |
| 242 | # carry the same renderer split - Grok Bot / Cursor agent chat hidden-link, |
| 243 | # Codex/Gemini CLI/raw CLI visible-URL - so a chunked read of any one |
| 244 | # section cannot resurrect "Cursor is visible-URL". |
| 245 | text = SKILL_MD.read_text(encoding="utf-8") |
| 246 | law9_start = text.index("**LAW 9 -") |
| 247 | law9 = text[law9_start : text.index("**LAW 10 -", law9_start)] |
| 248 | assert "apply governing citations and LAW 8" in law9 |
| 249 | synthesis = reference_text("synthesis") |
| 250 | text = synthesis |
| 251 | fun_start = text.index("**FUN CONTENT") |
| 252 | fun = text[fun_start : fun_start + 2000] |
| 253 | assert "Grok Bot / Cursor agent chat" in fun |
| 254 | citation_start = text.index("**URL formatting is governed by LAW 8**") |
| 255 | citation = text[citation_start : citation_start + 1500] |
| 256 | assert "hidden-link hosts (Claude Code; Grok Bot / Cursor agent chat)" in citation |
| 257 | assert "Codex/Gemini CLI/raw CLI" in citation |
| 258 | root = SKILL_MD.read_text(encoding="utf-8") |
| 259 | pre_present = root[root.index("## PRE-PRESENT SELF-CHECK"):] |
| 260 | assert "Run LAW 8's citation check separately" in pre_present |
| 261 | |
| 262 | |
| 263 | def test_plan_invocation_warns_against_bash_lc_apostrophe_wrapper(): |
| 264 | # Codex aborted its first engine run by wrapping the query-plan heredoc in |
| 265 | # `bash -lc '...'`; the outer single quote ended at the first apostrophe in a |
| 266 | # ranking string. The guidance must steer off that wrapper explicitly. |
| 267 | text = reference_text("research-runbook") |
| 268 | assert "bash -lc '...'" in text |
| 269 | assert "unmatched" in text |
| 270 | |
| 271 | |
| 272 | def test_step055_documents_dedicated_vs_broad_subreddits(): |
| 273 | # Step 0.55 must instruct the model to split entity-home (dedicated) subs from |
| 274 | # broad subs and pass them via --dedicated-subreddits, which the engine pulls |
| 275 | # in full and exempts from the relevance floor. |
| 276 | text = reference_text("research-runbook") |
| 277 | assert "RESOLVED_DEDICATED_SUBREDDITS" in text |
| 278 | assert "--dedicated-subreddits" in text |
| 279 | assert "relevance floor" in text |
| 280 |