返回 DeepSeek-Reasonix
session_organization.go
根目录 / desktop / session_organization.go
1 package main
2
3 import (
4 "context"
5 "fmt"
6 "os"
7 "reflect"
8 "slices"
9 "strings"
10
11 "reasonix/desktop/internal/workspacestate"
12 "reasonix/internal/session"
13 )
14
15 type SessionOrganizationWorkspace struct {
16 Scope string `json:"scope"`
17 WorkspaceRoot string `json:"workspaceRoot,omitempty"`
18 HostID string `json:"hostId,omitempty"`
19 }
20 type SessionOrganizationSnapshot struct {
21 Revision uint64 `json:"revision"`
22 Applied bool `json:"applied"`
23 ManualOrderEnabled bool `json:"manualOrderEnabled"`
24 Order []string `json:"order"`
25 Groups []desktopGroup `json:"groups"`
26 }
27 type SessionOrganizationMutation struct {
28 Kind string `json:"kind"`
29 Target *SessionSelector `json:"target,omitempty"`
30 Anchor *SessionSelector `json:"anchor,omitempty"`
31 Position string `json:"position,omitempty"`
32 GroupID string `json:"groupId,omitempty"`
33 Title string `json:"title,omitempty"`
34 // SortMode is the activity order the sidebar shows while no manual order exists.
35 SortMode string `json:"sortMode,omitempty"`
36 }
37
38 func organizationSnapshot(o workspacestate.Organization, applied bool) SessionOrganizationSnapshot {
39 groups := []desktopGroup{}
40 for _, g := range o.Groups {
41 groups = append(groups, desktopGroup{ID: g.ID, Title: g.Title, SessionKeys: append([]string{}, g.Members...)})
42 }
43 return SessionOrganizationSnapshot{Revision: o.Revision, Applied: applied, ManualOrderEnabled: o.ManualOrderEnabled, Order: append([]string{}, o.Order...), Groups: groups}
44 }
45
46 // Import known sources incrementally. Imported includes explicit ungrouped
47 // choices, so later discoveries never reinstate a topic-level preference.
48 func (a *App) ensureSessionOrganization(scope, root string) (string, workspacestate.Organization, error) {
49 return a.ensureSessionOrganizationSources(scope, root, false)
50 }
51
52 func (a *App) ensureSessionOrganizationSources(scope, root string, mutation bool) (string, workspacestate.Organization, error) {
53 scope, root, err := normalizeOrganizationTarget(scope, root)
54 if err != nil {
55 return "", workspacestate.Organization{}, err
56 }
57 state, err := a.workspaceRegistry().LoadProjection(a.bootContext())
58 if err != nil {
59 return "", workspacestate.Organization{}, err
60 }
61 id, found := workspacestate.FindWorkspace(state, desktopWorkspaceRoot(scope, root))
62 if !found {
63 id, err = a.ensureDesktopWorkspace(a.bootContext(), scope, root)
64 if err != nil {
65 return "", workspacestate.Organization{}, err
66 }
67 state, err = a.workspaceRegistry().LoadProjection(a.bootContext())
68 if err != nil {
69 return "", workspacestate.Organization{}, err
70 }
71 }
72 workspace := state.Workspaces[id]
73 projects := loadProjectsFile()
74 importKey, cacheable := a.organizationImportKey(scope, root, state, id, projects)
75 if cacheable && !mutation {
76 if organization, ok := a.desktopSessions.organizations.get(importKey); ok {
77 return id, organization, nil
78 }
79 }
80 legacy := legacyOrganizationPreferences(projects, scope, root)
81 nodes := []ProjectNode{}
82 // Registry identities are already loaded; retain them for transactional
83 // fork/group attachment without consulting any session file or catalog page.
84 aliases := workspaceSourceAliases(state, id)
85 for _, sid := range workspace.SessionIDs {
86 p := state.Presentation[sid]
87 ref := session.SessionRef{HostID: localDesktopHostID, SessionID: sid}
88 nodes = append(nodes, ProjectNode{Session: &ref, TopicID: p.TopicID, SessionPath: sessionRoute(sid), IdentityAliases: aliases[sid]})
89 }
90 // Default organization has no source-dependent preferences to import.
91 // Do not enumerate every history page just to establish an empty group and
92 // automatic ordering projection on the first sidebar request.
93 if mutation || legacy.ManualSessionOrder || legacy.ManualTopicOrder || len(legacy.Groups) > 0 {
94 req := ProjectTopicPageRequest{Scope: scope, WorkspaceRoot: root, Limit: 200}
95 for {
96 page, e := a.listProjectTopics(req)
97 if e != nil {
98 return "", workspacestate.Organization{}, e
99 }
100 for _, node := range page.Items {
101 nodes = append(nodes, expandSessionSourceRows(node)...)
102 }
103 if page.NextCursor == "" {
104 break
105 }
106 if page.NextCursor == req.Cursor {
107 return "", workspacestate.Organization{}, fmt.Errorf("legacy cursor did not advance")
108 }
109 req.Cursor = page.NextCursor
110 }
111 }
112 importSources := func(o *workspacestate.Organization) error {
113 projectLegacyOrganization(o, nodes, legacy)
114 return nil
115 }
116 // A settled import is a read. Do not enter the cross-process write lock or
117 // serialize the whole registry merely to discover that nothing changed.
118 if workspace.Organization != nil {
119 candidate := workspace.Organization.Clone()
120 if err := importSources(&candidate); err != nil {
121 return "", workspacestate.Organization{}, err
122 }
123 if reflect.DeepEqual(candidate, *workspace.Organization) {
124 if cacheable {
125 a.desktopSessions.organizations.put(importKey, candidate)
126 }
127 return id, candidate, nil
128 }
129 }
130 org, _, err := a.workspaceRegistry().UpdateOrganization(a.bootContext(), id, nil, importSources)
131 return id, org, err
132 }
133
134 func legacyOrganizationPreferences(projects desktopProjectFile, scope, root string) desktopProject {
135 if scope == "project" {
136 // Most callers use the persisted spelling; avoid filesystem comparisons
137 // unless this request actually uses an alias.
138 for _, project := range projects.Projects {
139 if project.Root == root {
140 return project
141 }
142 }
143 if i := projectIndexByRoot(projects.Projects, root); i >= 0 {
144 return projects.Projects[i]
145 }
146 return desktopProject{}
147 }
148 return desktopProject{Groups: projects.GlobalGroups, SessionOrder: projects.GlobalSessionOrder,
149 Topics: projects.GlobalTopics, ManualSessionOrder: projects.GlobalManualSessionOrder, ManualTopicOrder: projects.GlobalManualTopicOrder}
150 }
151
152 // projectLegacyOrganization shares the import semantics with the writer, but
153 // changes only the caller-owned organization. Snapshot callers supply already
154 // materialized nodes and never perform a second discovery pass or persist it.
155 func projectLegacyOrganization(o *workspacestate.Organization, nodes []ProjectNode, legacy desktopProject) {
156 if o.Imported == nil {
157 o.Imported = map[string]bool{}
158 }
159 canonicalByAlias := map[string]string{}
160 for _, node := range nodes {
161 if node.Session != nil {
162 for _, alias := range node.IdentityAliases {
163 canonicalByAlias[alias] = projectNodeSessionKey(node)
164 }
165 }
166 }
167 initial := o.MigrationVersion == 0
168 manual := legacy.ManualSessionOrder || legacy.ManualTopicOrder
169 if initial {
170 o.ManualOrderEnabled = manual
171 for _, group := range legacy.Groups {
172 o.Groups = append(o.Groups, workspacestate.OrganizationGroup{ID: group.ID, Title: group.Title, Members: []string{}})
173 }
174 }
175 importOrganizationMembers(o, nodes, legacy.Groups, canonicalByAlias)
176 if initial {
177 importOrganizationOrder(o, nodes, legacy.SessionOrder, legacy.Topics, canonicalByAlias, manual)
178 }
179 o.MigrationVersion = 1
180 }
181
182 func projectedShellOrganization(workspace workspacestate.Workspace, state workspacestate.State, sources []ProjectNode, legacy desktopProject) workspacestate.Organization {
183 org := workspacestate.Organization{}
184 if workspace.Organization != nil {
185 org = workspace.Organization.Clone()
186 }
187 // Shells have no group filter. Without an old manual order there is
188 // nothing to project, so ordinary refreshes do not rebuild import members.
189 if org.MigrationVersion != 0 || !legacy.ManualSessionOrder && !legacy.ManualTopicOrder {
190 return org
191 }
192 aliases := workspaceSourceAliases(state, workspace.ID)
193 nodes := make([]ProjectNode, 0, len(workspace.SessionIDs)+len(sources))
194 for _, id := range workspace.SessionIDs {
195 ref := session.SessionRef{HostID: localDesktopHostID, SessionID: id}
196 nodes = append(nodes, ProjectNode{Session: &ref, TopicID: state.Presentation[id].TopicID, IdentityAliases: aliases[id]})
197 }
198 nodes = append(nodes, sources...)
199 projectLegacyOrganization(&org, nodes, legacy)
200 return org
201 }
202
203 func workspaceSourceAliases(state workspacestate.State, workspaceID string) map[string][]string {
204 result := map[string][]string{}
205 // A topic aliases its initial placeholder only while it has one owner.
206 // Build this once per workspace, not once per row of a large history page.
207 owners := map[string]string{}
208 for _, id := range state.Workspaces[workspaceID].SessionIDs {
209 if topic := state.Presentation[id].TopicID; topic != "" {
210 if _, seen := owners[topic]; seen {
211 owners[topic] = ""
212 } else {
213 owners[topic] = id
214 }
215 }
216 }
217 for topic, id := range owners {
218 if id != "" {
219 result[id] = append(result[id], "topic\x00"+topic)
220 }
221 }
222 for _, m := range state.SourceMappings {
223 if m.WorkspaceID != workspaceID {
224 continue
225 }
226 for _, key := range state.SourceKeys(m.SourceKey) {
227 result[m.SessionID] = append(result[m.SessionID], "source\x00local\x00"+key)
228 }
229 if sourceMappingHasPathAlias(m) {
230 // Lazy catalog pages use a path source key even for a single-head
231 // DAG. Include both displayed identities in runtime merges and
232 // lifecycle receipts, without claiming independent sibling heads.
233 result[m.SessionID] = append(result[m.SessionID], "path\x00"+m.Path,
234 "source\x00local\x00"+desktopSourceKey(m.Path, ""))
235 }
236 }
237 aliasOwners := map[string]string{}
238 for id, aliases := range result {
239 for _, alias := range aliases {
240 if owner, exists := aliasOwners[alias]; exists && owner != id {
241 aliasOwners[alias] = ""
242 } else if !exists {
243 aliasOwners[alias] = id
244 }
245 }
246 }
247 for id, aliases := range result {
248 // Old path spellings can normalize to the same source while retaining
249 // independent durable receipts. Ambiguity is not an ownership alias.
250 aliases = slices.DeleteFunc(aliases, func(alias string) bool { return aliasOwners[alias] != id })
251 slices.Sort(aliases)
252 result[id] = slices.Compact(aliases)
253 }
254 return result
255 }
256
257 func sourceAliases(state workspacestate.State, workspaceID, sessionID string) []string {
258 return append([]string{}, workspaceSourceAliases(state, workspaceID)[sessionID]...)
259 }
260
261 func (a *App) GetSessionOrganization(workspace SessionOrganizationWorkspace) (SessionOrganizationSnapshot, error) {
262 if workspace.HostID != "" && workspace.HostID != localDesktopHostID {
263 return a.remoteSessionOrganization(workspace, nil, nil)
264 }
265 _, o, err := a.ensureSessionOrganization(workspace.Scope, workspace.WorkspaceRoot)
266 return organizationSnapshot(o, err == nil), err
267 }
268
269 func (a *App) UpdateSessionOrganization(workspace SessionOrganizationWorkspace, expectedRevision uint64, mutation SessionOrganizationMutation) (SessionOrganizationSnapshot, error) {
270 if workspace.HostID != "" && workspace.HostID != localDesktopHostID {
271 return a.remoteSessionOrganization(workspace, &expectedRevision, &mutation)
272 }
273 // Group edits address either a group or one explicit source. Only moving
274 // in a manual order needs the complete relative order of other sources.
275 // Old source-dependent preferences still take their normal import path.
276 id, current, err := a.ensureSessionOrganizationSources(workspace.Scope, workspace.WorkspaceRoot, mutation.Kind == "move")
277 if err != nil {
278 return SessionOrganizationSnapshot{}, err
279 }
280 var shown []string
281 if mutation.Kind == "move" && !current.ManualOrderEnabled {
282 if shown, err = a.shownSessionOrder(workspace.Scope, workspace.WorkspaceRoot, mutation.SortMode); err != nil {
283 return SessionOrganizationSnapshot{}, err
284 }
285 }
286 resolved := []SessionTarget{}
287 resolve := func(selector *SessionSelector) (string, error) {
288 if selector == nil {
289 return "", newSessionOperationError("target_not_found", "Select a session.")
290 }
291 target, e := a.resolveSessionTarget(*selector)
292 if e != nil {
293 return "", e
294 }
295 targetWorkspaceID, e := a.resolveDesktopWorkspaceID(a.bootContext(), target.Scope, target.WorkspaceRoot)
296 if e != nil {
297 return "", e
298 }
299 if targetWorkspaceID != id {
300 return "", newSessionOperationError("target_changed", "The session moved to another workspace.")
301 }
302 var ref *session.SessionRef
303 if target.SessionRef.SessionID != "" {
304 ref = &target.SessionRef
305 } else {
306 if _, err := os.Stat(target.SessionPath); err != nil {
307 return "", newSessionOperationError("target_not_found", "The historical source is unavailable.")
308 }
309 // Both path-only compatibility selectors and explicit source refs
310 // must name the same physical member used by the sidebar.
311 head := ""
312 if target.Source != nil {
313 head = target.Source.HeadID
314 } else if selector.Source != nil {
315 head = selector.Source.HeadID
316 }
317 target.Source = &SessionSourceRef{HostID: localDesktopHostID, Path: target.SessionPath,
318 HeadID: head, SourceKey: desktopSourceKey(target.SessionPath, head)}
319 }
320 resolved = append(resolved, target)
321 return projectNodeSessionKey(ProjectNode{Session: ref, SessionPath: target.SessionPath, Source: target.Source}), nil
322 }
323 key, anchor := "", ""
324 if mutation.Kind == "move" || mutation.Kind == "set-group" {
325 key, err = resolve(mutation.Target)
326 if err != nil {
327 return SessionOrganizationSnapshot{}, err
328 }
329 }
330 if mutation.Kind == "move" {
331 anchor, err = resolve(mutation.Anchor)
332 if err != nil {
333 return SessionOrganizationSnapshot{}, err
334 }
335 }
336 o, applied, err := a.workspaceRegistry().UpdateOrganizationWithState(a.bootContext(), id, &expectedRevision, func(state *workspacestate.State, o *workspacestate.Organization) error {
337 if mutation.Kind == "move" && !o.ManualOrderEnabled {
338 seedManualOrder(o, shown)
339 }
340 return applyResolvedOrganizationMutation(state, id, o, resolved, mutation, key, anchor)
341 })
342 if err == nil && applied {
343 a.emitProjectTreeMetadataChanged()
344 }
345 return organizationSnapshot(o, applied), err
346 }
347
348 // shownSessionOrder lists a workspace in the activity order the sidebar is
349 // showing, so the first manual move starts from what the user was looking at.
350 func (a *App) shownSessionOrder(scope, root, sortMode string) ([]string, error) {
351 keys := []string{}
352 req := ProjectTopicPageRequest{Scope: scope, WorkspaceRoot: root, Limit: 200, SortMode: sortMode}
353 for {
354 page, err := a.unifiedProjectTopics(req)
355 a.ReleaseReadSnapshot(page.SnapshotID)
356 if err != nil {
357 return nil, err
358 }
359 for _, node := range page.Items {
360 for _, row := range expandSessionSourceRows(node) {
361 keys = append(keys, projectNodeSessionKey(row))
362 }
363 }
364 if page.NextCursor == "" {
365 return keys, nil
366 }
367 if page.NextCursor == req.Cursor {
368 return nil, fmt.Errorf("session order cursor did not advance")
369 }
370 req.Cursor = page.NextCursor
371 }
372 }
373
374 // seedManualOrder replaces the attach order, which is oldest first and never
375 // what the sidebar showed, with the shown order. Unlisted keys keep their place
376 // after the listed ones.
377 func seedManualOrder(o *workspacestate.Organization, shown []string) {
378 rank := make(map[string]int, len(shown))
379 for i, key := range shown {
380 if _, ok := rank[key]; !ok {
381 rank[key] = i
382 }
383 }
384 slices.SortStableFunc(o.Order, func(left, right string) int {
385 l, lok := rank[left]
386 r, rok := rank[right]
387 switch {
388 case lok && rok:
389 return l - r
390 case lok:
391 return -1
392 case rok:
393 return 1
394 }
395 return 0
396 })
397 }
398
399 // Run inside the registry transaction: resolution precedes the write lock, so
400 // source adoption and lifecycle changes must be checked again at commit time.
401 func applyResolvedOrganizationMutation(state *workspacestate.State, workspaceID string, o *workspacestate.Organization, resolved []SessionTarget, mutation SessionOrganizationMutation, key, anchor string) error {
402 for _, target := range resolved {
403 if target.SessionRef.SessionID == "" {
404 if target.Source == nil || target.Source.SourceKey == "" {
405 return workspacestate.ErrMutationConflict
406 }
407 if _, adopted, err := state.ResolveSource(target.Source.SourceKey); adopted || err != nil {
408 return workspacestate.ErrMutationConflict
409 }
410 continue
411 }
412 current := state.SessionStates[target.SessionRef.SessionID]
413 if current.Lifecycle != workspacestate.Active || current.Generation != target.LifecycleGeneration || !slices.Contains(state.Workspaces[workspaceID].SessionIDs, target.SessionRef.SessionID) {
414 return workspacestate.ErrMutationConflict
415 }
416 }
417 if mutation.Kind == "set-group" && !o.Imported[key] {
418 // Imported also records explicit ungrouping so later preference
419 // discovery cannot restore an older assignment.
420 o.Imported[key] = true
421 if !slices.Contains(o.Order, key) {
422 o.Order = append(o.Order, key)
423 }
424 }
425 return applyOrganizationMutation(o, mutation, key, anchor)
426 }
427
428 // replaceSessionOrganizationGroups retains old RPC signatures while moving their
429 // persistence into the same transaction as ordering and lifecycle mutations.
430 func (a *App) replaceSessionOrganizationGroups(ctx context.Context, scope, root string, revision *uint64, groups []desktopGroup) (ProjectGroupsSnapshot, error) {
431 id, _, err := a.ensureSessionOrganizationSources(scope, root, true)
432 if err != nil {
433 return ProjectGroupsSnapshot{}, err
434 }
435 if err = validateSessionGroups(groups); err != nil {
436 return ProjectGroupsSnapshot{}, err
437 }
438 state, err := a.workspaceRegistry().LoadProjection(ctx)
439 if err != nil {
440 return ProjectGroupsSnapshot{}, err
441 }
442 legacy, err := a.unadoptedLegacyTopics(ProjectTopicPageRequest{Scope: scope, WorkspaceRoot: root, Limit: 200}, map[string]bool{}, map[string]bool{})
443 if err != nil {
444 return ProjectGroupsSnapshot{}, err
445 }
446 nodes := append([]ProjectNode{}, legacy.Items...)
447 for _, sid := range state.Workspaces[id].SessionIDs {
448 if state.SessionStates[sid].Lifecycle != workspacestate.Active {
449 continue
450 }
451 ref := session.SessionRef{HostID: localDesktopHostID, SessionID: sid}
452 nodes = append(nodes, ProjectNode{Session: &ref, TopicID: state.Presentation[sid].TopicID})
453 }
454 o, applied, err := a.workspaceRegistry().UpdateOrganization(ctx, id, revision, func(o *workspacestate.Organization) error {
455 next := []workspacestate.OrganizationGroup{}
456 for _, g := range groups {
457 members := []string{}
458 for _, node := range nodes {
459 key := projectNodeSessionKey(node)
460 if o.Imported[key] && desktopGroupContainsNode(g, node) && !slices.Contains(members, key) {
461 members = append(members, key)
462 }
463 }
464 for _, key := range g.SessionKeys {
465 if !o.Imported[key] {
466 return workspacestate.ErrMutationConflict
467 }
468 if !slices.Contains(members, key) {
469 members = append(members, key)
470 }
471 }
472 group := workspacestate.OrganizationGroup{ID: g.ID, Title: g.Title, Members: members}
473 for _, existing := range o.Groups {
474 if existing.ID == g.ID {
475 group = existing
476 group.Title, group.Members = g.Title, members
477 break
478 }
479 }
480 next = append(next, group)
481 }
482 o.Groups = next
483 return nil
484 })
485 if err == nil && applied {
486 a.emitProjectTreeMetadataChanged()
487 }
488 s := organizationSnapshot(o, applied)
489 return ProjectGroupsSnapshot{Groups: s.Groups, Revision: s.Revision, Applied: applied}, err
490 }
491
492 func applyOrganizationMutation(o *workspacestate.Organization, mutation SessionOrganizationMutation, key, anchor string) error {
493 switch mutation.Kind {
494 case "reset-order":
495 // Keep the recorded order and groups; time sorting ignores ranks while disabled.
496 o.ManualOrderEnabled = false
497 case "move":
498 if key == anchor {
499 return nil
500 }
501 if !o.Imported[key] || !o.Imported[anchor] {
502 return workspacestate.ErrMutationConflict
503 }
504 if mutation.Position != "before" && mutation.Position != "after" {
505 return fmt.Errorf("invalid position")
506 }
507 o.Order = slices.DeleteFunc(o.Order, func(v string) bool { return v == key })
508 i := slices.Index(o.Order, anchor)
509 if i < 0 {
510 return workspacestate.ErrMutationConflict
511 }
512 if mutation.Position == "after" {
513 i++
514 }
515 o.Order = slices.Insert(o.Order, i, key)
516 o.ManualOrderEnabled = true
517 case "set-group":
518 if !o.Imported[key] {
519 return workspacestate.ErrMutationConflict
520 }
521 found := mutation.GroupID == ""
522 for _, g := range o.Groups {
523 found = found || g.ID == mutation.GroupID
524 }
525 if !found {
526 return workspacestate.ErrMutationConflict
527 }
528 for i := range o.Groups {
529 o.Groups[i].Members = slices.DeleteFunc(o.Groups[i].Members, func(v string) bool { return v == key })
530 if o.Groups[i].ID == mutation.GroupID {
531 o.Groups[i].Members = append(o.Groups[i].Members, key)
532 }
533 }
534 case "create-group":
535 if len(o.Groups) >= maxSessionGroups {
536 return fmt.Errorf("group limit exceeded")
537 }
538 if err := validateSessionGroups([]desktopGroup{{ID: mutation.GroupID, Title: mutation.Title}}); err != nil {
539 return err
540 }
541 if strings.TrimSpace(mutation.GroupID) == "" || strings.TrimSpace(mutation.Title) == "" {
542 return fmt.Errorf("group id and title required")
543 }
544 for _, g := range o.Groups {
545 if g.ID == mutation.GroupID {
546 return workspacestate.ErrMutationConflict
547 }
548 }
549 o.Groups = append(o.Groups, workspacestate.OrganizationGroup{ID: mutation.GroupID, Title: strings.TrimSpace(mutation.Title), Members: []string{}})
550 case "rename-group", "delete-group":
551 if mutation.Kind == "rename-group" {
552 if err := validateSessionGroups([]desktopGroup{{ID: mutation.GroupID, Title: mutation.Title}}); err != nil {
553 return err
554 }
555 }
556 index := slices.IndexFunc(o.Groups, func(g workspacestate.OrganizationGroup) bool { return g.ID == mutation.GroupID })
557 if index < 0 {
558 return workspacestate.ErrMutationConflict
559 }
560 if mutation.Kind == "delete-group" {
561 o.Groups = slices.Delete(o.Groups, index, index+1)
562 } else {
563 if strings.TrimSpace(mutation.Title) == "" {
564 return fmt.Errorf("title required")
565 }
566 o.Groups[index].Title = strings.TrimSpace(mutation.Title)
567 }
568 default:
569 return fmt.Errorf("unsupported organization mutation")
570 }
571 return nil
572 }
573
574 func importOrganizationMembers(o *workspacestate.Organization, nodes []ProjectNode, groups []desktopGroup, canonicalByAlias map[string]string) {
575 ordered := make(map[string]bool, len(o.Order)+len(nodes))
576 for _, key := range o.Order {
577 ordered[key] = true
578 }
579 for _, n := range nodes {
580 if n.Session == nil && n.SessionPath == "" {
581 continue
582 }
583 key := projectNodeSessionKey(n)
584 if _, adopted := canonicalByAlias[key]; adopted {
585 continue
586 }
587 if o.Imported[key] {
588 continue
589 }
590 for _, old := range groups {
591 included := desktopGroupContainsNode(old, n)
592 for _, alias := range n.IdentityAliases {
593 if slices.Contains(old.ExcludedSessionKeys, alias) {
594 included = false
595 break
596 }
597 if slices.Contains(old.SessionKeys, alias) {
598 included = true
599 }
600 }
601 if included {
602 for i := range o.Groups {
603 if o.Groups[i].ID == old.ID {
604 o.Groups[i].Members = append(o.Groups[i].Members, key)
605 break
606 }
607 }
608 }
609 }
610 if !ordered[key] {
611 o.Order = append(o.Order, key)
612 ordered[key] = true
613 }
614 o.Imported[key] = true
615 }
616 }
617
618 func importOrganizationOrder(o *workspacestate.Organization, nodes []ProjectNode, order, topicOrder []string, canonicalByAlias map[string]string, manual bool) {
619 if manual && len(order) == 0 {
620 for _, topic := range topicOrder {
621 for _, node := range nodes {
622 if node.TopicID == topic {
623 order = append(order, projectNodeSessionKey(node))
624 }
625 }
626 }
627 }
628 if len(order) > 0 {
629 next := []string{}
630 for _, key := range order {
631 if canonical, ok := canonicalByAlias[key]; ok {
632 key = canonical
633 }
634 if o.Imported[key] && !slices.Contains(next, key) {
635 next = append(next, key)
636 }
637 }
638 for _, key := range o.Order {
639 if !slices.Contains(next, key) {
640 next = append(next, key)
641 }
642 }
643 o.Order = next
644 }
645 }
646
646 lines GO