Skip to content

feat(training-agent): deterministic inventory delivery engine (#9025) - #9037

Merged
bokelley merged 2 commits into
mainfrom
inventory-sim/engine
Nov 15, 2026
Merged

bokelley merged 2 commits into
mainfrom
inventory-sim/engine

Conversation

@bokelley

@bokelley bokelley commented Nov 15, 2026 •

Copy link
Copy Markdown
Contributor

Part B of #9025, slice 1 of 5. This is the engine behind the two simulated inventory agents (the inventory role in #8993). Nothing serves it in this PR.

Stack: this PR, then #9038 (ad server tenant), then #9039 (sandbox_clock), then #9040 (content tenant), then #9041 (docs and deploy smoke). Review and merge them in that order.

Decisions confirmed with @bokelley for the series:

  • The tenants are inventory-adserver and inventory-content.
  • sandbox_clock is a new scenario, offered on those two tenants only. It moves the same per-caller simulated_now that reporting_subscription_probe already moves.
  • The engine lives in the training agent for now.

What it is

The engine is in server/src/training-agent/inventory-sim/engine/. It imports nothing from the rest of the training agent, so it can move into @adcp/sdk unchanged. The same seed and the same calls always give the same results. Every random draw is keyed by what it is for, using FNV-1a and mulberry32 with integer arithmetic only, so results don't depend on the order of calls.

  • Season seed (types.ts, seeds/). A seed defines:
    • regions, audience profiles that don't overlap, and segments (each a set of profiles);
    • properties, and placements with their formats, floor price, rates and reporting delay;
    • products and collections;
    • the weekday shape and trend of traffic, and how far actual traffic strays from the forecast;
    • scripted events: traffic multipliers, and postponed or cancelled installments.
  • Traffic (traffic.ts).
    • It is generated once per seed as cells. A cell is one day × region × property × placement × profile, plus the installment for content placements.
    • Each cell has a forecast and an actual impression count; the actual differs by day-level and cell-level noise and by scripted events.
    • Each cell also carries viewability, click rate, completion rate and conversion rate.
    • An installment's audience builds over its channel's curve (curves.ts): newsletter over 3 days, podcast over 30, video over 42.
  • Day steps (simulation.ts). Each simulated day runs in this order:
    1. Buys confirmed by the end of day N start serving on day N+1.
    2. Share-of-voice buys take their share.
    3. Buys with an impression goal are served by the high-water mark method (allocation.ts).
      • Each buy's daily target is its remaining goal divided by its remaining days.
      • Buys go in order of the supply they could use, smallest first, with ties broken by booking id.
      • Each takes the same share of every cell it can use, and never more than its target.
    4. A first-price auction above the floor takes what is left, until each bid's daily budget is spent.
    5. Delivery is recorded per buy and per cell.
  • Forecasts subtract what guaranteed buys have reserved, and are returned in AdCP's forecast shape (forecast-shape.ts).
  • Reporting: a day's delivery is released once the day has closed and the product's reporting delay has passed.
  • Two seeds:
    • adserver-2027: display, video and a homepage takeover. Scripted events: a five-day outage on the sports hero unit (2027-01-08 to 2027-01-12) and a spike in news traffic.
    • content-2027: a podcast, a newsletter and a creator series. Scripted events: episode morning_brief_012 is cancelled, and video maker_studio_006 is postponed to 2027-01-23.

Tests

server/tests/unit/inventory-sim-engine.test.ts has 22 tests.

  • Allocation primitives, including the case from the brief: two cells of 100 impressions; buy A can use both, buy B only the first, and each needs 100. Both are met: B goes first and takes the first cell, and A takes the second.
  • Traffic:
    • the same seed always generates the same tables;
    • actual traffic differs from the forecast;
    • targeting two overlapping segments counts each profile once;
    • the audience curves;
    • an inconsistent seed is refused.
  • Simulation:
    • a buy serves from the day after it is confirmed;
    • a guarantee on scarce inventory falls behind during an outage, then catches up and meets its goal exactly;
    • the auction gets only what guarantees leave;
    • forecasts subtract existing bookings;
    • delivery is held back until the reporting delay passes;
    • the same calls give the same state;
    • a second share-of-voice buy on the same inventory is refused;
    • pause and cancel.
  • Installments:
    • the cancelled episode delivers nothing;
    • the postponed video runs on its new date;
    • podcast listens arrive over 30 days.

Checks run locally

  • npx tsc --project server/tsconfig.json --noEmit: clean.
  • tests/lint-format-identity-boundaries.test.cjs: passes.
  • server/tests/unit/inventory-sim-engine.test.ts and server/tests/unit/training-agent-inventory-tenants.test.ts (run from the repo root): pass.

Committed and pushed with HUSKY=0, which @bokelley approved for this series. Under host load of 40 to 80, the pre-commit server unit suite timed out in unrelated suites (addie/matched-pair-ni/exact-inference). A local storyboard matrix run against the stack's tip was stopped after 66 minutes, by which point /signals had passed (57 clean against a floor of 45). CI's storyboard job is the check that existing storyboards still pass.

No changeset: this touches only the training agent and docs, not the protocol surface.

Refs #9025, #8993

🤖 Generated with Claude Code

Part B of #9025, slice 1: the engine behind the two inventory tenants.
It lives in server/src/training-agent/inventory-sim/engine/ with no
imports from the rest of the training agent, so it can move into
@adcp/sdk later.

- A season seed defines regions, non-overlapping audience profiles,
  segments, properties, placements (with formats, floors and rates),
  products, collections, traffic shape, forecast error and scripted
  events.
- Traffic is generated once per seed as cells (day x region x property
  x placement x profile, plus installment for content placements), in a
  forecast table and an actual table that differs by keyed noise and
  events.
- Each simulated day serves bookings confirmed by the end of the
  previous day; share-of-voice first, then impression goals by the
  high-water mark method, then a first-price auction above the floor;
  installments run, postpone or cancel; delivery is recorded per
  booking and per cell.
- Forecasts subtract guaranteed reservations and come out in AdCP's
  forecast shape. Delivery is released at each product's reporting lag.
- Two seeds: an ad server (display, video, takeover, a scripted outage
  and a traffic spike) and a content platform (podcast, newsletter,
  creator series, a cancelled episode and a postponed video).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
aao-secretariat[bot]
aao-secretariat Bot previously approved these changes Nov 15, 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 — self-contained, deterministic inventory-delivery engine plus unit tests, all new files (2240 additions, 0 deletions).

Checked:

  • No protocol surface touched (no static/schemas/source/, docs/reference/, or dist/** changes), so no changeset/schema-docs/immutability/oneOf obligations apply.
  • No external I/O; operational-readiness N/A.
  • Allocation math exact and never over-allocates (allocation.ts takeEqualShare largest-remainder, L54-71); no div-by-zero (serving filter guarantees day<flight_end_day).
  • Determinism holds via keyed FNV-1a/mulberry32 draws and immutable per-seed SeasonTraffic cache.
  • Forecast reservations clamp booked to capacity; SOV sold-out and spend cap sound.

Only finding is Low (test fixture uses format_ids instead of formats — cosmetic, tests excluded from tsconfig). Low findings do not block.

Gate status: gated_paths false, no high-risk paths, no no-auto-approve team match, no prior decision. No critical/high/medium findings. Falls through decision table to row 9.

Nit (non-blocking):

  • server/tests/unit/inventory-sim-engine.test.ts:62 — Test fixture uses format_ids instead of formats

…#9025)

Ladon nit on #9037: the scarce-seed fixture named the field format_ids;
SeedPlacement calls it formats. The engine never reads it, and the
server tsconfig excludes tests, so nothing caught it.

Co-Authored-By: Claude Opus 5.5 <[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.

Re-approval: no reviewable delta since the prior approval; re-submitting after stale-review dismissal.

@bokelley
bokelley merged commit 454773a into main Nov 15, 2026
56 checks passed
@bokelley
bokelley deleted the inventory-sim/engine branch November 15, 2026 20:32
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