Skip to content

Commit 3257c89

Browse files
authored
improvement(workflow-code): adopt, troubleshooting and how-it-works docs, slim sdk readme (#10343)
1 parent 3b3c0b5 commit 3257c89

16 files changed

Lines changed: 418 additions & 202 deletions

File tree

‎.claude/rules/sim-workflow-code.md‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,9 @@ and per-kind layout tables of `exit-codes.mdx`, `diagnostics.mdx`, `limits.mdx`
5555
`bun run generate:workflow-code-docs` (`check:workflow-code-docs` fails on drift, a diagnostic code a page names that
5656
no engine emits, a limit or diagnostic code with no docs entry, and a `typescript@x.y.z` pin other than the CLI's).
5757
A limit also needs a label in the generator's `WORKFLOW_LIMIT_LABELS` or `WORKSPACE_LIMIT_LABELS`.
58+
A new exit code also needs a `[#exit-<code>]` section in `troubleshooting.mdx`, which the same check requires
59+
for every non-zero code `sourceExitCodeRows` lists. Behaviour a user relies on (identity, stale, the three-way rule,
60+
promotion) is explained once, in `how-it-works.mdx`; change it there when an engine changes it.
5861

5962
Dependencies point one way: `packages/workflow-types` → `packages/workflow-code` →
6063
`packages/workspace-code` and `packages/workflow-sdk` → the CLI and `apps/sim`. `check:boundaries` keeps both engines and `packages/table-cells` pure (no

‎.cursor/rules/sim-workflow-code.mdc‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@ and per-kind layout tables of `exit-codes.mdx`, `diagnostics.mdx`, `limits.mdx`
3535
`bun run generate:workflow-code-docs` (`check:workflow-code-docs` fails on drift, a diagnostic code a page names that
3636
no engine emits, a limit or diagnostic code with no docs entry, and a `typescript@x.y.z` pin other than the CLI's).
3737
A limit also needs a label in the generator's `WORKFLOW_LIMIT_LABELS` or `WORKSPACE_LIMIT_LABELS`.
38+
A new exit code also needs a `[#exit-<code>]` section in `troubleshooting.mdx`, which the same check requires
39+
for every non-zero code `sourceExitCodeRows` lists. Behaviour a user relies on (identity, stale, the three-way rule,
40+
promotion) is explained once, in `how-it-works.mdx`; change it there when an engine changes it.
3841

3942
Dependencies point one way: `packages/workflow-types` → `packages/workflow-code` →
4043
`packages/workspace-code` and `packages/workflow-sdk` → the CLI and `apps/sim`. `check:boundaries` keeps both engines and `packages/table-cells` pure (no

‎apps/docs/content/docs/cli/troubleshooting.mdx‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,10 @@ Retain the original request/operation IDs and exact input. An operation may have
148148

149149
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.
150150

151+
## A `sim workflows source` or `sim workspaces source` command failed
152+
153+
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.
154+
151155
## Anything else
152156

153157
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.
@@ -158,3 +162,4 @@ For an unexpected stack trace, [open an issue](https://github.com/simstudioai/si
158162
- [Configuration](/cli/configuration)
159163
- [Output formats](/cli/output)
160164
- [Scripting](/cli/scripting)
165+
- [Workflows as code troubleshooting](/workflows/code/troubleshooting)

‎apps/docs/content/docs/cli/workflow-sync.mdx‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,7 @@ Validate provider connections in the destination environment as well as the plat
239239
- [Workspace forking](/platform/enterprise/forks)
240240
- [Workflow commands](/cli/workflows)
241241
- [Workspace commands](/cli/workspaces)
242+
- [Promote to another workspace](/workflows/code/promote): copy a workspace directory kept in git to another workspace, without linking the two
242243
- [Selectors](/cli/selectors)
243244
- [Exit codes](/cli/scripting#exit-codes)
244245
- [Workflow-sync contributor harness](/api-reference/workflow-sync#related-docs)
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
---
2+
title: Bring existing workflows into code
3+
description: Put workflows built in the workflow builder into git, keep editing them in the builder, and decide who owns each change.
4+
---
5+
6+
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.
7+
8+
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.
9+
10+
## Choose what to put in git
11+
12+
- **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.
13+
- **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.
14+
15+
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`.
16+
17+
## 1. Read the workflows
18+
19+
```bash
20+
sim workspaces source get --dir ./workspace
21+
```
22+
23+
`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.
24+
25+
## 2. Confirm the files match Sim
26+
27+
```bash
28+
sim workspaces source check --dir ./workspace
29+
sim workspaces source status --dir ./workspace
30+
```
31+
32+
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.
33+
34+
## 3. Commit the directory
35+
36+
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.
37+
38+
## 4. Change it from the files
39+
40+
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).
41+
42+
## Keep editing in the workflow builder
43+
44+
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:
45+
46+
- 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.
47+
- 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.
48+
- 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`).
49+
50+
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).
51+
52+
## What stays in the builder
53+
54+
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/`.
55+
56+
## Agree on who changes what
57+
58+
Decide per workflow where its changes are made, and write it down for the team:
59+
60+
- **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.
61+
- **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.
62+
63+
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.
64+
65+
## Roll back
66+
67+
- **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.
68+
- **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.
69+
- **The draft, from git**: restore the source files of an earlier commit but keep the current `.sim/`, then apply:
70+
71+
```bash
72+
git restore --source=<commit> -- ./support-triage ':(exclude,glob)support-triage/**/.sim/**'
73+
sim workflows source apply --dir ./support-triage --dry-run
74+
```
75+
76+
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`.
77+
78+
## Related docs
79+
80+
- [Edit a workflow](/workflows/code/edit)
81+
- [Manage a workspace as code](/workflows/code/workspace)
82+
- [Run in CI](/workflows/code/ci)
83+
- [How it works](/workflows/code/how-it-works)
84+
- [Troubleshooting](/workflows/code/troubleshooting)

‎apps/docs/content/docs/workflows/code/exit-codes.mdx‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,12 +70,11 @@ The table for the kind of directory you work in. Both are built from the table `
7070

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

73-
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.
74-
7573
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`).
7674

7775
## Related docs
7876

77+
- [Troubleshooting](/workflows/code/troubleshooting): the causes behind each code
7978
- [Run in CI](/workflows/code/ci)
8079
- [Diagnostic codes](/workflows/code/diagnostics)
8180
- [Limits](/workflows/code/limits)
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
---
2+
title: How it works
3+
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.
4+
---
5+
6+
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.
7+
8+
## Nothing in the file runs
9+
10+
`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.
11+
12+
`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.
13+
14+
## From a file to a saved draft
15+
16+
An apply goes through three steps, and a dry run stops after the second:
17+
18+
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.
19+
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.
20+
3. **Commit.** Sim writes the new draft in one transaction, after checking again that nobody saved the workflow meanwhile.
21+
22+
Deploying is never part of an apply unless you pass `--deploy` (or, for a workspace directory, add a file under `deployments/`).
23+
24+
## The canonical form
25+
26+
The stored workflow and the file are two views of one canonical form, so a round trip changes nothing:
27+
28+
- `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.
29+
- 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.
30+
- 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.
31+
32+
Some state has no syntax in the file. Sim keeps it from the stored workflow on every apply:
33+
34+
- **Layout.** Block positions stay in Sim. A new block is placed automatically. Moving blocks never makes a directory stale.
35+
- **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.
36+
- **Comments.** Comments in `workflow.ts` are stored with the workflow, by block, so a builder edit or a rename keeps them.
37+
38+
## Block identity
39+
40+
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.
41+
42+
`.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:
43+
44+
1. by its block key in the lock, the display name lowercased with whitespace and dots removed;
45+
2. by its `const` name in the lock, so changing only the display name keeps the block;
46+
3. by its block key in the stored workflow, when the lock does not have it;
47+
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`).
48+
49+
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.
50+
51+
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.
52+
53+
## Stale: what you read is not what Sim holds
54+
55+
`.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:
56+
57+
- the same: the apply goes ahead;
58+
- 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.
59+
60+
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.
61+
62+
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.
63+
64+
## Workspaces: one rule per resource
65+
66+
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')`.
67+
68+
`.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):
69+
70+
| Your file vs the base | Sim vs the base | Outcome |
71+
| --- | --- | --- |
72+
| Same | Any | Nothing to do. When Sim changed it, the apply keeps Sim's version (`W_SERVER_CHANGED`) |
73+
| Changed | Same | Written |
74+
| Changed | Changed | Nothing to do when both made the same change; otherwise stale, exit `5` |
75+
76+
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.
77+
78+
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.
79+
80+
An apply writes resources in dependency order, so a table exists before the workflow that writes to it, and deletes run in reverse.
81+
82+
### Applying a workspace
83+
84+
- **Whole or nothing at planning.** Every check runs before the first write. One error refuses the whole apply.
85+
- **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.
86+
- **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.
87+
- **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`.
88+
- **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.
89+
90+
## Promotion
91+
92+
`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.
93+
94+
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).
95+
96+
## Related docs
97+
98+
- [Edit a workflow](/workflows/code/edit)
99+
- [Manage a workspace as code](/workflows/code/workspace)
100+
- [Directory layout](/workflows/code/directory)
101+
- [Troubleshooting](/workflows/code/troubleshooting)
102+
- [Diagnostic codes](/workflows/code/diagnostics)

0 commit comments

Comments
 (0)