USDA lot drawdown commit
USDA commodity drawdown is the workflow that keeps weighted-average costing honest when a lot exhausts. The pricing of every assembly that uses the commodity (barrel cheddar, etc) depends on which lot we're drawing from — if a lot ends and we forget to recompute, every quote and pricing_master row sourced from that commodity is wrong.
The cascade re-bases the vendor_costs entry, recomputes the assembly_cost_rollup for every affected assembly, flags open quotes for re-quote, and updates the USDA program reconciliation status.
Risk level 4 (high) — because the cost basis touches active customer pricing.
Trigger conditions
- Inventory cron detects a lot remaining_qty crosses zero.
- Manual lot close from /admin-dashboard.html when reconciling with USDA reports.
- Triggered from
assembly_build_reviewwhen a BOM change references a different commodity lot. - USDA entitlement period rollover — old lots retire en masse.
The cost_diff_reasonable precondition warns if the new lot's cost differs by >30% from the exhausted lot. This catches the most common data error: wrong lot mapped to wrong commodity.
The 4 beats
-
01
INSERT new vendor_costs row
INSERT into
vendor_costswith the new weighted-average unit_cost and effective_from. The prior row remains (active=false implicit via effective_from ordering). -
02
Recompute affected assemblies
Call
recompute_assembly_cost_rollupfor each assembly whose BOM references the commodity sku. Rollups persist with freshlast_computed. -
03
Flag affected open quotes
For each quote in
(draft, proposed, sent)with a line on the commodity sku, stageflag_stale_quotein proposed_actions. -
04
USDA program reconciliation
UPDATE
usda_commodity_programs SET reconciliation_status='pending'for affected programs. Surfaces in the USDA reconciliation queue.
What's different after the workflow runs
- A new vendor_costs row carries the weighted-average from the next lot.
- Every affected assembly has a fresh cost rollup.
- Each open quote touching the commodity sku is in Mike's queue for re-quote.
usda_commodity_programsreconciliation is pending.- reflexion_log carries a
usda_drawdown,sku:?tagged row.
What can go wrong and how to recover
Precondition blocks: current_lots.length > 0. This means we have no commodity supply for the SKU. Manual escalation: USDA program manager to provision a new lot.
Precondition warns. Likely a data error (wrong lot mapped). Mike verifies the lot assignment before approving.
Retry policy is 2 linear-backoff attempts. Persistent failures alert; manual POST /admin/assembly/recompute recovers.
Sometimes a commodity drawdown happens with no open quotes. Cascade still completes; no flag rows stage.
Adjacent workflows + diagrams
Code paths + invariants
| Concern | Where |
|---|---|
| Workflow contract | workflow_definitions WHERE workflow_type='usda_drawdown_commit' |
| Lot table | usda_commodity_lots |
| Program table | usda_commodity_programs |
| Cost-basis pattern | weighted-average across active lots |
| Reflexion tag | usda_drawdown,sku:<sku> |
| Risk level | 4 |
| Expected duration | ~30 min |
| Trigger | event · sources=inventory_threshold_cron, manual_lot_close |