# Versions of agents, workflows and factories

> How saved changes to agents, workflows, factories and saved teams are kept and restored.

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

Each time a saved agent, workflow, factory or saved team (starter)
changes, Werker keeps a full copy of it as a numbered version, with
who changed it, when and why. A version can be read, compared and
restored.

The [audit log](observability.md#audit-log) records that an edit
happened. A version holds what the definition was. An improvement loop
that proposes changes is planned
([workflow options](agents-workflows-factories.md#workflow-options),
[#7](https://github.com/werker-ai/werker/issues/7)).

## What a version holds

One row of `definition_versions` (`packages/db/sql/001_schema.sql`):
the workspace, the kind and ID of the definition, a version number
that counts up from 1 for that definition, the snapshot as JSON,
`source` (`user`, `system` or `agent`, as in the audit log), the
user's ID and email as it was then (`actor_user_id`, `actor_label`),
an optional reason, `run_id` (the run that made or motivated the
change) and the time. The app role can add and read rows, never change
or delete them; row-level security keeps a workspace to its own. There
is no foreign key to the definition, so versions outlive it.

The snapshot types are in `packages/domain/src/versions.ts`:

| Kind              | Snapshot                                                                                                                                                                          |
| ----------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `agent`           | Name, instructions, task, and for a factory member its factory, role, team, responsibilities, model and allowed tools.                                                            |
| `workflow`        | Name, description and the ordered steps: name, agent ID, instructions, and `for_each` and `decide` when set.                                                                      |
| `factory`         | Name, description, room settings (conversation prompt, turn limit, `listen`), teams and each member: agent ID, name, instructions, role, team, responsibilities, model and tools. |
| `factory_starter` | Name, description, room settings, teams and each member: role, team, name, instructions, responsibilities, model and tools.                                                       |

Not in a snapshot: an agent's equipment, schedules, the plan a
definition was generated from, and the starter a factory came from.

## When a version is recorded

In the write functions of `packages/db`, in the same transaction as
the write (`record_version` in `packages/db/src/versions/record.ts`),
so the web app, the generator and any later caller are covered:

- Creation is version 1. A write that leaves the definition as its
  latest version has it records nothing.
- A change to a factory member records a version of the member's agent
  and of the factory, as each snapshot holds the member. Creating a
  factory records version 1 of the factory and of each member's agent.
- A definition with no version gets its state before the change as
  version 1 (`record_baseline`), then the change as version 2.
- Each write function takes an optional author (`ChangeAuthor`). The
  web app passes the signed-in user
  (`apps/app/src/lib/server/author.ts`). The generator's saves are
  recorded as source `agent`, label `generator`, with the run in the
  reason. A caller that passes none is recorded as Werker (`system`).

## History and restore

- **Where:** a History button opens a sheet
  (`apps/app/src/lib/components/history/`).
- **What it shows:** versions newest first, each with who, when, the
  reason if any, and what it changed from the version before. The
  comparison is `diff_versions` in
  `packages/rules/src/versions/diff.ts`, made on the server by
  `list_history` (`packages/core/src/versions.ts`). The newest 100
  versions are shown.
- **Workflow steps** are matched by name, then by place, so a step
  added at the start does not mark every later step as changed. A step
  both renamed and moved shows as one removed and one added.
- **Restore** asks for confirmation, then sets the definition back to
  that version and records the result as a new version with the reason
  "Restored version N" (`restore_version` in
  `packages/core/src/versions.ts`). Nothing in the history is removed.
  It needs the permission an edit needs (`update:agent` or
  `update:workflow`; factories and starters follow agents, as their
  other actions do) and is written to the audit log with
  `restored_version` (`apps/app/src/lib/remote/history.remote.ts`).
- **The rules of an edit apply.** A restore is refused when a member's
  model is no longer offered, when a factory member would change while
  its team has turns queued, running or waiting, and when a workflow
  step's agent no longer exists. Restoring an agent's role or task
  sets its equipment aside, as an edit does.
- **A factory restore** sets back its name, description, room
  settings, teams, and each member's role, team, texts and settings,
  through the board's writer (`save_team`). Members added since are
  removed; a member removed since comes back as a new member with a
  new agent, as an agent that left a factory cannot rejoin it.

## Limits

- A version is not linked to its audit event. The web app writes the
  audit event after the change, in its own transaction; the two are
  matched by definition and time.
- A definition written by SQL outside these functions has no history
  until its next change.
- Nothing prunes versions, and a deleted definition's versions stay.
- Runs do not record which version of a definition they ran. A run
  keeps its own snapshot of the steps in `runs.workflow`.
- The web app edit forms take no reason; only restores and the
  generator set one.
