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

fix([issue-781]): address codex+claude review — budget hole, stale suggestions, selection cap

[._.]/ Adam Eivy8b4b080e754c
diff --git a/client/src/components/writers-room/LiveContinuationPanel.jsx b/client/src/components/writers-room/LiveContinuationPanel.jsx
index 31fa533c1c..bb728d8ebc 100644
--- a/client/src/components/writers-room/LiveContinuationPanel.jsx
+++ b/client/src/components/writers-room/LiveContinuationPanel.jsx
@@ -34,22 +34,32 @@ export default function LiveContinuationPanel({
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
- // debounce-fire before the first response lands. Ignore the request while one
- // is in flight (the next pause re-triggers anyway).
+ // Overlapping-call control. A fast typist can re-trigger the debounce before
+ // the first response lands; rather than dropping the newer request and then
+ // applying the OLDER (now-stale) response against a moved cursor, we:
+ // - tag every request with a monotonic generation and ignore a response
+ // whose generation is no longer current (staleness guard), and
+ // - remember that a re-run was requested mid-flight and fire it once the
+ // current call settles, so the latest cursor context always wins.
const inFlightRef = useRef(false);
+ const genRef = useRef(0);
+ const rerunPendingRef = useRef(false);
const requestSuggest = useCallback(async () => {
- if (inFlightRef.current) return;
+ if (inFlightRef.current) { rerunPendingRef.current = true; return; }
const ctx = getCursorContext?.();
if (!ctx || (!ctx.before?.trim() && !ctx.after?.trim() && !ctx.selection?.trim())) return;
+ const gen = ++genRef.current;
inFlightRef.current = true;
setLoading(true);
setNotice(null);
+ // Drop stale options up front — a new request against a moved cursor must
+ // not leave the previous suggestions visible/insertable while it runs.
+ setOptions([]);
const res = await suggestWritersRoomContinuation(workId, ctx, { silent: true }).catch((err) => {
// 429 budget / 409 off are expected control-flow, not crashes — show them
// inline rather than as a red toast.
- if (mountedRef.current) {
+ if (mountedRef.current && gen === genRef.current) {
if (err?.status === 429) setNotice('Daily suggestion budget reached — resets at UTC midnight.');
else if (err?.status === 409) setNotice('Live mode is off for this work.');
else toast.error(`Suggestion failed: ${err.message}`);
@@ -57,7 +67,14 @@ export default function LiveContinuationPanel({
return null;
});
inFlightRef.current = false;
- if (!mountedRef.current) return;
+ // A newer request arrived while this one was in flight — discard this
+ // (stale) result and re-run with the latest cursor context.
+ const superseded = gen !== genRef.current || rerunPendingRef.current;
+ if (rerunPendingRef.current) {
+ rerunPendingRef.current = false;
+ if (mountedRef.current) requestSuggest();
+ }
+ if (!mountedRef.current || superseded) return;
setLoading(false);
if (!res) return;
setOptions(res.options || []);
diff --git a/client/src/components/writers-room/WorkEditor.jsx b/client/src/components/writers-room/WorkEditor.jsx
index dcd1dad822..85d5632389 100644
--- a/client/src/components/writers-room/WorkEditor.jsx
+++ b/client/src/components/writers-room/WorkEditor.jsx
@@ -463,10 +463,13 @@ export default function WorkEditor({ work, onChange, onToggleExercise, exerciseO
const start = ta.selectionStart ?? body.length;
const end = ta.selectionEnd ?? start;
const WINDOW = 4000;
+ // Clamp each slice to the server's per-field caps (before/after 12k,
+ // selection 8k) so a huge selection can't 400 the request — the schema
+ // would reject it and surface as a red toast instead of a graceful notice.
return {
before: body.slice(Math.max(0, start - WINDOW), start),
after: body.slice(end, end + WINDOW),
- selection: body.slice(start, end),
+ selection: body.slice(start, end).slice(0, 8000),
};
}, [body]);
diff --git a/server/services/writersRoom/liveDirector.js b/server/services/writersRoom/liveDirector.js
index 39c2f30a5f..ea7d713bc7 100644
--- a/server/services/writersRoom/liveDirector.js
+++ b/server/services/writersRoom/liveDirector.js
@@ -84,13 +84,13 @@ export async function suggestContinuation(workId, { before = '', after = '', sel
});
const options = shapeOptions(content);
- // Only spend budget on a call that actually produced something — a parse
- // that yields zero options shouldn't burn the writer's daily allowance.
- // recordLiveModeUsage returns the full resolved config; we surface just the
- // usage sub-object so the response shape is identical on both branches.
- const usage = options.length > 0
- ? (await recordLiveModeUsage(workId)).usage
- : live.usage;
+ // Charge the budget for every call that reached the LLM — the provider cost
+ // is incurred whether or not the response parsed into usable options. Only
+ // sparing a zero-option call would let a model that reliably returns garbage
+ // (or a prompt that always parses empty) run unbounded calls and never hit
+ // the 429 cap. recordLiveModeUsage returns the full resolved config; we
+ // surface just the usage sub-object.
+ const usage = (await recordLiveModeUsage(workId)).usage;
return {
options,
diff --git a/server/services/writersRoom/liveDirector.test.js b/server/services/writersRoom/liveDirector.test.js
index 74ce04e9eb..a1094627a1 100644
--- a/server/services/writersRoom/liveDirector.test.js
+++ b/server/services/writersRoom/liveDirector.test.js
@@ -58,14 +58,17 @@ describe('suggestContinuation', () => {
expect(runStagedLLM).toHaveBeenCalledOnce();
});
- it('does not spend budget when the model returns zero usable options', async () => {
+ it('charges budget even when the model returns zero usable options', async () => {
+ // The LLM cost is incurred regardless of whether the response parsed into
+ // usable options — sparing zero-option calls would open an unbounded-call
+ // hole that never reaches the 429 cap.
runStagedLLM.mockResolvedValue({ content: { options: [] } });
const work = await createWork({ title: 'Empty' });
await updateWork(work.id, { liveMode: { enabled: true } });
const res = await suggestContinuation(work.id, { before: 'Nothing comes of this.' });
expect(res.options).toHaveLength(0);
- expect(res.usage.count).toBe(0); // counter untouched
+ expect(res.usage.count).toBe(1); // call reached the LLM → budget charged
});
it('enforces the daily budget and rejects with BUDGET_EXCEEDED once spent', async () => {