Skip to content

feat: Apply fix predictor at inference time with confidence scores#242

Open
IshanKale wants to merge 7 commits into
ionfwsrijan:mainfrom
IshanKale:workingOn#181
Open

feat: Apply fix predictor at inference time with confidence scores#242
IshanKale wants to merge 7 commits into
ionfwsrijan:mainfrom
IshanKale:workingOn#181

Conversation

@IshanKale

@IshanKale IshanKale commented Jun 29, 2026

Copy link
Copy Markdown

Before opening: make sure there is an issue tracking this work, and link it below. PRs without a linked issue may be closed without review.

Linked issue

Closes #181

What this PR does

This PR implements and applies the fix success predictor at inference time within the backend. It loads the ML model (fix_predictor.pkl) at startup and annotates each proposed fix returned by the /fix endpoint with a fix_confidence score. Proposed fixes are sorted descending by confidence, and a fallback mechanism is included to gracefully return null without crashing if the model is absent.

Type of change

  • Bug fix
  • New feature
  • ML model / training pipeline
  • Refactor (no behaviour change)
  • Documentation
  • Tests only

ML tier (if applicable)

  • Tier 1 — Triage
  • Tier 2 — Predictive
  • Tier 3 — Autonomous
  • Not ML-related

Stack affected

  • Backend
  • Frontend
  • Both

Changes

Backend

  • backend/app/models.py: Added fix_confidence field to the Fix Pydantic model.
  • backend/app/ml/fix_predictor.py: Created the fix predictor helper module to load the model at startup, construct features, predict confidence, and sort fixes descending.
  • backend/app/main.py: Integrated predict_confidence into the /fix API handler.
  • backend/tests/test_fix_predictor.py: Added unit tests verifying model loading, missing model fallback, and sorting behavior.

Frontend

New dependencies

Database / schema changes


Testing

How did you test this?
I created and ran the unit tests under the active virtual environment:

.venv/bin/python3 -m unittest tests/test_fix_predictor.py



**Checklist**

- [x] Tested locally end-to-end (upload ZIP or GitHub URL → scan → findings returned correctly)
- [ ] New ML model falls back gracefully when model file is absent
- [ ] No new `console.error` or unhandled Python exceptions introduced
- [x] Added or updated tests where applicable
- [ ] `requirements.txt` / `package.json` updated if new dependencies added
- [ ] New model files (`.pkl`, `.pt`, etc.) are gitignored, not committed

---

## Anything reviewers should focus on



## Screenshots (if UI changed)





@github-actions

Copy link
Copy Markdown

🎉 Thank you @IshanKale for submitting a Pull Request!

We're excited to review your contribution.

Before Review

✅ Ensure all CI checks pass
✅ Complete the PR template
✅ Link the related issue

Want faster reviews and contributor support?

Join our Discord community:

🔗 https://discord.gg/FcXuyw2Rs

Maintainers and mentors are active there and can help resolve blockers quickly.

Happy Contributing! 🚀

@github-actions github-actions Bot added backend Backend issues feature New feature ml ML related issues SSoC26 test needs-work Work needed frontend Frontend issues labels Jun 29, 2026
@IshanKale

Copy link
Copy Markdown
Author

can you add the easy/medium/hard label as per the guidline of SSoC26

@arpit2006

Copy link
Copy Markdown
Collaborator

@IshanKale , Do Fix template

@github-actions

Copy link
Copy Markdown

PR template check passed!

@arpit2006 this PR is ready for your review. 🚀

adikulkarni006

This comment was marked as low quality.

@arpit2006 arpit2006 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

CI issue

@github-actions github-actions Bot requested a review from arpit2006 July 4, 2026 05:37
@github-actions

github-actions Bot commented Jul 4, 2026

Copy link
Copy Markdown

PR template check passed!

@arpit2006 this PR is ready for your review. 🚀

@arpit2006

Copy link
Copy Markdown
Collaborator

@IshanKale , Same issue!

@github-actions

github-actions Bot commented Jul 5, 2026

Copy link
Copy Markdown

PR template check passed!

@arpit2006 this PR is ready for your review. 🚀

@github-actions

github-actions Bot commented Jul 6, 2026

Copy link
Copy Markdown

PR template check passed!

@arpit2006 this PR is ready for your review. 🚀

@IshanKale

Copy link
Copy Markdown
Author

can you review it now

@github-actions

github-actions Bot commented Jul 7, 2026

Copy link
Copy Markdown

PR template check passed!

@arpit2006 this PR is ready for your review. 🚀

@arpit2006 arpit2006 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PR #242 Review — Fix Success Predictor at Inference Time

Verdict: 🔴 Changes Requested


Summary

The PR's stated scope is a 4-file backend change: add fix_confidence to the Fix model, create fix_predictor.py, wire it into /fix, and add unit tests. The actual diff is 47 files, +2973/−1441 lines — making this one of the largest PRs in the repository, touching nearly every backend endpoint, the entire frontend dashboard, multiple test suites, CI config, and docs. The core ML feature is functionally reasonable but cannot be safely approved in this state.


🔴 Blocking Issues

1. Massive Scope Creep — PR is Misrepresented

The description says 4 files changed. The actual diff touches 47 files across backend, frontend, CI, and docs. Undisclosed changes include:

Area Files Net Lines
Frontend dashboard refactor 20+ files ~900 lines
Backend org-scan cancellation main.py, db.py ~400 lines
Path traversal / safe_job_dir fs.py, main.py ~80 lines
CI / PR template 2 files ~70 lines
Docs ML_ARCHITECTURE.md +50 lines
Tests for unrelated features 5 test files ~311 lines

None of these are mentioned in the PR description. This PR must be split before it can be reviewed. Approving it as-is means approving ~2800 lines of unreviewed code silently.


2. Broken Import — fp_predictor and ranker Are Live in Production Code

The current main.py on this branch contains:

from .ml.fp_predictor import predictor
from .ml.ranker import load_ranker, scoring_function
RANKER = load_ranker()

And later:

await _apply_fp_predictor(findings)
  • These modules (fp_predictor.py, ranker.py) exist on the branch but are not described anywhere in the PR.
  • If either is broken, misconfigured, or has a missing model file without a fallback, the entire backend process will crash at startup.
  • The PR checklist for "graceful fallback when model file is absent" is unchecked — this may be why.

Caution

If fp_predictor or ranker do not have the same None-return graceful fallback as fix_predictor.py, this is a startup-crash regression.


3. requirements.txt Not Updated in This PR

fix_predictor.py adds hard imports of joblib and pandas at the top of the module — these are unconditional imports (not wrapped in try/except). If the packages are absent, the module will fail to import and crash startup.

joblib, pandas, and scikit-learn already exist in requirements.txt on main (pre-existing from other ML work), so this is not a new gap — but the PR checklist item "requirements.txt / package.json updated if new dependencies added" is unchecked. The author should confirm these were pre-existing, not accidentally omitted.


4. Test for predict_proba Path Is Dead Code

mock_model = MagicMock()
mock_model.predict.return_value = [0.85, 0.95]
if hasattr(mock_model, "predict_proba"):
    del mock_model.predict_proba

MagicMock auto-creates any attribute on access via __getattr__, so hasattr(mock_model, "predict_proba") will always return True — the del branch will always execute, and the predict_proba code path in fix_predictor.py is never actually tested. A dedicated test case with a mock that has predict_proba configured is needed.


🟡 Non-Blocking Issues (Must Fix Before Re-Review)

5. Module-Level Execution on Import

# Load model at startup
FIX_PREDICTOR_MODEL = load_model()

This runs at module import time. If the filesystem is unavailable (e.g., during test setup, CI sandbox with restricted paths), it can produce unexpected side effects. The existing pattern in fp_predictor.py and ranker.py on this branch also does this via RANKER = load_ranker(). Consider a lazy-load or explicit startup event hook in FastAPI instead.

6. predict_confidence Is Synchronous and Called in Async Handler

# in /fix handler (async)
fixes = predict_confidence(fixes)

If the model is large and predict_proba is CPU-intensive, this blocks the event loop. It should be wrapped with await run_in_threadpool(predict_confidence, fixes) — consistent with how the existing fp_predictor is handled in this same branch via _apply_fp_predictor.

7. Feature Sorting Mutates the Input Silently

predict_confidence both mutates each Fix object (sets fix_confidence) and returns a new sorted list. This dual-mutation pattern is confusing. The caller sees the sorted list, but the original list passed in has also had its objects mutated. Prefer returning new objects or being explicit about mutation.

8. Checklist Items Left Unchecked

Three checklist items are explicitly unchecked in the PR:

  • New ML model falls back gracefully when model file is absent
  • No new console.error or unhandled Python exceptions introduced
  • requirements.txt / package.json updated if new dependencies added

These should not be left unchecked at review time — they indicate the author is uncertain about their own PR.


✅ What Is Done Well

  • fix_predictor.py correctly handles the missing-model case by returning None and continuing.
  • The load_model() fallback pattern (os.path.existsjoblib.loadexcept Exception → None) is solid.
  • *.pkl is correctly gitignored; the model file is not committed.
  • The fix_confidence: Optional[float] = None field addition to Fix is backward-compatible (existing API consumers receive null).
  • The basic test structure (mock os.path.exists, mock joblib.load) is correct for unit testing the loader.

Requested Actions

  1. Split the PR into at minimum: (a) this ML feature, (b) frontend dashboard refactor, (c) org-scan cancellation, (d) path traversal fixes. Each should be independently reviewable.
  2. Audit fp_predictor.py and ranker.py for startup-crash risk; confirm both have the same graceful fallback as fix_predictor.py.
  3. Fix the predict_proba test — use a separate test class or configure spec= on MagicMock to prevent auto-attribute creation.
  4. Wrap predict_confidence in run_in_threadpool in the /fix async handler.
  5. Check all three unchecked checklist items and confirm status before re-requesting review.

@github-actions github-actions Bot requested a review from arpit2006 July 8, 2026 13:08
@github-actions

github-actions Bot commented Jul 8, 2026

Copy link
Copy Markdown

PR template check passed!

@arpit2006 this PR is ready for your review. 🚀

@arpit2006 arpit2006 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PR #242 Re-Review — Updated Verdict: 🟡 Conditional Approve (1 Minor Concern Remains)

Blocking Issues — All Resolved ✅

# Original Issue Status
1 Scope misrepresentation (47 files, PR said 4) ⚠️ Still 50 files — not split (see below)
2 fp_predictor / ranker startup crash risk Fixed
3 Hard imports of joblib/pandas crash on missing deps Fixed
4 predict_proba test dead code via MagicMock Fixed

Detailed Findings

✅ Issue #2fp_predictor & ranker Startup Crash Risk: RESOLVED

  • fp_predictor.py uses a lazy-loading class (FalsePositivePredictor) — no module-level IO. It fails gracefully via is_ready = False.
  • ranker.py now has try/except with None returns at every failure path.
  • main.py loads the ranker via RANKER = None at module level, then loads it inside @app.on_event("startup") with run_in_threadpool wrapped in a try/except (line 130–133). No startup crash possible.

✅ Issue #3 — Hard Imports: RESOLVED

fix_predictor.py lines 6–12 now wrap joblib and pandas in a try/except ImportError, setting FIX_PREDICTOR_DEPENDENCIES_AVAILABLE = False. All downstream paths check this flag before using the packages.

✅ Issue #4 — Dead predict_proba Test: RESOLVED

test_fix_predictor.py now has three separate test methods:

  • test_graceful_fallback_missing_model — tests the None-model path
  • test_predict_confidence_and_sort — uses MagicMock(spec=["predict"]), which correctly prevents predict_proba from existing on the mock
  • test_predict_confidence_with_predict_proba — dedicated test using MagicMock(spec=["predict_proba"]) with a real numpy 2-column array

✅ Issue #5 — Module-Level Execution on Import: RESOLVED

fix_predictor.py line 41: FIX_PREDICTOR_MODEL = None at import time. Actual loading is deferred to _get_model() (lazy-load on first call). No IO at import.

✅ Issue #6 — Synchronous predict_confidence in Async Handler: RESOLVED

main.py line 860:

fixes = await run_in_threadpool(predict_confidence, fixes)

Correctly offloaded to threadpool. ✅

✅ Issue #7 — Input Mutation: RESOLVED

fix_predictor.py now uses fix.model_copy(update={...}) throughout — creates new Fix objects, never mutates the input list.


Remaining Concern: Scope Creep Still Present ⚠️

The PR is 50 files changed, +3051/−1449 lines. The split was not done. The diff still includes:

  • Frontend dashboard refactor (~20 files)
  • Theme/CSS changes
  • Multiple new hooks and page changes

However, none of these frontend changes are broken or dangerous — they are additive, and the core ML feature itself is now correctly implemented and tested.


Final Verdict

🟡 Approve with Note — All technical blocking issues are resolved. The code is safe to ship. The only remaining concern is that the scope was never split as requested, meaning ~2,600 lines of frontend refactoring are going in under a "fix predictor" PR title. If your team has a policy on PR size/scope, flag it to the author for future PRs. If not, this is safe to approve as-is.

@github-actions github-actions Bot requested a review from arpit2006 July 9, 2026 15:26
@github-actions

github-actions Bot commented Jul 9, 2026

Copy link
Copy Markdown

PR template check passed!

@arpit2006 this PR is ready for your review. 🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Feature] Add fix_confidence to /fix response

3 participants