Explain waiting conditions and field insights
Use condition insights and field insights on WorkflowEvaluation to explain blocked actions, waiting transitions, and missing references.
Early access
Workflows is in early access, built in public. Read How early access works before you rely on it.
When a workflow waits, its evaluation can explain why. Each decision in a WorkflowEvaluation carries a condition insight that names the unmet parts of the condition and the field values that would change it.
This guide gives the quick start’s article-review workflow a field and a condition to wait on, then writes a script that explains why to-approved is waiting and which field value would let it fire.
You need:
- The finished quick start:
article-reviewdeployed to thedevpartition, and a published article. - The
scripts/engine.tsmodule from Run the engine from your own code, and the API token it reads fromSANITY_AUTH_TOKEN.
Explain a waiting transition
In article-review, to-approved fires as soon as sign-off is done, so there is little to explain. Give it something to wait on: in workflows/article-review.ts, add a legal approval field to the workflow and make the transition require it:
export const articleReview = defineWorkflow({
// ...
fields: [
defineField({
type: 'subject',
name: 'subject',
// ...
}),
defineField({type: 'boolean', name: 'legalApproved', title: 'Legal approved', editable: true}),
],
stages: [
// drafting is unchanged
defineStage({
name: 'review',
// ...
transitions: [
defineTransition({
name: 'to-approved',
title: 'Approve and finish',
to: 'approved',
when: '$allActivitiesDone && $fields.legalApproved == true',
}),
],
}),
// approved is unchanged
],
})editable: true lets a user set the field while the instance runs. A transition without when waits for $allActivitiesDone. This when keeps that check and adds the field. Fields and Conditions cover both in depth.
Deploy the change, then start a new instance and submit it so it reaches review. Instances keep the definition version they started with, so the quick start’s instance doesn’t have the new field:
npx sanity-workflows deploy
npx sanity-workflows start article-review \
--field subject='{"id":"dataset:PROJECT_ID:DATASET_NAME:DOCUMENT_ID","type":"article"}'
npx sanity-workflows fire-action INSTANCE_ID --activity write --action submitpnpm dlx sanity-workflows deploy
pnpm dlx sanity-workflows start article-review \
--field subject='{"id":"dataset:PROJECT_ID:DATASET_NAME:DOCUMENT_ID","type":"article"}'
pnpm dlx sanity-workflows fire-action INSTANCE_ID --activity write --action submityarn dlx sanity-workflows deploy
yarn dlx sanity-workflows start article-review \
--field subject='{"id":"dataset:PROJECT_ID:DATASET_NAME:DOCUMENT_ID","type":"article"}'
yarn dlx sanity-workflows fire-action INSTANCE_ID --activity write --action submitbunx sanity-workflows deploy
bunx sanity-workflows start article-review \
--field subject='{"id":"dataset:PROJECT_ID:DATASET_NAME:DOCUMENT_ID","type":"article"}'
bunx sanity-workflows fire-action INSTANCE_ID --activity write --action submitCreate scripts/explain-transition.ts. It takes a WorkflowEvaluation and reads both the transition’s condition insight and the field proposals:
import type {ConditionOutcome, WorkflowEvaluation} from '@sanity/workflow-engine'
interface TransitionChange {
field: string
value: unknown
before: ConditionOutcome
after: ConditionOutcome
}
interface TransitionExplanation {
outcome: ConditionOutcome
blockedBy: string[]
changes: TransitionChange[]
}
export function explainTransition(
evaluation: WorkflowEvaluation,
transitionName: string,
): TransitionExplanation {
const evaluated = evaluation.currentStage.transitions.find(
({transition}) => transition.name === transitionName,
)
if (!evaluated) throw new Error(`Unknown transition: ${transitionName}`)
const changes = evaluation.fieldInsights.flatMap((field) =>
field.proposals.flatMap((proposal) =>
proposal.consequences
.filter(
({site}) => site.kind === 'transition' && site.transition === transitionName,
)
.map(({before, after}) => ({
field: field.field,
value: proposal.assign.value,
before,
after,
})),
),
)
return {
outcome: evaluated.insight.outcome,
blockedBy: evaluated.insight.blockedBy.map(({atom}) => atom.groq),
changes,
}
}Create scripts/why-waiting.ts. It imports engine from the module you made in the engine guide, evaluates the instance, and explains to-approved:
import {engine} from './engine'
import {explainTransition} from './explain-transition'
async function main() {
const instanceId = process.argv[2]
if (!instanceId) {
throw new Error('Pass the instance _id: npx tsx scripts/why-waiting.ts INSTANCE_ID')
}
const evaluation = await engine.evaluate({instanceId})
console.log(`Stage: ${evaluation.currentStage.stage.name}`)
console.dir(explainTransition(evaluation, 'to-approved'), {depth: null})
}
main().catch((error) => {
console.error(error)
process.exit(1)
})Run it with the instance id:
SANITY_AUTH_TOKEN=YOUR_TOKEN npx tsx scripts/why-waiting.ts INSTANCE_IDBoth parts of the condition are false, so both appear in blockedBy:
Stage: review
{
outcome: 'unsatisfied',
blockedBy: [ '$allActivitiesDone', '$fields.legalApproved == true' ],
changes: []
}Each blockedBy entry is an atom: the smallest part of the condition, as GROQ, that currently blocks it. changes is empty because no single field value would satisfy the condition while sign-off is still open. Each changes entry names a proposed field value and the transition outcome before and after that value. These are verified condition consequences, not a prediction of document writes or external effects. A custom App SDK interface can pass session.evaluation to the same helper.
Resolve sign-off and run the script again:
npx sanity-workflows fire-action INSTANCE_ID --activity sign-off --action approve
SANITY_AUTH_TOKEN=YOUR_TOKEN npx tsx scripts/why-waiting.ts INSTANCE_IDpnpm dlx sanity-workflows fire-action INSTANCE_ID --activity sign-off --action approve
SANITY_AUTH_TOKEN=YOUR_TOKEN npx tsx scripts/why-waiting.ts INSTANCE_IDyarn dlx sanity-workflows fire-action INSTANCE_ID --activity sign-off --action approve
SANITY_AUTH_TOKEN=YOUR_TOKEN npx tsx scripts/why-waiting.ts INSTANCE_IDbunx sanity-workflows fire-action INSTANCE_ID --activity sign-off --action approve
SANITY_AUTH_TOKEN=YOUR_TOKEN npx tsx scripts/why-waiting.ts INSTANCE_IDThe instance stays in review. Only the field blocks the transition now, and the engine proposes the value that would satisfy it:
Stage: review
{
outcome: 'unsatisfied',
blockedBy: [ '$fields.legalApproved == true' ],
changes: [
{
field: 'legalApproved',
value: true,
before: 'unsatisfied',
after: 'satisfied'
}
]
}blockedBy lists the unmet atoms in source order, and each atom also has a pivotal flag. pivotal is true when satisfying that atom alone, with the others unchanged, would satisfy the condition. In the first run neither atom was pivotal. Now $fields.legalApproved == true is, so an interface can tell the reviewer that this one value unblocks the transition.
Writing that value with engine.editField({instanceId, target: {field: 'legalApproved'}, value: true}) fires to-approved in the same call. npx sanity-workflows show INSTANCE_ID then reports the instance in approved.
The outcome has three possible values:
- satisfied: the condition is true.
- unsatisfied: the condition is false.
- unevaluable: GROQ returned null because the condition could not be decided from the current values.
Read field insights
WorkflowEvaluation.fieldInsights provides the field-centric view of the same current-stage conditions. Use it to show where a field matters and which concrete assignments the engine verified would change an outcome.
A field can take part in several conditions without producing a useful proposal. A proposal appears only when a blocking atom points to one concrete value, and re-evaluating with that value confirms it changes at least one of the sites involved.
Every insight location, missing-document evidence, and the exported insight types are listed in Evaluation insights and diagnostic utilities.
Next steps
Use The reactive session to keep the evaluation current in a custom interface.
See Build a workflow interface with the App SDK to render an evaluation in a custom interface.
See Engine, effects, and insights reference for the complete WorkflowEvaluation projection.
Read why insights and verdicts are advisory in Advisory checks and Content Lake enforcement.
Visiting agent?
Workflows includes an MCP server for inspecting, operating, authoring, validating, and deploying workflows. Ask your human to set up the MCP server.
Was this page helpful?
Feedback submitted, thanks for helping us improve the docs. Have you tried these alternatives?