Skip to content

Commit 8ef9a45

Browse files
fix(env-vars): refactor for workspace/personal env vars to work with server side execution correctly (#2197)
* fix(env-var-resolution): new executor env var resolution changes * add sessionuser id" * cleanup code * add doc update * fix build * fix client session pass through" * add type change * fix env var with hitl * fix types
1 parent ca818a6 commit 8ef9a45

File tree

18 files changed

+476
-616
lines changed

18 files changed

+476
-616
lines changed

apps/docs/content/docs/en/variables/environment-variables.mdx

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -66,16 +66,16 @@ To reference environment variables in your workflows, use the `{{}}` notation. W
6666
height={350}
6767
/>
6868

69-
## Variable Precedence
69+
## How Variables are Resolved
7070

71-
When you have both personal and workspace variables with the same name:
71+
**Workspace variables always take precedence** over personal variables, regardless of who runs the workflow.
7272

73-
1. **Workspace variables take precedence** over personal variables
74-
2. This prevents naming conflicts and ensures consistent behavior across team workflows
75-
3. If a workspace variable exists, the personal variable with the same name is ignored
73+
When no workspace variable exists for a key, personal variables are used:
74+
- **Manual runs (UI)**: Your personal variables
75+
- **Automated runs (API, webhook, schedule, deployed chat)**: Workflow owner's personal variables
7676

77-
<Callout type="warning">
78-
Choose variable names carefully to avoid unintended overrides. Consider prefixing personal variables with your initials or workspace variables with the project name.
77+
<Callout type="info">
78+
Personal variables are best for testing. Use workspace variables for production workflows.
7979
</Callout>
8080

8181
## Security Best Practices

apps/sim/app/api/workflows/[id]/execute/route.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ const ExecuteWorkflowSchema = z.object({
3434
stream: z.boolean().optional(),
3535
useDraftState: z.boolean().optional(),
3636
input: z.any().optional(),
37-
// Optional workflow state override (for executing diff workflows)
37+
isClientSession: z.boolean().optional(),
3838
workflowStateOverride: z
3939
.object({
4040
blocks: z.record(z.any()),
@@ -92,16 +92,17 @@ export async function executeWorkflow(
9292
workflowId,
9393
workspaceId: workflow.workspaceId,
9494
userId: actorUserId,
95+
workflowUserId: workflow.userId,
9596
triggerType,
9697
useDraftState: false,
9798
startTime: new Date().toISOString(),
99+
isClientSession: false,
98100
}
99101

100102
const snapshot = new ExecutionSnapshot(
101103
metadata,
102104
workflow,
103105
input,
104-
{},
105106
workflow.variables || {},
106107
streamConfig?.selectedOutputs || []
107108
)
@@ -329,6 +330,7 @@ export async function POST(req: NextRequest, { params }: { params: Promise<{ id:
329330
stream: streamParam,
330331
useDraftState,
331332
input: validatedInput,
333+
isClientSession = false,
332334
workflowStateOverride,
333335
} = validation.data
334336

@@ -503,17 +505,19 @@ export async function POST(req: NextRequest, { params }: { params: Promise<{ id:
503505
workflowId,
504506
workspaceId: workflow.workspaceId ?? undefined,
505507
userId: actorUserId,
508+
sessionUserId: isClientSession ? userId : undefined,
509+
workflowUserId: workflow.userId,
506510
triggerType,
507511
useDraftState: shouldUseDraftState,
508512
startTime: new Date().toISOString(),
513+
isClientSession,
509514
workflowStateOverride: effectiveWorkflowStateOverride,
510515
}
511516

512517
const snapshot = new ExecutionSnapshot(
513518
metadata,
514519
workflow,
515520
processedInput,
516-
{},
517521
workflow.variables || {},
518522
selectedOutputs
519523
)
@@ -769,17 +773,19 @@ export async function POST(req: NextRequest, { params }: { params: Promise<{ id:
769773
workflowId,
770774
workspaceId: workflow.workspaceId ?? undefined,
771775
userId: actorUserId,
776+
sessionUserId: isClientSession ? userId : undefined,
777+
workflowUserId: workflow.userId,
772778
triggerType,
773779
useDraftState: shouldUseDraftState,
774780
startTime: new Date().toISOString(),
781+
isClientSession,
775782
workflowStateOverride: effectiveWorkflowStateOverride,
776783
}
777784

778785
const snapshot = new ExecutionSnapshot(
779786
metadata,
780787
workflow,
781788
processedInput,
782-
{},
783789
workflow.variables || {},
784790
selectedOutputs
785791
)

apps/sim/app/workspace/[workspaceId]/w/[workflowId]/hooks/use-workflow-execution.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -879,7 +879,7 @@ export function useWorkflowExecution() {
879879
selectedOutputs,
880880
triggerType: overrideTriggerType || 'manual',
881881
useDraftState: true,
882-
// Pass diff workflow state if available for execution
882+
isClientSession: true,
883883
workflowStateOverride: executionWorkflowState
884884
? {
885885
blocks: executionWorkflowState.blocks,

apps/sim/app/workspace/[workspaceId]/w/[workflowId]/utils/workflow-execution-utils.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,12 @@ export async function executeWorkflowWithFullLogging(
2727
const executionId = options.executionId || uuidv4()
2828
const { addConsole } = useTerminalConsoleStore.getState()
2929

30-
// Build request payload
3130
const payload: any = {
3231
input: options.workflowInput,
3332
stream: true,
3433
triggerType: options.overrideTriggerType || 'manual',
3534
useDraftState: true,
35+
isClientSession: true,
3636
}
3737

3838
const response = await fetch(`/api/workflows/${activeWorkflowId}/execute`, {

0 commit comments

Comments
 (0)