Skip to content

Commit 2e40feb

Browse files
authored
refactor(workflow-code): final-state audit cleanup (#10318)
Final-state audit of workflows/workspace as code: super-linear regexes and lookups on user input made linear (each with a failing-before test), staged blob uploads run 4 at a time, duplicated helpers consolidated onto shared ones, dead exports removed, hand-written refusal/strict-code/subflow-default data derived from their sources, and docs/guides/SYNTAX.md/exit-code tables brought back in line with the code under new drift tests.
1 parent 5aa429e commit 2e40feb

57 files changed

Lines changed: 379 additions & 312 deletions

Some content is hidden

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

‎.claude/rules/sim-workflow-code.md‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -157,8 +157,7 @@ satisfy.
157157
2. A codec `packages/workspace-code/src/kinds/<kind>.ts` with tests; `KIND_LAYOUTS`, and
158158
`BLOB_KIND_CODECS` if it holds blobs (`src/kinds/registry.ts`); its diagnostic codes.
159159
3. An adapter `apps/sim/lib/workspace-source/application/kinds/<kind>/adapter.ts` calling the
160-
kind's own use cases; `KIND_ADAPTERS` (`application/registry.ts`) and `NOUNS`
161-
(`application/findings.ts`); an integration test.
160+
kind's own use cases; `KIND_ADAPTERS` (`application/registry.ts`); an integration test.
162161
4. If workflows name it by key: `WORKFLOW_RESOURCE_KINDS`, the live loader in
163162
`lib/workspaces/resource-keys.ts`, the manifest in `read-source-resources.ts` and
164163
`codegen/workspace.ts`, and `PENDABLE_REFERENCES` in
@@ -222,5 +221,6 @@ Behaviour is proven by properties over the real registry, not restated unit test
222221
- The Code tab and the CLI have HTTP e2e scripts (`apps/sim/scripts/test-workflow-code-ui-e2e.ts`,
223222
`test-workflow-source-e2e.ts`, `test-workspace-source-e2e.ts`).
224223
- Generated artifacts and their checks: `generate:sdk-names` (`check:sdk-names`,
225-
`check:sdk-names-frozen`), the SDK embedded text (its `*-text.test.ts`), goldens
226-
(`check:workflow-code-golden`).
224+
`check:sdk-names-frozen`), `generate:workflow-sdk-embedded-text`
225+
(`check:workflow-sdk-embedded-text`), `generate:sdk-fixture-catalog`
226+
(`check:sdk-fixture-catalog`), goldens (`check:workflow-code-golden`).

‎.cursor/rules/sim-workflow-code.mdc‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -137,8 +137,7 @@ satisfy.
137137
2. A codec `packages/workspace-code/src/kinds/<kind>.ts` with tests; `KIND_LAYOUTS`, and
138138
`BLOB_KIND_CODECS` if it holds blobs (`src/kinds/registry.ts`); its diagnostic codes.
139139
3. An adapter `apps/sim/lib/workspace-source/application/kinds/<kind>/adapter.ts` calling the
140-
kind's own use cases; `KIND_ADAPTERS` (`application/registry.ts`) and `NOUNS`
141-
(`application/findings.ts`); an integration test.
140+
kind's own use cases; `KIND_ADAPTERS` (`application/registry.ts`); an integration test.
142141
4. If workflows name it by key: `WORKFLOW_RESOURCE_KINDS`, the live loader in
143142
`lib/workspaces/resource-keys.ts`, the manifest in `read-source-resources.ts` and
144143
`codegen/workspace.ts`, and `PENDABLE_REFERENCES` in
@@ -202,5 +201,6 @@ Behaviour is proven by properties over the real registry, not restated unit test
202201
- The Code tab and the CLI have HTTP e2e scripts (`apps/sim/scripts/test-workflow-code-ui-e2e.ts`,
203202
`test-workflow-source-e2e.ts`, `test-workspace-source-e2e.ts`).
204203
- Generated artifacts and their checks: `generate:sdk-names` (`check:sdk-names`,
205-
`check:sdk-names-frozen`), the SDK embedded text (its `*-text.test.ts`), goldens
206-
(`check:workflow-code-golden`).
204+
`check:sdk-names-frozen`), `generate:workflow-sdk-embedded-text`
205+
(`check:workflow-sdk-embedded-text`), `generate:sdk-fixture-catalog`
206+
(`check:sdk-fixture-catalog`), goldens (`check:workflow-code-golden`).

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ Nothing in the file runs on your machine. Sim reads it statically, checks it aga
4949
sim workflows source check --dir ./support-triage
5050
```
5151

52-
It exits 1 on an error, and on a missing required field, an unknown option or output path, a wrong value kind, an out-of-range value, an unwired condition or router branch or a literal secret in a block you added since the get (`--strict` fails on these in every block, except an unwired branch on a block the get printed, which the stored workflow may hold; `--no-strict` never fails on a warning). It also adds the blocks the workflow now uses to `tsconfig.json`. Your editor type-checks the file against the generated declarations, and `bunx -p typescript@7.0.2 tsc -p ./support-triage` checks it from the command line with the TypeScript release the declarations are built for; `-p ./support-triage/tsconfig.full.json` type-checks against every block.
52+
It exits 1 on an error, and on the warnings `sim workflows source check --help` lists (a missing required field, an unknown option, an unwired condition or router branch, a literal secret and more) in a block you added since the get (`--strict` fails on these in every block, except an unwired branch on a block the get printed, which the stored workflow may hold; `--no-strict` never fails on a warning). It also adds the blocks the workflow now uses to `tsconfig.json`. Your editor type-checks the file against the generated declarations, and `bunx -p typescript@7.0.2 tsc -p ./support-triage` checks it from the command line with the TypeScript release the declarations are built for; `-p ./support-triage/tsconfig.full.json` type-checks against every block.
5353

5454
3. See what changed, and whether someone changed the workflow since your `get`:
5555

‎apps/sim/lib/workspace-source/api/route-policies.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1+
import { utf8ByteLength } from '@sim/utils/paste'
12
import { boundAnswerFindings } from '@sim/workflow-code/diagnostics'
2-
import { utf8Bytes } from '@sim/workflow-code/limits'
33
import { type BlobEntry, budgetOfPath, WORKSPACE_ANSWER_BOUND } from '@sim/workspace-code'
44
import { KIND_LAYOUTS } from '@sim/workspace-code/kinds/registry'
55
import { workflowCodec, workflowFolder } from '@sim/workspace-code/kinds/workflow'
@@ -78,7 +78,7 @@ export function workspaceApplyRateCost(
7878
const kind = kindOfPath(path)
7979
if (kind === 'workflow') workflowPaths.push(path)
8080
else if (kind && budgetOfPath(KIND_LAYOUTS[kind], path) !== 'workflow') {
81-
textBytes += utf8Bytes(text)
81+
textBytes += utf8ByteLength(text)
8282
}
8383
}
8484
const { groups } = workflowCodec.group(workflowPaths)

‎apps/sim/lib/workspace-source/application/kinds/mcp/adapter.ts‎

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,7 @@
2323
import { createLogger } from '@sim/logger'
2424
import { mapWithConcurrency } from '@sim/utils/concurrency'
2525
import { getErrorMessage } from '@sim/utils/errors'
26-
import { isRecordLike } from '@sim/utils/object'
27-
import { didYouMean, type Json, type SourceSpan } from '@sim/workflow-code'
26+
import { didYouMean, type SourceSpan } from '@sim/workflow-code'
2827
import { SOURCE_ENTRY_FILE } from '@sim/workflow-code/limits'
2928
import {
3029
type BaseResource,
@@ -75,6 +74,7 @@ import {
7574
resolveStoredHeaders,
7675
storedFingerprint,
7776
} from '@/lib/workspace-source/application/kinds/mcp/stored'
77+
import { changedFields } from '@/lib/workspace-source/application/kinds/named'
7878
import {
7979
holdsIn,
8080
refuseUnreadAdopt,
@@ -122,16 +122,6 @@ function serverIdOf(ctx: SourceContext, url: string): string {
122122
return generateMcpServerId(ctx.workspace.workspaceId, url)
123123
}
124124

125-
/** The top-level declaration fields whose canonical values differ. */
126-
function changedFields(before: McpServerDecl, after: McpServerDecl): string[] {
127-
const a = mcpCodec.canonical(before)
128-
const b = mcpCodec.canonical(after)
129-
if (!isRecordLike(a) || !isRecordLike(b)) return []
130-
return Object.keys(b).filter(
131-
(field) => fingerprintOf(b[field] as Json) !== fingerprintOf(a[field] as Json)
132-
)
133-
}
134-
135125
function baseOf(record: StoredServer): BaseResource {
136126
return { id: record.id, fingerprint: storedFingerprint(record.state) }
137127
}
@@ -243,7 +233,7 @@ async function planServer(
243233
}
244234
}
245235
const record = planned.stored
246-
const changed = changedFields(declOf(record.state), decl)
236+
const changed = changedFields(mcpCodec.canonical(declOf(record.state)), mcpCodec.canonical(decl))
247237
if (planned.action === 'adopt' && !resumed) {
248238
const refused = refuseUnreadAdopt(change, record, changed)
249239
if (refused) return refused

‎apps/sim/lib/workspace-source/application/kinds/named.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ export function planNamedUndeclared<K extends NamedKindName, S>(
9090
}
9191

9292
/** The top-level declaration fields whose canonical values differ. */
93-
function changedFields(before: Json, after: Json): string[] {
93+
export function changedFields(before: Json, after: Json): string[] {
9494
if (!isRecordLike(before) || !isRecordLike(after)) return []
9595
return Object.keys(after).filter(
9696
(field) => fingerprintOf(after[field] as Json) !== fingerprintOf(before[field] as Json)

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
*/
88

99
import { mapWithConcurrency } from '@sim/utils/concurrency'
10-
import { utf8Bytes } from '@sim/workflow-code/limits'
10+
import { utf8ByteLength } from '@sim/utils/paste'
1111
import {
1212
APPLY_ORDER,
1313
type BlobEntry,
@@ -227,7 +227,7 @@ function checkLimits(
227227
const bytes: Record<SourceBudget, number> = { declaration: 0, workflow: 0, rows: 0 }
228228
for (const [path, text] of Object.entries(files)) {
229229
const kind = kindOfPath(path)
230-
if (kind) bytes[budgetOfPath(KIND_LAYOUTS[kind], path)] += utf8Bytes(text)
230+
if (kind) bytes[budgetOfPath(KIND_LAYOUTS[kind], path)] += utf8ByteLength(text)
231231
}
232232
const { maxDeclarationBytes, maxRowsBytes, maxWorkflowBytes } = WORKSPACE_SOURCE_LIMITS
233233
if (bytes.rows > maxRowsBytes) {

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { chunkArray } from '@sim/utils/helpers'
2-
import { utf8Bytes } from '@sim/workflow-code/limits'
2+
import { utf8ByteLength } from '@sim/utils/paste'
33
import {
44
APPLY_ORDER,
55
budgetOfPath,
@@ -76,7 +76,7 @@ function directoryBudget() {
7676
spend(kind: WorkspaceKind, read: { text: Record<string, string>; blobs: ReadBlob[] }) {
7777
const bytes: DirectoryBudgets = { declaration: 0, workflow: 0, rows: 0 }
7878
for (const [path, text] of Object.entries(read.text)) {
79-
bytes[budgetOfPath(adapterOf(kind).codec, path)] += utf8Bytes(text)
79+
bytes[budgetOfPath(adapterOf(kind).codec, path)] += utf8ByteLength(text)
8080
}
8181
for (const budget of SOURCE_BUDGETS) {
8282
if (spent[budget] + bytes[budget] > limits[budget]) {

‎apps/sim/lib/workspace-source/staging/store.ts‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import { db } from '@sim/db'
1515
import { workspace, workspaceSourceStagedBlob } from '@sim/db/schema'
1616
import { createLogger } from '@sim/logger'
1717
import { sha256Hex } from '@sim/security/hash'
18+
import { mapWithConcurrency } from '@sim/utils/concurrency'
1819
import { generateId, generateShortId } from '@sim/utils/id'
1920
import { WORKSPACE_SOURCE_LIMITS } from '@sim/workspace-code/limits'
2021
import { and, asc, eq, gt, inArray, isNull, lt, lte, notInArray, sql } from 'drizzle-orm'
@@ -50,6 +51,9 @@ const SWEEP_BATCH = 500
5051

5152
const STORAGE_CONTEXT = 'workspace' as const
5253

54+
/** Blob uploads one staging request runs at once; the bytes are already in memory. */
55+
const UPLOAD_CONCURRENCY = 4
56+
5357
const QUOTA_UNAVAILABLE = 'Could not check the storage quota for staging; retry shortly.'
5458

5559
/** A staged blob as an apply reads it. */
@@ -268,7 +272,9 @@ export async function stageBlobs(
268272
: null,
269273
])
270274
try {
271-
for (const blob of fresh) {
275+
// A failed upload starts no more, and throws once those running have settled, so every object
276+
// written is in `keys` by the time `finally` removes the ones no row recorded.
277+
await mapWithConcurrency(fresh, UPLOAD_CONCURRENCY, async (blob) => {
272278
const key = newStorageKey(target, blob.sha256)
273279
await uploadFile({
274280
file: blob.bytes,
@@ -280,7 +286,7 @@ export async function stageBlobs(
280286
persistMetadata: false,
281287
})
282288
keys.set(blob.sha256, key)
283-
}
289+
})
284290
recorded = await db.transaction(async (tx) => {
285291
await acquireAdvisoryXactLock(
286292
tx,

‎bun.lock‎

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)