T14.1 (D26): a sign-in problem never destroys a queued change

A 401 holds the queue: nothing is marked dead, no try is counted, and only
wp-auth-ready releases it. A 403 marks only that change dead, with a reason.
qWrite reports failure; a change the device cannot store stays in memory and the
badge says so. Each change has an owner and is sent only under that person.

tests/outbox_check.py (26 checks) fails against the old 401 rule.
This commit is contained in:
2026-10-17 14:00:52 -07:00
parent 512f0b6b1d
commit 73f0b5e69d
2 changed files with 341 additions and 33 deletions

View File

@@ -245,25 +245,63 @@
// local cache still updates immediately, so rendering never waits on the network.
var OUTBOX_KEY = 'wp_sync_outbox_v1';
var _flushTimer = null, _backoff = 0, _flushing = false;
// D26. `_held` is set when the server answers 401: the session ended, so the
// queue waits for a good sign-in. It is cleared ONLY by `wp-auth-ready` (a page
// load with a good session check), never by the online event, the timer or tab
// focus, because none of those prove the session is back.
var _held = false;
// D26. Changes the device could not store (localStorage full) stay here, in page
// memory, and are still sent. They are lost if the page closes, so the badge says
// they are not saved on this device. They are never dropped without a word.
var _mem = [];
var _deviceFull = false;
function qRead() { try { return JSON.parse(localStorage.getItem(OUTBOX_KEY) || '[]') || []; } catch (e) { return []; } }
function qWrite(list) { try { localStorage.setItem(OUTBOX_KEY, JSON.stringify(list)); } catch (e) {} }
// Returns true when the list is stored. A storage error used to be swallowed here,
// so a full device lost the change and nobody knew (D26, fault 2).
function qWrite(list) {
try { localStorage.setItem(OUTBOX_KEY, JSON.stringify(list)); return true; }
catch (e) { return false; }
}
// Stored ops plus any held only in memory, stored ones first (send order).
function qAll() {
var q = qRead();
if (!_mem.length) return q;
var ids = {};
q.forEach(function (o) { ids[o.opId] = true; });
return q.concat(_mem.filter(function (o) { return !ids[o.opId]; }));
}
// Remove one op everywhere it can be held.
function qDrop(opId) {
_mem = _mem.filter(function (o) { return o.opId !== opId; });
qWrite(qRead().filter(function (o) { return o.opId !== opId; }));
}
function sameUser(a, b) { return String(a || '').toLowerCase() === String(b || '').toLowerCase(); }
// Append an op, coalescing by (kind,key) so only the latest write per entity is
// queued. A delete supersedes any pending upsert for the same id.
// queued. A delete supersedes any pending upsert for the same id. Returns true
// when the change is stored on this device, false when it is only in memory.
function enqueue(op) {
var q = qRead();
if (op.kind === 'wp-del') {
q = q.filter(function (o) { return !(o.key === op.key && (o.kind === 'wp' || o.kind === 'wp-del')); });
} else {
q = q.filter(function (o) { return !(o.kind === op.kind && o.key === op.key); });
}
var supersedes = (op.kind === 'wp-del')
? function (o) { return o.key === op.key && (o.kind === 'wp' || o.kind === 'wp-del'); }
: function (o) { return o.kind === op.kind && o.key === op.key; };
var q = qRead().filter(function (o) { return !supersedes(o); });
_mem = _mem.filter(function (o) { return !supersedes(o); });
op.opId = op.kind + ':' + op.key + ':' + Date.now().toString(36) + Math.random().toString(36).slice(2, 6);
op.tries = 0;
op.owner = currentUser(); // D26: sent only when this person is signed in
q.push(op);
qWrite(q);
var stored = qWrite(q);
if (stored) {
// Room again: bring anything held in memory onto the device.
if (_mem.length && qWrite(q.concat(_mem))) _mem = [];
_deviceFull = _mem.length > 0;
} else {
_mem.push(op); _deviceFull = true;
}
notifySync();
scheduleFlush(0);
return stored;
}
function opRequest(op) {
@@ -275,41 +313,63 @@
});
}
// Apply `fn` to the op with this id, wherever it is held (device or memory).
function patchOp(opId, fn) {
var q = qRead(), hit = false;
for (var i = 0; i < q.length; i++) { if (q[i].opId === opId) { fn(q[i]); hit = true; break; } }
if (hit) qWrite(q);
for (var j = 0; j < _mem.length; j++) { if (_mem[j].opId === opId) { fn(_mem[j]); break; } }
}
function bumpTries(opId, err) {
var q = qRead();
for (var i = 0; i < q.length; i++) { if (q[i].opId === opId) { q[i].tries = (q[i].tries || 0) + 1; q[i].lastErr = err; break; } }
qWrite(q);
patchOp(opId, function (o) { o.tries = (o.tries || 0) + 1; o.lastErr = err; });
}
// Permanently-failed op (a 4xx client error) — keep it for visibility but stop
// retrying, so a rejected write can't loop forever. `err` is the server's own
// explanation when it sent one; it is what the sync badge shows the user.
function markDead(opId, err) {
var q = qRead();
for (var i = 0; i < q.length; i++) { if (q[i].opId === opId) { q[i].dead = true; q[i].lastErr = err; break; } }
qWrite(q);
patchOp(opId, function (o) { o.dead = true; o.lastErr = err; });
}
// Attempt every live op; successes are removed, 4xx client errors are marked
// dead (won't succeed on retry), transient failures (429/5xx/network) stay queued.
// True when this op is waiting for another person to sign in (D26). An op with no
// owner (queued before the sign-in check finished) can be sent by anyone, which is
// what every op did before owners existed.
function foreignOp(o) {
var me = currentUser();
return !!o.owner && !sameUser(o.owner, me);
}
function liveOps() { return qAll().filter(function (o) { return !o.dead; }); }
function sendableOps() { return liveOps().filter(function (o) { return !foreignOp(o); }); }
// Attempt every live op that belongs to the signed-in person; successes are
// removed. A 401 HOLDS the queue (D26): the session ended, which says nothing
// about the changes, so nothing is marked dead and no try is counted. A 403 or
// other 4xx is marked dead (it will not succeed on retry); transient failures
// (429/5xx/network) stay queued.
function flush() {
if (_flushing) return Promise.resolve();
var q = qRead().filter(function (o) { return !o.dead; });
if (_held) { notifySync(); return Promise.resolve(); }
var q = sendableOps();
if (!q.length) { notifySync(); return Promise.resolve(); }
_flushing = true; notifySync();
var chain = Promise.resolve(), anyFail = false;
var chain = Promise.resolve(), anyFail = false, halted = false;
q.forEach(function (op) {
chain = chain.then(function () {
if (halted) return; // a 401 stops the run; later ops keep their place
return opRequest(op).then(function (r) {
var status = r ? r.status : 0;
var done = r && (r.ok || (op.kind === 'wp-del' && status === 404)); // 404 on delete = already gone
if (done) { qWrite(qRead().filter(function (o) { return o.opId !== op.opId; })); }
if (done) { qDrop(op.opId); }
else if (status === 401) { halted = true; _held = true; }
else if (status >= 400 && status < 500 && status !== 429) {
// Refused once, refused forever — so the only useful thing left is the
// reason. A 409 here is the archived-project gate, whose detail tells
// the user the project is read-only and how to get it unarchived; a
// bare "HTTP 409" would leave them staring at a change that vanished.
return r.json().catch(function () { return null; }).then(function (j) {
var why = (j && typeof j.detail === 'string' && j.detail) || ('HTTP ' + status);
var detail = (j && typeof j.detail === 'string' && j.detail) || '';
var why = status === 403
? 'You no longer have access to this project.' + (detail ? ' (' + detail + ')' : '')
: (detail || ('HTTP ' + status));
markDead(op.opId, why);
});
}
@@ -320,7 +380,8 @@
return chain.then(function () {
_flushing = false;
notifySync();
if (qRead().filter(function (o) { return !o.dead; }).length) {
if (_held) return; // wait for wp-auth-ready; no timer retries a dead session
if (sendableOps().length) {
_backoff = anyFail ? Math.min((_backoff || 5000) * 2, 60000) : 0;
scheduleFlush(_backoff || 15000);
} else { _backoff = 0; }
@@ -336,14 +397,21 @@
// `failed` stays the total not-getting-through count (what listeners already
// read); `dead` splits out the ops the server has permanently refused, with the
// first reason it gave, because those two states need different words.
// D26 adds: `held` (the session ended; the queue waits for a sign-in), `others`
// and `otherOwner` (changes that belong to someone else, waiting for them),
// `deviceFull` and `unsaved` (changes that exist only in page memory).
function syncCounts() {
var q = qRead(), pending = 0, failed = 0, dead = 0, reason = '';
var q = qAll(), pending = 0, failed = 0, dead = 0, reason = '', others = 0, otherOwner = '';
for (var i = 0; i < q.length; i++) {
if (q[i].dead) { dead++; if (!reason && q[i].lastErr) reason = String(q[i].lastErr); }
else if (foreignOp(q[i])) { others++; if (!otherOwner) otherOwner = String(q[i].owner); }
else if ((q[i].tries || 0) >= 3) failed++;
else pending++;
}
return { pending: pending, failed: failed + dead, dead: dead, reason: reason, syncing: _flushing };
return {
pending: pending, failed: failed + dead, dead: dead, reason: reason, syncing: _flushing,
held: _held, others: others, otherOwner: otherOwner, deviceFull: _deviceFull, unsaved: _mem.length
};
}
ProjectData.syncStatus = syncCounts;
function notifySync() {
@@ -395,7 +463,15 @@
// Every string here now names the project explicitly. The draft indicator
// (WPAutosave.mountIndicator) is the one that speaks for the form, and the two
// can no longer be read as each other.
if (c.dead) {
// D26. These three are the states that used to be invisible or wrong. T14.3
// gives them their final wording and controls; until then they are plain text.
if (c.deviceFull) {
el.textContent = '✕ ' + c.unsaved + ' change' + (c.unsaved === 1 ? ' is' : 's are') + ' not saved on this device. Keep this page open until it is sent to the project.';
el.style.color = 'var(--wp-status-error-text)'; el.style.borderColor = 'var(--wp-status-error-border-a)'; el.style.background = 'var(--wp-status-error-bg)'; el.style.display = 'inline-flex';
} else if (c.held) {
el.textContent = '⚠ Sign in again to send your changes to the project. They are kept on this device.';
el.style.color = 'var(--wp-status-warning-text)'; el.style.borderColor = 'var(--cds-support-warning)'; el.style.background = 'var(--wp-status-warning-bg)'; el.style.display = 'inline-flex';
} else if (c.dead) {
el.innerHTML = '<span>✕ ' + c.dead + ' change' + (c.dead === 1 ? '' : 's') + ' rejected by the project — not saved</span>' +
(c.reason ? '<span style="font-weight:400">' + esc(c.reason) + '</span>' : '');
el.style.color = 'var(--wp-status-error-text)'; el.style.borderColor = 'var(--wp-status-error-border-a)'; el.style.background = 'var(--wp-status-error-bg)'; el.style.display = 'inline-flex';
@@ -405,6 +481,9 @@
} else if (c.pending) {
el.textContent = '↻ Sending ' + c.pending + ' change' + (c.pending === 1 ? '' : 's') + ' to the project…';
el.style.color = 'var(--cds-text-secondary)'; el.style.borderColor = 'var(--cds-border-subtle)'; el.style.background = 'var(--cds-layer)'; el.style.display = 'inline-flex';
} else if (c.others) {
el.textContent = '⚠ ' + c.others + ' change' + (c.others === 1 ? ' belongs' : 's belong') + ' to ' + c.otherOwner + '. ' + (c.others === 1 ? 'It goes' : 'They go') + ' to the project when ' + c.otherOwner + ' signs in.';
el.style.color = 'var(--wp-status-warning-text)'; el.style.borderColor = 'var(--cds-support-warning)'; el.style.background = 'var(--wp-status-warning-bg)'; el.style.display = 'inline-flex';
} else {
el.textContent = '✓ Everything sent to the project';
el.style.color = 'var(--wp-status-success-text)'; el.style.borderColor = 'var(--wp-status-success-border-a)'; el.style.background = 'var(--wp-status-success-bg)'; el.style.display = 'inline-flex';
@@ -418,7 +497,11 @@
window.addEventListener('online', function () { _backoff = 0; scheduleFlush(0); });
window.addEventListener('storage', function (e) { if (e.key === OUTBOX_KEY) { notifySync(); scheduleFlush(0); } });
document.addEventListener('visibilitychange', function () { if (!document.hidden) scheduleFlush(0); });
setInterval(function () { if (qRead().filter(function (o) { return !o.dead; }).length) scheduleFlush(0); }, 20000);
setInterval(function () { if (sendableOps().length) scheduleFlush(0); }, 20000);
// D26: the one thing that releases a held queue. auth-guard fires this after a
// good /api/auth/me (or the offline cache), and the next flush finds out for
// itself whether the session is really good: a 401 holds the queue again.
document.addEventListener('wp-auth-ready', function () { _held = false; _backoff = 0; notifySync(); scheduleFlush(0); });
} catch (e) {}
if (document.readyState === 'loading') { document.addEventListener('DOMContentLoaded', function () { notifySync(); scheduleFlush(0); }); }
else { setTimeout(function () { notifySync(); scheduleFlush(0); }, 0); }
@@ -428,7 +511,7 @@
// project so re-completing updates the same row.
ProjectData.pushSOP = function (projectId, sop, state) {
if (!projectId) return Promise.resolve(null);
enqueue({
return Promise.resolve(enqueue({
kind: 'sop', key: 'sop__' + projectId,
body: {
id: 'sop__' + projectId, project_id: projectId,
@@ -436,22 +519,21 @@
number: (sop && sop.project && sop.project.number) || '',
complete: true, created_by: currentUser(), data: { sop: sop, state: state }
}
});
return Promise.resolve(true);
}));
};
// Upsert a single Work Package. The local cache stays the source of truth for
// immediate rendering; the outbox guarantees the write reaches the server.
ProjectData.pushWP = function (p, projectId) {
if (!p || !p.id) return Promise.resolve(null);
enqueue({ kind: 'wp', key: p.id, body: pkgToServer(p, projectId) });
return Promise.resolve(true);
// D26: resolves false when the device could not store the change (it is still
// sent from memory, but it is lost if the page closes first).
return Promise.resolve(enqueue({ kind: 'wp', key: p.id, body: pkgToServer(p, projectId) }));
};
ProjectData.removeWP = function (id) {
if (!id) return Promise.resolve();
enqueue({ kind: 'wp-del', key: id });
return Promise.resolve(true);
return Promise.resolve(enqueue({ kind: 'wp-del', key: id }));
};
// Force a flush now and resolve when the queue drains (or a round-trip is done).