Repository navigation
Commit b70f7c9
authored
Agent guardrails for the MCP layer: leakage screen, plan-first, safer defaults (#21)
* Add agent guardrails to the MCP layer (leakage screen, plan-first, safer defaults)
The MCP server is the primary interface for non-technical procurement users
driving P2Predict through an agent. Those users don't know what a feature,
target, or leakage is, so the server — not the prompt — has to do the
thinking and protect them. This adds a guidance + guard layer in front of
the training tools.
Core
- feature_selection.find_leaky_features(): pure, additive detector that flags
numeric columns whose |corr| with the target exceeds a threshold (default
0.97) — i.e. an alternate form of the target (e.g. price_at_1k_usd vs
unit_price_at_1_usd, ~0.99 correlated). Returns plain-language reasons.
MCP server
- New tool propose_training_plan(): AutoML-style pre-flight. Inspects the CSV
and returns a plain-language plan — what it will predict, which specs it
will use, which columns it will leave out and why (leakage, ID-like,
no-variation), a log-target recommendation, and the questions to ask the
user. Trains nothing. The server instructions tell agents to call this
first and confirm before train.
- train() safe defaults (pit of success): when features are auto-selected it
now excludes leakage columns instead of ranking them first; when a price-like
(strictly positive) target is left additive by the skew test it recommends
log_target=on. Both are surfaced in a new `warnings` field. An explicitly
requested leaky feature returns status=needs_confirmation unless
allow_leaky_features=true.
- Rewritten FastMCP instructions and explain/predict_interval docstrings,
distilling the interpretation rules from the p2predict skill so the guidance
travels to every client (Copilot etc.), not just Claude.
Skill + evals
- SKILL.md: new interpretation rule #8 (screen for target leakage) backported
from this work, since the skill had no leakage rule.
- evals.json: new eval #3 that fails on a leaking model and passes when the
leakage column is excluded.
Tests
- tests/test_feature_selection_leakage.py (core detector).
- tests/test_mcp.py: auto-exclude, explicit-block, override, log-target
recommendation, and propose_training_plan coverage. 28 passed.
On the real bmics_clean.csv, train() with no feature list now drops
price_at_1k_usd and reports R2 ~0.58 instead of the leaked 0.90 "Excellent".
* De-dup interpretation rules across skill and MCP instructions
Add bidirectional sync markers so the two copies don't drift: SKILL.md is the
canonical full version; server.py instructions are a self-contained condensed
mirror (non-Claude clients never see the skill). Note which rules the MCP layer
now enforces in code (leakage, log-target) vs. applies manually on the CLI, and
acknowledge the MCP surface in the skill's 'shape of every job'.
---------
Co-authored-by: Ahmed K. Hafsi <25119333+ahmed-hafsi@users.noreply.github.com>1 parent 40b6a0c commit b70f7c9
6 files changed
Lines changed: 522 additions & 12 deletions
File tree
- .claude/skills/p2predict
- evals
- src/p2predict
- mcp
- tests
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
33 | 33 | | |
34 | 34 | | |
35 | 35 | | |
36 | | - | |
37 | | - | |
38 | | - | |
39 | | - | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
40 | 44 | | |
41 | 45 | | |
42 | 46 | | |
| |||
242 | 246 | | |
243 | 247 | | |
244 | 248 | | |
| 249 | + | |
| 250 | + | |
| 251 | + | |
| 252 | + | |
| 253 | + | |
| 254 | + | |
| 255 | + | |
| 256 | + | |
| 257 | + | |
245 | 258 | | |
246 | 259 | | |
247 | 260 | | |
| |||
332 | 345 | | |
333 | 346 | | |
334 | 347 | | |
| 348 | + | |
| 349 | + | |
| 350 | + | |
| 351 | + | |
| 352 | + | |
| 353 | + | |
| 354 | + | |
| 355 | + | |
| 356 | + | |
| 357 | + | |
| 358 | + | |
| 359 | + | |
| 360 | + | |
| 361 | + | |
| 362 | + | |
| 363 | + | |
| 364 | + | |
| 365 | + | |
| 366 | + | |
| 367 | + | |
| 368 | + | |
| 369 | + | |
| 370 | + | |
| 371 | + | |
| 372 | + | |
| 373 | + | |
| 374 | + | |
335 | 375 | | |
336 | 376 | | |
337 | 377 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
21 | 21 | | |
22 | 22 | | |
23 | 23 | | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
24 | 31 | | |
25 | 32 | | |
26 | 33 | | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
31 | 31 | | |
32 | 32 | | |
33 | 33 | | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
34 | 82 | | |
35 | 83 | | |
36 | 84 | | |
| |||
0 commit comments