Skip to content

[BUG-07] _registry_exists() ignores SQLite — spurious auto-setup after migrate #35

Description

@Wolfvin

Summary

_registry_exists(workspace) only checks for .codelens/backend.json or .codelens/frontend.json. A workspace that ran codelens migrate (JSON -> SQLite) and then deleted the JSON files will trigger _auto_setup on every registry-dependent command, even though .codelens/codelens.db is fully populated.

Evidence

scripts/codelens.py:234-242:

def _registry_exists(workspace: str) -> bool:
    codelens_dir = os.path.join(workspace, ".codelens")
    if not os.path.isdir(codelens_dir):
        return False
    backend_json = os.path.join(codelens_dir, "backend.json")
    frontend_json = os.path.join(codelens_dir, "frontend.json")
    return os.path.exists(backend_json) or os.path.exists(frontend_json)

No check for codelens.db, despite the migrate command (and PersistentRegistry) being first-class since v8.x.

Impact

  1. Performance regression post-migrate: Every query, trace, impact, context, etc. invocation on a migrated workspace re-runs init + scan --max-files 3000 from scratch, defeating the purpose of migrate.
  2. Stale data shadowing: The auto-scan rebuilds JSON files alongside the SQLite db, leaving the workspace in an inconsistent state where JSON and SQLite can drift.
  3. User-visible noise: Users see the "Auto-running init + scan..." message on every command, with no obvious way to suppress it.

Repro

WS=/tmp/demo
python3 scripts/codelens.py init $WS
python3 scripts/codelens.py scan $WS
python3 scripts/codelens.py migrate $WS
rm $WS/.codelens/backend.json $WS/.codelens/frontend.json
# Now: SQLite db is fully populated, but JSON files are gone.
python3 scripts/codelens.py query "main" $WS
# Observe: "Auto-running init + scan..." prints, even though codelens.db has the data.

Suggested fix

Extend _registry_exists to also accept a populated SQLite db as evidence of an existing registry:

def _registry_exists(workspace: str) -> bool:
    codelens_dir = os.path.join(workspace, ".codelens")
    if not os.path.isdir(codelens_dir):
        return False
    backend_json = os.path.join(codelens_dir, "backend.json")
    frontend_json = os.path.join(codelens_dir, "frontend.json")
    if os.path.exists(backend_json) or os.path.exists(frontend_json):
        return True
    # Also accept a populated SQLite db (post-migrate workspaces)
    db_path = os.path.join(codelens_dir, "codelens.db")
    if os.path.exists(db_path):
        try:
            from persistent_registry import db_exists
            return db_exists(workspace)
        except Exception:
            return True  # db file exists; assume populated
    return False

Add a regression test that migrates, deletes JSON, and asserts _registry_exists returns True.

Files

  • scripts/codelens.py (_registry_exists, L234-242)
  • tests/test_cli.py (new test)

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions