Skip to content

Commit 02fb744

Browse files
authored
fix(workflow-code): correctness, safety and speed for workflows and workspace as code (#10237)
Wave 1 from the staging evaluation of workflows/workspace as code, audited by 5 lenses and re-audited. Deploy/drift safety (no out-of-band draft publishes; expected-draft fingerprints on deploy and chat writes; drift warnings by default), adopt/promote/partial-apply safety, one shared refusal decision for server and offline check, compiler and CLI correctness, feature gate after authorization, read-cost plan routes, realtime flush lost-update fixes, deterministic edge/block load order, and diff-based normalized saves with restored workflow_edges block FK indexes (migration 0467).
1 parent 4a0f2e4 commit 02fb744

197 files changed

Lines changed: 48633 additions & 2118 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

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

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,9 +73,13 @@ and "The catalog".
7373

7474
One global flag, `workflow-code` (`lib/core/config/feature-flags.ts`, env fallback
7575
`WORKFLOW_CODE`), gates workflow source, workspace source, the source catalog and resources, and
76-
the Code tab. Every use case calls `requireWorkflowCodeEnabled()`
77-
(`lib/workflows/code/feature-flag.ts`); the UI reads it from the workspace layout's flags. A new
78-
surface gates through the same function, never its own check.
76+
the Code tab. Every source operation (the workflow source ones in
77+
`lib/workflows/application/operations.ts` and the workspace source ones in
78+
`lib/workspace-source/application/operations.ts`) declares `feature: WORKFLOW_CODE_FEATURE`, which
79+
the shared use-case wrapper checks after authorization (`FEATURE_NOT_ENABLED`); a new source
80+
operation declares the same feature, never its own check. A surface outside an operation (the Code
81+
tab, Copilot entitlements) calls `isWorkflowCodeEnabled()` (`lib/workflows/code/feature-flag.ts`);
82+
the UI reads it from the workspace layout's flags.
7983

8084
## Adding things
8185

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

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,13 @@ and "The catalog".
6363

6464
One global flag, `workflow-code` (`lib/core/config/feature-flags.ts`, env fallback
6565
`WORKFLOW_CODE`), gates workflow source, workspace source, the source catalog and resources, and
66-
the Code tab. Every use case calls `requireWorkflowCodeEnabled()`
67-
(`lib/workflows/code/feature-flag.ts`); the UI reads it from the workspace layout's flags. A new
68-
surface gates through the same function, never its own check.
66+
the Code tab. Every source operation (the workflow source ones in
67+
`lib/workflows/application/operations.ts` and the workspace source ones in
68+
`lib/workspace-source/application/operations.ts`) declares `feature: WORKFLOW_CODE_FEATURE`, which
69+
the shared use-case wrapper checks after authorization (`FEATURE_NOT_ENABLED`); a new source
70+
operation declares the same feature, never its own check. A surface outside an operation (the Code
71+
tab, Copilot entitlements) calls `isWorkflowCodeEnabled()` (`lib/workflows/code/feature-flag.ts`);
72+
the UI reads it from the workspace layout's flags.
6973

7074
## Adding things
7175

‎apps/docs/content/docs/api-reference/(generated)/workflows/meta.json‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,12 @@
1212
"getWorkflowOutline",
1313
"getWorkflowSource",
1414
"applyWorkflowSource",
15+
"planWorkflowSource",
1516
"createWorkflowFromSource",
1617
"getSourceResources",
1718
"getWorkspaceSource",
1819
"applyWorkspaceSource",
20+
"planWorkspaceSource",
1921
"findMissingWorkspaceSourceBlobs",
2022
"stageWorkspaceSourceBlobs",
2123
"replaceWorkflowState",

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

Lines changed: 13 additions & 12 deletions
Large diffs are not rendered by default.

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

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -396,6 +396,7 @@ sim workflows deploy <workflowId> [options]
396396
| --- | --- | --- |
397397
| `--name <value>` | No | Optional label for the deployment version. |
398398
| `--description <value>` | No | Optional release note for the deployment version. |
399+
| `--expected-draft-fingerprint <value>` | No | Deploy only this draft: the `sf1:` fingerprint a source apply answered with (`base.fingerprint`). When the draft changed since, the deploy answers `409` and publishes nothing. |
399400
| `-y, --yes` | No | Accepted and ignored: this command needs no confirmation. |
400401

401402
</CommandTable>
@@ -1316,7 +1317,7 @@ sim workflows source get <workflowId> [options]
13161317

13171318
</CommandTable>
13181319

1319-
Exit codes with --dir: 0 written; 1 the command failed, or the directory holds another workflow's source (SOURCE_WORKFLOW_MISMATCH) or edits no apply has saved (SOURCE_LOCAL_EDITS: apply them, get into another directory, or pass --force); 7 the workflow is too large to print as source; 9 rate limited (HTTP 429) after the CLI's retries: wait a minute and run it again. Exit 10 (busy, retry): --dir is a workflow folder of a workspace directory another sim command is using (SOURCE_DIR_BUSY); nothing was read or written. Run this one again once it finished.
1320+
Exit codes with --dir: 0 written; 1 the command failed, the directory is a workspace source directory (SOURCE_DIR_KIND_MISMATCH, even with --force), or it holds another workflow's source (SOURCE_WORKFLOW_MISMATCH) or edits no apply has saved (SOURCE_LOCAL_EDITS: apply them, get into another directory, or pass --force); 7 the workflow is too large to print as source; 9 rate limited (HTTP 429) after the CLI's retries: wait a minute and run it again. Exit 10 (busy, retry): --dir is a workflow folder of a workspace directory another sim command is using (SOURCE_DIR_BUSY); nothing was read or written. Run this one again once it finished.
13201321

13211322
API request and response schemas: [getWorkflowSource](/api-reference/workflows/getWorkflowSource).
13221323

@@ -1340,7 +1341,7 @@ sim workflows source status [options]
13401341

13411342
Exit codes: 0 clean (nothing to apply, and the server changed nothing since the directory was read); 11 local changes only (applying writes them); 12 the server changed since the directory was read: drift (a resource edited, added or deleted there that the directory leaves alone) or a conflict (also changed here, so applying it is refused as stale), with or without local changes; 7 the source is too large for the server to read in one dry run; 1 the command failed; 13 not logged in (AUTH_REQUIRED) or the credential was refused: the status shows the local changes only, then run `sim login`. A server that cannot be reached is not a failure: the status shows the local changes only and says so (exit 0 or 11).
13421343

1343-
API request and response schemas: [applyWorkflowSource](/api-reference/workflows/applyWorkflowSource).
1344+
API request and response schemas: [planWorkflowSource](/api-reference/workflows/planWorkflowSource), [applyWorkflowSource](/api-reference/workflows/applyWorkflowSource), [getSourceResources](/api-reference/workflows/getSourceResources).
13441345

13451346
### Create a workflow from a workflow source directory
13461347

@@ -1391,17 +1392,17 @@ sim workflows source apply [workflowId] [options]
13911392
| Option | Required | Description |
13921393
| --- | --- | --- |
13931394
| `--verbose` | No | With --output json, also list info findings (such as blocks placed by autolayout). |
1394-
| `--dir <dir>` | Yes | Source directory: workflow.ts, functions/*, .sim/lock.json and .sim/base.json. |
1395+
| `--dir <dir>` | No | Source directory: workflow.ts, functions/*, .sim/lock.json and .sim/base.json. Defaults to `.`. |
13951396
| `--dry-run` | No | Check the source and show the outcome without saving or writing files. |
13961397
| `-y, --yes` | No | Apply without asking for confirmation. |
13971398
| `--deploy` | No | After the write is saved, deploy the workflow (as `sim workflows deploy <workflowId>` does). |
1398-
| `--allow-delete` | No | Delete blocks the source no longer declares; without it, an apply that deletes a block is refused. |
1399+
| `--allow-delete` | No | Delete blocks and variables the source no longer declares; without it, an apply that deletes one is refused. |
13991400

14001401
</CommandTable>
14011402

14021403
Exit codes: 0 applied (or a clean dry run); 1 the source has errors (each printed as file:line:column with its frame and hint) or the command failed; 5 the workflow changed since the directory was read (SOURCE_STALE, on a dry run too: get it again, reapply the edit, retry); 6 the workflow or its folder is locked (E_APPLY_LOCKED; a locked block exits 1, E_APPLY_LOCKED_BLOCK); 7 the source is too large; 9 rate limited (HTTP 429) after the CLI's retries, so nothing was written: wait a minute and run it again; 14 without --yes and with no terminal to ask: the plan was printed with no errors and nothing was written (run the same command with --yes to apply it). Exit 10 (busy, retry): --dir is a workflow folder of a workspace directory another sim command is using (SOURCE_DIR_BUSY); nothing was read or written. Run this one again once it finished. Exit 13: not logged in, or the server refused the credential (AUTH_REQUIRED); nothing was sent. Run `sim login`, or fix or unset SIM_API_KEY when the key comes from it. Exit 130: interrupted (Ctrl-C). A write already sent is finished and saved into the directory first, waiting up to 60s for its answer; a second Ctrl-C, or no answer by then, stops at once and prints that it may have been written and how to check.
14031404

1404-
API request and response schemas: [applyWorkflowSource](/api-reference/workflows/applyWorkflowSource), [deployWorkflow](/api-reference/workflows/deployWorkflow).
1405+
API request and response schemas: [applyWorkflowSource](/api-reference/workflows/applyWorkflowSource), [deployWorkflow](/api-reference/workflows/deployWorkflow), [getSourceResources](/api-reference/workflows/getSourceResources).
14051406

14061407
### Show the block catalog workflow source is checked against
14071408

@@ -1435,7 +1436,7 @@ sim workflows source check [options]
14351436
| --- | --- | --- |
14361437
| `--dir <dir>` | No | Source directory: workflow.ts, functions/* and .sim/. Defaults to `.`. |
14371438
| `--strict` | No | Fail on the warnings listed below in every block, not only new ones. |
1438-
| `--no-strict` | No | Never fail on a warning. |
1439+
| `--no-strict` | No | Never fail on a warning an apply saves. |
14391440
| `--verbose` | No | With --output json, also list info findings (such as blocks placed by autolayout). |
14401441

14411442
</CommandTable>
@@ -1444,7 +1445,7 @@ Compiles workflow.ts (and the function files it names) with the compiler the ser
14441445

14451446
Exit codes: 0 no errors; 1 errors (each printed as file:line:column with its frame and hint), or no cached catalog (run the get once). With --output json, stdout is one document that also lists every finding under diagnostics.
14461447

1447-
These warnings, which an apply saves as written, fail the check as errors on every block the lock does not bind (a block added since the get, or every block of a new workflow): W_REQUIRED_MISSING, W_UNKNOWN_OPTION, W_UNKNOWN_OUTPUT_PATH, W_VALUE_KIND, W_OUT_OF_RANGE, W_LITERAL_SECRET, W_TABLE_UNKNOWN_COLUMN, W_CHILD_INPUT_UNKNOWN, W_BRANCH_UNWIRED, W_UNRESOLVED_REF. --strict fails on them in every block, except W_BRANCH_UNWIRED on a block the lock binds (the stored workflow may hold it, and the server then stays quiet); --no-strict fails on none of them. In a function file changed since the get, W_UNRESOLVED_REF is the error E_APPLY_UNRESOLVED_REF in every mode, as the server refuses it. W_JSON_REF_UNTYPED is reported only on blocks the lock does not bind, and with --strict on every block.
1448+
These warnings, which an apply saves as written, fail the check as errors on every block the lock does not bind (a block added since the get, or every block of a new workflow): W_REQUIRED_MISSING, W_UNKNOWN_OPTION, W_UNKNOWN_OUTPUT_PATH, W_VALUE_KIND, W_OUT_OF_RANGE, W_LITERAL_SECRET, W_TABLE_UNKNOWN_COLUMN, W_CHILD_INPUT_UNKNOWN, W_BRANCH_UNWIRED, W_UNRESOLVED_REF, W_REF_OUT_OF_SCOPE. --strict fails on them in every block, except W_BRANCH_UNWIRED on a block the lock binds (the stored workflow may hold it, and the server then stays quiet); --no-strict fails on none of them. In every mode, as the server refuses them: W_REF_OUT_OF_SCOPE is the error E_APPLY_REF_OUT_OF_SCOPE in any file of a block the edit added or changed; W_UNKNOWN_OUTPUT_PATH is the error E_APPLY_UNRESOLVED_REF in workflow.ts on a block the edit added or changed; W_UNRESOLVED_REF is the error E_APPLY_UNRESOLVED_REF in a function file changed since the get; W_UNKNOWN_OUTPUT_PATH is the error E_APPLY_UNRESOLVED_REF in a function file changed since the get; W_CODE_FILE_REF_SYNTAX is the error E_APPLY_CODE_FILE_REF_SYNTAX in a function file changed since the get. A block counts as changed when the lock does not bind it, when the source moved it into another container, or when it compiles differently than in the source the last get or apply printed (.sim/stored-edges.json records a digest of each block). That last comparison needs the catalog the source was printed with in the cache: without it, a bound block that did not move counts as unchanged, so the server may refuse what the check passes. A $ref in a function file to a block the lock binds that the source no longer declares stays a warning, unless a warning cap left findings out: the server may infer that block was renamed and rewrite the reference, and refuses it otherwise. W_JSON_REF_UNTYPED is reported only on blocks the lock does not bind, and with --strict on every block.
14481449

14491450
### Show the content of the changes in a workflow source directory
14501451

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -708,7 +708,7 @@ sim workspaces source get [options]
708708

709709
A directory that already holds a .sim/base.json is refreshed from the workspace it was read from, whatever the profile's default workspace.
710710

711-
Exit codes: 0 written; 1 the command failed, the directory holds edits no apply has saved (SOURCE_LOCAL_EDITS: apply them, get into another directory, or pass --force), --workspace names another workspace than the one the directory was read from (SOURCE_OTHER_WORKSPACE), or a downloaded binary file does not match its listing (SOURCE_BLOB_MISMATCH: nothing was written); 7 the workspace has more resources of a kind than one directory holds; 9 rate limited (HTTP 429) after the CLI's retries: nothing was written, the binary files already downloaded are kept in .sim/downloads, and running the get again downloads only the rest.
711+
Exit codes: 0 written; 1 the command failed, the directory holds edits no apply has saved (SOURCE_LOCAL_EDITS: apply them, get into another directory, or pass --force), --workspace names another workspace than the one the directory was read from (SOURCE_OTHER_WORKSPACE), the directory is a workflow source directory (SOURCE_DIR_KIND_MISMATCH, even with --force), or a downloaded binary file does not match its listing (SOURCE_BLOB_MISMATCH: nothing was written); 7 the workspace has more resources of a kind than one directory holds; 9 rate limited (HTTP 429) after the CLI's retries: nothing was written, the binary files already downloaded are kept in .sim/downloads, and running the get again downloads only the rest.
712712

713713
Exit 10 (busy, retry): another sim workspaces source command is using this directory (SOURCE_DIR_BUSY); nothing was read or written. Run this one again once it finished.
714714

@@ -737,7 +737,7 @@ Exit codes: 0 clean (nothing to apply, and the server changed nothing since the
737737

738738
Exit 10 (busy, retry): another sim workspaces source command is using this directory (SOURCE_DIR_BUSY); nothing was read or written. Run this one again once it finished.
739739

740-
API request and response schemas: [applyWorkspaceSource](/api-reference/workflows/applyWorkspaceSource).
740+
API request and response schemas: [planWorkspaceSource](/api-reference/workflows/planWorkspaceSource), [applyWorkspaceSource](/api-reference/workflows/applyWorkspaceSource).
741741

742742
### Apply a workspace source directory
743743

@@ -752,7 +752,7 @@ sim workspaces source apply [options]
752752
| Option | Required | Description |
753753
| --- | --- | --- |
754754
| `--verbose` | No | With --output json, also list info findings (such as blocks placed by autolayout). |
755-
| `--dir <dir>` | Yes | Workspace source directory written by `workspaces source get`. |
755+
| `--dir <dir>` | No | Workspace source directory written by `workspaces source get`. Defaults to `.`. |
756756
| `--dry-run` | No | Plan and check without writing anything. |
757757
| `-y, --yes` | No | Apply without asking for confirmation. |
758758
| `--allow-delete` | No | Delete resources, columns and data the directory no longer declares. |
@@ -779,7 +779,7 @@ sim workspaces source check [options]
779779
| --- | --- | --- |
780780
| `--dir <dir>` | No | Workspace source directory: the kind folders and .sim/base.json. Defaults to `.`. |
781781
| `--strict` | No | Fail on the warnings listed below in every block, not only new ones. |
782-
| `--no-strict` | No | Never fail on a warning. |
782+
| `--no-strict` | No | Never fail on a warning an apply saves. |
783783
| `--verbose` | No | With --output json, also list info findings (such as blocks placed by autolayout). |
784784

785785
</CommandTable>
@@ -788,7 +788,7 @@ Compiles every workflows/&lt;key&gt;/workflow.ts (and the function files each na
788788

789789
Exit codes: 0 no errors; 1 errors (each printed as file:line:column with its frame and hint), or no cached catalog (run the get once). With --output json, stdout is one document that also lists every finding under diagnostics.
790790

791-
These warnings, which an apply saves as written, fail the check as errors on every block the lock does not bind (a block added since the get, or every block of a new workflow): W_REQUIRED_MISSING, W_UNKNOWN_OPTION, W_UNKNOWN_OUTPUT_PATH, W_VALUE_KIND, W_OUT_OF_RANGE, W_LITERAL_SECRET, W_TABLE_UNKNOWN_COLUMN, W_CHILD_INPUT_UNKNOWN, W_BRANCH_UNWIRED, W_UNRESOLVED_REF. --strict fails on them in every block, except W_BRANCH_UNWIRED on a block the lock binds (the stored workflow may hold it, and the server then stays quiet); --no-strict fails on none of them. In a function file changed since the get, W_UNRESOLVED_REF is the error E_APPLY_UNRESOLVED_REF in every mode, as the server refuses it. W_JSON_REF_UNTYPED is reported only on blocks the lock does not bind, and with --strict on every block.
791+
These warnings, which an apply saves as written, fail the check as errors on every block the lock does not bind (a block added since the get, or every block of a new workflow): W_REQUIRED_MISSING, W_UNKNOWN_OPTION, W_UNKNOWN_OUTPUT_PATH, W_VALUE_KIND, W_OUT_OF_RANGE, W_LITERAL_SECRET, W_TABLE_UNKNOWN_COLUMN, W_CHILD_INPUT_UNKNOWN, W_BRANCH_UNWIRED, W_UNRESOLVED_REF, W_REF_OUT_OF_SCOPE. --strict fails on them in every block, except W_BRANCH_UNWIRED on a block the lock binds (the stored workflow may hold it, and the server then stays quiet); --no-strict fails on none of them. In every mode, as the server refuses them: W_REF_OUT_OF_SCOPE is the error E_APPLY_REF_OUT_OF_SCOPE in any file of a block the edit added or changed; W_UNKNOWN_OUTPUT_PATH is the error E_APPLY_UNRESOLVED_REF in workflow.ts on a block the edit added or changed; W_UNRESOLVED_REF is the error E_APPLY_UNRESOLVED_REF in a function file changed since the get; W_UNKNOWN_OUTPUT_PATH is the error E_APPLY_UNRESOLVED_REF in a function file changed since the get; W_CODE_FILE_REF_SYNTAX is the error E_APPLY_CODE_FILE_REF_SYNTAX in a function file changed since the get. A block counts as changed when the lock does not bind it, when the source moved it into another container, or when it compiles differently than in the source the last get or apply printed (.sim/stored-edges.json records a digest of each block). That last comparison needs the catalog the source was printed with in the cache: without it, a bound block that did not move counts as unchanged, so the server may refuse what the check passes. A $ref in a function file to a block the lock binds that the source no longer declares stays a warning, unless a warning cap left findings out: the server may infer that block was renamed and rewrite the reference, and refuses it otherwise. W_JSON_REF_UNTYPED is reported only on blocks the lock does not bind, and with --strict on every block.
792792

793793
Exit 10 (busy, retry): another sim workspaces source command is using this directory (SOURCE_DIR_BUSY); nothing was read or written. Run this one again once it finished.
794794

0 commit comments

Comments
 (0)