Skip to content

Self-hosted, Self-Improving, Federated Everything App — dev management, agent orchestration, persistent mind, digital twin, second brain, and creative pipeline from a single dashboard over Tailscale to port the user into the OS

address review (codex): don't bump updatedAt on lease-renewal heartbeats

[._.]/ Adam Eivy238751798b9b
diff --git a/server/services/cos.js b/server/services/cos.js
index 69f9693c2d..6dae0e13bf 100644
--- a/server/services/cos.js
+++ b/server/services/cos.js
@@ -536,7 +536,12 @@ async function resetOrphanedTasks() {
: (owner === null ? buildClaim(instanceId) : null);
if (renewal) {
const taskType = task.taskType || (isInternalTaskId(task.id) ? 'internal' : 'user');
- await updateTask(task.id, { metadata: { ...task.metadata, ...renewal } }, taskType).catch(() => {});
+ // Pass ONLY the renewal claim keys — updateTask merges them over the
+ // CURRENT persisted metadata, so spreading {...task.metadata} (this
+ // scan's possibly-stale copy) is redundant and would risk clobbering a
+ // concurrent content edit. It also keeps the heartbeat a claim-only
+ // patch so it never bumps the updatedAt LWW stamp (#1714).
+ await updateTask(task.id, { metadata: renewal }, taskType).catch(() => {});
}
continue;
}
diff --git a/server/services/cosTaskStore.js b/server/services/cosTaskStore.js
index af2cd4da9f..affec06656 100644
--- a/server/services/cosTaskStore.js
+++ b/server/services/cosTaskStore.js
@@ -41,18 +41,38 @@ const CLAIM_KEY_SET = new Set(CLAIM_METADATA_KEYS);
// content-edit detector and the normalizer below can't drift apart.
const LEGACY_DIRECT_FIELDS = ['context', 'model', 'provider', 'app'];
+// Equality for metadata values across a fresh markdown re-parse: primitives by
+// ===, arrays/objects (reviewers[], screenshots[], …) by JSON since the two
+// sides are independent parses with different references but equal content.
+const metaValueEqual = (a, b) => {
+ if (a === b) return true;
+ if (a && b && typeof a === 'object' && typeof b === 'object') {
+ return JSON.stringify(a) === JSON.stringify(b);
+ }
+ return false;
+};
+
/**
* Does an `updateTask` patch change a task's EDITABLE CONTENT (vs only its
* claim/lease metadata)? Used to decide whether to bump the `updatedAt` LWW stamp
* (#1714). A claim-only write — most importantly the periodic lease-renewal
- * heartbeat (`buildRenewal`, which passes only claim keys and no status) — must
- * NOT bump the stamp: a heartbeat is not a content edit, and letting it advance
- * `updatedAt` would make a lease-renewing peer spuriously win same-status content
- * ties over the other peer's genuine edit. Status transitions, priority/description
- * changes, approval flags, legacy `context`/`app`/… fields, and any non-claim
- * metadata key all count as content edits.
+ * heartbeat — must NOT bump the stamp: a heartbeat is not a content edit, and
+ * letting it advance `updatedAt` would make a lease-renewing peer spuriously win
+ * same-status content ties over the other peer's genuine edit.
+ *
+ * Crucially, the heartbeat does NOT pass a claim-only patch — `cos.js`
+ * `processOrphanedTasks` spreads the WHOLE existing metadata plus the fresh lease
+ * (`{ ...task.metadata, ...renewal }`). So presence of a non-claim key is not
+ * enough to call it an edit; we must compare VALUES against the task's current
+ * metadata and treat a key as content only when it actually changed. The edit
+ * stamp itself is never content. Status/priority/description/approval changes and
+ * legacy direct fields are always edits when present (callers only pass those
+ * with intent).
+ *
+ * @param {object} updates the updateTask patch
+ * @param {object} existingMetadata the task's current persisted metadata
*/
-function isContentEdit(updates) {
+function isContentEdit(updates, existingMetadata = {}) {
if (!updates || typeof updates !== 'object') return false;
if (updates.status !== undefined) return true;
if (updates.description !== undefined) return true;
@@ -60,8 +80,10 @@ function isContentEdit(updates) {
if (updates.approvalRequired !== undefined || updates.autoApproved !== undefined) return true;
if (LEGACY_DIRECT_FIELDS.some(f => updates[f] !== undefined)) return true;
if (updates.metadata && typeof updates.metadata === 'object') {
- for (const key of Object.keys(updates.metadata)) {
- if (!CLAIM_KEY_SET.has(key)) return true;
+ const existing = (existingMetadata && typeof existingMetadata === 'object') ? existingMetadata : {};
+ for (const [key, value] of Object.entries(updates.metadata)) {
+ if (CLAIM_KEY_SET.has(key) || key === 'updatedAt') continue; // claim keys + the stamp never count
+ if (!metaValueEqual(value, existing[key])) return true; // a non-claim key counts only if it CHANGED
}
}
return false;
@@ -328,10 +350,11 @@ export async function updateTask(taskId, updates, taskType = 'user', { now = Dat
}
// Bump the content-edit stamp (#1714) on a genuine content change so the peer's
- // claim-aware merge can resolve a same-status edit by newest-edit-wins. Skipped
- // for claim-only writes (lease-renewal heartbeats) — see isContentEdit. `now` is
+ // claim-aware merge can resolve a same-status edit by newest-edit-wins. Compared
+ // against the task's CURRENT metadata so a lease-renewal heartbeat that re-includes
+ // unchanged metadata doesn't read as an edit (see isContentEdit). `now` is
// injectable for deterministic test output.
- if (isContentEdit(updates)) {
+ if (isContentEdit(updates, tasks[taskIndex].metadata)) {
updatedMetadata.updatedAt = new Date(now).toISOString();
}
diff --git a/server/services/cosTaskStore.test.js b/server/services/cosTaskStore.test.js
index 1756af9b40..e34d1aebba 100644
--- a/server/services/cosTaskStore.test.js
+++ b/server/services/cosTaskStore.test.js
@@ -274,23 +274,41 @@ describe('cosTaskStore.updateTask', () => {
expect(updated.metadata.updatedAt).toBe(new Date(T1).toISOString());
});
- it('does NOT bump updatedAt on a claim-only lease renewal (heartbeat) (#1714)', async () => {
+ it('does NOT bump updatedAt on a lease-renewal heartbeat that re-includes existing metadata (#1714)', async () => {
const T0 = Date.parse('2026-06-25T00:00:00.000Z');
const T1 = Date.parse('2026-06-25T06:00:00.000Z');
const T2 = Date.parse('2026-06-25T12:00:00.000Z');
- await addTask({ description: 'beat', id: 'task-beat' }, 'user', { now: T0 });
+ // Task carries real non-claim content (context) so the heartbeat spread below
+ // actually re-includes a content key — the shape that broke the naive detector.
+ await addTask({ description: 'beat', id: 'task-beat', context: 'working on it' }, 'user', { now: T0 });
// Claiming the task is a content edit (status change) → stamp advances to T1.
await updateTask('task-beat', {
status: 'in_progress',
metadata: { claimedBy: 'a', claimedAt: '2026-01-01T00:00:00.000Z', leaseExpiresAt: '2026-01-01T00:30:00.000Z' }
}, 'user', { now: T1 });
- // A pure heartbeat (claim-only metadata, no status) must NOT advance the stamp,
- // or a lease-renewing peer would spuriously win same-status content ties.
+ // Real heartbeat shape (cos.js processOrphanedTasks once spread the whole
+ // metadata): { ...existing, ...freshLease }. The re-included unchanged keys
+ // (context, updatedAt) must NOT count as an edit — else every ~15min heartbeat
+ // bumps the stamp and a lease-renewing peer spuriously wins content ties.
+ const current = await getTaskById('task-beat');
const renewed = await updateTask('task-beat', {
- metadata: { claimedBy: 'a', claimedAt: '2026-01-01T00:00:00.000Z', leaseExpiresAt: '2026-01-01T01:00:00.000Z' }
+ metadata: { ...current.metadata, claimedBy: 'a', claimedAt: '2026-01-01T00:00:00.000Z', leaseExpiresAt: '2026-01-01T01:00:00.000Z' }
}, 'user', { now: T2 });
expect(renewed.metadata.leaseExpiresAt).toBe('2026-01-01T01:00:00.000Z');
- expect(renewed.metadata.updatedAt).toBe(new Date(T1).toISOString());
+ expect(renewed.metadata.updatedAt).toBe(new Date(T1).toISOString()); // NOT bumped to T2
+ });
+
+ it('DOES bump updatedAt when a spread metadata patch actually changes a non-claim value (#1714)', async () => {
+ const T0 = Date.parse('2026-06-25T00:00:00.000Z');
+ const T1 = Date.parse('2026-06-25T06:00:00.000Z');
+ await addTask({ description: 'meta edit', id: 'task-meta', context: 'old' }, 'user', { now: T0 });
+ const current = await getTaskById('task-meta');
+ // Same spread shape as the heartbeat, but context genuinely changes → real edit.
+ const updated = await updateTask('task-meta', {
+ metadata: { ...current.metadata, context: 'new' }
+ }, 'user', { now: T1 });
+ expect(updated.metadata.context).toBe('new');
+ expect(updated.metadata.updatedAt).toBe(new Date(T1).toISOString());
});
it('returns an error object when the file is missing', async () => {