Skip to content

Universal SEO skill for Claude Code. 26 sub-skills + 19 sub-agents covering technical SEO, E-E-A-T, schema, GEO/AEO, agent readiness (Lighthouse Agentic Browsing, WebMCP, llms.txt), backlinks, local SEO, e-commerce, international SEO, Google APIs, and PDF/Excel reporting. 9 optional extensions for live SEO data.

fix(agentic): the robots drafter split groups and could unblock a crawler

Agrici Danielf843419fc8e6
diff --git a/scripts/agentic_fix.py b/scripts/agentic_fix.py
index 5d1adc8..a5b6531 100644
--- a/scripts/agentic_fix.py
+++ b/scripts/agentic_fix.py
@@ -55,44 +55,57 @@ SLUG = re.compile(r"[^a-z0-9]+")
def add_content_signal(robots_text: str, signal: str) -> dict:
- """Insert ``Content-Signal: <signal>`` after the user-agent lines of each group
- that has none. Returns the new text plus a list of changed groups."""
+ """Insert ``Content-Signal: <signal>`` into each user-agent group that has none.
+
+ Groups follow RFC 9309 as ``agentic_check.parse_robots`` reads them: a group
+ is a run of ``user-agent`` lines (blank lines and comments between them do
+ not end it) followed by rule lines, until the next ``user-agent`` line after
+ a rule. The signal goes directly after the group's last ``user-agent`` line,
+ so no group is split and every Allow/Disallow outcome stays identical.
+ """
parsed = parse_content_signal(signal)
if parsed["issues"]:
raise ValueError("invalid signal: " + "; ".join(parsed["issues"]))
robots_text = robots_text.lstrip("\ufeff")
+ newline = "\r\n" if "\r\n" in robots_text else "\n"
lines = robots_text.splitlines()
- out, changed = [], []
- i = 0
- while i < len(lines):
- line = lines[i]
- if _field(line) == "user-agent":
- agents = []
- while i < len(lines) and _field(lines[i]) == "user-agent":
- agents.append(lines[i].split(":", 1)[1].split("#", 1)[0].strip())
- out.append(lines[i])
- i += 1
- j = i
- has_signal = False
- while j < len(lines) and _field(lines[j]) != "user-agent":
- if _field(lines[j]) == "content-signal":
- has_signal = True
- j += 1
- if not has_signal:
- out.append(f"Content-Signal: {signal}")
- changed.append(agents)
+
+ # Pass 1: find groups as (index of last user-agent line, agents, has_signal).
+ groups, current, in_ua_block = [], None, False
+ for idx, line in enumerate(lines):
+ field = _field(line)
+ if not field:
+ continue # blank line or comment: neither starts nor ends a block
+ if field == "user-agent":
+ if current is None or not in_ua_block:
+ current = {"last_ua": idx, "agents": [], "has_signal": False}
+ groups.append(current)
+ current["agents"].append(line.split(":", 1)[1].split("#", 1)[0].strip())
+ current["last_ua"] = idx
+ in_ua_block = True
continue
+ in_ua_block = False
+ if current is not None and field == "content-signal":
+ current["has_signal"] = True
+
+ # Pass 2: insert after the last user-agent line of each group lacking a signal.
+ insert_after = {g["last_ua"] for g in groups if not g["has_signal"]}
+ changed = [g["agents"] for g in groups if not g["has_signal"]]
+ out = []
+ for idx, line in enumerate(lines):
out.append(line)
- i += 1
- if not any(_field(ln) == "user-agent" for ln in lines):
+ if idx in insert_after:
+ out.append(f"Content-Signal: {signal}")
+
+ if not groups:
# Append (never prepend): rules outside any group stay inert instead of
# joining the new group, and no Allow/Disallow line is invented.
tail = ["User-agent: *", f"Content-Signal: {signal}"]
out = (out + [""] + tail) if any(ln.strip() for ln in out) else tail
changed.append(["*"])
- text = "\n".join(out)
- if robots_text.endswith("\n") or not robots_text:
- text += "\n"
+ text = newline.join(out)
+ if robots_text.endswith(("\n", "\r")) or not robots_text:
+ text += newline
return {"robots_txt": text, "groups_changed": changed}
diff --git a/tests/test_agentic.py b/tests/test_agentic.py
index 659ca8b..a783011 100644
--- a/tests/test_agentic.py
+++ b/tests/test_agentic.py
@@ -483,3 +483,50 @@ def test_real_404_passes_the_probe(monkeypatch):
monkeypatch.setattr(ac, "fetch", lambda url, headers=None: _rec(404))
checks, data = ac.audit_not_found("https://e.test")
assert checks[0]["status"] == "pass" and data["catch_all_200"] is False
+
+
+TOKENS = ["GPTBot", "CCBot", "ClaudeBot", "OAI-SearchBot", "Googlebot", "PerplexityBot", "*"]
+
+
+def _policy(text: str) -> dict:
+ parsed = ac.parse_robots(text)
+ return {t: ac.is_allowed(ac.select_group(parsed, t)["rules"], "/") for t in TOKENS}
+
+
[email protected]("robots", [
+ "User-agent: GPTBot\n# training bots\nUser-agent: CCBot\nDisallow: /\n",
+ "User-agent: GPTBot\n\nUser-agent: CCBot\nDisallow: /\n",
+ "User-agent: *\nAllow: /\n\nUser-agent: GPTBot\n# c\n\nUser-agent: ClaudeBot\nDisallow: /\n# tail\nUser-agent: OAI-SearchBot\nAllow: /\n",
+ "User-agent: GPTBot\r\nUser-agent: CCBot\r\nDisallow: /\r\n",
+])
+def test_content_signal_never_changes_who_is_allowed(robots: str) -> None:
+ """The drafter's promise: Allow/Disallow outcomes are identical after the edit."""
+ draft = af.add_content_signal(robots, "search=yes, ai-train=no")["robots_txt"]
+ assert _policy(draft) == _policy(robots), draft
+ parsed = ac.parse_robots(draft)
+ for group in parsed["groups"]:
+ assert group["content_signal"], f"group without signal: {group['agents']}"
+
+
+def test_content_signal_policy_equivalence_randomized() -> None:
+ """500 generated robots.txt files: the drafter never changes an outcome."""
+ import random
+
+ rng = random.Random(20260923)
+ agents = ["GPTBot", "CCBot", "ClaudeBot", "OAI-SearchBot", "PerplexityBot", "*"]
+ fillers = ["", "# note", " ", "Sitemap: https://e.test/s.xml"]
+ rules = ["Disallow: /", "Allow: /", "Disallow:", "Disallow: /private", "Crawl-delay: 5"]
+ for _ in range(500):
+ lines = []
+ for _ in range(rng.randint(1, 4)):
+ for _ in range(rng.randint(1, 3)):
+ lines.append(f"User-agent: {rng.choice(agents)}")
+ if rng.random() < 0.4:
+ lines.append(rng.choice(fillers))
+ for _ in range(rng.randint(0, 3)):
+ lines.append(rng.choice(rules))
+ if rng.random() < 0.3:
+ lines.append(rng.choice(fillers))
+ robots = rng.choice(["\n", "\r\n"]).join(lines) + "\n"
+ draft = af.add_content_signal(robots, "search=yes")["robots_txt"]
+ assert _policy(draft) == _policy(robots), robots