Created
March 12, 2026 03:36
-
-
Save Algomancer/03527d0301a0a365d405fbcefdb849cd to your computer and use it in GitHub Desktop.
.claude/hooks/inbox-bridge.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env python3 | |
| """ | |
| Bridge: unix socket events → Claude Code teammate inbox. | |
| Launched by session-start hook. Writes events as teammate messages | |
| so InboxPoller picks them up and injects into conversation. | |
| Usage: inbox-bridge.py <agent_name> <team_name> [socket_path] | |
| """ | |
| import sys, os, json, socket, time, fcntl | |
| SOCKET_PATH = "/tmp/minbridge-events.sock" | |
| # Only forward these event types to the inbox. Everything else is noise. | |
| # Do NOT include channels the agent sends ON (e.g. bridge:notify) — that creates a feedback loop. | |
| FORWARD_PREFIXES = [ | |
| "bridge:agent", # dashboard actions on the "agent" channel (ping, etc.) | |
| "bridge:command", # command channel | |
| ] | |
| def get_inbox_path(agent_name, team_name): | |
| claude_dir = os.path.expanduser("~/.claude") | |
| safe_team = team_name.replace(":", "-") | |
| safe_agent = agent_name.replace(":", "-") | |
| return os.path.join(claude_dir, "teams", safe_team, "inboxes", f"{safe_agent}.json") | |
| def write_to_inbox(inbox_path, message): | |
| """Append a message to the inbox JSON file with file locking.""" | |
| os.makedirs(os.path.dirname(inbox_path), exist_ok=True) | |
| fd = os.open(inbox_path, os.O_RDWR | os.O_CREAT) | |
| try: | |
| fcntl.flock(fd, fcntl.LOCK_EX) | |
| with os.fdopen(os.dup(fd), "r") as f: | |
| try: | |
| messages = json.loads(f.read()) | |
| except (json.JSONDecodeError, ValueError): | |
| messages = [] | |
| messages.append(message) | |
| os.ftruncate(fd, 0) | |
| os.lseek(fd, 0, os.SEEK_SET) | |
| os.write(fd, json.dumps(messages, indent=2).encode()) | |
| finally: | |
| fcntl.flock(fd, fcntl.LOCK_UN) | |
| os.close(fd) | |
| def main(): | |
| agent_name = sys.argv[1] if len(sys.argv) > 1 else "agent" | |
| team_name = sys.argv[2] if len(sys.argv) > 2 else "mancer" | |
| sock_path = sys.argv[3] if len(sys.argv) > 3 else SOCKET_PATH | |
| inbox_path = get_inbox_path(agent_name, team_name) | |
| while True: | |
| try: | |
| s = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) | |
| s.connect(sock_path) | |
| buf = b"" | |
| while True: | |
| data = s.recv(4096) | |
| if not data: | |
| time.sleep(2) | |
| break | |
| buf += data | |
| while b"\n" in buf: | |
| line, buf = buf.split(b"\n", 1) | |
| try: | |
| event = json.loads(line) | |
| except json.JSONDecodeError: | |
| continue | |
| event_type = event.get("type", "") | |
| if not any(event_type.startswith(p) for p in FORWARD_PREFIXES): | |
| continue | |
| msg = { | |
| "from": "app-server", | |
| "text": json.dumps(event), | |
| "read": False, | |
| "timestamp": int(time.time() * 1000) | |
| } | |
| write_to_inbox(inbox_path, msg) | |
| except (ConnectionRefusedError, FileNotFoundError): | |
| time.sleep(2) | |
| except Exception: | |
| time.sleep(2) | |
| finally: | |
| try: | |
| s.close() | |
| except: | |
| pass | |
| if __name__ == "__main__": | |
| main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment