Skip to content

Commit 57b358f

Browse files
authored
fix(cli): pick a trigger in workspace apply --run and skip deployment reads of new workflows (#10539)
* fix(cli): pick a trigger in workspace apply --run and skip deployment reads of new workflows - --run <key>.<trigger>=<input> enters a run through a named trigger (const, block key or id); a key may repeat once per trigger - a multi-trigger refusal is rewritten in --run terms and next names a trigger instead of repeating the refused value; no doubled period - created workflows with no deployments/ change report not deployed without a deployment GET - docs, primer and loop help name the trigger form and the untested-draft gate * fix(cli): retry a refused bare --run without an empty input, once per trigger, and name every trigger form * test(cli): pin the deployment read of a workflow whose folder rename moved its key * fix(cli): refuse two --run names of one trigger once the saved lock resolves them
1 parent 65f07c0 commit 57b358f

9 files changed

Lines changed: 377 additions & 74 deletions

File tree

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10145,7 +10145,7 @@ sim workspaces source apply [options]
1014510145
| `--delete-target-only` | No | With --to and --allow-delete, also delete the target's resources this directory never declared and no promotion from it left there (prod-only tables and their rows, skills made in prod). Without it a promotion keeps them, naming each with a warning. |
1014610146
| `--only <kinds>` | No | Apply only these kinds (comma-separated folders: secrets.ts, credentials.ts, tools, skills, mcp, tables, knowledge, files, workflows, deployments). |
1014710147
| `--to <workspaceId>` | No | Promote the directory to another workspace (dev to prod): its resources bind by key to that workspace's own and are updated in place. Only .sim/targets/&lt;workspaceId&gt;.json changes, the record the next promotion compares with: a resource changed there since is kept, or refused as stale (exit 5) when the directory changed it too. |
10148-
| `--run <key=input>` | No | After the apply, dry-run workflows/&lt;key&gt;/'s saved draft with this input (JSON or @file, as `sim workflows run --dry --input` takes it; `--run <key>` alone: no input). Repeat it to test more workflows. deployments/ is applied only after every run completed, and only when every draft it publishes was test-run; a run that does not complete exits 1 and leaves the applied drafts in place. Sandboxed code (Python, shell, imports) really runs in it, network requests included, and is listed in runs[].sideEffects.unchecked. |
10148+
| `--run <key[.trigger]=input>` | No | After the apply, dry-run workflows/&lt;key&gt;/'s saved draft with this input (JSON or @file, as `sim workflows run --dry --input` takes it; `--run <key>` alone: no input). In a workflow with several triggers, name the one the run enters through after a period (its const, block key or id): `--run 'orders.shopWebhook={…}'`. Repeat it to test more workflows, or more triggers of one. deployments/ is applied only after every run completed, and only when every draft it publishes was test-run; a run that does not complete exits 1 and leaves the applied drafts in place. Sandboxed code (Python, shell, imports) really runs in it, network requests included, and is listed in runs[].sideEffects.unchecked. |
1014910149
| `--json` | No | Print machine-readable JSON (same as --output json). |
1015010150

1015110151
</CommandTable>

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -760,7 +760,7 @@ sim workspaces source apply [options]
760760
| `--delete-target-only` | No | With --to and --allow-delete, also delete the target's resources this directory never declared and no promotion from it left there (prod-only tables and their rows, skills made in prod). Without it a promotion keeps them, naming each with a warning. |
761761
| `--only <kinds>` | No | Apply only these kinds (comma-separated folders: secrets.ts, credentials.ts, tools, skills, mcp, tables, knowledge, files, workflows, deployments). |
762762
| `--to <workspaceId>` | No | Promote the directory to another workspace (dev to prod): its resources bind by key to that workspace's own and are updated in place. Only .sim/targets/&lt;workspaceId&gt;.json changes, the record the next promotion compares with: a resource changed there since is kept, or refused as stale (exit 5) when the directory changed it too. |
763-
| `--run <key=input>` | No | After the apply, dry-run workflows/&lt;key&gt;/'s saved draft with this input (JSON or @file, as `sim workflows run --dry --input` takes it; `--run <key>` alone: no input). Repeat it to test more workflows. deployments/ is applied only after every run completed, and only when every draft it publishes was test-run; a run that does not complete exits 1 and leaves the applied drafts in place. Sandboxed code (Python, shell, imports) really runs in it, network requests included, and is listed in runs[].sideEffects.unchecked. |
763+
| `--run <key[.trigger]=input>` | No | After the apply, dry-run workflows/&lt;key&gt;/'s saved draft with this input (JSON or @file, as `sim workflows run --dry --input` takes it; `--run <key>` alone: no input). In a workflow with several triggers, name the one the run enters through after a period (its const, block key or id): `--run 'orders.shopWebhook={…}'`. Repeat it to test more workflows, or more triggers of one. deployments/ is applied only after every run completed, and only when every draft it publishes was test-run; a run that does not complete exits 1 and leaves the applied drafts in place. Sandboxed code (Python, shell, imports) really runs in it, network requests included, and is listed in runs[].sideEffects.unchecked. |
764764
| `--json` | No | Print machine-readable JSON (same as --output json). |
765765

766766
</CommandTable>

‎apps/docs/content/docs/workflows/code/workspace.mdx‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,9 @@ The dry run prints the plan (`+` create, `~` update, `=` adopt an existing resou
5656
sim workspaces source check --dir ./my-workspace && sim workspaces source apply --dir ./my-workspace --yes --run lead-intake='{"email":"a@b.co"}'
5757
```
5858

59-
`--run <key>='<input>'` dry-runs the saved draft of `workflows/<key>/` with that input after the apply, as `sim workflows run <workflowId> --dry --input` does: writes return placeholders and are listed under each run's `sideEffects`. The input is JSON or `@file`; repeat `--run` to test several workflows. The result lists each run under `runs` with its workflow key and id, status and output. A run that does not complete exits `1` and prints the failing block's file and line in `workflows/<key>/`; the applied drafts stay in place, so fix the source and run the same command again.
59+
`--run <key>='<input>'` dry-runs the saved draft of `workflows/<key>/` with that input after the apply, as `sim workflows run <workflowId> --dry --input` does: writes return placeholders and are listed under each run's `sideEffects`. The input is JSON or `@file`; repeat `--run` to test several workflows. A workflow with more than one trigger (an API trigger and a webhook, say) needs the trigger each run enters through, named after a period by its const, block key or id: `--run two.apiIn='{…}' --run two.hook='{…}'` tests both paths in one apply. The result lists each run under `runs` with its workflow key and id, status and output. A run that does not complete exits `1` and prints the failing block's file and line in `workflows/<key>/`; the applied drafts stay in place, so fix the source and run the same command again.
6060

61-
With `--run`, `deployments/` is applied only after every run completed, in a second write, so a deployment with `live: 'draft'` publishes only a draft that passed its test. Without `--run` it is applied with everything else. `--run` is refused with `--dry` (nothing is written to test), with `--to` (test in this workspace, then promote), and with an `--only` that leaves out `workflows`.
61+
With `--run`, `deployments/` is applied only after every run completed, in a second write, so a deployment with `live: 'draft'` publishes only a draft that passed its test. It is also applied only when every draft it publishes was test-run in this apply: if it would publish a workflow no `--run` tested, `deployments/` is skipped, the apply exits non-zero, and its `next` adds the `--run` for each untested workflow. Without `--run` it is applied with everything else. `--run` is refused with `--dry` (nothing is written to test), with `--to` (test in this workspace, then promote), and with an `--only` that leaves out `workflows`.
6262

6363
An apply adopts a resource it never read (one made in Sim under the name a new declaration gives) only when the declaration matches it, or only adds to it: a column, rows into an empty table, a document. An adopt that would change or remove anything the resource holds is refused with `E_APPLY_NO_BASE`, even with `--allow-delete`. Run `get` into a new directory to read the resource, then make your edit there, or give the declaration another name.
6464

‎packages/sim-cli/src/commands/protocol/source-loop-help.ts‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -105,9 +105,10 @@ and writes the workspace; --run <key>='{…}' (one per workflow, <key> its workf
105105
dry-runs that input on the saved draft: code runs and GET fetches are sent (point them at test
106106
resources); writes are stubbed and listed under runs[].sideEffects, whose .unchecked lists
107107
sandboxed code that really ran, network requests included, and .omitted counts entries left
108-
out. Use --run when the apply changes a workflow. A workflow deploys on apply when
109-
deployments/<key>.ts declares it; with --run, deployments/ is applied only after every run
110-
completed.
108+
out. A workflow with several triggers takes one --run '<key>.<trigger>={…}' per trigger (its
109+
const, block key or id). Use --run when the apply changes a workflow. A workflow deploys on
110+
apply when deployments/<key>.ts declares it; with --run, deployments/ is applied only after
111+
every run completed, and only when every draft it publishes was test-run.
111112
--allow-delete lets an edit remove a resource, column, rows, document, or a workflow's block or
112113
variable; apply --dry only prints the plan; get --force replaces edits no apply has saved.
113114
Promote dev to prod: \`sim workspaces source apply --dir ./ws --to <workspaceId> --dry\`, then --yes.

‎packages/sim-cli/src/commands/protocol/source-primer.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ const WORKSPACE_PRIMER = `# Sim workspace source: primer
127127
128128
A Sim workspace as files; nothing here runs. Commands here (table ones too) act on the workspace \`${SOURCE_BASE_FILE}\` names. Every \`.ts\` file starts with \`// @sim-sdk ${SDK_MAJOR}\`.
129129
130-
**Edit loop**: write every file, then run one chained command; fix what it reports: \`${WORKSPACE_LOOP}\` dry-runs \`workflows/<key>/\`'s draft (one \`--run\` per workflow you changed; drop it when you changed none) and deploys only if all complete. \`check\` also type-checks (no \`tsc\`). Apply re-checks (\`apply --dry\` only plans) and rewrites files: reread before editing.
130+
**Edit loop**: write every file, then run one chained command; fix what it reports: \`${WORKSPACE_LOOP}\` dry-runs \`workflows/<key>/\`'s draft (one \`--run\` per workflow you changed; drop it when you changed none; several triggers: \`--run '<key>.<const>={…}'\` each) and deploys only if all complete and every draft it deploys was tested. \`check\` also type-checks (no \`tsc\`). Apply re-checks (\`apply --dry\` only plans) and rewrites files: reread before editing.
131131
132132
- **Deletes**: removing anything (file, column, block) needs \`apply --allow-delete\` and is permanent.
133133
- **Exit ${SOURCE_HELD_EXIT_CODE}**: partly written: what needs an unset secret or unbound credential waits; set it, reapply.

‎packages/sim-cli/src/commands/protocol/workflow-test-runs.ts‎

Lines changed: 33 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,16 @@ export interface RunRequest {
2121
/** A `--run` test as a write's result shows it: the trigger it named, then the run. */
2222
export type NamedTestRun = TestRun & { trigger?: string }
2323

24+
/**
25+
* A run's `--run` value as a command writes it, from its `raw` and the trigger it names: a
26+
* workspace apply puts the workflow's key in front.
27+
*/
28+
type RunFlag = (run: Pick<RunRequest, 'raw' | 'trigger'>) => string
29+
2430
/** A `--run` run in turn: its result, why it did not complete, and the `--run` to repeat next. */
2531
export interface RunOutcome extends DraftTest {
2632
run: NamedTestRun
27-
/** The `--run` value the next command repeats: a trigger the refusal pointed at, else `raw`. */
33+
/** The `--run` the next command repeats: a trigger the refusal pointed at, else `raw` (as `flag` writes it). */
2834
next: string
2935
}
3036

@@ -107,6 +113,14 @@ function triggerId(blocks: readonly LockBlock[], name: string): string {
107113
return match?.id ?? name
108114
}
109115

116+
/**
117+
* The id of the trigger block `name` (its const, block key or id) is in the saved `lock`, so two
118+
* names of one block compare equal; a name the lock does not hold comes back as written.
119+
*/
120+
export function lockTriggerId(lock: unknown, name: string): string {
121+
return triggerId(lockBlocks(lock), name)
122+
}
123+
110124
/** The consts of the triggers a refusal lists by id, in its order, but the block `refusedId`. */
111125
function listedTriggers(
112126
message: string,
@@ -128,22 +142,29 @@ function triggerRefusal(
128142
request: RunRequest,
129143
message: string,
130144
blocks: readonly LockBlock[],
131-
refusedId: string | undefined
145+
refusedId: string | undefined,
146+
flag: RunFlag
132147
): { failure: string; next: string } | undefined {
133148
const triggers = listedTriggers(message, blocks, refusedId)
134149
if (triggers.length === 0) return undefined
135150
if (request.trigger === undefined && SEVERAL_TRIGGERS.test(message)) {
136151
const [first, ...others] = triggers
137-
const next = `${first}=${request.raw}`
152+
const next = flag({
153+
trigger: first,
154+
raw: request.raw === '' ? first : `${first}=${request.raw}`,
155+
})
138156
return {
139-
failure: `the workflow has more than one trigger (${triggers.join(', ')}), so name the one each --run enters through: --run ${shellWord(next)}${others.length > 0 ? ` (or ${others.map((other) => `${other}=<its input>`).join(', ')})` : ''}`,
157+
failure: `the workflow has more than one trigger (${triggers.join(', ')}), so name the one each --run enters through: --run ${shellWord(next)}${others.length > 0 ? ` (or ${others.map((other) => flag({ trigger: other, raw: `${other}=<its input>` })).join(', ')})` : ''}`,
140158
next,
141159
}
142160
}
143161
if (request.trigger !== undefined && NOT_A_TRIGGER.test(message)) {
144162
return {
145163
failure: `${request.trigger} is not a trigger of this workflow; name one of ${triggers.join(', ')} before = in --run`,
146-
next: `${triggers[0]}${request.raw.slice(request.trigger.length)}`,
164+
next: flag({
165+
trigger: triggers[0],
166+
raw: `${triggers[0]}${request.raw.slice(request.trigger.length)}`,
167+
}),
147168
}
148169
}
149170
return undefined
@@ -163,9 +184,12 @@ export async function runTests(
163184
workspaceId: string
164185
lock: unknown
165186
runs: readonly RunRequest[]
166-
onRun: (run: NamedTestRun) => void
187+
onRun?: (run: NamedTestRun) => void
188+
/** How this command writes a run's `--run` value; unset: its `raw`. */
189+
flag?: RunFlag
167190
}
168191
): Promise<RunOutcome[]> {
192+
const flag = args.flag ?? ((run: Pick<RunRequest, 'raw'>) => run.raw)
169193
const blocks = lockBlocks(args.lock)
170194
const outcomes: RunOutcome[] = []
171195
for (const request of args.runs) {
@@ -180,7 +204,7 @@ export async function runTests(
180204
)
181205
const named = request.trigger === undefined ? {} : { trigger: request.trigger }
182206
const refusal = test.refused
183-
? triggerRefusal(request, String(test.run.error), blocks, entryId)
207+
? triggerRefusal(request, String(test.run.error), blocks, entryId, flag)
184208
: undefined
185209
const outcome: RunOutcome = refusal
186210
? {
@@ -189,9 +213,9 @@ export async function runTests(
189213
failure: `the server refused it before it started: ${refusal.failure}`,
190214
next: refusal.next,
191215
}
192-
: { ...test, run: { ...named, ...test.run }, next: request.raw }
216+
: { ...test, run: { ...named, ...test.run }, next: flag(request) }
193217
outcomes.push(outcome)
194-
args.onRun(outcome.run)
218+
args.onRun?.(outcome.run)
195219
}
196220
return outcomes
197221
}

0 commit comments

Comments
 (0)