Skip to content

Commit 388013d

Browse files
committed
fix(workflow-code): review round 3: refuse deploying a workflow edited between the first write and the runs
1 parent b251fe1 commit 388013d

2 files changed

Lines changed: 47 additions & 15 deletions

File tree

‎packages/sim-cli/src/commands/protocol/workspace-source-apply.test.ts‎

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2314,11 +2314,32 @@ describe('workspaces source apply', () => {
23142314
})
23152315

23162316
await expect(apply('--yes', '--run', 'ingest={}')).rejects.toThrow(
2317-
/workflows\/ingest\/workflow\.ts changed in .* while they ran, so deployments\/ was not applied/
2317+
/workflows\/ingest\/workflow\.ts changed in .* since the apply read it, so deployments\/ was not applied/
23182318
)
23192319
expect(applies()).toHaveLength(1)
23202320
})
23212321

2322+
it('applies no deployments when a workflow file changed between the first write and the runs', async () => {
2323+
serveApplies()
2324+
// The first write's plan prints after its save and before the read ahead of the runs: the
2325+
// edit lands in that window.
2326+
let edited = false
2327+
vi.mocked(console.error).mockImplementation((line: unknown) => {
2328+
if (edited || !String(line).startsWith('workflows:')) return
2329+
edited = true
2330+
write('workflows/ingest/workflow.ts', `${INGEST}// edited after the save\n`)
2331+
})
2332+
contextMockFns.mockRequestRaw.mockResolvedValue(
2333+
runAnswer({ status: 'completed', output: {} })
2334+
)
2335+
2336+
await expect(apply('--yes', '--run', 'ingest={}')).rejects.toThrow(
2337+
/workflows\/ingest\/workflow\.ts changed in .* since the apply read it/
2338+
)
2339+
expect(edited).toBe(true)
2340+
expect(applies()).toHaveLength(1)
2341+
})
2342+
23222343
it("never prints the first write's warning that a deferred deployment's workflow is not deployed", async () => {
23232344
request.mockImplementation(async (path: string, options: { body?: { kinds?: string[] } }) => {
23242345
if (path === '/api/v2/workflows/wf_1/deployment') return deploymentStatus(1)

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

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1069,7 +1069,17 @@ export function attachWorkspaceSourceApply(source: Command): void {
10691069
}
10701070
if (!before) return finish(data)
10711071
const deployed = await afterWrite(async () => {
1072-
assertUnchangedSinceRuns(options.dir, before.read, (await readDeploymentWrite()).read)
1072+
const after = (await readDeploymentWrite()).read
1073+
const tested = before.read.files
1074+
assertUnchangedSinceRuns(options.dir, [
1075+
// The read ahead of the runs against what the first write saved (else read); its
1076+
// pruned workflow folders were never sent whole, so only its own paths count.
1077+
...changedPaths({ ...submission.files, ...data.files }, tested, Object.keys(tested)),
1078+
...changedPaths(tested, after.files, [
1079+
...Object.keys(tested),
1080+
...Object.keys(after.files),
1081+
]),
1082+
])
10731083
return runSourceWrite<ApplyData>({
10741084
...writeFlow(before.read, before.saved),
10751085
mode: 'write',
@@ -1173,23 +1183,24 @@ async function readRunInputs(runs: readonly RunRequest[]): Promise<RunRequest[]>
11731183
)
11741184
}
11751185

1186+
/** The `paths` whose text in `actual` is not the text in `expected`. */
1187+
function changedPaths(
1188+
expected: Readonly<Record<string, string>>,
1189+
actual: Readonly<Record<string, string>>,
1190+
paths: readonly string[]
1191+
): string[] {
1192+
return paths.filter((path) => expected[path] !== actual[path])
1193+
}
1194+
11761195
/**
1177-
* Refuses the deployments write of a `--run` apply when its workflow or deployment files changed
1178-
* while the runs ran (`before` was read ahead of them, `after` once they completed): deploying
1179-
* then would publish a draft, or settings, no run tested.
1196+
* Refuses the deployments write of a `--run` apply when a workflow or deployment file it sends
1197+
* `changed` since the apply read it: deploying then would publish a draft, or settings, no run
1198+
* tested.
11801199
*/
1181-
function assertUnchangedSinceRuns(
1182-
dir: string,
1183-
before: WorkspaceSubmission,
1184-
after: WorkspaceSubmission
1185-
): void {
1186-
const paths = new Set([...Object.keys(before.files), ...Object.keys(after.files)])
1187-
const changed = [...paths]
1188-
.filter((path) => before.files[path] !== after.files[path])
1189-
.sort(compareStrings)
1200+
function assertUnchangedSinceRuns(dir: string, changed: readonly string[]): void {
11901201
if (changed.length === 0) return
11911202
throw new SourceDirError(
1192-
`The apply was written and every test run completed, but ${changed.join(', ')} changed in ${dir} while they ran, so ${KIND_DIRS.deployment}/ was not applied: apply again to test and deploy what is there now.`
1203+
`The apply was written and every test run completed, but ${[...new Set(changed)].sort(compareStrings).join(', ')} changed in ${dir} since the apply read it, so ${KIND_DIRS.deployment}/ was not applied: apply again to test and deploy what is there now.`
11931204
)
11941205
}
11951206

0 commit comments

Comments
 (0)