Entropic 2.14.3
Local-first agentic inference engine
Loading...
Searching...
No Matches
sandbox.h
Go to the documentation of this file.
1// SPDX-License-Identifier: Apache-2.0
51#pragma once
52
53#include <atomic>
54#include <filesystem>
55#include <mutex>
56#include <optional>
57#include <string>
58#include <vector>
59
60namespace entropic {
61
67 std::filesystem::path path;
68 std::string delegation_id;
69 std::filesystem::path base_dir;
70};
71
84 std::string patch;
85 std::vector<std::filesystem::path> files_touched;
86 std::filesystem::path base_dir;
87 std::filesystem::path head_dir;
88};
89
117public:
133 explicit SandboxManager(const std::filesystem::path& project_dir);
134
144
145 SandboxManager(SandboxManager&&) = default;
146 SandboxManager& operator=(SandboxManager&&) = default;
147 SandboxManager(const SandboxManager&) = delete;
148 SandboxManager& operator=(const SandboxManager&) = delete;
149
171 std::optional<SandboxInfo> create_sandbox(
172 const std::string& delegation_id,
173 std::optional<SandboxInfo> chain_from = std::nullopt);
174
189 std::optional<SandboxResult> finalize_sandbox(const SandboxInfo& info);
190
200 void discard_sandbox(const SandboxInfo& info);
201
217 std::optional<std::filesystem::path> write_pending_patch(
218 const std::string& delegation_id,
219 const std::string& patch);
220
226 const std::filesystem::path& project_dir() const;
227
233 const std::filesystem::path& session_base() const;
234
253 std::string next_delegation_id(const std::string& prefix);
254
255private:
261 bool ensure_base_snapshot();
262
275 bool snapshot_tree(const std::filesystem::path& source,
276 const std::filesystem::path& target);
277
290 bool path_in_session_base(const std::filesystem::path& p) const;
291
297 void safe_remove(const std::filesystem::path& p);
298
303 void prune_stale_sessions();
304
305 std::filesystem::path project_dir_;
306 std::string session_id_;
307 std::filesystem::path session_base_;
308 std::filesystem::path base_dir_;
309 bool base_ready_ = false;
316 mutable std::mutex base_mutex_;
318 std::atomic<unsigned> id_seq_{0};
319};
320
332public:
358 using SwapDirFn = void (*)(
359 const std::string& session_key,
360 const std::filesystem::path& path,
361 bool entering, void* user_data);
362
376 void* user_data,
377 std::string session_key,
378 const std::filesystem::path& sandbox_path,
379 const std::filesystem::path& original_path);
380
386
387 ScopedSandbox(const ScopedSandbox&) = delete;
388 ScopedSandbox& operator=(const ScopedSandbox&) = delete;
389
390private:
391 SwapDirFn swap_fn_;
392 void* user_data_;
393 std::string session_key_;
394 std::filesystem::path original_path_;
395};
396
397} // namespace entropic
Create, finalize, and discard per-delegation filesystem sandboxes.
Definition sandbox.h:116
const std::filesystem::path & session_base() const
Get this session's sandbox base directory.
Definition sandbox.cpp:607
void discard_sandbox(const SandboxInfo &info)
Remove a sandbox directory.
Definition sandbox.cpp:541
std::string next_delegation_id(const std::string &prefix)
Mint a sandbox id that no sibling delegation can collide with.
Definition sandbox.cpp:618
std::optional< SandboxResult > finalize_sandbox(const SandboxInfo &info)
Produce the final patch artifact for a sandbox.
Definition sandbox.cpp:514
std::optional< SandboxInfo > create_sandbox(const std::string &delegation_id, std::optional< SandboxInfo > chain_from=std::nullopt)
Create a new delegation sandbox.
Definition sandbox.cpp:398
~SandboxManager()
Remove this session's sandbox tree.
Definition sandbox.cpp:192
std::optional< std::filesystem::path > write_pending_patch(const std::string &delegation_id, const std::string &patch)
Write a patch to the session's pending/ directory.
Definition sandbox.cpp:560
const std::filesystem::path & project_dir() const
Get the project directory this manager snapshots from.
Definition sandbox.cpp:597
RAII directory swapper for sandbox-scoped tool execution.
Definition sandbox.h:331
~ScopedSandbox()
Restore the original directory.
Definition sandbox.cpp:656
void(*)(const std::string &session_key, const std::filesystem::path &path, bool entering, void *user_data) SwapDirFn
Callback type for directory swapping.
Definition sandbox.h:361
Activate model on GPU (WARM → ACTIVE).
@ optional
optional: every escape asks the host's path approver
Identifies one delegation's sandbox directory.
Definition sandbox.h:66
std::filesystem::path base_dir
Snapshot used as this sandbox's starting state.
Definition sandbox.h:69
std::filesystem::path path
Sandbox directory (under session base)
Definition sandbox.h:67
std::string delegation_id
Short delegation id (e.g. "d1", "pipeline")
Definition sandbox.h:68
Final artifact emitted by a finalized sandbox.
Definition sandbox.h:83
std::filesystem::path head_dir
Final sandbox state.
Definition sandbox.h:87
std::vector< std::filesystem::path > files_touched
Relative paths that changed.
Definition sandbox.h:85
std::string patch
Unified diff text.
Definition sandbox.h:84
std::filesystem::path base_dir
Snapshot the diff is against.
Definition sandbox.h:86