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
address review (codex): keep Writers Room live-mode budget counters local across merges (#1565)
[._.]/ Adam Eivy045f7a37ff4b
diff --git a/server/lib/syncWire.js b/server/lib/syncWire.jsindex 5c26a0a0da..d452c1e675 100644--- a/server/lib/syncWire.js+++ b/server/lib/syncWire.js@@ -163,20 +163,33 @@ export function sanitizeRecordForWire(kind, record) {case 'album':case 'track':case 'creativeDirectorProject':- case 'moodBoard':+ case 'moodBoard': {+ // Persona/music/creative-director/mood-board records: like mediaCollection,+ // no `ephemeral` flag — always wire-syncable when present. Strip-then-tail-+ // re-add the soft-delete pair for byte-stable checksums. The whole record+ // is LWW-overwritten on merge (no item-union), so the wire form converges+ // byte-for-byte and feeds the conflict-journal content hash directly (no+ // scalar narrowing in contentHashForRecord). A creativeDirectorProject is+ // larger than a persona (treatment/scenes/runs) but follows the same whole-+ // record LWW contract; a moodBoard (name/description/items) is the same.+ const { deleted: _d, deletedAt: _da, ...rest } = record;+ return { ...rest, ...sanitizeSoftDeleteFields(record) };+ }case 'writersRoomWork': {- // Persona/music/creative-director/mood-board/writers-room records: like- // mediaCollection, no `ephemeral` flag — always wire-syncable when present.- // Strip-then-tail-re-add the soft-delete pair for byte-stable checksums. The- // whole record is LWW-overwritten on merge (no item-union), so the wire form- // converges byte-for-byte and feeds the conflict-journal content hash- // directly (no scalar narrowing in contentHashForRecord). A- // creativeDirectorProject is larger than a persona (treatment/scenes/runs)- // but follows the same whole-record LWW contract; a moodBoard- // (name/description/items) and a writersRoomWork (the manifest +- // decomposed draft-version METADATA in drafts[]; the .md prose bodies ride- // a separate body manifest) are the same whole-record contract.+ // A writersRoomWork is the manifest + decomposed draft-version METADATA in+ // drafts[] (the .md prose bodies ride a separate body manifest). Whole-record+ // LWW like the kinds above, BUT `liveMode.usage` / `liveMode.renderUsage` are+ // LOCAL-ONLY daily-budget counters (per machine, regenerable) — strip them+ // from the wire so (1) they never transit and reset a peer's budget on a+ // whole-record overwrite, and (2) the conflict-journal content hash stays+ // byte-stable across peers regardless of counter state (mirrors the+ // styleImageRefs strip for universes). The user-editable live-mode knobs+ // (enabled/debounceMs/dailyCallBudget/dailyRenderBudget) still sync.const { deleted: _d, deletedAt: _da, ...rest } = record;+ if (rest.liveMode && typeof rest.liveMode === 'object' && !Array.isArray(rest.liveMode)) {+ const { usage: _u, renderUsage: _ru, ...liveModeKnobs } = rest.liveMode;+ rest.liveMode = liveModeKnobs;+ }return { ...rest, ...sanitizeSoftDeleteFields(record) };}default:diff --git a/server/services/writersRoom/syncLogic.js b/server/services/writersRoom/syncLogic.jsindex 39e2b67d36..5fc1126742 100644--- a/server/services/writersRoom/syncLogic.js+++ b/server/services/writersRoom/syncLogic.js@@ -64,11 +64,27 @@ export function mergeWorkRecord(local, remoteRaw) {if (!remote) return { next: null, inserted: false, remoteWins: false, changed: false };if (!local) return { next: remote, inserted: true, remoteWins: true, changed: true };const remoteWins = compareNewerWins(remote.updatedAt, local.updatedAt);- const next = remoteWins ? remote : local;+ // The remote arrives wire-stripped of `liveMode.usage`/`renderUsage` (local-only+ // daily budgets — see sanitizeRecordForWire). When the remote wins the LWW,+ // carry the RECEIVER's own counters forward onto it so a peer's manuscript edit+ // doesn't reset this machine's budget state. When local wins, `next` is already+ // local (counters intact).+ const next = remoteWins ? preserveLocalLiveCounters(remote, local) : local;const changed = JSON.stringify(next) !== JSON.stringify(local);return { next, inserted: false, remoteWins, changed };}+/** Re-attach the receiver's local-only live-mode counters onto a winning remote. */+function preserveLocalLiveCounters(remote, local) {+ const localLive = local?.liveMode;+ if (!localLive || typeof localLive !== 'object' || Array.isArray(localLive)) return remote;+ const carried = {};+ if (localLive.usage !== undefined) carried.usage = localLive.usage;+ if (localLive.renderUsage !== undefined) carried.renderUsage = localLive.renderUsage;+ if (Object.keys(carried).length === 0) return remote;+ return { ...remote, liveMode: { ...(remote.liveMode || {}), ...carried } };+}+/*** The draft-version → file-body asset references a work carries: one entry per* draft version whose id + work id are valid path segments. Pure (no disk I/O) —diff --git a/server/services/writersRoom/syncLogic.test.js b/server/services/writersRoom/syncLogic.test.jsindex 1d5336341b..5a574b5318 100644--- a/server/services/writersRoom/syncLogic.test.js+++ b/server/services/writersRoom/syncLogic.test.js@@ -12,6 +12,7 @@ import {sanitizeWorkForSync, mergeWorkRecord, draftAssetEntries,WRITERS_ROOM_WORK_KIND, WRITERS_ROOM_DRAFT_ASSET_KIND,} from './syncLogic.js';+import { sanitizeRecordForWire } from '../../lib/syncWire.js';const WORK = 'wr-work-11111111-1111-1111-1111-111111111111';const DRAFT = 'wr-draft-22222222-2222-2222-2222-222222222222';@@ -102,6 +103,40 @@ describe('mergeWorkRecord', () => {});});+describe('live-mode counters stay local', () => {+ const live = (extra) => ({ enabled: true, debounceMs: 2500, dailyCallBudget: 100, dailyRenderBudget: 20, ...extra });++ it('sanitizeRecordForWire strips liveMode usage/renderUsage but keeps the user knobs', () => {+ const wire = sanitizeRecordForWire('writersRoomWork', work({+ liveMode: live({ usage: { date: '2026-01-02', count: 9 }, renderUsage: { date: '2026-01-02', count: 2 } }),+ }));+ expect(wire.liveMode).toEqual(live());+ expect(wire.liveMode.usage).toBeUndefined();+ expect(wire.liveMode.renderUsage).toBeUndefined();+ });++ it('mergeWorkRecord carries the receiver local counters onto a winning (wire-stripped) remote', () => {+ const local = sanitizeWorkForSync(work({+ updatedAt: '2026-01-02T00:00:00.000Z',+ liveMode: live({ usage: { date: '2026-01-02', count: 7 }, renderUsage: { date: '2026-01-02', count: 3 } }),+ }));+ // Remote arrives wire-stripped (no counters) and is newer → wins.+ const remote = work({ updatedAt: '2026-01-05T00:00:00.000Z', title: 'Edited', liveMode: live() });+ const { next, remoteWins } = mergeWorkRecord(local, remote);+ expect(remoteWins).toBe(true);+ expect(next.title).toBe('Edited');+ expect(next.liveMode.usage).toEqual({ date: '2026-01-02', count: 7 });+ expect(next.liveMode.renderUsage).toEqual({ date: '2026-01-02', count: 3 });+ });++ it('mergeWorkRecord does not crash when neither side has counters', () => {+ const local = sanitizeWorkForSync(work({ updatedAt: '2026-01-02T00:00:00.000Z' }));+ const { next, remoteWins } = mergeWorkRecord(local, work({ updatedAt: '2026-01-05T00:00:00.000Z', title: 'E' }));+ expect(remoteWins).toBe(true);+ expect(next.title).toBe('E');+ });+});+describe('draftAssetEntries', () => {it('returns one entry per valid draft with the work id', () => {const entries = draftAssetEntries(work());