Skip to content

fix(storage): raise on unknown get_stats_by group_by; close #476 - #527

Merged
Sinity merged 1 commit into
masterfrom
feature/fix/mcp-limits-and-id-safety
Apr 27, 2026
Merged

Sinity merged 1 commit into
masterfrom
feature/fix/mcp-limits-and-id-safety

Conversation

@Sinity

@Sinity Sinity commented Apr 27, 2026 •

Copy link
Copy Markdown
Owner

Summary

Closes #476. Three of the five sub-issues are already resolved on master; one defense-in-depth tightening remaining.

Problem

#476 catalogued five MCP/storage surface issues. Three are already fixed on master:

Sub-item Status
`_clamp_limit` had no upper bound Already capped at `min(limit, 1000)` (`mcp/server_support.py:87`)
`resolve_id` prefix-matched on destructive ops Already exposes `strict: bool = False`; every MCP destructive call site (`server_mutation_tools.py`: `add_tag`, `remove_tag`, `set_meta`, `delete_conversation`, …) already passes `strict=True`
`get_stats_by` user string with fragile whitelist MCP wrapper already declares `group_by: Literal["provider", "month", "year"]` (`server_tools.py:214`); deeper layer still accepts `str` and silently falls back

Two remaining items are larger refactors and tracked separately:

  • Asymmetric `search`/`list_conversations` filter surfaces (calls for unifying the tools or extending parameter sets — design choice).
  • `bulk_tag_conversations` O(N×M) in separate transactions (needs batched single-transaction implementation with a size limit).

Solution

Tighten the deeper `get_stats_by` (`storage/backends/queries/stats.py`) to raise `ValueError` on unknown `group_by` instead of silently falling back to provider grouping. Each SQL branch is a literal — no interpolation — but a future contributor adding a branch that does interpolate would inherit a silent fallback as their input validation. Closing the door on that.

Verification

```
$ nix develop -c devtools verify --quick
verify: all checks passed

$ nix develop -c pytest -q tests/ --ignore=tests/integration
5596 passed, 10 xfailed in 160.24s
```

Summary by CodeRabbit

  • Bug Fixes
    • Enhanced parameter validation to provide explicit error messages for invalid inputs instead of silently defaulting to alternative behavior.

Closes #476.

Three of the five sub-issues in #476 are already resolved on master:

- _clamp_limit (mcp/server_support.py:87) already caps at min(limit, 1000),
  so the "no upper bound → gigabyte JSON response" DoS path is gated.
- resolve_id (storage/backends/queries/conversations_identity.py:15) already
  exposes a `strict: bool = False` parameter, and every destructive MCP
  call site (server_mutation_tools.py: add_tag, remove_tag, set_meta,
  delete_conversation, etc.) already passes strict=True.
- The MCP wrapper already declares group_by as
  Literal["provider", "month", "year"] (server_tools.py:214).

Two large items remain — asymmetric search/list_conversations filter
surfaces (which calls for unifying the tools) and bulk_tag_conversations
O(N×M) batching — both are larger refactors and tracked separately in
the next slice of MCP work.

This change tightens defense-in-depth on the deeper get_stats_by
implementation: it previously fell back to provider grouping for any
unknown group_by string. Each branch in the SQL is a literal, so no
interpolation occurs today, but a future contributor adding a branch that
does interpolate would inherit a silent fallback as their input
validation. Raise ValueError on unknown group_by instead — the validated
top-level Literal never delivers an unknown value, so the only callers
who'd hit the raise are misuses worth surfacing.

Co-Authored-By: Claude <[email protected]>
@coderabbitai

coderabbitai Bot commented Apr 27, 2026 •

Copy link
Copy Markdown

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: ASSERTIVE

Plan: Pro

Run ID: 52cdd8d3-5f1a-4310-91db-f5ac5761b938

📥 Commits

Reviewing files that changed from the base of the PR and between ab58c95 and 52cd65c.

📒 Files selected for processing (1)
  • polylogue/storage/backends/queries/stats.py

📝 Walkthrough

Walkthrough

The get_stats_by function now validates the group_by parameter explicitly. Previously, any unrecognized value defaulted silently to "provider"; now the function raises ValueError for invalid inputs while supporting "month", "year", and "provider" options. The docstring was updated accordingly.

Changes

Cohort / File(s) Summary
Validation Enhancement
polylogue/storage/backends/queries/stats.py
Replaced implicit fallback behavior with explicit parameter validation. The group_by argument now triggers ValueError for unsupported values instead of silently defaulting to "provider" counts. Docstring updated to document exception-raising behavior.

Estimated code review effort

🎯 1 (Trivial) | ⏱️ ~5 minutes

Poem

🐰 A rabbit checked stats with newfound care,
No sneaky defaults lurking anywhere!
month, year, provider—all validated true,
Invalid values? I'll error on cue! 🥕✨

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title accurately and concisely describes the main change: raising ValueError on unknown group_by values in get_stats_by, and references the related issue #476.
Description check ✅ Passed The PR description follows the template with all required sections: Summary, Problem, Solution, and Verification. It clearly explains context, changes, and validation performed.
Linked Issues check ✅ Passed The PR successfully implements one of the five sub-issues from #476: hardening get_stats_by input validation [#476]. The other four items are documented as already fixed or deferred separately.
Out of Scope Changes check ✅ Passed All changes are narrowly scoped to tightening get_stats_by validation as defined in #476, with no extraneous modifications to unrelated functionality.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch feature/fix/mcp-limits-and-id-safety

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.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@Sinity
Sinity merged commit c691f4c into master Apr 27, 2026
14 checks passed
@Sinity
Sinity deleted the feature/fix/mcp-limits-and-id-safety branch April 27, 2026 04:40
@Sinity

Sinity commented May 1, 2026

Copy link
Copy Markdown
Owner Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Nice work!

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@Sinity
Sinity requested a review from Copilot May 1, 2026 01:55
@Sinity
Sinity removed the request for review from Copilot May 1, 2026 02:15
Sinity added a commit that referenced this pull request May 7, 2026
## Summary

Closes #476. Three of the five sub-issues are already resolved on
master; one defense-in-depth tightening remaining.

## Problem

#476 catalogued five MCP/storage surface issues. Three are already fixed
on master:

| Sub-item | Status |
| --- | --- |
| \`_clamp_limit\` had no upper bound | Already capped at \`min(limit,
1000)\` (\`mcp/server_support.py:87\`) |
| \`resolve_id\` prefix-matched on destructive ops | Already exposes
\`strict: bool = False\`; every MCP destructive call site
(\`server_mutation_tools.py\`: \`add_tag\`, \`remove_tag\`,
\`set_meta\`, \`delete_conversation\`, …) already passes \`strict=True\`
|
| \`get_stats_by\` user string with fragile whitelist | MCP wrapper
already declares \`group_by: Literal["provider", "month", "year"]\`
(\`server_tools.py:214\`); deeper layer still accepts \`str\` and
silently falls back |

Two remaining items are larger refactors and tracked separately:
- Asymmetric \`search\`/\`list_conversations\` filter surfaces (calls
for unifying the tools or extending parameter sets — design choice).
- \`bulk_tag_conversations\` O(N×M) in separate transactions (needs
batched single-transaction implementation with a size limit).

## Solution

Tighten the deeper \`get_stats_by\`
(\`storage/backends/queries/stats.py\`) to raise \`ValueError\` on
unknown \`group_by\` instead of silently falling back to provider
grouping. Each SQL branch is a literal — no interpolation — but a future
contributor adding a branch that *does* interpolate would inherit a
silent fallback as their input validation. Closing the door on that.

## Verification

\`\`\`
$ nix develop -c devtools verify --quick
verify: all checks passed

$ nix develop -c pytest -q tests/ --ignore=tests/integration
5596 passed, 10 xfailed in 160.24s
\`\`\`

<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->

## Summary by CodeRabbit

* **Bug Fixes**
* Enhanced parameter validation to provide explicit error messages for
invalid inputs instead of silently defaulting to alternative behavior.

<!-- end of auto-generated comment: release notes by coderabbit.ai -->

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

fix(mcp): complement #444 — limit upper-bound, filter asymmetry, prefix-match safety

1 participant