Skip to content

Fix supermarket basket orphan rows and add remove_basket_item - #459

Merged
adri merged 10 commits into
masterfrom
adri/basket-orphan-rows
Nov 10, 2026
Merged

adri merged 10 commits into
masterfrom
adri/basket-orphan-rows

Conversation

@adri

@adri adri commented Nov 9, 2026

Copy link
Copy Markdown
Owner

Summary

  • edit_ingredient remaps now move the basket row: the old supermarket
    product leaves the basket and the new one gets the wanted quantity, with
    the same placed-order freeze as every other basket write.
  • New MCP tool remove_basket_item deletes a basket row that no ingredient
    is linked to (ingredient: null in get_shopping_list), by supermarket
    product id. It refuses linked rows (points to set_ingredient_quantity),
    missing products, non-ah modes and the placed-order freeze.
  • start_shopping now removes the closed plan's own products from the
    basket so the next order does not inherit them. Rows Picape does not own,
    like products added in the supermarket's own app, stay.
  • CartItems.apply_changes sends the target quantity for reductions instead
    of always 0, so shrinking a row no longer deletes it.
  • archive_placed_order fetches the cart before writing the PlacedOrder
    marker, so a flaky fetch no longer skips the archive on every retry.
  • mark_recipe_as_cooked updates an existing plan row only; it no longer
    inserts phantom order_planned_recipe rows for unplanned recipes.

Test plan

  • mix check — format, credo, compile, 206 tests, sobelow all pass
  • New regression tests for remap swap, freeze, off-order remap,
    orphan removal (5 cases), order-close cleanup preserving external
    rows, quantity reduction, flaky archive fetch, cooked marking
  • No GraphQL schema or frontend changes

Reported by the grocery agent on 9 Nov 2026 (orphan rows after remaps, leftovers after closing an order). Written with Devin SWE-2 Flash High; adversarial review by SWE-2 High follows. Not for merge until reviewed.

adri and others added 10 commits November 9, 2026 20:31
Order.Sync puts the wanted quantity in changes.remove, but
CartItems.apply_changes sent quantity 0 for every removal, so
shrinking a row deleted it instead of reducing it. A decrease
from 3 to 1 now leaves 1 in the basket.

Generated with [Devin](https://devin.ai)

Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
edit_ingredient only changed the database, so Order.Sync kept
 treating the old product as an external basket row and it stayed
 forever with no ingredient linked. Order.remap_basket_product now
 swaps the row while the ingredient is on the current order, keeps
 the wanted quantity, and writes nothing while the order is placed
 or the ingredient is not on the list.

Generated with [Devin](https://devin.ai)

Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
Rows with ingredient: null had no MCP cleanup path: the agent
 could not touch them and the user deleted them by hand. The new
 tool removes one by supermarket product id, refuses rows an
 ingredient claims, missing products, non-ah modes and the
 placed-order freeze, and reports what it removed.

Generated with [Devin](https://devin.ai)

Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
start_shopping moved the plan rows and snapshotted the basket,
but the plan's products stayed behind. The next sync saw them
as external rows and kept them, so leftovers from the closed
order lingered in the new one. Closing now removes exactly the
products the plan wanted, after substitution, intersected with
what is in the basket. Rows Picape does not own, like products
added in the supermarket's own app, stay. Frozen while the
paid order is still being processed.

Generated with [Devin](https://devin.ai)

Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
mark_recipe_as_cooked inserted a PlannedRecipe row for a recipe
that was never planned, which made it look planned and reported
is_cooked: false anyway because cooked is not a cast field. It
now updates an existing row only and reports :not_on_order.

archive_placed_order wrote the PlacedOrder marker before
fetching the cart. A flaky fetch raised after the marker
committed, so every retry skipped the archive and the plan
stayed on the open order. The fetch now runs first and falls
back to an empty snapshot like the other supermarket reads.

Generated with [Devin](https://devin.ai)

Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
remap_basket_product read the wanted quantity from cart/1,
which is keyed per ingredient and ignores substitution. The
wanted set sync actually pushes is substitute/2 of the merged
plan quantities, so a remap onto an unavailable product could
add a row sync never wanted. Both remap and the order-close
cleanup now share wanted_basket_quantities/2.

Generated with [Devin](https://devin.ai)

Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
UpdateMyListBasket can answer SUCCESS while a row survives, for
example a row in externalItems that the fake keeps untouched. After
each removal or swap, invalidate the cart cache, read it back and
check the product is gone, or that the swapped product shows the
expected quantity.

remove_basket_item now returns an error naming the section when the
supermarket keeps the row, and publishes the order change on success
like start_shopping does. The swap and the close-order cleanup run
inside larger operations, so they log a warning instead.

Also refresh the cart before remove_basket_item reads it, and cover
the tool's picnic mode, string and missing product id, and the
surviving externalItems row with tests.
The close-order cleanup only removed products the plan wanted at a
positive quantity, so a row the plan wanted gone stayed in the
basket, and a product pushed before it became unavailable with a
replacement was never considered either.

wanted_basket_quantities now returns the wanted set both before and
after substitution, and shopped_product_ids unions both key sets
without the positive-quantity filter, matching sync semantics: a
wanted zero means sync removes the row, and either side of a
substitution may be the row that sits in the basket. Anything else
may be the user's own addition in the supermarket's app and stays.
remap_basket_product required the old product to sit in the basket
before it pushed the new one. When the old row was already absent,
for example after a failed push, the swap was skipped and the new
product never reached the basket: edit_ingredient reported
ordered_quantity 0 and the ingredient vanished from the shopping
list.

The swap now reads the substituted wanted quantity and pushes the
new product unconditionally; removing an absent old row is a no-op.
It also invalidates the cart cache before reading, like the other
basket writes, and the doc says the row lands at the plan's wanted
quantity, not the old row's quantity.
The archive comment claimed the cart fetch runs before the marker,
but what makes a flaky read safe is that
ordered_supermarket_items rescues it to an empty snapshot like the
other supermarket reads. The README said the remapped row keeps its
quantity; it actually lands at the plan's wanted quantity.
@adri
adri merged commit 444902b into master Nov 10, 2026
3 checks passed
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