Skip to content

Named context "Repos Linked: 0" desync: index_helper early-return skips register_repo_in_context for already-populated graphs #1317

Closed

Description

@apklein

Summary

In named context mode, a context's repos: list in config.yaml can permanently desync from the actual graph: cgc context list shows "Repos Linked: 0" even though the Neo4j graph is fully populated (Repository node + hundreds of thousands of nodes) and cgc find / cgc query / the MCP tools all resolve correctly against it.

The root cause is an ordering bug in index_helper (src/codegraphcontext/cli/cli_helpers.py): the "already indexed, skipping" early-return sits above the register_repo_in_context(...) call. Once a context's graph has been populated by any path that did not register the repo into config.yaml (e.g. the MCP add_code_to_graph tool, or a CLI run interrupted/killed mid-resolution), every subsequent cgc index . --context <ctx> short-circuits at the skip guard before reaching the registration line — so the two stores can never reconcile through the normal command path. It is self-perpetuating.

Environment

  • CodeGraphContext: 0.5.1 (pipx)
  • Neo4j: 2026.05.0 (Homebrew), bolt://localhost:7687
  • OS: macOS
  • Mode: named (single context, set as default_context)

Reproduction

  1. Populate a named context's graph via a path that does not register the repo in config.yaml — e.g. the MCP add_code_to_graph tool, or start cgc index . --context X and kill it during the CALLS-resolution phase (graph nodes are written incrementally; the repo registration is not reached).
  2. Confirm the graph is real:
    cgc query "MATCH (n) RETURN count(n)"                 # -> hundreds of thousands
    cgc query "MATCH (r:Repository) RETURN r.path, r.name" # -> your repo
    
  3. Observe the desync:
    cgc context list      # -> "Repos Linked: 0"
    cat ~/.codegraphcontext/config.yaml   # -> contexts.X.repos: []
    
  4. Try to repair via the normal command:
    cgc index . --context X
    # -> "Repository '.' is already indexed with N files. Skipping."
    
    It returns before registering the repo, so repos: stays [] forever.

Root cause

In cli/cli_helpers.py, index_helper (around the lines below in 0.5.1):

# repo already in graph WITH files -> early return
if file_count > 0:
    console.print(f"...already indexed with {file_count} files. Skipping.")
    console.print("...Use 'cgc index --force' to re-index")
    db_manager.close_driver()
    return                                   # <-- returns here (~line 300)

# ...registration happens AFTER, so it is never reached on the skip path:
if context and ctx.mode == "named":
    if not register_repo_in_context(context, str(path_obj), auto_create=False):  # ~line 306-309
        db_manager.close_driver()
        raise typer.Exit(code=1)

register_repo_in_context (in cli/config_manager.py) is the only writer of ctx.repos. Because the early return is above it, a context whose graph was populated without registration can never be repaired by cgc index — the skip guard fires first.

This is closely related to #701 (cgc index . says repo "exists but has no files"), which lives in the same repo-state-detection block (MATCH (r:Repository)-[:CONTAINS*]->(f:File) file-count check).

Impact

  • The graph works fine, so it's not data loss — but cgc context list misreports Repos Linked: 0, which reads as "nothing is indexed" and is alarming/confusing.
  • Any feature that scopes off ctx.repos (e.g. watch, re-index targeting) sees an empty list for a fully-indexed context.

Suggested fix

Register the repo before the "already indexed, skip" early-return (registration is idempotent), or register inside the skip branch before returning. Minimal change — move the if context and ctx.mode == "named": register_repo_in_context(...) block above the file_count > 0 early return, so a known/indexed repo still gets linked into the named context's repos: list.

Workaround

Call the idempotent registration writer directly (no re-index needed):

cd "$(pipx environment --value PIPX_LOCAL_VENVS)/codegraphcontext"  # or your venv
./bin/python -c "from codegraphcontext.cli.config_manager import register_repo_in_context as r; r('X', '/abs/path/to/repo')"
cgc context list   # -> Repos Linked: 1

(cgc index --force also works but triggers a full re-index, which is expensive on large repos — see the separate CALLS-resolution perf work in #1304.)

Activity

  1. github-actions commented on Jun 25, 2026

    @github-actions
    Contributor

    Hi! 👋 Join our CodeGraphContext Discord channel to collaborate: https://discord.gg/dR4QY32uYQ

  2. aishwaryagangaraj-web commented on Jun 27, 2026

    @aishwaryagangaraj-web
    Contributor

    Hi @apklein,

    Thank you for the detailed investigation and root-cause analysis.

    I would like to work on this issue. From the description, it appears that the repository registration logic is skipped when the existing-index early return is triggered, causing the named context configuration and graph state to become permanently out of sync.

    I can investigate the indexing flow, implement a fix to ensure repository registration remains consistent for already-indexed repositories, and add regression tests to prevent future desynchronization issues.

    Could you please assign this issue to me?

    Thank you!

  3. apklein commented on Jun 27, 2026

    @apklein
    Author

    How?

    @> Hi @apklein,

    Thank you for the detailed investigation and root-cause analysis.

    I would like to work on this issue. From the description, it appears that the repository registration logic is skipped when the existing-index early return is triggered, causing the named context configuration and graph state to become permanently out of sync.

    I can investigate the indexing flow, implement a fix to ensure repository registration remains consistent for already-indexed repositories, and add regression tests to prevent future desynchronization issues.

    Could you please assign this issue to me?

    Thank you!

  4. moved this from Backlog tasks to In progress in CGC Progress Boardon Sep 2, 2026
  5. moved this from In progress to Done in CGC Progress Boardon Sep 2, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

No labels
No labels

Type

No type

Projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions