# Your Uncommitted Changes in a Sandbox (sync)

## Your uncommitted changes in a sandbox (sync)

Write a test next to your code and run it against your real app, before
you commit: claim a sandbox, sync your working tree into it, rebuild what
the change needs, and open it or run a flow on it. No commit, no image
build, no pull request.

```bash
cofactor sandbox claim --application "Project Tracker" --wait
# edit files
cofactor sandbox sync <id>
cofactor sandbox exec <id> -- cofactor-sandbox restart app
cofactor workflow run <flow> --sandbox <id>
# edit, and sync again
```


Start even the first sandbox from uncommitted files:

```bash
cofactor sandbox validate
cofactor sandbox claim --application "Project Tracker" --from-local --wait
# edit and iterate on the same copy
cofactor sandbox sync <id> --build
```

The CLI connects from a deterministic wip commit object using your Git
credentials; your HEAD and index stay untouched. For changes sync cannot
adopt (setup, prepare, seeds, personas or actions), validate and claim
--from-local again. After the final edit, make a fresh claim and verify
its printed source SHA with `verify-env --at <sha>`. No merge or user
commit is required. See `cli`, "Setting up a new GitHub application".


A plain sync only writes and deletes files; you decide what to restart.
`sync --build` also reruns the build steps whose inputs changed and restarts
the services marked `after_build`. Each sync extends the sandbox's lease by up to 2 hours, up to 8 hours
after the claim. Syncing needs the `sandbox:exec` scope for API keys.

## What a sync does

`sync` makes the sandbox's files match your working tree, the way git
sees it: tracked files with your uncommitted edits, plus untracked files
that aren't ignored. Ignored files (`node_modules/`, `.env` if you ignore it,
build output) are not synced. Your git index is never touched.

- GitHub application: your repository's top level syncs into the
  sandbox's checkout. Run `sync` anywhere inside the repository.


  With several repositories, run `sync` anywhere inside a declared
  repository: the CLI detects it and syncs its top level into the
  matching sandbox checkout; other repositories are left alone. Run it
  again from another member checkout to sync that member. `--to` is
  relative to the selected repository root. The definition directory
  remains the working directory for commands that do not select a repo.
  `--from-local` from a member checkout pins that member (from the
  definition repository's checkout it connects the application at the
  local commit, see `cli`); other repositories use their default-branch
  heads, resolved once for the revision. A repository not declared by
  the application cannot be synced or selected. Pass `--repo owner/name`
  to name the checkout's repository when its origin is not a github.com
  URL (a CI mirror, an SSH host alias).


- Pushed image: run `sync` from the directory holding
  cofactor.sandbox.yaml; it syncs into the image's WORKDIR (see
  "Making your image syncable" in `guide v2 upload`).

Before it writes anything, `sync` prints the files it will write (each
untracked one marked), the files it will delete, and what it keeps. Check
for a stray untracked file you didn't mean to send, such as an unignored
.env.

It ends with the synced tree id, which `cofactor sandbox show` and every
flow run on the sandbox record. A later commit of exactly these files has
the same tree (`git rev-parse <commit>^{tree}`, or `<commit>:<dir>` for a
pushed image's directory).

## Deletions

A file only the sandbox has is deleted only when all three hold:

- it isn't in your working tree,
- your ignore rules don't match it, and
- some commit in your local history once had a file at that path.

So a file you deleted, or a directory you removed, is deleted in the
sandbox, while everything your image or build put there (`node_modules/`,
`.venv/`, compiled assets, a script your Dockerfile copied elsewhere) is
kept. `sync` lists both.

Syncing again is always safe: it compares against the sandbox's real
files, so a sync that stopped part-way converges on the next one.

## Rebuild and restart

Services keep running the old code until you restart them:

```bash
cofactor sandbox exec <id> -- cofactor-sandbox restart app
```

`restart` names one or more services, waits until they're healthy, and
prints each one's status. An unknown name lists the sandbox's services.

Install dependencies, run migrations or a build with `exec` first: it runs
in the same directory and environment your services use (the definition's
directory in the checkout, or a pushed image's WORKDIR):

```bash
cofactor sandbox exec <id> -- npm ci
cofactor sandbox exec <id> -- npx prisma migrate deploy
```

Logs: each service writes `/var/lib/cofactor/logs/<service>.log`.

```bash
cofactor sandbox exec <id> -- tail -n 100 /var/lib/cofactor/logs/app.log
```

Let the definition do it (GitHub and pushed applications alike):

```bash
cofactor sandbox sync <id> --build
```

reruns each `lifecycle.build` step whose `inputs` changed (and every step
after it), then restarts the `after_build` services. Only this flag runs
them: a sync without it, a push and a claim never do. A pushed image's
steps run in its WORKDIR and come from the definition baked into the image,
so changing them takes a new `push`. The first `--build` on a claimed
sandbox has no record of earlier runs, so it runs every step.

## Compiled output

When the sandbox runs compiled output (a bundled frontend, compiled
TypeScript), build it locally and sync the result to where the sandbox
serves it:

```bash
bun run build
cofactor sandbox sync <id> frontend/dist --to static
```

`--to` is relative to the sync root (the checkout, or the image's
WORKDIR). This mirrors the local directory as is: ignore rules don't
apply, so a git-ignored dist/ is sent; every file in it is written, and
every other file under --to is deleted.
`show` and runs then record it as build output.

## Data is forward-only

A sync changes files, never data. A migration you ran stays applied when
you sync older files back, and seeded rows stay. To start over, destroy
the sandbox and claim a new one.

To run Playwright tests against the sandbox while you develop, see the
`playwright` topic.

## Run a flow on it

```bash
cofactor workflow run <flow> --sandbox <id>
```

The run uses this sandbox instead of claiming a fresh copy, and never
destroys it. The run targets the sandbox's own application and
environment (no --environment needed; naming another one is refused), the
sandbox must be ready, and it must be one you claimed, not a test run's. The run records the version and the synced tree it ran
against, as of when it started; `cofactor workflow debug overview <run>`
shows them. Syncing during a run changes the files under it, so sync
between runs.

## Limits

- Sync only sandboxes claimed with `cofactor sandbox claim`; a test run's
  own copy can't be synced.
- Symlinks and submodules are not synced (`sync` lists them).
- A single file larger than 63 MiB is refused; a sync of many files is
  sent in several requests.
- The `sync` port counts toward the 8-port limit (see `guide v2 ports`).
