Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion demo_app/src/checkout/submit-order.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,17 @@ export const runCheckoutRaceScenario = async (mode: "concurrent" | "serial"): Pr
return [await processCheckoutWorker(requests[0])];
}

return Promise.all(requests.map((request) => processCheckoutWorker(request)));
const settled = await Promise.allSettled(requests.map((request) => processCheckoutWorker(request)));

const hardFailure = settled.find(
(result) =>
result.status === "rejected" &&
!(result.reason instanceof Error && result.reason.message.startsWith("OutOfStock:"))
);
Comment on lines +19 to +23
Copy link

Copilot AI Apr 19, 2026

Choose a reason for hiding this comment

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

The OutOfStock detection here relies on parsing Error.message with startsWith("OutOfStock:"). This is brittle (message wording changes, wrapping, localization) and couples submit-order to reserve-stock’s string format. Prefer a typed error (e.g., class OutOfStockError extends Error { sku }) or an error code property so callers can branch reliably without string matching.

Copilot uses AI. Check for mistakes.

if (hardFailure && hardFailure.status === "rejected") {
throw hardFailure.reason;
}

return settled.flatMap((result) => (result.status === "fulfilled" ? [result.value] : []));
Comment on lines +17 to +29
Copy link

Copilot AI Apr 19, 2026

Choose a reason for hiding this comment

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

runCheckoutRaceScenario now returns only fulfilled results in concurrent mode, which can make the returned array shorter than the requests array. Since this function is exported and used by the server/script, consider returning a structure that preserves per-request outcomes (e.g., { successes, failures } or an array of discriminated union results) so callers can distinguish “one succeeded, one OutOfStock” from other scenarios instead of silently dropping failures.

Copilot uses AI. Check for mistakes.
};
9 changes: 7 additions & 2 deletions demo_app/src/inventory/reserve-stock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ export const reserveStock = async (sku: string, quantity: number, requestId: str
await delay(25);

const record = getInventoryRecord(sku);

if (record.available < quantity) {
throw new Error(`OutOfStock: ${sku}`);
}

record.available -= quantity;
record.snapshotVersion += 1;

Expand All @@ -29,8 +34,8 @@ export const reserveStock = async (sku: string, quantity: number, requestId: str
}

return {
reservationToken: `res_${requestId}_${snapshot.snapshotVersion}`,
snapshotVersion: snapshot.snapshotVersion,
reservationToken: `res_${requestId}_${record.snapshotVersion}`,
snapshotVersion: record.snapshotVersion,
availableAfter: record.available
};
};