Repository navigation
fix(context): harden relationships load + validate like views - #2613
Conversation
|
Note Reviews pausedIt looks like this branch is under active development. To avoid overwhelming you with review comments due to an influx of new commits, CodeRabbit has automatically paused this review. You can configure this behavior by changing the Use the following commands to manage reviews:
Use the checkboxes below for quick actions:
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Repository UI Review profile: CHILL Plan: Pro Plus Run ID: 📒 Files selected for processing (1)
🚧 Files skipped from review as they are similar to previous changes (1)
Walkthrough
ChangesRelationship YAML validation
Estimated code review effort: 2 (Simple) | ~10 minutes Possibly related PRs
Poem
🚥 Pre-merge checks | ✅ 3 | ❌ 2❌ Failed checks (2 warnings)
✅ Passed checks (3 passed)
✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
|
The loader half here is right, but #2604 landed as 9bdae39 after this branch was opened and set the convention for exactly this situation — and this PR implements only half of it.
Measured on views.yml — reported (9bdae39): relationships.yml — silently gone: Same repo, same file type, same class of bad data, opposite user experience. What to do#2613 and #2607 are the two halves of one change and should be a single PR — the relationships counterpart of 9bdae39:
Note the ordering trap: with #2613's filter in place, That pair also makes #2606 ( RebaseThis branch is 17 commits behind |
Match 9bdae39 (Canner#2604): load_relationships drops non-dict entries; validate_project re-reads raw relationships.yml and reports malformed list/entries so hand-edits do not vanish silently.
0245eba to
f70dd32
Compare
|
Thanks @goldmedal — agreed on the #2604 pattern. Rebased onto current
Also closing #2607 as the incomplete reporting-only half, and #2606/#2608 as redundant consumer re-guards once this lands. |
|
Note GitHub couldn't provide a complete incremental comparison for this pull request, so CodeRabbit is performing a full review instead. This review may take a little longer. |
There was a problem hiding this comment.
Actionable comments posted: 1
🧹 Nitpick comments (1)
core/wren/tests/unit/test_context.py (1)
1761-1766: 🎯 Functional Correctness | 🔵 Trivial | ⚡ Quick winAssert the concrete type in the validation message.
The validation contract includes the offending type. This assertion only checks the prefix, so it would pass if
got strwere removed.Proposed test assertion
- assert any("'relationships' must be a list" in m for m in msgs) + assert any("'relationships' must be a list, got str" in m for m in msgs)🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@core/wren/tests/unit/test_context.py` around lines 1761 - 1766, Strengthen test_validate_project_reports_relationships_not_list by asserting the complete validation message includes both "'relationships' must be a list" and the offending type "got str", rather than checking only the message prefix.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@core/wren/tests/unit/test_context.py`:
- Line 10: Fix the import statement in test_context.py by moving
load_relationships and validate_project inside the parenthesized from
wren.context import list, preserving the existing imported symbols and valid
Python syntax.
---
Nitpick comments:
In `@core/wren/tests/unit/test_context.py`:
- Around line 1761-1766: Strengthen
test_validate_project_reports_relationships_not_list by asserting the complete
validation message includes both "'relationships' must be a list" and the
offending type "got str", rather than checking only the message prefix.
🪄 Autofix
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Repository UI
Review profile: CHILL
Plan: Pro Plus
Run ID: 61fcd03b-3ca1-4dde-9141-5e8da4ea159f
📒 Files selected for processing (2)
core/wren/src/wren/context.pycore/wren/tests/unit/test_context.py
Include load_relationships/validate_project inside the import paren list.
goldmedal
left a comment
There was a problem hiding this comment.
Approve with changes. The core fix is correct and the motivation checks out. Three things should be cleaned up first; one is cosmetic-but-real, two are behavioral.
What I verified
CI is green across all 8 jobs. pytest tests/unit/test_context.py → 132 passed locally.
The fix is genuinely worth having — I reproduced the before/after on relationships: nope:
MAIN: load_relationships() -> 'nope' # str, not list[dict]
[ERROR] relationships[0]: relationship entry must be an object
[ERROR] relationships[1]: ... [2] ... [3] # iterating "n","o","p","e"
PR: load_relationships() -> []
[ERROR] relationships.yml > relationships: 'relationships' must be a list, got str
Four garbage errors from iterating a string's characters → one accurate one. The loader also now actually honors its list[dict] annotation, which is what makes the downstream re-guards unnecessary as claimed — mcp_server.describe_model (rel.get("models"), line 267) and context_cli.py:553 both consume it unguarded today.
1. Duplicate imports in the test file — should fix
from wren.context import (
load_relationships, # ← added
validate_project, # ← added
UpgradeError,
...
load_relationships, # ← already there (line 25)
...
validate_project, # ← already there (line 30)
)Both names were already imported. The follow-up commit a54c844b moved them inside the parens (addressing the earlier syntax catch) but didn't notice they were redundant. Net effect: ruff check tests/unit/test_context.py now fails I001.
CI won't catch this — the lint job runs ruff check src/ only, not tests/ — so it's not a merge blocker, but the file goes from lint-clean to not. Fix is deleting the two added lines.
2. The PR leaves dead code behind — should fix
context.py:1223-1232, inside the "Check relationships" loop:
for i, rel in enumerate(relationships):
if not isinstance(rel, dict):
errors.append(ValidationError("error", f"relationships[{i}]",
"relationship entry must be an object"))
continuerelationships now comes from the filtering loader, so this branch is unreachable. It also carries a different message for the same condition than the new block ("must be an object" vs "must be a mapping, got int"). The views precedent (9bdae39) has no equivalent guard in its loop — removing this is what actually makes the two paths symmetric.
3. Entry indices no longer match the file — worth a look
Because filtering happens before the loop, indices in the main loop are now positions in the filtered list. Repro — file with - 42 at index 0 and an unnamed relationship at index 1:
MAIN: [ERROR] relationships[0]: relationship entry must be an object
[WARNING] relationships > relationships[1]: missing join_type ← correct
PR: [ERROR] relationships.yml > relationships[0]: ... got int
[WARNING] relationships > relationships[0]: missing join_type ← now wrong
Two errors both labelled relationships[0], pointing at different entries. #2604 has the same wart for views, so it's inherited rather than introduced here — but it bites harder in this file, since f"relationships[{i}]" is the fallback display name for real relationships that lack a name. Either enumerate raw and continue on non-dicts in the main loop, or carry a raw index on the filtered entries.
4. Nit — the one hand-edit that still vanishes quietly
A relationships.yml whose root is a bare list (i.e. someone omitted the relationships: key — plausibly the most likely hand-edit mistake) still produces zero diagnostics:
root is a bare list: load=[] relationship_errors=[]
root is a scalar: load=[] relationship_errors=[]
The new block guards with isinstance(raw, dict) and then does nothing when that's false. Given the PR's stated premise, cheap to close:
if raw and not isinstance(raw, dict):
errors.append(ValidationError("error", "relationships.yml",
f"relationships.yml must be a mapping with a 'relationships' key, got {type(raw).__name__}"))On the outstanding CodeRabbit nit
It asks the not-a-list test to assert the concrete type. Agreed it's trivial — the other two tests already assert got str / got int; only test_validate_project_reports_relationships_not_list stops at the prefix.
Not worth changing
validate_project now parses relationships.yml twice (once via load_relationships at line 948, once in the new block). Matches the views precedent, and the alternative is threading raw data through the loader — leave it.
- Drop duplicate test imports (ruff I001) - Remove unreachable non-dict guard in filtered loop - Walk raw relationship list so file indices stay stable - Report bare-list/scalar relationships.yml roots - Tighten not-a-list assertion + cover bare root + index cases
|
Thanks @goldmedal — addressed all four points on the latest push:
Also tightened the CodeRabbit not-a-list assertion to |
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@core/wren/tests/unit/test_context.py`:
- Around line 1794-1799: Strengthen the assertions near the `errors` diagnostic
checks so a single error message contains both `relationships[1]` and
`join_type`. Keep the existing checks for the integer type error, but replace
the separate combined-blob assertions with a per-diagnostic assertion that
prevents these details from being satisfied by different errors.
🪄 Autofix
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Repository UI
Review profile: CHILL
Plan: Pro Plus
Run ID: c7bfe4d3-4edc-4ac1-b373-627d7456dc0b
📒 Files selected for processing (2)
core/wren/src/wren/context.pycore/wren/tests/unit/test_context.py
🚧 Files skipped from review as they are similar to previous changes (1)
- core/wren/src/wren/context.py
CodeRabbit: require relationships[1] and join_type on one diagnostic.
|
Thanks — tightened the index-stability test so one diagnostic must contain both |
Summary
Relationships counterpart of 9bdae39 (#2604):
load_relationshipsfilters tolist[dict](drop non-mapping entries)validate_projectre-reads rawrelationships.ymland reports:'relationships' must be a list, got …relationship entry must be a mapping, got …Motivation
After #2604, views report hand-edited junk; relationships silently dropped it. Same class of bad data, opposite UX.
Note on siblings
This supersedes the split approach in #2607 (reporting half only / models-field only). Downstream consumer re-guards #2606/#2608 become unnecessary once context normalizes the contract — closing those as follow-through.
Verification
Summary by CodeRabbit