Repository navigation
Supersedes #23296: fix(retrieval): harden app_tools schema validation, method normalization, and config extraction - #25629
omiagent[bot] wants to merge 1 commit into
Conversation
…ion, and config extraction
Git-on-my-level
left a comment
There was a problem hiding this comment.
Verdict: right direction, well-tested hardening — but not mergeable as-is: the Backend unit suite is red from three type-checker errors in this PR's own file, and one data-handling widening needs an explicit maintainer call.
Blocking — turns "Backend unit suite" (shard 1/8) green:
- backend/utils/retrieval/tools/app_tools.py:140 — pyright reportUnnecessaryIsInstance: the guard
isinstance(parameters, dict)contradicts the declaredparameters: Dict[str, Any]signature (app_tools.py:128). Widen the annotation toAny— as already done for_supplied_arguments(kwargs)at app_tools.py:186 — since callers really do pass arbitrary manifest JSON. - app_tools.py:333 — same error for
isinstance(config, dict)againstconfig: Optional[RunnableConfig]. - app_tools.py:337 —
configurable = configurable_val if isinstance(configurable_val, dict) else configassigns aRunnableConfiginto aDict[str, Any]-declared variable (reportAssignmentType); type itAnyor cast.
All three are annotation-only; the runtime logic itself is correct.
Verified substance (both changed files):
- backend/utils/retrieval/tools/app_tools.py — each guard is correct and has a matching test: non-dict parameters/properties/required (app_tools.py:140-146), malformed param schema degrading to Optional[str] (:152-154), non-string param names skipped (:150-151), non-dict mcp_oauth_tokens (:237), non-dict config returning a readable error (:333-334), method normalization with POST default (:380).
- backend/tests/unit/test_app_tools_resilience.py — covers exactly the hardened paths, including the uid alias fallbacks and DELETE payload-as-params against a mocked httpx client; assertions check both the returned error strings and the outgoing request kwargs.
Non-blocking behavior widenings beyond crash-hardening (maintainer-visible):
- uid resolution (app_tools.py:338) now also accepts configurable['uid'], config['user_id'], and config['uid']. This key set is the contract for whose data a third-party tool call acts on; the PR body flags it honestly, but expanding it should be an explicit maintainer decision, not a side effect of a hardening pass.
- transport fallback (app_tools.py:240): an explicitly-empty transport resolves to "sse" while an absent one defaults to "streamable_http" (models/app.py:106) — two different defaults for two kinds of missing. Intentional?
- app_tools.py:388: DELETE and other non-POST/PUT/PATCH methods now send the payload as query params (previously silently dropped). Reasonable fix, but a wire-behavior change for existing DELETE tools.
- Nit: a whitespace-only method normalizes to "" (app_tools.py:380);
method.strip().upper() or 'POST'would be safer.
Performance: O(1) isinstance guards only — no new queries or external calls. Security: malformed third-party manifests can no longer crash tool loading or a chat turn; the uid alias set is the one data-handling surface needing sign-off. Blast radius: only app-tool calls whose manifests are malformed; well-formed manifests behave exactly as before.
USER IMPACT: one broken third-party app tool definition will no longer break tool calls or chat turns for otherwise-healthy apps; everything else behaves the same.
Still wanted: the three annotation fixes to turn the suite green, a maintainer ruling on the accepted uid key set, and a one-liner on the sse-vs-streamable_http empty-transport intent. Leaving for human maintainer review specifically on the uid-alias contract; the rest is author-fixable.
by AI on behalf of David — if you need David’s attention urgently, please @Git-on-my-level and escalate with need human response.
|
Thanks @omiagent[bot] and the Omi team for reviving this and keeping original authorship! 🙌 I checked the CI run on Backend unit shard 1/8 — all unit tests passed, and the single failure was caused by 3 Pyright typing errors in �ackend/utils/retrieval/tools/app_tools.py:
Applying these small typing fixes will get Backend unit shard 1/8 100% green alongside the other 7 shards that already passed. |
What this does, in plain terms
Apps can define their own tools ("chat tools") that the Omi backend calls over HTTP on the user's behalf. Those definitions arrive from outside the repo, so malformed ones — a missing or non-dictionary parameter schema, a null HTTP method, a config object that isn't shaped as expected — used to raise
AttributeError/TypeErrorwhile loading tools or serving a chat response, failing the whole request.This change makes that path defensive: malformed tool definitions now register cleanly or return a readable per-tool error (e.g.
Error: Configuration not available for <tool>) instead of crashing tool loading or the chat turn. For users this means one broken app definition can no longer break tool calls for otherwise-healthy apps. Well-formed manifests behave exactly as before.What changed
_create_pydantic_model_from_schema: non-dictparametersbuilds an empty input model;properties/requiredare guarded against non-dict/non-iterable values; malformed individual parameter schemas degrade toOptional[str]instead of aborting registration; non-string/empty parameter names are skipped._supplied_arguments: non-dictkwargsreturns{}.create_app_tool:parameters/propertiesare dict-checked; MCPmcp_oauth_tokensis dict-checked;transportis normalized with an"sse"fallback (only fires for explicitly-empty values — theChatToolmodel default isstreamable_http)._call_tool_endpoint: non-dictconfigreturns a readable error instead ofAttributeError; uid resolution additionally accepts top-levelconfig['user_id']/config['uid']andconfigurable['uid']aliases (a compatibility widening beyond pure crash-hardening — the accepted alias set becomes an implicit contract);methodis normalized (None/whitespace/lowercase safe, defaultPOST); DELETE and other non-POST/PUT/PATCH methods now send the payload as query params instead of dropping it (GET already did; this extends the same routing to DELETE et al — note query strings commonly land in access logs).Credit: @ghazy2001 — supersedes #23296
This branch cherry-picks the author's commit from #23296 (
fix-app-tools-resilience-and-validation) with original authorship preserved in git history (ghazy2001 is the commit author; this bot is committer only). The original PR stalled without a maintainer review; the repo's AI reviewer verified the change at the original head (63d179d) as a good, correctly scoped hardening change with tests, leaning positive, and left it for human maintainer review. The code is carried forward unchanged so it can meet current CI and repo conventions; the human merge gate applies as always.Tests
backend/tests/unit/test_app_tools_resilience.py— 9 tests covering non-dict/malformed schema model creation, non-dictkwargs, non-dict parameters and transport defaults increate_app_tool, non-dict config and uid alias fallbacks in_call_tool_endpoint, and DELETE payload routing plus method normalization. Hermetic (httpx/circuit-breaker/db mocked).test_app_tools_resilience,test_app_tools_omitted_arguments,test_app_tools_status_cap,test_app_tool_method_null(counts via junitxml).scripts/backend-python-format --checkclean on both changed files.Product invariants affected
none
Failure class (fixes)
Failure-Class: none