test(p02): guard stale Redis claims and verify CSV replay edge cases [skip ci]

This commit is contained in:
victorsteele428 committed 2026-11-22 16:45:45 +06:00
1 parent 76cd972bde
commit bf525f0d4e
3 files changed
+211 -8

No files matched your search

+27 -8
View File
@@ -551,7 +551,7 @@ function setupSocketHandlers(io, bufferQueue, worker, options = {}) {
// A previous Redis write failure can leave an uncommitted idempotency
// claim. Never report that claim as a successful durable duplicate.
const failedAt = idempotencyMap.get(`${clientId}:${eventId}`);
const failedAt = idempotencyMap.get(`\u0000failed-persist:${clientId}:${eventId}`);
const check = await validateSingleEventAsync(
clientId,
eventId,
@@ -560,8 +560,8 @@ function setupSocketHandlers(io, bufferQueue, worker, options = {}) {
);
if (!check.allowed) {
if (check.duplicate) {
if (typeof failedAt === "number" && failedAt < 0 &&
Date.now() + failedAt < IDEMPOTENCY_TTL_MS) {
if (typeof failedAt === "number" &&
Date.now() - failedAt < IDEMPOTENCY_TTL_MS) {
if (callback) callback({
success: false, eventId, code: "BUFFER_PERSIST_FAILED",
error: "BUFFER_PERSIST_FAILED: Event not durably queued",
@@ -597,7 +597,8 @@ function setupSocketHandlers(io, bufferQueue, worker, options = {}) {
// Invalidate the pre-persistence idempotency claim so SDK retry can
// resubmit after Redis recovers. Keep the volatile RAM event pending.
const claimKey = `${clientId}:${eventId}`;
idempotencyMap.set(claimKey, -Date.now());
idempotencyMap.delete(claimKey);
idempotencyMap.set(`\u0000failed-persist:${claimKey}`, Date.now());
const claimRedis = getRedisClient();
if (claimRedis && typeof claimRedis.del === "function") {
try {
@@ -621,6 +622,7 @@ function setupSocketHandlers(io, bufferQueue, worker, options = {}) {
return;
}
idempotencyMap.delete(`\u0000failed-persist:${clientId}:${eventId}`);
if (callback) callback({ success: true, eventId });
} catch (err) {
if (callback) callback({ success: false, error: err.message });
@@ -726,7 +728,7 @@ function setupSocketHandlers(io, bufferQueue, worker, options = {}) {
: new Date().toISOString();
const rawData = extractBatchItemData(safeItem);
const failedAt = idempotencyMap.get(`${clientId}:${eventId}`);
const failedAt = idempotencyMap.get(`\u0000failed-persist:${clientId}:${eventId}`);
const check = await validateSingleEventAsync(
clientId,
eventId,
@@ -735,8 +737,8 @@ function setupSocketHandlers(io, bufferQueue, worker, options = {}) {
);
if (!check.allowed) {
if (check.duplicate) {
if (typeof failedAt === "number" && failedAt < 0 &&
Date.now() + failedAt < IDEMPOTENCY_TTL_MS) {
if (typeof failedAt === "number" &&
Date.now() - failedAt < IDEMPOTENCY_TTL_MS) {
rejected += 1;
volatileDuplicate = true;
if (errors.length < 5) errors.push(
@@ -779,7 +781,8 @@ function setupSocketHandlers(io, bufferQueue, worker, options = {}) {
const claimRedis = getRedisClient();
for (const eventId of acceptedEventIds) {
const claimKey = `${clientId}:${eventId}`;
idempotencyMap.set(claimKey, -Date.now());
idempotencyMap.delete(claimKey);
idempotencyMap.set(`\u0000failed-persist:${claimKey}`, Date.now());
if (claimRedis && typeof claimRedis.del === "function") {
try {
await claimRedis.del(`${REDIS_IDEMPOTENCY_PREFIX}${claimKey}`);
@@ -806,6 +809,22 @@ function setupSocketHandlers(io, bufferQueue, worker, options = {}) {
}
}
// Only durable new writes clear pending failure markers.
for (const persistedId of acceptedEventIds) {
idempotencyMap.delete(`\u0000failed-persist:${clientId}:${persistedId}`);
}
// A mixed batch must never receive success while one event still has
// an uncommitted Redis claim, even if other items were persisted.
if (volatileDuplicate && processed > 0) {
if (callback) callback({
success: false, processed, rejected, duplicates,
code: "BUFFER_PERSIST_FAILED",
error: "BUFFER_PERSIST_FAILED: Batch contains non-durable event",
errors,
});
return;
}
if (processed === 0 && duplicates === 0 && rejected > 0) {
const code = volatileDuplicate
? "BUFFER_PERSIST_FAILED"