From 21aeffef46f9d6e76069ecd8890d1f4eb945365d Mon Sep 17 00:00:00 2001 From: Waleed Latif Date: Fri, 20 Nov 2026 19:35:01 -0800 Subject: [PATCH 1/5] refactor(sim-cli): remove the directory lock and uninstall its interrupt release in one synchronous step An interrupt is handled only between event-loop turns, so a synchronous rmSync followed by the release's disposal leaves no turn in which the release could remove a lock another command took, or find this one half removed. That makes the owner-record check the last commits layered on unnecessary; it is gone. --- .../protocol/source-dir-lock-release.test.ts | 61 +++++++------------ .../protocol/source-dir/command-lock.ts | 37 ++++------- 2 files changed, 34 insertions(+), 64 deletions(-) diff --git a/packages/sim-cli/src/commands/protocol/source-dir-lock-release.test.ts b/packages/sim-cli/src/commands/protocol/source-dir-lock-release.test.ts index 89c0b9584ac..26d7523a8ab 100644 --- a/packages/sim-cli/src/commands/protocol/source-dir-lock-release.test.ts +++ b/packages/sim-cli/src/commands/protocol/source-dir-lock-release.test.ts @@ -1,26 +1,24 @@ -import { - existsSync, - mkdirSync, - mkdtempSync, - realpathSync, - rmSync, - symlinkSync, - writeFileSync, -} from 'node:fs' +import { existsSync, mkdtempSync, realpathSync, rmSync, symlinkSync } from 'node:fs' import { tmpdir } from 'node:os' import { join } from 'node:path' import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' -/** What each installed interrupt release would remove, read when the signal arrives. */ -const signalTargets: Array<() => string | null> = [] +/** Each installed interrupt release: what it would remove, and whether the lock was there when it was uninstalled. */ +const releases: Array<{ target: () => string | null; lockAtDispose?: boolean }> = [] +let lockPath = '' vi.mock('./files-get', async (importOriginal) => { const actual = await importOriginal() return { ...actual, removeStagingOnSignal: (target: () => string | null) => { - signalTargets.push(target) - return actual.removeStagingOnSignal(target) + const release: (typeof releases)[number] = { target } + releases.push(release) + const dispose = actual.removeStagingOnSignal(target) + return () => { + release.lockAtDispose = existsSync(lockPath) + dispose() + } }, } }) @@ -31,7 +29,8 @@ let dir: string beforeEach(() => { dir = realpathSync(mkdtempSync(join(tmpdir(), 'sim-source-lock-release-'))) - signalTargets.length = 0 + lockPath = join(dir, SOURCE_DIR_LOCK) + releases.length = 0 }) afterEach(() => { @@ -42,42 +41,26 @@ describe('the interrupt release of a directory lock', () => { it('targets the lock while the command holds it', async () => { let held: string | null = null await withSourceDirLock(dir, async () => { - held = signalTargets[0]?.() ?? null + held = releases[0]?.target() ?? null }) - expect(held).toBe(join(dir, SOURCE_DIR_LOCK)) + expect(held).toBe(lockPath) }) - it('targets nothing once the command removed its lock, so an interrupt during the rest of its cleanup leaves the next command’s lock', async () => { - await withSourceDirLock(join(dir, 'out/a'), async () => {}) - const next = join(dir, 'out/a', SOURCE_DIR_LOCK) - mkdirSync(next, { recursive: true }) + it('is uninstalled only once the lock is gone, so it never outlives the lock it removes', async () => { + await withSourceDirLock(dir, async () => {}) - expect(signalTargets[0]?.()).toBeNull() - writeFileSync(join(next, 'owner.json'), JSON.stringify({ pid: process.pid + 1, host: 'other' })) - expect(signalTargets[0]?.()).toBeNull() - expect(existsSync(next)).toBe(true) + expect(releases).toHaveLength(1) + expect(releases[0].lockAtDispose).toBe(false) }) - it('targets nothing when the lock at the path names another command', async () => { - let target: string | null = 'unread' - await withSourceDirLock(dir, async () => { - writeFileSync( - join(dir, SOURCE_DIR_LOCK, 'owner.json'), - JSON.stringify({ pid: process.pid + 1, host: 'other' }) - ) - target = signalTargets[0]?.() ?? null - }) - - expect(target).toBeNull() - }) - it('uninstalls the interrupt release even when removing the lock fails', async () => { + it('is uninstalled even when removing the lock fails', async () => { const listening = process.listenerCount('SIGINT') const outside = realpathSync(mkdtempSync(join(tmpdir(), 'sim-source-lock-outside-'))) const failed = withSourceDirLock(dir, async () => { - rmSync(join(dir, SOURCE_DIR_LOCK), { recursive: true, force: true }) - symlinkSync(outside, join(dir, SOURCE_DIR_LOCK)) + rmSync(lockPath, { recursive: true, force: true }) + symlinkSync(outside, lockPath) }) await expect(failed).rejects.toMatchObject({ code: 'SOURCE_SYMLINK' }) diff --git a/packages/sim-cli/src/commands/protocol/source-dir/command-lock.ts b/packages/sim-cli/src/commands/protocol/source-dir/command-lock.ts index c44b4d0e5ae..5a05366adc9 100644 --- a/packages/sim-cli/src/commands/protocol/source-dir/command-lock.ts +++ b/packages/sim-cli/src/commands/protocol/source-dir/command-lock.ts @@ -3,7 +3,7 @@ * rewrites the directory, so two commands on it never interleave their writes. */ -import { readFileSync, type Stats } from 'node:fs' +import { rmSync, type Stats } from 'node:fs' import { lstat, mkdir, readFile, realpath, rm, rmdir, stat, writeFile } from 'node:fs/promises' import { hostname } from 'node:os' import { dirname, join, resolve } from 'node:path' @@ -183,38 +183,25 @@ export async function withSourceDirLock( } const owner: LockOwner = { pid: process.pid, host: hostname() } // An interrupt (Ctrl-C) releases the lock too: a lock left behind is taken over only by a - // command on the same host, so one left on a folder shared with a container would stay. - // Once this command removes its lock the path may become another command's, which an interrupt - // must leave alone: past the owner record, the release removes the lock only while it still - // names this command. - let ownerRecorded = false - const ownedLock = (): string | null => { - if (!ownerRecorded) return lock - try { - const recorded = toRecord(JSON.parse(readFileSync(join(lock, LOCK_OWNER_FILE), 'utf8'))) - return recorded.pid === owner.pid && recorded.host === owner.host ? lock : null - } catch { - return null - } - } - const disposeSignalRelease = removeStagingOnSignal(ownedLock) + // command on the same host, so one left on a folder shared with a container would stay. The + // release stays installed until the lock is gone, since an interrupt a deferred write held is + // raised again as `run` ends and would otherwise end the process mid-removal. + const disposeSignalRelease = removeStagingOnSignal(() => lock) try { await writeFile(join(lock, LOCK_OWNER_FILE), JSON.stringify(owner)) - ownerRecorded = true return await run() } finally { - // The signal release stays installed until the lock is gone: an interrupt a deferred write - // held is raised again as `run` ends, and with no handler it would end the process mid-removal. + // An interrupt is handled only between turns of the event loop, so removing the lock and + // uninstalling its release in one synchronous step leaves no turn in which the release could + // remove a lock another command took after this one, or find this one half removed. try { - await rm(await checkedPath(root, SOURCE_DIR_LOCK, new Set()), { - recursive: true, - force: true, - }) - // Folders the lock alone created go with it; one the command filled stays. - await removeCreatedFolders() + const held = await checkedPath(root, SOURCE_DIR_LOCK, new Set()) + rmSync(held, { recursive: true, force: true }) } finally { disposeSignalRelease() } + // Folders the lock alone created go with it; one the command filled stays. + await removeCreatedFolders() } } From 3f5aa9310773eef9f7c89860f97cab728542b211 Mon Sep 17 00:00:00 2001 From: Waleed Latif Date: Fri, 20 Nov 2026 19:35:02 -0800 Subject: [PATCH 2/5] refactor(workflows): type the block rename rewrite by its input, dropping the casts at its callers --- apps/sim/lib/workflows/editing/operations.ts | 2 +- .../lib/workflows/references/block-rename.ts | 18 ++++++++++-------- apps/sim/stores/workflows/utils.ts | 5 +---- 3 files changed, 12 insertions(+), 13 deletions(-) diff --git a/apps/sim/lib/workflows/editing/operations.ts b/apps/sim/lib/workflows/editing/operations.ts index b4c3002aca0..452dcec06cf 100644 --- a/apps/sim/lib/workflows/editing/operations.ts +++ b/apps/sim/lib/workflows/editing/operations.ts @@ -81,7 +81,7 @@ function renameBlock(blocks: Record, blockId: string, name: if (id === blockId) continue for (const subBlock of Object.values(block.subBlocks ?? {})) { if (subBlock.value === undefined || subBlock.value === null) continue - subBlock.value = rewriteBlockNameReferences(subBlock.value, nameMap) as typeof subBlock.value + subBlock.value = rewriteBlockNameReferences(subBlock.value, nameMap) } const data = block.data if (!data || (block.type !== 'loop' && block.type !== 'parallel')) continue diff --git a/apps/sim/lib/workflows/references/block-rename.ts b/apps/sim/lib/workflows/references/block-rename.ts index 09accb96ceb..5b9733a9060 100644 --- a/apps/sim/lib/workflows/references/block-rename.ts +++ b/apps/sim/lib/workflows/references/block-rename.ts @@ -2,13 +2,15 @@ import { escapeRegExp } from '@sim/utils/string' /** * `value` with every ` -): unknown { +export function rewriteBlockNameReferences(value: T, nameMap: ReadonlyMap): T { + return rewrite(value, nameMap) as T +} + +function rewrite(value: unknown, nameMap: ReadonlyMap): unknown { if (typeof value === 'string') { let updatedValue = value nameMap.forEach((newName, oldName) => { @@ -34,12 +36,12 @@ export function rewriteBlockNameReferences( return updatedValue } if (Array.isArray(value)) { - return value.map((item) => rewriteBlockNameReferences(item, nameMap)) + return value.map((item) => rewrite(item, nameMap)) } if (value && typeof value === 'object') { const result: Record = {} for (const [key, val] of Object.entries(value)) { - result[key] = rewriteBlockNameReferences(val, nameMap) + result[key] = rewrite(val, nameMap) } return result } diff --git a/apps/sim/stores/workflows/utils.ts b/apps/sim/stores/workflows/utils.ts index ff64e4885e0..264aec0c3a1 100644 --- a/apps/sim/stores/workflows/utils.ts +++ b/apps/sim/stores/workflows/utils.ts @@ -161,10 +161,7 @@ function updateBlockReferences( } if (subBlock.value !== undefined && subBlock.value !== null) { - const updatedValue = rewriteBlockNameReferences( - subBlock.value, - nameMap - ) as SubBlockState['value'] + const updatedValue = rewriteBlockNameReferences(subBlock.value, nameMap) block.subBlocks[subBlockId] = { ...subBlock, value: updatedValue } } }) From 08e5e5d81dd35b135894228475cadd4158cd557d Mon Sep 17 00:00:00 2001 From: Waleed Latif Date: Fri, 20 Nov 2026 19:35:03 -0800 Subject: [PATCH 3/5] test(workflows): build the loop fixture's data from a parameter instead of mutating it, and shrink the test-types baseline --- apps/sim/lib/workflows/editing/operations.test.ts | 7 +++---- scripts/check-test-types.baseline.json | 3 --- 2 files changed, 3 insertions(+), 7 deletions(-) diff --git a/apps/sim/lib/workflows/editing/operations.test.ts b/apps/sim/lib/workflows/editing/operations.test.ts index 71691cd00c2..329ade91be5 100644 --- a/apps/sim/lib/workflows/editing/operations.test.ts +++ b/apps/sim/lib/workflows/editing/operations.test.ts @@ -166,7 +166,7 @@ mockGetBlock.mockImplementation((type: string) => MOCK_REGISTRY_BLOCKS[type]) vi.mock('@/lib/integrations/availability.server', () => integrationsAvailabilityMock) -function makeLoopWorkflow() { +function makeLoopWorkflow(loopData: Record = { loopType: 'for', count: 5 }) { return { blocks: { 'loop-1': { @@ -177,7 +177,7 @@ function makeLoopWorkflow() { enabled: true, subBlocks: {}, outputs: {}, - data: { loopType: 'for', count: 5 }, + data: loopData, }, 'condition-1': { id: 'condition-1', @@ -691,8 +691,7 @@ describe('a block rename', () => { }) it('rewrites references a loop holds in its collection and conditions, and in the loop record', () => { - const workflow = makeLoopWorkflow() - Object.assign(workflow.blocks['loop-1'].data, { + const workflow = makeLoopWorkflow({ loopType: 'forEach', collection: '', whileCondition: ' === true', diff --git a/scripts/check-test-types.baseline.json b/scripts/check-test-types.baseline.json index 788bf846153..c7870d0d293 100644 --- a/scripts/check-test-types.baseline.json +++ b/scripts/check-test-types.baseline.json @@ -2283,9 +2283,6 @@ "apps/sim/lib/workflows/dynamic-handle-topology.test.ts": { "TS2352": 1 }, - "apps/sim/lib/workflows/editing/operations.test.ts": { - "TS2353": 1 - }, "apps/sim/lib/workflows/editing/validation.test.ts": { "TS2322": 1 }, From d013b0b02bfa57a8c0138fe1406e36813c9a157c Mon Sep 17 00:00:00 2001 From: Waleed Latif Date: Fri, 20 Nov 2026 19:35:04 -0800 Subject: [PATCH 4/5] refactor(table): write the column strip's row condition as one sql clause, without a type cast --- apps/sim/lib/table/columns/service.ts | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/apps/sim/lib/table/columns/service.ts b/apps/sim/lib/table/columns/service.ts index 14fb8a0a5fb..b1da99383b9 100644 --- a/apps/sim/lib/table/columns/service.ts +++ b/apps/sim/lib/table/columns/service.ts @@ -20,7 +20,7 @@ import { userTableDefinitions, userTableRows } from '@sim/db/schema' import { createLogger } from '@sim/logger' import { resolveCurrencyCode } from '@sim/table-cells/currency' import { omit } from '@sim/utils/object' -import { and, asc, count, eq, gt, type SQL, sql } from 'drizzle-orm' +import { and, asc, count, eq, gt, sql } from 'drizzle-orm' import { OrchestrationError } from '@/lib/core/orchestration/types' import { columnMatchesRef, generateColumnId, getColumnId } from '@/lib/table/column-keys' import { @@ -395,10 +395,7 @@ async function stripColumnData( columnIds: string[] ): Promise { await updateTableRowsWithDerivedSecretProvenance(trx, { - rowWhere: and( - eq(userTableRows.tableId, tableId), - eq(userTableRows.workspaceId, workspaceId) - ) as SQL, + rowWhere: sql`${userTableRows.tableId} = ${tableId} and ${userTableRows.workspaceId} = ${workspaceId}`, transformation: { mode: 'remove-columns', columnIds }, }) } From 4d904003d14c966fdf201e1253b747628476a16b Mon Sep 17 00:00:00 2001 From: Waleed Latif Date: Fri, 20 Nov 2026 19:35:05 -0800 Subject: [PATCH 5/5] refactor(workflow-code): name the name-taken hint, inline the one-field option check, and share the exit-code text --- .../workspace-source/application/errors.ts | 24 ++++++++++++------- .../src/commands/protocol/source-check.ts | 6 +---- .../commands/protocol/source-exit-codes.ts | 5 +--- .../workflow-code/src/diagnostics/refusals.ts | 4 ++-- 4 files changed, 20 insertions(+), 19 deletions(-) diff --git a/apps/sim/lib/workspace-source/application/errors.ts b/apps/sim/lib/workspace-source/application/errors.ts index 458bb4aa546..3e9fc8a1416 100644 --- a/apps/sim/lib/workspace-source/application/errors.ts +++ b/apps/sim/lib/workspace-source/application/errors.ts @@ -95,6 +95,21 @@ export function isRefusal(error: unknown): error is OrchestrationError { ) } +/** What to do about a name or key another resource holds, by what holds it and where it is declared. */ +function nameTakenHint(error: unknown, kind: WorkspaceKind): string { + if (error instanceof ResourceKeyTakenError) { + return 'Rename the key, or delete the resource that holds it.' + } + if (error instanceof WorkspaceFileFolderConflictError) { + return 'Rename that folder in the file paths locally, or rename or delete the folder that holds the name in Sim.' + } + // A file has no declaration: its name is its path in the directory. + if (kind === 'file') { + return 'Rename it locally, or rename or delete the file that holds the name in Sim.' + } + return 'Rename it in its declaration, or rename or delete the resource that holds the name in Sim.' +} + /** * A child use case's refusal as a finding about one resource: a resource (or a table's rows) * changed since the plan read it is `E_APPLY_STALE`, forbidden is `E_APPLY_FORBIDDEN`, @@ -131,14 +146,7 @@ export function refusalDiagnostic( } if (error instanceof ResourceKeyTakenError || isNameTaken(error)) { return finding('E_NAME_TAKEN', error.message, { - hint: - error instanceof ResourceKeyTakenError - ? 'Rename the key, or delete the resource that holds it.' - : error instanceof WorkspaceFileFolderConflictError - ? 'Rename that folder in the file paths locally, or rename or delete the folder that holds the name in Sim.' - : resource.kind === 'file' - ? 'Rename it locally, or rename or delete the file that holds the name in Sim.' - : 'Rename it in its declaration, or rename or delete the resource that holds the name in Sim.', + hint: nameTakenHint(error, resource.kind), ...resource, }) } diff --git a/packages/sim-cli/src/commands/protocol/source-check.ts b/packages/sim-cli/src/commands/protocol/source-check.ts index ccc0e66ba23..2705043c6e4 100644 --- a/packages/sim-cli/src/commands/protocol/source-check.ts +++ b/packages/sim-cli/src/commands/protocol/source-check.ts @@ -560,13 +560,9 @@ export async function checkWorkflowDir( if (finding.code !== 'W_UNKNOWN_OPTION' || blockKey === undefined || param === undefined) { return false } - return optionsVaryOn(blockKey, (id) => id === param) - } - /** Whether a field of block `blockKey` that `matches` takes options varying by deployment. */ - const optionsVaryOn = (blockKey: string, matches: (subBlockId: string) => boolean) => { const type = compiled.workflow ? getOwn(compiled.workflow.blocks, blockKey)?.type : undefined const block = type === undefined ? undefined : catalogBlock(compiledDir.catalog, type) - return block?.subBlocks.some((sub) => matches(sub.id) && sub.optionsVary === true) === true + return block?.subBlocks.some((sub) => sub.id === param && sub.optionsVary === true) === true } const exempt = (finding: (typeof compiled.diagnostics)[number]) => mayBeRenamed(finding) || variesByDeployment(finding) diff --git a/packages/sim-cli/src/commands/protocol/source-exit-codes.ts b/packages/sim-cli/src/commands/protocol/source-exit-codes.ts index c991a51918f..24d9d4a8bd8 100644 --- a/packages/sim-cli/src/commands/protocol/source-exit-codes.ts +++ b/packages/sim-cli/src/commands/protocol/source-exit-codes.ts @@ -69,10 +69,7 @@ function sourceExitCodeRows(prefix: SourceCommandGroup): SourceExitCodeRow[] { }, { code: 1, - meaning: - prefix === 'sim workspaces source' - ? 'The source has errors (each printed as `file:line:column` with a hint), a locked block (`E_APPLY_LOCKED_BLOCK`), a name another resource holds (`E_NAME_TAKEN`), a cancelled prompt, or the command failed' - : 'The source has errors (each printed as `file:line:column` with a hint), a locked block (`E_APPLY_LOCKED_BLOCK`), a name another workflow holds (`SOURCE_NAME_TAKEN`), a cancelled prompt, or the command failed', + meaning: `The source has errors (each printed as \`file:line:column\` with a hint), a locked block (\`E_APPLY_LOCKED_BLOCK\`), ${prefix === 'sim workspaces source' ? 'a name another resource holds (`E_NAME_TAKEN`)' : 'a name another workflow holds (`SOURCE_NAME_TAKEN`)'}, a cancelled prompt, or the command failed`, next: 'Fix what it printed and run it again', help: { 'workflows source apply': diff --git a/packages/workflow-code/src/diagnostics/refusals.ts b/packages/workflow-code/src/diagnostics/refusals.ts index f6f3a127694..6eb96842440 100644 --- a/packages/workflow-code/src/diagnostics/refusals.ts +++ b/packages/workflow-code/src/diagnostics/refusals.ts @@ -98,8 +98,8 @@ const CHANGED_BLOCK_REFUSALS: ReadonlyMap = new Map([ * changed: a dropdown value none of its options names, which the server's field gate refuses. A * value the stored block already holds outside its options prints as `raw()`, which the compile * does not check, and one written bare again is not a field the edit set, so only a value the - * author changed is refused. `operation` is left out: an unknown one is already an error, and its warning means the - * stored block holds a retired operation, which the gate keeps. + * author changed is refused. `operation` is left out: an unknown one is already an error, and its + * warning means the stored block holds a retired operation, which the gate keeps. */ const PARAM_REFUSALS: ReadonlyMap = new Map([ ['W_UNKNOWN_OPTION', 'E_APPLY_INVALID_SUBBLOCK_FIELD'],