fix: Simplify notification logic - use direct or file, never both

- Check upfront if we have listeners for direct notifications
- Use EITHER direct notifications OR file watcher, not both
- This eliminates any possibility of duplicate broadcasts
- Server sessions get instant updates via direct notifications
- Forwarded sessions use optimized file watcher

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
Mario Zechner 2025-06-22 20:41:13 +02:00
parent 52feefddf2
commit c43a549ed8

View file

@ -206,12 +206,15 @@ export class StreamWatcher {
* Start watching a file for changes * Start watching a file for changes
*/ */
private startWatching(sessionId: string, streamPath: string, watcherInfo: WatcherInfo): void { private startWatching(sessionId: string, streamPath: string, watcherInfo: WatcherInfo): void {
// First, set up direct notification listener for lowest latency // Check if we should use direct notifications or file watching
let hasDirectNotifications = false; const hasListeners = streamNotifier.hasListeners(sessionId);
if (hasListeners) {
console.log(`[STREAM] Using direct notifications for session ${sessionId}`);
// Set up direct notification listener for lowest latency
watcherInfo.notificationListener = (update) => { watcherInfo.notificationListener = (update) => {
if (update.sessionId === sessionId) { if (update.sessionId === sessionId) {
hasDirectNotifications = true;
// Process the notification data directly // Process the notification data directly
const lines = update.data.split('\n').filter((line) => line.trim()); const lines = update.data.split('\n').filter((line) => line.trim());
for (const line of lines) { for (const line of lines) {
@ -220,15 +223,10 @@ export class StreamWatcher {
} }
}; };
streamNotifier.on('stream-update', watcherInfo.notificationListener); streamNotifier.on('stream-update', watcherInfo.notificationListener);
} else {
console.log(`[STREAM] Using file watcher for session ${sessionId} (cross-process)`);
// Only use file watcher if we're not getting direct notifications // Use optimized file watcher for cross-process scenarios
// Give it a moment to see if we get direct notifications
setTimeout(() => {
if (!hasDirectNotifications) {
console.log(
`[STREAM] No direct notifications for session ${sessionId}, using file watcher`
);
// Use optimized file watcher as fallback (for cross-process scenarios)
watcherInfo.watcher = new OptimizedFileWatcher(streamPath, { persistent: true }); watcherInfo.watcher = new OptimizedFileWatcher(streamPath, { persistent: true });
watcherInfo.watcher.on('change', (stats) => { watcherInfo.watcher.on('change', (stats) => {
@ -269,9 +267,6 @@ export class StreamWatcher {
// Start the watcher // Start the watcher
watcherInfo.watcher.start(); watcherInfo.watcher.start();
} }
}, 100); // Wait 100ms to see if we get direct notifications
console.log(`[STREAM] Started watching for session ${sessionId}`);
} }
/** /**