From 78895382359fc39479dcfa433efd14d002d3826d Mon Sep 17 00:00:00 2001 From: omind conveyor factory Date: Thu, 22 Oct 2026 09:12:39 -0500 Subject: [PATCH] refactor: delete code no production path calls Remove SearchIndex.search_page with SearchPage and _pack_edge_first (the cursor/token-budget search API: only its own tests called it; MCP tools page through server._page), plus orphaned helpers: hooks._latest_by_name, hooks._journal_tail and their constants, guard._shell_body, policy._heredoc_owner_is_shell, mesh.commit_local, compliance.recidivism and four never-read harness marker constants in agents.py. Co-Authored-By: Claude Opus 5.5 --- docs/mesh.md | 2 +- src/omind/agents.py | 9 ---- src/omind/compliance.py | 5 --- src/omind/guard.py | 10 +---- src/omind/hooks.py | 27 ------------ src/omind/mesh.py | 8 ---- src/omind/policy.py | 12 +----- src/omind/searchindex.py | 90 --------------------------------------- tests/test_compliance.py | 3 +- tests/test_hooks.py | 2 +- tests/test_searchindex.py | 79 ---------------------------------- 11 files changed, 6 insertions(+), 241 deletions(-) diff --git a/docs/mesh.md b/docs/mesh.md index fea6d05..ab0f296 100644 --- a/docs/mesh.md +++ b/docs/mesh.md @@ -69,7 +69,7 @@ Every machine runs an identical **omind node**: | `omind/server.py` | Local **node MCP server** exposing `OmiStore` as MCP tools over stdio. Replaces the provisioned `obsidian-mcp`. (Since 2.27.0 the debounced-sync write signal lives in `OmiStore` itself, so every write surface — MCP, web UI, `omind note`, import — triggers it.) | | `omind/clock.py` | **Logical versioning** — a Lamport counter + stable node-id stamped into each note, the source of ordering truth for merges. | | `omind/merge.py` | The **git merge driver** for OMI notes (the core of the project). Field-level 3-way merge over `NoteFields`. | -| `omind/mesh.py` | **Replication daemon** — `init`, `commit_local`, `sync(peers)`, `daemon`, `clone`, peer membership. | +| `omind/mesh.py` | **Replication daemon** — `init`, `sync(peers)`, `daemon`, `clone`, peer membership. | ### Reused unchanged diff --git a/src/omind/agents.py b/src/omind/agents.py index 126c6c9..34a5382 100644 --- a/src/omind/agents.py +++ b/src/omind/agents.py @@ -197,11 +197,6 @@ def dsh_guard_plugin_path() -> Path: return dsh_home() / "plugins" / "omi-guard.dsh.js" -#: Substring identifying omind's own DSH guard plugin in the patch YAML, -#: so a re-run finds and replaces only our entry (never duplicates). -DSH_GUARD_MARKER = "omi-guard.dsh.js" - - def codex_config_dir() -> Path: """OpenAI Codex CLI's config directory: ``$CODEX_HOME`` or ``~/.codex``.""" base = os.environ.get("CODEX_HOME") @@ -283,8 +278,6 @@ def grok_rules_path() -> Path: return grok_config_dir() / "rules" / "omind.md" -#: Substring identifying omind's own Grok guard hook, so doctor can see it. -GROK_GUARD_MARKER = "guard adapter --harness grok" GROK_BOOTSTRAP_START = "" GROK_BOOTSTRAP_END = "" GROK_HOOK_EVENTS = ( @@ -426,8 +419,6 @@ def agy_skill_dir() -> Path: AGY_HOOK_NAME = "omind-omi-memory" -AGY_GUARD_MARKER = "guard adapter --harness agy" -AGY_HOOK_MARKER = "--harness agy" AGY_BOOTSTRAP_START = seeds.AGY_BOOTSTRAP_START AGY_BOOTSTRAP_END = seeds.AGY_BOOTSTRAP_END diff --git a/src/omind/compliance.py b/src/omind/compliance.py index 9e5b79d..5684c58 100644 --- a/src/omind/compliance.py +++ b/src/omind/compliance.py @@ -241,11 +241,6 @@ def read_events(limit: int | None = None) -> list[dict[str, Any]]: return list(events[-limit:]) if limit is not None else list(events) -def recidivism(rule_id: str) -> int: - """How many times ``rule_id`` has been recorded (decision + violation).""" - return sum(1 for e in read_events() if e.get("rule_id") == rule_id) - - def recidivism_counts(events: list[dict[str, Any]] | None = None) -> Counter[str]: """Per-rule occurrence counts across the whole log (drives escalation). diff --git a/src/omind/guard.py b/src/omind/guard.py index 6e80900..64c3e29 100644 --- a/src/omind/guard.py +++ b/src/omind/guard.py @@ -1937,15 +1937,9 @@ def _env_chdir_value(word: str, after: str) -> str | None: return None -def _shell_body(tokens: list[str]) -> str | None: - """The ``-c`` body of ``sh/bash/zsh … -c ''`` (``-lc``, ``-ec`` and - ``-o opt`` before it included), or ``None`` when there is no ``-c``.""" - i = _shell_body_index(tokens) - return None if i is None else tokens[i] - - def _shell_body_index(tokens: list[str]) -> int | None: - """Where :func:`_shell_body` finds the body in ``tokens``.""" + """Index of the ``-c`` body of ``sh/bash/zsh … -c ''`` in ``tokens`` + (``-lc``, ``-ec`` and ``-o opt`` before it included), or ``None``.""" has_c = False i = 1 while i < len(tokens): diff --git a/src/omind/hooks.py b/src/omind/hooks.py index f1e6d72..9716ce8 100644 --- a/src/omind/hooks.py +++ b/src/omind/hooks.py @@ -96,8 +96,6 @@ def command_is_omind_hook(command: str) -> bool: # current working directory. Auto-journal actions remain searchable but are not # injected at session start. _SESSION_STATE_GLOB = "Session State *.md" -_JOURNAL_GLOB = paths.JOURNAL_GLOB -_JOURNAL_TAIL_BULLETS = 20 # Maximum canonical "full" capsule. Runtime selection lives in ai_usage. _TOTAL_CONTEXT_CHAR_CAP = 24_000 _TRUNCATION_MARKER = "\n…[truncated]" @@ -410,19 +408,6 @@ def _read_priming_note(path: Path) -> str | None: return text -def _latest_by_name(directory: Path, pattern: str) -> Path | None: - """Newest note matching ``pattern``, by filename descending. Never raises. - - Filenames embed ``YYYY-MM-DD``, so a plain lexicographic sort is also a - chronological sort. - """ - try: - matches = sorted(directory.glob(pattern), key=lambda p: p.name, reverse=True) - except OSError: - return None - return matches[0] if matches else None - - def action_bullets(text: str) -> list[str]: """The ``- `` bullets under a journal's ``## Actions`` heading. @@ -441,18 +426,6 @@ def action_bullets(text: str) -> list[str]: return bullets -def _journal_tail(path: Path, limit: int = _JOURNAL_TAIL_BULLETS) -> str | None: - """Last ``limit`` action bullets of a journal note, or ``None``. Never raises.""" - try: - text = path.read_text(encoding="utf-8", errors="replace") - except OSError: - return None - bullets = action_bullets(text) - if not bullets: - return None - return "\n".join(bullets[-limit:]) - - def _update_nudge_line() -> str | None: """The "newer omind available" line for the SessionStart context, or ``None``. diff --git a/src/omind/mesh.py b/src/omind/mesh.py index 248bb72..2f2d9f3 100644 --- a/src/omind/mesh.py +++ b/src/omind/mesh.py @@ -530,14 +530,6 @@ def _commit_locked(omi_dir: Path, message: str) -> bool: return True -def commit_local(omi_dir: Path, node_id: str) -> bool: - """Commit local changes. The lock spans staging, so a concurrent store - write can never be half-staged.""" - store = OmiStore(omi_dir) - with store.write_lock(): - return _commit_locked(omi_dir, f"omind: local changes on {node_id}") - - def _first_line(text: str) -> str: """The most diagnostic line of a git message (CONFLICT/error/fatal first).""" lines = [ln for ln in text.strip().splitlines() if ln.strip()] diff --git a/src/omind/policy.py b/src/omind/policy.py index b9cb65d..d064765 100644 --- a/src/omind/policy.py +++ b/src/omind/policy.py @@ -388,16 +388,6 @@ def command_search(compiled: re.Pattern[str], pattern: str, text: str) -> bool: _OWNER_SEP_RE = re.compile(r"[\n;&|(`]") -def _heredoc_owner_is_shell(command: str, start: int) -> bool: - """True when the simple command owning the heredoc at ``start`` is a shell. - - Scans back to the nearest separator and takes the first token that is not a - ``VAR=val`` assignment or a transparent wrapper, comparing its basename. - One heredoc's answer; :class:`_HeredocOwners` answers a run of them. - """ - return _HeredocOwners(command).is_shell(start) - - def _owner_word_skipped(token: str) -> bool: """Whether a word before a heredoc's owner is passed over: a ``VAR=val`` assignment or a transparent wrapper.""" @@ -412,7 +402,7 @@ def _owner_word_skipped(token: str) -> bool: class _HeredocOwners: - """:func:`_heredoc_owner_is_shell` for every heredoc in ``command``, in + """Whether each heredoc's owning simple command is a shell, in linear time overall (#445). Each separator-bounded segment's words are found once, with the first one that is not skipped; a heredoc's owner is that word when it ends before the heredoc. Walking the words from the diff --git a/src/omind/searchindex.py b/src/omind/searchindex.py index e3dfd48..9e5b6e2 100644 --- a/src/omind/searchindex.py +++ b/src/omind/searchindex.py @@ -129,18 +129,6 @@ def available() -> bool: return not os.environ.get(DISABLE_ENV) and _fts5_available() -@dataclass -class SearchPage: - """One page of hits, with what the caller needs to decide whether to go on.""" - - hits: list[Hit] - #: Pass back as ``cursor`` to get the next page; ``None`` means this was all. - next_cursor: int | None = None - #: Summed estimate for this page, so context spend is a number the caller - #: can act on rather than something it discovers after the fact. - tokens_returned: int = 0 - - @dataclass class Hit: """One ranked chunk. ``excerpt`` is the matched text, not the whole note.""" @@ -1051,50 +1039,6 @@ def _embed_chunks(self, db: sqlite3.Connection, pending: list[tuple[int, str]]) # -- query -------------------------------------------------------------- - @_locked - def search_page( - self, - query: str, - *, - tag: str | None = None, - limit: int = 25, - include_disabled: bool = False, - max_tokens: int | None = None, - cursor: int = 0, - ) -> SearchPage | None: - """One bounded page of results, plus the cursor needed to ask for more. - - Why a cursor rather than a bigger ``limit``: under load a model tends to - conclude before it has exhausted what it was given — it stops early and - answers from what it has. A page that ends with an explicit - ``next_cursor`` makes continuing a deliberate act rather than something - the model has to remember to keep doing, and makes "there was more" a - fact the caller can see instead of one it has to infer. - - ``max_tokens`` bounds the page by estimated cost and reorders it - edge-first; see :func:`_pack_edge_first`. ``None`` leaves both off. - """ - hits = self.search( - query, - tag=tag, - limit=limit + cursor + 1, # +1 so we can tell "more" from "exactly full" - include_disabled=include_disabled, - ) - if hits is None: - return None - window = hits[cursor : cursor + limit] - has_more = len(hits) > cursor + limit - if max_tokens is not None and window: - window = _pack_edge_first(window, max_tokens) - # Packing can drop the tail, so "more" is now also true when the - # budget — not the page size — is what cut the results short. - has_more = has_more or len(window) < len(hits[cursor : cursor + limit]) - return SearchPage( - hits=window, - next_cursor=(cursor + len(window)) if has_more else None, - tokens_returned=sum(h.tokens for h in window), - ) - @_locked def search( self, @@ -2106,40 +2050,6 @@ def _annotate(hits: list[Hit]) -> None: hit.separation = hit.score - hits[i + 1].score if i + 1 < len(hits) else 0.0 -def _pack_edge_first(hits: list[Hit], max_tokens: int) -> list[Hit]: - """Greedily fit hits into ``max_tokens``, then order them best-outward. - - Two separate jobs, and the second is the interesting one. - - The budget exists because retrieval quality is not the binding constraint: - even with the right passage located, accuracy falls as the surrounding - context grows. Better ranking only decides how large the bill is; it cannot - stop the caller from paying it. So cap what leaves this function. - - The ORDER exists because attention over a long context is U-shaped — the - beginning and end are read well and the middle thins out. Emitting - best-first / second-best-LAST / weakest-in-the-middle puts the strongest - evidence where it is actually read. Note this only pays off once a caller - concatenates enough text to have a "middle" at all; below that it is - harmless reordering, which is why the budget is opt-in rather than default. - """ - kept: list[Hit] = [] - spent = 0 - for hit in hits: - cost = hit.tokens or _estimate_tokens(hit.excerpt) - # Always keep the best hit, even if it alone busts the budget: returning - # nothing is strictly worse than returning the one thing that matched. - if kept and spent + cost > max_tokens: - break - kept.append(hit) - spent += cost - head: list[Hit] = [] - tail: list[Hit] = [] - for i, hit in enumerate(kept): - (head if i % 2 == 0 else tail).append(hit) - return head + tail[::-1] - - def _fuse(legs: list[tuple[float, list[int]]]) -> list[tuple[int, float]]: """Reciprocal Rank Fusion over weighted ranked lists, best first.""" scores: dict[int, float] = {} diff --git a/tests/test_compliance.py b/tests/test_compliance.py index eeff60d..811900a 100644 --- a/tests/test_compliance.py +++ b/tests/test_compliance.py @@ -88,13 +88,12 @@ def test_the_log_rotates_at_the_cap_without_losing_counts( # The live log restarted, but history spans both generations — escalation # still counts every hit that came before the rotation. assert compliance.compliance_log_path().stat().st_size <= 200 - assert compliance.recidivism("r") == logged + assert compliance.recidivism_counts()["r"] == logged def test_recidivism_counts_exclude_the_gate() -> None: for rid in ("r1", "r1", "r2", "omi-gate"): compliance.log_event(compliance.KIND_DECISION, rule_id=rid, outcome="deny") - assert compliance.recidivism("r1") == 2 counts = compliance.recidivism_counts() assert counts["r1"] == 2 and counts["r2"] == 1 assert "omi-gate" not in counts diff --git a/tests/test_hooks.py b/tests/test_hooks.py index a5d0e20..be8b8d5 100644 --- a/tests/test_hooks.py +++ b/tests/test_hooks.py @@ -530,7 +530,7 @@ def test_session_start_journal_tail_is_last_bullets_only(tmp_path: Path) -> None _write_priming_files(tmp_path) old = datetime(2026, 6, 1, 9, 0, 0) hooks.append_entry(tmp_path, "- 09:00 [session old00000] PostToolUse Bash -> OLD-J (ok)", old) - for i in range(hooks._JOURNAL_TAIL_BULLETS + 5): + for i in range(25): bullet = f"- 14:32 [session abcd1234] PostToolUse Bash -> c{i:02d} (ok)" hooks.append_entry(tmp_path, bullet, _NOW) ctx = hooks.build_session_start_context(tmp_path) diff --git a/tests/test_searchindex.py b/tests/test_searchindex.py index 2282fc8..d47be5b 100644 --- a/tests/test_searchindex.py +++ b/tests/test_searchindex.py @@ -617,85 +617,6 @@ def test_annotate_reports_gap_to_next_hit() -> None: assert hits[-1].separation == 0.0 # nothing follows the last hit -def test_pack_edge_first_puts_the_best_at_both_ends() -> None: - """Attention over long context is U-shaped, so rank 2 goes LAST, not second.""" - hits = [ - searchindex.Hit(filename=f"n{i}", heading="", excerpt="x" * 40, score=1.0 - i / 10) - for i in range(5) - ] - searchindex._annotate(hits) - packed = searchindex._pack_edge_first(hits, 10_000) - assert packed[0].filename == "n0" # best first - assert packed[-1].filename == "n1" # second best last - assert packed[2].filename == "n4" # weakest buried in the middle - assert sorted(h.filename for h in packed) == sorted(h.filename for h in hits) - - -def test_pack_respects_the_budget() -> None: - hits = [ - searchindex.Hit(filename=f"n{i}", heading="", excerpt="x" * 400, score=1.0 - i / 10) - for i in range(5) - ] - searchindex._annotate(hits) - assert all(h.tokens == 100 for h in hits) - assert len(searchindex._pack_edge_first(hits, 250)) == 2 - - -def test_pack_always_returns_the_top_hit_even_when_it_busts_the_budget() -> None: - """Returning nothing is strictly worse than returning the one match.""" - hits = [searchindex.Hit(filename="huge", heading="", excerpt="x" * 4000, score=1.0)] - searchindex._annotate(hits) - assert [h.filename for h in searchindex._pack_edge_first(hits, 1)] == ["huge"] - - -def test_search_page_cursor_walks_the_results(omi: Path) -> None: - _note(omi, "Release One", "release checklist alpha", ["release"]) - _note(omi, "Release Two", "release checklist beta", ["release"]) - _note(omi, "Release Three", "release checklist gamma", ["release"]) - idx = searchindex.SearchIndex(omi) - idx.refresh() - - first = idx.search_page("release", limit=1) - assert first is not None and first.hits - assert first.tokens_returned == sum(h.tokens for h in first.hits) - assert first.next_cursor is not None # three notes, one per page - - second = idx.search_page("release", limit=1, cursor=first.next_cursor) - assert second is not None and second.hits - # A cursor must advance. A page that returns the same note forever is worse - # than no paging, because it looks like progress. - assert [h.filename for h in second.hits] != [h.filename for h in first.hits] - - -def test_search_page_without_budget_matches_plain_search(omi: Path) -> None: - """The new surface must not change what the old one returns.""" - _note(omi, "Release One", "release checklist alpha", ["release"]) - _note(omi, "Release Two", "release checklist beta", ["release"]) - idx = searchindex.SearchIndex(omi) - idx.refresh() - - plain = idx.search("release", limit=5) - page = idx.search_page("release", limit=5) - assert plain is not None and page is not None - assert [h.filename for h in page.hits] == [h.filename for h in plain[:5]] - assert page.next_cursor is None # everything fit - - -def test_search_page_budget_signals_there_is_more(omi: Path) -> None: - """A budget that truncates must still report next_cursor, or the caller - silently believes it saw everything.""" - for i in range(4): - _note(omi, f"Release {i}", "release checklist " + ("word " * 200), ["release"]) - idx = searchindex.SearchIndex(omi) - idx.refresh() - - page = idx.search_page("release", limit=4, max_tokens=60) - assert page is not None and page.hits - assert len(page.hits) < 4 # the budget cut it short - assert page.next_cursor is not None # ...and said so - assert page.tokens_returned == sum(h.tokens for h in page.hits) - - def test_separation_reaches_the_search_vault_payload(omi: Path) -> None: """The margin must survive Hit -> NoteSummary, or the MCP tool never sees it.