Isolated Coding Example¶
Demonstrates CodingEnsemble.runIsolated() which runs a coding agent in a git worktree,
keeping changes isolated from the main working tree.
What It Does¶
- Accepts a git repository root as a command-line argument
- Creates an isolated git worktree (branch from HEAD)
- Runs a feature-implementation task inside the worktree
- Preserves the worktree on success so you can review and merge
Code¶
ChatModel model = OpenAiChatModel.builder()
.apiKey(System.getenv("OPENAI_API_KEY"))
.modelName("gpt-4o")
.build();
EnsembleOutput output = CodingEnsemble.runIsolated(
model,
Path.of("/path/to/git/repo"),
CodingTask.implement("Add a README.md file with a project overview"));
System.out.println(output.getRaw());
Running¶
export OPENAI_API_KEY=sk-...
./gradlew :agentensemble-examples:runIsolatedCoding --args="/path/to/git/repo"
The argument must be the root of a git repository.
How Isolation Works¶
runIsolated() uses the agentensemble-workspace module to:
- Create a worktree:
git worktree add -b agent-<uuid> .agentensemble/workspaces/agent-<uuid> HEAD - Scope tools: All coding tools operate inside the worktree directory
- Run the ensemble: The agent reads, edits, and tests code in the worktree
- Preserve on success: The worktree stays so you can
cdinto it, review changes, and merge - Clean up on failure: If the agent fails, the worktree is removed automatically
After a successful run, you can merge the changes:
Or cherry-pick specific commits from the worktree branch.