Allow concurrent runs in different workspaces

Track each active run's resolved workspace alongside its cancel function
in a single runner.active struct map. A new request key is now refused
only when an already-active run is changing the same resolved workspace;
the refusal names the conflicting run id and advises waiting or using a
different workspace. Workspace resolution is unchanged (default cwd,
absolute request workspace, EvalSymlinks), so two spellings of one
directory still compare equal. The conflict check and registration stay
inside the existing r.mu critical section, so concurrent starts for one
workspace cannot both pass. Idempotent repeats of an active request key
and the restart-recovery guard are unchanged.

Add concurrency_test.go covering concurrent different-workspace runs,
same-workspace refusal, symlink/default-cwd equivalence, idempotent
repeats, restart recovery and retry workspace scoping; strengthen the
runner_test.go refusal assertion. Update docs/cli.md,
docs/first-version-testing.md and the packaged operations skill reference
to describe per-workspace concurrency.

Assisted-by: DeepSeek V4.1 Flash
This commit is contained in:
2026-10-15 15:08:44 +01:00
parent 1828171a9c
commit 2618f0d244
7 changed files with 390 additions and 20 deletions
+22 -6
View File
@@ -19,11 +19,19 @@ import (
"git.a-wan.net/alan/pipelines/internal/stage"
)
// activeRun records one in-flight run's cancellation seam and the resolved
// workspace it is changing. A single map is the only active-run state, so the
// workspace cannot drift from the cancel function.
type activeRun struct {
cancel context.CancelFunc
workspace string
}
type runner struct {
ctx context.Context
store *pipeline.Store
mu sync.Mutex
active map[int64]context.CancelFunc
active map[int64]activeRun
execute func(context.Context, stage.Definition, stage.Values) (stage.Result, error)
wg sync.WaitGroup
schedulerWG sync.WaitGroup
@@ -50,7 +58,7 @@ func newRunner(ctx context.Context, s *pipeline.Store) (*runner, error) {
if err := s.RecoverRuns(ctx); err != nil {
return nil, err
}
return &runner{ctx: ctx, store: s, active: map[int64]context.CancelFunc{}}, nil
return &runner{ctx: ctx, store: s, active: map[int64]activeRun{}}, nil
}
func (r *runner) start(request protocol.Message, retryOf ...int64) (*protocol.RunDetail, error) {
@@ -166,7 +174,15 @@ func (r *runner) start(request protocol.Message, retryOf ...int64) (*protocol.Ru
return nil, err
}
if existing == 0 {
return nil, fmt.Errorf("another run is active; inspect it before starting more work")
// The workspace is the only scope a run reserves: refuse a second
// run only when an active run is already changing this exact
// resolved directory. One active run per workspace keeps this scan
// finding at most one match, so the message is deterministic.
for id, entry := range r.active {
if entry.workspace == workspace {
return nil, fmt.Errorf("run %d is already active in workspace %s; wait for it to finish or start this run in a different workspace (for example another checkout of the repository)", id, workspace)
}
}
}
}
id, created, err := r.store.CreateRun(r.ctx, d, request.RequestKey, workspace, request.AllowTools, request.WritePaths, request.Input, retryOf...)
@@ -175,7 +191,7 @@ func (r *runner) start(request protocol.Message, retryOf ...int64) (*protocol.Ru
}
if created {
ctx, cancel := context.WithCancel(r.ctx)
r.active[id] = cancel
r.active[id] = activeRun{cancel: cancel, workspace: workspace}
r.wg.Add(1)
go func() {
defer r.wg.Done()
@@ -247,11 +263,11 @@ func (r *runner) retry(request protocol.Message) (*protocol.RunDetail, error) {
func (r *runner) cancel(id int64) error {
r.mu.Lock()
defer r.mu.Unlock()
cancel, ok := r.active[id]
entry, ok := r.active[id]
if !ok {
return fmt.Errorf("run is not active")
}
cancel()
entry.cancel()
return nil
}