refactor: isolate chat hot paths and lazy server watchers

Split static history from live tail, stabilize chat data identities, and reduce scroll-shell churn so old viewport content stops waking on stream deltas. Also defer watcher startup and health polling so idle runtime work better matches actual usage.
This commit is contained in:
Bohdan Triapitsyn
2026-04-05 15:36:11 +03:00
parent fc44465c00
commit 4663abd6f3
17 changed files with 1053 additions and 306 deletions
@@ -26,6 +26,7 @@ export const registerNotificationRoutes = (app, dependencies) => {
const {
uiAuthController,
ensurePushInitialized,
ensureGlobalWatcherStarted,
getOrCreateVapidKeys,
getUiSessionTokenFromRequest,
readSettingsFromDiskMigrated,
@@ -45,6 +46,17 @@ export const registerNotificationRoutes = (app, dependencies) => {
setPushInitialized,
} = dependencies;
const ensureSessionWatcher = async () => {
if (typeof ensureGlobalWatcherStarted !== 'function') {
return;
}
try {
await ensureGlobalWatcherStarted();
} catch (error) {
console.warn('[OpenCodeWatcher] lazy start failed:', error?.message ?? error);
}
};
app.get('/api/push/vapid-public-key', async (_req, res) => {
try {
await ensurePushInitialized();
@@ -58,6 +70,7 @@ export const registerNotificationRoutes = (app, dependencies) => {
app.post('/api/push/subscribe', async (req, res) => {
await ensurePushInitialized();
await ensureSessionWatcher();
const uiToken = uiAuthController?.ensureSessionToken
? await uiAuthController.ensureSessionToken(req, res)
@@ -146,10 +159,12 @@ export const registerNotificationRoutes = (app, dependencies) => {
});
app.get('/api/session-activity', (_req, res) => {
void ensureSessionWatcher();
res.json(getSessionActivitySnapshot());
});
app.get('/api/sessions/snapshot', (_req, res) => {
app.get('/api/sessions/snapshot', async (_req, res) => {
await ensureSessionWatcher();
res.json({
statusSessions: getSessionStateSnapshot(),
attentionSessions: getSessionAttentionSnapshot(),
@@ -157,7 +172,8 @@ export const registerNotificationRoutes = (app, dependencies) => {
});
});
app.get('/api/sessions/status', (_req, res) => {
app.get('/api/sessions/status', async (_req, res) => {
await ensureSessionWatcher();
const snapshot = getSessionStateSnapshot();
res.json({
sessions: snapshot,
@@ -165,7 +181,8 @@ export const registerNotificationRoutes = (app, dependencies) => {
});
});
app.get('/api/sessions/:id/status', (req, res) => {
app.get('/api/sessions/:id/status', async (req, res) => {
await ensureSessionWatcher();
const sessionId = req.params.id;
const state = getSessionState(sessionId);
@@ -182,7 +199,8 @@ export const registerNotificationRoutes = (app, dependencies) => {
});
});
app.get('/api/sessions/attention', (_req, res) => {
app.get('/api/sessions/attention', async (_req, res) => {
await ensureSessionWatcher();
const snapshot = getSessionAttentionSnapshot();
res.json({
sessions: snapshot,
@@ -190,7 +208,8 @@ export const registerNotificationRoutes = (app, dependencies) => {
});
});
app.get('/api/sessions/:id/attention', (req, res) => {
app.get('/api/sessions/:id/attention', async (req, res) => {
await ensureSessionWatcher();
const sessionId = req.params.id;
const state = getSessionAttentionState(sessionId);