omi icon indicating copy to clipboard operation
omi copied to clipboard

[Bounty proposal] fix(preference-tools): validate non-empty preference, fallback top-level config, and guard non-dict duplicate hits ($50 proposed)

Open Dimank92 opened this issue 1 month ago • 2 comments

Summary

In backend/utils/retrieval/tools/preference_tools.py, several edge cases in agent preference storage and retrieval cause runtime exceptions or state pollution:

  1. Empty / None / Whitespace-only preference parameter: In save_user_preference_tool:
    • If preference is None, preference.split() in _write_provenance crashes with AttributeError: 'NoneType' object has no attribute 'split'.
    • If preference is empty string or whitespace-only (e.g. "" or " "), it bypasses validation and creates empty memory ledger records, polluting the user's persistent knowledge ledger.
    • Pydantic schema validation crashes if preference is passed as None or omitted by an LLM agent without Optional[str] = None annotation.
  2. Missing top-level user_id / uid fallback in _get_uid: _get_uid only checked configurable['user_id']. When callers or framework adapters supply user_id or uid at the top level of the config dict (e.g. {"user_id": "..."}), _get_uid returned an empty string, causing premature "Error: Could not determine user ID" failures.
  3. replace_memory_id crashes on non-string / None inputs: replace_memory_id.strip() crashes with AttributeError: 'NoneType' object has no attribute 'strip' if an agent passes replace_memory_id=None or an integer.
  4. preference_duplicate_message crashes on non-dict items: If search results from canonical memory providers or mock harnesses contain non-dict elements (e.g. None), hit.get(...) crashes with AttributeError: 'NoneType' object has no attribute 'get'.
  5. _write_provenance missing session resolution fallback: _write_provenance did not check top-level chat_session_id, session_id, or thread_id, defaulting to 'direct-agent-tool' even when a session ID was present in the top-level configuration.

Proposed Changes

  1. Input Validation:
    • Add early validation: if not isinstance(preference, str) or not preference.strip(): return "Error: Preference content cannot be empty".
    • Annotate preference, slot, and replace_memory_id with Optional[str] to allow graceful tool invocation when arguments are omitted or passed as None.
  2. Robust replace_memory_id Handling:
    • Guard with replacement = replace_memory_id.strip() if isinstance(replace_memory_id, str) else "" to prevent AttributeError.
  3. Enhanced UID Resolution:
    • Update _get_uid to check both configurable['user_id'] and top-level user_id / uid in both config and agent_config_context.
  4. Resilient Duplicate Hit Filtering:
    • Add if not isinstance(hit, dict): continue in preference_duplicate_message before key extraction.
  5. Session Resolution in Provenance:
    • Support top-level chat_session_id, session_id, and thread_id in _write_provenance.
  6. Hermetic Unit Tests:
    • Add backend/tests/unit/test_preference_tools_guards.py verifying empty/whitespace/None preference rejection, top-level UID resolution, safe replace_memory_id handling, non-dict duplicate hit resilience, and top-level session provenance.

Bounty Proposal

Proposed bounty: $50 USDC Payout address: 0xC345C07229307d4f1c7935aD2cAe48e390215808 (USDC Polygon/Base/Arbitrum)

Dimank92 avatar Oct 22 '26 05:10 Dimank92

Fix and hermetic test suite opened in PR #23216: https://github.com/BasedHardware/omi/pull/23216

Payout address (USDC Polygon / Base / Arbitrum): 0xC345C07229307d4f1c7935aD2cAe48e390215808

Dimank92 avatar Oct 22 '26 05:10 Dimank92

Thanks for putting this together. We aren't doing bounties at the moment, so we're closing the bounty proposal issues for now — thanks for the fix! 🙏

undivisible avatar Oct 22 '26 11:10 undivisible