omi
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)
Summary
In backend/utils/retrieval/tools/preference_tools.py, several edge cases in agent preference storage and retrieval cause runtime exceptions or state pollution:
- Empty / None / Whitespace-only
preferenceparameter: Insave_user_preference_tool:- If
preferenceisNone,preference.split()in_write_provenancecrashes withAttributeError: 'NoneType' object has no attribute 'split'. - If
preferenceis 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
preferenceis passed asNoneor omitted by an LLM agent withoutOptional[str] = Noneannotation.
- If
- Missing top-level
user_id/uidfallback in_get_uid:_get_uidonly checkedconfigurable['user_id']. When callers or framework adapters supplyuser_idoruidat the top level of the config dict (e.g.{"user_id": "..."}),_get_uidreturned an empty string, causing premature"Error: Could not determine user ID"failures. replace_memory_idcrashes on non-string /Noneinputs:replace_memory_id.strip()crashes withAttributeError: 'NoneType' object has no attribute 'strip'if an agent passesreplace_memory_id=Noneor an integer.preference_duplicate_messagecrashes on non-dict items: If search results from canonical memory providers or mock harnesses contain non-dict elements (e.g.None),hit.get(...)crashes withAttributeError: 'NoneType' object has no attribute 'get'._write_provenancemissing session resolution fallback:_write_provenancedid not check top-levelchat_session_id,session_id, orthread_id, defaulting to'direct-agent-tool'even when a session ID was present in the top-level configuration.
Proposed Changes
- Input Validation:
- Add early validation:
if not isinstance(preference, str) or not preference.strip(): return "Error: Preference content cannot be empty". - Annotate
preference,slot, andreplace_memory_idwithOptional[str]to allow graceful tool invocation when arguments are omitted or passed asNone.
- Add early validation:
- Robust
replace_memory_idHandling:- Guard with
replacement = replace_memory_id.strip() if isinstance(replace_memory_id, str) else ""to preventAttributeError.
- Guard with
- Enhanced UID Resolution:
- Update
_get_uidto check bothconfigurable['user_id']and top-leveluser_id/uidin bothconfigandagent_config_context.
- Update
- Resilient Duplicate Hit Filtering:
- Add
if not isinstance(hit, dict): continueinpreference_duplicate_messagebefore key extraction.
- Add
- Session Resolution in Provenance:
- Support top-level
chat_session_id,session_id, andthread_idin_write_provenance.
- Support top-level
- Hermetic Unit Tests:
- Add
backend/tests/unit/test_preference_tools_guards.pyverifying empty/whitespace/None preference rejection, top-level UID resolution, safereplace_memory_idhandling, non-dict duplicate hit resilience, and top-level session provenance.
- Add
Bounty Proposal
Proposed bounty: $50 USDC
Payout address: 0xC345C07229307d4f1c7935aD2cAe48e390215808 (USDC Polygon/Base/Arbitrum)
Fix and hermetic test suite opened in PR #23216: https://github.com/BasedHardware/omi/pull/23216
Payout address (USDC Polygon / Base / Arbitrum):
0xC345C07229307d4f1c7935aD2cAe48e390215808
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! 🙏