Skip to content

improvement(func-exec): normalize inputs to match schema#4473

Merged
icecrasher321 merged 1 commit intostagingfrom
improvement/func-exec
May 6, 2026
Merged

improvement(func-exec): normalize inputs to match schema#4473
icecrasher321 merged 1 commit intostagingfrom
improvement/func-exec

Conversation

@icecrasher321
Copy link
Copy Markdown
Collaborator

Summary

Legacy / corrupted workflow vars was leading to schema violations.

Type of Change

  • Other: Reliability Improvement

Testing

Tested manually

Checklist

  • Code follows project style guidelines
  • Self-reviewed my changes
  • Tests added/updated and passing
  • No new warnings introduced
  • I confirm that I have read and agree to the terms outlined in the Contributor License Agreement (CLA)

@vercel
Copy link
Copy Markdown

vercel Bot commented May 6, 2026

The latest updates on your projects. Learn more about Vercel for GitHub.

1 Skipped Deployment
Project Deployment Actions Updated (UTC)
docs Skipped Skipped May 6, 2026 4:47pm

Request Review

@cursor
Copy link
Copy Markdown

cursor Bot commented May 6, 2026

PR Summary

Medium Risk
Touches workflow execution boundaries (snapshots, executor context, tool/provider request building) by coercing/stripping malformed persisted values, which could change behavior for edge-case runs. Changes are mostly defensive normalization with added unit tests, so overall risk is moderate.

Overview
Hardens workflow execution against legacy/corrupted persisted state by normalizing environmentVariables, workflowVariables, selectedOutputs, and other context maps before they cross tool/provider boundaries.

Introduces shared utilities (normalizeStringArray, normalizeStringRecord, normalizeRecord, normalizeRecordMap, normalizeWorkflowVariables) and applies them across the executor (DAGExecutor, BlockExecutor streaming), snapshot creation/serialization (ExecutionSnapshot now normalizes at construction), block handlers (FunctionBlockHandler, condition/agent), provider tool execution (prepareToolExecution), and function_execute request body construction.

Adds targeted tests covering normalization behavior for arrays/records, ExecutionSnapshot, and function handler execution with malformed context inputs.

Reviewed by Cursor Bugbot for commit da50222. Configure here.

@greptile-apps
Copy link
Copy Markdown
Contributor

greptile-apps Bot commented May 6, 2026

Greptile Summary

This PR adds a normalization layer at all execution-context boundaries to handle legacy or corrupted workflow state where workflowVariables might arrive as an array instead of a record, environment variables might contain non-string values, or selectedOutputs might contain non-string elements.

  • Introduces normalizeStringArray, normalizeRecord, normalizeStringRecord, normalizeRecordMap, and normalizeWorkflowVariables utilities that sanitize unknown inputs into the shapes expected by internal APIs, replacing || {} / ?? [] fallbacks throughout handlers, providers, and tools.
  • Refactors ExecutionSnapshot to accept unknown for workflowVariables and selectedOutputs, normalizing at construction time, and moves the parseVariableValueByType in-place mutation loop to before Executor construction so typed values are visible at initialization.

Confidence Score: 4/5

Safe to merge — the change is additive normalization that degrades gracefully and is well-covered by new unit tests

The normalization utilities are correct and test coverage is solid. Two low-risk design concerns exist: normalizeWorkflowVariables silently lets a later legacy-array entry overwrite an earlier one when keys collide, and the function returns the original object reference for already-valid records, creating an implicit coupling with the in-place mutation in execution-core.ts. Neither issue manifests in the normal (non-legacy-array) path that most workflows use today.

apps/sim/lib/core/utils/records.ts — the normalizeWorkflowVariables key-collision and shared-reference behaviour are the two points most worth a second look

Important Files Changed

Filename Overview
apps/sim/lib/core/utils/records.ts New utility module introducing normalizeRecord, normalizeStringRecord, normalizeRecordMap, normalizeWorkflowVariables; returns original object reference for already-valid plain records
apps/sim/lib/core/utils/arrays.ts New normalizeStringArray utility that filters non-string elements from an array or returns [] for non-array inputs; straightforward and well-tested
apps/sim/executor/execution/snapshot.ts Constructor now accepts unknown for workflowVariables/selectedOutputs and normalizes them inline, replacing the previous parameter-level defaults
apps/sim/lib/workflows/executor/execution-core.ts Moved parseVariableValueByType in-place mutation loop to before Executor construction so the executor sees typed values; old post-construction ordering was fragile
apps/sim/executor/execution/executor.ts Constructor now normalizes envVarValues via normalizeStringRecord and workflowVariables via normalizeWorkflowVariables; selectedOutputs also normalized
apps/sim/providers/utils.ts Normalizes environmentVariables, workflowVariables, blockData, and blockNameMapping before injecting into tool execution params in prepareToolExecution
apps/sim/tools/function/execute.ts Replaces all

Flowchart

%%{init: {'theme': 'neutral'}}%%
flowchart TD
    A["ExecutionSnapshot / DAGExecutor constructor (unknown inputs)"]
    B["normalizeWorkflowVariables"]
    C{"isPlainRecord?"}
    D["Return same ref"]
    E{"Array?"}
    F["Map id/name to record"]
    G["Return {}"]
    H["normalizeStringRecord"]
    I{"isPlainRecord?"}
    J["Drop null, coerce to String"]
    K["Return {}"]
    L["normalizeStringArray"]
    M{"Array?"}
    N["Filter string elements"]
    O["Return []"]
    A --> B & H & L
    B --> C
    C -- yes --> D
    C -- no --> E
    E -- yes --> F
    E -- no --> G
    H --> I
    I -- yes --> J
    I -- no --> K
    L --> M
    M -- yes --> N
    M -- no --> O
    D & F & G & J & K & N & O --> P["Executor / Handler typed inputs"]
Loading

Comments Outside Diff (2)

  1. apps/sim/lib/core/utils/records.ts, line 79-86 (link)

    P2 When normalizeWorkflowVariables receives an array of legacy variable objects, two entries can silently collide on the same key. If one variable has id: "x" and a later entry has name: "x" (or two entries share the same id), the later entry overwrites the earlier one with no warning. In a migrated workflow with duplicate variable names this causes silent data loss.

  2. apps/sim/lib/core/utils/records.ts, line 64-67 (link)

    P2 normalizeWorkflowVariables returns the original object by reference for valid plain records. The parseVariableValueByType mutation loop in execution-core.ts relies on this: it mutates variable.value in-place on the snapshot's workflowVariables before passing the same reference to the Executor. This coupling is invisible from either call site — if either side adds a defensive copy in the future, the variable type-coercion will silently stop working. Consider documenting this contract or making the mutation an explicit separate step rather than relying on shared-reference side-effects.

Reviews (1): Last reviewed commit: "improvement(func-exec): normalize inputs..." | Re-trigger Greptile

@icecrasher321 icecrasher321 merged commit 1989a12 into staging May 6, 2026
14 checks passed
@waleedlatif1 waleedlatif1 deleted the improvement/func-exec branch May 7, 2026 05:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant