Skip to content

fix(scoring): pin upstream constants to the resolved SHA and fail closed on fetch failure - #1192

Merged
JSONbored merged 2 commits into
mainfrom
fix/scoring-pin-sha-fail-closed
Jun 24, 2026
Merged

fix(scoring): pin upstream constants to the resolved SHA and fail closed on fetch failure#1192
JSONbored merged 2 commits into
mainfrom
fix/scoring-pin-sha-fail-closed

Conversation

@JSONbored

Copy link
Copy Markdown
Owner

Summary

Two correctness gaps in the scoring constants refresh path:

SHA-pin: The fetch was using the mutable branch ref (e.g. test) directly in the raw-content URL. A force-push or branch-rename between the ref-resolve and the fetch could silently change what constants every repo scores against mid-cycle. Now resolves the ref to an immutable HEAD commit SHA first (GET /repos/{repo}/commits/{ref}), then builds the constants URL from that SHA. The SHA is also recorded in the snapshot payload for audit traceability. Fail-open on the SHA lookup itself — a transient API error falls back to the mutable ref so the refresh is never blocked.

Fail-closed: A failed constants fetch previously fell through to DEFAULT_SCORING_CONSTANTS, silently overwriting the last verified upstream values with hardcoded defaults. That would move live scoring with no one noticing. Now: if a verified raw-github snapshot exists, freeze it in place and append a warning instead of persisting defaults. Only bootstraps to defaults when there is genuinely no prior snapshot to fall back to.

Scope

  • src/scoring/model.ts — SHA-pin + fail-closed logic
  • test/unit/scoring.test.ts — 3 new tests (SHA recorded, freeze-last-good, bootstrap-to-defaults)
  • No DB migration, no API/schema changes, no generated files to regenerate

Validation

  • npm run test:ci — all tests pass
  • npm run typecheck — clean

Safety

  • No auth or CORS changes
  • Fail-closed strictly tightens behavior (never loosens)
  • SHA-pin is best-effort / fail-open so no refresh is ever blocked

@dosubot dosubot Bot added the size:S label Jun 24, 2026
@superagent-security

Copy link
Copy Markdown
Contributor

Superagent didn't find any vulnerabilities or security issues in this PR.

@codecov

codecov Bot commented Jun 24, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 94.80%. Comparing base (a21dc3b) to head (b2a8269).
✅ All tests successful. No failed tests found.

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #1192   +/-   ##
=======================================
  Coverage   94.80%   94.80%           
=======================================
  Files         157      157           
  Lines       19083    19090    +7     
  Branches     6909     6912    +3     
=======================================
+ Hits        18091    18098    +7     
  Misses        399      399           
  Partials      593      593           

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@JSONbored
JSONbored merged commit 1bc3ee6 into main Jun 24, 2026
17 checks passed
@JSONbored
JSONbored deleted the fix/scoring-pin-sha-fail-closed branch June 24, 2026 07:48
JSONbored added a commit that referenced this pull request Jun 24, 2026
…g branches (#1193)

Pagination cap: getLastCloserLogin now exits after 10 pages (1 000 events)
instead of looping indefinitely — prevents GitHub API rate exhaustion on
pathologically long issue timelines. Returns the best lastCloser found within
the cap; adds a test that validates the cap fires and page 11 is never fetched.

Scoring coverage: add tests for the four uncovered branches in the #1192 diff:
- fetchUpstreamRefSha catch path (network throw → null → mutable-ref fallback)
- non-string SHA response (typeof guard → null → mutable-ref fallback)
- empty SHA string (length guard → null → mutable-ref fallback)
- prior fallback snapshot on second fail-closed attempt (sourceKind === "fallback"
  guard → does not freeze the prior fallback, bootstraps fresh defaults instead)
JSONbored added a commit that referenced this pull request Jun 24, 2026
#1235)

#1192 pinned the upstream constants fetch and froze last-good on a non-2xx
response, but a 200 with a semantically-garbage body — an HTML interstitial,
a Git-LFS pointer, or a truncated file — parsed to ~0 recognized constants
and silently reverted every constant to DEFAULT_SCORING_CONSTANTS while
keeping sourceKind="raw-github", moving live scoring with no staleness
signal.

The refresh now applies a sanity floor: a 200 body must parse at least
MIN_RECOGNIZED_SCORING_CONSTANTS recognized constants to be trusted.
Otherwise it is handled exactly like a failed fetch — freeze the last-good
snapshot, or bootstrap to a clearly-labeled "fallback" when there is no
verified last-good. Also surfaces a warning when the upstream ref can't be
resolved to an immutable SHA (the fetch falls back to the mutable ref), and
records the resolved fetch ref in the drift-sync source.
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.

1 participant