Skip to content

Commit 274687d

Browse files
committed
follow recommendation
1 parent 5bb2585 commit 274687d

2 files changed

Lines changed: 26 additions & 4 deletions

File tree

internal/agent/component/begin.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -105,19 +105,19 @@ func (b *BeginComponent) Invoke(ctx context.Context, db *gorm.DB, inputs map[str
105105
for _, field := range b.inputFields {
106106
if _, direct := inputs[field]; !direct {
107107
if query, scalar := inputs["query"].(string); scalar {
108-
if value, initialized := state.Globals["begin@"+field]; initialized {
108+
if value, initialized := state.GetGlobal("begin@" + field); initialized {
109109
out[field] = value
110110
continue
111111
}
112112
out[field] = query
113-
state.Globals["begin@"+field] = query
113+
state.SetGlobal("begin@"+field, query)
114114
continue
115115
}
116116
}
117117
if value, ok := beginInputValue(inputs, field); ok {
118118
out[field] = value
119-
state.Globals["begin@"+field] = value
120-
} else if value, ok := state.Globals["begin@"+field]; ok {
119+
state.SetGlobal("begin@"+field, value)
120+
} else if value, ok := state.GetGlobal("begin@" + field); ok {
121121
out[field] = value
122122
}
123123
}

internal/agent/component/begin_test.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package component
1818

1919
import (
2020
"context"
21+
"encoding/json"
2122
"reflect"
2223
"testing"
2324

@@ -140,6 +141,27 @@ func TestBegin_PreservesStarterInputAcrossQueries(t *testing.T) {
140141
}
141142
}
142143

144+
func TestBegin_InitializesNilGlobalsAfterRestore(t *testing.T) {
145+
c, _ := NewBeginComponent(map[string]any{
146+
"inputs": map[string]any{"name": map[string]any{}},
147+
})
148+
var state canvas.CanvasState
149+
if err := json.Unmarshal([]byte(`{"sys":{}}`), &state); err != nil {
150+
t.Fatalf("Unmarshal: %v", err)
151+
}
152+
153+
out, err := c.Invoke(canvas.WithState(t.Context(), &state), nil, map[string]any{"query": "Alice"})
154+
if err != nil {
155+
t.Fatalf("Invoke: %v", err)
156+
}
157+
if out["name"] != "Alice" {
158+
t.Fatalf("name = %v, want Alice", out["name"])
159+
}
160+
if value, ok := state.GetGlobal("begin@name"); !ok || value != "Alice" {
161+
t.Fatalf("begin@name = %v, %v; want Alice, true", value, ok)
162+
}
163+
}
164+
143165
// TestBegin_PassesThroughInputs asserts the full inputs map — including
144166
// arbitrary keys beyond query / user_id — is returned unchanged as
145167
// outputs. This is the contract downstream components rely on to access

0 commit comments

Comments
 (0)