Skip to content

Commit 67732b3

Browse files
committed
fix(workflow-code): review round 2 for apply --run and get by name
- get <name> --dir looks the name up in the workspace the directory was read from. - A starter's next keeps --run and --deploy. - Guide: flags that imply the draft, and sideEffects.omitted before trusting a list. - Drop the test that compared two help strings.
1 parent b98ac6b commit 67732b3

5 files changed

Lines changed: 39 additions & 16 deletions

File tree

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

Lines changed: 0 additions & 11 deletions
This file was deleted.

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,7 @@ const ADVANCED_GUIDE = `# Sim workflow source: advanced
250250

251251
## Edit loop in detail
252252

253-
Batch it: write every file first, then run steps 2 and 4 (with a test run and the deploy) as one chained command, \`sim workflows source check --dir . && sim workflows source apply --dir . --yes --run '{…}' --deploy\`, and fix what it reports in the next step. Each non-zero exit stops the chain. Step 3 (with its \`apply --dry\`, which only plans) is for a person, CI, or a change you want reviewed first. The test run (\`--run\`, or \`sim workflows run <workflowId> --dry --input '{…}'\`) is another thing than \`apply --dry\`: it executes the saved draft, while a run without \`--dry\` or \`--draft\` runs the active deployment (\`--deployment-version <n>\`: that deployed version). A dry run answers every tool call not declared read (sends, table writes, HTTP requests other than GET, HEAD or OPTIONS, custom and MCP tools) with a placeholder and lists it under \`sideEffects.stubbed\` with the request it would have sent; check those payloads instead of reading back or cleaning up data. Placeholders are distinctive (\`dry_run_row_1\`, numbers above 900000000), and a read that would send one (a tool read, an API GET, a Function's fetch, its headers included) is not sent either and is listed with \`reason: 'placeholder'\`. An MCP tool is stubbed even when its server declares it read-only, which Sim cannot verify: that entry has \`reason: 'mcp-read-only'\`, and \`--allow <block>\` sends it. Code (a Function block's, a Condition's expressions, a While or Do While loop's condition, code an Agent's model writes) runs: its GET, HEAD and OPTIONS fetches are sent (an API that writes on a GET is not stubbed, so point those at a test resource), and any other fetch, or one carrying a method-override header, gets an empty 200 and is listed under \`sideEffects.stubbed\`; code in a remote sandbox (shell, Python, imports) is listed under \`sideEffects.unchecked\`, so check that list too. Child workflows run dry too, and custom, Human in the loop and Wait blocks are answered whole. \`--mock <block>='{…}'\` answers a block with fixed output to steer a branch; a bare \`--mock <block>\` with \`--mock-from <runId>\` replays that block's output from the recorded run (this workflow's blocks, not a child's; not inside a loop or parallel unless that run mocked it). \`--allow <block>\` sends that block's writes for real (a Function's fetches included) and \`--draft\` runs the draft with every write sent, so point both at test resources. \`--allow\` and \`--mock\` name a child workflow's block as \`<workflow>.<block>\`.
253+
Batch it: write every file first, then run steps 2 and 4 (with a test run and the deploy) as one chained command, \`sim workflows source check --dir . && sim workflows source apply --dir . --yes --run '{…}' --deploy\`, and fix what it reports in the next step. Each non-zero exit stops the chain. Step 3 (with its \`apply --dry\`, which only plans) is for a person, CI, or a change you want reviewed first. The test run (\`--run\`, or \`sim workflows run <workflowId> --dry --input '{…}'\`) is another thing than \`apply --dry\`: it executes the saved draft, while a run without \`--dry\`, \`--draft\` or a flag that implies the draft (\`--trigger\`, \`--sample\`, \`--stop-after\`, \`--from-block\`) runs the active deployment (\`--deployment-version <n>\`: that deployed version). A dry run answers every tool call not declared read (sends, table writes, HTTP requests other than GET, HEAD or OPTIONS, custom and MCP tools) with a placeholder and lists it under \`sideEffects.stubbed\` with the request it would have sent (each list keeps a bounded number of entries and counts the rest in \`sideEffects.omitted\`, so check it before treating a list as complete); check those payloads instead of reading back or cleaning up data. Placeholders are distinctive (\`dry_run_row_1\`, numbers above 900000000), and a read that would send one (a tool read, an API GET, a Function's fetch, its headers included) is not sent either and is listed with \`reason: 'placeholder'\`. An MCP tool is stubbed even when its server declares it read-only, which Sim cannot verify: that entry has \`reason: 'mcp-read-only'\`, and \`--allow <block>\` sends it. Code (a Function block's, a Condition's expressions, a While or Do While loop's condition, code an Agent's model writes) runs: its GET, HEAD and OPTIONS fetches are sent (an API that writes on a GET is not stubbed, so point those at a test resource), and any other fetch, or one carrying a method-override header, gets an empty 200 and is listed under \`sideEffects.stubbed\`; code in a remote sandbox (shell, Python, imports) is listed under \`sideEffects.unchecked\`, so check that list too. Child workflows run dry too, and custom, Human in the loop and Wait blocks are answered whole. \`--mock <block>='{…}'\` answers a block with fixed output to steer a branch; a bare \`--mock <block>\` with \`--mock-from <runId>\` replays that block's output from the recorded run (this workflow's blocks, not a child's; not inside a loop or parallel unless that run mocked it). \`--allow <block>\` sends that block's writes for real (a Function's fetches included) and \`--draft\` runs the draft with every write sent, so point both at test resources. \`--allow\` and \`--mock\` name a child workflow's block as \`<workflow>.<block>\`.
254254

255255
1. Edit \`${SOURCE_ENTRY_FILE}\` (and \`functions/*\`). \`${SOURCE_EXAMPLES_DIR}/\` holds correct examples to copy from: \`cookbook.ts\` (agent with structured output, condition, function, table query and upsert, knowledge search, Slack, human in the loop, loop, response), \`integrations.ts\` (webhook trigger, weekday cron schedule, HTTP request with headers, table update by row id, Gmail, an error path) and \`agents.ts\` (router, agent tools: custom tool, MCP tool, integration operation, child workflow; run a child workflow; parallel). Their resource keys are examples; this workspace's are in \`${WORKSPACE_TYPES_FILE}\`.
256256
2. \`sim workflows source check --dir .\`: compiles with the server's compiler, in milliseconds and without a request. It catches what \`tsc\` cannot (duplicate names, cycles, literal secrets), and fails (exit 1) on ${STRICT_FINDINGS_CLAUSE} in a block you added (\`--strict\`: in every block). It then adds the blocks the source now uses to \`tsconfig.json\` and type-checks with it, using the TypeScript release the declarations are built for (installed once into the CLI's cache, on the first check; \`E_TYPE\` findings). That catches what the compile and \`apply --dry\` do not, such as a reference to a start input the trigger does not declare. Offline before that install, it warns \`W_TYPES_UNCHECKED\` and passes on the compile alone; \`--no-types\` skips it. \`apply --dry\` is the authority: a type error on a printed line you did not edit that it accepts can be skipped with \`--no-types\`.

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -759,7 +759,7 @@ export function attachWorkflowSourceCreate(source: Command): void {
759759
const workspaceId = client.requireWorkspace()
760760
const tree = await writeSourceScaffold(client, options.dir, workspaceId)
761761
const next: NextStep = {
762-
command: `${createCommand(shellWord(options.dir), workspaceId)} --yes${summaryFileFlag(options.summaryFile)}`,
762+
command: `${createCommand(shellWord(options.dir), workspaceId)} --yes${runFlag(options.run)}${options.deploy ? ' --deploy' : ''}${summaryFileFlag(options.summaryFile)}`,
763763
reason: `${options.dir} had no ${ENTRY_FILE}, so a starter, the SDK types for workspace ${workspaceId}, tsconfig.json, .sim/AGENTS.md and the examples in .sim/examples/ were written and nothing was created. Edit ${ENTRY_FILE}, check it, then create it.`,
764764
}
765765
const machine = isMachine(profile.output)

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,25 @@ describe('sim workflows source get <name>', () => {
224224
)
225225
})
226226

227+
it("looks a name up in the workspace the directory was read from, not the profile's", async () => {
228+
mkdirSync(join(outDir, '.sim'), { recursive: true })
229+
writeFileSync(
230+
join(outDir, '.sim/base.json'),
231+
JSON.stringify({
232+
version: 1,
233+
workflowId: WORKFLOW_ID,
234+
workspaceId: WORKSPACE_ID,
235+
fingerprint: 'sf1:a',
236+
catalogHash: 'h',
237+
})
238+
)
239+
serve({ [LIST_PATH]: listed({ id: WORKFLOW_ID, name: 'Lead Scorer' }) })
240+
241+
expect(await getNamed('Lead Scorer')).toBeNull()
242+
243+
expect(callsTo(LIST_PATH)[0][1].query).toMatchObject({ workspaceId: WORKSPACE_ID })
244+
})
245+
227246
it.each([
228247
[
229248
'several workflows hold the name',

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

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,11 @@ import {
1212
absoluteDirOption,
1313
assertPrintedSourcePaths,
1414
assertSourceDirKind,
15+
directoryWorkspace,
1516
locallyEditedFiles,
1617
readPreviousLock,
1718
readSourceJson,
19+
recordedWorkspace,
1820
withSourceCommandLock,
1921
} from './source-dir'
2022
import { sourceExitCodesHelp } from './source-exit-codes'
@@ -67,10 +69,12 @@ const NAMED_CANDIDATES = 10
6769
*/
6870
async function resolveWorkflow(
6971
client: OperationClient<'listWorkflows'>,
70-
workflow: string
72+
workflow: string,
73+
/** The workspace to look in when not the command's own: a directory's recorded one. */
74+
inWorkspace?: string
7175
): Promise<string> {
7276
if (isValidUuid(workflow)) return workflow
73-
const workspaceId = client.requireWorkspace()
77+
const workspaceId = client.requireWorkspace(inWorkspace)
7478
// Every page: an exact match or a second one can sort after the first page of substring hits.
7579
const found = await client.requestAllPages<ListWorkflowsResponse['data'][number]>(
7680
'listWorkflows',
@@ -148,7 +152,18 @@ export function attachWorkflowSourceGet(source: Command): void {
148152
const force = flags[FORCE_FLAG] === true
149153
await assertSourceDirKind(dir, 'workflow')
150154
const { client, profile } = connectGet()
151-
const workflowId = await resolveWorkflow(client, workflow)
155+
// A name is looked up in the workspace the directory was read from, as its other commands
156+
// act on it; --force gets into it from the command's own workspace instead.
157+
const recorded = force
158+
? null
159+
: directoryWorkspace(
160+
dir,
161+
await recordedWorkspace(dir, profile),
162+
profile,
163+
(_from, _named, setting) =>
164+
`Drop ${setting}, or pass --force to replace it with a workflow from ${setting}.`
165+
)
166+
const workflowId = await resolveWorkflow(client, workflow, recorded ?? undefined)
152167
if (!force) await assertSameWorkflow(dir, workflowId)
153168
const request = await buildRequest(
154169
'getWorkflowSource',

0 commit comments

Comments
 (0)