From 85da3480933ef9308b9682cde7e07c9898a46d8b Mon Sep 17 00:00:00 2001 From: Sycun <165354365+Sycun@users.noreply.github.com> Date: Thu, 19 Nov 2026 11:18:05 +0800 Subject: [PATCH] fix(notifications): hide long-running task alerts after marking them read --- internal/handler/notification.go | 5 +- internal/handler/notification_rbac_test.go | 105 +++++++++++++++++++++ 2 files changed, 108 insertions(+), 2 deletions(-) diff --git a/internal/handler/notification.go b/internal/handler/notification.go index 278c7836..18a041f1 100644 --- a/internal/handler/notification.go +++ b/internal/handler/notification.go @@ -432,14 +432,14 @@ func (h *NotificationHandler) summarizeLongRunningTasks(threshold time.Duration, } if now.Sub(t.StartedAt) >= threshold { items = append(items, NotificationSummaryItem{ - ID: "task_long:" + t.ConversationID, + ID: "task_long:" + t.RunID, Level: "p1", Type: "long_running_tasks", Title: i18nText(english, "长时间运行任务", "Long Running Task"), Desc: i18nText(english, "会话 "+t.ConversationID+" 运行超过 15 分钟", "Conversation "+t.ConversationID+" has been running over 15 minutes"), Ts: t.StartedAt.UTC().Format(time.RFC3339), Count: 1, - Actionable: true, + Actionable: false, Read: false, ConversationID: t.ConversationID, }) @@ -616,6 +616,7 @@ func normalizeMarkableEventID(id string) (string, bool) { allowedPrefixes := []string{ "vuln:", "exec_failed:", + "task_long:", "task_completed:", "c2evt:", } diff --git a/internal/handler/notification_rbac_test.go b/internal/handler/notification_rbac_test.go index ad05edef..2f6baa24 100644 --- a/internal/handler/notification_rbac_test.go +++ b/internal/handler/notification_rbac_test.go @@ -2,10 +2,12 @@ package handler import ( "bytes" + "encoding/json" "net/http" "net/http/httptest" "path/filepath" "testing" + "time" "cyberstrike-ai/internal/database" "cyberstrike-ai/internal/security" @@ -43,3 +45,106 @@ func TestNotificationReadStateIsPerUser(t *testing.T) { t.Fatalf("u2 inherited u1 read state = %#v, err=%v", u2, err) } } + +func TestLongRunningNotificationCanBeMarkedRead(t *testing.T) { + db, err := database.NewDB(filepath.Join(t.TempDir(), "notifications.db"), zap.NewNop()) + if err != nil { + t.Fatal(err) + } + t.Cleanup(func() { _ = db.Close() }) + + const conversationID = "conversation-1" + if _, err := db.Exec(` + INSERT INTO hitl_interrupts (id, conversation_id, mode, tool_name, status, created_at) + VALUES ('approval-1', ?, 'approval', 'fixture', 'pending', CURRENT_TIMESTAMP) + `, conversationID); err != nil { + t.Fatal(err) + } + + tasks := NewAgentTaskManager() + t.Cleanup(tasks.Shutdown) + startLongRun := func() *AgentTask { + t.Helper() + task, err := tasks.StartTask(conversationID, "fixture", nil) + if err != nil { + t.Fatal(err) + } + tasks.mu.Lock() + task.StartedAt = time.Now().Add(-16 * time.Minute) + tasks.mu.Unlock() + return task + } + first := startLongRun() + h := NewNotificationHandler(db, &AgentHandler{tasks: tasks}, zap.NewNop()) + router := gin.New() + router.Use(func(c *gin.Context) { + c.Set(security.ContextSessionKey, security.Session{ + UserID: c.GetHeader("X-Test-User"), Scope: database.RBACScopeAll, + Permissions: map[string]bool{"tasks:read": true, "hitl:read": true}, + }) + }) + router.GET("/notifications/summary", h.GetSummary) + router.POST("/notifications/read", h.MarkRead) + request := func(method, path, userID string, body []byte) []byte { + t.Helper() + req := httptest.NewRequest(method, path, bytes.NewReader(body)) + req.Header.Set("X-Test-User", userID) + req.Header.Set("Content-Type", "application/json") + w := httptest.NewRecorder() + router.ServeHTTP(w, req) + if w.Code != http.StatusOK { + t.Fatalf("%s %s status = %d: %s", method, path, w.Code, w.Body.String()) + } + return w.Body.Bytes() + } + summary := func(userID string) NotificationSummaryResponse { + t.Helper() + var result NotificationSummaryResponse + if err := json.Unmarshal(request(http.MethodGet, "/notifications/summary", userID, nil), &result); err != nil { + t.Fatal(err) + } + return result + } + longRunningID := func(result NotificationSummaryResponse) string { + for _, item := range result.Items { + if item.Type == "long_running_tasks" && item.ConversationID == conversationID { + return item.ID + } + } + return "" + } + + before := summary("u1") + id := longRunningID(before) + if id == "" || before.UnreadCount != 2 { + t.Fatalf("expected a long-running and an approval notification, got %+v", before) + } + body, err := json.Marshal(markReadRequest{EventIDs: []string{id, "hitl:approval-1"}}) + if err != nil { + t.Fatal(err) + } + var marked struct { + Marked int `json:"marked"` + } + if err := json.Unmarshal(request(http.MethodPost, "/notifications/read", "u1", body), &marked); err != nil { + t.Fatal(err) + } + if marked.Marked != 1 { + t.Fatalf("marked = %d, want only the long-running notification marked", marked.Marked) + } + + after := summary("u1") + if after.UnreadCount != 1 || len(after.Items) != 1 || after.Items[0].Type != "hitl_pending" || after.Counts["longRunningTasks"] != 1 { + t.Fatalf("expected only the approval notification while the task keeps running, got %+v", after) + } + if otherID := longRunningID(summary("u2")); otherID != id { + t.Fatalf("other user saw notification %q, want %q", otherID, id) + } + if err := tasks.FinishTaskRun(conversationID, first.RunID, "completed"); err != nil { + t.Fatal(err) + } + startLongRun() + if nextID := longRunningID(summary("u1")); nextID == "" || nextID == id { + t.Fatalf("new run must have a new notification, got %q after %q", nextID, id) + } +}