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
7 changes: 2 additions & 5 deletions apps/sim/lib/table/columns/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -395,10 +395,7 @@ async function stripColumnData(
columnIds: string[]
): Promise<void> {
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 },
})
}
Expand Down
7 changes: 3 additions & 4 deletions apps/sim/lib/workflows/editing/operations.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, unknown> = { loopType: 'for', count: 5 }) {
return {
blocks: {
'loop-1': {
Expand All @@ -177,7 +177,7 @@ function makeLoopWorkflow() {
enabled: true,
subBlocks: {},
outputs: {},
data: { loopType: 'for', count: 5 },
data: loopData,
},
'condition-1': {
id: 'condition-1',
Expand Down Expand Up @@ -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: '<agent1.items>',
whileCondition: '<agent1.more> === true',
Expand Down
2 changes: 1 addition & 1 deletion apps/sim/lib/workflows/editing/operations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ function renameBlock(blocks: Record<string, BlockState>, 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
Expand Down
18 changes: 10 additions & 8 deletions apps/sim/lib/workflows/references/block-rename.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@ import { escapeRegExp } from '@sim/utils/string'

/**
* `value` with every `<oldName.` reference token rewritten to `<newName.` for each entry of
* `nameMap` (normalized block names), recursing through arrays and objects. The editor's rename,
* an operations-batch rename, and a workflow import all rewrite block references through this.
* `nameMap` (normalized block names), recursing through arrays and objects; the result has the
* shape `value` had. The editor's rename, an operations-batch rename, and a workflow import all
* rewrite block references through this.
*/
export function rewriteBlockNameReferences(
value: unknown,
nameMap: ReadonlyMap<string, string>
): unknown {
export function rewriteBlockNameReferences<T>(value: T, nameMap: ReadonlyMap<string, string>): T {
return rewrite(value, nameMap) as T
}

function rewrite(value: unknown, nameMap: ReadonlyMap<string, string>): unknown {
if (typeof value === 'string') {
let updatedValue = value
nameMap.forEach((newName, oldName) => {
Expand All @@ -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<string, unknown> = {}
for (const [key, val] of Object.entries(value)) {
result[key] = rewriteBlockNameReferences(val, nameMap)
result[key] = rewrite(val, nameMap)
}
return result
}
Expand Down
24 changes: 16 additions & 8 deletions apps/sim/lib/workspace-source/application/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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`,
Expand Down Expand Up @@ -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,
})
}
Expand Down
5 changes: 1 addition & 4 deletions apps/sim/stores/workflows/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
}
})
Expand Down
6 changes: 1 addition & 5 deletions packages/sim-cli/src/commands/protocol/source-check.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
@@ -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<typeof import('./files-get')>()
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()
}
},
}
})
Expand All @@ -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(() => {
Expand All @@ -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' })
Expand Down
37 changes: 12 additions & 25 deletions packages/sim-cli/src/commands/protocol/source-dir/command-lock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -183,38 +183,25 @@ export async function withSourceDirLock<T>(
}
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()
}
}

Expand Down
5 changes: 1 addition & 4 deletions packages/sim-cli/src/commands/protocol/source-exit-codes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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':
Expand Down
4 changes: 2 additions & 2 deletions packages/workflow-code/src/diagnostics/refusals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,8 @@ const CHANGED_BLOCK_REFUSALS: ReadonlyMap<string, EditRefusalCode> = 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<string, EditRefusalCode> = new Map([
['W_UNKNOWN_OPTION', 'E_APPLY_INVALID_SUBBLOCK_FIELD'],
Expand Down
3 changes: 0 additions & 3 deletions scripts/check-test-types.baseline.json
Original file line number Diff line number Diff line change
Expand Up @@ -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
},
Expand Down
Loading