Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 9 additions & 5 deletions apps/docs/content/docs/workflows/code/coding-agents.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ Claude Code, Cursor, Codex and other coding agents work from a directory that `g

## What get gives an agent

- 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.
- 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.
- **`.sim/AGENTS.md`**: the primer followed by a reference, by task. A workspace directory also gets `.sim/GUIDE-advanced.md`.
- **`.sim/examples/`**: one correct example of each common block to copy from.
- **`.sim/sdk/`**: the type declarations, with `.sim/sdk/INDEX.md` listing the builders.
Expand All @@ -17,10 +17,14 @@ Point the agent at `.sim/AGENTS.md` before it edits.
## The loop

1. `sim workflows source get <workflowId> --dir <dir>` writes the workflow and the guide.
2. Edit `workflow.ts`.
3. `sim workflows source check --dir <dir>` compiles it offline.
4. `sim workflows source status --dir <dir>` shows what changed and what to run next.
5. `sim workflows source apply --dir <dir> --dry-run`, then `--yes`, saves the draft.
2. Write every file the change needs.
3. Run one chained command, then fix whatever it reports in the next step:

```bash
sim workflows source check --dir <dir> && sim workflows source apply --dir <dir> --yes && sim workflows run <workflowId> --manual --input '{...}'
```

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.

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

Expand Down
2 changes: 1 addition & 1 deletion apps/docs/content/docs/workflows/code/directory.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ workflows/support/workflow.ts a workflow directory
deployments/support.ts how workflows/support is deployed
```

A resource's key is its file or folder name, and a workflow names it by that key. What each kind's file holds:
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:

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -642,6 +642,8 @@ describe('workspace source files', () => {

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

const deleted = await apply(workspaceId, dropped, { base: read.base, allowDelete: true })
Expand Down
4 changes: 2 additions & 2 deletions apps/sim/lib/workspace-source/application/findings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -463,12 +463,12 @@ export function planUndeclared<K extends WorkspaceKind, S>(
kind,
key,
'E_APPLY_DELETE_NOT_ALLOWED',
`${where(kind, key)} is no longer declared; deleting "${resource.name}" needs --allow-delete.`,
`${where(kind, key)} is no longer declared: a resource missing from the directory is a delete, and deleting "${resource.name}" needs --allow-delete.`,
{
hint:
kind === 'deployment'
? 'Pass --allow-delete to delete it, or restore its file to keep it.'
: '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.',
: '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.',
}
)
: kindFinding(
Expand Down
19 changes: 11 additions & 8 deletions packages/sim-cli/src/commands/protocol/source-loop-help.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,14 @@ export const WORKFLOW_SOURCE_LOOP_HELP = `
The loop:
${loopRows([
['sim workflows source get <workflowId> --dir ./wf', 'write workflow.ts, functions/* and .sim/'],
['edit ./wf/workflow.ts', ''],
['edit ./wf/workflow.ts', 'every file first, then the one command below'],
['sim workflows source check --dir ./wf', 'offline: each error as file:line:column'],
[typecheckCommand('./wf'), 'type-check the edit'],
['sim workflows source apply --dir ./wf --dry-run', 'the plan; nothing is written'],
['sim workflows source apply --dir ./wf --yes', 'save the draft'],
[' && sim workflows source apply --dir ./wf --yes', 'save the draft (it re-checks)'],
[' && sim workflows run <workflowId> --manual', 'test the saved draft'],
])}
Run those three as one chained command: a non-zero exit stops it at the first failure, and the next
step fixes what it reported. For review, \`apply --dry-run\` prints the plan and writes nothing;
\`${typecheckCommand('./wf')}\` adds type errors.
A new workflow: \`sim workflows source create --dir ./new --workspace <workspaceId>\` writes a
starter that records its workspace; edit and check it, then \`create --dir ./new --yes\` creates the
workflow there, and the loop above goes on from there.
Expand All @@ -56,12 +58,13 @@ export const WORKSPACE_SOURCE_LOOP_HELP = `
The loop:
${loopRows([
['sim workspaces source get --dir ./ws', 'write tables/, workflows/<key>/, … and .sim/'],
['edit the files', 'a new workflow: workflows/<key>/workflow.ts'],
['edit the files', 'every file first; a new workflow: workflows/<key>/workflow.ts'],
['sim workspaces source check --dir ./ws', 'offline: each error as file:line:column'],
[typecheckCommand('./ws'), 'type-check the edit'],
['sim workspaces source apply --dir ./ws --dry-run', 'the plan; nothing is written'],
['sim workspaces source apply --dir ./ws --yes', 'write it to the workspace'],
[' && sim workspaces source apply --dir ./ws --yes', 'write it to the workspace (it re-checks)'],
])}
Run those two as one chained command: a non-zero exit stops it at the first failure, and the next
step fixes what it reported. For review, \`apply --dry-run\` prints the plan and writes nothing;
\`${typecheckCommand('./ws')}\` adds type errors.
Promote dev to prod: \`sim workspaces source apply --dir ./ws --to <workspaceId> --dry-run\`, then
--yes.
${WORKSPACE_LINE}
Expand Down
18 changes: 9 additions & 9 deletions packages/sim-cli/src/commands/protocol/source-primer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
builderSnippet,
describeBuilder,
referencedBuilders,
SDK_MAJOR,
typecheckCommand,
} from '@sim/workflow-sdk/codegen'
import {
Expand Down Expand Up @@ -40,21 +41,21 @@ const PARAMS_COMMAND = 'sim workflows source params'

/** The rules every source directory shares, after the kind's own edit loop. */
function sharedRules(group: string, extraRenames = ''): string {
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.
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\`).
- **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\`).
- **Renames**: one variable per apply (two at once read as a delete and an add).${extraRenames}
- **Look up a builder**: \`${PARAMS_COMMAND} <builder>\` (a namespace like \`gmail\` lists them).
- **Credentials**: \`sim credentials connect <provider> --name "<Name>"\`, give the owner its link, then \`credential('<key>')\` (\`sim workflows source resources\`).
- **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\`.
- **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>\`.
- **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}\`.`
- **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>\`.
- **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.`
}

const WORKFLOW_PRIMER = `# Sim workflow source: primer
\`${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.
**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.
**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.
- **Deletes**: removing a block or a variable needs \`apply --allow-delete\` (\`E_APPLY_BLOCK_DELETE\` / \`E_APPLY_VARIABLE_DELETE\`).
- **Deploy**: apply saves a draft; \`apply --deploy\` also deploys it, or \`sim workflows deploy <workflowId>\` publishes the saved draft later.
Expand All @@ -67,13 +68,12 @@ const WORKSPACE_RENAMES =

const WORKSPACE_PRIMER = `# Sim workspace source: primer
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.
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}\`.
**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.
**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.
- **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.
- **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.
- **Deploy**: \`deployments/<workflow key>.ts\` (\`defineDeployment({ live: 'draft' })\`) deploys that workflow on apply; without one, apply never deploys.
- **Promote** (dev → prod): \`sim workspaces source apply --dir . --to <workspaceId> --dry-run\`, then \`--yes\`.
- **Exit ${SOURCE_HELD_EXIT_CODE}**: written in part; changes using an unset secret or unbound credential were held back: set it, apply again.
${sharedRules('sim workspaces source', WORKSPACE_RENAMES)}
`
Expand All @@ -96,7 +96,7 @@ export function agentsPointer(kind: PrimerKind): string {
${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.
Loop: edit, then \`${group} check --dir .\`, \`${typecheckCommand()}\`, \`${group} apply --dir . --dry-run\`, \`--yes\`.
Loop: write every file, then one chained command: \`${group} check --dir . && ${group} apply --dir . --yes\`; fix what it reports next.
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.
`
}
Expand Down
13 changes: 5 additions & 8 deletions packages/sim-cli/src/commands/protocol/source-tree.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ import {
SDK_MAJOR,
SDK_MODELS_FILE,
sdkIndexHeader,
typecheckCommand,
type WorkspaceManifest,
} from '@sim/workflow-sdk/codegen'
import type { Command } from 'commander'
Expand Down Expand Up @@ -601,17 +600,15 @@ export function primerOption(command: Command): void {
}

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

/** What a directory's editable tree holds beside the printed source. */
Expand Down
Loading
Loading