fix: Amazon multi-shipment charge matching and Monarch fallback#40
Merged
Conversation
Adds CLIShipment type and GetItemsForCharge() on the Amazon Order adapter. When the scraper provides shipment groupings, each Monarch transaction in a multi-delivery order is split using only the items that were in that box rather than pro-rating the entire order. Falls back to all items when shipment data is absent or the order has a single shipment.
Three bugs fixed: 1. LLM returns invalid category ID (e.g. "Uncategorized") instead of a Monarch numeric ID → Monarch API rejects the update. Fix: validate each returned ID against the loaded category list; fall back to name match, then empty string if unresolvable. 2. When category ID is empty (unresolvable), the handler still tried to set it on the Monarch transaction, causing an API failure. Fix: skip the CategoryID field in UpdateTransactionParams when the ID is empty so we still write notes but don't fail the order. 3. LLM occasionally returns extra categorization entries (hallucination) for orders with fewer items. The extra entries inflated categoryGroups to size >1, sending a single-item order through the splits path with only 1 split → Monarch "must be split into two or more" error. Fix: truncate LLM results to len(uncachedItems) in CategorizeItems, and cap the categoryGroups loop in CreateSplits to len(items). Also updated the categorizer prompt to remove the Walmart-specific framing and explicitly instruct the model to use exact IDs from the list.
…ization After setting category/splits via the Monarch API, transactions left in needsReview=true state are re-processed by Monarch's rule engine, which resets the category back to the rule's target (e.g. [TEMP] Amazon). Fix: set NeedsReview=false on UpdateTransaction for single-category updates. For split updates, call a follow-up UpdateTransaction after UpdateSplits to clear the review flag, since updateTransactionSplit doesn't expose needsReview directly.
…arges When the Amazon scraper returns charges that don't sum to the order total (e.g. a shipment charge posted after the scraper visited the page), try to find a matching set of Monarch transactions via subset-sum search before giving up. Uses a 10-day date window and recursive backtracking over the small candidate set (typically 1–5 transactions). Also fixes a nil matcher panic in TestAmazonHandler_ProcessOrder_InvalidCharges — the test now supplies a real matcher since the handler calls FindSubsetByTotal on validation failure.
Codecov Report❌ Patch coverage is ❌ Your patch status has failed because the patch coverage (42.55%) is below the target coverage (80.00%). You can increase the patch coverage or adjust the target coverage. Additional details and impacted files@@ Coverage Diff @@
## main #40 +/- ##
==========================================
- Coverage 61.48% 60.28% -1.20%
==========================================
Files 44 45 +1
Lines 5045 5200 +155
==========================================
+ Hits 3102 3135 +33
- Misses 1763 1878 +115
- Partials 180 187 +7
Flags with carried forward coverage won't be shown. Click here to find out more.
🚀 New features to boost your workflow:
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Four changes that together fix categorization of multi-shipment Amazon orders:
GetItemsForChargeestimates each shipment's charge (subtotal × tax rate) and returns the correct item subset for that shipment, so splits show the right items per Monarch transactionFindSubsetByTotalsearches unmatched Monarch transactions via subset-sum to recover the match. New file:internal/domain/matcher/subset.goTest plan
go test ./... -racepasses./itemize amazon -dry-run -days 30 -forcewithAMAZON_ACCOUNT_NAME=amazon-wifeshows 22 orders would be processed, including multi-shipment orders 112-7165040-1665851 and 112-9922593-0590612