1818a06d2f
chat_sessions now brokers GET /api/session but filters the global list to the sessions this server recorded in run_steps, deduped by session id, and fails closed to an empty list when no ownership source is configured. A session another client holds (in-server claim, released on stream end or disconnect) or an active run is marked busy; a second client's chat_prompt for a held session is rejected before any OpenCode call. chat_create stays unbrokered.
215 lines
6.6 KiB
Go
215 lines
6.6 KiB
Go
package pipeline
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"path/filepath"
|
|
"reflect"
|
|
"testing"
|
|
)
|
|
|
|
func TestRunListIncludesHistoryBeyondFiftyForSearchAndPipelineScope(t *testing.T) {
|
|
ctx := context.Background()
|
|
store, err := Open(filepath.Join(t.TempDir(), "runs.db"))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer store.Close()
|
|
if _, err := store.Submit(ctx, "history", Template([]string{"one"}, true)); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
items, err := store.List(ctx)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
definition, err := store.Get(ctx, items[0].ID, 1)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
for i := 0; i < 55; i++ {
|
|
if _, _, err := store.CreateRun(ctx, definition, fmt.Sprintf("history-%d", i), "workspace", false, nil, nil); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
runs, err := store.ListRuns(ctx)
|
|
if err != nil || len(runs) != 55 || runs[len(runs)-1].ID != 1 {
|
|
t.Fatal("older searchable runs missing", len(runs), err)
|
|
}
|
|
}
|
|
|
|
func TestRunRecoveryAndVersionPinning(t *testing.T) {
|
|
ctx := context.Background()
|
|
path := filepath.Join(t.TempDir(), "audit.db")
|
|
store, err := Open(path)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
source := Template([]string{"one"}, true)
|
|
if _, err := store.Submit(ctx, "audit", source); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
items, _ := store.List(ctx)
|
|
definition, _ := store.Get(ctx, items[0].ID, 1)
|
|
id, _, err := store.CreateRun(ctx, definition, "recover", "workspace", false, nil, map[string]any{"n": 123})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := store.BeginStep(ctx, id, 1, "one", map[string]any{"n": 123}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := store.RunSession(ctx, id, 1, "ses_recover", true); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := store.Close(); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
store, err = Open(path)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer store.Close()
|
|
owners, err := store.ActiveSessionOwners(ctx)
|
|
if err != nil || len(owners) != 1 {
|
|
t.Fatal("lost active session ownership")
|
|
}
|
|
if err := store.RecoverRuns(ctx); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if _, err := store.Submit(ctx, "audit", Template([]string{"replacement", "another"}, true)); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
run, err := store.GetRun(ctx, id)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if run.Status != "interrupted" || run.Steps[0].Status != "interrupted" || run.Steps[0].AgentActive || run.ContentHash != definition.ContentHash || run.Version != 1 {
|
|
t.Fatal("recovery silently resumed or changed version", run)
|
|
}
|
|
if !reflect.DeepEqual(run.Graph, definition.Graph) {
|
|
t.Fatal("run graph did not retain its pinned definition", run.Graph)
|
|
}
|
|
loaded, _ := store.Get(ctx, definition.ID, 1)
|
|
if loaded.Source != source {
|
|
t.Fatal("run modified immutable source")
|
|
}
|
|
}
|
|
|
|
// TestActiveAgentSessionsAreScopedToRun proves the run-scoped query returns only
|
|
// the row's own active session: a second run's active session is never returned,
|
|
// and clearing the session clears the result.
|
|
func TestActiveAgentSessionsAreScopedToRun(t *testing.T) {
|
|
ctx := context.Background()
|
|
store, err := Open(filepath.Join(t.TempDir(), "scoped.db"))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer store.Close()
|
|
if _, err := store.Submit(ctx, "scoped", Template([]string{"one"}, true)); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
items, err := store.List(ctx)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
definition, err := store.Get(ctx, items[0].ID, 1)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
runA, _, err := store.CreateRun(ctx, definition, "a", "workspace-a", false, nil, nil)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
runB, _, err := store.CreateRun(ctx, definition, "b", "workspace-b", false, nil, nil)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
for _, run := range []struct {
|
|
id int64
|
|
session string
|
|
}{{runA, "ses_a"}, {runB, "ses_b"}} {
|
|
if err := store.BeginStep(ctx, run.id, 1, "one", map[string]any{"prompt": "task"}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := store.RunSession(ctx, run.id, 1, run.session, true); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
sessions, err := store.ActiveAgentSessionsForRun(ctx, runA)
|
|
if err != nil || !reflect.DeepEqual(sessions, []string{"ses_a"}) {
|
|
t.Fatalf("run-scoped query leaked another run's session: %v %v", sessions, err)
|
|
}
|
|
if err := store.RunSession(ctx, runA, 1, "ses_a", false); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
sessions, err = store.ActiveAgentSessionsForRun(ctx, runA)
|
|
if err != nil || len(sessions) != 0 {
|
|
t.Fatalf("inactive session still reported active: %v %v", sessions, err)
|
|
}
|
|
if sessions, err = store.ActiveAgentSessionsForRun(ctx, runB); err != nil || !reflect.DeepEqual(sessions, []string{"ses_b"}) {
|
|
t.Fatalf("second run's own session was affected: %v %v", sessions, err)
|
|
}
|
|
}
|
|
|
|
// TestOwnedSessionsListsRecordedWithActiveFlag proves OwnedSessions returns every
|
|
// session the server ever recorded (active or not) in run_steps, deduped by
|
|
// session id in favour of the newest owning run, with that run's workspace
|
|
// directory and the active flag.
|
|
func TestOwnedSessionsListsRecordedWithActiveFlag(t *testing.T) {
|
|
ctx := context.Background()
|
|
store, err := Open(filepath.Join(t.TempDir(), "owned.db"))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer store.Close()
|
|
if _, err := store.Submit(ctx, "owned", Template([]string{"one"}, true)); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
items, err := store.List(ctx)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
definition, err := store.Get(ctx, items[0].ID, 1)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
record := func(key, workspace, session string, active bool) {
|
|
t.Helper()
|
|
run, _, err := store.CreateRun(ctx, definition, key, workspace, false, nil, nil)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := store.BeginStep(ctx, run, 1, "one", map[string]any{"prompt": "task"}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := store.RunSession(ctx, run, 1, session, active, "v2", "/repo"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
record("owned-a", "/work/a", "ses_active", true)
|
|
record("owned-b", "/work/b", "ses_idle", false)
|
|
// The same session recorded by a newer run must dedupe to one entry, taking
|
|
// the newest run's workspace and active flag.
|
|
record("owned-c", "/work/c", "ses_idle", true)
|
|
|
|
owners, err := store.OwnedSessions(ctx)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if len(owners) != 2 {
|
|
t.Fatalf("owned sessions = %+v, want 2 deduped", owners)
|
|
}
|
|
byID := map[string]SessionOwner{}
|
|
for _, owner := range owners {
|
|
byID[owner.ID] = owner
|
|
}
|
|
if owner, ok := byID["ses_active"]; !ok || !owner.Active || owner.Directory != "/work/a" {
|
|
t.Fatalf("active owner = %+v", owner)
|
|
}
|
|
if owner, ok := byID["ses_idle"]; !ok || !owner.Active || owner.Directory != "/work/c" {
|
|
t.Fatalf("deduped owner should come from the newest run: %+v", owner)
|
|
}
|
|
if active, err := store.ActiveSessionOwners(ctx); err != nil || len(active) != 2 {
|
|
t.Fatalf("ActiveSessionOwners = %+v %v, want both now-active sessions", active, err)
|
|
}
|
|
}
|