feat: Add OutputBindings to TreeAction for declarative variable binding#84
Draft
ilang898 wants to merge 1 commit into
Draft
feat: Add OutputBindings to TreeAction for declarative variable binding#84ilang898 wants to merge 1 commit into
ilang898 wants to merge 1 commit into
Conversation
Adds a new OutputBindings property to TreeAction that allows schema authors to declaratively bind ActionResponse properties to named variables. These variables are accessible via Vars.<name> in Roslyn expressions (ShouldSelect, action inputs, etc.), reducing schema verbosity and improving readability. Key changes: - TreeAction.OutputBindings: Dictionary<string, string> mapping variable names to dot-path expressions into the ActionResponse - Vars ExpandoObject on CodeGenInputParams for Roslyn expression access - Dot-path resolution supporting nested properties, array indexing, and JObject/CLR object traversal - Session-scoped variable persistence via ForgeState for rehydration - OutputVars exposed on TreeNodeContext for AfterVisitNode callbacks - Thread-safe binding resolution (post all parallel actions) - OutputBindingException for clear error reporting - 14 unit tests covering basic, cross-node, overwrite, and accessor scenarios - Schema validation rules updated for OutputBindings Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Author
@microsoft-github-policy-service agree company="Microsoft" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Adds a new
OutputBindingsproperty toTreeActionthat allows schema authors to declaratively bind ActionResponse properties to named variables. These variables are accessible viaVars.<name>in Roslyn expressions (ShouldSelect, action inputs, etc.), significantly reducing schema verbosity and improving readability.Motivation
Today,
ShouldSelectexpressions in Forge schemas must use verbose Roslyn expressions to access action output properties:Before (current):
{ "ShouldSelect": "C#|Session.GetLastActionResponse().Status == \"Success\" && ((Newtonsoft.Json.Linq.JObject)Session.GetLastActionResponse().Output)[\"Metrics\"][\"Utilization\"].Value<double>() > 80.0" }This is hard to read, error-prone, and requires deep knowledge of the Roslyn expression API. With
OutputBindings, the same logic becomes:After (with OutputBindings):
{ "Actions": { "CollectMetrics_Action": { "Action": "CollectMetricsAction", "OutputBindings": { "diagStatus": "Status", "utilization": "Output.Metrics.Utilization" } } }, "ChildSelector": [ { "ShouldSelect": "C#|Vars.diagStatus == \"Success\" && (double)Vars.utilization > 80.0", "Child": "HighUtilizationNode" } ] }How It Works
Schema Definition
OutputBindingsis an optionalDictionary<string, string>onTreeAction:Vars.<name>in expressions)ActionResponse(e.g.,Status,Output.Metrics.Score,Output.Items[0].Name)Supported Dot-Path Expressions
StatusActionResponse.Status(string)StatusCodeActionResponse.StatusCode(int)OutputActionResponse.Output(object)Output.PropertyNameOutput.Items[0]Output.Items[0].NameExpression Access
Bound variables are available on the
Varsdynamic object in all Roslyn expressions:Variable Scope & Lifetime
AfterVisitNodecallbacks viaTreeNodeContext.OutputVarsComplete Schema Example
{ "Tree": { "Root": { "Type": "Action", "Actions": { "CollectDiagnostics_Action": { "Action": "CollectDiagnosticsAction", "Input": "C#|\"RunDiagnostics\"", "OutputBindings": { "diagStatus": "Status", "diagOutput": "Output" } } }, "ChildSelector": [ { "ShouldSelect": "C#|Vars.diagStatus == \"Success\"", "Child": "SuccessNode" }, { "ShouldSelect": "C#|Vars.diagStatus != \"Success\"", "Child": "FailureNode" } ] }, "SuccessNode": { "Type": "Action", "Actions": { "ApplyFix_Action": { "Action": "ApplyFixAction", "Input": "C#|Vars.diagOutput", "OutputBindings": { "fixResult": "Status" } } }, "ChildSelector": [ { "ShouldSelect": "C#|Vars.fixResult == \"Success\"", "Child": "VerifyNode" } ] }, "FailureNode": { "Type": "Leaf" }, "VerifyNode": { "Type": "Leaf" } } }Implementation Details
Files Changed
ForgeTree.csOutputBindingsproperty toTreeActionExpressionExecutor.csVars(ExpandoObject) toCodeGenInputParams,System.Dynamicassembly refTreeWalkerSession.csResolveAllOutputBindingsForNode,ResolveOutputBindings,ResolveDotPath,CommitOutputVars,RehydrateOutputVars,GetVarITreeSession.csGetVar(string name)methodTreeNodeContext.csOutputVarsdictionary for callback accessForgeExceptions.csOutputBindingExceptionForgeSchemaValidationRules.jsonOutputBindingsto ActionDefinition schemaDesign Decisions
VarsExpandoObject.ResolveAllOutputBindingsForNodereads the committed response regardless of how it was produced.JValueto CLR types so string comparisons (Vars.status == "Success") work correctly.Items[0]foo) are rejected with descriptiveOutputBindingExceptionmessages.Breaking Change Note
ITreeSession.GetVar(string name)is a new method on the public interface. Existing implementations will need to add this method (can returnnull). This was chosen over an extension method because .NET Standard 2.0 doesn't support default interface methods.Testing
GetVaraccessor, schema validation