Repository navigation
fix(training-agent): keep inventory cancels from being undone by concurrent requests - #9147
Conversation
…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]>
There was a problem hiding this comment.
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 isdocs/building/**(non-normative), so no schema↔docs coherence concern. - Root cause and two-part fix verified by the reviewer:
state.ts:275mergeMapEntriesdoes 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:79withCallerMarketsserializes 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.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
There was a problem hiding this comment.
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 despitereview_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, notescalate— 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.
…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).
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_dailybuy. A later booking of the same product still gotPRODUCT_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:flushDirtySessionsmerges 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 asget_products,get_media_buysandget_media_buy_deliveryalso 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_buyon/inventory-content:canceledpending_creativesPRODUCT_UNAVAILABLEcanceledcanceledWithout a concurrent request, cancel → rebook works on both tenants: the engine already skips canceled bookings when checking the sold share of voice.
Fix
state.ts). On flush,principalConfigurationsmerges 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.market-store.ts).withCallerMarketsruns 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_clockandreset_state.CONFLICT.PRODUCT_UNAVAILABLEuntil the first is cancelled. A token's buys under every account share one market.Tests
training-agent-inventory-market-concurrency.test.tsholds two requests open across each other's reads and writes:state.tschange.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/25npm run typecheckverify:local: affected root tests 183/183; 55 training-agent server files passed before I stopped the sweep. Touchingstate.tsmakes--changedselect the whole server suite. The only failures were 2 load timeouts (30 s+) inaddie/matched-v4-evaluation, unrelated to sessions; the integration files skip for want of Postgres. CI runs the full suites.🤖 Generated with Claude Code