#!/usr/bin/env python3
"""crew-say PAIR ROLE TEXT — type TEXT into that agent's pane and press Enter.
PAIR is the crew's short name; ROLE is architect or builder (the relay's slots).

Refuses anything over 400 characters. Locked rules 12: agents talk in files and
what reaches a pane is one line naming one. A long message is re-read on every
later turn of that session, so it is the most expensive mistake a crew makes.
Override for the rare real exception: --long.
"""
import pathlib, sys, importlib.machinery, importlib.util

argv = [a for a in sys.argv[1:] if a != "--long"]
allow_long = "--long" in sys.argv
pair, role, text = argv[0], argv[1], " ".join(argv[2:])

if len(text) > 400 and not allow_long and not text.startswith("Read "):
    sys.stderr.write(
        f"crew-say: message is {len(text)} characters, the limit is 400.\n"
        "Write it to a file in the crew chat folder and send one line naming\n"
        'it: Read "<path>" and continue.   (--long overrides)\n')
    sys.exit(2)

sys.argv = ["crew-relay", f"--pair={pair}"]
loader = importlib.machinery.SourceFileLoader("relay", str(pathlib.Path(__file__).resolve().parent / "crew-relay"))
spec = importlib.util.spec_from_loader("relay", loader)
relay = importlib.util.module_from_spec(spec)
loader.exec_module(relay)
sys.exit(0 if relay.send(role, text) else 1)
