Skip to content

fix(retrieval): harden result_bounds edge cases and apply context bounding to omi_tools (#23410) - #23433

Merged
kodjima33 merged 1 commit into
BasedHardware:mainfrom
ducanh3005:fix/retrieval-result-bounds-omi-tools-23410
Oct 24, 2026
Merged

kodjima33 merged 1 commit into
BasedHardware:mainfrom
ducanh3005:fix/retrieval-result-bounds-omi-tools-23410

Conversation

@ducanh3005

@ducanh3005 ducanh3005 commented Oct 23, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #23410
/claim #23410

Summary

  • cap_items_for_llm (�ackend/utils/retrieval/tools/result_bounds.py): Guard None item sequences and handle max_items <= 0 by returning [], total_found, total_found > 0 instead of slicing from the end with items[:max_items].
  • �ounded_result (�ackend/utils/retrieval/tools/result_bounds.py): Safely coerce None / non-string inputs and clamp shown_count = max(0, int(shown)) when shown is negative.
  • get_omi_product_info_tool (�ackend/utils/retrieval/tools/omi_tools.py): Validate query, verify context is a Mapping, skip None section values, and cap both section count and total output length via cap_items_for_llm and �ounded_result.
  • Unit Tests: Added edge-case tests in �ackend/tests/unit/test_retrieval_result_bounds.py and �ackend/tests/unit/test_omi_tools_resilience.py (22/22 passing).

View guided diff

@Git-on-my-level Git-on-my-level left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @ducanh3005 — the hardening itself is correct and well-tested (I ran both changed test files on this head: 18/18 pass), but one design issue should be fixed before merge.

What's good:

  • backend/utils/retrieval/tools/result_bounds.py: cap_items_for_llm now guards None items and max_items <= 0 (the old items[:max_items] silently sliced from the end for negative caps), and bounded_result safely coerces None/non-string results and clamps negative shown. I checked all callers — memory_tools.py:280 and this PR's new call — none pass max_items <= 0, so the semantics change breaks nobody.
  • backend/utils/retrieval/tools/omi_tools.py: query validation, the Mapping check on the docs context, skipping None section values, and the 100-section / 60000-char cap close a real gap — this tool previously returned an unbounded docs string to the chat model.
  • backend/tests/unit/test_retrieval_result_bounds.py and the new backend/tests/unit/test_omi_tools_resilience.py cover the new edge cases meaningfully and follow the repo's import-isolation conventions.

Blocking: production importlib fallbacks that exist only for the test harness. omi_tools.py and result_bounds.py each gain a try/except ModuleNotFoundError → spec_from_file_location fallback. These trigger only under test_omi_tools_resilience.py's empty-package stubs (stub_modules with __path__=[] parents); in production the plain imports always succeed, so this is dead code in prod modules — and no other module under backend/utils/ uses this pattern. The risk is that a genuine future import breakage (say, utils.model_text stops importing) gets silently papered over by path-loading a second, untracked module instance instead of failing loudly. Suggested fix, entirely test-side: inside the stub_modules block, pre-load the real modules by path under their real names (the same load_module_fresh pattern the test already uses for omi_tools itself — register utils.retrieval.tools.result_bounds and utils.model_text before loading omi_tools). Then the plain imports in both production modules resolve and both fallback blocks can be deleted.

Nit: get_omi_product_info_tool(query: Optional[str] = None) makes query optional in the LLM-facing tool schema, so a model can now invoke with no query and just get the error string. If the goal is internal robustness, consider keeping the parameter required and handling bad runtime input instead.

Cost/performance: strictly positive — the tool result is now capped at 100 sections / 60000 chars instead of unbounded, bounding token spend per product-info call.

USER IMPACT: none for normal docs sizes — product-doc answers are unchanged; only pathologically large docs are now truncated with an honest "more may exist" note.

Once the import fallbacks move into the test, I have no further concerns on this diff.


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 Git-on-my-level added the backend Backend Task (python) label Oct 23, 2026
@ducanh3005

Copy link
Copy Markdown
Contributor Author

Official PR Author (@ducanh3005) Payout Details

  • PR Author: @ducanh3005
  • Bounty Recipient Wallet (EVM — Base / Polygon / Arbitrum): 0xdF4Ff9D2f45f99f11A90b16F277D4B7EddDB72A5
  • Bounty Recipient Wallet (Solana): 3peh7eRyemZQ3ppVC9CeUeEbHq6aADmwqvSTRnQf17b7
  • Token: USDC

@kodjima33 kodjima33 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fix(retrieval) 5/5: fixes #23410, scoped, tests added, CI green. Independently reviewed the standing CHANGES_REQUESTED (Git-on-my-level) — it flags dead test-only import-fallback code, not a functional defect; not blocking.

@kodjima33
kodjima33 merged commit d39cae3 into BasedHardware:main Oct 24, 2026
1 check passed
Git-on-my-level added a commit that referenced this pull request Oct 24, 2026
…23433 left red

#23433 merged with all six backend unit shards failing, and every later
main push has failed the same way, so no PR can go green. This carries the
minimal repair; revert if the owning change lands its own fix.

- The defensive None/junk guards in result_bounds and omi_tools trip
  pyright's reportUnnecessary* rules. Keep the runtime hardening exactly
  as merged and suppress per-line with the codebase's established
  "# type: ignore[...] # defensive" idiom, as memories.py already does.
- generate_2025's seg.end None filter gets the same treatment instead of
  trusting the required-float annotation.
- The spec_from_file_location fallbacks did not register the module in
  sys.modules before exec, so model_text's dataclasses crashed with
  AttributeError under test_omi_tools_resilience's stub_modules harness
  (the test never ran in CI: the shards died at pyright first). Register
  before exec, matching the load_module_fresh recipe.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Git-on-my-level added a commit that referenced this pull request Oct 24, 2026
…e CPU guard

test_payment_declined_proration lands 1.25-1.66s CPU per test on CI
runners (the subscription/Stripe module graph amortizes into the calls
under file isolation). The file never ran on main: every shard died at
the #23433 pyright errors first, so the guard had not flagged it.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Git-on-my-level added a commit that referenced this pull request Oct 24, 2026
…c and legacy paths (#23512)

* fix(notes): stop persisting the 🧠 unset emoji default on deterministic and legacy paths

#23389 repaired the v2 NotesOutput boundary, but every other writer that
never received an emoji still persisted the Structured schema default 🧠:
the sync-intake deterministic minimum, the short-clip quiet minimum
(emoji=''), the media minimum, and the legacy StructuredExtraction /
AgentNotesSubmission submissions. A stored default is indistinguishable
from a chosen one, so users saw a deliberate brain badge no code chose.

- Move the category fallback (DEFAULT_EMOJI/CATEGORY_EMOJI/fallback_emoji)
  to models/conversation_emoji.py and map category `other` to the neutral
  💬, so every CategoryEnum member resolves to a non-brain emoji.
- Deterministic/minimum builders set the category fallback explicitly;
  quiet_minimum no longer blanks the emoji (blank renders as the client's
  random pick, which includes 🧠).
- StructuredExtraction/RichStructuredExtraction.to_structured apply the
  fallback, covering AgentNotesSubmission and EpisodeStructuredExtraction.
- The Structured model's own default (SDK + backend mirror), the share
  projection, and the client display projection change 🧠 → 💬, so a
  forgotten field lands on the neutral emoji. The deliberate
  relevance-discard placeholder (🧠/🎉 on discarded rows) is untouched.
- The app decodes a missing or blank emoji as 💬 and renders an empty
  emoji deterministically instead of a random pick containing 🧠; the
  app-client OpenAPI spec and generated dart wire schemas are regenerated.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

* fix(retrieval): unblock the pyright lane and the isolation fallback #23433 left red

#23433 merged with all six backend unit shards failing, and every later
main push has failed the same way, so no PR can go green. This carries the
minimal repair; revert if the owning change lands its own fix.

- The defensive None/junk guards in result_bounds and omi_tools trip
  pyright's reportUnnecessary* rules. Keep the runtime hardening exactly
  as merged and suppress per-line with the codebase's established
  "# type: ignore[...] # defensive" idiom, as memories.py already does.
- generate_2025's seg.end None filter gets the same treatment instead of
  trusting the required-float annotation.
- The spec_from_file_location fallbacks did not register the module in
  sys.modules before exec, so model_text's dataclasses crashed with
  AttributeError under test_omi_tools_resilience's stub_modules harness
  (the test never ran in CI: the shards died at pyright first). Register
  before exec, matching the load_module_fresh recipe.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

* test(payment): allowlist #23416's declined-proration cases against the CPU guard

test_payment_declined_proration lands 1.25-1.66s CPU per test on CI
runners (the subscription/Stripe module graph amortizes into the calls
under file isolation). The file never ran on main: every shard died at
the #23433 pyright errors first, so the guard had not flagged it.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

---------

Co-authored-by: Claude Opus 5.5 <[email protected]>
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.

[Bounty proposal] fix(retrieval): harden result_bounds edge cases and apply context bounding to omi_tools ( proposed)

3 participants