"""Byte forwarder between a TCP port and a Unix socket. Python stdlib only. The predictor's sandbox has its own empty network namespace (unshare -rn). Its only way out is this bridge: inside the namespace: fwd.py tcp2unix 127.0.0.1:8844 /tmp/value_shim.sock (Claude Code -> socket) on the host: fwd.py unix2tcp /tmp/value_shim.sock 127.0.0.1:8844 (socket -> Carbon shim) So the sandbox can reach the Carbon shim and nothing else (the predictor spec's network allowlist). """ import asyncio import os import sys async def pipe(r, w): try: while True: b = await r.read(65536) if not b: break w.write(b) await w.drain() except Exception: pass finally: try: w.close() except Exception: pass async def main(): mode, src, dst = sys.argv[1:4] async def handle(r, w): try: if mode == 'tcp2unix': r2, w2 = await asyncio.open_unix_connection(dst) else: h, p = dst.rsplit(':', 1) r2, w2 = await asyncio.open_connection(h, int(p)) except Exception: w.close() return await asyncio.gather(pipe(r, w2), pipe(r2, w)) if mode == 'tcp2unix': h, p = src.rsplit(':', 1) srv = await asyncio.start_server(handle, h, int(p)) else: if os.path.exists(src): os.unlink(src) srv = await asyncio.start_unix_server(handle, src) os.chmod(src, 0o600) print('ready', flush=True) async with srv: await srv.serve_forever() if __name__ == '__main__': asyncio.run(main())