Skip to content

fix(honcho): truncate resolve_session_name output to Honcho's 100-char limit (#13868)#13931

Closed
Sanjays2402 wants to merge 0 commit into
NousResearch:mainfrom
Sanjays2402:fix/13868-resolve-session-name-truncation
Closed

fix(honcho): truncate resolve_session_name output to Honcho's 100-char limit (#13868)#13931
Sanjays2402 wants to merge 0 commit into
NousResearch:mainfrom
Sanjays2402:fix/13868-resolve-session-name-truncation

Conversation

@Sanjays2402

Copy link
Copy Markdown
Contributor

What does this PR do?

HonchoClientConfig.resolve_session_name() sanitizes the gateway_session_key to strip characters Honcho doesn't accept, but never enforces Honcho's 100-character limit on session IDs. For gateways that build rich session keys — Matrix !room:server + thread event IDs, Telegram supergroup reply chains, Slack thread IDs with long workspace prefixes — the sanitized ID overflows the limit and every Honcho API call for that session 400s with session_id too long.

This PR adds a length-enforcement helper, _enforce_session_id_limit, at the end of the gateway-key branch in resolve_session_name. If the sanitized ID is over 100 characters, it's truncated to a shorter prefix and a deterministic -<sha256 prefix> suffix is appended — so two long keys that share a leading segment can't collide onto the same ID. Short keys (the common case) are unchanged: the fast path returns the sanitized string as-is with zero extra work.

The hash is taken over the original pre-sanitization key, so two inputs that sanitize to the same string still collide intentionally (same logical session), but two inputs that only share a prefix do not.

Related Issue

Fixes #13868

Type of Change

  • 🐛 Bug fix (non-breaking change that fixes an issue)

Changes Made

  • plugins/memory/honcho/client.py (+37 / −1): new _enforce_session_id_limit(sanitized, original) classmethod + two class constants; resolve_session_name gateway-key branch routes through it.
  • tests/honcho_plugin/test_client.py (+76): new TestResolveSessionNameLengthLimit class with 7 regression cases.

Implementation diff (core change):

# plugins/memory/honcho/client.py

# Honcho enforces a 100-char limit on session IDs. See issue #13868.
_HONCHO_SESSION_ID_MAX_LEN = 100
_HONCHO_SESSION_ID_HASH_LEN = 8

@classmethod
def _enforce_session_id_limit(cls, sanitized: str, original: str) -> str:
    max_len = cls._HONCHO_SESSION_ID_MAX_LEN
    if len(sanitized) <= max_len:
        return sanitized

    import hashlib
    hash_len = cls._HONCHO_SESSION_ID_HASH_LEN
    digest = hashlib.sha256(original.encode("utf-8")).hexdigest()[:hash_len]
    prefix_len = max_len - hash_len - 1  # room for '-' separator
    prefix = sanitized[:prefix_len].rstrip("-")
    return f"{prefix}-{digest}"
         if gateway_session_key:
             sanitized = re.sub(r'[^a-zA-Z0-9_-]+', '-', gateway_session_key).strip('-')
             if sanitized:
-                return sanitized
+                return self._enforce_session_id_limit(sanitized, gateway_session_key)

How to Test

Reporter-style repro:

from plugins.memory.honcho.client import HonchoClientConfig

config = HonchoClientConfig()
long_key = "!roomid:matrix.example.org|$event_" + "a" * 300
resolved = config.resolve_session_name(gateway_session_key=long_key)
assert resolved is not None
assert len(resolved) == 100          # was: > 300 before fix
assert resolved.endswith(resolved[-9:])  # ends with '-<8 hex>'

Before: len(resolved) > 300, Honcho rejects the session with HTTP 400 session_id too long.
After: len(resolved) == 100, deterministic, collision-resistant across distinct long keys.

Run the regression suite:

pytest tests/honcho_plugin/test_client.py -k "LengthLimit or GatewayKey" -q

Checklist

Code

  • I've read the Contributing Guide
  • My commit messages follow Conventional Commits (fix(honcho):)
  • I searched for existing PRs to make sure this isn't a duplicate
  • My PR contains only changes related to this fix
  • I've run pytest tests/honcho_plugin/test_client.py -q (scoped to the affected file) and all tests pass
  • I've added tests for my changes (7 new regression cases)
  • I've tested on my platform: macOS 26.5 (arm64), Python 3.11.14 via uv

Documentation & Housekeeping

  • I've updated relevant documentation — N/A (internal helper, no user-visible surface change beyond bug fix)
  • I've updated cli-config.yaml.example if I added/changed config keys — N/A (no new config)
  • I've updated CONTRIBUTING.md or AGENTS.md — N/A
  • I've considered cross-platform impact (Windows, macOS) — uses stdlib hashlib only, no OS-specific behavior
  • I've updated tool descriptions/schemas — N/A

Screenshots / Logs

Regression tests added

Test Scenario
test_short_gateway_key_unchanged Short key (<100 chars) passes through untouched — no hash suffix
test_key_at_exact_limit_unchanged Sanitized key of exactly 100 chars returned as-is
test_long_gateway_key_truncated_to_limit 300+ char key truncated to exactly 100 chars
test_truncation_is_deterministic Same long key → same truncated ID across calls
test_truncated_result_respects_char_allowlist Truncated result still matches [a-zA-Z0-9_-]+ allowlist
test_distinct_long_keys_do_not_collide Two 200+ char keys sharing a prefix produce different truncated IDs
test_truncated_result_has_hash_suffix Truncated IDs end with -<8 hex chars>

Verification

$ python3 -m py_compile plugins/memory/honcho/client.py tests/honcho_plugin/test_client.py
OK

$ uv run --with pytest --with pytest-xdist python -m pytest \
    tests/honcho_plugin/test_client.py -k "LengthLimit or GatewayKey" -q
...........                                                              [100%]
11 passed in 1.01s

(11 tests = 7 new LengthLimit cases + 4 existing GatewayKey cases, all green.)

The truncation branch only fires when the sanitized ID actually exceeds 100 characters, so short-key callers (the common case across Telegram DMs, Discord DMs, single-room Matrix channels, per-session CLI strategy) see zero behavioral change — the fast path short-circuits before any hashing. The hash suffix is deterministic (same input → same output), so session lookups are stable across restarts and processes.

@Sanjays2402

Copy link
Copy Markdown
Contributor Author

Closing — this fix already landed on main as cd1c481 (fix(honcho): truncate resolve_session_name output to Honcho's 100-char limit (#13868)). Verified: git log --grep=13868 on main shows the commit, and a rebase against current main produces an empty diff. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

comp/plugins Plugin system and bundled plugins P2 Medium — degraded but workaround exists type/bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: resolve_session_name sanitizes gateway_session_key without length truncation, overflows Honcho's 100-char session ID limit

3 participants