sgm-index on a mistyped or empty folder overwrites the existing index with an empty one and exits 0

Open
#130 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

Nobody has claimed this yet.

Assessment

Difficulty
3/5
Estimated time
1-2 days
Newbie friendliness
78/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
python, sqlite

Research direction

Start with build_index and main in src/sqlite_graph_memory/index_notes.py, then read check_index in src/sqlite_graph_memory/doctor.py. Run the focused tests in tests/test_incremental_index.py and tests/test_doctor.py; they use a fake encoder. Done when missing or empty note folders preserve a valid index and exit non-zero, zero-chunk runs can be followed by a normal run, and doctor flags a zero-row index.

Written by the indexing model from the issue text.

Description

bug good first issue

What happens

If sgm-index finds no indexable notes, it still writes the index files. An existing good index is replaced by an empty one, and the exit code is 0.

Reproduction on a clone of master (0.3.9, Windows 11, Python 3.12, [lite] backend), with BRAIN_INDEX_DIR pointing at a fresh folder and the 202-note bench/vault copied to ./notes:

$ sgm-index ./notes
indexed 204 chunks from 202 files -> <index folder>
$ ls -la <index folder>
626816 _brain_e5.npy
 93762 _brain_e5_meta.pkl

$ sgm-index ./notez          # typo, folder does not exist
no indexable .md notes found under notez
$ echo $?
0
$ ls -la <index folder>
   128 _brain_e5.npy
   171 _brain_e5_manifest.json
     5 _brain_e5_meta.pkl

$ sgm-ask "ouzel"
0 hits (scope=all) -> _brain_answer.txt
$ echo $?
0

The 626 KB index is gone and has to be re-embedded from scratch. The same happens with an existing folder that contains no .md files: _brain_e5.npy goes from 626816 to 128 bytes, exit code 0.

In code: build_index() in src/sqlite_graph_memory/index_notes.py never checks that notes_dir exists (iter_notes uses rglob, which returns nothing for a missing folder), and lines 248-251 write _brain_e5.npy, the meta pickle and the manifest unconditionally. main() prints the "no indexable" line at 269-270 and returns normally.

Three follow-on symptoms on the resulting empty index:

  • sgm-doctor reports [ok] Index Index found in <folder> (0.0 MB). and ends with All good., exit code 0.
  • When the empty index comes from a first run (no previous index, so line 246 writes shape (0, 0)), sgm-ask stops with a traceback: ValueError: matmul: Input operand 1 has a mismatch in its core dimension 0 ... (size 768 is different from 0) from query_sims (brain_ask.py:261).
  • After such a first run, adding a valid note and running sgm-index ./notes again also stops with a traceback: ValueError: all the input array dimensions except for the concatenation axis must match exactly ... the array at index 0 has size 0 and the array at index 1 has size 768, raised from build_index at index_notes.py:240. sgm-index --full ./notes gets out of it.

Why it matters

One typo in the path, or an unmounted drive, silently destroys an index that can take a long time to build on a large vault. Because the exit code is 0, a scheduled re-index or an agent following the README cannot notice. After that, recall returns nothing and the health check says everything is fine.

Suggested approach

  • In main() (or at the top of build_index): if notes_dir is not an existing directory, print one line to stderr and exit 1 before touching the index folder.
  • In build_index: when the run would produce zero chunks, do not overwrite an existing non-empty index and do not write a (0, 0) array. Return the zero result and let main() exit non-zero with a message that says the existing index was kept.
  • In sgm-doctor check_index: treat an index with zero rows as a failure with the hint Rebuild it: sgm-index /path/to/your/notes (the 0-byte file case is already handled there).
  • Optional, same PR or a follow-up: make sgm-ask print one line instead of a traceback on a zero-row index.

Done when

  • sgm-index ./does-not-exist exits non-zero, prints a clear message on stderr, and leaves the files in BRAIN_INDEX_DIR byte-for-byte unchanged.
  • sgm-index on an existing folder with no .md files does not replace a non-empty index.
  • A zero-chunk first run followed by a normal run does not crash.
  • New tests in tests/test_incremental_index.py (they already inject a fake encoder, so no model is needed) cover these cases and fail on current master.
  • A test in tests/test_doctor.py asserts that a zero-row index is not reported as OK.
  • python -m pytest -q shows no new failures.

Where to start

  • src/sqlite_graph_memory/index_notes.py: build_index (ends at line 254) and main (from line 257)
  • src/sqlite_graph_memory/doctor.py: check_index (from line 121)
  • tests/test_incremental_index.py, tests/test_doctor.py

New here? Comment that you are taking it and ask anything in this thread; small PRs are reviewed within a day.

Dominant language
Python
Stars
9
Forks
6
Avg merge
13h 8m
Merged PRs (30d)
40

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from tonydzi/sqlite-graph-memory

All issues in tonydzi/sqlite-graph-memory

Similar issues

More Python issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.