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 14): dedupe-guard turn append, invert mode-set ownership, guard SSE write

Adam Eivy51a3619077ea
diff --git a/client/src/pages/Ask.jsx b/client/src/pages/Ask.jsx
index 00671568f1..cabd071d05 100644
--- a/client/src/pages/Ask.jsx
+++ b/client/src/pages/Ask.jsx
@@ -335,10 +335,17 @@ export default function Ask() {
// 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
+ // GET'd the conversation after the server persisted the turn, so the
+ // turn id can already be present in `prev.turns`.
if (persistedTurn && serverConvId) {
setActiveConv((prev) => {
if (!prev || (prev.id !== 'pending' && prev.id !== serverConvId)) return prev;
- return { ...prev, id: serverConvId, turns: [...(prev.turns || []), persistedTurn], updatedAt: persistedTurn.createdAt };
+ const existingTurns = prev.turns || [];
+ if (existingTurns.some((t) => t.id === persistedTurn.id)) {
+ return prev.id === serverConvId ? prev : { ...prev, id: serverConvId };
+ }
+ return { ...prev, id: serverConvId, turns: [...existingTurns, persistedTurn], updatedAt: persistedTurn.createdAt };
});
}
refreshList();
diff --git a/server/routes/ask.js b/server/routes/ask.js
index fa271513f9..26d69ee6a9 100644
--- a/server/routes/ask.js
+++ b/server/routes/ask.js
@@ -133,10 +133,23 @@ router.post('/', asyncHandler(async (req, res) => {
// `close`) before queuing more frames. Both listeners are torn down on
// settle so a slow client that disconnects mid-drain doesn't leak
// listeners.
+ //
+ // Guard the write itself: between the `aborted` check and the syscall,
+ // the socket can transition to destroyed, in which case `res.write` would
+ // throw `ERR_STREAM_WRITE_AFTER_END`. Treat any throw as an abort so the
+ // disconnect doesn't bubble out as a 500 after we've already flushed
+ // SSE headers (which would also surface as ERR_HTTP_HEADERS_SENT noise).
const send = async (event, data) => {
- if (aborted) return;
+ if (aborted || res.writableEnded || res.destroyed) return;
const frame = `event: ${event}\ndata: ${JSON.stringify(data)}\n\n`;
- if (!res.write(frame)) {
+ let writeOk;
+ try {
+ writeOk = res.write(frame);
+ } catch {
+ onClose();
+ return;
+ }
+ if (!writeOk) {
await new Promise((resolve) => {
const settle = () => {
res.off('drain', settle);
diff --git a/server/services/askConversations.js b/server/services/askConversations.js
index ccef0a4cf3..b1750130ea 100644
--- a/server/services/askConversations.js
+++ b/server/services/askConversations.js
@@ -27,12 +27,15 @@ import { join } from 'path';
import { readdir, stat, unlink } from 'fs/promises';
import { randomUUID } from 'crypto';
import { PATHS, atomicWrite, ensureDir, readJSONFile, safeDate } from '../lib/fileUtils.js';
-import { VALID_MODES as VALID_MODES_SET } from './askService.js';
export const ASK_DIR = join(PATHS.data, 'ask-conversations');
export const EXPIRY_DAYS = 30;
export const TITLE_MAX_LENGTH = 120;
-export { VALID_MODES_SET as VALID_MODES };
+// Owned here (the leaf storage module) and consumed by askService. Keeping
+// this declaration on the storage side means the persistence layer has no
+// upstream imports — a fresh test can mount this file alone without pulling
+// the retrieval/provider stack via askService.
+export const VALID_MODES = new Set(['ask', 'advise', 'draft']);
const ID_RE = /^ask_[a-z0-9]+_[a-f0-9]+$/;
@@ -153,7 +156,7 @@ export async function listConversations({ limit = 50 } = {}) {
}
export async function createConversation({ mode = 'ask', title = '' } = {}) {
- if (!VALID_MODES_SET.has(mode)) throw new Error(`Invalid mode: ${mode}`);
+ if (!VALID_MODES.has(mode)) throw new Error(`Invalid mode: ${mode}`);
await ensureDir(ASK_DIR);
const now = new Date().toISOString();
const conv = {
diff --git a/server/services/askService.js b/server/services/askService.js
index f72d889256..bd57e5d63e 100644
--- a/server/services/askService.js
+++ b/server/services/askService.js
@@ -28,8 +28,11 @@ import { getGoals } from './identity.js';
import { getCharacter } from './character.js';
import { getEvents as getCalendarEvents } from './calendarSync.js';
import { tokenize as bm25Tokenize, STOP_WORDS } from '../lib/bm25.js';
+import { VALID_MODES as STORAGE_VALID_MODES } from './askConversations.js';
-export const VALID_MODES = new Set(['ask', 'advise', 'draft']);
+// Re-export so the route can keep importing modes via askService — but
+// askConversations is the source of truth (it owns the persistence schema).
+export const VALID_MODES = STORAGE_VALID_MODES;
export const SOURCE_KINDS = ['memory', 'brain-note', 'autobiography', 'goal', 'calendar'];
const PER_SOURCE_LIMIT = {