feat: show queued prompt count badge in the task sidebar (#2354)
This commit is contained in:
@@ -0,0 +1,141 @@
|
||||
package messagequeue
|
||||
|
||||
import (
|
||||
"context"
|
||||
"reflect"
|
||||
"testing"
|
||||
|
||||
"github.com/kandev/kandev/internal/common/logger"
|
||||
)
|
||||
|
||||
// repositoriesUnderTest returns one SQLite and one memory Repository so the
|
||||
// pending-count contract is proven on both storage backends.
|
||||
func repositoriesUnderTest(t *testing.T) map[string]Repository {
|
||||
t.Helper()
|
||||
return map[string]Repository{
|
||||
"sqlite": newTestSQLiteRepo(t),
|
||||
"memory": NewMemoryRepository(),
|
||||
}
|
||||
}
|
||||
|
||||
func TestCountPendingByTaskIDsAccumulatesAcrossSessions(t *testing.T) {
|
||||
for name, repo := range repositoriesUnderTest(t) {
|
||||
t.Run(name, func(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
insert := func(sessionID, taskID string) {
|
||||
t.Helper()
|
||||
msg := &QueuedMessage{SessionID: sessionID, TaskID: taskID, Content: "x", QueuedBy: QueuedByUser}
|
||||
if err := repo.Insert(ctx, msg, 0); err != nil {
|
||||
t.Fatalf("insert %s/%s: %v", sessionID, taskID, err)
|
||||
}
|
||||
}
|
||||
insert("s1", "t1")
|
||||
insert("s1", "t1")
|
||||
insert("s2", "t1")
|
||||
insert("s2", "t2")
|
||||
|
||||
got, err := repo.CountPendingByTaskIDs(ctx, []string{"t1", "t2"})
|
||||
if err != nil {
|
||||
t.Fatalf("CountPendingByTaskIDs: %v", err)
|
||||
}
|
||||
want := map[string]int{"t1": 3, "t2": 1}
|
||||
if !reflect.DeepEqual(got, want) {
|
||||
t.Errorf("counts = %v, want %v", got, want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestCountPendingByTaskIDsExcludesReservedInFlight(t *testing.T) {
|
||||
for name, repo := range repositoriesUnderTest(t) {
|
||||
t.Run(name, func(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
insert := func(sessionID, taskID, content string, metadata map[string]interface{}) {
|
||||
t.Helper()
|
||||
msg := &QueuedMessage{SessionID: sessionID, TaskID: taskID, Content: content, QueuedBy: QueuedByWorkflow, Metadata: metadata}
|
||||
if err := repo.Insert(ctx, msg, 0); err != nil {
|
||||
t.Fatalf("insert: %v", err)
|
||||
}
|
||||
}
|
||||
// The durable lifecycle row must be the head so ReserveHead marks IT
|
||||
// in flight; the ordinary pending row stays pending below it.
|
||||
insert("s1", "t1", "lifecycle", map[string]interface{}{MetadataLifecycleDurable: true})
|
||||
insert("s1", "t1", "pending", nil)
|
||||
// Reserve the durable head: it becomes in-flight and must not count.
|
||||
if _, err := repo.ReserveHead(ctx, "s1"); err != nil {
|
||||
t.Fatalf("reserve: %v", err)
|
||||
}
|
||||
|
||||
got, err := repo.CountPendingByTaskIDs(ctx, []string{"t1"})
|
||||
if err != nil {
|
||||
t.Fatalf("CountPendingByTaskIDs: %v", err)
|
||||
}
|
||||
if got["t1"] != 1 {
|
||||
t.Errorf("pending count for t1 = %d, want 1 (reserved in-flight row excluded)", got["t1"])
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestCountPendingByTaskIDsReturnsZeroForEmptyAndMissing(t *testing.T) {
|
||||
for name, repo := range repositoriesUnderTest(t) {
|
||||
t.Run(name, func(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
got, err := repo.CountPendingByTaskIDs(ctx, []string{"missing"})
|
||||
if err != nil {
|
||||
t.Fatalf("CountPendingByTaskIDs: %v", err)
|
||||
}
|
||||
want := map[string]int{"missing": 0}
|
||||
if !reflect.DeepEqual(got, want) {
|
||||
t.Errorf("counts = %v, want %v", got, want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestCountPendingByTaskIDsIsolatesTasks(t *testing.T) {
|
||||
for name, repo := range repositoriesUnderTest(t) {
|
||||
t.Run(name, func(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
for _, taskID := range []string{"ta", "tb", "tc"} {
|
||||
msg := &QueuedMessage{SessionID: "s1", TaskID: taskID, Content: "x", QueuedBy: QueuedByUser}
|
||||
if err := repo.Insert(ctx, msg, 0); err != nil {
|
||||
t.Fatalf("insert %s: %v", taskID, err)
|
||||
}
|
||||
}
|
||||
got, err := repo.CountPendingByTaskIDs(ctx, []string{"ta", "tc"})
|
||||
if err != nil {
|
||||
t.Fatalf("CountPendingByTaskIDs: %v", err)
|
||||
}
|
||||
want := map[string]int{"ta": 1, "tc": 1}
|
||||
if !reflect.DeepEqual(got, want) {
|
||||
t.Errorf("counts = %v, want %v", got, want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestServiceCountPendingByTask(t *testing.T) {
|
||||
svc := NewServiceMemory(logger.Default())
|
||||
ctx := context.Background()
|
||||
msg := &QueuedMessage{SessionID: "s1", TaskID: "t1", Content: "x", QueuedBy: QueuedByUser}
|
||||
if err := svc.repo.Insert(ctx, msg, 0); err != nil {
|
||||
t.Fatalf("insert: %v", err)
|
||||
}
|
||||
|
||||
count, err := svc.CountPendingByTask(ctx, "t1")
|
||||
if err != nil {
|
||||
t.Fatalf("CountPendingByTask: %v", err)
|
||||
}
|
||||
if count != 1 {
|
||||
t.Errorf("CountPendingByTask(t1) = %d, want 1", count)
|
||||
}
|
||||
|
||||
empty, err := svc.CountPendingByTask(ctx, "nope")
|
||||
if err != nil {
|
||||
t.Fatalf("CountPendingByTask empty: %v", err)
|
||||
}
|
||||
if empty != 0 {
|
||||
t.Errorf("CountPendingByTask(nope) = %d, want 0", empty)
|
||||
}
|
||||
}
|
||||
@@ -45,6 +45,13 @@ type Repository interface {
|
||||
// CountBySession returns the number of entries for a session.
|
||||
CountBySession(ctx context.Context, sessionID string) (int, error)
|
||||
|
||||
// CountPendingByTaskIDs returns the number of pending entries per task,
|
||||
// keyed by task_id, for every requested task ID (zero when a task has no
|
||||
// pending entries). Pending excludes durable lifecycle rows already
|
||||
// reserved in flight, matching GetStatus semantics. The reserved exclusion
|
||||
// is applied in Go via IsReservedInFlight, never by matching JSON in SQL.
|
||||
CountPendingByTaskIDs(ctx context.Context, taskIDs []string) (map[string]int, error)
|
||||
|
||||
// TakeHead atomically returns and deletes the lowest-position entry for the
|
||||
// session. Returns nil, nil if the queue is empty.
|
||||
TakeHead(ctx context.Context, sessionID string) (*QueuedMessage, error)
|
||||
|
||||
@@ -60,6 +60,31 @@ func (r *memoryRepository) PurgeTask(_ context.Context, taskID string) (int, err
|
||||
return removed, nil
|
||||
}
|
||||
|
||||
// CountPendingByTaskIDs counts pending entries per task, excluding durable
|
||||
// lifecycle rows reserved in flight (filtered via IsReservedInFlight).
|
||||
func (r *memoryRepository) CountPendingByTaskIDs(_ context.Context, taskIDs []string) (map[string]int, error) {
|
||||
r.mu.Lock()
|
||||
defer r.mu.Unlock()
|
||||
counts := make(map[string]int, len(taskIDs))
|
||||
for _, taskID := range taskIDs {
|
||||
counts[taskID] = 0
|
||||
}
|
||||
if len(taskIDs) == 0 {
|
||||
return counts, nil
|
||||
}
|
||||
for _, list := range r.entries {
|
||||
for _, msg := range list {
|
||||
if msg.IsReservedInFlight() {
|
||||
continue
|
||||
}
|
||||
if _, wanted := counts[msg.TaskID]; wanted {
|
||||
counts[msg.TaskID]++
|
||||
}
|
||||
}
|
||||
}
|
||||
return counts, nil
|
||||
}
|
||||
|
||||
func (r *memoryRepository) Insert(_ context.Context, msg *QueuedMessage, maxPerSession int) error {
|
||||
r.mu.Lock()
|
||||
defer r.mu.Unlock()
|
||||
|
||||
@@ -537,6 +537,46 @@ func (r *sqliteRepository) CountBySession(ctx context.Context, sessionID string)
|
||||
return n, err
|
||||
}
|
||||
|
||||
// CountPendingByTaskIDs counts pending entries per task, excluding durable
|
||||
// lifecycle rows reserved in flight (filtered in Go via IsReservedInFlight).
|
||||
func (r *sqliteRepository) CountPendingByTaskIDs(ctx context.Context, taskIDs []string) (map[string]int, error) {
|
||||
counts := make(map[string]int, len(taskIDs))
|
||||
for _, taskID := range taskIDs {
|
||||
counts[taskID] = 0
|
||||
}
|
||||
if len(taskIDs) == 0 {
|
||||
return counts, nil
|
||||
}
|
||||
query, args, err := sqlx.In(`
|
||||
SELECT id, session_id, task_id, position, content, model, plan_mode,
|
||||
attachments_json, metadata_json, queued_at, queued_by
|
||||
FROM queued_messages
|
||||
WHERE task_id IN (?)
|
||||
`, taskIDs)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("count pending by task ids: %w", err)
|
||||
}
|
||||
rows, err := r.ro.QueryxContext(ctx, r.ro.Rebind(query), args...)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("count pending by task ids: %w", err)
|
||||
}
|
||||
defer func() { _ = rows.Close() }()
|
||||
for rows.Next() {
|
||||
msg, err := scanQueuedRow(rows)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("count pending by task ids scan: %w", err)
|
||||
}
|
||||
if msg.IsReservedInFlight() {
|
||||
continue
|
||||
}
|
||||
counts[msg.TaskID]++
|
||||
}
|
||||
if err := rows.Err(); err != nil {
|
||||
return nil, fmt.Errorf("count pending by task ids rows: %w", err)
|
||||
}
|
||||
return counts, nil
|
||||
}
|
||||
|
||||
func (r *sqliteRepository) TakeHead(ctx context.Context, sessionID string) (*QueuedMessage, error) {
|
||||
// Share the per-session lock with MergeIntoAbove so a drain and a merge on
|
||||
// the same queue are serialized in-process, not just at the DB layer.
|
||||
|
||||
@@ -649,6 +649,30 @@ func (s *Service) GetStatus(ctx context.Context, sessionID string) *QueueStatus
|
||||
}
|
||||
}
|
||||
|
||||
// CountPendingByTaskIDs returns the pending prompt count per task, keyed by
|
||||
// task_id, for every requested task. Reserved in-flight lifecycle rows are
|
||||
// excluded, matching GetStatus. Used by task-list assembly and the status
|
||||
// summary projector to render per-task queued-prompt badges.
|
||||
func (s *Service) CountPendingByTaskIDs(ctx context.Context, taskIDs []string) (map[string]int, error) {
|
||||
counts, err := s.repo.CountPendingByTaskIDs(ctx, taskIDs)
|
||||
if err != nil {
|
||||
s.logger.Error("count pending by task ids failed",
|
||||
zap.Int("task_count", len(taskIDs)),
|
||||
zap.Error(err))
|
||||
return nil, err
|
||||
}
|
||||
return counts, nil
|
||||
}
|
||||
|
||||
// CountPendingByTask returns the pending prompt count for one task.
|
||||
func (s *Service) CountPendingByTask(ctx context.Context, taskID string) (int, error) {
|
||||
counts, err := s.CountPendingByTaskIDs(ctx, []string{taskID})
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return counts[taskID], nil
|
||||
}
|
||||
|
||||
// SnapshotSession returns the complete persisted queue state for rollback.
|
||||
// Unlike GetStatus, it includes durable lifecycle rows reserved by an in-flight
|
||||
// delivery so a later restore cannot erase them before acknowledgement.
|
||||
|
||||
Reference in New Issue
Block a user