Entropic 2.14.3
Local-first agentic inference engine
Loading...
Searching...
No Matches
engine_handle.h
Go to the documentation of this file.
1// SPDX-License-Identifier: Apache-2.0
18#pragma once
19
26#include <entropic/entropic.h> // ent_path_approval_cb (v2.13.0)
30
31#include "http_listener.h"
41
42#include <atomic>
43#include <filesystem>
44#include <map>
45#include <memory>
46#include <mutex>
47#include <optional>
48#include <string>
49
50// Forward declarations for Phase 1+ subsystems.
51// Headers included as each phase wires them.
52namespace entropic {
53class ModelOrchestrator;
54class ServerManager;
55class ToolExecutor;
56class IdentityManager;
57class MCPAuthorizationManager;
58class AgentEngine;
59class SqliteStorageBackend;
60class AuditLogger;
61class ConstitutionalValidator;
62class CompactorRegistry;
63struct ParsedConfig;
64class BundledModels;
65struct InterfaceContext; // gh#58 follow-up (v2.2.6): per-handle iface ctx
66} // namespace entropic
67
85 std::string name;
86 std::filesystem::path root;
91 std::unique_ptr<entropic::ServerManager> servers;
93 std::optional<std::string> app_context;
94};
95
109 // ── Phase 0: Lifecycle ──────────────────────────────────
111 std::mutex api_mutex;
112 std::string last_error;
113 std::atomic<bool> configured{false};
114 std::atomic<bool> running{false};
119 int log_id = 0;
120
121 // ── Phase 1: Configuration ────────────────────────────────
124
125 // ── Phase 2: Inference ─────────────────────────────────────
126 std::unique_ptr<entropic::ModelOrchestrator> orchestrator;
131
132 // ── Phase 3: MCP + Identity + Authorization ──────────────
133 std::unique_ptr<entropic::ServerManager> server_manager;
134 std::unique_ptr<entropic::ToolExecutor> tool_executor;
135 std::unique_ptr<entropic::IdentityManager> identity_manager;
136 std::unique_ptr<entropic::MCPAuthorizationManager> mcp_auth;
137
138 // ── Phase 3b: Delegation isolation + workspaces (gh#160/gh#166) ──
139 // gh#158: the DEFAULT workspace's sandbox swap lock is
140 // `server_manager`'s own `ToolRootLock` — exclusive from `ScopedSandbox`
141 // construction to destruction, re-entrant for a nested delegation, and
142 // taken SHARED by every in-process dispatch on the set. The facade
143 // `recursive_mutex` it replaces serialized swaps against each other
144 // only, not against another session's tool calls.
145
148 std::map<std::string, std::unique_ptr<EntropicWorkspace>> workspaces;
152 std::map<std::string, std::string> session_workspace;
154 mutable std::mutex workspace_mutex;
155
156 // ── Phase 4: Engine Loop + Storage + Audit ─────────────────
157 std::unique_ptr<entropic::AgentEngine> engine;
158 std::unique_ptr<entropic::SqliteStorageBackend> storage;
159 std::unique_ptr<entropic::AuditLogger> audit_logger;
160 std::unique_ptr<entropic::SessionLogger> session_logger;
161
162 // ── Phase 5: Constitutional + Compaction ────────────────────
163 std::unique_ptr<entropic::ConstitutionalValidator> validator;
164 std::unique_ptr<entropic::CompactorRegistry> compactor_registry;
165
166 // ── Phase 6: Tier metadata (v2.0.4) ───────────────────────
168 std::unordered_map<std::string, std::vector<std::string>> tier_allowed_tools;
170 std::unordered_map<std::string, std::vector<std::string>> tier_validation_rules;
171
172 // ── Phase 7: External MCP bridge (v2.0.8) ──────────────────
173 std::unique_ptr<entropic::ExternalBridge> external_bridge;
176 std::unique_ptr<entropic::http::HttpListener> http_listener;
177
178 // ── Stream observer (v2.0.10) ────────────────────────────
180 void (*stream_observer)(const char*, size_t, void*) = nullptr;
181 void* stream_observer_data = nullptr;
182
183 // ── State-change observer (P1-5 follow-up, 2.0.6-rc16.2) ─
187 void (*state_observer)(int, void*) = nullptr;
188 void* state_observer_data = nullptr;
189
190 // ── Mid-gen queue observer (gh#40, v2.1.10) ───────────────
194 void (*queue_observer)(const char*, size_t, void*) = nullptr;
195 void* queue_observer_data = nullptr;
196
197 // ── Critique start/end callbacks (gh#50, v2.1.12) ─────────
203 void (*critique_start_cb)(void*) = nullptr;
205 void (*critique_end_cb)(void*) = nullptr;
207 void* critique_cb_data = nullptr;
208
209 // ── Outside-root path approver (v2.13.0) ───────────────────
215 void* path_approval_data = nullptr;
219};
220
221namespace entropic {
222
238 const std::string& key) {
239 if (h == nullptr) { return nullptr; }
240 std::lock_guard<std::mutex> lock(h->workspace_mutex);
241 auto bound = h->session_workspace.find(key);
242 if (bound == h->session_workspace.end()) { return nullptr; }
243 auto ws = h->workspaces.find(bound->second);
244 return ws == h->workspaces.end() ? nullptr : ws->second.get();
245}
246
256 const std::string& key) {
257 auto* ws = workspace_for(h, key);
258 if (ws != nullptr && ws->servers) { return ws->servers.get(); }
259 return h != nullptr ? h->server_manager.get() : nullptr;
260}
261
275
288std::filesystem::path expected_bridge_socket(entropic_handle_t h);
289
296std::filesystem::path bridge_project_dir(entropic_handle_t h);
297
317 ServerManager& servers, bool with_declared);
318
334 const std::string& workspace,
335 const std::optional<std::string>& app_context);
336
353ENTROPIC_EXPORT void swap_session_tool_dir(
354 const std::string& session_key, const std::filesystem::path& path,
355 bool entering, void* ud);
356
377 const std::string& key);
378
396public:
399 : lock_(h->api_mutex), log_scope_(h->log_id) {}
400 HandleApiLock(const HandleApiLock&) = delete;
401 HandleApiLock& operator=(const HandleApiLock&) = delete;
402private:
403 // Order matters: api_mutex acquired first, log_scope second.
404 // Destruction reverses (scope first, mutex second) — fine.
405 std::lock_guard<std::mutex> lock_;
407};
408
444public:
462 const char* session_key = nullptr)
463 : engine_(h != nullptr ? h->engine.get() : nullptr),
464 log_scope_(h != nullptr ? h->log_id : 0),
465 key_(session_key != nullptr ? session_key : ""),
466 // gh#166: publish the key to THIS thread for the whole call, so
467 // the tool-prompt callback — which arrives through an interface
468 // header that takes only a tier name — can resolve the
469 // workspace this turn belongs to.
470 session_scope_(session_key != nullptr ? session_key : "") {}
471
477 if (claimed_) { engine_->end_turn(key_); }
478 }
479
480 HandleTurnGuard(const HandleTurnGuard&) = delete;
481 HandleTurnGuard& operator=(const HandleTurnGuard&) = delete;
482
490 bool claim() {
491 if (!claimed_ && engine_ != nullptr) {
492 claimed_ = engine_->try_begin_turn(key_);
493 }
494 return claimed_;
495 }
496
503 const std::string& key() const { return key_; }
504
510 bool claimed() const { return claimed_; }
511
512private:
513 entropic::AgentEngine* engine_;
518 std::string key_;
523 entropic::RunSessionScope session_scope_;
524 bool claimed_ = false;
525};
526
527} // namespace entropic
JSONL audit logger for MCP tool calls.
Bundled model registry — resolves keys to filesystem paths.
Core agent execution engine.
Definition engine.h:65
bool try_begin_turn(const std::string &key)
Try to claim one turn for a named session (gh#144 / gh#158).
Definition engine.cpp:3614
void end_turn(const std::string &key)
Release a turn claimed by try_begin_turn (gh#144 / gh#158).
Definition engine.cpp:3646
gh#59 (v2.3.1): RAII guard combining api_mutex + log scope.
HandleApiLock(entropic_handle_t h)
Lock handle mutex + enter log scope.
gh#144 (v2.12.0): RAII turn claim + log scope, for the RUN paths.
bool claim()
Claim the turn.
bool claimed() const
Whether this guard owns the turn.
HandleTurnGuard(entropic_handle_t h, const char *session_key=nullptr)
Enter the handle's log scope.
const std::string & key() const
The session this guard claims for.
~HandleTurnGuard()
Release the turn if this guard claimed it.
Thread-safe hook registration and dispatch.
RAII publication of one run's session key to its own thread.
Definition run_scope.h:158
Manages MCP server instances and routes tool calls.
Bundled model registry loaded from bundled_models.yaml.
gh#59 (v2.3.1): RAII guard — sets thread's current handle_id.
Definition logging.h:159
Per-identity compactor registration and dispatch.
Configuration structs with defaults.
Post-generation constitutional compliance validator.
AgentEngine * engine
Fallback interrupt target.
Definition engine.cpp:4075
Core agent execution engine.
Public C API for the Entropic inference engine.
ent_decision_t(* ent_path_approval_cb)(const ent_path_approval_request_t *req, void *user_data)
Host decision for one outside-root filesystem access.
Definition entropic.h:1807
Error types for cross-.so error reporting.
Unix socket MCP bridge — exposes a running engine to external clients.
Thread-safe hook registration and dispatch.
Serve the external MCP bridge over Streamable HTTP (gh#204, v2.14.0).
IdentityManager – lifecycle management for static and dynamic identities.
spdlog initialization and logger access.
Per-identity MCP authorization with runtime grant/revoke.
Activate model on GPU (WARM → ACTIVE).
void wire_external_server_policy(entropic_handle_t h, ServerManager &servers, bool with_declared)
Give a server set the handle's external-server policy (v2.14.0).
void apply_workspace_app_context(entropic_handle_t h, const std::string &workspace, const std::optional< std::string > &app_context)
Give a workspace's sessions prompts built on its own app_context (gh#179), or return them to the hand...
@ optional
optional: every escape asks the host's path approver
EntropicWorkspace * workspace_for(entropic_handle_t h, const std::string &key)
The workspace a session is bound to, or nullptr for the default.
std::filesystem::path bridge_project_dir(entropic_handle_t h)
The project dir the external bridge derives its socket from.
ENTROPIC_EXPORT void swap_session_tool_dir(const std::string &session_key, const std::filesystem::path &path, bool entering, void *ud)
The facade's gh#160 ScopedSandbox swap callback.
std::filesystem::path expected_bridge_socket(entropic_handle_t h)
The socket this handle's external bridge binds, or empty when the handle runs no bridge (v2....
void wire_outside_root_approver(entropic_handle_t h)
Point the DEFAULT server set's filesystem server at the handle's path-approval slot (v2....
std::size_t release_session_tool_state(entropic_handle_t h, const std::string &key)
Release a session's per-session tool state on every server set (gh#158).
entropic::ServerManager * workspace_servers(entropic_handle_t h, const std::string &key)
The MCP servers a session's tools must run against (gh#166).
ModelOrchestrator — multi-model lifecycle and routing.
gh#158: the cancel token of the run executing on THIS thread.
MCP server lifecycle management and tool routing.
Session model log — raw streaming transcript.
SqliteStorageBackend — conversation persistence via SQLite.
A named repository a session's tools operate in (gh#166).
std::unique_ptr< entropic::ServerManager > servers
Its own instances.
std::optional< std::string > app_context
gh#179: this workspace's app_context; nullopt = the handle's.
std::string name
Caller-chosen workspace name.
std::filesystem::path root
Tool root for bound sessions.
Holds orchestrator + tier for C callback user_data.
Full parsed configuration.
Definition config.h:1338
Engine handle struct — owns all subsystems.
std::unique_ptr< entropic::ConstitutionalValidator > validator
Constitutional validation.
std::unique_ptr< entropic::ToolExecutor > tool_executor
Tool dispatch.
void(* critique_end_cb)(void *)
Fires after the critique generate returns.
int log_id
gh#59 (v2.3.1): unique handle id for per-handle log routing via entropic::log::HandleAwareSink.
std::mutex workspace_mutex
gh#166: guards both maps above. Held for lookups only.
std::map< std::string, std::unique_ptr< EntropicWorkspace > > workspaces
gh#166: named workspaces, by name.
std::atomic< bool > running
True during run()
std::unordered_map< std::string, std::vector< std::string > > tier_validation_rules
Per-tier validation_rules from identity frontmatter (v2.0.6).
std::unique_ptr< entropic::SqliteStorageBackend > storage
SQLite persistence.
entropic::InferenceInterface inference_iface
Stable copy for validator lifetime.
std::unique_ptr< entropic::http::HttpListener > http_listener
Streamable-HTTP front end for external_bridge (gh#204).
std::unique_ptr< entropic::AuditLogger > audit_logger
Audit log.
std::unique_ptr< entropic::SessionLogger > session_logger
Model transcript log.
std::unordered_map< std::string, std::vector< std::string > > tier_allowed_tools
Per-tier allowed_tools from identity frontmatter.
std::unique_ptr< entropic::CompactorRegistry > compactor_registry
Compaction strategies.
std::atomic< bool > configured
True after configure()
std::unique_ptr< entropic::ExternalBridge > external_bridge
Unix socket MCP bridge.
void(* state_observer)(int, void *)
Observer for engine state transitions.
entropic::config::BundledModels bundled_models
Model registry.
void * stream_observer_data
Observer user_data.
std::map< std::string, std::string > session_workspace
gh#166: session key → workspace name.
std::unique_ptr< entropic::IdentityManager > identity_manager
Identity lifecycle.
std::string last_error
Per-handle error message.
std::unique_ptr< entropic::AgentEngine > engine
Agentic loop (owns conversation state)
void * critique_cb_data
Forwarded to both callbacks.
std::unique_ptr< entropic::MCPAuthorizationManager > mcp_auth
Per-identity tool auth.
void * path_approval_data
Forwarded to the approver.
std::unique_ptr< entropic::ModelOrchestrator > orchestrator
Model pool + routing.
entropic::ParsedConfig config
Parsed config.
void(* stream_observer)(const char *, size_t, void *)
Global stream observer — fires for all streaming output.
void(* queue_observer)(const char *, size_t, void *)
Observer fired when a queued mid-gen user message is consumed and seeded as the next turn.
std::unique_ptr< entropic::ServerManager > server_manager
MCP server lifecycle.
std::mutex path_approval_mutex
Guards the pair above: the thunk reads it from run threads while the setter may write it from any thr...
entropic::InterfaceContext * inference_iface_ctx
Per-handle owned context backing inference_iface.user_data.
std::mutex api_mutex
Serializes API calls.
ent_path_approval_cb path_approval_cb
Consumer's approver for filesystem access outside the root.
void(* critique_start_cb)(void *)
Fires before the constitutional validator's critique generate begins.
entropic::HookRegistry hook_registry
Hook dispatch.
Processes tool calls from model output.