#!/usr/bin/env python3
"""List lane sessions that stopped on the owner, and our PRs nobody has touched, for the conductor.

Why (11-08 to 11-11): three lane sessions sat stopped for three days and nobody noticed. Two ways a
session waits on a person: its last turn ends on a question or a hand-off ("your call", "should I"),
or a tool call is waiting on a permission prompt (a hook's `ask` prompts even with permissions
bypassed, so an unattended lane just stalls there). And a PR we opened can sit untouched for days.

    asks-waiting [--prs] [--push HOST]   scan this Mac's transcripts (and with --prs, our open PRs);
                                         write $RSTACK_STATE_DIR/asks-waiting/<host>.tsv; --push copies it
                                         to the same path on HOST (the conductor's Mac)
    asks-waiting --hook                  UserPromptSubmit hook: in the conductor session only, print every
                                         host's list so the conductor sees it on every turn

Run the scan from launchd every few minutes on each Mac. No model tokens, no notifications.
The conductor is the session whose working directory ends with CONDUCTOR_CWD_SUFFIX; its own
transcripts are never listed.
"""
import argparse
import json
import os
from pathlib import Path
import re
import socket
import subprocess
import sys
import time
from datetime import datetime

sys.path.insert(0, str(Path(__file__).resolve().parents[2] / 'harness'))
sys.path.append(str(Path(os.environ.get('RH_ROOT') or '~/.local/share/research-harness').expanduser()))
import lane_registry

ASK = re.compile(r"\?\s*(\)|\*\*)?\s*$|\b(want me to|should I|shall I|do you want|would you like|your call|"
                 r"your decision|up to you|needs? (you|your)|waiting on (you|your)|over to you|say (send|go))\b", re.I)
# "Nothing needs you" is a report, not an ask (planets, 11-12, flagged as a question). A negated hand-off
# is removed before ASK looks; a trailing question mark still counts.
NOT_ASK = re.compile(r"\b(nothing|no one|nobody|none of this)( else)? (needs|is waiting on|waits on) (you|your)\w*|"
                     r"\b(doesn.t|does not|no need to) (need|wait on) (you|your)\w*", re.I)
IDLE_S = 20 * 60          # a question unanswered this long
TOOL_S = 15 * 60          # a tool call with no result this long: a permission prompt (Bash's own cap is 10 min)
LOOKBACK_S = 3 * 86400
PR_IDLE_H = 6
PR_EVERY_S = 30 * 60      # 11-12: a PR search every 5 min ran the shared GitHub token into its rate limit



def host_label():
    """A stable short host label. 11-17: the m5's HostName was unset, so the network named it mac.lan and
    every per-host file switched from m5-* to mac-*; the conductor saw a 15-hour-old m5 list and thought the
    scan was dead. RSTACK_HOST wins; on macOS the LocalHostName (set by the owner, not the network); else
    the short hostname."""
    import os, socket, subprocess, sys
    value = os.environ.get('RSTACK_HOST', '').strip()
    if value:
        return value
    if sys.platform == 'darwin':
        try:
            r = subprocess.run(['/usr/sbin/scutil', '--get', 'LocalHostName'], capture_output=True, text=True, timeout=5)
            if r.returncode == 0 and r.stdout.strip():
                return r.stdout.strip()
        except (OSError, subprocess.SubprocessError):
            pass
    return socket.gethostname().split('.')[0]

def state_dir():
    base = os.environ.get('RSTACK_STATE_DIR') or os.path.join(
        os.environ.get('XDG_STATE_HOME') or os.path.expanduser('~/.local/state'), 'rstack')
    return Path(os.path.expanduser(base)) / 'asks-waiting'


def stamp(rec):
    try:
        return datetime.fromisoformat(rec['timestamp'].replace('Z', '+00:00')).timestamp()
    except (KeyError, ValueError, AttributeError):
        return None


def last_state(path):
    """(name, cwd, kind, when, text): kind is 'ask', 'tool' or None for the session's last real turn."""
    with path.open('rb') as fh:
        fh.seek(max(0, path.stat().st_size - 1_500_000))
        # split('\n'), not splitlines(): a U+2028 inside a JSON string split one record in two, and the
        # half holding a tool_result was dropped, so the digest session read as stuck on a prompt (11-12).
        lines = fh.read().decode('utf-8', 'replace').split('\n')
    name, cwd, kind, when, text, pending = None, None, None, None, '', {}
    for line in lines:
        try:
            d = json.loads(line)
        except ValueError:
            continue
        if not isinstance(d, dict):
            continue
        t = d.get('type')
        if t in ('agent-name', 'custom-title'):
            name = d.get('agentName') or d.get('customTitle') or name
        cwd = d.get('cwd') or cwd
        if t not in ('user', 'assistant') or d.get('isSidechain') or d.get('isMeta') or d.get('isCompactSummary'):
            continue
        content = (d.get('message') or {}).get('content') or []
        content = [c for c in content if isinstance(c, dict)] if isinstance(content, list) else []
        if t == 'user':
            for c in content:
                if c.get('type') == 'tool_result':
                    pending.pop(c.get('tool_use_id'), None)
            if not pending:
                kind, text = None, ''
            continue
        uses = [c for c in content if c.get('type') == 'tool_use']
        for u in uses:
            pending[u.get('id')] = u.get('name', '?')
        words = '\n'.join(c.get('text', '') for c in content if c.get('type') == 'text').strip()
        if pending:
            kind, when, text = 'tool', stamp(d), 'waiting on ' + ', '.join(sorted(set(pending.values())))
        elif words:
            tail = words[-300:]
            kind, when, text = ('ask' if ASK.search(NOT_ASK.sub('', tail)) else None), stamp(d), tail
    return name, cwd, kind, when, text


def live_dirs(projects):
    """Transcript directories of sessions that have a tmux pane now, or None when tmux cannot be read.
    A session that is not running cannot be waiting on anyone; its last question is history."""
    tmux = os.environ.get('TMUX_BIN') or next((t for t in ('tmux', os.path.expanduser('~/.local/bin/tmux'),
                                                          '/opt/homebrew/bin/tmux') if t == 'tmux' or os.path.exists(t)), 'tmux')
    try:
        out = subprocess.run([tmux, 'list-panes', '-a', '-F', '#{pane_current_path}'], capture_output=True,
                             text=True, timeout=10)
    except (OSError, subprocess.SubprocessError):
        return None
    if out.returncode:
        return None
    return {projects / re.sub(r'[/.]', '-', p) for p in out.stdout.split('\n') if p}


def scan_sessions(projects, now, conductor=''):
    rows = []
    registry = lane_registry.load()
    newest = {}
    live = live_dirs(projects)
    for f in projects.glob('*/*.jsonl'):
        if live is not None and f.parent not in live:
            continue
        try:
            m = f.stat().st_mtime
        except OSError:
            continue
        if now - m < LOOKBACK_S and m > newest.get(f.parent, (0, None))[0]:
            newest[f.parent] = (m, f)   # one live session per directory: older ones there are history
    for _, f in newest.values():
        try:
            name, cwd, kind, when, text = last_state(f)
        except OSError:
            continue
        if not kind or not when or (conductor and (cwd or '').rstrip('/').endswith(conductor)):
            continue
        idle = now - when
        if idle < (IDLE_S if kind == 'ask' else TOOL_S):
            continue
        label = name or Path(cwd or f.parent.name).name
        what = 'question' if kind == 'ask' else 'tool call with no result (permission prompt?)'
        retired = registry.retired_reason(name, Path(cwd or f.parent.name).name)
        if retired:
            what = 'retired: {}; {}'.format(retired, what)
        rows.append('{:.1f}h\t{}\t{}\t{}'.format(idle / 3600, label, what, ' '.join(text.split())[-160:]))
    return rows


def scan_prs(now):
    out = subprocess.run(['gh', 'search', 'prs', '--author', '@me', '--state', 'open', '--limit', '100',
                          '--json', 'url,title,updatedAt'], capture_output=True, text=True, timeout=90)
    if out.returncode:
        return ['?\tgh\tPR scan failed\t' + (out.stderr.strip().splitlines() or ['no output'])[-1][:120]]
    idle = []
    for pr in json.loads(out.stdout or '[]'):
        h = (now - datetime.fromisoformat(pr['updatedAt'].replace('Z', '+00:00')).timestamp()) / 3600
        if h >= PR_IDLE_H:
            idle.append((h, pr))
    by_repo = {}
    for h, pr in idle:
        repo, _, number = pr['url'].split('github.com/')[-1].rpartition('/pull/')
        by_repo.setdefault(repo, []).append((h, number))
    # One line per repo: the conductor reads this on every turn.
    return ['{:.0f}h\t{}\t{} open PR(s) idle {:.0f}-{:.0f}h\toldest #{}'.format(
                max(r)[0], repo, len(r), min(r)[0], max(r)[0], max(r)[1])
            for repo, r in sorted(by_repo.items(), key=lambda kv: -max(kv[1])[0])]


def cached_prs(now, scan=None):
    """The PR rows, refreshed at most every PR_EVERY_S. A failed refresh keeps the last good list
    (marked with its age) instead of replacing it with an error line."""
    cache = state_dir() / 'prs.json'
    try:
        old = json.loads(cache.read_text())
    except (OSError, ValueError):
        old = {}
    if now - old.get('at', 0) < PR_EVERY_S:
        return old.get('rows', [])
    rows = (scan or scan_prs)(now)
    if rows and rows[0].startswith('?\t'):
        if old.get('rows'):
            age = (now - old['at']) / 3600
            return [r + ' (list {:.1f}h old; refresh failed)'.format(age) for r in old['rows']]
        return rows
    cache.parent.mkdir(parents=True, exist_ok=True)
    cache.write_text(json.dumps(dict(at=now, rows=rows)))
    return rows


def hook():
    try:
        event = json.loads(sys.stdin.read())
    except ValueError:
        return 0
    suffix = os.environ.get('CONDUCTOR_CWD_SUFFIX', '').rstrip('/')
    if not suffix or not str(event.get('cwd') or '').rstrip('/').endswith(suffix):
        return 0
    registry = lane_registry.load()
    lines, retired = [], []
    for f in sorted(state_dir().glob('*.tsv')):
        age = (time.time() - f.stat().st_mtime) / 60
        body = [l for l in f.read_text().splitlines() if l.strip()]
        if age > 30:
            lines.append('{}: list is {:.0f} min old; its scan may have stopped'.format(f.stem, age))
        for row in body:
            row = classify(row, registry)
            target = retired if is_retired(row) else lines
            target.append('{}: {}'.format(f.stem, row.replace('\t', ' | ')))
    if lines:
        print('[asks-waiting] sessions stopped on a person, and stale PRs (hours idle | session | what):')
        print('\n'.join(lines))
    if retired:
        print('[asks-waiting] retired (visible history; no action or relay required):')
        print('\n'.join(retired))
    return 0


def is_retired(row):
    return len(row.split('\t')) >= 3 and row.split('\t')[2].startswith('retired: ')


def classify(row, registry):
    """Reclassify cached host rows too, so retirement takes effect before the next scan."""
    fields = row.split('\t')
    if len(fields) >= 3 and not is_retired(row):
        reason = registry.retired_reason(fields[1])
        if reason:
            fields[2] = 'retired: {}; {}'.format(reason, fields[2])
    return '\t'.join(fields)


def main(argv=None):
    ap = argparse.ArgumentParser(description=__doc__.split('\n')[0])
    ap.add_argument('--hook', action='store_true')
    ap.add_argument('--prs', action='store_true')
    ap.add_argument('--push')
    ap.add_argument('--projects', default=os.path.expanduser('~/.claude/projects'))
    a = ap.parse_args(argv)
    if a.hook:
        return hook()
    now = time.time()
    projects = Path(a.projects)
    rows = scan_sessions(projects, now, os.environ.get('CONDUCTOR_CWD_SUFFIX', '').rstrip('/'))
    if a.prs:
        rows += cached_prs(now)
    out = state_dir() / (host_label() + '.tsv')
    out.parent.mkdir(parents=True, exist_ok=True)
    tmp = out.with_suffix('.tmp')
    tmp.write_text(''.join(r + '\n' for r in rows))
    os.replace(tmp, out)
    if a.push:
        subprocess.run(['ssh', '-o', 'BatchMode=yes', a.push, 'mkdir -p ' + str(out.parent)],
                       capture_output=True, timeout=60)
        subprocess.run(['scp', '-q', str(out), '{}:{}'.format(a.push, out)], capture_output=True, timeout=60)
    print('\n'.join(r for r in rows if not is_retired(r)))
    retired = [r for r in rows if is_retired(r)]
    if retired:
        print('[asks-waiting] retired (visible history; no action or relay required):')
        print('\n'.join(retired))
    return 0


if __name__ == '__main__':
    sys.exit(main())
