<commit_before>(ns shadow.nrepl-debug
  (:require [nrepl.transport :as transport]
            [clojure.pprint :refer (pprint)]
            [clojure.walk :as walk]
            [clojure.java.io :as io])
  (:import [java.net ServerSocket Socket]
           [java.io FileOutputStream OutputStreamWriter]))

(defonce print-lock (Object.))

(defn log [out from msg]
  (let [text (with-out-str (pprint msg))]
    (prn [:msg from (count text)])
    (locking print-lock
      (binding [*out* out]
        (println ";; ----------------------------------------")
        (println ";; -- FROM " from)
        (println ";; ----------------------------------------")
        (println text))
      )))

(defn client-loop [id client to-port]
  (let [target (Socket. "localhost" to-port)

        target-transport
        (transport/bencode target)

        client-transport
        (transport/bencode client)

        log-file
        (io/file "target" "nrepl-debug" (str "nrepl-" id ".log.edn"))]

    (io/make-parents log-file)

    (with-open [log-out (-> log-file (FileOutputStream.) (OutputStreamWriter.))]

      ;; target -> client
      (doto (Thread.
              #(do (try
                     (loop []
                       (when-some [target-msg (transport/recv target-transport)]
                         (log log-out :target target-msg)
                         (transport/send client-transport target-msg)
                         (recur)))
                     (catch Exception e
                       (prn [:target-ex e])))
                   (.close client)))
        (.start))

      (try
        ;; client -> target
        (loop []
          (when-some [client-msg (transport/recv client-transport)]
            (log log-out :client client-msg)
            (transport/send target-transport client-msg)
            (recur)))
        (catch Exception e
          (prn [:client-ex e]))))<commit_msg>tweak nrepl-debug util<commit_after>(ns shadow.nrepl-debug
  (:require [nrepl.transport :as transport]
            [clojure.pprint :refer (pprint)]
            [clojure.walk :as walk]
            [clojure.java.io :as io])
  (:import [java.net ServerSocket Socket]
           [java.io FileOutputStream OutputStreamWriter]))

(defonce print-lock (Object.))

;; using an agent so log writes don't delay messages
(defonce write-agent (agent {}))

(defn log [agent-state out from msg]
  (let [text (with-out-str (pprint msg))]
    (prn [:msg from (count text)])
    (locking print-lock
      (binding [*out* out]
        (println ";; ----------------------------------------")
        (println ";; -- FROM " from)
        (println ";; ----------------------------------------")
        (println text))))
  agent-state)

(defn client-loop [id client to-port]
  (let [target (Socket. "localhost" to-port)

        target-transport
        (transport/bencode target)

        client-transport
        (transport/bencode client)

        log-file
        (io/file "target" "nrepl-debug" (str "nrepl-" id ".log.edn"))]

    (io/make-parents log-file)

    (with-open [log-out (-> log-file (FileOutputStream.) (OutputStreamWriter.))]

      ;; target -> client
      (doto (Thread.
              #(do (try
                     (loop []
                       (when-some [target-msg (transport/recv target-transport)]
                         (send write-agent log log-out :target target-msg)
                         (transport/send client-transport target-msg)
                         (recur)))
                     (catch Exception e
                       (prn [:target-ex e])))
                   (.close client)))
        (.start))

      (try
        ;; client -> target
        (loop []
          (when-some [client-msg (transport/recv client-transport)]
            (send write-agent log log-out :client client-msg)
            (transport/send target-transport client-msg)
            (recur)))
        (catch Exception e
          (prn [:client-ex e]))))
{'fraction_non_alphanumeric': 0.17159464240586303, 'fraction_numerical': 0.0, 'mean_word_length': 1.5420680796403339, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3690475', 'n_tokens_mistral': 1151, 'n_tokens_neox': 1103, 'n_words': 285}