-
Notifications
You must be signed in to change notification settings - Fork 715
Sprint 4 [FIX] Backend fixes: UN-2900, UN-2902, UN-2953, UN-3038, UN-3133, UN-3176, UN-3333 #2257
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
be9d774
da198d0
aa0df6f
aa4f086
f45d4e0
c830880
1f8a4a7
68eaec4
fc2962c
df75e37
07ed225
eb9ff6a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -486,33 +486,57 @@ def validate_adapter_permissions( | |
| adapter_ids: set[str] = set() | ||
|
|
||
| for llm in tool.properties.adapter.language_models: | ||
| adapter_id = None | ||
| if llm.is_enabled and llm.adapter_id: | ||
| adapter_id = tool_meta[llm.adapter_id] | ||
| adapter_id = tool_meta.get(llm.adapter_id) | ||
| elif llm.is_enabled: | ||
| adapter_id = tool_meta[AdapterPropertyKey.DEFAULT_LLM_ADAPTER_ID] | ||
| adapter_id = tool_meta.get(AdapterPropertyKey.DEFAULT_LLM_ADAPTER_ID) | ||
|
|
||
| adapter_ids.add(adapter_id) | ||
| # UN-2953: a tool instance may carry "" for an adapter id. | ||
| # Adding it made the schema validator compare "" against the | ||
| # UUID enum and raise, once per validation pass. | ||
| if adapter_id: | ||
| adapter_ids.add(adapter_id) | ||
| for vdb in tool.properties.adapter.vector_stores: | ||
| adapter_id = None | ||
| if vdb.is_enabled and vdb.adapter_id: | ||
| adapter_id = tool_meta[vdb.adapter_id] | ||
| adapter_id = tool_meta.get(vdb.adapter_id) | ||
| elif vdb.is_enabled: | ||
| adapter_id = tool_meta[AdapterPropertyKey.DEFAULT_VECTOR_DB_ADAPTER_ID] | ||
| adapter_id = tool_meta.get( | ||
| AdapterPropertyKey.DEFAULT_VECTOR_DB_ADAPTER_ID | ||
| ) | ||
|
|
||
| adapter_ids.add(adapter_id) | ||
| # UN-2953: a tool instance may carry "" for an adapter id. | ||
| # Adding it made the schema validator compare "" against the | ||
| # UUID enum and raise, once per validation pass. | ||
| if adapter_id: | ||
| adapter_ids.add(adapter_id) | ||
| for embedding in tool.properties.adapter.embedding_services: | ||
| adapter_id = None | ||
| if embedding.is_enabled and embedding.adapter_id: | ||
| adapter_id = tool_meta[embedding.adapter_id] | ||
| adapter_id = tool_meta.get(embedding.adapter_id) | ||
| elif embedding.is_enabled: | ||
| adapter_id = tool_meta[AdapterPropertyKey.DEFAULT_EMBEDDING_ADAPTER_ID] | ||
| adapter_id = tool_meta.get( | ||
| AdapterPropertyKey.DEFAULT_EMBEDDING_ADAPTER_ID | ||
| ) | ||
|
|
||
| adapter_ids.add(adapter_id) | ||
| # UN-2953: a tool instance may carry "" for an adapter id. | ||
| # Adding it made the schema validator compare "" against the | ||
| # UUID enum and raise, once per validation pass. | ||
| if adapter_id: | ||
| adapter_ids.add(adapter_id) | ||
| for text_extractor in tool.properties.adapter.text_extractors: | ||
| adapter_id = None | ||
| if text_extractor.is_enabled and text_extractor.adapter_id: | ||
| adapter_id = tool_meta[text_extractor.adapter_id] | ||
| adapter_id = tool_meta.get(text_extractor.adapter_id) | ||
| elif text_extractor.is_enabled: | ||
| adapter_id = tool_meta[AdapterPropertyKey.DEFAULT_X2TEXT_ADAPTER_ID] | ||
| adapter_id = tool_meta.get(AdapterPropertyKey.DEFAULT_X2TEXT_ADAPTER_ID) | ||
|
|
||
| adapter_ids.add(adapter_id) | ||
| # UN-2953: a tool instance may carry "" for an adapter id. | ||
| # Adding it made the schema validator compare "" against the | ||
| # UUID enum and raise, once per validation pass. | ||
| if adapter_id: | ||
| adapter_ids.add(adapter_id) | ||
|
|
||
| ToolInstanceHelper.validate_adapter_access(user=user, adapter_ids=adapter_ids) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [High] [Lens 13 — Testing] — This permission-check rewrite ships with zero testsEvery
Fix — three cases against a stubbed
Confidence: High. |
||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -13,6 +13,7 @@ | |
| from unstract.connectors.databases.exceptions import ( | ||
| BigQueryForbiddenException, | ||
| BigQueryNotFoundException, | ||
| BigQueryValueException, | ||
| ColumnMissingException, | ||
| ) | ||
| from unstract.connectors.databases.sql_safety import ( | ||
|
|
@@ -106,13 +107,14 @@ | |
| if data == 0: | ||
| return 0.0 | ||
|
|
||
| # Limit total significant figures to 15 for IEEE 754 compatibility | ||
| # BigQuery PARSE_JSON requires values that round-trip cleanly | ||
| # For large numbers (like Unix timestamps), this reduces decimal precision | ||
| # For small numbers (like costs), full precision is preserved | ||
| magnitude = math.floor(math.log10(abs(data))) + 1 | ||
| safe_decimals = max(0, 15 - magnitude) | ||
| return float(f"{data:.{safe_decimals}f}") | ||
| # Limit total significant figures to 15 for IEEE 754 compatibility. | ||
| # BigQuery PARSE_JSON requires values that round-trip cleanly. | ||
| # | ||
| # UN-3176: `g` asks for significant figures directly. The previous | ||
| # form asked for a DECIMAL-place count derived from the magnitude, | ||
| # which is not the same quantity and drifted from 15 sig figs at | ||
| # both ends of the range. | ||
| return float(f"{data:.15g}") | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [High] [Lens 3, 5] —
|
||
|
|
||
| elif isinstance(data, dict): | ||
| return {k: BigQuery._sanitize_for_bigquery(v) for k, v in data.items()} | ||
|
|
@@ -318,6 +320,17 @@ | |
| detail=e.message, table_name=table_name | ||
| ) from e | ||
| except google.api_core.exceptions.BadRequest as e: | ||
| # UN-3176: BigQuery returns BadRequest for VALUE-level failures as | ||
| # well as for schema mismatches. Mapping them all to | ||
| # ColumnMissingException told users to check their columns when the | ||
| # columns were fine (e.g. a float that will not round-trip through | ||
| # PARSE_JSON), which misdirects the investigation. Discriminate | ||
| # before wrapping. | ||
| if BigQuery._is_value_error(e): | ||
| logger.error(f"Value rejected by BigQuery on insert: {str(e)}") | ||
|
Check failure on line 330 in unstract/connectors/src/unstract/connectors/databases/bigquery/bigquery.py
|
||
| raise BigQueryValueException( | ||
| detail=e.message, table_name=table_name | ||
| ) from e | ||
| logger.error(f"Column missing in inserting data: {str(e)}") | ||
| db, schema, table = table_name.split(".") | ||
| raise ColumnMissingException( | ||
|
|
@@ -327,6 +340,38 @@ | |
| table_name=table, | ||
| ) from e | ||
|
|
||
| @staticmethod | ||
| def _is_value_error(e: Any) -> bool: | ||
| """True if a BigQuery BadRequest is about the DATA, not the schema. | ||
|
|
||
| UN-3176. Matches known value-level signatures -- notably the PARSE_JSON | ||
| round-trip failure in this ticket -- against the exception's own text | ||
| and then against each entry of its structured ``errors`` payload. The | ||
| payload is checked separately because BigQuery's REST path fills it | ||
| with plain dicts, and ``GoogleAPICallError.__str__`` folds an entry | ||
| into the string only when it exposes ``.code``/``.message`` attributes. | ||
| Unknown shapes fall through to the existing column-missing behaviour, | ||
| so this only ever narrows a message that was already wrong for these | ||
| cases. | ||
| """ | ||
| value_error_markers = ( | ||
| "parse_json", | ||
| "round-trip through string representation", | ||
| "invalid json", | ||
| "cannot round-trip", | ||
| "failed to parse json", | ||
| ) | ||
| text = f"{getattr(e, 'message', '') or ''} {str(e)}".lower() | ||
| if any(marker in text for marker in value_error_markers): | ||
| return True | ||
| for error in getattr(e, "errors", None) or []: | ||
| if not isinstance(error, dict): | ||
| continue | ||
| message = str(error.get("message", "")).lower() | ||
| if any(marker in message for marker in value_error_markers): | ||
| return True | ||
| return False | ||
|
|
||
| def get_information_schema(self, table_name: str) -> dict[str, str]: | ||
| """Function to generate information schema of the big query table. | ||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[High] [Lens 16, 15] — This comment (repeated ×4, verbatim) names a mechanism that does not exist on this path, and hides the bugs actually fixed
Also at
:509-511,:523-525,:535-537.The comment says adding
"""made the schema validator compare""against the UUID enum and raise."adapter_idsis a function-local set whose only consumer isvalidate_adapter_access(:553) →AdapterInstance.objects.filter(id__in=adapter_ids). No schema validator, no enum on that path. The only validator in this file (:451) validatestool_meta— a different input — and runs after this function returns. A maintainer will go read the JSON schema and find nothing.The real failure was a Django UUID-coercion error (
AdapterInstance.idis aUUIDField,backend/adapter_processor_v2/models.py:63), or the oldKeyError.And the comment never mentions the two other bugs this diff silently fixes: the old code left
adapter_idunassigned whenis_enabledwasFalse, soadapter_ids.add(adapter_id)re-added the previous iteration's id (leaking across all four loops) or raisedUnboundLocalErroron the first. The newadapter_id = Noneinitialiser is the load-bearing change and is undocumented.Fix: one accurate comment above the loop group —
# Skip unset adapter ids: "" is not a valid UUID and breaks the id__in filter.Hoist the four near-identical loops into a helper.Confidence: High. Independently reached by three agents and a manual trace.