返回 DeepSeek-Reasonix
session_catalog_lifecycle.go
根目录 / desktop / session_catalog_lifecycle.go
1 package main
2
3 import (
4 "context"
5 "errors"
6 "log/slog"
7 "os"
8 "strings"
9
10 "reasonix/internal/history"
11 "reasonix/internal/projectiondb"
12 "reasonix/internal/sessioncatalog"
13 "reasonix/internal/taskcatalog"
14 )
15
16 func (a *App) runSessionCatalog(ctx context.Context, initialReconcileDone chan struct{}, metadataRequests <-chan struct{}) {
17 initialReconcileFinished := false
18 defer func() {
19 if !initialReconcileFinished {
20 close(initialReconcileDone)
21 }
22 }()
23 path := sessioncatalog.DefaultPath()
24 freshGeneration := false
25 if strings.TrimSpace(path) != "" {
26 _, statErr := os.Stat(path)
27 freshGeneration = errors.Is(statErr, os.ErrNotExist)
28 }
29 targets := a.sessionCatalogTargets()
30 history.RegisterCatalogRoots(historyCatalogRoots(targets))
31 projects := loadProjectsFile()
32 taskcatalog.RegisterSharedProject(globalWorkspaceRoot(), projects.GlobalTitle)
33 for _, project := range projects.Projects {
34 taskcatalog.RegisterSharedProject(project.Root, projectDisplayName(project))
35 }
36 var revisionFloor uint64
37 deferredIntegrity := true
38 for ctx.Err() == nil {
39 catalog, err := sessioncatalog.Open(ctx, sessioncatalog.Options{
40 Path: path, MetadataOnly: true, StartPaused: true,
41 DeferredMetadataIntegrity: deferredIntegrity, RevisionFloor: revisionFloor,
42 Maintenance: &a.historyMaintenance,
43 OnDiscovery: func(event sessioncatalog.DiscoveryEvent) {
44 slog.Info("desktop: history discovery", "root", event.Root, "sequence", event.Sequence,
45 "phase", event.Phase, "origin", event.Origin, "failure", event.Failure)
46 if event.Phase == "completed" {
47 a.requestHistoricalLegacyReconciliation(ctx)
48 go a.proveCatalogRecoveryLineage(ctx)
49 }
50 },
51 OnRevision: func(revision uint64, roots []string, reason string) {
52 a.emitProjectTreeChangedV2(revision, roots, reason)
53 },
54 })
55 if err != nil {
56 if deferredIntegrity && projectiondb.IsCorruptionError(err) {
57 deferredIntegrity = false
58 continue
59 }
60 slog.Warn("desktop: open session catalog", "err", err)
61 return
62 }
63 // Pair publication with stopSessionCatalog's lifecycle lock. A stopped
64 // owner cannot publish a replacement after shutdown removed its pointer.
65 a.catalogLifecycleMu.Lock()
66 stopped := ctx.Err() != nil || a.shuttingDown.Load()
67 if !stopped {
68 a.sessionCatalog.Store(catalog)
69 }
70 a.catalogLifecycleMu.Unlock()
71 if stopped {
72 _ = catalog.Close(context.Background())
73 return
74 }
75 if freshGeneration {
76 catalog.MarkRepairReason("generation_upgrade")
77 }
78 a.watchSessionCatalog(ctx, catalog, metadataRequests, func() {
79 if !initialReconcileFinished {
80 close(initialReconcileDone)
81 initialReconcileFinished = true
82 }
83 })
84 select {
85 case <-catalog.Invalidated():
86 // Invalidate the published owner before closing every read lease.
87 // Only then may the normal validating open quarantine the database.
88 a.sessionCatalog.CompareAndSwap(catalog, nil)
89 if err := catalog.Close(context.Background()); err != nil {
90 slog.Warn("desktop: close invalid catalog", "err", err)
91 return
92 }
93 revisionFloor = catalog.Status().Revision + 1
94 deferredIntegrity, freshGeneration = false, true
95 default:
96 return
97 }
98 }
99 }
100
101 // proveCatalogRecoveryLineage runs after discovery has listed every root: the
102 // metadata scan lists recovery copies as ordinary conversations because it never
103 // reads content, and this is the step that hides the ones a parent covers.
104 func (a *App) proveCatalogRecoveryLineage(ctx context.Context) {
105 catalog := a.sessionCatalog.Load()
106 if catalog == nil || ctx.Err() != nil || a.shuttingDown.Load() {
107 return
108 }
109 if err := catalog.ProveRecoveryLineage(ctx); err != nil && !errors.Is(err, context.Canceled) {
110 slog.Warn("desktop: recovery lineage proof deferred", "err", err)
111 }
112 }
113
113 lines GO