@sanity/workflows-cli 0.0.0-main-039-20261113092657

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (132) hide show
  1. package/CHANGELOG.md +1936 -0
  2. package/LICENSE +21 -0
  3. package/README.md +814 -0
  4. package/bin/run.js +8 -0
  5. package/dist/command-ids.d.ts +10 -0
  6. package/dist/command-ids.js +10 -0
  7. package/dist/commands/workflows/abort.d.ts +29 -0
  8. package/dist/commands/workflows/abort.js +50 -0
  9. package/dist/commands/workflows/blueprints/generate.d.ts +12 -0
  10. package/dist/commands/workflows/blueprints/generate.js +46 -0
  11. package/dist/commands/workflows/codemod/packages.d.ts +13 -0
  12. package/dist/commands/workflows/codemod/packages.js +41 -0
  13. package/dist/commands/workflows/definitions/delete.d.ts +44 -0
  14. package/dist/commands/workflows/definitions/delete.js +74 -0
  15. package/dist/commands/workflows/definitions/diff.d.ts +23 -0
  16. package/dist/commands/workflows/definitions/diff.js +55 -0
  17. package/dist/commands/workflows/definitions/list.d.ts +51 -0
  18. package/dist/commands/workflows/definitions/list.js +139 -0
  19. package/dist/commands/workflows/definitions/show.d.ts +65 -0
  20. package/dist/commands/workflows/definitions/show.js +129 -0
  21. package/dist/commands/workflows/definitions/validate.d.ts +14 -0
  22. package/dist/commands/workflows/definitions/validate.js +50 -0
  23. package/dist/commands/workflows/deploy.d.ts +117 -0
  24. package/dist/commands/workflows/deploy.js +266 -0
  25. package/dist/commands/workflows/diagnose.d.ts +52 -0
  26. package/dist/commands/workflows/diagnose.js +195 -0
  27. package/dist/commands/workflows/fire-action.d.ts +53 -0
  28. package/dist/commands/workflows/fire-action.js +182 -0
  29. package/dist/commands/workflows/list.d.ts +78 -0
  30. package/dist/commands/workflows/list.js +248 -0
  31. package/dist/commands/workflows/nuke.d.ts +73 -0
  32. package/dist/commands/workflows/nuke.js +176 -0
  33. package/dist/commands/workflows/reset-activity.d.ts +32 -0
  34. package/dist/commands/workflows/reset-activity.js +60 -0
  35. package/dist/commands/workflows/show.d.ts +29 -0
  36. package/dist/commands/workflows/show.js +156 -0
  37. package/dist/commands/workflows/start.d.ts +57 -0
  38. package/dist/commands/workflows/start.js +196 -0
  39. package/dist/commands/workflows/tail.d.ts +23 -0
  40. package/dist/commands/workflows/tail.js +83 -0
  41. package/dist/commands/workflows/whoami.d.ts +15 -0
  42. package/dist/commands/workflows/whoami.js +53 -0
  43. package/dist/help.d.ts +13 -0
  44. package/dist/help.js +50 -0
  45. package/dist/hooks/finally/telemetry.d.ts +3 -0
  46. package/dist/hooks/finally/telemetry.js +10 -0
  47. package/dist/hooks/prerun/telemetry.d.ts +28 -0
  48. package/dist/hooks/prerun/telemetry.js +37 -0
  49. package/dist/index.d.ts +1 -0
  50. package/dist/index.js +1 -0
  51. package/dist/lib/base-command.d.ts +14 -0
  52. package/dist/lib/base-command.js +41 -0
  53. package/dist/lib/blueprint-emit.d.ts +56 -0
  54. package/dist/lib/blueprint-emit.js +233 -0
  55. package/dist/lib/blueprint-needs.d.ts +5 -0
  56. package/dist/lib/blueprint-needs.js +231 -0
  57. package/dist/lib/blueprint-stacks.d.ts +6 -0
  58. package/dist/lib/blueprint-stacks.js +111 -0
  59. package/dist/lib/cause-detail.d.ts +9 -0
  60. package/dist/lib/cause-detail.js +112 -0
  61. package/dist/lib/client.d.ts +27 -0
  62. package/dist/lib/client.js +45 -0
  63. package/dist/lib/codemod/ast.d.ts +17 -0
  64. package/dist/lib/codemod/ast.js +14 -0
  65. package/dist/lib/codemod/export-rename.d.ts +17 -0
  66. package/dist/lib/codemod/export-rename.js +253 -0
  67. package/dist/lib/codemod/manifest-rewrite.d.ts +14 -0
  68. package/dist/lib/codemod/manifest-rewrite.js +97 -0
  69. package/dist/lib/codemod/package-map.d.ts +26 -0
  70. package/dist/lib/codemod/package-map.js +90 -0
  71. package/dist/lib/codemod/report.d.ts +4 -0
  72. package/dist/lib/codemod/report.js +46 -0
  73. package/dist/lib/codemod/run.d.ts +23 -0
  74. package/dist/lib/codemod/run.js +106 -0
  75. package/dist/lib/codemod/source-rewrite.d.ts +20 -0
  76. package/dist/lib/codemod/source-rewrite.js +149 -0
  77. package/dist/lib/command-context.d.ts +20 -0
  78. package/dist/lib/command-context.js +24 -0
  79. package/dist/lib/command-telemetry.d.ts +54 -0
  80. package/dist/lib/command-telemetry.js +82 -0
  81. package/dist/lib/context.d.ts +155 -0
  82. package/dist/lib/context.js +272 -0
  83. package/dist/lib/default-partition.d.ts +23 -0
  84. package/dist/lib/default-partition.js +48 -0
  85. package/dist/lib/definition-files.d.ts +14 -0
  86. package/dist/lib/definition-files.js +60 -0
  87. package/dist/lib/definitions.d.ts +63 -0
  88. package/dist/lib/definitions.js +84 -0
  89. package/dist/lib/diff.d.ts +12 -0
  90. package/dist/lib/diff.js +93 -0
  91. package/dist/lib/env.d.ts +7 -0
  92. package/dist/lib/env.js +8 -0
  93. package/dist/lib/fail.d.ts +36 -0
  94. package/dist/lib/fail.js +55 -0
  95. package/dist/lib/failure-kind.d.ts +7 -0
  96. package/dist/lib/failure-kind.js +66 -0
  97. package/dist/lib/flags.d.ts +49 -0
  98. package/dist/lib/flags.js +117 -0
  99. package/dist/lib/follow-up-command.d.ts +18 -0
  100. package/dist/lib/follow-up-command.js +12 -0
  101. package/dist/lib/history-rows.d.ts +3 -0
  102. package/dist/lib/history-rows.js +44 -0
  103. package/dist/lib/load-config.d.ts +51 -0
  104. package/dist/lib/load-config.js +108 -0
  105. package/dist/lib/nuke.d.ts +124 -0
  106. package/dist/lib/nuke.js +206 -0
  107. package/dist/lib/operation-args.d.ts +42 -0
  108. package/dist/lib/operation-args.js +21 -0
  109. package/dist/lib/ops-report.d.ts +45 -0
  110. package/dist/lib/ops-report.js +31 -0
  111. package/dist/lib/params.d.ts +3 -0
  112. package/dist/lib/params.js +21 -0
  113. package/dist/lib/pick-dataset.d.ts +10 -0
  114. package/dist/lib/pick-dataset.js +30 -0
  115. package/dist/lib/prompt.d.ts +35 -0
  116. package/dist/lib/prompt.js +11 -0
  117. package/dist/lib/read-fanout.d.ts +45 -0
  118. package/dist/lib/read-fanout.js +44 -0
  119. package/dist/lib/select-deployment.d.ts +31 -0
  120. package/dist/lib/select-deployment.js +91 -0
  121. package/dist/lib/share-definitions.d.ts +101 -0
  122. package/dist/lib/share-definitions.js +188 -0
  123. package/dist/lib/telemetry-setup.d.ts +81 -0
  124. package/dist/lib/telemetry-setup.js +111 -0
  125. package/dist/lib/telemetry.d.ts +81 -0
  126. package/dist/lib/telemetry.js +90 -0
  127. package/dist/lib/ui.d.ts +91 -0
  128. package/dist/lib/ui.js +116 -0
  129. package/dist/standalone-argv.d.ts +14 -0
  130. package/dist/standalone-argv.js +57 -0
  131. package/oclif.manifest.json +1731 -0
  132. package/package.json +121 -0
package/README.md ADDED
@@ -0,0 +1,814 @@
1
+ # @sanity/workflows-cli
2
+
3
+ > **Documentation:** [Workflows docs](https://www.sanity.io/docs/workflows). For this package, see [CLI reference](https://www.sanity.io/docs/workflows/cli-reference).
4
+
5
+ Command-line tool for deploying, inspecting, and administering Sanity workflow
6
+ definitions and instances.
7
+
8
+ The package is in prerelease and is publicly available on npm. Commands that
9
+ access Sanity use the resources in `sanity.workflow.ts` and authenticate with a
10
+ login session or an API token. `deploy --check` validates definitions and
11
+ checks the generated runtime offline, and `definitions validate` checks
12
+ definition JSON files without a config.
13
+
14
+ ## Run
15
+
16
+ Use Node.js 20.12 or later. Install the CLI and its engine peer from the same
17
+ Workflows release in your project:
18
+
19
+ ```sh
20
+ npm install --save-dev @sanity/workflows-cli @sanity/workflows
21
+ npx @sanity/workflows-cli --help
22
+ ```
23
+
24
+ After authentication and [configuration](#configuration), run commands through
25
+ the project-local package:
26
+
27
+ ```sh
28
+ npx @sanity/workflows-cli deploy --dry-run
29
+ npx @sanity/workflows-cli list --include-completed
30
+ npx @sanity/workflows-cli show INSTANCE_ID
31
+ ```
32
+
33
+ Replace `INSTANCE_ID` with the workflow instance ID returned by `list`.
34
+
35
+ The package is an [oclif](https://oclif.io) plugin: every command's canonical
36
+ id nests under the `workflows` topic
37
+ (`sanity-workflows workflows deploy`), so the `sanity` CLI runs the same
38
+ commands as `sanity workflows …` without colliding with its own root
39
+ commands (the sanity CLI already has a `deploy`). The short forms used
40
+ throughout this README (`deploy`, `definitions list`, …) are the standalone
41
+ binary's stable surface — its entrypoint rewrites them onto the topic before
42
+ oclif resolves the command. Those short forms are **not** registered as oclif
43
+ aliases, so a host mount does not pollute the host root.
44
+
45
+ Authenticate with `npx sanity@latest login`; the CLI reads that session token.
46
+ For CI, set `SANITY_AUTH_TOKEN` instead. Create a `sanity.workflow.ts` in your
47
+ project, as described in [Configuration](#configuration).
48
+
49
+ The token must span **every resource the workflow references**, not just the
50
+ workflow resource. Runtime commands build one client from the deployment
51
+ descriptor; when a workflow's subject or `doc.ref` fields point into other
52
+ datasets (or a canvas / media library), the engine reaches them through
53
+ sibling clients derived from that same token. A `sanity login` session or an
54
+ org-capable token covers this out of the box; a token scoped to a single
55
+ project will fail on cross-resource reads.
56
+
57
+ ## Configuration
58
+
59
+ The CLI is configured by a `sanity.workflow.ts` (or `.js`/`.mjs`). It looks in
60
+ the directory you run from, then in each parent directory, and uses the first
61
+ one it finds. Pass `--path` to name a config file, or a directory to start the
62
+ search from. Wherever you run the command, `blueprints generate` writes the
63
+ generated Blueprint files next to the config file, and `deploy` checks them
64
+ there.
65
+
66
+ The file exports a config built with `defineWorkflowConfig`, declaring your
67
+ **deployments** (typically one per environment):
68
+
69
+ ```ts
70
+ import type {WorkflowDeploymentInput} from '@sanity/workflows'
71
+ import {defineWorkflowConfig} from '@sanity/workflows/define'
72
+
73
+ import {articleReview, urlDraft} from './src/workflows.ts'
74
+
75
+ // Export each deployment under its own `name`, then list the exports in the
76
+ // config. `blueprints generate` requires these named exports, and so does
77
+ // `deploy` once a generated runtime sits next to this file; nothing else does.
78
+ export const production = {
79
+ expectedMinReaderModel: 4,
80
+ name: 'production', // the deployment's unique identity (lowercase letters, digits, dashes)
81
+ partition: 'prod', // the environment partition the engine's docs are scoped to
82
+ workflowResource: {type: 'dataset', id: 'acme.workflows'}, // where those docs live
83
+ resourceAliases: [
84
+ // binds each `@<handle>:` a definition references to a physical resource
85
+ // in a DIFFERENT dataset from `workflowResource` (same-resource content
86
+ // needs no alias — see below)
87
+ {name: 'content', resource: {type: 'dataset', id: 'acme.content'}},
88
+ {name: 'assets', resource: {type: 'dataset', id: 'acme.assets'}},
89
+ ],
90
+ definitions: [articleReview, urlDraft], // the batch this deployment ships
91
+ } satisfies WorkflowDeploymentInput
92
+
93
+ export default defineWorkflowConfig({deployments: [production]})
94
+ ```
95
+
96
+ `name` is the deployment's identity: unique across the config and constrained
97
+ to the same grammar as `partition`. Partitions group deployments by environment and may
98
+ repeat, as long as no two deployments share both a `workflowResource` and a
99
+ `partition`. The config rejects that collision and names both deployments.
100
+
101
+ For `deploy`, `start`, `definitions diff`, and `definitions delete`, select a
102
+ deployment with `--deployment <name>`. With one deployment configured, you can
103
+ omit the selector. `--partition <partition>` selects one deployment when the partition is unique;
104
+ on `deploy`, it targets every deployment carrying the partition. With several
105
+ deployments and no selector, these commands prompt for a deployment in an
106
+ interactive terminal. In a non-interactive shell, they fail asking for
107
+ `--deployment` or `--partition`; `deploy` also suggests `--all-partitions`.
108
+
109
+ The instance-targeted commands `abort`, `reset-activity`, `fire-action`,
110
+ `diagnose`, `show`, and `tail` locate the instance by ID across
111
+ configured resources. Their optional `--deployment` and `--partition` flags
112
+ narrow the resources searched; the operation's partition comes from the loaded
113
+ instance.
114
+
115
+ `list`, `definitions list`, and `definitions show` read every configured resource
116
+ and partition by default. `--partition` filters to one partition, and
117
+ `--deployment` reads only that deployment's resource and partition.
118
+
119
+ The read commands `list`, `show`, `tail`, `diagnose`, `definitions list`,
120
+ `definitions show`, and `whoami` can also read one resource you name, with or without a
121
+ `sanity.workflow.ts`. Use the same flags as the Sanity CLI:
122
+ `--project-id <id>` and `--dataset <name>` (`-p` and `-d`) together for a
123
+ dataset, or `--media-library-id <id>` for a media library. The CLI then reads
124
+ only that resource and loads no config, so `--deployment` and `--path` can't be
125
+ combined with them. `--partition` still filters. Commands that write, such as
126
+ `start`, `abort`, and `deploy`, always act through a deployment in
127
+ `sanity.workflow.ts`.
128
+
129
+ While the experimental `SANITY_STUDIO_WORKFLOWS_SERVICE=true` is set in your
130
+ shell, `start`, `fire-action`, and `abort` send their write to the project's
131
+ Workflows service when your token can read workflow instances but not write
132
+ them, and the project has a service. Otherwise they write directly with your
133
+ token.
134
+
135
+ ```sh
136
+ sanity workflows definitions list --project-id abc123 --dataset production
137
+ sanity workflows show prod.wf-instance.x7k2 -p abc123 -d production
138
+ ```
139
+
140
+ When no `sanity.workflow.ts` is found and you pass none of these flags or
141
+ `--path` or `--deployment`, a read in an interactive terminal asks you to pick
142
+ a project, then one of its datasets, from those your login can access. It then
143
+ prints the `--project-id` and `--dataset` flags that read the same dataset, so
144
+ you can script the next run. The picker offers datasets only; pass
145
+ `--media-library-id` to read a media library. With `--json` or in a
146
+ non-interactive shell, the read fails and names the flags instead.
147
+
148
+ When an instance ID or definition name is found in more than one deployment,
149
+ for example two deployments on different resources that both use `default`,
150
+ an interactive terminal prompts you to pick one. Each choice shows the
151
+ deployment name with its dataset. With `--json` or in a non-interactive shell,
152
+ the command fails, lists each candidate as `name (dataset)`, and asks for
153
+ `--deployment`.
154
+
155
+ A deployment alone on its configured resource may omit `partition`; it resolves
156
+ to `default`, independent of its name. If several deployments share that
157
+ resource, give every one an explicit, distinct partition. The engine stores the
158
+ partition on every definition and instance the deployment writes, so a
159
+ deployment only reaches documents written under its own partition value.
160
+
161
+ `--all-partitions` deploys every deployment in the config in one run: a failure in one doesn't
162
+ stop the rest — the run continues, prints a summary of what failed, and exits
163
+ non-zero. The client's project + dataset are derived from the deployment's
164
+ `workflowResource`, and `deploy` expands each definition's `@<handle>:`
165
+ reference to the bound physical resource. An invalid config fails with a clean,
166
+ path-prefixed error before the command runs.
167
+
168
+ `resourceAliases` is only for content that lives in a _different_ resource from
169
+ `workflowResource`. A definition can reference a document in the **same**
170
+ resource by bare id, no alias needed — bare ids root at `workflowResource` at
171
+ runtime. `@<handle>:` references exist at deploy time only: `deploy` expands
172
+ each one to its bound physical GDR, and nothing alias-shaped survives into the
173
+ deployed definition or the runtime. Rule of thumb: **different resource →
174
+ bind an alias; same resource → use a bare id and omit `resourceAliases`
175
+ entirely.** So the simplest single-dataset setup is just a `workflowResource`
176
+ and `definitions`, with no `resourceAliases` at all.
177
+
178
+ Refs supplied at runtime (`start --field`, `fire-action` ref params) are gated
179
+ separately: they may target the `workflowResource` itself or any resource the
180
+ deployment binds in `resourceAliases`, and anything else is rejected at the
181
+ write with `RefResourceUndeclaredError`. To start a workflow on content in
182
+ another dataset or project, bind that resource even when no definition
183
+ references the alias. The CLI reaches each bound resource with the same token
184
+ and API host it uses for `workflowResource`, so that login must be a member of
185
+ every bound project. Instance commands use the bindings of the configured
186
+ deployment that owns the instance's partition on the resource it was found in.
187
+ When no configured deployment owns that partition, refs may target only the
188
+ workflow resource.
189
+
190
+ For a `release.ref` field, `start --field` also takes a bare Content Release
191
+ name, such as `--field release=rA1b2C3d4`. The CLI builds the release ref on
192
+ the one resource the deployment binds in `resourceAliases` other than
193
+ `workflowResource`, or on `workflowResource` when it binds no other. When it
194
+ binds several other resources, name one with `--release-resource <alias>` or
195
+ pass the full object instead.
196
+
197
+ | Var | Purpose |
198
+ | ------------------- | ---------------------------------------------------------------------------------------------------- |
199
+ | `SANITY_AUTH_TOKEN` | Explicit token for CI / scripted use — wins over the `sanity login` session (editor role for writes) |
200
+ | `SANITY_API_HOST` | Optional API host override (defaults to the prod API) |
201
+
202
+ Your `sanity.workflow.ts` loads straight from TypeScript source: the CLI
203
+ transpiles the config file on the fly with `jiti`. Definitions you author
204
+ **inline** in the config, or import by **relative path**, transpile the same
205
+ way — no build step. A definition imported from an installed package loads
206
+ that package's published JavaScript.
207
+
208
+ ## Command list
209
+
210
+ Invoke as `sanity workflows <command>` in the Sanity CLI, or `sanity-workflows <command>` through the standalone binary. Suggested next commands in the output use whichever form you ran.
211
+
212
+ | Command | Description |
213
+ | ----------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
214
+ | `deploy` | Validate the definitions, check any generated runtime still matches them and, with the experimental service switch set, that the Workflows service can read them, then deploy the selected deployment. |
215
+ | `deploy --all-partitions` | Deploy every deployment in the config. A failure in one does not stop the rest. |
216
+ | `deploy --check` | Validate local definitions and check the generated runtime, without contacting the dataset or the Workflows service. |
217
+ | `deploy --dry-run` | Check the generated runtime and, with the experimental service switch set, the Workflows service's data model, then show a diff against what is already deployed, without writing. |
218
+ | `deploy --verbose` | Print the full report of the functions the selected deployments need when no generated runtime is here, in place of the summary. |
219
+ | `deploy --only <name>` | Limit deploy, check, or dry-run to one definition. The generated-runtime check still covers the whole config. |
220
+ | `start <name>` | Start an instance from a deployed definition. Pass `--field` for input values. |
221
+ | `list` | List instances (in-flight by default). Filter with `--definition`. |
222
+ | `show <instance-id>` | Show an instance's state, activities, and effects. |
223
+ | `diagnose <instance-id>` | Explain why an instance is or isn't progressing, and what would unstick it. |
224
+ | `tail <instance-id>` | Stream new history entries as they land. |
225
+ | `abort <instance-id>` | Stop an in-flight instance. Pending effects cancel; the instance stays where it is. |
226
+ | `reset-activity <instance-id> <activity>` | Re-run a failed activity, or `--skip` it so a gated transition can fire. |
227
+ | `fire-action <instance-id>` | Fire an action on a waiting activity. Omit `--action` to list what can be fired. |
228
+ | `whoami` | Show your user ID as Workflows records it. Put the Sanity user ID in user assignee fields. `--json` prints an object. |
229
+ | `blueprints generate` | Write the Sanity Blueprints runtime the definitions require next to `sanity.workflow.ts`. |
230
+ | `blueprints generate --check` | Verify that runtime still matches the definitions. Writes nothing; exits non-zero on any difference. |
231
+ | `blueprints generate --verbose` | Print the full runtime-needs report, every file with its status, and the heartbeat stack's instructions on every run, in place of the summary. |
232
+ | `definitions list` | List deployed definitions. |
233
+ | `definitions show <name>` | Show a deployed definition (latest version, or `--version`). |
234
+ | `definitions validate <file...>` | Validate definitions in JSON files without a config, login, or network. `--json` prints the machine-readable report. |
235
+ | `definitions diff <name>` | Diff the in-code definition against what is deployed. |
236
+ | `definitions delete <name>` | Delete a deployed definition. Refuses while live instances exist unless `--cascade`. |
237
+ | `nuke --deployment <name>` / `nuke --partition <partition>` | Dev reset: delete every engine-owned document for that partition (instances, definitions, guards). Prints a plan, then asks for confirmation. Content documents are never touched. |
238
+ | `nuke --instance <id>` | Dev reset: delete one finished instance and its guards. Refuses in-flight instances (abort first). |
239
+
240
+ The `definitions` and `blueprints` commands also answer to the singular
241
+ `definition` and `blueprint`, so `definition list` runs `definitions list`.
242
+
243
+ ## Generating the Blueprints runtime
244
+
245
+ A definition that declares an effect, reads `$now`, or starts autonomously needs
246
+ server-side code: something has to run the effect handlers, sample deadlines, and
247
+ start instances from document writes. `blueprints generate` derives that runtime
248
+ from the definitions and writes it next to `sanity.workflow.ts`, so
249
+ `npx sanity@latest blueprints deploy` has something to deploy. A purely interactive
250
+ workflow needs none of it, and the command says so.
251
+
252
+ Current gaps:
253
+
254
+ - A start watcher in a dataset starts a definition on the write that makes its
255
+ `start.filter` true, even for a document that already has an instance from
256
+ an earlier time it became true. A start filter that reads `$now` or another
257
+ document, reads `$tag` or `$definition` and a field of the document other
258
+ than `_type` (or the whole document), or is not an expression the generator
259
+ recognizes as boolean, starts on every matching write instead, and the
260
+ generator warns about it and names which of these applies. Only the definition's own
261
+ start requirements refuse a start, such as a `singleSubject` requirement.
262
+ - A Media Library trigger receives one upload as more than one write, so
263
+ `blueprints generate` fails for an autonomous asset definition watched on a
264
+ Media Library whose `start.filter` would start on every write: one that reads
265
+ `$now` or another document, reads `$tag` or `$definition` and a field of the
266
+ asset other than `_type`, or is not an expression the generator recognizes as
267
+ boolean. Other asset start filters are watched as on a dataset.
268
+ - Definitions and functions deploy in two steps, definitions first, until the
269
+ Blueprints service registers the `sanity.workflow` resource.
270
+ - A generated file cannot be taken over: editing one is drift, and the next
271
+ generation rewrites it.
272
+ - A retry policy is not checked against its function's timeout at generation, so
273
+ an effect can be configured to retry for longer than its function may run.
274
+
275
+ Blueprint generation parses `sanity.blueprint.ts` without loading your
276
+ project's TypeScript installation. The CLI works beside TypeScript 6 or 7 and
277
+ does not require TypeScript for this command.
278
+
279
+ Five prerequisites:
280
+
281
+ - **Each deployment is exported under its own `name`, and the export is the
282
+ deployment object itself**, as the [Configuration](#configuration) example
283
+ does: every generated module imports its deployment from `./sanity.workflow`
284
+ by that name. The command refuses to write anything until each name is
285
+ exported, and names the export to add. It checks the name only, so make sure
286
+ each export is the deployment your config lists. A typecheck of the generated
287
+ modules catches a wrong export only where a generated function uses it, and a
288
+ deployment that needs no functions emits none.
289
+ - **`sanity.workflow.ts` is importable inside a deployed function.** Every
290
+ generated module imports it, and each function bundles it, so the file runs
291
+ where your shell and your `.env` do not exist. Reading an absent variable
292
+ there yields `undefined`; what fails is a config that **throws** when a
293
+ variable is missing, or requires one to build the deployment, because that
294
+ runs on init before your handler is reached. Keep the deployment's resource
295
+ coordinates literal in the file and read secrets inside your handlers.
296
+ - **The Blueprints packages the generated modules import are installed**,
297
+ alongside the CLI and its peers.
298
+ - **Your definitions are deployed before the functions.** The generated
299
+ `workflows.blueprint.ts` carries the definitions entry commented out, because
300
+ the Blueprints service does not accept the `sanity.workflow` resource type
301
+ yet, so `npx sanity@latest blueprints deploy` does not deploy them. Run
302
+ `npx @sanity/workflows-cli deploy` first: a function that starts or advances an
303
+ instance resolves its definition from the Content Lake, and finds nothing
304
+ until that deploy has run.
305
+ - **A generated heartbeat deploys from its own directory, with an organization
306
+ deploy token.** The heartbeat is a scheduled function, and the platform only
307
+ accepts one on a stack in organization scope. The command writes it into
308
+ `workflows-heartbeat/sanity.blueprint.ts`, a blueprint of its own, so the
309
+ document functions in your `sanity.blueprint.ts` deploy to a project-scoped
310
+ stack with a token that can deploy to the project. Each heartbeat stack counts
311
+ toward your organization's limit of 10 organization-scoped stacks. How often
312
+ it may run is a plan limit rather than something the generator chooses — see
313
+ [the schedule](#the-generated-heartbeats-schedule) below.
314
+
315
+ ```sh
316
+ npm install --save-dev @sanity/blueprints @sanity/functions @sanity/workflows
317
+ npx @sanity/workflows-cli blueprints generate
318
+ npx @sanity/workflows-cli deploy
319
+ # Project stack, with a token that can deploy to the project
320
+ npx sanity@latest blueprints init . --project-id <projectId> # first deploy only
321
+ npx sanity@latest blueprints deploy
322
+ # Heartbeat stack, with an organization deploy token, when one was generated
323
+ cd workflows-heartbeat
324
+ npx sanity@latest blueprints init . --organization-id <organizationId> # first deploy only
325
+ npx sanity@latest blueprints deploy
326
+ ```
327
+
328
+ Each generated function's `package.json`, and the heartbeat stack's
329
+ `workflows-heartbeat/package.json`, copies the specifications your project
330
+ declares for `@sanity/functions`, `@sanity/workflows/blueprints`, and
331
+ `@sanity/workflows`. A package you declare nowhere fails the command
332
+ rather than being stamped with a guessed range. The Functions build installs a
333
+ function's dependencies from the `package.json` beside the blueprint it deploys
334
+ from, so the heartbeat built from `workflows-heartbeat/` runs the versions your
335
+ project pins rather than the latest ones.
336
+
337
+ The command prints a short summary: one line per deployment saying how many
338
+ functions it needs, the files that need you (a new handler scaffold, a change to
339
+ your own `sanity.blueprint.ts`, anything it could not resolve), a count of the
340
+ files it wrote, and a `Deploy:` section naming which stack to deploy with which
341
+ token. The project stack is always named when there are document functions,
342
+ because each function bundles `sanity.workflow.ts`; the heartbeat stack is named
343
+ when this run created or updated one of its files, and on every run when a
344
+ deployment has scheduled drains (a workflow stored in a Media Library). On the
345
+ first run:
346
+
347
+ ```
348
+ Runtime needs:
349
+ prod (prod) needs 3 Sanity Functions (1 effect drain, 1 heartbeat, 1 start watcher)
350
+
351
+ Generated tree:
352
+ created effect-handlers/send-notification.ts yours now — implement the effect
353
+ created sanity.blueprint.ts the workflow resources spread
354
+
355
+ ✔ 12 file(s) written (12 created), 0 left untouched
356
+
357
+ Deploy:
358
+ project stack: `npx sanity@latest blueprints deploy` here, with a token that can deploy to the project. It holds every document function; redeploy it after any definition change.
359
+ heartbeat stack: `npx sanity@latest blueprints deploy` in workflows-heartbeat/, with an organization deploy token. It holds only the scheduled heartbeat.
360
+ ⚠ Without the heartbeat stack nothing checks these on a schedule, and nothing reports it:
361
+ $now deadlines take effect only when another engine call evaluates them in article-review
362
+ an effect queued before its drain existed, or whose drain timed out, waits until a later drain picks it up: send-notification
363
+
364
+ Pass --verbose for the full report.
365
+ ```
366
+
367
+ When a later run changes a heartbeat-stack file, the command cannot tell
368
+ whether you deployed that stack, so it names the files and asks conditionally:
369
+
370
+ ```
371
+ ⚠ workflows-heartbeat/sanity.blueprint.ts changed: if you have deployed the heartbeat stack, redeploy it with the organization deploy token.
372
+ ```
373
+
374
+ `--verbose` prints the full needs report, every file with its status, and the
375
+ heartbeat stack's instructions whether or not this run changed its files.
376
+
377
+ The heartbeat stack's blueprint lists, in its header, the partition, the
378
+ workflow resource and each resource an alias binds, the `'function'`-hosted
379
+ workflows the heartbeat ticks, the `'function'`-hosted effects it re-queues, and
380
+ any effects it drains on schedule. Editing an existing workflow stored in a
381
+ dataset leaves it unchanged, so the default `Deploy:` section does not name the
382
+ heartbeat stack, and `--verbose` lists the file as `unchanged`. A workflow
383
+ stored in a Media Library drains its effects from the heartbeat stack, which
384
+ bundles your handlers, so the `Deploy:` section always names that stack: redeploy
385
+ it after you change a handler or a definition. Adding or removing a
386
+ `'function'`-hosted workflow or effect changes it: redeploy the heartbeat stack
387
+ too. So does moving the declared specification of `@sanity/functions`,
388
+ `@sanity/workflows/blueprints`, or `@sanity/workflows`,
389
+ which changes `workflows-heartbeat/package.json`, the manifest the heartbeat
390
+ stack's build installs from; the `Deploy:`
391
+ section names every heartbeat-stack file that changed. The heartbeat bundles the
392
+ installed runtime when it is deployed, so after upgrading the Workflows packages
393
+ redeploy both stacks even when every file is `unchanged`. `blueprints generate
394
+ --check` reports a changed file as `differs` until you regenerate, like any
395
+ other generated file.
396
+
397
+ Fill in each `effect-handlers/<effect name>.ts`: they are yours, scaffolded once
398
+ and never overwritten. Everything marked `created`, `updated`, or `unchanged`
399
+ under `workflows.blueprint.ts`, `workflows-heartbeat/`,
400
+ `functions/`, and `effect-handlers/all.ts` is regenerated on every run, so
401
+ edits there do not survive. Your own `sanity.blueprint.ts` receives the `workflowResources` import and spread once;
402
+ after that the command leaves it alone. The command never deletes a file. A
403
+ handler for an effect you removed, a scaffold blocked by a path that differs
404
+ only in case, and a blueprint it could not wire are reported for you to resolve.
405
+ So is a stale function directory: one under `functions/` whose `index.ts`
406
+ carries the generator's header but which the definitions no longer produce,
407
+ such as the directory a renamed function leaves behind. The generated blueprint
408
+ does not deploy it. A directory whose `index.ts` has no generator header is
409
+ yours, and is never reported. When no deployment needs a heartbeat any more, a
410
+ `workflows-heartbeat/sanity.blueprint.ts` left from an earlier run is reported
411
+ too: destroy its stack with the organization deploy token, then delete the
412
+ directory.
413
+
414
+ The command covers every deployment in the config, because the emitted
415
+ `workflows.blueprint.ts` declares them all, and
416
+ `workflows-heartbeat/sanity.blueprint.ts` declares every one that needs a
417
+ heartbeat.
418
+
419
+ ### Where each workflow runs
420
+
421
+ The `--verbose` report opens with the hosting each `runtime` block resolved to, after every
422
+ level has inherited from the one above it. It matters because a name listed
423
+ under `durableFunction` or `selfHosted` has no emitted function to point at:
424
+
425
+ - `function` — a drain and the start watchers on the project stack, and the
426
+ heartbeat on its own stack.
427
+ - `durableFunction` — nothing is emitted yet. The rest of the deployment emits
428
+ normally around it.
429
+ - `selfHosted` — nothing is emitted, and the report ends with what your own
430
+ process must do instead: which deadlines to tick, which effects to drain,
431
+ which parents to re-evaluate after they miss a child settling, and which
432
+ autonomous definitions to start. The heartbeat ticks only the
433
+ `'function'`-hosted workflows.
434
+
435
+ ### The generated heartbeat's schedule
436
+
437
+ The heartbeat is emitted on `* * * * *`, one run a minute, and how often the
438
+ platform will actually run it is a plan limit rather than something the
439
+ generator can choose: Sanity documents the threshold as
440
+ [Free daily, Growth hourly, Enterprise minutely](https://www.sanity.io/docs/functions/functions-introduction).
441
+ A workflow whose deadlines depend on the heartbeat advances no faster than the
442
+ heartbeat runs. An effect no drain picked up becomes eligible to re-queue five
443
+ minutes after it was queued, and a later heartbeat run re-queues it; each run
444
+ re-queues on at most 30 instances, so a large backlog takes several runs. Check
445
+ your plan's cadence before relying on a `$now` deadline in a deployed workflow.
446
+
447
+ ### Checking for drift in CI
448
+
449
+ `blueprints generate --check` renders the same runtime in memory and compares it
450
+ with the files on disk. It writes nothing and exits zero when they match:
451
+
452
+ ```
453
+ $ npx @sanity/workflows-cli blueprints generate --check
454
+ ✔ the generated tree matches the definitions
455
+ ```
456
+
457
+ Otherwise it exits `1` and names every file, so the log alone says what to fix:
458
+
459
+ ```
460
+ $ npx @sanity/workflows-cli blueprints generate --check
461
+ ✖ The generated tree does not match the definitions (1):
462
+ differs effect-handlers/all.ts
463
+ Run `sanity-workflows blueprints generate` to bring it back in line.
464
+ $ echo $?
465
+ 1
466
+ ```
467
+
468
+ A file reports `missing` when nothing is at its path, `differs` when the file
469
+ there has other contents, and a handler for a declared effect reports `missing`
470
+ when you have not written it yet. A stale function directory also fails the
471
+ check, and the report names it. Add the command to the job that builds your
472
+ blueprint and a definition change can no longer ship without the runtime it
473
+ needs.
474
+
475
+ ### Every deploy runs the same check
476
+
477
+ `deploy` validates the definitions, then looks for `workflows.blueprint.ts` next
478
+ to `sanity.workflow.ts`. When that file is there, the deploy runs the same
479
+ comparison as [Checking for drift in CI](#checking-for-drift-in-ci), and a
480
+ difference stops it:
481
+
482
+ ```
483
+ $ npx @sanity/workflows-cli deploy --partition prod
484
+ ✖ The generated tree does not match the definitions (1):
485
+ differs workflows.blueprint.ts
486
+ Run `sanity-workflows blueprints generate` to bring it back in line.
487
+ $ echo $?
488
+ 1
489
+ ```
490
+
491
+ Nothing is written when that happens: the check runs before the first definition
492
+ reaches the dataset, and before the CLI resolves a token. A stale function directory
493
+ does not stop the deploy, because the generated blueprint does not deploy it.
494
+ The deploy prints a warning naming the directory and continues. Run
495
+ `blueprints generate`, review what it wrote, and deploy again.
496
+
497
+ Definitions deploy first and the functions follow: `sanity-workflows deploy`
498
+ writes the definitions, then `npx sanity@latest blueprints deploy` ships the runtime
499
+ that serves them. That order is why the deploy is where drift is caught. It is
500
+ the step that would otherwise put definitions in front of a runtime that cannot
501
+ serve them.
502
+
503
+ When no `workflows.blueprint.ts` is there, the deploy summarises the functions
504
+ the selected deployments need and continues, so a project that has not
505
+ generated one yet still deploys:
506
+
507
+ ```
508
+ $ npx @sanity/workflows-cli deploy --partition prod
509
+ Runtime needs:
510
+ prod (prod) needs 3 Sanity Functions (1 effect drain, 1 heartbeat, 1 start watcher)
511
+
512
+ ⚠ No generated runtime is here. Run `sanity-workflows blueprints generate` to emit one, unless a runtime you maintain yourself already serves these definitions. Pass --verbose for the full report.
513
+ ```
514
+
515
+ `deploy --verbose` prints the full report in place of the summary: each
516
+ function, what it runs, and why the definitions need it, the same report
517
+ `blueprints generate --verbose` prints before it writes. Nothing is written either way.
518
+ The summary and the report cover the deployments the run selects, so a
519
+ deployment needing no functions prints nothing about the runtime even when a
520
+ sibling needs some. Definitions no runtime can be derived from at all print a
521
+ warning saying so. Either way the deploy continues.
522
+
523
+ The check has no off switch, by design: the definition deploy is the one moment
524
+ every deployment passes through, and a flag would be reached for in exactly the
525
+ situation the check exists to catch, which is shipping a definition change whose
526
+ runtime was never regenerated. Nothing runs for a hand-written runtime, because
527
+ a project that never generated a tree has no `workflows.blueprint.ts` for the
528
+ check to compare.
529
+
530
+ Two consequences worth knowing:
531
+
532
+ - The check covers the whole config, because the generated tree declares every
533
+ deployment in it. `deploy --deployment one` still holds the tree to every
534
+ deployment's definitions, so a sibling deployment stops it by drifting or by
535
+ failing validation.
536
+ - `deploy --check` and `deploy --dry-run` run it too, and the check itself is
537
+ offline in both. `--check` is the complete credential-free pre-flight:
538
+ definitions valid, runtime in line, dataset untouched. `--dry-run` goes on to
539
+ authenticate and read the deployed state to build its diff.
540
+
541
+ ## Telemetry
542
+
543
+ The CLI collects usage telemetry through Sanity's standard pipeline. Consent
544
+ is the account-wide status managed by `npx sanity telemetry enable|disable|status`.
545
+ CI and trueish `DO_NOT_TRACK` suppress everything except a deploy that may share
546
+ new definitions, which forces that deploy's telemetry unless `--no-share-defs`
547
+ is passed — see [Definition sharing](#definition-sharing). A session that isn't
548
+ logged in sends nothing. A logged-in run sends whether or not a
549
+ `sanity.workflow.ts` is found. A one-time notice on stderr discloses collection
550
+ on first use.
551
+
552
+ What may be collected is governed by [Sanity's telemetry
553
+ policy](https://www.sanity.io/telemetry). This package sends a subset of it.
554
+
555
+ Collected:
556
+
557
+ - Project id, dataset, and organization id, as session properties. The
558
+ organization id is best effort: it needs a lookup that can resolve to
559
+ nothing, and the event sends either way.
560
+ - Operating system, CPU architecture, JavaScript runtime and version, and the
561
+ CLI version.
562
+ - The command invoked, and the flags it was given. Each is normalised to its
563
+ canonical long form, so `--partition=prod`, `--partition prod`, and the
564
+ deprecated `--tag prod` all report `--partition=prod`. A flag the command declared, and did not redact, reports its
565
+ **value**; because values ship, a flag naming a definition, stage,
566
+ deployment, document, or user reports that name or id verbatim. Redact a
567
+ flag whose value should not leave the machine.
568
+ - Structural counts and closed vocabulary values derived from a definition or
569
+ instance: stage and action counts, activity and field kinds, lifecycle,
570
+ status enums, and durations.
571
+ - Content fingerprints of definitions, and random run ids.
572
+ - `instanceId`, the instance document `_id`. Engine-minted ids embed the
573
+ deployment partition (`<partition>.wf-instance.<random>`), so the partition
574
+ reaches telemetry through this field. A caller-supplied instance id is sent verbatim.
575
+ - The author-chosen effect name, on effect events.
576
+ - Whether a command succeeded and, when it failed, the kind of failure: a
577
+ fixed label such as `usage`, `auth-missing`, `permission-denied`,
578
+ `resource-not-found`, `request-failed`, `config-invalid`,
579
+ `definition-file-invalid`, or the engine error kind (for example
580
+ `instance-not-found`). A failure that matches none of these reports `other`.
581
+
582
+ Never collected, because the policy excludes them:
583
+
584
+ - Environment variables, file paths, and file contents.
585
+ - Logs and serialized JavaScript stack traces.
586
+
587
+ Never collected, because this package goes further than the policy requires.
588
+ The policy does not prohibit any of these, so their absence here is this
589
+ package's choice and not a company-wide guarantee:
590
+
591
+ - The value of a redacted flag. A flag whose value is text a person typed
592
+ reports its name alone: `--reason` on `abort` and `definitions delete`,
593
+ `--field` on `start`, and `--param` on `fire-action`.
594
+ - The value of any flag the command did not declare, including a misspelling
595
+ of a redacted flag. Only the name is reported, and the token after it is
596
+ withheld unless the flag carries its value in the same token (`--resaon=x`,
597
+ `-zx`). The `sanity` CLI sends these whole; withholding them is what keeps
598
+ a mistyped `--reason` from sending the text redaction exists to withhold.
599
+ - Anything after the `--` terminator. Those tokens are not reported at all.
600
+ - Error messages, and stack traces of any language. The policy permits error
601
+ information that excludes sensitive data, but this package's errors quote
602
+ definition names, partitions, and argv, so none is sent. A failure is reported as
603
+ a success boolean and a fixed failure kind, never the error's text.
604
+ - Document content, GROQ, and field values.
605
+ - Definition names and stage names in the engine's own event payloads. A stage
606
+ transition reports positions in `stages[]`, never names. This does not
607
+ extend to flag values, which ship verbatim as described above.
608
+
609
+ Note that a dataset name, a deployment partition, and an effect name are all
610
+ customer-authored. They are collected deliberately, as identifiers and as a
611
+ closed label, not as free text.
612
+
613
+ Definition sharing sends the definition document to a first-party feedback
614
+ endpoint, not through this pipeline; telemetry only records the content-free
615
+ markers below.
616
+
617
+ Each event includes `context.surface: 'cli'`, the `@sanity/workflows-cli`
618
+ package version in `context.packageVersion` (also when run as `sanity
619
+ workflows`), and its process execution mode in `context.environment`. `NODE_ENV=production` reports `production`;
620
+ `development` and `test` report `development`. Unset, empty, and unrecognized
621
+ values default to `production` for installed CLI and MCP runs. The SDK reports
622
+ build mode and defaults to `development` instead.
623
+
624
+ When comparing activity across surfaces, group or filter by `context.surface`
625
+ alongside `context.environment`. Environment does not identify a production
626
+ dataset or deployment; the API host, dataset name, and deployment partition do not
627
+ set it. Existing telemetry consent and opt-out settings still apply.
628
+
629
+ One extension point: supply your own logger as `telemetry` in
630
+ `sanity.workflow.ts` and the built-in pipeline is not constructed at all —
631
+ every event flows to your implementation unconditionally (CI included), and
632
+ consent, suppression, and destination become its business.
633
+
634
+ ### Session properties
635
+
636
+ The built-in store attaches these user properties once per invocation (joined
637
+ to events by session id). User identity is not in the payload; the intake
638
+ service resolves the sender from the authenticated session.
639
+
640
+ | Property | What it is |
641
+ | ----------------- | ----------------------------------------------------------------------------------------------------------------------------------------- |
642
+ | `surface` | Always `'cli'`. |
643
+ | `machinePlatform` | Node `process.platform` (for example `darwin`, `linux`). |
644
+ | `cpuArchitecture` | Node `process.arch`. |
645
+ | `runtime` | Always `'node'` for this CLI. |
646
+ | `runtimeVersion` | Node `process.version`. |
647
+ | `cliVersion` | The CLI package version, when oclif supplied it. |
648
+ | `projectId` | The `--project-id` the run names, else the first dataset-backed deployment's project in `sanity.workflow.ts`. Absent when neither exists. |
649
+ | `dataset` | The `--dataset` the run names, else that deployment's dataset. Absent when neither exists. |
650
+ | `orgId` | Organization id for that project, when the lookup succeeds before the command flushes. Optional. |
651
+
652
+ ### CLI events
653
+
654
+ These events are defined by this package.
655
+
656
+ #### `Workflows CLI Command Executed` (version 3)
657
+
658
+ One trace per workflows command, shaped like the `sanity` CLI's own
659
+ `CLI Command Executed` so both join on the same columns. The built-in store
660
+ fixes the invocation's identity as the trace context in the prerun hook and
661
+ completes the trace in the finally hook; the context rides every event the
662
+ trace emits. Host commands outside the `workflows` topic do not emit.
663
+
664
+ The context carries:
665
+
666
+ | Field | Type | What is collected |
667
+ | ------------------ | ---------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
668
+ | `groupOrCommand` | `string` | Always `'workflows'`. |
669
+ | `commandArguments` | `string[]` | The oclif command path below the topic: `['deploy']`, `['definitions', 'list']`. Empty for the bare topic and for an invocation oclif resolved to no command. Never argv free-text. |
670
+ | `extraArguments` | `string[]` | Each flag on the invocation, in the order given and repeated if repeated, in its canonical `--name=value` form: `--partition prod`, `--partition=prod`, and the deprecated `--tag prod` all report `--partition=prod`. A boolean reports `--name` or `--no-name`. A redacted flag reports `--name` alone, and an undeclared flag reports its name as typed (`--resaon`, `-z`) alone. Tokens after `--` are not reported. |
671
+ | `coreOptions` | `object` | `{debug, help, version}`. `debug` comes from a set `DEBUG` variable, `help` from a literal `help` token, `version` from `--version`. |
672
+
673
+ The payload is sent on `trace.log`:
674
+
675
+ | Field | Type | What is collected |
676
+ | ------------- | --------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
677
+ | `success` | `boolean` | `true` when the command finished without an oclif error. |
678
+ | `failureKind` | `string` | Present only when `success` is `false`. The engine error kind (for example `instance-not-found`), or one of `usage`, `auth-missing`, `permission-denied`, `resource-not-found`, `request-failed`, `config-invalid`, `definition-file-invalid`, and `other`. Never the error's message, name, or stack. |
679
+
680
+ A config-supplied logger has no trace, so it receives the four context fields,
681
+ `success`, and on failure `failureKind`, together as one log payload.
682
+
683
+ #### `Workflows CLI Definition Shared` (version 1)
684
+
685
+ One content-free marker per newly created definition version that actually
686
+ reached Sanity's definition-feedback endpoint. Unchanged redeploys, dry runs,
687
+ and `--check` never emit it. The document itself is not in this event.
688
+
689
+ | Field | Type | What is collected |
690
+ | ------------------ | ---------- | ---------------------------------------------------------------------------------------------------- |
691
+ | `contentHash` | `string` | Content fingerprint of the donated definition. |
692
+ | `deployId` | `string` | Random run id shared with that invocation's `Workflows Engine Definition Deployed` events. |
693
+ | `stageCount` | `number` | Declared stages. |
694
+ | `activityCount` | `number` | Activities across those stages. |
695
+ | `actionCount` | `number` | Actions across those activities. |
696
+ | `transitionCount` | `number` | Declared transitions. |
697
+ | `fieldCount` | `number` | Declared field entries at workflow, stage, and activity scope. |
698
+ | `activityKinds` | `string[]` | Distinct effective activity kinds, sorted: `user`, `service`, `script`, `manual`, `receive`. |
699
+ | `fieldKinds` | `string[]` | Distinct declared field kinds, sorted (engine field kinds such as `string`, `subject`, `assignees`). |
700
+ | `guardCount` | `number` | Guards declared on stages. |
701
+ | `effectCount` | `number` | Effects declared on actions. |
702
+ | `subworkflowCount` | `number` | Actions that carry a `spawn` block. |
703
+ | `lifecycle` | `string` | `'standalone'` or `'child'`. |
704
+
705
+ #### `Workflows CLI Definition Sharing Decided` (version 2)
706
+
707
+ One event per `deploy` that created at least one new definition version,
708
+ whether or not anything was donated. Measures opt-out rate without carrying
709
+ the document.
710
+
711
+ | Field | Type | What is collected |
712
+ | ----------------- | --------- | ----------------------------------------------------------------------------------------------------------------------------- |
713
+ | `decision` | `string` | `'opt-in'` (`--share-defs`), `'opt-out'` (`--no-share-defs`), or `'default'` (neither flag). |
714
+ | `shared` | `boolean` | Whether a donation POST reached the endpoint. `false` for any non-sharing decision, and for a chosen share whose POST failed. |
715
+ | `definitionCount` | `number` | Newly created definition versions this invocation could have donated. |
716
+
717
+ ### Engine events the CLI records
718
+
719
+ Write commands pass the CLI logger into the engine, so a successful operation
720
+ that writes directly also records the engine's adoption events on the same
721
+ session. A `start`, `fire-action`, or `abort` sent to the project's Workflows
722
+ service records none here, and `createWorkflowService` records no product
723
+ telemetry for it either. Read commands
724
+ (`list`, `show`, `diagnose`, `tail`, `definitions list` / `show` / `diff`) do
725
+ not emit engine events. `nuke` deletes engine-owned documents outside those
726
+ verbs and does not emit them either.
727
+
728
+ Every engine event includes:
729
+
730
+ | Field | Type | What is collected |
731
+ | ------------------ | -------- | --------------------------------------------------------------------------------------- |
732
+ | `executionRuntime` | `string` | The JavaScript runtime the engine detected around itself. Always `'node'` for this CLI. |
733
+ | `executionKind` | `string` | The host the engine was told it is running in. Always `'cli'` for this CLI. |
734
+
735
+ Every instance-scoped engine event additionally includes:
736
+
737
+ | Field | Type | What is collected |
738
+ | ----------------------- | -------- | -------------------------------------------------------------------------------------------------- |
739
+ | `definitionContentHash` | `string` | Content fingerprint of the pinned definition. Omitted when the definition predates fingerprinting. |
740
+ | `instanceId` | `string` | Instance document `_id`. |
741
+
742
+ | Event | Version | When the CLI emits it | Additional payload |
743
+ | -------------------------------------- | ------- | ---------------------------------------------------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
744
+ | `Workflows Engine Definition Deployed` | 2 | `deploy` for each definition (created or unchanged) | Same structural counts as `Workflows CLI Definition Shared`, plus `status`: `'created'` or `'unchanged'`. |
745
+ | `Workflows Engine Instance Started` | 2 | `start`, and a `fire-action` that spawns a child | `initialFieldCount` (number of caller-supplied initial fields), `viaSpawn` (`true` only for a spawned child), `lifecycle`. |
746
+ | `Workflows Engine Stage Transitioned` | 2 | A committed hop from `fire-action`, `start`, or `reset-activity` | `fromStageIndex`, `toStageIndex` (indexes into `stages[]`, never names), `toIsTerminal`, `isRevisit`, `dwellMs` (milliseconds in the exited stage, omitted if that entry is missing), `via`: `'transition'`. Unsampled. |
747
+ | `Workflows Engine Action Fired` | 2 | `fire-action` | `activityKind` (optional; derived kind of the fired activity), `hasParams` (whether params were supplied — not the params), `cascaded` (auto-transitions in the post-action cascade). |
748
+ | `Workflows Engine Instance Aborted` | 2 | `abort` | `changed`: `false` if the instance was already terminal. |
749
+ | `Workflows Engine Activity Reset` | 2 | `reset-activity` | `changed`: `false` if already at the target status, or the instance was terminal. |
750
+ | `Workflows Engine Definition Deleted` | 2 | `definitions delete` | `deletedVersionCount`, `cascadeAbortedCount`, `deletedGuardCount` (guards only when the last version goes). |
751
+ | `Workflows Engine Effect Completed` | 2 | `abort` cancelling a pending effect | `effect` (author-chosen name), `status` (`'done'`, `'failed'`, or `'cancelled'`), `origin` (`'action'`), `cascaded`. |
752
+
753
+ The engine also defines `Workflows Engine Field Edited`,
754
+ `Workflows Engine Instance Ticked`, `Workflows Engine Effects Drained`, and
755
+ `Workflows Engine Effect State Reported`. This CLI does not call those verbs,
756
+ so it does not record those events. Field-edit and tick events are sampled at
757
+ most once per 60 seconds when some other shell does emit them.
758
+
759
+ ## Definition sharing
760
+
761
+ Separate from telemetry, a `deploy` that creates new definition versions
762
+ donates those documents to Sanity — verbatim (structure, names, titles, GROQ
763
+ filters, effect configuration, seeded values), plus their deployment
764
+ coordinates (project and dataset, or resource id) — never content documents,
765
+ instances, or your auth token. The documents go to Sanity's first-party
766
+ definition-feedback endpoint, not the telemetry pipeline; telemetry carries
767
+ only a content-free marker (content hash plus structural counts) per shared
768
+ definition and a per-invocation decision event.
769
+
770
+ Sharing is **opt-out in every environment**. Pass `--no-share-defs` to decline
771
+ for one invocation. With neither flag, the first interactive deploy prints a
772
+ full notice before sending and remembers that the notice was shown; every later
773
+ flagless deploy prints a status block showing that sharing is on by default and
774
+ showing runnable shell examples for the default, explicit opt-in, and opt-out
775
+ forms; the explicit flags suppress the status block. Unattended runs (CI,
776
+ `DO_NOT_TRACK`, or a non-TTY pipe) also share by default and print that status
777
+ block in their logs.
778
+ A flagless deploy or explicit `--share-defs` forces that deploy's telemetry
779
+ through the environment gate so donated definitions retain their content-free
780
+ markers; account-level `sanity telemetry disable` still applies. A failed share
781
+ only warns, never failing a completed deploy.
782
+
783
+ ## Known gaps
784
+
785
+ - **Not listed in `sanity --help` yet.** Every command runs as
786
+ `sanity workflows <command>`, but the Sanity CLI's help output does not list
787
+ the `workflows` topic. Run `npx @sanity/workflows-cli --help` to see the
788
+ commands.
789
+
790
+ ## Identity
791
+
792
+ Identity is the token behind the configured client: the engine resolves the
793
+ acting user (and their roles) from `/users/me`, so every write is attributed
794
+ to whoever the token authenticates — there is no identity injection and no
795
+ `--as` impersonation. Run `whoami` to see that user: its Sanity user ID is the
796
+ value user assignee fields take. `whoami` asks about one resource; when the
797
+ config names several, pass `--deployment` or `--project-id` with `--dataset`.
798
+ Alongside that "who", the CLI declares an advisory "via what": every verb carries
799
+ `executionContext: { kind: "cli", id: "workflow-cli" }`, stamped on the history
800
+ entries of the writes it commits directly. A write it sends to the project's
801
+ Workflows service is stamped `{ kind: "server", id: "workflows-service" }`.
802
+
803
+ ## Tech stack
804
+
805
+ Mirrors the Sanity CLI's stack so the eventual plugin port is small:
806
+
807
+ - [`@oclif/core`](https://oclif.io) for command definitions, flags,
808
+ help.
809
+ - `@oclif/plugin-help` for help output.
810
+ - `ora`, `boxen`, `log-symbols`, `console-table-printer` for terminal UX;
811
+ color comes from `node:util`'s `styleText` (no separate color dep).
812
+ - `@sanity/cli-core` for timestamp formatting (absolute for detail views, relative
813
+ "… ago" for list tables).
814
+ - `diff` for the colored JSON unified diff in `deploy --dry-run`.