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.
- checksums.yaml +7 -0
- data/.agents/backlog/001-named-sessions.md +19 -0
- data/.agents/backlog/002-agent-to-agent.md +23 -0
- data/.github/workflows/release.yml +63 -0
- data/.release-please-manifest.json +3 -0
- data/.rspec +1 -0
- data/.rubocop.yml +62 -0
- data/CHANGELOG.md +10 -0
- data/LICENSE.txt +21 -0
- data/README.md +106 -0
- data/Rakefile +8 -0
- data/exe/elelemd +54 -0
- data/lib/elelem/daemon/client.rb +76 -0
- data/lib/elelem/daemon/connection.rb +34 -0
- data/lib/elelem/daemon/terminal.rb +72 -0
- data/lib/elelem/daemon/version.rb +7 -0
- data/lib/elelem/daemon/worker.rb +121 -0
- data/lib/elelem/daemon.rb +151 -0
- data/release-please-config.json +12 -0
- data/spec/elelem/daemon/client_spec.rb +88 -0
- data/spec/elelem/daemon/connection_spec.rb +40 -0
- data/spec/elelem/daemon/terminal_spec.rb +44 -0
- data/spec/elelem/daemon_spec.rb +253 -0
- data/spec/spec_helper.rb +12 -0
- metadata +102 -0
|
@@ -0,0 +1,253 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
RSpec.describe Elelem::Daemon do
|
|
4
|
+
around do |example|
|
|
5
|
+
Dir.mktmpdir("elelemd") do |dir|
|
|
6
|
+
@dir = dir
|
|
7
|
+
example.run
|
|
8
|
+
end
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
let(:path) { File.join(@dir, "run", "elelemd.sock") }
|
|
12
|
+
|
|
13
|
+
def start(**)
|
|
14
|
+
@daemon = described_class.new(path: path, **)
|
|
15
|
+
allow($stdout).to receive(:puts)
|
|
16
|
+
@thread = Thread.new { @daemon.start }
|
|
17
|
+
Thread.pass until File.socket?(path)
|
|
18
|
+
@daemon
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
after do
|
|
22
|
+
@thread&.kill&.join
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
# A socket left behind can exist before the daemon listens on it.
|
|
26
|
+
def connect
|
|
27
|
+
UNIXSocket.new(path)
|
|
28
|
+
rescue Errno::ECONNREFUSED, Errno::ENOENT
|
|
29
|
+
Thread.pass
|
|
30
|
+
retry
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
def send_line(socket, message) = socket.write("#{JSON.generate(message)}\n")
|
|
34
|
+
def read_event(socket) = socket.gets&.then { JSON.parse(it) }
|
|
35
|
+
|
|
36
|
+
# The events after hello, up to the first done or the end.
|
|
37
|
+
def events_until_done(socket)
|
|
38
|
+
[].tap do |events|
|
|
39
|
+
while (event = read_event(socket))
|
|
40
|
+
events << event
|
|
41
|
+
break if event["type"] == "done"
|
|
42
|
+
end
|
|
43
|
+
end
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
def request(message)
|
|
47
|
+
socket = connect
|
|
48
|
+
read_event(socket)
|
|
49
|
+
send_line(socket, message)
|
|
50
|
+
events_until_done(socket)
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
def folder(name, plugin: nil)
|
|
54
|
+
File.join(@dir, name).tap do |dir|
|
|
55
|
+
FileUtils.mkdir_p(File.join(dir, ".agents", "plugins"))
|
|
56
|
+
File.write(File.join(dir, ".agents", "plugins", "plugin.rb"), plugin) if plugin
|
|
57
|
+
end
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
def command_plugin(name, body)
|
|
61
|
+
<<~RUBY
|
|
62
|
+
Elelem.configure do |config|
|
|
63
|
+
config.setup(:#{name}) do |session|
|
|
64
|
+
session.commands.register("#{name}", description: "spec") { #{body} }
|
|
65
|
+
end
|
|
66
|
+
end
|
|
67
|
+
RUBY
|
|
68
|
+
end
|
|
69
|
+
|
|
70
|
+
describe "the socket" do
|
|
71
|
+
it "is private to its owner" do
|
|
72
|
+
start
|
|
73
|
+
expect(File.stat(path).mode & 0o777).to eq(0o600)
|
|
74
|
+
expect(File.stat(File.dirname(path)).mode & 0o777).to eq(0o700)
|
|
75
|
+
end
|
|
76
|
+
|
|
77
|
+
it "replaces a socket left by a daemon that died" do
|
|
78
|
+
FileUtils.mkdir_p(File.dirname(path))
|
|
79
|
+
UNIXServer.new(path).close
|
|
80
|
+
start
|
|
81
|
+
expect(request(status: true).first).to include("type" => "status")
|
|
82
|
+
end
|
|
83
|
+
|
|
84
|
+
it "refuses to start while another daemon owns the socket" do
|
|
85
|
+
start
|
|
86
|
+
expect { described_class.new(path: path).listen }.to raise_error(RuntimeError, /already running/)
|
|
87
|
+
end
|
|
88
|
+
|
|
89
|
+
it "removes the socket when it stops" do
|
|
90
|
+
start
|
|
91
|
+
@thread.kill.join
|
|
92
|
+
expect(File.exist?(path)).to be(false)
|
|
93
|
+
end
|
|
94
|
+
|
|
95
|
+
it "hangs up on a process of another user" do
|
|
96
|
+
start
|
|
97
|
+
allow_any_instance_of(UNIXSocket).to receive(:getpeereid).and_return([Process.euid + 1, 0])
|
|
98
|
+
expect(connect.gets).to be_nil
|
|
99
|
+
end
|
|
100
|
+
end
|
|
101
|
+
|
|
102
|
+
describe "a request" do
|
|
103
|
+
it "says hello with the protocol first" do
|
|
104
|
+
start
|
|
105
|
+
expect(read_event(connect)).to eq("type" => "hello", "protocol" => 1)
|
|
106
|
+
end
|
|
107
|
+
|
|
108
|
+
it "reports status" do
|
|
109
|
+
start
|
|
110
|
+
expect(request(status: true)).to eq([{ "type" => "status", "pid" => Process.pid, "workers" => 1,
|
|
111
|
+
"version" => Elelem::Daemon::VERSION }])
|
|
112
|
+
end
|
|
113
|
+
|
|
114
|
+
it "runs a turn in the folder it names and replies once with done" do
|
|
115
|
+
start
|
|
116
|
+
dir = folder("app")
|
|
117
|
+
events = request(cwd: dir, prompt: "pwd")
|
|
118
|
+
expect(events).to include({ "type" => "content", "text" => "#{File.realpath(dir)}\n" })
|
|
119
|
+
expect(events.last).to eq("type" => "done", "reply" => "exit 0", "interrupted" => false)
|
|
120
|
+
expect(events.count { it["type"] == "done" }).to eq(1)
|
|
121
|
+
end
|
|
122
|
+
|
|
123
|
+
it "loads each folder's own plugins, and never in the daemon" do
|
|
124
|
+
start
|
|
125
|
+
app = folder("app", plugin: command_plugin("who", 'session.output.say("app")'))
|
|
126
|
+
lib = folder("lib", plugin: command_plugin("who", 'session.output.say("lib")'))
|
|
127
|
+
expect(request(cwd: app, prompt: "/who").first).to include("text" => "app")
|
|
128
|
+
expect(request(cwd: lib, prompt: "/who").first).to include("text" => "lib")
|
|
129
|
+
expect(Elelem::Config.names).not_to include("who")
|
|
130
|
+
end
|
|
131
|
+
|
|
132
|
+
it "uses the environment the client sends" do
|
|
133
|
+
start
|
|
134
|
+
events = request(cwd: folder("app"), prompt: "echo $SPEC_GREETING", env: { "SPEC_GREETING" => "howdy" })
|
|
135
|
+
expect(events).to include({ "type" => "content", "text" => "howdy\n" })
|
|
136
|
+
end
|
|
137
|
+
|
|
138
|
+
it "refuses a folder that is not an absolute path to a directory" do
|
|
139
|
+
start
|
|
140
|
+
expect(request(cwd: "relative", prompt: "pwd")).to eq([{ "type" => "error",
|
|
141
|
+
"text" => "cwd must be the absolute path of a folder" }])
|
|
142
|
+
end
|
|
143
|
+
|
|
144
|
+
it "reports an unknown provider" do
|
|
145
|
+
start
|
|
146
|
+
expect(request(cwd: folder("app"), prompt: "pwd", provider: "nope").first["text"]).to match(/unknown provider/)
|
|
147
|
+
end
|
|
148
|
+
|
|
149
|
+
it "keeps the conversation across prompts on one connection" do
|
|
150
|
+
start
|
|
151
|
+
socket = connect
|
|
152
|
+
read_event(socket)
|
|
153
|
+
send_line(socket, cwd: folder("app"), prompt: "true")
|
|
154
|
+
events_until_done(socket)
|
|
155
|
+
send_line(socket, prompt: "false")
|
|
156
|
+
expect(events_until_done(socket).last).to include("reply" => "exit 1")
|
|
157
|
+
end
|
|
158
|
+
|
|
159
|
+
it "finishes the queued prompts after the client stops sending" do
|
|
160
|
+
start
|
|
161
|
+
socket = connect
|
|
162
|
+
read_event(socket)
|
|
163
|
+
send_line(socket, cwd: folder("app"), prompt: "true")
|
|
164
|
+
send_line(socket, prompt: "false")
|
|
165
|
+
socket.close_write
|
|
166
|
+
replies = Array.new(2) { events_until_done(socket).last["reply"] }
|
|
167
|
+
expect(replies).to eq(["exit 0", "exit 1"])
|
|
168
|
+
expect(socket.gets).to be_nil
|
|
169
|
+
end
|
|
170
|
+
end
|
|
171
|
+
|
|
172
|
+
describe "prompts" do
|
|
173
|
+
let(:asking) { command_plugin("ask", 'session.output.say("got " + session.input.ask("Allow?").inspect)') }
|
|
174
|
+
|
|
175
|
+
it "asks an interactive client and uses its answer" do
|
|
176
|
+
start
|
|
177
|
+
socket = connect
|
|
178
|
+
read_event(socket)
|
|
179
|
+
send_line(socket, cwd: folder("app", plugin: asking), prompt: "/ask", interactive: true)
|
|
180
|
+
prompt = read_event(socket)
|
|
181
|
+
expect(prompt).to include("type" => "prompt", "text" => "Allow?")
|
|
182
|
+
send_line(socket, id: prompt["id"], answer: "y")
|
|
183
|
+
expect(read_event(socket)).to include("text" => 'got "y"')
|
|
184
|
+
end
|
|
185
|
+
|
|
186
|
+
it "denies, never approves, when the client cannot answer" do
|
|
187
|
+
start
|
|
188
|
+
expect(request(cwd: folder("app", plugin: asking), prompt: "/ask").first).to include("text" => "got nil")
|
|
189
|
+
end
|
|
190
|
+
|
|
191
|
+
it "passes on a missing answer as a refusal" do
|
|
192
|
+
start
|
|
193
|
+
socket = connect
|
|
194
|
+
read_event(socket)
|
|
195
|
+
send_line(socket, cwd: folder("app", plugin: asking), prompt: "/ask", interactive: true)
|
|
196
|
+
send_line(socket, id: read_event(socket)["id"], answer: nil)
|
|
197
|
+
expect(read_event(socket)).to include("text" => "got nil")
|
|
198
|
+
end
|
|
199
|
+
end
|
|
200
|
+
|
|
201
|
+
describe "an interrupt" do
|
|
202
|
+
it "stops the turn and the command it is running" do
|
|
203
|
+
start
|
|
204
|
+
socket = connect
|
|
205
|
+
read_event(socket)
|
|
206
|
+
send_line(socket, cwd: folder("app"), prompt: "sleep 31.5")
|
|
207
|
+
Thread.pass until read_event(socket)["type"] == "tool"
|
|
208
|
+
started = Process.clock_gettime(Process::CLOCK_MONOTONIC)
|
|
209
|
+
sleep 0.2 # let the shell start
|
|
210
|
+
send_line(socket, interrupt: true)
|
|
211
|
+
expect(events_until_done(socket).last).to include("interrupted" => true)
|
|
212
|
+
expect(Process.clock_gettime(Process::CLOCK_MONOTONIC) - started).to be < 5
|
|
213
|
+
expect(IO.popen(["pgrep", "-f", "sleep 31.5"], &:read)).to be_empty
|
|
214
|
+
end
|
|
215
|
+
|
|
216
|
+
it "leaves the connection ready for the next prompt" do
|
|
217
|
+
start
|
|
218
|
+
socket = connect
|
|
219
|
+
read_event(socket)
|
|
220
|
+
send_line(socket, cwd: folder("app"))
|
|
221
|
+
send_line(socket, interrupt: true)
|
|
222
|
+
send_line(socket, prompt: "true")
|
|
223
|
+
expect(events_until_done(socket).last).to include("reply" => "exit 0")
|
|
224
|
+
end
|
|
225
|
+
end
|
|
226
|
+
|
|
227
|
+
describe "workers" do
|
|
228
|
+
it "turns away a connection over the limit" do
|
|
229
|
+
start(max_workers: 1)
|
|
230
|
+
waiting = connect
|
|
231
|
+
read_event(waiting)
|
|
232
|
+
expect(read_event(connect)).to eq("type" => "error", "text" => "busy: 1 workers are running")
|
|
233
|
+
end
|
|
234
|
+
|
|
235
|
+
it "keep the daemon running when one exits" do
|
|
236
|
+
start
|
|
237
|
+
expect(request(cwd: folder("app"), prompt: "/exit")).to be_empty
|
|
238
|
+
expect(request(status: true).first).to include("type" => "status")
|
|
239
|
+
end
|
|
240
|
+
|
|
241
|
+
it "are stopped with the daemon" do
|
|
242
|
+
start
|
|
243
|
+
socket = connect
|
|
244
|
+
read_event(socket)
|
|
245
|
+
send_line(socket, cwd: folder("app"), prompt: "sleep 32.5")
|
|
246
|
+
Thread.pass until read_event(socket)["type"] == "tool"
|
|
247
|
+
sleep 0.2
|
|
248
|
+
@thread.kill.join
|
|
249
|
+
expect(socket.gets).to be_nil
|
|
250
|
+
expect(IO.popen(["pgrep", "-f", "sleep 32.5"], &:read)).to be_empty
|
|
251
|
+
end
|
|
252
|
+
end
|
|
253
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: elelem-daemon
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.1.0
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- mo khan
|
|
8
|
+
bindir: exe
|
|
9
|
+
cert_chain: []
|
|
10
|
+
date: 1980-01-02 00:00:00.000000000 Z
|
|
11
|
+
dependencies:
|
|
12
|
+
- !ruby/object:Gem::Dependency
|
|
13
|
+
name: elelem
|
|
14
|
+
requirement: !ruby/object:Gem::Requirement
|
|
15
|
+
requirements:
|
|
16
|
+
- - ">="
|
|
17
|
+
- !ruby/object:Gem::Version
|
|
18
|
+
version: '0.16'
|
|
19
|
+
- - "<"
|
|
20
|
+
- !ruby/object:Gem::Version
|
|
21
|
+
version: '1'
|
|
22
|
+
type: :runtime
|
|
23
|
+
prerelease: false
|
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
25
|
+
requirements:
|
|
26
|
+
- - ">="
|
|
27
|
+
- !ruby/object:Gem::Version
|
|
28
|
+
version: '0.16'
|
|
29
|
+
- - "<"
|
|
30
|
+
- !ruby/object:Gem::Version
|
|
31
|
+
version: '1'
|
|
32
|
+
- !ruby/object:Gem::Dependency
|
|
33
|
+
name: json
|
|
34
|
+
requirement: !ruby/object:Gem::Requirement
|
|
35
|
+
requirements:
|
|
36
|
+
- - ">="
|
|
37
|
+
- !ruby/object:Gem::Version
|
|
38
|
+
version: '2.18'
|
|
39
|
+
type: :runtime
|
|
40
|
+
prerelease: false
|
|
41
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
42
|
+
requirements:
|
|
43
|
+
- - ">="
|
|
44
|
+
- !ruby/object:Gem::Version
|
|
45
|
+
version: '2.18'
|
|
46
|
+
description: An always-on elelem you talk to over a Unix socket.
|
|
47
|
+
email:
|
|
48
|
+
- mo@mokhan.ca
|
|
49
|
+
executables:
|
|
50
|
+
- elelemd
|
|
51
|
+
extensions: []
|
|
52
|
+
extra_rdoc_files: []
|
|
53
|
+
files:
|
|
54
|
+
- ".agents/backlog/001-named-sessions.md"
|
|
55
|
+
- ".agents/backlog/002-agent-to-agent.md"
|
|
56
|
+
- ".github/workflows/release.yml"
|
|
57
|
+
- ".release-please-manifest.json"
|
|
58
|
+
- ".rspec"
|
|
59
|
+
- ".rubocop.yml"
|
|
60
|
+
- CHANGELOG.md
|
|
61
|
+
- LICENSE.txt
|
|
62
|
+
- README.md
|
|
63
|
+
- Rakefile
|
|
64
|
+
- exe/elelemd
|
|
65
|
+
- lib/elelem/daemon.rb
|
|
66
|
+
- lib/elelem/daemon/client.rb
|
|
67
|
+
- lib/elelem/daemon/connection.rb
|
|
68
|
+
- lib/elelem/daemon/terminal.rb
|
|
69
|
+
- lib/elelem/daemon/version.rb
|
|
70
|
+
- lib/elelem/daemon/worker.rb
|
|
71
|
+
- release-please-config.json
|
|
72
|
+
- spec/elelem/daemon/client_spec.rb
|
|
73
|
+
- spec/elelem/daemon/connection_spec.rb
|
|
74
|
+
- spec/elelem/daemon/terminal_spec.rb
|
|
75
|
+
- spec/elelem/daemon_spec.rb
|
|
76
|
+
- spec/spec_helper.rb
|
|
77
|
+
homepage: https://elelem.sh
|
|
78
|
+
licenses:
|
|
79
|
+
- MIT
|
|
80
|
+
metadata:
|
|
81
|
+
allowed_push_host: https://rubygems.org
|
|
82
|
+
homepage_uri: https://elelem.sh
|
|
83
|
+
source_code_uri: https://github.com/elelem-sh/daemon
|
|
84
|
+
rubygems_mfa_required: 'true'
|
|
85
|
+
rdoc_options: []
|
|
86
|
+
require_paths:
|
|
87
|
+
- lib
|
|
88
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
89
|
+
requirements:
|
|
90
|
+
- - ">="
|
|
91
|
+
- !ruby/object:Gem::Version
|
|
92
|
+
version: 4.0.0
|
|
93
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
94
|
+
requirements:
|
|
95
|
+
- - ">="
|
|
96
|
+
- !ruby/object:Gem::Version
|
|
97
|
+
version: '0'
|
|
98
|
+
requirements: []
|
|
99
|
+
rubygems_version: 4.0.20
|
|
100
|
+
specification_version: 4
|
|
101
|
+
summary: An always-on elelem you talk to over a Unix socket.
|
|
102
|
+
test_files: []
|