#!/usr/bin/env python3 """x-submit-gate: a Chrome submit on X needs a reason the browser may do it (PreToolUse, 11-19). Ricardo, 11-19: "How can we make them use it?" Replies and posts go through Grok; likes and follows through the API; the browser is last. This hook refuses a Claude in Chrome click, key or form input that submits an X action (reply, post, like, follow, repost) unless all three hold: 1. the action kind has no Grok or API route, or that route failed for the same target in the last 15 minutes (x-route writes the route ledger; harness/x_route.py reads it); 2. the tab passed x-handle-guard's probe for the expected handle in the selected browser; 3. the selected browser is not a denied device (the owner's private account), ever. It acts only on tabs whose URL is x.com or twitter.com. A submit is recognised by its action_summary, which X actions here already must carry ("Posts the reply on X to @HANDLE", "Likes ...", "Follows @HANDLE"), and the summary must name the target: a post URL or ID, or the @handle. Anything that is not an X submit passes untouched. Config ~/.config/rstack/x-submit-gate.json (or X_SUBMIT_GATE_CONFIG): {"deny_devices": [""], "handle": ""} No config, or an unreadable one, refuses every X submit. Runs through _hookfail.run: a call it cannot read is refused only on a known X tab; everything else passes, with the error on stderr. """ import json import os from pathlib import Path import re import sys import time ROOT = Path(__file__).resolve().parents[2] sys.path.insert(0, str(ROOT / 'harness' if (ROOT / 'harness/x_route.py').exists() else ROOT)) import x_route # noqa: E402 PREFIX = 'mcp__claude-in-chrome__' ACTING = {'left_click', 'right_click', 'double_click', 'triple_click', 'key', 'type', 'left_click_drag'} # An @handle is not part of an e-mail address: no word character before it, no '.domain' after it. X_CONTEXT = re.compile(r'\bon X\b|\bx\.com\b|\btweet\b|\bX post\b|(?