<commit_before>require "hq/engine/subprocess-rule-provider/rule-provider"

module HQ
module Engine
class SubProcessRuleProvider

	def self.start path_to_server

		ctl_rd, ctl_wr = IO.pipe
		req_rd, req_wr = IO.pipe
		resp_rd, resp_wr = IO.pipe

		File.executable? path_to_server \
			or raise "Not found: #{path_to_server}"

		# TODO why do we fork twice? i don't think we need to...

		outer_pid = fork do

			at_exit { exit! }

			ctl_rd.close

			inner_pid = fork do

				$stdin.reopen req_rd
				$stdout.reopen resp_wr
				#$stderr.reopen "/dev/null", "w"

				req_rd.close
				resp_wr.close

				exec path_to_server

			end

			ctl_wr.puts inner_pid

			exit!

		end

		ctl_wr.close
		req_rd.close
		resp_wr.close

		inner_pid = ctl_rd.gets.strip.to_i
		ctl_rd.close

		Process.wait outer_pid

		at_exit do
			begin
				Process.kill "TERM", inner_pid
			rescue Errno::ESRCH
				# do nothing
			end
		end

		require "hq/engine/subprocess-rule-provider/rule-provider"

		client =
			RuleProvider.new \<commit_msg>yet more fixex to rule provider initialisation<commit_after>module HQ
module Engine
class SubProcessRuleProvider

	def self.start path_to_server

		ctl_rd, ctl_wr = IO.pipe
		req_rd, req_wr = IO.pipe
		resp_rd, resp_wr = IO.pipe

		File.executable? path_to_server \
			or raise "Not found: #{path_to_server}"

		# TODO why do we fork twice? i don't think we need to...

		outer_pid = fork do

			at_exit { exit! }

			ctl_rd.close

			inner_pid = fork do

				$stdin.reopen req_rd
				$stdout.reopen resp_wr
				#$stderr.reopen "/dev/null", "w"

				req_rd.close
				resp_wr.close

				exec path_to_server

			end

			ctl_wr.puts inner_pid

			exit!

		end

		ctl_wr.close
		req_rd.close
		resp_wr.close

		inner_pid = ctl_rd.gets.strip.to_i
		ctl_rd.close

		Process.wait outer_pid

		at_exit do
			begin
				Process.kill "TERM", inner_pid
			rescue Errno::ESRCH
				# do nothing
			end
		end

		require "hq/engine/subprocess-rule-provider/self"

		client =
			SubProcessRuleProvider.new \
{'fraction_non_alphanumeric': 0.08225324027916252, 'fraction_numerical': 0.0, 'mean_word_length': 2.8374760994263863, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '2759372', 'n_tokens_mistral': 912, 'n_tokens_neox': 737, 'n_words': 180}