← Back to Pull requests

[18.0][FIX] ddmrp_cron_actions_as_job: procure only on fully refreshed buffer #659

Open
unknown opened this pull request with 322 lines diff across 4 files
View on GitHub
unknown unknown commented on Nov 18, 2026 11:20 UTC
Author

Alternative to #644 that keeps its fix but avoids most of its cost.

Problem (from #644, by @divad1196)

When a move is confirmed, stock.move._update_ddmrp_nfp delays one cron_actions job per buffer side: only_nfp="out" for demand and "in" for supply. Each job refreshes one side of the net flow position and then runs do_auto_procure. If the job for the other side of the same buffer is still pending, the procurement is based on an outdated quantity for that side. It can then order something that a confirmed receipt already covers.

Approach

The first two commits are @divad1196's from #644, cherry-picked unchanged:

  • the test that reproduces the issue;

  • the as_job fix, which always refreshes both sides.
  • The ddmrp core change from #644 (stock_move.py) is left out.

    The third commit refreshes both sides only when it is needed. Before running a partial refresh, the job looks for a pending job that refreshes the other side of the same buffer:

  • It uses that job's identity key, the same indexed lookup queue_job does when enqueuing.

  • If such a job exists, this one refreshes both sides, so procurement sees up-to-date figures.

  • Otherwise it keeps the partial refresh that was requested.
  • Without queue_job delaying the jobs, behaviour doesn't change.

    Measurements

    On a production copy with ~50k buffers, I validated a bunch of deliveries and receipts, then ran the same resulting jobs under each variant:

    | cron_actions job time | Partial (current) | Always full (#644) | Conditional (this PR) |
    |---|---|---|---|
    | Deliveries (202 jobs) | 54.5 s | 78.8 s (+45%) | 56.8 s (+4%) |
    | Receipts (253 jobs) | 53.1 s | 77.3 s (+46%) | 55.5 s (+4.5%) |

  • The check costs ~2.5 ms per job, against ~250 ms for the refresh itself. The rest of the +4% is run-to-run noise.

  • It triggered a full refresh in 0 of the 202 delivery jobs.

  • It triggered a full refresh in 23 of the 253 receipt jobs. Those are exactly the buffers that ended up with both an in and an out job pending, caused by the chained moves of the receipt.
  • How often the issue happens

    Over 40 days of job history on the same database:

  • 3.8% of the partial jobs ran while a job for the other side of the same buffer was still pending.

  • Only 0.09% of all partial jobs procured.
  • So the race is real but rare. Making every job ~45% more expensive to cover it seemed too high a price.

    Tests

  • @divad1196's test passes.

  • A new test checks that a job with no pending counterpart still refreshes only its own side.
  • Diff: 322 lines (+241 / -81) across 4 files Reviewer: divad1196 Review duration: 19459m
    All Vouch review confidence checks passed
    Residual risk 0.22 = Change Risk (0.42) × (1 − Review Confidence (0.47)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.42 · 322 lines diff across 4 files
    0.42
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.02 · 1 substantive comment(s)
    0.02
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 35%
    46%
    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.346
    Shallow Review Depth shallow_review_depth
    0.344
    Real PR Conversation Timeline
    1 comments
    "Review active on #659"
    general weight: 0.35
    Reviewers
    Assignees
    Unassigned
    Labels
    risk: low confidence: 46.7% 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: 41.9% (MEDIUM) · Higher = larger blast radius
    41.9%
    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: 46.7% · Lower = rushed or shallow review
    46.7%
    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: 22.4% · Safe to merge
    22.4%
    Repository