relay-flow 0.1.0-alpha.0 → 0.2.1-alpha

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (118) hide show
  1. package/README.md +160 -143
  2. package/cmd/relay-flow/commands_test.go +968 -0
  3. package/cmd/relay-flow/main.go +877 -175
  4. package/cmd/relay-flow/scenario_test.go +1310 -0
  5. package/cmd/relay-flow/serve.go +610 -0
  6. package/examples/default-story-workflow.yaml +88 -0
  7. package/go.mod +69 -2
  8. package/go.sum +185 -0
  9. package/internal/config/config.go +88 -0
  10. package/internal/config/machine.go +99 -48
  11. package/internal/config/machine_test.go +248 -0
  12. package/internal/config/merge_test.go +118 -0
  13. package/internal/config/writeatomic.go +36 -0
  14. package/internal/config/writeatomic_test.go +98 -0
  15. package/internal/execution/goworkflows/activities.go +490 -0
  16. package/internal/execution/goworkflows/engine.go +520 -0
  17. package/internal/execution/goworkflows/engine_test.go +660 -0
  18. package/internal/execution/goworkflows/fakes_test.go +509 -0
  19. package/internal/execution/goworkflows/interpreter.go +613 -0
  20. package/internal/execution/goworkflows/logging_test.go +154 -0
  21. package/internal/execution/goworkflows/mailbox_test.go +423 -0
  22. package/internal/execution/goworkflows/node_runtime_integration_test.go +133 -0
  23. package/internal/execution/goworkflows/node_runtime_test.go +510 -0
  24. package/internal/execution/goworkflows/projection.go +504 -0
  25. package/internal/execution/goworkflows/recovery_test.go +1092 -0
  26. package/internal/execution/goworkflows/retry_log_test.go +59 -0
  27. package/internal/execution/goworkflows/retry_projection_test.go +98 -0
  28. package/internal/harness/contract_test.go +174 -0
  29. package/internal/harness/factory.go +63 -0
  30. package/internal/harness/harness.go +41 -0
  31. package/internal/harness/opencode/opencode.go +166 -0
  32. package/internal/harness/opencode/opencode_test.go +50 -0
  33. package/internal/harness/plugin_selection_test.go +126 -0
  34. package/internal/identity/identity.go +37 -0
  35. package/internal/logging/logging.go +56 -0
  36. package/internal/logging/logging_test.go +116 -0
  37. package/internal/paths/paths.go +69 -0
  38. package/internal/recover/recover.go +115 -0
  39. package/internal/repo/poller.go +186 -0
  40. package/internal/repo/poller_test.go +327 -0
  41. package/internal/repo/repo.go +132 -0
  42. package/internal/repo/service.go +216 -0
  43. package/internal/repo/service_test.go +298 -0
  44. package/internal/retry/retry.go +118 -0
  45. package/internal/router/router.go +84 -0
  46. package/internal/router/router_test.go +231 -0
  47. package/internal/run/manager.go +121 -0
  48. package/internal/run/run.go +140 -0
  49. package/internal/run/run_identity_test.go +52 -0
  50. package/internal/run/run_manager_test.go +286 -0
  51. package/internal/runner/contract_test.go +259 -0
  52. package/internal/runner/factory.go +65 -0
  53. package/internal/runner/orca/orca.go +341 -172
  54. package/internal/runner/orca/orca_test.go +256 -143
  55. package/internal/runner/orca/orcacli/orcacli.go +220 -0
  56. package/internal/runner/orca/orcacli/orcacli_test.go +157 -0
  57. package/internal/runner/orca/orcacli/testdata/repo-list.json +18 -0
  58. package/internal/runner/orca/orcacli/testdata/strict-orca.sh +32 -0
  59. package/internal/runner/orca/orcacli/testdata/terminal-close.json +12 -0
  60. package/internal/runner/orca/orcacli/testdata/terminal-create.json +18 -0
  61. package/internal/runner/orca/orcacli/testdata/terminal-list.json +51 -0
  62. package/internal/runner/orca/orcacli/testdata/terminal-send.json +1 -0
  63. package/internal/runner/orca/orcacli/testdata/terminal-show.json +1 -0
  64. package/internal/runner/orca/orcacli/testdata/worktree-create.json +22 -0
  65. package/internal/runner/orca/orcacli/testdata/worktree-list.json +31 -0
  66. package/internal/runner/orca/orcacli/testdata/worktree-remove.json +6 -0
  67. package/internal/runner/runner.go +54 -64
  68. package/internal/server/api_test.go +300 -0
  69. package/internal/server/client.go +192 -74
  70. package/internal/server/fixture_test.go +248 -0
  71. package/internal/server/server.go +425 -248
  72. package/internal/server/shutdown_test.go +116 -0
  73. package/internal/task/auth_test.go +48 -0
  74. package/internal/task/contract_test.go +225 -0
  75. package/internal/task/factory.go +119 -0
  76. package/internal/task/jira/auth.go +183 -0
  77. package/internal/task/jira/auth_test.go +107 -0
  78. package/internal/task/jira/effects_test.go +39 -0
  79. package/internal/task/jira/filters_test.go +254 -0
  80. package/internal/task/jira/helpers_test.go +70 -0
  81. package/internal/task/jira/jira.go +538 -0
  82. package/internal/task/jira/normalize.go +119 -0
  83. package/internal/task/jira/rest/adf.go +128 -0
  84. package/internal/task/jira/rest/client.go +573 -0
  85. package/internal/task/jira/rest/client_test.go +381 -0
  86. package/internal/task/jira/testdata/jira_search_issues.json +120 -0
  87. package/internal/task/jira/transition_defaults_test.go +158 -0
  88. package/internal/task/jira/validation_test.go +94 -0
  89. package/internal/task/task.go +84 -0
  90. package/internal/workflow/report.go +85 -0
  91. package/internal/workflow/report_test.go +259 -0
  92. package/internal/workflow/service.go +142 -0
  93. package/internal/workflow/store.go +136 -0
  94. package/internal/workflow/store_test.go +282 -0
  95. package/internal/workflow/workflow.go +345 -0
  96. package/internal/workflow/workflow_test.go +412 -0
  97. package/package.json +7 -2
  98. package/internal/acli/acli.go +0 -229
  99. package/internal/config/demo_test.go +0 -17
  100. package/internal/config/schema.go +0 -193
  101. package/internal/config/schema_test.go +0 -162
  102. package/internal/daemon/daemon.go +0 -218
  103. package/internal/daemon/daemon_test.go +0 -204
  104. package/internal/discovery/discovery.go +0 -122
  105. package/internal/discovery/discovery_test.go +0 -62
  106. package/internal/opencode/opencode.go +0 -26
  107. package/internal/orcacli/orcacli.go +0 -264
  108. package/internal/runner/orca/README.md +0 -64
  109. package/internal/runner/runner_test.go +0 -64
  110. package/internal/server/server_test.go +0 -195
  111. package/internal/tasks/jira/README.md +0 -69
  112. package/internal/tasks/jira/component_test.go +0 -16
  113. package/internal/tasks/jira/decode.go +0 -24
  114. package/internal/tasks/jira/jira.go +0 -231
  115. package/internal/tasks/jira/jira_test.go +0 -259
  116. package/internal/tasks/jira/jql_test.go +0 -16
  117. package/internal/tasks/tasks.go +0 -90
  118. package/internal/tasks/tasks_test.go +0 -91
@@ -0,0 +1,345 @@
1
+ // Package workflow defines workflow values, strict parsing, validation,
2
+ // structured reports, storage, and the workflow service.
3
+ package workflow
4
+
5
+ import (
6
+ "bytes"
7
+ "fmt"
8
+ "regexp"
9
+ "sort"
10
+
11
+ "gopkg.in/yaml.v3"
12
+
13
+ "github.com/rajpopat27/relay-flow/internal/config"
14
+ )
15
+
16
+ type NodeType string
17
+
18
+ const (
19
+ NodeAgent NodeType = "agent"
20
+ NodeHITL NodeType = "hitl"
21
+ )
22
+
23
+ type Outcome string
24
+
25
+ const (
26
+ OutcomeSuccess Outcome = "success"
27
+ OutcomeFailure Outcome = "failure"
28
+ )
29
+
30
+ // Reserved lifecycle node names. The word "terminal" is runner-only.
31
+ const (
32
+ StartNode = "start"
33
+ EndNode = "end"
34
+ )
35
+
36
+ type Workflow struct {
37
+ Name string `yaml:"name" json:"name"`
38
+ Repos []string `yaml:"repos" json:"repos"`
39
+ CleanupRunnerOnEnd bool `yaml:"cleanupRunnerOnEnd" json:"cleanupRunnerOnEnd"`
40
+ TaskConfig config.RawValues `yaml:"taskConfig,omitempty" json:"taskConfig,omitempty"`
41
+ Nodes map[string]Node `yaml:"nodes" json:"nodes"`
42
+ }
43
+
44
+ type Node struct {
45
+ Type NodeType `yaml:"type,omitempty" json:"type,omitempty"`
46
+ Agent string `yaml:"agent,omitempty" json:"agent,omitempty"`
47
+ Description string `yaml:"description,omitempty" json:"description,omitempty"`
48
+ NudgePrompt string `yaml:"nudgePrompt,omitempty" json:"nudgePrompt,omitempty"`
49
+ TaskConfig config.RawValues `yaml:"taskConfig,omitempty" json:"taskConfig,omitempty"`
50
+ OnSuccess []Route `yaml:"onSuccess,omitempty" json:"onSuccess,omitempty"`
51
+ OnFailure []Route `yaml:"onFailure,omitempty" json:"onFailure,omitempty"`
52
+ }
53
+
54
+ type Route struct {
55
+ Target string `yaml:"target" json:"target"`
56
+ When string `yaml:"when,omitempty" json:"when,omitempty"`
57
+ }
58
+
59
+ type NudgeTemplateData struct {
60
+ TaskSystem string
61
+ Ticket string
62
+ Workflow string
63
+ Repo string
64
+ Node string
65
+ NextSteps string
66
+ }
67
+
68
+ var namePattern = regexp.MustCompile(`^[a-z][a-zA-Z0-9]*$`)
69
+
70
+ var nudgeVarPattern = regexp.MustCompile(`\{\{([^{}]*)\}\}`)
71
+
72
+ var knownNudgeVars = map[string]bool{
73
+ "taskSystem": true, "ticket": true, "workflow": true, "repo": true, "node": true, "nextSteps": true,
74
+ }
75
+
76
+ // Parse strictly decodes a workflow YAML document. Unknown fields and
77
+ // explicit null values inside any taskConfig are rejected. The returned
78
+ // workflow is not yet validated.
79
+ func Parse(name string, yamlBytes []byte) (*Workflow, error) {
80
+ var raw map[string]any
81
+ if err := yaml.Unmarshal(yamlBytes, &raw); err != nil {
82
+ return nil, fmt.Errorf("parse workflow %q: %w", name, err)
83
+ }
84
+ var wf Workflow
85
+ dec := yaml.NewDecoder(bytes.NewReader(yamlBytes))
86
+ dec.KnownFields(true)
87
+ if err := dec.Decode(&wf); err != nil {
88
+ return nil, fmt.Errorf("parse workflow %q: %w", name, err)
89
+ }
90
+ if wf.Nodes == nil {
91
+ wf.Nodes = map[string]Node{}
92
+ }
93
+ if v, present := raw["taskConfig"]; present && v == nil {
94
+ return nil, fmt.Errorf("parse workflow %q: taskConfig: explicit null is not allowed", name)
95
+ }
96
+ if err := rejectNullValues(wf.TaskConfig, "taskConfig"); err != nil {
97
+ return nil, fmt.Errorf("parse workflow %q: %w", name, err)
98
+ }
99
+ rawNodes, _ := raw["nodes"].(map[string]any)
100
+ for nodeName, n := range wf.Nodes {
101
+ if rn, ok := rawNodes[nodeName].(map[string]any); ok {
102
+ if v, present := rn["taskConfig"]; present && v == nil {
103
+ return nil, fmt.Errorf("parse workflow %q node %q: taskConfig: explicit null is not allowed", name, nodeName)
104
+ }
105
+ }
106
+ if err := rejectNullValues(n.TaskConfig, "nodes."+nodeName+".taskConfig"); err != nil {
107
+ return nil, fmt.Errorf("parse workflow %q: %w", name, err)
108
+ }
109
+ }
110
+ return &wf, nil
111
+ }
112
+
113
+ // rejectNullValues recursively rejects explicit YAML nulls in raw task
114
+ // config values.
115
+ func rejectNullValues(values map[string]any, path string) error {
116
+ for k, v := range values {
117
+ key := path + "." + k
118
+ if err := rejectNullValue(v, key); err != nil {
119
+ return err
120
+ }
121
+ }
122
+ return nil
123
+ }
124
+
125
+ func rejectNullValue(v any, key string) error {
126
+ if v == nil {
127
+ return fmt.Errorf("config key %q: explicit null is not allowed", key)
128
+ }
129
+ switch t := v.(type) {
130
+ case map[string]any:
131
+ return rejectNullValues(t, key)
132
+ case []any:
133
+ for i, elem := range t {
134
+ if err := rejectNullValue(elem, fmt.Sprintf("%s[%d]", key, i)); err != nil {
135
+ return err
136
+ }
137
+ }
138
+ }
139
+ return nil
140
+ }
141
+
142
+ // Validate checks the workflow graph, identity, repos, nodes, routes, and
143
+ // nudge templates against the workflow-definition spec.
144
+ func (w *Workflow) Validate() error {
145
+ if !namePattern.MatchString(w.Name) {
146
+ return fmt.Errorf("workflow name %q must be lower-camel alphanumeric beginning with a lowercase letter", w.Name)
147
+ }
148
+ if len(w.Repos) == 0 {
149
+ return fmt.Errorf("workflow %q must list at least one repo", w.Name)
150
+ }
151
+ seen := map[string]bool{}
152
+ for _, r := range w.Repos {
153
+ if seen[r] {
154
+ return fmt.Errorf("workflow %q lists duplicate repo %q", w.Name, r)
155
+ }
156
+ seen[r] = true
157
+ }
158
+
159
+ start, ok := w.Nodes[StartNode]
160
+ if !ok {
161
+ return fmt.Errorf("workflow %q must contain a %q node", w.Name, StartNode)
162
+ }
163
+ if err := validateStart(start); err != nil {
164
+ return fmt.Errorf("workflow %q start: %w", w.Name, err)
165
+ }
166
+ end, ok := w.Nodes[EndNode]
167
+ if !ok {
168
+ return fmt.Errorf("workflow %q must contain an %q node", w.Name, EndNode)
169
+ }
170
+ if err := validateEnd(end); err != nil {
171
+ return fmt.Errorf("workflow %q end: %w", w.Name, err)
172
+ }
173
+
174
+ for name, n := range w.Nodes {
175
+ if name == StartNode || name == EndNode {
176
+ continue
177
+ }
178
+ if err := validateWorkNode(w.Name, name, n); err != nil {
179
+ return err
180
+ }
181
+ }
182
+ for name, n := range w.Nodes {
183
+ for _, r := range allRoutes(n) {
184
+ if r.Target == StartNode {
185
+ return fmt.Errorf("workflow %q node %q: routes cannot target reserved %q", w.Name, name, StartNode)
186
+ }
187
+ if _, ok := w.Nodes[r.Target]; !ok {
188
+ return fmt.Errorf("workflow %q node %q: route target %q does not exist", w.Name, name, r.Target)
189
+ }
190
+ }
191
+ }
192
+ if err := w.validateReachability(); err != nil {
193
+ return err
194
+ }
195
+ return nil
196
+ }
197
+
198
+ func validateStart(n Node) error {
199
+ if n.Type != "" || n.Agent != "" || n.Description != "" || n.NudgePrompt != "" {
200
+ return fmt.Errorf("start must not declare type, agent, description, or nudgePrompt")
201
+ }
202
+ if len(n.OnFailure) != 0 {
203
+ return fmt.Errorf("start must not declare failure routes")
204
+ }
205
+ if len(n.OnSuccess) != 1 {
206
+ return fmt.Errorf("start must declare exactly one success target, got %d", len(n.OnSuccess))
207
+ }
208
+ return nil
209
+ }
210
+
211
+ func validateEnd(n Node) error {
212
+ if n.Type != "" || n.Agent != "" || n.Description != "" || n.NudgePrompt != "" {
213
+ return fmt.Errorf("end must not declare type, agent, description, or nudgePrompt")
214
+ }
215
+ if len(n.OnSuccess) != 0 || len(n.OnFailure) != 0 {
216
+ return fmt.Errorf("end must not declare routes")
217
+ }
218
+ return nil
219
+ }
220
+
221
+ func validateWorkNode(wfName, name string, n Node) error {
222
+ if n.Type != NodeAgent && n.Type != NodeHITL {
223
+ return fmt.Errorf("workflow %q node %q: type must be %q or %q", wfName, name, NodeAgent, NodeHITL)
224
+ }
225
+ if n.Agent == "" {
226
+ return fmt.Errorf("workflow %q node %q: agent is required", wfName, name)
227
+ }
228
+ if n.Description == "" {
229
+ return fmt.Errorf("workflow %q node %q: description is required", wfName, name)
230
+ }
231
+ if len(n.OnSuccess) == 0 {
232
+ return fmt.Errorf("workflow %q node %q: at least one success route is required", wfName, name)
233
+ }
234
+ if len(n.OnFailure) == 0 {
235
+ return fmt.Errorf("workflow %q node %q: at least one failure route is required", wfName, name)
236
+ }
237
+ for _, m := range nudgeVarPattern.FindAllStringSubmatch(n.NudgePrompt, -1) {
238
+ if !knownNudgeVars[m[1]] {
239
+ return fmt.Errorf("workflow %q node %q: unknown nudge template variable {{%s}}", wfName, name, m[1])
240
+ }
241
+ }
242
+ return nil
243
+ }
244
+
245
+ func allRoutes(n Node) []Route {
246
+ out := make([]Route, 0, len(n.OnSuccess)+len(n.OnFailure))
247
+ out = append(out, n.OnSuccess...)
248
+ out = append(out, n.OnFailure...)
249
+ return out
250
+ }
251
+
252
+ // validateReachability ensures every work node is reachable from start and
253
+ // that end is reachable. Backward edges and self-loops are allowed.
254
+ func (w *Workflow) validateReachability() error {
255
+ adj := map[string][]string{}
256
+ for name, n := range w.Nodes {
257
+ for _, r := range allRoutes(n) {
258
+ adj[name] = append(adj[name], r.Target)
259
+ }
260
+ }
261
+ visited := map[string]bool{}
262
+ queue := []string{StartNode}
263
+ for len(queue) > 0 {
264
+ cur := queue[0]
265
+ queue = queue[1:]
266
+ if visited[cur] {
267
+ continue
268
+ }
269
+ visited[cur] = true
270
+ queue = append(queue, adj[cur]...)
271
+ }
272
+ for name := range w.Nodes {
273
+ if !visited[name] {
274
+ return fmt.Errorf("workflow %q node %q is not reachable from %q", w.Name, name, StartNode)
275
+ }
276
+ }
277
+ return nil
278
+ }
279
+
280
+ // StartTarget returns the single success target of the start node.
281
+ func (w *Workflow) StartTarget() (string, error) {
282
+ start, ok := w.Nodes[StartNode]
283
+ if !ok {
284
+ return "", fmt.Errorf("workflow %q has no %q node", w.Name, StartNode)
285
+ }
286
+ if len(start.OnSuccess) != 1 {
287
+ return "", fmt.Errorf("workflow %q start must have exactly one success target, got %d", w.Name, len(start.OnSuccess))
288
+ }
289
+ return start.OnSuccess[0].Target, nil
290
+ }
291
+
292
+ // Routes returns the configured routes for a node and outcome.
293
+ func (w *Workflow) Routes(node string, outcome Outcome) ([]Route, error) {
294
+ n, ok := w.Nodes[node]
295
+ if !ok {
296
+ return nil, fmt.Errorf("workflow %q has no node %q", w.Name, node)
297
+ }
298
+ switch outcome {
299
+ case OutcomeSuccess:
300
+ return n.OnSuccess, nil
301
+ case OutcomeFailure:
302
+ return n.OnFailure, nil
303
+ default:
304
+ return nil, fmt.Errorf("workflow %q node %q: unknown outcome %q", w.Name, node, outcome)
305
+ }
306
+ }
307
+
308
+ // RenderNudge renders the node's optional custom instructions with the
309
+ // supported variables.
310
+ func (w *Workflow) RenderNudge(node string, data NudgeTemplateData) (string, error) {
311
+ n, ok := w.Nodes[node]
312
+ if !ok {
313
+ return "", fmt.Errorf("workflow %q has no node %q", w.Name, node)
314
+ }
315
+ tmpl := n.NudgePrompt
316
+ out := nudgeVarPattern.ReplaceAllStringFunc(tmpl, func(m string) string {
317
+ varName := nudgeVarPattern.FindStringSubmatch(m)[1]
318
+ switch varName {
319
+ case "taskSystem":
320
+ return data.TaskSystem
321
+ case "ticket":
322
+ return data.Ticket
323
+ case "workflow":
324
+ return data.Workflow
325
+ case "repo":
326
+ return data.Repo
327
+ case "node":
328
+ return data.Node
329
+ case "nextSteps":
330
+ return data.NextSteps
331
+ }
332
+ return m
333
+ })
334
+ return out, nil
335
+ }
336
+
337
+ // sortedTargets is a helper for deterministic error messages.
338
+ func sortedTargets(routes []Route) []string {
339
+ out := make([]string, 0, len(routes))
340
+ for _, r := range routes {
341
+ out = append(out, r.Target)
342
+ }
343
+ sort.Strings(out)
344
+ return out
345
+ }