Entropic 2.14.3
Local-first agentic inference engine
Loading...
Searching...
No Matches
filesystem.h
Go to the documentation of this file.
1// SPDX-License-Identifier: Apache-2.0
13#pragma once
14
19
20#include <filesystem>
21#include <memory>
22#include <mutex>
23#include <string>
24#include <unordered_map>
25#include <vector>
26
27namespace entropic {
28
43public:
52 void record_read(const std::string& session, const std::string& path,
53 size_t hash);
54
63 bool was_read(const std::string& session, const std::string& path) const;
64
72 bool release_session(const std::string& session);
73
79 std::size_t session_count() const;
80
81private:
84};
85
95enum class PathAccess {
96 read,
97 write,
98};
99
105 std::string path;
106 std::string root;
107 std::string tool;
109};
110
121 approved,
122 rejected,
124};
125
134 const OutsideRootRequest& request, void* user_data);
135
136// Forward declarations for tool classes
137class ReadFileTool;
138class WriteFileTool;
139class EditFileTool;
140class GlobTool;
141class GrepTool;
143
154public:
163 FilesystemServer(const std::filesystem::path& root_dir,
165 const std::string& data_dir,
166 int model_context_bytes = 0);
167
169
176 bool skip_duplicate_check(const std::string& tool_name) const override;
177
184 bool set_working_dir(const std::string& path) override;
185
191 const std::filesystem::path& root_dir() const;
192
199
206 bool release_session(const std::string& key) override;
207
213 std::size_t session_count() const override;
214
220 const FilesystemConfig& config() const;
221
227 int max_read_bytes() const;
228
247 std::filesystem::path resolve_path(const std::string& requested,
248 PathAccess access,
249 const std::string& tool) const;
250
262 void set_outside_root_approver(OutsideRootApprover fn, void* user_data);
263
274 const IgnoreMatcher& ignore() const;
275
276private:
283 void create_fs_tools(const std::string& data_dir);
284
290 void register_fs_tools();
291
301 void authorize_outside_root(const std::filesystem::path& resolved,
302 PathAccess access,
303 const std::string& tool) const;
304
314 void ask_outside_root_approver(const std::filesystem::path& resolved,
315 PathAccess access,
316 const std::string& tool) const;
317
323 void log_outside_root_policy() const;
324
325 std::filesystem::path root_dir_;
326 FilesystemConfig config_;
327 int max_read_bytes_ = 0;
328 FileAccessTracker tracker_;
329 IgnoreMatcher ignore_;
330
332 mutable std::mutex approver_mutex_;
333 OutsideRootApprover approver_ = nullptr;
334 void* approver_data_ = nullptr;
335
336 // Owned tool instances
337 std::unique_ptr<ReadFileTool> read_file_;
338 std::unique_ptr<WriteFileTool> write_file_;
339 std::unique_ptr<EditFileTool> edit_file_;
340 std::unique_ptr<GlobTool> glob_;
341 std::unique_ptr<GrepTool> grep_;
342 std::unique_ptr<ListDirectoryTool> list_dir_;
343};
344
345} // namespace entropic
Tool for in-place file editing (string replace or insert).
Tracks file read state for read-before-write enforcement.
Definition filesystem.h:42
bool was_read(const std::string &session, const std::string &path) const
Check whether session has read a file.
void record_read(const std::string &session, const std::string &path, size_t hash)
Record that session read a file.
std::size_t session_count() const
Sessions currently holding recorded reads.
bool release_session(const std::string &session)
Forget every read session recorded (gh#158).
Filesystem MCP server with read-before-write enforcement.
Definition filesystem.h:153
int max_read_bytes() const
Get max read bytes (size gate).
void set_outside_root_approver(OutsideRootApprover fn, void *user_data)
Install the approver consulted under optional (v2.13.0).
const IgnoreMatcher & ignore() const
Get the ignore matcher (#15, v2.1.4).
std::size_t session_count() const override
Sessions whose reads this server currently tracks (gh#158).
bool skip_duplicate_check(const std::string &tool_name) const override
read_file must always execute (updates FileAccessTracker).
bool set_working_dir(const std::string &path) override
Set working directory (changes root_dir).
~FilesystemServer() override
Destructor (default, unique_ptr cleanup).
const FilesystemConfig & config() const
Get the filesystem config.
std::filesystem::path resolve_path(const std::string &requested, PathAccess access, const std::string &tool) const
Resolve a path and apply the outside-root policy (v2.13.0).
FileAccessTracker & tracker()
Get the file access tracker.
bool release_session(const std::string &key) override
Release key's recorded reads (gh#158).
const std::filesystem::path & root_dir() const
Get the root directory.
Tool for recursive file pattern matching.
Tool for regex content search across files.
gitignore-style path matcher (#15, v2.1.4).
Tool for listing directory contents.
Concrete base class for MCP servers (80% logic).
Definition server_base.h:66
Tool for reading file contents with line numbering.
One T per session key, under one leaf mutex (gh#158).
A server that keeps state per session and can release it (gh#158).
Tool for writing file contents with read-before-write.
Configuration structs with defaults.
Path-relative ignore matching honoring .gitignore + .explorerignore.
Activate model on GPU (WARM → ACTIVE).
OutsideRootVerdict
The host's answer to an OutsideRootRequest (v2.13.0).
Definition filesystem.h:120
@ approved
Serve this one call.
@ rejected
The approver said no.
@ no_approver
No approver is registered — refuse, fail loud.
@ optional
optional: every escape asks the host's path approver
@ request
GenerationParams::grammar (COMMON_GRAMMAR_TYPE_USER)
OutsideRootVerdict(*)(const OutsideRootRequest &request, void *user_data) OutsideRootApprover
Approver the server consults under allow_outside_root: optional.
Definition filesystem.h:134
PathAccess
What a filesystem tool is about to do with a path (v2.13.0).
Definition filesystem.h:95
@ read
read_file, list_directory
@ write
write_file, edit_file
MCPServerBase concrete base class + ServerResponse.
Server state that belongs to a SESSION, not to the server (gh#158).
Filesystem MCP server configuration.
Definition config.h:781
One outside-root access awaiting the host's decision (v2.13.0).
Definition filesystem.h:104
PathAccess access
Read or write.
Definition filesystem.h:108
std::string root
Canonical root the path lies outside.
Definition filesystem.h:106
std::string tool
Fully-qualified tool ("filesystem.write_file")
Definition filesystem.h:107
std::string path
Canonical absolute path the tool resolved.
Definition filesystem.h:105