|
| 1 | +// @index Existing-graph file-state loading and change/force detection. |
| 2 | +package service |
| 3 | + |
| 4 | +import ( |
| 5 | + "context" |
| 6 | + |
| 7 | + "gorm.io/gorm" |
| 8 | + |
| 9 | + "github.com/tae2089/code-context-graph/internal/analysis/incremental" |
| 10 | + "github.com/tae2089/code-context-graph/internal/ctxns" |
| 11 | + "github.com/tae2089/code-context-graph/internal/model" |
| 12 | + "github.com/tae2089/code-context-graph/internal/pathutil" |
| 13 | +) |
| 14 | + |
| 15 | +// ExistingGraphFiles returns namespace-scoped graph file paths currently stored. |
| 16 | +// @intent share deletion-scope discovery across CLI and MCP incremental updates |
| 17 | +func ExistingGraphFiles(ctx context.Context, db *gorm.DB) ([]string, error) { |
| 18 | + filePaths, _, err := existingGraphFileState(ctx, db) |
| 19 | + return filePaths, err |
| 20 | +} |
| 21 | + |
| 22 | +// existingGraphFileState loads the namespace-scoped current graph state grouped by file path. |
| 23 | +// @intent provide both deletion-scope file paths and per-file node projections from a single query. |
| 24 | +func existingGraphFileState(ctx context.Context, db *gorm.DB) ([]string, map[string][]model.Node, error) { |
| 25 | + if db == nil { |
| 26 | + return nil, map[string][]model.Node{}, nil |
| 27 | + } |
| 28 | + |
| 29 | + ns := ctxns.FromContext(ctx) |
| 30 | + var nodes []graphFileNodeState |
| 31 | + if err := db.WithContext(ctx). |
| 32 | + Model(&model.Node{}). |
| 33 | + Select("id", "file_path", "hash"). |
| 34 | + Where("namespace = ? AND kind <> ?", ns, model.NodeKindPackage). |
| 35 | + Find(&nodes).Error; err != nil { |
| 36 | + return nil, nil, err |
| 37 | + } |
| 38 | + nodesByFile := make(map[string][]model.Node) |
| 39 | + fileSeen := make(map[string]struct{}) |
| 40 | + filePaths := make([]string, 0) |
| 41 | + for _, node := range nodes { |
| 42 | + minimalNode := model.Node{ID: node.ID, FilePath: node.FilePath, Hash: node.Hash} |
| 43 | + nodesByFile[node.FilePath] = append(nodesByFile[node.FilePath], minimalNode) |
| 44 | + if _, ok := fileSeen[node.FilePath]; !ok { |
| 45 | + fileSeen[node.FilePath] = struct{}{} |
| 46 | + filePaths = append(filePaths, node.FilePath) |
| 47 | + } |
| 48 | + } |
| 49 | + return filePaths, nodesByFile, nil |
| 50 | +} |
| 51 | + |
| 52 | +// filterExistingStateByInclude restricts existing graph state to file paths that match the include filter. |
| 53 | +// @intent prevent partial-scope updates from deleting files that live outside the requested include paths. |
| 54 | +func filterExistingStateByInclude(filePaths []string, nodesByFile map[string][]model.Node, includePaths []string) ([]string, map[string][]model.Node) { |
| 55 | + filteredFiles := make([]string, 0, len(filePaths)) |
| 56 | + filteredNodes := make(map[string][]model.Node) |
| 57 | + for _, fp := range filePaths { |
| 58 | + if !pathutil.MatchIncludePaths(fp, includePaths) { |
| 59 | + continue |
| 60 | + } |
| 61 | + filteredFiles = append(filteredFiles, fp) |
| 62 | + filteredNodes[fp] = nodesByFile[fp] |
| 63 | + } |
| 64 | + return filteredFiles, filteredNodes |
| 65 | +} |
| 66 | + |
| 67 | +// forceReparseFiles finds files whose edges reference nodes from changed files and therefore must be reparsed. |
| 68 | +// @intent keep cross-file edges consistent by reparsing edge-source files when their referenced nodes change. |
| 69 | +func forceReparseFiles(ctx context.Context, db *gorm.DB, existingNodesByFile map[string][]model.Node, currentHashes map[string]string) (map[string]struct{}, error) { |
| 70 | + forceFiles := make(map[string]struct{}) |
| 71 | + if db == nil || len(existingNodesByFile) == 0 || len(currentHashes) == 0 { |
| 72 | + return forceFiles, nil |
| 73 | + } |
| 74 | + if !db.Migrator().HasTable(&model.Edge{}) { |
| 75 | + return forceFiles, nil |
| 76 | + } |
| 77 | + |
| 78 | + var changedNodeIDs []uint |
| 79 | + for filePath, nodes := range existingNodesByFile { |
| 80 | + if len(nodes) == 0 { |
| 81 | + continue |
| 82 | + } |
| 83 | + currentHash, stillPresent := currentHashes[filePath] |
| 84 | + if !stillPresent || nodes[0].Hash != currentHash { |
| 85 | + for _, node := range nodes { |
| 86 | + changedNodeIDs = append(changedNodeIDs, node.ID) |
| 87 | + } |
| 88 | + } |
| 89 | + } |
| 90 | + if len(changedNodeIDs) == 0 { |
| 91 | + return forceFiles, nil |
| 92 | + } |
| 93 | + |
| 94 | + ns := ctxns.FromContext(ctx) |
| 95 | + edgeFileSeen := make(map[string]struct{}) |
| 96 | + for start := 0; start < len(changedNodeIDs); start += forceReparseEdgeChunkSize { |
| 97 | + end := min(start+forceReparseEdgeChunkSize, len(changedNodeIDs)) |
| 98 | + chunk := changedNodeIDs[start:end] |
| 99 | + var chunkFiles []string |
| 100 | + if err := db.WithContext(ctx). |
| 101 | + Model(&model.Edge{}). |
| 102 | + Where("namespace = ? AND file_path <> '' AND (from_node_id IN ? OR to_node_id IN ?)", ns, chunk, chunk). |
| 103 | + Distinct(). |
| 104 | + Pluck("file_path", &chunkFiles).Error; err != nil { |
| 105 | + return nil, err |
| 106 | + } |
| 107 | + for _, filePath := range chunkFiles { |
| 108 | + edgeFileSeen[filePath] = struct{}{} |
| 109 | + } |
| 110 | + |
| 111 | + var relatedImplements []model.Edge |
| 112 | + if err := db.WithContext(ctx). |
| 113 | + Model(&model.Edge{}). |
| 114 | + Where("namespace = ? AND kind = ? AND file_path <> '' AND (from_node_id IN ? OR to_node_id IN ?)", ns, model.EdgeKindImplements, chunk, chunk). |
| 115 | + Find(&relatedImplements).Error; err != nil { |
| 116 | + return nil, err |
| 117 | + } |
| 118 | + var relatedTypeIDs []uint |
| 119 | + seenTypeID := make(map[uint]struct{}, len(relatedImplements)*2) |
| 120 | + for _, edge := range relatedImplements { |
| 121 | + for _, id := range []uint{edge.FromNodeID, edge.ToNodeID} { |
| 122 | + if id == 0 { |
| 123 | + continue |
| 124 | + } |
| 125 | + if _, ok := seenTypeID[id]; ok { |
| 126 | + continue |
| 127 | + } |
| 128 | + seenTypeID[id] = struct{}{} |
| 129 | + relatedTypeIDs = append(relatedTypeIDs, id) |
| 130 | + } |
| 131 | + } |
| 132 | + for relatedStart := 0; relatedStart < len(relatedTypeIDs); relatedStart += forceReparseEdgeChunkSize { |
| 133 | + relatedEnd := min(relatedStart+forceReparseEdgeChunkSize, len(relatedTypeIDs)) |
| 134 | + relatedChunk := relatedTypeIDs[relatedStart:relatedEnd] |
| 135 | + var dispatchFiles []string |
| 136 | + if err := db.WithContext(ctx). |
| 137 | + Model(&model.Edge{}). |
| 138 | + Where("namespace = ? AND kind IN ? AND file_path <> '' AND (from_node_id IN ? OR to_node_id IN ?)", ns, model.CallEdgeKinds(), relatedChunk, relatedChunk). |
| 139 | + Distinct(). |
| 140 | + Pluck("file_path", &dispatchFiles).Error; err != nil { |
| 141 | + return nil, err |
| 142 | + } |
| 143 | + for _, filePath := range dispatchFiles { |
| 144 | + edgeFileSeen[filePath] = struct{}{} |
| 145 | + } |
| 146 | + } |
| 147 | + } |
| 148 | + for filePath := range edgeFileSeen { |
| 149 | + currentHash, stillPresent := currentHashes[filePath] |
| 150 | + if !stillPresent { |
| 151 | + continue |
| 152 | + } |
| 153 | + nodes := existingNodesByFile[filePath] |
| 154 | + if len(nodes) == 0 || nodes[0].Hash != currentHash { |
| 155 | + continue |
| 156 | + } |
| 157 | + forceFiles[filePath] = struct{}{} |
| 158 | + } |
| 159 | + return forceFiles, nil |
| 160 | +} |
| 161 | + |
| 162 | +// splitForcedFiles partitions inputs into normal and forced-reparse buckets and marks the forced ones. |
| 163 | +// @intent process unchanged-hash forced files separately so the syncer can bypass its hash short-circuit. |
| 164 | +func splitForcedFiles(files map[string]incremental.FileInfo, forceFiles map[string]struct{}) (map[string]incremental.FileInfo, map[string]incremental.FileInfo) { |
| 165 | + if len(files) == 0 { |
| 166 | + return nil, nil |
| 167 | + } |
| 168 | + if len(forceFiles) == 0 { |
| 169 | + return files, nil |
| 170 | + } |
| 171 | + normal := make(map[string]incremental.FileInfo, len(files)) |
| 172 | + forced := make(map[string]incremental.FileInfo) |
| 173 | + for filePath, info := range files { |
| 174 | + if _, ok := forceFiles[filePath]; ok { |
| 175 | + info.Force = true |
| 176 | + forced[filePath] = info |
| 177 | + continue |
| 178 | + } |
| 179 | + normal[filePath] = info |
| 180 | + } |
| 181 | + return normal, forced |
| 182 | +} |
0 commit comments