Skip to content

fix(retrieval): harden search_files_tool validation, config resilience, and numeric coercion - #23290

Closed
ghazy2001 wants to merge 1 commit into
BasedHardware:mainfrom
ghazy2001:fix-file-tools-resilience-and-validation
Closed

ghazy2001 wants to merge 1 commit into
BasedHardware:mainfrom
ghazy2001:fix-file-tools-resilience-and-validation

Conversation

@ghazy2001

@ghazy2001 ghazy2001 commented Oct 22, 2026 •

Copy link
Copy Markdown

Failure-Class: none

Summary

Hardens backend/utils/retrieval/tools/file_tools.py (search_files_tool) against model hallucinated parameters, config structure variations, and numeric coercion faults:

  1. Defensive Question Validation:

    • Validates question is a non-empty, non-whitespace string, preventing empty/invalid queries from being sent to downstream file-chat processors.
  2. Config Resilience & Top-Level / UID Fallbacks:

    • Safely extracts uid and chat_session_id from nested configurable as well as top-level config, supporting both user_id and uid keys.
    • Replaces fragile None.get() pattern with safe dict lookups, avoiding unhandled AttributeError.
    • Validates that both uid and chat_session_id are resolved before querying session data.
  3. file_ids Normalization & Type Safety:

    • Normalizes single string arguments (e.g. file_ids="file_123") into a single-element list, avoiding Python string character iteration bugs.
    • Accepts tuples, sets, and lists while stripping whitespace and filtering empty/None elements.
    • Safely handles non-iterable arguments without raising TypeError.
  4. Numeric Coercion & Bounds for Video Windows:

    • Added _parse_seconds() helper to parse numeric string arguments (e.g. start_seconds="10.5") to float while filtering out non-numeric inputs, NaN, and Inf.
    • Clamps start time to >= 0.0 and ensures valid (start, end) spans.
  5. Hermetic Test Suite:

    • Added backend/tests/unit/test_file_tools_resilience.py with 11 comprehensive unit tests (11/11 passing hermetically).
    • Verified 100% backwards compatibility with test_file_tools_sanitization.py (4/4 passing).

Bounty Proposal ($50 USD)

  • Proposed Tier: $50 USD
  • Settlement: Available for backend & tool engineering contracts ([email protected]).

View guided diff

@Git-on-my-level

Copy link
Copy Markdown
Member

Solid, well-scoped hardening — the production changes are correct and the new tests are genuinely hermetic; leaving for a maintainer to merge. Findings on dd1558b:

backend/utils/retrieval/tools/file_tools.py

  • Real bug fixed: under the old if file_ids and len(file_ids) > 0 branch, a bare string like file_ids="file_100" was iterated character-by-character ('f','i','l',...), so it never matched session file ids and every single-file query returned "The specified files are not available in this chat session." The new normalization (str → single-element list, tuple/set/list cleaned of None/whitespace) fixes that.
  • _parse_seconds() is the right shape: rejects NaN/Inf and non-numeric strings, and the reworked window math keeps the clamp (start >= 0.0) and the ±5s defaults. Previously a garbage start_seconds string raised ValueError into the exception sanitizer instead of degrading to a full-file query.
  • Config fallback chain (configurable.user_id → configurable.uid → top-level keys) removes the None.get() fragility. Note the or-chaining treats empty-string ids as missing — fine for ids, just worth knowing.
  • Minor deliberate behavior change to be aware of: a config missing both uid and chat_session_id now returns "Error: Configuration error" instead of "No active chat session. Files are not available." — covered by tests, reasonable.

backend/tests/unit/test_file_tools_resilience.py

  • Correctly mirrors the existing hermetic harness convention (test_file_tools_sanitization.py), stubbing langchain_core/database/models so it runs without backend deps. The ENCRYPTION_SECRET default matches the shared repo-wide test value. I ran the suite in isolation: 11/11 pass.

Security: improves input validation on an agent-facing tool; no new data exposure or dependency surface. Performance: pure local validation, negligible. Blast radius: single tool (search_files_tool).

USER IMPACT: none — internal change; malformed agent tool calls now degrade gracefully (full-file search / clear error) instead of failing.

Nothing blocking from my side. Optional nice-to-have: a case for set-typed file_ids and for file_ids containing non-string members (int coerced via str(fid).strip()) to lock in the coercion contract.


by AI on behalf of David — if you need David’s attention urgently, please @Git-on-my-level and escalate with need human response.

@aryanorastar

Copy link
Copy Markdown
Collaborator

Closing #23290 at dd1558b5f4e0875ac04f9aff08c9bdb6fd08300a as a later copy of #23200, which remains the open vehicle for the same change. Not merged.

@github-actions

Copy link
Copy Markdown
Contributor

Hey @ghazy2001 👋

Thank you so much for taking the time to contribute to Omi! We truly appreciate you putting in the effort to submit this pull request.

After careful review, we've decided not to merge this particular PR. Please don't take this personally — we genuinely try to merge as many contributions as possible, but sometimes we have to make tough calls based on:

  • Project standards — Ensuring consistency across the codebase
  • User needs — Making sure changes align with what our users need
  • Code best practices — Maintaining code quality and maintainability
  • Project direction — Keeping aligned with our product principles and locked invariants

Before your next PR, please skim:

  • PRODUCT.md — product north star
  • Product invariants — locked rules (shared chat, memory tiers, agent control plane, integrations, brand)

If this was declined for direction or taste, maintainers should cite an invariant ID or open a proposed one — ask if that citation is missing.

Your contribution is still valuable to us, and we'd love to see you contribute again in the future! If you'd like feedback on how to improve this PR or want to discuss alternative approaches, please don't hesitate to reach out.

Thank you for being part of the Omi community!

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

Labels

backend Backend Task (python)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants