> ## Documentation Index
> Fetch the complete documentation index at: https://docs.fibre.dev/llms.txt
> Use this file to discover all available pages before exploring further.

# A coding agent on your repo

> Hand a failing test to Claude Code in a sandbox: it gets the repo, the tools and a shell of its own, fixes the bug, proves it with the tests, and you review the diff before anything leaves the sandbox.

Coding agents such as Claude Code can **fix bugs, add features and upgrade dependencies** on their own. They work
best when they can actually run the code while they work: install packages, run the tests, and try again when
something fails. On your laptop, that means letting a model run commands on your own machine. In a sandbox, **the
agent gets a machine of its own**, and you decide what comes back out of it.

The whole process is a simple loop:

1. **Start a sandbox** and put your repository in it.
2. **Give the agent its task**, together with an API key for its model, for that one command only.
3. **Check the agent's work yourself**: run the tests and read the changes.
4. **Ship the change** by pushing a branch, or **ask for more changes**. Between rounds, the sandbox sleeps with
   everything in place.

## The task

To keep the example small, the repository is a single Python file with a real bug: `average([])` divides by zero,
but a test says it should return `0`. Follow the steps to have Claude Code fix it.

**1. A sandbox with the repo**

```bash title="CLI" run theme={null}
SBX=$(fibre create --size medium --lifetime 2h --idle-timeout 10m --label agent=fix-average --json | jq -r .id)

# in your project: fibre exec "$SBX" -- git clone https://github.com/you/your-repo /work/repo
printf 'def average(xs):\n    return sum(xs) / len(xs)\n' | fibre exec "$SBX" -i -- sh -c 'mkdir -p /work/repo && cat > /work/repo/stats.py'
fibre exec "$SBX" -i -- sh -c 'cat > /work/repo/test_stats.py' <<'PY'
import unittest
from stats import average

class TestAverage(unittest.TestCase):
    def test_numbers(self):
        self.assertEqual(average([2, 4, 6]), 4)
    def test_empty(self):
        self.assertEqual(average([]), 0)
PY
fibre exec "$SBX" --cwd /work/repo -- "git init -q && git add . && git -c user.name=dev -c user.email=dev@example.com commit -qm 'initial'"
```

```typescript title="TypeScript" run theme={null}
import { Sandbox } from "@fibre-dev/sdk";

const sbx = await Sandbox.create({ size: "medium", lifetime: 7200, idleTimeout: 600, labels: { agent: "fix-average" } });

// in your project: git clone https://github.com/you/your-repo /work/repo
await sbx.files.write("/work/repo/stats.py", "def average(xs):\n    return sum(xs) / len(xs)\n");
await sbx.files.write("/work/repo/test_stats.py", [
  "import unittest",
  "from stats import average",
  "",
  "class TestAverage(unittest.TestCase):",
  "    def test_numbers(self):",
  "        self.assertEqual(average([2, 4, 6]), 4)",
  "    def test_empty(self):",
  "        self.assertEqual(average([]), 0)",
].join("\n") + "\n");
await sbx.run("cd /work/repo && git init -q && git add . && git -c user.name=dev -c user.email=dev@example.com commit -qm 'initial'");
```

```python title="Python" run theme={null}
from fibre import Sandbox

sbx = Sandbox.create(size="medium", lifetime=7200, idle_timeout=600, labels={"agent": "fix-average"})

# in your project: git clone https://github.com/you/your-repo /work/repo
sbx.files.write("/work/repo/stats.py", "def average(xs):\n    return sum(xs) / len(xs)\n")
sbx.files.write("/work/repo/test_stats.py", "\n".join([
    "import unittest",
    "from stats import average",
    "",
    "class TestAverage(unittest.TestCase):",
    "    def test_numbers(self):",
    "        self.assertEqual(average([2, 4, 6]), 4)",
    "    def test_empty(self):",
    "        self.assertEqual(average([]), 0)",
]) + "\n")
sbx.run("cd /work/repo && git init -q && git add . && git -c user.name=dev -c user.email=dev@example.com commit -qm 'initial'")
```

**2. See the failure**

```bash title="CLI" run theme={null}
fibre exec "$SBX" --cwd /work/repo -- 'python3 -m unittest -q 2>&1 | tail -n 3'   # … ZeroDivisionError … FAILED (errors=1)
```

```typescript title="TypeScript" run theme={null}
const before = await sbx.run("cd /work/repo && python3 -m unittest -q 2>&1 | tail -n 3");
console.log(before.exitCode, before.stdout); // 0 … ZeroDivisionError … FAILED (errors=1)
```

```python title="Python" run theme={null}
before = sbx.run("cd /work/repo && python3 -m unittest -q 2>&1 | tail -n 3")
print(before.exit_code, before.stdout)  # 0 … ZeroDivisionError … FAILED (errors=1)
```

**3. Install the agent**

Claude Code's installer takes about a minute. You can install it in each sandbox, or install it once in a
[snapshot](https://docs.fibre.dev/setup/snapshots.md) and start every task from that.

```bash title="CLI" run theme={null}
fibre exec "$SBX" --timeout 10m -- 'curl -fsSL https://claude.ai/install.sh | bash > /dev/null && ~/.local/bin/claude --version'
```

```typescript title="TypeScript" run theme={null}
const install = await sbx.run("curl -fsSL https://claude.ai/install.sh | bash > /dev/null && ~/.local/bin/claude --version", { timeout: 600 });
console.log(install.stdout.trim()); // 2.1.… (Claude Code)
```

```python title="Python" run theme={null}
install = sbx.run("curl -fsSL https://claude.ai/install.sh | bash > /dev/null && ~/.local/bin/claude --version", timeout=600)
print(install.stdout.strip())  # 2.1.… (Claude Code)
```

**4. Give it the task**

Now give the agent its task. The model's API key goes in this command's `env` only, so other commands in the
sandbox never see it. `--allowedTools` lists what the agent may do without asking for permission. Since nobody is
there to answer its questions, anything not on that list is simply refused. Here it may read and edit files, and
run the tests.

The output is streamed, so you can watch the agent work as it happens:

```bash title="CLI" norun reason="needs your Anthropic API key" theme={null}
# the agent's output streams here as it thinks and acts
fibre exec "$SBX" --cwd /work/repo \
  --env ANTHROPIC_API_KEY="$ANTHROPIC_API_KEY" \
  --env TASK="test_empty fails: average([]) should return 0. Fix stats.py and run the tests until they pass." \
  -- '~/.local/bin/claude -p "$TASK" --allowedTools "Read,Edit,Bash(python3 -m unittest*)" --output-format stream-json --verbose'
```

```typescript title="TypeScript" norun reason="needs your Anthropic API key" theme={null}
const agent = await sbx.spawn(
  ["sh", "-c", 'cd /work/repo && ~/.local/bin/claude -p "$TASK" --allowedTools "Read,Edit,Bash(python3 -m unittest*)" --output-format stream-json --verbose'],
  {
    env: {
      ANTHROPIC_API_KEY: process.env.ANTHROPIC_API_KEY!,
      TASK: "test_empty fails: average([]) should return 0. Fix stats.py and run the tests until they pass.",
    },
  },
);

for await (const chunk of agent.output()) process.stdout.write(chunk.data);   // watch it think and act, live
const finished = await agent.wait();
```

```python title="Python" norun reason="needs your Anthropic API key" theme={null}
import os
import sys

agent = sbx.spawn(
    ["sh", "-c", 'cd /work/repo && ~/.local/bin/claude -p "$TASK" --allowedTools "Read,Edit,Bash(python3 -m unittest*)" --output-format stream-json --verbose'],
    env={
        "ANTHROPIC_API_KEY": os.environ["ANTHROPIC_API_KEY"],
        "TASK": "test_empty fails: average([]) should return 0. Fix stats.py and run the tests until they pass.",
    },
)

for chunk in agent.output():
    sys.stdout.write(chunk.data)  # watch it think and act, live
finished = agent.wait()
```

**5. Check its work yourself**

An agent can be wrong, even when it says it has finished. So **run the tests and read the changes yourself** before
anything leaves the sandbox:

```bash title="CLI" norun reason="runs after the agent's change" theme={null}
fibre exec "$SBX" --cwd /work/repo -- python3 -m unittest -q && echo "tests pass"

fibre exec "$SBX" --cwd /work/repo -- git diff
# -    return sum(xs) / len(xs)
# +    return sum(xs) / len(xs) if xs else 0
```

```typescript title="TypeScript" norun reason="runs after the agent's change" theme={null}
const after = await sbx.run("cd /work/repo && python3 -m unittest -q");
console.log(after.exitCode === 0 ? "tests pass" : after.stdout);

const diff = await sbx.run("cd /work/repo && git diff");
console.log(diff.stdout);
// -    return sum(xs) / len(xs)
// +    return sum(xs) / len(xs) if xs else 0
```

```python title="Python" norun reason="runs after the agent's change" theme={null}
after = sbx.run("cd /work/repo && python3 -m unittest -q")
print("tests pass" if after.exit_code == 0 else after.stdout)

diff = sbx.run("cd /work/repo && git diff")
print(diff.stdout)
# -    return sum(xs) / len(xs)
# +    return sum(xs) / len(xs) if xs else 0
```

**6. Ship it, or ask for more**

If you are happy with the change, push a branch from the sandbox, using a token that can only push to that
repository. Or read the diff out of the sandbox and apply it on your side.

If the change needs more work, **send your next instruction to the same sandbox**, even hours later. It wakes up
with the repository, the installed agent and the agent's session exactly where they were, and `--continue` picks
up the same conversation:

```bash title="CLI" norun reason="needs your Anthropic API key" theme={null}
fibre exec "$SBX" --cwd /work/repo --env ANTHROPIC_API_KEY="$ANTHROPIC_API_KEY" --env TASK="Also add a docstring to average." \
  -- '~/.local/bin/claude -p "$TASK" --continue --allowedTools "Read,Edit"'
```

```typescript title="TypeScript" norun reason="needs your Anthropic API key" theme={null}
await sbx.run(
  ["sh", "-c", 'cd /work/repo && ~/.local/bin/claude -p "$TASK" --continue --allowedTools "Read,Edit"'],
  { env: { ANTHROPIC_API_KEY: process.env.ANTHROPIC_API_KEY!, TASK: "Also add a docstring to average." } },
);
```

```python title="Python" norun reason="needs your Anthropic API key" theme={null}
sbx.run(
    ["sh", "-c", 'cd /work/repo && ~/.local/bin/claude -p "$TASK" --continue --allowedTools "Read,Edit"'],
    env={"ANTHROPIC_API_KEY": os.environ["ANTHROPIC_API_KEY"], "TASK": "Also add a docstring to average."},
)
```

## Clean up

```bash title="CLI" run theme={null}
fibre delete "$SBX"
```

```typescript title="TypeScript" run theme={null}
await sbx.delete();
```

```python title="Python" run theme={null}
sbx.delete()
```

## Make it production-ready

These changes turn the example into something you can run for real:

| Concern | What to do |
| - | - |
| Setup time | A [snapshot](https://docs.fibre.dev/setup/snapshots.md) with the agent installed and the repo cloned: each task starts in seconds |
| Private repos | Clone with a short-lived token that reads one repo; push with one that writes one branch |
| What the agent can reach | A [network allow list](https://docs.fibre.dev/production/network.md): your Git host, the package registry, the model API |
| The model key | In the command's `env` only; a key with a spending limit, revocable |
| Watching | Stream `--output-format stream-json` to your UI; open the sandbox's [desktop](https://docs.fibre.dev/connect/desktop.md) or browser for UI work |
| Cost | The sandbox sleeps while you review: a task that works 10 minutes costs 10 minutes |

## Related

> [card] [Claude Code in a sandbox](https://docs.fibre.dev/build-with-ai/claude-code.md)
>
> Every Claude Code option, its sessions, and the API key, explained in detail.

> [card] [Many agents in parallel](https://docs.fibre.dev/use-cases/parallel.md)
>
> One task per sandbox, with many running at once.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.