elsa-core icon indicating copy to clipboard operation
elsa-core copied to clipboard

Restore Literal handling in ActivityExecutionContext.TryGet

Open Copilot opened this issue 3 months ago • 0 comments

Version 3.5.2 removed code that handled Literal objects in ActivityExecutionContext.TryGet(), breaking the common pattern of programmatically creating activities with literal inputs. This throws InvalidOperationException: The memory block 'Elsa.Expressions.Models.Literal1[T]' does not exist.`

Root Cause

When Input<T> is constructed with a Literal, the literal becomes the MemoryBlockReference. Unlike variables, literals hold values directly rather than storing them in the memory register. Without special handling, TryGet attempts to look up the literal in the register and fails.

Changes

  • Restored literal check in ActivityExecutionContext.TryGet(): Added early return when blockReference is Literal to retrieve the value directly
  • Added test coverage: Integration test reproducing the reported issue, unit tests validating literal behavior
public bool TryGet(MemoryBlockReference blockReference, out object? value)
{
    if (blockReference is Literal literal)
    {
        value = literal.Value;
        return true;
    }
    // ... existing lookup logic
}

Use Case

Activities that dynamically instantiate and configure other activities with literal values:

var activity = new SetVariable
{
    Variable = myVar,
    Value = new Input<string>(new Literal<string>("constant value"))
};
// Accessing the input now works instead of throwing
Original prompt

This section details on the original issue you should resolve

<issue_title>ActivityExecutionContext doesn't handle dynamic Literal's anymore</issue_title> <issue_description>## Description In version 3.5.2 the code fragment if (blockReference is Literal literal) { value = literal.Value; return true; } Is removed in ActivityExecutionContext:TryGet(MemoryBlockReference blockReference)

Steps to Reproduce

We have a Activity that re-uses an other activity execute method by creating an instance and setting the inputs of that activity with literals. Since 3.5.2 this fails because Get cannot find the memory block

System.InvalidOperationException HResult=0x80131509 Message=The memory block 'Elsa.Expressions.Models.Literal1[System.Object]' does not exist. Source=Elsa.Workflows.Core StackTrace: at Elsa.Workflows.ActivityExecutionContext.Get(MemoryBlockReference blockReference) in /_/src/modules/Elsa.Workflows.Core/Contexts/ActivityExecutionContext.cs:line 726 at Elsa.Workflows.ActivityExecutionContext.Get[T](MemoryBlockReference blockReference) in /_/src/modules/Elsa.Workflows.Core/Contexts/ActivityExecutionContext.cs:line 739 at Elsa.Workflows.ActivityExecutionContext.Get[T](Input1 input) in /_/src/modules/Elsa.Workflows.Core/Contexts/ActivityExecutionContext.cs:line 701</issue_description>

<agent_instructions>See if you can gather some more context to understand the removal of the mentioned code, and what the consequence might be to put it back.</agent_instructions>

Comments on the Issue (you are @copilot in this section)

@sfmskywalker I'm not 100% sure, but I usually have a reason to remove things. But, it's always possible I have't considered enough angles. Would you be able to provide steps to reproduce?
  • Fixes elsa-workflows/elsa-core#7074

💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.


This change is Reviewable

Copilot avatar Nov 19 '25 19:11 Copilot