Scope agent session bookkeeping to the run for concurrent runs

The per-workspace concurrency added in 2618f0d exposed three places that assumed a
single run, and the second concurrent run failed with 'agent quiescence not
confirmed' because of them.

- ActiveAgentSessionsForRun(runID) scopes the end-of-step quiescence check to the
  run's own sessions, so another run's legitimately active agent no longer fails
  this one. A run still fails when its own session cannot be confirmed idle.
- The startup guard now uses ActiveSessionOwners (which carries the owning run id)
  and refuses only an ORPHANED session - one whose run is not active now - which is
  what restart recovery is actually about. A session owned by a currently active
  run no longer blocks new work.
- sessionActive examines only the target session: foreign entries in the
  /api/session/active map are ignored, the target's absence is inactive, and the
  target present with an unsupported state still fails closed.

Verified: go build, go vet and go test ./... pass, including ten concurrency tests
(two workspaces run concurrently, same-workspace refusal, symlink and default-cwd
equivalence, idempotent repeats, restart recovery, retry scoping, overlapping runs
not failing quiescence, and a run's own active session still failing it).

Operator verification, which a stage cannot perform because restarting the server
would abort the run doing it: the service was rebuilt and restarted on this
commit, then two runs in separate workspaces (/tmp/ws-c and /tmp/ws-d) executed
concurrently - both reported executing at the same time, a third run in ws-c was
refused naming run 46, and both finished succeeded with no diagnostic.
This commit is contained in:
2026-10-15 15:38:50 +01:00
parent 2618f0d244
commit e6d00d4ee5
7 changed files with 330 additions and 23 deletions
+174 -1
View File
@@ -16,6 +16,16 @@ import (
)
func concurrentStore(t *testing.T) (context.Context, *runner, int64) {
t.Helper()
return newConcurrentRunner(t, pipeline.Template([]string{"one"}, true))
}
// newConcurrentRunner builds the shared test store/runner around a submitted
// pipeline source. Tests that need an agent stage (useAgent true) must pass a
// source with a non-empty tools block: the default template's empty tools block
// explicitly denies tools, so useAgent is false and the step-completion
// quiescence check is never exercised.
func newConcurrentRunner(t *testing.T, source string) (context.Context, *runner, int64) {
t.Helper()
ctx, cancel := context.WithCancel(context.Background())
store, err := pipeline.Open(filepath.Join(t.TempDir(), "concurrent.db"))
@@ -23,7 +33,7 @@ func concurrentStore(t *testing.T) (context.Context, *runner, int64) {
cancel()
t.Fatal(err)
}
if _, err := store.Submit(ctx, "test", pipeline.Template([]string{"one"}, true)); err != nil {
if _, err := store.Submit(ctx, "test", source); err != nil {
cancel()
store.Close()
t.Fatal(err)
@@ -48,6 +58,14 @@ func concurrentStore(t *testing.T) (context.Context, *runner, int64) {
return ctx, r, items[0].ID
}
// agentStore returns a runner whose pipeline has an agent stage (a non-empty,
// explicitly declared tools block), so the step-completion quiescence check runs.
func agentStore(t *testing.T) (context.Context, *runner, int64) {
t.Helper()
source := strings.Replace(pipeline.Template([]string{"one"}, true), "```tools\n```", "```tools\nread\nshell\n```", 1)
return newConcurrentRunner(t, source)
}
// waitEntered fails fast instead of hanging when a stage never starts.
func waitEntered(t *testing.T, entered <-chan struct{}) {
t.Helper()
@@ -344,3 +362,158 @@ func TestRetryConflictsOnlyWithSameWorkspaceActiveRun(t *testing.T) {
close(release)
r.wg.Wait()
}
// waitTerminal polls one run until it leaves queued/running, failing fast instead
// of hanging. It is scoped to the run so a blocked sibling does not stall it.
func waitTerminal(t *testing.T, r *runner, id int64) *protocol.RunDetail {
t.Helper()
deadline := time.Now().Add(10 * time.Second)
for {
detail, err := r.getRun(id)
if err != nil {
t.Fatal(err)
}
switch detail.Status {
case "succeeded", "failed", "canceled":
return detail
}
if time.Now().After(deadline) {
t.Fatalf("run %d did not reach a terminal state: %+v", id, detail)
}
time.Sleep(5 * time.Millisecond)
}
}
// TestOverlappingRunsDoNotFailQuiescence proves the step-completion check is
// scoped to the run: with two agent runs in different workspaces each recording
// its OWN session active, releasing run A lets A reach succeeded while run B's
// session is still active, and vice versa. A server-wide check would fail A
// deterministically against B's active session.
func TestOverlappingRunsDoNotFailQuiescence(t *testing.T) {
_, r, pipelineID := agentStore(t)
a, b := t.TempDir(), t.TempDir()
type agentRun struct {
id int64
session string
}
var mu sync.Mutex
byWorkspace := map[string]agentRun{}
releases := map[string]chan struct{}{}
ready := make(chan struct{})
entered := make(chan string, 2)
resolve := func(dir string) string {
t.Helper()
resolved, err := filepath.EvalSymlinks(dir)
if err != nil {
t.Fatal(err)
}
return resolved
}
r.execute = func(ctx context.Context, _ stage.Definition, input stage.Values) (stage.Result, error) {
<-ready
workspace, _ := input["pwd"].(string)
mu.Lock()
agent, ok := byWorkspace[workspace]
release := releases[workspace]
mu.Unlock()
if !ok {
return stage.Result{}, fmt.Errorf("execute for unknown workspace %q", workspace)
}
if err := r.store.RunSession(context.Background(), agent.id, 1, agent.session, true, "v2", ""); err != nil {
return stage.Result{}, err
}
entered <- workspace
select {
case <-release:
case <-ctx.Done():
return stage.Result{}, ctx.Err()
}
if err := r.store.RunSession(context.Background(), agent.id, 1, agent.session, false, "v2", ""); err != nil {
return stage.Result{}, err
}
return stage.Result{Outcome: "success", Output: stage.Values{}}, nil
}
first, err := r.start(protocol.Message{PipelineID: pipelineID, Version: 1, RequestKey: "a", Workspace: a, Input: map[string]any{"prompt": "task"}})
if err != nil {
t.Fatal(err)
}
second, err := r.start(protocol.Message{PipelineID: pipelineID, Version: 1, RequestKey: "b", Workspace: b, Input: map[string]any{"prompt": "task"}})
if err != nil {
t.Fatalf("second workspace was blocked by the first: %v", err)
}
if first.Workspace != resolve(a) || second.Workspace != resolve(b) {
t.Fatalf("unexpected resolved workspaces: %q, %q", first.Workspace, second.Workspace)
}
mu.Lock()
byWorkspace[first.Workspace] = agentRun{id: first.ID, session: "ses_a"}
byWorkspace[second.Workspace] = agentRun{id: second.ID, session: "ses_b"}
releases[first.Workspace] = make(chan struct{})
releases[second.Workspace] = make(chan struct{})
mu.Unlock()
close(ready)
waitEnteredString(t, entered)
waitEnteredString(t, entered)
for _, id := range []int64{first.ID, second.ID} {
detail, err := r.getRun(id)
if err != nil || !detail.Executing {
t.Fatalf("run %d was not reported executing: %v", id, err)
}
}
// Release A only; B's own session stays active while A finishes.
close(releases[first.Workspace])
done := waitTerminal(t, r, first.ID)
if done.Status != "succeeded" || strings.Contains(done.Diagnostic, "agent quiescence not confirmed") {
t.Fatalf("run A failed despite its own session being idle: %+v", done)
}
if detail, err := r.getRun(second.ID); err != nil || !detail.Executing {
t.Fatalf("run B stopped while A finished: %+v %v", detail, err)
}
close(releases[second.Workspace])
done = waitTerminal(t, r, second.ID)
if done.Status != "succeeded" || strings.Contains(done.Diagnostic, "agent quiescence not confirmed") {
t.Fatalf("run B failed despite its own session being idle: %+v", done)
}
r.wg.Wait()
}
// waitEnteredString fails fast instead of hanging when a stage never starts; it
// carries the entering workspace for tests that key on it.
func waitEnteredString(t *testing.T, entered <-chan string) {
t.Helper()
select {
case <-entered:
case <-time.After(10 * time.Second):
t.Fatal("stage execution did not start")
}
}
// TestOwnActiveSessionStillFailsRun proves a run still fails when one of its OWN
// agent sessions is left active after the stage completes.
func TestOwnActiveSessionStillFailsRun(t *testing.T) {
_, r, pipelineID := agentStore(t)
workspace := t.TempDir()
ready := make(chan struct{})
var runID int64
r.execute = func(_ context.Context, _ stage.Definition, _ stage.Values) (stage.Result, error) {
<-ready
if err := r.store.RunSession(context.Background(), runID, 1, "ses_own", true, "v2", ""); err != nil {
return stage.Result{}, err
}
return stage.Result{Outcome: "success", Output: stage.Values{}}, nil
}
run, err := r.start(protocol.Message{PipelineID: pipelineID, Version: 1, RequestKey: "own", Workspace: workspace, Input: map[string]any{"prompt": "task"}})
if err != nil {
t.Fatal(err)
}
runID = run.ID
close(ready)
r.wg.Wait()
detail, err := r.store.GetRun(context.Background(), run.ID)
if err != nil {
t.Fatal(err)
}
if detail.Status != "failed" || !strings.Contains(detail.Diagnostic, "agent quiescence not confirmed") {
t.Fatalf("own active session did not fail the run: %+v", detail)
}
}
+9 -4
View File
@@ -160,12 +160,17 @@ func (r *runner) start(request protocol.Message, retryOf ...int64) (*protocol.Ru
if r.ctx.Err() != nil {
return nil, r.ctx.Err()
}
sessions, err := r.store.ActiveAgentSessions(r.ctx)
owners, err := r.store.ActiveSessionOwners(r.ctx)
if err != nil {
return nil, err
}
if len(sessions) > 0 && len(r.active) == 0 {
return nil, fmt.Errorf("an agent may still be active; restart recovery must interrupt it before new work")
// Refuse only an orphaned session: one whose owning run is not active now.
// A session owned by a currently active run (another run in another
// workspace) must not block new work.
for _, owner := range owners {
if _, ok := r.active[owner.RunID]; !ok {
return nil, fmt.Errorf("an agent may still be active; restart recovery must interrupt it before new work")
}
}
// Idempotent repeats are accepted even while the original run is active.
if len(r.active) > 0 {
@@ -489,7 +494,7 @@ func (r *runner) run(ctx context.Context, id int64, d *protocol.PipelineDetail,
}
input = stage.Values(step.Output)
if useAgent || errors.Is(err, stage.ErrStageCleanup) {
sessions, checkErr := r.store.ActiveAgentSessions(context.Background())
sessions, checkErr := r.store.ActiveAgentSessionsForRun(context.Background(), id)
if checkErr != nil || len(sessions) > 0 {
finish("failed", "error", "agent quiescence not confirmed; inspect retained session before further work")
return