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.
- package/README.md +160 -143
- package/cmd/relay-flow/commands_test.go +968 -0
- package/cmd/relay-flow/main.go +877 -175
- package/cmd/relay-flow/scenario_test.go +1310 -0
- package/cmd/relay-flow/serve.go +610 -0
- package/examples/default-story-workflow.yaml +88 -0
- package/go.mod +69 -2
- package/go.sum +185 -0
- package/internal/config/config.go +88 -0
- package/internal/config/machine.go +99 -48
- package/internal/config/machine_test.go +248 -0
- package/internal/config/merge_test.go +118 -0
- package/internal/config/writeatomic.go +36 -0
- package/internal/config/writeatomic_test.go +98 -0
- package/internal/execution/goworkflows/activities.go +490 -0
- package/internal/execution/goworkflows/engine.go +520 -0
- package/internal/execution/goworkflows/engine_test.go +660 -0
- package/internal/execution/goworkflows/fakes_test.go +509 -0
- package/internal/execution/goworkflows/interpreter.go +613 -0
- package/internal/execution/goworkflows/logging_test.go +154 -0
- package/internal/execution/goworkflows/mailbox_test.go +423 -0
- package/internal/execution/goworkflows/node_runtime_integration_test.go +133 -0
- package/internal/execution/goworkflows/node_runtime_test.go +510 -0
- package/internal/execution/goworkflows/projection.go +504 -0
- package/internal/execution/goworkflows/recovery_test.go +1092 -0
- package/internal/execution/goworkflows/retry_log_test.go +59 -0
- package/internal/execution/goworkflows/retry_projection_test.go +98 -0
- package/internal/harness/contract_test.go +174 -0
- package/internal/harness/factory.go +63 -0
- package/internal/harness/harness.go +41 -0
- package/internal/harness/opencode/opencode.go +166 -0
- package/internal/harness/opencode/opencode_test.go +50 -0
- package/internal/harness/plugin_selection_test.go +126 -0
- package/internal/identity/identity.go +37 -0
- package/internal/logging/logging.go +56 -0
- package/internal/logging/logging_test.go +116 -0
- package/internal/paths/paths.go +69 -0
- package/internal/recover/recover.go +115 -0
- package/internal/repo/poller.go +186 -0
- package/internal/repo/poller_test.go +327 -0
- package/internal/repo/repo.go +132 -0
- package/internal/repo/service.go +216 -0
- package/internal/repo/service_test.go +298 -0
- package/internal/retry/retry.go +118 -0
- package/internal/router/router.go +84 -0
- package/internal/router/router_test.go +231 -0
- package/internal/run/manager.go +121 -0
- package/internal/run/run.go +140 -0
- package/internal/run/run_identity_test.go +52 -0
- package/internal/run/run_manager_test.go +286 -0
- package/internal/runner/contract_test.go +259 -0
- package/internal/runner/factory.go +65 -0
- package/internal/runner/orca/orca.go +341 -172
- package/internal/runner/orca/orca_test.go +256 -143
- package/internal/runner/orca/orcacli/orcacli.go +220 -0
- package/internal/runner/orca/orcacli/orcacli_test.go +157 -0
- package/internal/runner/orca/orcacli/testdata/repo-list.json +18 -0
- package/internal/runner/orca/orcacli/testdata/strict-orca.sh +32 -0
- package/internal/runner/orca/orcacli/testdata/terminal-close.json +12 -0
- package/internal/runner/orca/orcacli/testdata/terminal-create.json +18 -0
- package/internal/runner/orca/orcacli/testdata/terminal-list.json +51 -0
- package/internal/runner/orca/orcacli/testdata/terminal-send.json +1 -0
- package/internal/runner/orca/orcacli/testdata/terminal-show.json +1 -0
- package/internal/runner/orca/orcacli/testdata/worktree-create.json +22 -0
- package/internal/runner/orca/orcacli/testdata/worktree-list.json +31 -0
- package/internal/runner/orca/orcacli/testdata/worktree-remove.json +6 -0
- package/internal/runner/runner.go +54 -64
- package/internal/server/api_test.go +300 -0
- package/internal/server/client.go +192 -74
- package/internal/server/fixture_test.go +248 -0
- package/internal/server/server.go +425 -248
- package/internal/server/shutdown_test.go +116 -0
- package/internal/task/auth_test.go +48 -0
- package/internal/task/contract_test.go +225 -0
- package/internal/task/factory.go +119 -0
- package/internal/task/jira/auth.go +183 -0
- package/internal/task/jira/auth_test.go +107 -0
- package/internal/task/jira/effects_test.go +39 -0
- package/internal/task/jira/filters_test.go +254 -0
- package/internal/task/jira/helpers_test.go +70 -0
- package/internal/task/jira/jira.go +538 -0
- package/internal/task/jira/normalize.go +119 -0
- package/internal/task/jira/rest/adf.go +128 -0
- package/internal/task/jira/rest/client.go +573 -0
- package/internal/task/jira/rest/client_test.go +381 -0
- package/internal/task/jira/testdata/jira_search_issues.json +120 -0
- package/internal/task/jira/transition_defaults_test.go +158 -0
- package/internal/task/jira/validation_test.go +94 -0
- package/internal/task/task.go +84 -0
- package/internal/workflow/report.go +85 -0
- package/internal/workflow/report_test.go +259 -0
- package/internal/workflow/service.go +142 -0
- package/internal/workflow/store.go +136 -0
- package/internal/workflow/store_test.go +282 -0
- package/internal/workflow/workflow.go +345 -0
- package/internal/workflow/workflow_test.go +412 -0
- package/package.json +7 -2
- package/internal/acli/acli.go +0 -229
- package/internal/config/demo_test.go +0 -17
- package/internal/config/schema.go +0 -193
- package/internal/config/schema_test.go +0 -162
- package/internal/daemon/daemon.go +0 -218
- package/internal/daemon/daemon_test.go +0 -204
- package/internal/discovery/discovery.go +0 -122
- package/internal/discovery/discovery_test.go +0 -62
- package/internal/opencode/opencode.go +0 -26
- package/internal/orcacli/orcacli.go +0 -264
- package/internal/runner/orca/README.md +0 -64
- package/internal/runner/runner_test.go +0 -64
- package/internal/server/server_test.go +0 -195
- package/internal/tasks/jira/README.md +0 -69
- package/internal/tasks/jira/component_test.go +0 -16
- package/internal/tasks/jira/decode.go +0 -24
- package/internal/tasks/jira/jira.go +0 -231
- package/internal/tasks/jira/jira_test.go +0 -259
- package/internal/tasks/jira/jql_test.go +0 -16
- package/internal/tasks/tasks.go +0 -90
- package/internal/tasks/tasks_test.go +0 -91
|
@@ -1,243 +1,412 @@
|
|
|
1
|
-
// Package orca is the
|
|
2
|
-
//
|
|
3
|
-
//
|
|
1
|
+
// Package orca is the Orca runner adapter. It owns ticket-scoped worktrees
|
|
2
|
+
// (environments), terminals, liveness, and cleanup. It does not know
|
|
3
|
+
// task-system fields, workflow routes, report contents, or agent command
|
|
4
|
+
// syntax.
|
|
5
|
+
//
|
|
6
|
+
// 9.5 external-call logging: every adapter boundary emits one debug line
|
|
7
|
+
// BEFORE the call (operation, ticket/runID, title when applicable) and one
|
|
8
|
+
// info line AFTER with only the outcome (ok/error), never payloads.
|
|
4
9
|
package orca
|
|
5
10
|
|
|
6
11
|
import (
|
|
12
|
+
"context"
|
|
13
|
+
"errors"
|
|
7
14
|
"fmt"
|
|
15
|
+
"log/slog"
|
|
8
16
|
"strings"
|
|
9
|
-
"time"
|
|
10
17
|
|
|
11
|
-
"github.com/rajpopat27/relay-flow/internal/
|
|
12
|
-
"github.com/rajpopat27/relay-flow/internal/orcacli"
|
|
18
|
+
"github.com/rajpopat27/relay-flow/internal/config"
|
|
13
19
|
"github.com/rajpopat27/relay-flow/internal/runner"
|
|
14
|
-
"github.com/rajpopat27/relay-flow/internal/
|
|
20
|
+
"github.com/rajpopat27/relay-flow/internal/runner/orca/orcacli"
|
|
15
21
|
)
|
|
16
22
|
|
|
23
|
+
// Config is the adapter-owned root runnerConfig.
|
|
24
|
+
type Config struct {
|
|
25
|
+
// BaseRef is the base branch for ticket worktrees. When empty, the base
|
|
26
|
+
// branch is derived from the repo's primary worktree reported by Orca.
|
|
27
|
+
BaseRef string `yaml:"baseRef,omitempty"`
|
|
28
|
+
}
|
|
29
|
+
|
|
17
30
|
func init() {
|
|
18
|
-
runner.Register("orca", runner.
|
|
19
|
-
|
|
20
|
-
New: func(cfg any) (runner.Runner, error) {
|
|
21
|
-
c, ok := cfg.(Config)
|
|
22
|
-
if !ok {
|
|
23
|
-
return nil, fmt.Errorf("internal: orca factory received %T", cfg)
|
|
24
|
-
}
|
|
25
|
-
return NewRunner(c, nil), nil
|
|
26
|
-
},
|
|
31
|
+
runner.Register("orca", func(raw config.RawValues) (runner.Runner, error) {
|
|
32
|
+
return New(orcacli.New(), raw)
|
|
27
33
|
})
|
|
28
34
|
}
|
|
29
35
|
|
|
30
|
-
//
|
|
31
|
-
//
|
|
32
|
-
|
|
33
|
-
|
|
36
|
+
// adapter is the Orca runner.Runner. It is safe for concurrent use; the CLI
|
|
37
|
+
// client owns subprocess serialization.
|
|
38
|
+
type adapter struct {
|
|
39
|
+
cli orcacli.Client
|
|
40
|
+
cfg Config
|
|
41
|
+
}
|
|
34
42
|
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
43
|
+
// New constructs the adapter from root runnerConfig around an explicit CLI
|
|
44
|
+
// seam (tests inject a fake Client).
|
|
45
|
+
func New(cli orcacli.Client, raw config.RawValues) (runner.Runner, error) {
|
|
46
|
+
var cfg Config
|
|
47
|
+
if err := config.DecodeStrict(raw, &cfg); err != nil {
|
|
48
|
+
return nil, fmt.Errorf("orca runnerConfig: %w", err)
|
|
40
49
|
}
|
|
41
|
-
return
|
|
50
|
+
return &adapter{cli: cli, cfg: cfg}, nil
|
|
42
51
|
}
|
|
43
52
|
|
|
44
|
-
//
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
53
|
+
// --- Repos ---
|
|
54
|
+
|
|
55
|
+
// DiscoverRepos returns Orca-registered repos as registration candidates.
|
|
56
|
+
func (a *adapter) DiscoverRepos(ctx context.Context) ([]runner.RepoCandidate, error) {
|
|
57
|
+
repos, err := a.cli.ListRepos(ctx)
|
|
58
|
+
if err != nil {
|
|
59
|
+
return nil, err
|
|
60
|
+
}
|
|
61
|
+
out := make([]runner.RepoCandidate, 0, len(repos))
|
|
62
|
+
for _, r := range repos {
|
|
63
|
+
out = append(out, runner.RepoCandidate{Name: r.DisplayName, Path: r.Path})
|
|
64
|
+
}
|
|
65
|
+
return out, nil
|
|
56
66
|
}
|
|
57
67
|
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
findBranch func(repoPath, key string) (string, bool, error)
|
|
65
|
-
sleep func(time.Duration) // test seam for ensureWorktree retries
|
|
68
|
+
// ValidateRepo verifies the named repo exists in Orca at the given path.
|
|
69
|
+
func (a *adapter) ValidateRepo(ctx context.Context, name, path string) error {
|
|
70
|
+
if _, err := a.repoID(ctx, name, path); err != nil {
|
|
71
|
+
return err
|
|
72
|
+
}
|
|
73
|
+
return nil
|
|
66
74
|
}
|
|
67
75
|
|
|
68
|
-
//
|
|
69
|
-
//
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
76
|
+
// repoID resolves a registered repo to its Orca repo ID. The repo path is
|
|
77
|
+
// the stable identity (the Orca repo ID is an internal detail that can
|
|
78
|
+
// change across machines); name is matched as a secondary check.
|
|
79
|
+
func (a *adapter) repoID(ctx context.Context, name, path string) (string, error) {
|
|
80
|
+
repos, err := a.cli.ListRepos(ctx)
|
|
81
|
+
if err != nil {
|
|
82
|
+
return "", err
|
|
75
83
|
}
|
|
76
|
-
|
|
77
|
-
r.
|
|
84
|
+
for _, r := range repos {
|
|
85
|
+
if r.Path == path && r.DisplayName == name {
|
|
86
|
+
return r.ID, nil
|
|
87
|
+
}
|
|
78
88
|
}
|
|
79
|
-
return
|
|
89
|
+
return "", fmt.Errorf("orca: repo %q at %q not registered", name, path)
|
|
80
90
|
}
|
|
81
91
|
|
|
82
|
-
//
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
92
|
+
// --- Environment ---
|
|
93
|
+
|
|
94
|
+
// EnsureEnvironment returns the ticket-scoped worktree, creating it from the
|
|
95
|
+
// repo's main worktree and the configured base ref (or the primary
|
|
96
|
+
// worktree's branch) when absent.
|
|
97
|
+
func (a *adapter) EnsureEnvironment(ctx context.Context, spec runner.RunSpec) (runner.Environment, error) {
|
|
98
|
+
slog.Debug("orca call",
|
|
99
|
+
"op", "ensure-environment", "ticket", spec.TicketKey,
|
|
100
|
+
"runID", string(spec.RunID), "repo", spec.RepoName)
|
|
101
|
+
env, reused, err := a.ensureEnvironment(ctx, spec)
|
|
102
|
+
attrs := []any{
|
|
103
|
+
"op", "ensure-environment", "ticket", spec.TicketKey,
|
|
104
|
+
"runID", string(spec.RunID), "repo", spec.RepoName,
|
|
105
|
+
}
|
|
106
|
+
if err != nil {
|
|
107
|
+
attrs = append(attrs, "result", "error", "error", sanitizeErr(err))
|
|
108
|
+
} else if reused {
|
|
109
|
+
attrs = append(attrs, "result", "exists")
|
|
110
|
+
} else {
|
|
111
|
+
attrs = append(attrs, "result", "created")
|
|
88
112
|
}
|
|
113
|
+
slog.Info("orca outcome", attrs...)
|
|
114
|
+
return env, err
|
|
89
115
|
}
|
|
90
116
|
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
117
|
+
func (a *adapter) SetEnvironmentStatus(ctx context.Context, env runner.Environment, status string) error {
|
|
118
|
+
slog.Debug("orca call", "op", "set-environment-status", "envID", env.ID, "status", status)
|
|
119
|
+
err := a.cli.SetWorktreeStatus(ctx, env.ID, status)
|
|
120
|
+
if err != nil {
|
|
121
|
+
slog.Info("orca outcome", "op", "set-environment-status", "envID", env.ID, "status", status, "result", "error", "error", sanitizeErr(err))
|
|
122
|
+
} else {
|
|
123
|
+
slog.Info("orca outcome", "op", "set-environment-status", "envID", env.ID, "status", status, "result", "ok")
|
|
124
|
+
}
|
|
125
|
+
return err
|
|
95
126
|
}
|
|
96
127
|
|
|
97
|
-
//
|
|
98
|
-
//
|
|
99
|
-
//
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
if
|
|
103
|
-
return
|
|
104
|
-
} else if !ok {
|
|
105
|
-
return fmt.Errorf("opencode agent %q does not exist", agent)
|
|
128
|
+
// ensureEnvironment is the unlogged body factored out so the public method
|
|
129
|
+
// can emit one outcome line (created/exists/error) without duplicate
|
|
130
|
+
// logging on the inner re-reads.
|
|
131
|
+
func (a *adapter) ensureEnvironment(ctx context.Context, spec runner.RunSpec) (runner.Environment, bool, error) {
|
|
132
|
+
repoID, err := a.repoID(ctx, spec.RepoName, spec.RepoPath)
|
|
133
|
+
if err != nil {
|
|
134
|
+
return runner.Environment{}, false, err
|
|
106
135
|
}
|
|
107
|
-
|
|
108
|
-
|
|
136
|
+
wts, err := a.cli.ListWorktrees(ctx)
|
|
137
|
+
if err != nil {
|
|
138
|
+
return runner.Environment{}, false, err
|
|
109
139
|
}
|
|
110
|
-
|
|
111
|
-
|
|
140
|
+
var main *orcacli.Worktree
|
|
141
|
+
for i := range wts {
|
|
142
|
+
w := &wts[i]
|
|
143
|
+
if w.RepoID != repoID {
|
|
144
|
+
continue
|
|
145
|
+
}
|
|
146
|
+
if w.DisplayName == spec.TicketKey {
|
|
147
|
+
return runner.Environment{ID: w.ID, Path: w.Path}, true, nil
|
|
148
|
+
}
|
|
149
|
+
if w.IsMainWorktree {
|
|
150
|
+
main = w
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
if main == nil {
|
|
154
|
+
return runner.Environment{}, false, fmt.Errorf("orca: repo %q has no main worktree", spec.RepoName)
|
|
155
|
+
}
|
|
156
|
+
baseRef := a.cfg.BaseRef
|
|
157
|
+
if existing, ok, findErr := orcacli.FindExistingBranch(main.Path, spec.TicketKey); findErr == nil && ok {
|
|
158
|
+
baseRef = existing
|
|
159
|
+
} else if baseRef == "" {
|
|
160
|
+
baseRef = primaryBranch(main)
|
|
161
|
+
}
|
|
162
|
+
if err := a.cli.CreateWorktree(ctx, spec.TicketKey, repoID, main.ID, baseRef); err != nil {
|
|
163
|
+
return runner.Environment{}, false, err
|
|
164
|
+
}
|
|
165
|
+
// Re-read to return the created worktree's identity.
|
|
166
|
+
wts, err = a.cli.ListWorktrees(ctx)
|
|
112
167
|
if err != nil {
|
|
113
|
-
return
|
|
168
|
+
return runner.Environment{}, false, err
|
|
114
169
|
}
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
170
|
+
for _, w := range wts {
|
|
171
|
+
if w.RepoID == repoID && w.DisplayName == spec.TicketKey {
|
|
172
|
+
return runner.Environment{ID: w.ID, Path: w.Path}, false, nil
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
return runner.Environment{}, false, fmt.Errorf("orca: worktree %q not found after create", spec.TicketKey)
|
|
119
176
|
}
|
|
120
177
|
|
|
121
|
-
//
|
|
122
|
-
//
|
|
123
|
-
//
|
|
124
|
-
//
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
178
|
+
// primaryBranch derives the base branch name from the repo's primary
|
|
179
|
+
// worktree reported by Orca. Orca returns a fully-qualified ref like
|
|
180
|
+
// "refs/heads/master"; normalize to the bare branch name. When the
|
|
181
|
+
// worktree's branch is empty (no primary branch recorded), fall back to
|
|
182
|
+
// "main".
|
|
183
|
+
func primaryBranch(w *orcacli.Worktree) string {
|
|
184
|
+
const prefix = "refs/heads/"
|
|
185
|
+
b := w.Branch
|
|
186
|
+
if strings.HasPrefix(b, prefix) {
|
|
187
|
+
b = strings.TrimPrefix(b, prefix)
|
|
188
|
+
}
|
|
189
|
+
if b == "" {
|
|
190
|
+
return "main"
|
|
132
191
|
}
|
|
133
|
-
return
|
|
134
|
-
strings.Join(parts, " "), shellQuote(agent), shellQuote(prompt))
|
|
192
|
+
return b
|
|
135
193
|
}
|
|
136
194
|
|
|
137
|
-
|
|
138
|
-
|
|
195
|
+
// --- Terminals ---
|
|
196
|
+
|
|
197
|
+
// CloseTerminal closes one terminal by handle.
|
|
198
|
+
func (a *adapter) CloseTerminal(ctx context.Context, terminal runner.Terminal) error {
|
|
199
|
+
slog.Debug("orca call", "op", "close-terminal", "title", terminal.Title, "handle", terminal.ID)
|
|
200
|
+
err := a.cli.CloseTerminal(ctx, terminal.ID)
|
|
201
|
+
if err != nil {
|
|
202
|
+
slog.Info("orca outcome", "op", "close-terminal", "title", terminal.Title, "result", "error", "error", sanitizeErr(err))
|
|
203
|
+
} else {
|
|
204
|
+
slog.Info("orca outcome", "op", "close-terminal", "title", terminal.Title, "result", "ok")
|
|
205
|
+
}
|
|
206
|
+
return err
|
|
139
207
|
}
|
|
140
208
|
|
|
141
|
-
//
|
|
142
|
-
//
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
209
|
+
// FindTerminal addresses a persisted handle directly. Normal execution never
|
|
210
|
+
// lists terminals or rediscovers by title.
|
|
211
|
+
func (a *adapter) FindTerminal(ctx context.Context, terminal runner.Terminal) (runner.Terminal, bool, error) {
|
|
212
|
+
slog.Debug("orca call", "op", "find-terminal", "title", terminal.Title, "handle", terminal.ID)
|
|
213
|
+
if terminal.ID == "" {
|
|
214
|
+
slog.Info("orca outcome", "op", "find-terminal", "title", terminal.Title, "result", "absent")
|
|
215
|
+
return runner.Terminal{}, false, nil
|
|
216
|
+
}
|
|
217
|
+
t, err := a.cli.ShowTerminal(ctx, terminal.ID)
|
|
218
|
+
if errors.Is(err, orcacli.ErrTerminalUnavailable) {
|
|
219
|
+
slog.Info("orca outcome", "op", "find-terminal", "title", terminal.Title, "result", "absent")
|
|
220
|
+
return runner.Terminal{}, false, nil
|
|
221
|
+
}
|
|
146
222
|
if err != nil {
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
}
|
|
150
|
-
return runner.Session{}, false, fmt.Errorf("terminal list: %w", err)
|
|
223
|
+
slog.Info("orca outcome", "op", "find-terminal", "title", terminal.Title, "result", "error", "error", sanitizeErr(err))
|
|
224
|
+
return runner.Terminal{}, false, err
|
|
151
225
|
}
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
return runner.Session{ID: term.Handle, Title: term.Title}, true, nil
|
|
156
|
-
}
|
|
226
|
+
if !t.Connected {
|
|
227
|
+
slog.Info("orca outcome", "op", "find-terminal", "title", terminal.Title, "result", "absent")
|
|
228
|
+
return runner.Terminal{}, false, nil
|
|
157
229
|
}
|
|
158
|
-
|
|
230
|
+
slog.Info("orca outcome", "op", "find-terminal", "title", terminal.Title, "result", "found")
|
|
231
|
+
return runner.Terminal{ID: t.Handle, Title: terminal.Title}, true, nil
|
|
159
232
|
}
|
|
160
233
|
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
234
|
+
func (a *adapter) SendTerminal(ctx context.Context, terminal runner.Terminal, text string) error {
|
|
235
|
+
return a.cli.SendTerminal(ctx, terminal.ID, text)
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
// CreateTerminal always creates a terminal; it performs no title discovery.
|
|
239
|
+
func (a *adapter) CreateTerminal(ctx context.Context, env runner.Environment, title string, command runner.Command) (runner.Terminal, error) {
|
|
240
|
+
name := strings.SplitN(title, ":", 2)[0]
|
|
241
|
+
handle, err := a.cli.CreateTerminal(ctx, name, title, shellCommand(command))
|
|
242
|
+
if err != nil {
|
|
243
|
+
return runner.Terminal{}, err
|
|
167
244
|
}
|
|
168
|
-
return
|
|
245
|
+
return runner.Terminal{ID: handle, Title: title}, nil
|
|
169
246
|
}
|
|
170
247
|
|
|
171
|
-
//
|
|
172
|
-
//
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
248
|
+
// EnsureTerminal checks the persisted terminal, then creates a replacement
|
|
249
|
+
// only when that terminal is unavailable. The title is used only for creation.
|
|
250
|
+
func (a *adapter) EnsureTerminal(ctx context.Context, env runner.Environment, stored runner.Terminal, title string, command runner.Command) (runner.Terminal, error) {
|
|
251
|
+
slog.Debug("orca call", "op", "ensure-terminal", "title", title, "envID", env.ID)
|
|
252
|
+
if t, ok, err := a.FindTerminal(ctx, stored); err != nil {
|
|
253
|
+
slog.Info("orca outcome", "op", "ensure-terminal", "title", title, "result", "error", "error", sanitizeErr(err))
|
|
254
|
+
return runner.Terminal{}, err
|
|
255
|
+
} else if ok {
|
|
256
|
+
slog.Info("orca outcome", "op", "ensure-terminal", "title", title, "result", "exists")
|
|
257
|
+
return t, nil
|
|
258
|
+
}
|
|
259
|
+
terminal, err := a.CreateTerminal(ctx, env, title, command)
|
|
176
260
|
if err != nil {
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
261
|
+
slog.Info("orca outcome", "op", "ensure-terminal", "title", title, "result", "error", "error", sanitizeErr(err))
|
|
262
|
+
return runner.Terminal{}, err
|
|
263
|
+
}
|
|
264
|
+
slog.Info("orca outcome", "op", "ensure-terminal", "title", title, "result", "created")
|
|
265
|
+
return terminal, nil
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
// sanitizeErr strips the leading "orca [args...]:" prefix from orcacli
|
|
269
|
+
// errors so info-level outcome lines never leak argv payloads (notably the
|
|
270
|
+
// --command string built by shellCommand, which carries the agent prompt
|
|
271
|
+
// and RELAY_FLOW_* env). Keeps the trailing stderr/exit fragment.
|
|
272
|
+
func sanitizeErr(err error) string {
|
|
273
|
+
if err == nil {
|
|
274
|
+
return ""
|
|
275
|
+
}
|
|
276
|
+
s := err.Error()
|
|
277
|
+
// "orca terminal create: orca [args...]: <err>: <out>" — drop the
|
|
278
|
+
// "[args...]" middle.
|
|
279
|
+
if i := strings.Index(s, "]: "); i >= 0 {
|
|
280
|
+
// Keep any "orca <words>:" prefix before the "[" (e.g. the
|
|
281
|
+
// "orca terminal create:" wrap), then append the post-] tail.
|
|
282
|
+
prefix := ""
|
|
283
|
+
if j := strings.Index(s, "["); j > 0 {
|
|
284
|
+
prefix = strings.TrimSuffix(s[:j], " ")
|
|
185
285
|
}
|
|
286
|
+
if prefix != "" {
|
|
287
|
+
return prefix + "]: " + s[i+3:]
|
|
288
|
+
}
|
|
289
|
+
return s[i+3:]
|
|
186
290
|
}
|
|
187
|
-
return
|
|
291
|
+
return s
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
// shellCommand renders the structured command as one shell line with env
|
|
295
|
+
// assignments; the runner executes it but never constructs it.
|
|
296
|
+
func shellCommand(c runner.Command) string {
|
|
297
|
+
var b strings.Builder
|
|
298
|
+
for _, k := range sortedKeys(c.Env) {
|
|
299
|
+
b.WriteString(k)
|
|
300
|
+
b.WriteString("=")
|
|
301
|
+
b.WriteString(shellQuote(c.Env[k]))
|
|
302
|
+
b.WriteString(" ")
|
|
303
|
+
}
|
|
304
|
+
b.WriteString(shellQuote(c.Executable))
|
|
305
|
+
for _, arg := range c.Args {
|
|
306
|
+
b.WriteString(" ")
|
|
307
|
+
b.WriteString(shellQuote(arg))
|
|
308
|
+
}
|
|
309
|
+
return b.String()
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
func shellQuote(s string) string {
|
|
313
|
+
return "'" + strings.ReplaceAll(s, "'", `'\''`) + "'"
|
|
188
314
|
}
|
|
189
315
|
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
316
|
+
func sortedKeys(m map[string]string) []string {
|
|
317
|
+
keys := make([]string, 0, len(m))
|
|
318
|
+
for k := range m {
|
|
319
|
+
keys = append(keys, k)
|
|
320
|
+
}
|
|
321
|
+
for i := 1; i < len(keys); i++ {
|
|
322
|
+
for j := i; j > 0 && keys[j] < keys[j-1]; j-- {
|
|
323
|
+
keys[j], keys[j-1] = keys[j-1], keys[j]
|
|
198
324
|
}
|
|
199
|
-
|
|
200
|
-
|
|
325
|
+
}
|
|
326
|
+
return keys
|
|
327
|
+
}
|
|
328
|
+
|
|
329
|
+
// --- Cleanup ---
|
|
330
|
+
|
|
331
|
+
// CloseTerminals closes the run's agent terminals while preserving the
|
|
332
|
+
// worktree and any non-run tabs (setup, user shells). Run-owned terminals
|
|
333
|
+
// are identified by the stable <ticket>:<node> title prefix.
|
|
334
|
+
func (a *adapter) CloseTerminals(ctx context.Context, spec runner.RunSpec) error {
|
|
335
|
+
slog.Debug("orca call", "op", "close-terminals", "ticket", spec.TicketKey, "runID", string(spec.RunID))
|
|
336
|
+
env, ok, err := a.findEnvironment(ctx, spec)
|
|
337
|
+
if err != nil || !ok {
|
|
338
|
+
if err != nil {
|
|
339
|
+
slog.Info("orca outcome", "op", "close-terminals", "ticket", spec.TicketKey, "result", "error", "error", sanitizeErr(err))
|
|
340
|
+
} else {
|
|
341
|
+
slog.Info("orca outcome", "op", "close-terminals", "ticket", spec.TicketKey, "result", "no-environment")
|
|
201
342
|
}
|
|
343
|
+
return err
|
|
202
344
|
}
|
|
203
|
-
|
|
345
|
+
terms, err := a.cli.ListTerminals(ctx, "id:"+env.ID)
|
|
204
346
|
if err != nil {
|
|
347
|
+
slog.Info("orca outcome", "op", "close-terminals", "ticket", spec.TicketKey, "result", "error", "error", sanitizeErr(err))
|
|
205
348
|
return err
|
|
206
349
|
}
|
|
207
|
-
|
|
350
|
+
prefix := spec.TicketKey + ":"
|
|
351
|
+
closed := 0
|
|
352
|
+
for _, t := range terms {
|
|
353
|
+
if !strings.HasPrefix(t.Title, prefix) {
|
|
354
|
+
continue
|
|
355
|
+
}
|
|
356
|
+
// 9.5: one outcome line per actual terminal close, with its title.
|
|
357
|
+
slog.Debug("orca call", "op", "close-terminal", "title", t.Title, "handle", t.Handle)
|
|
358
|
+
if err := a.cli.CloseTerminal(ctx, t.Handle); err != nil {
|
|
359
|
+
slog.Info("orca outcome", "op", "close-terminal", "title", t.Title, "result", "error", "error", sanitizeErr(err))
|
|
360
|
+
slog.Info("orca outcome", "op", "close-terminals", "ticket", spec.TicketKey, "result", "error", "error", sanitizeErr(err))
|
|
361
|
+
return fmt.Errorf("close terminal %q: %w", t.Title, err)
|
|
362
|
+
}
|
|
363
|
+
slog.Info("orca outcome", "op", "close-terminal", "title", t.Title, "result", "ok")
|
|
364
|
+
closed++
|
|
365
|
+
}
|
|
366
|
+
slog.Info("orca outcome", "op", "close-terminals", "ticket", spec.TicketKey, "result", "ok", "closed", closed)
|
|
367
|
+
return nil
|
|
368
|
+
}
|
|
369
|
+
|
|
370
|
+
// CleanupRun removes all runner-owned run resources: terminals, then the
|
|
371
|
+
// ticket worktree itself.
|
|
372
|
+
func (a *adapter) CleanupRun(ctx context.Context, spec runner.RunSpec) error {
|
|
373
|
+
slog.Debug("orca call", "op", "cleanup-run", "ticket", spec.TicketKey, "runID", string(spec.RunID))
|
|
374
|
+
if err := a.CloseTerminals(ctx, spec); err != nil {
|
|
375
|
+
slog.Info("orca outcome", "op", "cleanup-run", "ticket", spec.TicketKey, "result", "error", "error", sanitizeErr(err))
|
|
208
376
|
return err
|
|
209
377
|
}
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
if attempt < 2 {
|
|
217
|
-
r.sleep(2 * time.Second)
|
|
378
|
+
env, ok, err := a.findEnvironment(ctx, spec)
|
|
379
|
+
if err != nil || !ok {
|
|
380
|
+
if err != nil {
|
|
381
|
+
slog.Info("orca outcome", "op", "cleanup-run", "ticket", spec.TicketKey, "result", "error", "error", sanitizeErr(err))
|
|
382
|
+
} else {
|
|
383
|
+
slog.Info("orca outcome", "op", "cleanup-run", "ticket", spec.TicketKey, "result", "no-environment")
|
|
218
384
|
}
|
|
385
|
+
return err
|
|
219
386
|
}
|
|
220
|
-
|
|
387
|
+
err = a.cli.DeleteWorktree(ctx, env.ID)
|
|
388
|
+
if err != nil {
|
|
389
|
+
slog.Info("orca outcome", "op", "cleanup-run", "ticket", spec.TicketKey, "result", "error", "error", sanitizeErr(err))
|
|
390
|
+
} else {
|
|
391
|
+
slog.Info("orca outcome", "op", "cleanup-run", "ticket", spec.TicketKey, "result", "ok")
|
|
392
|
+
}
|
|
393
|
+
return err
|
|
221
394
|
}
|
|
222
395
|
|
|
223
|
-
//
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
// parent info that tasks.Ticket doesn't carry — deferred; YAGNI until a
|
|
227
|
-
// tracker adapter exposes them.)
|
|
228
|
-
func (r *orcaRunner) resolveWorktreeParent(t tasks.Ticket) (parentWorktreeID, baseBranch string, err error) {
|
|
229
|
-
w, ok, err := r.orca.MainWorktree(r.repoID)
|
|
396
|
+
// findEnvironment locates the ticket worktree without creating it.
|
|
397
|
+
func (a *adapter) findEnvironment(ctx context.Context, spec runner.RunSpec) (runner.Environment, bool, error) {
|
|
398
|
+
repoID, err := a.repoID(ctx, spec.RepoName, spec.RepoPath)
|
|
230
399
|
if err != nil {
|
|
231
|
-
return
|
|
400
|
+
return runner.Environment{}, false, err
|
|
232
401
|
}
|
|
233
|
-
|
|
234
|
-
|
|
402
|
+
wts, err := a.cli.ListWorktrees(ctx)
|
|
403
|
+
if err != nil {
|
|
404
|
+
return runner.Environment{}, false, err
|
|
235
405
|
}
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
return w.ID, existing, nil
|
|
406
|
+
for _, w := range wts {
|
|
407
|
+
if w.RepoID == repoID && w.DisplayName == spec.TicketKey {
|
|
408
|
+
return runner.Environment{ID: w.ID, Path: w.Path}, true, nil
|
|
409
|
+
}
|
|
241
410
|
}
|
|
242
|
-
return
|
|
411
|
+
return runner.Environment{}, false, nil
|
|
243
412
|
}
|