mirror of
https://github.com/Ed1s0nZ/CyberStrikeAI.git
synced 2026-10-21 14:52:05 +02:00
fix: retain knowledge indexing errors and explain failure causes
This commit is contained in:
1 parent
bb96a084e0
commit
5dc9cb1541
7 files changed
+94
-11
No files matched your search
@@ -0,0 +1,30 @@
|
||||
package agent
|
||||
|
||||
import (
|
||||
"cyberstrike-ai/internal/knowledge"
|
||||
"cyberstrike-ai/internal/mcp/builtin"
|
||||
"go.uber.org/zap"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestRegisteredKnowledgeToolsAreAvailableToDefaultAndSelectedRoles(t *testing.T) {
|
||||
a := setupTestAgent(t)
|
||||
knowledge.RegisterKnowledgeTool(a.mcpServer, nil, nil, zap.NewNop())
|
||||
for _, role := range [][]string{nil, {builtin.ToolSearchKnowledgeBase, builtin.ToolListKnowledgeRiskTypes}} {
|
||||
available := map[string]bool{}
|
||||
for _, tool := range a.ToolsForRole(role) {
|
||||
available[tool.Function.Name] = true
|
||||
}
|
||||
for _, name := range []string{builtin.ToolSearchKnowledgeBase, builtin.ToolListKnowledgeRiskTypes} {
|
||||
if !available[name] {
|
||||
t.Errorf("registered tool %s unavailable to role %v", name, role)
|
||||
}
|
||||
}
|
||||
}
|
||||
// Explicit role restrictions remain effective.
|
||||
for _, tool := range a.ToolsForRole([]string{builtin.ToolListKnowledgeRiskTypes}) {
|
||||
if tool.Function.Name == builtin.ToolSearchKnowledgeBase {
|
||||
t.Fatal("role restriction bypassed")
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -489,13 +489,8 @@ func (h *KnowledgeHandler) GetIndexStatus(c *gin.Context) {
|
||||
// 获取索引器的错误信息
|
||||
if h.indexer != nil {
|
||||
lastError, lastErrorTime := h.indexer.GetLastError()
|
||||
if lastError != "" {
|
||||
// 如果错误是最近发生的(5 分钟内),则返回错误信息
|
||||
if time.Since(lastErrorTime) < 5*time.Minute {
|
||||
status["last_error"] = lastError
|
||||
status["last_error_time"] = lastErrorTime.Format(time.RFC3339)
|
||||
}
|
||||
}
|
||||
// Keep a failed operation visible until the indexer clears it for the next run.
|
||||
appendKnowledgeIndexError(status, lastError, lastErrorTime)
|
||||
|
||||
// 获取重建索引状态
|
||||
isRebuilding, totalItems, current, failed, lastItemID, lastChunks, startTime := h.indexer.GetRebuildStatus()
|
||||
@@ -564,3 +559,11 @@ func parseInt(s string) (int, error) {
|
||||
_, err := fmt.Sscanf(s, "%d", &result)
|
||||
return result, err
|
||||
}
|
||||
|
||||
func appendKnowledgeIndexError(status map[string]interface{}, message string, occurredAt time.Time) {
|
||||
if message == "" {
|
||||
return
|
||||
}
|
||||
status["last_error"] = message
|
||||
status["last_error_time"] = occurredAt.Format(time.RFC3339)
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func TestKnowledgeIndexFailureRemainsVisibleUntilCleared(t *testing.T) {
|
||||
occurred := time.Now().Add(-time.Hour)
|
||||
status := map[string]interface{}{}
|
||||
appendKnowledgeIndexError(status, "test-only embedding failure: 401 Unauthorized", occurred)
|
||||
if status["last_error"] == nil || status["last_error_time"] != occurred.Format(time.RFC3339) {
|
||||
t.Fatal("older failure disappeared")
|
||||
}
|
||||
cleared := map[string]interface{}{}
|
||||
appendKnowledgeIndexError(cleared, "", time.Time{})
|
||||
if _, ok := cleared["last_error"]; ok {
|
||||
t.Fatal("cleared failure remained")
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user