#!/usr/bin/env python3
"""x-handle-guard: no X action from a browser that is not logged in as the posting account.

A Claude Code hook (PreToolUse and PostToolUse) for the Claude in Chrome tools. It acts only on
tabs whose URL is x.com or twitter.com (11-19, general: global registration must not refuse non-X
work); every other tab, and a tab whose URL it has not seen, passes untouched. PostToolUse learns
each tab's URL from the "Tab Context" every Chrome tool returns, and from navigate.

Why (11-19): two Chrome browsers are paired with the same Claude login on the project host.
One is the project account; the other is the owner's private account and must never like,
follow or reply. The reviewer prompt said "check the handle first", which is prose. This makes it code:
every click, keypress, form input, upload or script in a tab is refused until the handle probe
has run in that tab, in the currently selected browser, and returned the expected handle.

The probe is one `javascript_tool` call whose text is exactly PROBE (whitespace aside). It reads
the logged-in account from X's own left navigation, so it answers "who is logged in", not
"which handles appear on this page". PostToolUse records the answer. Switching or selecting a
browser forgets every earlier answer. Reads (screenshots, scrolling, page text, find,
navigation, tab management) are always allowed, so the agent can open x.com and probe.

  handle                  expected screen_name, from "handle" in ~/.config/rstack/x-submit-gate.json
                          (X_SUBMIT_GATE_CONFIG), the config both X hooks read; X_HANDLE_GUARD_HANDLE
                          overrides it. None configured refuses every X action.
  X_HANDLE_GUARD_TTL      seconds a probe stays valid (default 2700, the reviewer's alarm)
  RSTACK_STATE_DIR        state root (default ~/.local/state/rstack)

Runs through _hookfail.run: a call it cannot read is refused only when it is a click, key, form
input or script on a known X tab; anything else passes with the error on stderr. Unreadable state
is treated as empty (URLs unknown). The decision is hook JSON on stdout.
"""
import json
import os
from pathlib import Path
import re
import sys
import time

PREFIX = 'mcp__claude-in-chrome__'
PROBE = ("(() => { const a = document.querySelector('a[data-testid=AppTabBar_Profile_Link]'); "
         "const s = document.querySelector('[data-testid=SideNav_AccountSwitcher_Button]'); "
         "const m = s && (s.innerText || '').match(/@([A-Za-z0-9_]+)/); "
         "if (location.hostname !== 'x.com') return 'XHANDLE=off-x'; "
         "return 'XHANDLE=' + (a ? a.pathname.split('/')[1] : m ? m[1] : 'none'); })()")
ANSWER = re.compile(r'XHANDLE=([A-Za-z0-9_-]+)')
# Every Chrome tool result carries 'tabId N: "title" ("url")' lines, and tabs_context also JSON rows.
TAB_LINE = re.compile(r'tabId (\d+): .*\("([^"\s]+)"\)\s*$', re.M)
TAB_JSON = re.compile(r'"tabId"\s*:\s*(\d+)\s*,\s*"title"\s*:\s*"(?:[^"\\]|\\.)*"\s*,\s*"url"\s*:\s*"([^"]+)"')


def is_x(url):
    m = re.match(r'https?://([^/:?#]+)', str(url or ''), re.I)
    host = m[1].casefold() if m else ''
    return host in ('x.com', 'twitter.com') or host.endswith(('.x.com', '.twitter.com'))


def tab_url(state, tab):
    return (state.get('urls') or {}).get(str(tab))
# Computer actions that only look. Everything else (clicks, typing, keys, drags) acts.
LOOK = {'screenshot', 'scroll', 'scroll_to', 'wait', 'zoom', 'hover', 'mouse_move', 'cursor_position'}
# Tools that never act on a page.
PASSIVE = {'tabs_context_mcp', 'tabs_create_mcp', 'tabs_close_mcp', 'navigate', 'read_page', 'get_page_text',
           'find', 'list_connected_browsers', 'read_console_messages', 'read_network_requests',
           'gif_creator', 'resize_window', 'shortcuts_list', 'select_browser', 'switch_browser'}


def handle():
    return shared_handle()


def ttl():
    try:
        return float(os.environ.get('X_HANDLE_GUARD_TTL', '2700'))
    except ValueError:
        return 2700.0


def state_path(event):
    root = Path(os.environ.get('RSTACK_STATE_DIR') or Path.home() / '.local/state/rstack').expanduser()
    sid = re.sub(r'[^A-Za-z0-9_-]', '', str(event.get('session_id', '')))[:64] or 'none'
    return root / 'x-handle-guard' / (sid + '.json')


def load(event):
    path = state_path(event)
    try:
        data = json.loads(path.read_text()) if path.exists() else {}
    except (OSError, ValueError):
        data = {}
    if not isinstance(data, dict) or not isinstance(data.get('tabs'), dict):
        data = dict(device=None, tabs={})
    if not isinstance(data.get('urls'), dict):
        data['urls'] = {}
    return data


def save(event, data):
    path = state_path(event)
    path.parent.mkdir(parents=True, exist_ok=True, mode=0o700)
    tmp = path.with_suffix('.tmp')
    tmp.write_text(json.dumps(data))
    tmp.replace(path)


def norm(text):
    return re.sub(r'\s+', '', str(text))


def is_probe(inp):
    return isinstance(inp, dict) and norm(inp.get('text', '')) == norm(PROBE)


def steps(name, inp):
    """(tool, input) pairs: one call, or every action inside a browser_batch."""
    if not isinstance(inp, dict):
        raise ValueError('tool_input is not an object')
    if name != 'browser_batch':
        return [(name, inp)]
    found = inp.get('actions')
    if not isinstance(found, list):
        raise ValueError('browser_batch actions is not a list')
    out = []
    for item in found:
        if not isinstance(item, dict) or not isinstance(item.get('input', {}), dict):
            raise ValueError('browser_batch action is not an object')
        out.append((str(item.get('name', '')).replace(PREFIX, ''), item.get('input', {})))
    return out


def acting(tool, inp):
    if tool in PASSIVE:
        return False
    if tool == 'computer':
        return inp.get('action') not in LOOK
    if tool == 'javascript_tool':
        return not is_probe(inp)
    return True     # form_input, file_upload, upload_image, shortcuts_execute and anything new


def verified(state, tab):
    row = state['tabs'].get(str(tab))
    return (bool(handle()) and isinstance(row, dict) and row.get('handle', '').casefold() == handle().casefold()
            and row.get('device') == state.get('device') and time.time() - float(row.get('utc', 0)) <= ttl())


def reason(tab, why):
    return ('x-handle-guard: refused an X action in tab {}: {}. Before any click, keypress, form input or '
            'script, run this exact javascript_tool call in that tab on an x.com page, and act only if it '
            'returns XHANDLE={}: {}').format(tab, why, handle(), PROBE)


# --- fail-safe wrapper (11-19, hook-audit: both guards crashed on a JSON string or list and so failed open) ---
# _hookfail: the repo's rstack/hooks copy, else the installed one in ~/.claude/hooks (byte-identical, hook-wrap-check).
for _lib in (Path(__file__).resolve().parents[1] / 'hooks', Path(os.path.expanduser('~/.claude/hooks'))):
    if (_lib / '_hookfail.py').is_file():
        sys.path.insert(0, str(_lib))
        break
import _hookfail  # noqa: E402

ACT_WORDS = re.compile(r'left_click|right_click|double_click|triple_click|left_click_drag|"type"|"key"|'
                       r'form_input|javascript_tool|browser_batch|file_upload|upload_image|shortcuts_execute')


def x_tab_triage(raw, event):
    """Is this call a click, key, form input or script on a tab known to be x.com or twitter.com?

    Raw text and the tab-URL state only. An unknown tab, a non-X tab, or anything unreadable answers
    no: general, 11-19, the guard must never refuse non-X work, even when it breaks."""
    try:
        tabs = set(re.findall(r'"tabId"\s*:\s*"?(\d+)', raw))
        if not tabs or not ACT_WORDS.search(raw):
            return False
        sid = re.search(r'"session_id"\s*:\s*"([^"]*)"', raw)
        sid = re.sub(r'[^A-Za-z0-9_-]', '', sid.group(1) if sid else '')[:64] or 'none'
        root = Path(os.environ.get('RSTACK_STATE_DIR') or Path.home() / '.local/state/rstack').expanduser()
        urls = json.loads((root / 'x-handle-guard' / (sid + '.json')).read_text()).get('urls') or {}
        return any(_on_x(urls.get(t)) for t in tabs)
    except Exception:
        return False


def _on_x(url):
    m = re.match(r'https?://([^/:?#]+)', str(url or ''), re.I)
    host = m[1].casefold() if m else ''
    return host in ('x.com', 'twitter.com') or host.endswith(('.x.com', '.twitter.com'))


def shared_handle():
    """The expected handle: X_HANDLE_GUARD_HANDLE, else "handle" in the config both X hooks read."""
    if os.environ.get('X_HANDLE_GUARD_HANDLE', '').strip():
        return os.environ['X_HANDLE_GUARD_HANDLE'].strip().lstrip('@')
    path = Path(os.environ.get('X_SUBMIT_GATE_CONFIG') or '~/.config/rstack/x-submit-gate.json').expanduser()
    try:
        return str(json.loads(path.read_text()).get('handle', '')).strip().lstrip('@')
    except (OSError, ValueError, AttributeError):
        return ''


def deny(text):
    print(json.dumps(dict(hookSpecificOutput=dict(hookEventName='PreToolUse', permissionDecision='deny',
                                                  permissionDecisionReason=text))))


def pre(event, name):
    # A call it cannot read raises; _hookfail.run then refuses it only on a known X tab (x_tab_triage).
    state = load(event)
    pairs = steps(name, event.get('tool_input'))
    for tool, inp in pairs:
        if not acting(tool, inp):
            continue
        tab = inp.get('tabId')
        if tab is None or not is_x(tab_url(state, tab)):
            continue    # not an X tab (or one it has not seen): not this guard's business
        if not verified(state, tab):
            row = state['tabs'].get(str(tab))
            seen = row.get('handle') if isinstance(row, dict) else None
            why = ('the handle probe returned @{} there, not @{}'.format(seen, handle()) if seen and seen != handle()
                   else 'no current handle probe for this tab in the selected browser')
            return deny(reason(tab, why))
    return None


def response_text(value):
    if isinstance(value, str):
        return value
    if isinstance(value, list):
        return '\n'.join(response_text(v) for v in value)
    if isinstance(value, dict):
        return response_text(value.get('text', '')) + '\n' + response_text(value.get('content', ''))
    return ''


def post(event, name):
    inp = event.get('tool_input')
    state = load(event)
    if name in ('select_browser', 'switch_browser'):
        # A different browser may be a different account: forget every earlier probe and tab.
        state = dict(device=str((inp or {}).get('deviceId') or (inp or {}).get('device_id') or 'switched'),
                     tabs={}, urls={})
        save(event, state)
        return
    text = response_text(event.get('tool_response'))
    seen = dict(TAB_JSON.findall(text))
    seen.update(TAB_LINE.findall(text))
    if name == 'navigate' and isinstance(inp, dict) and inp.get('tabId') is not None and inp.get('url'):
        seen.setdefault(str(inp['tabId']), str(inp['url']))
    if seen and seen != {k: state['urls'].get(k) for k in seen}:
        state['urls'].update(seen)
        save(event, state)
    if name == 'javascript_tool' and is_probe(inp) and inp.get('tabId') is not None:
        found = ANSWER.findall(response_text(event.get('tool_response')))
        seen = found[-1] if len(set(found)) == 1 else 'unclear'
        state['tabs'][str(inp['tabId'])] = dict(handle=seen, device=state.get('device'), utc=time.time())
        save(event, state)


def body(event):
    name = str(event.get('tool_name', ''))
    if not name.startswith(PREFIX):
        return 0
    name = name[len(PREFIX):]
    if event.get('hook_event_name') == 'PostToolUse':
        try:
            post(event, name)
        except Exception:
            pass    # an unrecorded probe or URL only means the next X action is refused: the safe way
        return 0
    pre(event, name)
    return 0


if __name__ == '__main__':
    sys.exit(_hookfail.run('x-handle-guard', body, x_tab_triage, timeout=8,
                           guards='clicks, keys, form inputs and scripts on x.com and twitter.com tabs'))
