relay-flow 0.2.7-alpha → 0.2.9-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 (52) hide show
  1. package/README.md +76 -37
  2. package/cmd/relay-flow/commands_test.go +199 -19
  3. package/cmd/relay-flow/main.go +183 -144
  4. package/cmd/relay-flow/onboarding.go +344 -0
  5. package/cmd/relay-flow/onboarding_test.go +515 -0
  6. package/cmd/relay-flow/repo_registration.go +454 -0
  7. package/cmd/relay-flow/serve.go +5 -2
  8. package/examples/config-reference.yaml +7 -0
  9. package/go.mod +12 -8
  10. package/go.sum +24 -17
  11. package/internal/execution/goworkflows/engine_test.go +3 -3
  12. package/internal/harness/opencode/opencode_test.go +4 -4
  13. package/internal/harness/opencode/repo_setup.go +1 -1
  14. package/internal/harness/pi/prompt_test.go +3 -3
  15. package/internal/repo/service.go +47 -3
  16. package/internal/repo/service_test.go +36 -1
  17. package/internal/runner/herdr/herdr.go +103 -4
  18. package/internal/runner/herdr/herdr_test.go +72 -2
  19. package/internal/runner/herdr/herdrcli/contract.go +12 -3
  20. package/internal/runner/herdr/herdrcli/herdrcli_test.go +17 -2
  21. package/internal/runner/herdr/herdrcli/operations.go +16 -0
  22. package/internal/runner/herdr/herdrcli/testdata/strict-herdr.sh +8 -0
  23. package/internal/runner/herdr/herdrcli/testdata/workspace-create.json +1 -0
  24. package/internal/runner/orca/orca.go +68 -9
  25. package/internal/runner/orca/orca_test.go +134 -2
  26. package/internal/runner/orca/orcacli/orcacli.go +21 -1
  27. package/internal/runner/orca/orcacli/orcacli_test.go +22 -0
  28. package/internal/runner/orca/orcacli/testdata/strict-orca.sh +5 -0
  29. package/internal/runner/runner.go +9 -0
  30. package/internal/server/api_test.go +65 -0
  31. package/internal/server/client.go +40 -2
  32. package/internal/server/fixture_test.go +27 -16
  33. package/internal/server/server.go +80 -4
  34. package/internal/task/beads/beads.go +7 -2
  35. package/internal/task/beads/beads_test.go +13 -0
  36. package/internal/task/factory.go +9 -7
  37. package/internal/task/jira/auth_test.go +9 -1
  38. package/internal/task/jira/filters_test.go +2 -2
  39. package/internal/task/jira/helpers_test.go +13 -0
  40. package/internal/task/jira/jira.go +233 -44
  41. package/internal/task/jira/lifecycle_inheritance_test.go +5 -5
  42. package/internal/task/jira/rest/client.go +73 -31
  43. package/internal/task/jira/rest/client_test.go +25 -0
  44. package/internal/task/jira/status_defaults_test.go +134 -0
  45. package/internal/task/jira/templates_test.go +2 -2
  46. package/internal/task/jira/testdata/jira_search_issues.json +4 -4
  47. package/internal/task/jira/transition_defaults_test.go +8 -12
  48. package/internal/task/jira/validation_test.go +3 -1
  49. package/internal/task/registration.go +59 -0
  50. package/internal/workflow/workflow.go +4 -1
  51. package/internal/workflow/workflow_test.go +4 -4
  52. package/package.json +1 -1
@@ -10,7 +10,7 @@ import (
10
10
  "github.com/rajpopat27/relay-flow/internal/config"
11
11
  )
12
12
 
13
- const relayFlowPlugin = "relay-flow-plugin@0.2.7-alpha"
13
+ const relayFlowPlugin = "relay-flow-plugin@0.2.9-alpha"
14
14
 
15
15
  type jsoncToken struct {
16
16
  kind byte
@@ -23,13 +23,13 @@ func TestPiRenderPromptSubstitutesInitialAndFeedbackData(t *testing.T) {
23
23
  NextSteps: "review (when: ready)",
24
24
  Mailbox: "PAY-234",
25
25
  }
26
- nudge := "nudge {{taskSystem}}|{{ticket}}|{{workflow}}|{{repo}}|{{node}}|{{nextSteps}}"
26
+ nudge := "nudge {{taskSystem}}|{{ticket}}|{{workflow}}|{{repo}}|{{node}}|{{mailbox}}|{{nextSteps}}"
27
27
 
28
28
  initial, err := h.RenderPrompt(harness.PromptInitial, data, nudge)
29
29
  if err != nil {
30
30
  t.Fatalf("RenderPrompt(initial): %v", err)
31
31
  }
32
- wantInitial := "Task system: jira\nUse the jira tools to read the parent ticket PAY-101.\n\nYour mailbox is PAY-234. Read its description and comments for node instructions and feedback.\n\nKeep the summary brief, and make the feedback as detailed and actionable as possible for the next agent.\n\nnudge jira|PAY-101|basicFlow|payments|implement|review (when: ready)"
32
+ wantInitial := "Task system: jira\nUse the jira tools to read the parent ticket PAY-101.\n\nYour mailbox is PAY-234. Read its description and comments for node instructions and feedback.\n\nKeep the summary brief, and make the feedback as detailed and actionable as possible for the next agent.\n\nnudge jira|PAY-101|basicFlow|payments|implement|PAY-234|review (when: ready)"
33
33
  if initial != wantInitial {
34
34
  t.Fatalf("initial prompt = %q, want %q", initial, wantInitial)
35
35
  }
@@ -38,7 +38,7 @@ func TestPiRenderPromptSubstitutesInitialAndFeedbackData(t *testing.T) {
38
38
  if err != nil {
39
39
  t.Fatalf("RenderPrompt(feedback): %v", err)
40
40
  }
41
- wantFeedback := "New feedback was added to the comments section of your mailbox subtask PAY-234. Read it.\n\nnudge jira|PAY-101|basicFlow|payments|implement|review (when: ready)"
41
+ wantFeedback := "New feedback was added to the comments section of your mailbox subtask PAY-234. Read it.\n\nnudge jira|PAY-101|basicFlow|payments|implement|PAY-234|review (when: ready)"
42
42
  if feedback != wantFeedback {
43
43
  t.Fatalf("feedback prompt = %q, want %q", feedback, wantFeedback)
44
44
  }
@@ -4,6 +4,7 @@ import (
4
4
  "context"
5
5
  "fmt"
6
6
  "path/filepath"
7
+ "strings"
7
8
 
8
9
  "github.com/rajpopat27/relay-flow/internal/config"
9
10
  "github.com/rajpopat27/relay-flow/internal/harness"
@@ -70,6 +71,25 @@ func (s *Service) Discover(ctx context.Context) ([]runner.RepoCandidate, error)
70
71
  return s.runner.DiscoverRepos(ctx)
71
72
  }
72
73
 
74
+ // EnsureRepo makes the runner resource for a repository available without
75
+ // changing relay-flow's machine configuration. Runner adapters that expose
76
+ // RepoRegistrar perform their idempotent external provisioning; other
77
+ // runners validate the repository through the existing read-only contract.
78
+ func (s *Service) EnsureRepo(ctx context.Context, name, path string) error {
79
+ name = strings.TrimSpace(name)
80
+ path = strings.TrimSpace(path)
81
+ if name == "" {
82
+ return fmt.Errorf("repo: name is required")
83
+ }
84
+ if path == "" {
85
+ return fmt.Errorf("repo %q: path is required", name)
86
+ }
87
+ if registrar, ok := s.runner.(runner.RepoRegistrar); ok {
88
+ return registrar.EnsureRepo(ctx, name, path)
89
+ }
90
+ return s.runner.ValidateRepo(ctx, name, path)
91
+ }
92
+
73
93
  // RequiredRepoKeys delegates to the task factory's method of the same name.
74
94
  func (s *Service) RequiredRepoKeys() []string {
75
95
  keys, err := task.RequiredRepoKeys(s.taskPlugin)
@@ -79,6 +99,12 @@ func (s *Service) RequiredRepoKeys() []string {
79
99
  return keys
80
100
  }
81
101
 
102
+ // RegistrationFields returns task-plugin-owned registration prompts and
103
+ // choices. Core repo management does not interpret the returned keys.
104
+ func (s *Service) RegistrationFields(ctx context.Context, values config.RawValues) ([]task.RegistrationField, error) {
105
+ return task.RegistrationFields(ctx, s.taskPlugin, values)
106
+ }
107
+
82
108
  type RegisterInput struct {
83
109
  Name string
84
110
  Path string
@@ -89,9 +115,14 @@ type RegisterInput struct {
89
115
  // connectivity, duplicate names, canonical paths, and the task plugin's
90
116
  // registration identity before atomically writing machine config.
91
117
  func (s *Service) Register(ctx context.Context, input RegisterInput) (Info, error) {
118
+ input.Name = strings.TrimSpace(input.Name)
119
+ input.Path = strings.TrimSpace(input.Path)
92
120
  if input.Name == "" {
93
121
  return Info{}, fmt.Errorf("repo: name is required")
94
122
  }
123
+ if input.Path == "" {
124
+ return Info{}, fmt.Errorf("repo %q: path is required", input.Name)
125
+ }
95
126
  cfg, err := config.LoadMachine(s.cfgPath)
96
127
  if err != nil {
97
128
  return Info{}, err
@@ -116,6 +147,13 @@ func (s *Service) Register(ctx context.Context, input RegisterInput) (Info, erro
116
147
  return Info{}, fmt.Errorf("repo %q: required task config key %q is missing", input.Name, k)
117
148
  }
118
149
  }
150
+ if err := task.ValidateRegistration(ctx, s.taskPlugin, task.RepoRegistrationSpec{
151
+ Name: input.Name,
152
+ RootConfig: cfg.TaskConfig,
153
+ RepoConfig: input.TaskConfig,
154
+ }); err != nil {
155
+ return Info{}, fmt.Errorf("repo %q: %w", input.Name, err)
156
+ }
119
157
  // Duplicate registration identity: compute this repo's identity and compare
120
158
  // against every registered repo. Most plugins use their physical task scope;
121
159
  // Beads includes its derived repository label so distinct repositories may
@@ -141,8 +179,10 @@ func (s *Service) Register(ctx context.Context, input RegisterInput) (Info, erro
141
179
  return Info{}, fmt.Errorf("repo %q: task registration identity already used by repo %q", input.Name, name)
142
180
  }
143
181
  }
144
- // Runner validates the repo.
145
- if err := s.runner.ValidateRepo(ctx, input.Name, input.Path); err != nil {
182
+ // Runner validates or provisions the repo, depending on the adapter
183
+ // capability. Provisioning is idempotent so explicit registration and the
184
+ // interactive Add action share one runner-owned path.
185
+ if err := s.EnsureRepo(ctx, input.Name, input.Path); err != nil {
146
186
  return Info{}, fmt.Errorf("repo %q: runner validation: %w", input.Name, err)
147
187
  }
148
188
  // Task-system connectivity: construct the repo-bound System.
@@ -232,5 +272,9 @@ func canonicalPath(p string) string {
232
272
  if err != nil {
233
273
  return filepath.Clean(p)
234
274
  }
235
- return filepath.Clean(abs)
275
+ abs = filepath.Clean(abs)
276
+ if resolved, err := filepath.EvalSymlinks(abs); err == nil {
277
+ return filepath.Clean(resolved)
278
+ }
279
+ return abs
236
280
  }
@@ -23,6 +23,17 @@ type fakeRunnerDiscovery struct {
23
23
  validErr error
24
24
  }
25
25
 
26
+ type fakeRunnerRegistrar struct {
27
+ *fakeRunnerDiscovery
28
+ ensureErr error
29
+ ensureCalls []runner.RepoCandidate
30
+ }
31
+
32
+ func (f *fakeRunnerRegistrar) EnsureRepo(_ context.Context, name, path string) error {
33
+ f.ensureCalls = append(f.ensureCalls, runner.RepoCandidate{Name: name, Path: path})
34
+ return f.ensureErr
35
+ }
36
+
26
37
  func (f *fakeRunnerDiscovery) DiscoverRepos(context.Context) ([]runner.RepoCandidate, error) {
27
38
  return f.candidates, nil
28
39
  }
@@ -228,6 +239,17 @@ func TestRegisterValidatesRunnerRepo(t *testing.T) {
228
239
  }
229
240
  }
230
241
 
242
+ func TestRegisterUsesRunnerRepoProvisioningCapability(t *testing.T) {
243
+ rn := &fakeRunnerRegistrar{fakeRunnerDiscovery: &fakeRunnerDiscovery{validErr: errInvalidRepo{}}}
244
+ fx := newServiceFixture(t, rn)
245
+ if _, err := fx.svc.Register(context.Background(), repo.RegisterInput{Name: "payments", Path: "/srv/payments", TaskConfig: config.RawValues{"project": "P", "component": "c"}}); err != nil {
246
+ t.Fatal(err)
247
+ }
248
+ if len(rn.ensureCalls) != 1 || rn.ensureCalls[0] != (runner.RepoCandidate{Name: "payments", Path: "/srv/payments"}) {
249
+ t.Fatalf("EnsureRepo calls = %+v", rn.ensureCalls)
250
+ }
251
+ }
252
+
231
253
  func TestRegisterValidatesTaskConnectivity(t *testing.T) {
232
254
  // The task factory's New is invoked to validate connectivity; its error
233
255
  // must reject registration before persisting.
@@ -264,7 +286,10 @@ func (errInvalidRepo) Error() string { return "invalid repo" }
264
286
 
265
287
  func TestRegisterAtomicallyPersists(t *testing.T) {
266
288
  fx := newServiceFixture(t, &fakeRunnerDiscovery{})
267
- if _, err := fx.svc.Register(context.Background(), repo.RegisterInput{Name: "payments", Path: "/srv/payments", TaskConfig: config.RawValues{"project": "PAY", "component": "api"}}); err != nil {
289
+ statusDefaults := map[string]any{"start": "Open", "work": "Working", "end": "Closed"}
290
+ if _, err := fx.svc.Register(context.Background(), repo.RegisterInput{Name: "payments", Path: "/srv/payments", TaskConfig: config.RawValues{
291
+ "project": "PAY", "component": "api", "statusDefaults": statusDefaults,
292
+ }}); err != nil {
268
293
  t.Fatal(err)
269
294
  }
270
295
  cfg, err := config.LoadMachine(fx.cfgPath)
@@ -278,6 +303,16 @@ func TestRegisterAtomicallyPersists(t *testing.T) {
278
303
  if r.TaskConfig["project"] != "PAY" {
279
304
  t.Fatalf("repo taskConfig not persisted: %+v", r.TaskConfig)
280
305
  }
306
+ var gotDefaults map[string]any
307
+ switch values := r.TaskConfig["statusDefaults"].(type) {
308
+ case map[string]any:
309
+ gotDefaults = values
310
+ case config.RawValues:
311
+ gotDefaults = map[string]any(values)
312
+ }
313
+ if gotDefaults == nil || gotDefaults["start"] != "Open" || gotDefaults["work"] != "Working" || gotDefaults["end"] != "Closed" {
314
+ t.Fatalf("repo statusDefaults not persisted: %#v", r.TaskConfig["statusDefaults"])
315
+ }
281
316
  }
282
317
 
283
318
  func TestRegisterSetsUpHarnessRepo(t *testing.T) {
@@ -91,11 +91,15 @@ func (a *adapter) DiscoverRepos(ctx context.Context) ([]runner.RepoCandidate, er
91
91
  }
92
92
  candidates := make(map[string]runner.RepoCandidate)
93
93
  for _, workspace := range snapshot.Workspaces {
94
- root := normalizePath(workspace.Worktree.RepoRoot)
95
- if root == "" {
94
+ candidate, ok, err := a.workspaceRepository(ctx, workspace, snapshot.Panes)
95
+ if err != nil {
96
+ logOutcome("discover-repos", "error")
97
+ return nil, err
98
+ }
99
+ if !ok {
96
100
  continue
97
101
  }
98
- candidates[root] = runner.RepoCandidate{Name: workspace.Worktree.RepoName, Path: root}
102
+ candidates[canonicalCandidatePath(candidate.Path)] = candidate
99
103
  }
100
104
  out := make([]runner.RepoCandidate, 0, len(candidates))
101
105
  for _, candidate := range candidates {
@@ -111,8 +115,102 @@ func (a *adapter) DiscoverRepos(ctx context.Context) ([]runner.RepoCandidate, er
111
115
  return out, nil
112
116
  }
113
117
 
118
+ // workspaceRepository resolves the Git identity of a workspace. Herdr's
119
+ // snapshot includes worktree metadata for linked worktrees, but plain source
120
+ // workspaces may omit the worktree object; their pane CWD is the supported
121
+ // lookup handle for worktree list.
122
+ func (a *adapter) workspaceRepository(ctx context.Context, workspace herdrcli.Workspace, panes []herdrcli.Pane) (runner.RepoCandidate, bool, error) {
123
+ if root := normalizePath(workspace.Worktree.RepoRoot); root != "" {
124
+ name := workspace.Worktree.RepoName
125
+ if name == "" {
126
+ name = workspace.Label
127
+ }
128
+ return runner.RepoCandidate{Name: name, Path: root}, true, nil
129
+ }
130
+ for _, pane := range panes {
131
+ if pane.WorkspaceID != workspace.ID || !filepath.IsAbs(strings.TrimSpace(pane.CWD)) {
132
+ continue
133
+ }
134
+ listing, err := a.cli.WorktreeList(ctx, pane.CWD)
135
+ if errors.Is(err, herdrcli.ErrNotGitWorktree) {
136
+ continue
137
+ }
138
+ if err != nil {
139
+ return runner.RepoCandidate{}, false, err
140
+ }
141
+ root := normalizePath(listing.Source.RepoRoot)
142
+ if root == "" {
143
+ continue
144
+ }
145
+ name := listing.Source.RepoName
146
+ if name == "" {
147
+ name = workspace.Label
148
+ }
149
+ return runner.RepoCandidate{Name: name, Path: root}, true, nil
150
+ }
151
+ return runner.RepoCandidate{}, false, nil
152
+ }
153
+
154
+ func canonicalCandidatePath(path string) string {
155
+ if normalized := normalizePath(path); normalized != "" {
156
+ return normalized
157
+ }
158
+ return path
159
+ }
160
+
161
+ // EnsureRepo opens a source repository as a Herdr workspace when it is not
162
+ // already open. WorktreeList is the source of truth for both the repository
163
+ // root and the source workspace handle; it is checked before workspace create
164
+ // so repeated registration remains idempotent even when snapshots omit
165
+ // worktree metadata for plain source workspaces.
166
+ func (a *adapter) EnsureRepo(ctx context.Context, name, path string) error {
167
+ attrs := []any{"repo", name}
168
+ logCall("ensure-repo", attrs...)
169
+ root := normalizePath(path)
170
+ if root == "" {
171
+ logOutcome("ensure-repo", "error", attrs...)
172
+ return fmt.Errorf("herdr: repository %q has an empty path", name)
173
+ }
174
+ listing, err := a.cli.WorktreeList(ctx, root)
175
+ if err != nil {
176
+ logOutcome("ensure-repo", "error", attrs...)
177
+ return err
178
+ }
179
+ if reported := normalizePath(listing.Source.RepoRoot); reported != root {
180
+ logOutcome("ensure-repo", "error", attrs...)
181
+ return fmt.Errorf("herdr: repository %q path %q is not its repository root %q", name, path, reported)
182
+ }
183
+ if hasOpenSourceWorkspace(listing) {
184
+ logOutcome("ensure-repo", "exists", attrs...)
185
+ return nil
186
+ }
187
+ registrar, ok := a.cli.(herdrcli.RepositoryRegistrar)
188
+ if !ok {
189
+ logOutcome("ensure-repo", "error", attrs...)
190
+ return fmt.Errorf("herdr: repository workspace provisioning is unavailable")
191
+ }
192
+ if _, err := registrar.CreateWorkspace(ctx, root, name); err != nil {
193
+ logOutcome("ensure-repo", "error", attrs...)
194
+ return err
195
+ }
196
+ logOutcome("ensure-repo", "created", attrs...)
197
+ return nil
198
+ }
199
+
200
+ func hasOpenSourceWorkspace(listing herdrcli.WorktreeListing) bool {
201
+ if listing.Source.SourceWorkspaceID != "" {
202
+ return true
203
+ }
204
+ for _, worktree := range listing.Worktrees {
205
+ if !worktree.IsLinked && worktree.OpenWorkspaceID != "" {
206
+ return true
207
+ }
208
+ }
209
+ return false
210
+ }
211
+
114
212
  // ValidateRepo verifies the registered path is the root of a Git repository
115
- // Herdr can manage. Registration needs no Herdr workspace and creates none.
213
+ // Herdr can manage without creating a workspace.
116
214
  func (a *adapter) ValidateRepo(ctx context.Context, name, path string) error {
117
215
  logCall("validate-repo", "repo", name)
118
216
  registered := normalizePath(path)
@@ -590,3 +688,4 @@ func normalizePath(path string) string {
590
688
  }
591
689
 
592
690
  var _ runner.Runner = (*adapter)(nil)
691
+ var _ runner.RepoRegistrar = (*adapter)(nil)
@@ -29,8 +29,11 @@ type fakeClient struct {
29
29
  openWorkspace herdrcli.Workspace
30
30
  openErr error
31
31
 
32
- createWorkspace herdrcli.Workspace
33
- createErr error
32
+ createWorkspace herdrcli.Workspace
33
+ createErr error
34
+ createdSourceWorkspace herdrcli.Workspace
35
+ createdSourceErr error
36
+ workspaceCreateCalls []worktreeCall
34
37
 
35
38
  tabs []herdrcli.Tab
36
39
  tabsErr error
@@ -95,6 +98,13 @@ func (f *fakeClient) WorktreeOpen(_ context.Context, repoPath, branch, label str
95
98
  return f.openWorkspace, f.openErr
96
99
  }
97
100
 
101
+ func (f *fakeClient) CreateWorkspace(_ context.Context, repoPath, label string) (herdrcli.Workspace, error) {
102
+ f.mu.Lock()
103
+ defer f.mu.Unlock()
104
+ f.workspaceCreateCalls = append(f.workspaceCreateCalls, worktreeCall{RepoPath: repoPath, Label: label})
105
+ return f.createdSourceWorkspace, f.createdSourceErr
106
+ }
107
+
98
108
  func (f *fakeClient) CreateTab(_ context.Context, workspaceID, cwd, label string) (herdrcli.Tab, herdrcli.Pane, error) {
99
109
  f.mu.Lock()
100
110
  defer f.mu.Unlock()
@@ -240,6 +250,26 @@ func TestDiscoverReposDeduplicatesRepositoryRoots(t *testing.T) {
240
250
  }
241
251
  }
242
252
 
253
+ func TestDiscoverReposResolvesPlainSourceWorkspaceFromPaneCWD(t *testing.T) {
254
+ cli := &fakeClient{
255
+ snapshot: herdrcli.Snapshot{
256
+ Workspaces: []herdrcli.Workspace{{ID: "w-source", Label: "payments"}},
257
+ Panes: []herdrcli.Pane{{ID: "w-source:p1", WorkspaceID: "w-source", CWD: repoPath}},
258
+ },
259
+ listing: herdrcli.WorktreeListing{
260
+ Source: herdrcli.WorktreeSource{RepoName: "payments", RepoRoot: repoPath, SourceCheckoutPath: repoPath},
261
+ },
262
+ }
263
+ got, err := newAdapter(cli).DiscoverRepos(context.Background())
264
+ if err != nil {
265
+ t.Fatal(err)
266
+ }
267
+ want := []runner.RepoCandidate{{Name: "payments", Path: repoPath}}
268
+ if len(got) != 1 || got[0] != want[0] {
269
+ t.Fatalf("DiscoverRepos = %+v, want %+v", got, want)
270
+ }
271
+ }
272
+
243
273
  func TestValidateRepoAcceptsRepositoryRootAndRejectsInnerPaths(t *testing.T) {
244
274
  cli := &fakeClient{listing: herdrcli.WorktreeListing{
245
275
  Source: herdrcli.WorktreeSource{RepoName: "payments", RepoRoot: repoPath, SourceCheckoutPath: repoPath},
@@ -278,6 +308,46 @@ func TestValidateRepoCreatesNothing(t *testing.T) {
278
308
  }
279
309
  }
280
310
 
311
+ func TestEnsureRepoReusesAnOpenSourceWorkspace(t *testing.T) {
312
+ cli := &fakeClient{
313
+ listing: herdrcli.WorktreeListing{Source: herdrcli.WorktreeSource{RepoRoot: repoPath, SourceWorkspaceID: "w-source"}},
314
+ snapshot: herdrcli.Snapshot{Workspaces: []herdrcli.Workspace{{ID: "w-source"}}},
315
+ }
316
+ a := newAdapter(cli)
317
+ if err := a.EnsureRepo(context.Background(), "payments", repoPath); err != nil {
318
+ t.Fatal(err)
319
+ }
320
+ if err := a.EnsureRepo(context.Background(), "payments", repoPath); err != nil {
321
+ t.Fatal(err)
322
+ }
323
+ if len(cli.workspaceCreateCalls) != 0 {
324
+ t.Fatalf("CreateWorkspace calls = %+v, want none", cli.workspaceCreateCalls)
325
+ }
326
+ }
327
+
328
+ func TestEnsureRepoOpensMissingSourceWorkspace(t *testing.T) {
329
+ cli := &fakeClient{
330
+ listing: herdrcli.WorktreeListing{Source: herdrcli.WorktreeSource{RepoRoot: repoPath}},
331
+ createdSourceWorkspace: herdrcli.Workspace{ID: "source-workspace"},
332
+ }
333
+ if err := newAdapter(cli).EnsureRepo(context.Background(), "payments", repoPath); err != nil {
334
+ t.Fatal(err)
335
+ }
336
+ if len(cli.workspaceCreateCalls) != 1 || cli.workspaceCreateCalls[0] != (worktreeCall{RepoPath: repoPath, Label: "payments"}) {
337
+ t.Fatalf("CreateWorkspace calls = %+v", cli.workspaceCreateCalls)
338
+ }
339
+ }
340
+
341
+ func TestEnsureRepoPropagatesSourceWorkspaceFailure(t *testing.T) {
342
+ cli := &fakeClient{
343
+ listing: herdrcli.WorktreeListing{Source: herdrcli.WorktreeSource{RepoRoot: repoPath}},
344
+ createdSourceErr: errors.New("herdr unavailable"),
345
+ }
346
+ if err := newAdapter(cli).EnsureRepo(context.Background(), "payments", repoPath); err == nil || !strings.Contains(err.Error(), "herdr unavailable") {
347
+ t.Fatalf("EnsureRepo error = %v, want workspace creation failure", err)
348
+ }
349
+ }
350
+
281
351
  // --- Environment ---
282
352
 
283
353
  func TestEnsureEnvironmentReusesExistingTicketWorktree(t *testing.T) {
@@ -35,6 +35,13 @@ type Client interface {
35
35
  CloseWorkspace(ctx context.Context, workspaceID string) error
36
36
  }
37
37
 
38
+ // RepositoryRegistrar is the optional repository-workspace provisioning seam
39
+ // used by the Herdr runner adapter. It remains separate from Client so
40
+ // existing runtime-only test clients do not need to implement registration.
41
+ type RepositoryRegistrar interface {
42
+ CreateWorkspace(ctx context.Context, cwd, label string) (Workspace, error)
43
+ }
44
+
38
45
  // Options selects the Herdr session or explicit socket used by the CLI.
39
46
  type Options struct {
40
47
  Session string
@@ -51,9 +58,10 @@ func New(options Options) *CLI {
51
58
  return &CLI{options: options}
52
59
  }
53
60
 
54
- // WorkspaceWorktree is the Git identity Herdr reports for a workspace. The
55
- // source checkout of a repository has IsLinked false; every ticket worktree
56
- // workspace has IsLinked true and the same RepoRoot.
61
+ // WorkspaceWorktree is the Git identity Herdr may report for a workspace.
62
+ // Linked ticket workspaces have IsLinked true and the source RepoRoot. Plain
63
+ // source workspaces can omit this object; callers resolve those through their
64
+ // pane CWD and worktree list.
57
65
  type WorkspaceWorktree struct {
58
66
  CheckoutPath string
59
67
  RepoName string
@@ -82,6 +90,7 @@ type WorktreeSource struct {
82
90
  RepoName string
83
91
  RepoRoot string
84
92
  SourceCheckoutPath string
93
+ SourceWorkspaceID string
85
94
  }
86
95
 
87
96
  // WorktreeListing is the response of worktree list for one repository.
@@ -24,6 +24,9 @@ func TestCLIUsesExactProductionCommandShapes(t *testing.T) {
24
24
  if _, err := cli.WorktreeOpen(ctx, "/work/payments", "PAY-101", "PAY-101"); err != nil {
25
25
  t.Fatalf("WorktreeOpen: %v", err)
26
26
  }
27
+ if _, err := cli.CreateWorkspace(ctx, "/work/payments", "payments"); err != nil {
28
+ t.Fatalf("CreateWorkspace: %v", err)
29
+ }
27
30
  if _, err := cli.WorktreeCreate(ctx, "/work/payments", "PAY-101", "origin/main", "PAY-101"); err != nil {
28
31
  t.Fatalf("WorktreeCreate: %v", err)
29
32
  }
@@ -85,11 +88,19 @@ func TestCLIDecodesCapturedResponseLocations(t *testing.T) {
85
88
  t.Fatalf("Snapshot panes = %+v", snapshot.Panes)
86
89
  }
87
90
 
91
+ sourceWorkspace, err := cli.CreateWorkspace(ctx, "/work/payments", "payments")
92
+ if err != nil {
93
+ t.Fatalf("CreateWorkspace: %v", err)
94
+ }
95
+ if sourceWorkspace.ID != "w1" || sourceWorkspace.Label != "payments" || sourceWorkspace.Worktree.RepoRoot != "" {
96
+ t.Fatalf("CreateWorkspace = %+v, want the captured plain source workspace shape", sourceWorkspace)
97
+ }
98
+
88
99
  listing, err := cli.WorktreeList(ctx, "/work/payments")
89
100
  if err != nil {
90
101
  t.Fatalf("WorktreeList: %v", err)
91
102
  }
92
- if listing.Source.RepoRoot != "/work/payments" || listing.Source.RepoName != "repo" {
103
+ if listing.Source.RepoRoot != "/work/payments" || listing.Source.RepoName != "repo" || listing.Source.SourceWorkspaceID != "w1" {
93
104
  t.Fatalf("WorktreeList source = %+v", listing.Source)
94
105
  }
95
106
  if len(listing.Worktrees) != 2 {
@@ -231,8 +242,12 @@ func TestCLIRejectsRelativeCWD(t *testing.T) {
231
242
 
232
243
  func TestStrictFakeHerdrRejectsUnsupportedProductionShapes(t *testing.T) {
233
244
  fake := installStrictFakeHerdr(t)
245
+ // This test invokes the fake executable directly rather than through CLI,
246
+ // so provide the same selector environment the production wrapper sets.
247
+ t.Setenv("HERDR_SESSION", "relay-flow")
248
+ t.Setenv("HERDR_SOCKET_PATH", "/tmp/relay-flow-herdr.sock")
234
249
  unsupported := [][]string{
235
- {"workspace", "create", "--cwd", "/work/payments", "--label", "payments", "--no-focus"},
250
+ {"workspace", "get", "w2"},
236
251
  {"worktree", "remove", "--workspace", "w2"},
237
252
  {"terminal", "create", "--pane", "w2:p2"},
238
253
  {"pane", "get", "--pane", "w2:p2"},
@@ -26,6 +26,7 @@ type worktreeSourceResponse struct {
26
26
  RepoName string `json:"repo_name"`
27
27
  RepoRoot string `json:"repo_root"`
28
28
  SourceCheckoutPath string `json:"source_checkout_path"`
29
+ SourceWorkspaceID string `json:"source_workspace_id"`
29
30
  }
30
31
 
31
32
  type tabResponse struct {
@@ -72,6 +73,20 @@ func (c *CLI) Snapshot(ctx context.Context) (Snapshot, error) {
72
73
  }, nil
73
74
  }
74
75
 
76
+ // CreateWorkspace opens a repository path as a Herdr workspace. A source
77
+ // workspace is enough for repository discovery; ticket worktrees are still
78
+ // created lazily by EnsureEnvironment.
79
+ func (c *CLI) CreateWorkspace(ctx context.Context, cwd, label string) (Workspace, error) {
80
+ var response struct {
81
+ Workspace workspaceResponse `json:"workspace"`
82
+ }
83
+ if err := c.runJSON(ctx, "workspace create", &response,
84
+ "workspace", "create", "--cwd", cwd, "--label", label, "--no-focus"); err != nil {
85
+ return Workspace{}, err
86
+ }
87
+ return convertWorkspace(response.Workspace), nil
88
+ }
89
+
75
90
  func (c *CLI) WorktreeList(ctx context.Context, repoPath string) (WorktreeListing, error) {
76
91
  var response struct {
77
92
  Source worktreeSourceResponse `json:"source"`
@@ -85,6 +100,7 @@ func (c *CLI) WorktreeList(ctx context.Context, repoPath string) (WorktreeListin
85
100
  RepoName: response.Source.RepoName,
86
101
  RepoRoot: response.Source.RepoRoot,
87
102
  SourceCheckoutPath: response.Source.SourceCheckoutPath,
103
+ SourceWorkspaceID: response.Source.SourceWorkspaceID,
88
104
  },
89
105
  Worktrees: convertWorktrees(response.Worktrees),
90
106
  }, nil
@@ -162,6 +162,14 @@ case "$#:${1-}:${2-}" in
162
162
  *) emit_result pane-close.json ;;
163
163
  esac
164
164
  ;;
165
+ 7:workspace:create)
166
+ [ "$3" = --cwd ] || fail "$@"
167
+ check_absolute_cwd "$4"
168
+ [ "$5" = --label ] || fail "$@"
169
+ check_value "$6"
170
+ [ "$7" = --no-focus ] || fail "$@"
171
+ emit_result workspace-create.json
172
+ ;;
165
173
  3:workspace:close)
166
174
  check_value "$3"
167
175
  case "$3" in
@@ -0,0 +1 @@
1
+ {"id":"cli:workspace:create","result":{"root_pane":{"agent_status":"unknown","cwd":"/tmp/relay-flow-herdr-fixture-9wIblp/repo","focused":true,"foreground_cwd":"/tmp/relay-flow-herdr-fixture-9wIblp/repo","pane_id":"w1:p1","revision":0,"scroll":{"max_offset_from_bottom":0,"offset_from_bottom":0,"viewport_rows":40},"tab_id":"w1:t1","terminal_id":"term_65b0ccebd6e821","workspace_id":"w1"},"tab":{"agent_status":"unknown","focused":true,"label":"1","number":1,"pane_count":1,"tab_id":"w1:t1","workspace_id":"w1"},"type":"workspace_created","workspace":{"active_tab_id":"w1:t1","agent_status":"unknown","focused":true,"label":"payments","number":1,"pane_count":1,"tab_count":1,"workspace_id":"w1"}}}