mirror of
https://github.com/Ed1s0nZ/CyberStrikeAI.git
synced 2026-12-01 12:03:26 +01:00
fix(notifications): hide long-running task alerts after marking them read
This commit is contained in:
1 parent
b6f755717b
commit
85da348093
2 files changed
+108
-2
No files matched your search
@@ -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:",
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user