Metadata-Version: 2.5
Name: keble-ads-advice
Version: 0.6.8
Summary: The Amazon Ads Advice artifect kind plugin (AMZ_ADS_ADVICE + AMZ_ADS_CHANGES): screen, agent audit, change-list policy, renderer and workbook.
Requires-Python: <3.14,>=3.13
Requires-Dist: elta-contract<5,>=4
Requires-Dist: keble-artifect-contract<0.27,>=0.26.4
Requires-Dist: keble-data-infra-contract<1,>=0.53.0
Requires-Dist: keble-helpers<2,>=1.63.0
Requires-Dist: keble-pretty-md-response<0.3,>=0.2.2
Requires-Dist: openpyxl<4,>=3.1
Requires-Dist: pydantic<3,>=2.10
Description-Content-Type: text/markdown

# Keble Amazon Ads Advice

This repository owns the Amazon Ads advice Artifect as one isolated kind plugin under `artifect/`.
It exports `plugin(source)`: a `KindPlugin` with the run kind `AMZ_ADS_ADVICE` and its embedded
change-list kind `AMZ_ADS_CHANGES`, built against `keble-artifect-contract` 0.25 (plan B3.3).
Core (`keble-artifect`) owns every run's lifecycle, storage, replay, question pages and coverage,
deadlines and revisions; this package owns the meaning. Design history is in `arch/record/`.

## Boundary

- The `AMZ_ADS_ADVICE` wire models moved here byte-identically from keble-artifect `8db9bc9`
  (`schemas`, `vocabulary`); `tests/unit/goldens/` pins their released schemas, payloads and the
  EN/ZH card text.
- Own the Ads screen, the `ADS_ROW_AUDIT` question and its validation, completion (fold, one-click
  join, per-marketplace list outcome), removal, the card text and the pure Excel workbook.
- Read owner-fenced, typed Ads windows (with each marketplace's ELTA Ads account) through the
  backend's `AdsDataSource` once at `start`; no model runs here: the AI audit is the user's agent
  answering `ADS_ROW_AUDIT`; one-click rows go through core's shared change-list engine only.
- Never import core runtime modules; the runtime package `keble-artifect` is a test dependency.

## Runtime package

| Module | Responsibility |
|---|---|
| `plugin` | `plugin(AdsAdviceSource(windows, legacy, settings)) -> KindPlugin`. |
| `kinds` | The typed handles: `ADS_ROW_AUDIT`, `ADS_ADVICE_REMOVE`, `ADS_ADVICE_DETAIL`, `AMZ_ADS_ADVICE`, `AMZ_ADS_CHANGES`. |
| `hooks` | `AdsHooks`: `start` screens and asks the audit, `answer` validates and records or completes, `work` closes an expired audit with the answers so far, `adjust` removes advice, `stop` cancels. |
| `views` | `AdsViews` (snapshot, detail, audit page from the stored state) and `AdsRenderer` (agent text). |
| `display`, `display_labels` | The moved EN/ZH ads card text and finite words (one owner for card and workbook). |
| `legacy`, `records` | `AdsAdopter` over the backend's `AdsLegacyRunsPort`, and the released run document it reads (deleted after the adoption pass). |
| `schemas`, `vocabulary` | The released wire models and the plugin state `AdsAdviceState`. |
| `typings` | `ADS_ONE_CLICK_ROUTE`, the ELTA action handles, `AdsScreenThresholds`, `AdsAdviceSettings`, `AdsMarketplaceAccount`. |
| `evidence` | `AdsDataSource` Protocol and the screen's typed input models. |
| `advice` | The deterministic screen ported from ELTA1 plus the audit offer and validation. |
| `change_policy` | `AdsChangePolicy` (the `ChangeKindPolicy` of `AMZ_ADS_CHANGES`), `fold_advice`, `one_click_for`, `change_list_creates_for`. |
| `workbook` | `render_workbook(snapshot, language, store_label) -> WorkbookDownload`. |

`arch/` is architecture-only declarations; runtime code never imports it.

## One-click routing

`ADS_ONE_CLICK_ROUTE` stays pure static data: thirteen writable level/action pairs use
eight ELTA keys, including campaign PAUSE, ad-group LOWER_BID/RAISE_BID and (elta-amz 0.17.0)
product-ad PAUSE as `amazon.ads.set_product_ad_state` at level AD on the row's `ad_id`.
The backend sets `ad_id` from ELTA's `amazon.ads.sp_ad_settings` only when exactly one
ENABLED/PAUSED product ad matches the row's campaign, ad group and ASIN; otherwise the
pause stays manual (`NO_VALUE`). This package never maps an ASIN to an ad id.
Ad-group bids reuse target bid suggestion forms and currency rounding: relative
suggestions need the saved default; absolute suggestions do not. Campaign PAUSE
requests PAUSED without guessing the current state. Missing provider ids or unresolved
bid values produce `NO_VALUE`; ELTA reads the live baseline for the confirm diff and
records the apply-time before-value for guarded undo. No ELTA plugin dependency or
catalog network call enters this package; each routed key's level and change shape
are pinned by unit tests against the cited elta-amz declaration.

## Development

Python 3.13 with `uv`; TypeScript tooling is only needed for `pyright`.

```bash
uv sync --all-groups
uv run pytest
source ~/.nvm/nvm.sh && nvm use 21 && npx --yes pyright .
uvx ruff check keble_ads_advice tests
```

The core test kit (`keble_artifect.testing`) arrives with the dev7 core wheel; the hook paths
that need a run context (completion opening the change-list children, the G1 parity cases) are
tested there, never against a private fake of core.

## Dependency pins

`deps/` vendors every released wheel this repo resolves. The released contract `0.26.4`, the
released test-only runtime `0.37.0` and the released ELTA `4.2.0` contract (open level/refusal keys)
are vendored in `deps/` and pinned through relative `[tool.uv.sources]` paths. Contract dev6 admitted
the stored failure facts of adopted runs (R58/CG-108am) and the nullable frozen title. The kind cut is
`0.6.2`; already-taken dev artifacts are never rebuilt.

The backend's legacy reader must additionally pass `AdsLegacyRun.task_error = task.error` and
`task_exception_type = TaskExceptionType(task.exception_type.value)` (the Artifect contract enum),
or None for an absent class. The task row stores no separate failure code or blame. The adopter
carries only a FAILURE row's stored reason and class; an absent reason or another stage keeps
`AdoptedRun.failure=None`. Historical terminal adoption must not settle its charge again.

## Version history

- **0.6.8** — ELTA standard entity levels all declare no parent. One-click AD_GROUP and TARGET refs now send no parent; target labels retain the row's saved campaign › ad-group › own-name path, while ad-group labels retain campaign › ad-group. Missing names fall back to ids only where the former parent renderer did. Campaign and AD refs remain parentless. A focused recursive parent-law test covers ad-group bid/negative, target bid, product-ad, campaign budget/state, and fails on the 0.6.7 source.

- **0.6.7** — Artifect UX wave (T9/OB-2, D-19, T11/KD-2, LM-4, F6). Seller text now reads window words
  (`last 7 days` / `prior 7 days` / `day before` / `same day last week`; `近7天` / `前7天` / `前一天` /
  `上周同日`), and a reason or reasoning carrying `L7`/`P7`/`D-1`/`D-7` is refused `INVALID_EVIDENCE`
  (the codes stay in `evidenceRefs`). Every seeded one-click row fills the existing
  `EntityRef.label` (the row's own text) and `EntityRef.parent` (ad group › campaign, with labels), so
  the change list prints Amazon's path without the advice join; level AD keeps no parent, as ELTA
  declares none. The advice-page usage line and purpose name the advice page as the place the seller
  reviews and applies the one-click rows, with the run's own `AMZ_ADS_CHANGES` list as their engine
  (D-19). The change-list guide states the rationale direction, the text-only row naming, the draft
  word and the one-line-plus-one-step answer shape. `ArtifectAdsAdviceCreate` and
  `ArtifectAdsAdviceRemove` publish one JSON Schema `examples` body each (no new field, no payload
  change).

- **0.6.6** — CG-ADS-ADID-1: an SP product-ad PAUSE is one-click. `typings.SET_PRODUCT_AD_STATE`
  (`amazon.ads.set_product_ad_state`, elta-amz 0.17.0, SET_STATE at level AD, its own default-off
  `amazon.ads.product_ad_state` family) is the eighth `AMZ_ADS_CHANGES` action, and
  `(ADVERTISED_PRODUCT, PAUSE)` routes to it with `{"state": "PAUSED"}` on `EntityRef(AD, ad_id)`.
  `AdsEntityWindowRow` and `AdsAdviceRow` gain `ad_id: str | None = None`, which the backend sets from a
  unique ENABLED/PAUSED `sp_ad_settings` match on (campaign, ad group, ASIN); without it the row
  stays manual (`NO_VALUE`). The rejected-row family note names "product ad state". Wire change:
  `AdsAdviceRow` gains one nullable field, so the wire goldens and describe output change.

- **0.6.5** — CG-ADS-ADV-1: one total `vocabulary.ADS_VALID_ACTIONS` table names the actions valid
  at each level (CAMPAIGN: RAISE_BUDGET, PAUSE; AD_GROUP, KEYWORD, TARGET: LOWER_BID, RAISE_BID, PAUSE;
  SEARCH_TERM: NEGATE, HARVEST_EXACT; ADVERTISED_PRODUCT: PAUSE; HOLD everywhere). The audit
  instructions offer each level only its set, and an answer with any other action (LOWER_BID on a
  product ad) is refused `INVALID_EVIDENCE` naming the row's allowed actions; the one-click route is a
  tested subset. The stale "results are read-only" usage line now says one-click rows go to the run's
  `AMZ_ADS_CHANGES` list and the rest to the Amazon Ads console. Stored reviews are not re-validated.
  No wire model, enum value, golden or dependency change.

- **0.6.2** — CG-NO-APPROVAL-1 wording only: proposing a filing applies the listed changes to the
  store now, so no instruction, stage guide or refusal copy asks the seller to approve, confirm or
  decide a filing in their inbox and none waits in To do. No wire model, golden or dependency change.

- **0.6.1** — review-v3 fixes: a bid change that moves against its action is refused (ADV-1), the
  agent is told the screen's target ACoS the advice is judged against (ADV-5, D-C8), each refusal
  code and change-list action carries its own words (ADV-6, ADV-9, ADC-1, ADC-4, ADC-12, ADC-16), and
  a parent row that only repeats one child's numbers folds into it (ADV-8, D-C9). No wire model,
  golden or dependency changes.

- **0.6.0** — ELTA 4: elta-contract `>=4,<5` and Artifect core 0.26/0.37; key constants and `==`
  comparisons; no behavior change.

- **0.5.0** — R3 final: version and lock only, on core final (contract `0.25.0` / test runtime
  `0.36.0`); no source change since `0.5.0rc1` beyond the test-only per-park input key and ref-shape declarations.

- **0.5.0rc1** — R1 release candidate: version and lock only, on core rc1 (contract `0.25.0rc1` /
  test runtime `0.36.0rc1`); no source change since `0.5.0.dev7` beyond the label-only commit.

- **0.5.0.dev7** — R58/CG-108am on contract `0.25.0.dev6` / test runtime `0.36.0.dev7`:
  a failed legacy run carries its stored task error verbatim, its stored class by value, the released
  null refinement code and R23's unstored blame. The backend reader supplies `task_error` and
  `task_exception_type`; missing reasons and non-FAILURE rows keep `failure=None`. G1 byte-checks
  CANCELLED and FAILED_TO_START status and card reasons in en/zh, with no second settlement.
  Full kind suite: 185 passed; architecture: 17 passed, both with strict markers; pyright: 0 errors.
  No wire model, golden, backend source or `0.5.0.dev6` wheel changes.

- **0.5.0.dev6** — adoption restores released ADS-C6: an adopted run's `updated_at` is the later of
  `AdsLegacyRun.run.updated_at` and the legacy keble-task row's stored `updated` (`task_updated`),
  never `created_at` and never the read clock (released host.py@7f8dccf:515-528; R41). The adopter
  carries it from the joined task row; core previously fell back to `created_at` because `AdoptedRun`
  was built without it. G1 adds two rows over the legacy fixtures (task later than run; run later and
  task absent) asserting the later-of rule and the never-created/never-now boundary. Wire models,
  goldens and dependency pins are unchanged.

- **0.5.0.dev5** — contract 0.25.0.dev4 / test runtime 0.36.0.dev5. START stores its screened
  title once; adoption keeps the legacy title verbatim, including `None`, through later commits
  (amendment T). PROCESSING adoption schedules its next work now (R41). G1 declares D1,
  CG-108ad, create convergence (CG-108ae), the ten-second retryable CONFLICT (CG-108ae-a), and
  claim-before-spend with no loser hold (CG-108ae-c); it pins the released neutral failures.
  R26 and create-convergence xfails are passing assertions. Wire models and goldens are unchanged;
  the one marked hook typing gap awaits dev6.

- **0.5.0.dev4** — `AdsAdviceSource.changes_hosted=False` preserves released seed-only `NOT_HOSTED`
  absences without child creation; kit G1 records D1 wrong-key audit-page rejection bytes (parked
  owner revision, else head). Wire models and the contract/runtime dev3 pins stay unchanged.

- **0.5.0.dev3** — on contract 0.25.0.dev3 / runtime 0.36.0.dev3: `Kind.facts(create, now)` owns
  the released one-run-per-store-and-day key and future-day refusal; the retry fence reads
  `ctx.runs.head`; removing a linked row discards the child; titles come from `title_from_state`
  and `AdoptedRun.title`; the audit next step is a `status_line`; question coverage is `covered`;
  an equal-state cancel keeps the revision; adopted refs use the contract's `legacy_run_ref`. G1
  parity tests run on `keble_artifect.testing`.

- **0.5.0.dev2** — on contract 0.25.0.dev2: completion opens one `AMZ_ADS_CHANGES` child per
  seeded marketplace with the released `NewChangeRow` rows (a refused child is a `CREATE_FAILED`
  absence); `Kind.facts`; refusals carry `item_keys` and the released reasons, reads refuse with
  the released failure codes; the renderer and change policy add no refusal or answer lines; the
  adopter carries `worked`, `spend_id` and the released input key and refuses task-less orphans
  `SOURCE_INCOMPLETE`.

- **0.5.0.dev1** — the kind as one `KindPlugin` on contract 0.25.0.dev1: hooks, views, renderer,
  adopter and change policy replace the kind host and its run store; the ads wire models and EN/ZH
  wording move here from the contract. The child-list row seeding waits for the dev2 contract.

- **0.4.0** — dependency-only release: the artifect contract 0.24.0 / runtime 0.35.0 released
  wheels and the ELTA 3.5.0 set (no feature change).

- **0.3.0** — static ELTA campaign-pause and ad-group-bid intent routes, and the artifect
  contract 0.23.0 / runtime 0.34.0 released wheels (neutral `asnapshot` board-focus host).
- **0.2.0** — per-row change-list filing (`row_stages`), `AdsMatchType`, `AdsReadOnlyReason`,
  the remove arm, and on the artifect contract 0.22.0 / runtime 0.33.0 released wheels.
- **0.1.1** — `AdsAdviceSettings.run_database` and `task_database` are required (no
  `__keble_artifect__` fallback); the composing backend passes `MONGO_DB_DATABASE`.
  `AdsAdviceArtifectHost` now requires `settings`. Fixes the CO-498 startup failure class.
- **0.1.0** — first build line: the Ads kind moved out of `keble-artifect` plus the agent
  audit, change-list seed and workbook parity.
