-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathtracker_inputs_test.go
More file actions
262 lines (247 loc) · 7.95 KB
/
Copy pathtracker_inputs_test.go
File metadata and controls
262 lines (247 loc) · 7.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
// ABOUTME: Tests for the public pipeline-inputs API — introspection, validation,
// ABOUTME: and bind-at-run-start fail-closed behavior (#553).
package tracker
import (
"context"
"errors"
"os"
"path/filepath"
"strings"
"testing"
"github.com/2389-research/tracker/pipeline"
)
const secretDip = `workflow SecretRun
goal: "x"
start: Plan
exit: Done
inputs
token: secret
required: true
agent Plan
label: p
prompt:
Read the token from ${inputs.token}.
agent Done
label: d
prompt:
done
edges
Plan -> Done
`
const inputsFileDip = `workflow SpecBuild
goal: "x"
start: Plan
exit: Done
inputs
spec: file
agent Plan
label: p
prompt:
Read the spec at ${inputs.spec}.
agent Done
label: d
prompt:
done
edges
Plan -> Done
`
const inputsDip = `workflow IdeaToPR
goal: "x"
start: Plan
exit: Done
inputs
idea: text
required: true
max_length: 4000
risk: enum
default: medium
options: low, medium, high
agent Plan
label: "p"
prompt:
Build ${inputs.idea} at risk ${inputs.risk}.
agent Done
label: d
prompt:
done
edges
Plan -> Done
`
func TestDescribeInputs(t *testing.T) {
specs, err := DescribeInputs(inputsDip, "dip")
if err != nil {
t.Fatalf("describe: %v", err)
}
if len(specs) != 2 {
t.Fatalf("want 2 specs, got %d", len(specs))
}
if specs[0].Name != "idea" || !specs[0].Required || specs[0].MaxLength != 4000 {
t.Fatalf("idea spec wrong: %+v", specs[0])
}
if specs[1].Name != "risk" || specs[1].Default != "medium" || len(specs[1].Options) != 3 {
t.Fatalf("risk spec wrong: %+v", specs[1])
}
}
// TestValidateSource_InputsRefsNotFlagged guards that inputs.* is treated as a
// runtime-produced ambient namespace: neither a ${inputs.x} interpolation nor a
// `when inputs.x` condition operand should warn about an undefined variable.
func TestValidateSource_InputsRefsNotFlagged(t *testing.T) {
res, err := ValidateSource(inputsDip)
if err != nil {
t.Fatalf("validate: %v", err)
}
for _, w := range res.Warnings {
if strings.Contains(w, "inputs.") {
t.Fatalf("inputs.* reference wrongly flagged: %s", w)
}
}
}
// TestBindInputs_ResumeSkipsMissingRequired guards resume: a required input that
// is not re-supplied must fail a FRESH run but NOT a resume (the checkpoint
// restores the original run's inputs and staged files persist in the run dir).
func TestBindInputs_ResumeSkipsMissingRequired(t *testing.T) {
graph := &pipeline.Graph{Inputs: []pipeline.InputSpec{
{Name: "idea", Kind: pipeline.InputText, Required: true},
{Name: "n", Kind: pipeline.InputNumber},
}}
if _, err := bindInputs(graph, Config{}, t.TempDir()); err == nil {
t.Fatal("fresh run with an unsatisfied required input should fail closed")
}
if _, err := bindInputs(graph, Config{ResumeRunID: "r1"}, t.TempDir()); err != nil {
t.Fatalf("resume must not fail on an un-resupplied required input: %v", err)
}
// A re-supplied value with a genuine (non-missing) constraint violation still
// fails on resume — only missing_required is tolerated.
bad := Config{ResumeRunID: "r1", Inputs: []Input{StringInput("n", "not-a-number")}}
if _, err := bindInputs(graph, bad, t.TempDir()); err == nil {
t.Fatal("resume must still reject a re-supplied invalid value")
}
}
func TestDescribeInputs_NoBlockIsEmpty(t *testing.T) {
specs, err := DescribeInputs(quickDip, "dip")
if err != nil {
t.Fatalf("describe: %v", err)
}
if len(specs) != 0 {
t.Fatalf("want no inputs for a workflow with no inputs block, got %v", specs)
}
}
func TestValidateInputs_Public(t *testing.T) {
specs, err := DescribeInputs(inputsDip, "dip")
if err != nil {
t.Fatalf("describe: %v", err)
}
// Missing required "idea" and an out-of-set risk both reported.
errs := ValidateInputs(specs, []Input{StringInput("risk", "extreme")})
if len(errs) == 0 {
t.Fatal("expected validation errors")
}
// Valid set passes.
if errs := ValidateInputs(specs, []Input{StringInput("idea", "ship"), StringInput("risk", "low")}); len(errs) != 0 {
t.Fatalf("expected valid, got %v", errs)
}
}
// TestRun_FailsClosedOnMissingRequiredInput asserts a run with an unsatisfied
// required input never executes a node — it fails at construction with a typed
// InputValidationError rather than expanding ${inputs.idea} to empty string.
func TestRun_FailsClosedOnMissingRequiredInput(t *testing.T) {
_, err := NewEngineWithContext(context.Background(), inputsDip, Config{
Format: "dip",
LLMClient: successStub(),
Inputs: []Input{StringInput("risk", "low")}, // "idea" (required) omitted
})
if err == nil {
t.Fatal("expected fail-closed error for missing required input")
}
var ive *InputValidationError
if !errors.As(err, &ive) {
t.Fatalf("want *InputValidationError, got %T: %v", err, err)
}
if !strings.Contains(err.Error(), "idea") {
t.Fatalf("error should name the missing input: %v", err)
}
}
// TestRun_StagesFileInputToFixedPath asserts a file input supplied as inline
// bytes is staged to <workDir>/.tracker/inputs/<name> (the fixed path a
// workflow's shell reads) and exposed as its relative path in the context.
func TestRun_StagesFileInputToFixedPath(t *testing.T) {
workDir := t.TempDir()
res, err := Run(context.Background(), inputsFileDip, Config{
Format: "dip",
WorkingDir: workDir,
LLMClient: successStub(),
Inputs: []Input{FileInputBytes("spec", []byte("build a widget"))},
})
if err != nil {
t.Fatalf("run: %v", err)
}
staged := filepath.Join(workDir, ".tracker", "inputs", "spec")
got, rerr := os.ReadFile(staged)
if rerr != nil {
t.Fatalf("staged spec not written: %v", rerr)
}
if string(got) != "build a widget" {
t.Fatalf("staged contents = %q", got)
}
if res.Context["inputs.spec"] != ".tracker/inputs/spec" {
t.Fatalf("inputs.spec = %q, want the relative staged path", res.Context["inputs.spec"])
}
}
// TestRun_SecretInputStagedNotInContext asserts a secret input's VALUE is staged
// to a 0600 file and never lands in the context — ${inputs.<name>} is the staged
// path, and the raw secret appears in neither Result.Context nor the checkpointed
// snapshot (#555).
func TestRun_SecretInputStagedNotInContext(t *testing.T) {
const secret = "sk-super-secret-value-xyz"
workDir := t.TempDir()
res, err := Run(context.Background(), secretDip, Config{
Format: "dip",
WorkingDir: workDir,
LLMClient: successStub(),
Inputs: []Input{SecretInput("token", secret)},
})
if err != nil {
t.Fatalf("run: %v", err)
}
// The value is on disk at the staged path, mode 0600.
staged := filepath.Join(workDir, ".tracker", "inputs", "token")
data, rerr := os.ReadFile(staged)
if rerr != nil {
t.Fatalf("secret not staged: %v", rerr)
}
if string(data) != secret {
t.Fatalf("staged secret = %q, want the supplied value", data)
}
if info, _ := os.Stat(staged); info.Mode().Perm() != 0o600 {
t.Fatalf("staged secret mode = %o, want 600", info.Mode().Perm())
}
// ${inputs.token} is the PATH, not the value.
if got := res.Context["inputs.token"]; got != ".tracker/inputs/token" {
t.Fatalf("inputs.token = %q, want the staged path", got)
}
// The raw secret value must appear in NO context value.
for k, v := range res.Context {
if strings.Contains(v, secret) {
t.Fatalf("secret value leaked into context[%q] = %q", k, v)
}
}
}
// TestRun_BindsInputsIntoContext asserts a valid input set is seeded under the
// inputs. prefix so ${inputs.name} resolves during the run.
func TestRun_BindsInputsIntoContext(t *testing.T) {
res, err := Run(context.Background(), inputsDip, Config{
Format: "dip",
WorkingDir: t.TempDir(),
LLMClient: successStub(),
Inputs: []Input{StringInput("idea", "ship it"), StringInput("risk", "high")},
})
if err != nil {
t.Fatalf("run: %v", err)
}
if got := res.Context["inputs.idea"]; got != "ship it" {
t.Fatalf("inputs.idea in context = %q, want %q", got, "ship it")
}
if got := res.Context["inputs.risk"]; got != "high" {
t.Fatalf("inputs.risk in context = %q, want %q", got, "high")
}
}