@sanity/workflows-cli 0.0.0-pr-744-20261109125055

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