# Forks

Running multiple versions of a runtime with recursive forks.



**Definitions**

A beam runtime is an ephemeral micro-VM that expires in 11 hours. Beam has identity, state and virtual network.
A phase is a versioned and layered images for beam runtimes. A phase holds filesystem state and its build instructions.

In this guide, we will start a runtime, make some changes in it and create a fork and a child to compare.

<!-- illustration
id: phase-snapshots
style: docs-isometric
type: animated
title: Forking state
description: Fork state between agent runs.

Show a first step - beam with a foundation - a snapshot at a start. Show a second stage that takes
the snapshot layer, moves it to the right. Show a third stage that takes this snapshot and starts another beam.

-->

**Install the client tools and sign in**

Beams is a [Teleport](https://goteleport.com) project and uses [official Teleport client tools](https://goteleport.com/download/client-tools/).

The client tools include `tsh` to sign in and run commands, and `tctl` to administer your cluster if you have permission.



Cluster: `example.beams.run`. Edit this example to use your cluster name.

Install version **18.11.10** of the client:

**macOS**

Download and open the macOS installer, then follow its instructions.

```sh
curl -O https://cdn.teleport.dev/teleport-tools-18.11.10.pkg
open teleport-tools-18.11.10.pkg
```

**Linux**

For Linux x86-64, download and install the client tools:

```sh
curl -O https://cdn.teleport.dev/teleport-v18.11.10-linux-amd64-bin.tar.gz
tar -xzf teleport-v18.11.10-linux-amd64-bin.tar.gz
sudo ./teleport/install
```

**Windows**

For Windows x86-64, run these commands in PowerShell. Add the extraction directory (`$env:LOCALAPPDATA\Teleport`) to your user PATH to use `tsh` in future terminals; the last command adds it to this terminal's PATH.

```powershell
Invoke-WebRequest -Uri https://cdn.teleport.dev/teleport-v18.11.10-windows-amd64-bin.zip -OutFile teleport.zip
Expand-Archive -Path teleport.zip -DestinationPath "$env:LOCALAPPDATA\Teleport" -Force
$env:Path += ";$env:LOCALAPPDATA\Teleport"
```

Follow the official client [tools install guide](https://goteleport.com/download/client-tools/) for other versions and architectures.

Check that you’re running v18.11.10 of the client with `tsh version`.

```sh
tsh version
```

**Passwordless**

I sign in without a password (e.g. a passkey).

```sh
tsh login --proxy='example.beams.run' --auth=passwordless
```

**Password \& MFA**

I have a password & MFA set up for my account:

Replace `you@example.com` with your Teleport username.

```sh
tsh login --proxy='example.beams.run' --user=you@example.com
```

**SSO**

I sign in with SSO (e.g. Okta, GitHub, SAML).

```sh
tsh login --proxy='example.beams.run'
```

## Start a beam

This command creates a **build beam**. Any snapshots to it's `/home` will be saved to a phase image with the same name:

**Local terminal**

```sh
# create a build beam workspace and associated phase with the same name
tsh phase add workspace
```

Save one file:

**Build beam: workspace**

```sh
printf 'v1\n' > ~/example.txt
tsh phase snapshot --final
```

`--final` saves the files, ends the build beam and returns you to your local terminal.

The new phase's parent defaults to `default:v1` phase image.

## Create a child

Create a child of `workspace`:

**Local terminal**

```sh
tsh phase add child --from=workspace
```

Save without changing any files:

**Build beam: child**

```sh
tsh phase snapshot --final
```

The child's parent phase is `workspace`. It follows `workspace`'s published images. We will revisit `child` later.

## Create a fork

To compare, let's create a fork of a `workspace`.

Fork `workspace`'s saved files:

**Local terminal**

```sh
tsh phase add fork --fork-from=workspace
```

Save the fork:

**Build beam: fork**

```sh
tsh phase snapshot --final
```

The fork copies workspace's saved `v1` file but keeps `workspace`'s parent, `default:v1`.
It does not follow workspace.

## Change the source

Let's re-open `workspace`'s build beam to change the `workspace` phase:

**Local terminal**

```sh
tsh phase edit workspace
```

Change the file and snapshot:

**Build beam: workspace**

```sh
printf 'v2\n' > ~/example.txt
tsh phase snapshot --final
```

**Tip**

To leave an agent working in a fork after disconnecting, run it as a managed service with [beamctl](/docs/guides/long-running-agents/). The agent must explicitly save its work before the beam expires. Disconnecting with Ctrl-D neither saves the fork nor ends its beam.


## Fork vs child

A `child` keeps a parent relationship with `workspace`. A `fork` takes a one-time copy
of `workspace`'s saved files, but keeps workspace's parent, `default:v1`.

When workspace publishes `v2`, the child inherits that file after refresh. The
fork keeps its copied `v1`. Both phases still follow their own parent's updates,
and existing running beams do not change.

Here is the end state:

| Phase | Parent | `~/example.txt` in a new beam after refresh |
| --- | --- | --- |
| workspace | `default:v1` | `v2` |
| child | `workspace` | `v2` |
| fork | `default:v1` | `v1` |

**Tip**

If you edit the same file in the child, its saved changes override the parent's file.

<!-- illustration
id: phase-child-vs-fork
style: docs-isometric
type: animated
title: Fork vs child
description:

Show four phase objects as saved-file stacks, not running beams. Place default:v1
above workspace and fork, with child below workspace. Label each object with its
phase name. Solid arrows point from parent to descendant: default:v1 to workspace,
default:v1 to fork, and workspace to child. There must never be a parent arrow
from workspace to fork.

Use three selectable states:

1. Source saved: show default:v1 and workspace. Workspace's saved file is labelled
   "~/example.txt: v1". Do not invent a value for that file in default:v1.
2. Child and fork created: reveal child and fork and their parent relationships.
   Both show "~/example.txt: v1". Child inherits the file from workspace; fork
   receives a copied saved-file plate from workspace along a dashed arrow labelled
   "One-time file copy". Move only the copied plate; workspace retains its files.
   The dashed arrow is visible during this creation state, not a lasting dependency.
3. Source changed and descendants refreshed: workspace's file becomes v2, then
   the refreshed child's file becomes v2. Fork's file remains v1. Remove the dashed
   copy arrow and retain all solid parent relationships unchanged. Make the final
   file values readable together so the difference is clear without motion.

Keep object names and file-label positions stationary as values change. Do not
imply that child has saved a local override, that fork follows workspace, that
fork has no parent, or that refreshing phases changes existing running beams.
Show the completed third state in the no-JavaScript fallback,
with the parent routes and v2/v2/v1 comparison intact.
-->

**Tip**

You can only use `--fork-from=workspace` for saved phases.
To fork unsaved work, run `tsh phase add experiment --fork` **inside workspace's build beam** instead.

Forks only copy files, not running processes or memory. See the [fork restrictions](/docs/api/phase/#snapshot-and-edit-restrictions).

#### A fork or a child?

| Goal                                             | Command                                          | Relationship                                                         |
| ------------------------------------------------ | ------------------------------------------------ | -------------------------------------------------------------------- |
| Make an independent copy of saved work           | `tsh phase add experiment --fork-from=workspace` | Copies the snapshot and follows the source's parent.                 |
| Capture current work in a build beam             | `tsh phase add experiment --fork`                | Copies the running filesystem into a new phase with the same parent. |
| Add another stage that follows a phase's updates | `tsh phase add app-work --from=app-tools`        | Creates a child of `app-tools`.                                      |
