← Back to Pull requests

fix(utils): resolve $ref pointers into nested $defs blocks #3898

Open
unknown opened this pull request with 373 lines diff across 4 files
View on GitHub
unknown unknown commented on Nov 09, 2026 15:27 UTC
Author

Problem

_flatten_json_schema drops every $defs / definitions block it walks past, but resolves only references spelled #/$defs/<name> or #/definitions/<name>. A schema that nests definitions under a property therefore keeps an unresolvable pointer:

{
  "properties": {
    "conf": { "$defs": { "Entry": { "type": "object" } }, "type": "object" },
    "e":    { "$ref": "#/properties/conf/$defs/Entry" }
  }
}

→ properties.conf.$defs is removed, properties.e.$ref stays. MCP tool adapters preserve inputSchema, so externally supplied schemas reach this helper in exactly that shape.

The second half of the report: when the only definitions are nested, the root check in has_defs is false and the schema is returned untouched — so it is never flattened at all.

Fix

Pointers now resolve against an untouched copy of the input, as ordinary local JSON Pointers:

target = _lookup_pointer(source, ref_path)   # "#/properties/conf/$defs/Entry"

_lookup_pointer handles #, ~1 / ~0 escapes and array indices, and answers _MISSING for anything it cannot resolve so the node is left exactly as written. _has_definition_block replaces the root-only check and looks at every level (skipping instance-value keywords, whose contents must stay verbatim).

Why not "keep the nested block"

That would make the output legal but not flat. The function exists because some providers cannot process $defs at all — leaving a nested one in place just moves the failure. Resolving the pointer is the only shape that satisfies both.

Contract preserved

| Case | Behaviour |
| --- | --- |
| #/$defs/Name / #/definitions/Name | unchanged |
| Circular reference | unchanged: {"type": "object", "description": "(circular: Node)"} |
| Schema keywords used as property names (properties.$defs.…) | unchanged: names preserved |
| default / const / enum / examples instance data | unchanged: kept verbatim |
| Nothing to flatten | unchanged: same object returned (identity) |
| Unresolvable pointer | leaves the node as written |

Verification

tests/model_gemini_test.py imports the whole package (needs aiofiles, docstring_parser, google-genai), so I replayed its assertions against the helper directly together with the new cases:

| Check | Result |
| --- | --- |
| All 16 existing assertions from TestGeminiSchemaUtils (root defs, circular, property-name keywords ×4, instance values ×8, legacy definitions, identity) | pass, no change |
| Nested pointer beside a root block (the reported case) | inlined |
| Nested-only definitions | flattened |
| ~1 escape and array index | resolve |
| Unresolvable pointer | left as written |
| Total | 20 / 20 passed |

Four cases were added to TestGeminiSchemaUtils so CI covers them permanently. The full suite was not run locally — the package needs deps this machine does not have — so CI is the first end-to-end run.

Fixes #3894

Diff: 373 lines (+279 / -94) across 4 files Reviewer: chrikrah Review duration: 6306m
All Vouch review confidence checks passed
Residual risk 0.23 = Change Risk (0.43) × (1 − Review Confidence (0.47)). Review depth and reviewer attention state are adequate.
Model 1 · Change Risk (XGBoost)
Score: 0.43 · 373 lines diff across 4 files
0.43
Model 2 · Review Depth (DistilBERT)
Depth Score: 0.02 · 1 substantive comment(s)
0.02
Model 3 · Review Timing & Attention
Session adequacy: 100% · Reviewer familiarity: 35%
46%
Bedrock Claude 3 · Risk Narration
(disconnected)
Groq
Groq Risk Narration
AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
Model 1 · Change Risk Attribution
SHAP Weights
Diff Size diff_size
0.354
Shallow Review Depth shallow_review_depth
0.344
Real PR Conversation Timeline
1 comments
"Review active on #3898"
general weight: 0.35
Reviewers
Assignees
Unassigned
Labels
risk: low confidence: 46.7% reviewed
Vouch Risk Breakdown
AI-powered review risk analysis
Change Risk
Change Risk · Model 1 (XGBoost)
Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
Score: 42.8% (MEDIUM) · Higher = larger blast radius
42.8%
Review Confidence
Review Confidence · Models 2 & 3
Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
Score: 46.7% · Lower = rushed or shallow review
46.7%
Residual Risk
Residual Risk · Core Equation
Residual = Change × (1 − Confidence)
Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
Score: 22.8% · Safe to merge
22.8%