include/entropic/core/sandbox.h

← API

include/entropic/core/sandbox.h

entropic::SandboxInfo

Identifies one delegation's sandbox directory.

std::filesystem::path entropic::SandboxInfo::path

Sandbox directory (under session base)

std::string entropic::SandboxInfo::delegation_id

Short delegation id (e.g. "d1", "pipeline")

std::filesystem::path entropic::SandboxInfo::base_dir

Snapshot used as this sandbox's starting state.

entropic::SandboxManager

Create, finalize, and discard per-delegation filesystem sandboxes.

entropic::SandboxManager::SandboxManager(const std::filesystem::path &project_dir)

Construct with the user's project directory.

Generates <pid>-<hex8> session id and reserves ~/.entropic/sandbox/<session_id>/. The base snapshot is not created until the first create_sandbox() call — sessions that never delegate pay no snapshot cost. Prunes at startup: session directories under ~/.entropic/sandbox/ whose owner pid is no longer alive.

Parameters
  • project_dir — Path to the user's project root (used as snapshot source; never written to by this class).
  • project_dir — Project root (read-only to this class).

entropic::SandboxManager::~SandboxManager()

Remove this session's sandbox tree.

Destructor — remove this session's sandbox tree. Best-effort: errors are logged, not thrown. Path containment is enforced — refuses to remove anything outside session_base_.

entropic::SandboxManager::SandboxManager(SandboxManager &&)=default

SandboxManager & entropic::SandboxManager::operator=(SandboxManager &&)=default

entropic::SandboxManager::SandboxManager(const SandboxManager &)=delete

SandboxManager & entropic::SandboxManager::operator=(const SandboxManager &)=delete

std::optional< SandboxInfo > entropic::SandboxManager::create_sandbox(const std::string &delegation_id, std::optional< SandboxInfo > chain_from=std::nullopt)

Create a new delegation sandbox.

On the first call of a session, materializes the base/ snapshot from the project directory, honoring .gitignore (via git ls-files --cached --others --exclude-standard) when the project is a git repository. For non-git projects, the entire project tree is copied verbatim. The new sandbox is a copy of either base/ (when chain_from is empty) or chain_from->path (for pipeline forward-carry).

Parameters
  • delegation_id — Short, filesystem-safe id (e.g. "d1", "d2", "pipeline"). Becomes the sandbox dir name.
  • chain_from — Optional prior sandbox to chain from. When set, the new sandbox starts from that sandbox's current state instead of base/.
  • delegation_id — Short id (becomes the sandbox dir name).
  • chain_from — Optional prior sandbox to chain from.
Returns
  • SandboxInfo on success, std::nullopt if snapshot or copy failed (logged at ERROR).
  • SandboxInfo on success.

std::optional< SandboxResult > entropic::SandboxManager::finalize_sandbox(const SandboxInfo &info)

Produce the final patch artifact for a sandbox.

Runs git diff --no-index --binary <base_dir>/ <sandbox>/ to generate a portable unified diff, collects the list of changed files, and returns them in a SandboxResult. The sandbox directory is NOT removed by this call — the caller decides when to discard (see discard_sandbox()), allowing the patch consumer to inspect files before cleanup.

Parameters
  • info — Sandbox to finalize.
Returns
  • SandboxResult on success, std::nullopt on failure.
  • SandboxResult on success.

void entropic::SandboxManager::discard_sandbox(const SandboxInfo &info)

Remove a sandbox directory.

Path-containment guarded — refuses to remove anything outside session_base_. Safe to call multiple times.

Parameters
  • info — Sandbox to remove.

std::optional< std::filesystem::path > entropic::SandboxManager::write_pending_patch(const std::string &delegation_id, const std::string &patch)

Write a patch to the session's pending/ directory.

Persist a patch under the session's pending/ directory. Default-deny fallback: invoked by DelegationManager when no complete callback is registered, or when the consumer's callback returned ENT_DECISION_REJECT. The pending dir is created on demand under session_base_/pending/. Path containment is enforced by path_in_session_base(). Default-deny fallback path (gh#29, v2.1.5). Used when no delegation-complete callback is registered or the consumer returns REJECT. Refuses to write to anything outside session_base_.

Parameters
  • delegation_id — Short id (becomes the basename).
  • patch — Unified-diff text to persist.
  • delegation_id — Short id (basename for the .patch file).
  • patch — Unified-diff text.
Returns
  • Path to the written file on success, std::nullopt on failure (logged at ERROR).
  • Path written, or std::nullopt on failure.

const std::filesystem::path & entropic::SandboxManager::project_dir() const

Get the project directory this manager snapshots from.

Returns
Project root path.

const std::filesystem::path & entropic::SandboxManager::session_base() const

Get this session's sandbox base directory.

Returns
Path to ~/.entropic/sandbox/<session-id>/.

std::string entropic::SandboxManager::next_delegation_id(const std::string &prefix)

Mint a sandbox id that no sibling delegation can collide with.

Mint a collision-free delegation id (gh#160). gh#160 (v2.13.0): delegation ids were d<depth>, so two delegations issued at the same depth — the ordinary lead-fans-out-to-specialists shape — both asked for d1. create_sandbox then wiped the first sandbox's directory, and pending/d1.patch overwrote the first delegation's patch with the second's. Only one of the two survived, silently. The counter lives here because this manager is the engine-scoped object every delegation shares; DelegationManager is constructed per delegation and cannot count its own siblings.

Parameters
  • prefix — Human-readable stem (e.g. "d1", "d1r", "pipeline").
  • prefix — Human-readable stem ("d1", "pipeline", ...).
Returns
  • <prefix>_<n>, unique for the life of this manager.
  • <prefix>_<n> where n is unique for this manager's lifetime.

entropic::SandboxResult

Final artifact emitted by a finalized sandbox.

std::string entropic::SandboxResult::patch

Unified diff text.

std::vector<std::filesystem::path> entropic::SandboxResult::files_touched

Relative paths that changed.

std::filesystem::path entropic::SandboxResult::base_dir

Snapshot the diff is against.

std::filesystem::path entropic::SandboxResult::head_dir

Final sandbox state.

entropic::ScopedSandbox

RAII directory swapper for sandbox-scoped tool execution.

using entropic::ScopedSandbox::SwapDirFn = void (*)( const std::string& session_key, const std::filesystem::path& path, bool entering, void* user_data) hook point

Callback type for directory swapping.

The facade implements this to iterate the registered servers of the session's workspace and call entropic_mcp_server_set_working_dir() on each. gh#160 (v2.13.0): carries the session key. The servers a swap must move are the ones the RUNNING SESSION's tools resolve against, and with concurrent_sessions on (gh#158) two sessions delegate at the same time — a swap that names no session is a swap that cannot be routed. gh#166 re-points the facade side of this at the session's workspace without touching the signature again. entering distinguishes the two calls of a scope: the facade takes its per-workspace swap lock on entry and releases it on restore, which is what keeps two concurrent sandboxed delegations from interleaving their swaps on one set of server objects.

Parameters
  • session_key — Session the delegation belongs to ("" = default).
  • path — New working directory.
  • entering — True on sandbox entry, false on restore.
  • user_data — Opaque pointer (facade context).

entropic::ScopedSandbox::ScopedSandbox(SwapDirFn swap_fn, void *user_data, std::string session_key, const std::filesystem::path &sandbox_path, const std::filesystem::path &original_path)

Construct and swap directories.

Parameters
  • swap_fn — Directory swap callback (may be null — no-op).
  • user_data — Opaque pointer for swap_fn.
  • session_key — Session whose tool root is being swapped.
  • sandbox_path — Target sandbox directory.
  • original_path — Directory to restore on destruction — the caller's ACTIVE root, which for a nested delegation is the parent's sandbox and not the project root (gh#160).
  • swap_fn — Directory swap callback.
  • original_path — Original directory to restore.

entropic::ScopedSandbox::~ScopedSandbox()

Restore the original directory.

Restore the caller's active directory (gh#160).

entropic::ScopedSandbox::ScopedSandbox(const ScopedSandbox &)=delete

ScopedSandbox & entropic::ScopedSandbox::operator=(const ScopedSandbox &)=delete