Repository navigation
fix(file-tools): guard string end_seconds coercion, fallback top-level config, and validate question (#23199) - #23200
Conversation
Git-on-my-level
left a comment
There was a problem hiding this comment.
Thanks @Dimank92 — the production change here is correct and well scoped; two blocking issues below are both mechanical and quick to fix.
What's right (verified by execution against this head): _parse_seconds correctly rejects booleans/NaN/Inf/non-numerics and clamps negatives; the new window math handles end_seconds="15" → (10.0, 15.0), start_seconds="10.5" → (10.5, 15.5), and inverted spans clamp to start. The provider-400 error-classification block (_provider_400_names_request_field etc.) is unchanged from main — good restraint.
Blocking 1 — the new test suite fails on its own fixtures. backend/tests/unit/test_file_tools_coercion_and_guards.py: every mock session dict ({"id": "s1", "file_ids": [...]}) is missing created_at, which is a required field of the real ChatSession model (backend/models/chat.py:36, created_at: datetime, no default). ChatSession(**session_data) raises ValidationError, the tool's except ValueError catches it, and it returns "Session error: Invalid session parameters." before FileChatTool is ever constructed — so 5 of the 8 tests assert unreachable behavior. I ran pytest tests/unit/test_file_tools_coercion_and_guards.py at this head in a clean worktree: 3 passed, 5 failed, all five with this same root cause. Fix: add created_at (e.g. a fixed datetime.now(timezone.utc) value) to each mock session.
Blocking 2 — file_tools.py was re-saved with a UTF-8 BOM and CRLF line endings. Verified at byte level: the head blob starts ef bb bf and all 255 lines end \r\n; base is clean UTF-8/LF. That turns a ~40-line logical change into a full-file rewrite (+255/−227), buries the real diff, and fails scripts/backend-python-format --check. Please re-emit the file without BOM/CRLF (or restore the original and reapply your edits with your editor set to UTF-8 no-BOM, LF).
Performance/cost/security: no new call paths, no external API changes, no dependency/workflow/secret surface — nothing material beyond the above.
USER IMPACT: none once landed — this hardens an internal agent tool against malformed LLM-supplied arguments; users would only notice fewer silent failures when asking about attached files/videos.
Action items: (1) add created_at to all mock sessions in the new test file; (2) re-emit file_tools.py as UTF-8 no-BOM with LF endings; (3) re-run backend/test.sh (or at least the new test file) and paste the result into the PR.
by AI on behalf of David — if you need David’s attention urgently, please @Git-on-my-level and escalate with need human response.
|
@Git-on-my-level @undivisible Ready for workflow run approval & review:
Bounty Proposal: $50 USD (#23140) |
5875fe2 to
2ef0bde
Compare
|
@Git-on-my-level Addressed all review feedback for PR #23200:
Latest clean commit 2ef0bde pushed to branch. |
Git-on-my-level
left a comment
There was a problem hiding this comment.
Since my last review: addressed (both blockers — mock sessions now include created_at and all 8 tests pass when run at this head; file_tools.py re-emitted as clean UTF-8/LF, byte-verified, targeted diff restored) · still open (format check, see below) · newly introduced (nothing functional).
The production change is correct and verified — one mechanical formatting pass is all that remains. I ran pytest tests/unit/test_file_tools_coercion_and_guards.py at this head: 8/8 passed, and the window math behaves exactly as the tests claim (end_seconds="15" → (10.0, 15.0), start_seconds="10.5" → (10.5, 15.5), non-numeric input → no window).
What I verified in this diff:
backend/utils/retrieval/tools/file_tools.py—_parse_seconds(lines 157-163) correctly rejects bools/NaN/Inf/non-numerics and clamps negatives to 0.0; the fallback window math (lines 213-217) preserves the oldor-based semantics for zero values. The top-level config fallback (lines 161-171) and theconfigurable-type guard are sound, and the question guard (lines 177-178) short-circuits before any DB or LLM work.backend/tests/unit/test_file_tools_coercion_and_guards.py— good coverage of the exact failure modes in #23199, including the stringfile_idscoercion; mocks no longer construct invalidChatSessions.
The one remaining blocker is mechanical: scripts/backend-python-format --check (run by CI in .github/workflows/repo-checks.yml) still fails on four spots, all on lines this PR adds — the chat_session_id = ... and start = ... lines in file_tools.py exceed the 120-char limit, there's a stray blank line before _parse_seconds, a trailing blank line at EOF, and one overlong assert in the test file.
Security: this hardens an LLM-facing tool against malformed arguments — strictly an improvement, no new call paths or external surfaces. Performance/cost: nothing material.
USER IMPACT: none — internal change; users would only notice fewer silent failures when asking about attached files or videos.
Action items: (1) run scripts/backend-python-format --write backend/utils/retrieval/tools/file_tools.py backend/tests/unit/test_file_tools_coercion_and_guards.py and push; (2) optional nit — import pytest (line 5) and from models.chat import ChatSession (line 8) in the test file are unused and can be dropped in the same pass. After the format pass this looks mergeable from my side.
by AI on behalf of David — if you need David’s attention urgently, please @Git-on-my-level and escalate with need human response.
Dismissing my earlier changes-requested review: both blocking items (mock sessions missing created_at; BOM/CRLF rewrite of file_tools.py) are verified resolved on head 2ef0bde — 8/8 tests pass and the file is clean UTF-8/LF. The follow-up review on the current head tracks the remaining format-check fix.
|
@Git-on-my-level Thank you for the detailed review! All four formatting action items have been addressed in the latest push:
Ready for final check and merge. Bounty Disbursement Requisites:
|
a4278be to
f567f72
Compare
|
@Git-on-my-level Rebased cleanly onto latest
Ready for final approval and merge! Bounty Disbursement Requisites:
|
f567f72 to
e4fd8ea
Compare
|
@Git-on-my-level Thank you for the thorough review! All action items from your review have been addressed and the branch is rebased onto latest
All 8 tests pass: Ready for CI workflow approval and merge. Payout Address: |
Stale: the formatting issues raised here (overlong lines, stray/trailing blank lines, overlong assert) are fixed at head e4fd8ea — repo format check (black 26.5.1, line-length 120) now passes on both changed files, verified by execution; tests re-run 8/8.
|
Since my last review: addressed (the one remaining blocker — formatting; plus the unused-imports nit) · still open (nothing) · newly introduced (nothing). Everything from my earlier reviews is now resolved at this head. Verified by execution: The two commits since my last review are style-only — the production logic stands as previously verified: USER IMPACT: none — internal change; users would only notice fewer silent failures when asking about attached files or videos. Nothing further from my side — this looks ready for a maintainer merge decision. by AI on behalf of David — if you need David’s attention urgently, please @Git-on-my-level and escalate with |
|
Hey @Dimank92 👋 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:
Before your next PR, please skim:
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! |
Resolves #23199
Summary
In
backend/utils/retrieval/tools/file_tools.py, invokingsearch_files_toolexhibits several resilience and input-type failure modes:end_secondsTypeError:When LLM tool calls supply
end_secondsas a string (e.g.,"15") withstart_seconds=None, line 203 previously executed:start = start_seconds if start_seconds is not None else max(0, (end_seconds or 0) - 5)In Python,
("15" or 0) - 5raises an unhandledTypeError: unsupported operand type(s) for -: 'str' and 'int'.user_idandchat_session_idare only checked incfg.get('configurable', {}). In standard LangChain/LangGraph invocations or alternate runnables where configuration provides top-leveluser_idorchat_session_id, the tool previously defaulted to empty strings without fallback.Non-numeric strings or invalid float values (such as
nan,inf, or malformed text) passed into timestamp fields can cause unexpected math or conversion errors in downstream file window slicing.Empty or whitespace-only questions were passed down to vision/multimodal LLM processing instead of returning an immediate validation message.
file_idscoercion:If an LLM supplies a single string
file_ids="file_123"instead of a list, iterating or testing membership treats the string as a sequence of single characters unless normalized to[file_ids].Key Changes
_parse_seconds):Added
_parse_secondshelper to safely coerce integers, floats, or numeric strings into non-negative finite floats, while cleanly handlingNone, booleans, non-numeric strings,inf, andnan.Replaced direct subtraction with numeric bounds calculation
start = parsed_start if parsed_start is not None else max(0.0, (parsed_end if parsed_end is not None else 0.0) - 5.0)andend = max(start, parsed_end if parsed_end is not None else start + 5.0).Added fallback to
cfg.get('user_id')andcfg.get('chat_session_id')when not found inconfigurable.Early validation returns
"Please provide a question about the files."for empty/whitespace questions, and single stringfile_idsis normalized into a list.Added
backend/tests/unit/test_file_tools_coercion_and_guards.py(8 passing unit tests) covering:_parse_secondswith ints, floats, numeric strings, negative values, booleans, and text.end_secondsandstart_secondswithout TypeError.Product invariants affected
none
UI evidence
UI-Evidence: none -- internal logic and test suite only; no UI change.
Failure class (fixes)
Failure-Class: none