60aef37cbd
Co-authored-by: ayattara <alassane.yattara@savoirfairelinux.com> Co-authored-by: Carlos Florencio <carlosmflorencio@gmail.com>
918 lines
28 KiB
Go
918 lines
28 KiB
Go
package service
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"sync"
|
|
"testing"
|
|
|
|
"github.com/kandev/kandev/internal/events"
|
|
"github.com/kandev/kandev/internal/events/bus"
|
|
"github.com/kandev/kandev/internal/task/models"
|
|
"github.com/kandev/kandev/pkg/api/v1"
|
|
"github.com/kandev/kandev/pkg/pluginsdk"
|
|
"google.golang.org/grpc/codes"
|
|
"google.golang.org/grpc/status"
|
|
)
|
|
|
|
// ── fakes ────────────────────────────────────────────────────────────────
|
|
|
|
type acFakeTaskRepo struct {
|
|
mu sync.Mutex
|
|
tasks []*models.Task
|
|
nextIdx int
|
|
workspace *models.Workspace
|
|
}
|
|
|
|
func (f *acFakeTaskRepo) GetWorkspace(_ context.Context, _ string) (*models.Workspace, error) {
|
|
f.mu.Lock()
|
|
defer f.mu.Unlock()
|
|
if f.workspace == nil {
|
|
profileID := "profile-workspace-default"
|
|
return &models.Workspace{ID: "workspace-1", DefaultAgentProfileID: &profileID}, nil
|
|
}
|
|
return f.workspace, nil
|
|
}
|
|
|
|
func (f *acFakeTaskRepo) ListTasksByWorkspace(ctx context.Context, workspaceID, _, _, _ string, page, pageSize int, _ string, _, includeEphemeral, onlyEphemeral, _ bool) ([]*models.Task, int, error) {
|
|
f.mu.Lock()
|
|
defer f.mu.Unlock()
|
|
var filtered []*models.Task
|
|
for _, t := range f.tasks {
|
|
if t.WorkspaceID != workspaceID {
|
|
continue
|
|
}
|
|
if onlyEphemeral && !t.IsEphemeral {
|
|
continue
|
|
}
|
|
if !includeEphemeral && t.IsEphemeral {
|
|
continue
|
|
}
|
|
filtered = append(filtered, t)
|
|
}
|
|
return filtered, len(filtered), nil
|
|
}
|
|
|
|
func (f *acFakeTaskRepo) ListEphemeralTasksAllWorkspaces(_ context.Context) ([]*models.Task, error) {
|
|
f.mu.Lock()
|
|
defer f.mu.Unlock()
|
|
var filtered []*models.Task
|
|
for _, t := range f.tasks {
|
|
if t.IsEphemeral {
|
|
filtered = append(filtered, t)
|
|
}
|
|
}
|
|
return filtered, nil
|
|
}
|
|
|
|
func (f *acFakeTaskRepo) CreateTask(_ context.Context, task *models.Task) error {
|
|
f.mu.Lock()
|
|
defer f.mu.Unlock()
|
|
if task.ID == "" {
|
|
task.ID = f.makeID()
|
|
}
|
|
for _, existing := range f.tasks {
|
|
if existing.ID == task.ID {
|
|
return errors.New("task already exists")
|
|
}
|
|
}
|
|
f.tasks = append(f.tasks, task)
|
|
return nil
|
|
}
|
|
|
|
func (f *acFakeTaskRepo) UpdateTask(_ context.Context, task *models.Task) error {
|
|
f.mu.Lock()
|
|
defer f.mu.Unlock()
|
|
for i, existing := range f.tasks {
|
|
if existing.ID == task.ID {
|
|
f.tasks[i] = task
|
|
return nil
|
|
}
|
|
}
|
|
return errors.New("task not found")
|
|
}
|
|
|
|
func (f *acFakeTaskRepo) DeleteTask(_ context.Context, id string) error {
|
|
f.mu.Lock()
|
|
defer f.mu.Unlock()
|
|
for i, t := range f.tasks {
|
|
if t.ID == id {
|
|
f.tasks = append(f.tasks[:i], f.tasks[i+1:]...)
|
|
return nil
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (f *acFakeTaskRepo) count() int {
|
|
f.mu.Lock()
|
|
defer f.mu.Unlock()
|
|
return len(f.tasks)
|
|
}
|
|
|
|
func (f *acFakeTaskRepo) makeID() string {
|
|
f.nextIdx++
|
|
return "task-" + acItoa(f.nextIdx)
|
|
}
|
|
|
|
func acItoa(n int) string {
|
|
if n == 0 {
|
|
return "0"
|
|
}
|
|
var buf [16]byte
|
|
i := len(buf)
|
|
for n > 0 {
|
|
i--
|
|
buf[i] = byte('0' + n%10)
|
|
n /= 10
|
|
}
|
|
return string(buf[i:])
|
|
}
|
|
|
|
type acFakeSessionRepo struct {
|
|
mu sync.Mutex
|
|
sessions map[string]*models.TaskSession
|
|
nextIdx int
|
|
}
|
|
|
|
func newACFakeSessionRepo() *acFakeSessionRepo {
|
|
return &acFakeSessionRepo{sessions: make(map[string]*models.TaskSession)}
|
|
}
|
|
|
|
func (f *acFakeSessionRepo) GetPrimarySessionByTaskID(_ context.Context, taskID string) (*models.TaskSession, error) {
|
|
f.mu.Lock()
|
|
defer f.mu.Unlock()
|
|
for _, s := range f.sessions {
|
|
if s.TaskID == taskID && s.IsPrimary {
|
|
return s, nil
|
|
}
|
|
}
|
|
return nil, nil
|
|
}
|
|
|
|
func (f *acFakeSessionRepo) CreateTaskSession(_ context.Context, session *models.TaskSession) error {
|
|
f.mu.Lock()
|
|
defer f.mu.Unlock()
|
|
if session.ID == "" {
|
|
f.nextIdx++
|
|
session.ID = "session-" + acItoa(f.nextIdx)
|
|
}
|
|
if _, exists := f.sessions[session.ID]; exists {
|
|
return errors.New("session already exists")
|
|
}
|
|
f.sessions[session.ID] = session
|
|
return nil
|
|
}
|
|
|
|
func (f *acFakeSessionRepo) UpdateTaskSession(_ context.Context, session *models.TaskSession) error {
|
|
f.mu.Lock()
|
|
defer f.mu.Unlock()
|
|
if _, ok := f.sessions[session.ID]; !ok {
|
|
return errors.New("session not found")
|
|
}
|
|
f.sessions[session.ID] = session
|
|
return nil
|
|
}
|
|
|
|
func (f *acFakeSessionRepo) count() int {
|
|
f.mu.Lock()
|
|
defer f.mu.Unlock()
|
|
return len(f.sessions)
|
|
}
|
|
|
|
func (f *acFakeSessionRepo) setState(id string, state models.TaskSessionState) {
|
|
f.mu.Lock()
|
|
defer f.mu.Unlock()
|
|
if s, ok := f.sessions[id]; ok {
|
|
s.State = state
|
|
}
|
|
}
|
|
|
|
// acFakeProfileRepo defaults every profile ID to "found and enabled" so
|
|
// tests that don't care about profile validation (most Ensure tests
|
|
// predating it) need no setup. Tests exercising the configuration_required
|
|
// gate mark specific IDs missing/disabled explicitly.
|
|
type acFakeProfileRepo struct {
|
|
mu sync.Mutex
|
|
missing map[string]bool
|
|
disabled map[string]bool
|
|
calls []string
|
|
}
|
|
|
|
func newACFakeProfileRepo() *acFakeProfileRepo {
|
|
return &acFakeProfileRepo{missing: map[string]bool{}, disabled: map[string]bool{}}
|
|
}
|
|
|
|
func (f *acFakeProfileRepo) GetProfile(_ context.Context, profileID string) (AgentConversationProfileInfo, bool, error) {
|
|
f.mu.Lock()
|
|
defer f.mu.Unlock()
|
|
f.calls = append(f.calls, profileID)
|
|
if f.missing[profileID] {
|
|
return AgentConversationProfileInfo{}, false, nil
|
|
}
|
|
if f.disabled[profileID] {
|
|
return AgentConversationProfileInfo{Enabled: false}, true, nil
|
|
}
|
|
return AgentConversationProfileInfo{Enabled: true}, true, nil
|
|
}
|
|
|
|
func (f *acFakeProfileRepo) markMissing(id string) {
|
|
f.mu.Lock()
|
|
defer f.mu.Unlock()
|
|
f.missing[id] = true
|
|
}
|
|
|
|
func (f *acFakeProfileRepo) markDisabled(id string) {
|
|
f.mu.Lock()
|
|
defer f.mu.Unlock()
|
|
f.disabled[id] = true
|
|
}
|
|
|
|
// acDeliverCall records one call reaching the fake dispatcher — i.e. one
|
|
// call that would have reached the real agent runtime in production.
|
|
type acDeliverCall struct {
|
|
taskID string
|
|
sessionID string
|
|
text string
|
|
source string
|
|
idempotencyID string
|
|
}
|
|
|
|
// acFakeDispatcher stands in for the orchestrator-backed dispatcher.
|
|
// Recording calls here (rather than just asserting on a created message row,
|
|
// as the pre-fix tests did) is what proves Dispatch reaches the runtime
|
|
// delivery seam and not just message persistence.
|
|
type acFakeDispatcher struct {
|
|
mu sync.Mutex
|
|
calls []acDeliverCall
|
|
err error
|
|
}
|
|
|
|
func newACFakeDispatcher() *acFakeDispatcher {
|
|
return &acFakeDispatcher{}
|
|
}
|
|
|
|
func (f *acFakeDispatcher) Deliver(_ context.Context, taskID string, session *models.TaskSession, text, source, idempotencyID string) (string, error) {
|
|
f.mu.Lock()
|
|
defer f.mu.Unlock()
|
|
if f.err != nil {
|
|
return "", f.err
|
|
}
|
|
f.calls = append(f.calls, acDeliverCall{taskID: taskID, sessionID: session.ID, text: text, source: source, idempotencyID: idempotencyID})
|
|
if session.State == models.TaskSessionStateCreated {
|
|
return "started", nil
|
|
}
|
|
return "sent", nil
|
|
}
|
|
|
|
func (f *acFakeDispatcher) callCount() int {
|
|
f.mu.Lock()
|
|
defer f.mu.Unlock()
|
|
return len(f.calls)
|
|
}
|
|
|
|
func (f *acFakeDispatcher) lastCall() acDeliverCall {
|
|
f.mu.Lock()
|
|
defer f.mu.Unlock()
|
|
if len(f.calls) == 0 {
|
|
return acDeliverCall{}
|
|
}
|
|
return f.calls[len(f.calls)-1]
|
|
}
|
|
|
|
type acFakeEventBus struct {
|
|
mu sync.Mutex
|
|
events []*bus.Event
|
|
}
|
|
|
|
func (f *acFakeEventBus) Publish(_ context.Context, _ string, event *bus.Event) error {
|
|
f.mu.Lock()
|
|
defer f.mu.Unlock()
|
|
f.events = append(f.events, event)
|
|
return nil
|
|
}
|
|
|
|
func (f *acFakeEventBus) getPublished() []*bus.Event {
|
|
f.mu.Lock()
|
|
defer f.mu.Unlock()
|
|
out := make([]*bus.Event, len(f.events))
|
|
copy(out, f.events)
|
|
return out
|
|
}
|
|
|
|
type acTestDeps struct {
|
|
tasks *acFakeTaskRepo
|
|
sess *acFakeSessionRepo
|
|
profiles *acFakeProfileRepo
|
|
state *acFakeStateRepo
|
|
dispatcher *acFakeDispatcher
|
|
eventer *acFakeEventBus
|
|
}
|
|
|
|
func newACTestService() (*AgentConversationService, acTestDeps) {
|
|
deps := acTestDeps{
|
|
tasks: &acFakeTaskRepo{},
|
|
sess: newACFakeSessionRepo(),
|
|
profiles: newACFakeProfileRepo(),
|
|
state: newACFakeStateRepo(),
|
|
dispatcher: newACFakeDispatcher(),
|
|
eventer: &acFakeEventBus{},
|
|
}
|
|
svc := NewAgentConversationService(deps.tasks, deps.sess, deps.profiles, deps.state, deps.eventer)
|
|
svc.SetDispatcher(deps.dispatcher)
|
|
return svc, deps
|
|
}
|
|
|
|
// ── Ensure tests ────────────────────────────────────────────────────────
|
|
|
|
func TestEnsureCreatesConversation(t *testing.T) {
|
|
svc, deps := newACTestService()
|
|
|
|
spec := pluginsdk.AgentConversationSpec{
|
|
WorkspaceID: "ws-1",
|
|
ConversationKey: "coordinator",
|
|
BasePrompt: "You are a coordinator.",
|
|
}
|
|
|
|
desc, statusStr, err := svc.Ensure(context.Background(), "plugin-coordinator", spec)
|
|
if err != nil {
|
|
t.Fatalf("Ensure: %v", err)
|
|
}
|
|
if desc.TaskID == "" {
|
|
t.Fatal("expected non-empty TaskID")
|
|
}
|
|
if desc.SessionID == "" {
|
|
t.Fatal("expected non-empty SessionID")
|
|
}
|
|
if desc.WorkspaceID != "ws-1" {
|
|
t.Fatalf("WorkspaceID = %q, want ws-1", desc.WorkspaceID)
|
|
}
|
|
if desc.ConversationKey != "coordinator" {
|
|
t.Fatalf("ConversationKey = %q, want coordinator", desc.ConversationKey)
|
|
}
|
|
if statusStr != AgentConversationStatusCreated {
|
|
t.Fatalf("status = %q, want created", statusStr)
|
|
}
|
|
|
|
published := deps.eventer.getPublished()
|
|
if len(published) != 1 {
|
|
t.Fatalf("expected 1 event, got %d", len(published))
|
|
}
|
|
if published[0].Type != events.TaskCreated {
|
|
t.Fatalf("event type = %q, want %q", published[0].Type, events.TaskCreated)
|
|
}
|
|
}
|
|
|
|
func TestEnsureUsesWorkspaceDefaultProfileWhenPluginOmitsProfile(t *testing.T) {
|
|
svc, deps := newACTestService()
|
|
profileID := "workspace-profile"
|
|
deps.tasks.workspace = &models.Workspace{ID: "ws-1", DefaultAgentProfileID: &profileID}
|
|
|
|
desc, gotStatus, err := svc.Ensure(context.Background(), "plugin-coordinator", pluginsdk.AgentConversationSpec{
|
|
WorkspaceID: "ws-1", ConversationKey: "coordinator",
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("Ensure: %v", err)
|
|
}
|
|
if gotStatus != AgentConversationStatusCreated {
|
|
t.Fatalf("status = %q, want %q", gotStatus, AgentConversationStatusCreated)
|
|
}
|
|
if desc.AgentProfileID != profileID {
|
|
t.Fatalf("descriptor profile = %q, want %q", desc.AgentProfileID, profileID)
|
|
}
|
|
if deps.tasks.count() != 1 || deps.sess.count() != 1 {
|
|
t.Fatalf("created tasks/sessions = %d/%d, want 1/1", deps.tasks.count(), deps.sess.count())
|
|
}
|
|
if got := deps.tasks.tasks[0].Metadata[models.MetaKeyAgentProfileID]; got != profileID {
|
|
t.Fatalf("task profile metadata = %v, want %q", got, profileID)
|
|
}
|
|
for _, session := range deps.sess.sessions {
|
|
if session.AgentProfileID != profileID {
|
|
t.Fatalf("session profile = %q, want %q", session.AgentProfileID, profileID)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestEnsureRejectsMissingWorkspaceDefaultProfileWithoutPartialRows(t *testing.T) {
|
|
svc, deps := newACTestService()
|
|
missing := "missing-profile"
|
|
deps.tasks.workspace = &models.Workspace{ID: "ws-1", DefaultAgentProfileID: &missing}
|
|
deps.profiles.markMissing(missing)
|
|
|
|
_, gotStatus, err := svc.Ensure(context.Background(), "plugin-coordinator", pluginsdk.AgentConversationSpec{
|
|
WorkspaceID: "ws-1", ConversationKey: "coordinator",
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("Ensure: %v", err)
|
|
}
|
|
if gotStatus != AgentConversationStatusConfigurationRequired {
|
|
t.Fatalf("status = %q, want %q", gotStatus, AgentConversationStatusConfigurationRequired)
|
|
}
|
|
if deps.tasks.count() != 0 || deps.sess.count() != 0 {
|
|
t.Fatalf("partial rows = tasks:%d sessions:%d, want 0/0", deps.tasks.count(), deps.sess.count())
|
|
}
|
|
}
|
|
|
|
func TestEnsureRejectsDisabledWorkspaceDefaultProfileWithoutPartialRows(t *testing.T) {
|
|
svc, deps := newACTestService()
|
|
disabled := "disabled-profile"
|
|
deps.tasks.workspace = &models.Workspace{ID: "ws-1", DefaultAgentProfileID: &disabled}
|
|
deps.profiles.markDisabled(disabled)
|
|
|
|
_, gotStatus, err := svc.Ensure(context.Background(), "plugin-coordinator", pluginsdk.AgentConversationSpec{
|
|
WorkspaceID: "ws-1", ConversationKey: "coordinator",
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("Ensure: %v", err)
|
|
}
|
|
if gotStatus != AgentConversationStatusConfigurationRequired {
|
|
t.Fatalf("status = %q, want %q", gotStatus, AgentConversationStatusConfigurationRequired)
|
|
}
|
|
if deps.tasks.count() != 0 || deps.sess.count() != 0 {
|
|
t.Fatalf("partial rows = tasks:%d sessions:%d, want 0/0", deps.tasks.count(), deps.sess.count())
|
|
}
|
|
}
|
|
|
|
func TestEnsureReturnsExistsOnSecondCall(t *testing.T) {
|
|
svc, _ := newACTestService()
|
|
|
|
spec := pluginsdk.AgentConversationSpec{
|
|
WorkspaceID: "ws-1",
|
|
ConversationKey: "coordinator",
|
|
}
|
|
|
|
desc1, _, err := svc.Ensure(context.Background(), "plugin-coordinator", spec)
|
|
if err != nil {
|
|
t.Fatalf("first Ensure: %v", err)
|
|
}
|
|
|
|
desc2, statusStr, err := svc.Ensure(context.Background(), "plugin-coordinator", spec)
|
|
if err != nil {
|
|
t.Fatalf("second Ensure: %v", err)
|
|
}
|
|
if statusStr != AgentConversationStatusExists {
|
|
t.Fatalf("status = %q, want exists", statusStr)
|
|
}
|
|
if desc1.TaskID != desc2.TaskID {
|
|
t.Fatalf("TaskID changed: %q → %q", desc1.TaskID, desc2.TaskID)
|
|
}
|
|
if desc1.SessionID != desc2.SessionID {
|
|
t.Fatalf("SessionID changed: %q → %q", desc1.SessionID, desc2.SessionID)
|
|
}
|
|
}
|
|
|
|
func TestEnsureRejectsEmptyArguments(t *testing.T) {
|
|
svc, _ := newACTestService()
|
|
|
|
tests := []struct {
|
|
name string
|
|
plugin string
|
|
spec pluginsdk.AgentConversationSpec
|
|
}{
|
|
{"empty plugin id", "", pluginsdk.AgentConversationSpec{WorkspaceID: "ws-1", ConversationKey: "key"}},
|
|
{"empty workspace", "p", pluginsdk.AgentConversationSpec{WorkspaceID: "", ConversationKey: "key"}},
|
|
{"empty key", "p", pluginsdk.AgentConversationSpec{WorkspaceID: "ws-1", ConversationKey: ""}},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
_, _, err := svc.Ensure(context.Background(), tt.plugin, tt.spec)
|
|
if err == nil {
|
|
t.Fatal("expected error")
|
|
}
|
|
if code := status.Code(err); code != codes.InvalidArgument {
|
|
t.Fatalf("got code %v, want InvalidArgument", code)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestEnsureRepairsMissingPrimarySession(t *testing.T) {
|
|
svc, deps := newACTestService()
|
|
|
|
spec := pluginsdk.AgentConversationSpec{
|
|
WorkspaceID: "ws-1",
|
|
ConversationKey: "coordinator",
|
|
}
|
|
|
|
desc, statusStr, err := svc.Ensure(context.Background(), "plugin-coordinator", spec)
|
|
if err != nil {
|
|
t.Fatalf("first Ensure: %v", err)
|
|
}
|
|
if statusStr != AgentConversationStatusCreated {
|
|
t.Fatalf("status = %q, want created", statusStr)
|
|
}
|
|
|
|
// Delete the session to simulate a partial creation.
|
|
deps.sess.mu.Lock()
|
|
delete(deps.sess.sessions, desc.SessionID)
|
|
deps.sess.mu.Unlock()
|
|
|
|
// Second Ensure should repair the missing session.
|
|
desc2, statusStr, err := svc.Ensure(context.Background(), "plugin-coordinator", spec)
|
|
if err != nil {
|
|
t.Fatalf("second Ensure: %v", err)
|
|
}
|
|
if statusStr != AgentConversationStatusExists {
|
|
t.Fatalf("status = %q, want exists", statusStr)
|
|
}
|
|
if desc2.TaskID != desc.TaskID {
|
|
t.Fatalf("TaskID changed")
|
|
}
|
|
if desc2.SessionID != desc.SessionID {
|
|
t.Fatalf("SessionID = %q, want stable identity %q", desc2.SessionID, desc.SessionID)
|
|
}
|
|
if desc2.SessionID != conversationPrimarySessionID(desc.TaskID) {
|
|
t.Fatalf("SessionID = %q, want deterministic identity for task %q", desc2.SessionID, desc.TaskID)
|
|
}
|
|
if got := deps.sess.count(); got != 1 {
|
|
t.Fatalf("session count after repair = %d, want 1", got)
|
|
}
|
|
}
|
|
|
|
func TestEnsureStoresProfileIDInMetadata(t *testing.T) {
|
|
svc, deps := newACTestService()
|
|
|
|
spec := pluginsdk.AgentConversationSpec{
|
|
WorkspaceID: "ws-1",
|
|
ConversationKey: "coordinator",
|
|
AgentProfileID: "profile-gpt4",
|
|
}
|
|
|
|
desc, statusStr, err := svc.Ensure(context.Background(), "plugin-coordinator", spec)
|
|
if err != nil {
|
|
t.Fatalf("Ensure: %v", err)
|
|
}
|
|
if statusStr != AgentConversationStatusCreated {
|
|
t.Fatalf("status = %q, want created", statusStr)
|
|
}
|
|
|
|
if desc.AgentProfileID != "profile-gpt4" {
|
|
t.Fatalf("AgentProfileID = %q, want profile-gpt4", desc.AgentProfileID)
|
|
}
|
|
|
|
task, _, _ := deps.tasks.ListTasksByWorkspace(context.Background(), "ws-1", "", "", "", 1, 100, "", false, true, true, false)
|
|
if len(task) == 0 {
|
|
t.Fatal("no tasks found")
|
|
}
|
|
if p, ok := task[0].Metadata[models.MetaKeyAgentProfileID].(string); !ok || p != "profile-gpt4" {
|
|
t.Fatalf("metadata agent_profile_id = %v, want profile-gpt4", task[0].Metadata[models.MetaKeyAgentProfileID])
|
|
}
|
|
}
|
|
|
|
func TestEnsureProfileInDescriptorFromExistingTask(t *testing.T) {
|
|
svc, _ := newACTestService()
|
|
|
|
spec := pluginsdk.AgentConversationSpec{
|
|
WorkspaceID: "ws-1",
|
|
ConversationKey: "coordinator",
|
|
AgentProfileID: "profile-gpt4",
|
|
}
|
|
|
|
desc, _, err := svc.Ensure(context.Background(), "plugin-coordinator", spec)
|
|
if err != nil {
|
|
t.Fatalf("first Ensure: %v", err)
|
|
}
|
|
if desc.AgentProfileID != "profile-gpt4" {
|
|
t.Fatalf("first AgentProfileID = %q", desc.AgentProfileID)
|
|
}
|
|
|
|
desc2, statusStr, err := svc.Ensure(context.Background(), "plugin-coordinator", spec)
|
|
if err != nil {
|
|
t.Fatalf("second Ensure: %v", err)
|
|
}
|
|
if statusStr != AgentConversationStatusExists {
|
|
t.Fatalf("status = %q, want exists", statusStr)
|
|
}
|
|
if desc2.AgentProfileID != "profile-gpt4" {
|
|
t.Fatalf("second AgentProfileID = %q", desc2.AgentProfileID)
|
|
}
|
|
}
|
|
|
|
func TestEnsureCrossPluginSeparation(t *testing.T) {
|
|
svc, _ := newACTestService()
|
|
|
|
spec := pluginsdk.AgentConversationSpec{
|
|
WorkspaceID: "ws-1",
|
|
ConversationKey: "coordinator",
|
|
}
|
|
|
|
descA, _, err := svc.Ensure(context.Background(), "plugin-a", spec)
|
|
if err != nil {
|
|
t.Fatalf("plugin-a Ensure: %v", err)
|
|
}
|
|
|
|
descB, statusStr, err := svc.Ensure(context.Background(), "plugin-b", spec)
|
|
if err != nil {
|
|
t.Fatalf("plugin-b Ensure: %v", err)
|
|
}
|
|
if statusStr != AgentConversationStatusCreated {
|
|
t.Fatalf("plugin-b status = %q, want created (separate)", statusStr)
|
|
}
|
|
if descA.TaskID == descB.TaskID {
|
|
t.Fatal("plugin-a and plugin-b should have different task IDs")
|
|
}
|
|
}
|
|
|
|
func TestEnsureCrossWorkspaceSeparation(t *testing.T) {
|
|
svc, _ := newACTestService()
|
|
|
|
desc1, _, err := svc.Ensure(context.Background(), "plugin-c", pluginsdk.AgentConversationSpec{
|
|
WorkspaceID: "ws-1",
|
|
ConversationKey: "coordinator",
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("ws-1 Ensure: %v", err)
|
|
}
|
|
|
|
desc2, statusStr, err := svc.Ensure(context.Background(), "plugin-c", pluginsdk.AgentConversationSpec{
|
|
WorkspaceID: "ws-2",
|
|
ConversationKey: "coordinator",
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("ws-2 Ensure: %v", err)
|
|
}
|
|
if statusStr != AgentConversationStatusCreated {
|
|
t.Fatalf("ws-2 status = %q, want created", statusStr)
|
|
}
|
|
if desc1.TaskID == desc2.TaskID {
|
|
t.Fatal("different workspaces should have different task IDs")
|
|
}
|
|
}
|
|
|
|
func TestConcurrentEnsureIsIdempotent(t *testing.T) {
|
|
svc, _ := newACTestService()
|
|
|
|
spec := pluginsdk.AgentConversationSpec{
|
|
WorkspaceID: "ws-1",
|
|
ConversationKey: "coordinator",
|
|
}
|
|
|
|
var wg sync.WaitGroup
|
|
results := make([]struct {
|
|
desc pluginsdk.AgentConversationDescriptor
|
|
err error
|
|
}, 10)
|
|
|
|
for i := range 10 {
|
|
wg.Add(1)
|
|
go func(idx int) {
|
|
defer wg.Done()
|
|
desc, _, err := svc.Ensure(context.Background(), "plugin-coordinator", spec)
|
|
results[idx] = struct {
|
|
desc pluginsdk.AgentConversationDescriptor
|
|
err error
|
|
}{desc, err}
|
|
}(i)
|
|
}
|
|
wg.Wait()
|
|
|
|
taskIDs := make(map[string]int)
|
|
for _, r := range results {
|
|
if r.err != nil {
|
|
t.Fatalf("concurrent Ensure error: %v", r.err)
|
|
}
|
|
taskIDs[r.desc.TaskID]++
|
|
}
|
|
if len(taskIDs) != 1 {
|
|
t.Fatalf("expected 1 unique task ID, got %d: %v", len(taskIDs), taskIDs)
|
|
}
|
|
}
|
|
|
|
func TestStoresBasePromptInMetadata(t *testing.T) {
|
|
svc, deps := newACTestService()
|
|
|
|
spec := pluginsdk.AgentConversationSpec{
|
|
WorkspaceID: "ws-1",
|
|
ConversationKey: "coordinator",
|
|
BasePrompt: "You are a cycle coordinator.",
|
|
}
|
|
|
|
_, _, err := svc.Ensure(context.Background(), "plugin-coordinator", spec)
|
|
if err != nil {
|
|
t.Fatalf("Ensure: %v", err)
|
|
}
|
|
|
|
task, _, _ := deps.tasks.ListTasksByWorkspace(context.Background(), "ws-1", "", "", "", 1, 100, "", false, true, true, false)
|
|
if len(task) == 0 {
|
|
t.Fatal("no tasks")
|
|
}
|
|
if p, ok := task[0].Metadata["kandev.base_prompt"].(string); !ok || p != "You are a cycle coordinator." {
|
|
t.Fatalf("base_prompt = %v, want 'You are a cycle coordinator.'", task[0].Metadata["kandev.base_prompt"])
|
|
}
|
|
}
|
|
|
|
func TestEnsureReconcilesExistingConversationProfileAndBasePrompt(t *testing.T) {
|
|
svc, deps := newACTestService()
|
|
ctx := context.Background()
|
|
|
|
_, _, err := svc.Ensure(ctx, "plugin-coordinator", pluginsdk.AgentConversationSpec{
|
|
WorkspaceID: "ws-1",
|
|
ConversationKey: "coordinator",
|
|
AgentProfileID: "profile-old",
|
|
BasePrompt: "old base prompt",
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("first Ensure: %v", err)
|
|
}
|
|
|
|
desc, statusStr, err := svc.Ensure(ctx, "plugin-coordinator", pluginsdk.AgentConversationSpec{
|
|
WorkspaceID: "ws-1",
|
|
ConversationKey: "coordinator",
|
|
AgentProfileID: "profile-new",
|
|
BasePrompt: "new base prompt",
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("reconcile Ensure: %v", err)
|
|
}
|
|
if statusStr != AgentConversationStatusExists {
|
|
t.Fatalf("status = %q, want exists", statusStr)
|
|
}
|
|
if desc.AgentProfileID != "profile-new" {
|
|
t.Fatalf("descriptor profile = %q, want profile-new", desc.AgentProfileID)
|
|
}
|
|
|
|
primary, err := deps.sess.GetPrimarySessionByTaskID(ctx, desc.TaskID)
|
|
if err != nil {
|
|
t.Fatalf("GetPrimarySessionByTaskID: %v", err)
|
|
}
|
|
if primary.AgentProfileID != "profile-new" {
|
|
t.Fatalf("session profile = %q, want profile-new", primary.AgentProfileID)
|
|
}
|
|
|
|
if _, err := svc.Dispatch(ctx, "plugin-coordinator", "ws-1", "coordinator", "WAKE:CYCLE", ""); err != nil {
|
|
t.Fatalf("Dispatch: %v", err)
|
|
}
|
|
if got := deps.dispatcher.lastCall().text; got != "new base prompt\n\nWAKE:CYCLE" {
|
|
t.Fatalf("dispatch text = %q, want refreshed base prompt composed with wake", got)
|
|
}
|
|
}
|
|
|
|
func TestEnsureRejectsProfileChangeForLiveConversationSession(t *testing.T) {
|
|
svc, deps := newACTestService()
|
|
ctx := context.Background()
|
|
desc, _, err := svc.Ensure(ctx, "plugin-coordinator", pluginsdk.AgentConversationSpec{
|
|
WorkspaceID: "ws-1", ConversationKey: "coordinator", AgentProfileID: "profile-old",
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("first Ensure: %v", err)
|
|
}
|
|
deps.sess.setState(desc.SessionID, models.TaskSessionStateRunning)
|
|
|
|
_, _, err = svc.Ensure(ctx, "plugin-coordinator", pluginsdk.AgentConversationSpec{
|
|
WorkspaceID: "ws-1", ConversationKey: "coordinator", AgentProfileID: "profile-new",
|
|
})
|
|
if status.Code(err) != codes.FailedPrecondition {
|
|
t.Fatalf("Ensure() error code = %v, want FailedPrecondition (err=%v)", status.Code(err), err)
|
|
}
|
|
primary, err := deps.sess.GetPrimarySessionByTaskID(ctx, desc.TaskID)
|
|
if err != nil {
|
|
t.Fatalf("GetPrimarySessionByTaskID: %v", err)
|
|
}
|
|
if primary.AgentProfileID != "profile-old" {
|
|
t.Fatalf("live session profile = %q, want profile-old", primary.AgentProfileID)
|
|
}
|
|
}
|
|
|
|
func TestDispatchRetriesOccurrenceAfterDeliveryFailure(t *testing.T) {
|
|
svc, deps := newACTestService()
|
|
ctx := context.Background()
|
|
if _, _, err := svc.Ensure(ctx, "plugin-coordinator", pluginsdk.AgentConversationSpec{WorkspaceID: "ws-1", ConversationKey: "coordinator"}); err != nil {
|
|
t.Fatalf("Ensure: %v", err)
|
|
}
|
|
|
|
deps.dispatcher.err = errors.New("temporary dispatcher outage")
|
|
if _, err := svc.Dispatch(ctx, "plugin-coordinator", "ws-1", "coordinator", "WAKE:CYCLE", "occurrence-1"); err == nil {
|
|
t.Fatal("first Dispatch error = nil, want delivery failure")
|
|
}
|
|
deps.dispatcher.err = nil
|
|
|
|
dispatch, err := svc.Dispatch(ctx, "plugin-coordinator", "ws-1", "coordinator", "WAKE:CYCLE", "occurrence-1")
|
|
if err != nil {
|
|
t.Fatalf("retry Dispatch: %v", err)
|
|
}
|
|
if dispatch.Status != "started" {
|
|
t.Fatalf("retry status = %q, want started", dispatch.Status)
|
|
}
|
|
if got := deps.dispatcher.callCount(); got != 1 {
|
|
t.Fatalf("dispatcher calls = %d, want one successful retry", got)
|
|
}
|
|
duplicate, err := svc.Dispatch(ctx, "plugin-coordinator", "ws-1", "coordinator", "WAKE:CYCLE", "occurrence-1")
|
|
if err != nil {
|
|
t.Fatalf("duplicate Dispatch: %v", err)
|
|
}
|
|
if duplicate.Status != "duplicate_occurrence" {
|
|
t.Fatalf("duplicate status = %q, want duplicate_occurrence", duplicate.Status)
|
|
}
|
|
if got := deps.dispatcher.callCount(); got != 1 {
|
|
t.Fatalf("dispatcher calls after duplicate = %d, want one", got)
|
|
}
|
|
}
|
|
|
|
func TestDispatchRecoversPendingOccurrenceAfterRestart(t *testing.T) {
|
|
svc, deps := newACTestService()
|
|
ctx := context.Background()
|
|
if _, _, err := svc.Ensure(ctx, "plugin-coordinator", pluginsdk.AgentConversationSpec{
|
|
WorkspaceID: "ws-1", ConversationKey: "coordinator",
|
|
}); err != nil {
|
|
t.Fatalf("Ensure: %v", err)
|
|
}
|
|
|
|
// Simulate a process stopping after the durable claim but before it reaches
|
|
// the dispatcher. A pending claim is recoverable; it is not an accepted
|
|
// duplicate and it must not require claim expiry.
|
|
deps.state.seedPending("plugin-coordinator", "ws-1", "coordinator", "occurrence-restart")
|
|
dispatch, err := svc.Dispatch(ctx, "plugin-coordinator", "ws-1", "coordinator", "WAKE:CYCLE", "occurrence-restart")
|
|
if err != nil {
|
|
t.Fatalf("recovered Dispatch: %v", err)
|
|
}
|
|
if dispatch.Status != "started" {
|
|
t.Fatalf("recovered status = %q, want started", dispatch.Status)
|
|
}
|
|
if got := deps.dispatcher.callCount(); got != 1 {
|
|
t.Fatalf("dispatcher calls = %d, want one recovered delivery", got)
|
|
}
|
|
raw, found, err := deps.state.Get(ctx, "plugin-coordinator", agentConversationOccurrenceScope, "ws-1/coordinator", "occurrence-restart")
|
|
if err != nil || !found {
|
|
t.Fatalf("accepted occurrence state = (%v, %v), want a stored value", string(raw), found)
|
|
}
|
|
if got := occurrenceStatus(raw); got != agentConversationOccurrenceAccepted {
|
|
t.Fatalf("occurrence status = %q, want accepted", got)
|
|
}
|
|
}
|
|
|
|
func TestDispatchBusySessionDoesNotConsumeOccurrence(t *testing.T) {
|
|
svc, deps := newACTestService()
|
|
ctx := context.Background()
|
|
desc, _, err := svc.Ensure(ctx, "plugin-coordinator", pluginsdk.AgentConversationSpec{WorkspaceID: "ws-1", ConversationKey: "coordinator"})
|
|
if err != nil {
|
|
t.Fatalf("Ensure: %v", err)
|
|
}
|
|
deps.sess.setState(desc.SessionID, models.TaskSessionStateRunning)
|
|
|
|
busy, err := svc.Dispatch(ctx, "plugin-coordinator", "ws-1", "coordinator", "WAKE:CYCLE", "occurrence-1")
|
|
if err != nil {
|
|
t.Fatalf("busy Dispatch: %v", err)
|
|
}
|
|
if busy.Status != "skipped_busy" {
|
|
t.Fatalf("busy status = %q, want skipped_busy", busy.Status)
|
|
}
|
|
|
|
deps.sess.setState(desc.SessionID, models.TaskSessionStateIdle)
|
|
dispatch, err := svc.Dispatch(ctx, "plugin-coordinator", "ws-1", "coordinator", "WAKE:CYCLE", "occurrence-1")
|
|
if err != nil {
|
|
t.Fatalf("retry Dispatch: %v", err)
|
|
}
|
|
if dispatch.Status != "sent" {
|
|
t.Fatalf("retry status = %q, want sent", dispatch.Status)
|
|
}
|
|
}
|
|
|
|
func TestDeleteRejectsEmptyOwnershipIdentifiers(t *testing.T) {
|
|
svc, _ := newACTestService()
|
|
|
|
for _, tc := range []struct {
|
|
name, pluginID, workspaceID, conversationKey string
|
|
}{
|
|
{name: "plugin", workspaceID: "ws-1", conversationKey: "coordinator"},
|
|
{name: "workspace", pluginID: "plugin-coordinator", conversationKey: "coordinator"},
|
|
{name: "key", pluginID: "plugin-coordinator", workspaceID: "ws-1"},
|
|
} {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
_, err := svc.Delete(context.Background(), tc.pluginID, tc.workspaceID, tc.conversationKey)
|
|
if status.Code(err) != codes.InvalidArgument {
|
|
t.Fatalf("Delete error code = %v, want InvalidArgument (err=%v)", status.Code(err), err)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestAgentConversationTaskIsCreatedAsEphemeral(t *testing.T) {
|
|
svc, deps := newACTestService()
|
|
|
|
spec := pluginsdk.AgentConversationSpec{
|
|
WorkspaceID: "ws-1",
|
|
ConversationKey: "coordinator",
|
|
}
|
|
if _, _, err := svc.Ensure(context.Background(), "plugin-coordinator", spec); err != nil {
|
|
t.Fatalf("Ensure: %v", err)
|
|
}
|
|
|
|
all, _, _ := deps.tasks.ListTasksByWorkspace(context.Background(), "ws-1", "", "", "", 1, 100, "", false, true, true, false)
|
|
if len(all) != 1 {
|
|
t.Fatalf("expected 1 task, got %d", len(all))
|
|
}
|
|
if !all[0].IsEphemeral {
|
|
t.Fatal("task should be ephemeral")
|
|
}
|
|
if all[0].Origin != models.TaskOriginManual {
|
|
t.Fatalf("origin = %q, want manual", all[0].Origin)
|
|
}
|
|
if all[0].Priority != "medium" {
|
|
t.Fatalf("priority = %q, want medium", all[0].Priority)
|
|
}
|
|
if all[0].State != v1.TaskStateCreated {
|
|
t.Fatalf("state = %v, want CREATED", all[0].State)
|
|
}
|
|
}
|