Entropic 2.3.8
Local-first agentic inference engine
Loading...
Searching...
No Matches
external_bridge.h
Go to the documentation of this file.
1// SPDX-License-Identifier: Apache-2.0
29#pragma once
30
33
34#include <nlohmann/json_fwd.hpp>
35
36#include <atomic>
37#include <chrono>
38#include <filesystem>
39#include <memory>
40#include <mutex>
41#include <string>
42#include <thread>
43#include <unordered_map>
44#include <unordered_set>
45#include <vector>
46
47// Forward declaration — bridge holds a raw pointer to the opaque handle.
48struct entropic_engine;
50
51namespace entropic {
52
68class ENTROPIC_EXPORT ExternalBridge {
69public:
78 entropic_handle_t handle,
79 const ExternalMCPConfig& config,
80 const std::filesystem::path& project_dir);
81
83
89 bool start();
90
95 void stop();
96
103 const std::filesystem::path& socket_path() const { return socket_path_; }
104
112 nlohmann::json handle_ask_status(const nlohmann::json& args);
113
135 void attach_phase_observer(const std::string& task_id);
136
142 void detach_phase_observer();
143
144 void run_async_ask(const std::string& prompt,
145 const std::string& task_id,
146 int client_fd);
147
168 void write_sentinel(const std::string& task_id,
169 const std::string& status);
170
186 std::filesystem::path async_sentinel_dir() const;
187
201 void set_async_sentinel_root(const std::filesystem::path& root);
202
208 void cleanup_expired_tasks();
209
211 mutable std::mutex tasks_mutex_;
212
222 struct AsyncTask {
223 std::string status = "queued";
224 std::string phase = "queued";
225 std::string result;
226 std::chrono::steady_clock::time_point created;
227 };
228
241 std::unordered_map<std::string, AsyncTask>& tasks_for_cancel() {
242 return tasks_;
243 }
244
251 const std::string& active_task_id_for_observer() const {
252 return active_task_id_;
253 }
254
261 void subscribe(int fd);
262
269 void unsubscribe(int fd);
270
282 void broadcast_notification(const nlohmann::json& notif);
283
290 size_t subscriber_count() const {
291 std::lock_guard<std::mutex> lock(subscribers_mutex_);
292 return subscribers_.size();
293 }
294
308 void update_task_phase(const std::string& task_id,
309 const std::string& status,
310 const std::string& phase);
311
326 return observer_gen_.load() != attached_gen_;
327 }
328
329private:
335 void accept_loop();
336
343 void serve_client(int client_fd);
344
353 std::string dispatch(const std::string& request, int client_fd);
354
365 void reap_finished_clients_locked();
366
367 entropic_handle_t handle_;
368 ExternalMCPConfig config_;
369 std::filesystem::path socket_path_;
370 std::string bound_canonical_;
371 int listen_fd_ = -1;
372 std::atomic<bool> running_{false};
373 std::thread accept_thread_;
374
383 struct ClientThread {
384 int fd;
385 std::atomic<bool> finished{false};
386 std::thread thread;
387 };
388 std::vector<std::unique_ptr<ClientThread>> client_threads_;
389 mutable std::mutex client_threads_mutex_;
390
392 std::unordered_map<std::string, AsyncTask> tasks_;
393
399 std::unordered_set<int> subscribers_;
400 mutable std::mutex subscribers_mutex_;
401
407 std::string active_task_id_;
408
413 std::atomic<uint64_t> observer_gen_{0};
414
418 uint64_t attached_gen_ = 0;
419
423 std::filesystem::path async_sentinel_root_override_;
424};
425
426} // namespace entropic
Unix socket MCP bridge for external client access.
std::mutex tasks_mutex_
Async task mutex (public for dispatch_tool access).
const std::string & active_task_id_for_observer() const
Return the currently-active async task_id (if any).
const std::filesystem::path & socket_path() const
Get the socket path (for logging/diagnostics).
bool observer_call_is_stale() const
Test whether an in-flight observer callback is stale.
std::unordered_map< std::string, AsyncTask > & tasks_for_cancel()
Mutable accessor to the task registry.
size_t subscriber_count() const
Current subscriber count (for diagnostics / testing).
Configuration structs with defaults.
Symbol visibility macro for all exported symbols.
Activate model on GPU (WARM → ACTIVE).
Async task state for background entropic.ask runs.
std::string result
Final text or error message.
std::chrono::steady_clock::time_point created
For TTL cleanup.
External MCP server configuration (Entropic-as-server).
Definition config.h:423
Engine handle struct — owns all subsystems.