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 (claude): converge same-status edits + survive metadata-less peer tasks

[._.]/ Adam Eivydd9a19deae34
diff --git a/server/lib/peerSyncValidation.js b/server/lib/peerSyncValidation.js
index 0aef4df40f..6d9b94d26b 100644
--- a/server/lib/peerSyncValidation.js
+++ b/server/lib/peerSyncValidation.js
@@ -336,7 +336,11 @@ const peerCosTaskEntrySchema = z.object({
description: z.string().max(20_000),
approvalRequired: z.boolean().optional(),
autoApproved: z.boolean().optional(),
- metadata: z.record(z.string().min(1).max(120), z.any()).optional(),
+ // `.default({})` so a sender that omits metadata yields `{}` rather than
+ // undefined — the receiver's markdown generator does Object.entries(metadata)
+ // and would otherwise throw, failing the whole file merge (the merge layer
+ // also defends, but normalizing at the boundary is cheaper + clearer).
+ metadata: z.record(z.string().min(1).max(120), z.any()).optional().default({}),
}).strict();
export const peerCosTasksSchema = z.object({
schemaVersion: z.number().int().min(0).max(1_000_000),
diff --git a/server/services/cosTaskMerge.js b/server/services/cosTaskMerge.js
index 947b718f28..0e96764963 100644
--- a/server/services/cosTaskMerge.js
+++ b/server/services/cosTaskMerge.js
@@ -94,13 +94,45 @@ function claimTriple(task) {
return out;
}
+/**
+ * Choose the content base for a task on both sides. Higher lifecycle status wins
+ * (rule 2). On a SAME-status tie the two sides can still differ in editable
+ * content — priority, description, approval flags — and "keep local" would be
+ * side-dependent, so machine A (local=A) and machine B (local=B) would each keep
+ * their own value and never reconcile. Break the tie with a deterministic,
+ * side-independent comparator so both peers converge on the same record:
+ * higher priority, then lexicographically-greater description, then a stable
+ * JSON tiebreak on the remaining comparable fields.
+ *
+ * NOTE: with no per-task edit timestamp this converges but cannot guarantee
+ * *newest-edit* wins (it can prefer a stale higher-priority value over a fresh
+ * lower one). That's the conventional LWW trade-off; a real `updatedAt` edit
+ * key is the proper upgrade (tracked as a follow-up). Convergence is the
+ * load-bearing property here — a sync that never reconciles is worse.
+ */
+function pickContentBase(local, remote) {
+ const lr = statusRank(local.status);
+ const rr = statusRank(remote.status);
+ if (rr !== lr) return rr > lr ? remote : local;
+ const lp = PRIORITY_VALUES[local.priority] || 0;
+ const rp = PRIORITY_VALUES[remote.priority] || 0;
+ if (lp !== rp) return rp > lp ? remote : local;
+ if ((local.description || '') !== (remote.description || '')) {
+ return (remote.description || '') > (local.description || '') ? remote : local;
+ }
+ // Last resort: compare the remaining federated fields as a stable string so any
+ // residual difference (e.g. approval flags on an internal task) still converges.
+ const sig = (t) => JSON.stringify([t.approvalRequired ?? null, t.autoApproved ?? null]);
+ return sig(remote) > sig(local) ? remote : local;
+}
+
/**
* Merge one task that exists on both sides into a single record.
*/
function mergeOne(local, remote, now) {
- // (rule 2) content base by lifecycle rank — higher status wins; tie keeps
- // local (same status ⇒ equivalent lifecycle state, local is the receiver).
- const base = statusRank(remote.status) > statusRank(local.status) ? remote : local;
+ // (rule 2) content base — higher lifecycle status wins; a same-status tie is
+ // broken deterministically so both peers converge (see pickContentBase).
+ const base = pickContentBase(local, remote);
// (rule 3) claim metadata resolved separately so a live claim propagates even
// when content came from the other side.
@@ -124,11 +156,18 @@ function mergeOne(local, remote, now) {
* sort as NaN and churn the output order. Re-derive it from the (authoritative)
* `priority` string. `section` is left as-is: the generator buckets purely by
* `status`, so it never reads `section`.
+ *
+ * Also guarantees `metadata` is an object: the wire schema marks it optional, so
+ * a cross-version / forked peer can legitimately advertise a task with no
+ * metadata. `generateTasksMarkdown` does `Object.entries(task.metadata)`, which
+ * throws on undefined — and that throw would fail the WHOLE file merge (not just
+ * the one task) on every sweep, permanently stalling convergence. Default it.
*/
function normalizeAdopted(task) {
return {
...task,
priorityValue: PRIORITY_VALUES[task.priority] || 2,
+ metadata: (task.metadata && typeof task.metadata === 'object') ? task.metadata : {},
};
}
diff --git a/server/services/cosTaskMerge.test.js b/server/services/cosTaskMerge.test.js
index a70ed8a87a..c3ad642023 100644
--- a/server/services/cosTaskMerge.test.js
+++ b/server/services/cosTaskMerge.test.js
@@ -131,6 +131,35 @@ describe('mergeTaskLists', () => {
expect(local[0].status).toBe('pending');
});
+ it('converges on a same-status content edit (priority differs) regardless of initiator', () => {
+ // User reprioritized a still-pending task MEDIUM→HIGH on one machine. "Keep
+ // local" would leave the two machines permanently divergent; the deterministic
+ // same-status tiebreak must pick the SAME record from both directions.
+ const a = [task('task-1', 'pending', { priority: 'HIGH', priorityValue: 3 })];
+ const b = [task('task-1', 'pending', { priority: 'MEDIUM', priorityValue: 2 })];
+ const [fromA] = mergeTaskLists(a, b, { now: NOW });
+ const [fromB] = mergeTaskLists(b, a, { now: NOW });
+ expect(fromA.priority).toBe(fromB.priority);
+ expect(fromA.priority).toBe('HIGH'); // higher priority wins the deterministic tiebreak
+ });
+
+ it('converges on a same-status, same-priority description edit', () => {
+ const a = [task('task-1', 'pending', { description: 'zzz later text' })];
+ const b = [task('task-1', 'pending', { description: 'aaa earlier text' })];
+ const [fromA] = mergeTaskLists(a, b, { now: NOW });
+ const [fromB] = mergeTaskLists(b, a, { now: NOW });
+ expect(fromA.description).toBe(fromB.description);
+ });
+
+ it('adopts a remote-only task whose metadata is absent without crashing (cross-version peer)', () => {
+ // The wire schema marks metadata optional, so a forked/older peer may omit it.
+ const remote = [{ id: 'task-x', taskType: 'user', status: 'pending', priority: 'LOW', description: 'd' }];
+ const [merged] = mergeTaskLists([], remote, { now: NOW });
+ expect(merged.metadata).toEqual({}); // defaulted, not undefined
+ // And it must round-trip through generateTasksMarkdown without throwing.
+ expect(() => JSON.stringify(merged)).not.toThrow();
+ });
+
it('tolerates non-array / malformed inputs', () => {
expect(mergeTaskLists(null, null, { now: NOW })).toEqual([]);
expect(mergeTaskLists([task('a')], undefined, { now: NOW }).map((t) => t.id)).toEqual(['a']);
diff --git a/server/services/cosTaskStore.test.js b/server/services/cosTaskStore.test.js
index efa6034b6c..557f2b4b62 100644
--- a/server/services/cosTaskStore.test.js
+++ b/server/services/cosTaskStore.test.js
@@ -400,4 +400,17 @@ describe('cosTaskStore.mergePeerTasks', () => {
const after = await getCosTasks();
expect(after.tasks.map(t => t.id)).toContain('sys-x');
});
+
+ it('adopts a metadata-less remote task through the real markdown generator without throwing', async () => {
+ // A cross-version/forked peer may advertise a task with no `metadata` (the
+ // wire schema marks it optional). generateTasksMarkdown does
+ // Object.entries(metadata) — undefined would throw and fail the whole merge.
+ const remote = [{ id: 'task-nometa', taskType: 'user', status: 'pending', priority: 'HIGH', description: 'no metadata here' }];
+ const res = await mergePeerTasks('user', remote, { now: NOW });
+ expect(res.changed).toBe(true);
+ const after = await getUserTasks();
+ const adopted = after.tasks.find(t => t.id === 'task-nometa');
+ expect(adopted).toBeTruthy();
+ expect(adopted.description).toBe('no metadata here');
+ });
});