Skip to content

Commit d84ee0f

Browse files
committed
fix(workflow-code): decode JSON-text input mappings, keep wiring beside its blocks, add source find
- A Workflow block's inputMapping (and any native object/list field) stored as JSON text by the operations route printed as a t`` template that never type-checks; an untouched directory passed only because check suppresses errors in files still as get wrote them. Canonicalize now decodes the container and materialize keeps the stored text encoding. Deploy-change detection treats the two encodings as one value. - The printer emits each wiring statement under the last-declared block it names, so a block's edges sit next to it instead of ~1000 lines away in a 192-block workflow. - New `sim workflows source find <block>`: a block's code with line numbers, function file, incoming/outgoing wiring and readers, offline. - The operations batch cap error says how to split a larger edit.
1 parent 2fbc5f3 commit d84ee0f

46 files changed

Lines changed: 718 additions & 114 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

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

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8837,6 +8837,38 @@ Prints a builder in brief (an example call, the required params with types, the
88378837

88388838
Exit codes: 0 printed; 1 no builder or namespace of that name (the closest one is named), or no cached catalog (run a get once).
88398839

8840+
### sim workflows source find
8841+
8842+
Print a block's code, edges and readers with line numbers (offline)
8843+
8844+
```bash
8845+
sim workflows source find <block> [options]
8846+
```
8847+
8848+
**Arguments**
8849+
8850+
<CommandTable>
8851+
8852+
| Argument | Required | Description |
8853+
| --- | --- | --- |
8854+
| `block` | Yes | Block name, key or identifier; a part of a name matches every block |
8855+
8856+
</CommandTable>
8857+
8858+
**Options**
8859+
8860+
<CommandTable>
8861+
8862+
| Option | Required | Description |
8863+
| --- | --- | --- |
8864+
| `--dir <dir>` | No | Workflow source directory to read. Defaults to `.`. |
8865+
8866+
</CommandTable>
8867+
8868+
Prints each block the name matches, read from the directory alone (no request is made): its declaration in workflow.ts with line numbers, its function file, the wiring statements of its incoming and outgoing edges, and every block that reads its outputs (a param, a branch, or a function file's $ref), each as file:line. &lt;block&gt; is a block name (Security Score), its key (securityscore) or its identifier (securityScore); with no exact match, every block whose name contains it is printed (at most 20). Run it from a workflow source directory, or pass --dir.
8869+
8870+
Exit codes: 0 printed; 1 no block matches (the closest name is suggested), or the directory is not a workflow source directory.
8871+
88408872
### sim workflows get
88418873

88428874
Get workflow

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

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1569,6 +1569,38 @@ Without --server, each file created, edited or deleted since the directory was r
15691569

15701570
API request and response schemas: [getWorkflowSource](/api-reference/workflows/getWorkflowSource).
15711571

1572+
### Print a block's code, edges and readers with line numbers
1573+
1574+
Offline.
1575+
1576+
```bash
1577+
sim workflows source find <block> [options]
1578+
```
1579+
1580+
**Arguments**
1581+
1582+
<CommandTable>
1583+
1584+
| Argument | Required | Description |
1585+
| --- | --- | --- |
1586+
| `block` | Yes | Block name, key or identifier; a part of a name matches every block |
1587+
1588+
</CommandTable>
1589+
1590+
**Options**
1591+
1592+
<CommandTable>
1593+
1594+
| Option | Required | Description |
1595+
| --- | --- | --- |
1596+
| `--dir <dir>` | No | Workflow source directory to read. Defaults to `.`. |
1597+
1598+
</CommandTable>
1599+
1600+
Prints each block the name matches, read from the directory alone (no request is made): its declaration in workflow.ts with line numbers, its function file, the wiring statements of its incoming and outgoing edges, and every block that reads its outputs (a param, a branch, or a function file's $ref), each as file:line. &lt;block&gt; is a block name (Security Score), its key (securityscore) or its identifier (securityScore); with no exact match, every block whose name contains it is printed (at most 20). Run it from a workflow source directory, or pass --dir.
1601+
1602+
Exit codes: 0 printed; 1 no block matches (the closest name is suggested), or the directory is not a workflow source directory.
1603+
15721604
### Print a builder's params and outputs from the cached block catalog
15731605

15741606
Offline.

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ For a whole workspace, use `sim workspaces source` with the same steps; see [Man
3232

3333
## Look things up
3434

35+
- `sim workflows source find <block>` prints one block of the workflow by name, key or identifier, from the directory without a request: its declaration with line numbers, its function file, its incoming and outgoing wiring and every block that reads its outputs, each as `file:line`. A part of a name prints every block it matches.
3536
- `sim workflows source params <builder>` prints a builder in brief (an example call, the required params with types, the optional ones by name and the outputs) from the cached block catalog without a request. `--full` prints every param with its type, options, default and doc. A legacy builder names the one that replaces it, and a namespace such as `gmail` lists its builders.
3637
- `sim workflows source resources` lists the tables, knowledge bases, credentials, workflows, skills, custom tools, MCP servers and environment variable names the file can name.
3738
- `sim workflows source catalog` shows the block catalog the source is checked against.

‎apps/sim/lib/api/contracts/v2/workflows.ts‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3340,7 +3340,10 @@ export const v2ImportWorkflowContract = defineRouteContract({
33403340
*/
33413341
export const MAX_WORKFLOW_GRAPH_BLOCKS = 2000
33423342
export const MAX_WORKFLOW_GRAPH_EDGES = 10_000
3343-
/** Ceiling on one `POST /operations` batch. */
3343+
/**
3344+
* Ceiling on one `POST /operations` batch: a bound on the request, the one transaction and the
3345+
* report an atomic batch writes, not on a workflow's size. A larger edit splits into batches.
3346+
*/
33443347
export const MAX_WORKFLOW_EDIT_OPERATIONS = 200
33453348
/** Ceiling on the complete tool list assigned to one Agent block. */
33463349
export const MAX_AGENT_TOOLS_PER_BLOCK = 100
@@ -4342,7 +4345,7 @@ export const v2ApplyWorkflowOperationsBodySchema = z
43424345
.min(1, 'operations cannot be empty')
43434346
.max(
43444347
MAX_WORKFLOW_EDIT_OPERATIONS,
4345-
`operations cannot exceed ${MAX_WORKFLOW_EDIT_OPERATIONS} entries`
4348+
`operations cannot exceed ${MAX_WORKFLOW_EDIT_OPERATIONS} entries: split the edit into batches of at most ${MAX_WORKFLOW_EDIT_OPERATIONS}, applied in turn (atomic covers one batch); a later batch names a block an earlier one added by the id in that apply's mintedBlockIds`
43464349
)
43474350
.describe('Edits to apply, in a single batch.'),
43484351
atomic: z

‎apps/sim/lib/workflows/canonical/subblock-value.ts‎

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { parseJsonContainer } from '@sim/workflow-code'
2+
import { holdsNativeContainer } from '@/lib/workflows/code/catalog/value-kind'
13
import {
24
normalizedStringify,
35
sanitizeInputFormat,
@@ -56,6 +58,16 @@ export function shapeSubBlockValue(
5658
return shaped
5759
}
5860

61+
/**
62+
* JSON text in a field that holds a container natively (an `inputMapping` the operations route
63+
* stored as text) is that container: the executor parses both alike, so a change of encoding alone
64+
* is no change to deploy. The diff view still shows the encoding (`shapeSubBlockValue`).
65+
*/
66+
function decodeNativeContainer(value: unknown, spec: CanonicalFieldSpec | undefined): unknown {
67+
if (typeof value !== 'string' || !spec?.type || !holdsNativeContainer(spec.type)) return value
68+
return parseJsonContainer(value) ?? value
69+
}
70+
5971
/**
6072
* Resolves a stored subblock value to the configuration it actually represents.
6173
*
@@ -80,7 +92,7 @@ export function canonicalizeSubBlockValue(
8092
stored: unknown,
8193
spec: CanonicalFieldSpec | undefined
8294
): unknown {
83-
const shaped = shapeSubBlockValue(subBlockId, stored, spec?.type)
95+
const shaped = decodeNativeContainer(shapeSubBlockValue(subBlockId, stored, spec?.type), spec)
8496

8597
/*
8698
* Root absence is canonicalized separately from structured values, where

‎apps/sim/lib/workflows/code/api-built-source.test.ts‎

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44
* unnamed input row) and a function block added through the operations route.
55
*/
66

7+
import { mkdtempSync, rmSync, writeFileSync } from 'node:fs'
8+
import { tmpdir } from 'node:os'
9+
import path from 'node:path'
710
import { isRecordLike } from '@sim/utils/object'
811
import {
912
canonicalize,
@@ -14,14 +17,15 @@ import {
1417
sourceFingerprint,
1518
} from '@sim/workflow-code'
1619
import type { WorkflowState } from '@sim/workflow-types/workflow'
17-
import { describe, expect, it, vi } from 'vitest'
20+
import { afterAll, describe, expect, it, vi } from 'vitest'
1821

1922
vi.unmock('@/blocks/registry')
2023
vi.unmock('@/tools/metadata-outputs')
2124
vi.unmock('@/tools/registry')
2225

2326
import { registryCatalog as catalog } from '@/lib/workflows/code/catalog/registry-catalog.test-helpers'
2427
import { createMaterializeHooks } from '@/lib/workflows/code/materialize-hooks'
28+
import { typeCheckAgainstSdk, writeSdkTypes } from '@/lib/workflows/code/sdk-typecheck.test-helpers'
2529
import { buildDefaultWorkflowArtifacts } from '@/lib/workflows/defaults'
2630
import { createBlockFromParams } from '@/lib/workflows/editing/builders'
2731

@@ -126,3 +130,41 @@ describe('a workflow built by the editor and the API', () => {
126130
expect(JSON.stringify(run.state)).toBe(JSON.stringify(stored))
127131
})
128132
})
133+
134+
describe('a child workflow call whose input mapping the operations route stored as JSON text', () => {
135+
const MAPPING = '{"ticketId":"<start.input>","source":"api"}'
136+
const workDir = mkdtempSync(path.join(tmpdir(), 'api-built-source-'))
137+
afterAll(() => rmSync(workDir, { recursive: true, force: true }))
138+
139+
function callWorkflow(): WorkflowState {
140+
const stored = builtWorkflow()
141+
const call = createBlockFromParams('call-block', {
142+
type: 'workflow_input',
143+
name: 'Record Audit',
144+
inputs: { workflowId: 'child-workflow', inputMapping: MAPPING },
145+
})
146+
return {
147+
...stored,
148+
blocks: { ...stored.blocks, [call.id]: { ...call, position: { x: 800, y: 0 } } },
149+
}
150+
}
151+
152+
it('prints source that type-checks after an edit, and stores the mapping as text as before', () => {
153+
const stored = callWorkflow()
154+
const untouched = getThenApply(stored)
155+
expect(untouched.noop).toBe(true)
156+
expect(untouched.state.blocks['call-block']?.subBlocks.inputMapping?.value).toBe(MAPPING)
157+
158+
const renamed = (text: string) => text.replace("fn('Score'", "fn('Final Score'")
159+
const remapped = (text: string) => text.replace("source: 'api'", "source: 'ops'")
160+
const edited = getThenApply(stored, (text) => remapped(renamed(text)))
161+
expect(edited.errors).toEqual([])
162+
expect(edited.state.blocks['call-block']?.subBlocks.inputMapping?.value).toBe(
163+
MAPPING.replace('"api"', '"ops"')
164+
)
165+
writeSdkTypes(workDir, catalog)
166+
const file = path.join(workDir, 'workflow.ts')
167+
writeFileSync(file, remapped(renamed(edited.text)))
168+
expect(typeCheckAgainstSdk(workDir, 'edited', [file])).toBe('')
169+
}, 120_000)
170+
})

‎apps/sim/lib/workflows/code/catalog/value-kind.ts‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import type {
44
ParamStorage,
55
ResourceKind,
66
} from '@sim/workflow-code'
7+
import { getOwn } from '@sim/workflow-code'
78
import type { SubBlockConfig, SubBlockType } from '@/blocks/types'
89

910
/**
@@ -88,6 +89,20 @@ const TYPE_RULES: Record<SubBlockType, TypeRule> = {
8889
modal: { valueKind: 'none' },
8990
}
9091

92+
/**
93+
* Whether a field of `type` holds a JSON object or list natively: the editor writes the container,
94+
* while the operations route and older editors may store the same container as JSON text, which
95+
* the executor parses alike.
96+
*/
97+
export function holdsNativeContainer(type: string): boolean {
98+
const rule = getOwn(TYPE_RULES, type)
99+
return (
100+
(rule?.valueKind === 'object' || rule?.valueKind === 'array') &&
101+
rule.storage === undefined &&
102+
rule.structured === undefined
103+
)
104+
}
105+
91106
/**
92107
* Derives a subBlock's value shape from its type plus the few config flags that change it: a
93108
* JSON-language code field holds JSON, a multi-select dropdown holds an array, a `readOnly` field

‎apps/sim/lib/workflows/code/corpus/typecheck.corpus.test.ts‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,6 @@ const KNOWN_UNTYPED: ReadonlyArray<{ match: RegExp; cause: string }> = [
5050
match: /:(response|human_in_the_loop|human_in_the_loop_v2):/,
5151
cause: 'a response format stored as text',
5252
},
53-
{ match: /:workflow_input:/, cause: 'an input mapping stored as JSON text' },
5453
{
5554
match: /^(operation|error-edge|variables):variables:/,
5655
cause: 'variable rows with no variableId',

‎packages/sim-cli/src/commands/protocol/index.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import { attachWorkflowRunGet } from './workflow-run-get'
2525
import { attachWorkflowRunWait } from './workflow-run-wait'
2626
import { attachWorkflowSourceApply, attachWorkflowSourceCreate } from './workflow-source-apply'
2727
import { attachWorkflowSourceCheck } from './workflow-source-check'
28+
import { attachWorkflowSourceFind } from './workflow-source-find'
2829
import { attachWorkflowSourceGet } from './workflow-source-get'
2930
import { attachWorkflowSourceParams } from './workflow-source-params'
3031
import { attachWorkflowSourceDiff, attachWorkflowSourceStatus } from './workflow-source-status'
@@ -107,6 +108,7 @@ export function attachProtocolCommands(program: Command): void {
107108
attachWorkflowSourceStatus(source)
108109
attachWorkflowSourceDiff(source)
109110
attachWorkflowSourceParams(source)
111+
attachWorkflowSourceFind(source)
110112

111113
const workspaces = group(program, 'workspaces')
112114
attachWorkspaceOperationWait(group(workspaces, 'operations'))

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -464,7 +464,7 @@ function declarationFindings(
464464
* `catalog`, checking env names against `envNames` (what `secrets.ts` declares) when given. Its
465465
* calls are untyped: {@link compiledWithChildResults} types them.
466466
*/
467-
async function compileWorkflowDir(
467+
export async function compileWorkflowDir(
468468
dir: string,
469469
workspaceCatalog: BlockCatalog,
470470
envNames?: ReadonlySet<string>,

0 commit comments

Comments
 (0)