fix(chat): resolve ChatManager before flushing SSE headers

Move resolveScopedChatManager() before res.flushHeaders() in the
POST /messages route so that failures (e.g. project DB cannot be
opened) produce a proper HTTP error instead of silently closing the
SSE connection.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
gsxdsm
2026-05-26 21:44:12 -07:00
parent e721d8481a
commit 0cabe582aa

View File

@@ -566,6 +566,10 @@ export function registerChatRoutes(ctx: ApiRoutesContext, deps: ChatRouteDeps):
throw notFound(`Chat session ${sessionId} not found`);
}
// Resolve per-project ChatManager before opening the SSE stream so
// failures (e.g. project DB cannot be opened) produce a proper HTTP error.
const chatManager = await resolveScopedChatManager(req.query.projectId as string | undefined);
// Set SSE headers
res.setHeader("Content-Type", "text/event-stream");
res.setHeader("Cache-Control", "no-cache");
@@ -602,9 +606,6 @@ export function registerChatRoutes(ctx: ApiRoutesContext, deps: ChatRouteDeps):
}
}
// Resolve per-project ChatManager (falls back to global when no projectId)
const chatManager = await resolveScopedChatManager(req.query.projectId as string | undefined);
// Allocate a generation up front so subscription and sendMessage broadcasts
// share the same id. This filters out stragglers from a prior, just-cancelled
// generation that would otherwise hit this fresh subscriber and falsely look