Skip to content

Self-hosted workflows for coding-agent delivery. Orchestrate CodeBuddy, Codex and Claude Code with scripts and human approval gates. Includes Local workflows for coding assistants.

【架构优化】从 listSessions 中拆出会话去重与可见性过滤 visibleAssistantConversations (#585)

fx-kurumie86d108be332
diff --git a/internal/httpapi/assistant_session.go b/internal/httpapi/assistant_session.go
index 73393aea..5d6ded0c 100644
--- a/internal/httpapi/assistant_session.go
+++ b/internal/httpapi/assistant_session.go
@@ -76,38 +76,16 @@ func (h *assistantHandlers) listSessions(c *gin.Context) {
allowedProjects[id] = workbenchProjectAllowsUser(project, current.ID, platformAdmin)
}
}
- seen := map[string]bool{}
- out := make([]assistantConversation, 0, len(items))
- for _, item := range append(append(items, shared...), projectChats...) {
- if seen[item.ID] {
- continue
- }
- seen[item.ID] = true
- if item.Shared {
- // As in conversationScope, only valid shared records use project
- // authorization; other owned rows retain the private-session fallback.
- if sharedIDs[item.ID] && (item.ProjectID == "" || runProjects[item.RunID] != item.ProjectID || !allowedProjects[item.ProjectID]) {
- continue
- }
- } else if item.RunID != "" {
- if sharedIDs[repo.RunAssistantSessionID(item.RunID)] {
- continue
- }
- } else if item.UserID != current.ID && !allowedProjects[item.ProjectID] {
- // Other members' project conversations require current membership.
- continue
- }
- item.GenerationStatus = "idle"
- item.GenerationID = ""
- item.Unread = false
- out = append(out, item)
- }
+ out := visibleAssistantConversations(append(append(items, shared...), projectChats...), current.ID, sharedIDs, runProjects, allowedProjects)
ids := make([]string, 0, len(out))
for _, item := range out {
ids = append(ids, item.ID)
}
statuses := h.repo.AssistantGenerationStatuses(current.ID, ids)
for i := range out {
+ out[i].GenerationStatus = "idle"
+ out[i].GenerationID = ""
+ out[i].Unread = false
if state, ok := statuses[out[i].ID]; ok {
out[i].GenerationStatus = state.Status
out[i].GenerationID = state.ID
@@ -136,6 +114,37 @@ func (h *assistantHandlers) listSessions(c *gin.Context) {
c.JSON(200, decorateRunSessions(h.repo, out))
}
+// visibleAssistantConversations deduplicates candidates by ID (keeping the
+// first occurrence) and drops the ones userID may not see in the session list.
+// sharedIDs holds the IDs of cross-user shared records, runProjects maps a run
+// to its owning project and allowedProjects records project access.
+func visibleAssistantConversations(candidates []assistantConversation, userID int64, sharedIDs map[string]bool, runProjects map[string]string, allowedProjects map[string]bool) []assistantConversation {
+ seen := map[string]bool{}
+ out := make([]assistantConversation, 0, len(candidates))
+ for _, item := range candidates {
+ if seen[item.ID] {
+ continue
+ }
+ seen[item.ID] = true
+ if item.Shared {
+ // As in conversationScope, only valid shared records use project
+ // authorization; other owned rows retain the private-session fallback.
+ if sharedIDs[item.ID] && (item.ProjectID == "" || runProjects[item.RunID] != item.ProjectID || !allowedProjects[item.ProjectID]) {
+ continue
+ }
+ } else if item.RunID != "" {
+ if sharedIDs[repo.RunAssistantSessionID(item.RunID)] {
+ continue
+ }
+ } else if item.UserID != userID && !allowedProjects[item.ProjectID] {
+ // Other members' project conversations require current membership.
+ continue
+ }
+ out = append(out, item)
+ }
+ return out
+}
+
func (h *assistantHandlers) archiveSession(c *gin.Context) {
user, ok := h.requestUser(c.Request, true)
if !ok {
diff --git a/internal/httpapi/assistant_session_visible_test.go b/internal/httpapi/assistant_session_visible_test.go
new file mode 100644
index 00000000..9058b6f2
--- /dev/null
+++ b/internal/httpapi/assistant_session_visible_test.go
@@ -0,0 +1,80 @@
+package httpapi
+
+import (
+ "reflect"
+ "testing"
+
+ "agentflow/internal/repo"
+)
+
+func TestVisibleAssistantConversations(t *testing.T) {
+ const me, other = int64(1), int64(2)
+ sharedRunID := repo.RunAssistantSessionID("run-shared")
+ sharedIDs := map[string]bool{"shared-ok": true, "shared-no-project": true, "shared-wrong-run": true, "shared-forbidden": true, sharedRunID: true}
+ runProjects := map[string]string{"run-ok": "p1", "run-wrong": "p2", "run-forbidden": "p3"}
+ allowedProjects := map[string]bool{"p1": true, "p2": true, "p3": false}
+
+ cases := []struct {
+ name string
+ candidate assistantConversation
+ visible bool
+ }{
+ {"own plain conversation", assistantConversation{ID: "own", UserID: me}, true},
+ {"valid shared record", assistantConversation{ID: "shared-ok", Shared: true, RunID: "run-ok", ProjectID: "p1"}, true},
+ {"shared record without project", assistantConversation{ID: "shared-no-project", Shared: true, RunID: "run-ok"}, false},
+ {"shared record whose run belongs elsewhere", assistantConversation{ID: "shared-wrong-run", Shared: true, RunID: "run-wrong", ProjectID: "p1"}, false},
+ {"shared record in forbidden project", assistantConversation{ID: "shared-forbidden", Shared: true, RunID: "run-forbidden", ProjectID: "p3"}, false},
+ {"owned row flagged shared keeps private fallback", assistantConversation{ID: "owned-shared", Shared: true, UserID: me}, true},
+ {"run conversation covered by shared copy", assistantConversation{ID: "own-run", UserID: me, RunID: "run-shared"}, false},
+ {"run conversation without shared copy", assistantConversation{ID: "own-run-solo", UserID: me, RunID: "run-solo"}, true},
+ {"other member project chat with access", assistantConversation{ID: "other-allowed", UserID: other, ProjectID: "p1"}, true},
+ {"other member project chat without access", assistantConversation{ID: "other-denied", UserID: other, ProjectID: "p3"}, false},
+ }
+ for _, tc := range cases {
+ t.Run(tc.name, func(t *testing.T) {
+ got := visibleAssistantConversations([]assistantConversation{tc.candidate}, me, sharedIDs, runProjects, allowedProjects)
+ if visible := len(got) == 1; visible != tc.visible {
+ t.Fatalf("visible = %v, want %v", visible, tc.visible)
+ }
+ })
+ }
+}
+
+func TestVisibleAssistantConversationsDeduplicatesKeepingFirst(t *testing.T) {
+ candidates := []assistantConversation{
+ {ID: "a", UserID: 1, Title: "first"},
+ {ID: "b", UserID: 1},
+ {ID: "a", UserID: 1, Title: "duplicate"},
+ }
+ got := visibleAssistantConversations(candidates, 1, nil, nil, nil)
+ var titles []string
+ var ids []string
+ for _, item := range got {
+ ids = append(ids, item.ID)
+ titles = append(titles, item.Title)
+ }
+ if !reflect.DeepEqual(ids, []string{"a", "b"}) || titles[0] != "first" {
+ t.Fatalf("got ids %v titles %v, want [a b] with first title kept", ids, titles)
+ }
+}
+
+func TestVisibleAssistantConversationsDoesNotResetGenerationFields(t *testing.T) {
+ candidates := []assistantConversation{{ID: "a", UserID: 1, GenerationStatus: "running", GenerationID: "g1", Unread: true}}
+ got := visibleAssistantConversations(candidates, 1, nil, nil, nil)
+ if len(got) != 1 || got[0].GenerationStatus != "running" || got[0].GenerationID != "g1" || !got[0].Unread {
+ t.Fatalf("filter must leave generation fields untouched for listSessions to reset, got %+v", got)
+ }
+}
+
+// A shared record that names no project must stay hidden on the project-ID
+// guard alone, even when its run is unbound too and the caller's access map
+// would otherwise let the empty project through.
+func TestVisibleAssistantConversationsHidesSharedRecordWithoutProject(t *testing.T) {
+ candidates := []assistantConversation{{ID: "shared-unbound", Shared: true, RunID: "run-unbound"}}
+ sharedIDs := map[string]bool{"shared-unbound": true}
+ runProjects := map[string]string{"run-unbound": ""}
+ allowedProjects := map[string]bool{"": true}
+ if got := visibleAssistantConversations(candidates, 1, sharedIDs, runProjects, allowedProjects); len(got) != 0 {
+ t.Fatalf("shared record without a project must be hidden, got %+v", got)
+ }
+}