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

address review (round 21): roll back optimistic state on pre-open failure, fix base36 math comment

Adam Eivy12e46b8789a9
diff --git a/client/src/pages/Ask.jsx b/client/src/pages/Ask.jsx
index 17821bc5a3..f7195f6348 100644
--- a/client/src/pages/Ask.jsx
+++ b/client/src/pages/Ask.jsx
@@ -300,6 +300,12 @@ export default function Ask() {
setStreamingTurn({ content: chunks.join(''), sources: collectedSources });
};
let persistedTurn = null;
+ // Tracks whether the SSE stream actually opened — until the first 'open'
+ // event lands, the optimistic state isn't backed by a server record. A
+ // network/4xx failure before then must roll back local state so a refresh
+ // doesn't ghost a turn that was never persisted.
+ let streamOpened = false;
+ let streamFailure = null;
await api.streamAskTurn(
{ conversationId: persistedConvId, question: trimmed, mode },
@@ -310,6 +316,7 @@ export default function Ask() {
// otherwise we'd write into a stale conversation's local state.
if (controller.signal.aborted) return;
if (event === 'open') {
+ streamOpened = true;
serverConvId = data.conversationId;
// Pin the stream to its conversation id so the navigate() below
// (which fires on first send) doesn't get treated as a
@@ -332,7 +339,10 @@ export default function Ask() {
},
},
).catch((err) => {
- if (err.name !== 'AbortError') toast.error(err.message || 'Ask failed');
+ if (err.name !== 'AbortError') {
+ streamFailure = err;
+ toast.error(err.message || 'Ask failed');
+ }
});
// Cancel any rAF queued after the last delta so a flush can't race with
@@ -351,6 +361,25 @@ export default function Ask() {
// updates would land in a stale conversation.
if (wasAborted) return;
+ // If the stream failed before the server even acknowledged it (network
+ // error, 4xx, transport hiccup), roll back the optimistic state — the
+ // user turn was never persisted, so leaving it in local state would
+ // ghost it on refresh / cause a duplicate when the user retries.
+ if (streamFailure && !streamOpened) {
+ setActiveConv((prev) => {
+ if (!prev) return prev;
+ // Brand-new 'pending' conversation: drop the whole conversation so
+ // the empty state returns; the user can retry without artifacts.
+ if (prev.id === 'pending') return null;
+ // Existing conversation: pop just the optimistic user turn.
+ const turns = (prev.turns || []).filter((t) => t.id !== optimisticUserTurn.id);
+ return { ...prev, turns };
+ });
+ // Restore the question so the user can retry without re-typing.
+ setQuestion(trimmed);
+ return;
+ }
+
// Append the persisted assistant turn locally rather than refetching the
// whole conversation — server returned the canonical record in 'done'.
// Guard against double-append: the URL-change effect may have already
diff --git a/server/services/askConversations.js b/server/services/askConversations.js
index a6b8e3500e..e8a2f9fb18 100644
--- a/server/services/askConversations.js
+++ b/server/services/askConversations.js
@@ -37,7 +37,7 @@ export const TITLE_MAX_LENGTH = 120;
// the retrieval/provider stack via askService.
export const VALID_MODES = new Set(['ask', 'advise', 'draft']);
-// Pad base36-ms to 9 chars (35^9 > 5e13, valid until year ~3700) so
+// Pad base36-ms to 9 chars (36^9 > 1e14, valid until year ~5100) so
// filename lexical order always matches chronological order — without
// padding, a digit rollover would make a newer id sort *before* an older
// one and the newest-first listing would silently flip. The hex suffix is