stage 6 of 6 · try of as-shipped

cancel-before-pickup

right

Cancel a shipment unless it is too late (needs from the docs: cancel path, 409 once collected)

Made
28 Nov 2026, 20:39 UTC
Model
claude-sonnet-5-5
Steps
5
Tool calls
3
Friction
1 flagged step
Tokens read
100k
Time
12s
Cost
$0.029
Address
tern-docs/try/20261128-2029-7znr/cancel-before-pickup
1quote-at-checkoutright2quote-optionsright3book-shipmentright4sync-shipmentsright5order-statusright6cancel-before-pickupright
diff --git a/shop/shipping.py b/shop/shipping.py
index 5c4358a..9fd1edb 100644
--- a/shop/shipping.py
+++ b/shop/shipping.py
@@ -2,7 +2,8 @@
 from datetime import date
 
 from shop.cart import Cart
-from shop.orders import Order
+from shop.orders import CANCELLED, Order
+from shop.tern import call
 
 
 def quote_at_checkout(cart: Cart, postcode: str) -> int:
@@ -33,4 +34,12 @@ def order_status(shipment_id: str) -> str:
 
 def cancel(order: Order) -> bool:
     """Cancel the order's Tern shipment. True if Tern cancelled it; False if it is too late to cancel."""
-    raise NotImplementedError
+    if order.shipment_id is None:
+        raise ValueError(f"order {order.id} has no Tern shipment to cancel")
+    status, body = call("POST", f"/v2/shipments/{order.shipment_id}/cancel")
+    if status == 200:
+        order.status = CANCELLED
+        return True
+    if status == 409:
+        return False
+    raise RuntimeError(f"Tern cancel failed for {order.shipment_id}: {status} {body}")
Previous stage: 5. order-statusLast stage of this try