# How projects work

> How files are kept between runs, merged and previewed, in the code.

Source: https://werker.cloud/docs/engineering/projects (updated 2026-12-02)

A project is the folder of files a workflow, agent or factory keeps
between runs. A run starts with its project's files in `/work` and
saves `/work` back when a step finishes. Every file Werker keeps is a
file of a project: the files a step names in its result are recorded
as files of the version that step saved (`step_files`), not copied.

## Which project a run uses

`run_owner` in `packages/db/src/projects.ts` decides, in this order:

| The run is                                                 | Its project is                   |
| ---------------------------------------------------------- | -------------------------------- |
| one where the person mentioned a project on `/new`         | that project (`runs.project_id`) |
| a factory member's turn                                    | the factory's                    |
| a run of saved work made by a run that mentioned a project | that project                     |
| a run of a saved workflow                                  | the workflow's                   |
| a run of a saved agent                                     | the agent's                      |
| anything else with a sandbox                               | the run's own                    |

Each workflow, agent and factory has at most one project
(`projects.workflow_id`, `agent_id`, `factory_id`), made the first
time one of its runs saves and named after it. A run from `/new` saves
an agent or workflow first, so it uses that one. A schedule runs a
saved workflow or agent and so carries on in that project. A run that
belongs to none of them has a project of its own (`projects.run_id`),
named after what it was asked.

A project mentioned with `@` on `/new`
([ui](ui.md#mentions-and-commands-on-new)) is checked against the
workspace when the run is queued and kept as `runs.project_id`; the
planner is told to plan the work as a change to it. The run loads it
into `/work` and saves back to it as its next version, and later runs
of the agent or workflow that run saved go back to it, ahead of a
project of that agent's or workflow's own. If the project is deleted
the column is cleared and the usual order applies.

## How files move

A sandbox needs `tar`, not Git. Git is used only on the worker.

- **In:** before a sandbox's first command, the project's newest
  version is packed (`git archive`), written into the sandbox and
  unpacked into `/work` (`project_sandbox` in
  `packages/runtime/src/project/project.ts`). The version is recorded
  in `/work/.werker/version`, so a sandbox picked up again after a
  worker restart is not loaded twice.
- **Out:** when a step finishes done, `/work` is packed with `tar`,
  read out, unpacked on the worker and committed
  (`save_project_version` in
  `packages/connections/src/projects/store.ts`). The commit message is
  the step's name and summary.
- **Left out:** the folders in `project_excluded_folders`
  (`packages/domain/src/projects.ts`: `node_modules`, `.venv`,
  `__pycache__` and the like), and whatever the project's own
  `.gitignore` names.
- **Moving bytes:** `write_sandbox_file` and `read_sandbox_file`
  (`packages/connections/src/sandbox/transfer.ts`) use only the
  sandbox's shell, in pieces, so they work on every adapter. Measured
  on Daytona: 5 MB in took 17 s, 50 MB out took 14 s.
- **Limit:** a packed project is at most 200 MB (`max_project_bytes`).
- **A step is told:** `project_note`, added to what a step is told
  about its sandbox. It includes `preview_note`: keep
  `werker.preview.json` right
  ([Seeing a project running](#seeing-a-project-running)).

## Files a step names

`finish_step` takes paths under `/work` to show with the result. When
the step finishes, `/work` is saved first; each named path must then
be a file of that version, or the step is told which is not (missing,
outside `/work`, or in a folder that is not saved) and can report
again (`step_files` in `packages/runtime/src/project/files.ts`). A row
in `step_files` holds the path, name, type and size, with the project
and version that hold the content. `/runs/<id>/files/<file id>` reads
the content from the project (`get_run_file` in
`packages/core/src/runs/reads.ts`), up to 50 MB. There is no limit on
how many files a step names.

## Where projects are kept

One bare Git repository for each project, at
`<projects folder>/<workspace id>/<project id>.git`. Postgres holds
which projects exist (`projects`) and which run saved which version
(`project_saves`); the files are not in the database.

- **Development:** `data/projects/` in the repository.
- **Self-host install:** the `projects` volume in
  `deploy/compose.yaml`, mounted read-write in the worker and
  read-only in the web app. The image has Git.
- **Setting:** `WERKER_PROJECTS_DIR` for the worker and the web app.
  Both must see the same folder.
- **Backups:** a database backup does not include projects. The
  projects folder has to be backed up too.

## Deleting a project

`delete_project` (`packages/db/src/project-removals.ts`) locks the
`projects` row and, in one transaction, deletes it and inserts a row
in `project_removals`. Previews, saves and `step_files` go with the
project (`ON DELETE CASCADE`); `runs.project_id` is set null, and the
run stays.

It is refused, with a message, while a run that `run_owner` resolves
to the project has an active status, or a preview of it has a live
status. The `projects.delete` operation (`delete:run`) turns the
refusal into a conflict and records `project.deleted`.

The web app mounts the projects folder read-only, so a worker removes
the files. The insert notifies the `project_removals` channel. The
loop in `packages/runtime/src/loops/project-removals.ts` listens on
it, polls every 30 seconds, reads the rows through
`pending_project_removals()` (every workspace's, oldest first),
removes each repository (`remove_project` in
`packages/connections/src/projects/store.ts`) and then deletes the
row. A folder that cannot be removed keeps its row and is tried again.
The next run of the workflow, agent or factory that worked in the
project makes a new, empty one.

## Two runs at once

A save records the version it was built on. When another run saved
meanwhile:

- the two versions are joined if they changed different lines
  (`merge_project_versions`);
- if they changed the same lines, the project is left as it was, the
  run's files are kept under `refs/runs/<run id>` in the repository,
  and the run's activity says so.

Nothing in the web app shows or recovers such a kept version yet.

## In the web app

`/projects` lists projects. A row opens a drawer
(`/projects?project=<id>`) with the project's folders, a file shown as
text, a version picker, Download zip and Run it
(`apps/app/src/lib/components/project/`). Reading needs `view:run`. A
row's menu also has Delete project, with a confirm step, for roles
with `delete:run`.

## Seeing a project running

Run it, in the drawer, starts the version being browsed in a sandbox
of its own and shows a link to the running site. The site runs in the
sandbox and is served from the sandbox provider's address, not from
Werker.

See [running what was built](../projects.md#running-what-was-built).

### How a project says how to start

`werker.preview.json` at the project's root, written by the agent that
built the project (`preview_file` in
`packages/domain/src/previews.ts`). It is not in `.werker/`, so it is
saved with the project.

```json
{ "install": "npm install", "start": "node server.js", "port": 4000 }
```

- `start` (required): a command run in `/work` that keeps running. It
  must listen on `127.0.0.1` (`0.0.0.0` covers it); without node in
  the sandbox it must listen on `0.0.0.0` and accept any host name
  (see the proxy below).
- `port` (required): the port it listens on, 1 to 65535.
- `install` (optional): run once in `/work` before `start`.
- `HOST=0.0.0.0` and `PORT=<port>` are set for `start`.
- Every step is told to keep the file right: `preview_note` in
  `packages/runtime/src/project/project.ts`, part of `project_note`.
- A file that cannot be read fails the preview with the reason;
  nothing is guessed then.

Without the file, `plan_preview`
(`packages/runtime/src/preview/plan.ts`) tries these in order and the
drawer says what was assumed:

| The version has                                                            | Install                                       | Start                                                                            | Port                                |
| -------------------------------------------------------------------------- | --------------------------------------------- | -------------------------------------------------------------------------------- | ----------------------------------- |
| `package.json` with a `dev`, `start` or `preview` script                   | `npm install`, when it has dependencies       | `npm run <script>`; Vite, Astro and Next are told the host and port              | the one the script names, else 3000 |
| `manage.py`, or `app.py`, `main.py` or `server.py`                         | `pip install -r requirements.txt`, when there | Django, Streamlit, Uvicorn or Flask by `requirements.txt`, else `python3 <file>` | 8000; Flask 5000; Streamlit 8501    |
| `index.html` at the root, or in `public`, `dist`, `build`, `site` or `www` | none                                          | a static file server in Node, or `python3 -m http.server`                        | 8080                                |

With none of these the preview fails at once, with no sandbox started,
and says what it looked for.

### How it is started

The web app starts nothing. It inserts a row in `project_previews`
(`start_project_preview` in `packages/core/src/project-previews.ts`)
and follows it (`watch_project_preview` in
`apps/app/src/lib/remote/projects.remote.ts`). Every change to a row
notifies the `project_previews` channel, which wakes workers and the
drawer.

A worker (`packages/runtime/src/preview/`):

1. claims the row (`claim_project_preview`), so one worker starts it;
2. reads the version's files from the project store and decides how to
   start it, before any sandbox exists;
3. opens a sandbox from the image the run that saved the version
   worked in, else the default image, and unpacks the version into
   `/work`;
4. runs the install (at most 10 minutes);
5. looks for `node` in the sandbox and asks the adapter for the
   address of the port it will publish (`Sandbox.preview`): the
   proxy's port, 47381 (`proxy_port` in
   `packages/runtime/src/preview/proxy.ts`), when node is there, else
   the project's own port;
6. starts the project, with `__VITE_ADDITIONAL_SERVER_ALLOWED_HOSTS`
   set to the address's host name, and waits up to 180 seconds for
   something to listen on the project's port;
7. with node, starts the proxy and waits up to 20 seconds for it to
   listen (`packages/runtime/src/preview/launch-proxy.ts`);
8. requests the address once, with no cookie or key, to see that it
   answers, and records it. The row is then `running`.

The proxy is a script in plain Node (`http` and `net` only), written
to `/work/.werker/preview-proxy.cjs`, which is never saved to the
project. It listens on `0.0.0.0:47381` and forwards every request,
WebSocket upgrades included, to `127.0.0.1:<port>` in the same
sandbox, with `Host` set to `localhost:<port>` and `Origin`, when
sent, to `http://localhost:<port>`, so dev servers that refuse unknown
host names answer the provider's address. Bodies are streamed. A
redirect to `http://localhost:<port>` is made relative. Without node,
the project's port is published as it is and the drawer's "assumed"
note says a host check may refuse the address (`without_proxy`).

Two previews are started at once by one worker (`starting_at_once`). A
preview does not count towards "Sandboxes at once" in Admin →
Sandboxes.

### Which providers

Adapters with `previews: true` (Docker Cloud Sandboxes, Daytona) can
give a port an address. Docker with gVisor cannot, so there Run it is
disabled and the drawer says why. See [sandboxes](sandboxes.md).

### When it stops

- **By itself:** 30 minutes after it started running
  (`preview_lifetime_ms`). This is a fixed time, not an idle time:
  Werker does not see requests to the provider's address.
- **By a person:** Stop in the drawer.
- **One at a time:** a version has at most one preview waiting,
  starting or running (a unique index). Asking again returns that one.
- **Who ends it:** any worker. Every 15 seconds and on every change,
  workers read the live previews and end the ones that were stopped or
  ran out of time (`preview_end` in `packages/rules/src/previews.ts`):
  the sandbox is deleted, then the row is marked. A running preview
  needs no worker until then.
- **A lost worker:** a worker starting a preview reports every 20
  seconds. After 90 seconds without a report (`preview_stale_ms`) the
  preview is marked failed and its sandbox deleted.
- **The sweep** keeps the sandbox of a preview that has not ended and
  removes the sandbox of one that has ([sandboxes](sandboxes.md)).
- **If every worker stays down** nothing ends a preview.

### Safety

The previewed site is code an agent wrote.

- **Not Werker's origin:** the link is the provider's address. The
  drawer shows a link only for an `http(s)` address on another origin
  than Werker's own (`preview_link`), so the browser sends none of
  Werker's cookies.
- **No keys:** the sandbox is opened with no grant: no connection keys
  and no Git, on every adapter and network setting.
- **The proxy** forwards only to `127.0.0.1` in the preview's own
  sandbox. It changes which host name the project sees, not who can
  reach it: the site is still served only from the provider's address.
- **Network:** the instance's sandbox network setting
  (`instance_settings.sandbox_network`), the same as a run's. A
  preview does not widen what agent-written code can reach, and an
  install needs whatever a run's install needed. With `none` an
  install that downloads packages fails; a site of plain files still
  starts.
- **The address is not private:** whoever has it can open the site
  until the preview stops.
- **Not checked:** whether the provider's address is reachable when
  the sandbox's own network is `none` or an allowlist.

### Time and audit

- A preview's sandbox writes the same `sandbox.created` and
  `sandbox.deleted` audit events as a run's, with the project where a
  run's event has the run (`packages/runtime/src/preview/audit.ts`).
  `read_sandbox_time` counts it.
- It is in no run's cost: per-run sandbox time (`sandbox_usage` in
  `packages/db/src/observability/spend.ts`) reads events that name a
  run. No page shows preview time yet.
- Run it and Stop are audit events of the person:
  `project.preview_started` and `project.preview_stopped`.
