---
name: target-grocery-sync
description: Sync Target grocery orders from the last 30 days into the canonical purchase tracker ledger and yearly merchant CSV files using the authenticated automation browser profile.
---

# Target Grocery Sync

## Target Humanization
- Use native input events only for login and all credential entry.
- Introduce randomized delays between interactions (100–400ms baseline) and avoid instant sequential navigation.
- Enter through Target order-history URLs (`/orders` or `/account/orders`), not bare `/login`.
- Check for bot-detection signals before proceeding: CAPTCHA, challenge pages, blank/non-interactive form renders, or unexpected auth shells.
- Record any Target-specific humanization adaptations in `references/extraction-notes.md`.

1. Step 0: Confirm automation browser profile (`/Users/openclaw/.openclaw/browser/merchant-automation/user-data`) session is active before navigation.
2. Detect logged-out state (URL contains `/login` or sign-in prompts are visible). If logged out, call `scripts/get_credential.sh target.com`, complete Target's two-step sign-in (username/email/mobile + Continue, then password) using native typing semantics only, and enable any remember-me/stay-signed-in option present. If login returns `Please enter a valid email or phone number`, stop and report credential-mapping blocker.
3. Open Target order history and ensure all fulfillment tabs/segments are covered.
4. Capture page snapshots at each major step.
5. Collect orders from the last 30 days across all fulfillment segments; set `fulfillment_type` per segment label.
6. For each detail page, run expand + repeated bottom scroll + load-more/pagination until stable.
7. Explicitly detect and click `Show all`/`View all items` controls before extraction.
8. Extract all visible line items and adjustment lines separately.
9. Branch extraction by detail route family and preserve branch metadata:
   - Online detail routes: `/orders/{id}`
   - In-store detail routes: `/orders/stores/{id}`
10. Run strict sanity/reconciliation gate (5%): if extracted item subtotal (+ adjustments) does not reconcile with order total within 5%, mark order `partial-sync` and do **not** write that order/items/adjustments to canonical tables.
11. Record strict-gate skips in `purchase_order_exceptions` (exception lane) with retry metadata and extraction context (`route_family`, totals, delta, detail URL, artifacts).
12. Send one lightweight Telegram alert when a single sync run exceeds 2 `partial-sync` exceptions for Target (include merchant, count, and note to check `purchase_order_exceptions`).
13. Write normalized payload via `scripts/sync_to_grocery_db.py` using canonical DB/classification/log paths.
14. Merge yearly CSV exports with `scripts/merge_grocery_csv.py`.
15. Stop and report blocker on login failure, CAPTCHA, or unrecoverable page-design breakage.

## References
- Shared schema: `../grocery-shared/references/schema.md`
- Shared input utility: `../shared/common_input.js`
- Merchant notes: `references/extraction-notes.md`
