Skip to content

feat(training-agent): implement package and shared MediaBuy frequency caps - #7494

Merged
bokelley merged 2 commits into
mainfrom
feat/training-agent-frequency-caps
Sep 14, 2026
Merged

bokelley merged 2 commits into
mainfrom
feat/training-agent-frequency-caps

Conversation

@bokelley

@bokelley bokelley commented Sep 14, 2026 •

Copy link
Copy Markdown
Contributor

Closes #7488.

Why

#7438 and #7449 shipped structured package frequency-cap constraints and the shared MediaBuy-level cap with three compliance scenarios, but every one of them was gated on a capability the reference training seller never advertised, so none of them had ever executed. This makes the training agent implement the spec section "Frequency-cap scope and product support" as written, adds the follow-up media_buy_frequency_cap_updates scenario, and gets all four frequency-cap scenarios running green in the storyboard matrix.

What changed

Training agent (server/src/training-agent/)

  • New frequency-caps.ts: seller-wide declarations (media_buy.frequency_capping, media_buy.aggregate_frequency_capping), product support resolution with seller-wide inheritance (omitted structured fields inherit and never broaden; product values absent from the seller-wide list are dropped), requirement containment (frequency_cap_support, required_media_buy_support, exact media_buy_frequency_cap), cap validation against control mode / per unit / count preset-or-range / window unit-and-interval / suppression, root-cap validation against seller-wide plus every selected product, and mutable_fields change detection.
  • Capabilities: both declarations on the sales tenant router and the legacy capabilities handler (aggregate gated to 3.2+ responses).
  • Products: default catalog products declare legacy overlay_support.frequency_cap: true and media_buy_support.frequency_cap: true; controller-seeded overlay_support.frequency_cap / frequency_cap_support / media_buy_support flow through the existing fixture path unchanged.
  • Discovery: required_overlay_support.frequency_cap_support matches legacy true or a containing structured declaration after inheritance (mutable_fields: [] never matches a non-empty requirement); required_media_buy_support and media_buy_frequency_cap filter on participation and exact executability; media_buy_support is projected whenever requested regardless of fields; concrete targeting_overlay.frequency_cap at discovery resolves through either declaration form; legacy proposals echo frequency_cap and compact proposals carry commercial_terms.frequency_cap.
  • Create and buy: package caps validated against the product's resolved constraints, root caps against seller-wide plus every selected product; UNSUPPORTED_FEATURE before any mutation, never clamped; package caps echoed in packages[].targeting_overlay, root cap at the response root; proposal execution takes the cap from accepted terms; buy_products binds the root cap into the accepted commercial terms.
  • Read-back: get_media_buys echoes root and package caps separately; available_actions[] carries update_frequency_caps with applicable_package_ids naming only eligible packages when the buy is heterogeneous, and update_media_buy_frequency_cap when every active product participates and can change the root cap.
  • Update and control: a targeting-overlay update that changes only the cap now maps to update_frequency_caps alone (not update_targeting); ineligible package → ACTION_NOT_ALLOWED; field outside mutable_fields or outside constraints → UNSUPPORTED_FEATURE; root replace and null clear through control_media_buy and update_media_buy with no counter reset; new_packages rejected atomically when a product cannot join, evaluated on the resulting state (frequency_cap: null + new_packages adds them uncapped); root-cap changes outside accepted terms → REQUOTE_REQUIRED; ACTION_NOT_ALLOWED.attempted_action may be update_media_buy_frequency_cap.
  • Proposal refinement: remove_media_buy_frequency_cap: true clears an inherited root cap, criteria.media_buy_frequency_cap replaces it, and a revised mix that cannot share the cap is unable with unsatisfied_constraints: ["media_buy_frequency_cap"].
  • Delivery: when a package or root cap is enforced, totals report reach and frequency with observed frequency at or below the tightest cap.
  • Account bridging: requests that name the account by account_id (the runner sends raw sample requests for expect_error steps and control_media_buy) now reach the same media buy and fixture catalog as natural-form requests for the same synced account.

Compliance

  • New media_buy_frequency_cap_updates scenario (17 steps): mutable-root discovery, create with root + independent package cap, update_media_buy_frequency_cap availability, root replace via control_media_buy, new_packages rejection, frequency_cap: null + new_packages resulting-state rule, and ACTION_NOT_ALLOWED once the mix can no longer share a counter.
  • Two fixes to the existing scenarios so they can run against a seller that requires bids: explicit bid_price on auction packages after the first (the SDK runner only injects a bid for the first package), and affected_package_ids[*] wildcard paths for field_contains membership (a bare array path compares the scalar against the whole array).

Spec (docs/media-buy/specification.mdx): the package-cap paragraph that lists ACTION_NOT_ALLOWED / UNSUPPORTED_FEATURE / REQUOTE_REQUIRED now states the check order explicitly (available_actions authorization, then product capability, then accepted commercial terms; the earliest failing check wins), and the root-cap paragraph applies the same order, naming ACTION_NOT_ALLOWED for a change without update_media_buy_frequency_cap. The root-cap text previously said the action was required without naming the error code, which the new scenario's negative path needed. The training agent evaluates in that order at both levels: control_media_buy runs the cap gate ahead of the accepted-envelope REQUOTE_REQUIRED checks, covered by new unit tests (out-of-range count on a create-only package → ACTION_NOT_ALLOWED; a cap outside seller-wide capability that also differs from the accepted purchase → UNSUPPORTED_FEATURE) and by a new frequency_cap_capability_updates step (reject_create_only_out_of_range_update) that the original in-range step could not distinguish.

Testing

Frequency-cap scenarios, TENANT_PATH=sales, current source bundle (ADCP_STORYBOARD_CANDIDATE_VERSION_MODE=1, as CI runs PRs to main), final code:

Storyboard Result
media_buy_seller/frequency_cap_enforcement ✓ 7 passed / 0 skipped / 0 N/A
media_buy_seller/frequency_cap_capability_updates ✓ 13 passed / 0 skipped / 0 N/A (one new step, see below)
media_buy_seller/media_buy_frequency_cap_readback ✓ 14 passed / 0 skipped / 0 N/A
media_buy_seller/media_buy_frequency_cap_updates (new) ✓ 17 passed / 0 skipped / 0 N/A

storyboards: 4/4 clean | selection: 4 applicable | steps: 51 passed | 0 failed | 0 skipped (re-run on the final head). Before this change all four graded capability_unsupported (0 steps executed) because neither media_buy.frequency_capping nor media_buy.aggregate_frequency_capping was advertised.

Full bash scripts/run-storyboards-matrix.sh (same env), every tenant above its floor and all 31 required-clean / required-exact checks passing:

Tenant Floor (clean / steps) Result
signals 45 / 80 ✓ 53 clean, 86 steps, 54 applicable, 1 quarantined
sales 133 / 632 ✓ 156 clean, 821 steps, 157 applicable, 2 quarantined
governance 47 / 161 ✓ 57 clean, 161 steps
creative 49 / 209 ✓ 59 clean, 209 steps
creative-builder 50 / 184 ✓ 65 clean, 199 steps
brand 45 / 116 ✓ 56 clean, 116 steps
si 42 / 50 ✓ 51 clean, 54 steps (run as a single tenant after the sandbox exceeded the 60‑minute wrapper timeout mid-si; si_baseline and si_sponsored_context_accountability clean)

Also run locally:

  • npx tsc --project server/tsconfig.json --noEmit clean.
  • vitest run --config server/vitest.config.ts tests/unit/ (full server unit suite, includes the new training-agent-frequency-caps.test.ts with 12 tests and 5 new root-cap cases in proposal-negotiation-profiles.test.ts) 614 files, 9388 passed, 31 skipped; npm run test:unit 71 files, 1063 passed.
  • node --test tests/lint-storyboard-*.test.cjs 228/228; npm run build:compliance (symbol links current); test:storyboard-doc-parity, test:compliance-source-authority, test:compliance-packaged-refs, test:sdk-runner-capability-gates, test:doc-compliance-drift, test:storyboard-sample-request-schema, test:storyboard-response-schema, test:compliance-snippets, test:json-schema, test:schemas all pass.
  • node scripts/check-changeset-protocol-scope.cjs origin/main and changeset status --since=origin/main (one patch bump) pass.

Expert review: code-reviewer, security-reviewer, and ad-tech-protocol-expert agents ran on the diff; every Must Fix / Should Fix finding was addressed (two-pass authorization-before-capability ordering, exact-overlap matching for id→natural account bridging, owner-partition catalog precedence, root-cap mutable_fields enforcement, structural cap validation, softened over-constraining scenario assertions, PACKAGE_NOT_FOUND preserved for unknown new_packages products).

Behavior notes for reviewers:

  • A byte-identical restated targeting_overlay no longer classifies as update_targeting (it is an exact restatement, not a change); a cap-only overlay on a package that has other targeting still classifies as both update_targeting and update_frequency_caps because the overlay replaces the whole targeting state.
  • Delivery totals now surface reach/frequency for any buy with an enforced frequency cap, not only reach-goal buys, and the placeholder reach rounds up so impressions / reach never exceeds the cap (a reach-goal buy at 100 impressions now reports reach 34 instead of 33).
  • The cross-session media-buy fallback added to control_media_buy mirrors the pre-existing update_media_buy and get_media_buys fallbacks; it does not widen what an authenticated principal can reach, but the pre-existing brand-plus-operator matching for natural-reference buys under one principal is unchanged.

Follow-ups

  • @adcp/sdk 14.0.0-rc.35 validateRefineProposalsRequest rejects remove_media_buy_frequency_cap (not in its revise-key allowlist) and criteria.media_buy_frequency_cap / criteria.required_media_buy_support (not in its criteria-key allowlist), so the training agent's refinement handling is unit-tested directly and cannot yet be exercised through the hosted refine_proposals tool. Tracked in refine_proposals: SDK preflight rejects remove_media_buy_frequency_cap and criteria.media_buy_frequency_cap #7493, which also covers the scenario step to add once an SDK with the allowlist fix is adopted.

🤖 Generated with Claude Code


Open workspace in Conductor

… caps

Closes #7488. The three frequency-cap compliance scenarios from #7438 and
#7449 were gated on capabilities the reference seller never advertised, so
they had never executed. The training agent now implements the spec section
"Frequency-cap scope and product support":

- Advertise media_buy.frequency_capping and the complete executable
  media_buy.aggregate_frequency_capping domain (sales tenant and legacy
  capabilities handler).
- Resolve product support with seller-wide inheritance for
  overlay_support.frequency_cap / frequency_cap_support and
  media_buy_support.frequency_cap (+ frequency_cap_constraints); default
  catalog products declare both legacy package support and shared-counter
  participation.
- Discovery honours required_overlay_support.frequency_cap_support,
  required_media_buy_support, and exact media_buy_frequency_cap; returned
  products carry media_buy_support regardless of fields; proposals echo the
  bound root cap.
- Create, buy, update, and control validate package caps against resolved
  product constraints and root caps against seller-wide plus every product,
  rejecting with UNSUPPORTED_FEATURE before any mutation and never clamping.
  Root caps replace or clear (null) without resetting counters; new_packages
  are checked against the resulting state; ineligible packages and
  non-mutable root caps return ACTION_NOT_ALLOWED before capability checks.
- available_actions carries update_frequency_caps with applicable_package_ids
  for heterogeneous buys and update_media_buy_frequency_cap when every active
  product can change the root cap.
- Proposal refinement clears an inherited root cap through
  remove_media_buy_frequency_cap, replaces it through
  criteria.media_buy_frequency_cap, and is unable when the revised mix cannot
  share it.
- Delivery totals report reach and frequency at or below the tightest
  enforced cap.
- Requests addressing a synced account by account_id reach the same media
  buy and fixture catalog as natural-form requests.

Compliance: add media_buy_frequency_cap_updates; give the multi-package
frequency-cap scenarios explicit bids after the first package (the runner
only injects a bid for the first) and use affected_package_ids[*] for
membership checks. Spec: state that a root-cap change without
update_media_buy_frequency_cap returns ACTION_NOT_ALLOWED ahead of the
capability check, and that mutable_fields governs changes to an existing cap.

Co-Authored-By: Claude Fable 5.1 <[email protected]>
aao-secretariat[bot]
aao-secretariat Bot previously approved these changes Sep 14, 2026

@aao-secretariat aao-secretariat Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ladon verdict: Approve

Approve — reference-seller implementation of package and shared MediaBuy frequency caps.

What I checked:

  • No schema-source change (static/schemas/source/** untouched); the wire surface shipped previously in #7438/#7449 and is present in base schemas, so schema↔docs coherence is not at issue here.
  • The docs change (docs/media-buy/specification.mdx) is a one-sentence normative clarification, consistent with the existing wire shape.
  • Additive compliance scenarios under static/compliance/source/protocols/media-buy/scenarios/** — new files, not edits to released dist/** artifacts, so immutability rules are not implicated.
  • Changeset present (.changeset/training-agent-frequency-caps.md) and correctly scoped as a patch for the reference-seller/training-agent implementation.
  • Reviewer verified authorization-before-capability ordering, resulting-state root-cap rule, AND-semantics validation, and cross-session account bridging against the implementation and test suite.
  • New tests accompany the new branches (proposal-negotiation-profiles, frequency-caps).

Decision path: No critical/high/medium findings. gated_paths is false, high_risk is false, no no-auto-approve team match, no prior decision. Rows 1–8 do not fire (review_decision: REVIEW_REQUIRED does not block absent a gated path). Falls through to row 9 → approve.

…cap levels

The package-cap paragraph never stated that available_actions authorization
runs before the capability check, so the root-cap sentence added earlier
referenced a rule the spec did not make. State the order once for package
caps (authorization, then capability, then accepted commercial terms) and
have the root-cap paragraph apply the same order.

Make the training agent honour that order for package caps on
control_media_buy as well: the cap gate now runs ahead of the accepted-
envelope REQUOTE_REQUIRED checks, so a cap that is both outside capability
and outside accepted terms reports UNSUPPORTED_FEATURE. Add unit coverage
for authorization-before-capability (out-of-range count on a create-only
package, mixed request) and capability-before-terms, and a
frequency_cap_capability_updates step that sends an out-of-range count to
the create-only package and expects ACTION_NOT_ALLOWED.

Co-Authored-By: Claude Fable 5.1 <[email protected]>

@aao-secretariat aao-secretariat Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ladon verdict: Approve

Approve — clean subsequent pass, no blocking findings.

This is a follow-up to a prior approve (head 1473e2d). The reviewer reports zero findings. The delta is correct and consistent: explicit error-code ordering in docs/media-buy/specification.mdx, a pure idempotent frequencyCapMutationRejection gate added to handleControlMediaBuyUnlocked ahead of the REQUOTE_REQUIRED loop, with authorization → capability → accepted-terms ordering and no false rejection on the accepted-terms path. No schema/dist/oneOf/async surface touched; a changeset is present.

Gate checks:

  • No critical/high findings (row 1 clear).
  • gated_paths: false — row 2 does not apply despite review_decision being REVIEW_REQUIRED.
  • high_risk: false — rows 3/5 clear.
  • No medium findings (rows 4/8 clear).
  • No no-auto-approve team match (row 7 clear).
  • Prior decision was approve, not escalate (row 6 clear).

No breaking-class protocol change: this is a training-agent server behavior addition plus normative doc clarification with proper changeset scope. Falls through to row 9 → approve.

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.

feat(training-agent): implement package and shared frequency caps so the capability-gated scenarios run

1 participant