Skip to content

feat(training-agent): run the inventory tenants on a season file (#9025) - #9142

Merged
bokelley merged 20 commits into
adcontextprotocol:mainfrom
yvassiliadis:yvassiliadis/feat-inventory-sim-season-files
Nov 26, 2026
Merged

bokelley merged 20 commits into
adcontextprotocol:mainfrom
yvassiliadis:yvassiliadis/feat-inventory-sim-season-files

Conversation

@yvassiliadis

@yvassiliadis yvassiliadis commented Nov 20, 2026 •

Copy link
Copy Markdown
Contributor

Refs #9025.

Summary

Lets anyone run the two simulated inventory tenants (/inventory-adserver, /inventory-content) on a season they supply as a file, rather than only the two seasons compiled into the code. This answers the "Do you need your own seeds?" question in #9025. Builders of a market simulator need seasons of their own, including held-out ones that must never be uploaded to a hosted service. With this change those seasons run on the builder's own machine.

  • Format. A season file is YAML 1.2 or JSON with the same fields as SeasonSeed (engine/types.ts). inventory-sim/season-file.ts refuses a season the tenants could not serve. Each problem is reported with its path (placements[0].floor_cpm: must be 0 or more). It checks:

    • files over 5 MB, and anything that isn't a regular file, are refused before they are read
    • the shape and value ranges, with unknown keys refused; at least one placement and one product
    • that dates are real calendar dates, and that every scheduled or postponed installment falls inside the season (which also bounds a collection's count)
    • the references the engine leaves unchecked: a product's properties and collections must be on its placements, and a traffic event's ids must exist
    • that placement formats are labels the training agent can serve (the host passes its list in, so the loader stays independent of the training agent)
    • the engine's own rules (shares sum to 1, and so on)
    • that every product covers some inventory

    All of these except the engine's rules list every problem they find at once. YAML errors, including the alias limit, come back in the same INVALID_SEED format with the file's path. A strict compile-time equality check fails npm run typecheck if the schema and SeasonSeed drift apart, including an optional field added to only one side.

  • Choosing a season. Two env vars, TRAINING_AGENT_INVENTORY_ADSERVER_SEASON_FILE and TRAINING_AGENT_INVENTORY_CONTENT_SEASON_FILE.

    • Unset or empty: the tenant serves its shipped season, the same object as today.
    • Set: the tenant serves the file and logs one line naming it.
    • Missing or invalid file: the server refuses to start. The error names the variable, the path and the problems.
  • The engine is unchanged. inventory-sim/engine/ gains no imports and no edits, so it can still move into @adcp/sdk. Library users can call readSeasonFile and pass the result to InventorySimulation.create.

  • Docs. A "Run on your own season" section at the end of the simulated inventory agents page covers:

    • the starter file
    • printing a shipped season as a starting point (seasonToYaml)
    • the env vars
    • a verified local run command, which needs PostgreSQL and placeholder WorkOS values, and sets FLY_PROCESS_GROUP=web so a local run starts no crawlers or scheduled jobs
    • changing seed_id whenever a season changes

Why the shipped seasons stay TypeScript constants

Moving them into files would add runtime-asset plumbing and make the server read files at boot, with no change in behaviour. A test proves the format instead. Each shipped season is written to YAML and to JSON and loaded back unchanged, and the same scripted run on the loaded season and on the constant gives an identical SimulationState and forecast.

Hosted tenants

Nothing changes. The hosted deploy sets neither variable, so both tenants serve the shipped seasons by identity, and the same calls at the same simulated time give the same results.

The variables apply to the whole server: every caller of a tenant gets the file's season. So this is for local and self-hosted runs, and the docs say so. A per-caller season on the hosted agent would be a separate design.

Testing

  • server/tests/unit/inventory-sim-season-file.test.ts covers:
    • YAML and JSON round-trips of both shipped seasons
    • identical simulation state from a file and from the constant
    • path-level errors: malformed and impossible dates, out-of-range values, missing placements or products, installments outside the season, unresolved references, unsupported formats, products that cover no inventory, and engine errors
    • oversized files, non-regular files and YAML alias bombs refused as INVALID_SEED
    • unquoted dates loading as strings, refused digit separators (2_200_000)
    • empty, multi-document, duplicate-key and syntax-error files
    • env var resolution: unset, empty, one tenant on a file, missing file, invalid file
  • server/tests/unit/training-agent-inventory-season-file-tenant.test.ts boots the router with the ad server pointed at the starter fixture, provisions the account with sync_accounts, and checks over MCP, against the canonical schemas, that get_products serves exactly the fixture's products. It then books the guaranteed product, advances the sandbox clock and reads its delivery.
  • training-agent-inventory-tenants.test.ts, training-agent-inventory-market-concurrency.test.ts, inventory-sim-engine.test.ts and tests/run-storyboards-sharding.test.cjs pass unchanged on current main. npm run typecheck passes, and the pre-commit suite passes.
  • The documented run command was run locally (compose Postgres, the starter fixture), and get_products on the local ad server tenant returned the fixture's products.
  • No changeset: this touches the training agent and docs only (check-changeset-protocol-scope passes).

Relation to recent inventory changes

Rebased on current main, including #9116 (creative library) and #9147–#9150. Seeded products (seed_product) read the loaded season, so they work with a season file. #9116 makes the platform build each placement's format options when the tenant is built. The format check here makes a season with an unknown format fail at load with its path, instead of failing during that build.

Follow-up

  • A stored market is matched to its season by seed_id only, so editing a season file without changing seed_id would replay old bookings against new placements. The docs tell people to change seed_id; storing a content hash with the market would make it safe. Not in this PR.
  • A second PR, stacked on this one, will replace the engine's forecast error model with one whose error depends on lead time. It will extend the season format with that model's parameters.

🤖 Generated with Claude Code

@mintlify

mintlify Bot commented Nov 20, 2026 •

Copy link
Copy Markdown

Preview approval required

This pull request comes from a fork, so Mintlify won't build a preview until someone with write access approves commit f3a2b11.

Approve preview

@aao-ipr-bot

aao-ipr-bot Bot commented Nov 20, 2026

Copy link
Copy Markdown

IPR Policy Agreement Required

@yvassiliadis — thanks for the contribution. Before this PR can be merged, the AgenticAdvertising.Org IPR Policy requires your agreement.

To agree, post a new comment on this PR with the exact phrase:

I have read the IPR Policy

Your signature is recorded once and covers all contributions to AAO repositories. See signatures/README.md for what gets recorded and why.

aao-secretariat[bot]
aao-secretariat Bot previously approved these changes Nov 20, 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 — clean, well-tested training-agent feature.

This PR adds the ability to load a SeasonSeed from a YAML/JSON file to run the two inventory tenants on a supplied season. No protocol surface is touched (no static/schemas/source/**, no docs/reference/**), so no changeset is required and check-changeset-protocol-scope passes correctly.

Reviewed highlights:

  • The three-pass loader in season-file.ts is sound: zod strictObject shape pass, referenceProblems for engine-unchecked cases, then trafficFor/coverage. Layering is correct — unknown ids and share-sum/days-range rules are backstopped by the engine's SeasonTraffic constructor; the collection && guard at :346 is safe.
  • Compile-time Equals check enforces schema↔SeasonSeed parity.
  • Season resolution is a synchronous boot-time readFileSync that fails closed (server refuses to start, error names env var + path) — correct for a startup path, no timeout needed. Hosted deploy sets neither env var, so output is unchanged.

No Critical/High/Medium findings. Not path-gated (gated_paths: false), not high-risk, no no-auto-approve team match, first review. Falls through to row 9.

@yvassiliadis

Copy link
Copy Markdown
Contributor Author

I have read the IPR Policy

aao-ipr-bot Bot pushed a commit that referenced this pull request Nov 20, 2026
@aao-ipr-bot

aao-ipr-bot Bot commented Nov 20, 2026

Copy link
Copy Markdown

IPR Policy — signed

Thanks, @yvassiliadis. Your agreement to the IPR Policy is recorded at signatures/ipr-signatures.json and applies to all AAO repositories.

@bokelley bokelley 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.

Thanks Yannis, this is well built. Happy to approve once it's rebased. It's 28 commits behind main and has one conflict, so it needs a fresh approval after the rebase anyway.

Blocking after the rebase

  • The new tenant test will fail on current main. Since #9149, get_products returns ACCOUNT_NOT_FOUND for an account that was never synced. The two get_products cases in training-agent-inventory-season-file-tenant.test.ts (around lines 85-99) call it with an unprovisioned account. Add the sync_accounts step that main's training-agent-inventory-tenants.test.ts uses (provision()).
  • The only textual conflict is the import line in tenants/inventory.ts. Keep main's resetMarket, withCallerMarkets alongside your additions. Nothing in #9147's per-caller serialization, #9148 or #9150 is affected. Seeded products use the loaded season's placements, so season files work with seed_product.

Small hardening (please include)

  • Require at least one placement and one product. With placements: [], main's seeded-products.ts reads seed.placements[0].channel and seed_product throws a TypeError.
  • Cap collections[].count, or reject installments scheduled after the season ends. Today a huge count loads.
  • Cap the file size.
  • Wrap toJS() so a YAML alias-limit error comes back as INVALID_SEED rather than a plain error. It already fails closed.

Worth noting in the PR or docs

  • The season comes from an environment variable and applies to the whole server, so this is for local and self-hosted runs. Setting it on the hosted test agent would change every caller's market. A per-caller season on the hosted agent is a separate design change, if we want one later.
  • Follow-up, not for this PR: market-store.ts checks only seed_id when deciding whether a stored market is current, so editing a season file without changing seed_id would replay old bookings against new placements. Storing a content hash with the market would make that safe.

The rest looks right: strict zod validation, safe YAML handling, the compile-time check that the schema matches SeasonSeed, and real round-trip and run-equality tests.

yvassiliadis and others added 18 commits November 23, 2026 10:16
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
… var

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
…'t serve

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…oducts

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
…lments, size, aliases

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
aao-secretariat[bot]
aao-secretariat Bot previously approved these changes Nov 23, 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.

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

@yvassiliadis

Copy link
Copy Markdown
Contributor Author

Thanks Brian. Rebased on main; the only conflict was the import line in tenants/inventory.ts, resolved by keeping resetMarket, withCallerMarkets. Changes:

  • The season-file tenant test provisions the account with sync_accounts before get_products, as training-agent-inventory-tenants.test.ts does.
  • A season needs at least one placement and one product.
  • Installments scheduled after the season ends, and postponements to a date after it, are refused, which bounds collections[].count.
  • Files over 5 MB, or that aren't regular files, are refused before they are read.
  • YAML errors from toJS(), including the alias limit, come back as INVALID_SEED with the file path.
  • Docs and PR description say the season env vars apply to the whole server, so this is for local and self-hosted runs; the hosted agent keeps its shipped seasons.
  • The content-hash check for stored markets is listed as a follow-up in the PR description, not in this PR.

…ing their date

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
aao-secretariat[bot]
aao-secretariat Bot previously approved these changes Nov 24, 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.

The only delta since the prior approve is dateIfRepresentable (season-file.ts:313-318), which guards calendar.dateOf() against day values beyond JavaScript's ±8.64e15 Date range that previously threw "Invalid time value". The guard mirrors dateOf's date math exactly, and lastDate uses the daysUsable-bounded seed.days - 1 so it stays representable. New tests cover cadence_days of 1e9 and MAX_SAFE_INTEGER. Training-agent/test-only, no protocol surface (static/schemas/source/**, docs/reference/**, dist artifacts) touched, so no changeset is required. Reviewer reported no findings.

Gate checks: no no-auto-approve team match; gated_paths is false; high_risk is false. No critical/high/medium findings. None of decision-table rows 1–8 fire, so this falls through to row 9 → approve.

CodeQL (js/file-system-race) flagged readSeasonFile: it stat-ed the path,
then read it by path, so the file could change between the size check and
the read. Open it once, fstat and read through the same descriptor, and
re-check the size of what was actually read.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

@bokelley bokelley 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.

Approved at Brian's direction: his 21 Nov findings are all addressed (provisioning, imports, placement/product minimums, installment and size caps, INVALID_SEED wrapping). Added one commit fixing the CodeQL file-system race in readSeasonFile (check and read through one descriptor).

@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 — single-file hardening of readSeasonFile with no blocking or medium findings.

What I checked

  • The delta hardens season-file.ts (L327-343): replaces statSync+readFileSync with a single descriptor flow (openSync→fstatSync→readFileSync(fd) under try/finally closeSync), closing the TOCTOU window between stat and read, plus a post-read Buffer.byteLength size recheck.
  • No descriptor leak: invalid() throws (never), and the finally closes the fd on the throw path. Node does not auto-close an fd passed to readFileSync, and the code handles that correctly.
  • Not protocol surface: changes are confined to server/** training-agent code, a docs page under docs/building/** (non-normative), and tests/fixtures. No static/schemas/source/**, no dist/** artifacts, no migrations — schema↔docs coherence, changeset-scope, oneOf-discriminator, and artifact-immutability gates do not apply.
  • No high-risk flag, no gated paths, no author-team gate, review_decision not required here.

Findings: none (reviewer reports a clean pass; prior approve had no blockers, none re-surfaced).

Rows 1–8 do not fire → row 9 approve.

@bokelley
bokelley merged commit 4c8f5a7 into adcontextprotocol:main Nov 26, 2026
66 checks passed
bokelley added a commit that referenced this pull request Nov 27, 2026
main added a third root install with install scripts enabled in
training-agent-storyboards.yml (the inventory-adserver / inventory-content
job, from #9142) after this PR was written. Route it through
.github/scripts/npm-ci.mjs like the other root installs, and update the
helper's coverage note to 13 root installs.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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.

3 participants