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
Never drop a reminder whose timer wakes early, and run plan writes one at a time
Claude8b0af8af2386
diff --git a/server/lib/humanActions.js b/server/lib/humanActions.jsindex b038d91dd2..fe69db5ceb 100644--- a/server/lib/humanActions.js+++ b/server/lib/humanActions.js@@ -22,6 +22,10 @@ export const HUMAN_ACTION_PLAN_TAG_PREFIX = 'plan:';// reminder.export const HUMAN_ACTION_CATCH_UP_MS = 24 * 60 * 60 * 1000;+// Node timers run off libuv's cached clock and can fire a millisecond or so+// before `Date.now()` reaches the due time; a step this close counts as due.+export const HUMAN_ACTION_EARLY_FIRE_MS = 1000;+const planKey = z.string().trim().min(1).max(40).regex(/^[a-z0-9]+(-[a-z0-9]+)*$/, 'planKey must be a lowercase dash slug');@@ -116,6 +120,6 @@ export function humanActionReminderDecision(thread, now = Date.now()) {const due = Date.parse(thread.dueAt);if (!Number.isFinite(due)) return null;if (thread.remindedFor === thread.dueAt) return null;- if (due > now) return { delayMs: due - now };+ if (due - now > HUMAN_ACTION_EARLY_FIRE_MS) return { delayMs: due - now };return now - due <= HUMAN_ACTION_CATCH_UP_MS ? { fire: true } : null;}diff --git a/server/lib/humanActions.test.js b/server/lib/humanActions.test.jsindex a798a1f942..c488a1a161 100644--- a/server/lib/humanActions.test.js+++ b/server/lib/humanActions.test.js@@ -58,6 +58,10 @@ describe('humanActionReminderDecision', () => {expect(humanActionReminderDecision(thread({ dueAt: '2026-10-06T17:59:00.000Z' }), NOW)).toEqual({ fire: true });});+ it('counts a timer that fires a hair before the due time as due', () => {+ expect(humanActionReminderDecision(thread({ dueAt: new Date(NOW + 1).toISOString() }), NOW)).toEqual({ fire: true });+ });+it('never repeats a reminder for the same due time, but re-arms a moved one', () => {expect(humanActionReminderDecision(thread({ remindedFor: '2026-10-06T19:00:00.000Z' }), NOW)).toBeNull();expect(humanActionReminderDecision(thread({ remindedFor: '2026-10-06T12:00:00.000Z' }), NOW)).toEqual({ delayMs: 60 * 60 * 1000 });diff --git a/server/routes/humanActions.test.js b/server/routes/humanActions.test.jsindex c470d2f117..dda0f015b1 100644--- a/server/routes/humanActions.test.js+++ b/server/routes/humanActions.test.js@@ -4,10 +4,13 @@ import { request } from '../lib/testHelper.js';import { errorMiddleware } from '../lib/errorHandler.js';// An in-memory thread store behind the brainStorage calls the service makes.+// Each call yields a turn like real disk I/O, so overlapping requests interleave.const store = new Map();+const tick = () => new Promise((resolve) => setImmediate(resolve));vi.mock('../services/brainStorage.js', () => ({- getThreads: vi.fn(async () => [...store.values()]),+ getThreads: vi.fn(async () => { await tick(); return [...store.values()]; }),createThread: vi.fn(async (data) => {+ await tick();const record = { id: `t${store.size + 1}`, ...data };store.set(record.id, record);return record;@@ -60,6 +63,16 @@ describe('/api/human-actions', () => {expect(list.body.actions.map((t) => t.title)).toEqual(['Old', 'Done', 'New']);});+ it('runs overlapping requests for the same plan one after the other, so a retry never doubles the steps', async () => {+ const body = planBody([step('Only', '2026-10-07T18:00:00Z')]);+ await Promise.all([+ request(app).post('/api/human-actions/plans').send(body),+ request(app).post('/api/human-actions/plans').send(body),+ ]);+ const list = await request(app).get('/api/human-actions?planKey=promo-example');+ expect(list.body.actions.map((t) => t.title)).toEqual(['Only']);+ });+it('refuses a step with no instructions or a due time without an offset', async () => {const noSteps = await request(app).post('/api/human-actions/plans').send(planBody([{ ...step('X', '2026-10-07T18:00:00Z'), instructions: [] }]));expect(noSteps.status).toBe(400);diff --git a/server/services/humanActionReminders.js b/server/services/humanActionReminders.jsindex fd4669d406..3d70d7b5d0 100644--- a/server/services/humanActionReminders.js+++ b/server/services/humanActionReminders.js@@ -44,13 +44,21 @@ async function fireHumanActionReminder(threadId, deps = {}) {const now = deps.now ? deps.now() : Date.now();armed.delete(threadId);let reminded = null;+ let notYet = false;// Stamp first, under the record's write queue, so two fires (a timer and a// boot catch-up) can't both notify.await storage.updateWith('threads', threadId, (fresh) => {- if (humanActionReminderDecision(fresh, now)?.fire !== true) return null;+ const decision = humanActionReminderDecision(fresh, now);+ if (decision?.fire !== true) {+ notYet = Boolean(decision?.delayMs);+ return null;+ }reminded = fresh;return { remindedFor: fresh.dueAt };});+ // Woke before its time (a moved due date, a clock adjustment): arm it again+ // rather than dropping the reminder until the next unrelated write.+ if (notYet) (deps.requeue || queueReconcile)();if (!reminded) return false;await notify({type: NOTIFICATION_TYPES.ACTION_DUE,diff --git a/server/services/humanActionReminders.test.js b/server/services/humanActionReminders.test.jsindex 91685c1861..20a274a90a 100644--- a/server/services/humanActionReminders.test.js+++ b/server/services/humanActionReminders.test.js@@ -49,6 +49,7 @@ const depsFor = (storage) => ({addNotification: vi.fn(async () => ({})),now: () => NOW,instanceId: ME,+ requeue: vi.fn(),});describe('human action reminders', () => {@@ -98,4 +99,12 @@ describe('human action reminders', () => {expect(fired.filter(Boolean)).toHaveLength(1);expect(deps.addNotification).toHaveBeenCalledTimes(1);});++ it('re-arms a step whose timer woke before it was due instead of dropping it', async () => {+ const storage = fakeStore([step({ dueAt: '2026-10-06T19:00:00.000Z' })]);+ const deps = depsFor(storage);+ expect(await fireHumanActionReminder('step-1', deps)).toBe(false);+ expect(deps.addNotification).not.toHaveBeenCalled();+ expect(deps.requeue).toHaveBeenCalledTimes(1);+ });});diff --git a/server/services/humanActions.js b/server/services/humanActions.jsindex 57d85e58da..4bf8291461 100644--- a/server/services/humanActions.js+++ b/server/services/humanActions.js@@ -20,11 +20,25 @@ import {} from '../lib/humanActions.js';import { isTerminalThreadStatus } from '../lib/brainThreads.js';+// One write at a time per plan: two overlapping requests for the same plan (an+// agent retry) would otherwise both archive the same open steps and both+// create a full set, doubling every step and reminder.+const planTails = new Map();+/*** Write a validated plan (see `humanActionPlanSchema`) as Brain threads.* Returns `{ planKey, created: [thread], replaced: number }`.*/-export async function scheduleHumanActionPlan(plan) {+export function scheduleHumanActionPlan(plan) {+ const previous = planTails.get(plan.planKey) || Promise.resolve();+ const run = previous.catch(() => {}).then(() => writePlan(plan));+ const tail = run.catch(() => {});+ planTails.set(plan.planKey, tail);+ tail.then(() => { if (planTails.get(plan.planKey) === tail) planTails.delete(plan.planKey); });+ return run;+}++async function writePlan(plan) {const tag = humanActionPlanTag(plan.planKey);const existing = (await brainStorage.getThreads()) || [];const stale = existing.filter((thread) => isHumanActionThread(thread)