elelem-daemon 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,121 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Elelem
4
+ class Daemon
5
+ # Serves one connection in a forked process that leads its own process
6
+ # group. Turns run on the main thread, one at a time; a reader thread
7
+ # queues prompts and passes on answers. An interrupt signals the whole
8
+ # group, so it stops a running command as Ctrl-C does in a terminal.
9
+ class Worker
10
+ TTY_ONLY = %w[shell].freeze
11
+
12
+ def initialize(socket, provider:, status:)
13
+ @connection = Connection.new(socket, on_hang_up: -> { Process.kill("TERM", 0) })
14
+ @provider = provider
15
+ @status = status
16
+ @prompts = Queue.new
17
+ end
18
+
19
+ # Never returns: a worker must not run the daemon's own cleanup.
20
+ def run
21
+ Process.setpgrp
22
+ %w[INT TERM].each { |signal| trap(signal, "DEFAULT") } # Ctrl-C raises Interrupt, as in a terminal
23
+ $stdin.reopen(File::NULL)
24
+ serve
25
+ rescue StandardError, ScriptError => e
26
+ @connection.write(type: "error", text: e.message)
27
+ rescue SystemExit, SignalException
28
+ nil
29
+ ensure
30
+ $stdout.flush
31
+ $stderr.flush
32
+ exit!(0)
33
+ end
34
+
35
+ private
36
+
37
+ def serve
38
+ @connection.write(type: "hello", protocol: PROTOCOL)
39
+ request = @connection.read or return
40
+ return @connection.write(@status) if request["status"]
41
+
42
+ open_session(request)
43
+ @prompts << request["prompt"].to_s if request.key?("prompt")
44
+ Thread.new { read_messages }
45
+ turns
46
+ end
47
+
48
+ def open_session(request)
49
+ cwd = folder(request["cwd"])
50
+ ENV.update(request["env"].to_h { |name, value| [name.to_s, value&.to_s] }) if request["env"].is_a?(Hash)
51
+ Dir.chdir(cwd)
52
+ @terminal = Terminal.new(@connection, interactive: request["interactive"] == true)
53
+ options = { model: request["model"] }.compact
54
+ @session = Config.build_session(request["provider"] || @provider,
55
+ input: @terminal, output: @terminal, provider_options: options)
56
+ end
57
+
58
+ def folder(cwd)
59
+ return cwd if cwd.is_a?(String) && File.absolute_path?(cwd) && File.directory?(cwd)
60
+
61
+ raise ArgumentError, "cwd must be the absolute path of a folder"
62
+ end
63
+
64
+ # The end of input means the client is done sending, not gone: the
65
+ # turns already queued still run and reply.
66
+ def read_messages
67
+ while (message = next_message)
68
+ dispatch(message)
69
+ end
70
+ ensure
71
+ @prompts.close
72
+ end
73
+
74
+ def next_message
75
+ @connection.read
76
+ rescue ArgumentError => e
77
+ @connection.write(type: "error", text: e.message)
78
+ retry
79
+ end
80
+
81
+ def dispatch(message)
82
+ # A missing answer, as at Ctrl-D, stays nil and refuses.
83
+ if message.key?("answer") then @terminal.answer(message["id"], message["answer"]&.to_s)
84
+ elsif message["interrupt"] then Process.kill("INT", 0)
85
+ elsif message.key?("prompt") then @prompts << message["prompt"].to_s
86
+ end
87
+ end
88
+
89
+ def turns
90
+ loop do
91
+ text = @prompts.pop or break
92
+ turn(text)
93
+ rescue Interrupt
94
+ next # sent while no turn was running
95
+ end
96
+ end
97
+
98
+ def turn(text)
99
+ reply = text.start_with?("/") ? command(text) : @session.turn(text)
100
+ done(reply: reply.to_s)
101
+ rescue Interrupt
102
+ done(interrupted: true)
103
+ rescue StandardError, ScriptError => e
104
+ @connection.write(type: "error", text: e.message)
105
+ done
106
+ end
107
+
108
+ def done(reply: "", interrupted: false)
109
+ @connection.write(type: "done", reply: reply, interrupted: interrupted)
110
+ end
111
+
112
+ def command(line)
113
+ name, args = line.delete_prefix("/").split(" ", 2)
114
+ return @terminal.say("/#{name} needs a terminal; run elelem there instead.") if TTY_ONLY.include?(name)
115
+ return if @session.commands.run(name, args)
116
+
117
+ @terminal.say(@session.commands.names.join(" "))
118
+ end
119
+ end
120
+ end
121
+ end
@@ -0,0 +1,151 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "elelem"
4
+ require "fileutils"
5
+ require "json"
6
+ require "socket"
7
+
8
+ require_relative "daemon/version"
9
+ require_relative "daemon/connection"
10
+ require_relative "daemon/terminal"
11
+ require_relative "daemon/worker"
12
+ require_relative "daemon/client"
13
+
14
+ module Elelem
15
+ # One always-on elelem per user. It listens on a Unix socket and forks a
16
+ # worker for each connection. The worker moves to the folder the client
17
+ # names, so that folder's plugins and permissions apply, and a crash or a
18
+ # plugin in one folder cannot reach another. This process never loads
19
+ # plugins: a worker would then skip its own folder's.
20
+ class Daemon
21
+ MAX_WORKERS = 16
22
+ PROTOCOL = 1
23
+ STOP_WAIT = 10
24
+
25
+ def self.socket_path
26
+ ENV.fetch("ELELEM_SOCKET") do
27
+ runtime = ENV.fetch("XDG_RUNTIME_DIR", nil)
28
+ File.join(runtime ? File.join(runtime, "elelem") : Elelem.state_dir, "elelemd.sock")
29
+ end
30
+ end
31
+
32
+ attr_reader :path
33
+
34
+ def initialize(path: self.class.socket_path, provider: ENV.fetch("ELELEM_PROVIDER", "demo"),
35
+ max_workers: MAX_WORKERS)
36
+ @path = path
37
+ @provider = provider
38
+ @max_workers = max_workers
39
+ @workers = {}
40
+ end
41
+
42
+ # Runs until TERM or INT, which Ruby raises here as an exception.
43
+ def start
44
+ listen
45
+ $stdout.puts "elelemd v#{VERSION} listening on #{@path}"
46
+ $stdout.flush
47
+ loop { serve(@server.accept) }
48
+ rescue SignalException
49
+ nil
50
+ ensure
51
+ stop
52
+ end
53
+
54
+ def listen
55
+ FileUtils.mkdir_p(File.dirname(@path), mode: 0o700)
56
+ lock
57
+ FileUtils.rm_f(@path) # a socket left by a daemon that died; the lock says none is running
58
+ @server = private_umask { UNIXServer.new(@path) }
59
+ preload
60
+ end
61
+
62
+ def stop
63
+ @server&.close
64
+ FileUtils.rm_f(@path) if @server
65
+ stop_workers
66
+ @lock&.close
67
+ end
68
+
69
+ private
70
+
71
+ # Only one daemon may own the socket. The lock goes with the process, so
72
+ # a daemon that was killed never blocks the next one.
73
+ def lock
74
+ @lock = File.open("#{@path}.lock", File::RDWR | File::CREAT, 0o600)
75
+ raise "elelemd is already running on #{@path}" unless @lock.flock(File::LOCK_EX | File::LOCK_NB)
76
+ end
77
+
78
+ def private_umask
79
+ old = File.umask(0o177)
80
+ yield
81
+ ensure
82
+ File.umask(old)
83
+ end
84
+
85
+ # Loads the provider's code, but not plugins, so workers share it.
86
+ def preload
87
+ require "elelem/#{@provider}"
88
+ rescue LoadError
89
+ nil # a provider from a plugin, or a typo the worker will report
90
+ end
91
+
92
+ def serve(socket)
93
+ return socket.close unless same_user?(socket)
94
+
95
+ reap
96
+ return refuse(socket) if @workers.size >= @max_workers
97
+
98
+ spawn(socket)
99
+ ensure
100
+ socket.close unless socket.closed?
101
+ end
102
+
103
+ def same_user?(socket)
104
+ socket.getpeereid.first == Process.euid
105
+ end
106
+
107
+ def refuse(socket)
108
+ Connection.new(socket).write(type: "error", text: "busy: #{@max_workers} workers are running")
109
+ end
110
+
111
+ def spawn(socket)
112
+ status = { type: "status", pid: Process.pid, workers: @workers.size + 1, version: VERSION }
113
+ pid = fork do
114
+ @server.close
115
+ @lock.close
116
+ Worker.new(socket, provider: @provider, status: status).run
117
+ end
118
+ lead_group(pid)
119
+ @workers[pid] = Process.detach(pid)
120
+ end
121
+
122
+ # The worker does this too; whichever runs first wins, so a signal sent
123
+ # right away still reaches the whole group.
124
+ def lead_group(pid)
125
+ Process.setpgid(pid, pid)
126
+ rescue SystemCallError
127
+ nil
128
+ end
129
+
130
+ def reap
131
+ @workers.select! { |_, waiter| waiter.alive? }
132
+ end
133
+
134
+ def stop_workers
135
+ signal_workers("TERM")
136
+ deadline = Process.clock_gettime(Process::CLOCK_MONOTONIC) + STOP_WAIT
137
+ @workers.each_value { |waiter| waiter.join([deadline - Process.clock_gettime(Process::CLOCK_MONOTONIC), 0].max) }
138
+ signal_workers("KILL")
139
+ end
140
+
141
+ # A worker leads its own process group, so this reaches its commands too.
142
+ def signal_workers(signal)
143
+ reap
144
+ @workers.each_key do |pid|
145
+ Process.kill(signal, -pid)
146
+ rescue Errno::ESRCH, Errno::EPERM
147
+ nil
148
+ end
149
+ end
150
+ end
151
+ end
@@ -0,0 +1,12 @@
1
+ {
2
+ "packages": {
3
+ ".": {
4
+ "release-type": "ruby",
5
+ "package-name": "elelem-daemon",
6
+ "version-file": "lib/elelem/daemon/version.rb",
7
+ "bump-minor-pre-major": true,
8
+ "initial-version": "0.1.0",
9
+ "include-component-in-tag": false
10
+ }
11
+ }
12
+ }
@@ -0,0 +1,88 @@
1
+ # frozen_string_literal: true
2
+
3
+ RSpec.describe Elelem::Daemon::Client do
4
+ # A person at a terminal who gives these answers, then ends input.
5
+ let(:person_class) do
6
+ Class.new do
7
+ attr_reader :asked
8
+
9
+ def initialize(*answers)
10
+ @answers = answers
11
+ @asked = []
12
+ end
13
+
14
+ def interactive? = true
15
+ def remember(_line) = nil
16
+
17
+ def ask(prompt)
18
+ @asked << prompt
19
+ @answers.shift
20
+ end
21
+ end
22
+ end
23
+
24
+ around do |example|
25
+ Dir.mktmpdir("elelemd") do |dir|
26
+ @path = File.join(dir, "elelemd.sock")
27
+ @folder = File.join(dir, "app")
28
+ FileUtils.mkdir_p(File.join(@folder, ".agents", "plugins"))
29
+ example.run
30
+ end
31
+ end
32
+
33
+ before do
34
+ allow($stdout).to receive(:puts)
35
+ daemon = Elelem::Daemon.new(path: @path)
36
+ @thread = Thread.new { daemon.start }
37
+ Thread.pass until File.socket?(@path)
38
+ end
39
+
40
+ after { @thread.kill.join }
41
+
42
+ let(:screen) { StringIO.new }
43
+
44
+ def client(input = Elelem::NullInput.new)
45
+ described_class.new(path: @path, input: input, output: Elelem::Output.new(stream: screen))
46
+ end
47
+
48
+ it "sends one prompt for this folder and returns the reply" do
49
+ expect(client.ask("pwd", cwd: @folder)).to eq("exit 0")
50
+ expect(screen.string).to include("execute", File.realpath(@folder))
51
+ end
52
+
53
+ it "sends this process's environment" do
54
+ ENV["SPEC_GREETING"] = "howdy"
55
+ client.ask("echo $SPEC_GREETING", cwd: @folder)
56
+ expect(screen.string).to include("howdy")
57
+ ensure
58
+ ENV.delete("SPEC_GREETING")
59
+ end
60
+
61
+ it "holds a conversation, one prompt per line" do
62
+ client(person_class.new("true", "", "false", nil)).chat(cwd: @folder)
63
+ expect(screen.string).to include("exit 0", "exit 1")
64
+ end
65
+
66
+ it "answers approval prompts with the person's answer" do
67
+ File.write(File.join(@folder, ".agents", "plugins", "ask.rb"), <<~RUBY)
68
+ Elelem.configure do |config|
69
+ config.setup(:ask) do |session|
70
+ session.commands.register("ask", description: "spec") { session.output.say("got " + session.input.ask("Allow?").inspect) }
71
+ end
72
+ end
73
+ RUBY
74
+ person = person_class.new("/ask", "y", nil)
75
+ client(person).chat(cwd: @folder)
76
+ expect(person.asked).to eq(["> ", "Allow?", "> "])
77
+ expect(screen.string).to include('got "y"')
78
+ end
79
+
80
+ it "shows the daemon's error" do
81
+ expect { client.ask("pwd", cwd: @folder, provider: "nope") }.to raise_error(RuntimeError, /unknown provider/)
82
+ end
83
+
84
+ it "says when the daemon is not running" do
85
+ @thread.kill.join
86
+ expect { client.ask("pwd") }.to raise_error(RuntimeError, /elelemd is not running/)
87
+ end
88
+ end
@@ -0,0 +1,40 @@
1
+ # frozen_string_literal: true
2
+
3
+ RSpec.describe Elelem::Daemon::Connection do
4
+ let(:pair) { UNIXSocket.pair }
5
+ let(:ours) { described_class.new(pair[0]) }
6
+ let(:theirs) { pair[1] }
7
+
8
+ after { pair.each(&:close) }
9
+
10
+ it "writes one JSON object per line" do
11
+ ours.write(type: "done", reply: "a\nb")
12
+ expect(theirs.gets).to eq(%({"type":"done","reply":"a\\nb"}\n))
13
+ end
14
+
15
+ it "reads objects until the end" do
16
+ theirs.write(%({"prompt":"hi"}\n{"interrupt":true}))
17
+ theirs.close_write
18
+ expect([ours.read, ours.read, ours.read]).to eq([{ "prompt" => "hi" }, { "interrupt" => true }, nil])
19
+ end
20
+
21
+ it "refuses a line that is not a JSON object" do
22
+ theirs.write("[1]\nnope\n")
23
+ expect { ours.read }.to raise_error(ArgumentError, "expected a JSON object")
24
+ expect { ours.read }.to raise_error(ArgumentError, "expected a JSON object")
25
+ end
26
+
27
+ it "refuses a line that is too long" do
28
+ stub_const("#{described_class}::LINE_LIMIT", 8)
29
+ theirs.write(%({"prompt":"far too long"}\n))
30
+ expect { ours.read }.to raise_error(ArgumentError, /longer than 8 bytes/)
31
+ end
32
+
33
+ it "tells its owner when the other end has gone" do
34
+ hung_up = false
35
+ connection = described_class.new(pair[0], on_hang_up: -> { hung_up = true })
36
+ theirs.close
37
+ connection.write(type: "content", text: "x" * 100_000)
38
+ expect(hung_up).to be(true)
39
+ end
40
+ end
@@ -0,0 +1,44 @@
1
+ # frozen_string_literal: true
2
+
3
+ RSpec.describe Elelem::Daemon::Terminal do
4
+ let(:events) { [] }
5
+ let(:connection) { instance_double(Elelem::Daemon::Connection).tap { |c| allow(c).to receive(:write) { events << it } } }
6
+
7
+ def terminal(interactive: true) = described_class.new(connection, interactive:)
8
+
9
+ it "always reports itself interactive, so approvals are asked for, not skipped" do
10
+ expect(terminal(interactive: false)).to be_interactive
11
+ end
12
+
13
+ it "sends text as plain content" do
14
+ terminal.say("\e[36mhi\e[0m", as: :markdown)
15
+ terminal.print("more")
16
+ terminal.thinking("hmm")
17
+ terminal.say(" ")
18
+ expect(events).to eq([{ type: "content", text: "hi", as: "markdown" }, { type: "content", text: "more" },
19
+ { type: "thinking", text: "hmm" }])
20
+ end
21
+
22
+ it "sends tool calls as their own event" do
23
+ terminal.doing("execute", { "command" => "ls" }, state: "x")
24
+ expect(events).to eq([{ type: "tool", name: "execute", args: { "command" => "ls" }, state: "x" }])
25
+ end
26
+
27
+ it "waits for the answer to a prompt" do
28
+ t = terminal
29
+ answer = Thread.new { t.ask("Allow?") }
30
+ Thread.pass until events.any?
31
+ t.answer(events.last[:id], "y")
32
+ expect(answer.value).to eq("y")
33
+ expect(events).to eq([{ type: "prompt", id: 1, text: "Allow?" }])
34
+ end
35
+
36
+ it "asks nothing of a client that cannot answer, which denies" do
37
+ expect(terminal(interactive: false).ask("Allow?")).to be_nil
38
+ expect(events).to be_empty
39
+ end
40
+
41
+ it "ignores an answer to no open prompt" do
42
+ expect { terminal.answer(7, "y") }.not_to raise_error
43
+ end
44
+ end