brainiac 0.6.0 → 0.7.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,207 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "json"
4
+ require "net/http"
5
+ require "socket"
6
+ require "uri"
7
+
8
+ module Brainiac
9
+ module Hub
10
+ # This machine's side of the hub contract: check in, hold or give up the
11
+ # leader role, verify deliveries, and pick up work that queued while away.
12
+ class Node
13
+ TUNNEL_SETTLE_SECONDS = 5
14
+
15
+ attr_reader :settings, :ledger
16
+ attr_accessor :client, :tunnel, :last_ok
17
+
18
+ def initialize(config = {}, client: nil)
19
+ @settings = Settings.new(config)
20
+ @ledger = Ledger.new(@settings)
21
+ @client = client
22
+ @mutex = Mutex.new
23
+ @leader = false
24
+ @sent_claims = {}
25
+ @wake = Queue.new
26
+ @leader_events = Queue.new
27
+ end
28
+
29
+ def enabled? = settings.enabled?
30
+
31
+ def public_url
32
+ settings.public_url.empty? ? tunnel&.url : settings.public_url
33
+ end
34
+
35
+ # True when this machine should run the single-holder work (Discord
36
+ # gateways, cron). Always true without a hub.
37
+ #
38
+ # A leader that cannot reach the hub gives the role up on its own after two
39
+ # missed check-ins. The hub's lease lasts three, so the old leader has
40
+ # stopped before another machine can take over.
41
+ def leader?
42
+ return true unless enabled?
43
+
44
+ @mutex.synchronize { @leader && fresh? }
45
+ end
46
+
47
+ # Called once by the server after plugins are loaded.
48
+ def start(port:)
49
+ return unless enabled?
50
+
51
+ @port = port
52
+ @client ||= Client.new(url: settings.url, token: settings.token)
53
+ start_tunnel
54
+ start_leader_events
55
+ @thread = Thread.new { run_loop }
56
+ LOG.info "[Hub] Node '#{settings.machine_name}' -> #{settings.url} (check-in every #{settings.interval}s)"
57
+ end
58
+
59
+ # Clean shutdown: tell the hub we are gone so it stops pushing here and
60
+ # frees the leader lease straight away.
61
+ def stop
62
+ return unless enabled? && @client
63
+
64
+ @thread&.kill
65
+ tunnel&.stop
66
+ @client.delete("/nodes/#{settings.machine_name}")
67
+ update_leader(false)
68
+ rescue StandardError => e
69
+ LOG.warn "[Hub] Deregister failed: #{e.message}"
70
+ end
71
+
72
+ # One check-in. Returns the hub's answer, or nil if it did not go through.
73
+ def check_in
74
+ url = public_url
75
+ if url.to_s.empty?
76
+ LOG.warn "[Hub] No public URL yet (tunnel still starting?) — skipping check-in"
77
+ return step_down_if_stale
78
+ end
79
+
80
+ claims = pending_claims
81
+ response = @client.post("/nodes", check_in_payload(url, claims))
82
+ return step_down_if_stale unless response
83
+
84
+ claims.each { |key| @sent_claims[key] = true }
85
+ @mutex.synchronize { @last_ok = monotonic }
86
+ update_leader(response["leader"] == settings.machine_name)
87
+ replay(response["jobs"] || [])
88
+ response
89
+ end
90
+
91
+ # Ask for a check-in now instead of at the next tick (e.g. the tunnel URL changed).
92
+ def wake
93
+ @wake << :now
94
+ end
95
+
96
+ # Leader hooks can be slow (opening a gateway per bot), so they run on
97
+ # their own thread, in order, and never hold up a check-in.
98
+ def start_leader_events
99
+ @start_leader_events ||= Thread.new do
100
+ loop do
101
+ Brainiac.emit(@leader_events.pop, machine: settings.machine_name)
102
+ rescue StandardError => e
103
+ LOG.error "[Hub] Leader hook failed: #{e.message}"
104
+ end
105
+ end
106
+ end
107
+
108
+ private
109
+
110
+ def monotonic = Process.clock_gettime(Process::CLOCK_MONOTONIC)
111
+
112
+ def fresh?
113
+ @last_ok && (monotonic - @last_ok) < settings.interval * 2
114
+ end
115
+
116
+ def step_down_if_stale
117
+ update_leader(false) if @mutex.synchronize { @leader && !fresh? }
118
+ nil
119
+ end
120
+
121
+ def update_leader(value)
122
+ changed = @mutex.synchronize do
123
+ next false if @leader == value
124
+
125
+ @leader = value
126
+ true
127
+ end
128
+ return unless changed
129
+
130
+ LOG.info "[Hub] #{value ? "This machine is now the leader" : "No longer the leader"} (Discord gateways, cron)"
131
+ @leader_events << (value ? :hub_leader_acquired : :hub_leader_lost)
132
+ end
133
+
134
+ def check_in_payload(url, claims)
135
+ { name: settings.machine_name, url: url, priority: settings.priority, interval: settings.interval,
136
+ wants_leader: settings.wants_leader?, version: defined?(BRAINIAC_VERSION) ? BRAINIAC_VERSION : nil,
137
+ claims: claims }
138
+ end
139
+
140
+ def pending_claims
141
+ projects = defined?(PROJECTS) ? PROJECTS : {}
142
+ Claims.local_branch_keys(projects).reject { |key| @sent_claims[key] }.first(200)
143
+ end
144
+
145
+ def run_loop
146
+ wait_for_server
147
+ loop do
148
+ begin
149
+ check_in
150
+ rescue StandardError => e
151
+ LOG.error "[Hub] Check-in error: #{e.message}"
152
+ step_down_if_stale
153
+ end
154
+ @wake.pop(timeout: settings.interval)
155
+ @wake.clear
156
+ end
157
+ end
158
+
159
+ # The hub hands over queued work as soon as we check in, so the local
160
+ # server has to be accepting connections first.
161
+ def wait_for_server
162
+ 60.times do
163
+ TCPSocket.new("127.0.0.1", @port).close
164
+ return
165
+ rescue SystemCallError
166
+ sleep 1
167
+ end
168
+ end
169
+
170
+ def start_tunnel
171
+ return if settings.tunnel.nil? || settings.tunnel == false || !settings.public_url.empty?
172
+
173
+ @tunnel = Tunnel.from_config(settings.tunnel, port: @port) do |_url|
174
+ # Give the tunnel a moment to start routing before the hub pushes to it.
175
+ Thread.new do
176
+ sleep TUNNEL_SETTLE_SECONDS
177
+ wake
178
+ end
179
+ end
180
+ @tunnel.start
181
+ end
182
+
183
+ # Work that queued in the hub while this machine was away. It is posted to
184
+ # our own server so it takes the same path as a live delivery.
185
+ def replay(jobs)
186
+ jobs.each do |job|
187
+ response = post_locally(job)
188
+ LOG.info "[Hub] Picked up queued job #{job["id"]} for #{job["path"]} (#{response.code})"
189
+ rescue StandardError => e
190
+ LOG.error "[Hub] Could not replay queued job #{job["id"]}: #{e.message}"
191
+ end
192
+ end
193
+
194
+ def post_locally(job)
195
+ uri = URI("http://127.0.0.1:#{@port}#{job["path"]}")
196
+ request = Net::HTTP::Post.new(uri.request_uri)
197
+ (job["headers"] || {}).each { |name, value| request[name] = value }
198
+ request["X-Brainiac-Hub-Job"] = job["id"]
199
+ request["X-Brainiac-Hub-Timestamp"] = job["timestamp"].to_s
200
+ request["X-Brainiac-Hub-Signature"] = job["signature"].to_s
201
+ request["Content-Type"] ||= "application/json" # a form content type would let Rack consume the body
202
+ request.body = job["body"].to_s
203
+ Net::HTTP.start(uri.host, uri.port, open_timeout: 5, read_timeout: 10) { |http| http.request(request) }
204
+ end
205
+ end
206
+ end
207
+ end
@@ -0,0 +1,46 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "socket"
4
+
5
+ module Brainiac
6
+ module Hub
7
+ # The "hub" block of brainiac.json:
8
+ #
9
+ # "hub": {
10
+ # "url": "https://<api id>.execute-api.<region>.amazonaws.com/prod",
11
+ # "token": "<node token>", // or the BRAINIAC_HUB_TOKEN env var
12
+ # "machine": "mini", // default: this machine's hostname
13
+ # "priority": 10, // lower number = preferred for new work
14
+ # "leader": true, // may hold the Discord gateways and cron
15
+ # "interval": 120, // seconds between check-ins
16
+ # "tunnel": "cloudflared", // managed tunnel, or use "public_url"
17
+ # "public_url": "https://..." // fixed public URL for this machine
18
+ # }
19
+ class Settings
20
+ DEFAULT_INTERVAL = 120
21
+ DEFAULT_PRIORITY = 100
22
+
23
+ def initialize(config)
24
+ @config = config || {}
25
+ end
26
+
27
+ def url = @config["url"].to_s.chomp("/")
28
+ def token = ENV["BRAINIAC_HUB_TOKEN"] || @config["token"]
29
+ def enabled? = !url.empty? && !token.to_s.empty?
30
+ def tunnel = @config["tunnel"]
31
+ def public_url = @config["public_url"].to_s.chomp("/")
32
+ def priority = @config.fetch("priority", DEFAULT_PRIORITY).to_i
33
+ def wants_leader? = @config.fetch("leader", true) ? true : false
34
+
35
+ def machine_name
36
+ name = @config["machine"].to_s.empty? ? Socket.gethostname.split(".").first : @config["machine"]
37
+ name.to_s.downcase.gsub(/[^a-z0-9._-]/, "-")
38
+ end
39
+
40
+ def interval
41
+ value = @config["interval"].to_i
42
+ value.positive? ? value : DEFAULT_INTERVAL
43
+ end
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,83 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "open3"
4
+
5
+ module Brainiac
6
+ module Hub
7
+ # Runs a tunnel process, reads its public URL from the output, and restarts
8
+ # it if it exits. A new URL triggers a fresh check-in, so the URL is allowed
9
+ # to change every time.
10
+ class Tunnel
11
+ PRESETS = {
12
+ "cloudflared" => {
13
+ "command" => ["cloudflared", "tunnel", "--url", "http://127.0.0.1:%<port>d"],
14
+ "url_pattern" => 'https://(?!api\.)[a-z0-9-]+\.trycloudflare\.com'
15
+ }
16
+ }.freeze
17
+ RESTART_DELAY = 10
18
+
19
+ attr_reader :url
20
+
21
+ # @param setting [String, Hash] a preset name, or { "command" => [...], "url_pattern" => "..." }
22
+ def self.from_config(setting, port:, &on_url)
23
+ spec = setting.is_a?(Hash) ? setting : PRESETS[setting.to_s]
24
+ raise ArgumentError, "Unknown hub tunnel '#{setting}' (known: #{PRESETS.keys.join(", ")})" unless spec
25
+
26
+ command = Array(spec["command"]).map { |part| format(part.to_s, port: port) }
27
+ new(command: command, url_pattern: Regexp.new(spec["url_pattern"].to_s), &on_url)
28
+ end
29
+
30
+ def initialize(command:, url_pattern:, &on_url)
31
+ @command = command
32
+ @url_pattern = url_pattern
33
+ @on_url = on_url
34
+ @stopping = false
35
+ end
36
+
37
+ def start
38
+ @thread = Thread.new do
39
+ until @stopping
40
+ run_once
41
+ sleep RESTART_DELAY unless @stopping
42
+ end
43
+ end
44
+ end
45
+
46
+ def stop
47
+ @stopping = true
48
+ Process.kill("TERM", @pid) if @pid
49
+ rescue Errno::ESRCH
50
+ nil
51
+ end
52
+
53
+ # Feed one line of tunnel output. Public so it can be tested without a process.
54
+ def read_line(line)
55
+ found = line[@url_pattern]
56
+ return unless found && found != @url
57
+
58
+ @url = found
59
+ LOG.info "[Hub] Tunnel URL: #{found}"
60
+ @on_url&.call(found)
61
+ end
62
+
63
+ private
64
+
65
+ def run_once
66
+ Open3.popen2e(*@command) do |stdin, output, wait|
67
+ stdin.close
68
+ @pid = wait.pid
69
+ output.each_line { |line| read_line(line) }
70
+ LOG.warn "[Hub] Tunnel exited (#{wait.value.exitstatus})" unless @stopping
71
+ end
72
+ rescue Errno::ENOENT
73
+ LOG.error "[Hub] Tunnel command not found: #{@command.first}. Install it or set hub.public_url instead."
74
+ sleep 60
75
+ rescue StandardError => e
76
+ LOG.error "[Hub] Tunnel error: #{e.message}"
77
+ ensure
78
+ @url = nil
79
+ @pid = nil
80
+ end
81
+ end
82
+ end
83
+ end
@@ -0,0 +1,86 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "forwardable"
4
+ require "json"
5
+ require "uri"
6
+
7
+ require_relative "hub/settings"
8
+ require_relative "hub/client"
9
+ require_relative "hub/tunnel"
10
+ require_relative "hub/claims"
11
+ require_relative "hub/ledger"
12
+ require_relative "hub/node"
13
+ require_relative "hub/inbox"
14
+
15
+ # Multi-machine support: this machine as a node of a brainiac-hub.
16
+ #
17
+ # The hub is a small cloud app that receives webhooks and pushes each one to
18
+ # exactly one machine. A node's side of that contract:
19
+ #
20
+ # - check in with the hub on start and on a timer (liveness, the leader
21
+ # lease, branch claims, and pickup of anything queued while we were away)
22
+ # - keep a public URL for the hub to push to (a managed tunnel or a fixed URL)
23
+ # - acknowledge hub deliveries immediately, then run them
24
+ # - hold or give up the "leader" role, which decides who runs the Discord
25
+ # gateways and cron
26
+ #
27
+ # Everything is off unless brainiac.json has a "hub" block with a url and a
28
+ # token. With no hub configured, leader? is true and nothing else runs, so a
29
+ # single machine behaves exactly as before.
30
+ #
31
+ # See docs/multi-machine.md.
32
+ module Brainiac
33
+ module Hub
34
+ class << self
35
+ extend Forwardable
36
+
37
+ attr_writer :node
38
+
39
+ def_delegators :node, :enabled?, :leader?, :settings, :client, :ledger
40
+ def_delegators :ledger, :valid_signature?, :first_delivery?
41
+
42
+ def node
43
+ @node ||= Node.new(defined?(load_brainiac_config) ? load_brainiac_config["hub"] : {})
44
+ end
45
+
46
+ def start!(port:) = node.start(port: port)
47
+ def stop! = node.stop
48
+ def machine_name = settings.machine_name
49
+
50
+ # --- hub API (CLI and plugins) ---
51
+
52
+ # { "leader" => name, "nodes" => [...], "queued" => { machine => count } } or nil
53
+ def status
54
+ client.get("/nodes")
55
+ end
56
+
57
+ def assignments(prefix = nil)
58
+ path = prefix.to_s.empty? ? "/assignments" : "/assignments?prefix=#{URI.encode_www_form_component(prefix)}"
59
+ (client.get(path) || {})["assignments"] || []
60
+ end
61
+
62
+ # The machine that owns one work key, or nil (also nil if the hub did not answer).
63
+ def owner(key)
64
+ rows = (client.get("/assignments?key=#{URI.encode_www_form_component(key)}") || {})["assignments"] || []
65
+ rows.find { |row| row["key"] == key }&.fetch("machine", nil)
66
+ end
67
+
68
+ # Forget a key's owner (the work is finished or its worktree is gone).
69
+ def release(key)
70
+ client.delete("/assignments?key=#{URI.encode_www_form_component(key)}")
71
+ end
72
+
73
+ # Give work keys to a machine. overwrite: false only fills in unowned keys.
74
+ def assign(keys, machine, overwrite: true)
75
+ client.put("/assignments", { machine: machine, keys: Array(keys), overwrite: overwrite })
76
+ end
77
+
78
+ # Hand a JSON payload to another machine through the hub.
79
+ # Returns { "status" => "delivered" | "queued", "machine" => name } or nil.
80
+ def relay(path:, payload:, keys: [], machine: nil, reassign: false)
81
+ client.post("/relays", { path: path, body: JSON.generate(payload), keys: keys,
82
+ machine: machine, reassign: reassign })
83
+ end
84
+ end
85
+ end
86
+ end
@@ -2,5 +2,5 @@
2
2
 
3
3
  module Brainiac
4
4
  # @return [String] the current gem version
5
- VERSION = "0.6.0"
5
+ VERSION = "0.7.0"
6
6
  end
data/receiver.rb CHANGED
@@ -14,6 +14,7 @@ SERVER_ROOT = File.expand_path(__dir__)
14
14
  # Load all modules
15
15
  require_relative "lib/brainiac/hooks"
16
16
  require_relative "lib/brainiac/config"
17
+ require_relative "lib/brainiac/hub"
17
18
  require_relative "lib/brainiac/users"
18
19
  require_relative "lib/brainiac/agents"
19
20
  require_relative "lib/brainiac/brain"
@@ -100,8 +101,20 @@ end
100
101
 
101
102
  configure do
102
103
  use SelectiveLogger, LOG
104
+ # Multi-machine: acknowledge deliveries pushed by the hub, then run them.
105
+ # Passes everything else straight through (see lib/brainiac/hub.rb).
106
+ use Brainiac::Hub::Inbox
103
107
  end
104
108
 
109
+ # Tell the hub when this machine shuts down, so it stops pushing here and the
110
+ # leader role moves on. quit! runs inside a signal handler, hence the thread.
111
+ Sinatra::Application.singleton_class.prepend(Module.new do
112
+ def quit!
113
+ Thread.new { Brainiac::Hub.stop! }.join(5)
114
+ super
115
+ end
116
+ end)
117
+
105
118
  LOG.info "[Brainiac] Starting v#{BRAINIAC_VERSION} on port #{settings.port} (#{settings.environment})"
106
119
  LOG.info "[Brainiac] Server root: #{SERVER_ROOT} (pid #{Process.pid})"
107
120
 
@@ -137,29 +150,30 @@ helpers GitHelpers
137
150
  # Register BeltHelpers for Belt app detection
138
151
  helpers BeltHelpers
139
152
 
140
- # Dashboard authentication helpers
153
+ # Admin API and dashboard authentication.
154
+ #
155
+ # Requests made on this machine (CLI, waybar, daemon) need no token. Everything
156
+ # else needs dashboard_token from brainiac.json, and is refused outright when no
157
+ # token is configured: the server usually sits behind a public tunnel for
158
+ # webhooks, and that tunnel reaches these routes too.
141
159
  helpers do
142
- def authenticate_dashboard!
143
- return unless DASHBOARD_TOKEN # No token configured = no auth (local-only mode)
144
-
160
+ def authenticate_admin!
145
161
  provided = params["token"] || request.env["HTTP_AUTHORIZATION"]&.sub(/^Bearer /i, "")
146
- halt 401, "Unauthorized" unless provided == DASHBOARD_TOKEN
147
- end
162
+ verdict = admin_access(request.env, provided)
163
+ return if verdict == :ok
148
164
 
149
- def localhost_request?
150
- host = request.env["HTTP_HOST"].to_s
151
- host.include?("localhost") || host.include?("127.0.0.1")
165
+ reason = verdict == :no_token ? "no dashboard_token is configured" : "wrong or missing token"
166
+ security_alert("admin", "Refused #{request.request_method} #{request.path_info[0, 80]} from #{request_origin(request.env)} (#{reason})")
167
+ halt(verdict == :no_token ? 403 : 401, "Unauthorized")
152
168
  end
153
169
  end
154
170
 
155
171
  before "/dashboard" do
156
- authenticate_dashboard!
172
+ authenticate_admin!
157
173
  end
158
174
 
159
175
  before "/api/*" do
160
- # Skip auth for all localhost requests (CLI, waybar, daemon, etc.)
161
- pass if localhost_request?
162
- authenticate_dashboard!
176
+ authenticate_admin!
163
177
  end
164
178
 
165
179
  # --- Admin API routes ---
@@ -194,6 +208,8 @@ start_cron_thread
194
208
  CURATOR_THREAD = Thread.new do
195
209
  loop do
196
210
  sleep(86_400) # Run once per day
211
+ next unless Brainiac::Hub.leader? # the brain is shared; one machine curates it
212
+
197
213
  LOG.info "[Curator] Running scheduled skill curation..."
198
214
  curate_skills
199
215
  rescue StandardError => e
@@ -201,6 +217,10 @@ CURATOR_THREAD = Thread.new do
201
217
  end
202
218
  end
203
219
 
220
+ # Multi-machine: register this machine with the hub (no-op without a "hub" block
221
+ # in brainiac.json). Runs after plugins load so their leader hooks are in place.
222
+ Brainiac::Hub.start!(port: settings.port)
223
+
204
224
  LOG.info "[Monitor] Starting daemon..."
205
225
  daemon_path = File.join(__dir__, "monitor", "daemon.rb")
206
226
  daemon_pid_file = "/tmp/brainiac-daemon.pid"
@@ -46,10 +46,12 @@ if [[ -n "$ROLE" ]] && [[ -f "${HOME}/.brainiac/roles/${ROLE}.md" ]]; then
46
46
  fi
47
47
 
48
48
  # Create the agent file
49
+ # Use AGENT_KEY (lowercase) for name — Kiro-cli 3.x is case-sensitive
50
+ # and brainiac passes lowercase to --agent
49
51
  if [[ -n "$ROLE_RESOURCE" ]]; then
50
52
  cat > "$AGENT_FILE" <<EOF
51
53
  {
52
- "name": "${DISPLAY_NAME:-$AGENT_KEY}",
54
+ "name": "${AGENT_KEY}",
53
55
  "tools": ["*"],
54
56
  "resources": [
55
57
  ${ROLE_RESOURCE}
@@ -59,7 +61,7 @@ EOF
59
61
  else
60
62
  cat > "$AGENT_FILE" <<EOF
61
63
  {
62
- "name": "${DISPLAY_NAME:-$AGENT_KEY}",
64
+ "name": "${AGENT_KEY}",
63
65
  "tools": ["*"]
64
66
  }
65
67
  EOF
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: brainiac
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.6.0
4
+ version: 0.7.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andy Davis
@@ -122,6 +122,7 @@ files:
122
122
  - bin/brainiac
123
123
  - bin/brainiac-completion.bash
124
124
  - brainiac.gemspec
125
+ - docs/multi-machine.md
125
126
  - docs/resume.md
126
127
  - docs/waybar-config.md
127
128
  - lib/brainiac.rb
@@ -135,6 +136,15 @@ files:
135
136
  - lib/brainiac/handlers/shared/inline_tags.rb
136
137
  - lib/brainiac/helpers.rb
137
138
  - lib/brainiac/hooks.rb
139
+ - lib/brainiac/hub.rb
140
+ - lib/brainiac/hub/claims.rb
141
+ - lib/brainiac/hub/cli.rb
142
+ - lib/brainiac/hub/client.rb
143
+ - lib/brainiac/hub/inbox.rb
144
+ - lib/brainiac/hub/ledger.rb
145
+ - lib/brainiac/hub/node.rb
146
+ - lib/brainiac/hub/settings.rb
147
+ - lib/brainiac/hub/tunnel.rb
138
148
  - lib/brainiac/intent.rb
139
149
  - lib/brainiac/model_parser.rb
140
150
  - lib/brainiac/notifications.rb