#!/usr/bin/env python3
# sloppatrol-scope: all
# sloppatrol-fix: get the client from parallax.track.sink.connect() and never shut it down. The SDK caches its workers per public key and shutdown() leaves the dead entry cached, so a second client in the same process hangs in flush.
"""Only parallax/track/sink.py builds a Langfuse client, and nothing in parallax/ shuts one down."""

import os
import re
import sys
from pathlib import Path

SINK = "parallax/track/sink.py"
BUILD = re.compile(r"\bLangfuse\(")
SHUTDOWN = re.compile(r"\.shutdown\(\)")

root = Path(os.environ["SLOPPATROL_ROOT"])
hits = []
for path in Path(os.environ["SLOPPATROL_FILES"]).read_text().splitlines():
    if not (path.startswith("parallax/") and path.endswith(".py")):
        continue
    for number, line in enumerate((root / path).read_text().splitlines(), 1):
        if BUILD.search(line) and path != SINK:
            hits.append(f"{path}:{number}: one-langfuse-client builds a second Langfuse client")
        if SHUTDOWN.search(line):
            hits.append(f"{path}:{number}: one-langfuse-client shuts down a shared client")
print("\n".join(hits)) if hits else None
sys.exit(1 if hits else 0)
