Skip to content

fix(tools): validate Brave search parameters - #1245

Merged
penso merged 3 commits into
moltis-org:mainfrom
rubenssoto:fix/brave-search-parameter-validation
Aug 26, 2026
Merged

fix(tools): validate Brave search parameters#1245
penso merged 3 commits into
moltis-org:mainfrom
rubenssoto:fix/brave-search-parameter-validation

Conversation

@rubenssoto

@rubenssoto rubenssoto commented Aug 25, 2026

Copy link
Copy Markdown
Contributor

Summary

  • expose Brave localization parameters only when the active search provider is Brave, with provider-supported enums in the tool schema
  • normalize country, search language, UI language, and freshness values before constructing the Brave request; unsupported markets fall back to ALL
  • infer zh-hans or zh-hant from supported Chinese country/UI regions when callers provide the generic zh language
  • keep cached Brave results separated by normalized localization, freshness, and Accept-Language context
  • retry an HTTP 422 once without optional Brave filters so a stale or rejected localization value does not abort the search
  • cover the complete Brave HTTP retry flow with a mock server, including request sanitization, header preservation, retry selection, and retry-response errors

Validation

Completed

  • cargo fmt --all -- --check
  • git diff --check
  • ./scripts/check-file-size.sh
  • cargo test --locked -p moltis-tools --all-features web_search -- --nocapture (43 passed)
  • cargo clippy --locked -p moltis-tools --all-features --all-targets -- -D warnings
  • ./scripts/local-validate.sh 1245 at a035d594 (partial: fmt, Biome, TypeScript, i18n, zizmor, install checks, file-size, lockfile, and web assets passed)
  • full release Docker image build at a035d594; the cache/Chinese normalization follow-up was validated with targeted tests and clippy

Remaining

  • remaining workspace-wide local validation — attempted locally; the lint build is blocked by this host's native toolchain while compiling the unrelated llama-cpp-sys-2 dependency
  • CI

Manual QA

  • built and deployed moltis-local:20260824.01-obscura-v021-brave-pr1245; the gateway started normally and /health returned HTTP 200
  • sent the formerly failing Paraguay/Spanish combination as the sanitized request generated by the implementation (country=ALL, search_lang=es, invalid ui_lang omitted, freshness=pw) from the running container; Brave returned HTTP 200 with three results
  • mocked HTTP coverage verifies the first request, one filter-free retry after 422, preserved authentication/localization headers, successful response parsing, no retry when no optional filters exist, and propagation of a failed retry response

Brave accepts fixed country and language values, but web_search exposed unconstrained strings and forwarded them verbatim. That allowed model-generated localization values to fail with HTTP 422.

Scope the localization schema to the Brave provider, normalize optional values before sending them, and retry once without optional filters when Brave still rejects the request.
@rubenssoto
rubenssoto force-pushed the fix/brave-search-parameter-validation branch from 78db36c to 43dd17c Compare August 25, 2026 17:55
@greptile-apps

greptile-apps Bot commented Aug 25, 2026

Copy link
Copy Markdown
Contributor

Greptile Summary

The PR validates and normalizes Brave-specific search parameters while improving retry behavior and cache separation.

  • Exposes provider-supported localization parameters only for Brave.
  • Normalizes localization, freshness, and Chinese script variants before request construction.
  • Retries rejected optional filters once without those filters.
  • Separates cached results by normalized Brave parameters and Accept-Language.
  • Adds mock HTTP coverage for successful and failed retry paths.

Confidence Score: 5/5

The PR appears safe to merge.

No blocking failure remains within the scope of this follow-up review, and the previously requested retry-path coverage is present at the current head.

Important Files Changed

Filename Overview
crates/tools/src/web_search.rs Integrates normalized Brave parameters into schema generation, request execution, cache keys, and fully mocked retry-path tests.
crates/tools/src/web_search/brave.rs Adds provider-local enum definitions and normalization logic for Brave localization and freshness parameters.

Reviews (2): Last reviewed commit: "fix(tools): preserve Brave search contex..." | Re-trigger Greptile

Comment thread crates/tools/src/web_search.rs
Exercise the Brave HTTP path with a mock server so the 422 fallback cannot regress silently. Verify sanitized first-request parameters, a filter-free retry, preserved headers, successful retry parsing, no retry without filters, and propagation of the retry response error.
Include normalized localization, freshness, and accepted language in Brave cache keys so distinct searches cannot reuse mismatched results. Also infer simplified or traditional Chinese from supported Brave regions.
@rubenssoto

Copy link
Copy Markdown
Contributor Author

Addressed two additional correctness cases in 7ad57b78:

  • Brave cache keys now include the normalized country, search language, UI language, freshness, and Accept-Language, preventing localized/freshness variants of the same query from reusing mismatched cached results.
  • Generic zh now resolves to zh-hans for CN and zh-hant for HK/TW, using the supported country/UI locale context.

Added regression coverage for normalized cache-key equivalence/separation and both Chinese scripts. Validation passes: 43 targeted web_search tests, clippy with all features/all targets and -D warnings, fmt, diff check, and file-size check.

@greptile-apps please re-review the updated head.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants