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

refactor([issue-781]): share utcDayKey, re-sync live-panel usage from prop

[._.]/ Adam Eivy33c4e91562da
diff --git a/client/src/components/writers-room/LiveContinuationPanel.jsx b/client/src/components/writers-room/LiveContinuationPanel.jsx
index b1dc6360a7..31fa533c1c 100644
--- a/client/src/components/writers-room/LiveContinuationPanel.jsx
+++ b/client/src/components/writers-room/LiveContinuationPanel.jsx
@@ -26,7 +26,12 @@ export default function LiveContinuationPanel({
}) {
const [options, setOptions] = useState([]);
const [loading, setLoading] = useState(false);
+ // Local usage so a fresh suggest updates the "N left today" readout
+ // immediately (the response carries the new count, which we don't push up to
+ // the parent's work manifest). Re-synced from the prop below so a
+ // parent-driven liveMode change (budget edit, work swap) isn't shadowed.
const [usage, setUsage] = useState(liveMode?.usage || null);
+ useEffect(() => { setUsage(liveMode?.usage || null); }, [liveMode?.usage]);
const [notice, setNotice] = useState(null);
const mountedRef = useMounted();
// Guard against overlapping suggest calls: a fast typist can trigger a second
diff --git a/server/services/writersRoom/liveDirector.js b/server/services/writersRoom/liveDirector.js
index 0183a441d0..39c2f30a5f 100644
--- a/server/services/writersRoom/liveDirector.js
+++ b/server/services/writersRoom/liveDirector.js
@@ -15,7 +15,7 @@
import { ServerError } from '../../lib/errorHandler.js';
import { runStagedLLM } from '../../lib/stageRunner.js';
-import { getWork, resolveLiveMode, recordLiveModeUsage } from './local.js';
+import { getWork, resolveLiveMode, recordLiveModeUsage, utcDayKey } from './local.js';
import { badRequest } from './_shared.js';
const STAGE = 'writers-room-continue';
@@ -58,8 +58,9 @@ export async function suggestContinuation(workId, { before = '', after = '', sel
// inside recordLiveModeUsage; here we just compare today's count against the
// cap before spending an LLM call. Reading the resolved usage (which is not
// date-normalized) would let yesterday's count block today, so treat a
- // stale-date counter as 0 spent-today.
- const today = new Date().toISOString().slice(0, 10);
+ // stale-date counter as 0 spent-today. Same utcDayKey as the writer so the
+ // boundaries can't drift.
+ const today = utcDayKey();
const spentToday = live.usage.date === today ? live.usage.count : 0;
if (live.dailyCallBudget > 0 && spentToday >= live.dailyCallBudget) {
throw new ServerError(
diff --git a/server/services/writersRoom/local.js b/server/services/writersRoom/local.js
index 5f570a7d3e..8480a7a264 100644
--- a/server/services/writersRoom/local.js
+++ b/server/services/writersRoom/local.js
@@ -397,7 +397,9 @@ export async function linkToPipeline(id, { seriesId = null, issueId = null } = {
// UTC day key (YYYY-MM-DD) for the daily budget window. UTC (not local) so the
// reset boundary is deterministic across machines a single user federates.
-function utcDayKey() {
+// Exported so the live-director's pre-call budget check compares against the
+// same boundary recordLiveModeUsage rolls over on.
+export function utcDayKey() {
return nowIso().slice(0, 10);
}