When implementing concurrent async workflows with callbacks (Stop/Submit/Cancel, permission settlement, card actions), make the operation safe against stale/foreign/parallel events by using atomic identity validation plus a synchronous “claim” before any awaited side effects.
Apply these rules:
Example (pattern for exact-run Stop):
async function cancelRunFromCard(expectedRunId: string, ownerKey: OwnerKey): Promise<boolean> {
// 1) parse/validate owner + callback identity (no awaits yet)
if (!isOwnerMatch(ownerKey)) return false;
// 2) synchronous claim of the current live record
const active = getCurrentActivePrompt(); // atomic read
if (!active || active.runId !== expectedRunId) return false;
const claim = tryClaim(active); // must be synchronous
if (!claim) return false;
// 3) only now do awaited side effects
const ok = await channelBase.cancelExactRun(active.sessionId, expectedRunId);
if (ok) {
blockNewStreamingChunks(active.runId);
commitStoppedProjection();
claim.releaseSuccess();
} else {
claim.release(); // keep card active for retry
}
return ok;
}
Adopt this as a standard for any callback-driven cancellation/submission path so parallel runs and near-simultaneous callbacks cannot corrupt state or show incorrect success to users.
Enter the URL of a public GitHub repository