From 904c0b479b60649f9f92cd8a3988da625e8ca1d8 Mon Sep 17 00:00:00 2001 From: teknium1 <127238744+teknium1@users.noreply.github.com> Date: Fri, 29 May 2026 04:51:21 -0700 Subject: [PATCH] refactor(state): return FTS index count from vacuum() Have vacuum() return optimize_fts()'s count so the CLI 'sessions optimize' summary uses the real merged-index count instead of probing the private _FTS_TABLES / _fts_table_exists() members. --- hermes_cli/main.py | 11 +++-------- hermes_state.py | 9 +++++++-- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/hermes_cli/main.py b/hermes_cli/main.py index 081f61786..904e47f8a 100644 --- a/hermes_cli/main.py +++ b/hermes_cli/main.py @@ -13576,14 +13576,9 @@ Examples: ) print("Optimizing session store (FTS merge + VACUUM)…") try: - # vacuum() merges FTS5 segments (optimize_fts) then VACUUMs. - # Probe the index count first for the summary line. - n = sum( - 1 - for t in db._FTS_TABLES - if db._fts_table_exists(t) - ) - db.vacuum() + # vacuum() merges FTS5 segments (optimize_fts) then VACUUMs, + # and returns the number of indexes it merged. + n = db.vacuum() except Exception as e: print(f"Error: optimization failed: {e}") db.close() diff --git a/hermes_state.py b/hermes_state.py index 7a03ffbdf..2b6cedeaa 100644 --- a/hermes_state.py +++ b/hermes_state.py @@ -3303,7 +3303,7 @@ class SessionDB: ) return optimized - def vacuum(self) -> None: + def vacuum(self) -> int: """Run VACUUM to reclaim disk space after large deletes. SQLite does not shrink the database file when rows are deleted — @@ -3320,11 +3320,15 @@ class SessionDB: FTS5 segments are merged first via :meth:`optimize_fts` so the subsequent VACUUM reclaims the pages freed by the merge. This is a layout-only optimization — search results are unchanged. + + Returns the number of FTS indexes that were optimized (0 if the + merge step failed or no FTS tables exist). """ # Merge FTS5 segments before VACUUM so the freed pages are returned # to the OS in the same pass. optimize_fts() manages its own lock. + optimized = 0 try: - self.optimize_fts() + optimized = self.optimize_fts() except Exception as exc: logger.warning("FTS optimize before VACUUM failed: %s", exc) # VACUUM cannot be executed inside a transaction. @@ -3335,6 +3339,7 @@ class SessionDB: except Exception: pass self._conn.execute("VACUUM") + return optimized def maybe_auto_prune_and_vacuum( self,