# Build a software factory

Run three tasks on three forks of one base VM, and let each agent push a branch and open a pull request.

Source: https://slashinfra.com/docs/build/software-factory
Last modified: 2026-11-30T16:36:19.000Z

Three issues become three pull requests. Each issue runs on its own fork of one base VM. Each agent pushes its branch, and the VM opens a pull request for it.

## Before you start

- An approved [account](https://app.slashinfra.com/) and an API key from the [API keys](https://app.slashinfra.com/keys) page. 
- A GitHub fine-grained token with Contents and Pull requests set to read and write for the repository, as in [Open pull requests](https://slashinfra.com/docs/agents/pull-requests). 
- Three open issues in that repository. This guide uses issues `101`, `102` and `103`. 
- Python 3.12 with `requests`, Node 22 for the TypeScript tab, or `jq` for the curl tab.

Set these once in your terminal.

```bash
export SI_API_KEY=si_...
export GH_PAT=github_pat_...
```

## 1. Store the token

The VM never reads the token. The key vault adds it to GitHub requests on the way out. Store it as a secret named `github`.

### curl

```bash
curl -sS -X POST "https://api.slashinfra.com/secrets" \
-H "Authorization: Bearer $SI_API_KEY" -H "Content-Type: application/json" \
-d "{\"name\":\"github\",\"value\":\"$GH_PAT\"}"
```

### Python

```python
import os, requests

res = requests.post(
"https://api.slashinfra.com/secrets",
headers={"Authorization": f"Bearer {os.environ['SI_API_KEY']}"},
json={"name": "github", "value": os.environ["GH_PAT"]},
)
res.raise_for_status()
print(res.text)
```

### TypeScript

```ts
const res = await fetch('https://api.slashinfra.com/secrets', {
method: 'POST',
headers: { Authorization: `Bearer ${process.env.SI_API_KEY!}`, 'Content-Type': 'application/json' },
body: JSON.stringify({ name: 'github', value: process.env.GH_PAT! }),
})
if (!res.ok) throw new Error(`secret failed: ${res.status}`)
console.log(await res.text())
```

Output

```text
{"id":"sec_6f1c0f0e8a","name":"github",...}
```

Set `SECRET` to the `id` in the reply. The secret answers with its name and id only, never its value.

```bash
export SECRET=sec_6f1c0f0e8a
```

## 2. Create the base VM

Create the VM that the forks copy. Two rules send the token to GitHub: one for `git` over HTTPS, one for the GitHub API that `gh` calls. The `GH_TOKEN` variable holds a placeholder, so `gh` sends a header.

### curl

```bash
curl -sS -X POST "https://api.slashinfra.com/svms" \
-H "Authorization: Bearer $SI_API_KEY" -H "Content-Type: application/json" \
-d @- <<JSON
{"name":"factory-base","env":{"GH_TOKEN":"injected"},
"secret_injections":[
{"domain":"github.com","header":"authorization","value_format":"basic_x_access_token","secret_id":"$SECRET"},
{"domain":"api.github.com","header":"authorization","value_prefix":"Bearer ","secret_id":"$SECRET"}]}
JSON
```

### Python

```python
import os, requests

res = requests.post(
"https://api.slashinfra.com/svms",
headers={"Authorization": f"Bearer {os.environ['SI_API_KEY']}"},
json={
"name": "factory-base",
"env": {"GH_TOKEN": "injected"},
"secret_injections": [
{"domain": "github.com", "header": "authorization", "value_format": "basic_x_access_token", "secret_id": os.environ["SECRET"]},
{"domain": "api.github.com", "header": "authorization", "value_prefix": "Bearer ", "secret_id": os.environ["SECRET"]},
],
},
)
res.raise_for_status()
print(res.text)
```

### TypeScript

```ts
const secretId = process.env.SECRET!
const res = await fetch('https://api.slashinfra.com/svms', {
method: 'POST',
headers: { Authorization: `Bearer ${process.env.SI_API_KEY!}`, 'Content-Type': 'application/json' },
body: JSON.stringify({
name: 'factory-base',
env: { GH_TOKEN: 'injected' },
secret_injections: [
{ domain: 'github.com', header: 'authorization', value_format: 'basic_x_access_token', secret_id: secretId },
{ domain: 'api.github.com', header: 'authorization', value_prefix: 'Bearer ', secret_id: secretId },
],
}),
})
if (!res.ok) throw new Error(`create failed: ${res.status}`)
console.log(await res.text())
```

Output

```text
{"id":"6f1c0f0e8a","generation":1,...}
```

Set `BASE` to the `id` in the reply, then sign in to Claude Code in the base VM. Open its terminal from the dashboard, run `claude`, and complete the sign-in, as in [Fix a failing test with Claude Code](https://slashinfra.com/docs/agents/claude-code). Every fork copies that sign-in.

```bash
export BASE=6f1c0f0e8a
```

## 3. Clone the repository

Start a job in the base VM that clones the repository. Then wait for it to end. The wait repeats while the reply has `X-Timeout: 1`, as in [Background work](https://slashinfra.com/docs/build/background-work).

Keep the two functions from this step in the same file. Step 4 uses them.

### curl

```bash
JOB=$(curl -sS -X POST "https://$BASE.slashinfra.dev/usr/bin/job?cwd=/home/user&arg=-q" \
-H "Authorization: Bearer $SI_API_KEY" \
--data-binary 'git clone https://github.com/your-org/your-repo.git /home/user/app')

while HEADERS=$(curl -sS -D - -o /dev/null -X POST "https://$BASE.slashinfra.dev/usr/bin/job?arg=wait&arg=$JOB" \
-H "Authorization: Bearer $SI_API_KEY"); grep -qi '^X-Timeout: 1' <<<"$HEADERS"; do :; done
grep -i '^X-Exit-Code' <<<"$HEADERS" | cut -d' ' -f2 | tr -d '\r'
```

### Python

```python
import os, requests

def start_job(vm_id: str, script: str, cwd: str) -> str:
res = requests.post(
f"https://{vm_id}.slashinfra.dev/usr/bin/job",
headers={"Authorization": f"Bearer {os.environ['SI_API_KEY']}"},
params={"cwd": cwd, "arg": "-q"},
data=script.encode(),
)
res.raise_for_status()
return res.text.strip()

def wait_for_job(vm_id: str, job: str) -> int:
while True:
res = requests.post(
f"https://{vm_id}.slashinfra.dev/usr/bin/job",
headers={"Authorization": f"Bearer {os.environ['SI_API_KEY']}"},
params=[("arg", "wait"), ("arg", job)],
)
res.raise_for_status()
if "X-Timeout" not in res.headers:
return int(res.headers["X-Exit-Code"])

job = start_job(os.environ["BASE"], "git clone https://github.com/your-org/your-repo.git /home/user/app", "/home/user")
print(wait_for_job(os.environ["BASE"], job))
```

### TypeScript

```ts
const auth = { Authorization: `Bearer ${process.env.SI_API_KEY!}` }

async function startJob(vmId: string, script: string, cwd: string): Promise<string> {
const url = `https://${vmId}.slashinfra.dev/usr/bin/job?cwd=${encodeURIComponent(cwd)}&arg=-q`
const res = await fetch(url, { method: 'POST', headers: auth, body: script })
if (!res.ok) throw new Error(`job start failed: ${res.status}`)
return (await res.text()).trim()
}

async function waitForJob(vmId: string, job: string): Promise<number> {
const url = `https://${vmId}.slashinfra.dev/usr/bin/job?arg=wait&arg=${job}`
for (;;) {
const res = await fetch(url, { method: 'POST', headers: auth })
if (!res.ok) throw new Error(`wait failed: ${res.status}`)
if (!res.headers.has('X-Timeout')) return Number(res.headers.get('X-Exit-Code'))
}
}

const job = await startJob(process.env.BASE!, 'git clone https://github.com/your-org/your-repo.git /home/user/app', '/home/user')
console.log(await waitForJob(process.env.BASE!, job))
```

Output

```text
0
```

## 4. Run the three issues

For each issue, fork the base VM, then start an agent that fixes the issue, pushes its branch and opens a pull request. The forks run at the same time. The output lists each fork’s id and the exit code of its job. A pull request step that fails leaves a non-zero code.

The forks carry the label `run=factory`, so step 5 finds them. `--permission-mode bypassPermissions` lets Claude Code edit files and run commands without asking. A job has no one to answer a prompt, and the agent works inside its own VM.

### curl

```bash
VMS=()
JOBS=()
for ISSUE in 101 102 103; do
VM_ID=$(curl -sS -X POST "https://api.slashinfra.com/svms" \
-H "Authorization: Bearer $SI_API_KEY" -H "Content-Type: application/json" \
-d "{\"name\":\"issue-$ISSUE\",\"fork_of\":\"$BASE\",\"labels\":{\"run\":\"factory\"}}" | jq -r .id)
JOB=$(curl -sS -X POST "https://$VM_ID.slashinfra.dev/usr/bin/job?cwd=/home/user&arg=-q" \
-H "Authorization: Bearer $SI_API_KEY" \
--data-binary "cd /home/user/app && git checkout -b issue-$ISSUE && claude -p --permission-mode bypassPermissions 'Read GitHub issue $ISSUE with gh issue view, fix it and commit the fix.' && git push -u origin issue-$ISSUE && gh pr create --head issue-$ISSUE --fill")
VMS+=("$VM_ID")
JOBS+=("$JOB")
done

for i in "${!VMS[@]}"; do
while HEADERS=$(curl -sS -D - -o /dev/null -X POST "https://${VMS[$i]}.slashinfra.dev/usr/bin/job?arg=wait&arg=${JOBS[$i]}" \
-H "Authorization: Bearer $SI_API_KEY"); grep -qi '^X-Timeout: 1' <<<"$HEADERS"; do :; done
echo "${VMS[$i]} $(grep -i '^X-Exit-Code' <<<"$HEADERS" | cut -d' ' -f2 | tr -d '\r')"
done
```

### Python

```python
import os, requests

def fork_vm(issue: int) -> str:
res = requests.post(
"https://api.slashinfra.com/svms",
headers={"Authorization": f"Bearer {os.environ['SI_API_KEY']}"},
json={"name": f"issue-{issue}", "fork_of": os.environ["BASE"], "labels": {"run": "factory"}},
)
res.raise_for_status()
return res.json()["id"]

runs = []
for issue in [101, 102, 103]:
vm_id = fork_vm(issue)
script = (
f"cd /home/user/app && git checkout -b issue-{issue} && "
f"claude -p --permission-mode bypassPermissions 'Read GitHub issue {issue} with gh issue view, fix it and commit the fix.' && "
f"git push -u origin issue-{issue} && gh pr create --head issue-{issue} --fill"
)
runs.append((vm_id, start_job(vm_id, script, "/home/user")))

for vm_id, job in runs:
print(vm_id, wait_for_job(vm_id, job))
```

### TypeScript

```ts
async function forkVm(issue: number): Promise<string> {
const res = await fetch('https://api.slashinfra.com/svms', {
method: 'POST',
headers: { Authorization: `Bearer ${process.env.SI_API_KEY!}`, 'Content-Type': 'application/json' },
body: JSON.stringify({ name: `issue-${issue}`, fork_of: process.env.BASE!, labels: { run: 'factory' } }),
})
if (!res.ok) throw new Error(`fork failed: ${res.status}`)
const svm = (await res.json()) as { id: string }
return svm.id
}

const runs: { vmId: string; job: string }[] = []
for (const issue of [101, 102, 103]) {
const vmId = await forkVm(issue)
const script = `cd /home/user/app && git checkout -b issue-${issue} && claude -p --permission-mode bypassPermissions 'Read GitHub issue ${issue} with gh issue view, fix it and commit the fix.' && git push -u origin issue-${issue} && gh pr create --head issue-${issue} --fill`
runs.push({ vmId, job: await startJob(vmId, script, '/home/user') })
}

for (const run of runs) {
console.log(run.vmId, await waitForJob(run.vmId, run.job))
}
```

Output

```text
6f1c0f0e8a 0
7h2d1g1f9b 0
8j3e2h2g0c 0
```

## 5. Clean up

Delete the three forks, then the base VM, then the secret. The secret cannot be deleted while a VM still uses it.

### curl

```bash
for ID in $(curl -sS "https://api.slashinfra.com/svms?label.run=factory" -H "Authorization: Bearer $SI_API_KEY" | jq -r '.svms[].id'); do
curl -sS -o /dev/null -w "%{http_code}\n" -X DELETE "https://api.slashinfra.com/svms/$ID" -H "Authorization: Bearer $SI_API_KEY"
done
curl -sS -o /dev/null -w "%{http_code}\n" -X DELETE "https://api.slashinfra.com/svms/$BASE" -H "Authorization: Bearer $SI_API_KEY"
curl -sS -o /dev/null -w "%{http_code}\n" -X DELETE "https://api.slashinfra.com/secrets/$SECRET" -H "Authorization: Bearer $SI_API_KEY"
```

### Python

```python
import os, requests

auth = {"Authorization": f"Bearer {os.environ['SI_API_KEY']}"}
listing = requests.get("https://api.slashinfra.com/svms", headers=auth, params={"label.run": "factory"})
listing.raise_for_status()
for svm in listing.json()["svms"]:
print(requests.delete(f"https://api.slashinfra.com/svms/{svm['id']}", headers=auth).status_code)
print(requests.delete(f"https://api.slashinfra.com/svms/{os.environ['BASE']}", headers=auth).status_code)
print(requests.delete(f"https://api.slashinfra.com/secrets/{os.environ['SECRET']}", headers=auth).status_code)
```

### TypeScript

```ts
const auth = { Authorization: `Bearer ${process.env.SI_API_KEY!}` }
const listing = await fetch('https://api.slashinfra.com/svms?label.run=factory', { headers: auth })
if (!listing.ok) throw new Error(`list failed: ${listing.status}`)
const { svms } = (await listing.json()) as { svms: { id: string }[] }

const targets = [
...svms.map((svm) => `https://api.slashinfra.com/svms/${svm.id}`),
`https://api.slashinfra.com/svms/${process.env.BASE!}`,
`https://api.slashinfra.com/secrets/${process.env.SECRET!}`,
]
for (const target of targets) {
const res = await fetch(target, { method: 'DELETE', headers: auth })
console.log(res.status)
}
```

Output

```text
204
204
204
204
204
```

> Caution
> 
> 
> 
> Deletes the VMs and their disks. The pushed branches and pull requests stay on GitHub. Deletion cannot be undone.

## Check it works

You should now see three open pull requests in your repository, one for each branch: `issue-101`, `issue-102` and `issue-103`.

## If it doesn’t work

| Symptom | Cause | Fix |
| --- | --- | --- |   
| A job exits with `1` and `gh` prints a `401` or `403` | The token lacks write access to Contents or Pull requests. | Create a token with both set to write, then replace the value with [`PUT /secrets/{ref}`](https://slashinfra.com/docs/api-reference/control/secrets#putSecretsByRef). | 
| `409` on a create | A VM or a secret with that name exists. | Delete the old one, then run the step again. | 
| `409` with `secret_in_use` in step 5 | A VM still uses the secret. | Delete the VMs first, then the secret. |

## Next

- [Open pull requests](https://slashinfra.com/docs/agents/pull-requests): the token rules and the `github` secret. 
- [One VM per task](https://slashinfra.com/docs/agents/many-tasks): the same forks, started from the CLI. 
- [Background work](https://slashinfra.com/docs/build/background-work): the jobs that run the agents.
