Skip to content

Commit bf36a17

Browse files
committed
feat: enhance community handling - add GraphFingerprint method and skip finalization if unchanged; update min community size default
1 parent 9c01fc3 commit bf36a17

4 files changed

Lines changed: 35 additions & 1 deletion

File tree

internal/config/config.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ func Load(cfgFile string) (*Config, error) {
8181
v.SetDefault("indexing.extract_graph", true)
8282
v.SetDefault("indexing.extract_claims", true)
8383
v.SetDefault("indexing.max_gleanings", 1)
84-
v.SetDefault("community.min_community_size", 3)
84+
v.SetDefault("community.min_community_size", 2)
8585
v.SetDefault("community.max_levels", 3)
8686
v.SetDefault("server.host", "127.0.0.1")
8787
v.SetDefault("server.port", 8080)

internal/pipeline/pipeline.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,12 @@ func (p *Pipeline) IndexURL(ctx context.Context, rootURL string, opts IndexOptio
181181

182182
// indexFile processes a single file through Phases 1-2.
183183
func (p *Pipeline) indexFile(ctx context.Context, path string, opts IndexOptions) error {
184+
absPath, err := filepath.Abs(path)
185+
if err != nil {
186+
return fmt.Errorf("resolve absolute path: %w", err)
187+
}
188+
path = absPath
189+
184190
data, err := os.ReadFile(path)
185191
if err != nil {
186192
return err
@@ -550,6 +556,17 @@ func (p *Pipeline) Finalize(ctx context.Context, verbose bool) error {
550556
if err != nil {
551557
return fmt.Errorf("load relationships: %w", err)
552558
}
559+
560+
// Check if finalization can be skipped: communities already exist and
561+
// entity/relationship counts haven't changed since last run.
562+
existingEntities, existingRels, existingComms, fpErr := p.store.GraphFingerprint(ctx)
563+
if fpErr == nil && existingComms > 0 &&
564+
existingEntities == len(entities) && existingRels == len(rels) {
565+
slog.Info("⏭️ skipping finalization — graph unchanged since last run",
566+
"entities", len(entities), "relationships", len(rels), "communities", existingComms)
567+
return nil
568+
}
569+
553570
slog.Info("🧩 Phase 3: running Louvain community detection",
554571
"entities", len(entities), "relationships", len(rels))
555572

@@ -640,6 +657,12 @@ func (p *Pipeline) Finalize(ctx context.Context, verbose bool) error {
640657
}
641658
}
642659

660+
if len(workItems) == 0 {
661+
slog.Warn("⚠️ all communities filtered out by min_community_size",
662+
"min_community_size", p.cfg.Community.MinCommunitySize,
663+
"hint", "lower community.min_community_size in config or index more documents")
664+
return nil
665+
}
643666
slog.Info("🧩 Phase 4: summarising communities", "communities", len(workItems))
644667

645668
type commResult struct {

internal/store/store.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -778,6 +778,17 @@ func (s *Store) ClearCommunities(ctx context.Context) error {
778778
return err
779779
}
780780

781+
// GraphFingerprint returns counts of entities, relationships, and communities
782+
// to detect whether the graph has changed since last finalization.
783+
func (s *Store) GraphFingerprint(ctx context.Context) (entities, relationships, communities int, err error) {
784+
err = s.db.QueryRowContext(ctx, `
785+
SELECT
786+
(SELECT COUNT(*) FROM entities),
787+
(SELECT COUNT(*) FROM relationships),
788+
(SELECT COUNT(*) FROM communities)`).Scan(&entities, &relationships, &communities)
789+
return
790+
}
791+
781792
func scanCommunity(row *sql.Row) (*Community, error) {
782793
var c Community
783794
var blob []byte

ui/dist/.gitkeep

Whitespace-only changes.

0 commit comments

Comments
 (0)