Skip to content

Conversation

@TooTallNate
Copy link
Member

@TooTallNate TooTallNate commented Nov 19, 2025

Added support for closure variables in serialized step functions, allowing step functions to access variables from their lexical scope when executed remotely.

What changed?

  • Modified the step function serialization format to include closure variables
  • Updated the StepFunction reducer to capture and serialize closure variables
  • Enhanced the StepFunction reviver to restore closure variables in the execution context
  • Added a wrapper function that sets up the proper context with closure variables when the step function is invoked
  • Added tests to verify that step functions can access closure variables after serialization and deserialization

Why make this change?

Previously, serialized step functions could only access their arguments when executed remotely, but not variables from their lexical scope. This limitation made it difficult to write reusable step functions that needed to access shared configuration or context. This change enables more natural programming patterns by allowing step functions to access closure variables, making the code more intuitive and reducing the need to pass all context as function arguments.

@changeset-bot
Copy link

changeset-bot bot commented Nov 19, 2025

🦋 Changeset detected

Latest commit: a431eb2

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 11 packages
Name Type
@workflow/core Patch
@workflow/builders Patch
@workflow/cli Patch
@workflow/next Patch
@workflow/nitro Patch
@workflow/web-shared Patch
workflow Patch
@workflow/sveltekit Patch
@workflow/world-testing Patch
@workflow/nuxt Patch
@workflow/ai Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@vercel
Copy link
Contributor

vercel bot commented Nov 19, 2025

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

Project Deployment Preview Comments Updated (UTC)
example-nextjs-workflow-turbopack Ready Ready Preview Comment Nov 25, 2025 8:28am
example-nextjs-workflow-webpack Ready Ready Preview Comment Nov 25, 2025 8:28am
example-workflow Ready Ready Preview Comment Nov 25, 2025 8:28am
workbench-astro-workflow Error Error Nov 25, 2025 8:28am
workbench-express-workflow Ready Ready Preview Comment Nov 25, 2025 8:28am
workbench-fastify-workflow Error Error Nov 25, 2025 8:28am
workbench-hono-workflow Ready Ready Preview Comment Nov 25, 2025 8:28am
workbench-nitro-workflow Ready Ready Preview Comment Nov 25, 2025 8:28am
workbench-nuxt-workflow Ready Ready Preview Comment Nov 25, 2025 8:28am
workbench-sveltekit-workflow Ready Ready Preview Comment Nov 25, 2025 8:28am
workbench-vite-workflow Ready Ready Preview Comment Nov 25, 2025 8:28am
workflow-docs Ready Ready Preview Comment Nov 25, 2025 8:28am

Copy link
Member Author

TooTallNate commented Nov 19, 2025

Object.defineProperty(wrappedStepFn, 'name', {
value: stepFn.name,
});
Object.defineProperty(wrappedStepFn, 'stepId', {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd prefer if we renamed stepId in this PR to stepName or similar to avoid future confusion with actual stepIDs like step_<ulid>

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's fair. I named it as such here to be analogous to the workflowId property which follows the similar string format.

stepName/workflowName is perhaps less ambiguous, but in actuality the string contains both a filename and the step/workflow name. I wonder if we can pick a name that properly encompasses both pieces of information (location?). Anyways, if we do change the name let's do for both step+workflow in a separate PR.

Copy link
Member Author

TooTallNate commented Nov 25, 2025

Merge activity

  • Nov 25, 8:20 AM UTC: A user started a stack merge that includes this pull request via Graphite.
  • Nov 25, 8:22 AM UTC: Graphite couldn't merge this pull request because a downstack PR Apply SWC transformation on step functions returned from factory function #360 failed to merge.
  • Nov 25, 8:25 AM UTC: A user started a stack merge that includes this pull request via Graphite.
  • Nov 25, 8:27 AM UTC: Graphite rebased this pull request as part of a merge.
  • Nov 25, 8:28 AM UTC: Graphite couldn't merge this PR because it was not satisfying all requirements (Failed CI: 'DCO', 'Vercel – workbench-fastify-workflow').

@TooTallNate TooTallNate force-pushed the factory-with-step-method branch from 7e5fe40 to 40ab768 Compare November 25, 2025 08:24
@TooTallNate TooTallNate force-pushed the 11-19-support_closure_variables_for_serialized_step_functions branch from 2854b16 to 247fa49 Compare November 25, 2025 08:24
@TooTallNate TooTallNate changed the base branch from factory-with-step-method to graphite-base/366 November 25, 2025 08:25
@TooTallNate TooTallNate changed the base branch from graphite-base/366 to main November 25, 2025 08:25
@TooTallNate TooTallNate force-pushed the 11-19-support_closure_variables_for_serialized_step_functions branch from 247fa49 to a431eb2 Compare November 25, 2025 08:26
@TooTallNate TooTallNate merged commit 07800c2 into main Nov 25, 2025
51 of 54 checks passed
@TooTallNate TooTallNate deleted the 11-19-support_closure_variables_for_serialized_step_functions branch November 25, 2025 08:29
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.

3 participants