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

# Give an agent sandbox a repo

> Clone a repo into an Archil sandbox with a short-lived token, let an agent work, and push the result, without the token reaching the agent.

A coding agent usually needs a working copy of a repo, a place to run commands, and a way to hand its work back. With
Git hosting and [persistent sandboxes](https://docs.archil.com/compute/sandboxes/introduction.md), your code clones the repo into a sandbox with
a short-lived token, the agent works in the sandbox, and your code pushes the result. The token is passed only to the
`git` commands that need it, so it never lands in `.git/config` and the agent's own commands never see it.

## Clone, work, push

```typescript TypeScript theme={null}
import { Archil } from "disk";

const archil = new Archil({ git: { endpoint: process.env.ARCHIL_GIT_URL } });
const repo = await archil.git.repos.get(repoId);
const sandbox = await archil.sandboxes.create({ baseImage: "alpine/git" });

try {
  // 1. Clone with a read token, passed only to this command
  const read = await archil.git.tokens.credentials(repo, { scope: "read", ttlSeconds: 300 });
  await sandbox.exec(`git clone -q ${repo.remoteUrl} /work`, { env: read.env });

  // 2. The agent works in /work. Its commands get no token.
  await sandbox.exec("cd /work && date > agent-was-here.txt");

  // 3. Commit and push with a write token, passed only to this command
  const write = await archil.git.tokens.credentials(repo, { scope: "write", ttlSeconds: 300 });
  const result = await sandbox.exec(
    [
      "cd /work",
      "git add -A",
      "git -c user.name=agent -c user.email=agent@example.com commit -qm 'agent run'",
      `git push -q origin HEAD:agent/run-1`,
    ].join(" && "),
    { env: write.env },
  );
  console.log(`exit ${result.exitCode}`, result.stderr);
} finally {
  await sandbox.stop();
  await sandbox.delete();
}
```

```python Python theme={null}
import os
from archil import Archil

archil = Archil(git_endpoint=os.environ["ARCHIL_GIT_URL"])
repo = archil.git.repos.get(repo_id)
sandbox = archil.sandboxes.create(base_image="alpine/git")

try:
    # 1. Clone with a read token, passed only to this command
    read = archil.git.tokens.credentials(repo, scope="read", ttl_seconds=300)
    sandbox.exec(f"git clone -q {repo.remote_url} /work", env=read.env)

    # 2. The agent works in /work. Its commands get no token.
    sandbox.exec("cd /work && date > agent-was-here.txt")

    # 3. Commit and push with a write token, passed only to this command
    write = archil.git.tokens.credentials(repo, scope="write", ttl_seconds=300)
    result = sandbox.exec(
        " && ".join([
            "cd /work",
            "git add -A",
            "git -c user.name=agent -c user.email=agent@example.com commit -qm 'agent run'",
            "git push -q origin HEAD:agent/run-1",
        ]),
        env=write.env,
    )
    print("exit", result.exit_code, result.stderr)
finally:
    sandbox.stop()
    sandbox.delete()
```

Any image with `git` works; `alpine/git` is a small one. Pushing to a branch per run, such as `agent/run-1`, leaves
`main` for you to merge into after review. A [push webhook](https://docs.archil.com/git/webhooks.md) tells your backend when the branch arrives.

If the agent should push by itself, pass a write token in the environment of the agent's commands instead, or set it
for the whole sandbox when you create it. Keep the lifetime short, and give the token only the repos the agent needs.

## Network access

The sandbox reaches your git endpoint over HTTPS. If you set a [firewall](https://docs.archil.com/compute/sandboxes/firewall.md) policy on the
sandbox, allow your git endpoint's hostname. On the Free plan, sandboxes reach only a
[managed allowlist](https://docs.archil.com/compute/sandboxes/firewall.md#free-plan-allowlist), which doesn't list Git hosting endpoints.


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