Skip to content

fix(training-agent): keep inventory cancels from being undone by concurrent requests - #9147

Merged
bokelley merged 2 commits into
mainfrom
fix/inventory-market-mutex
Nov 21, 2026
Merged

bokelley merged 2 commits into
mainfrom
fix/inventory-market-mutex

Conversation

@bokelley

Copy link
Copy Markdown
Contributor

Summary

On the inventory tenants, a canceled guaranteed buy could come back to life and keep its flight sold. The Apostra simulator saw this on staging: it booked and then canceled the homepage_takeover_daily buy. A later booking of the same product still got PRODUCT_UNAVAILABLE: already sold for part of this flight.

Cause. Each request reads the caller's session and writes it back when it ends. Several documents live in one session field, principalConfigurations:

  • the caller's market on both inventory tenants;
  • their sandbox document;
  • the sandbox clock.

flushDirtySessions merges changes per top-level field. So any request that touched one document wrote back its stale copy of all the others. When a cancel overlapped another request from the same caller, the later write put the live booking back. Reads such as get_products, get_media_buys and get_media_buy_delivery also save the market.

Live reproduction on production, on my own fresh demo callers. In each of 5 trials I booked the takeover, then fired its cancel at the same time as a create_media_buy on /inventory-content:

Trials Cancel answered Buy afterwards Rebook of the same flight
2 canceled read back pending_creatives PRODUCT_UNAVAILABLE
3 canceled read back canceled succeeded

Without a concurrent request, cancel → rebook works on both tenants: the engine already skips canceled bookings when checking the sold share of voice.

Fix

  • Per-entry merge (state.ts). On flush, principalConfigurations merges entry by entry. Entries this request changed or removed take its value; every other entry takes the newest durable value. So writes to different documents both land. Other session fields keep the existing top-level merge.
  • Per-caller market lock (market-store.ts). withCallerMarkets runs one inventory market request per caller at a time and flushes before releasing. This covers two requests on the same market, which a per-entry merge alone cannot. It applies to every market handler, sandbox_clock and reset_state.
    • It follows the holds ledger's pattern: an in-process queue orders this machine's requests, and a claim in the shared idempotency store orders them across machines.
    • After 10 s waiting, a request gets a transient CONFLICT.
  • Docs. The simulated-inventory page now says that an exclusive booking sells every day or installment in its flight, whatever its budget. Another booking of those days or installments fails with PRODUCT_UNAVAILABLE until the first is cancelled. A token's buys under every account share one market.

Tests

training-agent-inventory-market-concurrency.test.ts holds two requests open across each other's reads and writes:

  • Entry merge: a cancel on one market and a buy on another, both read before either writes. Both writes land, and a removed entry stays removed. Fails without the state.ts change.
  • Lock: three concurrent read-modify-writes of one market entry end at 3. Fails (ends at 1) without the lock.

Over HTTP, in-process requests on the memory store never interleave between load and flush, so an HTTP-level race test passed with or without the fix. It is not included.

  • training-agent-inventory-tenants.test.ts: 25/25
  • new concurrency suite: 2/2, and each test fails without its fix
  • npm run typecheck
  • verify:local: affected root tests 183/183; 55 training-agent server files passed before I stopped the sweep. Touching state.ts makes --changed select the whole server suite. The only failures were 2 load timeouts (30 s+) in addie/matched-v4-evaluation, unrelated to sessions; the integration files skip for want of Postgres. CI runs the full suites.
  • After deploy: rerun the live race script on fresh demo callers and expect 5/5 trials to rebook.

🤖 Generated with Claude Code

…urrent requests

A request reads the caller's session and writes it back when it ends.
The inventory markets, the sandbox document and the clock all live in one
session field, principalConfigurations, which the flush merged as a whole.
So a concurrent request from the same caller (a buy on the other tenant,
a delivery read) could write back its stale copy and undo a cancel: the
canceled exclusive booking came back and kept its flight sold.

- Merge principalConfigurations entry by entry on flush, so writes to
  different documents both land.
- Run one inventory market request per caller at a time (in-process queue
  plus an idempotency-store claim across machines), and persist it before
  the next one reads.
- Say in the docs that an exclusive booking sells its days or
  installments until it is cancelled, whatever its budget.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
aao-secretariat[bot]
aao-secretariat Bot previously approved these changes Nov 21, 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 concurrency fix in the training-agent (app-only) subsystem.

What I checked:

  • No static/schemas/source/** touch, so no changeset required; docs edit is docs/building/** (non-normative), so no schema↔docs coherence concern.
  • Root cause and two-part fix verified by the reviewer: state.ts:275 mergeMapEntries does a sound three-way per-entry merge (changed/removed entries take the request's value, others take newest durable, added-elsewhere entries survive); market-store.ts:79 withCallerMarkets serializes per-caller market work via the holds-ledger pattern with correct lock/flush ordering and a 10s bound → transient CONFLICT.
  • Operational readiness confirmed: claim release in finally, no reentrancy among wrapped paths, read handlers correctly locked.
  • New concurrency test suite exercises both the entry-merge and the lock, and fails without each fix.

Gate checks: gated_paths=false; no no-auto-approve team match; high_risk=false. No blocking or medium findings. review_decision is REVIEW_REQUIRED but no hard gate applies (gated_paths false), so this falls through to row 9.

No blocking findings. No medium findings. No escalation triggers.

@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 — no blocking findings.

This is a subsequent review following a prior approve. The delta since that pass is a single line in docs/building/verification/simulated-inventory-agents.mdx:10, converting plain-text PRODUCT_UNAVAILABLE into a deep link. The reviewer verified the anchor target #error-code-product-unavailable exists at docs/snippets/compliance-error-codes.mdx:991 and resolves correctly via <ComplianceErrorCodes />. No findings surfaced.

Checked against the decision table:

  • No critical/high/medium findings (rows 1, 8 do not fire).
  • gated_paths: false — row 2 does not apply despite review_decision: REVIEW_REQUIRED.
  • high_risk: false — rows 3, 5 do not apply.
  • No no-auto-approve team match — row 7 does not apply.
  • Prior decision was approve, not escalate — row 6 does not apply.
  • No schema/docs coherence, changeset, oneOf, immutable-artifact, or governance concerns triggered by this doc-only link change.

Falls through to row 9: approve.

@bokelley
bokelley merged commit 02d7376 into main Nov 21, 2026
61 checks passed
@bokelley
bokelley deleted the fix/inventory-market-mutex branch November 21, 2026 03:10
KonstantinMirin added a commit to KonstantinMirin/adcp that referenced this pull request Nov 21, 2026
…9147, adcontextprotocol#9148)

No conflicts. Two files changed on both sides, and git's merge keeps both:
- server/src/training-agent/state.ts: this branch's narrow
  `@adcp/sdk/server` import, plus main's session merge for concurrent
  requests.
- server/tests/unit/training-agent-inventory-tenants.test.ts: this branch's
  top-level router import, loopback listener and per-test clock reset, plus
  main's media buy context test, in a different describe block.

Resolution ledger:
- mechanical: none
- semantic: none; the two auto-merged files above were read, and they keep
  both sides' changes

Check: typecheck passes, and the full server unit suite passes
(867 files, 14,704 tests, 31 skipped; 69 s at load 60-100).
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