Repository navigation
Description
Problem
The current core models a coding agent as running against a tool.Workspace, while command execution is injected separately through CommandRunner. Bash reconnects the two by passing ws.Root() as a local working directory.
That makes Workspace.Root() serve three different purposes:
- durable workspace identity;
- local filesystem location;
- command-execution working directory.
Those coincide for an in-process OS workspace but break down for a remote filesystem. A remote coding environment also needs a command executor operating against the exact same filesystem namespace and snapshot. The current contracts cannot express that affinity without leaking a remote host path into the engine.
The Edit read ledger is also optimized for local storage: Read does not return a version, RecordRead has no context and the OS adapter re-reads/hashes the file, while WasReadUnchanged and Write remain separate operations. A remote backend needs version-aware conditional writes to preserve the unchanged-since-read invariant without a network TOCTOU race.
Forking and merge-back similarly expose path-shaped state (forkRoot) instead of environment identities.
Principle
A coding agent runs inside an execution environment, not merely against a filesystem. The environment owns a coherent filesystem and command-execution namespace and may optionally support forks and merge-back.
The environment aggregates capabilities but does not absorb authorization policy.
Conceptually:
ExecutionEnvironment
├── durable, non-secret EnvironmentRef
├── FileWorkspace capability
├── CommandRunner capability (optional)
├── Fork capability (optional)
└── Merge capability (optional)
The filesystem and command runner supplied by one environment MUST address the same namespace/snapshot.
Proposed direction
Environment identity and rehydration
Introduce a neutral durable reference, for example:
type EnvironmentRef struct {
Backend string
ID string
}Composition resolves the reference through an environment factory. Local paths become adapter-private details rather than a universal core contract. Persist only non-secret identity; credentials and endpoints remain operator-side configuration.
Bound command execution
A CommandRunner should be bound to its environment instead of receiving ws.Root() for every call:
type CommandRunner interface {
Run(ctx context.Context, command string) (CommandResult, error)
}A local runner privately holds its local root. A remote runner privately holds the remote environment ID and asks the remote execution service to run inside it. Bash is advertised only when the environment has command capability.
Version-aware file operations
Replace the hidden read-ledger re-read with opaque file versions and conditional writes, conceptually:
type File struct {
Data []byte
Version FileVersion
}
Read(ctx, path) (File, error)
Write(ctx, path, data, expectedVersion) (newVersion, error)Edit records the version returned by Read, computes its replacement, and writes conditionally against that version. The backend performs the compare-and-write atomically. Create-only and overwrite behavior must have explicit expected-version semantics.
Environment-aware tools and delegation
Tools should receive the execution environment (or an equivalent narrow execution context) rather than only a filesystem workspace. Filesystem tools request the file capability; Bash requests the command capability; remote MCP tools may ignore both.
Forking returns a complete child environment—filesystem plus command runner—not only a child path. Merge operates on environment references rather than a forkRoot path. Local git worktrees, directory copies, remote snapshots, overlays, and remote git worktrees remain adapter strategies.
Security boundary
The environment supplies mechanism and tenant isolation. It does not decide agent authorization.
The existing governance layers continue to decide:
- whether a tool is allowed, denied, or requires approval;
- whether project content is trusted;
- whether path escapes are admitted;
- which tools are visible in plan/no-FS/read-only postures.
Remote services must independently authenticate and authorize environment IDs and must never trust a model-controlled path or identifier.
Scope
- Write a new ADR capturing the execution-environment principle, capability boundaries, identity/rehydration model, local and remote semantics, and migration plan.
- Replace the universal local-path
Workspace.Root()assumption with a neutral environment identity/reference. - Introduce an execution-environment abstraction aggregating file and optional command/fork/merge capabilities.
- Bind local and remote command runners to an environment; remove Bash's dependence on passing
ws.Root()asworkdir. - Redesign file read/write version semantics so Edit's read-before-edit-and-unchanged guarantee ends in an atomic conditional write.
- Refactor Workspace/Forker/Merger contracts around environment capabilities and references rather than exposed local paths.
- Update session snapshot fidelity and service rehydration to persist and resolve environment references.
- Adapt local OS, in-memory, no-FS, fork/copy/worktree, Bash, Subagent, Parallel, permission resolution, prompt instruction discovery, and server composition paths.
- Add a reference remote test adapter or protocol-level fake proving that files and Bash share one remote environment identity without requiring a local mount.
- Update API baselines, changelog, architecture/implementation docs and public user documentation as applicable.
Non-goals
- Moving permission or trust policy into the environment backend.
- Requiring every environment to provide Bash, forks, or merge-back.
- Treating
Sandboxas a security guarantee; direct-write/local environments may not be isolated. - Selecting a production remote storage or remote-execution vendor in this issue.
- Changing
Tool.ReadOnly(); its effect semantics remain the concurrency and safety default. - Reworking the core model→tool→result agent-loop algorithm.
Acceptance criteria
- Core loop and tool contracts no longer require a local absolute path to identify an environment or execute Bash.
- One environment construction path guarantees that its file and command capabilities address the same namespace/snapshot.
- Local OS, memory and no-FS environments preserve their current observable behavior.
- An environment without command capability does not advertise or execute Bash and fails honestly if invoked through a stale call.
- A remote test environment can read a file, run a command that observes that file, write through either capability, and observe the same resulting state through the other capability.
- Read returns an opaque version and Edit finishes with an atomic conditional write; a concurrent change produces a model-visible conflict instead of overwriting newer data.
- Write distinguishes create-if-missing from replace-if-version-matches without introducing an unconditional-overwrite escape in the agent-facing tool path.
- Forking produces a complete child environment whose commands execute against the child filesystem, never the parent.
- Merge-back addresses parent and child environment identities without exposing a required local
forkRootcontract. - Session persistence and restart rehydration restore the same environment identity and capabilities without persisting credentials.
- Existing path-escape, secret-scrubbing, read-before-edit, read-parallel/mutate-serial, child-isolation, no-FS, and restart-rehydration tests remain green or are replaced by stronger environment-level equivalents.
- Intentional exported API changes are recorded through
task api:updateandengine/CHANGELOG.md. task lint,task test,task api:check, and the offlinego run ./cmd/mecademosmoke test pass.
Sequencing
This is phase 2 and follows #461. The RunRequest consolidation should land first so changing the execution argument from Workspace to Environment affects one canonical normal-run method rather than three public variants.
Activity
refactor(engine): introduce execution environments (#503)