Files
hermes-webui/tests/test_pin_limit_lineage_visibility.py
nesquena-hermes cccb97d970
Some checks failed
Release & Docker / release (push) Has been cancelled
Release v0.51.222 — Release GP (stage-p4 — backend bugfix batch: title language drift #3293 + orphaned CLI sidecar prune #3238 + pin-quota lineage #3288) (#3452)
* fix: reject cross-script drifted auto-generated session titles (#3293)

The title-language mismatch guard only knew two states: German (de) or empty,
and _title_language_mismatch early-returned False whenever the user start
wasn't German. So an English conversation whose LLM-generated title came back
in Chinese / Spanish / Russian sailed through and persisted with
llm_title_generated=true. The German case was the only one covered because
that's the one prior report it was built for.

Generalize from a German-specific binary to a language-agnostic cross-script
check. Add _script_counts() + _dominant_script() (cheap, dependency-free
Unicode-block classification: latin / cjk / cyrillic / arabic / hebrew / greek
/ devanagari). _title_language_mismatch now rejects a title that introduces a
substantial amount (>=35% of alphabetic chars, min 2) of a script different
from the conversation start's dominant script — so short titles that embed a
borrowed Latin technical term still trip, while an English title with a single
foreign place-name does not. The legacy German->English same-script heuristic
is preserved verbatim.

Kept api/streaming.py ASCII-only (the test_title_generation_source_has_no_cjk_
literals guard) — all CJK examples live in the test file, not the source.

Closes #3293

Co-authored-by: andrewkangkr <andrewkangkr@users.noreply.github.com>

* fix: prune orphaned imported-CLI sidecars from the WebUI sidebar (#3238)

When a CLI/agent session is opened in WebUI it gets a WebUI-owned sidecar
(webui/sessions/<id>.json + _index.json row) so it can render and reopen;
all_sessions() then returns it independently of the agent state.db. If the
user later deletes that session from the CLI / local Hermes storage, nothing
pruned the sidecar — the merge loop only overlays CLI metadata when a matching
state.db row exists and otherwise continues, so the stale row lingered in the
sidebar indefinitely (there is no WebUI delete affordance for CLI rows).

Add api.models.agent_session_row_exists(): an exact, uncapped existence probe
against the state.db sessions table. The sidebar merge loop now drops a row
that is_cli_session_row + not WebUI-native + absent from cli_by_id + whose
state.db row is genuinely gone, and calls prune_session_from_index() so
_index.json self-heals.

The state.db probe is deliberate: get_cli_sessions() caps at
CLI_VISIBLE_SESSION_LIMIT (20), so a still-existing session can fall out of
that window and look deleted — pruning on cli_by_id absence alone would delete
live sessions. WebUI-native rows with a CLI ancestor are never pruned, and any
probe error degrades to keep-the-row so a transient failure can't lose data.

Closes #3238

Co-authored-by: Luxciax <Luxciax@users.noreply.github.com>

* fix: count pin quota by visible session lineage

* docs(changelog): v0.51.222 — backend bugfix batch (#3293 title drift, #3238 sidecar prune, #3288 pin lineage)

* fix(pins): forks count as own pin lineage, not collapsed to parent (#3288 Codex follow-up)

Codex review of the batch found a pin-limit UNDERCOUNT: _session_row_lineage_root_id
followed any parent_session_id to the root, but /api/session/branch creates
independent visible fork sessions that also carry parent_session_id (session_source=
'fork'). Two pinned forks of the same parent collapsed to one quota lineage, letting
a user exceed pinned_sessions_limit with no 400. Fix: a fork returns its own id as
its lineage root (it's a separately-visible session); only compression/continuation
rows still collapse to a shared root. Adds a regression test with two pinned forks +
the parent counting as three distinct lineages, and confirms the existing
pre-compression-snapshot collapse case still passes.

* test(pins): update #2508/#2821 source-match tests for #3288 lineage rename

#3288 replaced the raw-session-id pin counter (pinned_ids set) with a
visible-lineage counter (pinned_lineage_ids via _visible_pinned_lineage_ids over
persisted_rows/candidate_rows). Two pre-existing source-string-matching tests
asserted the OLD implementation literals (pinned_ids = {, _session_field(existing,
session_id...), len(pinned_ids) >=). Updated both to assert the new mechanism while
preserving the invariants they actually guard: snapshot computed BEFORE LOCK (no
all_sessions()-inside-LOCK deadlock), quota filtering via the shared
_session_counts_toward_pin_quota helper, and the limit/400 guard. Behaviour
unchanged; these were implementation-detail assertions, not behaviour tests.

---------

Co-authored-by: nesquena-hermes <[email protected]>
Co-authored-by: andrewkangkr <andrewkangkr@users.noreply.github.com>
Co-authored-by: Luxciax <Luxciax@users.noreply.github.com>
Co-authored-by: Andy Kang <andrewkang.kr@gmail.com>
2026-06-02 17:35:18 -07:00

113 lines
3.6 KiB
Python

import sys
from pathlib import Path
REPO_ROOT = Path(__file__).resolve().parents[1]
if str(REPO_ROOT) not in sys.path:
sys.path.insert(0, str(REPO_ROOT))
from api.routes import _session_row_lineage_root_id, _visible_pinned_lineage_ids
def test_visible_pinned_lineage_ids_dedupes_multiple_pinned_continuations():
rows = [
{
"session_id": "gov-new",
"title": "Project OS Governor",
"pinned": True,
"archived": False,
"parent_session_id": "gov-mid",
},
{
"session_id": "gov-mid",
"title": "Project OS Governor",
"pinned": True,
"archived": False,
"parent_session_id": "gov-root",
},
{
"session_id": "gov-root",
"title": "Project OS Governor",
"pinned": True,
"archived": False,
"parent_session_id": None,
},
{
"session_id": "other-pin",
"title": "Import Preview",
"pinned": True,
"archived": False,
"parent_session_id": None,
},
]
roots = _visible_pinned_lineage_ids(rows)
assert roots == {"gov-root", "other-pin"}
def test_visible_pinned_lineage_ids_ignores_hidden_precompression_snapshots():
rows = [
{
"session_id": "snap-root",
"title": "Project OS Governor",
"pinned": True,
"archived": False,
"pre_compression_snapshot": True,
"parent_session_id": None,
},
{
"session_id": "live-root",
"title": "Project OS Governor",
"pinned": True,
"archived": False,
"parent_session_id": "snap-root",
},
]
roots = _visible_pinned_lineage_ids(rows)
assert roots == {"snap-root"}
def test_session_row_lineage_root_uses_explicit_root_when_present():
row = {
"session_id": "tip",
"_lineage_root_id": "root-123",
"parent_session_id": "older",
}
assert _session_row_lineage_root_id(row, {"tip": row}) == "root-123"
def test_pinned_forks_of_same_parent_count_as_separate_lineages():
"""A branch/fork (session_source='fork') is an independent visible session and
must consume its own pin slot — two pinned forks of the same parent must NOT
collapse to one quota lineage (would let the user exceed the pin limit). #3288."""
rows = [
{
"session_id": "parent-root",
"title": "Original",
"pinned": True,
"archived": False,
"parent_session_id": None,
},
{
"session_id": "fork-a",
"title": "Fork A",
"pinned": True,
"archived": False,
"session_source": "fork",
"parent_session_id": "parent-root",
},
{
"session_id": "fork-b",
"title": "Fork B",
"pinned": True,
"archived": False,
"session_source": "fork",
"parent_session_id": "parent-root",
},
]
# Each fork is its own lineage root; the parent is its own root too → 3 lineages.
assert _session_row_lineage_root_id(rows[1], {r["session_id"]: r for r in rows}) == "fork-a"
assert _session_row_lineage_root_id(rows[2], {r["session_id"]: r for r in rows}) == "fork-b"
roots = _visible_pinned_lineage_ids(rows)
assert roots == {"parent-root", "fork-a", "fork-b"}
# Three distinct pinned lineages → would exceed a limit of 2 (no false collapse).
assert len(roots) == 3