Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .claude/rules/sim-workflow-code.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ and per-kind layout tables of `exit-codes.mdx`, `diagnostics.mdx`, `limits.mdx`
`bun run generate:workflow-code-docs` (`check:workflow-code-docs` fails on drift, a diagnostic code a page names that
no engine emits, a limit or diagnostic code with no docs entry, and a `typescript@x.y.z` pin other than the CLI's).
A limit also needs a label in the generator's `WORKFLOW_LIMIT_LABELS` or `WORKSPACE_LIMIT_LABELS`.
A new exit code also needs a `[#exit-<code>]` section in `troubleshooting.mdx`, which the same check requires
for every non-zero code `sourceExitCodeRows` lists. Behaviour a user relies on (identity, stale, the three-way rule,
promotion) is explained once, in `how-it-works.mdx`; change it there when an engine changes it.

Dependencies point one way: `packages/workflow-types` → `packages/workflow-code` →
`packages/workspace-code` and `packages/workflow-sdk` → the CLI and `apps/sim`. `check:boundaries` keeps both engines and `packages/table-cells` pure (no
Expand Down
3 changes: 3 additions & 0 deletions .cursor/rules/sim-workflow-code.mdc
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ and per-kind layout tables of `exit-codes.mdx`, `diagnostics.mdx`, `limits.mdx`
`bun run generate:workflow-code-docs` (`check:workflow-code-docs` fails on drift, a diagnostic code a page names that
no engine emits, a limit or diagnostic code with no docs entry, and a `typescript@x.y.z` pin other than the CLI's).
A limit also needs a label in the generator's `WORKFLOW_LIMIT_LABELS` or `WORKSPACE_LIMIT_LABELS`.
A new exit code also needs a `[#exit-<code>]` section in `troubleshooting.mdx`, which the same check requires
for every non-zero code `sourceExitCodeRows` lists. Behaviour a user relies on (identity, stale, the three-way rule,
promotion) is explained once, in `how-it-works.mdx`; change it there when an engine changes it.

Dependencies point one way: `packages/workflow-types` → `packages/workflow-code` →
`packages/workspace-code` and `packages/workflow-sdk` → the CLI and `apps/sim`. `check:boundaries` keeps both engines and `packages/table-cells` pure (no
Expand Down
5 changes: 5 additions & 0 deletions apps/docs/content/docs/cli/troubleshooting.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,10 @@ Retain the original request/operation IDs and exact input. An operation may have

A workflow wait timeout also leaves its run active. Inspect that run ID; do not assume another execution is a retry of the same work.

## A `sim workflows source` or `sim workspaces source` command failed

These commands exit with codes of their own, such as `5` when the workflow changed since it was read and `10` when another apply is running, and print each problem with its file, line and code. [Workflows as code troubleshooting](/workflows/code/troubleshooting) covers each exit code and the common diagnostic codes.

## Anything else

For an unexpected stack trace, [open an issue](https://github.com/simstudioai/sim/issues) with the CLI version, command, sanitized trace, and relevant resolved settings. Remove credentials, sensitive inputs, and private file contents before posting. Debug output can contain request selectors.
Expand All @@ -158,3 +162,4 @@ For an unexpected stack trace, [open an issue](https://github.com/simstudioai/si
- [Configuration](/cli/configuration)
- [Output formats](/cli/output)
- [Scripting](/cli/scripting)
- [Workflows as code troubleshooting](/workflows/code/troubleshooting)
1 change: 1 addition & 0 deletions apps/docs/content/docs/cli/workflow-sync.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,7 @@ Validate provider connections in the destination environment as well as the plat
- [Workspace forking](/platform/enterprise/forks)
- [Workflow commands](/cli/workflows)
- [Workspace commands](/cli/workspaces)
- [Promote to another workspace](/workflows/code/promote): copy a workspace directory kept in git to another workspace, without linking the two
- [Selectors](/cli/selectors)
- [Exit codes](/cli/scripting#exit-codes)
- [Workflow-sync contributor harness](/api-reference/workflow-sync#related-docs)
84 changes: 84 additions & 0 deletions apps/docs/content/docs/workflows/code/adopt.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
---
title: Bring existing workflows into code
description: Put workflows built in the workflow builder into git, keep editing them in the builder, and decide who owns each change.
---

Result: workflows you built in the workflow builder kept in a git repository, with edits from the builder and from the files brought together instead of overwritten.

Nothing has to be converted. Every workflow already has a source form: `get` prints it, and the workflow keeps its ID, block IDs, webhook URLs, schedules and run history.

## Choose what to put in git

- **One workflow**: `sim workflows source get <workflowId> --dir <dir>` writes one workflow directory. Use it when a few workflows change through code and the rest stay in the builder.
- **A whole workspace**: `sim workspaces source get --dir <dir>` writes the workflows together with the tables, knowledge bases, files, custom tools, skills, MCP servers, secret names and credential keys they use. Use it when the repository should describe the workspace, so one apply can create what a workflow needs before the workflow.

A workspace directory manages only the kinds whose folder or file it holds: to leave one kind in the builder only, read the rest with `--only`, such as `--only tables,workflows`.

## 1. Read the workflows

```bash
sim workspaces source get --dir ./workspace
```

`get` names everything it could not write, such as a resource over a [limit](/workflows/code/limits), and lists it in `.sim/skipped.json`. Each workflow lands in `workflows/<key>/workflow.ts`, keyed by its name.

## 2. Confirm the files match Sim

```bash
sim workspaces source check --dir ./workspace
sim workspaces source status --dir ./workspace
```

A fresh `get` applies back unchanged, so `check` reports no errors and `status` exits `0` until someone edits either side. A warning on a block you have not changed does not fail the check: it describes a value the workflow already stores, such as a required field left empty in the builder. Fix it in the file when you next change that block.

## 3. Commit the directory

Commit everything `get` wrote, `.sim/` included. The `.sim/` files are how a later apply recognizes each block and resource and knows which version you read. [Directory layout](/workflows/code/directory) lists what to commit for each kind of directory.

## 4. Change it from the files

Make the first change through the loop in [Edit a workflow](/workflows/code/edit) or [Manage a workspace as code](/workflows/code/workspace), then commit the files the apply rewrote. To review changes on pull requests and apply them on merge, see [Run in CI](/workflows/code/ci).

## Keep editing in the workflow builder

The builder stays fully editable. Its **Code** tab shows the same workflow as read-only source. The files and the builder never overwrite each other silently:

- A builder edit since the `get` is **drift**: `status` exits `12`. When the directory holds no unapplied edits, its `Next:` line is a `get` into the same directory, which brings the files up to date. Commit them.
- When the files and the builder changed the same workflow, it is a **conflict**: an apply is refused with exit `5` and writes nothing; see [Exit 5](/workflows/code/troubleshooting#exit-5). There is no automatic merge.
- In a workspace directory, drift on a resource you did not edit is kept: the apply writes your changes and leaves the other resource as Sim holds it (`W_SERVER_CHANGED`).

To find drift before an apply does, run `status` on a schedule; see [Detect drift on a schedule](/workflows/code/ci#detect-drift-on-a-schedule).

## What stays in the builder

Block positions, values in fields the current settings hide, and your comments stay in the stored workflow, and an apply keeps them; [How it works](/workflows/code/how-it-works#the-canonical-form) has the rules. An apply saves the draft only: deploy with `apply --deploy` or `sim workflows deploy`, or, in a workspace directory, with a file in `deployments/`.

## Agree on who changes what

Decide per workflow where its changes are made, and write it down for the team:

- **Files first**: changes go through pull requests, and the builder is for viewing and quick fixes that someone brings back with a `get`. Run the scheduled drift check.
- **Builder first**: the repository is a record. Refresh it with a `get` on a schedule or after a change, and open a pull request with what changed.

Locked blocks cannot be changed from the files, by anyone. Lock a block in the builder to keep it out of reach of an apply.

## Roll back

- **The deployed version**: `sim workflows rollback <workflowId>` (workspace Admin) makes the previous deployment live again and leaves the draft and the directory as they are.
- **The draft, from a deployment**: `sim workflows revert create <workflowId> <version> --yes` replaces the draft with that deployed version. The directory then has drift (`status` exits `12`); `get` brings it up to date.
- **The draft, from git**: restore the source files of an earlier commit but keep the current `.sim/`, then apply:

```bash
git restore --source=<commit> -- ./support-triage ':(exclude,glob)support-triage/**/.sim/**'
sim workflows source apply --dir ./support-triage --dry-run
```

Applying an earlier commit as a whole, `.sim/` included, is refused as stale (exit `5`), because its `.sim/base.json` records an older version than Sim holds. A block or resource deleted since that commit comes back as a new one, without its run history, webhook URLs or schedules, and blocks added since are deletes the apply refuses without `--allow-delete`.

## Related docs

- [Edit a workflow](/workflows/code/edit)
- [Manage a workspace as code](/workflows/code/workspace)
- [Run in CI](/workflows/code/ci)
- [How it works](/workflows/code/how-it-works)
- [Troubleshooting](/workflows/code/troubleshooting)
3 changes: 1 addition & 2 deletions apps/docs/content/docs/workflows/code/exit-codes.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -70,12 +70,11 @@ The table for the kind of directory you work in. Both are built from the table `

{/* workflow-code-docs:exit-codes:end */}

Earlier CLI releases exited `1` when an apply held changes back, whether or not it wrote the rest, and its dry run exited `1` in either case. A script that reads `1` as "held back" should test for `15` too.

Exit `9` follows the CLI's retries: it resends a request the API answered `429` only when sending it twice is safe, which includes every `GET`, a source `apply` and a `create`. See [Scripting](/cli/scripting#exit-codes) for the retry schedule and the codes every command shares (`13` and `9`).

## Related docs

- [Troubleshooting](/workflows/code/troubleshooting): the causes behind each code
- [Run in CI](/workflows/code/ci)
- [Diagnostic codes](/workflows/code/diagnostics)
- [Limits](/workflows/code/limits)
Expand Down
102 changes: 102 additions & 0 deletions apps/docs/content/docs/workflows/code/how-it-works.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
---
title: How it works
description: How Sim turns a file into a saved workflow, keeps each block's identity, and refuses an apply that would overwrite someone else's change.
---

A source file is a declaration, not a program. Sim reads it, compares it with the stored workflow, and saves the difference. This page explains the rules behind `get`, `check` and `apply`, so their results and refusals are predictable.

## Nothing in the file runs

`workflow.ts` is TypeScript so that your editor can complete and type-check it, but no command executes it. Sim parses the file and reads each `const x = builder('Name', {…})` as one block, each `.to()` as one connection, and each param as one setting. That is why the file holds declarations only: a helper function, a loop that creates blocks, or an import from anything but `@sim/workflow-sdk` has no value Sim could read without running it, so it is refused with `E_UNSUPPORTED_SYNTAX` and a hint.

`check` runs this compiler on your machine, the same one the server runs, against the block catalog the last `get` cached. Function code under `functions/` is not run either: it is stored as the Function block's code and runs in Sim, like code typed into the workflow builder.

## From a file to a saved draft

An apply goes through three steps, and a dry run stops after the second:

1. **Compile.** The file becomes the workflow's canonical form: its blocks keyed by name, their settings, connections, branches, loops and variables, with no IDs and no layout.
2. **Evaluate.** Sim reads the stored draft into the same form, matches each compiled block to a stored one, and checks what a save would change: unknown settings, missing required fields, references, permissions, locked blocks, deletes and the workflow's lint. Every finding maps back to a line in your files.
3. **Commit.** Sim writes the new draft in one transaction, after checking again that nobody saved the workflow meanwhile.

Deploying is never part of an apply unless you pass `--deploy` (or, for a workspace directory, add a file under `deployments/`).

## The canonical form

The stored workflow and the file are two views of one canonical form, so a round trip changes nothing:

- `get` prints the canonical form. An `apply` of an unchanged directory is a no-op, and the stored workflow stays byte for byte the same.
- A setting equal to the value a block gets when you add it in the workflow builder is left out of the file. Writing it out has the same effect as leaving it out.
- After a save, Sim prints the files again from what it stored. A value the server normalized, a resource key it allocated, or a reference it rewrote for a rename shows up in your files, so read them again before the next edit and commit what changed.

Some state has no syntax in the file. Sim keeps it from the stored workflow on every apply:

- **Layout.** Block positions stay in Sim. A new block is placed automatically. Moving blocks never makes a directory stale.
- **Settings that do not run.** A value in a field that the block's current choices hide stays stored, so switching back in the workflow builder brings it back.
- **Comments.** Comments in `workflow.ts` are stored with the workflow, by block, so a builder edit or a rename keeps them.

## Block identity

Each stored block has an ID that its webhook URL, schedules and run history hang on. The file names blocks by `const` name and display name instead, so Sim needs a way to tell an edited block from a new one.

`.sim/lock.json` records, for each block in the file, the ID of the stored block it is. An apply matches each block in the file, in this order:

1. by its block key in the lock, the display name lowercased with whitespace and dots removed;
2. by its `const` name in the lock, so changing only the display name keeps the block;
3. by its block key in the stored workflow, when the lock does not have it;
4. by inference: when a block left and one of the same type appeared in the same place with the same connections or settings, it is a rename (`W_MAT_INFERRED_RENAME`).

A block that matches nothing is new, and a stored block nothing matched is a delete, which an apply refuses without `--allow-delete` (`E_APPLY_BLOCK_DELETE`). Renaming two blocks of the same type in the same place in one save leaves the inference nothing to tell apart, so rename them one save at a time.

Sim also stores each block's `const` name, its order in the file and its comments, by block ID. A `get` into a new directory, or after a builder edit, prints the same names in the same order, so a one-setting change stays a one-line diff.

## Stale: what you read is not what Sim holds

`.sim/base.json` records a fingerprint of the draft you read: a hash of its canonical form, its variables and its block IDs, without layout. An apply sends it, and Sim computes the fingerprint of the draft it holds now:

- the same: the apply goes ahead;
- different: someone saved the workflow since you read it. The apply is refused as stale with exit `5`, on a dry run too, and nothing is written; [Troubleshooting](/workflows/code/troubleshooting#exit-5) has the steps.

The check is made again inside the write's transaction, so two applies that race cannot both win. `status` asks the same question with a dry run and reports it as drift (exit `12`) before you apply.

The base also records the block catalog you read against. When a compile fails and the catalog changed since, for example after a block gained a setting, the apply adds `W_APPLY_CATALOG_MOVED`: run `get` again to pick up the new types.

## Workspaces: one rule per resource

A workspace directory adds tables, knowledge bases, files, custom tools, skills, MCP servers, secret names, credential keys and deployments. Each resource has a key, its file or folder name, which Sim stores and never changes: a rename in Sim keeps the key, and a workflow names the resource by it, as in `table('orders')`.

`.sim/base.json` at the root records each resource's ID and fingerprint as the last `get` or apply saw it. An apply decides each declared resource by comparing three fingerprints: your file (local), the base, and what Sim holds now (server):

| Your file vs the base | Sim vs the base | Outcome |
| --- | --- | --- |
| Same | Any | Nothing to do. When Sim changed it, the apply keeps Sim's version (`W_SERVER_CHANGED`) |
| Changed | Same | Written |
| Changed | Changed | Nothing to do when both made the same change; otherwise stale, exit `5` |
Comment thread
waleedlatif1 marked this conversation as resolved.
Resolved

A resource with no base entry is created, or **adopted** when Sim already holds one under the same name. An adopt only adds to what is there, such as a column or a document; one that would change or remove anything is refused with `E_APPLY_NO_BASE`. A resource the directory stops declaring is deleted only when the base recorded it and you pass `--allow-delete`; anything else Sim holds is kept and named in a `W_UNDECLARED` warning.

Workflows are the exception to this table. Each workflow folder is applied exactly as a single workflow, with its own `.sim/lock.json` and `.sim/base.json`: a folder you did not edit is skipped without asking whether Sim changed it, so it gets no `W_SERVER_CHANGED`, and an edited folder is refused as stale whenever Sim changed the workflow since its base, even when both made the same change.

An apply writes resources in dependency order, so a table exists before the workflow that writes to it, and deletes run in reverse.

### Applying a workspace

- **Whole or nothing at planning.** Every check runs before the first write. One error refuses the whole apply.
- **One apply at a time.** An apply holds the workspace from planning to its last write. A second apply meanwhile exits `10` with nothing written. A dry run does not take part, so it never waits.
- **Writes commit one by one.** If a write fails part-way, the apply stops, saves what it wrote into the directory and exits `8`. Applying the same directory again finishes the rest.
- **Held back.** A change that uses a secret with no value in the workspace, or a credential key nothing binds to, is held back while the rest is written, and the apply exits `15`.
- **Files travel separately.** Workspace files and knowledge base documents are uploaded before the apply, and only the ones Sim does not have yet, so a retry uploads nothing again.

## Promotion

`apply --to <workspaceId>` sends a directory read from one workspace to another. Sim sees an ordinary apply against the target's own IDs: each resource binds by key, so a workflow keeps its ID and webhook URLs in the target, and secrets and credentials bind by name to the target's own.

The directory stays bound to the workspace it was read from. The only file a promotion writes is `.sim/targets/<workspaceId>.json`, a record of what it left in the target. The next promotion uses that record as the base for the three-way rule above, so a hotfix made in the target is kept when the directory left that resource alone, and refused as stale when the directory changed it too. See [Promote to another workspace](/workflows/code/promote).

## Related docs

- [Edit a workflow](/workflows/code/edit)
- [Manage a workspace as code](/workflows/code/workspace)
- [Directory layout](/workflows/code/directory)
- [Troubleshooting](/workflows/code/troubleshooting)
- [Diagnostic codes](/workflows/code/diagnostics)
Loading
Loading