Skip to content

Commit 15bc7dc

Browse files
committed
fix(workspace-source): say on a dry run whether the apply would refuse
The CLI guessed from each finding's kind/key whether it belonged to a held change, and E_PLAN_REF_DELETED names the held workflow while the server records it on the delete, so a dry run exited 15 for an apply that refuses. The dry run now carries refuses from the plan's own refusing list; the CLI falls back to the guess, minus that code, for older servers.
1 parent 3ad3796 commit 15bc7dc

7 files changed

Lines changed: 112 additions & 13 deletions

File tree

‎apps/docs/openapi-v2-workflows.json‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26972,6 +26972,10 @@
2697226972
},
2697326973
"description": "Every planned change, no-ops included, in apply order."
2697426974
},
26975+
"refuses": {
26976+
"description": "Set on a dry run: whether the apply would refuse. Errors of a change held back (`held`) refuse nothing else; any other error refuses. Absent on an apply.",
26977+
"type": "boolean"
26978+
},
2697526979
"applied": {
2697626980
"type": "array",
2697726981
"items": {
@@ -28266,6 +28270,10 @@
2826628270
},
2826728271
"description": "Every planned change, no-ops included, in apply order."
2826828272
},
28273+
"refuses": {
28274+
"description": "Set on a dry run: whether the apply would refuse. Errors of a change held back (`held`) refuse nothing else; any other error refuses. Absent on an apply.",
28275+
"type": "boolean"
28276+
},
2826928277
"applied": {
2827028278
"type": "array",
2827128279
"items": {

‎apps/sim/lib/api/contracts/v2/workspace-source.ts‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,12 @@ const applyDataSchema = z
301301
workspaceId: z.string().describe('Workspace id.'),
302302
dryRun: z.boolean().describe('True when nothing was written because `dryRun` was set.'),
303303
plan: z.array(changeSchema).describe('Every planned change, no-ops included, in apply order.'),
304+
refuses: z
305+
.boolean()
306+
.optional()
307+
.describe(
308+
'Set on a dry run: whether the apply would refuse. Errors of a change held back (`held`) refuse nothing else; any other error refuses. Absent on an apply.'
309+
),
304310
applied: z.array(changeSchema).describe('The changes written, in order.'),
305311
failed: z
306312
.object({

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,11 @@ export interface ApplyWorkspaceSourceResult {
7878
dryRun: boolean
7979
/** Every change the apply plans, no-ops included, in apply order. */
8080
plan: WorkspaceSourceChange[]
81+
/**
82+
* Set on a dry run: whether the apply would refuse, for an error outside the changes held back
83+
* (those refuse nothing else). Absent on an apply, which a refusal throws instead.
84+
*/
85+
refuses?: boolean
8186
/** The changes written, in the order they were written. */
8287
applied: WorkspaceSourceChange[]
8388
/**
@@ -262,6 +267,7 @@ function planned(
262267
workspaceId,
263268
dryRun,
264269
plan: plan.changes.map((change) => summarize(change)),
270+
...(dryRun && { refuses: plan.refusing.length > 0 }),
265271
applied: [],
266272
needBlobs: plan.needBlobs,
267273
files: {},

‎apps/sim/lib/workspace-source/application/references.integration.ts‎

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -324,11 +324,13 @@ describe('workspace source: secrets and credentials', () => {
324324
const missing = dryRun.diagnostics.find((finding) => finding.code === 'E_SECRET_MISSING')
325325
expect(missing?.hint).toContain('sim secrets set OPENAI_API_KEY --scope workspace')
326326
expect(held(dryRun.plan)).toEqual(['workflow:ask'])
327+
expect(dryRun.refuses).toBe(false)
327328

328329
const result = await apply(workspaceId, files)
329330
expect(result.failed).toBeUndefined()
330331
expect(actions(result.applied)).toEqual(['create workflow:plain'])
331332
expect(held(result.plan)).toEqual(['workflow:ask'])
333+
expect(result.refuses).toBeUndefined()
332334
expect(await liveWorkflowNames(workspaceId)).toEqual(['Plain'])
333335
expect(result.base.resources['workflow:ask']).toBeUndefined()
334336

@@ -353,16 +355,21 @@ describe('workspace source: secrets and credentials', () => {
353355
)
354356

355357
// The edit drops the child for an agent reading a secret the workspace lacks: it is held.
358+
const edited = {
359+
...withoutChild,
360+
'secrets.ts': SECRETS(),
361+
'workflows/parent/workflow.ts': ASK.replace("'Ask'", "'Parent'"),
362+
}
363+
// The dry run says the apply refuses, though the finding names the held workflow.
364+
const preview = await apply(workspaceId, edited, {
365+
base: created.base,
366+
allowDelete: true,
367+
dryRun: true,
368+
})
369+
expect(held(preview.plan)).toEqual(['workflow:parent'])
370+
expect(preview.refuses).toBe(true)
356371
const refused = await refusal(
357-
apply(
358-
workspaceId,
359-
{
360-
...withoutChild,
361-
'secrets.ts': SECRETS(),
362-
'workflows/parent/workflow.ts': ASK.replace("'Ask'", "'Parent'"),
363-
},
364-
{ base: created.base, allowDelete: true }
365-
)
372+
apply(workspaceId, edited, { base: created.base, allowDelete: true })
366373
)
367374
expect(findings(refused.diagnostics)).toContain('E_SECRET_MISSING workflow:parent')
368375
expect(findings(refused.diagnostics)).toContain('E_PLAN_REF_DELETED workflow:parent')

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

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1853,6 +1853,67 @@ describe('workspaces source apply', () => {
18531853
}
18541854
)
18551855

1856+
it('exits a dry run as refused when the server says it refuses, though its only error names a held change', async () => {
1857+
request.mockResolvedValue(
1858+
answer({
1859+
dryRun: true,
1860+
refuses: true,
1861+
plan: [
1862+
{ kind: 'table', key: 'leads', action: 'create' },
1863+
{ kind: 'workflow', key: 'ingest', action: 'create', held: true },
1864+
],
1865+
diagnostics: [
1866+
{
1867+
code: 'E_SECRET_MISSING',
1868+
severity: 'error',
1869+
message: 'workflows/ingest is held back: it reads the secret OPENAI_API_KEY.',
1870+
kind: 'workflow',
1871+
key: 'ingest',
1872+
},
1873+
],
1874+
})
1875+
)
1876+
1877+
await apply('--dry-run')
1878+
1879+
expect(process.exitCode).toBe(1)
1880+
})
1881+
1882+
it('exits a dry run as refused when a delete breaks a workflow it holds back, for a server without refuses, as the apply refuses it', async () => {
1883+
// E_PLAN_REF_DELETED names the held workflow, but the server records it on the delete (or the
1884+
// plan), which no hold excuses: the apply would refuse, so the dry run cannot exit 15.
1885+
request.mockResolvedValue(
1886+
answer({
1887+
dryRun: true,
1888+
plan: [
1889+
{ kind: 'table', key: 'leads', action: 'create' },
1890+
{ kind: 'workflow', key: 'parent', action: 'update', held: true },
1891+
{ kind: 'workflow', key: 'child', action: 'delete' },
1892+
],
1893+
diagnostics: [
1894+
{
1895+
code: 'E_SECRET_MISSING',
1896+
severity: 'error',
1897+
message: 'workflows/parent is held back: it reads the secret OPENAI_API_KEY.',
1898+
kind: 'workflow',
1899+
key: 'parent',
1900+
},
1901+
{
1902+
code: 'E_PLAN_REF_DELETED',
1903+
severity: 'error',
1904+
message: 'workflows/parent uses workflow "child", which this apply deletes.',
1905+
kind: 'workflow',
1906+
key: 'parent',
1907+
},
1908+
],
1909+
})
1910+
)
1911+
1912+
await apply('--dry-run', '--allow-delete')
1913+
1914+
expect(process.exitCode).toBe(1)
1915+
})
1916+
18561917
it('saves the applied answer when the catalog of a workflow it reprinted cannot be fetched, and says its edges are unrecorded', async () => {
18571918
const configDir = mkdtempSync(join(tmpdir(), 'sim-workspace-apply-config-'))
18581919
vi.stubEnv('SIM_CONFIG_DIR', configDir)

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

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -124,18 +124,27 @@ function failedExitCode(failed: NonNullable<ApplyData['failed']>): number {
124124
: refusedExitCode([{ code: failed.code, severity: 'error' }])
125125
}
126126

127-
/** Whether `finding` is about a change the plan holds back, which refuses nothing else. */
127+
/**
128+
* Whether `finding` is about a change the plan holds back, which refuses nothing else: one naming
129+
* that change. For a server that predates `refuses` only; `E_PLAN_REF_DELETED` names the workflow
130+
* that uses what a delete removes but sits on the delete or the plan, so no hold excuses it.
131+
*/
128132
function isHeldFinding(
129133
plan: readonly Change[],
130134
finding: ApplyData['diagnostics'][number]
131135
): boolean {
132-
return plan.some(
133-
(change) => change.held && change.kind === finding.kind && change.key === finding.key
136+
return (
137+
finding.code !== 'E_PLAN_REF_DELETED' &&
138+
plan.some((change) => change.held && change.kind === finding.kind && change.key === finding.key)
134139
)
135140
}
136141

137-
/** Whether a dry run's errors refuse the apply: any error but those of changes held back. */
142+
/**
143+
* Whether a dry run's errors refuse the apply: the server's answer (`refuses`), or for a server
144+
* that predates it, any error but those of changes held back.
145+
*/
138146
function refusesApply(data: ApplyData): boolean {
147+
if (data.refuses !== undefined) return data.refuses
139148
return data.diagnostics.some(
140149
(finding) => finding.severity === 'error' && !isHeldFinding(data.plan, finding)
141150
)

‎packages/sim-cli/src/generated/v2-api.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1191,6 +1191,7 @@ export type ApplyWorkspaceSourceResponse = {
11911191
id?: string
11921192
held?: true
11931193
}>
1194+
refuses?: boolean
11941195
applied: Array<{
11951196
kind:
11961197
| 'secret'
@@ -14768,6 +14769,7 @@ export type PlanWorkspaceSourceResponse = {
1476814769
id?: string
1476914770
held?: true
1477014771
}>
14772+
refuses?: boolean
1477114773
applied: Array<{
1477214774
kind:
1477314775
| 'secret'

0 commit comments

Comments
 (0)