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
+13 -9
View File
@@ -201,9 +201,12 @@ func (c OpenCodeClient) waitForSessionIdle(ctx context.Context, sessionID string
}
}
// sessionActive reports whether sessionID is currently active. Missing, null or
// non-object data, and any state other than the documented "running" type, are
// errors so a bad response can never be mistaken for a settled session.
// sessionActive reports whether sessionID is currently active. Only the target
// session is examined; entries for other sessions are ignored entirely, so a
// foreign session's state never affects this wait. The target's absence is
// inactive. Missing, null or non-object data, and the target present with any
// state other than the documented "running" type, are errors so a bad response
// can never be mistaken for a settled session.
func (c OpenCodeClient) sessionActive(ctx context.Context, sessionID string) (bool, error) {
data, err := c.call(ctx, "get", "/api/session/active", nil)
if err != nil {
@@ -215,13 +218,14 @@ func (c OpenCodeClient) sessionActive(ctx context.Context, sessionID string) (bo
if err := json.Unmarshal(data, &response); err != nil || response.Data == nil {
return false, fmt.Errorf("%w: OpenCode returned an invalid active-session response", ErrHarnessTransport)
}
for _, state := range response.Data {
if state.Type != "running" {
return false, fmt.Errorf("%w: OpenCode reported an unsupported active-session state", ErrHarnessTransport)
}
state, ok := response.Data[sessionID]
if !ok {
return false, nil
}
_, active := response.Data[sessionID]
return active, nil
if state.Type != "running" {
return false, fmt.Errorf("%w: OpenCode reported an unsupported active-session state", ErrHarnessTransport)
}
return true, nil
}
func (c *OpenCodeClient) Generate(ctx context.Context, config ModelConfig, prompt string) (text string, err error) {
+54 -1
View File
@@ -40,7 +40,6 @@ func TestWaitForSessionIdleFailsClosedOnInvalidActiveResponse(t *testing.T) {
``, `not json`, `{}`, `{"data":null}`, `{"data":[]}`, `{"data":"running"}`,
`{"data":{"ses_wait":"running"}}`, `{"data":{"ses_wait":{}}}`,
`{"data":{"ses_wait":{"type":""}}}`, `{"data":{"ses_wait":{"type":"idle"}}}`,
`{"data":{"ses_wait":{"type":"running"},"ses_other":{"type":"unknown"}}}`,
} {
client := OpenCodeClient{sessionPollInterval: time.Millisecond, command: func(_ context.Context, _ string, _ ...string) ([]byte, error) {
return []byte(payload), nil
@@ -58,6 +57,60 @@ func TestWaitForSessionIdleFailsClosedOnInvalidActiveResponse(t *testing.T) {
}
}
// TestWaitForSessionIdleScopesToTargetSession proves the poll examines only the
// admitted session: a foreign session in an unsupported state is ignored (the
// target's absence is inactive), the target running keeps polling while that
// foreign entry exists, while the target's own unsupported state still fails
// closed and malformed/missing/non-object responses still fail closed.
func TestWaitForSessionIdleScopesToTargetSession(t *testing.T) {
// (a) Foreign session in an unsupported state, target absent: inactive, one
// poll, no error.
polls := 0
foreign := OpenCodeClient{sessionPollInterval: time.Millisecond, command: func(_ context.Context, _ string, _ ...string) ([]byte, error) {
polls++
return []byte(`{"data":{"ses_other":{"type":"unknown"}}}`), nil
}}
if err := foreign.waitForSessionIdle(context.Background(), "ses_wait"); err != nil {
t.Fatalf("foreign unsupported state failed the target wait: %v", err)
}
if polls != 1 {
t.Fatalf("foreign-only response should settle on one poll, got %d", polls)
}
// (b) Target running alongside a foreign unsupported entry keeps polling
// until the target leaves; assert a bounded poll count.
polls = 0
transition := OpenCodeClient{sessionPollInterval: time.Millisecond, command: func(_ context.Context, _ string, _ ...string) ([]byte, error) {
polls++
if polls < 3 {
return []byte(`{"data":{"ses_wait":{"type":"running"},"ses_other":{"type":"unknown"}}}`), nil
}
return []byte(`{"data":{"ses_other":{"type":"unknown"}}}`), nil
}}
if err := transition.waitForSessionIdle(context.Background(), "ses_wait"); err != nil {
t.Fatalf("wait did not settle after the target left: %v", err)
}
if polls != 3 {
t.Fatalf("expected 3 bounded polls, got %d", polls)
}
// (c) Target present with an unsupported type still fails closed even while
// a foreign entry exists.
unsupported := OpenCodeClient{sessionPollInterval: time.Millisecond, command: func(_ context.Context, _ string, _ ...string) ([]byte, error) {
return []byte(`{"data":{"ses_wait":{"type":"idle"},"ses_other":{"type":"unknown"}}}`), nil
}}
if err := unsupported.waitForSessionIdle(context.Background(), "ses_wait"); !errors.Is(err, ErrHarnessTransport) {
t.Fatalf("target unsupported state did not fail closed: %v", err)
}
// (d) Empty/malformed/missing/non-object data still fail closed.
for _, payload := range []string{``, `not json`, `{}`, `{"data":null}`, `{"data":[]}`, `{"data":"running"}`} {
malformed := OpenCodeClient{sessionPollInterval: time.Millisecond, command: func(_ context.Context, _ string, _ ...string) ([]byte, error) {
return []byte(payload), nil
}}
if err := malformed.waitForSessionIdle(context.Background(), "ses_wait"); !errors.Is(err, ErrHarnessTransport) {
t.Fatalf("malformed active response accepted: %q: %v", payload, err)
}
}
}
// TestWaitForSessionIdlePropagatesRequestFailure ensures a transport/CLI error
// aborts promptly and is surfaced rather than swallowed.
func TestWaitForSessionIdlePropagatesRequestFailure(t *testing.T) {