Skip to content

Commit 4c65d95

Browse files
authored
improvement(workflow-code): recommend the batched edit loop in the agent primer, help and docs (#10338)
1 parent 67857b4 commit 4c65d95

15 files changed

Lines changed: 95 additions & 47 deletions

File tree

‎apps/docs/content/docs/workflows/code/coding-agents.mdx‎

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ Claude Code, Cursor, Codex and other coding agents work from a directory that `g
77

88
## What get gives an agent
99

10-
- A **primer**, which opens `.sim/AGENTS.md`. `get` prints a short summary instead (the files written, the guide, and the `editLoop` commands to run); `get --primer` also prints the primer (in JSON output, its `primer` field). It covers the edit loop, how block identity, references and renames work, failures and retries, test runs, deletes, deploy, exit codes, and the params of each builder the workflow already uses.
10+
- A **primer**, which opens `.sim/AGENTS.md`. `get` prints a short summary instead (the files written, the guide, and the `editLoop` command to run); `get --primer` also prints the primer (in JSON output, its `primer` field). It covers the edit loop, how block identity, references and renames work, failures and retries, test runs, deletes, deploy, exit codes, and the params of each builder the workflow already uses.
1111
- **`.sim/AGENTS.md`**: the primer followed by a reference, by task. A workspace directory also gets `.sim/GUIDE-advanced.md`.
1212
- **`.sim/examples/`**: one correct example of each common block to copy from.
1313
- **`.sim/sdk/`**: the type declarations, with `.sim/sdk/INDEX.md` listing the builders.
@@ -17,10 +17,14 @@ Point the agent at `.sim/AGENTS.md` before it edits.
1717
## The loop
1818

1919
1. `sim workflows source get <workflowId> --dir <dir>` writes the workflow and the guide.
20-
2. Edit `workflow.ts`.
21-
3. `sim workflows source check --dir <dir>` compiles it offline.
22-
4. `sim workflows source status --dir <dir>` shows what changed and what to run next.
23-
5. `sim workflows source apply --dir <dir> --dry-run`, then `--yes`, saves the draft.
20+
2. Write every file the change needs.
21+
3. Run one chained command, then fix whatever it reports in the next step:
22+
23+
```bash
24+
sim workflows source check --dir <dir> && sim workflows source apply --dir <dir> --yes && sim workflows run <workflowId> --manual --input '{...}'
25+
```
26+
27+
A non-zero exit code stops the chain at the first failure. `apply` compiles the source and compares it with the saved draft again, so an agent can skip `--dry-run`. People and CI use `apply --dry-run` to review the plan, and `status` shows what changed and what to run next. Add `bunx -p typescript@7.0.2 tsc -p <dir>` after `check` to catch type errors.
2428

2529
For a whole workspace, use `sim workspaces source` with the same steps; see [Manage a workspace as code](/workflows/code/workspace).
2630

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ workflows/support/workflow.ts a workflow directory
4949
deployments/support.ts how workflows/support is deployed
5050
```
5151

52-
A resource's key is its file or folder name, and a workflow names it by that key. What each kind's file holds:
52+
A resource's key is its file or folder name, and a workflow names it by that key. Every `.ts` file starts with the line `// @sim-sdk 1`, a deployment included; the one-line forms below show only the call. What each kind's file holds:
5353

5454
{/* workflow-code-docs:directory-kinds:begin — generated by `bun run generate:workflow-code-docs`; do not edit between markers */}
5555

‎apps/sim/lib/workspace-source/application/files.integration.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -642,6 +642,8 @@ describe('workspace source files', () => {
642642

643643
const refused = await refusal(apply(workspaceId, dropped, { base: read.base }))
644644
expect(codes(refused.diagnostics)).toEqual(['E_APPLY_DELETE_NOT_ALLOWED'])
645+
expect(refused.diagnostics[0].message).toContain('missing from the directory is a delete')
646+
expect(refused.diagnostics[0].message).toContain('--allow-delete')
645647
expect((await storedFiles(workspaceId)).map((file) => file.id)).toContain(id)
646648

647649
const deleted = await apply(workspaceId, dropped, { base: read.base, allowDelete: true })

‎apps/sim/lib/workspace-source/application/findings.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -463,12 +463,12 @@ export function planUndeclared<K extends WorkspaceKind, S>(
463463
kind,
464464
key,
465465
'E_APPLY_DELETE_NOT_ALLOWED',
466-
`${where(kind, key)} is no longer declared; deleting "${resource.name}" needs --allow-delete.`,
466+
`${where(kind, key)} is no longer declared: a resource missing from the directory is a delete, and deleting "${resource.name}" needs --allow-delete.`,
467467
{
468468
hint:
469469
kind === 'deployment'
470470
? 'Pass --allow-delete to delete it, or restore its file to keep it.'
471-
: 'Pass --allow-delete to delete it, or restore its file to keep it. Run with --allow-delete --dry-run to see which workflows use it.',
471+
: 'Pass --allow-delete to delete it, or restore its file or folder to keep it. Run with --allow-delete --dry-run to see which workflows use it.',
472472
}
473473
)
474474
: kindFinding(

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

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,14 @@ export const WORKFLOW_SOURCE_LOOP_HELP = `
3434
The loop:
3535
${loopRows([
3636
['sim workflows source get <workflowId> --dir ./wf', 'write workflow.ts, functions/* and .sim/'],
37-
['edit ./wf/workflow.ts', ''],
37+
['edit ./wf/workflow.ts', 'every file first, then the one command below'],
3838
['sim workflows source check --dir ./wf', 'offline: each error as file:line:column'],
39-
[typecheckCommand('./wf'), 'type-check the edit'],
40-
['sim workflows source apply --dir ./wf --dry-run', 'the plan; nothing is written'],
41-
['sim workflows source apply --dir ./wf --yes', 'save the draft'],
39+
[' && sim workflows source apply --dir ./wf --yes', 'save the draft (it re-checks)'],
40+
[' && sim workflows run <workflowId> --manual', 'test the saved draft'],
4241
])}
42+
Run those three as one chained command: a non-zero exit stops it at the first failure, and the next
43+
step fixes what it reported. For review, \`apply --dry-run\` prints the plan and writes nothing;
44+
\`${typecheckCommand('./wf')}\` adds type errors.
4345
A new workflow: \`sim workflows source create --dir ./new --workspace <workspaceId>\` writes a
4446
starter that records its workspace; edit and check it, then \`create --dir ./new --yes\` creates the
4547
workflow there, and the loop above goes on from there.
@@ -56,12 +58,13 @@ export const WORKSPACE_SOURCE_LOOP_HELP = `
5658
The loop:
5759
${loopRows([
5860
['sim workspaces source get --dir ./ws', 'write tables/, workflows/<key>/, … and .sim/'],
59-
['edit the files', 'a new workflow: workflows/<key>/workflow.ts'],
61+
['edit the files', 'every file first; a new workflow: workflows/<key>/workflow.ts'],
6062
['sim workspaces source check --dir ./ws', 'offline: each error as file:line:column'],
61-
[typecheckCommand('./ws'), 'type-check the edit'],
62-
['sim workspaces source apply --dir ./ws --dry-run', 'the plan; nothing is written'],
63-
['sim workspaces source apply --dir ./ws --yes', 'write it to the workspace'],
63+
[' && sim workspaces source apply --dir ./ws --yes', 'write it to the workspace (it re-checks)'],
6464
])}
65+
Run those two as one chained command: a non-zero exit stops it at the first failure, and the next
66+
step fixes what it reported. For review, \`apply --dry-run\` prints the plan and writes nothing;
67+
\`${typecheckCommand('./ws')}\` adds type errors.
6568
Promote dev to prod: \`sim workspaces source apply --dir ./ws --to <workspaceId> --dry-run\`, then
6669
--yes.
6770
${WORKSPACE_LINE}

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

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {
1010
builderSnippet,
1111
describeBuilder,
1212
referencedBuilders,
13+
SDK_MAJOR,
1314
typecheckCommand,
1415
} from '@sim/workflow-sdk/codegen'
1516
import {
@@ -40,21 +41,21 @@ const PARAMS_COMMAND = 'sim workflows source params'
4041

4142
/** The rules every source directory shares, after the kind's own edit loop. */
4243
function sharedRules(group: string, extraRenames = ''): string {
43-
return `- **Identity**: a block is its entry in \`${SOURCE_LOCK_FILE}\`, which binds it by its const name or its display name. Change the display name (first argument) freely: references through the const follow it, and the apply rewrites every \`$ref.<old key>\` and \`<old key.…>\` for you (don't hand-edit). Renaming the const too keeps it when settings, neighbours or references still tie it to the old block; otherwise the apply refuses a delete (\`E_APPLY_BLOCK_DELETE\`). Safest: keep the const.
44+
return `- **Identity**: a block is its entry in \`${SOURCE_LOCK_FILE}\`, which binds it by its const name or its display name. Change the display name (first argument) freely: references through the const follow it, and the apply rewrites every \`$ref.<old key>\` and \`<old key.…>\` for you. Renaming the const too keeps it when settings, neighbours or references still tie it to the old block; otherwise the apply refuses a delete (\`E_APPLY_BLOCK_DELETE\`).
4445
- **References**: in \`${SOURCE_ENTRY_FILE}\` read an output as \`x.content\`, a loop or parallel item as \`p.currentItem\` (\`p.index\`), text as \`\` t\`…\` \`\`, env as \`env.NAME\`. In a function file (\`codeFile('./functions/x.js')\`) read an output as \`$ref.<key>.<path>\`, the key: the display name lowercased, whitespace and dots removed ("Triage Agent" → \`$ref.triageagent.content\`); in a loop body \`$ref.loop.currentItem\` / \`$ref.loop.index\`, in a parallel \`$ref.parallel.currentItem\` / \`$ref.parallel.index\`; env as \`$env.NAME\`. In \`.py\`/\`.sh\` files write \`<key.path>\` and \`{{NAME}}\`, not \`$ref\`/\`$env\`. Read an index or item only inside its container (\`E_APPLY_REF_OUT_OF_SCOPE\`), after it its \`.results\`. A \`$ref\` naming no block is an error in a function file you edited (\`E_APPLY_UNRESOLVED_REF\`).
4546
- **Renames**: one variable per apply (two at once read as a delete and an add).${extraRenames}
4647
- **Look up a builder**: \`${PARAMS_COMMAND} <builder>\` (a namespace like \`gmail\` lists them).
4748
- **Credentials**: \`sim credentials connect <provider> --name "<Name>"\`, give the owner its link, then \`credential('<key>')\` (\`sim workflows source resources\`).
4849
- **Failures**: \`x.onError(y)\` runs \`y\` when \`x\` fails, reading the message as \`x.error\` (a Function's \`x.errorMessage\` drops the line and code: reply with it). Retry is block meta (third argument): \`agent('Name', {…}, { retry: { enabled: true, maxTries: 3, waitBetweenTriesMs: 2000 } })\`. A reference to a block that did not run is \`null\` (a whole value, function code, a condition) or \`''\` in text. Branches may rejoin; only branches that end separately each need a \`response\`.
49-
- **Run and debug**: \`sim workflows run <workflowId> --manual --input '{…}'\` runs the saved draft (without it, the deployed version). Runs write real data (rows, emails): use test resources. With \`--dir <the workflow's folder>\`, a failed run names the failing file and line; then \`sim logs get <runId> --trace --no-include-workflow-state\`; rows: \`sim tables rows list <table name>\`.
50-
- **Exit codes**: 0 ok, 1 errors, ${SOURCE_STALE_EXIT_CODE} stale (\`${group} get --dir <fresh dir>\`, redo the edit there), ${RATE_LIMITED_EXIT_CODE} rate limited, ${SOURCE_BUSY_EXIT_CODE} busy, ${SOURCE_LOCAL_CHANGES_EXIT_CODE}/${SOURCE_DRIFT_EXIT_CODE} \`status\`: your edits / the server changed. Every code: \`${SOURCE_AGENTS_FILE}\`.`
50+
- **Run and debug**: \`--manual\` runs the saved draft, without it the deployed version. Runs write real data (rows, emails): use test resources. With \`--dir <the workflow's folder>\`, a failed run names the failing file and line; then \`sim logs get <runId> --trace --no-include-workflow-state\`; rows: \`sim tables rows list <table name>\`.
51+
- **Exit codes**: 0 ok, 1 errors, ${SOURCE_STALE_EXIT_CODE} stale (\`${group} get --dir <fresh dir>\`, redo the edit there), ${RATE_LIMITED_EXIT_CODE} rate limited, ${SOURCE_BUSY_EXIT_CODE} busy, ${SOURCE_LOCAL_CHANGES_EXIT_CODE}/${SOURCE_DRIFT_EXIT_CODE} \`status\`: your edits / the server changed.`
5152
}
5253

5354
const WORKFLOW_PRIMER = `# Sim workflow source: primer
5455
5556
\`${SOURCE_ENTRY_FILE}\` is a Sim workflow as TypeScript; the server compiles it statically and nothing in it runs. Commands read the workflow id from \`${SOURCE_BASE_FILE}\`; run them from this directory.
5657
57-
**Edit loop**: edit \`${SOURCE_ENTRY_FILE}\` (and \`functions/*\`) → \`sim workflows source check --dir .\` (offline; it also adds new blocks to \`tsconfig.json\`) → \`${typecheckCommand()}\` → \`sim workflows source apply --dir . --dry-run\` → \`--yes\`. Rerun each step until it exits 0. Apply saves the draft in canonical form: read \`${SOURCE_ENTRY_FILE}\` again before the next edit.
58+
**Edit loop**: write every file, then run one chained command and fix what it reports next: \`sim workflows source check --dir . && sim workflows source apply --dir . --yes && sim workflows run <workflowId> --manual --input '{…}'\`. A non-zero exit stops the chain. Apply re-checks the source and its base, so \`apply --dry-run\` is only the review for people and CI. Add \`${typecheckCommand()}\` after \`check\` for type errors. Apply saves the draft in canonical form: read \`${SOURCE_ENTRY_FILE}\` again before the next edit.
5859
5960
- **Deletes**: removing a block or a variable needs \`apply --allow-delete\` (\`E_APPLY_BLOCK_DELETE\` / \`E_APPLY_VARIABLE_DELETE\`).
6061
- **Deploy**: apply saves a draft; \`apply --deploy\` also deploys it, or \`sim workflows deploy <workflowId>\` publishes the saved draft later.
@@ -67,13 +68,12 @@ const WORKSPACE_RENAMES =
6768

6869
const WORKSPACE_PRIMER = `# Sim workspace source: primer
6970
70-
This directory is a Sim workspace as files (\`tables/\`, \`workflows/<key>/workflow.ts\`, …); nothing in it runs. Commands read the workspace id from \`${SOURCE_BASE_FILE}\`; run them from this directory.
71+
This directory is a Sim workspace as files (\`tables/\`, \`workflows/<key>/workflow.ts\`, …); nothing in it runs. Commands read the workspace id from \`${SOURCE_BASE_FILE}\`; run them from this directory. Every \`.ts\` file, \`deployments/\` too, starts with \`// @sim-sdk ${SDK_MAJOR}\`.
7172
72-
**Edit loop**: edit the files → \`sim workspaces source check --dir .\` (offline; refreshes \`workspace.d.ts\` and \`tsconfig.json\`) → \`${typecheckCommand()}\` → \`sim workspaces source apply --dir . --dry-run\` → \`--yes\`. Rerun each step until it exits 0. Apply rewrites the directory: read a file again before the next edit.
73+
**Edit loop**: write every file, then run one chained command and fix what it reports next: \`sim workspaces source check --dir . && sim workspaces source apply --dir . --yes\` (add a \`sim workflows run\` to test). A non-zero exit stops the chain. Apply re-checks the source and its base, so \`apply --dry-run\` is only the review for people and CI. Add \`${typecheckCommand()}\` after \`check\` for type errors. Apply rewrites the directory: read a file again before the next edit.
7374
74-
- **Deletes**: a removed file, table column, workflow block or variable is deleted only with \`apply --allow-delete\` and cannot be undone. A kind is managed only when its folder exists.
75+
- **Deletes**: a file or folder missing from a kind's folder, or a removed table column, workflow block or variable, is a delete: refused without \`apply --allow-delete\`, and it cannot be undone.
7576
- **Deploy**: \`deployments/<workflow key>.ts\` (\`defineDeployment({ live: 'draft' })\`) deploys that workflow on apply; without one, apply never deploys.
76-
- **Promote** (dev → prod): \`sim workspaces source apply --dir . --to <workspaceId> --dry-run\`, then \`--yes\`.
7777
- **Exit ${SOURCE_HELD_EXIT_CODE}**: written in part; changes using an unset secret or unbound credential were held back: set it, apply again.
7878
${sharedRules('sim workspaces source', WORKSPACE_RENAMES)}
7979
`
@@ -96,7 +96,7 @@ export function agentsPointer(kind: PrimerKind): string {
9696
9797
${kind === 'workflow' ? '`workflow.ts` is a Sim workflow as TypeScript that Sim compiles' : 'This directory is a Sim workspace as files that Sim applies'}; nothing in it runs. Read \`${SOURCE_AGENTS_FILE}\` before editing: the rules, the edit loop and every exit code.
9898
99-
Loop: edit, then \`${group} check --dir .\`, \`${typecheckCommand()}\`, \`${group} apply --dir . --dry-run\`, \`--yes\`.
99+
Loop: write every file, then one chained command: \`${group} check --dir . && ${group} apply --dir . --yes\`; fix what it reports next.
100100
Exit codes: 0 ok, 1 errors, ${SOURCE_STALE_EXIT_CODE} stale (get a fresh copy and redo the edit), ${SOURCE_DRIFT_EXIT_CODE} \`status\`: the server changed.
101101
`
102102
}

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

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ import {
2727
SDK_MAJOR,
2828
SDK_MODELS_FILE,
2929
sdkIndexHeader,
30-
typecheckCommand,
3130
type WorkspaceManifest,
3231
} from '@sim/workflow-sdk/codegen'
3332
import type { Command } from 'commander'
@@ -601,17 +600,15 @@ export function primerOption(command: Command): void {
601600
}
602601

603602
/**
604-
* The commands of a directory's edit loop, in order, for a get's result: what the primer's loop
605-
* says, runnable from anywhere because each names the directory.
603+
* The edit loop of a directory for a get's result, as the one chained command the primer
604+
* recommends: runnable from anywhere because it names the directory. Apply re-checks everything
605+
* on the server, so a dry run is not a step of it. Still a list, so the JSON field keeps the shape
606+
* earlier results had.
606607
*/
607608
export function editLoop(kind: PrimerKind, dir: string): string[] {
608609
const group = kind === 'workflow' ? 'sim workflows source' : 'sim workspaces source'
609610
const at = shellWord(dir)
610-
return [
611-
`${group} check --dir ${at}`,
612-
typecheckCommand(at),
613-
`${group} apply --dir ${at} --dry-run`,
614-
]
611+
return [`${group} check --dir ${at} && ${group} apply --dir ${at} --yes`]
615612
}
616613

617614
/** What a directory's editable tree holds beside the printed source. */

0 commit comments

Comments
 (0)