The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(sessions): optimize-storage sizes chunks by rows, stops on a dead chunk, paces under --force
Tekniumead7bedca6dd
diff --git a/hermes_cli/sessions_cmd.py b/hermes_cli/sessions_cmd.pyindex 8f5e6b9ea47..81fc9b9efe0 100644--- a/hermes_cli/sessions_cmd.py+++ b/hermes_cli/sessions_cmd.py@@ -999,7 +999,7 @@ class _OptimizeProgress:table), space-padded over the previous text (``ESC[K`` leaks under prompt_toolkit)."""def __init__(self):- self._key, self._last, self._width = None, None, 0+ self._key, self._last, self._line = None, None, ""def __call__(self, info) -> None:phase = info.get("phase")@@ -1014,16 +1014,24 @@ class _OptimizeProgress:self._draw(_optimize_progress_line(info))def _draw(self, line: str) -> None:- print(f"\r{line}{' ' * max(0, self._width - len(line))}", end="", flush=True)- self._width = len(line)+ if line == self._line: # a table's final chunk, its DROP and the switch all report 100%+ return+ print(f"\r{line}{' ' * max(0, len(self._line) - len(line))}", end="", flush=True)+ self._line = linedef close(self, completed: bool = False) -> None:last = self._lastif completed and last and last.get("phase") in ("backfill", "teardown"):self._draw(_optimize_progress_line({**last, "percent": 100, "indexed": last.get("total", 0)}))- if self._width:+ if self._line:print(flush=True)- self._last, self._width = None, 0+ self._last, self._line = None, ""+++def _store_has_foreign_holder(db_path) -> bool:+ from hermes_state_holders import held_store_refusal++ return held_store_refusal(db_path, command="optimize-storage") is not Nonedef _cmd_optimize_storage(db, args):@@ -1060,10 +1068,19 @@ def _cmd_optimize_storage(db, args):if resp not in ("y", "yes"):print("Cancelled.")return+ # The holder check was skipped under --force: if someone really holds the store, keep the old+ # duty-cycle pause so their writes are not starved (slower, but they stay responsive).+ yield_to_writers = bool(getattr(args, "force", False)) and _store_has_foreign_holder(db_path)+ if yield_to_writers:+ print(" Another process is using the database (--force): pacing the rebuild so it stays responsive.")progress = _OptimizeProgress()print("Optimizing search-index storage…")try:- result = db.optimize_fts_storage(progress_cb=progress, vacuum=do_vacuum)+ result = db.optimize_fts_storage(progress_cb=progress, vacuum=do_vacuum, yield_to_writers=yield_to_writers)+ except KeyboardInterrupt:+ progress.close()+ print("Interrupted — no data was lost; re-run to resume.")+ return 130except Exception as e:progress.close()print(f"Error: optimization failed: {e}\nNo data was lost. Re-run to resume.")diff --git a/hermes_cli/subcommands/sessions.py b/hermes_cli/subcommands/sessions.pyindex b4b9d026783..4dc9388e1d9 100644--- a/hermes_cli/subcommands/sessions.py+++ b/hermes_cli/subcommands/sessions.py@@ -180,7 +180,7 @@ def build_sessions_parser(subparsers, *, cmd_sessions: Callable) -> None:_flag(sessions_optimize_storage, "--yes", "-y", default=False,help="Skip the disk-space confirmation prompt")_flag(sessions_optimize_storage, "--force",- help="Run even while another Hermes process (gateway, Desktop, dashboard, cron) holds state.db — rewriting the store under a live writer can leave every agent refusing turns until all writers are stopped")+ help="Run even while another Hermes process (gateway, Desktop, dashboard, cron) holds state.db — rewriting the store under a live writer can leave every agent refusing turns until all writers are stopped; the rebuild then paces itself so that process stays responsive (much slower)")sessions_repair = sessions_subparsers.add_parser("repair", help="Repair a malformed state.db schema so hidden sessions reappear",diff --git a/hermes_state_fts.py b/hermes_state_fts.pyindex 26309ad4d52..00b51a86696 100644--- a/hermes_state_fts.py+++ b/hermes_state_fts.py@@ -542,18 +542,24 @@ class SessionFtsSetupMixin:# foreground `hermes sessions optimize-storage`, which refuses while any other# process holds state.db (held_store_refusal, #110054), so there is no writer# to yield to; a fixed 0.2 s pause after each few-ms chunk was >95% of the- # wall time. Chunk size adapts toward TARGET seconds, since rows cost ~1000x- # more in trigram_data than in docsize.- _FTS_REBUILD_CHUNK_ROWS = 500 # first chunk; later ones adapt+ # wall time. Chunk size (in ROWS, never an id span: a pruned store has id gaps)+ # adapts toward TARGET seconds, since rows cost ~1000x more in trigram_data+ # than in docsize. `--force` past a live holder (yield_to_writers) keeps the+ # duty cycle instead: chunks near PACED_TARGET, a pause of max(MIN_PAUSE,+ # cost x DUTY_FACTOR) after each, so that holder's writes are not starved.+ _FTS_REBUILD_CHUNK_ROWS = 500 # first chunk of every phase and trash table; later ones adapt_FTS_REBUILD_CHUNK_MIN_ROWS = 50- _FTS_REBUILD_CHUNK_MAX_ROWS = 1_000_000+ _FTS_REBUILD_CHUNK_MAX_ROWS = 100_000_FTS_REBUILD_CHUNK_TARGET_S = 0.25- _fts_chunk_rows = _FTS_REBUILD_CHUNK_ROWS # rows per step; optimize's _drive adapts it per instance-- def _next_chunk_rows(self, rows: int, cost_s: float) -> int:- """Rows for the next chunk so it costs about ``_FTS_REBUILD_CHUNK_TARGET_S``; at most- doubles or halves per step so one noisy chunk (a cold page, a checkpoint) can't swing it."""- scale = self._FTS_REBUILD_CHUNK_TARGET_S / max(cost_s, 1e-4)+ _FTS_REBUILD_PACED_TARGET_S = 0.05+ _FTS_REBUILD_DUTY_FACTOR = 4.0 # paced: sleep >= 4x chunk cost (≤20% duty)+ _FTS_REBUILD_MIN_PAUSE = 0.2 # paced: seconds — floor between chunks+ _fts_chunk_rows = _FTS_REBUILD_CHUNK_ROWS # rows per step; optimize's drive loop adapts it per instance++ def _next_chunk_rows(self, rows: int, cost_s: float, target_s: float) -> int:+ """Rows for the next chunk so it costs about *target_s*; at most doubles or halves per+ step so one noisy chunk (a cold page, a checkpoint) can't swing it."""+ scale = target_s / max(cost_s, 1e-4)rows = int(rows * min(2.0, max(0.5, scale)))return min(self._FTS_REBUILD_CHUNK_MAX_ROWS, max(self._FTS_REBUILD_CHUNK_MIN_ROWS, rows))diff --git a/hermes_state_search.py b/hermes_state_search.pyindex e73875e4cd3..2493d38a2ac 100644--- a/hermes_state_search.py+++ b/hermes_state_search.py@@ -5,11 +5,13 @@ Must never import hermes_state (cycle); shared constants live in hermes_state_co"""import contextlib+import contextvarsimport loggingimport reimport sqlite3import threadingimport time+from dataclasses import dataclass, fieldfrom typing import Any, Callable, Collection, Dict, List, Optional, Tuplefrom agent.skill_commands import describe_skill_invocation@@ -26,6 +28,40 @@ from hermes_state_fts import fts_layout_current, fts_layout_driftlogger = logging.getLogger("hermes_state")+@dataclass+class _TeardownProgress:+ """Which demoted shadow table optimize-storage is draining and how far (``progress_cb``+ payload). Owned and kept current by :meth:`SessionSearchMixin._fts_teardown_trash_step`."""++ table: str = ""+ table_index: int = 0+ table_count: int = 0+ total: int = 0+ drained: int = 0++ def info(self) -> dict[str, Any]:+ done, total = self.drained, self.total+ return {"phase": "teardown", "percent": min(100, 100 * done // total) if total else 100,+ "indexed": done, "total": total, "table": self.table,+ "table_index": self.table_index, "table_count": self.table_count}+++@dataclass+class _VacuumProgress:+ """VACUUM is one statement: liveness is elapsed time against the size before and the estimated+ size after (live pages only), never a percent."""++ size_bytes: int+ estimated_bytes: int+ started: float = field(default_factory=time.monotonic)++ def info(self) -> dict[str, Any]:+ return {"phase": "vacuum", "percent": 0, "indexed": 0, "total": 0,+ "elapsed": time.monotonic() - self.started, "size_bytes": self.size_bytes,+ "estimated_bytes": self.estimated_bytes}+++def _search_slow_ms() -> float:"""``sessions.search_slow_ms`` for the served profile (default 1000; 0 logs every call)."""value = routed_sessions_setting("search_slow_ms", "HERMES_SEARCH_SLOW_MS")@@ -317,27 +353,25 @@ class SessionSearchMixin:self._execute_write(_do)def fts_rebuild_step(self) -> bool:- """Backfill one chunk (``_fts_chunk_rows`` ids) of the deferred FTS rebuild; True while work- remains. Chunks are claimed atomically inside the write transaction, so concurrent processes- interleave instead of duplicating rows."""+ """Backfill one chunk (``_fts_chunk_rows`` messages) of the deferred FTS rebuild; True while+ work remains. Chunks are claimed atomically inside the write transaction, so concurrent+ processes interleave instead of duplicating rows. A failed chunk raises (nothing committed;+ the caller decides whether to retry)."""if not self._fts_enabled:return Falseinserts = [self._CHUNK_INSERT_SQL.format(table="messages_fts", extra="")]if self._trigram_available:inserts.append(self._TRIGRAM_CHUNK_INSERT_SQL)- return self._rebuild_step("fts_rebuild", inserts, fail_msg="FTS rebuild chunk failed (will retry): %s",- finish=self._fts_rebuild_finish, finish_when_empty=True)+ return self._rebuild_step("fts_rebuild", inserts, finish=self._fts_rebuild_finish, finish_when_empty=True)def fts_cjk_rebuild_step(self) -> bool:"""Backfill one chunk of the CJK index. True while work remains."""if not self._fts_enabled or not self._fts_cjk_loaded:return Falseinsert = self._CHUNK_INSERT_SQL.format(table="messages_fts_cjk", extra=" AND role <> 'tool'")- return self._rebuild_step("fts_cjk_rebuild", [insert], finish=self._fts_cjk_rebuild_finish,- fail_msg="CJK FTS rebuild chunk failed (will retry): %s")+ return self._rebuild_step("fts_cjk_rebuild", [insert], finish=self._fts_cjk_rebuild_finish)- def _rebuild_step(self, prefix: str, insert_sqls: list[str], *, fail_msg: str, finish,- finish_when_empty: bool = False) -> bool:+ def _rebuild_step(self, prefix: str, insert_sqls: list[str], *, finish, finish_when_empty: bool = False) -> bool:"""Shared chunk engine for the base and CJK deferred backfills. ``finish_when_empty``finalizes a high_water <= 0 marker (empty messages table) instead of leaving it pending."""high_water_raw = self.get_meta(f"{prefix}_high_water")@@ -355,19 +389,18 @@ class SessionSearchMixin:progress = int(row[0])if progress >= high_water:return False- # Upper bound is an id, not a row count, so deleted-row gaps don't shrink chunks.- upper = min(progress + chunk, high_water)+ # The window is the next `chunk` messages, not `chunk` ids: an id gap (pruned+ # sessions) neither shrinks a chunk nor lets the next one span a million dense ids.+ nth = conn.execute("SELECT id FROM messages WHERE id > ? AND id <= ? ORDER BY id LIMIT 1 OFFSET ?",+ (progress, high_water, chunk - 1)).fetchone()+ upper = int(nth[0]) if nth is not None else high_waterfor sql in insert_sqls:conn.execute(sql, (progress, upper))# Progress lands in the same transaction as its rows (crash-atomic).conn.execute("UPDATE state_meta SET value = ? WHERE key = ?", (str(upper), f"{prefix}_progress"))return upper < high_water- try:- more = self._execute_write(_do)- except sqlite3.OperationalError as exc:- logger.debug(fail_msg, exc)- return True # transient (lock contention) — caller retries+ more = self._execute_write(_do)if more is False:status = self._rebuild_status(prefix)if (finish_when_empty and high_water <= 0) or (@@ -377,10 +410,13 @@ class SessionSearchMixin:return Falsereturn bool(more)- def _fts_teardown_trash_step(self, progress: Optional[dict] = None) -> bool:+ def _fts_teardown_trash_step(self, progress: Optional[_TeardownProgress] = None) -> bool:"""Tear down one chunk (``_fts_chunk_rows`` rows) of a demoted v22 FTS shadow table (a PLAIN- table now); True while work remains. ``progress``, when given, is kept current as {"table", "table_index",- "table_count", "drained", "total"} for the table being drained.+ table now); True while work remains. A failed chunk raises (nothing committed).++ With *progress* (optimize-storage), the first step on each trash table deletes nothing: it+ points *progress* at the table and restarts the chunk size (rows cost ~1000x more in one+ shadow table than in another); later steps add the rows they deleted.Single-column INTEGER-key trash tables (the common shape — FTS shadow tables carry a rowid/integerPK) are drained with a high-water marker mirroring :meth:`fts_rebuild_step`: each chunk deletes only@@ -394,6 +430,9 @@ class SessionSearchMixin:"SELECT name FROM sqlite_master WHERE type = 'table' AND name LIKE ? ESCAPE '\\'",(self._FTS_TRASH_PREFIX.replace("_", "\\_") + "%",),).fetchall()]+ if trash and progress is not None and progress.table != trash[0]:+ self._fts_teardown_start_table(conn, trash, progress)+ return Trueif not trash:return Falsetbl = trash[0]@@ -434,34 +473,24 @@ class SessionSearchMixin:logger.info("Old FTS shadow table %s torn down.", tbl)return True- try:- more, deleted = self._execute_write(_do)- except sqlite3.OperationalError as exc:- logger.debug("FTS trash teardown chunk failed (will retry): %s", exc)- return True+ more, deleted = self._execute_write(_do)if progress is not None:# Counted after the commit: _execute_write may replay _do on contention.- progress["drained"] = progress.get("drained", 0) + deleted+ progress.drained += deletedreturn bool(more)- def _fts_teardown_track(self, progress: dict) -> None:- """Point *progress* at the trash table the next teardown chunk drains: its position in the run- and the rows left in it. One COUNT per table, on the reader (no writer waits on it) and outside- the timed chunk (so it never skews the chunk sizing)."""- with self._read_ctx() as conn:- trash = [r[0] for r in conn.execute(- "SELECT name FROM sqlite_master WHERE type = 'table' AND name LIKE ? ESCAPE '\\'",- (self._FTS_TRASH_PREFIX.replace("_", "\\_") + "%",),- ).fetchall()]- if not trash or progress.get("table") == trash[0]:- return- tbl = trash[0]- # Only INTEGER-pk tables (rowid aliases) carry a resume marker; count what is left past it.- marker = _meta_row(conn, f"fts_teardown_{tbl}_progress")- total = conn.execute(f"SELECT COUNT(*) FROM {tbl} WHERE rowid > ?", (int(marker[0]),)).fetchone()[0] \- if marker is not None else conn.execute(f"SELECT COUNT(*) FROM {tbl}").fetchone()[0]- count = progress.setdefault("table_count", len(trash))- progress.update(table=tbl, table_index=max(1, count - len(trash) + 1), drained=0, total=int(total))+ def _fts_teardown_start_table(self, conn, trash: list[str], progress: _TeardownProgress) -> None:+ """Point *progress* at ``trash[0]`` (its place in the run, the rows left past its resume+ marker) and restart the chunk size. One COUNT per table, on the pooled reader *conn*."""+ tbl = trash[0]+ # Only INTEGER-pk tables (rowid aliases) carry a resume marker; count what is left past it.+ marker = _meta_row(conn, f"fts_teardown_{tbl}_progress")+ where, params = ("WHERE rowid > ?", (int(marker[0]),)) if marker is not None else ("", ())+ progress.table_count = progress.table_count or len(trash)+ progress.table, progress.table_index = tbl, max(1, progress.table_count - len(trash) + 1)+ progress.total = int(conn.execute(f"SELECT COUNT(*) FROM {tbl} {where}", params).fetchone()[0])+ progress.drained = 0+ self._fts_chunk_rows = self._FTS_REBUILD_CHUNK_ROWSdef _fts_cjk_reset_if_stale(self) -> None:"""From-scratch rebuild of a stale cjk index (triggers were dropped, gap extent unknown):@@ -631,43 +660,64 @@ class SessionSearchMixin:raise sqlite3.OperationalError(failure_message)self._conn.commit()- def _optimize_progress_info(self, phase: str, track: dict) -> dict[str, Any]:- """``progress_cb`` payload: {"phase", "percent", "indexed", "total"} for every phase, plus- {"table", "table_index", "table_count"} while tearing down and {"elapsed", "size_bytes",- "estimated_bytes"} for the VACUUM (one statement: elapsed time, not a percent)."""- if phase == "teardown" and track.get("table"):- done, total = track["drained"], track["total"]- return {"phase": phase, "percent": min(100, 100 * done // total) if total else 100, "indexed": done,- "total": total, **{k: track[k] for k in ("table", "table_index", "table_count")}}- if phase == "vacuum" and "vacuum_started" in track:- return {"phase": phase, "percent": 0, "indexed": 0, "total": 0,- "elapsed": time.monotonic() - track["vacuum_started"],- **{k: track[k] for k in ("size_bytes", "estimated_bytes")}}+ def _backfill_progress_info(self, phase: str = "backfill") -> dict[str, Any]:st = self.fts_rebuild_status() or self.fts_cjk_rebuild_status()return {"phase": phase, "percent": st["percent"] if st else 100,"indexed": st["indexed"] if st else 0, "total": st["total"] if st else 0}- def _optimize_vacuum(self, track: dict, tick: Optional[Callable[[], None]] = None) -> bool:+ # A chunk that fails this many times in a row ends the run (each lock failure already waited out+ # _execute_write's patience); a full disk or I/O error never heals by retrying.+ _OPTIMIZE_CHUNK_ATTEMPTS = 3+ _VACUUM_TICK_S = 1.0++ def _optimize_drive(self, step: Callable[[], bool], emit: Callable[[], None], *, paced: bool) -> None:+ """Run *step* to completion, emitting after every chunk. Back to back (no other writer: see+ the chunk engine note in hermes_state_fts) unless *paced*, which keeps the duty cycle for a+ holder ``--force`` ran past. Chunk size adapts to its measured cost; a step may restart it+ (teardown on each new table)."""+ target = self._FTS_REBUILD_PACED_TARGET_S if paced else self._FTS_REBUILD_CHUNK_TARGET_S+ self._fts_chunk_rows = self._FTS_REBUILD_CHUNK_ROWS+ failures = 0+ while True:+ t0 = time.monotonic()+ try:+ more = step()+ except sqlite3.OperationalError as exc:+ failures += 1+ if failures >= self._OPTIMIZE_CHUNK_ATTEMPTS:+ raise+ logger.warning("FTS optimize chunk failed (attempt %d, retrying): %s", failures, exc)+ time.sleep(0.25 * failures)+ continue+ failures = 0+ if not more:+ return+ cost = time.monotonic() - t0+ emit()+ self._fts_chunk_rows = self._next_chunk_rows(self._fts_chunk_rows, cost, target)+ if paced:+ time.sleep(max(self._FTS_REBUILD_MIN_PAUSE, cost * self._FTS_REBUILD_DUTY_FACTOR))++ def _optimize_vacuum(self, tick: Optional[Callable[[dict[str, Any]], None]] = None) -> bool:"""Phase 3: reclaim freed pages to the OS. False when VACUUM failed (usually no free diskfor its temp copy; a later VACUUM reclaims). VACUUM is one statement that runs for minutes- on a big store, so *tick* is called every second from a side thread to show it is alive;- *track* gets the size before and the estimated size after (live pages only)."""- with self._lock:- page_size, pages, free = (self._conn.execute(f"PRAGMA {p}").fetchone()[0]- for p in ("page_size", "page_count", "freelist_count"))- track.update(vacuum_started=time.monotonic(), size_bytes=pages * page_size,- estimated_bytes=(pages - free) * page_size)- stop = threading.Event()-- def _tick() -> None:- tick()- while not stop.wait(1.0):- tick()-- from agent.memory_provider import spawn_context_thread-- ticker = spawn_context_thread(_tick, name="optimize-vacuum-ticker")+ on a big store, so *tick* gets a :class:`_VacuumProgress` payload at once and every+ ``_VACUUM_TICK_S`` from a side thread, which is stopped and joined before this returns."""+ ticker, stop = None, threading.Event()if tick is not None:+ with self._read_ctx() as conn:+ page_size, pages, free = (conn.execute(f"PRAGMA {p}").fetchone()[0]+ for p in ("page_size", "page_count", "freelist_count"))+ state = _VacuumProgress(size_bytes=pages * page_size, estimated_bytes=(pages - free) * page_size)++ def _tick() -> None:+ tick(state.info())+ while not stop.wait(self._VACUUM_TICK_S):+ tick(state.info())++ # copy_context().run: the ticker keeps the caller's profile scope (HX012).+ ticker = threading.Thread(target=contextvars.copy_context().run, args=(_tick,),+ name="optimize-vacuum-ticker", daemon=True)ticker.start()try:with self._lock:@@ -678,7 +728,7 @@ class SessionSearchMixin:vacuum_ok = Falsefinally:stop.set()- if ticker.is_alive():+ if ticker is not None:ticker.join()# Best-effort WAL fold-back, REFUSED (SQLITE_BUSY) while another connection holds a# read-mark (callers size via logical_size_bytes, not stat()). PASSIVE, never TRUNCATE:@@ -718,13 +768,16 @@ class SessionSearchMixin:return Nonedef optimize_fts_storage(- self, *, progress_cb: Optional[Callable[[dict[str, Any]], None]] = None, vacuum: bool = True+ self, *, progress_cb: Optional[Callable[[dict[str, Any]], None]] = None, vacuum: bool = True,+ yield_to_writers: bool = False,) -> dict[str, Any]:"""Repair an older FTS layout into the current v23 shape, foreground and to completion:legacy-v22 inline -> external-content, or a v23 ``messages_fts_trigram`` that still stores- ``tool_calls``. Re-running resumes. ``progress_cb`` receives :meth:`_optimize_progress_info`- after every chunk and every second of the VACUUM. A missing trigram tokenizer is not fatal- (CJK falls back to LIKE)."""+ ``tool_calls``. Re-running resumes; a chunk that keeps failing raises (nothing is lost).+ ``progress_cb`` receives {"phase", "percent", "indexed", "total"} after every chunk (plus+ :meth:`_TeardownProgress.info` / :meth:`_VacuumProgress.info` fields) and every second of the+ VACUUM. ``yield_to_writers`` (``--force`` past a live holder) paces chunks so its writes get+ through. A missing trigram tokenizer is not fatal (CJK falls back to LIKE)."""if not self._fts_enabled:return {"ok": False, "reason": "fts5_unavailable"}if self.read_only:@@ -748,35 +801,15 @@ class SessionSearchMixin:if self._fts_cjk_loaded:self._ensure_cjk_schema_committed()- track: dict[str, Any] = {} # teardown table/rows and VACUUM sizes, for progress_cb-- def _emit(phase: str) -> None:- if progress_cb is not None:- progress_cb(self._optimize_progress_info(phase, track))-- def _drive(phase: str, step, before=None, **step_kwargs) -> None:- """Run *step* to completion, back to back (no other writer: see the chunk engine- note in hermes_state_fts). Chunk size adapts to its measured cost."""- self._fts_chunk_rows = self._FTS_REBUILD_CHUNK_ROWS- while True:- if before is not None:- before()- _t0 = time.monotonic()- if not step(**step_kwargs):- break- cost = time.monotonic() - _t0- _emit(phase)- self._fts_chunk_rows = self._next_chunk_rows(self._fts_chunk_rows, cost)-+ emit = progress_cb or (lambda _info: None)+ teardown = _TeardownProgress()# Phase 1: base backfill; 1b: CJK-bigram backfill (own marker pair).- _emit("backfill")- _drive("backfill", self.fts_rebuild_step)- _drive("backfill", self.fts_cjk_rebuild_step)+ emit(self._backfill_progress_info())+ for step in (self.fts_rebuild_step, self.fts_cjk_rebuild_step):+ self._optimize_drive(step, lambda: emit(self._backfill_progress_info()), paced=yield_to_writers)# Phase 2: tear down the demoted legacy shadow tables in chunks.- self._fts_teardown_track(track)- _emit("teardown")- _drive("teardown", self._fts_teardown_trash_step, before=lambda: self._fts_teardown_track(track),- progress=track)+ self._optimize_drive(lambda: self._fts_teardown_trash_step(teardown), lambda: emit(teardown.info()),+ paced=yield_to_writers)with self._read_ctx() as conn:still_pending = _meta_row(conn, "fts_rebuild_high_water") is not Nonestill_trash = self._has_fts_trash(conn)@@ -789,13 +822,13 @@ class SessionSearchMixin:vacuum_ok = Noneif vacuum:- vacuum_ok = self._optimize_vacuum(track, None if progress_cb is None else lambda: _emit("vacuum"))+ vacuum_ok = self._optimize_vacuum(progress_cb)refusal = self._execute_write(self._optimize_settle)if refusal is not None:# A concurrent process changed state since the pre-vacuum check; a re-run can still settle.logger.warning("FTS storage optimization settle refused (%s)", refusal)return {"ok": False, "reason": refusal, "vacuumed": vacuum_ok}- _emit("done")+ emit(self._backfill_progress_info("done"))logger.info("FTS storage optimization complete (layout v%d).", FTS_STORAGE_VERSION)return {"ok": True, "vacuumed": vacuum_ok}diff --git a/tests/hermes_state/test_fts_optimize_progress.py b/tests/hermes_state/test_fts_optimize_progress.pyindex a66a79cfcc7..33137e7db8f 100644--- a/tests/hermes_state/test_fts_optimize_progress.py+++ b/tests/hermes_state/test_fts_optimize_progress.py@@ -1,7 +1,10 @@"""`hermes sessions optimize-storage` runs in the foreground behind the held-store refusal, so it must-report real progress in every phase and never idle between chunks."""+report real progress in every phase, never idle between chunks, and end (not spin) when a chunk+cannot succeed."""+import itertoolsimport sqlite3+import threadingimport timeimport pytest@@ -9,15 +12,18 @@ import pytestfrom hermes_state import SessionDBfrom hermes_state_common import LEGACY_FTS_SQL, SCHEMA_SQL+GAP_AFTER, GAP_IDS = 600, 100_000 # a pruned store: ids jump after the first 600 messages+def _legacy_inline_store(db_path, n=1200):+ ids = [i + 1 + (GAP_IDS if i >= GAP_AFTER else 0) for i in range(n)]raw = sqlite3.connect(str(db_path))try:raw.executescript(SCHEMA_SQL + LEGACY_FTS_SQL)raw.execute("INSERT INTO sessions (id, source, started_at) VALUES ('s1', 'cli', ?)", (time.time(),))raw.executemany(- "INSERT INTO messages (session_id, timestamp, role, content) VALUES ('s1', ?, 'user', ?)",- [(time.time(), f"legacy payload {i} zebra quokka") for i in range(n)],+ "INSERT INTO messages (id, session_id, timestamp, role, content) VALUES (?, 's1', ?, 'user', ?)",+ [(mid, time.time(), f"legacy payload {mid} zebra quokka") for mid in ids],)raw.commit()finally:@@ -33,22 +39,85 @@ def legacy_db(tmp_path):db.close()-def test_teardown_reports_which_table_and_how_many_rows(legacy_db):- events = []- result = legacy_db.optimize_fts_storage(progress_cb=events.append, vacuum=False)- assert result["ok"] is True[email protected]("vacuum_fails", [False, True], ids=["vacuum-ok", "vacuum-fails"])+def test_progress_reports_teardown_rows_and_vacuum_liveness(legacy_db, monkeypatch, vacuum_fails):+ monkeypatch.setattr(SessionDB, "_VACUUM_TICK_S", 0.01)+ events, in_vacuum, ticked_in_vacuum = [], threading.Event(), threading.Event()++ def progress(info):+ events.append(info)+ if info["phase"] == "vacuum" and in_vacuum.is_set():+ ticked_in_vacuum.set()++ # Hold the VACUUM statement (SQLite's own progress hook) until a tick arrives while it runs.+ vacuum_started = []+ legacy_db._conn.set_trace_callback(lambda sql: vacuum_started.append(sql) if sql.strip() == "VACUUM" else None)++ def hold_vacuum():+ if not vacuum_started or in_vacuum.is_set():+ return 0+ in_vacuum.set()+ ticked_in_vacuum.wait(10)+ return 1 if vacuum_fails else 0 # non-zero interrupts the VACUUM++ legacy_db._conn.set_progress_handler(hold_vacuum, 100)+ result = legacy_db.optimize_fts_storage(progress_cb=progress, vacuum=True)++ assert result == {"ok": True, "vacuumed": not vacuum_fails}+ assert ticked_in_vacuum.is_set()+ # The ticker is stopped and joined on success and on failure: nothing can report after return.+ assert not [t for t in threading.enumerate() if t.name == "optimize-vacuum-ticker"]+ assert events[-1]["phase"] == "done"+ vacuum = [e for e in events if e["phase"] == "vacuum"]+ assert vacuum[0]["size_bytes"] >= vacuum[0]["estimated_bytes"] > 0teardown = [e for e in events if e["phase"] == "teardown"]# The docsize shadow table holds one row per message: its drain must be visible as rows, not a bare label.- docsize = [e for e in teardown if e.get("table", "").endswith("messages_fts_docsize")]- assert docsize and docsize[0]["total"] == 1200+ docsize = [e for e in teardown if e["table"].endswith("messages_fts_docsize")]+ assert docsize[0]["total"] == 1200 and docsize[0]["indexed"] == 0assert docsize[-1]["indexed"] == 1200 and docsize[-1]["percent"] == 100count = teardown[0]["table_count"]assert count >= 2 and {e["table_index"] for e in teardown} <= set(range(1, count + 1))assert legacy_db.search_messages("quokka")-def test_foreground_optimize_never_sleeps_between_chunks(legacy_db, monkeypatch):+def test_foreground_optimize_never_sleeps_and_chunks_by_rows_across_id_gaps(legacy_db, monkeypatch):slept = []monkeypatch.setattr(time, "sleep", slept.append)+ monkeypatch.setattr(SessionDB, "_FTS_REBUILD_CHUNK_MIN_ROWS", 500)+ monkeypatch.setattr(SessionDB, "_FTS_REBUILD_CHUNK_MAX_ROWS", 500)+ markers, real_step = [0], legacy_db.fts_rebuild_step++ def observed_step():+ more = real_step()+ status = legacy_db.fts_rebuild_status()+ markers.append(status["indexed"] if status else GAP_IDS + 1200)+ return more++ legacy_db.fts_rebuild_step = observed_stepassert legacy_db.optimize_fts_storage(vacuum=False)["ok"] is Trueassert slept == []+ with legacy_db._read_ctx() as conn:+ windows = [conn.execute("SELECT COUNT(*) FROM messages WHERE id > ? AND id <= ?", (lo, hi)).fetchone()[0]+ for lo, hi in itertools.pairwise(markers)]+ # 500-row chunks: the 100k-id gap costs no extra chunk and never widens one past 500 messages.+ assert windows == [500, 500, 200]+++def test_chunk_that_keeps_failing_ends_the_run_and_a_rerun_resumes(legacy_db, monkeypatch):+ monkeypatch.setattr(time, "sleep", lambda _s: None) # the retry backoff+ calls, real_step = [], legacy_db.fts_rebuild_step++ def disk_fills_during_backfill():+ if not calls:+ pages = legacy_db._conn.execute("PRAGMA page_count").fetchone()[0]+ legacy_db._conn.execute(f"PRAGMA max_page_count = {pages + 5}")+ calls.append(1)+ assert len(calls) < 50, "optimize kept retrying a chunk that can never succeed"+ return real_step()++ legacy_db.fts_rebuild_step = disk_fills_during_backfill+ with pytest.raises(sqlite3.OperationalError, match="full"):+ legacy_db.optimize_fts_storage(vacuum=False)+ legacy_db._conn.execute("PRAGMA max_page_count = 1073741823")+ assert legacy_db.optimize_fts_storage(vacuum=False)["ok"] is True+ assert len(legacy_db.search_messages("quokka", limit=2000)) == 1200