Skip to content
This repository was archived by the owner on Oct 13, 2026. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
228 changes: 228 additions & 0 deletions FINDINGS-VERDICTS-PR183.md

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -385,6 +385,7 @@ Most installs only need `plugins.enabled` and `context.engine: lcm`.
| `LCM_NEW_SESSION_RETAIN_DEPTH` | `2` | DAG depth retained after manual `/new` (`-1` all, `0` none) |
| `LCM_DATABASE_PATH` | auto | SQLite database path. Empty config resolves to `HERMES_HOME/lcm.db`; plugin installs or operators may set this env var to another profile-scoped path such as `~/.hermes/hermes-lcm.db`. |
| `LCM_FTS_INTEGRITY_CHECK_INTERVAL_HOURS` | `24` | Minimum hours between startup FTS5 deep integrity-checks (O(index size)). `0` checks every startup; a negative value never checks on startup. Structural checks always run regardless. |
| `LCM_FTS_PROSE_MODE` | `false` | Opt raw natural-language queries into a bounded disjunctive FTS form; compact keyword and caller-composed operator queries keep their current semantics |
| `LCM_ENABLE_SLASH_COMMAND` | `false` | Enable the optional `/lcm` operator command surface |

When `LCM_FRESH_TAIL_MAX_TOKENS` is enabled, the protected suffix must satisfy
Expand Down
4 changes: 4 additions & 0 deletions config.py
Original file line number Diff line number Diff line change
Expand Up @@ -374,6 +374,7 @@ class _EnvFieldSpec:
_EnvFieldSpec("recall_scan_rows", "LCM_RECALL_SCAN_ROWS", int),
_EnvFieldSpec("recall_scan_max_rows", "LCM_RECALL_SCAN_MAX_ROWS", int),
_EnvFieldSpec("recall_scan_budget_s", "LCM_RECALL_SCAN_BUDGET_S", float),
_EnvFieldSpec("fts_prose_mode", "LCM_FTS_PROSE_MODE", bool),
_EnvFieldSpec("recall_reference_strict", "LCM_RECALL_REFERENCE_STRICT", bool),
_EnvFieldSpec("proactive_recall_enabled", "LCM_PROACTIVE_RECALL_ENABLED", bool),
_EnvFieldSpec("proactive_recall_min_score", "LCM_PROACTIVE_RECALL_MIN_SCORE", float),
Expand Down Expand Up @@ -658,6 +659,9 @@ class LCMConfig:
# batches and degrades to coverage='bounded' rather than silently paying an
# unbounded cost. This is the ONLY thing that truncates a default scan.
recall_scan_budget_s: float = 0.0
# Opt raw natural-language questions into a bounded disjunctive FTS5 form.
# Default-off preserves the existing implicit-AND query bytes and ranking.
fts_prose_mode: bool = False
# Per-arm RRF fusion weights for lcm_recall's 3-arm hybrid (fts/summary/chunk).
# Down-weighting the weak FTS arm keeps naive equal-weight fusion from dragging
# fused recall below its best (vector) arm — measured −21 R@5 on LongMemEval.
Expand Down
65 changes: 54 additions & 11 deletions dag.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
_DELETE_SESSION_SCOPE_TABLE = "temp_lcm_delete_session_scope"
_DELETE_SESSION_SCOPE_INSERT_CHUNK = 512
from .search_query import (
build_fts5_match_query,
AGE_DECAY_RATE,
compute_search_candidate_cap,
compute_directness_rank_bonus_upper_bound,
Expand All @@ -42,12 +43,14 @@
count_term_matches,
escape_like,
extract_quoted_phrases,
extract_prose_search_terms,
extract_search_terms,
normalize_search_sort,
requires_like_fallback,
sanitize_fts5_query,
resolve_prose_sort,
sanitize_like_query,
should_apply_directness_rank_adjustment,
should_use_fts_prose_mode,
)
from .store import _normalize_source_value, _UNKNOWN_SOURCE, _legacy_blank_source_clause

Expand Down Expand Up @@ -551,7 +554,8 @@ def get_uncondensed_at_depth(self, session_id: str, depth: int,

def search(self, query: str, session_id: str | None = None,
limit: int = 20, sort: str | None = None,
source: str | None = None) -> List[SummaryNode]:
source: str | None = None,
fts_prose_mode: bool = False) -> List[SummaryNode]:
"""FTS5 search across summary nodes.

Retrieval contract:
Expand All @@ -562,15 +566,32 @@ def search(self, query: str, session_id: str | None = None,
session-level source presence
- mixed-source nodes may match more than one ``source`` filter
"""
safe_query = sanitize_fts5_query(query)
safe_query = build_fts5_match_query(query, prose_mode=fts_prose_mode)
sort = resolve_prose_sort(sort, fts_prose_mode, query)
terms = extract_search_terms(safe_query)
phrases = extract_quoted_phrases(safe_query)
# LIKE is the fallback for text sanitization LOSES (CJK/emoji) and for a
# query with no term left after it. A raw natural-language question is
# NOT one of those: it sanitizes to a term form the index answers, so it
# stays on the FTS path (F31 §3).
if requires_like_fallback(query, safe_query):
return self._search_like(query, session_id=session_id, limit=limit, sort=sort, source=source)
if requires_like_fallback(
query,
safe_query,
# Symbol preservation is gated on the PROSE CLASSIFICATION, not the
# flag alone: a compact classifier-negative query must keep its
# flag-off route and conjunctive semantics.
preserve_unicode_symbols=(
fts_prose_mode and should_use_fts_prose_mode(query)
),
):
return self._search_like(
query,
session_id=session_id,
limit=limit,
sort=sort,
source=source,
prose_mode=fts_prose_mode,
Comment thread
100yenadmin marked this conversation as resolved.
Resolved
)

order_by = _build_search_order_by(sort, "COALESCE(n.latest_at, n.created_at)")
fetch_limit = compute_search_fetch_limit(limit, terms, phrases)
Expand Down Expand Up @@ -603,7 +624,14 @@ def search(self, query: str, session_id: str | None = None,
scanned_rows += len(rows)
except sqlite3.Error as exc:
logger.warning("FTS node search failed, falling back to LIKE: %s", exc)
return self._search_like(query, session_id=session_id, limit=limit, sort=sort, source=source)
return self._search_like(
query,
session_id=session_id,
limit=limit,
sort=sort,
source=source,
prose_mode=fts_prose_mode,
)

raw_nodes = [self._row_to_node(r) for r in rows]
for node in raw_nodes:
Expand Down Expand Up @@ -642,11 +670,20 @@ def search(self, query: str, session_id: str | None = None,

def _search_like(self, query: str, session_id: str | None = None,
limit: int = 20, sort: str | None = None,
source: str | None = None) -> List[SummaryNode]:
source: str | None = None,
prose_mode: bool = False) -> List[SummaryNode]:
# LIKE keeps every character the index cannot spell (emoji, punctuation)
# because substring matching is the only way to find those rows.
safe_query = sanitize_like_query(query)
terms = extract_search_terms(safe_query)
# Classification always keys off the canonical FTS-sanitized view
# (should_use_fts_prose_mode sanitizes internally); extraction stays on
# the LIKE-sanitized form so unindexable characters remain searchable.
prose_branch = prose_mode and should_use_fts_prose_mode(query)
terms = (
extract_prose_search_terms(query, safe_query)
if prose_branch
else extract_search_terms(safe_query)
)
phrases = extract_quoted_phrases(safe_query)
if not terms:
return []
Expand All @@ -664,7 +701,9 @@ def _search_like(self, query: str, session_id: str | None = None,
where.append("(" + " OR ".join(like_clauses) + ")")
fetch_limit = compute_like_fallback_fetch_limit(limit, terms, phrases)
base_args = list(args)
collapse_risky_repeats = contains_risky_fts_ascii(query)
collapse_term_repeats = (
prose_branch or contains_risky_fts_ascii(query)
)
candidate_cap = compute_search_candidate_cap(limit)
offset = 0
scanned_rows = 0
Expand All @@ -675,6 +714,7 @@ def _search_like(self, query: str, session_id: str | None = None,
rows = self._conn.execute(
f"""SELECT * FROM summary_nodes
WHERE {' AND '.join(where)}
ORDER BY rowid
LIMIT ? OFFSET ?""",
[*base_args, fetch_limit, offset],
).fetchall()
Expand All @@ -684,7 +724,7 @@ def _search_like(self, query: str, session_id: str | None = None,
if source and not self._node_matches_source(node.node_id, source, cache=source_match_cache):
continue
score = sum(
min(count_term_matches(node.summary, term), 1) if collapse_risky_repeats else count_term_matches(node.summary, term)
min(count_term_matches(node.summary, term), 1) if collapse_term_repeats else count_term_matches(node.summary, term)
for term in terms
)
if score <= 0:
Expand All @@ -694,7 +734,10 @@ def _search_like(self, query: str, session_id: str | None = None,
nodes.append(node)

nodes.sort(key=lambda node: _fallback_result_sort_key(node, sort))
if not source or len(rows) < fetch_limit or scanned_rows >= candidate_cap:
# Truncating after the FIRST batch
# can drop the best match at row fetch_limit+1. Always scan the
# bounded candidate set (candidate_cap) before applying `limit`.
if len(rows) < fetch_limit or scanned_rows >= candidate_cap:
Comment thread
100yenadmin marked this conversation as resolved.
Resolved
Comment thread
100yenadmin marked this conversation as resolved.
Resolved
return nodes[:limit]

offset += len(rows)
Expand Down
Loading