← Back to Pull requests

surveys: make the atomic per-rider cap part of the repository contract #117

Closed
unknown closed this pull request without merging on Nov 30, 2026 15:28 UTC
View on GitHub
unknown unknown commented on Nov 30, 2026 11:01 UTC
Author

Fixes #116. Follow-up to #114 (merged).

Context

#114 enforces the per-rider survey cap inside the write transaction via an optional surveys.CappedCreator, which createSurveyResponse type-asserts for, falling back to plain CreateResponse (pre-check only, races) when missing; internal/store/storetest doesn't exercise it. So a future adapter could omit it and pass conformance, reintroducing concurrent over-cap creates. Also, surveys.ErrTooManyResponses → 422 had no handler coverage.

Change (option: fold in + drop fallback)

  • surveys.Repository: CreateResponseCapped(ctx, in, now, maxPerRider int64) (Response, error) is now mandatory (ErrNotFound, ErrTooManyResponses; maxPerRider <= 0 = no cap; atomic per-rider cap inside the write transaction). sqlite surveyRepo already implements it.

  • remove surveys.CappedCreator; createSurveyResponse calls h.deps.Surveys.CreateResponseCapped(..., limit) directly, drop the fallback branch; CountResponsesByUser fast pre-check kept; ErrTooManyResponses → same 422 "User identifier has already responded to this survey".

  • update test fakes implementing surveys.Repository: failingSurveys, erroringSurveysRepo, fatalOnWriteRepo (CreateResponseCapped); fatalOnWriteRepo t.Fatals on any create.

  • internal/store/storetest unaffected (calls CreateResponse); conformance now can't omit CreateResponseCapped.
  • Handler test

    TestSurveyResponse_ErrTooManyResponses422 (internal/httpapi/surveys_responses_test.go): tooManyRepo CreateResponseCapped → surveys.ErrTooManyResponses, CountResponsesByUser → 0, rest real → 422 + per-rider message. (Pre-check passes; atomic branch rejects.)

    Existing atomicity pin kept: TestCreateResponseCappedConcurrentSameUser (internal/store/sqlite, N=8 same user, maxPerRider=1 → exactly 1 success, rest ErrTooManyResponses, 1 stored; _txlock=immediate).

    Verification

    go vet ./...; gofmt; go test ./internal/httpapi ./internal/surveys ./internal/store/... -count=1. internal/store/sqlite has only pre-existing Windows TestOpen_FileModesOwnerOnly (Unix file modes; passes on Linux). Full CI pending.

    Diff: 273 lines (+204 / -69) across 3 files Reviewer: aaronbrethorst Review duration: 266m
    All Vouch review confidence checks passed
    Residual risk 0.16 = Change Risk (0.40) × (1 − Review Confidence (0.60)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.40 · 273 lines diff across 3 files
    0.40
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.17 · 1 substantive comment(s)
    0.17
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 35%
    60%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    Groq Risk Narration
    AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
    Model 1 · Change Risk Attribution
    SHAP Weights
    Diff Size diff_size
    0.336
    Shallow Review Depth shallow_review_depth
    0.289
    Real PR Conversation Timeline
    1 comments
    "Review active on #117"
    general weight: 0.35
    Assignees
    Unassigned
    Labels
    risk: low confidence: 60.5% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 39.5% (MEDIUM) · Higher = larger blast radius
    39.5%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 60.5% · Lower = rushed or shallow review
    60.5%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 15.6% · Safe to merge
    15.6%