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
JavaScript ai-agentsdashboarddeveloper-toolsdigital-twinquantified-selfsecond-brainself-hostedtailscale
fix thread list ordering and two wrong field reads found in self-review (#7664)
[._.]/ Adam Eivy93986cb2c1f2
diff --git a/server/routes/brainThreads.js b/server/routes/brainThreads.jsindex baf83dccc8..76e11a7ef6 100644--- a/server/routes/brainThreads.js+++ b/server/routes/brainThreads.js@@ -117,8 +117,13 @@ const dueSortKey = (thread) => {// and an unstable one drops or duplicates rows at the slice boundary.function compareThreads(a, b) {if (Boolean(b.pinned) !== Boolean(a.pinned)) return Boolean(b.pinned) - Boolean(a.pinned);- const due = dueSortKey(a) - dueSortKey(b);- if (due !== 0) return due;+ // Compared, not subtracted: two UNDATED threads are both Infinity, and+ // `Infinity - Infinity` is NaN — a comparator returning NaN skips the+ // remaining tiebreaks and leaves the order engine-defined, which the+ // pagination slice below cannot tolerate.+ const aDue = dueSortKey(a);+ const bDue = dueSortKey(b);+ if (aDue !== bDue) return aDue - bDue;const touched = Date.parse(b?.updatedAt ?? '') - Date.parse(a?.updatedAt ?? '');if (!Number.isNaN(touched) && touched !== 0) return touched;return String(a.id).localeCompare(String(b.id));diff --git a/server/routes/brainThreads.test.js b/server/routes/brainThreads.test.jsindex d1e92b29db..e77914f4c7 100644--- a/server/routes/brainThreads.test.js+++ b/server/routes/brainThreads.test.js@@ -234,6 +234,20 @@ describe('Brain Threads routes', () => {expect(res.body.threads.map((t) => t.id)).toEqual(['pinned', 'sooner', 'later', 'undated']);});+ it('keeps a total order when two threads share a due date — including none at all', async () => {+ // Regression: two UNDATED threads both key to Infinity, and subtracting+ // them yields NaN. A comparator that returns NaN abandons the remaining+ // tiebreaks, so the order goes engine-defined — and the pagination slice+ // below drops or duplicates rows at its boundary when that happens.+ brainStorage.getAll.mockResolvedValue([+ baseThread({ id: 'c', dueAt: null, updatedAt: '2026-09-01T00:00:00.000Z' }),+ baseThread({ id: 'a', dueAt: null, updatedAt: '2026-09-03T00:00:00.000Z' }),+ baseThread({ id: 'b', dueAt: null, updatedAt: '2026-09-02T00:00:00.000Z' }),+ ]);+ const res = await request(app).get('/api/brain/threads');+ // Falls through to most-recently-touched, which NaN would have skipped.+ expect(res.body.threads.map((t) => t.id)).toEqual(['a', 'b', 'c']);+ });it('returns the paginated envelope only when a pagination param is passed', async () => {brainStorage.getAll.mockResolvedValue([baseThread({ id: 'a' }), baseThread({ id: 'b' })]);const plain = await request(app).get('/api/brain/threads');diff --git a/server/services/threadRefs.js b/server/services/threadRefs.jsindex ce85d49453..58505022dc 100644--- a/server/services/threadRefs.js+++ b/server/services/threadRefs.js@@ -89,12 +89,14 @@ const SERVICE_REF_LOOKUPS = Object.freeze({const separator = id.indexOf(':');if (separator <= 0) return null;const message = await getMessage(id.slice(0, separator), id.slice(separator + 1));- return message ? { title: message.subject || message.snippet || message.id } : null;+ return message ? { title: message.subject } : null;},'cos.task': async (id) => {- const { getTaskById } = await import('./cosTaskStore.js');+ const { getTaskById, firstLine } = await import('./cosTaskStore.js');const task = await getTaskById(id);- return task ? { title: task.text || task.title || task.id } : null;+ // A task's text lives in `description`, which is MULTI-LINE (the body a+ // generator folds in below line 1) — a chip gets the first line only.+ return task ? { title: firstLine(task.description) } : null;},});diff --git a/server/services/threadRefs.test.js b/server/services/threadRefs.test.jsindex d88b7d59c2..71edf2e1e2 100644--- a/server/services/threadRefs.test.js+++ b/server/services/threadRefs.test.js@@ -3,10 +3,15 @@ import { describe, it, expect, vi, beforeEach } from 'vitest';const query = vi.fn();const getById = vi.fn();const getAppById = vi.fn();+const getTaskById = vi.fn();vi.mock('../lib/db.js', () => ({ query: (...args) => query(...args) }));vi.mock('./brainStorage.js', () => ({ getById: (...args) => getById(...args) }));vi.mock('./apps.js', () => ({ getAppById: (...args) => getAppById(...args) }));+vi.mock('./cosTaskStore.js', () => ({+ getTaskById: (...args) => getTaskById(...args),+ firstLine: (s) => (s || '').split('\n').map((l) => l.trim()).find(Boolean) || '',+}));const { resolveThreadRefs } = await import('./threadRefs.js');@@ -14,9 +19,11 @@ beforeEach(() => {query.mockReset();getById.mockReset();getAppById.mockReset();+ getTaskById.mockReset();query.mockResolvedValue({ rows: [] });getById.mockResolvedValue(null);getAppById.mockResolvedValue(null);+ getTaskById.mockResolvedValue(null);});describe('resolveThreadRefs — hydration', () => {@@ -55,6 +62,13 @@ describe('resolveThreadRefs — hydration', () => {expect(ref).toMatchObject({ label: 'PortOS', url: '/apps/app-1', resolved: true });});+ it('labels a CoS task with the first line of its description', async () => {+ // A task's text lives in `description` (there is no `text` field), and that+ // description is multi-line — a generator folds the body in below line 1.+ getTaskById.mockResolvedValue({ id: 'task-1', description: 'Renew the cert\nlong body here' });+ const [ref] = await resolveThreadRefs([{ kind: 'cos.task', id: 'task-1' }]);+ expect(ref).toMatchObject({ label: 'Renew the cert', url: '/cos/tasks', resolved: true });+ });it('keys a journal off its date id, which is the only name it has', async () => {getById.mockResolvedValue({ id: '2026-09-19', entries: [] });const [ref] = await resolveThreadRefs([{ kind: 'brain.journal', id: '2026-09-19' }]);