Codumentor logo Codumentor Enterprise

Helper Processes — Running a Worker Under the Sandbox

Some plugins need a process of their own that outlives one tool call: the
Office editors keep each open document in a worker that parses it and runs the
agent's *_python snippets against it. Such a helper handles untrusted
input (a file the agent opened, code the agent wrote), so it must run with the
same containment as the shell tool, never as you on the host.

Two calls in codumentor.agent.helper_process do this for every kind of
conversation, so your plugin needs no sandbox-specific code:

CallWhenWhat it does
spawn_helper_process(strategy, argv, env=…, **stdio)Once, to start the helperStarts argv under the conversation's sandbox (see the table below).
async with helper_request(strategy):Around every request you send itChecks the protected paths: anything the helper created or changed during the request is moved out of the folder, and the tool call's result says so.

strategy is the conversation's execution strategy: self.execution_strategy
in a tool (or ctx["execution_strategy"] in a hook), and None when there is
none.

Starting the helper

import asyncio, sys
from codumentor.agent.helper_process import spawn_helper_process

proc = await spawn_helper_process(
    strategy,
    [sys.executable, "-u", WORKER_SCRIPT],    # the helper's own command
    env=env,                                  # its spawn env
    sandbox_env=("MY_WORKER_MODE",),          # names it keeps in the overlay sandbox
    label="diagram worker",                   # used in the refusal message
    stdin=asyncio.subprocess.PIPE,
    stdout=asyncio.subprocess.PIPE,
    stderr=asyncio.subprocess.PIPE,
    limit=8 * 1024 * 1024,                    # stream line limit, if you speak JSON lines
)

It returns an asyncio.subprocess.Process-shaped object: stdin (a stream
writer), stdout/stderr (stream readers), wait(), returncode,
terminate(), kill(). Only use those; on the Landlock backend it is not a
real asyncio.subprocess.Process.

What it starts depends on the conversation:

ConversationThe helper runsNotes
Light sandbox (light_sandbox plugin: bwrap, Landlock, Windows AppContainer or Low IL)Confined like a shell command, network offWrites only to the working folder and the session temp. Secrets and $CODUMENTOR_HOME are hidden. Codumentor's own code (interpreter, venv, src/ or the bundle) is readable to the helper alone on Linux, and to every confined process on Windows. cwd is the working folder.
Overlay sandbox (workspace_isolation)Inside the overlay's bwrapThe interpreter, packages and Codumentor's source are bound read-only. bwrap passes the spawn env through, so only the sandbox_env names survive.
Overlay configured but unusable (no bwrap)—Raises RuntimeError and does not fall back to the host. Turn it into a tool error.
No sandboxHost-direct, as youThe same as the shell tool in that conversation.

Paths you hand the helper must be the ones it sees: put every host path
through strategy.translate_for_subprocess(path) (identity outside the
overlay). The translate_for_worker helper in _office_common.tool_helpers
does this.

Every request: helper_request

from codumentor.agent.helper_process import helper_request

async with self._lock, helper_request(self._strategy):
    proc.stdin.write(line)
    await proc.stdin.drain()
    reply = await proc.stdout.readline()

On entry it fingerprints the protected paths (.git/hooks, .git/config,
.envrc, .vscode/tasks.json, … — the list in
doc/core-dev/design/light-sandbox.md §4.3). On exit, also when the block
raises, it moves whatever the helper created or replaced to the session's
quarantine folder. These are files something on the host runs later, outside
any sandbox, and the shell tool gets the same check after each command.
The note naming what moved is **added to the result of the tool call in flight
for you**: your tool returns its result as usual, and core appends the line
before the result is stored, shown to the model or drawn on the tool card.
Outside a tool call (an HTTP route of yours, session teardown) it is logged.
The yielded object's .note holds the same text if you want it.

Rules of thumb:

Not covered: a process the helper leaves running can write between two
requests, after one check and before the next snapshot. It dies with the
helper under bwrap (its PID namespace) and on Windows (its job object), not
under Landlock. Don't let agent code spawn processes: the Office workers'
code_safety rejects imports outright.

Packaged builds

In a packaged (frozen) Codumentor, sys.executable is the Codumentor binary,
not a Python interpreter. The built-in editors re-execute it with a format key
(_office_common/frozen_worker.py, the CODUMENTOR_OFFICE_WORKER env var,
kept in the overlay through sandbox_env); that map is not yet open to
external plugins. Under the light sandbox the binary and its bundle are
readable to the helper, as the source tree is in a source run.

A complete example

codumentor/plugins/_office_common/worker_client.py (OfficeWorkerClient) is
the reference: a JSON-lines client that starts its worker with
spawn_helper_process, wraps send_command and the shutdown in
helper_request, and drains stderr in the background. A new worker that
speaks the same protocol can subclass it and set worker_script.

Testing

Extra result notes from inside a call

helper_request is built on a general seam you can use for anything that
runs inside a tool call but is not the tool itself:
codumentor.agent.tool_result_notes.add_pending_note(text) appends one line
to the result of the call in flight. It returns False outside a call. For a
note that depends on the finished result, use the onToolResult hook
(Backend Hooks).

For strategy authors

Both calls duck-type the strategy, so a new execution strategy opts in
without core changes by implementing:

MethodMeaning
confines_helpers() -> boolspawn_helper starts helpers confined.
async spawn_helper(argv, , env, *stdio)Start one: a Process-shaped object, network off, stdio including limit.
protected_paths_guard()An async context manager yielding a ProtectedPathsCheck whose note it sets on exit.

ConfinedExecutionStrategy (plugins/light_sandbox/strategy.py) is the
implementation; its shell commands use the same guard.